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.
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
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
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)
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). */
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)
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. */
161 #include "safe-ctype.h"
165 #include "aout/aout64.h"
166 #include "aout/stab_gnu.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. */
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]
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
220 #ifndef MY_relocate_contents
221 #define MY_relocate_contents _bfd_relocate_contents
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
234 struct aout_link_includes_totals
236 struct aout_link_includes_totals
*next
;
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
;
258 /* Reloc file positions. */
259 file_ptr treloff
, dreloff
;
260 /* File position of symbols. */
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. */
268 /* A buffer large enough to hold the relocs of any section. */
270 /* A buffer large enough to hold the symbol map of any input BFD. */
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
*);
300 NAME (aout
, reloc_type_lookup
) (bfd
* abfd ATTRIBUTE_UNUSED
,
301 bfd_reloc_code_real_type code
)
306 return &howto_table_pdp11
[0];
307 case BFD_RELOC_16_PCREL
:
308 return &howto_table_pdp11
[1];
310 return &howto_table_pdp11
[2];
317 NAME (aout
, reloc_name_lookup
) (bfd
*abfd ATTRIBUTE_UNUSED
,
323 i
< sizeof (howto_table_pdp11
) / sizeof (howto_table_pdp11
[0]);
325 if (howto_table_pdp11
[i
].name
!= NULL
326 && strcasecmp (howto_table_pdp11
[i
].name
, r_name
) == 0)
327 return &howto_table_pdp11
[i
];
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. */
338 is_stab (int type
, const char *name
)
340 if (type
== N_GSYM
|| type
== N_FUN
)
341 return strchr (name
, ':') != NULL
;
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
;
368 if (!NAME (aout
, swap_exec_header_out
) (abfd
, execp
, & exec_bytes
))
371 if (bfd_seek (abfd
, 0, SEEK_SET
) != 0)
374 if (bfd_write (&exec_bytes
, EXEC_BYTES_SIZE
, abfd
) != EXEC_BYTES_SIZE
)
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)
384 if (! NAME (aout
, write_syms
) (abfd
))
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
)))
401 /* Write an object file.
402 Section contents have already been written. We write the
403 file header, symbols, and relocation. */
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
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
)
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)
458 /* Swap the information in an internal exec header structure
459 "execp" into the buffer "bytes" ready for writing to disk. */
461 NAME (aout
, swap_exec_header_out
) (bfd
*abfd
,
462 struct internal_exec
*execp
,
463 struct external_exec
*bytes
)
465 const char *err
= NULL
;
467 #define MAXVAL(x) ((UINT64_C (1) << (8 * sizeof (x) - 1) << 1) - 1)
468 if ((val
= execp
->a_text
) > MAXVAL (bytes
->e_text
))
470 else if ((val
= execp
->a_data
) > MAXVAL (bytes
->e_data
))
472 else if ((val
= execp
->a_bss
) > MAXVAL (bytes
->e_bss
))
474 else if ((val
= execp
->a_syms
) > MAXVAL (bytes
->e_syms
))
476 else if ((val
= execp
->a_entry
) > MAXVAL (bytes
->e_entry
))
481 _bfd_error_handler (_("%pB: %#" PRIx64
" overflows header %s field"),
483 bfd_set_error (bfd_error_file_too_big
);
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
);
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
);
511 /* Make all the section for an a.out file. */
514 NAME (aout
, make_sections
) (bfd
*abfd
)
516 if (obj_textsec (abfd
) == NULL
&& bfd_make_section (abfd
, ".text") == NULL
)
518 if (obj_datasec (abfd
) == NULL
&& bfd_make_section (abfd
, ".data") == NULL
)
520 if (obj_bsssec (abfd
) == NULL
&& bfd_make_section (abfd
, ".bss") == NULL
)
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. */
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
;
538 size_t amt
= sizeof (*rawptr
);
540 rawptr
= bfd_zalloc (abfd
, amt
);
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. */
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
;
582 /* Should have been checked with N_BADMAG before this routine
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
))
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
;
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 */
712 bfd_release (abfd
, rawptr
);
713 abfd
->tdata
.aout_data
= oldrawptr
;
717 /* Initialize ABFD for use with a.out files. */
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
);
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
;
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. */
752 NAME (aout
, machine_type
) (enum bfd_architecture arch
,
753 unsigned long machine
,
756 enum machine_type arch_flags
;
758 arch_flags
= M_UNKNOWN
;
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
;
775 || machine
== bfd_mach_i386_i386
776 || machine
== bfd_mach_i386_i386_intel_syntax
)
781 if (machine
== 0) arch_flags
= M_ARM
;
789 case bfd_mach_mips3000
:
790 arch_flags
= M_MIPS1
;
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
;
799 arch_flags
= M_UNKNOWN
;
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;
815 /* TODO: arch_flags = M_PDP11; */
824 arch_flags
= M_UNKNOWN
;
827 if (arch_flags
!= M_UNKNOWN
)
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. */
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
))
845 if (arch
!= bfd_arch_unknown
)
849 NAME (aout
, machine_type
) (arch
, machine
, &unknown
);
854 obj_reloc_entry_size (abfd
) = RELOC_SIZE
;
856 return (*aout_backend_info(abfd
)->set_sizes
) (abfd
);
860 adjust_o_magic (bfd
*abfd
, struct internal_exec
*execp
)
862 file_ptr pos
= adata (abfd
).exec_bytes_size
;
865 asection
*text
= obj_textsec (abfd
);
866 asection
*data
= obj_datasec (abfd
);
867 asection
*bss
= obj_bsssec (abfd
);
871 if (!text
->user_set_vma
)
876 pos
+= execp
->a_text
;
877 vma
+= execp
->a_text
;
880 if (!data
->user_set_vma
)
888 execp
->a_text
+= pad
;
895 if (!bss
->user_set_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
;
911 execp
->a_data
= data
->size
+ pad
;
913 execp
->a_bss
= bss
->size
;
915 N_SET_MAGIC (execp
, OMAGIC
);
919 adjust_z_magic (bfd
*abfd
, struct internal_exec
*execp
)
921 bfd_size_type data_pad
, text_pad
;
923 const struct aout_backend_data
*abdp
;
924 /* TRUE if text includes exec header. */
926 asection
*text
= obj_textsec (abfd
);
927 asection
*data
= obj_datasec (abfd
);
928 asection
*bss
= obj_bsssec (abfd
);
930 abdp
= aout_backend_info (abfd
);
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
)
945 ? abdp
->default_text_vma
+ adata (abfd
).exec_bytes_size
946 : abdp
->default_text_vma
));
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
955 text_pad
= ((text
->filepos
- text
->vma
)
956 & (adata (abfd
).page_size
- 1));
958 text_pad
= (-text
->vma
959 & (adata (abfd
).page_size
- 1));
962 /* Find start of data. */
965 text_end
= text
->filepos
+ execp
->a_text
;
966 text_pad
+= BFD_ALIGN (text_end
, adata (abfd
).page_size
) - text_end
;
970 /* Note that if page_size == zmagic_disk_block_size, then
971 filepos == page_size, and this case is the same as the ztih
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
;
980 if (!data
->user_set_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. */
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
;
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
;
1019 execp
->a_bss
= bss
->size
;
1023 adjust_n_magic (bfd
*abfd
, struct internal_exec
*execp
)
1025 file_ptr pos
= adata (abfd
).exec_bytes_size
;
1028 asection
*text
= obj_textsec (abfd
);
1029 asection
*data
= obj_datasec (abfd
);
1030 asection
*bss
= obj_bsssec (abfd
);
1033 text
->filepos
= pos
;
1034 if (!text
->user_set_vma
)
1038 pos
+= execp
->a_text
;
1039 vma
+= execp
->a_text
;
1042 data
->filepos
= pos
;
1043 if (!data
->user_set_vma
)
1044 data
->vma
= BFD_ALIGN (vma
, adata (abfd
).segment_size
);
1047 /* Since BSS follows data immediately, see if it needs alignment. */
1049 pad
= align_power (vma
, bss
->alignment_power
) - vma
;
1050 execp
->a_data
= data
->size
+ pad
;
1051 pos
+= execp
->a_data
;
1054 if (!bss
->user_set_vma
)
1059 /* Fix up exec header. */
1060 execp
->a_bss
= bss
->size
;
1061 N_SET_MAGIC (execp
, NMAGIC
);
1065 adjust_i_magic (bfd
*abfd
, struct internal_exec
*execp
)
1067 file_ptr pos
= adata (abfd
).exec_bytes_size
;
1070 asection
*text
= obj_textsec (abfd
);
1071 asection
*data
= obj_datasec (abfd
);
1072 asection
*bss
= obj_bsssec (abfd
);
1075 text
->filepos
= pos
;
1076 if (!text
->user_set_vma
)
1080 pos
+= execp
->a_text
;
1083 data
->filepos
= pos
;
1084 if (!data
->user_set_vma
)
1088 /* Since BSS follows data immediately, see if it needs alignment. */
1090 pad
= align_power (vma
, bss
->alignment_power
) - vma
;
1091 execp
->a_data
= data
->size
+ pad
;
1092 pos
+= execp
->a_data
;
1095 if (!bss
->user_set_vma
)
1100 /* Fix up exec header. */
1101 execp
->a_bss
= bss
->size
;
1102 N_SET_MAGIC (execp
, IMAGIC
);
1106 NAME (aout
, adjust_sizes_and_vmas
) (bfd
*abfd
)
1108 struct internal_exec
*execp
= exec_hdr (abfd
);
1110 if (! NAME (aout
, make_sections
) (abfd
))
1113 if (adata (abfd
).magic
!= undecided_magic
)
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
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
1136 adata (abfd
).magic
= i_magic
;
1137 else if (abfd
->flags
& WP_TEXT
)
1138 adata (abfd
).magic
= n_magic
;
1140 adata (abfd
).magic
= o_magic
;
1142 #ifdef BFD_AOUT_DEBUG /* requires gcc2 */
1144 fprintf (stderr
, "%s text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x,%x>\n",
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;
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
);
1165 switch (adata (abfd
).magic
)
1168 adjust_o_magic (abfd
, execp
);
1171 adjust_z_magic (abfd
, execp
);
1174 adjust_n_magic (abfd
, execp
);
1177 adjust_i_magic (abfd
, execp
);
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
);
1195 /* Called by the BFD in response to a bfd_make_section request. */
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
);
1230 NAME (aout
, set_section_contents
) (bfd
*abfd
,
1232 const void * location
,
1234 bfd_size_type count
)
1236 if (! abfd
->output_has_begun
)
1238 if (! NAME (aout
, adjust_sizes_and_vmas
) (abfd
))
1242 if (section
== obj_bsssec (abfd
))
1244 bfd_set_error (bfd_error_no_contents
);
1248 if (section
!= obj_textsec (abfd
)
1249 && section
!= obj_datasec (abfd
))
1252 /* xgettext:c-format */
1253 (_("%pB: can not represent section `%pA' in a.out object file format"),
1255 bfd_set_error (bfd_error_nonrepresentable_section
);
1261 if (bfd_seek (abfd
, section
->filepos
+ offset
, SEEK_SET
) != 0
1262 || bfd_write (location
, count
, abfd
) != count
)
1269 /* Read the external symbols from an a.out file. */
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. */
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)
1291 syms
= _bfd_malloc_and_read (abfd
, amt
, amt
);
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
;
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
)
1311 stringsize
= H_GET_32 (abfd
, string_chars
);
1312 if (stringsize
== 0)
1314 else if (stringsize
+ 1 < BYTES_IN_LONG
+ 1
1315 || (size_t) stringsize
!= stringsize
)
1317 bfd_set_error (bfd_error_bad_value
);
1321 strings
= (char *) bfd_malloc (stringsize
+ 1);
1322 if (strings
== NULL
)
1325 if (stringsize
>= BYTES_IN_LONG
)
1327 amt
= stringsize
- BYTES_IN_LONG
;
1328 if (bfd_read (strings
+ BYTES_IN_LONG
, amt
, abfd
) != amt
)
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
;
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. */
1355 translate_from_native_sym_flags (bfd
*abfd
,
1356 aout_symbol_type
*cache_ptr
)
1360 if (is_stab (cache_ptr
->type
, cache_ptr
->symbol
.name
))
1364 /* This is a debugging symbol. */
1365 cache_ptr
->symbol
.flags
= BSF_DEBUGGING
;
1367 /* Work out the symbol section. */
1368 switch (cache_ptr
->type
)
1376 sec
= obj_textsec (abfd
);
1380 sec
= obj_datasec (abfd
);
1384 sec
= obj_bsssec (abfd
);
1387 sec
= bfd_abs_section_ptr
;
1391 cache_ptr
->symbol
.section
= sec
;
1392 cache_ptr
->symbol
.value
-= sec
->vma
;
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
;
1402 visible
= BSF_GLOBAL
;
1404 switch (cache_ptr
->type
)
1407 case N_ABS
: case N_ABS
| N_EXT
:
1408 cache_ptr
->symbol
.section
= bfd_abs_section_ptr
;
1409 cache_ptr
->symbol
.flags
= visible
;
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
;
1421 cache_ptr
->symbol
.flags
= 0;
1422 cache_ptr
->symbol
.section
= bfd_und_section_ptr
;
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
;
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
;
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
;
1448 /* Set the fields of SYM_POINTER according to CACHE_PTR. */
1451 translate_to_native_sym_flags (bfd
*abfd
,
1453 struct external_nlist
*sym_pointer
)
1455 bfd_vma value
= cache_ptr
->value
;
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
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
);
1470 /* This case occurs, e.g., for the *DEBUG* section of a COFF
1473 /* xgettext:c-format */
1474 (_("%pB: can not represent section for symbol `%s' in a.out object file format"),
1476 bfd_set_error (bfd_error_nonrepresentable_section
);
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
;
1501 /* xgettext:c-format */
1502 (_("%pB: can not represent section `%pA' in a.out object file format"),
1504 bfd_set_error (bfd_error_nonrepresentable_section
);
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
);
1521 /* Native-level interface to symbols. */
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
)
1531 new_symbol_type
->symbol
.the_bfd
= abfd
;
1533 return &new_symbol_type
->symbol
;
1536 /* Translate a set of external symbols into internal symbols. */
1539 NAME (aout
, translate_symbol_table
) (bfd
*abfd
,
1540 aout_symbol_type
*in
,
1541 struct external_nlist
*ext
,
1542 bfd_size_type count
,
1544 bfd_size_type strsize
,
1547 struct external_nlist
*ext_end
;
1549 ext_end
= ext
+ count
;
1550 for (; ext
< ext_end
; ext
++, in
++)
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
;
1570 (_("%pB: invalid string offset %" PRIu64
" >= %" PRIu64
),
1571 abfd
, (uint64_t) x
, (uint64_t) strsize
);
1572 bfd_set_error (bfd_error_bad_value
);
1576 ovly
= H_GET_8 (abfd
, ext
->e_ovly
);
1580 (_("%pB: symbol indicates overlay (not supported)"), abfd
);
1581 bfd_set_error (bfd_error_bad_value
);
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
);
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
))
1597 in
->symbol
.flags
|= BSF_DYNAMIC
;
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. */
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
)
1618 old_external_syms
= obj_aout_external_syms (abfd
);
1620 if (! aout_get_external_symbols (abfd
))
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)
1629 /* Convert from external symbol information to internal. */
1630 if (! (NAME (aout
, translate_symbol_table
)
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
),
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
;
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
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
1672 static inline bfd_size_type
1673 add_to_stringtab (bfd
*abfd
,
1674 struct bfd_strtab_hash
*tab
,
1679 bfd_size_type str_index
;
1681 /* An index of 0 always means the empty string. */
1682 if (str
== 0 || *str
== '\0')
1685 /* Don't hash if BFD_TRADITIONAL_FORMAT is set, because SunOS dbx
1686 doesn't understand a hashed string table. */
1688 if ((abfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
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
;
1701 /* Write out a strtab. ABFD is already at the right location in the
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
)
1714 return _bfd_stringtab_emit (abfd
, tab
);
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 ();
1728 for (count
= 0; count
< bfd_get_symcount (abfd
); count
++)
1730 asymbol
*g
= generic
[count
];
1732 struct external_nlist nsp
;
1734 indx
= add_to_stringtab (abfd
, strtab
, g
->name
, false);
1735 if (indx
== (bfd_size_type
) -1)
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
);
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
))
1755 if (bfd_write (&nsp
, EXTERNAL_NLIST_SIZE
, abfd
) != EXTERNAL_NLIST_SIZE
)
1758 /* NB: `KEEPIT' currently overlays `udata.p', so set this only
1759 here, at the end. */
1763 if (! emit_stringtab (abfd
, strtab
))
1766 _bfd_stringtab_free (strtab
);
1771 _bfd_stringtab_free (strtab
);
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
))
1785 for (symbase
= obj_aout_symbols (abfd
); counter
++ < bfd_get_symcount (abfd
);)
1786 *(location
++) = (asymbol
*)(symbase
++);
1788 return bfd_get_symcount (abfd
);
1792 /* Output extended relocation information to a file in target byte order. */
1795 pdp11_aout_swap_reloc_out (bfd
*abfd
, arelent
*g
, bfd_byte
*natptr
)
1801 asymbol
*sym
= *(g
->sym_ptr_ptr
);
1802 asection
*output_section
= sym
->section
->output_section
;
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
))
1811 else if (output_section
== obj_textsec (abfd
))
1813 else if (output_section
== obj_datasec (abfd
))
1815 else if (output_section
== obj_bsssec (abfd
))
1817 else if (bfd_is_und_section (output_section
))
1819 else if (bfd_is_com_section (output_section
))
1824 BFD_ASSERT (r_type
!= -1);
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) \
1848 /* Undefined symbol. */ \
1849 if (symbols != NULL && r_index < bfd_get_symcount (abfd)) \
1850 cache_ptr->sym_ptr_ptr = symbols + r_index; \
1852 cache_ptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr; \
1853 cache_ptr->addend = ad; \
1857 /* Defined, section relative. replace symbol with pointer to \
1858 symbol which points to section. */ \
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; \
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; \
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; \
1878 case N_ABS | N_EXT: \
1879 cache_ptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr; \
1880 cache_ptr->addend = ad; \
1886 pdp11_aout_swap_reloc_in (bfd
* abfd
,
1888 arelent
* cache_ptr
,
1889 bfd_size_type offset
,
1891 bfd_size_type symcount
)
1893 struct aoutdata
*su
= &(abfd
->tdata
.aout_data
->a
);
1894 unsigned int r_index
;
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
)
1909 r_index
= RINDEX (reloc_entry
);
1911 /* r_extern reflects whether the symbol the reloc is against is
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. */
1928 /* Read and swap the relocs for a section. */
1931 NAME (aout
, slurp_reloc_table
) (bfd
*abfd
, sec_ptr asect
, asymbol
**symbols
)
1934 bfd_size_type count
;
1935 bfd_size_type reloc_size
;
1937 arelent
*reloc_cache
;
1939 unsigned int counter
= 0;
1942 if (asect
->relocation
)
1945 if (asect
->flags
& SEC_CONSTRUCTOR
)
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
))
1956 bfd_set_error (bfd_error_invalid_operation
);
1960 if (bfd_seek (abfd
, asect
->rel_filepos
, SEEK_SET
) != 0)
1962 relocs
= _bfd_malloc_and_read (abfd
, reloc_size
, reloc_size
);
1963 if (relocs
== NULL
&& reloc_size
!= 0)
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
++)
1977 x
= GET_WORD (abfd
, (char *) relocs
+ each_size
* counter
);
1985 reloc_cache
= bfd_zmalloc (count
* sizeof (arelent
));
1986 if (reloc_cache
== NULL
&& count
!= 0)
1989 cache_ptr
= reloc_cache
;
1994 counter
++, rptr
+= RELOC_SIZE
, cache_ptr
++)
1996 while (GET_WORD (abfd
, (void *) rptr
) == 0)
1999 if ((char *) rptr
>= (char *) relocs
+ reloc_size
)
2003 pdp11_aout_swap_reloc_in (abfd
, rptr
, cache_ptr
,
2004 (bfd_size_type
) ((char *) rptr
- (char *) relocs
),
2006 (bfd_size_type
) bfd_get_symcount (abfd
));
2009 /* Just in case, if rptr >= relocs + reloc_size should happen
2011 BFD_ASSERT (counter
== count
);
2015 asect
->relocation
= reloc_cache
;
2016 asect
->reloc_count
= cache_ptr
- reloc_cache
;
2021 /* Write out a relocation section into an object file. */
2024 NAME (aout
, squirt_out_relocs
) (bfd
*abfd
, asection
*section
)
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
);
2036 generic
= section
->orelocation
;
2037 if (generic
!= NULL
)
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
);
2052 r
= native
+ (*generic
)->address
;
2053 pdp11_aout_swap_reloc_out (abfd
, *generic
, r
);
2059 if (bfd_write (native
, natsize
, abfd
) != natsize
)
2061 bfd_release (abfd
, native
);
2065 bfd_release (abfd
, native
);
2069 /* This is stupid. This function should be a boolean predicate. */
2072 NAME (aout
, canonicalize_reloc
) (bfd
*abfd
,
2077 arelent
*tblptr
= section
->relocation
;
2080 if (section
== obj_bsssec (abfd
))
2086 if (!(tblptr
|| NAME (aout
, slurp_reloc_table
)(abfd
, section
, symbols
)))
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
;
2101 tblptr
= section
->relocation
;
2103 for (count
= 0; count
++ < section
->reloc_count
;)
2104 *relptr
++ = tblptr
++;
2109 return section
->reloc_count
;
2113 NAME (aout
, get_reloc_upper_bound
) (bfd
*abfd
, sec_ptr asect
)
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
))
2127 bfd_set_error (bfd_error_invalid_operation
);
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
);
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
);
2146 return (count
+ 1) * sizeof (arelent
*);
2151 NAME (aout
, get_symtab_upper_bound
) (bfd
*abfd
)
2153 if (!NAME (aout
, slurp_symbol_table
) (abfd
))
2156 return (bfd_get_symcount (abfd
) + 1) * (sizeof (aout_symbol_type
*));
2160 NAME (aout
, get_lineno
) (bfd
* abfd ATTRIBUTE_UNUSED
,
2161 asymbol
* symbol ATTRIBUTE_UNUSED
)
2167 NAME (aout
, get_symbol_info
) (bfd
* abfd ATTRIBUTE_UNUSED
,
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
);
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
;
2193 NAME (aout
, print_symbol
) (bfd
* abfd
,
2196 bfd_print_symbol_type how
)
2198 FILE *file
= (FILE *) afile
;
2202 case bfd_print_symbol_name
:
2204 fprintf(file
,"%s", symbol
->name
);
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
));
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",
2220 (unsigned) (aout_symbol (symbol
)->desc
& 0xffff),
2221 (unsigned) (aout_symbol (symbol
)->other
& 0xff),
2222 (unsigned) (aout_symbol (symbol
)->type
& 0xff));
2224 fprintf(file
," %s", symbol
->name
);
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. */
2240 NAME (aout
, read_minisymbols
) (bfd
*abfd
,
2243 unsigned int *sizep
)
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
))
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
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. */
2273 NAME (aout
, minisymbol_to_symbol
) (bfd
*abfd
,
2275 const void * minisym
,
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
)
2287 (aout_symbol_type
*) sym
,
2288 (struct external_nlist
*) minisym
,
2290 obj_aout_external_strings (abfd
),
2291 obj_aout_external_string_size (abfd
),
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
2303 NAME (aout
, find_nearest_line
) (bfd
*abfd
,
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. */
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;
2321 size_t filelen
, funclen
;
2324 *filename_ptr
= bfd_get_filename (abfd
);
2325 *functionname_ptr
= NULL
;
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
);
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
2343 if (q
->symbol
.value
<= offset
2344 && ((q
->symbol
.value
> low_line_vma
2345 && (line_file_name
!= NULL
2347 || (q
->symbol
.value
> low_func_vma
2350 const char * symname
;
2352 symname
= q
->symbol
.name
;
2355 && strlen (symname
) > 2
2356 && strcmp (symname
+ strlen (symname
) - 2, ".o") == 0)
2358 if (q
->symbol
.value
> low_line_vma
)
2361 line_file_name
= NULL
;
2363 if (q
->symbol
.value
> low_func_vma
)
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
)
2378 line_file_name
= NULL
;
2380 if (q
->symbol
.value
> low_func_vma
)
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. */
2389 q
= (aout_symbol_type
*)(*p
);
2390 if (q
->type
!= (int) N_SO
)
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
)
2400 current_file_name
= q
->symbol
.name
;
2406 /* We'll keep this if it resolves nearer than the one we have
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
;
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
)
2436 main_file_name
= line_file_name
;
2438 if (main_file_name
== NULL
2439 || main_file_name
[0] == '/'
2440 || directory_name
== NULL
)
2443 filelen
= strlen (directory_name
) + strlen (main_file_name
);
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
;
2454 buf
= bfd_malloc ((bfd_size_type
) filelen
+ funclen
+ 3);
2455 adata (abfd
).line_buf
= buf
;
2460 if (main_file_name
!= NULL
)
2462 if (main_file_name
[0] == '/' || directory_name
== NULL
)
2463 *filename_ptr
= main_file_name
;
2467 /* PR binutils/20891: In a corrupt input file both
2468 main_file_name and directory_name can be empty... */
2469 * filename_ptr
= NULL
;
2472 snprintf (buf
, filelen
+ 1, "%s%s", directory_name
,
2474 *filename_ptr
= buf
;
2482 const char *function
= func
->name
;
2487 /* PR binutils/20892: In a corrupt input file func can be empty. */
2488 * functionname_ptr
= NULL
;
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
);
2498 buf
[0] = bfd_get_symbol_leading_char (abfd
);
2499 strcpy (buf
+ 1, function
);
2502 /* Have to remove : stuff. */
2503 colon
= strchr (buf
, ':');
2506 *functionname_ptr
= buf
;
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. */
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
);
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
,
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
2553 ret
= bfd_hash_allocate (table
, sizeof (* ret
));
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
);
2562 /* Set local fields. */
2563 ret
->written
= false;
2567 return (struct bfd_hash_entry
*) ret
;
2570 /* Initialize an a.out link hash table. */
2573 NAME (aout
, link_hash_table_init
) (struct aout_link_hash_table
*table
,
2575 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
2576 struct bfd_hash_table
*,
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
);
2594 if (! NAME (aout
, link_hash_table_init
) (ret
, abfd
,
2595 NAME (aout
, link_hash_newfunc
),
2596 sizeof (struct aout_link_hash_entry
)))
2604 /* Free up the internal symbols read from an a.out file. */
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
;
2623 /* Given an a.out BFD, add symbols to the global hash table as
2627 NAME (aout
, link_add_symbols
) (bfd
*abfd
, struct bfd_link_info
*info
)
2629 switch (bfd_get_format (abfd
))
2632 return aout_link_add_object_symbols (abfd
, info
);
2634 return _bfd_generic_link_add_archive_symbols
2635 (abfd
, info
, aout_link_check_archive_element
);
2637 bfd_set_error (bfd_error_wrong_format
);
2642 /* Add symbols from an a.out object file. */
2645 aout_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
2647 if (! aout_get_external_symbols (abfd
))
2649 if (! aout_link_add_symbols (abfd
, info
))
2651 if (! info
->keep_memory
)
2653 if (! aout_link_free_symbols (abfd
))
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). */
2667 aout_link_check_ar_symbols (bfd
*abfd
,
2668 struct bfd_link_info
*info
,
2672 struct external_nlist
*p
;
2673 struct external_nlist
*pend
;
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
2691 if ((type
& N_EXT
) == 0
2692 || is_stab(type
, name
)
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. */
2701 || (h
->type
!= bfd_link_hash_undefined
2702 && h
->type
!= bfd_link_hash_common
))
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
2716 and this object file from the archive includes
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
))
2732 if (type
== (N_UNDF
| N_EXT
))
2736 value
= GET_WORD (abfd
, p
->e_value
);
2739 /* This symbol is common in the object from the archive
2741 if (h
->type
== bfd_link_hash_undefined
)
2746 symbfd
= h
->u
.undef
.abfd
;
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
))
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
)
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
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
,
2783 /* Adjust the size of the common symbol if
2785 if (value
> h
->u
.c
.size
)
2786 h
->u
.c
.size
= value
;
2792 /* We do not need this object file. */
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. */
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
,
2811 if (!aout_get_external_symbols (abfd
))
2815 if (!aout_link_check_ar_symbols (abfd
, info
, pneeded
, &abfd
))
2821 /* Potentially, the add_archive_element hook may have set a
2822 substitute BFD for us. */
2825 if (!info
->keep_memory
2826 && !aout_link_free_symbols (oldbfd
))
2828 if (!aout_get_external_symbols (abfd
))
2831 if (!aout_link_add_symbols (abfd
, info
))
2835 if (!info
->keep_memory
|| !needed
)
2837 if (!aout_link_free_symbols (abfd
))
2844 /* Add all symbols from an object file to the hash table. */
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
;
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
)
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
)))
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)
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
;
2890 pend
= p
+ sym_count
;
2891 for (; p
< pend
; p
++, sym_hash
++)
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
))
2907 name
= strings
+ GET_WORD (abfd
, p
->e_strx
);
2909 /* Ignore debugging symbols. */
2910 if (is_stab (type
, name
))
2913 value
= GET_WORD (abfd
, p
->e_value
);
2919 /* Shouldn't be any types not covered. */
2930 /* Ignore symbols that are not externally visible. */
2933 case N_UNDF
| N_EXT
:
2936 section
= bfd_und_section_ptr
;
2940 section
= bfd_com_section_ptr
;
2943 section
= bfd_abs_section_ptr
;
2945 case N_TEXT
| N_EXT
:
2946 section
= obj_textsec (abfd
);
2947 value
-= bfd_section_vma (section
);
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
);
2956 section
= obj_bsssec (abfd
);
2957 value
-= bfd_section_vma (section
);
2961 if (! ((*add_one_symbol
)
2962 (info
, abfd
, name
, flags
, section
, value
, string
, copy
, false,
2963 (struct bfd_link_hash_entry
**) sym_hash
)))
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);
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
,
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
3009 ret
= bfd_hash_allocate (table
,
3010 sizeof (struct aout_link_includes_entry
));
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
));
3018 /* Set local fields. */
3021 return (struct bfd_hash_entry
*) ret
;
3024 /* Write out a symbol that was not associated with an a.out input
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
;
3035 struct external_nlist outsym
;
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
)
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. */
3063 /* An indx of -2 means the symbol must be written. */
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
)))
3071 switch (h
->root
.type
)
3075 /* Avoid variable not initialized warnings. */
3077 case bfd_link_hash_new
:
3078 /* This can happen for set symbols when sets are not being
3081 case bfd_link_hash_undefined
:
3082 type
= N_UNDF
| N_EXT
;
3085 case bfd_link_hash_defined
:
3086 case bfd_link_hash_defweak
:
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
;
3100 type
= h
->root
.type
== bfd_link_hash_defined
? N_ABS
: N_WEAKA
;
3102 val
= (h
->root
.u
.def
.value
3104 + h
->root
.u
.def
.section
->output_offset
);
3107 case bfd_link_hash_common
:
3108 type
= N_UNDF
| N_EXT
;
3109 val
= h
->root
.u
.c
.size
;
3111 case bfd_link_hash_undefweak
:
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. */
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
,
3126 if (indx
== (bfd_size_type
) -1)
3127 /* FIXME: No way to handle errors. */
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. */
3140 flaginfo
->symoff
+= amt
;
3141 h
->indx
= obj_aout_external_sym_count (output_bfd
);
3142 ++obj_aout_external_sym_count (output_bfd
);
3147 /* Handle a link order which is supposed to generate a reloc. */
3150 aout_link_reloc_link_order (struct aout_final_link_info
*flaginfo
,
3152 struct bfd_link_order
*p
)
3154 struct bfd_link_order_reloc
*pr
;
3157 reloc_howto_type
*howto
;
3158 file_ptr
*reloff_ptr
;
3159 struct reloc_std_external srel
;
3161 bfd_size_type rel_size
;
3165 if (p
->type
== bfd_section_reloc_link_order
)
3168 if (bfd_is_abs_section (pr
->u
.section
))
3169 r_index
= N_ABS
| N_EXT
;
3172 BFD_ASSERT (pr
->u
.section
->owner
== flaginfo
->output_bfd
);
3173 r_index
= pr
->u
.section
->target_index
;
3178 struct aout_link_hash_entry
*h
;
3180 BFD_ASSERT (p
->type
== bfd_symbol_reloc_link_order
);
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));
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
3196 if (!aout_link_write_other_symbol (&h
->root
.root
, flaginfo
))
3202 (*flaginfo
->info
->callbacks
->unattached_reloc
)
3203 (flaginfo
->info
, pr
->u
.name
, NULL
, NULL
, (bfd_vma
) 0);
3208 howto
= bfd_reloc_type_lookup (flaginfo
->output_bfd
, pr
->reloc
);
3211 bfd_set_error (bfd_error_bad_value
);
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
;
3223 MY_put_reloc(flaginfo
->output_bfd
, r_extern
, r_index
, p
->offset
, howto
,
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
;
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
));
3257 srel
.r_index
[2] = r_index
>> 16;
3258 srel
.r_index
[1] = r_index
>> 8;
3259 srel
.r_index
[0] = r_index
;
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
));
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)
3280 bfd_reloc_status_type r
;
3284 size
= bfd_get_reloc_size (howto
);
3285 buf
= bfd_zmalloc (size
);
3286 if (buf
== NULL
&& size
!= 0)
3288 r
= MY_relocate_contents (howto
, flaginfo
->output_bfd
,
3295 case bfd_reloc_outofrange
:
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
)
3303 howto
->name
, pr
->addend
, NULL
,
3304 (asection
*) NULL
, (bfd_vma
) 0);
3307 ok
= bfd_set_section_contents (flaginfo
->output_bfd
, o
,
3309 (file_ptr
) p
->offset
,
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
)
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
3328 <= obj_datasec (flaginfo
->output_bfd
)->rel_filepos
)));
3333 /* Get the section corresponding to a reloc index. */
3335 static inline asection
*
3336 aout_reloc_type_to_section (bfd
*abfd
, int 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
;
3350 pdp11_aout_link_input_section (struct aout_final_link_info
*flaginfo
,
3352 asection
*input_section
,
3354 bfd_size_type rel_size
,
3357 bool (*check_dynamic_reloc
)
3358 (struct bfd_link_info
*, bfd
*, asection
*,
3359 struct aout_link_hash_entry
*, void *, bfd_byte
*, bool *, bfd_vma
*);
3362 struct external_nlist
*syms
;
3364 struct aout_link_hash_entry
**sym_hashes
;
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
;
3383 rel_end
= rel
+ rel_size
;
3384 for (; rel
< rel_end
; rel
+= RELOC_SIZE
)
3391 reloc_howto_type
*howto
;
3392 struct aout_link_hash_entry
*h
= NULL
;
3394 bfd_reloc_status_type r
;
3397 reloc_entry
= GET_WORD (input_bfd
, (void *) rel
);
3398 if (reloc_entry
== 0)
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
;
3416 _bfd_error_handler (_("%pB: unsupported relocation type"),
3418 bfd_set_error (bfd_error_bad_value
);
3425 /* We are generating a relocatable output file, and must
3426 modify the reloc accordingly. */
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
];
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
))
3443 else if (output_section
== obj_datasec (output_bfd
))
3445 else if (output_section
== obj_bsssec (output_bfd
))
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
);
3458 /* We must change r_index according to the symbol
3460 r_index
= symbol_map
[r_index
];
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
3475 if (!aout_link_write_other_symbol (&h
->root
.root
,
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
,
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
);
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
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. */
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
);
3530 if (relocation
== 0)
3533 r
= MY_relocate_contents (howto
,
3534 input_bfd
, relocation
,
3541 /* We are generating an executable, and must do a full
3546 h
= sym_hashes
[r_index
];
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
);
3557 && h
->root
.type
== bfd_link_hash_undefweak
)
3569 section
= aout_reloc_type_to_section (input_bfd
, r_type
);
3570 relocation
= (section
->output_section
->vma
3571 + section
->output_offset
3574 relocation
+= input_section
->vma
;
3577 if (check_dynamic_reloc
!= NULL
)
3581 if (! ((*check_dynamic_reloc
)
3582 (flaginfo
->info
, input_bfd
, input_section
, h
,
3583 (void *) rel
, contents
, &skip
, &relocation
)))
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
))
3597 name
= h
->root
.root
.string
;
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
,
3605 r
= MY_final_link_relocate (howto
,
3606 input_bfd
, input_section
,
3607 contents
, r_addr
, relocation
,
3611 if (r
!= bfd_reloc_ok
)
3616 case bfd_reloc_outofrange
:
3618 case bfd_reloc_overflow
:
3625 name
= strings
+ GET_WORD (input_bfd
,
3626 syms
[r_index
].e_strx
);
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
);
3646 /* Link an a.out section into the output file. */
3649 aout_link_input_section (struct aout_final_link_info
*flaginfo
,
3651 asection
*input_section
,
3652 file_ptr
*reloff_ptr
,
3653 bfd_size_type rel_size
)
3655 bfd_size_type input_size
;
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
))
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
;
3671 relocs
= flaginfo
->relocs
;
3674 if (bfd_seek (input_bfd
, input_section
->rel_filepos
, SEEK_SET
) != 0
3675 || bfd_read (relocs
, rel_size
, input_bfd
) != rel_size
)
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
))
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
,
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)
3700 if (bfd_write (relocs
, rel_size
, flaginfo
->output_bfd
) != rel_size
)
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
3707 BFD_ASSERT (*reloff_ptr
<= obj_sym_filepos (flaginfo
->output_bfd
)
3708 && (reloff_ptr
!= &flaginfo
->treloff
3710 <= obj_datasec (flaginfo
->output_bfd
)->rel_filepos
)));
3716 /* Link an a.out input BFD into the output file. */
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
))
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
))
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
),
3748 exec_hdr (input_bfd
)->a_trsize
))
3751 if (obj_datasec (input_bfd
)->linker_mark
)
3753 if (! aout_link_input_section (flaginfo
, input_bfd
,
3754 obj_datasec (input_bfd
),
3756 exec_hdr (input_bfd
)->a_drsize
))
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
))
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. */
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;
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
;
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
),
3811 includes_hash_initialized
= true;
3813 /* Figure out the largest section size. Also, if generating
3814 relocatable output, count the relocs. */
3817 max_contents_size
= 0;
3818 max_relocs_size
= 0;
3820 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
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
;
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. */
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
);
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
)
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
))
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
,
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
)
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
)
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
3928 struct aout_link_hash_entry
*h
;
3930 h
= aout_link_hash_lookup (aout_hash_table (info
), "__DYNAMIC",
3931 false, false, false);
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
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
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
;
3978 if (p
->type
== bfd_indirect_link_order
3979 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
3980 == bfd_target_aout_flavour
))
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
))
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;
3998 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
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
,
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
4013 if (have_link_order_relocs
)
4015 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4017 for (p
= o
->map_head
.link_order
;
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
))
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
))
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
))
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. */
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
;
4089 if (bfd_seek (abfd
, pos
- 1, SEEK_SET
) != 0
4090 || bfd_write (&b
, 1, abfd
) != 1)
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
);
4107 /* Adjust and write out the symbols for an a.out file. Set the new
4108 symbol indices into a symbol_map. */
4111 aout_link_write_symbols (struct aout_final_link_info
*flaginfo
, bfd
*input_bfd
)
4114 bfd_size_type sym_count
;
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
;
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)
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
),
4155 ++obj_aout_external_sym_count (output_bfd
);
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
++)
4170 struct aout_link_hash_entry
*h
;
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)
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. */
4188 type
= H_GET_8 (input_bfd
, sym
->e_type
);
4189 name
= strings
+ GET_WORD (input_bfd
, sym
->e_strx
);
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
);
4202 /* Skip this symbol, which is the target of an indirect
4203 symbol that we have changed to no longer be an indirect
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
4218 /* Use the name from the hash table, in case the symbol was
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
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. */
4242 && h
->root
.type
!= bfd_link_hash_warning
4245 if ((type
& N_TYPE
) == N_INDR
4246 || type
== N_WARNING
)
4248 *symbol_map
= h
->indx
;
4252 /* See if we are stripping this symbol. */
4258 case strip_debugger
:
4259 if (is_stab (type
, name
))
4263 if (bfd_hash_lookup (flaginfo
->info
->keep_hash
, name
,
4264 false, false) == NULL
)
4278 /* Get the value of the symbol. */
4279 if (is_stab (type
, name
))
4284 symsec
= bfd_abs_section_ptr
;
4292 symsec
= obj_textsec (input_bfd
);
4296 symsec
= obj_datasec (input_bfd
);
4300 symsec
= obj_bsssec (input_bfd
);
4303 val
= GET_WORD (input_bfd
, sym
->e_value
);
4305 else if ((type
& N_TYPE
) == N_TEXT
4307 symsec
= obj_textsec (input_bfd
);
4308 else if ((type
& N_TYPE
) == N_DATA
4310 symsec
= obj_datasec (input_bfd
);
4311 else if ((type
& N_TYPE
) == N_BSS
4313 symsec
= obj_bsssec (input_bfd
);
4314 else if ((type
& N_TYPE
) == N_ABS
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
4330 val
= GET_WORD (input_bfd
, sym
->e_value
);
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
)
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. */
4349 switch (type
& N_TYPE
)
4352 symsec
= obj_textsec (input_bfd
);
4355 symsec
= obj_datasec (input_bfd
);
4358 symsec
= obj_bsssec (input_bfd
);
4361 symsec
= bfd_abs_section_ptr
;
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. */
4395 if (output_section
== obj_textsec (output_bfd
))
4396 type
|= (hresolve
->root
.type
== bfd_link_hash_defined
4399 else if (output_section
== obj_datasec (output_bfd
))
4400 type
|= (hresolve
->root
.type
== bfd_link_hash_defined
4403 else if (output_section
== obj_bsssec (output_bfd
))
4404 type
|= (hresolve
->root
.type
== bfd_link_hash_defined
4408 type
|= (hresolve
->root
.type
== bfd_link_hash_defined
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
)
4423 val
= (symsec
->output_section
->vma
4424 + symsec
->output_offset
4425 + (GET_WORD (input_bfd
, sym
->e_value
)
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. */
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
)
4443 case discard_sec_merge
:
4446 if (!is_stab (type
, name
)
4447 && bfd_is_local_label_name (input_bfd
, name
))
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
4467 if (type
== N_BINCL
)
4469 struct external_nlist
*incl_sym
;
4471 struct aout_link_includes_entry
*incl_entry
;
4472 struct aout_link_includes_totals
*t
;
4476 for (incl_sym
= sym
+ 1; incl_sym
< sym_end
; incl_sym
++)
4480 incl_type
= H_GET_8 (input_bfd
, incl_sym
->e_type
);
4481 if (incl_type
== N_EINCL
)
4487 else if (incl_type
== N_BINCL
)
4493 s
= strings
+ GET_WORD (input_bfd
, incl_sym
->e_strx
);
4494 for (; *s
!= '\0'; s
++)
4499 /* Skip the file number. */
4501 while (ISDIGIT (*s
))
4509 /* If we have already included a header file with the
4510 same value, then replace this one with an N_EXCL
4512 copy
= ! flaginfo
->info
->keep_memory
;
4513 incl_entry
= aout_link_includes_lookup (&flaginfo
->includes
,
4515 if (incl_entry
== NULL
)
4517 for (t
= incl_entry
->totals
; t
!= NULL
; t
= t
->next
)
4518 if (t
->total
== val
)
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
,
4529 t
->next
= incl_entry
->totals
;
4530 incl_entry
->totals
= t
;
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. */
4542 for (incl_sym
= sym
+ 1, incl_map
= symbol_map
+ 1;
4544 incl_sym
++, incl_map
++)
4548 incl_type
= H_GET_8 (input_bfd
, incl_sym
->e_type
);
4549 if (incl_type
== N_EINCL
)
4558 else if (incl_type
== N_BINCL
)
4567 /* Copy this symbol into the list of symbols we are going to
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
);
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. */
4579 name
= h
->root
.root
.string
;
4583 strtab_index
= add_to_stringtab (output_bfd
, flaginfo
->strtab
,
4585 if (strtab_index
== (bfd_size_type
) -1)
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
);
4594 /* Write out the output symbols we have just constructed. */
4595 if (outsym
> flaginfo
->output_syms
)
4599 if (bfd_seek (output_bfd
, flaginfo
->symoff
, SEEK_SET
) != 0)
4601 size
= outsym
- flaginfo
->output_syms
;
4602 size
*= EXTERNAL_NLIST_SIZE
;
4603 if (bfd_write (flaginfo
->output_syms
, size
, output_bfd
) != size
)
4605 flaginfo
->symoff
+= size
;
4611 /* Write out a symbol that was not associated with an a.out input
4615 bfd_getp32 (const void *p
)
4617 const bfd_byte
*addr
= p
;
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];
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
;
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
);
4643 bfd_putp32 (bfd_vma data
, void *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. */
4677 bfd_generic_archive_p
,
4680 { /* bfd_set_format. */
4681 _bfd_bool_bfd_false_error
,
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. */
4706 (void *) MY_backend_data