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