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