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