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