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