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