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