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