Apply patch for binutils bug 806, approved by nickc@redhat.com.
[deliverable/binutils-gdb.git] / bfd / aoutx.h
CommitLineData
252b5132 1/* BFD semi-generic back-end for a.out binaries.
9553c638
AM
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005
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)
c3864421 2751 goto done;
923f08ff
AM
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
9f400ee9
HPN
3433 about. If there is no next symbol, just look away. */
3434 if (p + 1 >= pend)
3435 return TRUE;
252b5132
RH
3436 ++p;
3437 string = name;
3438 name = strings + GET_WORD (abfd, p->e_strx);
3439 section = bfd_und_section_ptr;
3440 flags |= BSF_WARNING;
3441 break;
3442 case N_WEAKU:
3443 section = bfd_und_section_ptr;
3444 flags = BSF_WEAK;
3445 break;
3446 case N_WEAKA:
3447 section = bfd_abs_section_ptr;
3448 flags = BSF_WEAK;
3449 break;
3450 case N_WEAKT:
3451 section = obj_textsec (abfd);
3452 value -= bfd_get_section_vma (abfd, section);
3453 flags = BSF_WEAK;
3454 break;
3455 case N_WEAKD:
3456 section = obj_datasec (abfd);
3457 value -= bfd_get_section_vma (abfd, section);
3458 flags = BSF_WEAK;
3459 break;
3460 case N_WEAKB:
3461 section = obj_bsssec (abfd);
3462 value -= bfd_get_section_vma (abfd, section);
3463 flags = BSF_WEAK;
3464 break;
3465 }
3466
3467 if (! ((*add_one_symbol)
b34976b6 3468 (info, abfd, name, flags, section, value, string, copy, FALSE,
252b5132 3469 (struct bfd_link_hash_entry **) sym_hash)))
b34976b6 3470 return FALSE;
252b5132
RH
3471
3472 /* Restrict the maximum alignment of a common symbol based on
3473 the architecture, since a.out has no way to represent
3474 alignment requirements of a section in a .o file. FIXME:
3475 This isn't quite right: it should use the architecture of the
3476 output file, not the input files. */
3477 if ((*sym_hash)->root.type == bfd_link_hash_common
3478 && ((*sym_hash)->root.u.c.p->alignment_power >
3479 bfd_get_arch_info (abfd)->section_align_power))
3480 (*sym_hash)->root.u.c.p->alignment_power =
3481 bfd_get_arch_info (abfd)->section_align_power;
3482
3483 /* If this is a set symbol, and we are not building sets, then
3484 it is possible for the hash entry to not have been set. In
3485 such a case, treat the symbol as not globally defined. */
3486 if ((*sym_hash)->root.type == bfd_link_hash_new)
3487 {
3488 BFD_ASSERT ((flags & BSF_CONSTRUCTOR) != 0);
3489 *sym_hash = NULL;
3490 }
3491
3492 if (type == (N_INDR | N_EXT) || type == N_WARNING)
3493 ++sym_hash;
3494 }
3495
b34976b6 3496 return TRUE;
252b5132
RH
3497}
3498\f
3499/* A hash table used for header files with N_BINCL entries. */
3500
3501struct aout_link_includes_table
3502{
3503 struct bfd_hash_table root;
3504};
3505
3506/* A linked list of totals that we have found for a particular header
3507 file. */
3508
3509struct aout_link_includes_totals
3510{
3511 struct aout_link_includes_totals *next;
3512 bfd_vma total;
3513};
3514
3515/* An entry in the header file hash table. */
3516
3517struct aout_link_includes_entry
3518{
3519 struct bfd_hash_entry root;
3520 /* List of totals we have found for this file. */
3521 struct aout_link_includes_totals *totals;
3522};
3523
3524/* Look up an entry in an the header file hash table. */
3525
923f08ff
AM
3526#define aout_link_includes_lookup(table, string, create, copy) \
3527 ((struct aout_link_includes_entry *) \
252b5132
RH
3528 bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
3529
3530/* During the final link step we need to pass around a bunch of
3531 information, so we do it in an instance of this structure. */
3532
3533struct aout_final_link_info
3534{
3535 /* General link information. */
3536 struct bfd_link_info *info;
3537 /* Output bfd. */
3538 bfd *output_bfd;
3539 /* Reloc file positions. */
3540 file_ptr treloff, dreloff;
3541 /* File position of symbols. */
3542 file_ptr symoff;
3543 /* String table. */
3544 struct bfd_strtab_hash *strtab;
3545 /* Header file hash table. */
3546 struct aout_link_includes_table includes;
3547 /* A buffer large enough to hold the contents of any section. */
3548 bfd_byte *contents;
3549 /* A buffer large enough to hold the relocs of any section. */
3550 PTR relocs;
3551 /* A buffer large enough to hold the symbol map of any input BFD. */
3552 int *symbol_map;
3553 /* A buffer large enough to hold output symbols of any input BFD. */
3554 struct external_nlist *output_syms;
3555};
3556
3557static struct bfd_hash_entry *aout_link_includes_newfunc
3558 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
b34976b6 3559static bfd_boolean aout_link_input_bfd
252b5132 3560 PARAMS ((struct aout_final_link_info *, bfd *input_bfd));
b34976b6 3561static bfd_boolean aout_link_write_symbols
252b5132 3562 PARAMS ((struct aout_final_link_info *, bfd *input_bfd));
b34976b6 3563static bfd_boolean aout_link_write_other_symbol
252b5132 3564 PARAMS ((struct aout_link_hash_entry *, PTR));
b34976b6 3565static bfd_boolean aout_link_input_section
252b5132
RH
3566 PARAMS ((struct aout_final_link_info *, bfd *input_bfd,
3567 asection *input_section, file_ptr *reloff_ptr,
3568 bfd_size_type rel_size));
b34976b6 3569static bfd_boolean aout_link_input_section_std
252b5132
RH
3570 PARAMS ((struct aout_final_link_info *, bfd *input_bfd,
3571 asection *input_section, struct reloc_std_external *,
3572 bfd_size_type rel_size, bfd_byte *contents));
b34976b6 3573static bfd_boolean aout_link_input_section_ext
252b5132
RH
3574 PARAMS ((struct aout_final_link_info *, bfd *input_bfd,
3575 asection *input_section, struct reloc_ext_external *,
3576 bfd_size_type rel_size, bfd_byte *contents));
3577static INLINE asection *aout_reloc_index_to_section
3578 PARAMS ((bfd *, int));
b34976b6 3579static bfd_boolean aout_link_reloc_link_order
252b5132
RH
3580 PARAMS ((struct aout_final_link_info *, asection *,
3581 struct bfd_link_order *));
3582
3583/* The function to create a new entry in the header file hash table. */
3584
3585static struct bfd_hash_entry *
3586aout_link_includes_newfunc (entry, table, string)
3587 struct bfd_hash_entry *entry;
3588 struct bfd_hash_table *table;
3589 const char *string;
3590{
3591 struct aout_link_includes_entry *ret =
3592 (struct aout_link_includes_entry *) entry;
3593
3594 /* Allocate the structure if it has not already been allocated by a
3595 subclass. */
3596 if (ret == (struct aout_link_includes_entry *) NULL)
3597 ret = ((struct aout_link_includes_entry *)
3598 bfd_hash_allocate (table,
3599 sizeof (struct aout_link_includes_entry)));
3600 if (ret == (struct aout_link_includes_entry *) NULL)
3601 return (struct bfd_hash_entry *) ret;
3602
3603 /* Call the allocation method of the superclass. */
3604 ret = ((struct aout_link_includes_entry *)
3605 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
3606 if (ret)
3607 {
3608 /* Set local fields. */
3609 ret->totals = NULL;
3610 }
3611
3612 return (struct bfd_hash_entry *) ret;
3613}
3614
3615/* Do the final link step. This is called on the output BFD. The
3616 INFO structure should point to a list of BFDs linked through the
3617 link_next field which can be used to find each BFD which takes part
3618 in the output. Also, each section in ABFD should point to a list
3619 of bfd_link_order structures which list all the input sections for
3620 the output section. */
3621
b34976b6 3622bfd_boolean
252b5132
RH
3623NAME(aout,final_link) (abfd, info, callback)
3624 bfd *abfd;
3625 struct bfd_link_info *info;
3626 void (*callback) PARAMS ((bfd *, file_ptr *, file_ptr *, file_ptr *));
3627{
3628 struct aout_final_link_info aout_info;
b34976b6 3629 bfd_boolean includes_hash_initialized = FALSE;
252b5132
RH
3630 register bfd *sub;
3631 bfd_size_type trsize, drsize;
dc810e39
AM
3632 bfd_size_type max_contents_size;
3633 bfd_size_type max_relocs_size;
3634 bfd_size_type max_sym_count;
252b5132
RH
3635 bfd_size_type text_size;
3636 file_ptr text_end;
3637 register struct bfd_link_order *p;
3638 asection *o;
b34976b6 3639 bfd_boolean have_link_order_relocs;
252b5132
RH
3640
3641 if (info->shared)
3642 abfd->flags |= DYNAMIC;
3643
3644 aout_info.info = info;
3645 aout_info.output_bfd = abfd;
3646 aout_info.contents = NULL;
3647 aout_info.relocs = NULL;
3648 aout_info.symbol_map = NULL;
3649 aout_info.output_syms = NULL;
3650
3651 if (! bfd_hash_table_init_n (&aout_info.includes.root,
3652 aout_link_includes_newfunc,
3653 251))
3654 goto error_return;
b34976b6 3655 includes_hash_initialized = TRUE;
252b5132
RH
3656
3657 /* Figure out the largest section size. Also, if generating
1049f94e 3658 relocatable output, count the relocs. */
252b5132
RH
3659 trsize = 0;
3660 drsize = 0;
3661 max_contents_size = 0;
3662 max_relocs_size = 0;
3663 max_sym_count = 0;
3664 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3665 {
dc810e39 3666 bfd_size_type sz;
252b5132 3667
1049f94e 3668 if (info->relocatable)
252b5132
RH
3669 {
3670 if (bfd_get_flavour (sub) == bfd_target_aout_flavour)
3671 {
3672 trsize += exec_hdr (sub)->a_trsize;
3673 drsize += exec_hdr (sub)->a_drsize;
3674 }
3675 else
3676 {
3677 /* FIXME: We need to identify the .text and .data sections
3678 and call get_reloc_upper_bound and canonicalize_reloc to
3679 work out the number of relocs needed, and then multiply
3680 by the reloc size. */
3681 (*_bfd_error_handler)
1049f94e 3682 (_("%s: relocatable link from %s to %s not supported"),
252b5132
RH
3683 bfd_get_filename (abfd),
3684 sub->xvec->name, abfd->xvec->name);
3685 bfd_set_error (bfd_error_invalid_operation);
3686 goto error_return;
3687 }
3688 }
3689
3690 if (bfd_get_flavour (sub) == bfd_target_aout_flavour)
3691 {
eea6121a 3692 sz = obj_textsec (sub)->size;
252b5132
RH
3693 if (sz > max_contents_size)
3694 max_contents_size = sz;
eea6121a 3695 sz = obj_datasec (sub)->size;
252b5132
RH
3696 if (sz > max_contents_size)
3697 max_contents_size = sz;
3698
3699 sz = exec_hdr (sub)->a_trsize;
3700 if (sz > max_relocs_size)
3701 max_relocs_size = sz;
3702 sz = exec_hdr (sub)->a_drsize;
3703 if (sz > max_relocs_size)
3704 max_relocs_size = sz;
3705
3706 sz = obj_aout_external_sym_count (sub);
3707 if (sz > max_sym_count)
3708 max_sym_count = sz;
3709 }
3710 }
3711
1049f94e 3712 if (info->relocatable)
252b5132
RH
3713 {
3714 if (obj_textsec (abfd) != (asection *) NULL)
3715 trsize += (_bfd_count_link_order_relocs (obj_textsec (abfd)
3716 ->link_order_head)
3717 * obj_reloc_entry_size (abfd));
3718 if (obj_datasec (abfd) != (asection *) NULL)
3719 drsize += (_bfd_count_link_order_relocs (obj_datasec (abfd)
3720 ->link_order_head)
3721 * obj_reloc_entry_size (abfd));
3722 }
3723
3724 exec_hdr (abfd)->a_trsize = trsize;
3725 exec_hdr (abfd)->a_drsize = drsize;
3726
3727 exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd);
3728
3729 /* Adjust the section sizes and vmas according to the magic number.
3730 This sets a_text, a_data and a_bss in the exec_hdr and sets the
3731 filepos for each section. */
3732 if (! NAME(aout,adjust_sizes_and_vmas) (abfd, &text_size, &text_end))
3733 goto error_return;
3734
3735 /* The relocation and symbol file positions differ among a.out
3736 targets. We are passed a callback routine from the backend
3737 specific code to handle this.
3738 FIXME: At this point we do not know how much space the symbol
3739 table will require. This will not work for any (nonstandard)
3740 a.out target that needs to know the symbol table size before it
3741 can compute the relocation file positions. This may or may not
3742 be the case for the hp300hpux target, for example. */
3743 (*callback) (abfd, &aout_info.treloff, &aout_info.dreloff,
3744 &aout_info.symoff);
3745 obj_textsec (abfd)->rel_filepos = aout_info.treloff;
3746 obj_datasec (abfd)->rel_filepos = aout_info.dreloff;
3747 obj_sym_filepos (abfd) = aout_info.symoff;
3748
3749 /* We keep a count of the symbols as we output them. */
3750 obj_aout_external_sym_count (abfd) = 0;
3751
3752 /* We accumulate the string table as we write out the symbols. */
3753 aout_info.strtab = _bfd_stringtab_init ();
3754 if (aout_info.strtab == NULL)
3755 goto error_return;
3756
3757 /* Allocate buffers to hold section contents and relocs. */
3758 aout_info.contents = (bfd_byte *) bfd_malloc (max_contents_size);
3759 aout_info.relocs = (PTR) bfd_malloc (max_relocs_size);
3760 aout_info.symbol_map = (int *) bfd_malloc (max_sym_count * sizeof (int *));
3761 aout_info.output_syms = ((struct external_nlist *)
3762 bfd_malloc ((max_sym_count + 1)
3763 * sizeof (struct external_nlist)));
3764 if ((aout_info.contents == NULL && max_contents_size != 0)
3765 || (aout_info.relocs == NULL && max_relocs_size != 0)
3766 || (aout_info.symbol_map == NULL && max_sym_count != 0)
3767 || aout_info.output_syms == NULL)
3768 goto error_return;
3769
3770 /* If we have a symbol named __DYNAMIC, force it out now. This is
3771 required by SunOS. Doing this here rather than in sunos.c is a
3772 hack, but it's easier than exporting everything which would be
3773 needed. */
3774 {
3775 struct aout_link_hash_entry *h;
3776
3777 h = aout_link_hash_lookup (aout_hash_table (info), "__DYNAMIC",
b34976b6 3778 FALSE, FALSE, FALSE);
252b5132
RH
3779 if (h != NULL)
3780 aout_link_write_other_symbol (h, &aout_info);
3781 }
3782
3783 /* The most time efficient way to do the link would be to read all
3784 the input object files into memory and then sort out the
3785 information into the output file. Unfortunately, that will
3786 probably use too much memory. Another method would be to step
3787 through everything that composes the text section and write it
3788 out, and then everything that composes the data section and write
3789 it out, and then write out the relocs, and then write out the
3790 symbols. Unfortunately, that requires reading stuff from each
3791 input file several times, and we will not be able to keep all the
3792 input files open simultaneously, and reopening them will be slow.
3793
3794 What we do is basically process one input file at a time. We do
3795 everything we need to do with an input file once--copy over the
3796 section contents, handle the relocation information, and write
3797 out the symbols--and then we throw away the information we read
3798 from it. This approach requires a lot of lseeks of the output
3799 file, which is unfortunate but still faster than reopening a lot
3800 of files.
3801
3802 We use the output_has_begun field of the input BFDs to see
3803 whether we have already handled it. */
3804 for (sub = info->input_bfds; sub != (bfd *) NULL; sub = sub->link_next)
b34976b6 3805 sub->output_has_begun = FALSE;
252b5132
RH
3806
3807 /* Mark all sections which are to be included in the link. This
3808 will normally be every section. We need to do this so that we
3809 can identify any sections which the linker has decided to not
3810 include. */
3811 for (o = abfd->sections; o != NULL; o = o->next)
3812 {
3813 for (p = o->link_order_head; p != NULL; p = p->next)
5ed6aba4
NC
3814 if (p->type == bfd_indirect_link_order)
3815 p->u.indirect.section->linker_mark = TRUE;
252b5132
RH
3816 }
3817
b34976b6 3818 have_link_order_relocs = FALSE;
252b5132
RH
3819 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3820 {
3821 for (p = o->link_order_head;
3822 p != (struct bfd_link_order *) NULL;
3823 p = p->next)
3824 {
3825 if (p->type == bfd_indirect_link_order
3826 && (bfd_get_flavour (p->u.indirect.section->owner)
3827 == bfd_target_aout_flavour))
3828 {
3829 bfd *input_bfd;
3830
3831 input_bfd = p->u.indirect.section->owner;
3832 if (! input_bfd->output_has_begun)
3833 {
3834 if (! aout_link_input_bfd (&aout_info, input_bfd))
3835 goto error_return;
b34976b6 3836 input_bfd->output_has_begun = TRUE;
252b5132
RH
3837 }
3838 }
3839 else if (p->type == bfd_section_reloc_link_order
3840 || p->type == bfd_symbol_reloc_link_order)
3841 {
3842 /* These are handled below. */
b34976b6 3843 have_link_order_relocs = TRUE;
252b5132
RH
3844 }
3845 else
3846 {
3847 if (! _bfd_default_link_order (abfd, info, o, p))
3848 goto error_return;
3849 }
3850 }
3851 }
3852
3853 /* Write out any symbols that we have not already written out. */
3854 aout_link_hash_traverse (aout_hash_table (info),
3855 aout_link_write_other_symbol,
3856 (PTR) &aout_info);
3857
3858 /* Now handle any relocs we were asked to create by the linker.
3859 These did not come from any input file. We must do these after
3860 we have written out all the symbols, so that we know the symbol
3861 indices to use. */
3862 if (have_link_order_relocs)
3863 {
3864 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3865 {
3866 for (p = o->link_order_head;
3867 p != (struct bfd_link_order *) NULL;
3868 p = p->next)
3869 {
3870 if (p->type == bfd_section_reloc_link_order
3871 || p->type == bfd_symbol_reloc_link_order)
3872 {
3873 if (! aout_link_reloc_link_order (&aout_info, o, p))
3874 goto error_return;
3875 }
3876 }
3877 }
3878 }
3879
3880 if (aout_info.contents != NULL)
3881 {
3882 free (aout_info.contents);
3883 aout_info.contents = NULL;
3884 }
3885 if (aout_info.relocs != NULL)
3886 {
3887 free (aout_info.relocs);
3888 aout_info.relocs = NULL;
3889 }
3890 if (aout_info.symbol_map != NULL)
3891 {
3892 free (aout_info.symbol_map);
3893 aout_info.symbol_map = NULL;
3894 }
3895 if (aout_info.output_syms != NULL)
3896 {
3897 free (aout_info.output_syms);
3898 aout_info.output_syms = NULL;
3899 }
3900 if (includes_hash_initialized)
3901 {
3902 bfd_hash_table_free (&aout_info.includes.root);
b34976b6 3903 includes_hash_initialized = FALSE;
252b5132
RH
3904 }
3905
3906 /* Finish up any dynamic linking we may be doing. */
3907 if (aout_backend_info (abfd)->finish_dynamic_link != NULL)
3908 {
3909 if (! (*aout_backend_info (abfd)->finish_dynamic_link) (abfd, info))
3910 goto error_return;
3911 }
3912
3913 /* Update the header information. */
3914 abfd->symcount = obj_aout_external_sym_count (abfd);
3915 exec_hdr (abfd)->a_syms = abfd->symcount * EXTERNAL_NLIST_SIZE;
3916 obj_str_filepos (abfd) = obj_sym_filepos (abfd) + exec_hdr (abfd)->a_syms;
3917 obj_textsec (abfd)->reloc_count =
3918 exec_hdr (abfd)->a_trsize / obj_reloc_entry_size (abfd);
3919 obj_datasec (abfd)->reloc_count =
3920 exec_hdr (abfd)->a_drsize / obj_reloc_entry_size (abfd);
3921
3922 /* Write out the string table, unless there are no symbols. */
3923 if (abfd->symcount > 0)
3924 {
3925 if (bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET) != 0
3926 || ! emit_stringtab (abfd, aout_info.strtab))
3927 goto error_return;
3928 }
3929 else if (obj_textsec (abfd)->reloc_count == 0
3930 && obj_datasec (abfd)->reloc_count == 0)
3931 {
3932 bfd_byte b;
dc810e39 3933 file_ptr pos;
252b5132
RH
3934
3935 b = 0;
dc810e39
AM
3936 pos = obj_datasec (abfd)->filepos + exec_hdr (abfd)->a_data - 1;
3937 if (bfd_seek (abfd, pos, SEEK_SET) != 0
3938 || bfd_bwrite (&b, (bfd_size_type) 1, abfd) != 1)
252b5132
RH
3939 goto error_return;
3940 }
3941
b34976b6 3942 return TRUE;
252b5132
RH
3943
3944 error_return:
3945 if (aout_info.contents != NULL)
3946 free (aout_info.contents);
3947 if (aout_info.relocs != NULL)
3948 free (aout_info.relocs);
3949 if (aout_info.symbol_map != NULL)
3950 free (aout_info.symbol_map);
3951 if (aout_info.output_syms != NULL)
3952 free (aout_info.output_syms);
3953 if (includes_hash_initialized)
3954 bfd_hash_table_free (&aout_info.includes.root);
b34976b6 3955 return FALSE;
252b5132
RH
3956}
3957
3958/* Link an a.out input BFD into the output file. */
3959
b34976b6 3960static bfd_boolean
252b5132
RH
3961aout_link_input_bfd (finfo, input_bfd)
3962 struct aout_final_link_info *finfo;
3963 bfd *input_bfd;
3964{
3965 bfd_size_type sym_count;
3966
3967 BFD_ASSERT (bfd_get_format (input_bfd) == bfd_object);
3968
3969 /* If this is a dynamic object, it may need special handling. */
3970 if ((input_bfd->flags & DYNAMIC) != 0
3971 && aout_backend_info (input_bfd)->link_dynamic_object != NULL)
3972 {
3973 return ((*aout_backend_info (input_bfd)->link_dynamic_object)
3974 (finfo->info, input_bfd));
3975 }
3976
3977 /* Get the symbols. We probably have them already, unless
b34976b6 3978 finfo->info->keep_memory is FALSE. */
252b5132 3979 if (! aout_get_external_symbols (input_bfd))
b34976b6 3980 return FALSE;
252b5132
RH
3981
3982 sym_count = obj_aout_external_sym_count (input_bfd);
3983
3984 /* Write out the symbols and get a map of the new indices. The map
3985 is placed into finfo->symbol_map. */
3986 if (! aout_link_write_symbols (finfo, input_bfd))
b34976b6 3987 return FALSE;
252b5132
RH
3988
3989 /* Relocate and write out the sections. These functions use the
3990 symbol map created by aout_link_write_symbols. The linker_mark
3991 field will be set if these sections are to be included in the
3992 link, which will normally be the case. */
3993 if (obj_textsec (input_bfd)->linker_mark)
3994 {
3995 if (! aout_link_input_section (finfo, input_bfd,
3996 obj_textsec (input_bfd),
3997 &finfo->treloff,
3998 exec_hdr (input_bfd)->a_trsize))
b34976b6 3999 return FALSE;
252b5132
RH
4000 }
4001 if (obj_datasec (input_bfd)->linker_mark)
4002 {
4003 if (! aout_link_input_section (finfo, input_bfd,
4004 obj_datasec (input_bfd),
4005 &finfo->dreloff,
4006 exec_hdr (input_bfd)->a_drsize))
b34976b6 4007 return FALSE;
252b5132
RH
4008 }
4009
4010 /* If we are not keeping memory, we don't need the symbols any
4011 longer. We still need them if we are keeping memory, because the
4012 strings in the hash table point into them. */
4013 if (! finfo->info->keep_memory)
4014 {
4015 if (! aout_link_free_symbols (input_bfd))
b34976b6 4016 return FALSE;
252b5132
RH
4017 }
4018
b34976b6 4019 return TRUE;
252b5132
RH
4020}
4021
4022/* Adjust and write out the symbols for an a.out file. Set the new
4023 symbol indices into a symbol_map. */
4024
b34976b6 4025static bfd_boolean
252b5132
RH
4026aout_link_write_symbols (finfo, input_bfd)
4027 struct aout_final_link_info *finfo;
4028 bfd *input_bfd;
4029{
4030 bfd *output_bfd;
4031 bfd_size_type sym_count;
4032 char *strings;
4033 enum bfd_link_strip strip;
4034 enum bfd_link_discard discard;
4035 struct external_nlist *outsym;
4036 bfd_size_type strtab_index;
4037 register struct external_nlist *sym;
4038 struct external_nlist *sym_end;
4039 struct aout_link_hash_entry **sym_hash;
4040 int *symbol_map;
b34976b6
AM
4041 bfd_boolean pass;
4042 bfd_boolean skip_next;
252b5132
RH
4043
4044 output_bfd = finfo->output_bfd;
4045 sym_count = obj_aout_external_sym_count (input_bfd);
4046 strings = obj_aout_external_strings (input_bfd);
4047 strip = finfo->info->strip;
4048 discard = finfo->info->discard;
4049 outsym = finfo->output_syms;
4050
4051 /* First write out a symbol for this object file, unless we are
4052 discarding such symbols. */
4053 if (strip != strip_all
4054 && (strip != strip_some
4055 || bfd_hash_lookup (finfo->info->keep_hash, input_bfd->filename,
b34976b6 4056 FALSE, FALSE) != NULL)
252b5132
RH
4057 && discard != discard_all)
4058 {
dc810e39
AM
4059 H_PUT_8 (output_bfd, N_TEXT, outsym->e_type);
4060 H_PUT_8 (output_bfd, 0, outsym->e_other);
4061 H_PUT_16 (output_bfd, 0, outsym->e_desc);
252b5132 4062 strtab_index = add_to_stringtab (output_bfd, finfo->strtab,
b34976b6 4063 input_bfd->filename, FALSE);
252b5132 4064 if (strtab_index == (bfd_size_type) -1)
b34976b6 4065 return FALSE;
252b5132
RH
4066 PUT_WORD (output_bfd, strtab_index, outsym->e_strx);
4067 PUT_WORD (output_bfd,
4068 (bfd_get_section_vma (output_bfd,
4069 obj_textsec (input_bfd)->output_section)
4070 + obj_textsec (input_bfd)->output_offset),
4071 outsym->e_value);
4072 ++obj_aout_external_sym_count (output_bfd);
4073 ++outsym;
4074 }
4075
b34976b6
AM
4076 pass = FALSE;
4077 skip_next = FALSE;
252b5132
RH
4078 sym = obj_aout_external_syms (input_bfd);
4079 sym_end = sym + sym_count;
4080 sym_hash = obj_aout_sym_hashes (input_bfd);
4081 symbol_map = finfo->symbol_map;
dc810e39 4082 memset (symbol_map, 0, (size_t) sym_count * sizeof *symbol_map);
252b5132
RH
4083 for (; sym < sym_end; sym++, sym_hash++, symbol_map++)
4084 {
4085 const char *name;
4086 int type;
4087 struct aout_link_hash_entry *h;
b34976b6 4088 bfd_boolean skip;
252b5132
RH
4089 asection *symsec;
4090 bfd_vma val = 0;
b34976b6 4091 bfd_boolean copy;
252b5132
RH
4092
4093 /* We set *symbol_map to 0 above for all symbols. If it has
4094 already been set to -1 for this symbol, it means that we are
4095 discarding it because it appears in a duplicate header file.
4096 See the N_BINCL code below. */
4097 if (*symbol_map == -1)
4098 continue;
4099
4100 /* Initialize *symbol_map to -1, which means that the symbol was
4101 not copied into the output file. We will change it later if
4102 we do copy the symbol over. */
4103 *symbol_map = -1;
4104
dc810e39 4105 type = H_GET_8 (input_bfd, sym->e_type);
252b5132
RH
4106 name = strings + GET_WORD (input_bfd, sym->e_strx);
4107
4108 h = NULL;
4109
4110 if (pass)
4111 {
4112 /* Pass this symbol through. It is the target of an
4113 indirect or warning symbol. */
4114 val = GET_WORD (input_bfd, sym->e_value);
b34976b6 4115 pass = FALSE;
252b5132
RH
4116 }
4117 else if (skip_next)
4118 {
4119 /* Skip this symbol, which is the target of an indirect
4120 symbol that we have changed to no longer be an indirect
4121 symbol. */
b34976b6 4122 skip_next = FALSE;
252b5132
RH
4123 continue;
4124 }
4125 else
4126 {
4127 struct aout_link_hash_entry *hresolve;
4128
4129 /* We have saved the hash table entry for this symbol, if
4130 there is one. Note that we could just look it up again
4131 in the hash table, provided we first check that it is an
0ef5a5bd 4132 external symbol. */
252b5132
RH
4133 h = *sym_hash;
4134
4135 /* Use the name from the hash table, in case the symbol was
4136 wrapped. */
0f3bd64f
AM
4137 if (h != NULL
4138 && h->root.type != bfd_link_hash_warning)
252b5132
RH
4139 name = h->root.root.string;
4140
4141 /* If this is an indirect or warning symbol, then change
4142 hresolve to the base symbol. We also change *sym_hash so
4143 that the relocation routines relocate against the real
4144 symbol. */
4145 hresolve = h;
4146 if (h != (struct aout_link_hash_entry *) NULL
4147 && (h->root.type == bfd_link_hash_indirect
4148 || h->root.type == bfd_link_hash_warning))
4149 {
4150 hresolve = (struct aout_link_hash_entry *) h->root.u.i.link;
4151 while (hresolve->root.type == bfd_link_hash_indirect
4152 || hresolve->root.type == bfd_link_hash_warning)
4153 hresolve = ((struct aout_link_hash_entry *)
4154 hresolve->root.u.i.link);
4155 *sym_hash = hresolve;
4156 }
4157
4158 /* If the symbol has already been written out, skip it. */
4159 if (h != (struct aout_link_hash_entry *) NULL
252b5132
RH
4160 && h->written)
4161 {
4162 if ((type & N_TYPE) == N_INDR
4163 || type == N_WARNING)
b34976b6 4164 skip_next = TRUE;
252b5132
RH
4165 *symbol_map = h->indx;
4166 continue;
4167 }
4168
4169 /* See if we are stripping this symbol. */
b34976b6 4170 skip = FALSE;
252b5132
RH
4171 switch (strip)
4172 {
4173 case strip_none:
4174 break;
4175 case strip_debugger:
4176 if ((type & N_STAB) != 0)
b34976b6 4177 skip = TRUE;
252b5132
RH
4178 break;
4179 case strip_some:
b34976b6 4180 if (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
252b5132 4181 == NULL)
b34976b6 4182 skip = TRUE;
252b5132
RH
4183 break;
4184 case strip_all:
b34976b6 4185 skip = TRUE;
252b5132
RH
4186 break;
4187 }
4188 if (skip)
4189 {
4190 if (h != (struct aout_link_hash_entry *) NULL)
b34976b6 4191 h->written = TRUE;
252b5132
RH
4192 continue;
4193 }
4194
4195 /* Get the value of the symbol. */
4196 if ((type & N_TYPE) == N_TEXT
4197 || type == N_WEAKT)
4198 symsec = obj_textsec (input_bfd);
4199 else if ((type & N_TYPE) == N_DATA
4200 || type == N_WEAKD)
4201 symsec = obj_datasec (input_bfd);
4202 else if ((type & N_TYPE) == N_BSS
4203 || type == N_WEAKB)
4204 symsec = obj_bsssec (input_bfd);
4205 else if ((type & N_TYPE) == N_ABS
4206 || type == N_WEAKA)
4207 symsec = bfd_abs_section_ptr;
4208 else if (((type & N_TYPE) == N_INDR
4209 && (hresolve == (struct aout_link_hash_entry *) NULL
4210 || (hresolve->root.type != bfd_link_hash_defined
4211 && hresolve->root.type != bfd_link_hash_defweak
4212 && hresolve->root.type != bfd_link_hash_common)))
4213 || type == N_WARNING)
4214 {
4215 /* Pass the next symbol through unchanged. The
4216 condition above for indirect symbols is so that if
4217 the indirect symbol was defined, we output it with
4218 the correct definition so the debugger will
4219 understand it. */
b34976b6 4220 pass = TRUE;
252b5132
RH
4221 val = GET_WORD (input_bfd, sym->e_value);
4222 symsec = NULL;
4223 }
4224 else if ((type & N_STAB) != 0)
4225 {
4226 val = GET_WORD (input_bfd, sym->e_value);
4227 symsec = NULL;
4228 }
4229 else
4230 {
4231 /* If we get here with an indirect symbol, it means that
4232 we are outputting it with a real definition. In such
4233 a case we do not want to output the next symbol,
4234 which is the target of the indirection. */
4235 if ((type & N_TYPE) == N_INDR)
b34976b6 4236 skip_next = TRUE;
252b5132
RH
4237
4238 symsec = NULL;
4239
4240 /* We need to get the value from the hash table. We use
4241 hresolve so that if we have defined an indirect
4242 symbol we output the final definition. */
4243 if (h == (struct aout_link_hash_entry *) NULL)
4244 {
4245 switch (type & N_TYPE)
4246 {
4247 case N_SETT:
4248 symsec = obj_textsec (input_bfd);
4249 break;
4250 case N_SETD:
4251 symsec = obj_datasec (input_bfd);
4252 break;
4253 case N_SETB:
4254 symsec = obj_bsssec (input_bfd);
4255 break;
4256 case N_SETA:
4257 symsec = bfd_abs_section_ptr;
4258 break;
4259 default:
4260 val = 0;
4261 break;
4262 }
4263 }
4264 else if (hresolve->root.type == bfd_link_hash_defined
4265 || hresolve->root.type == bfd_link_hash_defweak)
4266 {
4267 asection *input_section;
4268 asection *output_section;
4269
4270 /* This case usually means a common symbol which was
4271 turned into a defined symbol. */
4272 input_section = hresolve->root.u.def.section;
4273 output_section = input_section->output_section;
4274 BFD_ASSERT (bfd_is_abs_section (output_section)
4275 || output_section->owner == output_bfd);
4276 val = (hresolve->root.u.def.value
4277 + bfd_get_section_vma (output_bfd, output_section)
4278 + input_section->output_offset);
4279
4280 /* Get the correct type based on the section. If
4281 this is a constructed set, force it to be
4282 globally visible. */
4283 if (type == N_SETT
4284 || type == N_SETD
4285 || type == N_SETB
4286 || type == N_SETA)
4287 type |= N_EXT;
4288
4289 type &=~ N_TYPE;
4290
4291 if (output_section == obj_textsec (output_bfd))
4292 type |= (hresolve->root.type == bfd_link_hash_defined
4293 ? N_TEXT
4294 : N_WEAKT);
4295 else if (output_section == obj_datasec (output_bfd))
4296 type |= (hresolve->root.type == bfd_link_hash_defined
4297 ? N_DATA
4298 : N_WEAKD);
4299 else if (output_section == obj_bsssec (output_bfd))
4300 type |= (hresolve->root.type == bfd_link_hash_defined
4301 ? N_BSS
4302 : N_WEAKB);
4303 else
4304 type |= (hresolve->root.type == bfd_link_hash_defined
4305 ? N_ABS
4306 : N_WEAKA);
4307 }
4308 else if (hresolve->root.type == bfd_link_hash_common)
4309 val = hresolve->root.u.c.size;
4310 else if (hresolve->root.type == bfd_link_hash_undefweak)
4311 {
4312 val = 0;
4313 type = N_WEAKU;
4314 }
4315 else
4316 val = 0;
4317 }
4318 if (symsec != (asection *) NULL)
4319 val = (symsec->output_section->vma
4320 + symsec->output_offset
4321 + (GET_WORD (input_bfd, sym->e_value)
4322 - symsec->vma));
4323
4324 /* If this is a global symbol set the written flag, and if
4325 it is a local symbol see if we should discard it. */
4326 if (h != (struct aout_link_hash_entry *) NULL)
4327 {
b34976b6 4328 h->written = TRUE;
252b5132
RH
4329 h->indx = obj_aout_external_sym_count (output_bfd);
4330 }
4331 else if ((type & N_TYPE) != N_SETT
4332 && (type & N_TYPE) != N_SETD
4333 && (type & N_TYPE) != N_SETB
4334 && (type & N_TYPE) != N_SETA)
4335 {
4336 switch (discard)
4337 {
4338 case discard_none:
f5fa8ca2 4339 case discard_sec_merge:
252b5132
RH
4340 break;
4341 case discard_l:
4342 if ((type & N_STAB) == 0
4343 && bfd_is_local_label_name (input_bfd, name))
b34976b6 4344 skip = TRUE;
252b5132
RH
4345 break;
4346 case discard_all:
b34976b6 4347 skip = TRUE;
252b5132
RH
4348 break;
4349 }
4350 if (skip)
4351 {
b34976b6 4352 pass = FALSE;
252b5132
RH
4353 continue;
4354 }
4355 }
4356
4357 /* An N_BINCL symbol indicates the start of the stabs
4358 entries for a header file. We need to scan ahead to the
4359 next N_EINCL symbol, ignoring nesting, adding up all the
4360 characters in the symbol names, not including the file
4361 numbers in types (the first number after an open
4362 parenthesis). */
d45913a0 4363 if (type == (int) N_BINCL)
252b5132
RH
4364 {
4365 struct external_nlist *incl_sym;
4366 int nest;
4367 struct aout_link_includes_entry *incl_entry;
4368 struct aout_link_includes_totals *t;
4369
4370 val = 0;
4371 nest = 0;
4372 for (incl_sym = sym + 1; incl_sym < sym_end; incl_sym++)
4373 {
4374 int incl_type;
4375
dc810e39 4376 incl_type = H_GET_8 (input_bfd, incl_sym->e_type);
d45913a0 4377 if (incl_type == (int) N_EINCL)
252b5132
RH
4378 {
4379 if (nest == 0)
4380 break;
4381 --nest;
4382 }
d45913a0 4383 else if (incl_type == (int) N_BINCL)
252b5132
RH
4384 ++nest;
4385 else if (nest == 0)
4386 {
4387 const char *s;
4388
4389 s = strings + GET_WORD (input_bfd, incl_sym->e_strx);
4390 for (; *s != '\0'; s++)
4391 {
4392 val += *s;
4393 if (*s == '(')
4394 {
4395 /* Skip the file number. */
4396 ++s;
3882b010 4397 while (ISDIGIT (*s))
252b5132
RH
4398 ++s;
4399 --s;
4400 }
4401 }
4402 }
4403 }
4404
4405 /* If we have already included a header file with the
4406 same value, then replace this one with an N_EXCL
4407 symbol. */
b34976b6 4408 copy = (bfd_boolean) (! finfo->info->keep_memory);
252b5132 4409 incl_entry = aout_link_includes_lookup (&finfo->includes,
b34976b6 4410 name, TRUE, copy);
252b5132 4411 if (incl_entry == NULL)
b34976b6 4412 return FALSE;
252b5132
RH
4413 for (t = incl_entry->totals; t != NULL; t = t->next)
4414 if (t->total == val)
4415 break;
4416 if (t == NULL)
4417 {
4418 /* This is the first time we have seen this header
4419 file with this set of stabs strings. */
4420 t = ((struct aout_link_includes_totals *)
4421 bfd_hash_allocate (&finfo->includes.root,
4422 sizeof *t));
4423 if (t == NULL)
b34976b6 4424 return FALSE;
252b5132
RH
4425 t->total = val;
4426 t->next = incl_entry->totals;
4427 incl_entry->totals = t;
4428 }
4429 else
4430 {
4431 int *incl_map;
4432
4433 /* This is a duplicate header file. We must change
4434 it to be an N_EXCL entry, and mark all the
4435 included symbols to prevent outputting them. */
d45913a0 4436 type = (int) N_EXCL;
252b5132
RH
4437
4438 nest = 0;
4439 for (incl_sym = sym + 1, incl_map = symbol_map + 1;
4440 incl_sym < sym_end;
4441 incl_sym++, incl_map++)
4442 {
4443 int incl_type;
4444
dc810e39 4445 incl_type = H_GET_8 (input_bfd, incl_sym->e_type);
d45913a0 4446 if (incl_type == (int) N_EINCL)
252b5132
RH
4447 {
4448 if (nest == 0)
4449 {
4450 *incl_map = -1;
4451 break;
4452 }
4453 --nest;
4454 }
d45913a0 4455 else if (incl_type == (int) N_BINCL)
252b5132
RH
4456 ++nest;
4457 else if (nest == 0)
4458 *incl_map = -1;
4459 }
4460 }
4461 }
4462 }
4463
4464 /* Copy this symbol into the list of symbols we are going to
4465 write out. */
dc810e39
AM
4466 H_PUT_8 (output_bfd, type, outsym->e_type);
4467 H_PUT_8 (output_bfd, H_GET_8 (input_bfd, sym->e_other), outsym->e_other);
4468 H_PUT_16 (output_bfd, H_GET_16 (input_bfd, sym->e_desc), outsym->e_desc);
b34976b6 4469 copy = FALSE;
252b5132
RH
4470 if (! finfo->info->keep_memory)
4471 {
4472 /* name points into a string table which we are going to
4473 free. If there is a hash table entry, use that string.
4474 Otherwise, copy name into memory. */
4475 if (h != (struct aout_link_hash_entry *) NULL)
4476 name = h->root.root.string;
4477 else
b34976b6 4478 copy = TRUE;
252b5132
RH
4479 }
4480 strtab_index = add_to_stringtab (output_bfd, finfo->strtab,
4481 name, copy);
4482 if (strtab_index == (bfd_size_type) -1)
b34976b6 4483 return FALSE;
252b5132
RH
4484 PUT_WORD (output_bfd, strtab_index, outsym->e_strx);
4485 PUT_WORD (output_bfd, val, outsym->e_value);
4486 *symbol_map = obj_aout_external_sym_count (output_bfd);
4487 ++obj_aout_external_sym_count (output_bfd);
4488 ++outsym;
4489 }
4490
4491 /* Write out the output symbols we have just constructed. */
4492 if (outsym > finfo->output_syms)
4493 {
dc810e39 4494 bfd_size_type outsym_size;
252b5132
RH
4495
4496 if (bfd_seek (output_bfd, finfo->symoff, SEEK_SET) != 0)
b34976b6 4497 return FALSE;
dc810e39
AM
4498 outsym_size = outsym - finfo->output_syms;
4499 outsym_size *= EXTERNAL_NLIST_SIZE;
4500 if (bfd_bwrite ((PTR) finfo->output_syms, outsym_size, output_bfd)
4501 != outsym_size)
b34976b6 4502 return FALSE;
dc810e39 4503 finfo->symoff += outsym_size;
252b5132
RH
4504 }
4505
b34976b6 4506 return TRUE;
252b5132
RH
4507}
4508
4509/* Write out a symbol that was not associated with an a.out input
4510 object. */
4511
b34976b6 4512static bfd_boolean
252b5132
RH
4513aout_link_write_other_symbol (h, data)
4514 struct aout_link_hash_entry *h;
4515 PTR data;
4516{
4517 struct aout_final_link_info *finfo = (struct aout_final_link_info *) data;
4518 bfd *output_bfd;
4519 int type;
4520 bfd_vma val;
4521 struct external_nlist outsym;
4522 bfd_size_type indx;
dc810e39 4523 bfd_size_type amt;
252b5132 4524
e92d460e
AM
4525 if (h->root.type == bfd_link_hash_warning)
4526 {
4527 h = (struct aout_link_hash_entry *) h->root.u.i.link;
4528 if (h->root.type == bfd_link_hash_new)
b34976b6 4529 return TRUE;
e92d460e
AM
4530 }
4531
252b5132
RH
4532 output_bfd = finfo->output_bfd;
4533
4534 if (aout_backend_info (output_bfd)->write_dynamic_symbol != NULL)
4535 {
4536 if (! ((*aout_backend_info (output_bfd)->write_dynamic_symbol)
4537 (output_bfd, finfo->info, h)))
4538 {
4539 /* FIXME: No way to handle errors. */
4540 abort ();
4541 }
4542 }
4543
4544 if (h->written)
b34976b6 4545 return TRUE;
252b5132 4546
b34976b6 4547 h->written = TRUE;
252b5132
RH
4548
4549 /* An indx of -2 means the symbol must be written. */
4550 if (h->indx != -2
4551 && (finfo->info->strip == strip_all
4552 || (finfo->info->strip == strip_some
4553 && bfd_hash_lookup (finfo->info->keep_hash, h->root.root.string,
b34976b6
AM
4554 FALSE, FALSE) == NULL)))
4555 return TRUE;
252b5132
RH
4556
4557 switch (h->root.type)
4558 {
4559 default:
e92d460e 4560 case bfd_link_hash_warning:
252b5132
RH
4561 abort ();
4562 /* Avoid variable not initialized warnings. */
b34976b6 4563 return TRUE;
252b5132
RH
4564 case bfd_link_hash_new:
4565 /* This can happen for set symbols when sets are not being
4566 built. */
b34976b6 4567 return TRUE;
252b5132
RH
4568 case bfd_link_hash_undefined:
4569 type = N_UNDF | N_EXT;
4570 val = 0;
4571 break;
4572 case bfd_link_hash_defined:
4573 case bfd_link_hash_defweak:
4574 {
4575 asection *sec;
4576
4577 sec = h->root.u.def.section->output_section;
4578 BFD_ASSERT (bfd_is_abs_section (sec)
4579 || sec->owner == output_bfd);
4580 if (sec == obj_textsec (output_bfd))
4581 type = h->root.type == bfd_link_hash_defined ? N_TEXT : N_WEAKT;
4582 else if (sec == obj_datasec (output_bfd))
4583 type = h->root.type == bfd_link_hash_defined ? N_DATA : N_WEAKD;
4584 else if (sec == obj_bsssec (output_bfd))
4585 type = h->root.type == bfd_link_hash_defined ? N_BSS : N_WEAKB;
4586 else
4587 type = h->root.type == bfd_link_hash_defined ? N_ABS : N_WEAKA;
4588 type |= N_EXT;
4589 val = (h->root.u.def.value
4590 + sec->vma
4591 + h->root.u.def.section->output_offset);
4592 }
4593 break;
4594 case bfd_link_hash_common:
4595 type = N_UNDF | N_EXT;
4596 val = h->root.u.c.size;
4597 break;
4598 case bfd_link_hash_undefweak:
4599 type = N_WEAKU;
4600 val = 0;
4601 case bfd_link_hash_indirect:
e92d460e
AM
4602 /* We ignore these symbols, since the indirected symbol is
4603 already in the hash table. */
b34976b6 4604 return TRUE;
252b5132
RH
4605 }
4606
dc810e39
AM
4607 H_PUT_8 (output_bfd, type, outsym.e_type);
4608 H_PUT_8 (output_bfd, 0, outsym.e_other);
4609 H_PUT_16 (output_bfd, 0, outsym.e_desc);
252b5132 4610 indx = add_to_stringtab (output_bfd, finfo->strtab, h->root.root.string,
b34976b6 4611 FALSE);
dc810e39 4612 if (indx == - (bfd_size_type) 1)
252b5132
RH
4613 {
4614 /* FIXME: No way to handle errors. */
4615 abort ();
4616 }
4617 PUT_WORD (output_bfd, indx, outsym.e_strx);
4618 PUT_WORD (output_bfd, val, outsym.e_value);
4619
dc810e39 4620 amt = EXTERNAL_NLIST_SIZE;
252b5132 4621 if (bfd_seek (output_bfd, finfo->symoff, SEEK_SET) != 0
dc810e39 4622 || bfd_bwrite ((PTR) &outsym, amt, output_bfd) != amt)
252b5132
RH
4623 {
4624 /* FIXME: No way to handle errors. */
4625 abort ();
4626 }
4627
4628 finfo->symoff += EXTERNAL_NLIST_SIZE;
4629 h->indx = obj_aout_external_sym_count (output_bfd);
4630 ++obj_aout_external_sym_count (output_bfd);
4631
b34976b6 4632 return TRUE;
252b5132
RH
4633}
4634
4635/* Link an a.out section into the output file. */
4636
b34976b6 4637static bfd_boolean
252b5132
RH
4638aout_link_input_section (finfo, input_bfd, input_section, reloff_ptr,
4639 rel_size)
4640 struct aout_final_link_info *finfo;
4641 bfd *input_bfd;
4642 asection *input_section;
4643 file_ptr *reloff_ptr;
4644 bfd_size_type rel_size;
4645{
4646 bfd_size_type input_size;
4647 PTR relocs;
4648
4649 /* Get the section contents. */
eea6121a 4650 input_size = input_section->size;
252b5132
RH
4651 if (! bfd_get_section_contents (input_bfd, input_section,
4652 (PTR) finfo->contents,
4653 (file_ptr) 0, input_size))
b34976b6 4654 return FALSE;
252b5132
RH
4655
4656 /* Read in the relocs if we haven't already done it. */
4657 if (aout_section_data (input_section) != NULL
4658 && aout_section_data (input_section)->relocs != NULL)
4659 relocs = aout_section_data (input_section)->relocs;
4660 else
4661 {
4662 relocs = finfo->relocs;
4663 if (rel_size > 0)
4664 {
4665 if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0
dc810e39 4666 || bfd_bread (relocs, rel_size, input_bfd) != rel_size)
b34976b6 4667 return FALSE;
252b5132
RH
4668 }
4669 }
4670
4671 /* Relocate the section contents. */
4672 if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE)
4673 {
4674 if (! aout_link_input_section_std (finfo, input_bfd, input_section,
4675 (struct reloc_std_external *) relocs,
4676 rel_size, finfo->contents))
b34976b6 4677 return FALSE;
252b5132
RH
4678 }
4679 else
4680 {
4681 if (! aout_link_input_section_ext (finfo, input_bfd, input_section,
4682 (struct reloc_ext_external *) relocs,
4683 rel_size, finfo->contents))
b34976b6 4684 return FALSE;
252b5132
RH
4685 }
4686
4687 /* Write out the section contents. */
4688 if (! bfd_set_section_contents (finfo->output_bfd,
4689 input_section->output_section,
4690 (PTR) finfo->contents,
dc810e39 4691 (file_ptr) input_section->output_offset,
252b5132 4692 input_size))
b34976b6 4693 return FALSE;
252b5132 4694
1049f94e 4695 /* If we are producing relocatable output, the relocs were
252b5132 4696 modified, and we now write them out. */
1049f94e 4697 if (finfo->info->relocatable && rel_size > 0)
252b5132
RH
4698 {
4699 if (bfd_seek (finfo->output_bfd, *reloff_ptr, SEEK_SET) != 0)
b34976b6 4700 return FALSE;
dc810e39 4701 if (bfd_bwrite (relocs, rel_size, finfo->output_bfd) != rel_size)
b34976b6 4702 return FALSE;
252b5132
RH
4703 *reloff_ptr += rel_size;
4704
4705 /* Assert that the relocs have not run into the symbols, and
4706 that if these are the text relocs they have not run into the
4707 data relocs. */
4708 BFD_ASSERT (*reloff_ptr <= obj_sym_filepos (finfo->output_bfd)
4709 && (reloff_ptr != &finfo->treloff
4710 || (*reloff_ptr
4711 <= obj_datasec (finfo->output_bfd)->rel_filepos)));
4712 }
4713
b34976b6 4714 return TRUE;
252b5132
RH
4715}
4716
4717/* Get the section corresponding to a reloc index. */
4718
4719static INLINE asection *
4720aout_reloc_index_to_section (abfd, indx)
4721 bfd *abfd;
4722 int indx;
4723{
4724 switch (indx & N_TYPE)
4725 {
4726 case N_TEXT:
4727 return obj_textsec (abfd);
4728 case N_DATA:
4729 return obj_datasec (abfd);
4730 case N_BSS:
4731 return obj_bsssec (abfd);
4732 case N_ABS:
4733 case N_UNDF:
4734 return bfd_abs_section_ptr;
4735 default:
4736 abort ();
4737 }
7442e600
ILT
4738 /*NOTREACHED*/
4739 return NULL;
252b5132
RH
4740}
4741
4742/* Relocate an a.out section using standard a.out relocs. */
4743
b34976b6 4744static bfd_boolean
252b5132
RH
4745aout_link_input_section_std (finfo, input_bfd, input_section, relocs,
4746 rel_size, contents)
4747 struct aout_final_link_info *finfo;
4748 bfd *input_bfd;
4749 asection *input_section;
4750 struct reloc_std_external *relocs;
4751 bfd_size_type rel_size;
4752 bfd_byte *contents;
4753{
b34976b6
AM
4754 bfd_boolean (*check_dynamic_reloc)
4755 PARAMS ((struct bfd_link_info *, bfd *, asection *,
4756 struct aout_link_hash_entry *, PTR, bfd_byte *, bfd_boolean *,
4757 bfd_vma *));
252b5132 4758 bfd *output_bfd;
1049f94e 4759 bfd_boolean relocatable;
252b5132
RH
4760 struct external_nlist *syms;
4761 char *strings;
4762 struct aout_link_hash_entry **sym_hashes;
4763 int *symbol_map;
4764 bfd_size_type reloc_count;
4765 register struct reloc_std_external *rel;
4766 struct reloc_std_external *rel_end;
4767
4768 output_bfd = finfo->output_bfd;
4769 check_dynamic_reloc = aout_backend_info (output_bfd)->check_dynamic_reloc;
4770
4771 BFD_ASSERT (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE);
4772 BFD_ASSERT (input_bfd->xvec->header_byteorder
4773 == output_bfd->xvec->header_byteorder);
4774
1049f94e 4775 relocatable = finfo->info->relocatable;
252b5132
RH
4776 syms = obj_aout_external_syms (input_bfd);
4777 strings = obj_aout_external_strings (input_bfd);
4778 sym_hashes = obj_aout_sym_hashes (input_bfd);
4779 symbol_map = finfo->symbol_map;
4780
4781 reloc_count = rel_size / RELOC_STD_SIZE;
4782 rel = relocs;
4783 rel_end = rel + reloc_count;
4784 for (; rel < rel_end; rel++)
4785 {
4786 bfd_vma r_addr;
4787 int r_index;
4788 int r_extern;
4789 int r_pcrel;
4790 int r_baserel = 0;
4791 reloc_howto_type *howto;
4792 struct aout_link_hash_entry *h = NULL;
4793 bfd_vma relocation;
4794 bfd_reloc_status_type r;
4795
4796 r_addr = GET_SWORD (input_bfd, rel->r_address);
4797
4798#ifdef MY_reloc_howto
923f08ff 4799 howto = MY_reloc_howto (input_bfd, rel, r_index, r_extern, r_pcrel);
0ef5a5bd 4800#else
252b5132
RH
4801 {
4802 int r_jmptable;
4803 int r_relative;
4804 int r_length;
4805 unsigned int howto_idx;
4806
4807 if (bfd_header_big_endian (input_bfd))
4808 {
d45913a0
DA
4809 r_index = (((unsigned int) rel->r_index[0] << 16)
4810 | ((unsigned int) rel->r_index[1] << 8)
252b5132
RH
4811 | rel->r_index[2]);
4812 r_extern = (0 != (rel->r_type[0] & RELOC_STD_BITS_EXTERN_BIG));
4813 r_pcrel = (0 != (rel->r_type[0] & RELOC_STD_BITS_PCREL_BIG));
4814 r_baserel = (0 != (rel->r_type[0] & RELOC_STD_BITS_BASEREL_BIG));
4815 r_jmptable= (0 != (rel->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG));
4816 r_relative= (0 != (rel->r_type[0] & RELOC_STD_BITS_RELATIVE_BIG));
4817 r_length = ((rel->r_type[0] & RELOC_STD_BITS_LENGTH_BIG)
4818 >> RELOC_STD_BITS_LENGTH_SH_BIG);
4819 }
4820 else
4821 {
d45913a0
DA
4822 r_index = (((unsigned int) rel->r_index[2] << 16)
4823 | ((unsigned int) rel->r_index[1] << 8)
252b5132
RH
4824 | rel->r_index[0]);
4825 r_extern = (0 != (rel->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE));
4826 r_pcrel = (0 != (rel->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
4827 r_baserel = (0 != (rel->r_type[0]
4828 & RELOC_STD_BITS_BASEREL_LITTLE));
4829 r_jmptable= (0 != (rel->r_type[0]
4830 & RELOC_STD_BITS_JMPTABLE_LITTLE));
4831 r_relative= (0 != (rel->r_type[0]
4832 & RELOC_STD_BITS_RELATIVE_LITTLE));
4833 r_length = ((rel->r_type[0] & RELOC_STD_BITS_LENGTH_LITTLE)
4834 >> RELOC_STD_BITS_LENGTH_SH_LITTLE);
4835 }
4836
4837 howto_idx = (r_length + 4 * r_pcrel + 8 * r_baserel
4838 + 16 * r_jmptable + 32 * r_relative);
4839 BFD_ASSERT (howto_idx < TABLE_SIZE (howto_table_std));
4840 howto = howto_table_std + howto_idx;
4841 }
4842#endif
4843
1049f94e 4844 if (relocatable)
252b5132 4845 {
1049f94e 4846 /* We are generating a relocatable output file, and must
252b5132
RH
4847 modify the reloc accordingly. */
4848 if (r_extern)
4849 {
4850 /* If we know the symbol this relocation is against,
4851 convert it into a relocation against a section. This
4852 is what the native linker does. */
4853 h = sym_hashes[r_index];
4854 if (h != (struct aout_link_hash_entry *) NULL
4855 && (h->root.type == bfd_link_hash_defined
4856 || h->root.type == bfd_link_hash_defweak))
4857 {
4858 asection *output_section;
4859
4860 /* Change the r_extern value. */
4861 if (bfd_header_big_endian (output_bfd))
4862 rel->r_type[0] &=~ RELOC_STD_BITS_EXTERN_BIG;
4863 else
4864 rel->r_type[0] &=~ RELOC_STD_BITS_EXTERN_LITTLE;
4865
4866 /* Compute a new r_index. */
4867 output_section = h->root.u.def.section->output_section;
4868 if (output_section == obj_textsec (output_bfd))
4869 r_index = N_TEXT;
4870 else if (output_section == obj_datasec (output_bfd))
4871 r_index = N_DATA;
4872 else if (output_section == obj_bsssec (output_bfd))
4873 r_index = N_BSS;
4874 else
4875 r_index = N_ABS;
4876
4877 /* Add the symbol value and the section VMA to the
4878 addend stored in the contents. */
4879 relocation = (h->root.u.def.value
4880 + output_section->vma
4881 + h->root.u.def.section->output_offset);
4882 }
4883 else
4884 {
4885 /* We must change r_index according to the symbol
4886 map. */
4887 r_index = symbol_map[r_index];
4888
4889 if (r_index == -1)
4890 {
4891 if (h != NULL)
4892 {
4893 /* We decided to strip this symbol, but it
4894 turns out that we can't. Note that we
4895 lose the other and desc information here.
4896 I don't think that will ever matter for a
4897 global symbol. */
4898 if (h->indx < 0)
4899 {
4900 h->indx = -2;
b34976b6 4901 h->written = FALSE;
252b5132
RH
4902 if (! aout_link_write_other_symbol (h,
4903 (PTR) finfo))
b34976b6 4904 return FALSE;
252b5132
RH
4905 }
4906 r_index = h->indx;
4907 }
4908 else
4909 {
4910 const char *name;
4911
4912 name = strings + GET_WORD (input_bfd,
4913 syms[r_index].e_strx);
4914 if (! ((*finfo->info->callbacks->unattached_reloc)
4915 (finfo->info, name, input_bfd, input_section,
4916 r_addr)))
b34976b6 4917 return FALSE;
252b5132
RH
4918 r_index = 0;
4919 }
4920 }
4921
4922 relocation = 0;
4923 }
4924
4925 /* Write out the new r_index value. */
4926 if (bfd_header_big_endian (output_bfd))
4927 {
4928 rel->r_index[0] = r_index >> 16;
4929 rel->r_index[1] = r_index >> 8;
4930 rel->r_index[2] = r_index;
4931 }
4932 else
4933 {
4934 rel->r_index[2] = r_index >> 16;
4935 rel->r_index[1] = r_index >> 8;
4936 rel->r_index[0] = r_index;
4937 }
4938 }
4939 else
4940 {
4941 asection *section;
4942
4943 /* This is a relocation against a section. We must
4944 adjust by the amount that the section moved. */
4945 section = aout_reloc_index_to_section (input_bfd, r_index);
4946 relocation = (section->output_section->vma
4947 + section->output_offset
4948 - section->vma);
4949 }
4950
4951 /* Change the address of the relocation. */
4952 PUT_WORD (output_bfd,
4953 r_addr + input_section->output_offset,
4954 rel->r_address);
4955
4956 /* Adjust a PC relative relocation by removing the reference
4957 to the original address in the section and including the
4958 reference to the new address. */
4959 if (r_pcrel)
4960 relocation -= (input_section->output_section->vma
4961 + input_section->output_offset
4962 - input_section->vma);
4963
4964#ifdef MY_relocatable_reloc
4965 MY_relocatable_reloc (howto, output_bfd, rel, relocation, r_addr);
4966#endif
4967
4968 if (relocation == 0)
4969 r = bfd_reloc_ok;
4970 else
4971 r = MY_relocate_contents (howto,
4972 input_bfd, relocation,
4973 contents + r_addr);
4974 }
4975 else
4976 {
b34976b6 4977 bfd_boolean hundef;
252b5132
RH
4978
4979 /* We are generating an executable, and must do a full
4980 relocation. */
b34976b6 4981 hundef = FALSE;
252b5132
RH
4982
4983 if (r_extern)
4984 {
4985 h = sym_hashes[r_index];
4986
4987 if (h != (struct aout_link_hash_entry *) NULL
4988 && (h->root.type == bfd_link_hash_defined
4989 || h->root.type == bfd_link_hash_defweak))
4990 {
4991 relocation = (h->root.u.def.value
4992 + h->root.u.def.section->output_section->vma
4993 + h->root.u.def.section->output_offset);
4994 }
4995 else if (h != (struct aout_link_hash_entry *) NULL
4996 && h->root.type == bfd_link_hash_undefweak)
4997 relocation = 0;
4998 else
4999 {
b34976b6 5000 hundef = TRUE;
252b5132
RH
5001 relocation = 0;
5002 }
5003 }
5004 else
5005 {
5006 asection *section;
5007
5008 section = aout_reloc_index_to_section (input_bfd, r_index);
5009 relocation = (section->output_section->vma
5010 + section->output_offset
5011 - section->vma);
5012 if (r_pcrel)
5013 relocation += input_section->vma;
5014 }
5015
5016 if (check_dynamic_reloc != NULL)
5017 {
b34976b6 5018 bfd_boolean skip;
252b5132
RH
5019
5020 if (! ((*check_dynamic_reloc)
5021 (finfo->info, input_bfd, input_section, h,
5022 (PTR) rel, contents, &skip, &relocation)))
b34976b6 5023 return FALSE;
252b5132
RH
5024 if (skip)
5025 continue;
5026 }
5027
5028 /* Now warn if a global symbol is undefined. We could not
5029 do this earlier, because check_dynamic_reloc might want
5030 to skip this reloc. */
5031 if (hundef && ! finfo->info->shared && ! r_baserel)
5032 {
5033 const char *name;
5034
5035 if (h != NULL)
5036 name = h->root.root.string;
5037 else
5038 name = strings + GET_WORD (input_bfd, syms[r_index].e_strx);
5039 if (! ((*finfo->info->callbacks->undefined_symbol)
5cc7c785 5040 (finfo->info, name, input_bfd, input_section,
b34976b6
AM
5041 r_addr, TRUE)))
5042 return FALSE;
252b5132
RH
5043 }
5044
5045 r = MY_final_link_relocate (howto,
5046 input_bfd, input_section,
5047 contents, r_addr, relocation,
5048 (bfd_vma) 0);
5049 }
5050
5051 if (r != bfd_reloc_ok)
5052 {
5053 switch (r)
5054 {
5055 default:
5056 case bfd_reloc_outofrange:
5057 abort ();
5058 case bfd_reloc_overflow:
5059 {
5060 const char *name;
5061
5062 if (h != NULL)
dfeffb9f 5063 name = NULL;
252b5132
RH
5064 else if (r_extern)
5065 name = strings + GET_WORD (input_bfd,
5066 syms[r_index].e_strx);
5067 else
5068 {
5069 asection *s;
5070
5071 s = aout_reloc_index_to_section (input_bfd, r_index);
5072 name = bfd_section_name (input_bfd, s);
5073 }
5074 if (! ((*finfo->info->callbacks->reloc_overflow)
dfeffb9f
L
5075 (finfo->info, (h ? &h->root : NULL), name,
5076 howto->name, (bfd_vma) 0, input_bfd,
5077 input_section, r_addr)))
b34976b6 5078 return FALSE;
252b5132
RH
5079 }
5080 break;
5081 }
5082 }
5083 }
5084
b34976b6 5085 return TRUE;
252b5132
RH
5086}
5087
5088/* Relocate an a.out section using extended a.out relocs. */
5089
b34976b6 5090static bfd_boolean
252b5132
RH
5091aout_link_input_section_ext (finfo, input_bfd, input_section, relocs,
5092 rel_size, contents)
5093 struct aout_final_link_info *finfo;
5094 bfd *input_bfd;
5095 asection *input_section;
5096 struct reloc_ext_external *relocs;
5097 bfd_size_type rel_size;
5098 bfd_byte *contents;
5099{
b34976b6
AM
5100 bfd_boolean (*check_dynamic_reloc)
5101 PARAMS ((struct bfd_link_info *, bfd *, asection *,
5102 struct aout_link_hash_entry *, PTR, bfd_byte *, bfd_boolean *,
5103 bfd_vma *));
252b5132 5104 bfd *output_bfd;
1049f94e 5105 bfd_boolean relocatable;
252b5132
RH
5106 struct external_nlist *syms;
5107 char *strings;
5108 struct aout_link_hash_entry **sym_hashes;
5109 int *symbol_map;
5110 bfd_size_type reloc_count;
5111 register struct reloc_ext_external *rel;
5112 struct reloc_ext_external *rel_end;
5113
5114 output_bfd = finfo->output_bfd;
5115 check_dynamic_reloc = aout_backend_info (output_bfd)->check_dynamic_reloc;
5116
5117 BFD_ASSERT (obj_reloc_entry_size (input_bfd) == RELOC_EXT_SIZE);
5118 BFD_ASSERT (input_bfd->xvec->header_byteorder
5119 == output_bfd->xvec->header_byteorder);
5120
1049f94e 5121 relocatable = finfo->info->relocatable;
252b5132
RH
5122 syms = obj_aout_external_syms (input_bfd);
5123 strings = obj_aout_external_strings (input_bfd);
5124 sym_hashes = obj_aout_sym_hashes (input_bfd);
5125 symbol_map = finfo->symbol_map;
5126
5127 reloc_count = rel_size / RELOC_EXT_SIZE;
5128 rel = relocs;
5129 rel_end = rel + reloc_count;
5130 for (; rel < rel_end; rel++)
5131 {
5132 bfd_vma r_addr;
5133 int r_index;
5134 int r_extern;
5135 unsigned int r_type;
5136 bfd_vma r_addend;
5137 struct aout_link_hash_entry *h = NULL;
5138 asection *r_section = NULL;
5139 bfd_vma relocation;
5140
5141 r_addr = GET_SWORD (input_bfd, rel->r_address);
5142
5143 if (bfd_header_big_endian (input_bfd))
5144 {
d45913a0
DA
5145 r_index = (((unsigned int) rel->r_index[0] << 16)
5146 | ((unsigned int) rel->r_index[1] << 8)
252b5132
RH
5147 | rel->r_index[2]);
5148 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG));
5149 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
5150 >> RELOC_EXT_BITS_TYPE_SH_BIG);
5151 }
5152 else
5153 {
d45913a0
DA
5154 r_index = (((unsigned int) rel->r_index[2] << 16)
5155 | ((unsigned int) rel->r_index[1] << 8)
252b5132
RH
5156 | rel->r_index[0]);
5157 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
5158 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
5159 >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
5160 }
5161
5162 r_addend = GET_SWORD (input_bfd, rel->r_addend);
5163
5164 BFD_ASSERT (r_type < TABLE_SIZE (howto_table_ext));
5165
1049f94e 5166 if (relocatable)
252b5132 5167 {
1049f94e 5168 /* We are generating a relocatable output file, and must
252b5132
RH
5169 modify the reloc accordingly. */
5170 if (r_extern
d45913a0
DA
5171 || r_type == (unsigned int) RELOC_BASE10
5172 || r_type == (unsigned int) RELOC_BASE13
5173 || r_type == (unsigned int) RELOC_BASE22)
252b5132
RH
5174 {
5175 /* If we know the symbol this relocation is against,
5176 convert it into a relocation against a section. This
5177 is what the native linker does. */
d45913a0
DA
5178 if (r_type == (unsigned int) RELOC_BASE10
5179 || r_type == (unsigned int) RELOC_BASE13
5180 || r_type == (unsigned int) RELOC_BASE22)
252b5132
RH
5181 h = NULL;
5182 else
5183 h = sym_hashes[r_index];
5184 if (h != (struct aout_link_hash_entry *) NULL
5185 && (h->root.type == bfd_link_hash_defined
5186 || h->root.type == bfd_link_hash_defweak))
5187 {
5188 asection *output_section;
5189
5190 /* Change the r_extern value. */
5191 if (bfd_header_big_endian (output_bfd))
5192 rel->r_type[0] &=~ RELOC_EXT_BITS_EXTERN_BIG;
5193 else
5194 rel->r_type[0] &=~ RELOC_EXT_BITS_EXTERN_LITTLE;
5195
5196 /* Compute a new r_index. */
5197 output_section = h->root.u.def.section->output_section;
5198 if (output_section == obj_textsec (output_bfd))
5199 r_index = N_TEXT;
5200 else if (output_section == obj_datasec (output_bfd))
5201 r_index = N_DATA;
5202 else if (output_section == obj_bsssec (output_bfd))
5203 r_index = N_BSS;
5204 else
5205 r_index = N_ABS;
5206
5207 /* Add the symbol value and the section VMA to the
5208 addend. */
5209 relocation = (h->root.u.def.value
5210 + output_section->vma
5211 + h->root.u.def.section->output_offset);
5212
5213 /* Now RELOCATION is the VMA of the final
5214 destination. If this is a PC relative reloc,
5215 then ADDEND is the negative of the source VMA.
5216 We want to set ADDEND to the difference between
5217 the destination VMA and the source VMA, which
5218 means we must adjust RELOCATION by the change in
5219 the source VMA. This is done below. */
5220 }
5221 else
5222 {
5223 /* We must change r_index according to the symbol
5224 map. */
5225 r_index = symbol_map[r_index];
5226
5227 if (r_index == -1)
5228 {
5229 if (h != NULL)
5230 {
5231 /* We decided to strip this symbol, but it
5232 turns out that we can't. Note that we
5233 lose the other and desc information here.
5234 I don't think that will ever matter for a
5235 global symbol. */
5236 if (h->indx < 0)
5237 {
5238 h->indx = -2;
b34976b6 5239 h->written = FALSE;
252b5132
RH
5240 if (! aout_link_write_other_symbol (h,
5241 (PTR) finfo))
b34976b6 5242 return FALSE;
252b5132
RH
5243 }
5244 r_index = h->indx;
5245 }
5246 else
5247 {
5248 const char *name;
5249
5250 name = strings + GET_WORD (input_bfd,
5251 syms[r_index].e_strx);
5252 if (! ((*finfo->info->callbacks->unattached_reloc)
5253 (finfo->info, name, input_bfd, input_section,
5254 r_addr)))
b34976b6 5255 return FALSE;
252b5132
RH
5256 r_index = 0;
5257 }
5258 }
5259
5260 relocation = 0;
5261
5262 /* If this is a PC relative reloc, then the addend
5263 is the negative of the source VMA. We must
5264 adjust it by the change in the source VMA. This
5265 is done below. */
5266 }
5267
5268 /* Write out the new r_index value. */
5269 if (bfd_header_big_endian (output_bfd))
5270 {
5271 rel->r_index[0] = r_index >> 16;
5272 rel->r_index[1] = r_index >> 8;
5273 rel->r_index[2] = r_index;
5274 }
5275 else
5276 {
5277 rel->r_index[2] = r_index >> 16;
5278 rel->r_index[1] = r_index >> 8;
5279 rel->r_index[0] = r_index;
5280 }
5281 }
5282 else
5283 {
5284 /* This is a relocation against a section. We must
5285 adjust by the amount that the section moved. */
5286 r_section = aout_reloc_index_to_section (input_bfd, r_index);
5287 relocation = (r_section->output_section->vma
5288 + r_section->output_offset
5289 - r_section->vma);
5290
5291 /* If this is a PC relative reloc, then the addend is
5292 the difference in VMA between the destination and the
5293 source. We have just adjusted for the change in VMA
5294 of the destination, so we must also adjust by the
5295 change in VMA of the source. This is done below. */
5296 }
5297
5298 /* As described above, we must always adjust a PC relative
5299 reloc by the change in VMA of the source. However, if
5300 pcrel_offset is set, then the addend does not include the
5301 location within the section, in which case we don't need
5302 to adjust anything. */
5303 if (howto_table_ext[r_type].pc_relative
5304 && ! howto_table_ext[r_type].pcrel_offset)
5305 relocation -= (input_section->output_section->vma
5306 + input_section->output_offset
5307 - input_section->vma);
5308
5309 /* Change the addend if necessary. */
5310 if (relocation != 0)
5311 PUT_WORD (output_bfd, r_addend + relocation, rel->r_addend);
5312
5313 /* Change the address of the relocation. */
5314 PUT_WORD (output_bfd,
5315 r_addr + input_section->output_offset,
5316 rel->r_address);
5317 }
5318 else
5319 {
b34976b6 5320 bfd_boolean hundef;
252b5132
RH
5321 bfd_reloc_status_type r;
5322
5323 /* We are generating an executable, and must do a full
5324 relocation. */
b34976b6 5325 hundef = FALSE;
252b5132
RH
5326
5327 if (r_extern)
5328 {
5329 h = sym_hashes[r_index];
5330
5331 if (h != (struct aout_link_hash_entry *) NULL
5332 && (h->root.type == bfd_link_hash_defined
5333 || h->root.type == bfd_link_hash_defweak))
5334 {
5335 relocation = (h->root.u.def.value
5336 + h->root.u.def.section->output_section->vma
5337 + h->root.u.def.section->output_offset);
5338 }
5339 else if (h != (struct aout_link_hash_entry *) NULL
5340 && h->root.type == bfd_link_hash_undefweak)
5341 relocation = 0;
5342 else
5343 {
b34976b6 5344 hundef = TRUE;
252b5132
RH
5345 relocation = 0;
5346 }
5347 }
d45913a0
DA
5348 else if (r_type == (unsigned int) RELOC_BASE10
5349 || r_type == (unsigned int) RELOC_BASE13
5350 || r_type == (unsigned int) RELOC_BASE22)
252b5132
RH
5351 {
5352 struct external_nlist *sym;
5353 int type;
5354
5355 /* For base relative relocs, r_index is always an index
5356 into the symbol table, even if r_extern is 0. */
5357 sym = syms + r_index;
dc810e39 5358 type = H_GET_8 (input_bfd, sym->e_type);
252b5132
RH
5359 if ((type & N_TYPE) == N_TEXT
5360 || type == N_WEAKT)
5361 r_section = obj_textsec (input_bfd);
5362 else if ((type & N_TYPE) == N_DATA
5363 || type == N_WEAKD)
5364 r_section = obj_datasec (input_bfd);
5365 else if ((type & N_TYPE) == N_BSS
5366 || type == N_WEAKB)
5367 r_section = obj_bsssec (input_bfd);
5368 else if ((type & N_TYPE) == N_ABS
5369 || type == N_WEAKA)
5370 r_section = bfd_abs_section_ptr;
5371 else
5372 abort ();
5373 relocation = (r_section->output_section->vma
5374 + r_section->output_offset
5375 + (GET_WORD (input_bfd, sym->e_value)
5376 - r_section->vma));
5377 }
5378 else
5379 {
5380 r_section = aout_reloc_index_to_section (input_bfd, r_index);
5381
5382 /* If this is a PC relative reloc, then R_ADDEND is the
5383 difference between the two vmas, or
5384 old_dest_sec + old_dest_off - (old_src_sec + old_src_off)
5385 where
5386 old_dest_sec == section->vma
5387 and
5388 old_src_sec == input_section->vma
5389 and
5390 old_src_off == r_addr
5391
5392 _bfd_final_link_relocate expects RELOCATION +
5393 R_ADDEND to be the VMA of the destination minus
5394 r_addr (the minus r_addr is because this relocation
5395 is not pcrel_offset, which is a bit confusing and
5396 should, perhaps, be changed), or
5397 new_dest_sec
5398 where
5399 new_dest_sec == output_section->vma + output_offset
5400 We arrange for this to happen by setting RELOCATION to
5401 new_dest_sec + old_src_sec - old_dest_sec
5402
5403 If this is not a PC relative reloc, then R_ADDEND is
5404 simply the VMA of the destination, so we set
5405 RELOCATION to the change in the destination VMA, or
5406 new_dest_sec - old_dest_sec
5407 */
5408 relocation = (r_section->output_section->vma
5409 + r_section->output_offset
5410 - r_section->vma);
5411 if (howto_table_ext[r_type].pc_relative)
5412 relocation += input_section->vma;
5413 }
5414
5415 if (check_dynamic_reloc != NULL)
5416 {
b34976b6 5417 bfd_boolean skip;
252b5132
RH
5418
5419 if (! ((*check_dynamic_reloc)
5420 (finfo->info, input_bfd, input_section, h,
5421 (PTR) rel, contents, &skip, &relocation)))
b34976b6 5422 return FALSE;
252b5132
RH
5423 if (skip)
5424 continue;
5425 }
5426
5427 /* Now warn if a global symbol is undefined. We could not
5428 do this earlier, because check_dynamic_reloc might want
5429 to skip this reloc. */
5430 if (hundef
5431 && ! finfo->info->shared
d45913a0
DA
5432 && r_type != (unsigned int) RELOC_BASE10
5433 && r_type != (unsigned int) RELOC_BASE13
5434 && r_type != (unsigned int) RELOC_BASE22)
252b5132
RH
5435 {
5436 const char *name;
5437
5438 if (h != NULL)
5439 name = h->root.root.string;
5440 else
5441 name = strings + GET_WORD (input_bfd, syms[r_index].e_strx);
5442 if (! ((*finfo->info->callbacks->undefined_symbol)
5cc7c785 5443 (finfo->info, name, input_bfd, input_section,
b34976b6
AM
5444 r_addr, TRUE)))
5445 return FALSE;
252b5132
RH
5446 }
5447
d45913a0 5448 if (r_type != (unsigned int) RELOC_SPARC_REV32)
252b5132
RH
5449 r = MY_final_link_relocate (howto_table_ext + r_type,
5450 input_bfd, input_section,
5451 contents, r_addr, relocation,
5452 r_addend);
5453 else
5454 {
5455 bfd_vma x;
5456
5457 x = bfd_get_32 (input_bfd, contents + r_addr);
5458 x = x + relocation + r_addend;
5459 bfd_putl32 (/*input_bfd,*/ x, contents + r_addr);
5460 r = bfd_reloc_ok;
5461 }
5462
5463 if (r != bfd_reloc_ok)
5464 {
5465 switch (r)
5466 {
5467 default:
5468 case bfd_reloc_outofrange:
5469 abort ();
5470 case bfd_reloc_overflow:
5471 {
5472 const char *name;
5473
5474 if (h != NULL)
dfeffb9f 5475 name = NULL;
252b5132 5476 else if (r_extern
d45913a0
DA
5477 || r_type == (unsigned int) RELOC_BASE10
5478 || r_type == (unsigned int) RELOC_BASE13
5479 || r_type == (unsigned int) RELOC_BASE22)
252b5132
RH
5480 name = strings + GET_WORD (input_bfd,
5481 syms[r_index].e_strx);
5482 else
5483 {
5484 asection *s;
5485
5486 s = aout_reloc_index_to_section (input_bfd, r_index);
5487 name = bfd_section_name (input_bfd, s);
5488 }
5489 if (! ((*finfo->info->callbacks->reloc_overflow)
dfeffb9f
L
5490 (finfo->info, (h ? &h->root : NULL), name,
5491 howto_table_ext[r_type].name,
252b5132 5492 r_addend, input_bfd, input_section, r_addr)))
b34976b6 5493 return FALSE;
252b5132
RH
5494 }
5495 break;
5496 }
5497 }
5498 }
5499 }
5500
b34976b6 5501 return TRUE;
252b5132
RH
5502}
5503
5504/* Handle a link order which is supposed to generate a reloc. */
5505
b34976b6 5506static bfd_boolean
252b5132
RH
5507aout_link_reloc_link_order (finfo, o, p)
5508 struct aout_final_link_info *finfo;
5509 asection *o;
5510 struct bfd_link_order *p;
5511{
5512 struct bfd_link_order_reloc *pr;
5513 int r_index;
5514 int r_extern;
5515 reloc_howto_type *howto;
7442e600 5516 file_ptr *reloff_ptr = NULL;
252b5132
RH
5517 struct reloc_std_external srel;
5518 struct reloc_ext_external erel;
5519 PTR rel_ptr;
dc810e39 5520 bfd_size_type amt;
252b5132
RH
5521
5522 pr = p->u.reloc.p;
5523
5524 if (p->type == bfd_section_reloc_link_order)
5525 {
5526 r_extern = 0;
5527 if (bfd_is_abs_section (pr->u.section))
5528 r_index = N_ABS | N_EXT;
5529 else
5530 {
5531 BFD_ASSERT (pr->u.section->owner == finfo->output_bfd);
5532 r_index = pr->u.section->target_index;
5533 }
5534 }
5535 else
5536 {
5537 struct aout_link_hash_entry *h;
5538
5539 BFD_ASSERT (p->type == bfd_symbol_reloc_link_order);
5540 r_extern = 1;
5541 h = ((struct aout_link_hash_entry *)
5542 bfd_wrapped_link_hash_lookup (finfo->output_bfd, finfo->info,
b34976b6 5543 pr->u.name, FALSE, FALSE, TRUE));
252b5132
RH
5544 if (h != (struct aout_link_hash_entry *) NULL
5545 && h->indx >= 0)
5546 r_index = h->indx;
5547 else if (h != NULL)
5548 {
5549 /* We decided to strip this symbol, but it turns out that we
5550 can't. Note that we lose the other and desc information
5551 here. I don't think that will ever matter for a global
5552 symbol. */
5553 h->indx = -2;
b34976b6 5554 h->written = FALSE;
252b5132 5555 if (! aout_link_write_other_symbol (h, (PTR) finfo))
b34976b6 5556 return FALSE;
252b5132
RH
5557 r_index = h->indx;
5558 }
5559 else
5560 {
5561 if (! ((*finfo->info->callbacks->unattached_reloc)
5562 (finfo->info, pr->u.name, (bfd *) NULL,
5563 (asection *) NULL, (bfd_vma) 0)))
b34976b6 5564 return FALSE;
252b5132
RH
5565 r_index = 0;
5566 }
5567 }
5568
5569 howto = bfd_reloc_type_lookup (finfo->output_bfd, pr->reloc);
5570 if (howto == 0)
5571 {
5572 bfd_set_error (bfd_error_bad_value);
b34976b6 5573 return FALSE;
252b5132
RH
5574 }
5575
5576 if (o == obj_textsec (finfo->output_bfd))
5577 reloff_ptr = &finfo->treloff;
5578 else if (o == obj_datasec (finfo->output_bfd))
5579 reloff_ptr = &finfo->dreloff;
5580 else
5581 abort ();
5582
5583 if (obj_reloc_entry_size (finfo->output_bfd) == RELOC_STD_SIZE)
5584 {
5585#ifdef MY_put_reloc
923f08ff
AM
5586 MY_put_reloc (finfo->output_bfd, r_extern, r_index, p->offset, howto,
5587 &srel);
252b5132
RH
5588#else
5589 {
5590 int r_pcrel;
5591 int r_baserel;
5592 int r_jmptable;
5593 int r_relative;
5594 int r_length;
5595
d45913a0 5596 r_pcrel = (int) howto->pc_relative;
252b5132
RH
5597 r_baserel = (howto->type & 8) != 0;
5598 r_jmptable = (howto->type & 16) != 0;
5599 r_relative = (howto->type & 32) != 0;
5600 r_length = howto->size;
5601
5602 PUT_WORD (finfo->output_bfd, p->offset, srel.r_address);
5603 if (bfd_header_big_endian (finfo->output_bfd))
5604 {
5605 srel.r_index[0] = r_index >> 16;
5606 srel.r_index[1] = r_index >> 8;
5607 srel.r_index[2] = r_index;
5608 srel.r_type[0] =
5609 ((r_extern ? RELOC_STD_BITS_EXTERN_BIG : 0)
5610 | (r_pcrel ? RELOC_STD_BITS_PCREL_BIG : 0)
5611 | (r_baserel ? RELOC_STD_BITS_BASEREL_BIG : 0)
5612 | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_BIG : 0)
5613 | (r_relative ? RELOC_STD_BITS_RELATIVE_BIG : 0)
5614 | (r_length << RELOC_STD_BITS_LENGTH_SH_BIG));
5615 }
5616 else
5617 {
5618 srel.r_index[2] = r_index >> 16;
5619 srel.r_index[1] = r_index >> 8;
5620 srel.r_index[0] = r_index;
5621 srel.r_type[0] =
5622 ((r_extern ? RELOC_STD_BITS_EXTERN_LITTLE : 0)
5623 | (r_pcrel ? RELOC_STD_BITS_PCREL_LITTLE : 0)
5624 | (r_baserel ? RELOC_STD_BITS_BASEREL_LITTLE : 0)
5625 | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_LITTLE : 0)
5626 | (r_relative ? RELOC_STD_BITS_RELATIVE_LITTLE : 0)
5627 | (r_length << RELOC_STD_BITS_LENGTH_SH_LITTLE));
5628 }
5629 }
5630#endif
5631 rel_ptr = (PTR) &srel;
5632
5633 /* We have to write the addend into the object file, since
5634 standard a.out relocs are in place. It would be more
5635 reliable if we had the current contents of the file here,
5636 rather than assuming zeroes, but we can't read the file since
5637 it was opened using bfd_openw. */
5638 if (pr->addend != 0)
5639 {
5640 bfd_size_type size;
5641 bfd_reloc_status_type r;
5642 bfd_byte *buf;
b34976b6 5643 bfd_boolean ok;
252b5132
RH
5644
5645 size = bfd_get_reloc_size (howto);
5646 buf = (bfd_byte *) bfd_zmalloc (size);
5647 if (buf == (bfd_byte *) NULL)
b34976b6 5648 return FALSE;
252b5132 5649 r = MY_relocate_contents (howto, finfo->output_bfd,
dc810e39 5650 (bfd_vma) pr->addend, buf);
252b5132
RH
5651 switch (r)
5652 {
5653 case bfd_reloc_ok:
5654 break;
5655 default:
5656 case bfd_reloc_outofrange:
5657 abort ();
5658 case bfd_reloc_overflow:
5659 if (! ((*finfo->info->callbacks->reloc_overflow)
dfeffb9f 5660 (finfo->info, NULL,
252b5132
RH
5661 (p->type == bfd_section_reloc_link_order
5662 ? bfd_section_name (finfo->output_bfd,
5663 pr->u.section)
5664 : pr->u.name),
5665 howto->name, pr->addend, (bfd *) NULL,
5666 (asection *) NULL, (bfd_vma) 0)))
5667 {
5668 free (buf);
b34976b6 5669 return FALSE;
252b5132
RH
5670 }
5671 break;
5672 }
dc810e39
AM
5673 ok = bfd_set_section_contents (finfo->output_bfd, o, (PTR) buf,
5674 (file_ptr) p->offset, size);
252b5132
RH
5675 free (buf);
5676 if (! ok)
b34976b6 5677 return FALSE;
252b5132
RH
5678 }
5679 }
5680 else
5681 {
1642229e
HPN
5682#ifdef MY_put_ext_reloc
5683 MY_put_ext_reloc (finfo->output_bfd, r_extern, r_index, p->offset,
5684 howto, &erel, pr->addend);
5685#else
252b5132
RH
5686 PUT_WORD (finfo->output_bfd, p->offset, erel.r_address);
5687
5688 if (bfd_header_big_endian (finfo->output_bfd))
5689 {
5690 erel.r_index[0] = r_index >> 16;
5691 erel.r_index[1] = r_index >> 8;
5692 erel.r_index[2] = r_index;
5693 erel.r_type[0] =
5694 ((r_extern ? RELOC_EXT_BITS_EXTERN_BIG : 0)
5695 | (howto->type << RELOC_EXT_BITS_TYPE_SH_BIG));
5696 }
5697 else
5698 {
5699 erel.r_index[2] = r_index >> 16;
5700 erel.r_index[1] = r_index >> 8;
5701 erel.r_index[0] = r_index;
5702 erel.r_type[0] =
5703 (r_extern ? RELOC_EXT_BITS_EXTERN_LITTLE : 0)
5704 | (howto->type << RELOC_EXT_BITS_TYPE_SH_LITTLE);
5705 }
5706
dc810e39 5707 PUT_WORD (finfo->output_bfd, (bfd_vma) pr->addend, erel.r_addend);
1642229e 5708#endif /* MY_put_ext_reloc */
252b5132
RH
5709
5710 rel_ptr = (PTR) &erel;
5711 }
5712
dc810e39 5713 amt = obj_reloc_entry_size (finfo->output_bfd);
252b5132 5714 if (bfd_seek (finfo->output_bfd, *reloff_ptr, SEEK_SET) != 0
dc810e39 5715 || bfd_bwrite (rel_ptr, amt, finfo->output_bfd) != amt)
b34976b6 5716 return FALSE;
252b5132
RH
5717
5718 *reloff_ptr += obj_reloc_entry_size (finfo->output_bfd);
5719
5720 /* Assert that the relocs have not run into the symbols, and that n
5721 the text relocs have not run into the data relocs. */
5722 BFD_ASSERT (*reloff_ptr <= obj_sym_filepos (finfo->output_bfd)
5723 && (reloff_ptr != &finfo->treloff
5724 || (*reloff_ptr
5725 <= obj_datasec (finfo->output_bfd)->rel_filepos)));
5726
b34976b6 5727 return TRUE;
252b5132 5728}
This page took 0.523383 seconds and 4 git commands to generate.