Fix RELOC_FOR_GLOBAL_SYMBOLS macro so that it can cope with user defined symbols...
[binutils-gdb.git] / bfd / pdp11.c
blobef86400e842f507bc8addfb64859474b5ff17b72
1 /* BFD back-end for PDP-11 a.out binaries.
2 Copyright (C) 2001-2024 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
22 /* BFD backend for PDP-11, running 2.11BSD in particular.
24 This file was hacked up by looking hard at the existing vaxnetbsd
25 back end and the header files in 2.11BSD. The symbol table format
26 of 2.11BSD has been extended to accommodate .stab symbols. See
27 struct pdp11_external_nlist below for details.
29 TODO
30 * support for V7 file formats
31 * support for overlay object files (see 2.11 a.out(5))
32 * support for old and very old archives
33 (see 2.11 ar(5), historical section)
35 Search for TODO to find other areas needing more work. */
37 #define BYTES_IN_WORD 2
38 #define BYTES_IN_LONG 4
39 #define ARCH_SIZE 16
40 #undef TARGET_IS_BIG_ENDIAN_P
42 #define TARGET_PAGE_SIZE 8192
43 #define SEGMENT__SIZE TARGET_PAGE_SIZE
45 #define DEFAULT_ARCH bfd_arch_pdp11
46 #define DEFAULT_MID M_PDP11
48 /* Do not "beautify" the CONCAT* macro args. Traditional C will not
49 remove whitespace added here, and thus will fail to concatenate
50 the tokens. */
51 #define MY(OP) CONCAT2 (pdp11_aout_,OP)
53 /* This needs to start with a.out so GDB knows it is an a.out variant. */
54 #define TARGETNAME "a.out-pdp11"
56 /* This is the normal load address for executables. */
57 #define TEXT_START_ADDR 0
59 /* The header is not included in the text segment. */
60 #define N_HEADER_IN_TEXT(x) 0
62 /* There is no flags field. */
63 #define N_FLAGS(execp) 0
65 #define N_SET_FLAGS(execp, flags) do { } while (0)
66 #define N_BADMAG(x) (N_MAGIC(x) != OMAGIC \
67 && N_MAGIC(x) != NMAGIC \
68 && N_MAGIC(x) != IMAGIC \
69 && N_MAGIC(x) != ZMAGIC)
71 #include "sysdep.h"
72 #include <limits.h>
73 #include "bfd.h"
75 #define external_exec pdp11_external_exec
76 struct pdp11_external_exec
78 bfd_byte e_info[2]; /* Magic number. */
79 bfd_byte e_text[2]; /* Length of text section in bytes. */
80 bfd_byte e_data[2]; /* Length of data section in bytes. */
81 bfd_byte e_bss[2]; /* Length of bss area in bytes. */
82 bfd_byte e_syms[2]; /* Length of symbol table in bytes. */
83 bfd_byte e_entry[2]; /* Start address. */
84 bfd_byte e_unused[2]; /* Not used. */
85 bfd_byte e_flag[2]; /* Relocation info stripped. */
86 bfd_byte e_relocatable; /* Ugly hack. */
89 #define EXEC_BYTES_SIZE (8 * 2)
91 #define A_MAGIC1 OMAGIC
92 #define OMAGIC 0407 /* ...object file or impure executable. */
93 #define A_MAGIC2 NMAGIC
94 #define NMAGIC 0410 /* Pure executable. */
95 #define ZMAGIC 0413 /* Demand-paged executable. */
96 #define IMAGIC 0411 /* Separated I&D. */
97 #define A_MAGIC3 IMAGIC
98 #define A_MAGIC4 0405 /* Overlay. */
99 #define A_MAGIC5 0430 /* Auto-overlay (nonseparate). */
100 #define A_MAGIC6 0431 /* Auto-overlay (separate). */
101 #define QMAGIC 0
102 #define BMAGIC 0
104 #define A_FLAG_RELOC_STRIPPED 0x0001
106 /* The following struct defines the format of an entry in the object file
107 symbol table. In the original 2.11BSD struct the index into the string
108 table is stored as a long, but the PDP11 C convention for storing a long in
109 memory placed the most significant word first even though the bytes within a
110 word are stored least significant first. So here the string table index is
111 considered to be just 16 bits and the first two bytes of the struct were
112 previously named e_unused. To extend the symbol table format to accommodate
113 .stab symbols, the e_unused bytes are renamed e_desc to store the desc field
114 of the .stab symbol. The GDP Project's STABS document says that the "other"
115 field is almost always unused and can be set to zero; the only nonzero cases
116 identified were for stabs in their own sections, which does not apply for
117 pdp11 a.out format, and for a special case of GNU Modula2 which is not
118 supported for the PDP11. */
119 #define external_nlist pdp11_external_nlist
120 struct pdp11_external_nlist
122 bfd_byte e_desc[2]; /* The desc field for .stab symbols, else 0. */
123 bfd_byte e_strx[2]; /* Index into string table of name. */
124 bfd_byte e_type[1]; /* Type of symbol. */
125 bfd_byte e_ovly[1]; /* Overlay number. */
126 bfd_byte e_value[2]; /* Value of symbol. */
129 #define EXTERNAL_NLIST_SIZE 8
131 #define N_TXTOFF(x) (EXEC_BYTES_SIZE)
132 #define N_DATOFF(x) (N_TXTOFF(x) + (x)->a_text)
133 #define N_TRELOFF(x) (N_DATOFF(x) + (x)->a_data)
134 #define N_DRELOFF(x) (N_TRELOFF(x) + (x)->a_trsize)
135 #define N_SYMOFF(x) (N_DRELOFF(x) + (x)->a_drsize)
136 #define N_STROFF(x) (N_SYMOFF(x) + (x)->a_syms)
138 #define WRITE_HEADERS(abfd, execp) pdp11_aout_write_headers (abfd, execp)
140 #include "libbfd.h"
141 #include "libaout.h"
143 #define SWAP_MAGIC(ext) bfd_getl16 (ext)
145 #define MY_entry_is_text_address 1
147 #define MY_write_object_contents MY(write_object_contents)
148 static bool MY(write_object_contents) (bfd *);
149 #define MY_text_includes_header 1
151 #define MY_BFD_TARGET
153 #include "aout-target.h"
155 /* Start of modified aoutx.h. */
156 #define KEEPIT udata.i
158 #include <string.h> /* For strchr and friends. */
159 #include "bfd.h"
160 #include "sysdep.h"
161 #include "safe-ctype.h"
162 #include "bfdlink.h"
164 #include "libaout.h"
165 #include "aout/aout64.h"
166 #include "aout/stab_gnu.h"
167 #include "aout/ar.h"
169 /* The symbol type numbers for the 16-bit a.out format from 2.11BSD differ from
170 those defined in aout64.h so we must redefine them here. N_EXT changes from
171 0x01 to 0x20 which creates a conflict with some .stab values, in particular
172 between undefined externals (N_UNDF+N_EXT) vs. global variables (N_GYSM) and
173 between external bss symbols (N_BSS+N_EXT) vs. function names (N_FUN). We
174 disambiguate those conflicts with a hack in is_stab() to look for the ':' in
175 the global variable or function name string. */
176 #undef N_TYPE
177 #undef N_UNDF
178 #undef N_ABS
179 #undef N_TEXT
180 #undef N_DATA
181 #undef N_BSS
182 #undef N_REG
183 #undef N_FN
184 #undef N_EXT
185 #undef N_STAB
186 #define N_TYPE 0x1f /* Type mask. */
187 #define N_UNDF 0x00 /* Undefined. */
188 #define N_ABS 0x01 /* Absolute. */
189 #define N_TEXT 0x02 /* Text segment. */
190 #define N_DATA 0x03 /* Data segment. */
191 #define N_BSS 0x04 /* Bss segment. */
192 #define N_REG 0x14 /* Register symbol. */
193 #define N_FN 0x1f /* File name. */
194 #define N_EXT 0x20 /* External flag. */
195 /* Type numbers from .stab entries that could conflict:
196 N_GSYM 0x20 Global variable [conflict with external undef]
197 N_FNAME 0x22 Function name (for BSD Fortran) [ignored]
198 N_FUN 0x24 Function name [conflict with external BSS]
199 N_NOMAP 0x34 No DST map for sym. [ext. reg. doesn't exist]
202 #define RELOC_SIZE 2
204 #define RELFLG 0x0001 /* PC-relative flag. */
205 #define RTYPE 0x000e /* Type mask. */
206 #define RIDXMASK 0xfff0 /* Index mask. */
208 #define RABS 0x00 /* Absolute. */
209 #define RTEXT 0x02 /* Text. */
210 #define RDATA 0x04 /* Data. */
211 #define RBSS 0x06 /* Bss. */
212 #define REXT 0x08 /* External. */
214 #define RINDEX(x) (((x) & 0xfff0) >> 4)
216 #ifndef MY_final_link_relocate
217 #define MY_final_link_relocate _bfd_final_link_relocate
218 #endif
220 #ifndef MY_relocate_contents
221 #define MY_relocate_contents _bfd_relocate_contents
222 #endif
224 /* A hash table used for header files with N_BINCL entries. */
226 struct aout_link_includes_table
228 struct bfd_hash_table root;
231 /* A linked list of totals that we have found for a particular header
232 file. */
234 struct aout_link_includes_totals
236 struct aout_link_includes_totals *next;
237 bfd_vma total;
240 /* An entry in the header file hash table. */
242 struct aout_link_includes_entry
244 struct bfd_hash_entry root;
245 /* List of totals we have found for this file. */
246 struct aout_link_includes_totals *totals;
249 /* During the final link step we need to pass around a bunch of
250 information, so we do it in an instance of this structure. */
252 struct aout_final_link_info
254 /* General link information. */
255 struct bfd_link_info *info;
256 /* Output bfd. */
257 bfd *output_bfd;
258 /* Reloc file positions. */
259 file_ptr treloff, dreloff;
260 /* File position of symbols. */
261 file_ptr symoff;
262 /* String table. */
263 struct bfd_strtab_hash *strtab;
264 /* Header file hash table. */
265 struct aout_link_includes_table includes;
266 /* A buffer large enough to hold the contents of any section. */
267 bfd_byte *contents;
268 /* A buffer large enough to hold the relocs of any section. */
269 void * relocs;
270 /* A buffer large enough to hold the symbol map of any input BFD. */
271 int *symbol_map;
272 /* A buffer large enough to hold output symbols of any input BFD. */
273 struct external_nlist *output_syms;
276 /* Copy of the link_info.separate_code boolean to select the output format with
277 separate instruction and data spaces selected by --imagic */
278 static bool separate_i_d = false;
280 reloc_howto_type howto_table_pdp11[] =
282 /* type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone */
283 HOWTO( 0, 0, 2, 16, false, 0, complain_overflow_dont,0,"16", true, 0x0000ffff,0x0000ffff, false),
284 HOWTO( 1, 0, 2, 16, true, 0, complain_overflow_dont,0,"DISP16", true, 0x0000ffff,0x0000ffff, false),
285 HOWTO( 2, 0, 4, 32, false, 0, complain_overflow_dont,0,"32", true, 0x0000ffff,0x0000ffff, false),
288 #define TABLE_SIZE(TABLE) (sizeof(TABLE)/sizeof(TABLE[0]))
291 static bool aout_link_check_archive_element (bfd *, struct bfd_link_info *,
292 struct bfd_link_hash_entry *,
293 const char *, bool *);
294 static bool aout_link_add_object_symbols (bfd *, struct bfd_link_info *);
295 static bool aout_link_add_symbols (bfd *, struct bfd_link_info *);
296 static bool aout_link_write_symbols (struct aout_final_link_info *, bfd *);
299 reloc_howto_type *
300 NAME (aout, reloc_type_lookup) (bfd * abfd ATTRIBUTE_UNUSED,
301 bfd_reloc_code_real_type code)
303 switch (code)
305 case BFD_RELOC_16:
306 return &howto_table_pdp11[0];
307 case BFD_RELOC_16_PCREL:
308 return &howto_table_pdp11[1];
309 case BFD_RELOC_32:
310 return &howto_table_pdp11[2];
311 default:
312 return NULL;
316 reloc_howto_type *
317 NAME (aout, reloc_name_lookup) (bfd *abfd ATTRIBUTE_UNUSED,
318 const char *r_name)
320 unsigned int i;
322 for (i = 0;
323 i < sizeof (howto_table_pdp11) / sizeof (howto_table_pdp11[0]);
324 i++)
325 if (howto_table_pdp11[i].name != NULL
326 && strcasecmp (howto_table_pdp11[i].name, r_name) == 0)
327 return &howto_table_pdp11[i];
329 return NULL;
332 /* Disambiguate conflicts between normal symbol types and .stab symbol types
333 (undefined externals N_UNDF+N_EXT vs. global variables N_GYSM and external
334 bss symbols N_BSS+N_EXT vs. function names N_FUN) with a hack to look for
335 the ':' in the global variable or function name string. */
337 static int
338 is_stab (int type, const char *name)
340 if (type == N_GSYM || type == N_FUN)
341 return strchr (name, ':') != NULL;
342 return type > N_FUN;
345 static int
346 pdp11_aout_write_headers (bfd *abfd, struct internal_exec *execp)
348 struct external_exec exec_bytes;
350 if (adata(abfd).magic == undecided_magic)
351 NAME (aout, adjust_sizes_and_vmas) (abfd);
353 execp->a_syms = bfd_get_symcount (abfd) * EXTERNAL_NLIST_SIZE;
354 execp->a_entry = bfd_get_start_address (abfd);
356 if (obj_textsec (abfd)->reloc_count > 0
357 || obj_datasec (abfd)->reloc_count > 0)
359 execp->a_trsize = execp->a_text;
360 execp->a_drsize = execp->a_data;
362 else
364 execp->a_trsize = 0;
365 execp->a_drsize = 0;
368 if (!NAME (aout, swap_exec_header_out) (abfd, execp, & exec_bytes))
369 return false;
371 if (bfd_seek (abfd, 0, SEEK_SET) != 0)
372 return false;
374 if (bfd_write (&exec_bytes, EXEC_BYTES_SIZE, abfd) != EXEC_BYTES_SIZE)
375 return false;
377 /* Now write out reloc info, followed by syms and strings. */
378 if (bfd_get_outsymbols (abfd) != NULL
379 && bfd_get_symcount (abfd) != 0)
381 if (bfd_seek (abfd, N_SYMOFF (execp), SEEK_SET) != 0)
382 return false;
384 if (! NAME (aout, write_syms) (abfd))
385 return false;
388 if (obj_textsec (abfd)->reloc_count > 0
389 || obj_datasec (abfd)->reloc_count > 0)
391 if (bfd_seek (abfd, N_TRELOFF (execp), SEEK_SET) != 0
392 || !NAME (aout, squirt_out_relocs) (abfd, obj_textsec (abfd))
393 || bfd_seek (abfd, N_DRELOFF (execp), SEEK_SET) != 0
394 || !NAME (aout, squirt_out_relocs) (abfd, obj_datasec (abfd)))
395 return false;
398 return true;
401 /* Write an object file.
402 Section contents have already been written. We write the
403 file header, symbols, and relocation. */
405 static bool
406 MY(write_object_contents) (bfd *abfd)
408 struct internal_exec *execp = exec_hdr (abfd);
410 /* We must make certain that the magic number has been set. This
411 will normally have been done by set_section_contents, but only if
412 there actually are some section contents. */
413 if (! abfd->output_has_begun)
414 NAME (aout, adjust_sizes_and_vmas) (abfd);
416 obj_reloc_entry_size (abfd) = RELOC_SIZE;
418 return WRITE_HEADERS (abfd, execp);
421 /* Swap the information in an executable header @var{raw_bytes} taken
422 from a raw byte stream memory image into the internal exec header
423 structure "execp". */
425 #ifndef NAME_swap_exec_header_in
426 void
427 NAME (aout, swap_exec_header_in) (bfd *abfd,
428 struct external_exec *bytes,
429 struct internal_exec *execp)
431 /* The internal_exec structure has some fields that are unused in this
432 configuration (IE for i960), so ensure that all such uninitialized
433 fields are zero'd out. There are places where two of these structs
434 are memcmp'd, and thus the contents do matter. */
435 memset ((void *) execp, 0, sizeof (struct internal_exec));
436 /* Now fill in fields in the execp, from the bytes in the raw data. */
437 execp->a_info = GET_MAGIC (abfd, bytes->e_info);
438 execp->a_text = GET_WORD (abfd, bytes->e_text);
439 execp->a_data = GET_WORD (abfd, bytes->e_data);
440 execp->a_bss = GET_WORD (abfd, bytes->e_bss);
441 execp->a_syms = GET_WORD (abfd, bytes->e_syms);
442 execp->a_entry = GET_WORD (abfd, bytes->e_entry);
444 if (GET_WORD (abfd, bytes->e_flag) & A_FLAG_RELOC_STRIPPED)
446 execp->a_trsize = 0;
447 execp->a_drsize = 0;
449 else
451 execp->a_trsize = execp->a_text;
452 execp->a_drsize = execp->a_data;
455 #define NAME_swap_exec_header_in NAME (aout, swap_exec_header_in)
456 #endif
458 /* Swap the information in an internal exec header structure
459 "execp" into the buffer "bytes" ready for writing to disk. */
460 bool
461 NAME (aout, swap_exec_header_out) (bfd *abfd,
462 struct internal_exec *execp,
463 struct external_exec *bytes)
465 const char *err = NULL;
466 uint64_t val;
467 #define MAXVAL(x) ((UINT64_C (1) << (8 * sizeof (x) - 1) << 1) - 1)
468 if ((val = execp->a_text) > MAXVAL (bytes->e_text))
469 err = "e_text";
470 else if ((val = execp->a_data) > MAXVAL (bytes->e_data))
471 err = "e_data";
472 else if ((val = execp->a_bss) > MAXVAL (bytes->e_bss))
473 err = "e_bss";
474 else if ((val = execp->a_syms) > MAXVAL (bytes->e_syms))
475 err = "e_syms";
476 else if ((val = execp->a_entry) > MAXVAL (bytes->e_entry))
477 err = "e_entry";
478 #undef MAXVAL
479 if (err)
481 _bfd_error_handler (_("%pB: %#" PRIx64 " overflows header %s field"),
482 abfd, val, err);
483 bfd_set_error (bfd_error_file_too_big);
484 return false;
487 /* Now fill in fields in the raw data, from the fields in the exec struct. */
488 PUT_MAGIC (abfd, execp->a_info, bytes->e_info);
489 PUT_WORD (abfd, execp->a_text, bytes->e_text);
490 PUT_WORD (abfd, execp->a_data, bytes->e_data);
491 PUT_WORD (abfd, execp->a_bss, bytes->e_bss);
492 PUT_WORD (abfd, execp->a_syms, bytes->e_syms);
493 PUT_WORD (abfd, execp->a_entry, bytes->e_entry);
494 PUT_WORD (abfd, 0, bytes->e_unused);
496 if ((execp->a_trsize == 0 || execp->a_text == 0)
497 && (execp->a_drsize == 0 || execp->a_data == 0))
498 PUT_WORD (abfd, A_FLAG_RELOC_STRIPPED, bytes->e_flag);
499 else if (execp->a_trsize == execp->a_text
500 && execp->a_drsize == execp->a_data)
501 PUT_WORD (abfd, 0, bytes->e_flag);
502 else
504 /* TODO: print a proper warning message. */
505 fprintf (stderr, "BFD:%s:%d: internal error\n", __FILE__, __LINE__);
506 PUT_WORD (abfd, 0, bytes->e_flag);
508 return true;
511 /* Make all the section for an a.out file. */
513 bool
514 NAME (aout, make_sections) (bfd *abfd)
516 if (obj_textsec (abfd) == NULL && bfd_make_section (abfd, ".text") == NULL)
517 return false;
518 if (obj_datasec (abfd) == NULL && bfd_make_section (abfd, ".data") == NULL)
519 return false;
520 if (obj_bsssec (abfd) == NULL && bfd_make_section (abfd, ".bss") == NULL)
521 return false;
522 return true;
525 /* Some a.out variant thinks that the file open in ABFD
526 checking is an a.out file. Do some more checking, and set up
527 for access if it really is. Call back to the calling
528 environment's "finish up" function just before returning, to
529 handle any last-minute setup. */
531 bfd_cleanup
532 NAME (aout, some_aout_object_p) (bfd *abfd,
533 struct internal_exec *execp,
534 bfd_cleanup (*callback_to_real_object_p) (bfd *))
536 struct aout_data_struct *rawptr, *oldrawptr;
537 bfd_cleanup result;
538 size_t amt = sizeof (*rawptr);
540 rawptr = bfd_zalloc (abfd, amt);
541 if (rawptr == NULL)
542 return NULL;
544 oldrawptr = abfd->tdata.aout_data;
545 abfd->tdata.aout_data = rawptr;
547 /* Copy the contents of the old tdata struct. */
548 if (oldrawptr != NULL)
549 *abfd->tdata.aout_data = *oldrawptr;
551 abfd->tdata.aout_data->a.hdr = &rawptr->e;
552 /* Copy in the internal_exec struct. */
553 *(abfd->tdata.aout_data->a.hdr) = *execp;
554 execp = abfd->tdata.aout_data->a.hdr;
556 /* Set the file flags. */
557 abfd->flags = BFD_NO_FLAGS;
558 if (execp->a_drsize || execp->a_trsize)
559 abfd->flags |= HAS_RELOC;
560 /* Setting of EXEC_P has been deferred to the bottom of this function. */
561 if (execp->a_syms)
562 abfd->flags |= HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS;
563 if (N_DYNAMIC (execp))
564 abfd->flags |= DYNAMIC;
566 if (N_MAGIC (execp) == ZMAGIC)
568 abfd->flags |= D_PAGED | WP_TEXT;
569 adata (abfd).magic = z_magic;
571 else if (N_MAGIC (execp) == NMAGIC)
573 abfd->flags |= WP_TEXT;
574 adata (abfd).magic = n_magic;
576 else if (N_MAGIC (execp) == OMAGIC)
577 adata (abfd).magic = o_magic;
578 else if (N_MAGIC (execp) == IMAGIC)
579 adata (abfd).magic = i_magic;
580 else
582 /* Should have been checked with N_BADMAG before this routine
583 was called. */
584 abort ();
587 abfd->start_address = execp->a_entry;
589 abfd->symcount = execp->a_syms / sizeof (struct external_nlist);
591 /* The default relocation entry size is that of traditional V7 Unix. */
592 obj_reloc_entry_size (abfd) = RELOC_SIZE;
594 /* The default symbol entry size is that of traditional Unix. */
595 obj_symbol_entry_size (abfd) = EXTERNAL_NLIST_SIZE;
597 if (! NAME (aout, make_sections) (abfd))
598 goto error_ret;
600 obj_datasec (abfd)->size = execp->a_data;
601 obj_bsssec (abfd)->size = execp->a_bss;
603 obj_textsec (abfd)->flags =
604 (execp->a_trsize != 0
605 ? (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_RELOC)
606 : (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS));
607 obj_datasec (abfd)->flags =
608 (execp->a_drsize != 0
609 ? (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS | SEC_RELOC)
610 : (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS));
611 obj_bsssec (abfd)->flags = SEC_ALLOC;
613 #ifdef THIS_IS_ONLY_DOCUMENTATION
614 /* The common code can't fill in these things because they depend
615 on either the start address of the text segment, the rounding
616 up of virtual addresses between segments, or the starting file
617 position of the text segment -- all of which varies among different
618 versions of a.out. */
620 /* Call back to the format-dependent code to fill in the rest of the
621 fields and do any further cleanup. Things that should be filled
622 in by the callback: */
623 struct exec *execp = exec_hdr (abfd);
625 obj_textsec (abfd)->size = N_TXTSIZE (execp);
626 /* Data and bss are already filled in since they're so standard. */
628 /* The virtual memory addresses of the sections. */
629 obj_textsec (abfd)->vma = N_TXTADDR (execp);
630 obj_datasec (abfd)->vma = N_DATADDR (execp);
631 obj_bsssec (abfd)->vma = N_BSSADDR (execp);
633 /* The file offsets of the sections. */
634 obj_textsec (abfd)->filepos = N_TXTOFF (execp);
635 obj_datasec (abfd)->filepos = N_DATOFF (execp);
637 /* The file offsets of the relocation info. */
638 obj_textsec (abfd)->rel_filepos = N_TRELOFF (execp);
639 obj_datasec (abfd)->rel_filepos = N_DRELOFF (execp);
641 /* The file offsets of the string table and symbol table. */
642 obj_str_filepos (abfd) = N_STROFF (execp);
643 obj_sym_filepos (abfd) = N_SYMOFF (execp);
645 /* Determine the architecture and machine type of the object file. */
646 abfd->obj_arch = bfd_arch_obscure;
648 adata (abfd)->page_size = TARGET_PAGE_SIZE;
649 adata (abfd)->segment_size = SEGMENT_SIZE;
650 adata (abfd)->exec_bytes_size = EXEC_BYTES_SIZE;
652 return _bfd_no_cleanup;
654 /* The architecture is encoded in various ways in various a.out variants,
655 or is not encoded at all in some of them. The relocation size depends
656 on the architecture and the a.out variant. Finally, the return value
657 is the bfd_target vector in use. If an error occurs, return zero and
658 set bfd_error to the appropriate error code.
660 Formats such as b.out, which have additional fields in the a.out
661 header, should cope with them in this callback as well. */
662 #endif /* DOCUMENTATION */
664 result = (*callback_to_real_object_p) (abfd);
666 /* Now that the segment addresses have been worked out, take a better
667 guess at whether the file is executable. If the entry point
668 is within the text segment, assume it is. (This makes files
669 executable even if their entry point address is 0, as long as
670 their text starts at zero.).
672 This test had to be changed to deal with systems where the text segment
673 runs at a different location than the default. The problem is that the
674 entry address can appear to be outside the text segment, thus causing an
675 erroneous conclusion that the file isn't executable.
677 To fix this, we now accept any non-zero entry point as an indication of
678 executability. This will work most of the time, since only the linker
679 sets the entry point, and that is likely to be non-zero for most systems. */
681 if (execp->a_entry != 0
682 || (execp->a_entry >= obj_textsec (abfd)->vma
683 && execp->a_entry < (obj_textsec (abfd)->vma
684 + obj_textsec (abfd)->size)
685 && execp->a_trsize == 0
686 && execp->a_drsize == 0))
687 abfd->flags |= EXEC_P;
688 #ifdef STAT_FOR_EXEC
689 else
691 struct stat stat_buf;
693 /* The original heuristic doesn't work in some important cases.
694 The a.out file has no information about the text start
695 address. For files (like kernels) linked to non-standard
696 addresses (ld -Ttext nnn) the entry point may not be between
697 the default text start (obj_textsec(abfd)->vma) and
698 (obj_textsec(abfd)->vma) + text size. This is not just a mach
699 issue. Many kernels are loaded at non standard addresses. */
700 if (abfd->iostream != NULL
701 && (abfd->flags & BFD_IN_MEMORY) == 0
702 && (fstat (fileno ((FILE *) (abfd->iostream)), &stat_buf) == 0)
703 && ((stat_buf.st_mode & 0111) != 0))
704 abfd->flags |= EXEC_P;
706 #endif /* STAT_FOR_EXEC */
708 if (result)
709 return result;
711 error_ret:
712 bfd_release (abfd, rawptr);
713 abfd->tdata.aout_data = oldrawptr;
714 return NULL;
717 /* Initialize ABFD for use with a.out files. */
719 bool
720 NAME (aout, mkobject) (bfd *abfd)
722 struct aout_data_struct *rawptr;
723 size_t amt = sizeof (struct aout_data_struct);
725 bfd_set_error (bfd_error_system_call);
727 /* Use an intermediate variable for clarity. */
728 rawptr = bfd_zalloc (abfd, amt);
730 if (rawptr == NULL)
731 return false;
733 abfd->tdata.aout_data = rawptr;
734 exec_hdr (abfd) = &(rawptr->e);
736 obj_textsec (abfd) = NULL;
737 obj_datasec (abfd) = NULL;
738 obj_bsssec (abfd) = NULL;
740 return true;
743 /* Keep track of machine architecture and machine type for
744 a.out's. Return the <<machine_type>> for a particular
745 architecture and machine, or <<M_UNKNOWN>> if that exact architecture
746 and machine can't be represented in a.out format.
748 If the architecture is understood, machine type 0 (default)
749 is always understood. */
751 enum machine_type
752 NAME (aout, machine_type) (enum bfd_architecture arch,
753 unsigned long machine,
754 bool *unknown)
756 enum machine_type arch_flags;
758 arch_flags = M_UNKNOWN;
759 *unknown = true;
761 switch (arch)
763 case bfd_arch_sparc:
764 if (machine == 0
765 || machine == bfd_mach_sparc
766 || machine == bfd_mach_sparc_sparclite
767 || machine == bfd_mach_sparc_v9)
768 arch_flags = M_SPARC;
769 else if (machine == bfd_mach_sparc_sparclet)
770 arch_flags = M_SPARCLET;
771 break;
773 case bfd_arch_i386:
774 if (machine == 0
775 || machine == bfd_mach_i386_i386
776 || machine == bfd_mach_i386_i386_intel_syntax)
777 arch_flags = M_386;
778 break;
780 case bfd_arch_arm:
781 if (machine == 0) arch_flags = M_ARM;
782 break;
784 case bfd_arch_mips:
785 switch (machine)
787 case 0:
788 case 2000:
789 case bfd_mach_mips3000:
790 arch_flags = M_MIPS1;
791 break;
792 case bfd_mach_mips4000: /* MIPS3 */
793 case bfd_mach_mips4400:
794 case bfd_mach_mips8000: /* MIPS4 */
795 case bfd_mach_mips6000: /* Real MIPS2: */
796 arch_flags = M_MIPS2;
797 break;
798 default:
799 arch_flags = M_UNKNOWN;
800 break;
802 break;
804 case bfd_arch_ns32k:
805 switch (machine)
807 case 0: arch_flags = M_NS32532; break;
808 case 32032: arch_flags = M_NS32032; break;
809 case 32532: arch_flags = M_NS32532; break;
810 default: arch_flags = M_UNKNOWN; break;
812 break;
814 case bfd_arch_pdp11:
815 /* TODO: arch_flags = M_PDP11; */
816 *unknown = false;
817 break;
819 case bfd_arch_vax:
820 *unknown = false;
821 break;
823 default:
824 arch_flags = M_UNKNOWN;
827 if (arch_flags != M_UNKNOWN)
828 *unknown = false;
830 return arch_flags;
833 /* Set the architecture and the machine of the ABFD to the
834 values ARCH and MACHINE. Verify that @ABFD's format
835 can support the architecture required. */
837 bool
838 NAME (aout, set_arch_mach) (bfd *abfd,
839 enum bfd_architecture arch,
840 unsigned long machine)
842 if (! bfd_default_set_arch_mach (abfd, arch, machine))
843 return false;
845 if (arch != bfd_arch_unknown)
847 bool unknown;
849 NAME (aout, machine_type) (arch, machine, &unknown);
850 if (unknown)
851 return false;
854 obj_reloc_entry_size (abfd) = RELOC_SIZE;
856 return (*aout_backend_info(abfd)->set_sizes) (abfd);
859 static void
860 adjust_o_magic (bfd *abfd, struct internal_exec *execp)
862 file_ptr pos = adata (abfd).exec_bytes_size;
863 bfd_vma vma = 0;
864 int pad = 0;
865 asection *text = obj_textsec (abfd);
866 asection *data = obj_datasec (abfd);
867 asection *bss = obj_bsssec (abfd);
869 /* Text. */
870 text->filepos = pos;
871 if (!text->user_set_vma)
872 text->vma = vma;
873 else
874 vma = text->vma;
876 pos += execp->a_text;
877 vma += execp->a_text;
879 /* Data. */
880 if (!data->user_set_vma)
882 pos += pad;
883 vma += pad;
884 data->vma = vma;
886 else
887 vma = data->vma;
888 execp->a_text += pad;
890 data->filepos = pos;
891 pos += data->size;
892 vma += data->size;
894 /* BSS. */
895 if (!bss->user_set_vma)
897 pos += pad;
898 vma += pad;
899 bss->vma = vma;
901 else if (data->size > 0 || bss->size > 0) /* PR25677: for objcopy --extract-symbol */
903 /* The VMA of the .bss section is set by the VMA of the
904 .data section plus the size of the .data section. We may
905 need to add padding bytes to make this true. */
906 pad = bss->vma - vma;
907 if (pad < 0)
908 pad = 0;
909 pos += pad;
911 execp->a_data = data->size + pad;
912 bss->filepos = pos;
913 execp->a_bss = bss->size;
915 N_SET_MAGIC (execp, OMAGIC);
918 static void
919 adjust_z_magic (bfd *abfd, struct internal_exec *execp)
921 bfd_size_type data_pad, text_pad;
922 file_ptr text_end;
923 const struct aout_backend_data *abdp;
924 /* TRUE if text includes exec header. */
925 bool ztih;
926 asection *text = obj_textsec (abfd);
927 asection *data = obj_datasec (abfd);
928 asection *bss = obj_bsssec (abfd);
930 abdp = aout_backend_info (abfd);
932 /* Text. */
933 ztih = (abdp != NULL
934 && (abdp->text_includes_header
935 || obj_aout_subformat (abfd) == q_magic_format));
936 text->filepos = (ztih
937 ? adata (abfd).exec_bytes_size
938 : adata (abfd).zmagic_disk_block_size);
939 if (!text->user_set_vma)
941 /* ?? Do we really need to check for relocs here? */
942 text->vma = ((abfd->flags & HAS_RELOC)
944 : (ztih
945 ? abdp->default_text_vma + adata (abfd).exec_bytes_size
946 : abdp->default_text_vma));
947 text_pad = 0;
949 else
951 /* The .text section is being loaded at an unusual address. We
952 may need to pad it such that the .data section starts at a page
953 boundary. */
954 if (ztih)
955 text_pad = ((text->filepos - text->vma)
956 & (adata (abfd).page_size - 1));
957 else
958 text_pad = (-text->vma
959 & (adata (abfd).page_size - 1));
962 /* Find start of data. */
963 if (ztih)
965 text_end = text->filepos + execp->a_text;
966 text_pad += BFD_ALIGN (text_end, adata (abfd).page_size) - text_end;
968 else
970 /* Note that if page_size == zmagic_disk_block_size, then
971 filepos == page_size, and this case is the same as the ztih
972 case. */
973 text_end = execp->a_text;
974 text_pad += BFD_ALIGN (text_end, adata (abfd).page_size) - text_end;
975 text_end += text->filepos;
977 execp->a_text += text_pad;
979 /* Data. */
980 if (!data->user_set_vma)
982 bfd_vma vma;
983 vma = text->vma + execp->a_text;
984 data->vma = BFD_ALIGN (vma, adata (abfd).segment_size);
986 if (abdp && abdp->zmagic_mapped_contiguous)
988 text_pad = data->vma - (text->vma + execp->a_text);
989 /* Only pad the text section if the data
990 section is going to be placed after it. */
991 if (text_pad > 0)
992 execp->a_text += text_pad;
994 data->filepos = text->filepos + execp->a_text;
996 /* Fix up exec header while we're at it. */
997 if (ztih && (!abdp || (abdp && !abdp->exec_header_not_counted)))
998 execp->a_text += adata (abfd).exec_bytes_size;
999 N_SET_MAGIC (execp, ZMAGIC);
1001 /* Spec says data section should be rounded up to page boundary. */
1002 execp->a_data = align_power (data->size, bss->alignment_power);
1003 execp->a_data = BFD_ALIGN (execp->a_data, adata (abfd).page_size);
1004 data_pad = execp->a_data - data->size;
1006 /* BSS. */
1007 if (!bss->user_set_vma)
1008 bss->vma = data->vma + execp->a_data;
1009 /* If the BSS immediately follows the data section and extra space
1010 in the page is left after the data section, fudge data
1011 in the header so that the bss section looks smaller by that
1012 amount. We'll start the bss section there, and lie to the OS.
1013 (Note that a linker script, as well as the above assignment,
1014 could have explicitly set the BSS vma to immediately follow
1015 the data section.) */
1016 if (align_power (bss->vma, bss->alignment_power) == data->vma + execp->a_data)
1017 execp->a_bss = data_pad > bss->size ? 0 : bss->size - data_pad;
1018 else
1019 execp->a_bss = bss->size;
1022 static void
1023 adjust_n_magic (bfd *abfd, struct internal_exec *execp)
1025 file_ptr pos = adata (abfd).exec_bytes_size;
1026 bfd_vma vma = 0;
1027 int pad;
1028 asection *text = obj_textsec (abfd);
1029 asection *data = obj_datasec (abfd);
1030 asection *bss = obj_bsssec (abfd);
1032 /* Text. */
1033 text->filepos = pos;
1034 if (!text->user_set_vma)
1035 text->vma = vma;
1036 else
1037 vma = text->vma;
1038 pos += execp->a_text;
1039 vma += execp->a_text;
1041 /* Data. */
1042 data->filepos = pos;
1043 if (!data->user_set_vma)
1044 data->vma = BFD_ALIGN (vma, adata (abfd).segment_size);
1045 vma = data->vma;
1047 /* Since BSS follows data immediately, see if it needs alignment. */
1048 vma += data->size;
1049 pad = align_power (vma, bss->alignment_power) - vma;
1050 execp->a_data = data->size + pad;
1051 pos += execp->a_data;
1053 /* BSS. */
1054 if (!bss->user_set_vma)
1055 bss->vma = vma;
1056 else
1057 vma = bss->vma;
1059 /* Fix up exec header. */
1060 execp->a_bss = bss->size;
1061 N_SET_MAGIC (execp, NMAGIC);
1064 static void
1065 adjust_i_magic (bfd *abfd, struct internal_exec *execp)
1067 file_ptr pos = adata (abfd).exec_bytes_size;
1068 bfd_vma vma = 0;
1069 int pad;
1070 asection *text = obj_textsec (abfd);
1071 asection *data = obj_datasec (abfd);
1072 asection *bss = obj_bsssec (abfd);
1074 /* Text. */
1075 text->filepos = pos;
1076 if (!text->user_set_vma)
1077 text->vma = vma;
1078 else
1079 vma = text->vma;
1080 pos += execp->a_text;
1082 /* Data. */
1083 data->filepos = pos;
1084 if (!data->user_set_vma)
1085 data->vma = 0;
1086 vma = data->vma;
1088 /* Since BSS follows data immediately, see if it needs alignment. */
1089 vma += data->size;
1090 pad = align_power (vma, bss->alignment_power) - vma;
1091 execp->a_data = data->size + pad;
1092 pos += execp->a_data;
1094 /* BSS. */
1095 if (!bss->user_set_vma)
1096 bss->vma = vma;
1097 else
1098 vma = bss->vma;
1100 /* Fix up exec header. */
1101 execp->a_bss = bss->size;
1102 N_SET_MAGIC (execp, IMAGIC);
1105 bool
1106 NAME (aout, adjust_sizes_and_vmas) (bfd *abfd)
1108 struct internal_exec *execp = exec_hdr (abfd);
1110 if (! NAME (aout, make_sections) (abfd))
1111 return false;
1113 if (adata (abfd).magic != undecided_magic)
1114 return true;
1116 execp->a_text = align_power (obj_textsec (abfd)->size,
1117 obj_textsec (abfd)->alignment_power);
1119 /* Rule (heuristic) for when to pad to a new page. Note that there
1120 are (at least) two ways demand-paged (ZMAGIC) files have been
1121 handled. Most Berkeley-based systems start the text segment at
1122 (TARGET_PAGE_SIZE). However, newer versions of SUNOS start the text
1123 segment right after the exec header; the latter is counted in the
1124 text segment size, and is paged in by the kernel with the rest of
1125 the text. */
1127 /* This perhaps isn't the right way to do this, but made it simpler for me
1128 to understand enough to implement it. Better would probably be to go
1129 right from BFD flags to alignment/positioning characteristics. But the
1130 old code was sloppy enough about handling the flags, and had enough
1131 other magic, that it was a little hard for me to understand. I think
1132 I understand it better now, but I haven't time to do the cleanup this
1133 minute. */
1135 if (separate_i_d)
1136 adata (abfd).magic = i_magic;
1137 else if (abfd->flags & WP_TEXT)
1138 adata (abfd).magic = n_magic;
1139 else
1140 adata (abfd).magic = o_magic;
1142 #ifdef BFD_AOUT_DEBUG /* requires gcc2 */
1143 #if __GNUC__ >= 2
1144 fprintf (stderr, "%s text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x,%x>\n",
1145 ({ char *str;
1146 switch (adata (abfd).magic)
1148 case n_magic: str = "NMAGIC"; break;
1149 case o_magic: str = "OMAGIC"; break;
1150 case i_magic: str = "IMAGIC"; break;
1151 case z_magic: str = "ZMAGIC"; break;
1152 default: abort ();
1154 str;
1156 obj_textsec (abfd)->vma, obj_textsec (abfd)->size,
1157 obj_textsec (abfd)->alignment_power,
1158 obj_datasec (abfd)->vma, obj_datasec (abfd)->size,
1159 obj_datasec (abfd)->alignment_power,
1160 obj_bsssec (abfd)->vma, obj_bsssec (abfd)->size,
1161 obj_bsssec (abfd)->alignment_power);
1162 #endif
1163 #endif
1165 switch (adata (abfd).magic)
1167 case o_magic:
1168 adjust_o_magic (abfd, execp);
1169 break;
1170 case z_magic:
1171 adjust_z_magic (abfd, execp);
1172 break;
1173 case n_magic:
1174 adjust_n_magic (abfd, execp);
1175 break;
1176 case i_magic:
1177 adjust_i_magic (abfd, execp);
1178 break;
1179 default:
1180 abort ();
1183 #ifdef BFD_AOUT_DEBUG
1184 fprintf (stderr, " text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x>\n",
1185 obj_textsec (abfd)->vma, execp->a_text,
1186 obj_textsec (abfd)->filepos,
1187 obj_datasec (abfd)->vma, execp->a_data,
1188 obj_datasec (abfd)->filepos,
1189 obj_bsssec (abfd)->vma, execp->a_bss);
1190 #endif
1192 return true;
1195 /* Called by the BFD in response to a bfd_make_section request. */
1197 bool
1198 NAME (aout, new_section_hook) (bfd *abfd, asection *newsect)
1200 /* Align to double at least. */
1201 newsect->alignment_power = bfd_get_arch_info(abfd)->section_align_power;
1203 if (bfd_get_format (abfd) == bfd_object)
1205 if (obj_textsec (abfd) == NULL
1206 && !strcmp (newsect->name, ".text"))
1208 obj_textsec(abfd)= newsect;
1209 newsect->target_index = N_TEXT;
1211 else if (obj_datasec (abfd) == NULL
1212 && !strcmp (newsect->name, ".data"))
1214 obj_datasec (abfd) = newsect;
1215 newsect->target_index = N_DATA;
1217 else if (obj_bsssec (abfd) == NULL
1218 && !strcmp (newsect->name, ".bss"))
1220 obj_bsssec (abfd) = newsect;
1221 newsect->target_index = N_BSS;
1225 /* We allow more than three sections internally. */
1226 return _bfd_generic_new_section_hook (abfd, newsect);
1229 bool
1230 NAME (aout, set_section_contents) (bfd *abfd,
1231 sec_ptr section,
1232 const void * location,
1233 file_ptr offset,
1234 bfd_size_type count)
1236 if (! abfd->output_has_begun)
1238 if (! NAME (aout, adjust_sizes_and_vmas) (abfd))
1239 return false;
1242 if (section == obj_bsssec (abfd))
1244 bfd_set_error (bfd_error_no_contents);
1245 return false;
1248 if (section != obj_textsec (abfd)
1249 && section != obj_datasec (abfd))
1251 _bfd_error_handler
1252 /* xgettext:c-format */
1253 (_("%pB: can not represent section `%pA' in a.out object file format"),
1254 abfd, section);
1255 bfd_set_error (bfd_error_nonrepresentable_section);
1256 return false;
1259 if (count != 0)
1261 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
1262 || bfd_write (location, count, abfd) != count)
1263 return false;
1266 return true;
1269 /* Read the external symbols from an a.out file. */
1271 static bool
1272 aout_get_external_symbols (bfd *abfd)
1274 if (obj_aout_external_syms (abfd) == NULL)
1276 bfd_size_type count;
1277 struct external_nlist *syms = NULL;
1278 bfd_size_type amt = exec_hdr (abfd)->a_syms;
1280 count = amt / EXTERNAL_NLIST_SIZE;
1282 /* PR 17512: file: 011f5a08. */
1283 if (count == 0)
1284 return true;
1286 /* We allocate using malloc to make the values easy to free
1287 later on. If we put them on the objalloc it might not be
1288 possible to free them. */
1289 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1290 return false;
1291 syms = _bfd_malloc_and_read (abfd, amt, amt);
1292 if (syms == NULL)
1293 return false;
1295 obj_aout_external_syms (abfd) = syms;
1296 obj_aout_external_sym_count (abfd) = count;
1299 if (obj_aout_external_strings (abfd) == NULL
1300 && exec_hdr (abfd)->a_syms != 0)
1302 unsigned char string_chars[BYTES_IN_LONG];
1303 bfd_size_type stringsize;
1304 char *strings;
1305 bfd_size_type amt = BYTES_IN_LONG;
1307 /* Get the size of the strings. */
1308 if (bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET) != 0
1309 || bfd_read (string_chars, amt, abfd) != amt)
1310 return false;
1311 stringsize = H_GET_32 (abfd, string_chars);
1312 if (stringsize == 0)
1313 stringsize = 1;
1314 else if (stringsize + 1 < BYTES_IN_LONG + 1
1315 || (size_t) stringsize != stringsize)
1317 bfd_set_error (bfd_error_bad_value);
1318 return false;
1321 strings = (char *) bfd_malloc (stringsize + 1);
1322 if (strings == NULL)
1323 return false;
1325 if (stringsize >= BYTES_IN_LONG)
1327 amt = stringsize - BYTES_IN_LONG;
1328 if (bfd_read (strings + BYTES_IN_LONG, amt, abfd) != amt)
1330 free (strings);
1331 return false;
1335 /* Ensure that a zero index yields an empty string. */
1336 if (stringsize >= BYTES_IN_WORD)
1337 memset (strings, 0, BYTES_IN_LONG);
1339 /* Ensure that the string buffer is NUL terminated. */
1340 strings[stringsize] = 0;
1342 obj_aout_external_strings (abfd) = strings;
1343 obj_aout_external_string_size (abfd) = stringsize;
1346 return true;
1349 /* Translate an a.out symbol into a BFD symbol. The desc, other, type
1350 and symbol->value fields of CACHE_PTR will be set from the a.out
1351 nlist structure. This function is responsible for setting
1352 symbol->flags and symbol->section, and adjusting symbol->value. */
1354 static bool
1355 translate_from_native_sym_flags (bfd *abfd,
1356 aout_symbol_type *cache_ptr)
1358 flagword visible;
1360 if (is_stab (cache_ptr->type, cache_ptr->symbol.name))
1362 asection *sec;
1364 /* This is a debugging symbol. */
1365 cache_ptr->symbol.flags = BSF_DEBUGGING;
1367 /* Work out the symbol section. */
1368 switch (cache_ptr->type)
1370 case N_SO:
1371 case N_SOL:
1372 case N_FUN:
1373 case N_ENTRY:
1374 case N_SLINE:
1375 case N_FN:
1376 sec = obj_textsec (abfd);
1377 break;
1378 case N_STSYM:
1379 case N_DSLINE:
1380 sec = obj_datasec (abfd);
1381 break;
1382 case N_LCSYM:
1383 case N_BSLINE:
1384 sec = obj_bsssec (abfd);
1385 break;
1386 default:
1387 sec = bfd_abs_section_ptr;
1388 break;
1391 cache_ptr->symbol.section = sec;
1392 cache_ptr->symbol.value -= sec->vma;
1394 return true;
1397 /* Get the default visibility. This does not apply to all types, so
1398 we just hold it in a local variable to use if wanted. */
1399 if ((cache_ptr->type & N_EXT) == 0)
1400 visible = BSF_LOCAL;
1401 else
1402 visible = BSF_GLOBAL;
1404 switch (cache_ptr->type)
1406 default:
1407 case N_ABS: case N_ABS | N_EXT:
1408 cache_ptr->symbol.section = bfd_abs_section_ptr;
1409 cache_ptr->symbol.flags = visible;
1410 break;
1412 case N_UNDF | N_EXT:
1413 if (cache_ptr->symbol.value != 0)
1415 /* This is a common symbol. */
1416 cache_ptr->symbol.flags = BSF_GLOBAL;
1417 cache_ptr->symbol.section = bfd_com_section_ptr;
1419 else
1421 cache_ptr->symbol.flags = 0;
1422 cache_ptr->symbol.section = bfd_und_section_ptr;
1424 break;
1426 case N_TEXT: case N_TEXT | N_EXT:
1427 cache_ptr->symbol.section = obj_textsec (abfd);
1428 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1429 cache_ptr->symbol.flags = visible;
1430 break;
1432 case N_DATA: case N_DATA | N_EXT:
1433 cache_ptr->symbol.section = obj_datasec (abfd);
1434 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1435 cache_ptr->symbol.flags = visible;
1436 break;
1438 case N_BSS: case N_BSS | N_EXT:
1439 cache_ptr->symbol.section = obj_bsssec (abfd);
1440 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1441 cache_ptr->symbol.flags = visible;
1442 break;
1445 return true;
1448 /* Set the fields of SYM_POINTER according to CACHE_PTR. */
1450 static bool
1451 translate_to_native_sym_flags (bfd *abfd,
1452 asymbol *cache_ptr,
1453 struct external_nlist *sym_pointer)
1455 bfd_vma value = cache_ptr->value;
1456 asection *sec;
1457 bfd_vma off;
1458 const char *name = cache_ptr->name != NULL ? cache_ptr->name : "*unknown*";
1460 /* Mask out any existing type bits in case copying from one section
1461 to another. */
1462 if (!is_stab (sym_pointer->e_type[0], name))
1463 sym_pointer->e_type[0] &= ~N_TYPE;
1465 sec = bfd_asymbol_section (cache_ptr);
1466 off = 0;
1468 if (sec == NULL)
1470 /* This case occurs, e.g., for the *DEBUG* section of a COFF
1471 file. */
1472 _bfd_error_handler
1473 /* xgettext:c-format */
1474 (_("%pB: can not represent section for symbol `%s' in a.out object file format"),
1475 abfd, name);
1476 bfd_set_error (bfd_error_nonrepresentable_section);
1477 return false;
1480 if (sec->output_section != NULL)
1482 off = sec->output_offset;
1483 sec = sec->output_section;
1486 if (bfd_is_abs_section (sec))
1487 sym_pointer->e_type[0] |= N_ABS;
1488 else if (sec == obj_textsec (abfd))
1489 sym_pointer->e_type[0] |= N_TEXT;
1490 else if (sec == obj_datasec (abfd))
1491 sym_pointer->e_type[0] |= N_DATA;
1492 else if (sec == obj_bsssec (abfd))
1493 sym_pointer->e_type[0] |= N_BSS;
1494 else if (bfd_is_und_section (sec))
1495 sym_pointer->e_type[0] = N_UNDF | N_EXT;
1496 else if (bfd_is_com_section (sec))
1497 sym_pointer->e_type[0] = N_UNDF | N_EXT;
1498 else
1500 _bfd_error_handler
1501 /* xgettext:c-format */
1502 (_("%pB: can not represent section `%pA' in a.out object file format"),
1503 abfd, sec);
1504 bfd_set_error (bfd_error_nonrepresentable_section);
1505 return false;
1508 /* Turn the symbol from section relative to absolute again */
1509 value += sec->vma + off;
1511 if ((cache_ptr->flags & BSF_DEBUGGING) != 0)
1512 sym_pointer->e_type[0] = ((aout_symbol_type *) cache_ptr)->type;
1513 else if ((cache_ptr->flags & BSF_GLOBAL) != 0)
1514 sym_pointer->e_type[0] |= N_EXT;
1516 PUT_WORD(abfd, value, sym_pointer->e_value);
1518 return true;
1521 /* Native-level interface to symbols. */
1523 asymbol *
1524 NAME (aout, make_empty_symbol) (bfd *abfd)
1526 size_t amt = sizeof (aout_symbol_type);
1527 aout_symbol_type *new_symbol_type = bfd_zalloc (abfd, amt);
1529 if (!new_symbol_type)
1530 return NULL;
1531 new_symbol_type->symbol.the_bfd = abfd;
1533 return &new_symbol_type->symbol;
1536 /* Translate a set of external symbols into internal symbols. */
1538 bool
1539 NAME (aout, translate_symbol_table) (bfd *abfd,
1540 aout_symbol_type *in,
1541 struct external_nlist *ext,
1542 bfd_size_type count,
1543 char *str,
1544 bfd_size_type strsize,
1545 bool dynamic)
1547 struct external_nlist *ext_end;
1549 ext_end = ext + count;
1550 for (; ext < ext_end; ext++, in++)
1552 bfd_vma x;
1553 int ovly;
1555 x = GET_WORD (abfd, ext->e_strx);
1556 in->symbol.the_bfd = abfd;
1558 /* For the normal symbols, the zero index points at the number
1559 of bytes in the string table but is to be interpreted as the
1560 null string. For the dynamic symbols, the number of bytes in
1561 the string table is stored in the __DYNAMIC structure and the
1562 zero index points at an actual string. */
1563 if (x == 0 && ! dynamic)
1564 in->symbol.name = "";
1565 else if (x < strsize)
1566 in->symbol.name = str + x;
1567 else
1569 _bfd_error_handler
1570 (_("%pB: invalid string offset %" PRIu64 " >= %" PRIu64),
1571 abfd, (uint64_t) x, (uint64_t) strsize);
1572 bfd_set_error (bfd_error_bad_value);
1573 return false;
1576 ovly = H_GET_8 (abfd, ext->e_ovly);
1577 if (ovly != 0)
1579 _bfd_error_handler
1580 (_("%pB: symbol indicates overlay (not supported)"), abfd);
1581 bfd_set_error (bfd_error_bad_value);
1582 return false;
1585 in->symbol.value = GET_WORD (abfd, ext->e_value);
1586 /* e_desc is zero for normal symbols but for .stab symbols it
1587 carries the desc field in our extended 2.11BSD format. */
1588 in->desc = H_GET_16 (abfd, ext->e_desc);
1589 in->other = 0;
1590 in->type = H_GET_8 (abfd, ext->e_type);
1591 in->symbol.udata.p = NULL;
1593 if (! translate_from_native_sym_flags (abfd, in))
1594 return false;
1596 if (dynamic)
1597 in->symbol.flags |= BSF_DYNAMIC;
1600 return true;
1603 /* We read the symbols into a buffer, which is discarded when this
1604 function exits. We read the strings into a buffer large enough to
1605 hold them all plus all the cached symbol entries. */
1607 bool
1608 NAME (aout, slurp_symbol_table) (bfd *abfd)
1610 struct external_nlist *old_external_syms;
1611 aout_symbol_type *cached;
1612 bfd_size_type cached_size;
1614 /* If there's no work to be done, don't do any. */
1615 if (obj_aout_symbols (abfd) != NULL)
1616 return true;
1618 old_external_syms = obj_aout_external_syms (abfd);
1620 if (! aout_get_external_symbols (abfd))
1621 return false;
1623 cached_size = obj_aout_external_sym_count (abfd);
1624 cached_size *= sizeof (aout_symbol_type);
1625 cached = bfd_zmalloc (cached_size);
1626 if (cached == NULL && cached_size != 0)
1627 return false;
1629 /* Convert from external symbol information to internal. */
1630 if (! (NAME (aout, translate_symbol_table)
1631 (abfd, cached,
1632 obj_aout_external_syms (abfd),
1633 obj_aout_external_sym_count (abfd),
1634 obj_aout_external_strings (abfd),
1635 obj_aout_external_string_size (abfd),
1636 false)))
1638 free (cached);
1639 return false;
1642 abfd->symcount = obj_aout_external_sym_count (abfd);
1644 obj_aout_symbols (abfd) = cached;
1646 /* It is very likely that anybody who calls this function will not
1647 want the external symbol information, so if it was allocated
1648 because of our call to aout_get_external_symbols, we free it up
1649 right away to save space. */
1650 if (old_external_syms == NULL
1651 && obj_aout_external_syms (abfd) != NULL)
1653 free (obj_aout_external_syms (abfd));
1654 obj_aout_external_syms (abfd) = NULL;
1657 return true;
1660 /* We use a hash table when writing out symbols so that we only write
1661 out a particular string once. This helps particularly when the
1662 linker writes out stabs debugging entries, because each different
1663 contributing object file tends to have many duplicate stabs
1664 strings.
1666 This hash table code breaks dbx on SunOS 4.1.3, so we don't do it
1667 if BFD_TRADITIONAL_FORMAT is set. */
1669 /* Get the index of a string in a strtab, adding it if it is not
1670 already present. */
1672 static inline bfd_size_type
1673 add_to_stringtab (bfd *abfd,
1674 struct bfd_strtab_hash *tab,
1675 const char *str,
1676 bool copy)
1678 bool hash;
1679 bfd_size_type str_index;
1681 /* An index of 0 always means the empty string. */
1682 if (str == 0 || *str == '\0')
1683 return 0;
1685 /* Don't hash if BFD_TRADITIONAL_FORMAT is set, because SunOS dbx
1686 doesn't understand a hashed string table. */
1687 hash = true;
1688 if ((abfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
1689 hash = false;
1691 str_index = _bfd_stringtab_add (tab, str, hash, copy);
1693 if (str_index != (bfd_size_type) -1)
1694 /* Add BYTES_IN_LONG to the return value to account for the
1695 space taken up by the string table size. */
1696 str_index += BYTES_IN_LONG;
1698 return str_index;
1701 /* Write out a strtab. ABFD is already at the right location in the
1702 file. */
1704 static bool
1705 emit_stringtab (bfd *abfd, struct bfd_strtab_hash *tab)
1707 bfd_byte buffer[BYTES_IN_LONG];
1709 /* The string table starts with the size. */
1710 H_PUT_32 (abfd, _bfd_stringtab_size (tab) + BYTES_IN_LONG, buffer);
1711 if (bfd_write (buffer, BYTES_IN_LONG, abfd) != BYTES_IN_LONG)
1712 return false;
1714 return _bfd_stringtab_emit (abfd, tab);
1717 bool
1718 NAME (aout, write_syms) (bfd *abfd)
1720 unsigned int count ;
1721 asymbol **generic = bfd_get_outsymbols (abfd);
1722 struct bfd_strtab_hash *strtab;
1724 strtab = _bfd_stringtab_init ();
1725 if (strtab == NULL)
1726 return false;
1728 for (count = 0; count < bfd_get_symcount (abfd); count++)
1730 asymbol *g = generic[count];
1731 bfd_size_type indx;
1732 struct external_nlist nsp;
1734 indx = add_to_stringtab (abfd, strtab, g->name, false);
1735 if (indx == (bfd_size_type) -1)
1736 goto error_return;
1737 PUT_WORD (abfd, indx, nsp.e_strx);
1739 if (bfd_asymbol_flavour(g) == abfd->xvec->flavour)
1741 H_PUT_16 (abfd, aout_symbol (g)->desc, nsp.e_desc);
1742 H_PUT_8 (abfd, 0, nsp.e_ovly);
1743 H_PUT_8 (abfd, aout_symbol (g)->type, nsp.e_type);
1745 else
1747 H_PUT_16 (abfd, 0, nsp.e_desc);
1748 H_PUT_8 (abfd, 0, nsp.e_ovly);
1749 H_PUT_8 (abfd, 0, nsp.e_type);
1752 if (! translate_to_native_sym_flags (abfd, g, &nsp))
1753 goto error_return;
1755 if (bfd_write (&nsp, EXTERNAL_NLIST_SIZE, abfd) != EXTERNAL_NLIST_SIZE)
1756 goto error_return;
1758 /* NB: `KEEPIT' currently overlays `udata.p', so set this only
1759 here, at the end. */
1760 g->KEEPIT = count;
1763 if (! emit_stringtab (abfd, strtab))
1764 goto error_return;
1766 _bfd_stringtab_free (strtab);
1768 return true;
1770 error_return:
1771 _bfd_stringtab_free (strtab);
1772 return false;
1776 long
1777 NAME (aout, canonicalize_symtab) (bfd *abfd, asymbol **location)
1779 unsigned int counter = 0;
1780 aout_symbol_type *symbase;
1782 if (!NAME (aout, slurp_symbol_table) (abfd))
1783 return -1;
1785 for (symbase = obj_aout_symbols (abfd); counter++ < bfd_get_symcount (abfd);)
1786 *(location++) = (asymbol *)(symbase++);
1787 *location++ =0;
1788 return bfd_get_symcount (abfd);
1792 /* Output extended relocation information to a file in target byte order. */
1794 static void
1795 pdp11_aout_swap_reloc_out (bfd *abfd, arelent *g, bfd_byte *natptr)
1797 int r_index;
1798 int r_pcrel;
1799 int reloc_entry;
1800 int r_type;
1801 asymbol *sym = *(g->sym_ptr_ptr);
1802 asection *output_section = sym->section->output_section;
1804 if (g->addend != 0)
1805 fprintf (stderr, "BFD: can't do this reloc addend stuff\n");
1807 r_pcrel = g->howto->pc_relative;
1809 if (bfd_is_abs_section (output_section))
1810 r_type = RABS;
1811 else if (output_section == obj_textsec (abfd))
1812 r_type = RTEXT;
1813 else if (output_section == obj_datasec (abfd))
1814 r_type = RDATA;
1815 else if (output_section == obj_bsssec (abfd))
1816 r_type = RBSS;
1817 else if (bfd_is_und_section (output_section))
1818 r_type = REXT;
1819 else if (bfd_is_com_section (output_section))
1820 r_type = REXT;
1821 else
1822 r_type = -1;
1824 BFD_ASSERT (r_type != -1);
1826 if (r_type == RABS)
1827 r_index = 0;
1828 else
1829 r_index = (*(g->sym_ptr_ptr))->KEEPIT;
1831 reloc_entry = r_index << 4 | r_type | r_pcrel;
1833 PUT_WORD (abfd, reloc_entry, natptr);
1836 /* BFD deals internally with all things based from the section they're
1837 in. so, something in 10 bytes into a text section with a base of
1838 50 would have a symbol (.text+10) and know .text vma was 50.
1840 Aout keeps all it's symbols based from zero, so the symbol would
1841 contain 60. This macro subs the base of each section from the value
1842 to give the true offset from the section */
1845 #define MOVE_ADDRESS(ad) \
1846 if (r_extern) \
1848 /* Undefined symbol. */ \
1849 if (symbols != NULL && r_index < bfd_get_symcount (abfd)) \
1850 cache_ptr->sym_ptr_ptr = symbols + r_index; \
1851 else \
1852 cache_ptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr; \
1853 cache_ptr->addend = ad; \
1855 else \
1857 /* Defined, section relative. replace symbol with pointer to \
1858 symbol which points to section. */ \
1859 switch (r_index) \
1861 case N_TEXT: \
1862 case N_TEXT | N_EXT: \
1863 cache_ptr->sym_ptr_ptr = obj_textsec (abfd)->symbol_ptr_ptr; \
1864 cache_ptr->addend = ad - su->textsec->vma; \
1865 break; \
1866 case N_DATA: \
1867 case N_DATA | N_EXT: \
1868 cache_ptr->sym_ptr_ptr = obj_datasec (abfd)->symbol_ptr_ptr; \
1869 cache_ptr->addend = ad - su->datasec->vma; \
1870 break; \
1871 case N_BSS: \
1872 case N_BSS | N_EXT: \
1873 cache_ptr->sym_ptr_ptr = obj_bsssec (abfd)->symbol_ptr_ptr; \
1874 cache_ptr->addend = ad - su->bsssec->vma; \
1875 break; \
1876 default: \
1877 case N_ABS: \
1878 case N_ABS | N_EXT: \
1879 cache_ptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr; \
1880 cache_ptr->addend = ad; \
1881 break; \
1885 static void
1886 pdp11_aout_swap_reloc_in (bfd * abfd,
1887 bfd_byte * bytes,
1888 arelent * cache_ptr,
1889 bfd_size_type offset,
1890 asymbol ** symbols,
1891 bfd_size_type symcount)
1893 struct aoutdata *su = &(abfd->tdata.aout_data->a);
1894 unsigned int r_index;
1895 int reloc_entry;
1896 int r_extern;
1897 int r_pcrel;
1899 reloc_entry = GET_WORD (abfd, (void *) bytes);
1901 r_pcrel = reloc_entry & RELFLG;
1903 cache_ptr->address = offset;
1904 cache_ptr->howto = howto_table_pdp11 + (r_pcrel ? 1 : 0);
1906 if ((reloc_entry & RTYPE) == RABS)
1907 r_index = N_ABS;
1908 else
1909 r_index = RINDEX (reloc_entry);
1911 /* r_extern reflects whether the symbol the reloc is against is
1912 local or global. */
1913 r_extern = (reloc_entry & RTYPE) == REXT;
1915 if (r_extern && r_index >= symcount)
1917 /* We could arrange to return an error, but it might be useful
1918 to see the file even if it is bad. FIXME: Of course this
1919 means that objdump -r *doesn't* see the actual reloc, and
1920 objcopy silently writes a different reloc. */
1921 r_extern = 0;
1922 r_index = N_ABS;
1925 MOVE_ADDRESS(0);
1928 /* Read and swap the relocs for a section. */
1930 bool
1931 NAME (aout, slurp_reloc_table) (bfd *abfd, sec_ptr asect, asymbol **symbols)
1933 bfd_byte *rptr;
1934 bfd_size_type count;
1935 bfd_size_type reloc_size;
1936 void * relocs;
1937 arelent *reloc_cache;
1938 size_t each_size;
1939 unsigned int counter = 0;
1940 arelent *cache_ptr;
1942 if (asect->relocation)
1943 return true;
1945 if (asect->flags & SEC_CONSTRUCTOR)
1946 return true;
1948 if (asect == obj_datasec (abfd))
1949 reloc_size = exec_hdr(abfd)->a_drsize;
1950 else if (asect == obj_textsec (abfd))
1951 reloc_size = exec_hdr(abfd)->a_trsize;
1952 else if (asect == obj_bsssec (abfd))
1953 reloc_size = 0;
1954 else
1956 bfd_set_error (bfd_error_invalid_operation);
1957 return false;
1960 if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0)
1961 return false;
1962 relocs = _bfd_malloc_and_read (abfd, reloc_size, reloc_size);
1963 if (relocs == NULL && reloc_size != 0)
1964 return false;
1966 each_size = obj_reloc_entry_size (abfd);
1967 count = reloc_size / each_size;
1969 /* Count the number of NON-ZERO relocs, this is the count we want. */
1971 unsigned int real_count = 0;
1973 for (counter = 0; counter < count; counter++)
1975 int x;
1977 x = GET_WORD (abfd, (char *) relocs + each_size * counter);
1978 if (x != 0)
1979 real_count++;
1982 count = real_count;
1985 reloc_cache = bfd_zmalloc (count * sizeof (arelent));
1986 if (reloc_cache == NULL && count != 0)
1987 return false;
1989 cache_ptr = reloc_cache;
1991 rptr = relocs;
1992 for (counter = 0;
1993 counter < count;
1994 counter++, rptr += RELOC_SIZE, cache_ptr++)
1996 while (GET_WORD (abfd, (void *) rptr) == 0)
1998 rptr += RELOC_SIZE;
1999 if ((char *) rptr >= (char *) relocs + reloc_size)
2000 goto done;
2003 pdp11_aout_swap_reloc_in (abfd, rptr, cache_ptr,
2004 (bfd_size_type) ((char *) rptr - (char *) relocs),
2005 symbols,
2006 (bfd_size_type) bfd_get_symcount (abfd));
2008 done:
2009 /* Just in case, if rptr >= relocs + reloc_size should happen
2010 too early. */
2011 BFD_ASSERT (counter == count);
2013 free (relocs);
2015 asect->relocation = reloc_cache;
2016 asect->reloc_count = cache_ptr - reloc_cache;
2018 return true;
2021 /* Write out a relocation section into an object file. */
2023 bool
2024 NAME (aout, squirt_out_relocs) (bfd *abfd, asection *section)
2026 arelent **generic;
2027 unsigned char *native;
2028 unsigned int count = section->reloc_count;
2029 bfd_size_type natsize;
2031 natsize = section->size;
2032 native = bfd_zalloc (abfd, natsize);
2033 if (!native)
2034 return false;
2036 generic = section->orelocation;
2037 if (generic != NULL)
2039 while (count > 0)
2041 bfd_byte *r;
2043 if ((*generic)->howto == NULL
2044 || (*generic)->sym_ptr_ptr == NULL)
2046 bfd_set_error (bfd_error_invalid_operation);
2047 _bfd_error_handler (_("%pB: attempt to write out "
2048 "unknown reloc type"), abfd);
2049 bfd_release (abfd, native);
2050 return false;
2052 r = native + (*generic)->address;
2053 pdp11_aout_swap_reloc_out (abfd, *generic, r);
2054 count--;
2055 generic++;
2059 if (bfd_write (native, natsize, abfd) != natsize)
2061 bfd_release (abfd, native);
2062 return false;
2065 bfd_release (abfd, native);
2066 return true;
2069 /* This is stupid. This function should be a boolean predicate. */
2071 long
2072 NAME (aout, canonicalize_reloc) (bfd *abfd,
2073 sec_ptr section,
2074 arelent **relptr,
2075 asymbol **symbols)
2077 arelent *tblptr = section->relocation;
2078 unsigned int count;
2080 if (section == obj_bsssec (abfd))
2082 *relptr = NULL;
2083 return 0;
2086 if (!(tblptr || NAME (aout, slurp_reloc_table)(abfd, section, symbols)))
2087 return -1;
2089 if (section->flags & SEC_CONSTRUCTOR)
2091 arelent_chain *chain = section->constructor_chain;
2093 for (count = 0; count < section->reloc_count; count ++)
2095 *relptr ++ = &chain->relent;
2096 chain = chain->next;
2099 else
2101 tblptr = section->relocation;
2103 for (count = 0; count++ < section->reloc_count;)
2104 *relptr++ = tblptr++;
2107 *relptr = 0;
2109 return section->reloc_count;
2112 long
2113 NAME (aout, get_reloc_upper_bound) (bfd *abfd, sec_ptr asect)
2115 size_t count, raw;
2117 if (asect->flags & SEC_CONSTRUCTOR)
2118 count = asect->reloc_count;
2119 else if (asect == obj_datasec (abfd))
2120 count = exec_hdr (abfd)->a_drsize / obj_reloc_entry_size (abfd);
2121 else if (asect == obj_textsec (abfd))
2122 count = exec_hdr (abfd)->a_trsize / obj_reloc_entry_size (abfd);
2123 else if (asect == obj_bsssec (abfd))
2124 count = 0;
2125 else
2127 bfd_set_error (bfd_error_invalid_operation);
2128 return -1;
2131 if (count >= LONG_MAX / sizeof (arelent *)
2132 || _bfd_mul_overflow (count, obj_reloc_entry_size (abfd), &raw))
2134 bfd_set_error (bfd_error_file_too_big);
2135 return -1;
2137 if (!bfd_write_p (abfd))
2139 ufile_ptr filesize = bfd_get_file_size (abfd);
2140 if (filesize != 0 && raw > filesize)
2142 bfd_set_error (bfd_error_file_truncated);
2143 return -1;
2146 return (count + 1) * sizeof (arelent *);
2150 long
2151 NAME (aout, get_symtab_upper_bound) (bfd *abfd)
2153 if (!NAME (aout, slurp_symbol_table) (abfd))
2154 return -1;
2156 return (bfd_get_symcount (abfd) + 1) * (sizeof (aout_symbol_type *));
2159 alent *
2160 NAME (aout, get_lineno) (bfd * abfd ATTRIBUTE_UNUSED,
2161 asymbol * symbol ATTRIBUTE_UNUSED)
2163 return NULL;
2166 void
2167 NAME (aout, get_symbol_info) (bfd * abfd ATTRIBUTE_UNUSED,
2168 asymbol *symbol,
2169 symbol_info *ret)
2171 bfd_symbol_info (symbol, ret);
2173 if (ret->type == '?')
2175 int type_code = aout_symbol(symbol)->type & 0xff;
2176 const char *stab_name = bfd_get_stab_name (type_code);
2177 static char buf[10];
2179 if (stab_name == NULL)
2181 sprintf(buf, "(%d)", type_code);
2182 stab_name = buf;
2184 ret->type = '-';
2185 ret->stab_type = type_code;
2186 ret->stab_other = (unsigned) (aout_symbol(symbol)->other & 0xff);
2187 ret->stab_desc = (unsigned) (aout_symbol(symbol)->desc & 0xffff);
2188 ret->stab_name = stab_name;
2192 void
2193 NAME (aout, print_symbol) (bfd * abfd,
2194 void * afile,
2195 asymbol *symbol,
2196 bfd_print_symbol_type how)
2198 FILE *file = (FILE *) afile;
2200 switch (how)
2202 case bfd_print_symbol_name:
2203 if (symbol->name)
2204 fprintf(file,"%s", symbol->name);
2205 break;
2206 case bfd_print_symbol_more:
2207 fprintf(file,"%4x %2x %2x",
2208 (unsigned) (aout_symbol (symbol)->desc & 0xffff),
2209 (unsigned) (aout_symbol (symbol)->other & 0xff),
2210 (unsigned) (aout_symbol (symbol)->type));
2211 break;
2212 case bfd_print_symbol_all:
2214 const char *section_name = symbol->section->name;
2216 bfd_print_symbol_vandf (abfd, (void *) file, symbol);
2218 fprintf (file," %-5s %04x %02x %02x",
2219 section_name,
2220 (unsigned) (aout_symbol (symbol)->desc & 0xffff),
2221 (unsigned) (aout_symbol (symbol)->other & 0xff),
2222 (unsigned) (aout_symbol (symbol)->type & 0xff));
2223 if (symbol->name)
2224 fprintf(file," %s", symbol->name);
2226 break;
2230 /* If we don't have to allocate more than 1MB to hold the generic
2231 symbols, we use the generic minisymbol method: it's faster, since
2232 it only translates the symbols once, not multiple times. */
2233 #define MINISYM_THRESHOLD (1000000 / sizeof (asymbol))
2235 /* Read minisymbols. For minisymbols, we use the unmodified a.out
2236 symbols. The minisymbol_to_symbol function translates these into
2237 BFD asymbol structures. */
2239 long
2240 NAME (aout, read_minisymbols) (bfd *abfd,
2241 bool dynamic,
2242 void * *minisymsp,
2243 unsigned int *sizep)
2245 if (dynamic)
2246 /* We could handle the dynamic symbols here as well, but it's
2247 easier to hand them off. */
2248 return _bfd_generic_read_minisymbols (abfd, dynamic, minisymsp, sizep);
2250 if (! aout_get_external_symbols (abfd))
2251 return -1;
2253 if (obj_aout_external_sym_count (abfd) < MINISYM_THRESHOLD)
2254 return _bfd_generic_read_minisymbols (abfd, dynamic, minisymsp, sizep);
2256 *minisymsp = (void *) obj_aout_external_syms (abfd);
2258 /* By passing the external symbols back from this routine, we are
2259 giving up control over the memory block. Clear
2260 obj_aout_external_syms, so that we do not try to free it
2261 ourselves. */
2262 obj_aout_external_syms (abfd) = NULL;
2264 *sizep = EXTERNAL_NLIST_SIZE;
2265 return obj_aout_external_sym_count (abfd);
2268 /* Convert a minisymbol to a BFD asymbol. A minisymbol is just an
2269 unmodified a.out symbol. The SYM argument is a structure returned
2270 by bfd_make_empty_symbol, which we fill in here. */
2272 asymbol *
2273 NAME (aout, minisymbol_to_symbol) (bfd *abfd,
2274 bool dynamic,
2275 const void * minisym,
2276 asymbol *sym)
2278 if (dynamic
2279 || obj_aout_external_sym_count (abfd) < MINISYM_THRESHOLD)
2280 return _bfd_generic_minisymbol_to_symbol (abfd, dynamic, minisym, sym);
2282 memset (sym, 0, sizeof (aout_symbol_type));
2284 /* We call translate_symbol_table to translate a single symbol. */
2285 if (! (NAME (aout, translate_symbol_table)
2286 (abfd,
2287 (aout_symbol_type *) sym,
2288 (struct external_nlist *) minisym,
2289 (bfd_size_type) 1,
2290 obj_aout_external_strings (abfd),
2291 obj_aout_external_string_size (abfd),
2292 false)))
2293 return NULL;
2295 return sym;
2298 /* Provided a BFD, a section and an offset into the section, calculate
2299 and return the name of the source file and the line nearest to the
2300 wanted location. */
2302 bool
2303 NAME (aout, find_nearest_line) (bfd *abfd,
2304 asymbol **symbols,
2305 asection *section,
2306 bfd_vma offset,
2307 const char **filename_ptr,
2308 const char **functionname_ptr,
2309 unsigned int *line_ptr,
2310 unsigned int *discriminator_ptr)
2312 /* Run down the file looking for the filename, function and linenumber. */
2313 asymbol **p;
2314 const char *directory_name = NULL;
2315 const char *main_file_name = NULL;
2316 const char *current_file_name = NULL;
2317 const char *line_file_name = NULL; /* Value of current_file_name at line number. */
2318 bfd_vma low_line_vma = 0;
2319 bfd_vma low_func_vma = 0;
2320 asymbol *func = 0;
2321 size_t filelen, funclen;
2322 char *buf;
2324 *filename_ptr = bfd_get_filename (abfd);
2325 *functionname_ptr = NULL;
2326 *line_ptr = 0;
2327 if (discriminator_ptr)
2328 *discriminator_ptr = 0;
2330 if (symbols != NULL)
2332 for (p = symbols; *p; p++)
2334 aout_symbol_type *q = (aout_symbol_type *)(*p);
2335 next:
2336 switch (q->type)
2338 case N_TEXT:
2339 /* If this looks like a file name symbol, and it comes after
2340 the line number we have found so far, but before the
2341 offset, then we have probably not found the right line
2342 number. */
2343 if (q->symbol.value <= offset
2344 && ((q->symbol.value > low_line_vma
2345 && (line_file_name != NULL
2346 || *line_ptr != 0))
2347 || (q->symbol.value > low_func_vma
2348 && func != NULL)))
2350 const char * symname;
2352 symname = q->symbol.name;
2354 if (symname != NULL
2355 && strlen (symname) > 2
2356 && strcmp (symname + strlen (symname) - 2, ".o") == 0)
2358 if (q->symbol.value > low_line_vma)
2360 *line_ptr = 0;
2361 line_file_name = NULL;
2363 if (q->symbol.value > low_func_vma)
2364 func = NULL;
2367 break;
2369 case N_SO:
2370 /* If this symbol is less than the offset, but greater than
2371 the line number we have found so far, then we have not
2372 found the right line number. */
2373 if (q->symbol.value <= offset)
2375 if (q->symbol.value > low_line_vma)
2377 *line_ptr = 0;
2378 line_file_name = NULL;
2380 if (q->symbol.value > low_func_vma)
2381 func = NULL;
2384 main_file_name = current_file_name = q->symbol.name;
2385 /* Look ahead to next symbol to check if that too is an N_SO. */
2386 p++;
2387 if (*p == NULL)
2388 goto done;
2389 q = (aout_symbol_type *)(*p);
2390 if (q->type != (int) N_SO)
2391 goto next;
2393 /* Found a second N_SO First is directory; second is filename. */
2394 directory_name = current_file_name;
2395 main_file_name = current_file_name = q->symbol.name;
2396 if (obj_textsec(abfd) != section)
2397 goto done;
2398 break;
2399 case N_SOL:
2400 current_file_name = q->symbol.name;
2401 break;
2403 case N_SLINE:
2404 case N_DSLINE:
2405 case N_BSLINE:
2406 /* We'll keep this if it resolves nearer than the one we have
2407 already. */
2408 if (q->symbol.value >= low_line_vma
2409 && q->symbol.value <= offset)
2411 *line_ptr = q->desc;
2412 low_line_vma = q->symbol.value;
2413 line_file_name = current_file_name;
2415 break;
2417 case N_FUN:
2419 /* We'll keep this if it is nearer than the one we have already. */
2420 if (q->symbol.value >= low_func_vma &&
2421 q->symbol.value <= offset)
2423 low_func_vma = q->symbol.value;
2424 func = (asymbol *) q;
2426 else if (q->symbol.value > offset)
2427 goto done;
2429 break;
2434 done:
2435 if (*line_ptr != 0)
2436 main_file_name = line_file_name;
2438 if (main_file_name == NULL
2439 || main_file_name[0] == '/'
2440 || directory_name == NULL)
2441 filelen = 0;
2442 else
2443 filelen = strlen (directory_name) + strlen (main_file_name);
2444 if (func == NULL)
2445 funclen = 0;
2446 else
2447 funclen = strlen (bfd_asymbol_name (func));
2449 free (adata (abfd).line_buf);
2450 if (filelen + funclen == 0)
2451 adata (abfd).line_buf = buf = NULL;
2452 else
2454 buf = bfd_malloc ((bfd_size_type) filelen + funclen + 3);
2455 adata (abfd).line_buf = buf;
2456 if (buf == NULL)
2457 return false;
2460 if (main_file_name != NULL)
2462 if (main_file_name[0] == '/' || directory_name == NULL)
2463 *filename_ptr = main_file_name;
2464 else
2466 if (buf == NULL)
2467 /* PR binutils/20891: In a corrupt input file both
2468 main_file_name and directory_name can be empty... */
2469 * filename_ptr = NULL;
2470 else
2472 snprintf (buf, filelen + 1, "%s%s", directory_name,
2473 main_file_name);
2474 *filename_ptr = buf;
2475 buf += filelen + 1;
2480 if (func)
2482 const char *function = func->name;
2483 char *colon;
2485 if (buf == NULL)
2487 /* PR binutils/20892: In a corrupt input file func can be empty. */
2488 * functionname_ptr = NULL;
2489 return true;
2491 /* The caller expects a symbol name. We actually have a
2492 function name, without the leading underscore. Put the
2493 underscore back in, so that the caller gets a symbol name. */
2494 if (bfd_get_symbol_leading_char (abfd) == '\0')
2495 strcpy (buf, function);
2496 else
2498 buf[0] = bfd_get_symbol_leading_char (abfd);
2499 strcpy (buf + 1, function);
2502 /* Have to remove : stuff. */
2503 colon = strchr (buf, ':');
2504 if (colon != NULL)
2505 *colon = '\0';
2506 *functionname_ptr = buf;
2509 return true;
2513 NAME (aout, sizeof_headers) (bfd *abfd,
2514 struct bfd_link_info *info ATTRIBUTE_UNUSED)
2516 return adata (abfd).exec_bytes_size;
2519 /* Throw away most malloc'd and alloc'd information for this BFD. */
2521 bool
2522 NAME (aout, bfd_free_cached_info) (bfd *abfd)
2524 if ((bfd_get_format (abfd) == bfd_object
2525 || bfd_get_format (abfd) == bfd_core)
2526 && abfd->tdata.aout_data != NULL)
2528 #define BFCI_FREE(x) do { free (x); x = NULL; } while (0)
2529 BFCI_FREE (adata (abfd).line_buf);
2530 BFCI_FREE (obj_aout_symbols (abfd));
2531 BFCI_FREE (obj_aout_external_syms (abfd));
2532 BFCI_FREE (obj_aout_external_strings (abfd));
2533 for (asection *o = abfd->sections; o != NULL; o = o->next)
2534 BFCI_FREE (o->relocation);
2535 #undef BFCI_FREE
2538 return _bfd_generic_bfd_free_cached_info (abfd);
2541 /* Routine to create an entry in an a.out link hash table. */
2543 struct bfd_hash_entry *
2544 NAME (aout, link_hash_newfunc) (struct bfd_hash_entry *entry,
2545 struct bfd_hash_table *table,
2546 const char *string)
2548 struct aout_link_hash_entry *ret = (struct aout_link_hash_entry *) entry;
2550 /* Allocate the structure if it has not already been allocated by a
2551 subclass. */
2552 if (ret == NULL)
2553 ret = bfd_hash_allocate (table, sizeof (* ret));
2554 if (ret == NULL)
2555 return NULL;
2557 /* Call the allocation method of the superclass. */
2558 ret = (struct aout_link_hash_entry *)
2559 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret, table, string);
2560 if (ret)
2562 /* Set local fields. */
2563 ret->written = false;
2564 ret->indx = -1;
2567 return (struct bfd_hash_entry *) ret;
2570 /* Initialize an a.out link hash table. */
2572 bool
2573 NAME (aout, link_hash_table_init) (struct aout_link_hash_table *table,
2574 bfd *abfd,
2575 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
2576 struct bfd_hash_table *,
2577 const char *),
2578 unsigned int entsize)
2580 return _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
2583 /* Create an a.out link hash table. */
2585 struct bfd_link_hash_table *
2586 NAME (aout, link_hash_table_create) (bfd *abfd)
2588 struct aout_link_hash_table *ret;
2589 size_t amt = sizeof (struct aout_link_hash_table);
2591 ret = bfd_malloc (amt);
2592 if (ret == NULL)
2593 return NULL;
2594 if (! NAME (aout, link_hash_table_init) (ret, abfd,
2595 NAME (aout, link_hash_newfunc),
2596 sizeof (struct aout_link_hash_entry)))
2598 free (ret);
2599 return NULL;
2601 return &ret->root;
2604 /* Free up the internal symbols read from an a.out file. */
2606 static bool
2607 aout_link_free_symbols (bfd *abfd)
2609 if (obj_aout_external_syms (abfd) != NULL)
2611 free ((void *) obj_aout_external_syms (abfd));
2612 obj_aout_external_syms (abfd) = NULL;
2615 if (obj_aout_external_strings (abfd) != NULL)
2617 free ((void *) obj_aout_external_strings (abfd));
2618 obj_aout_external_strings (abfd) = NULL;
2620 return true;
2623 /* Given an a.out BFD, add symbols to the global hash table as
2624 appropriate. */
2626 bool
2627 NAME (aout, link_add_symbols) (bfd *abfd, struct bfd_link_info *info)
2629 switch (bfd_get_format (abfd))
2631 case bfd_object:
2632 return aout_link_add_object_symbols (abfd, info);
2633 case bfd_archive:
2634 return _bfd_generic_link_add_archive_symbols
2635 (abfd, info, aout_link_check_archive_element);
2636 default:
2637 bfd_set_error (bfd_error_wrong_format);
2638 return false;
2642 /* Add symbols from an a.out object file. */
2644 static bool
2645 aout_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
2647 if (! aout_get_external_symbols (abfd))
2648 return false;
2649 if (! aout_link_add_symbols (abfd, info))
2650 return false;
2651 if (! info->keep_memory)
2653 if (! aout_link_free_symbols (abfd))
2654 return false;
2656 return true;
2659 /* Look through the internal symbols to see if this object file should
2660 be included in the link. We should include this object file if it
2661 defines any symbols which are currently undefined. If this object
2662 file defines a common symbol, then we may adjust the size of the
2663 known symbol but we do not include the object file in the link
2664 (unless there is some other reason to include it). */
2666 static bool
2667 aout_link_check_ar_symbols (bfd *abfd,
2668 struct bfd_link_info *info,
2669 bool *pneeded,
2670 bfd **subsbfd)
2672 struct external_nlist *p;
2673 struct external_nlist *pend;
2674 char *strings;
2676 *pneeded = false;
2678 /* Look through all the symbols. */
2679 p = obj_aout_external_syms (abfd);
2680 pend = p + obj_aout_external_sym_count (abfd);
2681 strings = obj_aout_external_strings (abfd);
2682 for (; p < pend; p++)
2684 int type = H_GET_8 (abfd, p->e_type);
2685 const char *name = strings + GET_WORD (abfd, p->e_strx);
2686 struct bfd_link_hash_entry *h;
2688 /* Ignore symbols that are not externally visible. This is an
2689 optimization only, as we check the type more thoroughly
2690 below. */
2691 if ((type & N_EXT) == 0
2692 || is_stab(type, name)
2693 || type == N_FN)
2694 continue;
2696 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
2698 /* We are only interested in symbols that are currently
2699 undefined or common. */
2700 if (h == NULL
2701 || (h->type != bfd_link_hash_undefined
2702 && h->type != bfd_link_hash_common))
2703 continue;
2705 if (type == (N_TEXT | N_EXT)
2706 || type == (N_DATA | N_EXT)
2707 || type == (N_BSS | N_EXT)
2708 || type == (N_ABS | N_EXT))
2710 /* This object file defines this symbol. We must link it
2711 in. This is true regardless of whether the current
2712 definition of the symbol is undefined or common. If the
2713 current definition is common, we have a case in which we
2714 have already seen an object file including
2715 int a;
2716 and this object file from the archive includes
2717 int a = 5;
2718 In such a case we must include this object file.
2720 FIXME: The SunOS 4.1.3 linker will pull in the archive
2721 element if the symbol is defined in the .data section,
2722 but not if it is defined in the .text section. That
2723 seems a bit crazy to me, and I haven't implemented it.
2724 However, it might be correct. */
2725 if (!(*info->callbacks
2726 ->add_archive_element) (info, abfd, name, subsbfd))
2727 continue;
2728 *pneeded = true;
2729 return true;
2732 if (type == (N_UNDF | N_EXT))
2734 bfd_vma value;
2736 value = GET_WORD (abfd, p->e_value);
2737 if (value != 0)
2739 /* This symbol is common in the object from the archive
2740 file. */
2741 if (h->type == bfd_link_hash_undefined)
2743 bfd *symbfd;
2744 unsigned int power;
2746 symbfd = h->u.undef.abfd;
2747 if (symbfd == NULL)
2749 /* This symbol was created as undefined from
2750 outside BFD. We assume that we should link
2751 in the object file. This is done for the -u
2752 option in the linker. */
2753 if (!(*info->callbacks
2754 ->add_archive_element) (info, abfd, name, subsbfd))
2755 return false;
2756 *pneeded = true;
2757 return true;
2759 /* Turn the current link symbol into a common
2760 symbol. It is already on the undefs list. */
2761 h->type = bfd_link_hash_common;
2762 h->u.c.p = bfd_hash_allocate (&info->hash->table,
2763 sizeof (struct bfd_link_hash_common_entry));
2764 if (h->u.c.p == NULL)
2765 return false;
2767 h->u.c.size = value;
2769 /* FIXME: This isn't quite right. The maximum
2770 alignment of a common symbol should be set by the
2771 architecture of the output file, not of the input
2772 file. */
2773 power = bfd_log2 (value);
2774 if (power > bfd_get_arch_info (abfd)->section_align_power)
2775 power = bfd_get_arch_info (abfd)->section_align_power;
2776 h->u.c.p->alignment_power = power;
2778 h->u.c.p->section = bfd_make_section_old_way (symbfd,
2779 "COMMON");
2781 else
2783 /* Adjust the size of the common symbol if
2784 necessary. */
2785 if (value > h->u.c.size)
2786 h->u.c.size = value;
2792 /* We do not need this object file. */
2793 return true;
2796 /* Check a single archive element to see if we need to include it in
2797 the link. *PNEEDED is set according to whether this element is
2798 needed in the link or not. This is called from
2799 _bfd_generic_link_add_archive_symbols. */
2801 static bool
2802 aout_link_check_archive_element (bfd *abfd,
2803 struct bfd_link_info *info,
2804 struct bfd_link_hash_entry *h ATTRIBUTE_UNUSED,
2805 const char *name ATTRIBUTE_UNUSED,
2806 bool *pneeded)
2808 bfd *oldbfd;
2809 bool needed;
2811 if (!aout_get_external_symbols (abfd))
2812 return false;
2814 oldbfd = abfd;
2815 if (!aout_link_check_ar_symbols (abfd, info, pneeded, &abfd))
2816 return false;
2818 needed = *pneeded;
2819 if (needed)
2821 /* Potentially, the add_archive_element hook may have set a
2822 substitute BFD for us. */
2823 if (abfd != oldbfd)
2825 if (!info->keep_memory
2826 && !aout_link_free_symbols (oldbfd))
2827 return false;
2828 if (!aout_get_external_symbols (abfd))
2829 return false;
2831 if (!aout_link_add_symbols (abfd, info))
2832 return false;
2835 if (!info->keep_memory || !needed)
2837 if (!aout_link_free_symbols (abfd))
2838 return false;
2841 return true;
2844 /* Add all symbols from an object file to the hash table. */
2846 static bool
2847 aout_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
2849 bool (*add_one_symbol)
2850 (struct bfd_link_info *, bfd *, const char *, flagword, asection *,
2851 bfd_vma, const char *, bool, bool, struct bfd_link_hash_entry **);
2852 struct external_nlist *syms;
2853 bfd_size_type sym_count;
2854 char *strings;
2855 bool copy;
2856 struct aout_link_hash_entry **sym_hash;
2857 struct external_nlist *p;
2858 struct external_nlist *pend;
2860 syms = obj_aout_external_syms (abfd);
2861 sym_count = obj_aout_external_sym_count (abfd);
2862 strings = obj_aout_external_strings (abfd);
2863 if (info->keep_memory)
2864 copy = false;
2865 else
2866 copy = true;
2868 if (aout_backend_info (abfd)->add_dynamic_symbols != NULL)
2870 if (! ((*aout_backend_info (abfd)->add_dynamic_symbols)
2871 (abfd, info, &syms, &sym_count, &strings)))
2872 return false;
2875 /* We keep a list of the linker hash table entries that correspond
2876 to particular symbols. We could just look them up in the hash
2877 table, but keeping the list is more efficient. Perhaps this
2878 should be conditional on info->keep_memory. */
2879 sym_hash = bfd_alloc (abfd,
2880 sym_count * sizeof (struct aout_link_hash_entry *));
2881 if (sym_hash == NULL && sym_count != 0)
2882 return false;
2883 obj_aout_sym_hashes (abfd) = sym_hash;
2885 add_one_symbol = aout_backend_info (abfd)->add_one_symbol;
2886 if (add_one_symbol == NULL)
2887 add_one_symbol = _bfd_generic_link_add_one_symbol;
2889 p = syms;
2890 pend = p + sym_count;
2891 for (; p < pend; p++, sym_hash++)
2893 int type;
2894 const char *name;
2895 bfd_vma value;
2896 asection *section;
2897 flagword flags;
2898 const char *string;
2900 *sym_hash = NULL;
2902 type = H_GET_8 (abfd, p->e_type);
2904 /* PR 19629: Corrupt binaries can contain illegal string offsets. */
2905 if (GET_WORD (abfd, p->e_strx) >= obj_aout_external_string_size (abfd))
2906 return false;
2907 name = strings + GET_WORD (abfd, p->e_strx);
2909 /* Ignore debugging symbols. */
2910 if (is_stab (type, name))
2911 continue;
2913 value = GET_WORD (abfd, p->e_value);
2914 flags = BSF_GLOBAL;
2915 string = NULL;
2916 switch (type)
2918 default:
2919 /* Shouldn't be any types not covered. */
2920 BFD_ASSERT (0);
2921 continue;
2923 case N_UNDF:
2924 case N_ABS:
2925 case N_TEXT:
2926 case N_DATA:
2927 case N_BSS:
2928 case N_REG:
2929 case N_FN:
2930 /* Ignore symbols that are not externally visible. */
2931 continue;
2933 case N_UNDF | N_EXT:
2934 if (value == 0)
2936 section = bfd_und_section_ptr;
2937 flags = 0;
2939 else
2940 section = bfd_com_section_ptr;
2941 break;
2942 case N_ABS | N_EXT:
2943 section = bfd_abs_section_ptr;
2944 break;
2945 case N_TEXT | N_EXT:
2946 section = obj_textsec (abfd);
2947 value -= bfd_section_vma (section);
2948 break;
2949 case N_DATA | N_EXT:
2950 /* Treat N_SETV symbols as N_DATA symbol; see comment in
2951 translate_from_native_sym_flags. */
2952 section = obj_datasec (abfd);
2953 value -= bfd_section_vma (section);
2954 break;
2955 case N_BSS | N_EXT:
2956 section = obj_bsssec (abfd);
2957 value -= bfd_section_vma (section);
2958 break;
2961 if (! ((*add_one_symbol)
2962 (info, abfd, name, flags, section, value, string, copy, false,
2963 (struct bfd_link_hash_entry **) sym_hash)))
2964 return false;
2966 /* Restrict the maximum alignment of a common symbol based on
2967 the architecture, since a.out has no way to represent
2968 alignment requirements of a section in a .o file. FIXME:
2969 This isn't quite right: it should use the architecture of the
2970 output file, not the input files. */
2971 if ((*sym_hash)->root.type == bfd_link_hash_common
2972 && ((*sym_hash)->root.u.c.p->alignment_power >
2973 bfd_get_arch_info (abfd)->section_align_power))
2974 (*sym_hash)->root.u.c.p->alignment_power =
2975 bfd_get_arch_info (abfd)->section_align_power;
2977 /* If this is a set symbol, and we are not building sets, then
2978 it is possible for the hash entry to not have been set. In
2979 such a case, treat the symbol as not globally defined. */
2980 if ((*sym_hash)->root.type == bfd_link_hash_new)
2982 BFD_ASSERT ((flags & BSF_CONSTRUCTOR) != 0);
2983 *sym_hash = NULL;
2987 return true;
2990 /* Look up an entry in an the header file hash table. */
2992 #define aout_link_includes_lookup(table, string, create, copy) \
2993 ((struct aout_link_includes_entry *) \
2994 bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
2996 /* The function to create a new entry in the header file hash table. */
2998 static struct bfd_hash_entry *
2999 aout_link_includes_newfunc (struct bfd_hash_entry *entry,
3000 struct bfd_hash_table *table,
3001 const char *string)
3003 struct aout_link_includes_entry * ret =
3004 (struct aout_link_includes_entry *) entry;
3006 /* Allocate the structure if it has not already been allocated by a
3007 subclass. */
3008 if (ret == NULL)
3009 ret = bfd_hash_allocate (table,
3010 sizeof (struct aout_link_includes_entry));
3011 if (ret == NULL)
3012 return NULL;
3014 /* Call the allocation method of the superclass. */
3015 ret = ((struct aout_link_includes_entry *)
3016 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
3017 if (ret)
3018 /* Set local fields. */
3019 ret->totals = NULL;
3021 return (struct bfd_hash_entry *) ret;
3024 /* Write out a symbol that was not associated with an a.out input
3025 object. */
3027 static bool
3028 aout_link_write_other_symbol (struct bfd_hash_entry *bh, void *data)
3030 struct aout_link_hash_entry *h = (struct aout_link_hash_entry *) bh;
3031 struct aout_final_link_info *flaginfo = (struct aout_final_link_info *) data;
3032 bfd *output_bfd;
3033 int type;
3034 bfd_vma val;
3035 struct external_nlist outsym;
3036 bfd_size_type indx;
3037 size_t amt;
3039 if (h->root.type == bfd_link_hash_warning)
3041 h = (struct aout_link_hash_entry *) h->root.u.i.link;
3042 if (h->root.type == bfd_link_hash_new)
3043 return true;
3046 output_bfd = flaginfo->output_bfd;
3048 if (aout_backend_info (output_bfd)->write_dynamic_symbol != NULL)
3050 if (! ((*aout_backend_info (output_bfd)->write_dynamic_symbol)
3051 (output_bfd, flaginfo->info, h)))
3053 /* FIXME: No way to handle errors. */
3054 abort ();
3058 if (h->written)
3059 return true;
3061 h->written = true;
3063 /* An indx of -2 means the symbol must be written. */
3064 if (h->indx != -2
3065 && (flaginfo->info->strip == strip_all
3066 || (flaginfo->info->strip == strip_some
3067 && bfd_hash_lookup (flaginfo->info->keep_hash, h->root.root.string,
3068 false, false) == NULL)))
3069 return true;
3071 switch (h->root.type)
3073 default:
3074 abort ();
3075 /* Avoid variable not initialized warnings. */
3076 return true;
3077 case bfd_link_hash_new:
3078 /* This can happen for set symbols when sets are not being
3079 built. */
3080 return true;
3081 case bfd_link_hash_undefined:
3082 type = N_UNDF | N_EXT;
3083 val = 0;
3084 break;
3085 case bfd_link_hash_defined:
3086 case bfd_link_hash_defweak:
3088 asection *sec;
3090 sec = h->root.u.def.section->output_section;
3091 BFD_ASSERT (bfd_is_abs_section (sec)
3092 || sec->owner == output_bfd);
3093 if (sec == obj_textsec (output_bfd))
3094 type = h->root.type == bfd_link_hash_defined ? N_TEXT : N_WEAKT;
3095 else if (sec == obj_datasec (output_bfd))
3096 type = h->root.type == bfd_link_hash_defined ? N_DATA : N_WEAKD;
3097 else if (sec == obj_bsssec (output_bfd))
3098 type = h->root.type == bfd_link_hash_defined ? N_BSS : N_WEAKB;
3099 else
3100 type = h->root.type == bfd_link_hash_defined ? N_ABS : N_WEAKA;
3101 type |= N_EXT;
3102 val = (h->root.u.def.value
3103 + sec->vma
3104 + h->root.u.def.section->output_offset);
3106 break;
3107 case bfd_link_hash_common:
3108 type = N_UNDF | N_EXT;
3109 val = h->root.u.c.size;
3110 break;
3111 case bfd_link_hash_undefweak:
3112 type = N_WEAKU;
3113 val = 0;
3114 /* Fall through. */
3115 case bfd_link_hash_indirect:
3116 case bfd_link_hash_warning:
3117 /* FIXME: Ignore these for now. The circumstances under which
3118 they should be written out are not clear to me. */
3119 return true;
3122 H_PUT_8 (output_bfd, type, outsym.e_type);
3123 H_PUT_8 (output_bfd, 0, outsym.e_ovly);
3124 indx = add_to_stringtab (output_bfd, flaginfo->strtab, h->root.root.string,
3125 false);
3126 if (indx == (bfd_size_type) -1)
3127 /* FIXME: No way to handle errors. */
3128 abort ();
3130 PUT_WORD (output_bfd, 0, outsym.e_desc);
3131 PUT_WORD (output_bfd, indx, outsym.e_strx);
3132 PUT_WORD (output_bfd, val, outsym.e_value);
3134 amt = EXTERNAL_NLIST_SIZE;
3135 if (bfd_seek (output_bfd, flaginfo->symoff, SEEK_SET) != 0
3136 || bfd_write (&outsym, amt, output_bfd) != amt)
3137 /* FIXME: No way to handle errors. */
3138 abort ();
3140 flaginfo->symoff += amt;
3141 h->indx = obj_aout_external_sym_count (output_bfd);
3142 ++obj_aout_external_sym_count (output_bfd);
3144 return true;
3147 /* Handle a link order which is supposed to generate a reloc. */
3149 static bool
3150 aout_link_reloc_link_order (struct aout_final_link_info *flaginfo,
3151 asection *o,
3152 struct bfd_link_order *p)
3154 struct bfd_link_order_reloc *pr;
3155 int r_index;
3156 int r_extern;
3157 reloc_howto_type *howto;
3158 file_ptr *reloff_ptr;
3159 struct reloc_std_external srel;
3160 void * rel_ptr;
3161 bfd_size_type rel_size;
3163 pr = p->u.reloc.p;
3165 if (p->type == bfd_section_reloc_link_order)
3167 r_extern = 0;
3168 if (bfd_is_abs_section (pr->u.section))
3169 r_index = N_ABS | N_EXT;
3170 else
3172 BFD_ASSERT (pr->u.section->owner == flaginfo->output_bfd);
3173 r_index = pr->u.section->target_index;
3176 else
3178 struct aout_link_hash_entry *h;
3180 BFD_ASSERT (p->type == bfd_symbol_reloc_link_order);
3181 r_extern = 1;
3182 h = ((struct aout_link_hash_entry *)
3183 bfd_wrapped_link_hash_lookup (flaginfo->output_bfd, flaginfo->info,
3184 pr->u.name, false, false, true));
3185 if (h != NULL
3186 && h->indx >= 0)
3187 r_index = h->indx;
3188 else if (h != NULL)
3190 /* We decided to strip this symbol, but it turns out that we
3191 can't. Note that we lose the other and desc information
3192 here. I don't think that will ever matter for a global
3193 symbol. */
3194 h->indx = -2;
3195 h->written = false;
3196 if (!aout_link_write_other_symbol (&h->root.root, flaginfo))
3197 return false;
3198 r_index = h->indx;
3200 else
3202 (*flaginfo->info->callbacks->unattached_reloc)
3203 (flaginfo->info, pr->u.name, NULL, NULL, (bfd_vma) 0);
3204 r_index = 0;
3208 howto = bfd_reloc_type_lookup (flaginfo->output_bfd, pr->reloc);
3209 if (howto == 0)
3211 bfd_set_error (bfd_error_bad_value);
3212 return false;
3215 if (o == obj_textsec (flaginfo->output_bfd))
3216 reloff_ptr = &flaginfo->treloff;
3217 else if (o == obj_datasec (flaginfo->output_bfd))
3218 reloff_ptr = &flaginfo->dreloff;
3219 else
3220 abort ();
3222 #ifdef MY_put_reloc
3223 MY_put_reloc(flaginfo->output_bfd, r_extern, r_index, p->offset, howto,
3224 &srel);
3225 #else
3227 int r_pcrel;
3228 int r_baserel;
3229 int r_jmptable;
3230 int r_relative;
3231 int r_length;
3233 fprintf (stderr, "TODO: line %d in bfd/pdp11.c\n", __LINE__);
3235 r_pcrel = howto->pc_relative;
3236 r_baserel = (howto->type & 8) != 0;
3237 r_jmptable = (howto->type & 16) != 0;
3238 r_relative = (howto->type & 32) != 0;
3239 r_length = bfd_log2 (bfd_get_reloc_size (howto));
3241 PUT_WORD (flaginfo->output_bfd, p->offset, srel.r_address);
3242 if (bfd_header_big_endian (flaginfo->output_bfd))
3244 srel.r_index[0] = r_index >> 16;
3245 srel.r_index[1] = r_index >> 8;
3246 srel.r_index[2] = r_index;
3247 srel.r_type[0] =
3248 ((r_extern ? RELOC_STD_BITS_EXTERN_BIG : 0)
3249 | (r_pcrel ? RELOC_STD_BITS_PCREL_BIG : 0)
3250 | (r_baserel ? RELOC_STD_BITS_BASEREL_BIG : 0)
3251 | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_BIG : 0)
3252 | (r_relative ? RELOC_STD_BITS_RELATIVE_BIG : 0)
3253 | (r_length << RELOC_STD_BITS_LENGTH_SH_BIG));
3255 else
3257 srel.r_index[2] = r_index >> 16;
3258 srel.r_index[1] = r_index >> 8;
3259 srel.r_index[0] = r_index;
3260 srel.r_type[0] =
3261 ((r_extern ? RELOC_STD_BITS_EXTERN_LITTLE : 0)
3262 | (r_pcrel ? RELOC_STD_BITS_PCREL_LITTLE : 0)
3263 | (r_baserel ? RELOC_STD_BITS_BASEREL_LITTLE : 0)
3264 | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_LITTLE : 0)
3265 | (r_relative ? RELOC_STD_BITS_RELATIVE_LITTLE : 0)
3266 | (r_length << RELOC_STD_BITS_LENGTH_SH_LITTLE));
3269 #endif
3270 rel_ptr = (void *) &srel;
3272 /* We have to write the addend into the object file, since
3273 standard a.out relocs are in place. It would be more
3274 reliable if we had the current contents of the file here,
3275 rather than assuming zeroes, but we can't read the file since
3276 it was opened using bfd_openw. */
3277 if (pr->addend != 0)
3279 bfd_size_type size;
3280 bfd_reloc_status_type r;
3281 bfd_byte *buf;
3282 bool ok;
3284 size = bfd_get_reloc_size (howto);
3285 buf = bfd_zmalloc (size);
3286 if (buf == NULL && size != 0)
3287 return false;
3288 r = MY_relocate_contents (howto, flaginfo->output_bfd,
3289 pr->addend, buf);
3290 switch (r)
3292 case bfd_reloc_ok:
3293 break;
3294 default:
3295 case bfd_reloc_outofrange:
3296 abort ();
3297 case bfd_reloc_overflow:
3298 (*flaginfo->info->callbacks->reloc_overflow)
3299 (flaginfo->info, NULL,
3300 (p->type == bfd_section_reloc_link_order
3301 ? bfd_section_name (pr->u.section)
3302 : pr->u.name),
3303 howto->name, pr->addend, NULL,
3304 (asection *) NULL, (bfd_vma) 0);
3305 break;
3307 ok = bfd_set_section_contents (flaginfo->output_bfd, o,
3308 (void *) buf,
3309 (file_ptr) p->offset,
3310 size);
3311 free (buf);
3312 if (! ok)
3313 return false;
3316 rel_size = obj_reloc_entry_size (flaginfo->output_bfd);
3317 if (bfd_seek (flaginfo->output_bfd, *reloff_ptr, SEEK_SET) != 0
3318 || bfd_write (rel_ptr, rel_size, flaginfo->output_bfd) != rel_size)
3319 return false;
3321 *reloff_ptr += rel_size;
3323 /* Assert that the relocs have not run into the symbols, and that n
3324 the text relocs have not run into the data relocs. */
3325 BFD_ASSERT (*reloff_ptr <= obj_sym_filepos (flaginfo->output_bfd)
3326 && (reloff_ptr != &flaginfo->treloff
3327 || (*reloff_ptr
3328 <= obj_datasec (flaginfo->output_bfd)->rel_filepos)));
3330 return true;
3333 /* Get the section corresponding to a reloc index. */
3335 static inline asection *
3336 aout_reloc_type_to_section (bfd *abfd, int type)
3338 switch (type)
3340 case RTEXT: return obj_textsec (abfd);
3341 case RDATA: return obj_datasec (abfd);
3342 case RBSS: return obj_bsssec (abfd);
3343 case RABS: return bfd_abs_section_ptr;
3344 case REXT: return bfd_und_section_ptr;
3345 default: abort ();
3349 static bool
3350 pdp11_aout_link_input_section (struct aout_final_link_info *flaginfo,
3351 bfd *input_bfd,
3352 asection *input_section,
3353 bfd_byte *relocs,
3354 bfd_size_type rel_size,
3355 bfd_byte *contents)
3357 bool (*check_dynamic_reloc)
3358 (struct bfd_link_info *, bfd *, asection *,
3359 struct aout_link_hash_entry *, void *, bfd_byte *, bool *, bfd_vma *);
3360 bfd *output_bfd;
3361 bool relocatable;
3362 struct external_nlist *syms;
3363 char *strings;
3364 struct aout_link_hash_entry **sym_hashes;
3365 int *symbol_map;
3366 bfd_byte *rel;
3367 bfd_byte *rel_end;
3369 output_bfd = flaginfo->output_bfd;
3370 check_dynamic_reloc = aout_backend_info (output_bfd)->check_dynamic_reloc;
3372 BFD_ASSERT (obj_reloc_entry_size (input_bfd) == RELOC_SIZE);
3373 BFD_ASSERT (input_bfd->xvec->header_byteorder
3374 == output_bfd->xvec->header_byteorder);
3376 relocatable = bfd_link_relocatable (flaginfo->info);
3377 syms = obj_aout_external_syms (input_bfd);
3378 strings = obj_aout_external_strings (input_bfd);
3379 sym_hashes = obj_aout_sym_hashes (input_bfd);
3380 symbol_map = flaginfo->symbol_map;
3382 rel = relocs;
3383 rel_end = rel + rel_size;
3384 for (; rel < rel_end; rel += RELOC_SIZE)
3386 bfd_vma r_addr;
3387 int r_index;
3388 int r_type;
3389 int r_pcrel;
3390 int r_extern;
3391 reloc_howto_type *howto;
3392 struct aout_link_hash_entry *h = NULL;
3393 bfd_vma relocation;
3394 bfd_reloc_status_type r;
3395 int reloc_entry;
3397 reloc_entry = GET_WORD (input_bfd, (void *) rel);
3398 if (reloc_entry == 0)
3399 continue;
3402 unsigned int howto_idx;
3404 r_index = (reloc_entry & RIDXMASK) >> 4;
3405 r_type = reloc_entry & RTYPE;
3406 r_pcrel = reloc_entry & RELFLG;
3407 r_addr = (char *) rel - (char *) relocs;
3409 r_extern = (r_type == REXT);
3411 howto_idx = r_pcrel;
3412 if (howto_idx < TABLE_SIZE (howto_table_pdp11))
3413 howto = howto_table_pdp11 + howto_idx;
3414 else
3416 _bfd_error_handler (_("%pB: unsupported relocation type"),
3417 input_bfd);
3418 bfd_set_error (bfd_error_bad_value);
3419 return false;
3423 if (relocatable)
3425 /* We are generating a relocatable output file, and must
3426 modify the reloc accordingly. */
3427 if (r_extern)
3429 /* If we know the symbol this relocation is against,
3430 convert it into a relocation against a section. This
3431 is what the native linker does. */
3432 h = sym_hashes[r_index];
3433 if (h != NULL
3434 && (h->root.type == bfd_link_hash_defined
3435 || h->root.type == bfd_link_hash_defweak))
3437 asection *output_section;
3439 /* Compute a new r_index. */
3440 output_section = h->root.u.def.section->output_section;
3441 if (output_section == obj_textsec (output_bfd))
3442 r_type = N_TEXT;
3443 else if (output_section == obj_datasec (output_bfd))
3444 r_type = N_DATA;
3445 else if (output_section == obj_bsssec (output_bfd))
3446 r_type = N_BSS;
3447 else
3448 r_type = N_ABS;
3450 /* Add the symbol value and the section VMA to the
3451 addend stored in the contents. */
3452 relocation = (h->root.u.def.value
3453 + output_section->vma
3454 + h->root.u.def.section->output_offset);
3456 else
3458 /* We must change r_index according to the symbol
3459 map. */
3460 r_index = symbol_map[r_index];
3462 if (r_index == -1)
3464 if (h != NULL)
3466 /* We decided to strip this symbol, but it
3467 turns out that we can't. Note that we
3468 lose the other and desc information here.
3469 I don't think that will ever matter for a
3470 global symbol. */
3471 if (h->indx < 0)
3473 h->indx = -2;
3474 h->written = false;
3475 if (!aout_link_write_other_symbol (&h->root.root,
3476 flaginfo))
3477 return false;
3479 r_index = h->indx;
3481 else
3483 const char *name;
3485 name = strings + GET_WORD (input_bfd,
3486 syms[r_index].e_strx);
3487 (*flaginfo->info->callbacks->unattached_reloc)
3488 (flaginfo->info, name, input_bfd, input_section,
3489 r_addr);
3490 r_index = 0;
3494 relocation = 0;
3497 /* Write out the new r_index value. */
3498 reloc_entry = GET_WORD (input_bfd, rel);
3499 reloc_entry &= RIDXMASK;
3500 reloc_entry |= r_index << 4;
3501 PUT_WORD (input_bfd, reloc_entry, rel);
3503 else
3505 asection *section;
3507 /* This is a relocation against a section. We must
3508 adjust by the amount that the section moved. */
3509 section = aout_reloc_type_to_section (input_bfd, r_type);
3510 relocation = (section->output_section->vma
3511 + section->output_offset
3512 - section->vma);
3515 /* Change the address of the relocation. */
3516 fprintf (stderr, "TODO: change the address of the relocation\n");
3518 /* Adjust a PC relative relocation by removing the reference
3519 to the original address in the section and including the
3520 reference to the new address. */
3521 if (r_pcrel)
3522 relocation -= (input_section->output_section->vma
3523 + input_section->output_offset
3524 - input_section->vma);
3526 #ifdef MY_relocatable_reloc
3527 MY_relocatable_reloc (howto, output_bfd, rel, relocation, r_addr);
3528 #endif
3530 if (relocation == 0)
3531 r = bfd_reloc_ok;
3532 else
3533 r = MY_relocate_contents (howto,
3534 input_bfd, relocation,
3535 contents + r_addr);
3537 else
3539 bool hundef;
3541 /* We are generating an executable, and must do a full
3542 relocation. */
3543 hundef = false;
3544 if (r_extern)
3546 h = sym_hashes[r_index];
3548 if (h != NULL
3549 && (h->root.type == bfd_link_hash_defined
3550 || h->root.type == bfd_link_hash_defweak))
3552 relocation = (h->root.u.def.value
3553 + h->root.u.def.section->output_section->vma
3554 + h->root.u.def.section->output_offset);
3556 else if (h != NULL
3557 && h->root.type == bfd_link_hash_undefweak)
3558 relocation = 0;
3559 else
3561 hundef = true;
3562 relocation = 0;
3565 else
3567 asection *section;
3569 section = aout_reloc_type_to_section (input_bfd, r_type);
3570 relocation = (section->output_section->vma
3571 + section->output_offset
3572 - section->vma);
3573 if (r_pcrel)
3574 relocation += input_section->vma;
3577 if (check_dynamic_reloc != NULL)
3579 bool skip;
3581 if (! ((*check_dynamic_reloc)
3582 (flaginfo->info, input_bfd, input_section, h,
3583 (void *) rel, contents, &skip, &relocation)))
3584 return false;
3585 if (skip)
3586 continue;
3589 /* Now warn if a global symbol is undefined. We could not
3590 do this earlier, because check_dynamic_reloc might want
3591 to skip this reloc. */
3592 if (hundef && ! bfd_link_pic (flaginfo->info))
3594 const char *name;
3596 if (h != NULL)
3597 name = h->root.root.string;
3598 else
3599 name = strings + GET_WORD (input_bfd, syms[r_index].e_strx);
3600 (*flaginfo->info->callbacks->undefined_symbol)
3601 (flaginfo->info, name, input_bfd, input_section,
3602 r_addr, true);
3605 r = MY_final_link_relocate (howto,
3606 input_bfd, input_section,
3607 contents, r_addr, relocation,
3608 (bfd_vma) 0);
3611 if (r != bfd_reloc_ok)
3613 switch (r)
3615 default:
3616 case bfd_reloc_outofrange:
3617 abort ();
3618 case bfd_reloc_overflow:
3620 const char *name;
3622 if (h != NULL)
3623 name = NULL;
3624 else if (r_extern)
3625 name = strings + GET_WORD (input_bfd,
3626 syms[r_index].e_strx);
3627 else
3629 asection *s;
3631 s = aout_reloc_type_to_section (input_bfd, r_type);
3632 name = bfd_section_name (s);
3634 (*flaginfo->info->callbacks->reloc_overflow)
3635 (flaginfo->info, (h ? &h->root : NULL), name, howto->name,
3636 (bfd_vma) 0, input_bfd, input_section, r_addr);
3638 break;
3643 return true;
3646 /* Link an a.out section into the output file. */
3648 static bool
3649 aout_link_input_section (struct aout_final_link_info *flaginfo,
3650 bfd *input_bfd,
3651 asection *input_section,
3652 file_ptr *reloff_ptr,
3653 bfd_size_type rel_size)
3655 bfd_size_type input_size;
3656 void * relocs;
3658 /* Get the section contents. */
3659 input_size = input_section->size;
3660 if (! bfd_get_section_contents (input_bfd, input_section,
3661 (void *) flaginfo->contents,
3662 (file_ptr) 0, input_size))
3663 return false;
3665 /* Read in the relocs if we haven't already done it. */
3666 if (aout_section_data (input_section) != NULL
3667 && aout_section_data (input_section)->relocs != NULL)
3668 relocs = aout_section_data (input_section)->relocs;
3669 else
3671 relocs = flaginfo->relocs;
3672 if (rel_size > 0)
3674 if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0
3675 || bfd_read (relocs, rel_size, input_bfd) != rel_size)
3676 return false;
3680 /* Relocate the section contents. */
3681 if (! pdp11_aout_link_input_section (flaginfo, input_bfd, input_section,
3682 (bfd_byte *) relocs,
3683 rel_size, flaginfo->contents))
3684 return false;
3686 /* Write out the section contents. */
3687 if (! bfd_set_section_contents (flaginfo->output_bfd,
3688 input_section->output_section,
3689 (void *) flaginfo->contents,
3690 (file_ptr) input_section->output_offset,
3691 input_size))
3692 return false;
3694 /* If we are producing relocatable output, the relocs were
3695 modified, and we now write them out. */
3696 if (bfd_link_relocatable (flaginfo->info) && rel_size > 0)
3698 if (bfd_seek (flaginfo->output_bfd, *reloff_ptr, SEEK_SET) != 0)
3699 return false;
3700 if (bfd_write (relocs, rel_size, flaginfo->output_bfd) != rel_size)
3701 return false;
3702 *reloff_ptr += rel_size;
3704 /* Assert that the relocs have not run into the symbols, and
3705 that if these are the text relocs they have not run into the
3706 data relocs. */
3707 BFD_ASSERT (*reloff_ptr <= obj_sym_filepos (flaginfo->output_bfd)
3708 && (reloff_ptr != &flaginfo->treloff
3709 || (*reloff_ptr
3710 <= obj_datasec (flaginfo->output_bfd)->rel_filepos)));
3713 return true;
3716 /* Link an a.out input BFD into the output file. */
3718 static bool
3719 aout_link_input_bfd (struct aout_final_link_info *flaginfo, bfd *input_bfd)
3721 BFD_ASSERT (bfd_get_format (input_bfd) == bfd_object);
3723 /* If this is a dynamic object, it may need special handling. */
3724 if ((input_bfd->flags & DYNAMIC) != 0
3725 && aout_backend_info (input_bfd)->link_dynamic_object != NULL)
3726 return ((*aout_backend_info (input_bfd)->link_dynamic_object)
3727 (flaginfo->info, input_bfd));
3729 /* Get the symbols. We probably have them already, unless
3730 flaginfo->info->keep_memory is FALSE. */
3731 if (! aout_get_external_symbols (input_bfd))
3732 return false;
3734 /* Write out the symbols and get a map of the new indices. The map
3735 is placed into flaginfo->symbol_map. */
3736 if (! aout_link_write_symbols (flaginfo, input_bfd))
3737 return false;
3739 /* Relocate and write out the sections. These functions use the
3740 symbol map created by aout_link_write_symbols. The linker_mark
3741 field will be set if these sections are to be included in the
3742 link, which will normally be the case. */
3743 if (obj_textsec (input_bfd)->linker_mark)
3745 if (! aout_link_input_section (flaginfo, input_bfd,
3746 obj_textsec (input_bfd),
3747 &flaginfo->treloff,
3748 exec_hdr (input_bfd)->a_trsize))
3749 return false;
3751 if (obj_datasec (input_bfd)->linker_mark)
3753 if (! aout_link_input_section (flaginfo, input_bfd,
3754 obj_datasec (input_bfd),
3755 &flaginfo->dreloff,
3756 exec_hdr (input_bfd)->a_drsize))
3757 return false;
3760 /* If we are not keeping memory, we don't need the symbols any
3761 longer. We still need them if we are keeping memory, because the
3762 strings in the hash table point into them. */
3763 if (! flaginfo->info->keep_memory)
3765 if (! aout_link_free_symbols (input_bfd))
3766 return false;
3769 return true;
3772 /* Do the final link step. This is called on the output BFD. The
3773 INFO structure should point to a list of BFDs linked through the
3774 link.next field which can be used to find each BFD which takes part
3775 in the output. Also, each section in ABFD should point to a list
3776 of bfd_link_order structures which list all the input sections for
3777 the output section. */
3779 bool
3780 NAME (aout, final_link) (bfd *abfd,
3781 struct bfd_link_info *info,
3782 void (*callback) (bfd *, file_ptr *, file_ptr *, file_ptr *))
3784 struct aout_final_link_info aout_info;
3785 bool includes_hash_initialized = false;
3786 bfd *sub;
3787 bfd_size_type trsize, drsize;
3788 bfd_size_type max_contents_size;
3789 bfd_size_type max_relocs_size;
3790 bfd_size_type max_sym_count;
3791 struct bfd_link_order *p;
3792 asection *o;
3793 bool have_link_order_relocs;
3795 if (bfd_link_pic (info))
3796 abfd->flags |= DYNAMIC;
3798 separate_i_d = info->separate_code;
3799 aout_info.info = info;
3800 aout_info.output_bfd = abfd;
3801 aout_info.contents = NULL;
3802 aout_info.relocs = NULL;
3803 aout_info.symbol_map = NULL;
3804 aout_info.output_syms = NULL;
3806 if (!bfd_hash_table_init_n (&aout_info.includes.root,
3807 aout_link_includes_newfunc,
3808 sizeof (struct aout_link_includes_entry),
3809 251))
3810 goto error_return;
3811 includes_hash_initialized = true;
3813 /* Figure out the largest section size. Also, if generating
3814 relocatable output, count the relocs. */
3815 trsize = 0;
3816 drsize = 0;
3817 max_contents_size = 0;
3818 max_relocs_size = 0;
3819 max_sym_count = 0;
3820 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3822 size_t sz;
3824 if (bfd_link_relocatable (info))
3826 if (bfd_get_flavour (sub) == bfd_target_aout_flavour)
3828 trsize += exec_hdr (sub)->a_trsize;
3829 drsize += exec_hdr (sub)->a_drsize;
3831 else
3833 /* FIXME: We need to identify the .text and .data sections
3834 and call get_reloc_upper_bound and canonicalize_reloc to
3835 work out the number of relocs needed, and then multiply
3836 by the reloc size. */
3837 _bfd_error_handler
3838 /* xgettext:c-format */
3839 (_("%pB: relocatable link from %s to %s not supported"),
3840 abfd, sub->xvec->name, abfd->xvec->name);
3841 bfd_set_error (bfd_error_invalid_operation);
3842 goto error_return;
3846 if (bfd_get_flavour (sub) == bfd_target_aout_flavour)
3848 sz = obj_textsec (sub)->size;
3849 if (sz > max_contents_size)
3850 max_contents_size = sz;
3851 sz = obj_datasec (sub)->size;
3852 if (sz > max_contents_size)
3853 max_contents_size = sz;
3855 sz = exec_hdr (sub)->a_trsize;
3856 if (sz > max_relocs_size)
3857 max_relocs_size = sz;
3858 sz = exec_hdr (sub)->a_drsize;
3859 if (sz > max_relocs_size)
3860 max_relocs_size = sz;
3862 sz = obj_aout_external_sym_count (sub);
3863 if (sz > max_sym_count)
3864 max_sym_count = sz;
3868 if (bfd_link_relocatable (info))
3870 if (obj_textsec (abfd) != NULL)
3871 trsize += (_bfd_count_link_order_relocs (obj_textsec (abfd)
3872 ->map_head.link_order)
3873 * obj_reloc_entry_size (abfd));
3874 if (obj_datasec (abfd) != NULL)
3875 drsize += (_bfd_count_link_order_relocs (obj_datasec (abfd)
3876 ->map_head.link_order)
3877 * obj_reloc_entry_size (abfd));
3880 exec_hdr (abfd)->a_trsize = trsize;
3881 exec_hdr (abfd)->a_drsize = drsize;
3882 exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd);
3884 /* Adjust the section sizes and vmas according to the magic number.
3885 This sets a_text, a_data and a_bss in the exec_hdr and sets the
3886 filepos for each section. */
3887 if (! NAME (aout, adjust_sizes_and_vmas) (abfd))
3888 goto error_return;
3890 /* The relocation and symbol file positions differ among a.out
3891 targets. We are passed a callback routine from the backend
3892 specific code to handle this.
3893 FIXME: At this point we do not know how much space the symbol
3894 table will require. This will not work for any (nonstandard)
3895 a.out target that needs to know the symbol table size before it
3896 can compute the relocation file positions. */
3897 (*callback) (abfd, &aout_info.treloff, &aout_info.dreloff,
3898 &aout_info.symoff);
3899 obj_textsec (abfd)->rel_filepos = aout_info.treloff;
3900 obj_datasec (abfd)->rel_filepos = aout_info.dreloff;
3901 obj_sym_filepos (abfd) = aout_info.symoff;
3903 /* We keep a count of the symbols as we output them. */
3904 obj_aout_external_sym_count (abfd) = 0;
3906 /* We accumulate the string table as we write out the symbols. */
3907 aout_info.strtab = _bfd_stringtab_init ();
3908 if (aout_info.strtab == NULL)
3909 goto error_return;
3911 /* Allocate buffers to hold section contents and relocs. */
3912 aout_info.contents = bfd_malloc (max_contents_size);
3913 aout_info.relocs = bfd_malloc (max_relocs_size);
3914 aout_info.symbol_map = bfd_malloc (max_sym_count * sizeof (int *));
3915 aout_info.output_syms = bfd_malloc ((max_sym_count + 1)
3916 * sizeof (struct external_nlist));
3917 if ((aout_info.contents == NULL && max_contents_size != 0)
3918 || (aout_info.relocs == NULL && max_relocs_size != 0)
3919 || (aout_info.symbol_map == NULL && max_sym_count != 0)
3920 || aout_info.output_syms == NULL)
3921 goto error_return;
3923 /* If we have a symbol named __DYNAMIC, force it out now. This is
3924 required by SunOS. Doing this here rather than in sunos.c is a
3925 hack, but it's easier than exporting everything which would be
3926 needed. */
3928 struct aout_link_hash_entry *h;
3930 h = aout_link_hash_lookup (aout_hash_table (info), "__DYNAMIC",
3931 false, false, false);
3932 if (h != NULL)
3933 aout_link_write_other_symbol (&h->root.root, &aout_info);
3936 /* The most time efficient way to do the link would be to read all
3937 the input object files into memory and then sort out the
3938 information into the output file. Unfortunately, that will
3939 probably use too much memory. Another method would be to step
3940 through everything that composes the text section and write it
3941 out, and then everything that composes the data section and write
3942 it out, and then write out the relocs, and then write out the
3943 symbols. Unfortunately, that requires reading stuff from each
3944 input file several times, and we will not be able to keep all the
3945 input files open simultaneously, and reopening them will be slow.
3947 What we do is basically process one input file at a time. We do
3948 everything we need to do with an input file once--copy over the
3949 section contents, handle the relocation information, and write
3950 out the symbols--and then we throw away the information we read
3951 from it. This approach requires a lot of lseeks of the output
3952 file, which is unfortunate but still faster than reopening a lot
3953 of files.
3955 We use the output_has_begun field of the input BFDs to see
3956 whether we have already handled it. */
3957 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3958 sub->output_has_begun = false;
3960 /* Mark all sections which are to be included in the link. This
3961 will normally be every section. We need to do this so that we
3962 can identify any sections which the linker has decided to not
3963 include. */
3964 for (o = abfd->sections; o != NULL; o = o->next)
3966 for (p = o->map_head.link_order; p != NULL; p = p->next)
3967 if (p->type == bfd_indirect_link_order)
3968 p->u.indirect.section->linker_mark = true;
3971 have_link_order_relocs = false;
3972 for (o = abfd->sections; o != NULL; o = o->next)
3974 for (p = o->map_head.link_order;
3975 p != NULL;
3976 p = p->next)
3978 if (p->type == bfd_indirect_link_order
3979 && (bfd_get_flavour (p->u.indirect.section->owner)
3980 == bfd_target_aout_flavour))
3982 bfd *input_bfd;
3984 input_bfd = p->u.indirect.section->owner;
3985 if (! input_bfd->output_has_begun)
3987 if (! aout_link_input_bfd (&aout_info, input_bfd))
3988 goto error_return;
3989 input_bfd->output_has_begun = true;
3992 else if (p->type == bfd_section_reloc_link_order
3993 || p->type == bfd_symbol_reloc_link_order)
3994 /* These are handled below. */
3995 have_link_order_relocs = true;
3996 else
3998 if (! _bfd_default_link_order (abfd, info, o, p))
3999 goto error_return;
4004 /* Write out any symbols that we have not already written out. */
4005 bfd_hash_traverse (&info->hash->table,
4006 aout_link_write_other_symbol,
4007 &aout_info);
4009 /* Now handle any relocs we were asked to create by the linker.
4010 These did not come from any input file. We must do these after
4011 we have written out all the symbols, so that we know the symbol
4012 indices to use. */
4013 if (have_link_order_relocs)
4015 for (o = abfd->sections; o != NULL; o = o->next)
4017 for (p = o->map_head.link_order;
4018 p != NULL;
4019 p = p->next)
4021 if (p->type == bfd_section_reloc_link_order
4022 || p->type == bfd_symbol_reloc_link_order)
4024 if (! aout_link_reloc_link_order (&aout_info, o, p))
4025 goto error_return;
4031 free (aout_info.contents);
4032 aout_info.contents = NULL;
4033 free (aout_info.relocs);
4034 aout_info.relocs = NULL;
4035 free (aout_info.symbol_map);
4036 aout_info.symbol_map = NULL;
4037 free (aout_info.output_syms);
4038 aout_info.output_syms = NULL;
4039 if (includes_hash_initialized)
4041 bfd_hash_table_free (&aout_info.includes.root);
4042 includes_hash_initialized = false;
4045 /* Finish up any dynamic linking we may be doing. */
4046 if (aout_backend_info (abfd)->finish_dynamic_link != NULL)
4048 if (! (*aout_backend_info (abfd)->finish_dynamic_link) (abfd, info))
4049 goto error_return;
4052 /* Update the header information. */
4053 abfd->symcount = obj_aout_external_sym_count (abfd);
4054 exec_hdr (abfd)->a_syms = abfd->symcount * EXTERNAL_NLIST_SIZE;
4055 obj_str_filepos (abfd) = obj_sym_filepos (abfd) + exec_hdr (abfd)->a_syms;
4056 obj_textsec (abfd)->reloc_count =
4057 exec_hdr (abfd)->a_trsize / obj_reloc_entry_size (abfd);
4058 obj_datasec (abfd)->reloc_count =
4059 exec_hdr (abfd)->a_drsize / obj_reloc_entry_size (abfd);
4061 /* Write out the string table, unless there are no symbols. */
4062 if (abfd->symcount > 0)
4064 if (bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET) != 0
4065 || ! emit_stringtab (abfd, aout_info.strtab))
4066 goto error_return;
4068 else if (obj_textsec (abfd)->reloc_count == 0
4069 && obj_datasec (abfd)->reloc_count == 0)
4071 /* The layout of a typical a.out file is header, text, data,
4072 relocs, symbols, string table. When there are no relocs,
4073 symbols or string table, the last thing in the file is data
4074 and a_data may be rounded up. However we may have a smaller
4075 sized .data section and thus not written final padding. The
4076 same thing can happen with text if there is no data. Write
4077 final padding here to extend the file. */
4078 file_ptr pos = 0;
4080 if (exec_hdr (abfd)->a_data > obj_datasec (abfd)->size)
4081 pos = obj_datasec (abfd)->filepos + exec_hdr (abfd)->a_data;
4082 else if (obj_datasec (abfd)->size == 0
4083 && exec_hdr (abfd)->a_text > obj_textsec (abfd)->size)
4084 pos = obj_textsec (abfd)->filepos + exec_hdr (abfd)->a_text;
4085 if (pos != 0)
4087 bfd_byte b = 0;
4089 if (bfd_seek (abfd, pos - 1, SEEK_SET) != 0
4090 || bfd_write (&b, 1, abfd) != 1)
4091 goto error_return;
4095 return true;
4097 error_return:
4098 free (aout_info.contents);
4099 free (aout_info.relocs);
4100 free (aout_info.symbol_map);
4101 free (aout_info.output_syms);
4102 if (includes_hash_initialized)
4103 bfd_hash_table_free (&aout_info.includes.root);
4104 return false;
4107 /* Adjust and write out the symbols for an a.out file. Set the new
4108 symbol indices into a symbol_map. */
4110 static bool
4111 aout_link_write_symbols (struct aout_final_link_info *flaginfo, bfd *input_bfd)
4113 bfd *output_bfd;
4114 bfd_size_type sym_count;
4115 char *strings;
4116 enum bfd_link_strip strip;
4117 enum bfd_link_discard discard;
4118 struct external_nlist *outsym;
4119 bfd_size_type strtab_index;
4120 struct external_nlist *sym;
4121 struct external_nlist *sym_end;
4122 struct aout_link_hash_entry **sym_hash;
4123 int *symbol_map;
4124 bool pass;
4125 bool skip_next;
4127 output_bfd = flaginfo->output_bfd;
4128 sym_count = obj_aout_external_sym_count (input_bfd);
4129 strings = obj_aout_external_strings (input_bfd);
4130 strip = flaginfo->info->strip;
4131 discard = flaginfo->info->discard;
4132 outsym = flaginfo->output_syms;
4134 /* First write out a symbol for this object file, unless we are
4135 discarding such symbols. */
4136 if (strip != strip_all
4137 && (strip != strip_some
4138 || bfd_hash_lookup (flaginfo->info->keep_hash,
4139 bfd_get_filename (input_bfd),
4140 false, false) != NULL)
4141 && discard != discard_all)
4143 H_PUT_8 (output_bfd, N_TEXT, outsym->e_type);
4144 H_PUT_8 (output_bfd, 0, outsym->e_ovly);
4145 H_PUT_16 (output_bfd, 0, outsym->e_desc);
4146 strtab_index = add_to_stringtab (output_bfd, flaginfo->strtab,
4147 bfd_get_filename (input_bfd), false);
4148 if (strtab_index == (bfd_size_type) -1)
4149 return false;
4150 PUT_WORD (output_bfd, strtab_index, outsym->e_strx);
4151 PUT_WORD (output_bfd,
4152 (bfd_section_vma (obj_textsec (input_bfd)->output_section)
4153 + obj_textsec (input_bfd)->output_offset),
4154 outsym->e_value);
4155 ++obj_aout_external_sym_count (output_bfd);
4156 ++outsym;
4159 pass = false;
4160 skip_next = false;
4161 sym = obj_aout_external_syms (input_bfd);
4162 sym_end = sym + sym_count;
4163 sym_hash = obj_aout_sym_hashes (input_bfd);
4164 symbol_map = flaginfo->symbol_map;
4165 memset (symbol_map, 0, (size_t) sym_count * sizeof *symbol_map);
4166 for (; sym < sym_end; sym++, sym_hash++, symbol_map++)
4168 const char *name;
4169 int type;
4170 struct aout_link_hash_entry *h;
4171 bool skip;
4172 asection *symsec;
4173 bfd_vma val = 0;
4174 bool copy;
4176 /* We set *symbol_map to 0 above for all symbols. If it has
4177 already been set to -1 for this symbol, it means that we are
4178 discarding it because it appears in a duplicate header file.
4179 See the N_BINCL code below. */
4180 if (*symbol_map == -1)
4181 continue;
4183 /* Initialize *symbol_map to -1, which means that the symbol was
4184 not copied into the output file. We will change it later if
4185 we do copy the symbol over. */
4186 *symbol_map = -1;
4188 type = H_GET_8 (input_bfd, sym->e_type);
4189 name = strings + GET_WORD (input_bfd, sym->e_strx);
4191 h = NULL;
4193 if (pass)
4195 /* Pass this symbol through. It is the target of an
4196 indirect or warning symbol. */
4197 val = GET_WORD (input_bfd, sym->e_value);
4198 pass = false;
4200 else if (skip_next)
4202 /* Skip this symbol, which is the target of an indirect
4203 symbol that we have changed to no longer be an indirect
4204 symbol. */
4205 skip_next = false;
4206 continue;
4208 else
4210 struct aout_link_hash_entry *hresolve;
4212 /* We have saved the hash table entry for this symbol, if
4213 there is one. Note that we could just look it up again
4214 in the hash table, provided we first check that it is an
4215 external symbol. */
4216 h = *sym_hash;
4218 /* Use the name from the hash table, in case the symbol was
4219 wrapped. */
4220 if (h != NULL)
4221 name = h->root.root.string;
4223 /* If this is an indirect or warning symbol, then change
4224 hresolve to the base symbol. We also change *sym_hash so
4225 that the relocation routines relocate against the real
4226 symbol. */
4227 hresolve = h;
4228 if (h != NULL
4229 && (h->root.type == bfd_link_hash_indirect
4230 || h->root.type == bfd_link_hash_warning))
4232 hresolve = (struct aout_link_hash_entry *) h->root.u.i.link;
4233 while (hresolve->root.type == bfd_link_hash_indirect
4234 || hresolve->root.type == bfd_link_hash_warning)
4235 hresolve = ((struct aout_link_hash_entry *)
4236 hresolve->root.u.i.link);
4237 *sym_hash = hresolve;
4240 /* If the symbol has already been written out, skip it. */
4241 if (h != NULL
4242 && h->root.type != bfd_link_hash_warning
4243 && h->written)
4245 if ((type & N_TYPE) == N_INDR
4246 || type == N_WARNING)
4247 skip_next = true;
4248 *symbol_map = h->indx;
4249 continue;
4252 /* See if we are stripping this symbol. */
4253 skip = false;
4254 switch (strip)
4256 case strip_none:
4257 break;
4258 case strip_debugger:
4259 if (is_stab (type, name))
4260 skip = true;
4261 break;
4262 case strip_some:
4263 if (bfd_hash_lookup (flaginfo->info->keep_hash, name,
4264 false, false) == NULL)
4265 skip = true;
4266 break;
4267 case strip_all:
4268 skip = true;
4269 break;
4271 if (skip)
4273 if (h != NULL)
4274 h->written = true;
4275 continue;
4278 /* Get the value of the symbol. */
4279 if (is_stab (type, name))
4281 switch (type)
4283 default:
4284 symsec = bfd_abs_section_ptr;
4285 break;
4286 case N_SO:
4287 case N_SOL:
4288 case N_FUN:
4289 case N_ENTRY:
4290 case N_SLINE:
4291 case N_FN:
4292 symsec = obj_textsec (input_bfd);
4293 break;
4294 case N_STSYM:
4295 case N_DSLINE:
4296 symsec = obj_datasec (input_bfd);
4297 break;
4298 case N_LCSYM:
4299 case N_BSLINE:
4300 symsec = obj_bsssec (input_bfd);
4301 break;
4303 val = GET_WORD (input_bfd, sym->e_value);
4305 else if ((type & N_TYPE) == N_TEXT
4306 || type == N_WEAKT)
4307 symsec = obj_textsec (input_bfd);
4308 else if ((type & N_TYPE) == N_DATA
4309 || type == N_WEAKD)
4310 symsec = obj_datasec (input_bfd);
4311 else if ((type & N_TYPE) == N_BSS
4312 || type == N_WEAKB)
4313 symsec = obj_bsssec (input_bfd);
4314 else if ((type & N_TYPE) == N_ABS
4315 || type == N_WEAKA)
4316 symsec = bfd_abs_section_ptr;
4317 else if (((type & N_TYPE) == N_INDR
4318 && (hresolve == NULL
4319 || (hresolve->root.type != bfd_link_hash_defined
4320 && hresolve->root.type != bfd_link_hash_defweak
4321 && hresolve->root.type != bfd_link_hash_common)))
4322 || type == N_WARNING)
4324 /* Pass the next symbol through unchanged. The
4325 condition above for indirect symbols is so that if
4326 the indirect symbol was defined, we output it with
4327 the correct definition so the debugger will
4328 understand it. */
4329 pass = true;
4330 val = GET_WORD (input_bfd, sym->e_value);
4331 symsec = NULL;
4333 else
4335 /* If we get here with an indirect symbol, it means that
4336 we are outputting it with a real definition. In such
4337 a case we do not want to output the next symbol,
4338 which is the target of the indirection. */
4339 if ((type & N_TYPE) == N_INDR)
4340 skip_next = true;
4342 symsec = NULL;
4344 /* We need to get the value from the hash table. We use
4345 hresolve so that if we have defined an indirect
4346 symbol we output the final definition. */
4347 if (h == NULL)
4349 switch (type & N_TYPE)
4351 case N_SETT:
4352 symsec = obj_textsec (input_bfd);
4353 break;
4354 case N_SETD:
4355 symsec = obj_datasec (input_bfd);
4356 break;
4357 case N_SETB:
4358 symsec = obj_bsssec (input_bfd);
4359 break;
4360 case N_SETA:
4361 symsec = bfd_abs_section_ptr;
4362 break;
4363 default:
4364 val = 0;
4365 break;
4368 else if (hresolve->root.type == bfd_link_hash_defined
4369 || hresolve->root.type == bfd_link_hash_defweak)
4371 asection *input_section;
4372 asection *output_section;
4374 /* This case usually means a common symbol which was
4375 turned into a defined symbol. */
4376 input_section = hresolve->root.u.def.section;
4377 output_section = input_section->output_section;
4378 BFD_ASSERT (bfd_is_abs_section (output_section)
4379 || output_section->owner == output_bfd);
4380 val = (hresolve->root.u.def.value
4381 + bfd_section_vma (output_section)
4382 + input_section->output_offset);
4384 /* Get the correct type based on the section. If
4385 this is a constructed set, force it to be
4386 globally visible. */
4387 if (type == N_SETT
4388 || type == N_SETD
4389 || type == N_SETB
4390 || type == N_SETA)
4391 type |= N_EXT;
4393 type &=~ N_TYPE;
4395 if (output_section == obj_textsec (output_bfd))
4396 type |= (hresolve->root.type == bfd_link_hash_defined
4397 ? N_TEXT
4398 : N_WEAKT);
4399 else if (output_section == obj_datasec (output_bfd))
4400 type |= (hresolve->root.type == bfd_link_hash_defined
4401 ? N_DATA
4402 : N_WEAKD);
4403 else if (output_section == obj_bsssec (output_bfd))
4404 type |= (hresolve->root.type == bfd_link_hash_defined
4405 ? N_BSS
4406 : N_WEAKB);
4407 else
4408 type |= (hresolve->root.type == bfd_link_hash_defined
4409 ? N_ABS
4410 : N_WEAKA);
4412 else if (hresolve->root.type == bfd_link_hash_common)
4413 val = hresolve->root.u.c.size;
4414 else if (hresolve->root.type == bfd_link_hash_undefweak)
4416 val = 0;
4417 type = N_WEAKU;
4419 else
4420 val = 0;
4422 if (symsec != NULL)
4423 val = (symsec->output_section->vma
4424 + symsec->output_offset
4425 + (GET_WORD (input_bfd, sym->e_value)
4426 - symsec->vma));
4428 /* If this is a global symbol set the written flag, and if
4429 it is a local symbol see if we should discard it. */
4430 if (h != NULL)
4432 h->written = true;
4433 h->indx = obj_aout_external_sym_count (output_bfd);
4435 else if ((type & N_TYPE) != N_SETT
4436 && (type & N_TYPE) != N_SETD
4437 && (type & N_TYPE) != N_SETB
4438 && (type & N_TYPE) != N_SETA)
4440 switch (discard)
4442 case discard_none:
4443 case discard_sec_merge:
4444 break;
4445 case discard_l:
4446 if (!is_stab (type, name)
4447 && bfd_is_local_label_name (input_bfd, name))
4448 skip = true;
4449 break;
4450 case discard_all:
4451 skip = true;
4452 break;
4454 if (skip)
4456 pass = false;
4457 continue;
4461 /* An N_BINCL symbol indicates the start of the stabs
4462 entries for a header file. We need to scan ahead to the
4463 next N_EINCL symbol, ignoring nesting, adding up all the
4464 characters in the symbol names, not including the file
4465 numbers in types (the first number after an open
4466 parenthesis). */
4467 if (type == N_BINCL)
4469 struct external_nlist *incl_sym;
4470 int nest;
4471 struct aout_link_includes_entry *incl_entry;
4472 struct aout_link_includes_totals *t;
4474 val = 0;
4475 nest = 0;
4476 for (incl_sym = sym + 1; incl_sym < sym_end; incl_sym++)
4478 int incl_type;
4480 incl_type = H_GET_8 (input_bfd, incl_sym->e_type);
4481 if (incl_type == N_EINCL)
4483 if (nest == 0)
4484 break;
4485 --nest;
4487 else if (incl_type == N_BINCL)
4488 ++nest;
4489 else if (nest == 0)
4491 const char *s;
4493 s = strings + GET_WORD (input_bfd, incl_sym->e_strx);
4494 for (; *s != '\0'; s++)
4496 val += *s;
4497 if (*s == '(')
4499 /* Skip the file number. */
4500 ++s;
4501 while (ISDIGIT (*s))
4502 ++s;
4503 --s;
4509 /* If we have already included a header file with the
4510 same value, then replace this one with an N_EXCL
4511 symbol. */
4512 copy = ! flaginfo->info->keep_memory;
4513 incl_entry = aout_link_includes_lookup (&flaginfo->includes,
4514 name, true, copy);
4515 if (incl_entry == NULL)
4516 return false;
4517 for (t = incl_entry->totals; t != NULL; t = t->next)
4518 if (t->total == val)
4519 break;
4520 if (t == NULL)
4522 /* This is the first time we have seen this header
4523 file with this set of stabs strings. */
4524 t = bfd_hash_allocate (&flaginfo->includes.root,
4525 sizeof *t);
4526 if (t == NULL)
4527 return false;
4528 t->total = val;
4529 t->next = incl_entry->totals;
4530 incl_entry->totals = t;
4532 else
4534 int *incl_map;
4536 /* This is a duplicate header file. We must change
4537 it to be an N_EXCL entry, and mark all the
4538 included symbols to prevent outputting them. */
4539 type = N_EXCL;
4541 nest = 0;
4542 for (incl_sym = sym + 1, incl_map = symbol_map + 1;
4543 incl_sym < sym_end;
4544 incl_sym++, incl_map++)
4546 int incl_type;
4548 incl_type = H_GET_8 (input_bfd, incl_sym->e_type);
4549 if (incl_type == N_EINCL)
4551 if (nest == 0)
4553 *incl_map = -1;
4554 break;
4556 --nest;
4558 else if (incl_type == N_BINCL)
4559 ++nest;
4560 else if (nest == 0)
4561 *incl_map = -1;
4567 /* Copy this symbol into the list of symbols we are going to
4568 write out. */
4569 H_PUT_8 (output_bfd, type, outsym->e_type);
4570 H_PUT_8 (output_bfd, H_GET_8 (input_bfd, sym->e_ovly), outsym->e_ovly);
4571 H_PUT_16 (output_bfd, H_GET_16 (input_bfd, sym->e_desc), outsym->e_desc);
4572 copy = false;
4573 if (! flaginfo->info->keep_memory)
4575 /* name points into a string table which we are going to
4576 free. If there is a hash table entry, use that string.
4577 Otherwise, copy name into memory. */
4578 if (h != NULL)
4579 name = h->root.root.string;
4580 else
4581 copy = true;
4583 strtab_index = add_to_stringtab (output_bfd, flaginfo->strtab,
4584 name, copy);
4585 if (strtab_index == (bfd_size_type) -1)
4586 return false;
4587 PUT_WORD (output_bfd, strtab_index, outsym->e_strx);
4588 PUT_WORD (output_bfd, val, outsym->e_value);
4589 *symbol_map = obj_aout_external_sym_count (output_bfd);
4590 ++obj_aout_external_sym_count (output_bfd);
4591 ++outsym;
4594 /* Write out the output symbols we have just constructed. */
4595 if (outsym > flaginfo->output_syms)
4597 bfd_size_type size;
4599 if (bfd_seek (output_bfd, flaginfo->symoff, SEEK_SET) != 0)
4600 return false;
4601 size = outsym - flaginfo->output_syms;
4602 size *= EXTERNAL_NLIST_SIZE;
4603 if (bfd_write (flaginfo->output_syms, size, output_bfd) != size)
4604 return false;
4605 flaginfo->symoff += size;
4608 return true;
4611 /* Write out a symbol that was not associated with an a.out input
4612 object. */
4614 static bfd_vma
4615 bfd_getp32 (const void *p)
4617 const bfd_byte *addr = p;
4618 unsigned long v;
4620 v = (unsigned long) addr[1] << 24;
4621 v |= (unsigned long) addr[0] << 16;
4622 v |= (unsigned long) addr[3] << 8;
4623 v |= (unsigned long) addr[2];
4624 return v;
4627 #define COERCE32(x) (((bfd_signed_vma) (x) ^ 0x80000000) - 0x80000000)
4629 static bfd_signed_vma
4630 bfd_getp_signed_32 (const void *p)
4632 const bfd_byte *addr = p;
4633 unsigned long v;
4635 v = (unsigned long) addr[1] << 24;
4636 v |= (unsigned long) addr[0] << 16;
4637 v |= (unsigned long) addr[3] << 8;
4638 v |= (unsigned long) addr[2];
4639 return COERCE32 (v);
4642 static void
4643 bfd_putp32 (bfd_vma data, void *p)
4645 bfd_byte *addr = p;
4647 addr[0] = (data >> 16) & 0xff;
4648 addr[1] = (data >> 24) & 0xff;
4649 addr[2] = (data >> 0) & 0xff;
4650 addr[3] = (data >> 8) & 0xff;
4653 const bfd_target MY (vec) =
4655 TARGETNAME, /* Name. */
4656 bfd_target_aout_flavour,
4657 BFD_ENDIAN_LITTLE, /* Target byte order (little). */
4658 BFD_ENDIAN_LITTLE, /* Target headers byte order (little). */
4659 (HAS_RELOC | EXEC_P | /* Object flags. */
4660 HAS_LINENO | HAS_DEBUG |
4661 HAS_SYMS | HAS_LOCALS | WP_TEXT),
4662 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA),
4663 MY_symbol_leading_char,
4664 AR_PAD_CHAR, /* AR_pad_char. */
4665 15, /* AR_max_namelen. */
4666 0, /* match priority. */
4667 TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
4668 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
4669 bfd_getp32, bfd_getp_signed_32, bfd_putp32,
4670 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* Data. */
4671 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
4672 bfd_getp32, bfd_getp_signed_32, bfd_putp32,
4673 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* Headers. */
4674 { /* bfd_check_format. */
4675 _bfd_dummy_target,
4676 MY_object_p,
4677 bfd_generic_archive_p,
4678 MY_core_file_p
4680 { /* bfd_set_format. */
4681 _bfd_bool_bfd_false_error,
4682 MY_mkobject,
4683 _bfd_generic_mkarchive,
4684 _bfd_bool_bfd_false_error
4686 { /* bfd_write_contents. */
4687 _bfd_bool_bfd_false_error,
4688 MY_write_object_contents,
4689 _bfd_write_archive_contents,
4690 _bfd_bool_bfd_false_error
4693 BFD_JUMP_TABLE_GENERIC (MY),
4694 BFD_JUMP_TABLE_COPY (MY),
4695 BFD_JUMP_TABLE_CORE (MY),
4696 BFD_JUMP_TABLE_ARCHIVE (MY),
4697 BFD_JUMP_TABLE_SYMBOLS (MY),
4698 BFD_JUMP_TABLE_RELOCS (MY),
4699 BFD_JUMP_TABLE_WRITE (MY),
4700 BFD_JUMP_TABLE_LINK (MY),
4701 BFD_JUMP_TABLE_DYNAMIC (MY),
4703 /* Alternative_target. */
4704 NULL,
4706 (void *) MY_backend_data