source: trunk/src/binutils/bfd/aoutx.h@ 10

Last change on this file since 10 was 10, checked in by bird, 22 years ago

Initial revision

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