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