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