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