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