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 *));
142 The file @file{aoutx.h} provides for both the @emph{standard}
143 and @emph{extended} forms of a.out relocation records.
145 The standard records contain only an
146 address, a symbol index, and a type field. The extended records
147 (used on 29ks and sparcs) also have a full integer for an
151 #define CTOR_TABLE_RELOC_IDX 2
153 #define howto_table_ext NAME(aout,ext_howto_table)
154 #define howto_table_std NAME(aout,std_howto_table)
156 reloc_howto_type howto_table_ext[] =
158 /* type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone */
159 HOWTO(RELOC_8, 0, 0, 8, false, 0, complain_overflow_bitfield,0,"8", false, 0,0x000000ff, false),
160 HOWTO(RELOC_16, 0, 1, 16, false, 0, complain_overflow_bitfield,0,"16", false, 0,0x0000ffff, false),
161 HOWTO(RELOC_32, 0, 2, 32, false, 0, complain_overflow_bitfield,0,"32", false, 0,0xffffffff, false),
162 HOWTO(RELOC_DISP8, 0, 0, 8, true, 0, complain_overflow_signed,0,"DISP8", false, 0,0x000000ff, false),
163 HOWTO(RELOC_DISP16, 0, 1, 16, true, 0, complain_overflow_signed,0,"DISP16", false, 0,0x0000ffff, false),
164 HOWTO(RELOC_DISP32, 0, 2, 32, true, 0, complain_overflow_signed,0,"DISP32", false, 0,0xffffffff, false),
165 HOWTO(RELOC_WDISP30,2, 2, 30, true, 0, complain_overflow_signed,0,"WDISP30", false, 0,0x3fffffff, false),
166 HOWTO(RELOC_WDISP22,2, 2, 22, true, 0, complain_overflow_signed,0,"WDISP22", false, 0,0x003fffff, false),
167 HOWTO(RELOC_HI22, 10, 2, 22, false, 0, complain_overflow_bitfield,0,"HI22", false, 0,0x003fffff, false),
168 HOWTO(RELOC_22, 0, 2, 22, false, 0, complain_overflow_bitfield,0,"22", false, 0,0x003fffff, false),
169 HOWTO(RELOC_13, 0, 2, 13, false, 0, complain_overflow_bitfield,0,"13", false, 0,0x00001fff, false),
170 HOWTO(RELOC_LO10, 0, 2, 10, false, 0, complain_overflow_dont,0,"LO10", false, 0,0x000003ff, false),
171 HOWTO(RELOC_SFA_BASE,0, 2, 32, false, 0, complain_overflow_bitfield,0,"SFA_BASE", false, 0,0xffffffff, false),
172 HOWTO(RELOC_SFA_OFF13,0,2, 32, false, 0, complain_overflow_bitfield,0,"SFA_OFF13",false, 0,0xffffffff, false),
173 HOWTO(RELOC_BASE10, 0, 2, 16, false, 0, complain_overflow_bitfield,0,"BASE10", false, 0,0x0000ffff, false),
174 HOWTO(RELOC_BASE13, 0, 2, 13, false, 0, complain_overflow_bitfield,0,"BASE13", false, 0,0x00001fff, false),
175 HOWTO(RELOC_BASE22, 0, 2, 0, false, 0, complain_overflow_bitfield,0,"BASE22", false, 0,0x00000000, false),
176 HOWTO(RELOC_PC10, 0, 2, 10, false, 0, complain_overflow_bitfield,0,"PC10", false, 0,0x000003ff, false),
177 HOWTO(RELOC_PC22, 0, 2, 22, false, 0, complain_overflow_bitfield,0,"PC22", false, 0,0x003fffff, false),
178 HOWTO(RELOC_JMP_TBL,0, 2, 32, false, 0, complain_overflow_bitfield,0,"JMP_TBL", false, 0,0xffffffff, false),
179 HOWTO(RELOC_SEGOFF16,0, 2, 0, false, 0, complain_overflow_bitfield,0,"SEGOFF16", false, 0,0x00000000, false),
180 HOWTO(RELOC_GLOB_DAT,0, 2, 0, false, 0, complain_overflow_bitfield,0,"GLOB_DAT", false, 0,0x00000000, false),
181 HOWTO(RELOC_JMP_SLOT,0, 2, 0, false, 0, complain_overflow_bitfield,0,"JMP_SLOT", false, 0,0x00000000, false),
182 HOWTO(RELOC_RELATIVE,0, 2, 0, false, 0, complain_overflow_bitfield,0,"RELATIVE", false, 0,0x00000000, false),
185 /* Convert standard reloc records to "arelent" format (incl byte swap). */
187 reloc_howto_type howto_table_std[] = {
188 /* type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone */
189 HOWTO( 0, 0, 0, 8, false, 0, complain_overflow_bitfield,0,"8", true, 0x000000ff,0x000000ff, false),
190 HOWTO( 1, 0, 1, 16, false, 0, complain_overflow_bitfield,0,"16", true, 0x0000ffff,0x0000ffff, false),
191 HOWTO( 2, 0, 2, 32, false, 0, complain_overflow_bitfield,0,"32", true, 0xffffffff,0xffffffff, false),
192 HOWTO( 3, 0, 4, 64, false, 0, complain_overflow_bitfield,0,"64", true, 0xdeaddead,0xdeaddead, false),
193 HOWTO( 4, 0, 0, 8, true, 0, complain_overflow_signed, 0,"DISP8", true, 0x000000ff,0x000000ff, false),
194 HOWTO( 5, 0, 1, 16, true, 0, complain_overflow_signed, 0,"DISP16", true, 0x0000ffff,0x0000ffff, false),
195 HOWTO( 6, 0, 2, 32, true, 0, complain_overflow_signed, 0,"DISP32", true, 0xffffffff,0xffffffff, false),
196 HOWTO( 7, 0, 4, 64, true, 0, complain_overflow_signed, 0,"DISP64", true, 0xfeedface,0xfeedface, false),
198 HOWTO( 9, 0, 1, 16, false, 0, complain_overflow_bitfield,0,"BASE16", false,0xffffffff,0xffffffff, false),
199 HOWTO(10, 0, 2, 32, false, 0, complain_overflow_bitfield,0,"BASE32", false,0xffffffff,0xffffffff, false),
205 HOWTO(16, 0, 2, 0, false, 0, complain_overflow_bitfield,0,"JMP_TABLE", false, 0,0x00000000, false),
213 { -1 }, { -1 }, { -1 }, { -1 }, { -1 }, { -1 }, { -1 }, { -1 },
214 HOWTO(32, 0, 2, 0, false, 0, complain_overflow_bitfield,0,"RELATIVE", false, 0,0x00000000, false),
222 HOWTO(40, 0, 2, 0, false, 0, complain_overflow_bitfield,0,"BASEREL", false, 0,0x00000000, false),
225 #define TABLE_SIZE(TABLE) (sizeof(TABLE)/sizeof(TABLE[0]))
227 CONST struct reloc_howto_struct *
228 NAME(aout,reloc_type_lookup) (abfd,code)
230 bfd_reloc_code_real_type code;
232 #define EXT(i,j) case i: return &howto_table_ext[j]
233 #define STD(i,j) case i: return &howto_table_std[j]
234 int ext = obj_reloc_entry_size (abfd) == RELOC_EXT_SIZE;
235 if (code == BFD_RELOC_CTOR)
236 switch (bfd_get_arch_info (abfd)->bits_per_address)
248 EXT (BFD_RELOC_32, 2);
249 EXT (BFD_RELOC_HI22, 8);
250 EXT (BFD_RELOC_LO10, 11);
251 EXT (BFD_RELOC_32_PCREL_S2, 6);
252 EXT (BFD_RELOC_SPARC_WDISP22, 7);
253 EXT (BFD_RELOC_SPARC13, 10);
254 EXT (BFD_RELOC_SPARC_BASE13, 15);
255 default: return (CONST struct reloc_howto_struct *) 0;
261 STD (BFD_RELOC_16, 1);
262 STD (BFD_RELOC_32, 2);
263 STD (BFD_RELOC_8_PCREL, 4);
264 STD (BFD_RELOC_16_PCREL, 5);
265 STD (BFD_RELOC_32_PCREL, 6);
266 STD (BFD_RELOC_16_BASEREL, 9);
267 STD (BFD_RELOC_32_BASEREL, 10);
268 default: return (CONST struct reloc_howto_struct *) 0;
274 Internal entry points
277 @file{aoutx.h} exports several routines for accessing the
278 contents of an a.out file, which are gathered and exported in
279 turn by various format specific files (eg sunos.c).
285 aout_@var{size}_swap_exec_header_in
288 void aout_@var{size}_swap_exec_header_in,
290 struct external_exec *raw_bytes,
291 struct internal_exec *execp);
294 Swap the information in an executable header @var{raw_bytes} taken
295 from a raw byte stream memory image into the internal exec header
296 structure @var{execp}.
299 #ifndef NAME_swap_exec_header_in
301 NAME(aout,swap_exec_header_in) (abfd, raw_bytes, execp)
303 struct external_exec *raw_bytes;
304 struct internal_exec *execp;
306 struct external_exec *bytes = (struct external_exec *)raw_bytes;
308 /* The internal_exec structure has some fields that are unused in this
309 configuration (IE for i960), so ensure that all such uninitialized
310 fields are zero'd out. There are places where two of these structs
311 are memcmp'd, and thus the contents do matter. */
312 memset ((PTR) execp, 0, sizeof (struct internal_exec));
313 /* Now fill in fields in the execp, from the bytes in the raw data. */
314 execp->a_info = bfd_h_get_32 (abfd, bytes->e_info);
315 execp->a_text = GET_WORD (abfd, bytes->e_text);
316 execp->a_data = GET_WORD (abfd, bytes->e_data);
317 execp->a_bss = GET_WORD (abfd, bytes->e_bss);
318 execp->a_syms = GET_WORD (abfd, bytes->e_syms);
319 execp->a_entry = GET_WORD (abfd, bytes->e_entry);
320 execp->a_trsize = GET_WORD (abfd, bytes->e_trsize);
321 execp->a_drsize = GET_WORD (abfd, bytes->e_drsize);
323 #define NAME_swap_exec_header_in NAME(aout,swap_exec_header_in)
328 aout_@var{size}_swap_exec_header_out
331 void aout_@var{size}_swap_exec_header_out
333 struct internal_exec *execp,
334 struct external_exec *raw_bytes);
337 Swap the information in an internal exec header structure
338 @var{execp} into the buffer @var{raw_bytes} ready for writing to disk.
341 NAME(aout,swap_exec_header_out) (abfd, execp, raw_bytes)
343 struct internal_exec *execp;
344 struct external_exec *raw_bytes;
346 struct external_exec *bytes = (struct external_exec *)raw_bytes;
348 /* Now fill in fields in the raw data, from the fields in the exec struct. */
349 bfd_h_put_32 (abfd, execp->a_info , bytes->e_info);
350 PUT_WORD (abfd, execp->a_text , bytes->e_text);
351 PUT_WORD (abfd, execp->a_data , bytes->e_data);
352 PUT_WORD (abfd, execp->a_bss , bytes->e_bss);
353 PUT_WORD (abfd, execp->a_syms , bytes->e_syms);
354 PUT_WORD (abfd, execp->a_entry , bytes->e_entry);
355 PUT_WORD (abfd, execp->a_trsize, bytes->e_trsize);
356 PUT_WORD (abfd, execp->a_drsize, bytes->e_drsize);
359 /* Make all the section for an a.out file. */
362 NAME(aout,make_sections) (abfd)
365 if (obj_textsec (abfd) == (asection *) NULL
366 && bfd_make_section (abfd, ".text") == (asection *) NULL)
368 if (obj_datasec (abfd) == (asection *) NULL
369 && bfd_make_section (abfd, ".data") == (asection *) NULL)
371 if (obj_bsssec (abfd) == (asection *) NULL
372 && bfd_make_section (abfd, ".bss") == (asection *) NULL)
379 aout_@var{size}_some_aout_object_p
382 bfd_target *aout_@var{size}_some_aout_object_p
384 bfd_target *(*callback_to_real_object_p)());
387 Some a.out variant thinks that the file open in @var{abfd}
388 checking is an a.out file. Do some more checking, and set up
389 for access if it really is. Call back to the calling
390 environment's "finish up" function just before returning, to
391 handle any last-minute setup.
395 NAME(aout,some_aout_object_p) (abfd, execp, callback_to_real_object_p)
397 struct internal_exec *execp;
398 bfd_target *(*callback_to_real_object_p) PARAMS ((bfd *));
400 struct aout_data_struct *rawptr, *oldrawptr;
403 rawptr = (struct aout_data_struct *) bfd_zalloc (abfd, sizeof (struct aout_data_struct ));
404 if (rawptr == NULL) {
405 bfd_set_error (bfd_error_no_memory);
409 oldrawptr = abfd->tdata.aout_data;
410 abfd->tdata.aout_data = rawptr;
412 /* Copy the contents of the old tdata struct.
413 In particular, we want the subformat, since for hpux it was set in
414 hp300hpux.c:swap_exec_header_in and will be used in
415 hp300hpux.c:callback. */
416 if (oldrawptr != NULL)
417 *abfd->tdata.aout_data = *oldrawptr;
419 abfd->tdata.aout_data->a.hdr = &rawptr->e;
420 *(abfd->tdata.aout_data->a.hdr) = *execp; /* Copy in the internal_exec struct */
421 execp = abfd->tdata.aout_data->a.hdr;
423 /* Set the file flags */
424 abfd->flags = NO_FLAGS;
425 if (execp->a_drsize || execp->a_trsize)
426 abfd->flags |= HAS_RELOC;
427 /* Setting of EXEC_P has been deferred to the bottom of this function */
429 abfd->flags |= HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS;
430 if (N_DYNAMIC(*execp))
431 abfd->flags |= DYNAMIC;
433 if (N_MAGIC (*execp) == ZMAGIC)
435 abfd->flags |= D_PAGED|WP_TEXT;
436 adata(abfd).magic = z_magic;
438 else if (N_MAGIC (*execp) == NMAGIC)
440 abfd->flags |= WP_TEXT;
441 adata(abfd).magic = n_magic;
444 adata(abfd).magic = o_magic;
446 bfd_get_start_address (abfd) = execp->a_entry;
448 obj_aout_symbols (abfd) = (aout_symbol_type *)NULL;
449 bfd_get_symcount (abfd) = execp->a_syms / sizeof (struct external_nlist);
451 /* The default relocation entry size is that of traditional V7 Unix. */
452 obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
454 /* The default symbol entry size is that of traditional Unix. */
455 obj_symbol_entry_size (abfd) = EXTERNAL_NLIST_SIZE;
457 obj_aout_external_syms (abfd) = NULL;
458 obj_aout_external_strings (abfd) = NULL;
459 obj_aout_sym_hashes (abfd) = NULL;
461 if (! NAME(aout,make_sections) (abfd))
464 obj_datasec (abfd)->_raw_size = execp->a_data;
465 obj_bsssec (abfd)->_raw_size = execp->a_bss;
467 /* If this object is dynamically linked, we assume that both
468 sections have relocs. This does no real harm, even though it may
470 obj_textsec (abfd)->flags =
471 (execp->a_trsize != 0 || (abfd->flags & DYNAMIC) != 0
472 ? (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_RELOC)
473 : (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS));
474 obj_datasec (abfd)->flags =
475 (execp->a_drsize != 0 || (abfd->flags & DYNAMIC) != 0
476 ? (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS | SEC_RELOC)
477 : (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS));
478 obj_bsssec (abfd)->flags = SEC_ALLOC;
480 #ifdef THIS_IS_ONLY_DOCUMENTATION
481 /* The common code can't fill in these things because they depend
482 on either the start address of the text segment, the rounding
483 up of virtual addresses between segments, or the starting file
484 position of the text segment -- all of which varies among different
485 versions of a.out. */
487 /* Call back to the format-dependent code to fill in the rest of the
488 fields and do any further cleanup. Things that should be filled
489 in by the callback: */
491 struct exec *execp = exec_hdr (abfd);
493 obj_textsec (abfd)->size = N_TXTSIZE(*execp);
494 obj_textsec (abfd)->raw_size = N_TXTSIZE(*execp);
495 /* data and bss are already filled in since they're so standard */
497 /* The virtual memory addresses of the sections */
498 obj_textsec (abfd)->vma = N_TXTADDR(*execp);
499 obj_datasec (abfd)->vma = N_DATADDR(*execp);
500 obj_bsssec (abfd)->vma = N_BSSADDR(*execp);
502 /* The file offsets of the sections */
503 obj_textsec (abfd)->filepos = N_TXTOFF(*execp);
504 obj_datasec (abfd)->filepos = N_DATOFF(*execp);
506 /* The file offsets of the relocation info */
507 obj_textsec (abfd)->rel_filepos = N_TRELOFF(*execp);
508 obj_datasec (abfd)->rel_filepos = N_DRELOFF(*execp);
510 /* The file offsets of the string table and symbol table. */
511 obj_str_filepos (abfd) = N_STROFF (*execp);
512 obj_sym_filepos (abfd) = N_SYMOFF (*execp);
514 /* Determine the architecture and machine type of the object file. */
515 switch (N_MACHTYPE (*exec_hdr (abfd))) {
517 abfd->obj_arch = bfd_arch_obscure;
521 adata(abfd)->page_size = PAGE_SIZE;
522 adata(abfd)->segment_size = SEGMENT_SIZE;
523 adata(abfd)->exec_bytes_size = EXEC_BYTES_SIZE;
527 /* The architecture is encoded in various ways in various a.out variants,
528 or is not encoded at all in some of them. The relocation size depends
529 on the architecture and the a.out variant. Finally, the return value
530 is the bfd_target vector in use. If an error occurs, return zero and
531 set bfd_error to the appropriate error code.
533 Formats such as b.out, which have additional fields in the a.out
534 header, should cope with them in this callback as well. */
535 #endif /* DOCUMENTATION */
537 result = (*callback_to_real_object_p)(abfd);
539 /* Now that the segment addresses have been worked out, take a better
540 guess at whether the file is executable. If the entry point
541 is within the text segment, assume it is. (This makes files
542 executable even if their entry point address is 0, as long as
543 their text starts at zero.)
545 At some point we should probably break down and stat the file and
546 declare it executable if (one of) its 'x' bits are on... */
547 if ((execp->a_entry >= obj_textsec(abfd)->vma) &&
548 (execp->a_entry < obj_textsec(abfd)->vma + obj_textsec(abfd)->_raw_size))
549 abfd->flags |= EXEC_P;
552 #if 0 /* These should be set correctly anyways. */
553 abfd->sections = obj_textsec (abfd);
554 obj_textsec (abfd)->next = obj_datasec (abfd);
555 obj_datasec (abfd)->next = obj_bsssec (abfd);
561 abfd->tdata.aout_data = oldrawptr;
568 aout_@var{size}_mkobject
571 boolean aout_@var{size}_mkobject, (bfd *abfd);
574 Initialize BFD @var{abfd} for use with a.out files.
578 NAME(aout,mkobject) (abfd)
581 struct aout_data_struct *rawptr;
583 bfd_set_error (bfd_error_system_call);
585 /* Use an intermediate variable for clarity */
586 rawptr = (struct aout_data_struct *)bfd_zalloc (abfd, sizeof (struct aout_data_struct ));
588 if (rawptr == NULL) {
589 bfd_set_error (bfd_error_no_memory);
593 abfd->tdata.aout_data = rawptr;
594 exec_hdr (abfd) = &(rawptr->e);
596 obj_textsec (abfd) = (asection *)NULL;
597 obj_datasec (abfd) = (asection *)NULL;
598 obj_bsssec (abfd) = (asection *)NULL;
606 aout_@var{size}_machine_type
609 enum machine_type aout_@var{size}_machine_type
610 (enum bfd_architecture arch,
611 unsigned long machine));
614 Keep track of machine architecture and machine type for
615 a.out's. Return the <<machine_type>> for a particular
616 architecture and machine, or <<M_UNKNOWN>> if that exact architecture
617 and machine can't be represented in a.out format.
619 If the architecture is understood, machine type 0 (default)
620 is always understood.
624 NAME(aout,machine_type) (arch, machine)
625 enum bfd_architecture arch;
626 unsigned long machine;
628 enum machine_type arch_flags;
630 arch_flags = M_UNKNOWN;
634 if (machine == 0) arch_flags = M_SPARC;
639 case 0: arch_flags = M_68010; break;
640 case 68000: arch_flags = M_UNKNOWN; break;
641 case 68010: arch_flags = M_68010; break;
642 case 68020: arch_flags = M_68020; break;
643 default: arch_flags = M_UNKNOWN; break;
648 if (machine == 0) arch_flags = M_386;
652 if (machine == 0) arch_flags = M_29K;
659 case 3000: arch_flags = M_MIPS1; break;
662 case 6000: arch_flags = M_MIPS2; break;
663 default: arch_flags = M_UNKNOWN; break;
668 arch_flags = M_UNKNOWN;
676 aout_@var{size}_set_arch_mach
679 boolean aout_@var{size}_set_arch_mach,
681 enum bfd_architecture arch,
682 unsigned long machine));
685 Set the architecture and the machine of the BFD @var{abfd} to the
686 values @var{arch} and @var{machine}. Verify that @var{abfd}'s format
687 can support the architecture required.
691 NAME(aout,set_arch_mach) (abfd, arch, machine)
693 enum bfd_architecture arch;
694 unsigned long machine;
696 if (! bfd_default_set_arch_mach (abfd, arch, machine))
699 if (arch != bfd_arch_unknown &&
700 NAME(aout,machine_type) (arch, machine) == M_UNKNOWN)
701 return false; /* We can't represent this type */
703 /* Determine the size of a relocation entry */
708 obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE;
711 obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
715 return (*aout_backend_info(abfd)->set_sizes) (abfd);
719 adjust_o_magic (abfd, execp)
721 struct internal_exec *execp;
723 file_ptr pos = adata (abfd).exec_bytes_size;
728 obj_textsec(abfd)->filepos = pos;
729 pos += obj_textsec(abfd)->_raw_size;
730 vma += obj_textsec(abfd)->_raw_size;
733 if (!obj_datasec(abfd)->user_set_vma)
735 #if 0 /* ?? Does alignment in the file image really matter? */
736 pad = align_power (vma, obj_datasec(abfd)->alignment_power) - vma;
738 obj_textsec(abfd)->_raw_size += pad;
741 obj_datasec(abfd)->vma = vma;
743 obj_datasec(abfd)->filepos = pos;
744 pos += obj_datasec(abfd)->_raw_size;
745 vma += obj_datasec(abfd)->_raw_size;
748 if (!obj_bsssec(abfd)->user_set_vma)
751 pad = align_power (vma, obj_bsssec(abfd)->alignment_power) - vma;
753 obj_datasec(abfd)->_raw_size += pad;
756 obj_bsssec(abfd)->vma = vma;
758 obj_bsssec(abfd)->filepos = pos;
760 /* Fix up the exec header. */
761 execp->a_text = obj_textsec(abfd)->_raw_size;
762 execp->a_data = obj_datasec(abfd)->_raw_size;
763 execp->a_bss = obj_bsssec(abfd)->_raw_size;
764 N_SET_MAGIC (*execp, OMAGIC);
768 adjust_z_magic (abfd, execp)
770 struct internal_exec *execp;
772 bfd_size_type data_pad, text_pad;
774 CONST struct aout_backend_data *abdp;
775 int ztih; /* Nonzero if text includes exec header. */
777 abdp = aout_backend_info (abfd);
780 ztih = abdp && abdp->text_includes_header;
781 obj_textsec(abfd)->filepos = (ztih
782 ? adata(abfd).exec_bytes_size
783 : adata(abfd).page_size);
784 if (! obj_textsec(abfd)->user_set_vma)
785 /* ?? Do we really need to check for relocs here? */
786 obj_textsec(abfd)->vma = ((abfd->flags & HAS_RELOC)
789 ? (abdp->default_text_vma
790 + adata(abfd).exec_bytes_size)
791 : abdp->default_text_vma));
792 /* Could take strange alignment of text section into account here? */
794 /* Find start of data. */
795 text_end = obj_textsec(abfd)->filepos + obj_textsec(abfd)->_raw_size;
796 text_pad = BFD_ALIGN (text_end, adata(abfd).page_size) - text_end;
797 obj_textsec(abfd)->_raw_size += text_pad;
798 text_end += text_pad;
801 if (!obj_datasec(abfd)->user_set_vma)
804 vma = obj_textsec(abfd)->vma + obj_textsec(abfd)->_raw_size;
805 obj_datasec(abfd)->vma = BFD_ALIGN (vma, adata(abfd).segment_size);
807 if (abdp && abdp->zmagic_mapped_contiguous)
809 text_pad = (obj_datasec(abfd)->vma
810 - obj_textsec(abfd)->vma
811 - obj_textsec(abfd)->_raw_size);
812 obj_textsec(abfd)->_raw_size += text_pad;
814 obj_datasec(abfd)->filepos = (obj_textsec(abfd)->filepos
815 + obj_textsec(abfd)->_raw_size);
817 /* Fix up exec header while we're at it. */
818 execp->a_text = obj_textsec(abfd)->_raw_size;
819 if (ztih && (!abdp || (abdp && !abdp->exec_header_not_counted)))
820 execp->a_text += adata(abfd).exec_bytes_size;
821 N_SET_MAGIC (*execp, ZMAGIC);
823 /* Spec says data section should be rounded up to page boundary. */
824 obj_datasec(abfd)->_raw_size
825 = align_power (obj_datasec(abfd)->_raw_size,
826 obj_bsssec(abfd)->alignment_power);
827 execp->a_data = BFD_ALIGN (obj_datasec(abfd)->_raw_size,
828 adata(abfd).page_size);
829 data_pad = execp->a_data - obj_datasec(abfd)->_raw_size;
832 if (!obj_bsssec(abfd)->user_set_vma)
833 obj_bsssec(abfd)->vma = (obj_datasec(abfd)->vma
834 + obj_datasec(abfd)->_raw_size);
835 /* If the BSS immediately follows the data section and extra space
836 in the page is left after the data section, fudge data
837 in the header so that the bss section looks smaller by that
838 amount. We'll start the bss section there, and lie to the OS.
839 (Note that a linker script, as well as the above assignment,
840 could have explicitly set the BSS vma to immediately follow
841 the data section.) */
842 if (align_power (obj_bsssec(abfd)->vma, obj_bsssec(abfd)->alignment_power)
843 == obj_datasec(abfd)->vma + obj_datasec(abfd)->_raw_size)
844 execp->a_bss = (data_pad > obj_bsssec(abfd)->_raw_size) ? 0 :
845 obj_bsssec(abfd)->_raw_size - data_pad;
847 execp->a_bss = obj_bsssec(abfd)->_raw_size;
851 adjust_n_magic (abfd, execp)
853 struct internal_exec *execp;
855 file_ptr pos = adata(abfd).exec_bytes_size;
860 obj_textsec(abfd)->filepos = pos;
861 if (!obj_textsec(abfd)->user_set_vma)
862 obj_textsec(abfd)->vma = vma;
864 vma = obj_textsec(abfd)->vma;
865 pos += obj_textsec(abfd)->_raw_size;
866 vma += obj_textsec(abfd)->_raw_size;
869 obj_datasec(abfd)->filepos = pos;
870 if (!obj_datasec(abfd)->user_set_vma)
871 obj_datasec(abfd)->vma = BFD_ALIGN (vma, adata(abfd).segment_size);
872 vma = obj_datasec(abfd)->vma;
874 /* Since BSS follows data immediately, see if it needs alignment. */
875 vma += obj_datasec(abfd)->_raw_size;
876 pad = align_power (vma, obj_bsssec(abfd)->alignment_power) - vma;
877 obj_datasec(abfd)->_raw_size += pad;
878 pos += obj_datasec(abfd)->_raw_size;
881 if (!obj_bsssec(abfd)->user_set_vma)
882 obj_bsssec(abfd)->vma = vma;
884 vma = obj_bsssec(abfd)->vma;
886 /* Fix up exec header. */
887 execp->a_text = obj_textsec(abfd)->_raw_size;
888 execp->a_data = obj_datasec(abfd)->_raw_size;
889 execp->a_bss = obj_bsssec(abfd)->_raw_size;
890 N_SET_MAGIC (*execp, NMAGIC);
894 NAME(aout,adjust_sizes_and_vmas) (abfd, text_size, text_end)
896 bfd_size_type *text_size;
899 struct internal_exec *execp = exec_hdr (abfd);
901 if (! NAME(aout,make_sections) (abfd))
904 if (adata(abfd).magic != undecided_magic) return true;
906 obj_textsec(abfd)->_raw_size =
907 align_power(obj_textsec(abfd)->_raw_size,
908 obj_textsec(abfd)->alignment_power);
910 *text_size = obj_textsec (abfd)->_raw_size;
911 /* Rule (heuristic) for when to pad to a new page. Note that there
912 are (at least) two ways demand-paged (ZMAGIC) files have been
913 handled. Most Berkeley-based systems start the text segment at
914 (PAGE_SIZE). However, newer versions of SUNOS start the text
915 segment right after the exec header; the latter is counted in the
916 text segment size, and is paged in by the kernel with the rest of
919 /* This perhaps isn't the right way to do this, but made it simpler for me
920 to understand enough to implement it. Better would probably be to go
921 right from BFD flags to alignment/positioning characteristics. But the
922 old code was sloppy enough about handling the flags, and had enough
923 other magic, that it was a little hard for me to understand. I think
924 I understand it better now, but I haven't time to do the cleanup this
927 if (abfd->flags & D_PAGED)
928 /* Whether or not WP_TEXT is set -- let D_PAGED override. */
929 /* @@ What about QMAGIC? */
930 adata(abfd).magic = z_magic;
931 else if (abfd->flags & WP_TEXT)
932 adata(abfd).magic = n_magic;
934 adata(abfd).magic = o_magic;
936 #ifdef BFD_AOUT_DEBUG /* requires gcc2 */
938 fprintf (stderr, "%s text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x,%x>\n",
940 switch (adata(abfd).magic) {
941 case n_magic: str = "NMAGIC"; break;
942 case o_magic: str = "OMAGIC"; break;
943 case z_magic: str = "ZMAGIC"; break;
948 obj_textsec(abfd)->vma, obj_textsec(abfd)->_raw_size,
949 obj_textsec(abfd)->alignment_power,
950 obj_datasec(abfd)->vma, obj_datasec(abfd)->_raw_size,
951 obj_datasec(abfd)->alignment_power,
952 obj_bsssec(abfd)->vma, obj_bsssec(abfd)->_raw_size,
953 obj_bsssec(abfd)->alignment_power);
957 switch (adata(abfd).magic)
960 adjust_o_magic (abfd, execp);
963 adjust_z_magic (abfd, execp);
966 adjust_n_magic (abfd, execp);
972 #ifdef BFD_AOUT_DEBUG
973 fprintf (stderr, " text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x>\n",
974 obj_textsec(abfd)->vma, obj_textsec(abfd)->_raw_size,
975 obj_textsec(abfd)->filepos,
976 obj_datasec(abfd)->vma, obj_datasec(abfd)->_raw_size,
977 obj_datasec(abfd)->filepos,
978 obj_bsssec(abfd)->vma, obj_bsssec(abfd)->_raw_size);
986 aout_@var{size}_new_section_hook
989 boolean aout_@var{size}_new_section_hook,
994 Called by the BFD in response to a @code{bfd_make_section}
998 NAME(aout,new_section_hook) (abfd, newsect)
1002 /* align to double at least */
1003 newsect->alignment_power = bfd_get_arch_info(abfd)->section_align_power;
1006 if (bfd_get_format (abfd) == bfd_object)
1008 if (obj_textsec(abfd) == NULL && !strcmp(newsect->name, ".text")) {
1009 obj_textsec(abfd)= newsect;
1010 newsect->target_index = N_TEXT;
1014 if (obj_datasec(abfd) == NULL && !strcmp(newsect->name, ".data")) {
1015 obj_datasec(abfd) = newsect;
1016 newsect->target_index = N_DATA;
1020 if (obj_bsssec(abfd) == NULL && !strcmp(newsect->name, ".bss")) {
1021 obj_bsssec(abfd) = newsect;
1022 newsect->target_index = N_BSS;
1028 /* We allow more than three sections internally */
1033 NAME(aout,set_section_contents) (abfd, section, location, offset, count)
1038 bfd_size_type count;
1041 bfd_size_type text_size;
1043 if (abfd->output_has_begun == false)
1045 if (NAME(aout,adjust_sizes_and_vmas) (abfd,
1047 &text_end) == false)
1051 /* regardless, once we know what we're doing, we might as well get going */
1052 if (section != obj_bsssec(abfd))
1054 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0)
1058 return (bfd_write ((PTR)location, 1, count, abfd) == count) ?
1066 /* Classify stabs symbols */
1068 #define sym_in_text_section(sym) \
1069 (((sym)->type & (N_ABS | N_TEXT | N_DATA | N_BSS))== N_TEXT)
1071 #define sym_in_data_section(sym) \
1072 (((sym)->type & (N_ABS | N_TEXT | N_DATA | N_BSS))== N_DATA)
1074 #define sym_in_bss_section(sym) \
1075 (((sym)->type & (N_ABS | N_TEXT | N_DATA | N_BSS))== N_BSS)
1077 /* Symbol is undefined if type is N_UNDF|N_EXT and if it has
1078 zero in the "value" field. Nonzeroes there are fortrancommon
1080 #define sym_is_undefined(sym) \
1081 ((sym)->type == (N_UNDF | N_EXT) && (sym)->symbol.value == 0)
1083 /* Symbol is a global definition if N_EXT is on and if it has
1084 a nonzero type field. */
1085 #define sym_is_global_defn(sym) \
1086 (((sym)->type & N_EXT) && (sym)->type & N_TYPE)
1088 /* Symbol is debugger info if any bits outside N_TYPE or N_EXT
1090 #define sym_is_debugger_info(sym) \
1091 (((sym)->type & ~(N_EXT | N_TYPE)) || (sym)->type == N_FN)
1093 #define sym_is_fortrancommon(sym) \
1094 (((sym)->type == (N_EXT)) && (sym)->symbol.value != 0)
1096 /* Symbol is absolute if it has N_ABS set */
1097 #define sym_is_absolute(sym) \
1098 (((sym)->type & N_TYPE)== N_ABS)
1101 #define sym_is_indirect(sym) \
1102 (((sym)->type & N_ABS)== N_ABS)
1104 /* Read the external symbols from an a.out file. */
1107 aout_get_external_symbols (abfd)
1110 if (obj_aout_external_syms (abfd) == (struct external_nlist *) NULL)
1112 bfd_size_type count;
1113 struct external_nlist *syms;
1115 count = exec_hdr (abfd)->a_syms / EXTERNAL_NLIST_SIZE;
1117 /* We allocate using malloc to make the values easy to free
1118 later on. If we put them on the obstack it might not be
1119 possible to free them. */
1120 syms = ((struct external_nlist *)
1121 malloc ((size_t) count * EXTERNAL_NLIST_SIZE));
1122 if (syms == (struct external_nlist *) NULL && count != 0)
1124 bfd_set_error (bfd_error_no_memory);
1128 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
1129 || (bfd_read (syms, 1, exec_hdr (abfd)->a_syms, abfd)
1130 != exec_hdr (abfd)->a_syms))
1136 obj_aout_external_syms (abfd) = syms;
1137 obj_aout_external_sym_count (abfd) = count;
1140 if (obj_aout_external_strings (abfd) == NULL)
1142 unsigned char string_chars[BYTES_IN_WORD];
1143 bfd_size_type stringsize;
1146 /* Get the size of the strings. */
1147 if (bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET) != 0
1148 || (bfd_read ((PTR) string_chars, BYTES_IN_WORD, 1, abfd)
1151 stringsize = GET_WORD (abfd, string_chars);
1153 strings = (char *) malloc ((size_t) stringsize + 1);
1154 if (strings == NULL)
1156 bfd_set_error (bfd_error_no_memory);
1160 /* Skip space for the string count in the buffer for convenience
1161 when using indexes. */
1162 if (bfd_read (strings + BYTES_IN_WORD, 1, stringsize - BYTES_IN_WORD,
1164 != stringsize - BYTES_IN_WORD)
1170 /* Sanity preservation. */
1171 strings[stringsize] = '\0';
1173 obj_aout_external_strings (abfd) = strings;
1174 obj_aout_external_string_size (abfd) = stringsize;
1180 /* Only in their own functions for ease of debugging; when sym flags have
1181 stabilised these should be inlined into their (single) caller */
1184 translate_from_native_sym_flags (sym_pointer, cache_ptr, abfd)
1185 struct external_nlist *sym_pointer;
1186 aout_symbol_type * cache_ptr;
1189 cache_ptr->symbol.section = 0;
1190 switch (cache_ptr->type & N_TYPE)
1192 case N_SETA: case N_SETA | N_EXT:
1193 case N_SETT: case N_SETT | N_EXT:
1194 case N_SETD: case N_SETD | N_EXT:
1195 case N_SETB: case N_SETB | N_EXT:
1197 char *copy = bfd_alloc (abfd, strlen (cache_ptr->symbol.name) + 1);
1199 asection *into_section;
1200 arelent_chain *reloc = (arelent_chain *) bfd_alloc (abfd, sizeof (arelent_chain));
1202 if (!copy || !reloc)
1204 bfd_set_error (bfd_error_no_memory);
1208 strcpy (copy, cache_ptr->symbol.name);
1210 /* Make sure that this bfd has a section with the right contructor
1212 section = bfd_get_section_by_name (abfd, copy);
1214 section = bfd_make_section (abfd, copy);
1216 /* Build a relocation entry for the constructor */
1217 switch ((cache_ptr->type & N_TYPE))
1219 case N_SETA: case N_SETA | N_EXT:
1220 into_section = &bfd_abs_section;
1221 cache_ptr->type = N_ABS;
1223 case N_SETT: case N_SETT | N_EXT:
1224 into_section = (asection *) obj_textsec (abfd);
1225 cache_ptr->type = N_TEXT;
1227 case N_SETD: case N_SETD | N_EXT:
1228 into_section = (asection *) obj_datasec (abfd);
1229 cache_ptr->type = N_DATA;
1231 case N_SETB: case N_SETB | N_EXT:
1232 into_section = (asection *) obj_bsssec (abfd);
1233 cache_ptr->type = N_BSS;
1236 bfd_set_error (bfd_error_bad_value);
1240 /* Build a relocation pointing into the constuctor section
1241 pointing at the symbol in the set vector specified */
1243 reloc->relent.addend = cache_ptr->symbol.value;
1244 cache_ptr->symbol.section = into_section->symbol->section;
1245 reloc->relent.sym_ptr_ptr = into_section->symbol_ptr_ptr;
1248 /* We modify the symbol to belong to a section depending upon the
1249 name of the symbol - probably __CTOR__ or __DTOR__ but we don't
1250 really care, and add to the size of the section to contain a
1251 pointer to the symbol. Build a reloc entry to relocate to this
1252 symbol attached to this section. */
1254 section->flags = SEC_CONSTRUCTOR | SEC_RELOC;
1257 section->reloc_count++;
1258 section->alignment_power = 2;
1260 reloc->next = section->constructor_chain;
1261 section->constructor_chain = reloc;
1262 reloc->relent.address = section->_raw_size;
1263 section->_raw_size += sizeof (int *);
1266 = (obj_reloc_entry_size(abfd) == RELOC_EXT_SIZE
1267 ? howto_table_ext : howto_table_std)
1268 + CTOR_TABLE_RELOC_IDX;
1269 cache_ptr->symbol.flags |= BSF_CONSTRUCTOR;
1273 if (cache_ptr->type == N_WARNING)
1275 /* This symbol is the text of a warning message, the next symbol
1276 is the symbol to associate the warning with */
1277 cache_ptr->symbol.flags = BSF_DEBUGGING | BSF_WARNING;
1279 /* @@ Stuffing pointers into integers is a no-no.
1280 We can usually get away with it if the integer is
1281 large enough though. */
1282 if (sizeof (cache_ptr + 1) > sizeof (bfd_vma))
1284 cache_ptr->symbol.value = (bfd_vma) ((cache_ptr + 1));
1286 /* We don't use a warning symbol's section, but we need
1287 it to be nonzero for the sanity check below, so
1288 pick one arbitrarily. */
1289 cache_ptr->symbol.section = &bfd_abs_section;
1293 if ((cache_ptr->type | N_EXT) == (N_INDR | N_EXT))
1295 /* Two symbols in a row for an INDR message. The first symbol
1296 contains the name we will match, the second symbol contains
1297 the name the first name is translated into. It is supplied to
1298 us undefined. This is good, since we want to pull in any files
1300 cache_ptr->symbol.flags = BSF_DEBUGGING | BSF_INDIRECT;
1302 /* @@ Stuffing pointers into integers is a no-no.
1303 We can usually get away with it if the integer is
1304 large enough though. */
1305 if (sizeof (cache_ptr + 1) > sizeof (bfd_vma))
1308 cache_ptr->symbol.value = (bfd_vma) ((cache_ptr + 1));
1309 cache_ptr->symbol.section = &bfd_ind_section;
1312 else if (sym_is_debugger_info (cache_ptr))
1314 cache_ptr->symbol.flags = BSF_DEBUGGING;
1315 /* Work out the section correct for this symbol */
1316 switch (cache_ptr->type & N_TYPE)
1320 cache_ptr->symbol.section = obj_textsec (abfd);
1321 cache_ptr->symbol.value -= obj_textsec (abfd)->vma;
1324 cache_ptr->symbol.value -= obj_datasec (abfd)->vma;
1325 cache_ptr->symbol.section = obj_datasec (abfd);
1328 cache_ptr->symbol.section = obj_bsssec (abfd);
1329 cache_ptr->symbol.value -= obj_bsssec (abfd)->vma;
1333 cache_ptr->symbol.section = &bfd_abs_section;
1340 if (sym_is_fortrancommon (cache_ptr))
1342 cache_ptr->symbol.flags = 0;
1343 cache_ptr->symbol.section = &bfd_com_section;
1351 /* In a.out, the value of a symbol is always relative to the
1352 * start of the file, if this is a data symbol we'll subtract
1353 * the size of the text section to get the section relative
1354 * value. If this is a bss symbol (which would be strange)
1355 * we'll subtract the size of the previous two sections
1356 * to find the section relative address.
1359 if (sym_in_text_section (cache_ptr))
1361 cache_ptr->symbol.value -= obj_textsec (abfd)->vma;
1362 cache_ptr->symbol.section = obj_textsec (abfd);
1364 else if (sym_in_data_section (cache_ptr))
1366 cache_ptr->symbol.value -= obj_datasec (abfd)->vma;
1367 cache_ptr->symbol.section = obj_datasec (abfd);
1369 else if (sym_in_bss_section (cache_ptr))
1371 cache_ptr->symbol.section = obj_bsssec (abfd);
1372 cache_ptr->symbol.value -= obj_bsssec (abfd)->vma;
1374 else if (sym_is_undefined (cache_ptr))
1376 cache_ptr->symbol.flags = 0;
1377 cache_ptr->symbol.section = &bfd_und_section;
1379 else if (sym_is_absolute (cache_ptr))
1381 cache_ptr->symbol.section = &bfd_abs_section;
1384 if (sym_is_global_defn (cache_ptr))
1386 cache_ptr->symbol.flags = BSF_GLOBAL | BSF_EXPORT;
1388 else if (! sym_is_undefined (cache_ptr))
1390 cache_ptr->symbol.flags = BSF_LOCAL;
1394 if (cache_ptr->symbol.section == 0)
1401 translate_to_native_sym_flags (sym_pointer, cache_ptr, abfd)
1402 struct external_nlist *sym_pointer;
1406 bfd_vma value = cache_ptr->value;
1408 /* mask out any existing type bits in case copying from one section
1410 sym_pointer->e_type[0] &= ~N_TYPE;
1412 /* We attempt to order these tests by decreasing frequency of success,
1413 according to tcov when linking the linker. */
1414 if (bfd_get_output_section(cache_ptr) == &bfd_abs_section) {
1415 sym_pointer->e_type[0] |= N_ABS;
1417 else if (bfd_get_output_section(cache_ptr) == obj_textsec (abfd)) {
1418 sym_pointer->e_type[0] |= N_TEXT;
1420 else if (bfd_get_output_section(cache_ptr) == obj_datasec (abfd)) {
1421 sym_pointer->e_type[0] |= N_DATA;
1423 else if (bfd_get_output_section(cache_ptr) == obj_bsssec (abfd)) {
1424 sym_pointer->e_type[0] |= N_BSS;
1426 else if (bfd_get_output_section(cache_ptr) == &bfd_und_section) {
1427 sym_pointer->e_type[0] = (N_UNDF | N_EXT);
1429 else if (bfd_get_output_section(cache_ptr) == &bfd_ind_section) {
1430 sym_pointer->e_type[0] = N_INDR;
1432 else if (bfd_get_output_section(cache_ptr) == NULL) {
1433 /* Protect the bfd_is_com_section call.
1434 This case occurs, e.g., for the *DEBUG* section of a COFF file. */
1435 bfd_set_error (bfd_error_nonrepresentable_section);
1438 else if (bfd_is_com_section (bfd_get_output_section (cache_ptr))) {
1439 sym_pointer->e_type[0] = (N_UNDF | N_EXT);
1442 bfd_set_error (bfd_error_nonrepresentable_section);
1446 /* Turn the symbol from section relative to absolute again */
1448 value += cache_ptr->section->output_section->vma + cache_ptr->section->output_offset ;
1451 if (cache_ptr->flags & (BSF_WARNING)) {
1452 sym_pointer->e_type[0] = N_WARNING;
1455 if (cache_ptr->flags & BSF_DEBUGGING) {
1456 sym_pointer->e_type[0] = ((aout_symbol_type *)cache_ptr)->type;
1458 else if (cache_ptr->flags & (BSF_GLOBAL | BSF_EXPORT)) {
1459 sym_pointer->e_type[0] |= N_EXT;
1461 if (cache_ptr->flags & BSF_CONSTRUCTOR) {
1462 int type = ((aout_symbol_type *)cache_ptr)->type;
1465 case N_ABS: type = N_SETA; break;
1466 case N_TEXT: type = N_SETT; break;
1467 case N_DATA: type = N_SETD; break;
1468 case N_BSS: type = N_SETB; break;
1470 sym_pointer->e_type[0] = type;
1473 PUT_WORD(abfd, value, sym_pointer->e_value);
1478 /* Native-level interface to symbols. */
1482 NAME(aout,make_empty_symbol) (abfd)
1485 aout_symbol_type *new =
1486 (aout_symbol_type *)bfd_zalloc (abfd, sizeof (aout_symbol_type));
1489 bfd_set_error (bfd_error_no_memory);
1492 new->symbol.the_bfd = abfd;
1494 return &new->symbol;
1497 /* Translate a set of internal symbols into external symbols. */
1500 NAME(aout,translate_symbol_table) (abfd, in, ext, count, str, strsize, dynamic)
1502 aout_symbol_type *in;
1503 struct external_nlist *ext;
1504 bfd_size_type count;
1506 bfd_size_type strsize;
1509 struct external_nlist *ext_end;
1511 ext_end = ext + count;
1512 for (; ext < ext_end; ext++, in++)
1516 x = GET_WORD (abfd, ext->e_strx);
1517 in->symbol.the_bfd = abfd;
1519 /* For the normal symbols, the zero index points at the number
1520 of bytes in the string table but is to be interpreted as the
1521 null string. For the dynamic symbols, the number of bytes in
1522 the string table is stored in the __DYNAMIC structure and the
1523 zero index points at an actual string. */
1524 if (x == 0 && ! dynamic)
1525 in->symbol.name = "";
1526 else if (x < strsize)
1527 in->symbol.name = str + x;
1531 in->symbol.value = GET_SWORD (abfd, ext->e_value);
1532 in->desc = bfd_h_get_16 (abfd, ext->e_desc);
1533 in->other = bfd_h_get_8 (abfd, ext->e_other);
1534 in->type = bfd_h_get_8 (abfd, ext->e_type);
1535 in->symbol.udata = 0;
1537 if (!translate_from_native_sym_flags (ext, in, abfd))
1541 in->symbol.flags |= BSF_DYNAMIC;
1547 /* We read the symbols into a buffer, which is discarded when this
1548 function exits. We read the strings into a buffer large enough to
1549 hold them all plus all the cached symbol entries. */
1552 NAME(aout,slurp_symbol_table) (abfd)
1555 struct external_nlist *old_external_syms;
1556 aout_symbol_type *cached;
1559 /* If there's no work to be done, don't do any */
1560 if (obj_aout_symbols (abfd) != (aout_symbol_type *) NULL)
1563 old_external_syms = obj_aout_external_syms (abfd);
1565 if (! aout_get_external_symbols (abfd))
1568 if (obj_aout_external_sym_count (abfd) == 0)
1570 bfd_set_error (bfd_error_no_symbols);
1574 cached_size = (obj_aout_external_sym_count (abfd)
1575 * sizeof (aout_symbol_type));
1576 cached = (aout_symbol_type *) malloc (cached_size);
1579 bfd_set_error (bfd_error_no_memory);
1582 memset (cached, 0, cached_size);
1584 /* Convert from external symbol information to internal. */
1585 if (! (NAME(aout,translate_symbol_table)
1587 obj_aout_external_syms (abfd),
1588 obj_aout_external_sym_count (abfd),
1589 obj_aout_external_strings (abfd),
1590 obj_aout_external_string_size (abfd),
1597 bfd_get_symcount (abfd) = obj_aout_external_sym_count (abfd);
1599 obj_aout_symbols (abfd) = cached;
1601 /* It is very likely that anybody who calls this function will not
1602 want the external symbol information, so if it was allocated
1603 because of our call to aout_get_external_symbols, we free it up
1604 right away to save space. */
1605 if (old_external_syms == (struct external_nlist *) NULL
1606 && obj_aout_external_syms (abfd) != (struct external_nlist *) NULL)
1608 free (obj_aout_external_syms (abfd));
1609 obj_aout_external_syms (abfd) = NULL;
1615 /* Possible improvements:
1616 + look for strings matching trailing substrings of other strings
1617 + better data structures? balanced trees?
1618 + smaller per-string or per-symbol data? re-use some of the symbol's
1620 + also look at reducing memory use elsewhere -- maybe if we didn't have to
1621 construct the entire symbol table at once, we could get by with smaller
1622 amounts of VM? (What effect does that have on the string table
1624 + rip this out of here, put it into its own file in bfd or libiberty, so
1625 coff and elf can use it too. I'll work on this soon, but have more
1626 pressing tasks right now.
1628 A hash table might(?) be more efficient for handling exactly the cases that
1629 are handled now, but for trailing substring matches, I think we want to
1630 examine the `nearest' values (reverse-)lexically, not merely impose a strict
1631 order, nor look only for exact-match or not-match. I don't think a hash
1632 table would be very useful for that, and I don't feel like fleshing out two
1633 completely different implementations. [raeburn:930419.0331EDT] */
1635 struct stringtab_entry {
1636 /* Hash value for this string. Only useful so long as we aren't doing
1637 substring matches. */
1640 /* Next node to look at, depending on whether the hash value of the string
1641 being searched for is less than or greater than the hash value of the
1642 current node. For now, `equal to' is lumped in with `greater than', for
1643 space efficiency. It's not a common enough case to warrant another field
1644 to be used for all nodes. */
1645 struct stringtab_entry *less;
1646 struct stringtab_entry *greater;
1648 /* The string itself. */
1651 /* The index allocated for this string. */
1652 bfd_size_type index;
1654 #ifdef GATHER_STATISTICS
1655 /* How many references have there been to this string? (Not currently used;
1656 could be dumped out for anaylsis, if anyone's interested.) */
1657 unsigned long count;
1660 /* Next node in linked list, in suggested output order. */
1661 struct stringtab_entry *next_to_output;
1664 struct stringtab_data {
1665 /* Tree of string table entries. */
1666 struct stringtab_entry *strings;
1668 /* Fudge factor used to center top node of tree. */
1671 /* Next index value to issue. */
1672 bfd_size_type index;
1674 /* Index used for empty strings. Cached here because checking for them
1675 is really easy, and we can avoid searching the tree. */
1676 bfd_size_type empty_string_index;
1678 /* These fields indicate the two ends of a singly-linked list that indicates
1679 the order strings should be written out in. Use this order, and no
1680 seeking will need to be done, so output efficiency should be maximized. */
1681 struct stringtab_entry **end;
1682 struct stringtab_entry *output_order;
1684 #ifdef GATHER_STATISTICS
1685 /* Number of strings which duplicate strings already in the table. */
1686 unsigned long duplicates;
1688 /* Number of bytes saved by not having to write all the duplicate strings. */
1689 unsigned long bytes_saved;
1691 /* Number of zero-length strings. Currently, these all turn into
1692 references to the null byte at the end of the first string. In some
1693 cases (possibly not all? explore this...), it should be possible to
1694 simply write out a zero index value. */
1695 unsigned long empty_strings;
1697 /* Number of times the hash values matched but the strings were different.
1698 Note that this includes the number of times the other string(s) occurs, so
1699 there may only be two strings hashing to the same value, even if this
1700 number is very large. */
1701 unsigned long bad_hash_matches;
1703 /* Null strings aren't counted in this one.
1704 This will probably only be nonzero if we've got an input file
1705 which was produced by `ld -r' (i.e., it's already been processed
1706 through this code). Under some operating systems, native tools
1707 may make all empty strings have the same index; but the pointer
1708 check won't catch those, because to get to that stage we'd already
1709 have to compute the checksum, which requires reading the string,
1710 so we short-circuit that case with empty_string_index above. */
1711 unsigned long pointer_matches;
1713 /* Number of comparisons done. I figure with the algorithms in use below,
1714 the average number of comparisons done (per symbol) should be roughly
1715 log-base-2 of the number of unique strings. */
1716 unsigned long n_compares;
1720 /* Some utility functions for the string table code. */
1722 /* For speed, only hash on the first this many bytes of strings.
1723 This number was chosen by profiling ld linking itself, with -g. */
1724 #define HASHMAXLEN 25
1726 #define HASH_CHAR(c) (sum ^= sum >> 20, sum ^= sum << 7, sum += (c))
1728 static INLINE unsigned int
1730 unsigned char *string;
1731 register unsigned int len;
1733 register unsigned int sum = 0;
1735 if (len > HASHMAXLEN)
1743 HASH_CHAR (*string++);
1749 stringtab_init (tab)
1750 struct stringtab_data *tab;
1753 tab->output_order = 0;
1755 tab->end = &tab->output_order;
1757 /* Initial string table length includes size of length field. */
1758 tab->index = BYTES_IN_WORD;
1759 tab->empty_string_index = -1;
1760 #ifdef GATHER_STATISTICS
1761 tab->duplicates = 0;
1762 tab->empty_strings = 0;
1763 tab->bad_hash_matches = 0;
1764 tab->pointer_matches = 0;
1765 tab->bytes_saved = 0;
1766 tab->n_compares = 0;
1771 compare (entry, str, hash)
1772 struct stringtab_entry *entry;
1776 return hash - entry->hash;
1779 #ifdef GATHER_STATISTICS
1780 /* Don't want to have to link in math library with all bfd applications... */
1781 static INLINE double
1789 return ((d > 1.41) ? 0.5 : 0) + n;
1793 /* Main string table routines. */
1794 /* Returns index in string table. Whether or not this actually adds an
1795 entry into the string table should be irrelevant -- it just has to
1796 return a valid index. */
1797 static bfd_size_type
1798 add_to_stringtab (abfd, str, tab)
1801 struct stringtab_data *tab;
1803 struct stringtab_entry **ep;
1804 register struct stringtab_entry *entry;
1805 unsigned int hashval, len;
1809 bfd_size_type index;
1810 CONST bfd_size_type minus_one = -1;
1812 #ifdef GATHER_STATISTICS
1813 tab->empty_strings++;
1815 index = tab->empty_string_index;
1816 if (index != minus_one)
1819 #ifdef GATHER_STATISTICS
1826 /* Need to find it. */
1827 entry = tab->strings;
1830 index = entry->index + strlen (entry->string);
1831 tab->empty_string_index = index;
1839 /* The hash_zero value is chosen such that the first symbol gets a value of
1840 zero. With a balanced tree, this wouldn't be very useful, but without it,
1841 we might get a more even split at the top level, instead of skewing it
1842 badly should hash("/usr/lib/crt0.o") (or whatever) be far from zero. */
1843 hashval = hash (str, len) ^ tab->hash_zero;
1847 tab->hash_zero = hashval;
1857 #ifdef GATHER_STATISTICS
1860 cmp = compare (entry, str, hashval);
1861 /* The not-equal cases are more frequent, so check them first. */
1863 ep = &entry->greater;
1868 if (entry->string == str)
1870 #ifdef GATHER_STATISTICS
1871 tab->pointer_matches++;
1875 /* Compare the first bytes to save a function call if they
1877 if (entry->string[0] == str[0] && !strcmp (entry->string, str))
1880 #ifdef GATHER_STATISTICS
1882 tab->bytes_saved += len + 1;
1885 /* If we're in the linker, and the new string is from a new
1886 input file which might have already had these reductions
1887 run over it, we want to keep the new string pointer. I
1888 don't think we're likely to see any (or nearly as many,
1889 at least) cases where a later string is in the same location
1890 as an earlier one rather than this one. */
1891 entry->string = str;
1892 return entry->index;
1894 #ifdef GATHER_STATISTICS
1895 tab->bad_hash_matches++;
1897 ep = &entry->greater;
1901 /* If we get here, nothing that's in the table already matched.
1902 EP points to the `next' field at the end of the chain; stick a
1903 new entry on here. */
1905 entry = (struct stringtab_entry *)
1906 bfd_alloc_by_size_t (abfd, sizeof (struct stringtab_entry));
1909 bfd_set_error (bfd_error_no_memory);
1910 abort(); /* FIXME */
1913 entry->less = entry->greater = 0;
1914 entry->hash = hashval;
1915 entry->index = tab->index;
1916 entry->string = str;
1917 entry->next_to_output = 0;
1918 #ifdef GATHER_STATISTICS
1922 BFD_ASSERT (*tab->end == 0);
1923 *(tab->end) = entry;
1924 tab->end = &entry->next_to_output;
1925 BFD_ASSERT (*tab->end == 0);
1928 tab->index += len + 1;
1930 tab->empty_string_index = entry->index;
1932 BFD_ASSERT (*ep == 0);
1934 return entry->index;
1938 emit_strtab (abfd, tab)
1940 struct stringtab_data *tab;
1942 struct stringtab_entry *entry;
1943 #ifdef GATHER_STATISTICS
1947 /* Be sure to put string length into correct byte ordering before writing
1949 char buffer[BYTES_IN_WORD];
1951 PUT_WORD (abfd, tab->index, (unsigned char *) buffer);
1952 if (bfd_write ((PTR) buffer, 1, BYTES_IN_WORD, abfd) != BYTES_IN_WORD)
1955 for (entry = tab->output_order; entry; entry = entry->next_to_output)
1957 size_t len = strlen (entry->string) + 1;
1959 if (bfd_write ((PTR) entry->string, 1, len, abfd) != len)
1962 #ifdef GATHER_STATISTICS
1967 #ifdef GATHER_STATISTICS
1968 /* Short form only, for now.
1969 To do: Specify output file. Conditionalize on environment? Detailed
1970 analysis if desired. */
1972 int n_syms = bfd_get_symcount (abfd);
1974 fprintf (stderr, "String table data for output file:\n");
1975 fprintf (stderr, " %8d symbols output\n", n_syms);
1976 fprintf (stderr, " %8d duplicate strings\n", tab->duplicates);
1977 fprintf (stderr, " %8d empty strings\n", tab->empty_strings);
1978 fprintf (stderr, " %8d unique strings output\n", count);
1979 fprintf (stderr, " %8d pointer matches\n", tab->pointer_matches);
1980 fprintf (stderr, " %8d bytes saved\n", tab->bytes_saved);
1981 fprintf (stderr, " %8d bad hash matches\n", tab->bad_hash_matches);
1982 fprintf (stderr, " %8d hash-val comparisons\n", tab->n_compares);
1985 double n_compares = tab->n_compares;
1986 double avg_compares = n_compares / n_syms;
1987 /* The second value here should usually be near one. */
1989 "\t average %f comparisons per symbol (%f * log2 nstrings)\n",
1990 avg_compares, avg_compares / log2 (count));
1997 generic = bfd_get_outsymbols(abfd);
1998 for (count = 0; count < bfd_get_symcount(abfd); count++)
2000 asymbol *g = *(generic++);
2004 size_t length = strlen(g->name)+1;
2005 bfd_write((PTR)g->name, 1, length, abfd);
2007 g->KEEPIT = (KEEPITTYPE) count;
2014 NAME(aout,write_syms) (abfd)
2017 unsigned int count ;
2018 asymbol **generic = bfd_get_outsymbols (abfd);
2019 struct stringtab_data strtab;
2021 stringtab_init (&strtab);
2023 for (count = 0; count < bfd_get_symcount (abfd); count++)
2025 asymbol *g = generic[count];
2026 struct external_nlist nsp;
2029 PUT_WORD (abfd, add_to_stringtab (abfd, g->name, &strtab),
2030 (unsigned char *) nsp.e_strx);
2032 PUT_WORD (abfd, 0, (unsigned char *)nsp.e_strx);
2034 if (bfd_asymbol_flavour(g) == abfd->xvec->flavour)
2036 bfd_h_put_16(abfd, aout_symbol(g)->desc, nsp.e_desc);
2037 bfd_h_put_8(abfd, aout_symbol(g)->other, nsp.e_other);
2038 bfd_h_put_8(abfd, aout_symbol(g)->type, nsp.e_type);
2042 bfd_h_put_16(abfd,0, nsp.e_desc);
2043 bfd_h_put_8(abfd, 0, nsp.e_other);
2044 bfd_h_put_8(abfd, 0, nsp.e_type);
2047 if (! translate_to_native_sym_flags (&nsp, g, abfd))
2050 if (bfd_write((PTR)&nsp,1,EXTERNAL_NLIST_SIZE, abfd)
2051 != EXTERNAL_NLIST_SIZE)
2054 /* NB: `KEEPIT' currently overlays `flags', so set this only
2055 here, at the end. */
2059 return emit_strtab (abfd, &strtab);
2064 NAME(aout,get_symtab) (abfd, location)
2068 unsigned int counter = 0;
2069 aout_symbol_type *symbase;
2071 if (!NAME(aout,slurp_symbol_table)(abfd))
2074 for (symbase = obj_aout_symbols(abfd); counter++ < bfd_get_symcount (abfd);)
2075 *(location++) = (asymbol *)( symbase++);
2077 return bfd_get_symcount (abfd);
2081 /* Standard reloc stuff */
2082 /* Output standard relocation information to a file in target byte order. */
2085 NAME(aout,swap_std_reloc_out) (abfd, g, natptr)
2088 struct reloc_std_external *natptr;
2091 asymbol *sym = *(g->sym_ptr_ptr);
2093 unsigned int r_length;
2095 int r_baserel, r_jmptable, r_relative;
2096 asection *output_section = sym->section->output_section;
2098 PUT_WORD(abfd, g->address, natptr->r_address);
2100 r_length = g->howto->size ; /* Size as a power of two */
2101 r_pcrel = (int) g->howto->pc_relative; /* Relative to PC? */
2102 /* XXX This relies on relocs coming from a.out files. */
2103 r_baserel = (g->howto->type & 8) != 0;
2104 r_jmptable = (g->howto->type & 16) != 0;
2105 r_relative = (g->howto->type & 32) != 0;
2108 /* For a standard reloc, the addend is in the object file. */
2109 r_addend = g->addend + (*(g->sym_ptr_ptr))->section->output_section->vma;
2112 /* name was clobbered by aout_write_syms to be symbol index */
2114 /* If this relocation is relative to a symbol then set the
2115 r_index to the symbols index, and the r_extern bit.
2117 Absolute symbols can come in in two ways, either as an offset
2118 from the abs section, or as a symbol which has an abs value.
2123 if (bfd_is_com_section (output_section)
2124 || output_section == &bfd_abs_section
2125 || output_section == &bfd_und_section)
2127 if (bfd_abs_section.symbol == sym)
2129 /* Whoops, looked like an abs symbol, but is really an offset
2130 from the abs section */
2136 /* Fill in symbol */
2138 r_index = stoi((*(g->sym_ptr_ptr))->KEEPIT);
2144 /* Just an ordinary section */
2146 r_index = output_section->target_index;
2149 /* now the fun stuff */
2150 if (abfd->xvec->header_byteorder_big_p != false) {
2151 natptr->r_index[0] = r_index >> 16;
2152 natptr->r_index[1] = r_index >> 8;
2153 natptr->r_index[2] = r_index;
2155 (r_extern? RELOC_STD_BITS_EXTERN_BIG: 0)
2156 | (r_pcrel? RELOC_STD_BITS_PCREL_BIG: 0)
2157 | (r_baserel? RELOC_STD_BITS_BASEREL_BIG: 0)
2158 | (r_jmptable? RELOC_STD_BITS_JMPTABLE_BIG: 0)
2159 | (r_relative? RELOC_STD_BITS_RELATIVE_BIG: 0)
2160 | (r_length << RELOC_STD_BITS_LENGTH_SH_BIG);
2162 natptr->r_index[2] = r_index >> 16;
2163 natptr->r_index[1] = r_index >> 8;
2164 natptr->r_index[0] = r_index;
2166 (r_extern? RELOC_STD_BITS_EXTERN_LITTLE: 0)
2167 | (r_pcrel? RELOC_STD_BITS_PCREL_LITTLE: 0)
2168 | (r_baserel? RELOC_STD_BITS_BASEREL_LITTLE: 0)
2169 | (r_jmptable? RELOC_STD_BITS_JMPTABLE_LITTLE: 0)
2170 | (r_relative? RELOC_STD_BITS_RELATIVE_LITTLE: 0)
2171 | (r_length << RELOC_STD_BITS_LENGTH_SH_LITTLE);
2176 /* Extended stuff */
2177 /* Output extended relocation information to a file in target byte order. */
2180 NAME(aout,swap_ext_reloc_out) (abfd, g, natptr)
2183 register struct reloc_ext_external *natptr;
2187 unsigned int r_type;
2188 unsigned int r_addend;
2189 asymbol *sym = *(g->sym_ptr_ptr);
2190 asection *output_section = sym->section->output_section;
2192 PUT_WORD (abfd, g->address, natptr->r_address);
2194 r_type = (unsigned int) g->howto->type;
2196 r_addend = g->addend + (*(g->sym_ptr_ptr))->section->output_section->vma;
2198 /* If this relocation is relative to a symbol then set the
2199 r_index to the symbols index, and the r_extern bit.
2201 Absolute symbols can come in in two ways, either as an offset
2202 from the abs section, or as a symbol which has an abs value.
2203 check for that here. */
2205 if (bfd_is_com_section (output_section)
2206 || output_section == &bfd_abs_section
2207 || output_section == &bfd_und_section)
2209 if (bfd_abs_section.symbol == sym)
2211 /* Whoops, looked like an abs symbol, but is really an offset
2212 from the abs section */
2219 r_index = stoi((*(g->sym_ptr_ptr))->KEEPIT);
2224 /* Just an ordinary section */
2226 r_index = output_section->target_index;
2229 /* now the fun stuff */
2230 if (abfd->xvec->header_byteorder_big_p != false) {
2231 natptr->r_index[0] = r_index >> 16;
2232 natptr->r_index[1] = r_index >> 8;
2233 natptr->r_index[2] = r_index;
2235 ((r_extern? RELOC_EXT_BITS_EXTERN_BIG: 0)
2236 | (r_type << RELOC_EXT_BITS_TYPE_SH_BIG));
2238 natptr->r_index[2] = r_index >> 16;
2239 natptr->r_index[1] = r_index >> 8;
2240 natptr->r_index[0] = r_index;
2242 (r_extern? RELOC_EXT_BITS_EXTERN_LITTLE: 0)
2243 | (r_type << RELOC_EXT_BITS_TYPE_SH_LITTLE);
2246 PUT_WORD (abfd, r_addend, natptr->r_addend);
2249 /* BFD deals internally with all things based from the section they're
2250 in. so, something in 10 bytes into a text section with a base of
2251 50 would have a symbol (.text+10) and know .text vma was 50.
2253 Aout keeps all it's symbols based from zero, so the symbol would
2254 contain 60. This macro subs the base of each section from the value
2255 to give the true offset from the section */
2258 #define MOVE_ADDRESS(ad) \
2260 /* undefined symbol */ \
2261 cache_ptr->sym_ptr_ptr = symbols + r_index; \
2262 cache_ptr->addend = ad; \
2264 /* defined, section relative. replace symbol with pointer to \
2265 symbol which points to section */ \
2266 switch (r_index) { \
2268 case N_TEXT | N_EXT: \
2269 cache_ptr->sym_ptr_ptr = obj_textsec(abfd)->symbol_ptr_ptr; \
2270 cache_ptr->addend = ad - su->textsec->vma; \
2273 case N_DATA | N_EXT: \
2274 cache_ptr->sym_ptr_ptr = obj_datasec(abfd)->symbol_ptr_ptr; \
2275 cache_ptr->addend = ad - su->datasec->vma; \
2278 case N_BSS | N_EXT: \
2279 cache_ptr->sym_ptr_ptr = obj_bsssec(abfd)->symbol_ptr_ptr; \
2280 cache_ptr->addend = ad - su->bsssec->vma; \
2284 case N_ABS | N_EXT: \
2285 cache_ptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr; \
2286 cache_ptr->addend = ad; \
2292 NAME(aout,swap_ext_reloc_in) (abfd, bytes, cache_ptr, symbols)
2294 struct reloc_ext_external *bytes;
2300 unsigned int r_type;
2301 struct aoutdata *su = &(abfd->tdata.aout_data->a);
2303 cache_ptr->address = (GET_SWORD (abfd, bytes->r_address));
2305 /* now the fun stuff */
2306 if (abfd->xvec->header_byteorder_big_p != false) {
2307 r_index = (bytes->r_index[0] << 16)
2308 | (bytes->r_index[1] << 8)
2309 | bytes->r_index[2];
2310 r_extern = (0 != (bytes->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG));
2311 r_type = (bytes->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
2312 >> RELOC_EXT_BITS_TYPE_SH_BIG;
2314 r_index = (bytes->r_index[2] << 16)
2315 | (bytes->r_index[1] << 8)
2316 | bytes->r_index[0];
2317 r_extern = (0 != (bytes->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
2318 r_type = (bytes->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
2319 >> RELOC_EXT_BITS_TYPE_SH_LITTLE;
2322 cache_ptr->howto = howto_table_ext + r_type;
2323 MOVE_ADDRESS(GET_SWORD(abfd, bytes->r_addend));
2327 NAME(aout,swap_std_reloc_in) (abfd, bytes, cache_ptr, symbols)
2329 struct reloc_std_external *bytes;
2335 unsigned int r_length;
2337 int r_baserel, r_jmptable, r_relative;
2338 struct aoutdata *su = &(abfd->tdata.aout_data->a);
2341 cache_ptr->address = bfd_h_get_32 (abfd, bytes->r_address);
2343 /* now the fun stuff */
2344 if (abfd->xvec->header_byteorder_big_p != false) {
2345 r_index = (bytes->r_index[0] << 16)
2346 | (bytes->r_index[1] << 8)
2347 | bytes->r_index[2];
2348 r_extern = (0 != (bytes->r_type[0] & RELOC_STD_BITS_EXTERN_BIG));
2349 r_pcrel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_PCREL_BIG));
2350 r_baserel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_BASEREL_BIG));
2351 r_jmptable= (0 != (bytes->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG));
2352 r_relative= (0 != (bytes->r_type[0] & RELOC_STD_BITS_RELATIVE_BIG));
2353 r_length = (bytes->r_type[0] & RELOC_STD_BITS_LENGTH_BIG)
2354 >> RELOC_STD_BITS_LENGTH_SH_BIG;
2356 r_index = (bytes->r_index[2] << 16)
2357 | (bytes->r_index[1] << 8)
2358 | bytes->r_index[0];
2359 r_extern = (0 != (bytes->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE));
2360 r_pcrel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
2361 r_baserel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_BASEREL_LITTLE));
2362 r_jmptable= (0 != (bytes->r_type[0] & RELOC_STD_BITS_JMPTABLE_LITTLE));
2363 r_relative= (0 != (bytes->r_type[0] & RELOC_STD_BITS_RELATIVE_LITTLE));
2364 r_length = (bytes->r_type[0] & RELOC_STD_BITS_LENGTH_LITTLE)
2365 >> RELOC_STD_BITS_LENGTH_SH_LITTLE;
2368 howto_idx = r_length + 4 * r_pcrel + 8 * r_baserel
2369 + 16 * r_jmptable + 32 * r_relative;
2370 BFD_ASSERT (howto_idx < TABLE_SIZE (howto_table_std));
2371 cache_ptr->howto = howto_table_std + howto_idx;
2372 BFD_ASSERT (cache_ptr->howto->type != -1);
2377 /* Read and swap the relocs for a section. */
2380 NAME(aout,slurp_reloc_table) (abfd, asect, symbols)
2386 bfd_size_type reloc_size;
2388 arelent *reloc_cache;
2390 unsigned int counter = 0;
2393 if (asect->relocation)
2396 if (asect->flags & SEC_CONSTRUCTOR)
2399 if (asect == obj_datasec (abfd))
2400 reloc_size = exec_hdr(abfd)->a_drsize;
2401 else if (asect == obj_textsec (abfd))
2402 reloc_size = exec_hdr(abfd)->a_trsize;
2405 bfd_set_error (bfd_error_invalid_operation);
2409 if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0)
2412 each_size = obj_reloc_entry_size (abfd);
2414 count = reloc_size / each_size;
2416 reloc_cache = (arelent *) malloc ((size_t) (count * sizeof (arelent)));
2417 if (reloc_cache == NULL && count != 0)
2419 bfd_set_error (bfd_error_no_memory);
2422 memset (reloc_cache, 0, count * sizeof (arelent));
2424 relocs = malloc (reloc_size);
2425 if (relocs == NULL && reloc_size != 0)
2428 bfd_set_error (bfd_error_no_memory);
2432 if (bfd_read (relocs, 1, reloc_size, abfd) != reloc_size)
2439 cache_ptr = reloc_cache;
2440 if (each_size == RELOC_EXT_SIZE)
2442 register struct reloc_ext_external *rptr =
2443 (struct reloc_ext_external *) relocs;
2445 for (; counter < count; counter++, rptr++, cache_ptr++)
2446 NAME(aout,swap_ext_reloc_in) (abfd, rptr, cache_ptr, symbols);
2450 register struct reloc_std_external *rptr =
2451 (struct reloc_std_external *) relocs;
2453 for (; counter < count; counter++, rptr++, cache_ptr++)
2454 NAME(aout,swap_std_reloc_in) (abfd, rptr, cache_ptr, symbols);
2459 asect->relocation = reloc_cache;
2460 asect->reloc_count = cache_ptr - reloc_cache;
2465 /* Write out a relocation section into an object file. */
2468 NAME(aout,squirt_out_relocs) (abfd, section)
2473 unsigned char *native, *natptr;
2476 unsigned int count = section->reloc_count;
2479 if (count == 0) return true;
2481 each_size = obj_reloc_entry_size (abfd);
2482 natsize = each_size * count;
2483 native = (unsigned char *) bfd_zalloc (abfd, natsize);
2485 bfd_set_error (bfd_error_no_memory);
2489 generic = section->orelocation;
2491 if (each_size == RELOC_EXT_SIZE)
2493 for (natptr = native;
2495 --count, natptr += each_size, ++generic)
2496 NAME(aout,swap_ext_reloc_out) (abfd, *generic, (struct reloc_ext_external *)natptr);
2500 for (natptr = native;
2502 --count, natptr += each_size, ++generic)
2503 NAME(aout,swap_std_reloc_out)(abfd, *generic, (struct reloc_std_external *)natptr);
2506 if ( bfd_write ((PTR) native, 1, natsize, abfd) != natsize) {
2507 bfd_release(abfd, native);
2510 bfd_release (abfd, native);
2515 /* This is stupid. This function should be a boolean predicate */
2517 NAME(aout,canonicalize_reloc) (abfd, section, relptr, symbols)
2523 arelent *tblptr = section->relocation;
2526 if (!(tblptr || NAME(aout,slurp_reloc_table)(abfd, section, symbols)))
2529 if (section->flags & SEC_CONSTRUCTOR) {
2530 arelent_chain *chain = section->constructor_chain;
2531 for (count = 0; count < section->reloc_count; count ++) {
2532 *relptr ++ = &chain->relent;
2533 chain = chain->next;
2537 tblptr = section->relocation;
2539 for (count = 0; count++ < section->reloc_count;)
2541 *relptr++ = tblptr++;
2546 return section->reloc_count;
2550 NAME(aout,get_reloc_upper_bound) (abfd, asect)
2554 if (bfd_get_format (abfd) != bfd_object) {
2555 bfd_set_error (bfd_error_invalid_operation);
2558 if (asect->flags & SEC_CONSTRUCTOR) {
2559 return (sizeof (arelent *) * (asect->reloc_count+1));
2562 if (asect == obj_datasec (abfd))
2563 return (sizeof (arelent *)
2564 * ((exec_hdr(abfd)->a_drsize / obj_reloc_entry_size (abfd))
2567 if (asect == obj_textsec (abfd))
2568 return (sizeof (arelent *)
2569 * ((exec_hdr(abfd)->a_trsize / obj_reloc_entry_size (abfd))
2572 bfd_set_error (bfd_error_invalid_operation);
2578 NAME(aout,get_symtab_upper_bound) (abfd)
2581 if (!NAME(aout,slurp_symbol_table)(abfd))
2584 return (bfd_get_symcount (abfd)+1) * (sizeof (aout_symbol_type *));
2589 NAME(aout,get_lineno) (ignore_abfd, ignore_symbol)
2591 asymbol *ignore_symbol;
2593 return (alent *)NULL;
2598 NAME(aout,get_symbol_info) (ignore_abfd, symbol, ret)
2603 bfd_symbol_info (symbol, ret);
2605 if (ret->type == '?')
2607 int type_code = aout_symbol(symbol)->type & 0xff;
2608 CONST char *stab_name = aout_stab_name(type_code);
2609 static char buf[10];
2611 if (stab_name == NULL)
2613 sprintf(buf, "(%d)", type_code);
2617 ret->stab_other = (unsigned)(aout_symbol(symbol)->other & 0xff);
2618 ret->stab_desc = (unsigned)(aout_symbol(symbol)->desc & 0xffff);
2619 ret->stab_name = stab_name;
2625 NAME(aout,print_symbol) (ignore_abfd, afile, symbol, how)
2629 bfd_print_symbol_type how;
2631 FILE *file = (FILE *)afile;
2634 case bfd_print_symbol_name:
2636 fprintf(file,"%s", symbol->name);
2638 case bfd_print_symbol_more:
2639 fprintf(file,"%4x %2x %2x",(unsigned)(aout_symbol(symbol)->desc & 0xffff),
2640 (unsigned)(aout_symbol(symbol)->other & 0xff),
2641 (unsigned)(aout_symbol(symbol)->type));
2643 case bfd_print_symbol_all:
2645 CONST char *section_name = symbol->section->name;
2648 bfd_print_symbol_vandf((PTR)file,symbol);
2650 fprintf(file," %-5s %04x %02x %02x",
2652 (unsigned)(aout_symbol(symbol)->desc & 0xffff),
2653 (unsigned)(aout_symbol(symbol)->other & 0xff),
2654 (unsigned)(aout_symbol(symbol)->type & 0xff));
2656 fprintf(file," %s", symbol->name);
2663 provided a BFD, a section and an offset into the section, calculate
2664 and return the name of the source file and the line nearest to the
2669 NAME(aout,find_nearest_line)
2670 (abfd, section, symbols, offset, filename_ptr, functionname_ptr, line_ptr)
2675 CONST char **filename_ptr;
2676 CONST char **functionname_ptr;
2677 unsigned int *line_ptr;
2679 /* Run down the file looking for the filename, function and linenumber */
2681 static char buffer[100];
2682 static char filename_buffer[200];
2683 CONST char *directory_name = NULL;
2684 CONST char *main_file_name = NULL;
2685 CONST char *current_file_name = NULL;
2686 CONST char *line_file_name = NULL; /* Value of current_file_name at line number. */
2687 bfd_vma high_line_vma = ~0;
2688 bfd_vma low_func_vma = 0;
2690 *filename_ptr = abfd->filename;
2691 *functionname_ptr = 0;
2693 if (symbols != (asymbol **)NULL) {
2694 for (p = symbols; *p; p++) {
2695 aout_symbol_type *q = (aout_symbol_type *)(*p);
2699 main_file_name = current_file_name = q->symbol.name;
2700 /* Look ahead to next symbol to check if that too is an N_SO. */
2704 q = (aout_symbol_type *)(*p);
2705 if (q->type != (int)N_SO)
2708 /* Found a second N_SO First is directory; second is filename. */
2709 directory_name = current_file_name;
2710 main_file_name = current_file_name = q->symbol.name;
2711 if (obj_textsec(abfd) != section)
2715 current_file_name = q->symbol.name;
2722 /* We'll keep this if it resolves nearer than the one we have already */
2723 if (q->symbol.value >= offset &&
2724 q->symbol.value < high_line_vma) {
2725 *line_ptr = q->desc;
2726 high_line_vma = q->symbol.value;
2727 line_file_name = current_file_name;
2732 /* We'll keep this if it is nearer than the one we have already */
2733 if (q->symbol.value >= low_func_vma &&
2734 q->symbol.value <= offset) {
2735 low_func_vma = q->symbol.value;
2736 func = (asymbol *)q;
2738 if (*line_ptr && func) {
2739 CONST char *function = func->name;
2742 /* The caller expects a symbol name. We actually have a
2743 function name, without the leading underscore. Put the
2744 underscore back in, so that the caller gets a symbol
2746 if (bfd_get_symbol_leading_char (abfd) == '\0')
2747 strncpy (buffer, function, sizeof (buffer) - 1);
2750 buffer[0] = bfd_get_symbol_leading_char (abfd);
2751 strncpy (buffer + 1, function, sizeof (buffer) - 2);
2753 buffer[sizeof(buffer)-1] = 0;
2754 /* Have to remove : stuff */
2755 p = strchr(buffer,':');
2756 if (p != NULL) { *p = '\0'; }
2757 *functionname_ptr = buffer;
2769 main_file_name = line_file_name;
2770 if (main_file_name) {
2771 if (main_file_name[0] == '/' || directory_name == NULL)
2772 *filename_ptr = main_file_name;
2774 sprintf(filename_buffer, "%.140s%.50s",
2775 directory_name, main_file_name);
2776 *filename_ptr = filename_buffer;
2785 NAME(aout,sizeof_headers) (abfd, execable)
2789 return adata(abfd).exec_bytes_size;
2792 /* Free all information we have cached for this BFD. We can always
2793 read it again later if we need it. */
2796 NAME(aout,bfd_free_cached_info) (abfd)
2801 if (bfd_get_format (abfd) != bfd_object)
2804 #define FREE(x) if (x != NULL) { free (x); x = NULL; }
2805 FREE (obj_aout_symbols (abfd));
2806 FREE (obj_aout_external_syms (abfd));
2807 FREE (obj_aout_external_strings (abfd));
2808 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
2809 FREE (o->relocation);
2815 /* a.out link code. */
2817 static boolean aout_link_add_object_symbols
2818 PARAMS ((bfd *, struct bfd_link_info *));
2819 static boolean aout_link_check_archive_element
2820 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
2821 static boolean aout_link_free_symbols PARAMS ((bfd *));
2822 static boolean aout_link_check_ar_symbols
2823 PARAMS ((bfd *, struct bfd_link_info *, boolean *pneeded));
2824 static boolean aout_link_add_symbols
2825 PARAMS ((bfd *, struct bfd_link_info *));
2827 /* Routine to create an entry in an a.out link hash table. */
2829 struct bfd_hash_entry *
2830 NAME(aout,link_hash_newfunc) (entry, table, string)
2831 struct bfd_hash_entry *entry;
2832 struct bfd_hash_table *table;
2835 struct aout_link_hash_entry *ret = (struct aout_link_hash_entry *) entry;
2837 /* Allocate the structure if it has not already been allocated by a
2839 if (ret == (struct aout_link_hash_entry *) NULL)
2840 ret = ((struct aout_link_hash_entry *)
2841 bfd_hash_allocate (table, sizeof (struct aout_link_hash_entry)));
2842 if (ret == (struct aout_link_hash_entry *) NULL)
2844 bfd_set_error (bfd_error_no_memory);
2845 return (struct bfd_hash_entry *) ret;
2848 /* Call the allocation method of the superclass. */
2849 ret = ((struct aout_link_hash_entry *)
2850 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2854 /* Set local fields. */
2855 ret->written = false;
2859 return (struct bfd_hash_entry *) ret;
2862 /* Initialize an a.out link hash table. */
2865 NAME(aout,link_hash_table_init) (table, abfd, newfunc)
2866 struct aout_link_hash_table *table;
2868 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
2869 struct bfd_hash_table *,
2872 return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
2875 /* Create an a.out link hash table. */
2877 struct bfd_link_hash_table *
2878 NAME(aout,link_hash_table_create) (abfd)
2881 struct aout_link_hash_table *ret;
2883 ret = ((struct aout_link_hash_table *)
2884 malloc (sizeof (struct aout_link_hash_table)));
2885 if (ret == (struct aout_link_hash_table *) NULL)
2887 bfd_set_error (bfd_error_no_memory);
2888 return (struct bfd_link_hash_table *) NULL;
2890 if (! NAME(aout,link_hash_table_init) (ret, abfd,
2891 NAME(aout,link_hash_newfunc)))
2894 return (struct bfd_link_hash_table *) NULL;
2899 /* Given an a.out BFD, add symbols to the global hash table as
2903 NAME(aout,link_add_symbols) (abfd, info)
2905 struct bfd_link_info *info;
2907 switch (bfd_get_format (abfd))
2910 return aout_link_add_object_symbols (abfd, info);
2912 return _bfd_generic_link_add_archive_symbols
2913 (abfd, info, aout_link_check_archive_element);
2915 bfd_set_error (bfd_error_wrong_format);
2920 /* Add symbols from an a.out object file. */
2923 aout_link_add_object_symbols (abfd, info)
2925 struct bfd_link_info *info;
2927 if (! aout_get_external_symbols (abfd))
2929 if (! aout_link_add_symbols (abfd, info))
2931 if (! info->keep_memory)
2933 if (! aout_link_free_symbols (abfd))
2939 /* Check a single archive element to see if we need to include it in
2940 the link. *PNEEDED is set according to whether this element is
2941 needed in the link or not. This is called from
2942 _bfd_generic_link_add_archive_symbols. */
2945 aout_link_check_archive_element (abfd, info, pneeded)
2947 struct bfd_link_info *info;
2950 if (! aout_get_external_symbols (abfd))
2953 if (! aout_link_check_ar_symbols (abfd, info, pneeded))
2958 if (! aout_link_add_symbols (abfd, info))
2962 /* We keep around the symbols even if we aren't going to use this
2963 object file, because we may want to reread it. This doesn't
2964 waste too much memory, because it isn't all that common to read
2965 an archive element but not need it. */
2966 if (! info->keep_memory)
2968 if (! aout_link_free_symbols (abfd))
2975 /* Free up the internal symbols read from an a.out file. */
2978 aout_link_free_symbols (abfd)
2981 if (obj_aout_external_syms (abfd) != (struct external_nlist *) NULL)
2983 free ((PTR) obj_aout_external_syms (abfd));
2984 obj_aout_external_syms (abfd) = (struct external_nlist *) NULL;
2986 if (obj_aout_external_strings (abfd) != (char *) NULL)
2988 free ((PTR) obj_aout_external_strings (abfd));
2989 obj_aout_external_strings (abfd) = (char *) NULL;
2994 /* Look through the internal symbols to see if this object file should
2995 be included in the link. We should include this object file if it
2996 defines any symbols which are currently undefined. If this object
2997 file defines a common symbol, then we may adjust the size of the
2998 known symbol but we do not include the object file in the link
2999 (unless there is some other reason to include it). */
3002 aout_link_check_ar_symbols (abfd, info, pneeded)
3004 struct bfd_link_info *info;
3007 register struct external_nlist *p;
3008 struct external_nlist *pend;
3013 /* Look through all the symbols. */
3014 p = obj_aout_external_syms (abfd);
3015 pend = p + obj_aout_external_sym_count (abfd);
3016 strings = obj_aout_external_strings (abfd);
3017 for (; p < pend; p++)
3019 int type = bfd_h_get_8 (abfd, p->e_type);
3021 struct bfd_link_hash_entry *h;
3023 /* Ignore symbols that are not externally visible. */
3024 if ((type & N_EXT) == 0)
3026 if (type == N_WARNING
3032 name = strings + GET_WORD (abfd, p->e_strx);
3033 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
3035 /* We are only interested in symbols that are currently
3036 undefined or common. */
3037 if (h == (struct bfd_link_hash_entry *) NULL
3038 || (h->type != bfd_link_hash_undefined
3039 && h->type != bfd_link_hash_common))
3041 if (type == (N_INDR | N_EXT))
3046 if (type == (N_TEXT | N_EXT)
3047 || type == (N_DATA | N_EXT)
3048 || type == (N_BSS | N_EXT)
3049 || type == (N_ABS | N_EXT)
3050 || type == (N_INDR | N_EXT))
3052 /* This object file defines this symbol. We must link it
3053 in. This is true regardless of whether the current
3054 definition of the symbol is undefined or common. If the
3055 current definition is common, we have a case in which we
3056 have already seen an object file including
3058 and this object file from the archive includes
3060 In such a case we must include this object file. */
3061 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
3067 if (type == (N_UNDF | N_EXT))
3071 value = GET_WORD (abfd, p->e_value);
3074 /* This symbol is common in the object from the archive
3076 if (h->type == bfd_link_hash_undefined)
3080 symbfd = h->u.undef.abfd;
3081 if (symbfd == (bfd *) NULL)
3083 /* This symbol was created as undefined from
3084 outside BFD. We assume that we should link
3085 in the object file. This is done for the -u
3086 option in the linker. */
3087 if (! (*info->callbacks->add_archive_element) (info,
3094 /* Turn the current link symbol into a common
3095 symbol. It is already on the undefs list. */
3096 h->type = bfd_link_hash_common;
3097 h->u.c.size = value;
3098 h->u.c.section = bfd_make_section_old_way (symbfd,
3103 /* Adjust the size of the common symbol if
3105 if (value > h->u.c.size)
3106 h->u.c.size = value;
3112 /* We do not need this object file. */
3116 /* Add all symbols from an object file to the hash table. */
3119 aout_link_add_symbols (abfd, info)
3121 struct bfd_link_info *info;
3123 boolean (*add_one_symbol) PARAMS ((struct bfd_link_info *, bfd *,
3124 const char *, flagword, asection *,
3125 bfd_vma, const char *, boolean,
3127 struct bfd_link_hash_entry **));
3128 bfd_size_type sym_count;
3131 struct aout_link_hash_entry **sym_hash;
3132 register struct external_nlist *p;
3133 struct external_nlist *pend;
3135 sym_count = obj_aout_external_sym_count (abfd);
3136 strings = obj_aout_external_strings (abfd);
3137 if (info->keep_memory)
3142 /* We keep a list of the linker hash table entries that correspond
3143 to particular symbols. We could just look them up in the hash
3144 table, but keeping the list is more efficient. Perhaps this
3145 should be conditional on info->keep_memory. */
3146 sym_hash = ((struct aout_link_hash_entry **)
3149 * sizeof (struct aout_link_hash_entry *))));
3150 if (sym_hash == NULL && sym_count != 0)
3152 bfd_set_error (bfd_error_no_memory);
3155 obj_aout_sym_hashes (abfd) = sym_hash;
3157 if ((abfd->flags & DYNAMIC) != 0
3158 && aout_backend_info (abfd)->add_dynamic_symbols != NULL)
3160 if (! (*aout_backend_info (abfd)->add_dynamic_symbols) (abfd, info))
3164 add_one_symbol = aout_backend_info (abfd)->add_one_symbol;
3165 if (add_one_symbol == NULL)
3166 add_one_symbol = _bfd_generic_link_add_one_symbol;
3168 p = obj_aout_external_syms (abfd);
3169 pend = p + sym_count;
3170 for (; p < pend; p++, sym_hash++)
3181 type = bfd_h_get_8 (abfd, p->e_type);
3183 /* Ignore debugging symbols. */
3184 if ((type & N_STAB) != 0)
3187 /* Ignore symbols that are not external. */
3188 if ((type & N_EXT) == 0
3189 && type != N_WARNING
3195 /* If this is an N_INDR symbol we must skip the next entry,
3196 which is the symbol to indirect to (actually, an N_INDR
3197 symbol without N_EXT set is pretty useless). */
3206 /* Ignore N_FN symbols (these appear to have N_EXT set). */
3210 name = strings + GET_WORD (abfd, p->e_strx);
3211 value = GET_WORD (abfd, p->e_value);
3218 case N_UNDF | N_EXT:
3220 section = &bfd_com_section;
3222 section = &bfd_und_section;
3225 section = &bfd_abs_section;
3227 case N_TEXT | N_EXT:
3228 section = obj_textsec (abfd);
3229 value -= bfd_get_section_vma (abfd, section);
3231 case N_DATA | N_EXT:
3232 section = obj_datasec (abfd);
3233 value -= bfd_get_section_vma (abfd, section);
3236 section = obj_bsssec (abfd);
3237 value -= bfd_get_section_vma (abfd, section);
3239 case N_INDR | N_EXT:
3240 /* An indirect symbol. The next symbol is the symbol
3241 which this one really is. */
3242 BFD_ASSERT (p + 1 < pend);
3244 string = strings + GET_WORD (abfd, p->e_strx);
3245 section = &bfd_ind_section;
3246 flags |= BSF_INDIRECT;
3248 case N_COMM | N_EXT:
3249 section = &bfd_com_section;
3251 case N_SETA: case N_SETA | N_EXT:
3252 section = &bfd_abs_section;
3253 flags |= BSF_CONSTRUCTOR;
3255 case N_SETT: case N_SETT | N_EXT:
3256 section = obj_textsec (abfd);
3257 flags |= BSF_CONSTRUCTOR;
3258 value -= bfd_get_section_vma (abfd, section);
3260 case N_SETD: case N_SETD | N_EXT:
3261 section = obj_datasec (abfd);
3262 flags |= BSF_CONSTRUCTOR;
3263 value -= bfd_get_section_vma (abfd, section);
3265 case N_SETB: case N_SETB | N_EXT:
3266 section = obj_bsssec (abfd);
3267 flags |= BSF_CONSTRUCTOR;
3268 value -= bfd_get_section_vma (abfd, section);
3271 /* A warning symbol. The next symbol is the one to warn
3273 BFD_ASSERT (p + 1 < pend);
3276 name = strings + GET_WORD (abfd, p->e_strx);
3277 section = &bfd_und_section;
3278 flags |= BSF_WARNING;
3282 if (! ((*add_one_symbol)
3283 (info, abfd, name, flags, section, value, string, copy, false,
3284 (struct bfd_link_hash_entry **) sym_hash)))
3287 if (type == (N_INDR | N_EXT) || type == N_WARNING)
3294 /* During the final link step we need to pass around a bunch of
3295 information, so we do it in an instance of this structure. */
3297 struct aout_final_link_info
3299 /* General link information. */
3300 struct bfd_link_info *info;
3303 /* Reloc file positions. */
3304 file_ptr treloff, dreloff;
3305 /* File position of symbols. */
3308 struct stringtab_data strtab;
3311 static boolean aout_link_input_bfd
3312 PARAMS ((struct aout_final_link_info *, bfd *input_bfd));
3313 static boolean aout_link_write_symbols
3314 PARAMS ((struct aout_final_link_info *, bfd *input_bfd, int *symbol_map));
3315 static boolean aout_link_write_other_symbol
3316 PARAMS ((struct aout_link_hash_entry *, PTR));
3317 static boolean aout_link_input_section
3318 PARAMS ((struct aout_final_link_info *, bfd *input_bfd,
3319 asection *input_section, file_ptr *reloff_ptr,
3320 bfd_size_type rel_size, int *symbol_map));
3321 static boolean aout_link_input_section_std
3322 PARAMS ((struct aout_final_link_info *, bfd *input_bfd,
3323 asection *input_section, struct reloc_std_external *,
3324 bfd_size_type rel_size, bfd_byte *contents, int *symbol_map));
3325 static boolean aout_link_input_section_ext
3326 PARAMS ((struct aout_final_link_info *, bfd *input_bfd,
3327 asection *input_section, struct reloc_ext_external *,
3328 bfd_size_type rel_size, bfd_byte *contents, int *symbol_map));
3329 static INLINE asection *aout_reloc_index_to_section
3330 PARAMS ((bfd *, int));
3331 static boolean aout_link_reloc_link_order
3332 PARAMS ((struct aout_final_link_info *, asection *,
3333 struct bfd_link_order *));
3335 /* Do the final link step. This is called on the output BFD. The
3336 INFO structure should point to a list of BFDs linked through the
3337 link_next field which can be used to find each BFD which takes part
3338 in the output. Also, each section in ABFD should point to a list
3339 of bfd_link_order structures which list all the input sections for
3340 the output section. */
3343 NAME(aout,final_link) (abfd, info, callback)
3345 struct bfd_link_info *info;
3346 void (*callback) PARAMS ((bfd *, file_ptr *, file_ptr *, file_ptr *));
3348 struct aout_final_link_info aout_info;
3350 bfd_size_type text_size;
3352 register struct bfd_link_order *p;
3354 boolean have_link_order_relocs;
3356 aout_info.info = info;
3357 aout_info.output_bfd = abfd;
3359 if (! info->relocateable)
3361 exec_hdr (abfd)->a_trsize = 0;
3362 exec_hdr (abfd)->a_drsize = 0;
3366 bfd_size_type trsize, drsize;
3368 /* Count up the relocation sizes. */
3371 for (sub = info->input_bfds; sub != (bfd *) NULL; sub = sub->link_next)
3373 if (bfd_get_flavour (abfd) == bfd_target_aout_flavour)
3375 trsize += exec_hdr (sub)->a_trsize;
3376 drsize += exec_hdr (sub)->a_drsize;
3380 /* FIXME: We need to identify the .text and .data sections
3381 and call get_reloc_upper_bound and canonicalize_reloc to
3382 work out the number of relocs needed, and then multiply
3383 by the reloc size. */
3387 if (obj_textsec (abfd) != (asection *) NULL)
3388 trsize += (_bfd_count_link_order_relocs (obj_textsec (abfd)
3390 * obj_reloc_entry_size (abfd));
3391 exec_hdr (abfd)->a_trsize = trsize;
3392 if (obj_datasec (abfd) != (asection *) NULL)
3393 drsize += (_bfd_count_link_order_relocs (obj_datasec (abfd)
3395 * obj_reloc_entry_size (abfd));
3396 exec_hdr (abfd)->a_drsize = drsize;
3399 exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd);
3401 /* Adjust the section sizes and vmas according to the magic number.
3402 This sets a_text, a_data and a_bss in the exec_hdr and sets the
3403 filepos for each section. */
3404 if (! NAME(aout,adjust_sizes_and_vmas) (abfd, &text_size, &text_end))
3407 /* The relocation and symbol file positions differ among a.out
3408 targets. We are passed a callback routine from the backend
3409 specific code to handle this.
3410 FIXME: At this point we do not know how much space the symbol
3411 table will require. This will not work for any (nonstandard)
3412 a.out target that needs to know the symbol table size before it
3413 can compute the relocation file positions. This may or may not
3414 be the case for the hp300hpux target, for example. */
3415 (*callback) (abfd, &aout_info.treloff, &aout_info.dreloff,
3417 obj_textsec (abfd)->rel_filepos = aout_info.treloff;
3418 obj_datasec (abfd)->rel_filepos = aout_info.dreloff;
3419 obj_sym_filepos (abfd) = aout_info.symoff;
3421 /* We keep a count of the symbols as we output them. */
3422 obj_aout_external_sym_count (abfd) = 0;
3424 /* We accumulate the string table as we write out the symbols. */
3425 stringtab_init (&aout_info.strtab);
3427 /* The most time efficient way to do the link would be to read all
3428 the input object files into memory and then sort out the
3429 information into the output file. Unfortunately, that will
3430 probably use too much memory. Another method would be to step
3431 through everything that composes the text section and write it
3432 out, and then everything that composes the data section and write
3433 it out, and then write out the relocs, and then write out the
3434 symbols. Unfortunately, that requires reading stuff from each
3435 input file several times, and we will not be able to keep all the
3436 input files open simultaneously, and reopening them will be slow.
3438 What we do is basically process one input file at a time. We do
3439 everything we need to do with an input file once--copy over the
3440 section contents, handle the relocation information, and write
3441 out the symbols--and then we throw away the information we read
3442 from it. This approach requires a lot of lseeks of the output
3443 file, which is unfortunate but still faster than reopening a lot
3446 We use the output_has_begun field of the input BFDs to see
3447 whether we have already handled it. */
3448 for (sub = info->input_bfds; sub != (bfd *) NULL; sub = sub->link_next)
3449 sub->output_has_begun = false;
3451 have_link_order_relocs = false;
3452 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3454 for (p = o->link_order_head;
3455 p != (struct bfd_link_order *) NULL;
3458 if (p->type == bfd_indirect_link_order
3459 && (bfd_get_flavour (p->u.indirect.section->owner)
3460 == bfd_target_aout_flavour))
3464 input_bfd = p->u.indirect.section->owner;
3465 if (! input_bfd->output_has_begun)
3467 if (! aout_link_input_bfd (&aout_info, input_bfd))
3469 input_bfd->output_has_begun = true;
3472 else if (p->type == bfd_section_reloc_link_order
3473 || p->type == bfd_symbol_reloc_link_order)
3475 /* These are handled below. */
3476 have_link_order_relocs = true;
3480 if (! _bfd_default_link_order (abfd, info, o, p))
3486 /* Write out any symbols that we have not already written out. */
3487 aout_link_hash_traverse (aout_hash_table (info),
3488 aout_link_write_other_symbol,
3491 /* Now handle any relocs we were asked to create by the linker.
3492 These did not come from any input file. We must do these after
3493 we have written out all the symbols, so that we know the symbol
3495 if (have_link_order_relocs)
3497 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3499 for (p = o->link_order_head;
3500 p != (struct bfd_link_order *) NULL;
3503 if (p->type == bfd_section_reloc_link_order
3504 || p->type == bfd_symbol_reloc_link_order)
3506 if (! aout_link_reloc_link_order (&aout_info, o, p))
3513 /* Finish up any dynamic linking we may be doing. */
3514 if (aout_backend_info (abfd)->finish_dynamic_link != NULL)
3516 if (! (*aout_backend_info (abfd)->finish_dynamic_link) (abfd, info))
3520 /* Update the header information. */
3521 abfd->symcount = obj_aout_external_sym_count (abfd);
3522 exec_hdr (abfd)->a_syms = abfd->symcount * EXTERNAL_NLIST_SIZE;
3523 obj_str_filepos (abfd) = obj_sym_filepos (abfd) + exec_hdr (abfd)->a_syms;
3524 obj_textsec (abfd)->reloc_count =
3525 exec_hdr (abfd)->a_trsize / obj_reloc_entry_size (abfd);
3526 obj_datasec (abfd)->reloc_count =
3527 exec_hdr (abfd)->a_drsize / obj_reloc_entry_size (abfd);
3529 /* Write out the string table. */
3530 if (bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET) != 0)
3532 return emit_strtab (abfd, &aout_info.strtab);
3535 /* Link an a.out input BFD into the output file. */
3538 aout_link_input_bfd (finfo, input_bfd)
3539 struct aout_final_link_info *finfo;
3542 bfd_size_type sym_count;
3543 int *symbol_map = NULL;
3545 BFD_ASSERT (bfd_get_format (input_bfd) == bfd_object);
3547 /* If this is a dynamic object, it may need special handling. */
3548 if ((input_bfd->flags & DYNAMIC) != 0
3549 && aout_backend_info (input_bfd)->link_dynamic_object != NULL)
3551 return ((*aout_backend_info (input_bfd)->link_dynamic_object)
3552 (finfo->info, input_bfd));
3555 /* Get the symbols. We probably have them already, unless
3556 finfo->info->keep_memory is false. */
3557 if (! aout_get_external_symbols (input_bfd))
3560 sym_count = obj_aout_external_sym_count (input_bfd);
3561 symbol_map = (int *) malloc ((size_t) sym_count * sizeof (int));
3562 if (symbol_map == NULL && sym_count != 0)
3564 bfd_set_error (bfd_error_no_memory);
3568 /* Write out the symbols and get a map of the new indices. */
3569 if (! aout_link_write_symbols (finfo, input_bfd, symbol_map))
3572 /* Relocate and write out the sections. */
3573 if (! aout_link_input_section (finfo, input_bfd,
3574 obj_textsec (input_bfd),
3576 exec_hdr (input_bfd)->a_trsize,
3578 || ! aout_link_input_section (finfo, input_bfd,
3579 obj_datasec (input_bfd),
3581 exec_hdr (input_bfd)->a_drsize,
3585 /* If we are not keeping memory, we don't need the symbols any
3586 longer. We still need them if we are keeping memory, because the
3587 strings in the hash table point into them. */
3588 if (! finfo->info->keep_memory)
3590 if (! aout_link_free_symbols (input_bfd))
3594 if (symbol_map != NULL)
3598 if (symbol_map != NULL)
3603 /* Adjust and write out the symbols for an a.out file. Set the new
3604 symbol indices into a symbol_map. */
3607 aout_link_write_symbols (finfo, input_bfd, symbol_map)
3608 struct aout_final_link_info *finfo;
3613 bfd_size_type sym_count;
3615 enum bfd_link_strip strip;
3616 enum bfd_link_discard discard;
3617 struct external_nlist *output_syms = NULL;
3618 struct external_nlist *outsym;
3619 register struct external_nlist *sym;
3620 struct external_nlist *sym_end;
3621 struct aout_link_hash_entry **sym_hash;
3623 boolean skip_indirect;
3625 output_bfd = finfo->output_bfd;
3626 sym_count = obj_aout_external_sym_count (input_bfd);
3627 strings = obj_aout_external_strings (input_bfd);
3628 strip = finfo->info->strip;
3629 discard = finfo->info->discard;
3630 output_syms = ((struct external_nlist *)
3631 malloc ((size_t) (sym_count + 1) * EXTERNAL_NLIST_SIZE));
3632 if (output_syms == NULL)
3634 bfd_set_error (bfd_error_no_memory);
3637 outsym = output_syms;
3639 /* First write out a symbol for this object file, unless we are
3640 discarding such symbols. */
3641 if (strip != strip_all
3642 && (strip != strip_some
3643 || bfd_hash_lookup (finfo->info->keep_hash, input_bfd->filename,
3644 false, false) != NULL)
3645 && discard != discard_all)
3647 bfd_h_put_8 (output_bfd, N_TEXT, outsym->e_type);
3648 bfd_h_put_8 (output_bfd, 0, outsym->e_other);
3649 bfd_h_put_16 (output_bfd, (bfd_vma) 0, outsym->e_desc);
3650 PUT_WORD (output_bfd,
3651 add_to_stringtab (output_bfd, input_bfd->filename,
3654 PUT_WORD (output_bfd,
3655 (bfd_get_section_vma (output_bfd,
3656 obj_textsec (input_bfd)->output_section)
3657 + obj_textsec (input_bfd)->output_offset),
3659 ++obj_aout_external_sym_count (output_bfd);
3664 skip_indirect = false;
3665 sym = obj_aout_external_syms (input_bfd);
3666 sym_end = sym + sym_count;
3667 sym_hash = obj_aout_sym_hashes (input_bfd);
3668 for (; sym < sym_end; sym++, sym_hash++, symbol_map++)
3672 struct aout_link_hash_entry *h;
3679 type = bfd_h_get_8 (input_bfd, sym->e_type);
3680 name = strings + GET_WORD (input_bfd, sym->e_strx);
3686 /* Pass this symbol through. It is the target of an
3687 indirect or warning symbol. */
3688 val = GET_WORD (input_bfd, sym->e_value);
3691 else if (skip_indirect)
3693 /* Skip this symbol, which is the target of an indirect
3694 symbol that we have changed to no longer be an indirect
3696 skip_indirect = false;
3701 struct aout_link_hash_entry *hresolve;
3703 /* We have saved the hash table entry for this symbol, if
3704 there is one. Note that we could just look it up again
3705 in the hash table, provided we first check that it is an
3709 /* If this is an indirect or warning symbol, then change
3710 hresolve to the base symbol. We also change *sym_hash so
3711 that the relocation routines relocate against the real
3714 if (h != (struct aout_link_hash_entry *) NULL
3715 && (h->root.type == bfd_link_hash_indirect
3716 || h->root.type == bfd_link_hash_warning))
3718 hresolve = (struct aout_link_hash_entry *) h->root.u.i.link;
3719 while (hresolve->root.type == bfd_link_hash_indirect
3720 || hresolve->root.type == bfd_link_hash_warning)
3721 hresolve = ((struct aout_link_hash_entry *)
3722 hresolve->root.u.i.link);
3723 *sym_hash = hresolve;
3726 /* If the symbol has already been written out, skip it. */
3727 if (h != (struct aout_link_hash_entry *) NULL
3728 && h->root.type != bfd_link_hash_warning
3731 if ((type & N_TYPE) == N_INDR)
3732 skip_indirect = true;
3733 *symbol_map = h->indx;
3737 /* See if we are stripping this symbol. */
3743 case strip_debugger:
3744 if ((type & N_STAB) != 0)
3748 if (bfd_hash_lookup (finfo->info->keep_hash, name, false, false)
3758 if (h != (struct aout_link_hash_entry *) NULL)
3763 /* Get the value of the symbol. */
3764 if ((type & N_TYPE) == N_TEXT)
3765 symsec = obj_textsec (input_bfd);
3766 else if ((type & N_TYPE) == N_DATA)
3767 symsec = obj_datasec (input_bfd);
3768 else if ((type & N_TYPE) == N_BSS)
3769 symsec = obj_bsssec (input_bfd);
3770 else if ((type & N_TYPE) == N_ABS)
3771 symsec = &bfd_abs_section;
3772 else if (((type & N_TYPE) == N_INDR
3773 && (hresolve == (struct aout_link_hash_entry *) NULL
3774 || (hresolve->root.type != bfd_link_hash_defined
3775 && hresolve->root.type != bfd_link_hash_common)))
3776 || type == N_WARNING)
3778 /* Pass the next symbol through unchanged. The
3779 condition above for indirect symbols is so that if
3780 the indirect symbol was defined, we output it with
3781 the correct definition so the debugger will
3784 val = GET_WORD (input_bfd, sym->e_value);
3787 else if ((type & N_STAB) != 0)
3789 val = GET_WORD (input_bfd, sym->e_value);
3794 /* If we get here with an indirect symbol, it means that
3795 we are outputting it with a real definition. In such
3796 a case we do not want to output the next symbol,
3797 which is the target of the indirection. */
3798 if ((type & N_TYPE) == N_INDR)
3799 skip_indirect = true;
3801 /* We need to get the value from the hash table. We use
3802 hresolve so that if we have defined an indirect
3803 symbol we output the final definition. */
3804 if (h == (struct aout_link_hash_entry *) NULL)
3806 else if (hresolve->root.type == bfd_link_hash_defined)
3808 asection *input_section;
3809 asection *output_section;
3811 /* This case means a common symbol which was turned
3812 into a defined symbol. */
3813 input_section = hresolve->root.u.def.section;
3814 output_section = input_section->output_section;
3815 BFD_ASSERT (output_section == &bfd_abs_section
3816 || output_section->owner == output_bfd);
3817 val = (hresolve->root.u.def.value
3818 + bfd_get_section_vma (output_bfd, output_section)
3819 + input_section->output_offset);
3821 /* Get the correct type based on the section. If
3822 this is a constructed set, force it to be
3823 globally visible. */
3832 if (output_section == obj_textsec (output_bfd))
3834 else if (output_section == obj_datasec (output_bfd))
3836 else if (output_section == obj_bsssec (output_bfd))
3841 else if (hresolve->root.type == bfd_link_hash_common)
3842 val = hresolve->root.u.c.size;
3848 if (symsec != (asection *) NULL)
3849 val = (symsec->output_section->vma
3850 + symsec->output_offset
3851 + (GET_WORD (input_bfd, sym->e_value)
3854 /* If this is a global symbol set the written flag, and if
3855 it is a local symbol see if we should discard it. */
3856 if (h != (struct aout_link_hash_entry *) NULL)
3859 h->indx = obj_aout_external_sym_count (output_bfd);
3868 if (*name == *finfo->info->lprefix
3869 && (finfo->info->lprefix_len == 1
3870 || strncmp (name, finfo->info->lprefix,
3871 finfo->info->lprefix_len) == 0))
3886 /* Copy this symbol into the list of symbols we are going to
3888 bfd_h_put_8 (output_bfd, type, outsym->e_type);
3889 bfd_h_put_8 (output_bfd, bfd_h_get_8 (input_bfd, sym->e_other),
3891 bfd_h_put_16 (output_bfd, bfd_h_get_16 (input_bfd, sym->e_desc),
3893 if (! finfo->info->keep_memory)
3895 /* name points into a string table which we are going to
3896 free. If there is a hash table entry, use that string.
3897 Otherwise, copy name into memory. */
3898 if (h != (struct aout_link_hash_entry *) NULL)
3899 name = (*sym_hash)->root.root.string;
3904 n = bfd_alloc (output_bfd, strlen (name) + 1);
3909 PUT_WORD (output_bfd,
3910 add_to_stringtab (output_bfd, name, &finfo->strtab),
3912 PUT_WORD (output_bfd, val, outsym->e_value);
3913 *symbol_map = obj_aout_external_sym_count (output_bfd);
3914 ++obj_aout_external_sym_count (output_bfd);
3918 /* Write out the output symbols we have just constructed. */
3919 if (outsym > output_syms)
3921 bfd_size_type outsym_count;
3923 if (bfd_seek (output_bfd, finfo->symoff, SEEK_SET) != 0)
3925 outsym_count = outsym - output_syms;
3926 if (bfd_write ((PTR) output_syms, (bfd_size_type) EXTERNAL_NLIST_SIZE,
3927 (bfd_size_type) outsym_count, output_bfd)
3928 != outsym_count * EXTERNAL_NLIST_SIZE)
3930 finfo->symoff += outsym_count * EXTERNAL_NLIST_SIZE;
3933 if (output_syms != NULL)
3937 if (output_syms != NULL)
3942 /* Write out a symbol that was not associated with an a.out input
3946 aout_link_write_other_symbol (h, data)
3947 struct aout_link_hash_entry *h;
3950 struct aout_final_link_info *finfo = (struct aout_final_link_info *) data;
3954 struct external_nlist outsym;
3956 output_bfd = finfo->output_bfd;
3958 if (aout_backend_info (output_bfd)->write_dynamic_symbol != NULL)
3960 if (! ((*aout_backend_info (output_bfd)->write_dynamic_symbol)
3961 (output_bfd, finfo->info, h)))
3963 /* FIXME: No way to handle errors. */
3973 if (finfo->info->strip == strip_all
3974 || (finfo->info->strip == strip_some
3975 && bfd_hash_lookup (finfo->info->keep_hash, h->root.root.string,
3976 false, false) == NULL))
3979 switch (h->root.type)
3982 case bfd_link_hash_new:
3984 /* Avoid variable not initialized warnings. */
3986 case bfd_link_hash_undefined:
3987 type = N_UNDF | N_EXT;
3990 case bfd_link_hash_defined:
3994 sec = h->root.u.def.section;
3995 BFD_ASSERT (sec == &bfd_abs_section
3996 || sec->owner == output_bfd);
3997 if (sec == obj_textsec (output_bfd))
3998 type = N_TEXT | N_EXT;
3999 else if (sec == obj_datasec (output_bfd))
4000 type = N_DATA | N_EXT;
4001 else if (sec == obj_bsssec (output_bfd))
4002 type = N_BSS | N_EXT;
4004 type = N_ABS | N_EXT;
4005 val = (h->root.u.def.value
4006 + sec->output_section->vma
4007 + sec->output_offset);
4010 case bfd_link_hash_common:
4011 type = N_UNDF | N_EXT;
4012 val = h->root.u.c.size;
4014 case bfd_link_hash_indirect:
4015 case bfd_link_hash_warning:
4016 /* FIXME: Ignore these for now. The circumstances under which
4017 they should be written out are not clear to me. */
4021 bfd_h_put_8 (output_bfd, type, outsym.e_type);
4022 bfd_h_put_8 (output_bfd, 0, outsym.e_other);
4023 bfd_h_put_16 (output_bfd, 0, outsym.e_desc);
4024 PUT_WORD (output_bfd,
4025 add_to_stringtab (output_bfd, h->root.root.string, &finfo->strtab),
4027 PUT_WORD (output_bfd, val, outsym.e_value);
4029 if (bfd_seek (output_bfd, finfo->symoff, SEEK_SET) != 0
4030 || bfd_write ((PTR) &outsym, (bfd_size_type) EXTERNAL_NLIST_SIZE,
4031 (bfd_size_type) 1, output_bfd) != EXTERNAL_NLIST_SIZE)
4033 /* FIXME: No way to handle errors. */
4037 finfo->symoff += EXTERNAL_NLIST_SIZE;
4038 h->indx = obj_aout_external_sym_count (output_bfd);
4039 ++obj_aout_external_sym_count (output_bfd);
4044 /* Link an a.out section into the output file. */
4047 aout_link_input_section (finfo, input_bfd, input_section, reloff_ptr,
4048 rel_size, symbol_map)
4049 struct aout_final_link_info *finfo;
4051 asection *input_section;
4052 file_ptr *reloff_ptr;
4053 bfd_size_type rel_size;
4056 bfd_size_type input_size;
4057 bfd_byte *contents = NULL;
4059 PTR free_relocs = NULL;
4061 /* Get the section contents. */
4062 input_size = bfd_section_size (input_bfd, input_section);
4063 contents = (bfd_byte *) malloc (input_size);
4064 if (contents == NULL && input_size != 0)
4066 bfd_set_error (bfd_error_no_memory);
4069 if (! bfd_get_section_contents (input_bfd, input_section, (PTR) contents,
4070 (file_ptr) 0, input_size))
4073 /* Read in the relocs if we haven't already done it. */
4074 if (aout_section_data (input_section) != NULL
4075 && aout_section_data (input_section)->relocs != NULL)
4076 relocs = aout_section_data (input_section)->relocs;
4079 relocs = free_relocs = (PTR) malloc (rel_size);
4080 if (relocs == NULL && rel_size != 0)
4082 bfd_set_error (bfd_error_no_memory);
4085 if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0
4086 || bfd_read (relocs, 1, rel_size, input_bfd) != rel_size)
4090 /* Relocate the section contents. */
4091 if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE)
4093 if (! aout_link_input_section_std (finfo, input_bfd, input_section,
4094 (struct reloc_std_external *) relocs,
4095 rel_size, contents, symbol_map))
4100 if (! aout_link_input_section_ext (finfo, input_bfd, input_section,
4101 (struct reloc_ext_external *) relocs,
4102 rel_size, contents, symbol_map))
4106 /* Write out the section contents. */
4107 if (! bfd_set_section_contents (finfo->output_bfd,
4108 input_section->output_section,
4110 input_section->output_offset,
4114 /* If we are producing relocateable output, the relocs were
4115 modified, and we now write them out. */
4116 if (finfo->info->relocateable)
4118 if (bfd_seek (finfo->output_bfd, *reloff_ptr, SEEK_SET) != 0)
4120 if (bfd_write (relocs, (bfd_size_type) 1, rel_size, finfo->output_bfd)
4123 *reloff_ptr += rel_size;
4125 /* Assert that the relocs have not run into the symbols, and
4126 that if these are the text relocs they have not run into the
4128 BFD_ASSERT (*reloff_ptr <= obj_sym_filepos (finfo->output_bfd)
4129 && (reloff_ptr != &finfo->treloff
4131 <= obj_datasec (finfo->output_bfd)->rel_filepos)));
4134 if (free_relocs != NULL)
4136 if (contents != NULL)
4140 if (free_relocs != NULL)
4142 if (contents != NULL)
4147 /* Get the section corresponding to a reloc index. */
4149 static INLINE asection *
4150 aout_reloc_index_to_section (abfd, indx)
4154 switch (indx & N_TYPE)
4157 return obj_textsec (abfd);
4159 return obj_datasec (abfd);
4161 return obj_bsssec (abfd);
4164 return &bfd_abs_section;
4170 /* Relocate an a.out section using standard a.out relocs. */
4173 aout_link_input_section_std (finfo, input_bfd, input_section, relocs,
4174 rel_size, contents, symbol_map)
4175 struct aout_final_link_info *finfo;
4177 asection *input_section;
4178 struct reloc_std_external *relocs;
4179 bfd_size_type rel_size;
4183 boolean (*check_dynamic_reloc) PARAMS ((struct bfd_link_info *,
4185 struct aout_link_hash_entry *,
4188 boolean relocateable;
4189 struct external_nlist *syms;
4191 struct aout_link_hash_entry **sym_hashes;
4192 bfd_size_type reloc_count;
4193 register struct reloc_std_external *rel;
4194 struct reloc_std_external *rel_end;
4196 output_bfd = finfo->output_bfd;
4197 check_dynamic_reloc = aout_backend_info (output_bfd)->check_dynamic_reloc;
4199 BFD_ASSERT (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE);
4200 BFD_ASSERT (input_bfd->xvec->header_byteorder_big_p
4201 == output_bfd->xvec->header_byteorder_big_p);
4203 relocateable = finfo->info->relocateable;
4204 syms = obj_aout_external_syms (input_bfd);
4205 strings = obj_aout_external_strings (input_bfd);
4206 sym_hashes = obj_aout_sym_hashes (input_bfd);
4208 reloc_count = rel_size / RELOC_STD_SIZE;
4210 rel_end = rel + reloc_count;
4211 for (; rel < rel_end; rel++)
4223 bfd_reloc_status_type r;
4225 r_addr = GET_SWORD (input_bfd, rel->r_address);
4227 if (input_bfd->xvec->header_byteorder_big_p)
4229 r_index = ((rel->r_index[0] << 16)
4230 | (rel->r_index[1] << 8)
4232 r_extern = (0 != (rel->r_type[0] & RELOC_STD_BITS_EXTERN_BIG));
4233 r_pcrel = (0 != (rel->r_type[0] & RELOC_STD_BITS_PCREL_BIG));
4234 r_baserel = (0 != (rel->r_type[0] & RELOC_STD_BITS_BASEREL_BIG));
4235 r_jmptable= (0 != (rel->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG));
4236 r_relative= (0 != (rel->r_type[0] & RELOC_STD_BITS_RELATIVE_BIG));
4237 r_length = ((rel->r_type[0] & RELOC_STD_BITS_LENGTH_BIG)
4238 >> RELOC_STD_BITS_LENGTH_SH_BIG);
4242 r_index = ((rel->r_index[2] << 16)
4243 | (rel->r_index[1] << 8)
4245 r_extern = (0 != (rel->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE));
4246 r_pcrel = (0 != (rel->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
4247 r_baserel = (0 != (rel->r_type[0] & RELOC_STD_BITS_BASEREL_LITTLE));
4248 r_jmptable= (0 != (rel->r_type[0] & RELOC_STD_BITS_JMPTABLE_LITTLE));
4249 r_relative= (0 != (rel->r_type[0] & RELOC_STD_BITS_RELATIVE_LITTLE));
4250 r_length = ((rel->r_type[0] & RELOC_STD_BITS_LENGTH_LITTLE)
4251 >> RELOC_STD_BITS_LENGTH_SH_LITTLE);
4254 howto_idx = r_length + 4 * r_pcrel + 8 * r_baserel
4255 + 16 * r_jmptable + 32 * r_relative;
4256 BFD_ASSERT (howto_idx < TABLE_SIZE (howto_table_std));
4260 /* We are generating a relocateable output file, and must
4261 modify the reloc accordingly. */
4264 struct aout_link_hash_entry *h;
4266 /* If we know the symbol this relocation is against,
4267 convert it into a relocation against a section. This
4268 is what the native linker does. */
4269 h = sym_hashes[r_index];
4270 if (h != (struct aout_link_hash_entry *) NULL
4271 && h->root.type == bfd_link_hash_defined)
4273 asection *output_section;
4275 /* Change the r_extern value. */
4276 if (output_bfd->xvec->header_byteorder_big_p)
4277 rel->r_type[0] &=~ RELOC_STD_BITS_EXTERN_BIG;
4279 rel->r_type[0] &=~ RELOC_STD_BITS_EXTERN_LITTLE;
4281 /* Compute a new r_index. */
4282 output_section = h->root.u.def.section->output_section;
4283 if (output_section == obj_textsec (output_bfd))
4285 else if (output_section == obj_datasec (output_bfd))
4287 else if (output_section == obj_bsssec (output_bfd))
4292 /* Add the symbol value and the section VMA to the
4293 addend stored in the contents. */
4294 relocation = (h->root.u.def.value
4295 + output_section->vma
4296 + h->root.u.def.section->output_offset);
4300 /* We must change r_index according to the symbol
4302 r_index = symbol_map[r_index];
4308 name = strings + GET_WORD (input_bfd,
4309 syms[r_index].e_strx);
4310 if (! ((*finfo->info->callbacks->unattached_reloc)
4311 (finfo->info, name, input_bfd, input_section,
4320 /* Write out the new r_index value. */
4321 if (output_bfd->xvec->header_byteorder_big_p)
4323 rel->r_index[0] = r_index >> 16;
4324 rel->r_index[1] = r_index >> 8;
4325 rel->r_index[2] = r_index;
4329 rel->r_index[2] = r_index >> 16;
4330 rel->r_index[1] = r_index >> 8;
4331 rel->r_index[0] = r_index;
4338 /* This is a relocation against a section. We must
4339 adjust by the amount that the section moved. */
4340 section = aout_reloc_index_to_section (input_bfd, r_index);
4341 relocation = (section->output_section->vma
4342 + section->output_offset
4346 /* Change the address of the relocation. */
4347 PUT_WORD (output_bfd,
4348 r_addr + input_section->output_offset,
4351 /* Adjust a PC relative relocation by removing the reference
4352 to the original address in the section and including the
4353 reference to the new address. */
4355 relocation -= (input_section->output_section->vma
4356 + input_section->output_offset
4357 - input_section->vma);
4359 if (relocation == 0)
4362 r = _bfd_relocate_contents (howto_table_std + howto_idx,
4363 input_bfd, relocation,
4368 /* We are generating an executable, and must do a full
4372 struct aout_link_hash_entry *h;
4374 h = sym_hashes[r_index];
4376 if (check_dynamic_reloc != NULL)
4380 if (! ((*check_dynamic_reloc)
4381 (finfo->info, input_bfd, input_section, h,
4388 if (h != (struct aout_link_hash_entry *) NULL
4389 && h->root.type == bfd_link_hash_defined)
4391 relocation = (h->root.u.def.value
4392 + h->root.u.def.section->output_section->vma
4393 + h->root.u.def.section->output_offset);
4399 name = strings + GET_WORD (input_bfd, syms[r_index].e_strx);
4400 if (! ((*finfo->info->callbacks->undefined_symbol)
4401 (finfo->info, name, input_bfd, input_section,
4411 section = aout_reloc_index_to_section (input_bfd, r_index);
4412 relocation = (section->output_section->vma
4413 + section->output_offset
4416 relocation += input_section->vma;
4419 r = _bfd_final_link_relocate (howto_table_std + howto_idx,
4420 input_bfd, input_section,
4421 contents, r_addr, relocation,
4425 if (r != bfd_reloc_ok)
4430 case bfd_reloc_outofrange:
4432 case bfd_reloc_overflow:
4437 name = strings + GET_WORD (input_bfd,
4438 syms[r_index].e_strx);
4443 s = aout_reloc_index_to_section (input_bfd, r_index);
4444 name = bfd_section_name (input_bfd, s);
4446 if (! ((*finfo->info->callbacks->reloc_overflow)
4447 (finfo->info, name, howto_table_std[howto_idx].name,
4448 (bfd_vma) 0, input_bfd, input_section, r_addr)))
4459 /* Relocate an a.out section using extended a.out relocs. */
4462 aout_link_input_section_ext (finfo, input_bfd, input_section, relocs,
4463 rel_size, contents, symbol_map)
4464 struct aout_final_link_info *finfo;
4466 asection *input_section;
4467 struct reloc_ext_external *relocs;
4468 bfd_size_type rel_size;
4472 boolean (*check_dynamic_reloc) PARAMS ((struct bfd_link_info *,
4474 struct aout_link_hash_entry *,
4477 boolean relocateable;
4478 struct external_nlist *syms;
4480 struct aout_link_hash_entry **sym_hashes;
4481 bfd_size_type reloc_count;
4482 register struct reloc_ext_external *rel;
4483 struct reloc_ext_external *rel_end;
4485 output_bfd = finfo->output_bfd;
4486 check_dynamic_reloc = aout_backend_info (output_bfd)->check_dynamic_reloc;
4488 BFD_ASSERT (obj_reloc_entry_size (input_bfd) == RELOC_EXT_SIZE);
4489 BFD_ASSERT (input_bfd->xvec->header_byteorder_big_p
4490 == output_bfd->xvec->header_byteorder_big_p);
4492 relocateable = finfo->info->relocateable;
4493 syms = obj_aout_external_syms (input_bfd);
4494 strings = obj_aout_external_strings (input_bfd);
4495 sym_hashes = obj_aout_sym_hashes (input_bfd);
4497 reloc_count = rel_size / RELOC_EXT_SIZE;
4499 rel_end = rel + reloc_count;
4500 for (; rel < rel_end; rel++)
4509 r_addr = GET_SWORD (input_bfd, rel->r_address);
4511 if (input_bfd->xvec->header_byteorder_big_p)
4513 r_index = ((rel->r_index[0] << 16)
4514 | (rel->r_index[1] << 8)
4516 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG));
4517 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
4518 >> RELOC_EXT_BITS_TYPE_SH_BIG);
4522 r_index = ((rel->r_index[2] << 16)
4523 | (rel->r_index[1] << 8)
4525 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
4526 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
4527 >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
4530 r_addend = GET_SWORD (input_bfd, rel->r_addend);
4532 BFD_ASSERT (r_type >= 0
4533 && r_type < TABLE_SIZE (howto_table_ext));
4537 /* We are generating a relocateable output file, and must
4538 modify the reloc accordingly. */
4541 struct aout_link_hash_entry *h;
4543 /* If we know the symbol this relocation is against,
4544 convert it into a relocation against a section. This
4545 is what the native linker does. */
4546 h = sym_hashes[r_index];
4547 if (h != (struct aout_link_hash_entry *) NULL
4548 && h->root.type == bfd_link_hash_defined)
4550 asection *output_section;
4552 /* Change the r_extern value. */
4553 if (output_bfd->xvec->header_byteorder_big_p)
4554 rel->r_type[0] &=~ RELOC_EXT_BITS_EXTERN_BIG;
4556 rel->r_type[0] &=~ RELOC_EXT_BITS_EXTERN_LITTLE;
4558 /* Compute a new r_index. */
4559 output_section = h->root.u.def.section->output_section;
4560 if (output_section == obj_textsec (output_bfd))
4562 else if (output_section == obj_datasec (output_bfd))
4564 else if (output_section == obj_bsssec (output_bfd))
4569 /* Add the symbol value and the section VMA to the
4571 relocation = (h->root.u.def.value
4572 + output_section->vma
4573 + h->root.u.def.section->output_offset);
4575 /* Now RELOCATION is the VMA of the final
4576 destination. If this is a PC relative reloc,
4577 then ADDEND is the negative of the source VMA.
4578 We want to set ADDEND to the difference between
4579 the destination VMA and the source VMA, which
4580 means we must adjust RELOCATION by the change in
4581 the source VMA. This is done below. */
4585 /* We must change r_index according to the symbol
4587 r_index = symbol_map[r_index];
4594 + GET_WORD (input_bfd, syms[r_index].e_strx));
4595 if (! ((*finfo->info->callbacks->unattached_reloc)
4596 (finfo->info, name, input_bfd, input_section,
4604 /* If this is a PC relative reloc, then the addend
4605 is the negative of the source VMA. We must
4606 adjust it by the change in the source VMA. This
4610 /* Write out the new r_index value. */
4611 if (output_bfd->xvec->header_byteorder_big_p)
4613 rel->r_index[0] = r_index >> 16;
4614 rel->r_index[1] = r_index >> 8;
4615 rel->r_index[2] = r_index;
4619 rel->r_index[2] = r_index >> 16;
4620 rel->r_index[1] = r_index >> 8;
4621 rel->r_index[0] = r_index;
4628 /* This is a relocation against a section. We must
4629 adjust by the amount that the section moved. */
4630 section = aout_reloc_index_to_section (input_bfd, r_index);
4631 relocation = (section->output_section->vma
4632 + section->output_offset
4635 /* If this is a PC relative reloc, then the addend is
4636 the difference in VMA between the destination and the
4637 source. We have just adjusted for the change in VMA
4638 of the destination, so we must also adjust by the
4639 change in VMA of the source. This is done below. */
4642 /* As described above, we must always adjust a PC relative
4643 reloc by the change in VMA of the source. */
4644 if (howto_table_ext[r_type].pc_relative)
4645 relocation -= (input_section->output_section->vma
4646 + input_section->output_offset
4647 - input_section->vma);
4649 /* Change the addend if necessary. */
4650 if (relocation != 0)
4651 PUT_WORD (output_bfd, r_addend + relocation, rel->r_addend);
4653 /* Change the address of the relocation. */
4654 PUT_WORD (output_bfd,
4655 r_addr + input_section->output_offset,
4660 bfd_reloc_status_type r;
4662 /* We are generating an executable, and must do a full
4666 struct aout_link_hash_entry *h;
4668 h = sym_hashes[r_index];
4670 if (check_dynamic_reloc != NULL)
4674 if (! ((*check_dynamic_reloc)
4675 (finfo->info, input_bfd, input_section, h,
4682 if (h != (struct aout_link_hash_entry *) NULL
4683 && h->root.type == bfd_link_hash_defined)
4685 relocation = (h->root.u.def.value
4686 + h->root.u.def.section->output_section->vma
4687 + h->root.u.def.section->output_offset);
4693 name = strings + GET_WORD (input_bfd, syms[r_index].e_strx);
4694 if (! ((*finfo->info->callbacks->undefined_symbol)
4695 (finfo->info, name, input_bfd, input_section,
4705 section = aout_reloc_index_to_section (input_bfd, r_index);
4707 /* If this is a PC relative reloc, then R_ADDEND is the
4708 difference between the two vmas, or
4709 old_dest_sec + old_dest_off - (old_src_sec + old_src_off)
4711 old_dest_sec == section->vma
4713 old_src_sec == input_section->vma
4715 old_src_off == r_addr
4717 _bfd_final_link_relocate expects RELOCATION +
4718 R_ADDEND to be the VMA of the destination minus
4719 r_addr (the minus r_addr is because this relocation
4720 is not pcrel_offset, which is a bit confusing and
4721 should, perhaps, be changed), or
4724 new_dest_sec == output_section->vma + output_offset
4725 We arrange for this to happen by setting RELOCATION to
4726 new_dest_sec + old_src_sec - old_dest_sec
4728 If this is not a PC relative reloc, then R_ADDEND is
4729 simply the VMA of the destination, so we set
4730 RELOCATION to the change in the destination VMA, or
4731 new_dest_sec - old_dest_sec
4733 relocation = (section->output_section->vma
4734 + section->output_offset
4736 if (howto_table_ext[r_type].pc_relative)
4737 relocation += input_section->vma;
4740 r = _bfd_final_link_relocate (howto_table_ext + r_type,
4741 input_bfd, input_section,
4742 contents, r_addr, relocation,
4744 if (r != bfd_reloc_ok)
4749 case bfd_reloc_outofrange:
4751 case bfd_reloc_overflow:
4756 name = strings + GET_WORD (input_bfd,
4757 syms[r_index].e_strx);
4762 s = aout_reloc_index_to_section (input_bfd, r_index);
4763 name = bfd_section_name (input_bfd, s);
4765 if (! ((*finfo->info->callbacks->reloc_overflow)
4766 (finfo->info, name, howto_table_ext[r_type].name,
4767 r_addend, input_bfd, input_section, r_addr)))
4779 /* Handle a link order which is supposed to generate a reloc. */
4782 aout_link_reloc_link_order (finfo, o, p)
4783 struct aout_final_link_info *finfo;
4785 struct bfd_link_order *p;
4787 struct bfd_link_order_reloc *pr;
4790 const reloc_howto_type *howto;
4791 file_ptr *reloff_ptr;
4792 struct reloc_std_external srel;
4793 struct reloc_ext_external erel;
4798 if (p->type == bfd_section_reloc_link_order)
4801 if (pr->u.section == &bfd_abs_section)
4802 r_index = N_ABS | N_EXT;
4805 BFD_ASSERT (pr->u.section->owner == finfo->output_bfd);
4806 r_index = pr->u.section->target_index;
4811 struct aout_link_hash_entry *h;
4813 BFD_ASSERT (p->type == bfd_symbol_reloc_link_order);
4815 h = aout_link_hash_lookup (aout_hash_table (finfo->info),
4816 pr->u.name, false, false, true);
4817 if (h != (struct aout_link_hash_entry *) NULL
4822 if (! ((*finfo->info->callbacks->unattached_reloc)
4823 (finfo->info, pr->u.name, (bfd *) NULL,
4824 (asection *) NULL, (bfd_vma) 0)))
4830 howto = bfd_reloc_type_lookup (finfo->output_bfd, pr->reloc);
4831 if (howto == (const reloc_howto_type *) NULL)
4833 bfd_set_error (bfd_error_bad_value);
4837 if (o == obj_textsec (finfo->output_bfd))
4838 reloff_ptr = &finfo->treloff;
4839 else if (o == obj_datasec (finfo->output_bfd))
4840 reloff_ptr = &finfo->dreloff;
4844 if (obj_reloc_entry_size (finfo->output_bfd) == RELOC_STD_SIZE)
4852 r_pcrel = howto->pc_relative;
4853 r_baserel = (howto->type & 8) != 0;
4854 r_jmptable = (howto->type & 16) != 0;
4855 r_relative = (howto->type & 32) != 0;
4856 r_length = howto->size;
4858 PUT_WORD (finfo->output_bfd, p->offset, srel.r_address);
4859 if (finfo->output_bfd->xvec->header_byteorder_big_p)
4861 srel.r_index[0] = r_index >> 16;
4862 srel.r_index[1] = r_index >> 8;
4863 srel.r_index[2] = r_index;
4865 ((r_extern ? RELOC_STD_BITS_EXTERN_BIG : 0)
4866 | (r_pcrel ? RELOC_STD_BITS_PCREL_BIG : 0)
4867 | (r_baserel ? RELOC_STD_BITS_BASEREL_BIG : 0)
4868 | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_BIG : 0)
4869 | (r_relative ? RELOC_STD_BITS_RELATIVE_BIG : 0)
4870 | (r_length << RELOC_STD_BITS_LENGTH_SH_BIG));
4874 srel.r_index[2] = r_index >> 16;
4875 srel.r_index[1] = r_index >> 8;
4876 srel.r_index[0] = r_index;
4878 ((r_extern ? RELOC_STD_BITS_EXTERN_LITTLE : 0)
4879 | (r_pcrel ? RELOC_STD_BITS_PCREL_LITTLE : 0)
4880 | (r_baserel ? RELOC_STD_BITS_BASEREL_LITTLE : 0)
4881 | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_LITTLE : 0)
4882 | (r_relative ? RELOC_STD_BITS_RELATIVE_LITTLE : 0)
4883 | (r_length << RELOC_STD_BITS_LENGTH_SH_LITTLE));
4886 rel_ptr = (PTR) &srel;
4888 /* We have to write the addend into the object file, since
4889 standard a.out relocs are in place. It would be more
4890 reliable if we had the current contents of the file here,
4891 rather than assuming zeroes, but we can't read the file since
4892 it was opened using bfd_openw. */
4893 if (pr->addend != 0)
4896 bfd_reloc_status_type r;
4900 size = bfd_get_reloc_size (howto);
4901 buf = (bfd_byte *) bfd_zmalloc (size);
4902 if (buf == (bfd_byte *) NULL)
4904 bfd_set_error (bfd_error_no_memory);
4907 r = _bfd_relocate_contents (howto, finfo->output_bfd,
4914 case bfd_reloc_outofrange:
4916 case bfd_reloc_overflow:
4917 if (! ((*finfo->info->callbacks->reloc_overflow)
4919 (p->type == bfd_section_reloc_link_order
4920 ? bfd_section_name (finfo->output_bfd,
4923 howto->name, pr->addend, (bfd *) NULL,
4924 (asection *) NULL, (bfd_vma) 0)))
4931 ok = bfd_set_section_contents (finfo->output_bfd, o,
4933 (file_ptr) p->offset,
4942 PUT_WORD (finfo->output_bfd, p->offset, erel.r_address);
4944 if (finfo->output_bfd->xvec->header_byteorder_big_p)
4946 erel.r_index[0] = r_index >> 16;
4947 erel.r_index[1] = r_index >> 8;
4948 erel.r_index[2] = r_index;
4950 ((r_extern ? RELOC_EXT_BITS_EXTERN_BIG : 0)
4951 | (howto->type << RELOC_EXT_BITS_TYPE_SH_BIG));
4955 erel.r_index[2] = r_index >> 16;
4956 erel.r_index[1] = r_index >> 8;
4957 erel.r_index[0] = r_index;
4959 (r_extern ? RELOC_EXT_BITS_EXTERN_LITTLE : 0)
4960 | (howto->type << RELOC_EXT_BITS_TYPE_SH_LITTLE);
4963 PUT_WORD (finfo->output_bfd, pr->addend, erel.r_addend);
4965 rel_ptr = (PTR) &erel;
4968 if (bfd_seek (finfo->output_bfd, *reloff_ptr, SEEK_SET) != 0
4969 || (bfd_write (rel_ptr, (bfd_size_type) 1,
4970 obj_reloc_entry_size (finfo->output_bfd),
4972 != obj_reloc_entry_size (finfo->output_bfd)))
4975 *reloff_ptr += obj_reloc_entry_size (finfo->output_bfd);
4977 /* Assert that the relocs have not run into the symbols, and that n
4978 the text relocs have not run into the data relocs. */
4979 BFD_ASSERT (*reloff_ptr <= obj_sym_filepos (finfo->output_bfd)
4980 && (reloff_ptr != &finfo->treloff
4982 <= obj_datasec (finfo->output_bfd)->rel_filepos)));