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