Remove i860, i960, bout and aout-adobe targets
[deliverable/binutils-gdb.git] / bfd / aoutx.h
1 /* BFD semi-generic back-end for a.out binaries.
2 Copyright (C) 1990-2018 Free Software Foundation, Inc.
3 Written by Cygnus Support.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
21
22 /*
23 SECTION
24 a.out backends
25
26 DESCRIPTION
27
28 BFD supports a number of different flavours of a.out format,
29 though the major differences are only the sizes of the
30 structures on disk, and the shape of the relocation
31 information.
32
33 The support is split into a basic support file @file{aoutx.h}
34 and other files which derive functions from the base. One
35 derivation file is @file{aoutf1.h} (for a.out flavour 1), and
36 adds to the basic a.out functions support for sun3, sun4, and
37 386 a.out files, to create a target jump vector for a specific
38 target.
39
40 This information is further split out into more specific files
41 for each machine, including @file{sunos.c} for sun3 and sun4,
42 @file{newsos3.c} for the Sony NEWS, and @file{demo64.c} for a
43 demonstration of a 64 bit a.out format.
44
45 The base file @file{aoutx.h} defines general mechanisms for
46 reading and writing records to and from disk and various
47 other methods which BFD requires. It is included by
48 @file{aout32.c} and @file{aout64.c} to form the names
49 <<aout_32_swap_exec_header_in>>, <<aout_64_swap_exec_header_in>>, etc.
50
51 As an example, this is what goes on to make the back end for a
52 sun4, from @file{aout32.c}:
53
54 | #define ARCH_SIZE 32
55 | #include "aoutx.h"
56
57 Which exports names:
58
59 | ...
60 | aout_32_canonicalize_reloc
61 | aout_32_find_nearest_line
62 | aout_32_get_lineno
63 | aout_32_get_reloc_upper_bound
64 | ...
65
66 from @file{sunos.c}:
67
68 | #define TARGET_NAME "a.out-sunos-big"
69 | #define VECNAME sparc_aout_sunos_be_vec
70 | #include "aoutf1.h"
71
72 requires all the names from @file{aout32.c}, and produces the jump vector
73
74 | sparc_aout_sunos_be_vec
75
76 The file @file{host-aout.c} is a special case. It is for a large set
77 of hosts that use ``more or less standard'' a.out files, and
78 for which cross-debugging is not interesting. It uses the
79 standard 32-bit a.out support routines, but determines the
80 file offsets and addresses of the text, data, and BSS
81 sections, the machine architecture and machine type, and the
82 entry point address, in a host-dependent manner. Once these
83 values have been determined, generic code is used to handle
84 the object file.
85
86 When porting it to run on a new system, you must supply:
87
88 | HOST_PAGE_SIZE
89 | HOST_SEGMENT_SIZE
90 | HOST_MACHINE_ARCH (optional)
91 | HOST_MACHINE_MACHINE (optional)
92 | HOST_TEXT_START_ADDR
93 | HOST_STACK_END_ADDR
94
95 in the file @file{../include/sys/h-@var{XXX}.h} (for your host). These
96 values, plus the structures and macros defined in @file{a.out.h} on
97 your host system, will produce a BFD target that will access
98 ordinary a.out files on your host. To configure a new machine
99 to use @file{host-aout.c}, specify:
100
101 | TDEFAULTS = -DDEFAULT_VECTOR=host_aout_big_vec
102 | TDEPFILES= host-aout.o trad-core.o
103
104 in the @file{config/@var{XXX}.mt} file, and modify @file{configure.ac}
105 to use the
106 @file{@var{XXX}.mt} file (by setting "<<bfd_target=XXX>>") when your
107 configuration is selected. */
108
109 /* Some assumptions:
110 * Any BFD with D_PAGED set is ZMAGIC, and vice versa.
111 Doesn't matter what the setting of WP_TEXT is on output, but it'll
112 get set on input.
113 * Any BFD with D_PAGED clear and WP_TEXT set is NMAGIC.
114 * Any BFD with both flags clear is OMAGIC.
115 (Just want to make these explicit, so the conditions tested in this
116 file make sense if you're more familiar with a.out than with BFD.) */
117
118 #define KEEPIT udata.i
119
120 #include "sysdep.h"
121 #include "bfd.h"
122 #include "safe-ctype.h"
123 #include "bfdlink.h"
124
125 #include "libaout.h"
126 #include "libbfd.h"
127 #include "aout/aout64.h"
128 #include "aout/stab_gnu.h"
129 #include "aout/ar.h"
130
131 /*
132 SUBSECTION
133 Relocations
134
135 DESCRIPTION
136 The file @file{aoutx.h} provides for both the @emph{standard}
137 and @emph{extended} forms of a.out relocation records.
138
139 The standard records contain only an address, a symbol index,
140 and a type field. The extended records also have a full
141 integer for an addend. */
142
143 #ifndef CTOR_TABLE_RELOC_HOWTO
144 #define CTOR_TABLE_RELOC_IDX 2
145 #define CTOR_TABLE_RELOC_HOWTO(BFD) \
146 ((obj_reloc_entry_size (BFD) == RELOC_EXT_SIZE \
147 ? howto_table_ext : howto_table_std) \
148 + CTOR_TABLE_RELOC_IDX)
149 #endif
150
151 #ifndef MY_swap_std_reloc_in
152 #define MY_swap_std_reloc_in NAME (aout, swap_std_reloc_in)
153 #endif
154
155 #ifndef MY_swap_ext_reloc_in
156 #define MY_swap_ext_reloc_in NAME (aout, swap_ext_reloc_in)
157 #endif
158
159 #ifndef MY_swap_std_reloc_out
160 #define MY_swap_std_reloc_out NAME (aout, swap_std_reloc_out)
161 #endif
162
163 #ifndef MY_swap_ext_reloc_out
164 #define MY_swap_ext_reloc_out NAME (aout, swap_ext_reloc_out)
165 #endif
166
167 #ifndef MY_final_link_relocate
168 #define MY_final_link_relocate _bfd_final_link_relocate
169 #endif
170
171 #ifndef MY_relocate_contents
172 #define MY_relocate_contents _bfd_relocate_contents
173 #endif
174
175 #define howto_table_ext NAME (aout, ext_howto_table)
176 #define howto_table_std NAME (aout, std_howto_table)
177
178 reloc_howto_type howto_table_ext[] =
179 {
180 /* Type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone. */
181 HOWTO (RELOC_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, 0, "8", FALSE, 0, 0x000000ff, FALSE),
182 HOWTO (RELOC_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, 0, "16", FALSE, 0, 0x0000ffff, FALSE),
183 HOWTO (RELOC_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, 0, "32", FALSE, 0, 0xffffffff, FALSE),
184 HOWTO (RELOC_DISP8, 0, 0, 8, TRUE, 0, complain_overflow_signed, 0, "DISP8", FALSE, 0, 0x000000ff, FALSE),
185 HOWTO (RELOC_DISP16, 0, 1, 16, TRUE, 0, complain_overflow_signed, 0, "DISP16", FALSE, 0, 0x0000ffff, FALSE),
186 HOWTO (RELOC_DISP32, 0, 2, 32, TRUE, 0, complain_overflow_signed, 0, "DISP32", FALSE, 0, 0xffffffff, FALSE),
187 HOWTO (RELOC_WDISP30, 2, 2, 30, TRUE, 0, complain_overflow_signed, 0, "WDISP30", FALSE, 0, 0x3fffffff, FALSE),
188 HOWTO (RELOC_WDISP22, 2, 2, 22, TRUE, 0, complain_overflow_signed, 0, "WDISP22", FALSE, 0, 0x003fffff, FALSE),
189 HOWTO (RELOC_HI22, 10, 2, 22, FALSE, 0, complain_overflow_bitfield, 0, "HI22", FALSE, 0, 0x003fffff, FALSE),
190 HOWTO (RELOC_22, 0, 2, 22, FALSE, 0, complain_overflow_bitfield, 0, "22", FALSE, 0, 0x003fffff, FALSE),
191 HOWTO (RELOC_13, 0, 2, 13, FALSE, 0, complain_overflow_bitfield, 0, "13", FALSE, 0, 0x00001fff, FALSE),
192 HOWTO (RELOC_LO10, 0, 2, 10, FALSE, 0, complain_overflow_dont, 0, "LO10", FALSE, 0, 0x000003ff, FALSE),
193 HOWTO (RELOC_SFA_BASE,0, 2, 32, FALSE, 0, complain_overflow_bitfield, 0, "SFA_BASE", FALSE, 0, 0xffffffff, FALSE),
194 HOWTO (RELOC_SFA_OFF13,0, 2, 32, FALSE, 0, complain_overflow_bitfield, 0, "SFA_OFF13", FALSE, 0, 0xffffffff, FALSE),
195 HOWTO (RELOC_BASE10, 0, 2, 10, FALSE, 0, complain_overflow_dont, 0, "BASE10", FALSE, 0, 0x000003ff, FALSE),
196 HOWTO (RELOC_BASE13, 0, 2, 13, FALSE, 0, complain_overflow_signed, 0, "BASE13", FALSE, 0, 0x00001fff, FALSE),
197 HOWTO (RELOC_BASE22, 10, 2, 22, FALSE, 0, complain_overflow_bitfield, 0, "BASE22", FALSE, 0, 0x003fffff, FALSE),
198 HOWTO (RELOC_PC10, 0, 2, 10, TRUE, 0, complain_overflow_dont, 0, "PC10", FALSE, 0, 0x000003ff, TRUE),
199 HOWTO (RELOC_PC22, 10, 2, 22, TRUE, 0, complain_overflow_signed, 0, "PC22", FALSE, 0, 0x003fffff, TRUE),
200 HOWTO (RELOC_JMP_TBL, 2, 2, 30, TRUE, 0, complain_overflow_signed, 0, "JMP_TBL", FALSE, 0, 0x3fffffff, FALSE),
201 HOWTO (RELOC_SEGOFF16,0, 2, 0, FALSE, 0, complain_overflow_bitfield, 0, "SEGOFF16", FALSE, 0, 0x00000000, FALSE),
202 HOWTO (RELOC_GLOB_DAT,0, 2, 0, FALSE, 0, complain_overflow_bitfield, 0, "GLOB_DAT", FALSE, 0, 0x00000000, FALSE),
203 HOWTO (RELOC_JMP_SLOT,0, 2, 0, FALSE, 0, complain_overflow_bitfield, 0, "JMP_SLOT", FALSE, 0, 0x00000000, FALSE),
204 HOWTO (RELOC_RELATIVE,0, 2, 0, FALSE, 0, complain_overflow_bitfield, 0, "RELATIVE", FALSE, 0, 0x00000000, FALSE),
205 HOWTO (0, 0, 3, 0, FALSE, 0, complain_overflow_dont, 0, "R_SPARC_NONE",FALSE, 0, 0x00000000, TRUE),
206 HOWTO (0, 0, 3, 0, FALSE, 0, complain_overflow_dont, 0, "R_SPARC_NONE",FALSE, 0, 0x00000000, TRUE),
207 #define RELOC_SPARC_REV32 RELOC_WDISP19
208 HOWTO (RELOC_SPARC_REV32, 0, 2, 32, FALSE, 0, complain_overflow_dont, 0,"R_SPARC_REV32",FALSE, 0, 0xffffffff, FALSE),
209 };
210
211 /* Convert standard reloc records to "arelent" format (incl byte swap). */
212
213 reloc_howto_type howto_table_std[] =
214 {
215 /* type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone. */
216 HOWTO ( 0, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,0,"8", TRUE, 0x000000ff,0x000000ff, FALSE),
217 HOWTO ( 1, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,0,"16", TRUE, 0x0000ffff,0x0000ffff, FALSE),
218 HOWTO ( 2, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,0,"32", TRUE, 0xffffffff,0xffffffff, FALSE),
219 HOWTO ( 3, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,0,"64", TRUE, 0xdeaddead,0xdeaddead, FALSE),
220 HOWTO ( 4, 0, 0, 8, TRUE, 0, complain_overflow_signed, 0,"DISP8", TRUE, 0x000000ff,0x000000ff, FALSE),
221 HOWTO ( 5, 0, 1, 16, TRUE, 0, complain_overflow_signed, 0,"DISP16", TRUE, 0x0000ffff,0x0000ffff, FALSE),
222 HOWTO ( 6, 0, 2, 32, TRUE, 0, complain_overflow_signed, 0,"DISP32", TRUE, 0xffffffff,0xffffffff, FALSE),
223 HOWTO ( 7, 0, 4, 64, TRUE, 0, complain_overflow_signed, 0,"DISP64", TRUE, 0xfeedface,0xfeedface, FALSE),
224 HOWTO ( 8, 0, 2, 0, FALSE, 0, complain_overflow_bitfield,0,"GOT_REL", FALSE, 0,0x00000000, FALSE),
225 HOWTO ( 9, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,0,"BASE16", FALSE,0xffffffff,0xffffffff, FALSE),
226 HOWTO (10, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,0,"BASE32", FALSE,0xffffffff,0xffffffff, FALSE),
227 EMPTY_HOWTO (-1),
228 EMPTY_HOWTO (-1),
229 EMPTY_HOWTO (-1),
230 EMPTY_HOWTO (-1),
231 EMPTY_HOWTO (-1),
232 HOWTO (16, 0, 2, 0, FALSE, 0, complain_overflow_bitfield,0,"JMP_TABLE", FALSE, 0,0x00000000, FALSE),
233 EMPTY_HOWTO (-1),
234 EMPTY_HOWTO (-1),
235 EMPTY_HOWTO (-1),
236 EMPTY_HOWTO (-1),
237 EMPTY_HOWTO (-1),
238 EMPTY_HOWTO (-1),
239 EMPTY_HOWTO (-1),
240 EMPTY_HOWTO (-1),
241 EMPTY_HOWTO (-1),
242 EMPTY_HOWTO (-1),
243 EMPTY_HOWTO (-1),
244 EMPTY_HOWTO (-1),
245 EMPTY_HOWTO (-1),
246 EMPTY_HOWTO (-1),
247 EMPTY_HOWTO (-1),
248 HOWTO (32, 0, 2, 0, FALSE, 0, complain_overflow_bitfield,0,"RELATIVE", FALSE, 0,0x00000000, FALSE),
249 EMPTY_HOWTO (-1),
250 EMPTY_HOWTO (-1),
251 EMPTY_HOWTO (-1),
252 EMPTY_HOWTO (-1),
253 EMPTY_HOWTO (-1),
254 EMPTY_HOWTO (-1),
255 EMPTY_HOWTO (-1),
256 HOWTO (40, 0, 2, 0, FALSE, 0, complain_overflow_bitfield,0,"BASEREL", FALSE, 0,0x00000000, FALSE),
257 };
258
259 #define TABLE_SIZE(TABLE) (sizeof (TABLE) / sizeof (TABLE[0]))
260
261 reloc_howto_type *
262 NAME (aout, reloc_type_lookup) (bfd *abfd, bfd_reloc_code_real_type code)
263 {
264 #define EXT(i, j) case i: return & howto_table_ext [j]
265 #define STD(i, j) case i: return & howto_table_std [j]
266 int ext = obj_reloc_entry_size (abfd) == RELOC_EXT_SIZE;
267
268 if (code == BFD_RELOC_CTOR)
269 switch (bfd_arch_bits_per_address (abfd))
270 {
271 case 32:
272 code = BFD_RELOC_32;
273 break;
274 case 64:
275 code = BFD_RELOC_64;
276 break;
277 }
278
279 if (ext)
280 switch (code)
281 {
282 EXT (BFD_RELOC_8, 0);
283 EXT (BFD_RELOC_16, 1);
284 EXT (BFD_RELOC_32, 2);
285 EXT (BFD_RELOC_HI22, 8);
286 EXT (BFD_RELOC_LO10, 11);
287 EXT (BFD_RELOC_32_PCREL_S2, 6);
288 EXT (BFD_RELOC_SPARC_WDISP22, 7);
289 EXT (BFD_RELOC_SPARC13, 10);
290 EXT (BFD_RELOC_SPARC_GOT10, 14);
291 EXT (BFD_RELOC_SPARC_BASE13, 15);
292 EXT (BFD_RELOC_SPARC_GOT13, 15);
293 EXT (BFD_RELOC_SPARC_GOT22, 16);
294 EXT (BFD_RELOC_SPARC_PC10, 17);
295 EXT (BFD_RELOC_SPARC_PC22, 18);
296 EXT (BFD_RELOC_SPARC_WPLT30, 19);
297 EXT (BFD_RELOC_SPARC_REV32, 26);
298 default:
299 return NULL;
300 }
301 else
302 /* std relocs. */
303 switch (code)
304 {
305 STD (BFD_RELOC_8, 0);
306 STD (BFD_RELOC_16, 1);
307 STD (BFD_RELOC_32, 2);
308 STD (BFD_RELOC_8_PCREL, 4);
309 STD (BFD_RELOC_16_PCREL, 5);
310 STD (BFD_RELOC_32_PCREL, 6);
311 STD (BFD_RELOC_16_BASEREL, 9);
312 STD (BFD_RELOC_32_BASEREL, 10);
313 default:
314 return NULL;
315 }
316 }
317
318 reloc_howto_type *
319 NAME (aout, reloc_name_lookup) (bfd *abfd, const char *r_name)
320 {
321 unsigned int i, size;
322 reloc_howto_type *howto_table;
323
324 if (obj_reloc_entry_size (abfd) == RELOC_EXT_SIZE)
325 {
326 howto_table = howto_table_ext;
327 size = sizeof (howto_table_ext) / sizeof (howto_table_ext[0]);
328 }
329 else
330 {
331 howto_table = howto_table_std;
332 size = sizeof (howto_table_std) / sizeof (howto_table_std[0]);
333 }
334
335 for (i = 0; i < size; i++)
336 if (howto_table[i].name != NULL
337 && strcasecmp (howto_table[i].name, r_name) == 0)
338 return &howto_table[i];
339
340 return NULL;
341 }
342
343 /*
344 SUBSECTION
345 Internal entry points
346
347 DESCRIPTION
348 @file{aoutx.h} exports several routines for accessing the
349 contents of an a.out file, which are gathered and exported in
350 turn by various format specific files (eg sunos.c).
351 */
352
353 /*
354 FUNCTION
355 aout_@var{size}_swap_exec_header_in
356
357 SYNOPSIS
358 void aout_@var{size}_swap_exec_header_in,
359 (bfd *abfd,
360 struct external_exec *bytes,
361 struct internal_exec *execp);
362
363 DESCRIPTION
364 Swap the information in an executable header @var{raw_bytes} taken
365 from a raw byte stream memory image into the internal exec header
366 structure @var{execp}.
367 */
368
369 #ifndef NAME_swap_exec_header_in
370 void
371 NAME (aout, swap_exec_header_in) (bfd *abfd,
372 struct external_exec *bytes,
373 struct internal_exec *execp)
374 {
375 /* The internal_exec structure has some fields that are unused in this
376 configuration (IE for i960), so ensure that all such uninitialized
377 fields are zero'd out. There are places where two of these structs
378 are memcmp'd, and thus the contents do matter. */
379 memset ((void *) execp, 0, sizeof (struct internal_exec));
380 /* Now fill in fields in the execp, from the bytes in the raw data. */
381 execp->a_info = H_GET_32 (abfd, bytes->e_info);
382 execp->a_text = GET_WORD (abfd, bytes->e_text);
383 execp->a_data = GET_WORD (abfd, bytes->e_data);
384 execp->a_bss = GET_WORD (abfd, bytes->e_bss);
385 execp->a_syms = GET_WORD (abfd, bytes->e_syms);
386 execp->a_entry = GET_WORD (abfd, bytes->e_entry);
387 execp->a_trsize = GET_WORD (abfd, bytes->e_trsize);
388 execp->a_drsize = GET_WORD (abfd, bytes->e_drsize);
389 }
390 #define NAME_swap_exec_header_in NAME (aout, swap_exec_header_in)
391 #endif
392
393 /*
394 FUNCTION
395 aout_@var{size}_swap_exec_header_out
396
397 SYNOPSIS
398 void aout_@var{size}_swap_exec_header_out
399 (bfd *abfd,
400 struct internal_exec *execp,
401 struct external_exec *raw_bytes);
402
403 DESCRIPTION
404 Swap the information in an internal exec header structure
405 @var{execp} into the buffer @var{raw_bytes} ready for writing to disk.
406 */
407 void
408 NAME (aout, swap_exec_header_out) (bfd *abfd,
409 struct internal_exec *execp,
410 struct external_exec *bytes)
411 {
412 /* Now fill in fields in the raw data, from the fields in the exec struct. */
413 H_PUT_32 (abfd, execp->a_info , bytes->e_info);
414 PUT_WORD (abfd, execp->a_text , bytes->e_text);
415 PUT_WORD (abfd, execp->a_data , bytes->e_data);
416 PUT_WORD (abfd, execp->a_bss , bytes->e_bss);
417 PUT_WORD (abfd, execp->a_syms , bytes->e_syms);
418 PUT_WORD (abfd, execp->a_entry , bytes->e_entry);
419 PUT_WORD (abfd, execp->a_trsize, bytes->e_trsize);
420 PUT_WORD (abfd, execp->a_drsize, bytes->e_drsize);
421 }
422
423 /* Make all the section for an a.out file. */
424
425 bfd_boolean
426 NAME (aout, make_sections) (bfd *abfd)
427 {
428 if (obj_textsec (abfd) == NULL && bfd_make_section (abfd, ".text") == NULL)
429 return FALSE;
430 if (obj_datasec (abfd) == NULL && bfd_make_section (abfd, ".data") == NULL)
431 return FALSE;
432 if (obj_bsssec (abfd) == NULL && bfd_make_section (abfd, ".bss") == NULL)
433 return FALSE;
434 return TRUE;
435 }
436
437 /*
438 FUNCTION
439 aout_@var{size}_some_aout_object_p
440
441 SYNOPSIS
442 const bfd_target *aout_@var{size}_some_aout_object_p
443 (bfd *abfd,
444 struct internal_exec *execp,
445 const bfd_target *(*callback_to_real_object_p) (bfd *));
446
447 DESCRIPTION
448 Some a.out variant thinks that the file open in @var{abfd}
449 checking is an a.out file. Do some more checking, and set up
450 for access if it really is. Call back to the calling
451 environment's "finish up" function just before returning, to
452 handle any last-minute setup.
453 */
454
455 const bfd_target *
456 NAME (aout, some_aout_object_p) (bfd *abfd,
457 struct internal_exec *execp,
458 const bfd_target *(*callback_to_real_object_p) (bfd *))
459 {
460 struct aout_data_struct *rawptr, *oldrawptr;
461 const bfd_target *result;
462 bfd_size_type amt = sizeof (* rawptr);
463
464 rawptr = (struct aout_data_struct *) bfd_zalloc (abfd, amt);
465 if (rawptr == NULL)
466 return NULL;
467
468 oldrawptr = abfd->tdata.aout_data;
469 abfd->tdata.aout_data = rawptr;
470
471 /* Copy the contents of the old tdata struct.
472 In particular, we want the subformat, since for hpux it was set in
473 hp300hpux.c:swap_exec_header_in and will be used in
474 hp300hpux.c:callback. */
475 if (oldrawptr != NULL)
476 *abfd->tdata.aout_data = *oldrawptr;
477
478 abfd->tdata.aout_data->a.hdr = &rawptr->e;
479 /* Copy in the internal_exec struct. */
480 *(abfd->tdata.aout_data->a.hdr) = *execp;
481 execp = abfd->tdata.aout_data->a.hdr;
482
483 /* Set the file flags. */
484 abfd->flags = BFD_NO_FLAGS;
485 if (execp->a_drsize || execp->a_trsize)
486 abfd->flags |= HAS_RELOC;
487 /* Setting of EXEC_P has been deferred to the bottom of this function. */
488 if (execp->a_syms)
489 abfd->flags |= HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS;
490 if (N_DYNAMIC (execp))
491 abfd->flags |= DYNAMIC;
492
493 if (N_MAGIC (execp) == ZMAGIC)
494 {
495 abfd->flags |= D_PAGED | WP_TEXT;
496 adata (abfd).magic = z_magic;
497 }
498 else if (N_MAGIC (execp) == QMAGIC)
499 {
500 abfd->flags |= D_PAGED | WP_TEXT;
501 adata (abfd).magic = z_magic;
502 adata (abfd).subformat = q_magic_format;
503 }
504 else if (N_MAGIC (execp) == NMAGIC)
505 {
506 abfd->flags |= WP_TEXT;
507 adata (abfd).magic = n_magic;
508 }
509 else if (N_MAGIC (execp) == OMAGIC
510 || N_MAGIC (execp) == BMAGIC)
511 adata (abfd).magic = o_magic;
512 else
513 /* Should have been checked with N_BADMAG before this routine
514 was called. */
515 abort ();
516
517 bfd_get_start_address (abfd) = execp->a_entry;
518
519 obj_aout_symbols (abfd) = NULL;
520 bfd_get_symcount (abfd) = execp->a_syms / sizeof (struct external_nlist);
521
522 /* The default relocation entry size is that of traditional V7 Unix. */
523 obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
524
525 /* The default symbol entry size is that of traditional Unix. */
526 obj_symbol_entry_size (abfd) = EXTERNAL_NLIST_SIZE;
527
528 #ifdef USE_MMAP
529 bfd_init_window (&obj_aout_sym_window (abfd));
530 bfd_init_window (&obj_aout_string_window (abfd));
531 #endif
532 obj_aout_external_syms (abfd) = NULL;
533 obj_aout_external_strings (abfd) = NULL;
534 obj_aout_sym_hashes (abfd) = NULL;
535
536 if (! NAME (aout, make_sections) (abfd))
537 goto error_ret;
538
539 obj_datasec (abfd)->size = execp->a_data;
540 obj_bsssec (abfd)->size = execp->a_bss;
541
542 obj_textsec (abfd)->flags =
543 (execp->a_trsize != 0
544 ? (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_RELOC)
545 : (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS));
546 obj_datasec (abfd)->flags =
547 (execp->a_drsize != 0
548 ? (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS | SEC_RELOC)
549 : (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS));
550 obj_bsssec (abfd)->flags = SEC_ALLOC;
551
552 #ifdef THIS_IS_ONLY_DOCUMENTATION
553 /* The common code can't fill in these things because they depend
554 on either the start address of the text segment, the rounding
555 up of virtual addresses between segments, or the starting file
556 position of the text segment -- all of which varies among different
557 versions of a.out. */
558
559 /* Call back to the format-dependent code to fill in the rest of the
560 fields and do any further cleanup. Things that should be filled
561 in by the callback: */
562
563 struct exec *execp = exec_hdr (abfd);
564
565 obj_textsec (abfd)->size = N_TXTSIZE (execp);
566 /* Data and bss are already filled in since they're so standard. */
567
568 /* The virtual memory addresses of the sections. */
569 obj_textsec (abfd)->vma = N_TXTADDR (execp);
570 obj_datasec (abfd)->vma = N_DATADDR (execp);
571 obj_bsssec (abfd)->vma = N_BSSADDR (execp);
572
573 /* The file offsets of the sections. */
574 obj_textsec (abfd)->filepos = N_TXTOFF (execp);
575 obj_datasec (abfd)->filepos = N_DATOFF (execp);
576
577 /* The file offsets of the relocation info. */
578 obj_textsec (abfd)->rel_filepos = N_TRELOFF (execp);
579 obj_datasec (abfd)->rel_filepos = N_DRELOFF (execp);
580
581 /* The file offsets of the string table and symbol table. */
582 obj_str_filepos (abfd) = N_STROFF (execp);
583 obj_sym_filepos (abfd) = N_SYMOFF (execp);
584
585 /* Determine the architecture and machine type of the object file. */
586 switch (N_MACHTYPE (exec_hdr (abfd)))
587 {
588 default:
589 abfd->obj_arch = bfd_arch_obscure;
590 break;
591 }
592
593 adata (abfd)->page_size = TARGET_PAGE_SIZE;
594 adata (abfd)->segment_size = SEGMENT_SIZE;
595 adata (abfd)->exec_bytes_size = EXEC_BYTES_SIZE;
596
597 return abfd->xvec;
598
599 /* The architecture is encoded in various ways in various a.out variants,
600 or is not encoded at all in some of them. The relocation size depends
601 on the architecture and the a.out variant. Finally, the return value
602 is the bfd_target vector in use. If an error occurs, return zero and
603 set bfd_error to the appropriate error code.
604
605 Formats such as b.out, which have additional fields in the a.out
606 header, should cope with them in this callback as well. */
607 #endif /* DOCUMENTATION */
608
609 result = (*callback_to_real_object_p) (abfd);
610
611 /* Now that the segment addresses have been worked out, take a better
612 guess at whether the file is executable. If the entry point
613 is within the text segment, assume it is. (This makes files
614 executable even if their entry point address is 0, as long as
615 their text starts at zero.).
616
617 This test had to be changed to deal with systems where the text segment
618 runs at a different location than the default. The problem is that the
619 entry address can appear to be outside the text segment, thus causing an
620 erroneous conclusion that the file isn't executable.
621
622 To fix this, we now accept any non-zero entry point as an indication of
623 executability. This will work most of the time, since only the linker
624 sets the entry point, and that is likely to be non-zero for most systems. */
625
626 if (execp->a_entry != 0
627 || (execp->a_entry >= obj_textsec (abfd)->vma
628 && execp->a_entry < (obj_textsec (abfd)->vma
629 + obj_textsec (abfd)->size)
630 && execp->a_trsize == 0
631 && execp->a_drsize == 0))
632 abfd->flags |= EXEC_P;
633 #ifdef STAT_FOR_EXEC
634 else
635 {
636 struct stat stat_buf;
637
638 /* The original heuristic doesn't work in some important cases.
639 The a.out file has no information about the text start
640 address. For files (like kernels) linked to non-standard
641 addresses (ld -Ttext nnn) the entry point may not be between
642 the default text start (obj_textsec(abfd)->vma) and
643 (obj_textsec(abfd)->vma) + text size. This is not just a mach
644 issue. Many kernels are loaded at non standard addresses. */
645 if (abfd->iostream != NULL
646 && (abfd->flags & BFD_IN_MEMORY) == 0
647 && (fstat (fileno ((FILE *) (abfd->iostream)), &stat_buf) == 0)
648 && ((stat_buf.st_mode & 0111) != 0))
649 abfd->flags |= EXEC_P;
650 }
651 #endif /* STAT_FOR_EXEC */
652
653 if (result)
654 return result;
655
656 error_ret:
657 bfd_release (abfd, rawptr);
658 abfd->tdata.aout_data = oldrawptr;
659 return NULL;
660 }
661
662 /*
663 FUNCTION
664 aout_@var{size}_mkobject
665
666 SYNOPSIS
667 bfd_boolean aout_@var{size}_mkobject, (bfd *abfd);
668
669 DESCRIPTION
670 Initialize BFD @var{abfd} for use with a.out files.
671 */
672
673 bfd_boolean
674 NAME (aout, mkobject) (bfd *abfd)
675 {
676 struct aout_data_struct *rawptr;
677 bfd_size_type amt = sizeof (* rawptr);
678
679 bfd_set_error (bfd_error_system_call);
680
681 rawptr = (struct aout_data_struct *) bfd_zalloc (abfd, amt);
682 if (rawptr == NULL)
683 return FALSE;
684
685 abfd->tdata.aout_data = rawptr;
686 exec_hdr (abfd) = &(rawptr->e);
687
688 obj_textsec (abfd) = NULL;
689 obj_datasec (abfd) = NULL;
690 obj_bsssec (abfd) = NULL;
691
692 return TRUE;
693 }
694
695 /*
696 FUNCTION
697 aout_@var{size}_machine_type
698
699 SYNOPSIS
700 enum machine_type aout_@var{size}_machine_type
701 (enum bfd_architecture arch,
702 unsigned long machine,
703 bfd_boolean *unknown);
704
705 DESCRIPTION
706 Keep track of machine architecture and machine type for
707 a.out's. Return the <<machine_type>> for a particular
708 architecture and machine, or <<M_UNKNOWN>> if that exact architecture
709 and machine can't be represented in a.out format.
710
711 If the architecture is understood, machine type 0 (default)
712 is always understood.
713 */
714
715 enum machine_type
716 NAME (aout, machine_type) (enum bfd_architecture arch,
717 unsigned long machine,
718 bfd_boolean *unknown)
719 {
720 enum machine_type arch_flags;
721
722 arch_flags = M_UNKNOWN;
723 *unknown = TRUE;
724
725 switch (arch)
726 {
727 case bfd_arch_sparc:
728 if (machine == 0
729 || machine == bfd_mach_sparc
730 || machine == bfd_mach_sparc_sparclite
731 || machine == bfd_mach_sparc_sparclite_le
732 || machine == bfd_mach_sparc_v8plus
733 || machine == bfd_mach_sparc_v8plusa
734 || machine == bfd_mach_sparc_v8plusb
735 || machine == bfd_mach_sparc_v8plusc
736 || machine == bfd_mach_sparc_v8plusd
737 || machine == bfd_mach_sparc_v8pluse
738 || machine == bfd_mach_sparc_v8plusv
739 || machine == bfd_mach_sparc_v8plusm
740 || machine == bfd_mach_sparc_v8plusm8
741 || machine == bfd_mach_sparc_v9
742 || machine == bfd_mach_sparc_v9a
743 || machine == bfd_mach_sparc_v9b
744 || machine == bfd_mach_sparc_v9c
745 || machine == bfd_mach_sparc_v9d
746 || machine == bfd_mach_sparc_v9e
747 || machine == bfd_mach_sparc_v9v
748 || machine == bfd_mach_sparc_v9m
749 || machine == bfd_mach_sparc_v9m8)
750 arch_flags = M_SPARC;
751 else if (machine == bfd_mach_sparc_sparclet)
752 arch_flags = M_SPARCLET;
753 break;
754
755 case bfd_arch_m68k:
756 switch (machine)
757 {
758 case 0: arch_flags = M_68010; break;
759 case bfd_mach_m68000: arch_flags = M_UNKNOWN; *unknown = FALSE; break;
760 case bfd_mach_m68010: arch_flags = M_68010; break;
761 case bfd_mach_m68020: arch_flags = M_68020; break;
762 default: arch_flags = M_UNKNOWN; break;
763 }
764 break;
765
766 case bfd_arch_i386:
767 if (machine == 0
768 || machine == bfd_mach_i386_i386
769 || machine == bfd_mach_i386_i386_intel_syntax)
770 arch_flags = M_386;
771 break;
772
773 case bfd_arch_arm:
774 if (machine == 0)
775 arch_flags = M_ARM;
776 break;
777
778 case bfd_arch_mips:
779 switch (machine)
780 {
781 case 0:
782 case bfd_mach_mips3000:
783 case bfd_mach_mips3900:
784 arch_flags = M_MIPS1;
785 break;
786 case bfd_mach_mips6000:
787 arch_flags = M_MIPS2;
788 break;
789 case bfd_mach_mips4000:
790 case bfd_mach_mips4010:
791 case bfd_mach_mips4100:
792 case bfd_mach_mips4300:
793 case bfd_mach_mips4400:
794 case bfd_mach_mips4600:
795 case bfd_mach_mips4650:
796 case bfd_mach_mips8000:
797 case bfd_mach_mips9000:
798 case bfd_mach_mips10000:
799 case bfd_mach_mips12000:
800 case bfd_mach_mips14000:
801 case bfd_mach_mips16000:
802 case bfd_mach_mips16:
803 case bfd_mach_mipsisa32:
804 case bfd_mach_mipsisa32r2:
805 case bfd_mach_mipsisa32r3:
806 case bfd_mach_mipsisa32r5:
807 case bfd_mach_mipsisa32r6:
808 case bfd_mach_mips5:
809 case bfd_mach_mipsisa64:
810 case bfd_mach_mipsisa64r2:
811 case bfd_mach_mipsisa64r3:
812 case bfd_mach_mipsisa64r5:
813 case bfd_mach_mipsisa64r6:
814 case bfd_mach_mips_sb1:
815 case bfd_mach_mips_xlr:
816 /* FIXME: These should be MIPS3, MIPS4, MIPS16, MIPS32, etc. */
817 arch_flags = M_MIPS2;
818 break;
819 default:
820 arch_flags = M_UNKNOWN;
821 break;
822 }
823 break;
824
825 case bfd_arch_ns32k:
826 switch (machine)
827 {
828 case 0: arch_flags = M_NS32532; break;
829 case 32032: arch_flags = M_NS32032; break;
830 case 32532: arch_flags = M_NS32532; break;
831 default: arch_flags = M_UNKNOWN; break;
832 }
833 break;
834
835 case bfd_arch_vax:
836 *unknown = FALSE;
837 break;
838
839 case bfd_arch_cris:
840 if (machine == 0 || machine == 255)
841 arch_flags = M_CRIS;
842 break;
843
844 case bfd_arch_m88k:
845 *unknown = FALSE;
846 break;
847
848 default:
849 arch_flags = M_UNKNOWN;
850 }
851
852 if (arch_flags != M_UNKNOWN)
853 *unknown = FALSE;
854
855 return arch_flags;
856 }
857
858 /*
859 FUNCTION
860 aout_@var{size}_set_arch_mach
861
862 SYNOPSIS
863 bfd_boolean aout_@var{size}_set_arch_mach,
864 (bfd *,
865 enum bfd_architecture arch,
866 unsigned long machine);
867
868 DESCRIPTION
869 Set the architecture and the machine of the BFD @var{abfd} to the
870 values @var{arch} and @var{machine}. Verify that @var{abfd}'s format
871 can support the architecture required.
872 */
873
874 bfd_boolean
875 NAME (aout, set_arch_mach) (bfd *abfd,
876 enum bfd_architecture arch,
877 unsigned long machine)
878 {
879 if (! bfd_default_set_arch_mach (abfd, arch, machine))
880 return FALSE;
881
882 if (arch != bfd_arch_unknown)
883 {
884 bfd_boolean unknown;
885
886 NAME (aout, machine_type) (arch, machine, &unknown);
887 if (unknown)
888 return FALSE;
889 }
890
891 /* Determine the size of a relocation entry. */
892 switch (arch)
893 {
894 case bfd_arch_sparc:
895 case bfd_arch_mips:
896 obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE;
897 break;
898 default:
899 obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
900 break;
901 }
902
903 return (*aout_backend_info (abfd)->set_sizes) (abfd);
904 }
905
906 static void
907 adjust_o_magic (bfd *abfd, struct internal_exec *execp)
908 {
909 file_ptr pos = adata (abfd).exec_bytes_size;
910 bfd_vma vma = 0;
911 int pad = 0;
912
913 /* Text. */
914 obj_textsec (abfd)->filepos = pos;
915 if (!obj_textsec (abfd)->user_set_vma)
916 obj_textsec (abfd)->vma = vma;
917 else
918 vma = obj_textsec (abfd)->vma;
919
920 pos += obj_textsec (abfd)->size;
921 vma += obj_textsec (abfd)->size;
922
923 /* Data. */
924 if (!obj_datasec (abfd)->user_set_vma)
925 {
926 obj_textsec (abfd)->size += pad;
927 pos += pad;
928 vma += pad;
929 obj_datasec (abfd)->vma = vma;
930 }
931 else
932 vma = obj_datasec (abfd)->vma;
933 obj_datasec (abfd)->filepos = pos;
934 pos += obj_datasec (abfd)->size;
935 vma += obj_datasec (abfd)->size;
936
937 /* BSS. */
938 if (!obj_bsssec (abfd)->user_set_vma)
939 {
940 obj_datasec (abfd)->size += pad;
941 pos += pad;
942 vma += pad;
943 obj_bsssec (abfd)->vma = vma;
944 }
945 else
946 {
947 /* The VMA of the .bss section is set by the VMA of the
948 .data section plus the size of the .data section. We may
949 need to add padding bytes to make this true. */
950 pad = obj_bsssec (abfd)->vma - vma;
951 if (pad > 0)
952 {
953 obj_datasec (abfd)->size += pad;
954 pos += pad;
955 }
956 }
957 obj_bsssec (abfd)->filepos = pos;
958
959 /* Fix up the exec header. */
960 execp->a_text = obj_textsec (abfd)->size;
961 execp->a_data = obj_datasec (abfd)->size;
962 execp->a_bss = obj_bsssec (abfd)->size;
963 N_SET_MAGIC (execp, OMAGIC);
964 }
965
966 static void
967 adjust_z_magic (bfd *abfd, struct internal_exec *execp)
968 {
969 bfd_size_type data_pad, text_pad;
970 file_ptr text_end;
971 const struct aout_backend_data *abdp;
972 /* TRUE if text includes exec header. */
973 bfd_boolean ztih;
974
975 abdp = aout_backend_info (abfd);
976
977 /* Text. */
978 ztih = (abdp != NULL
979 && (abdp->text_includes_header
980 || obj_aout_subformat (abfd) == q_magic_format));
981 obj_textsec (abfd)->filepos = (ztih
982 ? adata (abfd).exec_bytes_size
983 : adata (abfd).zmagic_disk_block_size);
984 if (! obj_textsec (abfd)->user_set_vma)
985 {
986 /* ?? Do we really need to check for relocs here? */
987 obj_textsec (abfd)->vma = ((abfd->flags & HAS_RELOC)
988 ? 0
989 : (ztih
990 ? (abdp->default_text_vma
991 + adata (abfd).exec_bytes_size)
992 : abdp->default_text_vma));
993 text_pad = 0;
994 }
995 else
996 {
997 /* The .text section is being loaded at an unusual address. We
998 may need to pad it such that the .data section starts at a page
999 boundary. */
1000 if (ztih)
1001 text_pad = ((obj_textsec (abfd)->filepos - obj_textsec (abfd)->vma)
1002 & (adata (abfd).page_size - 1));
1003 else
1004 text_pad = ((- obj_textsec (abfd)->vma)
1005 & (adata (abfd).page_size - 1));
1006 }
1007
1008 /* Find start of data. */
1009 if (ztih)
1010 {
1011 text_end = obj_textsec (abfd)->filepos + obj_textsec (abfd)->size;
1012 text_pad += BFD_ALIGN (text_end, adata (abfd).page_size) - text_end;
1013 }
1014 else
1015 {
1016 /* Note that if page_size == zmagic_disk_block_size, then
1017 filepos == page_size, and this case is the same as the ztih
1018 case. */
1019 text_end = obj_textsec (abfd)->size;
1020 text_pad += BFD_ALIGN (text_end, adata (abfd).page_size) - text_end;
1021 text_end += obj_textsec (abfd)->filepos;
1022 }
1023 obj_textsec (abfd)->size += text_pad;
1024 text_end += text_pad;
1025
1026 /* Data. */
1027 if (!obj_datasec (abfd)->user_set_vma)
1028 {
1029 bfd_vma vma;
1030 vma = obj_textsec (abfd)->vma + obj_textsec (abfd)->size;
1031 obj_datasec (abfd)->vma = BFD_ALIGN (vma, adata (abfd).segment_size);
1032 }
1033 if (abdp && abdp->zmagic_mapped_contiguous)
1034 {
1035 asection * text = obj_textsec (abfd);
1036 asection * data = obj_datasec (abfd);
1037
1038 text_pad = data->vma - (text->vma + text->size);
1039 /* Only pad the text section if the data
1040 section is going to be placed after it. */
1041 if (text_pad > 0)
1042 text->size += text_pad;
1043 }
1044 obj_datasec (abfd)->filepos = (obj_textsec (abfd)->filepos
1045 + obj_textsec (abfd)->size);
1046
1047 /* Fix up exec header while we're at it. */
1048 execp->a_text = obj_textsec (abfd)->size;
1049 if (ztih && (!abdp || (abdp && !abdp->exec_header_not_counted)))
1050 execp->a_text += adata (abfd).exec_bytes_size;
1051 if (obj_aout_subformat (abfd) == q_magic_format)
1052 N_SET_MAGIC (execp, QMAGIC);
1053 else
1054 N_SET_MAGIC (execp, ZMAGIC);
1055
1056 /* Spec says data section should be rounded up to page boundary. */
1057 obj_datasec (abfd)->size
1058 = align_power (obj_datasec (abfd)->size,
1059 obj_bsssec (abfd)->alignment_power);
1060 execp->a_data = BFD_ALIGN (obj_datasec (abfd)->size,
1061 adata (abfd).page_size);
1062 data_pad = execp->a_data - obj_datasec (abfd)->size;
1063
1064 /* BSS. */
1065 if (!obj_bsssec (abfd)->user_set_vma)
1066 obj_bsssec (abfd)->vma = (obj_datasec (abfd)->vma
1067 + obj_datasec (abfd)->size);
1068 /* If the BSS immediately follows the data section and extra space
1069 in the page is left after the data section, fudge data
1070 in the header so that the bss section looks smaller by that
1071 amount. We'll start the bss section there, and lie to the OS.
1072 (Note that a linker script, as well as the above assignment,
1073 could have explicitly set the BSS vma to immediately follow
1074 the data section.) */
1075 if (align_power (obj_bsssec (abfd)->vma, obj_bsssec (abfd)->alignment_power)
1076 == obj_datasec (abfd)->vma + obj_datasec (abfd)->size)
1077 execp->a_bss = (data_pad > obj_bsssec (abfd)->size
1078 ? 0 : obj_bsssec (abfd)->size - data_pad);
1079 else
1080 execp->a_bss = obj_bsssec (abfd)->size;
1081 }
1082
1083 static void
1084 adjust_n_magic (bfd *abfd, struct internal_exec *execp)
1085 {
1086 file_ptr pos = adata (abfd).exec_bytes_size;
1087 bfd_vma vma = 0;
1088 int pad;
1089
1090 /* Text. */
1091 obj_textsec (abfd)->filepos = pos;
1092 if (!obj_textsec (abfd)->user_set_vma)
1093 obj_textsec (abfd)->vma = vma;
1094 else
1095 vma = obj_textsec (abfd)->vma;
1096 pos += obj_textsec (abfd)->size;
1097 vma += obj_textsec (abfd)->size;
1098
1099 /* Data. */
1100 obj_datasec (abfd)->filepos = pos;
1101 if (!obj_datasec (abfd)->user_set_vma)
1102 obj_datasec (abfd)->vma = BFD_ALIGN (vma, adata (abfd).segment_size);
1103 vma = obj_datasec (abfd)->vma;
1104
1105 /* Since BSS follows data immediately, see if it needs alignment. */
1106 vma += obj_datasec (abfd)->size;
1107 pad = align_power (vma, obj_bsssec (abfd)->alignment_power) - vma;
1108 obj_datasec (abfd)->size += pad;
1109 pos += obj_datasec (abfd)->size;
1110
1111 /* BSS. */
1112 if (!obj_bsssec (abfd)->user_set_vma)
1113 obj_bsssec (abfd)->vma = vma;
1114 else
1115 vma = obj_bsssec (abfd)->vma;
1116
1117 /* Fix up exec header. */
1118 execp->a_text = obj_textsec (abfd)->size;
1119 execp->a_data = obj_datasec (abfd)->size;
1120 execp->a_bss = obj_bsssec (abfd)->size;
1121 N_SET_MAGIC (execp, NMAGIC);
1122 }
1123
1124 bfd_boolean
1125 NAME (aout, adjust_sizes_and_vmas) (bfd *abfd)
1126 {
1127 struct internal_exec *execp = exec_hdr (abfd);
1128
1129 if (! NAME (aout, make_sections) (abfd))
1130 return FALSE;
1131
1132 if (adata (abfd).magic != undecided_magic)
1133 return TRUE;
1134
1135 obj_textsec (abfd)->size =
1136 align_power (obj_textsec (abfd)->size,
1137 obj_textsec (abfd)->alignment_power);
1138
1139 /* Rule (heuristic) for when to pad to a new page. Note that there
1140 are (at least) two ways demand-paged (ZMAGIC) files have been
1141 handled. Most Berkeley-based systems start the text segment at
1142 (TARGET_PAGE_SIZE). However, newer versions of SUNOS start the text
1143 segment right after the exec header; the latter is counted in the
1144 text segment size, and is paged in by the kernel with the rest of
1145 the text. */
1146
1147 /* This perhaps isn't the right way to do this, but made it simpler for me
1148 to understand enough to implement it. Better would probably be to go
1149 right from BFD flags to alignment/positioning characteristics. But the
1150 old code was sloppy enough about handling the flags, and had enough
1151 other magic, that it was a little hard for me to understand. I think
1152 I understand it better now, but I haven't time to do the cleanup this
1153 minute. */
1154
1155 if (abfd->flags & D_PAGED)
1156 /* Whether or not WP_TEXT is set -- let D_PAGED override. */
1157 adata (abfd).magic = z_magic;
1158 else if (abfd->flags & WP_TEXT)
1159 adata (abfd).magic = n_magic;
1160 else
1161 adata (abfd).magic = o_magic;
1162
1163 #ifdef BFD_AOUT_DEBUG /* requires gcc2 */
1164 #if __GNUC__ >= 2
1165 fprintf (stderr, "%s text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x,%x>\n",
1166 ({ char *str;
1167 switch (adata (abfd).magic)
1168 {
1169 case n_magic: str = "NMAGIC"; break;
1170 case o_magic: str = "OMAGIC"; break;
1171 case z_magic: str = "ZMAGIC"; break;
1172 default: abort ();
1173 }
1174 str;
1175 }),
1176 obj_textsec (abfd)->vma, obj_textsec (abfd)->size,
1177 obj_textsec (abfd)->alignment_power,
1178 obj_datasec (abfd)->vma, obj_datasec (abfd)->size,
1179 obj_datasec (abfd)->alignment_power,
1180 obj_bsssec (abfd)->vma, obj_bsssec (abfd)->size,
1181 obj_bsssec (abfd)->alignment_power);
1182 #endif
1183 #endif
1184
1185 switch (adata (abfd).magic)
1186 {
1187 case o_magic:
1188 adjust_o_magic (abfd, execp);
1189 break;
1190 case z_magic:
1191 adjust_z_magic (abfd, execp);
1192 break;
1193 case n_magic:
1194 adjust_n_magic (abfd, execp);
1195 break;
1196 default:
1197 abort ();
1198 }
1199
1200 #ifdef BFD_AOUT_DEBUG
1201 fprintf (stderr, " text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x>\n",
1202 obj_textsec (abfd)->vma, obj_textsec (abfd)->size,
1203 obj_textsec (abfd)->filepos,
1204 obj_datasec (abfd)->vma, obj_datasec (abfd)->size,
1205 obj_datasec (abfd)->filepos,
1206 obj_bsssec (abfd)->vma, obj_bsssec (abfd)->size);
1207 #endif
1208
1209 return TRUE;
1210 }
1211
1212 /*
1213 FUNCTION
1214 aout_@var{size}_new_section_hook
1215
1216 SYNOPSIS
1217 bfd_boolean aout_@var{size}_new_section_hook,
1218 (bfd *abfd,
1219 asection *newsect);
1220
1221 DESCRIPTION
1222 Called by the BFD in response to a @code{bfd_make_section}
1223 request.
1224 */
1225 bfd_boolean
1226 NAME (aout, new_section_hook) (bfd *abfd, asection *newsect)
1227 {
1228 /* Align to double at least. */
1229 newsect->alignment_power = bfd_get_arch_info (abfd)->section_align_power;
1230
1231 if (bfd_get_format (abfd) == bfd_object)
1232 {
1233 if (obj_textsec (abfd) == NULL && !strcmp (newsect->name, ".text"))
1234 {
1235 obj_textsec (abfd)= newsect;
1236 newsect->target_index = N_TEXT;
1237 }
1238 else if (obj_datasec (abfd) == NULL && !strcmp (newsect->name, ".data"))
1239 {
1240 obj_datasec (abfd) = newsect;
1241 newsect->target_index = N_DATA;
1242 }
1243 else if (obj_bsssec (abfd) == NULL && !strcmp (newsect->name, ".bss"))
1244 {
1245 obj_bsssec (abfd) = newsect;
1246 newsect->target_index = N_BSS;
1247 }
1248 }
1249
1250 /* We allow more than three sections internally. */
1251 return _bfd_generic_new_section_hook (abfd, newsect);
1252 }
1253
1254 bfd_boolean
1255 NAME (aout, set_section_contents) (bfd *abfd,
1256 sec_ptr section,
1257 const void * location,
1258 file_ptr offset,
1259 bfd_size_type count)
1260 {
1261 if (! abfd->output_has_begun)
1262 {
1263 if (! NAME (aout, adjust_sizes_and_vmas) (abfd))
1264 return FALSE;
1265 }
1266
1267 if (section == obj_bsssec (abfd))
1268 {
1269 bfd_set_error (bfd_error_no_contents);
1270 return FALSE;
1271 }
1272
1273 if (section != obj_textsec (abfd)
1274 && section != obj_datasec (abfd))
1275 {
1276 if (aout_section_merge_with_text_p (abfd, section))
1277 section->filepos = obj_textsec (abfd)->filepos +
1278 (section->vma - obj_textsec (abfd)->vma);
1279 else
1280 {
1281 _bfd_error_handler
1282 /* xgettext:c-format */
1283 (_("%pB: can not represent section `%pA' in a.out object file format"),
1284 abfd, section);
1285 bfd_set_error (bfd_error_nonrepresentable_section);
1286 return FALSE;
1287 }
1288 }
1289
1290 if (count != 0)
1291 {
1292 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
1293 || bfd_bwrite (location, count, abfd) != count)
1294 return FALSE;
1295 }
1296
1297 return TRUE;
1298 }
1299 \f
1300 /* Read the external symbols from an a.out file. */
1301
1302 static bfd_boolean
1303 aout_get_external_symbols (bfd *abfd)
1304 {
1305 if (obj_aout_external_syms (abfd) == NULL)
1306 {
1307 bfd_size_type count;
1308 struct external_nlist *syms;
1309 bfd_size_type amt = exec_hdr (abfd)->a_syms;
1310
1311 count = amt / EXTERNAL_NLIST_SIZE;
1312 if (count == 0)
1313 return TRUE; /* Nothing to do. */
1314
1315 #ifdef USE_MMAP
1316 if (! bfd_get_file_window (abfd, obj_sym_filepos (abfd), amt,
1317 &obj_aout_sym_window (abfd), TRUE))
1318 return FALSE;
1319 syms = (struct external_nlist *) obj_aout_sym_window (abfd).data;
1320 #else
1321 /* We allocate using malloc to make the values easy to free
1322 later on. If we put them on the objalloc it might not be
1323 possible to free them. */
1324 syms = (struct external_nlist *) bfd_malloc (amt);
1325 if (syms == NULL)
1326 return FALSE;
1327
1328 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
1329 || bfd_bread (syms, amt, abfd) != amt)
1330 {
1331 free (syms);
1332 return FALSE;
1333 }
1334 #endif
1335
1336 obj_aout_external_syms (abfd) = syms;
1337 obj_aout_external_sym_count (abfd) = count;
1338 }
1339
1340 if (obj_aout_external_strings (abfd) == NULL
1341 && exec_hdr (abfd)->a_syms != 0)
1342 {
1343 unsigned char string_chars[BYTES_IN_WORD];
1344 bfd_size_type stringsize;
1345 char *strings;
1346 bfd_size_type amt = BYTES_IN_WORD;
1347
1348 /* Get the size of the strings. */
1349 if (bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET) != 0
1350 || bfd_bread ((void *) string_chars, amt, abfd) != amt)
1351 return FALSE;
1352 stringsize = GET_WORD (abfd, string_chars);
1353 if (stringsize == 0)
1354 stringsize = 1;
1355 else if (stringsize < BYTES_IN_WORD
1356 || (size_t) stringsize != stringsize)
1357 {
1358 bfd_set_error (bfd_error_bad_value);
1359 return FALSE;
1360 }
1361
1362 #ifdef USE_MMAP
1363 if (stringsize >= BYTES_IN_WORD)
1364 {
1365 if (! bfd_get_file_window (abfd, obj_str_filepos (abfd), stringsize,
1366 &obj_aout_string_window (abfd), TRUE))
1367 return FALSE;
1368 strings = (char *) obj_aout_string_window (abfd).data;
1369 }
1370 else
1371 #endif
1372 {
1373 strings = (char *) bfd_malloc (stringsize);
1374 if (strings == NULL)
1375 return FALSE;
1376
1377 if (stringsize >= BYTES_IN_WORD)
1378 {
1379 /* Keep the string count in the buffer for convenience
1380 when indexing with e_strx. */
1381 amt = stringsize - BYTES_IN_WORD;
1382 if (bfd_bread (strings + BYTES_IN_WORD, amt, abfd) != amt)
1383 {
1384 free (strings);
1385 return FALSE;
1386 }
1387 }
1388 }
1389 /* Ensure that a zero index yields an empty string. */
1390 strings[0] = '\0';
1391
1392 strings[stringsize - 1] = 0;
1393
1394 obj_aout_external_strings (abfd) = strings;
1395 obj_aout_external_string_size (abfd) = stringsize;
1396 }
1397
1398 return TRUE;
1399 }
1400
1401 /* Translate an a.out symbol into a BFD symbol. The desc, other, type
1402 and symbol->value fields of CACHE_PTR will be set from the a.out
1403 nlist structure. This function is responsible for setting
1404 symbol->flags and symbol->section, and adjusting symbol->value. */
1405
1406 static bfd_boolean
1407 translate_from_native_sym_flags (bfd *abfd, aout_symbol_type *cache_ptr)
1408 {
1409 flagword visible;
1410
1411 if ((cache_ptr->type & N_STAB) != 0
1412 || cache_ptr->type == N_FN)
1413 {
1414 asection *sec;
1415
1416 /* This is a debugging symbol. */
1417 cache_ptr->symbol.flags = BSF_DEBUGGING;
1418
1419 /* Work out the symbol section. */
1420 switch (cache_ptr->type & N_TYPE)
1421 {
1422 case N_TEXT:
1423 case N_FN:
1424 sec = obj_textsec (abfd);
1425 break;
1426 case N_DATA:
1427 sec = obj_datasec (abfd);
1428 break;
1429 case N_BSS:
1430 sec = obj_bsssec (abfd);
1431 break;
1432 default:
1433 case N_ABS:
1434 sec = bfd_abs_section_ptr;
1435 break;
1436 }
1437
1438 cache_ptr->symbol.section = sec;
1439 cache_ptr->symbol.value -= sec->vma;
1440
1441 return TRUE;
1442 }
1443
1444 /* Get the default visibility. This does not apply to all types, so
1445 we just hold it in a local variable to use if wanted. */
1446 if ((cache_ptr->type & N_EXT) == 0)
1447 visible = BSF_LOCAL;
1448 else
1449 visible = BSF_GLOBAL;
1450
1451 switch (cache_ptr->type)
1452 {
1453 default:
1454 case N_ABS: case N_ABS | N_EXT:
1455 cache_ptr->symbol.section = bfd_abs_section_ptr;
1456 cache_ptr->symbol.flags = visible;
1457 break;
1458
1459 case N_UNDF | N_EXT:
1460 if (cache_ptr->symbol.value != 0)
1461 {
1462 /* This is a common symbol. */
1463 cache_ptr->symbol.flags = BSF_GLOBAL;
1464 cache_ptr->symbol.section = bfd_com_section_ptr;
1465 }
1466 else
1467 {
1468 cache_ptr->symbol.flags = 0;
1469 cache_ptr->symbol.section = bfd_und_section_ptr;
1470 }
1471 break;
1472
1473 case N_TEXT: case N_TEXT | N_EXT:
1474 cache_ptr->symbol.section = obj_textsec (abfd);
1475 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1476 cache_ptr->symbol.flags = visible;
1477 break;
1478
1479 /* N_SETV symbols used to represent set vectors placed in the
1480 data section. They are no longer generated. Theoretically,
1481 it was possible to extract the entries and combine them with
1482 new ones, although I don't know if that was ever actually
1483 done. Unless that feature is restored, treat them as data
1484 symbols. */
1485 case N_SETV: case N_SETV | N_EXT:
1486 case N_DATA: case N_DATA | N_EXT:
1487 cache_ptr->symbol.section = obj_datasec (abfd);
1488 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1489 cache_ptr->symbol.flags = visible;
1490 break;
1491
1492 case N_BSS: case N_BSS | N_EXT:
1493 cache_ptr->symbol.section = obj_bsssec (abfd);
1494 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1495 cache_ptr->symbol.flags = visible;
1496 break;
1497
1498 case N_SETA: case N_SETA | N_EXT:
1499 case N_SETT: case N_SETT | N_EXT:
1500 case N_SETD: case N_SETD | N_EXT:
1501 case N_SETB: case N_SETB | N_EXT:
1502 {
1503 /* This code is no longer needed. It used to be used to make
1504 the linker handle set symbols, but they are now handled in
1505 the add_symbols routine instead. */
1506 switch (cache_ptr->type & N_TYPE)
1507 {
1508 case N_SETA:
1509 cache_ptr->symbol.section = bfd_abs_section_ptr;
1510 break;
1511 case N_SETT:
1512 cache_ptr->symbol.section = obj_textsec (abfd);
1513 break;
1514 case N_SETD:
1515 cache_ptr->symbol.section = obj_datasec (abfd);
1516 break;
1517 case N_SETB:
1518 cache_ptr->symbol.section = obj_bsssec (abfd);
1519 break;
1520 }
1521
1522 cache_ptr->symbol.flags |= BSF_CONSTRUCTOR;
1523 }
1524 break;
1525
1526 case N_WARNING:
1527 /* This symbol is the text of a warning message. The next
1528 symbol is the symbol to associate the warning with. If a
1529 reference is made to that symbol, a warning is issued. */
1530 cache_ptr->symbol.flags = BSF_DEBUGGING | BSF_WARNING;
1531 cache_ptr->symbol.section = bfd_abs_section_ptr;
1532 break;
1533
1534 case N_INDR: case N_INDR | N_EXT:
1535 /* An indirect symbol. This consists of two symbols in a row.
1536 The first symbol is the name of the indirection. The second
1537 symbol is the name of the target. A reference to the first
1538 symbol becomes a reference to the second. */
1539 cache_ptr->symbol.flags = BSF_DEBUGGING | BSF_INDIRECT | visible;
1540 cache_ptr->symbol.section = bfd_ind_section_ptr;
1541 break;
1542
1543 case N_WEAKU:
1544 cache_ptr->symbol.section = bfd_und_section_ptr;
1545 cache_ptr->symbol.flags = BSF_WEAK;
1546 break;
1547
1548 case N_WEAKA:
1549 cache_ptr->symbol.section = bfd_abs_section_ptr;
1550 cache_ptr->symbol.flags = BSF_WEAK;
1551 break;
1552
1553 case N_WEAKT:
1554 cache_ptr->symbol.section = obj_textsec (abfd);
1555 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1556 cache_ptr->symbol.flags = BSF_WEAK;
1557 break;
1558
1559 case N_WEAKD:
1560 cache_ptr->symbol.section = obj_datasec (abfd);
1561 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1562 cache_ptr->symbol.flags = BSF_WEAK;
1563 break;
1564
1565 case N_WEAKB:
1566 cache_ptr->symbol.section = obj_bsssec (abfd);
1567 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1568 cache_ptr->symbol.flags = BSF_WEAK;
1569 break;
1570 }
1571
1572 return TRUE;
1573 }
1574
1575 /* Set the fields of SYM_POINTER according to CACHE_PTR. */
1576
1577 static bfd_boolean
1578 translate_to_native_sym_flags (bfd *abfd,
1579 asymbol *cache_ptr,
1580 struct external_nlist *sym_pointer)
1581 {
1582 bfd_vma value = cache_ptr->value;
1583 asection *sec;
1584 bfd_vma off;
1585
1586 /* Mask out any existing type bits in case copying from one section
1587 to another. */
1588 sym_pointer->e_type[0] &= ~N_TYPE;
1589
1590 sec = bfd_get_section (cache_ptr);
1591 off = 0;
1592
1593 if (sec == NULL)
1594 {
1595 /* This case occurs, e.g., for the *DEBUG* section of a COFF
1596 file. */
1597 _bfd_error_handler
1598 /* xgettext:c-format */
1599 (_("%pB: can not represent section for symbol `%s' in a.out "
1600 "object file format"),
1601 abfd,
1602 cache_ptr->name != NULL ? cache_ptr->name : _("*unknown*"));
1603 bfd_set_error (bfd_error_nonrepresentable_section);
1604 return FALSE;
1605 }
1606
1607 if (sec->output_section != NULL)
1608 {
1609 off = sec->output_offset;
1610 sec = sec->output_section;
1611 }
1612
1613 if (bfd_is_abs_section (sec))
1614 sym_pointer->e_type[0] |= N_ABS;
1615 else if (sec == obj_textsec (abfd))
1616 sym_pointer->e_type[0] |= N_TEXT;
1617 else if (sec == obj_datasec (abfd))
1618 sym_pointer->e_type[0] |= N_DATA;
1619 else if (sec == obj_bsssec (abfd))
1620 sym_pointer->e_type[0] |= N_BSS;
1621 else if (bfd_is_und_section (sec))
1622 sym_pointer->e_type[0] = N_UNDF | N_EXT;
1623 else if (bfd_is_ind_section (sec))
1624 sym_pointer->e_type[0] = N_INDR;
1625 else if (bfd_is_com_section (sec))
1626 sym_pointer->e_type[0] = N_UNDF | N_EXT;
1627 else
1628 {
1629 if (aout_section_merge_with_text_p (abfd, sec))
1630 sym_pointer->e_type[0] |= N_TEXT;
1631 else
1632 {
1633 _bfd_error_handler
1634 /* xgettext:c-format */
1635 (_("%pB: can not represent section `%pA' in a.out object file format"),
1636 abfd, sec);
1637 bfd_set_error (bfd_error_nonrepresentable_section);
1638 return FALSE;
1639 }
1640 }
1641
1642 /* Turn the symbol from section relative to absolute again. */
1643 value += sec->vma + off;
1644
1645 if ((cache_ptr->flags & BSF_WARNING) != 0)
1646 sym_pointer->e_type[0] = N_WARNING;
1647
1648 if ((cache_ptr->flags & BSF_DEBUGGING) != 0)
1649 sym_pointer->e_type[0] = ((aout_symbol_type *) cache_ptr)->type;
1650 else if ((cache_ptr->flags & BSF_GLOBAL) != 0)
1651 sym_pointer->e_type[0] |= N_EXT;
1652 else if ((cache_ptr->flags & BSF_LOCAL) != 0)
1653 sym_pointer->e_type[0] &= ~N_EXT;
1654
1655 if ((cache_ptr->flags & BSF_CONSTRUCTOR) != 0)
1656 {
1657 int type = ((aout_symbol_type *) cache_ptr)->type;
1658
1659 switch (type)
1660 {
1661 case N_ABS: type = N_SETA; break;
1662 case N_TEXT: type = N_SETT; break;
1663 case N_DATA: type = N_SETD; break;
1664 case N_BSS: type = N_SETB; break;
1665 }
1666 sym_pointer->e_type[0] = type;
1667 }
1668
1669 if ((cache_ptr->flags & BSF_WEAK) != 0)
1670 {
1671 int type;
1672
1673 switch (sym_pointer->e_type[0] & N_TYPE)
1674 {
1675 default:
1676 case N_ABS: type = N_WEAKA; break;
1677 case N_TEXT: type = N_WEAKT; break;
1678 case N_DATA: type = N_WEAKD; break;
1679 case N_BSS: type = N_WEAKB; break;
1680 case N_UNDF: type = N_WEAKU; break;
1681 }
1682 sym_pointer->e_type[0] = type;
1683 }
1684
1685 PUT_WORD (abfd, value, sym_pointer->e_value);
1686
1687 return TRUE;
1688 }
1689 \f
1690 /* Native-level interface to symbols. */
1691
1692 asymbol *
1693 NAME (aout, make_empty_symbol) (bfd *abfd)
1694 {
1695 bfd_size_type amt = sizeof (aout_symbol_type);
1696
1697 aout_symbol_type *new_symbol = (aout_symbol_type *) bfd_zalloc (abfd, amt);
1698 if (!new_symbol)
1699 return NULL;
1700 new_symbol->symbol.the_bfd = abfd;
1701
1702 return &new_symbol->symbol;
1703 }
1704
1705 /* Translate a set of internal symbols into external symbols. */
1706
1707 bfd_boolean
1708 NAME (aout, translate_symbol_table) (bfd *abfd,
1709 aout_symbol_type *in,
1710 struct external_nlist *ext,
1711 bfd_size_type count,
1712 char *str,
1713 bfd_size_type strsize,
1714 bfd_boolean dynamic)
1715 {
1716 struct external_nlist *ext_end;
1717
1718 ext_end = ext + count;
1719 for (; ext < ext_end; ext++, in++)
1720 {
1721 bfd_vma x;
1722
1723 x = GET_WORD (abfd, ext->e_strx);
1724 in->symbol.the_bfd = abfd;
1725
1726 /* For the normal symbols, the zero index points at the number
1727 of bytes in the string table but is to be interpreted as the
1728 null string. For the dynamic symbols, the number of bytes in
1729 the string table is stored in the __DYNAMIC structure and the
1730 zero index points at an actual string. */
1731 if (x == 0 && ! dynamic)
1732 in->symbol.name = "";
1733 else if (x < strsize)
1734 in->symbol.name = str + x;
1735 else
1736 {
1737 _bfd_error_handler
1738 (_("%pB: invalid string offset %" PRIu64 " >= %" PRIu64),
1739 abfd, (uint64_t) x, (uint64_t) strsize);
1740 bfd_set_error (bfd_error_bad_value);
1741 return FALSE;
1742 }
1743
1744 in->symbol.value = GET_SWORD (abfd, ext->e_value);
1745 in->desc = H_GET_16 (abfd, ext->e_desc);
1746 in->other = H_GET_8 (abfd, ext->e_other);
1747 in->type = H_GET_8 (abfd, ext->e_type);
1748 in->symbol.udata.p = NULL;
1749
1750 if (! translate_from_native_sym_flags (abfd, in))
1751 return FALSE;
1752
1753 if (dynamic)
1754 in->symbol.flags |= BSF_DYNAMIC;
1755 }
1756
1757 return TRUE;
1758 }
1759
1760 /* We read the symbols into a buffer, which is discarded when this
1761 function exits. We read the strings into a buffer large enough to
1762 hold them all plus all the cached symbol entries. */
1763
1764 bfd_boolean
1765 NAME (aout, slurp_symbol_table) (bfd *abfd)
1766 {
1767 struct external_nlist *old_external_syms;
1768 aout_symbol_type *cached;
1769 bfd_size_type cached_size;
1770
1771 /* If there's no work to be done, don't do any. */
1772 if (obj_aout_symbols (abfd) != NULL)
1773 return TRUE;
1774
1775 old_external_syms = obj_aout_external_syms (abfd);
1776
1777 if (! aout_get_external_symbols (abfd))
1778 return FALSE;
1779
1780 cached_size = obj_aout_external_sym_count (abfd);
1781 if (cached_size == 0)
1782 return TRUE; /* Nothing to do. */
1783
1784 cached_size *= sizeof (aout_symbol_type);
1785 cached = (aout_symbol_type *) bfd_zmalloc (cached_size);
1786 if (cached == NULL)
1787 return FALSE;
1788
1789 /* Convert from external symbol information to internal. */
1790 if (! (NAME (aout, translate_symbol_table)
1791 (abfd, cached,
1792 obj_aout_external_syms (abfd),
1793 obj_aout_external_sym_count (abfd),
1794 obj_aout_external_strings (abfd),
1795 obj_aout_external_string_size (abfd),
1796 FALSE)))
1797 {
1798 free (cached);
1799 return FALSE;
1800 }
1801
1802 bfd_get_symcount (abfd) = obj_aout_external_sym_count (abfd);
1803
1804 obj_aout_symbols (abfd) = cached;
1805
1806 /* It is very likely that anybody who calls this function will not
1807 want the external symbol information, so if it was allocated
1808 because of our call to aout_get_external_symbols, we free it up
1809 right away to save space. */
1810 if (old_external_syms == NULL
1811 && obj_aout_external_syms (abfd) != NULL)
1812 {
1813 #ifdef USE_MMAP
1814 bfd_free_window (&obj_aout_sym_window (abfd));
1815 #else
1816 free (obj_aout_external_syms (abfd));
1817 #endif
1818 obj_aout_external_syms (abfd) = NULL;
1819 }
1820
1821 return TRUE;
1822 }
1823 \f
1824 /* We use a hash table when writing out symbols so that we only write
1825 out a particular string once. This helps particularly when the
1826 linker writes out stabs debugging entries, because each different
1827 contributing object file tends to have many duplicate stabs
1828 strings.
1829
1830 This hash table code breaks dbx on SunOS 4.1.3, so we don't do it
1831 if BFD_TRADITIONAL_FORMAT is set. */
1832
1833 /* Get the index of a string in a strtab, adding it if it is not
1834 already present. */
1835
1836 static inline bfd_size_type
1837 add_to_stringtab (bfd *abfd,
1838 struct bfd_strtab_hash *tab,
1839 const char *str,
1840 bfd_boolean copy)
1841 {
1842 bfd_boolean hash;
1843 bfd_size_type str_index;
1844
1845 /* An index of 0 always means the empty string. */
1846 if (str == 0 || *str == '\0')
1847 return 0;
1848
1849 /* Don't hash if BFD_TRADITIONAL_FORMAT is set, because SunOS dbx
1850 doesn't understand a hashed string table. */
1851 hash = TRUE;
1852 if ((abfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
1853 hash = FALSE;
1854
1855 str_index = _bfd_stringtab_add (tab, str, hash, copy);
1856
1857 if (str_index != (bfd_size_type) -1)
1858 /* Add BYTES_IN_WORD to the return value to account for the
1859 space taken up by the string table size. */
1860 str_index += BYTES_IN_WORD;
1861
1862 return str_index;
1863 }
1864
1865 /* Write out a strtab. ABFD is already at the right location in the
1866 file. */
1867
1868 static bfd_boolean
1869 emit_stringtab (bfd *abfd, struct bfd_strtab_hash *tab)
1870 {
1871 bfd_byte buffer[BYTES_IN_WORD];
1872 bfd_size_type amt = BYTES_IN_WORD;
1873
1874 /* The string table starts with the size. */
1875 PUT_WORD (abfd, _bfd_stringtab_size (tab) + BYTES_IN_WORD, buffer);
1876 if (bfd_bwrite ((void *) buffer, amt, abfd) != amt)
1877 return FALSE;
1878
1879 return _bfd_stringtab_emit (abfd, tab);
1880 }
1881 \f
1882 bfd_boolean
1883 NAME (aout, write_syms) (bfd *abfd)
1884 {
1885 unsigned int count ;
1886 asymbol **generic = bfd_get_outsymbols (abfd);
1887 struct bfd_strtab_hash *strtab;
1888
1889 strtab = _bfd_stringtab_init ();
1890 if (strtab == NULL)
1891 return FALSE;
1892
1893 for (count = 0; count < bfd_get_symcount (abfd); count++)
1894 {
1895 asymbol *g = generic[count];
1896 bfd_size_type indx;
1897 struct external_nlist nsp;
1898 bfd_size_type amt;
1899
1900 indx = add_to_stringtab (abfd, strtab, g->name, FALSE);
1901 if (indx == (bfd_size_type) -1)
1902 goto error_return;
1903 PUT_WORD (abfd, indx, (bfd_byte *) nsp.e_strx);
1904
1905 if (bfd_asymbol_flavour (g) == abfd->xvec->flavour)
1906 {
1907 H_PUT_16 (abfd, aout_symbol (g)->desc, nsp.e_desc);
1908 H_PUT_8 (abfd, aout_symbol (g)->other, nsp.e_other);
1909 H_PUT_8 (abfd, aout_symbol (g)->type, nsp.e_type);
1910 }
1911 else
1912 {
1913 H_PUT_16 (abfd, 0, nsp.e_desc);
1914 H_PUT_8 (abfd, 0, nsp.e_other);
1915 H_PUT_8 (abfd, 0, nsp.e_type);
1916 }
1917
1918 if (! translate_to_native_sym_flags (abfd, g, &nsp))
1919 goto error_return;
1920
1921 amt = EXTERNAL_NLIST_SIZE;
1922 if (bfd_bwrite ((void *) &nsp, amt, abfd) != amt)
1923 goto error_return;
1924
1925 /* NB: `KEEPIT' currently overlays `udata.p', so set this only
1926 here, at the end. */
1927 g->KEEPIT = count;
1928 }
1929
1930 if (! emit_stringtab (abfd, strtab))
1931 goto error_return;
1932
1933 _bfd_stringtab_free (strtab);
1934
1935 return TRUE;
1936
1937 error_return:
1938 _bfd_stringtab_free (strtab);
1939 return FALSE;
1940 }
1941 \f
1942 long
1943 NAME (aout, canonicalize_symtab) (bfd *abfd, asymbol **location)
1944 {
1945 unsigned int counter = 0;
1946 aout_symbol_type *symbase;
1947
1948 if (!NAME (aout, slurp_symbol_table) (abfd))
1949 return -1;
1950
1951 for (symbase = obj_aout_symbols (abfd);
1952 counter++ < bfd_get_symcount (abfd);
1953 )
1954 *(location++) = (asymbol *) (symbase++);
1955 *location++ =0;
1956 return bfd_get_symcount (abfd);
1957 }
1958 \f
1959 /* Standard reloc stuff. */
1960 /* Output standard relocation information to a file in target byte order. */
1961
1962 extern void NAME (aout, swap_std_reloc_out)
1963 (bfd *, arelent *, struct reloc_std_external *);
1964
1965 void
1966 NAME (aout, swap_std_reloc_out) (bfd *abfd,
1967 arelent *g,
1968 struct reloc_std_external *natptr)
1969 {
1970 int r_index;
1971 asymbol *sym = *(g->sym_ptr_ptr);
1972 int r_extern;
1973 unsigned int r_length;
1974 int r_pcrel;
1975 int r_baserel, r_jmptable, r_relative;
1976 asection *output_section = sym->section->output_section;
1977
1978 PUT_WORD (abfd, g->address, natptr->r_address);
1979
1980 BFD_ASSERT (g->howto != NULL);
1981 r_length = g->howto->size ; /* Size as a power of two. */
1982 r_pcrel = (int) g->howto->pc_relative; /* Relative to PC? */
1983 /* XXX This relies on relocs coming from a.out files. */
1984 r_baserel = (g->howto->type & 8) != 0;
1985 r_jmptable = (g->howto->type & 16) != 0;
1986 r_relative = (g->howto->type & 32) != 0;
1987
1988 /* Name was clobbered by aout_write_syms to be symbol index. */
1989
1990 /* If this relocation is relative to a symbol then set the
1991 r_index to the symbols index, and the r_extern bit.
1992
1993 Absolute symbols can come in in two ways, either as an offset
1994 from the abs section, or as a symbol which has an abs value.
1995 check for that here. */
1996
1997 if (bfd_is_com_section (output_section)
1998 || bfd_is_abs_section (output_section)
1999 || bfd_is_und_section (output_section)
2000 /* PR gas/3041 a.out relocs against weak symbols
2001 must be treated as if they were against externs. */
2002 || (sym->flags & BSF_WEAK))
2003 {
2004 if (bfd_abs_section_ptr->symbol == sym)
2005 {
2006 /* Whoops, looked like an abs symbol, but is
2007 really an offset from the abs section. */
2008 r_index = N_ABS;
2009 r_extern = 0;
2010 }
2011 else
2012 {
2013 /* Fill in symbol. */
2014 r_extern = 1;
2015 r_index = (*(g->sym_ptr_ptr))->KEEPIT;
2016 }
2017 }
2018 else
2019 {
2020 /* Just an ordinary section. */
2021 r_extern = 0;
2022 r_index = output_section->target_index;
2023 }
2024
2025 /* Now the fun stuff. */
2026 if (bfd_header_big_endian (abfd))
2027 {
2028 natptr->r_index[0] = r_index >> 16;
2029 natptr->r_index[1] = r_index >> 8;
2030 natptr->r_index[2] = r_index;
2031 natptr->r_type[0] = ((r_extern ? RELOC_STD_BITS_EXTERN_BIG : 0)
2032 | (r_pcrel ? RELOC_STD_BITS_PCREL_BIG : 0)
2033 | (r_baserel ? RELOC_STD_BITS_BASEREL_BIG : 0)
2034 | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_BIG : 0)
2035 | (r_relative ? RELOC_STD_BITS_RELATIVE_BIG : 0)
2036 | (r_length << RELOC_STD_BITS_LENGTH_SH_BIG));
2037 }
2038 else
2039 {
2040 natptr->r_index[2] = r_index >> 16;
2041 natptr->r_index[1] = r_index >> 8;
2042 natptr->r_index[0] = r_index;
2043 natptr->r_type[0] = ((r_extern ? RELOC_STD_BITS_EXTERN_LITTLE : 0)
2044 | (r_pcrel ? RELOC_STD_BITS_PCREL_LITTLE : 0)
2045 | (r_baserel ? RELOC_STD_BITS_BASEREL_LITTLE : 0)
2046 | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_LITTLE : 0)
2047 | (r_relative ? RELOC_STD_BITS_RELATIVE_LITTLE : 0)
2048 | (r_length << RELOC_STD_BITS_LENGTH_SH_LITTLE));
2049 }
2050 }
2051
2052 /* Extended stuff. */
2053 /* Output extended relocation information to a file in target byte order. */
2054
2055 extern void NAME (aout, swap_ext_reloc_out)
2056 (bfd *, arelent *, struct reloc_ext_external *);
2057
2058 void
2059 NAME (aout, swap_ext_reloc_out) (bfd *abfd,
2060 arelent *g,
2061 struct reloc_ext_external *natptr)
2062 {
2063 int r_index;
2064 int r_extern;
2065 unsigned int r_type;
2066 bfd_vma r_addend;
2067 asymbol *sym = *(g->sym_ptr_ptr);
2068 asection *output_section = sym->section->output_section;
2069
2070 PUT_WORD (abfd, g->address, natptr->r_address);
2071
2072 r_type = (unsigned int) g->howto->type;
2073
2074 r_addend = g->addend;
2075 if ((sym->flags & BSF_SECTION_SYM) != 0)
2076 r_addend += (*(g->sym_ptr_ptr))->section->output_section->vma;
2077
2078 /* If this relocation is relative to a symbol then set the
2079 r_index to the symbols index, and the r_extern bit.
2080
2081 Absolute symbols can come in in two ways, either as an offset
2082 from the abs section, or as a symbol which has an abs value.
2083 check for that here. */
2084 if (bfd_is_abs_section (bfd_get_section (sym)))
2085 {
2086 r_extern = 0;
2087 r_index = N_ABS;
2088 }
2089 else if ((sym->flags & BSF_SECTION_SYM) == 0)
2090 {
2091 if (bfd_is_und_section (bfd_get_section (sym))
2092 || (sym->flags & BSF_GLOBAL) != 0)
2093 r_extern = 1;
2094 else
2095 r_extern = 0;
2096 r_index = (*(g->sym_ptr_ptr))->KEEPIT;
2097 }
2098 else
2099 {
2100 /* Just an ordinary section. */
2101 r_extern = 0;
2102 r_index = output_section->target_index;
2103 }
2104
2105 /* Now the fun stuff. */
2106 if (bfd_header_big_endian (abfd))
2107 {
2108 natptr->r_index[0] = r_index >> 16;
2109 natptr->r_index[1] = r_index >> 8;
2110 natptr->r_index[2] = r_index;
2111 natptr->r_type[0] = ((r_extern ? RELOC_EXT_BITS_EXTERN_BIG : 0)
2112 | (r_type << RELOC_EXT_BITS_TYPE_SH_BIG));
2113 }
2114 else
2115 {
2116 natptr->r_index[2] = r_index >> 16;
2117 natptr->r_index[1] = r_index >> 8;
2118 natptr->r_index[0] = r_index;
2119 natptr->r_type[0] = ((r_extern ? RELOC_EXT_BITS_EXTERN_LITTLE : 0)
2120 | (r_type << RELOC_EXT_BITS_TYPE_SH_LITTLE));
2121 }
2122
2123 PUT_WORD (abfd, r_addend, natptr->r_addend);
2124 }
2125
2126 /* BFD deals internally with all things based from the section they're
2127 in. so, something in 10 bytes into a text section with a base of
2128 50 would have a symbol (.text+10) and know .text vma was 50.
2129
2130 Aout keeps all it's symbols based from zero, so the symbol would
2131 contain 60. This macro subs the base of each section from the value
2132 to give the true offset from the section. */
2133
2134 #define MOVE_ADDRESS(ad) \
2135 if (r_extern) \
2136 { \
2137 /* Undefined symbol. */ \
2138 cache_ptr->sym_ptr_ptr = symbols + r_index; \
2139 cache_ptr->addend = ad; \
2140 } \
2141 else \
2142 { \
2143 /* Defined, section relative. Replace symbol with pointer to \
2144 symbol which points to section. */ \
2145 switch (r_index) \
2146 { \
2147 case N_TEXT: \
2148 case N_TEXT | N_EXT: \
2149 cache_ptr->sym_ptr_ptr = obj_textsec (abfd)->symbol_ptr_ptr; \
2150 cache_ptr->addend = ad - su->textsec->vma; \
2151 break; \
2152 case N_DATA: \
2153 case N_DATA | N_EXT: \
2154 cache_ptr->sym_ptr_ptr = obj_datasec (abfd)->symbol_ptr_ptr; \
2155 cache_ptr->addend = ad - su->datasec->vma; \
2156 break; \
2157 case N_BSS: \
2158 case N_BSS | N_EXT: \
2159 cache_ptr->sym_ptr_ptr = obj_bsssec (abfd)->symbol_ptr_ptr; \
2160 cache_ptr->addend = ad - su->bsssec->vma; \
2161 break; \
2162 default: \
2163 case N_ABS: \
2164 case N_ABS | N_EXT: \
2165 cache_ptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr; \
2166 cache_ptr->addend = ad; \
2167 break; \
2168 } \
2169 }
2170
2171 void
2172 NAME (aout, swap_ext_reloc_in) (bfd *abfd,
2173 struct reloc_ext_external *bytes,
2174 arelent *cache_ptr,
2175 asymbol **symbols,
2176 bfd_size_type symcount)
2177 {
2178 unsigned int r_index;
2179 int r_extern;
2180 unsigned int r_type;
2181 struct aoutdata *su = &(abfd->tdata.aout_data->a);
2182
2183 cache_ptr->address = (GET_SWORD (abfd, bytes->r_address));
2184
2185 /* Now the fun stuff. */
2186 if (bfd_header_big_endian (abfd))
2187 {
2188 r_index = (((unsigned int) bytes->r_index[0] << 16)
2189 | ((unsigned int) bytes->r_index[1] << 8)
2190 | bytes->r_index[2]);
2191 r_extern = (0 != (bytes->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG));
2192 r_type = ((bytes->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
2193 >> RELOC_EXT_BITS_TYPE_SH_BIG);
2194 }
2195 else
2196 {
2197 r_index = (((unsigned int) bytes->r_index[2] << 16)
2198 | ((unsigned int) bytes->r_index[1] << 8)
2199 | bytes->r_index[0]);
2200 r_extern = (0 != (bytes->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
2201 r_type = ((bytes->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
2202 >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
2203 }
2204
2205 if (r_type < TABLE_SIZE (howto_table_ext))
2206 cache_ptr->howto = howto_table_ext + r_type;
2207 else
2208 cache_ptr->howto = NULL;
2209
2210 /* Base relative relocs are always against the symbol table,
2211 regardless of the setting of r_extern. r_extern just reflects
2212 whether the symbol the reloc is against is local or global. */
2213 if (r_type == (unsigned int) RELOC_BASE10
2214 || r_type == (unsigned int) RELOC_BASE13
2215 || r_type == (unsigned int) RELOC_BASE22)
2216 r_extern = 1;
2217
2218 if (r_extern && r_index > symcount)
2219 {
2220 /* We could arrange to return an error, but it might be useful
2221 to see the file even if it is bad. */
2222 r_extern = 0;
2223 r_index = N_ABS;
2224 }
2225
2226 MOVE_ADDRESS (GET_SWORD (abfd, bytes->r_addend));
2227 }
2228
2229 void
2230 NAME (aout, swap_std_reloc_in) (bfd *abfd,
2231 struct reloc_std_external *bytes,
2232 arelent *cache_ptr,
2233 asymbol **symbols,
2234 bfd_size_type symcount)
2235 {
2236 unsigned int r_index;
2237 int r_extern;
2238 unsigned int r_length;
2239 int r_pcrel;
2240 int r_baserel, r_jmptable, r_relative;
2241 struct aoutdata *su = &(abfd->tdata.aout_data->a);
2242 unsigned int howto_idx;
2243
2244 cache_ptr->address = H_GET_32 (abfd, bytes->r_address);
2245
2246 /* Now the fun stuff. */
2247 if (bfd_header_big_endian (abfd))
2248 {
2249 r_index = (((unsigned int) bytes->r_index[0] << 16)
2250 | ((unsigned int) bytes->r_index[1] << 8)
2251 | bytes->r_index[2]);
2252 r_extern = (0 != (bytes->r_type[0] & RELOC_STD_BITS_EXTERN_BIG));
2253 r_pcrel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_PCREL_BIG));
2254 r_baserel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_BASEREL_BIG));
2255 r_jmptable= (0 != (bytes->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG));
2256 r_relative= (0 != (bytes->r_type[0] & RELOC_STD_BITS_RELATIVE_BIG));
2257 r_length = ((bytes->r_type[0] & RELOC_STD_BITS_LENGTH_BIG)
2258 >> RELOC_STD_BITS_LENGTH_SH_BIG);
2259 }
2260 else
2261 {
2262 r_index = (((unsigned int) bytes->r_index[2] << 16)
2263 | ((unsigned int) bytes->r_index[1] << 8)
2264 | bytes->r_index[0]);
2265 r_extern = (0 != (bytes->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE));
2266 r_pcrel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
2267 r_baserel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_BASEREL_LITTLE));
2268 r_jmptable= (0 != (bytes->r_type[0] & RELOC_STD_BITS_JMPTABLE_LITTLE));
2269 r_relative= (0 != (bytes->r_type[0] & RELOC_STD_BITS_RELATIVE_LITTLE));
2270 r_length = ((bytes->r_type[0] & RELOC_STD_BITS_LENGTH_LITTLE)
2271 >> RELOC_STD_BITS_LENGTH_SH_LITTLE);
2272 }
2273
2274 howto_idx = (r_length + 4 * r_pcrel + 8 * r_baserel
2275 + 16 * r_jmptable + 32 * r_relative);
2276 if (howto_idx < TABLE_SIZE (howto_table_std))
2277 {
2278 cache_ptr->howto = howto_table_std + howto_idx;
2279 if (cache_ptr->howto->type == (unsigned int) -1)
2280 cache_ptr->howto = NULL;
2281 }
2282 else
2283 cache_ptr->howto = NULL;
2284
2285 /* Base relative relocs are always against the symbol table,
2286 regardless of the setting of r_extern. r_extern just reflects
2287 whether the symbol the reloc is against is local or global. */
2288 if (r_baserel)
2289 r_extern = 1;
2290
2291 if (r_extern && r_index >= symcount)
2292 {
2293 /* We could arrange to return an error, but it might be useful
2294 to see the file even if it is bad. FIXME: Of course this
2295 means that objdump -r *doesn't* see the actual reloc, and
2296 objcopy silently writes a different reloc. */
2297 r_extern = 0;
2298 r_index = N_ABS;
2299 }
2300
2301 MOVE_ADDRESS (0);
2302 }
2303
2304 /* Read and swap the relocs for a section. */
2305
2306 bfd_boolean
2307 NAME (aout, slurp_reloc_table) (bfd *abfd, sec_ptr asect, asymbol **symbols)
2308 {
2309 bfd_size_type count;
2310 bfd_size_type reloc_size;
2311 void * relocs;
2312 arelent *reloc_cache;
2313 size_t each_size;
2314 unsigned int counter = 0;
2315 arelent *cache_ptr;
2316 bfd_size_type amt;
2317
2318 if (asect->relocation)
2319 return TRUE;
2320
2321 if (asect->flags & SEC_CONSTRUCTOR)
2322 return TRUE;
2323
2324 if (asect == obj_datasec (abfd))
2325 reloc_size = exec_hdr (abfd)->a_drsize;
2326 else if (asect == obj_textsec (abfd))
2327 reloc_size = exec_hdr (abfd)->a_trsize;
2328 else if (asect == obj_bsssec (abfd))
2329 reloc_size = 0;
2330 else
2331 {
2332 bfd_set_error (bfd_error_invalid_operation);
2333 return FALSE;
2334 }
2335
2336 if (reloc_size == 0)
2337 return TRUE; /* Nothing to be done. */
2338
2339 if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0)
2340 return FALSE;
2341
2342 each_size = obj_reloc_entry_size (abfd);
2343
2344 count = reloc_size / each_size;
2345 if (count == 0)
2346 return TRUE; /* Nothing to be done. */
2347
2348 amt = count * sizeof (arelent);
2349 reloc_cache = (arelent *) bfd_zmalloc (amt);
2350 if (reloc_cache == NULL)
2351 return FALSE;
2352
2353 relocs = bfd_malloc (reloc_size);
2354 if (relocs == NULL)
2355 {
2356 free (reloc_cache);
2357 return FALSE;
2358 }
2359
2360 if (bfd_bread (relocs, reloc_size, abfd) != reloc_size)
2361 {
2362 free (relocs);
2363 free (reloc_cache);
2364 return FALSE;
2365 }
2366
2367 cache_ptr = reloc_cache;
2368 if (each_size == RELOC_EXT_SIZE)
2369 {
2370 struct reloc_ext_external *rptr = (struct reloc_ext_external *) relocs;
2371
2372 for (; counter < count; counter++, rptr++, cache_ptr++)
2373 MY_swap_ext_reloc_in (abfd, rptr, cache_ptr, symbols,
2374 (bfd_size_type) bfd_get_symcount (abfd));
2375 }
2376 else
2377 {
2378 struct reloc_std_external *rptr = (struct reloc_std_external *) relocs;
2379
2380 for (; counter < count; counter++, rptr++, cache_ptr++)
2381 MY_swap_std_reloc_in (abfd, rptr, cache_ptr, symbols,
2382 (bfd_size_type) bfd_get_symcount (abfd));
2383 }
2384
2385 free (relocs);
2386
2387 asect->relocation = reloc_cache;
2388 asect->reloc_count = cache_ptr - reloc_cache;
2389
2390 return TRUE;
2391 }
2392
2393 /* Write out a relocation section into an object file. */
2394
2395 bfd_boolean
2396 NAME (aout, squirt_out_relocs) (bfd *abfd, asection *section)
2397 {
2398 arelent **generic;
2399 unsigned char *native, *natptr;
2400 size_t each_size;
2401
2402 unsigned int count = section->reloc_count;
2403 bfd_size_type natsize;
2404
2405 if (count == 0 || section->orelocation == NULL)
2406 return TRUE;
2407
2408 each_size = obj_reloc_entry_size (abfd);
2409 natsize = (bfd_size_type) each_size * count;
2410 native = (unsigned char *) bfd_zalloc (abfd, natsize);
2411 if (!native)
2412 return FALSE;
2413
2414 generic = section->orelocation;
2415
2416 if (each_size == RELOC_EXT_SIZE)
2417 {
2418 for (natptr = native;
2419 count != 0;
2420 --count, natptr += each_size, ++generic)
2421 {
2422 /* PR 20921: If the howto field has not been initialised then skip
2423 this reloc.
2424 PR 20929: Similarly for the symbol field. */
2425 if ((*generic)->howto == NULL
2426 || (*generic)->sym_ptr_ptr == NULL)
2427 {
2428 bfd_set_error (bfd_error_invalid_operation);
2429 _bfd_error_handler (_("\
2430 %pB: attempt to write out unknown reloc type"), abfd);
2431 return FALSE;
2432 }
2433 MY_swap_ext_reloc_out (abfd, *generic,
2434 (struct reloc_ext_external *) natptr);
2435 }
2436 }
2437 else
2438 {
2439 for (natptr = native;
2440 count != 0;
2441 --count, natptr += each_size, ++generic)
2442 {
2443 if ((*generic)->howto == NULL
2444 || (*generic)->sym_ptr_ptr == NULL)
2445 {
2446 bfd_set_error (bfd_error_invalid_operation);
2447 _bfd_error_handler (_("\
2448 %pB: attempt to write out unknown reloc type"), abfd);
2449 return FALSE;
2450 }
2451 MY_swap_std_reloc_out (abfd, *generic,
2452 (struct reloc_std_external *) natptr);
2453 }
2454 }
2455
2456 if (bfd_bwrite ((void *) native, natsize, abfd) != natsize)
2457 {
2458 bfd_release (abfd, native);
2459 return FALSE;
2460 }
2461 bfd_release (abfd, native);
2462
2463 return TRUE;
2464 }
2465
2466 /* This is stupid. This function should be a boolean predicate. */
2467
2468 long
2469 NAME (aout, canonicalize_reloc) (bfd *abfd,
2470 sec_ptr section,
2471 arelent **relptr,
2472 asymbol **symbols)
2473 {
2474 arelent *tblptr = section->relocation;
2475 unsigned int count;
2476
2477 if (section == obj_bsssec (abfd))
2478 {
2479 *relptr = NULL;
2480 return 0;
2481 }
2482
2483 if (!(tblptr || NAME (aout, slurp_reloc_table) (abfd, section, symbols)))
2484 return -1;
2485
2486 if (section->flags & SEC_CONSTRUCTOR)
2487 {
2488 arelent_chain *chain = section->constructor_chain;
2489 for (count = 0; count < section->reloc_count; count ++)
2490 {
2491 *relptr ++ = &chain->relent;
2492 chain = chain->next;
2493 }
2494 }
2495 else
2496 {
2497 tblptr = section->relocation;
2498
2499 for (count = 0; count++ < section->reloc_count; )
2500 {
2501 *relptr++ = tblptr++;
2502 }
2503 }
2504 *relptr = 0;
2505
2506 return section->reloc_count;
2507 }
2508
2509 long
2510 NAME (aout, get_reloc_upper_bound) (bfd *abfd, sec_ptr asect)
2511 {
2512 if (bfd_get_format (abfd) != bfd_object)
2513 {
2514 bfd_set_error (bfd_error_invalid_operation);
2515 return -1;
2516 }
2517
2518 if (asect->flags & SEC_CONSTRUCTOR)
2519 return sizeof (arelent *) * (asect->reloc_count + 1);
2520
2521 if (asect == obj_datasec (abfd))
2522 return sizeof (arelent *)
2523 * ((exec_hdr (abfd)->a_drsize / obj_reloc_entry_size (abfd))
2524 + 1);
2525
2526 if (asect == obj_textsec (abfd))
2527 return sizeof (arelent *)
2528 * ((exec_hdr (abfd)->a_trsize / obj_reloc_entry_size (abfd))
2529 + 1);
2530
2531 if (asect == obj_bsssec (abfd))
2532 return sizeof (arelent *);
2533
2534 if (asect == obj_bsssec (abfd))
2535 return 0;
2536
2537 bfd_set_error (bfd_error_invalid_operation);
2538 return -1;
2539 }
2540 \f
2541 long
2542 NAME (aout, get_symtab_upper_bound) (bfd *abfd)
2543 {
2544 if (!NAME (aout, slurp_symbol_table) (abfd))
2545 return -1;
2546
2547 return (bfd_get_symcount (abfd)+1) * (sizeof (aout_symbol_type *));
2548 }
2549
2550 alent *
2551 NAME (aout, get_lineno) (bfd *ignore_abfd ATTRIBUTE_UNUSED,
2552 asymbol *ignore_symbol ATTRIBUTE_UNUSED)
2553 {
2554 return NULL;
2555 }
2556
2557 void
2558 NAME (aout, get_symbol_info) (bfd *ignore_abfd ATTRIBUTE_UNUSED,
2559 asymbol *symbol,
2560 symbol_info *ret)
2561 {
2562 bfd_symbol_info (symbol, ret);
2563
2564 if (ret->type == '?')
2565 {
2566 int type_code = aout_symbol (symbol)->type & 0xff;
2567 const char *stab_name = bfd_get_stab_name (type_code);
2568 static char buf[10];
2569
2570 if (stab_name == NULL)
2571 {
2572 sprintf (buf, "(%d)", type_code);
2573 stab_name = buf;
2574 }
2575 ret->type = '-';
2576 ret->stab_type = type_code;
2577 ret->stab_other = (unsigned) (aout_symbol (symbol)->other & 0xff);
2578 ret->stab_desc = (unsigned) (aout_symbol (symbol)->desc & 0xffff);
2579 ret->stab_name = stab_name;
2580 }
2581 }
2582
2583 void
2584 NAME (aout, print_symbol) (bfd *abfd,
2585 void * afile,
2586 asymbol *symbol,
2587 bfd_print_symbol_type how)
2588 {
2589 FILE *file = (FILE *)afile;
2590
2591 switch (how)
2592 {
2593 case bfd_print_symbol_name:
2594 if (symbol->name)
2595 fprintf (file,"%s", symbol->name);
2596 break;
2597 case bfd_print_symbol_more:
2598 fprintf (file,"%4x %2x %2x",
2599 (unsigned) (aout_symbol (symbol)->desc & 0xffff),
2600 (unsigned) (aout_symbol (symbol)->other & 0xff),
2601 (unsigned) (aout_symbol (symbol)->type));
2602 break;
2603 case bfd_print_symbol_all:
2604 {
2605 const char *section_name = symbol->section->name;
2606
2607 bfd_print_symbol_vandf (abfd, (void *)file, symbol);
2608
2609 fprintf (file," %-5s %04x %02x %02x",
2610 section_name,
2611 (unsigned) (aout_symbol (symbol)->desc & 0xffff),
2612 (unsigned) (aout_symbol (symbol)->other & 0xff),
2613 (unsigned) (aout_symbol (symbol)->type & 0xff));
2614 if (symbol->name)
2615 fprintf (file," %s", symbol->name);
2616 }
2617 break;
2618 }
2619 }
2620
2621 /* If we don't have to allocate more than 1MB to hold the generic
2622 symbols, we use the generic minisymbol methord: it's faster, since
2623 it only translates the symbols once, not multiple times. */
2624 #define MINISYM_THRESHOLD (1000000 / sizeof (asymbol))
2625
2626 /* Read minisymbols. For minisymbols, we use the unmodified a.out
2627 symbols. The minisymbol_to_symbol function translates these into
2628 BFD asymbol structures. */
2629
2630 long
2631 NAME (aout, read_minisymbols) (bfd *abfd,
2632 bfd_boolean dynamic,
2633 void * *minisymsp,
2634 unsigned int *sizep)
2635 {
2636 if (dynamic)
2637 /* We could handle the dynamic symbols here as well, but it's
2638 easier to hand them off. */
2639 return _bfd_generic_read_minisymbols (abfd, dynamic, minisymsp, sizep);
2640
2641 if (! aout_get_external_symbols (abfd))
2642 return -1;
2643
2644 if (obj_aout_external_sym_count (abfd) < MINISYM_THRESHOLD)
2645 return _bfd_generic_read_minisymbols (abfd, dynamic, minisymsp, sizep);
2646
2647 *minisymsp = (void *) obj_aout_external_syms (abfd);
2648
2649 /* By passing the external symbols back from this routine, we are
2650 giving up control over the memory block. Clear
2651 obj_aout_external_syms, so that we do not try to free it
2652 ourselves. */
2653 obj_aout_external_syms (abfd) = NULL;
2654
2655 *sizep = EXTERNAL_NLIST_SIZE;
2656 return obj_aout_external_sym_count (abfd);
2657 }
2658
2659 /* Convert a minisymbol to a BFD asymbol. A minisymbol is just an
2660 unmodified a.out symbol. The SYM argument is a structure returned
2661 by bfd_make_empty_symbol, which we fill in here. */
2662
2663 asymbol *
2664 NAME (aout, minisymbol_to_symbol) (bfd *abfd,
2665 bfd_boolean dynamic,
2666 const void * minisym,
2667 asymbol *sym)
2668 {
2669 if (dynamic
2670 || obj_aout_external_sym_count (abfd) < MINISYM_THRESHOLD)
2671 return _bfd_generic_minisymbol_to_symbol (abfd, dynamic, minisym, sym);
2672
2673 memset (sym, 0, sizeof (aout_symbol_type));
2674
2675 /* We call translate_symbol_table to translate a single symbol. */
2676 if (! (NAME (aout, translate_symbol_table)
2677 (abfd,
2678 (aout_symbol_type *) sym,
2679 (struct external_nlist *) minisym,
2680 (bfd_size_type) 1,
2681 obj_aout_external_strings (abfd),
2682 obj_aout_external_string_size (abfd),
2683 FALSE)))
2684 return NULL;
2685
2686 return sym;
2687 }
2688
2689 /* Provided a BFD, a section and an offset into the section, calculate
2690 and return the name of the source file and the line nearest to the
2691 wanted location. */
2692
2693 bfd_boolean
2694 NAME (aout, find_nearest_line) (bfd *abfd,
2695 asymbol **symbols,
2696 asection *section,
2697 bfd_vma offset,
2698 const char **filename_ptr,
2699 const char **functionname_ptr,
2700 unsigned int *line_ptr,
2701 unsigned int *disriminator_ptr)
2702 {
2703 /* Run down the file looking for the filename, function and linenumber. */
2704 asymbol **p;
2705 const char *directory_name = NULL;
2706 const char *main_file_name = NULL;
2707 const char *current_file_name = NULL;
2708 const char *line_file_name = NULL; /* Value of current_file_name at line number. */
2709 const char *line_directory_name = NULL; /* Value of directory_name at line number. */
2710 bfd_vma low_line_vma = 0;
2711 bfd_vma low_func_vma = 0;
2712 asymbol *func = 0;
2713 bfd_size_type filelen, funclen;
2714 char *buf;
2715
2716 *filename_ptr = abfd->filename;
2717 *functionname_ptr = NULL;
2718 *line_ptr = 0;
2719 if (disriminator_ptr)
2720 *disriminator_ptr = 0;
2721
2722 if (symbols != NULL)
2723 {
2724 for (p = symbols; *p; p++)
2725 {
2726 aout_symbol_type *q = (aout_symbol_type *) (*p);
2727 next:
2728 switch (q->type)
2729 {
2730 case N_TEXT:
2731 /* If this looks like a file name symbol, and it comes after
2732 the line number we have found so far, but before the
2733 offset, then we have probably not found the right line
2734 number. */
2735 if (q->symbol.value <= offset
2736 && ((q->symbol.value > low_line_vma
2737 && (line_file_name != NULL
2738 || *line_ptr != 0))
2739 || (q->symbol.value > low_func_vma
2740 && func != NULL)))
2741 {
2742 const char *symname;
2743
2744 symname = q->symbol.name;
2745 if (strcmp (symname + strlen (symname) - 2, ".o") == 0)
2746 {
2747 if (q->symbol.value > low_line_vma)
2748 {
2749 *line_ptr = 0;
2750 line_file_name = NULL;
2751 }
2752 if (q->symbol.value > low_func_vma)
2753 func = NULL;
2754 }
2755 }
2756 break;
2757
2758 case N_SO:
2759 /* If this symbol is less than the offset, but greater than
2760 the line number we have found so far, then we have not
2761 found the right line number. */
2762 if (q->symbol.value <= offset)
2763 {
2764 if (q->symbol.value > low_line_vma)
2765 {
2766 *line_ptr = 0;
2767 line_file_name = NULL;
2768 }
2769 if (q->symbol.value > low_func_vma)
2770 func = NULL;
2771 }
2772
2773 main_file_name = current_file_name = q->symbol.name;
2774 /* Look ahead to next symbol to check if that too is an N_SO. */
2775 p++;
2776 if (*p == NULL)
2777 goto done;
2778 q = (aout_symbol_type *) (*p);
2779 if (q->type != (int)N_SO)
2780 goto next;
2781
2782 /* Found a second N_SO First is directory; second is filename. */
2783 directory_name = current_file_name;
2784 main_file_name = current_file_name = q->symbol.name;
2785 if (obj_textsec (abfd) != section)
2786 goto done;
2787 break;
2788 case N_SOL:
2789 current_file_name = q->symbol.name;
2790 break;
2791
2792 case N_SLINE:
2793
2794 case N_DSLINE:
2795 case N_BSLINE:
2796 /* We'll keep this if it resolves nearer than the one we have
2797 already. */
2798 if (q->symbol.value >= low_line_vma
2799 && q->symbol.value <= offset)
2800 {
2801 *line_ptr = q->desc;
2802 low_line_vma = q->symbol.value;
2803 line_file_name = current_file_name;
2804 line_directory_name = directory_name;
2805 }
2806 break;
2807 case N_FUN:
2808 {
2809 /* We'll keep this if it is nearer than the one we have already. */
2810 if (q->symbol.value >= low_func_vma &&
2811 q->symbol.value <= offset)
2812 {
2813 low_func_vma = q->symbol.value;
2814 func = (asymbol *)q;
2815 }
2816 else if (q->symbol.value > offset)
2817 goto done;
2818 }
2819 break;
2820 }
2821 }
2822 }
2823
2824 done:
2825 if (*line_ptr != 0)
2826 {
2827 main_file_name = line_file_name;
2828 directory_name = line_directory_name;
2829 }
2830
2831 if (main_file_name == NULL
2832 || IS_ABSOLUTE_PATH (main_file_name)
2833 || directory_name == NULL)
2834 filelen = 0;
2835 else
2836 filelen = strlen (directory_name) + strlen (main_file_name);
2837
2838 if (func == NULL)
2839 funclen = 0;
2840 else
2841 funclen = strlen (bfd_asymbol_name (func));
2842
2843 if (adata (abfd).line_buf != NULL)
2844 free (adata (abfd).line_buf);
2845
2846 if (filelen + funclen == 0)
2847 adata (abfd).line_buf = buf = NULL;
2848 else
2849 {
2850 buf = (char *) bfd_malloc (filelen + funclen + 3);
2851 adata (abfd).line_buf = buf;
2852 if (buf == NULL)
2853 return FALSE;
2854 }
2855
2856 if (main_file_name != NULL)
2857 {
2858 if (IS_ABSOLUTE_PATH (main_file_name) || directory_name == NULL)
2859 *filename_ptr = main_file_name;
2860 else
2861 {
2862 if (buf == NULL)
2863 /* PR binutils/20891: In a corrupt input file both
2864 main_file_name and directory_name can be empty... */
2865 * filename_ptr = NULL;
2866 else
2867 {
2868 snprintf (buf, filelen + 1, "%s%s", directory_name,
2869 main_file_name);
2870 *filename_ptr = buf;
2871 buf += filelen + 1;
2872 }
2873 }
2874 }
2875
2876 if (func)
2877 {
2878 const char *function = func->name;
2879 char *colon;
2880
2881 if (buf == NULL)
2882 {
2883 /* PR binutils/20892: In a corrupt input file func can be empty. */
2884 * functionname_ptr = NULL;
2885 return TRUE;
2886 }
2887 /* The caller expects a symbol name. We actually have a
2888 function name, without the leading underscore. Put the
2889 underscore back in, so that the caller gets a symbol name. */
2890 if (bfd_get_symbol_leading_char (abfd) == '\0')
2891 strcpy (buf, function);
2892 else
2893 {
2894 buf[0] = bfd_get_symbol_leading_char (abfd);
2895 strcpy (buf + 1, function);
2896 }
2897 /* Have to remove : stuff. */
2898 colon = strchr (buf, ':');
2899 if (colon != NULL)
2900 *colon = '\0';
2901 *functionname_ptr = buf;
2902 }
2903
2904 return TRUE;
2905 }
2906
2907 int
2908 NAME (aout, sizeof_headers) (bfd *abfd,
2909 struct bfd_link_info *info ATTRIBUTE_UNUSED)
2910 {
2911 return adata (abfd).exec_bytes_size;
2912 }
2913
2914 /* Free all information we have cached for this BFD. We can always
2915 read it again later if we need it. */
2916
2917 bfd_boolean
2918 NAME (aout, bfd_free_cached_info) (bfd *abfd)
2919 {
2920 asection *o;
2921
2922 if (bfd_get_format (abfd) != bfd_object
2923 || abfd->tdata.aout_data == NULL)
2924 return TRUE;
2925
2926 #define BFCI_FREE(x) if (x != NULL) { free (x); x = NULL; }
2927 BFCI_FREE (obj_aout_symbols (abfd));
2928 #ifdef USE_MMAP
2929 obj_aout_external_syms (abfd) = 0;
2930 bfd_free_window (&obj_aout_sym_window (abfd));
2931 bfd_free_window (&obj_aout_string_window (abfd));
2932 obj_aout_external_strings (abfd) = 0;
2933 #else
2934 BFCI_FREE (obj_aout_external_syms (abfd));
2935 BFCI_FREE (obj_aout_external_strings (abfd));
2936 #endif
2937 for (o = abfd->sections; o != NULL; o = o->next)
2938 BFCI_FREE (o->relocation);
2939 #undef BFCI_FREE
2940
2941 return TRUE;
2942 }
2943 \f
2944 /* a.out link code. */
2945
2946 /* Routine to create an entry in an a.out link hash table. */
2947
2948 struct bfd_hash_entry *
2949 NAME (aout, link_hash_newfunc) (struct bfd_hash_entry *entry,
2950 struct bfd_hash_table *table,
2951 const char *string)
2952 {
2953 struct aout_link_hash_entry *ret = (struct aout_link_hash_entry *) entry;
2954
2955 /* Allocate the structure if it has not already been allocated by a
2956 subclass. */
2957 if (ret == NULL)
2958 ret = (struct aout_link_hash_entry *) bfd_hash_allocate (table,
2959 sizeof (* ret));
2960 if (ret == NULL)
2961 return NULL;
2962
2963 /* Call the allocation method of the superclass. */
2964 ret = ((struct aout_link_hash_entry *)
2965 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2966 table, string));
2967 if (ret)
2968 {
2969 /* Set local fields. */
2970 ret->written = FALSE;
2971 ret->indx = -1;
2972 }
2973
2974 return (struct bfd_hash_entry *) ret;
2975 }
2976
2977 /* Initialize an a.out link hash table. */
2978
2979 bfd_boolean
2980 NAME (aout, link_hash_table_init) (struct aout_link_hash_table *table,
2981 bfd *abfd,
2982 struct bfd_hash_entry *(*newfunc)
2983 (struct bfd_hash_entry *, struct bfd_hash_table *,
2984 const char *),
2985 unsigned int entsize)
2986 {
2987 return _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
2988 }
2989
2990 /* Create an a.out link hash table. */
2991
2992 struct bfd_link_hash_table *
2993 NAME (aout, link_hash_table_create) (bfd *abfd)
2994 {
2995 struct aout_link_hash_table *ret;
2996 bfd_size_type amt = sizeof (* ret);
2997
2998 ret = (struct aout_link_hash_table *) bfd_malloc (amt);
2999 if (ret == NULL)
3000 return NULL;
3001
3002 if (!NAME (aout, link_hash_table_init) (ret, abfd,
3003 NAME (aout, link_hash_newfunc),
3004 sizeof (struct aout_link_hash_entry)))
3005 {
3006 free (ret);
3007 return NULL;
3008 }
3009 return &ret->root;
3010 }
3011
3012 /* Add all symbols from an object file to the hash table. */
3013
3014 static bfd_boolean
3015 aout_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
3016 {
3017 bfd_boolean (*add_one_symbol)
3018 (struct bfd_link_info *, bfd *, const char *, flagword, asection *,
3019 bfd_vma, const char *, bfd_boolean, bfd_boolean,
3020 struct bfd_link_hash_entry **);
3021 struct external_nlist *syms;
3022 bfd_size_type sym_count;
3023 char *strings;
3024 bfd_boolean copy;
3025 struct aout_link_hash_entry **sym_hash;
3026 struct external_nlist *p;
3027 struct external_nlist *pend;
3028 bfd_size_type amt;
3029
3030 syms = obj_aout_external_syms (abfd);
3031 sym_count = obj_aout_external_sym_count (abfd);
3032 strings = obj_aout_external_strings (abfd);
3033 if (info->keep_memory)
3034 copy = FALSE;
3035 else
3036 copy = TRUE;
3037
3038 if (aout_backend_info (abfd)->add_dynamic_symbols != NULL)
3039 {
3040 if (! ((*aout_backend_info (abfd)->add_dynamic_symbols)
3041 (abfd, info, &syms, &sym_count, &strings)))
3042 return FALSE;
3043 }
3044
3045 if (sym_count == 0)
3046 return TRUE; /* Nothing to do. */
3047
3048 /* We keep a list of the linker hash table entries that correspond
3049 to particular symbols. We could just look them up in the hash
3050 table, but keeping the list is more efficient. Perhaps this
3051 should be conditional on info->keep_memory. */
3052 amt = sym_count * sizeof (struct aout_link_hash_entry *);
3053 sym_hash = (struct aout_link_hash_entry **) bfd_alloc (abfd, amt);
3054 if (sym_hash == NULL)
3055 return FALSE;
3056 obj_aout_sym_hashes (abfd) = sym_hash;
3057
3058 add_one_symbol = aout_backend_info (abfd)->add_one_symbol;
3059 if (add_one_symbol == NULL)
3060 add_one_symbol = _bfd_generic_link_add_one_symbol;
3061
3062 p = syms;
3063 pend = p + sym_count;
3064 for (; p < pend; p++, sym_hash++)
3065 {
3066 int type;
3067 const char *name;
3068 bfd_vma value;
3069 asection *section;
3070 flagword flags;
3071 const char *string;
3072
3073 *sym_hash = NULL;
3074
3075 type = H_GET_8 (abfd, p->e_type);
3076
3077 /* Ignore debugging symbols. */
3078 if ((type & N_STAB) != 0)
3079 continue;
3080
3081 /* PR 19629: Corrupt binaries can contain illegal string offsets. */
3082 if (GET_WORD (abfd, p->e_strx) >= obj_aout_external_string_size (abfd))
3083 return FALSE;
3084 name = strings + GET_WORD (abfd, p->e_strx);
3085 value = GET_WORD (abfd, p->e_value);
3086 flags = BSF_GLOBAL;
3087 string = NULL;
3088 switch (type)
3089 {
3090 default:
3091 abort ();
3092
3093 case N_UNDF:
3094 case N_ABS:
3095 case N_TEXT:
3096 case N_DATA:
3097 case N_BSS:
3098 case N_FN_SEQ:
3099 case N_COMM:
3100 case N_SETV:
3101 case N_FN:
3102 /* Ignore symbols that are not externally visible. */
3103 continue;
3104 case N_INDR:
3105 /* Ignore local indirect symbol. */
3106 ++p;
3107 ++sym_hash;
3108 continue;
3109
3110 case N_UNDF | N_EXT:
3111 if (value == 0)
3112 {
3113 section = bfd_und_section_ptr;
3114 flags = 0;
3115 }
3116 else
3117 section = bfd_com_section_ptr;
3118 break;
3119 case N_ABS | N_EXT:
3120 section = bfd_abs_section_ptr;
3121 break;
3122 case N_TEXT | N_EXT:
3123 section = obj_textsec (abfd);
3124 value -= bfd_get_section_vma (abfd, section);
3125 break;
3126 case N_DATA | N_EXT:
3127 case N_SETV | N_EXT:
3128 /* Treat N_SETV symbols as N_DATA symbol; see comment in
3129 translate_from_native_sym_flags. */
3130 section = obj_datasec (abfd);
3131 value -= bfd_get_section_vma (abfd, section);
3132 break;
3133 case N_BSS | N_EXT:
3134 section = obj_bsssec (abfd);
3135 value -= bfd_get_section_vma (abfd, section);
3136 break;
3137 case N_INDR | N_EXT:
3138 /* An indirect symbol. The next symbol is the symbol
3139 which this one really is. */
3140 /* See PR 20925 for a reproducer. */
3141 if (p + 1 >= pend)
3142 return FALSE;
3143 ++p;
3144 /* PR 19629: Corrupt binaries can contain illegal string offsets. */
3145 if (GET_WORD (abfd, p->e_strx) >= obj_aout_external_string_size (abfd))
3146 return FALSE;
3147 string = strings + GET_WORD (abfd, p->e_strx);
3148 section = bfd_ind_section_ptr;
3149 flags |= BSF_INDIRECT;
3150 break;
3151 case N_COMM | N_EXT:
3152 section = bfd_com_section_ptr;
3153 break;
3154 case N_SETA: case N_SETA | N_EXT:
3155 section = bfd_abs_section_ptr;
3156 flags |= BSF_CONSTRUCTOR;
3157 break;
3158 case N_SETT: case N_SETT | N_EXT:
3159 section = obj_textsec (abfd);
3160 flags |= BSF_CONSTRUCTOR;
3161 value -= bfd_get_section_vma (abfd, section);
3162 break;
3163 case N_SETD: case N_SETD | N_EXT:
3164 section = obj_datasec (abfd);
3165 flags |= BSF_CONSTRUCTOR;
3166 value -= bfd_get_section_vma (abfd, section);
3167 break;
3168 case N_SETB: case N_SETB | N_EXT:
3169 section = obj_bsssec (abfd);
3170 flags |= BSF_CONSTRUCTOR;
3171 value -= bfd_get_section_vma (abfd, section);
3172 break;
3173 case N_WARNING:
3174 /* A warning symbol. The next symbol is the one to warn
3175 about. If there is no next symbol, just look away. */
3176 if (p + 1 >= pend)
3177 return TRUE;
3178 ++p;
3179 string = name;
3180 /* PR 19629: Corrupt binaries can contain illegal string offsets. */
3181 if (GET_WORD (abfd, p->e_strx) >= obj_aout_external_string_size (abfd))
3182 return FALSE;
3183 name = strings + GET_WORD (abfd, p->e_strx);
3184 section = bfd_und_section_ptr;
3185 flags |= BSF_WARNING;
3186 break;
3187 case N_WEAKU:
3188 section = bfd_und_section_ptr;
3189 flags = BSF_WEAK;
3190 break;
3191 case N_WEAKA:
3192 section = bfd_abs_section_ptr;
3193 flags = BSF_WEAK;
3194 break;
3195 case N_WEAKT:
3196 section = obj_textsec (abfd);
3197 value -= bfd_get_section_vma (abfd, section);
3198 flags = BSF_WEAK;
3199 break;
3200 case N_WEAKD:
3201 section = obj_datasec (abfd);
3202 value -= bfd_get_section_vma (abfd, section);
3203 flags = BSF_WEAK;
3204 break;
3205 case N_WEAKB:
3206 section = obj_bsssec (abfd);
3207 value -= bfd_get_section_vma (abfd, section);
3208 flags = BSF_WEAK;
3209 break;
3210 }
3211
3212 if (! ((*add_one_symbol)
3213 (info, abfd, name, flags, section, value, string, copy, FALSE,
3214 (struct bfd_link_hash_entry **) sym_hash)))
3215 return FALSE;
3216
3217 /* Restrict the maximum alignment of a common symbol based on
3218 the architecture, since a.out has no way to represent
3219 alignment requirements of a section in a .o file. FIXME:
3220 This isn't quite right: it should use the architecture of the
3221 output file, not the input files. */
3222 if ((*sym_hash)->root.type == bfd_link_hash_common
3223 && ((*sym_hash)->root.u.c.p->alignment_power >
3224 bfd_get_arch_info (abfd)->section_align_power))
3225 (*sym_hash)->root.u.c.p->alignment_power =
3226 bfd_get_arch_info (abfd)->section_align_power;
3227
3228 /* If this is a set symbol, and we are not building sets, then
3229 it is possible for the hash entry to not have been set. In
3230 such a case, treat the symbol as not globally defined. */
3231 if ((*sym_hash)->root.type == bfd_link_hash_new)
3232 {
3233 BFD_ASSERT ((flags & BSF_CONSTRUCTOR) != 0);
3234 *sym_hash = NULL;
3235 }
3236
3237 if (type == (N_INDR | N_EXT) || type == N_WARNING)
3238 ++sym_hash;
3239 }
3240
3241 return TRUE;
3242 }
3243
3244 /* Free up the internal symbols read from an a.out file. */
3245
3246 static bfd_boolean
3247 aout_link_free_symbols (bfd *abfd)
3248 {
3249 if (obj_aout_external_syms (abfd) != NULL)
3250 {
3251 #ifdef USE_MMAP
3252 bfd_free_window (&obj_aout_sym_window (abfd));
3253 #else
3254 free ((void *) obj_aout_external_syms (abfd));
3255 #endif
3256 obj_aout_external_syms (abfd) = NULL;
3257 }
3258 if (obj_aout_external_strings (abfd) != NULL)
3259 {
3260 #ifdef USE_MMAP
3261 bfd_free_window (&obj_aout_string_window (abfd));
3262 #else
3263 free ((void *) obj_aout_external_strings (abfd));
3264 #endif
3265 obj_aout_external_strings (abfd) = NULL;
3266 }
3267 return TRUE;
3268 }
3269
3270 /* Add symbols from an a.out object file. */
3271
3272 static bfd_boolean
3273 aout_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3274 {
3275 if (! aout_get_external_symbols (abfd))
3276 return FALSE;
3277 if (! aout_link_add_symbols (abfd, info))
3278 return FALSE;
3279 if (! info->keep_memory)
3280 {
3281 if (! aout_link_free_symbols (abfd))
3282 return FALSE;
3283 }
3284 return TRUE;
3285 }
3286
3287 /* Look through the internal symbols to see if this object file should
3288 be included in the link. We should include this object file if it
3289 defines any symbols which are currently undefined. If this object
3290 file defines a common symbol, then we may adjust the size of the
3291 known symbol but we do not include the object file in the link
3292 (unless there is some other reason to include it). */
3293
3294 static bfd_boolean
3295 aout_link_check_ar_symbols (bfd *abfd,
3296 struct bfd_link_info *info,
3297 bfd_boolean *pneeded,
3298 bfd **subsbfd)
3299 {
3300 struct external_nlist *p;
3301 struct external_nlist *pend;
3302 char *strings;
3303
3304 *pneeded = FALSE;
3305
3306 /* Look through all the symbols. */
3307 p = obj_aout_external_syms (abfd);
3308 pend = p + obj_aout_external_sym_count (abfd);
3309 strings = obj_aout_external_strings (abfd);
3310 for (; p < pend; p++)
3311 {
3312 int type = H_GET_8 (abfd, p->e_type);
3313 const char *name;
3314 struct bfd_link_hash_entry *h;
3315
3316 /* Ignore symbols that are not externally visible. This is an
3317 optimization only, as we check the type more thoroughly
3318 below. */
3319 if (((type & N_EXT) == 0
3320 || (type & N_STAB) != 0
3321 || type == N_FN)
3322 && type != N_WEAKA
3323 && type != N_WEAKT
3324 && type != N_WEAKD
3325 && type != N_WEAKB)
3326 {
3327 if (type == N_WARNING
3328 || type == N_INDR)
3329 ++p;
3330 continue;
3331 }
3332
3333 name = strings + GET_WORD (abfd, p->e_strx);
3334 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
3335
3336 /* We are only interested in symbols that are currently
3337 undefined or common. */
3338 if (h == NULL
3339 || (h->type != bfd_link_hash_undefined
3340 && h->type != bfd_link_hash_common))
3341 {
3342 if (type == (N_INDR | N_EXT))
3343 ++p;
3344 continue;
3345 }
3346
3347 if (type == (N_TEXT | N_EXT)
3348 || type == (N_DATA | N_EXT)
3349 || type == (N_BSS | N_EXT)
3350 || type == (N_ABS | N_EXT)
3351 || type == (N_INDR | N_EXT))
3352 {
3353 /* This object file defines this symbol. We must link it
3354 in. This is true regardless of whether the current
3355 definition of the symbol is undefined or common.
3356
3357 If the current definition is common, we have a case in
3358 which we have already seen an object file including:
3359 int a;
3360 and this object file from the archive includes:
3361 int a = 5;
3362 In such a case, whether to include this object is target
3363 dependant for backward compatibility.
3364
3365 FIXME: The SunOS 4.1.3 linker will pull in the archive
3366 element if the symbol is defined in the .data section,
3367 but not if it is defined in the .text section. That
3368 seems a bit crazy to me, and it has not been implemented
3369 yet. However, it might be correct. */
3370 if (h->type == bfd_link_hash_common)
3371 {
3372 int skip = 0;
3373
3374 switch (info->common_skip_ar_symbols)
3375 {
3376 case bfd_link_common_skip_none:
3377 break;
3378 case bfd_link_common_skip_text:
3379 skip = (type == (N_TEXT | N_EXT));
3380 break;
3381 case bfd_link_common_skip_data:
3382 skip = (type == (N_DATA | N_EXT));
3383 break;
3384 case bfd_link_common_skip_all:
3385 skip = 1;
3386 break;
3387 }
3388
3389 if (skip)
3390 continue;
3391 }
3392
3393 if (!(*info->callbacks
3394 ->add_archive_element) (info, abfd, name, subsbfd))
3395 return FALSE;
3396 *pneeded = TRUE;
3397 return TRUE;
3398 }
3399
3400 if (type == (N_UNDF | N_EXT))
3401 {
3402 bfd_vma value;
3403
3404 value = GET_WORD (abfd, p->e_value);
3405 if (value != 0)
3406 {
3407 /* This symbol is common in the object from the archive
3408 file. */
3409 if (h->type == bfd_link_hash_undefined)
3410 {
3411 bfd *symbfd;
3412 unsigned int power;
3413
3414 symbfd = h->u.undef.abfd;
3415 if (symbfd == NULL)
3416 {
3417 /* This symbol was created as undefined from
3418 outside BFD. We assume that we should link
3419 in the object file. This is done for the -u
3420 option in the linker. */
3421 if (!(*info->callbacks
3422 ->add_archive_element) (info, abfd, name, subsbfd))
3423 return FALSE;
3424 *pneeded = TRUE;
3425 return TRUE;
3426 }
3427 /* Turn the current link symbol into a common
3428 symbol. It is already on the undefs list. */
3429 h->type = bfd_link_hash_common;
3430 h->u.c.p = (struct bfd_link_hash_common_entry *)
3431 bfd_hash_allocate (&info->hash->table,
3432 sizeof (struct bfd_link_hash_common_entry));
3433 if (h->u.c.p == NULL)
3434 return FALSE;
3435
3436 h->u.c.size = value;
3437
3438 /* FIXME: This isn't quite right. The maximum
3439 alignment of a common symbol should be set by the
3440 architecture of the output file, not of the input
3441 file. */
3442 power = bfd_log2 (value);
3443 if (power > bfd_get_arch_info (abfd)->section_align_power)
3444 power = bfd_get_arch_info (abfd)->section_align_power;
3445 h->u.c.p->alignment_power = power;
3446
3447 h->u.c.p->section = bfd_make_section_old_way (symbfd,
3448 "COMMON");
3449 }
3450 else
3451 {
3452 /* Adjust the size of the common symbol if
3453 necessary. */
3454 if (value > h->u.c.size)
3455 h->u.c.size = value;
3456 }
3457 }
3458 }
3459
3460 if (type == N_WEAKA
3461 || type == N_WEAKT
3462 || type == N_WEAKD
3463 || type == N_WEAKB)
3464 {
3465 /* This symbol is weak but defined. We must pull it in if
3466 the current link symbol is undefined, but we don't want
3467 it if the current link symbol is common. */
3468 if (h->type == bfd_link_hash_undefined)
3469 {
3470 if (!(*info->callbacks
3471 ->add_archive_element) (info, abfd, name, subsbfd))
3472 return FALSE;
3473 *pneeded = TRUE;
3474 return TRUE;
3475 }
3476 }
3477 }
3478
3479 /* We do not need this object file. */
3480 return TRUE;
3481 }
3482 /* Check a single archive element to see if we need to include it in
3483 the link. *PNEEDED is set according to whether this element is
3484 needed in the link or not. This is called from
3485 _bfd_generic_link_add_archive_symbols. */
3486
3487 static bfd_boolean
3488 aout_link_check_archive_element (bfd *abfd,
3489 struct bfd_link_info *info,
3490 struct bfd_link_hash_entry *h ATTRIBUTE_UNUSED,
3491 const char *name ATTRIBUTE_UNUSED,
3492 bfd_boolean *pneeded)
3493 {
3494 bfd *oldbfd;
3495 bfd_boolean needed;
3496
3497 if (!aout_get_external_symbols (abfd))
3498 return FALSE;
3499
3500 oldbfd = abfd;
3501 if (!aout_link_check_ar_symbols (abfd, info, pneeded, &abfd))
3502 return FALSE;
3503
3504 needed = *pneeded;
3505 if (needed)
3506 {
3507 /* Potentially, the add_archive_element hook may have set a
3508 substitute BFD for us. */
3509 if (abfd != oldbfd)
3510 {
3511 if (!info->keep_memory
3512 && !aout_link_free_symbols (oldbfd))
3513 return FALSE;
3514 if (!aout_get_external_symbols (abfd))
3515 return FALSE;
3516 }
3517 if (!aout_link_add_symbols (abfd, info))
3518 return FALSE;
3519 }
3520
3521 if (!info->keep_memory || !needed)
3522 {
3523 if (!aout_link_free_symbols (abfd))
3524 return FALSE;
3525 }
3526
3527 return TRUE;
3528 }
3529
3530 /* Given an a.out BFD, add symbols to the global hash table as
3531 appropriate. */
3532
3533 bfd_boolean
3534 NAME (aout, link_add_symbols) (bfd *abfd, struct bfd_link_info *info)
3535 {
3536 switch (bfd_get_format (abfd))
3537 {
3538 case bfd_object:
3539 return aout_link_add_object_symbols (abfd, info);
3540 case bfd_archive:
3541 return _bfd_generic_link_add_archive_symbols
3542 (abfd, info, aout_link_check_archive_element);
3543 default:
3544 bfd_set_error (bfd_error_wrong_format);
3545 return FALSE;
3546 }
3547 }
3548 \f
3549 /* A hash table used for header files with N_BINCL entries. */
3550
3551 struct aout_link_includes_table
3552 {
3553 struct bfd_hash_table root;
3554 };
3555
3556 /* A linked list of totals that we have found for a particular header
3557 file. */
3558
3559 struct aout_link_includes_totals
3560 {
3561 struct aout_link_includes_totals *next;
3562 bfd_vma total;
3563 };
3564
3565 /* An entry in the header file hash table. */
3566
3567 struct aout_link_includes_entry
3568 {
3569 struct bfd_hash_entry root;
3570 /* List of totals we have found for this file. */
3571 struct aout_link_includes_totals *totals;
3572 };
3573
3574 /* Look up an entry in an the header file hash table. */
3575
3576 #define aout_link_includes_lookup(table, string, create, copy) \
3577 ((struct aout_link_includes_entry *) \
3578 bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
3579
3580 /* During the final link step we need to pass around a bunch of
3581 information, so we do it in an instance of this structure. */
3582
3583 struct aout_final_link_info
3584 {
3585 /* General link information. */
3586 struct bfd_link_info *info;
3587 /* Output bfd. */
3588 bfd *output_bfd;
3589 /* Reloc file positions. */
3590 file_ptr treloff, dreloff;
3591 /* File position of symbols. */
3592 file_ptr symoff;
3593 /* String table. */
3594 struct bfd_strtab_hash *strtab;
3595 /* Header file hash table. */
3596 struct aout_link_includes_table includes;
3597 /* A buffer large enough to hold the contents of any section. */
3598 bfd_byte *contents;
3599 /* A buffer large enough to hold the relocs of any section. */
3600 void * relocs;
3601 /* A buffer large enough to hold the symbol map of any input BFD. */
3602 int *symbol_map;
3603 /* A buffer large enough to hold output symbols of any input BFD. */
3604 struct external_nlist *output_syms;
3605 };
3606
3607 /* The function to create a new entry in the header file hash table. */
3608
3609 static struct bfd_hash_entry *
3610 aout_link_includes_newfunc (struct bfd_hash_entry *entry,
3611 struct bfd_hash_table *table,
3612 const char *string)
3613 {
3614 struct aout_link_includes_entry *ret =
3615 (struct aout_link_includes_entry *) entry;
3616
3617 /* Allocate the structure if it has not already been allocated by a
3618 subclass. */
3619 if (ret == NULL)
3620 ret = (struct aout_link_includes_entry *)
3621 bfd_hash_allocate (table, sizeof (* ret));
3622 if (ret == NULL)
3623 return NULL;
3624
3625 /* Call the allocation method of the superclass. */
3626 ret = ((struct aout_link_includes_entry *)
3627 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
3628 if (ret)
3629 {
3630 /* Set local fields. */
3631 ret->totals = NULL;
3632 }
3633
3634 return (struct bfd_hash_entry *) ret;
3635 }
3636
3637 /* Write out a symbol that was not associated with an a.out input
3638 object. */
3639
3640 static bfd_boolean
3641 aout_link_write_other_symbol (struct bfd_hash_entry *bh, void *data)
3642 {
3643 struct aout_link_hash_entry *h = (struct aout_link_hash_entry *) bh;
3644 struct aout_final_link_info *flaginfo = (struct aout_final_link_info *) data;
3645 bfd *output_bfd;
3646 int type;
3647 bfd_vma val;
3648 struct external_nlist outsym;
3649 bfd_size_type indx;
3650 bfd_size_type amt;
3651
3652 if (h->root.type == bfd_link_hash_warning)
3653 {
3654 h = (struct aout_link_hash_entry *) h->root.u.i.link;
3655 if (h->root.type == bfd_link_hash_new)
3656 return TRUE;
3657 }
3658
3659 output_bfd = flaginfo->output_bfd;
3660
3661 if (aout_backend_info (output_bfd)->write_dynamic_symbol != NULL)
3662 {
3663 if (! ((*aout_backend_info (output_bfd)->write_dynamic_symbol)
3664 (output_bfd, flaginfo->info, h)))
3665 {
3666 /* FIXME: No way to handle errors. */
3667 abort ();
3668 }
3669 }
3670
3671 if (h->written)
3672 return TRUE;
3673
3674 h->written = TRUE;
3675
3676 /* An indx of -2 means the symbol must be written. */
3677 if (h->indx != -2
3678 && (flaginfo->info->strip == strip_all
3679 || (flaginfo->info->strip == strip_some
3680 && bfd_hash_lookup (flaginfo->info->keep_hash, h->root.root.string,
3681 FALSE, FALSE) == NULL)))
3682 return TRUE;
3683
3684 switch (h->root.type)
3685 {
3686 default:
3687 case bfd_link_hash_warning:
3688 abort ();
3689 /* Avoid variable not initialized warnings. */
3690 return TRUE;
3691 case bfd_link_hash_new:
3692 /* This can happen for set symbols when sets are not being
3693 built. */
3694 return TRUE;
3695 case bfd_link_hash_undefined:
3696 type = N_UNDF | N_EXT;
3697 val = 0;
3698 break;
3699 case bfd_link_hash_defined:
3700 case bfd_link_hash_defweak:
3701 {
3702 asection *sec;
3703
3704 sec = h->root.u.def.section->output_section;
3705 BFD_ASSERT (bfd_is_abs_section (sec)
3706 || sec->owner == output_bfd);
3707 if (sec == obj_textsec (output_bfd))
3708 type = h->root.type == bfd_link_hash_defined ? N_TEXT : N_WEAKT;
3709 else if (sec == obj_datasec (output_bfd))
3710 type = h->root.type == bfd_link_hash_defined ? N_DATA : N_WEAKD;
3711 else if (sec == obj_bsssec (output_bfd))
3712 type = h->root.type == bfd_link_hash_defined ? N_BSS : N_WEAKB;
3713 else
3714 type = h->root.type == bfd_link_hash_defined ? N_ABS : N_WEAKA;
3715 type |= N_EXT;
3716 val = (h->root.u.def.value
3717 + sec->vma
3718 + h->root.u.def.section->output_offset);
3719 }
3720 break;
3721 case bfd_link_hash_common:
3722 type = N_UNDF | N_EXT;
3723 val = h->root.u.c.size;
3724 break;
3725 case bfd_link_hash_undefweak:
3726 type = N_WEAKU;
3727 val = 0;
3728 break;
3729 case bfd_link_hash_indirect:
3730 /* We ignore these symbols, since the indirected symbol is
3731 already in the hash table. */
3732 return TRUE;
3733 }
3734
3735 H_PUT_8 (output_bfd, type, outsym.e_type);
3736 H_PUT_8 (output_bfd, 0, outsym.e_other);
3737 H_PUT_16 (output_bfd, 0, outsym.e_desc);
3738 indx = add_to_stringtab (output_bfd, flaginfo->strtab, h->root.root.string,
3739 FALSE);
3740 if (indx == - (bfd_size_type) 1)
3741 /* FIXME: No way to handle errors. */
3742 abort ();
3743
3744 PUT_WORD (output_bfd, indx, outsym.e_strx);
3745 PUT_WORD (output_bfd, val, outsym.e_value);
3746
3747 amt = EXTERNAL_NLIST_SIZE;
3748 if (bfd_seek (output_bfd, flaginfo->symoff, SEEK_SET) != 0
3749 || bfd_bwrite ((void *) &outsym, amt, output_bfd) != amt)
3750 /* FIXME: No way to handle errors. */
3751 abort ();
3752
3753 flaginfo->symoff += EXTERNAL_NLIST_SIZE;
3754 h->indx = obj_aout_external_sym_count (output_bfd);
3755 ++obj_aout_external_sym_count (output_bfd);
3756
3757 return TRUE;
3758 }
3759
3760 /* Handle a link order which is supposed to generate a reloc. */
3761
3762 static bfd_boolean
3763 aout_link_reloc_link_order (struct aout_final_link_info *flaginfo,
3764 asection *o,
3765 struct bfd_link_order *p)
3766 {
3767 struct bfd_link_order_reloc *pr;
3768 int r_index;
3769 int r_extern;
3770 reloc_howto_type *howto;
3771 file_ptr *reloff_ptr = NULL;
3772 struct reloc_std_external srel;
3773 struct reloc_ext_external erel;
3774 void * rel_ptr;
3775 bfd_size_type amt;
3776
3777 pr = p->u.reloc.p;
3778
3779 if (p->type == bfd_section_reloc_link_order)
3780 {
3781 r_extern = 0;
3782 if (bfd_is_abs_section (pr->u.section))
3783 r_index = N_ABS | N_EXT;
3784 else
3785 {
3786 BFD_ASSERT (pr->u.section->owner == flaginfo->output_bfd);
3787 r_index = pr->u.section->target_index;
3788 }
3789 }
3790 else
3791 {
3792 struct aout_link_hash_entry *h;
3793
3794 BFD_ASSERT (p->type == bfd_symbol_reloc_link_order);
3795 r_extern = 1;
3796 h = ((struct aout_link_hash_entry *)
3797 bfd_wrapped_link_hash_lookup (flaginfo->output_bfd, flaginfo->info,
3798 pr->u.name, FALSE, FALSE, TRUE));
3799 if (h != NULL
3800 && h->indx >= 0)
3801 r_index = h->indx;
3802 else if (h != NULL)
3803 {
3804 /* We decided to strip this symbol, but it turns out that we
3805 can't. Note that we lose the other and desc information
3806 here. I don't think that will ever matter for a global
3807 symbol. */
3808 h->indx = -2;
3809 h->written = FALSE;
3810 if (!aout_link_write_other_symbol (&h->root.root, flaginfo))
3811 return FALSE;
3812 r_index = h->indx;
3813 }
3814 else
3815 {
3816 (*flaginfo->info->callbacks->unattached_reloc)
3817 (flaginfo->info, pr->u.name, NULL, NULL, (bfd_vma) 0);
3818 r_index = 0;
3819 }
3820 }
3821
3822 howto = bfd_reloc_type_lookup (flaginfo->output_bfd, pr->reloc);
3823 if (howto == 0)
3824 {
3825 bfd_set_error (bfd_error_bad_value);
3826 return FALSE;
3827 }
3828
3829 if (o == obj_textsec (flaginfo->output_bfd))
3830 reloff_ptr = &flaginfo->treloff;
3831 else if (o == obj_datasec (flaginfo->output_bfd))
3832 reloff_ptr = &flaginfo->dreloff;
3833 else
3834 abort ();
3835
3836 if (obj_reloc_entry_size (flaginfo->output_bfd) == RELOC_STD_SIZE)
3837 {
3838 #ifdef MY_put_reloc
3839 MY_put_reloc (flaginfo->output_bfd, r_extern, r_index, p->offset, howto,
3840 &srel);
3841 #else
3842 {
3843 int r_pcrel;
3844 int r_baserel;
3845 int r_jmptable;
3846 int r_relative;
3847 int r_length;
3848
3849 r_pcrel = (int) howto->pc_relative;
3850 r_baserel = (howto->type & 8) != 0;
3851 r_jmptable = (howto->type & 16) != 0;
3852 r_relative = (howto->type & 32) != 0;
3853 r_length = howto->size;
3854
3855 PUT_WORD (flaginfo->output_bfd, p->offset, srel.r_address);
3856 if (bfd_header_big_endian (flaginfo->output_bfd))
3857 {
3858 srel.r_index[0] = r_index >> 16;
3859 srel.r_index[1] = r_index >> 8;
3860 srel.r_index[2] = r_index;
3861 srel.r_type[0] =
3862 ((r_extern ? RELOC_STD_BITS_EXTERN_BIG : 0)
3863 | (r_pcrel ? RELOC_STD_BITS_PCREL_BIG : 0)
3864 | (r_baserel ? RELOC_STD_BITS_BASEREL_BIG : 0)
3865 | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_BIG : 0)
3866 | (r_relative ? RELOC_STD_BITS_RELATIVE_BIG : 0)
3867 | (r_length << RELOC_STD_BITS_LENGTH_SH_BIG));
3868 }
3869 else
3870 {
3871 srel.r_index[2] = r_index >> 16;
3872 srel.r_index[1] = r_index >> 8;
3873 srel.r_index[0] = r_index;
3874 srel.r_type[0] =
3875 ((r_extern ? RELOC_STD_BITS_EXTERN_LITTLE : 0)
3876 | (r_pcrel ? RELOC_STD_BITS_PCREL_LITTLE : 0)
3877 | (r_baserel ? RELOC_STD_BITS_BASEREL_LITTLE : 0)
3878 | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_LITTLE : 0)
3879 | (r_relative ? RELOC_STD_BITS_RELATIVE_LITTLE : 0)
3880 | (r_length << RELOC_STD_BITS_LENGTH_SH_LITTLE));
3881 }
3882 }
3883 #endif
3884 rel_ptr = (void *) &srel;
3885
3886 /* We have to write the addend into the object file, since
3887 standard a.out relocs are in place. It would be more
3888 reliable if we had the current contents of the file here,
3889 rather than assuming zeroes, but we can't read the file since
3890 it was opened using bfd_openw. */
3891 if (pr->addend != 0)
3892 {
3893 bfd_size_type size;
3894 bfd_reloc_status_type r;
3895 bfd_byte *buf;
3896 bfd_boolean ok;
3897
3898 size = bfd_get_reloc_size (howto);
3899 buf = (bfd_byte *) bfd_zmalloc (size);
3900 if (buf == NULL && size != 0)
3901 return FALSE;
3902 r = MY_relocate_contents (howto, flaginfo->output_bfd,
3903 (bfd_vma) pr->addend, buf);
3904 switch (r)
3905 {
3906 case bfd_reloc_ok:
3907 break;
3908 default:
3909 case bfd_reloc_outofrange:
3910 abort ();
3911 case bfd_reloc_overflow:
3912 (*flaginfo->info->callbacks->reloc_overflow)
3913 (flaginfo->info, NULL,
3914 (p->type == bfd_section_reloc_link_order
3915 ? bfd_section_name (flaginfo->output_bfd,
3916 pr->u.section)
3917 : pr->u.name),
3918 howto->name, pr->addend, NULL, NULL, (bfd_vma) 0);
3919 break;
3920 }
3921 ok = bfd_set_section_contents (flaginfo->output_bfd, o, (void *) buf,
3922 (file_ptr) p->offset, size);
3923 free (buf);
3924 if (! ok)
3925 return FALSE;
3926 }
3927 }
3928 else
3929 {
3930 #ifdef MY_put_ext_reloc
3931 MY_put_ext_reloc (flaginfo->output_bfd, r_extern, r_index, p->offset,
3932 howto, &erel, pr->addend);
3933 #else
3934 PUT_WORD (flaginfo->output_bfd, p->offset, erel.r_address);
3935
3936 if (bfd_header_big_endian (flaginfo->output_bfd))
3937 {
3938 erel.r_index[0] = r_index >> 16;
3939 erel.r_index[1] = r_index >> 8;
3940 erel.r_index[2] = r_index;
3941 erel.r_type[0] =
3942 ((r_extern ? RELOC_EXT_BITS_EXTERN_BIG : 0)
3943 | (howto->type << RELOC_EXT_BITS_TYPE_SH_BIG));
3944 }
3945 else
3946 {
3947 erel.r_index[2] = r_index >> 16;
3948 erel.r_index[1] = r_index >> 8;
3949 erel.r_index[0] = r_index;
3950 erel.r_type[0] =
3951 (r_extern ? RELOC_EXT_BITS_EXTERN_LITTLE : 0)
3952 | (howto->type << RELOC_EXT_BITS_TYPE_SH_LITTLE);
3953 }
3954
3955 PUT_WORD (flaginfo->output_bfd, (bfd_vma) pr->addend, erel.r_addend);
3956 #endif /* MY_put_ext_reloc */
3957
3958 rel_ptr = (void *) &erel;
3959 }
3960
3961 amt = obj_reloc_entry_size (flaginfo->output_bfd);
3962 if (bfd_seek (flaginfo->output_bfd, *reloff_ptr, SEEK_SET) != 0
3963 || bfd_bwrite (rel_ptr, amt, flaginfo->output_bfd) != amt)
3964 return FALSE;
3965
3966 *reloff_ptr += obj_reloc_entry_size (flaginfo->output_bfd);
3967
3968 /* Assert that the relocs have not run into the symbols, and that n
3969 the text relocs have not run into the data relocs. */
3970 BFD_ASSERT (*reloff_ptr <= obj_sym_filepos (flaginfo->output_bfd)
3971 && (reloff_ptr != &flaginfo->treloff
3972 || (*reloff_ptr
3973 <= obj_datasec (flaginfo->output_bfd)->rel_filepos)));
3974
3975 return TRUE;
3976 }
3977
3978 /* Get the section corresponding to a reloc index. */
3979
3980 static INLINE asection *
3981 aout_reloc_index_to_section (bfd *abfd, int indx)
3982 {
3983 switch (indx & N_TYPE)
3984 {
3985 case N_TEXT: return obj_textsec (abfd);
3986 case N_DATA: return obj_datasec (abfd);
3987 case N_BSS: return obj_bsssec (abfd);
3988 case N_ABS:
3989 case N_UNDF: return bfd_abs_section_ptr;
3990 default: abort ();
3991 }
3992 return NULL;
3993 }
3994
3995 /* Relocate an a.out section using standard a.out relocs. */
3996
3997 static bfd_boolean
3998 aout_link_input_section_std (struct aout_final_link_info *flaginfo,
3999 bfd *input_bfd,
4000 asection *input_section,
4001 struct reloc_std_external *relocs,
4002 bfd_size_type rel_size,
4003 bfd_byte *contents)
4004 {
4005 bfd_boolean (*check_dynamic_reloc)
4006 (struct bfd_link_info *, bfd *, asection *,
4007 struct aout_link_hash_entry *, void *, bfd_byte *, bfd_boolean *,
4008 bfd_vma *);
4009 bfd *output_bfd;
4010 bfd_boolean relocatable;
4011 struct external_nlist *syms;
4012 char *strings;
4013 struct aout_link_hash_entry **sym_hashes;
4014 int *symbol_map;
4015 bfd_size_type reloc_count;
4016 struct reloc_std_external *rel;
4017 struct reloc_std_external *rel_end;
4018
4019 output_bfd = flaginfo->output_bfd;
4020 check_dynamic_reloc = aout_backend_info (output_bfd)->check_dynamic_reloc;
4021
4022 BFD_ASSERT (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE);
4023 BFD_ASSERT (input_bfd->xvec->header_byteorder
4024 == output_bfd->xvec->header_byteorder);
4025
4026 relocatable = bfd_link_relocatable (flaginfo->info);
4027 syms = obj_aout_external_syms (input_bfd);
4028 strings = obj_aout_external_strings (input_bfd);
4029 sym_hashes = obj_aout_sym_hashes (input_bfd);
4030 symbol_map = flaginfo->symbol_map;
4031
4032 reloc_count = rel_size / RELOC_STD_SIZE;
4033 rel = relocs;
4034 rel_end = rel + reloc_count;
4035 for (; rel < rel_end; rel++)
4036 {
4037 bfd_vma r_addr;
4038 int r_index;
4039 int r_extern;
4040 int r_pcrel;
4041 int r_baserel = 0;
4042 reloc_howto_type *howto;
4043 struct aout_link_hash_entry *h = NULL;
4044 bfd_vma relocation;
4045 bfd_reloc_status_type r;
4046
4047 r_addr = GET_SWORD (input_bfd, rel->r_address);
4048
4049 #ifdef MY_reloc_howto
4050 howto = MY_reloc_howto (input_bfd, rel, r_index, r_extern, r_pcrel);
4051 #else
4052 {
4053 int r_jmptable;
4054 int r_relative;
4055 int r_length;
4056 unsigned int howto_idx;
4057
4058 if (bfd_header_big_endian (input_bfd))
4059 {
4060 r_index = (((unsigned int) rel->r_index[0] << 16)
4061 | ((unsigned int) rel->r_index[1] << 8)
4062 | rel->r_index[2]);
4063 r_extern = (0 != (rel->r_type[0] & RELOC_STD_BITS_EXTERN_BIG));
4064 r_pcrel = (0 != (rel->r_type[0] & RELOC_STD_BITS_PCREL_BIG));
4065 r_baserel = (0 != (rel->r_type[0] & RELOC_STD_BITS_BASEREL_BIG));
4066 r_jmptable= (0 != (rel->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG));
4067 r_relative= (0 != (rel->r_type[0] & RELOC_STD_BITS_RELATIVE_BIG));
4068 r_length = ((rel->r_type[0] & RELOC_STD_BITS_LENGTH_BIG)
4069 >> RELOC_STD_BITS_LENGTH_SH_BIG);
4070 }
4071 else
4072 {
4073 r_index = (((unsigned int) rel->r_index[2] << 16)
4074 | ((unsigned int) rel->r_index[1] << 8)
4075 | rel->r_index[0]);
4076 r_extern = (0 != (rel->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE));
4077 r_pcrel = (0 != (rel->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
4078 r_baserel = (0 != (rel->r_type[0]
4079 & RELOC_STD_BITS_BASEREL_LITTLE));
4080 r_jmptable= (0 != (rel->r_type[0]
4081 & RELOC_STD_BITS_JMPTABLE_LITTLE));
4082 r_relative= (0 != (rel->r_type[0]
4083 & RELOC_STD_BITS_RELATIVE_LITTLE));
4084 r_length = ((rel->r_type[0] & RELOC_STD_BITS_LENGTH_LITTLE)
4085 >> RELOC_STD_BITS_LENGTH_SH_LITTLE);
4086 }
4087
4088 howto_idx = (r_length + 4 * r_pcrel + 8 * r_baserel
4089 + 16 * r_jmptable + 32 * r_relative);
4090 if (howto_idx < TABLE_SIZE (howto_table_std))
4091 howto = howto_table_std + howto_idx;
4092 else
4093 howto = NULL;
4094 }
4095 #endif
4096
4097 if (howto == NULL)
4098 {
4099 _bfd_error_handler (_("%pB: unsupported relocation type"),
4100 input_bfd);
4101 bfd_set_error (bfd_error_bad_value);
4102 return FALSE;
4103 }
4104
4105 if (relocatable)
4106 {
4107 /* We are generating a relocatable output file, and must
4108 modify the reloc accordingly. */
4109 if (r_extern)
4110 {
4111 /* If we know the symbol this relocation is against,
4112 convert it into a relocation against a section. This
4113 is what the native linker does. */
4114 h = sym_hashes[r_index];
4115 if (h != NULL
4116 && (h->root.type == bfd_link_hash_defined
4117 || h->root.type == bfd_link_hash_defweak))
4118 {
4119 asection *output_section;
4120
4121 /* Change the r_extern value. */
4122 if (bfd_header_big_endian (output_bfd))
4123 rel->r_type[0] &=~ RELOC_STD_BITS_EXTERN_BIG;
4124 else
4125 rel->r_type[0] &=~ RELOC_STD_BITS_EXTERN_LITTLE;
4126
4127 /* Compute a new r_index. */
4128 output_section = h->root.u.def.section->output_section;
4129 if (output_section == obj_textsec (output_bfd))
4130 r_index = N_TEXT;
4131 else if (output_section == obj_datasec (output_bfd))
4132 r_index = N_DATA;
4133 else if (output_section == obj_bsssec (output_bfd))
4134 r_index = N_BSS;
4135 else
4136 r_index = N_ABS;
4137
4138 /* Add the symbol value and the section VMA to the
4139 addend stored in the contents. */
4140 relocation = (h->root.u.def.value
4141 + output_section->vma
4142 + h->root.u.def.section->output_offset);
4143 }
4144 else
4145 {
4146 /* We must change r_index according to the symbol
4147 map. */
4148 r_index = symbol_map[r_index];
4149
4150 if (r_index == -1)
4151 {
4152 if (h != NULL)
4153 {
4154 /* We decided to strip this symbol, but it
4155 turns out that we can't. Note that we
4156 lose the other and desc information here.
4157 I don't think that will ever matter for a
4158 global symbol. */
4159 if (h->indx < 0)
4160 {
4161 h->indx = -2;
4162 h->written = FALSE;
4163 if (!aout_link_write_other_symbol (&h->root.root,
4164 flaginfo))
4165 return FALSE;
4166 }
4167 r_index = h->indx;
4168 }
4169 else
4170 {
4171 const char *name;
4172
4173 name = strings + GET_WORD (input_bfd,
4174 syms[r_index].e_strx);
4175 (*flaginfo->info->callbacks->unattached_reloc)
4176 (flaginfo->info, name,
4177 input_bfd, input_section, r_addr);
4178 r_index = 0;
4179 }
4180 }
4181
4182 relocation = 0;
4183 }
4184
4185 /* Write out the new r_index value. */
4186 if (bfd_header_big_endian (output_bfd))
4187 {
4188 rel->r_index[0] = r_index >> 16;
4189 rel->r_index[1] = r_index >> 8;
4190 rel->r_index[2] = r_index;
4191 }
4192 else
4193 {
4194 rel->r_index[2] = r_index >> 16;
4195 rel->r_index[1] = r_index >> 8;
4196 rel->r_index[0] = r_index;
4197 }
4198 }
4199 else
4200 {
4201 asection *section;
4202
4203 /* This is a relocation against a section. We must
4204 adjust by the amount that the section moved. */
4205 section = aout_reloc_index_to_section (input_bfd, r_index);
4206 relocation = (section->output_section->vma
4207 + section->output_offset
4208 - section->vma);
4209 }
4210
4211 /* Change the address of the relocation. */
4212 PUT_WORD (output_bfd,
4213 r_addr + input_section->output_offset,
4214 rel->r_address);
4215
4216 /* Adjust a PC relative relocation by removing the reference
4217 to the original address in the section and including the
4218 reference to the new address. */
4219 if (r_pcrel)
4220 relocation -= (input_section->output_section->vma
4221 + input_section->output_offset
4222 - input_section->vma);
4223
4224 #ifdef MY_relocatable_reloc
4225 MY_relocatable_reloc (howto, output_bfd, rel, relocation, r_addr);
4226 #endif
4227
4228 if (relocation == 0)
4229 r = bfd_reloc_ok;
4230 else
4231 r = MY_relocate_contents (howto,
4232 input_bfd, relocation,
4233 contents + r_addr);
4234 }
4235 else
4236 {
4237 bfd_boolean hundef;
4238
4239 /* We are generating an executable, and must do a full
4240 relocation. */
4241 hundef = FALSE;
4242
4243 if (r_extern)
4244 {
4245 h = sym_hashes[r_index];
4246
4247 if (h != NULL
4248 && (h->root.type == bfd_link_hash_defined
4249 || h->root.type == bfd_link_hash_defweak))
4250 {
4251 relocation = (h->root.u.def.value
4252 + h->root.u.def.section->output_section->vma
4253 + h->root.u.def.section->output_offset);
4254 }
4255 else if (h != NULL
4256 && h->root.type == bfd_link_hash_undefweak)
4257 relocation = 0;
4258 else
4259 {
4260 hundef = TRUE;
4261 relocation = 0;
4262 }
4263 }
4264 else
4265 {
4266 asection *section;
4267
4268 section = aout_reloc_index_to_section (input_bfd, r_index);
4269 relocation = (section->output_section->vma
4270 + section->output_offset
4271 - section->vma);
4272 if (r_pcrel)
4273 relocation += input_section->vma;
4274 }
4275
4276 if (check_dynamic_reloc != NULL)
4277 {
4278 bfd_boolean skip;
4279
4280 if (! ((*check_dynamic_reloc)
4281 (flaginfo->info, input_bfd, input_section, h,
4282 (void *) rel, contents, &skip, &relocation)))
4283 return FALSE;
4284 if (skip)
4285 continue;
4286 }
4287
4288 /* Now warn if a global symbol is undefined. We could not
4289 do this earlier, because check_dynamic_reloc might want
4290 to skip this reloc. */
4291 if (hundef && ! bfd_link_pic (flaginfo->info) && ! r_baserel)
4292 {
4293 const char *name;
4294
4295 if (h != NULL)
4296 name = h->root.root.string;
4297 else
4298 name = strings + GET_WORD (input_bfd, syms[r_index].e_strx);
4299 (*flaginfo->info->callbacks->undefined_symbol)
4300 (flaginfo->info, name, input_bfd, input_section, r_addr, TRUE);
4301 }
4302
4303 r = MY_final_link_relocate (howto,
4304 input_bfd, input_section,
4305 contents, r_addr, relocation,
4306 (bfd_vma) 0);
4307 }
4308
4309 if (r != bfd_reloc_ok)
4310 {
4311 switch (r)
4312 {
4313 default:
4314 case bfd_reloc_outofrange:
4315 abort ();
4316 case bfd_reloc_overflow:
4317 {
4318 const char *name;
4319
4320 if (h != NULL)
4321 name = NULL;
4322 else if (r_extern)
4323 name = strings + GET_WORD (input_bfd,
4324 syms[r_index].e_strx);
4325 else
4326 {
4327 asection *s;
4328
4329 s = aout_reloc_index_to_section (input_bfd, r_index);
4330 name = bfd_section_name (input_bfd, s);
4331 }
4332 (*flaginfo->info->callbacks->reloc_overflow)
4333 (flaginfo->info, (h ? &h->root : NULL), name, howto->name,
4334 (bfd_vma) 0, input_bfd, input_section, r_addr);
4335 }
4336 break;
4337 }
4338 }
4339 }
4340
4341 return TRUE;
4342 }
4343
4344 /* Relocate an a.out section using extended a.out relocs. */
4345
4346 static bfd_boolean
4347 aout_link_input_section_ext (struct aout_final_link_info *flaginfo,
4348 bfd *input_bfd,
4349 asection *input_section,
4350 struct reloc_ext_external *relocs,
4351 bfd_size_type rel_size,
4352 bfd_byte *contents)
4353 {
4354 bfd_boolean (*check_dynamic_reloc)
4355 (struct bfd_link_info *, bfd *, asection *,
4356 struct aout_link_hash_entry *, void *, bfd_byte *, bfd_boolean *,
4357 bfd_vma *);
4358 bfd *output_bfd;
4359 bfd_boolean relocatable;
4360 struct external_nlist *syms;
4361 char *strings;
4362 struct aout_link_hash_entry **sym_hashes;
4363 int *symbol_map;
4364 bfd_size_type reloc_count;
4365 struct reloc_ext_external *rel;
4366 struct reloc_ext_external *rel_end;
4367
4368 output_bfd = flaginfo->output_bfd;
4369 check_dynamic_reloc = aout_backend_info (output_bfd)->check_dynamic_reloc;
4370
4371 BFD_ASSERT (obj_reloc_entry_size (input_bfd) == RELOC_EXT_SIZE);
4372 BFD_ASSERT (input_bfd->xvec->header_byteorder
4373 == output_bfd->xvec->header_byteorder);
4374
4375 relocatable = bfd_link_relocatable (flaginfo->info);
4376 syms = obj_aout_external_syms (input_bfd);
4377 strings = obj_aout_external_strings (input_bfd);
4378 sym_hashes = obj_aout_sym_hashes (input_bfd);
4379 symbol_map = flaginfo->symbol_map;
4380
4381 reloc_count = rel_size / RELOC_EXT_SIZE;
4382 rel = relocs;
4383 rel_end = rel + reloc_count;
4384 for (; rel < rel_end; rel++)
4385 {
4386 bfd_vma r_addr;
4387 int r_index;
4388 int r_extern;
4389 unsigned int r_type;
4390 bfd_vma r_addend;
4391 struct aout_link_hash_entry *h = NULL;
4392 asection *r_section = NULL;
4393 bfd_vma relocation;
4394
4395 r_addr = GET_SWORD (input_bfd, rel->r_address);
4396
4397 if (bfd_header_big_endian (input_bfd))
4398 {
4399 r_index = (((unsigned int) rel->r_index[0] << 16)
4400 | ((unsigned int) rel->r_index[1] << 8)
4401 | rel->r_index[2]);
4402 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG));
4403 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
4404 >> RELOC_EXT_BITS_TYPE_SH_BIG);
4405 }
4406 else
4407 {
4408 r_index = (((unsigned int) rel->r_index[2] << 16)
4409 | ((unsigned int) rel->r_index[1] << 8)
4410 | rel->r_index[0]);
4411 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
4412 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
4413 >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
4414 }
4415
4416 r_addend = GET_SWORD (input_bfd, rel->r_addend);
4417
4418 if (r_type >= TABLE_SIZE (howto_table_ext))
4419 {
4420 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
4421 input_bfd, r_type);
4422 bfd_set_error (bfd_error_bad_value);
4423 return FALSE;
4424 }
4425
4426 if (relocatable)
4427 {
4428 /* We are generating a relocatable output file, and must
4429 modify the reloc accordingly. */
4430 if (r_extern
4431 || r_type == (unsigned int) RELOC_BASE10
4432 || r_type == (unsigned int) RELOC_BASE13
4433 || r_type == (unsigned int) RELOC_BASE22)
4434 {
4435 /* If we know the symbol this relocation is against,
4436 convert it into a relocation against a section. This
4437 is what the native linker does. */
4438 if (r_type == (unsigned int) RELOC_BASE10
4439 || r_type == (unsigned int) RELOC_BASE13
4440 || r_type == (unsigned int) RELOC_BASE22)
4441 h = NULL;
4442 else
4443 h = sym_hashes[r_index];
4444 if (h != NULL
4445 && (h->root.type == bfd_link_hash_defined
4446 || h->root.type == bfd_link_hash_defweak))
4447 {
4448 asection *output_section;
4449
4450 /* Change the r_extern value. */
4451 if (bfd_header_big_endian (output_bfd))
4452 rel->r_type[0] &=~ RELOC_EXT_BITS_EXTERN_BIG;
4453 else
4454 rel->r_type[0] &=~ RELOC_EXT_BITS_EXTERN_LITTLE;
4455
4456 /* Compute a new r_index. */
4457 output_section = h->root.u.def.section->output_section;
4458 if (output_section == obj_textsec (output_bfd))
4459 r_index = N_TEXT;
4460 else if (output_section == obj_datasec (output_bfd))
4461 r_index = N_DATA;
4462 else if (output_section == obj_bsssec (output_bfd))
4463 r_index = N_BSS;
4464 else
4465 r_index = N_ABS;
4466
4467 /* Add the symbol value and the section VMA to the
4468 addend. */
4469 relocation = (h->root.u.def.value
4470 + output_section->vma
4471 + h->root.u.def.section->output_offset);
4472
4473 /* Now RELOCATION is the VMA of the final
4474 destination. If this is a PC relative reloc,
4475 then ADDEND is the negative of the source VMA.
4476 We want to set ADDEND to the difference between
4477 the destination VMA and the source VMA, which
4478 means we must adjust RELOCATION by the change in
4479 the source VMA. This is done below. */
4480 }
4481 else
4482 {
4483 /* We must change r_index according to the symbol
4484 map. */
4485 r_index = symbol_map[r_index];
4486
4487 if (r_index == -1)
4488 {
4489 if (h != NULL)
4490 {
4491 /* We decided to strip this symbol, but it
4492 turns out that we can't. Note that we
4493 lose the other and desc information here.
4494 I don't think that will ever matter for a
4495 global symbol. */
4496 if (h->indx < 0)
4497 {
4498 h->indx = -2;
4499 h->written = FALSE;
4500 if (!aout_link_write_other_symbol (&h->root.root,
4501 flaginfo))
4502 return FALSE;
4503 }
4504 r_index = h->indx;
4505 }
4506 else
4507 {
4508 const char *name;
4509
4510 name = strings + GET_WORD (input_bfd,
4511 syms[r_index].e_strx);
4512 (*flaginfo->info->callbacks->unattached_reloc)
4513 (flaginfo->info, name,
4514 input_bfd, input_section, r_addr);
4515 r_index = 0;
4516 }
4517 }
4518
4519 relocation = 0;
4520
4521 /* If this is a PC relative reloc, then the addend
4522 is the negative of the source VMA. We must
4523 adjust it by the change in the source VMA. This
4524 is done below. */
4525 }
4526
4527 /* Write out the new r_index value. */
4528 if (bfd_header_big_endian (output_bfd))
4529 {
4530 rel->r_index[0] = r_index >> 16;
4531 rel->r_index[1] = r_index >> 8;
4532 rel->r_index[2] = r_index;
4533 }
4534 else
4535 {
4536 rel->r_index[2] = r_index >> 16;
4537 rel->r_index[1] = r_index >> 8;
4538 rel->r_index[0] = r_index;
4539 }
4540 }
4541 else
4542 {
4543 /* This is a relocation against a section. We must
4544 adjust by the amount that the section moved. */
4545 r_section = aout_reloc_index_to_section (input_bfd, r_index);
4546 relocation = (r_section->output_section->vma
4547 + r_section->output_offset
4548 - r_section->vma);
4549
4550 /* If this is a PC relative reloc, then the addend is
4551 the difference in VMA between the destination and the
4552 source. We have just adjusted for the change in VMA
4553 of the destination, so we must also adjust by the
4554 change in VMA of the source. This is done below. */
4555 }
4556
4557 /* As described above, we must always adjust a PC relative
4558 reloc by the change in VMA of the source. However, if
4559 pcrel_offset is set, then the addend does not include the
4560 location within the section, in which case we don't need
4561 to adjust anything. */
4562 if (howto_table_ext[r_type].pc_relative
4563 && ! howto_table_ext[r_type].pcrel_offset)
4564 relocation -= (input_section->output_section->vma
4565 + input_section->output_offset
4566 - input_section->vma);
4567
4568 /* Change the addend if necessary. */
4569 if (relocation != 0)
4570 PUT_WORD (output_bfd, r_addend + relocation, rel->r_addend);
4571
4572 /* Change the address of the relocation. */
4573 PUT_WORD (output_bfd,
4574 r_addr + input_section->output_offset,
4575 rel->r_address);
4576 }
4577 else
4578 {
4579 bfd_boolean hundef;
4580 bfd_reloc_status_type r;
4581
4582 /* We are generating an executable, and must do a full
4583 relocation. */
4584 hundef = FALSE;
4585
4586 if (r_extern)
4587 {
4588 h = sym_hashes[r_index];
4589
4590 if (h != NULL
4591 && (h->root.type == bfd_link_hash_defined
4592 || h->root.type == bfd_link_hash_defweak))
4593 {
4594 relocation = (h->root.u.def.value
4595 + h->root.u.def.section->output_section->vma
4596 + h->root.u.def.section->output_offset);
4597 }
4598 else if (h != NULL
4599 && h->root.type == bfd_link_hash_undefweak)
4600 relocation = 0;
4601 else
4602 {
4603 hundef = TRUE;
4604 relocation = 0;
4605 }
4606 }
4607 else if (r_type == (unsigned int) RELOC_BASE10
4608 || r_type == (unsigned int) RELOC_BASE13
4609 || r_type == (unsigned int) RELOC_BASE22)
4610 {
4611 struct external_nlist *sym;
4612 int type;
4613
4614 /* For base relative relocs, r_index is always an index
4615 into the symbol table, even if r_extern is 0. */
4616 sym = syms + r_index;
4617 type = H_GET_8 (input_bfd, sym->e_type);
4618 if ((type & N_TYPE) == N_TEXT
4619 || type == N_WEAKT)
4620 r_section = obj_textsec (input_bfd);
4621 else if ((type & N_TYPE) == N_DATA
4622 || type == N_WEAKD)
4623 r_section = obj_datasec (input_bfd);
4624 else if ((type & N_TYPE) == N_BSS
4625 || type == N_WEAKB)
4626 r_section = obj_bsssec (input_bfd);
4627 else if ((type & N_TYPE) == N_ABS
4628 || type == N_WEAKA)
4629 r_section = bfd_abs_section_ptr;
4630 else
4631 abort ();
4632 relocation = (r_section->output_section->vma
4633 + r_section->output_offset
4634 + (GET_WORD (input_bfd, sym->e_value)
4635 - r_section->vma));
4636 }
4637 else
4638 {
4639 r_section = aout_reloc_index_to_section (input_bfd, r_index);
4640
4641 /* If this is a PC relative reloc, then R_ADDEND is the
4642 difference between the two vmas, or
4643 old_dest_sec + old_dest_off - (old_src_sec + old_src_off)
4644 where
4645 old_dest_sec == section->vma
4646 and
4647 old_src_sec == input_section->vma
4648 and
4649 old_src_off == r_addr
4650
4651 _bfd_final_link_relocate expects RELOCATION +
4652 R_ADDEND to be the VMA of the destination minus
4653 r_addr (the minus r_addr is because this relocation
4654 is not pcrel_offset, which is a bit confusing and
4655 should, perhaps, be changed), or
4656 new_dest_sec
4657 where
4658 new_dest_sec == output_section->vma + output_offset
4659 We arrange for this to happen by setting RELOCATION to
4660 new_dest_sec + old_src_sec - old_dest_sec
4661
4662 If this is not a PC relative reloc, then R_ADDEND is
4663 simply the VMA of the destination, so we set
4664 RELOCATION to the change in the destination VMA, or
4665 new_dest_sec - old_dest_sec
4666 */
4667 relocation = (r_section->output_section->vma
4668 + r_section->output_offset
4669 - r_section->vma);
4670 if (howto_table_ext[r_type].pc_relative)
4671 relocation += input_section->vma;
4672 }
4673
4674 if (check_dynamic_reloc != NULL)
4675 {
4676 bfd_boolean skip;
4677
4678 if (! ((*check_dynamic_reloc)
4679 (flaginfo->info, input_bfd, input_section, h,
4680 (void *) rel, contents, &skip, &relocation)))
4681 return FALSE;
4682 if (skip)
4683 continue;
4684 }
4685
4686 /* Now warn if a global symbol is undefined. We could not
4687 do this earlier, because check_dynamic_reloc might want
4688 to skip this reloc. */
4689 if (hundef
4690 && ! bfd_link_pic (flaginfo->info)
4691 && r_type != (unsigned int) RELOC_BASE10
4692 && r_type != (unsigned int) RELOC_BASE13
4693 && r_type != (unsigned int) RELOC_BASE22)
4694 {
4695 const char *name;
4696
4697 if (h != NULL)
4698 name = h->root.root.string;
4699 else
4700 name = strings + GET_WORD (input_bfd, syms[r_index].e_strx);
4701 (*flaginfo->info->callbacks->undefined_symbol)
4702 (flaginfo->info, name, input_bfd, input_section, r_addr, TRUE);
4703 }
4704
4705 if (r_type != (unsigned int) RELOC_SPARC_REV32)
4706 r = MY_final_link_relocate (howto_table_ext + r_type,
4707 input_bfd, input_section,
4708 contents, r_addr, relocation,
4709 r_addend);
4710 else
4711 {
4712 bfd_vma x;
4713
4714 x = bfd_get_32 (input_bfd, contents + r_addr);
4715 x = x + relocation + r_addend;
4716 bfd_putl32 (/*input_bfd,*/ x, contents + r_addr);
4717 r = bfd_reloc_ok;
4718 }
4719
4720 if (r != bfd_reloc_ok)
4721 {
4722 switch (r)
4723 {
4724 default:
4725 case bfd_reloc_outofrange:
4726 abort ();
4727 case bfd_reloc_overflow:
4728 {
4729 const char *name;
4730
4731 if (h != NULL)
4732 name = NULL;
4733 else if (r_extern
4734 || r_type == (unsigned int) RELOC_BASE10
4735 || r_type == (unsigned int) RELOC_BASE13
4736 || r_type == (unsigned int) RELOC_BASE22)
4737 name = strings + GET_WORD (input_bfd,
4738 syms[r_index].e_strx);
4739 else
4740 {
4741 asection *s;
4742
4743 s = aout_reloc_index_to_section (input_bfd, r_index);
4744 name = bfd_section_name (input_bfd, s);
4745 }
4746 (*flaginfo->info->callbacks->reloc_overflow)
4747 (flaginfo->info, (h ? &h->root : NULL), name,
4748 howto_table_ext[r_type].name,
4749 r_addend, input_bfd, input_section, r_addr);
4750 }
4751 break;
4752 }
4753 }
4754 }
4755 }
4756
4757 return TRUE;
4758 }
4759
4760 /* Link an a.out section into the output file. */
4761
4762 static bfd_boolean
4763 aout_link_input_section (struct aout_final_link_info *flaginfo,
4764 bfd *input_bfd,
4765 asection *input_section,
4766 file_ptr *reloff_ptr,
4767 bfd_size_type rel_size)
4768 {
4769 bfd_size_type input_size;
4770 void * relocs;
4771
4772 /* Get the section contents. */
4773 input_size = input_section->size;
4774 if (! bfd_get_section_contents (input_bfd, input_section,
4775 (void *) flaginfo->contents,
4776 (file_ptr) 0, input_size))
4777 return FALSE;
4778
4779 /* Read in the relocs if we haven't already done it. */
4780 if (aout_section_data (input_section) != NULL
4781 && aout_section_data (input_section)->relocs != NULL)
4782 relocs = aout_section_data (input_section)->relocs;
4783 else
4784 {
4785 relocs = flaginfo->relocs;
4786 if (rel_size > 0)
4787 {
4788 if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0
4789 || bfd_bread (relocs, rel_size, input_bfd) != rel_size)
4790 return FALSE;
4791 }
4792 }
4793
4794 /* Relocate the section contents. */
4795 if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE)
4796 {
4797 if (! aout_link_input_section_std (flaginfo, input_bfd, input_section,
4798 (struct reloc_std_external *) relocs,
4799 rel_size, flaginfo->contents))
4800 return FALSE;
4801 }
4802 else
4803 {
4804 if (! aout_link_input_section_ext (flaginfo, input_bfd, input_section,
4805 (struct reloc_ext_external *) relocs,
4806 rel_size, flaginfo->contents))
4807 return FALSE;
4808 }
4809
4810 /* Write out the section contents. */
4811 if (! bfd_set_section_contents (flaginfo->output_bfd,
4812 input_section->output_section,
4813 (void *) flaginfo->contents,
4814 (file_ptr) input_section->output_offset,
4815 input_size))
4816 return FALSE;
4817
4818 /* If we are producing relocatable output, the relocs were
4819 modified, and we now write them out. */
4820 if (bfd_link_relocatable (flaginfo->info) && rel_size > 0)
4821 {
4822 if (bfd_seek (flaginfo->output_bfd, *reloff_ptr, SEEK_SET) != 0)
4823 return FALSE;
4824 if (bfd_bwrite (relocs, rel_size, flaginfo->output_bfd) != rel_size)
4825 return FALSE;
4826 *reloff_ptr += rel_size;
4827
4828 /* Assert that the relocs have not run into the symbols, and
4829 that if these are the text relocs they have not run into the
4830 data relocs. */
4831 BFD_ASSERT (*reloff_ptr <= obj_sym_filepos (flaginfo->output_bfd)
4832 && (reloff_ptr != &flaginfo->treloff
4833 || (*reloff_ptr
4834 <= obj_datasec (flaginfo->output_bfd)->rel_filepos)));
4835 }
4836
4837 return TRUE;
4838 }
4839
4840 /* Adjust and write out the symbols for an a.out file. Set the new
4841 symbol indices into a symbol_map. */
4842
4843 static bfd_boolean
4844 aout_link_write_symbols (struct aout_final_link_info *flaginfo, bfd *input_bfd)
4845 {
4846 bfd *output_bfd;
4847 bfd_size_type sym_count;
4848 char *strings;
4849 enum bfd_link_strip strip;
4850 enum bfd_link_discard discard;
4851 struct external_nlist *outsym;
4852 bfd_size_type strtab_index;
4853 struct external_nlist *sym;
4854 struct external_nlist *sym_end;
4855 struct aout_link_hash_entry **sym_hash;
4856 int *symbol_map;
4857 bfd_boolean pass;
4858 bfd_boolean skip_next;
4859
4860 output_bfd = flaginfo->output_bfd;
4861 sym_count = obj_aout_external_sym_count (input_bfd);
4862 strings = obj_aout_external_strings (input_bfd);
4863 strip = flaginfo->info->strip;
4864 discard = flaginfo->info->discard;
4865 outsym = flaginfo->output_syms;
4866
4867 /* First write out a symbol for this object file, unless we are
4868 discarding such symbols. */
4869 if (strip != strip_all
4870 && (strip != strip_some
4871 || bfd_hash_lookup (flaginfo->info->keep_hash, input_bfd->filename,
4872 FALSE, FALSE) != NULL)
4873 && discard != discard_all)
4874 {
4875 H_PUT_8 (output_bfd, N_TEXT, outsym->e_type);
4876 H_PUT_8 (output_bfd, 0, outsym->e_other);
4877 H_PUT_16 (output_bfd, 0, outsym->e_desc);
4878 strtab_index = add_to_stringtab (output_bfd, flaginfo->strtab,
4879 input_bfd->filename, FALSE);
4880 if (strtab_index == (bfd_size_type) -1)
4881 return FALSE;
4882 PUT_WORD (output_bfd, strtab_index, outsym->e_strx);
4883 PUT_WORD (output_bfd,
4884 (bfd_get_section_vma (output_bfd,
4885 obj_textsec (input_bfd)->output_section)
4886 + obj_textsec (input_bfd)->output_offset),
4887 outsym->e_value);
4888 ++obj_aout_external_sym_count (output_bfd);
4889 ++outsym;
4890 }
4891
4892 pass = FALSE;
4893 skip_next = FALSE;
4894 sym = obj_aout_external_syms (input_bfd);
4895 sym_end = sym + sym_count;
4896 sym_hash = obj_aout_sym_hashes (input_bfd);
4897 symbol_map = flaginfo->symbol_map;
4898 memset (symbol_map, 0, (size_t) sym_count * sizeof *symbol_map);
4899 for (; sym < sym_end; sym++, sym_hash++, symbol_map++)
4900 {
4901 const char *name;
4902 int type;
4903 struct aout_link_hash_entry *h;
4904 bfd_boolean skip;
4905 asection *symsec;
4906 bfd_vma val = 0;
4907 bfd_boolean copy;
4908
4909 /* We set *symbol_map to 0 above for all symbols. If it has
4910 already been set to -1 for this symbol, it means that we are
4911 discarding it because it appears in a duplicate header file.
4912 See the N_BINCL code below. */
4913 if (*symbol_map == -1)
4914 continue;
4915
4916 /* Initialize *symbol_map to -1, which means that the symbol was
4917 not copied into the output file. We will change it later if
4918 we do copy the symbol over. */
4919 *symbol_map = -1;
4920
4921 type = H_GET_8 (input_bfd, sym->e_type);
4922 name = strings + GET_WORD (input_bfd, sym->e_strx);
4923
4924 h = NULL;
4925
4926 if (pass)
4927 {
4928 /* Pass this symbol through. It is the target of an
4929 indirect or warning symbol. */
4930 val = GET_WORD (input_bfd, sym->e_value);
4931 pass = FALSE;
4932 }
4933 else if (skip_next)
4934 {
4935 /* Skip this symbol, which is the target of an indirect
4936 symbol that we have changed to no longer be an indirect
4937 symbol. */
4938 skip_next = FALSE;
4939 continue;
4940 }
4941 else
4942 {
4943 struct aout_link_hash_entry *hresolve;
4944
4945 /* We have saved the hash table entry for this symbol, if
4946 there is one. Note that we could just look it up again
4947 in the hash table, provided we first check that it is an
4948 external symbol. */
4949 h = *sym_hash;
4950
4951 /* Use the name from the hash table, in case the symbol was
4952 wrapped. */
4953 if (h != NULL
4954 && h->root.type != bfd_link_hash_warning)
4955 name = h->root.root.string;
4956
4957 /* If this is an indirect or warning symbol, then change
4958 hresolve to the base symbol. We also change *sym_hash so
4959 that the relocation routines relocate against the real
4960 symbol. */
4961 hresolve = h;
4962 if (h != (struct aout_link_hash_entry *) NULL
4963 && (h->root.type == bfd_link_hash_indirect
4964 || h->root.type == bfd_link_hash_warning))
4965 {
4966 hresolve = (struct aout_link_hash_entry *) h->root.u.i.link;
4967 while (hresolve->root.type == bfd_link_hash_indirect
4968 || hresolve->root.type == bfd_link_hash_warning)
4969 hresolve = ((struct aout_link_hash_entry *)
4970 hresolve->root.u.i.link);
4971 *sym_hash = hresolve;
4972 }
4973
4974 /* If the symbol has already been written out, skip it. */
4975 if (h != NULL
4976 && h->written)
4977 {
4978 if ((type & N_TYPE) == N_INDR
4979 || type == N_WARNING)
4980 skip_next = TRUE;
4981 *symbol_map = h->indx;
4982 continue;
4983 }
4984
4985 /* See if we are stripping this symbol. */
4986 skip = FALSE;
4987 switch (strip)
4988 {
4989 case strip_none:
4990 break;
4991 case strip_debugger:
4992 if ((type & N_STAB) != 0)
4993 skip = TRUE;
4994 break;
4995 case strip_some:
4996 if (bfd_hash_lookup (flaginfo->info->keep_hash, name, FALSE, FALSE)
4997 == NULL)
4998 skip = TRUE;
4999 break;
5000 case strip_all:
5001 skip = TRUE;
5002 break;
5003 }
5004 if (skip)
5005 {
5006 if (h != NULL)
5007 h->written = TRUE;
5008 continue;
5009 }
5010
5011 /* Get the value of the symbol. */
5012 if ((type & N_TYPE) == N_TEXT
5013 || type == N_WEAKT)
5014 symsec = obj_textsec (input_bfd);
5015 else if ((type & N_TYPE) == N_DATA
5016 || type == N_WEAKD)
5017 symsec = obj_datasec (input_bfd);
5018 else if ((type & N_TYPE) == N_BSS
5019 || type == N_WEAKB)
5020 symsec = obj_bsssec (input_bfd);
5021 else if ((type & N_TYPE) == N_ABS
5022 || type == N_WEAKA)
5023 symsec = bfd_abs_section_ptr;
5024 else if (((type & N_TYPE) == N_INDR
5025 && (hresolve == NULL
5026 || (hresolve->root.type != bfd_link_hash_defined
5027 && hresolve->root.type != bfd_link_hash_defweak
5028 && hresolve->root.type != bfd_link_hash_common)))
5029 || type == N_WARNING)
5030 {
5031 /* Pass the next symbol through unchanged. The
5032 condition above for indirect symbols is so that if
5033 the indirect symbol was defined, we output it with
5034 the correct definition so the debugger will
5035 understand it. */
5036 pass = TRUE;
5037 val = GET_WORD (input_bfd, sym->e_value);
5038 symsec = NULL;
5039 }
5040 else if ((type & N_STAB) != 0)
5041 {
5042 val = GET_WORD (input_bfd, sym->e_value);
5043 symsec = NULL;
5044 }
5045 else
5046 {
5047 /* If we get here with an indirect symbol, it means that
5048 we are outputting it with a real definition. In such
5049 a case we do not want to output the next symbol,
5050 which is the target of the indirection. */
5051 if ((type & N_TYPE) == N_INDR)
5052 skip_next = TRUE;
5053
5054 symsec = NULL;
5055
5056 /* We need to get the value from the hash table. We use
5057 hresolve so that if we have defined an indirect
5058 symbol we output the final definition. */
5059 if (h == NULL)
5060 {
5061 switch (type & N_TYPE)
5062 {
5063 case N_SETT:
5064 symsec = obj_textsec (input_bfd);
5065 break;
5066 case N_SETD:
5067 symsec = obj_datasec (input_bfd);
5068 break;
5069 case N_SETB:
5070 symsec = obj_bsssec (input_bfd);
5071 break;
5072 case N_SETA:
5073 symsec = bfd_abs_section_ptr;
5074 break;
5075 default:
5076 val = 0;
5077 break;
5078 }
5079 }
5080 else if (hresolve->root.type == bfd_link_hash_defined
5081 || hresolve->root.type == bfd_link_hash_defweak)
5082 {
5083 asection *input_section;
5084 asection *output_section;
5085
5086 /* This case usually means a common symbol which was
5087 turned into a defined symbol. */
5088 input_section = hresolve->root.u.def.section;
5089 output_section = input_section->output_section;
5090 BFD_ASSERT (bfd_is_abs_section (output_section)
5091 || output_section->owner == output_bfd);
5092 val = (hresolve->root.u.def.value
5093 + bfd_get_section_vma (output_bfd, output_section)
5094 + input_section->output_offset);
5095
5096 /* Get the correct type based on the section. If
5097 this is a constructed set, force it to be
5098 globally visible. */
5099 if (type == N_SETT
5100 || type == N_SETD
5101 || type == N_SETB
5102 || type == N_SETA)
5103 type |= N_EXT;
5104
5105 type &=~ N_TYPE;
5106
5107 if (output_section == obj_textsec (output_bfd))
5108 type |= (hresolve->root.type == bfd_link_hash_defined
5109 ? N_TEXT
5110 : N_WEAKT);
5111 else if (output_section == obj_datasec (output_bfd))
5112 type |= (hresolve->root.type == bfd_link_hash_defined
5113 ? N_DATA
5114 : N_WEAKD);
5115 else if (output_section == obj_bsssec (output_bfd))
5116 type |= (hresolve->root.type == bfd_link_hash_defined
5117 ? N_BSS
5118 : N_WEAKB);
5119 else
5120 type |= (hresolve->root.type == bfd_link_hash_defined
5121 ? N_ABS
5122 : N_WEAKA);
5123 }
5124 else if (hresolve->root.type == bfd_link_hash_common)
5125 val = hresolve->root.u.c.size;
5126 else if (hresolve->root.type == bfd_link_hash_undefweak)
5127 {
5128 val = 0;
5129 type = N_WEAKU;
5130 }
5131 else
5132 val = 0;
5133 }
5134 if (symsec != NULL)
5135 val = (symsec->output_section->vma
5136 + symsec->output_offset
5137 + (GET_WORD (input_bfd, sym->e_value)
5138 - symsec->vma));
5139
5140 /* If this is a global symbol set the written flag, and if
5141 it is a local symbol see if we should discard it. */
5142 if (h != NULL)
5143 {
5144 h->written = TRUE;
5145 h->indx = obj_aout_external_sym_count (output_bfd);
5146 }
5147 else if ((type & N_TYPE) != N_SETT
5148 && (type & N_TYPE) != N_SETD
5149 && (type & N_TYPE) != N_SETB
5150 && (type & N_TYPE) != N_SETA)
5151 {
5152 switch (discard)
5153 {
5154 case discard_none:
5155 case discard_sec_merge:
5156 break;
5157 case discard_l:
5158 if ((type & N_STAB) == 0
5159 && bfd_is_local_label_name (input_bfd, name))
5160 skip = TRUE;
5161 break;
5162 case discard_all:
5163 skip = TRUE;
5164 break;
5165 }
5166 if (skip)
5167 {
5168 pass = FALSE;
5169 continue;
5170 }
5171 }
5172
5173 /* An N_BINCL symbol indicates the start of the stabs
5174 entries for a header file. We need to scan ahead to the
5175 next N_EINCL symbol, ignoring nesting, adding up all the
5176 characters in the symbol names, not including the file
5177 numbers in types (the first number after an open
5178 parenthesis). */
5179 if (type == (int) N_BINCL)
5180 {
5181 struct external_nlist *incl_sym;
5182 int nest;
5183 struct aout_link_includes_entry *incl_entry;
5184 struct aout_link_includes_totals *t;
5185
5186 val = 0;
5187 nest = 0;
5188 for (incl_sym = sym + 1; incl_sym < sym_end; incl_sym++)
5189 {
5190 int incl_type;
5191
5192 incl_type = H_GET_8 (input_bfd, incl_sym->e_type);
5193 if (incl_type == (int) N_EINCL)
5194 {
5195 if (nest == 0)
5196 break;
5197 --nest;
5198 }
5199 else if (incl_type == (int) N_BINCL)
5200 ++nest;
5201 else if (nest == 0)
5202 {
5203 const char *s;
5204
5205 s = strings + GET_WORD (input_bfd, incl_sym->e_strx);
5206 for (; *s != '\0'; s++)
5207 {
5208 val += *s;
5209 if (*s == '(')
5210 {
5211 /* Skip the file number. */
5212 ++s;
5213 while (ISDIGIT (*s))
5214 ++s;
5215 --s;
5216 }
5217 }
5218 }
5219 }
5220
5221 /* If we have already included a header file with the
5222 same value, then replace this one with an N_EXCL
5223 symbol. */
5224 copy = (bfd_boolean) (! flaginfo->info->keep_memory);
5225 incl_entry = aout_link_includes_lookup (&flaginfo->includes,
5226 name, TRUE, copy);
5227 if (incl_entry == NULL)
5228 return FALSE;
5229 for (t = incl_entry->totals; t != NULL; t = t->next)
5230 if (t->total == val)
5231 break;
5232 if (t == NULL)
5233 {
5234 /* This is the first time we have seen this header
5235 file with this set of stabs strings. */
5236 t = (struct aout_link_includes_totals *)
5237 bfd_hash_allocate (&flaginfo->includes.root,
5238 sizeof *t);
5239 if (t == NULL)
5240 return FALSE;
5241 t->total = val;
5242 t->next = incl_entry->totals;
5243 incl_entry->totals = t;
5244 }
5245 else
5246 {
5247 int *incl_map;
5248
5249 /* This is a duplicate header file. We must change
5250 it to be an N_EXCL entry, and mark all the
5251 included symbols to prevent outputting them. */
5252 type = (int) N_EXCL;
5253
5254 nest = 0;
5255 for (incl_sym = sym + 1, incl_map = symbol_map + 1;
5256 incl_sym < sym_end;
5257 incl_sym++, incl_map++)
5258 {
5259 int incl_type;
5260
5261 incl_type = H_GET_8 (input_bfd, incl_sym->e_type);
5262 if (incl_type == (int) N_EINCL)
5263 {
5264 if (nest == 0)
5265 {
5266 *incl_map = -1;
5267 break;
5268 }
5269 --nest;
5270 }
5271 else if (incl_type == (int) N_BINCL)
5272 ++nest;
5273 else if (nest == 0)
5274 *incl_map = -1;
5275 }
5276 }
5277 }
5278 }
5279
5280 /* Copy this symbol into the list of symbols we are going to
5281 write out. */
5282 H_PUT_8 (output_bfd, type, outsym->e_type);
5283 H_PUT_8 (output_bfd, H_GET_8 (input_bfd, sym->e_other), outsym->e_other);
5284 H_PUT_16 (output_bfd, H_GET_16 (input_bfd, sym->e_desc), outsym->e_desc);
5285 copy = FALSE;
5286 if (! flaginfo->info->keep_memory)
5287 {
5288 /* name points into a string table which we are going to
5289 free. If there is a hash table entry, use that string.
5290 Otherwise, copy name into memory. */
5291 if (h != NULL)
5292 name = h->root.root.string;
5293 else
5294 copy = TRUE;
5295 }
5296 strtab_index = add_to_stringtab (output_bfd, flaginfo->strtab,
5297 name, copy);
5298 if (strtab_index == (bfd_size_type) -1)
5299 return FALSE;
5300 PUT_WORD (output_bfd, strtab_index, outsym->e_strx);
5301 PUT_WORD (output_bfd, val, outsym->e_value);
5302 *symbol_map = obj_aout_external_sym_count (output_bfd);
5303 ++obj_aout_external_sym_count (output_bfd);
5304 ++outsym;
5305 }
5306
5307 /* Write out the output symbols we have just constructed. */
5308 if (outsym > flaginfo->output_syms)
5309 {
5310 bfd_size_type outsym_size;
5311
5312 if (bfd_seek (output_bfd, flaginfo->symoff, SEEK_SET) != 0)
5313 return FALSE;
5314 outsym_size = outsym - flaginfo->output_syms;
5315 outsym_size *= EXTERNAL_NLIST_SIZE;
5316 if (bfd_bwrite ((void *) flaginfo->output_syms, outsym_size, output_bfd)
5317 != outsym_size)
5318 return FALSE;
5319 flaginfo->symoff += outsym_size;
5320 }
5321
5322 return TRUE;
5323 }
5324
5325 /* Link an a.out input BFD into the output file. */
5326
5327 static bfd_boolean
5328 aout_link_input_bfd (struct aout_final_link_info *flaginfo, bfd *input_bfd)
5329 {
5330 BFD_ASSERT (bfd_get_format (input_bfd) == bfd_object);
5331
5332 /* If this is a dynamic object, it may need special handling. */
5333 if ((input_bfd->flags & DYNAMIC) != 0
5334 && aout_backend_info (input_bfd)->link_dynamic_object != NULL)
5335 return ((*aout_backend_info (input_bfd)->link_dynamic_object)
5336 (flaginfo->info, input_bfd));
5337
5338 /* Get the symbols. We probably have them already, unless
5339 flaginfo->info->keep_memory is FALSE. */
5340 if (! aout_get_external_symbols (input_bfd))
5341 return FALSE;
5342
5343 /* Write out the symbols and get a map of the new indices. The map
5344 is placed into flaginfo->symbol_map. */
5345 if (! aout_link_write_symbols (flaginfo, input_bfd))
5346 return FALSE;
5347
5348 /* Relocate and write out the sections. These functions use the
5349 symbol map created by aout_link_write_symbols. The linker_mark
5350 field will be set if these sections are to be included in the
5351 link, which will normally be the case. */
5352 if (obj_textsec (input_bfd)->linker_mark)
5353 {
5354 if (! aout_link_input_section (flaginfo, input_bfd,
5355 obj_textsec (input_bfd),
5356 &flaginfo->treloff,
5357 exec_hdr (input_bfd)->a_trsize))
5358 return FALSE;
5359 }
5360 if (obj_datasec (input_bfd)->linker_mark)
5361 {
5362 if (! aout_link_input_section (flaginfo, input_bfd,
5363 obj_datasec (input_bfd),
5364 &flaginfo->dreloff,
5365 exec_hdr (input_bfd)->a_drsize))
5366 return FALSE;
5367 }
5368
5369 /* If we are not keeping memory, we don't need the symbols any
5370 longer. We still need them if we are keeping memory, because the
5371 strings in the hash table point into them. */
5372 if (! flaginfo->info->keep_memory)
5373 {
5374 if (! aout_link_free_symbols (input_bfd))
5375 return FALSE;
5376 }
5377
5378 return TRUE;
5379 }
5380
5381 /* Do the final link step. This is called on the output BFD. The
5382 INFO structure should point to a list of BFDs linked through the
5383 link.next field which can be used to find each BFD which takes part
5384 in the output. Also, each section in ABFD should point to a list
5385 of bfd_link_order structures which list all the input sections for
5386 the output section. */
5387
5388 bfd_boolean
5389 NAME (aout, final_link) (bfd *abfd,
5390 struct bfd_link_info *info,
5391 void (*callback) (bfd *, file_ptr *, file_ptr *, file_ptr *))
5392 {
5393 struct aout_final_link_info aout_info;
5394 bfd_boolean includes_hash_initialized = FALSE;
5395 bfd *sub;
5396 bfd_size_type trsize, drsize;
5397 bfd_size_type max_contents_size;
5398 bfd_size_type max_relocs_size;
5399 bfd_size_type max_sym_count;
5400 struct bfd_link_order *p;
5401 asection *o;
5402 bfd_boolean have_link_order_relocs;
5403
5404 if (bfd_link_pic (info))
5405 abfd->flags |= DYNAMIC;
5406
5407 aout_info.info = info;
5408 aout_info.output_bfd = abfd;
5409 aout_info.contents = NULL;
5410 aout_info.relocs = NULL;
5411 aout_info.symbol_map = NULL;
5412 aout_info.output_syms = NULL;
5413
5414 if (!bfd_hash_table_init_n (&aout_info.includes.root,
5415 aout_link_includes_newfunc,
5416 sizeof (struct aout_link_includes_entry),
5417 251))
5418 goto error_return;
5419 includes_hash_initialized = TRUE;
5420
5421 /* Figure out the largest section size. Also, if generating
5422 relocatable output, count the relocs. */
5423 trsize = 0;
5424 drsize = 0;
5425 max_contents_size = 0;
5426 max_relocs_size = 0;
5427 max_sym_count = 0;
5428 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
5429 {
5430 bfd_size_type sz;
5431
5432 if (bfd_link_relocatable (info))
5433 {
5434 if (bfd_get_flavour (sub) == bfd_target_aout_flavour)
5435 {
5436 trsize += exec_hdr (sub)->a_trsize;
5437 drsize += exec_hdr (sub)->a_drsize;
5438 }
5439 else
5440 {
5441 /* FIXME: We need to identify the .text and .data sections
5442 and call get_reloc_upper_bound and canonicalize_reloc to
5443 work out the number of relocs needed, and then multiply
5444 by the reloc size. */
5445 _bfd_error_handler
5446 /* xgettext:c-format */
5447 (_("%pB: relocatable link from %s to %s not supported"),
5448 abfd, sub->xvec->name, abfd->xvec->name);
5449 bfd_set_error (bfd_error_invalid_operation);
5450 goto error_return;
5451 }
5452 }
5453
5454 if (bfd_get_flavour (sub) == bfd_target_aout_flavour)
5455 {
5456 sz = obj_textsec (sub)->size;
5457 if (sz > max_contents_size)
5458 max_contents_size = sz;
5459 sz = obj_datasec (sub)->size;
5460 if (sz > max_contents_size)
5461 max_contents_size = sz;
5462
5463 sz = exec_hdr (sub)->a_trsize;
5464 if (sz > max_relocs_size)
5465 max_relocs_size = sz;
5466 sz = exec_hdr (sub)->a_drsize;
5467 if (sz > max_relocs_size)
5468 max_relocs_size = sz;
5469
5470 sz = obj_aout_external_sym_count (sub);
5471 if (sz > max_sym_count)
5472 max_sym_count = sz;
5473 }
5474 }
5475
5476 if (bfd_link_relocatable (info))
5477 {
5478 if (obj_textsec (abfd) != NULL)
5479 trsize += (_bfd_count_link_order_relocs (obj_textsec (abfd)
5480 ->map_head.link_order)
5481 * obj_reloc_entry_size (abfd));
5482 if (obj_datasec (abfd) != NULL)
5483 drsize += (_bfd_count_link_order_relocs (obj_datasec (abfd)
5484 ->map_head.link_order)
5485 * obj_reloc_entry_size (abfd));
5486 }
5487
5488 exec_hdr (abfd)->a_trsize = trsize;
5489 exec_hdr (abfd)->a_drsize = drsize;
5490
5491 exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd);
5492
5493 /* Adjust the section sizes and vmas according to the magic number.
5494 This sets a_text, a_data and a_bss in the exec_hdr and sets the
5495 filepos for each section. */
5496 if (! NAME (aout, adjust_sizes_and_vmas) (abfd))
5497 goto error_return;
5498
5499 /* The relocation and symbol file positions differ among a.out
5500 targets. We are passed a callback routine from the backend
5501 specific code to handle this.
5502 FIXME: At this point we do not know how much space the symbol
5503 table will require. This will not work for any (nonstandard)
5504 a.out target that needs to know the symbol table size before it
5505 can compute the relocation file positions. This may or may not
5506 be the case for the hp300hpux target, for example. */
5507 (*callback) (abfd, &aout_info.treloff, &aout_info.dreloff,
5508 &aout_info.symoff);
5509 obj_textsec (abfd)->rel_filepos = aout_info.treloff;
5510 obj_datasec (abfd)->rel_filepos = aout_info.dreloff;
5511 obj_sym_filepos (abfd) = aout_info.symoff;
5512
5513 /* We keep a count of the symbols as we output them. */
5514 obj_aout_external_sym_count (abfd) = 0;
5515
5516 /* We accumulate the string table as we write out the symbols. */
5517 aout_info.strtab = _bfd_stringtab_init ();
5518 if (aout_info.strtab == NULL)
5519 goto error_return;
5520
5521 /* Allocate buffers to hold section contents and relocs. */
5522 aout_info.contents = (bfd_byte *) bfd_malloc (max_contents_size);
5523 aout_info.relocs = bfd_malloc (max_relocs_size);
5524 aout_info.symbol_map = (int *) bfd_malloc (max_sym_count * sizeof (int));
5525 aout_info.output_syms = (struct external_nlist *)
5526 bfd_malloc ((max_sym_count + 1) * sizeof (struct external_nlist));
5527 if ((aout_info.contents == NULL && max_contents_size != 0)
5528 || (aout_info.relocs == NULL && max_relocs_size != 0)
5529 || (aout_info.symbol_map == NULL && max_sym_count != 0)
5530 || aout_info.output_syms == NULL)
5531 goto error_return;
5532
5533 /* If we have a symbol named __DYNAMIC, force it out now. This is
5534 required by SunOS. Doing this here rather than in sunos.c is a
5535 hack, but it's easier than exporting everything which would be
5536 needed. */
5537 {
5538 struct aout_link_hash_entry *h;
5539
5540 h = aout_link_hash_lookup (aout_hash_table (info), "__DYNAMIC",
5541 FALSE, FALSE, FALSE);
5542 if (h != NULL)
5543 aout_link_write_other_symbol (&h->root.root, &aout_info);
5544 }
5545
5546 /* The most time efficient way to do the link would be to read all
5547 the input object files into memory and then sort out the
5548 information into the output file. Unfortunately, that will
5549 probably use too much memory. Another method would be to step
5550 through everything that composes the text section and write it
5551 out, and then everything that composes the data section and write
5552 it out, and then write out the relocs, and then write out the
5553 symbols. Unfortunately, that requires reading stuff from each
5554 input file several times, and we will not be able to keep all the
5555 input files open simultaneously, and reopening them will be slow.
5556
5557 What we do is basically process one input file at a time. We do
5558 everything we need to do with an input file once--copy over the
5559 section contents, handle the relocation information, and write
5560 out the symbols--and then we throw away the information we read
5561 from it. This approach requires a lot of lseeks of the output
5562 file, which is unfortunate but still faster than reopening a lot
5563 of files.
5564
5565 We use the output_has_begun field of the input BFDs to see
5566 whether we have already handled it. */
5567 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
5568 sub->output_has_begun = FALSE;
5569
5570 /* Mark all sections which are to be included in the link. This
5571 will normally be every section. We need to do this so that we
5572 can identify any sections which the linker has decided to not
5573 include. */
5574 for (o = abfd->sections; o != NULL; o = o->next)
5575 {
5576 for (p = o->map_head.link_order; p != NULL; p = p->next)
5577 if (p->type == bfd_indirect_link_order)
5578 p->u.indirect.section->linker_mark = TRUE;
5579 }
5580
5581 have_link_order_relocs = FALSE;
5582 for (o = abfd->sections; o != NULL; o = o->next)
5583 {
5584 for (p = o->map_head.link_order;
5585 p != NULL;
5586 p = p->next)
5587 {
5588 if (p->type == bfd_indirect_link_order
5589 && (bfd_get_flavour (p->u.indirect.section->owner)
5590 == bfd_target_aout_flavour))
5591 {
5592 bfd *input_bfd;
5593
5594 input_bfd = p->u.indirect.section->owner;
5595 if (! input_bfd->output_has_begun)
5596 {
5597 if (! aout_link_input_bfd (&aout_info, input_bfd))
5598 goto error_return;
5599 input_bfd->output_has_begun = TRUE;
5600 }
5601 }
5602 else if (p->type == bfd_section_reloc_link_order
5603 || p->type == bfd_symbol_reloc_link_order)
5604 {
5605 /* These are handled below. */
5606 have_link_order_relocs = TRUE;
5607 }
5608 else
5609 {
5610 if (! _bfd_default_link_order (abfd, info, o, p))
5611 goto error_return;
5612 }
5613 }
5614 }
5615
5616 /* Write out any symbols that we have not already written out. */
5617 bfd_hash_traverse (&info->hash->table,
5618 aout_link_write_other_symbol,
5619 &aout_info);
5620
5621 /* Now handle any relocs we were asked to create by the linker.
5622 These did not come from any input file. We must do these after
5623 we have written out all the symbols, so that we know the symbol
5624 indices to use. */
5625 if (have_link_order_relocs)
5626 {
5627 for (o = abfd->sections; o != NULL; o = o->next)
5628 {
5629 for (p = o->map_head.link_order;
5630 p != NULL;
5631 p = p->next)
5632 {
5633 if (p->type == bfd_section_reloc_link_order
5634 || p->type == bfd_symbol_reloc_link_order)
5635 {
5636 if (! aout_link_reloc_link_order (&aout_info, o, p))
5637 goto error_return;
5638 }
5639 }
5640 }
5641 }
5642
5643 if (aout_info.contents != NULL)
5644 {
5645 free (aout_info.contents);
5646 aout_info.contents = NULL;
5647 }
5648 if (aout_info.relocs != NULL)
5649 {
5650 free (aout_info.relocs);
5651 aout_info.relocs = NULL;
5652 }
5653 if (aout_info.symbol_map != NULL)
5654 {
5655 free (aout_info.symbol_map);
5656 aout_info.symbol_map = NULL;
5657 }
5658 if (aout_info.output_syms != NULL)
5659 {
5660 free (aout_info.output_syms);
5661 aout_info.output_syms = NULL;
5662 }
5663 if (includes_hash_initialized)
5664 {
5665 bfd_hash_table_free (&aout_info.includes.root);
5666 includes_hash_initialized = FALSE;
5667 }
5668
5669 /* Finish up any dynamic linking we may be doing. */
5670 if (aout_backend_info (abfd)->finish_dynamic_link != NULL)
5671 {
5672 if (! (*aout_backend_info (abfd)->finish_dynamic_link) (abfd, info))
5673 goto error_return;
5674 }
5675
5676 /* Update the header information. */
5677 abfd->symcount = obj_aout_external_sym_count (abfd);
5678 exec_hdr (abfd)->a_syms = abfd->symcount * EXTERNAL_NLIST_SIZE;
5679 obj_str_filepos (abfd) = obj_sym_filepos (abfd) + exec_hdr (abfd)->a_syms;
5680 obj_textsec (abfd)->reloc_count =
5681 exec_hdr (abfd)->a_trsize / obj_reloc_entry_size (abfd);
5682 obj_datasec (abfd)->reloc_count =
5683 exec_hdr (abfd)->a_drsize / obj_reloc_entry_size (abfd);
5684
5685 /* Write out the string table, unless there are no symbols. */
5686 if (bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET) != 0)
5687 goto error_return;
5688 if (abfd->symcount > 0)
5689 {
5690 if (!emit_stringtab (abfd, aout_info.strtab))
5691 goto error_return;
5692 }
5693 else
5694 {
5695 bfd_byte b[BYTES_IN_WORD];
5696
5697 memset (b, 0, BYTES_IN_WORD);
5698 if (bfd_bwrite (b, (bfd_size_type) BYTES_IN_WORD, abfd) != BYTES_IN_WORD)
5699 goto error_return;
5700 }
5701
5702 return TRUE;
5703
5704 error_return:
5705 if (aout_info.contents != NULL)
5706 free (aout_info.contents);
5707 if (aout_info.relocs != NULL)
5708 free (aout_info.relocs);
5709 if (aout_info.symbol_map != NULL)
5710 free (aout_info.symbol_map);
5711 if (aout_info.output_syms != NULL)
5712 free (aout_info.output_syms);
5713 if (includes_hash_initialized)
5714 bfd_hash_table_free (&aout_info.includes.root);
5715 return FALSE;
5716 }
This page took 0.21015 seconds and 5 git commands to generate.