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