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
124 #include <string.h> /* For strchr and friends */
131 #include "aout/aout64.h"
132 #include "aout/stab_gnu.h"
135 static boolean aout_get_external_symbols PARAMS ((bfd *));
136 static boolean translate_from_native_sym_flags
137 PARAMS ((bfd *, aout_symbol_type *));
138 static boolean translate_to_native_sym_flags
139 PARAMS ((bfd *, asymbol *, struct external_nlist *));
146 The file @file{aoutx.h} provides for both the @emph{standard}
147 and @emph{extended} forms of a.out relocation records.
149 The standard records contain only an
150 address, a symbol index, and a type field. The extended records
151 (used on 29ks and sparcs) also have a full integer for an
155 #define CTOR_TABLE_RELOC_IDX 2
157 #define howto_table_ext NAME(aout,ext_howto_table)
158 #define howto_table_std NAME(aout,std_howto_table)
160 reloc_howto_type howto_table_ext[] =
162 /* type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone */
163 HOWTO(RELOC_8, 0, 0, 8, false, 0, complain_overflow_bitfield,0,"8", false, 0,0x000000ff, false),
164 HOWTO(RELOC_16, 0, 1, 16, false, 0, complain_overflow_bitfield,0,"16", false, 0,0x0000ffff, false),
165 HOWTO(RELOC_32, 0, 2, 32, false, 0, complain_overflow_bitfield,0,"32", false, 0,0xffffffff, false),
166 HOWTO(RELOC_DISP8, 0, 0, 8, true, 0, complain_overflow_signed,0,"DISP8", false, 0,0x000000ff, false),
167 HOWTO(RELOC_DISP16, 0, 1, 16, true, 0, complain_overflow_signed,0,"DISP16", false, 0,0x0000ffff, false),
168 HOWTO(RELOC_DISP32, 0, 2, 32, true, 0, complain_overflow_signed,0,"DISP32", false, 0,0xffffffff, false),
169 HOWTO(RELOC_WDISP30,2, 2, 30, true, 0, complain_overflow_signed,0,"WDISP30", false, 0,0x3fffffff, false),
170 HOWTO(RELOC_WDISP22,2, 2, 22, true, 0, complain_overflow_signed,0,"WDISP22", false, 0,0x003fffff, false),
171 HOWTO(RELOC_HI22, 10, 2, 22, false, 0, complain_overflow_bitfield,0,"HI22", false, 0,0x003fffff, false),
172 HOWTO(RELOC_22, 0, 2, 22, false, 0, complain_overflow_bitfield,0,"22", false, 0,0x003fffff, false),
173 HOWTO(RELOC_13, 0, 2, 13, false, 0, complain_overflow_bitfield,0,"13", false, 0,0x00001fff, false),
174 HOWTO(RELOC_LO10, 0, 2, 10, false, 0, complain_overflow_dont,0,"LO10", false, 0,0x000003ff, false),
175 HOWTO(RELOC_SFA_BASE,0, 2, 32, false, 0, complain_overflow_bitfield,0,"SFA_BASE", false, 0,0xffffffff, false),
176 HOWTO(RELOC_SFA_OFF13,0,2, 32, false, 0, complain_overflow_bitfield,0,"SFA_OFF13",false, 0,0xffffffff, false),
177 HOWTO(RELOC_BASE10, 0, 2, 16, false, 0, complain_overflow_bitfield,0,"BASE10", false, 0,0x0000ffff, false),
178 HOWTO(RELOC_BASE13, 0, 2, 13, false, 0, complain_overflow_bitfield,0,"BASE13", false, 0,0x00001fff, false),
179 HOWTO(RELOC_BASE22, 0, 2, 0, false, 0, complain_overflow_bitfield,0,"BASE22", false, 0,0x00000000, false),
180 HOWTO(RELOC_PC10, 0, 2, 10, false, 0, complain_overflow_bitfield,0,"PC10", false, 0,0x000003ff, false),
181 HOWTO(RELOC_PC22, 0, 2, 22, false, 0, complain_overflow_bitfield,0,"PC22", false, 0,0x003fffff, false),
182 HOWTO(RELOC_JMP_TBL,0, 2, 32, false, 0, complain_overflow_bitfield,0,"JMP_TBL", false, 0,0xffffffff, false),
183 HOWTO(RELOC_SEGOFF16,0, 2, 0, false, 0, complain_overflow_bitfield,0,"SEGOFF16", false, 0,0x00000000, false),
184 HOWTO(RELOC_GLOB_DAT,0, 2, 0, false, 0, complain_overflow_bitfield,0,"GLOB_DAT", false, 0,0x00000000, false),
185 HOWTO(RELOC_JMP_SLOT,0, 2, 0, false, 0, complain_overflow_bitfield,0,"JMP_SLOT", false, 0,0x00000000, false),
186 HOWTO(RELOC_RELATIVE,0, 2, 0, false, 0, complain_overflow_bitfield,0,"RELATIVE", false, 0,0x00000000, false),
189 /* Convert standard reloc records to "arelent" format (incl byte swap). */
191 reloc_howto_type howto_table_std[] = {
192 /* type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone */
193 HOWTO( 0, 0, 0, 8, false, 0, complain_overflow_bitfield,0,"8", true, 0x000000ff,0x000000ff, false),
194 HOWTO( 1, 0, 1, 16, false, 0, complain_overflow_bitfield,0,"16", true, 0x0000ffff,0x0000ffff, false),
195 HOWTO( 2, 0, 2, 32, false, 0, complain_overflow_bitfield,0,"32", true, 0xffffffff,0xffffffff, false),
196 HOWTO( 3, 0, 4, 64, false, 0, complain_overflow_bitfield,0,"64", true, 0xdeaddead,0xdeaddead, false),
197 HOWTO( 4, 0, 0, 8, true, 0, complain_overflow_signed, 0,"DISP8", true, 0x000000ff,0x000000ff, false),
198 HOWTO( 5, 0, 1, 16, true, 0, complain_overflow_signed, 0,"DISP16", true, 0x0000ffff,0x0000ffff, false),
199 HOWTO( 6, 0, 2, 32, true, 0, complain_overflow_signed, 0,"DISP32", true, 0xffffffff,0xffffffff, false),
200 HOWTO( 7, 0, 4, 64, true, 0, complain_overflow_signed, 0,"DISP64", true, 0xfeedface,0xfeedface, false),
202 HOWTO( 9, 0, 1, 16, false, 0, complain_overflow_bitfield,0,"BASE16", false,0xffffffff,0xffffffff, false),
203 HOWTO(10, 0, 2, 32, false, 0, complain_overflow_bitfield,0,"BASE32", false,0xffffffff,0xffffffff, false),
209 HOWTO(16, 0, 2, 0, false, 0, complain_overflow_bitfield,0,"JMP_TABLE", false, 0,0x00000000, false),
217 { -1 }, { -1 }, { -1 }, { -1 }, { -1 }, { -1 }, { -1 }, { -1 },
218 HOWTO(32, 0, 2, 0, false, 0, complain_overflow_bitfield,0,"RELATIVE", false, 0,0x00000000, false),
226 HOWTO(40, 0, 2, 0, false, 0, complain_overflow_bitfield,0,"BASEREL", false, 0,0x00000000, false),
229 #define TABLE_SIZE(TABLE) (sizeof(TABLE)/sizeof(TABLE[0]))
231 CONST struct reloc_howto_struct *
232 NAME(aout,reloc_type_lookup) (abfd,code)
234 bfd_reloc_code_real_type code;
236 #define EXT(i,j) case i: return &howto_table_ext[j]
237 #define STD(i,j) case i: return &howto_table_std[j]
238 int ext = obj_reloc_entry_size (abfd) == RELOC_EXT_SIZE;
239 if (code == BFD_RELOC_CTOR)
240 switch (bfd_get_arch_info (abfd)->bits_per_address)
252 EXT (BFD_RELOC_32, 2);
253 EXT (BFD_RELOC_HI22, 8);
254 EXT (BFD_RELOC_LO10, 11);
255 EXT (BFD_RELOC_32_PCREL_S2, 6);
256 EXT (BFD_RELOC_SPARC_WDISP22, 7);
257 EXT (BFD_RELOC_SPARC13, 10);
258 EXT (BFD_RELOC_SPARC_BASE13, 15);
259 default: return (CONST struct reloc_howto_struct *) 0;
265 STD (BFD_RELOC_16, 1);
266 STD (BFD_RELOC_32, 2);
267 STD (BFD_RELOC_8_PCREL, 4);
268 STD (BFD_RELOC_16_PCREL, 5);
269 STD (BFD_RELOC_32_PCREL, 6);
270 STD (BFD_RELOC_16_BASEREL, 9);
271 STD (BFD_RELOC_32_BASEREL, 10);
272 default: return (CONST struct reloc_howto_struct *) 0;
278 Internal entry points
281 @file{aoutx.h} exports several routines for accessing the
282 contents of an a.out file, which are gathered and exported in
283 turn by various format specific files (eg sunos.c).
289 aout_@var{size}_swap_exec_header_in
292 void aout_@var{size}_swap_exec_header_in,
294 struct external_exec *raw_bytes,
295 struct internal_exec *execp);
298 Swap the information in an executable header @var{raw_bytes} taken
299 from a raw byte stream memory image into the internal exec header
300 structure @var{execp}.
303 #ifndef NAME_swap_exec_header_in
305 NAME(aout,swap_exec_header_in) (abfd, raw_bytes, execp)
307 struct external_exec *raw_bytes;
308 struct internal_exec *execp;
310 struct external_exec *bytes = (struct external_exec *)raw_bytes;
312 /* The internal_exec structure has some fields that are unused in this
313 configuration (IE for i960), so ensure that all such uninitialized
314 fields are zero'd out. There are places where two of these structs
315 are memcmp'd, and thus the contents do matter. */
316 memset ((PTR) execp, 0, sizeof (struct internal_exec));
317 /* Now fill in fields in the execp, from the bytes in the raw data. */
318 execp->a_info = bfd_h_get_32 (abfd, bytes->e_info);
319 execp->a_text = GET_WORD (abfd, bytes->e_text);
320 execp->a_data = GET_WORD (abfd, bytes->e_data);
321 execp->a_bss = GET_WORD (abfd, bytes->e_bss);
322 execp->a_syms = GET_WORD (abfd, bytes->e_syms);
323 execp->a_entry = GET_WORD (abfd, bytes->e_entry);
324 execp->a_trsize = GET_WORD (abfd, bytes->e_trsize);
325 execp->a_drsize = GET_WORD (abfd, bytes->e_drsize);
327 #define NAME_swap_exec_header_in NAME(aout,swap_exec_header_in)
332 aout_@var{size}_swap_exec_header_out
335 void aout_@var{size}_swap_exec_header_out
337 struct internal_exec *execp,
338 struct external_exec *raw_bytes);
341 Swap the information in an internal exec header structure
342 @var{execp} into the buffer @var{raw_bytes} ready for writing to disk.
345 NAME(aout,swap_exec_header_out) (abfd, execp, raw_bytes)
347 struct internal_exec *execp;
348 struct external_exec *raw_bytes;
350 struct external_exec *bytes = (struct external_exec *)raw_bytes;
352 /* Now fill in fields in the raw data, from the fields in the exec struct. */
353 bfd_h_put_32 (abfd, execp->a_info , bytes->e_info);
354 PUT_WORD (abfd, execp->a_text , bytes->e_text);
355 PUT_WORD (abfd, execp->a_data , bytes->e_data);
356 PUT_WORD (abfd, execp->a_bss , bytes->e_bss);
357 PUT_WORD (abfd, execp->a_syms , bytes->e_syms);
358 PUT_WORD (abfd, execp->a_entry , bytes->e_entry);
359 PUT_WORD (abfd, execp->a_trsize, bytes->e_trsize);
360 PUT_WORD (abfd, execp->a_drsize, bytes->e_drsize);
363 /* Make all the section for an a.out file. */
366 NAME(aout,make_sections) (abfd)
369 if (obj_textsec (abfd) == (asection *) NULL
370 && bfd_make_section (abfd, ".text") == (asection *) NULL)
372 if (obj_datasec (abfd) == (asection *) NULL
373 && bfd_make_section (abfd, ".data") == (asection *) NULL)
375 if (obj_bsssec (abfd) == (asection *) NULL
376 && bfd_make_section (abfd, ".bss") == (asection *) NULL)
383 aout_@var{size}_some_aout_object_p
386 bfd_target *aout_@var{size}_some_aout_object_p
388 bfd_target *(*callback_to_real_object_p)());
391 Some a.out variant thinks that the file open in @var{abfd}
392 checking is an a.out file. Do some more checking, and set up
393 for access if it really is. Call back to the calling
394 environment's "finish up" function just before returning, to
395 handle any last-minute setup.
399 NAME(aout,some_aout_object_p) (abfd, execp, callback_to_real_object_p)
401 struct internal_exec *execp;
402 bfd_target *(*callback_to_real_object_p) PARAMS ((bfd *));
404 struct aout_data_struct *rawptr, *oldrawptr;
407 rawptr = (struct aout_data_struct *) bfd_zalloc (abfd, sizeof (struct aout_data_struct ));
408 if (rawptr == NULL) {
409 bfd_set_error (bfd_error_no_memory);
413 oldrawptr = abfd->tdata.aout_data;
414 abfd->tdata.aout_data = rawptr;
416 /* Copy the contents of the old tdata struct.
417 In particular, we want the subformat, since for hpux it was set in
418 hp300hpux.c:swap_exec_header_in and will be used in
419 hp300hpux.c:callback. */
420 if (oldrawptr != NULL)
421 *abfd->tdata.aout_data = *oldrawptr;
423 abfd->tdata.aout_data->a.hdr = &rawptr->e;
424 *(abfd->tdata.aout_data->a.hdr) = *execp; /* Copy in the internal_exec struct */
425 execp = abfd->tdata.aout_data->a.hdr;
427 /* Set the file flags */
428 abfd->flags = NO_FLAGS;
429 if (execp->a_drsize || execp->a_trsize)
430 abfd->flags |= HAS_RELOC;
431 /* Setting of EXEC_P has been deferred to the bottom of this function */
433 abfd->flags |= HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS;
434 if (N_DYNAMIC(*execp))
435 abfd->flags |= DYNAMIC;
437 if (N_MAGIC (*execp) == ZMAGIC)
439 abfd->flags |= D_PAGED|WP_TEXT;
440 adata(abfd).magic = z_magic;
442 else if (N_MAGIC (*execp) == NMAGIC)
444 abfd->flags |= WP_TEXT;
445 adata(abfd).magic = n_magic;
448 adata(abfd).magic = o_magic;
450 bfd_get_start_address (abfd) = execp->a_entry;
452 obj_aout_symbols (abfd) = (aout_symbol_type *)NULL;
453 bfd_get_symcount (abfd) = execp->a_syms / sizeof (struct external_nlist);
455 /* The default relocation entry size is that of traditional V7 Unix. */
456 obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
458 /* The default symbol entry size is that of traditional Unix. */
459 obj_symbol_entry_size (abfd) = EXTERNAL_NLIST_SIZE;
461 obj_aout_external_syms (abfd) = NULL;
462 obj_aout_external_strings (abfd) = NULL;
463 obj_aout_sym_hashes (abfd) = NULL;
465 if (! NAME(aout,make_sections) (abfd))
468 obj_datasec (abfd)->_raw_size = execp->a_data;
469 obj_bsssec (abfd)->_raw_size = execp->a_bss;
471 /* If this object is dynamically linked, we assume that both
472 sections have relocs. This does no real harm, even though it may
474 obj_textsec (abfd)->flags =
475 (execp->a_trsize != 0 || (abfd->flags & DYNAMIC) != 0
476 ? (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_RELOC)
477 : (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS));
478 obj_datasec (abfd)->flags =
479 (execp->a_drsize != 0 || (abfd->flags & DYNAMIC) != 0
480 ? (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS | SEC_RELOC)
481 : (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS));
482 obj_bsssec (abfd)->flags = SEC_ALLOC;
484 #ifdef THIS_IS_ONLY_DOCUMENTATION
485 /* The common code can't fill in these things because they depend
486 on either the start address of the text segment, the rounding
487 up of virtual addresses between segments, or the starting file
488 position of the text segment -- all of which varies among different
489 versions of a.out. */
491 /* Call back to the format-dependent code to fill in the rest of the
492 fields and do any further cleanup. Things that should be filled
493 in by the callback: */
495 struct exec *execp = exec_hdr (abfd);
497 obj_textsec (abfd)->size = N_TXTSIZE(*execp);
498 obj_textsec (abfd)->raw_size = N_TXTSIZE(*execp);
499 /* data and bss are already filled in since they're so standard */
501 /* The virtual memory addresses of the sections */
502 obj_textsec (abfd)->vma = N_TXTADDR(*execp);
503 obj_datasec (abfd)->vma = N_DATADDR(*execp);
504 obj_bsssec (abfd)->vma = N_BSSADDR(*execp);
506 /* The file offsets of the sections */
507 obj_textsec (abfd)->filepos = N_TXTOFF(*execp);
508 obj_datasec (abfd)->filepos = N_DATOFF(*execp);
510 /* The file offsets of the relocation info */
511 obj_textsec (abfd)->rel_filepos = N_TRELOFF(*execp);
512 obj_datasec (abfd)->rel_filepos = N_DRELOFF(*execp);
514 /* The file offsets of the string table and symbol table. */
515 obj_str_filepos (abfd) = N_STROFF (*execp);
516 obj_sym_filepos (abfd) = N_SYMOFF (*execp);
518 /* Determine the architecture and machine type of the object file. */
519 switch (N_MACHTYPE (*exec_hdr (abfd))) {
521 abfd->obj_arch = bfd_arch_obscure;
525 adata(abfd)->page_size = PAGE_SIZE;
526 adata(abfd)->segment_size = SEGMENT_SIZE;
527 adata(abfd)->exec_bytes_size = EXEC_BYTES_SIZE;
531 /* The architecture is encoded in various ways in various a.out variants,
532 or is not encoded at all in some of them. The relocation size depends
533 on the architecture and the a.out variant. Finally, the return value
534 is the bfd_target vector in use. If an error occurs, return zero and
535 set bfd_error to the appropriate error code.
537 Formats such as b.out, which have additional fields in the a.out
538 header, should cope with them in this callback as well. */
539 #endif /* DOCUMENTATION */
541 result = (*callback_to_real_object_p)(abfd);
543 /* Now that the segment addresses have been worked out, take a better
544 guess at whether the file is executable. If the entry point
545 is within the text segment, assume it is. (This makes files
546 executable even if their entry point address is 0, as long as
547 their text starts at zero.)
549 At some point we should probably break down and stat the file and
550 declare it executable if (one of) its 'x' bits are on... */
551 if ((execp->a_entry >= obj_textsec(abfd)->vma) &&
552 (execp->a_entry < obj_textsec(abfd)->vma + obj_textsec(abfd)->_raw_size))
553 abfd->flags |= EXEC_P;
556 #if 0 /* These should be set correctly anyways. */
557 abfd->sections = obj_textsec (abfd);
558 obj_textsec (abfd)->next = obj_datasec (abfd);
559 obj_datasec (abfd)->next = obj_bsssec (abfd);
565 abfd->tdata.aout_data = oldrawptr;
572 aout_@var{size}_mkobject
575 boolean aout_@var{size}_mkobject, (bfd *abfd);
578 Initialize BFD @var{abfd} for use with a.out files.
582 NAME(aout,mkobject) (abfd)
585 struct aout_data_struct *rawptr;
587 bfd_set_error (bfd_error_system_call);
589 /* Use an intermediate variable for clarity */
590 rawptr = (struct aout_data_struct *)bfd_zalloc (abfd, sizeof (struct aout_data_struct ));
592 if (rawptr == NULL) {
593 bfd_set_error (bfd_error_no_memory);
597 abfd->tdata.aout_data = rawptr;
598 exec_hdr (abfd) = &(rawptr->e);
600 obj_textsec (abfd) = (asection *)NULL;
601 obj_datasec (abfd) = (asection *)NULL;
602 obj_bsssec (abfd) = (asection *)NULL;
610 aout_@var{size}_machine_type
613 enum machine_type aout_@var{size}_machine_type
614 (enum bfd_architecture arch,
615 unsigned long machine));
618 Keep track of machine architecture and machine type for
619 a.out's. Return the <<machine_type>> for a particular
620 architecture and machine, or <<M_UNKNOWN>> if that exact architecture
621 and machine can't be represented in a.out format.
623 If the architecture is understood, machine type 0 (default)
624 is always understood.
628 NAME(aout,machine_type) (arch, machine)
629 enum bfd_architecture arch;
630 unsigned long machine;
632 enum machine_type arch_flags;
634 arch_flags = M_UNKNOWN;
638 if (machine == 0) arch_flags = M_SPARC;
643 case 0: arch_flags = M_68010; break;
644 case 68000: arch_flags = M_UNKNOWN; break;
645 case 68010: arch_flags = M_68010; break;
646 case 68020: arch_flags = M_68020; break;
647 default: arch_flags = M_UNKNOWN; break;
652 if (machine == 0) arch_flags = M_386;
656 if (machine == 0) arch_flags = M_29K;
663 case 3000: arch_flags = M_MIPS1; break;
666 case 6000: arch_flags = M_MIPS2; break;
667 default: arch_flags = M_UNKNOWN; break;
672 arch_flags = M_UNKNOWN;
680 aout_@var{size}_set_arch_mach
683 boolean aout_@var{size}_set_arch_mach,
685 enum bfd_architecture arch,
686 unsigned long machine));
689 Set the architecture and the machine of the BFD @var{abfd} to the
690 values @var{arch} and @var{machine}. Verify that @var{abfd}'s format
691 can support the architecture required.
695 NAME(aout,set_arch_mach) (abfd, arch, machine)
697 enum bfd_architecture arch;
698 unsigned long machine;
700 if (! bfd_default_set_arch_mach (abfd, arch, machine))
703 if (arch != bfd_arch_unknown &&
704 NAME(aout,machine_type) (arch, machine) == M_UNKNOWN)
705 return false; /* We can't represent this type */
707 /* Determine the size of a relocation entry */
712 obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE;
715 obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
719 return (*aout_backend_info(abfd)->set_sizes) (abfd);
723 adjust_o_magic (abfd, execp)
725 struct internal_exec *execp;
727 file_ptr pos = adata (abfd).exec_bytes_size;
732 obj_textsec(abfd)->filepos = pos;
733 pos += obj_textsec(abfd)->_raw_size;
734 vma += obj_textsec(abfd)->_raw_size;
737 if (!obj_datasec(abfd)->user_set_vma)
739 #if 0 /* ?? Does alignment in the file image really matter? */
740 pad = align_power (vma, obj_datasec(abfd)->alignment_power) - vma;
742 obj_textsec(abfd)->_raw_size += pad;
745 obj_datasec(abfd)->vma = vma;
747 obj_datasec(abfd)->filepos = pos;
748 pos += obj_datasec(abfd)->_raw_size;
749 vma += obj_datasec(abfd)->_raw_size;
752 if (!obj_bsssec(abfd)->user_set_vma)
755 pad = align_power (vma, obj_bsssec(abfd)->alignment_power) - vma;
757 obj_datasec(abfd)->_raw_size += pad;
760 obj_bsssec(abfd)->vma = vma;
762 obj_bsssec(abfd)->filepos = pos;
764 /* Fix up the exec header. */
765 execp->a_text = obj_textsec(abfd)->_raw_size;
766 execp->a_data = obj_datasec(abfd)->_raw_size;
767 execp->a_bss = obj_bsssec(abfd)->_raw_size;
768 N_SET_MAGIC (*execp, OMAGIC);
772 adjust_z_magic (abfd, execp)
774 struct internal_exec *execp;
776 bfd_size_type data_pad, text_pad;
778 CONST struct aout_backend_data *abdp;
779 int ztih; /* Nonzero if text includes exec header. */
781 abdp = aout_backend_info (abfd);
784 ztih = abdp && abdp->text_includes_header;
785 obj_textsec(abfd)->filepos = (ztih
786 ? adata(abfd).exec_bytes_size
787 : adata(abfd).page_size);
788 if (! obj_textsec(abfd)->user_set_vma)
789 /* ?? Do we really need to check for relocs here? */
790 obj_textsec(abfd)->vma = ((abfd->flags & HAS_RELOC)
793 ? (abdp->default_text_vma
794 + adata(abfd).exec_bytes_size)
795 : abdp->default_text_vma));
796 /* Could take strange alignment of text section into account here? */
798 /* Find start of data. */
799 text_end = obj_textsec(abfd)->filepos + obj_textsec(abfd)->_raw_size;
800 text_pad = BFD_ALIGN (text_end, adata(abfd).page_size) - text_end;
801 obj_textsec(abfd)->_raw_size += text_pad;
802 text_end += text_pad;
805 if (!obj_datasec(abfd)->user_set_vma)
808 vma = obj_textsec(abfd)->vma + obj_textsec(abfd)->_raw_size;
809 obj_datasec(abfd)->vma = BFD_ALIGN (vma, adata(abfd).segment_size);
811 if (abdp && abdp->zmagic_mapped_contiguous)
813 text_pad = (obj_datasec(abfd)->vma
814 - obj_textsec(abfd)->vma
815 - obj_textsec(abfd)->_raw_size);
816 obj_textsec(abfd)->_raw_size += text_pad;
818 obj_datasec(abfd)->filepos = (obj_textsec(abfd)->filepos
819 + obj_textsec(abfd)->_raw_size);
821 /* Fix up exec header while we're at it. */
822 execp->a_text = obj_textsec(abfd)->_raw_size;
823 if (ztih && (!abdp || (abdp && !abdp->exec_header_not_counted)))
824 execp->a_text += adata(abfd).exec_bytes_size;
825 N_SET_MAGIC (*execp, ZMAGIC);
827 /* Spec says data section should be rounded up to page boundary. */
828 obj_datasec(abfd)->_raw_size
829 = align_power (obj_datasec(abfd)->_raw_size,
830 obj_bsssec(abfd)->alignment_power);
831 execp->a_data = BFD_ALIGN (obj_datasec(abfd)->_raw_size,
832 adata(abfd).page_size);
833 data_pad = execp->a_data - obj_datasec(abfd)->_raw_size;
836 if (!obj_bsssec(abfd)->user_set_vma)
837 obj_bsssec(abfd)->vma = (obj_datasec(abfd)->vma
838 + obj_datasec(abfd)->_raw_size);
839 /* If the BSS immediately follows the data section and extra space
840 in the page is left after the data section, fudge data
841 in the header so that the bss section looks smaller by that
842 amount. We'll start the bss section there, and lie to the OS.
843 (Note that a linker script, as well as the above assignment,
844 could have explicitly set the BSS vma to immediately follow
845 the data section.) */
846 if (align_power (obj_bsssec(abfd)->vma, obj_bsssec(abfd)->alignment_power)
847 == obj_datasec(abfd)->vma + obj_datasec(abfd)->_raw_size)
848 execp->a_bss = (data_pad > obj_bsssec(abfd)->_raw_size) ? 0 :
849 obj_bsssec(abfd)->_raw_size - data_pad;
851 execp->a_bss = obj_bsssec(abfd)->_raw_size;
855 adjust_n_magic (abfd, execp)
857 struct internal_exec *execp;
859 file_ptr pos = adata(abfd).exec_bytes_size;
864 obj_textsec(abfd)->filepos = pos;
865 if (!obj_textsec(abfd)->user_set_vma)
866 obj_textsec(abfd)->vma = vma;
868 vma = obj_textsec(abfd)->vma;
869 pos += obj_textsec(abfd)->_raw_size;
870 vma += obj_textsec(abfd)->_raw_size;
873 obj_datasec(abfd)->filepos = pos;
874 if (!obj_datasec(abfd)->user_set_vma)
875 obj_datasec(abfd)->vma = BFD_ALIGN (vma, adata(abfd).segment_size);
876 vma = obj_datasec(abfd)->vma;
878 /* Since BSS follows data immediately, see if it needs alignment. */
879 vma += obj_datasec(abfd)->_raw_size;
880 pad = align_power (vma, obj_bsssec(abfd)->alignment_power) - vma;
881 obj_datasec(abfd)->_raw_size += pad;
882 pos += obj_datasec(abfd)->_raw_size;
885 if (!obj_bsssec(abfd)->user_set_vma)
886 obj_bsssec(abfd)->vma = vma;
888 vma = obj_bsssec(abfd)->vma;
890 /* Fix up exec header. */
891 execp->a_text = obj_textsec(abfd)->_raw_size;
892 execp->a_data = obj_datasec(abfd)->_raw_size;
893 execp->a_bss = obj_bsssec(abfd)->_raw_size;
894 N_SET_MAGIC (*execp, NMAGIC);
898 NAME(aout,adjust_sizes_and_vmas) (abfd, text_size, text_end)
900 bfd_size_type *text_size;
903 struct internal_exec *execp = exec_hdr (abfd);
905 if (! NAME(aout,make_sections) (abfd))
908 if (adata(abfd).magic != undecided_magic) return true;
910 obj_textsec(abfd)->_raw_size =
911 align_power(obj_textsec(abfd)->_raw_size,
912 obj_textsec(abfd)->alignment_power);
914 *text_size = obj_textsec (abfd)->_raw_size;
915 /* Rule (heuristic) for when to pad to a new page. Note that there
916 are (at least) two ways demand-paged (ZMAGIC) files have been
917 handled. Most Berkeley-based systems start the text segment at
918 (PAGE_SIZE). However, newer versions of SUNOS start the text
919 segment right after the exec header; the latter is counted in the
920 text segment size, and is paged in by the kernel with the rest of
923 /* This perhaps isn't the right way to do this, but made it simpler for me
924 to understand enough to implement it. Better would probably be to go
925 right from BFD flags to alignment/positioning characteristics. But the
926 old code was sloppy enough about handling the flags, and had enough
927 other magic, that it was a little hard for me to understand. I think
928 I understand it better now, but I haven't time to do the cleanup this
931 if (abfd->flags & D_PAGED)
932 /* Whether or not WP_TEXT is set -- let D_PAGED override. */
933 /* @@ What about QMAGIC? */
934 adata(abfd).magic = z_magic;
935 else if (abfd->flags & WP_TEXT)
936 adata(abfd).magic = n_magic;
938 adata(abfd).magic = o_magic;
940 #ifdef BFD_AOUT_DEBUG /* requires gcc2 */
942 fprintf (stderr, "%s text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x,%x>\n",
944 switch (adata(abfd).magic) {
945 case n_magic: str = "NMAGIC"; break;
946 case o_magic: str = "OMAGIC"; break;
947 case z_magic: str = "ZMAGIC"; break;
952 obj_textsec(abfd)->vma, obj_textsec(abfd)->_raw_size,
953 obj_textsec(abfd)->alignment_power,
954 obj_datasec(abfd)->vma, obj_datasec(abfd)->_raw_size,
955 obj_datasec(abfd)->alignment_power,
956 obj_bsssec(abfd)->vma, obj_bsssec(abfd)->_raw_size,
957 obj_bsssec(abfd)->alignment_power);
961 switch (adata(abfd).magic)
964 adjust_o_magic (abfd, execp);
967 adjust_z_magic (abfd, execp);
970 adjust_n_magic (abfd, execp);
976 #ifdef BFD_AOUT_DEBUG
977 fprintf (stderr, " text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x>\n",
978 obj_textsec(abfd)->vma, obj_textsec(abfd)->_raw_size,
979 obj_textsec(abfd)->filepos,
980 obj_datasec(abfd)->vma, obj_datasec(abfd)->_raw_size,
981 obj_datasec(abfd)->filepos,
982 obj_bsssec(abfd)->vma, obj_bsssec(abfd)->_raw_size);
990 aout_@var{size}_new_section_hook
993 boolean aout_@var{size}_new_section_hook,
998 Called by the BFD in response to a @code{bfd_make_section}
1002 NAME(aout,new_section_hook) (abfd, newsect)
1006 /* align to double at least */
1007 newsect->alignment_power = bfd_get_arch_info(abfd)->section_align_power;
1010 if (bfd_get_format (abfd) == bfd_object)
1012 if (obj_textsec(abfd) == NULL && !strcmp(newsect->name, ".text")) {
1013 obj_textsec(abfd)= newsect;
1014 newsect->target_index = N_TEXT;
1018 if (obj_datasec(abfd) == NULL && !strcmp(newsect->name, ".data")) {
1019 obj_datasec(abfd) = newsect;
1020 newsect->target_index = N_DATA;
1024 if (obj_bsssec(abfd) == NULL && !strcmp(newsect->name, ".bss")) {
1025 obj_bsssec(abfd) = newsect;
1026 newsect->target_index = N_BSS;
1032 /* We allow more than three sections internally */
1037 NAME(aout,set_section_contents) (abfd, section, location, offset, count)
1042 bfd_size_type count;
1045 bfd_size_type text_size;
1047 if (abfd->output_has_begun == false)
1049 if (NAME(aout,adjust_sizes_and_vmas) (abfd,
1051 &text_end) == false)
1055 /* regardless, once we know what we're doing, we might as well get going */
1056 if (section != obj_bsssec(abfd))
1058 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0)
1062 return (bfd_write ((PTR)location, 1, count, abfd) == count) ?
1070 /* Read the external symbols from an a.out file. */
1073 aout_get_external_symbols (abfd)
1076 if (obj_aout_external_syms (abfd) == (struct external_nlist *) NULL)
1078 bfd_size_type count;
1079 struct external_nlist *syms;
1081 count = exec_hdr (abfd)->a_syms / EXTERNAL_NLIST_SIZE;
1083 /* We allocate using malloc to make the values easy to free
1084 later on. If we put them on the obstack it might not be
1085 possible to free them. */
1086 syms = ((struct external_nlist *)
1087 malloc ((size_t) count * EXTERNAL_NLIST_SIZE));
1088 if (syms == (struct external_nlist *) NULL && count != 0)
1090 bfd_set_error (bfd_error_no_memory);
1094 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
1095 || (bfd_read (syms, 1, exec_hdr (abfd)->a_syms, abfd)
1096 != exec_hdr (abfd)->a_syms))
1102 obj_aout_external_syms (abfd) = syms;
1103 obj_aout_external_sym_count (abfd) = count;
1106 if (obj_aout_external_strings (abfd) == NULL
1107 && exec_hdr (abfd)->a_syms != 0)
1109 unsigned char string_chars[BYTES_IN_WORD];
1110 bfd_size_type stringsize;
1113 /* Get the size of the strings. */
1114 if (bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET) != 0
1115 || (bfd_read ((PTR) string_chars, BYTES_IN_WORD, 1, abfd)
1118 stringsize = GET_WORD (abfd, string_chars);
1120 strings = (char *) malloc ((size_t) stringsize + 1);
1121 if (strings == NULL)
1123 bfd_set_error (bfd_error_no_memory);
1127 /* Skip space for the string count in the buffer for convenience
1128 when using indexes. */
1129 if (bfd_read (strings + BYTES_IN_WORD, 1, stringsize - BYTES_IN_WORD,
1131 != stringsize - BYTES_IN_WORD)
1137 /* Sanity preservation. */
1138 strings[stringsize] = '\0';
1140 obj_aout_external_strings (abfd) = strings;
1141 obj_aout_external_string_size (abfd) = stringsize;
1147 /* Translate an a.out symbol into a BFD symbol. The desc, other, type
1148 and symbol->value fields of CACHE_PTR will be set from the a.out
1149 nlist structure. This function is responsible for setting
1150 symbol->flags and symbol->section, and adjusting symbol->value. */
1153 translate_from_native_sym_flags (abfd, cache_ptr)
1155 aout_symbol_type *cache_ptr;
1159 if ((cache_ptr->type & N_STAB) != 0
1160 || cache_ptr->type == N_FN)
1164 /* This is a debugging symbol. */
1166 cache_ptr->symbol.flags = BSF_DEBUGGING;
1168 /* Work out the symbol section. */
1169 switch (cache_ptr->type & N_TYPE)
1173 sec = obj_textsec (abfd);
1176 sec = obj_datasec (abfd);
1179 sec = obj_bsssec (abfd);
1183 sec = &bfd_abs_section;
1187 cache_ptr->symbol.section = sec;
1188 cache_ptr->symbol.value -= sec->vma;
1193 /* Get the default visibility. This does not apply to all types, so
1194 we just hold it in a local variable to use if wanted. */
1195 if ((cache_ptr->type & N_EXT) == 0)
1196 visible = BSF_LOCAL;
1198 visible = BSF_GLOBAL;
1200 switch (cache_ptr->type)
1203 case N_ABS: case N_ABS | N_EXT:
1204 cache_ptr->symbol.section = &bfd_abs_section;
1205 cache_ptr->symbol.flags = visible;
1208 case N_UNDF | N_EXT:
1209 if (cache_ptr->symbol.value != 0)
1211 /* This is a common symbol. */
1212 cache_ptr->symbol.flags = BSF_GLOBAL;
1213 cache_ptr->symbol.section = &bfd_com_section;
1217 cache_ptr->symbol.flags = 0;
1218 cache_ptr->symbol.section = &bfd_und_section;
1222 case N_TEXT: case N_TEXT | N_EXT:
1223 cache_ptr->symbol.section = obj_textsec (abfd);
1224 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1225 cache_ptr->symbol.flags = visible;
1228 case N_DATA: case N_DATA | N_EXT:
1229 cache_ptr->symbol.section = obj_datasec (abfd);
1230 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1231 cache_ptr->symbol.flags = visible;
1234 case N_BSS: case N_BSS | N_EXT:
1235 cache_ptr->symbol.section = obj_bsssec (abfd);
1236 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1237 cache_ptr->symbol.flags = visible;
1240 case N_SETA: case N_SETA | N_EXT:
1241 case N_SETT: case N_SETT | N_EXT:
1242 case N_SETD: case N_SETD | N_EXT:
1243 case N_SETB: case N_SETB | N_EXT:
1246 arelent_chain *reloc;
1247 asection *into_section;
1249 /* This is a set symbol. The name of the symbol is the name
1250 of the set (e.g., __CTOR_LIST__). The value of the symbol
1251 is the value to add to the set. We create a section with
1252 the same name as the symbol, and add a reloc to insert the
1253 appropriate value into the section.
1255 This action is actually obsolete; it used to make the
1256 linker do the right thing, but the linker no longer uses
1259 section = bfd_get_section_by_name (abfd, cache_ptr->symbol.name);
1260 if (section == NULL)
1264 copy = bfd_alloc (abfd, strlen (cache_ptr->symbol.name) + 1);
1267 bfd_set_error (bfd_error_no_memory);
1271 strcpy (copy, cache_ptr->symbol.name);
1272 section = bfd_make_section (abfd, copy);
1273 if (section == NULL)
1277 reloc = (arelent_chain *) bfd_alloc (abfd, sizeof (arelent_chain));
1280 bfd_set_error (bfd_error_no_memory);
1284 /* Build a relocation entry for the constructor. */
1285 switch (cache_ptr->type & N_TYPE)
1288 into_section = &bfd_abs_section;
1289 cache_ptr->type = N_ABS;
1292 into_section = obj_textsec (abfd);
1293 cache_ptr->type = N_TEXT;
1296 into_section = obj_datasec (abfd);
1297 cache_ptr->type = N_DATA;
1300 into_section = obj_bsssec (abfd);
1301 cache_ptr->type = N_BSS;
1305 /* Build a relocation pointing into the constructor section
1306 pointing at the symbol in the set vector specified. */
1307 reloc->relent.addend = cache_ptr->symbol.value;
1308 cache_ptr->symbol.section = into_section;
1309 reloc->relent.sym_ptr_ptr = into_section->symbol_ptr_ptr;
1311 /* We modify the symbol to belong to a section depending upon
1312 the name of the symbol, and add to the size of the section
1313 to contain a pointer to the symbol. Build a reloc entry to
1314 relocate to this symbol attached to this section. */
1315 section->flags = SEC_CONSTRUCTOR | SEC_RELOC;
1317 section->reloc_count++;
1318 section->alignment_power = 2;
1320 reloc->next = section->constructor_chain;
1321 section->constructor_chain = reloc;
1322 reloc->relent.address = section->_raw_size;
1323 section->_raw_size += BYTES_IN_WORD;
1325 if (obj_reloc_entry_size (abfd) == RELOC_EXT_SIZE)
1326 reloc->relent.howto = howto_table_ext + CTOR_TABLE_RELOC_IDX;
1328 reloc->relent.howto = howto_table_std + CTOR_TABLE_RELOC_IDX;
1330 cache_ptr->symbol.flags |= BSF_CONSTRUCTOR;
1335 /* This symbol is the text of a warning message. The next
1336 symbol is the symbol to associate the warning with. If a
1337 reference is made to that symbol, a warning is issued. */
1338 cache_ptr->symbol.flags = BSF_DEBUGGING | BSF_WARNING;
1340 /* @@ Stuffing pointers into integers is a no-no. We can
1341 usually get away with it if the integer is large enough
1343 if (sizeof (cache_ptr + 1) > sizeof (bfd_vma))
1345 cache_ptr->symbol.value = (bfd_vma) (cache_ptr + 1);
1347 cache_ptr->symbol.section = &bfd_abs_section;
1351 case N_INDR: case N_INDR | N_EXT:
1352 /* An indirect symbol. This consists of two symbols in a row.
1353 The first symbol is the name of the indirection. The second
1354 symbol is the name of the target. A reference to the first
1355 symbol becomes a reference to the second. */
1356 cache_ptr->symbol.flags = BSF_DEBUGGING | BSF_INDIRECT | visible;
1358 /* @@ Stuffing pointers into integers is a no-no. We can
1359 usually get away with it if the integer is large enough
1361 if (sizeof (cache_ptr + 1) > sizeof (bfd_vma))
1363 cache_ptr->symbol.value = (bfd_vma) (cache_ptr + 1);
1365 cache_ptr->symbol.section = &bfd_ind_section;
1370 cache_ptr->symbol.section = &bfd_und_section;
1371 cache_ptr->symbol.flags = BSF_WEAK;
1375 cache_ptr->symbol.section = &bfd_abs_section;
1376 cache_ptr->symbol.flags = BSF_WEAK;
1380 cache_ptr->symbol.section = obj_textsec (abfd);
1381 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1382 cache_ptr->symbol.flags = BSF_WEAK;
1386 cache_ptr->symbol.section = obj_datasec (abfd);
1387 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1388 cache_ptr->symbol.flags = BSF_WEAK;
1392 cache_ptr->symbol.section = obj_bsssec (abfd);
1393 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1394 cache_ptr->symbol.flags = BSF_WEAK;
1401 /* Set the fields of SYM_POINTER according to CACHE_PTR. */
1404 translate_to_native_sym_flags (abfd, cache_ptr, sym_pointer)
1407 struct external_nlist *sym_pointer;
1409 bfd_vma value = cache_ptr->value;
1411 /* Mask out any existing type bits in case copying from one section
1413 sym_pointer->e_type[0] &= ~N_TYPE;
1415 if (bfd_get_section (cache_ptr) == &bfd_abs_section)
1416 sym_pointer->e_type[0] |= N_ABS;
1417 else if (bfd_get_section (cache_ptr) == obj_textsec (abfd))
1418 sym_pointer->e_type[0] |= N_TEXT;
1419 else if (bfd_get_section (cache_ptr) == obj_datasec (abfd))
1420 sym_pointer->e_type[0] |= N_DATA;
1421 else if (bfd_get_section (cache_ptr) == obj_bsssec (abfd))
1422 sym_pointer->e_type[0] |= N_BSS;
1423 else if (bfd_get_section (cache_ptr) == &bfd_und_section)
1424 sym_pointer->e_type[0] = N_UNDF | N_EXT;
1425 else if (bfd_get_section (cache_ptr) == &bfd_ind_section)
1426 sym_pointer->e_type[0] = N_INDR;
1427 else if (bfd_get_section (cache_ptr) == NULL)
1429 /* Protect the bfd_is_com_section call. This case occurs, e.g.,
1430 for the *DEBUG* section of a COFF file. */
1431 bfd_set_error (bfd_error_nonrepresentable_section);
1434 else if (bfd_is_com_section (bfd_get_section (cache_ptr)))
1435 sym_pointer->e_type[0] = N_UNDF | N_EXT;
1438 bfd_set_error (bfd_error_nonrepresentable_section);
1442 /* Turn the symbol from section relative to absolute again */
1443 value += cache_ptr->section->vma;
1445 if ((cache_ptr->flags & BSF_WARNING) != 0)
1446 sym_pointer->e_type[0] = N_WARNING;
1448 if ((cache_ptr->flags & BSF_DEBUGGING) != 0)
1449 sym_pointer->e_type[0] = ((aout_symbol_type *) cache_ptr)->type;
1450 else if ((cache_ptr->flags & BSF_GLOBAL) != 0)
1451 sym_pointer->e_type[0] |= N_EXT;
1453 if ((cache_ptr->flags & BSF_CONSTRUCTOR) != 0)
1455 int type = ((aout_symbol_type *) cache_ptr)->type;
1458 case N_ABS: type = N_SETA; break;
1459 case N_TEXT: type = N_SETT; break;
1460 case N_DATA: type = N_SETD; break;
1461 case N_BSS: type = N_SETB; break;
1463 sym_pointer->e_type[0] = type;
1466 if ((cache_ptr->flags & BSF_WEAK) != 0)
1470 switch (sym_pointer->e_type[0] & N_TYPE)
1473 case N_ABS: type = N_WEAKA; break;
1474 case N_TEXT: type = N_WEAKT; break;
1475 case N_DATA: type = N_WEAKD; break;
1476 case N_BSS: type = N_WEAKB; break;
1477 case N_UNDF: type = N_WEAKU; break;
1479 sym_pointer->e_type[0] = type;
1482 PUT_WORD(abfd, value, sym_pointer->e_value);
1487 /* Native-level interface to symbols. */
1490 NAME(aout,make_empty_symbol) (abfd)
1493 aout_symbol_type *new =
1494 (aout_symbol_type *)bfd_zalloc (abfd, sizeof (aout_symbol_type));
1497 bfd_set_error (bfd_error_no_memory);
1500 new->symbol.the_bfd = abfd;
1502 return &new->symbol;
1505 /* Translate a set of internal symbols into external symbols. */
1508 NAME(aout,translate_symbol_table) (abfd, in, ext, count, str, strsize, dynamic)
1510 aout_symbol_type *in;
1511 struct external_nlist *ext;
1512 bfd_size_type count;
1514 bfd_size_type strsize;
1517 struct external_nlist *ext_end;
1519 ext_end = ext + count;
1520 for (; ext < ext_end; ext++, in++)
1524 x = GET_WORD (abfd, ext->e_strx);
1525 in->symbol.the_bfd = abfd;
1527 /* For the normal symbols, the zero index points at the number
1528 of bytes in the string table but is to be interpreted as the
1529 null string. For the dynamic symbols, the number of bytes in
1530 the string table is stored in the __DYNAMIC structure and the
1531 zero index points at an actual string. */
1532 if (x == 0 && ! dynamic)
1533 in->symbol.name = "";
1534 else if (x < strsize)
1535 in->symbol.name = str + x;
1539 in->symbol.value = GET_SWORD (abfd, ext->e_value);
1540 in->desc = bfd_h_get_16 (abfd, ext->e_desc);
1541 in->other = bfd_h_get_8 (abfd, ext->e_other);
1542 in->type = bfd_h_get_8 (abfd, ext->e_type);
1543 in->symbol.udata = 0;
1545 if (! translate_from_native_sym_flags (abfd, in))
1549 in->symbol.flags |= BSF_DYNAMIC;
1555 /* We read the symbols into a buffer, which is discarded when this
1556 function exits. We read the strings into a buffer large enough to
1557 hold them all plus all the cached symbol entries. */
1560 NAME(aout,slurp_symbol_table) (abfd)
1563 struct external_nlist *old_external_syms;
1564 aout_symbol_type *cached;
1567 /* If there's no work to be done, don't do any */
1568 if (obj_aout_symbols (abfd) != (aout_symbol_type *) NULL)
1571 old_external_syms = obj_aout_external_syms (abfd);
1573 if (! aout_get_external_symbols (abfd))
1576 if (obj_aout_external_sym_count (abfd) == 0)
1578 bfd_set_error (bfd_error_no_symbols);
1582 cached_size = (obj_aout_external_sym_count (abfd)
1583 * sizeof (aout_symbol_type));
1584 cached = (aout_symbol_type *) malloc (cached_size);
1587 bfd_set_error (bfd_error_no_memory);
1590 memset (cached, 0, cached_size);
1592 /* Convert from external symbol information to internal. */
1593 if (! (NAME(aout,translate_symbol_table)
1595 obj_aout_external_syms (abfd),
1596 obj_aout_external_sym_count (abfd),
1597 obj_aout_external_strings (abfd),
1598 obj_aout_external_string_size (abfd),
1605 bfd_get_symcount (abfd) = obj_aout_external_sym_count (abfd);
1607 obj_aout_symbols (abfd) = cached;
1609 /* It is very likely that anybody who calls this function will not
1610 want the external symbol information, so if it was allocated
1611 because of our call to aout_get_external_symbols, we free it up
1612 right away to save space. */
1613 if (old_external_syms == (struct external_nlist *) NULL
1614 && obj_aout_external_syms (abfd) != (struct external_nlist *) NULL)
1616 free (obj_aout_external_syms (abfd));
1617 obj_aout_external_syms (abfd) = NULL;
1623 /* Possible improvements:
1624 + look for strings matching trailing substrings of other strings
1625 + better data structures? balanced trees?
1626 + smaller per-string or per-symbol data? re-use some of the symbol's
1628 + also look at reducing memory use elsewhere -- maybe if we didn't have to
1629 construct the entire symbol table at once, we could get by with smaller
1630 amounts of VM? (What effect does that have on the string table
1632 + rip this out of here, put it into its own file in bfd or libiberty, so
1633 coff and elf can use it too. I'll work on this soon, but have more
1634 pressing tasks right now.
1636 A hash table might(?) be more efficient for handling exactly the cases that
1637 are handled now, but for trailing substring matches, I think we want to
1638 examine the `nearest' values (reverse-)lexically, not merely impose a strict
1639 order, nor look only for exact-match or not-match. I don't think a hash
1640 table would be very useful for that, and I don't feel like fleshing out two
1641 completely different implementations. [raeburn:930419.0331EDT] */
1643 struct stringtab_entry {
1644 /* Hash value for this string. Only useful so long as we aren't doing
1645 substring matches. */
1648 /* Next node to look at, depending on whether the hash value of the string
1649 being searched for is less than or greater than the hash value of the
1650 current node. For now, `equal to' is lumped in with `greater than', for
1651 space efficiency. It's not a common enough case to warrant another field
1652 to be used for all nodes. */
1653 struct stringtab_entry *less;
1654 struct stringtab_entry *greater;
1656 /* The string itself. */
1659 /* The index allocated for this string. */
1660 bfd_size_type index;
1662 #ifdef GATHER_STATISTICS
1663 /* How many references have there been to this string? (Not currently used;
1664 could be dumped out for anaylsis, if anyone's interested.) */
1665 unsigned long count;
1668 /* Next node in linked list, in suggested output order. */
1669 struct stringtab_entry *next_to_output;
1672 struct stringtab_data {
1673 /* Tree of string table entries. */
1674 struct stringtab_entry *strings;
1676 /* Fudge factor used to center top node of tree. */
1679 /* Next index value to issue. */
1680 bfd_size_type index;
1682 /* Index used for empty strings. Cached here because checking for them
1683 is really easy, and we can avoid searching the tree. */
1684 bfd_size_type empty_string_index;
1686 /* These fields indicate the two ends of a singly-linked list that indicates
1687 the order strings should be written out in. Use this order, and no
1688 seeking will need to be done, so output efficiency should be maximized. */
1689 struct stringtab_entry **end;
1690 struct stringtab_entry *output_order;
1692 #ifdef GATHER_STATISTICS
1693 /* Number of strings which duplicate strings already in the table. */
1694 unsigned long duplicates;
1696 /* Number of bytes saved by not having to write all the duplicate strings. */
1697 unsigned long bytes_saved;
1699 /* Number of zero-length strings. Currently, these all turn into
1700 references to the null byte at the end of the first string. In some
1701 cases (possibly not all? explore this...), it should be possible to
1702 simply write out a zero index value. */
1703 unsigned long empty_strings;
1705 /* Number of times the hash values matched but the strings were different.
1706 Note that this includes the number of times the other string(s) occurs, so
1707 there may only be two strings hashing to the same value, even if this
1708 number is very large. */
1709 unsigned long bad_hash_matches;
1711 /* Null strings aren't counted in this one.
1712 This will probably only be nonzero if we've got an input file
1713 which was produced by `ld -r' (i.e., it's already been processed
1714 through this code). Under some operating systems, native tools
1715 may make all empty strings have the same index; but the pointer
1716 check won't catch those, because to get to that stage we'd already
1717 have to compute the checksum, which requires reading the string,
1718 so we short-circuit that case with empty_string_index above. */
1719 unsigned long pointer_matches;
1721 /* Number of comparisons done. I figure with the algorithms in use below,
1722 the average number of comparisons done (per symbol) should be roughly
1723 log-base-2 of the number of unique strings. */
1724 unsigned long n_compares;
1728 /* Some utility functions for the string table code. */
1730 /* For speed, only hash on the first this many bytes of strings.
1731 This number was chosen by profiling ld linking itself, with -g. */
1732 #define HASHMAXLEN 25
1734 #define HASH_CHAR(c) (sum ^= sum >> 20, sum ^= sum << 7, sum += (c))
1736 static INLINE unsigned int
1738 unsigned char *string;
1739 register unsigned int len;
1741 register unsigned int sum = 0;
1743 if (len > HASHMAXLEN)
1751 HASH_CHAR (*string++);
1757 stringtab_init (tab)
1758 struct stringtab_data *tab;
1761 tab->output_order = 0;
1763 tab->end = &tab->output_order;
1765 /* Initial string table length includes size of length field. */
1766 tab->index = BYTES_IN_WORD;
1767 tab->empty_string_index = -1;
1768 #ifdef GATHER_STATISTICS
1769 tab->duplicates = 0;
1770 tab->empty_strings = 0;
1771 tab->bad_hash_matches = 0;
1772 tab->pointer_matches = 0;
1773 tab->bytes_saved = 0;
1774 tab->n_compares = 0;
1779 compare (entry, str, hash)
1780 struct stringtab_entry *entry;
1784 return hash - entry->hash;
1787 #ifdef GATHER_STATISTICS
1788 /* Don't want to have to link in math library with all bfd applications... */
1789 static INLINE double
1797 return ((d > 1.41) ? 0.5 : 0) + n;
1801 /* Main string table routines. */
1802 /* Returns index in string table. Whether or not this actually adds an
1803 entry into the string table should be irrelevant -- it just has to
1804 return a valid index. */
1805 static bfd_size_type
1806 add_to_stringtab (abfd, str, tab)
1809 struct stringtab_data *tab;
1811 struct stringtab_entry **ep;
1812 register struct stringtab_entry *entry;
1813 unsigned int hashval, len;
1817 bfd_size_type index;
1818 CONST bfd_size_type minus_one = -1;
1820 #ifdef GATHER_STATISTICS
1821 tab->empty_strings++;
1823 index = tab->empty_string_index;
1824 if (index != minus_one)
1827 #ifdef GATHER_STATISTICS
1834 /* Need to find it. */
1835 entry = tab->strings;
1838 index = entry->index + strlen (entry->string);
1839 tab->empty_string_index = index;
1847 /* The hash_zero value is chosen such that the first symbol gets a value of
1848 zero. With a balanced tree, this wouldn't be very useful, but without it,
1849 we might get a more even split at the top level, instead of skewing it
1850 badly should hash("/usr/lib/crt0.o") (or whatever) be far from zero. */
1851 hashval = hash (str, len) ^ tab->hash_zero;
1855 tab->hash_zero = hashval;
1865 #ifdef GATHER_STATISTICS
1868 cmp = compare (entry, str, hashval);
1869 /* The not-equal cases are more frequent, so check them first. */
1871 ep = &entry->greater;
1876 if (entry->string == str)
1878 #ifdef GATHER_STATISTICS
1879 tab->pointer_matches++;
1883 /* Compare the first bytes to save a function call if they
1885 if (entry->string[0] == str[0] && !strcmp (entry->string, str))
1888 #ifdef GATHER_STATISTICS
1890 tab->bytes_saved += len + 1;
1893 /* If we're in the linker, and the new string is from a new
1894 input file which might have already had these reductions
1895 run over it, we want to keep the new string pointer. I
1896 don't think we're likely to see any (or nearly as many,
1897 at least) cases where a later string is in the same location
1898 as an earlier one rather than this one. */
1899 entry->string = str;
1900 return entry->index;
1902 #ifdef GATHER_STATISTICS
1903 tab->bad_hash_matches++;
1905 ep = &entry->greater;
1909 /* If we get here, nothing that's in the table already matched.
1910 EP points to the `next' field at the end of the chain; stick a
1911 new entry on here. */
1913 entry = (struct stringtab_entry *)
1914 bfd_alloc_by_size_t (abfd, sizeof (struct stringtab_entry));
1917 bfd_set_error (bfd_error_no_memory);
1918 abort(); /* FIXME */
1921 entry->less = entry->greater = 0;
1922 entry->hash = hashval;
1923 entry->index = tab->index;
1924 entry->string = str;
1925 entry->next_to_output = 0;
1926 #ifdef GATHER_STATISTICS
1930 BFD_ASSERT (*tab->end == 0);
1931 *(tab->end) = entry;
1932 tab->end = &entry->next_to_output;
1933 BFD_ASSERT (*tab->end == 0);
1936 tab->index += len + 1;
1938 tab->empty_string_index = entry->index;
1940 BFD_ASSERT (*ep == 0);
1942 return entry->index;
1946 emit_strtab (abfd, tab)
1948 struct stringtab_data *tab;
1950 struct stringtab_entry *entry;
1951 #ifdef GATHER_STATISTICS
1955 /* Be sure to put string length into correct byte ordering before writing
1957 char buffer[BYTES_IN_WORD];
1959 PUT_WORD (abfd, tab->index, (unsigned char *) buffer);
1960 if (bfd_write ((PTR) buffer, 1, BYTES_IN_WORD, abfd) != BYTES_IN_WORD)
1963 for (entry = tab->output_order; entry; entry = entry->next_to_output)
1965 size_t len = strlen (entry->string) + 1;
1967 if (bfd_write ((PTR) entry->string, 1, len, abfd) != len)
1970 #ifdef GATHER_STATISTICS
1975 #ifdef GATHER_STATISTICS
1976 /* Short form only, for now.
1977 To do: Specify output file. Conditionalize on environment? Detailed
1978 analysis if desired. */
1980 int n_syms = bfd_get_symcount (abfd);
1982 fprintf (stderr, "String table data for output file:\n");
1983 fprintf (stderr, " %8d symbols output\n", n_syms);
1984 fprintf (stderr, " %8d duplicate strings\n", tab->duplicates);
1985 fprintf (stderr, " %8d empty strings\n", tab->empty_strings);
1986 fprintf (stderr, " %8d unique strings output\n", count);
1987 fprintf (stderr, " %8d pointer matches\n", tab->pointer_matches);
1988 fprintf (stderr, " %8d bytes saved\n", tab->bytes_saved);
1989 fprintf (stderr, " %8d bad hash matches\n", tab->bad_hash_matches);
1990 fprintf (stderr, " %8d hash-val comparisons\n", tab->n_compares);
1993 double n_compares = tab->n_compares;
1994 double avg_compares = n_compares / n_syms;
1995 /* The second value here should usually be near one. */
1997 "\t average %f comparisons per symbol (%f * log2 nstrings)\n",
1998 avg_compares, avg_compares / log2 (count));
2005 generic = bfd_get_outsymbols(abfd);
2006 for (count = 0; count < bfd_get_symcount(abfd); count++)
2008 asymbol *g = *(generic++);
2012 size_t length = strlen(g->name)+1;
2013 bfd_write((PTR)g->name, 1, length, abfd);
2015 g->KEEPIT = (KEEPITTYPE) count;
2022 NAME(aout,write_syms) (abfd)
2025 unsigned int count ;
2026 asymbol **generic = bfd_get_outsymbols (abfd);
2027 struct stringtab_data strtab;
2029 stringtab_init (&strtab);
2031 for (count = 0; count < bfd_get_symcount (abfd); count++)
2033 asymbol *g = generic[count];
2034 struct external_nlist nsp;
2037 PUT_WORD (abfd, add_to_stringtab (abfd, g->name, &strtab),
2038 (unsigned char *) nsp.e_strx);
2040 PUT_WORD (abfd, 0, (unsigned char *)nsp.e_strx);
2042 if (bfd_asymbol_flavour(g) == abfd->xvec->flavour)
2044 bfd_h_put_16(abfd, aout_symbol(g)->desc, nsp.e_desc);
2045 bfd_h_put_8(abfd, aout_symbol(g)->other, nsp.e_other);
2046 bfd_h_put_8(abfd, aout_symbol(g)->type, nsp.e_type);
2050 bfd_h_put_16(abfd,0, nsp.e_desc);
2051 bfd_h_put_8(abfd, 0, nsp.e_other);
2052 bfd_h_put_8(abfd, 0, nsp.e_type);
2055 if (! translate_to_native_sym_flags (abfd, g, &nsp))
2058 if (bfd_write((PTR)&nsp,1,EXTERNAL_NLIST_SIZE, abfd)
2059 != EXTERNAL_NLIST_SIZE)
2062 /* NB: `KEEPIT' currently overlays `flags', so set this only
2063 here, at the end. */
2067 return emit_strtab (abfd, &strtab);
2072 NAME(aout,get_symtab) (abfd, location)
2076 unsigned int counter = 0;
2077 aout_symbol_type *symbase;
2079 if (!NAME(aout,slurp_symbol_table)(abfd))
2082 for (symbase = obj_aout_symbols(abfd); counter++ < bfd_get_symcount (abfd);)
2083 *(location++) = (asymbol *)( symbase++);
2085 return bfd_get_symcount (abfd);
2089 /* Standard reloc stuff */
2090 /* Output standard relocation information to a file in target byte order. */
2093 NAME(aout,swap_std_reloc_out) (abfd, g, natptr)
2096 struct reloc_std_external *natptr;
2099 asymbol *sym = *(g->sym_ptr_ptr);
2101 unsigned int r_length;
2103 int r_baserel, r_jmptable, r_relative;
2104 asection *output_section = sym->section->output_section;
2106 PUT_WORD(abfd, g->address, natptr->r_address);
2108 r_length = g->howto->size ; /* Size as a power of two */
2109 r_pcrel = (int) g->howto->pc_relative; /* Relative to PC? */
2110 /* XXX This relies on relocs coming from a.out files. */
2111 r_baserel = (g->howto->type & 8) != 0;
2112 r_jmptable = (g->howto->type & 16) != 0;
2113 r_relative = (g->howto->type & 32) != 0;
2116 /* For a standard reloc, the addend is in the object file. */
2117 r_addend = g->addend + (*(g->sym_ptr_ptr))->section->output_section->vma;
2120 /* name was clobbered by aout_write_syms to be symbol index */
2122 /* If this relocation is relative to a symbol then set the
2123 r_index to the symbols index, and the r_extern bit.
2125 Absolute symbols can come in in two ways, either as an offset
2126 from the abs section, or as a symbol which has an abs value.
2131 if (bfd_is_com_section (output_section)
2132 || output_section == &bfd_abs_section
2133 || output_section == &bfd_und_section)
2135 if (bfd_abs_section.symbol == sym)
2137 /* Whoops, looked like an abs symbol, but is really an offset
2138 from the abs section */
2144 /* Fill in symbol */
2146 r_index = stoi((*(g->sym_ptr_ptr))->KEEPIT);
2152 /* Just an ordinary section */
2154 r_index = output_section->target_index;
2157 /* now the fun stuff */
2158 if (abfd->xvec->header_byteorder_big_p != false) {
2159 natptr->r_index[0] = r_index >> 16;
2160 natptr->r_index[1] = r_index >> 8;
2161 natptr->r_index[2] = r_index;
2163 (r_extern? RELOC_STD_BITS_EXTERN_BIG: 0)
2164 | (r_pcrel? RELOC_STD_BITS_PCREL_BIG: 0)
2165 | (r_baserel? RELOC_STD_BITS_BASEREL_BIG: 0)
2166 | (r_jmptable? RELOC_STD_BITS_JMPTABLE_BIG: 0)
2167 | (r_relative? RELOC_STD_BITS_RELATIVE_BIG: 0)
2168 | (r_length << RELOC_STD_BITS_LENGTH_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_STD_BITS_EXTERN_LITTLE: 0)
2175 | (r_pcrel? RELOC_STD_BITS_PCREL_LITTLE: 0)
2176 | (r_baserel? RELOC_STD_BITS_BASEREL_LITTLE: 0)
2177 | (r_jmptable? RELOC_STD_BITS_JMPTABLE_LITTLE: 0)
2178 | (r_relative? RELOC_STD_BITS_RELATIVE_LITTLE: 0)
2179 | (r_length << RELOC_STD_BITS_LENGTH_SH_LITTLE);
2184 /* Extended stuff */
2185 /* Output extended relocation information to a file in target byte order. */
2188 NAME(aout,swap_ext_reloc_out) (abfd, g, natptr)
2191 register struct reloc_ext_external *natptr;
2195 unsigned int r_type;
2196 unsigned int r_addend;
2197 asymbol *sym = *(g->sym_ptr_ptr);
2198 asection *output_section = sym->section->output_section;
2200 PUT_WORD (abfd, g->address, natptr->r_address);
2202 r_type = (unsigned int) g->howto->type;
2204 r_addend = g->addend + (*(g->sym_ptr_ptr))->section->output_section->vma;
2206 /* If this relocation is relative to a symbol then set the
2207 r_index to the symbols index, and the r_extern bit.
2209 Absolute symbols can come in in two ways, either as an offset
2210 from the abs section, or as a symbol which has an abs value.
2211 check for that here. */
2213 if (bfd_is_com_section (output_section)
2214 || output_section == &bfd_abs_section
2215 || output_section == &bfd_und_section)
2217 if (bfd_abs_section.symbol == sym)
2219 /* Whoops, looked like an abs symbol, but is really an offset
2220 from the abs section */
2227 r_index = stoi((*(g->sym_ptr_ptr))->KEEPIT);
2232 /* Just an ordinary section */
2234 r_index = output_section->target_index;
2237 /* now the fun stuff */
2238 if (abfd->xvec->header_byteorder_big_p != false) {
2239 natptr->r_index[0] = r_index >> 16;
2240 natptr->r_index[1] = r_index >> 8;
2241 natptr->r_index[2] = r_index;
2243 ((r_extern? RELOC_EXT_BITS_EXTERN_BIG: 0)
2244 | (r_type << RELOC_EXT_BITS_TYPE_SH_BIG));
2246 natptr->r_index[2] = r_index >> 16;
2247 natptr->r_index[1] = r_index >> 8;
2248 natptr->r_index[0] = r_index;
2250 (r_extern? RELOC_EXT_BITS_EXTERN_LITTLE: 0)
2251 | (r_type << RELOC_EXT_BITS_TYPE_SH_LITTLE);
2254 PUT_WORD (abfd, r_addend, natptr->r_addend);
2257 /* BFD deals internally with all things based from the section they're
2258 in. so, something in 10 bytes into a text section with a base of
2259 50 would have a symbol (.text+10) and know .text vma was 50.
2261 Aout keeps all it's symbols based from zero, so the symbol would
2262 contain 60. This macro subs the base of each section from the value
2263 to give the true offset from the section */
2266 #define MOVE_ADDRESS(ad) \
2268 /* undefined symbol */ \
2269 cache_ptr->sym_ptr_ptr = symbols + r_index; \
2270 cache_ptr->addend = ad; \
2272 /* defined, section relative. replace symbol with pointer to \
2273 symbol which points to section */ \
2274 switch (r_index) { \
2276 case N_TEXT | N_EXT: \
2277 cache_ptr->sym_ptr_ptr = obj_textsec(abfd)->symbol_ptr_ptr; \
2278 cache_ptr->addend = ad - su->textsec->vma; \
2281 case N_DATA | N_EXT: \
2282 cache_ptr->sym_ptr_ptr = obj_datasec(abfd)->symbol_ptr_ptr; \
2283 cache_ptr->addend = ad - su->datasec->vma; \
2286 case N_BSS | N_EXT: \
2287 cache_ptr->sym_ptr_ptr = obj_bsssec(abfd)->symbol_ptr_ptr; \
2288 cache_ptr->addend = ad - su->bsssec->vma; \
2292 case N_ABS | N_EXT: \
2293 cache_ptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr; \
2294 cache_ptr->addend = ad; \
2300 NAME(aout,swap_ext_reloc_in) (abfd, bytes, cache_ptr, symbols)
2302 struct reloc_ext_external *bytes;
2308 unsigned int r_type;
2309 struct aoutdata *su = &(abfd->tdata.aout_data->a);
2311 cache_ptr->address = (GET_SWORD (abfd, bytes->r_address));
2313 /* now the fun stuff */
2314 if (abfd->xvec->header_byteorder_big_p != false) {
2315 r_index = (bytes->r_index[0] << 16)
2316 | (bytes->r_index[1] << 8)
2317 | bytes->r_index[2];
2318 r_extern = (0 != (bytes->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG));
2319 r_type = (bytes->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
2320 >> RELOC_EXT_BITS_TYPE_SH_BIG;
2322 r_index = (bytes->r_index[2] << 16)
2323 | (bytes->r_index[1] << 8)
2324 | bytes->r_index[0];
2325 r_extern = (0 != (bytes->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
2326 r_type = (bytes->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
2327 >> RELOC_EXT_BITS_TYPE_SH_LITTLE;
2330 cache_ptr->howto = howto_table_ext + r_type;
2331 MOVE_ADDRESS(GET_SWORD(abfd, bytes->r_addend));
2335 NAME(aout,swap_std_reloc_in) (abfd, bytes, cache_ptr, symbols)
2337 struct reloc_std_external *bytes;
2343 unsigned int r_length;
2345 int r_baserel, r_jmptable, r_relative;
2346 struct aoutdata *su = &(abfd->tdata.aout_data->a);
2349 cache_ptr->address = bfd_h_get_32 (abfd, bytes->r_address);
2351 /* now the fun stuff */
2352 if (abfd->xvec->header_byteorder_big_p != false) {
2353 r_index = (bytes->r_index[0] << 16)
2354 | (bytes->r_index[1] << 8)
2355 | bytes->r_index[2];
2356 r_extern = (0 != (bytes->r_type[0] & RELOC_STD_BITS_EXTERN_BIG));
2357 r_pcrel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_PCREL_BIG));
2358 r_baserel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_BASEREL_BIG));
2359 r_jmptable= (0 != (bytes->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG));
2360 r_relative= (0 != (bytes->r_type[0] & RELOC_STD_BITS_RELATIVE_BIG));
2361 r_length = (bytes->r_type[0] & RELOC_STD_BITS_LENGTH_BIG)
2362 >> RELOC_STD_BITS_LENGTH_SH_BIG;
2364 r_index = (bytes->r_index[2] << 16)
2365 | (bytes->r_index[1] << 8)
2366 | bytes->r_index[0];
2367 r_extern = (0 != (bytes->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE));
2368 r_pcrel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
2369 r_baserel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_BASEREL_LITTLE));
2370 r_jmptable= (0 != (bytes->r_type[0] & RELOC_STD_BITS_JMPTABLE_LITTLE));
2371 r_relative= (0 != (bytes->r_type[0] & RELOC_STD_BITS_RELATIVE_LITTLE));
2372 r_length = (bytes->r_type[0] & RELOC_STD_BITS_LENGTH_LITTLE)
2373 >> RELOC_STD_BITS_LENGTH_SH_LITTLE;
2376 howto_idx = r_length + 4 * r_pcrel + 8 * r_baserel
2377 + 16 * r_jmptable + 32 * r_relative;
2378 BFD_ASSERT (howto_idx < TABLE_SIZE (howto_table_std));
2379 cache_ptr->howto = howto_table_std + howto_idx;
2380 BFD_ASSERT (cache_ptr->howto->type != -1);
2385 /* Read and swap the relocs for a section. */
2388 NAME(aout,slurp_reloc_table) (abfd, asect, symbols)
2394 bfd_size_type reloc_size;
2396 arelent *reloc_cache;
2398 unsigned int counter = 0;
2401 if (asect->relocation)
2404 if (asect->flags & SEC_CONSTRUCTOR)
2407 if (asect == obj_datasec (abfd))
2408 reloc_size = exec_hdr(abfd)->a_drsize;
2409 else if (asect == obj_textsec (abfd))
2410 reloc_size = exec_hdr(abfd)->a_trsize;
2413 bfd_set_error (bfd_error_invalid_operation);
2417 if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0)
2420 each_size = obj_reloc_entry_size (abfd);
2422 count = reloc_size / each_size;
2424 reloc_cache = (arelent *) malloc ((size_t) (count * sizeof (arelent)));
2425 if (reloc_cache == NULL && count != 0)
2427 bfd_set_error (bfd_error_no_memory);
2430 memset (reloc_cache, 0, count * sizeof (arelent));
2432 relocs = malloc (reloc_size);
2433 if (relocs == NULL && reloc_size != 0)
2436 bfd_set_error (bfd_error_no_memory);
2440 if (bfd_read (relocs, 1, reloc_size, abfd) != reloc_size)
2447 cache_ptr = reloc_cache;
2448 if (each_size == RELOC_EXT_SIZE)
2450 register struct reloc_ext_external *rptr =
2451 (struct reloc_ext_external *) relocs;
2453 for (; counter < count; counter++, rptr++, cache_ptr++)
2454 NAME(aout,swap_ext_reloc_in) (abfd, rptr, cache_ptr, symbols);
2458 register struct reloc_std_external *rptr =
2459 (struct reloc_std_external *) relocs;
2461 for (; counter < count; counter++, rptr++, cache_ptr++)
2462 NAME(aout,swap_std_reloc_in) (abfd, rptr, cache_ptr, symbols);
2467 asect->relocation = reloc_cache;
2468 asect->reloc_count = cache_ptr - reloc_cache;
2473 /* Write out a relocation section into an object file. */
2476 NAME(aout,squirt_out_relocs) (abfd, section)
2481 unsigned char *native, *natptr;
2484 unsigned int count = section->reloc_count;
2487 if (count == 0) return true;
2489 each_size = obj_reloc_entry_size (abfd);
2490 natsize = each_size * count;
2491 native = (unsigned char *) bfd_zalloc (abfd, natsize);
2493 bfd_set_error (bfd_error_no_memory);
2497 generic = section->orelocation;
2499 if (each_size == RELOC_EXT_SIZE)
2501 for (natptr = native;
2503 --count, natptr += each_size, ++generic)
2504 NAME(aout,swap_ext_reloc_out) (abfd, *generic, (struct reloc_ext_external *)natptr);
2508 for (natptr = native;
2510 --count, natptr += each_size, ++generic)
2511 NAME(aout,swap_std_reloc_out)(abfd, *generic, (struct reloc_std_external *)natptr);
2514 if ( bfd_write ((PTR) native, 1, natsize, abfd) != natsize) {
2515 bfd_release(abfd, native);
2518 bfd_release (abfd, native);
2523 /* This is stupid. This function should be a boolean predicate */
2525 NAME(aout,canonicalize_reloc) (abfd, section, relptr, symbols)
2531 arelent *tblptr = section->relocation;
2534 if (section == obj_bsssec (abfd))
2540 if (!(tblptr || NAME(aout,slurp_reloc_table)(abfd, section, symbols)))
2543 if (section->flags & SEC_CONSTRUCTOR) {
2544 arelent_chain *chain = section->constructor_chain;
2545 for (count = 0; count < section->reloc_count; count ++) {
2546 *relptr ++ = &chain->relent;
2547 chain = chain->next;
2551 tblptr = section->relocation;
2553 for (count = 0; count++ < section->reloc_count;)
2555 *relptr++ = tblptr++;
2560 return section->reloc_count;
2564 NAME(aout,get_reloc_upper_bound) (abfd, asect)
2568 if (bfd_get_format (abfd) != bfd_object) {
2569 bfd_set_error (bfd_error_invalid_operation);
2572 if (asect->flags & SEC_CONSTRUCTOR) {
2573 return (sizeof (arelent *) * (asect->reloc_count+1));
2576 if (asect == obj_datasec (abfd))
2577 return (sizeof (arelent *)
2578 * ((exec_hdr(abfd)->a_drsize / obj_reloc_entry_size (abfd))
2581 if (asect == obj_textsec (abfd))
2582 return (sizeof (arelent *)
2583 * ((exec_hdr(abfd)->a_trsize / obj_reloc_entry_size (abfd))
2586 if (asect == obj_bsssec (abfd))
2587 return sizeof (arelent *);
2589 bfd_set_error (bfd_error_invalid_operation);
2595 NAME(aout,get_symtab_upper_bound) (abfd)
2598 if (!NAME(aout,slurp_symbol_table)(abfd))
2601 return (bfd_get_symcount (abfd)+1) * (sizeof (aout_symbol_type *));
2606 NAME(aout,get_lineno) (ignore_abfd, ignore_symbol)
2608 asymbol *ignore_symbol;
2610 return (alent *)NULL;
2615 NAME(aout,get_symbol_info) (ignore_abfd, symbol, ret)
2620 bfd_symbol_info (symbol, ret);
2622 if (ret->type == '?')
2624 int type_code = aout_symbol(symbol)->type & 0xff;
2625 CONST char *stab_name = aout_stab_name(type_code);
2626 static char buf[10];
2628 if (stab_name == NULL)
2630 sprintf(buf, "(%d)", type_code);
2634 ret->stab_other = (unsigned)(aout_symbol(symbol)->other & 0xff);
2635 ret->stab_desc = (unsigned)(aout_symbol(symbol)->desc & 0xffff);
2636 ret->stab_name = stab_name;
2642 NAME(aout,print_symbol) (ignore_abfd, afile, symbol, how)
2646 bfd_print_symbol_type how;
2648 FILE *file = (FILE *)afile;
2651 case bfd_print_symbol_name:
2653 fprintf(file,"%s", symbol->name);
2655 case bfd_print_symbol_more:
2656 fprintf(file,"%4x %2x %2x",(unsigned)(aout_symbol(symbol)->desc & 0xffff),
2657 (unsigned)(aout_symbol(symbol)->other & 0xff),
2658 (unsigned)(aout_symbol(symbol)->type));
2660 case bfd_print_symbol_all:
2662 CONST char *section_name = symbol->section->name;
2665 bfd_print_symbol_vandf((PTR)file,symbol);
2667 fprintf(file," %-5s %04x %02x %02x",
2669 (unsigned)(aout_symbol(symbol)->desc & 0xffff),
2670 (unsigned)(aout_symbol(symbol)->other & 0xff),
2671 (unsigned)(aout_symbol(symbol)->type & 0xff));
2673 fprintf(file," %s", symbol->name);
2680 provided a BFD, a section and an offset into the section, calculate
2681 and return the name of the source file and the line nearest to the
2686 NAME(aout,find_nearest_line)
2687 (abfd, section, symbols, offset, filename_ptr, functionname_ptr, line_ptr)
2692 CONST char **filename_ptr;
2693 CONST char **functionname_ptr;
2694 unsigned int *line_ptr;
2696 /* Run down the file looking for the filename, function and linenumber */
2698 static char buffer[100];
2699 static char filename_buffer[200];
2700 CONST char *directory_name = NULL;
2701 CONST char *main_file_name = NULL;
2702 CONST char *current_file_name = NULL;
2703 CONST char *line_file_name = NULL; /* Value of current_file_name at line number. */
2704 bfd_vma high_line_vma = ~0;
2705 bfd_vma low_func_vma = 0;
2707 *filename_ptr = abfd->filename;
2708 *functionname_ptr = 0;
2710 if (symbols != (asymbol **)NULL) {
2711 for (p = symbols; *p; p++) {
2712 aout_symbol_type *q = (aout_symbol_type *)(*p);
2716 main_file_name = current_file_name = q->symbol.name;
2717 /* Look ahead to next symbol to check if that too is an N_SO. */
2721 q = (aout_symbol_type *)(*p);
2722 if (q->type != (int)N_SO)
2725 /* Found a second N_SO First is directory; second is filename. */
2726 directory_name = current_file_name;
2727 main_file_name = current_file_name = q->symbol.name;
2728 if (obj_textsec(abfd) != section)
2732 current_file_name = q->symbol.name;
2739 /* We'll keep this if it resolves nearer than the one we have already */
2740 if (q->symbol.value >= offset &&
2741 q->symbol.value < high_line_vma) {
2742 *line_ptr = q->desc;
2743 high_line_vma = q->symbol.value;
2744 line_file_name = current_file_name;
2749 /* We'll keep this if it is nearer than the one we have already */
2750 if (q->symbol.value >= low_func_vma &&
2751 q->symbol.value <= offset) {
2752 low_func_vma = q->symbol.value;
2753 func = (asymbol *)q;
2755 if (*line_ptr && func) {
2756 CONST char *function = func->name;
2759 /* The caller expects a symbol name. We actually have a
2760 function name, without the leading underscore. Put the
2761 underscore back in, so that the caller gets a symbol
2763 if (bfd_get_symbol_leading_char (abfd) == '\0')
2764 strncpy (buffer, function, sizeof (buffer) - 1);
2767 buffer[0] = bfd_get_symbol_leading_char (abfd);
2768 strncpy (buffer + 1, function, sizeof (buffer) - 2);
2770 buffer[sizeof(buffer)-1] = 0;
2771 /* Have to remove : stuff */
2772 p = strchr(buffer,':');
2773 if (p != NULL) { *p = '\0'; }
2774 *functionname_ptr = buffer;
2786 main_file_name = line_file_name;
2787 if (main_file_name) {
2788 if (main_file_name[0] == '/' || directory_name == NULL)
2789 *filename_ptr = main_file_name;
2791 sprintf(filename_buffer, "%.140s%.50s",
2792 directory_name, main_file_name);
2793 *filename_ptr = filename_buffer;
2802 NAME(aout,sizeof_headers) (abfd, execable)
2806 return adata(abfd).exec_bytes_size;
2809 /* Free all information we have cached for this BFD. We can always
2810 read it again later if we need it. */
2813 NAME(aout,bfd_free_cached_info) (abfd)
2818 if (bfd_get_format (abfd) != bfd_object)
2821 #define FREE(x) if (x != NULL) { free (x); x = NULL; }
2822 FREE (obj_aout_symbols (abfd));
2823 FREE (obj_aout_external_syms (abfd));
2824 FREE (obj_aout_external_strings (abfd));
2825 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
2826 FREE (o->relocation);
2832 /* a.out link code. */
2834 static boolean aout_link_add_object_symbols
2835 PARAMS ((bfd *, struct bfd_link_info *));
2836 static boolean aout_link_check_archive_element
2837 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
2838 static boolean aout_link_free_symbols PARAMS ((bfd *));
2839 static boolean aout_link_check_ar_symbols
2840 PARAMS ((bfd *, struct bfd_link_info *, boolean *pneeded));
2841 static boolean aout_link_add_symbols
2842 PARAMS ((bfd *, struct bfd_link_info *));
2844 /* Routine to create an entry in an a.out link hash table. */
2846 struct bfd_hash_entry *
2847 NAME(aout,link_hash_newfunc) (entry, table, string)
2848 struct bfd_hash_entry *entry;
2849 struct bfd_hash_table *table;
2852 struct aout_link_hash_entry *ret = (struct aout_link_hash_entry *) entry;
2854 /* Allocate the structure if it has not already been allocated by a
2856 if (ret == (struct aout_link_hash_entry *) NULL)
2857 ret = ((struct aout_link_hash_entry *)
2858 bfd_hash_allocate (table, sizeof (struct aout_link_hash_entry)));
2859 if (ret == (struct aout_link_hash_entry *) NULL)
2861 bfd_set_error (bfd_error_no_memory);
2862 return (struct bfd_hash_entry *) ret;
2865 /* Call the allocation method of the superclass. */
2866 ret = ((struct aout_link_hash_entry *)
2867 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2871 /* Set local fields. */
2872 ret->written = false;
2876 return (struct bfd_hash_entry *) ret;
2879 /* Initialize an a.out link hash table. */
2882 NAME(aout,link_hash_table_init) (table, abfd, newfunc)
2883 struct aout_link_hash_table *table;
2885 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
2886 struct bfd_hash_table *,
2889 return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
2892 /* Create an a.out link hash table. */
2894 struct bfd_link_hash_table *
2895 NAME(aout,link_hash_table_create) (abfd)
2898 struct aout_link_hash_table *ret;
2900 ret = ((struct aout_link_hash_table *)
2901 malloc (sizeof (struct aout_link_hash_table)));
2902 if (ret == (struct aout_link_hash_table *) NULL)
2904 bfd_set_error (bfd_error_no_memory);
2905 return (struct bfd_link_hash_table *) NULL;
2907 if (! NAME(aout,link_hash_table_init) (ret, abfd,
2908 NAME(aout,link_hash_newfunc)))
2911 return (struct bfd_link_hash_table *) NULL;
2916 /* Given an a.out BFD, add symbols to the global hash table as
2920 NAME(aout,link_add_symbols) (abfd, info)
2922 struct bfd_link_info *info;
2924 switch (bfd_get_format (abfd))
2927 return aout_link_add_object_symbols (abfd, info);
2929 return _bfd_generic_link_add_archive_symbols
2930 (abfd, info, aout_link_check_archive_element);
2932 bfd_set_error (bfd_error_wrong_format);
2937 /* Add symbols from an a.out object file. */
2940 aout_link_add_object_symbols (abfd, info)
2942 struct bfd_link_info *info;
2944 if (! aout_get_external_symbols (abfd))
2946 if (! aout_link_add_symbols (abfd, info))
2948 if (! info->keep_memory)
2950 if (! aout_link_free_symbols (abfd))
2956 /* Check a single archive element to see if we need to include it in
2957 the link. *PNEEDED is set according to whether this element is
2958 needed in the link or not. This is called from
2959 _bfd_generic_link_add_archive_symbols. */
2962 aout_link_check_archive_element (abfd, info, pneeded)
2964 struct bfd_link_info *info;
2967 if (! aout_get_external_symbols (abfd))
2970 if (! aout_link_check_ar_symbols (abfd, info, pneeded))
2975 if (! aout_link_add_symbols (abfd, info))
2979 /* We keep around the symbols even if we aren't going to use this
2980 object file, because we may want to reread it. This doesn't
2981 waste too much memory, because it isn't all that common to read
2982 an archive element but not need it. */
2983 if (! info->keep_memory)
2985 if (! aout_link_free_symbols (abfd))
2992 /* Free up the internal symbols read from an a.out file. */
2995 aout_link_free_symbols (abfd)
2998 if (obj_aout_external_syms (abfd) != (struct external_nlist *) NULL)
3000 free ((PTR) obj_aout_external_syms (abfd));
3001 obj_aout_external_syms (abfd) = (struct external_nlist *) NULL;
3003 if (obj_aout_external_strings (abfd) != (char *) NULL)
3005 free ((PTR) obj_aout_external_strings (abfd));
3006 obj_aout_external_strings (abfd) = (char *) NULL;
3011 /* Look through the internal symbols to see if this object file should
3012 be included in the link. We should include this object file if it
3013 defines any symbols which are currently undefined. If this object
3014 file defines a common symbol, then we may adjust the size of the
3015 known symbol but we do not include the object file in the link
3016 (unless there is some other reason to include it). */
3019 aout_link_check_ar_symbols (abfd, info, pneeded)
3021 struct bfd_link_info *info;
3024 register struct external_nlist *p;
3025 struct external_nlist *pend;
3030 /* Look through all the symbols. */
3031 p = obj_aout_external_syms (abfd);
3032 pend = p + obj_aout_external_sym_count (abfd);
3033 strings = obj_aout_external_strings (abfd);
3034 for (; p < pend; p++)
3036 int type = bfd_h_get_8 (abfd, p->e_type);
3038 struct bfd_link_hash_entry *h;
3040 /* Ignore symbols that are not externally visible. This is an
3041 optimization only, as we check the type more thoroughly
3043 if ((type & N_EXT) == 0
3049 if (type == N_WARNING
3055 name = strings + GET_WORD (abfd, p->e_strx);
3056 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
3058 /* We are only interested in symbols that are currently
3059 undefined or common. */
3060 if (h == (struct bfd_link_hash_entry *) NULL
3061 || (h->type != bfd_link_hash_undefined
3062 && h->type != bfd_link_hash_common))
3064 if (type == (N_INDR | N_EXT))
3069 if (type == (N_TEXT | N_EXT)
3070 || type == (N_DATA | N_EXT)
3071 || type == (N_BSS | N_EXT)
3072 || type == (N_ABS | N_EXT)
3073 || type == (N_INDR | N_EXT))
3075 /* This object file defines this symbol. We must link it
3076 in. This is true regardless of whether the current
3077 definition of the symbol is undefined or common. If the
3078 current definition is common, we have a case in which we
3079 have already seen an object file including
3081 and this object file from the archive includes
3083 In such a case we must include this object file.
3085 FIXME: The SunOS 4.1.3 linker will pull in the archive
3086 element if the symbol is defined in the .data section,
3087 but not if it is defined in the .text section. That
3088 seems a bit crazy to me, and I haven't implemented it.
3089 However, it might be correct. */
3090 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
3096 if (type == (N_UNDF | N_EXT))
3100 value = GET_WORD (abfd, p->e_value);
3103 /* This symbol is common in the object from the archive
3105 if (h->type == bfd_link_hash_undefined)
3109 symbfd = h->u.undef.abfd;
3110 if (symbfd == (bfd *) NULL)
3112 /* This symbol was created as undefined from
3113 outside BFD. We assume that we should link
3114 in the object file. This is done for the -u
3115 option in the linker. */
3116 if (! (*info->callbacks->add_archive_element) (info,
3123 /* Turn the current link symbol into a common
3124 symbol. It is already on the undefs list. */
3125 h->type = bfd_link_hash_common;
3126 h->u.c.size = value;
3127 h->u.c.section = bfd_make_section_old_way (symbfd,
3132 /* Adjust the size of the common symbol if
3134 if (value > h->u.c.size)
3135 h->u.c.size = value;
3145 /* This symbol is weak but defined. We must pull it in if
3146 the current link symbol is undefined, but we don't want
3147 it if the current link symbol is common. */
3148 if (h->type == bfd_link_hash_undefined)
3150 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
3158 /* We do not need this object file. */
3162 /* Add all symbols from an object file to the hash table. */
3165 aout_link_add_symbols (abfd, info)
3167 struct bfd_link_info *info;
3169 boolean (*add_one_symbol) PARAMS ((struct bfd_link_info *, bfd *,
3170 const char *, flagword, asection *,
3171 bfd_vma, const char *, boolean,
3173 struct bfd_link_hash_entry **));
3174 bfd_size_type sym_count;
3177 struct aout_link_hash_entry **sym_hash;
3178 register struct external_nlist *p;
3179 struct external_nlist *pend;
3181 sym_count = obj_aout_external_sym_count (abfd);
3182 strings = obj_aout_external_strings (abfd);
3183 if (info->keep_memory)
3188 /* We keep a list of the linker hash table entries that correspond
3189 to particular symbols. We could just look them up in the hash
3190 table, but keeping the list is more efficient. Perhaps this
3191 should be conditional on info->keep_memory. */
3192 sym_hash = ((struct aout_link_hash_entry **)
3195 * sizeof (struct aout_link_hash_entry *))));
3196 if (sym_hash == NULL && sym_count != 0)
3198 bfd_set_error (bfd_error_no_memory);
3201 obj_aout_sym_hashes (abfd) = sym_hash;
3203 if ((abfd->flags & DYNAMIC) != 0
3204 && aout_backend_info (abfd)->add_dynamic_symbols != NULL)
3206 if (! (*aout_backend_info (abfd)->add_dynamic_symbols) (abfd, info))
3210 add_one_symbol = aout_backend_info (abfd)->add_one_symbol;
3211 if (add_one_symbol == NULL)
3212 add_one_symbol = _bfd_generic_link_add_one_symbol;
3214 p = obj_aout_external_syms (abfd);
3215 pend = p + sym_count;
3216 for (; p < pend; p++, sym_hash++)
3227 type = bfd_h_get_8 (abfd, p->e_type);
3229 /* Ignore debugging symbols. */
3230 if ((type & N_STAB) != 0)
3233 name = strings + GET_WORD (abfd, p->e_strx);
3234 value = GET_WORD (abfd, p->e_value);
3251 /* Ignore symbols that are not externally visible. */
3254 /* Ignore local indirect symbol. */
3259 case N_UNDF | N_EXT:
3262 section = &bfd_und_section;
3266 section = &bfd_com_section;
3269 section = &bfd_abs_section;
3271 case N_TEXT | N_EXT:
3272 section = obj_textsec (abfd);
3273 value -= bfd_get_section_vma (abfd, section);
3275 case N_DATA | N_EXT:
3276 section = obj_datasec (abfd);
3277 value -= bfd_get_section_vma (abfd, section);
3280 section = obj_bsssec (abfd);
3281 value -= bfd_get_section_vma (abfd, section);
3283 case N_INDR | N_EXT:
3284 /* An indirect symbol. The next symbol is the symbol
3285 which this one really is. */
3286 BFD_ASSERT (p + 1 < pend);
3288 string = strings + GET_WORD (abfd, p->e_strx);
3289 section = &bfd_ind_section;
3290 flags |= BSF_INDIRECT;
3292 case N_COMM | N_EXT:
3293 section = &bfd_com_section;
3295 case N_SETA: case N_SETA | N_EXT:
3296 section = &bfd_abs_section;
3297 flags |= BSF_CONSTRUCTOR;
3299 case N_SETT: case N_SETT | N_EXT:
3300 section = obj_textsec (abfd);
3301 flags |= BSF_CONSTRUCTOR;
3302 value -= bfd_get_section_vma (abfd, section);
3304 case N_SETD: case N_SETD | N_EXT:
3305 section = obj_datasec (abfd);
3306 flags |= BSF_CONSTRUCTOR;
3307 value -= bfd_get_section_vma (abfd, section);
3309 case N_SETB: case N_SETB | N_EXT:
3310 section = obj_bsssec (abfd);
3311 flags |= BSF_CONSTRUCTOR;
3312 value -= bfd_get_section_vma (abfd, section);
3315 /* A warning symbol. The next symbol is the one to warn
3317 BFD_ASSERT (p + 1 < pend);
3320 name = strings + GET_WORD (abfd, p->e_strx);
3321 section = &bfd_und_section;
3322 flags |= BSF_WARNING;
3325 section = &bfd_und_section;
3329 section = &bfd_abs_section;
3333 section = obj_textsec (abfd);
3334 value -= bfd_get_section_vma (abfd, section);
3338 section = obj_datasec (abfd);
3339 value -= bfd_get_section_vma (abfd, section);
3343 section = obj_bsssec (abfd);
3344 value -= bfd_get_section_vma (abfd, section);
3349 if (! ((*add_one_symbol)
3350 (info, abfd, name, flags, section, value, string, copy, false,
3351 (struct bfd_link_hash_entry **) sym_hash)))
3354 if (type == (N_INDR | N_EXT) || type == N_WARNING)
3361 /* During the final link step we need to pass around a bunch of
3362 information, so we do it in an instance of this structure. */
3364 struct aout_final_link_info
3366 /* General link information. */
3367 struct bfd_link_info *info;
3370 /* Reloc file positions. */
3371 file_ptr treloff, dreloff;
3372 /* File position of symbols. */
3375 struct stringtab_data strtab;
3378 static boolean aout_link_input_bfd
3379 PARAMS ((struct aout_final_link_info *, bfd *input_bfd));
3380 static boolean aout_link_write_symbols
3381 PARAMS ((struct aout_final_link_info *, bfd *input_bfd, int *symbol_map));
3382 static boolean aout_link_write_other_symbol
3383 PARAMS ((struct aout_link_hash_entry *, PTR));
3384 static boolean aout_link_input_section
3385 PARAMS ((struct aout_final_link_info *, bfd *input_bfd,
3386 asection *input_section, file_ptr *reloff_ptr,
3387 bfd_size_type rel_size, int *symbol_map));
3388 static boolean aout_link_input_section_std
3389 PARAMS ((struct aout_final_link_info *, bfd *input_bfd,
3390 asection *input_section, struct reloc_std_external *,
3391 bfd_size_type rel_size, bfd_byte *contents, int *symbol_map));
3392 static boolean aout_link_input_section_ext
3393 PARAMS ((struct aout_final_link_info *, bfd *input_bfd,
3394 asection *input_section, struct reloc_ext_external *,
3395 bfd_size_type rel_size, bfd_byte *contents, int *symbol_map));
3396 static INLINE asection *aout_reloc_index_to_section
3397 PARAMS ((bfd *, int));
3398 static boolean aout_link_reloc_link_order
3399 PARAMS ((struct aout_final_link_info *, asection *,
3400 struct bfd_link_order *));
3402 /* Do the final link step. This is called on the output BFD. The
3403 INFO structure should point to a list of BFDs linked through the
3404 link_next field which can be used to find each BFD which takes part
3405 in the output. Also, each section in ABFD should point to a list
3406 of bfd_link_order structures which list all the input sections for
3407 the output section. */
3410 NAME(aout,final_link) (abfd, info, callback)
3412 struct bfd_link_info *info;
3413 void (*callback) PARAMS ((bfd *, file_ptr *, file_ptr *, file_ptr *));
3415 struct aout_final_link_info aout_info;
3417 bfd_size_type text_size;
3419 register struct bfd_link_order *p;
3421 boolean have_link_order_relocs;
3423 aout_info.info = info;
3424 aout_info.output_bfd = abfd;
3426 if (! info->relocateable)
3428 exec_hdr (abfd)->a_trsize = 0;
3429 exec_hdr (abfd)->a_drsize = 0;
3433 bfd_size_type trsize, drsize;
3435 /* Count up the relocation sizes. */
3438 for (sub = info->input_bfds; sub != (bfd *) NULL; sub = sub->link_next)
3440 if (bfd_get_flavour (abfd) == bfd_target_aout_flavour)
3442 trsize += exec_hdr (sub)->a_trsize;
3443 drsize += exec_hdr (sub)->a_drsize;
3447 /* FIXME: We need to identify the .text and .data sections
3448 and call get_reloc_upper_bound and canonicalize_reloc to
3449 work out the number of relocs needed, and then multiply
3450 by the reloc size. */
3454 if (obj_textsec (abfd) != (asection *) NULL)
3455 trsize += (_bfd_count_link_order_relocs (obj_textsec (abfd)
3457 * obj_reloc_entry_size (abfd));
3458 exec_hdr (abfd)->a_trsize = trsize;
3459 if (obj_datasec (abfd) != (asection *) NULL)
3460 drsize += (_bfd_count_link_order_relocs (obj_datasec (abfd)
3462 * obj_reloc_entry_size (abfd));
3463 exec_hdr (abfd)->a_drsize = drsize;
3466 exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd);
3468 /* Adjust the section sizes and vmas according to the magic number.
3469 This sets a_text, a_data and a_bss in the exec_hdr and sets the
3470 filepos for each section. */
3471 if (! NAME(aout,adjust_sizes_and_vmas) (abfd, &text_size, &text_end))
3474 /* The relocation and symbol file positions differ among a.out
3475 targets. We are passed a callback routine from the backend
3476 specific code to handle this.
3477 FIXME: At this point we do not know how much space the symbol
3478 table will require. This will not work for any (nonstandard)
3479 a.out target that needs to know the symbol table size before it
3480 can compute the relocation file positions. This may or may not
3481 be the case for the hp300hpux target, for example. */
3482 (*callback) (abfd, &aout_info.treloff, &aout_info.dreloff,
3484 obj_textsec (abfd)->rel_filepos = aout_info.treloff;
3485 obj_datasec (abfd)->rel_filepos = aout_info.dreloff;
3486 obj_sym_filepos (abfd) = aout_info.symoff;
3488 /* We keep a count of the symbols as we output them. */
3489 obj_aout_external_sym_count (abfd) = 0;
3491 /* We accumulate the string table as we write out the symbols. */
3492 stringtab_init (&aout_info.strtab);
3494 /* The most time efficient way to do the link would be to read all
3495 the input object files into memory and then sort out the
3496 information into the output file. Unfortunately, that will
3497 probably use too much memory. Another method would be to step
3498 through everything that composes the text section and write it
3499 out, and then everything that composes the data section and write
3500 it out, and then write out the relocs, and then write out the
3501 symbols. Unfortunately, that requires reading stuff from each
3502 input file several times, and we will not be able to keep all the
3503 input files open simultaneously, and reopening them will be slow.
3505 What we do is basically process one input file at a time. We do
3506 everything we need to do with an input file once--copy over the
3507 section contents, handle the relocation information, and write
3508 out the symbols--and then we throw away the information we read
3509 from it. This approach requires a lot of lseeks of the output
3510 file, which is unfortunate but still faster than reopening a lot
3513 We use the output_has_begun field of the input BFDs to see
3514 whether we have already handled it. */
3515 for (sub = info->input_bfds; sub != (bfd *) NULL; sub = sub->link_next)
3516 sub->output_has_begun = false;
3518 have_link_order_relocs = false;
3519 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3521 for (p = o->link_order_head;
3522 p != (struct bfd_link_order *) NULL;
3525 if (p->type == bfd_indirect_link_order
3526 && (bfd_get_flavour (p->u.indirect.section->owner)
3527 == bfd_target_aout_flavour))
3531 input_bfd = p->u.indirect.section->owner;
3532 if (! input_bfd->output_has_begun)
3534 if (! aout_link_input_bfd (&aout_info, input_bfd))
3536 input_bfd->output_has_begun = true;
3539 else if (p->type == bfd_section_reloc_link_order
3540 || p->type == bfd_symbol_reloc_link_order)
3542 /* These are handled below. */
3543 have_link_order_relocs = true;
3547 if (! _bfd_default_link_order (abfd, info, o, p))
3553 /* Write out any symbols that we have not already written out. */
3554 aout_link_hash_traverse (aout_hash_table (info),
3555 aout_link_write_other_symbol,
3558 /* Now handle any relocs we were asked to create by the linker.
3559 These did not come from any input file. We must do these after
3560 we have written out all the symbols, so that we know the symbol
3562 if (have_link_order_relocs)
3564 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3566 for (p = o->link_order_head;
3567 p != (struct bfd_link_order *) NULL;
3570 if (p->type == bfd_section_reloc_link_order
3571 || p->type == bfd_symbol_reloc_link_order)
3573 if (! aout_link_reloc_link_order (&aout_info, o, p))
3580 /* Finish up any dynamic linking we may be doing. */
3581 if (aout_backend_info (abfd)->finish_dynamic_link != NULL)
3583 if (! (*aout_backend_info (abfd)->finish_dynamic_link) (abfd, info))
3587 /* Update the header information. */
3588 abfd->symcount = obj_aout_external_sym_count (abfd);
3589 exec_hdr (abfd)->a_syms = abfd->symcount * EXTERNAL_NLIST_SIZE;
3590 obj_str_filepos (abfd) = obj_sym_filepos (abfd) + exec_hdr (abfd)->a_syms;
3591 obj_textsec (abfd)->reloc_count =
3592 exec_hdr (abfd)->a_trsize / obj_reloc_entry_size (abfd);
3593 obj_datasec (abfd)->reloc_count =
3594 exec_hdr (abfd)->a_drsize / obj_reloc_entry_size (abfd);
3596 /* Write out the string table. */
3597 if (bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET) != 0)
3599 return emit_strtab (abfd, &aout_info.strtab);
3602 /* Link an a.out input BFD into the output file. */
3605 aout_link_input_bfd (finfo, input_bfd)
3606 struct aout_final_link_info *finfo;
3609 bfd_size_type sym_count;
3610 int *symbol_map = NULL;
3612 BFD_ASSERT (bfd_get_format (input_bfd) == bfd_object);
3614 /* If this is a dynamic object, it may need special handling. */
3615 if ((input_bfd->flags & DYNAMIC) != 0
3616 && aout_backend_info (input_bfd)->link_dynamic_object != NULL)
3618 return ((*aout_backend_info (input_bfd)->link_dynamic_object)
3619 (finfo->info, input_bfd));
3622 /* Get the symbols. We probably have them already, unless
3623 finfo->info->keep_memory is false. */
3624 if (! aout_get_external_symbols (input_bfd))
3627 sym_count = obj_aout_external_sym_count (input_bfd);
3628 symbol_map = (int *) malloc ((size_t) sym_count * sizeof (int));
3629 if (symbol_map == NULL && sym_count != 0)
3631 bfd_set_error (bfd_error_no_memory);
3635 /* Write out the symbols and get a map of the new indices. */
3636 if (! aout_link_write_symbols (finfo, input_bfd, symbol_map))
3639 /* Relocate and write out the sections. */
3640 if (! aout_link_input_section (finfo, input_bfd,
3641 obj_textsec (input_bfd),
3643 exec_hdr (input_bfd)->a_trsize,
3645 || ! aout_link_input_section (finfo, input_bfd,
3646 obj_datasec (input_bfd),
3648 exec_hdr (input_bfd)->a_drsize,
3652 /* If we are not keeping memory, we don't need the symbols any
3653 longer. We still need them if we are keeping memory, because the
3654 strings in the hash table point into them. */
3655 if (! finfo->info->keep_memory)
3657 if (! aout_link_free_symbols (input_bfd))
3661 if (symbol_map != NULL)
3665 if (symbol_map != NULL)
3670 /* Adjust and write out the symbols for an a.out file. Set the new
3671 symbol indices into a symbol_map. */
3674 aout_link_write_symbols (finfo, input_bfd, symbol_map)
3675 struct aout_final_link_info *finfo;
3680 bfd_size_type sym_count;
3682 enum bfd_link_strip strip;
3683 enum bfd_link_discard discard;
3684 struct external_nlist *output_syms = NULL;
3685 struct external_nlist *outsym;
3686 register struct external_nlist *sym;
3687 struct external_nlist *sym_end;
3688 struct aout_link_hash_entry **sym_hash;
3690 boolean skip_indirect;
3692 output_bfd = finfo->output_bfd;
3693 sym_count = obj_aout_external_sym_count (input_bfd);
3694 strings = obj_aout_external_strings (input_bfd);
3695 strip = finfo->info->strip;
3696 discard = finfo->info->discard;
3697 output_syms = ((struct external_nlist *)
3698 malloc ((size_t) (sym_count + 1) * EXTERNAL_NLIST_SIZE));
3699 if (output_syms == NULL)
3701 bfd_set_error (bfd_error_no_memory);
3704 outsym = output_syms;
3706 /* First write out a symbol for this object file, unless we are
3707 discarding such symbols. */
3708 if (strip != strip_all
3709 && (strip != strip_some
3710 || bfd_hash_lookup (finfo->info->keep_hash, input_bfd->filename,
3711 false, false) != NULL)
3712 && discard != discard_all)
3714 bfd_h_put_8 (output_bfd, N_TEXT, outsym->e_type);
3715 bfd_h_put_8 (output_bfd, 0, outsym->e_other);
3716 bfd_h_put_16 (output_bfd, (bfd_vma) 0, outsym->e_desc);
3717 PUT_WORD (output_bfd,
3718 add_to_stringtab (output_bfd, input_bfd->filename,
3721 PUT_WORD (output_bfd,
3722 (bfd_get_section_vma (output_bfd,
3723 obj_textsec (input_bfd)->output_section)
3724 + obj_textsec (input_bfd)->output_offset),
3726 ++obj_aout_external_sym_count (output_bfd);
3731 skip_indirect = false;
3732 sym = obj_aout_external_syms (input_bfd);
3733 sym_end = sym + sym_count;
3734 sym_hash = obj_aout_sym_hashes (input_bfd);
3735 for (; sym < sym_end; sym++, sym_hash++, symbol_map++)
3739 struct aout_link_hash_entry *h;
3746 type = bfd_h_get_8 (input_bfd, sym->e_type);
3747 name = strings + GET_WORD (input_bfd, sym->e_strx);
3753 /* Pass this symbol through. It is the target of an
3754 indirect or warning symbol. */
3755 val = GET_WORD (input_bfd, sym->e_value);
3758 else if (skip_indirect)
3760 /* Skip this symbol, which is the target of an indirect
3761 symbol that we have changed to no longer be an indirect
3763 skip_indirect = false;
3768 struct aout_link_hash_entry *hresolve;
3770 /* We have saved the hash table entry for this symbol, if
3771 there is one. Note that we could just look it up again
3772 in the hash table, provided we first check that it is an
3776 /* If this is an indirect or warning symbol, then change
3777 hresolve to the base symbol. We also change *sym_hash so
3778 that the relocation routines relocate against the real
3781 if (h != (struct aout_link_hash_entry *) NULL
3782 && (h->root.type == bfd_link_hash_indirect
3783 || h->root.type == bfd_link_hash_warning))
3785 hresolve = (struct aout_link_hash_entry *) h->root.u.i.link;
3786 while (hresolve->root.type == bfd_link_hash_indirect
3787 || hresolve->root.type == bfd_link_hash_warning)
3788 hresolve = ((struct aout_link_hash_entry *)
3789 hresolve->root.u.i.link);
3790 *sym_hash = hresolve;
3793 /* If the symbol has already been written out, skip it. */
3794 if (h != (struct aout_link_hash_entry *) NULL
3795 && h->root.type != bfd_link_hash_warning
3798 if ((type & N_TYPE) == N_INDR)
3799 skip_indirect = true;
3800 *symbol_map = h->indx;
3804 /* See if we are stripping this symbol. */
3810 case strip_debugger:
3811 if ((type & N_STAB) != 0)
3815 if (bfd_hash_lookup (finfo->info->keep_hash, name, false, false)
3825 if (h != (struct aout_link_hash_entry *) NULL)
3830 /* Get the value of the symbol. */
3831 if ((type & N_TYPE) == N_TEXT
3833 symsec = obj_textsec (input_bfd);
3834 else if ((type & N_TYPE) == N_DATA
3836 symsec = obj_datasec (input_bfd);
3837 else if ((type & N_TYPE) == N_BSS
3839 symsec = obj_bsssec (input_bfd);
3840 else if ((type & N_TYPE) == N_ABS
3842 symsec = &bfd_abs_section;
3843 else if (((type & N_TYPE) == N_INDR
3844 && (hresolve == (struct aout_link_hash_entry *) NULL
3845 || (hresolve->root.type != bfd_link_hash_defined
3846 && hresolve->root.type != bfd_link_hash_common)))
3847 || type == N_WARNING)
3849 /* Pass the next symbol through unchanged. The
3850 condition above for indirect symbols is so that if
3851 the indirect symbol was defined, we output it with
3852 the correct definition so the debugger will
3855 val = GET_WORD (input_bfd, sym->e_value);
3858 else if ((type & N_STAB) != 0)
3860 val = GET_WORD (input_bfd, sym->e_value);
3865 /* If we get here with an indirect symbol, it means that
3866 we are outputting it with a real definition. In such
3867 a case we do not want to output the next symbol,
3868 which is the target of the indirection. */
3869 if ((type & N_TYPE) == N_INDR)
3870 skip_indirect = true;
3872 /* We need to get the value from the hash table. We use
3873 hresolve so that if we have defined an indirect
3874 symbol we output the final definition. */
3875 if (h == (struct aout_link_hash_entry *) NULL)
3877 else if (hresolve->root.type == bfd_link_hash_defined)
3879 asection *input_section;
3880 asection *output_section;
3882 /* This case means a common symbol which was turned
3883 into a defined symbol. */
3884 input_section = hresolve->root.u.def.section;
3885 output_section = input_section->output_section;
3886 BFD_ASSERT (output_section == &bfd_abs_section
3887 || output_section->owner == output_bfd);
3888 val = (hresolve->root.u.def.value
3889 + bfd_get_section_vma (output_bfd, output_section)
3890 + input_section->output_offset);
3892 /* Get the correct type based on the section. If
3893 this is a constructed set, force it to be
3894 globally visible. */
3903 if (output_section == obj_textsec (output_bfd))
3905 else if (output_section == obj_datasec (output_bfd))
3907 else if (output_section == obj_bsssec (output_bfd))
3912 else if (hresolve->root.type == bfd_link_hash_common)
3913 val = hresolve->root.u.c.size;
3914 else if (hresolve->root.type == bfd_link_hash_weak)
3924 if (symsec != (asection *) NULL)
3925 val = (symsec->output_section->vma
3926 + symsec->output_offset
3927 + (GET_WORD (input_bfd, sym->e_value)
3930 /* If this is a global symbol set the written flag, and if
3931 it is a local symbol see if we should discard it. */
3932 if (h != (struct aout_link_hash_entry *) NULL)
3935 h->indx = obj_aout_external_sym_count (output_bfd);
3944 if (*name == *finfo->info->lprefix
3945 && (finfo->info->lprefix_len == 1
3946 || strncmp (name, finfo->info->lprefix,
3947 finfo->info->lprefix_len) == 0))
3962 /* Copy this symbol into the list of symbols we are going to
3964 bfd_h_put_8 (output_bfd, type, outsym->e_type);
3965 bfd_h_put_8 (output_bfd, bfd_h_get_8 (input_bfd, sym->e_other),
3967 bfd_h_put_16 (output_bfd, bfd_h_get_16 (input_bfd, sym->e_desc),
3969 if (! finfo->info->keep_memory)
3971 /* name points into a string table which we are going to
3972 free. If there is a hash table entry, use that string.
3973 Otherwise, copy name into memory. */
3974 if (h != (struct aout_link_hash_entry *) NULL)
3975 name = (*sym_hash)->root.root.string;
3980 n = bfd_alloc (output_bfd, strlen (name) + 1);
3985 PUT_WORD (output_bfd,
3986 add_to_stringtab (output_bfd, name, &finfo->strtab),
3988 PUT_WORD (output_bfd, val, outsym->e_value);
3989 *symbol_map = obj_aout_external_sym_count (output_bfd);
3990 ++obj_aout_external_sym_count (output_bfd);
3994 /* Write out the output symbols we have just constructed. */
3995 if (outsym > output_syms)
3997 bfd_size_type outsym_count;
3999 if (bfd_seek (output_bfd, finfo->symoff, SEEK_SET) != 0)
4001 outsym_count = outsym - output_syms;
4002 if (bfd_write ((PTR) output_syms, (bfd_size_type) EXTERNAL_NLIST_SIZE,
4003 (bfd_size_type) outsym_count, output_bfd)
4004 != outsym_count * EXTERNAL_NLIST_SIZE)
4006 finfo->symoff += outsym_count * EXTERNAL_NLIST_SIZE;
4009 if (output_syms != NULL)
4013 if (output_syms != NULL)
4018 /* Write out a symbol that was not associated with an a.out input
4022 aout_link_write_other_symbol (h, data)
4023 struct aout_link_hash_entry *h;
4026 struct aout_final_link_info *finfo = (struct aout_final_link_info *) data;
4030 struct external_nlist outsym;
4032 output_bfd = finfo->output_bfd;
4034 if (aout_backend_info (output_bfd)->write_dynamic_symbol != NULL)
4036 if (! ((*aout_backend_info (output_bfd)->write_dynamic_symbol)
4037 (output_bfd, finfo->info, h)))
4039 /* FIXME: No way to handle errors. */
4049 if (finfo->info->strip == strip_all
4050 || (finfo->info->strip == strip_some
4051 && bfd_hash_lookup (finfo->info->keep_hash, h->root.root.string,
4052 false, false) == NULL))
4055 switch (h->root.type)
4058 case bfd_link_hash_new:
4060 /* Avoid variable not initialized warnings. */
4062 case bfd_link_hash_undefined:
4063 type = N_UNDF | N_EXT;
4066 case bfd_link_hash_defined:
4070 sec = h->root.u.def.section->output_section;
4071 BFD_ASSERT (sec == &bfd_abs_section
4072 || sec->owner == output_bfd);
4073 if (sec == obj_textsec (output_bfd))
4074 type = N_TEXT | N_EXT;
4075 else if (sec == obj_datasec (output_bfd))
4076 type = N_DATA | N_EXT;
4077 else if (sec == obj_bsssec (output_bfd))
4078 type = N_BSS | N_EXT;
4080 type = N_ABS | N_EXT;
4081 val = (h->root.u.def.value
4083 + h->root.u.def.section->output_offset);
4086 case bfd_link_hash_common:
4087 type = N_UNDF | N_EXT;
4088 val = h->root.u.c.size;
4090 case bfd_link_hash_weak:
4093 case bfd_link_hash_indirect:
4094 case bfd_link_hash_warning:
4095 /* FIXME: Ignore these for now. The circumstances under which
4096 they should be written out are not clear to me. */
4100 bfd_h_put_8 (output_bfd, type, outsym.e_type);
4101 bfd_h_put_8 (output_bfd, 0, outsym.e_other);
4102 bfd_h_put_16 (output_bfd, 0, outsym.e_desc);
4103 PUT_WORD (output_bfd,
4104 add_to_stringtab (output_bfd, h->root.root.string, &finfo->strtab),
4106 PUT_WORD (output_bfd, val, outsym.e_value);
4108 if (bfd_seek (output_bfd, finfo->symoff, SEEK_SET) != 0
4109 || bfd_write ((PTR) &outsym, (bfd_size_type) EXTERNAL_NLIST_SIZE,
4110 (bfd_size_type) 1, output_bfd) != EXTERNAL_NLIST_SIZE)
4112 /* FIXME: No way to handle errors. */
4116 finfo->symoff += EXTERNAL_NLIST_SIZE;
4117 h->indx = obj_aout_external_sym_count (output_bfd);
4118 ++obj_aout_external_sym_count (output_bfd);
4123 /* Link an a.out section into the output file. */
4126 aout_link_input_section (finfo, input_bfd, input_section, reloff_ptr,
4127 rel_size, symbol_map)
4128 struct aout_final_link_info *finfo;
4130 asection *input_section;
4131 file_ptr *reloff_ptr;
4132 bfd_size_type rel_size;
4135 bfd_size_type input_size;
4136 bfd_byte *contents = NULL;
4138 PTR free_relocs = NULL;
4140 /* Get the section contents. */
4141 input_size = bfd_section_size (input_bfd, input_section);
4142 contents = (bfd_byte *) malloc (input_size);
4143 if (contents == NULL && input_size != 0)
4145 bfd_set_error (bfd_error_no_memory);
4148 if (! bfd_get_section_contents (input_bfd, input_section, (PTR) contents,
4149 (file_ptr) 0, input_size))
4152 /* Read in the relocs if we haven't already done it. */
4153 if (aout_section_data (input_section) != NULL
4154 && aout_section_data (input_section)->relocs != NULL)
4155 relocs = aout_section_data (input_section)->relocs;
4158 relocs = free_relocs = (PTR) malloc (rel_size);
4159 if (relocs == NULL && rel_size != 0)
4161 bfd_set_error (bfd_error_no_memory);
4164 if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0
4165 || bfd_read (relocs, 1, rel_size, input_bfd) != rel_size)
4169 /* Relocate the section contents. */
4170 if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE)
4172 if (! aout_link_input_section_std (finfo, input_bfd, input_section,
4173 (struct reloc_std_external *) relocs,
4174 rel_size, contents, symbol_map))
4179 if (! aout_link_input_section_ext (finfo, input_bfd, input_section,
4180 (struct reloc_ext_external *) relocs,
4181 rel_size, contents, symbol_map))
4185 /* Write out the section contents. */
4186 if (! bfd_set_section_contents (finfo->output_bfd,
4187 input_section->output_section,
4189 input_section->output_offset,
4193 /* If we are producing relocateable output, the relocs were
4194 modified, and we now write them out. */
4195 if (finfo->info->relocateable)
4197 if (bfd_seek (finfo->output_bfd, *reloff_ptr, SEEK_SET) != 0)
4199 if (bfd_write (relocs, (bfd_size_type) 1, rel_size, finfo->output_bfd)
4202 *reloff_ptr += rel_size;
4204 /* Assert that the relocs have not run into the symbols, and
4205 that if these are the text relocs they have not run into the
4207 BFD_ASSERT (*reloff_ptr <= obj_sym_filepos (finfo->output_bfd)
4208 && (reloff_ptr != &finfo->treloff
4210 <= obj_datasec (finfo->output_bfd)->rel_filepos)));
4213 if (free_relocs != NULL)
4215 if (contents != NULL)
4219 if (free_relocs != NULL)
4221 if (contents != NULL)
4226 /* Get the section corresponding to a reloc index. */
4228 static INLINE asection *
4229 aout_reloc_index_to_section (abfd, indx)
4233 switch (indx & N_TYPE)
4236 return obj_textsec (abfd);
4238 return obj_datasec (abfd);
4240 return obj_bsssec (abfd);
4243 return &bfd_abs_section;
4249 /* Relocate an a.out section using standard a.out relocs. */
4252 aout_link_input_section_std (finfo, input_bfd, input_section, relocs,
4253 rel_size, contents, symbol_map)
4254 struct aout_final_link_info *finfo;
4256 asection *input_section;
4257 struct reloc_std_external *relocs;
4258 bfd_size_type rel_size;
4262 boolean (*check_dynamic_reloc) PARAMS ((struct bfd_link_info *,
4264 struct aout_link_hash_entry *,
4267 boolean relocateable;
4268 struct external_nlist *syms;
4270 struct aout_link_hash_entry **sym_hashes;
4271 bfd_size_type reloc_count;
4272 register struct reloc_std_external *rel;
4273 struct reloc_std_external *rel_end;
4275 output_bfd = finfo->output_bfd;
4276 check_dynamic_reloc = aout_backend_info (output_bfd)->check_dynamic_reloc;
4278 BFD_ASSERT (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE);
4279 BFD_ASSERT (input_bfd->xvec->header_byteorder_big_p
4280 == output_bfd->xvec->header_byteorder_big_p);
4282 relocateable = finfo->info->relocateable;
4283 syms = obj_aout_external_syms (input_bfd);
4284 strings = obj_aout_external_strings (input_bfd);
4285 sym_hashes = obj_aout_sym_hashes (input_bfd);
4287 reloc_count = rel_size / RELOC_STD_SIZE;
4289 rel_end = rel + reloc_count;
4290 for (; rel < rel_end; rel++)
4302 bfd_reloc_status_type r;
4304 r_addr = GET_SWORD (input_bfd, rel->r_address);
4306 if (input_bfd->xvec->header_byteorder_big_p)
4308 r_index = ((rel->r_index[0] << 16)
4309 | (rel->r_index[1] << 8)
4311 r_extern = (0 != (rel->r_type[0] & RELOC_STD_BITS_EXTERN_BIG));
4312 r_pcrel = (0 != (rel->r_type[0] & RELOC_STD_BITS_PCREL_BIG));
4313 r_baserel = (0 != (rel->r_type[0] & RELOC_STD_BITS_BASEREL_BIG));
4314 r_jmptable= (0 != (rel->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG));
4315 r_relative= (0 != (rel->r_type[0] & RELOC_STD_BITS_RELATIVE_BIG));
4316 r_length = ((rel->r_type[0] & RELOC_STD_BITS_LENGTH_BIG)
4317 >> RELOC_STD_BITS_LENGTH_SH_BIG);
4321 r_index = ((rel->r_index[2] << 16)
4322 | (rel->r_index[1] << 8)
4324 r_extern = (0 != (rel->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE));
4325 r_pcrel = (0 != (rel->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
4326 r_baserel = (0 != (rel->r_type[0] & RELOC_STD_BITS_BASEREL_LITTLE));
4327 r_jmptable= (0 != (rel->r_type[0] & RELOC_STD_BITS_JMPTABLE_LITTLE));
4328 r_relative= (0 != (rel->r_type[0] & RELOC_STD_BITS_RELATIVE_LITTLE));
4329 r_length = ((rel->r_type[0] & RELOC_STD_BITS_LENGTH_LITTLE)
4330 >> RELOC_STD_BITS_LENGTH_SH_LITTLE);
4333 howto_idx = r_length + 4 * r_pcrel + 8 * r_baserel
4334 + 16 * r_jmptable + 32 * r_relative;
4335 BFD_ASSERT (howto_idx < TABLE_SIZE (howto_table_std));
4339 /* We are generating a relocateable output file, and must
4340 modify the reloc accordingly. */
4343 struct aout_link_hash_entry *h;
4345 /* If we know the symbol this relocation is against,
4346 convert it into a relocation against a section. This
4347 is what the native linker does. */
4348 h = sym_hashes[r_index];
4349 if (h != (struct aout_link_hash_entry *) NULL
4350 && h->root.type == bfd_link_hash_defined)
4352 asection *output_section;
4354 /* Change the r_extern value. */
4355 if (output_bfd->xvec->header_byteorder_big_p)
4356 rel->r_type[0] &=~ RELOC_STD_BITS_EXTERN_BIG;
4358 rel->r_type[0] &=~ RELOC_STD_BITS_EXTERN_LITTLE;
4360 /* Compute a new r_index. */
4361 output_section = h->root.u.def.section->output_section;
4362 if (output_section == obj_textsec (output_bfd))
4364 else if (output_section == obj_datasec (output_bfd))
4366 else if (output_section == obj_bsssec (output_bfd))
4371 /* Add the symbol value and the section VMA to the
4372 addend stored in the contents. */
4373 relocation = (h->root.u.def.value
4374 + output_section->vma
4375 + h->root.u.def.section->output_offset);
4379 /* We must change r_index according to the symbol
4381 r_index = symbol_map[r_index];
4387 name = strings + GET_WORD (input_bfd,
4388 syms[r_index].e_strx);
4389 if (! ((*finfo->info->callbacks->unattached_reloc)
4390 (finfo->info, name, input_bfd, input_section,
4399 /* Write out the new r_index value. */
4400 if (output_bfd->xvec->header_byteorder_big_p)
4402 rel->r_index[0] = r_index >> 16;
4403 rel->r_index[1] = r_index >> 8;
4404 rel->r_index[2] = r_index;
4408 rel->r_index[2] = r_index >> 16;
4409 rel->r_index[1] = r_index >> 8;
4410 rel->r_index[0] = r_index;
4417 /* This is a relocation against a section. We must
4418 adjust by the amount that the section moved. */
4419 section = aout_reloc_index_to_section (input_bfd, r_index);
4420 relocation = (section->output_section->vma
4421 + section->output_offset
4425 /* Change the address of the relocation. */
4426 PUT_WORD (output_bfd,
4427 r_addr + input_section->output_offset,
4430 /* Adjust a PC relative relocation by removing the reference
4431 to the original address in the section and including the
4432 reference to the new address. */
4434 relocation -= (input_section->output_section->vma
4435 + input_section->output_offset
4436 - input_section->vma);
4438 if (relocation == 0)
4441 r = _bfd_relocate_contents (howto_table_std + howto_idx,
4442 input_bfd, relocation,
4447 /* We are generating an executable, and must do a full
4451 struct aout_link_hash_entry *h;
4453 h = sym_hashes[r_index];
4455 if (check_dynamic_reloc != NULL)
4459 if (! ((*check_dynamic_reloc)
4460 (finfo->info, input_bfd, input_section, h,
4467 if (h != (struct aout_link_hash_entry *) NULL
4468 && h->root.type == bfd_link_hash_defined)
4470 relocation = (h->root.u.def.value
4471 + h->root.u.def.section->output_section->vma
4472 + h->root.u.def.section->output_offset);
4474 else if (h != (struct aout_link_hash_entry *) NULL
4475 && h->root.type == bfd_link_hash_weak)
4481 name = strings + GET_WORD (input_bfd, syms[r_index].e_strx);
4482 if (! ((*finfo->info->callbacks->undefined_symbol)
4483 (finfo->info, name, input_bfd, input_section,
4493 section = aout_reloc_index_to_section (input_bfd, r_index);
4494 relocation = (section->output_section->vma
4495 + section->output_offset
4498 relocation += input_section->vma;
4501 r = _bfd_final_link_relocate (howto_table_std + howto_idx,
4502 input_bfd, input_section,
4503 contents, r_addr, relocation,
4507 if (r != bfd_reloc_ok)
4512 case bfd_reloc_outofrange:
4514 case bfd_reloc_overflow:
4519 name = strings + GET_WORD (input_bfd,
4520 syms[r_index].e_strx);
4525 s = aout_reloc_index_to_section (input_bfd, r_index);
4526 name = bfd_section_name (input_bfd, s);
4528 if (! ((*finfo->info->callbacks->reloc_overflow)
4529 (finfo->info, name, howto_table_std[howto_idx].name,
4530 (bfd_vma) 0, input_bfd, input_section, r_addr)))
4541 /* Relocate an a.out section using extended a.out relocs. */
4544 aout_link_input_section_ext (finfo, input_bfd, input_section, relocs,
4545 rel_size, contents, symbol_map)
4546 struct aout_final_link_info *finfo;
4548 asection *input_section;
4549 struct reloc_ext_external *relocs;
4550 bfd_size_type rel_size;
4554 boolean (*check_dynamic_reloc) PARAMS ((struct bfd_link_info *,
4556 struct aout_link_hash_entry *,
4559 boolean relocateable;
4560 struct external_nlist *syms;
4562 struct aout_link_hash_entry **sym_hashes;
4563 bfd_size_type reloc_count;
4564 register struct reloc_ext_external *rel;
4565 struct reloc_ext_external *rel_end;
4567 output_bfd = finfo->output_bfd;
4568 check_dynamic_reloc = aout_backend_info (output_bfd)->check_dynamic_reloc;
4570 BFD_ASSERT (obj_reloc_entry_size (input_bfd) == RELOC_EXT_SIZE);
4571 BFD_ASSERT (input_bfd->xvec->header_byteorder_big_p
4572 == output_bfd->xvec->header_byteorder_big_p);
4574 relocateable = finfo->info->relocateable;
4575 syms = obj_aout_external_syms (input_bfd);
4576 strings = obj_aout_external_strings (input_bfd);
4577 sym_hashes = obj_aout_sym_hashes (input_bfd);
4579 reloc_count = rel_size / RELOC_EXT_SIZE;
4581 rel_end = rel + reloc_count;
4582 for (; rel < rel_end; rel++)
4591 r_addr = GET_SWORD (input_bfd, rel->r_address);
4593 if (input_bfd->xvec->header_byteorder_big_p)
4595 r_index = ((rel->r_index[0] << 16)
4596 | (rel->r_index[1] << 8)
4598 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG));
4599 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
4600 >> RELOC_EXT_BITS_TYPE_SH_BIG);
4604 r_index = ((rel->r_index[2] << 16)
4605 | (rel->r_index[1] << 8)
4607 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
4608 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
4609 >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
4612 r_addend = GET_SWORD (input_bfd, rel->r_addend);
4614 BFD_ASSERT (r_type >= 0
4615 && r_type < TABLE_SIZE (howto_table_ext));
4619 /* We are generating a relocateable output file, and must
4620 modify the reloc accordingly. */
4623 struct aout_link_hash_entry *h;
4625 /* If we know the symbol this relocation is against,
4626 convert it into a relocation against a section. This
4627 is what the native linker does. */
4628 h = sym_hashes[r_index];
4629 if (h != (struct aout_link_hash_entry *) NULL
4630 && h->root.type == bfd_link_hash_defined)
4632 asection *output_section;
4634 /* Change the r_extern value. */
4635 if (output_bfd->xvec->header_byteorder_big_p)
4636 rel->r_type[0] &=~ RELOC_EXT_BITS_EXTERN_BIG;
4638 rel->r_type[0] &=~ RELOC_EXT_BITS_EXTERN_LITTLE;
4640 /* Compute a new r_index. */
4641 output_section = h->root.u.def.section->output_section;
4642 if (output_section == obj_textsec (output_bfd))
4644 else if (output_section == obj_datasec (output_bfd))
4646 else if (output_section == obj_bsssec (output_bfd))
4651 /* Add the symbol value and the section VMA to the
4653 relocation = (h->root.u.def.value
4654 + output_section->vma
4655 + h->root.u.def.section->output_offset);
4657 /* Now RELOCATION is the VMA of the final
4658 destination. If this is a PC relative reloc,
4659 then ADDEND is the negative of the source VMA.
4660 We want to set ADDEND to the difference between
4661 the destination VMA and the source VMA, which
4662 means we must adjust RELOCATION by the change in
4663 the source VMA. This is done below. */
4667 /* We must change r_index according to the symbol
4669 r_index = symbol_map[r_index];
4676 + GET_WORD (input_bfd, syms[r_index].e_strx));
4677 if (! ((*finfo->info->callbacks->unattached_reloc)
4678 (finfo->info, name, input_bfd, input_section,
4686 /* If this is a PC relative reloc, then the addend
4687 is the negative of the source VMA. We must
4688 adjust it by the change in the source VMA. This
4692 /* Write out the new r_index value. */
4693 if (output_bfd->xvec->header_byteorder_big_p)
4695 rel->r_index[0] = r_index >> 16;
4696 rel->r_index[1] = r_index >> 8;
4697 rel->r_index[2] = r_index;
4701 rel->r_index[2] = r_index >> 16;
4702 rel->r_index[1] = r_index >> 8;
4703 rel->r_index[0] = r_index;
4710 /* This is a relocation against a section. We must
4711 adjust by the amount that the section moved. */
4712 section = aout_reloc_index_to_section (input_bfd, r_index);
4713 relocation = (section->output_section->vma
4714 + section->output_offset
4717 /* If this is a PC relative reloc, then the addend is
4718 the difference in VMA between the destination and the
4719 source. We have just adjusted for the change in VMA
4720 of the destination, so we must also adjust by the
4721 change in VMA of the source. This is done below. */
4724 /* As described above, we must always adjust a PC relative
4725 reloc by the change in VMA of the source. */
4726 if (howto_table_ext[r_type].pc_relative)
4727 relocation -= (input_section->output_section->vma
4728 + input_section->output_offset
4729 - input_section->vma);
4731 /* Change the addend if necessary. */
4732 if (relocation != 0)
4733 PUT_WORD (output_bfd, r_addend + relocation, rel->r_addend);
4735 /* Change the address of the relocation. */
4736 PUT_WORD (output_bfd,
4737 r_addr + input_section->output_offset,
4742 bfd_reloc_status_type r;
4744 /* We are generating an executable, and must do a full
4748 struct aout_link_hash_entry *h;
4750 h = sym_hashes[r_index];
4752 if (check_dynamic_reloc != NULL)
4756 if (! ((*check_dynamic_reloc)
4757 (finfo->info, input_bfd, input_section, h,
4764 if (h != (struct aout_link_hash_entry *) NULL
4765 && h->root.type == bfd_link_hash_defined)
4767 relocation = (h->root.u.def.value
4768 + h->root.u.def.section->output_section->vma
4769 + h->root.u.def.section->output_offset);
4771 else if (h != (struct aout_link_hash_entry *) NULL
4772 && h->root.type == bfd_link_hash_weak)
4778 name = strings + GET_WORD (input_bfd, syms[r_index].e_strx);
4779 if (! ((*finfo->info->callbacks->undefined_symbol)
4780 (finfo->info, name, input_bfd, input_section,
4790 section = aout_reloc_index_to_section (input_bfd, r_index);
4792 /* If this is a PC relative reloc, then R_ADDEND is the
4793 difference between the two vmas, or
4794 old_dest_sec + old_dest_off - (old_src_sec + old_src_off)
4796 old_dest_sec == section->vma
4798 old_src_sec == input_section->vma
4800 old_src_off == r_addr
4802 _bfd_final_link_relocate expects RELOCATION +
4803 R_ADDEND to be the VMA of the destination minus
4804 r_addr (the minus r_addr is because this relocation
4805 is not pcrel_offset, which is a bit confusing and
4806 should, perhaps, be changed), or
4809 new_dest_sec == output_section->vma + output_offset
4810 We arrange for this to happen by setting RELOCATION to
4811 new_dest_sec + old_src_sec - old_dest_sec
4813 If this is not a PC relative reloc, then R_ADDEND is
4814 simply the VMA of the destination, so we set
4815 RELOCATION to the change in the destination VMA, or
4816 new_dest_sec - old_dest_sec
4818 relocation = (section->output_section->vma
4819 + section->output_offset
4821 if (howto_table_ext[r_type].pc_relative)
4822 relocation += input_section->vma;
4825 r = _bfd_final_link_relocate (howto_table_ext + r_type,
4826 input_bfd, input_section,
4827 contents, r_addr, relocation,
4829 if (r != bfd_reloc_ok)
4834 case bfd_reloc_outofrange:
4836 case bfd_reloc_overflow:
4841 name = strings + GET_WORD (input_bfd,
4842 syms[r_index].e_strx);
4847 s = aout_reloc_index_to_section (input_bfd, r_index);
4848 name = bfd_section_name (input_bfd, s);
4850 if (! ((*finfo->info->callbacks->reloc_overflow)
4851 (finfo->info, name, howto_table_ext[r_type].name,
4852 r_addend, input_bfd, input_section, r_addr)))
4864 /* Handle a link order which is supposed to generate a reloc. */
4867 aout_link_reloc_link_order (finfo, o, p)
4868 struct aout_final_link_info *finfo;
4870 struct bfd_link_order *p;
4872 struct bfd_link_order_reloc *pr;
4875 const reloc_howto_type *howto;
4876 file_ptr *reloff_ptr;
4877 struct reloc_std_external srel;
4878 struct reloc_ext_external erel;
4883 if (p->type == bfd_section_reloc_link_order)
4886 if (pr->u.section == &bfd_abs_section)
4887 r_index = N_ABS | N_EXT;
4890 BFD_ASSERT (pr->u.section->owner == finfo->output_bfd);
4891 r_index = pr->u.section->target_index;
4896 struct aout_link_hash_entry *h;
4898 BFD_ASSERT (p->type == bfd_symbol_reloc_link_order);
4900 h = aout_link_hash_lookup (aout_hash_table (finfo->info),
4901 pr->u.name, false, false, true);
4902 if (h != (struct aout_link_hash_entry *) NULL
4907 if (! ((*finfo->info->callbacks->unattached_reloc)
4908 (finfo->info, pr->u.name, (bfd *) NULL,
4909 (asection *) NULL, (bfd_vma) 0)))
4915 howto = bfd_reloc_type_lookup (finfo->output_bfd, pr->reloc);
4916 if (howto == (const reloc_howto_type *) NULL)
4918 bfd_set_error (bfd_error_bad_value);
4922 if (o == obj_textsec (finfo->output_bfd))
4923 reloff_ptr = &finfo->treloff;
4924 else if (o == obj_datasec (finfo->output_bfd))
4925 reloff_ptr = &finfo->dreloff;
4929 if (obj_reloc_entry_size (finfo->output_bfd) == RELOC_STD_SIZE)
4937 r_pcrel = howto->pc_relative;
4938 r_baserel = (howto->type & 8) != 0;
4939 r_jmptable = (howto->type & 16) != 0;
4940 r_relative = (howto->type & 32) != 0;
4941 r_length = howto->size;
4943 PUT_WORD (finfo->output_bfd, p->offset, srel.r_address);
4944 if (finfo->output_bfd->xvec->header_byteorder_big_p)
4946 srel.r_index[0] = r_index >> 16;
4947 srel.r_index[1] = r_index >> 8;
4948 srel.r_index[2] = r_index;
4950 ((r_extern ? RELOC_STD_BITS_EXTERN_BIG : 0)
4951 | (r_pcrel ? RELOC_STD_BITS_PCREL_BIG : 0)
4952 | (r_baserel ? RELOC_STD_BITS_BASEREL_BIG : 0)
4953 | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_BIG : 0)
4954 | (r_relative ? RELOC_STD_BITS_RELATIVE_BIG : 0)
4955 | (r_length << RELOC_STD_BITS_LENGTH_SH_BIG));
4959 srel.r_index[2] = r_index >> 16;
4960 srel.r_index[1] = r_index >> 8;
4961 srel.r_index[0] = r_index;
4963 ((r_extern ? RELOC_STD_BITS_EXTERN_LITTLE : 0)
4964 | (r_pcrel ? RELOC_STD_BITS_PCREL_LITTLE : 0)
4965 | (r_baserel ? RELOC_STD_BITS_BASEREL_LITTLE : 0)
4966 | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_LITTLE : 0)
4967 | (r_relative ? RELOC_STD_BITS_RELATIVE_LITTLE : 0)
4968 | (r_length << RELOC_STD_BITS_LENGTH_SH_LITTLE));
4971 rel_ptr = (PTR) &srel;
4973 /* We have to write the addend into the object file, since
4974 standard a.out relocs are in place. It would be more
4975 reliable if we had the current contents of the file here,
4976 rather than assuming zeroes, but we can't read the file since
4977 it was opened using bfd_openw. */
4978 if (pr->addend != 0)
4981 bfd_reloc_status_type r;
4985 size = bfd_get_reloc_size (howto);
4986 buf = (bfd_byte *) bfd_zmalloc (size);
4987 if (buf == (bfd_byte *) NULL)
4989 bfd_set_error (bfd_error_no_memory);
4992 r = _bfd_relocate_contents (howto, finfo->output_bfd,
4999 case bfd_reloc_outofrange:
5001 case bfd_reloc_overflow:
5002 if (! ((*finfo->info->callbacks->reloc_overflow)
5004 (p->type == bfd_section_reloc_link_order
5005 ? bfd_section_name (finfo->output_bfd,
5008 howto->name, pr->addend, (bfd *) NULL,
5009 (asection *) NULL, (bfd_vma) 0)))
5016 ok = bfd_set_section_contents (finfo->output_bfd, o,
5018 (file_ptr) p->offset,
5027 PUT_WORD (finfo->output_bfd, p->offset, erel.r_address);
5029 if (finfo->output_bfd->xvec->header_byteorder_big_p)
5031 erel.r_index[0] = r_index >> 16;
5032 erel.r_index[1] = r_index >> 8;
5033 erel.r_index[2] = r_index;
5035 ((r_extern ? RELOC_EXT_BITS_EXTERN_BIG : 0)
5036 | (howto->type << RELOC_EXT_BITS_TYPE_SH_BIG));
5040 erel.r_index[2] = r_index >> 16;
5041 erel.r_index[1] = r_index >> 8;
5042 erel.r_index[0] = r_index;
5044 (r_extern ? RELOC_EXT_BITS_EXTERN_LITTLE : 0)
5045 | (howto->type << RELOC_EXT_BITS_TYPE_SH_LITTLE);
5048 PUT_WORD (finfo->output_bfd, pr->addend, erel.r_addend);
5050 rel_ptr = (PTR) &erel;
5053 if (bfd_seek (finfo->output_bfd, *reloff_ptr, SEEK_SET) != 0
5054 || (bfd_write (rel_ptr, (bfd_size_type) 1,
5055 obj_reloc_entry_size (finfo->output_bfd),
5057 != obj_reloc_entry_size (finfo->output_bfd)))
5060 *reloff_ptr += obj_reloc_entry_size (finfo->output_bfd);
5062 /* Assert that the relocs have not run into the symbols, and that n
5063 the text relocs have not run into the data relocs. */
5064 BFD_ASSERT (*reloff_ptr <= obj_sym_filepos (finfo->output_bfd)
5065 && (reloff_ptr != &finfo->treloff
5067 <= obj_datasec (finfo->output_bfd)->rel_filepos)));