1 /* Support for the generic parts of most COFF variants, for BFD.
2 Copyright 1990, 91, 92, 93, 94, 95, 96, 97, 98, 1999
3 Free Software Foundation, Inc.
4 Written by Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 Most of this hacked by Steve Chamberlain,
31 BFD supports a number of different flavours of coff format.
32 The major differences between formats are the sizes and
33 alignments of fields in structures on disk, and the occasional
36 Coff in all its varieties is implemented with a few common
37 files and a number of implementation specific files. For
38 example, The 88k bcs coff format is implemented in the file
39 @file{coff-m88k.c}. This file @code{#include}s
40 @file{coff/m88k.h} which defines the external structure of the
41 coff format for the 88k, and @file{coff/internal.h} which
42 defines the internal structure. @file{coff-m88k.c} also
43 defines the relocations used by the 88k format
46 The Intel i960 processor version of coff is implemented in
47 @file{coff-i960.c}. This file has the same structure as
48 @file{coff-m88k.c}, except that it includes @file{coff/i960.h}
49 rather than @file{coff-m88k.h}.
52 Porting to a new version of coff
54 The recommended method is to select from the existing
55 implementations the version of coff which is most like the one
56 you want to use. For example, we'll say that i386 coff is
57 the one you select, and that your coff flavour is called foo.
58 Copy @file{i386coff.c} to @file{foocoff.c}, copy
59 @file{../include/coff/i386.h} to @file{../include/coff/foo.h},
60 and add the lines to @file{targets.c} and @file{Makefile.in}
61 so that your new back end is used. Alter the shapes of the
62 structures in @file{../include/coff/foo.h} so that they match
63 what you need. You will probably also have to add
64 @code{#ifdef}s to the code in @file{coff/internal.h} and
65 @file{coffcode.h} if your version of coff is too wild.
67 You can verify that your new BFD backend works quite simply by
68 building @file{objdump} from the @file{binutils} directory,
69 and making sure that its version of what's going on and your
70 host system's idea (assuming it has the pretty standard coff
71 dump utility, usually called @code{att-dump} or just
72 @code{dump}) are the same. Then clean up your code, and send
73 what you've done to Cygnus. Then your stuff will be in the
74 next release, and you won't have to keep integrating it.
77 How the coff backend works
82 The Coff backend is split into generic routines that are
83 applicable to any Coff target and routines that are specific
84 to a particular target. The target-specific routines are
85 further split into ones which are basically the same for all
86 Coff targets except that they use the external symbol format
87 or use different values for certain constants.
89 The generic routines are in @file{coffgen.c}. These routines
90 work for any Coff target. They use some hooks into the target
91 specific code; the hooks are in a @code{bfd_coff_backend_data}
92 structure, one of which exists for each target.
94 The essentially similar target-specific routines are in
95 @file{coffcode.h}. This header file includes executable C code.
96 The various Coff targets first include the appropriate Coff
97 header file, make any special defines that are needed, and
98 then include @file{coffcode.h}.
100 Some of the Coff targets then also have additional routines in
101 the target source file itself.
103 For example, @file{coff-i960.c} includes
104 @file{coff/internal.h} and @file{coff/i960.h}. It then
105 defines a few constants, such as @code{I960}, and includes
106 @file{coffcode.h}. Since the i960 has complex relocation
107 types, @file{coff-i960.c} also includes some code to
108 manipulate the i960 relocs. This code is not in
109 @file{coffcode.h} because it would not be used by any other
115 Each flavour of coff supported in BFD has its own header file
116 describing the external layout of the structures. There is also
117 an internal description of the coff layout, in
118 @file{coff/internal.h}. A major function of the
119 coff backend is swapping the bytes and twiddling the bits to
120 translate the external form of the structures into the normal
121 internal form. This is all performed in the
122 @code{bfd_swap}_@i{thing}_@i{direction} routines. Some
123 elements are different sizes between different versions of
124 coff; it is the duty of the coff version specific include file
125 to override the definitions of various packing routines in
126 @file{coffcode.h}. E.g., the size of line number entry in coff is
127 sometimes 16 bits, and sometimes 32 bits. @code{#define}ing
128 @code{PUT_LNSZ_LNNO} and @code{GET_LNSZ_LNNO} will select the
129 correct one. No doubt, some day someone will find a version of
130 coff which has a varying field size not catered to at the
131 moment. To port BFD, that person will have to add more @code{#defines}.
132 Three of the bit twiddling routines are exported to
133 @code{gdb}; @code{coff_swap_aux_in}, @code{coff_swap_sym_in}
134 and @code{coff_swap_lineno_in}. @code{GDB} reads the symbol
135 table on its own, but uses BFD to fix things up. More of the
136 bit twiddlers are exported for @code{gas};
137 @code{coff_swap_aux_out}, @code{coff_swap_sym_out},
138 @code{coff_swap_lineno_out}, @code{coff_swap_reloc_out},
139 @code{coff_swap_filehdr_out}, @code{coff_swap_aouthdr_out},
140 @code{coff_swap_scnhdr_out}. @code{Gas} currently keeps track
141 of all the symbol table and reloc drudgery itself, thereby
142 saving the internal BFD overhead, but uses BFD to swap things
143 on the way out, making cross ports much safer. Doing so also
144 allows BFD (and thus the linker) to use the same header files
145 as @code{gas}, which makes one avenue to disaster disappear.
150 The simple canonical form for symbols used by BFD is not rich
151 enough to keep all the information available in a coff symbol
152 table. The back end gets around this problem by keeping the original
153 symbol table around, "behind the scenes".
155 When a symbol table is requested (through a call to
156 @code{bfd_canonicalize_symtab}), a request gets through to
157 @code{coff_get_normalized_symtab}. This reads the symbol table from
158 the coff file and swaps all the structures inside into the
159 internal form. It also fixes up all the pointers in the table
160 (represented in the file by offsets from the first symbol in
161 the table) into physical pointers to elements in the new
162 internal table. This involves some work since the meanings of
163 fields change depending upon context: a field that is a
164 pointer to another structure in the symbol table at one moment
165 may be the size in bytes of a structure at the next. Another
166 pass is made over the table. All symbols which mark file names
167 (<<C_FILE>> symbols) are modified so that the internal
168 string points to the value in the auxent (the real filename)
169 rather than the normal text associated with the symbol
172 At this time the symbol names are moved around. Coff stores
173 all symbols less than nine characters long physically
174 within the symbol table; longer strings are kept at the end of
175 the file in the string table. This pass moves all strings
176 into memory and replaces them with pointers to the strings.
179 The symbol table is massaged once again, this time to create
180 the canonical table used by the BFD application. Each symbol
181 is inspected in turn, and a decision made (using the
182 @code{sclass} field) about the various flags to set in the
183 @code{asymbol}. @xref{Symbols}. The generated canonical table
184 shares strings with the hidden internal symbol table.
186 Any linenumbers are read from the coff file too, and attached
187 to the symbols which own the functions the linenumbers belong to.
192 Writing a symbol to a coff file which didn't come from a coff
193 file will lose any debugging information. The @code{asymbol}
194 structure remembers the BFD from which the symbol was taken, and on
195 output the back end makes sure that the same destination target as
196 source target is present.
198 When the symbols have come from a coff file then all the
199 debugging information is preserved.
201 Symbol tables are provided for writing to the back end in a
202 vector of pointers to pointers. This allows applications like
203 the linker to accumulate and output large symbol tables
204 without having to do too much byte copying.
206 This function runs through the provided symbol table and
207 patches each symbol marked as a file place holder
208 (@code{C_FILE}) to point to the next file place holder in the
209 list. It also marks each @code{offset} field in the list with
210 the offset from the first symbol of the current symbol.
212 Another function of this procedure is to turn the canonical
213 value form of BFD into the form used by coff. Internally, BFD
214 expects symbol values to be offsets from a section base; so a
215 symbol physically at 0x120, but in a section starting at
216 0x100, would have the value 0x20. Coff expects symbols to
217 contain their final value, so symbols have their values
218 changed at this point to reflect their sum with their owning
219 section. This transformation uses the
220 <<output_section>> field of the @code{asymbol}'s
221 @code{asection} @xref{Sections}.
223 o <<coff_mangle_symbols>>
225 This routine runs though the provided symbol table and uses
226 the offsets generated by the previous pass and the pointers
227 generated when the symbol table was read in to create the
228 structured hierachy required by coff. It changes each pointer
229 to a symbol into the index into the symbol table of the asymbol.
231 o <<coff_write_symbols>>
233 This routine runs through the symbol table and patches up the
234 symbols from their internal form into the coff way, calls the
235 bit twiddlers, and writes out the table to the file.
244 The hidden information for an <<asymbol>> is described in a
245 <<combined_entry_type>>:
249 .typedef struct coff_ptr_struct
252 . {* Remembers the offset from the first symbol in the file for
253 . this symbol. Generated by coff_renumber_symbols. *}
254 .unsigned int offset;
256 . {* Should the value of this symbol be renumbered. Used for
257 . XCOFF C_BSTAT symbols. Set by coff_slurp_symbol_table. *}
258 .unsigned int fix_value : 1;
260 . {* Should the tag field of this symbol be renumbered.
261 . Created by coff_pointerize_aux. *}
262 .unsigned int fix_tag : 1;
264 . {* Should the endidx field of this symbol be renumbered.
265 . Created by coff_pointerize_aux. *}
266 .unsigned int fix_end : 1;
268 . {* Should the x_csect.x_scnlen field be renumbered.
269 . Created by coff_pointerize_aux. *}
270 .unsigned int fix_scnlen : 1;
272 . {* Fix up an XCOFF C_BINCL/C_EINCL symbol. The value is the
273 . index into the line number entries. Set by
274 . coff_slurp_symbol_table. *}
275 .unsigned int fix_line : 1;
277 . {* The container for the symbol structure as read and translated
281 . union internal_auxent auxent;
282 . struct internal_syment syment;
284 .} combined_entry_type;
287 .{* Each canonical asymbol really looks like this: *}
289 .typedef struct coff_symbol_struct
291 . {* The actual symbol which the rest of BFD works with *}
294 . {* A pointer to the hidden information for this symbol *}
295 .combined_entry_type *native;
297 . {* A pointer to the linenumber information for this symbol *}
298 .struct lineno_cache_entry *lineno;
300 . {* Have the line numbers been relocated yet ? *}
301 .boolean done_lineno;
310 #include "coffswap.h"
313 #define STRING_SIZE_SIZE (4)
315 static long sec_to_styp_flags
PARAMS ((const char *, flagword
));
316 static flagword styp_to_sec_flags
PARAMS ((bfd
*, PTR
, const char *));
317 static boolean coff_bad_format_hook
PARAMS ((bfd
*, PTR
));
318 static void coff_set_custom_section_alignment
319 PARAMS ((bfd
*, asection
*, const struct coff_section_alignment_entry
*,
321 static boolean coff_new_section_hook
PARAMS ((bfd
*, asection
*));
322 static boolean coff_set_arch_mach_hook
PARAMS ((bfd
*, PTR
));
323 static boolean coff_write_relocs
PARAMS ((bfd
*, int));
324 static boolean coff_set_flags
325 PARAMS ((bfd
*, unsigned int *, unsigned short *));
326 static boolean coff_set_arch_mach
327 PARAMS ((bfd
*, enum bfd_architecture
, unsigned long));
328 static boolean coff_compute_section_file_positions
PARAMS ((bfd
*));
329 static boolean coff_write_object_contents
PARAMS ((bfd
*));
330 static boolean coff_set_section_contents
331 PARAMS ((bfd
*, asection
*, PTR
, file_ptr
, bfd_size_type
));
332 static PTR buy_and_read
PARAMS ((bfd
*, file_ptr
, int, size_t));
333 static boolean coff_slurp_line_table
PARAMS ((bfd
*, asection
*));
334 static boolean coff_slurp_symbol_table
PARAMS ((bfd
*));
335 static enum coff_symbol_classification coff_classify_symbol
336 PARAMS ((bfd
*, struct internal_syment
*));
337 static boolean coff_slurp_reloc_table
PARAMS ((bfd
*, asection
*, asymbol
**));
338 static long coff_canonicalize_reloc
339 PARAMS ((bfd
*, asection
*, arelent
**, asymbol
**));
340 #ifndef coff_mkobject_hook
341 static PTR coff_mkobject_hook
PARAMS ((bfd
*, PTR
, PTR
));
344 /* void warning(); */
347 * Return a word with STYP_* (scnhdr.s_flags) flags set to represent the
348 * incoming SEC_* flags. The inverse of this function is styp_to_sec_flags().
349 * NOTE: If you add to/change this routine, you should mirror the changes
350 * in styp_to_sec_flags().
353 sec_to_styp_flags (sec_name
, sec_flags
)
354 CONST
char *sec_name
;
359 if (!strcmp (sec_name
, _TEXT
))
361 styp_flags
= STYP_TEXT
;
363 else if (!strcmp (sec_name
, _DATA
))
365 styp_flags
= STYP_DATA
;
367 else if (!strcmp (sec_name
, _BSS
))
369 styp_flags
= STYP_BSS
;
372 else if (!strcmp (sec_name
, _COMMENT
))
374 styp_flags
= STYP_INFO
;
375 #endif /* _COMMENT */
378 else if (!strcmp (sec_name
, _LIB
))
380 styp_flags
= STYP_LIB
;
384 else if (!strcmp (sec_name
, _LIT
))
386 styp_flags
= STYP_LIT
;
389 else if (!strcmp (sec_name
, ".debug"))
392 styp_flags
= STYP_DEBUG
;
394 styp_flags
= STYP_INFO
;
397 else if (!strncmp (sec_name
, ".stab", 5))
399 styp_flags
= STYP_INFO
;
402 else if (!strcmp (sec_name
, ".edata"))
404 styp_flags
= STYP_DATA
;
408 else if (!strcmp (sec_name
, _PAD
))
410 styp_flags
= STYP_PAD
;
412 else if (!strcmp (sec_name
, _LOADER
))
414 styp_flags
= STYP_LOADER
;
417 /* Try and figure out what it should be */
418 else if (sec_flags
& SEC_CODE
)
420 styp_flags
= STYP_TEXT
;
422 else if (sec_flags
& SEC_DATA
)
424 styp_flags
= STYP_DATA
;
426 else if (sec_flags
& SEC_READONLY
)
428 #ifdef STYP_LIT /* 29k readonly text/data section */
429 styp_flags
= STYP_LIT
;
431 styp_flags
= STYP_TEXT
;
432 #endif /* STYP_LIT */
434 else if (sec_flags
& SEC_LOAD
)
436 styp_flags
= STYP_TEXT
;
438 else if (sec_flags
& SEC_ALLOC
)
440 styp_flags
= STYP_BSS
;
444 if ((sec_flags
& (SEC_NEVER_LOAD
| SEC_COFF_SHARED_LIBRARY
)) != 0)
445 styp_flags
|= STYP_NOLOAD
;
449 if (sec_flags
& SEC_LINK_ONCE
)
450 styp_flags
|= IMAGE_SCN_LNK_COMDAT
;
451 if (sec_flags
& SEC_SHARED
)
452 styp_flags
|= IMAGE_SCN_MEM_SHARED
;
458 * Return a word with SEC_* flags set to represent the incoming
459 * STYP_* flags (from scnhdr.s_flags). The inverse of this
460 * function is sec_to_styp_flags().
461 * NOTE: If you add to/change this routine, you should mirror the changes
462 * in sec_to_styp_flags().
465 styp_to_sec_flags (abfd
, hdr
, name
)
466 bfd
*abfd ATTRIBUTE_UNUSED
;
470 struct internal_scnhdr
*internal_s
= (struct internal_scnhdr
*) hdr
;
471 long styp_flags
= internal_s
->s_flags
;
472 flagword sec_flags
= 0;
475 if (styp_flags
& STYP_NOLOAD
)
477 sec_flags
|= SEC_NEVER_LOAD
;
479 #endif /* STYP_NOLOAD */
481 /* For 386 COFF, at least, an unloadable text or data section is
482 actually a shared library section. */
483 if (styp_flags
& STYP_TEXT
)
485 if (sec_flags
& SEC_NEVER_LOAD
)
486 sec_flags
|= SEC_CODE
| SEC_COFF_SHARED_LIBRARY
;
488 sec_flags
|= SEC_CODE
| SEC_LOAD
| SEC_ALLOC
;
490 else if (styp_flags
& STYP_DATA
)
492 if (sec_flags
& SEC_NEVER_LOAD
)
493 sec_flags
|= SEC_DATA
| SEC_COFF_SHARED_LIBRARY
;
495 sec_flags
|= SEC_DATA
| SEC_LOAD
| SEC_ALLOC
;
497 else if (styp_flags
& STYP_BSS
)
499 #ifdef BSS_NOLOAD_IS_SHARED_LIBRARY
500 if (sec_flags
& SEC_NEVER_LOAD
)
501 sec_flags
|= SEC_ALLOC
| SEC_COFF_SHARED_LIBRARY
;
504 sec_flags
|= SEC_ALLOC
;
506 else if (styp_flags
& STYP_INFO
)
508 /* We mark these as SEC_DEBUGGING, but only if COFF_PAGE_SIZE is
509 defined. coff_compute_section_file_positions uses
510 COFF_PAGE_SIZE to ensure that the low order bits of the
511 section VMA and the file offset match. If we don't know
512 COFF_PAGE_SIZE, we can't ensure the correct correspondence,
513 and demand page loading of the file will fail. */
514 #if defined (COFF_PAGE_SIZE) && !defined (COFF_ALIGN_IN_S_FLAGS)
515 sec_flags
|= SEC_DEBUGGING
;
518 else if (styp_flags
& STYP_PAD
)
522 else if (strcmp (name
, _TEXT
) == 0)
524 if (sec_flags
& SEC_NEVER_LOAD
)
525 sec_flags
|= SEC_CODE
| SEC_COFF_SHARED_LIBRARY
;
527 sec_flags
|= SEC_CODE
| SEC_LOAD
| SEC_ALLOC
;
529 else if (strcmp (name
, _DATA
) == 0)
531 if (sec_flags
& SEC_NEVER_LOAD
)
532 sec_flags
|= SEC_DATA
| SEC_COFF_SHARED_LIBRARY
;
534 sec_flags
|= SEC_DATA
| SEC_LOAD
| SEC_ALLOC
;
536 else if (strcmp (name
, _BSS
) == 0)
538 #ifdef BSS_NOLOAD_IS_SHARED_LIBRARY
539 if (sec_flags
& SEC_NEVER_LOAD
)
540 sec_flags
|= SEC_ALLOC
| SEC_COFF_SHARED_LIBRARY
;
543 sec_flags
|= SEC_ALLOC
;
545 else if (strcmp (name
, ".debug") == 0
547 || strcmp (name
, _COMMENT
) == 0
549 || strncmp (name
, ".stab", 5) == 0)
551 #ifdef COFF_PAGE_SIZE
552 sec_flags
|= SEC_DEBUGGING
;
556 else if (strcmp (name
, _LIB
) == 0)
560 else if (strcmp (name
, _LIT
) == 0)
562 sec_flags
= SEC_LOAD
| SEC_ALLOC
| SEC_READONLY
;
567 sec_flags
|= SEC_ALLOC
| SEC_LOAD
;
570 #ifdef STYP_LIT /* A29k readonly text/data section type */
571 if ((styp_flags
& STYP_LIT
) == STYP_LIT
)
573 sec_flags
= (SEC_LOAD
| SEC_ALLOC
| SEC_READONLY
);
575 #endif /* STYP_LIT */
576 #ifdef STYP_OTHER_LOAD /* Other loaded sections */
577 if (styp_flags
& STYP_OTHER_LOAD
)
579 sec_flags
= (SEC_LOAD
| SEC_ALLOC
);
581 #endif /* STYP_SDATA */
584 if (styp_flags
& IMAGE_SCN_LNK_REMOVE
)
585 sec_flags
|= SEC_EXCLUDE
;
587 if (styp_flags
& IMAGE_SCN_MEM_SHARED
)
588 sec_flags
|= SEC_SHARED
;
590 if (styp_flags
& IMAGE_SCN_LNK_COMDAT
)
592 sec_flags
|= SEC_LINK_ONCE
;
594 /* Unfortunately, the PE format stores essential information in
595 the symbol table, of all places. We need to extract that
596 information now, so that objdump and the linker will know how
597 to handle the section without worrying about the symbols. We
598 can't call slurp_symtab, because the linker doesn't want the
601 /* COMDAT sections are special. The first symbol is the section
602 symbol, which tells what kind of COMDAT section it is. The
603 *second* symbol is the "comdat symbol" - the one with the
604 unique name. GNU uses the section symbol for the unique
605 name; MS uses ".text" for every comdat section. Sigh. - DJ */
607 if (_bfd_coff_get_external_symbols (abfd
))
609 bfd_byte
*esym
, *esymend
;
611 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
612 esymend
= esym
+ obj_raw_syment_count (abfd
) * SYMESZ
;
614 while (esym
< esymend
)
616 struct internal_syment isym
;
618 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &isym
);
620 if (sizeof (internal_s
->s_name
) > SYMNMLEN
)
622 /* This case implies that the matching symbol name
623 will be in the string table. */
627 if (isym
.n_sclass
== C_STAT
628 && isym
.n_type
== T_NULL
629 && isym
.n_numaux
== 1)
631 char buf
[SYMNMLEN
+ 1];
634 symname
= _bfd_coff_internal_syment_name (abfd
, &isym
, buf
);
638 if (strcmp (name
, symname
) == 0)
640 union internal_auxent aux
;
642 /* This is the section symbol. */
644 bfd_coff_swap_aux_in (abfd
, (PTR
) (esym
+ SYMESZ
),
645 isym
.n_type
, isym
.n_sclass
,
646 0, isym
.n_numaux
, (PTR
) &aux
);
648 /* FIXME: Microsoft uses NODUPLICATES and
649 ASSOCIATIVE, but gnu uses ANY and SAME_SIZE.
650 Unfortunately, gnu doesn't do the comdat
651 symbols right. So, until we can fix it to do
652 the right thing, we are temporarily disabling
653 comdats for the MS types (they're used in
654 DLLs and C++, but we don't support *their*
655 C++ libraries anyway - DJ */
657 switch (aux
.x_scn
.x_comdat
)
659 case IMAGE_COMDAT_SELECT_NODUPLICATES
:
661 sec_flags
|= SEC_LINK_DUPLICATES_ONE_ONLY
;
663 sec_flags
&= ~SEC_LINK_ONCE
;
668 case IMAGE_COMDAT_SELECT_ANY
:
669 sec_flags
|= SEC_LINK_DUPLICATES_DISCARD
;
672 case IMAGE_COMDAT_SELECT_SAME_SIZE
:
673 sec_flags
|= SEC_LINK_DUPLICATES_SAME_SIZE
;
676 case IMAGE_COMDAT_SELECT_EXACT_MATCH
:
677 sec_flags
|= SEC_LINK_DUPLICATES_SAME_CONTENTS
;
680 case IMAGE_COMDAT_SELECT_ASSOCIATIVE
:
682 /* FIXME: This is not currently implemented. */
683 sec_flags
|= SEC_LINK_DUPLICATES_DISCARD
;
685 sec_flags
&= ~SEC_LINK_ONCE
;
694 esym
+= (isym
.n_numaux
+ 1) * SYMESZ
;
700 #if defined (COFF_LONG_SECTION_NAMES) && defined (COFF_SUPPORT_GNU_LINKONCE)
701 /* As a GNU extension, if the name begins with .gnu.linkonce, we
702 only link a single copy of the section. This is used to support
703 g++. g++ will emit each template expansion in its own section.
704 The symbols will be defined as weak, so that multiple definitions
705 are permitted. The GNU linker extension is to actually discard
706 all but one of the sections. */
707 if (strncmp (name
, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
708 sec_flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
714 #define get_index(symbol) ((symbol)->udata.i)
718 bfd_coff_backend_data
722 .{* COFF symbol classifications. *}
724 .enum coff_symbol_classification
726 . {* Global symbol. *}
727 . COFF_SYMBOL_GLOBAL,
728 . {* Common symbol. *}
729 . COFF_SYMBOL_COMMON,
730 . {* Undefined symbol. *}
731 . COFF_SYMBOL_UNDEFINED,
732 . {* Local symbol. *}
734 . {* PE section symbol. *}
735 . COFF_SYMBOL_PE_SECTION
738 Special entry points for gdb to swap in coff symbol table parts:
741 . void (*_bfd_coff_swap_aux_in) PARAMS ((
750 . void (*_bfd_coff_swap_sym_in) PARAMS ((
755 . void (*_bfd_coff_swap_lineno_in) PARAMS ((
761 Special entry points for gas to swap out coff parts:
763 . unsigned int (*_bfd_coff_swap_aux_out) PARAMS ((
772 . unsigned int (*_bfd_coff_swap_sym_out) PARAMS ((
777 . unsigned int (*_bfd_coff_swap_lineno_out) PARAMS ((
782 . unsigned int (*_bfd_coff_swap_reloc_out) PARAMS ((
787 . unsigned int (*_bfd_coff_swap_filehdr_out) PARAMS ((
792 . unsigned int (*_bfd_coff_swap_aouthdr_out) PARAMS ((
797 . unsigned int (*_bfd_coff_swap_scnhdr_out) PARAMS ((
803 Special entry points for generic COFF routines to call target
804 dependent COFF routines:
806 . unsigned int _bfd_filhsz;
807 . unsigned int _bfd_aoutsz;
808 . unsigned int _bfd_scnhsz;
809 . unsigned int _bfd_symesz;
810 . unsigned int _bfd_auxesz;
811 . unsigned int _bfd_relsz;
812 . unsigned int _bfd_linesz;
813 . boolean _bfd_coff_long_filenames;
814 . boolean _bfd_coff_long_section_names;
815 . unsigned int _bfd_coff_default_section_alignment_power;
816 . void (*_bfd_coff_swap_filehdr_in) PARAMS ((
820 . void (*_bfd_coff_swap_aouthdr_in) PARAMS ((
824 . void (*_bfd_coff_swap_scnhdr_in) PARAMS ((
828 . void (*_bfd_coff_swap_reloc_in) PARAMS ((
832 . boolean (*_bfd_coff_bad_format_hook) PARAMS ((
834 . PTR internal_filehdr));
835 . boolean (*_bfd_coff_set_arch_mach_hook) PARAMS ((
837 . PTR internal_filehdr));
838 . PTR (*_bfd_coff_mkobject_hook) PARAMS ((
840 . PTR internal_filehdr,
841 . PTR internal_aouthdr));
842 . flagword (*_bfd_styp_to_sec_flags_hook) PARAMS ((
844 . PTR internal_scnhdr,
845 . const char *name));
846 . void (*_bfd_set_alignment_hook) PARAMS ((
849 . PTR internal_scnhdr));
850 . boolean (*_bfd_coff_slurp_symbol_table) PARAMS ((
852 . boolean (*_bfd_coff_symname_in_debug) PARAMS ((
854 . struct internal_syment *sym));
855 . boolean (*_bfd_coff_pointerize_aux_hook) PARAMS ((
857 . combined_entry_type *table_base,
858 . combined_entry_type *symbol,
859 . unsigned int indaux,
860 . combined_entry_type *aux));
861 . boolean (*_bfd_coff_print_aux) PARAMS ((
864 . combined_entry_type *table_base,
865 . combined_entry_type *symbol,
866 . combined_entry_type *aux,
867 . unsigned int indaux));
868 . void (*_bfd_coff_reloc16_extra_cases) PARAMS ((
870 . struct bfd_link_info *link_info,
871 . struct bfd_link_order *link_order,
874 . unsigned int *src_ptr,
875 . unsigned int *dst_ptr));
876 . int (*_bfd_coff_reloc16_estimate) PARAMS ((
878 . asection *input_section,
880 . unsigned int shrink,
881 . struct bfd_link_info *link_info));
882 . enum coff_symbol_classification (*_bfd_coff_classify_symbol) PARAMS ((
884 . struct internal_syment *));
885 . boolean (*_bfd_coff_compute_section_file_positions) PARAMS ((
887 . boolean (*_bfd_coff_start_final_link) PARAMS ((
889 . struct bfd_link_info *info));
890 . boolean (*_bfd_coff_relocate_section) PARAMS ((
892 . struct bfd_link_info *info,
894 . asection *input_section,
895 . bfd_byte *contents,
896 . struct internal_reloc *relocs,
897 . struct internal_syment *syms,
898 . asection **sections));
899 . reloc_howto_type *(*_bfd_coff_rtype_to_howto) PARAMS ((
902 . struct internal_reloc *rel,
903 . struct coff_link_hash_entry *h,
904 . struct internal_syment *sym,
905 . bfd_vma *addendp));
906 . boolean (*_bfd_coff_adjust_symndx) PARAMS ((
908 . struct bfd_link_info *info,
911 . struct internal_reloc *reloc,
912 . boolean *adjustedp));
913 . boolean (*_bfd_coff_link_add_one_symbol) PARAMS ((
914 . struct bfd_link_info *info,
920 . const char *string,
923 . struct bfd_link_hash_entry **hashp));
925 . boolean (*_bfd_coff_link_output_has_begun) PARAMS ((
927 . struct coff_final_link_info * pfinfo));
928 . boolean (*_bfd_coff_final_link_postscript) PARAMS ((
930 . struct coff_final_link_info * pfinfo));
932 .} bfd_coff_backend_data;
934 .#define coff_backend_info(abfd) ((bfd_coff_backend_data *) (abfd)->xvec->backend_data)
936 .#define bfd_coff_swap_aux_in(a,e,t,c,ind,num,i) \
937 . ((coff_backend_info (a)->_bfd_coff_swap_aux_in) (a,e,t,c,ind,num,i))
939 .#define bfd_coff_swap_sym_in(a,e,i) \
940 . ((coff_backend_info (a)->_bfd_coff_swap_sym_in) (a,e,i))
942 .#define bfd_coff_swap_lineno_in(a,e,i) \
943 . ((coff_backend_info ( a)->_bfd_coff_swap_lineno_in) (a,e,i))
945 .#define bfd_coff_swap_reloc_out(abfd, i, o) \
946 . ((coff_backend_info (abfd)->_bfd_coff_swap_reloc_out) (abfd, i, o))
948 .#define bfd_coff_swap_lineno_out(abfd, i, o) \
949 . ((coff_backend_info (abfd)->_bfd_coff_swap_lineno_out) (abfd, i, o))
951 .#define bfd_coff_swap_aux_out(a,i,t,c,ind,num,o) \
952 . ((coff_backend_info (a)->_bfd_coff_swap_aux_out) (a,i,t,c,ind,num,o))
954 .#define bfd_coff_swap_sym_out(abfd, i,o) \
955 . ((coff_backend_info (abfd)->_bfd_coff_swap_sym_out) (abfd, i, o))
957 .#define bfd_coff_swap_scnhdr_out(abfd, i,o) \
958 . ((coff_backend_info (abfd)->_bfd_coff_swap_scnhdr_out) (abfd, i, o))
960 .#define bfd_coff_swap_filehdr_out(abfd, i,o) \
961 . ((coff_backend_info (abfd)->_bfd_coff_swap_filehdr_out) (abfd, i, o))
963 .#define bfd_coff_swap_aouthdr_out(abfd, i,o) \
964 . ((coff_backend_info (abfd)->_bfd_coff_swap_aouthdr_out) (abfd, i, o))
966 .#define bfd_coff_filhsz(abfd) (coff_backend_info (abfd)->_bfd_filhsz)
967 .#define bfd_coff_aoutsz(abfd) (coff_backend_info (abfd)->_bfd_aoutsz)
968 .#define bfd_coff_scnhsz(abfd) (coff_backend_info (abfd)->_bfd_scnhsz)
969 .#define bfd_coff_symesz(abfd) (coff_backend_info (abfd)->_bfd_symesz)
970 .#define bfd_coff_auxesz(abfd) (coff_backend_info (abfd)->_bfd_auxesz)
971 .#define bfd_coff_relsz(abfd) (coff_backend_info (abfd)->_bfd_relsz)
972 .#define bfd_coff_linesz(abfd) (coff_backend_info (abfd)->_bfd_linesz)
973 .#define bfd_coff_long_filenames(abfd) (coff_backend_info (abfd)->_bfd_coff_long_filenames)
974 .#define bfd_coff_long_section_names(abfd) \
975 . (coff_backend_info (abfd)->_bfd_coff_long_section_names)
976 .#define bfd_coff_default_section_alignment_power(abfd) \
977 . (coff_backend_info (abfd)->_bfd_coff_default_section_alignment_power)
978 .#define bfd_coff_swap_filehdr_in(abfd, i,o) \
979 . ((coff_backend_info (abfd)->_bfd_coff_swap_filehdr_in) (abfd, i, o))
981 .#define bfd_coff_swap_aouthdr_in(abfd, i,o) \
982 . ((coff_backend_info (abfd)->_bfd_coff_swap_aouthdr_in) (abfd, i, o))
984 .#define bfd_coff_swap_scnhdr_in(abfd, i,o) \
985 . ((coff_backend_info (abfd)->_bfd_coff_swap_scnhdr_in) (abfd, i, o))
987 .#define bfd_coff_swap_reloc_in(abfd, i, o) \
988 . ((coff_backend_info (abfd)->_bfd_coff_swap_reloc_in) (abfd, i, o))
990 .#define bfd_coff_bad_format_hook(abfd, filehdr) \
991 . ((coff_backend_info (abfd)->_bfd_coff_bad_format_hook) (abfd, filehdr))
993 .#define bfd_coff_set_arch_mach_hook(abfd, filehdr)\
994 . ((coff_backend_info (abfd)->_bfd_coff_set_arch_mach_hook) (abfd, filehdr))
995 .#define bfd_coff_mkobject_hook(abfd, filehdr, aouthdr)\
996 . ((coff_backend_info (abfd)->_bfd_coff_mkobject_hook) (abfd, filehdr, aouthdr))
998 .#define bfd_coff_styp_to_sec_flags_hook(abfd, scnhdr, name)\
999 . ((coff_backend_info (abfd)->_bfd_styp_to_sec_flags_hook) (abfd, scnhdr, name))
1001 .#define bfd_coff_set_alignment_hook(abfd, sec, scnhdr)\
1002 . ((coff_backend_info (abfd)->_bfd_set_alignment_hook) (abfd, sec, scnhdr))
1004 .#define bfd_coff_slurp_symbol_table(abfd)\
1005 . ((coff_backend_info (abfd)->_bfd_coff_slurp_symbol_table) (abfd))
1007 .#define bfd_coff_symname_in_debug(abfd, sym)\
1008 . ((coff_backend_info (abfd)->_bfd_coff_symname_in_debug) (abfd, sym))
1010 .#define bfd_coff_print_aux(abfd, file, base, symbol, aux, indaux)\
1011 . ((coff_backend_info (abfd)->_bfd_coff_print_aux)\
1012 . (abfd, file, base, symbol, aux, indaux))
1014 .#define bfd_coff_reloc16_extra_cases(abfd, link_info, link_order, reloc, data, src_ptr, dst_ptr)\
1015 . ((coff_backend_info (abfd)->_bfd_coff_reloc16_extra_cases)\
1016 . (abfd, link_info, link_order, reloc, data, src_ptr, dst_ptr))
1018 .#define bfd_coff_reloc16_estimate(abfd, section, reloc, shrink, link_info)\
1019 . ((coff_backend_info (abfd)->_bfd_coff_reloc16_estimate)\
1020 . (abfd, section, reloc, shrink, link_info))
1022 .#define bfd_coff_classify_symbol(abfd, sym)\
1023 . ((coff_backend_info (abfd)->_bfd_coff_classify_symbol)\
1026 .#define bfd_coff_compute_section_file_positions(abfd)\
1027 . ((coff_backend_info (abfd)->_bfd_coff_compute_section_file_positions)\
1030 .#define bfd_coff_start_final_link(obfd, info)\
1031 . ((coff_backend_info (obfd)->_bfd_coff_start_final_link)\
1033 .#define bfd_coff_relocate_section(obfd,info,ibfd,o,con,rel,isyms,secs)\
1034 . ((coff_backend_info (ibfd)->_bfd_coff_relocate_section)\
1035 . (obfd, info, ibfd, o, con, rel, isyms, secs))
1036 .#define bfd_coff_rtype_to_howto(abfd, sec, rel, h, sym, addendp)\
1037 . ((coff_backend_info (abfd)->_bfd_coff_rtype_to_howto)\
1038 . (abfd, sec, rel, h, sym, addendp))
1039 .#define bfd_coff_adjust_symndx(obfd, info, ibfd, sec, rel, adjustedp)\
1040 . ((coff_backend_info (abfd)->_bfd_coff_adjust_symndx)\
1041 . (obfd, info, ibfd, sec, rel, adjustedp))
1042 .#define bfd_coff_link_add_one_symbol(info,abfd,name,flags,section,value,string,cp,coll,hashp)\
1043 . ((coff_backend_info (abfd)->_bfd_coff_link_add_one_symbol)\
1044 . (info, abfd, name, flags, section, value, string, cp, coll, hashp))
1046 .#define bfd_coff_link_output_has_begun(a,p) \
1047 . ((coff_backend_info (a)->_bfd_coff_link_output_has_begun) (a,p))
1048 .#define bfd_coff_final_link_postscript(a,p) \
1049 . ((coff_backend_info (a)->_bfd_coff_final_link_postscript) (a,p))
1053 /* See whether the magic number matches. */
1056 coff_bad_format_hook (abfd
, filehdr
)
1057 bfd
* abfd ATTRIBUTE_UNUSED
;
1060 struct internal_filehdr
*internal_f
= (struct internal_filehdr
*) filehdr
;
1062 if (BADMAG (*internal_f
))
1065 /* if the optional header is NULL or not the correct size then
1066 quit; the only difference I can see between m88k dgux headers (MC88DMAGIC)
1067 and Intel 960 readwrite headers (I960WRMAGIC) is that the
1068 optional header is of a different size.
1070 But the mips keeps extra stuff in it's opthdr, so dont check
1074 #if defined(M88) || defined(I960)
1075 if (internal_f
->f_opthdr
!= 0 && AOUTSZ
!= internal_f
->f_opthdr
)
1082 /* Check whether this section uses an alignment other than the
1086 coff_set_custom_section_alignment (abfd
, section
, alignment_table
, table_size
)
1087 bfd
*abfd ATTRIBUTE_UNUSED
;
1089 const struct coff_section_alignment_entry
*alignment_table
;
1090 const unsigned int table_size
;
1092 const unsigned int default_alignment
= COFF_DEFAULT_SECTION_ALIGNMENT_POWER
;
1095 for (i
= 0; i
< table_size
; ++i
)
1097 const char *secname
= bfd_get_section_name (abfd
, section
);
1098 if (alignment_table
[i
].comparison_length
== (unsigned int) -1
1099 ? strcmp (alignment_table
[i
].name
, secname
) == 0
1100 : strncmp (alignment_table
[i
].name
, secname
,
1101 alignment_table
[i
].comparison_length
) == 0)
1104 if (i
>= table_size
)
1107 if (alignment_table
[i
].default_alignment_min
!= COFF_ALIGNMENT_FIELD_EMPTY
1108 && default_alignment
< alignment_table
[i
].default_alignment_min
)
1111 if (alignment_table
[i
].default_alignment_max
!= COFF_ALIGNMENT_FIELD_EMPTY
1112 && default_alignment
> alignment_table
[i
].default_alignment_max
)
1115 section
->alignment_power
= alignment_table
[i
].alignment_power
;
1118 /* Custom section alignment records. */
1120 static const struct coff_section_alignment_entry
1121 coff_section_alignment_table
[] =
1123 #ifdef COFF_SECTION_ALIGNMENT_ENTRIES
1124 COFF_SECTION_ALIGNMENT_ENTRIES
,
1126 /* There must not be any gaps between .stabstr sections. */
1127 { COFF_SECTION_NAME_PARTIAL_MATCH (".stabstr"),
1128 1, COFF_ALIGNMENT_FIELD_EMPTY
, 0 },
1129 /* The .stab section must be aligned to 2**2 at most, to avoid gaps. */
1130 { COFF_SECTION_NAME_PARTIAL_MATCH (".stab"),
1131 3, COFF_ALIGNMENT_FIELD_EMPTY
, 2 },
1132 /* Similarly for the .ctors and .dtors sections. */
1133 { COFF_SECTION_NAME_EXACT_MATCH (".ctors"),
1134 3, COFF_ALIGNMENT_FIELD_EMPTY
, 2 },
1135 { COFF_SECTION_NAME_EXACT_MATCH (".dtors"),
1136 3, COFF_ALIGNMENT_FIELD_EMPTY
, 2 }
1139 static const unsigned int coff_section_alignment_table_size
=
1140 sizeof coff_section_alignment_table
/ sizeof coff_section_alignment_table
[0];
1142 /* Initialize a section structure with information peculiar to this
1143 particular implementation of COFF. */
1146 coff_new_section_hook (abfd
, section
)
1150 combined_entry_type
*native
;
1152 section
->alignment_power
= COFF_DEFAULT_SECTION_ALIGNMENT_POWER
;
1155 if (xcoff_data (abfd
)->text_align_power
!= 0
1156 && strcmp (bfd_get_section_name (abfd
, section
), ".text") == 0)
1157 section
->alignment_power
= xcoff_data (abfd
)->text_align_power
;
1158 if (xcoff_data (abfd
)->data_align_power
!= 0
1159 && strcmp (bfd_get_section_name (abfd
, section
), ".data") == 0)
1160 section
->alignment_power
= xcoff_data (abfd
)->data_align_power
;
1163 /* Allocate aux records for section symbols, to store size and
1166 @@ The 10 is a guess at a plausible maximum number of aux entries
1167 (but shouldn't be a constant). */
1168 native
= ((combined_entry_type
*)
1169 bfd_zalloc (abfd
, sizeof (combined_entry_type
) * 10));
1173 /* We don't need to set up n_name, n_value, or n_scnum in the native
1174 symbol information, since they'll be overriden by the BFD symbol
1175 anyhow. However, we do need to set the type and storage class,
1176 in case this symbol winds up getting written out. The value 0
1177 for n_numaux is already correct. */
1179 native
->u
.syment
.n_type
= T_NULL
;
1180 native
->u
.syment
.n_sclass
= C_STAT
;
1182 coffsymbol (section
->symbol
)->native
= native
;
1184 coff_set_custom_section_alignment (abfd
, section
,
1185 coff_section_alignment_table
,
1186 coff_section_alignment_table_size
);
1191 #ifdef COFF_ALIGN_IN_SECTION_HEADER
1193 /* Set the alignment of a BFD section. */
1195 static void coff_set_alignment_hook
PARAMS ((bfd
*, asection
*, PTR
));
1198 coff_set_alignment_hook (abfd
, section
, scnhdr
)
1199 bfd
* abfd ATTRIBUTE_UNUSED
;
1203 struct internal_scnhdr
*hdr
= (struct internal_scnhdr
*) scnhdr
;
1207 /* Extract ALIGN from 2**ALIGN stored in section header */
1208 for (i
= 0; i
< 32; i
++)
1209 if ((1 << i
) >= hdr
->s_align
)
1213 /* TI tools hijack bits 8-11 for the alignment */
1214 i
= (hdr
->s_flags
>> 8) & 0xF ;
1216 section
->alignment_power
= i
;
1219 #else /* ! COFF_ALIGN_IN_SECTION_HEADER */
1222 /* a couple of macros to help setting the alignment power field */
1223 #define ALIGN_SET(field,x,y) \
1224 if (((field) & IMAGE_SCN_ALIGN_64BYTES) == x )\
1226 section->alignment_power = y;\
1229 #define ELIFALIGN_SET(field,x,y) \
1230 else if (( (field) & IMAGE_SCN_ALIGN_64BYTES) == x ) \
1232 section->alignment_power = y;\
1235 static void coff_set_alignment_hook
PARAMS ((bfd
*, asection
*, PTR
));
1238 coff_set_alignment_hook (abfd
, section
, scnhdr
)
1239 bfd
* abfd ATTRIBUTE_UNUSED
;
1243 struct internal_scnhdr
*hdr
= (struct internal_scnhdr
*) scnhdr
;
1245 ALIGN_SET (hdr
->s_flags
, IMAGE_SCN_ALIGN_64BYTES
, 6)
1246 ELIFALIGN_SET (hdr
->s_flags
, IMAGE_SCN_ALIGN_32BYTES
, 5)
1247 ELIFALIGN_SET (hdr
->s_flags
, IMAGE_SCN_ALIGN_16BYTES
, 4)
1248 ELIFALIGN_SET (hdr
->s_flags
, IMAGE_SCN_ALIGN_8BYTES
, 3)
1249 ELIFALIGN_SET (hdr
->s_flags
, IMAGE_SCN_ALIGN_4BYTES
, 2)
1250 ELIFALIGN_SET (hdr
->s_flags
, IMAGE_SCN_ALIGN_2BYTES
, 1)
1251 ELIFALIGN_SET (hdr
->s_flags
, IMAGE_SCN_ALIGN_1BYTES
, 0)
1253 #ifdef POWERPC_LE_PE
1254 if (strcmp (section
->name
, ".idata$2") == 0)
1256 section
->alignment_power
= 0;
1258 else if (strcmp (section
->name
, ".idata$3") == 0)
1260 section
->alignment_power
= 0;
1262 else if (strcmp (section
->name
, ".idata$4") == 0)
1264 section
->alignment_power
= 2;
1266 else if (strcmp (section
->name
, ".idata$5") == 0)
1268 section
->alignment_power
= 2;
1270 else if (strcmp (section
->name
, ".idata$6") == 0)
1272 section
->alignment_power
= 1;
1274 else if (strcmp (section
->name
, ".reloc") == 0)
1276 section
->alignment_power
= 1;
1278 else if (strncmp (section
->name
, ".stab", 5) == 0)
1280 section
->alignment_power
= 2;
1284 #ifdef COFF_IMAGE_WITH_PE
1285 /* In a PE image file, the s_paddr field holds the virtual size of a
1286 section, while the s_size field holds the raw size. */
1287 if (hdr
->s_paddr
!= 0)
1289 if (coff_section_data (abfd
, section
) == NULL
)
1291 section
->used_by_bfd
=
1292 (PTR
) bfd_zalloc (abfd
, sizeof (struct coff_section_tdata
));
1293 if (section
->used_by_bfd
== NULL
)
1295 /* FIXME: Return error. */
1299 if (pei_section_data (abfd
, section
) == NULL
)
1301 coff_section_data (abfd
, section
)->tdata
=
1302 (PTR
) bfd_zalloc (abfd
, sizeof (struct pei_section_tdata
));
1303 if (coff_section_data (abfd
, section
)->tdata
== NULL
)
1305 /* FIXME: Return error. */
1309 pei_section_data (abfd
, section
)->virt_size
= hdr
->s_paddr
;
1314 section
->lma
= hdr
->s_vaddr
;
1318 #undef ELIFALIGN_SET
1320 #else /* ! COFF_WITH_PE */
1323 /* We grossly abuse this function to handle XCOFF overflow headers.
1324 When we see one, we correct the reloc and line number counts in the
1325 real header, and remove the section we just created. */
1327 static void coff_set_alignment_hook
PARAMS ((bfd
*, asection
*, PTR
));
1330 coff_set_alignment_hook (abfd
, section
, scnhdr
)
1335 struct internal_scnhdr
*hdr
= (struct internal_scnhdr
*) scnhdr
;
1339 if ((hdr
->s_flags
& STYP_OVRFLO
) == 0)
1342 real_sec
= coff_section_from_bfd_index (abfd
, hdr
->s_nreloc
);
1343 if (real_sec
== NULL
)
1346 real_sec
->reloc_count
= hdr
->s_paddr
;
1347 real_sec
->lineno_count
= hdr
->s_vaddr
;
1349 for (ps
= &abfd
->sections
; *ps
!= NULL
; ps
= &(*ps
)->next
)
1354 --abfd
->section_count
;
1360 #else /* ! RS6000COFF_C */
1362 #define coff_set_alignment_hook \
1363 ((void (*) PARAMS ((bfd *, asection *, PTR))) bfd_void)
1365 #endif /* ! RS6000COFF_C */
1366 #endif /* ! COFF_WITH_PE */
1367 #endif /* ! COFF_ALIGN_IN_SECTION_HEADER */
1369 #ifndef coff_mkobject
1371 static boolean coff_mkobject
PARAMS ((bfd
*));
1374 coff_mkobject (abfd
)
1377 coff_data_type
*coff
;
1379 abfd
->tdata
.coff_obj_data
= (struct coff_tdata
*) bfd_zalloc (abfd
, sizeof (coff_data_type
));
1380 if (abfd
->tdata
.coff_obj_data
== 0)
1382 coff
= coff_data (abfd
);
1383 coff
->symbols
= (coff_symbol_type
*) NULL
;
1384 coff
->conversion_table
= (unsigned int *) NULL
;
1385 coff
->raw_syments
= (struct coff_ptr_struct
*) NULL
;
1386 coff
->relocbase
= 0;
1387 coff
->local_toc_sym_map
= 0;
1389 /* make_abs_section(abfd);*/
1395 /* Create the COFF backend specific information. */
1396 #ifndef coff_mkobject_hook
1398 coff_mkobject_hook (abfd
, filehdr
, aouthdr
)
1401 PTR aouthdr ATTRIBUTE_UNUSED
;
1403 struct internal_filehdr
*internal_f
= (struct internal_filehdr
*) filehdr
;
1404 coff_data_type
*coff
;
1406 if (coff_mkobject (abfd
) == false)
1409 coff
= coff_data (abfd
);
1411 coff
->sym_filepos
= internal_f
->f_symptr
;
1413 /* These members communicate important constants about the symbol
1414 table to GDB's symbol-reading code. These `constants'
1415 unfortunately vary among coff implementations... */
1416 coff
->local_n_btmask
= N_BTMASK
;
1417 coff
->local_n_btshft
= N_BTSHFT
;
1418 coff
->local_n_tmask
= N_TMASK
;
1419 coff
->local_n_tshift
= N_TSHIFT
;
1420 coff
->local_symesz
= SYMESZ
;
1421 coff
->local_auxesz
= AUXESZ
;
1422 coff
->local_linesz
= LINESZ
;
1424 obj_raw_syment_count (abfd
) =
1425 obj_conv_table_size (abfd
) =
1426 internal_f
->f_nsyms
;
1429 if ((internal_f
->f_flags
& F_SHROBJ
) != 0)
1430 abfd
->flags
|= DYNAMIC
;
1431 if (aouthdr
!= NULL
&& internal_f
->f_opthdr
>= AOUTSZ
)
1433 struct internal_aouthdr
*internal_a
=
1434 (struct internal_aouthdr
*) aouthdr
;
1435 struct xcoff_tdata
*xcoff
;
1437 xcoff
= xcoff_data (abfd
);
1438 xcoff
->full_aouthdr
= true;
1439 xcoff
->toc
= internal_a
->o_toc
;
1440 xcoff
->sntoc
= internal_a
->o_sntoc
;
1441 xcoff
->snentry
= internal_a
->o_snentry
;
1442 xcoff
->text_align_power
= internal_a
->o_algntext
;
1443 xcoff
->data_align_power
= internal_a
->o_algndata
;
1444 xcoff
->modtype
= internal_a
->o_modtype
;
1445 xcoff
->cputype
= internal_a
->o_cputype
;
1446 xcoff
->maxdata
= internal_a
->o_maxdata
;
1447 xcoff
->maxstack
= internal_a
->o_maxstack
;
1452 /* Set the flags field from the COFF header read in */
1453 if (! _bfd_coff_arm_set_private_flags (abfd
, internal_f
->f_flags
))
1461 /* Determine the machine architecture and type. FIXME: This is target
1462 dependent because the magic numbers are defined in the target
1463 dependent header files. But there is no particular need for this.
1464 If the magic numbers were moved to a separate file, this function
1465 would be target independent and would also be much more successful
1466 at linking together COFF files for different architectures. */
1469 coff_set_arch_mach_hook (abfd
, filehdr
)
1474 enum bfd_architecture arch
;
1475 struct internal_filehdr
*internal_f
= (struct internal_filehdr
*) filehdr
;
1478 switch (internal_f
->f_magic
)
1482 arch
= bfd_arch_powerpc
;
1483 machine
= 0; /* what does this mean? (krk) */
1489 case I386AIXMAGIC
: /* Danbury PS/2 AIX C Compiler */
1490 case LYNXCOFFMAGIC
: /* shadows the m68k Lynx number below, sigh */
1491 arch
= bfd_arch_i386
;
1495 #ifdef A29K_MAGIC_BIG
1496 case A29K_MAGIC_BIG
:
1497 case A29K_MAGIC_LITTLE
:
1498 arch
= bfd_arch_a29k
;
1504 arch
= bfd_arch_arm
;
1505 switch (internal_f
->f_flags
& F_ARM_ARCHITECTURE_MASK
)
1507 case F_ARM_2
: machine
= bfd_mach_arm_2
; break;
1508 case F_ARM_2a
: machine
= bfd_mach_arm_2a
; break;
1509 case F_ARM_3
: machine
= bfd_mach_arm_3
; break;
1511 case F_ARM_3M
: machine
= bfd_mach_arm_3M
; break;
1512 case F_ARM_4
: machine
= bfd_mach_arm_4
; break;
1513 case F_ARM_4T
: machine
= bfd_mach_arm_4T
; break;
1514 case F_ARM_5
: machine
= bfd_mach_arm_5
; break;
1521 #ifdef MC68KBCSMAGIC
1524 #ifdef APOLLOM68KMAGIC
1525 case APOLLOM68KMAGIC
:
1527 #ifdef LYNXCOFFMAGIC
1530 arch
= bfd_arch_m68k
;
1531 machine
= bfd_mach_m68020
;
1538 arch
= bfd_arch_m88k
;
1544 arch
= bfd_arch_z8k
;
1545 switch (internal_f
->f_flags
& F_MACHMASK
)
1548 machine
= bfd_mach_z8001
;
1551 machine
= bfd_mach_z8002
;
1560 arch
= bfd_arch_i860
;
1567 arch
= bfd_arch_i960
;
1568 switch (F_I960TYPE
& internal_f
->f_flags
)
1572 machine
= bfd_mach_i960_core
;
1575 machine
= bfd_mach_i960_kb_sb
;
1578 machine
= bfd_mach_i960_mc
;
1581 machine
= bfd_mach_i960_xa
;
1584 machine
= bfd_mach_i960_ca
;
1587 machine
= bfd_mach_i960_ka_sa
;
1590 machine
= bfd_mach_i960_jx
;
1593 machine
= bfd_mach_i960_hx
;
1607 if (xcoff_data (abfd
)->cputype
!= -1)
1608 cputype
= xcoff_data (abfd
)->cputype
& 0xff;
1611 /* We did not get a value from the a.out header. If the
1612 file has not been stripped, we may be able to get the
1613 architecture information from the first symbol, if it
1614 is a .file symbol. */
1615 if (obj_raw_syment_count (abfd
) == 0)
1619 bfd_byte buf
[SYMESZ
];
1620 struct internal_syment sym
;
1622 if (bfd_seek (abfd
, obj_sym_filepos (abfd
), SEEK_SET
) != 0
1623 || bfd_read (buf
, 1, SYMESZ
, abfd
) != SYMESZ
)
1625 coff_swap_sym_in (abfd
, (PTR
) buf
, (PTR
) &sym
);
1626 if (sym
.n_sclass
== C_FILE
)
1627 cputype
= sym
.n_type
& 0xff;
1633 /* FIXME: We don't handle all cases here. */
1639 /* PowerPC Macs use the same magic numbers as RS/6000
1640 (because that's how they were bootstrapped originally),
1641 but they are always PowerPC architecture. */
1642 arch
= bfd_arch_powerpc
;
1645 arch
= bfd_arch_rs6000
;
1647 #endif /* POWERMAC */
1651 arch
= bfd_arch_powerpc
;
1654 case 2: /* 64 bit PowerPC */
1655 arch
= bfd_arch_powerpc
;
1659 arch
= bfd_arch_powerpc
;
1663 arch
= bfd_arch_rs6000
;
1673 arch
= bfd_arch_we32k
;
1680 arch
= bfd_arch_h8300
;
1681 machine
= bfd_mach_h8300
;
1682 /* !! FIXME this probably isn't the right place for this */
1683 abfd
->flags
|= BFD_IS_RELAXABLE
;
1689 arch
= bfd_arch_h8300
;
1690 machine
= bfd_mach_h8300h
;
1691 /* !! FIXME this probably isn't the right place for this */
1692 abfd
->flags
|= BFD_IS_RELAXABLE
;
1698 arch
= bfd_arch_h8300
;
1699 machine
= bfd_mach_h8300s
;
1700 /* !! FIXME this probably isn't the right place for this */
1701 abfd
->flags
|= BFD_IS_RELAXABLE
;
1705 #ifdef SH_ARCH_MAGIC_BIG
1706 case SH_ARCH_MAGIC_BIG
:
1707 case SH_ARCH_MAGIC_LITTLE
:
1715 arch
= bfd_arch_h8500
;
1722 #ifdef LYNXCOFFMAGIC
1725 arch
= bfd_arch_sparc
;
1732 arch
= bfd_arch_tic30
;
1736 #ifdef TIC80_ARCH_MAGIC
1737 case TIC80_ARCH_MAGIC
:
1738 arch
= bfd_arch_tic80
;
1744 arch
= bfd_arch_mcore
;
1747 default: /* Unreadable input file type */
1748 arch
= bfd_arch_obscure
;
1752 bfd_default_set_arch_mach (abfd
, arch
, machine
);
1756 #ifdef SYMNAME_IN_DEBUG
1758 static boolean symname_in_debug_hook
1759 PARAMS ((bfd
*, struct internal_syment
*));
1762 symname_in_debug_hook (abfd
, sym
)
1763 bfd
* abfd ATTRIBUTE_UNUSED
;
1764 struct internal_syment
*sym
;
1766 return SYMNAME_IN_DEBUG (sym
) ? true : false;
1771 #define symname_in_debug_hook \
1772 (boolean (*) PARAMS ((bfd *, struct internal_syment *))) bfd_false
1778 /* Handle the csect auxent of a C_EXT or C_HIDEXT symbol. */
1780 static boolean coff_pointerize_aux_hook
1781 PARAMS ((bfd
*, combined_entry_type
*, combined_entry_type
*,
1782 unsigned int, combined_entry_type
*));
1786 coff_pointerize_aux_hook (abfd
, table_base
, symbol
, indaux
, aux
)
1787 bfd
*abfd ATTRIBUTE_UNUSED
;
1788 combined_entry_type
*table_base
;
1789 combined_entry_type
*symbol
;
1790 unsigned int indaux
;
1791 combined_entry_type
*aux
;
1793 int class = symbol
->u
.syment
.n_sclass
;
1795 if ((class == C_EXT
|| class == C_HIDEXT
)
1796 && indaux
+ 1 == symbol
->u
.syment
.n_numaux
)
1798 if (SMTYP_SMTYP (aux
->u
.auxent
.x_csect
.x_smtyp
) == XTY_LD
)
1800 aux
->u
.auxent
.x_csect
.x_scnlen
.p
=
1801 table_base
+ aux
->u
.auxent
.x_csect
.x_scnlen
.l
;
1802 aux
->fix_scnlen
= 1;
1805 /* Return true to indicate that the caller should not do any
1806 further work on this auxent. */
1810 /* Return false to indicate that this auxent should be handled by
1818 /* We don't want to pointerize bal entries. */
1820 static boolean coff_pointerize_aux_hook
1821 PARAMS ((bfd
*, combined_entry_type
*, combined_entry_type
*,
1822 unsigned int, combined_entry_type
*));
1826 coff_pointerize_aux_hook (abfd
, table_base
, symbol
, indaux
, aux
)
1827 bfd
*abfd ATTRIBUTE_UNUSED
;
1828 combined_entry_type
*table_base ATTRIBUTE_UNUSED
;
1829 combined_entry_type
*symbol
;
1830 unsigned int indaux
;
1831 combined_entry_type
*aux ATTRIBUTE_UNUSED
;
1833 /* Return true if we don't want to pointerize this aux entry, which
1834 is the case for the lastfirst aux entry for a C_LEAFPROC symbol. */
1836 && (symbol
->u
.syment
.n_sclass
== C_LEAFPROC
1837 || symbol
->u
.syment
.n_sclass
== C_LEAFSTAT
1838 || symbol
->u
.syment
.n_sclass
== C_LEAFEXT
));
1843 #define coff_pointerize_aux_hook 0
1846 #endif /* ! RS6000COFF_C */
1848 /* Print an aux entry. This returns true if it has printed it. */
1850 static boolean coff_print_aux
1851 PARAMS ((bfd
*, FILE *, combined_entry_type
*, combined_entry_type
*,
1852 combined_entry_type
*, unsigned int));
1855 coff_print_aux (abfd
, file
, table_base
, symbol
, aux
, indaux
)
1856 bfd
*abfd ATTRIBUTE_UNUSED
;
1857 FILE *file ATTRIBUTE_UNUSED
;
1858 combined_entry_type
*table_base ATTRIBUTE_UNUSED
;
1859 combined_entry_type
*symbol ATTRIBUTE_UNUSED
;
1860 combined_entry_type
*aux ATTRIBUTE_UNUSED
;
1861 unsigned int indaux ATTRIBUTE_UNUSED
;
1864 if ((symbol
->u
.syment
.n_sclass
== C_EXT
1865 || symbol
->u
.syment
.n_sclass
== C_HIDEXT
)
1866 && indaux
+ 1 == symbol
->u
.syment
.n_numaux
)
1868 /* This is a csect entry. */
1869 fprintf (file
, "AUX ");
1870 if (SMTYP_SMTYP (aux
->u
.auxent
.x_csect
.x_smtyp
) != XTY_LD
)
1872 BFD_ASSERT (! aux
->fix_scnlen
);
1873 fprintf (file
, "val %5ld", aux
->u
.auxent
.x_csect
.x_scnlen
.l
);
1877 fprintf (file
, "indx ");
1878 if (! aux
->fix_scnlen
)
1879 fprintf (file
, "%4ld", aux
->u
.auxent
.x_csect
.x_scnlen
.l
);
1881 fprintf (file
, "%4ld",
1882 (long) (aux
->u
.auxent
.x_csect
.x_scnlen
.p
- table_base
));
1885 " prmhsh %ld snhsh %u typ %d algn %d clss %u stb %ld snstb %u",
1886 aux
->u
.auxent
.x_csect
.x_parmhash
,
1887 (unsigned int) aux
->u
.auxent
.x_csect
.x_snhash
,
1888 SMTYP_SMTYP (aux
->u
.auxent
.x_csect
.x_smtyp
),
1889 SMTYP_ALIGN (aux
->u
.auxent
.x_csect
.x_smtyp
),
1890 (unsigned int) aux
->u
.auxent
.x_csect
.x_smclas
,
1891 aux
->u
.auxent
.x_csect
.x_stab
,
1892 (unsigned int) aux
->u
.auxent
.x_csect
.x_snstab
);
1897 /* Return false to indicate that no special action was taken. */
1905 To write relocations, the back end steps though the
1906 canonical relocation table and create an
1907 @code{internal_reloc}. The symbol index to use is removed from
1908 the @code{offset} field in the symbol table supplied. The
1909 address comes directly from the sum of the section base
1910 address and the relocation offset; the type is dug directly
1911 from the howto field. Then the @code{internal_reloc} is
1912 swapped into the shape of an @code{external_reloc} and written
1919 static int compare_arelent_ptr
PARAMS ((const PTR
, const PTR
));
1921 /* AUX's ld wants relocations to be sorted */
1923 compare_arelent_ptr (x
, y
)
1927 const arelent
**a
= (const arelent
**) x
;
1928 const arelent
**b
= (const arelent
**) y
;
1929 bfd_size_type aadr
= (*a
)->address
;
1930 bfd_size_type badr
= (*b
)->address
;
1932 return (aadr
< badr
? -1 : badr
< aadr
? 1 : 0);
1935 #endif /* TARG_AUX */
1938 coff_write_relocs (abfd
, first_undef
)
1944 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
)
1947 struct external_reloc dst
;
1953 /* sort relocations before we write them out */
1954 p
= (arelent
**) bfd_malloc (s
->reloc_count
* sizeof (arelent
*));
1955 if (p
== NULL
&& s
->reloc_count
> 0)
1957 memcpy (p
, s
->orelocation
, s
->reloc_count
* sizeof (arelent
*));
1958 qsort (p
, s
->reloc_count
, sizeof (arelent
*), compare_arelent_ptr
);
1961 if (bfd_seek (abfd
, s
->rel_filepos
, SEEK_SET
) != 0)
1963 for (i
= 0; i
< s
->reloc_count
; i
++)
1965 struct internal_reloc n
;
1967 memset ((PTR
) & n
, 0, sizeof (n
));
1969 /* Now we've renumbered the symbols we know where the
1970 undefined symbols live in the table. Check the reloc
1971 entries for symbols who's output bfd isn't the right one.
1972 This is because the symbol was undefined (which means
1973 that all the pointers are never made to point to the same
1974 place). This is a bad thing,'cause the symbols attached
1975 to the output bfd are indexed, so that the relocation
1976 entries know which symbol index they point to. So we
1977 have to look up the output symbol here. */
1979 if (q
->sym_ptr_ptr
[0]->the_bfd
!= abfd
)
1982 const char *sname
= q
->sym_ptr_ptr
[0]->name
;
1983 asymbol
**outsyms
= abfd
->outsymbols
;
1984 for (i
= first_undef
; outsyms
[i
]; i
++)
1986 const char *intable
= outsyms
[i
]->name
;
1987 if (strcmp (intable
, sname
) == 0) {
1988 /* got a hit, so repoint the reloc */
1989 q
->sym_ptr_ptr
= outsyms
+ i
;
1995 n
.r_vaddr
= q
->address
+ s
->vma
;
1998 /* The 29k const/consth reloc pair is a real kludge. The consth
1999 part doesn't have a symbol; it has an offset. So rebuilt
2001 if (q
->howto
->type
== R_IHCONST
)
2002 n
.r_symndx
= q
->addend
;
2007 if (q
->sym_ptr_ptr
== bfd_abs_section_ptr
->symbol_ptr_ptr
)
2008 /* This is a relocation relative to the absolute symbol. */
2012 n
.r_symndx
= get_index ((*(q
->sym_ptr_ptr
)));
2013 /* Take notice if the symbol reloc points to a symbol
2014 we don't have in our symbol table. What should we
2016 if (n
.r_symndx
> obj_conv_table_size (abfd
))
2021 #ifdef SWAP_OUT_RELOC_OFFSET
2022 n
.r_offset
= q
->addend
;
2026 /* Work out reloc type from what is required */
2027 SELECT_RELOC (n
, q
->howto
);
2029 n
.r_type
= q
->howto
->type
;
2031 coff_swap_reloc_out (abfd
, &n
, &dst
);
2032 if (bfd_write ((PTR
) & dst
, 1, RELSZ
, abfd
) != RELSZ
)
2045 /* Set flags and magic number of a coff file from architecture and machine
2046 type. Result is true if we can represent the arch&type, false if not. */
2049 coff_set_flags (abfd
, magicp
, flagsp
)
2051 unsigned int *magicp ATTRIBUTE_UNUSED
;
2052 unsigned short *flagsp ATTRIBUTE_UNUSED
;
2054 switch (bfd_get_arch (abfd
))
2059 switch (bfd_get_mach (abfd
))
2061 case bfd_mach_z8001
:
2064 case bfd_mach_z8002
:
2078 *magicp
= I960ROMAGIC
;
2080 ((bfd_get_file_flags(abfd) & WP_TEXT) ? I960ROMAGIC :
2081 I960RWMAGIC); FIXME???
2083 switch (bfd_get_mach (abfd
))
2085 case bfd_mach_i960_core
:
2088 case bfd_mach_i960_kb_sb
:
2091 case bfd_mach_i960_mc
:
2094 case bfd_mach_i960_xa
:
2097 case bfd_mach_i960_ca
:
2100 case bfd_mach_i960_ka_sa
:
2103 case bfd_mach_i960_jx
:
2106 case bfd_mach_i960_hx
:
2119 case bfd_arch_tic30
:
2120 *magicp
= TIC30MAGIC
;
2123 #ifdef TIC80_ARCH_MAGIC
2124 case bfd_arch_tic80
:
2125 *magicp
= TIC80_ARCH_MAGIC
;
2130 * magicp
= ARMMAGIC
;
2132 if (APCS_SET (abfd
))
2134 if (APCS_26_FLAG (abfd
))
2135 * flagsp
|= F_APCS26
;
2137 if (APCS_FLOAT_FLAG (abfd
))
2138 * flagsp
|= F_APCS_FLOAT
;
2140 if (PIC_FLAG (abfd
))
2143 if (INTERWORK_SET (abfd
) && INTERWORK_FLAG (abfd
))
2144 * flagsp
|= F_INTERWORK
;
2145 switch (bfd_get_mach (abfd
))
2147 case bfd_mach_arm_2
: * flagsp
|= F_ARM_2
; break;
2148 case bfd_mach_arm_2a
: * flagsp
|= F_ARM_2a
; break;
2149 case bfd_mach_arm_3
: * flagsp
|= F_ARM_3
; break;
2150 case bfd_mach_arm_3M
: * flagsp
|= F_ARM_3M
; break;
2151 case bfd_mach_arm_4
: * flagsp
|= F_ARM_4
; break;
2152 case bfd_mach_arm_4T
: * flagsp
|= F_ARM_4T
; break;
2153 case bfd_mach_arm_5
: * flagsp
|= F_ARM_5
; break;
2154 case bfd_mach_arm_5T
: * flagsp
|= F_ARM_5
; break; /* XXX - we do not have an F_ARM_5T */
2159 case bfd_arch_powerpc
:
2166 *magicp
= I386MAGIC
;
2168 /* Just overwrite the usual value if we're doing Lynx. */
2169 *magicp
= LYNXCOFFMAGIC
;
2176 *magicp
= I860MAGIC
;
2182 #ifdef APOLLOM68KMAGIC
2183 *magicp
= APOLLO_COFF_VERSION_NUMBER
;
2185 /* NAMES_HAVE_UNDERSCORE may be defined by coff-u68k.c. */
2186 #ifdef NAMES_HAVE_UNDERSCORE
2187 *magicp
= MC68KBCSMAGIC
;
2189 *magicp
= MC68MAGIC
;
2193 /* Just overwrite the usual value if we're doing Lynx. */
2194 *magicp
= LYNXCOFFMAGIC
;
2202 *magicp
= MC88OMAGIC
;
2207 case bfd_arch_h8300
:
2208 switch (bfd_get_mach (abfd
))
2210 case bfd_mach_h8300
:
2211 *magicp
= H8300MAGIC
;
2213 case bfd_mach_h8300h
:
2214 *magicp
= H8300HMAGIC
;
2216 case bfd_mach_h8300s
:
2217 *magicp
= H8300SMAGIC
;
2223 #ifdef SH_ARCH_MAGIC_BIG
2225 if (bfd_big_endian (abfd
))
2226 *magicp
= SH_ARCH_MAGIC_BIG
;
2228 *magicp
= SH_ARCH_MAGIC_LITTLE
;
2234 case bfd_arch_sparc
:
2235 *magicp
= SPARCMAGIC
;
2237 /* Just overwrite the usual value if we're doing Lynx. */
2238 *magicp
= LYNXCOFFMAGIC
;
2245 case bfd_arch_h8500
:
2246 *magicp
= H8500MAGIC
;
2250 #ifdef A29K_MAGIC_BIG
2252 if (bfd_big_endian (abfd
))
2253 *magicp
= A29K_MAGIC_BIG
;
2255 *magicp
= A29K_MAGIC_LITTLE
;
2261 case bfd_arch_we32k
:
2262 *magicp
= WE32KMAGIC
;
2268 case bfd_arch_rs6000
:
2270 case bfd_arch_powerpc
:
2272 *magicp
= U802TOCMAGIC
;
2278 case bfd_arch_mcore
:
2279 * magicp
= MCOREMAGIC
;
2283 default: /* Unknown architecture */
2284 /* return false; -- fall through to "return false" below, to avoid
2285 "statement never reached" errors on the one below. */
2294 coff_set_arch_mach (abfd
, arch
, machine
)
2296 enum bfd_architecture arch
;
2297 unsigned long machine
;
2300 unsigned short dummy2
;
2302 if (! bfd_default_set_arch_mach (abfd
, arch
, machine
))
2305 if (arch
!= bfd_arch_unknown
&&
2306 coff_set_flags (abfd
, &dummy1
, &dummy2
) != true)
2307 return false; /* We can't represent this type */
2309 return true; /* We're easy ... */
2313 /* Calculate the file position for each section. */
2316 #define ALIGN_SECTIONS_IN_FILE
2319 #undef ALIGN_SECTIONS_IN_FILE
2323 coff_compute_section_file_positions (abfd
)
2327 asection
*previous
= (asection
*) NULL
;
2328 file_ptr sofar
= FILHSZ
;
2329 boolean align_adjust
;
2331 #ifdef ALIGN_SECTIONS_IN_FILE
2336 /* On XCOFF, if we have symbols, set up the .debug section. */
2337 if (bfd_get_symcount (abfd
) > 0)
2340 bfd_size_type i
, symcount
;
2344 symcount
= bfd_get_symcount (abfd
);
2345 for (symp
= abfd
->outsymbols
, i
= 0; i
< symcount
; symp
++, i
++)
2347 coff_symbol_type
*cf
;
2349 cf
= coff_symbol_from (abfd
, *symp
);
2351 && cf
->native
!= NULL
2352 && SYMNAME_IN_DEBUG (&cf
->native
->u
.syment
))
2356 len
= strlen (bfd_asymbol_name (*symp
));
2365 dsec
= bfd_make_section_old_way (abfd
, ".debug");
2368 dsec
->_raw_size
= sz
;
2369 dsec
->flags
|= SEC_HAS_CONTENTS
;
2374 #ifdef COFF_IMAGE_WITH_PE
2376 if (coff_data (abfd
)->link_info
)
2378 page_size
= pe_data (abfd
)->pe_opthdr
.FileAlignment
;
2381 page_size
= PE_DEF_FILE_ALIGNMENT
;
2383 #ifdef COFF_PAGE_SIZE
2384 int page_size
= COFF_PAGE_SIZE
;
2388 if (bfd_get_start_address (abfd
))
2390 /* A start address may have been added to the original file. In this
2391 case it will need an optional header to record it. */
2392 abfd
->flags
|= EXEC_P
;
2395 if (abfd
->flags
& EXEC_P
)
2398 else if (xcoff_data (abfd
)->full_aouthdr
)
2401 sofar
+= SMALL_AOUTSZ
;
2404 sofar
+= abfd
->section_count
* SCNHSZ
;
2407 /* XCOFF handles overflows in the reloc and line number count fields
2408 by allocating a new section header to hold the correct counts. */
2409 for (current
= abfd
->sections
; current
!= NULL
; current
= current
->next
)
2410 if (current
->reloc_count
>= 0xffff || current
->lineno_count
>= 0xffff)
2414 align_adjust
= false;
2415 for (current
= abfd
->sections
, count
= 1;
2416 current
!= (asection
*) NULL
;
2417 current
= current
->next
, ++count
)
2419 #ifdef COFF_IMAGE_WITH_PE
2420 /* The NT loader does not want empty section headers, so we omit
2421 them. We don't actually remove the section from the BFD,
2422 although we probably should. This matches code in
2423 coff_write_object_contents. */
2424 if (current
->_raw_size
== 0)
2426 current
->target_index
= -1;
2432 current
->target_index
= count
;
2434 /* Only deal with sections which have contents */
2435 if (!(current
->flags
& SEC_HAS_CONTENTS
))
2438 /* Align the sections in the file to the same boundary on
2439 which they are aligned in virtual memory. I960 doesn't
2440 do this (FIXME) so we can stay in sync with Intel. 960
2441 doesn't yet page from files... */
2442 #ifdef ALIGN_SECTIONS_IN_FILE
2443 if ((abfd
->flags
& EXEC_P
) != 0)
2445 /* make sure this section is aligned on the right boundary - by
2446 padding the previous section up if necessary */
2449 sofar
= BFD_ALIGN (sofar
, 1 << current
->alignment_power
);
2450 if (previous
!= (asection
*) NULL
)
2452 previous
->_raw_size
+= sofar
- old_sofar
;
2458 /* In demand paged files the low order bits of the file offset
2459 must match the low order bits of the virtual address. */
2460 #ifdef COFF_PAGE_SIZE
2461 if ((abfd
->flags
& D_PAGED
) != 0
2462 && (current
->flags
& SEC_ALLOC
) != 0)
2463 sofar
+= (current
->vma
- sofar
) % page_size
;
2465 current
->filepos
= sofar
;
2467 #ifdef COFF_IMAGE_WITH_PE
2468 /* With PE we have to pad each section to be a multiple of its
2469 page size too, and remember both sizes. */
2471 if (coff_section_data (abfd
, current
) == NULL
)
2473 current
->used_by_bfd
=
2474 (PTR
) bfd_zalloc (abfd
, sizeof (struct coff_section_tdata
));
2475 if (current
->used_by_bfd
== NULL
)
2478 if (pei_section_data (abfd
, current
) == NULL
)
2480 coff_section_data (abfd
, current
)->tdata
=
2481 (PTR
) bfd_zalloc (abfd
, sizeof (struct pei_section_tdata
));
2482 if (coff_section_data (abfd
, current
)->tdata
== NULL
)
2485 if (pei_section_data (abfd
, current
)->virt_size
== 0)
2486 pei_section_data (abfd
, current
)->virt_size
= current
->_raw_size
;
2488 current
->_raw_size
= (current
->_raw_size
+ page_size
-1) & -page_size
;
2491 sofar
+= current
->_raw_size
;
2493 #ifdef ALIGN_SECTIONS_IN_FILE
2494 /* make sure that this section is of the right size too */
2495 if ((abfd
->flags
& EXEC_P
) == 0)
2497 bfd_size_type old_size
;
2499 old_size
= current
->_raw_size
;
2500 current
->_raw_size
= BFD_ALIGN (current
->_raw_size
,
2501 1 << current
->alignment_power
);
2502 align_adjust
= current
->_raw_size
!= old_size
;
2503 sofar
+= current
->_raw_size
- old_size
;
2508 sofar
= BFD_ALIGN (sofar
, 1 << current
->alignment_power
);
2509 align_adjust
= sofar
!= old_sofar
;
2510 current
->_raw_size
+= sofar
- old_sofar
;
2514 #ifdef COFF_IMAGE_WITH_PE
2515 /* For PE we need to make sure we pad out to the aligned
2516 _raw_size, in case the caller only writes out data to the
2517 unaligned _raw_size. */
2518 if (pei_section_data (abfd
, current
)->virt_size
< current
->_raw_size
)
2519 align_adjust
= true;
2523 /* Force .lib sections to start at zero. The vma is then
2524 incremented in coff_set_section_contents. This is right for
2526 if (strcmp (current
->name
, _LIB
) == 0)
2527 bfd_set_section_vma (abfd
, current
, 0);
2533 /* It is now safe to write to the output file. If we needed an
2534 alignment adjustment for the last section, then make sure that
2535 there is a byte at offset sofar. If there are no symbols and no
2536 relocs, then nothing follows the last section. If we don't force
2537 the last byte out, then the file may appear to be truncated. */
2543 if (bfd_seek (abfd
, sofar
- 1, SEEK_SET
) != 0
2544 || bfd_write (&b
, 1, 1, abfd
) != 1)
2548 /* Make sure the relocations are aligned. We don't need to make
2549 sure that this byte exists, because it will only matter if there
2550 really are relocs. */
2551 sofar
= BFD_ALIGN (sofar
, 1 << COFF_DEFAULT_SECTION_ALIGNMENT_POWER
);
2553 obj_relocbase (abfd
) = sofar
;
2554 abfd
->output_has_begun
= true;
2561 /* This can never work, because it is called too late--after the
2562 section positions have been set. I can't figure out what it is
2563 for, so I am going to disable it--Ian Taylor 20 March 1996. */
2565 /* If .file, .text, .data, .bss symbols are missing, add them. */
2566 /* @@ Should we only be adding missing symbols, or overriding the aux
2567 values for existing section symbols? */
2569 coff_add_missing_symbols (abfd
)
2572 unsigned int nsyms
= bfd_get_symcount (abfd
);
2573 asymbol
**sympp
= abfd
->outsymbols
;
2576 int need_text
= 1, need_data
= 1, need_bss
= 1, need_file
= 1;
2578 for (i
= 0; i
< nsyms
; i
++)
2580 coff_symbol_type
*csym
= coff_symbol_from (abfd
, sympp
[i
]);
2584 /* only do this if there is a coff representation of the input
2586 if (csym
->native
&& csym
->native
->u
.syment
.n_sclass
== C_FILE
)
2591 name
= csym
->symbol
.name
;
2594 if (!strcmp (name
, _TEXT
))
2597 else if (!strcmp (name
, ".wtext"))
2600 else if (!strcmp (name
, _DATA
))
2602 else if (!strcmp (name
, _BSS
))
2606 /* Now i == bfd_get_symcount (abfd). */
2607 /* @@ For now, don't deal with .file symbol. */
2610 if (!need_text
&& !need_data
&& !need_bss
&& !need_file
)
2612 nsyms
+= need_text
+ need_data
+ need_bss
+ need_file
;
2613 sympp2
= (asymbol
**) bfd_alloc (abfd
, nsyms
* sizeof (asymbol
*));
2616 memcpy (sympp2
, sympp
, i
* sizeof (asymbol
*));
2619 /* @@ Generate fake .file symbol, in sympp2[i], and increment i. */
2623 sympp2
[i
++] = coff_section_symbol (abfd
, _TEXT
);
2625 sympp2
[i
++] = coff_section_symbol (abfd
, _DATA
);
2627 sympp2
[i
++] = coff_section_symbol (abfd
, _BSS
);
2628 BFD_ASSERT (i
== nsyms
);
2629 bfd_set_symtab (abfd
, sympp2
, nsyms
);
2638 coff_write_object_contents (abfd
)
2642 boolean hasrelocs
= false;
2643 boolean haslinno
= false;
2645 file_ptr reloc_base
;
2646 file_ptr lineno_base
;
2648 unsigned long reloc_size
= 0;
2649 unsigned long lnno_size
= 0;
2650 boolean long_section_names
;
2651 asection
*text_sec
= NULL
;
2652 asection
*data_sec
= NULL
;
2653 asection
*bss_sec
= NULL
;
2654 struct internal_filehdr internal_f
;
2655 struct internal_aouthdr internal_a
;
2656 #ifdef COFF_LONG_SECTION_NAMES
2657 size_t string_size
= STRING_SIZE_SIZE
;
2660 bfd_set_error (bfd_error_system_call
);
2662 /* Make a pass through the symbol table to count line number entries and
2663 put them into the correct asections */
2665 lnno_size
= coff_count_linenumbers (abfd
) * LINESZ
;
2667 if (abfd
->output_has_begun
== false)
2669 if (! coff_compute_section_file_positions (abfd
))
2673 reloc_base
= obj_relocbase (abfd
);
2675 /* Work out the size of the reloc and linno areas */
2677 for (current
= abfd
->sections
; current
!= NULL
; current
=
2679 reloc_size
+= current
->reloc_count
* RELSZ
;
2681 lineno_base
= reloc_base
+ reloc_size
;
2682 sym_base
= lineno_base
+ lnno_size
;
2684 /* Indicate in each section->line_filepos its actual file address */
2685 for (current
= abfd
->sections
; current
!= NULL
; current
=
2688 if (current
->lineno_count
)
2690 current
->line_filepos
= lineno_base
;
2691 current
->moving_line_filepos
= lineno_base
;
2692 lineno_base
+= current
->lineno_count
* LINESZ
;
2696 current
->line_filepos
= 0;
2698 if (current
->reloc_count
)
2700 current
->rel_filepos
= reloc_base
;
2701 reloc_base
+= current
->reloc_count
* RELSZ
;
2705 current
->rel_filepos
= 0;
2709 /* Write section headers to the file. */
2710 internal_f
.f_nscns
= 0;
2712 if ((abfd
->flags
& EXEC_P
) != 0)
2713 scn_base
= FILHSZ
+ AOUTSZ
;
2718 if (xcoff_data (abfd
)->full_aouthdr
)
2721 scn_base
+= SMALL_AOUTSZ
;
2725 if (bfd_seek (abfd
, scn_base
, SEEK_SET
) != 0)
2728 long_section_names
= false;
2729 for (current
= abfd
->sections
;
2731 current
= current
->next
)
2733 struct internal_scnhdr section
;
2736 /* If we've got a .reloc section, remember. */
2738 #ifdef COFF_IMAGE_WITH_PE
2739 if (strcmp (current
->name
, ".reloc") == 0)
2741 pe_data (abfd
)->has_reloc_section
= 1;
2746 internal_f
.f_nscns
++;
2748 strncpy (section
.s_name
, current
->name
, SCNNMLEN
);
2750 #ifdef COFF_LONG_SECTION_NAMES
2751 /* Handle long section names as in PE. This must be compatible
2752 with the code in coff_write_symbols and _bfd_coff_final_link. */
2756 len
= strlen (current
->name
);
2759 memset (section
.s_name
, 0, SCNNMLEN
);
2760 sprintf (section
.s_name
, "/%lu", (unsigned long) string_size
);
2761 string_size
+= len
+ 1;
2762 long_section_names
= true;
2768 /* Always set s_vaddr of .lib to 0. This is right for SVR3.2
2769 Ian Taylor <ian@cygnus.com>. */
2770 if (strcmp (current
->name
, _LIB
) == 0)
2771 section
.s_vaddr
= 0;
2774 section
.s_vaddr
= current
->vma
;
2775 section
.s_paddr
= current
->lma
;
2776 section
.s_size
= current
->_raw_size
;
2779 section
.s_paddr
= 0;
2781 #ifdef COFF_IMAGE_WITH_PE
2782 /* Reminder: s_paddr holds the virtual size of the section. */
2783 if (coff_section_data (abfd
, current
) != NULL
2784 && pei_section_data (abfd
, current
) != NULL
)
2785 section
.s_paddr
= pei_section_data (abfd
, current
)->virt_size
;
2787 section
.s_paddr
= 0;
2791 If this section has no size or is unloadable then the scnptr
2794 if (current
->_raw_size
== 0 ||
2795 (current
->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
2797 section
.s_scnptr
= 0;
2801 section
.s_scnptr
= current
->filepos
;
2803 section
.s_relptr
= current
->rel_filepos
;
2804 section
.s_lnnoptr
= current
->line_filepos
;
2805 section
.s_nreloc
= current
->reloc_count
;
2806 section
.s_nlnno
= current
->lineno_count
;
2807 if (current
->reloc_count
!= 0)
2809 if (current
->lineno_count
!= 0)
2813 /* Indicate the use of an XCOFF overflow section header. */
2814 if (current
->reloc_count
>= 0xffff || current
->lineno_count
>= 0xffff)
2816 section
.s_nreloc
= 0xffff;
2817 section
.s_nlnno
= 0xffff;
2821 section
.s_flags
= sec_to_styp_flags (current
->name
, current
->flags
);
2823 if (!strcmp (current
->name
, _TEXT
))
2827 else if (!strcmp (current
->name
, _DATA
))
2831 else if (!strcmp (current
->name
, _BSS
))
2837 section
.s_align
= (current
->alignment_power
2838 ? 1 << current
->alignment_power
2842 section
.s_flags
|= (current
->alignment_power
& 0xF) << 8;
2846 #ifdef COFF_IMAGE_WITH_PE
2847 /* Suppress output of the sections if they are null. ld
2848 includes the bss and data sections even if there is no size
2849 assigned to them. NT loader doesn't like it if these section
2850 headers are included if the sections themselves are not
2851 needed. See also coff_compute_section_file_positions. */
2852 if (section
.s_size
== 0)
2853 internal_f
.f_nscns
--;
2858 if (coff_swap_scnhdr_out (abfd
, §ion
, &buff
) == 0
2859 || bfd_write ((PTR
) (&buff
), 1, SCNHSZ
, abfd
) != SCNHSZ
)
2864 /* PE stores COMDAT section information in the symbol table. If
2865 this section is supposed to have some COMDAT info, track down
2866 the symbol in the symbol table and modify it. */
2867 if ((current
->flags
& SEC_LINK_ONCE
) != 0)
2869 unsigned int i
, count
;
2871 coff_symbol_type
*csym
= NULL
;
2875 count
= bfd_get_symcount (abfd
);
2876 for (i
= 0, psym
= abfd
->outsymbols
; i
< count
; i
++, psym
++)
2878 if ((*psym
)->section
!= current
)
2881 /* Remember the location of the first symbol in this
2883 if (psymsec
== NULL
)
2886 /* See if this is the section symbol. */
2887 if (strcmp ((*psym
)->name
, current
->name
) == 0)
2889 csym
= coff_symbol_from (abfd
, *psym
);
2891 || csym
->native
== NULL
2892 || csym
->native
->u
.syment
.n_numaux
< 1
2893 || csym
->native
->u
.syment
.n_sclass
!= C_STAT
2894 || csym
->native
->u
.syment
.n_type
!= T_NULL
)
2897 /* Here *PSYM is the section symbol for CURRENT. */
2904 Note that we might not if we're converting the file from
2905 some other object file format. */
2908 combined_entry_type
*aux
;
2910 /* We don't touch the x_checksum field. The
2911 x_associated field is not currently supported. */
2913 aux
= csym
->native
+ 1;
2914 switch (current
->flags
& SEC_LINK_DUPLICATES
)
2916 case SEC_LINK_DUPLICATES_DISCARD
:
2917 aux
->u
.auxent
.x_scn
.x_comdat
= IMAGE_COMDAT_SELECT_ANY
;
2920 case SEC_LINK_DUPLICATES_ONE_ONLY
:
2921 aux
->u
.auxent
.x_scn
.x_comdat
=
2922 IMAGE_COMDAT_SELECT_NODUPLICATES
;
2925 case SEC_LINK_DUPLICATES_SAME_SIZE
:
2926 aux
->u
.auxent
.x_scn
.x_comdat
=
2927 IMAGE_COMDAT_SELECT_SAME_SIZE
;
2930 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
2931 aux
->u
.auxent
.x_scn
.x_comdat
=
2932 IMAGE_COMDAT_SELECT_EXACT_MATCH
;
2936 /* The COMDAT symbol must be the first symbol from this
2937 section in the symbol table. In order to make this
2938 work, we move the COMDAT symbol before the first
2939 symbol we found in the search above. It's OK to
2940 rearrange the symbol table at this point, because
2941 coff_renumber_symbols is going to rearrange it
2942 further and fix up all the aux entries. */
2943 if (psym
!= psymsec
)
2949 for (pcopy
= psym
; pcopy
> psymsec
; pcopy
--)
2950 pcopy
[0] = pcopy
[-1];
2955 #endif /* COFF_WITH_PE */
2959 /* XCOFF handles overflows in the reloc and line number count fields
2960 by creating a new section header to hold the correct values. */
2961 for (current
= abfd
->sections
; current
!= NULL
; current
= current
->next
)
2963 if (current
->reloc_count
>= 0xffff || current
->lineno_count
>= 0xffff)
2965 struct internal_scnhdr scnhdr
;
2968 internal_f
.f_nscns
++;
2969 strncpy (&(scnhdr
.s_name
[0]), current
->name
, 8);
2970 scnhdr
.s_paddr
= current
->reloc_count
;
2971 scnhdr
.s_vaddr
= current
->lineno_count
;
2973 scnhdr
.s_scnptr
= 0;
2974 scnhdr
.s_relptr
= current
->rel_filepos
;
2975 scnhdr
.s_lnnoptr
= current
->line_filepos
;
2976 scnhdr
.s_nreloc
= current
->target_index
;
2977 scnhdr
.s_nlnno
= current
->target_index
;
2978 scnhdr
.s_flags
= STYP_OVRFLO
;
2979 if (coff_swap_scnhdr_out (abfd
, &scnhdr
, &buff
) == 0
2980 || bfd_write ((PTR
) &buff
, 1, SCNHSZ
, abfd
) != SCNHSZ
)
2986 /* OK, now set up the filehdr... */
2988 /* Don't include the internal abs section in the section count */
2991 We will NOT put a fucking timestamp in the header here. Every time you
2992 put it back, I will come in and take it out again. I'm sorry. This
2993 field does not belong here. We fill it with a 0 so it compares the
2994 same but is not a reasonable time. -- gnu@cygnus.com
2996 internal_f
.f_timdat
= 0;
2998 internal_f
.f_flags
= 0;
3000 if (abfd
->flags
& EXEC_P
)
3001 internal_f
.f_opthdr
= AOUTSZ
;
3004 internal_f
.f_opthdr
= 0;
3006 if (xcoff_data (abfd
)->full_aouthdr
)
3007 internal_f
.f_opthdr
= AOUTSZ
;
3009 internal_f
.f_opthdr
= SMALL_AOUTSZ
;
3014 internal_f
.f_flags
|= F_RELFLG
;
3016 internal_f
.f_flags
|= F_LNNO
;
3017 if (abfd
->flags
& EXEC_P
)
3018 internal_f
.f_flags
|= F_EXEC
;
3020 /* FIXME: this is wrong for PPC_PE! */
3021 if (bfd_little_endian (abfd
))
3022 internal_f
.f_flags
|= F_AR32WR
;
3024 internal_f
.f_flags
|= F_AR32W
;
3026 #ifdef TIC80_TARGET_ID
3027 internal_f
.f_target_id
= TIC80_TARGET_ID
;
3031 FIXME, should do something about the other byte orders and
3036 if ((abfd
->flags
& DYNAMIC
) != 0)
3037 internal_f
.f_flags
|= F_SHROBJ
;
3038 if (bfd_get_section_by_name (abfd
, _LOADER
) != NULL
)
3039 internal_f
.f_flags
|= F_DYNLOAD
;
3042 memset (&internal_a
, 0, sizeof internal_a
);
3044 /* Set up architecture-dependent stuff */
3047 unsigned int magic
= 0;
3048 unsigned short flags
= 0;
3049 coff_set_flags (abfd
, &magic
, &flags
);
3050 internal_f
.f_magic
= magic
;
3051 internal_f
.f_flags
|= flags
;
3052 /* ...and the "opt"hdr... */
3055 #ifdef ULTRA3 /* NYU's machine */
3056 /* FIXME: This is a bogus check. I really want to see if there
3057 * is a .shbss or a .shdata section, if so then set the magic
3058 * number to indicate a shared data executable.
3060 if (internal_f
.f_nscns
>= 7)
3061 internal_a
.magic
= SHMAGIC
; /* Shared magic */
3064 internal_a
.magic
= NMAGIC
; /* Assume separate i/d */
3065 #define __A_MAGIC_SET__
3068 internal_a
.magic
= TIC80_ARCH_MAGIC
;
3069 #define __A_MAGIC_SET__
3072 /* FIXME: What are the a.out magic numbers for the i860? */
3073 internal_a
.magic
= 0;
3074 #define __A_MAGIC_SET__
3077 internal_a
.magic
= (magic
== I960ROMAGIC
? NMAGIC
: OMAGIC
);
3078 #define __A_MAGIC_SET__
3081 #define __A_MAGIC_SET__
3082 internal_a
.magic
= PAGEMAGICBCS
;
3086 #define __A_MAGIC_SET__
3087 internal_a
.magic
= APOLLO_COFF_VERSION_NUMBER
;
3090 #if defined(M68) || defined(WE32K) || defined(M68K)
3091 #define __A_MAGIC_SET__
3093 internal_a
.magic
= LYNXCOFFMAGIC
;
3095 #if defined(TARG_AUX)
3096 internal_a
.magic
= (abfd
->flags
& D_PAGED
? PAGEMAGICPEXECPAGED
:
3097 abfd
->flags
& WP_TEXT
? PAGEMAGICPEXECSWAPPED
:
3098 PAGEMAGICEXECSWAPPED
);
3100 #if defined (PAGEMAGICPEXECPAGED)
3101 internal_a
.magic
= PAGEMAGICPEXECPAGED
;
3103 #endif /* TARG_AUX */
3105 #endif /* M68 || WE32K || M68K */
3108 #define __A_MAGIC_SET__
3109 internal_a
.magic
= ZMAGIC
;
3113 #define __A_MAGIC_SET__
3114 internal_a
.magic
= IMAGE_NT_OPTIONAL_HDR_MAGIC
;
3117 #if defined MCORE_PE
3118 #define __A_MAGIC_SET__
3119 internal_a
.magic
= IMAGE_NT_OPTIONAL_HDR_MAGIC
;
3123 #define __A_MAGIC_SET__
3125 internal_a
.magic
= LYNXCOFFMAGIC
;
3127 internal_a
.magic
= ZMAGIC
;
3132 #define __A_MAGIC_SET__
3134 internal_a
.magic
= LYNXCOFFMAGIC
;
3139 #define __A_MAGIC_SET__
3140 internal_a
.magic
= (abfd
->flags
& D_PAGED
) ? RS6K_AOUTHDR_ZMAGIC
:
3141 (abfd
->flags
& WP_TEXT
) ? RS6K_AOUTHDR_NMAGIC
:
3142 RS6K_AOUTHDR_OMAGIC
;
3145 #ifndef __A_MAGIC_SET__
3146 #include "Your aouthdr magic number is not being set!"
3148 #undef __A_MAGIC_SET__
3152 /* FIXME: Does anybody ever set this to another value? */
3153 internal_a
.vstamp
= 0;
3155 /* Now should write relocs, strings, syms */
3156 obj_sym_filepos (abfd
) = sym_base
;
3158 if (bfd_get_symcount (abfd
) != 0)
3162 if (!coff_add_missing_symbols (abfd
))
3165 if (!coff_renumber_symbols (abfd
, &firstundef
))
3167 coff_mangle_symbols (abfd
);
3168 if (! coff_write_symbols (abfd
))
3170 if (! coff_write_linenumbers (abfd
))
3172 if (! coff_write_relocs (abfd
, firstundef
))
3175 #ifdef COFF_LONG_SECTION_NAMES
3176 else if (long_section_names
)
3178 /* If we have long section names we have to write out the string
3179 table even if there are no symbols. */
3180 if (! coff_write_symbols (abfd
))
3184 #ifdef COFF_IMAGE_WITH_PE
3186 else if ((abfd
->flags
& EXEC_P
) != 0)
3190 /* PowerPC PE appears to require that all executable files be
3191 rounded up to the page size. */
3194 BFD_ALIGN (sym_base
, COFF_PAGE_SIZE
) - 1,
3196 || bfd_write (&b
, 1, 1, abfd
) != 1)
3202 /* If bfd_get_symcount (abfd) != 0, then we are not using the COFF
3203 backend linker, and obj_raw_syment_count is not valid until after
3204 coff_write_symbols is called. */
3205 if (obj_raw_syment_count (abfd
) != 0)
3207 internal_f
.f_symptr
= sym_base
;
3209 /* AIX appears to require that F_RELFLG not be set if there are
3210 local symbols but no relocations. */
3211 internal_f
.f_flags
&=~ F_RELFLG
;
3216 if (long_section_names
)
3217 internal_f
.f_symptr
= sym_base
;
3219 internal_f
.f_symptr
= 0;
3220 internal_f
.f_flags
|= F_LSYMS
;
3225 internal_a
.tsize
= bfd_get_section_size_before_reloc (text_sec
);
3226 internal_a
.text_start
= internal_a
.tsize
? text_sec
->vma
: 0;
3230 internal_a
.dsize
= bfd_get_section_size_before_reloc (data_sec
);
3231 internal_a
.data_start
= internal_a
.dsize
? data_sec
->vma
: 0;
3235 internal_a
.bsize
= bfd_get_section_size_before_reloc (bss_sec
);
3236 if (internal_a
.bsize
&& bss_sec
->vma
< internal_a
.data_start
)
3237 internal_a
.data_start
= bss_sec
->vma
;
3240 internal_a
.entry
= bfd_get_start_address (abfd
);
3241 internal_f
.f_nsyms
= obj_raw_syment_count (abfd
);
3244 if (xcoff_data (abfd
)->full_aouthdr
)
3247 asection
*loader_sec
;
3249 internal_a
.vstamp
= 1;
3251 internal_a
.o_snentry
= xcoff_data (abfd
)->snentry
;
3252 if (internal_a
.o_snentry
== 0)
3253 internal_a
.entry
= (bfd_vma
) -1;
3255 if (text_sec
!= NULL
)
3257 internal_a
.o_sntext
= text_sec
->target_index
;
3258 internal_a
.o_algntext
= bfd_get_section_alignment (abfd
, text_sec
);
3262 internal_a
.o_sntext
= 0;
3263 internal_a
.o_algntext
= 0;
3265 if (data_sec
!= NULL
)
3267 internal_a
.o_sndata
= data_sec
->target_index
;
3268 internal_a
.o_algndata
= bfd_get_section_alignment (abfd
, data_sec
);
3272 internal_a
.o_sndata
= 0;
3273 internal_a
.o_algndata
= 0;
3275 loader_sec
= bfd_get_section_by_name (abfd
, ".loader");
3276 if (loader_sec
!= NULL
)
3277 internal_a
.o_snloader
= loader_sec
->target_index
;
3279 internal_a
.o_snloader
= 0;
3280 if (bss_sec
!= NULL
)
3281 internal_a
.o_snbss
= bss_sec
->target_index
;
3283 internal_a
.o_snbss
= 0;
3285 toc
= xcoff_data (abfd
)->toc
;
3286 internal_a
.o_toc
= toc
;
3287 internal_a
.o_sntoc
= xcoff_data (abfd
)->sntoc
;
3289 internal_a
.o_modtype
= xcoff_data (abfd
)->modtype
;
3290 if (xcoff_data (abfd
)->cputype
!= -1)
3291 internal_a
.o_cputype
= xcoff_data (abfd
)->cputype
;
3294 switch (bfd_get_arch (abfd
))
3296 case bfd_arch_rs6000
:
3297 internal_a
.o_cputype
= 4;
3299 case bfd_arch_powerpc
:
3300 if (bfd_get_mach (abfd
) == 0)
3301 internal_a
.o_cputype
= 3;
3303 internal_a
.o_cputype
= 1;
3309 internal_a
.o_maxstack
= xcoff_data (abfd
)->maxstack
;
3310 internal_a
.o_maxdata
= xcoff_data (abfd
)->maxdata
;
3314 /* now write them */
3315 if (bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
) != 0)
3319 coff_swap_filehdr_out (abfd
, (PTR
) & internal_f
, (PTR
) buff
);
3320 if (bfd_write ((PTR
) buff
, 1, FILHSZ
, abfd
) != FILHSZ
)
3323 if (abfd
->flags
& EXEC_P
)
3325 /* Note that peicode.h fills in a PEAOUTHDR, not an AOUTHDR.
3326 include/coff/pe.h sets AOUTSZ == sizeof(PEAOUTHDR)) */
3328 coff_swap_aouthdr_out (abfd
, (PTR
) & internal_a
, (PTR
) buff
);
3329 if (bfd_write ((PTR
) buff
, 1, AOUTSZ
, abfd
) != AOUTSZ
)
3338 /* XCOFF seems to always write at least a small a.out header. */
3339 coff_swap_aouthdr_out (abfd
, (PTR
) &internal_a
, (PTR
) &buff
);
3340 if (xcoff_data (abfd
)->full_aouthdr
)
3343 size
= SMALL_AOUTSZ
;
3344 if (bfd_write ((PTR
) &buff
, 1, size
, abfd
) != size
)
3353 coff_set_section_contents (abfd
, section
, location
, offset
, count
)
3358 bfd_size_type count
;
3360 if (abfd
->output_has_begun
== false) /* set by bfd.c handler */
3362 if (! coff_compute_section_file_positions (abfd
))
3366 #if defined(_LIB) && !defined(TARG_AUX)
3368 /* The physical address field of a .lib section is used to hold the
3369 number of shared libraries in the section. This code counts the
3370 number of sections being written, and increments the lma field
3373 I have found no documentation on the contents of this section.
3374 Experimentation indicates that the section contains zero or more
3375 records, each of which has the following structure:
3377 - a (four byte) word holding the length of this record, in words,
3378 - a word that always seems to be set to "2",
3379 - the path to a shared library, null-terminated and then padded
3380 to a whole word boundary.
3382 bfd_assert calls have been added to alert if an attempt is made
3383 to write a section which doesn't follow these assumptions. The
3384 code has been tested on ISC 4.1 by me, and on SCO by Robert Lipe
3385 <robertl@arnet.com> (Thanks!).
3387 Gvran Uddeborg <gvran@uddeborg.pp.se> */
3389 if (strcmp (section
->name
, _LIB
) == 0)
3391 bfd_byte
*rec
, *recend
;
3393 rec
= (bfd_byte
*) location
;
3394 recend
= rec
+ count
;
3395 while (rec
< recend
)
3398 rec
+= bfd_get_32 (abfd
, rec
) * 4;
3401 BFD_ASSERT (rec
== recend
);
3406 /* Don't write out bss sections - one way to do this is to
3407 see if the filepos has not been set. */
3408 if (section
->filepos
== 0)
3411 if (bfd_seek (abfd
, (file_ptr
) (section
->filepos
+ offset
), SEEK_SET
) != 0)
3416 return (bfd_write (location
, 1, count
, abfd
) == count
) ? true : false;
3422 coff_close_and_cleanup (abfd
)
3425 if (!bfd_read_p (abfd
))
3426 switch (abfd
->format
)
3429 if (!_bfd_write_archive_contents (abfd
))
3433 if (!coff_write_object_contents (abfd
))
3437 bfd_set_error (bfd_error_invalid_operation
);
3441 /* We depend on bfd_close to free all the memory on the objalloc. */
3448 buy_and_read (abfd
, where
, seek_direction
, size
)
3454 PTR area
= (PTR
) bfd_alloc (abfd
, size
);
3457 if (bfd_seek (abfd
, where
, seek_direction
) != 0
3458 || bfd_read (area
, 1, size
, abfd
) != size
)
3461 } /* buy_and_read() */
3467 Creating the linenumber table is done by reading in the entire
3468 coff linenumber table, and creating another table for internal use.
3470 A coff linenumber table is structured so that each function
3471 is marked as having a line number of 0. Each line within the
3472 function is an offset from the first line in the function. The
3473 base of the line number information for the table is stored in
3474 the symbol associated with the function.
3476 Note: The PE format uses line number 0 for a flag indicating a
3479 The information is copied from the external to the internal
3480 table, and each symbol which marks a function is marked by
3483 How does this work ?
3488 coff_slurp_line_table (abfd
, asect
)
3492 LINENO
*native_lineno
;
3493 alent
*lineno_cache
;
3495 BFD_ASSERT (asect
->lineno
== (alent
*) NULL
);
3497 native_lineno
= (LINENO
*) buy_and_read (abfd
,
3498 asect
->line_filepos
,
3501 asect
->lineno_count
));
3503 (alent
*) bfd_alloc (abfd
, (size_t) ((asect
->lineno_count
+ 1) * sizeof (alent
)));
3504 if (lineno_cache
== NULL
)
3508 unsigned int counter
= 0;
3509 alent
*cache_ptr
= lineno_cache
;
3510 LINENO
*src
= native_lineno
;
3512 while (counter
< asect
->lineno_count
)
3514 struct internal_lineno dst
;
3515 coff_swap_lineno_in (abfd
, src
, &dst
);
3516 cache_ptr
->line_number
= dst
.l_lnno
;
3518 if (cache_ptr
->line_number
== 0)
3522 coff_symbol_type
*sym
;
3525 symndx
= dst
.l_addr
.l_symndx
;
3527 || (unsigned long) symndx
>= obj_raw_syment_count (abfd
))
3529 (*_bfd_error_handler
)
3530 (_("%s: warning: illegal symbol index %ld in line numbers"),
3531 bfd_get_filename (abfd
), dst
.l_addr
.l_symndx
);
3535 /* FIXME: We should not be casting between ints and
3536 pointers like this. */
3537 sym
= ((coff_symbol_type
*)
3538 ((symndx
+ obj_raw_syments (abfd
))
3539 ->u
.syment
._n
._n_n
._n_zeroes
));
3540 cache_ptr
->u
.sym
= (asymbol
*) sym
;
3541 if (sym
->lineno
!= NULL
&& ! warned
)
3543 (*_bfd_error_handler
)
3544 (_("%s: warning: duplicate line number information for `%s'"),
3545 bfd_get_filename (abfd
),
3546 bfd_asymbol_name (&sym
->symbol
));
3548 sym
->lineno
= cache_ptr
;
3552 cache_ptr
->u
.offset
= dst
.l_addr
.l_paddr
3553 - bfd_section_vma (abfd
, asect
);
3554 } /* If no linenumber expect a symbol index */
3560 cache_ptr
->line_number
= 0;
3563 asect
->lineno
= lineno_cache
;
3564 /* FIXME, free native_lineno here, or use alloca or something. */
3568 /* Slurp in the symbol table, converting it to generic form. Note
3569 that if coff_relocate_section is defined, the linker will read
3570 symbols via coff_link_add_symbols, rather than via this routine. */
3573 coff_slurp_symbol_table (abfd
)
3576 combined_entry_type
*native_symbols
;
3577 coff_symbol_type
*cached_area
;
3578 unsigned int *table_ptr
;
3580 unsigned int number_of_symbols
= 0;
3582 if (obj_symbols (abfd
))
3585 /* Read in the symbol table */
3586 if ((native_symbols
= coff_get_normalized_symtab (abfd
)) == NULL
)
3591 /* Allocate enough room for all the symbols in cached form */
3592 cached_area
= ((coff_symbol_type
*)
3594 (obj_raw_syment_count (abfd
)
3595 * sizeof (coff_symbol_type
))));
3597 if (cached_area
== NULL
)
3599 table_ptr
= ((unsigned int *)
3601 (obj_raw_syment_count (abfd
)
3602 * sizeof (unsigned int))));
3604 if (table_ptr
== NULL
)
3608 coff_symbol_type
*dst
= cached_area
;
3609 unsigned int last_native_index
= obj_raw_syment_count (abfd
);
3610 unsigned int this_index
= 0;
3611 while (this_index
< last_native_index
)
3613 combined_entry_type
*src
= native_symbols
+ this_index
;
3614 table_ptr
[this_index
] = number_of_symbols
;
3615 dst
->symbol
.the_bfd
= abfd
;
3617 dst
->symbol
.name
= (char *) (src
->u
.syment
._n
._n_n
._n_offset
);
3618 /* We use the native name field to point to the cached field. */
3619 src
->u
.syment
._n
._n_n
._n_zeroes
= (long) dst
;
3620 dst
->symbol
.section
= coff_section_from_bfd_index (abfd
,
3621 src
->u
.syment
.n_scnum
);
3622 dst
->symbol
.flags
= 0;
3623 dst
->done_lineno
= false;
3625 switch (src
->u
.syment
.n_sclass
)
3630 dst
->symbol
.value
= src
->u
.syment
.n_value
- dst
->symbol
.section
->vma
;
3631 dst
->symbol
.flags
= BSF_EXPORT
| BSF_GLOBAL
;
3632 dst
->symbol
.flags
|= BSF_NOT_AT_END
| BSF_FUNCTION
;
3634 /* Fall through to next case */
3642 case C_THUMBEXTFUNC
:
3648 case C_SYSTEM
: /* System Wide variable */
3651 /* In PE, 0x68 (104) denotes a section symbol */
3653 /* In PE, 0x69 (105) denotes a weak external symbol. */
3656 switch (coff_classify_symbol (abfd
, &src
->u
.syment
))
3658 case COFF_SYMBOL_GLOBAL
:
3659 dst
->symbol
.flags
= BSF_EXPORT
| BSF_GLOBAL
;
3660 #if defined COFF_WITH_PE
3661 /* PE sets the symbol to a value relative to the
3662 start of the section. */
3663 dst
->symbol
.value
= src
->u
.syment
.n_value
;
3665 dst
->symbol
.value
= (src
->u
.syment
.n_value
3666 - dst
->symbol
.section
->vma
);
3668 if (ISFCN ((src
->u
.syment
.n_type
)))
3670 /* A function ext does not go at the end of a
3672 dst
->symbol
.flags
|= BSF_NOT_AT_END
| BSF_FUNCTION
;
3676 case COFF_SYMBOL_COMMON
:
3677 dst
->symbol
.section
= bfd_com_section_ptr
;
3678 dst
->symbol
.value
= src
->u
.syment
.n_value
;
3681 case COFF_SYMBOL_UNDEFINED
:
3682 dst
->symbol
.section
= bfd_und_section_ptr
;
3683 dst
->symbol
.value
= 0;
3686 case COFF_SYMBOL_PE_SECTION
:
3687 dst
->symbol
.flags
|= BSF_EXPORT
| BSF_SECTION_SYM
;
3688 dst
->symbol
.value
= 0;
3691 case COFF_SYMBOL_LOCAL
:
3692 dst
->symbol
.flags
= BSF_LOCAL
;
3693 #if defined COFF_WITH_PE
3694 /* PE sets the symbol to a value relative to the
3695 start of the section. */
3696 dst
->symbol
.value
= src
->u
.syment
.n_value
;
3698 dst
->symbol
.value
= (src
->u
.syment
.n_value
3699 - dst
->symbol
.section
->vma
);
3701 if (ISFCN ((src
->u
.syment
.n_type
)))
3702 dst
->symbol
.flags
|= BSF_NOT_AT_END
| BSF_FUNCTION
;
3707 /* A symbol with a csect entry should not go at the end. */
3708 if (src
->u
.syment
.n_numaux
> 0)
3709 dst
->symbol
.flags
|= BSF_NOT_AT_END
;
3713 if (src
->u
.syment
.n_sclass
== C_NT_WEAK
)
3714 dst
->symbol
.flags
= BSF_WEAK
;
3715 if (src
->u
.syment
.n_sclass
== C_SECTION
3716 && src
->u
.syment
.n_scnum
> 0)
3718 dst
->symbol
.flags
= BSF_LOCAL
;
3722 if (src
->u
.syment
.n_sclass
== C_WEAKEXT
)
3723 dst
->symbol
.flags
= BSF_WEAK
;
3727 case C_STAT
: /* static */
3729 case C_LEAFSTAT
: /* static leaf procedure */
3732 case C_THUMBSTAT
: /* Thumb static */
3733 case C_THUMBLABEL
: /* Thumb label */
3734 case C_THUMBSTATFUNC
:/* Thumb static function */
3736 case C_LABEL
: /* label */
3737 if (src
->u
.syment
.n_scnum
== N_DEBUG
)
3738 dst
->symbol
.flags
= BSF_DEBUGGING
;
3740 dst
->symbol
.flags
= BSF_LOCAL
;
3742 /* Base the value as an index from the base of the
3743 section, if there is one. */
3744 if (dst
->symbol
.section
)
3746 #if defined COFF_WITH_PE
3747 /* PE sets the symbol to a value relative to the
3748 start of the section. */
3749 dst
->symbol
.value
= src
->u
.syment
.n_value
;
3751 dst
->symbol
.value
= (src
->u
.syment
.n_value
3752 - dst
->symbol
.section
->vma
);
3756 dst
->symbol
.value
= src
->u
.syment
.n_value
;
3759 case C_MOS
: /* member of structure */
3760 case C_EOS
: /* end of structure */
3761 #ifdef NOTDEF /* C_AUTOARG has the same value */
3763 case C_GLBLREG
: /* A29k-specific storage class */
3766 case C_REGPARM
: /* register parameter */
3767 case C_REG
: /* register variable */
3770 case C_AUTOARG
: /* 960-specific storage class */
3773 case C_TPDEF
: /* type definition */
3775 case C_AUTO
: /* automatic variable */
3776 case C_FIELD
: /* bit field */
3777 case C_ENTAG
: /* enumeration tag */
3778 case C_MOE
: /* member of enumeration */
3779 case C_MOU
: /* member of union */
3780 case C_UNTAG
: /* union tag */
3781 dst
->symbol
.flags
= BSF_DEBUGGING
;
3782 dst
->symbol
.value
= (src
->u
.syment
.n_value
);
3785 case C_FILE
: /* file name */
3786 case C_STRTAG
: /* structure tag */
3801 dst
->symbol
.flags
= BSF_DEBUGGING
;
3802 dst
->symbol
.value
= (src
->u
.syment
.n_value
);
3806 case C_BINCL
: /* beginning of include file */
3807 case C_EINCL
: /* ending of include file */
3808 /* The value is actually a pointer into the line numbers
3809 of the file. We locate the line number entry, and
3810 set the section to the section which contains it, and
3811 the value to the index in that section. */
3815 dst
->symbol
.flags
= BSF_DEBUGGING
;
3816 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3817 if (sec
->line_filepos
<= (file_ptr
) src
->u
.syment
.n_value
3818 && ((file_ptr
) (sec
->line_filepos
3819 + sec
->lineno_count
* LINESZ
)
3820 > (file_ptr
) src
->u
.syment
.n_value
))
3823 dst
->symbol
.value
= 0;
3826 dst
->symbol
.section
= sec
;
3827 dst
->symbol
.value
= ((src
->u
.syment
.n_value
3828 - sec
->line_filepos
)
3836 dst
->symbol
.flags
= BSF_DEBUGGING
;
3838 /* The value is actually a symbol index. Save a pointer
3839 to the symbol instead of the index. FIXME: This
3840 should use a union. */
3841 src
->u
.syment
.n_value
=
3842 (long) (native_symbols
+ src
->u
.syment
.n_value
);
3843 dst
->symbol
.value
= src
->u
.syment
.n_value
;
3848 case C_BLOCK
: /* ".bb" or ".eb" */
3849 case C_FCN
: /* ".bf" or ".ef" */
3850 case C_EFCN
: /* physical end of function */
3851 dst
->symbol
.flags
= BSF_LOCAL
;
3852 #if defined COFF_WITH_PE
3853 /* PE sets the symbol to a value relative to the start
3855 dst
->symbol
.value
= src
->u
.syment
.n_value
;
3857 /* Base the value as an index from the base of the
3859 dst
->symbol
.value
= (src
->u
.syment
.n_value
3860 - dst
->symbol
.section
->vma
);
3865 /* PE DLLs sometimes have zeroed out symbols for some
3866 reason. Just ignore them without a warning. */
3867 if (src
->u
.syment
.n_type
== 0
3868 && src
->u
.syment
.n_value
== 0
3869 && src
->u
.syment
.n_scnum
== 0)
3872 case C_EXTDEF
: /* external definition */
3873 case C_ULABEL
: /* undefined label */
3874 case C_USTATIC
: /* undefined static */
3875 #ifndef COFF_WITH_PE
3876 /* C_LINE in regular coff is 0x68. NT has taken over this storage
3877 class to represent a section symbol */
3878 case C_LINE
: /* line # reformatted as symbol table entry */
3879 /* NT uses 0x67 for a weak symbol, not C_ALIAS. */
3880 case C_ALIAS
: /* duplicate tag */
3882 /* New storage classes for TIc80 */
3884 case C_UEXT
: /* Tentative external definition */
3886 case C_STATLAB
: /* Static load time label */
3887 case C_EXTLAB
: /* External load time label */
3888 case C_HIDDEN
: /* ext symbol in dmert public lib */
3890 (*_bfd_error_handler
)
3891 (_("%s: Unrecognized storage class %d for %s symbol `%s'"),
3892 bfd_get_filename (abfd
), src
->u
.syment
.n_sclass
,
3893 dst
->symbol
.section
->name
, dst
->symbol
.name
);
3894 dst
->symbol
.flags
= BSF_DEBUGGING
;
3895 dst
->symbol
.value
= (src
->u
.syment
.n_value
);
3899 /* BFD_ASSERT(dst->symbol.flags != 0);*/
3903 dst
->symbol
.udata
.i
= 0;
3904 dst
->lineno
= (alent
*) NULL
;
3905 this_index
+= (src
->u
.syment
.n_numaux
) + 1;
3907 number_of_symbols
++;
3908 } /* walk the native symtab */
3909 } /* bfdize the native symtab */
3911 obj_symbols (abfd
) = cached_area
;
3912 obj_raw_syments (abfd
) = native_symbols
;
3914 bfd_get_symcount (abfd
) = number_of_symbols
;
3915 obj_convert (abfd
) = table_ptr
;
3916 /* Slurp the line tables for each section too */
3922 coff_slurp_line_table (abfd
, p
);
3927 } /* coff_slurp_symbol_table() */
3929 /* Classify a COFF symbol. A couple of targets have globally visible
3930 symbols which are not class C_EXT, and this handles those. It also
3931 recognizes some special PE cases. */
3933 static enum coff_symbol_classification
3934 coff_classify_symbol (abfd
, syment
)
3936 struct internal_syment
*syment
;
3938 /* FIXME: This partially duplicates the switch in
3939 coff_slurp_symbol_table. */
3940 switch (syment
->n_sclass
)
3949 case C_THUMBEXTFUNC
:
3957 if (syment
->n_scnum
== 0)
3959 if (syment
->n_value
== 0)
3960 return COFF_SYMBOL_UNDEFINED
;
3962 return COFF_SYMBOL_COMMON
;
3964 return COFF_SYMBOL_GLOBAL
;
3971 if (syment
->n_sclass
== C_STAT
)
3973 if (syment
->n_scnum
== 0)
3975 /* The Microsoft compiler sometimes generates these if a
3976 small static function is inlined every time it is used.
3977 The function is discarded, but the symbol table entry
3979 return COFF_SYMBOL_LOCAL
;
3983 /* This is correct for Microsoft generated objects, but it
3984 breaks gas generated objects. */
3986 if (syment
->n_value
== 0)
3989 char buf
[SYMNMLEN
+ 1];
3991 sec
= coff_section_from_bfd_index (abfd
, syment
->n_scnum
);
3993 && (strcmp (bfd_get_section_name (abfd
, sec
),
3994 _bfd_coff_internal_syment_name (abfd
, syment
, buf
))
3996 return COFF_SYMBOL_PE_SECTION
;
4000 return COFF_SYMBOL_LOCAL
;
4003 if (syment
->n_sclass
== C_SECTION
)
4005 /* In some cases in a DLL generated by the Microsoft linker, the
4006 n_value field will contain garbage. FIXME: This should
4007 probably be handled by the swapping function instead. */
4008 syment
->n_value
= 0;
4009 if (syment
->n_scnum
== 0)
4010 return COFF_SYMBOL_UNDEFINED
;
4011 return COFF_SYMBOL_PE_SECTION
;
4013 #endif /* COFF_WITH_PE */
4015 /* If it is not a global symbol, we presume it is a local symbol. */
4017 if (syment
->n_scnum
== 0)
4019 char buf
[SYMNMLEN
+ 1];
4021 (*_bfd_error_handler
)
4022 (_("warning: %s: local symbol `%s' has no section"),
4023 bfd_get_filename (abfd
),
4024 _bfd_coff_internal_syment_name (abfd
, syment
, buf
));
4027 return COFF_SYMBOL_LOCAL
;
4034 Coff relocations are easily transformed into the internal BFD form
4037 Reading a coff relocation table is done in the following stages:
4039 o Read the entire coff relocation table into memory.
4041 o Process each relocation in turn; first swap it from the
4042 external to the internal form.
4044 o Turn the symbol referenced in the relocation's symbol index
4045 into a pointer into the canonical symbol table.
4046 This table is the same as the one returned by a call to
4047 @code{bfd_canonicalize_symtab}. The back end will call that
4048 routine and save the result if a canonicalization hasn't been done.
4050 o The reloc index is turned into a pointer to a howto
4051 structure, in a back end specific way. For instance, the 386
4052 and 960 use the @code{r_type} to directly produce an index
4053 into a howto table vector; the 88k subtracts a number from the
4054 @code{r_type} field and creates an addend field.
4060 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
4062 coff_symbol_type *coffsym = (coff_symbol_type *) NULL; \
4063 if (ptr && bfd_asymbol_bfd (ptr) != abfd) \
4064 coffsym = (obj_symbols (abfd) \
4065 + (cache_ptr->sym_ptr_ptr - symbols)); \
4067 coffsym = coff_symbol_from (abfd, ptr); \
4068 if (coffsym != (coff_symbol_type *) NULL \
4069 && coffsym->native->u.syment.n_scnum == 0) \
4070 cache_ptr->addend = 0; \
4071 else if (ptr && bfd_asymbol_bfd (ptr) == abfd \
4072 && ptr->section != (asection *) NULL) \
4073 cache_ptr->addend = - (ptr->section->vma + ptr->value); \
4075 cache_ptr->addend = 0; \
4080 coff_slurp_reloc_table (abfd
, asect
, symbols
)
4085 RELOC
*native_relocs
;
4086 arelent
*reloc_cache
;
4091 if (asect
->relocation
)
4093 if (asect
->reloc_count
== 0)
4095 if (asect
->flags
& SEC_CONSTRUCTOR
)
4097 if (!coff_slurp_symbol_table (abfd
))
4100 (RELOC
*) buy_and_read (abfd
,
4104 asect
->reloc_count
));
4105 reloc_cache
= (arelent
*)
4106 bfd_alloc (abfd
, (size_t) (asect
->reloc_count
* sizeof (arelent
)));
4108 if (reloc_cache
== NULL
)
4112 for (idx
= 0; idx
< asect
->reloc_count
; idx
++)
4114 struct internal_reloc dst
;
4115 struct external_reloc
*src
;
4116 #ifndef RELOC_PROCESSING
4120 cache_ptr
= reloc_cache
+ idx
;
4121 src
= native_relocs
+ idx
;
4123 coff_swap_reloc_in (abfd
, src
, &dst
);
4125 #ifdef RELOC_PROCESSING
4126 RELOC_PROCESSING (cache_ptr
, &dst
, symbols
, abfd
, asect
);
4128 cache_ptr
->address
= dst
.r_vaddr
;
4130 if (dst
.r_symndx
!= -1)
4132 if (dst
.r_symndx
< 0 || dst
.r_symndx
>= obj_conv_table_size (abfd
))
4134 (*_bfd_error_handler
)
4135 (_("%s: warning: illegal symbol index %ld in relocs"),
4136 bfd_get_filename (abfd
), dst
.r_symndx
);
4137 cache_ptr
->sym_ptr_ptr
= bfd_abs_section_ptr
->symbol_ptr_ptr
;
4142 cache_ptr
->sym_ptr_ptr
= (symbols
4143 + obj_convert (abfd
)[dst
.r_symndx
]);
4144 ptr
= *(cache_ptr
->sym_ptr_ptr
);
4149 cache_ptr
->sym_ptr_ptr
= bfd_abs_section_ptr
->symbol_ptr_ptr
;
4153 /* The symbols definitions that we have read in have been
4154 relocated as if their sections started at 0. But the offsets
4155 refering to the symbols in the raw data have not been
4156 modified, so we have to have a negative addend to compensate.
4158 Note that symbols which used to be common must be left alone */
4160 /* Calculate any reloc addend by looking at the symbol */
4161 CALC_ADDEND (abfd
, ptr
, dst
, cache_ptr
);
4163 cache_ptr
->address
-= asect
->vma
;
4164 /* !! cache_ptr->section = (asection *) NULL;*/
4166 /* Fill in the cache_ptr->howto field from dst.r_type */
4167 RTYPE2HOWTO (cache_ptr
, &dst
);
4168 #endif /* RELOC_PROCESSING */
4170 if (cache_ptr
->howto
== NULL
)
4172 (*_bfd_error_handler
)
4173 (_("%s: illegal relocation type %d at address 0x%lx"),
4174 bfd_get_filename (abfd
), dst
.r_type
, (long) dst
.r_vaddr
);
4175 bfd_set_error (bfd_error_bad_value
);
4180 asect
->relocation
= reloc_cache
;
4184 #ifndef coff_rtype_to_howto
4187 /* Get the howto structure for a reloc. This is only used if the file
4188 including this one defines coff_relocate_section to be
4189 _bfd_coff_generic_relocate_section, so it is OK if it does not
4190 always work. It is the responsibility of the including file to
4191 make sure it is reasonable if it is needed. */
4193 static reloc_howto_type
*coff_rtype_to_howto
4194 PARAMS ((bfd
*, asection
*, struct internal_reloc
*,
4195 struct coff_link_hash_entry
*, struct internal_syment
*,
4199 static reloc_howto_type
*
4200 coff_rtype_to_howto (abfd
, sec
, rel
, h
, sym
, addendp
)
4201 bfd
*abfd ATTRIBUTE_UNUSED
;
4202 asection
*sec ATTRIBUTE_UNUSED
;
4203 struct internal_reloc
*rel
;
4204 struct coff_link_hash_entry
*h ATTRIBUTE_UNUSED
;
4205 struct internal_syment
*sym ATTRIBUTE_UNUSED
;
4206 bfd_vma
*addendp ATTRIBUTE_UNUSED
;
4210 RTYPE2HOWTO (&genrel
, rel
);
4211 return genrel
.howto
;
4214 #else /* ! defined (RTYPE2HOWTO) */
4216 #define coff_rtype_to_howto NULL
4218 #endif /* ! defined (RTYPE2HOWTO) */
4219 #endif /* ! defined (coff_rtype_to_howto) */
4221 /* This is stupid. This function should be a boolean predicate. */
4223 coff_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
4229 arelent
*tblptr
= section
->relocation
;
4230 unsigned int count
= 0;
4233 if (section
->flags
& SEC_CONSTRUCTOR
)
4235 /* this section has relocs made up by us, they are not in the
4236 file, so take them out of their chain and place them into
4237 the data area provided */
4238 arelent_chain
*chain
= section
->constructor_chain
;
4239 for (count
= 0; count
< section
->reloc_count
; count
++)
4241 *relptr
++ = &chain
->relent
;
4242 chain
= chain
->next
;
4248 if (! coff_slurp_reloc_table (abfd
, section
, symbols
))
4251 tblptr
= section
->relocation
;
4253 for (; count
++ < section
->reloc_count
;)
4254 *relptr
++ = tblptr
++;
4259 return section
->reloc_count
;
4264 coff_sym_filepos (abfd
)
4267 return obj_sym_filepos (abfd
);
4271 #ifndef coff_reloc16_estimate
4272 #define coff_reloc16_estimate dummy_reloc16_estimate
4274 static int dummy_reloc16_estimate
4275 PARAMS ((bfd
*, asection
*, arelent
*, unsigned int,
4276 struct bfd_link_info
*));
4279 dummy_reloc16_estimate (abfd
, input_section
, reloc
, shrink
, link_info
)
4280 bfd
*abfd ATTRIBUTE_UNUSED
;
4281 asection
*input_section ATTRIBUTE_UNUSED
;
4282 arelent
*reloc ATTRIBUTE_UNUSED
;
4283 unsigned int shrink ATTRIBUTE_UNUSED
;
4284 struct bfd_link_info
*link_info ATTRIBUTE_UNUSED
;
4292 #ifndef coff_reloc16_extra_cases
4294 #define coff_reloc16_extra_cases dummy_reloc16_extra_cases
4296 /* This works even if abort is not declared in any header file. */
4298 static void dummy_reloc16_extra_cases
4299 PARAMS ((bfd
*, struct bfd_link_info
*, struct bfd_link_order
*, arelent
*,
4300 bfd_byte
*, unsigned int *, unsigned int *));
4303 dummy_reloc16_extra_cases (abfd
, link_info
, link_order
, reloc
, data
, src_ptr
,
4305 bfd
*abfd ATTRIBUTE_UNUSED
;
4306 struct bfd_link_info
*link_info ATTRIBUTE_UNUSED
;
4307 struct bfd_link_order
*link_order ATTRIBUTE_UNUSED
;
4308 arelent
*reloc ATTRIBUTE_UNUSED
;
4309 bfd_byte
*data ATTRIBUTE_UNUSED
;
4310 unsigned int *src_ptr ATTRIBUTE_UNUSED
;
4311 unsigned int *dst_ptr ATTRIBUTE_UNUSED
;
4317 /* If coff_relocate_section is defined, we can use the optimized COFF
4318 backend linker. Otherwise we must continue to use the old linker. */
4319 #ifdef coff_relocate_section
4320 #ifndef coff_bfd_link_hash_table_create
4321 #define coff_bfd_link_hash_table_create _bfd_coff_link_hash_table_create
4323 #ifndef coff_bfd_link_add_symbols
4324 #define coff_bfd_link_add_symbols _bfd_coff_link_add_symbols
4326 #ifndef coff_bfd_final_link
4327 #define coff_bfd_final_link _bfd_coff_final_link
4329 #else /* ! defined (coff_relocate_section) */
4330 #define coff_relocate_section NULL
4331 #ifndef coff_bfd_link_hash_table_create
4332 #define coff_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
4334 #ifndef coff_bfd_link_add_symbols
4335 #define coff_bfd_link_add_symbols _bfd_generic_link_add_symbols
4337 #define coff_bfd_final_link _bfd_generic_final_link
4338 #endif /* ! defined (coff_relocate_section) */
4340 #define coff_bfd_link_split_section _bfd_generic_link_split_section
4342 #ifndef coff_start_final_link
4343 #define coff_start_final_link NULL
4346 #ifndef coff_adjust_symndx
4347 #define coff_adjust_symndx NULL
4350 #ifndef coff_link_add_one_symbol
4351 #define coff_link_add_one_symbol _bfd_generic_link_add_one_symbol
4354 #ifndef coff_link_output_has_begun
4356 static boolean coff_link_output_has_begun
4357 PARAMS ((bfd
*, struct coff_final_link_info
*));
4360 coff_link_output_has_begun (abfd
, info
)
4362 struct coff_final_link_info
* info ATTRIBUTE_UNUSED
;
4364 return abfd
->output_has_begun
;
4368 #ifndef coff_final_link_postscript
4370 static boolean coff_final_link_postscript
4371 PARAMS ((bfd
*, struct coff_final_link_info
*));
4374 coff_final_link_postscript (abfd
, pfinfo
)
4375 bfd
* abfd ATTRIBUTE_UNUSED
;
4376 struct coff_final_link_info
* pfinfo ATTRIBUTE_UNUSED
;
4382 #ifndef coff_SWAP_aux_in
4383 #define coff_SWAP_aux_in coff_swap_aux_in
4385 #ifndef coff_SWAP_sym_in
4386 #define coff_SWAP_sym_in coff_swap_sym_in
4388 #ifndef coff_SWAP_lineno_in
4389 #define coff_SWAP_lineno_in coff_swap_lineno_in
4391 #ifndef coff_SWAP_aux_out
4392 #define coff_SWAP_aux_out coff_swap_aux_out
4394 #ifndef coff_SWAP_sym_out
4395 #define coff_SWAP_sym_out coff_swap_sym_out
4397 #ifndef coff_SWAP_lineno_out
4398 #define coff_SWAP_lineno_out coff_swap_lineno_out
4400 #ifndef coff_SWAP_reloc_out
4401 #define coff_SWAP_reloc_out coff_swap_reloc_out
4403 #ifndef coff_SWAP_filehdr_out
4404 #define coff_SWAP_filehdr_out coff_swap_filehdr_out
4406 #ifndef coff_SWAP_aouthdr_out
4407 #define coff_SWAP_aouthdr_out coff_swap_aouthdr_out
4409 #ifndef coff_SWAP_scnhdr_out
4410 #define coff_SWAP_scnhdr_out coff_swap_scnhdr_out
4412 #ifndef coff_SWAP_reloc_in
4413 #define coff_SWAP_reloc_in coff_swap_reloc_in
4415 #ifndef coff_SWAP_filehdr_in
4416 #define coff_SWAP_filehdr_in coff_swap_filehdr_in
4418 #ifndef coff_SWAP_aouthdr_in
4419 #define coff_SWAP_aouthdr_in coff_swap_aouthdr_in
4421 #ifndef coff_SWAP_scnhdr_in
4422 #define coff_SWAP_scnhdr_in coff_swap_scnhdr_in
4427 static CONST bfd_coff_backend_data bfd_coff_std_swap_table
=
4429 coff_SWAP_aux_in
, coff_SWAP_sym_in
, coff_SWAP_lineno_in
,
4430 coff_SWAP_aux_out
, coff_SWAP_sym_out
,
4431 coff_SWAP_lineno_out
, coff_SWAP_reloc_out
,
4432 coff_SWAP_filehdr_out
, coff_SWAP_aouthdr_out
,
4433 coff_SWAP_scnhdr_out
,
4434 FILHSZ
, AOUTSZ
, SCNHSZ
, SYMESZ
, AUXESZ
, RELSZ
, LINESZ
,
4435 #ifdef COFF_LONG_FILENAMES
4440 #ifdef COFF_LONG_SECTION_NAMES
4445 COFF_DEFAULT_SECTION_ALIGNMENT_POWER
,
4446 coff_SWAP_filehdr_in
, coff_SWAP_aouthdr_in
, coff_SWAP_scnhdr_in
,
4447 coff_SWAP_reloc_in
, coff_bad_format_hook
, coff_set_arch_mach_hook
,
4448 coff_mkobject_hook
, styp_to_sec_flags
, coff_set_alignment_hook
,
4449 coff_slurp_symbol_table
, symname_in_debug_hook
, coff_pointerize_aux_hook
,
4450 coff_print_aux
, coff_reloc16_extra_cases
, coff_reloc16_estimate
,
4451 coff_classify_symbol
, coff_compute_section_file_positions
,
4452 coff_start_final_link
, coff_relocate_section
, coff_rtype_to_howto
,
4453 coff_adjust_symndx
, coff_link_add_one_symbol
,
4454 coff_link_output_has_begun
, coff_final_link_postscript
4457 #ifndef coff_close_and_cleanup
4458 #define coff_close_and_cleanup _bfd_generic_close_and_cleanup
4461 #ifndef coff_bfd_free_cached_info
4462 #define coff_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
4465 #ifndef coff_get_section_contents
4466 #define coff_get_section_contents _bfd_generic_get_section_contents
4469 #ifndef coff_bfd_copy_private_symbol_data
4470 #define coff_bfd_copy_private_symbol_data _bfd_generic_bfd_copy_private_symbol_data
4473 #ifndef coff_bfd_copy_private_section_data
4474 #define coff_bfd_copy_private_section_data _bfd_generic_bfd_copy_private_section_data
4477 #ifndef coff_bfd_copy_private_bfd_data
4478 #define coff_bfd_copy_private_bfd_data _bfd_generic_bfd_copy_private_bfd_data
4481 #ifndef coff_bfd_merge_private_bfd_data
4482 #define coff_bfd_merge_private_bfd_data _bfd_generic_bfd_merge_private_bfd_data
4485 #ifndef coff_bfd_set_private_flags
4486 #define coff_bfd_set_private_flags _bfd_generic_bfd_set_private_flags
4489 #ifndef coff_bfd_print_private_bfd_data
4490 #define coff_bfd_print_private_bfd_data _bfd_generic_bfd_print_private_bfd_data
4493 #ifndef coff_bfd_is_local_label_name
4494 #define coff_bfd_is_local_label_name _bfd_coff_is_local_label_name
4497 #ifndef coff_read_minisymbols
4498 #define coff_read_minisymbols _bfd_generic_read_minisymbols
4501 #ifndef coff_minisymbol_to_symbol
4502 #define coff_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
4505 /* The reloc lookup routine must be supplied by each individual COFF
4507 #ifndef coff_bfd_reloc_type_lookup
4508 #define coff_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
4511 #ifndef coff_bfd_get_relocated_section_contents
4512 #define coff_bfd_get_relocated_section_contents \
4513 bfd_generic_get_relocated_section_contents
4516 #ifndef coff_bfd_relax_section
4517 #define coff_bfd_relax_section bfd_generic_relax_section
4520 #ifndef coff_bfd_gc_sections
4521 #define coff_bfd_gc_sections bfd_generic_gc_sections
4524 #define CREATE_BIG_COFF_TARGET_VEC(VAR, NAME, EXTRA_O_FLAGS, EXTRA_S_FLAGS, UNDER, ALTERNATIVE) \
4525 const bfd_target VAR = \
4528 bfd_target_coff_flavour, \
4529 BFD_ENDIAN_BIG, /* data byte order is big */ \
4530 BFD_ENDIAN_BIG, /* header byte order is big */ \
4531 /* object flags */ \
4532 (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | \
4533 HAS_SYMS | HAS_LOCALS | WP_TEXT | EXTRA_O_FLAGS), \
4534 /* section flags */ \
4535 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | EXTRA_S_FLAGS), \
4536 UNDER, /* leading symbol underscore */ \
4537 '/', /* ar_pad_char */ \
4538 15, /* ar_max_namelen */ \
4540 /* Data conversion functions. */ \
4541 bfd_getb64, bfd_getb_signed_64, bfd_putb64, \
4542 bfd_getb32, bfd_getb_signed_32, bfd_putb32, \
4543 bfd_getb16, bfd_getb_signed_16, bfd_putb16, \
4545 /* Header conversion functions. */ \
4546 bfd_getb64, bfd_getb_signed_64, bfd_putb64, \
4547 bfd_getb32, bfd_getb_signed_32, bfd_putb32, \
4548 bfd_getb16, bfd_getb_signed_16, bfd_putb16, \
4550 /* bfd_check_format */ \
4551 { _bfd_dummy_target, coff_object_p, bfd_generic_archive_p, _bfd_dummy_target }, \
4552 /* bfd_set_format */ \
4553 { bfd_false, coff_mkobject, _bfd_generic_mkarchive, bfd_false }, \
4554 /* bfd_write_contents */ \
4555 { bfd_false, coff_write_object_contents, _bfd_write_archive_contents, bfd_false }, \
4557 BFD_JUMP_TABLE_GENERIC (coff), \
4558 BFD_JUMP_TABLE_COPY (coff), \
4559 BFD_JUMP_TABLE_CORE (_bfd_nocore), \
4560 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff), \
4561 BFD_JUMP_TABLE_SYMBOLS (coff), \
4562 BFD_JUMP_TABLE_RELOCS (coff), \
4563 BFD_JUMP_TABLE_WRITE (coff), \
4564 BFD_JUMP_TABLE_LINK (coff), \
4565 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), \
4572 #define CREATE_LITTLE_COFF_TARGET_VEC(VAR, NAME, EXTRA_O_FLAGS, EXTRA_S_FLAGS, UNDER, ALTERNATIVE) \
4573 const bfd_target VAR = \
4576 bfd_target_coff_flavour, \
4577 BFD_ENDIAN_LITTLE, /* data byte order is little */ \
4578 BFD_ENDIAN_LITTLE, /* header byte order is little */ \
4579 /* object flags */ \
4580 (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | \
4581 HAS_SYMS | HAS_LOCALS | WP_TEXT | EXTRA_O_FLAGS), \
4582 /* section flags */ \
4583 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | EXTRA_S_FLAGS), \
4584 UNDER, /* leading symbol underscore */ \
4585 '/', /* ar_pad_char */ \
4586 15, /* ar_max_namelen */ \
4588 /* Data conversion functions. */ \
4589 bfd_getl64, bfd_getl_signed_64, bfd_putl64, \
4590 bfd_getl32, bfd_getl_signed_32, bfd_putl32, \
4591 bfd_getl16, bfd_getl_signed_16, bfd_putl16, \
4592 /* Header conversion functions. */ \
4593 bfd_getl64, bfd_getl_signed_64, bfd_putl64, \
4594 bfd_getl32, bfd_getl_signed_32, bfd_putl32, \
4595 bfd_getl16, bfd_getl_signed_16, bfd_putl16, \
4596 /* bfd_check_format */ \
4597 { _bfd_dummy_target, coff_object_p, bfd_generic_archive_p, _bfd_dummy_target }, \
4598 /* bfd_set_format */ \
4599 { bfd_false, coff_mkobject, _bfd_generic_mkarchive, bfd_false }, \
4600 /* bfd_write_contents */ \
4601 { bfd_false, coff_write_object_contents, _bfd_write_archive_contents, bfd_false }, \
4603 BFD_JUMP_TABLE_GENERIC (coff), \
4604 BFD_JUMP_TABLE_COPY (coff), \
4605 BFD_JUMP_TABLE_CORE (_bfd_nocore), \
4606 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff), \
4607 BFD_JUMP_TABLE_SYMBOLS (coff), \
4608 BFD_JUMP_TABLE_RELOCS (coff), \
4609 BFD_JUMP_TABLE_WRITE (coff), \
4610 BFD_JUMP_TABLE_LINK (coff), \
4611 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), \