Automatic date update in version.in
[binutils-gdb.git] / bfd / coffcode.h
blob0487555dda1911b0934df5289f0b47f7e0762adf
1 /* Support for the generic parts of most COFF variants, for BFD.
2 Copyright (C) 1990-2023 Free Software Foundation, Inc.
3 Written by Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
22 /* Most of this hacked by Steve Chamberlain,
23 sac@cygnus.com. */
25 SECTION
26 coff backends
28 BFD supports a number of different flavours of coff format.
29 The major differences between formats are the sizes and
30 alignments of fields in structures on disk, and the occasional
31 extra field.
33 Coff in all its varieties is implemented with a few common
34 files and a number of implementation specific files. For
35 example, the i386 coff format is implemented in the file
36 @file{coff-i386.c}. This file @code{#include}s
37 @file{coff/i386.h} which defines the external structure of the
38 coff format for the i386, and @file{coff/internal.h} which
39 defines the internal structure. @file{coff-i386.c} also
40 defines the relocations used by the i386 coff format
41 @xref{Relocations}.
43 SUBSECTION
44 Porting to a new version of coff
46 The recommended method is to select from the existing
47 implementations the version of coff which is most like the one
48 you want to use. For example, we'll say that i386 coff is
49 the one you select, and that your coff flavour is called foo.
50 Copy @file{i386coff.c} to @file{foocoff.c}, copy
51 @file{../include/coff/i386.h} to @file{../include/coff/foo.h},
52 and add the lines to @file{targets.c} and @file{Makefile.in}
53 so that your new back end is used. Alter the shapes of the
54 structures in @file{../include/coff/foo.h} so that they match
55 what you need. You will probably also have to add
56 @code{#ifdef}s to the code in @file{coff/internal.h} and
57 @file{coffcode.h} if your version of coff is too wild.
59 You can verify that your new BFD backend works quite simply by
60 building @file{objdump} from the @file{binutils} directory,
61 and making sure that its version of what's going on and your
62 host system's idea (assuming it has the pretty standard coff
63 dump utility, usually called @code{att-dump} or just
64 @code{dump}) are the same. Then clean up your code, and send
65 what you've done to Cygnus. Then your stuff will be in the
66 next release, and you won't have to keep integrating it.
68 SUBSECTION
69 How the coff backend works
71 SUBSUBSECTION
72 File layout
74 The Coff backend is split into generic routines that are
75 applicable to any Coff target and routines that are specific
76 to a particular target. The target-specific routines are
77 further split into ones which are basically the same for all
78 Coff targets except that they use the external symbol format
79 or use different values for certain constants.
81 The generic routines are in @file{coffgen.c}. These routines
82 work for any Coff target. They use some hooks into the target
83 specific code; the hooks are in a @code{bfd_coff_backend_data}
84 structure, one of which exists for each target.
86 The essentially similar target-specific routines are in
87 @file{coffcode.h}. This header file includes executable C code.
88 The various Coff targets first include the appropriate Coff
89 header file, make any special defines that are needed, and
90 then include @file{coffcode.h}.
92 Some of the Coff targets then also have additional routines in
93 the target source file itself.
95 SUBSUBSECTION
96 Coff long section names
98 In the standard Coff object format, section names are limited to
99 the eight bytes available in the @code{s_name} field of the
100 @code{SCNHDR} section header structure. The format requires the
101 field to be NUL-padded, but not necessarily NUL-terminated, so
102 the longest section names permitted are a full eight characters.
104 The Microsoft PE variants of the Coff object file format add
105 an extension to support the use of long section names. This
106 extension is defined in section 4 of the Microsoft PE/COFF
107 specification (rev 8.1). If a section name is too long to fit
108 into the section header's @code{s_name} field, it is instead
109 placed into the string table, and the @code{s_name} field is
110 filled with a slash ("/") followed by the ASCII decimal
111 representation of the offset of the full name relative to the
112 string table base.
114 Note that this implies that the extension can only be used in object
115 files, as executables do not contain a string table. The standard
116 specifies that long section names from objects emitted into executable
117 images are to be truncated.
119 However, as a GNU extension, BFD can generate executable images
120 that contain a string table and long section names. This
121 would appear to be technically valid, as the standard only says
122 that Coff debugging information is deprecated, not forbidden,
123 and in practice it works, although some tools that parse PE files
124 expecting the MS standard format may become confused; @file{PEview} is
125 one known example.
127 The functionality is supported in BFD by code implemented under
128 the control of the macro @code{COFF_LONG_SECTION_NAMES}. If not
129 defined, the format does not support long section names in any way.
130 If defined, it is used to initialise a flag,
131 @code{_bfd_coff_long_section_names}, and a hook function pointer,
132 @code{_bfd_coff_set_long_section_names}, in the Coff backend data
133 structure. The flag controls the generation of long section names
134 in output BFDs at runtime; if it is false, as it will be by default
135 when generating an executable image, long section names are truncated;
136 if true, the long section names extension is employed. The hook
137 points to a function that allows the value of a copy of the flag
138 in coff object tdata to be altered at runtime, on formats that
139 support long section names at all; on other formats it points
140 to a stub that returns an error indication.
142 With input BFDs, the flag is set according to whether any long section
143 names are detected while reading the section headers. For a completely
144 new BFD, the flag is set to the default for the target format. This
145 information can be used by a client of the BFD library when deciding
146 what output format to generate, and means that a BFD that is opened
147 for read and subsequently converted to a writeable BFD and modified
148 in-place will retain whatever format it had on input.
150 If @code{COFF_LONG_SECTION_NAMES} is simply defined (blank), or is
151 defined to the value "1", then long section names are enabled by
152 default; if it is defined to the value zero, they are disabled by
153 default (but still accepted in input BFDs). The header @file{coffcode.h}
154 defines a macro, @code{COFF_DEFAULT_LONG_SECTION_NAMES}, which is
155 used in the backends to initialise the backend data structure fields
156 appropriately; see the comments for further detail.
158 SUBSUBSECTION
159 Bit twiddling
161 Each flavour of coff supported in BFD has its own header file
162 describing the external layout of the structures. There is also
163 an internal description of the coff layout, in
164 @file{coff/internal.h}. A major function of the
165 coff backend is swapping the bytes and twiddling the bits to
166 translate the external form of the structures into the normal
167 internal form. This is all performed in the
168 @code{bfd_swap}_@i{thing}_@i{direction} routines. Some
169 elements are different sizes between different versions of
170 coff; it is the duty of the coff version specific include file
171 to override the definitions of various packing routines in
172 @file{coffcode.h}. E.g., the size of line number entry in coff is
173 sometimes 16 bits, and sometimes 32 bits. @code{#define}ing
174 @code{PUT_LNSZ_LNNO} and @code{GET_LNSZ_LNNO} will select the
175 correct one. No doubt, some day someone will find a version of
176 coff which has a varying field size not catered to at the
177 moment. To port BFD, that person will have to add more @code{#defines}.
178 Three of the bit twiddling routines are exported to
179 @code{gdb}; @code{coff_swap_aux_in}, @code{coff_swap_sym_in}
180 and @code{coff_swap_lineno_in}. @code{GDB} reads the symbol
181 table on its own, but uses BFD to fix things up. More of the
182 bit twiddlers are exported for @code{gas};
183 @code{coff_swap_aux_out}, @code{coff_swap_sym_out},
184 @code{coff_swap_lineno_out}, @code{coff_swap_reloc_out},
185 @code{coff_swap_filehdr_out}, @code{coff_swap_aouthdr_out},
186 @code{coff_swap_scnhdr_out}. @code{Gas} currently keeps track
187 of all the symbol table and reloc drudgery itself, thereby
188 saving the internal BFD overhead, but uses BFD to swap things
189 on the way out, making cross ports much safer. Doing so also
190 allows BFD (and thus the linker) to use the same header files
191 as @code{gas}, which makes one avenue to disaster disappear.
193 SUBSUBSECTION
194 Symbol reading
196 The simple canonical form for symbols used by BFD is not rich
197 enough to keep all the information available in a coff symbol
198 table. The back end gets around this problem by keeping the original
199 symbol table around, "behind the scenes".
201 When a symbol table is requested (through a call to
202 @code{bfd_canonicalize_symtab}), a request gets through to
203 @code{coff_get_normalized_symtab}. This reads the symbol table from
204 the coff file and swaps all the structures inside into the
205 internal form. It also fixes up all the pointers in the table
206 (represented in the file by offsets from the first symbol in
207 the table) into physical pointers to elements in the new
208 internal table. This involves some work since the meanings of
209 fields change depending upon context: a field that is a
210 pointer to another structure in the symbol table at one moment
211 may be the size in bytes of a structure at the next. Another
212 pass is made over the table. All symbols which mark file names
213 (<<C_FILE>> symbols) are modified so that the internal
214 string points to the value in the auxent (the real filename)
215 rather than the normal text associated with the symbol
216 (@code{".file"}).
218 At this time the symbol names are moved around. Coff stores
219 all symbols less than nine characters long physically
220 within the symbol table; longer strings are kept at the end of
221 the file in the string table. This pass moves all strings
222 into memory and replaces them with pointers to the strings.
224 The symbol table is massaged once again, this time to create
225 the canonical table used by the BFD application. Each symbol
226 is inspected in turn, and a decision made (using the
227 @code{sclass} field) about the various flags to set in the
228 @code{asymbol}. @xref{Symbols}. The generated canonical table
229 shares strings with the hidden internal symbol table.
231 Any linenumbers are read from the coff file too, and attached
232 to the symbols which own the functions the linenumbers belong to.
234 SUBSUBSECTION
235 Symbol writing
237 Writing a symbol to a coff file which didn't come from a coff
238 file will lose any debugging information. The @code{asymbol}
239 structure remembers the BFD from which the symbol was taken, and on
240 output the back end makes sure that the same destination target as
241 source target is present.
243 When the symbols have come from a coff file then all the
244 debugging information is preserved.
246 Symbol tables are provided for writing to the back end in a
247 vector of pointers to pointers. This allows applications like
248 the linker to accumulate and output large symbol tables
249 without having to do too much byte copying.
251 This function runs through the provided symbol table and
252 patches each symbol marked as a file place holder
253 (@code{C_FILE}) to point to the next file place holder in the
254 list. It also marks each @code{offset} field in the list with
255 the offset from the first symbol of the current symbol.
257 Another function of this procedure is to turn the canonical
258 value form of BFD into the form used by coff. Internally, BFD
259 expects symbol values to be offsets from a section base; so a
260 symbol physically at 0x120, but in a section starting at
261 0x100, would have the value 0x20. Coff expects symbols to
262 contain their final value, so symbols have their values
263 changed at this point to reflect their sum with their owning
264 section. This transformation uses the
265 <<output_section>> field of the @code{asymbol}'s
266 @code{asection} @xref{Sections}.
268 o <<coff_mangle_symbols>>
270 This routine runs though the provided symbol table and uses
271 the offsets generated by the previous pass and the pointers
272 generated when the symbol table was read in to create the
273 structured hierarchy required by coff. It changes each pointer
274 to a symbol into the index into the symbol table of the asymbol.
276 o <<coff_write_symbols>>
278 This routine runs through the symbol table and patches up the
279 symbols from their internal form into the coff way, calls the
280 bit twiddlers, and writes out the table to the file.
285 INTERNAL_DEFINITION
286 coff_symbol_type
288 DESCRIPTION
289 The hidden information for an <<asymbol>> is described in a
290 <<combined_entry_type>>:
292 CODE_FRAGMENT
293 .typedef struct coff_ptr_struct
295 . {* Remembers the offset from the first symbol in the file for
296 . this symbol. Generated by coff_renumber_symbols. *}
297 . unsigned int offset;
299 . {* Selects between the elements of the union below. *}
300 . unsigned int is_sym : 1;
302 . {* Selects between the elements of the x_sym.x_tagndx union. If set,
303 . p is valid and the field will be renumbered. *}
304 . unsigned int fix_tag : 1;
306 . {* Selects between the elements of the x_sym.x_fcnary.x_fcn.x_endndx
307 . union. If set, p is valid and the field will be renumbered. *}
308 . unsigned int fix_end : 1;
310 . {* Selects between the elements of the x_csect.x_scnlen union. If set,
311 . p is valid and the field will be renumbered. *}
312 . unsigned int fix_scnlen : 1;
314 . {* If set, u.syment.n_value contains a pointer to a symbol. The final
315 . value will be the offset field. Used for XCOFF C_BSTAT symbols. *}
316 . unsigned int fix_value : 1;
318 . {* If set, u.syment.n_value is an index into the line number entries.
319 . Used for XCOFF C_BINCL/C_EINCL symbols. *}
320 . unsigned int fix_line : 1;
322 . {* The container for the symbol structure as read and translated
323 . from the file. *}
324 . union
326 . union internal_auxent auxent;
327 . struct internal_syment syment;
328 . } u;
330 . {* An extra pointer which can used by format based on COFF (like XCOFF)
331 . to provide extra information to their backend. *}
332 . void *extrap;
333 .} combined_entry_type;
335 .{* Each canonical asymbol really looks like this: *}
337 .typedef struct coff_symbol_struct
339 . {* The actual symbol which the rest of BFD works with *}
340 . asymbol symbol;
342 . {* A pointer to the hidden information for this symbol *}
343 . combined_entry_type *native;
345 . {* A pointer to the linenumber information for this symbol *}
346 . struct lineno_cache_entry *lineno;
348 . {* Have the line numbers been relocated yet ? *}
349 . bool done_lineno;
350 .} coff_symbol_type;
354 #include "libiberty.h"
355 #include <string.h>
357 #ifdef COFF_WITH_PE
358 #include "peicode.h"
359 #else
360 #include "coffswap.h"
361 #endif
363 #define STRING_SIZE_SIZE 4
365 #define DOT_DEBUG ".debug"
366 #define DOT_ZDEBUG ".zdebug"
367 #define GNU_LINKONCE_WI ".gnu.linkonce.wi."
368 #define GNU_LINKONCE_WT ".gnu.linkonce.wt."
369 #define DOT_RELOC ".reloc"
371 #if defined(COFF_WITH_PE) || defined(COFF_GO32_EXE) || defined(COFF_GO32)
372 # define COFF_WITH_EXTENDED_RELOC_COUNTER
373 #endif
375 #if defined (COFF_LONG_SECTION_NAMES)
376 /* Needed to expand the inputs to BLANKOR1TOODD. */
377 #define COFFLONGSECTIONCATHELPER(x,y) x ## y
378 /* If the input macro Y is blank or '1', return an odd number; if it is
379 '0', return an even number. Result undefined in all other cases. */
380 #define BLANKOR1TOODD(y) COFFLONGSECTIONCATHELPER(1,y)
381 /* Defined to numerical 0 or 1 according to whether generation of long
382 section names is disabled or enabled by default. */
383 #define COFF_ENABLE_LONG_SECTION_NAMES (BLANKOR1TOODD(COFF_LONG_SECTION_NAMES) & 1)
384 /* Where long section names are supported, we allow them to be enabled
385 and disabled at runtime, so select an appropriate hook function for
386 _bfd_coff_set_long_section_names. */
387 #define COFF_LONG_SECTION_NAMES_SETTER bfd_coff_set_long_section_names_allowed
388 #else /* !defined (COFF_LONG_SECTION_NAMES) */
389 /* If long section names are not supported, this stub disallows any
390 attempt to enable them at run-time. */
391 #define COFF_LONG_SECTION_NAMES_SETTER bfd_coff_set_long_section_names_disallowed
392 #endif /* defined (COFF_LONG_SECTION_NAMES) */
394 /* Define a macro that can be used to initialise both the fields relating
395 to long section names in the backend data struct simultaneously. */
396 #if COFF_ENABLE_LONG_SECTION_NAMES
397 #define COFF_DEFAULT_LONG_SECTION_NAMES (true), COFF_LONG_SECTION_NAMES_SETTER
398 #else /* !COFF_ENABLE_LONG_SECTION_NAMES */
399 #define COFF_DEFAULT_LONG_SECTION_NAMES (false), COFF_LONG_SECTION_NAMES_SETTER
400 #endif /* COFF_ENABLE_LONG_SECTION_NAMES */
402 static enum coff_symbol_classification coff_classify_symbol
403 (bfd *, struct internal_syment *);
405 /* void warning(); */
407 #if defined (COFF_LONG_SECTION_NAMES)
408 static bool
409 bfd_coff_set_long_section_names_allowed (bfd *abfd, int enable)
411 bfd_coff_long_section_names (abfd) = enable;
412 return true;
414 #else /* !defined (COFF_LONG_SECTION_NAMES) */
415 static bool
416 bfd_coff_set_long_section_names_disallowed (bfd *abfd ATTRIBUTE_UNUSED,
417 int enable ATTRIBUTE_UNUSED)
419 return false;
421 #endif /* defined (COFF_LONG_SECTION_NAMES) */
423 /* Return a word with STYP_* (scnhdr.s_flags) flags set to represent
424 the incoming SEC_* flags. The inverse of this function is
425 styp_to_sec_flags(). NOTE: If you add to/change this routine, you
426 should probably mirror the changes in styp_to_sec_flags(). */
428 #ifndef COFF_WITH_PE
430 /* Macros for setting debugging flags. */
432 #ifdef STYP_DEBUG
433 #define STYP_XCOFF_DEBUG STYP_DEBUG
434 #else
435 #define STYP_XCOFF_DEBUG STYP_INFO
436 #endif
438 #ifdef COFF_ALIGN_IN_S_FLAGS
439 #define STYP_DEBUG_INFO STYP_DSECT
440 #else
441 #define STYP_DEBUG_INFO STYP_INFO
442 #endif
444 static long
445 sec_to_styp_flags (const char *sec_name, flagword sec_flags)
447 long styp_flags = 0;
449 if (!strcmp (sec_name, _TEXT))
451 styp_flags = STYP_TEXT;
453 else if (!strcmp (sec_name, _DATA))
455 styp_flags = STYP_DATA;
457 else if (!strcmp (sec_name, _BSS))
459 styp_flags = STYP_BSS;
460 #ifdef _COMMENT
462 else if (!strcmp (sec_name, _COMMENT))
464 styp_flags = STYP_INFO;
465 #endif /* _COMMENT */
466 #ifdef _LIB
468 else if (!strcmp (sec_name, _LIB))
470 styp_flags = STYP_LIB;
471 #endif /* _LIB */
472 #ifdef _LIT
474 else if (!strcmp (sec_name, _LIT))
476 styp_flags = STYP_LIT;
477 #endif /* _LIT */
479 else if (startswith (sec_name, DOT_DEBUG)
480 || startswith (sec_name, DOT_ZDEBUG))
482 /* Handle the XCOFF debug section and DWARF2 debug sections. */
483 if (!sec_name[6])
484 styp_flags = STYP_XCOFF_DEBUG;
485 else
486 styp_flags = STYP_DEBUG_INFO;
488 else if (startswith (sec_name, ".stab"))
490 styp_flags = STYP_DEBUG_INFO;
492 #ifdef COFF_LONG_SECTION_NAMES
493 else if (startswith (sec_name, GNU_LINKONCE_WI)
494 || startswith (sec_name, GNU_LINKONCE_WT))
496 styp_flags = STYP_DEBUG_INFO;
498 #endif
499 #ifdef RS6000COFF_C
500 else if (!strcmp (sec_name, _TDATA))
502 styp_flags = STYP_TDATA;
504 else if (!strcmp (sec_name, _TBSS))
506 styp_flags = STYP_TBSS;
508 else if (!strcmp (sec_name, _PAD))
510 styp_flags = STYP_PAD;
512 else if (!strcmp (sec_name, _LOADER))
514 styp_flags = STYP_LOADER;
516 else if (!strcmp (sec_name, _EXCEPT))
518 styp_flags = STYP_EXCEPT;
520 else if (!strcmp (sec_name, _TYPCHK))
522 styp_flags = STYP_TYPCHK;
524 else if (sec_flags & SEC_DEBUGGING)
526 int i;
528 for (i = 0; i < XCOFF_DWSECT_NBR_NAMES; i++)
529 if (!strcmp (sec_name, xcoff_dwsect_names[i].xcoff_name))
531 styp_flags = STYP_DWARF | xcoff_dwsect_names[i].flag;
532 break;
535 #endif
536 /* Try and figure out what it should be */
537 else if (sec_flags & SEC_CODE)
539 styp_flags = STYP_TEXT;
541 else if (sec_flags & SEC_DATA)
543 styp_flags = STYP_DATA;
545 else if (sec_flags & SEC_READONLY)
547 #ifdef STYP_LIT /* 29k readonly text/data section */
548 styp_flags = STYP_LIT;
549 #else
550 styp_flags = STYP_TEXT;
551 #endif /* STYP_LIT */
553 else if (sec_flags & SEC_LOAD)
555 styp_flags = STYP_TEXT;
557 else if (sec_flags & SEC_ALLOC)
559 styp_flags = STYP_BSS;
562 #ifdef STYP_CLINK
563 if (sec_flags & SEC_TIC54X_CLINK)
564 styp_flags |= STYP_CLINK;
565 #endif
567 #ifdef STYP_BLOCK
568 if (sec_flags & SEC_TIC54X_BLOCK)
569 styp_flags |= STYP_BLOCK;
570 #endif
572 #ifdef STYP_NOLOAD
573 if ((sec_flags & (SEC_NEVER_LOAD | SEC_COFF_SHARED_LIBRARY)) != 0)
574 styp_flags |= STYP_NOLOAD;
575 #endif
577 return styp_flags;
580 #else /* COFF_WITH_PE */
582 /* The PE version; see above for the general comments. The non-PE
583 case seems to be more guessing, and breaks PE format; specifically,
584 .rdata is readonly, but it sure ain't text. Really, all this
585 should be set up properly in gas (or whatever assembler is in use),
586 and honor whatever objcopy/strip, etc. sent us as input. */
588 static long
589 sec_to_styp_flags (const char *sec_name, flagword sec_flags)
591 long styp_flags = 0;
592 bool is_dbg = false;
594 if (startswith (sec_name, DOT_DEBUG)
595 || startswith (sec_name, DOT_ZDEBUG)
596 #ifdef COFF_LONG_SECTION_NAMES
597 || startswith (sec_name, GNU_LINKONCE_WI)
598 || startswith (sec_name, GNU_LINKONCE_WT)
599 #endif
600 || startswith (sec_name, ".stab"))
601 is_dbg = true;
603 /* caution: there are at least three groups of symbols that have
604 very similar bits and meanings: IMAGE_SCN*, SEC_*, and STYP_*.
605 SEC_* are the BFD internal flags, used for generic BFD
606 information. STYP_* are the COFF section flags which appear in
607 COFF files. IMAGE_SCN_* are the PE section flags which appear in
608 PE files. The STYP_* flags and the IMAGE_SCN_* flags overlap,
609 but there are more IMAGE_SCN_* flags. */
611 /* FIXME: There is no gas syntax to specify the debug section flag. */
612 if (is_dbg)
614 sec_flags &= (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD
615 | SEC_LINK_DUPLICATES_SAME_CONTENTS
616 | SEC_LINK_DUPLICATES_SAME_SIZE);
617 sec_flags |= SEC_DEBUGGING | SEC_READONLY;
620 /* skip LOAD */
621 /* READONLY later */
622 /* skip RELOC */
623 if ((sec_flags & SEC_CODE) != 0)
624 styp_flags |= IMAGE_SCN_CNT_CODE;
625 if ((sec_flags & (SEC_DATA | SEC_DEBUGGING)) != 0)
626 styp_flags |= IMAGE_SCN_CNT_INITIALIZED_DATA;
627 if ((sec_flags & SEC_ALLOC) != 0 && (sec_flags & SEC_LOAD) == 0)
628 styp_flags |= IMAGE_SCN_CNT_UNINITIALIZED_DATA; /* ==STYP_BSS */
629 /* skip ROM */
630 /* skip constRUCTOR */
631 /* skip CONTENTS */
632 #ifndef COFF_IMAGE_WITH_PE
633 /* I don't think any of the IMAGE_SCN_LNK_* flags set below should be set
634 when the output is PE. Only object files should have them, for the linker
635 to consume. */
636 if ((sec_flags & SEC_IS_COMMON) != 0)
637 styp_flags |= IMAGE_SCN_LNK_COMDAT;
638 #endif
639 if ((sec_flags & SEC_DEBUGGING) != 0)
640 styp_flags |= IMAGE_SCN_MEM_DISCARDABLE;
641 if ((sec_flags & (SEC_EXCLUDE | SEC_NEVER_LOAD)) != 0 && !is_dbg)
642 #ifdef COFF_IMAGE_WITH_PE
643 styp_flags |= IMAGE_SCN_MEM_DISCARDABLE;
644 #else
645 styp_flags |= IMAGE_SCN_LNK_REMOVE;
646 #endif
647 /* skip IN_MEMORY */
648 /* skip SORT */
649 #ifndef COFF_IMAGE_WITH_PE
650 if (sec_flags & SEC_LINK_ONCE)
651 styp_flags |= IMAGE_SCN_LNK_COMDAT;
652 if ((sec_flags
653 & (SEC_LINK_DUPLICATES_DISCARD | SEC_LINK_DUPLICATES_SAME_CONTENTS
654 | SEC_LINK_DUPLICATES_SAME_SIZE)) != 0)
655 styp_flags |= IMAGE_SCN_LNK_COMDAT;
656 #endif
658 /* skip LINKER_CREATED */
660 if ((sec_flags & SEC_COFF_NOREAD) == 0)
661 styp_flags |= IMAGE_SCN_MEM_READ; /* Invert NOREAD for read. */
662 if ((sec_flags & SEC_READONLY) == 0)
663 styp_flags |= IMAGE_SCN_MEM_WRITE; /* Invert READONLY for write. */
664 if (sec_flags & SEC_CODE)
665 styp_flags |= IMAGE_SCN_MEM_EXECUTE; /* CODE->EXECUTE. */
666 if (sec_flags & SEC_COFF_SHARED)
667 styp_flags |= IMAGE_SCN_MEM_SHARED; /* Shared remains meaningful. */
669 return styp_flags;
672 #endif /* COFF_WITH_PE */
674 /* Return a word with SEC_* flags set to represent the incoming STYP_*
675 flags (from scnhdr.s_flags). The inverse of this function is
676 sec_to_styp_flags(). NOTE: If you add to/change this routine, you
677 should probably mirror the changes in sec_to_styp_flags(). */
679 #ifndef COFF_WITH_PE
681 static bool
682 styp_to_sec_flags (bfd *abfd,
683 void * hdr,
684 const char *name,
685 asection *section ATTRIBUTE_UNUSED,
686 flagword *flags_ptr)
688 struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
689 unsigned long styp_flags = internal_s->s_flags;
690 flagword sec_flags = 0;
692 #ifdef STYP_BLOCK
693 if (styp_flags & STYP_BLOCK)
694 sec_flags |= SEC_TIC54X_BLOCK;
695 #endif
697 #ifdef STYP_CLINK
698 if (styp_flags & STYP_CLINK)
699 sec_flags |= SEC_TIC54X_CLINK;
700 #endif
702 #ifdef STYP_NOLOAD
703 if (styp_flags & STYP_NOLOAD)
704 sec_flags |= SEC_NEVER_LOAD;
705 #endif /* STYP_NOLOAD */
707 /* For 386 COFF, at least, an unloadable text or data section is
708 actually a shared library section. */
709 if (styp_flags & STYP_TEXT)
711 if (sec_flags & SEC_NEVER_LOAD)
712 sec_flags |= SEC_CODE | SEC_COFF_SHARED_LIBRARY;
713 else
714 sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
716 else if (styp_flags & STYP_DATA)
718 if (sec_flags & SEC_NEVER_LOAD)
719 sec_flags |= SEC_DATA | SEC_COFF_SHARED_LIBRARY;
720 else
721 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
723 else if (styp_flags & STYP_BSS)
725 #ifdef BSS_NOLOAD_IS_SHARED_LIBRARY
726 if (sec_flags & SEC_NEVER_LOAD)
727 sec_flags |= SEC_ALLOC | SEC_COFF_SHARED_LIBRARY;
728 else
729 #endif
730 sec_flags |= SEC_ALLOC;
732 else if (styp_flags & STYP_INFO)
734 /* We mark these as SEC_DEBUGGING, but only if COFF_PAGE_SIZE is
735 defined. coff_compute_section_file_positions uses
736 COFF_PAGE_SIZE to ensure that the low order bits of the
737 section VMA and the file offset match. If we don't know
738 COFF_PAGE_SIZE, we can't ensure the correct correspondence,
739 and demand page loading of the file will fail. */
740 #if defined (COFF_PAGE_SIZE) && !defined (COFF_ALIGN_IN_S_FLAGS)
741 sec_flags |= SEC_DEBUGGING;
742 #endif
744 else if (styp_flags & STYP_PAD)
745 sec_flags = 0;
746 #ifdef RS6000COFF_C
747 else if (styp_flags & STYP_TDATA)
749 if (sec_flags & SEC_NEVER_LOAD)
750 sec_flags |= SEC_DATA | SEC_THREAD_LOCAL | SEC_COFF_SHARED_LIBRARY;
751 else
752 sec_flags |= SEC_DATA | SEC_THREAD_LOCAL | SEC_LOAD | SEC_ALLOC;
754 else if (styp_flags & STYP_TBSS)
756 #ifdef BSS_NOLOAD_IS_SHARED_LIBRARY
757 if (sec_flags & SEC_NEVER_LOAD)
758 sec_flags |= SEC_ALLOC | SEC_THREAD_LOCAL | SEC_COFF_SHARED_LIBRARY;
759 else
760 #endif
761 sec_flags |= SEC_ALLOC | SEC_THREAD_LOCAL;
763 else if (styp_flags & STYP_EXCEPT)
764 sec_flags |= SEC_LOAD;
765 else if (styp_flags & STYP_LOADER)
766 sec_flags |= SEC_LOAD;
767 else if (styp_flags & STYP_TYPCHK)
768 sec_flags |= SEC_LOAD;
769 else if (styp_flags & STYP_DWARF)
770 sec_flags |= SEC_DEBUGGING;
771 #endif
772 else if (strcmp (name, _TEXT) == 0)
774 if (sec_flags & SEC_NEVER_LOAD)
775 sec_flags |= SEC_CODE | SEC_COFF_SHARED_LIBRARY;
776 else
777 sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
779 else if (strcmp (name, _DATA) == 0)
781 if (sec_flags & SEC_NEVER_LOAD)
782 sec_flags |= SEC_DATA | SEC_COFF_SHARED_LIBRARY;
783 else
784 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
786 else if (strcmp (name, _BSS) == 0)
788 #ifdef BSS_NOLOAD_IS_SHARED_LIBRARY
789 if (sec_flags & SEC_NEVER_LOAD)
790 sec_flags |= SEC_ALLOC | SEC_COFF_SHARED_LIBRARY;
791 else
792 #endif
793 sec_flags |= SEC_ALLOC;
795 else if (startswith (name, DOT_DEBUG)
796 || startswith (name, DOT_ZDEBUG)
797 #ifdef _COMMENT
798 || strcmp (name, _COMMENT) == 0
799 #endif
800 #ifdef COFF_LONG_SECTION_NAMES
801 || startswith (name, GNU_LINKONCE_WI)
802 || startswith (name, GNU_LINKONCE_WT)
803 #endif
804 || startswith (name, ".stab"))
806 #ifdef COFF_PAGE_SIZE
807 sec_flags |= SEC_DEBUGGING;
808 #endif
810 #ifdef _LIB
811 else if (strcmp (name, _LIB) == 0)
813 #endif
814 #ifdef _LIT
815 else if (strcmp (name, _LIT) == 0)
816 sec_flags = SEC_LOAD | SEC_ALLOC | SEC_READONLY;
817 #endif
818 else
819 sec_flags |= SEC_ALLOC | SEC_LOAD;
821 #ifdef STYP_LIT /* A29k readonly text/data section type. */
822 if ((styp_flags & STYP_LIT) == STYP_LIT)
823 sec_flags = (SEC_LOAD | SEC_ALLOC | SEC_READONLY);
824 #endif /* STYP_LIT */
826 #ifdef STYP_OTHER_LOAD /* Other loaded sections. */
827 if (styp_flags & STYP_OTHER_LOAD)
828 sec_flags = (SEC_LOAD | SEC_ALLOC);
829 #endif /* STYP_SDATA */
831 if ((bfd_applicable_section_flags (abfd) & SEC_SMALL_DATA) != 0
832 && (startswith (name, ".sbss")
833 || startswith (name, ".sdata")))
834 sec_flags |= SEC_SMALL_DATA;
836 #if defined (COFF_LONG_SECTION_NAMES) && defined (COFF_SUPPORT_GNU_LINKONCE)
837 /* As a GNU extension, if the name begins with .gnu.linkonce, we
838 only link a single copy of the section. This is used to support
839 g++. g++ will emit each template expansion in its own section.
840 The symbols will be defined as weak, so that multiple definitions
841 are permitted. The GNU linker extension is to actually discard
842 all but one of the sections. */
843 if (startswith (name, ".gnu.linkonce"))
844 sec_flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
845 #endif
847 if (flags_ptr == NULL)
848 return false;
850 * flags_ptr = sec_flags;
851 return true;
854 #else /* COFF_WITH_PE */
856 static hashval_t
857 comdat_hashf (const void *entry)
859 const struct comdat_hash_entry *fe = entry;
860 return fe->target_index;
863 static int
864 comdat_eqf (const void *e1, const void *e2)
866 const struct comdat_hash_entry *fe1 = e1;
867 const struct comdat_hash_entry *fe2 = e2;
868 return fe1->target_index == fe2->target_index;
871 static void
872 comdat_delf (void *ent)
874 struct comdat_hash_entry *e = ent;
875 free (e->comdat_name);
876 free (e->symname);
877 free (e);
880 static struct comdat_hash_entry *
881 find_flags (htab_t comdat_hash, int target_index)
883 struct comdat_hash_entry needle;
884 needle.target_index = target_index;
886 return htab_find (comdat_hash, &needle);
889 static bool
890 fill_comdat_hash (bfd *abfd)
892 bfd_byte *esymstart, *esym, *esymend;
894 /* Unfortunately, the PE format stores essential information in the
895 symbol table, of all places. We need to extract that information
896 now, so that objdump and the linker will know how to handle the
897 section without worrying about the symbols. We can't call
898 slurp_symtab, because the linker doesn't want the swapped symbols. */
900 /* COMDAT sections are special. The first symbol is the section
901 symbol, which tells what kind of COMDAT section it is. The
902 second symbol is the "comdat symbol" - the one with the unique
903 name. GNU uses the section symbol for the unique name; MS uses
904 ".text" for every comdat section. Sigh. - DJ. */
906 /* This is not mirrored in sec_to_styp_flags(), but there doesn't
907 seem to be a need to, either, and it would at best be rather messy. */
909 if (! _bfd_coff_get_external_symbols (abfd))
910 return true;
912 esymstart = esym = (bfd_byte *) obj_coff_external_syms (abfd);
913 esymend = esym + obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd);
915 for (struct internal_syment isym;
916 esym < esymend;
917 esym += (isym.n_numaux + 1) * bfd_coff_symesz (abfd))
919 char buf[SYMNMLEN + 1];
920 const char *symname;
921 flagword sec_flags = SEC_LINK_ONCE;
923 bfd_coff_swap_sym_in (abfd, esym, &isym);
925 /* According to the MSVC documentation, the first TWO entries
926 with the section # are both of interest to us. The first one
927 is the "section symbol" (section name). The second is the
928 comdat symbol name. Here, we've found the first qualifying
929 entry; we distinguish it from the second with a state flag.
931 In the case of gas-generated (at least until that is fixed)
932 .o files, it isn't necessarily the second one. It may be
933 some other later symbol.
935 Since gas also doesn't follow MS conventions and emits the
936 section similar to .text$<name>, where <something> is the
937 name we're looking for, we distinguish the two as follows:
939 If the section name is simply a section name (no $) we
940 presume it's MS-generated, and look at precisely the second
941 symbol for the comdat name. If the section name has a $, we
942 assume it's gas-generated, and look for <something> (whatever
943 follows the $) as the comdat symbol. */
945 /* All 3 branches use this. */
946 symname = _bfd_coff_internal_syment_name (abfd, &isym, buf);
948 /* PR 17512 file: 078-11867-0.004 */
949 if (symname == NULL)
951 _bfd_error_handler (_("%pB: unable to load COMDAT section name"),
952 abfd);
953 continue;
956 union internal_auxent aux;
958 struct comdat_hash_entry needle;
959 needle.target_index = isym.n_scnum;
961 void **slot
962 = htab_find_slot (pe_data (abfd)->comdat_hash, &needle, INSERT);
963 if (slot == NULL)
964 return false;
966 if (*slot == NULL)
968 if (isym.n_numaux != 1)
969 aux.x_scn.x_comdat = 0;
970 else
972 /* PR 17512: file: e2cfe54f. */
973 if (esym + isym.n_numaux * bfd_coff_symesz (abfd) >= esymend)
975 /* xgettext:c-format */
976 _bfd_error_handler (_("%pB: warning: no symbol for"
977 " section '%s' found"),
978 abfd, symname);
979 continue;
981 bfd_coff_swap_aux_in (abfd, (esym + bfd_coff_symesz (abfd)),
982 isym.n_type, isym.n_sclass, 0,
983 isym.n_numaux, &aux);
986 /* FIXME: Microsoft uses NODUPLICATES and ASSOCIATIVE, but
987 gnu uses ANY and SAME_SIZE. Unfortunately, gnu doesn't
988 do the comdat symbols right. So, until we can fix it to
989 do the right thing, we are temporarily disabling comdats
990 for the MS types (they're used in DLLs and C++, but we
991 don't support *their* C++ libraries anyway - DJ. */
993 /* Cygwin does not follow the MS style, and uses ANY and
994 SAME_SIZE where NODUPLICATES and ASSOCIATIVE should be
995 used. For Interix, we just do the right thing up
996 front. */
998 switch (aux.x_scn.x_comdat)
1000 case IMAGE_COMDAT_SELECT_NODUPLICATES:
1001 #ifdef STRICT_PE_FORMAT
1002 sec_flags |= SEC_LINK_DUPLICATES_ONE_ONLY;
1003 #else
1004 sec_flags &= ~SEC_LINK_ONCE;
1005 #endif
1006 break;
1008 case IMAGE_COMDAT_SELECT_ANY:
1009 sec_flags |= SEC_LINK_DUPLICATES_DISCARD;
1010 break;
1012 case IMAGE_COMDAT_SELECT_SAME_SIZE:
1013 sec_flags |= SEC_LINK_DUPLICATES_SAME_SIZE;
1014 break;
1016 case IMAGE_COMDAT_SELECT_EXACT_MATCH:
1017 /* Not yet fully implemented ??? */
1018 sec_flags |= SEC_LINK_DUPLICATES_SAME_CONTENTS;
1019 break;
1021 /* debug$S gets this case; other implications ??? */
1023 /* There may be no symbol. We'll search the whole
1024 table. Is this the right place to play this game?
1025 Or should we do it when reading it in? */
1026 case IMAGE_COMDAT_SELECT_ASSOCIATIVE:
1027 #ifdef STRICT_PE_FORMAT
1028 /* FIXME: This is not currently implemented. */
1029 sec_flags |= SEC_LINK_DUPLICATES_DISCARD;
1030 #else
1031 sec_flags &= ~SEC_LINK_ONCE;
1032 #endif
1033 break;
1035 default: /* 0 means "no symbol" */
1036 /* debug$F gets this case; other implications ??? */
1037 sec_flags |= SEC_LINK_DUPLICATES_DISCARD;
1038 break;
1041 *slot = bfd_zmalloc (sizeof (struct comdat_hash_entry));
1042 if (*slot == NULL)
1043 return false;
1044 struct comdat_hash_entry *newentry = *slot;
1045 newentry->sec_flags = sec_flags;
1046 newentry->symname = bfd_strdup (symname);
1047 newentry->target_index = isym.n_scnum;
1048 newentry->isym = isym;
1049 newentry->comdat_symbol = -1;
1051 else
1053 struct comdat_hash_entry *entry = *slot;
1055 if (entry->comdat_symbol != -1)
1056 continue;
1058 char *target_name = strchr (entry->symname, '$');
1059 if (target_name != NULL)
1061 /* Gas mode: the first matching on partial name. */
1063 target_name += 1;
1064 #ifndef TARGET_UNDERSCORE
1065 #define TARGET_UNDERSCORE 0
1066 #endif
1067 /* Is this the name we're looking for ? */
1068 if (strcmp (target_name,
1069 symname + (TARGET_UNDERSCORE ? 1 : 0)) != 0)
1071 /* Not the name we're looking for */
1072 continue;
1075 /* MSVC mode: the lexically second symbol (or drop through
1076 from the above). */
1077 /* This must the second symbol with the section #. It is
1078 the actual symbol name. Intel puts the two adjacent, but
1079 Alpha (at least) spreads them out. */
1081 entry->comdat_symbol = (esym - esymstart) / bfd_coff_symesz (abfd);
1082 entry->comdat_name = bfd_strdup (symname);
1086 return true;
1089 static bool
1090 insert_coff_comdat_info (bfd *abfd, asection *section, const char *symname,
1091 long symbol)
1093 struct coff_comdat_info *comdat;
1094 size_t len = strlen (symname) + 1;
1096 comdat = bfd_alloc (abfd, sizeof (*comdat) + len);
1097 if (comdat == NULL)
1098 return false;
1100 coff_section_data (abfd, section)->comdat = comdat;
1101 comdat->symbol = symbol;
1102 char *newname = (char *) (comdat + 1);
1103 comdat->name = newname;
1104 memcpy (newname, symname, len);
1105 return true;
1108 static bool
1109 handle_COMDAT (bfd *abfd, flagword *sec_flags, const char *name,
1110 asection *section)
1112 if (pe_data (abfd)->comdat_hash == NULL)
1114 pe_data (abfd)->comdat_hash = htab_create (10, comdat_hashf, comdat_eqf,
1115 comdat_delf);
1116 if (pe_data (abfd)->comdat_hash == NULL)
1117 return false;
1120 if (htab_elements (pe_data (abfd)->comdat_hash) == 0)
1121 if (! fill_comdat_hash (abfd))
1122 return false;
1124 struct comdat_hash_entry *found
1125 = find_flags (pe_data (abfd)->comdat_hash, section->target_index);
1126 if (found != NULL)
1128 struct internal_syment isym = found->isym;
1130 /* If it isn't the stuff we're expecting, die; The MS
1131 documentation is vague, but it appears that the second entry
1132 serves BOTH as the comdat symbol and the defining symbol
1133 record (either C_STAT or C_EXT, possibly with an aux entry
1134 with debug information if it's a function.) It appears the
1135 only way to find the second one is to count. (On Intel, they
1136 appear to be adjacent, but on Alpha, they have been found
1137 separated.)
1139 Here, we think we've found the first one, but there's some
1140 checking we can do to be sure. */
1142 if (! ((isym.n_sclass == C_STAT || isym.n_sclass == C_EXT)
1143 && BTYPE (isym.n_type) == T_NULL && isym.n_value == 0))
1145 /* Malformed input files can trigger this test.
1146 cf PR 21781. */
1147 _bfd_error_handler
1148 (_("%pB: error: unexpected symbol '%s' in COMDAT section"),
1149 abfd, found->symname);
1150 return false;
1153 /* FIXME LATER: MSVC generates section names like .text for
1154 comdats. Gas generates names like .text$foo__Fv (in the case
1155 of a function). See comment above for more. */
1157 if (isym.n_sclass == C_STAT && strcmp (name, found->symname) != 0)
1158 /* xgettext:c-format */
1159 _bfd_error_handler (_("%pB: warning: COMDAT symbol '%s'"
1160 " does not match section name '%s'"),
1161 abfd, found->symname, name);
1163 if (found->comdat_symbol != -1)
1165 if (! insert_coff_comdat_info (abfd, section, found->comdat_name,
1166 found->comdat_symbol))
1167 return false;
1169 *sec_flags = *sec_flags | found->sec_flags;
1170 return true;
1172 *sec_flags = *sec_flags | SEC_LINK_ONCE;
1173 return true;
1177 /* The PE version; see above for the general comments.
1179 Since to set the SEC_LINK_ONCE and associated flags, we have to
1180 look at the symbol table anyway, we return the symbol table index
1181 of the symbol being used as the COMDAT symbol. This is admittedly
1182 ugly, but there's really nowhere else that we have access to the
1183 required information. FIXME: Is the COMDAT symbol index used for
1184 any purpose other than objdump? */
1186 static bool
1187 styp_to_sec_flags (bfd *abfd,
1188 void * hdr,
1189 const char *name,
1190 asection *section,
1191 flagword *flags_ptr)
1193 struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
1194 unsigned long styp_flags = internal_s->s_flags;
1195 flagword sec_flags;
1196 bool result = true;
1197 bool is_dbg = false;
1199 if (startswith (name, DOT_DEBUG)
1200 || startswith (name, DOT_ZDEBUG)
1201 #ifdef COFF_LONG_SECTION_NAMES
1202 || startswith (name, GNU_LINKONCE_WI)
1203 || startswith (name, GNU_LINKONCE_WT)
1204 /* FIXME: These definitions ought to be in a header file. */
1205 #define GNU_DEBUGLINK ".gnu_debuglink"
1206 #define GNU_DEBUGALTLINK ".gnu_debugaltlink"
1207 || startswith (name, GNU_DEBUGLINK)
1208 || startswith (name, GNU_DEBUGALTLINK)
1209 #endif
1210 || startswith (name, ".stab"))
1211 is_dbg = true;
1212 /* Assume read only unless IMAGE_SCN_MEM_WRITE is specified. */
1213 sec_flags = SEC_READONLY;
1215 /* If section disallows read, then set the NOREAD flag. */
1216 if ((styp_flags & IMAGE_SCN_MEM_READ) == 0)
1217 sec_flags |= SEC_COFF_NOREAD;
1219 /* Process each flag bit in styp_flags in turn. */
1220 while (styp_flags)
1222 unsigned long flag = styp_flags & - styp_flags;
1223 char * unhandled = NULL;
1225 styp_flags &= ~ flag;
1227 /* We infer from the distinct read/write/execute bits the settings
1228 of some of the bfd flags; the actual values, should we need them,
1229 are also in pei_section_data (abfd, section)->pe_flags. */
1231 switch (flag)
1233 case STYP_DSECT:
1234 unhandled = "STYP_DSECT";
1235 break;
1236 case STYP_GROUP:
1237 unhandled = "STYP_GROUP";
1238 break;
1239 case STYP_COPY:
1240 unhandled = "STYP_COPY";
1241 break;
1242 case STYP_OVER:
1243 unhandled = "STYP_OVER";
1244 break;
1245 #ifdef SEC_NEVER_LOAD
1246 case STYP_NOLOAD:
1247 sec_flags |= SEC_NEVER_LOAD;
1248 break;
1249 #endif
1250 case IMAGE_SCN_MEM_READ:
1251 sec_flags &= ~SEC_COFF_NOREAD;
1252 break;
1253 case IMAGE_SCN_TYPE_NO_PAD:
1254 /* Skip. */
1255 break;
1256 case IMAGE_SCN_LNK_OTHER:
1257 unhandled = "IMAGE_SCN_LNK_OTHER";
1258 break;
1259 case IMAGE_SCN_MEM_NOT_CACHED:
1260 unhandled = "IMAGE_SCN_MEM_NOT_CACHED";
1261 break;
1262 case IMAGE_SCN_MEM_NOT_PAGED:
1263 /* Generate a warning message rather using the 'unhandled'
1264 variable as this will allow some .sys files generate by
1265 other toolchains to be processed. See bugzilla issue 196. */
1266 /* xgettext:c-format */
1267 _bfd_error_handler (_("%pB: warning: ignoring section flag"
1268 " %s in section %s"),
1269 abfd, "IMAGE_SCN_MEM_NOT_PAGED", name);
1270 break;
1271 case IMAGE_SCN_MEM_EXECUTE:
1272 sec_flags |= SEC_CODE;
1273 break;
1274 case IMAGE_SCN_MEM_WRITE:
1275 sec_flags &= ~ SEC_READONLY;
1276 break;
1277 case IMAGE_SCN_MEM_DISCARDABLE:
1278 /* The MS PE spec says that debug sections are DISCARDABLE,
1279 but the presence of a DISCARDABLE flag does not necessarily
1280 mean that a given section contains debug information. Thus
1281 we only set the SEC_DEBUGGING flag on sections that we
1282 recognise as containing debug information. */
1283 if (is_dbg
1284 #ifdef _COMMENT
1285 || strcmp (name, _COMMENT) == 0
1286 #endif
1289 sec_flags |= SEC_DEBUGGING | SEC_READONLY;
1291 break;
1292 case IMAGE_SCN_MEM_SHARED:
1293 sec_flags |= SEC_COFF_SHARED;
1294 break;
1295 case IMAGE_SCN_LNK_REMOVE:
1296 if (!is_dbg)
1297 sec_flags |= SEC_EXCLUDE;
1298 break;
1299 case IMAGE_SCN_CNT_CODE:
1300 sec_flags |= SEC_CODE | SEC_ALLOC | SEC_LOAD;
1301 break;
1302 case IMAGE_SCN_CNT_INITIALIZED_DATA:
1303 if (is_dbg)
1304 sec_flags |= SEC_DEBUGGING;
1305 else
1306 sec_flags |= SEC_DATA | SEC_ALLOC | SEC_LOAD;
1307 break;
1308 case IMAGE_SCN_CNT_UNINITIALIZED_DATA:
1309 sec_flags |= SEC_ALLOC;
1310 break;
1311 case IMAGE_SCN_LNK_INFO:
1312 /* We mark these as SEC_DEBUGGING, but only if COFF_PAGE_SIZE is
1313 defined. coff_compute_section_file_positions uses
1314 COFF_PAGE_SIZE to ensure that the low order bits of the
1315 section VMA and the file offset match. If we don't know
1316 COFF_PAGE_SIZE, we can't ensure the correct correspondence,
1317 and demand page loading of the file will fail. */
1318 #ifdef COFF_PAGE_SIZE
1319 sec_flags |= SEC_DEBUGGING;
1320 #endif
1321 break;
1322 case IMAGE_SCN_LNK_COMDAT:
1323 /* COMDAT gets very special treatment. */
1324 if (!handle_COMDAT (abfd, &sec_flags, name, section))
1325 result = false;
1326 break;
1327 default:
1328 /* Silently ignore for now. */
1329 break;
1332 /* If the section flag was not handled, report it here. */
1333 if (unhandled != NULL)
1335 _bfd_error_handler
1336 /* xgettext:c-format */
1337 (_("%pB (%s): section flag %s (%#lx) ignored"),
1338 abfd, name, unhandled, flag);
1339 result = false;
1343 if ((bfd_applicable_section_flags (abfd) & SEC_SMALL_DATA) != 0
1344 && (startswith (name, ".sbss")
1345 || startswith (name, ".sdata")))
1346 sec_flags |= SEC_SMALL_DATA;
1348 #if defined (COFF_LONG_SECTION_NAMES) && defined (COFF_SUPPORT_GNU_LINKONCE)
1349 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1350 only link a single copy of the section. This is used to support
1351 g++. g++ will emit each template expansion in its own section.
1352 The symbols will be defined as weak, so that multiple definitions
1353 are permitted. The GNU linker extension is to actually discard
1354 all but one of the sections. */
1355 if (startswith (name, ".gnu.linkonce"))
1356 sec_flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1357 #endif
1359 if (flags_ptr)
1360 * flags_ptr = sec_flags;
1362 return result;
1365 #endif /* COFF_WITH_PE */
1367 #define get_index(symbol) ((symbol)->udata.i)
1370 INTERNAL_DEFINITION
1371 bfd_coff_backend_data
1373 INTERNAL
1374 .{* COFF symbol classifications. *}
1376 .enum coff_symbol_classification
1378 . {* Global symbol. *}
1379 . COFF_SYMBOL_GLOBAL,
1380 . {* Common symbol. *}
1381 . COFF_SYMBOL_COMMON,
1382 . {* Undefined symbol. *}
1383 . COFF_SYMBOL_UNDEFINED,
1384 . {* Local symbol. *}
1385 . COFF_SYMBOL_LOCAL,
1386 . {* PE section symbol. *}
1387 . COFF_SYMBOL_PE_SECTION
1390 .typedef asection * (*coff_gc_mark_hook_fn)
1391 . (asection *, struct bfd_link_info *, struct internal_reloc *,
1392 . struct coff_link_hash_entry *, struct internal_syment *);
1395 Special entry points for gdb to swap in coff symbol table parts:
1397 CODE_FRAGMENT
1398 .typedef struct
1400 . void (*_bfd_coff_swap_aux_in)
1401 . (bfd *, void *, int, int, int, int, void *);
1403 . void (*_bfd_coff_swap_sym_in)
1404 . (bfd *, void *, void *);
1406 . void (*_bfd_coff_swap_lineno_in)
1407 . (bfd *, void *, void *);
1409 . unsigned int (*_bfd_coff_swap_aux_out)
1410 . (bfd *, void *, int, int, int, int, void *);
1412 . unsigned int (*_bfd_coff_swap_sym_out)
1413 . (bfd *, void *, void *);
1415 . unsigned int (*_bfd_coff_swap_lineno_out)
1416 . (bfd *, void *, void *);
1418 . unsigned int (*_bfd_coff_swap_reloc_out)
1419 . (bfd *, void *, void *);
1421 . unsigned int (*_bfd_coff_swap_filehdr_out)
1422 . (bfd *, void *, void *);
1424 . unsigned int (*_bfd_coff_swap_aouthdr_out)
1425 . (bfd *, void *, void *);
1427 . unsigned int (*_bfd_coff_swap_scnhdr_out)
1428 . (bfd *, void *, void *);
1430 . unsigned int _bfd_filhsz;
1431 . unsigned int _bfd_aoutsz;
1432 . unsigned int _bfd_scnhsz;
1433 . unsigned int _bfd_symesz;
1434 . unsigned int _bfd_auxesz;
1435 . unsigned int _bfd_relsz;
1436 . unsigned int _bfd_linesz;
1437 . unsigned int _bfd_filnmlen;
1438 . bool _bfd_coff_long_filenames;
1440 . bool _bfd_coff_long_section_names;
1441 . bool (*_bfd_coff_set_long_section_names)
1442 . (bfd *, int);
1444 . unsigned int _bfd_coff_default_section_alignment_power;
1445 . bool _bfd_coff_force_symnames_in_strings;
1446 . unsigned int _bfd_coff_debug_string_prefix_length;
1447 . unsigned int _bfd_coff_max_nscns;
1449 . void (*_bfd_coff_swap_filehdr_in)
1450 . (bfd *, void *, void *);
1452 . void (*_bfd_coff_swap_aouthdr_in)
1453 . (bfd *, void *, void *);
1455 . void (*_bfd_coff_swap_scnhdr_in)
1456 . (bfd *, void *, void *);
1458 . void (*_bfd_coff_swap_reloc_in)
1459 . (bfd *abfd, void *, void *);
1461 . bool (*_bfd_coff_bad_format_hook)
1462 . (bfd *, void *);
1464 . bool (*_bfd_coff_set_arch_mach_hook)
1465 . (bfd *, void *);
1467 . void * (*_bfd_coff_mkobject_hook)
1468 . (bfd *, void *, void *);
1470 . bool (*_bfd_styp_to_sec_flags_hook)
1471 . (bfd *, void *, const char *, asection *, flagword *);
1473 . void (*_bfd_set_alignment_hook)
1474 . (bfd *, asection *, void *);
1476 . bool (*_bfd_coff_slurp_symbol_table)
1477 . (bfd *);
1479 . bool (*_bfd_coff_symname_in_debug)
1480 . (bfd *, struct internal_syment *);
1482 . bool (*_bfd_coff_pointerize_aux_hook)
1483 . (bfd *, combined_entry_type *, combined_entry_type *,
1484 . unsigned int, combined_entry_type *);
1486 . bool (*_bfd_coff_print_aux)
1487 . (bfd *, FILE *, combined_entry_type *, combined_entry_type *,
1488 . combined_entry_type *, unsigned int);
1490 . bool (*_bfd_coff_reloc16_extra_cases)
1491 . (bfd *, struct bfd_link_info *, struct bfd_link_order *, arelent *,
1492 . bfd_byte *, size_t *, size_t *);
1494 . int (*_bfd_coff_reloc16_estimate)
1495 . (bfd *, asection *, arelent *, unsigned int,
1496 . struct bfd_link_info *);
1498 . enum coff_symbol_classification (*_bfd_coff_classify_symbol)
1499 . (bfd *, struct internal_syment *);
1501 . bool (*_bfd_coff_compute_section_file_positions)
1502 . (bfd *);
1504 . bool (*_bfd_coff_start_final_link)
1505 . (bfd *, struct bfd_link_info *);
1507 . bool (*_bfd_coff_relocate_section)
1508 . (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
1509 . struct internal_reloc *, struct internal_syment *, asection **);
1511 . reloc_howto_type *(*_bfd_coff_rtype_to_howto)
1512 . (bfd *, asection *, struct internal_reloc *,
1513 . struct coff_link_hash_entry *, struct internal_syment *, bfd_vma *);
1515 . bool (*_bfd_coff_adjust_symndx)
1516 . (bfd *, struct bfd_link_info *, bfd *, asection *,
1517 . struct internal_reloc *, bool *);
1519 . bool (*_bfd_coff_link_add_one_symbol)
1520 . (struct bfd_link_info *, bfd *, const char *, flagword,
1521 . asection *, bfd_vma, const char *, bool, bool,
1522 . struct bfd_link_hash_entry **);
1524 . bool (*_bfd_coff_link_output_has_begun)
1525 . (bfd *, struct coff_final_link_info *);
1527 . bool (*_bfd_coff_final_link_postscript)
1528 . (bfd *, struct coff_final_link_info *);
1530 . bool (*_bfd_coff_print_pdata)
1531 . (bfd *, void *);
1533 .} bfd_coff_backend_data;
1536 INTERNAL
1537 .#define coff_backend_info(abfd) \
1538 . ((const bfd_coff_backend_data *) (abfd)->xvec->backend_data)
1540 .#define bfd_coff_swap_aux_in(a,e,t,c,ind,num,i) \
1541 . ((coff_backend_info (a)->_bfd_coff_swap_aux_in) (a,e,t,c,ind,num,i))
1543 .#define bfd_coff_swap_sym_in(a,e,i) \
1544 . ((coff_backend_info (a)->_bfd_coff_swap_sym_in) (a,e,i))
1546 .#define bfd_coff_swap_lineno_in(a,e,i) \
1547 . ((coff_backend_info ( a)->_bfd_coff_swap_lineno_in) (a,e,i))
1549 .#define bfd_coff_swap_reloc_out(abfd, i, o) \
1550 . ((coff_backend_info (abfd)->_bfd_coff_swap_reloc_out) (abfd, i, o))
1552 .#define bfd_coff_swap_lineno_out(abfd, i, o) \
1553 . ((coff_backend_info (abfd)->_bfd_coff_swap_lineno_out) (abfd, i, o))
1555 .#define bfd_coff_swap_aux_out(a,i,t,c,ind,num,o) \
1556 . ((coff_backend_info (a)->_bfd_coff_swap_aux_out) (a,i,t,c,ind,num,o))
1558 .#define bfd_coff_swap_sym_out(abfd, i,o) \
1559 . ((coff_backend_info (abfd)->_bfd_coff_swap_sym_out) (abfd, i, o))
1561 .#define bfd_coff_swap_scnhdr_out(abfd, i,o) \
1562 . ((coff_backend_info (abfd)->_bfd_coff_swap_scnhdr_out) (abfd, i, o))
1564 .#define bfd_coff_swap_filehdr_out(abfd, i,o) \
1565 . ((coff_backend_info (abfd)->_bfd_coff_swap_filehdr_out) (abfd, i, o))
1567 .#define bfd_coff_swap_aouthdr_out(abfd, i,o) \
1568 . ((coff_backend_info (abfd)->_bfd_coff_swap_aouthdr_out) (abfd, i, o))
1570 .#define bfd_coff_filhsz(abfd) (coff_backend_info (abfd)->_bfd_filhsz)
1571 .#define bfd_coff_aoutsz(abfd) (coff_backend_info (abfd)->_bfd_aoutsz)
1572 .#define bfd_coff_scnhsz(abfd) (coff_backend_info (abfd)->_bfd_scnhsz)
1573 .#define bfd_coff_symesz(abfd) (coff_backend_info (abfd)->_bfd_symesz)
1574 .#define bfd_coff_auxesz(abfd) (coff_backend_info (abfd)->_bfd_auxesz)
1575 .#define bfd_coff_relsz(abfd) (coff_backend_info (abfd)->_bfd_relsz)
1576 .#define bfd_coff_linesz(abfd) (coff_backend_info (abfd)->_bfd_linesz)
1577 .#define bfd_coff_filnmlen(abfd) (coff_backend_info (abfd)->_bfd_filnmlen)
1578 .#define bfd_coff_long_filenames(abfd) \
1579 . (coff_backend_info (abfd)->_bfd_coff_long_filenames)
1580 .#define bfd_coff_long_section_names(abfd) \
1581 . (coff_data (abfd)->long_section_names)
1582 .#define bfd_coff_set_long_section_names(abfd, enable) \
1583 . ((coff_backend_info (abfd)->_bfd_coff_set_long_section_names) (abfd, enable))
1584 .#define bfd_coff_default_section_alignment_power(abfd) \
1585 . (coff_backend_info (abfd)->_bfd_coff_default_section_alignment_power)
1586 .#define bfd_coff_max_nscns(abfd) \
1587 . (coff_backend_info (abfd)->_bfd_coff_max_nscns)
1589 .#define bfd_coff_swap_filehdr_in(abfd, i,o) \
1590 . ((coff_backend_info (abfd)->_bfd_coff_swap_filehdr_in) (abfd, i, o))
1592 .#define bfd_coff_swap_aouthdr_in(abfd, i,o) \
1593 . ((coff_backend_info (abfd)->_bfd_coff_swap_aouthdr_in) (abfd, i, o))
1595 .#define bfd_coff_swap_scnhdr_in(abfd, i,o) \
1596 . ((coff_backend_info (abfd)->_bfd_coff_swap_scnhdr_in) (abfd, i, o))
1598 .#define bfd_coff_swap_reloc_in(abfd, i, o) \
1599 . ((coff_backend_info (abfd)->_bfd_coff_swap_reloc_in) (abfd, i, o))
1601 .#define bfd_coff_bad_format_hook(abfd, filehdr) \
1602 . ((coff_backend_info (abfd)->_bfd_coff_bad_format_hook) (abfd, filehdr))
1604 .#define bfd_coff_set_arch_mach_hook(abfd, filehdr)\
1605 . ((coff_backend_info (abfd)->_bfd_coff_set_arch_mach_hook) (abfd, filehdr))
1606 .#define bfd_coff_mkobject_hook(abfd, filehdr, aouthdr)\
1607 . ((coff_backend_info (abfd)->_bfd_coff_mkobject_hook)\
1608 . (abfd, filehdr, aouthdr))
1610 .#define bfd_coff_styp_to_sec_flags_hook(abfd, scnhdr, name, section, flags_ptr)\
1611 . ((coff_backend_info (abfd)->_bfd_styp_to_sec_flags_hook)\
1612 . (abfd, scnhdr, name, section, flags_ptr))
1614 .#define bfd_coff_set_alignment_hook(abfd, sec, scnhdr)\
1615 . ((coff_backend_info (abfd)->_bfd_set_alignment_hook) (abfd, sec, scnhdr))
1617 .#define bfd_coff_slurp_symbol_table(abfd)\
1618 . ((coff_backend_info (abfd)->_bfd_coff_slurp_symbol_table) (abfd))
1620 .#define bfd_coff_symname_in_debug(abfd, sym)\
1621 . ((coff_backend_info (abfd)->_bfd_coff_symname_in_debug) (abfd, sym))
1623 .#define bfd_coff_force_symnames_in_strings(abfd)\
1624 . (coff_backend_info (abfd)->_bfd_coff_force_symnames_in_strings)
1626 .#define bfd_coff_debug_string_prefix_length(abfd)\
1627 . (coff_backend_info (abfd)->_bfd_coff_debug_string_prefix_length)
1629 .#define bfd_coff_print_aux(abfd, file, base, symbol, aux, indaux)\
1630 . ((coff_backend_info (abfd)->_bfd_coff_print_aux)\
1631 . (abfd, file, base, symbol, aux, indaux))
1633 .#define bfd_coff_reloc16_extra_cases(abfd, link_info, link_order,\
1634 . reloc, data, src_ptr, dst_ptr)\
1635 . ((coff_backend_info (abfd)->_bfd_coff_reloc16_extra_cases)\
1636 . (abfd, link_info, link_order, reloc, data, src_ptr, dst_ptr))
1638 .#define bfd_coff_reloc16_estimate(abfd, section, reloc, shrink, link_info)\
1639 . ((coff_backend_info (abfd)->_bfd_coff_reloc16_estimate)\
1640 . (abfd, section, reloc, shrink, link_info))
1642 .#define bfd_coff_classify_symbol(abfd, sym)\
1643 . ((coff_backend_info (abfd)->_bfd_coff_classify_symbol)\
1644 . (abfd, sym))
1646 .#define bfd_coff_compute_section_file_positions(abfd)\
1647 . ((coff_backend_info (abfd)->_bfd_coff_compute_section_file_positions)\
1648 . (abfd))
1650 .#define bfd_coff_start_final_link(obfd, info)\
1651 . ((coff_backend_info (obfd)->_bfd_coff_start_final_link)\
1652 . (obfd, info))
1653 .#define bfd_coff_relocate_section(obfd,info,ibfd,o,con,rel,isyms,secs)\
1654 . ((coff_backend_info (ibfd)->_bfd_coff_relocate_section)\
1655 . (obfd, info, ibfd, o, con, rel, isyms, secs))
1656 .#define bfd_coff_rtype_to_howto(abfd, sec, rel, h, sym, addendp)\
1657 . ((coff_backend_info (abfd)->_bfd_coff_rtype_to_howto)\
1658 . (abfd, sec, rel, h, sym, addendp))
1659 .#define bfd_coff_adjust_symndx(obfd, info, ibfd, sec, rel, adjustedp)\
1660 . ((coff_backend_info (abfd)->_bfd_coff_adjust_symndx)\
1661 . (obfd, info, ibfd, sec, rel, adjustedp))
1662 .#define bfd_coff_link_add_one_symbol(info, abfd, name, flags, section,\
1663 . value, string, cp, coll, hashp)\
1664 . ((coff_backend_info (abfd)->_bfd_coff_link_add_one_symbol)\
1665 . (info, abfd, name, flags, section, value, string, cp, coll, hashp))
1667 .#define bfd_coff_link_output_has_begun(a,p) \
1668 . ((coff_backend_info (a)->_bfd_coff_link_output_has_begun) (a, p))
1669 .#define bfd_coff_final_link_postscript(a,p) \
1670 . ((coff_backend_info (a)->_bfd_coff_final_link_postscript) (a, p))
1672 .#define bfd_coff_have_print_pdata(a) \
1673 . (coff_backend_info (a)->_bfd_coff_print_pdata)
1674 .#define bfd_coff_print_pdata(a,p) \
1675 . ((coff_backend_info (a)->_bfd_coff_print_pdata) (a, p))
1677 .{* Macro: Returns true if the bfd is a PE executable as opposed to a
1678 . PE object file. *}
1679 .#define bfd_pei_p(abfd) \
1680 . (startswith ((abfd)->xvec->name, "pei-"))
1683 /* See whether the magic number matches. */
1685 static bool
1686 coff_bad_format_hook (bfd * abfd ATTRIBUTE_UNUSED, void * filehdr)
1688 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
1690 if (BADMAG (*internal_f))
1691 return false;
1693 return true;
1696 #ifdef TICOFF
1697 static bool
1698 ticoff0_bad_format_hook (bfd *abfd ATTRIBUTE_UNUSED, void * filehdr)
1700 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
1702 if (COFF0_BADMAG (*internal_f))
1703 return false;
1705 return true;
1707 #endif
1709 #ifdef TICOFF
1710 static bool
1711 ticoff1_bad_format_hook (bfd *abfd ATTRIBUTE_UNUSED, void * filehdr)
1713 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
1715 if (COFF1_BADMAG (*internal_f))
1716 return false;
1718 return true;
1720 #endif
1722 /* Check whether this section uses an alignment other than the
1723 default. */
1725 static void
1726 coff_set_custom_section_alignment (bfd *abfd ATTRIBUTE_UNUSED,
1727 asection *section,
1728 const struct coff_section_alignment_entry *alignment_table,
1729 const unsigned int table_size)
1731 const unsigned int default_alignment = COFF_DEFAULT_SECTION_ALIGNMENT_POWER;
1732 unsigned int i;
1734 for (i = 0; i < table_size; ++i)
1736 const char *secname = bfd_section_name (section);
1738 if (alignment_table[i].comparison_length == (unsigned int) -1
1739 ? strcmp (alignment_table[i].name, secname) == 0
1740 : strncmp (alignment_table[i].name, secname,
1741 alignment_table[i].comparison_length) == 0)
1742 break;
1744 if (i >= table_size)
1745 return;
1747 if (alignment_table[i].default_alignment_min != COFF_ALIGNMENT_FIELD_EMPTY
1748 && default_alignment < alignment_table[i].default_alignment_min)
1749 return;
1751 if (alignment_table[i].default_alignment_max != COFF_ALIGNMENT_FIELD_EMPTY
1752 #if COFF_DEFAULT_SECTION_ALIGNMENT_POWER != 0
1753 && default_alignment > alignment_table[i].default_alignment_max
1754 #endif
1756 return;
1758 section->alignment_power = alignment_table[i].alignment_power;
1761 /* Custom section alignment records. */
1763 static const struct coff_section_alignment_entry
1764 coff_section_alignment_table[] =
1766 #ifdef COFF_SECTION_ALIGNMENT_ENTRIES
1767 COFF_SECTION_ALIGNMENT_ENTRIES,
1768 #endif
1769 /* There must not be any gaps between .stabstr sections. */
1770 { COFF_SECTION_NAME_PARTIAL_MATCH (".stabstr"),
1771 1, COFF_ALIGNMENT_FIELD_EMPTY, 0 },
1772 /* The .stab section must be aligned to 2**2 at most, to avoid gaps. */
1773 { COFF_SECTION_NAME_PARTIAL_MATCH (".stab"),
1774 3, COFF_ALIGNMENT_FIELD_EMPTY, 2 },
1775 /* Similarly for the .ctors and .dtors sections. */
1776 { COFF_SECTION_NAME_EXACT_MATCH (".ctors"),
1777 3, COFF_ALIGNMENT_FIELD_EMPTY, 2 },
1778 { COFF_SECTION_NAME_EXACT_MATCH (".dtors"),
1779 3, COFF_ALIGNMENT_FIELD_EMPTY, 2 }
1782 static const unsigned int coff_section_alignment_table_size =
1783 sizeof coff_section_alignment_table / sizeof coff_section_alignment_table[0];
1785 /* Initialize a section structure with information peculiar to this
1786 particular implementation of COFF. */
1788 static bool
1789 coff_new_section_hook (bfd * abfd, asection * section)
1791 combined_entry_type *native;
1792 size_t amt;
1793 unsigned char sclass = C_STAT;
1795 section->alignment_power = COFF_DEFAULT_SECTION_ALIGNMENT_POWER;
1797 #ifdef RS6000COFF_C
1798 if (bfd_xcoff_text_align_power (abfd) != 0
1799 && strcmp (bfd_section_name (section), ".text") == 0)
1800 section->alignment_power = bfd_xcoff_text_align_power (abfd);
1801 else if (bfd_xcoff_data_align_power (abfd) != 0
1802 && strcmp (bfd_section_name (section), ".data") == 0)
1803 section->alignment_power = bfd_xcoff_data_align_power (abfd);
1804 else
1806 int i;
1808 for (i = 0; i < XCOFF_DWSECT_NBR_NAMES; i++)
1809 if (strcmp (bfd_section_name (section),
1810 xcoff_dwsect_names[i].xcoff_name) == 0)
1812 section->alignment_power = 0;
1813 sclass = C_DWARF;
1814 break;
1817 #endif
1819 /* Set up the section symbol. */
1820 if (!_bfd_generic_new_section_hook (abfd, section))
1821 return false;
1823 /* Allocate aux records for section symbols, to store size and
1824 related info.
1826 @@ The 10 is a guess at a plausible maximum number of aux entries
1827 (but shouldn't be a constant). */
1828 amt = sizeof (combined_entry_type) * 10;
1829 native = (combined_entry_type *) bfd_zalloc (abfd, amt);
1830 if (native == NULL)
1831 return false;
1833 /* We don't need to set up n_name, n_value, or n_scnum in the native
1834 symbol information, since they'll be overridden by the BFD symbol
1835 anyhow. However, we do need to set the type and storage class,
1836 in case this symbol winds up getting written out. The value 0
1837 for n_numaux is already correct. */
1839 native->is_sym = true;
1840 native->u.syment.n_type = T_NULL;
1841 native->u.syment.n_sclass = sclass;
1843 coffsymbol (section->symbol)->native = native;
1845 coff_set_custom_section_alignment (abfd, section,
1846 coff_section_alignment_table,
1847 coff_section_alignment_table_size);
1849 return true;
1852 #ifdef COFF_ALIGN_IN_SECTION_HEADER
1854 /* Set the alignment of a BFD section. */
1856 static void
1857 coff_set_alignment_hook (bfd * abfd ATTRIBUTE_UNUSED,
1858 asection * section,
1859 void * scnhdr)
1861 struct internal_scnhdr *hdr = (struct internal_scnhdr *) scnhdr;
1862 unsigned int i;
1864 #ifdef COFF_DECODE_ALIGNMENT
1865 i = COFF_DECODE_ALIGNMENT(hdr->s_flags);
1866 #endif
1867 section->alignment_power = i;
1869 #ifdef coff_set_section_load_page
1870 coff_set_section_load_page (section, hdr->s_page);
1871 #endif
1874 #else /* ! COFF_ALIGN_IN_SECTION_HEADER */
1875 #ifdef COFF_WITH_PE
1877 static void
1878 coff_set_alignment_hook (bfd * abfd ATTRIBUTE_UNUSED,
1879 asection * section,
1880 void * scnhdr)
1882 struct internal_scnhdr *hdr = (struct internal_scnhdr *) scnhdr;
1883 size_t amt;
1884 unsigned int alignment_power_const
1885 = hdr->s_flags & IMAGE_SCN_ALIGN_POWER_BIT_MASK;
1887 switch (alignment_power_const)
1889 case IMAGE_SCN_ALIGN_8192BYTES:
1890 case IMAGE_SCN_ALIGN_4096BYTES:
1891 case IMAGE_SCN_ALIGN_2048BYTES:
1892 case IMAGE_SCN_ALIGN_1024BYTES:
1893 case IMAGE_SCN_ALIGN_512BYTES:
1894 case IMAGE_SCN_ALIGN_256BYTES:
1895 case IMAGE_SCN_ALIGN_128BYTES:
1896 case IMAGE_SCN_ALIGN_64BYTES:
1897 case IMAGE_SCN_ALIGN_32BYTES:
1898 case IMAGE_SCN_ALIGN_16BYTES:
1899 case IMAGE_SCN_ALIGN_8BYTES:
1900 case IMAGE_SCN_ALIGN_4BYTES:
1901 case IMAGE_SCN_ALIGN_2BYTES:
1902 case IMAGE_SCN_ALIGN_1BYTES:
1903 section->alignment_power
1904 = IMAGE_SCN_ALIGN_POWER_NUM (alignment_power_const);
1905 break;
1906 default:
1907 break;
1910 /* In a PE image file, the s_paddr field holds the virtual size of a
1911 section, while the s_size field holds the raw size. We also keep
1912 the original section flag value, since not every bit can be
1913 mapped onto a generic BFD section bit. */
1914 if (coff_section_data (abfd, section) == NULL)
1916 amt = sizeof (struct coff_section_tdata);
1917 section->used_by_bfd = bfd_zalloc (abfd, amt);
1918 if (section->used_by_bfd == NULL)
1919 /* FIXME: Return error. */
1920 abort ();
1923 if (pei_section_data (abfd, section) == NULL)
1925 amt = sizeof (struct pei_section_tdata);
1926 coff_section_data (abfd, section)->tdata = bfd_zalloc (abfd, amt);
1927 if (coff_section_data (abfd, section)->tdata == NULL)
1928 /* FIXME: Return error. */
1929 abort ();
1931 pei_section_data (abfd, section)->virt_size = hdr->s_paddr;
1932 pei_section_data (abfd, section)->pe_flags = hdr->s_flags;
1934 section->lma = hdr->s_vaddr;
1936 /* Check for extended relocs. */
1937 if (hdr->s_flags & IMAGE_SCN_LNK_NRELOC_OVFL)
1939 struct external_reloc dst;
1940 struct internal_reloc n;
1941 file_ptr oldpos = bfd_tell (abfd);
1942 bfd_size_type relsz = bfd_coff_relsz (abfd);
1944 if (bfd_seek (abfd, hdr->s_relptr, 0) != 0)
1945 return;
1946 if (bfd_read (& dst, relsz, abfd) != relsz)
1947 return;
1949 bfd_coff_swap_reloc_in (abfd, &dst, &n);
1950 if (bfd_seek (abfd, oldpos, 0) != 0)
1951 return;
1952 if (n.r_vaddr < 0x10000)
1954 _bfd_error_handler (_("%pB: overflow reloc count too small"), abfd);
1955 bfd_set_error (bfd_error_bad_value);
1956 return;
1958 section->reloc_count = hdr->s_nreloc = n.r_vaddr - 1;
1959 section->rel_filepos += relsz;
1961 else if (hdr->s_nreloc == 0xffff)
1962 _bfd_error_handler
1963 (_("%pB: warning: claims to have 0xffff relocs, without overflow"),
1964 abfd);
1966 #undef ALIGN_SET
1967 #undef ELIFALIGN_SET
1969 #else /* ! COFF_WITH_PE */
1970 #ifdef RS6000COFF_C
1972 /* We grossly abuse this function to handle XCOFF overflow headers.
1973 When we see one, we correct the reloc and line number counts in the
1974 real header, and remove the section we just created. */
1976 static void
1977 coff_set_alignment_hook (bfd *abfd, asection *section, void * scnhdr)
1979 struct internal_scnhdr *hdr = (struct internal_scnhdr *) scnhdr;
1980 asection *real_sec;
1982 if ((hdr->s_flags & STYP_OVRFLO) == 0)
1983 return;
1985 real_sec = coff_section_from_bfd_index (abfd, (int) hdr->s_nreloc);
1986 if (real_sec == NULL)
1987 return;
1989 real_sec->reloc_count = hdr->s_paddr;
1990 real_sec->lineno_count = hdr->s_vaddr;
1992 if (!bfd_section_removed_from_list (abfd, section))
1994 bfd_section_list_remove (abfd, section);
1995 --abfd->section_count;
1999 #else /* ! RS6000COFF_C */
2000 #if defined (COFF_GO32_EXE) || defined (COFF_GO32)
2002 static void
2003 coff_set_alignment_hook (bfd * abfd, asection * section, void * scnhdr)
2005 struct internal_scnhdr *hdr = (struct internal_scnhdr *) scnhdr;
2007 /* Check for extended relocs. */
2008 if (hdr->s_flags & IMAGE_SCN_LNK_NRELOC_OVFL)
2010 struct external_reloc dst;
2011 struct internal_reloc n;
2012 const file_ptr oldpos = bfd_tell (abfd);
2013 const bfd_size_type relsz = bfd_coff_relsz (abfd);
2015 if (bfd_seek (abfd, hdr->s_relptr, 0) != 0)
2016 return;
2017 if (bfd_read (& dst, relsz, abfd) != relsz)
2018 return;
2020 bfd_coff_swap_reloc_in (abfd, &dst, &n);
2021 if (bfd_seek (abfd, oldpos, 0) != 0)
2022 return;
2023 section->reloc_count = hdr->s_nreloc = n.r_vaddr - 1;
2024 section->rel_filepos += relsz;
2026 else if (hdr->s_nreloc == 0xffff)
2027 _bfd_error_handler
2028 (_("%pB: warning: claims to have 0xffff relocs, without overflow"),
2029 abfd);
2032 #else /* ! COFF_GO32_EXE && ! COFF_GO32 */
2034 static void
2035 coff_set_alignment_hook (bfd *abfd ATTRIBUTE_UNUSED,
2036 asection *section ATTRIBUTE_UNUSED,
2037 void *scnhdr ATTRIBUTE_UNUSED)
2041 #endif /* ! COFF_GO32_EXE && ! COFF_GO32 */
2042 #endif /* ! RS6000COFF_C */
2043 #endif /* ! COFF_WITH_PE */
2044 #endif /* ! COFF_ALIGN_IN_SECTION_HEADER */
2046 #ifndef coff_mkobject
2048 static bool
2049 coff_mkobject (bfd * abfd)
2051 coff_data_type *coff;
2052 size_t amt = sizeof (coff_data_type);
2054 abfd->tdata.coff_obj_data = bfd_zalloc (abfd, amt);
2055 if (abfd->tdata.coff_obj_data == NULL)
2056 return false;
2058 coff = coff_data (abfd);
2059 coff->symbols = NULL;
2060 coff->conversion_table = NULL;
2061 coff->raw_syments = NULL;
2062 coff->relocbase = 0;
2063 coff->local_toc_sym_map = 0;
2065 bfd_coff_long_section_names (abfd)
2066 = coff_backend_info (abfd)->_bfd_coff_long_section_names;
2068 /* make_abs_section(abfd);*/
2070 return true;
2072 #endif
2074 /* Create the COFF backend specific information. */
2076 #ifndef coff_mkobject_hook
2077 static void *
2078 coff_mkobject_hook (bfd * abfd,
2079 void * filehdr,
2080 void * aouthdr ATTRIBUTE_UNUSED)
2082 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
2083 coff_data_type *coff;
2085 if (! coff_mkobject (abfd))
2086 return NULL;
2088 coff = coff_data (abfd);
2090 coff->sym_filepos = internal_f->f_symptr;
2092 /* These members communicate important constants about the symbol
2093 table to GDB's symbol-reading code. These `constants'
2094 unfortunately vary among coff implementations... */
2095 coff->local_n_btmask = N_BTMASK;
2096 coff->local_n_btshft = N_BTSHFT;
2097 coff->local_n_tmask = N_TMASK;
2098 coff->local_n_tshift = N_TSHIFT;
2099 coff->local_symesz = bfd_coff_symesz (abfd);
2100 coff->local_auxesz = bfd_coff_auxesz (abfd);
2101 coff->local_linesz = bfd_coff_linesz (abfd);
2103 coff->timestamp = internal_f->f_timdat;
2105 obj_raw_syment_count (abfd) =
2106 obj_conv_table_size (abfd) =
2107 internal_f->f_nsyms;
2109 #ifdef RS6000COFF_C
2110 if ((internal_f->f_flags & F_SHROBJ) != 0)
2111 abfd->flags |= DYNAMIC;
2112 if (aouthdr != NULL && internal_f->f_opthdr >= bfd_coff_aoutsz (abfd))
2114 struct internal_aouthdr *internal_a =
2115 (struct internal_aouthdr *) aouthdr;
2116 struct xcoff_tdata *xcoff;
2118 xcoff = xcoff_data (abfd);
2119 # ifdef U803XTOCMAGIC
2120 xcoff->xcoff64 = internal_f->f_magic == U803XTOCMAGIC;
2121 # else
2122 xcoff->xcoff64 = 0;
2123 # endif
2124 xcoff->full_aouthdr = true;
2125 xcoff->toc = internal_a->o_toc;
2126 xcoff->sntoc = internal_a->o_sntoc;
2127 xcoff->snentry = internal_a->o_snentry;
2128 bfd_xcoff_text_align_power (abfd) = internal_a->o_algntext;
2129 bfd_xcoff_data_align_power (abfd) = internal_a->o_algndata;
2130 xcoff->modtype = internal_a->o_modtype;
2131 xcoff->cputype = internal_a->o_cputype;
2132 xcoff->maxdata = internal_a->o_maxdata;
2133 xcoff->maxstack = internal_a->o_maxstack;
2135 #endif
2137 #ifdef ARM
2138 /* Set the flags field from the COFF header read in. */
2139 if (! _bfd_coff_arm_set_private_flags (abfd, internal_f->f_flags))
2140 coff->flags = 0;
2141 #endif
2143 #ifdef COFF_WITH_PE
2144 /* FIXME: I'm not sure this is ever executed, since peicode.h
2145 defines coff_mkobject_hook. */
2146 if ((internal_f->f_flags & IMAGE_FILE_DEBUG_STRIPPED) == 0)
2147 abfd->flags |= HAS_DEBUG;
2148 #endif
2150 return coff;
2152 #endif
2154 /* Determine the machine architecture and type. FIXME: This is target
2155 dependent because the magic numbers are defined in the target
2156 dependent header files. But there is no particular need for this.
2157 If the magic numbers were moved to a separate file, this function
2158 would be target independent and would also be much more successful
2159 at linking together COFF files for different architectures. */
2161 static bool
2162 coff_set_arch_mach_hook (bfd *abfd, void * filehdr)
2164 unsigned long machine;
2165 enum bfd_architecture arch;
2166 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
2168 /* Zero selects the default machine for an arch. */
2169 machine = 0;
2170 switch (internal_f->f_magic)
2172 #ifdef I386MAGIC
2173 case I386MAGIC:
2174 case I386PTXMAGIC:
2175 case I386AIXMAGIC: /* Danbury PS/2 AIX C Compiler. */
2176 case LYNXCOFFMAGIC:
2177 case I386_APPLE_MAGIC:
2178 case I386_FREEBSD_MAGIC:
2179 case I386_LINUX_MAGIC:
2180 case I386_NETBSD_MAGIC:
2181 arch = bfd_arch_i386;
2182 break;
2183 #endif
2184 #ifdef AMD64MAGIC
2185 case AMD64MAGIC:
2186 case AMD64_APPLE_MAGIC:
2187 case AMD64_FREEBSD_MAGIC:
2188 case AMD64_LINUX_MAGIC:
2189 case AMD64_NETBSD_MAGIC:
2190 arch = bfd_arch_i386;
2191 machine = bfd_mach_x86_64;
2192 break;
2193 #endif
2194 #ifdef IA64MAGIC
2195 case IA64MAGIC:
2196 arch = bfd_arch_ia64;
2197 break;
2198 #endif
2199 #ifdef ARMMAGIC
2200 case ARMMAGIC:
2201 case ARMPEMAGIC:
2202 case THUMBPEMAGIC:
2203 arch = bfd_arch_arm;
2204 machine = bfd_arm_get_mach_from_notes (abfd, ARM_NOTE_SECTION);
2205 if (machine == bfd_mach_arm_unknown)
2207 switch (internal_f->f_flags & F_ARM_ARCHITECTURE_MASK)
2209 case F_ARM_2: machine = bfd_mach_arm_2; break;
2210 case F_ARM_2a: machine = bfd_mach_arm_2a; break;
2211 case F_ARM_3: machine = bfd_mach_arm_3; break;
2212 default:
2213 case F_ARM_3M: machine = bfd_mach_arm_3M; break;
2214 case F_ARM_4: machine = bfd_mach_arm_4; break;
2215 case F_ARM_4T: machine = bfd_mach_arm_4T; break;
2216 /* The COFF header does not have enough bits available
2217 to cover all the different ARM architectures. So
2218 we interpret F_ARM_5, the highest flag value to mean
2219 "the highest ARM architecture known to BFD" which is
2220 currently the XScale. */
2221 case F_ARM_5: machine = bfd_mach_arm_XScale; break;
2224 break;
2225 #endif
2226 #ifdef AARCH64MAGIC
2227 case AARCH64MAGIC:
2228 arch = bfd_arch_aarch64;
2229 machine = internal_f->f_flags & F_AARCH64_ARCHITECTURE_MASK;
2230 break;
2231 #endif
2232 #ifdef LOONGARCH64MAGIC
2233 case LOONGARCH64MAGIC:
2234 arch = bfd_arch_loongarch;
2235 machine = internal_f->f_flags & F_LOONGARCH64_ARCHITECTURE_MASK;
2236 break;
2237 #endif
2238 #ifdef Z80MAGIC
2239 case Z80MAGIC:
2240 arch = bfd_arch_z80;
2241 switch (internal_f->f_flags & F_MACHMASK)
2243 case bfd_mach_z80strict << 12:
2244 case bfd_mach_z80 << 12:
2245 case bfd_mach_z80n << 12:
2246 case bfd_mach_z80full << 12:
2247 case bfd_mach_r800 << 12:
2248 case bfd_mach_gbz80 << 12:
2249 case bfd_mach_z180 << 12:
2250 case bfd_mach_ez80_z80 << 12:
2251 case bfd_mach_ez80_adl << 12:
2252 machine = ((unsigned)internal_f->f_flags & F_MACHMASK) >> 12;
2253 break;
2254 default:
2255 return false;
2257 break;
2258 #endif
2259 #ifdef Z8KMAGIC
2260 case Z8KMAGIC:
2261 arch = bfd_arch_z8k;
2262 switch (internal_f->f_flags & F_MACHMASK)
2264 case F_Z8001:
2265 machine = bfd_mach_z8001;
2266 break;
2267 case F_Z8002:
2268 machine = bfd_mach_z8002;
2269 break;
2270 default:
2271 return false;
2273 break;
2274 #endif
2276 #ifdef RS6000COFF_C
2277 #ifdef XCOFF64
2278 case U64_TOCMAGIC:
2279 case U803XTOCMAGIC:
2280 #else
2281 case U802ROMAGIC:
2282 case U802WRMAGIC:
2283 case U802TOCMAGIC:
2284 #endif
2286 int cputype;
2288 if (xcoff_data (abfd)->cputype != -1)
2289 cputype = xcoff_data (abfd)->cputype & 0xff;
2290 else
2292 /* We did not get a value from the a.out header. If the
2293 file has not been stripped, we may be able to get the
2294 architecture information from the first symbol, if it
2295 is a .file symbol. */
2296 if (obj_raw_syment_count (abfd) == 0)
2297 cputype = 0;
2298 else
2300 bfd_byte *buf;
2301 struct internal_syment sym;
2302 bfd_size_type amt = bfd_coff_symesz (abfd);
2304 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
2305 return false;
2306 buf = _bfd_malloc_and_read (abfd, amt, amt);
2307 if (buf == NULL)
2308 return false;
2309 bfd_coff_swap_sym_in (abfd, buf, & sym);
2310 if (sym.n_sclass == C_FILE)
2311 cputype = sym.n_type & 0xff;
2312 else
2313 cputype = 0;
2314 free (buf);
2318 /* FIXME: We don't handle all cases here. */
2319 switch (cputype)
2321 default:
2322 case 0:
2323 arch = bfd_xcoff_architecture (abfd);
2324 machine = bfd_xcoff_machine (abfd);
2325 break;
2327 case 1:
2328 arch = bfd_arch_powerpc;
2329 machine = bfd_mach_ppc_601;
2330 break;
2331 case 2: /* 64 bit PowerPC */
2332 arch = bfd_arch_powerpc;
2333 machine = bfd_mach_ppc_620;
2334 break;
2335 case 3:
2336 arch = bfd_arch_powerpc;
2337 machine = bfd_mach_ppc;
2338 break;
2339 case 4:
2340 arch = bfd_arch_rs6000;
2341 machine = bfd_mach_rs6k;
2342 break;
2345 break;
2346 #endif
2348 #ifdef SH_ARCH_MAGIC_BIG
2349 case SH_ARCH_MAGIC_BIG:
2350 case SH_ARCH_MAGIC_LITTLE:
2351 #ifdef COFF_WITH_PE
2352 case SH_ARCH_MAGIC_WINCE:
2353 #endif
2354 arch = bfd_arch_sh;
2355 break;
2356 #endif
2358 #ifdef MIPS_ARCH_MAGIC_WINCE
2359 case MIPS_ARCH_MAGIC_WINCE:
2360 arch = bfd_arch_mips;
2361 break;
2362 #endif
2364 #ifdef SPARCMAGIC
2365 case SPARCMAGIC:
2366 #ifdef LYNXCOFFMAGIC
2367 case LYNXCOFFMAGIC:
2368 #endif
2369 arch = bfd_arch_sparc;
2370 break;
2371 #endif
2373 #ifdef TIC30MAGIC
2374 case TIC30MAGIC:
2375 arch = bfd_arch_tic30;
2376 break;
2377 #endif
2379 #ifdef TICOFF0MAGIC
2380 #ifdef TICOFF_TARGET_ARCH
2381 /* This TI COFF section should be used by all new TI COFF v0 targets. */
2382 case TICOFF0MAGIC:
2383 arch = TICOFF_TARGET_ARCH;
2384 machine = TICOFF_TARGET_MACHINE_GET (internal_f->f_flags);
2385 break;
2386 #endif
2387 #endif
2389 #ifdef TICOFF1MAGIC
2390 /* This TI COFF section should be used by all new TI COFF v1/2 targets. */
2391 /* TI COFF1 and COFF2 use the target_id field to specify which arch. */
2392 case TICOFF1MAGIC:
2393 case TICOFF2MAGIC:
2394 switch (internal_f->f_target_id)
2396 #ifdef TI_TARGET_ID
2397 case TI_TARGET_ID:
2398 arch = TICOFF_TARGET_ARCH;
2399 machine = TICOFF_TARGET_MACHINE_GET (internal_f->f_flags);
2400 break;
2401 #endif
2402 default:
2403 arch = bfd_arch_obscure;
2404 _bfd_error_handler
2405 (_("unrecognized TI COFF target id '0x%x'"),
2406 internal_f->f_target_id);
2407 break;
2409 break;
2410 #endif
2412 #ifdef MCOREMAGIC
2413 case MCOREMAGIC:
2414 arch = bfd_arch_mcore;
2415 break;
2416 #endif
2418 default: /* Unreadable input file type. */
2419 arch = bfd_arch_obscure;
2420 break;
2423 bfd_default_set_arch_mach (abfd, arch, machine);
2424 return true;
2427 static bool
2428 symname_in_debug_hook (bfd *abfd ATTRIBUTE_UNUSED,
2429 struct internal_syment *sym ATTRIBUTE_UNUSED)
2431 #ifdef SYMNAME_IN_DEBUG
2432 return SYMNAME_IN_DEBUG (sym) != 0;
2433 #else
2434 return false;
2435 #endif
2438 #ifdef RS6000COFF_C
2440 #ifdef XCOFF64
2441 #define FORCE_SYMNAMES_IN_STRINGS
2442 #endif
2444 /* Handle the csect auxent of a C_EXT, C_AIX_WEAKEXT or C_HIDEXT symbol. */
2446 static bool
2447 coff_pointerize_aux_hook (bfd *abfd ATTRIBUTE_UNUSED,
2448 combined_entry_type *table_base,
2449 combined_entry_type *symbol,
2450 unsigned int indaux,
2451 combined_entry_type *aux)
2453 BFD_ASSERT (symbol->is_sym);
2454 int n_sclass = symbol->u.syment.n_sclass;
2456 if (CSECT_SYM_P (n_sclass)
2457 && indaux + 1 == symbol->u.syment.n_numaux)
2459 BFD_ASSERT (! aux->is_sym);
2460 if (SMTYP_SMTYP (aux->u.auxent.x_csect.x_smtyp) == XTY_LD
2461 && aux->u.auxent.x_csect.x_scnlen.u64 < obj_raw_syment_count (abfd))
2463 aux->u.auxent.x_csect.x_scnlen.p =
2464 table_base + aux->u.auxent.x_csect.x_scnlen.u64;
2465 aux->fix_scnlen = 1;
2468 /* Return TRUE to indicate that the caller should not do any
2469 further work on this auxent. */
2470 return true;
2473 /* Return FALSE to indicate that this auxent should be handled by
2474 the caller. */
2475 return false;
2478 #else
2479 #define coff_pointerize_aux_hook 0
2480 #endif /* ! RS6000COFF_C */
2482 /* Print an aux entry. This returns TRUE if it has printed it. */
2484 static bool
2485 coff_print_aux (bfd *abfd ATTRIBUTE_UNUSED,
2486 FILE *file ATTRIBUTE_UNUSED,
2487 combined_entry_type *table_base ATTRIBUTE_UNUSED,
2488 combined_entry_type *symbol ATTRIBUTE_UNUSED,
2489 combined_entry_type *aux ATTRIBUTE_UNUSED,
2490 unsigned int indaux ATTRIBUTE_UNUSED)
2492 BFD_ASSERT (symbol->is_sym);
2493 BFD_ASSERT (! aux->is_sym);
2494 #ifdef RS6000COFF_C
2495 if (CSECT_SYM_P (symbol->u.syment.n_sclass)
2496 && indaux + 1 == symbol->u.syment.n_numaux)
2498 /* This is a csect entry. */
2499 fprintf (file, "AUX ");
2500 if (SMTYP_SMTYP (aux->u.auxent.x_csect.x_smtyp) != XTY_LD)
2502 BFD_ASSERT (! aux->fix_scnlen);
2503 fprintf (file, "val %5" PRIu64,
2504 aux->u.auxent.x_csect.x_scnlen.u64);
2506 else
2508 fprintf (file, "indx ");
2509 if (! aux->fix_scnlen)
2510 fprintf (file, "%4" PRIu64,
2511 aux->u.auxent.x_csect.x_scnlen.u64);
2512 else
2513 fprintf (file, "%4ld",
2514 (long) (aux->u.auxent.x_csect.x_scnlen.p - table_base));
2516 fprintf (file,
2517 " prmhsh %u snhsh %u typ %d algn %d clss %u stb %u snstb %u",
2518 aux->u.auxent.x_csect.x_parmhash,
2519 (unsigned int) aux->u.auxent.x_csect.x_snhash,
2520 SMTYP_SMTYP (aux->u.auxent.x_csect.x_smtyp),
2521 SMTYP_ALIGN (aux->u.auxent.x_csect.x_smtyp),
2522 (unsigned int) aux->u.auxent.x_csect.x_smclas,
2523 aux->u.auxent.x_csect.x_stab,
2524 (unsigned int) aux->u.auxent.x_csect.x_snstab);
2525 return true;
2527 #endif
2529 /* Return FALSE to indicate that no special action was taken. */
2530 return false;
2534 SUBSUBSECTION
2535 Writing relocations
2537 To write relocations, the back end steps though the
2538 canonical relocation table and create an
2539 @code{internal_reloc}. The symbol index to use is removed from
2540 the @code{offset} field in the symbol table supplied. The
2541 address comes directly from the sum of the section base
2542 address and the relocation offset; the type is dug directly
2543 from the howto field. Then the @code{internal_reloc} is
2544 swapped into the shape of an @code{external_reloc} and written
2545 out to disk.
2549 #ifdef TARG_AUX
2552 /* AUX's ld wants relocations to be sorted. */
2553 static int
2554 compare_arelent_ptr (const void * x, const void * y)
2556 const arelent **a = (const arelent **) x;
2557 const arelent **b = (const arelent **) y;
2558 bfd_size_type aadr = (*a)->address;
2559 bfd_size_type badr = (*b)->address;
2561 return (aadr < badr ? -1 : badr < aadr ? 1 : 0);
2564 #endif /* TARG_AUX */
2566 static bool
2567 coff_write_relocs (bfd * abfd, int first_undef)
2569 asection *s;
2571 for (s = abfd->sections; s != NULL; s = s->next)
2573 unsigned int i;
2574 struct external_reloc dst;
2575 arelent **p;
2577 #ifndef TARG_AUX
2578 p = s->orelocation;
2579 #else
2581 /* Sort relocations before we write them out. */
2582 bfd_size_type amt;
2584 amt = s->reloc_count;
2585 amt *= sizeof (arelent *);
2586 p = bfd_malloc (amt);
2587 if (p == NULL)
2589 if (s->reloc_count > 0)
2590 return false;
2592 else
2594 memcpy (p, s->orelocation, (size_t) amt);
2595 qsort (p, s->reloc_count, sizeof (arelent *), compare_arelent_ptr);
2598 #endif
2600 if (bfd_seek (abfd, s->rel_filepos, SEEK_SET) != 0)
2601 return false;
2603 #ifdef COFF_WITH_EXTENDED_RELOC_COUNTER
2604 if ((obj_pe (abfd) || obj_go32 (abfd)) && s->reloc_count >= 0xffff)
2606 /* Encode real count here as first reloc. */
2607 struct internal_reloc n;
2609 memset (& n, 0, sizeof (n));
2610 /* Add one to count *this* reloc (grr). */
2611 n.r_vaddr = s->reloc_count + 1;
2612 coff_swap_reloc_out (abfd, &n, &dst);
2613 if (bfd_write (&dst, bfd_coff_relsz (abfd), abfd)
2614 != bfd_coff_relsz (abfd))
2615 return false;
2617 #endif
2619 for (i = 0; i < s->reloc_count; i++)
2621 struct internal_reloc n;
2622 arelent *q = p[i];
2624 memset (& n, 0, sizeof (n));
2626 /* Now we've renumbered the symbols we know where the
2627 undefined symbols live in the table. Check the reloc
2628 entries for symbols who's output bfd isn't the right one.
2629 This is because the symbol was undefined (which means
2630 that all the pointers are never made to point to the same
2631 place). This is a bad thing,'cause the symbols attached
2632 to the output bfd are indexed, so that the relocation
2633 entries know which symbol index they point to. So we
2634 have to look up the output symbol here. */
2636 if (q->sym_ptr_ptr[0] != NULL && q->sym_ptr_ptr[0]->the_bfd != abfd)
2638 int j;
2639 const char *sname = q->sym_ptr_ptr[0]->name;
2640 asymbol **outsyms = abfd->outsymbols;
2642 for (j = first_undef; outsyms[j]; j++)
2644 const char *intable = outsyms[j]->name;
2646 if (strcmp (intable, sname) == 0)
2648 /* Got a hit, so repoint the reloc. */
2649 q->sym_ptr_ptr = outsyms + j;
2650 break;
2655 n.r_vaddr = q->address + s->vma;
2657 #ifdef R_IHCONST
2658 /* The 29k const/consth reloc pair is a real kludge. The consth
2659 part doesn't have a symbol; it has an offset. So rebuilt
2660 that here. */
2661 if (q->howto->type == R_IHCONST)
2662 n.r_symndx = q->addend;
2663 else
2664 #endif
2665 if (q->sym_ptr_ptr && q->sym_ptr_ptr[0] != NULL)
2667 #ifdef SECTION_RELATIVE_ABSOLUTE_SYMBOL_P
2668 if (SECTION_RELATIVE_ABSOLUTE_SYMBOL_P (q, s))
2669 #else
2670 if ((*q->sym_ptr_ptr)->section == bfd_abs_section_ptr
2671 && ((*q->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0)
2672 #endif
2673 /* This is a relocation relative to the absolute symbol. */
2674 n.r_symndx = -1;
2675 else
2677 n.r_symndx = get_index ((*(q->sym_ptr_ptr)));
2678 /* Check to see if the symbol reloc points to a symbol
2679 we don't have in our symbol table. */
2680 if (n.r_symndx > obj_conv_table_size (abfd))
2682 bfd_set_error (bfd_error_bad_value);
2683 /* xgettext:c-format */
2684 _bfd_error_handler (_("%pB: reloc against a non-existent"
2685 " symbol index: %ld"),
2686 abfd, n.r_symndx);
2687 return false;
2692 #ifdef SWAP_OUT_RELOC_OFFSET
2693 n.r_offset = q->addend;
2694 #endif
2696 #ifdef SELECT_RELOC
2697 /* Work out reloc type from what is required. */
2698 if (q->howto)
2699 SELECT_RELOC (n, q->howto);
2700 #else
2701 if (q->howto)
2702 n.r_type = q->howto->type;
2703 #endif
2704 coff_swap_reloc_out (abfd, &n, &dst);
2706 if (bfd_write (&dst, bfd_coff_relsz (abfd), abfd)
2707 != bfd_coff_relsz (abfd))
2708 return false;
2711 #ifdef TARG_AUX
2712 free (p);
2713 #endif
2716 return true;
2719 /* Set flags and magic number of a coff file from architecture and machine
2720 type. Result is TRUE if we can represent the arch&type, FALSE if not. */
2722 static bool
2723 coff_set_flags (bfd * abfd,
2724 unsigned int *magicp ATTRIBUTE_UNUSED,
2725 unsigned short *flagsp ATTRIBUTE_UNUSED)
2727 switch (bfd_get_arch (abfd))
2729 #ifdef Z80MAGIC
2730 case bfd_arch_z80:
2731 *magicp = Z80MAGIC;
2732 switch (bfd_get_mach (abfd))
2734 case bfd_mach_z80strict:
2735 case bfd_mach_z80:
2736 case bfd_mach_z80n:
2737 case bfd_mach_z80full:
2738 case bfd_mach_r800:
2739 case bfd_mach_gbz80:
2740 case bfd_mach_z180:
2741 case bfd_mach_ez80_z80:
2742 case bfd_mach_ez80_adl:
2743 *flagsp = bfd_get_mach (abfd) << 12;
2744 break;
2745 default:
2746 return false;
2748 return true;
2749 #endif
2751 #ifdef Z8KMAGIC
2752 case bfd_arch_z8k:
2753 *magicp = Z8KMAGIC;
2755 switch (bfd_get_mach (abfd))
2757 case bfd_mach_z8001: *flagsp = F_Z8001; break;
2758 case bfd_mach_z8002: *flagsp = F_Z8002; break;
2759 default: return false;
2761 return true;
2762 #endif
2764 #ifdef TIC30MAGIC
2765 case bfd_arch_tic30:
2766 *magicp = TIC30MAGIC;
2767 return true;
2768 #endif
2770 #ifdef TICOFF_DEFAULT_MAGIC
2771 case TICOFF_TARGET_ARCH:
2772 /* If there's no indication of which version we want, use the default. */
2773 if (!abfd->xvec )
2774 *magicp = TICOFF_DEFAULT_MAGIC;
2775 else
2777 /* We may want to output in a different COFF version. */
2778 switch (abfd->xvec->name[4])
2780 case '0':
2781 *magicp = TICOFF0MAGIC;
2782 break;
2783 case '1':
2784 *magicp = TICOFF1MAGIC;
2785 break;
2786 case '2':
2787 *magicp = TICOFF2MAGIC;
2788 break;
2789 default:
2790 return false;
2793 TICOFF_TARGET_MACHINE_SET (flagsp, bfd_get_mach (abfd));
2794 return true;
2795 #endif
2797 #ifdef AARCH64MAGIC
2798 case bfd_arch_aarch64:
2799 * magicp = AARCH64MAGIC;
2800 return true;
2801 #endif
2803 #ifdef LOONGARCH64MAGIC
2804 case bfd_arch_loongarch:
2805 * magicp = LOONGARCH64MAGIC;
2806 return true;
2807 #endif
2809 #ifdef ARMMAGIC
2810 case bfd_arch_arm:
2811 #ifdef ARM_WINCE
2812 * magicp = ARMPEMAGIC;
2813 #else
2814 * magicp = ARMMAGIC;
2815 #endif
2816 * flagsp = 0;
2817 if (APCS_SET (abfd))
2819 if (APCS_26_FLAG (abfd))
2820 * flagsp |= F_APCS26;
2822 if (APCS_FLOAT_FLAG (abfd))
2823 * flagsp |= F_APCS_FLOAT;
2825 if (PIC_FLAG (abfd))
2826 * flagsp |= F_PIC;
2828 if (INTERWORK_SET (abfd) && INTERWORK_FLAG (abfd))
2829 * flagsp |= F_INTERWORK;
2830 switch (bfd_get_mach (abfd))
2832 case bfd_mach_arm_2: * flagsp |= F_ARM_2; break;
2833 case bfd_mach_arm_2a: * flagsp |= F_ARM_2a; break;
2834 case bfd_mach_arm_3: * flagsp |= F_ARM_3; break;
2835 case bfd_mach_arm_3M: * flagsp |= F_ARM_3M; break;
2836 case bfd_mach_arm_4: * flagsp |= F_ARM_4; break;
2837 case bfd_mach_arm_4T: * flagsp |= F_ARM_4T; break;
2838 case bfd_mach_arm_5: * flagsp |= F_ARM_5; break;
2839 /* FIXME: we do not have F_ARM vaues greater than F_ARM_5.
2840 See also the comment in coff_set_arch_mach_hook(). */
2841 case bfd_mach_arm_5T: * flagsp |= F_ARM_5; break;
2842 case bfd_mach_arm_5TE: * flagsp |= F_ARM_5; break;
2843 case bfd_mach_arm_XScale: * flagsp |= F_ARM_5; break;
2845 return true;
2846 #endif
2848 #if defined(I386MAGIC) || defined(AMD64MAGIC)
2849 case bfd_arch_i386:
2850 #if defined(I386MAGIC)
2851 *magicp = I386MAGIC;
2852 #endif
2853 #if defined LYNXOS
2854 /* Just overwrite the usual value if we're doing Lynx. */
2855 *magicp = LYNXCOFFMAGIC;
2856 #endif
2857 #if defined AMD64MAGIC
2858 *magicp = AMD64MAGIC;
2859 #endif
2860 return true;
2861 #endif
2863 #ifdef IA64MAGIC
2864 case bfd_arch_ia64:
2865 *magicp = IA64MAGIC;
2866 return true;
2867 #endif
2869 #ifdef SH_ARCH_MAGIC_BIG
2870 case bfd_arch_sh:
2871 #ifdef COFF_IMAGE_WITH_PE
2872 *magicp = SH_ARCH_MAGIC_WINCE;
2873 #else
2874 if (bfd_big_endian (abfd))
2875 *magicp = SH_ARCH_MAGIC_BIG;
2876 else
2877 *magicp = SH_ARCH_MAGIC_LITTLE;
2878 #endif
2879 return true;
2880 #endif
2882 #ifdef MIPS_ARCH_MAGIC_WINCE
2883 case bfd_arch_mips:
2884 *magicp = MIPS_ARCH_MAGIC_WINCE;
2885 return true;
2886 #endif
2888 #ifdef SPARCMAGIC
2889 case bfd_arch_sparc:
2890 *magicp = SPARCMAGIC;
2891 #ifdef LYNXOS
2892 /* Just overwrite the usual value if we're doing Lynx. */
2893 *magicp = LYNXCOFFMAGIC;
2894 #endif
2895 return true;
2896 #endif
2898 #ifdef RS6000COFF_C
2899 case bfd_arch_rs6000:
2900 case bfd_arch_powerpc:
2901 BFD_ASSERT (bfd_get_flavour (abfd) == bfd_target_xcoff_flavour);
2902 *magicp = bfd_xcoff_magic_number (abfd);
2903 return true;
2904 #endif
2906 #ifdef MCOREMAGIC
2907 case bfd_arch_mcore:
2908 * magicp = MCOREMAGIC;
2909 return true;
2910 #endif
2912 default: /* Unknown architecture. */
2913 break;
2916 return false;
2919 static bool
2920 coff_set_arch_mach (bfd * abfd,
2921 enum bfd_architecture arch,
2922 unsigned long machine)
2924 unsigned dummy1;
2925 unsigned short dummy2;
2927 if (! bfd_default_set_arch_mach (abfd, arch, machine))
2928 return false;
2930 if (arch != bfd_arch_unknown
2931 && ! coff_set_flags (abfd, &dummy1, &dummy2))
2932 return false; /* We can't represent this type. */
2934 return true; /* We're easy... */
2937 #ifdef COFF_IMAGE_WITH_PE
2939 /* This is used to sort sections by VMA, as required by PE image
2940 files. */
2942 static int
2943 sort_by_secaddr (const void * arg1, const void * arg2)
2945 const asection *a = *(const asection **) arg1;
2946 const asection *b = *(const asection **) arg2;
2948 if (a->vma < b->vma)
2949 return -1;
2950 else if (a->vma > b->vma)
2951 return 1;
2953 return 0;
2956 #endif /* COFF_IMAGE_WITH_PE */
2958 /* Calculate the file position for each section. */
2960 #define ALIGN_SECTIONS_IN_FILE
2961 #ifdef TICOFF
2962 #undef ALIGN_SECTIONS_IN_FILE
2963 #endif
2965 static bool
2966 coff_compute_section_file_positions (bfd * abfd)
2968 asection *current;
2969 file_ptr sofar = bfd_coff_filhsz (abfd);
2970 bool align_adjust;
2971 unsigned int target_index;
2972 #ifdef ALIGN_SECTIONS_IN_FILE
2973 asection *previous = NULL;
2974 file_ptr old_sofar;
2975 #endif
2977 #ifdef COFF_IMAGE_WITH_PE
2978 unsigned int page_size;
2980 if (coff_data (abfd)->link_info
2981 || (pe_data (abfd) && pe_data (abfd)->pe_opthdr.FileAlignment))
2983 page_size = pe_data (abfd)->pe_opthdr.FileAlignment;
2985 /* If no file alignment has been set, default to one.
2986 This repairs 'ld -r' for arm-wince-pe target. */
2987 if (page_size == 0)
2988 page_size = 1;
2990 else
2991 page_size = PE_DEF_FILE_ALIGNMENT;
2992 #else
2993 #ifdef COFF_PAGE_SIZE
2994 unsigned int page_size = COFF_PAGE_SIZE;
2995 #endif
2996 #endif
2998 #ifdef RS6000COFF_C
2999 /* On XCOFF, if we have symbols, set up the .debug section. */
3000 if (bfd_get_symcount (abfd) > 0)
3002 bfd_size_type sz;
3003 bfd_size_type i, symcount;
3004 asymbol **symp;
3006 sz = 0;
3007 symcount = bfd_get_symcount (abfd);
3008 for (symp = abfd->outsymbols, i = 0; i < symcount; symp++, i++)
3010 coff_symbol_type *cf;
3012 cf = coff_symbol_from (*symp);
3013 if (cf != NULL
3014 && cf->native != NULL
3015 && cf->native->is_sym
3016 && SYMNAME_IN_DEBUG (&cf->native->u.syment))
3018 size_t len;
3020 len = strlen (bfd_asymbol_name (*symp));
3021 if (len > SYMNMLEN || bfd_coff_force_symnames_in_strings (abfd))
3022 sz += len + 1 + bfd_coff_debug_string_prefix_length (abfd);
3025 if (sz > 0)
3027 asection *dsec;
3029 dsec = bfd_make_section_old_way (abfd, DOT_DEBUG);
3030 if (dsec == NULL)
3031 abort ();
3032 dsec->size = sz;
3033 dsec->flags |= SEC_HAS_CONTENTS;
3036 #endif
3038 if (bfd_get_start_address (abfd))
3039 /* A start address may have been added to the original file. In this
3040 case it will need an optional header to record it. */
3041 abfd->flags |= EXEC_P;
3043 if (abfd->flags & EXEC_P)
3044 sofar += bfd_coff_aoutsz (abfd);
3045 #ifdef RS6000COFF_C
3046 else if (xcoff_data (abfd)->full_aouthdr)
3047 sofar += bfd_coff_aoutsz (abfd);
3048 else
3049 sofar += SMALL_AOUTSZ;
3050 #endif
3052 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
3054 #ifdef RS6000COFF_C
3055 /* XCOFF handles overflows in the reloc and line number count fields
3056 by allocating a new section header to hold the correct counts. */
3057 for (current = abfd->sections; current != NULL; current = current->next)
3058 if (current->reloc_count >= 0xffff || current->lineno_count >= 0xffff)
3059 sofar += bfd_coff_scnhsz (abfd);
3060 #endif
3062 if (coff_data (abfd)->section_by_target_index)
3063 htab_empty (coff_data (abfd)->section_by_target_index);
3065 #ifdef COFF_IMAGE_WITH_PE
3067 /* PE requires the sections to be in memory order when listed in
3068 the section headers. It also does not like empty loadable
3069 sections. The sections apparently do not have to be in the
3070 right order in the image file itself, but we do need to get the
3071 target_index values right. */
3073 unsigned int count;
3074 asection **section_list;
3075 unsigned int i;
3076 bfd_size_type amt;
3078 #ifdef COFF_PAGE_SIZE
3079 /* Clear D_PAGED if section / file alignment aren't suitable for
3080 paging at COFF_PAGE_SIZE granularity. */
3081 if (pe_data (abfd)->pe_opthdr.SectionAlignment < COFF_PAGE_SIZE
3082 || page_size < COFF_PAGE_SIZE)
3083 abfd->flags &= ~D_PAGED;
3084 #endif
3086 count = 0;
3087 for (current = abfd->sections; current != NULL; current = current->next)
3088 ++count;
3090 /* We allocate an extra cell to simplify the final loop. */
3091 amt = sizeof (struct asection *) * (count + 1);
3092 section_list = (asection **) bfd_malloc (amt);
3093 if (section_list == NULL)
3094 return false;
3096 i = 0;
3097 for (current = abfd->sections; current != NULL; current = current->next)
3099 section_list[i] = current;
3100 ++i;
3102 section_list[i] = NULL;
3104 qsort (section_list, count, sizeof (asection *), sort_by_secaddr);
3106 /* Rethread the linked list into sorted order; at the same time,
3107 assign target_index values. */
3108 target_index = 1;
3109 abfd->sections = NULL;
3110 abfd->section_last = NULL;
3111 for (i = 0; i < count; i++)
3113 current = section_list[i];
3114 bfd_section_list_append (abfd, current);
3116 /* Later, if the section has zero size, we'll be throwing it
3117 away, so we don't want to number it now. Note that having
3118 a zero size and having real contents are different
3119 concepts: .bss has no contents, but (usually) non-zero
3120 size. */
3121 if (current->size == 0)
3123 /* Discard. However, it still might have (valid) symbols
3124 in it, so arbitrarily set it to section 1 (indexing is
3125 1-based here; usually .text). __end__ and other
3126 contents of .endsection really have this happen.
3127 FIXME: This seems somewhat dubious. */
3128 current->target_index = 1;
3130 else
3131 current->target_index = target_index++;
3134 free (section_list);
3136 #else /* ! COFF_IMAGE_WITH_PE */
3138 /* Set the target_index field. */
3139 target_index = 1;
3140 for (current = abfd->sections; current != NULL; current = current->next)
3141 current->target_index = target_index++;
3143 #endif /* ! COFF_IMAGE_WITH_PE */
3145 if (target_index >= bfd_coff_max_nscns (abfd))
3147 bfd_set_error (bfd_error_file_too_big);
3148 _bfd_error_handler
3149 /* xgettext:c-format */
3150 (_("%pB: too many sections (%d)"), abfd, target_index);
3151 return false;
3154 align_adjust = false;
3155 for (current = abfd->sections;
3156 current != NULL;
3157 current = current->next)
3159 #ifdef COFF_IMAGE_WITH_PE
3160 /* With PE we have to pad each section to be a multiple of its
3161 page size too, and remember both sizes. */
3162 if (coff_section_data (abfd, current) == NULL)
3164 size_t amt = sizeof (struct coff_section_tdata);
3166 current->used_by_bfd = bfd_zalloc (abfd, amt);
3167 if (current->used_by_bfd == NULL)
3168 return false;
3170 if (pei_section_data (abfd, current) == NULL)
3172 size_t amt = sizeof (struct pei_section_tdata);
3174 coff_section_data (abfd, current)->tdata = bfd_zalloc (abfd, amt);
3175 if (coff_section_data (abfd, current)->tdata == NULL)
3176 return false;
3178 if (pei_section_data (abfd, current)->virt_size == 0)
3179 pei_section_data (abfd, current)->virt_size = current->size;
3180 #endif
3182 /* Only deal with sections which have contents. */
3183 if (!(current->flags & SEC_HAS_CONTENTS))
3184 continue;
3186 current->rawsize = current->size;
3188 #ifdef COFF_IMAGE_WITH_PE
3189 /* Make sure we skip empty sections in a PE image. */
3190 if (current->size == 0)
3191 continue;
3192 #endif
3194 /* Align the sections in the file to the same boundary on
3195 which they are aligned in virtual memory. */
3196 #ifdef ALIGN_SECTIONS_IN_FILE
3197 if ((abfd->flags & EXEC_P) != 0)
3199 /* Make sure this section is aligned on the right boundary - by
3200 padding the previous section up if necessary. */
3201 old_sofar = sofar;
3203 #ifdef COFF_IMAGE_WITH_PE
3204 sofar = BFD_ALIGN (sofar, page_size);
3205 #else
3206 sofar = BFD_ALIGN (sofar, (bfd_vma) 1 << current->alignment_power);
3207 #endif
3209 #ifdef RS6000COFF_C
3210 /* Make sure the file offset and the vma of .text/.data are at the
3211 same page offset, so that the file can be mmap'ed without being
3212 relocated. Failing that, AIX is able to load and execute the
3213 program, but it will be silently relocated (possible as
3214 executables are PIE). But the relocation is slightly costly and
3215 complexify the use of addr2line or gdb. So better to avoid it,
3216 like does the native linker. Usually gnu ld makes sure that
3217 the vma of .text is the file offset so this issue shouldn't
3218 appear unless you are stripping such an executable.
3220 AIX loader checks the text section alignment of (vma - filepos),
3221 and the native linker doesn't try to align the text sections.
3222 For example:
3224 0 .text 000054cc 10000128 10000128 00000128 2**5
3225 CONTENTS, ALLOC, LOAD, CODE
3227 Don't perform the above tweak if the previous one is .tdata,
3228 as it will increase the memory allocated for every threads
3229 created and not just improve performances with gdb.
3232 if ((current->flags & SEC_LOAD) != 0
3233 && (!strcmp (current->name, _TEXT)
3234 || !strcmp (current->name, _DATA))
3235 && (previous == NULL || strcmp(previous->name, _TDATA)))
3237 bfd_vma align = 4096;
3238 bfd_vma sofar_off = sofar % align;
3239 bfd_vma vma_off = current->vma % align;
3241 if (vma_off > sofar_off)
3242 sofar += vma_off - sofar_off;
3243 else if (vma_off < sofar_off)
3244 sofar += align + vma_off - sofar_off;
3246 #endif
3247 if (previous != NULL
3248 && (previous->flags & SEC_LOAD) != 0)
3249 previous->size += sofar - old_sofar;
3252 #endif
3254 /* In demand paged files the low order bits of the file offset
3255 must match the low order bits of the virtual address. */
3256 #ifdef COFF_PAGE_SIZE
3257 if ((abfd->flags & D_PAGED) != 0
3258 && (current->flags & SEC_ALLOC) != 0)
3259 sofar += (current->vma - (bfd_vma) sofar) % page_size;
3260 #endif
3261 current->filepos = sofar;
3263 #ifdef COFF_IMAGE_WITH_PE
3264 /* Set the padded size. */
3265 current->size = (current->size + page_size - 1) & -page_size;
3266 #endif
3268 sofar += current->size;
3270 #ifdef ALIGN_SECTIONS_IN_FILE
3271 /* Make sure that this section is of the right size too. */
3272 if ((abfd->flags & EXEC_P) == 0)
3274 bfd_size_type old_size;
3276 old_size = current->size;
3277 current->size = BFD_ALIGN (current->size,
3278 (bfd_vma) 1 << current->alignment_power);
3279 align_adjust = current->size != old_size;
3280 sofar += current->size - old_size;
3282 else
3284 old_sofar = sofar;
3285 #ifdef COFF_IMAGE_WITH_PE
3286 sofar = BFD_ALIGN (sofar, page_size);
3287 #else
3288 sofar = BFD_ALIGN (sofar, (bfd_vma) 1 << current->alignment_power);
3289 #endif
3290 align_adjust = sofar != old_sofar;
3291 current->size += sofar - old_sofar;
3293 #endif
3295 #ifdef COFF_IMAGE_WITH_PE
3296 /* For PE we need to make sure we pad out to the aligned
3297 size, in case the caller only writes out data to the
3298 unaligned size. */
3299 if (pei_section_data (abfd, current)->virt_size < current->size)
3300 align_adjust = true;
3301 #endif
3303 #ifdef _LIB
3304 /* Force .lib sections to start at zero. The vma is then
3305 incremented in coff_set_section_contents. This is right for
3306 SVR3.2. */
3307 if (strcmp (current->name, _LIB) == 0)
3308 bfd_set_section_vma (current, 0);
3309 #endif
3311 #ifdef ALIGN_SECTIONS_IN_FILE
3312 previous = current;
3313 #endif
3316 /* It is now safe to write to the output file. If we needed an
3317 alignment adjustment for the last section, then make sure that
3318 there is a byte at offset sofar. If there are no symbols and no
3319 relocs, then nothing follows the last section. If we don't force
3320 the last byte out, then the file may appear to be truncated. */
3321 if (align_adjust)
3323 bfd_byte b;
3325 b = 0;
3326 if (bfd_seek (abfd, sofar - 1, SEEK_SET) != 0
3327 || bfd_write (&b, 1, abfd) != 1)
3328 return false;
3331 /* Make sure the relocations are aligned. We don't need to make
3332 sure that this byte exists, because it will only matter if there
3333 really are relocs. */
3334 sofar = BFD_ALIGN (sofar,
3335 (bfd_vma) 1 << COFF_DEFAULT_SECTION_ALIGNMENT_POWER);
3337 obj_relocbase (abfd) = sofar;
3338 abfd->output_has_begun = true;
3340 return true;
3343 #ifdef COFF_IMAGE_WITH_PE
3345 static bool
3346 coff_read_word (bfd *abfd, unsigned int *value, unsigned int *pelength)
3348 unsigned char b[2];
3349 int status;
3351 status = bfd_read (b, 2, abfd);
3352 if (status < 1)
3354 *value = 0;
3355 return false;
3358 if (status == 1)
3359 *value = (unsigned int) b[0];
3360 else
3361 *value = (unsigned int) (b[0] + (b[1] << 8));
3363 *pelength += status;
3365 return true;
3368 /* Read a two byte number from buffer B returning the result in VALUE.
3369 No more than BUF_SIZE bytes will be read.
3370 Returns true upobn success, false otherwise.
3371 If successful, increases the value stored in PELENGTH by the number
3372 of bytes read. */
3374 static bool
3375 coff_read_word_from_buffer (unsigned char * b,
3376 int buf_size,
3377 unsigned int * value,
3378 unsigned int * pelength)
3380 if (buf_size < 1)
3382 *value = 0;
3383 return false;
3386 if (buf_size == 1)
3388 *value = (unsigned int)b[0];
3389 *pelength += 1;
3391 else
3393 *value = (unsigned int)(b[0] + (b[1] << 8));
3394 *pelength += 2;
3397 return true;
3400 #define COFF_CHECKSUM_BUFFER_SIZE 0x800000
3402 static unsigned int
3403 coff_compute_checksum (bfd *abfd, unsigned int *pelength)
3405 file_ptr filepos;
3406 unsigned int value;
3407 unsigned int total;
3408 unsigned char *buf;
3409 int buf_size;
3411 total = 0;
3412 *pelength = 0;
3413 filepos = (file_ptr) 0;
3414 buf = (unsigned char *) bfd_malloc (COFF_CHECKSUM_BUFFER_SIZE);
3415 if (buf == NULL)
3416 return 0;
3417 buf_size = 0;
3421 unsigned char *cur_buf;
3422 int cur_buf_size;
3424 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
3425 return 0;
3427 buf_size = bfd_read (buf, COFF_CHECKSUM_BUFFER_SIZE, abfd);
3428 cur_buf_size = buf_size;
3429 cur_buf = buf;
3431 while (cur_buf_size > 0)
3433 coff_read_word_from_buffer (cur_buf, cur_buf_size, &value, pelength);
3434 cur_buf += 2;
3435 cur_buf_size -= 2;
3436 total += value;
3437 total = 0xffff & (total + (total >> 0x10));
3440 filepos += buf_size;
3442 while (buf_size > 0);
3444 free (buf);
3446 return (0xffff & (total + (total >> 0x10)));
3449 static bool
3450 coff_apply_checksum (bfd *abfd)
3452 unsigned int computed;
3453 unsigned int checksum = 0;
3454 unsigned int peheader;
3455 unsigned int pelength;
3457 if (bfd_seek (abfd, 0x3c, SEEK_SET) != 0)
3458 return false;
3460 if (!coff_read_word (abfd, &peheader, &pelength))
3461 return false;
3463 if (bfd_seek (abfd, peheader + 0x58, SEEK_SET) != 0)
3464 return false;
3466 checksum = 0;
3467 if (bfd_write (&checksum, 4, abfd) != 4)
3468 return false;
3470 if (bfd_seek (abfd, peheader, SEEK_SET) != 0)
3471 return false;
3473 computed = coff_compute_checksum (abfd, &pelength);
3475 checksum = computed + pelength;
3477 if (bfd_seek (abfd, peheader + 0x58, SEEK_SET) != 0)
3478 return false;
3480 return bfd_write (&checksum, 4, abfd) == 4;
3483 #endif /* COFF_IMAGE_WITH_PE */
3485 static bool
3486 coff_write_object_contents (bfd * abfd)
3488 asection *current;
3489 bool hasrelocs = false;
3490 bool haslinno = false;
3491 #ifdef COFF_IMAGE_WITH_PE
3492 bool hasdebug = false;
3493 #endif
3494 file_ptr scn_base;
3495 file_ptr reloc_base;
3496 file_ptr lineno_base;
3497 file_ptr sym_base;
3498 unsigned long reloc_size = 0, reloc_count = 0;
3499 unsigned long lnno_size = 0;
3500 bool long_section_names;
3501 asection *text_sec = NULL;
3502 asection *data_sec = NULL;
3503 asection *bss_sec = NULL;
3504 #ifdef RS6000COFF_C
3505 asection *tdata_sec = NULL;
3506 asection *tbss_sec = NULL;
3507 #endif
3508 struct internal_filehdr internal_f;
3509 struct internal_aouthdr internal_a;
3510 #ifdef COFF_LONG_SECTION_NAMES
3511 size_t string_size = STRING_SIZE_SIZE;
3512 #endif
3514 bfd_set_error (bfd_error_system_call);
3516 /* Make a pass through the symbol table to count line number entries and
3517 put them into the correct asections. */
3518 lnno_size = coff_count_linenumbers (abfd) * bfd_coff_linesz (abfd);
3520 if (! abfd->output_has_begun)
3522 if (! coff_compute_section_file_positions (abfd))
3523 return false;
3526 reloc_base = obj_relocbase (abfd);
3528 /* Work out the size of the reloc and linno areas. */
3530 for (current = abfd->sections; current != NULL; current =
3531 current->next)
3533 #ifdef COFF_WITH_EXTENDED_RELOC_COUNTER
3534 /* We store the actual reloc count in the first reloc's addr. */
3535 if ((obj_pe (abfd) || obj_go32 (abfd)) && current->reloc_count >= 0xffff)
3536 reloc_count ++;
3537 #endif
3538 reloc_count += current->reloc_count;
3541 reloc_size = reloc_count * bfd_coff_relsz (abfd);
3543 lineno_base = reloc_base + reloc_size;
3544 sym_base = lineno_base + lnno_size;
3546 /* Indicate in each section->line_filepos its actual file address. */
3547 for (current = abfd->sections; current != NULL; current =
3548 current->next)
3550 if (current->lineno_count)
3552 current->line_filepos = lineno_base;
3553 current->moving_line_filepos = lineno_base;
3554 lineno_base += current->lineno_count * bfd_coff_linesz (abfd);
3556 else
3557 current->line_filepos = 0;
3559 if (current->reloc_count)
3561 current->rel_filepos = reloc_base;
3562 reloc_base += current->reloc_count * bfd_coff_relsz (abfd);
3563 #ifdef COFF_WITH_EXTENDED_RELOC_COUNTER
3564 /* Extra reloc to hold real count. */
3565 if ((obj_pe (abfd) || obj_go32 (abfd)) && current->reloc_count >= 0xffff)
3566 reloc_base += bfd_coff_relsz (abfd);
3567 #endif
3569 else
3570 current->rel_filepos = 0;
3573 /* Write section headers to the file. */
3574 internal_f.f_nscns = 0;
3576 if ((abfd->flags & EXEC_P) != 0)
3577 scn_base = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
3578 else
3580 scn_base = bfd_coff_filhsz (abfd);
3581 #ifdef RS6000COFF_C
3582 #ifndef XCOFF64
3583 if (xcoff_data (abfd)->full_aouthdr)
3584 scn_base += bfd_coff_aoutsz (abfd);
3585 else
3586 scn_base += SMALL_AOUTSZ;
3587 #endif
3588 #endif
3591 if (bfd_seek (abfd, scn_base, SEEK_SET) != 0)
3592 return false;
3594 long_section_names = false;
3595 for (current = abfd->sections;
3596 current != NULL;
3597 current = current->next)
3599 struct internal_scnhdr section;
3600 #ifdef COFF_IMAGE_WITH_PE
3601 bool is_reloc_section = false;
3603 if (strcmp (current->name, DOT_RELOC) == 0)
3605 is_reloc_section = true;
3606 hasrelocs = true;
3607 pe_data (abfd)->has_reloc_section = 1;
3609 #endif
3611 internal_f.f_nscns++;
3613 strncpy (section.s_name, current->name, SCNNMLEN);
3615 #ifdef COFF_LONG_SECTION_NAMES
3616 /* Handle long section names as in PE. This must be compatible
3617 with the code in coff_write_symbols and _bfd_coff_final_link. */
3618 if (bfd_coff_long_section_names (abfd))
3620 size_t len;
3622 len = strlen (current->name);
3623 if (len > SCNNMLEN)
3626 /* An inherent limitation of the /nnnnnnn notation used to indicate
3627 the offset of the long name in the string table is that we
3628 cannot address entries beyone the ten million byte boundary. */
3629 if (string_size < 10000000)
3631 /* The s_name field is defined to be NUL-padded but need not
3632 be NUL-terminated. We use a temporary buffer so that we
3633 can still sprintf all eight chars without splatting a
3634 terminating NUL over the first byte of the following
3635 member (s_paddr). */
3636 /* PR 21096: The +20 is to stop a bogus warning from gcc7
3637 about a possible buffer overflow. */
3638 char s_name_buf[SCNNMLEN + 1 + 20];
3640 /* We do not need to use snprintf here as we have already
3641 verified that string_size is not too big, plus we have
3642 an overlarge buffer, just in case. */
3643 sprintf (s_name_buf, "/%lu", (unsigned long) string_size);
3644 /* Then strncpy takes care of any padding for us. */
3645 strncpy (section.s_name, s_name_buf, SCNNMLEN);
3647 else
3648 #ifdef COFF_WITH_PE
3650 /* PE use a base 64 encoding for long section names whose
3651 index is very large. But contrary to RFC 4648, there is
3652 no padding: 6 characters must be generated. */
3653 static const char base64[] =
3654 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
3655 "abcdefghijklmnopqrstuvwxyz"
3656 "0123456789+/";
3657 unsigned long off = string_size;
3658 unsigned i;
3660 section.s_name[0] = '/';
3661 section.s_name[1] = '/';
3662 for (i = SCNNMLEN - 1; i >= 2; i--)
3664 section.s_name[i] = base64[off & 0x3f];
3665 off >>= 6;
3668 #endif
3669 if (string_size > 0xffffffffUL - (len + 1)
3670 #ifndef COFF_WITH_PE
3671 || string_size >= 10000000
3672 #endif
3675 bfd_set_error (bfd_error_file_too_big);
3676 _bfd_error_handler
3677 /* xgettext:c-format */
3678 (_("%pB: section %pA: string table overflow at offset %ld"),
3679 abfd, current, (unsigned long) string_size);
3680 return false;
3683 string_size += len + 1;
3684 long_section_names = true;
3687 #endif
3689 #ifdef _LIB
3690 /* Always set s_vaddr of .lib to 0. This is right for SVR3.2
3691 Ian Taylor <ian@cygnus.com>. */
3692 if (strcmp (current->name, _LIB) == 0)
3693 section.s_vaddr = 0;
3694 else
3695 #endif
3696 section.s_vaddr = current->vma;
3697 section.s_paddr = current->lma;
3698 section.s_size = current->size;
3699 #ifdef coff_get_section_load_page
3700 section.s_page = coff_get_section_load_page (current);
3701 #else
3702 section.s_page = 0;
3703 #endif
3705 #ifdef COFF_WITH_PE
3706 section.s_paddr = 0;
3707 #endif
3708 #ifdef COFF_IMAGE_WITH_PE
3709 /* Reminder: s_paddr holds the virtual size of the section. */
3710 if (coff_section_data (abfd, current) != NULL
3711 && pei_section_data (abfd, current) != NULL)
3712 section.s_paddr = pei_section_data (abfd, current)->virt_size;
3713 else
3714 section.s_paddr = 0;
3715 #endif
3717 /* If this section has no size or is unloadable then the scnptr
3718 will be 0 too. */
3719 if (current->size == 0
3720 || (current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3721 section.s_scnptr = 0;
3722 else
3723 section.s_scnptr = current->filepos;
3725 section.s_relptr = current->rel_filepos;
3726 section.s_lnnoptr = current->line_filepos;
3727 section.s_nreloc = current->reloc_count;
3728 section.s_nlnno = current->lineno_count;
3729 #ifndef COFF_IMAGE_WITH_PE
3730 /* In PEI, relocs come in the .reloc section. */
3731 if (current->reloc_count != 0)
3732 hasrelocs = true;
3733 #endif
3734 if (current->lineno_count != 0)
3735 haslinno = true;
3736 #ifdef COFF_IMAGE_WITH_PE
3737 if ((current->flags & SEC_DEBUGGING) != 0
3738 && ! is_reloc_section)
3739 hasdebug = true;
3740 #endif
3742 #ifdef RS6000COFF_C
3743 #ifndef XCOFF64
3744 /* Indicate the use of an XCOFF overflow section header. */
3745 if (current->reloc_count >= 0xffff || current->lineno_count >= 0xffff)
3747 section.s_nreloc = 0xffff;
3748 section.s_nlnno = 0xffff;
3750 #endif
3751 #endif
3753 section.s_flags = sec_to_styp_flags (current->name, current->flags);
3755 if (!strcmp (current->name, _TEXT))
3756 text_sec = current;
3757 else if (!strcmp (current->name, _DATA))
3758 data_sec = current;
3759 else if (!strcmp (current->name, _BSS))
3760 bss_sec = current;
3761 #ifdef RS6000COFF_C
3762 else if (!strcmp (current->name, _TDATA))
3763 tdata_sec = current;
3764 else if (!strcmp (current->name, _TBSS))
3765 tbss_sec = current;
3766 #endif
3769 #ifdef COFF_ENCODE_ALIGNMENT
3770 if (COFF_ENCODE_ALIGNMENT (abfd, section, current->alignment_power)
3771 && (COFF_DECODE_ALIGNMENT (section.s_flags)
3772 != current->alignment_power))
3774 bool warn = (coff_data (abfd)->link_info
3775 && !bfd_link_relocatable (coff_data (abfd)->link_info));
3777 _bfd_error_handler
3778 /* xgettext:c-format */
3779 (_("%pB:%s section %s: alignment 2**%u not representable"),
3780 abfd, warn ? " warning:" : "", current->name,
3781 current->alignment_power);
3782 if (!warn)
3784 bfd_set_error (bfd_error_nonrepresentable_section);
3785 return false;
3788 #endif
3790 #ifdef COFF_IMAGE_WITH_PE
3791 /* Suppress output of the sections if they are null. ld
3792 includes the bss and data sections even if there is no size
3793 assigned to them. NT loader doesn't like it if these section
3794 headers are included if the sections themselves are not
3795 needed. See also coff_compute_section_file_positions. */
3796 if (section.s_size == 0)
3797 internal_f.f_nscns--;
3798 else
3799 #endif
3801 SCNHDR buff;
3802 bfd_size_type amt = bfd_coff_scnhsz (abfd);
3804 if (bfd_coff_swap_scnhdr_out (abfd, &section, &buff) == 0
3805 || bfd_write (& buff, amt, abfd) != amt)
3806 return false;
3809 #ifdef COFF_WITH_PE
3810 /* PE stores COMDAT section information in the symbol table. If
3811 this section is supposed to have some COMDAT info, track down
3812 the symbol in the symbol table and modify it. */
3813 if ((current->flags & SEC_LINK_ONCE) != 0)
3815 unsigned int i, count;
3816 asymbol **psym;
3817 coff_symbol_type *csym = NULL;
3818 asymbol **psymsec;
3820 psymsec = NULL;
3821 count = bfd_get_symcount (abfd);
3822 for (i = 0, psym = abfd->outsymbols; i < count; i++, psym++)
3824 if ((*psym)->section != current)
3825 continue;
3827 /* Remember the location of the first symbol in this
3828 section. */
3829 if (psymsec == NULL)
3830 psymsec = psym;
3832 /* See if this is the section symbol. */
3833 if (strcmp ((*psym)->name, current->name) == 0)
3835 csym = coff_symbol_from (*psym);
3836 if (csym == NULL
3837 || csym->native == NULL
3838 || ! csym->native->is_sym
3839 || csym->native->u.syment.n_numaux < 1
3840 || csym->native->u.syment.n_sclass != C_STAT
3841 || csym->native->u.syment.n_type != T_NULL)
3842 continue;
3844 /* Here *PSYM is the section symbol for CURRENT. */
3846 break;
3850 /* Did we find it?
3851 Note that we might not if we're converting the file from
3852 some other object file format. */
3853 if (i < count)
3855 combined_entry_type *aux;
3857 /* We don't touch the x_checksum field. The
3858 x_associated field is not currently supported. */
3860 aux = csym->native + 1;
3861 BFD_ASSERT (! aux->is_sym);
3862 switch (current->flags & SEC_LINK_DUPLICATES)
3864 case SEC_LINK_DUPLICATES_DISCARD:
3865 aux->u.auxent.x_scn.x_comdat = IMAGE_COMDAT_SELECT_ANY;
3866 break;
3868 case SEC_LINK_DUPLICATES_ONE_ONLY:
3869 aux->u.auxent.x_scn.x_comdat =
3870 IMAGE_COMDAT_SELECT_NODUPLICATES;
3871 break;
3873 case SEC_LINK_DUPLICATES_SAME_SIZE:
3874 aux->u.auxent.x_scn.x_comdat =
3875 IMAGE_COMDAT_SELECT_SAME_SIZE;
3876 break;
3878 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
3879 aux->u.auxent.x_scn.x_comdat =
3880 IMAGE_COMDAT_SELECT_EXACT_MATCH;
3881 break;
3884 /* The COMDAT symbol must be the first symbol from this
3885 section in the symbol table. In order to make this
3886 work, we move the COMDAT symbol before the first
3887 symbol we found in the search above. It's OK to
3888 rearrange the symbol table at this point, because
3889 coff_renumber_symbols is going to rearrange it
3890 further and fix up all the aux entries. */
3891 if (psym != psymsec)
3893 asymbol *hold;
3894 asymbol **pcopy;
3896 hold = *psym;
3897 for (pcopy = psym; pcopy > psymsec; pcopy--)
3898 pcopy[0] = pcopy[-1];
3899 *psymsec = hold;
3903 #endif /* COFF_WITH_PE */
3906 #ifdef RS6000COFF_C
3907 #ifndef XCOFF64
3908 /* XCOFF handles overflows in the reloc and line number count fields
3909 by creating a new section header to hold the correct values. */
3910 for (current = abfd->sections; current != NULL; current = current->next)
3912 if (current->reloc_count >= 0xffff || current->lineno_count >= 0xffff)
3914 struct internal_scnhdr scnhdr;
3915 SCNHDR buff;
3916 bfd_size_type amt;
3918 internal_f.f_nscns++;
3919 memcpy (scnhdr.s_name, ".ovrflo", 8);
3920 scnhdr.s_paddr = current->reloc_count;
3921 scnhdr.s_vaddr = current->lineno_count;
3922 scnhdr.s_size = 0;
3923 scnhdr.s_scnptr = 0;
3924 scnhdr.s_relptr = current->rel_filepos;
3925 scnhdr.s_lnnoptr = current->line_filepos;
3926 scnhdr.s_nreloc = current->target_index;
3927 scnhdr.s_nlnno = current->target_index;
3928 scnhdr.s_flags = STYP_OVRFLO;
3929 amt = bfd_coff_scnhsz (abfd);
3930 if (bfd_coff_swap_scnhdr_out (abfd, &scnhdr, &buff) == 0
3931 || bfd_write (& buff, amt, abfd) != amt)
3932 return false;
3935 #endif
3936 #endif
3938 #if defined (COFF_GO32_EXE) || defined (COFF_GO32)
3939 /* Pad section headers. */
3940 if ((abfd->flags & EXEC_P) != 0)
3942 asection *s = abfd->sections;
3943 while (s != NULL && s->filepos == 0)
3944 s = s->next;
3945 if (s != NULL)
3947 file_ptr cur_ptr
3948 = scn_base + abfd->section_count * bfd_coff_scnhsz (abfd);
3949 file_ptr fill_size = s->filepos - cur_ptr;
3950 if (fill_size > 0)
3952 bfd_byte *b = bfd_zmalloc (fill_size);
3953 if (!b)
3954 return false;
3955 if (bfd_write (b, fill_size, abfd) != (ufile_ptr) fill_size)
3957 free (b);
3958 return false;
3960 free (b);
3964 #endif
3966 /* OK, now set up the filehdr... */
3968 /* Don't include the internal abs section in the section count */
3970 /* We will NOT put a fucking timestamp in the header here. Every time you
3971 put it back, I will come in and take it out again. I'm sorry. This
3972 field does not belong here. We fill it with a 0 so it compares the
3973 same but is not a reasonable time. -- gnu@cygnus.com */
3974 internal_f.f_timdat = 0;
3975 internal_f.f_flags = 0;
3977 if (abfd->flags & EXEC_P)
3978 internal_f.f_opthdr = bfd_coff_aoutsz (abfd);
3979 else
3981 internal_f.f_opthdr = 0;
3982 #ifdef RS6000COFF_C
3983 #ifndef XCOFF64
3984 if (xcoff_data (abfd)->full_aouthdr)
3985 internal_f.f_opthdr = bfd_coff_aoutsz (abfd);
3986 else
3987 internal_f.f_opthdr = SMALL_AOUTSZ;
3988 #endif
3989 #endif
3992 if (!hasrelocs)
3993 internal_f.f_flags |= F_RELFLG;
3994 if (!haslinno)
3995 internal_f.f_flags |= F_LNNO;
3996 if (abfd->flags & EXEC_P)
3997 internal_f.f_flags |= F_EXEC;
3998 #ifdef COFF_IMAGE_WITH_PE
3999 if (! hasdebug)
4000 internal_f.f_flags |= IMAGE_FILE_DEBUG_STRIPPED;
4001 if (pe_data (abfd)->real_flags & IMAGE_FILE_LARGE_ADDRESS_AWARE)
4002 internal_f.f_flags |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
4003 #endif
4005 #if !defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64) && !defined(COFF_WITH_peLoongArch64)
4006 #ifdef COFF_WITH_PE
4007 internal_f.f_flags |= IMAGE_FILE_32BIT_MACHINE;
4008 #else
4009 if (bfd_little_endian (abfd))
4010 internal_f.f_flags |= F_AR32WR;
4011 else
4012 internal_f.f_flags |= F_AR32W;
4013 #endif
4014 #endif
4016 #ifdef TI_TARGET_ID
4017 /* Target id is used in TI COFF v1 and later; COFF0 won't use this field,
4018 but it doesn't hurt to set it internally. */
4019 internal_f.f_target_id = TI_TARGET_ID;
4020 #endif
4022 /* FIXME, should do something about the other byte orders and
4023 architectures. */
4025 #ifdef RS6000COFF_C
4026 if ((abfd->flags & DYNAMIC) != 0)
4027 internal_f.f_flags |= F_SHROBJ;
4028 if (bfd_get_section_by_name (abfd, _LOADER) != NULL)
4029 internal_f.f_flags |= F_DYNLOAD;
4030 #endif
4032 memset (&internal_a, 0, sizeof internal_a);
4034 /* Set up architecture-dependent stuff. */
4036 unsigned int magic = 0;
4037 unsigned short flags = 0;
4039 coff_set_flags (abfd, &magic, &flags);
4040 internal_f.f_magic = magic;
4041 internal_f.f_flags |= flags;
4042 /* ...and the "opt"hdr... */
4044 #ifdef TICOFF_AOUT_MAGIC
4045 internal_a.magic = TICOFF_AOUT_MAGIC;
4046 #define __A_MAGIC_SET__
4047 #endif
4049 #if defined(ARM)
4050 #define __A_MAGIC_SET__
4051 internal_a.magic = ZMAGIC;
4052 #endif
4054 #if defined(AARCH64)
4055 #define __A_MAGIC_SET__
4056 internal_a.magic = ZMAGIC;
4057 #endif
4059 #if defined(LOONGARCH64)
4060 #define __A_MAGIC_SET__
4061 internal_a.magic = ZMAGIC;
4062 #endif
4064 #if defined MCORE_PE
4065 #define __A_MAGIC_SET__
4066 internal_a.magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
4067 #endif
4069 #if defined(I386)
4070 #define __A_MAGIC_SET__
4071 #if defined LYNXOS
4072 internal_a.magic = LYNXCOFFMAGIC;
4073 #elif defined AMD64
4074 internal_a.magic = IMAGE_NT_OPTIONAL_HDR64_MAGIC;
4075 #else
4076 internal_a.magic = ZMAGIC;
4077 #endif
4078 #endif /* I386 */
4080 #if defined(IA64)
4081 #define __A_MAGIC_SET__
4082 internal_a.magic = PE32PMAGIC;
4083 #endif /* IA64 */
4085 #if defined(SPARC)
4086 #define __A_MAGIC_SET__
4087 #if defined(LYNXOS)
4088 internal_a.magic = LYNXCOFFMAGIC;
4089 #endif /* LYNXOS */
4090 #endif /* SPARC */
4092 #ifdef RS6000COFF_C
4093 #define __A_MAGIC_SET__
4094 internal_a.magic = (abfd->flags & D_PAGED) ? RS6K_AOUTHDR_ZMAGIC :
4095 (abfd->flags & WP_TEXT) ? RS6K_AOUTHDR_NMAGIC :
4096 RS6K_AOUTHDR_OMAGIC;
4097 #endif
4099 #if defined(SH) && defined(COFF_WITH_PE)
4100 #define __A_MAGIC_SET__
4101 internal_a.magic = SH_PE_MAGIC;
4102 #endif
4104 #if defined(MIPS) && defined(COFF_WITH_PE)
4105 #define __A_MAGIC_SET__
4106 internal_a.magic = MIPS_PE_MAGIC;
4107 #endif
4109 #ifndef __A_MAGIC_SET__
4110 #include "Your aouthdr magic number is not being set!"
4111 #else
4112 #undef __A_MAGIC_SET__
4113 #endif
4116 #ifdef RS6000COFF_C
4117 /* XCOFF 32bit needs this to have new behaviour for n_type field. */
4118 internal_a.vstamp = 2;
4119 #else
4120 /* FIXME: Does anybody ever set this to another value? */
4121 internal_a.vstamp = 0;
4122 #endif
4124 /* Now should write relocs, strings, syms. */
4125 obj_sym_filepos (abfd) = sym_base;
4127 if (bfd_get_symcount (abfd) != 0)
4129 int firstundef;
4131 if (!coff_renumber_symbols (abfd, &firstundef))
4132 return false;
4133 coff_mangle_symbols (abfd);
4134 if (! coff_write_symbols (abfd))
4135 return false;
4136 if (! coff_write_linenumbers (abfd))
4137 return false;
4138 if (! coff_write_relocs (abfd, firstundef))
4139 return false;
4141 #ifdef COFF_LONG_SECTION_NAMES
4142 else if (long_section_names && ! obj_coff_strings_written (abfd))
4144 /* If we have long section names we have to write out the string
4145 table even if there are no symbols. */
4146 if (! coff_write_symbols (abfd))
4147 return false;
4149 #endif
4150 /* If bfd_get_symcount (abfd) != 0, then we are not using the COFF
4151 backend linker, and obj_raw_syment_count is not valid until after
4152 coff_write_symbols is called. */
4153 if (obj_raw_syment_count (abfd) != 0)
4155 internal_f.f_symptr = sym_base;
4156 #ifdef RS6000COFF_C
4157 /* AIX appears to require that F_RELFLG not be set if there are
4158 local symbols but no relocations. */
4159 internal_f.f_flags &=~ F_RELFLG;
4160 #endif
4162 else
4164 if (long_section_names)
4165 internal_f.f_symptr = sym_base;
4166 else
4167 internal_f.f_symptr = 0;
4168 internal_f.f_flags |= F_LSYMS;
4171 if (text_sec)
4173 internal_a.tsize = text_sec->size;
4174 internal_a.text_start = internal_a.tsize ? text_sec->vma : 0;
4176 if (data_sec)
4178 internal_a.dsize = data_sec->size;
4179 internal_a.data_start = internal_a.dsize ? data_sec->vma : 0;
4181 if (bss_sec)
4183 internal_a.bsize = bss_sec->size;
4184 if (internal_a.bsize && bss_sec->vma < internal_a.data_start)
4185 internal_a.data_start = bss_sec->vma;
4188 internal_a.entry = bfd_get_start_address (abfd);
4189 internal_f.f_nsyms = obj_raw_syment_count (abfd);
4191 #ifdef RS6000COFF_C
4192 if (xcoff_data (abfd)->full_aouthdr)
4194 bfd_vma toc;
4195 asection *loader_sec;
4197 internal_a.vstamp = 2;
4199 internal_a.o_snentry = xcoff_data (abfd)->snentry;
4200 if (internal_a.o_snentry == 0)
4201 internal_a.entry = (bfd_vma) -1;
4203 if (text_sec != NULL)
4205 internal_a.o_sntext = text_sec->target_index;
4206 internal_a.o_algntext = bfd_section_alignment (text_sec);
4208 else
4210 internal_a.o_sntext = 0;
4211 internal_a.o_algntext = 0;
4213 if (data_sec != NULL)
4215 internal_a.o_sndata = data_sec->target_index;
4216 internal_a.o_algndata = bfd_section_alignment (data_sec);
4218 else
4220 internal_a.o_sndata = 0;
4221 internal_a.o_algndata = 0;
4223 loader_sec = bfd_get_section_by_name (abfd, ".loader");
4224 if (loader_sec != NULL)
4225 internal_a.o_snloader = loader_sec->target_index;
4226 else
4227 internal_a.o_snloader = 0;
4228 if (bss_sec != NULL)
4229 internal_a.o_snbss = bss_sec->target_index;
4230 else
4231 internal_a.o_snbss = 0;
4233 if (tdata_sec != NULL)
4235 internal_a.o_sntdata = tdata_sec->target_index;
4236 /* TODO: o_flags should be set to RS6K_AOUTHDR_TLS_LE
4237 if there is at least one R_TLS_LE relocations. */
4238 internal_a.o_flags = 0;
4239 #ifdef XCOFF64
4240 internal_a.o_x64flags = 0;
4241 #endif
4243 else
4245 internal_a.o_sntdata = 0;
4246 internal_a.o_flags = 0;
4247 #ifdef XCOFF64
4248 internal_a.o_x64flags = 0;
4249 #endif
4251 if (tbss_sec != NULL)
4252 internal_a.o_sntbss = tbss_sec->target_index;
4253 else
4254 internal_a.o_sntbss = 0;
4256 toc = xcoff_data (abfd)->toc;
4257 internal_a.o_toc = toc;
4258 internal_a.o_sntoc = xcoff_data (abfd)->sntoc;
4260 internal_a.o_modtype = xcoff_data (abfd)->modtype;
4261 if (xcoff_data (abfd)->cputype != -1)
4262 internal_a.o_cputype = xcoff_data (abfd)->cputype;
4263 else
4265 switch (bfd_get_arch (abfd))
4267 case bfd_arch_rs6000:
4268 internal_a.o_cputype = 4;
4269 break;
4270 case bfd_arch_powerpc:
4271 if (bfd_get_mach (abfd) == bfd_mach_ppc)
4272 internal_a.o_cputype = 3;
4273 else if (bfd_get_mach (abfd) == bfd_mach_ppc_620)
4274 internal_a.o_cputype = 2;
4275 else
4276 internal_a.o_cputype = 1;
4277 break;
4278 default:
4279 abort ();
4282 internal_a.o_maxstack = xcoff_data (abfd)->maxstack;
4283 internal_a.o_maxdata = xcoff_data (abfd)->maxdata;
4285 #endif
4287 #ifdef COFF_WITH_PE
4289 /* After object contents are finalized so we can compute a reasonable hash,
4290 but before header is written so we can update it to point to debug directory. */
4291 struct pe_tdata *pe = pe_data (abfd);
4293 if (pe->build_id.after_write_object_contents != NULL)
4294 (*pe->build_id.after_write_object_contents) (abfd);
4296 #endif
4298 /* Now write header. */
4299 if (bfd_seek (abfd, 0, SEEK_SET) != 0)
4300 return false;
4303 char * buff;
4304 bfd_size_type amount = bfd_coff_filhsz (abfd);
4306 buff = (char *) bfd_malloc (amount);
4307 if (buff == NULL)
4308 return false;
4310 bfd_coff_swap_filehdr_out (abfd, & internal_f, buff);
4311 amount = bfd_write (buff, amount, abfd);
4313 free (buff);
4315 if (amount != bfd_coff_filhsz (abfd))
4316 return false;
4319 if (abfd->flags & EXEC_P)
4321 /* Note that peicode.h fills in a PEAOUTHDR, not an AOUTHDR.
4322 include/coff/pe.h sets AOUTSZ == sizeof (PEAOUTHDR)). */
4323 char * buff;
4324 bfd_size_type amount = bfd_coff_aoutsz (abfd);
4326 buff = (char *) bfd_malloc (amount);
4327 if (buff == NULL)
4328 return false;
4330 coff_swap_aouthdr_out (abfd, & internal_a, buff);
4331 amount = bfd_write (buff, amount, abfd);
4333 free (buff);
4335 if (amount != bfd_coff_aoutsz (abfd))
4336 return false;
4338 #ifdef COFF_IMAGE_WITH_PE
4339 if (! coff_apply_checksum (abfd))
4340 return false;
4341 #endif
4343 #ifdef RS6000COFF_C
4344 #ifndef XCOFF64
4345 else
4347 AOUTHDR buff;
4348 size_t size;
4350 /* XCOFF32 seems to always write at least a small a.out header. */
4351 coff_swap_aouthdr_out (abfd, & internal_a, & buff);
4352 if (xcoff_data (abfd)->full_aouthdr)
4353 size = bfd_coff_aoutsz (abfd);
4354 else
4355 size = SMALL_AOUTSZ;
4356 if (bfd_write (&buff, size, abfd) != size)
4357 return false;
4359 #endif
4360 #endif
4362 return true;
4365 static bool
4366 coff_set_section_contents (bfd * abfd,
4367 sec_ptr section,
4368 const void * location,
4369 file_ptr offset,
4370 bfd_size_type count)
4372 if (! abfd->output_has_begun) /* Set by bfd.c handler. */
4374 if (! coff_compute_section_file_positions (abfd))
4375 return false;
4378 #if defined(_LIB) && !defined(TARG_AUX)
4379 /* The physical address field of a .lib section is used to hold the
4380 number of shared libraries in the section. This code counts the
4381 number of sections being written, and increments the lma field
4382 with the number.
4384 I have found no documentation on the contents of this section.
4385 Experimentation indicates that the section contains zero or more
4386 records, each of which has the following structure:
4388 - a (four byte) word holding the length of this record, in words,
4389 - a word that always seems to be set to "2",
4390 - the path to a shared library, null-terminated and then padded
4391 to a whole word boundary.
4393 bfd_assert calls have been added to alert if an attempt is made
4394 to write a section which doesn't follow these assumptions. The
4395 code has been tested on ISC 4.1 by me, and on SCO by Robert Lipe
4396 <robertl@arnet.com> (Thanks!).
4398 Gvran Uddeborg <gvran@uddeborg.pp.se>. */
4399 if (strcmp (section->name, _LIB) == 0)
4401 bfd_byte *rec, *recend;
4403 rec = (bfd_byte *) location;
4404 recend = rec + count;
4405 while (recend - rec >= 4)
4407 size_t len = bfd_get_32 (abfd, rec);
4408 if (len == 0 || len > (size_t) (recend - rec) / 4)
4409 break;
4410 rec += len * 4;
4411 ++section->lma;
4414 BFD_ASSERT (rec == recend);
4416 #endif
4418 /* Don't write out bss sections - one way to do this is to
4419 see if the filepos has not been set. */
4420 if (section->filepos == 0)
4421 return true;
4423 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0)
4424 return false;
4426 if (count == 0)
4427 return true;
4429 return bfd_write (location, count, abfd) == count;
4432 static void *
4433 buy_and_read (bfd *abfd, file_ptr where,
4434 bfd_size_type nmemb, bfd_size_type size)
4436 size_t amt;
4438 if (_bfd_mul_overflow (nmemb, size, &amt))
4440 bfd_set_error (bfd_error_file_too_big);
4441 return NULL;
4443 if (bfd_seek (abfd, where, SEEK_SET) != 0)
4444 return NULL;
4445 return _bfd_malloc_and_read (abfd, amt, amt);
4449 SUBSUBSECTION
4450 Reading linenumbers
4452 Creating the linenumber table is done by reading in the entire
4453 coff linenumber table, and creating another table for internal use.
4455 A coff linenumber table is structured so that each function
4456 is marked as having a line number of 0. Each line within the
4457 function is an offset from the first line in the function. The
4458 base of the line number information for the table is stored in
4459 the symbol associated with the function.
4461 Note: The PE format uses line number 0 for a flag indicating a
4462 new source file.
4464 The information is copied from the external to the internal
4465 table, and each symbol which marks a function is marked by
4466 pointing its...
4468 How does this work ?
4471 static int
4472 coff_sort_func_alent (const void * arg1, const void * arg2)
4474 const alent *al1 = *(const alent **) arg1;
4475 const alent *al2 = *(const alent **) arg2;
4476 const coff_symbol_type *s1 = (const coff_symbol_type *) (al1->u.sym);
4477 const coff_symbol_type *s2 = (const coff_symbol_type *) (al2->u.sym);
4479 if (s1 == NULL || s2 == NULL)
4480 return 0;
4481 if (s1->symbol.value < s2->symbol.value)
4482 return -1;
4483 else if (s1->symbol.value > s2->symbol.value)
4484 return 1;
4486 return 0;
4489 static bool
4490 coff_slurp_line_table (bfd *abfd, asection *asect)
4492 LINENO *native_lineno;
4493 alent *lineno_cache;
4494 unsigned int counter;
4495 alent *cache_ptr;
4496 bfd_vma prev_offset = 0;
4497 bool ordered = true;
4498 unsigned int nbr_func;
4499 LINENO *src;
4500 bool have_func;
4501 bool ret = true;
4502 size_t amt;
4504 if (asect->lineno_count == 0)
4505 return true;
4507 BFD_ASSERT (asect->lineno == NULL);
4509 native_lineno = (LINENO *) buy_and_read (abfd, asect->line_filepos,
4510 asect->lineno_count,
4511 bfd_coff_linesz (abfd));
4512 if (native_lineno == NULL)
4514 _bfd_error_handler
4515 (_("%pB: warning: line number table read failed"), abfd);
4516 return false;
4519 if (_bfd_mul_overflow (asect->lineno_count + 1, sizeof (alent), &amt))
4521 bfd_set_error (bfd_error_file_too_big);
4522 free (native_lineno);
4523 return false;
4525 lineno_cache = (alent *) bfd_alloc (abfd, amt);
4526 if (lineno_cache == NULL)
4528 free (native_lineno);
4529 return false;
4532 cache_ptr = lineno_cache;
4533 asect->lineno = lineno_cache;
4534 src = native_lineno;
4535 nbr_func = 0;
4536 have_func = false;
4538 for (counter = 0; counter < asect->lineno_count; counter++, src++)
4540 struct internal_lineno dst;
4542 bfd_coff_swap_lineno_in (abfd, src, &dst);
4543 cache_ptr->line_number = dst.l_lnno;
4544 /* Appease memory checkers that get all excited about
4545 uninitialised memory when copying alents if u.offset is
4546 larger than u.sym. (64-bit BFD on 32-bit host.) */
4547 memset (&cache_ptr->u, 0, sizeof (cache_ptr->u));
4549 if (cache_ptr->line_number == 0)
4551 combined_entry_type * ent;
4552 unsigned long symndx;
4553 coff_symbol_type *sym;
4555 have_func = false;
4556 symndx = dst.l_addr.l_symndx;
4557 if (symndx >= obj_raw_syment_count (abfd))
4559 _bfd_error_handler
4560 /* xgettext:c-format */
4561 (_("%pB: warning: illegal symbol index 0x%lx in line number entry %d"),
4562 abfd, symndx, counter);
4563 cache_ptr->line_number = -1;
4564 ret = false;
4565 continue;
4568 ent = obj_raw_syments (abfd) + symndx;
4569 /* FIXME: We should not be casting between ints and
4570 pointers like this. */
4571 if (! ent->is_sym)
4573 _bfd_error_handler
4574 /* xgettext:c-format */
4575 (_("%pB: warning: illegal symbol index 0x%lx in line number entry %d"),
4576 abfd, symndx, counter);
4577 cache_ptr->line_number = -1;
4578 ret = false;
4579 continue;
4581 sym = (coff_symbol_type *) (ent->u.syment._n._n_n._n_zeroes);
4583 /* PR 17512 file: 078-10659-0.004 */
4584 if (sym < obj_symbols (abfd)
4585 || sym >= obj_symbols (abfd) + bfd_get_symcount (abfd))
4587 _bfd_error_handler
4588 /* xgettext:c-format */
4589 (_("%pB: warning: illegal symbol in line number entry %d"),
4590 abfd, counter);
4591 cache_ptr->line_number = -1;
4592 ret = false;
4593 continue;
4596 have_func = true;
4597 nbr_func++;
4598 cache_ptr->u.sym = (asymbol *) sym;
4599 if (sym->lineno != NULL)
4600 _bfd_error_handler
4601 /* xgettext:c-format */
4602 (_("%pB: warning: duplicate line number information for `%s'"),
4603 abfd, bfd_asymbol_name (&sym->symbol));
4605 sym->lineno = cache_ptr;
4606 if (sym->symbol.value < prev_offset)
4607 ordered = false;
4608 prev_offset = sym->symbol.value;
4610 else if (!have_func)
4611 /* Drop line information that has no associated function.
4612 PR 17521: file: 078-10659-0.004. */
4613 continue;
4614 else
4615 cache_ptr->u.offset = dst.l_addr.l_paddr - bfd_section_vma (asect);
4616 cache_ptr++;
4619 asect->lineno_count = cache_ptr - lineno_cache;
4620 memset (cache_ptr, 0, sizeof (*cache_ptr));
4621 free (native_lineno);
4623 /* On some systems (eg AIX5.3) the lineno table may not be sorted. */
4624 if (!ordered)
4626 /* Sort the table. */
4627 alent **func_table;
4628 alent *n_lineno_cache;
4630 /* Create a table of functions. */
4631 if (_bfd_mul_overflow (nbr_func, sizeof (alent *), &amt))
4633 bfd_set_error (bfd_error_file_too_big);
4634 ret = false;
4636 else if ((func_table = (alent **) bfd_alloc (abfd, amt)) != NULL)
4638 alent **p = func_table;
4639 unsigned int i;
4641 for (i = 0; i < asect->lineno_count; i++)
4642 if (lineno_cache[i].line_number == 0)
4643 *p++ = &lineno_cache[i];
4645 BFD_ASSERT ((unsigned int) (p - func_table) == nbr_func);
4647 /* Sort by functions. */
4648 qsort (func_table, nbr_func, sizeof (alent *), coff_sort_func_alent);
4650 /* Create the new sorted table. */
4651 if (_bfd_mul_overflow (asect->lineno_count, sizeof (alent), &amt))
4653 bfd_set_error (bfd_error_file_too_big);
4654 ret = false;
4656 else if ((n_lineno_cache = (alent *) bfd_alloc (abfd, amt)) != NULL)
4658 alent *n_cache_ptr = n_lineno_cache;
4660 for (i = 0; i < nbr_func; i++)
4662 coff_symbol_type *sym;
4663 alent *old_ptr = func_table[i];
4665 /* Update the function entry. */
4666 sym = (coff_symbol_type *) old_ptr->u.sym;
4667 /* PR binutils/17512: Point the lineno to where
4668 this entry will be after the memcpy below. */
4669 sym->lineno = lineno_cache + (n_cache_ptr - n_lineno_cache);
4670 /* Copy the function and line number entries. */
4672 *n_cache_ptr++ = *old_ptr++;
4673 while (old_ptr->line_number != 0);
4676 memcpy (lineno_cache, n_lineno_cache,
4677 asect->lineno_count * sizeof (alent));
4679 else
4680 ret = false;
4681 bfd_release (abfd, func_table);
4683 else
4684 ret = false;
4687 return ret;
4690 /* Slurp in the symbol table, converting it to generic form. Note
4691 that if coff_relocate_section is defined, the linker will read
4692 symbols via coff_link_add_symbols, rather than via this routine. */
4694 static bool
4695 coff_slurp_symbol_table (bfd * abfd)
4697 combined_entry_type *native_symbols;
4698 coff_symbol_type *cached_area;
4699 unsigned int *table_ptr;
4700 unsigned int number_of_symbols = 0;
4701 bool ret = true;
4702 size_t amt;
4704 if (obj_symbols (abfd))
4705 return true;
4707 /* Read in the symbol table. */
4708 if ((native_symbols = coff_get_normalized_symtab (abfd)) == NULL)
4709 return false;
4711 /* Allocate enough room for all the symbols in cached form. */
4712 if (_bfd_mul_overflow (obj_raw_syment_count (abfd),
4713 sizeof (*cached_area), &amt))
4715 bfd_set_error (bfd_error_file_too_big);
4716 return false;
4718 cached_area = (coff_symbol_type *) bfd_alloc (abfd, amt);
4719 if (cached_area == NULL)
4720 return false;
4722 if (_bfd_mul_overflow (obj_raw_syment_count (abfd),
4723 sizeof (*table_ptr), &amt))
4725 bfd_set_error (bfd_error_file_too_big);
4726 return false;
4728 table_ptr = (unsigned int *) bfd_zalloc (abfd, amt);
4729 if (table_ptr == NULL)
4730 return false;
4731 else
4733 coff_symbol_type *dst = cached_area;
4734 unsigned int last_native_index = obj_raw_syment_count (abfd);
4735 unsigned int this_index = 0;
4737 while (this_index < last_native_index)
4739 combined_entry_type *src = native_symbols + this_index;
4740 table_ptr[this_index] = number_of_symbols;
4742 dst->symbol.the_bfd = abfd;
4743 BFD_ASSERT (src->is_sym);
4744 dst->symbol.name = (char *) (src->u.syment._n._n_n._n_offset);
4745 /* We use the native name field to point to the cached field. */
4746 src->u.syment._n._n_n._n_zeroes = (uintptr_t) dst;
4747 dst->symbol.section = coff_section_from_bfd_index (abfd,
4748 src->u.syment.n_scnum);
4749 dst->symbol.flags = 0;
4750 /* PR 17512: file: 079-7098-0.001:0.1. */
4751 dst->symbol.value = 0;
4752 dst->done_lineno = false;
4754 switch (src->u.syment.n_sclass)
4756 case C_EXT:
4757 case C_WEAKEXT:
4758 #if defined ARM
4759 case C_THUMBEXT:
4760 case C_THUMBEXTFUNC:
4761 #endif
4762 #ifdef RS6000COFF_C
4763 case C_HIDEXT:
4764 #ifndef AIX_WEAK_SUPPORT
4765 case C_AIX_WEAKEXT:
4766 #endif
4767 #endif
4768 #ifdef C_SYSTEM
4769 case C_SYSTEM: /* System Wide variable. */
4770 #endif
4771 #ifdef COFF_WITH_PE
4772 /* In PE, 0x68 (104) denotes a section symbol. */
4773 case C_SECTION:
4774 /* In PE, 0x69 (105) denotes a weak external symbol. */
4775 case C_NT_WEAK:
4776 #endif
4777 switch (coff_classify_symbol (abfd, &src->u.syment))
4779 case COFF_SYMBOL_GLOBAL:
4780 dst->symbol.flags = BSF_EXPORT | BSF_GLOBAL;
4781 #if defined COFF_WITH_PE
4782 /* PE sets the symbol to a value relative to the
4783 start of the section. */
4784 dst->symbol.value = src->u.syment.n_value;
4785 #else
4786 dst->symbol.value = (src->u.syment.n_value
4787 - dst->symbol.section->vma);
4788 #endif
4789 if (ISFCN ((src->u.syment.n_type)))
4790 /* A function ext does not go at the end of a
4791 file. */
4792 dst->symbol.flags |= BSF_NOT_AT_END | BSF_FUNCTION;
4793 break;
4795 case COFF_SYMBOL_COMMON:
4796 dst->symbol.section = bfd_com_section_ptr;
4797 dst->symbol.value = src->u.syment.n_value;
4798 break;
4800 case COFF_SYMBOL_UNDEFINED:
4801 dst->symbol.section = bfd_und_section_ptr;
4802 dst->symbol.value = 0;
4803 break;
4805 case COFF_SYMBOL_PE_SECTION:
4806 dst->symbol.flags |= BSF_EXPORT | BSF_SECTION_SYM;
4807 dst->symbol.value = 0;
4808 break;
4810 case COFF_SYMBOL_LOCAL:
4811 dst->symbol.flags = BSF_LOCAL;
4812 #if defined COFF_WITH_PE
4813 /* PE sets the symbol to a value relative to the
4814 start of the section. */
4815 dst->symbol.value = src->u.syment.n_value;
4816 #else
4817 dst->symbol.value = (src->u.syment.n_value
4818 - dst->symbol.section->vma);
4819 #endif
4820 if (ISFCN ((src->u.syment.n_type)))
4821 dst->symbol.flags |= BSF_NOT_AT_END | BSF_FUNCTION;
4822 break;
4825 #ifdef RS6000COFF_C
4826 /* A symbol with a csect entry should not go at the end. */
4827 if (src->u.syment.n_numaux > 0)
4828 dst->symbol.flags |= BSF_NOT_AT_END;
4829 #endif
4831 #ifdef COFF_WITH_PE
4832 if (src->u.syment.n_sclass == C_NT_WEAK)
4833 dst->symbol.flags |= BSF_WEAK;
4835 if (src->u.syment.n_sclass == C_SECTION
4836 && src->u.syment.n_scnum > 0)
4837 dst->symbol.flags = BSF_LOCAL;
4838 #endif
4839 if (src->u.syment.n_sclass == C_WEAKEXT
4840 #ifdef RS6000COFF_C
4841 || src->u.syment.n_sclass == C_AIX_WEAKEXT
4842 #endif
4844 dst->symbol.flags |= BSF_WEAK;
4846 break;
4848 case C_STAT: /* Static. */
4849 #if defined ARM
4850 case C_THUMBSTAT: /* Thumb static. */
4851 case C_THUMBLABEL: /* Thumb label. */
4852 case C_THUMBSTATFUNC:/* Thumb static function. */
4853 #endif
4854 #ifdef RS6000COFF_C
4855 case C_DWARF: /* A label in a dwarf section. */
4856 case C_INFO: /* A label in a comment section. */
4857 #endif
4858 case C_LABEL: /* Label. */
4859 if (src->u.syment.n_scnum == N_DEBUG)
4860 dst->symbol.flags = BSF_DEBUGGING;
4861 else
4862 dst->symbol.flags = BSF_LOCAL;
4864 /* Base the value as an index from the base of the
4865 section, if there is one. */
4866 if (dst->symbol.section)
4868 #if defined COFF_WITH_PE
4869 /* PE sets the symbol to a value relative to the
4870 start of the section. */
4871 dst->symbol.value = src->u.syment.n_value;
4872 #else
4873 dst->symbol.value = (src->u.syment.n_value
4874 - dst->symbol.section->vma);
4875 #endif
4877 else
4878 dst->symbol.value = src->u.syment.n_value;
4879 break;
4881 case C_FILE: /* File name. */
4882 dst->symbol.flags = BSF_FILE;
4883 /* Fall through. */
4884 case C_MOS: /* Member of structure. */
4885 case C_EOS: /* End of structure. */
4886 case C_REGPARM: /* Register parameter. */
4887 case C_REG: /* register variable. */
4888 /* C_AUTOARG conflicts with TI COFF C_UEXT. */
4889 case C_TPDEF: /* Type definition. */
4890 case C_ARG:
4891 case C_AUTO: /* Automatic variable. */
4892 case C_FIELD: /* Bit field. */
4893 case C_ENTAG: /* Enumeration tag. */
4894 case C_MOE: /* Member of enumeration. */
4895 case C_MOU: /* Member of union. */
4896 case C_UNTAG: /* Union tag. */
4897 case C_STRTAG: /* Structure tag. */
4898 #ifdef RS6000COFF_C
4899 case C_GSYM:
4900 case C_LSYM:
4901 case C_PSYM:
4902 case C_RSYM:
4903 case C_RPSYM:
4904 case C_STSYM:
4905 case C_TCSYM:
4906 case C_BCOMM:
4907 case C_ECOML:
4908 case C_ECOMM:
4909 case C_DECL:
4910 case C_ENTRY:
4911 case C_FUN:
4912 case C_ESTAT:
4913 #endif
4914 dst->symbol.flags |= BSF_DEBUGGING;
4915 dst->symbol.value = (src->u.syment.n_value);
4916 break;
4918 #ifdef RS6000COFF_C
4919 case C_BINCL: /* Beginning of include file. */
4920 case C_EINCL: /* Ending of include file. */
4921 /* The value is actually a pointer into the line numbers
4922 of the file. We locate the line number entry, and
4923 set the section to the section which contains it, and
4924 the value to the index in that section. */
4926 asection *sec;
4928 dst->symbol.flags = BSF_DEBUGGING;
4929 for (sec = abfd->sections; sec != NULL; sec = sec->next)
4930 if (sec->line_filepos <= (file_ptr) src->u.syment.n_value
4931 && ((file_ptr) (sec->line_filepos
4932 + sec->lineno_count * bfd_coff_linesz (abfd))
4933 > (file_ptr) src->u.syment.n_value))
4934 break;
4935 if (sec == NULL)
4936 dst->symbol.value = 0;
4937 else
4939 dst->symbol.section = sec;
4940 dst->symbol.value = ((src->u.syment.n_value
4941 - sec->line_filepos)
4942 / bfd_coff_linesz (abfd));
4943 src->fix_line = 1;
4946 break;
4948 case C_BSTAT:
4949 dst->symbol.flags = BSF_DEBUGGING;
4951 if (src->u.syment.n_value >= obj_raw_syment_count (abfd))
4952 dst->symbol.value = 0;
4953 else
4955 /* The value is actually a symbol index. Save a pointer
4956 to the symbol instead of the index. FIXME: This
4957 should use a union. */
4958 src->u.syment.n_value
4959 = (uintptr_t) (native_symbols + src->u.syment.n_value);
4960 dst->symbol.value = src->u.syment.n_value;
4961 src->fix_value = 1;
4963 break;
4964 #endif
4966 case C_BLOCK: /* ".bb" or ".eb". */
4967 case C_FCN: /* ".bf" or ".ef" (or PE ".lf"). */
4968 case C_EFCN: /* Physical end of function. */
4969 #if defined COFF_WITH_PE
4970 /* PE sets the symbol to a value relative to the start
4971 of the section. */
4972 dst->symbol.value = src->u.syment.n_value;
4973 if (strcmp (dst->symbol.name, ".bf") != 0)
4975 /* PE uses funny values for .ef and .lf; don't
4976 relocate them. */
4977 dst->symbol.flags = BSF_DEBUGGING;
4979 else
4980 dst->symbol.flags = BSF_DEBUGGING | BSF_DEBUGGING_RELOC;
4981 #else
4982 /* Base the value as an index from the base of the
4983 section. */
4984 dst->symbol.flags = BSF_LOCAL;
4985 dst->symbol.value = (src->u.syment.n_value
4986 - dst->symbol.section->vma);
4987 #endif
4988 break;
4990 case C_STATLAB: /* Static load time label. */
4991 dst->symbol.value = src->u.syment.n_value;
4992 dst->symbol.flags = BSF_GLOBAL;
4993 break;
4995 case C_NULL:
4996 /* PE DLLs sometimes have zeroed out symbols for some
4997 reason. Just ignore them without a warning. */
4998 if (src->u.syment.n_type == 0
4999 && src->u.syment.n_value == 0
5000 && src->u.syment.n_scnum == 0)
5001 break;
5002 #ifdef RS6000COFF_C
5003 /* XCOFF specific: deleted entry. */
5004 if (src->u.syment.n_value == C_NULL_VALUE)
5005 break;
5006 #endif
5007 /* Fall through. */
5008 case C_EXTDEF: /* External definition. */
5009 case C_ULABEL: /* Undefined label. */
5010 case C_USTATIC: /* Undefined static. */
5011 #ifndef COFF_WITH_PE
5012 /* C_LINE in regular coff is 0x68. NT has taken over this storage
5013 class to represent a section symbol. */
5014 case C_LINE: /* line # reformatted as symbol table entry. */
5015 /* NT uses 0x67 for a weak symbol, not C_ALIAS. */
5016 case C_ALIAS: /* Duplicate tag. */
5017 #endif
5018 /* New storage classes for TI COFF. */
5019 #ifdef TICOFF
5020 case C_UEXT: /* Tentative external definition. */
5021 #endif
5022 case C_EXTLAB: /* External load time label. */
5023 default:
5024 _bfd_error_handler
5025 /* xgettext:c-format */
5026 (_("%pB: unrecognized storage class %d for %s symbol `%s'"),
5027 abfd, src->u.syment.n_sclass,
5028 dst->symbol.section->name, dst->symbol.name);
5029 ret = false;
5030 /* Fall through. */
5031 case C_HIDDEN: /* Ext symbol in dmert public lib. */
5032 /* PR 20722: These symbols can also be generated by
5033 building DLLs with --gc-sections enabled. */
5034 dst->symbol.flags = BSF_DEBUGGING;
5035 dst->symbol.value = (src->u.syment.n_value);
5036 break;
5039 dst->native = src;
5040 dst->symbol.udata.i = 0;
5041 dst->lineno = NULL;
5043 this_index += (src->u.syment.n_numaux) + 1;
5044 dst++;
5045 number_of_symbols++;
5049 obj_symbols (abfd) = cached_area;
5050 obj_raw_syments (abfd) = native_symbols;
5052 abfd->symcount = number_of_symbols;
5053 obj_convert (abfd) = table_ptr;
5054 /* Slurp the line tables for each section too. */
5056 asection *p;
5058 p = abfd->sections;
5059 while (p)
5061 if (! coff_slurp_line_table (abfd, p))
5062 return false;
5063 p = p->next;
5067 return ret;
5070 /* Classify a COFF symbol. A couple of targets have globally visible
5071 symbols which are not class C_EXT, and this handles those. It also
5072 recognizes some special PE cases. */
5074 static enum coff_symbol_classification
5075 coff_classify_symbol (bfd *abfd,
5076 struct internal_syment *syment)
5078 /* FIXME: This partially duplicates the switch in
5079 coff_slurp_symbol_table. */
5080 switch (syment->n_sclass)
5082 case C_EXT:
5083 case C_WEAKEXT:
5084 #ifdef ARM
5085 case C_THUMBEXT:
5086 case C_THUMBEXTFUNC:
5087 #endif
5088 #ifdef RS6000COFF_C
5089 case C_HIDEXT:
5090 #ifndef AIX_WEAK_SUPPORT
5091 case C_AIX_WEAKEXT:
5092 #endif
5093 #endif
5094 #ifdef C_SYSTEM
5095 case C_SYSTEM:
5096 #endif
5097 #ifdef COFF_WITH_PE
5098 case C_NT_WEAK:
5099 #endif
5100 if (syment->n_scnum == 0)
5102 if (syment->n_value == 0)
5103 return COFF_SYMBOL_UNDEFINED;
5104 else
5105 return COFF_SYMBOL_COMMON;
5107 #ifdef RS6000COFF_C
5108 if (syment->n_sclass == C_HIDEXT)
5109 return COFF_SYMBOL_LOCAL;
5110 #endif
5111 return COFF_SYMBOL_GLOBAL;
5113 default:
5114 break;
5117 #ifdef COFF_WITH_PE
5118 if (syment->n_sclass == C_STAT)
5120 if (syment->n_scnum == 0)
5121 /* The Microsoft compiler sometimes generates these if a
5122 small static function is inlined every time it is used.
5123 The function is discarded, but the symbol table entry
5124 remains. */
5125 return COFF_SYMBOL_LOCAL;
5127 #ifdef STRICT_PE_FORMAT
5128 /* This is correct for Microsoft generated objects, but it
5129 breaks gas generated objects. */
5130 if (syment->n_value == 0)
5132 const asection *sec;
5133 const char *name;
5134 char buf[SYMNMLEN + 1];
5136 name = _bfd_coff_internal_syment_name (abfd, syment, buf);
5137 sec = coff_section_from_bfd_index (abfd, syment->n_scnum);
5138 if (sec != NULL && name != NULL
5139 && (strcmp (bfd_section_name (sec), name) == 0))
5140 return COFF_SYMBOL_PE_SECTION;
5142 #endif
5144 return COFF_SYMBOL_LOCAL;
5147 if (syment->n_sclass == C_SECTION)
5149 /* In some cases in a DLL generated by the Microsoft linker, the
5150 n_value field will contain garbage. FIXME: This should
5151 probably be handled by the swapping function instead. */
5152 syment->n_value = 0;
5153 if (syment->n_scnum == 0)
5154 return COFF_SYMBOL_UNDEFINED;
5155 return COFF_SYMBOL_PE_SECTION;
5157 #endif /* COFF_WITH_PE */
5159 /* If it is not a global symbol, we presume it is a local symbol. */
5160 if (syment->n_scnum == 0)
5162 char buf[SYMNMLEN + 1];
5164 _bfd_error_handler
5165 /* xgettext:c-format */
5166 (_("warning: %pB: local symbol `%s' has no section"),
5167 abfd, _bfd_coff_internal_syment_name (abfd, syment, buf));
5170 return COFF_SYMBOL_LOCAL;
5174 SUBSUBSECTION
5175 Reading relocations
5177 Coff relocations are easily transformed into the internal BFD form
5178 (@code{arelent}).
5180 Reading a coff relocation table is done in the following stages:
5182 o Read the entire coff relocation table into memory.
5184 o Process each relocation in turn; first swap it from the
5185 external to the internal form.
5187 o Turn the symbol referenced in the relocation's symbol index
5188 into a pointer into the canonical symbol table.
5189 This table is the same as the one returned by a call to
5190 @code{bfd_canonicalize_symtab}. The back end will call that
5191 routine and save the result if a canonicalization hasn't been done.
5193 o The reloc index is turned into a pointer to a howto
5194 structure, in a back end specific way. For instance, the 386
5195 uses the @code{r_type} to directly produce an index
5196 into a howto table vector.
5198 o Note that @code{arelent.addend} for COFF is often not what
5199 most people understand as a relocation addend, but rather an
5200 adjustment to the relocation addend stored in section contents
5201 of relocatable object files. The value found in section
5202 contents may also be confusing, depending on both symbol value
5203 and addend somewhat similar to the field value for a
5204 final-linked object. See @code{CALC_ADDEND}.
5207 #ifdef COFF_WITH_PE
5208 #define COFF_PE_ADDEND_BIAS(ptr) 0 /* Symbol value not stored in raw data. */
5209 #else
5210 #define COFF_PE_ADDEND_BIAS(ptr) ((ptr)->value)
5211 #endif
5213 #ifndef CALC_ADDEND
5214 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
5216 coff_symbol_type *coffsym = NULL; \
5218 if (ptr && bfd_asymbol_bfd (ptr) != abfd) \
5219 coffsym = (obj_symbols (abfd) \
5220 + (cache_ptr->sym_ptr_ptr - symbols)); \
5221 else if (ptr) \
5222 coffsym = coff_symbol_from (ptr); \
5223 if (coffsym != NULL \
5224 && coffsym->native->is_sym \
5225 && coffsym->native->u.syment.n_scnum == 0) \
5226 cache_ptr->addend = 0; \
5227 else if (ptr && bfd_asymbol_bfd (ptr) == abfd \
5228 && ptr->section != NULL) \
5229 cache_ptr->addend = - (ptr->section->vma \
5230 + COFF_PE_ADDEND_BIAS (ptr)); \
5231 else \
5232 cache_ptr->addend = 0; \
5234 #endif
5236 static bool
5237 coff_slurp_reloc_table (bfd * abfd, sec_ptr asect, asymbol ** symbols)
5239 bfd_byte *native_relocs;
5240 arelent *reloc_cache;
5241 arelent *cache_ptr;
5242 unsigned int idx;
5243 size_t amt;
5245 if (asect->relocation)
5246 return true;
5247 if (asect->reloc_count == 0)
5248 return true;
5249 if (asect->flags & SEC_CONSTRUCTOR)
5250 return true;
5251 if (!coff_slurp_symbol_table (abfd))
5252 return false;
5254 native_relocs = (bfd_byte *) buy_and_read (abfd, asect->rel_filepos,
5255 asect->reloc_count,
5256 bfd_coff_relsz (abfd));
5257 if (native_relocs == NULL)
5258 return false;
5260 if (_bfd_mul_overflow (asect->reloc_count, sizeof (arelent), &amt))
5262 bfd_set_error (bfd_error_file_too_big);
5263 return false;
5265 reloc_cache = (arelent *) bfd_alloc (abfd, amt);
5266 if (reloc_cache == NULL)
5268 free (native_relocs);
5269 return false;
5272 for (idx = 0; idx < asect->reloc_count; idx++)
5274 struct internal_reloc dst;
5275 void *src;
5276 #ifndef RELOC_PROCESSING
5277 asymbol *ptr;
5278 #endif
5280 cache_ptr = reloc_cache + idx;
5281 src = native_relocs + idx * (size_t) bfd_coff_relsz (abfd);
5283 dst.r_offset = 0;
5284 bfd_coff_swap_reloc_in (abfd, src, &dst);
5286 #ifdef RELOC_PROCESSING
5287 RELOC_PROCESSING (cache_ptr, &dst, symbols, abfd, asect);
5288 #else
5289 cache_ptr->address = dst.r_vaddr;
5291 if (dst.r_symndx != -1 && symbols != NULL)
5293 if (dst.r_symndx < 0 || dst.r_symndx >= obj_conv_table_size (abfd))
5295 _bfd_error_handler
5296 /* xgettext:c-format */
5297 (_("%pB: warning: illegal symbol index %ld in relocs"),
5298 abfd, dst.r_symndx);
5299 cache_ptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
5300 ptr = NULL;
5302 else
5304 cache_ptr->sym_ptr_ptr = (symbols
5305 + obj_convert (abfd)[dst.r_symndx]);
5306 ptr = *(cache_ptr->sym_ptr_ptr);
5309 else
5311 cache_ptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
5312 ptr = NULL;
5315 /* The symbols definitions that we have read in have been
5316 relocated as if their sections started at 0. But the offsets
5317 refering to the symbols in the raw data have not been
5318 modified, so we have to have a negative addend to compensate.
5320 Note that symbols which used to be common must be left alone. */
5322 /* Calculate any reloc addend by looking at the symbol. */
5323 CALC_ADDEND (abfd, ptr, dst, cache_ptr);
5324 (void) ptr;
5326 cache_ptr->address -= asect->vma;
5327 /* !! cache_ptr->section = NULL;*/
5329 /* Fill in the cache_ptr->howto field from dst.r_type. */
5330 RTYPE2HOWTO (cache_ptr, &dst);
5331 #endif /* RELOC_PROCESSING */
5333 if (cache_ptr->howto == NULL)
5335 _bfd_error_handler
5336 /* xgettext:c-format */
5337 (_("%pB: illegal relocation type %d at address %#" PRIx64),
5338 abfd, dst.r_type, (uint64_t) dst.r_vaddr);
5339 bfd_set_error (bfd_error_bad_value);
5340 free (native_relocs);
5341 return false;
5345 free (native_relocs);
5346 asect->relocation = reloc_cache;
5347 return true;
5350 #ifndef coff_rtype_to_howto
5351 #ifdef RTYPE2HOWTO
5353 /* Get the howto structure for a reloc. This is only used if the file
5354 including this one defines coff_relocate_section to be
5355 _bfd_coff_generic_relocate_section, so it is OK if it does not
5356 always work. It is the responsibility of the including file to
5357 make sure it is reasonable if it is needed. */
5359 static reloc_howto_type *
5360 coff_rtype_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
5361 asection *sec ATTRIBUTE_UNUSED,
5362 struct internal_reloc *rel ATTRIBUTE_UNUSED,
5363 struct coff_link_hash_entry *h ATTRIBUTE_UNUSED,
5364 struct internal_syment *sym ATTRIBUTE_UNUSED,
5365 bfd_vma *addendp ATTRIBUTE_UNUSED)
5367 arelent genrel;
5369 genrel.howto = NULL;
5370 RTYPE2HOWTO (&genrel, rel);
5371 return genrel.howto;
5374 #else /* ! defined (RTYPE2HOWTO) */
5376 #define coff_rtype_to_howto NULL
5378 #endif /* ! defined (RTYPE2HOWTO) */
5379 #endif /* ! defined (coff_rtype_to_howto) */
5381 /* This is stupid. This function should be a boolean predicate. */
5383 static long
5384 coff_canonicalize_reloc (bfd * abfd,
5385 sec_ptr section,
5386 arelent ** relptr,
5387 asymbol ** symbols)
5389 arelent *tblptr = section->relocation;
5390 unsigned int count = 0;
5392 if (section->flags & SEC_CONSTRUCTOR)
5394 /* This section has relocs made up by us, they are not in the
5395 file, so take them out of their chain and place them into
5396 the data area provided. */
5397 arelent_chain *chain = section->constructor_chain;
5399 for (count = 0; count < section->reloc_count; count++)
5401 *relptr++ = &chain->relent;
5402 chain = chain->next;
5405 else
5407 if (! coff_slurp_reloc_table (abfd, section, symbols))
5408 return -1;
5410 tblptr = section->relocation;
5412 for (; count++ < section->reloc_count;)
5413 *relptr++ = tblptr++;
5415 *relptr = 0;
5416 return section->reloc_count;
5419 #ifndef coff_set_reloc
5420 #define coff_set_reloc _bfd_generic_set_reloc
5421 #endif
5423 #ifndef coff_reloc16_estimate
5424 #define coff_reloc16_estimate dummy_reloc16_estimate
5426 static int
5427 dummy_reloc16_estimate (bfd *abfd ATTRIBUTE_UNUSED,
5428 asection *input_section ATTRIBUTE_UNUSED,
5429 arelent *reloc ATTRIBUTE_UNUSED,
5430 unsigned int shrink ATTRIBUTE_UNUSED,
5431 struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
5433 abort ();
5434 return 0;
5437 #endif
5439 #ifndef coff_reloc16_extra_cases
5441 #define coff_reloc16_extra_cases dummy_reloc16_extra_cases
5443 static bool
5444 dummy_reloc16_extra_cases (bfd *abfd ATTRIBUTE_UNUSED,
5445 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
5446 struct bfd_link_order *link_order ATTRIBUTE_UNUSED,
5447 arelent *reloc ATTRIBUTE_UNUSED,
5448 bfd_byte *data ATTRIBUTE_UNUSED,
5449 size_t *src_ptr ATTRIBUTE_UNUSED,
5450 size_t *dst_ptr ATTRIBUTE_UNUSED)
5452 return false;
5454 #endif
5456 /* If coff_relocate_section is defined, we can use the optimized COFF
5457 backend linker. Otherwise we must continue to use the old linker. */
5459 #ifdef coff_relocate_section
5461 #ifndef coff_bfd_link_hash_table_create
5462 #define coff_bfd_link_hash_table_create _bfd_coff_link_hash_table_create
5463 #endif
5464 #ifndef coff_bfd_link_add_symbols
5465 #define coff_bfd_link_add_symbols _bfd_coff_link_add_symbols
5466 #endif
5467 #ifndef coff_bfd_final_link
5468 #define coff_bfd_final_link _bfd_coff_final_link
5469 #endif
5471 #else /* ! defined (coff_relocate_section) */
5473 #define coff_relocate_section NULL
5474 #ifndef coff_bfd_link_hash_table_create
5475 #define coff_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
5476 #endif
5477 #ifndef coff_bfd_link_add_symbols
5478 #define coff_bfd_link_add_symbols _bfd_generic_link_add_symbols
5479 #endif
5480 #define coff_bfd_final_link _bfd_generic_final_link
5482 #endif /* ! defined (coff_relocate_section) */
5484 #define coff_bfd_link_just_syms _bfd_generic_link_just_syms
5485 #define coff_bfd_copy_link_hash_symbol_type \
5486 _bfd_generic_copy_link_hash_symbol_type
5487 #define coff_bfd_link_split_section _bfd_generic_link_split_section
5489 #define coff_bfd_link_check_relocs _bfd_generic_link_check_relocs
5491 #ifndef coff_start_final_link
5492 #define coff_start_final_link NULL
5493 #endif
5495 #ifndef coff_adjust_symndx
5496 #define coff_adjust_symndx NULL
5497 #endif
5499 #ifndef coff_link_add_one_symbol
5500 #define coff_link_add_one_symbol _bfd_generic_link_add_one_symbol
5501 #endif
5503 #ifndef coff_link_output_has_begun
5505 static bool
5506 coff_link_output_has_begun (bfd * abfd,
5507 struct coff_final_link_info * info ATTRIBUTE_UNUSED)
5509 return abfd->output_has_begun;
5511 #endif
5513 #ifndef coff_final_link_postscript
5515 static bool
5516 coff_final_link_postscript (bfd * abfd ATTRIBUTE_UNUSED,
5517 struct coff_final_link_info * pfinfo ATTRIBUTE_UNUSED)
5519 return true;
5521 #endif
5523 #ifndef coff_SWAP_aux_in
5524 #define coff_SWAP_aux_in coff_swap_aux_in
5525 #endif
5526 #ifndef coff_SWAP_sym_in
5527 #define coff_SWAP_sym_in coff_swap_sym_in
5528 #endif
5529 #ifndef coff_SWAP_lineno_in
5530 #define coff_SWAP_lineno_in coff_swap_lineno_in
5531 #endif
5532 #ifndef coff_SWAP_aux_out
5533 #define coff_SWAP_aux_out coff_swap_aux_out
5534 #endif
5535 #ifndef coff_SWAP_sym_out
5536 #define coff_SWAP_sym_out coff_swap_sym_out
5537 #endif
5538 #ifndef coff_SWAP_lineno_out
5539 #define coff_SWAP_lineno_out coff_swap_lineno_out
5540 #endif
5541 #ifndef coff_SWAP_reloc_out
5542 #define coff_SWAP_reloc_out coff_swap_reloc_out
5543 #endif
5544 #ifndef coff_SWAP_filehdr_out
5545 #define coff_SWAP_filehdr_out coff_swap_filehdr_out
5546 #endif
5547 #ifndef coff_SWAP_aouthdr_out
5548 #define coff_SWAP_aouthdr_out coff_swap_aouthdr_out
5549 #endif
5550 #ifndef coff_SWAP_scnhdr_out
5551 #define coff_SWAP_scnhdr_out coff_swap_scnhdr_out
5552 #endif
5553 #ifndef coff_SWAP_reloc_in
5554 #define coff_SWAP_reloc_in coff_swap_reloc_in
5555 #endif
5556 #ifndef coff_SWAP_filehdr_in
5557 #define coff_SWAP_filehdr_in coff_swap_filehdr_in
5558 #endif
5559 #ifndef coff_SWAP_aouthdr_in
5560 #define coff_SWAP_aouthdr_in coff_swap_aouthdr_in
5561 #endif
5562 #ifndef coff_SWAP_scnhdr_in
5563 #define coff_SWAP_scnhdr_in coff_swap_scnhdr_in
5564 #endif
5566 #define COFF_SWAP_TABLE (void *) &bfd_coff_std_swap_table
5568 static const bfd_coff_backend_data bfd_coff_std_swap_table ATTRIBUTE_UNUSED =
5570 coff_SWAP_aux_in, coff_SWAP_sym_in, coff_SWAP_lineno_in,
5571 coff_SWAP_aux_out, coff_SWAP_sym_out,
5572 coff_SWAP_lineno_out, coff_SWAP_reloc_out,
5573 coff_SWAP_filehdr_out, coff_SWAP_aouthdr_out,
5574 coff_SWAP_scnhdr_out,
5575 FILHSZ, AOUTSZ, SCNHSZ, SYMESZ, AUXESZ, RELSZ, LINESZ, FILNMLEN,
5576 #ifdef COFF_LONG_FILENAMES
5577 true,
5578 #else
5579 false,
5580 #endif
5581 COFF_DEFAULT_LONG_SECTION_NAMES,
5582 COFF_DEFAULT_SECTION_ALIGNMENT_POWER,
5583 #ifdef COFF_FORCE_SYMBOLS_IN_STRINGS
5584 true,
5585 #else
5586 false,
5587 #endif
5588 #ifdef COFF_DEBUG_STRING_WIDE_PREFIX
5590 #else
5592 #endif
5593 32768,
5594 coff_SWAP_filehdr_in, coff_SWAP_aouthdr_in, coff_SWAP_scnhdr_in,
5595 coff_SWAP_reloc_in, coff_bad_format_hook, coff_set_arch_mach_hook,
5596 coff_mkobject_hook, styp_to_sec_flags, coff_set_alignment_hook,
5597 coff_slurp_symbol_table, symname_in_debug_hook, coff_pointerize_aux_hook,
5598 coff_print_aux, coff_reloc16_extra_cases, coff_reloc16_estimate,
5599 coff_classify_symbol, coff_compute_section_file_positions,
5600 coff_start_final_link, coff_relocate_section, coff_rtype_to_howto,
5601 coff_adjust_symndx, coff_link_add_one_symbol,
5602 coff_link_output_has_begun, coff_final_link_postscript,
5603 bfd_pe_print_pdata
5606 #ifdef TICOFF
5607 /* COFF0 differs in file/section header size and relocation entry size. */
5609 static const bfd_coff_backend_data ticoff0_swap_table =
5611 coff_SWAP_aux_in, coff_SWAP_sym_in, coff_SWAP_lineno_in,
5612 coff_SWAP_aux_out, coff_SWAP_sym_out,
5613 coff_SWAP_lineno_out, coff_swap_reloc_v0_out,
5614 coff_SWAP_filehdr_out, coff_SWAP_aouthdr_out,
5615 coff_SWAP_scnhdr_out,
5616 FILHSZ_V0, AOUTSZ, SCNHSZ_V01, SYMESZ, AUXESZ, RELSZ_V0, LINESZ, FILNMLEN,
5617 #ifdef COFF_LONG_FILENAMES
5618 true,
5619 #else
5620 false,
5621 #endif
5622 COFF_DEFAULT_LONG_SECTION_NAMES,
5623 COFF_DEFAULT_SECTION_ALIGNMENT_POWER,
5624 #ifdef COFF_FORCE_SYMBOLS_IN_STRINGS
5625 true,
5626 #else
5627 false,
5628 #endif
5629 #ifdef COFF_DEBUG_STRING_WIDE_PREFIX
5631 #else
5633 #endif
5634 32768,
5635 coff_SWAP_filehdr_in, coff_SWAP_aouthdr_in, coff_SWAP_scnhdr_in,
5636 coff_swap_reloc_v0_in, ticoff0_bad_format_hook, coff_set_arch_mach_hook,
5637 coff_mkobject_hook, styp_to_sec_flags, coff_set_alignment_hook,
5638 coff_slurp_symbol_table, symname_in_debug_hook, coff_pointerize_aux_hook,
5639 coff_print_aux, coff_reloc16_extra_cases, coff_reloc16_estimate,
5640 coff_classify_symbol, coff_compute_section_file_positions,
5641 coff_start_final_link, coff_relocate_section, coff_rtype_to_howto,
5642 coff_adjust_symndx, coff_link_add_one_symbol,
5643 coff_link_output_has_begun, coff_final_link_postscript,
5644 bfd_pe_print_pdata
5646 #endif
5648 #ifdef TICOFF
5649 /* COFF1 differs in section header size. */
5651 static const bfd_coff_backend_data ticoff1_swap_table =
5653 coff_SWAP_aux_in, coff_SWAP_sym_in, coff_SWAP_lineno_in,
5654 coff_SWAP_aux_out, coff_SWAP_sym_out,
5655 coff_SWAP_lineno_out, coff_SWAP_reloc_out,
5656 coff_SWAP_filehdr_out, coff_SWAP_aouthdr_out,
5657 coff_SWAP_scnhdr_out,
5658 FILHSZ, AOUTSZ, SCNHSZ_V01, SYMESZ, AUXESZ, RELSZ, LINESZ, FILNMLEN,
5659 #ifdef COFF_LONG_FILENAMES
5660 true,
5661 #else
5662 false,
5663 #endif
5664 COFF_DEFAULT_LONG_SECTION_NAMES,
5665 COFF_DEFAULT_SECTION_ALIGNMENT_POWER,
5666 #ifdef COFF_FORCE_SYMBOLS_IN_STRINGS
5667 true,
5668 #else
5669 false,
5670 #endif
5671 #ifdef COFF_DEBUG_STRING_WIDE_PREFIX
5673 #else
5675 #endif
5676 32768,
5677 coff_SWAP_filehdr_in, coff_SWAP_aouthdr_in, coff_SWAP_scnhdr_in,
5678 coff_SWAP_reloc_in, ticoff1_bad_format_hook, coff_set_arch_mach_hook,
5679 coff_mkobject_hook, styp_to_sec_flags, coff_set_alignment_hook,
5680 coff_slurp_symbol_table, symname_in_debug_hook, coff_pointerize_aux_hook,
5681 coff_print_aux, coff_reloc16_extra_cases, coff_reloc16_estimate,
5682 coff_classify_symbol, coff_compute_section_file_positions,
5683 coff_start_final_link, coff_relocate_section, coff_rtype_to_howto,
5684 coff_adjust_symndx, coff_link_add_one_symbol,
5685 coff_link_output_has_begun, coff_final_link_postscript,
5686 bfd_pe_print_pdata /* huh */
5688 #endif
5690 #ifdef COFF_WITH_PE_BIGOBJ
5691 /* The UID for bigobj files. */
5693 static const char header_bigobj_classid[16] =
5695 0xC7, 0xA1, 0xBA, 0xD1,
5696 0xEE, 0xBA,
5697 0xa9, 0x4b,
5698 0xAF, 0x20,
5699 0xFA, 0xF6, 0x6A, 0xA4, 0xDC, 0xB8
5702 /* Swap routines. */
5704 static void
5705 coff_bigobj_swap_filehdr_in (bfd * abfd, void * src, void * dst)
5707 struct external_ANON_OBJECT_HEADER_BIGOBJ *filehdr_src =
5708 (struct external_ANON_OBJECT_HEADER_BIGOBJ *) src;
5709 struct internal_filehdr *filehdr_dst = (struct internal_filehdr *) dst;
5711 filehdr_dst->f_magic = H_GET_16 (abfd, filehdr_src->Machine);
5712 filehdr_dst->f_nscns = H_GET_32 (abfd, filehdr_src->NumberOfSections);
5713 filehdr_dst->f_timdat = H_GET_32 (abfd, filehdr_src->TimeDateStamp);
5714 filehdr_dst->f_symptr =
5715 GET_FILEHDR_SYMPTR (abfd, filehdr_src->PointerToSymbolTable);
5716 filehdr_dst->f_nsyms = H_GET_32 (abfd, filehdr_src->NumberOfSymbols);
5717 filehdr_dst->f_opthdr = 0;
5718 filehdr_dst->f_flags = 0;
5720 /* Check other magic numbers. */
5721 if (H_GET_16 (abfd, filehdr_src->Sig1) != IMAGE_FILE_MACHINE_UNKNOWN
5722 || H_GET_16 (abfd, filehdr_src->Sig2) != 0xffff
5723 || H_GET_16 (abfd, filehdr_src->Version) != 2
5724 || memcmp (filehdr_src->ClassID, header_bigobj_classid, 16) != 0)
5725 filehdr_dst->f_opthdr = 0xffff;
5727 /* Note that CLR metadata are ignored. */
5730 static unsigned int
5731 coff_bigobj_swap_filehdr_out (bfd *abfd, void * in, void * out)
5733 struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
5734 struct external_ANON_OBJECT_HEADER_BIGOBJ *filehdr_out =
5735 (struct external_ANON_OBJECT_HEADER_BIGOBJ *) out;
5737 memset (filehdr_out, 0, sizeof (*filehdr_out));
5739 H_PUT_16 (abfd, IMAGE_FILE_MACHINE_UNKNOWN, filehdr_out->Sig1);
5740 H_PUT_16 (abfd, 0xffff, filehdr_out->Sig2);
5741 H_PUT_16 (abfd, 2, filehdr_out->Version);
5742 memcpy (filehdr_out->ClassID, header_bigobj_classid, 16);
5743 H_PUT_16 (abfd, filehdr_in->f_magic, filehdr_out->Machine);
5744 H_PUT_32 (abfd, filehdr_in->f_nscns, filehdr_out->NumberOfSections);
5745 H_PUT_32 (abfd, filehdr_in->f_timdat, filehdr_out->TimeDateStamp);
5746 PUT_FILEHDR_SYMPTR (abfd, filehdr_in->f_symptr,
5747 filehdr_out->PointerToSymbolTable);
5748 H_PUT_32 (abfd, filehdr_in->f_nsyms, filehdr_out->NumberOfSymbols);
5750 return bfd_coff_filhsz (abfd);
5753 static void
5754 coff_bigobj_swap_sym_in (bfd * abfd, void * ext1, void * in1)
5756 SYMENT_BIGOBJ *ext = (SYMENT_BIGOBJ *) ext1;
5757 struct internal_syment *in = (struct internal_syment *) in1;
5759 if (ext->e.e_name[0] == 0)
5761 in->_n._n_n._n_zeroes = 0;
5762 in->_n._n_n._n_offset = H_GET_32 (abfd, ext->e.e.e_offset);
5764 else
5766 #if SYMNMLEN != E_SYMNMLEN
5767 #error we need to cope with truncating or extending SYMNMLEN
5768 #else
5769 memcpy (in->_n._n_name, ext->e.e_name, SYMNMLEN);
5770 #endif
5773 in->n_value = H_GET_32 (abfd, ext->e_value);
5774 BFD_ASSERT (sizeof (in->n_scnum) >= 4);
5775 in->n_scnum = H_GET_32 (abfd, ext->e_scnum);
5776 in->n_type = H_GET_16 (abfd, ext->e_type);
5777 in->n_sclass = H_GET_8 (abfd, ext->e_sclass);
5778 in->n_numaux = H_GET_8 (abfd, ext->e_numaux);
5781 static unsigned int
5782 coff_bigobj_swap_sym_out (bfd * abfd, void * inp, void * extp)
5784 struct internal_syment *in = (struct internal_syment *) inp;
5785 SYMENT_BIGOBJ *ext = (SYMENT_BIGOBJ *) extp;
5787 if (in->_n._n_name[0] == 0)
5789 H_PUT_32 (abfd, 0, ext->e.e.e_zeroes);
5790 H_PUT_32 (abfd, in->_n._n_n._n_offset, ext->e.e.e_offset);
5792 else
5794 #if SYMNMLEN != E_SYMNMLEN
5795 #error we need to cope with truncating or extending SYMNMLEN
5796 #else
5797 memcpy (ext->e.e_name, in->_n._n_name, SYMNMLEN);
5798 #endif
5801 H_PUT_32 (abfd, in->n_value, ext->e_value);
5802 H_PUT_32 (abfd, in->n_scnum, ext->e_scnum);
5804 H_PUT_16 (abfd, in->n_type, ext->e_type);
5805 H_PUT_8 (abfd, in->n_sclass, ext->e_sclass);
5806 H_PUT_8 (abfd, in->n_numaux, ext->e_numaux);
5808 return SYMESZ_BIGOBJ;
5811 static void
5812 coff_bigobj_swap_aux_in (bfd *abfd,
5813 void * ext1,
5814 int type,
5815 int in_class,
5816 int indx ATTRIBUTE_UNUSED,
5817 int numaux ATTRIBUTE_UNUSED,
5818 void * in1)
5820 AUXENT_BIGOBJ *ext = (AUXENT_BIGOBJ *) ext1;
5821 union internal_auxent *in = (union internal_auxent *) in1;
5823 /* Make sure that all fields in the aux structure are
5824 initialised. */
5825 memset (in, 0, sizeof * in);
5826 switch (in_class)
5828 case C_FILE:
5829 memcpy (in->x_file.x_n.x_fname, ext->File.Name, sizeof (ext->File.Name));
5830 break;
5832 case C_STAT:
5833 case C_LEAFSTAT:
5834 case C_HIDDEN:
5835 if (type == T_NULL)
5837 in->x_scn.x_scnlen = H_GET_32 (abfd, ext->Section.Length);
5838 in->x_scn.x_nreloc =
5839 H_GET_16 (abfd, ext->Section.NumberOfRelocations);
5840 in->x_scn.x_nlinno =
5841 H_GET_16 (abfd, ext->Section.NumberOfLinenumbers);
5842 in->x_scn.x_checksum = H_GET_32 (abfd, ext->Section.Checksum);
5843 in->x_scn.x_associated = H_GET_16 (abfd, ext->Section.Number)
5844 | (H_GET_16 (abfd, ext->Section.HighNumber) << 16);
5845 in->x_scn.x_comdat = H_GET_8 (abfd, ext->Section.Selection);
5846 return;
5848 break;
5850 default:
5851 in->x_sym.x_tagndx.u32 = H_GET_32 (abfd, ext->Sym.WeakDefaultSymIndex);
5852 /* Characteristics is ignored. */
5853 break;
5857 static unsigned int
5858 coff_bigobj_swap_aux_out (bfd * abfd,
5859 void * inp,
5860 int type,
5861 int in_class,
5862 int indx ATTRIBUTE_UNUSED,
5863 int numaux ATTRIBUTE_UNUSED,
5864 void * extp)
5866 union internal_auxent * in = (union internal_auxent *) inp;
5867 AUXENT_BIGOBJ *ext = (AUXENT_BIGOBJ *) extp;
5869 memset (ext, 0, AUXESZ);
5871 switch (in_class)
5873 case C_FILE:
5874 memcpy (ext->File.Name, in->x_file.x_n.x_fname, sizeof (ext->File.Name));
5876 return AUXESZ;
5878 case C_STAT:
5879 case C_LEAFSTAT:
5880 case C_HIDDEN:
5881 if (type == T_NULL)
5883 H_PUT_32 (abfd, in->x_scn.x_scnlen, ext->Section.Length);
5884 H_PUT_16 (abfd, in->x_scn.x_nreloc,
5885 ext->Section.NumberOfRelocations);
5886 H_PUT_16 (abfd, in->x_scn.x_nlinno,
5887 ext->Section.NumberOfLinenumbers);
5888 H_PUT_32 (abfd, in->x_scn.x_checksum, ext->Section.Checksum);
5889 H_PUT_16 (abfd, in->x_scn.x_associated & 0xffff,
5890 ext->Section.Number);
5891 H_PUT_16 (abfd, (in->x_scn.x_associated >> 16),
5892 ext->Section.HighNumber);
5893 H_PUT_8 (abfd, in->x_scn.x_comdat, ext->Section.Selection);
5894 return AUXESZ;
5896 break;
5899 H_PUT_32 (abfd, in->x_sym.x_tagndx.u32, ext->Sym.WeakDefaultSymIndex);
5900 H_PUT_32 (abfd, 1, ext->Sym.WeakSearchType);
5902 return AUXESZ;
5905 static const bfd_coff_backend_data bigobj_swap_table =
5907 coff_bigobj_swap_aux_in, coff_bigobj_swap_sym_in, coff_SWAP_lineno_in,
5908 coff_bigobj_swap_aux_out, coff_bigobj_swap_sym_out,
5909 coff_SWAP_lineno_out, coff_SWAP_reloc_out,
5910 coff_bigobj_swap_filehdr_out, coff_SWAP_aouthdr_out,
5911 coff_SWAP_scnhdr_out,
5912 FILHSZ_BIGOBJ, AOUTSZ, SCNHSZ, SYMESZ_BIGOBJ, AUXESZ_BIGOBJ,
5913 RELSZ, LINESZ, FILNMLEN_BIGOBJ,
5914 true,
5915 COFF_DEFAULT_LONG_SECTION_NAMES,
5916 COFF_DEFAULT_SECTION_ALIGNMENT_POWER,
5917 false,
5919 1U << 31,
5920 coff_bigobj_swap_filehdr_in, coff_SWAP_aouthdr_in, coff_SWAP_scnhdr_in,
5921 coff_SWAP_reloc_in, coff_bad_format_hook, coff_set_arch_mach_hook,
5922 coff_mkobject_hook, styp_to_sec_flags, coff_set_alignment_hook,
5923 coff_slurp_symbol_table, symname_in_debug_hook, coff_pointerize_aux_hook,
5924 coff_print_aux, coff_reloc16_extra_cases, coff_reloc16_estimate,
5925 coff_classify_symbol, coff_compute_section_file_positions,
5926 coff_start_final_link, coff_relocate_section, coff_rtype_to_howto,
5927 coff_adjust_symndx, coff_link_add_one_symbol,
5928 coff_link_output_has_begun, coff_final_link_postscript,
5929 bfd_pe_print_pdata /* huh */
5932 #endif /* COFF_WITH_PE_BIGOBJ */
5934 #ifndef coff_close_and_cleanup
5935 #define coff_close_and_cleanup _bfd_generic_close_and_cleanup
5936 #endif
5938 #ifndef coff_bfd_free_cached_info
5939 #define coff_bfd_free_cached_info _bfd_coff_free_cached_info
5940 #endif
5942 #ifndef coff_get_section_contents
5943 #define coff_get_section_contents _bfd_generic_get_section_contents
5944 #endif
5946 #ifndef coff_bfd_copy_private_symbol_data
5947 #define coff_bfd_copy_private_symbol_data _bfd_generic_bfd_copy_private_symbol_data
5948 #endif
5950 #ifndef coff_bfd_copy_private_header_data
5951 #define coff_bfd_copy_private_header_data _bfd_generic_bfd_copy_private_header_data
5952 #endif
5954 #ifndef coff_bfd_copy_private_section_data
5955 #define coff_bfd_copy_private_section_data _bfd_generic_bfd_copy_private_section_data
5956 #endif
5958 #ifndef coff_bfd_copy_private_bfd_data
5959 #define coff_bfd_copy_private_bfd_data _bfd_generic_bfd_copy_private_bfd_data
5960 #endif
5962 #ifndef coff_bfd_merge_private_bfd_data
5963 #define coff_bfd_merge_private_bfd_data _bfd_generic_bfd_merge_private_bfd_data
5964 #endif
5966 #ifndef coff_bfd_set_private_flags
5967 #define coff_bfd_set_private_flags _bfd_generic_bfd_set_private_flags
5968 #endif
5970 #ifndef coff_bfd_print_private_bfd_data
5971 #define coff_bfd_print_private_bfd_data _bfd_generic_bfd_print_private_bfd_data
5972 #endif
5974 #ifndef coff_bfd_is_local_label_name
5975 #define coff_bfd_is_local_label_name _bfd_coff_is_local_label_name
5976 #endif
5978 #ifndef coff_bfd_is_target_special_symbol
5979 #define coff_bfd_is_target_special_symbol _bfd_bool_bfd_asymbol_false
5980 #endif
5982 #ifndef coff_read_minisymbols
5983 #define coff_read_minisymbols _bfd_generic_read_minisymbols
5984 #endif
5986 #ifndef coff_minisymbol_to_symbol
5987 #define coff_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
5988 #endif
5990 /* The reloc lookup routine must be supplied by each individual COFF
5991 backend. */
5992 #ifndef coff_bfd_reloc_type_lookup
5993 #define coff_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
5994 #endif
5995 #ifndef coff_bfd_reloc_name_lookup
5996 #define coff_bfd_reloc_name_lookup _bfd_norelocs_bfd_reloc_name_lookup
5997 #endif
5999 #ifndef coff_bfd_get_relocated_section_contents
6000 #define coff_bfd_get_relocated_section_contents \
6001 bfd_generic_get_relocated_section_contents
6002 #endif
6004 #ifndef coff_bfd_relax_section
6005 #define coff_bfd_relax_section bfd_generic_relax_section
6006 #endif
6008 #ifndef coff_bfd_gc_sections
6009 #define coff_bfd_gc_sections bfd_coff_gc_sections
6010 #endif
6012 #ifndef coff_bfd_lookup_section_flags
6013 #define coff_bfd_lookup_section_flags bfd_generic_lookup_section_flags
6014 #endif
6016 #ifndef coff_bfd_merge_sections
6017 #define coff_bfd_merge_sections bfd_generic_merge_sections
6018 #endif
6020 #ifndef coff_bfd_is_group_section
6021 #define coff_bfd_is_group_section bfd_generic_is_group_section
6022 #endif
6024 #ifndef coff_bfd_group_name
6025 #define coff_bfd_group_name bfd_coff_group_name
6026 #endif
6028 #ifndef coff_bfd_discard_group
6029 #define coff_bfd_discard_group bfd_generic_discard_group
6030 #endif
6032 #ifndef coff_section_already_linked
6033 #define coff_section_already_linked \
6034 _bfd_coff_section_already_linked
6035 #endif
6037 #ifndef coff_bfd_define_common_symbol
6038 #define coff_bfd_define_common_symbol bfd_generic_define_common_symbol
6039 #endif
6041 #ifndef coff_bfd_link_hide_symbol
6042 #define coff_bfd_link_hide_symbol _bfd_generic_link_hide_symbol
6043 #endif
6045 #ifndef coff_bfd_define_start_stop
6046 #define coff_bfd_define_start_stop bfd_generic_define_start_stop
6047 #endif
6049 #define CREATE_BIG_COFF_TARGET_VEC(VAR, NAME, EXTRA_O_FLAGS, EXTRA_S_FLAGS, UNDER, ALTERNATIVE, SWAP_TABLE) \
6050 const bfd_target VAR = \
6052 NAME , \
6053 bfd_target_coff_flavour, \
6054 BFD_ENDIAN_BIG, /* Data byte order is big. */ \
6055 BFD_ENDIAN_BIG, /* Header byte order is big. */ \
6056 /* object flags */ \
6057 (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | \
6058 HAS_SYMS | HAS_LOCALS | WP_TEXT | EXTRA_O_FLAGS), \
6059 /* section flags */ \
6060 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | EXTRA_S_FLAGS),\
6061 UNDER, /* Leading symbol underscore. */ \
6062 '/', /* AR_pad_char. */ \
6063 15, /* AR_max_namelen. */ \
6064 0, /* match priority. */ \
6065 TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */ \
6067 /* Data conversion functions. */ \
6068 bfd_getb64, bfd_getb_signed_64, bfd_putb64, \
6069 bfd_getb32, bfd_getb_signed_32, bfd_putb32, \
6070 bfd_getb16, bfd_getb_signed_16, bfd_putb16, \
6072 /* Header conversion functions. */ \
6073 bfd_getb64, bfd_getb_signed_64, bfd_putb64, \
6074 bfd_getb32, bfd_getb_signed_32, bfd_putb32, \
6075 bfd_getb16, bfd_getb_signed_16, bfd_putb16, \
6077 { /* bfd_check_format. */ \
6078 _bfd_dummy_target, \
6079 coff_object_p, \
6080 bfd_generic_archive_p, \
6081 _bfd_dummy_target \
6082 }, \
6083 { /* bfd_set_format. */ \
6084 _bfd_bool_bfd_false_error, \
6085 coff_mkobject, \
6086 _bfd_generic_mkarchive, \
6087 _bfd_bool_bfd_false_error \
6088 }, \
6089 { /* bfd_write_contents. */ \
6090 _bfd_bool_bfd_false_error, \
6091 coff_write_object_contents, \
6092 _bfd_write_archive_contents, \
6093 _bfd_bool_bfd_false_error \
6094 }, \
6096 BFD_JUMP_TABLE_GENERIC (coff), \
6097 BFD_JUMP_TABLE_COPY (coff), \
6098 BFD_JUMP_TABLE_CORE (_bfd_nocore), \
6099 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff), \
6100 BFD_JUMP_TABLE_SYMBOLS (coff), \
6101 BFD_JUMP_TABLE_RELOCS (coff), \
6102 BFD_JUMP_TABLE_WRITE (coff), \
6103 BFD_JUMP_TABLE_LINK (coff), \
6104 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), \
6106 ALTERNATIVE, \
6108 SWAP_TABLE \
6111 #define CREATE_BIGHDR_COFF_TARGET_VEC(VAR, NAME, EXTRA_O_FLAGS, EXTRA_S_FLAGS, UNDER, ALTERNATIVE, SWAP_TABLE) \
6112 const bfd_target VAR = \
6114 NAME , \
6115 bfd_target_coff_flavour, \
6116 BFD_ENDIAN_LITTLE, /* Data byte order is little. */ \
6117 BFD_ENDIAN_BIG, /* Header byte order is big. */ \
6118 /* object flags */ \
6119 (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | \
6120 HAS_SYMS | HAS_LOCALS | WP_TEXT | EXTRA_O_FLAGS), \
6121 /* section flags */ \
6122 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | EXTRA_S_FLAGS),\
6123 UNDER, /* Leading symbol underscore. */ \
6124 '/', /* AR_pad_char. */ \
6125 15, /* AR_max_namelen. */ \
6126 0, /* match priority. */ \
6127 TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */ \
6129 /* Data conversion functions. */ \
6130 bfd_getb64, bfd_getb_signed_64, bfd_putb64, \
6131 bfd_getb32, bfd_getb_signed_32, bfd_putb32, \
6132 bfd_getb16, bfd_getb_signed_16, bfd_putb16, \
6134 /* Header conversion functions. */ \
6135 bfd_getb64, bfd_getb_signed_64, bfd_putb64, \
6136 bfd_getb32, bfd_getb_signed_32, bfd_putb32, \
6137 bfd_getb16, bfd_getb_signed_16, bfd_putb16, \
6139 { /* bfd_check_format. */ \
6140 _bfd_dummy_target, \
6141 coff_object_p, \
6142 bfd_generic_archive_p, \
6143 _bfd_dummy_target \
6144 }, \
6145 { /* bfd_set_format. */ \
6146 _bfd_bool_bfd_false_error, \
6147 coff_mkobject, \
6148 _bfd_generic_mkarchive, \
6149 _bfd_bool_bfd_false_error \
6150 }, \
6151 { /* bfd_write_contents. */ \
6152 _bfd_bool_bfd_false_error, \
6153 coff_write_object_contents, \
6154 _bfd_write_archive_contents, \
6155 _bfd_bool_bfd_false_error \
6156 }, \
6158 BFD_JUMP_TABLE_GENERIC (coff), \
6159 BFD_JUMP_TABLE_COPY (coff), \
6160 BFD_JUMP_TABLE_CORE (_bfd_nocore), \
6161 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff), \
6162 BFD_JUMP_TABLE_SYMBOLS (coff), \
6163 BFD_JUMP_TABLE_RELOCS (coff), \
6164 BFD_JUMP_TABLE_WRITE (coff), \
6165 BFD_JUMP_TABLE_LINK (coff), \
6166 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), \
6168 ALTERNATIVE, \
6170 SWAP_TABLE \
6173 #define CREATE_LITTLE_COFF_TARGET_VEC(VAR, NAME, EXTRA_O_FLAGS, EXTRA_S_FLAGS, UNDER, ALTERNATIVE, SWAP_TABLE) \
6174 const bfd_target VAR = \
6176 NAME , \
6177 bfd_target_coff_flavour, \
6178 BFD_ENDIAN_LITTLE, /* Data byte order is little. */ \
6179 BFD_ENDIAN_LITTLE, /* Header byte order is little. */ \
6180 /* object flags */ \
6181 (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | \
6182 HAS_SYMS | HAS_LOCALS | WP_TEXT | EXTRA_O_FLAGS), \
6183 /* section flags */ \
6184 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | EXTRA_S_FLAGS),\
6185 UNDER, /* Leading symbol underscore. */ \
6186 '/', /* AR_pad_char. */ \
6187 15, /* AR_max_namelen. */ \
6188 0, /* match priority. */ \
6189 TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */ \
6191 /* Data conversion functions. */ \
6192 bfd_getl64, bfd_getl_signed_64, bfd_putl64, \
6193 bfd_getl32, bfd_getl_signed_32, bfd_putl32, \
6194 bfd_getl16, bfd_getl_signed_16, bfd_putl16, \
6195 /* Header conversion functions. */ \
6196 bfd_getl64, bfd_getl_signed_64, bfd_putl64, \
6197 bfd_getl32, bfd_getl_signed_32, bfd_putl32, \
6198 bfd_getl16, bfd_getl_signed_16, bfd_putl16, \
6200 { /* bfd_check_format. */ \
6201 _bfd_dummy_target, \
6202 coff_object_p, \
6203 bfd_generic_archive_p, \
6204 _bfd_dummy_target \
6205 }, \
6206 { /* bfd_set_format. */ \
6207 _bfd_bool_bfd_false_error, \
6208 coff_mkobject, \
6209 _bfd_generic_mkarchive, \
6210 _bfd_bool_bfd_false_error \
6211 }, \
6212 { /* bfd_write_contents. */ \
6213 _bfd_bool_bfd_false_error, \
6214 coff_write_object_contents, \
6215 _bfd_write_archive_contents, \
6216 _bfd_bool_bfd_false_error \
6217 }, \
6219 BFD_JUMP_TABLE_GENERIC (coff), \
6220 BFD_JUMP_TABLE_COPY (coff), \
6221 BFD_JUMP_TABLE_CORE (_bfd_nocore), \
6222 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff), \
6223 BFD_JUMP_TABLE_SYMBOLS (coff), \
6224 BFD_JUMP_TABLE_RELOCS (coff), \
6225 BFD_JUMP_TABLE_WRITE (coff), \
6226 BFD_JUMP_TABLE_LINK (coff), \
6227 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), \
6229 ALTERNATIVE, \
6231 SWAP_TABLE \