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