get rid of rcs crud
[deliverable/binutils-gdb.git] / bfd / aoutx.h
1 /* BFD semi-generic back-end for a.out binaries.
2 Copyright 1990, 1991, 1992, 1993 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 (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_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 addersses 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_error = system_call_error;
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_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_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 void
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
1104 arelent_chain *reloc = (arelent_chain *) bfd_alloc (abfd, sizeof (arelent_chain));
1105 strcpy (copy, cache_ptr->symbol.name);
1106
1107 /* Make sure that this bfd has a section with the right contructor
1108 name */
1109 section = bfd_get_section_by_name (abfd, copy);
1110 if (!section)
1111 section = bfd_make_section (abfd, copy);
1112
1113 /* Build a relocation entry for the constructor */
1114 switch ((cache_ptr->type & N_TYPE))
1115 {
1116 case N_SETA: case N_SETA | N_EXT:
1117 into_section = &bfd_abs_section;
1118 cache_ptr->type = N_ABS;
1119 break;
1120 case N_SETT: case N_SETT | N_EXT:
1121 into_section = (asection *) obj_textsec (abfd);
1122 cache_ptr->type = N_TEXT;
1123 break;
1124 case N_SETD: case N_SETD | N_EXT:
1125 into_section = (asection *) obj_datasec (abfd);
1126 cache_ptr->type = N_DATA;
1127 break;
1128 case N_SETB: case N_SETB | N_EXT:
1129 into_section = (asection *) obj_bsssec (abfd);
1130 cache_ptr->type = N_BSS;
1131 break;
1132 default:
1133 abort ();
1134 }
1135
1136 /* Build a relocation pointing into the constuctor section
1137 pointing at the symbol in the set vector specified */
1138
1139 reloc->relent.addend = cache_ptr->symbol.value;
1140 cache_ptr->symbol.section = into_section->symbol->section;
1141 reloc->relent.sym_ptr_ptr = into_section->symbol_ptr_ptr;
1142
1143
1144 /* We modify the symbol to belong to a section depending upon the
1145 name of the symbol - probably __CTOR__ or __DTOR__ but we don't
1146 really care, and add to the size of the section to contain a
1147 pointer to the symbol. Build a reloc entry to relocate to this
1148 symbol attached to this section. */
1149
1150 section->flags = SEC_CONSTRUCTOR;
1151
1152
1153 section->reloc_count++;
1154 section->alignment_power = 2;
1155
1156 reloc->next = section->constructor_chain;
1157 section->constructor_chain = reloc;
1158 reloc->relent.address = section->_raw_size;
1159 section->_raw_size += sizeof (int *);
1160
1161 reloc->relent.howto
1162 = (obj_reloc_entry_size(abfd) == RELOC_EXT_SIZE
1163 ? howto_table_ext : howto_table_std)
1164 + CTOR_TABLE_RELOC_IDX;
1165 cache_ptr->symbol.flags |= BSF_CONSTRUCTOR;
1166 }
1167 break;
1168 default:
1169 if (cache_ptr->type == N_WARNING)
1170 {
1171 /* This symbol is the text of a warning message, the next symbol
1172 is the symbol to associate the warning with */
1173 cache_ptr->symbol.flags = BSF_DEBUGGING | BSF_WARNING;
1174
1175 /* @@ Stuffing pointers into integers is a no-no.
1176 We can usually get away with it if the integer is
1177 large enough though. */
1178 if (sizeof (cache_ptr + 1) > sizeof (bfd_vma))
1179 abort ();
1180 cache_ptr->symbol.value = (bfd_vma) ((cache_ptr + 1));
1181
1182 /* We don't use a warning symbol's section, but we need
1183 it to be nonzero for the sanity check below, so
1184 pick one arbitrarily. */
1185 cache_ptr->symbol.section = &bfd_abs_section;
1186
1187 /* We furgle with the next symbol in place.
1188 We don't want it to be undefined, we'll trample the type */
1189 (sym_pointer + 1)->e_type[0] = 0xff;
1190 break;
1191 }
1192 if ((cache_ptr->type | N_EXT) == (N_INDR | N_EXT))
1193 {
1194 /* Two symbols in a row for an INDR message. The first symbol
1195 contains the name we will match, the second symbol contains
1196 the name the first name is translated into. It is supplied to
1197 us undefined. This is good, since we want to pull in any files
1198 which define it */
1199 cache_ptr->symbol.flags = BSF_DEBUGGING | BSF_INDIRECT;
1200
1201 /* @@ Stuffing pointers into integers is a no-no.
1202 We can usually get away with it if the integer is
1203 large enough though. */
1204 if (sizeof (cache_ptr + 1) > sizeof (bfd_vma))
1205 abort ();
1206
1207 cache_ptr->symbol.value = (bfd_vma) ((cache_ptr + 1));
1208 cache_ptr->symbol.section = &bfd_ind_section;
1209 }
1210
1211 else if (sym_is_debugger_info (cache_ptr))
1212 {
1213 cache_ptr->symbol.flags = BSF_DEBUGGING;
1214 /* Work out the section correct for this symbol */
1215 switch (cache_ptr->type & N_TYPE)
1216 {
1217 case N_TEXT:
1218 case N_FN:
1219 cache_ptr->symbol.section = obj_textsec (abfd);
1220 cache_ptr->symbol.value -= obj_textsec (abfd)->vma;
1221 break;
1222 case N_DATA:
1223 cache_ptr->symbol.value -= obj_datasec (abfd)->vma;
1224 cache_ptr->symbol.section = obj_datasec (abfd);
1225 break;
1226 case N_BSS:
1227 cache_ptr->symbol.section = obj_bsssec (abfd);
1228 cache_ptr->symbol.value -= obj_bsssec (abfd)->vma;
1229 break;
1230 default:
1231 case N_ABS:
1232 cache_ptr->symbol.section = &bfd_abs_section;
1233 break;
1234 }
1235 }
1236 else
1237 {
1238
1239 if (sym_is_fortrancommon (cache_ptr))
1240 {
1241 cache_ptr->symbol.flags = 0;
1242 cache_ptr->symbol.section = &bfd_com_section;
1243 }
1244 else
1245 {
1246
1247
1248 }
1249
1250 /* In a.out, the value of a symbol is always relative to the
1251 * start of the file, if this is a data symbol we'll subtract
1252 * the size of the text section to get the section relative
1253 * value. If this is a bss symbol (which would be strange)
1254 * we'll subtract the size of the previous two sections
1255 * to find the section relative address.
1256 */
1257
1258 if (sym_in_text_section (cache_ptr))
1259 {
1260 cache_ptr->symbol.value -= obj_textsec (abfd)->vma;
1261 cache_ptr->symbol.section = obj_textsec (abfd);
1262 }
1263 else if (sym_in_data_section (cache_ptr))
1264 {
1265 cache_ptr->symbol.value -= obj_datasec (abfd)->vma;
1266 cache_ptr->symbol.section = obj_datasec (abfd);
1267 }
1268 else if (sym_in_bss_section (cache_ptr))
1269 {
1270 cache_ptr->symbol.section = obj_bsssec (abfd);
1271 cache_ptr->symbol.value -= obj_bsssec (abfd)->vma;
1272 }
1273 else if (sym_is_undefined (cache_ptr))
1274 {
1275 cache_ptr->symbol.flags = 0;
1276 cache_ptr->symbol.section = &bfd_und_section;
1277 }
1278 else if (sym_is_absolute (cache_ptr))
1279 {
1280 cache_ptr->symbol.section = &bfd_abs_section;
1281 }
1282
1283 if (sym_is_global_defn (cache_ptr))
1284 {
1285 cache_ptr->symbol.flags = BSF_GLOBAL | BSF_EXPORT;
1286 }
1287 else if (! sym_is_undefined (cache_ptr))
1288 {
1289 cache_ptr->symbol.flags = BSF_LOCAL;
1290 }
1291 }
1292 }
1293 if (cache_ptr->symbol.section == 0)
1294 abort ();
1295 }
1296
1297
1298
1299 static boolean
1300 DEFUN(translate_to_native_sym_flags,(sym_pointer, cache_ptr, abfd),
1301 struct external_nlist *sym_pointer AND
1302 asymbol *cache_ptr AND
1303 bfd *abfd)
1304 {
1305 bfd_vma value = cache_ptr->value;
1306
1307 /* mask out any existing type bits in case copying from one section
1308 to another */
1309 sym_pointer->e_type[0] &= ~N_TYPE;
1310
1311 /* We attempt to order these tests by decreasing frequency of success,
1312 according to tcov when linking the linker. */
1313 if (bfd_get_output_section(cache_ptr) == &bfd_abs_section) {
1314 sym_pointer->e_type[0] |= N_ABS;
1315 }
1316 else if (bfd_get_output_section(cache_ptr) == obj_textsec (abfd)) {
1317 sym_pointer->e_type[0] |= N_TEXT;
1318 }
1319 else if (bfd_get_output_section(cache_ptr) == obj_datasec (abfd)) {
1320 sym_pointer->e_type[0] |= N_DATA;
1321 }
1322 else if (bfd_get_output_section(cache_ptr) == obj_bsssec (abfd)) {
1323 sym_pointer->e_type[0] |= N_BSS;
1324 }
1325 else if (bfd_get_output_section(cache_ptr) == &bfd_und_section) {
1326 sym_pointer->e_type[0] = (N_UNDF | N_EXT);
1327 }
1328 else if (bfd_get_output_section(cache_ptr) == &bfd_ind_section) {
1329 sym_pointer->e_type[0] = N_INDR;
1330 }
1331 else if (bfd_get_output_section(cache_ptr) == NULL) {
1332 /* Protect the bfd_is_com_section call.
1333 This case occurs, e.g., for the *DEBUG* section of a COFF file. */
1334 bfd_error = nonrepresentable_section;
1335 return false;
1336 }
1337 else if (bfd_is_com_section (bfd_get_output_section (cache_ptr))) {
1338 sym_pointer->e_type[0] = (N_UNDF | N_EXT);
1339 }
1340 else {
1341 bfd_error = nonrepresentable_section;
1342 return false;
1343 }
1344
1345 /* Turn the symbol from section relative to absolute again */
1346
1347 value += cache_ptr->section->output_section->vma + cache_ptr->section->output_offset ;
1348
1349
1350 if (cache_ptr->flags & (BSF_WARNING)) {
1351 sym_pointer->e_type[0] = N_WARNING;
1352 (sym_pointer+1)->e_type[0] = 1;
1353 }
1354
1355 if (cache_ptr->flags & BSF_DEBUGGING) {
1356 sym_pointer->e_type[0] = ((aout_symbol_type *)cache_ptr)->type;
1357 }
1358 else if (cache_ptr->flags & (BSF_GLOBAL | BSF_EXPORT)) {
1359 sym_pointer->e_type[0] |= N_EXT;
1360 }
1361 if (cache_ptr->flags & BSF_CONSTRUCTOR) {
1362 int type = ((aout_symbol_type *)cache_ptr)->type;
1363 switch (type)
1364 {
1365 case N_ABS: type = N_SETA; break;
1366 case N_TEXT: type = N_SETT; break;
1367 case N_DATA: type = N_SETD; break;
1368 case N_BSS: type = N_SETB; break;
1369 }
1370 sym_pointer->e_type[0] = type;
1371 }
1372
1373 PUT_WORD(abfd, value, sym_pointer->e_value);
1374
1375 return true;
1376 }
1377 \f
1378 /* Native-level interface to symbols. */
1379
1380
1381 asymbol *
1382 DEFUN(NAME(aout,make_empty_symbol),(abfd),
1383 bfd *abfd)
1384 {
1385 aout_symbol_type *new =
1386 (aout_symbol_type *)bfd_zalloc (abfd, sizeof (aout_symbol_type));
1387 new->symbol.the_bfd = abfd;
1388
1389 return &new->symbol;
1390 }
1391
1392 /* Translate a set of internal symbols into external symbols. */
1393
1394 static boolean
1395 translate_symbol_table (abfd, in, ext, count, str, strsize, dynamic)
1396 bfd *abfd;
1397 aout_symbol_type *in;
1398 struct external_nlist *ext;
1399 bfd_size_type count;
1400 char *str;
1401 bfd_size_type strsize;
1402 boolean dynamic;
1403 {
1404 struct external_nlist *ext_end;
1405
1406 ext_end = ext + count;
1407 for (; ext < ext_end; ext++, in++)
1408 {
1409 bfd_vma x;
1410
1411 x = GET_WORD (abfd, ext->e_strx);
1412 in->symbol.the_bfd = abfd;
1413
1414 /* For the normal symbols, the zero index points at the number
1415 of bytes in the string table but is to be interpreted as the
1416 null string. For the dynamic symbols, the number of bytes in
1417 the string table is stored in the __DYNAMIC structure and the
1418 zero index points at an actual string. */
1419 if (x == 0 && ! dynamic)
1420 in->symbol.name = "";
1421 else if (x < strsize)
1422 in->symbol.name = str + x;
1423 else
1424 return false;
1425
1426 in->symbol.value = GET_SWORD (abfd, ext->e_value);
1427 in->desc = bfd_h_get_16 (abfd, ext->e_desc);
1428 in->other = bfd_h_get_8 (abfd, ext->e_other);
1429 in->type = bfd_h_get_8 (abfd, ext->e_type);
1430 in->symbol.udata = 0;
1431
1432 translate_from_native_sym_flags (ext, in, abfd);
1433
1434 if (dynamic)
1435 in->symbol.flags |= BSF_DYNAMIC;
1436 }
1437
1438 return true;
1439 }
1440
1441 /* We read the symbols into a buffer, which is discarded when this
1442 function exits. We read the strings into a buffer large enough to
1443 hold them all plus all the cached symbol entries. */
1444
1445 boolean
1446 DEFUN(NAME(aout,slurp_symbol_table),(abfd),
1447 bfd *abfd)
1448 {
1449 bfd_size_type symbol_size;
1450 bfd_size_type string_size;
1451 unsigned char string_chars[BYTES_IN_WORD];
1452 struct external_nlist *syms;
1453 char *strings;
1454 aout_symbol_type *cached;
1455 bfd_size_type dynsym_count = 0;
1456 struct external_nlist *dynsyms = NULL;
1457 char *dynstrs = NULL;
1458 bfd_size_type dynstr_size;
1459
1460 /* If there's no work to be done, don't do any */
1461 if (obj_aout_symbols (abfd) != (aout_symbol_type *)NULL) return true;
1462 symbol_size = exec_hdr(abfd)->a_syms;
1463 if (symbol_size == 0)
1464 {
1465 bfd_error = no_symbols;
1466 return false;
1467 }
1468
1469 bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET);
1470 if (bfd_read ((PTR)string_chars, BYTES_IN_WORD, 1, abfd) != BYTES_IN_WORD)
1471 return false;
1472 string_size = GET_WORD (abfd, string_chars);
1473
1474 /* If this is a dynamic object, see if we can get the dynamic symbol
1475 table. */
1476 if ((bfd_get_file_flags (abfd) & DYNAMIC) != 0
1477 && aout_backend_info (abfd)->read_dynamic_symbols)
1478 {
1479 dynsym_count = ((*aout_backend_info (abfd)->read_dynamic_symbols)
1480 (abfd, &dynsyms, &dynstrs, &dynstr_size));
1481 if (dynsym_count == (bfd_size_type) -1)
1482 return false;
1483 }
1484
1485 strings = (char *) bfd_alloc (abfd, string_size + 1);
1486 cached = ((aout_symbol_type *)
1487 bfd_zalloc (abfd,
1488 ((bfd_get_symcount (abfd) + dynsym_count)
1489 * sizeof (aout_symbol_type))));
1490
1491 /* Don't allocate on the obstack, so we can free it easily. */
1492 syms = (struct external_nlist *) bfd_xmalloc(symbol_size);
1493 bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET);
1494 if (bfd_read ((PTR)syms, 1, symbol_size, abfd) != symbol_size)
1495 {
1496 bailout:
1497 if (syms)
1498 free (syms);
1499 if (cached)
1500 bfd_release (abfd, cached);
1501 if (strings)
1502 bfd_release (abfd, strings);
1503 return false;
1504 }
1505
1506 bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET);
1507 if (bfd_read ((PTR)strings, 1, string_size, abfd) != string_size)
1508 {
1509 goto bailout;
1510 }
1511 strings[string_size] = 0; /* Just in case. */
1512
1513 /* OK, now walk the new symtable, cacheing symbol properties */
1514 if (! translate_symbol_table (abfd, cached, syms, bfd_get_symcount (abfd),
1515 strings, string_size, false))
1516 goto bailout;
1517 if (dynsym_count > 0)
1518 {
1519 if (! translate_symbol_table (abfd, cached + bfd_get_symcount (abfd),
1520 dynsyms, dynsym_count, dynstrs,
1521 dynstr_size, true))
1522 goto bailout;
1523
1524 bfd_get_symcount (abfd) += dynsym_count;
1525 }
1526
1527 obj_aout_symbols (abfd) = cached;
1528 free((PTR)syms);
1529
1530 return true;
1531 }
1532
1533 \f
1534 /* Possible improvements:
1535 + look for strings matching trailing substrings of other strings
1536 + better data structures? balanced trees?
1537 + smaller per-string or per-symbol data? re-use some of the symbol's
1538 data fields?
1539 + also look at reducing memory use elsewhere -- maybe if we didn't have to
1540 construct the entire symbol table at once, we could get by with smaller
1541 amounts of VM? (What effect does that have on the string table
1542 reductions?)
1543 + rip this out of here, put it into its own file in bfd or libiberty, so
1544 coff and elf can use it too. I'll work on this soon, but have more
1545 pressing tasks right now.
1546
1547 A hash table might(?) be more efficient for handling exactly the cases that
1548 are handled now, but for trailing substring matches, I think we want to
1549 examine the `nearest' values (reverse-)lexically, not merely impose a strict
1550 order, nor look only for exact-match or not-match. I don't think a hash
1551 table would be very useful for that, and I don't feel like fleshing out two
1552 completely different implementations. [raeburn:930419.0331EDT] */
1553
1554 struct stringtab_entry {
1555 /* Hash value for this string. Only useful so long as we aren't doing
1556 substring matches. */
1557 unsigned int hash;
1558
1559 /* Next node to look at, depending on whether the hash value of the string
1560 being searched for is less than or greater than the hash value of the
1561 current node. For now, `equal to' is lumped in with `greater than', for
1562 space efficiency. It's not a common enough case to warrant another field
1563 to be used for all nodes. */
1564 struct stringtab_entry *less;
1565 struct stringtab_entry *greater;
1566
1567 /* The string itself. */
1568 CONST char *string;
1569
1570 /* The index allocated for this string. */
1571 bfd_size_type index;
1572
1573 #ifdef GATHER_STATISTICS
1574 /* How many references have there been to this string? (Not currently used;
1575 could be dumped out for anaylsis, if anyone's interested.) */
1576 unsigned long count;
1577 #endif
1578
1579 /* Next node in linked list, in suggested output order. */
1580 struct stringtab_entry *next_to_output;
1581 };
1582
1583 struct stringtab_data {
1584 /* Tree of string table entries. */
1585 struct stringtab_entry *strings;
1586
1587 /* Fudge factor used to center top node of tree. */
1588 int hash_zero;
1589
1590 /* Next index value to issue. */
1591 bfd_size_type index;
1592
1593 /* Index used for empty strings. Cached here because checking for them
1594 is really easy, and we can avoid searching the tree. */
1595 bfd_size_type empty_string_index;
1596
1597 /* These fields indicate the two ends of a singly-linked list that indicates
1598 the order strings should be written out in. Use this order, and no
1599 seeking will need to be done, so output efficiency should be maximized. */
1600 struct stringtab_entry **end;
1601 struct stringtab_entry *output_order;
1602
1603 #ifdef GATHER_STATISTICS
1604 /* Number of strings which duplicate strings already in the table. */
1605 unsigned long duplicates;
1606
1607 /* Number of bytes saved by not having to write all the duplicate strings. */
1608 unsigned long bytes_saved;
1609
1610 /* Number of zero-length strings. Currently, these all turn into
1611 references to the null byte at the end of the first string. In some
1612 cases (possibly not all? explore this...), it should be possible to
1613 simply write out a zero index value. */
1614 unsigned long empty_strings;
1615
1616 /* Number of times the hash values matched but the strings were different.
1617 Note that this includes the number of times the other string(s) occurs, so
1618 there may only be two strings hashing to the same value, even if this
1619 number is very large. */
1620 unsigned long bad_hash_matches;
1621
1622 /* Null strings aren't counted in this one.
1623 This will probably only be nonzero if we've got an input file
1624 which was produced by `ld -r' (i.e., it's already been processed
1625 through this code). Under some operating systems, native tools
1626 may make all empty strings have the same index; but the pointer
1627 check won't catch those, because to get to that stage we'd already
1628 have to compute the checksum, which requires reading the string,
1629 so we short-circuit that case with empty_string_index above. */
1630 unsigned long pointer_matches;
1631
1632 /* Number of comparisons done. I figure with the algorithms in use below,
1633 the average number of comparisons done (per symbol) should be roughly
1634 log-base-2 of the number of unique strings. */
1635 unsigned long n_compares;
1636 #endif
1637 };
1638
1639 /* Some utility functions for the string table code. */
1640
1641 /* For speed, only hash on the first this many bytes of strings.
1642 This number was chosen by profiling ld linking itself, with -g. */
1643 #define HASHMAXLEN 25
1644
1645 #define HASH_CHAR(c) (sum ^= sum >> 20, sum ^= sum << 7, sum += (c))
1646
1647 static INLINE unsigned int
1648 hash (string, len)
1649 unsigned char *string;
1650 register unsigned int len;
1651 {
1652 register unsigned int sum = 0;
1653
1654 if (len > HASHMAXLEN)
1655 {
1656 HASH_CHAR (len);
1657 len = HASHMAXLEN;
1658 }
1659
1660 while (len--)
1661 {
1662 HASH_CHAR (*string++);
1663 }
1664 return sum;
1665 }
1666
1667 static INLINE void
1668 stringtab_init (tab)
1669 struct stringtab_data *tab;
1670 {
1671 tab->strings = 0;
1672 tab->output_order = 0;
1673 tab->hash_zero = 0;
1674 tab->end = &tab->output_order;
1675
1676 /* Initial string table length includes size of length field. */
1677 tab->index = BYTES_IN_WORD;
1678 tab->empty_string_index = -1;
1679 #ifdef GATHER_STATISTICS
1680 tab->duplicates = 0;
1681 tab->empty_strings = 0;
1682 tab->bad_hash_matches = 0;
1683 tab->pointer_matches = 0;
1684 tab->bytes_saved = 0;
1685 tab->n_compares = 0;
1686 #endif
1687 }
1688
1689 static INLINE int
1690 compare (entry, str, hash)
1691 struct stringtab_entry *entry;
1692 CONST char *str;
1693 unsigned int hash;
1694 {
1695 return hash - entry->hash;
1696 }
1697
1698 #ifdef GATHER_STATISTICS
1699 /* Don't want to have to link in math library with all bfd applications... */
1700 static INLINE double
1701 log2 (num)
1702 int num;
1703 {
1704 double d = num;
1705 int n = 0;
1706 while (d >= 2.0)
1707 n++, d /= 2.0;
1708 return ((d > 1.41) ? 0.5 : 0) + n;
1709 }
1710 #endif
1711
1712 /* Main string table routines. */
1713 /* Returns index in string table. Whether or not this actually adds an
1714 entry into the string table should be irrelevant -- it just has to
1715 return a valid index. */
1716 static bfd_size_type
1717 add_to_stringtab (abfd, str, tab)
1718 bfd *abfd;
1719 CONST char *str;
1720 struct stringtab_data *tab;
1721 {
1722 struct stringtab_entry **ep;
1723 register struct stringtab_entry *entry;
1724 unsigned int hashval, len;
1725
1726 if (str[0] == 0)
1727 {
1728 bfd_size_type index;
1729 CONST bfd_size_type minus_one = -1;
1730
1731 #ifdef GATHER_STATISTICS
1732 tab->empty_strings++;
1733 #endif
1734 index = tab->empty_string_index;
1735 if (index != minus_one)
1736 {
1737 got_empty:
1738 #ifdef GATHER_STATISTICS
1739 tab->bytes_saved++;
1740 tab->duplicates++;
1741 #endif
1742 return index;
1743 }
1744
1745 /* Need to find it. */
1746 entry = tab->strings;
1747 if (entry)
1748 {
1749 index = entry->index + strlen (entry->string);
1750 tab->empty_string_index = index;
1751 goto got_empty;
1752 }
1753 len = 0;
1754 }
1755 else
1756 len = strlen (str);
1757
1758 /* The hash_zero value is chosen such that the first symbol gets a value of
1759 zero. With a balanced tree, this wouldn't be very useful, but without it,
1760 we might get a more even split at the top level, instead of skewing it
1761 badly should hash("/usr/lib/crt0.o") (or whatever) be far from zero. */
1762 hashval = hash (str, len) ^ tab->hash_zero;
1763 ep = &tab->strings;
1764 if (!*ep)
1765 {
1766 tab->hash_zero = hashval;
1767 hashval = 0;
1768 goto add_it;
1769 }
1770
1771 while (*ep)
1772 {
1773 register int cmp;
1774
1775 entry = *ep;
1776 #ifdef GATHER_STATISTICS
1777 tab->n_compares++;
1778 #endif
1779 cmp = compare (entry, str, hashval);
1780 /* The not-equal cases are more frequent, so check them first. */
1781 if (cmp > 0)
1782 ep = &entry->greater;
1783 else if (cmp < 0)
1784 ep = &entry->less;
1785 else
1786 {
1787 if (entry->string == str)
1788 {
1789 #ifdef GATHER_STATISTICS
1790 tab->pointer_matches++;
1791 #endif
1792 goto match;
1793 }
1794 /* Compare the first bytes to save a function call if they
1795 don't match. */
1796 if (entry->string[0] == str[0] && !strcmp (entry->string, str))
1797 {
1798 match:
1799 #ifdef GATHER_STATISTICS
1800 entry->count++;
1801 tab->bytes_saved += len + 1;
1802 tab->duplicates++;
1803 #endif
1804 /* If we're in the linker, and the new string is from a new
1805 input file which might have already had these reductions
1806 run over it, we want to keep the new string pointer. I
1807 don't think we're likely to see any (or nearly as many,
1808 at least) cases where a later string is in the same location
1809 as an earlier one rather than this one. */
1810 entry->string = str;
1811 return entry->index;
1812 }
1813 #ifdef GATHER_STATISTICS
1814 tab->bad_hash_matches++;
1815 #endif
1816 ep = &entry->greater;
1817 }
1818 }
1819
1820 /* If we get here, nothing that's in the table already matched.
1821 EP points to the `next' field at the end of the chain; stick a
1822 new entry on here. */
1823 add_it:
1824 entry = (struct stringtab_entry *)
1825 bfd_alloc_by_size_t (abfd, sizeof (struct stringtab_entry));
1826
1827 entry->less = entry->greater = 0;
1828 entry->hash = hashval;
1829 entry->index = tab->index;
1830 entry->string = str;
1831 entry->next_to_output = 0;
1832 #ifdef GATHER_STATISTICS
1833 entry->count = 1;
1834 #endif
1835
1836 assert (*tab->end == 0);
1837 *(tab->end) = entry;
1838 tab->end = &entry->next_to_output;
1839 assert (*tab->end == 0);
1840
1841 {
1842 tab->index += len + 1;
1843 if (len == 0)
1844 tab->empty_string_index = entry->index;
1845 }
1846 assert (*ep == 0);
1847 *ep = entry;
1848 return entry->index;
1849 }
1850
1851 static void
1852 emit_strtab (abfd, tab)
1853 bfd *abfd;
1854 struct stringtab_data *tab;
1855 {
1856 struct stringtab_entry *entry;
1857 #ifdef GATHER_STATISTICS
1858 int count = 0;
1859 #endif
1860
1861 /* Be sure to put string length into correct byte ordering before writing
1862 it out. */
1863 char buffer[BYTES_IN_WORD];
1864
1865 PUT_WORD (abfd, tab->index, (unsigned char *) buffer);
1866 bfd_write ((PTR) buffer, 1, BYTES_IN_WORD, abfd);
1867
1868 for (entry = tab->output_order; entry; entry = entry->next_to_output)
1869 {
1870 bfd_write ((PTR) entry->string, 1, strlen (entry->string) + 1, abfd);
1871 #ifdef GATHER_STATISTICS
1872 count++;
1873 #endif
1874 }
1875
1876 #ifdef GATHER_STATISTICS
1877 /* Short form only, for now.
1878 To do: Specify output file. Conditionalize on environment? Detailed
1879 analysis if desired. */
1880 {
1881 int n_syms = bfd_get_symcount (abfd);
1882
1883 fprintf (stderr, "String table data for output file:\n");
1884 fprintf (stderr, " %8d symbols output\n", n_syms);
1885 fprintf (stderr, " %8d duplicate strings\n", tab->duplicates);
1886 fprintf (stderr, " %8d empty strings\n", tab->empty_strings);
1887 fprintf (stderr, " %8d unique strings output\n", count);
1888 fprintf (stderr, " %8d pointer matches\n", tab->pointer_matches);
1889 fprintf (stderr, " %8d bytes saved\n", tab->bytes_saved);
1890 fprintf (stderr, " %8d bad hash matches\n", tab->bad_hash_matches);
1891 fprintf (stderr, " %8d hash-val comparisons\n", tab->n_compares);
1892 if (n_syms)
1893 {
1894 double n_compares = tab->n_compares;
1895 double avg_compares = n_compares / n_syms;
1896 /* The second value here should usually be near one. */
1897 fprintf (stderr,
1898 "\t average %f comparisons per symbol (%f * log2 nstrings)\n",
1899 avg_compares, avg_compares / log2 (count));
1900 }
1901 }
1902 #endif
1903
1904 /* Old code:
1905 unsigned int count;
1906 generic = bfd_get_outsymbols(abfd);
1907 for (count = 0; count < bfd_get_symcount(abfd); count++)
1908 {
1909 asymbol *g = *(generic++);
1910
1911 if (g->name)
1912 {
1913 size_t length = strlen(g->name)+1;
1914 bfd_write((PTR)g->name, 1, length, abfd);
1915 }
1916 g->KEEPIT = (KEEPITTYPE) count;
1917 } */
1918 }
1919
1920 boolean
1921 DEFUN(NAME(aout,write_syms),(abfd),
1922 bfd *abfd)
1923 {
1924 unsigned int count ;
1925 asymbol **generic = bfd_get_outsymbols (abfd);
1926 struct stringtab_data strtab;
1927
1928 stringtab_init (&strtab);
1929
1930 for (count = 0; count < bfd_get_symcount (abfd); count++)
1931 {
1932 asymbol *g = generic[count];
1933 struct external_nlist nsp;
1934
1935 if (g->name)
1936 PUT_WORD (abfd, add_to_stringtab (abfd, g->name, &strtab),
1937 (unsigned char *) nsp.e_strx);
1938 else
1939 PUT_WORD (abfd, 0, (unsigned char *)nsp.e_strx);
1940
1941 if (bfd_asymbol_flavour(g) == abfd->xvec->flavour)
1942 {
1943 bfd_h_put_16(abfd, aout_symbol(g)->desc, nsp.e_desc);
1944 bfd_h_put_8(abfd, aout_symbol(g)->other, nsp.e_other);
1945 bfd_h_put_8(abfd, aout_symbol(g)->type, nsp.e_type);
1946 }
1947 else
1948 {
1949 bfd_h_put_16(abfd,0, nsp.e_desc);
1950 bfd_h_put_8(abfd, 0, nsp.e_other);
1951 bfd_h_put_8(abfd, 0, nsp.e_type);
1952 }
1953
1954 if (! translate_to_native_sym_flags (&nsp, g, abfd))
1955 return false;
1956
1957 if (bfd_write((PTR)&nsp,1,EXTERNAL_NLIST_SIZE, abfd)
1958 != EXTERNAL_NLIST_SIZE)
1959 return false;
1960
1961 /* NB: `KEEPIT' currently overlays `flags', so set this only
1962 here, at the end. */
1963 g->KEEPIT = count;
1964 }
1965
1966 emit_strtab (abfd, &strtab);
1967
1968 return true;
1969 }
1970
1971 \f
1972 unsigned int
1973 DEFUN(NAME(aout,get_symtab),(abfd, location),
1974 bfd *abfd AND
1975 asymbol **location)
1976 {
1977 unsigned int counter = 0;
1978 aout_symbol_type *symbase;
1979
1980 if (!NAME(aout,slurp_symbol_table)(abfd)) return 0;
1981
1982 for (symbase = obj_aout_symbols(abfd); counter++ < bfd_get_symcount (abfd);)
1983 *(location++) = (asymbol *)( symbase++);
1984 *location++ =0;
1985 return bfd_get_symcount (abfd);
1986 }
1987
1988 \f
1989 /* Standard reloc stuff */
1990 /* Output standard relocation information to a file in target byte order. */
1991
1992 void
1993 DEFUN(NAME(aout,swap_std_reloc_out),(abfd, g, natptr),
1994 bfd *abfd AND
1995 arelent *g AND
1996 struct reloc_std_external *natptr)
1997 {
1998 int r_index;
1999 asymbol *sym = *(g->sym_ptr_ptr);
2000 int r_extern;
2001 unsigned int r_length;
2002 int r_pcrel;
2003 int r_baserel, r_jmptable, r_relative;
2004 asection *output_section = sym->section->output_section;
2005
2006 PUT_WORD(abfd, g->address, natptr->r_address);
2007
2008 r_length = g->howto->size ; /* Size as a power of two */
2009 r_pcrel = (int) g->howto->pc_relative; /* Relative to PC? */
2010 /* XXX This relies on relocs coming from a.out files. */
2011 r_baserel = (g->howto->type & 8) != 0;
2012 /* r_jmptable, r_relative??? FIXME-soon */
2013 r_jmptable = 0;
2014 r_relative = 0;
2015
2016 #if 0
2017 /* For a standard reloc, the addend is in the object file. */
2018 r_addend = g->addend + (*(g->sym_ptr_ptr))->section->output_section->vma;
2019 #endif
2020
2021 /* name was clobbered by aout_write_syms to be symbol index */
2022
2023 /* If this relocation is relative to a symbol then set the
2024 r_index to the symbols index, and the r_extern bit.
2025
2026 Absolute symbols can come in in two ways, either as an offset
2027 from the abs section, or as a symbol which has an abs value.
2028 check for that here
2029 */
2030
2031
2032 if (bfd_is_com_section (output_section)
2033 || output_section == &bfd_abs_section
2034 || output_section == &bfd_und_section)
2035 {
2036 if (bfd_abs_section.symbol == sym)
2037 {
2038 /* Whoops, looked like an abs symbol, but is really an offset
2039 from the abs section */
2040 r_index = 0;
2041 r_extern = 0;
2042 }
2043 else
2044 {
2045 /* Fill in symbol */
2046 r_extern = 1;
2047 r_index = stoi((*(g->sym_ptr_ptr))->KEEPIT);
2048
2049 }
2050 }
2051 else
2052 {
2053 /* Just an ordinary section */
2054 r_extern = 0;
2055 r_index = output_section->target_index;
2056 }
2057
2058 /* now the fun stuff */
2059 if (abfd->xvec->header_byteorder_big_p != false) {
2060 natptr->r_index[0] = r_index >> 16;
2061 natptr->r_index[1] = r_index >> 8;
2062 natptr->r_index[2] = r_index;
2063 natptr->r_type[0] =
2064 (r_extern? RELOC_STD_BITS_EXTERN_BIG: 0)
2065 | (r_pcrel? RELOC_STD_BITS_PCREL_BIG: 0)
2066 | (r_baserel? RELOC_STD_BITS_BASEREL_BIG: 0)
2067 | (r_jmptable? RELOC_STD_BITS_JMPTABLE_BIG: 0)
2068 | (r_relative? RELOC_STD_BITS_RELATIVE_BIG: 0)
2069 | (r_length << RELOC_STD_BITS_LENGTH_SH_BIG);
2070 } else {
2071 natptr->r_index[2] = r_index >> 16;
2072 natptr->r_index[1] = r_index >> 8;
2073 natptr->r_index[0] = r_index;
2074 natptr->r_type[0] =
2075 (r_extern? RELOC_STD_BITS_EXTERN_LITTLE: 0)
2076 | (r_pcrel? RELOC_STD_BITS_PCREL_LITTLE: 0)
2077 | (r_baserel? RELOC_STD_BITS_BASEREL_LITTLE: 0)
2078 | (r_jmptable? RELOC_STD_BITS_JMPTABLE_LITTLE: 0)
2079 | (r_relative? RELOC_STD_BITS_RELATIVE_LITTLE: 0)
2080 | (r_length << RELOC_STD_BITS_LENGTH_SH_LITTLE);
2081 }
2082 }
2083
2084
2085 /* Extended stuff */
2086 /* Output extended relocation information to a file in target byte order. */
2087
2088 void
2089 DEFUN(NAME(aout,swap_ext_reloc_out),(abfd, g, natptr),
2090 bfd *abfd AND
2091 arelent *g AND
2092 register struct reloc_ext_external *natptr)
2093 {
2094 int r_index;
2095 int r_extern;
2096 unsigned int r_type;
2097 unsigned int r_addend;
2098 asymbol *sym = *(g->sym_ptr_ptr);
2099 asection *output_section = sym->section->output_section;
2100
2101 PUT_WORD (abfd, g->address, natptr->r_address);
2102
2103 r_type = (unsigned int) g->howto->type;
2104
2105 r_addend = g->addend + (*(g->sym_ptr_ptr))->section->output_section->vma;
2106
2107 /* If this relocation is relative to a symbol then set the
2108 r_index to the symbols index, and the r_extern bit.
2109
2110 Absolute symbols can come in in two ways, either as an offset
2111 from the abs section, or as a symbol which has an abs value.
2112 check for that here. */
2113
2114 if (bfd_is_com_section (output_section)
2115 || output_section == &bfd_abs_section
2116 || output_section == &bfd_und_section)
2117 {
2118 if (bfd_abs_section.symbol == sym)
2119 {
2120 /* Whoops, looked like an abs symbol, but is really an offset
2121 from the abs section */
2122 r_index = 0;
2123 r_extern = 0;
2124 }
2125 else
2126 {
2127 r_extern = 1;
2128 r_index = stoi((*(g->sym_ptr_ptr))->KEEPIT);
2129 }
2130 }
2131 else
2132 {
2133 /* Just an ordinary section */
2134 r_extern = 0;
2135 r_index = output_section->target_index;
2136 }
2137
2138 /* now the fun stuff */
2139 if (abfd->xvec->header_byteorder_big_p != false) {
2140 natptr->r_index[0] = r_index >> 16;
2141 natptr->r_index[1] = r_index >> 8;
2142 natptr->r_index[2] = r_index;
2143 natptr->r_type[0] =
2144 ((r_extern? RELOC_EXT_BITS_EXTERN_BIG: 0)
2145 | (r_type << RELOC_EXT_BITS_TYPE_SH_BIG));
2146 } else {
2147 natptr->r_index[2] = r_index >> 16;
2148 natptr->r_index[1] = r_index >> 8;
2149 natptr->r_index[0] = r_index;
2150 natptr->r_type[0] =
2151 (r_extern? RELOC_EXT_BITS_EXTERN_LITTLE: 0)
2152 | (r_type << RELOC_EXT_BITS_TYPE_SH_LITTLE);
2153 }
2154
2155 PUT_WORD (abfd, r_addend, natptr->r_addend);
2156 }
2157
2158 /* BFD deals internally with all things based from the section they're
2159 in. so, something in 10 bytes into a text section with a base of
2160 50 would have a symbol (.text+10) and know .text vma was 50.
2161
2162 Aout keeps all it's symbols based from zero, so the symbol would
2163 contain 60. This macro subs the base of each section from the value
2164 to give the true offset from the section */
2165
2166
2167 #define MOVE_ADDRESS(ad) \
2168 if (r_extern) { \
2169 /* undefined symbol */ \
2170 cache_ptr->sym_ptr_ptr = symbols + r_index; \
2171 cache_ptr->addend = ad; \
2172 } else { \
2173 /* defined, section relative. replace symbol with pointer to \
2174 symbol which points to section */ \
2175 switch (r_index) { \
2176 case N_TEXT: \
2177 case N_TEXT | N_EXT: \
2178 cache_ptr->sym_ptr_ptr = obj_textsec(abfd)->symbol_ptr_ptr; \
2179 cache_ptr->addend = ad - su->textsec->vma; \
2180 break; \
2181 case N_DATA: \
2182 case N_DATA | N_EXT: \
2183 cache_ptr->sym_ptr_ptr = obj_datasec(abfd)->symbol_ptr_ptr; \
2184 cache_ptr->addend = ad - su->datasec->vma; \
2185 break; \
2186 case N_BSS: \
2187 case N_BSS | N_EXT: \
2188 cache_ptr->sym_ptr_ptr = obj_bsssec(abfd)->symbol_ptr_ptr; \
2189 cache_ptr->addend = ad - su->bsssec->vma; \
2190 break; \
2191 default: \
2192 case N_ABS: \
2193 case N_ABS | N_EXT: \
2194 cache_ptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr; \
2195 cache_ptr->addend = ad; \
2196 break; \
2197 } \
2198 } \
2199
2200 void
2201 DEFUN(NAME(aout,swap_ext_reloc_in), (abfd, bytes, cache_ptr, symbols),
2202 bfd *abfd AND
2203 struct reloc_ext_external *bytes AND
2204 arelent *cache_ptr AND
2205 asymbol **symbols)
2206 {
2207 int r_index;
2208 int r_extern;
2209 unsigned int r_type;
2210 struct aoutdata *su = &(abfd->tdata.aout_data->a);
2211
2212 cache_ptr->address = (GET_SWORD (abfd, bytes->r_address));
2213
2214 /* now the fun stuff */
2215 if (abfd->xvec->header_byteorder_big_p != false) {
2216 r_index = (bytes->r_index[0] << 16)
2217 | (bytes->r_index[1] << 8)
2218 | bytes->r_index[2];
2219 r_extern = (0 != (bytes->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG));
2220 r_type = (bytes->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
2221 >> RELOC_EXT_BITS_TYPE_SH_BIG;
2222 } else {
2223 r_index = (bytes->r_index[2] << 16)
2224 | (bytes->r_index[1] << 8)
2225 | bytes->r_index[0];
2226 r_extern = (0 != (bytes->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
2227 r_type = (bytes->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
2228 >> RELOC_EXT_BITS_TYPE_SH_LITTLE;
2229 }
2230
2231 cache_ptr->howto = howto_table_ext + r_type;
2232 MOVE_ADDRESS(GET_SWORD(abfd, bytes->r_addend));
2233 }
2234
2235 void
2236 DEFUN(NAME(aout,swap_std_reloc_in), (abfd, bytes, cache_ptr, symbols),
2237 bfd *abfd AND
2238 struct reloc_std_external *bytes AND
2239 arelent *cache_ptr AND
2240 asymbol **symbols)
2241 {
2242 int r_index;
2243 int r_extern;
2244 unsigned int r_length;
2245 int r_pcrel;
2246 int r_baserel, r_jmptable, r_relative;
2247 struct aoutdata *su = &(abfd->tdata.aout_data->a);
2248 int howto_idx;
2249
2250 cache_ptr->address = bfd_h_get_32 (abfd, bytes->r_address);
2251
2252 /* now the fun stuff */
2253 if (abfd->xvec->header_byteorder_big_p != false) {
2254 r_index = (bytes->r_index[0] << 16)
2255 | (bytes->r_index[1] << 8)
2256 | bytes->r_index[2];
2257 r_extern = (0 != (bytes->r_type[0] & RELOC_STD_BITS_EXTERN_BIG));
2258 r_pcrel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_PCREL_BIG));
2259 r_baserel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_BASEREL_BIG));
2260 r_jmptable= (0 != (bytes->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG));
2261 r_relative= (0 != (bytes->r_type[0] & RELOC_STD_BITS_RELATIVE_BIG));
2262 r_length = (bytes->r_type[0] & RELOC_STD_BITS_LENGTH_BIG)
2263 >> RELOC_STD_BITS_LENGTH_SH_BIG;
2264 } else {
2265 r_index = (bytes->r_index[2] << 16)
2266 | (bytes->r_index[1] << 8)
2267 | bytes->r_index[0];
2268 r_extern = (0 != (bytes->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE));
2269 r_pcrel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
2270 r_baserel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_BASEREL_LITTLE));
2271 r_jmptable= (0 != (bytes->r_type[0] & RELOC_STD_BITS_JMPTABLE_LITTLE));
2272 r_relative= (0 != (bytes->r_type[0] & RELOC_STD_BITS_RELATIVE_LITTLE));
2273 r_length = (bytes->r_type[0] & RELOC_STD_BITS_LENGTH_LITTLE)
2274 >> RELOC_STD_BITS_LENGTH_SH_LITTLE;
2275 }
2276
2277 howto_idx = r_length + 4 * r_pcrel + 8 * r_baserel;
2278 BFD_ASSERT (howto_idx < TABLE_SIZE (howto_table_std));
2279 cache_ptr->howto = howto_table_std + howto_idx;
2280 BFD_ASSERT (cache_ptr->howto->type != -1);
2281 BFD_ASSERT (r_jmptable == 0);
2282 BFD_ASSERT (r_relative == 0);
2283 /* FIXME-soon: Roll jmptable, relative bits into howto setting */
2284
2285 MOVE_ADDRESS(0);
2286 }
2287
2288 /* Reloc hackery */
2289
2290 boolean
2291 DEFUN(NAME(aout,slurp_reloc_table),(abfd, asect, symbols),
2292 bfd *abfd AND
2293 sec_ptr asect AND
2294 asymbol **symbols)
2295 {
2296 unsigned int count;
2297 bfd_size_type reloc_size;
2298 PTR relocs;
2299 bfd_size_type dynrel_count = 0;
2300 PTR dynrels = NULL;
2301 arelent *reloc_cache;
2302 size_t each_size;
2303 unsigned int counter = 0;
2304 arelent *cache_ptr;
2305
2306 if (asect->relocation) return true;
2307
2308 if (asect->flags & SEC_CONSTRUCTOR) return true;
2309
2310 if (asect == obj_datasec (abfd))
2311 reloc_size = exec_hdr(abfd)->a_drsize;
2312 else if (asect == obj_textsec (abfd))
2313 reloc_size = exec_hdr(abfd)->a_trsize;
2314 else
2315 {
2316 bfd_error = invalid_operation;
2317 return false;
2318 }
2319
2320 if ((bfd_get_file_flags (abfd) & DYNAMIC) != 0
2321 && aout_backend_info (abfd)->read_dynamic_relocs)
2322 {
2323 dynrel_count = ((*aout_backend_info (abfd)->read_dynamic_relocs)
2324 (abfd, &dynrels));
2325 if (dynrel_count == (bfd_size_type) -1)
2326 return false;
2327 }
2328
2329 bfd_seek (abfd, asect->rel_filepos, SEEK_SET);
2330 each_size = obj_reloc_entry_size (abfd);
2331
2332 count = reloc_size / each_size;
2333
2334 reloc_cache = ((arelent *)
2335 bfd_zalloc (abfd,
2336 (size_t) ((count + dynrel_count)
2337 * sizeof (arelent))));
2338 if (!reloc_cache)
2339 {
2340 nomem:
2341 bfd_error = no_memory;
2342 return false;
2343 }
2344
2345 relocs = (PTR) bfd_alloc (abfd, reloc_size);
2346 if (!relocs)
2347 {
2348 bfd_release (abfd, reloc_cache);
2349 goto nomem;
2350 }
2351
2352 if (bfd_read (relocs, 1, reloc_size, abfd) != reloc_size)
2353 {
2354 bfd_release (abfd, relocs);
2355 bfd_release (abfd, reloc_cache);
2356 bfd_error = system_call_error;
2357 return false;
2358 }
2359
2360 cache_ptr = reloc_cache;
2361 if (each_size == RELOC_EXT_SIZE)
2362 {
2363 register struct reloc_ext_external *rptr =
2364 (struct reloc_ext_external *) relocs;
2365
2366 for (; counter < count; counter++, rptr++, cache_ptr++)
2367 NAME(aout,swap_ext_reloc_in) (abfd, rptr, cache_ptr, symbols);
2368 }
2369 else
2370 {
2371 register struct reloc_std_external *rptr
2372 = (struct reloc_std_external *) relocs;
2373
2374 for (; counter < count; counter++, rptr++, cache_ptr++)
2375 NAME(aout,swap_std_reloc_in) (abfd, rptr, cache_ptr, symbols);
2376 }
2377
2378 if (dynrel_count > 0)
2379 {
2380 asymbol **dynsyms;
2381
2382 /* The dynamic symbols are at the end of the symbol table. */
2383 for (dynsyms = symbols;
2384 *dynsyms != NULL && ((*dynsyms)->flags & BSF_DYNAMIC) == 0;
2385 ++dynsyms)
2386 ;
2387
2388 /* Swap in the dynamic relocs. These relocs may be for either
2389 section, so we must discard ones we don't want. */
2390 counter = 0;
2391 if (each_size == RELOC_EXT_SIZE)
2392 {
2393 register struct reloc_ext_external *rptr
2394 = (struct reloc_ext_external *) dynrels;
2395
2396 for (; counter < dynrel_count; counter++, rptr++, cache_ptr++)
2397 {
2398 NAME(aout,swap_ext_reloc_in) (abfd, rptr, cache_ptr, dynsyms);
2399 cache_ptr->address -= bfd_get_section_vma (abfd, asect);
2400 if (cache_ptr->address >= bfd_section_size (abfd, asect))
2401 --cache_ptr;
2402 }
2403 }
2404 else
2405 {
2406 register struct reloc_std_external *rptr
2407 = (struct reloc_std_external *) dynrels;
2408
2409 for (; counter < dynrel_count; counter++, rptr++, cache_ptr++)
2410 {
2411 NAME(aout,swap_std_reloc_in) (abfd, rptr, cache_ptr, dynsyms);
2412 cache_ptr->address -= bfd_get_section_vma (abfd, asect);
2413 if (cache_ptr->address >= bfd_section_size (abfd, asect))
2414 --cache_ptr;
2415 }
2416 }
2417 }
2418
2419 bfd_release (abfd,relocs);
2420 asect->relocation = reloc_cache;
2421 asect->reloc_count = cache_ptr - reloc_cache;
2422 return true;
2423 }
2424
2425
2426
2427 /* Write out a relocation section into an object file. */
2428
2429 boolean
2430 DEFUN(NAME(aout,squirt_out_relocs),(abfd, section),
2431 bfd *abfd AND
2432 asection *section)
2433 {
2434 arelent **generic;
2435 unsigned char *native, *natptr;
2436 size_t each_size;
2437
2438 unsigned int count = section->reloc_count;
2439 size_t natsize;
2440
2441 if (count == 0) return true;
2442
2443 each_size = obj_reloc_entry_size (abfd);
2444 natsize = each_size * count;
2445 native = (unsigned char *) bfd_zalloc (abfd, natsize);
2446 if (!native) {
2447 bfd_error = no_memory;
2448 return false;
2449 }
2450
2451 generic = section->orelocation;
2452
2453 if (each_size == RELOC_EXT_SIZE)
2454 {
2455 for (natptr = native;
2456 count != 0;
2457 --count, natptr += each_size, ++generic)
2458 NAME(aout,swap_ext_reloc_out) (abfd, *generic, (struct reloc_ext_external *)natptr);
2459 }
2460 else
2461 {
2462 for (natptr = native;
2463 count != 0;
2464 --count, natptr += each_size, ++generic)
2465 NAME(aout,swap_std_reloc_out)(abfd, *generic, (struct reloc_std_external *)natptr);
2466 }
2467
2468 if ( bfd_write ((PTR) native, 1, natsize, abfd) != natsize) {
2469 bfd_release(abfd, native);
2470 return false;
2471 }
2472 bfd_release (abfd, native);
2473
2474 return true;
2475 }
2476
2477 /* This is stupid. This function should be a boolean predicate */
2478 unsigned int
2479 DEFUN(NAME(aout,canonicalize_reloc),(abfd, section, relptr, symbols),
2480 bfd *abfd AND
2481 sec_ptr section AND
2482 arelent **relptr AND
2483 asymbol **symbols)
2484 {
2485 arelent *tblptr = section->relocation;
2486 unsigned int count;
2487
2488 if (!(tblptr || NAME(aout,slurp_reloc_table)(abfd, section, symbols)))
2489 return 0;
2490
2491 if (section->flags & SEC_CONSTRUCTOR) {
2492 arelent_chain *chain = section->constructor_chain;
2493 for (count = 0; count < section->reloc_count; count ++) {
2494 *relptr ++ = &chain->relent;
2495 chain = chain->next;
2496 }
2497 }
2498 else {
2499 tblptr = section->relocation;
2500 if (!tblptr) return 0;
2501
2502 for (count = 0; count++ < section->reloc_count;)
2503 {
2504 *relptr++ = tblptr++;
2505 }
2506 }
2507 *relptr = 0;
2508
2509 return section->reloc_count;
2510 }
2511
2512 unsigned int
2513 DEFUN(NAME(aout,get_reloc_upper_bound),(abfd, asect),
2514 bfd *abfd AND
2515 sec_ptr asect)
2516 {
2517 bfd_size_type dynrel_count = 0;
2518
2519 if (bfd_get_format (abfd) != bfd_object) {
2520 bfd_error = invalid_operation;
2521 return 0;
2522 }
2523 if (asect->flags & SEC_CONSTRUCTOR) {
2524 return (sizeof (arelent *) * (asect->reloc_count+1));
2525 }
2526
2527 if ((bfd_get_file_flags (abfd) & DYNAMIC) != 0
2528 && aout_backend_info (abfd)->read_dynamic_relocs)
2529 {
2530 PTR dynrels;
2531
2532 dynrel_count = ((*aout_backend_info (abfd)->read_dynamic_relocs)
2533 (abfd, &dynrels));
2534 if (dynrel_count == (bfd_size_type) -1)
2535 return 0;
2536 }
2537
2538 if (asect == obj_datasec (abfd))
2539 return (sizeof (arelent *) *
2540 ((exec_hdr(abfd)->a_drsize / obj_reloc_entry_size (abfd))
2541 + dynrel_count + 1));
2542
2543 if (asect == obj_textsec (abfd))
2544 return (sizeof (arelent *) *
2545 ((exec_hdr(abfd)->a_trsize / obj_reloc_entry_size (abfd))
2546 + dynrel_count + 1));
2547
2548 bfd_error = invalid_operation;
2549 return 0;
2550 }
2551
2552 \f
2553 unsigned int
2554 DEFUN(NAME(aout,get_symtab_upper_bound),(abfd),
2555 bfd *abfd)
2556 {
2557 if (!NAME(aout,slurp_symbol_table)(abfd)) return 0;
2558
2559 return (bfd_get_symcount (abfd)+1) * (sizeof (aout_symbol_type *));
2560 }
2561
2562 /*ARGSUSED*/
2563 alent *
2564 DEFUN(NAME(aout,get_lineno),(ignore_abfd, ignore_symbol),
2565 bfd *ignore_abfd AND
2566 asymbol *ignore_symbol)
2567 {
2568 return (alent *)NULL;
2569 }
2570
2571 /*ARGSUSED*/
2572 void
2573 DEFUN(NAME(aout,get_symbol_info),(ignore_abfd, symbol, ret),
2574 bfd *ignore_abfd AND
2575 asymbol *symbol AND
2576 symbol_info *ret)
2577 {
2578 bfd_symbol_info (symbol, ret);
2579
2580 if (ret->type == '?')
2581 {
2582 int type_code = aout_symbol(symbol)->type & 0xff;
2583 CONST char *stab_name = aout_stab_name(type_code);
2584 static char buf[10];
2585
2586 if (stab_name == NULL)
2587 {
2588 sprintf(buf, "(%d)", type_code);
2589 stab_name = buf;
2590 }
2591 ret->type = '-';
2592 ret->stab_other = (unsigned)(aout_symbol(symbol)->other & 0xff);
2593 ret->stab_desc = (unsigned)(aout_symbol(symbol)->desc & 0xffff);
2594 ret->stab_name = stab_name;
2595 }
2596 }
2597
2598 /*ARGSUSED*/
2599 void
2600 DEFUN(NAME(aout,print_symbol),(ignore_abfd, afile, symbol, how),
2601 bfd *ignore_abfd AND
2602 PTR afile AND
2603 asymbol *symbol AND
2604 bfd_print_symbol_type how)
2605 {
2606 FILE *file = (FILE *)afile;
2607
2608 switch (how) {
2609 case bfd_print_symbol_name:
2610 if (symbol->name)
2611 fprintf(file,"%s", symbol->name);
2612 break;
2613 case bfd_print_symbol_more:
2614 fprintf(file,"%4x %2x %2x",(unsigned)(aout_symbol(symbol)->desc & 0xffff),
2615 (unsigned)(aout_symbol(symbol)->other & 0xff),
2616 (unsigned)(aout_symbol(symbol)->type));
2617 break;
2618 case bfd_print_symbol_all:
2619 {
2620 CONST char *section_name = symbol->section->name;
2621
2622
2623 bfd_print_symbol_vandf((PTR)file,symbol);
2624
2625 fprintf(file," %-5s %04x %02x %02x",
2626 section_name,
2627 (unsigned)(aout_symbol(symbol)->desc & 0xffff),
2628 (unsigned)(aout_symbol(symbol)->other & 0xff),
2629 (unsigned)(aout_symbol(symbol)->type & 0xff));
2630 if (symbol->name)
2631 fprintf(file," %s", symbol->name);
2632 }
2633 break;
2634 }
2635 }
2636
2637 /*
2638 provided a BFD, a section and an offset into the section, calculate
2639 and return the name of the source file and the line nearest to the
2640 wanted location.
2641 */
2642
2643 boolean
2644 DEFUN(NAME(aout,find_nearest_line),(abfd,
2645 section,
2646 symbols,
2647 offset,
2648 filename_ptr,
2649 functionname_ptr,
2650 line_ptr),
2651 bfd *abfd AND
2652 asection *section AND
2653 asymbol **symbols AND
2654 bfd_vma offset AND
2655 CONST char **filename_ptr AND
2656 CONST char **functionname_ptr AND
2657 unsigned int *line_ptr)
2658 {
2659 /* Run down the file looking for the filename, function and linenumber */
2660 asymbol **p;
2661 static char buffer[100];
2662 static char filename_buffer[200];
2663 CONST char *directory_name = NULL;
2664 CONST char *main_file_name = NULL;
2665 CONST char *current_file_name = NULL;
2666 CONST char *line_file_name = NULL; /* Value of current_file_name at line number. */
2667 bfd_vma high_line_vma = ~0;
2668 bfd_vma low_func_vma = 0;
2669 asymbol *func = 0;
2670 *filename_ptr = abfd->filename;
2671 *functionname_ptr = 0;
2672 *line_ptr = 0;
2673 if (symbols != (asymbol **)NULL) {
2674 for (p = symbols; *p; p++) {
2675 aout_symbol_type *q = (aout_symbol_type *)(*p);
2676 next:
2677 switch (q->type){
2678 case N_SO:
2679 main_file_name = current_file_name = q->symbol.name;
2680 /* Look ahead to next symbol to check if that too is an N_SO. */
2681 p++;
2682 if (*p == NULL)
2683 break;
2684 q = (aout_symbol_type *)(*p);
2685 if (q->type != (int)N_SO)
2686 goto next;
2687
2688 /* Found a second N_SO First is directory; second is filename. */
2689 directory_name = current_file_name;
2690 main_file_name = current_file_name = q->symbol.name;
2691 if (obj_textsec(abfd) != section)
2692 goto done;
2693 break;
2694 case N_SOL:
2695 current_file_name = q->symbol.name;
2696 break;
2697
2698 case N_SLINE:
2699
2700 case N_DSLINE:
2701 case N_BSLINE:
2702 /* We'll keep this if it resolves nearer than the one we have already */
2703 if (q->symbol.value >= offset &&
2704 q->symbol.value < high_line_vma) {
2705 *line_ptr = q->desc;
2706 high_line_vma = q->symbol.value;
2707 line_file_name = current_file_name;
2708 }
2709 break;
2710 case N_FUN:
2711 {
2712 /* We'll keep this if it is nearer than the one we have already */
2713 if (q->symbol.value >= low_func_vma &&
2714 q->symbol.value <= offset) {
2715 low_func_vma = q->symbol.value;
2716 func = (asymbol *)q;
2717 }
2718 if (*line_ptr && func) {
2719 CONST char *function = func->name;
2720 char *p;
2721 strncpy(buffer, function, sizeof(buffer)-1);
2722 buffer[sizeof(buffer)-1] = 0;
2723 /* Have to remove : stuff */
2724 p = strchr(buffer,':');
2725 if (p != NULL) { *p = '\0'; }
2726 *functionname_ptr = buffer;
2727 goto done;
2728
2729 }
2730 }
2731 break;
2732 }
2733 }
2734 }
2735
2736 done:
2737 if (*line_ptr)
2738 main_file_name = line_file_name;
2739 if (main_file_name) {
2740 if (main_file_name[0] == '/' || directory_name == NULL)
2741 *filename_ptr = main_file_name;
2742 else {
2743 sprintf(filename_buffer, "%.140s%.50s",
2744 directory_name, main_file_name);
2745 *filename_ptr = filename_buffer;
2746 }
2747 }
2748 return true;
2749
2750 }
2751
2752 /*ARGSUSED*/
2753 int
2754 DEFUN(NAME(aout,sizeof_headers),(abfd, execable),
2755 bfd *abfd AND
2756 boolean execable)
2757 {
2758 return adata(abfd).exec_bytes_size;
2759 }
2760 \f
2761 /* a.out link code. */
2762
2763 /* a.out linker hash table entries. */
2764
2765 struct aout_link_hash_entry
2766 {
2767 struct bfd_link_hash_entry root;
2768 /* Symbol index in output file. */
2769 int indx;
2770 };
2771
2772 /* a.out linker hash table. */
2773
2774 struct aout_link_hash_table
2775 {
2776 struct bfd_link_hash_table root;
2777 };
2778
2779 static struct bfd_hash_entry *aout_link_hash_newfunc
2780 PARAMS ((struct bfd_hash_entry *entry,
2781 struct bfd_hash_table *table,
2782 const char *string));
2783 static boolean aout_link_add_object_symbols
2784 PARAMS ((bfd *, struct bfd_link_info *));
2785 static boolean aout_link_check_archive_element
2786 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
2787 static boolean aout_link_get_symbols PARAMS ((bfd *));
2788 static boolean aout_link_free_symbols PARAMS ((bfd *));
2789 static boolean aout_link_check_ar_symbols
2790 PARAMS ((bfd *, struct bfd_link_info *, boolean *pneeded));
2791 static boolean aout_link_add_symbols
2792 PARAMS ((bfd *, struct bfd_link_info *));
2793
2794 /* Routine to create an entry in an a.out link hash table. */
2795
2796 static struct bfd_hash_entry *
2797 aout_link_hash_newfunc (entry, table, string)
2798 struct bfd_hash_entry *entry;
2799 struct bfd_hash_table *table;
2800 const char *string;
2801 {
2802 struct aout_link_hash_entry *ret = (struct aout_link_hash_entry *) entry;
2803
2804 /* Allocate the structure if it has not already been allocated by a
2805 subclass. */
2806 if (ret == (struct aout_link_hash_entry *) NULL)
2807 ret = ((struct aout_link_hash_entry *)
2808 bfd_hash_allocate (table, sizeof (struct aout_link_hash_entry)));
2809
2810 /* Call the allocation method of the superclass. */
2811 ret = ((struct aout_link_hash_entry *)
2812 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2813 table, string));
2814
2815 /* Set local fields. */
2816 ret->indx = -1;
2817
2818 return (struct bfd_hash_entry *) ret;
2819 }
2820
2821 /* Create an a.out link hash table. */
2822
2823 struct bfd_link_hash_table *
2824 NAME(aout,link_hash_table_create) (abfd)
2825 bfd *abfd;
2826 {
2827 struct aout_link_hash_table *ret;
2828
2829 ret = ((struct aout_link_hash_table *)
2830 bfd_xmalloc (sizeof (struct aout_link_hash_table)));
2831 if (! _bfd_link_hash_table_init (&ret->root, abfd,
2832 aout_link_hash_newfunc))
2833 {
2834 free (ret);
2835 return (struct bfd_link_hash_table *) NULL;
2836 }
2837 return &ret->root;
2838 }
2839
2840 /* Look up an entry in an a.out link hash table. */
2841
2842 #define aout_link_hash_lookup(table, string, create, copy, follow) \
2843 ((struct aout_link_hash_entry *) \
2844 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy), (follow)))
2845
2846 /* Traverse an a.out link hash table. */
2847
2848 #define aout_link_hash_traverse(table, func, info) \
2849 (bfd_link_hash_traverse \
2850 (&(table)->root, \
2851 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
2852 (info)))
2853
2854 /* Get the a.out link hash table from the info structure. This is
2855 just a cast. */
2856
2857 #define aout_hash_table(p) ((struct aout_link_hash_table *) ((p)->hash))
2858
2859 /* Given an a.out BFD, add symbols to the global hash table as
2860 appropriate. */
2861
2862 boolean
2863 NAME(aout,link_add_symbols) (abfd, info)
2864 bfd *abfd;
2865 struct bfd_link_info *info;
2866 {
2867 switch (bfd_get_format (abfd))
2868 {
2869 case bfd_object:
2870 return aout_link_add_object_symbols (abfd, info);
2871 case bfd_archive:
2872 return _bfd_generic_link_add_archive_symbols
2873 (abfd, info, aout_link_check_archive_element);
2874 default:
2875 bfd_error = wrong_format;
2876 return false;
2877 }
2878 }
2879
2880 /* Add symbols from an a.out object file. */
2881
2882 static boolean
2883 aout_link_add_object_symbols (abfd, info)
2884 bfd *abfd;
2885 struct bfd_link_info *info;
2886 {
2887 if (! aout_link_get_symbols (abfd))
2888 return false;
2889 if (! aout_link_add_symbols (abfd, info))
2890 return false;
2891 if (! info->keep_memory)
2892 {
2893 if (! aout_link_free_symbols (abfd))
2894 return false;
2895 }
2896 return true;
2897 }
2898
2899 /* Check a single archive element to see if we need to include it in
2900 the link. *PNEEDED is set according to whether this element is
2901 needed in the link or not. This is called from
2902 _bfd_generic_link_add_archive_symbols. */
2903
2904 static boolean
2905 aout_link_check_archive_element (abfd, info, pneeded)
2906 bfd *abfd;
2907 struct bfd_link_info *info;
2908 boolean *pneeded;
2909 {
2910 if (! aout_link_get_symbols (abfd))
2911 return false;
2912
2913 if (! aout_link_check_ar_symbols (abfd, info, pneeded))
2914 return false;
2915
2916 if (*pneeded)
2917 {
2918 if (! aout_link_add_symbols (abfd, info))
2919 return false;
2920 }
2921
2922 /* We keep around the symbols even if we aren't going to use this
2923 object file, because we may want to reread it. This doesn't
2924 waste too much memory, because it isn't all that common to read
2925 an archive element but not need it. */
2926 if (! info->keep_memory)
2927 {
2928 if (! aout_link_free_symbols (abfd))
2929 return false;
2930 }
2931
2932 return true;
2933 }
2934
2935 /* Read the internal symbols from an a.out file. */
2936
2937 static boolean
2938 aout_link_get_symbols (abfd)
2939 bfd *abfd;
2940 {
2941 bfd_size_type count;
2942 struct external_nlist *syms;
2943 unsigned char string_chars[BYTES_IN_WORD];
2944 bfd_size_type stringsize;
2945 char *strings;
2946
2947 if (obj_aout_external_syms (abfd) != (struct external_nlist *) NULL)
2948 {
2949 /* We already have them. */
2950 return true;
2951 }
2952
2953 count = exec_hdr (abfd)->a_syms / EXTERNAL_NLIST_SIZE;
2954
2955 /* We allocate using bfd_xmalloc to make the values easy to free
2956 later on. If we put them on the obstack it might not be possible
2957 to free them. */
2958 syms = ((struct external_nlist *)
2959 bfd_xmalloc ((size_t) count * EXTERNAL_NLIST_SIZE));
2960
2961 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
2962 || (bfd_read ((PTR) syms, 1, exec_hdr (abfd)->a_syms, abfd)
2963 != exec_hdr (abfd)->a_syms))
2964 return false;
2965
2966 /* Get the size of the strings. */
2967 if (bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET) != 0
2968 || (bfd_read ((PTR) string_chars, BYTES_IN_WORD, 1, abfd)
2969 != BYTES_IN_WORD))
2970 return false;
2971 stringsize = GET_WORD (abfd, string_chars);
2972 strings = (char *) bfd_xmalloc ((size_t) stringsize);
2973
2974 /* Skip space for the string count in the buffer for convenience
2975 when using indexes. */
2976 if (bfd_read (strings + BYTES_IN_WORD, 1, stringsize - BYTES_IN_WORD, abfd)
2977 != stringsize - BYTES_IN_WORD)
2978 return false;
2979
2980 /* Save the data. */
2981 obj_aout_external_syms (abfd) = syms;
2982 obj_aout_external_sym_count (abfd) = count;
2983 obj_aout_external_strings (abfd) = strings;
2984
2985 return true;
2986 }
2987
2988 /* Free up the internal symbols read from an a.out file. */
2989
2990 static boolean
2991 aout_link_free_symbols (abfd)
2992 bfd *abfd;
2993 {
2994 if (obj_aout_external_syms (abfd) != (struct external_nlist *) NULL)
2995 {
2996 free ((PTR) obj_aout_external_syms (abfd));
2997 obj_aout_external_syms (abfd) = (struct external_nlist *) NULL;
2998 }
2999 if (obj_aout_external_strings (abfd) != (char *) NULL)
3000 {
3001 free ((PTR) obj_aout_external_strings (abfd));
3002 obj_aout_external_strings (abfd) = (char *) NULL;
3003 }
3004 return true;
3005 }
3006
3007 /* Look through the internal symbols to see if this object file should
3008 be included in the link. We should include this object file if it
3009 defines any symbols which are currently undefined. If this object
3010 file defines a common symbol, then we may adjust the size of the
3011 known symbol but we do not include the object file in the link
3012 (unless there is some other reason to include it). */
3013
3014 static boolean
3015 aout_link_check_ar_symbols (abfd, info, pneeded)
3016 bfd *abfd;
3017 struct bfd_link_info *info;
3018 boolean *pneeded;
3019 {
3020 register struct external_nlist *p;
3021 struct external_nlist *pend;
3022 char *strings;
3023
3024 *pneeded = false;
3025
3026 /* Look through all the symbols. */
3027 p = obj_aout_external_syms (abfd);
3028 pend = p + obj_aout_external_sym_count (abfd);
3029 strings = obj_aout_external_strings (abfd);
3030 for (; p < pend; p++)
3031 {
3032 int type = bfd_h_get_8 (abfd, p->e_type);
3033 const char *name;
3034 struct bfd_link_hash_entry *h;
3035
3036 /* Ignore symbols that are not externally visible. */
3037 if ((type & N_EXT) == 0)
3038 continue;
3039
3040 name = strings + GET_WORD (abfd, p->e_strx);
3041 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
3042
3043 /* We are only interested in symbols that are currently
3044 undefined or common. */
3045 if (h == (struct bfd_link_hash_entry *) NULL
3046 || (h->type != bfd_link_hash_undefined
3047 && h->type != bfd_link_hash_common))
3048 continue;
3049
3050 if ((type & (N_TEXT | N_DATA | N_BSS)) != 0)
3051 {
3052 /* This object file defines this symbol. We must link it
3053 in. This is true regardless of whether the current
3054 definition of the symbol is undefined or common. If the
3055 current definition is common, we have a case in which we
3056 have already seen an object file including
3057 int a;
3058 and this object file from the archive includes
3059 int a = 5;
3060 In such a case we must include this object file. */
3061 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
3062 return false;
3063 *pneeded = true;
3064 return true;
3065 }
3066
3067 if (type == (N_EXT | N_UNDF))
3068 {
3069 bfd_vma value;
3070
3071 value = GET_WORD (abfd, p->e_value);
3072 if (value != 0)
3073 {
3074 /* This symbol is common in the object from the archive
3075 file. */
3076 if (h->type == bfd_link_hash_undefined)
3077 {
3078 bfd *symbfd;
3079
3080 symbfd = h->u.undef.abfd;
3081 if (symbfd == (bfd *) NULL)
3082 {
3083 /* This symbol was created as undefined from
3084 outside BFD. We assume that we should link
3085 in the object file. This is done for the -u
3086 option in the linker. */
3087 if (! (*info->callbacks->add_archive_element) (info,
3088 abfd,
3089 name))
3090 return false;
3091 *pneeded = true;
3092 return true;
3093 }
3094 /* Turn the current link symbol into a common
3095 symbol. It is already on the undefs list. */
3096 h->type = bfd_link_hash_common;
3097 h->u.c.size = value;
3098 h->u.c.section = bfd_make_section_old_way (symbfd,
3099 "COMMON");
3100 }
3101 else
3102 {
3103 /* Adjust the size of the common symbol if
3104 necessary. */
3105 if (value > h->u.c.size)
3106 h->u.c.size = value;
3107 }
3108 }
3109 }
3110 }
3111
3112 /* We do not need this object file. */
3113 return true;
3114 }
3115
3116 /* Add all symbols from an object file to the hash table. */
3117
3118 static boolean
3119 aout_link_add_symbols (abfd, info)
3120 bfd *abfd;
3121 struct bfd_link_info *info;
3122 {
3123 bfd_size_type sym_count;
3124 char *strings;
3125 boolean copy;
3126 struct aout_link_hash_entry **sym_hash;
3127 register struct external_nlist *p;
3128 struct external_nlist *pend;
3129
3130 sym_count = obj_aout_external_sym_count (abfd);
3131 strings = obj_aout_external_strings (abfd);
3132 if (info->keep_memory)
3133 copy = false;
3134 else
3135 copy = true;
3136
3137 /* We keep a list of the linker hash table entries that correspond
3138 to particular symbols. We could just look them up in the hash
3139 table, but keeping the list is more efficient. Perhaps this
3140 should be conditional on info->keep_memory. */
3141 sym_hash = ((struct aout_link_hash_entry **)
3142 bfd_alloc (abfd,
3143 ((size_t) sym_count
3144 * sizeof (struct aout_link_hash_entry *))));
3145 obj_aout_sym_hashes (abfd) = sym_hash;
3146
3147 p = obj_aout_external_syms (abfd);
3148 pend = p + sym_count;
3149 for (; p < pend; p++, sym_hash++)
3150 {
3151 int type;
3152 const char *name;
3153 bfd_vma value;
3154 asection *section;
3155 flagword flags;
3156 const char *string;
3157
3158 *sym_hash = NULL;
3159
3160 type = bfd_h_get_8 (abfd, p->e_type);
3161
3162 /* Ignore debugging symbols. */
3163 if ((type & N_STAB) != 0)
3164 continue;
3165
3166 /* Ignore symbols that are not external. */
3167 if ((type & N_EXT) == 0
3168 && type != N_WARNING
3169 && type != N_SETA
3170 && type != N_SETT
3171 && type != N_SETD
3172 && type != N_SETB)
3173 {
3174 /* If this is an N_INDR symbol we must skip the next entry,
3175 which is the symbol to indirect to (actually, an N_INDR
3176 symbol without N_EXT set is pretty useless). */
3177 if (type == N_INDR)
3178 {
3179 ++p;
3180 ++sym_hash;
3181 }
3182 continue;
3183 }
3184
3185 /* Ignore N_FN symbols (these appear to have N_EXT set). */
3186 if (type == N_FN)
3187 continue;
3188
3189 name = strings + GET_WORD (abfd, p->e_strx);
3190 value = GET_WORD (abfd, p->e_value);
3191 flags = BSF_GLOBAL;
3192 string = NULL;
3193 switch (type)
3194 {
3195 default:
3196 abort ();
3197 case N_UNDF | N_EXT:
3198 if (value != 0)
3199 section = &bfd_com_section;
3200 else
3201 section = &bfd_und_section;
3202 break;
3203 case N_ABS | N_EXT:
3204 section = &bfd_abs_section;
3205 break;
3206 case N_TEXT | N_EXT:
3207 section = obj_textsec (abfd);
3208 value -= bfd_get_section_vma (abfd, section);
3209 break;
3210 case N_DATA | N_EXT:
3211 section = obj_datasec (abfd);
3212 value -= bfd_get_section_vma (abfd, section);
3213 break;
3214 case N_BSS | N_EXT:
3215 section = obj_bsssec (abfd);
3216 value -= bfd_get_section_vma (abfd, section);
3217 break;
3218 case N_INDR | N_EXT:
3219 /* An indirect symbol. The next symbol is the symbol
3220 which this one really is. */
3221 BFD_ASSERT (p + 1 < pend);
3222 ++p;
3223 string = strings + GET_WORD (abfd, p->e_strx);
3224 section = &bfd_ind_section;
3225 flags |= BSF_INDIRECT;
3226 break;
3227 case N_COMM | N_EXT:
3228 section = &bfd_com_section;
3229 break;
3230 case N_SETA: case N_SETA | N_EXT:
3231 section = &bfd_abs_section;
3232 flags |= BSF_CONSTRUCTOR;
3233 break;
3234 case N_SETT: case N_SETT | N_EXT:
3235 section = obj_textsec (abfd);
3236 flags |= BSF_CONSTRUCTOR;
3237 value -= bfd_get_section_vma (abfd, section);
3238 break;
3239 case N_SETD: case N_SETD | N_EXT:
3240 section = obj_datasec (abfd);
3241 flags |= BSF_CONSTRUCTOR;
3242 value -= bfd_get_section_vma (abfd, section);
3243 break;
3244 case N_SETB: case N_SETB | N_EXT:
3245 section = obj_bsssec (abfd);
3246 flags |= BSF_CONSTRUCTOR;
3247 value -= bfd_get_section_vma (abfd, section);
3248 break;
3249 case N_WARNING:
3250 /* A warning symbol. The next symbol is the one to warn
3251 about. */
3252 BFD_ASSERT (p + 1 < pend);
3253 ++p;
3254 string = name;
3255 name = strings + GET_WORD (abfd, p->e_strx);
3256 section = &bfd_und_section;
3257 flags |= BSF_WARNING;
3258 break;
3259 }
3260
3261 if (! (_bfd_generic_link_add_one_symbol
3262 (info, abfd, name, flags, section, value, string, copy, false,
3263 ARCH_SIZE, (struct bfd_link_hash_entry **) sym_hash)))
3264 return false;
3265
3266 if (type == (N_INDR | N_EXT) || type == N_WARNING)
3267 ++sym_hash;
3268 }
3269
3270 return true;
3271 }
3272
3273 /* During the final link step we need to pass around a bunch of
3274 information, so we do it in an instance of this structure. */
3275
3276 struct aout_final_link_info
3277 {
3278 /* General link information. */
3279 struct bfd_link_info *info;
3280 /* Output bfd. */
3281 bfd *output_bfd;
3282 /* Reloc file positions. */
3283 file_ptr treloff, dreloff;
3284 /* File position of symbols. */
3285 file_ptr symoff;
3286 /* String table. */
3287 struct stringtab_data strtab;
3288 };
3289
3290 static boolean aout_link_input_bfd
3291 PARAMS ((struct aout_final_link_info *, bfd *input_bfd));
3292 static boolean aout_link_write_symbols
3293 PARAMS ((struct aout_final_link_info *, bfd *input_bfd, int *symbol_map));
3294 static boolean aout_link_write_other_symbol
3295 PARAMS ((struct aout_link_hash_entry *, PTR));
3296 static boolean aout_link_input_section
3297 PARAMS ((struct aout_final_link_info *, bfd *input_bfd,
3298 asection *input_section, file_ptr *reloff_ptr,
3299 bfd_size_type rel_size, int *symbol_map));
3300 static boolean aout_link_input_section_std
3301 PARAMS ((struct aout_final_link_info *, bfd *input_bfd,
3302 asection *input_section, struct reloc_std_external *,
3303 bfd_size_type rel_size, bfd_byte *contents, int *symbol_map));
3304 static boolean aout_link_input_section_ext
3305 PARAMS ((struct aout_final_link_info *, bfd *input_bfd,
3306 asection *input_section, struct reloc_ext_external *,
3307 bfd_size_type rel_size, bfd_byte *contents, int *symbol_map));
3308 static INLINE asection *aout_reloc_index_to_section
3309 PARAMS ((bfd *, int));
3310
3311 /* Do the final link step. This is called on the output BFD. The
3312 INFO structure should point to a list of BFDs linked through the
3313 link_next field which can be used to find each BFD which takes part
3314 in the output. Also, each section in ABFD should point to a list
3315 of bfd_link_order structures which list all the input sections for
3316 the output section. */
3317
3318 boolean
3319 NAME(aout,final_link) (abfd, info, callback)
3320 bfd *abfd;
3321 struct bfd_link_info *info;
3322 void (*callback) PARAMS ((bfd *, file_ptr *, file_ptr *, file_ptr *));
3323 {
3324 struct aout_final_link_info aout_info;
3325 register bfd *sub;
3326 bfd_size_type text_size;
3327 file_ptr text_end;
3328 register struct bfd_link_order *p;
3329 asection *o;
3330
3331 aout_info.info = info;
3332 aout_info.output_bfd = abfd;
3333
3334 if (! info->relocateable)
3335 {
3336 exec_hdr (abfd)->a_trsize = 0;
3337 exec_hdr (abfd)->a_drsize = 0;
3338 }
3339 else
3340 {
3341 bfd_size_type trsize, drsize;
3342
3343 /* Count up the relocation sizes. */
3344 trsize = 0;
3345 drsize = 0;
3346 for (sub = info->input_bfds; sub != (bfd *) NULL; sub = sub->link_next)
3347 {
3348 if (bfd_get_flavour (abfd) == bfd_target_aout_flavour)
3349 {
3350 trsize += exec_hdr (sub)->a_trsize;
3351 drsize += exec_hdr (sub)->a_drsize;
3352 }
3353 else
3354 {
3355 /* FIXME: We need to identify the .text and .data sections
3356 and call get_reloc_upper_bound and canonicalize_reloc to
3357 work out the number of relocs needed, and then multiply
3358 by the reloc size. */
3359 abort ();
3360 }
3361 }
3362 exec_hdr (abfd)->a_trsize = trsize;
3363 exec_hdr (abfd)->a_drsize = drsize;
3364 }
3365
3366 exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd);
3367
3368 /* Adjust the section sizes and vmas according to the magic number.
3369 This sets a_text, a_data and a_bss in the exec_hdr and sets the
3370 filepos for each section. */
3371 if (! NAME(aout,adjust_sizes_and_vmas) (abfd, &text_size, &text_end))
3372 return false;
3373
3374 /* The relocation and symbol file positions differ among a.out
3375 targets. We are passed a callback routine from the backend
3376 specific code to handle this.
3377 FIXME: At this point we do not know how much space the symbol
3378 table will require. This will not work for any (nonstandard)
3379 a.out target that needs to know the symbol table size before it
3380 can compute the relocation file positions. This may or may not
3381 be the case for the hp300hpux target, for example. */
3382 (*callback) (abfd, &aout_info.treloff, &aout_info.dreloff,
3383 &aout_info.symoff);
3384 obj_textsec (abfd)->rel_filepos = aout_info.treloff;
3385 obj_datasec (abfd)->rel_filepos = aout_info.dreloff;
3386 obj_sym_filepos (abfd) = aout_info.symoff;
3387
3388 /* We keep a count of the symbols as we output them. */
3389 obj_aout_external_sym_count (abfd) = 0;
3390
3391 /* We accumulate the string table as we write out the symbols. */
3392 stringtab_init (&aout_info.strtab);
3393
3394 /* The most time efficient way to do the link would be to read all
3395 the input object files into memory and then sort out the
3396 information into the output file. Unfortunately, that will
3397 probably use too much memory. Another method would be to step
3398 through everything that composes the text section and write it
3399 out, and then everything that composes the data section and write
3400 it out, and then write out the relocs, and then write out the
3401 symbols. Unfortunately, that requires reading stuff from each
3402 input file several times, and we will not be able to keep all the
3403 input files open simultaneously, and reopening them will be slow.
3404
3405 What we do is basically process one input file at a time. We do
3406 everything we need to do with an input file once--copy over the
3407 section contents, handle the relocation information, and write
3408 out the symbols--and then we throw away the information we read
3409 from it. This approach requires a lot of lseeks of the output
3410 file, which is unfortunate but still faster than reopening a lot
3411 of files.
3412
3413 We use the output_has_begun field of the input BFDs to see
3414 whether we have already handled it. */
3415 for (sub = info->input_bfds; sub != (bfd *) NULL; sub = sub->link_next)
3416 sub->output_has_begun = false;
3417
3418 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3419 {
3420 for (p = o->link_order_head;
3421 p != (struct bfd_link_order *) NULL;
3422 p = p->next)
3423 {
3424 /* If we might be using the C based alloca function, we need
3425 to dump the memory allocated by aout_link_input_bfd. */
3426 #ifndef __GNUC__
3427 #ifndef alloca
3428 (void) alloca (0);
3429 #endif
3430 #endif
3431 if (p->type == bfd_indirect_link_order
3432 && (bfd_get_flavour (p->u.indirect.section->owner)
3433 == bfd_target_aout_flavour))
3434 {
3435 bfd *input_bfd;
3436
3437 input_bfd = p->u.indirect.section->owner;
3438 if (! input_bfd->output_has_begun)
3439 {
3440 if (! aout_link_input_bfd (&aout_info, input_bfd))
3441 return false;
3442 input_bfd->output_has_begun = true;
3443 }
3444 }
3445 else
3446 {
3447 if (! _bfd_default_link_order (abfd, info, o, p))
3448 return false;
3449 }
3450 }
3451 }
3452
3453 /* Write out any symbols that we have not already written out. */
3454 aout_link_hash_traverse (aout_hash_table (info),
3455 aout_link_write_other_symbol,
3456 (PTR) &aout_info);
3457
3458 /* Update the header information. */
3459 abfd->symcount = obj_aout_external_sym_count (abfd);
3460 exec_hdr (abfd)->a_syms = abfd->symcount * EXTERNAL_NLIST_SIZE;
3461 obj_str_filepos (abfd) = obj_sym_filepos (abfd) + exec_hdr (abfd)->a_syms;
3462 obj_textsec (abfd)->reloc_count =
3463 exec_hdr (abfd)->a_trsize / obj_reloc_entry_size (abfd);
3464 obj_datasec (abfd)->reloc_count =
3465 exec_hdr (abfd)->a_drsize / obj_reloc_entry_size (abfd);
3466
3467 /* Write out the string table. */
3468 if (bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET) != 0)
3469 return false;
3470 emit_strtab (abfd, &aout_info.strtab);
3471
3472 return true;
3473 }
3474
3475 /* Link an a.out input BFD into the output file. */
3476
3477 static boolean
3478 aout_link_input_bfd (finfo, input_bfd)
3479 struct aout_final_link_info *finfo;
3480 bfd *input_bfd;
3481 {
3482 bfd_size_type sym_count;
3483 int *symbol_map;
3484
3485 BFD_ASSERT (bfd_get_format (input_bfd) == bfd_object);
3486
3487 /* Get the symbols. We probably have them already, unless
3488 finfo->info->keep_memory is false. */
3489 if (! aout_link_get_symbols (input_bfd))
3490 return false;
3491
3492 sym_count = obj_aout_external_sym_count (input_bfd);
3493 symbol_map = (int *) alloca ((size_t) sym_count * sizeof (int));
3494
3495 /* Write out the symbols and get a map of the new indices. */
3496 if (! aout_link_write_symbols (finfo, input_bfd, symbol_map))
3497 return false;
3498
3499 /* Relocate and write out the sections. */
3500 if (! aout_link_input_section (finfo, input_bfd,
3501 obj_textsec (input_bfd),
3502 &finfo->treloff,
3503 exec_hdr (input_bfd)->a_trsize,
3504 symbol_map)
3505 || ! aout_link_input_section (finfo, input_bfd,
3506 obj_datasec (input_bfd),
3507 &finfo->dreloff,
3508 exec_hdr (input_bfd)->a_drsize,
3509 symbol_map))
3510 return false;
3511
3512 /* If we are not keeping memory, we don't need the symbols any
3513 longer. We still need them if we are keeping memory, because the
3514 strings in the hash table point into them. */
3515 if (! finfo->info->keep_memory)
3516 {
3517 if (! aout_link_free_symbols (input_bfd))
3518 return false;
3519 }
3520
3521 return true;
3522 }
3523
3524 /* Adjust and write out the symbols for an a.out file. Set the new
3525 symbol indices into a symbol_map. */
3526
3527 static boolean
3528 aout_link_write_symbols (finfo, input_bfd, symbol_map)
3529 struct aout_final_link_info *finfo;
3530 bfd *input_bfd;
3531 int *symbol_map;
3532 {
3533 bfd *output_bfd;
3534 bfd_size_type sym_count;
3535 char *strings;
3536 enum bfd_link_strip strip;
3537 enum bfd_link_discard discard;
3538 struct external_nlist *output_syms;
3539 struct external_nlist *outsym;
3540 register struct external_nlist *sym;
3541 struct external_nlist *sym_end;
3542 struct aout_link_hash_entry **sym_hash;
3543 boolean pass;
3544 boolean skip_indirect;
3545
3546 output_bfd = finfo->output_bfd;
3547 sym_count = obj_aout_external_sym_count (input_bfd);
3548 strings = obj_aout_external_strings (input_bfd);
3549 strip = finfo->info->strip;
3550 discard = finfo->info->discard;
3551 output_syms = ((struct external_nlist *)
3552 alloca ((size_t) (sym_count + 1) * EXTERNAL_NLIST_SIZE));
3553 outsym = output_syms;
3554
3555 /* First write out a symbol for this object file, unless we are
3556 discarding such symbols. */
3557 if (strip != strip_all
3558 && (strip != strip_some
3559 || bfd_hash_lookup (finfo->info->keep_hash, input_bfd->filename,
3560 false, false) != NULL)
3561 && discard != discard_all)
3562 {
3563 bfd_h_put_8 (output_bfd, N_TEXT, outsym->e_type);
3564 bfd_h_put_8 (output_bfd, 0, outsym->e_other);
3565 bfd_h_put_16 (output_bfd, (bfd_vma) 0, outsym->e_desc);
3566 PUT_WORD (output_bfd,
3567 add_to_stringtab (output_bfd, input_bfd->filename,
3568 &finfo->strtab),
3569 outsym->e_strx);
3570 PUT_WORD (output_bfd,
3571 bfd_get_section_vma (input_bfd, obj_textsec (input_bfd)),
3572 outsym->e_value);
3573 ++obj_aout_external_sym_count (output_bfd);
3574 ++outsym;
3575 }
3576
3577 pass = false;
3578 skip_indirect = false;
3579 sym = obj_aout_external_syms (input_bfd);
3580 sym_end = sym + sym_count;
3581 sym_hash = obj_aout_sym_hashes (input_bfd);
3582 for (; sym < sym_end; sym++, sym_hash++, symbol_map++)
3583 {
3584 const char *name;
3585 int type;
3586 boolean skip;
3587 asection *symsec;
3588 bfd_vma val = 0;
3589
3590 *symbol_map = -1;
3591
3592 type = bfd_h_get_8 (input_bfd, sym->e_type);
3593 name = strings + GET_WORD (input_bfd, sym->e_strx);
3594
3595 if (pass)
3596 {
3597 /* Pass this symbol through. It is the target of an
3598 indirect or warning symbol. */
3599 val = GET_WORD (input_bfd, sym->e_value);
3600 pass = false;
3601 }
3602 else if (skip_indirect)
3603 {
3604 /* Skip this symbol, which is the target of an indirect
3605 symbol that we have changed to no longer be an indirect
3606 symbol. */
3607 skip_indirect = false;
3608 continue;
3609 }
3610 else
3611 {
3612 struct aout_link_hash_entry *h;
3613 struct aout_link_hash_entry *hresolve;
3614
3615 /* We have saved the hash table entry for this symbol, if
3616 there is one. Note that we could just look it up again
3617 in the hash table, provided we first check that it is an
3618 external symbol. */
3619 h = *sym_hash;
3620
3621 /* If this is an indirect or warning symbol, then change
3622 hresolve to the base symbol. We also change *sym_hash so
3623 that the relocation routines relocate against the real
3624 symbol. */
3625 hresolve = h;
3626 if (h != (struct aout_link_hash_entry *) NULL
3627 && (h->root.type == bfd_link_hash_indirect
3628 || h->root.type == bfd_link_hash_warning))
3629 {
3630 hresolve = (struct aout_link_hash_entry *) h->root.u.i.link;
3631 while (hresolve->root.type == bfd_link_hash_indirect)
3632 hresolve = ((struct aout_link_hash_entry *)
3633 hresolve->root.u.i.link);
3634 *sym_hash = hresolve;
3635 }
3636
3637 /* If the symbol has already been written out, skip it. */
3638 if (h != (struct aout_link_hash_entry *) NULL
3639 && h->root.written)
3640 {
3641 *symbol_map = h->indx;
3642 continue;
3643 }
3644
3645 /* See if we are stripping this symbol. */
3646 skip = false;
3647 switch (strip)
3648 {
3649 case strip_none:
3650 break;
3651 case strip_debugger:
3652 if ((type & N_STAB) != 0)
3653 skip = true;
3654 break;
3655 case strip_some:
3656 if (bfd_hash_lookup (finfo->info->keep_hash, name, false, false)
3657 == NULL)
3658 skip = true;
3659 break;
3660 case strip_all:
3661 skip = true;
3662 break;
3663 }
3664 if (skip)
3665 {
3666 if (h != (struct aout_link_hash_entry *) NULL)
3667 h->root.written = true;
3668 continue;
3669 }
3670
3671 /* Get the value of the symbol. */
3672 if ((type & N_TYPE) == N_TEXT)
3673 symsec = obj_textsec (input_bfd);
3674 else if ((type & N_TYPE) == N_DATA)
3675 symsec = obj_datasec (input_bfd);
3676 else if ((type & N_TYPE) == N_BSS)
3677 symsec = obj_bsssec (input_bfd);
3678 else if ((type & N_TYPE) == N_ABS)
3679 symsec = &bfd_abs_section;
3680 else if (((type & N_TYPE) == N_INDR
3681 && (hresolve == (struct aout_link_hash_entry *) NULL
3682 || (hresolve->root.type != bfd_link_hash_defined
3683 && hresolve->root.type != bfd_link_hash_common)))
3684 || type == N_WARNING)
3685 {
3686 /* Pass the next symbol through unchanged. The
3687 condition above for indirect symbols is so that if
3688 the indirect symbol was defined, we output it with
3689 the correct definition so the debugger will
3690 understand it. */
3691 pass = true;
3692 val = GET_WORD (input_bfd, sym->e_value);
3693 symsec = NULL;
3694 }
3695 else if ((type & N_STAB) != 0)
3696 {
3697 val = GET_WORD (input_bfd, sym->e_value);
3698 symsec = NULL;
3699 }
3700 else
3701 {
3702 /* If we get here with an indirect symbol, it means that
3703 we are outputting it with a real definition. In such
3704 a case we do not want to output the next symbol,
3705 which is the target of the indirection. */
3706 if ((type & N_TYPE) == N_INDR)
3707 skip_indirect = true;
3708
3709 /* We need to get the value from the hash table. We use
3710 hresolve so that if we have defined an indirect
3711 symbol we output the final definition. */
3712 if (h == (struct aout_link_hash_entry *) NULL)
3713 val = 0;
3714 else if (hresolve->root.type == bfd_link_hash_defined)
3715 {
3716 asection *input_section;
3717 asection *output_section;
3718
3719 /* This case means a common symbol which was turned
3720 into a defined symbol. */
3721 input_section = hresolve->root.u.def.section;
3722 output_section = input_section->output_section;
3723 BFD_ASSERT (output_section == &bfd_abs_section
3724 || output_section->owner == output_bfd);
3725 val = (hresolve->root.u.def.value
3726 + bfd_get_section_vma (output_bfd, output_section)
3727 + input_section->output_offset);
3728
3729 /* Get the correct type based on the section. If
3730 this is a constructed set, force it to be
3731 globally visible. */
3732 if (type == N_SETT
3733 || type == N_SETD
3734 || type == N_SETB
3735 || type == N_SETA)
3736 type |= N_EXT;
3737
3738 type &=~ N_TYPE;
3739
3740 if (output_section == obj_textsec (output_bfd))
3741 type |= N_TEXT;
3742 else if (output_section == obj_datasec (output_bfd))
3743 type |= N_DATA;
3744 else if (output_section == obj_bsssec (output_bfd))
3745 type |= N_BSS;
3746 else
3747 type |= N_ABS;
3748 }
3749 else if (hresolve->root.type == bfd_link_hash_common)
3750 val = hresolve->root.u.c.size;
3751 else
3752 val = 0;
3753
3754 symsec = NULL;
3755 }
3756 if (symsec != (asection *) NULL)
3757 val = (symsec->output_section->vma
3758 + symsec->output_offset
3759 + (GET_WORD (input_bfd, sym->e_value)
3760 - symsec->vma));
3761
3762 /* If this is a global symbol set the written flag, and if
3763 it is a local symbol see if we should discard it. */
3764 if (h != (struct aout_link_hash_entry *) NULL)
3765 {
3766 h->root.written = true;
3767 h->indx = obj_aout_external_sym_count (output_bfd);
3768 }
3769 else
3770 {
3771 switch (discard)
3772 {
3773 case discard_none:
3774 break;
3775 case discard_l:
3776 if (*name == *finfo->info->lprefix
3777 && (finfo->info->lprefix_len == 1
3778 || strncmp (name, finfo->info->lprefix,
3779 finfo->info->lprefix_len) == 0))
3780 skip = true;
3781 break;
3782 case discard_all:
3783 skip = true;
3784 break;
3785 }
3786 if (skip)
3787 {
3788 pass = false;
3789 continue;
3790 }
3791 }
3792 }
3793
3794 /* Copy this symbol into the list of symbols we are going to
3795 write out. */
3796 bfd_h_put_8 (output_bfd, type, outsym->e_type);
3797 bfd_h_put_8 (output_bfd, bfd_h_get_8 (input_bfd, sym->e_other),
3798 outsym->e_other);
3799 bfd_h_put_16 (output_bfd, bfd_h_get_16 (input_bfd, sym->e_desc),
3800 outsym->e_desc);
3801 PUT_WORD (output_bfd,
3802 add_to_stringtab (output_bfd, name, &finfo->strtab),
3803 outsym->e_strx);
3804 PUT_WORD (output_bfd, val, outsym->e_value);
3805 *symbol_map = obj_aout_external_sym_count (output_bfd);
3806 ++obj_aout_external_sym_count (output_bfd);
3807 ++outsym;
3808 }
3809
3810 /* Write out the output symbols we have just constructed. */
3811 if (outsym > output_syms)
3812 {
3813 bfd_size_type outsym_count;
3814
3815 if (bfd_seek (output_bfd, finfo->symoff, SEEK_SET) != 0)
3816 return false;
3817 outsym_count = outsym - output_syms;
3818 if (bfd_write ((PTR) output_syms, (bfd_size_type) EXTERNAL_NLIST_SIZE,
3819 (bfd_size_type) outsym_count, output_bfd)
3820 != outsym_count * EXTERNAL_NLIST_SIZE)
3821 return false;
3822 finfo->symoff += outsym_count * EXTERNAL_NLIST_SIZE;
3823 }
3824
3825 return true;
3826 }
3827
3828 /* Write out a symbol that was not associated with an a.out input
3829 object. */
3830
3831 static boolean
3832 aout_link_write_other_symbol (h, data)
3833 struct aout_link_hash_entry *h;
3834 PTR data;
3835 {
3836 struct aout_final_link_info *finfo = (struct aout_final_link_info *) data;
3837 bfd *output_bfd;
3838 int type;
3839 bfd_vma val;
3840 struct external_nlist outsym;
3841
3842 if (h->root.written)
3843 return true;
3844
3845 output_bfd = finfo->output_bfd;
3846
3847 switch (h->root.type)
3848 {
3849 default:
3850 case bfd_link_hash_new:
3851 abort ();
3852 /* Avoid variable not initialized warnings. */
3853 return true;
3854 case bfd_link_hash_undefined:
3855 type = N_UNDF | N_EXT;
3856 val = 0;
3857 break;
3858 case bfd_link_hash_defined:
3859 {
3860 asection *sec;
3861
3862 sec = h->root.u.def.section;
3863 BFD_ASSERT (sec == &bfd_abs_section
3864 || sec->owner == output_bfd);
3865 if (sec == obj_textsec (output_bfd))
3866 type = N_TEXT | N_EXT;
3867 else if (sec == obj_datasec (output_bfd))
3868 type = N_DATA | N_EXT;
3869 else if (sec == obj_bsssec (output_bfd))
3870 type = N_BSS | N_EXT;
3871 else
3872 type = N_ABS | N_EXT;
3873 val = (h->root.u.def.value
3874 + sec->output_section->vma
3875 + sec->output_offset);
3876 }
3877 break;
3878 case bfd_link_hash_common:
3879 type = N_UNDF | N_EXT;
3880 val = h->root.u.c.size;
3881 break;
3882 case bfd_link_hash_indirect:
3883 case bfd_link_hash_warning:
3884 /* FIXME: Ignore these for now. The circumstances under which
3885 they should be written out are not clear to me. */
3886 return true;
3887 }
3888
3889 bfd_h_put_8 (output_bfd, type, outsym.e_type);
3890 bfd_h_put_8 (output_bfd, 0, outsym.e_other);
3891 bfd_h_put_16 (output_bfd, 0, outsym.e_desc);
3892 PUT_WORD (output_bfd,
3893 add_to_stringtab (output_bfd, h->root.root.string, &finfo->strtab),
3894 outsym.e_strx);
3895 PUT_WORD (output_bfd, val, outsym.e_value);
3896
3897 if (bfd_seek (output_bfd, finfo->symoff, SEEK_SET) != 0
3898 || bfd_write ((PTR) &outsym, (bfd_size_type) EXTERNAL_NLIST_SIZE,
3899 (bfd_size_type) 1, output_bfd) != EXTERNAL_NLIST_SIZE)
3900 {
3901 /* FIXME: No way to handle errors. */
3902 abort ();
3903 }
3904
3905 finfo->symoff += EXTERNAL_NLIST_SIZE;
3906 h->indx = obj_aout_external_sym_count (output_bfd);
3907 ++obj_aout_external_sym_count (output_bfd);
3908
3909 return true;
3910 }
3911
3912 /* Link an a.out section into the output file. */
3913
3914 static boolean
3915 aout_link_input_section (finfo, input_bfd, input_section, reloff_ptr,
3916 rel_size, symbol_map)
3917 struct aout_final_link_info *finfo;
3918 bfd *input_bfd;
3919 asection *input_section;
3920 file_ptr *reloff_ptr;
3921 bfd_size_type rel_size;
3922 int *symbol_map;
3923 {
3924 bfd_size_type input_size;
3925 bfd_byte *contents;
3926 PTR relocs;
3927
3928 /* Get the section contents. */
3929 input_size = bfd_section_size (input_bfd, input_section);
3930 contents = (bfd_byte *) alloca (input_size);
3931 if (! bfd_get_section_contents (input_bfd, input_section, (PTR) contents,
3932 (file_ptr) 0, input_size))
3933 return false;
3934
3935 /* Read in the relocs. */
3936 relocs = (PTR) alloca (rel_size);
3937 if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0
3938 || bfd_read (relocs, 1, rel_size, input_bfd) != rel_size)
3939 return false;
3940
3941 /* Relocate the section contents. */
3942 if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE)
3943 {
3944 if (! aout_link_input_section_std (finfo, input_bfd, input_section,
3945 (struct reloc_std_external *) relocs,
3946 rel_size, contents, symbol_map))
3947 return false;
3948 }
3949 else
3950 {
3951 if (! aout_link_input_section_ext (finfo, input_bfd, input_section,
3952 (struct reloc_ext_external *) relocs,
3953 rel_size, contents, symbol_map))
3954 return false;
3955 }
3956
3957 /* Write out the section contents. */
3958 if (! bfd_set_section_contents (finfo->output_bfd,
3959 input_section->output_section,
3960 (PTR) contents,
3961 input_section->output_offset,
3962 input_size))
3963 return false;
3964
3965 /* If we are producing relocateable output, the relocs were
3966 modified, and we now write them out. */
3967 if (finfo->info->relocateable)
3968 {
3969 if (bfd_seek (finfo->output_bfd, *reloff_ptr, SEEK_SET) != 0)
3970 return false;
3971 if (bfd_write (relocs, (bfd_size_type) 1, rel_size, finfo->output_bfd)
3972 != rel_size)
3973 return false;
3974 *reloff_ptr += rel_size;
3975
3976 /* Assert that the relocs have not run into the symbols, and
3977 that if these are the text relocs they have not run into the
3978 data relocs. */
3979 BFD_ASSERT (*reloff_ptr <= obj_sym_filepos (finfo->output_bfd)
3980 && (reloff_ptr != &finfo->treloff
3981 || (*reloff_ptr
3982 <= obj_datasec (finfo->output_bfd)->rel_filepos)));
3983 }
3984
3985 return true;
3986 }
3987
3988 /* Get the section corresponding to a reloc index. */
3989
3990 static INLINE asection *
3991 aout_reloc_index_to_section (abfd, indx)
3992 bfd *abfd;
3993 int indx;
3994 {
3995 switch (indx & N_TYPE)
3996 {
3997 case N_TEXT:
3998 return obj_textsec (abfd);
3999 case N_DATA:
4000 return obj_datasec (abfd);
4001 case N_BSS:
4002 return obj_bsssec (abfd);
4003 case N_ABS:
4004 return &bfd_abs_section;
4005 default:
4006 abort ();
4007 }
4008 }
4009
4010 /* Relocate an a.out section using standard a.out relocs. */
4011
4012 static boolean
4013 aout_link_input_section_std (finfo, input_bfd, input_section, relocs,
4014 rel_size, contents, symbol_map)
4015 struct aout_final_link_info *finfo;
4016 bfd *input_bfd;
4017 asection *input_section;
4018 struct reloc_std_external *relocs;
4019 bfd_size_type rel_size;
4020 bfd_byte *contents;
4021 int *symbol_map;
4022 {
4023 bfd *output_bfd;
4024 boolean relocateable;
4025 struct external_nlist *syms;
4026 char *strings;
4027 struct aout_link_hash_entry **sym_hashes;
4028 bfd_size_type reloc_count;
4029 register struct reloc_std_external *rel;
4030 struct reloc_std_external *rel_end;
4031
4032 output_bfd = finfo->output_bfd;
4033
4034 BFD_ASSERT (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE);
4035 BFD_ASSERT (input_bfd->xvec->header_byteorder_big_p
4036 == output_bfd->xvec->header_byteorder_big_p);
4037
4038 relocateable = finfo->info->relocateable;
4039 syms = obj_aout_external_syms (input_bfd);
4040 strings = obj_aout_external_strings (input_bfd);
4041 sym_hashes = obj_aout_sym_hashes (input_bfd);
4042
4043 reloc_count = rel_size / RELOC_STD_SIZE;
4044 rel = relocs;
4045 rel_end = rel + reloc_count;
4046 for (; rel < rel_end; rel++)
4047 {
4048 bfd_vma r_addr;
4049 int r_index;
4050 int r_extern;
4051 int r_pcrel;
4052 int r_baserel;
4053 int r_jmptable;
4054 int r_relative;
4055 int r_length;
4056 int howto_idx;
4057 bfd_vma relocation;
4058 bfd_reloc_status_type r;
4059
4060 r_addr = GET_SWORD (input_bfd, rel->r_address);
4061
4062 if (input_bfd->xvec->header_byteorder_big_p)
4063 {
4064 r_index = ((rel->r_index[0] << 16)
4065 | (rel->r_index[1] << 8)
4066 | rel->r_index[2]);
4067 r_extern = (0 != (rel->r_type[0] & RELOC_STD_BITS_EXTERN_BIG));
4068 r_pcrel = (0 != (rel->r_type[0] & RELOC_STD_BITS_PCREL_BIG));
4069 r_baserel = (0 != (rel->r_type[0] & RELOC_STD_BITS_BASEREL_BIG));
4070 r_jmptable= (0 != (rel->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG));
4071 r_relative= (0 != (rel->r_type[0] & RELOC_STD_BITS_RELATIVE_BIG));
4072 r_length = ((rel->r_type[0] & RELOC_STD_BITS_LENGTH_BIG)
4073 >> RELOC_STD_BITS_LENGTH_SH_BIG);
4074 }
4075 else
4076 {
4077 r_index = ((rel->r_index[2] << 16)
4078 | (rel->r_index[1] << 8)
4079 | rel->r_index[0]);
4080 r_extern = (0 != (rel->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE));
4081 r_pcrel = (0 != (rel->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
4082 r_baserel = (0 != (rel->r_type[0] & RELOC_STD_BITS_BASEREL_LITTLE));
4083 r_jmptable= (0 != (rel->r_type[0] & RELOC_STD_BITS_JMPTABLE_LITTLE));
4084 r_relative= (0 != (rel->r_type[0] & RELOC_STD_BITS_RELATIVE_LITTLE));
4085 r_length = ((rel->r_type[0] & RELOC_STD_BITS_LENGTH_LITTLE)
4086 >> RELOC_STD_BITS_LENGTH_SH_LITTLE);
4087 }
4088
4089 howto_idx = r_length + 4 * r_pcrel + 8 * r_baserel;
4090 BFD_ASSERT (howto_idx < TABLE_SIZE (howto_table_std));
4091 BFD_ASSERT (r_jmptable == 0);
4092 BFD_ASSERT (r_relative == 0);
4093
4094 if (relocateable)
4095 {
4096 /* We are generating a relocateable output file, and must
4097 modify the reloc accordingly. */
4098 if (r_extern)
4099 {
4100 struct aout_link_hash_entry *h;
4101
4102 /* If we know the symbol this relocation is against,
4103 convert it into a relocation against a section. This
4104 is what the native linker does. */
4105 h = sym_hashes[r_index];
4106 if (h != (struct aout_link_hash_entry *) NULL
4107 && h->root.type == bfd_link_hash_defined)
4108 {
4109 asection *output_section;
4110
4111 /* Change the r_extern value. */
4112 if (output_bfd->xvec->header_byteorder_big_p)
4113 rel->r_type[0] &=~ RELOC_STD_BITS_EXTERN_BIG;
4114 else
4115 rel->r_type[0] &=~ RELOC_STD_BITS_EXTERN_LITTLE;
4116
4117 /* Compute a new r_index. */
4118 output_section = h->root.u.def.section->output_section;
4119 if (output_section == obj_textsec (output_bfd))
4120 r_index = N_TEXT;
4121 else if (output_section == obj_datasec (output_bfd))
4122 r_index = N_DATA;
4123 else if (output_section == obj_bsssec (output_bfd))
4124 r_index = N_BSS;
4125 else
4126 r_index = N_ABS;
4127
4128 /* Add the symbol value and the section VMA to the
4129 addend stored in the contents. */
4130 relocation = (h->root.u.def.value
4131 + output_section->vma
4132 + h->root.u.def.section->output_offset);
4133 }
4134 else
4135 {
4136 /* We must change r_index according to the symbol
4137 map. */
4138 r_index = symbol_map[r_index];
4139
4140 if (r_index == -1)
4141 {
4142 const char *name;
4143
4144 name = strings + GET_WORD (input_bfd,
4145 syms[r_index].e_strx);
4146 if (! ((*finfo->info->callbacks->unattached_reloc)
4147 (finfo->info, name, input_bfd, input_section,
4148 r_addr)))
4149 return false;
4150 r_index = 0;
4151 }
4152
4153 relocation = 0;
4154 }
4155
4156 /* Write out the new r_index value. */
4157 if (output_bfd->xvec->header_byteorder_big_p)
4158 {
4159 rel->r_index[0] = r_index >> 16;
4160 rel->r_index[1] = r_index >> 8;
4161 rel->r_index[2] = r_index;
4162 }
4163 else
4164 {
4165 rel->r_index[2] = r_index >> 16;
4166 rel->r_index[1] = r_index >> 8;
4167 rel->r_index[0] = r_index;
4168 }
4169 }
4170 else
4171 {
4172 asection *section;
4173
4174 /* This is a relocation against a section. We must
4175 adjust by the amount that the section moved. */
4176 section = aout_reloc_index_to_section (input_bfd, r_index);
4177 relocation = (section->output_section->vma
4178 + section->output_offset
4179 - section->vma);
4180 }
4181
4182 /* Change the address of the relocation. */
4183 PUT_WORD (output_bfd,
4184 r_addr + input_section->output_offset,
4185 rel->r_address);
4186
4187 /* Adjust a PC relative relocation by removing the reference
4188 to the original address in the section and including the
4189 reference to the new address. */
4190 if (r_pcrel)
4191 relocation -= (input_section->output_section->vma
4192 + input_section->output_offset
4193 - input_section->vma);
4194
4195 if (relocation == 0)
4196 r = bfd_reloc_ok;
4197 else
4198 r = _bfd_relocate_contents (howto_table_std + howto_idx,
4199 input_bfd, relocation,
4200 contents + r_addr);
4201 }
4202 else
4203 {
4204 /* We are generating an executable, and must do a full
4205 relocation. */
4206 if (r_extern)
4207 {
4208 struct aout_link_hash_entry *h;
4209
4210 h = sym_hashes[r_index];
4211 if (h != (struct aout_link_hash_entry *) NULL
4212 && h->root.type == bfd_link_hash_defined)
4213 {
4214 relocation = (h->root.u.def.value
4215 + h->root.u.def.section->output_section->vma
4216 + h->root.u.def.section->output_offset);
4217 }
4218 else
4219 {
4220 const char *name;
4221
4222 name = strings + GET_WORD (input_bfd, syms[r_index].e_strx);
4223 if (! ((*finfo->info->callbacks->undefined_symbol)
4224 (finfo->info, name, input_bfd, input_section,
4225 r_addr)))
4226 return false;
4227 relocation = 0;
4228 }
4229 }
4230 else
4231 {
4232 asection *section;
4233
4234 section = aout_reloc_index_to_section (input_bfd, r_index);
4235 relocation = (section->output_section->vma
4236 + section->output_offset
4237 - section->vma);
4238 if (r_pcrel)
4239 relocation += input_section->vma;
4240 }
4241
4242 r = _bfd_final_link_relocate (howto_table_std + howto_idx,
4243 input_bfd, input_section,
4244 contents, r_addr, relocation,
4245 (bfd_vma) 0);
4246 }
4247
4248 if (r != bfd_reloc_ok)
4249 {
4250 switch (r)
4251 {
4252 default:
4253 case bfd_reloc_outofrange:
4254 abort ();
4255 case bfd_reloc_overflow:
4256 {
4257 const char *name;
4258
4259 if (r_extern)
4260 name = strings + GET_WORD (input_bfd,
4261 syms[r_index].e_strx);
4262 else
4263 {
4264 asection *s;
4265
4266 s = aout_reloc_index_to_section (input_bfd, r_index);
4267 name = bfd_section_name (input_bfd, s);
4268 }
4269 if (! ((*finfo->info->callbacks->reloc_overflow)
4270 (finfo->info, name, howto_table_std[howto_idx].name,
4271 (bfd_vma) 0, input_bfd, input_section, r_addr)))
4272 return false;
4273 }
4274 break;
4275 }
4276 }
4277 }
4278
4279 return true;
4280 }
4281
4282 /* Relocate an a.out section using extended a.out relocs. */
4283
4284 static boolean
4285 aout_link_input_section_ext (finfo, input_bfd, input_section, relocs,
4286 rel_size, contents, symbol_map)
4287 struct aout_final_link_info *finfo;
4288 bfd *input_bfd;
4289 asection *input_section;
4290 struct reloc_ext_external *relocs;
4291 bfd_size_type rel_size;
4292 bfd_byte *contents;
4293 int *symbol_map;
4294 {
4295 bfd *output_bfd;
4296 boolean relocateable;
4297 struct external_nlist *syms;
4298 char *strings;
4299 struct aout_link_hash_entry **sym_hashes;
4300 bfd_size_type reloc_count;
4301 register struct reloc_ext_external *rel;
4302 struct reloc_ext_external *rel_end;
4303
4304 output_bfd = finfo->output_bfd;
4305
4306 BFD_ASSERT (obj_reloc_entry_size (input_bfd) == RELOC_EXT_SIZE);
4307 BFD_ASSERT (input_bfd->xvec->header_byteorder_big_p
4308 == output_bfd->xvec->header_byteorder_big_p);
4309
4310 relocateable = finfo->info->relocateable;
4311 syms = obj_aout_external_syms (input_bfd);
4312 strings = obj_aout_external_strings (input_bfd);
4313 sym_hashes = obj_aout_sym_hashes (input_bfd);
4314
4315 reloc_count = rel_size / RELOC_EXT_SIZE;
4316 rel = relocs;
4317 rel_end = rel + reloc_count;
4318 for (; rel < rel_end; rel++)
4319 {
4320 bfd_vma r_addr;
4321 int r_index;
4322 int r_extern;
4323 int r_type;
4324 bfd_vma r_addend;
4325 bfd_vma relocation;
4326
4327 r_addr = GET_SWORD (input_bfd, rel->r_address);
4328
4329 if (input_bfd->xvec->header_byteorder_big_p)
4330 {
4331 r_index = ((rel->r_index[0] << 16)
4332 | (rel->r_index[1] << 8)
4333 | rel->r_index[2]);
4334 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG));
4335 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
4336 >> RELOC_EXT_BITS_TYPE_SH_BIG);
4337 }
4338 else
4339 {
4340 r_index = ((rel->r_index[2] << 16)
4341 | (rel->r_index[1] << 8)
4342 | rel->r_index[0]);
4343 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
4344 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
4345 >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
4346 }
4347
4348 r_addend = GET_SWORD (input_bfd, rel->r_addend);
4349
4350 BFD_ASSERT (r_type >= 0
4351 && r_type < TABLE_SIZE (howto_table_ext));
4352
4353 if (relocateable)
4354 {
4355 /* We are generating a relocateable output file, and must
4356 modify the reloc accordingly. */
4357 if (r_extern)
4358 {
4359 struct aout_link_hash_entry *h;
4360
4361 /* If we know the symbol this relocation is against,
4362 convert it into a relocation against a section. This
4363 is what the native linker does. */
4364 h = sym_hashes[r_index];
4365 if (h != (struct aout_link_hash_entry *) NULL
4366 && h->root.type == bfd_link_hash_defined)
4367 {
4368 asection *output_section;
4369
4370 /* Change the r_extern value. */
4371 if (output_bfd->xvec->header_byteorder_big_p)
4372 rel->r_type[0] &=~ RELOC_EXT_BITS_EXTERN_BIG;
4373 else
4374 rel->r_type[0] &=~ RELOC_EXT_BITS_EXTERN_LITTLE;
4375
4376 /* Compute a new r_index. */
4377 output_section = h->root.u.def.section->output_section;
4378 if (output_section == obj_textsec (output_bfd))
4379 r_index = N_TEXT;
4380 else if (output_section == obj_datasec (output_bfd))
4381 r_index = N_DATA;
4382 else if (output_section == obj_bsssec (output_bfd))
4383 r_index = N_BSS;
4384 else
4385 r_index = N_ABS;
4386
4387 /* Add the symbol value and the section VMA to the
4388 addend. */
4389 relocation = (h->root.u.def.value
4390 + output_section->vma
4391 + h->root.u.def.section->output_offset);
4392
4393 /* Now RELOCATION is the VMA of the final
4394 destination. If this is a PC relative reloc,
4395 then ADDEND is the negative of the source VMA.
4396 We want to set ADDEND to the difference between
4397 the destination VMA and the source VMA, which
4398 means we must adjust RELOCATION by the change in
4399 the source VMA. This is done below. */
4400 }
4401 else
4402 {
4403 /* We must change r_index according to the symbol
4404 map. */
4405 r_index = symbol_map[r_index];
4406
4407 if (r_index == -1)
4408 {
4409 const char *name;
4410
4411 name = (strings
4412 + GET_WORD (input_bfd, syms[r_index].e_strx));
4413 if (! ((*finfo->info->callbacks->unattached_reloc)
4414 (finfo->info, name, input_bfd, input_section,
4415 r_addr)))
4416 return false;
4417 r_index = 0;
4418 }
4419
4420 relocation = 0;
4421
4422 /* If this is a PC relative reloc, then the addend
4423 is the negative of the source VMA. We must
4424 adjust it by the change in the source VMA. This
4425 is done below. */
4426 }
4427
4428 /* Write out the new r_index value. */
4429 if (output_bfd->xvec->header_byteorder_big_p)
4430 {
4431 rel->r_index[0] = r_index >> 16;
4432 rel->r_index[1] = r_index >> 8;
4433 rel->r_index[2] = r_index;
4434 }
4435 else
4436 {
4437 rel->r_index[2] = r_index >> 16;
4438 rel->r_index[1] = r_index >> 8;
4439 rel->r_index[0] = r_index;
4440 }
4441 }
4442 else
4443 {
4444 asection *section;
4445
4446 /* This is a relocation against a section. We must
4447 adjust by the amount that the section moved. */
4448 section = aout_reloc_index_to_section (input_bfd, r_index);
4449 relocation = (section->output_section->vma
4450 + section->output_offset
4451 - section->vma);
4452
4453 /* If this is a PC relative reloc, then the addend is
4454 the difference in VMA between the destination and the
4455 source. We have just adjusted for the change in VMA
4456 of the destination, so we must also adjust by the
4457 change in VMA of the source. This is done below. */
4458 }
4459
4460 /* As described above, we must always adjust a PC relative
4461 reloc by the change in VMA of the source. */
4462 if (howto_table_ext[r_type].pc_relative)
4463 relocation -= (input_section->output_section->vma
4464 + input_section->output_offset
4465 - input_section->vma);
4466
4467 /* Change the addend if necessary. */
4468 if (relocation != 0)
4469 PUT_WORD (output_bfd, r_addend + relocation, rel->r_addend);
4470
4471 /* Change the address of the relocation. */
4472 PUT_WORD (output_bfd,
4473 r_addr + input_section->output_offset,
4474 rel->r_address);
4475 }
4476 else
4477 {
4478 bfd_reloc_status_type r;
4479
4480 /* We are generating an executable, and must do a full
4481 relocation. */
4482 if (r_extern)
4483 {
4484 struct aout_link_hash_entry *h;
4485
4486 h = sym_hashes[r_index];
4487 if (h != (struct aout_link_hash_entry *) NULL
4488 && h->root.type == bfd_link_hash_defined)
4489 {
4490 relocation = (h->root.u.def.value
4491 + h->root.u.def.section->output_section->vma
4492 + h->root.u.def.section->output_offset);
4493 }
4494 else
4495 {
4496 const char *name;
4497
4498 name = strings + GET_WORD (input_bfd, syms[r_index].e_strx);
4499 if (! ((*finfo->info->callbacks->undefined_symbol)
4500 (finfo->info, name, input_bfd, input_section,
4501 r_addr)))
4502 return false;
4503 relocation = 0;
4504 }
4505 }
4506 else
4507 {
4508 asection *section;
4509
4510 section = aout_reloc_index_to_section (input_bfd, r_index);
4511
4512 /* If this is a PC relative reloc, then R_ADDEND is the
4513 difference between the two vmas, or
4514 old_dest_sec + old_dest_off - (old_src_sec + old_src_off)
4515 where
4516 old_dest_sec == section->vma
4517 and
4518 old_src_sec == input_section->vma
4519 and
4520 old_src_off == r_addr
4521
4522 _bfd_final_link_relocate expects RELOCATION +
4523 R_ADDEND to be the VMA of the destination minus
4524 r_addr (the minus r_addr is because this relocation
4525 is not pcrel_offset, which is a bit confusing and
4526 should, perhaps, be changed), or
4527 new_dest_sec
4528 where
4529 new_dest_sec == output_section->vma + output_offset
4530 We arrange for this to happen by setting RELOCATION to
4531 new_dest_sec + old_src_sec - old_dest_sec
4532
4533 If this is not a PC relative reloc, then R_ADDEND is
4534 simply the VMA of the destination, so we set
4535 RELOCATION to the change in the destination VMA, or
4536 new_dest_sec - old_dest_sec
4537 */
4538 relocation = (section->output_section->vma
4539 + section->output_offset
4540 - section->vma);
4541 if (howto_table_ext[r_type].pc_relative)
4542 relocation += input_section->vma;
4543 }
4544
4545 r = _bfd_final_link_relocate (howto_table_ext + r_type,
4546 input_bfd, input_section,
4547 contents, r_addr, relocation,
4548 r_addend);
4549 if (r != bfd_reloc_ok)
4550 {
4551 switch (r)
4552 {
4553 default:
4554 case bfd_reloc_outofrange:
4555 abort ();
4556 case bfd_reloc_overflow:
4557 {
4558 const char *name;
4559
4560 if (r_extern)
4561 name = strings + GET_WORD (input_bfd,
4562 syms[r_index].e_strx);
4563 else
4564 {
4565 asection *s;
4566
4567 s = aout_reloc_index_to_section (input_bfd, r_index);
4568 name = bfd_section_name (input_bfd, s);
4569 }
4570 if (! ((*finfo->info->callbacks->reloc_overflow)
4571 (finfo->info, name, howto_table_ext[r_type].name,
4572 r_addend, input_bfd, input_section, r_addr)))
4573 return false;
4574 }
4575 break;
4576 }
4577 }
4578 }
4579 }
4580
4581 return true;
4582 }
This page took 0.137862 seconds and 4 git commands to generate.