* MAINTAINERS: Add myself as M88k maintainer.
[binutils.git] / bfd / aoutx.h
blob9bea98ea066f611097908b91f85786519d538ccb
1 /* BFD semi-generic back-end for a.out binaries.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005
4 Free Software Foundation, Inc.
5 Written by Cygnus Support.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 SECTION
25 a.out backends
27 DESCRIPTION
29 BFD supports a number of different flavours of a.out format,
30 though the major differences are only the sizes of the
31 structures on disk, and the shape of the relocation
32 information.
34 The support is split into a basic support file @file{aoutx.h}
35 and other files which derive functions from the base. One
36 derivation file is @file{aoutf1.h} (for a.out flavour 1), and
37 adds to the basic a.out functions support for sun3, sun4, 386
38 and 29k a.out files, to create a target jump vector for a
39 specific target.
41 This information is further split out into more specific files
42 for each machine, including @file{sunos.c} for sun3 and sun4,
43 @file{newsos3.c} for the Sony NEWS, and @file{demo64.c} for a
44 demonstration of a 64 bit a.out format.
46 The base file @file{aoutx.h} defines general mechanisms for
47 reading and writing records to and from disk and various
48 other methods which BFD requires. It is included by
49 @file{aout32.c} and @file{aout64.c} to form the names
50 <<aout_32_swap_exec_header_in>>, <<aout_64_swap_exec_header_in>>, etc.
52 As an example, this is what goes on to make the back end for a
53 sun4, from @file{aout32.c}:
55 | #define ARCH_SIZE 32
56 | #include "aoutx.h"
58 Which exports names:
60 | ...
61 | aout_32_canonicalize_reloc
62 | aout_32_find_nearest_line
63 | aout_32_get_lineno
64 | aout_32_get_reloc_upper_bound
65 | ...
67 from @file{sunos.c}:
69 | #define TARGET_NAME "a.out-sunos-big"
70 | #define VECNAME sunos_big_vec
71 | #include "aoutf1.h"
73 requires all the names from @file{aout32.c}, and produces the jump vector
75 | sunos_big_vec
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
85 the object file.
87 When porting it to run on a new system, you must supply:
89 | HOST_PAGE_SIZE
90 | HOST_SEGMENT_SIZE
91 | HOST_MACHINE_ARCH (optional)
92 | HOST_MACHINE_MACHINE (optional)
93 | HOST_TEXT_START_ADDR
94 | HOST_STACK_END_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}
106 to use the
107 @file{@var{XXX}.mt} file (by setting "<<bfd_target=XXX>>") when your
108 configuration is selected. */
110 /* Some assumptions:
111 * Any BFD with D_PAGED set is ZMAGIC, and vice versa.
112 Doesn't matter what the setting of WP_TEXT is on output, but it'll
113 get set on input.
114 * Any BFD with D_PAGED clear and WP_TEXT set is NMAGIC.
115 * Any BFD with both flags clear is OMAGIC.
116 (Just want to make these explicit, so the conditions tested in this
117 file make sense if you're more familiar with a.out than with BFD.) */
119 #define KEEPIT udata.i
121 #include "bfd.h"
122 #include "sysdep.h"
123 #include "safe-ctype.h"
124 #include "bfdlink.h"
126 #include "libaout.h"
127 #include "libbfd.h"
128 #include "aout/aout64.h"
129 #include "aout/stab_gnu.h"
130 #include "aout/ar.h"
132 static bfd_boolean aout_get_external_symbols
133 PARAMS ((bfd *));
134 static bfd_boolean translate_from_native_sym_flags
135 PARAMS ((bfd *, aout_symbol_type *));
136 static bfd_boolean translate_to_native_sym_flags
137 PARAMS ((bfd *, asymbol *, struct external_nlist *));
138 static void adjust_o_magic
139 PARAMS ((bfd *, struct internal_exec *));
140 static void adjust_z_magic
141 PARAMS ((bfd *, struct internal_exec *));
142 static void adjust_n_magic
143 PARAMS ((bfd *, struct internal_exec *));
144 reloc_howto_type * NAME(aout,reloc_type_lookup)
145 PARAMS ((bfd *, bfd_reloc_code_real_type));
148 SUBSECTION
149 Relocations
151 DESCRIPTION
152 The file @file{aoutx.h} provides for both the @emph{standard}
153 and @emph{extended} forms of a.out relocation records.
155 The standard records contain only an
156 address, a symbol index, and a type field. The extended records
157 (used on 29ks and sparcs) also have a full integer for an
158 addend. */
160 #ifndef CTOR_TABLE_RELOC_HOWTO
161 #define CTOR_TABLE_RELOC_IDX 2
162 #define CTOR_TABLE_RELOC_HOWTO(BFD) \
163 ((obj_reloc_entry_size (BFD) == RELOC_EXT_SIZE \
164 ? howto_table_ext : howto_table_std) \
165 + CTOR_TABLE_RELOC_IDX)
166 #endif
168 #ifndef MY_swap_std_reloc_in
169 #define MY_swap_std_reloc_in NAME(aout,swap_std_reloc_in)
170 #endif
172 #ifndef MY_swap_ext_reloc_in
173 #define MY_swap_ext_reloc_in NAME(aout,swap_ext_reloc_in)
174 #endif
176 #ifndef MY_swap_std_reloc_out
177 #define MY_swap_std_reloc_out NAME(aout,swap_std_reloc_out)
178 #endif
180 #ifndef MY_swap_ext_reloc_out
181 #define MY_swap_ext_reloc_out NAME(aout,swap_ext_reloc_out)
182 #endif
184 #ifndef MY_final_link_relocate
185 #define MY_final_link_relocate _bfd_final_link_relocate
186 #endif
188 #ifndef MY_relocate_contents
189 #define MY_relocate_contents _bfd_relocate_contents
190 #endif
192 #define howto_table_ext NAME(aout,ext_howto_table)
193 #define howto_table_std NAME(aout,std_howto_table)
195 reloc_howto_type howto_table_ext[] =
197 /* type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone. */
198 HOWTO(RELOC_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,0,"8", FALSE, 0,0x000000ff, FALSE),
199 HOWTO(RELOC_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,0,"16", FALSE, 0,0x0000ffff, FALSE),
200 HOWTO(RELOC_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,0,"32", FALSE, 0,0xffffffff, FALSE),
201 HOWTO(RELOC_DISP8, 0, 0, 8, TRUE, 0, complain_overflow_signed,0,"DISP8", FALSE, 0,0x000000ff, FALSE),
202 HOWTO(RELOC_DISP16, 0, 1, 16, TRUE, 0, complain_overflow_signed,0,"DISP16", FALSE, 0,0x0000ffff, FALSE),
203 HOWTO(RELOC_DISP32, 0, 2, 32, TRUE, 0, complain_overflow_signed,0,"DISP32", FALSE, 0,0xffffffff, FALSE),
204 HOWTO(RELOC_WDISP30,2, 2, 30, TRUE, 0, complain_overflow_signed,0,"WDISP30", FALSE, 0,0x3fffffff, FALSE),
205 HOWTO(RELOC_WDISP22,2, 2, 22, TRUE, 0, complain_overflow_signed,0,"WDISP22", FALSE, 0,0x003fffff, FALSE),
206 HOWTO(RELOC_HI22, 10, 2, 22, FALSE, 0, complain_overflow_bitfield,0,"HI22", FALSE, 0,0x003fffff, FALSE),
207 HOWTO(RELOC_22, 0, 2, 22, FALSE, 0, complain_overflow_bitfield,0,"22", FALSE, 0,0x003fffff, FALSE),
208 HOWTO(RELOC_13, 0, 2, 13, FALSE, 0, complain_overflow_bitfield,0,"13", FALSE, 0,0x00001fff, FALSE),
209 HOWTO(RELOC_LO10, 0, 2, 10, FALSE, 0, complain_overflow_dont,0,"LO10", FALSE, 0,0x000003ff, FALSE),
210 HOWTO(RELOC_SFA_BASE,0, 2, 32, FALSE, 0, complain_overflow_bitfield,0,"SFA_BASE", FALSE, 0,0xffffffff, FALSE),
211 HOWTO(RELOC_SFA_OFF13,0,2, 32, FALSE, 0, complain_overflow_bitfield,0,"SFA_OFF13",FALSE, 0,0xffffffff, FALSE),
212 HOWTO(RELOC_BASE10, 0, 2, 10, FALSE, 0, complain_overflow_dont,0,"BASE10", FALSE, 0,0x000003ff, FALSE),
213 HOWTO(RELOC_BASE13, 0, 2, 13, FALSE, 0, complain_overflow_signed,0,"BASE13", FALSE, 0,0x00001fff, FALSE),
214 HOWTO(RELOC_BASE22, 10, 2, 22, FALSE, 0, complain_overflow_bitfield,0,"BASE22", FALSE, 0,0x003fffff, FALSE),
215 HOWTO(RELOC_PC10, 0, 2, 10, TRUE, 0, complain_overflow_dont,0,"PC10", FALSE, 0,0x000003ff, TRUE),
216 HOWTO(RELOC_PC22, 10, 2, 22, TRUE, 0, complain_overflow_signed,0,"PC22", FALSE, 0,0x003fffff, TRUE),
217 HOWTO(RELOC_JMP_TBL,2, 2, 30, TRUE, 0, complain_overflow_signed,0,"JMP_TBL", FALSE, 0,0x3fffffff, FALSE),
218 HOWTO(RELOC_SEGOFF16,0, 2, 0, FALSE, 0, complain_overflow_bitfield,0,"SEGOFF16", FALSE, 0,0x00000000, FALSE),
219 HOWTO(RELOC_GLOB_DAT,0, 2, 0, FALSE, 0, complain_overflow_bitfield,0,"GLOB_DAT", FALSE, 0,0x00000000, FALSE),
220 HOWTO(RELOC_JMP_SLOT,0, 2, 0, FALSE, 0, complain_overflow_bitfield,0,"JMP_SLOT", FALSE, 0,0x00000000, FALSE),
221 HOWTO(RELOC_RELATIVE,0, 2, 0, FALSE, 0, complain_overflow_bitfield,0,"RELATIVE", FALSE, 0,0x00000000, FALSE),
222 HOWTO(0, 0, 0, 0, FALSE, 0, complain_overflow_dont, 0, "R_SPARC_NONE", FALSE,0,0x00000000,TRUE),
223 HOWTO(0, 0, 0, 0, FALSE, 0, complain_overflow_dont, 0, "R_SPARC_NONE", FALSE,0,0x00000000,TRUE),
224 #define RELOC_SPARC_REV32 RELOC_WDISP19
225 HOWTO(RELOC_SPARC_REV32, 0, 2, 32, FALSE, 0, complain_overflow_dont,0,"R_SPARC_REV32", FALSE, 0,0xffffffff, FALSE),
228 /* Convert standard reloc records to "arelent" format (incl byte swap). */
230 reloc_howto_type howto_table_std[] =
232 /* type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone. */
233 HOWTO ( 0, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,0,"8", TRUE, 0x000000ff,0x000000ff, FALSE),
234 HOWTO ( 1, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,0,"16", TRUE, 0x0000ffff,0x0000ffff, FALSE),
235 HOWTO ( 2, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,0,"32", TRUE, 0xffffffff,0xffffffff, FALSE),
236 HOWTO ( 3, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,0,"64", TRUE, 0xdeaddead,0xdeaddead, FALSE),
237 HOWTO ( 4, 0, 0, 8, TRUE, 0, complain_overflow_signed, 0,"DISP8", TRUE, 0x000000ff,0x000000ff, FALSE),
238 HOWTO ( 5, 0, 1, 16, TRUE, 0, complain_overflow_signed, 0,"DISP16", TRUE, 0x0000ffff,0x0000ffff, FALSE),
239 HOWTO ( 6, 0, 2, 32, TRUE, 0, complain_overflow_signed, 0,"DISP32", TRUE, 0xffffffff,0xffffffff, FALSE),
240 HOWTO ( 7, 0, 4, 64, TRUE, 0, complain_overflow_signed, 0,"DISP64", TRUE, 0xfeedface,0xfeedface, FALSE),
241 HOWTO ( 8, 0, 2, 0, FALSE, 0, complain_overflow_bitfield,0,"GOT_REL", FALSE, 0,0x00000000, FALSE),
242 HOWTO ( 9, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,0,"BASE16", FALSE,0xffffffff,0xffffffff, FALSE),
243 HOWTO (10, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,0,"BASE32", FALSE,0xffffffff,0xffffffff, FALSE),
244 EMPTY_HOWTO (-1),
245 EMPTY_HOWTO (-1),
246 EMPTY_HOWTO (-1),
247 EMPTY_HOWTO (-1),
248 EMPTY_HOWTO (-1),
249 HOWTO (16, 0, 2, 0, FALSE, 0, complain_overflow_bitfield,0,"JMP_TABLE", FALSE, 0,0x00000000, FALSE),
250 EMPTY_HOWTO (-1),
251 EMPTY_HOWTO (-1),
252 EMPTY_HOWTO (-1),
253 EMPTY_HOWTO (-1),
254 EMPTY_HOWTO (-1),
255 EMPTY_HOWTO (-1),
256 EMPTY_HOWTO (-1),
257 EMPTY_HOWTO (-1),
258 EMPTY_HOWTO (-1),
259 EMPTY_HOWTO (-1),
260 EMPTY_HOWTO (-1),
261 EMPTY_HOWTO (-1),
262 EMPTY_HOWTO (-1),
263 EMPTY_HOWTO (-1),
264 EMPTY_HOWTO (-1),
265 HOWTO (32, 0, 2, 0, FALSE, 0, complain_overflow_bitfield,0,"RELATIVE", FALSE, 0,0x00000000, FALSE),
266 EMPTY_HOWTO (-1),
267 EMPTY_HOWTO (-1),
268 EMPTY_HOWTO (-1),
269 EMPTY_HOWTO (-1),
270 EMPTY_HOWTO (-1),
271 EMPTY_HOWTO (-1),
272 EMPTY_HOWTO (-1),
273 HOWTO (40, 0, 2, 0, FALSE, 0, complain_overflow_bitfield,0,"BASEREL", FALSE, 0,0x00000000, FALSE),
276 #define TABLE_SIZE(TABLE) (sizeof (TABLE) / sizeof (TABLE[0]))
278 reloc_howto_type *
279 NAME(aout,reloc_type_lookup) (abfd,code)
280 bfd *abfd;
281 bfd_reloc_code_real_type code;
283 #define EXT(i, j) case i: return &howto_table_ext[j]
284 #define STD(i, j) case i: return &howto_table_std[j]
285 int ext = obj_reloc_entry_size (abfd) == RELOC_EXT_SIZE;
287 if (code == BFD_RELOC_CTOR)
288 switch (bfd_get_arch_info (abfd)->bits_per_address)
290 case 32:
291 code = BFD_RELOC_32;
292 break;
293 case 64:
294 code = BFD_RELOC_64;
295 break;
298 if (ext)
299 switch (code)
301 EXT (BFD_RELOC_8, 0);
302 EXT (BFD_RELOC_16, 1);
303 EXT (BFD_RELOC_32, 2);
304 EXT (BFD_RELOC_HI22, 8);
305 EXT (BFD_RELOC_LO10, 11);
306 EXT (BFD_RELOC_32_PCREL_S2, 6);
307 EXT (BFD_RELOC_SPARC_WDISP22, 7);
308 EXT (BFD_RELOC_SPARC13, 10);
309 EXT (BFD_RELOC_SPARC_GOT10, 14);
310 EXT (BFD_RELOC_SPARC_BASE13, 15);
311 EXT (BFD_RELOC_SPARC_GOT13, 15);
312 EXT (BFD_RELOC_SPARC_GOT22, 16);
313 EXT (BFD_RELOC_SPARC_PC10, 17);
314 EXT (BFD_RELOC_SPARC_PC22, 18);
315 EXT (BFD_RELOC_SPARC_WPLT30, 19);
316 EXT (BFD_RELOC_SPARC_REV32, 26);
317 default: return (reloc_howto_type *) NULL;
319 else
320 /* std relocs. */
321 switch (code)
323 STD (BFD_RELOC_8, 0);
324 STD (BFD_RELOC_16, 1);
325 STD (BFD_RELOC_32, 2);
326 STD (BFD_RELOC_8_PCREL, 4);
327 STD (BFD_RELOC_16_PCREL, 5);
328 STD (BFD_RELOC_32_PCREL, 6);
329 STD (BFD_RELOC_16_BASEREL, 9);
330 STD (BFD_RELOC_32_BASEREL, 10);
331 default: return (reloc_howto_type *) NULL;
336 SUBSECTION
337 Internal entry points
339 DESCRIPTION
340 @file{aoutx.h} exports several routines for accessing the
341 contents of an a.out file, which are gathered and exported in
342 turn by various format specific files (eg sunos.c).
347 FUNCTION
348 aout_@var{size}_swap_exec_header_in
350 SYNOPSIS
351 void aout_@var{size}_swap_exec_header_in,
352 (bfd *abfd,
353 struct external_exec *raw_bytes,
354 struct internal_exec *execp);
356 DESCRIPTION
357 Swap the information in an executable header @var{raw_bytes} taken
358 from a raw byte stream memory image into the internal exec header
359 structure @var{execp}.
362 #ifndef NAME_swap_exec_header_in
363 void
364 NAME(aout,swap_exec_header_in) (abfd, raw_bytes, execp)
365 bfd *abfd;
366 struct external_exec *raw_bytes;
367 struct internal_exec *execp;
369 struct external_exec *bytes = (struct external_exec *)raw_bytes;
371 /* The internal_exec structure has some fields that are unused in this
372 configuration (IE for i960), so ensure that all such uninitialized
373 fields are zero'd out. There are places where two of these structs
374 are memcmp'd, and thus the contents do matter. */
375 memset ((PTR) execp, 0, sizeof (struct internal_exec));
376 /* Now fill in fields in the execp, from the bytes in the raw data. */
377 execp->a_info = H_GET_32 (abfd, bytes->e_info);
378 execp->a_text = GET_WORD (abfd, bytes->e_text);
379 execp->a_data = GET_WORD (abfd, bytes->e_data);
380 execp->a_bss = GET_WORD (abfd, bytes->e_bss);
381 execp->a_syms = GET_WORD (abfd, bytes->e_syms);
382 execp->a_entry = GET_WORD (abfd, bytes->e_entry);
383 execp->a_trsize = GET_WORD (abfd, bytes->e_trsize);
384 execp->a_drsize = GET_WORD (abfd, bytes->e_drsize);
386 #define NAME_swap_exec_header_in NAME(aout,swap_exec_header_in)
387 #endif
390 FUNCTION
391 aout_@var{size}_swap_exec_header_out
393 SYNOPSIS
394 void aout_@var{size}_swap_exec_header_out
395 (bfd *abfd,
396 struct internal_exec *execp,
397 struct external_exec *raw_bytes);
399 DESCRIPTION
400 Swap the information in an internal exec header structure
401 @var{execp} into the buffer @var{raw_bytes} ready for writing to disk.
403 void
404 NAME(aout,swap_exec_header_out) (abfd, execp, raw_bytes)
405 bfd *abfd;
406 struct internal_exec *execp;
407 struct external_exec *raw_bytes;
409 struct external_exec *bytes = (struct external_exec *)raw_bytes;
411 /* Now fill in fields in the raw data, from the fields in the exec struct. */
412 H_PUT_32 (abfd, execp->a_info , bytes->e_info);
413 PUT_WORD (abfd, execp->a_text , bytes->e_text);
414 PUT_WORD (abfd, execp->a_data , bytes->e_data);
415 PUT_WORD (abfd, execp->a_bss , bytes->e_bss);
416 PUT_WORD (abfd, execp->a_syms , bytes->e_syms);
417 PUT_WORD (abfd, execp->a_entry , bytes->e_entry);
418 PUT_WORD (abfd, execp->a_trsize, bytes->e_trsize);
419 PUT_WORD (abfd, execp->a_drsize, bytes->e_drsize);
422 /* Make all the section for an a.out file. */
424 bfd_boolean
425 NAME(aout,make_sections) (abfd)
426 bfd *abfd;
428 if (obj_textsec (abfd) == (asection *) NULL
429 && bfd_make_section (abfd, ".text") == (asection *) NULL)
430 return FALSE;
431 if (obj_datasec (abfd) == (asection *) NULL
432 && bfd_make_section (abfd, ".data") == (asection *) NULL)
433 return FALSE;
434 if (obj_bsssec (abfd) == (asection *) NULL
435 && bfd_make_section (abfd, ".bss") == (asection *) NULL)
436 return FALSE;
437 return TRUE;
441 FUNCTION
442 aout_@var{size}_some_aout_object_p
444 SYNOPSIS
445 const bfd_target *aout_@var{size}_some_aout_object_p
446 (bfd *abfd,
447 const bfd_target *(*callback_to_real_object_p) ());
449 DESCRIPTION
450 Some a.out variant thinks that the file open in @var{abfd}
451 checking is an a.out file. Do some more checking, and set up
452 for access if it really is. Call back to the calling
453 environment's "finish up" function just before returning, to
454 handle any last-minute setup.
457 const bfd_target *
458 NAME(aout,some_aout_object_p) (abfd, execp, callback_to_real_object_p)
459 bfd *abfd;
460 struct internal_exec *execp;
461 const bfd_target *(*callback_to_real_object_p) PARAMS ((bfd *));
463 struct aout_data_struct *rawptr, *oldrawptr;
464 const bfd_target *result;
465 bfd_size_type amt = sizeof (struct aout_data_struct);
467 rawptr = (struct aout_data_struct *) bfd_zalloc (abfd, amt);
468 if (rawptr == NULL)
469 return 0;
471 oldrawptr = abfd->tdata.aout_data;
472 abfd->tdata.aout_data = rawptr;
474 /* Copy the contents of the old tdata struct.
475 In particular, we want the subformat, since for hpux it was set in
476 hp300hpux.c:swap_exec_header_in and will be used in
477 hp300hpux.c:callback. */
478 if (oldrawptr != NULL)
479 *abfd->tdata.aout_data = *oldrawptr;
481 abfd->tdata.aout_data->a.hdr = &rawptr->e;
482 /* Copy in the internal_exec struct. */
483 *(abfd->tdata.aout_data->a.hdr) = *execp;
484 execp = abfd->tdata.aout_data->a.hdr;
486 /* Set the file flags. */
487 abfd->flags = BFD_NO_FLAGS;
488 if (execp->a_drsize || execp->a_trsize)
489 abfd->flags |= HAS_RELOC;
490 /* Setting of EXEC_P has been deferred to the bottom of this function. */
491 if (execp->a_syms)
492 abfd->flags |= HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS;
493 if (N_DYNAMIC (*execp))
494 abfd->flags |= DYNAMIC;
496 if (N_MAGIC (*execp) == ZMAGIC)
498 abfd->flags |= D_PAGED | WP_TEXT;
499 adata (abfd).magic = z_magic;
501 else if (N_MAGIC (*execp) == QMAGIC)
503 abfd->flags |= D_PAGED | WP_TEXT;
504 adata (abfd).magic = z_magic;
505 adata (abfd).subformat = q_magic_format;
507 else if (N_MAGIC (*execp) == NMAGIC)
509 abfd->flags |= WP_TEXT;
510 adata (abfd).magic = n_magic;
512 else if (N_MAGIC (*execp) == OMAGIC
513 || N_MAGIC (*execp) == BMAGIC)
514 adata (abfd).magic = o_magic;
515 else
517 /* Should have been checked with N_BADMAG before this routine
518 was called. */
519 abort ();
522 bfd_get_start_address (abfd) = execp->a_entry;
524 obj_aout_symbols (abfd) = (aout_symbol_type *)NULL;
525 bfd_get_symcount (abfd) = execp->a_syms / sizeof (struct external_nlist);
527 /* The default relocation entry size is that of traditional V7 Unix. */
528 obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
530 /* The default symbol entry size is that of traditional Unix. */
531 obj_symbol_entry_size (abfd) = EXTERNAL_NLIST_SIZE;
533 #ifdef USE_MMAP
534 bfd_init_window (&obj_aout_sym_window (abfd));
535 bfd_init_window (&obj_aout_string_window (abfd));
536 #endif
537 obj_aout_external_syms (abfd) = NULL;
538 obj_aout_external_strings (abfd) = NULL;
539 obj_aout_sym_hashes (abfd) = NULL;
541 if (! NAME(aout,make_sections) (abfd))
542 goto error_ret;
544 obj_datasec (abfd)->size = execp->a_data;
545 obj_bsssec (abfd)->size = execp->a_bss;
547 obj_textsec (abfd)->flags =
548 (execp->a_trsize != 0
549 ? (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_RELOC)
550 : (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS));
551 obj_datasec (abfd)->flags =
552 (execp->a_drsize != 0
553 ? (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS | SEC_RELOC)
554 : (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS));
555 obj_bsssec (abfd)->flags = SEC_ALLOC;
557 #ifdef THIS_IS_ONLY_DOCUMENTATION
558 /* The common code can't fill in these things because they depend
559 on either the start address of the text segment, the rounding
560 up of virtual addresses between segments, or the starting file
561 position of the text segment -- all of which varies among different
562 versions of a.out. */
564 /* Call back to the format-dependent code to fill in the rest of the
565 fields and do any further cleanup. Things that should be filled
566 in by the callback: */
568 struct exec *execp = exec_hdr (abfd);
570 obj_textsec (abfd)->size = N_TXTSIZE (*execp);
571 /* Data and bss are already filled in since they're so standard. */
573 /* The virtual memory addresses of the sections. */
574 obj_textsec (abfd)->vma = N_TXTADDR (*execp);
575 obj_datasec (abfd)->vma = N_DATADDR (*execp);
576 obj_bsssec (abfd)->vma = N_BSSADDR (*execp);
578 /* The file offsets of the sections. */
579 obj_textsec (abfd)->filepos = N_TXTOFF (*execp);
580 obj_datasec (abfd)->filepos = N_DATOFF (*execp);
582 /* The file offsets of the relocation info. */
583 obj_textsec (abfd)->rel_filepos = N_TRELOFF (*execp);
584 obj_datasec (abfd)->rel_filepos = N_DRELOFF (*execp);
586 /* The file offsets of the string table and symbol table. */
587 obj_str_filepos (abfd) = N_STROFF (*execp);
588 obj_sym_filepos (abfd) = N_SYMOFF (*execp);
590 /* Determine the architecture and machine type of the object file. */
591 switch (N_MACHTYPE (*exec_hdr (abfd)))
593 default:
594 abfd->obj_arch = bfd_arch_obscure;
595 break;
598 adata (abfd)->page_size = TARGET_PAGE_SIZE;
599 adata (abfd)->segment_size = SEGMENT_SIZE;
600 adata (abfd)->exec_bytes_size = EXEC_BYTES_SIZE;
602 return abfd->xvec;
604 /* The architecture is encoded in various ways in various a.out variants,
605 or is not encoded at all in some of them. The relocation size depends
606 on the architecture and the a.out variant. Finally, the return value
607 is the bfd_target vector in use. If an error occurs, return zero and
608 set bfd_error to the appropriate error code.
610 Formats such as b.out, which have additional fields in the a.out
611 header, should cope with them in this callback as well. */
612 #endif /* DOCUMENTATION */
614 result = (*callback_to_real_object_p) (abfd);
616 /* Now that the segment addresses have been worked out, take a better
617 guess at whether the file is executable. If the entry point
618 is within the text segment, assume it is. (This makes files
619 executable even if their entry point address is 0, as long as
620 their text starts at zero.).
622 This test had to be changed to deal with systems where the text segment
623 runs at a different location than the default. The problem is that the
624 entry address can appear to be outside the text segment, thus causing an
625 erroneous conclusion that the file isn't executable.
627 To fix this, we now accept any non-zero entry point as an indication of
628 executability. This will work most of the time, since only the linker
629 sets the entry point, and that is likely to be non-zero for most systems. */
631 if (execp->a_entry != 0
632 || (execp->a_entry >= obj_textsec (abfd)->vma
633 && execp->a_entry < (obj_textsec (abfd)->vma
634 + obj_textsec (abfd)->size)))
635 abfd->flags |= EXEC_P;
636 #ifdef STAT_FOR_EXEC
637 else
639 struct stat stat_buf;
641 /* The original heuristic doesn't work in some important cases.
642 The a.out file has no information about the text start
643 address. For files (like kernels) linked to non-standard
644 addresses (ld -Ttext nnn) the entry point may not be between
645 the default text start (obj_textsec(abfd)->vma) and
646 (obj_textsec(abfd)->vma) + text size. This is not just a mach
647 issue. Many kernels are loaded at non standard addresses. */
648 if (abfd->iostream != NULL
649 && (abfd->flags & BFD_IN_MEMORY) == 0
650 && (fstat (fileno ((FILE *) (abfd->iostream)), &stat_buf) == 0)
651 && ((stat_buf.st_mode & 0111) != 0))
652 abfd->flags |= EXEC_P;
654 #endif /* STAT_FOR_EXEC */
656 if (result)
657 return result;
659 error_ret:
660 bfd_release (abfd, rawptr);
661 abfd->tdata.aout_data = oldrawptr;
662 return NULL;
666 FUNCTION
667 aout_@var{size}_mkobject
669 SYNOPSIS
670 bfd_boolean aout_@var{size}_mkobject, (bfd *abfd);
672 DESCRIPTION
673 Initialize BFD @var{abfd} for use with a.out files.
676 bfd_boolean
677 NAME(aout,mkobject) (abfd)
678 bfd *abfd;
680 struct aout_data_struct *rawptr;
681 bfd_size_type amt = sizeof (struct aout_data_struct);
683 bfd_set_error (bfd_error_system_call);
685 rawptr = (struct aout_data_struct *) bfd_zalloc (abfd, amt);
686 if (rawptr == NULL)
687 return FALSE;
689 abfd->tdata.aout_data = rawptr;
690 exec_hdr (abfd) = &(rawptr->e);
692 obj_textsec (abfd) = (asection *) NULL;
693 obj_datasec (abfd) = (asection *) NULL;
694 obj_bsssec (abfd) = (asection *) NULL;
696 return TRUE;
700 FUNCTION
701 aout_@var{size}_machine_type
703 SYNOPSIS
704 enum machine_type aout_@var{size}_machine_type
705 (enum bfd_architecture arch,
706 unsigned long machine));
708 DESCRIPTION
709 Keep track of machine architecture and machine type for
710 a.out's. Return the <<machine_type>> for a particular
711 architecture and machine, or <<M_UNKNOWN>> if that exact architecture
712 and machine can't be represented in a.out format.
714 If the architecture is understood, machine type 0 (default)
715 is always understood.
718 enum machine_type
719 NAME(aout,machine_type) (arch, machine, unknown)
720 enum bfd_architecture arch;
721 unsigned long machine;
722 bfd_boolean *unknown;
724 enum machine_type arch_flags;
726 arch_flags = M_UNKNOWN;
727 *unknown = TRUE;
729 switch (arch)
731 case bfd_arch_sparc:
732 if (machine == 0
733 || machine == bfd_mach_sparc
734 || machine == bfd_mach_sparc_sparclite
735 || machine == bfd_mach_sparc_sparclite_le
736 || machine == bfd_mach_sparc_v9)
737 arch_flags = M_SPARC;
738 else if (machine == bfd_mach_sparc_sparclet)
739 arch_flags = M_SPARCLET;
740 break;
742 case bfd_arch_m68k:
743 switch (machine)
745 case 0: arch_flags = M_68010; break;
746 case bfd_mach_m68000: arch_flags = M_UNKNOWN; *unknown = FALSE; break;
747 case bfd_mach_m68010: arch_flags = M_68010; break;
748 case bfd_mach_m68020: arch_flags = M_68020; break;
749 default: arch_flags = M_UNKNOWN; break;
751 break;
753 case bfd_arch_i386:
754 if (machine == 0
755 || machine == bfd_mach_i386_i386
756 || machine == bfd_mach_i386_i386_intel_syntax)
757 arch_flags = M_386;
758 break;
760 case bfd_arch_a29k:
761 if (machine == 0)
762 arch_flags = M_29K;
763 break;
765 case bfd_arch_arm:
766 if (machine == 0)
767 arch_flags = M_ARM;
768 break;
770 case bfd_arch_mips:
771 switch (machine)
773 case 0:
774 case bfd_mach_mips3000:
775 case bfd_mach_mips3900:
776 arch_flags = M_MIPS1;
777 break;
778 case bfd_mach_mips6000:
779 arch_flags = M_MIPS2;
780 break;
781 case bfd_mach_mips4000:
782 case bfd_mach_mips4010:
783 case bfd_mach_mips4100:
784 case bfd_mach_mips4300:
785 case bfd_mach_mips4400:
786 case bfd_mach_mips4600:
787 case bfd_mach_mips4650:
788 case bfd_mach_mips8000:
789 case bfd_mach_mips9000:
790 case bfd_mach_mips10000:
791 case bfd_mach_mips12000:
792 case bfd_mach_mips16:
793 case bfd_mach_mipsisa32:
794 case bfd_mach_mipsisa32r2:
795 case bfd_mach_mips5:
796 case bfd_mach_mipsisa64:
797 case bfd_mach_mipsisa64r2:
798 case bfd_mach_mips_sb1:
799 /* FIXME: These should be MIPS3, MIPS4, MIPS16, MIPS32, etc. */
800 arch_flags = M_MIPS2;
801 break;
802 default:
803 arch_flags = M_UNKNOWN;
804 break;
806 break;
808 case bfd_arch_ns32k:
809 switch (machine)
811 case 0: arch_flags = M_NS32532; break;
812 case 32032: arch_flags = M_NS32032; break;
813 case 32532: arch_flags = M_NS32532; break;
814 default: arch_flags = M_UNKNOWN; break;
816 break;
818 case bfd_arch_vax:
819 *unknown = FALSE;
820 break;
822 case bfd_arch_cris:
823 if (machine == 0 || machine == 255)
824 arch_flags = M_CRIS;
825 break;
827 case bfd_arch_m88k:
828 *unknown = FALSE;
829 break;
831 default:
832 arch_flags = M_UNKNOWN;
835 if (arch_flags != M_UNKNOWN)
836 *unknown = FALSE;
838 return arch_flags;
842 FUNCTION
843 aout_@var{size}_set_arch_mach
845 SYNOPSIS
846 bfd_boolean aout_@var{size}_set_arch_mach,
847 (bfd *,
848 enum bfd_architecture arch,
849 unsigned long machine));
851 DESCRIPTION
852 Set the architecture and the machine of the BFD @var{abfd} to the
853 values @var{arch} and @var{machine}. Verify that @var{abfd}'s format
854 can support the architecture required.
857 bfd_boolean
858 NAME(aout,set_arch_mach) (abfd, arch, machine)
859 bfd *abfd;
860 enum bfd_architecture arch;
861 unsigned long machine;
863 if (! bfd_default_set_arch_mach (abfd, arch, machine))
864 return FALSE;
866 if (arch != bfd_arch_unknown)
868 bfd_boolean unknown;
870 NAME(aout,machine_type) (arch, machine, &unknown);
871 if (unknown)
872 return FALSE;
875 /* Determine the size of a relocation entry. */
876 switch (arch)
878 case bfd_arch_sparc:
879 case bfd_arch_a29k:
880 case bfd_arch_mips:
881 obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE;
882 break;
883 default:
884 obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
885 break;
888 return (*aout_backend_info (abfd)->set_sizes) (abfd);
891 static void
892 adjust_o_magic (abfd, execp)
893 bfd *abfd;
894 struct internal_exec *execp;
896 file_ptr pos = adata (abfd).exec_bytes_size;
897 bfd_vma vma = 0;
898 int pad = 0;
900 /* Text. */
901 obj_textsec (abfd)->filepos = pos;
902 if (!obj_textsec (abfd)->user_set_vma)
903 obj_textsec (abfd)->vma = vma;
904 else
905 vma = obj_textsec (abfd)->vma;
907 pos += obj_textsec (abfd)->size;
908 vma += obj_textsec (abfd)->size;
910 /* Data. */
911 if (!obj_datasec (abfd)->user_set_vma)
913 obj_textsec (abfd)->size += pad;
914 pos += pad;
915 vma += pad;
916 obj_datasec (abfd)->vma = vma;
918 else
919 vma = obj_datasec (abfd)->vma;
920 obj_datasec (abfd)->filepos = pos;
921 pos += obj_datasec (abfd)->size;
922 vma += obj_datasec (abfd)->size;
924 /* BSS. */
925 if (!obj_bsssec (abfd)->user_set_vma)
927 obj_datasec (abfd)->size += pad;
928 pos += pad;
929 vma += pad;
930 obj_bsssec (abfd)->vma = vma;
932 else
934 /* The VMA of the .bss section is set by the VMA of the
935 .data section plus the size of the .data section. We may
936 need to add padding bytes to make this true. */
937 pad = obj_bsssec (abfd)->vma - vma;
938 if (pad > 0)
940 obj_datasec (abfd)->size += pad;
941 pos += pad;
944 obj_bsssec (abfd)->filepos = pos;
946 /* Fix up the exec header. */
947 execp->a_text = obj_textsec (abfd)->size;
948 execp->a_data = obj_datasec (abfd)->size;
949 execp->a_bss = obj_bsssec (abfd)->size;
950 N_SET_MAGIC (*execp, OMAGIC);
953 static void
954 adjust_z_magic (abfd, execp)
955 bfd *abfd;
956 struct internal_exec *execp;
958 bfd_size_type data_pad, text_pad;
959 file_ptr text_end;
960 const struct aout_backend_data *abdp;
961 int ztih; /* Nonzero if text includes exec header. */
963 abdp = aout_backend_info (abfd);
965 /* Text. */
966 ztih = (abdp != NULL
967 && (abdp->text_includes_header
968 || obj_aout_subformat (abfd) == q_magic_format));
969 obj_textsec (abfd)->filepos = (ztih
970 ? adata (abfd).exec_bytes_size
971 : adata (abfd).zmagic_disk_block_size);
972 if (! obj_textsec (abfd)->user_set_vma)
974 /* ?? Do we really need to check for relocs here? */
975 obj_textsec (abfd)->vma = ((abfd->flags & HAS_RELOC)
977 : (ztih
978 ? (abdp->default_text_vma
979 + adata (abfd).exec_bytes_size)
980 : abdp->default_text_vma));
981 text_pad = 0;
983 else
985 /* The .text section is being loaded at an unusual address. We
986 may need to pad it such that the .data section starts at a page
987 boundary. */
988 if (ztih)
989 text_pad = ((obj_textsec (abfd)->filepos - obj_textsec (abfd)->vma)
990 & (adata (abfd).page_size - 1));
991 else
992 text_pad = ((- obj_textsec (abfd)->vma)
993 & (adata (abfd).page_size - 1));
996 /* Find start of data. */
997 if (ztih)
999 text_end = obj_textsec (abfd)->filepos + obj_textsec (abfd)->size;
1000 text_pad += BFD_ALIGN (text_end, adata (abfd).page_size) - text_end;
1002 else
1004 /* Note that if page_size == zmagic_disk_block_size, then
1005 filepos == page_size, and this case is the same as the ztih
1006 case. */
1007 text_end = obj_textsec (abfd)->size;
1008 text_pad += BFD_ALIGN (text_end, adata (abfd).page_size) - text_end;
1009 text_end += obj_textsec (abfd)->filepos;
1011 obj_textsec (abfd)->size += text_pad;
1012 text_end += text_pad;
1014 /* Data. */
1015 if (!obj_datasec (abfd)->user_set_vma)
1017 bfd_vma vma;
1018 vma = obj_textsec (abfd)->vma + obj_textsec (abfd)->size;
1019 obj_datasec (abfd)->vma = BFD_ALIGN (vma, adata (abfd).segment_size);
1021 if (abdp && abdp->zmagic_mapped_contiguous)
1023 asection * text = obj_textsec (abfd);
1024 asection * data = obj_datasec (abfd);
1026 text_pad = data->vma - (text->vma + text->size);
1027 /* Only pad the text section if the data
1028 section is going to be placed after it. */
1029 if (text_pad > 0)
1030 text->size += text_pad;
1032 obj_datasec (abfd)->filepos = (obj_textsec (abfd)->filepos
1033 + obj_textsec (abfd)->size);
1035 /* Fix up exec header while we're at it. */
1036 execp->a_text = obj_textsec (abfd)->size;
1037 if (ztih && (!abdp || (abdp && !abdp->exec_header_not_counted)))
1038 execp->a_text += adata (abfd).exec_bytes_size;
1039 if (obj_aout_subformat (abfd) == q_magic_format)
1040 N_SET_MAGIC (*execp, QMAGIC);
1041 else
1042 N_SET_MAGIC (*execp, ZMAGIC);
1044 /* Spec says data section should be rounded up to page boundary. */
1045 obj_datasec (abfd)->size
1046 = align_power (obj_datasec (abfd)->size,
1047 obj_bsssec (abfd)->alignment_power);
1048 execp->a_data = BFD_ALIGN (obj_datasec (abfd)->size,
1049 adata (abfd).page_size);
1050 data_pad = execp->a_data - obj_datasec (abfd)->size;
1052 /* BSS. */
1053 if (!obj_bsssec (abfd)->user_set_vma)
1054 obj_bsssec (abfd)->vma = (obj_datasec (abfd)->vma
1055 + obj_datasec (abfd)->size);
1056 /* If the BSS immediately follows the data section and extra space
1057 in the page is left after the data section, fudge data
1058 in the header so that the bss section looks smaller by that
1059 amount. We'll start the bss section there, and lie to the OS.
1060 (Note that a linker script, as well as the above assignment,
1061 could have explicitly set the BSS vma to immediately follow
1062 the data section.) */
1063 if (align_power (obj_bsssec (abfd)->vma, obj_bsssec (abfd)->alignment_power)
1064 == obj_datasec (abfd)->vma + obj_datasec (abfd)->size)
1065 execp->a_bss = (data_pad > obj_bsssec (abfd)->size
1066 ? 0 : obj_bsssec (abfd)->size - data_pad);
1067 else
1068 execp->a_bss = obj_bsssec (abfd)->size;
1071 static void
1072 adjust_n_magic (abfd, execp)
1073 bfd *abfd;
1074 struct internal_exec *execp;
1076 file_ptr pos = adata (abfd).exec_bytes_size;
1077 bfd_vma vma = 0;
1078 int pad;
1080 /* Text. */
1081 obj_textsec (abfd)->filepos = pos;
1082 if (!obj_textsec (abfd)->user_set_vma)
1083 obj_textsec (abfd)->vma = vma;
1084 else
1085 vma = obj_textsec (abfd)->vma;
1086 pos += obj_textsec (abfd)->size;
1087 vma += obj_textsec (abfd)->size;
1089 /* Data. */
1090 obj_datasec (abfd)->filepos = pos;
1091 if (!obj_datasec (abfd)->user_set_vma)
1092 obj_datasec (abfd)->vma = BFD_ALIGN (vma, adata (abfd).segment_size);
1093 vma = obj_datasec (abfd)->vma;
1095 /* Since BSS follows data immediately, see if it needs alignment. */
1096 vma += obj_datasec (abfd)->size;
1097 pad = align_power (vma, obj_bsssec (abfd)->alignment_power) - vma;
1098 obj_datasec (abfd)->size += pad;
1099 pos += obj_datasec (abfd)->size;
1101 /* BSS. */
1102 if (!obj_bsssec (abfd)->user_set_vma)
1103 obj_bsssec (abfd)->vma = vma;
1104 else
1105 vma = obj_bsssec (abfd)->vma;
1107 /* Fix up exec header. */
1108 execp->a_text = obj_textsec (abfd)->size;
1109 execp->a_data = obj_datasec (abfd)->size;
1110 execp->a_bss = obj_bsssec (abfd)->size;
1111 N_SET_MAGIC (*execp, NMAGIC);
1114 bfd_boolean
1115 NAME(aout,adjust_sizes_and_vmas) (abfd, text_size, text_end)
1116 bfd *abfd;
1117 bfd_size_type *text_size;
1118 file_ptr *text_end ATTRIBUTE_UNUSED;
1120 struct internal_exec *execp = exec_hdr (abfd);
1122 if (! NAME(aout,make_sections) (abfd))
1123 return FALSE;
1125 if (adata (abfd).magic != undecided_magic)
1126 return TRUE;
1128 obj_textsec (abfd)->size =
1129 align_power (obj_textsec (abfd)->size,
1130 obj_textsec (abfd)->alignment_power);
1132 *text_size = obj_textsec (abfd)->size;
1133 /* Rule (heuristic) for when to pad to a new page. Note that there
1134 are (at least) two ways demand-paged (ZMAGIC) files have been
1135 handled. Most Berkeley-based systems start the text segment at
1136 (TARGET_PAGE_SIZE). However, newer versions of SUNOS start the text
1137 segment right after the exec header; the latter is counted in the
1138 text segment size, and is paged in by the kernel with the rest of
1139 the text. */
1141 /* This perhaps isn't the right way to do this, but made it simpler for me
1142 to understand enough to implement it. Better would probably be to go
1143 right from BFD flags to alignment/positioning characteristics. But the
1144 old code was sloppy enough about handling the flags, and had enough
1145 other magic, that it was a little hard for me to understand. I think
1146 I understand it better now, but I haven't time to do the cleanup this
1147 minute. */
1149 if (abfd->flags & D_PAGED)
1150 /* Whether or not WP_TEXT is set -- let D_PAGED override. */
1151 adata (abfd).magic = z_magic;
1152 else if (abfd->flags & WP_TEXT)
1153 adata (abfd).magic = n_magic;
1154 else
1155 adata (abfd).magic = o_magic;
1157 #ifdef BFD_AOUT_DEBUG /* requires gcc2 */
1158 #if __GNUC__ >= 2
1159 fprintf (stderr, "%s text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x,%x>\n",
1160 ({ char *str;
1161 switch (adata (abfd).magic)
1163 case n_magic: str = "NMAGIC"; break;
1164 case o_magic: str = "OMAGIC"; break;
1165 case z_magic: str = "ZMAGIC"; break;
1166 default: abort ();
1168 str;
1170 obj_textsec (abfd)->vma, obj_textsec (abfd)->size,
1171 obj_textsec (abfd)->alignment_power,
1172 obj_datasec (abfd)->vma, obj_datasec (abfd)->size,
1173 obj_datasec (abfd)->alignment_power,
1174 obj_bsssec (abfd)->vma, obj_bsssec (abfd)->size,
1175 obj_bsssec (abfd)->alignment_power);
1176 #endif
1177 #endif
1179 switch (adata (abfd).magic)
1181 case o_magic:
1182 adjust_o_magic (abfd, execp);
1183 break;
1184 case z_magic:
1185 adjust_z_magic (abfd, execp);
1186 break;
1187 case n_magic:
1188 adjust_n_magic (abfd, execp);
1189 break;
1190 default:
1191 abort ();
1194 #ifdef BFD_AOUT_DEBUG
1195 fprintf (stderr, " text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x>\n",
1196 obj_textsec (abfd)->vma, obj_textsec (abfd)->size,
1197 obj_textsec (abfd)->filepos,
1198 obj_datasec (abfd)->vma, obj_datasec (abfd)->size,
1199 obj_datasec (abfd)->filepos,
1200 obj_bsssec (abfd)->vma, obj_bsssec (abfd)->size);
1201 #endif
1203 return TRUE;
1207 FUNCTION
1208 aout_@var{size}_new_section_hook
1210 SYNOPSIS
1211 bfd_boolean aout_@var{size}_new_section_hook,
1212 (bfd *abfd,
1213 asection *newsect));
1215 DESCRIPTION
1216 Called by the BFD in response to a @code{bfd_make_section}
1217 request.
1219 bfd_boolean
1220 NAME(aout,new_section_hook) (abfd, newsect)
1221 bfd *abfd;
1222 asection *newsect;
1224 /* Align to double at least. */
1225 newsect->alignment_power = bfd_get_arch_info (abfd)->section_align_power;
1227 if (bfd_get_format (abfd) == bfd_object)
1229 if (obj_textsec (abfd) == NULL && !strcmp (newsect->name, ".text"))
1231 obj_textsec (abfd)= newsect;
1232 newsect->target_index = N_TEXT;
1233 return TRUE;
1236 if (obj_datasec (abfd) == NULL && !strcmp (newsect->name, ".data"))
1238 obj_datasec (abfd) = newsect;
1239 newsect->target_index = N_DATA;
1240 return TRUE;
1243 if (obj_bsssec (abfd) == NULL && !strcmp (newsect->name, ".bss"))
1245 obj_bsssec (abfd) = newsect;
1246 newsect->target_index = N_BSS;
1247 return TRUE;
1251 /* We allow more than three sections internally. */
1252 return TRUE;
1255 bfd_boolean
1256 NAME(aout,set_section_contents) (abfd, section, location, offset, count)
1257 bfd *abfd;
1258 sec_ptr section;
1259 const PTR location;
1260 file_ptr offset;
1261 bfd_size_type count;
1263 file_ptr text_end;
1264 bfd_size_type text_size;
1266 if (! abfd->output_has_begun)
1268 if (! NAME(aout,adjust_sizes_and_vmas) (abfd, &text_size, &text_end))
1269 return FALSE;
1272 if (section == obj_bsssec (abfd))
1274 bfd_set_error (bfd_error_no_contents);
1275 return FALSE;
1278 if (section != obj_textsec (abfd)
1279 && section != obj_datasec (abfd))
1281 if (aout_section_merge_with_text_p (abfd, section))
1282 section->filepos = obj_textsec (abfd)->filepos +
1283 (section->vma - obj_textsec (abfd)->vma);
1284 else
1286 (*_bfd_error_handler)
1287 (_("%s: can not represent section `%s' in a.out object file format"),
1288 bfd_get_filename (abfd), bfd_get_section_name (abfd, section));
1289 bfd_set_error (bfd_error_nonrepresentable_section);
1290 return FALSE;
1294 if (count != 0)
1296 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
1297 || bfd_bwrite (location, count, abfd) != count)
1298 return FALSE;
1301 return TRUE;
1304 /* Read the external symbols from an a.out file. */
1306 static bfd_boolean
1307 aout_get_external_symbols (abfd)
1308 bfd *abfd;
1310 if (obj_aout_external_syms (abfd) == (struct external_nlist *) NULL)
1312 bfd_size_type count;
1313 struct external_nlist *syms;
1314 bfd_size_type amt;
1316 count = exec_hdr (abfd)->a_syms / EXTERNAL_NLIST_SIZE;
1318 #ifdef USE_MMAP
1319 if (! bfd_get_file_window (abfd, obj_sym_filepos (abfd),
1320 exec_hdr (abfd)->a_syms,
1321 &obj_aout_sym_window (abfd), TRUE))
1322 return FALSE;
1323 syms = (struct external_nlist *) obj_aout_sym_window (abfd).data;
1324 #else
1325 /* We allocate using malloc to make the values easy to free
1326 later on. If we put them on the objalloc it might not be
1327 possible to free them. */
1328 syms = ((struct external_nlist *)
1329 bfd_malloc (count * EXTERNAL_NLIST_SIZE));
1330 if (syms == (struct external_nlist *) NULL && count != 0)
1331 return FALSE;
1333 amt = exec_hdr (abfd)->a_syms;
1334 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
1335 || bfd_bread (syms, amt, abfd) != amt)
1337 free (syms);
1338 return FALSE;
1340 #endif
1342 obj_aout_external_syms (abfd) = syms;
1343 obj_aout_external_sym_count (abfd) = count;
1346 if (obj_aout_external_strings (abfd) == NULL
1347 && exec_hdr (abfd)->a_syms != 0)
1349 unsigned char string_chars[BYTES_IN_WORD];
1350 bfd_size_type stringsize;
1351 char *strings;
1352 bfd_size_type amt = BYTES_IN_WORD;
1354 /* Get the size of the strings. */
1355 if (bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET) != 0
1356 || bfd_bread ((PTR) string_chars, amt, abfd) != amt)
1357 return FALSE;
1358 stringsize = GET_WORD (abfd, string_chars);
1360 #ifdef USE_MMAP
1361 if (! bfd_get_file_window (abfd, obj_str_filepos (abfd), stringsize,
1362 &obj_aout_string_window (abfd), TRUE))
1363 return FALSE;
1364 strings = (char *) obj_aout_string_window (abfd).data;
1365 #else
1366 strings = (char *) bfd_malloc (stringsize + 1);
1367 if (strings == NULL)
1368 return FALSE;
1370 /* Skip space for the string count in the buffer for convenience
1371 when using indexes. */
1372 amt = stringsize - BYTES_IN_WORD;
1373 if (bfd_bread (strings + BYTES_IN_WORD, amt, abfd) != amt)
1375 free (strings);
1376 return FALSE;
1378 #endif
1380 /* Ensure that a zero index yields an empty string. */
1381 strings[0] = '\0';
1383 strings[stringsize - 1] = 0;
1385 obj_aout_external_strings (abfd) = strings;
1386 obj_aout_external_string_size (abfd) = stringsize;
1389 return TRUE;
1392 /* Translate an a.out symbol into a BFD symbol. The desc, other, type
1393 and symbol->value fields of CACHE_PTR will be set from the a.out
1394 nlist structure. This function is responsible for setting
1395 symbol->flags and symbol->section, and adjusting symbol->value. */
1397 static bfd_boolean
1398 translate_from_native_sym_flags (abfd, cache_ptr)
1399 bfd *abfd;
1400 aout_symbol_type *cache_ptr;
1402 flagword visible;
1404 if ((cache_ptr->type & N_STAB) != 0
1405 || cache_ptr->type == N_FN)
1407 asection *sec;
1409 /* This is a debugging symbol. */
1410 cache_ptr->symbol.flags = BSF_DEBUGGING;
1412 /* Work out the symbol section. */
1413 switch (cache_ptr->type & N_TYPE)
1415 case N_TEXT:
1416 case N_FN:
1417 sec = obj_textsec (abfd);
1418 break;
1419 case N_DATA:
1420 sec = obj_datasec (abfd);
1421 break;
1422 case N_BSS:
1423 sec = obj_bsssec (abfd);
1424 break;
1425 default:
1426 case N_ABS:
1427 sec = bfd_abs_section_ptr;
1428 break;
1431 cache_ptr->symbol.section = sec;
1432 cache_ptr->symbol.value -= sec->vma;
1434 return TRUE;
1437 /* Get the default visibility. This does not apply to all types, so
1438 we just hold it in a local variable to use if wanted. */
1439 if ((cache_ptr->type & N_EXT) == 0)
1440 visible = BSF_LOCAL;
1441 else
1442 visible = BSF_GLOBAL;
1444 switch (cache_ptr->type)
1446 default:
1447 case N_ABS: case N_ABS | N_EXT:
1448 cache_ptr->symbol.section = bfd_abs_section_ptr;
1449 cache_ptr->symbol.flags = visible;
1450 break;
1452 case N_UNDF | N_EXT:
1453 if (cache_ptr->symbol.value != 0)
1455 /* This is a common symbol. */
1456 cache_ptr->symbol.flags = BSF_GLOBAL;
1457 cache_ptr->symbol.section = bfd_com_section_ptr;
1459 else
1461 cache_ptr->symbol.flags = 0;
1462 cache_ptr->symbol.section = bfd_und_section_ptr;
1464 break;
1466 case N_TEXT: case N_TEXT | N_EXT:
1467 cache_ptr->symbol.section = obj_textsec (abfd);
1468 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1469 cache_ptr->symbol.flags = visible;
1470 break;
1472 /* N_SETV symbols used to represent set vectors placed in the
1473 data section. They are no longer generated. Theoretically,
1474 it was possible to extract the entries and combine them with
1475 new ones, although I don't know if that was ever actually
1476 done. Unless that feature is restored, treat them as data
1477 symbols. */
1478 case N_SETV: case N_SETV | N_EXT:
1479 case N_DATA: case N_DATA | N_EXT:
1480 cache_ptr->symbol.section = obj_datasec (abfd);
1481 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1482 cache_ptr->symbol.flags = visible;
1483 break;
1485 case N_BSS: case N_BSS | N_EXT:
1486 cache_ptr->symbol.section = obj_bsssec (abfd);
1487 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1488 cache_ptr->symbol.flags = visible;
1489 break;
1491 case N_SETA: case N_SETA | N_EXT:
1492 case N_SETT: case N_SETT | N_EXT:
1493 case N_SETD: case N_SETD | N_EXT:
1494 case N_SETB: case N_SETB | N_EXT:
1496 /* This code is no longer needed. It used to be used to make
1497 the linker handle set symbols, but they are now handled in
1498 the add_symbols routine instead. */
1499 switch (cache_ptr->type & N_TYPE)
1501 case N_SETA:
1502 cache_ptr->symbol.section = bfd_abs_section_ptr;
1503 break;
1504 case N_SETT:
1505 cache_ptr->symbol.section = obj_textsec (abfd);
1506 break;
1507 case N_SETD:
1508 cache_ptr->symbol.section = obj_datasec (abfd);
1509 break;
1510 case N_SETB:
1511 cache_ptr->symbol.section = obj_bsssec (abfd);
1512 break;
1515 cache_ptr->symbol.flags |= BSF_CONSTRUCTOR;
1517 break;
1519 case N_WARNING:
1520 /* This symbol is the text of a warning message. The next
1521 symbol is the symbol to associate the warning with. If a
1522 reference is made to that symbol, a warning is issued. */
1523 cache_ptr->symbol.flags = BSF_DEBUGGING | BSF_WARNING;
1524 cache_ptr->symbol.section = bfd_abs_section_ptr;
1525 break;
1527 case N_INDR: case N_INDR | N_EXT:
1528 /* An indirect symbol. This consists of two symbols in a row.
1529 The first symbol is the name of the indirection. The second
1530 symbol is the name of the target. A reference to the first
1531 symbol becomes a reference to the second. */
1532 cache_ptr->symbol.flags = BSF_DEBUGGING | BSF_INDIRECT | visible;
1533 cache_ptr->symbol.section = bfd_ind_section_ptr;
1534 break;
1536 case N_WEAKU:
1537 cache_ptr->symbol.section = bfd_und_section_ptr;
1538 cache_ptr->symbol.flags = BSF_WEAK;
1539 break;
1541 case N_WEAKA:
1542 cache_ptr->symbol.section = bfd_abs_section_ptr;
1543 cache_ptr->symbol.flags = BSF_WEAK;
1544 break;
1546 case N_WEAKT:
1547 cache_ptr->symbol.section = obj_textsec (abfd);
1548 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1549 cache_ptr->symbol.flags = BSF_WEAK;
1550 break;
1552 case N_WEAKD:
1553 cache_ptr->symbol.section = obj_datasec (abfd);
1554 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1555 cache_ptr->symbol.flags = BSF_WEAK;
1556 break;
1558 case N_WEAKB:
1559 cache_ptr->symbol.section = obj_bsssec (abfd);
1560 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1561 cache_ptr->symbol.flags = BSF_WEAK;
1562 break;
1565 return TRUE;
1568 /* Set the fields of SYM_POINTER according to CACHE_PTR. */
1570 static bfd_boolean
1571 translate_to_native_sym_flags (abfd, cache_ptr, sym_pointer)
1572 bfd *abfd;
1573 asymbol *cache_ptr;
1574 struct external_nlist *sym_pointer;
1576 bfd_vma value = cache_ptr->value;
1577 asection *sec;
1578 bfd_vma off;
1580 /* Mask out any existing type bits in case copying from one section
1581 to another. */
1582 sym_pointer->e_type[0] &= ~N_TYPE;
1584 sec = bfd_get_section (cache_ptr);
1585 off = 0;
1587 if (sec == NULL)
1589 /* This case occurs, e.g., for the *DEBUG* section of a COFF
1590 file. */
1591 (*_bfd_error_handler)
1592 (_("%s: can not represent section for symbol `%s' in a.out object file format"),
1593 bfd_get_filename (abfd),
1594 cache_ptr->name != NULL ? cache_ptr->name : _("*unknown*"));
1595 bfd_set_error (bfd_error_nonrepresentable_section);
1596 return FALSE;
1599 if (sec->output_section != NULL)
1601 off = sec->output_offset;
1602 sec = sec->output_section;
1605 if (bfd_is_abs_section (sec))
1606 sym_pointer->e_type[0] |= N_ABS;
1607 else if (sec == obj_textsec (abfd))
1608 sym_pointer->e_type[0] |= N_TEXT;
1609 else if (sec == obj_datasec (abfd))
1610 sym_pointer->e_type[0] |= N_DATA;
1611 else if (sec == obj_bsssec (abfd))
1612 sym_pointer->e_type[0] |= N_BSS;
1613 else if (bfd_is_und_section (sec))
1614 sym_pointer->e_type[0] = N_UNDF | N_EXT;
1615 else if (bfd_is_ind_section (sec))
1616 sym_pointer->e_type[0] = N_INDR;
1617 else if (bfd_is_com_section (sec))
1618 sym_pointer->e_type[0] = N_UNDF | N_EXT;
1619 else
1621 if (aout_section_merge_with_text_p (abfd, sec))
1622 sym_pointer->e_type[0] |= N_TEXT;
1623 else
1625 (*_bfd_error_handler)
1626 (_("%s: can not represent section `%s' in a.out object file format"),
1627 bfd_get_filename (abfd), bfd_get_section_name (abfd, sec));
1628 bfd_set_error (bfd_error_nonrepresentable_section);
1629 return FALSE;
1633 /* Turn the symbol from section relative to absolute again. */
1634 value += sec->vma + off;
1636 if ((cache_ptr->flags & BSF_WARNING) != 0)
1637 sym_pointer->e_type[0] = N_WARNING;
1639 if ((cache_ptr->flags & BSF_DEBUGGING) != 0)
1640 sym_pointer->e_type[0] = ((aout_symbol_type *) cache_ptr)->type;
1641 else if ((cache_ptr->flags & BSF_GLOBAL) != 0)
1642 sym_pointer->e_type[0] |= N_EXT;
1643 else if ((cache_ptr->flags & BSF_LOCAL) != 0)
1644 sym_pointer->e_type[0] &= ~N_EXT;
1646 if ((cache_ptr->flags & BSF_CONSTRUCTOR) != 0)
1648 int type = ((aout_symbol_type *) cache_ptr)->type;
1650 switch (type)
1652 case N_ABS: type = N_SETA; break;
1653 case N_TEXT: type = N_SETT; break;
1654 case N_DATA: type = N_SETD; break;
1655 case N_BSS: type = N_SETB; break;
1657 sym_pointer->e_type[0] = type;
1660 if ((cache_ptr->flags & BSF_WEAK) != 0)
1662 int type;
1664 switch (sym_pointer->e_type[0] & N_TYPE)
1666 default:
1667 case N_ABS: type = N_WEAKA; break;
1668 case N_TEXT: type = N_WEAKT; break;
1669 case N_DATA: type = N_WEAKD; break;
1670 case N_BSS: type = N_WEAKB; break;
1671 case N_UNDF: type = N_WEAKU; break;
1673 sym_pointer->e_type[0] = type;
1676 PUT_WORD (abfd, value, sym_pointer->e_value);
1678 return TRUE;
1681 /* Native-level interface to symbols. */
1683 asymbol *
1684 NAME(aout,make_empty_symbol) (abfd)
1685 bfd *abfd;
1687 bfd_size_type amt = sizeof (aout_symbol_type);
1688 aout_symbol_type *new = (aout_symbol_type *) bfd_zalloc (abfd, amt);
1689 if (!new)
1690 return NULL;
1691 new->symbol.the_bfd = abfd;
1693 return &new->symbol;
1696 /* Translate a set of internal symbols into external symbols. */
1698 bfd_boolean
1699 NAME(aout,translate_symbol_table) (abfd, in, ext, count, str, strsize, dynamic)
1700 bfd *abfd;
1701 aout_symbol_type *in;
1702 struct external_nlist *ext;
1703 bfd_size_type count;
1704 char *str;
1705 bfd_size_type strsize;
1706 bfd_boolean dynamic;
1708 struct external_nlist *ext_end;
1710 ext_end = ext + count;
1711 for (; ext < ext_end; ext++, in++)
1713 bfd_vma x;
1715 x = GET_WORD (abfd, ext->e_strx);
1716 in->symbol.the_bfd = abfd;
1718 /* For the normal symbols, the zero index points at the number
1719 of bytes in the string table but is to be interpreted as the
1720 null string. For the dynamic symbols, the number of bytes in
1721 the string table is stored in the __DYNAMIC structure and the
1722 zero index points at an actual string. */
1723 if (x == 0 && ! dynamic)
1724 in->symbol.name = "";
1725 else if (x < strsize)
1726 in->symbol.name = str + x;
1727 else
1728 return FALSE;
1730 in->symbol.value = GET_SWORD (abfd, ext->e_value);
1731 in->desc = H_GET_16 (abfd, ext->e_desc);
1732 in->other = H_GET_8 (abfd, ext->e_other);
1733 in->type = H_GET_8 (abfd, ext->e_type);
1734 in->symbol.udata.p = NULL;
1736 if (! translate_from_native_sym_flags (abfd, in))
1737 return FALSE;
1739 if (dynamic)
1740 in->symbol.flags |= BSF_DYNAMIC;
1743 return TRUE;
1746 /* We read the symbols into a buffer, which is discarded when this
1747 function exits. We read the strings into a buffer large enough to
1748 hold them all plus all the cached symbol entries. */
1750 bfd_boolean
1751 NAME(aout,slurp_symbol_table) (abfd)
1752 bfd *abfd;
1754 struct external_nlist *old_external_syms;
1755 aout_symbol_type *cached;
1756 bfd_size_type cached_size;
1758 /* If there's no work to be done, don't do any. */
1759 if (obj_aout_symbols (abfd) != (aout_symbol_type *) NULL)
1760 return TRUE;
1762 old_external_syms = obj_aout_external_syms (abfd);
1764 if (! aout_get_external_symbols (abfd))
1765 return FALSE;
1767 cached_size = obj_aout_external_sym_count (abfd);
1768 cached_size *= sizeof (aout_symbol_type);
1769 cached = (aout_symbol_type *) bfd_zmalloc (cached_size);
1770 if (cached == NULL && cached_size != 0)
1771 return FALSE;
1773 /* Convert from external symbol information to internal. */
1774 if (! (NAME(aout,translate_symbol_table)
1775 (abfd, cached,
1776 obj_aout_external_syms (abfd),
1777 obj_aout_external_sym_count (abfd),
1778 obj_aout_external_strings (abfd),
1779 obj_aout_external_string_size (abfd),
1780 FALSE)))
1782 free (cached);
1783 return FALSE;
1786 bfd_get_symcount (abfd) = obj_aout_external_sym_count (abfd);
1788 obj_aout_symbols (abfd) = cached;
1790 /* It is very likely that anybody who calls this function will not
1791 want the external symbol information, so if it was allocated
1792 because of our call to aout_get_external_symbols, we free it up
1793 right away to save space. */
1794 if (old_external_syms == (struct external_nlist *) NULL
1795 && obj_aout_external_syms (abfd) != (struct external_nlist *) NULL)
1797 #ifdef USE_MMAP
1798 bfd_free_window (&obj_aout_sym_window (abfd));
1799 #else
1800 free (obj_aout_external_syms (abfd));
1801 #endif
1802 obj_aout_external_syms (abfd) = NULL;
1805 return TRUE;
1808 /* We use a hash table when writing out symbols so that we only write
1809 out a particular string once. This helps particularly when the
1810 linker writes out stabs debugging entries, because each different
1811 contributing object file tends to have many duplicate stabs
1812 strings.
1814 This hash table code breaks dbx on SunOS 4.1.3, so we don't do it
1815 if BFD_TRADITIONAL_FORMAT is set. */
1817 static bfd_size_type add_to_stringtab
1818 PARAMS ((bfd *, struct bfd_strtab_hash *, const char *, bfd_boolean));
1819 static bfd_boolean emit_stringtab
1820 PARAMS ((bfd *, struct bfd_strtab_hash *));
1822 /* Get the index of a string in a strtab, adding it if it is not
1823 already present. */
1825 static INLINE bfd_size_type
1826 add_to_stringtab (abfd, tab, str, copy)
1827 bfd *abfd;
1828 struct bfd_strtab_hash *tab;
1829 const char *str;
1830 bfd_boolean copy;
1832 bfd_boolean hash;
1833 bfd_size_type index;
1835 /* An index of 0 always means the empty string. */
1836 if (str == 0 || *str == '\0')
1837 return 0;
1839 /* Don't hash if BFD_TRADITIONAL_FORMAT is set, because SunOS dbx
1840 doesn't understand a hashed string table. */
1841 hash = TRUE;
1842 if ((abfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
1843 hash = FALSE;
1845 index = _bfd_stringtab_add (tab, str, hash, copy);
1847 if (index != (bfd_size_type) -1)
1849 /* Add BYTES_IN_WORD to the return value to account for the
1850 space taken up by the string table size. */
1851 index += BYTES_IN_WORD;
1854 return index;
1857 /* Write out a strtab. ABFD is already at the right location in the
1858 file. */
1860 static bfd_boolean
1861 emit_stringtab (abfd, tab)
1862 register bfd *abfd;
1863 struct bfd_strtab_hash *tab;
1865 bfd_byte buffer[BYTES_IN_WORD];
1866 bfd_size_type amt = BYTES_IN_WORD;
1868 /* The string table starts with the size. */
1869 PUT_WORD (abfd, _bfd_stringtab_size (tab) + BYTES_IN_WORD, buffer);
1870 if (bfd_bwrite ((PTR) buffer, amt, abfd) != amt)
1871 return FALSE;
1873 return _bfd_stringtab_emit (abfd, tab);
1876 bfd_boolean
1877 NAME(aout,write_syms) (abfd)
1878 bfd *abfd;
1880 unsigned int count ;
1881 asymbol **generic = bfd_get_outsymbols (abfd);
1882 struct bfd_strtab_hash *strtab;
1884 strtab = _bfd_stringtab_init ();
1885 if (strtab == NULL)
1886 return FALSE;
1888 for (count = 0; count < bfd_get_symcount (abfd); count++)
1890 asymbol *g = generic[count];
1891 bfd_size_type indx;
1892 struct external_nlist nsp;
1893 bfd_size_type amt;
1895 indx = add_to_stringtab (abfd, strtab, g->name, FALSE);
1896 if (indx == (bfd_size_type) -1)
1897 goto error_return;
1898 PUT_WORD (abfd, indx, (bfd_byte *) nsp.e_strx);
1900 if (bfd_asymbol_flavour (g) == abfd->xvec->flavour)
1902 H_PUT_16 (abfd, aout_symbol (g)->desc, nsp.e_desc);
1903 H_PUT_8 (abfd, aout_symbol (g)->other, nsp.e_other);
1904 H_PUT_8 (abfd, aout_symbol (g)->type, nsp.e_type);
1906 else
1908 H_PUT_16 (abfd, 0, nsp.e_desc);
1909 H_PUT_8 (abfd, 0, nsp.e_other);
1910 H_PUT_8 (abfd, 0, nsp.e_type);
1913 if (! translate_to_native_sym_flags (abfd, g, &nsp))
1914 goto error_return;
1916 amt = EXTERNAL_NLIST_SIZE;
1917 if (bfd_bwrite ((PTR) &nsp, amt, abfd) != amt)
1918 goto error_return;
1920 /* NB: `KEEPIT' currently overlays `udata.p', so set this only
1921 here, at the end. */
1922 g->KEEPIT = count;
1925 if (! emit_stringtab (abfd, strtab))
1926 goto error_return;
1928 _bfd_stringtab_free (strtab);
1930 return TRUE;
1932 error_return:
1933 _bfd_stringtab_free (strtab);
1934 return FALSE;
1937 long
1938 NAME(aout,canonicalize_symtab) (abfd, location)
1939 bfd *abfd;
1940 asymbol **location;
1942 unsigned int counter = 0;
1943 aout_symbol_type *symbase;
1945 if (!NAME(aout,slurp_symbol_table) (abfd))
1946 return -1;
1948 for (symbase = obj_aout_symbols (abfd);
1949 counter++ < bfd_get_symcount (abfd);
1951 *(location++) = (asymbol *) (symbase++);
1952 *location++ =0;
1953 return bfd_get_symcount (abfd);
1956 /* Standard reloc stuff. */
1957 /* Output standard relocation information to a file in target byte order. */
1959 extern void NAME(aout,swap_std_reloc_out)
1960 PARAMS ((bfd *, arelent *, struct reloc_std_external *));
1962 void
1963 NAME(aout,swap_std_reloc_out) (abfd, g, natptr)
1964 bfd *abfd;
1965 arelent *g;
1966 struct reloc_std_external *natptr;
1968 int r_index;
1969 asymbol *sym = *(g->sym_ptr_ptr);
1970 int r_extern;
1971 unsigned int r_length;
1972 int r_pcrel;
1973 int r_baserel, r_jmptable, r_relative;
1974 asection *output_section = sym->section->output_section;
1976 PUT_WORD (abfd, g->address, natptr->r_address);
1978 r_length = g->howto->size ; /* Size as a power of two. */
1979 r_pcrel = (int) g->howto->pc_relative; /* Relative to PC? */
1980 /* XXX This relies on relocs coming from a.out files. */
1981 r_baserel = (g->howto->type & 8) != 0;
1982 r_jmptable = (g->howto->type & 16) != 0;
1983 r_relative = (g->howto->type & 32) != 0;
1985 /* Name was clobbered by aout_write_syms to be symbol index. */
1987 /* If this relocation is relative to a symbol then set the
1988 r_index to the symbols index, and the r_extern bit.
1990 Absolute symbols can come in in two ways, either as an offset
1991 from the abs section, or as a symbol which has an abs value.
1992 check for that here. */
1994 if (bfd_is_com_section (output_section)
1995 || bfd_is_abs_section (output_section)
1996 || bfd_is_und_section (output_section))
1998 if (bfd_abs_section_ptr->symbol == sym)
2000 /* Whoops, looked like an abs symbol, but is
2001 really an offset from the abs section. */
2002 r_index = N_ABS;
2003 r_extern = 0;
2005 else
2007 /* Fill in symbol. */
2008 r_extern = 1;
2009 r_index = (*(g->sym_ptr_ptr))->KEEPIT;
2012 else
2014 /* Just an ordinary section. */
2015 r_extern = 0;
2016 r_index = output_section->target_index;
2019 /* Now the fun stuff. */
2020 if (bfd_header_big_endian (abfd))
2022 natptr->r_index[0] = r_index >> 16;
2023 natptr->r_index[1] = r_index >> 8;
2024 natptr->r_index[2] = r_index;
2025 natptr->r_type[0] = ((r_extern ? RELOC_STD_BITS_EXTERN_BIG : 0)
2026 | (r_pcrel ? RELOC_STD_BITS_PCREL_BIG : 0)
2027 | (r_baserel ? RELOC_STD_BITS_BASEREL_BIG : 0)
2028 | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_BIG : 0)
2029 | (r_relative ? RELOC_STD_BITS_RELATIVE_BIG : 0)
2030 | (r_length << RELOC_STD_BITS_LENGTH_SH_BIG));
2032 else
2034 natptr->r_index[2] = r_index >> 16;
2035 natptr->r_index[1] = r_index >> 8;
2036 natptr->r_index[0] = r_index;
2037 natptr->r_type[0] = ((r_extern ? RELOC_STD_BITS_EXTERN_LITTLE : 0)
2038 | (r_pcrel ? RELOC_STD_BITS_PCREL_LITTLE : 0)
2039 | (r_baserel ? RELOC_STD_BITS_BASEREL_LITTLE : 0)
2040 | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_LITTLE : 0)
2041 | (r_relative ? RELOC_STD_BITS_RELATIVE_LITTLE : 0)
2042 | (r_length << RELOC_STD_BITS_LENGTH_SH_LITTLE));
2046 /* Extended stuff. */
2047 /* Output extended relocation information to a file in target byte order. */
2049 extern void NAME(aout,swap_ext_reloc_out)
2050 PARAMS ((bfd *, arelent *, struct reloc_ext_external *));
2052 void
2053 NAME(aout,swap_ext_reloc_out) (abfd, g, natptr)
2054 bfd *abfd;
2055 arelent *g;
2056 register struct reloc_ext_external *natptr;
2058 int r_index;
2059 int r_extern;
2060 unsigned int r_type;
2061 bfd_vma r_addend;
2062 asymbol *sym = *(g->sym_ptr_ptr);
2063 asection *output_section = sym->section->output_section;
2065 PUT_WORD (abfd, g->address, natptr->r_address);
2067 r_type = (unsigned int) g->howto->type;
2069 r_addend = g->addend;
2070 if ((sym->flags & BSF_SECTION_SYM) != 0)
2071 r_addend += (*(g->sym_ptr_ptr))->section->output_section->vma;
2073 /* If this relocation is relative to a symbol then set the
2074 r_index to the symbols index, and the r_extern bit.
2076 Absolute symbols can come in in two ways, either as an offset
2077 from the abs section, or as a symbol which has an abs value.
2078 check for that here. */
2079 if (bfd_is_abs_section (bfd_get_section (sym)))
2081 r_extern = 0;
2082 r_index = N_ABS;
2084 else if ((sym->flags & BSF_SECTION_SYM) == 0)
2086 if (bfd_is_und_section (bfd_get_section (sym))
2087 || (sym->flags & BSF_GLOBAL) != 0)
2088 r_extern = 1;
2089 else
2090 r_extern = 0;
2091 r_index = (*(g->sym_ptr_ptr))->KEEPIT;
2093 else
2095 /* Just an ordinary section. */
2096 r_extern = 0;
2097 r_index = output_section->target_index;
2100 /* Now the fun stuff. */
2101 if (bfd_header_big_endian (abfd))
2103 natptr->r_index[0] = r_index >> 16;
2104 natptr->r_index[1] = r_index >> 8;
2105 natptr->r_index[2] = r_index;
2106 natptr->r_type[0] = ((r_extern ? RELOC_EXT_BITS_EXTERN_BIG : 0)
2107 | (r_type << RELOC_EXT_BITS_TYPE_SH_BIG));
2109 else
2111 natptr->r_index[2] = r_index >> 16;
2112 natptr->r_index[1] = r_index >> 8;
2113 natptr->r_index[0] = r_index;
2114 natptr->r_type[0] = ((r_extern ? RELOC_EXT_BITS_EXTERN_LITTLE : 0)
2115 | (r_type << RELOC_EXT_BITS_TYPE_SH_LITTLE));
2118 PUT_WORD (abfd, r_addend, natptr->r_addend);
2121 /* BFD deals internally with all things based from the section they're
2122 in. so, something in 10 bytes into a text section with a base of
2123 50 would have a symbol (.text+10) and know .text vma was 50.
2125 Aout keeps all it's symbols based from zero, so the symbol would
2126 contain 60. This macro subs the base of each section from the value
2127 to give the true offset from the section. */
2129 #define MOVE_ADDRESS(ad) \
2130 if (r_extern) \
2132 /* Undefined symbol. */ \
2133 cache_ptr->sym_ptr_ptr = symbols + r_index; \
2134 cache_ptr->addend = ad; \
2136 else \
2138 /* Defined, section relative. Replace symbol with pointer to \
2139 symbol which points to section. */ \
2140 switch (r_index) \
2142 case N_TEXT: \
2143 case N_TEXT | N_EXT: \
2144 cache_ptr->sym_ptr_ptr = obj_textsec (abfd)->symbol_ptr_ptr; \
2145 cache_ptr->addend = ad - su->textsec->vma; \
2146 break; \
2147 case N_DATA: \
2148 case N_DATA | N_EXT: \
2149 cache_ptr->sym_ptr_ptr = obj_datasec (abfd)->symbol_ptr_ptr; \
2150 cache_ptr->addend = ad - su->datasec->vma; \
2151 break; \
2152 case N_BSS: \
2153 case N_BSS | N_EXT: \
2154 cache_ptr->sym_ptr_ptr = obj_bsssec (abfd)->symbol_ptr_ptr; \
2155 cache_ptr->addend = ad - su->bsssec->vma; \
2156 break; \
2157 default: \
2158 case N_ABS: \
2159 case N_ABS | N_EXT: \
2160 cache_ptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr; \
2161 cache_ptr->addend = ad; \
2162 break; \
2166 void
2167 NAME(aout,swap_ext_reloc_in) (abfd, bytes, cache_ptr, symbols, symcount)
2168 bfd *abfd;
2169 struct reloc_ext_external *bytes;
2170 arelent *cache_ptr;
2171 asymbol **symbols;
2172 bfd_size_type symcount;
2174 unsigned int r_index;
2175 int r_extern;
2176 unsigned int r_type;
2177 struct aoutdata *su = &(abfd->tdata.aout_data->a);
2179 cache_ptr->address = (GET_SWORD (abfd, bytes->r_address));
2181 /* Now the fun stuff. */
2182 if (bfd_header_big_endian (abfd))
2184 r_index = (((unsigned int) bytes->r_index[0] << 16)
2185 | ((unsigned int) bytes->r_index[1] << 8)
2186 | bytes->r_index[2]);
2187 r_extern = (0 != (bytes->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG));
2188 r_type = ((bytes->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
2189 >> RELOC_EXT_BITS_TYPE_SH_BIG);
2191 else
2193 r_index = (((unsigned int) bytes->r_index[2] << 16)
2194 | ((unsigned int) bytes->r_index[1] << 8)
2195 | bytes->r_index[0]);
2196 r_extern = (0 != (bytes->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
2197 r_type = ((bytes->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
2198 >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
2201 cache_ptr->howto = howto_table_ext + r_type;
2203 /* Base relative relocs are always against the symbol table,
2204 regardless of the setting of r_extern. r_extern just reflects
2205 whether the symbol the reloc is against is local or global. */
2206 if (r_type == (unsigned int) RELOC_BASE10
2207 || r_type == (unsigned int) RELOC_BASE13
2208 || r_type == (unsigned int) RELOC_BASE22)
2209 r_extern = 1;
2211 if (r_extern && r_index > symcount)
2213 /* We could arrange to return an error, but it might be useful
2214 to see the file even if it is bad. */
2215 r_extern = 0;
2216 r_index = N_ABS;
2219 MOVE_ADDRESS (GET_SWORD (abfd, bytes->r_addend));
2222 void
2223 NAME(aout,swap_std_reloc_in) (abfd, bytes, cache_ptr, symbols, symcount)
2224 bfd *abfd;
2225 struct reloc_std_external *bytes;
2226 arelent *cache_ptr;
2227 asymbol **symbols;
2228 bfd_size_type symcount;
2230 unsigned int r_index;
2231 int r_extern;
2232 unsigned int r_length;
2233 int r_pcrel;
2234 int r_baserel, r_jmptable, r_relative;
2235 struct aoutdata *su = &(abfd->tdata.aout_data->a);
2236 unsigned int howto_idx;
2238 cache_ptr->address = H_GET_32 (abfd, bytes->r_address);
2240 /* Now the fun stuff. */
2241 if (bfd_header_big_endian (abfd))
2243 r_index = (((unsigned int) bytes->r_index[0] << 16)
2244 | ((unsigned int) bytes->r_index[1] << 8)
2245 | bytes->r_index[2]);
2246 r_extern = (0 != (bytes->r_type[0] & RELOC_STD_BITS_EXTERN_BIG));
2247 r_pcrel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_PCREL_BIG));
2248 r_baserel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_BASEREL_BIG));
2249 r_jmptable= (0 != (bytes->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG));
2250 r_relative= (0 != (bytes->r_type[0] & RELOC_STD_BITS_RELATIVE_BIG));
2251 r_length = ((bytes->r_type[0] & RELOC_STD_BITS_LENGTH_BIG)
2252 >> RELOC_STD_BITS_LENGTH_SH_BIG);
2254 else
2256 r_index = (((unsigned int) bytes->r_index[2] << 16)
2257 | ((unsigned int) bytes->r_index[1] << 8)
2258 | bytes->r_index[0]);
2259 r_extern = (0 != (bytes->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE));
2260 r_pcrel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
2261 r_baserel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_BASEREL_LITTLE));
2262 r_jmptable= (0 != (bytes->r_type[0] & RELOC_STD_BITS_JMPTABLE_LITTLE));
2263 r_relative= (0 != (bytes->r_type[0] & RELOC_STD_BITS_RELATIVE_LITTLE));
2264 r_length = ((bytes->r_type[0] & RELOC_STD_BITS_LENGTH_LITTLE)
2265 >> RELOC_STD_BITS_LENGTH_SH_LITTLE);
2268 howto_idx = (r_length + 4 * r_pcrel + 8 * r_baserel
2269 + 16 * r_jmptable + 32 * r_relative);
2270 BFD_ASSERT (howto_idx < TABLE_SIZE (howto_table_std));
2271 cache_ptr->howto = howto_table_std + howto_idx;
2272 BFD_ASSERT (cache_ptr->howto->type != (unsigned int) -1);
2274 /* Base relative relocs are always against the symbol table,
2275 regardless of the setting of r_extern. r_extern just reflects
2276 whether the symbol the reloc is against is local or global. */
2277 if (r_baserel)
2278 r_extern = 1;
2280 if (r_extern && r_index > symcount)
2282 /* We could arrange to return an error, but it might be useful
2283 to see the file even if it is bad. */
2284 r_extern = 0;
2285 r_index = N_ABS;
2288 MOVE_ADDRESS (0);
2291 /* Read and swap the relocs for a section. */
2293 bfd_boolean
2294 NAME(aout,slurp_reloc_table) (abfd, asect, symbols)
2295 bfd *abfd;
2296 sec_ptr asect;
2297 asymbol **symbols;
2299 bfd_size_type count;
2300 bfd_size_type reloc_size;
2301 PTR relocs;
2302 arelent *reloc_cache;
2303 size_t each_size;
2304 unsigned int counter = 0;
2305 arelent *cache_ptr;
2306 bfd_size_type amt;
2308 if (asect->relocation)
2309 return TRUE;
2311 if (asect->flags & SEC_CONSTRUCTOR)
2312 return TRUE;
2314 if (asect == obj_datasec (abfd))
2315 reloc_size = exec_hdr (abfd)->a_drsize;
2316 else if (asect == obj_textsec (abfd))
2317 reloc_size = exec_hdr (abfd)->a_trsize;
2318 else if (asect == obj_bsssec (abfd))
2319 reloc_size = 0;
2320 else
2322 bfd_set_error (bfd_error_invalid_operation);
2323 return FALSE;
2326 if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0)
2327 return FALSE;
2329 each_size = obj_reloc_entry_size (abfd);
2331 count = reloc_size / each_size;
2333 amt = count * sizeof (arelent);
2334 reloc_cache = (arelent *) bfd_zmalloc (amt);
2335 if (reloc_cache == NULL && count != 0)
2336 return FALSE;
2338 relocs = bfd_malloc (reloc_size);
2339 if (relocs == NULL && reloc_size != 0)
2341 free (reloc_cache);
2342 return FALSE;
2345 if (bfd_bread (relocs, reloc_size, abfd) != reloc_size)
2347 free (relocs);
2348 free (reloc_cache);
2349 return FALSE;
2352 cache_ptr = reloc_cache;
2353 if (each_size == RELOC_EXT_SIZE)
2355 struct reloc_ext_external *rptr = (struct reloc_ext_external *) relocs;
2357 for (; counter < count; counter++, rptr++, cache_ptr++)
2358 MY_swap_ext_reloc_in (abfd, rptr, cache_ptr, symbols,
2359 (bfd_size_type) bfd_get_symcount (abfd));
2361 else
2363 struct reloc_std_external *rptr = (struct reloc_std_external *) relocs;
2365 for (; counter < count; counter++, rptr++, cache_ptr++)
2366 MY_swap_std_reloc_in (abfd, rptr, cache_ptr, symbols,
2367 (bfd_size_type) bfd_get_symcount (abfd));
2370 free (relocs);
2372 asect->relocation = reloc_cache;
2373 asect->reloc_count = cache_ptr - reloc_cache;
2375 return TRUE;
2378 /* Write out a relocation section into an object file. */
2380 bfd_boolean
2381 NAME(aout,squirt_out_relocs) (abfd, section)
2382 bfd *abfd;
2383 asection *section;
2385 arelent **generic;
2386 unsigned char *native, *natptr;
2387 size_t each_size;
2389 unsigned int count = section->reloc_count;
2390 bfd_size_type natsize;
2392 if (count == 0 || section->orelocation == NULL)
2393 return TRUE;
2395 each_size = obj_reloc_entry_size (abfd);
2396 natsize = (bfd_size_type) each_size * count;
2397 native = (unsigned char *) bfd_zalloc (abfd, natsize);
2398 if (!native)
2399 return FALSE;
2401 generic = section->orelocation;
2403 if (each_size == RELOC_EXT_SIZE)
2405 for (natptr = native;
2406 count != 0;
2407 --count, natptr += each_size, ++generic)
2408 MY_swap_ext_reloc_out (abfd, *generic,
2409 (struct reloc_ext_external *) natptr);
2411 else
2413 for (natptr = native;
2414 count != 0;
2415 --count, natptr += each_size, ++generic)
2416 MY_swap_std_reloc_out (abfd, *generic,
2417 (struct reloc_std_external *) natptr);
2420 if (bfd_bwrite ((PTR) native, natsize, abfd) != natsize)
2422 bfd_release (abfd, native);
2423 return FALSE;
2425 bfd_release (abfd, native);
2427 return TRUE;
2430 /* This is stupid. This function should be a boolean predicate. */
2432 long
2433 NAME(aout,canonicalize_reloc) (abfd, section, relptr, symbols)
2434 bfd *abfd;
2435 sec_ptr section;
2436 arelent **relptr;
2437 asymbol **symbols;
2439 arelent *tblptr = section->relocation;
2440 unsigned int count;
2442 if (section == obj_bsssec (abfd))
2444 *relptr = NULL;
2445 return 0;
2448 if (!(tblptr || NAME(aout,slurp_reloc_table) (abfd, section, symbols)))
2449 return -1;
2451 if (section->flags & SEC_CONSTRUCTOR)
2453 arelent_chain *chain = section->constructor_chain;
2454 for (count = 0; count < section->reloc_count; count ++)
2456 *relptr ++ = &chain->relent;
2457 chain = chain->next;
2460 else
2462 tblptr = section->relocation;
2464 for (count = 0; count++ < section->reloc_count; )
2466 *relptr++ = tblptr++;
2469 *relptr = 0;
2471 return section->reloc_count;
2474 long
2475 NAME(aout,get_reloc_upper_bound) (abfd, asect)
2476 bfd *abfd;
2477 sec_ptr asect;
2479 if (bfd_get_format (abfd) != bfd_object)
2481 bfd_set_error (bfd_error_invalid_operation);
2482 return -1;
2485 if (asect->flags & SEC_CONSTRUCTOR)
2486 return (sizeof (arelent *) * (asect->reloc_count+1));
2488 if (asect == obj_datasec (abfd))
2489 return (sizeof (arelent *)
2490 * ((exec_hdr (abfd)->a_drsize / obj_reloc_entry_size (abfd))
2491 + 1));
2493 if (asect == obj_textsec (abfd))
2494 return (sizeof (arelent *)
2495 * ((exec_hdr (abfd)->a_trsize / obj_reloc_entry_size (abfd))
2496 + 1));
2498 if (asect == obj_bsssec (abfd))
2499 return sizeof (arelent *);
2501 if (asect == obj_bsssec (abfd))
2502 return 0;
2504 bfd_set_error (bfd_error_invalid_operation);
2505 return -1;
2508 long
2509 NAME(aout,get_symtab_upper_bound) (abfd)
2510 bfd *abfd;
2512 if (!NAME(aout,slurp_symbol_table) (abfd))
2513 return -1;
2515 return (bfd_get_symcount (abfd)+1) * (sizeof (aout_symbol_type *));
2518 alent *
2519 NAME(aout,get_lineno) (ignore_abfd, ignore_symbol)
2520 bfd *ignore_abfd ATTRIBUTE_UNUSED;
2521 asymbol *ignore_symbol ATTRIBUTE_UNUSED;
2523 return (alent *)NULL;
2526 void
2527 NAME(aout,get_symbol_info) (ignore_abfd, symbol, ret)
2528 bfd *ignore_abfd ATTRIBUTE_UNUSED;
2529 asymbol *symbol;
2530 symbol_info *ret;
2532 bfd_symbol_info (symbol, ret);
2534 if (ret->type == '?')
2536 int type_code = aout_symbol (symbol)->type & 0xff;
2537 const char *stab_name = bfd_get_stab_name (type_code);
2538 static char buf[10];
2540 if (stab_name == NULL)
2542 sprintf (buf, "(%d)", type_code);
2543 stab_name = buf;
2545 ret->type = '-';
2546 ret->stab_type = type_code;
2547 ret->stab_other = (unsigned) (aout_symbol (symbol)->other & 0xff);
2548 ret->stab_desc = (unsigned) (aout_symbol (symbol)->desc & 0xffff);
2549 ret->stab_name = stab_name;
2553 void
2554 NAME(aout,print_symbol) (abfd, afile, symbol, how)
2555 bfd *abfd;
2556 PTR afile;
2557 asymbol *symbol;
2558 bfd_print_symbol_type how;
2560 FILE *file = (FILE *)afile;
2562 switch (how)
2564 case bfd_print_symbol_name:
2565 if (symbol->name)
2566 fprintf (file,"%s", symbol->name);
2567 break;
2568 case bfd_print_symbol_more:
2569 fprintf (file,"%4x %2x %2x",
2570 (unsigned) (aout_symbol (symbol)->desc & 0xffff),
2571 (unsigned) (aout_symbol (symbol)->other & 0xff),
2572 (unsigned) (aout_symbol (symbol)->type));
2573 break;
2574 case bfd_print_symbol_all:
2576 const char *section_name = symbol->section->name;
2578 bfd_print_symbol_vandf (abfd, (PTR)file, symbol);
2580 fprintf (file," %-5s %04x %02x %02x",
2581 section_name,
2582 (unsigned) (aout_symbol (symbol)->desc & 0xffff),
2583 (unsigned) (aout_symbol (symbol)->other & 0xff),
2584 (unsigned) (aout_symbol (symbol)->type & 0xff));
2585 if (symbol->name)
2586 fprintf (file," %s", symbol->name);
2588 break;
2592 /* If we don't have to allocate more than 1MB to hold the generic
2593 symbols, we use the generic minisymbol methord: it's faster, since
2594 it only translates the symbols once, not multiple times. */
2595 #define MINISYM_THRESHOLD (1000000 / sizeof (asymbol))
2597 /* Read minisymbols. For minisymbols, we use the unmodified a.out
2598 symbols. The minisymbol_to_symbol function translates these into
2599 BFD asymbol structures. */
2601 long
2602 NAME(aout,read_minisymbols) (abfd, dynamic, minisymsp, sizep)
2603 bfd *abfd;
2604 bfd_boolean dynamic;
2605 PTR *minisymsp;
2606 unsigned int *sizep;
2608 if (dynamic)
2610 /* We could handle the dynamic symbols here as well, but it's
2611 easier to hand them off. */
2612 return _bfd_generic_read_minisymbols (abfd, dynamic, minisymsp, sizep);
2615 if (! aout_get_external_symbols (abfd))
2616 return -1;
2618 if (obj_aout_external_sym_count (abfd) < MINISYM_THRESHOLD)
2619 return _bfd_generic_read_minisymbols (abfd, dynamic, minisymsp, sizep);
2621 *minisymsp = (PTR) obj_aout_external_syms (abfd);
2623 /* By passing the external symbols back from this routine, we are
2624 giving up control over the memory block. Clear
2625 obj_aout_external_syms, so that we do not try to free it
2626 ourselves. */
2627 obj_aout_external_syms (abfd) = NULL;
2629 *sizep = EXTERNAL_NLIST_SIZE;
2630 return obj_aout_external_sym_count (abfd);
2633 /* Convert a minisymbol to a BFD asymbol. A minisymbol is just an
2634 unmodified a.out symbol. The SYM argument is a structure returned
2635 by bfd_make_empty_symbol, which we fill in here. */
2637 asymbol *
2638 NAME(aout,minisymbol_to_symbol) (abfd, dynamic, minisym, sym)
2639 bfd *abfd;
2640 bfd_boolean dynamic;
2641 const PTR minisym;
2642 asymbol *sym;
2644 if (dynamic
2645 || obj_aout_external_sym_count (abfd) < MINISYM_THRESHOLD)
2646 return _bfd_generic_minisymbol_to_symbol (abfd, dynamic, minisym, sym);
2648 memset (sym, 0, sizeof (aout_symbol_type));
2650 /* We call translate_symbol_table to translate a single symbol. */
2651 if (! (NAME(aout,translate_symbol_table)
2652 (abfd,
2653 (aout_symbol_type *) sym,
2654 (struct external_nlist *) minisym,
2655 (bfd_size_type) 1,
2656 obj_aout_external_strings (abfd),
2657 obj_aout_external_string_size (abfd),
2658 FALSE)))
2659 return NULL;
2661 return sym;
2664 /* Provided a BFD, a section and an offset into the section, calculate
2665 and return the name of the source file and the line nearest to the
2666 wanted location. */
2668 bfd_boolean
2669 NAME(aout,find_nearest_line)
2670 (abfd, section, symbols, offset, filename_ptr, functionname_ptr, line_ptr)
2671 bfd *abfd;
2672 asection *section;
2673 asymbol **symbols;
2674 bfd_vma offset;
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. */
2680 asymbol **p;
2681 const char *directory_name = NULL;
2682 const char *main_file_name = NULL;
2683 const char *current_file_name = NULL;
2684 const char *line_file_name = NULL; /* Value of current_file_name at line number. */
2685 const char *line_directory_name = NULL; /* Value of directory_name at line number. */
2686 bfd_vma low_line_vma = 0;
2687 bfd_vma low_func_vma = 0;
2688 asymbol *func = 0;
2689 bfd_size_type filelen, funclen;
2690 char *buf;
2692 *filename_ptr = abfd->filename;
2693 *functionname_ptr = 0;
2694 *line_ptr = 0;
2696 if (symbols != (asymbol **)NULL)
2698 for (p = symbols; *p; p++)
2700 aout_symbol_type *q = (aout_symbol_type *) (*p);
2701 next:
2702 switch (q->type)
2704 case N_TEXT:
2705 /* If this looks like a file name symbol, and it comes after
2706 the line number we have found so far, but before the
2707 offset, then we have probably not found the right line
2708 number. */
2709 if (q->symbol.value <= offset
2710 && ((q->symbol.value > low_line_vma
2711 && (line_file_name != NULL
2712 || *line_ptr != 0))
2713 || (q->symbol.value > low_func_vma
2714 && func != NULL)))
2716 const char *symname;
2718 symname = q->symbol.name;
2719 if (strcmp (symname + strlen (symname) - 2, ".o") == 0)
2721 if (q->symbol.value > low_line_vma)
2723 *line_ptr = 0;
2724 line_file_name = NULL;
2726 if (q->symbol.value > low_func_vma)
2727 func = NULL;
2730 break;
2732 case N_SO:
2733 /* If this symbol is less than the offset, but greater than
2734 the line number we have found so far, then we have not
2735 found the right line number. */
2736 if (q->symbol.value <= offset)
2738 if (q->symbol.value > low_line_vma)
2740 *line_ptr = 0;
2741 line_file_name = NULL;
2743 if (q->symbol.value > low_func_vma)
2744 func = NULL;
2747 main_file_name = current_file_name = q->symbol.name;
2748 /* Look ahead to next symbol to check if that too is an N_SO. */
2749 p++;
2750 if (*p == NULL)
2751 goto done;
2752 q = (aout_symbol_type *) (*p);
2753 if (q->type != (int)N_SO)
2754 goto next;
2756 /* Found a second N_SO First is directory; second is filename. */
2757 directory_name = current_file_name;
2758 main_file_name = current_file_name = q->symbol.name;
2759 if (obj_textsec (abfd) != section)
2760 goto done;
2761 break;
2762 case N_SOL:
2763 current_file_name = q->symbol.name;
2764 break;
2766 case N_SLINE:
2768 case N_DSLINE:
2769 case N_BSLINE:
2770 /* We'll keep this if it resolves nearer than the one we have
2771 already. */
2772 if (q->symbol.value >= low_line_vma
2773 && q->symbol.value <= offset)
2775 *line_ptr = q->desc;
2776 low_line_vma = q->symbol.value;
2777 line_file_name = current_file_name;
2778 line_directory_name = directory_name;
2780 break;
2781 case N_FUN:
2783 /* We'll keep this if it is nearer than the one we have already. */
2784 if (q->symbol.value >= low_func_vma &&
2785 q->symbol.value <= offset)
2787 low_func_vma = q->symbol.value;
2788 func = (asymbol *)q;
2790 else if (q->symbol.value > offset)
2791 goto done;
2793 break;
2798 done:
2799 if (*line_ptr != 0)
2801 main_file_name = line_file_name;
2802 directory_name = line_directory_name;
2805 if (main_file_name == NULL
2806 || IS_ABSOLUTE_PATH (main_file_name)
2807 || directory_name == NULL)
2808 filelen = 0;
2809 else
2810 filelen = strlen (directory_name) + strlen (main_file_name);
2812 if (func == NULL)
2813 funclen = 0;
2814 else
2815 funclen = strlen (bfd_asymbol_name (func));
2817 if (adata (abfd).line_buf != NULL)
2818 free (adata (abfd).line_buf);
2820 if (filelen + funclen == 0)
2821 adata (abfd).line_buf = buf = NULL;
2822 else
2824 buf = (char *) bfd_malloc (filelen + funclen + 3);
2825 adata (abfd).line_buf = buf;
2826 if (buf == NULL)
2827 return FALSE;
2830 if (main_file_name != NULL)
2832 if (IS_ABSOLUTE_PATH (main_file_name) || directory_name == NULL)
2833 *filename_ptr = main_file_name;
2834 else
2836 sprintf (buf, "%s%s", directory_name, main_file_name);
2837 *filename_ptr = buf;
2838 buf += filelen + 1;
2842 if (func)
2844 const char *function = func->name;
2845 char *colon;
2847 /* The caller expects a symbol name. We actually have a
2848 function name, without the leading underscore. Put the
2849 underscore back in, so that the caller gets a symbol name. */
2850 if (bfd_get_symbol_leading_char (abfd) == '\0')
2851 strcpy (buf, function);
2852 else
2854 buf[0] = bfd_get_symbol_leading_char (abfd);
2855 strcpy (buf + 1, function);
2857 /* Have to remove : stuff. */
2858 colon = strchr (buf, ':');
2859 if (colon != NULL)
2860 *colon = '\0';
2861 *functionname_ptr = buf;
2864 return TRUE;
2868 NAME(aout,sizeof_headers) (abfd, execable)
2869 bfd *abfd;
2870 bfd_boolean execable ATTRIBUTE_UNUSED;
2872 return adata (abfd).exec_bytes_size;
2875 /* Free all information we have cached for this BFD. We can always
2876 read it again later if we need it. */
2878 bfd_boolean
2879 NAME(aout,bfd_free_cached_info) (abfd)
2880 bfd *abfd;
2882 asection *o;
2884 if (bfd_get_format (abfd) != bfd_object
2885 || abfd->tdata.aout_data == NULL)
2886 return TRUE;
2888 #define BFCI_FREE(x) if (x != NULL) { free (x); x = NULL; }
2889 BFCI_FREE (obj_aout_symbols (abfd));
2890 #ifdef USE_MMAP
2891 obj_aout_external_syms (abfd) = 0;
2892 bfd_free_window (&obj_aout_sym_window (abfd));
2893 bfd_free_window (&obj_aout_string_window (abfd));
2894 obj_aout_external_strings (abfd) = 0;
2895 #else
2896 BFCI_FREE (obj_aout_external_syms (abfd));
2897 BFCI_FREE (obj_aout_external_strings (abfd));
2898 #endif
2899 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
2900 BFCI_FREE (o->relocation);
2901 #undef BFCI_FREE
2903 return TRUE;
2906 /* a.out link code. */
2908 static bfd_boolean aout_link_add_object_symbols
2909 PARAMS ((bfd *, struct bfd_link_info *));
2910 static bfd_boolean aout_link_check_archive_element
2911 PARAMS ((bfd *, struct bfd_link_info *, bfd_boolean *));
2912 static bfd_boolean aout_link_free_symbols
2913 PARAMS ((bfd *));
2914 static bfd_boolean aout_link_check_ar_symbols
2915 PARAMS ((bfd *, struct bfd_link_info *, bfd_boolean *pneeded));
2916 static bfd_boolean aout_link_add_symbols
2917 PARAMS ((bfd *, struct bfd_link_info *));
2919 /* Routine to create an entry in an a.out link hash table. */
2921 struct bfd_hash_entry *
2922 NAME(aout,link_hash_newfunc) (entry, table, string)
2923 struct bfd_hash_entry *entry;
2924 struct bfd_hash_table *table;
2925 const char *string;
2927 struct aout_link_hash_entry *ret = (struct aout_link_hash_entry *) entry;
2929 /* Allocate the structure if it has not already been allocated by a
2930 subclass. */
2931 if (ret == (struct aout_link_hash_entry *) NULL)
2932 ret = ((struct aout_link_hash_entry *)
2933 bfd_hash_allocate (table, sizeof (struct aout_link_hash_entry)));
2934 if (ret == (struct aout_link_hash_entry *) NULL)
2935 return (struct bfd_hash_entry *) ret;
2937 /* Call the allocation method of the superclass. */
2938 ret = ((struct aout_link_hash_entry *)
2939 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2940 table, string));
2941 if (ret)
2943 /* Set local fields. */
2944 ret->written = FALSE;
2945 ret->indx = -1;
2948 return (struct bfd_hash_entry *) ret;
2951 /* Initialize an a.out link hash table. */
2953 bfd_boolean
2954 NAME(aout,link_hash_table_init) (table, abfd, newfunc)
2955 struct aout_link_hash_table *table;
2956 bfd *abfd;
2957 struct bfd_hash_entry *(*newfunc)
2958 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *,
2959 const char *));
2961 return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
2964 /* Create an a.out link hash table. */
2966 struct bfd_link_hash_table *
2967 NAME(aout,link_hash_table_create) (abfd)
2968 bfd *abfd;
2970 struct aout_link_hash_table *ret;
2971 bfd_size_type amt = sizeof (struct aout_link_hash_table);
2973 ret = (struct aout_link_hash_table *) bfd_malloc (amt);
2974 if (ret == NULL)
2975 return (struct bfd_link_hash_table *) NULL;
2977 if (! NAME(aout,link_hash_table_init) (ret, abfd,
2978 NAME(aout,link_hash_newfunc)))
2980 free (ret);
2981 return (struct bfd_link_hash_table *) NULL;
2983 return &ret->root;
2986 /* Given an a.out BFD, add symbols to the global hash table as
2987 appropriate. */
2989 bfd_boolean
2990 NAME(aout,link_add_symbols) (abfd, info)
2991 bfd *abfd;
2992 struct bfd_link_info *info;
2994 switch (bfd_get_format (abfd))
2996 case bfd_object:
2997 return aout_link_add_object_symbols (abfd, info);
2998 case bfd_archive:
2999 return _bfd_generic_link_add_archive_symbols
3000 (abfd, info, aout_link_check_archive_element);
3001 default:
3002 bfd_set_error (bfd_error_wrong_format);
3003 return FALSE;
3007 /* Add symbols from an a.out object file. */
3009 static bfd_boolean
3010 aout_link_add_object_symbols (abfd, info)
3011 bfd *abfd;
3012 struct bfd_link_info *info;
3014 if (! aout_get_external_symbols (abfd))
3015 return FALSE;
3016 if (! aout_link_add_symbols (abfd, info))
3017 return FALSE;
3018 if (! info->keep_memory)
3020 if (! aout_link_free_symbols (abfd))
3021 return FALSE;
3023 return TRUE;
3026 /* Check a single archive element to see if we need to include it in
3027 the link. *PNEEDED is set according to whether this element is
3028 needed in the link or not. This is called from
3029 _bfd_generic_link_add_archive_symbols. */
3031 static bfd_boolean
3032 aout_link_check_archive_element (abfd, info, pneeded)
3033 bfd *abfd;
3034 struct bfd_link_info *info;
3035 bfd_boolean *pneeded;
3037 if (! aout_get_external_symbols (abfd))
3038 return FALSE;
3040 if (! aout_link_check_ar_symbols (abfd, info, pneeded))
3041 return FALSE;
3043 if (*pneeded)
3045 if (! aout_link_add_symbols (abfd, info))
3046 return FALSE;
3049 if (! info->keep_memory || ! *pneeded)
3051 if (! aout_link_free_symbols (abfd))
3052 return FALSE;
3055 return TRUE;
3058 /* Free up the internal symbols read from an a.out file. */
3060 static bfd_boolean
3061 aout_link_free_symbols (abfd)
3062 bfd *abfd;
3064 if (obj_aout_external_syms (abfd) != (struct external_nlist *) NULL)
3066 #ifdef USE_MMAP
3067 bfd_free_window (&obj_aout_sym_window (abfd));
3068 #else
3069 free ((PTR) obj_aout_external_syms (abfd));
3070 #endif
3071 obj_aout_external_syms (abfd) = (struct external_nlist *) NULL;
3073 if (obj_aout_external_strings (abfd) != (char *) NULL)
3075 #ifdef USE_MMAP
3076 bfd_free_window (&obj_aout_string_window (abfd));
3077 #else
3078 free ((PTR) obj_aout_external_strings (abfd));
3079 #endif
3080 obj_aout_external_strings (abfd) = (char *) NULL;
3082 return TRUE;
3085 /* Look through the internal symbols to see if this object file should
3086 be included in the link. We should include this object file if it
3087 defines any symbols which are currently undefined. If this object
3088 file defines a common symbol, then we may adjust the size of the
3089 known symbol but we do not include the object file in the link
3090 (unless there is some other reason to include it). */
3092 static bfd_boolean
3093 aout_link_check_ar_symbols (abfd, info, pneeded)
3094 bfd *abfd;
3095 struct bfd_link_info *info;
3096 bfd_boolean *pneeded;
3098 register struct external_nlist *p;
3099 struct external_nlist *pend;
3100 char *strings;
3102 *pneeded = FALSE;
3104 /* Look through all the symbols. */
3105 p = obj_aout_external_syms (abfd);
3106 pend = p + obj_aout_external_sym_count (abfd);
3107 strings = obj_aout_external_strings (abfd);
3108 for (; p < pend; p++)
3110 int type = H_GET_8 (abfd, p->e_type);
3111 const char *name;
3112 struct bfd_link_hash_entry *h;
3114 /* Ignore symbols that are not externally visible. This is an
3115 optimization only, as we check the type more thoroughly
3116 below. */
3117 if (((type & N_EXT) == 0
3118 || (type & N_STAB) != 0
3119 || type == N_FN)
3120 && type != N_WEAKA
3121 && type != N_WEAKT
3122 && type != N_WEAKD
3123 && type != N_WEAKB)
3125 if (type == N_WARNING
3126 || type == N_INDR)
3127 ++p;
3128 continue;
3131 name = strings + GET_WORD (abfd, p->e_strx);
3132 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
3134 /* We are only interested in symbols that are currently
3135 undefined or common. */
3136 if (h == (struct bfd_link_hash_entry *) NULL
3137 || (h->type != bfd_link_hash_undefined
3138 && h->type != bfd_link_hash_common))
3140 if (type == (N_INDR | N_EXT))
3141 ++p;
3142 continue;
3145 if (type == (N_TEXT | N_EXT)
3146 || type == (N_DATA | N_EXT)
3147 || type == (N_BSS | N_EXT)
3148 || type == (N_ABS | N_EXT)
3149 || type == (N_INDR | N_EXT))
3151 /* This object file defines this symbol. We must link it
3152 in. This is true regardless of whether the current
3153 definition of the symbol is undefined or common.
3155 If the current definition is common, we have a case in
3156 which we have already seen an object file including:
3157 int a;
3158 and this object file from the archive includes:
3159 int a = 5;
3160 In such a case, whether to include this object is target
3161 dependant for backward compatibility.
3163 FIXME: The SunOS 4.1.3 linker will pull in the archive
3164 element if the symbol is defined in the .data section,
3165 but not if it is defined in the .text section. That
3166 seems a bit crazy to me, and it has not been implemented
3167 yet. However, it might be correct. */
3168 if (h->type == bfd_link_hash_common)
3170 int skip = 0;
3172 switch (info->common_skip_ar_aymbols)
3174 case bfd_link_common_skip_text:
3175 skip = (type == (N_TEXT | N_EXT));
3176 break;
3177 case bfd_link_common_skip_data:
3178 skip = (type == (N_DATA | N_EXT));
3179 break;
3180 default:
3181 case bfd_link_common_skip_all:
3182 skip = 1;
3183 break;
3186 if (skip)
3187 continue;
3190 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
3191 return FALSE;
3192 *pneeded = TRUE;
3193 return TRUE;
3196 if (type == (N_UNDF | N_EXT))
3198 bfd_vma value;
3200 value = GET_WORD (abfd, p->e_value);
3201 if (value != 0)
3203 /* This symbol is common in the object from the archive
3204 file. */
3205 if (h->type == bfd_link_hash_undefined)
3207 bfd *symbfd;
3208 unsigned int power;
3210 symbfd = h->u.undef.abfd;
3211 if (symbfd == (bfd *) NULL)
3213 /* This symbol was created as undefined from
3214 outside BFD. We assume that we should link
3215 in the object file. This is done for the -u
3216 option in the linker. */
3217 if (! (*info->callbacks->add_archive_element) (info,
3218 abfd,
3219 name))
3220 return FALSE;
3221 *pneeded = TRUE;
3222 return TRUE;
3224 /* Turn the current link symbol into a common
3225 symbol. It is already on the undefs list. */
3226 h->type = bfd_link_hash_common;
3227 h->u.c.p = ((struct bfd_link_hash_common_entry *)
3228 bfd_hash_allocate (&info->hash->table,
3229 sizeof (struct bfd_link_hash_common_entry)));
3230 if (h->u.c.p == NULL)
3231 return FALSE;
3233 h->u.c.size = value;
3235 /* FIXME: This isn't quite right. The maximum
3236 alignment of a common symbol should be set by the
3237 architecture of the output file, not of the input
3238 file. */
3239 power = bfd_log2 (value);
3240 if (power > bfd_get_arch_info (abfd)->section_align_power)
3241 power = bfd_get_arch_info (abfd)->section_align_power;
3242 h->u.c.p->alignment_power = power;
3244 h->u.c.p->section = bfd_make_section_old_way (symbfd,
3245 "COMMON");
3247 else
3249 /* Adjust the size of the common symbol if
3250 necessary. */
3251 if (value > h->u.c.size)
3252 h->u.c.size = value;
3257 if (type == N_WEAKA
3258 || type == N_WEAKT
3259 || type == N_WEAKD
3260 || type == N_WEAKB)
3262 /* This symbol is weak but defined. We must pull it in if
3263 the current link symbol is undefined, but we don't want
3264 it if the current link symbol is common. */
3265 if (h->type == bfd_link_hash_undefined)
3267 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
3268 return FALSE;
3269 *pneeded = TRUE;
3270 return TRUE;
3275 /* We do not need this object file. */
3276 return TRUE;
3279 /* Add all symbols from an object file to the hash table. */
3281 static bfd_boolean
3282 aout_link_add_symbols (abfd, info)
3283 bfd *abfd;
3284 struct bfd_link_info *info;
3286 bfd_boolean (*add_one_symbol)
3287 PARAMS ((struct bfd_link_info *, bfd *, const char *, flagword, asection *,
3288 bfd_vma, const char *, bfd_boolean, bfd_boolean,
3289 struct bfd_link_hash_entry **));
3290 struct external_nlist *syms;
3291 bfd_size_type sym_count;
3292 char *strings;
3293 bfd_boolean copy;
3294 struct aout_link_hash_entry **sym_hash;
3295 register struct external_nlist *p;
3296 struct external_nlist *pend;
3297 bfd_size_type amt;
3299 syms = obj_aout_external_syms (abfd);
3300 sym_count = obj_aout_external_sym_count (abfd);
3301 strings = obj_aout_external_strings (abfd);
3302 if (info->keep_memory)
3303 copy = FALSE;
3304 else
3305 copy = TRUE;
3307 if (aout_backend_info (abfd)->add_dynamic_symbols != NULL)
3309 if (! ((*aout_backend_info (abfd)->add_dynamic_symbols)
3310 (abfd, info, &syms, &sym_count, &strings)))
3311 return FALSE;
3314 /* We keep a list of the linker hash table entries that correspond
3315 to particular symbols. We could just look them up in the hash
3316 table, but keeping the list is more efficient. Perhaps this
3317 should be conditional on info->keep_memory. */
3318 amt = sym_count * sizeof (struct aout_link_hash_entry *);
3319 sym_hash = (struct aout_link_hash_entry **) bfd_alloc (abfd, amt);
3320 if (sym_hash == NULL && sym_count != 0)
3321 return FALSE;
3322 obj_aout_sym_hashes (abfd) = sym_hash;
3324 add_one_symbol = aout_backend_info (abfd)->add_one_symbol;
3325 if (add_one_symbol == NULL)
3326 add_one_symbol = _bfd_generic_link_add_one_symbol;
3328 p = syms;
3329 pend = p + sym_count;
3330 for (; p < pend; p++, sym_hash++)
3332 int type;
3333 const char *name;
3334 bfd_vma value;
3335 asection *section;
3336 flagword flags;
3337 const char *string;
3339 *sym_hash = NULL;
3341 type = H_GET_8 (abfd, p->e_type);
3343 /* Ignore debugging symbols. */
3344 if ((type & N_STAB) != 0)
3345 continue;
3347 name = strings + GET_WORD (abfd, p->e_strx);
3348 value = GET_WORD (abfd, p->e_value);
3349 flags = BSF_GLOBAL;
3350 string = NULL;
3351 switch (type)
3353 default:
3354 abort ();
3356 case N_UNDF:
3357 case N_ABS:
3358 case N_TEXT:
3359 case N_DATA:
3360 case N_BSS:
3361 case N_FN_SEQ:
3362 case N_COMM:
3363 case N_SETV:
3364 case N_FN:
3365 /* Ignore symbols that are not externally visible. */
3366 continue;
3367 case N_INDR:
3368 /* Ignore local indirect symbol. */
3369 ++p;
3370 ++sym_hash;
3371 continue;
3373 case N_UNDF | N_EXT:
3374 if (value == 0)
3376 section = bfd_und_section_ptr;
3377 flags = 0;
3379 else
3380 section = bfd_com_section_ptr;
3381 break;
3382 case N_ABS | N_EXT:
3383 section = bfd_abs_section_ptr;
3384 break;
3385 case N_TEXT | N_EXT:
3386 section = obj_textsec (abfd);
3387 value -= bfd_get_section_vma (abfd, section);
3388 break;
3389 case N_DATA | N_EXT:
3390 case N_SETV | N_EXT:
3391 /* Treat N_SETV symbols as N_DATA symbol; see comment in
3392 translate_from_native_sym_flags. */
3393 section = obj_datasec (abfd);
3394 value -= bfd_get_section_vma (abfd, section);
3395 break;
3396 case N_BSS | N_EXT:
3397 section = obj_bsssec (abfd);
3398 value -= bfd_get_section_vma (abfd, section);
3399 break;
3400 case N_INDR | N_EXT:
3401 /* An indirect symbol. The next symbol is the symbol
3402 which this one really is. */
3403 BFD_ASSERT (p + 1 < pend);
3404 ++p;
3405 string = strings + GET_WORD (abfd, p->e_strx);
3406 section = bfd_ind_section_ptr;
3407 flags |= BSF_INDIRECT;
3408 break;
3409 case N_COMM | N_EXT:
3410 section = bfd_com_section_ptr;
3411 break;
3412 case N_SETA: case N_SETA | N_EXT:
3413 section = bfd_abs_section_ptr;
3414 flags |= BSF_CONSTRUCTOR;
3415 break;
3416 case N_SETT: case N_SETT | N_EXT:
3417 section = obj_textsec (abfd);
3418 flags |= BSF_CONSTRUCTOR;
3419 value -= bfd_get_section_vma (abfd, section);
3420 break;
3421 case N_SETD: case N_SETD | N_EXT:
3422 section = obj_datasec (abfd);
3423 flags |= BSF_CONSTRUCTOR;
3424 value -= bfd_get_section_vma (abfd, section);
3425 break;
3426 case N_SETB: case N_SETB | N_EXT:
3427 section = obj_bsssec (abfd);
3428 flags |= BSF_CONSTRUCTOR;
3429 value -= bfd_get_section_vma (abfd, section);
3430 break;
3431 case N_WARNING:
3432 /* A warning symbol. The next symbol is the one to warn
3433 about. If there is no next symbol, just look away. */
3434 if (p + 1 >= pend)
3435 return TRUE;
3436 ++p;
3437 string = name;
3438 name = strings + GET_WORD (abfd, p->e_strx);
3439 section = bfd_und_section_ptr;
3440 flags |= BSF_WARNING;
3441 break;
3442 case N_WEAKU:
3443 section = bfd_und_section_ptr;
3444 flags = BSF_WEAK;
3445 break;
3446 case N_WEAKA:
3447 section = bfd_abs_section_ptr;
3448 flags = BSF_WEAK;
3449 break;
3450 case N_WEAKT:
3451 section = obj_textsec (abfd);
3452 value -= bfd_get_section_vma (abfd, section);
3453 flags = BSF_WEAK;
3454 break;
3455 case N_WEAKD:
3456 section = obj_datasec (abfd);
3457 value -= bfd_get_section_vma (abfd, section);
3458 flags = BSF_WEAK;
3459 break;
3460 case N_WEAKB:
3461 section = obj_bsssec (abfd);
3462 value -= bfd_get_section_vma (abfd, section);
3463 flags = BSF_WEAK;
3464 break;
3467 if (! ((*add_one_symbol)
3468 (info, abfd, name, flags, section, value, string, copy, FALSE,
3469 (struct bfd_link_hash_entry **) sym_hash)))
3470 return FALSE;
3472 /* Restrict the maximum alignment of a common symbol based on
3473 the architecture, since a.out has no way to represent
3474 alignment requirements of a section in a .o file. FIXME:
3475 This isn't quite right: it should use the architecture of the
3476 output file, not the input files. */
3477 if ((*sym_hash)->root.type == bfd_link_hash_common
3478 && ((*sym_hash)->root.u.c.p->alignment_power >
3479 bfd_get_arch_info (abfd)->section_align_power))
3480 (*sym_hash)->root.u.c.p->alignment_power =
3481 bfd_get_arch_info (abfd)->section_align_power;
3483 /* If this is a set symbol, and we are not building sets, then
3484 it is possible for the hash entry to not have been set. In
3485 such a case, treat the symbol as not globally defined. */
3486 if ((*sym_hash)->root.type == bfd_link_hash_new)
3488 BFD_ASSERT ((flags & BSF_CONSTRUCTOR) != 0);
3489 *sym_hash = NULL;
3492 if (type == (N_INDR | N_EXT) || type == N_WARNING)
3493 ++sym_hash;
3496 return TRUE;
3499 /* A hash table used for header files with N_BINCL entries. */
3501 struct aout_link_includes_table
3503 struct bfd_hash_table root;
3506 /* A linked list of totals that we have found for a particular header
3507 file. */
3509 struct aout_link_includes_totals
3511 struct aout_link_includes_totals *next;
3512 bfd_vma total;
3515 /* An entry in the header file hash table. */
3517 struct aout_link_includes_entry
3519 struct bfd_hash_entry root;
3520 /* List of totals we have found for this file. */
3521 struct aout_link_includes_totals *totals;
3524 /* Look up an entry in an the header file hash table. */
3526 #define aout_link_includes_lookup(table, string, create, copy) \
3527 ((struct aout_link_includes_entry *) \
3528 bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
3530 /* During the final link step we need to pass around a bunch of
3531 information, so we do it in an instance of this structure. */
3533 struct aout_final_link_info
3535 /* General link information. */
3536 struct bfd_link_info *info;
3537 /* Output bfd. */
3538 bfd *output_bfd;
3539 /* Reloc file positions. */
3540 file_ptr treloff, dreloff;
3541 /* File position of symbols. */
3542 file_ptr symoff;
3543 /* String table. */
3544 struct bfd_strtab_hash *strtab;
3545 /* Header file hash table. */
3546 struct aout_link_includes_table includes;
3547 /* A buffer large enough to hold the contents of any section. */
3548 bfd_byte *contents;
3549 /* A buffer large enough to hold the relocs of any section. */
3550 PTR relocs;
3551 /* A buffer large enough to hold the symbol map of any input BFD. */
3552 int *symbol_map;
3553 /* A buffer large enough to hold output symbols of any input BFD. */
3554 struct external_nlist *output_syms;
3557 static struct bfd_hash_entry *aout_link_includes_newfunc
3558 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
3559 static bfd_boolean aout_link_input_bfd
3560 PARAMS ((struct aout_final_link_info *, bfd *input_bfd));
3561 static bfd_boolean aout_link_write_symbols
3562 PARAMS ((struct aout_final_link_info *, bfd *input_bfd));
3563 static bfd_boolean aout_link_write_other_symbol
3564 PARAMS ((struct aout_link_hash_entry *, PTR));
3565 static bfd_boolean aout_link_input_section
3566 PARAMS ((struct aout_final_link_info *, bfd *input_bfd,
3567 asection *input_section, file_ptr *reloff_ptr,
3568 bfd_size_type rel_size));
3569 static bfd_boolean aout_link_input_section_std
3570 PARAMS ((struct aout_final_link_info *, bfd *input_bfd,
3571 asection *input_section, struct reloc_std_external *,
3572 bfd_size_type rel_size, bfd_byte *contents));
3573 static bfd_boolean aout_link_input_section_ext
3574 PARAMS ((struct aout_final_link_info *, bfd *input_bfd,
3575 asection *input_section, struct reloc_ext_external *,
3576 bfd_size_type rel_size, bfd_byte *contents));
3577 static INLINE asection *aout_reloc_index_to_section
3578 PARAMS ((bfd *, int));
3579 static bfd_boolean aout_link_reloc_link_order
3580 PARAMS ((struct aout_final_link_info *, asection *,
3581 struct bfd_link_order *));
3583 /* The function to create a new entry in the header file hash table. */
3585 static struct bfd_hash_entry *
3586 aout_link_includes_newfunc (entry, table, string)
3587 struct bfd_hash_entry *entry;
3588 struct bfd_hash_table *table;
3589 const char *string;
3591 struct aout_link_includes_entry *ret =
3592 (struct aout_link_includes_entry *) entry;
3594 /* Allocate the structure if it has not already been allocated by a
3595 subclass. */
3596 if (ret == (struct aout_link_includes_entry *) NULL)
3597 ret = ((struct aout_link_includes_entry *)
3598 bfd_hash_allocate (table,
3599 sizeof (struct aout_link_includes_entry)));
3600 if (ret == (struct aout_link_includes_entry *) NULL)
3601 return (struct bfd_hash_entry *) ret;
3603 /* Call the allocation method of the superclass. */
3604 ret = ((struct aout_link_includes_entry *)
3605 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
3606 if (ret)
3608 /* Set local fields. */
3609 ret->totals = NULL;
3612 return (struct bfd_hash_entry *) ret;
3615 /* Do the final link step. This is called on the output BFD. The
3616 INFO structure should point to a list of BFDs linked through the
3617 link_next field which can be used to find each BFD which takes part
3618 in the output. Also, each section in ABFD should point to a list
3619 of bfd_link_order structures which list all the input sections for
3620 the output section. */
3622 bfd_boolean
3623 NAME(aout,final_link) (abfd, info, callback)
3624 bfd *abfd;
3625 struct bfd_link_info *info;
3626 void (*callback) PARAMS ((bfd *, file_ptr *, file_ptr *, file_ptr *));
3628 struct aout_final_link_info aout_info;
3629 bfd_boolean includes_hash_initialized = FALSE;
3630 register bfd *sub;
3631 bfd_size_type trsize, drsize;
3632 bfd_size_type max_contents_size;
3633 bfd_size_type max_relocs_size;
3634 bfd_size_type max_sym_count;
3635 bfd_size_type text_size;
3636 file_ptr text_end;
3637 register struct bfd_link_order *p;
3638 asection *o;
3639 bfd_boolean have_link_order_relocs;
3641 if (info->shared)
3642 abfd->flags |= DYNAMIC;
3644 aout_info.info = info;
3645 aout_info.output_bfd = abfd;
3646 aout_info.contents = NULL;
3647 aout_info.relocs = NULL;
3648 aout_info.symbol_map = NULL;
3649 aout_info.output_syms = NULL;
3651 if (! bfd_hash_table_init_n (&aout_info.includes.root,
3652 aout_link_includes_newfunc,
3653 251))
3654 goto error_return;
3655 includes_hash_initialized = TRUE;
3657 /* Figure out the largest section size. Also, if generating
3658 relocatable output, count the relocs. */
3659 trsize = 0;
3660 drsize = 0;
3661 max_contents_size = 0;
3662 max_relocs_size = 0;
3663 max_sym_count = 0;
3664 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3666 bfd_size_type sz;
3668 if (info->relocatable)
3670 if (bfd_get_flavour (sub) == bfd_target_aout_flavour)
3672 trsize += exec_hdr (sub)->a_trsize;
3673 drsize += exec_hdr (sub)->a_drsize;
3675 else
3677 /* FIXME: We need to identify the .text and .data sections
3678 and call get_reloc_upper_bound and canonicalize_reloc to
3679 work out the number of relocs needed, and then multiply
3680 by the reloc size. */
3681 (*_bfd_error_handler)
3682 (_("%s: relocatable link from %s to %s not supported"),
3683 bfd_get_filename (abfd),
3684 sub->xvec->name, abfd->xvec->name);
3685 bfd_set_error (bfd_error_invalid_operation);
3686 goto error_return;
3690 if (bfd_get_flavour (sub) == bfd_target_aout_flavour)
3692 sz = obj_textsec (sub)->size;
3693 if (sz > max_contents_size)
3694 max_contents_size = sz;
3695 sz = obj_datasec (sub)->size;
3696 if (sz > max_contents_size)
3697 max_contents_size = sz;
3699 sz = exec_hdr (sub)->a_trsize;
3700 if (sz > max_relocs_size)
3701 max_relocs_size = sz;
3702 sz = exec_hdr (sub)->a_drsize;
3703 if (sz > max_relocs_size)
3704 max_relocs_size = sz;
3706 sz = obj_aout_external_sym_count (sub);
3707 if (sz > max_sym_count)
3708 max_sym_count = sz;
3712 if (info->relocatable)
3714 if (obj_textsec (abfd) != (asection *) NULL)
3715 trsize += (_bfd_count_link_order_relocs (obj_textsec (abfd)
3716 ->link_order_head)
3717 * obj_reloc_entry_size (abfd));
3718 if (obj_datasec (abfd) != (asection *) NULL)
3719 drsize += (_bfd_count_link_order_relocs (obj_datasec (abfd)
3720 ->link_order_head)
3721 * obj_reloc_entry_size (abfd));
3724 exec_hdr (abfd)->a_trsize = trsize;
3725 exec_hdr (abfd)->a_drsize = drsize;
3727 exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd);
3729 /* Adjust the section sizes and vmas according to the magic number.
3730 This sets a_text, a_data and a_bss in the exec_hdr and sets the
3731 filepos for each section. */
3732 if (! NAME(aout,adjust_sizes_and_vmas) (abfd, &text_size, &text_end))
3733 goto error_return;
3735 /* The relocation and symbol file positions differ among a.out
3736 targets. We are passed a callback routine from the backend
3737 specific code to handle this.
3738 FIXME: At this point we do not know how much space the symbol
3739 table will require. This will not work for any (nonstandard)
3740 a.out target that needs to know the symbol table size before it
3741 can compute the relocation file positions. This may or may not
3742 be the case for the hp300hpux target, for example. */
3743 (*callback) (abfd, &aout_info.treloff, &aout_info.dreloff,
3744 &aout_info.symoff);
3745 obj_textsec (abfd)->rel_filepos = aout_info.treloff;
3746 obj_datasec (abfd)->rel_filepos = aout_info.dreloff;
3747 obj_sym_filepos (abfd) = aout_info.symoff;
3749 /* We keep a count of the symbols as we output them. */
3750 obj_aout_external_sym_count (abfd) = 0;
3752 /* We accumulate the string table as we write out the symbols. */
3753 aout_info.strtab = _bfd_stringtab_init ();
3754 if (aout_info.strtab == NULL)
3755 goto error_return;
3757 /* Allocate buffers to hold section contents and relocs. */
3758 aout_info.contents = (bfd_byte *) bfd_malloc (max_contents_size);
3759 aout_info.relocs = (PTR) bfd_malloc (max_relocs_size);
3760 aout_info.symbol_map = (int *) bfd_malloc (max_sym_count * sizeof (int *));
3761 aout_info.output_syms = ((struct external_nlist *)
3762 bfd_malloc ((max_sym_count + 1)
3763 * sizeof (struct external_nlist)));
3764 if ((aout_info.contents == NULL && max_contents_size != 0)
3765 || (aout_info.relocs == NULL && max_relocs_size != 0)
3766 || (aout_info.symbol_map == NULL && max_sym_count != 0)
3767 || aout_info.output_syms == NULL)
3768 goto error_return;
3770 /* If we have a symbol named __DYNAMIC, force it out now. This is
3771 required by SunOS. Doing this here rather than in sunos.c is a
3772 hack, but it's easier than exporting everything which would be
3773 needed. */
3775 struct aout_link_hash_entry *h;
3777 h = aout_link_hash_lookup (aout_hash_table (info), "__DYNAMIC",
3778 FALSE, FALSE, FALSE);
3779 if (h != NULL)
3780 aout_link_write_other_symbol (h, &aout_info);
3783 /* The most time efficient way to do the link would be to read all
3784 the input object files into memory and then sort out the
3785 information into the output file. Unfortunately, that will
3786 probably use too much memory. Another method would be to step
3787 through everything that composes the text section and write it
3788 out, and then everything that composes the data section and write
3789 it out, and then write out the relocs, and then write out the
3790 symbols. Unfortunately, that requires reading stuff from each
3791 input file several times, and we will not be able to keep all the
3792 input files open simultaneously, and reopening them will be slow.
3794 What we do is basically process one input file at a time. We do
3795 everything we need to do with an input file once--copy over the
3796 section contents, handle the relocation information, and write
3797 out the symbols--and then we throw away the information we read
3798 from it. This approach requires a lot of lseeks of the output
3799 file, which is unfortunate but still faster than reopening a lot
3800 of files.
3802 We use the output_has_begun field of the input BFDs to see
3803 whether we have already handled it. */
3804 for (sub = info->input_bfds; sub != (bfd *) NULL; sub = sub->link_next)
3805 sub->output_has_begun = FALSE;
3807 /* Mark all sections which are to be included in the link. This
3808 will normally be every section. We need to do this so that we
3809 can identify any sections which the linker has decided to not
3810 include. */
3811 for (o = abfd->sections; o != NULL; o = o->next)
3813 for (p = o->link_order_head; p != NULL; p = p->next)
3814 if (p->type == bfd_indirect_link_order)
3815 p->u.indirect.section->linker_mark = TRUE;
3818 have_link_order_relocs = FALSE;
3819 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3821 for (p = o->link_order_head;
3822 p != (struct bfd_link_order *) NULL;
3823 p = p->next)
3825 if (p->type == bfd_indirect_link_order
3826 && (bfd_get_flavour (p->u.indirect.section->owner)
3827 == bfd_target_aout_flavour))
3829 bfd *input_bfd;
3831 input_bfd = p->u.indirect.section->owner;
3832 if (! input_bfd->output_has_begun)
3834 if (! aout_link_input_bfd (&aout_info, input_bfd))
3835 goto error_return;
3836 input_bfd->output_has_begun = TRUE;
3839 else if (p->type == bfd_section_reloc_link_order
3840 || p->type == bfd_symbol_reloc_link_order)
3842 /* These are handled below. */
3843 have_link_order_relocs = TRUE;
3845 else
3847 if (! _bfd_default_link_order (abfd, info, o, p))
3848 goto error_return;
3853 /* Write out any symbols that we have not already written out. */
3854 aout_link_hash_traverse (aout_hash_table (info),
3855 aout_link_write_other_symbol,
3856 (PTR) &aout_info);
3858 /* Now handle any relocs we were asked to create by the linker.
3859 These did not come from any input file. We must do these after
3860 we have written out all the symbols, so that we know the symbol
3861 indices to use. */
3862 if (have_link_order_relocs)
3864 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3866 for (p = o->link_order_head;
3867 p != (struct bfd_link_order *) NULL;
3868 p = p->next)
3870 if (p->type == bfd_section_reloc_link_order
3871 || p->type == bfd_symbol_reloc_link_order)
3873 if (! aout_link_reloc_link_order (&aout_info, o, p))
3874 goto error_return;
3880 if (aout_info.contents != NULL)
3882 free (aout_info.contents);
3883 aout_info.contents = NULL;
3885 if (aout_info.relocs != NULL)
3887 free (aout_info.relocs);
3888 aout_info.relocs = NULL;
3890 if (aout_info.symbol_map != NULL)
3892 free (aout_info.symbol_map);
3893 aout_info.symbol_map = NULL;
3895 if (aout_info.output_syms != NULL)
3897 free (aout_info.output_syms);
3898 aout_info.output_syms = NULL;
3900 if (includes_hash_initialized)
3902 bfd_hash_table_free (&aout_info.includes.root);
3903 includes_hash_initialized = FALSE;
3906 /* Finish up any dynamic linking we may be doing. */
3907 if (aout_backend_info (abfd)->finish_dynamic_link != NULL)
3909 if (! (*aout_backend_info (abfd)->finish_dynamic_link) (abfd, info))
3910 goto error_return;
3913 /* Update the header information. */
3914 abfd->symcount = obj_aout_external_sym_count (abfd);
3915 exec_hdr (abfd)->a_syms = abfd->symcount * EXTERNAL_NLIST_SIZE;
3916 obj_str_filepos (abfd) = obj_sym_filepos (abfd) + exec_hdr (abfd)->a_syms;
3917 obj_textsec (abfd)->reloc_count =
3918 exec_hdr (abfd)->a_trsize / obj_reloc_entry_size (abfd);
3919 obj_datasec (abfd)->reloc_count =
3920 exec_hdr (abfd)->a_drsize / obj_reloc_entry_size (abfd);
3922 /* Write out the string table, unless there are no symbols. */
3923 if (abfd->symcount > 0)
3925 if (bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET) != 0
3926 || ! emit_stringtab (abfd, aout_info.strtab))
3927 goto error_return;
3929 else if (obj_textsec (abfd)->reloc_count == 0
3930 && obj_datasec (abfd)->reloc_count == 0)
3932 bfd_byte b;
3933 file_ptr pos;
3935 b = 0;
3936 pos = obj_datasec (abfd)->filepos + exec_hdr (abfd)->a_data - 1;
3937 if (bfd_seek (abfd, pos, SEEK_SET) != 0
3938 || bfd_bwrite (&b, (bfd_size_type) 1, abfd) != 1)
3939 goto error_return;
3942 return TRUE;
3944 error_return:
3945 if (aout_info.contents != NULL)
3946 free (aout_info.contents);
3947 if (aout_info.relocs != NULL)
3948 free (aout_info.relocs);
3949 if (aout_info.symbol_map != NULL)
3950 free (aout_info.symbol_map);
3951 if (aout_info.output_syms != NULL)
3952 free (aout_info.output_syms);
3953 if (includes_hash_initialized)
3954 bfd_hash_table_free (&aout_info.includes.root);
3955 return FALSE;
3958 /* Link an a.out input BFD into the output file. */
3960 static bfd_boolean
3961 aout_link_input_bfd (finfo, input_bfd)
3962 struct aout_final_link_info *finfo;
3963 bfd *input_bfd;
3965 bfd_size_type sym_count;
3967 BFD_ASSERT (bfd_get_format (input_bfd) == bfd_object);
3969 /* If this is a dynamic object, it may need special handling. */
3970 if ((input_bfd->flags & DYNAMIC) != 0
3971 && aout_backend_info (input_bfd)->link_dynamic_object != NULL)
3973 return ((*aout_backend_info (input_bfd)->link_dynamic_object)
3974 (finfo->info, input_bfd));
3977 /* Get the symbols. We probably have them already, unless
3978 finfo->info->keep_memory is FALSE. */
3979 if (! aout_get_external_symbols (input_bfd))
3980 return FALSE;
3982 sym_count = obj_aout_external_sym_count (input_bfd);
3984 /* Write out the symbols and get a map of the new indices. The map
3985 is placed into finfo->symbol_map. */
3986 if (! aout_link_write_symbols (finfo, input_bfd))
3987 return FALSE;
3989 /* Relocate and write out the sections. These functions use the
3990 symbol map created by aout_link_write_symbols. The linker_mark
3991 field will be set if these sections are to be included in the
3992 link, which will normally be the case. */
3993 if (obj_textsec (input_bfd)->linker_mark)
3995 if (! aout_link_input_section (finfo, input_bfd,
3996 obj_textsec (input_bfd),
3997 &finfo->treloff,
3998 exec_hdr (input_bfd)->a_trsize))
3999 return FALSE;
4001 if (obj_datasec (input_bfd)->linker_mark)
4003 if (! aout_link_input_section (finfo, input_bfd,
4004 obj_datasec (input_bfd),
4005 &finfo->dreloff,
4006 exec_hdr (input_bfd)->a_drsize))
4007 return FALSE;
4010 /* If we are not keeping memory, we don't need the symbols any
4011 longer. We still need them if we are keeping memory, because the
4012 strings in the hash table point into them. */
4013 if (! finfo->info->keep_memory)
4015 if (! aout_link_free_symbols (input_bfd))
4016 return FALSE;
4019 return TRUE;
4022 /* Adjust and write out the symbols for an a.out file. Set the new
4023 symbol indices into a symbol_map. */
4025 static bfd_boolean
4026 aout_link_write_symbols (finfo, input_bfd)
4027 struct aout_final_link_info *finfo;
4028 bfd *input_bfd;
4030 bfd *output_bfd;
4031 bfd_size_type sym_count;
4032 char *strings;
4033 enum bfd_link_strip strip;
4034 enum bfd_link_discard discard;
4035 struct external_nlist *outsym;
4036 bfd_size_type strtab_index;
4037 register struct external_nlist *sym;
4038 struct external_nlist *sym_end;
4039 struct aout_link_hash_entry **sym_hash;
4040 int *symbol_map;
4041 bfd_boolean pass;
4042 bfd_boolean skip_next;
4044 output_bfd = finfo->output_bfd;
4045 sym_count = obj_aout_external_sym_count (input_bfd);
4046 strings = obj_aout_external_strings (input_bfd);
4047 strip = finfo->info->strip;
4048 discard = finfo->info->discard;
4049 outsym = finfo->output_syms;
4051 /* First write out a symbol for this object file, unless we are
4052 discarding such symbols. */
4053 if (strip != strip_all
4054 && (strip != strip_some
4055 || bfd_hash_lookup (finfo->info->keep_hash, input_bfd->filename,
4056 FALSE, FALSE) != NULL)
4057 && discard != discard_all)
4059 H_PUT_8 (output_bfd, N_TEXT, outsym->e_type);
4060 H_PUT_8 (output_bfd, 0, outsym->e_other);
4061 H_PUT_16 (output_bfd, 0, outsym->e_desc);
4062 strtab_index = add_to_stringtab (output_bfd, finfo->strtab,
4063 input_bfd->filename, FALSE);
4064 if (strtab_index == (bfd_size_type) -1)
4065 return FALSE;
4066 PUT_WORD (output_bfd, strtab_index, outsym->e_strx);
4067 PUT_WORD (output_bfd,
4068 (bfd_get_section_vma (output_bfd,
4069 obj_textsec (input_bfd)->output_section)
4070 + obj_textsec (input_bfd)->output_offset),
4071 outsym->e_value);
4072 ++obj_aout_external_sym_count (output_bfd);
4073 ++outsym;
4076 pass = FALSE;
4077 skip_next = FALSE;
4078 sym = obj_aout_external_syms (input_bfd);
4079 sym_end = sym + sym_count;
4080 sym_hash = obj_aout_sym_hashes (input_bfd);
4081 symbol_map = finfo->symbol_map;
4082 memset (symbol_map, 0, (size_t) sym_count * sizeof *symbol_map);
4083 for (; sym < sym_end; sym++, sym_hash++, symbol_map++)
4085 const char *name;
4086 int type;
4087 struct aout_link_hash_entry *h;
4088 bfd_boolean skip;
4089 asection *symsec;
4090 bfd_vma val = 0;
4091 bfd_boolean copy;
4093 /* We set *symbol_map to 0 above for all symbols. If it has
4094 already been set to -1 for this symbol, it means that we are
4095 discarding it because it appears in a duplicate header file.
4096 See the N_BINCL code below. */
4097 if (*symbol_map == -1)
4098 continue;
4100 /* Initialize *symbol_map to -1, which means that the symbol was
4101 not copied into the output file. We will change it later if
4102 we do copy the symbol over. */
4103 *symbol_map = -1;
4105 type = H_GET_8 (input_bfd, sym->e_type);
4106 name = strings + GET_WORD (input_bfd, sym->e_strx);
4108 h = NULL;
4110 if (pass)
4112 /* Pass this symbol through. It is the target of an
4113 indirect or warning symbol. */
4114 val = GET_WORD (input_bfd, sym->e_value);
4115 pass = FALSE;
4117 else if (skip_next)
4119 /* Skip this symbol, which is the target of an indirect
4120 symbol that we have changed to no longer be an indirect
4121 symbol. */
4122 skip_next = FALSE;
4123 continue;
4125 else
4127 struct aout_link_hash_entry *hresolve;
4129 /* We have saved the hash table entry for this symbol, if
4130 there is one. Note that we could just look it up again
4131 in the hash table, provided we first check that it is an
4132 external symbol. */
4133 h = *sym_hash;
4135 /* Use the name from the hash table, in case the symbol was
4136 wrapped. */
4137 if (h != NULL
4138 && h->root.type != bfd_link_hash_warning)
4139 name = h->root.root.string;
4141 /* If this is an indirect or warning symbol, then change
4142 hresolve to the base symbol. We also change *sym_hash so
4143 that the relocation routines relocate against the real
4144 symbol. */
4145 hresolve = h;
4146 if (h != (struct aout_link_hash_entry *) NULL
4147 && (h->root.type == bfd_link_hash_indirect
4148 || h->root.type == bfd_link_hash_warning))
4150 hresolve = (struct aout_link_hash_entry *) h->root.u.i.link;
4151 while (hresolve->root.type == bfd_link_hash_indirect
4152 || hresolve->root.type == bfd_link_hash_warning)
4153 hresolve = ((struct aout_link_hash_entry *)
4154 hresolve->root.u.i.link);
4155 *sym_hash = hresolve;
4158 /* If the symbol has already been written out, skip it. */
4159 if (h != (struct aout_link_hash_entry *) NULL
4160 && h->written)
4162 if ((type & N_TYPE) == N_INDR
4163 || type == N_WARNING)
4164 skip_next = TRUE;
4165 *symbol_map = h->indx;
4166 continue;
4169 /* See if we are stripping this symbol. */
4170 skip = FALSE;
4171 switch (strip)
4173 case strip_none:
4174 break;
4175 case strip_debugger:
4176 if ((type & N_STAB) != 0)
4177 skip = TRUE;
4178 break;
4179 case strip_some:
4180 if (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
4181 == NULL)
4182 skip = TRUE;
4183 break;
4184 case strip_all:
4185 skip = TRUE;
4186 break;
4188 if (skip)
4190 if (h != (struct aout_link_hash_entry *) NULL)
4191 h->written = TRUE;
4192 continue;
4195 /* Get the value of the symbol. */
4196 if ((type & N_TYPE) == N_TEXT
4197 || type == N_WEAKT)
4198 symsec = obj_textsec (input_bfd);
4199 else if ((type & N_TYPE) == N_DATA
4200 || type == N_WEAKD)
4201 symsec = obj_datasec (input_bfd);
4202 else if ((type & N_TYPE) == N_BSS
4203 || type == N_WEAKB)
4204 symsec = obj_bsssec (input_bfd);
4205 else if ((type & N_TYPE) == N_ABS
4206 || type == N_WEAKA)
4207 symsec = bfd_abs_section_ptr;
4208 else if (((type & N_TYPE) == N_INDR
4209 && (hresolve == (struct aout_link_hash_entry *) NULL
4210 || (hresolve->root.type != bfd_link_hash_defined
4211 && hresolve->root.type != bfd_link_hash_defweak
4212 && hresolve->root.type != bfd_link_hash_common)))
4213 || type == N_WARNING)
4215 /* Pass the next symbol through unchanged. The
4216 condition above for indirect symbols is so that if
4217 the indirect symbol was defined, we output it with
4218 the correct definition so the debugger will
4219 understand it. */
4220 pass = TRUE;
4221 val = GET_WORD (input_bfd, sym->e_value);
4222 symsec = NULL;
4224 else if ((type & N_STAB) != 0)
4226 val = GET_WORD (input_bfd, sym->e_value);
4227 symsec = NULL;
4229 else
4231 /* If we get here with an indirect symbol, it means that
4232 we are outputting it with a real definition. In such
4233 a case we do not want to output the next symbol,
4234 which is the target of the indirection. */
4235 if ((type & N_TYPE) == N_INDR)
4236 skip_next = TRUE;
4238 symsec = NULL;
4240 /* We need to get the value from the hash table. We use
4241 hresolve so that if we have defined an indirect
4242 symbol we output the final definition. */
4243 if (h == (struct aout_link_hash_entry *) NULL)
4245 switch (type & N_TYPE)
4247 case N_SETT:
4248 symsec = obj_textsec (input_bfd);
4249 break;
4250 case N_SETD:
4251 symsec = obj_datasec (input_bfd);
4252 break;
4253 case N_SETB:
4254 symsec = obj_bsssec (input_bfd);
4255 break;
4256 case N_SETA:
4257 symsec = bfd_abs_section_ptr;
4258 break;
4259 default:
4260 val = 0;
4261 break;
4264 else if (hresolve->root.type == bfd_link_hash_defined
4265 || hresolve->root.type == bfd_link_hash_defweak)
4267 asection *input_section;
4268 asection *output_section;
4270 /* This case usually means a common symbol which was
4271 turned into a defined symbol. */
4272 input_section = hresolve->root.u.def.section;
4273 output_section = input_section->output_section;
4274 BFD_ASSERT (bfd_is_abs_section (output_section)
4275 || output_section->owner == output_bfd);
4276 val = (hresolve->root.u.def.value
4277 + bfd_get_section_vma (output_bfd, output_section)
4278 + input_section->output_offset);
4280 /* Get the correct type based on the section. If
4281 this is a constructed set, force it to be
4282 globally visible. */
4283 if (type == N_SETT
4284 || type == N_SETD
4285 || type == N_SETB
4286 || type == N_SETA)
4287 type |= N_EXT;
4289 type &=~ N_TYPE;
4291 if (output_section == obj_textsec (output_bfd))
4292 type |= (hresolve->root.type == bfd_link_hash_defined
4293 ? N_TEXT
4294 : N_WEAKT);
4295 else if (output_section == obj_datasec (output_bfd))
4296 type |= (hresolve->root.type == bfd_link_hash_defined
4297 ? N_DATA
4298 : N_WEAKD);
4299 else if (output_section == obj_bsssec (output_bfd))
4300 type |= (hresolve->root.type == bfd_link_hash_defined
4301 ? N_BSS
4302 : N_WEAKB);
4303 else
4304 type |= (hresolve->root.type == bfd_link_hash_defined
4305 ? N_ABS
4306 : N_WEAKA);
4308 else if (hresolve->root.type == bfd_link_hash_common)
4309 val = hresolve->root.u.c.size;
4310 else if (hresolve->root.type == bfd_link_hash_undefweak)
4312 val = 0;
4313 type = N_WEAKU;
4315 else
4316 val = 0;
4318 if (symsec != (asection *) NULL)
4319 val = (symsec->output_section->vma
4320 + symsec->output_offset
4321 + (GET_WORD (input_bfd, sym->e_value)
4322 - symsec->vma));
4324 /* If this is a global symbol set the written flag, and if
4325 it is a local symbol see if we should discard it. */
4326 if (h != (struct aout_link_hash_entry *) NULL)
4328 h->written = TRUE;
4329 h->indx = obj_aout_external_sym_count (output_bfd);
4331 else if ((type & N_TYPE) != N_SETT
4332 && (type & N_TYPE) != N_SETD
4333 && (type & N_TYPE) != N_SETB
4334 && (type & N_TYPE) != N_SETA)
4336 switch (discard)
4338 case discard_none:
4339 case discard_sec_merge:
4340 break;
4341 case discard_l:
4342 if ((type & N_STAB) == 0
4343 && bfd_is_local_label_name (input_bfd, name))
4344 skip = TRUE;
4345 break;
4346 case discard_all:
4347 skip = TRUE;
4348 break;
4350 if (skip)
4352 pass = FALSE;
4353 continue;
4357 /* An N_BINCL symbol indicates the start of the stabs
4358 entries for a header file. We need to scan ahead to the
4359 next N_EINCL symbol, ignoring nesting, adding up all the
4360 characters in the symbol names, not including the file
4361 numbers in types (the first number after an open
4362 parenthesis). */
4363 if (type == (int) N_BINCL)
4365 struct external_nlist *incl_sym;
4366 int nest;
4367 struct aout_link_includes_entry *incl_entry;
4368 struct aout_link_includes_totals *t;
4370 val = 0;
4371 nest = 0;
4372 for (incl_sym = sym + 1; incl_sym < sym_end; incl_sym++)
4374 int incl_type;
4376 incl_type = H_GET_8 (input_bfd, incl_sym->e_type);
4377 if (incl_type == (int) N_EINCL)
4379 if (nest == 0)
4380 break;
4381 --nest;
4383 else if (incl_type == (int) N_BINCL)
4384 ++nest;
4385 else if (nest == 0)
4387 const char *s;
4389 s = strings + GET_WORD (input_bfd, incl_sym->e_strx);
4390 for (; *s != '\0'; s++)
4392 val += *s;
4393 if (*s == '(')
4395 /* Skip the file number. */
4396 ++s;
4397 while (ISDIGIT (*s))
4398 ++s;
4399 --s;
4405 /* If we have already included a header file with the
4406 same value, then replace this one with an N_EXCL
4407 symbol. */
4408 copy = (bfd_boolean) (! finfo->info->keep_memory);
4409 incl_entry = aout_link_includes_lookup (&finfo->includes,
4410 name, TRUE, copy);
4411 if (incl_entry == NULL)
4412 return FALSE;
4413 for (t = incl_entry->totals; t != NULL; t = t->next)
4414 if (t->total == val)
4415 break;
4416 if (t == NULL)
4418 /* This is the first time we have seen this header
4419 file with this set of stabs strings. */
4420 t = ((struct aout_link_includes_totals *)
4421 bfd_hash_allocate (&finfo->includes.root,
4422 sizeof *t));
4423 if (t == NULL)
4424 return FALSE;
4425 t->total = val;
4426 t->next = incl_entry->totals;
4427 incl_entry->totals = t;
4429 else
4431 int *incl_map;
4433 /* This is a duplicate header file. We must change
4434 it to be an N_EXCL entry, and mark all the
4435 included symbols to prevent outputting them. */
4436 type = (int) N_EXCL;
4438 nest = 0;
4439 for (incl_sym = sym + 1, incl_map = symbol_map + 1;
4440 incl_sym < sym_end;
4441 incl_sym++, incl_map++)
4443 int incl_type;
4445 incl_type = H_GET_8 (input_bfd, incl_sym->e_type);
4446 if (incl_type == (int) N_EINCL)
4448 if (nest == 0)
4450 *incl_map = -1;
4451 break;
4453 --nest;
4455 else if (incl_type == (int) N_BINCL)
4456 ++nest;
4457 else if (nest == 0)
4458 *incl_map = -1;
4464 /* Copy this symbol into the list of symbols we are going to
4465 write out. */
4466 H_PUT_8 (output_bfd, type, outsym->e_type);
4467 H_PUT_8 (output_bfd, H_GET_8 (input_bfd, sym->e_other), outsym->e_other);
4468 H_PUT_16 (output_bfd, H_GET_16 (input_bfd, sym->e_desc), outsym->e_desc);
4469 copy = FALSE;
4470 if (! finfo->info->keep_memory)
4472 /* name points into a string table which we are going to
4473 free. If there is a hash table entry, use that string.
4474 Otherwise, copy name into memory. */
4475 if (h != (struct aout_link_hash_entry *) NULL)
4476 name = h->root.root.string;
4477 else
4478 copy = TRUE;
4480 strtab_index = add_to_stringtab (output_bfd, finfo->strtab,
4481 name, copy);
4482 if (strtab_index == (bfd_size_type) -1)
4483 return FALSE;
4484 PUT_WORD (output_bfd, strtab_index, outsym->e_strx);
4485 PUT_WORD (output_bfd, val, outsym->e_value);
4486 *symbol_map = obj_aout_external_sym_count (output_bfd);
4487 ++obj_aout_external_sym_count (output_bfd);
4488 ++outsym;
4491 /* Write out the output symbols we have just constructed. */
4492 if (outsym > finfo->output_syms)
4494 bfd_size_type outsym_size;
4496 if (bfd_seek (output_bfd, finfo->symoff, SEEK_SET) != 0)
4497 return FALSE;
4498 outsym_size = outsym - finfo->output_syms;
4499 outsym_size *= EXTERNAL_NLIST_SIZE;
4500 if (bfd_bwrite ((PTR) finfo->output_syms, outsym_size, output_bfd)
4501 != outsym_size)
4502 return FALSE;
4503 finfo->symoff += outsym_size;
4506 return TRUE;
4509 /* Write out a symbol that was not associated with an a.out input
4510 object. */
4512 static bfd_boolean
4513 aout_link_write_other_symbol (h, data)
4514 struct aout_link_hash_entry *h;
4515 PTR data;
4517 struct aout_final_link_info *finfo = (struct aout_final_link_info *) data;
4518 bfd *output_bfd;
4519 int type;
4520 bfd_vma val;
4521 struct external_nlist outsym;
4522 bfd_size_type indx;
4523 bfd_size_type amt;
4525 if (h->root.type == bfd_link_hash_warning)
4527 h = (struct aout_link_hash_entry *) h->root.u.i.link;
4528 if (h->root.type == bfd_link_hash_new)
4529 return TRUE;
4532 output_bfd = finfo->output_bfd;
4534 if (aout_backend_info (output_bfd)->write_dynamic_symbol != NULL)
4536 if (! ((*aout_backend_info (output_bfd)->write_dynamic_symbol)
4537 (output_bfd, finfo->info, h)))
4539 /* FIXME: No way to handle errors. */
4540 abort ();
4544 if (h->written)
4545 return TRUE;
4547 h->written = TRUE;
4549 /* An indx of -2 means the symbol must be written. */
4550 if (h->indx != -2
4551 && (finfo->info->strip == strip_all
4552 || (finfo->info->strip == strip_some
4553 && bfd_hash_lookup (finfo->info->keep_hash, h->root.root.string,
4554 FALSE, FALSE) == NULL)))
4555 return TRUE;
4557 switch (h->root.type)
4559 default:
4560 case bfd_link_hash_warning:
4561 abort ();
4562 /* Avoid variable not initialized warnings. */
4563 return TRUE;
4564 case bfd_link_hash_new:
4565 /* This can happen for set symbols when sets are not being
4566 built. */
4567 return TRUE;
4568 case bfd_link_hash_undefined:
4569 type = N_UNDF | N_EXT;
4570 val = 0;
4571 break;
4572 case bfd_link_hash_defined:
4573 case bfd_link_hash_defweak:
4575 asection *sec;
4577 sec = h->root.u.def.section->output_section;
4578 BFD_ASSERT (bfd_is_abs_section (sec)
4579 || sec->owner == output_bfd);
4580 if (sec == obj_textsec (output_bfd))
4581 type = h->root.type == bfd_link_hash_defined ? N_TEXT : N_WEAKT;
4582 else if (sec == obj_datasec (output_bfd))
4583 type = h->root.type == bfd_link_hash_defined ? N_DATA : N_WEAKD;
4584 else if (sec == obj_bsssec (output_bfd))
4585 type = h->root.type == bfd_link_hash_defined ? N_BSS : N_WEAKB;
4586 else
4587 type = h->root.type == bfd_link_hash_defined ? N_ABS : N_WEAKA;
4588 type |= N_EXT;
4589 val = (h->root.u.def.value
4590 + sec->vma
4591 + h->root.u.def.section->output_offset);
4593 break;
4594 case bfd_link_hash_common:
4595 type = N_UNDF | N_EXT;
4596 val = h->root.u.c.size;
4597 break;
4598 case bfd_link_hash_undefweak:
4599 type = N_WEAKU;
4600 val = 0;
4601 case bfd_link_hash_indirect:
4602 /* We ignore these symbols, since the indirected symbol is
4603 already in the hash table. */
4604 return TRUE;
4607 H_PUT_8 (output_bfd, type, outsym.e_type);
4608 H_PUT_8 (output_bfd, 0, outsym.e_other);
4609 H_PUT_16 (output_bfd, 0, outsym.e_desc);
4610 indx = add_to_stringtab (output_bfd, finfo->strtab, h->root.root.string,
4611 FALSE);
4612 if (indx == - (bfd_size_type) 1)
4614 /* FIXME: No way to handle errors. */
4615 abort ();
4617 PUT_WORD (output_bfd, indx, outsym.e_strx);
4618 PUT_WORD (output_bfd, val, outsym.e_value);
4620 amt = EXTERNAL_NLIST_SIZE;
4621 if (bfd_seek (output_bfd, finfo->symoff, SEEK_SET) != 0
4622 || bfd_bwrite ((PTR) &outsym, amt, output_bfd) != amt)
4624 /* FIXME: No way to handle errors. */
4625 abort ();
4628 finfo->symoff += EXTERNAL_NLIST_SIZE;
4629 h->indx = obj_aout_external_sym_count (output_bfd);
4630 ++obj_aout_external_sym_count (output_bfd);
4632 return TRUE;
4635 /* Link an a.out section into the output file. */
4637 static bfd_boolean
4638 aout_link_input_section (finfo, input_bfd, input_section, reloff_ptr,
4639 rel_size)
4640 struct aout_final_link_info *finfo;
4641 bfd *input_bfd;
4642 asection *input_section;
4643 file_ptr *reloff_ptr;
4644 bfd_size_type rel_size;
4646 bfd_size_type input_size;
4647 PTR relocs;
4649 /* Get the section contents. */
4650 input_size = input_section->size;
4651 if (! bfd_get_section_contents (input_bfd, input_section,
4652 (PTR) finfo->contents,
4653 (file_ptr) 0, input_size))
4654 return FALSE;
4656 /* Read in the relocs if we haven't already done it. */
4657 if (aout_section_data (input_section) != NULL
4658 && aout_section_data (input_section)->relocs != NULL)
4659 relocs = aout_section_data (input_section)->relocs;
4660 else
4662 relocs = finfo->relocs;
4663 if (rel_size > 0)
4665 if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0
4666 || bfd_bread (relocs, rel_size, input_bfd) != rel_size)
4667 return FALSE;
4671 /* Relocate the section contents. */
4672 if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE)
4674 if (! aout_link_input_section_std (finfo, input_bfd, input_section,
4675 (struct reloc_std_external *) relocs,
4676 rel_size, finfo->contents))
4677 return FALSE;
4679 else
4681 if (! aout_link_input_section_ext (finfo, input_bfd, input_section,
4682 (struct reloc_ext_external *) relocs,
4683 rel_size, finfo->contents))
4684 return FALSE;
4687 /* Write out the section contents. */
4688 if (! bfd_set_section_contents (finfo->output_bfd,
4689 input_section->output_section,
4690 (PTR) finfo->contents,
4691 (file_ptr) input_section->output_offset,
4692 input_size))
4693 return FALSE;
4695 /* If we are producing relocatable output, the relocs were
4696 modified, and we now write them out. */
4697 if (finfo->info->relocatable && rel_size > 0)
4699 if (bfd_seek (finfo->output_bfd, *reloff_ptr, SEEK_SET) != 0)
4700 return FALSE;
4701 if (bfd_bwrite (relocs, rel_size, finfo->output_bfd) != rel_size)
4702 return FALSE;
4703 *reloff_ptr += rel_size;
4705 /* Assert that the relocs have not run into the symbols, and
4706 that if these are the text relocs they have not run into the
4707 data relocs. */
4708 BFD_ASSERT (*reloff_ptr <= obj_sym_filepos (finfo->output_bfd)
4709 && (reloff_ptr != &finfo->treloff
4710 || (*reloff_ptr
4711 <= obj_datasec (finfo->output_bfd)->rel_filepos)));
4714 return TRUE;
4717 /* Get the section corresponding to a reloc index. */
4719 static INLINE asection *
4720 aout_reloc_index_to_section (abfd, indx)
4721 bfd *abfd;
4722 int indx;
4724 switch (indx & N_TYPE)
4726 case N_TEXT:
4727 return obj_textsec (abfd);
4728 case N_DATA:
4729 return obj_datasec (abfd);
4730 case N_BSS:
4731 return obj_bsssec (abfd);
4732 case N_ABS:
4733 case N_UNDF:
4734 return bfd_abs_section_ptr;
4735 default:
4736 abort ();
4738 /*NOTREACHED*/
4739 return NULL;
4742 /* Relocate an a.out section using standard a.out relocs. */
4744 static bfd_boolean
4745 aout_link_input_section_std (finfo, input_bfd, input_section, relocs,
4746 rel_size, contents)
4747 struct aout_final_link_info *finfo;
4748 bfd *input_bfd;
4749 asection *input_section;
4750 struct reloc_std_external *relocs;
4751 bfd_size_type rel_size;
4752 bfd_byte *contents;
4754 bfd_boolean (*check_dynamic_reloc)
4755 PARAMS ((struct bfd_link_info *, bfd *, asection *,
4756 struct aout_link_hash_entry *, PTR, bfd_byte *, bfd_boolean *,
4757 bfd_vma *));
4758 bfd *output_bfd;
4759 bfd_boolean relocatable;
4760 struct external_nlist *syms;
4761 char *strings;
4762 struct aout_link_hash_entry **sym_hashes;
4763 int *symbol_map;
4764 bfd_size_type reloc_count;
4765 register struct reloc_std_external *rel;
4766 struct reloc_std_external *rel_end;
4768 output_bfd = finfo->output_bfd;
4769 check_dynamic_reloc = aout_backend_info (output_bfd)->check_dynamic_reloc;
4771 BFD_ASSERT (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE);
4772 BFD_ASSERT (input_bfd->xvec->header_byteorder
4773 == output_bfd->xvec->header_byteorder);
4775 relocatable = finfo->info->relocatable;
4776 syms = obj_aout_external_syms (input_bfd);
4777 strings = obj_aout_external_strings (input_bfd);
4778 sym_hashes = obj_aout_sym_hashes (input_bfd);
4779 symbol_map = finfo->symbol_map;
4781 reloc_count = rel_size / RELOC_STD_SIZE;
4782 rel = relocs;
4783 rel_end = rel + reloc_count;
4784 for (; rel < rel_end; rel++)
4786 bfd_vma r_addr;
4787 int r_index;
4788 int r_extern;
4789 int r_pcrel;
4790 int r_baserel = 0;
4791 reloc_howto_type *howto;
4792 struct aout_link_hash_entry *h = NULL;
4793 bfd_vma relocation;
4794 bfd_reloc_status_type r;
4796 r_addr = GET_SWORD (input_bfd, rel->r_address);
4798 #ifdef MY_reloc_howto
4799 howto = MY_reloc_howto (input_bfd, rel, r_index, r_extern, r_pcrel);
4800 #else
4802 int r_jmptable;
4803 int r_relative;
4804 int r_length;
4805 unsigned int howto_idx;
4807 if (bfd_header_big_endian (input_bfd))
4809 r_index = (((unsigned int) rel->r_index[0] << 16)
4810 | ((unsigned int) rel->r_index[1] << 8)
4811 | rel->r_index[2]);
4812 r_extern = (0 != (rel->r_type[0] & RELOC_STD_BITS_EXTERN_BIG));
4813 r_pcrel = (0 != (rel->r_type[0] & RELOC_STD_BITS_PCREL_BIG));
4814 r_baserel = (0 != (rel->r_type[0] & RELOC_STD_BITS_BASEREL_BIG));
4815 r_jmptable= (0 != (rel->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG));
4816 r_relative= (0 != (rel->r_type[0] & RELOC_STD_BITS_RELATIVE_BIG));
4817 r_length = ((rel->r_type[0] & RELOC_STD_BITS_LENGTH_BIG)
4818 >> RELOC_STD_BITS_LENGTH_SH_BIG);
4820 else
4822 r_index = (((unsigned int) rel->r_index[2] << 16)
4823 | ((unsigned int) rel->r_index[1] << 8)
4824 | rel->r_index[0]);
4825 r_extern = (0 != (rel->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE));
4826 r_pcrel = (0 != (rel->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
4827 r_baserel = (0 != (rel->r_type[0]
4828 & RELOC_STD_BITS_BASEREL_LITTLE));
4829 r_jmptable= (0 != (rel->r_type[0]
4830 & RELOC_STD_BITS_JMPTABLE_LITTLE));
4831 r_relative= (0 != (rel->r_type[0]
4832 & RELOC_STD_BITS_RELATIVE_LITTLE));
4833 r_length = ((rel->r_type[0] & RELOC_STD_BITS_LENGTH_LITTLE)
4834 >> RELOC_STD_BITS_LENGTH_SH_LITTLE);
4837 howto_idx = (r_length + 4 * r_pcrel + 8 * r_baserel
4838 + 16 * r_jmptable + 32 * r_relative);
4839 BFD_ASSERT (howto_idx < TABLE_SIZE (howto_table_std));
4840 howto = howto_table_std + howto_idx;
4842 #endif
4844 if (relocatable)
4846 /* We are generating a relocatable output file, and must
4847 modify the reloc accordingly. */
4848 if (r_extern)
4850 /* If we know the symbol this relocation is against,
4851 convert it into a relocation against a section. This
4852 is what the native linker does. */
4853 h = sym_hashes[r_index];
4854 if (h != (struct aout_link_hash_entry *) NULL
4855 && (h->root.type == bfd_link_hash_defined
4856 || h->root.type == bfd_link_hash_defweak))
4858 asection *output_section;
4860 /* Change the r_extern value. */
4861 if (bfd_header_big_endian (output_bfd))
4862 rel->r_type[0] &=~ RELOC_STD_BITS_EXTERN_BIG;
4863 else
4864 rel->r_type[0] &=~ RELOC_STD_BITS_EXTERN_LITTLE;
4866 /* Compute a new r_index. */
4867 output_section = h->root.u.def.section->output_section;
4868 if (output_section == obj_textsec (output_bfd))
4869 r_index = N_TEXT;
4870 else if (output_section == obj_datasec (output_bfd))
4871 r_index = N_DATA;
4872 else if (output_section == obj_bsssec (output_bfd))
4873 r_index = N_BSS;
4874 else
4875 r_index = N_ABS;
4877 /* Add the symbol value and the section VMA to the
4878 addend stored in the contents. */
4879 relocation = (h->root.u.def.value
4880 + output_section->vma
4881 + h->root.u.def.section->output_offset);
4883 else
4885 /* We must change r_index according to the symbol
4886 map. */
4887 r_index = symbol_map[r_index];
4889 if (r_index == -1)
4891 if (h != NULL)
4893 /* We decided to strip this symbol, but it
4894 turns out that we can't. Note that we
4895 lose the other and desc information here.
4896 I don't think that will ever matter for a
4897 global symbol. */
4898 if (h->indx < 0)
4900 h->indx = -2;
4901 h->written = FALSE;
4902 if (! aout_link_write_other_symbol (h,
4903 (PTR) finfo))
4904 return FALSE;
4906 r_index = h->indx;
4908 else
4910 const char *name;
4912 name = strings + GET_WORD (input_bfd,
4913 syms[r_index].e_strx);
4914 if (! ((*finfo->info->callbacks->unattached_reloc)
4915 (finfo->info, name, input_bfd, input_section,
4916 r_addr)))
4917 return FALSE;
4918 r_index = 0;
4922 relocation = 0;
4925 /* Write out the new r_index value. */
4926 if (bfd_header_big_endian (output_bfd))
4928 rel->r_index[0] = r_index >> 16;
4929 rel->r_index[1] = r_index >> 8;
4930 rel->r_index[2] = r_index;
4932 else
4934 rel->r_index[2] = r_index >> 16;
4935 rel->r_index[1] = r_index >> 8;
4936 rel->r_index[0] = r_index;
4939 else
4941 asection *section;
4943 /* This is a relocation against a section. We must
4944 adjust by the amount that the section moved. */
4945 section = aout_reloc_index_to_section (input_bfd, r_index);
4946 relocation = (section->output_section->vma
4947 + section->output_offset
4948 - section->vma);
4951 /* Change the address of the relocation. */
4952 PUT_WORD (output_bfd,
4953 r_addr + input_section->output_offset,
4954 rel->r_address);
4956 /* Adjust a PC relative relocation by removing the reference
4957 to the original address in the section and including the
4958 reference to the new address. */
4959 if (r_pcrel)
4960 relocation -= (input_section->output_section->vma
4961 + input_section->output_offset
4962 - input_section->vma);
4964 #ifdef MY_relocatable_reloc
4965 MY_relocatable_reloc (howto, output_bfd, rel, relocation, r_addr);
4966 #endif
4968 if (relocation == 0)
4969 r = bfd_reloc_ok;
4970 else
4971 r = MY_relocate_contents (howto,
4972 input_bfd, relocation,
4973 contents + r_addr);
4975 else
4977 bfd_boolean hundef;
4979 /* We are generating an executable, and must do a full
4980 relocation. */
4981 hundef = FALSE;
4983 if (r_extern)
4985 h = sym_hashes[r_index];
4987 if (h != (struct aout_link_hash_entry *) NULL
4988 && (h->root.type == bfd_link_hash_defined
4989 || h->root.type == bfd_link_hash_defweak))
4991 relocation = (h->root.u.def.value
4992 + h->root.u.def.section->output_section->vma
4993 + h->root.u.def.section->output_offset);
4995 else if (h != (struct aout_link_hash_entry *) NULL
4996 && h->root.type == bfd_link_hash_undefweak)
4997 relocation = 0;
4998 else
5000 hundef = TRUE;
5001 relocation = 0;
5004 else
5006 asection *section;
5008 section = aout_reloc_index_to_section (input_bfd, r_index);
5009 relocation = (section->output_section->vma
5010 + section->output_offset
5011 - section->vma);
5012 if (r_pcrel)
5013 relocation += input_section->vma;
5016 if (check_dynamic_reloc != NULL)
5018 bfd_boolean skip;
5020 if (! ((*check_dynamic_reloc)
5021 (finfo->info, input_bfd, input_section, h,
5022 (PTR) rel, contents, &skip, &relocation)))
5023 return FALSE;
5024 if (skip)
5025 continue;
5028 /* Now warn if a global symbol is undefined. We could not
5029 do this earlier, because check_dynamic_reloc might want
5030 to skip this reloc. */
5031 if (hundef && ! finfo->info->shared && ! r_baserel)
5033 const char *name;
5035 if (h != NULL)
5036 name = h->root.root.string;
5037 else
5038 name = strings + GET_WORD (input_bfd, syms[r_index].e_strx);
5039 if (! ((*finfo->info->callbacks->undefined_symbol)
5040 (finfo->info, name, input_bfd, input_section,
5041 r_addr, TRUE)))
5042 return FALSE;
5045 r = MY_final_link_relocate (howto,
5046 input_bfd, input_section,
5047 contents, r_addr, relocation,
5048 (bfd_vma) 0);
5051 if (r != bfd_reloc_ok)
5053 switch (r)
5055 default:
5056 case bfd_reloc_outofrange:
5057 abort ();
5058 case bfd_reloc_overflow:
5060 const char *name;
5062 if (h != NULL)
5063 name = NULL;
5064 else if (r_extern)
5065 name = strings + GET_WORD (input_bfd,
5066 syms[r_index].e_strx);
5067 else
5069 asection *s;
5071 s = aout_reloc_index_to_section (input_bfd, r_index);
5072 name = bfd_section_name (input_bfd, s);
5074 if (! ((*finfo->info->callbacks->reloc_overflow)
5075 (finfo->info, (h ? &h->root : NULL), name,
5076 howto->name, (bfd_vma) 0, input_bfd,
5077 input_section, r_addr)))
5078 return FALSE;
5080 break;
5085 return TRUE;
5088 /* Relocate an a.out section using extended a.out relocs. */
5090 static bfd_boolean
5091 aout_link_input_section_ext (finfo, input_bfd, input_section, relocs,
5092 rel_size, contents)
5093 struct aout_final_link_info *finfo;
5094 bfd *input_bfd;
5095 asection *input_section;
5096 struct reloc_ext_external *relocs;
5097 bfd_size_type rel_size;
5098 bfd_byte *contents;
5100 bfd_boolean (*check_dynamic_reloc)
5101 PARAMS ((struct bfd_link_info *, bfd *, asection *,
5102 struct aout_link_hash_entry *, PTR, bfd_byte *, bfd_boolean *,
5103 bfd_vma *));
5104 bfd *output_bfd;
5105 bfd_boolean relocatable;
5106 struct external_nlist *syms;
5107 char *strings;
5108 struct aout_link_hash_entry **sym_hashes;
5109 int *symbol_map;
5110 bfd_size_type reloc_count;
5111 register struct reloc_ext_external *rel;
5112 struct reloc_ext_external *rel_end;
5114 output_bfd = finfo->output_bfd;
5115 check_dynamic_reloc = aout_backend_info (output_bfd)->check_dynamic_reloc;
5117 BFD_ASSERT (obj_reloc_entry_size (input_bfd) == RELOC_EXT_SIZE);
5118 BFD_ASSERT (input_bfd->xvec->header_byteorder
5119 == output_bfd->xvec->header_byteorder);
5121 relocatable = finfo->info->relocatable;
5122 syms = obj_aout_external_syms (input_bfd);
5123 strings = obj_aout_external_strings (input_bfd);
5124 sym_hashes = obj_aout_sym_hashes (input_bfd);
5125 symbol_map = finfo->symbol_map;
5127 reloc_count = rel_size / RELOC_EXT_SIZE;
5128 rel = relocs;
5129 rel_end = rel + reloc_count;
5130 for (; rel < rel_end; rel++)
5132 bfd_vma r_addr;
5133 int r_index;
5134 int r_extern;
5135 unsigned int r_type;
5136 bfd_vma r_addend;
5137 struct aout_link_hash_entry *h = NULL;
5138 asection *r_section = NULL;
5139 bfd_vma relocation;
5141 r_addr = GET_SWORD (input_bfd, rel->r_address);
5143 if (bfd_header_big_endian (input_bfd))
5145 r_index = (((unsigned int) rel->r_index[0] << 16)
5146 | ((unsigned int) rel->r_index[1] << 8)
5147 | rel->r_index[2]);
5148 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG));
5149 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
5150 >> RELOC_EXT_BITS_TYPE_SH_BIG);
5152 else
5154 r_index = (((unsigned int) rel->r_index[2] << 16)
5155 | ((unsigned int) rel->r_index[1] << 8)
5156 | rel->r_index[0]);
5157 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
5158 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
5159 >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
5162 r_addend = GET_SWORD (input_bfd, rel->r_addend);
5164 BFD_ASSERT (r_type < TABLE_SIZE (howto_table_ext));
5166 if (relocatable)
5168 /* We are generating a relocatable output file, and must
5169 modify the reloc accordingly. */
5170 if (r_extern
5171 || r_type == (unsigned int) RELOC_BASE10
5172 || r_type == (unsigned int) RELOC_BASE13
5173 || r_type == (unsigned int) RELOC_BASE22)
5175 /* If we know the symbol this relocation is against,
5176 convert it into a relocation against a section. This
5177 is what the native linker does. */
5178 if (r_type == (unsigned int) RELOC_BASE10
5179 || r_type == (unsigned int) RELOC_BASE13
5180 || r_type == (unsigned int) RELOC_BASE22)
5181 h = NULL;
5182 else
5183 h = sym_hashes[r_index];
5184 if (h != (struct aout_link_hash_entry *) NULL
5185 && (h->root.type == bfd_link_hash_defined
5186 || h->root.type == bfd_link_hash_defweak))
5188 asection *output_section;
5190 /* Change the r_extern value. */
5191 if (bfd_header_big_endian (output_bfd))
5192 rel->r_type[0] &=~ RELOC_EXT_BITS_EXTERN_BIG;
5193 else
5194 rel->r_type[0] &=~ RELOC_EXT_BITS_EXTERN_LITTLE;
5196 /* Compute a new r_index. */
5197 output_section = h->root.u.def.section->output_section;
5198 if (output_section == obj_textsec (output_bfd))
5199 r_index = N_TEXT;
5200 else if (output_section == obj_datasec (output_bfd))
5201 r_index = N_DATA;
5202 else if (output_section == obj_bsssec (output_bfd))
5203 r_index = N_BSS;
5204 else
5205 r_index = N_ABS;
5207 /* Add the symbol value and the section VMA to the
5208 addend. */
5209 relocation = (h->root.u.def.value
5210 + output_section->vma
5211 + h->root.u.def.section->output_offset);
5213 /* Now RELOCATION is the VMA of the final
5214 destination. If this is a PC relative reloc,
5215 then ADDEND is the negative of the source VMA.
5216 We want to set ADDEND to the difference between
5217 the destination VMA and the source VMA, which
5218 means we must adjust RELOCATION by the change in
5219 the source VMA. This is done below. */
5221 else
5223 /* We must change r_index according to the symbol
5224 map. */
5225 r_index = symbol_map[r_index];
5227 if (r_index == -1)
5229 if (h != NULL)
5231 /* We decided to strip this symbol, but it
5232 turns out that we can't. Note that we
5233 lose the other and desc information here.
5234 I don't think that will ever matter for a
5235 global symbol. */
5236 if (h->indx < 0)
5238 h->indx = -2;
5239 h->written = FALSE;
5240 if (! aout_link_write_other_symbol (h,
5241 (PTR) finfo))
5242 return FALSE;
5244 r_index = h->indx;
5246 else
5248 const char *name;
5250 name = strings + GET_WORD (input_bfd,
5251 syms[r_index].e_strx);
5252 if (! ((*finfo->info->callbacks->unattached_reloc)
5253 (finfo->info, name, input_bfd, input_section,
5254 r_addr)))
5255 return FALSE;
5256 r_index = 0;
5260 relocation = 0;
5262 /* If this is a PC relative reloc, then the addend
5263 is the negative of the source VMA. We must
5264 adjust it by the change in the source VMA. This
5265 is done below. */
5268 /* Write out the new r_index value. */
5269 if (bfd_header_big_endian (output_bfd))
5271 rel->r_index[0] = r_index >> 16;
5272 rel->r_index[1] = r_index >> 8;
5273 rel->r_index[2] = r_index;
5275 else
5277 rel->r_index[2] = r_index >> 16;
5278 rel->r_index[1] = r_index >> 8;
5279 rel->r_index[0] = r_index;
5282 else
5284 /* This is a relocation against a section. We must
5285 adjust by the amount that the section moved. */
5286 r_section = aout_reloc_index_to_section (input_bfd, r_index);
5287 relocation = (r_section->output_section->vma
5288 + r_section->output_offset
5289 - r_section->vma);
5291 /* If this is a PC relative reloc, then the addend is
5292 the difference in VMA between the destination and the
5293 source. We have just adjusted for the change in VMA
5294 of the destination, so we must also adjust by the
5295 change in VMA of the source. This is done below. */
5298 /* As described above, we must always adjust a PC relative
5299 reloc by the change in VMA of the source. However, if
5300 pcrel_offset is set, then the addend does not include the
5301 location within the section, in which case we don't need
5302 to adjust anything. */
5303 if (howto_table_ext[r_type].pc_relative
5304 && ! howto_table_ext[r_type].pcrel_offset)
5305 relocation -= (input_section->output_section->vma
5306 + input_section->output_offset
5307 - input_section->vma);
5309 /* Change the addend if necessary. */
5310 if (relocation != 0)
5311 PUT_WORD (output_bfd, r_addend + relocation, rel->r_addend);
5313 /* Change the address of the relocation. */
5314 PUT_WORD (output_bfd,
5315 r_addr + input_section->output_offset,
5316 rel->r_address);
5318 else
5320 bfd_boolean hundef;
5321 bfd_reloc_status_type r;
5323 /* We are generating an executable, and must do a full
5324 relocation. */
5325 hundef = FALSE;
5327 if (r_extern)
5329 h = sym_hashes[r_index];
5331 if (h != (struct aout_link_hash_entry *) NULL
5332 && (h->root.type == bfd_link_hash_defined
5333 || h->root.type == bfd_link_hash_defweak))
5335 relocation = (h->root.u.def.value
5336 + h->root.u.def.section->output_section->vma
5337 + h->root.u.def.section->output_offset);
5339 else if (h != (struct aout_link_hash_entry *) NULL
5340 && h->root.type == bfd_link_hash_undefweak)
5341 relocation = 0;
5342 else
5344 hundef = TRUE;
5345 relocation = 0;
5348 else if (r_type == (unsigned int) RELOC_BASE10
5349 || r_type == (unsigned int) RELOC_BASE13
5350 || r_type == (unsigned int) RELOC_BASE22)
5352 struct external_nlist *sym;
5353 int type;
5355 /* For base relative relocs, r_index is always an index
5356 into the symbol table, even if r_extern is 0. */
5357 sym = syms + r_index;
5358 type = H_GET_8 (input_bfd, sym->e_type);
5359 if ((type & N_TYPE) == N_TEXT
5360 || type == N_WEAKT)
5361 r_section = obj_textsec (input_bfd);
5362 else if ((type & N_TYPE) == N_DATA
5363 || type == N_WEAKD)
5364 r_section = obj_datasec (input_bfd);
5365 else if ((type & N_TYPE) == N_BSS
5366 || type == N_WEAKB)
5367 r_section = obj_bsssec (input_bfd);
5368 else if ((type & N_TYPE) == N_ABS
5369 || type == N_WEAKA)
5370 r_section = bfd_abs_section_ptr;
5371 else
5372 abort ();
5373 relocation = (r_section->output_section->vma
5374 + r_section->output_offset
5375 + (GET_WORD (input_bfd, sym->e_value)
5376 - r_section->vma));
5378 else
5380 r_section = aout_reloc_index_to_section (input_bfd, r_index);
5382 /* If this is a PC relative reloc, then R_ADDEND is the
5383 difference between the two vmas, or
5384 old_dest_sec + old_dest_off - (old_src_sec + old_src_off)
5385 where
5386 old_dest_sec == section->vma
5388 old_src_sec == input_section->vma
5390 old_src_off == r_addr
5392 _bfd_final_link_relocate expects RELOCATION +
5393 R_ADDEND to be the VMA of the destination minus
5394 r_addr (the minus r_addr is because this relocation
5395 is not pcrel_offset, which is a bit confusing and
5396 should, perhaps, be changed), or
5397 new_dest_sec
5398 where
5399 new_dest_sec == output_section->vma + output_offset
5400 We arrange for this to happen by setting RELOCATION to
5401 new_dest_sec + old_src_sec - old_dest_sec
5403 If this is not a PC relative reloc, then R_ADDEND is
5404 simply the VMA of the destination, so we set
5405 RELOCATION to the change in the destination VMA, or
5406 new_dest_sec - old_dest_sec
5408 relocation = (r_section->output_section->vma
5409 + r_section->output_offset
5410 - r_section->vma);
5411 if (howto_table_ext[r_type].pc_relative)
5412 relocation += input_section->vma;
5415 if (check_dynamic_reloc != NULL)
5417 bfd_boolean skip;
5419 if (! ((*check_dynamic_reloc)
5420 (finfo->info, input_bfd, input_section, h,
5421 (PTR) rel, contents, &skip, &relocation)))
5422 return FALSE;
5423 if (skip)
5424 continue;
5427 /* Now warn if a global symbol is undefined. We could not
5428 do this earlier, because check_dynamic_reloc might want
5429 to skip this reloc. */
5430 if (hundef
5431 && ! finfo->info->shared
5432 && r_type != (unsigned int) RELOC_BASE10
5433 && r_type != (unsigned int) RELOC_BASE13
5434 && r_type != (unsigned int) RELOC_BASE22)
5436 const char *name;
5438 if (h != NULL)
5439 name = h->root.root.string;
5440 else
5441 name = strings + GET_WORD (input_bfd, syms[r_index].e_strx);
5442 if (! ((*finfo->info->callbacks->undefined_symbol)
5443 (finfo->info, name, input_bfd, input_section,
5444 r_addr, TRUE)))
5445 return FALSE;
5448 if (r_type != (unsigned int) RELOC_SPARC_REV32)
5449 r = MY_final_link_relocate (howto_table_ext + r_type,
5450 input_bfd, input_section,
5451 contents, r_addr, relocation,
5452 r_addend);
5453 else
5455 bfd_vma x;
5457 x = bfd_get_32 (input_bfd, contents + r_addr);
5458 x = x + relocation + r_addend;
5459 bfd_putl32 (/*input_bfd,*/ x, contents + r_addr);
5460 r = bfd_reloc_ok;
5463 if (r != bfd_reloc_ok)
5465 switch (r)
5467 default:
5468 case bfd_reloc_outofrange:
5469 abort ();
5470 case bfd_reloc_overflow:
5472 const char *name;
5474 if (h != NULL)
5475 name = NULL;
5476 else if (r_extern
5477 || r_type == (unsigned int) RELOC_BASE10
5478 || r_type == (unsigned int) RELOC_BASE13
5479 || r_type == (unsigned int) RELOC_BASE22)
5480 name = strings + GET_WORD (input_bfd,
5481 syms[r_index].e_strx);
5482 else
5484 asection *s;
5486 s = aout_reloc_index_to_section (input_bfd, r_index);
5487 name = bfd_section_name (input_bfd, s);
5489 if (! ((*finfo->info->callbacks->reloc_overflow)
5490 (finfo->info, (h ? &h->root : NULL), name,
5491 howto_table_ext[r_type].name,
5492 r_addend, input_bfd, input_section, r_addr)))
5493 return FALSE;
5495 break;
5501 return TRUE;
5504 /* Handle a link order which is supposed to generate a reloc. */
5506 static bfd_boolean
5507 aout_link_reloc_link_order (finfo, o, p)
5508 struct aout_final_link_info *finfo;
5509 asection *o;
5510 struct bfd_link_order *p;
5512 struct bfd_link_order_reloc *pr;
5513 int r_index;
5514 int r_extern;
5515 reloc_howto_type *howto;
5516 file_ptr *reloff_ptr = NULL;
5517 struct reloc_std_external srel;
5518 struct reloc_ext_external erel;
5519 PTR rel_ptr;
5520 bfd_size_type amt;
5522 pr = p->u.reloc.p;
5524 if (p->type == bfd_section_reloc_link_order)
5526 r_extern = 0;
5527 if (bfd_is_abs_section (pr->u.section))
5528 r_index = N_ABS | N_EXT;
5529 else
5531 BFD_ASSERT (pr->u.section->owner == finfo->output_bfd);
5532 r_index = pr->u.section->target_index;
5535 else
5537 struct aout_link_hash_entry *h;
5539 BFD_ASSERT (p->type == bfd_symbol_reloc_link_order);
5540 r_extern = 1;
5541 h = ((struct aout_link_hash_entry *)
5542 bfd_wrapped_link_hash_lookup (finfo->output_bfd, finfo->info,
5543 pr->u.name, FALSE, FALSE, TRUE));
5544 if (h != (struct aout_link_hash_entry *) NULL
5545 && h->indx >= 0)
5546 r_index = h->indx;
5547 else if (h != NULL)
5549 /* We decided to strip this symbol, but it turns out that we
5550 can't. Note that we lose the other and desc information
5551 here. I don't think that will ever matter for a global
5552 symbol. */
5553 h->indx = -2;
5554 h->written = FALSE;
5555 if (! aout_link_write_other_symbol (h, (PTR) finfo))
5556 return FALSE;
5557 r_index = h->indx;
5559 else
5561 if (! ((*finfo->info->callbacks->unattached_reloc)
5562 (finfo->info, pr->u.name, (bfd *) NULL,
5563 (asection *) NULL, (bfd_vma) 0)))
5564 return FALSE;
5565 r_index = 0;
5569 howto = bfd_reloc_type_lookup (finfo->output_bfd, pr->reloc);
5570 if (howto == 0)
5572 bfd_set_error (bfd_error_bad_value);
5573 return FALSE;
5576 if (o == obj_textsec (finfo->output_bfd))
5577 reloff_ptr = &finfo->treloff;
5578 else if (o == obj_datasec (finfo->output_bfd))
5579 reloff_ptr = &finfo->dreloff;
5580 else
5581 abort ();
5583 if (obj_reloc_entry_size (finfo->output_bfd) == RELOC_STD_SIZE)
5585 #ifdef MY_put_reloc
5586 MY_put_reloc (finfo->output_bfd, r_extern, r_index, p->offset, howto,
5587 &srel);
5588 #else
5590 int r_pcrel;
5591 int r_baserel;
5592 int r_jmptable;
5593 int r_relative;
5594 int r_length;
5596 r_pcrel = (int) howto->pc_relative;
5597 r_baserel = (howto->type & 8) != 0;
5598 r_jmptable = (howto->type & 16) != 0;
5599 r_relative = (howto->type & 32) != 0;
5600 r_length = howto->size;
5602 PUT_WORD (finfo->output_bfd, p->offset, srel.r_address);
5603 if (bfd_header_big_endian (finfo->output_bfd))
5605 srel.r_index[0] = r_index >> 16;
5606 srel.r_index[1] = r_index >> 8;
5607 srel.r_index[2] = r_index;
5608 srel.r_type[0] =
5609 ((r_extern ? RELOC_STD_BITS_EXTERN_BIG : 0)
5610 | (r_pcrel ? RELOC_STD_BITS_PCREL_BIG : 0)
5611 | (r_baserel ? RELOC_STD_BITS_BASEREL_BIG : 0)
5612 | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_BIG : 0)
5613 | (r_relative ? RELOC_STD_BITS_RELATIVE_BIG : 0)
5614 | (r_length << RELOC_STD_BITS_LENGTH_SH_BIG));
5616 else
5618 srel.r_index[2] = r_index >> 16;
5619 srel.r_index[1] = r_index >> 8;
5620 srel.r_index[0] = r_index;
5621 srel.r_type[0] =
5622 ((r_extern ? RELOC_STD_BITS_EXTERN_LITTLE : 0)
5623 | (r_pcrel ? RELOC_STD_BITS_PCREL_LITTLE : 0)
5624 | (r_baserel ? RELOC_STD_BITS_BASEREL_LITTLE : 0)
5625 | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_LITTLE : 0)
5626 | (r_relative ? RELOC_STD_BITS_RELATIVE_LITTLE : 0)
5627 | (r_length << RELOC_STD_BITS_LENGTH_SH_LITTLE));
5630 #endif
5631 rel_ptr = (PTR) &srel;
5633 /* We have to write the addend into the object file, since
5634 standard a.out relocs are in place. It would be more
5635 reliable if we had the current contents of the file here,
5636 rather than assuming zeroes, but we can't read the file since
5637 it was opened using bfd_openw. */
5638 if (pr->addend != 0)
5640 bfd_size_type size;
5641 bfd_reloc_status_type r;
5642 bfd_byte *buf;
5643 bfd_boolean ok;
5645 size = bfd_get_reloc_size (howto);
5646 buf = (bfd_byte *) bfd_zmalloc (size);
5647 if (buf == (bfd_byte *) NULL)
5648 return FALSE;
5649 r = MY_relocate_contents (howto, finfo->output_bfd,
5650 (bfd_vma) pr->addend, buf);
5651 switch (r)
5653 case bfd_reloc_ok:
5654 break;
5655 default:
5656 case bfd_reloc_outofrange:
5657 abort ();
5658 case bfd_reloc_overflow:
5659 if (! ((*finfo->info->callbacks->reloc_overflow)
5660 (finfo->info, NULL,
5661 (p->type == bfd_section_reloc_link_order
5662 ? bfd_section_name (finfo->output_bfd,
5663 pr->u.section)
5664 : pr->u.name),
5665 howto->name, pr->addend, (bfd *) NULL,
5666 (asection *) NULL, (bfd_vma) 0)))
5668 free (buf);
5669 return FALSE;
5671 break;
5673 ok = bfd_set_section_contents (finfo->output_bfd, o, (PTR) buf,
5674 (file_ptr) p->offset, size);
5675 free (buf);
5676 if (! ok)
5677 return FALSE;
5680 else
5682 #ifdef MY_put_ext_reloc
5683 MY_put_ext_reloc (finfo->output_bfd, r_extern, r_index, p->offset,
5684 howto, &erel, pr->addend);
5685 #else
5686 PUT_WORD (finfo->output_bfd, p->offset, erel.r_address);
5688 if (bfd_header_big_endian (finfo->output_bfd))
5690 erel.r_index[0] = r_index >> 16;
5691 erel.r_index[1] = r_index >> 8;
5692 erel.r_index[2] = r_index;
5693 erel.r_type[0] =
5694 ((r_extern ? RELOC_EXT_BITS_EXTERN_BIG : 0)
5695 | (howto->type << RELOC_EXT_BITS_TYPE_SH_BIG));
5697 else
5699 erel.r_index[2] = r_index >> 16;
5700 erel.r_index[1] = r_index >> 8;
5701 erel.r_index[0] = r_index;
5702 erel.r_type[0] =
5703 (r_extern ? RELOC_EXT_BITS_EXTERN_LITTLE : 0)
5704 | (howto->type << RELOC_EXT_BITS_TYPE_SH_LITTLE);
5707 PUT_WORD (finfo->output_bfd, (bfd_vma) pr->addend, erel.r_addend);
5708 #endif /* MY_put_ext_reloc */
5710 rel_ptr = (PTR) &erel;
5713 amt = obj_reloc_entry_size (finfo->output_bfd);
5714 if (bfd_seek (finfo->output_bfd, *reloff_ptr, SEEK_SET) != 0
5715 || bfd_bwrite (rel_ptr, amt, finfo->output_bfd) != amt)
5716 return FALSE;
5718 *reloff_ptr += obj_reloc_entry_size (finfo->output_bfd);
5720 /* Assert that the relocs have not run into the symbols, and that n
5721 the text relocs have not run into the data relocs. */
5722 BFD_ASSERT (*reloff_ptr <= obj_sym_filepos (finfo->output_bfd)
5723 && (reloff_ptr != &finfo->treloff
5724 || (*reloff_ptr
5725 <= obj_datasec (finfo->output_bfd)->rel_filepos)));
5727 return TRUE;