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