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