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