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