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