1 /* BFD semi-generic back-end for a.out binaries.
2 Copyright 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
3 Written by Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
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 2 of the License, or
10 (at your option) any later version.
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA. */
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
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, 386
37 and 29k a.out files, to create a target jump vector for a
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.
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.
51 As an example, this is what goes on to make the back end for a
52 sun4, from @file{aout32.c}:
54 | #define ARCH_SIZE 32
60 | aout_32_canonicalize_reloc
61 | aout_32_find_nearest_line
63 | aout_32_get_reloc_upper_bound
69 | #define TARGET_NAME "a.out-sunos-big"
70 | #define VECNAME sunos_big_vec
73 requires all the names from @file{aout32.c}, and produces the jump vector
77 The file @file{host-aout.c} is a special case. It is for a large set
78 of hosts that use ``more or less standard'' a.out files, and
79 for which cross-debugging is not interesting. It uses the
80 standard 32-bit a.out support routines, but determines the
81 file offsets and addresses of the text, data, and BSS
82 sections, the machine architecture and machine type, and the
83 entry point address, in a host-dependent manner. Once these
84 values have been determined, generic code is used to handle
87 When porting it to run on a new system, you must supply:
91 | HOST_MACHINE_ARCH (optional)
92 | HOST_MACHINE_MACHINE (optional)
93 | HOST_TEXT_START_ADDR
96 in the file @file{../include/sys/h-@var{XXX}.h} (for your host). These
97 values, plus the structures and macros defined in @file{a.out.h} on
98 your host system, will produce a BFD target that will access
99 ordinary a.out files on your host. To configure a new machine
100 to use @file{host-aout.c}, specify:
102 | TDEFAULTS = -DDEFAULT_VECTOR=host_aout_big_vec
103 | TDEPFILES= host-aout.o trad-core.o
105 in the @file{config/@var{XXX}.mt} file, and modify @file{configure.in}
107 @file{@var{XXX}.mt} file (by setting "<<bfd_target=XXX>>") when your
108 configuration is selected.
113 * Any BFD with D_PAGED set is ZMAGIC, and vice versa.
114 Doesn't matter what the setting of WP_TEXT is on output, but it'll
116 * Any BFD with D_PAGED clear and WP_TEXT set is NMAGIC.
117 * Any BFD with both flags clear is OMAGIC.
118 (Just want to make these explicit, so the conditions tested in this
119 file make sense if you're more familiar with a.out than with BFD.) */
122 #define KEEPITTYPE int
125 #include <string.h> /* For strchr and friends */
128 #include <ansidecl.h>
133 #include "aout/aout64.h"
134 #include "aout/stab_gnu.h"
137 static boolean translate_symbol_table PARAMS ((bfd *, aout_symbol_type *,
138 struct external_nlist *,
139 bfd_size_type, char *,
148 The file @file{aoutx.h} provides for both the @emph{standard}
149 and @emph{extended} forms of a.out relocation records.
151 The standard records contain only an
152 address, a symbol index, and a type field. The extended records
153 (used on 29ks and sparcs) also have a full integer for an
157 #define CTOR_TABLE_RELOC_IDX 2
159 #define howto_table_ext NAME(aout,ext_howto_table)
160 #define howto_table_std NAME(aout,std_howto_table)
162 reloc_howto_type howto_table_ext[] =
164 /* type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone */
165 HOWTO(RELOC_8, 0, 0, 8, false, 0, complain_overflow_bitfield,0,"8", false, 0,0x000000ff, false),
166 HOWTO(RELOC_16, 0, 1, 16, false, 0, complain_overflow_bitfield,0,"16", false, 0,0x0000ffff, false),
167 HOWTO(RELOC_32, 0, 2, 32, false, 0, complain_overflow_bitfield,0,"32", false, 0,0xffffffff, false),
168 HOWTO(RELOC_DISP8, 0, 0, 8, true, 0, complain_overflow_signed,0,"DISP8", false, 0,0x000000ff, false),
169 HOWTO(RELOC_DISP16, 0, 1, 16, true, 0, complain_overflow_signed,0,"DISP16", false, 0,0x0000ffff, false),
170 HOWTO(RELOC_DISP32, 0, 2, 32, true, 0, complain_overflow_signed,0,"DISP32", false, 0,0xffffffff, false),
171 HOWTO(RELOC_WDISP30,2, 2, 30, true, 0, complain_overflow_signed,0,"WDISP30", false, 0,0x3fffffff, false),
172 HOWTO(RELOC_WDISP22,2, 2, 22, true, 0, complain_overflow_signed,0,"WDISP22", false, 0,0x003fffff, false),
173 HOWTO(RELOC_HI22, 10, 2, 22, false, 0, complain_overflow_bitfield,0,"HI22", false, 0,0x003fffff, false),
174 HOWTO(RELOC_22, 0, 2, 22, false, 0, complain_overflow_bitfield,0,"22", false, 0,0x003fffff, false),
175 HOWTO(RELOC_13, 0, 2, 13, false, 0, complain_overflow_bitfield,0,"13", false, 0,0x00001fff, false),
176 HOWTO(RELOC_LO10, 0, 2, 10, false, 0, complain_overflow_dont,0,"LO10", false, 0,0x000003ff, false),
177 HOWTO(RELOC_SFA_BASE,0, 2, 32, false, 0, complain_overflow_bitfield,0,"SFA_BASE", false, 0,0xffffffff, false),
178 HOWTO(RELOC_SFA_OFF13,0,2, 32, false, 0, complain_overflow_bitfield,0,"SFA_OFF13",false, 0,0xffffffff, false),
179 HOWTO(RELOC_BASE10, 0, 2, 16, false, 0, complain_overflow_bitfield,0,"BASE10", false, 0,0x0000ffff, false),
180 HOWTO(RELOC_BASE13, 0, 2, 13, false, 0, complain_overflow_bitfield,0,"BASE13", false, 0,0x00001fff, false),
181 HOWTO(RELOC_BASE22, 0, 2, 0, false, 0, complain_overflow_bitfield,0,"BASE22", false, 0,0x00000000, false),
182 HOWTO(RELOC_PC10, 0, 2, 10, false, 0, complain_overflow_bitfield,0,"PC10", false, 0,0x000003ff, false),
183 HOWTO(RELOC_PC22, 0, 2, 22, false, 0, complain_overflow_bitfield,0,"PC22", false, 0,0x003fffff, false),
184 HOWTO(RELOC_JMP_TBL,0, 2, 32, false, 0, complain_overflow_bitfield,0,"JMP_TBL", false, 0,0xffffffff, false),
185 HOWTO(RELOC_SEGOFF16,0, 2, 0, false, 0, complain_overflow_bitfield,0,"SEGOFF16", false, 0,0x00000000, false),
186 HOWTO(RELOC_GLOB_DAT,0, 2, 0, false, 0, complain_overflow_bitfield,0,"GLOB_DAT", false, 0,0x00000000, false),
187 HOWTO(RELOC_JMP_SLOT,0, 2, 0, false, 0, complain_overflow_bitfield,0,"JMP_SLOT", false, 0,0x00000000, false),
188 HOWTO(RELOC_RELATIVE,0, 2, 0, false, 0, complain_overflow_bitfield,0,"RELATIVE", false, 0,0x00000000, false),
191 /* Convert standard reloc records to "arelent" format (incl byte swap). */
193 reloc_howto_type howto_table_std[] = {
194 /* type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone */
195 HOWTO( 0, 0, 0, 8, false, 0, complain_overflow_bitfield,0,"8", true, 0x000000ff,0x000000ff, false),
196 HOWTO( 1, 0, 1, 16, false, 0, complain_overflow_bitfield,0,"16", true, 0x0000ffff,0x0000ffff, false),
197 HOWTO( 2, 0, 2, 32, false, 0, complain_overflow_bitfield,0,"32", true, 0xffffffff,0xffffffff, false),
198 HOWTO( 3, 0, 4, 64, false, 0, complain_overflow_bitfield,0,"64", true, 0xdeaddead,0xdeaddead, false),
199 HOWTO( 4, 0, 0, 8, true, 0, complain_overflow_signed, 0,"DISP8", true, 0x000000ff,0x000000ff, false),
200 HOWTO( 5, 0, 1, 16, true, 0, complain_overflow_signed, 0,"DISP16", true, 0x0000ffff,0x0000ffff, false),
201 HOWTO( 6, 0, 2, 32, true, 0, complain_overflow_signed, 0,"DISP32", true, 0xffffffff,0xffffffff, false),
202 HOWTO( 7, 0, 4, 64, true, 0, complain_overflow_signed, 0,"DISP64", true, 0xfeedface,0xfeedface, false),
204 HOWTO( 9, 0, 1, 16, false, 0, complain_overflow_bitfield,0,"BASE16", false,0xffffffff,0xffffffff, false),
205 HOWTO(10, 0, 2, 32, false, 0, complain_overflow_bitfield,0,"BASE32", false,0xffffffff,0xffffffff, false),
208 #define TABLE_SIZE(TABLE) (sizeof(TABLE)/sizeof(TABLE[0]))
210 CONST struct reloc_howto_struct *
211 DEFUN(NAME(aout,reloc_type_lookup),(abfd,code),
213 bfd_reloc_code_real_type code)
215 #define EXT(i,j) case i: return &howto_table_ext[j]
216 #define STD(i,j) case i: return &howto_table_std[j]
217 int ext = obj_reloc_entry_size (abfd) == RELOC_EXT_SIZE;
218 if (code == BFD_RELOC_CTOR)
219 switch (bfd_get_arch_info (abfd)->bits_per_address)
228 EXT (BFD_RELOC_32, 2);
229 EXT (BFD_RELOC_HI22, 8);
230 EXT (BFD_RELOC_LO10, 11);
231 EXT (BFD_RELOC_32_PCREL_S2, 6);
232 EXT (BFD_RELOC_SPARC_WDISP22, 7);
233 default: return (CONST struct reloc_howto_struct *) 0;
239 STD (BFD_RELOC_16, 1);
240 STD (BFD_RELOC_32, 2);
241 STD (BFD_RELOC_8_PCREL, 4);
242 STD (BFD_RELOC_16_PCREL, 5);
243 STD (BFD_RELOC_32_PCREL, 6);
244 STD (BFD_RELOC_16_BASEREL, 9);
245 STD (BFD_RELOC_32_BASEREL, 10);
246 default: return (CONST struct reloc_howto_struct *) 0;
252 Internal entry points
255 @file{aoutx.h} exports several routines for accessing the
256 contents of an a.out file, which are gathered and exported in
257 turn by various format specific files (eg sunos.c).
263 aout_@var{size}_swap_exec_header_in
266 void aout_@var{size}_swap_exec_header_in,
268 struct external_exec *raw_bytes,
269 struct internal_exec *execp);
272 Swap the information in an executable header @var{raw_bytes} taken
273 from a raw byte stream memory image into the internal exec header
274 structure @var{execp}.
277 #ifndef NAME_swap_exec_header_in
279 DEFUN(NAME(aout,swap_exec_header_in),(abfd, raw_bytes, execp),
281 struct external_exec *raw_bytes AND
282 struct internal_exec *execp)
284 struct external_exec *bytes = (struct external_exec *)raw_bytes;
286 /* The internal_exec structure has some fields that are unused in this
287 configuration (IE for i960), so ensure that all such uninitialized
288 fields are zero'd out. There are places where two of these structs
289 are memcmp'd, and thus the contents do matter. */
290 memset (execp, 0, sizeof (struct internal_exec));
291 /* Now fill in fields in the execp, from the bytes in the raw data. */
292 execp->a_info = bfd_h_get_32 (abfd, bytes->e_info);
293 execp->a_text = GET_WORD (abfd, bytes->e_text);
294 execp->a_data = GET_WORD (abfd, bytes->e_data);
295 execp->a_bss = GET_WORD (abfd, bytes->e_bss);
296 execp->a_syms = GET_WORD (abfd, bytes->e_syms);
297 execp->a_entry = GET_WORD (abfd, bytes->e_entry);
298 execp->a_trsize = GET_WORD (abfd, bytes->e_trsize);
299 execp->a_drsize = GET_WORD (abfd, bytes->e_drsize);
301 #define NAME_swap_exec_header_in NAME(aout,swap_exec_header_in)
306 aout_@var{size}_swap_exec_header_out
309 void aout_@var{size}_swap_exec_header_out
311 struct internal_exec *execp,
312 struct external_exec *raw_bytes);
315 Swap the information in an internal exec header structure
316 @var{execp} into the buffer @var{raw_bytes} ready for writing to disk.
319 DEFUN(NAME(aout,swap_exec_header_out),(abfd, execp, raw_bytes),
321 struct internal_exec *execp AND
322 struct external_exec *raw_bytes)
324 struct external_exec *bytes = (struct external_exec *)raw_bytes;
326 /* Now fill in fields in the raw data, from the fields in the exec struct. */
327 bfd_h_put_32 (abfd, execp->a_info , bytes->e_info);
328 PUT_WORD (abfd, execp->a_text , bytes->e_text);
329 PUT_WORD (abfd, execp->a_data , bytes->e_data);
330 PUT_WORD (abfd, execp->a_bss , bytes->e_bss);
331 PUT_WORD (abfd, execp->a_syms , bytes->e_syms);
332 PUT_WORD (abfd, execp->a_entry , bytes->e_entry);
333 PUT_WORD (abfd, execp->a_trsize, bytes->e_trsize);
334 PUT_WORD (abfd, execp->a_drsize, bytes->e_drsize);
341 aout_@var{size}_some_aout_object_p
344 bfd_target *aout_@var{size}_some_aout_object_p
346 bfd_target *(*callback_to_real_object_p)());
349 Some a.out variant thinks that the file open in @var{abfd}
350 checking is an a.out file. Do some more checking, and set up
351 for access if it really is. Call back to the calling
352 environment's "finish up" function just before returning, to
353 handle any last-minute setup.
357 DEFUN(NAME(aout,some_aout_object_p),(abfd, execp, callback_to_real_object_p),
359 struct internal_exec *execp AND
360 bfd_target *(*callback_to_real_object_p) PARAMS ((bfd *)))
362 struct aout_data_struct *rawptr, *oldrawptr;
365 rawptr = (struct aout_data_struct *) bfd_zalloc (abfd, sizeof (struct aout_data_struct ));
366 if (rawptr == NULL) {
367 bfd_error = no_memory;
371 oldrawptr = abfd->tdata.aout_data;
372 abfd->tdata.aout_data = rawptr;
374 /* Copy the contents of the old tdata struct.
375 In particular, we want the subformat, since for hpux it was set in
376 hp300hpux.c:swap_exec_header_in and will be used in
377 hp300hpux.c:callback. */
378 if (oldrawptr != NULL)
379 *abfd->tdata.aout_data = *oldrawptr;
381 abfd->tdata.aout_data->a.hdr = &rawptr->e;
382 *(abfd->tdata.aout_data->a.hdr) = *execp; /* Copy in the internal_exec struct */
383 execp = abfd->tdata.aout_data->a.hdr;
385 /* Set the file flags */
386 abfd->flags = NO_FLAGS;
387 if (execp->a_drsize || execp->a_trsize)
388 abfd->flags |= HAS_RELOC;
389 /* Setting of EXEC_P has been deferred to the bottom of this function */
391 abfd->flags |= HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS;
392 if (N_DYNAMIC(*execp))
393 abfd->flags |= DYNAMIC;
395 if (N_MAGIC (*execp) == ZMAGIC)
397 abfd->flags |= D_PAGED|WP_TEXT;
398 adata(abfd).magic = z_magic;
400 else if (N_MAGIC (*execp) == NMAGIC)
402 abfd->flags |= WP_TEXT;
403 adata(abfd).magic = n_magic;
406 adata(abfd).magic = o_magic;
408 bfd_get_start_address (abfd) = execp->a_entry;
410 obj_aout_symbols (abfd) = (aout_symbol_type *)NULL;
411 bfd_get_symcount (abfd) = execp->a_syms / sizeof (struct external_nlist);
413 /* The default relocation entry size is that of traditional V7 Unix. */
414 obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
416 /* The default symbol entry size is that of traditional Unix. */
417 obj_symbol_entry_size (abfd) = EXTERNAL_NLIST_SIZE;
419 obj_aout_external_syms (abfd) = NULL;
420 obj_aout_external_strings (abfd) = NULL;
421 obj_aout_sym_hashes (abfd) = NULL;
423 /* Create the sections. This is raunchy, but bfd_close wants to reclaim
426 obj_textsec (abfd) = bfd_make_section_old_way (abfd, ".text");
427 obj_datasec (abfd) = bfd_make_section_old_way (abfd, ".data");
428 obj_bsssec (abfd) = bfd_make_section_old_way (abfd, ".bss");
431 (void)bfd_make_section (abfd, ".text");
432 (void)bfd_make_section (abfd, ".data");
433 (void)bfd_make_section (abfd, ".bss");
436 obj_datasec (abfd)->_raw_size = execp->a_data;
437 obj_bsssec (abfd)->_raw_size = execp->a_bss;
439 /* If this object is dynamically linked, we assume that both
440 sections have relocs. This does no real harm, even though it may
442 obj_textsec (abfd)->flags =
443 (execp->a_trsize != 0 || (abfd->flags & DYNAMIC) != 0
444 ? (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_RELOC)
445 : (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS));
446 obj_datasec (abfd)->flags =
447 (execp->a_drsize != 0 || (abfd->flags & DYNAMIC) != 0
448 ? (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS | SEC_RELOC)
449 : (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS));
450 obj_bsssec (abfd)->flags = SEC_ALLOC;
452 #ifdef THIS_IS_ONLY_DOCUMENTATION
453 /* The common code can't fill in these things because they depend
454 on either the start address of the text segment, the rounding
455 up of virtual addresses between segments, or the starting file
456 position of the text segment -- all of which varies among different
457 versions of a.out. */
459 /* Call back to the format-dependent code to fill in the rest of the
460 fields and do any further cleanup. Things that should be filled
461 in by the callback: */
463 struct exec *execp = exec_hdr (abfd);
465 obj_textsec (abfd)->size = N_TXTSIZE(*execp);
466 obj_textsec (abfd)->raw_size = N_TXTSIZE(*execp);
467 /* data and bss are already filled in since they're so standard */
469 /* The virtual memory addresses of the sections */
470 obj_textsec (abfd)->vma = N_TXTADDR(*execp);
471 obj_datasec (abfd)->vma = N_DATADDR(*execp);
472 obj_bsssec (abfd)->vma = N_BSSADDR(*execp);
474 /* The file offsets of the sections */
475 obj_textsec (abfd)->filepos = N_TXTOFF(*execp);
476 obj_datasec (abfd)->filepos = N_DATOFF(*execp);
478 /* The file offsets of the relocation info */
479 obj_textsec (abfd)->rel_filepos = N_TRELOFF(*execp);
480 obj_datasec (abfd)->rel_filepos = N_DRELOFF(*execp);
482 /* The file offsets of the string table and symbol table. */
483 obj_str_filepos (abfd) = N_STROFF (*execp);
484 obj_sym_filepos (abfd) = N_SYMOFF (*execp);
486 /* Determine the architecture and machine type of the object file. */
487 switch (N_MACHTYPE (*exec_hdr (abfd))) {
489 abfd->obj_arch = bfd_arch_obscure;
493 adata(abfd)->page_size = PAGE_SIZE;
494 adata(abfd)->segment_size = SEGMENT_SIZE;
495 adata(abfd)->exec_bytes_size = EXEC_BYTES_SIZE;
499 /* The architecture is encoded in various ways in various a.out variants,
500 or is not encoded at all in some of them. The relocation size depends
501 on the architecture and the a.out variant. Finally, the return value
502 is the bfd_target vector in use. If an error occurs, return zero and
503 set bfd_error to the appropriate error code.
505 Formats such as b.out, which have additional fields in the a.out
506 header, should cope with them in this callback as well. */
507 #endif /* DOCUMENTATION */
509 result = (*callback_to_real_object_p)(abfd);
511 /* Now that the segment addresses have been worked out, take a better
512 guess at whether the file is executable. If the entry point
513 is within the text segment, assume it is. (This makes files
514 executable even if their entry point address is 0, as long as
515 their text starts at zero.)
517 At some point we should probably break down and stat the file and
518 declare it executable if (one of) its 'x' bits are on... */
519 if ((execp->a_entry >= obj_textsec(abfd)->vma) &&
520 (execp->a_entry < obj_textsec(abfd)->vma + obj_textsec(abfd)->_raw_size))
521 abfd->flags |= EXEC_P;
524 #if 0 /* These should be set correctly anyways. */
525 abfd->sections = obj_textsec (abfd);
526 obj_textsec (abfd)->next = obj_datasec (abfd);
527 obj_datasec (abfd)->next = obj_bsssec (abfd);
533 abfd->tdata.aout_data = oldrawptr;
540 aout_@var{size}_mkobject
543 boolean aout_@var{size}_mkobject, (bfd *abfd);
546 Initialize BFD @var{abfd} for use with a.out files.
550 DEFUN(NAME(aout,mkobject),(abfd),
553 struct aout_data_struct *rawptr;
555 bfd_error = system_call_error;
557 /* Use an intermediate variable for clarity */
558 rawptr = (struct aout_data_struct *)bfd_zalloc (abfd, sizeof (struct aout_data_struct ));
560 if (rawptr == NULL) {
561 bfd_error = no_memory;
565 abfd->tdata.aout_data = rawptr;
566 exec_hdr (abfd) = &(rawptr->e);
568 /* For simplicity's sake we just make all the sections right here. */
570 obj_textsec (abfd) = (asection *)NULL;
571 obj_datasec (abfd) = (asection *)NULL;
572 obj_bsssec (abfd) = (asection *)NULL;
573 bfd_make_section (abfd, ".text");
574 bfd_make_section (abfd, ".data");
575 bfd_make_section (abfd, ".bss");
576 bfd_make_section (abfd, BFD_ABS_SECTION_NAME);
577 bfd_make_section (abfd, BFD_UND_SECTION_NAME);
578 bfd_make_section (abfd, BFD_COM_SECTION_NAME);
586 aout_@var{size}_machine_type
589 enum machine_type aout_@var{size}_machine_type
590 (enum bfd_architecture arch,
591 unsigned long machine));
594 Keep track of machine architecture and machine type for
595 a.out's. Return the <<machine_type>> for a particular
596 architecture and machine, or <<M_UNKNOWN>> if that exact architecture
597 and machine can't be represented in a.out format.
599 If the architecture is understood, machine type 0 (default)
600 is always understood.
604 DEFUN(NAME(aout,machine_type),(arch, machine),
605 enum bfd_architecture arch AND
606 unsigned long machine)
608 enum machine_type arch_flags;
610 arch_flags = M_UNKNOWN;
614 if (machine == 0) arch_flags = M_SPARC;
619 case 0: arch_flags = M_68010; break;
620 case 68000: arch_flags = M_UNKNOWN; break;
621 case 68010: arch_flags = M_68010; break;
622 case 68020: arch_flags = M_68020; break;
623 default: arch_flags = M_UNKNOWN; break;
628 if (machine == 0) arch_flags = M_386;
632 if (machine == 0) arch_flags = M_29K;
639 case 3000: arch_flags = M_MIPS1; break;
642 case 6000: arch_flags = M_MIPS2; break;
643 default: arch_flags = M_UNKNOWN; break;
648 arch_flags = M_UNKNOWN;
656 aout_@var{size}_set_arch_mach
659 boolean aout_@var{size}_set_arch_mach,
661 enum bfd_architecture arch,
662 unsigned long machine));
665 Set the architecture and the machine of the BFD @var{abfd} to the
666 values @var{arch} and @var{machine}. Verify that @var{abfd}'s format
667 can support the architecture required.
671 DEFUN(NAME(aout,set_arch_mach),(abfd, arch, machine),
673 enum bfd_architecture arch AND
674 unsigned long machine)
676 if (! bfd_default_set_arch_mach (abfd, arch, machine))
679 if (arch != bfd_arch_unknown &&
680 NAME(aout,machine_type) (arch, machine) == M_UNKNOWN)
681 return false; /* We can't represent this type */
683 /* Determine the size of a relocation entry */
688 obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE;
691 obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
695 return (*aout_backend_info(abfd)->set_sizes) (abfd);
699 adjust_o_magic (abfd, execp)
701 struct internal_exec *execp;
703 file_ptr pos = adata (abfd).exec_bytes_size;
708 obj_textsec(abfd)->filepos = pos;
709 pos += obj_textsec(abfd)->_raw_size;
710 vma += obj_textsec(abfd)->_raw_size;
713 if (!obj_datasec(abfd)->user_set_vma)
715 #if 0 /* ?? Does alignment in the file image really matter? */
716 pad = align_power (vma, obj_datasec(abfd)->alignment_power) - vma;
718 obj_textsec(abfd)->_raw_size += pad;
721 obj_datasec(abfd)->vma = vma;
723 obj_datasec(abfd)->filepos = pos;
724 pos += obj_datasec(abfd)->_raw_size;
725 vma += obj_datasec(abfd)->_raw_size;
728 if (!obj_bsssec(abfd)->user_set_vma)
731 pad = align_power (vma, obj_bsssec(abfd)->alignment_power) - vma;
733 obj_datasec(abfd)->_raw_size += pad;
736 obj_bsssec(abfd)->vma = vma;
738 obj_bsssec(abfd)->filepos = pos;
740 /* Fix up the exec header. */
741 execp->a_text = obj_textsec(abfd)->_raw_size;
742 execp->a_data = obj_datasec(abfd)->_raw_size;
743 execp->a_bss = obj_bsssec(abfd)->_raw_size;
744 N_SET_MAGIC (*execp, OMAGIC);
748 adjust_z_magic (abfd, execp)
750 struct internal_exec *execp;
752 bfd_size_type data_pad, text_pad;
754 CONST struct aout_backend_data *abdp;
755 int ztih; /* Nonzero if text includes exec header. */
757 abdp = aout_backend_info (abfd);
760 ztih = abdp && abdp->text_includes_header;
761 obj_textsec(abfd)->filepos = (ztih
762 ? adata(abfd).exec_bytes_size
763 : adata(abfd).page_size);
764 if (! obj_textsec(abfd)->user_set_vma)
765 /* ?? Do we really need to check for relocs here? */
766 obj_textsec(abfd)->vma = ((abfd->flags & HAS_RELOC)
769 ? (abdp->default_text_vma
770 + adata(abfd).exec_bytes_size)
771 : abdp->default_text_vma));
772 /* Could take strange alignment of text section into account here? */
774 /* Find start of data. */
775 text_end = obj_textsec(abfd)->filepos + obj_textsec(abfd)->_raw_size;
776 text_pad = BFD_ALIGN (text_end, adata(abfd).page_size) - text_end;
777 obj_textsec(abfd)->_raw_size += text_pad;
778 text_end += text_pad;
781 if (!obj_datasec(abfd)->user_set_vma)
784 vma = obj_textsec(abfd)->vma + obj_textsec(abfd)->_raw_size;
785 obj_datasec(abfd)->vma = BFD_ALIGN (vma, adata(abfd).segment_size);
787 if (abdp && abdp->zmagic_mapped_contiguous)
789 text_pad = (obj_datasec(abfd)->vma
790 - obj_textsec(abfd)->vma
791 - obj_textsec(abfd)->_raw_size);
792 obj_textsec(abfd)->_raw_size += text_pad;
794 obj_datasec(abfd)->filepos = (obj_textsec(abfd)->filepos
795 + obj_textsec(abfd)->_raw_size);
797 /* Fix up exec header while we're at it. */
798 execp->a_text = obj_textsec(abfd)->_raw_size;
799 if (ztih && (!abdp || (abdp && !abdp->exec_header_not_counted)))
800 execp->a_text += adata(abfd).exec_bytes_size;
801 N_SET_MAGIC (*execp, ZMAGIC);
803 /* Spec says data section should be rounded up to page boundary. */
804 obj_datasec(abfd)->_raw_size
805 = align_power (obj_datasec(abfd)->_raw_size,
806 obj_bsssec(abfd)->alignment_power);
807 execp->a_data = BFD_ALIGN (obj_datasec(abfd)->_raw_size,
808 adata(abfd).page_size);
809 data_pad = execp->a_data - obj_datasec(abfd)->_raw_size;
812 if (!obj_bsssec(abfd)->user_set_vma)
813 obj_bsssec(abfd)->vma = (obj_datasec(abfd)->vma
814 + obj_datasec(abfd)->_raw_size);
815 /* If the BSS immediately follows the data section and extra space
816 in the page is left after the data section, fudge data
817 in the header so that the bss section looks smaller by that
818 amount. We'll start the bss section there, and lie to the OS.
819 (Note that a linker script, as well as the above assignment,
820 could have explicitly set the BSS vma to immediately follow
821 the data section.) */
822 if (align_power (obj_bsssec(abfd)->vma, obj_bsssec(abfd)->alignment_power)
823 == obj_datasec(abfd)->vma + obj_datasec(abfd)->_raw_size)
824 execp->a_bss = (data_pad > obj_bsssec(abfd)->_raw_size) ? 0 :
825 obj_bsssec(abfd)->_raw_size - data_pad;
827 execp->a_bss = obj_bsssec(abfd)->_raw_size;
831 adjust_n_magic (abfd, execp)
833 struct internal_exec *execp;
835 file_ptr pos = adata(abfd).exec_bytes_size;
840 obj_textsec(abfd)->filepos = pos;
841 if (!obj_textsec(abfd)->user_set_vma)
842 obj_textsec(abfd)->vma = vma;
844 vma = obj_textsec(abfd)->vma;
845 pos += obj_textsec(abfd)->_raw_size;
846 vma += obj_textsec(abfd)->_raw_size;
849 obj_datasec(abfd)->filepos = pos;
850 if (!obj_datasec(abfd)->user_set_vma)
851 obj_datasec(abfd)->vma = BFD_ALIGN (vma, adata(abfd).segment_size);
852 vma = obj_datasec(abfd)->vma;
854 /* Since BSS follows data immediately, see if it needs alignment. */
855 vma += obj_datasec(abfd)->_raw_size;
856 pad = align_power (vma, obj_bsssec(abfd)->alignment_power) - vma;
857 obj_datasec(abfd)->_raw_size += pad;
858 pos += obj_datasec(abfd)->_raw_size;
861 if (!obj_bsssec(abfd)->user_set_vma)
862 obj_bsssec(abfd)->vma = vma;
864 vma = obj_bsssec(abfd)->vma;
866 /* Fix up exec header. */
867 execp->a_text = obj_textsec(abfd)->_raw_size;
868 execp->a_data = obj_datasec(abfd)->_raw_size;
869 execp->a_bss = obj_bsssec(abfd)->_raw_size;
870 N_SET_MAGIC (*execp, NMAGIC);
874 DEFUN (NAME(aout,adjust_sizes_and_vmas), (abfd, text_size, text_end),
875 bfd *abfd AND bfd_size_type *text_size AND file_ptr *text_end)
877 struct internal_exec *execp = exec_hdr (abfd);
879 if ((obj_textsec (abfd) == NULL) || (obj_datasec (abfd) == NULL))
881 bfd_error = invalid_operation;
884 if (adata(abfd).magic != undecided_magic) return true;
886 obj_textsec(abfd)->_raw_size =
887 align_power(obj_textsec(abfd)->_raw_size,
888 obj_textsec(abfd)->alignment_power);
890 *text_size = obj_textsec (abfd)->_raw_size;
891 /* Rule (heuristic) for when to pad to a new page. Note that there
892 are (at least) two ways demand-paged (ZMAGIC) files have been
893 handled. Most Berkeley-based systems start the text segment at
894 (PAGE_SIZE). However, newer versions of SUNOS start the text
895 segment right after the exec header; the latter is counted in the
896 text segment size, and is paged in by the kernel with the rest of
899 /* This perhaps isn't the right way to do this, but made it simpler for me
900 to understand enough to implement it. Better would probably be to go
901 right from BFD flags to alignment/positioning characteristics. But the
902 old code was sloppy enough about handling the flags, and had enough
903 other magic, that it was a little hard for me to understand. I think
904 I understand it better now, but I haven't time to do the cleanup this
907 if (abfd->flags & D_PAGED)
908 /* Whether or not WP_TEXT is set -- let D_PAGED override. */
909 /* @@ What about QMAGIC? */
910 adata(abfd).magic = z_magic;
911 else if (abfd->flags & WP_TEXT)
912 adata(abfd).magic = n_magic;
914 adata(abfd).magic = o_magic;
916 #ifdef BFD_AOUT_DEBUG /* requires gcc2 */
918 fprintf (stderr, "%s text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x,%x>\n",
920 switch (adata(abfd).magic) {
921 case n_magic: str = "NMAGIC"; break;
922 case o_magic: str = "OMAGIC"; break;
923 case z_magic: str = "ZMAGIC"; break;
928 obj_textsec(abfd)->vma, obj_textsec(abfd)->_raw_size,
929 obj_textsec(abfd)->alignment_power,
930 obj_datasec(abfd)->vma, obj_datasec(abfd)->_raw_size,
931 obj_datasec(abfd)->alignment_power,
932 obj_bsssec(abfd)->vma, obj_bsssec(abfd)->_raw_size,
933 obj_bsssec(abfd)->alignment_power);
937 switch (adata(abfd).magic)
940 adjust_o_magic (abfd, execp);
943 adjust_z_magic (abfd, execp);
946 adjust_n_magic (abfd, execp);
952 #ifdef BFD_AOUT_DEBUG
953 fprintf (stderr, " text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x>\n",
954 obj_textsec(abfd)->vma, obj_textsec(abfd)->_raw_size,
955 obj_textsec(abfd)->filepos,
956 obj_datasec(abfd)->vma, obj_datasec(abfd)->_raw_size,
957 obj_datasec(abfd)->filepos,
958 obj_bsssec(abfd)->vma, obj_bsssec(abfd)->_raw_size);
966 aout_@var{size}_new_section_hook
969 boolean aout_@var{size}_new_section_hook,
974 Called by the BFD in response to a @code{bfd_make_section}
978 DEFUN(NAME(aout,new_section_hook),(abfd, newsect),
982 /* align to double at least */
983 newsect->alignment_power = bfd_get_arch_info(abfd)->section_align_power;
986 if (bfd_get_format (abfd) == bfd_object)
988 if (obj_textsec(abfd) == NULL && !strcmp(newsect->name, ".text")) {
989 obj_textsec(abfd)= newsect;
990 newsect->target_index = N_TEXT | N_EXT;
994 if (obj_datasec(abfd) == NULL && !strcmp(newsect->name, ".data")) {
995 obj_datasec(abfd) = newsect;
996 newsect->target_index = N_DATA | N_EXT;
1000 if (obj_bsssec(abfd) == NULL && !strcmp(newsect->name, ".bss")) {
1001 obj_bsssec(abfd) = newsect;
1002 newsect->target_index = N_BSS | N_EXT;
1008 /* We allow more than three sections internally */
1013 DEFUN(NAME(aout,set_section_contents),(abfd, section, location, offset, count),
1018 bfd_size_type count)
1021 bfd_size_type text_size;
1023 if (abfd->output_has_begun == false)
1025 if (NAME(aout,adjust_sizes_and_vmas) (abfd,
1027 &text_end) == false)
1031 /* regardless, once we know what we're doing, we might as well get going */
1032 if (section != obj_bsssec(abfd))
1034 bfd_seek (abfd, section->filepos + offset, SEEK_SET);
1037 return (bfd_write ((PTR)location, 1, count, abfd) == count) ?
1045 /* Classify stabs symbols */
1047 #define sym_in_text_section(sym) \
1048 (((sym)->type & (N_ABS | N_TEXT | N_DATA | N_BSS))== N_TEXT)
1050 #define sym_in_data_section(sym) \
1051 (((sym)->type & (N_ABS | N_TEXT | N_DATA | N_BSS))== N_DATA)
1053 #define sym_in_bss_section(sym) \
1054 (((sym)->type & (N_ABS | N_TEXT | N_DATA | N_BSS))== N_BSS)
1056 /* Symbol is undefined if type is N_UNDF|N_EXT and if it has
1057 zero in the "value" field. Nonzeroes there are fortrancommon
1059 #define sym_is_undefined(sym) \
1060 ((sym)->type == (N_UNDF | N_EXT) && (sym)->symbol.value == 0)
1062 /* Symbol is a global definition if N_EXT is on and if it has
1063 a nonzero type field. */
1064 #define sym_is_global_defn(sym) \
1065 (((sym)->type & N_EXT) && (sym)->type & N_TYPE)
1067 /* Symbol is debugger info if any bits outside N_TYPE or N_EXT
1069 #define sym_is_debugger_info(sym) \
1070 (((sym)->type & ~(N_EXT | N_TYPE)) || (sym)->type == N_FN)
1072 #define sym_is_fortrancommon(sym) \
1073 (((sym)->type == (N_EXT)) && (sym)->symbol.value != 0)
1075 /* Symbol is absolute if it has N_ABS set */
1076 #define sym_is_absolute(sym) \
1077 (((sym)->type & N_TYPE)== N_ABS)
1080 #define sym_is_indirect(sym) \
1081 (((sym)->type & N_ABS)== N_ABS)
1083 /* Only in their own functions for ease of debugging; when sym flags have
1084 stabilised these should be inlined into their (single) caller */
1087 DEFUN (translate_from_native_sym_flags, (sym_pointer, cache_ptr, abfd),
1088 struct external_nlist *sym_pointer AND
1089 aout_symbol_type * cache_ptr AND
1092 cache_ptr->symbol.section = 0;
1093 switch (cache_ptr->type & N_TYPE)
1095 case N_SETA: case N_SETA | N_EXT:
1096 case N_SETT: case N_SETT | N_EXT:
1097 case N_SETD: case N_SETD | N_EXT:
1098 case N_SETB: case N_SETB | N_EXT:
1100 char *copy = bfd_alloc (abfd, strlen (cache_ptr->symbol.name) + 1);
1102 asection *into_section;
1103 arelent_chain *reloc = (arelent_chain *) bfd_alloc (abfd, sizeof (arelent_chain));
1105 if (!copy || !reloc)
1107 bfd_error = no_memory;
1111 strcpy (copy, cache_ptr->symbol.name);
1113 /* Make sure that this bfd has a section with the right contructor
1115 section = bfd_get_section_by_name (abfd, copy);
1117 section = bfd_make_section (abfd, copy);
1119 /* Build a relocation entry for the constructor */
1120 switch ((cache_ptr->type & N_TYPE))
1122 case N_SETA: case N_SETA | N_EXT:
1123 into_section = &bfd_abs_section;
1124 cache_ptr->type = N_ABS;
1126 case N_SETT: case N_SETT | N_EXT:
1127 into_section = (asection *) obj_textsec (abfd);
1128 cache_ptr->type = N_TEXT;
1130 case N_SETD: case N_SETD | N_EXT:
1131 into_section = (asection *) obj_datasec (abfd);
1132 cache_ptr->type = N_DATA;
1134 case N_SETB: case N_SETB | N_EXT:
1135 into_section = (asection *) obj_bsssec (abfd);
1136 cache_ptr->type = N_BSS;
1139 bfd_error = bad_value;
1143 /* Build a relocation pointing into the constuctor section
1144 pointing at the symbol in the set vector specified */
1146 reloc->relent.addend = cache_ptr->symbol.value;
1147 cache_ptr->symbol.section = into_section->symbol->section;
1148 reloc->relent.sym_ptr_ptr = into_section->symbol_ptr_ptr;
1151 /* We modify the symbol to belong to a section depending upon the
1152 name of the symbol - probably __CTOR__ or __DTOR__ but we don't
1153 really care, and add to the size of the section to contain a
1154 pointer to the symbol. Build a reloc entry to relocate to this
1155 symbol attached to this section. */
1157 section->flags = SEC_CONSTRUCTOR;
1160 section->reloc_count++;
1161 section->alignment_power = 2;
1163 reloc->next = section->constructor_chain;
1164 section->constructor_chain = reloc;
1165 reloc->relent.address = section->_raw_size;
1166 section->_raw_size += sizeof (int *);
1169 = (obj_reloc_entry_size(abfd) == RELOC_EXT_SIZE
1170 ? howto_table_ext : howto_table_std)
1171 + CTOR_TABLE_RELOC_IDX;
1172 cache_ptr->symbol.flags |= BSF_CONSTRUCTOR;
1176 if (cache_ptr->type == N_WARNING)
1178 /* This symbol is the text of a warning message, the next symbol
1179 is the symbol to associate the warning with */
1180 cache_ptr->symbol.flags = BSF_DEBUGGING | BSF_WARNING;
1182 /* @@ Stuffing pointers into integers is a no-no.
1183 We can usually get away with it if the integer is
1184 large enough though. */
1185 if (sizeof (cache_ptr + 1) > sizeof (bfd_vma))
1187 cache_ptr->symbol.value = (bfd_vma) ((cache_ptr + 1));
1189 /* We don't use a warning symbol's section, but we need
1190 it to be nonzero for the sanity check below, so
1191 pick one arbitrarily. */
1192 cache_ptr->symbol.section = &bfd_abs_section;
1194 /* We furgle with the next symbol in place.
1195 We don't want it to be undefined, we'll trample the type */
1196 (sym_pointer + 1)->e_type[0] = 0xff;
1199 if ((cache_ptr->type | N_EXT) == (N_INDR | N_EXT))
1201 /* Two symbols in a row for an INDR message. The first symbol
1202 contains the name we will match, the second symbol contains
1203 the name the first name is translated into. It is supplied to
1204 us undefined. This is good, since we want to pull in any files
1206 cache_ptr->symbol.flags = BSF_DEBUGGING | BSF_INDIRECT;
1208 /* @@ Stuffing pointers into integers is a no-no.
1209 We can usually get away with it if the integer is
1210 large enough though. */
1211 if (sizeof (cache_ptr + 1) > sizeof (bfd_vma))
1214 cache_ptr->symbol.value = (bfd_vma) ((cache_ptr + 1));
1215 cache_ptr->symbol.section = &bfd_ind_section;
1218 else if (sym_is_debugger_info (cache_ptr))
1220 cache_ptr->symbol.flags = BSF_DEBUGGING;
1221 /* Work out the section correct for this symbol */
1222 switch (cache_ptr->type & N_TYPE)
1226 cache_ptr->symbol.section = obj_textsec (abfd);
1227 cache_ptr->symbol.value -= obj_textsec (abfd)->vma;
1230 cache_ptr->symbol.value -= obj_datasec (abfd)->vma;
1231 cache_ptr->symbol.section = obj_datasec (abfd);
1234 cache_ptr->symbol.section = obj_bsssec (abfd);
1235 cache_ptr->symbol.value -= obj_bsssec (abfd)->vma;
1239 cache_ptr->symbol.section = &bfd_abs_section;
1246 if (sym_is_fortrancommon (cache_ptr))
1248 cache_ptr->symbol.flags = 0;
1249 cache_ptr->symbol.section = &bfd_com_section;
1257 /* In a.out, the value of a symbol is always relative to the
1258 * start of the file, if this is a data symbol we'll subtract
1259 * the size of the text section to get the section relative
1260 * value. If this is a bss symbol (which would be strange)
1261 * we'll subtract the size of the previous two sections
1262 * to find the section relative address.
1265 if (sym_in_text_section (cache_ptr))
1267 cache_ptr->symbol.value -= obj_textsec (abfd)->vma;
1268 cache_ptr->symbol.section = obj_textsec (abfd);
1270 else if (sym_in_data_section (cache_ptr))
1272 cache_ptr->symbol.value -= obj_datasec (abfd)->vma;
1273 cache_ptr->symbol.section = obj_datasec (abfd);
1275 else if (sym_in_bss_section (cache_ptr))
1277 cache_ptr->symbol.section = obj_bsssec (abfd);
1278 cache_ptr->symbol.value -= obj_bsssec (abfd)->vma;
1280 else if (sym_is_undefined (cache_ptr))
1282 cache_ptr->symbol.flags = 0;
1283 cache_ptr->symbol.section = &bfd_und_section;
1285 else if (sym_is_absolute (cache_ptr))
1287 cache_ptr->symbol.section = &bfd_abs_section;
1290 if (sym_is_global_defn (cache_ptr))
1292 cache_ptr->symbol.flags = BSF_GLOBAL | BSF_EXPORT;
1294 else if (! sym_is_undefined (cache_ptr))
1296 cache_ptr->symbol.flags = BSF_LOCAL;
1300 if (cache_ptr->symbol.section == 0)
1307 DEFUN(translate_to_native_sym_flags,(sym_pointer, cache_ptr, abfd),
1308 struct external_nlist *sym_pointer AND
1309 asymbol *cache_ptr AND
1312 bfd_vma value = cache_ptr->value;
1314 /* mask out any existing type bits in case copying from one section
1316 sym_pointer->e_type[0] &= ~N_TYPE;
1318 /* We attempt to order these tests by decreasing frequency of success,
1319 according to tcov when linking the linker. */
1320 if (bfd_get_output_section(cache_ptr) == &bfd_abs_section) {
1321 sym_pointer->e_type[0] |= N_ABS;
1323 else if (bfd_get_output_section(cache_ptr) == obj_textsec (abfd)) {
1324 sym_pointer->e_type[0] |= N_TEXT;
1326 else if (bfd_get_output_section(cache_ptr) == obj_datasec (abfd)) {
1327 sym_pointer->e_type[0] |= N_DATA;
1329 else if (bfd_get_output_section(cache_ptr) == obj_bsssec (abfd)) {
1330 sym_pointer->e_type[0] |= N_BSS;
1332 else if (bfd_get_output_section(cache_ptr) == &bfd_und_section) {
1333 sym_pointer->e_type[0] = (N_UNDF | N_EXT);
1335 else if (bfd_get_output_section(cache_ptr) == &bfd_ind_section) {
1336 sym_pointer->e_type[0] = N_INDR;
1338 else if (bfd_get_output_section(cache_ptr) == NULL) {
1339 /* Protect the bfd_is_com_section call.
1340 This case occurs, e.g., for the *DEBUG* section of a COFF file. */
1341 bfd_error = nonrepresentable_section;
1344 else if (bfd_is_com_section (bfd_get_output_section (cache_ptr))) {
1345 sym_pointer->e_type[0] = (N_UNDF | N_EXT);
1348 bfd_error = nonrepresentable_section;
1352 /* Turn the symbol from section relative to absolute again */
1354 value += cache_ptr->section->output_section->vma + cache_ptr->section->output_offset ;
1357 if (cache_ptr->flags & (BSF_WARNING)) {
1358 sym_pointer->e_type[0] = N_WARNING;
1359 (sym_pointer+1)->e_type[0] = 1;
1362 if (cache_ptr->flags & BSF_DEBUGGING) {
1363 sym_pointer->e_type[0] = ((aout_symbol_type *)cache_ptr)->type;
1365 else if (cache_ptr->flags & (BSF_GLOBAL | BSF_EXPORT)) {
1366 sym_pointer->e_type[0] |= N_EXT;
1368 if (cache_ptr->flags & BSF_CONSTRUCTOR) {
1369 int type = ((aout_symbol_type *)cache_ptr)->type;
1372 case N_ABS: type = N_SETA; break;
1373 case N_TEXT: type = N_SETT; break;
1374 case N_DATA: type = N_SETD; break;
1375 case N_BSS: type = N_SETB; break;
1377 sym_pointer->e_type[0] = type;
1380 PUT_WORD(abfd, value, sym_pointer->e_value);
1385 /* Native-level interface to symbols. */
1389 DEFUN(NAME(aout,make_empty_symbol),(abfd),
1392 aout_symbol_type *new =
1393 (aout_symbol_type *)bfd_zalloc (abfd, sizeof (aout_symbol_type));
1396 bfd_error = no_memory;
1399 new->symbol.the_bfd = abfd;
1401 return &new->symbol;
1404 /* Translate a set of internal symbols into external symbols. */
1407 translate_symbol_table (abfd, in, ext, count, str, strsize, dynamic)
1409 aout_symbol_type *in;
1410 struct external_nlist *ext;
1411 bfd_size_type count;
1413 bfd_size_type strsize;
1416 struct external_nlist *ext_end;
1418 ext_end = ext + count;
1419 for (; ext < ext_end; ext++, in++)
1423 x = GET_WORD (abfd, ext->e_strx);
1424 in->symbol.the_bfd = abfd;
1426 /* For the normal symbols, the zero index points at the number
1427 of bytes in the string table but is to be interpreted as the
1428 null string. For the dynamic symbols, the number of bytes in
1429 the string table is stored in the __DYNAMIC structure and the
1430 zero index points at an actual string. */
1431 if (x == 0 && ! dynamic)
1432 in->symbol.name = "";
1433 else if (x < strsize)
1434 in->symbol.name = str + x;
1438 in->symbol.value = GET_SWORD (abfd, ext->e_value);
1439 in->desc = bfd_h_get_16 (abfd, ext->e_desc);
1440 in->other = bfd_h_get_8 (abfd, ext->e_other);
1441 in->type = bfd_h_get_8 (abfd, ext->e_type);
1442 in->symbol.udata = 0;
1444 if (!translate_from_native_sym_flags (ext, in, abfd))
1448 in->symbol.flags |= BSF_DYNAMIC;
1454 /* We read the symbols into a buffer, which is discarded when this
1455 function exits. We read the strings into a buffer large enough to
1456 hold them all plus all the cached symbol entries. */
1459 DEFUN(NAME(aout,slurp_symbol_table),(abfd),
1462 bfd_size_type symbol_size;
1463 bfd_size_type string_size;
1464 unsigned char string_chars[BYTES_IN_WORD];
1465 struct external_nlist *syms;
1467 aout_symbol_type *cached;
1468 bfd_size_type dynsym_count = 0;
1469 struct external_nlist *dynsyms = NULL;
1470 char *dynstrs = NULL;
1471 bfd_size_type dynstr_size;
1473 /* If there's no work to be done, don't do any */
1474 if (obj_aout_symbols (abfd) != (aout_symbol_type *)NULL) return true;
1475 symbol_size = exec_hdr(abfd)->a_syms;
1476 if (symbol_size == 0)
1478 bfd_error = no_symbols;
1482 bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET);
1483 if (bfd_read ((PTR)string_chars, BYTES_IN_WORD, 1, abfd) != BYTES_IN_WORD)
1485 string_size = GET_WORD (abfd, string_chars);
1487 /* If this is a dynamic object, see if we can get the dynamic symbol
1489 if ((bfd_get_file_flags (abfd) & DYNAMIC) != 0
1490 && aout_backend_info (abfd)->read_dynamic_symbols)
1492 dynsym_count = ((*aout_backend_info (abfd)->read_dynamic_symbols)
1493 (abfd, &dynsyms, &dynstrs, &dynstr_size));
1494 if (dynsym_count == (bfd_size_type) -1)
1498 strings = (char *) bfd_alloc (abfd, string_size + 1);
1499 cached = ((aout_symbol_type *)
1501 ((bfd_get_symcount (abfd) + dynsym_count)
1502 * sizeof (aout_symbol_type))));
1504 /* Don't allocate on the obstack, so we can free it easily. */
1505 syms = (struct external_nlist *) malloc(symbol_size);
1506 if (!strings || !cached || !syms)
1508 bfd_error = no_memory;
1511 bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET);
1512 if (bfd_read ((PTR)syms, 1, symbol_size, abfd) != symbol_size)
1518 bfd_release (abfd, cached);
1520 bfd_release (abfd, strings);
1524 bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET);
1525 if (bfd_read ((PTR)strings, 1, string_size, abfd) != string_size)
1529 strings[string_size] = 0; /* Just in case. */
1531 /* OK, now walk the new symtable, cacheing symbol properties */
1532 if (! translate_symbol_table (abfd, cached, syms, bfd_get_symcount (abfd),
1533 strings, string_size, false))
1535 if (dynsym_count > 0)
1537 if (! translate_symbol_table (abfd, cached + bfd_get_symcount (abfd),
1538 dynsyms, dynsym_count, dynstrs,
1542 bfd_get_symcount (abfd) += dynsym_count;
1545 obj_aout_symbols (abfd) = cached;
1552 /* Possible improvements:
1553 + look for strings matching trailing substrings of other strings
1554 + better data structures? balanced trees?
1555 + smaller per-string or per-symbol data? re-use some of the symbol's
1557 + also look at reducing memory use elsewhere -- maybe if we didn't have to
1558 construct the entire symbol table at once, we could get by with smaller
1559 amounts of VM? (What effect does that have on the string table
1561 + rip this out of here, put it into its own file in bfd or libiberty, so
1562 coff and elf can use it too. I'll work on this soon, but have more
1563 pressing tasks right now.
1565 A hash table might(?) be more efficient for handling exactly the cases that
1566 are handled now, but for trailing substring matches, I think we want to
1567 examine the `nearest' values (reverse-)lexically, not merely impose a strict
1568 order, nor look only for exact-match or not-match. I don't think a hash
1569 table would be very useful for that, and I don't feel like fleshing out two
1570 completely different implementations. [raeburn:930419.0331EDT] */
1572 struct stringtab_entry {
1573 /* Hash value for this string. Only useful so long as we aren't doing
1574 substring matches. */
1577 /* Next node to look at, depending on whether the hash value of the string
1578 being searched for is less than or greater than the hash value of the
1579 current node. For now, `equal to' is lumped in with `greater than', for
1580 space efficiency. It's not a common enough case to warrant another field
1581 to be used for all nodes. */
1582 struct stringtab_entry *less;
1583 struct stringtab_entry *greater;
1585 /* The string itself. */
1588 /* The index allocated for this string. */
1589 bfd_size_type index;
1591 #ifdef GATHER_STATISTICS
1592 /* How many references have there been to this string? (Not currently used;
1593 could be dumped out for anaylsis, if anyone's interested.) */
1594 unsigned long count;
1597 /* Next node in linked list, in suggested output order. */
1598 struct stringtab_entry *next_to_output;
1601 struct stringtab_data {
1602 /* Tree of string table entries. */
1603 struct stringtab_entry *strings;
1605 /* Fudge factor used to center top node of tree. */
1608 /* Next index value to issue. */
1609 bfd_size_type index;
1611 /* Index used for empty strings. Cached here because checking for them
1612 is really easy, and we can avoid searching the tree. */
1613 bfd_size_type empty_string_index;
1615 /* These fields indicate the two ends of a singly-linked list that indicates
1616 the order strings should be written out in. Use this order, and no
1617 seeking will need to be done, so output efficiency should be maximized. */
1618 struct stringtab_entry **end;
1619 struct stringtab_entry *output_order;
1621 #ifdef GATHER_STATISTICS
1622 /* Number of strings which duplicate strings already in the table. */
1623 unsigned long duplicates;
1625 /* Number of bytes saved by not having to write all the duplicate strings. */
1626 unsigned long bytes_saved;
1628 /* Number of zero-length strings. Currently, these all turn into
1629 references to the null byte at the end of the first string. In some
1630 cases (possibly not all? explore this...), it should be possible to
1631 simply write out a zero index value. */
1632 unsigned long empty_strings;
1634 /* Number of times the hash values matched but the strings were different.
1635 Note that this includes the number of times the other string(s) occurs, so
1636 there may only be two strings hashing to the same value, even if this
1637 number is very large. */
1638 unsigned long bad_hash_matches;
1640 /* Null strings aren't counted in this one.
1641 This will probably only be nonzero if we've got an input file
1642 which was produced by `ld -r' (i.e., it's already been processed
1643 through this code). Under some operating systems, native tools
1644 may make all empty strings have the same index; but the pointer
1645 check won't catch those, because to get to that stage we'd already
1646 have to compute the checksum, which requires reading the string,
1647 so we short-circuit that case with empty_string_index above. */
1648 unsigned long pointer_matches;
1650 /* Number of comparisons done. I figure with the algorithms in use below,
1651 the average number of comparisons done (per symbol) should be roughly
1652 log-base-2 of the number of unique strings. */
1653 unsigned long n_compares;
1657 /* Some utility functions for the string table code. */
1659 /* For speed, only hash on the first this many bytes of strings.
1660 This number was chosen by profiling ld linking itself, with -g. */
1661 #define HASHMAXLEN 25
1663 #define HASH_CHAR(c) (sum ^= sum >> 20, sum ^= sum << 7, sum += (c))
1665 static INLINE unsigned int
1667 unsigned char *string;
1668 register unsigned int len;
1670 register unsigned int sum = 0;
1672 if (len > HASHMAXLEN)
1680 HASH_CHAR (*string++);
1686 stringtab_init (tab)
1687 struct stringtab_data *tab;
1690 tab->output_order = 0;
1692 tab->end = &tab->output_order;
1694 /* Initial string table length includes size of length field. */
1695 tab->index = BYTES_IN_WORD;
1696 tab->empty_string_index = -1;
1697 #ifdef GATHER_STATISTICS
1698 tab->duplicates = 0;
1699 tab->empty_strings = 0;
1700 tab->bad_hash_matches = 0;
1701 tab->pointer_matches = 0;
1702 tab->bytes_saved = 0;
1703 tab->n_compares = 0;
1708 compare (entry, str, hash)
1709 struct stringtab_entry *entry;
1713 return hash - entry->hash;
1716 #ifdef GATHER_STATISTICS
1717 /* Don't want to have to link in math library with all bfd applications... */
1718 static INLINE double
1726 return ((d > 1.41) ? 0.5 : 0) + n;
1730 /* Main string table routines. */
1731 /* Returns index in string table. Whether or not this actually adds an
1732 entry into the string table should be irrelevant -- it just has to
1733 return a valid index. */
1734 static bfd_size_type
1735 add_to_stringtab (abfd, str, tab)
1738 struct stringtab_data *tab;
1740 struct stringtab_entry **ep;
1741 register struct stringtab_entry *entry;
1742 unsigned int hashval, len;
1746 bfd_size_type index;
1747 CONST bfd_size_type minus_one = -1;
1749 #ifdef GATHER_STATISTICS
1750 tab->empty_strings++;
1752 index = tab->empty_string_index;
1753 if (index != minus_one)
1756 #ifdef GATHER_STATISTICS
1763 /* Need to find it. */
1764 entry = tab->strings;
1767 index = entry->index + strlen (entry->string);
1768 tab->empty_string_index = index;
1776 /* The hash_zero value is chosen such that the first symbol gets a value of
1777 zero. With a balanced tree, this wouldn't be very useful, but without it,
1778 we might get a more even split at the top level, instead of skewing it
1779 badly should hash("/usr/lib/crt0.o") (or whatever) be far from zero. */
1780 hashval = hash (str, len) ^ tab->hash_zero;
1784 tab->hash_zero = hashval;
1794 #ifdef GATHER_STATISTICS
1797 cmp = compare (entry, str, hashval);
1798 /* The not-equal cases are more frequent, so check them first. */
1800 ep = &entry->greater;
1805 if (entry->string == str)
1807 #ifdef GATHER_STATISTICS
1808 tab->pointer_matches++;
1812 /* Compare the first bytes to save a function call if they
1814 if (entry->string[0] == str[0] && !strcmp (entry->string, str))
1817 #ifdef GATHER_STATISTICS
1819 tab->bytes_saved += len + 1;
1822 /* If we're in the linker, and the new string is from a new
1823 input file which might have already had these reductions
1824 run over it, we want to keep the new string pointer. I
1825 don't think we're likely to see any (or nearly as many,
1826 at least) cases where a later string is in the same location
1827 as an earlier one rather than this one. */
1828 entry->string = str;
1829 return entry->index;
1831 #ifdef GATHER_STATISTICS
1832 tab->bad_hash_matches++;
1834 ep = &entry->greater;
1838 /* If we get here, nothing that's in the table already matched.
1839 EP points to the `next' field at the end of the chain; stick a
1840 new entry on here. */
1842 entry = (struct stringtab_entry *)
1843 bfd_alloc_by_size_t (abfd, sizeof (struct stringtab_entry));
1846 bfd_error = no_memory;
1847 abort(); /* FIXME */
1850 entry->less = entry->greater = 0;
1851 entry->hash = hashval;
1852 entry->index = tab->index;
1853 entry->string = str;
1854 entry->next_to_output = 0;
1855 #ifdef GATHER_STATISTICS
1859 assert (*tab->end == 0);
1860 *(tab->end) = entry;
1861 tab->end = &entry->next_to_output;
1862 assert (*tab->end == 0);
1865 tab->index += len + 1;
1867 tab->empty_string_index = entry->index;
1871 return entry->index;
1875 emit_strtab (abfd, tab)
1877 struct stringtab_data *tab;
1879 struct stringtab_entry *entry;
1880 #ifdef GATHER_STATISTICS
1884 /* Be sure to put string length into correct byte ordering before writing
1886 char buffer[BYTES_IN_WORD];
1888 PUT_WORD (abfd, tab->index, (unsigned char *) buffer);
1889 bfd_write ((PTR) buffer, 1, BYTES_IN_WORD, abfd);
1891 for (entry = tab->output_order; entry; entry = entry->next_to_output)
1893 bfd_write ((PTR) entry->string, 1, strlen (entry->string) + 1, abfd);
1894 #ifdef GATHER_STATISTICS
1899 #ifdef GATHER_STATISTICS
1900 /* Short form only, for now.
1901 To do: Specify output file. Conditionalize on environment? Detailed
1902 analysis if desired. */
1904 int n_syms = bfd_get_symcount (abfd);
1906 fprintf (stderr, "String table data for output file:\n");
1907 fprintf (stderr, " %8d symbols output\n", n_syms);
1908 fprintf (stderr, " %8d duplicate strings\n", tab->duplicates);
1909 fprintf (stderr, " %8d empty strings\n", tab->empty_strings);
1910 fprintf (stderr, " %8d unique strings output\n", count);
1911 fprintf (stderr, " %8d pointer matches\n", tab->pointer_matches);
1912 fprintf (stderr, " %8d bytes saved\n", tab->bytes_saved);
1913 fprintf (stderr, " %8d bad hash matches\n", tab->bad_hash_matches);
1914 fprintf (stderr, " %8d hash-val comparisons\n", tab->n_compares);
1917 double n_compares = tab->n_compares;
1918 double avg_compares = n_compares / n_syms;
1919 /* The second value here should usually be near one. */
1921 "\t average %f comparisons per symbol (%f * log2 nstrings)\n",
1922 avg_compares, avg_compares / log2 (count));
1929 generic = bfd_get_outsymbols(abfd);
1930 for (count = 0; count < bfd_get_symcount(abfd); count++)
1932 asymbol *g = *(generic++);
1936 size_t length = strlen(g->name)+1;
1937 bfd_write((PTR)g->name, 1, length, abfd);
1939 g->KEEPIT = (KEEPITTYPE) count;
1944 DEFUN(NAME(aout,write_syms),(abfd),
1947 unsigned int count ;
1948 asymbol **generic = bfd_get_outsymbols (abfd);
1949 struct stringtab_data strtab;
1951 stringtab_init (&strtab);
1953 for (count = 0; count < bfd_get_symcount (abfd); count++)
1955 asymbol *g = generic[count];
1956 struct external_nlist nsp;
1959 PUT_WORD (abfd, add_to_stringtab (abfd, g->name, &strtab),
1960 (unsigned char *) nsp.e_strx);
1962 PUT_WORD (abfd, 0, (unsigned char *)nsp.e_strx);
1964 if (bfd_asymbol_flavour(g) == abfd->xvec->flavour)
1966 bfd_h_put_16(abfd, aout_symbol(g)->desc, nsp.e_desc);
1967 bfd_h_put_8(abfd, aout_symbol(g)->other, nsp.e_other);
1968 bfd_h_put_8(abfd, aout_symbol(g)->type, nsp.e_type);
1972 bfd_h_put_16(abfd,0, nsp.e_desc);
1973 bfd_h_put_8(abfd, 0, nsp.e_other);
1974 bfd_h_put_8(abfd, 0, nsp.e_type);
1977 if (! translate_to_native_sym_flags (&nsp, g, abfd))
1980 if (bfd_write((PTR)&nsp,1,EXTERNAL_NLIST_SIZE, abfd)
1981 != EXTERNAL_NLIST_SIZE)
1984 /* NB: `KEEPIT' currently overlays `flags', so set this only
1985 here, at the end. */
1989 emit_strtab (abfd, &strtab);
1996 DEFUN(NAME(aout,get_symtab),(abfd, location),
2000 unsigned int counter = 0;
2001 aout_symbol_type *symbase;
2003 if (!NAME(aout,slurp_symbol_table)(abfd)) return 0;
2005 for (symbase = obj_aout_symbols(abfd); counter++ < bfd_get_symcount (abfd);)
2006 *(location++) = (asymbol *)( symbase++);
2008 return bfd_get_symcount (abfd);
2012 /* Standard reloc stuff */
2013 /* Output standard relocation information to a file in target byte order. */
2016 DEFUN(NAME(aout,swap_std_reloc_out),(abfd, g, natptr),
2019 struct reloc_std_external *natptr)
2022 asymbol *sym = *(g->sym_ptr_ptr);
2024 unsigned int r_length;
2026 int r_baserel, r_jmptable, r_relative;
2027 asection *output_section = sym->section->output_section;
2029 PUT_WORD(abfd, g->address, natptr->r_address);
2031 r_length = g->howto->size ; /* Size as a power of two */
2032 r_pcrel = (int) g->howto->pc_relative; /* Relative to PC? */
2033 /* XXX This relies on relocs coming from a.out files. */
2034 r_baserel = (g->howto->type & 8) != 0;
2035 /* r_jmptable, r_relative??? FIXME-soon */
2040 /* For a standard reloc, the addend is in the object file. */
2041 r_addend = g->addend + (*(g->sym_ptr_ptr))->section->output_section->vma;
2044 /* name was clobbered by aout_write_syms to be symbol index */
2046 /* If this relocation is relative to a symbol then set the
2047 r_index to the symbols index, and the r_extern bit.
2049 Absolute symbols can come in in two ways, either as an offset
2050 from the abs section, or as a symbol which has an abs value.
2055 if (bfd_is_com_section (output_section)
2056 || output_section == &bfd_abs_section
2057 || output_section == &bfd_und_section)
2059 if (bfd_abs_section.symbol == sym)
2061 /* Whoops, looked like an abs symbol, but is really an offset
2062 from the abs section */
2068 /* Fill in symbol */
2070 r_index = stoi((*(g->sym_ptr_ptr))->KEEPIT);
2076 /* Just an ordinary section */
2078 r_index = output_section->target_index;
2081 /* now the fun stuff */
2082 if (abfd->xvec->header_byteorder_big_p != false) {
2083 natptr->r_index[0] = r_index >> 16;
2084 natptr->r_index[1] = r_index >> 8;
2085 natptr->r_index[2] = r_index;
2087 (r_extern? RELOC_STD_BITS_EXTERN_BIG: 0)
2088 | (r_pcrel? RELOC_STD_BITS_PCREL_BIG: 0)
2089 | (r_baserel? RELOC_STD_BITS_BASEREL_BIG: 0)
2090 | (r_jmptable? RELOC_STD_BITS_JMPTABLE_BIG: 0)
2091 | (r_relative? RELOC_STD_BITS_RELATIVE_BIG: 0)
2092 | (r_length << RELOC_STD_BITS_LENGTH_SH_BIG);
2094 natptr->r_index[2] = r_index >> 16;
2095 natptr->r_index[1] = r_index >> 8;
2096 natptr->r_index[0] = r_index;
2098 (r_extern? RELOC_STD_BITS_EXTERN_LITTLE: 0)
2099 | (r_pcrel? RELOC_STD_BITS_PCREL_LITTLE: 0)
2100 | (r_baserel? RELOC_STD_BITS_BASEREL_LITTLE: 0)
2101 | (r_jmptable? RELOC_STD_BITS_JMPTABLE_LITTLE: 0)
2102 | (r_relative? RELOC_STD_BITS_RELATIVE_LITTLE: 0)
2103 | (r_length << RELOC_STD_BITS_LENGTH_SH_LITTLE);
2108 /* Extended stuff */
2109 /* Output extended relocation information to a file in target byte order. */
2112 DEFUN(NAME(aout,swap_ext_reloc_out),(abfd, g, natptr),
2115 register struct reloc_ext_external *natptr)
2119 unsigned int r_type;
2120 unsigned int r_addend;
2121 asymbol *sym = *(g->sym_ptr_ptr);
2122 asection *output_section = sym->section->output_section;
2124 PUT_WORD (abfd, g->address, natptr->r_address);
2126 r_type = (unsigned int) g->howto->type;
2128 r_addend = g->addend + (*(g->sym_ptr_ptr))->section->output_section->vma;
2130 /* If this relocation is relative to a symbol then set the
2131 r_index to the symbols index, and the r_extern bit.
2133 Absolute symbols can come in in two ways, either as an offset
2134 from the abs section, or as a symbol which has an abs value.
2135 check for that here. */
2137 if (bfd_is_com_section (output_section)
2138 || output_section == &bfd_abs_section
2139 || output_section == &bfd_und_section)
2141 if (bfd_abs_section.symbol == sym)
2143 /* Whoops, looked like an abs symbol, but is really an offset
2144 from the abs section */
2151 r_index = stoi((*(g->sym_ptr_ptr))->KEEPIT);
2156 /* Just an ordinary section */
2158 r_index = output_section->target_index;
2161 /* now the fun stuff */
2162 if (abfd->xvec->header_byteorder_big_p != false) {
2163 natptr->r_index[0] = r_index >> 16;
2164 natptr->r_index[1] = r_index >> 8;
2165 natptr->r_index[2] = r_index;
2167 ((r_extern? RELOC_EXT_BITS_EXTERN_BIG: 0)
2168 | (r_type << RELOC_EXT_BITS_TYPE_SH_BIG));
2170 natptr->r_index[2] = r_index >> 16;
2171 natptr->r_index[1] = r_index >> 8;
2172 natptr->r_index[0] = r_index;
2174 (r_extern? RELOC_EXT_BITS_EXTERN_LITTLE: 0)
2175 | (r_type << RELOC_EXT_BITS_TYPE_SH_LITTLE);
2178 PUT_WORD (abfd, r_addend, natptr->r_addend);
2181 /* BFD deals internally with all things based from the section they're
2182 in. so, something in 10 bytes into a text section with a base of
2183 50 would have a symbol (.text+10) and know .text vma was 50.
2185 Aout keeps all it's symbols based from zero, so the symbol would
2186 contain 60. This macro subs the base of each section from the value
2187 to give the true offset from the section */
2190 #define MOVE_ADDRESS(ad) \
2192 /* undefined symbol */ \
2193 cache_ptr->sym_ptr_ptr = symbols + r_index; \
2194 cache_ptr->addend = ad; \
2196 /* defined, section relative. replace symbol with pointer to \
2197 symbol which points to section */ \
2198 switch (r_index) { \
2200 case N_TEXT | N_EXT: \
2201 cache_ptr->sym_ptr_ptr = obj_textsec(abfd)->symbol_ptr_ptr; \
2202 cache_ptr->addend = ad - su->textsec->vma; \
2205 case N_DATA | N_EXT: \
2206 cache_ptr->sym_ptr_ptr = obj_datasec(abfd)->symbol_ptr_ptr; \
2207 cache_ptr->addend = ad - su->datasec->vma; \
2210 case N_BSS | N_EXT: \
2211 cache_ptr->sym_ptr_ptr = obj_bsssec(abfd)->symbol_ptr_ptr; \
2212 cache_ptr->addend = ad - su->bsssec->vma; \
2216 case N_ABS | N_EXT: \
2217 cache_ptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr; \
2218 cache_ptr->addend = ad; \
2224 DEFUN(NAME(aout,swap_ext_reloc_in), (abfd, bytes, cache_ptr, symbols),
2226 struct reloc_ext_external *bytes AND
2227 arelent *cache_ptr AND
2232 unsigned int r_type;
2233 struct aoutdata *su = &(abfd->tdata.aout_data->a);
2235 cache_ptr->address = (GET_SWORD (abfd, bytes->r_address));
2237 /* now the fun stuff */
2238 if (abfd->xvec->header_byteorder_big_p != false) {
2239 r_index = (bytes->r_index[0] << 16)
2240 | (bytes->r_index[1] << 8)
2241 | bytes->r_index[2];
2242 r_extern = (0 != (bytes->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG));
2243 r_type = (bytes->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
2244 >> RELOC_EXT_BITS_TYPE_SH_BIG;
2246 r_index = (bytes->r_index[2] << 16)
2247 | (bytes->r_index[1] << 8)
2248 | bytes->r_index[0];
2249 r_extern = (0 != (bytes->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
2250 r_type = (bytes->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
2251 >> RELOC_EXT_BITS_TYPE_SH_LITTLE;
2254 cache_ptr->howto = howto_table_ext + r_type;
2255 MOVE_ADDRESS(GET_SWORD(abfd, bytes->r_addend));
2259 DEFUN(NAME(aout,swap_std_reloc_in), (abfd, bytes, cache_ptr, symbols),
2261 struct reloc_std_external *bytes AND
2262 arelent *cache_ptr AND
2267 unsigned int r_length;
2269 int r_baserel, r_jmptable, r_relative;
2270 struct aoutdata *su = &(abfd->tdata.aout_data->a);
2273 cache_ptr->address = bfd_h_get_32 (abfd, bytes->r_address);
2275 /* now the fun stuff */
2276 if (abfd->xvec->header_byteorder_big_p != false) {
2277 r_index = (bytes->r_index[0] << 16)
2278 | (bytes->r_index[1] << 8)
2279 | bytes->r_index[2];
2280 r_extern = (0 != (bytes->r_type[0] & RELOC_STD_BITS_EXTERN_BIG));
2281 r_pcrel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_PCREL_BIG));
2282 r_baserel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_BASEREL_BIG));
2283 r_jmptable= (0 != (bytes->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG));
2284 r_relative= (0 != (bytes->r_type[0] & RELOC_STD_BITS_RELATIVE_BIG));
2285 r_length = (bytes->r_type[0] & RELOC_STD_BITS_LENGTH_BIG)
2286 >> RELOC_STD_BITS_LENGTH_SH_BIG;
2288 r_index = (bytes->r_index[2] << 16)
2289 | (bytes->r_index[1] << 8)
2290 | bytes->r_index[0];
2291 r_extern = (0 != (bytes->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE));
2292 r_pcrel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
2293 r_baserel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_BASEREL_LITTLE));
2294 r_jmptable= (0 != (bytes->r_type[0] & RELOC_STD_BITS_JMPTABLE_LITTLE));
2295 r_relative= (0 != (bytes->r_type[0] & RELOC_STD_BITS_RELATIVE_LITTLE));
2296 r_length = (bytes->r_type[0] & RELOC_STD_BITS_LENGTH_LITTLE)
2297 >> RELOC_STD_BITS_LENGTH_SH_LITTLE;
2300 howto_idx = r_length + 4 * r_pcrel + 8 * r_baserel;
2301 BFD_ASSERT (howto_idx < TABLE_SIZE (howto_table_std));
2302 cache_ptr->howto = howto_table_std + howto_idx;
2303 BFD_ASSERT (cache_ptr->howto->type != -1);
2304 BFD_ASSERT (r_jmptable == 0);
2305 BFD_ASSERT (r_relative == 0);
2306 /* FIXME-soon: Roll jmptable, relative bits into howto setting */
2314 DEFUN(NAME(aout,slurp_reloc_table),(abfd, asect, symbols),
2320 bfd_size_type reloc_size;
2322 bfd_size_type dynrel_count = 0;
2324 arelent *reloc_cache;
2326 unsigned int counter = 0;
2329 if (asect->relocation) return true;
2331 if (asect->flags & SEC_CONSTRUCTOR) return true;
2333 if (asect == obj_datasec (abfd))
2334 reloc_size = exec_hdr(abfd)->a_drsize;
2335 else if (asect == obj_textsec (abfd))
2336 reloc_size = exec_hdr(abfd)->a_trsize;
2339 bfd_error = invalid_operation;
2343 if ((bfd_get_file_flags (abfd) & DYNAMIC) != 0
2344 && aout_backend_info (abfd)->read_dynamic_relocs)
2346 dynrel_count = ((*aout_backend_info (abfd)->read_dynamic_relocs)
2348 if (dynrel_count == (bfd_size_type) -1)
2352 bfd_seek (abfd, asect->rel_filepos, SEEK_SET);
2353 each_size = obj_reloc_entry_size (abfd);
2355 count = reloc_size / each_size;
2357 reloc_cache = ((arelent *)
2359 (size_t) ((count + dynrel_count)
2360 * sizeof (arelent))));
2364 bfd_error = no_memory;
2368 relocs = (PTR) bfd_alloc (abfd, reloc_size);
2371 bfd_release (abfd, reloc_cache);
2375 if (bfd_read (relocs, 1, reloc_size, abfd) != reloc_size)
2377 bfd_release (abfd, relocs);
2378 bfd_release (abfd, reloc_cache);
2379 bfd_error = system_call_error;
2383 cache_ptr = reloc_cache;
2384 if (each_size == RELOC_EXT_SIZE)
2386 register struct reloc_ext_external *rptr =
2387 (struct reloc_ext_external *) relocs;
2389 for (; counter < count; counter++, rptr++, cache_ptr++)
2390 NAME(aout,swap_ext_reloc_in) (abfd, rptr, cache_ptr, symbols);
2394 register struct reloc_std_external *rptr
2395 = (struct reloc_std_external *) relocs;
2397 for (; counter < count; counter++, rptr++, cache_ptr++)
2398 NAME(aout,swap_std_reloc_in) (abfd, rptr, cache_ptr, symbols);
2401 if (dynrel_count > 0)
2405 /* The dynamic symbols are at the end of the symbol table. */
2406 for (dynsyms = symbols;
2407 *dynsyms != NULL && ((*dynsyms)->flags & BSF_DYNAMIC) == 0;
2411 /* Swap in the dynamic relocs. These relocs may be for either
2412 section, so we must discard ones we don't want. */
2414 if (each_size == RELOC_EXT_SIZE)
2416 register struct reloc_ext_external *rptr
2417 = (struct reloc_ext_external *) dynrels;
2419 for (; counter < dynrel_count; counter++, rptr++, cache_ptr++)
2421 NAME(aout,swap_ext_reloc_in) (abfd, rptr, cache_ptr, dynsyms);
2422 cache_ptr->address -= bfd_get_section_vma (abfd, asect);
2423 if (cache_ptr->address >= bfd_section_size (abfd, asect))
2429 register struct reloc_std_external *rptr
2430 = (struct reloc_std_external *) dynrels;
2432 for (; counter < dynrel_count; counter++, rptr++, cache_ptr++)
2434 NAME(aout,swap_std_reloc_in) (abfd, rptr, cache_ptr, dynsyms);
2435 cache_ptr->address -= bfd_get_section_vma (abfd, asect);
2436 if (cache_ptr->address >= bfd_section_size (abfd, asect))
2442 bfd_release (abfd,relocs);
2443 asect->relocation = reloc_cache;
2444 asect->reloc_count = cache_ptr - reloc_cache;
2450 /* Write out a relocation section into an object file. */
2453 DEFUN(NAME(aout,squirt_out_relocs),(abfd, section),
2458 unsigned char *native, *natptr;
2461 unsigned int count = section->reloc_count;
2464 if (count == 0) return true;
2466 each_size = obj_reloc_entry_size (abfd);
2467 natsize = each_size * count;
2468 native = (unsigned char *) bfd_zalloc (abfd, natsize);
2470 bfd_error = no_memory;
2474 generic = section->orelocation;
2476 if (each_size == RELOC_EXT_SIZE)
2478 for (natptr = native;
2480 --count, natptr += each_size, ++generic)
2481 NAME(aout,swap_ext_reloc_out) (abfd, *generic, (struct reloc_ext_external *)natptr);
2485 for (natptr = native;
2487 --count, natptr += each_size, ++generic)
2488 NAME(aout,swap_std_reloc_out)(abfd, *generic, (struct reloc_std_external *)natptr);
2491 if ( bfd_write ((PTR) native, 1, natsize, abfd) != natsize) {
2492 bfd_release(abfd, native);
2495 bfd_release (abfd, native);
2500 /* This is stupid. This function should be a boolean predicate */
2502 DEFUN(NAME(aout,canonicalize_reloc),(abfd, section, relptr, symbols),
2505 arelent **relptr AND
2508 arelent *tblptr = section->relocation;
2511 if (!(tblptr || NAME(aout,slurp_reloc_table)(abfd, section, symbols)))
2514 if (section->flags & SEC_CONSTRUCTOR) {
2515 arelent_chain *chain = section->constructor_chain;
2516 for (count = 0; count < section->reloc_count; count ++) {
2517 *relptr ++ = &chain->relent;
2518 chain = chain->next;
2522 tblptr = section->relocation;
2523 if (!tblptr) return 0;
2525 for (count = 0; count++ < section->reloc_count;)
2527 *relptr++ = tblptr++;
2532 return section->reloc_count;
2536 DEFUN(NAME(aout,get_reloc_upper_bound),(abfd, asect),
2540 bfd_size_type dynrel_count = 0;
2542 if (bfd_get_format (abfd) != bfd_object) {
2543 bfd_error = invalid_operation;
2546 if (asect->flags & SEC_CONSTRUCTOR) {
2547 return (sizeof (arelent *) * (asect->reloc_count+1));
2550 if ((bfd_get_file_flags (abfd) & DYNAMIC) != 0
2551 && aout_backend_info (abfd)->read_dynamic_relocs)
2555 dynrel_count = ((*aout_backend_info (abfd)->read_dynamic_relocs)
2557 if (dynrel_count == (bfd_size_type) -1)
2561 if (asect == obj_datasec (abfd))
2562 return (sizeof (arelent *) *
2563 ((exec_hdr(abfd)->a_drsize / obj_reloc_entry_size (abfd))
2564 + dynrel_count + 1));
2566 if (asect == obj_textsec (abfd))
2567 return (sizeof (arelent *) *
2568 ((exec_hdr(abfd)->a_trsize / obj_reloc_entry_size (abfd))
2569 + dynrel_count + 1));
2571 bfd_error = invalid_operation;
2577 DEFUN(NAME(aout,get_symtab_upper_bound),(abfd),
2580 if (!NAME(aout,slurp_symbol_table)(abfd)) return 0;
2582 return (bfd_get_symcount (abfd)+1) * (sizeof (aout_symbol_type *));
2587 DEFUN(NAME(aout,get_lineno),(ignore_abfd, ignore_symbol),
2588 bfd *ignore_abfd AND
2589 asymbol *ignore_symbol)
2591 return (alent *)NULL;
2596 DEFUN(NAME(aout,get_symbol_info),(ignore_abfd, symbol, ret),
2597 bfd *ignore_abfd AND
2601 bfd_symbol_info (symbol, ret);
2603 if (ret->type == '?')
2605 int type_code = aout_symbol(symbol)->type & 0xff;
2606 CONST char *stab_name = aout_stab_name(type_code);
2607 static char buf[10];
2609 if (stab_name == NULL)
2611 sprintf(buf, "(%d)", type_code);
2615 ret->stab_other = (unsigned)(aout_symbol(symbol)->other & 0xff);
2616 ret->stab_desc = (unsigned)(aout_symbol(symbol)->desc & 0xffff);
2617 ret->stab_name = stab_name;
2623 DEFUN(NAME(aout,print_symbol),(ignore_abfd, afile, symbol, how),
2624 bfd *ignore_abfd AND
2627 bfd_print_symbol_type how)
2629 FILE *file = (FILE *)afile;
2632 case bfd_print_symbol_name:
2634 fprintf(file,"%s", symbol->name);
2636 case bfd_print_symbol_more:
2637 fprintf(file,"%4x %2x %2x",(unsigned)(aout_symbol(symbol)->desc & 0xffff),
2638 (unsigned)(aout_symbol(symbol)->other & 0xff),
2639 (unsigned)(aout_symbol(symbol)->type));
2641 case bfd_print_symbol_all:
2643 CONST char *section_name = symbol->section->name;
2646 bfd_print_symbol_vandf((PTR)file,symbol);
2648 fprintf(file," %-5s %04x %02x %02x",
2650 (unsigned)(aout_symbol(symbol)->desc & 0xffff),
2651 (unsigned)(aout_symbol(symbol)->other & 0xff),
2652 (unsigned)(aout_symbol(symbol)->type & 0xff));
2654 fprintf(file," %s", symbol->name);
2661 provided a BFD, a section and an offset into the section, calculate
2662 and return the name of the source file and the line nearest to the
2667 DEFUN(NAME(aout,find_nearest_line),(abfd,
2675 asection *section AND
2676 asymbol **symbols AND
2678 CONST char **filename_ptr AND
2679 CONST char **functionname_ptr AND
2680 unsigned int *line_ptr)
2682 /* Run down the file looking for the filename, function and linenumber */
2684 static char buffer[100];
2685 static char filename_buffer[200];
2686 CONST char *directory_name = NULL;
2687 CONST char *main_file_name = NULL;
2688 CONST char *current_file_name = NULL;
2689 CONST char *line_file_name = NULL; /* Value of current_file_name at line number. */
2690 bfd_vma high_line_vma = ~0;
2691 bfd_vma low_func_vma = 0;
2693 *filename_ptr = abfd->filename;
2694 *functionname_ptr = 0;
2696 if (symbols != (asymbol **)NULL) {
2697 for (p = symbols; *p; p++) {
2698 aout_symbol_type *q = (aout_symbol_type *)(*p);
2702 main_file_name = current_file_name = q->symbol.name;
2703 /* Look ahead to next symbol to check if that too is an N_SO. */
2707 q = (aout_symbol_type *)(*p);
2708 if (q->type != (int)N_SO)
2711 /* Found a second N_SO First is directory; second is filename. */
2712 directory_name = current_file_name;
2713 main_file_name = current_file_name = q->symbol.name;
2714 if (obj_textsec(abfd) != section)
2718 current_file_name = q->symbol.name;
2725 /* We'll keep this if it resolves nearer than the one we have already */
2726 if (q->symbol.value >= offset &&
2727 q->symbol.value < high_line_vma) {
2728 *line_ptr = q->desc;
2729 high_line_vma = q->symbol.value;
2730 line_file_name = current_file_name;
2735 /* We'll keep this if it is nearer than the one we have already */
2736 if (q->symbol.value >= low_func_vma &&
2737 q->symbol.value <= offset) {
2738 low_func_vma = q->symbol.value;
2739 func = (asymbol *)q;
2741 if (*line_ptr && func) {
2742 CONST char *function = func->name;
2744 strncpy(buffer, function, sizeof(buffer)-1);
2745 buffer[sizeof(buffer)-1] = 0;
2746 /* Have to remove : stuff */
2747 p = strchr(buffer,':');
2748 if (p != NULL) { *p = '\0'; }
2749 *functionname_ptr = buffer;
2761 main_file_name = line_file_name;
2762 if (main_file_name) {
2763 if (main_file_name[0] == '/' || directory_name == NULL)
2764 *filename_ptr = main_file_name;
2766 sprintf(filename_buffer, "%.140s%.50s",
2767 directory_name, main_file_name);
2768 *filename_ptr = filename_buffer;
2777 DEFUN(NAME(aout,sizeof_headers),(abfd, execable),
2781 return adata(abfd).exec_bytes_size;
2784 /* a.out link code. */
2786 /* a.out linker hash table entries. */
2788 struct aout_link_hash_entry
2790 struct bfd_link_hash_entry root;
2791 /* Symbol index in output file. */
2795 /* a.out linker hash table. */
2797 struct aout_link_hash_table
2799 struct bfd_link_hash_table root;
2802 static struct bfd_hash_entry *aout_link_hash_newfunc
2803 PARAMS ((struct bfd_hash_entry *entry,
2804 struct bfd_hash_table *table,
2805 const char *string));
2806 static boolean aout_link_add_object_symbols
2807 PARAMS ((bfd *, struct bfd_link_info *));
2808 static boolean aout_link_check_archive_element
2809 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
2810 static boolean aout_link_get_symbols PARAMS ((bfd *));
2811 static boolean aout_link_free_symbols PARAMS ((bfd *));
2812 static boolean aout_link_check_ar_symbols
2813 PARAMS ((bfd *, struct bfd_link_info *, boolean *pneeded));
2814 static boolean aout_link_add_symbols
2815 PARAMS ((bfd *, struct bfd_link_info *));
2817 /* Routine to create an entry in an a.out link hash table. */
2819 static struct bfd_hash_entry *
2820 aout_link_hash_newfunc (entry, table, string)
2821 struct bfd_hash_entry *entry;
2822 struct bfd_hash_table *table;
2825 struct aout_link_hash_entry *ret = (struct aout_link_hash_entry *) entry;
2827 /* Allocate the structure if it has not already been allocated by a
2829 if (ret == (struct aout_link_hash_entry *) NULL)
2830 ret = ((struct aout_link_hash_entry *)
2831 bfd_hash_allocate (table, sizeof (struct aout_link_hash_entry)));
2832 if (ret == (struct aout_link_hash_entry *) NULL)
2834 bfd_error = no_memory;
2835 return (struct bfd_hash_entry *) ret;
2838 /* Call the allocation method of the superclass. */
2839 ret = ((struct aout_link_hash_entry *)
2840 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2843 /* Set local fields. */
2846 return (struct bfd_hash_entry *) ret;
2849 /* Create an a.out link hash table. */
2851 struct bfd_link_hash_table *
2852 NAME(aout,link_hash_table_create) (abfd)
2855 struct aout_link_hash_table *ret;
2857 ret = ((struct aout_link_hash_table *)
2858 malloc (sizeof (struct aout_link_hash_table)));
2859 if (ret == (struct aout_link_hash_table *) NULL)
2861 bfd_error = no_memory;
2862 return (struct bfd_link_hash_table *) NULL;
2864 if (! _bfd_link_hash_table_init (&ret->root, abfd,
2865 aout_link_hash_newfunc))
2868 return (struct bfd_link_hash_table *) NULL;
2873 /* Look up an entry in an a.out link hash table. */
2875 #define aout_link_hash_lookup(table, string, create, copy, follow) \
2876 ((struct aout_link_hash_entry *) \
2877 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy), (follow)))
2879 /* Traverse an a.out link hash table. */
2881 #define aout_link_hash_traverse(table, func, info) \
2882 (bfd_link_hash_traverse \
2884 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
2887 /* Get the a.out link hash table from the info structure. This is
2890 #define aout_hash_table(p) ((struct aout_link_hash_table *) ((p)->hash))
2892 /* Given an a.out BFD, add symbols to the global hash table as
2896 NAME(aout,link_add_symbols) (abfd, info)
2898 struct bfd_link_info *info;
2900 switch (bfd_get_format (abfd))
2903 return aout_link_add_object_symbols (abfd, info);
2905 return _bfd_generic_link_add_archive_symbols
2906 (abfd, info, aout_link_check_archive_element);
2908 bfd_error = wrong_format;
2913 /* Add symbols from an a.out object file. */
2916 aout_link_add_object_symbols (abfd, info)
2918 struct bfd_link_info *info;
2920 if (! aout_link_get_symbols (abfd))
2922 if (! aout_link_add_symbols (abfd, info))
2924 if (! info->keep_memory)
2926 if (! aout_link_free_symbols (abfd))
2932 /* Check a single archive element to see if we need to include it in
2933 the link. *PNEEDED is set according to whether this element is
2934 needed in the link or not. This is called from
2935 _bfd_generic_link_add_archive_symbols. */
2938 aout_link_check_archive_element (abfd, info, pneeded)
2940 struct bfd_link_info *info;
2943 if (! aout_link_get_symbols (abfd))
2946 if (! aout_link_check_ar_symbols (abfd, info, pneeded))
2951 if (! aout_link_add_symbols (abfd, info))
2955 /* We keep around the symbols even if we aren't going to use this
2956 object file, because we may want to reread it. This doesn't
2957 waste too much memory, because it isn't all that common to read
2958 an archive element but not need it. */
2959 if (! info->keep_memory)
2961 if (! aout_link_free_symbols (abfd))
2968 /* Read the internal symbols from an a.out file. */
2971 aout_link_get_symbols (abfd)
2974 bfd_size_type count;
2975 struct external_nlist *syms;
2976 unsigned char string_chars[BYTES_IN_WORD];
2977 bfd_size_type stringsize;
2980 if (obj_aout_external_syms (abfd) != (struct external_nlist *) NULL)
2982 /* We already have them. */
2986 count = exec_hdr (abfd)->a_syms / EXTERNAL_NLIST_SIZE;
2988 /* We allocate using malloc to make the values easy to free
2989 later on. If we put them on the obstack it might not be possible
2991 syms = ((struct external_nlist *)
2992 malloc ((size_t) count * EXTERNAL_NLIST_SIZE));
2993 if (syms == (struct external_nlist *) NULL)
2995 bfd_error = no_memory;
2999 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
3000 || (bfd_read ((PTR) syms, 1, exec_hdr (abfd)->a_syms, abfd)
3001 != exec_hdr (abfd)->a_syms))
3004 /* Get the size of the strings. */
3005 if (bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET) != 0
3006 || (bfd_read ((PTR) string_chars, BYTES_IN_WORD, 1, abfd)
3009 stringsize = GET_WORD (abfd, string_chars);
3010 strings = (char *) malloc ((size_t) stringsize);
3011 if (strings == NULL)
3013 bfd_error = no_memory;
3017 /* Skip space for the string count in the buffer for convenience
3018 when using indexes. */
3019 if (bfd_read (strings + BYTES_IN_WORD, 1, stringsize - BYTES_IN_WORD, abfd)
3020 != stringsize - BYTES_IN_WORD)
3023 /* Save the data. */
3024 obj_aout_external_syms (abfd) = syms;
3025 obj_aout_external_sym_count (abfd) = count;
3026 obj_aout_external_strings (abfd) = strings;
3031 /* Free up the internal symbols read from an a.out file. */
3034 aout_link_free_symbols (abfd)
3037 if (obj_aout_external_syms (abfd) != (struct external_nlist *) NULL)
3039 free ((PTR) obj_aout_external_syms (abfd));
3040 obj_aout_external_syms (abfd) = (struct external_nlist *) NULL;
3042 if (obj_aout_external_strings (abfd) != (char *) NULL)
3044 free ((PTR) obj_aout_external_strings (abfd));
3045 obj_aout_external_strings (abfd) = (char *) NULL;
3050 /* Look through the internal symbols to see if this object file should
3051 be included in the link. We should include this object file if it
3052 defines any symbols which are currently undefined. If this object
3053 file defines a common symbol, then we may adjust the size of the
3054 known symbol but we do not include the object file in the link
3055 (unless there is some other reason to include it). */
3058 aout_link_check_ar_symbols (abfd, info, pneeded)
3060 struct bfd_link_info *info;
3063 register struct external_nlist *p;
3064 struct external_nlist *pend;
3069 /* Look through all the symbols. */
3070 p = obj_aout_external_syms (abfd);
3071 pend = p + obj_aout_external_sym_count (abfd);
3072 strings = obj_aout_external_strings (abfd);
3073 for (; p < pend; p++)
3075 int type = bfd_h_get_8 (abfd, p->e_type);
3077 struct bfd_link_hash_entry *h;
3079 /* Ignore symbols that are not externally visible. */
3080 if ((type & N_EXT) == 0)
3082 if (type == N_WARNING
3088 name = strings + GET_WORD (abfd, p->e_strx);
3089 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
3091 /* We are only interested in symbols that are currently
3092 undefined or common. */
3093 if (h == (struct bfd_link_hash_entry *) NULL
3094 || (h->type != bfd_link_hash_undefined
3095 && h->type != bfd_link_hash_common))
3097 if (type == (N_INDR | N_EXT))
3102 if (type == (N_TEXT | N_EXT)
3103 || type == (N_DATA | N_EXT)
3104 || type == (N_BSS | N_EXT)
3105 || type == (N_ABS | N_EXT)
3106 || type == (N_INDR | N_EXT))
3108 /* This object file defines this symbol. We must link it
3109 in. This is true regardless of whether the current
3110 definition of the symbol is undefined or common. If the
3111 current definition is common, we have a case in which we
3112 have already seen an object file including
3114 and this object file from the archive includes
3116 In such a case we must include this object file. */
3117 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
3123 if (type == (N_UNDF | N_EXT))
3127 value = GET_WORD (abfd, p->e_value);
3130 /* This symbol is common in the object from the archive
3132 if (h->type == bfd_link_hash_undefined)
3136 symbfd = h->u.undef.abfd;
3137 if (symbfd == (bfd *) NULL)
3139 /* This symbol was created as undefined from
3140 outside BFD. We assume that we should link
3141 in the object file. This is done for the -u
3142 option in the linker. */
3143 if (! (*info->callbacks->add_archive_element) (info,
3150 /* Turn the current link symbol into a common
3151 symbol. It is already on the undefs list. */
3152 h->type = bfd_link_hash_common;
3153 h->u.c.size = value;
3154 h->u.c.section = bfd_make_section_old_way (symbfd,
3159 /* Adjust the size of the common symbol if
3161 if (value > h->u.c.size)
3162 h->u.c.size = value;
3168 /* We do not need this object file. */
3172 /* Add all symbols from an object file to the hash table. */
3175 aout_link_add_symbols (abfd, info)
3177 struct bfd_link_info *info;
3179 bfd_size_type sym_count;
3182 struct aout_link_hash_entry **sym_hash;
3183 register struct external_nlist *p;
3184 struct external_nlist *pend;
3186 sym_count = obj_aout_external_sym_count (abfd);
3187 strings = obj_aout_external_strings (abfd);
3188 if (info->keep_memory)
3193 /* We keep a list of the linker hash table entries that correspond
3194 to particular symbols. We could just look them up in the hash
3195 table, but keeping the list is more efficient. Perhaps this
3196 should be conditional on info->keep_memory. */
3197 sym_hash = ((struct aout_link_hash_entry **)
3200 * sizeof (struct aout_link_hash_entry *))));
3203 bfd_error = no_memory;
3206 obj_aout_sym_hashes (abfd) = sym_hash;
3208 p = obj_aout_external_syms (abfd);
3209 pend = p + sym_count;
3210 for (; p < pend; p++, sym_hash++)
3221 type = bfd_h_get_8 (abfd, p->e_type);
3223 /* Ignore debugging symbols. */
3224 if ((type & N_STAB) != 0)
3227 /* Ignore symbols that are not external. */
3228 if ((type & N_EXT) == 0
3229 && type != N_WARNING
3235 /* If this is an N_INDR symbol we must skip the next entry,
3236 which is the symbol to indirect to (actually, an N_INDR
3237 symbol without N_EXT set is pretty useless). */
3246 /* Ignore N_FN symbols (these appear to have N_EXT set). */
3250 name = strings + GET_WORD (abfd, p->e_strx);
3251 value = GET_WORD (abfd, p->e_value);
3258 case N_UNDF | N_EXT:
3260 section = &bfd_com_section;
3262 section = &bfd_und_section;
3265 section = &bfd_abs_section;
3267 case N_TEXT | N_EXT:
3268 section = obj_textsec (abfd);
3269 value -= bfd_get_section_vma (abfd, section);
3271 case N_DATA | N_EXT:
3272 section = obj_datasec (abfd);
3273 value -= bfd_get_section_vma (abfd, section);
3276 section = obj_bsssec (abfd);
3277 value -= bfd_get_section_vma (abfd, section);
3279 case N_INDR | N_EXT:
3280 /* An indirect symbol. The next symbol is the symbol
3281 which this one really is. */
3282 BFD_ASSERT (p + 1 < pend);
3284 string = strings + GET_WORD (abfd, p->e_strx);
3285 section = &bfd_ind_section;
3286 flags |= BSF_INDIRECT;
3288 case N_COMM | N_EXT:
3289 section = &bfd_com_section;
3291 case N_SETA: case N_SETA | N_EXT:
3292 section = &bfd_abs_section;
3293 flags |= BSF_CONSTRUCTOR;
3295 case N_SETT: case N_SETT | N_EXT:
3296 section = obj_textsec (abfd);
3297 flags |= BSF_CONSTRUCTOR;
3298 value -= bfd_get_section_vma (abfd, section);
3300 case N_SETD: case N_SETD | N_EXT:
3301 section = obj_datasec (abfd);
3302 flags |= BSF_CONSTRUCTOR;
3303 value -= bfd_get_section_vma (abfd, section);
3305 case N_SETB: case N_SETB | N_EXT:
3306 section = obj_bsssec (abfd);
3307 flags |= BSF_CONSTRUCTOR;
3308 value -= bfd_get_section_vma (abfd, section);
3311 /* A warning symbol. The next symbol is the one to warn
3313 BFD_ASSERT (p + 1 < pend);
3316 name = strings + GET_WORD (abfd, p->e_strx);
3317 section = &bfd_und_section;
3318 flags |= BSF_WARNING;
3322 if (! (_bfd_generic_link_add_one_symbol
3323 (info, abfd, name, flags, section, value, string, copy, false,
3324 ARCH_SIZE, (struct bfd_link_hash_entry **) sym_hash)))
3327 if (type == (N_INDR | N_EXT) || type == N_WARNING)
3334 /* During the final link step we need to pass around a bunch of
3335 information, so we do it in an instance of this structure. */
3337 struct aout_final_link_info
3339 /* General link information. */
3340 struct bfd_link_info *info;
3343 /* Reloc file positions. */
3344 file_ptr treloff, dreloff;
3345 /* File position of symbols. */
3348 struct stringtab_data strtab;
3351 static boolean aout_link_input_bfd
3352 PARAMS ((struct aout_final_link_info *, bfd *input_bfd));
3353 static boolean aout_link_write_symbols
3354 PARAMS ((struct aout_final_link_info *, bfd *input_bfd, int *symbol_map));
3355 static boolean aout_link_write_other_symbol
3356 PARAMS ((struct aout_link_hash_entry *, PTR));
3357 static boolean aout_link_input_section
3358 PARAMS ((struct aout_final_link_info *, bfd *input_bfd,
3359 asection *input_section, file_ptr *reloff_ptr,
3360 bfd_size_type rel_size, int *symbol_map));
3361 static boolean aout_link_input_section_std
3362 PARAMS ((struct aout_final_link_info *, bfd *input_bfd,
3363 asection *input_section, struct reloc_std_external *,
3364 bfd_size_type rel_size, bfd_byte *contents, int *symbol_map));
3365 static boolean aout_link_input_section_ext
3366 PARAMS ((struct aout_final_link_info *, bfd *input_bfd,
3367 asection *input_section, struct reloc_ext_external *,
3368 bfd_size_type rel_size, bfd_byte *contents, int *symbol_map));
3369 static INLINE asection *aout_reloc_index_to_section
3370 PARAMS ((bfd *, int));
3372 /* Do the final link step. This is called on the output BFD. The
3373 INFO structure should point to a list of BFDs linked through the
3374 link_next field which can be used to find each BFD which takes part
3375 in the output. Also, each section in ABFD should point to a list
3376 of bfd_link_order structures which list all the input sections for
3377 the output section. */
3380 NAME(aout,final_link) (abfd, info, callback)
3382 struct bfd_link_info *info;
3383 void (*callback) PARAMS ((bfd *, file_ptr *, file_ptr *, file_ptr *));
3385 struct aout_final_link_info aout_info;
3387 bfd_size_type text_size;
3389 register struct bfd_link_order *p;
3392 aout_info.info = info;
3393 aout_info.output_bfd = abfd;
3395 if (! info->relocateable)
3397 exec_hdr (abfd)->a_trsize = 0;
3398 exec_hdr (abfd)->a_drsize = 0;
3402 bfd_size_type trsize, drsize;
3404 /* Count up the relocation sizes. */
3407 for (sub = info->input_bfds; sub != (bfd *) NULL; sub = sub->link_next)
3409 if (bfd_get_flavour (abfd) == bfd_target_aout_flavour)
3411 trsize += exec_hdr (sub)->a_trsize;
3412 drsize += exec_hdr (sub)->a_drsize;
3416 /* FIXME: We need to identify the .text and .data sections
3417 and call get_reloc_upper_bound and canonicalize_reloc to
3418 work out the number of relocs needed, and then multiply
3419 by the reloc size. */
3423 exec_hdr (abfd)->a_trsize = trsize;
3424 exec_hdr (abfd)->a_drsize = drsize;
3427 exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd);
3429 /* Adjust the section sizes and vmas according to the magic number.
3430 This sets a_text, a_data and a_bss in the exec_hdr and sets the
3431 filepos for each section. */
3432 if (! NAME(aout,adjust_sizes_and_vmas) (abfd, &text_size, &text_end))
3435 /* The relocation and symbol file positions differ among a.out
3436 targets. We are passed a callback routine from the backend
3437 specific code to handle this.
3438 FIXME: At this point we do not know how much space the symbol
3439 table will require. This will not work for any (nonstandard)
3440 a.out target that needs to know the symbol table size before it
3441 can compute the relocation file positions. This may or may not
3442 be the case for the hp300hpux target, for example. */
3443 (*callback) (abfd, &aout_info.treloff, &aout_info.dreloff,
3445 obj_textsec (abfd)->rel_filepos = aout_info.treloff;
3446 obj_datasec (abfd)->rel_filepos = aout_info.dreloff;
3447 obj_sym_filepos (abfd) = aout_info.symoff;
3449 /* We keep a count of the symbols as we output them. */
3450 obj_aout_external_sym_count (abfd) = 0;
3452 /* We accumulate the string table as we write out the symbols. */
3453 stringtab_init (&aout_info.strtab);
3455 /* The most time efficient way to do the link would be to read all
3456 the input object files into memory and then sort out the
3457 information into the output file. Unfortunately, that will
3458 probably use too much memory. Another method would be to step
3459 through everything that composes the text section and write it
3460 out, and then everything that composes the data section and write
3461 it out, and then write out the relocs, and then write out the
3462 symbols. Unfortunately, that requires reading stuff from each
3463 input file several times, and we will not be able to keep all the
3464 input files open simultaneously, and reopening them will be slow.
3466 What we do is basically process one input file at a time. We do
3467 everything we need to do with an input file once--copy over the
3468 section contents, handle the relocation information, and write
3469 out the symbols--and then we throw away the information we read
3470 from it. This approach requires a lot of lseeks of the output
3471 file, which is unfortunate but still faster than reopening a lot
3474 We use the output_has_begun field of the input BFDs to see
3475 whether we have already handled it. */
3476 for (sub = info->input_bfds; sub != (bfd *) NULL; sub = sub->link_next)
3477 sub->output_has_begun = false;
3479 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3481 for (p = o->link_order_head;
3482 p != (struct bfd_link_order *) NULL;
3485 /* If we might be using the C based alloca function, we need
3486 to dump the memory allocated by aout_link_input_bfd. */
3492 if (p->type == bfd_indirect_link_order
3493 && (bfd_get_flavour (p->u.indirect.section->owner)
3494 == bfd_target_aout_flavour))
3498 input_bfd = p->u.indirect.section->owner;
3499 if (! input_bfd->output_has_begun)
3501 if (! aout_link_input_bfd (&aout_info, input_bfd))
3503 input_bfd->output_has_begun = true;
3508 if (! _bfd_default_link_order (abfd, info, o, p))
3514 /* Write out any symbols that we have not already written out. */
3515 aout_link_hash_traverse (aout_hash_table (info),
3516 aout_link_write_other_symbol,
3519 /* Update the header information. */
3520 abfd->symcount = obj_aout_external_sym_count (abfd);
3521 exec_hdr (abfd)->a_syms = abfd->symcount * EXTERNAL_NLIST_SIZE;
3522 obj_str_filepos (abfd) = obj_sym_filepos (abfd) + exec_hdr (abfd)->a_syms;
3523 obj_textsec (abfd)->reloc_count =
3524 exec_hdr (abfd)->a_trsize / obj_reloc_entry_size (abfd);
3525 obj_datasec (abfd)->reloc_count =
3526 exec_hdr (abfd)->a_drsize / obj_reloc_entry_size (abfd);
3528 /* Write out the string table. */
3529 if (bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET) != 0)
3531 emit_strtab (abfd, &aout_info.strtab);
3536 /* Link an a.out input BFD into the output file. */
3539 aout_link_input_bfd (finfo, input_bfd)
3540 struct aout_final_link_info *finfo;
3543 bfd_size_type sym_count;
3546 BFD_ASSERT (bfd_get_format (input_bfd) == bfd_object);
3548 /* Get the symbols. We probably have them already, unless
3549 finfo->info->keep_memory is false. */
3550 if (! aout_link_get_symbols (input_bfd))
3553 sym_count = obj_aout_external_sym_count (input_bfd);
3554 symbol_map = (int *) alloca ((size_t) sym_count * sizeof (int));
3556 /* Write out the symbols and get a map of the new indices. */
3557 if (! aout_link_write_symbols (finfo, input_bfd, symbol_map))
3560 /* Relocate and write out the sections. */
3561 if (! aout_link_input_section (finfo, input_bfd,
3562 obj_textsec (input_bfd),
3564 exec_hdr (input_bfd)->a_trsize,
3566 || ! aout_link_input_section (finfo, input_bfd,
3567 obj_datasec (input_bfd),
3569 exec_hdr (input_bfd)->a_drsize,
3573 /* If we are not keeping memory, we don't need the symbols any
3574 longer. We still need them if we are keeping memory, because the
3575 strings in the hash table point into them. */
3576 if (! finfo->info->keep_memory)
3578 if (! aout_link_free_symbols (input_bfd))
3585 /* Adjust and write out the symbols for an a.out file. Set the new
3586 symbol indices into a symbol_map. */
3589 aout_link_write_symbols (finfo, input_bfd, symbol_map)
3590 struct aout_final_link_info *finfo;
3595 bfd_size_type sym_count;
3597 enum bfd_link_strip strip;
3598 enum bfd_link_discard discard;
3599 struct external_nlist *output_syms;
3600 struct external_nlist *outsym;
3601 register struct external_nlist *sym;
3602 struct external_nlist *sym_end;
3603 struct aout_link_hash_entry **sym_hash;
3605 boolean skip_indirect;
3607 output_bfd = finfo->output_bfd;
3608 sym_count = obj_aout_external_sym_count (input_bfd);
3609 strings = obj_aout_external_strings (input_bfd);
3610 strip = finfo->info->strip;
3611 discard = finfo->info->discard;
3612 output_syms = ((struct external_nlist *)
3613 alloca ((size_t) (sym_count + 1) * EXTERNAL_NLIST_SIZE));
3614 outsym = output_syms;
3616 /* First write out a symbol for this object file, unless we are
3617 discarding such symbols. */
3618 if (strip != strip_all
3619 && (strip != strip_some
3620 || bfd_hash_lookup (finfo->info->keep_hash, input_bfd->filename,
3621 false, false) != NULL)
3622 && discard != discard_all)
3624 bfd_h_put_8 (output_bfd, N_TEXT, outsym->e_type);
3625 bfd_h_put_8 (output_bfd, 0, outsym->e_other);
3626 bfd_h_put_16 (output_bfd, (bfd_vma) 0, outsym->e_desc);
3627 PUT_WORD (output_bfd,
3628 add_to_stringtab (output_bfd, input_bfd->filename,
3631 PUT_WORD (output_bfd,
3632 (bfd_get_section_vma (output_bfd,
3633 obj_textsec (input_bfd)->output_section)
3634 + obj_textsec (input_bfd)->output_offset),
3636 ++obj_aout_external_sym_count (output_bfd);
3641 skip_indirect = false;
3642 sym = obj_aout_external_syms (input_bfd);
3643 sym_end = sym + sym_count;
3644 sym_hash = obj_aout_sym_hashes (input_bfd);
3645 for (; sym < sym_end; sym++, sym_hash++, symbol_map++)
3655 type = bfd_h_get_8 (input_bfd, sym->e_type);
3656 name = strings + GET_WORD (input_bfd, sym->e_strx);
3660 /* Pass this symbol through. It is the target of an
3661 indirect or warning symbol. */
3662 val = GET_WORD (input_bfd, sym->e_value);
3665 else if (skip_indirect)
3667 /* Skip this symbol, which is the target of an indirect
3668 symbol that we have changed to no longer be an indirect
3670 skip_indirect = false;
3675 struct aout_link_hash_entry *h;
3676 struct aout_link_hash_entry *hresolve;
3678 /* We have saved the hash table entry for this symbol, if
3679 there is one. Note that we could just look it up again
3680 in the hash table, provided we first check that it is an
3684 /* If this is an indirect or warning symbol, then change
3685 hresolve to the base symbol. We also change *sym_hash so
3686 that the relocation routines relocate against the real
3689 if (h != (struct aout_link_hash_entry *) NULL
3690 && (h->root.type == bfd_link_hash_indirect
3691 || h->root.type == bfd_link_hash_warning))
3693 hresolve = (struct aout_link_hash_entry *) h->root.u.i.link;
3694 while (hresolve->root.type == bfd_link_hash_indirect)
3695 hresolve = ((struct aout_link_hash_entry *)
3696 hresolve->root.u.i.link);
3697 *sym_hash = hresolve;
3700 /* If the symbol has already been written out, skip it. */
3701 if (h != (struct aout_link_hash_entry *) NULL
3704 *symbol_map = h->indx;
3708 /* See if we are stripping this symbol. */
3714 case strip_debugger:
3715 if ((type & N_STAB) != 0)
3719 if (bfd_hash_lookup (finfo->info->keep_hash, name, false, false)
3729 if (h != (struct aout_link_hash_entry *) NULL)
3730 h->root.written = true;
3734 /* Get the value of the symbol. */
3735 if ((type & N_TYPE) == N_TEXT)
3736 symsec = obj_textsec (input_bfd);
3737 else if ((type & N_TYPE) == N_DATA)
3738 symsec = obj_datasec (input_bfd);
3739 else if ((type & N_TYPE) == N_BSS)
3740 symsec = obj_bsssec (input_bfd);
3741 else if ((type & N_TYPE) == N_ABS)
3742 symsec = &bfd_abs_section;
3743 else if (((type & N_TYPE) == N_INDR
3744 && (hresolve == (struct aout_link_hash_entry *) NULL
3745 || (hresolve->root.type != bfd_link_hash_defined
3746 && hresolve->root.type != bfd_link_hash_common)))
3747 || type == N_WARNING)
3749 /* Pass the next symbol through unchanged. The
3750 condition above for indirect symbols is so that if
3751 the indirect symbol was defined, we output it with
3752 the correct definition so the debugger will
3755 val = GET_WORD (input_bfd, sym->e_value);
3758 else if ((type & N_STAB) != 0)
3760 val = GET_WORD (input_bfd, sym->e_value);
3765 /* If we get here with an indirect symbol, it means that
3766 we are outputting it with a real definition. In such
3767 a case we do not want to output the next symbol,
3768 which is the target of the indirection. */
3769 if ((type & N_TYPE) == N_INDR)
3770 skip_indirect = true;
3772 /* We need to get the value from the hash table. We use
3773 hresolve so that if we have defined an indirect
3774 symbol we output the final definition. */
3775 if (h == (struct aout_link_hash_entry *) NULL)
3777 else if (hresolve->root.type == bfd_link_hash_defined)
3779 asection *input_section;
3780 asection *output_section;
3782 /* This case means a common symbol which was turned
3783 into a defined symbol. */
3784 input_section = hresolve->root.u.def.section;
3785 output_section = input_section->output_section;
3786 BFD_ASSERT (output_section == &bfd_abs_section
3787 || output_section->owner == output_bfd);
3788 val = (hresolve->root.u.def.value
3789 + bfd_get_section_vma (output_bfd, output_section)
3790 + input_section->output_offset);
3792 /* Get the correct type based on the section. If
3793 this is a constructed set, force it to be
3794 globally visible. */
3803 if (output_section == obj_textsec (output_bfd))
3805 else if (output_section == obj_datasec (output_bfd))
3807 else if (output_section == obj_bsssec (output_bfd))
3812 else if (hresolve->root.type == bfd_link_hash_common)
3813 val = hresolve->root.u.c.size;
3819 if (symsec != (asection *) NULL)
3820 val = (symsec->output_section->vma
3821 + symsec->output_offset
3822 + (GET_WORD (input_bfd, sym->e_value)
3825 /* If this is a global symbol set the written flag, and if
3826 it is a local symbol see if we should discard it. */
3827 if (h != (struct aout_link_hash_entry *) NULL)
3829 h->root.written = true;
3830 h->indx = obj_aout_external_sym_count (output_bfd);
3839 if (*name == *finfo->info->lprefix
3840 && (finfo->info->lprefix_len == 1
3841 || strncmp (name, finfo->info->lprefix,
3842 finfo->info->lprefix_len) == 0))
3857 /* Copy this symbol into the list of symbols we are going to
3859 bfd_h_put_8 (output_bfd, type, outsym->e_type);
3860 bfd_h_put_8 (output_bfd, bfd_h_get_8 (input_bfd, sym->e_other),
3862 bfd_h_put_16 (output_bfd, bfd_h_get_16 (input_bfd, sym->e_desc),
3864 PUT_WORD (output_bfd,
3865 add_to_stringtab (output_bfd, name, &finfo->strtab),
3867 PUT_WORD (output_bfd, val, outsym->e_value);
3868 *symbol_map = obj_aout_external_sym_count (output_bfd);
3869 ++obj_aout_external_sym_count (output_bfd);
3873 /* Write out the output symbols we have just constructed. */
3874 if (outsym > output_syms)
3876 bfd_size_type outsym_count;
3878 if (bfd_seek (output_bfd, finfo->symoff, SEEK_SET) != 0)
3880 outsym_count = outsym - output_syms;
3881 if (bfd_write ((PTR) output_syms, (bfd_size_type) EXTERNAL_NLIST_SIZE,
3882 (bfd_size_type) outsym_count, output_bfd)
3883 != outsym_count * EXTERNAL_NLIST_SIZE)
3885 finfo->symoff += outsym_count * EXTERNAL_NLIST_SIZE;
3891 /* Write out a symbol that was not associated with an a.out input
3895 aout_link_write_other_symbol (h, data)
3896 struct aout_link_hash_entry *h;
3899 struct aout_final_link_info *finfo = (struct aout_final_link_info *) data;
3903 struct external_nlist outsym;
3905 if (h->root.written)
3908 h->root.written = true;
3910 if (finfo->info->strip == strip_all
3911 || (finfo->info->strip == strip_some
3912 && bfd_hash_lookup (finfo->info->keep_hash, h->root.root.string,
3913 false, false) == NULL))
3916 output_bfd = finfo->output_bfd;
3918 switch (h->root.type)
3921 case bfd_link_hash_new:
3923 /* Avoid variable not initialized warnings. */
3925 case bfd_link_hash_undefined:
3926 type = N_UNDF | N_EXT;
3929 case bfd_link_hash_defined:
3933 sec = h->root.u.def.section;
3934 BFD_ASSERT (sec == &bfd_abs_section
3935 || sec->owner == output_bfd);
3936 if (sec == obj_textsec (output_bfd))
3937 type = N_TEXT | N_EXT;
3938 else if (sec == obj_datasec (output_bfd))
3939 type = N_DATA | N_EXT;
3940 else if (sec == obj_bsssec (output_bfd))
3941 type = N_BSS | N_EXT;
3943 type = N_ABS | N_EXT;
3944 val = (h->root.u.def.value
3945 + sec->output_section->vma
3946 + sec->output_offset);
3949 case bfd_link_hash_common:
3950 type = N_UNDF | N_EXT;
3951 val = h->root.u.c.size;
3953 case bfd_link_hash_indirect:
3954 case bfd_link_hash_warning:
3955 /* FIXME: Ignore these for now. The circumstances under which
3956 they should be written out are not clear to me. */
3960 bfd_h_put_8 (output_bfd, type, outsym.e_type);
3961 bfd_h_put_8 (output_bfd, 0, outsym.e_other);
3962 bfd_h_put_16 (output_bfd, 0, outsym.e_desc);
3963 PUT_WORD (output_bfd,
3964 add_to_stringtab (output_bfd, h->root.root.string, &finfo->strtab),
3966 PUT_WORD (output_bfd, val, outsym.e_value);
3968 if (bfd_seek (output_bfd, finfo->symoff, SEEK_SET) != 0
3969 || bfd_write ((PTR) &outsym, (bfd_size_type) EXTERNAL_NLIST_SIZE,
3970 (bfd_size_type) 1, output_bfd) != EXTERNAL_NLIST_SIZE)
3972 /* FIXME: No way to handle errors. */
3976 finfo->symoff += EXTERNAL_NLIST_SIZE;
3977 h->indx = obj_aout_external_sym_count (output_bfd);
3978 ++obj_aout_external_sym_count (output_bfd);
3983 /* Link an a.out section into the output file. */
3986 aout_link_input_section (finfo, input_bfd, input_section, reloff_ptr,
3987 rel_size, symbol_map)
3988 struct aout_final_link_info *finfo;
3990 asection *input_section;
3991 file_ptr *reloff_ptr;
3992 bfd_size_type rel_size;
3995 bfd_size_type input_size;
3999 /* Get the section contents. */
4000 input_size = bfd_section_size (input_bfd, input_section);
4001 contents = (bfd_byte *) alloca (input_size);
4002 if (! bfd_get_section_contents (input_bfd, input_section, (PTR) contents,
4003 (file_ptr) 0, input_size))
4006 /* Read in the relocs. */
4007 relocs = (PTR) alloca (rel_size);
4008 if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0
4009 || bfd_read (relocs, 1, rel_size, input_bfd) != rel_size)
4012 /* Relocate the section contents. */
4013 if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE)
4015 if (! aout_link_input_section_std (finfo, input_bfd, input_section,
4016 (struct reloc_std_external *) relocs,
4017 rel_size, contents, symbol_map))
4022 if (! aout_link_input_section_ext (finfo, input_bfd, input_section,
4023 (struct reloc_ext_external *) relocs,
4024 rel_size, contents, symbol_map))
4028 /* Write out the section contents. */
4029 if (! bfd_set_section_contents (finfo->output_bfd,
4030 input_section->output_section,
4032 input_section->output_offset,
4036 /* If we are producing relocateable output, the relocs were
4037 modified, and we now write them out. */
4038 if (finfo->info->relocateable)
4040 if (bfd_seek (finfo->output_bfd, *reloff_ptr, SEEK_SET) != 0)
4042 if (bfd_write (relocs, (bfd_size_type) 1, rel_size, finfo->output_bfd)
4045 *reloff_ptr += rel_size;
4047 /* Assert that the relocs have not run into the symbols, and
4048 that if these are the text relocs they have not run into the
4050 BFD_ASSERT (*reloff_ptr <= obj_sym_filepos (finfo->output_bfd)
4051 && (reloff_ptr != &finfo->treloff
4053 <= obj_datasec (finfo->output_bfd)->rel_filepos)));
4059 /* Get the section corresponding to a reloc index. */
4061 static INLINE asection *
4062 aout_reloc_index_to_section (abfd, indx)
4066 switch (indx & N_TYPE)
4069 return obj_textsec (abfd);
4071 return obj_datasec (abfd);
4073 return obj_bsssec (abfd);
4076 return &bfd_abs_section;
4082 /* Relocate an a.out section using standard a.out relocs. */
4085 aout_link_input_section_std (finfo, input_bfd, input_section, relocs,
4086 rel_size, contents, symbol_map)
4087 struct aout_final_link_info *finfo;
4089 asection *input_section;
4090 struct reloc_std_external *relocs;
4091 bfd_size_type rel_size;
4096 boolean relocateable;
4097 struct external_nlist *syms;
4099 struct aout_link_hash_entry **sym_hashes;
4100 bfd_size_type reloc_count;
4101 register struct reloc_std_external *rel;
4102 struct reloc_std_external *rel_end;
4104 output_bfd = finfo->output_bfd;
4106 BFD_ASSERT (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE);
4107 BFD_ASSERT (input_bfd->xvec->header_byteorder_big_p
4108 == output_bfd->xvec->header_byteorder_big_p);
4110 relocateable = finfo->info->relocateable;
4111 syms = obj_aout_external_syms (input_bfd);
4112 strings = obj_aout_external_strings (input_bfd);
4113 sym_hashes = obj_aout_sym_hashes (input_bfd);
4115 reloc_count = rel_size / RELOC_STD_SIZE;
4117 rel_end = rel + reloc_count;
4118 for (; rel < rel_end; rel++)
4130 bfd_reloc_status_type r;
4132 r_addr = GET_SWORD (input_bfd, rel->r_address);
4134 if (input_bfd->xvec->header_byteorder_big_p)
4136 r_index = ((rel->r_index[0] << 16)
4137 | (rel->r_index[1] << 8)
4139 r_extern = (0 != (rel->r_type[0] & RELOC_STD_BITS_EXTERN_BIG));
4140 r_pcrel = (0 != (rel->r_type[0] & RELOC_STD_BITS_PCREL_BIG));
4141 r_baserel = (0 != (rel->r_type[0] & RELOC_STD_BITS_BASEREL_BIG));
4142 r_jmptable= (0 != (rel->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG));
4143 r_relative= (0 != (rel->r_type[0] & RELOC_STD_BITS_RELATIVE_BIG));
4144 r_length = ((rel->r_type[0] & RELOC_STD_BITS_LENGTH_BIG)
4145 >> RELOC_STD_BITS_LENGTH_SH_BIG);
4149 r_index = ((rel->r_index[2] << 16)
4150 | (rel->r_index[1] << 8)
4152 r_extern = (0 != (rel->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE));
4153 r_pcrel = (0 != (rel->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
4154 r_baserel = (0 != (rel->r_type[0] & RELOC_STD_BITS_BASEREL_LITTLE));
4155 r_jmptable= (0 != (rel->r_type[0] & RELOC_STD_BITS_JMPTABLE_LITTLE));
4156 r_relative= (0 != (rel->r_type[0] & RELOC_STD_BITS_RELATIVE_LITTLE));
4157 r_length = ((rel->r_type[0] & RELOC_STD_BITS_LENGTH_LITTLE)
4158 >> RELOC_STD_BITS_LENGTH_SH_LITTLE);
4161 howto_idx = r_length + 4 * r_pcrel + 8 * r_baserel;
4162 BFD_ASSERT (howto_idx < TABLE_SIZE (howto_table_std));
4163 BFD_ASSERT (r_jmptable == 0);
4164 BFD_ASSERT (r_relative == 0);
4168 /* We are generating a relocateable output file, and must
4169 modify the reloc accordingly. */
4172 struct aout_link_hash_entry *h;
4174 /* If we know the symbol this relocation is against,
4175 convert it into a relocation against a section. This
4176 is what the native linker does. */
4177 h = sym_hashes[r_index];
4178 if (h != (struct aout_link_hash_entry *) NULL
4179 && h->root.type == bfd_link_hash_defined)
4181 asection *output_section;
4183 /* Change the r_extern value. */
4184 if (output_bfd->xvec->header_byteorder_big_p)
4185 rel->r_type[0] &=~ RELOC_STD_BITS_EXTERN_BIG;
4187 rel->r_type[0] &=~ RELOC_STD_BITS_EXTERN_LITTLE;
4189 /* Compute a new r_index. */
4190 output_section = h->root.u.def.section->output_section;
4191 if (output_section == obj_textsec (output_bfd))
4193 else if (output_section == obj_datasec (output_bfd))
4195 else if (output_section == obj_bsssec (output_bfd))
4200 /* Add the symbol value and the section VMA to the
4201 addend stored in the contents. */
4202 relocation = (h->root.u.def.value
4203 + output_section->vma
4204 + h->root.u.def.section->output_offset);
4208 /* We must change r_index according to the symbol
4210 r_index = symbol_map[r_index];
4216 name = strings + GET_WORD (input_bfd,
4217 syms[r_index].e_strx);
4218 if (! ((*finfo->info->callbacks->unattached_reloc)
4219 (finfo->info, name, input_bfd, input_section,
4228 /* Write out the new r_index value. */
4229 if (output_bfd->xvec->header_byteorder_big_p)
4231 rel->r_index[0] = r_index >> 16;
4232 rel->r_index[1] = r_index >> 8;
4233 rel->r_index[2] = r_index;
4237 rel->r_index[2] = r_index >> 16;
4238 rel->r_index[1] = r_index >> 8;
4239 rel->r_index[0] = r_index;
4246 /* This is a relocation against a section. We must
4247 adjust by the amount that the section moved. */
4248 section = aout_reloc_index_to_section (input_bfd, r_index);
4249 relocation = (section->output_section->vma
4250 + section->output_offset
4254 /* Change the address of the relocation. */
4255 PUT_WORD (output_bfd,
4256 r_addr + input_section->output_offset,
4259 /* Adjust a PC relative relocation by removing the reference
4260 to the original address in the section and including the
4261 reference to the new address. */
4263 relocation -= (input_section->output_section->vma
4264 + input_section->output_offset
4265 - input_section->vma);
4267 if (relocation == 0)
4270 r = _bfd_relocate_contents (howto_table_std + howto_idx,
4271 input_bfd, relocation,
4276 /* We are generating an executable, and must do a full
4280 struct aout_link_hash_entry *h;
4282 h = sym_hashes[r_index];
4283 if (h != (struct aout_link_hash_entry *) NULL
4284 && h->root.type == bfd_link_hash_defined)
4286 relocation = (h->root.u.def.value
4287 + h->root.u.def.section->output_section->vma
4288 + h->root.u.def.section->output_offset);
4294 name = strings + GET_WORD (input_bfd, syms[r_index].e_strx);
4295 if (! ((*finfo->info->callbacks->undefined_symbol)
4296 (finfo->info, name, input_bfd, input_section,
4306 section = aout_reloc_index_to_section (input_bfd, r_index);
4307 relocation = (section->output_section->vma
4308 + section->output_offset
4311 relocation += input_section->vma;
4314 r = _bfd_final_link_relocate (howto_table_std + howto_idx,
4315 input_bfd, input_section,
4316 contents, r_addr, relocation,
4320 if (r != bfd_reloc_ok)
4325 case bfd_reloc_outofrange:
4327 case bfd_reloc_overflow:
4332 name = strings + GET_WORD (input_bfd,
4333 syms[r_index].e_strx);
4338 s = aout_reloc_index_to_section (input_bfd, r_index);
4339 name = bfd_section_name (input_bfd, s);
4341 if (! ((*finfo->info->callbacks->reloc_overflow)
4342 (finfo->info, name, howto_table_std[howto_idx].name,
4343 (bfd_vma) 0, input_bfd, input_section, r_addr)))
4354 /* Relocate an a.out section using extended a.out relocs. */
4357 aout_link_input_section_ext (finfo, input_bfd, input_section, relocs,
4358 rel_size, contents, symbol_map)
4359 struct aout_final_link_info *finfo;
4361 asection *input_section;
4362 struct reloc_ext_external *relocs;
4363 bfd_size_type rel_size;
4368 boolean relocateable;
4369 struct external_nlist *syms;
4371 struct aout_link_hash_entry **sym_hashes;
4372 bfd_size_type reloc_count;
4373 register struct reloc_ext_external *rel;
4374 struct reloc_ext_external *rel_end;
4376 output_bfd = finfo->output_bfd;
4378 BFD_ASSERT (obj_reloc_entry_size (input_bfd) == RELOC_EXT_SIZE);
4379 BFD_ASSERT (input_bfd->xvec->header_byteorder_big_p
4380 == output_bfd->xvec->header_byteorder_big_p);
4382 relocateable = finfo->info->relocateable;
4383 syms = obj_aout_external_syms (input_bfd);
4384 strings = obj_aout_external_strings (input_bfd);
4385 sym_hashes = obj_aout_sym_hashes (input_bfd);
4387 reloc_count = rel_size / RELOC_EXT_SIZE;
4389 rel_end = rel + reloc_count;
4390 for (; rel < rel_end; rel++)
4399 r_addr = GET_SWORD (input_bfd, rel->r_address);
4401 if (input_bfd->xvec->header_byteorder_big_p)
4403 r_index = ((rel->r_index[0] << 16)
4404 | (rel->r_index[1] << 8)
4406 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG));
4407 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
4408 >> RELOC_EXT_BITS_TYPE_SH_BIG);
4412 r_index = ((rel->r_index[2] << 16)
4413 | (rel->r_index[1] << 8)
4415 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
4416 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
4417 >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
4420 r_addend = GET_SWORD (input_bfd, rel->r_addend);
4422 BFD_ASSERT (r_type >= 0
4423 && r_type < TABLE_SIZE (howto_table_ext));
4427 /* We are generating a relocateable output file, and must
4428 modify the reloc accordingly. */
4431 struct aout_link_hash_entry *h;
4433 /* If we know the symbol this relocation is against,
4434 convert it into a relocation against a section. This
4435 is what the native linker does. */
4436 h = sym_hashes[r_index];
4437 if (h != (struct aout_link_hash_entry *) NULL
4438 && h->root.type == bfd_link_hash_defined)
4440 asection *output_section;
4442 /* Change the r_extern value. */
4443 if (output_bfd->xvec->header_byteorder_big_p)
4444 rel->r_type[0] &=~ RELOC_EXT_BITS_EXTERN_BIG;
4446 rel->r_type[0] &=~ RELOC_EXT_BITS_EXTERN_LITTLE;
4448 /* Compute a new r_index. */
4449 output_section = h->root.u.def.section->output_section;
4450 if (output_section == obj_textsec (output_bfd))
4452 else if (output_section == obj_datasec (output_bfd))
4454 else if (output_section == obj_bsssec (output_bfd))
4459 /* Add the symbol value and the section VMA to the
4461 relocation = (h->root.u.def.value
4462 + output_section->vma
4463 + h->root.u.def.section->output_offset);
4465 /* Now RELOCATION is the VMA of the final
4466 destination. If this is a PC relative reloc,
4467 then ADDEND is the negative of the source VMA.
4468 We want to set ADDEND to the difference between
4469 the destination VMA and the source VMA, which
4470 means we must adjust RELOCATION by the change in
4471 the source VMA. This is done below. */
4475 /* We must change r_index according to the symbol
4477 r_index = symbol_map[r_index];
4484 + GET_WORD (input_bfd, syms[r_index].e_strx));
4485 if (! ((*finfo->info->callbacks->unattached_reloc)
4486 (finfo->info, name, input_bfd, input_section,
4494 /* If this is a PC relative reloc, then the addend
4495 is the negative of the source VMA. We must
4496 adjust it by the change in the source VMA. This
4500 /* Write out the new r_index value. */
4501 if (output_bfd->xvec->header_byteorder_big_p)
4503 rel->r_index[0] = r_index >> 16;
4504 rel->r_index[1] = r_index >> 8;
4505 rel->r_index[2] = r_index;
4509 rel->r_index[2] = r_index >> 16;
4510 rel->r_index[1] = r_index >> 8;
4511 rel->r_index[0] = r_index;
4518 /* This is a relocation against a section. We must
4519 adjust by the amount that the section moved. */
4520 section = aout_reloc_index_to_section (input_bfd, r_index);
4521 relocation = (section->output_section->vma
4522 + section->output_offset
4525 /* If this is a PC relative reloc, then the addend is
4526 the difference in VMA between the destination and the
4527 source. We have just adjusted for the change in VMA
4528 of the destination, so we must also adjust by the
4529 change in VMA of the source. This is done below. */
4532 /* As described above, we must always adjust a PC relative
4533 reloc by the change in VMA of the source. */
4534 if (howto_table_ext[r_type].pc_relative)
4535 relocation -= (input_section->output_section->vma
4536 + input_section->output_offset
4537 - input_section->vma);
4539 /* Change the addend if necessary. */
4540 if (relocation != 0)
4541 PUT_WORD (output_bfd, r_addend + relocation, rel->r_addend);
4543 /* Change the address of the relocation. */
4544 PUT_WORD (output_bfd,
4545 r_addr + input_section->output_offset,
4550 bfd_reloc_status_type r;
4552 /* We are generating an executable, and must do a full
4556 struct aout_link_hash_entry *h;
4558 h = sym_hashes[r_index];
4559 if (h != (struct aout_link_hash_entry *) NULL
4560 && h->root.type == bfd_link_hash_defined)
4562 relocation = (h->root.u.def.value
4563 + h->root.u.def.section->output_section->vma
4564 + h->root.u.def.section->output_offset);
4570 name = strings + GET_WORD (input_bfd, syms[r_index].e_strx);
4571 if (! ((*finfo->info->callbacks->undefined_symbol)
4572 (finfo->info, name, input_bfd, input_section,
4582 section = aout_reloc_index_to_section (input_bfd, r_index);
4584 /* If this is a PC relative reloc, then R_ADDEND is the
4585 difference between the two vmas, or
4586 old_dest_sec + old_dest_off - (old_src_sec + old_src_off)
4588 old_dest_sec == section->vma
4590 old_src_sec == input_section->vma
4592 old_src_off == r_addr
4594 _bfd_final_link_relocate expects RELOCATION +
4595 R_ADDEND to be the VMA of the destination minus
4596 r_addr (the minus r_addr is because this relocation
4597 is not pcrel_offset, which is a bit confusing and
4598 should, perhaps, be changed), or
4601 new_dest_sec == output_section->vma + output_offset
4602 We arrange for this to happen by setting RELOCATION to
4603 new_dest_sec + old_src_sec - old_dest_sec
4605 If this is not a PC relative reloc, then R_ADDEND is
4606 simply the VMA of the destination, so we set
4607 RELOCATION to the change in the destination VMA, or
4608 new_dest_sec - old_dest_sec
4610 relocation = (section->output_section->vma
4611 + section->output_offset
4613 if (howto_table_ext[r_type].pc_relative)
4614 relocation += input_section->vma;
4617 r = _bfd_final_link_relocate (howto_table_ext + r_type,
4618 input_bfd, input_section,
4619 contents, r_addr, relocation,
4621 if (r != bfd_reloc_ok)
4626 case bfd_reloc_outofrange:
4628 case bfd_reloc_overflow:
4633 name = strings + GET_WORD (input_bfd,
4634 syms[r_index].e_strx);
4639 s = aout_reloc_index_to_section (input_bfd, r_index);
4640 name = bfd_section_name (input_bfd, s);
4642 if (! ((*finfo->info->callbacks->reloc_overflow)
4643 (finfo->info, name, howto_table_ext[r_type].name,
4644 r_addend, input_bfd, input_section, r_addr)))