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