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