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