Home | History | Annotate | Line # | Download | only in bfd
coffcode.h revision 1.1.1.10
      1 /* Support for the generic parts of most COFF variants, for BFD.
      2    Copyright (C) 1990-2022 Free Software Foundation, Inc.
      3    Written by Cygnus Support.
      4 
      5    This file is part of BFD, the Binary File Descriptor library.
      6 
      7    This program is free software; you can redistribute it and/or modify
      8    it under the terms of the GNU General Public License as published by
      9    the Free Software Foundation; either version 3 of the License, or
     10    (at your option) any later version.
     11 
     12    This program is distributed in the hope that it will be useful,
     13    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15    GNU General Public License for more details.
     16 
     17    You should have received a copy of the GNU General Public License
     18    along with this program; if not, write to the Free Software
     19    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     20    MA 02110-1301, USA.  */
     21 
     22 /* Most of this hacked by  Steve Chamberlain,
     23 			sac (at) cygnus.com.  */
     24 /*
     25 SECTION
     26 	coff backends
     27 
     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.
     32 
     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}.
     42 
     43 SUBSECTION
     44 	Porting to a new version of coff
     45 
     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.
     58 
     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.
     67 
     68 SUBSECTION
     69 	How the coff backend works
     70 
     71 SUBSUBSECTION
     72 	File layout
     73 
     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.
     80 
     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.
     85 
     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}.
     91 
     92 	Some of the Coff targets then also have additional routines in
     93 	the target source file itself.
     94 
     95 SUBSUBSECTION
     96 	Coff long section names
     97 
     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.
    103 
    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.
    113 
    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.
    118 
    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.
    126 
    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 the flag to be altered
    138 	at runtime, on formats that support long section names at all; on
    139 	other formats it points to a stub that returns an error indication.
    140 
    141 	With input BFDs, the flag is set according to whether any long section
    142 	names are detected while reading the section headers.  For a completely
    143 	new BFD, the flag is set to the default for the target format.  This
    144 	information can be used by a client of the BFD library when deciding
    145 	what output format to generate, and means that a BFD that is opened
    146 	for read and subsequently converted to a writeable BFD and modified
    147 	in-place will retain whatever format it had on input.
    148 
    149 	If @code{COFF_LONG_SECTION_NAMES} is simply defined (blank), or is
    150 	defined to the value "1", then long section names are enabled by
    151 	default; if it is defined to the value zero, they are disabled by
    152 	default (but still accepted in input BFDs).  The header @file{coffcode.h}
    153 	defines a macro, @code{COFF_DEFAULT_LONG_SECTION_NAMES}, which is
    154 	used in the backends to initialise the backend data structure fields
    155 	appropriately; see the comments for further detail.
    156 
    157 SUBSUBSECTION
    158 	Bit twiddling
    159 
    160 	Each flavour of coff supported in BFD has its own header file
    161 	describing the external layout of the structures. There is also
    162 	an internal description of the coff layout, in
    163 	@file{coff/internal.h}. A major function of the
    164 	coff backend is swapping the bytes and twiddling the bits to
    165 	translate the external form of the structures into the normal
    166 	internal form. This is all performed in the
    167 	@code{bfd_swap}_@i{thing}_@i{direction} routines. Some
    168 	elements are different sizes between different versions of
    169 	coff; it is the duty of the coff version specific include file
    170 	to override the definitions of various packing routines in
    171 	@file{coffcode.h}. E.g., the size of line number entry in coff is
    172 	sometimes 16 bits, and sometimes 32 bits. @code{#define}ing
    173 	@code{PUT_LNSZ_LNNO} and @code{GET_LNSZ_LNNO} will select the
    174 	correct one. No doubt, some day someone will find a version of
    175 	coff which has a varying field size not catered to at the
    176 	moment. To port BFD, that person will have to add more @code{#defines}.
    177 	Three of the bit twiddling routines are exported to
    178 	@code{gdb}; @code{coff_swap_aux_in}, @code{coff_swap_sym_in}
    179 	and @code{coff_swap_lineno_in}. @code{GDB} reads the symbol
    180 	table on its own, but uses BFD to fix things up.  More of the
    181 	bit twiddlers are exported for @code{gas};
    182 	@code{coff_swap_aux_out}, @code{coff_swap_sym_out},
    183 	@code{coff_swap_lineno_out}, @code{coff_swap_reloc_out},
    184 	@code{coff_swap_filehdr_out}, @code{coff_swap_aouthdr_out},
    185 	@code{coff_swap_scnhdr_out}. @code{Gas} currently keeps track
    186 	of all the symbol table and reloc drudgery itself, thereby
    187 	saving the internal BFD overhead, but uses BFD to swap things
    188 	on the way out, making cross ports much safer.  Doing so also
    189 	allows BFD (and thus the linker) to use the same header files
    190 	as @code{gas}, which makes one avenue to disaster disappear.
    191 
    192 SUBSUBSECTION
    193 	Symbol reading
    194 
    195 	The simple canonical form for symbols used by BFD is not rich
    196 	enough to keep all the information available in a coff symbol
    197 	table. The back end gets around this problem by keeping the original
    198 	symbol table around, "behind the scenes".
    199 
    200 	When a symbol table is requested (through a call to
    201 	@code{bfd_canonicalize_symtab}), a request gets through to
    202 	@code{coff_get_normalized_symtab}. This reads the symbol table from
    203 	the coff file and swaps all the structures inside into the
    204 	internal form. It also fixes up all the pointers in the table
    205 	(represented in the file by offsets from the first symbol in
    206 	the table) into physical pointers to elements in the new
    207 	internal table. This involves some work since the meanings of
    208 	fields change depending upon context: a field that is a
    209 	pointer to another structure in the symbol table at one moment
    210 	may be the size in bytes of a structure at the next.  Another
    211 	pass is made over the table. All symbols which mark file names
    212 	(<<C_FILE>> symbols) are modified so that the internal
    213 	string points to the value in the auxent (the real filename)
    214 	rather than the normal text associated with the symbol
    215 	(@code{".file"}).
    216 
    217 	At this time the symbol names are moved around. Coff stores
    218 	all symbols less than nine characters long physically
    219 	within the symbol table; longer strings are kept at the end of
    220 	the file in the string table. This pass moves all strings
    221 	into memory and replaces them with pointers to the strings.
    222 
    223 	The symbol table is massaged once again, this time to create
    224 	the canonical table used by the BFD application. Each symbol
    225 	is inspected in turn, and a decision made (using the
    226 	@code{sclass} field) about the various flags to set in the
    227 	@code{asymbol}.  @xref{Symbols}. The generated canonical table
    228 	shares strings with the hidden internal symbol table.
    229 
    230 	Any linenumbers are read from the coff file too, and attached
    231 	to the symbols which own the functions the linenumbers belong to.
    232 
    233 SUBSUBSECTION
    234 	Symbol writing
    235 
    236 	Writing a symbol to a coff file which didn't come from a coff
    237 	file will lose any debugging information. The @code{asymbol}
    238 	structure remembers the BFD from which the symbol was taken, and on
    239 	output the back end makes sure that the same destination target as
    240 	source target is present.
    241 
    242 	When the symbols have come from a coff file then all the
    243 	debugging information is preserved.
    244 
    245 	Symbol tables are provided for writing to the back end in a
    246 	vector of pointers to pointers. This allows applications like
    247 	the linker to accumulate and output large symbol tables
    248 	without having to do too much byte copying.
    249 
    250 	This function runs through the provided symbol table and
    251 	patches each symbol marked as a file place holder
    252 	(@code{C_FILE}) to point to the next file place holder in the
    253 	list. It also marks each @code{offset} field in the list with
    254 	the offset from the first symbol of the current symbol.
    255 
    256 	Another function of this procedure is to turn the canonical
    257 	value form of BFD into the form used by coff. Internally, BFD
    258 	expects symbol values to be offsets from a section base; so a
    259 	symbol physically at 0x120, but in a section starting at
    260 	0x100, would have the value 0x20. Coff expects symbols to
    261 	contain their final value, so symbols have their values
    262 	changed at this point to reflect their sum with their owning
    263 	section.  This transformation uses the
    264 	<<output_section>> field of the @code{asymbol}'s
    265 	@code{asection} @xref{Sections}.
    266 
    267 	o <<coff_mangle_symbols>>
    268 
    269 	This routine runs though the provided symbol table and uses
    270 	the offsets generated by the previous pass and the pointers
    271 	generated when the symbol table was read in to create the
    272 	structured hierarchy required by coff. It changes each pointer
    273 	to a symbol into the index into the symbol table of the asymbol.
    274 
    275 	o <<coff_write_symbols>>
    276 
    277 	This routine runs through the symbol table and patches up the
    278 	symbols from their internal form into the coff way, calls the
    279 	bit twiddlers, and writes out the table to the file.
    280 
    281 */
    282 
    283 /*
    284 INTERNAL_DEFINITION
    285 	coff_symbol_type
    286 
    287 DESCRIPTION
    288 	The hidden information for an <<asymbol>> is described in a
    289 	<<combined_entry_type>>:
    290 
    291 CODE_FRAGMENT
    292 .
    293 .typedef struct coff_ptr_struct
    294 .{
    295 .  {* Remembers the offset from the first symbol in the file for
    296 .     this symbol. Generated by coff_renumber_symbols.  *}
    297 .  unsigned int offset;
    298 .
    299 .  {* Should the value of this symbol be renumbered.  Used for
    300 .     XCOFF C_BSTAT symbols.  Set by coff_slurp_symbol_table.  *}
    301 .  unsigned int fix_value : 1;
    302 .
    303 .  {* Should the tag field of this symbol be renumbered.
    304 .     Created by coff_pointerize_aux.  *}
    305 .  unsigned int fix_tag : 1;
    306 .
    307 .  {* Should the endidx field of this symbol be renumbered.
    308 .     Created by coff_pointerize_aux.  *}
    309 .  unsigned int fix_end : 1;
    310 .
    311 .  {* Should the x_csect.x_scnlen field be renumbered.
    312 .     Created by coff_pointerize_aux.  *}
    313 .  unsigned int fix_scnlen : 1;
    314 .
    315 .  {* Fix up an XCOFF C_BINCL/C_EINCL symbol.  The value is the
    316 .     index into the line number entries.  Set by coff_slurp_symbol_table.  *}
    317 .  unsigned int fix_line : 1;
    318 .
    319 .  {* The container for the symbol structure as read and translated
    320 .     from the file.  *}
    321 .  union
    322 .  {
    323 .    union internal_auxent auxent;
    324 .    struct internal_syment syment;
    325 .  } u;
    326 .
    327 . {* Selector for the union above.  *}
    328 . bool is_sym;
    329 .
    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;
    334 .
    335 .
    336 .{* Each canonical asymbol really looks like this: *}
    337 .
    338 .typedef struct coff_symbol_struct
    339 .{
    340 .  {* The actual symbol which the rest of BFD works with *}
    341 .  asymbol symbol;
    342 .
    343 .  {* A pointer to the hidden information for this symbol *}
    344 .  combined_entry_type *native;
    345 .
    346 .  {* A pointer to the linenumber information for this symbol *}
    347 .  struct lineno_cache_entry *lineno;
    348 .
    349 .  {* Have the line numbers been relocated yet ? *}
    350 .  bool done_lineno;
    351 .} coff_symbol_type;
    352 
    353 */
    354 
    355 #include "libiberty.h"
    356 
    357 #ifdef COFF_WITH_PE
    358 #include "peicode.h"
    359 #else
    360 #include "coffswap.h"
    361 #endif
    362 
    363 #define STRING_SIZE_SIZE 4
    364 
    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"
    370 
    371 #if defined(COFF_WITH_PE) || defined(COFF_GO32_EXE) || defined(COFF_GO32)
    372 # define COFF_WITH_EXTENDED_RELOC_COUNTER
    373 #endif
    374 
    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) */
    393 
    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 */
    401 
    402 #if defined (COFF_LONG_SECTION_NAMES)
    403 static bool bfd_coff_set_long_section_names_allowed
    404   (bfd *, int);
    405 #else /* !defined (COFF_LONG_SECTION_NAMES) */
    406 static bool bfd_coff_set_long_section_names_disallowed
    407   (bfd *, int);
    408 #endif /* defined (COFF_LONG_SECTION_NAMES) */
    409 static long sec_to_styp_flags
    410   (const char *, flagword);
    411 static bool styp_to_sec_flags
    412   (bfd *, void *, const char *, asection *, flagword *);
    413 static bool coff_bad_format_hook
    414   (bfd *, void *);
    415 static void coff_set_custom_section_alignment
    416   (bfd *, asection *, const struct coff_section_alignment_entry *,
    417    const unsigned int);
    418 static bool coff_new_section_hook
    419   (bfd *, asection *);
    420 static bool coff_set_arch_mach_hook
    421   (bfd *, void *);
    422 static bool coff_write_relocs
    423   (bfd *, int);
    424 static bool coff_set_flags
    425   (bfd *, unsigned int *, unsigned short *);
    426 static bool coff_set_arch_mach
    427   (bfd *, enum bfd_architecture, unsigned long) ATTRIBUTE_UNUSED;
    428 static bool coff_compute_section_file_positions
    429   (bfd *);
    430 static bool coff_write_object_contents
    431   (bfd *) ATTRIBUTE_UNUSED;
    432 static bool coff_set_section_contents
    433   (bfd *, asection *, const void *, file_ptr, bfd_size_type);
    434 static bool coff_slurp_line_table
    435   (bfd *, asection *);
    436 static bool coff_slurp_symbol_table
    437   (bfd *);
    438 static enum coff_symbol_classification coff_classify_symbol
    439   (bfd *, struct internal_syment *);
    440 static bool coff_slurp_reloc_table
    441   (bfd *, asection *, asymbol **);
    442 static long coff_canonicalize_reloc
    443   (bfd *, asection *, arelent **, asymbol **);
    444 #ifndef coff_mkobject_hook
    445 static void * coff_mkobject_hook
    446   (bfd *, void *,  void *);
    447 #endif
    448 #ifdef COFF_WITH_PE
    449 static flagword handle_COMDAT
    450   (bfd *, flagword, void *, const char *, asection *);
    451 #endif
    452 #ifdef TICOFF
    453 static bool ticoff0_bad_format_hook
    454   (bfd *, void * );
    455 static bool ticoff1_bad_format_hook
    456   (bfd *, void * );
    457 #endif
    458 
    459 /* void warning(); */
    461 
    462 #if defined (COFF_LONG_SECTION_NAMES)
    463 static bool
    464 bfd_coff_set_long_section_names_allowed (bfd *abfd, int enable)
    465 {
    466   coff_backend_info (abfd)->_bfd_coff_long_section_names = enable;
    467   return true;
    468 }
    469 #else /* !defined (COFF_LONG_SECTION_NAMES) */
    470 static bool
    471 bfd_coff_set_long_section_names_disallowed (bfd *abfd, int enable)
    472 {
    473   (void) abfd;
    474   (void) enable;
    475   return false;
    476 }
    477 #endif /* defined (COFF_LONG_SECTION_NAMES) */
    478 
    479 /* Return a word with STYP_* (scnhdr.s_flags) flags set to represent
    480    the incoming SEC_* flags.  The inverse of this function is
    481    styp_to_sec_flags().  NOTE: If you add to/change this routine, you
    482    should probably mirror the changes in styp_to_sec_flags().  */
    483 
    484 #ifndef COFF_WITH_PE
    485 
    486 /* Macros for setting debugging flags.  */
    487 
    488 #ifdef STYP_DEBUG
    489 #define STYP_XCOFF_DEBUG STYP_DEBUG
    490 #else
    491 #define STYP_XCOFF_DEBUG STYP_INFO
    492 #endif
    493 
    494 #ifdef COFF_ALIGN_IN_S_FLAGS
    495 #define STYP_DEBUG_INFO STYP_DSECT
    496 #else
    497 #define STYP_DEBUG_INFO STYP_INFO
    498 #endif
    499 
    500 static long
    501 sec_to_styp_flags (const char *sec_name, flagword sec_flags)
    502 {
    503   long styp_flags = 0;
    504 
    505   if (!strcmp (sec_name, _TEXT))
    506     {
    507       styp_flags = STYP_TEXT;
    508     }
    509   else if (!strcmp (sec_name, _DATA))
    510     {
    511       styp_flags = STYP_DATA;
    512     }
    513   else if (!strcmp (sec_name, _BSS))
    514     {
    515       styp_flags = STYP_BSS;
    516 #ifdef _COMMENT
    517     }
    518   else if (!strcmp (sec_name, _COMMENT))
    519     {
    520       styp_flags = STYP_INFO;
    521 #endif /* _COMMENT */
    522 #ifdef _LIB
    523     }
    524   else if (!strcmp (sec_name, _LIB))
    525     {
    526       styp_flags = STYP_LIB;
    527 #endif /* _LIB */
    528 #ifdef _LIT
    529     }
    530   else if (!strcmp (sec_name, _LIT))
    531     {
    532       styp_flags = STYP_LIT;
    533 #endif /* _LIT */
    534     }
    535   else if (startswith (sec_name, DOT_DEBUG)
    536 	   || startswith (sec_name, DOT_ZDEBUG))
    537     {
    538       /* Handle the XCOFF debug section and DWARF2 debug sections.  */
    539       if (!sec_name[6])
    540 	styp_flags = STYP_XCOFF_DEBUG;
    541       else
    542 	styp_flags = STYP_DEBUG_INFO;
    543     }
    544   else if (startswith (sec_name, ".stab"))
    545     {
    546       styp_flags = STYP_DEBUG_INFO;
    547     }
    548 #ifdef COFF_LONG_SECTION_NAMES
    549   else if (startswith (sec_name, GNU_LINKONCE_WI)
    550 	   || startswith (sec_name, GNU_LINKONCE_WT))
    551     {
    552       styp_flags = STYP_DEBUG_INFO;
    553     }
    554 #endif
    555 #ifdef RS6000COFF_C
    556   else if (!strcmp (sec_name, _TDATA))
    557     {
    558       styp_flags = STYP_TDATA;
    559     }
    560   else if (!strcmp (sec_name, _TBSS))
    561     {
    562       styp_flags = STYP_TBSS;
    563     }
    564   else if (!strcmp (sec_name, _PAD))
    565     {
    566       styp_flags = STYP_PAD;
    567     }
    568   else if (!strcmp (sec_name, _LOADER))
    569     {
    570       styp_flags = STYP_LOADER;
    571     }
    572   else if (!strcmp (sec_name, _EXCEPT))
    573     {
    574       styp_flags = STYP_EXCEPT;
    575     }
    576   else if (!strcmp (sec_name, _TYPCHK))
    577     {
    578       styp_flags = STYP_TYPCHK;
    579     }
    580   else if (sec_flags & SEC_DEBUGGING)
    581     {
    582       int i;
    583 
    584       for (i = 0; i < XCOFF_DWSECT_NBR_NAMES; i++)
    585 	if (!strcmp (sec_name, xcoff_dwsect_names[i].xcoff_name))
    586 	  {
    587 	    styp_flags = STYP_DWARF | xcoff_dwsect_names[i].flag;
    588 	    break;
    589 	  }
    590     }
    591 #endif
    592   /* Try and figure out what it should be */
    593   else if (sec_flags & SEC_CODE)
    594     {
    595       styp_flags = STYP_TEXT;
    596     }
    597   else if (sec_flags & SEC_DATA)
    598     {
    599       styp_flags = STYP_DATA;
    600     }
    601   else if (sec_flags & SEC_READONLY)
    602     {
    603 #ifdef STYP_LIT			/* 29k readonly text/data section */
    604       styp_flags = STYP_LIT;
    605 #else
    606       styp_flags = STYP_TEXT;
    607 #endif /* STYP_LIT */
    608     }
    609   else if (sec_flags & SEC_LOAD)
    610     {
    611       styp_flags = STYP_TEXT;
    612     }
    613   else if (sec_flags & SEC_ALLOC)
    614     {
    615       styp_flags = STYP_BSS;
    616     }
    617 
    618 #ifdef STYP_CLINK
    619   if (sec_flags & SEC_TIC54X_CLINK)
    620     styp_flags |= STYP_CLINK;
    621 #endif
    622 
    623 #ifdef STYP_BLOCK
    624   if (sec_flags & SEC_TIC54X_BLOCK)
    625     styp_flags |= STYP_BLOCK;
    626 #endif
    627 
    628 #ifdef STYP_NOLOAD
    629   if ((sec_flags & (SEC_NEVER_LOAD | SEC_COFF_SHARED_LIBRARY)) != 0)
    630     styp_flags |= STYP_NOLOAD;
    631 #endif
    632 
    633   return styp_flags;
    634 }
    635 
    636 #else /* COFF_WITH_PE */
    637 
    638 /* The PE version; see above for the general comments.  The non-PE
    639    case seems to be more guessing, and breaks PE format; specifically,
    640    .rdata is readonly, but it sure ain't text.  Really, all this
    641    should be set up properly in gas (or whatever assembler is in use),
    642    and honor whatever objcopy/strip, etc. sent us as input.  */
    643 
    644 static long
    645 sec_to_styp_flags (const char *sec_name, flagword sec_flags)
    646 {
    647   long styp_flags = 0;
    648   bool is_dbg = false;
    649 
    650   if (startswith (sec_name, DOT_DEBUG)
    651       || startswith (sec_name, DOT_ZDEBUG)
    652 #ifdef COFF_LONG_SECTION_NAMES
    653       || startswith (sec_name, GNU_LINKONCE_WI)
    654       || startswith (sec_name, GNU_LINKONCE_WT)
    655 #endif
    656       || startswith (sec_name, ".stab"))
    657     is_dbg = true;
    658 
    659   /* caution: there are at least three groups of symbols that have
    660      very similar bits and meanings: IMAGE_SCN*, SEC_*, and STYP_*.
    661      SEC_* are the BFD internal flags, used for generic BFD
    662      information.  STYP_* are the COFF section flags which appear in
    663      COFF files.  IMAGE_SCN_* are the PE section flags which appear in
    664      PE files.  The STYP_* flags and the IMAGE_SCN_* flags overlap,
    665      but there are more IMAGE_SCN_* flags.  */
    666 
    667   /* FIXME: There is no gas syntax to specify the debug section flag.  */
    668   if (is_dbg)
    669     {
    670       sec_flags &= (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD
    671 		    | SEC_LINK_DUPLICATES_SAME_CONTENTS
    672 		    | SEC_LINK_DUPLICATES_SAME_SIZE);
    673       sec_flags |= SEC_DEBUGGING | SEC_READONLY;
    674     }
    675 
    676   /* skip LOAD */
    677   /* READONLY later */
    678   /* skip RELOC */
    679   if ((sec_flags & SEC_CODE) != 0)
    680     styp_flags |= IMAGE_SCN_CNT_CODE;
    681   if ((sec_flags & (SEC_DATA | SEC_DEBUGGING)) != 0)
    682     styp_flags |= IMAGE_SCN_CNT_INITIALIZED_DATA;
    683   if ((sec_flags & SEC_ALLOC) != 0 && (sec_flags & SEC_LOAD) == 0)
    684     styp_flags |= IMAGE_SCN_CNT_UNINITIALIZED_DATA;  /* ==STYP_BSS */
    685   /* skip ROM */
    686   /* skip constRUCTOR */
    687   /* skip CONTENTS */
    688 #ifndef COFF_IMAGE_WITH_PE
    689   /* I don't think any of the IMAGE_SCN_LNK_* flags set below should be set
    690      when the output is PE. Only object files should have them, for the linker
    691      to consume.  */
    692   if ((sec_flags & SEC_IS_COMMON) != 0)
    693     styp_flags |= IMAGE_SCN_LNK_COMDAT;
    694 #endif
    695   if ((sec_flags & SEC_DEBUGGING) != 0)
    696     styp_flags |= IMAGE_SCN_MEM_DISCARDABLE;
    697   if ((sec_flags & (SEC_EXCLUDE | SEC_NEVER_LOAD)) != 0 && !is_dbg)
    698 #ifdef COFF_IMAGE_WITH_PE
    699     styp_flags |= IMAGE_SCN_MEM_DISCARDABLE;
    700 #else
    701     styp_flags |= IMAGE_SCN_LNK_REMOVE;
    702 #endif
    703   /* skip IN_MEMORY */
    704   /* skip SORT */
    705 #ifndef COFF_IMAGE_WITH_PE
    706   if (sec_flags & SEC_LINK_ONCE)
    707     styp_flags |= IMAGE_SCN_LNK_COMDAT;
    708   if ((sec_flags
    709        & (SEC_LINK_DUPLICATES_DISCARD | SEC_LINK_DUPLICATES_SAME_CONTENTS
    710 	  | SEC_LINK_DUPLICATES_SAME_SIZE)) != 0)
    711     styp_flags |= IMAGE_SCN_LNK_COMDAT;
    712 #endif
    713 
    714   /* skip LINKER_CREATED */
    715 
    716   if ((sec_flags & SEC_COFF_NOREAD) == 0)
    717     styp_flags |= IMAGE_SCN_MEM_READ;     /* Invert NOREAD for read.  */
    718   if ((sec_flags & SEC_READONLY) == 0)
    719     styp_flags |= IMAGE_SCN_MEM_WRITE;    /* Invert READONLY for write.  */
    720   if (sec_flags & SEC_CODE)
    721     styp_flags |= IMAGE_SCN_MEM_EXECUTE;  /* CODE->EXECUTE.  */
    722   if (sec_flags & SEC_COFF_SHARED)
    723     styp_flags |= IMAGE_SCN_MEM_SHARED;   /* Shared remains meaningful.  */
    724 
    725   return styp_flags;
    726 }
    727 
    728 #endif /* COFF_WITH_PE */
    729 
    730 /* Return a word with SEC_* flags set to represent the incoming STYP_*
    731    flags (from scnhdr.s_flags).  The inverse of this function is
    732    sec_to_styp_flags().  NOTE: If you add to/change this routine, you
    733    should probably mirror the changes in sec_to_styp_flags().  */
    734 
    735 #ifndef COFF_WITH_PE
    736 
    737 static bool
    738 styp_to_sec_flags (bfd *abfd,
    739 		   void * hdr,
    740 		   const char *name,
    741 		   asection *section ATTRIBUTE_UNUSED,
    742 		   flagword *flags_ptr)
    743 {
    744   struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
    745   unsigned long styp_flags = internal_s->s_flags;
    746   flagword sec_flags = 0;
    747 
    748 #ifdef STYP_BLOCK
    749   if (styp_flags & STYP_BLOCK)
    750     sec_flags |= SEC_TIC54X_BLOCK;
    751 #endif
    752 
    753 #ifdef STYP_CLINK
    754   if (styp_flags & STYP_CLINK)
    755     sec_flags |= SEC_TIC54X_CLINK;
    756 #endif
    757 
    758 #ifdef STYP_NOLOAD
    759   if (styp_flags & STYP_NOLOAD)
    760     sec_flags |= SEC_NEVER_LOAD;
    761 #endif /* STYP_NOLOAD */
    762 
    763   /* For 386 COFF, at least, an unloadable text or data section is
    764      actually a shared library section.  */
    765   if (styp_flags & STYP_TEXT)
    766     {
    767       if (sec_flags & SEC_NEVER_LOAD)
    768 	sec_flags |= SEC_CODE | SEC_COFF_SHARED_LIBRARY;
    769       else
    770 	sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
    771     }
    772   else if (styp_flags & STYP_DATA)
    773     {
    774       if (sec_flags & SEC_NEVER_LOAD)
    775 	sec_flags |= SEC_DATA | SEC_COFF_SHARED_LIBRARY;
    776       else
    777 	sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
    778     }
    779   else if (styp_flags & STYP_BSS)
    780     {
    781 #ifdef BSS_NOLOAD_IS_SHARED_LIBRARY
    782       if (sec_flags & SEC_NEVER_LOAD)
    783 	sec_flags |= SEC_ALLOC | SEC_COFF_SHARED_LIBRARY;
    784       else
    785 #endif
    786 	sec_flags |= SEC_ALLOC;
    787     }
    788   else if (styp_flags & STYP_INFO)
    789     {
    790       /* We mark these as SEC_DEBUGGING, but only if COFF_PAGE_SIZE is
    791 	 defined.  coff_compute_section_file_positions uses
    792 	 COFF_PAGE_SIZE to ensure that the low order bits of the
    793 	 section VMA and the file offset match.  If we don't know
    794 	 COFF_PAGE_SIZE, we can't ensure the correct correspondence,
    795 	 and demand page loading of the file will fail.  */
    796 #if defined (COFF_PAGE_SIZE) && !defined (COFF_ALIGN_IN_S_FLAGS)
    797       sec_flags |= SEC_DEBUGGING;
    798 #endif
    799     }
    800   else if (styp_flags & STYP_PAD)
    801     sec_flags = 0;
    802 #ifdef RS6000COFF_C
    803   else if (styp_flags & STYP_TDATA)
    804     {
    805       if (sec_flags & SEC_NEVER_LOAD)
    806 	sec_flags |= SEC_DATA | SEC_THREAD_LOCAL | SEC_COFF_SHARED_LIBRARY;
    807       else
    808 	sec_flags |= SEC_DATA | SEC_THREAD_LOCAL | SEC_LOAD | SEC_ALLOC;
    809     }
    810   else if (styp_flags & STYP_TBSS)
    811     {
    812 #ifdef BSS_NOLOAD_IS_SHARED_LIBRARY
    813       if (sec_flags & SEC_NEVER_LOAD)
    814 	sec_flags |= SEC_ALLOC | SEC_THREAD_LOCAL | SEC_COFF_SHARED_LIBRARY;
    815       else
    816 #endif
    817 	sec_flags |= SEC_ALLOC | SEC_THREAD_LOCAL;
    818     }
    819   else if (styp_flags & STYP_EXCEPT)
    820     sec_flags |= SEC_LOAD;
    821   else if (styp_flags & STYP_LOADER)
    822     sec_flags |= SEC_LOAD;
    823   else if (styp_flags & STYP_TYPCHK)
    824     sec_flags |= SEC_LOAD;
    825   else if (styp_flags & STYP_DWARF)
    826     sec_flags |= SEC_DEBUGGING;
    827 #endif
    828   else if (strcmp (name, _TEXT) == 0)
    829     {
    830       if (sec_flags & SEC_NEVER_LOAD)
    831 	sec_flags |= SEC_CODE | SEC_COFF_SHARED_LIBRARY;
    832       else
    833 	sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
    834     }
    835   else if (strcmp (name, _DATA) == 0)
    836     {
    837       if (sec_flags & SEC_NEVER_LOAD)
    838 	sec_flags |= SEC_DATA | SEC_COFF_SHARED_LIBRARY;
    839       else
    840 	sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
    841     }
    842   else if (strcmp (name, _BSS) == 0)
    843     {
    844 #ifdef BSS_NOLOAD_IS_SHARED_LIBRARY
    845       if (sec_flags & SEC_NEVER_LOAD)
    846 	sec_flags |= SEC_ALLOC | SEC_COFF_SHARED_LIBRARY;
    847       else
    848 #endif
    849 	sec_flags |= SEC_ALLOC;
    850     }
    851   else if (startswith (name, DOT_DEBUG)
    852 	   || startswith (name, DOT_ZDEBUG)
    853 #ifdef _COMMENT
    854 	   || strcmp (name, _COMMENT) == 0
    855 #endif
    856 #ifdef COFF_LONG_SECTION_NAMES
    857 	   || startswith (name, GNU_LINKONCE_WI)
    858 	   || startswith (name, GNU_LINKONCE_WT)
    859 #endif
    860 	   || startswith (name, ".stab"))
    861     {
    862 #ifdef COFF_PAGE_SIZE
    863       sec_flags |= SEC_DEBUGGING;
    864 #endif
    865     }
    866 #ifdef _LIB
    867   else if (strcmp (name, _LIB) == 0)
    868     ;
    869 #endif
    870 #ifdef _LIT
    871   else if (strcmp (name, _LIT) == 0)
    872     sec_flags = SEC_LOAD | SEC_ALLOC | SEC_READONLY;
    873 #endif
    874   else
    875     sec_flags |= SEC_ALLOC | SEC_LOAD;
    876 
    877 #ifdef STYP_LIT			/* A29k readonly text/data section type.  */
    878   if ((styp_flags & STYP_LIT) == STYP_LIT)
    879     sec_flags = (SEC_LOAD | SEC_ALLOC | SEC_READONLY);
    880 #endif /* STYP_LIT */
    881 
    882 #ifdef STYP_OTHER_LOAD		/* Other loaded sections.  */
    883   if (styp_flags & STYP_OTHER_LOAD)
    884     sec_flags = (SEC_LOAD | SEC_ALLOC);
    885 #endif /* STYP_SDATA */
    886 
    887   if ((bfd_applicable_section_flags (abfd) & SEC_SMALL_DATA) != 0
    888       && (startswith (name, ".sbss")
    889 	  || startswith (name, ".sdata")))
    890     sec_flags |= SEC_SMALL_DATA;
    891 
    892 #if defined (COFF_LONG_SECTION_NAMES) && defined (COFF_SUPPORT_GNU_LINKONCE)
    893   /* As a GNU extension, if the name begins with .gnu.linkonce, we
    894      only link a single copy of the section.  This is used to support
    895      g++.  g++ will emit each template expansion in its own section.
    896      The symbols will be defined as weak, so that multiple definitions
    897      are permitted.  The GNU linker extension is to actually discard
    898      all but one of the sections.  */
    899   if (startswith (name, ".gnu.linkonce"))
    900     sec_flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
    901 #endif
    902 
    903   if (flags_ptr == NULL)
    904     return false;
    905 
    906   * flags_ptr = sec_flags;
    907   return true;
    908 }
    909 
    910 #else /* COFF_WITH_PE */
    911 
    912 static flagword
    913 handle_COMDAT (bfd * abfd,
    914 	       flagword sec_flags,
    915 	       void * hdr,
    916 	       const char *name,
    917 	       asection *section)
    918 {
    919   struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
    920   bfd_byte *esymstart, *esym, *esymend;
    921   int seen_state = 0;
    922   char *target_name = NULL;
    923 
    924   sec_flags |= SEC_LINK_ONCE;
    925 
    926   /* Unfortunately, the PE format stores essential information in
    927      the symbol table, of all places.  We need to extract that
    928      information now, so that objdump and the linker will know how
    929      to handle the section without worrying about the symbols.  We
    930      can't call slurp_symtab, because the linker doesn't want the
    931      swapped symbols.  */
    932 
    933   /* COMDAT sections are special.  The first symbol is the section
    934      symbol, which tells what kind of COMDAT section it is.  The
    935      second symbol is the "comdat symbol" - the one with the
    936      unique name.  GNU uses the section symbol for the unique
    937      name; MS uses ".text" for every comdat section.  Sigh.  - DJ */
    938 
    939   /* This is not mirrored in sec_to_styp_flags(), but there
    940      doesn't seem to be a need to, either, and it would at best be
    941      rather messy.  */
    942 
    943   if (! _bfd_coff_get_external_symbols (abfd))
    944     return sec_flags;
    945 
    946   esymstart = esym = (bfd_byte *) obj_coff_external_syms (abfd);
    947   esymend = esym + obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd);
    948 
    949   while (esym < esymend)
    950     {
    951       struct internal_syment isym;
    952       char buf[SYMNMLEN + 1];
    953       const char *symname;
    954 
    955       bfd_coff_swap_sym_in (abfd, esym, & isym);
    956 
    957       BFD_ASSERT (sizeof (internal_s->s_name) <= SYMNMLEN);
    958 
    959       if (isym.n_scnum == section->target_index)
    960 	{
    961 	  /* According to the MSVC documentation, the first
    962 	     TWO entries with the section # are both of
    963 	     interest to us.  The first one is the "section
    964 	     symbol" (section name).  The second is the comdat
    965 	     symbol name.  Here, we've found the first
    966 	     qualifying entry; we distinguish it from the
    967 	     second with a state flag.
    968 
    969 	     In the case of gas-generated (at least until that
    970 	     is fixed) .o files, it isn't necessarily the
    971 	     second one.  It may be some other later symbol.
    972 
    973 	     Since gas also doesn't follow MS conventions and
    974 	     emits the section similar to .text$<name>, where
    975 	     <something> is the name we're looking for, we
    976 	     distinguish the two as follows:
    977 
    978 	     If the section name is simply a section name (no
    979 	     $) we presume it's MS-generated, and look at
    980 	     precisely the second symbol for the comdat name.
    981 	     If the section name has a $, we assume it's
    982 	     gas-generated, and look for <something> (whatever
    983 	     follows the $) as the comdat symbol.  */
    984 
    985 	  /* All 3 branches use this.  */
    986 	  symname = _bfd_coff_internal_syment_name (abfd, &isym, buf);
    987 
    988 	  /* PR 17512 file: 078-11867-0.004  */
    989 	  if (symname == NULL)
    990 	    {
    991 	      _bfd_error_handler (_("%pB: unable to load COMDAT section name"),
    992 				  abfd);
    993 	      break;
    994 	    }
    995 
    996 	  switch (seen_state)
    997 	    {
    998 	    case 0:
    999 	      {
   1000 		/* The first time we've seen the symbol.  */
   1001 		union internal_auxent aux;
   1002 
   1003 		/* If it isn't the stuff we're expecting, die;
   1004 		   The MS documentation is vague, but it
   1005 		   appears that the second entry serves BOTH
   1006 		   as the comdat symbol and the defining
   1007 		   symbol record (either C_STAT or C_EXT,
   1008 		   possibly with an aux entry with debug
   1009 		   information if it's a function.)  It
   1010 		   appears the only way to find the second one
   1011 		   is to count.  (On Intel, they appear to be
   1012 		   adjacent, but on Alpha, they have been
   1013 		   found separated.)
   1014 
   1015 		   Here, we think we've found the first one,
   1016 		   but there's some checking we can do to be
   1017 		   sure.  */
   1018 
   1019 		if (! ((isym.n_sclass == C_STAT
   1020 			|| isym.n_sclass == C_EXT)
   1021 		       && BTYPE (isym.n_type) == T_NULL
   1022 		       && isym.n_value == 0))
   1023 		  {
   1024 		    /* Malformed input files can trigger this test.
   1025 		       cf PR 21781.  */
   1026 		    _bfd_error_handler (_("%pB: error: unexpected symbol '%s' in COMDAT section"),
   1027 					abfd, symname);
   1028 		    goto breakloop;
   1029 		  }
   1030 
   1031 		/* FIXME LATER: MSVC generates section names
   1032 		   like .text for comdats.  Gas generates
   1033 		   names like .text$foo__Fv (in the case of a
   1034 		   function).  See comment above for more.  */
   1035 
   1036 		if (isym.n_sclass == C_STAT && strcmp (name, symname) != 0)
   1037 		  /* xgettext:c-format */
   1038 		  _bfd_error_handler (_("%pB: warning: COMDAT symbol '%s'"
   1039 					" does not match section name '%s'"),
   1040 				      abfd, symname, name);
   1041 
   1042 		seen_state = 1;
   1043 
   1044 		/* PR 17512: file: e2cfe54f.  */
   1045 		if (esym + bfd_coff_symesz (abfd) >= esymend)
   1046 		  {
   1047 		    /* xgettext:c-format */
   1048 		    _bfd_error_handler (_("%pB: warning: no symbol for"
   1049 					  " section '%s' found"),
   1050 					abfd, symname);
   1051 		    break;
   1052 		  }
   1053 		/* This is the section symbol.  */
   1054 		bfd_coff_swap_aux_in (abfd, (esym + bfd_coff_symesz (abfd)),
   1055 				      isym.n_type, isym.n_sclass,
   1056 				      0, isym.n_numaux, & aux);
   1057 
   1058 		target_name = strchr (name, '$');
   1059 		if (target_name != NULL)
   1060 		  {
   1061 		    /* Gas mode.  */
   1062 		    seen_state = 2;
   1063 		    /* Skip the `$'.  */
   1064 		    target_name += 1;
   1065 		  }
   1066 
   1067 		/* FIXME: Microsoft uses NODUPLICATES and
   1068 		   ASSOCIATIVE, but gnu uses ANY and
   1069 		   SAME_SIZE.  Unfortunately, gnu doesn't do
   1070 		   the comdat symbols right.  So, until we can
   1071 		   fix it to do the right thing, we are
   1072 		   temporarily disabling comdats for the MS
   1073 		   types (they're used in DLLs and C++, but we
   1074 		   don't support *their* C++ libraries anyway
   1075 		   - DJ.  */
   1076 
   1077 		/* Cygwin does not follow the MS style, and
   1078 		   uses ANY and SAME_SIZE where NODUPLICATES
   1079 		   and ASSOCIATIVE should be used.  For
   1080 		   Interix, we just do the right thing up
   1081 		   front.  */
   1082 
   1083 		switch (aux.x_scn.x_comdat)
   1084 		  {
   1085 		  case IMAGE_COMDAT_SELECT_NODUPLICATES:
   1086 #ifdef STRICT_PE_FORMAT
   1087 		    sec_flags |= SEC_LINK_DUPLICATES_ONE_ONLY;
   1088 #else
   1089 		    sec_flags &= ~SEC_LINK_ONCE;
   1090 #endif
   1091 		    break;
   1092 
   1093 		  case IMAGE_COMDAT_SELECT_ANY:
   1094 		    sec_flags |= SEC_LINK_DUPLICATES_DISCARD;
   1095 		    break;
   1096 
   1097 		  case IMAGE_COMDAT_SELECT_SAME_SIZE:
   1098 		    sec_flags |= SEC_LINK_DUPLICATES_SAME_SIZE;
   1099 		    break;
   1100 
   1101 		  case IMAGE_COMDAT_SELECT_EXACT_MATCH:
   1102 		    /* Not yet fully implemented ??? */
   1103 		    sec_flags |= SEC_LINK_DUPLICATES_SAME_CONTENTS;
   1104 		    break;
   1105 
   1106 		    /* debug$S gets this case; other
   1107 		       implications ??? */
   1108 
   1109 		    /* There may be no symbol... we'll search
   1110 		       the whole table... Is this the right
   1111 		       place to play this game? Or should we do
   1112 		       it when reading it in.  */
   1113 		  case IMAGE_COMDAT_SELECT_ASSOCIATIVE:
   1114 #ifdef STRICT_PE_FORMAT
   1115 		    /* FIXME: This is not currently implemented.  */
   1116 		    sec_flags |= SEC_LINK_DUPLICATES_DISCARD;
   1117 #else
   1118 		    sec_flags &= ~SEC_LINK_ONCE;
   1119 #endif
   1120 		    break;
   1121 
   1122 		  default:  /* 0 means "no symbol" */
   1123 		    /* debug$F gets this case; other
   1124 		       implications ??? */
   1125 		    sec_flags |= SEC_LINK_DUPLICATES_DISCARD;
   1126 		    break;
   1127 		  }
   1128 	      }
   1129 	      break;
   1130 
   1131 	    case 2:
   1132 	      /* Gas mode: the first matching on partial name.  */
   1133 
   1134 #ifndef TARGET_UNDERSCORE
   1135 #define TARGET_UNDERSCORE 0
   1136 #endif
   1137 	      /* Is this the name we're looking for ?  */
   1138 	      if (strcmp (target_name,
   1139 			  symname + (TARGET_UNDERSCORE ? 1 : 0)) != 0)
   1140 		{
   1141 		  /* Not the name we're looking for */
   1142 		  esym += (isym.n_numaux + 1) * bfd_coff_symesz (abfd);
   1143 		  continue;
   1144 		}
   1145 	      /* Fall through.  */
   1146 	    case 1:
   1147 	      /* MSVC mode: the lexically second symbol (or
   1148 		 drop through from the above).  */
   1149 	      {
   1150 		char *newname;
   1151 		size_t amt;
   1152 
   1153 		/* This must the second symbol with the
   1154 		   section #.  It is the actual symbol name.
   1155 		   Intel puts the two adjacent, but Alpha (at
   1156 		   least) spreads them out.  */
   1157 
   1158 		amt = sizeof (struct coff_comdat_info);
   1159 		coff_section_data (abfd, section)->comdat
   1160 		  = (struct coff_comdat_info *) bfd_alloc (abfd, amt);
   1161 		if (coff_section_data (abfd, section)->comdat == NULL)
   1162 		  abort ();
   1163 
   1164 		coff_section_data (abfd, section)->comdat->symbol =
   1165 		  (esym - esymstart) / bfd_coff_symesz (abfd);
   1166 
   1167 		amt = strlen (symname) + 1;
   1168 		newname = (char *) bfd_alloc (abfd, amt);
   1169 		if (newname == NULL)
   1170 		  abort ();
   1171 
   1172 		strcpy (newname, symname);
   1173 		coff_section_data (abfd, section)->comdat->name
   1174 		  = newname;
   1175 	      }
   1176 
   1177 	      goto breakloop;
   1178 	    }
   1179 	}
   1180 
   1181       esym += (isym.n_numaux + 1) * bfd_coff_symesz (abfd);
   1182     }
   1183 
   1184  breakloop:
   1185   return sec_flags;
   1186 }
   1187 
   1188 
   1189 /* The PE version; see above for the general comments.
   1190 
   1191    Since to set the SEC_LINK_ONCE and associated flags, we have to
   1192    look at the symbol table anyway, we return the symbol table index
   1193    of the symbol being used as the COMDAT symbol.  This is admittedly
   1194    ugly, but there's really nowhere else that we have access to the
   1195    required information.  FIXME: Is the COMDAT symbol index used for
   1196    any purpose other than objdump?  */
   1197 
   1198 static bool
   1199 styp_to_sec_flags (bfd *abfd,
   1200 		   void * hdr,
   1201 		   const char *name,
   1202 		   asection *section,
   1203 		   flagword *flags_ptr)
   1204 {
   1205   struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
   1206   unsigned long styp_flags = internal_s->s_flags;
   1207   flagword sec_flags;
   1208   bool result = true;
   1209   bool is_dbg = false;
   1210 
   1211   if (startswith (name, DOT_DEBUG)
   1212       || startswith (name, DOT_ZDEBUG)
   1213 #ifdef COFF_LONG_SECTION_NAMES
   1214       || startswith (name, GNU_LINKONCE_WI)
   1215       || startswith (name, GNU_LINKONCE_WT)
   1216       /* FIXME: These definitions ought to be in a header file.  */
   1217 #define GNU_DEBUGLINK		".gnu_debuglink"
   1218 #define GNU_DEBUGALTLINK	".gnu_debugaltlink"
   1219       || startswith (name, GNU_DEBUGLINK)
   1220       || startswith (name, GNU_DEBUGALTLINK)
   1221 #endif
   1222       || startswith (name, ".stab"))
   1223     is_dbg = true;
   1224   /* Assume read only unless IMAGE_SCN_MEM_WRITE is specified.  */
   1225   sec_flags = SEC_READONLY;
   1226 
   1227   /* If section disallows read, then set the NOREAD flag. */
   1228   if ((styp_flags & IMAGE_SCN_MEM_READ) == 0)
   1229     sec_flags |= SEC_COFF_NOREAD;
   1230 
   1231   /* Process each flag bit in styp_flags in turn.  */
   1232   while (styp_flags)
   1233     {
   1234       unsigned long flag = styp_flags & - styp_flags;
   1235       char * unhandled = NULL;
   1236 
   1237       styp_flags &= ~ flag;
   1238 
   1239       /* We infer from the distinct read/write/execute bits the settings
   1240 	 of some of the bfd flags; the actual values, should we need them,
   1241 	 are also in pei_section_data (abfd, section)->pe_flags.  */
   1242 
   1243       switch (flag)
   1244 	{
   1245 	case STYP_DSECT:
   1246 	  unhandled = "STYP_DSECT";
   1247 	  break;
   1248 	case STYP_GROUP:
   1249 	  unhandled = "STYP_GROUP";
   1250 	  break;
   1251 	case STYP_COPY:
   1252 	  unhandled = "STYP_COPY";
   1253 	  break;
   1254 	case STYP_OVER:
   1255 	  unhandled = "STYP_OVER";
   1256 	  break;
   1257 #ifdef SEC_NEVER_LOAD
   1258 	case STYP_NOLOAD:
   1259 	  sec_flags |= SEC_NEVER_LOAD;
   1260 	  break;
   1261 #endif
   1262 	case IMAGE_SCN_MEM_READ:
   1263 	  sec_flags &= ~SEC_COFF_NOREAD;
   1264 	  break;
   1265 	case IMAGE_SCN_TYPE_NO_PAD:
   1266 	  /* Skip.  */
   1267 	  break;
   1268 	case IMAGE_SCN_LNK_OTHER:
   1269 	  unhandled = "IMAGE_SCN_LNK_OTHER";
   1270 	  break;
   1271 	case IMAGE_SCN_MEM_NOT_CACHED:
   1272 	  unhandled = "IMAGE_SCN_MEM_NOT_CACHED";
   1273 	  break;
   1274 	case IMAGE_SCN_MEM_NOT_PAGED:
   1275 	  /* Generate a warning message rather using the 'unhandled'
   1276 	     variable as this will allow some .sys files generate by
   1277 	     other toolchains to be processed.  See bugzilla issue 196.  */
   1278 	  /* xgettext:c-format */
   1279 	  _bfd_error_handler (_("%pB: warning: ignoring section flag"
   1280 				" %s in section %s"),
   1281 			      abfd, "IMAGE_SCN_MEM_NOT_PAGED", name);
   1282 	  break;
   1283 	case IMAGE_SCN_MEM_EXECUTE:
   1284 	  sec_flags |= SEC_CODE;
   1285 	  break;
   1286 	case IMAGE_SCN_MEM_WRITE:
   1287 	  sec_flags &= ~ SEC_READONLY;
   1288 	  break;
   1289 	case IMAGE_SCN_MEM_DISCARDABLE:
   1290 	  /* The MS PE spec says that debug sections are DISCARDABLE,
   1291 	     but the presence of a DISCARDABLE flag does not necessarily
   1292 	     mean that a given section contains debug information.  Thus
   1293 	     we only set the SEC_DEBUGGING flag on sections that we
   1294 	     recognise as containing debug information.  */
   1295 	     if (is_dbg
   1296 #ifdef _COMMENT
   1297 	      || strcmp (name, _COMMENT) == 0
   1298 #endif
   1299 	      )
   1300 	    {
   1301 	      sec_flags |= SEC_DEBUGGING | SEC_READONLY;
   1302 	    }
   1303 	  break;
   1304 	case IMAGE_SCN_MEM_SHARED:
   1305 	  sec_flags |= SEC_COFF_SHARED;
   1306 	  break;
   1307 	case IMAGE_SCN_LNK_REMOVE:
   1308 	  if (!is_dbg)
   1309 	    sec_flags |= SEC_EXCLUDE;
   1310 	  break;
   1311 	case IMAGE_SCN_CNT_CODE:
   1312 	  sec_flags |= SEC_CODE | SEC_ALLOC | SEC_LOAD;
   1313 	  break;
   1314 	case IMAGE_SCN_CNT_INITIALIZED_DATA:
   1315 	  if (is_dbg)
   1316 	    sec_flags |= SEC_DEBUGGING;
   1317 	  else
   1318 	    sec_flags |= SEC_DATA | SEC_ALLOC | SEC_LOAD;
   1319 	  break;
   1320 	case IMAGE_SCN_CNT_UNINITIALIZED_DATA:
   1321 	  sec_flags |= SEC_ALLOC;
   1322 	  break;
   1323 	case IMAGE_SCN_LNK_INFO:
   1324 	  /* We mark these as SEC_DEBUGGING, but only if COFF_PAGE_SIZE is
   1325 	     defined.  coff_compute_section_file_positions uses
   1326 	     COFF_PAGE_SIZE to ensure that the low order bits of the
   1327 	     section VMA and the file offset match.  If we don't know
   1328 	     COFF_PAGE_SIZE, we can't ensure the correct correspondence,
   1329 	     and demand page loading of the file will fail.  */
   1330 #ifdef COFF_PAGE_SIZE
   1331 	  sec_flags |= SEC_DEBUGGING;
   1332 #endif
   1333 	  break;
   1334 	case IMAGE_SCN_LNK_COMDAT:
   1335 	  /* COMDAT gets very special treatment.  */
   1336 	  sec_flags = handle_COMDAT (abfd, sec_flags, hdr, name, section);
   1337 	  break;
   1338 	default:
   1339 	  /* Silently ignore for now.  */
   1340 	  break;
   1341 	}
   1342 
   1343       /* If the section flag was not handled, report it here.  */
   1344       if (unhandled != NULL)
   1345 	{
   1346 	  _bfd_error_handler
   1347 	    /* xgettext:c-format */
   1348 	    (_("%pB (%s): section flag %s (%#lx) ignored"),
   1349 	     abfd, name, unhandled, flag);
   1350 	  result = false;
   1351 	}
   1352     }
   1353 
   1354   if ((bfd_applicable_section_flags (abfd) & SEC_SMALL_DATA) != 0
   1355       && (startswith (name, ".sbss")
   1356 	  || startswith (name, ".sdata")))
   1357     sec_flags |= SEC_SMALL_DATA;
   1358 
   1359 #if defined (COFF_LONG_SECTION_NAMES) && defined (COFF_SUPPORT_GNU_LINKONCE)
   1360   /* As a GNU extension, if the name begins with .gnu.linkonce, we
   1361      only link a single copy of the section.  This is used to support
   1362      g++.  g++ will emit each template expansion in its own section.
   1363      The symbols will be defined as weak, so that multiple definitions
   1364      are permitted.  The GNU linker extension is to actually discard
   1365      all but one of the sections.  */
   1366   if (startswith (name, ".gnu.linkonce"))
   1367     sec_flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
   1368 #endif
   1369 
   1370   if (flags_ptr)
   1371     * flags_ptr = sec_flags;
   1372 
   1373   return result;
   1374 }
   1375 
   1376 #endif /* COFF_WITH_PE */
   1377 
   1378 #define	get_index(symbol)	((symbol)->udata.i)
   1379 
   1380 /*
   1381 INTERNAL_DEFINITION
   1382 	bfd_coff_backend_data
   1383 
   1384 CODE_FRAGMENT
   1385 
   1386 .{* COFF symbol classifications.  *}
   1387 .
   1388 .enum coff_symbol_classification
   1389 .{
   1390 .  {* Global symbol.  *}
   1391 .  COFF_SYMBOL_GLOBAL,
   1392 .  {* Common symbol.  *}
   1393 .  COFF_SYMBOL_COMMON,
   1394 .  {* Undefined symbol.  *}
   1395 .  COFF_SYMBOL_UNDEFINED,
   1396 .  {* Local symbol.  *}
   1397 .  COFF_SYMBOL_LOCAL,
   1398 .  {* PE section symbol.  *}
   1399 .  COFF_SYMBOL_PE_SECTION
   1400 .};
   1401 .
   1402 .typedef asection * (*coff_gc_mark_hook_fn)
   1403 .  (asection *, struct bfd_link_info *, struct internal_reloc *,
   1404 .   struct coff_link_hash_entry *, struct internal_syment *);
   1405 .
   1406 Special entry points for gdb to swap in coff symbol table parts:
   1407 .typedef struct
   1408 .{
   1409 .  void (*_bfd_coff_swap_aux_in)
   1410 .    (bfd *, void *, int, int, int, int, void *);
   1411 .
   1412 .  void (*_bfd_coff_swap_sym_in)
   1413 .    (bfd *, void *, void *);
   1414 .
   1415 .  void (*_bfd_coff_swap_lineno_in)
   1416 .    (bfd *, void *, void *);
   1417 .
   1418 .  unsigned int (*_bfd_coff_swap_aux_out)
   1419 .    (bfd *, void *, int, int, int, int, void *);
   1420 .
   1421 .  unsigned int (*_bfd_coff_swap_sym_out)
   1422 .    (bfd *, void *, void *);
   1423 .
   1424 .  unsigned int (*_bfd_coff_swap_lineno_out)
   1425 .    (bfd *, void *, void *);
   1426 .
   1427 .  unsigned int (*_bfd_coff_swap_reloc_out)
   1428 .    (bfd *, void *, void *);
   1429 .
   1430 .  unsigned int (*_bfd_coff_swap_filehdr_out)
   1431 .    (bfd *, void *, void *);
   1432 .
   1433 .  unsigned int (*_bfd_coff_swap_aouthdr_out)
   1434 .    (bfd *, void *, void *);
   1435 .
   1436 .  unsigned int (*_bfd_coff_swap_scnhdr_out)
   1437 .    (bfd *, void *, void *);
   1438 .
   1439 .  unsigned int _bfd_filhsz;
   1440 .  unsigned int _bfd_aoutsz;
   1441 .  unsigned int _bfd_scnhsz;
   1442 .  unsigned int _bfd_symesz;
   1443 .  unsigned int _bfd_auxesz;
   1444 .  unsigned int _bfd_relsz;
   1445 .  unsigned int _bfd_linesz;
   1446 .  unsigned int _bfd_filnmlen;
   1447 .  bool _bfd_coff_long_filenames;
   1448 .
   1449 .  bool _bfd_coff_long_section_names;
   1450 .  bool (*_bfd_coff_set_long_section_names)
   1451 .    (bfd *, int);
   1452 .
   1453 .  unsigned int _bfd_coff_default_section_alignment_power;
   1454 .  bool _bfd_coff_force_symnames_in_strings;
   1455 .  unsigned int _bfd_coff_debug_string_prefix_length;
   1456 .  unsigned int _bfd_coff_max_nscns;
   1457 .
   1458 .  void (*_bfd_coff_swap_filehdr_in)
   1459 .    (bfd *, void *, void *);
   1460 .
   1461 .  void (*_bfd_coff_swap_aouthdr_in)
   1462 .    (bfd *, void *, void *);
   1463 .
   1464 .  void (*_bfd_coff_swap_scnhdr_in)
   1465 .    (bfd *, void *, void *);
   1466 .
   1467 .  void (*_bfd_coff_swap_reloc_in)
   1468 .    (bfd *abfd, void *, void *);
   1469 .
   1470 .  bool (*_bfd_coff_bad_format_hook)
   1471 .    (bfd *, void *);
   1472 .
   1473 .  bool (*_bfd_coff_set_arch_mach_hook)
   1474 .    (bfd *, void *);
   1475 .
   1476 .  void * (*_bfd_coff_mkobject_hook)
   1477 .    (bfd *, void *, void *);
   1478 .
   1479 .  bool (*_bfd_styp_to_sec_flags_hook)
   1480 .    (bfd *, void *, const char *, asection *, flagword *);
   1481 .
   1482 .  void (*_bfd_set_alignment_hook)
   1483 .    (bfd *, asection *, void *);
   1484 .
   1485 .  bool (*_bfd_coff_slurp_symbol_table)
   1486 .    (bfd *);
   1487 .
   1488 .  bool (*_bfd_coff_symname_in_debug)
   1489 .    (bfd *, struct internal_syment *);
   1490 .
   1491 .  bool (*_bfd_coff_pointerize_aux_hook)
   1492 .    (bfd *, combined_entry_type *, combined_entry_type *,
   1493 .     unsigned int, combined_entry_type *);
   1494 .
   1495 .  bool (*_bfd_coff_print_aux)
   1496 .    (bfd *, FILE *, combined_entry_type *, combined_entry_type *,
   1497 .     combined_entry_type *, unsigned int);
   1498 .
   1499 .  void (*_bfd_coff_reloc16_extra_cases)
   1500 .    (bfd *, struct bfd_link_info *, struct bfd_link_order *, arelent *,
   1501 .     bfd_byte *, unsigned int *, unsigned int *);
   1502 .
   1503 .  int (*_bfd_coff_reloc16_estimate)
   1504 .    (bfd *, asection *, arelent *, unsigned int,
   1505 .     struct bfd_link_info *);
   1506 .
   1507 .  enum coff_symbol_classification (*_bfd_coff_classify_symbol)
   1508 .    (bfd *, struct internal_syment *);
   1509 .
   1510 .  bool (*_bfd_coff_compute_section_file_positions)
   1511 .    (bfd *);
   1512 .
   1513 .  bool (*_bfd_coff_start_final_link)
   1514 .    (bfd *, struct bfd_link_info *);
   1515 .
   1516 .  bool (*_bfd_coff_relocate_section)
   1517 .    (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
   1518 .     struct internal_reloc *, struct internal_syment *, asection **);
   1519 .
   1520 .  reloc_howto_type *(*_bfd_coff_rtype_to_howto)
   1521 .    (bfd *, asection *, struct internal_reloc *,
   1522 .     struct coff_link_hash_entry *, struct internal_syment *, bfd_vma *);
   1523 .
   1524 .  bool (*_bfd_coff_adjust_symndx)
   1525 .    (bfd *, struct bfd_link_info *, bfd *, asection *,
   1526 .     struct internal_reloc *, bool *);
   1527 .
   1528 .  bool (*_bfd_coff_link_add_one_symbol)
   1529 .    (struct bfd_link_info *, bfd *, const char *, flagword,
   1530 .     asection *, bfd_vma, const char *, bool, bool,
   1531 .     struct bfd_link_hash_entry **);
   1532 .
   1533 .  bool (*_bfd_coff_link_output_has_begun)
   1534 .    (bfd *, struct coff_final_link_info *);
   1535 .
   1536 .  bool (*_bfd_coff_final_link_postscript)
   1537 .    (bfd *, struct coff_final_link_info *);
   1538 .
   1539 .  bool (*_bfd_coff_print_pdata)
   1540 .    (bfd *, void *);
   1541 .
   1542 .} bfd_coff_backend_data;
   1543 .
   1544 .#define coff_backend_info(abfd) \
   1545 .  ((bfd_coff_backend_data *) (abfd)->xvec->backend_data)
   1546 .
   1547 .#define bfd_coff_swap_aux_in(a,e,t,c,ind,num,i) \
   1548 .  ((coff_backend_info (a)->_bfd_coff_swap_aux_in) (a,e,t,c,ind,num,i))
   1549 .
   1550 .#define bfd_coff_swap_sym_in(a,e,i) \
   1551 .  ((coff_backend_info (a)->_bfd_coff_swap_sym_in) (a,e,i))
   1552 .
   1553 .#define bfd_coff_swap_lineno_in(a,e,i) \
   1554 .  ((coff_backend_info ( a)->_bfd_coff_swap_lineno_in) (a,e,i))
   1555 .
   1556 .#define bfd_coff_swap_reloc_out(abfd, i, o) \
   1557 .  ((coff_backend_info (abfd)->_bfd_coff_swap_reloc_out) (abfd, i, o))
   1558 .
   1559 .#define bfd_coff_swap_lineno_out(abfd, i, o) \
   1560 .  ((coff_backend_info (abfd)->_bfd_coff_swap_lineno_out) (abfd, i, o))
   1561 .
   1562 .#define bfd_coff_swap_aux_out(a,i,t,c,ind,num,o) \
   1563 .  ((coff_backend_info (a)->_bfd_coff_swap_aux_out) (a,i,t,c,ind,num,o))
   1564 .
   1565 .#define bfd_coff_swap_sym_out(abfd, i,o) \
   1566 .  ((coff_backend_info (abfd)->_bfd_coff_swap_sym_out) (abfd, i, o))
   1567 .
   1568 .#define bfd_coff_swap_scnhdr_out(abfd, i,o) \
   1569 .  ((coff_backend_info (abfd)->_bfd_coff_swap_scnhdr_out) (abfd, i, o))
   1570 .
   1571 .#define bfd_coff_swap_filehdr_out(abfd, i,o) \
   1572 .  ((coff_backend_info (abfd)->_bfd_coff_swap_filehdr_out) (abfd, i, o))
   1573 .
   1574 .#define bfd_coff_swap_aouthdr_out(abfd, i,o) \
   1575 .  ((coff_backend_info (abfd)->_bfd_coff_swap_aouthdr_out) (abfd, i, o))
   1576 .
   1577 .#define bfd_coff_filhsz(abfd) (coff_backend_info (abfd)->_bfd_filhsz)
   1578 .#define bfd_coff_aoutsz(abfd) (coff_backend_info (abfd)->_bfd_aoutsz)
   1579 .#define bfd_coff_scnhsz(abfd) (coff_backend_info (abfd)->_bfd_scnhsz)
   1580 .#define bfd_coff_symesz(abfd) (coff_backend_info (abfd)->_bfd_symesz)
   1581 .#define bfd_coff_auxesz(abfd) (coff_backend_info (abfd)->_bfd_auxesz)
   1582 .#define bfd_coff_relsz(abfd)  (coff_backend_info (abfd)->_bfd_relsz)
   1583 .#define bfd_coff_linesz(abfd) (coff_backend_info (abfd)->_bfd_linesz)
   1584 .#define bfd_coff_filnmlen(abfd) (coff_backend_info (abfd)->_bfd_filnmlen)
   1585 .#define bfd_coff_long_filenames(abfd) \
   1586 .  (coff_backend_info (abfd)->_bfd_coff_long_filenames)
   1587 .#define bfd_coff_long_section_names(abfd) \
   1588 .  (coff_backend_info (abfd)->_bfd_coff_long_section_names)
   1589 .#define bfd_coff_set_long_section_names(abfd, enable) \
   1590 .  ((coff_backend_info (abfd)->_bfd_coff_set_long_section_names) (abfd, enable))
   1591 .#define bfd_coff_default_section_alignment_power(abfd) \
   1592 .  (coff_backend_info (abfd)->_bfd_coff_default_section_alignment_power)
   1593 .#define bfd_coff_max_nscns(abfd) \
   1594 .  (coff_backend_info (abfd)->_bfd_coff_max_nscns)
   1595 .
   1596 .#define bfd_coff_swap_filehdr_in(abfd, i,o) \
   1597 .  ((coff_backend_info (abfd)->_bfd_coff_swap_filehdr_in) (abfd, i, o))
   1598 .
   1599 .#define bfd_coff_swap_aouthdr_in(abfd, i,o) \
   1600 .  ((coff_backend_info (abfd)->_bfd_coff_swap_aouthdr_in) (abfd, i, o))
   1601 .
   1602 .#define bfd_coff_swap_scnhdr_in(abfd, i,o) \
   1603 .  ((coff_backend_info (abfd)->_bfd_coff_swap_scnhdr_in) (abfd, i, o))
   1604 .
   1605 .#define bfd_coff_swap_reloc_in(abfd, i, o) \
   1606 .  ((coff_backend_info (abfd)->_bfd_coff_swap_reloc_in) (abfd, i, o))
   1607 .
   1608 .#define bfd_coff_bad_format_hook(abfd, filehdr) \
   1609 .  ((coff_backend_info (abfd)->_bfd_coff_bad_format_hook) (abfd, filehdr))
   1610 .
   1611 .#define bfd_coff_set_arch_mach_hook(abfd, filehdr)\
   1612 .  ((coff_backend_info (abfd)->_bfd_coff_set_arch_mach_hook) (abfd, filehdr))
   1613 .#define bfd_coff_mkobject_hook(abfd, filehdr, aouthdr)\
   1614 .  ((coff_backend_info (abfd)->_bfd_coff_mkobject_hook)\
   1615 .   (abfd, filehdr, aouthdr))
   1616 .
   1617 .#define bfd_coff_styp_to_sec_flags_hook(abfd, scnhdr, name, section, flags_ptr)\
   1618 .  ((coff_backend_info (abfd)->_bfd_styp_to_sec_flags_hook)\
   1619 .   (abfd, scnhdr, name, section, flags_ptr))
   1620 .
   1621 .#define bfd_coff_set_alignment_hook(abfd, sec, scnhdr)\
   1622 .  ((coff_backend_info (abfd)->_bfd_set_alignment_hook) (abfd, sec, scnhdr))
   1623 .
   1624 .#define bfd_coff_slurp_symbol_table(abfd)\
   1625 .  ((coff_backend_info (abfd)->_bfd_coff_slurp_symbol_table) (abfd))
   1626 .
   1627 .#define bfd_coff_symname_in_debug(abfd, sym)\
   1628 .  ((coff_backend_info (abfd)->_bfd_coff_symname_in_debug) (abfd, sym))
   1629 .
   1630 .#define bfd_coff_force_symnames_in_strings(abfd)\
   1631 .  (coff_backend_info (abfd)->_bfd_coff_force_symnames_in_strings)
   1632 .
   1633 .#define bfd_coff_debug_string_prefix_length(abfd)\
   1634 .  (coff_backend_info (abfd)->_bfd_coff_debug_string_prefix_length)
   1635 .
   1636 .#define bfd_coff_print_aux(abfd, file, base, symbol, aux, indaux)\
   1637 .  ((coff_backend_info (abfd)->_bfd_coff_print_aux)\
   1638 .   (abfd, file, base, symbol, aux, indaux))
   1639 .
   1640 .#define bfd_coff_reloc16_extra_cases(abfd, link_info, link_order,\
   1641 .				      reloc, data, src_ptr, dst_ptr)\
   1642 .  ((coff_backend_info (abfd)->_bfd_coff_reloc16_extra_cases)\
   1643 .   (abfd, link_info, link_order, reloc, data, src_ptr, dst_ptr))
   1644 .
   1645 .#define bfd_coff_reloc16_estimate(abfd, section, reloc, shrink, link_info)\
   1646 .  ((coff_backend_info (abfd)->_bfd_coff_reloc16_estimate)\
   1647 .   (abfd, section, reloc, shrink, link_info))
   1648 .
   1649 .#define bfd_coff_classify_symbol(abfd, sym)\
   1650 .  ((coff_backend_info (abfd)->_bfd_coff_classify_symbol)\
   1651 .   (abfd, sym))
   1652 .
   1653 .#define bfd_coff_compute_section_file_positions(abfd)\
   1654 .  ((coff_backend_info (abfd)->_bfd_coff_compute_section_file_positions)\
   1655 .   (abfd))
   1656 .
   1657 .#define bfd_coff_start_final_link(obfd, info)\
   1658 .  ((coff_backend_info (obfd)->_bfd_coff_start_final_link)\
   1659 .   (obfd, info))
   1660 .#define bfd_coff_relocate_section(obfd,info,ibfd,o,con,rel,isyms,secs)\
   1661 .  ((coff_backend_info (ibfd)->_bfd_coff_relocate_section)\
   1662 .   (obfd, info, ibfd, o, con, rel, isyms, secs))
   1663 .#define bfd_coff_rtype_to_howto(abfd, sec, rel, h, sym, addendp)\
   1664 .  ((coff_backend_info (abfd)->_bfd_coff_rtype_to_howto)\
   1665 .   (abfd, sec, rel, h, sym, addendp))
   1666 .#define bfd_coff_adjust_symndx(obfd, info, ibfd, sec, rel, adjustedp)\
   1667 .  ((coff_backend_info (abfd)->_bfd_coff_adjust_symndx)\
   1668 .   (obfd, info, ibfd, sec, rel, adjustedp))
   1669 .#define bfd_coff_link_add_one_symbol(info, abfd, name, flags, section,\
   1670 .				      value, string, cp, coll, hashp)\
   1671 .  ((coff_backend_info (abfd)->_bfd_coff_link_add_one_symbol)\
   1672 .   (info, abfd, name, flags, section, value, string, cp, coll, hashp))
   1673 .
   1674 .#define bfd_coff_link_output_has_begun(a,p) \
   1675 .  ((coff_backend_info (a)->_bfd_coff_link_output_has_begun) (a, p))
   1676 .#define bfd_coff_final_link_postscript(a,p) \
   1677 .  ((coff_backend_info (a)->_bfd_coff_final_link_postscript) (a, p))
   1678 .
   1679 .#define bfd_coff_have_print_pdata(a) \
   1680 .  (coff_backend_info (a)->_bfd_coff_print_pdata)
   1681 .#define bfd_coff_print_pdata(a,p) \
   1682 .  ((coff_backend_info (a)->_bfd_coff_print_pdata) (a, p))
   1683 .
   1684 .{* Macro: Returns true if the bfd is a PE executable as opposed to a
   1685 .   PE object file.  *}
   1686 .#define bfd_pei_p(abfd) \
   1687 .  (startswith ((abfd)->xvec->name, "pei-"))
   1688 */
   1689 
   1690 /* See whether the magic number matches.  */
   1691 
   1692 static bool
   1693 coff_bad_format_hook (bfd * abfd ATTRIBUTE_UNUSED, void * filehdr)
   1694 {
   1695   struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
   1696 
   1697   if (BADMAG (*internal_f))
   1698     return false;
   1699 
   1700   return true;
   1701 }
   1702 
   1703 #ifdef TICOFF
   1704 static bool
   1705 ticoff0_bad_format_hook (bfd *abfd ATTRIBUTE_UNUSED, void * filehdr)
   1706 {
   1707   struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
   1708 
   1709   if (COFF0_BADMAG (*internal_f))
   1710     return false;
   1711 
   1712   return true;
   1713 }
   1714 #endif
   1715 
   1716 #ifdef TICOFF
   1717 static bool
   1718 ticoff1_bad_format_hook (bfd *abfd ATTRIBUTE_UNUSED, void * filehdr)
   1719 {
   1720   struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
   1721 
   1722   if (COFF1_BADMAG (*internal_f))
   1723     return false;
   1724 
   1725   return true;
   1726 }
   1727 #endif
   1728 
   1729 /* Check whether this section uses an alignment other than the
   1730    default.  */
   1731 
   1732 static void
   1733 coff_set_custom_section_alignment (bfd *abfd ATTRIBUTE_UNUSED,
   1734 				   asection *section,
   1735 				   const struct coff_section_alignment_entry *alignment_table,
   1736 				   const unsigned int table_size)
   1737 {
   1738   const unsigned int default_alignment = COFF_DEFAULT_SECTION_ALIGNMENT_POWER;
   1739   unsigned int i;
   1740 
   1741   for (i = 0; i < table_size; ++i)
   1742     {
   1743       const char *secname = bfd_section_name (section);
   1744 
   1745       if (alignment_table[i].comparison_length == (unsigned int) -1
   1746 	  ? strcmp (alignment_table[i].name, secname) == 0
   1747 	  : strncmp (alignment_table[i].name, secname,
   1748 		     alignment_table[i].comparison_length) == 0)
   1749 	break;
   1750     }
   1751   if (i >= table_size)
   1752     return;
   1753 
   1754   if (alignment_table[i].default_alignment_min != COFF_ALIGNMENT_FIELD_EMPTY
   1755       && default_alignment < alignment_table[i].default_alignment_min)
   1756     return;
   1757 
   1758   if (alignment_table[i].default_alignment_max != COFF_ALIGNMENT_FIELD_EMPTY
   1759 #if COFF_DEFAULT_SECTION_ALIGNMENT_POWER != 0
   1760       && default_alignment > alignment_table[i].default_alignment_max
   1761 #endif
   1762       )
   1763     return;
   1764 
   1765   section->alignment_power = alignment_table[i].alignment_power;
   1766 }
   1767 
   1768 /* Custom section alignment records.  */
   1769 
   1770 static const struct coff_section_alignment_entry
   1771 coff_section_alignment_table[] =
   1772 {
   1773 #ifdef COFF_SECTION_ALIGNMENT_ENTRIES
   1774   COFF_SECTION_ALIGNMENT_ENTRIES,
   1775 #endif
   1776   /* There must not be any gaps between .stabstr sections.  */
   1777   { COFF_SECTION_NAME_PARTIAL_MATCH (".stabstr"),
   1778     1, COFF_ALIGNMENT_FIELD_EMPTY, 0 },
   1779   /* The .stab section must be aligned to 2**2 at most, to avoid gaps.  */
   1780   { COFF_SECTION_NAME_PARTIAL_MATCH (".stab"),
   1781     3, COFF_ALIGNMENT_FIELD_EMPTY, 2 },
   1782   /* Similarly for the .ctors and .dtors sections.  */
   1783   { COFF_SECTION_NAME_EXACT_MATCH (".ctors"),
   1784     3, COFF_ALIGNMENT_FIELD_EMPTY, 2 },
   1785   { COFF_SECTION_NAME_EXACT_MATCH (".dtors"),
   1786     3, COFF_ALIGNMENT_FIELD_EMPTY, 2 }
   1787 };
   1788 
   1789 static const unsigned int coff_section_alignment_table_size =
   1790   sizeof coff_section_alignment_table / sizeof coff_section_alignment_table[0];
   1791 
   1792 /* Initialize a section structure with information peculiar to this
   1793    particular implementation of COFF.  */
   1794 
   1795 static bool
   1796 coff_new_section_hook (bfd * abfd, asection * section)
   1797 {
   1798   combined_entry_type *native;
   1799   size_t amt;
   1800   unsigned char sclass = C_STAT;
   1801 
   1802   section->alignment_power = COFF_DEFAULT_SECTION_ALIGNMENT_POWER;
   1803 
   1804 #ifdef RS6000COFF_C
   1805   if (bfd_xcoff_text_align_power (abfd) != 0
   1806       && strcmp (bfd_section_name (section), ".text") == 0)
   1807     section->alignment_power = bfd_xcoff_text_align_power (abfd);
   1808   else if (bfd_xcoff_data_align_power (abfd) != 0
   1809       && strcmp (bfd_section_name (section), ".data") == 0)
   1810     section->alignment_power = bfd_xcoff_data_align_power (abfd);
   1811   else
   1812     {
   1813       int i;
   1814 
   1815       for (i = 0; i < XCOFF_DWSECT_NBR_NAMES; i++)
   1816 	if (strcmp (bfd_section_name (section),
   1817 		    xcoff_dwsect_names[i].xcoff_name) == 0)
   1818 	  {
   1819 	    section->alignment_power = 0;
   1820 	    sclass = C_DWARF;
   1821 	    break;
   1822 	  }
   1823     }
   1824 #endif
   1825 
   1826   /* Set up the section symbol.  */
   1827   if (!_bfd_generic_new_section_hook (abfd, section))
   1828     return false;
   1829 
   1830   /* Allocate aux records for section symbols, to store size and
   1831      related info.
   1832 
   1833      @@ The 10 is a guess at a plausible maximum number of aux entries
   1834      (but shouldn't be a constant).  */
   1835   amt = sizeof (combined_entry_type) * 10;
   1836   native = (combined_entry_type *) bfd_zalloc (abfd, amt);
   1837   if (native == NULL)
   1838     return false;
   1839 
   1840   /* We don't need to set up n_name, n_value, or n_scnum in the native
   1841      symbol information, since they'll be overridden by the BFD symbol
   1842      anyhow.  However, we do need to set the type and storage class,
   1843      in case this symbol winds up getting written out.  The value 0
   1844      for n_numaux is already correct.  */
   1845 
   1846   native->is_sym = true;
   1847   native->u.syment.n_type = T_NULL;
   1848   native->u.syment.n_sclass = sclass;
   1849 
   1850   coffsymbol (section->symbol)->native = native;
   1851 
   1852   coff_set_custom_section_alignment (abfd, section,
   1853 				     coff_section_alignment_table,
   1854 				     coff_section_alignment_table_size);
   1855 
   1856   return true;
   1857 }
   1858 
   1859 #ifdef COFF_ALIGN_IN_SECTION_HEADER
   1860 
   1861 /* Set the alignment of a BFD section.  */
   1862 
   1863 static void
   1864 coff_set_alignment_hook (bfd * abfd ATTRIBUTE_UNUSED,
   1865 			 asection * section,
   1866 			 void * scnhdr)
   1867 {
   1868   struct internal_scnhdr *hdr = (struct internal_scnhdr *) scnhdr;
   1869   unsigned int i;
   1870 
   1871 #ifdef COFF_DECODE_ALIGNMENT
   1872   i = COFF_DECODE_ALIGNMENT(hdr->s_flags);
   1873 #endif
   1874   section->alignment_power = i;
   1875 
   1876 #ifdef coff_set_section_load_page
   1877   coff_set_section_load_page (section, hdr->s_page);
   1878 #endif
   1879 }
   1880 
   1881 #else /* ! COFF_ALIGN_IN_SECTION_HEADER */
   1882 #ifdef COFF_WITH_PE
   1883 
   1884 static void
   1885 coff_set_alignment_hook (bfd * abfd ATTRIBUTE_UNUSED,
   1886 			 asection * section,
   1887 			 void * scnhdr)
   1888 {
   1889   struct internal_scnhdr *hdr = (struct internal_scnhdr *) scnhdr;
   1890   size_t amt;
   1891   unsigned int alignment_power_const
   1892     = hdr->s_flags & IMAGE_SCN_ALIGN_POWER_BIT_MASK;
   1893 
   1894   switch (alignment_power_const)
   1895     {
   1896     case IMAGE_SCN_ALIGN_8192BYTES:
   1897     case IMAGE_SCN_ALIGN_4096BYTES:
   1898     case IMAGE_SCN_ALIGN_2048BYTES:
   1899     case IMAGE_SCN_ALIGN_1024BYTES:
   1900     case IMAGE_SCN_ALIGN_512BYTES:
   1901     case IMAGE_SCN_ALIGN_256BYTES:
   1902     case IMAGE_SCN_ALIGN_128BYTES:
   1903     case IMAGE_SCN_ALIGN_64BYTES:
   1904     case IMAGE_SCN_ALIGN_32BYTES:
   1905     case IMAGE_SCN_ALIGN_16BYTES:
   1906     case IMAGE_SCN_ALIGN_8BYTES:
   1907     case IMAGE_SCN_ALIGN_4BYTES:
   1908     case IMAGE_SCN_ALIGN_2BYTES:
   1909     case IMAGE_SCN_ALIGN_1BYTES:
   1910       section->alignment_power
   1911 	= IMAGE_SCN_ALIGN_POWER_NUM (alignment_power_const);
   1912       break;
   1913     default:
   1914       break;
   1915     }
   1916 
   1917   /* In a PE image file, the s_paddr field holds the virtual size of a
   1918      section, while the s_size field holds the raw size.  We also keep
   1919      the original section flag value, since not every bit can be
   1920      mapped onto a generic BFD section bit.  */
   1921   if (coff_section_data (abfd, section) == NULL)
   1922     {
   1923       amt = sizeof (struct coff_section_tdata);
   1924       section->used_by_bfd = bfd_zalloc (abfd, amt);
   1925       if (section->used_by_bfd == NULL)
   1926 	/* FIXME: Return error.  */
   1927 	abort ();
   1928     }
   1929 
   1930   if (pei_section_data (abfd, section) == NULL)
   1931     {
   1932       amt = sizeof (struct pei_section_tdata);
   1933       coff_section_data (abfd, section)->tdata = bfd_zalloc (abfd, amt);
   1934       if (coff_section_data (abfd, section)->tdata == NULL)
   1935 	/* FIXME: Return error.  */
   1936 	abort ();
   1937     }
   1938   pei_section_data (abfd, section)->virt_size = hdr->s_paddr;
   1939   pei_section_data (abfd, section)->pe_flags = hdr->s_flags;
   1940 
   1941   section->lma = hdr->s_vaddr;
   1942 
   1943   /* Check for extended relocs.  */
   1944   if (hdr->s_flags & IMAGE_SCN_LNK_NRELOC_OVFL)
   1945     {
   1946       struct external_reloc dst;
   1947       struct internal_reloc n;
   1948       file_ptr oldpos = bfd_tell (abfd);
   1949       bfd_size_type relsz = bfd_coff_relsz (abfd);
   1950 
   1951       if (bfd_seek (abfd, (file_ptr) hdr->s_relptr, 0) != 0)
   1952 	return;
   1953       if (bfd_bread (& dst, relsz, abfd) != relsz)
   1954 	return;
   1955 
   1956       bfd_coff_swap_reloc_in (abfd, &dst, &n);
   1957       if (bfd_seek (abfd, oldpos, 0) != 0)
   1958 	return;
   1959       if (n.r_vaddr < 0x10000)
   1960 	{
   1961 	  _bfd_error_handler (_("%pB: overflow reloc count too small"), abfd);
   1962 	  bfd_set_error (bfd_error_bad_value);
   1963 	  return;
   1964 	}
   1965       section->reloc_count = hdr->s_nreloc = n.r_vaddr - 1;
   1966       section->rel_filepos += relsz;
   1967     }
   1968   else if (hdr->s_nreloc == 0xffff)
   1969     _bfd_error_handler
   1970       (_("%pB: warning: claims to have 0xffff relocs, without overflow"),
   1971        abfd);
   1972 }
   1973 #undef ALIGN_SET
   1974 #undef ELIFALIGN_SET
   1975 
   1976 #else /* ! COFF_WITH_PE */
   1977 #ifdef RS6000COFF_C
   1978 
   1979 /* We grossly abuse this function to handle XCOFF overflow headers.
   1980    When we see one, we correct the reloc and line number counts in the
   1981    real header, and remove the section we just created.  */
   1982 
   1983 static void
   1984 coff_set_alignment_hook (bfd *abfd, asection *section, void * scnhdr)
   1985 {
   1986   struct internal_scnhdr *hdr = (struct internal_scnhdr *) scnhdr;
   1987   asection *real_sec;
   1988 
   1989   if ((hdr->s_flags & STYP_OVRFLO) == 0)
   1990     return;
   1991 
   1992   real_sec = coff_section_from_bfd_index (abfd, (int) hdr->s_nreloc);
   1993   if (real_sec == NULL)
   1994     return;
   1995 
   1996   real_sec->reloc_count = hdr->s_paddr;
   1997   real_sec->lineno_count = hdr->s_vaddr;
   1998 
   1999   if (!bfd_section_removed_from_list (abfd, section))
   2000     {
   2001       bfd_section_list_remove (abfd, section);
   2002       --abfd->section_count;
   2003     }
   2004 }
   2005 
   2006 #else /* ! RS6000COFF_C */
   2007 #if defined (COFF_GO32_EXE) || defined (COFF_GO32)
   2008 
   2009 static void
   2010 coff_set_alignment_hook (bfd * abfd, asection * section, void * scnhdr)
   2011 {
   2012   struct internal_scnhdr *hdr = (struct internal_scnhdr *) scnhdr;
   2013 
   2014   /* Check for extended relocs.  */
   2015   if (hdr->s_flags & IMAGE_SCN_LNK_NRELOC_OVFL)
   2016     {
   2017       struct external_reloc dst;
   2018       struct internal_reloc n;
   2019       const file_ptr oldpos = bfd_tell (abfd);
   2020       const bfd_size_type relsz = bfd_coff_relsz (abfd);
   2021 
   2022       if (bfd_seek (abfd, (file_ptr) hdr->s_relptr, 0) != 0)
   2023 	return;
   2024       if (bfd_bread (& dst, relsz, abfd) != relsz)
   2025 	return;
   2026 
   2027       bfd_coff_swap_reloc_in (abfd, &dst, &n);
   2028       if (bfd_seek (abfd, oldpos, 0) != 0)
   2029 	return;
   2030       section->reloc_count = hdr->s_nreloc = n.r_vaddr - 1;
   2031       section->rel_filepos += relsz;
   2032     }
   2033   else if (hdr->s_nreloc == 0xffff)
   2034     _bfd_error_handler
   2035       (_("%pB: warning: claims to have 0xffff relocs, without overflow"),
   2036        abfd);
   2037 }
   2038 
   2039 #else /* ! COFF_GO32_EXE && ! COFF_GO32 */
   2040 
   2041 static void
   2042 coff_set_alignment_hook (bfd *abfd ATTRIBUTE_UNUSED,
   2043 			 asection *section ATTRIBUTE_UNUSED,
   2044 			 void *scnhdr ATTRIBUTE_UNUSED)
   2045 {
   2046 }
   2047 
   2048 #endif /* ! COFF_GO32_EXE && ! COFF_GO32 */
   2049 #endif /* ! RS6000COFF_C */
   2050 #endif /* ! COFF_WITH_PE */
   2051 #endif /* ! COFF_ALIGN_IN_SECTION_HEADER */
   2052 
   2053 #ifndef coff_mkobject
   2054 
   2055 static bool
   2056 coff_mkobject (bfd * abfd)
   2057 {
   2058   coff_data_type *coff;
   2059   size_t amt = sizeof (coff_data_type);
   2060 
   2061   abfd->tdata.coff_obj_data = bfd_zalloc (abfd, amt);
   2062   if (abfd->tdata.coff_obj_data == NULL)
   2063     return false;
   2064   coff = coff_data (abfd);
   2065   coff->symbols = NULL;
   2066   coff->conversion_table = NULL;
   2067   coff->raw_syments = NULL;
   2068   coff->relocbase = 0;
   2069   coff->local_toc_sym_map = 0;
   2070 
   2071 /*  make_abs_section(abfd);*/
   2072 
   2073   return true;
   2074 }
   2075 #endif
   2076 
   2077 /* Create the COFF backend specific information.  */
   2078 
   2079 #ifndef coff_mkobject_hook
   2080 static void *
   2081 coff_mkobject_hook (bfd * abfd,
   2082 		    void * filehdr,
   2083 		    void * aouthdr ATTRIBUTE_UNUSED)
   2084 {
   2085   struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
   2086   coff_data_type *coff;
   2087 
   2088   if (! coff_mkobject (abfd))
   2089     return NULL;
   2090 
   2091   coff = coff_data (abfd);
   2092 
   2093   coff->sym_filepos = internal_f->f_symptr;
   2094 
   2095   /* These members communicate important constants about the symbol
   2096      table to GDB's symbol-reading code.  These `constants'
   2097      unfortunately vary among coff implementations...  */
   2098   coff->local_n_btmask = N_BTMASK;
   2099   coff->local_n_btshft = N_BTSHFT;
   2100   coff->local_n_tmask = N_TMASK;
   2101   coff->local_n_tshift = N_TSHIFT;
   2102   coff->local_symesz = bfd_coff_symesz (abfd);
   2103   coff->local_auxesz = bfd_coff_auxesz (abfd);
   2104   coff->local_linesz = bfd_coff_linesz (abfd);
   2105 
   2106   coff->timestamp = internal_f->f_timdat;
   2107 
   2108   obj_raw_syment_count (abfd) =
   2109     obj_conv_table_size (abfd) =
   2110       internal_f->f_nsyms;
   2111 
   2112 #ifdef RS6000COFF_C
   2113   if ((internal_f->f_flags & F_SHROBJ) != 0)
   2114     abfd->flags |= DYNAMIC;
   2115   if (aouthdr != NULL && internal_f->f_opthdr >= bfd_coff_aoutsz (abfd))
   2116     {
   2117       struct internal_aouthdr *internal_a =
   2118 	(struct internal_aouthdr *) aouthdr;
   2119       struct xcoff_tdata *xcoff;
   2120 
   2121       xcoff = xcoff_data (abfd);
   2122 # ifdef U803XTOCMAGIC
   2123       xcoff->xcoff64 = internal_f->f_magic == U803XTOCMAGIC;
   2124 # else
   2125       xcoff->xcoff64 = 0;
   2126 # endif
   2127       xcoff->full_aouthdr = true;
   2128       xcoff->toc = internal_a->o_toc;
   2129       xcoff->sntoc = internal_a->o_sntoc;
   2130       xcoff->snentry = internal_a->o_snentry;
   2131       bfd_xcoff_text_align_power (abfd) = internal_a->o_algntext;
   2132       bfd_xcoff_data_align_power (abfd) = internal_a->o_algndata;
   2133       xcoff->modtype = internal_a->o_modtype;
   2134       xcoff->cputype = internal_a->o_cputype;
   2135       xcoff->maxdata = internal_a->o_maxdata;
   2136       xcoff->maxstack = internal_a->o_maxstack;
   2137     }
   2138 #endif
   2139 
   2140 #ifdef ARM
   2141   /* Set the flags field from the COFF header read in.  */
   2142   if (! _bfd_coff_arm_set_private_flags (abfd, internal_f->f_flags))
   2143     coff->flags = 0;
   2144 #endif
   2145 
   2146 #ifdef COFF_WITH_PE
   2147   /* FIXME: I'm not sure this is ever executed, since peicode.h
   2148      defines coff_mkobject_hook.  */
   2149   if ((internal_f->f_flags & IMAGE_FILE_DEBUG_STRIPPED) == 0)
   2150     abfd->flags |= HAS_DEBUG;
   2151 #endif
   2152 
   2153   return coff;
   2154 }
   2155 #endif
   2156 
   2157 /* Determine the machine architecture and type.  FIXME: This is target
   2158    dependent because the magic numbers are defined in the target
   2159    dependent header files.  But there is no particular need for this.
   2160    If the magic numbers were moved to a separate file, this function
   2161    would be target independent and would also be much more successful
   2162    at linking together COFF files for different architectures.  */
   2163 
   2164 static bool
   2165 coff_set_arch_mach_hook (bfd *abfd, void * filehdr)
   2166 {
   2167   unsigned long machine;
   2168   enum bfd_architecture arch;
   2169   struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
   2170 
   2171   /* Zero selects the default machine for an arch.  */
   2172   machine = 0;
   2173   switch (internal_f->f_magic)
   2174     {
   2175 #ifdef I386MAGIC
   2176     case I386MAGIC:
   2177     case I386PTXMAGIC:
   2178     case I386AIXMAGIC:		/* Danbury PS/2 AIX C Compiler.  */
   2179     case LYNXCOFFMAGIC:
   2180     case I386_APPLE_MAGIC:
   2181     case I386_FREEBSD_MAGIC:
   2182     case I386_LINUX_MAGIC:
   2183     case I386_NETBSD_MAGIC:
   2184       arch = bfd_arch_i386;
   2185       break;
   2186 #endif
   2187 #ifdef AMD64MAGIC
   2188     case AMD64MAGIC:
   2189     case AMD64_APPLE_MAGIC:
   2190     case AMD64_FREEBSD_MAGIC:
   2191     case AMD64_LINUX_MAGIC:
   2192     case AMD64_NETBSD_MAGIC:
   2193       arch = bfd_arch_i386;
   2194       machine = bfd_mach_x86_64;
   2195       break;
   2196 #endif
   2197 #ifdef IA64MAGIC
   2198     case IA64MAGIC:
   2199       arch = bfd_arch_ia64;
   2200       break;
   2201 #endif
   2202 #ifdef ARMMAGIC
   2203     case ARMMAGIC:
   2204     case ARMPEMAGIC:
   2205     case THUMBPEMAGIC:
   2206       arch = bfd_arch_arm;
   2207       machine = bfd_arm_get_mach_from_notes (abfd, ARM_NOTE_SECTION);
   2208       if (machine == bfd_mach_arm_unknown)
   2209 	{
   2210 	  switch (internal_f->f_flags & F_ARM_ARCHITECTURE_MASK)
   2211 	    {
   2212 	    case F_ARM_2:  machine = bfd_mach_arm_2;  break;
   2213 	    case F_ARM_2a: machine = bfd_mach_arm_2a; break;
   2214 	    case F_ARM_3:  machine = bfd_mach_arm_3;  break;
   2215 	    default:
   2216 	    case F_ARM_3M: machine = bfd_mach_arm_3M; break;
   2217 	    case F_ARM_4:  machine = bfd_mach_arm_4;  break;
   2218 	    case F_ARM_4T: machine = bfd_mach_arm_4T; break;
   2219 	      /* The COFF header does not have enough bits available
   2220 		 to cover all the different ARM architectures.  So
   2221 		 we interpret F_ARM_5, the highest flag value to mean
   2222 		 "the highest ARM architecture known to BFD" which is
   2223 		 currently the XScale.  */
   2224 	    case F_ARM_5:  machine = bfd_mach_arm_XScale;  break;
   2225 	    }
   2226 	}
   2227       break;
   2228 #endif
   2229 #ifdef AARCH64MAGIC
   2230     case AARCH64MAGIC:
   2231       arch = bfd_arch_aarch64;
   2232       machine = internal_f->f_flags & F_AARCH64_ARCHITECTURE_MASK;
   2233       break;
   2234 #endif
   2235 #ifdef LOONGARCH64MAGIC
   2236     case LOONGARCH64MAGIC:
   2237       arch = bfd_arch_loongarch;
   2238       machine = internal_f->f_flags & F_LOONGARCH64_ARCHITECTURE_MASK;
   2239       break;
   2240 #endif
   2241 #ifdef Z80MAGIC
   2242     case Z80MAGIC:
   2243       arch = bfd_arch_z80;
   2244       switch (internal_f->f_flags & F_MACHMASK)
   2245 	{
   2246 	case bfd_mach_z80strict << 12:
   2247 	case bfd_mach_z80 << 12:
   2248 	case bfd_mach_z80n << 12:
   2249 	case bfd_mach_z80full << 12:
   2250 	case bfd_mach_r800 << 12:
   2251 	case bfd_mach_gbz80 << 12:
   2252 	case bfd_mach_z180 << 12:
   2253 	case bfd_mach_ez80_z80 << 12:
   2254 	case bfd_mach_ez80_adl << 12:
   2255 	  machine = ((unsigned)internal_f->f_flags & F_MACHMASK) >> 12;
   2256 	  break;
   2257 	default:
   2258 	  return false;
   2259 	}
   2260       break;
   2261 #endif
   2262 #ifdef Z8KMAGIC
   2263     case Z8KMAGIC:
   2264       arch = bfd_arch_z8k;
   2265       switch (internal_f->f_flags & F_MACHMASK)
   2266 	{
   2267 	case F_Z8001:
   2268 	  machine = bfd_mach_z8001;
   2269 	  break;
   2270 	case F_Z8002:
   2271 	  machine = bfd_mach_z8002;
   2272 	  break;
   2273 	default:
   2274 	  return false;
   2275 	}
   2276       break;
   2277 #endif
   2278 
   2279 #ifdef RS6000COFF_C
   2280 #ifdef XCOFF64
   2281     case U64_TOCMAGIC:
   2282     case U803XTOCMAGIC:
   2283 #else
   2284     case U802ROMAGIC:
   2285     case U802WRMAGIC:
   2286     case U802TOCMAGIC:
   2287 #endif
   2288       {
   2289 	int cputype;
   2290 
   2291 	if (xcoff_data (abfd)->cputype != -1)
   2292 	  cputype = xcoff_data (abfd)->cputype & 0xff;
   2293 	else
   2294 	  {
   2295 	    /* We did not get a value from the a.out header.  If the
   2296 	       file has not been stripped, we may be able to get the
   2297 	       architecture information from the first symbol, if it
   2298 	       is a .file symbol.  */
   2299 	    if (obj_raw_syment_count (abfd) == 0)
   2300 	      cputype = 0;
   2301 	    else
   2302 	      {
   2303 		bfd_byte *buf;
   2304 		struct internal_syment sym;
   2305 		bfd_size_type amt = bfd_coff_symesz (abfd);
   2306 
   2307 		if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
   2308 		  return false;
   2309 		buf = _bfd_malloc_and_read (abfd, amt, amt);
   2310 		if (buf == NULL)
   2311 		  return false;
   2312 		bfd_coff_swap_sym_in (abfd, buf, & sym);
   2313 		if (sym.n_sclass == C_FILE)
   2314 		  cputype = sym.n_type & 0xff;
   2315 		else
   2316 		  cputype = 0;
   2317 		free (buf);
   2318 	      }
   2319 	  }
   2320 
   2321 	/* FIXME: We don't handle all cases here.  */
   2322 	switch (cputype)
   2323 	  {
   2324 	  default:
   2325 	  case 0:
   2326 	    arch = bfd_xcoff_architecture (abfd);
   2327 	    machine = bfd_xcoff_machine (abfd);
   2328 	    break;
   2329 
   2330 	  case 1:
   2331 	    arch = bfd_arch_powerpc;
   2332 	    machine = bfd_mach_ppc_601;
   2333 	    break;
   2334 	  case 2: /* 64 bit PowerPC */
   2335 	    arch = bfd_arch_powerpc;
   2336 	    machine = bfd_mach_ppc_620;
   2337 	    break;
   2338 	  case 3:
   2339 	    arch = bfd_arch_powerpc;
   2340 	    machine = bfd_mach_ppc;
   2341 	    break;
   2342 	  case 4:
   2343 	    arch = bfd_arch_rs6000;
   2344 	    machine = bfd_mach_rs6k;
   2345 	    break;
   2346 	  }
   2347       }
   2348       break;
   2349 #endif
   2350 
   2351 #ifdef SH_ARCH_MAGIC_BIG
   2352     case SH_ARCH_MAGIC_BIG:
   2353     case SH_ARCH_MAGIC_LITTLE:
   2354 #ifdef COFF_WITH_PE
   2355     case SH_ARCH_MAGIC_WINCE:
   2356 #endif
   2357       arch = bfd_arch_sh;
   2358       break;
   2359 #endif
   2360 
   2361 #ifdef MIPS_ARCH_MAGIC_WINCE
   2362     case MIPS_ARCH_MAGIC_WINCE:
   2363       arch = bfd_arch_mips;
   2364       break;
   2365 #endif
   2366 
   2367 #ifdef SPARCMAGIC
   2368     case SPARCMAGIC:
   2369 #ifdef LYNXCOFFMAGIC
   2370     case LYNXCOFFMAGIC:
   2371 #endif
   2372       arch = bfd_arch_sparc;
   2373       break;
   2374 #endif
   2375 
   2376 #ifdef TIC30MAGIC
   2377     case TIC30MAGIC:
   2378       arch = bfd_arch_tic30;
   2379       break;
   2380 #endif
   2381 
   2382 #ifdef TICOFF0MAGIC
   2383 #ifdef TICOFF_TARGET_ARCH
   2384       /* This TI COFF section should be used by all new TI COFF v0 targets.  */
   2385     case TICOFF0MAGIC:
   2386       arch = TICOFF_TARGET_ARCH;
   2387       machine = TICOFF_TARGET_MACHINE_GET (internal_f->f_flags);
   2388       break;
   2389 #endif
   2390 #endif
   2391 
   2392 #ifdef TICOFF1MAGIC
   2393       /* This TI COFF section should be used by all new TI COFF v1/2 targets.  */
   2394       /* TI COFF1 and COFF2 use the target_id field to specify which arch.  */
   2395     case TICOFF1MAGIC:
   2396     case TICOFF2MAGIC:
   2397       switch (internal_f->f_target_id)
   2398 	{
   2399 #ifdef TI_TARGET_ID
   2400 	case TI_TARGET_ID:
   2401 	  arch = TICOFF_TARGET_ARCH;
   2402 	  machine = TICOFF_TARGET_MACHINE_GET (internal_f->f_flags);
   2403 	  break;
   2404 #endif
   2405 	default:
   2406 	  arch = bfd_arch_obscure;
   2407 	  _bfd_error_handler
   2408 	    (_("unrecognized TI COFF target id '0x%x'"),
   2409 	     internal_f->f_target_id);
   2410 	  break;
   2411 	}
   2412       break;
   2413 #endif
   2414 
   2415 #ifdef MCOREMAGIC
   2416     case MCOREMAGIC:
   2417       arch = bfd_arch_mcore;
   2418       break;
   2419 #endif
   2420 
   2421     default:			/* Unreadable input file type.  */
   2422       arch = bfd_arch_obscure;
   2423       break;
   2424     }
   2425 
   2426   bfd_default_set_arch_mach (abfd, arch, machine);
   2427   return true;
   2428 }
   2429 
   2430 static bool
   2431 symname_in_debug_hook (bfd *abfd ATTRIBUTE_UNUSED,
   2432 		       struct internal_syment *sym ATTRIBUTE_UNUSED)
   2433 {
   2434 #ifdef SYMNAME_IN_DEBUG
   2435   return SYMNAME_IN_DEBUG (sym) != 0;
   2436 #else
   2437   return false;
   2438 #endif
   2439 }
   2440 
   2441 #ifdef RS6000COFF_C
   2442 
   2443 #ifdef XCOFF64
   2444 #define FORCE_SYMNAMES_IN_STRINGS
   2445 #endif
   2446 
   2447 /* Handle the csect auxent of a C_EXT, C_AIX_WEAKEXT or C_HIDEXT symbol.  */
   2448 
   2449 static bool
   2450 coff_pointerize_aux_hook (bfd *abfd ATTRIBUTE_UNUSED,
   2451 			  combined_entry_type *table_base,
   2452 			  combined_entry_type *symbol,
   2453 			  unsigned int indaux,
   2454 			  combined_entry_type *aux)
   2455 {
   2456   BFD_ASSERT (symbol->is_sym);
   2457   int n_sclass = symbol->u.syment.n_sclass;
   2458 
   2459   if (CSECT_SYM_P (n_sclass)
   2460       && indaux + 1 == symbol->u.syment.n_numaux)
   2461     {
   2462       BFD_ASSERT (! aux->is_sym);
   2463       if (SMTYP_SMTYP (aux->u.auxent.x_csect.x_smtyp) == XTY_LD)
   2464 	{
   2465 	  aux->u.auxent.x_csect.x_scnlen.p =
   2466 	    table_base + aux->u.auxent.x_csect.x_scnlen.l;
   2467 	  aux->fix_scnlen = 1;
   2468 	}
   2469 
   2470       /* Return TRUE to indicate that the caller should not do any
   2471 	 further work on this auxent.  */
   2472       return true;
   2473     }
   2474 
   2475   /* Return FALSE to indicate that this auxent should be handled by
   2476      the caller.  */
   2477   return false;
   2478 }
   2479 
   2480 #else
   2481 #define coff_pointerize_aux_hook 0
   2482 #endif /* ! RS6000COFF_C */
   2483 
   2484 /* Print an aux entry.  This returns TRUE if it has printed it.  */
   2485 
   2486 static bool
   2487 coff_print_aux (bfd *abfd ATTRIBUTE_UNUSED,
   2488 		FILE *file ATTRIBUTE_UNUSED,
   2489 		combined_entry_type *table_base ATTRIBUTE_UNUSED,
   2490 		combined_entry_type *symbol ATTRIBUTE_UNUSED,
   2491 		combined_entry_type *aux ATTRIBUTE_UNUSED,
   2492 		unsigned int indaux ATTRIBUTE_UNUSED)
   2493 {
   2494   BFD_ASSERT (symbol->is_sym);
   2495   BFD_ASSERT (! aux->is_sym);
   2496 #ifdef RS6000COFF_C
   2497   if (CSECT_SYM_P (symbol->u.syment.n_sclass)
   2498       && indaux + 1 == symbol->u.syment.n_numaux)
   2499     {
   2500       /* This is a csect entry.  */
   2501       fprintf (file, "AUX ");
   2502       if (SMTYP_SMTYP (aux->u.auxent.x_csect.x_smtyp) != XTY_LD)
   2503 	{
   2504 	  BFD_ASSERT (! aux->fix_scnlen);
   2505 	  fprintf (file, "val %5" PRId64,
   2506 		   (int64_t) aux->u.auxent.x_csect.x_scnlen.l);
   2507 	}
   2508       else
   2509 	{
   2510 	  fprintf (file, "indx ");
   2511 	  if (! aux->fix_scnlen)
   2512 	    fprintf (file, "%4" PRId64,
   2513 		     (int64_t) aux->u.auxent.x_csect.x_scnlen.l);
   2514 	  else
   2515 	    fprintf (file, "%4ld",
   2516 		     (long) (aux->u.auxent.x_csect.x_scnlen.p - table_base));
   2517 	}
   2518       fprintf (file,
   2519 	       " prmhsh %ld snhsh %u typ %d algn %d clss %u stb %ld snstb %u",
   2520 	       aux->u.auxent.x_csect.x_parmhash,
   2521 	       (unsigned int) aux->u.auxent.x_csect.x_snhash,
   2522 	       SMTYP_SMTYP (aux->u.auxent.x_csect.x_smtyp),
   2523 	       SMTYP_ALIGN (aux->u.auxent.x_csect.x_smtyp),
   2524 	       (unsigned int) aux->u.auxent.x_csect.x_smclas,
   2525 	       aux->u.auxent.x_csect.x_stab,
   2526 	       (unsigned int) aux->u.auxent.x_csect.x_snstab);
   2527       return true;
   2528     }
   2529 #endif
   2530 
   2531   /* Return FALSE to indicate that no special action was taken.  */
   2532   return false;
   2533 }
   2534 
   2535 /*
   2536 SUBSUBSECTION
   2537 	Writing relocations
   2538 
   2539 	To write relocations, the back end steps though the
   2540 	canonical relocation table and create an
   2541 	@code{internal_reloc}. The symbol index to use is removed from
   2542 	the @code{offset} field in the symbol table supplied.  The
   2543 	address comes directly from the sum of the section base
   2544 	address and the relocation offset; the type is dug directly
   2545 	from the howto field.  Then the @code{internal_reloc} is
   2546 	swapped into the shape of an @code{external_reloc} and written
   2547 	out to disk.
   2548 
   2549 */
   2550 
   2551 #ifdef TARG_AUX
   2552 
   2553 
   2554 /* AUX's ld wants relocations to be sorted.  */
   2555 static int
   2556 compare_arelent_ptr (const void * x, const void * y)
   2557 {
   2558   const arelent **a = (const arelent **) x;
   2559   const arelent **b = (const arelent **) y;
   2560   bfd_size_type aadr = (*a)->address;
   2561   bfd_size_type badr = (*b)->address;
   2562 
   2563   return (aadr < badr ? -1 : badr < aadr ? 1 : 0);
   2564 }
   2565 
   2566 #endif /* TARG_AUX */
   2567 
   2568 static bool
   2569 coff_write_relocs (bfd * abfd, int first_undef)
   2570 {
   2571   asection *s;
   2572 
   2573   for (s = abfd->sections; s != NULL; s = s->next)
   2574     {
   2575       unsigned int i;
   2576       struct external_reloc dst;
   2577       arelent **p;
   2578 
   2579 #ifndef TARG_AUX
   2580       p = s->orelocation;
   2581 #else
   2582       {
   2583 	/* Sort relocations before we write them out.  */
   2584 	bfd_size_type amt;
   2585 
   2586 	amt = s->reloc_count;
   2587 	amt *= sizeof (arelent *);
   2588 	p = bfd_malloc (amt);
   2589 	if (p == NULL)
   2590 	  {
   2591 	    if (s->reloc_count > 0)
   2592 	      return false;
   2593 	  }
   2594 	else
   2595 	  {
   2596 	    memcpy (p, s->orelocation, (size_t) amt);
   2597 	    qsort (p, s->reloc_count, sizeof (arelent *), compare_arelent_ptr);
   2598 	  }
   2599       }
   2600 #endif
   2601 
   2602       if (bfd_seek (abfd, s->rel_filepos, SEEK_SET) != 0)
   2603 	return false;
   2604 
   2605 #ifdef COFF_WITH_EXTENDED_RELOC_COUNTER
   2606       if ((obj_pe (abfd) || obj_go32 (abfd)) && s->reloc_count >= 0xffff)
   2607 	{
   2608 	  /* Encode real count here as first reloc.  */
   2609 	  struct internal_reloc n;
   2610 
   2611 	  memset (& n, 0, sizeof (n));
   2612 	  /* Add one to count *this* reloc (grr).  */
   2613 	  n.r_vaddr = s->reloc_count + 1;
   2614 	  coff_swap_reloc_out (abfd, &n, &dst);
   2615 	  if (bfd_bwrite (& dst, (bfd_size_type) bfd_coff_relsz (abfd),
   2616 			  abfd) != bfd_coff_relsz (abfd))
   2617 	    return false;
   2618 	}
   2619 #endif
   2620 
   2621       for (i = 0; i < s->reloc_count; i++)
   2622 	{
   2623 	  struct internal_reloc n;
   2624 	  arelent *q = p[i];
   2625 
   2626 	  memset (& n, 0, sizeof (n));
   2627 
   2628 	  /* Now we've renumbered the symbols we know where the
   2629 	     undefined symbols live in the table.  Check the reloc
   2630 	     entries for symbols who's output bfd isn't the right one.
   2631 	     This is because the symbol was undefined (which means
   2632 	     that all the pointers are never made to point to the same
   2633 	     place). This is a bad thing,'cause the symbols attached
   2634 	     to the output bfd are indexed, so that the relocation
   2635 	     entries know which symbol index they point to.  So we
   2636 	     have to look up the output symbol here.  */
   2637 
   2638 	  if (q->sym_ptr_ptr[0] != NULL && q->sym_ptr_ptr[0]->the_bfd != abfd)
   2639 	    {
   2640 	      int j;
   2641 	      const char *sname = q->sym_ptr_ptr[0]->name;
   2642 	      asymbol **outsyms = abfd->outsymbols;
   2643 
   2644 	      for (j = first_undef; outsyms[j]; j++)
   2645 		{
   2646 		  const char *intable = outsyms[j]->name;
   2647 
   2648 		  if (strcmp (intable, sname) == 0)
   2649 		    {
   2650 		      /* Got a hit, so repoint the reloc.  */
   2651 		      q->sym_ptr_ptr = outsyms + j;
   2652 		      break;
   2653 		    }
   2654 		}
   2655 	    }
   2656 
   2657 	  n.r_vaddr = q->address + s->vma;
   2658 
   2659 #ifdef R_IHCONST
   2660 	  /* The 29k const/consth reloc pair is a real kludge.  The consth
   2661 	     part doesn't have a symbol; it has an offset.  So rebuilt
   2662 	     that here.  */
   2663 	  if (q->howto->type == R_IHCONST)
   2664 	    n.r_symndx = q->addend;
   2665 	  else
   2666 #endif
   2667 	    if (q->sym_ptr_ptr && q->sym_ptr_ptr[0] != NULL)
   2668 	      {
   2669 #ifdef SECTION_RELATIVE_ABSOLUTE_SYMBOL_P
   2670 		if (SECTION_RELATIVE_ABSOLUTE_SYMBOL_P (q, s))
   2671 #else
   2672 		if ((*q->sym_ptr_ptr)->section == bfd_abs_section_ptr
   2673 		    && ((*q->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0)
   2674 #endif
   2675 		  /* This is a relocation relative to the absolute symbol.  */
   2676 		  n.r_symndx = -1;
   2677 		else
   2678 		  {
   2679 		    n.r_symndx = get_index ((*(q->sym_ptr_ptr)));
   2680 		    /* Check to see if the symbol reloc points to a symbol
   2681 		       we don't have in our symbol table.  */
   2682 		    if (n.r_symndx > obj_conv_table_size (abfd))
   2683 		      {
   2684 			bfd_set_error (bfd_error_bad_value);
   2685 			/* xgettext:c-format */
   2686 			_bfd_error_handler (_("%pB: reloc against a non-existent"
   2687 					      " symbol index: %ld"),
   2688 					    abfd, n.r_symndx);
   2689 			return false;
   2690 		      }
   2691 		  }
   2692 	      }
   2693 
   2694 #ifdef SWAP_OUT_RELOC_OFFSET
   2695 	  n.r_offset = q->addend;
   2696 #endif
   2697 
   2698 #ifdef SELECT_RELOC
   2699 	  /* Work out reloc type from what is required.  */
   2700 	  if (q->howto)
   2701 	    SELECT_RELOC (n, q->howto);
   2702 #else
   2703 	  if (q->howto)
   2704 	    n.r_type = q->howto->type;
   2705 #endif
   2706 	  coff_swap_reloc_out (abfd, &n, &dst);
   2707 
   2708 	  if (bfd_bwrite (& dst, (bfd_size_type) bfd_coff_relsz (abfd),
   2709 			 abfd) != bfd_coff_relsz (abfd))
   2710 	    return false;
   2711 	}
   2712 
   2713 #ifdef TARG_AUX
   2714       free (p);
   2715 #endif
   2716     }
   2717 
   2718   return true;
   2719 }
   2720 
   2721 /* Set flags and magic number of a coff file from architecture and machine
   2722    type.  Result is TRUE if we can represent the arch&type, FALSE if not.  */
   2723 
   2724 static bool
   2725 coff_set_flags (bfd * abfd,
   2726 		unsigned int *magicp ATTRIBUTE_UNUSED,
   2727 		unsigned short *flagsp ATTRIBUTE_UNUSED)
   2728 {
   2729   switch (bfd_get_arch (abfd))
   2730     {
   2731 #ifdef Z80MAGIC
   2732     case bfd_arch_z80:
   2733       *magicp = Z80MAGIC;
   2734       switch (bfd_get_mach (abfd))
   2735 	{
   2736 	case bfd_mach_z80strict:
   2737 	case bfd_mach_z80:
   2738 	case bfd_mach_z80n:
   2739 	case bfd_mach_z80full:
   2740 	case bfd_mach_r800:
   2741 	case bfd_mach_gbz80:
   2742 	case bfd_mach_z180:
   2743 	case bfd_mach_ez80_z80:
   2744 	case bfd_mach_ez80_adl:
   2745 	  *flagsp = bfd_get_mach (abfd) << 12;
   2746 	  break;
   2747 	default:
   2748 	  return false;
   2749 	}
   2750       return true;
   2751 #endif
   2752 
   2753 #ifdef Z8KMAGIC
   2754     case bfd_arch_z8k:
   2755       *magicp = Z8KMAGIC;
   2756 
   2757       switch (bfd_get_mach (abfd))
   2758 	{
   2759 	case bfd_mach_z8001: *flagsp = F_Z8001;	break;
   2760 	case bfd_mach_z8002: *flagsp = F_Z8002;	break;
   2761 	default:	     return false;
   2762 	}
   2763       return true;
   2764 #endif
   2765 
   2766 #ifdef TIC30MAGIC
   2767     case bfd_arch_tic30:
   2768       *magicp = TIC30MAGIC;
   2769       return true;
   2770 #endif
   2771 
   2772 #ifdef TICOFF_DEFAULT_MAGIC
   2773     case TICOFF_TARGET_ARCH:
   2774       /* If there's no indication of which version we want, use the default.  */
   2775       if (!abfd->xvec )
   2776 	*magicp = TICOFF_DEFAULT_MAGIC;
   2777       else
   2778 	{
   2779 	  /* We may want to output in a different COFF version.  */
   2780 	  switch (abfd->xvec->name[4])
   2781 	    {
   2782 	    case '0':
   2783 	      *magicp = TICOFF0MAGIC;
   2784 	      break;
   2785 	    case '1':
   2786 	      *magicp = TICOFF1MAGIC;
   2787 	      break;
   2788 	    case '2':
   2789 	      *magicp = TICOFF2MAGIC;
   2790 	      break;
   2791 	    default:
   2792 	      return false;
   2793 	    }
   2794 	}
   2795       TICOFF_TARGET_MACHINE_SET (flagsp, bfd_get_mach (abfd));
   2796       return true;
   2797 #endif
   2798 
   2799 #ifdef AARCH64MAGIC
   2800     case bfd_arch_aarch64:
   2801       * magicp = AARCH64MAGIC;
   2802       return true;
   2803 #endif
   2804 
   2805 #ifdef LOONGARCH64MAGIC
   2806     case bfd_arch_loongarch:
   2807       * magicp = LOONGARCH64MAGIC;
   2808       return true;
   2809 #endif
   2810 
   2811 #ifdef ARMMAGIC
   2812     case bfd_arch_arm:
   2813 #ifdef ARM_WINCE
   2814       * magicp = ARMPEMAGIC;
   2815 #else
   2816       * magicp = ARMMAGIC;
   2817 #endif
   2818       * flagsp = 0;
   2819       if (APCS_SET (abfd))
   2820 	{
   2821 	  if (APCS_26_FLAG (abfd))
   2822 	    * flagsp |= F_APCS26;
   2823 
   2824 	  if (APCS_FLOAT_FLAG (abfd))
   2825 	    * flagsp |= F_APCS_FLOAT;
   2826 
   2827 	  if (PIC_FLAG (abfd))
   2828 	    * flagsp |= F_PIC;
   2829 	}
   2830       if (INTERWORK_SET (abfd) && INTERWORK_FLAG (abfd))
   2831 	* flagsp |= F_INTERWORK;
   2832       switch (bfd_get_mach (abfd))
   2833 	{
   2834 	case bfd_mach_arm_2:  * flagsp |= F_ARM_2;  break;
   2835 	case bfd_mach_arm_2a: * flagsp |= F_ARM_2a; break;
   2836 	case bfd_mach_arm_3:  * flagsp |= F_ARM_3;  break;
   2837 	case bfd_mach_arm_3M: * flagsp |= F_ARM_3M; break;
   2838 	case bfd_mach_arm_4:  * flagsp |= F_ARM_4;  break;
   2839 	case bfd_mach_arm_4T: * flagsp |= F_ARM_4T; break;
   2840 	case bfd_mach_arm_5:  * flagsp |= F_ARM_5;  break;
   2841 	  /* FIXME: we do not have F_ARM vaues greater than F_ARM_5.
   2842 	     See also the comment in coff_set_arch_mach_hook().  */
   2843 	case bfd_mach_arm_5T: * flagsp |= F_ARM_5;  break;
   2844 	case bfd_mach_arm_5TE: * flagsp |= F_ARM_5; break;
   2845 	case bfd_mach_arm_XScale: * flagsp |= F_ARM_5; break;
   2846 	}
   2847       return true;
   2848 #endif
   2849 
   2850 #if defined(I386MAGIC) || defined(AMD64MAGIC)
   2851     case bfd_arch_i386:
   2852 #if defined(I386MAGIC)
   2853       *magicp = I386MAGIC;
   2854 #endif
   2855 #if defined LYNXOS
   2856       /* Just overwrite the usual value if we're doing Lynx.  */
   2857       *magicp = LYNXCOFFMAGIC;
   2858 #endif
   2859 #if defined AMD64MAGIC
   2860       *magicp = AMD64MAGIC;
   2861 #endif
   2862       return true;
   2863 #endif
   2864 
   2865 #ifdef IA64MAGIC
   2866     case bfd_arch_ia64:
   2867       *magicp = IA64MAGIC;
   2868       return true;
   2869 #endif
   2870 
   2871 #ifdef SH_ARCH_MAGIC_BIG
   2872     case bfd_arch_sh:
   2873 #ifdef COFF_IMAGE_WITH_PE
   2874       *magicp = SH_ARCH_MAGIC_WINCE;
   2875 #else
   2876       if (bfd_big_endian (abfd))
   2877 	*magicp = SH_ARCH_MAGIC_BIG;
   2878       else
   2879 	*magicp = SH_ARCH_MAGIC_LITTLE;
   2880 #endif
   2881       return true;
   2882 #endif
   2883 
   2884 #ifdef MIPS_ARCH_MAGIC_WINCE
   2885     case bfd_arch_mips:
   2886       *magicp = MIPS_ARCH_MAGIC_WINCE;
   2887       return true;
   2888 #endif
   2889 
   2890 #ifdef SPARCMAGIC
   2891     case bfd_arch_sparc:
   2892       *magicp = SPARCMAGIC;
   2893 #ifdef LYNXOS
   2894       /* Just overwrite the usual value if we're doing Lynx.  */
   2895       *magicp = LYNXCOFFMAGIC;
   2896 #endif
   2897       return true;
   2898 #endif
   2899 
   2900 #ifdef RS6000COFF_C
   2901     case bfd_arch_rs6000:
   2902     case bfd_arch_powerpc:
   2903       BFD_ASSERT (bfd_get_flavour (abfd) == bfd_target_xcoff_flavour);
   2904       *magicp = bfd_xcoff_magic_number (abfd);
   2905       return true;
   2906 #endif
   2907 
   2908 #ifdef MCOREMAGIC
   2909     case bfd_arch_mcore:
   2910       * magicp = MCOREMAGIC;
   2911       return true;
   2912 #endif
   2913 
   2914     default:			/* Unknown architecture.  */
   2915       break;
   2916     }
   2917 
   2918   return false;
   2919 }
   2920 
   2921 static bool
   2922 coff_set_arch_mach (bfd * abfd,
   2923 		    enum bfd_architecture arch,
   2924 		    unsigned long machine)
   2925 {
   2926   unsigned dummy1;
   2927   unsigned short dummy2;
   2928 
   2929   if (! bfd_default_set_arch_mach (abfd, arch, machine))
   2930     return false;
   2931 
   2932   if (arch != bfd_arch_unknown
   2933       && ! coff_set_flags (abfd, &dummy1, &dummy2))
   2934     return false;		/* We can't represent this type.  */
   2935 
   2936   return true;			/* We're easy...  */
   2937 }
   2938 
   2939 #ifdef COFF_IMAGE_WITH_PE
   2940 
   2941 /* This is used to sort sections by VMA, as required by PE image
   2942    files.  */
   2943 
   2944 static int
   2945 sort_by_secaddr (const void * arg1, const void * arg2)
   2946 {
   2947   const asection *a = *(const asection **) arg1;
   2948   const asection *b = *(const asection **) arg2;
   2949 
   2950   if (a->vma < b->vma)
   2951     return -1;
   2952   else if (a->vma > b->vma)
   2953     return 1;
   2954 
   2955   return 0;
   2956 }
   2957 
   2958 #endif /* COFF_IMAGE_WITH_PE */
   2959 
   2960 /* Calculate the file position for each section.  */
   2961 
   2962 #define ALIGN_SECTIONS_IN_FILE
   2963 #ifdef TICOFF
   2964 #undef ALIGN_SECTIONS_IN_FILE
   2965 #endif
   2966 
   2967 static bool
   2968 coff_compute_section_file_positions (bfd * abfd)
   2969 {
   2970   asection *current;
   2971   file_ptr sofar = bfd_coff_filhsz (abfd);
   2972   bool align_adjust;
   2973   unsigned int target_index;
   2974 #ifdef ALIGN_SECTIONS_IN_FILE
   2975   asection *previous = NULL;
   2976   file_ptr old_sofar;
   2977 #endif
   2978 
   2979 #ifdef COFF_IMAGE_WITH_PE
   2980   unsigned int page_size;
   2981 
   2982   if (coff_data (abfd)->link_info
   2983       || (pe_data (abfd) && pe_data (abfd)->pe_opthdr.FileAlignment))
   2984     {
   2985       page_size = pe_data (abfd)->pe_opthdr.FileAlignment;
   2986 
   2987       /* If no file alignment has been set, default to one.
   2988 	 This repairs 'ld -r' for arm-wince-pe target.  */
   2989       if (page_size == 0)
   2990 	page_size = 1;
   2991     }
   2992   else
   2993     page_size = PE_DEF_FILE_ALIGNMENT;
   2994 #else
   2995 #ifdef COFF_PAGE_SIZE
   2996   unsigned int page_size = COFF_PAGE_SIZE;
   2997 #endif
   2998 #endif
   2999 
   3000 #ifdef RS6000COFF_C
   3001   /* On XCOFF, if we have symbols, set up the .debug section.  */
   3002   if (bfd_get_symcount (abfd) > 0)
   3003     {
   3004       bfd_size_type sz;
   3005       bfd_size_type i, symcount;
   3006       asymbol **symp;
   3007 
   3008       sz = 0;
   3009       symcount = bfd_get_symcount (abfd);
   3010       for (symp = abfd->outsymbols, i = 0; i < symcount; symp++, i++)
   3011 	{
   3012 	  coff_symbol_type *cf;
   3013 
   3014 	  cf = coff_symbol_from (*symp);
   3015 	  if (cf != NULL
   3016 	      && cf->native != NULL
   3017 	      && cf->native->is_sym
   3018 	      && SYMNAME_IN_DEBUG (&cf->native->u.syment))
   3019 	    {
   3020 	      size_t len;
   3021 
   3022 	      len = strlen (bfd_asymbol_name (*symp));
   3023 	      if (len > SYMNMLEN || bfd_coff_force_symnames_in_strings (abfd))
   3024 		sz += len + 1 + bfd_coff_debug_string_prefix_length (abfd);
   3025 	    }
   3026 	}
   3027       if (sz > 0)
   3028 	{
   3029 	  asection *dsec;
   3030 
   3031 	  dsec = bfd_make_section_old_way (abfd, DOT_DEBUG);
   3032 	  if (dsec == NULL)
   3033 	    abort ();
   3034 	  dsec->size = sz;
   3035 	  dsec->flags |= SEC_HAS_CONTENTS;
   3036 	}
   3037     }
   3038 #endif
   3039 
   3040   if (bfd_get_start_address (abfd))
   3041     /*  A start address may have been added to the original file. In this
   3042 	case it will need an optional header to record it.  */
   3043     abfd->flags |= EXEC_P;
   3044 
   3045   if (abfd->flags & EXEC_P)
   3046     sofar += bfd_coff_aoutsz (abfd);
   3047 #ifdef RS6000COFF_C
   3048   else if (xcoff_data (abfd)->full_aouthdr)
   3049     sofar += bfd_coff_aoutsz (abfd);
   3050   else
   3051     sofar += SMALL_AOUTSZ;
   3052 #endif
   3053 
   3054   sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
   3055 
   3056 #ifdef RS6000COFF_C
   3057   /* XCOFF handles overflows in the reloc and line number count fields
   3058      by allocating a new section header to hold the correct counts.  */
   3059   for (current = abfd->sections; current != NULL; current = current->next)
   3060     if (current->reloc_count >= 0xffff || current->lineno_count >= 0xffff)
   3061       sofar += bfd_coff_scnhsz (abfd);
   3062 #endif
   3063 
   3064 #ifdef COFF_IMAGE_WITH_PE
   3065   {
   3066     /* PE requires the sections to be in memory order when listed in
   3067        the section headers.  It also does not like empty loadable
   3068        sections.  The sections apparently do not have to be in the
   3069        right order in the image file itself, but we do need to get the
   3070        target_index values right.  */
   3071 
   3072     unsigned int count;
   3073     asection **section_list;
   3074     unsigned int i;
   3075     bfd_size_type amt;
   3076 
   3077 #ifdef COFF_PAGE_SIZE
   3078     /* Clear D_PAGED if section / file alignment aren't suitable for
   3079        paging at COFF_PAGE_SIZE granularity.  */
   3080    if (pe_data (abfd)->pe_opthdr.SectionAlignment < COFF_PAGE_SIZE
   3081        || page_size < COFF_PAGE_SIZE)
   3082      abfd->flags &= ~D_PAGED;
   3083 #endif
   3084 
   3085     count = 0;
   3086     for (current = abfd->sections; current != NULL; current = current->next)
   3087       ++count;
   3088 
   3089     /* We allocate an extra cell to simplify the final loop.  */
   3090     amt = sizeof (struct asection *) * (count + 1);
   3091     section_list = (asection **) bfd_malloc (amt);
   3092     if (section_list == NULL)
   3093       return false;
   3094 
   3095     i = 0;
   3096     for (current = abfd->sections; current != NULL; current = current->next)
   3097       {
   3098 	section_list[i] = current;
   3099 	++i;
   3100       }
   3101     section_list[i] = NULL;
   3102 
   3103     qsort (section_list, count, sizeof (asection *), sort_by_secaddr);
   3104 
   3105     /* Rethread the linked list into sorted order; at the same time,
   3106        assign target_index values.  */
   3107     target_index = 1;
   3108     abfd->sections = NULL;
   3109     abfd->section_last = NULL;
   3110     for (i = 0; i < count; i++)
   3111       {
   3112 	current = section_list[i];
   3113 	bfd_section_list_append (abfd, current);
   3114 
   3115 	/* Later, if the section has zero size, we'll be throwing it
   3116 	   away, so we don't want to number it now.  Note that having
   3117 	   a zero size and having real contents are different
   3118 	   concepts: .bss has no contents, but (usually) non-zero
   3119 	   size.  */
   3120 	if (current->size == 0)
   3121 	  {
   3122 	    /* Discard.  However, it still might have (valid) symbols
   3123 	       in it, so arbitrarily set it to section 1 (indexing is
   3124 	       1-based here; usually .text).  __end__ and other
   3125 	       contents of .endsection really have this happen.
   3126 	       FIXME: This seems somewhat dubious.  */
   3127 	    current->target_index = 1;
   3128 	  }
   3129 	else
   3130 	  current->target_index = target_index++;
   3131       }
   3132 
   3133     free (section_list);
   3134   }
   3135 #else /* ! COFF_IMAGE_WITH_PE */
   3136   {
   3137     /* Set the target_index field.  */
   3138     target_index = 1;
   3139     for (current = abfd->sections; current != NULL; current = current->next)
   3140       current->target_index = target_index++;
   3141   }
   3142 #endif /* ! COFF_IMAGE_WITH_PE */
   3143 
   3144   if (target_index >= bfd_coff_max_nscns (abfd))
   3145     {
   3146       bfd_set_error (bfd_error_file_too_big);
   3147       _bfd_error_handler
   3148 	/* xgettext:c-format */
   3149 	(_("%pB: too many sections (%d)"), abfd, target_index);
   3150       return false;
   3151     }
   3152 
   3153   align_adjust = false;
   3154   for (current = abfd->sections;
   3155        current != NULL;
   3156        current = current->next)
   3157     {
   3158 #ifdef COFF_IMAGE_WITH_PE
   3159       /* With PE we have to pad each section to be a multiple of its
   3160 	 page size too, and remember both sizes.  */
   3161       if (coff_section_data (abfd, current) == NULL)
   3162 	{
   3163 	  size_t amt = sizeof (struct coff_section_tdata);
   3164 
   3165 	  current->used_by_bfd = bfd_zalloc (abfd, amt);
   3166 	  if (current->used_by_bfd == NULL)
   3167 	    return false;
   3168 	}
   3169       if (pei_section_data (abfd, current) == NULL)
   3170 	{
   3171 	  size_t amt = sizeof (struct pei_section_tdata);
   3172 
   3173 	  coff_section_data (abfd, current)->tdata = bfd_zalloc (abfd, amt);
   3174 	  if (coff_section_data (abfd, current)->tdata == NULL)
   3175 	    return false;
   3176 	}
   3177       if (pei_section_data (abfd, current)->virt_size == 0)
   3178 	pei_section_data (abfd, current)->virt_size = current->size;
   3179 #endif
   3180 
   3181       /* Only deal with sections which have contents.  */
   3182       if (!(current->flags & SEC_HAS_CONTENTS))
   3183 	continue;
   3184 
   3185       current->rawsize = current->size;
   3186 
   3187 #ifdef COFF_IMAGE_WITH_PE
   3188       /* Make sure we skip empty sections in a PE image.  */
   3189       if (current->size == 0)
   3190 	continue;
   3191 #endif
   3192 
   3193       /* Align the sections in the file to the same boundary on
   3194 	 which they are aligned in virtual memory.  */
   3195 #ifdef ALIGN_SECTIONS_IN_FILE
   3196       if ((abfd->flags & EXEC_P) != 0)
   3197 	{
   3198 	  /* Make sure this section is aligned on the right boundary - by
   3199 	     padding the previous section up if necessary.  */
   3200 	  old_sofar = sofar;
   3201 
   3202 #ifdef COFF_IMAGE_WITH_PE
   3203 	  sofar = BFD_ALIGN (sofar, page_size);
   3204 #else
   3205 	  sofar = BFD_ALIGN (sofar, (bfd_vma) 1 << current->alignment_power);
   3206 #endif
   3207 
   3208 #ifdef RS6000COFF_C
   3209 	  /* Make sure the file offset and the vma of .text/.data are at the
   3210 	     same page offset, so that the file can be mmap'ed without being
   3211 	     relocated.  Failing that, AIX is able to load and execute the
   3212 	     program, but it will be silently relocated (possible as
   3213 	     executables are PIE).  But the relocation is slightly costly and
   3214 	     complexify the use of addr2line or gdb.  So better to avoid it,
   3215 	     like does the native linker.  Usually gnu ld makes sure that
   3216 	     the vma of .text is the file offset so this issue shouldn't
   3217 	     appear unless you are stripping such an executable.
   3218 
   3219 	     AIX loader checks the text section alignment of (vma - filepos),
   3220 	     and the native linker doesn't try to align the text sections.
   3221 	     For example:
   3222 
   3223 	     0 .text	     000054cc  10000128	 10000128  00000128  2**5
   3224 			     CONTENTS, ALLOC, LOAD, CODE
   3225 
   3226 	     Don't perform the above tweak if the previous one is .tdata,
   3227 	     as it will increase the memory allocated for every threads
   3228 	     created and not just improve performances with gdb.
   3229 	  */
   3230 
   3231 	  if ((!strcmp (current->name, _TEXT)
   3232 	       || !strcmp (current->name, _DATA))
   3233 	      && (previous == NULL || strcmp(previous->name, _TDATA)))
   3234 	    {
   3235 	      bfd_vma align = 4096;
   3236 	      bfd_vma sofar_off = sofar % align;
   3237 	      bfd_vma vma_off = current->vma % align;
   3238 
   3239 	      if (vma_off > sofar_off)
   3240 		sofar += vma_off - sofar_off;
   3241 	      else if (vma_off < sofar_off)
   3242 		sofar += align + vma_off - sofar_off;
   3243 	    }
   3244 #endif
   3245 	  if (previous != NULL)
   3246 	    previous->size += sofar - old_sofar;
   3247 	}
   3248 
   3249 #endif
   3250 
   3251       /* In demand paged files the low order bits of the file offset
   3252 	 must match the low order bits of the virtual address.  */
   3253 #ifdef COFF_PAGE_SIZE
   3254       if ((abfd->flags & D_PAGED) != 0
   3255 	  && (current->flags & SEC_ALLOC) != 0)
   3256 	sofar += (current->vma - (bfd_vma) sofar) % page_size;
   3257 #endif
   3258       current->filepos = sofar;
   3259 
   3260 #ifdef COFF_IMAGE_WITH_PE
   3261       /* Set the padded size.  */
   3262       current->size = (current->size + page_size - 1) & -page_size;
   3263 #endif
   3264 
   3265       sofar += current->size;
   3266 
   3267 #ifdef ALIGN_SECTIONS_IN_FILE
   3268       /* Make sure that this section is of the right size too.  */
   3269       if ((abfd->flags & EXEC_P) == 0)
   3270 	{
   3271 	  bfd_size_type old_size;
   3272 
   3273 	  old_size = current->size;
   3274 	  current->size = BFD_ALIGN (current->size,
   3275 				     (bfd_vma) 1 << current->alignment_power);
   3276 	  align_adjust = current->size != old_size;
   3277 	  sofar += current->size - old_size;
   3278 	}
   3279       else
   3280 	{
   3281 	  old_sofar = sofar;
   3282 #ifdef COFF_IMAGE_WITH_PE
   3283 	  sofar = BFD_ALIGN (sofar, page_size);
   3284 #else
   3285 	  sofar = BFD_ALIGN (sofar, (bfd_vma) 1 << current->alignment_power);
   3286 #endif
   3287 	  align_adjust = sofar != old_sofar;
   3288 	  current->size += sofar - old_sofar;
   3289 	}
   3290 #endif
   3291 
   3292 #ifdef COFF_IMAGE_WITH_PE
   3293       /* For PE we need to make sure we pad out to the aligned
   3294 	 size, in case the caller only writes out data to the
   3295 	 unaligned size.  */
   3296       if (pei_section_data (abfd, current)->virt_size < current->size)
   3297 	align_adjust = true;
   3298 #endif
   3299 
   3300 #ifdef _LIB
   3301       /* Force .lib sections to start at zero.  The vma is then
   3302 	 incremented in coff_set_section_contents.  This is right for
   3303 	 SVR3.2.  */
   3304       if (strcmp (current->name, _LIB) == 0)
   3305 	bfd_set_section_vma (current, 0);
   3306 #endif
   3307 
   3308 #ifdef ALIGN_SECTIONS_IN_FILE
   3309       previous = current;
   3310 #endif
   3311     }
   3312 
   3313   /* It is now safe to write to the output file.  If we needed an
   3314      alignment adjustment for the last section, then make sure that
   3315      there is a byte at offset sofar.  If there are no symbols and no
   3316      relocs, then nothing follows the last section.  If we don't force
   3317      the last byte out, then the file may appear to be truncated.  */
   3318   if (align_adjust)
   3319     {
   3320       bfd_byte b;
   3321 
   3322       b = 0;
   3323       if (bfd_seek (abfd, sofar - 1, SEEK_SET) != 0
   3324 	  || bfd_bwrite (&b, (bfd_size_type) 1, abfd) != 1)
   3325 	return false;
   3326     }
   3327 
   3328   /* Make sure the relocations are aligned.  We don't need to make
   3329      sure that this byte exists, because it will only matter if there
   3330      really are relocs.  */
   3331   sofar = BFD_ALIGN (sofar,
   3332 		     (bfd_vma) 1 << COFF_DEFAULT_SECTION_ALIGNMENT_POWER);
   3333 
   3334   obj_relocbase (abfd) = sofar;
   3335   abfd->output_has_begun = true;
   3336 
   3337   return true;
   3338 }
   3339 
   3340 #ifdef COFF_IMAGE_WITH_PE
   3341 
   3342 static bool
   3343 coff_read_word (bfd *abfd, unsigned int *value, unsigned int *pelength)
   3344 {
   3345   unsigned char b[2];
   3346   int status;
   3347 
   3348   status = bfd_bread (b, (bfd_size_type) 2, abfd);
   3349   if (status < 1)
   3350     {
   3351       *value = 0;
   3352       return false;
   3353     }
   3354 
   3355   if (status == 1)
   3356     *value = (unsigned int) b[0];
   3357   else
   3358     *value = (unsigned int) (b[0] + (b[1] << 8));
   3359 
   3360   *pelength += status;
   3361 
   3362   return true;
   3363 }
   3364 
   3365 static unsigned int
   3366 coff_compute_checksum (bfd *abfd, unsigned int *pelength)
   3367 {
   3368   bool more_data;
   3369   file_ptr filepos;
   3370   unsigned int value;
   3371   unsigned int total;
   3372 
   3373   total = 0;
   3374   *pelength = 0;
   3375   filepos = (file_ptr) 0;
   3376 
   3377   do
   3378     {
   3379       if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
   3380 	return 0;
   3381 
   3382       more_data = coff_read_word (abfd, &value, pelength);
   3383       total += value;
   3384       total = 0xffff & (total + (total >> 0x10));
   3385       filepos += 2;
   3386     }
   3387   while (more_data);
   3388 
   3389   return (0xffff & (total + (total >> 0x10)));
   3390 }
   3391 
   3392 static bool
   3393 coff_apply_checksum (bfd *abfd)
   3394 {
   3395   unsigned int computed;
   3396   unsigned int checksum = 0;
   3397   unsigned int peheader;
   3398   unsigned int pelength;
   3399 
   3400   if (bfd_seek (abfd, 0x3c, SEEK_SET) != 0)
   3401     return false;
   3402 
   3403   if (!coff_read_word (abfd, &peheader, &pelength))
   3404     return false;
   3405 
   3406   if (bfd_seek (abfd, peheader + 0x58, SEEK_SET) != 0)
   3407     return false;
   3408 
   3409   checksum = 0;
   3410   bfd_bwrite (&checksum, (bfd_size_type) 4, abfd);
   3411 
   3412   if (bfd_seek (abfd, peheader, SEEK_SET) != 0)
   3413     return false;
   3414 
   3415   computed = coff_compute_checksum (abfd, &pelength);
   3416 
   3417   checksum = computed + pelength;
   3418 
   3419   if (bfd_seek (abfd, peheader + 0x58, SEEK_SET) != 0)
   3420     return false;
   3421 
   3422   bfd_bwrite (&checksum, (bfd_size_type) 4, abfd);
   3423 
   3424   return true;
   3425 }
   3426 
   3427 #endif /* COFF_IMAGE_WITH_PE */
   3428 
   3429 static bool
   3430 coff_write_object_contents (bfd * abfd)
   3431 {
   3432   asection *current;
   3433   bool hasrelocs = false;
   3434   bool haslinno = false;
   3435 #ifdef COFF_IMAGE_WITH_PE
   3436   bool hasdebug = false;
   3437 #endif
   3438   file_ptr scn_base;
   3439   file_ptr reloc_base;
   3440   file_ptr lineno_base;
   3441   file_ptr sym_base;
   3442   unsigned long reloc_size = 0, reloc_count = 0;
   3443   unsigned long lnno_size = 0;
   3444   bool long_section_names;
   3445   asection *text_sec = NULL;
   3446   asection *data_sec = NULL;
   3447   asection *bss_sec = NULL;
   3448 #ifdef RS6000COFF_C
   3449   asection *tdata_sec = NULL;
   3450   asection *tbss_sec = NULL;
   3451 #endif
   3452   struct internal_filehdr internal_f;
   3453   struct internal_aouthdr internal_a;
   3454 #ifdef COFF_LONG_SECTION_NAMES
   3455   size_t string_size = STRING_SIZE_SIZE;
   3456 #endif
   3457 
   3458   bfd_set_error (bfd_error_system_call);
   3459 
   3460   /* Make a pass through the symbol table to count line number entries and
   3461      put them into the correct asections.  */
   3462   lnno_size = coff_count_linenumbers (abfd) * bfd_coff_linesz (abfd);
   3463 
   3464   if (! abfd->output_has_begun)
   3465     {
   3466       if (! coff_compute_section_file_positions (abfd))
   3467 	return false;
   3468     }
   3469 
   3470   reloc_base = obj_relocbase (abfd);
   3471 
   3472   /* Work out the size of the reloc and linno areas.  */
   3473 
   3474   for (current = abfd->sections; current != NULL; current =
   3475        current->next)
   3476     {
   3477 #ifdef COFF_WITH_EXTENDED_RELOC_COUNTER
   3478       /* We store the actual reloc count in the first reloc's addr.  */
   3479       if ((obj_pe (abfd) || obj_go32 (abfd)) && current->reloc_count >= 0xffff)
   3480 	reloc_count ++;
   3481 #endif
   3482       reloc_count += current->reloc_count;
   3483     }
   3484 
   3485   reloc_size = reloc_count * bfd_coff_relsz (abfd);
   3486 
   3487   lineno_base = reloc_base + reloc_size;
   3488   sym_base = lineno_base + lnno_size;
   3489 
   3490   /* Indicate in each section->line_filepos its actual file address.  */
   3491   for (current = abfd->sections; current != NULL; current =
   3492        current->next)
   3493     {
   3494       if (current->lineno_count)
   3495 	{
   3496 	  current->line_filepos = lineno_base;
   3497 	  current->moving_line_filepos = lineno_base;
   3498 	  lineno_base += current->lineno_count * bfd_coff_linesz (abfd);
   3499 	}
   3500       else
   3501 	current->line_filepos = 0;
   3502 
   3503       if (current->reloc_count)
   3504 	{
   3505 	  current->rel_filepos = reloc_base;
   3506 	  reloc_base += current->reloc_count * bfd_coff_relsz (abfd);
   3507 #ifdef COFF_WITH_EXTENDED_RELOC_COUNTER
   3508 	  /* Extra reloc to hold real count.  */
   3509 	  if ((obj_pe (abfd) || obj_go32 (abfd)) && current->reloc_count >= 0xffff)
   3510 	    reloc_base += bfd_coff_relsz (abfd);
   3511 #endif
   3512 	}
   3513       else
   3514 	current->rel_filepos = 0;
   3515     }
   3516 
   3517   /* Write section headers to the file.  */
   3518   internal_f.f_nscns = 0;
   3519 
   3520   if ((abfd->flags & EXEC_P) != 0)
   3521     scn_base = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
   3522   else
   3523     {
   3524       scn_base = bfd_coff_filhsz (abfd);
   3525 #ifdef RS6000COFF_C
   3526 #ifndef XCOFF64
   3527       if (xcoff_data (abfd)->full_aouthdr)
   3528 	scn_base += bfd_coff_aoutsz (abfd);
   3529       else
   3530 	scn_base += SMALL_AOUTSZ;
   3531 #endif
   3532 #endif
   3533     }
   3534 
   3535   if (bfd_seek (abfd, scn_base, SEEK_SET) != 0)
   3536     return false;
   3537 
   3538   long_section_names = false;
   3539   for (current = abfd->sections;
   3540        current != NULL;
   3541        current = current->next)
   3542     {
   3543       struct internal_scnhdr section;
   3544 #ifdef COFF_IMAGE_WITH_PE
   3545       bool is_reloc_section = false;
   3546 
   3547       if (strcmp (current->name, DOT_RELOC) == 0)
   3548 	{
   3549 	  is_reloc_section = true;
   3550 	  hasrelocs = true;
   3551 	  pe_data (abfd)->has_reloc_section = 1;
   3552 	}
   3553 #endif
   3554 
   3555       internal_f.f_nscns++;
   3556 
   3557       strncpy (section.s_name, current->name, SCNNMLEN);
   3558 
   3559 #ifdef COFF_LONG_SECTION_NAMES
   3560       /* Handle long section names as in PE.  This must be compatible
   3561 	 with the code in coff_write_symbols and _bfd_coff_final_link.  */
   3562       if (bfd_coff_long_section_names (abfd))
   3563 	{
   3564 	  size_t len;
   3565 
   3566 	  len = strlen (current->name);
   3567 	  if (len > SCNNMLEN)
   3568 	    {
   3569 	      /* The s_name field is defined to be NUL-padded but need not be
   3570 		 NUL-terminated.  We use a temporary buffer so that we can still
   3571 		 sprintf all eight chars without splatting a terminating NUL
   3572 		 over the first byte of the following member (s_paddr).  */
   3573 	      /* PR 21096: The +20 is to stop a bogus warning from gcc7 about
   3574 		 a possible buffer overflow.  */
   3575 	      char s_name_buf[SCNNMLEN + 1 + 20];
   3576 
   3577 	      /* An inherent limitation of the /nnnnnnn notation used to indicate
   3578 		 the offset of the long name in the string table is that we
   3579 		 cannot address entries beyone the ten million byte boundary.  */
   3580 	      if (string_size >= 10000000)
   3581 		{
   3582 		  bfd_set_error (bfd_error_file_too_big);
   3583 		  _bfd_error_handler
   3584 		    /* xgettext:c-format */
   3585 		    (_("%pB: section %pA: string table overflow at offset %ld"),
   3586 		    abfd, current, (unsigned long) string_size);
   3587 		  return false;
   3588 		}
   3589 
   3590 	      /* We do not need to use snprintf here as we have already verfied
   3591 		 that string_size is not too big, plus we have an overlarge
   3592 		 buffer, just in case.  */
   3593 	      sprintf (s_name_buf, "/%lu", (unsigned long) string_size);
   3594 	      /* Then strncpy takes care of any padding for us.  */
   3595 	      strncpy (section.s_name, s_name_buf, SCNNMLEN);
   3596 	      string_size += len + 1;
   3597 	      long_section_names = true;
   3598 	    }
   3599 	}
   3600 #endif
   3601 
   3602 #ifdef _LIB
   3603       /* Always set s_vaddr of .lib to 0.  This is right for SVR3.2
   3604 	 Ian Taylor <ian (at) cygnus.com>.  */
   3605       if (strcmp (current->name, _LIB) == 0)
   3606 	section.s_vaddr = 0;
   3607       else
   3608 #endif
   3609       section.s_vaddr = current->vma;
   3610       section.s_paddr = current->lma;
   3611       section.s_size =  current->size;
   3612 #ifdef coff_get_section_load_page
   3613       section.s_page = coff_get_section_load_page (current);
   3614 #else
   3615       section.s_page = 0;
   3616 #endif
   3617 
   3618 #ifdef COFF_WITH_PE
   3619       section.s_paddr = 0;
   3620 #endif
   3621 #ifdef COFF_IMAGE_WITH_PE
   3622       /* Reminder: s_paddr holds the virtual size of the section.  */
   3623       if (coff_section_data (abfd, current) != NULL
   3624 	  && pei_section_data (abfd, current) != NULL)
   3625 	section.s_paddr = pei_section_data (abfd, current)->virt_size;
   3626       else
   3627 	section.s_paddr = 0;
   3628 #endif
   3629 
   3630       /* If this section has no size or is unloadable then the scnptr
   3631 	 will be 0 too.  */
   3632       if (current->size == 0
   3633 	  || (current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
   3634 	section.s_scnptr = 0;
   3635       else
   3636 	section.s_scnptr = current->filepos;
   3637 
   3638       section.s_relptr = current->rel_filepos;
   3639       section.s_lnnoptr = current->line_filepos;
   3640       section.s_nreloc = current->reloc_count;
   3641       section.s_nlnno = current->lineno_count;
   3642 #ifndef COFF_IMAGE_WITH_PE
   3643       /* In PEI, relocs come in the .reloc section.  */
   3644       if (current->reloc_count != 0)
   3645 	hasrelocs = true;
   3646 #endif
   3647       if (current->lineno_count != 0)
   3648 	haslinno = true;
   3649 #ifdef COFF_IMAGE_WITH_PE
   3650       if ((current->flags & SEC_DEBUGGING) != 0
   3651 	  && ! is_reloc_section)
   3652 	hasdebug = true;
   3653 #endif
   3654 
   3655 #ifdef RS6000COFF_C
   3656 #ifndef XCOFF64
   3657       /* Indicate the use of an XCOFF overflow section header.  */
   3658       if (current->reloc_count >= 0xffff || current->lineno_count >= 0xffff)
   3659 	{
   3660 	  section.s_nreloc = 0xffff;
   3661 	  section.s_nlnno = 0xffff;
   3662 	}
   3663 #endif
   3664 #endif
   3665 
   3666       section.s_flags = sec_to_styp_flags (current->name, current->flags);
   3667 
   3668       if (!strcmp (current->name, _TEXT))
   3669 	text_sec = current;
   3670       else if (!strcmp (current->name, _DATA))
   3671 	data_sec = current;
   3672       else if (!strcmp (current->name, _BSS))
   3673 	bss_sec = current;
   3674 #ifdef RS6000COFF_C
   3675       else if (!strcmp (current->name, _TDATA))
   3676 	tdata_sec = current;
   3677       else if (!strcmp (current->name, _TBSS))
   3678 	tbss_sec = current;
   3679 #endif
   3680 
   3681 
   3682 #ifdef COFF_ENCODE_ALIGNMENT
   3683       if (COFF_ENCODE_ALIGNMENT (abfd, section, current->alignment_power)
   3684 	  && (COFF_DECODE_ALIGNMENT (section.s_flags)
   3685 	      != current->alignment_power))
   3686 	{
   3687 	  bool warn = (coff_data (abfd)->link_info
   3688 		       && !bfd_link_relocatable (coff_data (abfd)->link_info));
   3689 
   3690 	  _bfd_error_handler
   3691 	    /* xgettext:c-format */
   3692 	    (_("%pB:%s section %s: alignment 2**%u not representable"),
   3693 	     abfd, warn ? " warning:" : "", current->name,
   3694 	     current->alignment_power);
   3695 	  if (!warn)
   3696 	    {
   3697 	      bfd_set_error (bfd_error_nonrepresentable_section);
   3698 	      return false;
   3699 	    }
   3700 	}
   3701 #endif
   3702 
   3703 #ifdef COFF_IMAGE_WITH_PE
   3704       /* Suppress output of the sections if they are null.  ld
   3705 	 includes the bss and data sections even if there is no size
   3706 	 assigned to them.  NT loader doesn't like it if these section
   3707 	 headers are included if the sections themselves are not
   3708 	 needed.  See also coff_compute_section_file_positions.  */
   3709       if (section.s_size == 0)
   3710 	internal_f.f_nscns--;
   3711       else
   3712 #endif
   3713 	{
   3714 	  SCNHDR buff;
   3715 	  bfd_size_type amt = bfd_coff_scnhsz (abfd);
   3716 
   3717 	  if (bfd_coff_swap_scnhdr_out (abfd, &section, &buff) == 0
   3718 	      || bfd_bwrite (& buff, amt, abfd) != amt)
   3719 	    return false;
   3720 	}
   3721 
   3722 #ifdef COFF_WITH_PE
   3723       /* PE stores COMDAT section information in the symbol table.  If
   3724 	 this section is supposed to have some COMDAT info, track down
   3725 	 the symbol in the symbol table and modify it.  */
   3726       if ((current->flags & SEC_LINK_ONCE) != 0)
   3727 	{
   3728 	  unsigned int i, count;
   3729 	  asymbol **psym;
   3730 	  coff_symbol_type *csym = NULL;
   3731 	  asymbol **psymsec;
   3732 
   3733 	  psymsec = NULL;
   3734 	  count = bfd_get_symcount (abfd);
   3735 	  for (i = 0, psym = abfd->outsymbols; i < count; i++, psym++)
   3736 	    {
   3737 	      if ((*psym)->section != current)
   3738 		continue;
   3739 
   3740 	      /* Remember the location of the first symbol in this
   3741 		 section.  */
   3742 	      if (psymsec == NULL)
   3743 		psymsec = psym;
   3744 
   3745 	      /* See if this is the section symbol.  */
   3746 	      if (strcmp ((*psym)->name, current->name) == 0)
   3747 		{
   3748 		  csym = coff_symbol_from (*psym);
   3749 		  if (csym == NULL
   3750 		      || csym->native == NULL
   3751 		      || ! csym->native->is_sym
   3752 		      || csym->native->u.syment.n_numaux < 1
   3753 		      || csym->native->u.syment.n_sclass != C_STAT
   3754 		      || csym->native->u.syment.n_type != T_NULL)
   3755 		    continue;
   3756 
   3757 		  /* Here *PSYM is the section symbol for CURRENT.  */
   3758 
   3759 		  break;
   3760 		}
   3761 	    }
   3762 
   3763 	  /* Did we find it?
   3764 	     Note that we might not if we're converting the file from
   3765 	     some other object file format.  */
   3766 	  if (i < count)
   3767 	    {
   3768 	      combined_entry_type *aux;
   3769 
   3770 	      /* We don't touch the x_checksum field.  The
   3771 		 x_associated field is not currently supported.  */
   3772 
   3773 	      aux = csym->native + 1;
   3774 	      BFD_ASSERT (! aux->is_sym);
   3775 	      switch (current->flags & SEC_LINK_DUPLICATES)
   3776 		{
   3777 		case SEC_LINK_DUPLICATES_DISCARD:
   3778 		  aux->u.auxent.x_scn.x_comdat = IMAGE_COMDAT_SELECT_ANY;
   3779 		  break;
   3780 
   3781 		case SEC_LINK_DUPLICATES_ONE_ONLY:
   3782 		  aux->u.auxent.x_scn.x_comdat =
   3783 		    IMAGE_COMDAT_SELECT_NODUPLICATES;
   3784 		  break;
   3785 
   3786 		case SEC_LINK_DUPLICATES_SAME_SIZE:
   3787 		  aux->u.auxent.x_scn.x_comdat =
   3788 		    IMAGE_COMDAT_SELECT_SAME_SIZE;
   3789 		  break;
   3790 
   3791 		case SEC_LINK_DUPLICATES_SAME_CONTENTS:
   3792 		  aux->u.auxent.x_scn.x_comdat =
   3793 		    IMAGE_COMDAT_SELECT_EXACT_MATCH;
   3794 		  break;
   3795 		}
   3796 
   3797 	      /* The COMDAT symbol must be the first symbol from this
   3798 		 section in the symbol table.  In order to make this
   3799 		 work, we move the COMDAT symbol before the first
   3800 		 symbol we found in the search above.  It's OK to
   3801 		 rearrange the symbol table at this point, because
   3802 		 coff_renumber_symbols is going to rearrange it
   3803 		 further and fix up all the aux entries.  */
   3804 	      if (psym != psymsec)
   3805 		{
   3806 		  asymbol *hold;
   3807 		  asymbol **pcopy;
   3808 
   3809 		  hold = *psym;
   3810 		  for (pcopy = psym; pcopy > psymsec; pcopy--)
   3811 		    pcopy[0] = pcopy[-1];
   3812 		  *psymsec = hold;
   3813 		}
   3814 	    }
   3815 	}
   3816 #endif /* COFF_WITH_PE */
   3817     }
   3818 
   3819 #ifdef RS6000COFF_C
   3820 #ifndef XCOFF64
   3821   /* XCOFF handles overflows in the reloc and line number count fields
   3822      by creating a new section header to hold the correct values.  */
   3823   for (current = abfd->sections; current != NULL; current = current->next)
   3824     {
   3825       if (current->reloc_count >= 0xffff || current->lineno_count >= 0xffff)
   3826 	{
   3827 	  struct internal_scnhdr scnhdr;
   3828 	  SCNHDR buff;
   3829 	  bfd_size_type amt;
   3830 
   3831 	  internal_f.f_nscns++;
   3832 	  memcpy (scnhdr.s_name, ".ovrflo", 8);
   3833 	  scnhdr.s_paddr = current->reloc_count;
   3834 	  scnhdr.s_vaddr = current->lineno_count;
   3835 	  scnhdr.s_size = 0;
   3836 	  scnhdr.s_scnptr = 0;
   3837 	  scnhdr.s_relptr = current->rel_filepos;
   3838 	  scnhdr.s_lnnoptr = current->line_filepos;
   3839 	  scnhdr.s_nreloc = current->target_index;
   3840 	  scnhdr.s_nlnno = current->target_index;
   3841 	  scnhdr.s_flags = STYP_OVRFLO;
   3842 	  amt = bfd_coff_scnhsz (abfd);
   3843 	  if (bfd_coff_swap_scnhdr_out (abfd, &scnhdr, &buff) == 0
   3844 	      || bfd_bwrite (& buff, amt, abfd) != amt)
   3845 	    return false;
   3846 	}
   3847     }
   3848 #endif
   3849 #endif
   3850 
   3851 #if defined (COFF_GO32_EXE) || defined (COFF_GO32)
   3852   /* Pad section headers.  */
   3853   if ((abfd->flags & EXEC_P) && abfd->sections != NULL)
   3854     {
   3855       file_ptr cur_ptr = scn_base
   3856 			 + abfd->section_count * bfd_coff_scnhsz (abfd);
   3857       long fill_size = (abfd->sections->filepos - cur_ptr);
   3858       bfd_byte *b = bfd_zmalloc (fill_size);
   3859       if (b)
   3860 	{
   3861 	  bfd_bwrite (b, fill_size, abfd);
   3862 	  free (b);
   3863 	}
   3864     }
   3865 #endif
   3866 
   3867   /* OK, now set up the filehdr...  */
   3868 
   3869   /* Don't include the internal abs section in the section count */
   3870 
   3871   /* We will NOT put a fucking timestamp in the header here. Every time you
   3872      put it back, I will come in and take it out again.  I'm sorry.  This
   3873      field does not belong here.  We fill it with a 0 so it compares the
   3874      same but is not a reasonable time. -- gnu (at) cygnus.com  */
   3875   internal_f.f_timdat = 0;
   3876   internal_f.f_flags = 0;
   3877 
   3878   if (abfd->flags & EXEC_P)
   3879     internal_f.f_opthdr = bfd_coff_aoutsz (abfd);
   3880   else
   3881     {
   3882       internal_f.f_opthdr = 0;
   3883 #ifdef RS6000COFF_C
   3884 #ifndef XCOFF64
   3885       if (xcoff_data (abfd)->full_aouthdr)
   3886 	internal_f.f_opthdr = bfd_coff_aoutsz (abfd);
   3887       else
   3888 	internal_f.f_opthdr = SMALL_AOUTSZ;
   3889 #endif
   3890 #endif
   3891     }
   3892 
   3893   if (!hasrelocs)
   3894     internal_f.f_flags |= F_RELFLG;
   3895   if (!haslinno)
   3896     internal_f.f_flags |= F_LNNO;
   3897   if (abfd->flags & EXEC_P)
   3898     internal_f.f_flags |= F_EXEC;
   3899 #ifdef COFF_IMAGE_WITH_PE
   3900   if (! hasdebug)
   3901     internal_f.f_flags |= IMAGE_FILE_DEBUG_STRIPPED;
   3902   if (pe_data (abfd)->real_flags & IMAGE_FILE_LARGE_ADDRESS_AWARE)
   3903     internal_f.f_flags |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
   3904 #endif
   3905 
   3906 #if !defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64) && !defined(COFF_WITH_peLoongArch64)
   3907 #ifdef COFF_WITH_PE
   3908   internal_f.f_flags |= IMAGE_FILE_32BIT_MACHINE;
   3909 #else
   3910   if (bfd_little_endian (abfd))
   3911     internal_f.f_flags |= F_AR32WR;
   3912   else
   3913     internal_f.f_flags |= F_AR32W;
   3914 #endif
   3915 #endif
   3916 
   3917 #ifdef TI_TARGET_ID
   3918   /* Target id is used in TI COFF v1 and later; COFF0 won't use this field,
   3919      but it doesn't hurt to set it internally.  */
   3920   internal_f.f_target_id = TI_TARGET_ID;
   3921 #endif
   3922 
   3923   /* FIXME, should do something about the other byte orders and
   3924      architectures.  */
   3925 
   3926 #ifdef RS6000COFF_C
   3927   if ((abfd->flags & DYNAMIC) != 0)
   3928     internal_f.f_flags |= F_SHROBJ;
   3929   if (bfd_get_section_by_name (abfd, _LOADER) != NULL)
   3930     internal_f.f_flags |= F_DYNLOAD;
   3931 #endif
   3932 
   3933   memset (&internal_a, 0, sizeof internal_a);
   3934 
   3935   /* Set up architecture-dependent stuff.  */
   3936   {
   3937     unsigned int magic = 0;
   3938     unsigned short flags = 0;
   3939 
   3940     coff_set_flags (abfd, &magic, &flags);
   3941     internal_f.f_magic = magic;
   3942     internal_f.f_flags |= flags;
   3943     /* ...and the "opt"hdr...  */
   3944 
   3945 #ifdef TICOFF_AOUT_MAGIC
   3946     internal_a.magic = TICOFF_AOUT_MAGIC;
   3947 #define __A_MAGIC_SET__
   3948 #endif
   3949 
   3950 #if defined(ARM)
   3951 #define __A_MAGIC_SET__
   3952     internal_a.magic = ZMAGIC;
   3953 #endif
   3954 
   3955 #if defined(AARCH64)
   3956 #define __A_MAGIC_SET__
   3957     internal_a.magic = ZMAGIC;
   3958 #endif
   3959 
   3960 #if defined(LOONGARCH64)
   3961 #define __A_MAGIC_SET__
   3962     internal_a.magic = ZMAGIC;
   3963 #endif
   3964 
   3965 #if defined MCORE_PE
   3966 #define __A_MAGIC_SET__
   3967     internal_a.magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
   3968 #endif
   3969 
   3970 #if defined(I386)
   3971 #define __A_MAGIC_SET__
   3972 #if defined LYNXOS
   3973     internal_a.magic = LYNXCOFFMAGIC;
   3974 #elif defined AMD64
   3975     internal_a.magic = IMAGE_NT_OPTIONAL_HDR64_MAGIC;
   3976 #else
   3977     internal_a.magic = ZMAGIC;
   3978 #endif
   3979 #endif /* I386 */
   3980 
   3981 #if defined(IA64)
   3982 #define __A_MAGIC_SET__
   3983     internal_a.magic = PE32PMAGIC;
   3984 #endif /* IA64 */
   3985 
   3986 #if defined(SPARC)
   3987 #define __A_MAGIC_SET__
   3988 #if defined(LYNXOS)
   3989     internal_a.magic = LYNXCOFFMAGIC;
   3990 #endif /* LYNXOS */
   3991 #endif /* SPARC */
   3992 
   3993 #ifdef RS6000COFF_C
   3994 #define __A_MAGIC_SET__
   3995     internal_a.magic = (abfd->flags & D_PAGED) ? RS6K_AOUTHDR_ZMAGIC :
   3996     (abfd->flags & WP_TEXT) ? RS6K_AOUTHDR_NMAGIC :
   3997     RS6K_AOUTHDR_OMAGIC;
   3998 #endif
   3999 
   4000 #if defined(SH) && defined(COFF_WITH_PE)
   4001 #define __A_MAGIC_SET__
   4002     internal_a.magic = SH_PE_MAGIC;
   4003 #endif
   4004 
   4005 #if defined(MIPS) && defined(COFF_WITH_PE)
   4006 #define __A_MAGIC_SET__
   4007     internal_a.magic = MIPS_PE_MAGIC;
   4008 #endif
   4009 
   4010 #ifndef __A_MAGIC_SET__
   4011 #include "Your aouthdr magic number is not being set!"
   4012 #else
   4013 #undef __A_MAGIC_SET__
   4014 #endif
   4015   }
   4016 
   4017 #ifdef RS6000COFF_C
   4018   /* XCOFF 32bit needs this to have new behaviour for n_type field.  */
   4019   internal_a.vstamp = 2;
   4020 #else
   4021   /* FIXME: Does anybody ever set this to another value?  */
   4022   internal_a.vstamp = 0;
   4023 #endif
   4024 
   4025   /* Now should write relocs, strings, syms.  */
   4026   obj_sym_filepos (abfd) = sym_base;
   4027 
   4028   if (bfd_get_symcount (abfd) != 0)
   4029     {
   4030       int firstundef;
   4031 
   4032       if (!coff_renumber_symbols (abfd, &firstundef))
   4033 	return false;
   4034       coff_mangle_symbols (abfd);
   4035       if (! coff_write_symbols (abfd))
   4036 	return false;
   4037       if (! coff_write_linenumbers (abfd))
   4038 	return false;
   4039       if (! coff_write_relocs (abfd, firstundef))
   4040 	return false;
   4041     }
   4042 #ifdef COFF_LONG_SECTION_NAMES
   4043   else if (long_section_names && ! obj_coff_strings_written (abfd))
   4044     {
   4045       /* If we have long section names we have to write out the string
   4046 	 table even if there are no symbols.  */
   4047       if (! coff_write_symbols (abfd))
   4048 	return false;
   4049     }
   4050 #endif
   4051   /* If bfd_get_symcount (abfd) != 0, then we are not using the COFF
   4052      backend linker, and obj_raw_syment_count is not valid until after
   4053      coff_write_symbols is called.  */
   4054   if (obj_raw_syment_count (abfd) != 0)
   4055     {
   4056       internal_f.f_symptr = sym_base;
   4057 #ifdef RS6000COFF_C
   4058       /* AIX appears to require that F_RELFLG not be set if there are
   4059 	 local symbols but no relocations.  */
   4060       internal_f.f_flags &=~ F_RELFLG;
   4061 #endif
   4062     }
   4063   else
   4064     {
   4065       if (long_section_names)
   4066 	internal_f.f_symptr = sym_base;
   4067       else
   4068 	internal_f.f_symptr = 0;
   4069       internal_f.f_flags |= F_LSYMS;
   4070     }
   4071 
   4072   if (text_sec)
   4073     {
   4074       internal_a.tsize = text_sec->size;
   4075       internal_a.text_start = internal_a.tsize ? text_sec->vma : 0;
   4076     }
   4077   if (data_sec)
   4078     {
   4079       internal_a.dsize = data_sec->size;
   4080       internal_a.data_start = internal_a.dsize ? data_sec->vma : 0;
   4081     }
   4082   if (bss_sec)
   4083     {
   4084       internal_a.bsize = bss_sec->size;
   4085       if (internal_a.bsize && bss_sec->vma < internal_a.data_start)
   4086 	internal_a.data_start = bss_sec->vma;
   4087     }
   4088 
   4089   internal_a.entry = bfd_get_start_address (abfd);
   4090   internal_f.f_nsyms = obj_raw_syment_count (abfd);
   4091 
   4092 #ifdef RS6000COFF_C
   4093   if (xcoff_data (abfd)->full_aouthdr)
   4094     {
   4095       bfd_vma toc;
   4096       asection *loader_sec;
   4097 
   4098       internal_a.vstamp = 2;
   4099 
   4100       internal_a.o_snentry = xcoff_data (abfd)->snentry;
   4101       if (internal_a.o_snentry == 0)
   4102 	internal_a.entry = (bfd_vma) -1;
   4103 
   4104       if (text_sec != NULL)
   4105 	{
   4106 	  internal_a.o_sntext = text_sec->target_index;
   4107 	  internal_a.o_algntext = bfd_section_alignment (text_sec);
   4108 	}
   4109       else
   4110 	{
   4111 	  internal_a.o_sntext = 0;
   4112 	  internal_a.o_algntext = 0;
   4113 	}
   4114       if (data_sec != NULL)
   4115 	{
   4116 	  internal_a.o_sndata = data_sec->target_index;
   4117 	  internal_a.o_algndata = bfd_section_alignment (data_sec);
   4118 	}
   4119       else
   4120 	{
   4121 	  internal_a.o_sndata = 0;
   4122 	  internal_a.o_algndata = 0;
   4123 	}
   4124       loader_sec = bfd_get_section_by_name (abfd, ".loader");
   4125       if (loader_sec != NULL)
   4126 	internal_a.o_snloader = loader_sec->target_index;
   4127       else
   4128 	internal_a.o_snloader = 0;
   4129       if (bss_sec != NULL)
   4130 	internal_a.o_snbss = bss_sec->target_index;
   4131       else
   4132 	internal_a.o_snbss = 0;
   4133 
   4134       if (tdata_sec != NULL)
   4135 	{
   4136 	  internal_a.o_sntdata = tdata_sec->target_index;
   4137 	  /* TODO: o_flags should be set to RS6K_AOUTHDR_TLS_LE
   4138 	     if there is at least one R_TLS_LE relocations.  */
   4139 	  internal_a.o_flags = 0;
   4140 #ifdef XCOFF64
   4141 	  internal_a.o_x64flags = 0;
   4142 #endif
   4143 	}
   4144       else
   4145 	{
   4146 	  internal_a.o_sntdata = 0;
   4147 	  internal_a.o_flags = 0;
   4148 #ifdef XCOFF64
   4149 	  internal_a.o_x64flags = 0;
   4150 #endif
   4151 	}
   4152       if (tbss_sec != NULL)
   4153 	  internal_a.o_sntbss = tbss_sec->target_index;
   4154       else
   4155 	  internal_a.o_sntbss = 0;
   4156 
   4157       toc = xcoff_data (abfd)->toc;
   4158       internal_a.o_toc = toc;
   4159       internal_a.o_sntoc = xcoff_data (abfd)->sntoc;
   4160 
   4161       internal_a.o_modtype = xcoff_data (abfd)->modtype;
   4162       if (xcoff_data (abfd)->cputype != -1)
   4163 	internal_a.o_cputype = xcoff_data (abfd)->cputype;
   4164       else
   4165 	{
   4166 	  switch (bfd_get_arch (abfd))
   4167 	    {
   4168 	    case bfd_arch_rs6000:
   4169 	      internal_a.o_cputype = 4;
   4170 	      break;
   4171 	    case bfd_arch_powerpc:
   4172 	      if (bfd_get_mach (abfd) == bfd_mach_ppc)
   4173 		internal_a.o_cputype = 3;
   4174 	      else if (bfd_get_mach (abfd) == bfd_mach_ppc_620)
   4175 		internal_a.o_cputype = 2;
   4176 	      else
   4177 		internal_a.o_cputype = 1;
   4178 	      break;
   4179 	    default:
   4180 	      abort ();
   4181 	    }
   4182 	}
   4183       internal_a.o_maxstack = xcoff_data (abfd)->maxstack;
   4184       internal_a.o_maxdata = xcoff_data (abfd)->maxdata;
   4185     }
   4186 #endif
   4187 
   4188 #ifdef COFF_WITH_PE
   4189   {
   4190     /* After object contents are finalized so we can compute a reasonable hash,
   4191        but before header is written so we can update it to point to debug directory.  */
   4192     struct pe_tdata *pe = pe_data (abfd);
   4193 
   4194     if (pe->build_id.after_write_object_contents != NULL)
   4195       (*pe->build_id.after_write_object_contents) (abfd);
   4196   }
   4197 #endif
   4198 
   4199   /* Now write header.  */
   4200   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
   4201     return false;
   4202 
   4203   {
   4204     char * buff;
   4205     bfd_size_type amount = bfd_coff_filhsz (abfd);
   4206 
   4207     buff = (char *) bfd_malloc (amount);
   4208     if (buff == NULL)
   4209       return false;
   4210 
   4211     bfd_coff_swap_filehdr_out (abfd, & internal_f, buff);
   4212     amount = bfd_bwrite (buff, amount, abfd);
   4213 
   4214     free (buff);
   4215 
   4216     if (amount != bfd_coff_filhsz (abfd))
   4217       return false;
   4218   }
   4219 
   4220   if (abfd->flags & EXEC_P)
   4221     {
   4222       /* Note that peicode.h fills in a PEAOUTHDR, not an AOUTHDR.
   4223 	 include/coff/pe.h sets AOUTSZ == sizeof (PEAOUTHDR)).  */
   4224       char * buff;
   4225       bfd_size_type amount = bfd_coff_aoutsz (abfd);
   4226 
   4227       buff = (char *) bfd_malloc (amount);
   4228       if (buff == NULL)
   4229 	return false;
   4230 
   4231       coff_swap_aouthdr_out (abfd, & internal_a, buff);
   4232       amount = bfd_bwrite (buff, amount, abfd);
   4233 
   4234       free (buff);
   4235 
   4236       if (amount != bfd_coff_aoutsz (abfd))
   4237 	return false;
   4238 
   4239 #ifdef COFF_IMAGE_WITH_PE
   4240       if (! coff_apply_checksum (abfd))
   4241 	return false;
   4242 #endif
   4243     }
   4244 #ifdef RS6000COFF_C
   4245 #ifndef XCOFF64
   4246   else
   4247     {
   4248       AOUTHDR buff;
   4249       size_t size;
   4250 
   4251       /* XCOFF32 seems to always write at least a small a.out header.  */
   4252       coff_swap_aouthdr_out (abfd, & internal_a, & buff);
   4253       if (xcoff_data (abfd)->full_aouthdr)
   4254 	size = bfd_coff_aoutsz (abfd);
   4255       else
   4256 	size = SMALL_AOUTSZ;
   4257       if (bfd_bwrite (& buff, (bfd_size_type) size, abfd) != size)
   4258 	return false;
   4259     }
   4260 #endif
   4261 #endif
   4262 
   4263   return true;
   4264 }
   4265 
   4266 static bool
   4267 coff_set_section_contents (bfd * abfd,
   4268 			   sec_ptr section,
   4269 			   const void * location,
   4270 			   file_ptr offset,
   4271 			   bfd_size_type count)
   4272 {
   4273   if (! abfd->output_has_begun)	/* Set by bfd.c handler.  */
   4274     {
   4275       if (! coff_compute_section_file_positions (abfd))
   4276 	return false;
   4277     }
   4278 
   4279 #if defined(_LIB) && !defined(TARG_AUX)
   4280    /* The physical address field of a .lib section is used to hold the
   4281       number of shared libraries in the section.  This code counts the
   4282       number of sections being written, and increments the lma field
   4283       with the number.
   4284 
   4285       I have found no documentation on the contents of this section.
   4286       Experimentation indicates that the section contains zero or more
   4287       records, each of which has the following structure:
   4288 
   4289       - a (four byte) word holding the length of this record, in words,
   4290       - a word that always seems to be set to "2",
   4291       - the path to a shared library, null-terminated and then padded
   4292 	to a whole word boundary.
   4293 
   4294       bfd_assert calls have been added to alert if an attempt is made
   4295       to write a section which doesn't follow these assumptions.  The
   4296       code has been tested on ISC 4.1 by me, and on SCO by Robert Lipe
   4297       <robertl (at) arnet.com> (Thanks!).
   4298 
   4299       Gvran Uddeborg <gvran (at) uddeborg.pp.se>.  */
   4300     if (strcmp (section->name, _LIB) == 0)
   4301       {
   4302 	bfd_byte *rec, *recend;
   4303 
   4304 	rec = (bfd_byte *) location;
   4305 	recend = rec + count;
   4306 	while (recend - rec >= 4)
   4307 	  {
   4308 	    size_t len = bfd_get_32 (abfd, rec);
   4309 	    if (len == 0 || len > (size_t) (recend - rec) / 4)
   4310 	      break;
   4311 	    rec += len * 4;
   4312 	    ++section->lma;
   4313 	  }
   4314 
   4315 	BFD_ASSERT (rec == recend);
   4316       }
   4317 #endif
   4318 
   4319   /* Don't write out bss sections - one way to do this is to
   4320        see if the filepos has not been set.  */
   4321   if (section->filepos == 0)
   4322     return true;
   4323 
   4324   if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0)
   4325     return false;
   4326 
   4327   if (count == 0)
   4328     return true;
   4329 
   4330   return bfd_bwrite (location, count, abfd) == count;
   4331 }
   4332 
   4333 static void *
   4334 buy_and_read (bfd *abfd, file_ptr where,
   4335 	      bfd_size_type nmemb, bfd_size_type size)
   4336 {
   4337   size_t amt;
   4338 
   4339   if (_bfd_mul_overflow (nmemb, size, &amt))
   4340     {
   4341       bfd_set_error (bfd_error_file_too_big);
   4342       return NULL;
   4343     }
   4344   if (bfd_seek (abfd, where, SEEK_SET) != 0)
   4345     return NULL;
   4346   return _bfd_malloc_and_read (abfd, amt, amt);
   4347 }
   4348 
   4349 /*
   4350 SUBSUBSECTION
   4351 	Reading linenumbers
   4352 
   4353 	Creating the linenumber table is done by reading in the entire
   4354 	coff linenumber table, and creating another table for internal use.
   4355 
   4356 	A coff linenumber table is structured so that each function
   4357 	is marked as having a line number of 0. Each line within the
   4358 	function is an offset from the first line in the function. The
   4359 	base of the line number information for the table is stored in
   4360 	the symbol associated with the function.
   4361 
   4362 	Note: The PE format uses line number 0 for a flag indicating a
   4363 	new source file.
   4364 
   4365 	The information is copied from the external to the internal
   4366 	table, and each symbol which marks a function is marked by
   4367 	pointing its...
   4368 
   4369 	How does this work ?
   4370 */
   4371 
   4372 static int
   4373 coff_sort_func_alent (const void * arg1, const void * arg2)
   4374 {
   4375   const alent *al1 = *(const alent **) arg1;
   4376   const alent *al2 = *(const alent **) arg2;
   4377   const coff_symbol_type *s1 = (const coff_symbol_type *) (al1->u.sym);
   4378   const coff_symbol_type *s2 = (const coff_symbol_type *) (al2->u.sym);
   4379 
   4380   if (s1 == NULL || s2 == NULL)
   4381     return 0;
   4382   if (s1->symbol.value < s2->symbol.value)
   4383     return -1;
   4384   else if (s1->symbol.value > s2->symbol.value)
   4385     return 1;
   4386 
   4387   return 0;
   4388 }
   4389 
   4390 static bool
   4391 coff_slurp_line_table (bfd *abfd, asection *asect)
   4392 {
   4393   LINENO *native_lineno;
   4394   alent *lineno_cache;
   4395   unsigned int counter;
   4396   alent *cache_ptr;
   4397   bfd_vma prev_offset = 0;
   4398   bool ordered = true;
   4399   unsigned int nbr_func;
   4400   LINENO *src;
   4401   bool have_func;
   4402   bool ret = true;
   4403   size_t amt;
   4404 
   4405   if (asect->lineno_count == 0)
   4406     return true;
   4407 
   4408   BFD_ASSERT (asect->lineno == NULL);
   4409 
   4410   native_lineno = (LINENO *) buy_and_read (abfd, asect->line_filepos,
   4411 					   asect->lineno_count,
   4412 					   bfd_coff_linesz (abfd));
   4413   if (native_lineno == NULL)
   4414     {
   4415       _bfd_error_handler
   4416 	(_("%pB: warning: line number table read failed"), abfd);
   4417       return false;
   4418     }
   4419 
   4420   if (_bfd_mul_overflow (asect->lineno_count + 1, sizeof (alent), &amt))
   4421     {
   4422       bfd_set_error (bfd_error_file_too_big);
   4423       free (native_lineno);
   4424       return false;
   4425     }
   4426   lineno_cache = (alent *) bfd_alloc (abfd, amt);
   4427   if (lineno_cache == NULL)
   4428     {
   4429       free (native_lineno);
   4430       return false;
   4431     }
   4432 
   4433   cache_ptr = lineno_cache;
   4434   asect->lineno = lineno_cache;
   4435   src = native_lineno;
   4436   nbr_func = 0;
   4437   have_func = false;
   4438 
   4439   for (counter = 0; counter < asect->lineno_count; counter++, src++)
   4440     {
   4441       struct internal_lineno dst;
   4442 
   4443       bfd_coff_swap_lineno_in (abfd, src, &dst);
   4444       cache_ptr->line_number = dst.l_lnno;
   4445       /* Appease memory checkers that get all excited about
   4446 	 uninitialised memory when copying alents if u.offset is
   4447 	 larger than u.sym.  (64-bit BFD on 32-bit host.)  */
   4448       memset (&cache_ptr->u, 0, sizeof (cache_ptr->u));
   4449 
   4450       if (cache_ptr->line_number == 0)
   4451 	{
   4452 	  combined_entry_type * ent;
   4453 	  unsigned long symndx;
   4454 	  coff_symbol_type *sym;
   4455 
   4456 	  have_func = false;
   4457 	  symndx = dst.l_addr.l_symndx;
   4458 	  if (symndx >= obj_raw_syment_count (abfd))
   4459 	    {
   4460 	      _bfd_error_handler
   4461 		/* xgettext:c-format */
   4462 		(_("%pB: warning: illegal symbol index 0x%lx in line number entry %d"),
   4463 		 abfd, symndx, counter);
   4464 	      cache_ptr->line_number = -1;
   4465 	      ret = false;
   4466 	      continue;
   4467 	    }
   4468 
   4469 	  ent = obj_raw_syments (abfd) + symndx;
   4470 	  /* FIXME: We should not be casting between ints and
   4471 	     pointers like this.  */
   4472 	  if (! ent->is_sym)
   4473 	    {
   4474 	      _bfd_error_handler
   4475 		/* xgettext:c-format */
   4476 		(_("%pB: warning: illegal symbol index 0x%lx in line number entry %d"),
   4477 		 abfd, symndx, counter);
   4478 	      cache_ptr->line_number = -1;
   4479 	      ret = false;
   4480 	      continue;
   4481 	    }
   4482 	  sym = (coff_symbol_type *) (ent->u.syment._n._n_n._n_zeroes);
   4483 
   4484 	  /* PR 17512 file: 078-10659-0.004  */
   4485 	  if (sym < obj_symbols (abfd)
   4486 	      || sym >= obj_symbols (abfd) + bfd_get_symcount (abfd))
   4487 	    {
   4488 	      _bfd_error_handler
   4489 		/* xgettext:c-format */
   4490 		(_("%pB: warning: illegal symbol in line number entry %d"),
   4491 		 abfd, counter);
   4492 	      cache_ptr->line_number = -1;
   4493 	      ret = false;
   4494 	      continue;
   4495 	    }
   4496 
   4497 	  have_func = true;
   4498 	  nbr_func++;
   4499 	  cache_ptr->u.sym = (asymbol *) sym;
   4500 	  if (sym->lineno != NULL)
   4501 	    _bfd_error_handler
   4502 	      /* xgettext:c-format */
   4503 	      (_("%pB: warning: duplicate line number information for `%s'"),
   4504 	       abfd, bfd_asymbol_name (&sym->symbol));
   4505 
   4506 	  sym->lineno = cache_ptr;
   4507 	  if (sym->symbol.value < prev_offset)
   4508 	    ordered = false;
   4509 	  prev_offset = sym->symbol.value;
   4510 	}
   4511       else if (!have_func)
   4512 	/* Drop line information that has no associated function.
   4513 	   PR 17521: file: 078-10659-0.004.  */
   4514 	continue;
   4515       else
   4516 	cache_ptr->u.offset = dst.l_addr.l_paddr - bfd_section_vma (asect);
   4517       cache_ptr++;
   4518     }
   4519 
   4520   asect->lineno_count = cache_ptr - lineno_cache;
   4521   memset (cache_ptr, 0, sizeof (*cache_ptr));
   4522   free (native_lineno);
   4523 
   4524   /* On some systems (eg AIX5.3) the lineno table may not be sorted.  */
   4525   if (!ordered)
   4526     {
   4527       /* Sort the table.  */
   4528       alent **func_table;
   4529       alent *n_lineno_cache;
   4530 
   4531       /* Create a table of functions.  */
   4532       if (_bfd_mul_overflow (nbr_func, sizeof (alent *), &amt))
   4533 	{
   4534 	  bfd_set_error (bfd_error_file_too_big);
   4535 	  ret = false;
   4536 	}
   4537       else if ((func_table = (alent **) bfd_alloc (abfd, amt)) != NULL)
   4538 	{
   4539 	  alent **p = func_table;
   4540 	  unsigned int i;
   4541 
   4542 	  for (i = 0; i < asect->lineno_count; i++)
   4543 	    if (lineno_cache[i].line_number == 0)
   4544 	      *p++ = &lineno_cache[i];
   4545 
   4546 	  BFD_ASSERT ((unsigned int) (p - func_table) == nbr_func);
   4547 
   4548 	  /* Sort by functions.  */
   4549 	  qsort (func_table, nbr_func, sizeof (alent *), coff_sort_func_alent);
   4550 
   4551 	  /* Create the new sorted table.  */
   4552 	  if (_bfd_mul_overflow (asect->lineno_count, sizeof (alent), &amt))
   4553 	    {
   4554 	      bfd_set_error (bfd_error_file_too_big);
   4555 	      ret = false;
   4556 	    }
   4557 	  else if ((n_lineno_cache = (alent *) bfd_alloc (abfd, amt)) != NULL)
   4558 	    {
   4559 	      alent *n_cache_ptr = n_lineno_cache;
   4560 
   4561 	      for (i = 0; i < nbr_func; i++)
   4562 		{
   4563 		  coff_symbol_type *sym;
   4564 		  alent *old_ptr = func_table[i];
   4565 
   4566 		  /* Update the function entry.  */
   4567 		  sym = (coff_symbol_type *) old_ptr->u.sym;
   4568 		  /* PR binutils/17512: Point the lineno to where
   4569 		     this entry will be after the memcpy below.  */
   4570 		  sym->lineno = lineno_cache + (n_cache_ptr - n_lineno_cache);
   4571 		  /* Copy the function and line number entries.  */
   4572 		  do
   4573 		    *n_cache_ptr++ = *old_ptr++;
   4574 		  while (old_ptr->line_number != 0);
   4575 		}
   4576 
   4577 	      memcpy (lineno_cache, n_lineno_cache,
   4578 		      asect->lineno_count * sizeof (alent));
   4579 	    }
   4580 	  else
   4581 	    ret = false;
   4582 	  bfd_release (abfd, func_table);
   4583 	}
   4584       else
   4585 	ret = false;
   4586     }
   4587 
   4588   return ret;
   4589 }
   4590 
   4591 /* Slurp in the symbol table, converting it to generic form.  Note
   4592    that if coff_relocate_section is defined, the linker will read
   4593    symbols via coff_link_add_symbols, rather than via this routine.  */
   4594 
   4595 static bool
   4596 coff_slurp_symbol_table (bfd * abfd)
   4597 {
   4598   combined_entry_type *native_symbols;
   4599   coff_symbol_type *cached_area;
   4600   unsigned int *table_ptr;
   4601   unsigned int number_of_symbols = 0;
   4602   bool ret = true;
   4603   size_t amt;
   4604 
   4605   if (obj_symbols (abfd))
   4606     return true;
   4607 
   4608   /* Read in the symbol table.  */
   4609   if ((native_symbols = coff_get_normalized_symtab (abfd)) == NULL)
   4610     return false;
   4611 
   4612   /* Allocate enough room for all the symbols in cached form.  */
   4613   if (_bfd_mul_overflow (obj_raw_syment_count (abfd),
   4614 			 sizeof (*cached_area), &amt))
   4615     {
   4616       bfd_set_error (bfd_error_file_too_big);
   4617       return false;
   4618     }
   4619   cached_area = (coff_symbol_type *) bfd_alloc (abfd, amt);
   4620   if (cached_area == NULL)
   4621     return false;
   4622 
   4623   if (_bfd_mul_overflow (obj_raw_syment_count (abfd),
   4624 			 sizeof (*table_ptr), &amt))
   4625     {
   4626       bfd_set_error (bfd_error_file_too_big);
   4627       return false;
   4628     }
   4629   table_ptr = (unsigned int *) bfd_zalloc (abfd, amt);
   4630   if (table_ptr == NULL)
   4631     return false;
   4632   else
   4633     {
   4634       coff_symbol_type *dst = cached_area;
   4635       unsigned int last_native_index = obj_raw_syment_count (abfd);
   4636       unsigned int this_index = 0;
   4637 
   4638       while (this_index < last_native_index)
   4639 	{
   4640 	  combined_entry_type *src = native_symbols + this_index;
   4641 	  table_ptr[this_index] = number_of_symbols;
   4642 
   4643 	  dst->symbol.the_bfd = abfd;
   4644 	  BFD_ASSERT (src->is_sym);
   4645 	  dst->symbol.name = (char *) (src->u.syment._n._n_n._n_offset);
   4646 	  /* We use the native name field to point to the cached field.  */
   4647 	  src->u.syment._n._n_n._n_zeroes = (uintptr_t) dst;
   4648 	  dst->symbol.section = coff_section_from_bfd_index (abfd,
   4649 						     src->u.syment.n_scnum);
   4650 	  dst->symbol.flags = 0;
   4651 	  /* PR 17512: file: 079-7098-0.001:0.1.  */
   4652 	  dst->symbol.value = 0;
   4653 	  dst->done_lineno = false;
   4654 
   4655 	  switch (src->u.syment.n_sclass)
   4656 	    {
   4657 	    case C_EXT:
   4658 	    case C_WEAKEXT:
   4659 #if defined ARM
   4660 	    case C_THUMBEXT:
   4661 	    case C_THUMBEXTFUNC:
   4662 #endif
   4663 #ifdef RS6000COFF_C
   4664 	    case C_HIDEXT:
   4665 #if ! defined _AIX52 && ! defined AIX_WEAK_SUPPORT
   4666 	    case C_AIX_WEAKEXT:
   4667 #endif
   4668 #endif
   4669 #ifdef C_SYSTEM
   4670 	    case C_SYSTEM:	/* System Wide variable.  */
   4671 #endif
   4672 #ifdef COFF_WITH_PE
   4673 	    /* In PE, 0x68 (104) denotes a section symbol.  */
   4674 	    case C_SECTION:
   4675 	    /* In PE, 0x69 (105) denotes a weak external symbol.  */
   4676 	    case C_NT_WEAK:
   4677 #endif
   4678 	      switch (coff_classify_symbol (abfd, &src->u.syment))
   4679 		{
   4680 		case COFF_SYMBOL_GLOBAL:
   4681 		  dst->symbol.flags = BSF_EXPORT | BSF_GLOBAL;
   4682 #if defined COFF_WITH_PE
   4683 		  /* PE sets the symbol to a value relative to the
   4684 		     start of the section.  */
   4685 		  dst->symbol.value = src->u.syment.n_value;
   4686 #else
   4687 		  dst->symbol.value = (src->u.syment.n_value
   4688 				       - dst->symbol.section->vma);
   4689 #endif
   4690 		  if (ISFCN ((src->u.syment.n_type)))
   4691 		    /* A function ext does not go at the end of a
   4692 		       file.  */
   4693 		    dst->symbol.flags |= BSF_NOT_AT_END | BSF_FUNCTION;
   4694 		  break;
   4695 
   4696 		case COFF_SYMBOL_COMMON:
   4697 		  dst->symbol.section = bfd_com_section_ptr;
   4698 		  dst->symbol.value = src->u.syment.n_value;
   4699 		  break;
   4700 
   4701 		case COFF_SYMBOL_UNDEFINED:
   4702 		  dst->symbol.section = bfd_und_section_ptr;
   4703 		  dst->symbol.value = 0;
   4704 		  break;
   4705 
   4706 		case COFF_SYMBOL_PE_SECTION:
   4707 		  dst->symbol.flags |= BSF_EXPORT | BSF_SECTION_SYM;
   4708 		  dst->symbol.value = 0;
   4709 		  break;
   4710 
   4711 		case COFF_SYMBOL_LOCAL:
   4712 		  dst->symbol.flags = BSF_LOCAL;
   4713 #if defined COFF_WITH_PE
   4714 		  /* PE sets the symbol to a value relative to the
   4715 		     start of the section.  */
   4716 		  dst->symbol.value = src->u.syment.n_value;
   4717 #else
   4718 		  dst->symbol.value = (src->u.syment.n_value
   4719 				       - dst->symbol.section->vma);
   4720 #endif
   4721 		  if (ISFCN ((src->u.syment.n_type)))
   4722 		    dst->symbol.flags |= BSF_NOT_AT_END | BSF_FUNCTION;
   4723 		  break;
   4724 		}
   4725 
   4726 #ifdef RS6000COFF_C
   4727 	      /* A symbol with a csect entry should not go at the end.  */
   4728 	      if (src->u.syment.n_numaux > 0)
   4729 		dst->symbol.flags |= BSF_NOT_AT_END;
   4730 #endif
   4731 
   4732 #ifdef COFF_WITH_PE
   4733 	      if (src->u.syment.n_sclass == C_NT_WEAK)
   4734 		dst->symbol.flags |= BSF_WEAK;
   4735 
   4736 	      if (src->u.syment.n_sclass == C_SECTION
   4737 		  && src->u.syment.n_scnum > 0)
   4738 		dst->symbol.flags = BSF_LOCAL;
   4739 #endif
   4740 	      if (src->u.syment.n_sclass == C_WEAKEXT
   4741 #ifdef RS6000COFF_C
   4742 		  || src->u.syment.n_sclass == C_AIX_WEAKEXT
   4743 #endif
   4744 		  )
   4745 		dst->symbol.flags |= BSF_WEAK;
   4746 
   4747 	      break;
   4748 
   4749 	    case C_STAT:	 /* Static.  */
   4750 #if defined ARM
   4751 	    case C_THUMBSTAT:    /* Thumb static.  */
   4752 	    case C_THUMBLABEL:   /* Thumb label.  */
   4753 	    case C_THUMBSTATFUNC:/* Thumb static function.  */
   4754 #endif
   4755 #ifdef RS6000COFF_C
   4756 	    case C_DWARF:	 /* A label in a dwarf section.  */
   4757 	    case C_INFO:	 /* A label in a comment section.  */
   4758 #endif
   4759 	    case C_LABEL:	 /* Label.  */
   4760 	      if (src->u.syment.n_scnum == N_DEBUG)
   4761 		dst->symbol.flags = BSF_DEBUGGING;
   4762 	      else
   4763 		dst->symbol.flags = BSF_LOCAL;
   4764 
   4765 	      /* Base the value as an index from the base of the
   4766 		 section, if there is one.  */
   4767 	      if (dst->symbol.section)
   4768 		{
   4769 #if defined COFF_WITH_PE
   4770 		  /* PE sets the symbol to a value relative to the
   4771 		     start of the section.  */
   4772 		  dst->symbol.value = src->u.syment.n_value;
   4773 #else
   4774 		  dst->symbol.value = (src->u.syment.n_value
   4775 				       - dst->symbol.section->vma);
   4776 #endif
   4777 		}
   4778 	      else
   4779 		dst->symbol.value = src->u.syment.n_value;
   4780 	      break;
   4781 
   4782 	    case C_FILE:	/* File name.  */
   4783 	      dst->symbol.flags = BSF_FILE;
   4784 	      /* Fall through.  */
   4785 	    case C_MOS:		/* Member of structure.  */
   4786 	    case C_EOS:		/* End of structure.  */
   4787 	    case C_REGPARM:	/* Register parameter.  */
   4788 	    case C_REG:		/* register variable.  */
   4789 	      /* C_AUTOARG conflicts with TI COFF C_UEXT.  */
   4790 	    case C_TPDEF:	/* Type definition.  */
   4791 	    case C_ARG:
   4792 	    case C_AUTO:	/* Automatic variable.  */
   4793 	    case C_FIELD:	/* Bit field.  */
   4794 	    case C_ENTAG:	/* Enumeration tag.  */
   4795 	    case C_MOE:		/* Member of enumeration.  */
   4796 	    case C_MOU:		/* Member of union.  */
   4797 	    case C_UNTAG:	/* Union tag.  */
   4798 	    case C_STRTAG:	/* Structure tag.  */
   4799 #ifdef RS6000COFF_C
   4800 	    case C_GSYM:
   4801 	    case C_LSYM:
   4802 	    case C_PSYM:
   4803 	    case C_RSYM:
   4804 	    case C_RPSYM:
   4805 	    case C_STSYM:
   4806 	    case C_TCSYM:
   4807 	    case C_BCOMM:
   4808 	    case C_ECOML:
   4809 	    case C_ECOMM:
   4810 	    case C_DECL:
   4811 	    case C_ENTRY:
   4812 	    case C_FUN:
   4813 	    case C_ESTAT:
   4814 #endif
   4815 	      dst->symbol.flags |= BSF_DEBUGGING;
   4816 	      dst->symbol.value = (src->u.syment.n_value);
   4817 	      break;
   4818 
   4819 #ifdef RS6000COFF_C
   4820 	    case C_BINCL:	/* Beginning of include file.  */
   4821 	    case C_EINCL:	/* Ending of include file.  */
   4822 	      /* The value is actually a pointer into the line numbers
   4823 		 of the file.  We locate the line number entry, and
   4824 		 set the section to the section which contains it, and
   4825 		 the value to the index in that section.  */
   4826 	      {
   4827 		asection *sec;
   4828 
   4829 		dst->symbol.flags = BSF_DEBUGGING;
   4830 		for (sec = abfd->sections; sec != NULL; sec = sec->next)
   4831 		  if (sec->line_filepos <= (file_ptr) src->u.syment.n_value
   4832 		      && ((file_ptr) (sec->line_filepos
   4833 				      + sec->lineno_count * bfd_coff_linesz (abfd))
   4834 			  > (file_ptr) src->u.syment.n_value))
   4835 		    break;
   4836 		if (sec == NULL)
   4837 		  dst->symbol.value = 0;
   4838 		else
   4839 		  {
   4840 		    dst->symbol.section = sec;
   4841 		    dst->symbol.value = ((src->u.syment.n_value
   4842 					  - sec->line_filepos)
   4843 					 / bfd_coff_linesz (abfd));
   4844 		    src->fix_line = 1;
   4845 		  }
   4846 	      }
   4847 	      break;
   4848 
   4849 	    case C_BSTAT:
   4850 	      dst->symbol.flags = BSF_DEBUGGING;
   4851 
   4852 	      /* The value is actually a symbol index.  Save a pointer
   4853 		 to the symbol instead of the index.  FIXME: This
   4854 		 should use a union.  */
   4855 	      src->u.syment.n_value
   4856 		= (uintptr_t) (native_symbols + src->u.syment.n_value);
   4857 	      dst->symbol.value = src->u.syment.n_value;
   4858 	      src->fix_value = 1;
   4859 	      break;
   4860 #endif
   4861 
   4862 	    case C_BLOCK:	/* ".bb" or ".eb".  */
   4863 	    case C_FCN:		/* ".bf" or ".ef" (or PE ".lf").  */
   4864 	    case C_EFCN:	/* Physical end of function.  */
   4865 #if defined COFF_WITH_PE
   4866 	      /* PE sets the symbol to a value relative to the start
   4867 		 of the section.  */
   4868 	      dst->symbol.value = src->u.syment.n_value;
   4869 	      if (strcmp (dst->symbol.name, ".bf") != 0)
   4870 		{
   4871 		  /* PE uses funny values for .ef and .lf; don't
   4872 		     relocate them.  */
   4873 		  dst->symbol.flags = BSF_DEBUGGING;
   4874 		}
   4875 	      else
   4876 		dst->symbol.flags = BSF_DEBUGGING | BSF_DEBUGGING_RELOC;
   4877 #else
   4878 	      /* Base the value as an index from the base of the
   4879 		 section.  */
   4880 	      dst->symbol.flags = BSF_LOCAL;
   4881 	      dst->symbol.value = (src->u.syment.n_value
   4882 				   - dst->symbol.section->vma);
   4883 #endif
   4884 	      break;
   4885 
   4886 	    case C_STATLAB:	/* Static load time label.  */
   4887 	      dst->symbol.value = src->u.syment.n_value;
   4888 	      dst->symbol.flags = BSF_GLOBAL;
   4889 	      break;
   4890 
   4891 	    case C_NULL:
   4892 	      /* PE DLLs sometimes have zeroed out symbols for some
   4893 		 reason.  Just ignore them without a warning.  */
   4894 	      if (src->u.syment.n_type == 0
   4895 		  && src->u.syment.n_value == 0
   4896 		  && src->u.syment.n_scnum == 0)
   4897 		break;
   4898 #ifdef RS6000COFF_C
   4899 	      /* XCOFF specific: deleted entry.  */
   4900 	      if (src->u.syment.n_value == C_NULL_VALUE)
   4901 		break;
   4902 #endif
   4903 	      /* Fall through.  */
   4904 	    case C_EXTDEF:	/* External definition.  */
   4905 	    case C_ULABEL:	/* Undefined label.  */
   4906 	    case C_USTATIC:	/* Undefined static.  */
   4907 #ifndef COFF_WITH_PE
   4908 	    /* C_LINE in regular coff is 0x68.  NT has taken over this storage
   4909 	       class to represent a section symbol.  */
   4910 	    case C_LINE:	/* line # reformatted as symbol table entry.  */
   4911 	      /* NT uses 0x67 for a weak symbol, not C_ALIAS.  */
   4912 	    case C_ALIAS:	/* Duplicate tag.  */
   4913 #endif
   4914 	      /* New storage classes for TI COFF.  */
   4915 #ifdef TICOFF
   4916 	    case C_UEXT:	/* Tentative external definition.  */
   4917 #endif
   4918 	    case C_EXTLAB:	/* External load time label.  */
   4919 	    default:
   4920 	      _bfd_error_handler
   4921 		/* xgettext:c-format */
   4922 		(_("%pB: unrecognized storage class %d for %s symbol `%s'"),
   4923 		 abfd, src->u.syment.n_sclass,
   4924 		 dst->symbol.section->name, dst->symbol.name);
   4925 	      ret = false;
   4926 	      /* Fall through.  */
   4927 	    case C_HIDDEN:	/* Ext symbol in dmert public lib.  */
   4928 	      /* PR 20722: These symbols can also be generated by
   4929 		 building DLLs with --gc-sections enabled.  */
   4930 	      dst->symbol.flags = BSF_DEBUGGING;
   4931 	      dst->symbol.value = (src->u.syment.n_value);
   4932 	      break;
   4933 	    }
   4934 
   4935 	  dst->native = src;
   4936 	  dst->symbol.udata.i = 0;
   4937 	  dst->lineno = NULL;
   4938 
   4939 	  this_index += (src->u.syment.n_numaux) + 1;
   4940 	  dst++;
   4941 	  number_of_symbols++;
   4942 	}
   4943     }
   4944 
   4945   obj_symbols (abfd) = cached_area;
   4946   obj_raw_syments (abfd) = native_symbols;
   4947 
   4948   abfd->symcount = number_of_symbols;
   4949   obj_convert (abfd) = table_ptr;
   4950   /* Slurp the line tables for each section too.  */
   4951   {
   4952     asection *p;
   4953 
   4954     p = abfd->sections;
   4955     while (p)
   4956       {
   4957 	if (! coff_slurp_line_table (abfd, p))
   4958 	  return false;
   4959 	p = p->next;
   4960       }
   4961   }
   4962 
   4963   return ret;
   4964 }
   4965 
   4966 /* Classify a COFF symbol.  A couple of targets have globally visible
   4967    symbols which are not class C_EXT, and this handles those.  It also
   4968    recognizes some special PE cases.  */
   4969 
   4970 static enum coff_symbol_classification
   4971 coff_classify_symbol (bfd *abfd,
   4972 		      struct internal_syment *syment)
   4973 {
   4974   /* FIXME: This partially duplicates the switch in
   4975      coff_slurp_symbol_table.  */
   4976   switch (syment->n_sclass)
   4977     {
   4978     case C_EXT:
   4979     case C_WEAKEXT:
   4980 #ifdef ARM
   4981     case C_THUMBEXT:
   4982     case C_THUMBEXTFUNC:
   4983 #endif
   4984 #ifdef RS6000COFF_C
   4985     case C_HIDEXT:
   4986 #if ! defined _AIX52 && ! defined AIX_WEAK_SUPPORT
   4987     case C_AIX_WEAKEXT:
   4988 #endif
   4989 #endif
   4990 #ifdef C_SYSTEM
   4991     case C_SYSTEM:
   4992 #endif
   4993 #ifdef COFF_WITH_PE
   4994     case C_NT_WEAK:
   4995 #endif
   4996       if (syment->n_scnum == 0)
   4997 	{
   4998 	  if (syment->n_value == 0)
   4999 	    return COFF_SYMBOL_UNDEFINED;
   5000 	  else
   5001 	    return COFF_SYMBOL_COMMON;
   5002 	}
   5003 #ifdef RS6000COFF_C
   5004       if (syment->n_sclass == C_HIDEXT)
   5005 	return COFF_SYMBOL_LOCAL;
   5006 #endif
   5007       return COFF_SYMBOL_GLOBAL;
   5008 
   5009     default:
   5010       break;
   5011     }
   5012 
   5013 #ifdef COFF_WITH_PE
   5014   if (syment->n_sclass == C_STAT)
   5015     {
   5016       if (syment->n_scnum == 0)
   5017 	/* The Microsoft compiler sometimes generates these if a
   5018 	   small static function is inlined every time it is used.
   5019 	   The function is discarded, but the symbol table entry
   5020 	   remains.  */
   5021 	return COFF_SYMBOL_LOCAL;
   5022 
   5023 #ifdef STRICT_PE_FORMAT
   5024       /* This is correct for Microsoft generated objects, but it
   5025 	 breaks gas generated objects.  */
   5026       if (syment->n_value == 0)
   5027 	{
   5028 	  asection *sec;
   5029 	  char * name;
   5030 	  char buf[SYMNMLEN + 1];
   5031 
   5032 	  name = _bfd_coff_internal_syment_name (abfd, syment, buf)
   5033 	  sec = coff_section_from_bfd_index (abfd, syment->n_scnum);
   5034 	  if (sec != NULL && name != NULL
   5035 	      && (strcmp (bfd_section_name (sec), name) == 0))
   5036 	    return COFF_SYMBOL_PE_SECTION;
   5037 	}
   5038 #endif
   5039 
   5040       return COFF_SYMBOL_LOCAL;
   5041     }
   5042 
   5043   if (syment->n_sclass == C_SECTION)
   5044     {
   5045       /* In some cases in a DLL generated by the Microsoft linker, the
   5046 	 n_value field will contain garbage.  FIXME: This should
   5047 	 probably be handled by the swapping function instead.  */
   5048       syment->n_value = 0;
   5049       if (syment->n_scnum == 0)
   5050 	return COFF_SYMBOL_UNDEFINED;
   5051       return COFF_SYMBOL_PE_SECTION;
   5052     }
   5053 #endif /* COFF_WITH_PE */
   5054 
   5055   /* If it is not a global symbol, we presume it is a local symbol.  */
   5056   if (syment->n_scnum == 0)
   5057     {
   5058       char buf[SYMNMLEN + 1];
   5059 
   5060       _bfd_error_handler
   5061 	/* xgettext:c-format */
   5062 	(_("warning: %pB: local symbol `%s' has no section"),
   5063 	 abfd, _bfd_coff_internal_syment_name (abfd, syment, buf));
   5064     }
   5065 
   5066   return COFF_SYMBOL_LOCAL;
   5067 }
   5068 
   5069 /*
   5070 SUBSUBSECTION
   5071 	Reading relocations
   5072 
   5073 	Coff relocations are easily transformed into the internal BFD form
   5074 	(@code{arelent}).
   5075 
   5076 	Reading a coff relocation table is done in the following stages:
   5077 
   5078 	o Read the entire coff relocation table into memory.
   5079 
   5080 	o Process each relocation in turn; first swap it from the
   5081 	external to the internal form.
   5082 
   5083 	o Turn the symbol referenced in the relocation's symbol index
   5084 	into a pointer into the canonical symbol table.
   5085 	This table is the same as the one returned by a call to
   5086 	@code{bfd_canonicalize_symtab}. The back end will call that
   5087 	routine and save the result if a canonicalization hasn't been done.
   5088 
   5089 	o The reloc index is turned into a pointer to a howto
   5090 	structure, in a back end specific way. For instance, the 386
   5091 	uses the @code{r_type} to directly produce an index
   5092 	into a howto table vector.
   5093 */
   5094 
   5095 #ifndef CALC_ADDEND
   5096 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr)		\
   5097   {								\
   5098     coff_symbol_type *coffsym = NULL;				\
   5099 								\
   5100     if (ptr && bfd_asymbol_bfd (ptr) != abfd)			\
   5101       coffsym = (obj_symbols (abfd)				\
   5102 		 + (cache_ptr->sym_ptr_ptr - symbols));		\
   5103     else if (ptr)						\
   5104       coffsym = coff_symbol_from (ptr);				\
   5105     if (coffsym != NULL						\
   5106 	&& coffsym->native->is_sym				\
   5107 	&& coffsym->native->u.syment.n_scnum == 0)		\
   5108       cache_ptr->addend = 0;					\
   5109     else if (ptr && bfd_asymbol_bfd (ptr) == abfd		\
   5110 	     && ptr->section != NULL)				\
   5111       cache_ptr->addend = - (ptr->section->vma + ptr->value);	\
   5112     else							\
   5113       cache_ptr->addend = 0;					\
   5114   }
   5115 #endif
   5116 
   5117 static bool
   5118 coff_slurp_reloc_table (bfd * abfd, sec_ptr asect, asymbol ** symbols)
   5119 {
   5120   bfd_byte *native_relocs;
   5121   arelent *reloc_cache;
   5122   arelent *cache_ptr;
   5123   unsigned int idx;
   5124   size_t amt;
   5125 
   5126   if (asect->relocation)
   5127     return true;
   5128   if (asect->reloc_count == 0)
   5129     return true;
   5130   if (asect->flags & SEC_CONSTRUCTOR)
   5131     return true;
   5132   if (!coff_slurp_symbol_table (abfd))
   5133     return false;
   5134 
   5135   native_relocs = (bfd_byte *) buy_and_read (abfd, asect->rel_filepos,
   5136 					     asect->reloc_count,
   5137 					     bfd_coff_relsz (abfd));
   5138   if (native_relocs == NULL)
   5139     return false;
   5140 
   5141   if (_bfd_mul_overflow (asect->reloc_count, sizeof (arelent), &amt))
   5142     {
   5143       bfd_set_error (bfd_error_file_too_big);
   5144       return false;
   5145     }
   5146   reloc_cache = (arelent *) bfd_alloc (abfd, amt);
   5147   if (reloc_cache == NULL)
   5148     {
   5149       free (native_relocs);
   5150       return false;
   5151     }
   5152 
   5153   for (idx = 0; idx < asect->reloc_count; idx++)
   5154     {
   5155       struct internal_reloc dst;
   5156       void *src;
   5157 #ifndef RELOC_PROCESSING
   5158       asymbol *ptr;
   5159 #endif
   5160 
   5161       cache_ptr = reloc_cache + idx;
   5162       src = native_relocs + idx * (size_t) bfd_coff_relsz (abfd);
   5163 
   5164       dst.r_offset = 0;
   5165       bfd_coff_swap_reloc_in (abfd, src, &dst);
   5166 
   5167 #ifdef RELOC_PROCESSING
   5168       RELOC_PROCESSING (cache_ptr, &dst, symbols, abfd, asect);
   5169 #else
   5170       cache_ptr->address = dst.r_vaddr;
   5171 
   5172       if (dst.r_symndx != -1 && symbols != NULL)
   5173 	{
   5174 	  if (dst.r_symndx < 0 || dst.r_symndx >= obj_conv_table_size (abfd))
   5175 	    {
   5176 	      _bfd_error_handler
   5177 		/* xgettext:c-format */
   5178 		(_("%pB: warning: illegal symbol index %ld in relocs"),
   5179 		 abfd, dst.r_symndx);
   5180 	      cache_ptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
   5181 	      ptr = NULL;
   5182 	    }
   5183 	  else
   5184 	    {
   5185 	      cache_ptr->sym_ptr_ptr = (symbols
   5186 					+ obj_convert (abfd)[dst.r_symndx]);
   5187 	      ptr = *(cache_ptr->sym_ptr_ptr);
   5188 	    }
   5189 	}
   5190       else
   5191 	{
   5192 	  cache_ptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
   5193 	  ptr = NULL;
   5194 	}
   5195 
   5196       /* The symbols definitions that we have read in have been
   5197 	 relocated as if their sections started at 0. But the offsets
   5198 	 refering to the symbols in the raw data have not been
   5199 	 modified, so we have to have a negative addend to compensate.
   5200 
   5201 	 Note that symbols which used to be common must be left alone.  */
   5202 
   5203       /* Calculate any reloc addend by looking at the symbol.  */
   5204       CALC_ADDEND (abfd, ptr, dst, cache_ptr);
   5205       (void) ptr;
   5206 
   5207       cache_ptr->address -= asect->vma;
   5208       /* !! cache_ptr->section = NULL;*/
   5209 
   5210       /* Fill in the cache_ptr->howto field from dst.r_type.  */
   5211       RTYPE2HOWTO (cache_ptr, &dst);
   5212 #endif	/* RELOC_PROCESSING */
   5213 
   5214       if (cache_ptr->howto == NULL)
   5215 	{
   5216 	  _bfd_error_handler
   5217 	    /* xgettext:c-format */
   5218 	    (_("%pB: illegal relocation type %d at address %#" PRIx64),
   5219 	     abfd, dst.r_type, (uint64_t) dst.r_vaddr);
   5220 	  bfd_set_error (bfd_error_bad_value);
   5221 	  free (native_relocs);
   5222 	  return false;
   5223 	}
   5224     }
   5225 
   5226   free (native_relocs);
   5227   asect->relocation = reloc_cache;
   5228   return true;
   5229 }
   5230 
   5231 #ifndef coff_rtype_to_howto
   5232 #ifdef RTYPE2HOWTO
   5233 
   5234 /* Get the howto structure for a reloc.  This is only used if the file
   5235    including this one defines coff_relocate_section to be
   5236    _bfd_coff_generic_relocate_section, so it is OK if it does not
   5237    always work.  It is the responsibility of the including file to
   5238    make sure it is reasonable if it is needed.  */
   5239 
   5240 static reloc_howto_type *
   5241 coff_rtype_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
   5242 		     asection *sec ATTRIBUTE_UNUSED,
   5243 		     struct internal_reloc *rel ATTRIBUTE_UNUSED,
   5244 		     struct coff_link_hash_entry *h ATTRIBUTE_UNUSED,
   5245 		     struct internal_syment *sym ATTRIBUTE_UNUSED,
   5246 		     bfd_vma *addendp ATTRIBUTE_UNUSED)
   5247 {
   5248   arelent genrel;
   5249 
   5250   genrel.howto = NULL;
   5251   RTYPE2HOWTO (&genrel, rel);
   5252   return genrel.howto;
   5253 }
   5254 
   5255 #else /* ! defined (RTYPE2HOWTO) */
   5256 
   5257 #define coff_rtype_to_howto NULL
   5258 
   5259 #endif /* ! defined (RTYPE2HOWTO) */
   5260 #endif /* ! defined (coff_rtype_to_howto) */
   5261 
   5262 /* This is stupid.  This function should be a boolean predicate.  */
   5263 
   5264 static long
   5265 coff_canonicalize_reloc (bfd * abfd,
   5266 			 sec_ptr section,
   5267 			 arelent ** relptr,
   5268 			 asymbol ** symbols)
   5269 {
   5270   arelent *tblptr = section->relocation;
   5271   unsigned int count = 0;
   5272 
   5273   if (section->flags & SEC_CONSTRUCTOR)
   5274     {
   5275       /* This section has relocs made up by us, they are not in the
   5276 	 file, so take them out of their chain and place them into
   5277 	 the data area provided.  */
   5278       arelent_chain *chain = section->constructor_chain;
   5279 
   5280       for (count = 0; count < section->reloc_count; count++)
   5281 	{
   5282 	  *relptr++ = &chain->relent;
   5283 	  chain = chain->next;
   5284 	}
   5285     }
   5286   else
   5287     {
   5288       if (! coff_slurp_reloc_table (abfd, section, symbols))
   5289 	return -1;
   5290 
   5291       tblptr = section->relocation;
   5292 
   5293       for (; count++ < section->reloc_count;)
   5294 	*relptr++ = tblptr++;
   5295     }
   5296   *relptr = 0;
   5297   return section->reloc_count;
   5298 }
   5299 
   5300 #ifndef coff_set_reloc
   5301 #define coff_set_reloc _bfd_generic_set_reloc
   5302 #endif
   5303 
   5304 #ifndef coff_reloc16_estimate
   5305 #define coff_reloc16_estimate dummy_reloc16_estimate
   5306 
   5307 static int
   5308 dummy_reloc16_estimate (bfd *abfd ATTRIBUTE_UNUSED,
   5309 			asection *input_section ATTRIBUTE_UNUSED,
   5310 			arelent *reloc ATTRIBUTE_UNUSED,
   5311 			unsigned int shrink ATTRIBUTE_UNUSED,
   5312 			struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
   5313 {
   5314   abort ();
   5315   return 0;
   5316 }
   5317 
   5318 #endif
   5319 
   5320 #ifndef coff_reloc16_extra_cases
   5321 
   5322 #define coff_reloc16_extra_cases dummy_reloc16_extra_cases
   5323 
   5324 /* This works even if abort is not declared in any header file.  */
   5325 
   5326 static void
   5327 dummy_reloc16_extra_cases (bfd *abfd ATTRIBUTE_UNUSED,
   5328 			   struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
   5329 			   struct bfd_link_order *link_order ATTRIBUTE_UNUSED,
   5330 			   arelent *reloc ATTRIBUTE_UNUSED,
   5331 			   bfd_byte *data ATTRIBUTE_UNUSED,
   5332 			   unsigned int *src_ptr ATTRIBUTE_UNUSED,
   5333 			   unsigned int *dst_ptr ATTRIBUTE_UNUSED)
   5334 {
   5335   abort ();
   5336 }
   5337 #endif
   5338 
   5339 /* If coff_relocate_section is defined, we can use the optimized COFF
   5340    backend linker.  Otherwise we must continue to use the old linker.  */
   5341 
   5342 #ifdef coff_relocate_section
   5343 
   5344 #ifndef coff_bfd_link_hash_table_create
   5345 #define coff_bfd_link_hash_table_create _bfd_coff_link_hash_table_create
   5346 #endif
   5347 #ifndef coff_bfd_link_add_symbols
   5348 #define coff_bfd_link_add_symbols _bfd_coff_link_add_symbols
   5349 #endif
   5350 #ifndef coff_bfd_final_link
   5351 #define coff_bfd_final_link _bfd_coff_final_link
   5352 #endif
   5353 
   5354 #else /* ! defined (coff_relocate_section) */
   5355 
   5356 #define coff_relocate_section NULL
   5357 #ifndef coff_bfd_link_hash_table_create
   5358 #define coff_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
   5359 #endif
   5360 #ifndef coff_bfd_link_add_symbols
   5361 #define coff_bfd_link_add_symbols _bfd_generic_link_add_symbols
   5362 #endif
   5363 #define coff_bfd_final_link _bfd_generic_final_link
   5364 
   5365 #endif /* ! defined (coff_relocate_section) */
   5366 
   5367 #define coff_bfd_link_just_syms      _bfd_generic_link_just_syms
   5368 #define coff_bfd_copy_link_hash_symbol_type \
   5369   _bfd_generic_copy_link_hash_symbol_type
   5370 #define coff_bfd_link_split_section  _bfd_generic_link_split_section
   5371 
   5372 #define coff_bfd_link_check_relocs   _bfd_generic_link_check_relocs
   5373 
   5374 #ifndef coff_start_final_link
   5375 #define coff_start_final_link NULL
   5376 #endif
   5377 
   5378 #ifndef coff_adjust_symndx
   5379 #define coff_adjust_symndx NULL
   5380 #endif
   5381 
   5382 #ifndef coff_link_add_one_symbol
   5383 #define coff_link_add_one_symbol _bfd_generic_link_add_one_symbol
   5384 #endif
   5385 
   5386 #ifndef coff_link_output_has_begun
   5387 
   5388 static bool
   5389 coff_link_output_has_begun (bfd * abfd,
   5390 			    struct coff_final_link_info * info ATTRIBUTE_UNUSED)
   5391 {
   5392   return abfd->output_has_begun;
   5393 }
   5394 #endif
   5395 
   5396 #ifndef coff_final_link_postscript
   5397 
   5398 static bool
   5399 coff_final_link_postscript (bfd * abfd ATTRIBUTE_UNUSED,
   5400 			    struct coff_final_link_info * pfinfo ATTRIBUTE_UNUSED)
   5401 {
   5402   return true;
   5403 }
   5404 #endif
   5405 
   5406 #ifndef coff_SWAP_aux_in
   5407 #define coff_SWAP_aux_in coff_swap_aux_in
   5408 #endif
   5409 #ifndef coff_SWAP_sym_in
   5410 #define coff_SWAP_sym_in coff_swap_sym_in
   5411 #endif
   5412 #ifndef coff_SWAP_lineno_in
   5413 #define coff_SWAP_lineno_in coff_swap_lineno_in
   5414 #endif
   5415 #ifndef coff_SWAP_aux_out
   5416 #define coff_SWAP_aux_out coff_swap_aux_out
   5417 #endif
   5418 #ifndef coff_SWAP_sym_out
   5419 #define coff_SWAP_sym_out coff_swap_sym_out
   5420 #endif
   5421 #ifndef coff_SWAP_lineno_out
   5422 #define coff_SWAP_lineno_out coff_swap_lineno_out
   5423 #endif
   5424 #ifndef coff_SWAP_reloc_out
   5425 #define coff_SWAP_reloc_out coff_swap_reloc_out
   5426 #endif
   5427 #ifndef coff_SWAP_filehdr_out
   5428 #define coff_SWAP_filehdr_out coff_swap_filehdr_out
   5429 #endif
   5430 #ifndef coff_SWAP_aouthdr_out
   5431 #define coff_SWAP_aouthdr_out coff_swap_aouthdr_out
   5432 #endif
   5433 #ifndef coff_SWAP_scnhdr_out
   5434 #define coff_SWAP_scnhdr_out coff_swap_scnhdr_out
   5435 #endif
   5436 #ifndef coff_SWAP_reloc_in
   5437 #define coff_SWAP_reloc_in coff_swap_reloc_in
   5438 #endif
   5439 #ifndef coff_SWAP_filehdr_in
   5440 #define coff_SWAP_filehdr_in coff_swap_filehdr_in
   5441 #endif
   5442 #ifndef coff_SWAP_aouthdr_in
   5443 #define coff_SWAP_aouthdr_in coff_swap_aouthdr_in
   5444 #endif
   5445 #ifndef coff_SWAP_scnhdr_in
   5446 #define coff_SWAP_scnhdr_in coff_swap_scnhdr_in
   5447 #endif
   5448 
   5449 static bfd_coff_backend_data bfd_coff_std_swap_table ATTRIBUTE_UNUSED =
   5450 {
   5451   coff_SWAP_aux_in, coff_SWAP_sym_in, coff_SWAP_lineno_in,
   5452   coff_SWAP_aux_out, coff_SWAP_sym_out,
   5453   coff_SWAP_lineno_out, coff_SWAP_reloc_out,
   5454   coff_SWAP_filehdr_out, coff_SWAP_aouthdr_out,
   5455   coff_SWAP_scnhdr_out,
   5456   FILHSZ, AOUTSZ, SCNHSZ, SYMESZ, AUXESZ, RELSZ, LINESZ, FILNMLEN,
   5457 #ifdef COFF_LONG_FILENAMES
   5458   true,
   5459 #else
   5460   false,
   5461 #endif
   5462   COFF_DEFAULT_LONG_SECTION_NAMES,
   5463   COFF_DEFAULT_SECTION_ALIGNMENT_POWER,
   5464 #ifdef COFF_FORCE_SYMBOLS_IN_STRINGS
   5465   true,
   5466 #else
   5467   false,
   5468 #endif
   5469 #ifdef COFF_DEBUG_STRING_WIDE_PREFIX
   5470   4,
   5471 #else
   5472   2,
   5473 #endif
   5474   32768,
   5475   coff_SWAP_filehdr_in, coff_SWAP_aouthdr_in, coff_SWAP_scnhdr_in,
   5476   coff_SWAP_reloc_in, coff_bad_format_hook, coff_set_arch_mach_hook,
   5477   coff_mkobject_hook, styp_to_sec_flags, coff_set_alignment_hook,
   5478   coff_slurp_symbol_table, symname_in_debug_hook, coff_pointerize_aux_hook,
   5479   coff_print_aux, coff_reloc16_extra_cases, coff_reloc16_estimate,
   5480   coff_classify_symbol, coff_compute_section_file_positions,
   5481   coff_start_final_link, coff_relocate_section, coff_rtype_to_howto,
   5482   coff_adjust_symndx, coff_link_add_one_symbol,
   5483   coff_link_output_has_begun, coff_final_link_postscript,
   5484   bfd_pe_print_pdata
   5485 };
   5486 
   5487 #ifdef TICOFF
   5488 /* COFF0 differs in file/section header size and relocation entry size.  */
   5489 
   5490 static bfd_coff_backend_data ticoff0_swap_table =
   5491 {
   5492   coff_SWAP_aux_in, coff_SWAP_sym_in, coff_SWAP_lineno_in,
   5493   coff_SWAP_aux_out, coff_SWAP_sym_out,
   5494   coff_SWAP_lineno_out, coff_swap_reloc_v0_out,
   5495   coff_SWAP_filehdr_out, coff_SWAP_aouthdr_out,
   5496   coff_SWAP_scnhdr_out,
   5497   FILHSZ_V0, AOUTSZ, SCNHSZ_V01, SYMESZ, AUXESZ, RELSZ_V0, LINESZ, FILNMLEN,
   5498 #ifdef COFF_LONG_FILENAMES
   5499   true,
   5500 #else
   5501   false,
   5502 #endif
   5503   COFF_DEFAULT_LONG_SECTION_NAMES,
   5504   COFF_DEFAULT_SECTION_ALIGNMENT_POWER,
   5505 #ifdef COFF_FORCE_SYMBOLS_IN_STRINGS
   5506   true,
   5507 #else
   5508   false,
   5509 #endif
   5510 #ifdef COFF_DEBUG_STRING_WIDE_PREFIX
   5511   4,
   5512 #else
   5513   2,
   5514 #endif
   5515   32768,
   5516   coff_SWAP_filehdr_in, coff_SWAP_aouthdr_in, coff_SWAP_scnhdr_in,
   5517   coff_swap_reloc_v0_in, ticoff0_bad_format_hook, coff_set_arch_mach_hook,
   5518   coff_mkobject_hook, styp_to_sec_flags, coff_set_alignment_hook,
   5519   coff_slurp_symbol_table, symname_in_debug_hook, coff_pointerize_aux_hook,
   5520   coff_print_aux, coff_reloc16_extra_cases, coff_reloc16_estimate,
   5521   coff_classify_symbol, coff_compute_section_file_positions,
   5522   coff_start_final_link, coff_relocate_section, coff_rtype_to_howto,
   5523   coff_adjust_symndx, coff_link_add_one_symbol,
   5524   coff_link_output_has_begun, coff_final_link_postscript,
   5525   bfd_pe_print_pdata
   5526 };
   5527 #endif
   5528 
   5529 #ifdef TICOFF
   5530 /* COFF1 differs in section header size.  */
   5531 
   5532 static bfd_coff_backend_data ticoff1_swap_table =
   5533 {
   5534   coff_SWAP_aux_in, coff_SWAP_sym_in, coff_SWAP_lineno_in,
   5535   coff_SWAP_aux_out, coff_SWAP_sym_out,
   5536   coff_SWAP_lineno_out, coff_SWAP_reloc_out,
   5537   coff_SWAP_filehdr_out, coff_SWAP_aouthdr_out,
   5538   coff_SWAP_scnhdr_out,
   5539   FILHSZ, AOUTSZ, SCNHSZ_V01, SYMESZ, AUXESZ, RELSZ, LINESZ, FILNMLEN,
   5540 #ifdef COFF_LONG_FILENAMES
   5541   true,
   5542 #else
   5543   false,
   5544 #endif
   5545   COFF_DEFAULT_LONG_SECTION_NAMES,
   5546   COFF_DEFAULT_SECTION_ALIGNMENT_POWER,
   5547 #ifdef COFF_FORCE_SYMBOLS_IN_STRINGS
   5548   true,
   5549 #else
   5550   false,
   5551 #endif
   5552 #ifdef COFF_DEBUG_STRING_WIDE_PREFIX
   5553   4,
   5554 #else
   5555   2,
   5556 #endif
   5557   32768,
   5558   coff_SWAP_filehdr_in, coff_SWAP_aouthdr_in, coff_SWAP_scnhdr_in,
   5559   coff_SWAP_reloc_in, ticoff1_bad_format_hook, coff_set_arch_mach_hook,
   5560   coff_mkobject_hook, styp_to_sec_flags, coff_set_alignment_hook,
   5561   coff_slurp_symbol_table, symname_in_debug_hook, coff_pointerize_aux_hook,
   5562   coff_print_aux, coff_reloc16_extra_cases, coff_reloc16_estimate,
   5563   coff_classify_symbol, coff_compute_section_file_positions,
   5564   coff_start_final_link, coff_relocate_section, coff_rtype_to_howto,
   5565   coff_adjust_symndx, coff_link_add_one_symbol,
   5566   coff_link_output_has_begun, coff_final_link_postscript,
   5567   bfd_pe_print_pdata	/* huh */
   5568 };
   5569 #endif
   5570 
   5571 #ifdef COFF_WITH_PE_BIGOBJ
   5572 /* The UID for bigobj files.  */
   5573 
   5574 static const char header_bigobj_classid[16] =
   5575 {
   5576   0xC7, 0xA1, 0xBA, 0xD1,
   5577   0xEE, 0xBA,
   5578   0xa9, 0x4b,
   5579   0xAF, 0x20,
   5580   0xFA, 0xF6, 0x6A, 0xA4, 0xDC, 0xB8
   5581 };
   5582 
   5583 /* Swap routines.  */
   5584 
   5585 static void
   5586 coff_bigobj_swap_filehdr_in (bfd * abfd, void * src, void * dst)
   5587 {
   5588   struct external_ANON_OBJECT_HEADER_BIGOBJ *filehdr_src =
   5589     (struct external_ANON_OBJECT_HEADER_BIGOBJ *) src;
   5590   struct internal_filehdr *filehdr_dst = (struct internal_filehdr *) dst;
   5591 
   5592   filehdr_dst->f_magic  = H_GET_16 (abfd, filehdr_src->Machine);
   5593   filehdr_dst->f_nscns  = H_GET_32 (abfd, filehdr_src->NumberOfSections);
   5594   filehdr_dst->f_timdat = H_GET_32 (abfd, filehdr_src->TimeDateStamp);
   5595   filehdr_dst->f_symptr =
   5596     GET_FILEHDR_SYMPTR (abfd, filehdr_src->PointerToSymbolTable);
   5597   filehdr_dst->f_nsyms  = H_GET_32 (abfd, filehdr_src->NumberOfSymbols);
   5598   filehdr_dst->f_opthdr = 0;
   5599   filehdr_dst->f_flags  = 0;
   5600 
   5601   /* Check other magic numbers.  */
   5602   if (H_GET_16 (abfd, filehdr_src->Sig1) != IMAGE_FILE_MACHINE_UNKNOWN
   5603       || H_GET_16 (abfd, filehdr_src->Sig2) != 0xffff
   5604       || H_GET_16 (abfd, filehdr_src->Version) != 2
   5605       || memcmp (filehdr_src->ClassID, header_bigobj_classid, 16) != 0)
   5606     filehdr_dst->f_opthdr = 0xffff;
   5607 
   5608   /* Note that CLR metadata are ignored.  */
   5609 }
   5610 
   5611 static unsigned int
   5612 coff_bigobj_swap_filehdr_out (bfd *abfd, void * in, void * out)
   5613 {
   5614   struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
   5615   struct external_ANON_OBJECT_HEADER_BIGOBJ *filehdr_out =
   5616     (struct external_ANON_OBJECT_HEADER_BIGOBJ *) out;
   5617 
   5618   memset (filehdr_out, 0, sizeof (*filehdr_out));
   5619 
   5620   H_PUT_16 (abfd, IMAGE_FILE_MACHINE_UNKNOWN, filehdr_out->Sig1);
   5621   H_PUT_16 (abfd, 0xffff, filehdr_out->Sig2);
   5622   H_PUT_16 (abfd, 2, filehdr_out->Version);
   5623   memcpy (filehdr_out->ClassID, header_bigobj_classid, 16);
   5624   H_PUT_16 (abfd, filehdr_in->f_magic, filehdr_out->Machine);
   5625   H_PUT_32 (abfd, filehdr_in->f_nscns, filehdr_out->NumberOfSections);
   5626   H_PUT_32 (abfd, filehdr_in->f_timdat, filehdr_out->TimeDateStamp);
   5627   PUT_FILEHDR_SYMPTR (abfd, filehdr_in->f_symptr,
   5628 		      filehdr_out->PointerToSymbolTable);
   5629   H_PUT_32 (abfd, filehdr_in->f_nsyms, filehdr_out->NumberOfSymbols);
   5630 
   5631   return bfd_coff_filhsz (abfd);
   5632 }
   5633 
   5634 static void
   5635 coff_bigobj_swap_sym_in (bfd * abfd, void * ext1, void * in1)
   5636 {
   5637   SYMENT_BIGOBJ *ext = (SYMENT_BIGOBJ *) ext1;
   5638   struct internal_syment *in = (struct internal_syment *) in1;
   5639 
   5640   if (ext->e.e_name[0] == 0)
   5641     {
   5642       in->_n._n_n._n_zeroes = 0;
   5643       in->_n._n_n._n_offset = H_GET_32 (abfd, ext->e.e.e_offset);
   5644     }
   5645   else
   5646     {
   5647 #if SYMNMLEN != E_SYMNMLEN
   5648 #error we need to cope with truncating or extending SYMNMLEN
   5649 #else
   5650       memcpy (in->_n._n_name, ext->e.e_name, SYMNMLEN);
   5651 #endif
   5652     }
   5653 
   5654   in->n_value = H_GET_32 (abfd, ext->e_value);
   5655   BFD_ASSERT (sizeof (in->n_scnum) >= 4);
   5656   in->n_scnum = H_GET_32 (abfd, ext->e_scnum);
   5657   in->n_type = H_GET_16 (abfd, ext->e_type);
   5658   in->n_sclass = H_GET_8 (abfd, ext->e_sclass);
   5659   in->n_numaux = H_GET_8 (abfd, ext->e_numaux);
   5660 }
   5661 
   5662 static unsigned int
   5663 coff_bigobj_swap_sym_out (bfd * abfd, void * inp, void * extp)
   5664 {
   5665   struct internal_syment *in = (struct internal_syment *) inp;
   5666   SYMENT_BIGOBJ *ext = (SYMENT_BIGOBJ *) extp;
   5667 
   5668   if (in->_n._n_name[0] == 0)
   5669     {
   5670       H_PUT_32 (abfd, 0, ext->e.e.e_zeroes);
   5671       H_PUT_32 (abfd, in->_n._n_n._n_offset, ext->e.e.e_offset);
   5672     }
   5673   else
   5674     {
   5675 #if SYMNMLEN != E_SYMNMLEN
   5676 #error we need to cope with truncating or extending SYMNMLEN
   5677 #else
   5678       memcpy (ext->e.e_name, in->_n._n_name, SYMNMLEN);
   5679 #endif
   5680     }
   5681 
   5682   H_PUT_32 (abfd, in->n_value, ext->e_value);
   5683   H_PUT_32 (abfd, in->n_scnum, ext->e_scnum);
   5684 
   5685   H_PUT_16 (abfd, in->n_type, ext->e_type);
   5686   H_PUT_8 (abfd, in->n_sclass, ext->e_sclass);
   5687   H_PUT_8 (abfd, in->n_numaux, ext->e_numaux);
   5688 
   5689   return SYMESZ_BIGOBJ;
   5690 }
   5691 
   5692 static void
   5693 coff_bigobj_swap_aux_in (bfd *abfd,
   5694 			 void * ext1,
   5695 			 int type,
   5696 			 int in_class,
   5697 			 int indx,
   5698 			 int numaux,
   5699 			 void * in1)
   5700 {
   5701   AUXENT_BIGOBJ *ext = (AUXENT_BIGOBJ *) ext1;
   5702   union internal_auxent *in = (union internal_auxent *) in1;
   5703 
   5704   /* Make sure that all fields in the aux structure are
   5705      initialised.  */
   5706   memset (in, 0, sizeof * in);
   5707   switch (in_class)
   5708     {
   5709     case C_FILE:
   5710       if (numaux > 1)
   5711 	{
   5712 	  if (indx == 0)
   5713 	    memcpy (in->x_file.x_n.x_fname, ext->File.Name,
   5714 		    numaux * sizeof (AUXENT_BIGOBJ));
   5715 	}
   5716       else
   5717 	memcpy (in->x_file.x_n.x_fname, ext->File.Name, sizeof (ext->File.Name));
   5718       break;
   5719 
   5720     case C_STAT:
   5721     case C_LEAFSTAT:
   5722     case C_HIDDEN:
   5723       if (type == T_NULL)
   5724 	{
   5725 	  in->x_scn.x_scnlen = H_GET_32 (abfd, ext->Section.Length);
   5726 	  in->x_scn.x_nreloc =
   5727 	    H_GET_16 (abfd, ext->Section.NumberOfRelocations);
   5728 	  in->x_scn.x_nlinno =
   5729 	    H_GET_16 (abfd, ext->Section.NumberOfLinenumbers);
   5730 	  in->x_scn.x_checksum = H_GET_32 (abfd, ext->Section.Checksum);
   5731 	  in->x_scn.x_associated = H_GET_16 (abfd, ext->Section.Number)
   5732 	    | (H_GET_16 (abfd, ext->Section.HighNumber) << 16);
   5733 	  in->x_scn.x_comdat = H_GET_8 (abfd, ext->Section.Selection);
   5734 	  return;
   5735 	}
   5736       break;
   5737 
   5738     default:
   5739       in->x_sym.x_tagndx.l = H_GET_32 (abfd, ext->Sym.WeakDefaultSymIndex);
   5740       /* Characteristics is ignored.  */
   5741       break;
   5742     }
   5743 }
   5744 
   5745 static unsigned int
   5746 coff_bigobj_swap_aux_out (bfd * abfd,
   5747 			  void * inp,
   5748 			  int type,
   5749 			  int in_class,
   5750 			  int indx ATTRIBUTE_UNUSED,
   5751 			  int numaux ATTRIBUTE_UNUSED,
   5752 			  void * extp)
   5753 {
   5754   union internal_auxent * in = (union internal_auxent *) inp;
   5755   AUXENT_BIGOBJ *ext = (AUXENT_BIGOBJ *) extp;
   5756 
   5757   memset (ext, 0, AUXESZ);
   5758 
   5759   switch (in_class)
   5760     {
   5761     case C_FILE:
   5762       memcpy (ext->File.Name, in->x_file.x_n.x_fname, sizeof (ext->File.Name));
   5763 
   5764       return AUXESZ;
   5765 
   5766     case C_STAT:
   5767     case C_LEAFSTAT:
   5768     case C_HIDDEN:
   5769       if (type == T_NULL)
   5770 	{
   5771 	  H_PUT_32 (abfd, in->x_scn.x_scnlen, ext->Section.Length);
   5772 	  H_PUT_16 (abfd, in->x_scn.x_nreloc,
   5773 		    ext->Section.NumberOfRelocations);
   5774 	  H_PUT_16 (abfd, in->x_scn.x_nlinno,
   5775 		    ext->Section.NumberOfLinenumbers);
   5776 	  H_PUT_32 (abfd, in->x_scn.x_checksum, ext->Section.Checksum);
   5777 	  H_PUT_16 (abfd, in->x_scn.x_associated & 0xffff,
   5778 		    ext->Section.Number);
   5779 	  H_PUT_16 (abfd, (in->x_scn.x_associated >> 16),
   5780 		    ext->Section.HighNumber);
   5781 	  H_PUT_8 (abfd, in->x_scn.x_comdat, ext->Section.Selection);
   5782 	  return AUXESZ;
   5783 	}
   5784       break;
   5785     }
   5786 
   5787   H_PUT_32 (abfd, in->x_sym.x_tagndx.l, ext->Sym.WeakDefaultSymIndex);
   5788   H_PUT_32 (abfd, 1, ext->Sym.WeakSearchType);
   5789 
   5790   return AUXESZ;
   5791 }
   5792 
   5793 static bfd_coff_backend_data bigobj_swap_table =
   5794 {
   5795   coff_bigobj_swap_aux_in, coff_bigobj_swap_sym_in, coff_SWAP_lineno_in,
   5796   coff_bigobj_swap_aux_out, coff_bigobj_swap_sym_out,
   5797   coff_SWAP_lineno_out, coff_SWAP_reloc_out,
   5798   coff_bigobj_swap_filehdr_out, coff_SWAP_aouthdr_out,
   5799   coff_SWAP_scnhdr_out,
   5800   FILHSZ_BIGOBJ, AOUTSZ, SCNHSZ, SYMESZ_BIGOBJ, AUXESZ_BIGOBJ,
   5801    RELSZ, LINESZ, FILNMLEN_BIGOBJ,
   5802   true,
   5803   COFF_DEFAULT_LONG_SECTION_NAMES,
   5804   COFF_DEFAULT_SECTION_ALIGNMENT_POWER,
   5805   false,
   5806   2,
   5807   1U << 31,
   5808   coff_bigobj_swap_filehdr_in, coff_SWAP_aouthdr_in, coff_SWAP_scnhdr_in,
   5809   coff_SWAP_reloc_in, coff_bad_format_hook, coff_set_arch_mach_hook,
   5810   coff_mkobject_hook, styp_to_sec_flags, coff_set_alignment_hook,
   5811   coff_slurp_symbol_table, symname_in_debug_hook, coff_pointerize_aux_hook,
   5812   coff_print_aux, coff_reloc16_extra_cases, coff_reloc16_estimate,
   5813   coff_classify_symbol, coff_compute_section_file_positions,
   5814   coff_start_final_link, coff_relocate_section, coff_rtype_to_howto,
   5815   coff_adjust_symndx, coff_link_add_one_symbol,
   5816   coff_link_output_has_begun, coff_final_link_postscript,
   5817   bfd_pe_print_pdata	/* huh */
   5818 };
   5819 
   5820 #endif /* COFF_WITH_PE_BIGOBJ */
   5821 
   5822 #ifndef coff_close_and_cleanup
   5823 #define coff_close_and_cleanup		    _bfd_coff_close_and_cleanup
   5824 #endif
   5825 
   5826 #ifndef coff_bfd_free_cached_info
   5827 #define coff_bfd_free_cached_info	    _bfd_generic_bfd_free_cached_info
   5828 #endif
   5829 
   5830 #ifndef coff_get_section_contents
   5831 #define coff_get_section_contents	    _bfd_generic_get_section_contents
   5832 #endif
   5833 
   5834 #ifndef coff_bfd_copy_private_symbol_data
   5835 #define coff_bfd_copy_private_symbol_data   _bfd_generic_bfd_copy_private_symbol_data
   5836 #endif
   5837 
   5838 #ifndef coff_bfd_copy_private_header_data
   5839 #define coff_bfd_copy_private_header_data   _bfd_generic_bfd_copy_private_header_data
   5840 #endif
   5841 
   5842 #ifndef coff_bfd_copy_private_section_data
   5843 #define coff_bfd_copy_private_section_data  _bfd_generic_bfd_copy_private_section_data
   5844 #endif
   5845 
   5846 #ifndef coff_bfd_copy_private_bfd_data
   5847 #define coff_bfd_copy_private_bfd_data      _bfd_generic_bfd_copy_private_bfd_data
   5848 #endif
   5849 
   5850 #ifndef coff_bfd_merge_private_bfd_data
   5851 #define coff_bfd_merge_private_bfd_data     _bfd_generic_bfd_merge_private_bfd_data
   5852 #endif
   5853 
   5854 #ifndef coff_bfd_set_private_flags
   5855 #define coff_bfd_set_private_flags	    _bfd_generic_bfd_set_private_flags
   5856 #endif
   5857 
   5858 #ifndef coff_bfd_print_private_bfd_data
   5859 #define coff_bfd_print_private_bfd_data     _bfd_generic_bfd_print_private_bfd_data
   5860 #endif
   5861 
   5862 #ifndef coff_bfd_is_local_label_name
   5863 #define coff_bfd_is_local_label_name	    _bfd_coff_is_local_label_name
   5864 #endif
   5865 
   5866 #ifndef coff_bfd_is_target_special_symbol
   5867 #define coff_bfd_is_target_special_symbol   _bfd_bool_bfd_asymbol_false
   5868 #endif
   5869 
   5870 #ifndef coff_read_minisymbols
   5871 #define coff_read_minisymbols		    _bfd_generic_read_minisymbols
   5872 #endif
   5873 
   5874 #ifndef coff_minisymbol_to_symbol
   5875 #define coff_minisymbol_to_symbol	    _bfd_generic_minisymbol_to_symbol
   5876 #endif
   5877 
   5878 /* The reloc lookup routine must be supplied by each individual COFF
   5879    backend.  */
   5880 #ifndef coff_bfd_reloc_type_lookup
   5881 #define coff_bfd_reloc_type_lookup	    _bfd_norelocs_bfd_reloc_type_lookup
   5882 #endif
   5883 #ifndef coff_bfd_reloc_name_lookup
   5884 #define coff_bfd_reloc_name_lookup    _bfd_norelocs_bfd_reloc_name_lookup
   5885 #endif
   5886 
   5887 #ifndef coff_bfd_get_relocated_section_contents
   5888 #define coff_bfd_get_relocated_section_contents \
   5889   bfd_generic_get_relocated_section_contents
   5890 #endif
   5891 
   5892 #ifndef coff_bfd_relax_section
   5893 #define coff_bfd_relax_section		    bfd_generic_relax_section
   5894 #endif
   5895 
   5896 #ifndef coff_bfd_gc_sections
   5897 #define coff_bfd_gc_sections		    bfd_coff_gc_sections
   5898 #endif
   5899 
   5900 #ifndef coff_bfd_lookup_section_flags
   5901 #define coff_bfd_lookup_section_flags	    bfd_generic_lookup_section_flags
   5902 #endif
   5903 
   5904 #ifndef coff_bfd_merge_sections
   5905 #define coff_bfd_merge_sections		    bfd_generic_merge_sections
   5906 #endif
   5907 
   5908 #ifndef coff_bfd_is_group_section
   5909 #define coff_bfd_is_group_section	    bfd_generic_is_group_section
   5910 #endif
   5911 
   5912 #ifndef coff_bfd_group_name
   5913 #define coff_bfd_group_name		    bfd_coff_group_name
   5914 #endif
   5915 
   5916 #ifndef coff_bfd_discard_group
   5917 #define coff_bfd_discard_group		    bfd_generic_discard_group
   5918 #endif
   5919 
   5920 #ifndef coff_section_already_linked
   5921 #define coff_section_already_linked \
   5922   _bfd_coff_section_already_linked
   5923 #endif
   5924 
   5925 #ifndef coff_bfd_define_common_symbol
   5926 #define coff_bfd_define_common_symbol	    bfd_generic_define_common_symbol
   5927 #endif
   5928 
   5929 #ifndef coff_bfd_link_hide_symbol
   5930 #define coff_bfd_link_hide_symbol	    _bfd_generic_link_hide_symbol
   5931 #endif
   5932 
   5933 #ifndef coff_bfd_define_start_stop
   5934 #define coff_bfd_define_start_stop	    bfd_generic_define_start_stop
   5935 #endif
   5936 
   5937 #define CREATE_BIG_COFF_TARGET_VEC(VAR, NAME, EXTRA_O_FLAGS, EXTRA_S_FLAGS, UNDER, ALTERNATIVE, SWAP_TABLE)	\
   5938 const bfd_target VAR =							\
   5939 {									\
   5940   NAME ,								\
   5941   bfd_target_coff_flavour,						\
   5942   BFD_ENDIAN_BIG,		/* Data byte order is big.  */		\
   5943   BFD_ENDIAN_BIG,		/* Header byte order is big.  */	\
   5944   /* object flags */							\
   5945   (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG |			\
   5946    HAS_SYMS | HAS_LOCALS | WP_TEXT | EXTRA_O_FLAGS),			\
   5947   /* section flags */							\
   5948   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | EXTRA_S_FLAGS),\
   5949   UNDER,			/* Leading symbol underscore.  */	\
   5950   '/',				/* AR_pad_char.  */			\
   5951   15,				/* AR_max_namelen.  */			\
   5952   0,				/* match priority.  */			\
   5953   TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols.  */ \
   5954 									\
   5955   /* Data conversion functions.  */					\
   5956   bfd_getb64, bfd_getb_signed_64, bfd_putb64,				\
   5957   bfd_getb32, bfd_getb_signed_32, bfd_putb32,				\
   5958   bfd_getb16, bfd_getb_signed_16, bfd_putb16,				\
   5959 									\
   5960   /* Header conversion functions.  */					\
   5961   bfd_getb64, bfd_getb_signed_64, bfd_putb64,				\
   5962   bfd_getb32, bfd_getb_signed_32, bfd_putb32,				\
   5963   bfd_getb16, bfd_getb_signed_16, bfd_putb16,				\
   5964 									\
   5965   {				/* bfd_check_format.  */		\
   5966     _bfd_dummy_target,							\
   5967     coff_object_p,							\
   5968     bfd_generic_archive_p,						\
   5969     _bfd_dummy_target							\
   5970   },									\
   5971   {				/* bfd_set_format.  */			\
   5972     _bfd_bool_bfd_false_error,						\
   5973     coff_mkobject,							\
   5974     _bfd_generic_mkarchive,						\
   5975     _bfd_bool_bfd_false_error						\
   5976   },									\
   5977   {				/* bfd_write_contents.  */		\
   5978     _bfd_bool_bfd_false_error,						\
   5979     coff_write_object_contents,						\
   5980     _bfd_write_archive_contents,					\
   5981     _bfd_bool_bfd_false_error						\
   5982   },									\
   5983 									\
   5984   BFD_JUMP_TABLE_GENERIC (coff),					\
   5985   BFD_JUMP_TABLE_COPY (coff),						\
   5986   BFD_JUMP_TABLE_CORE (_bfd_nocore),					\
   5987   BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),				\
   5988   BFD_JUMP_TABLE_SYMBOLS (coff),					\
   5989   BFD_JUMP_TABLE_RELOCS (coff),						\
   5990   BFD_JUMP_TABLE_WRITE (coff),						\
   5991   BFD_JUMP_TABLE_LINK (coff),						\
   5992   BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),				\
   5993 									\
   5994   ALTERNATIVE,								\
   5995 									\
   5996   SWAP_TABLE								\
   5997 };
   5998 
   5999 #define CREATE_BIGHDR_COFF_TARGET_VEC(VAR, NAME, EXTRA_O_FLAGS, EXTRA_S_FLAGS, UNDER, ALTERNATIVE, SWAP_TABLE)	\
   6000 const bfd_target VAR =							\
   6001 {									\
   6002   NAME ,								\
   6003   bfd_target_coff_flavour,						\
   6004   BFD_ENDIAN_LITTLE,		/* Data byte order is little.  */	\
   6005   BFD_ENDIAN_BIG,		/* Header byte order is big.  */	\
   6006   /* object flags */							\
   6007   (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG |			\
   6008    HAS_SYMS | HAS_LOCALS | WP_TEXT | EXTRA_O_FLAGS),			\
   6009   /* section flags */							\
   6010   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | EXTRA_S_FLAGS),\
   6011   UNDER,			/* Leading symbol underscore.  */	\
   6012   '/',				/* AR_pad_char.  */			\
   6013   15,				/* AR_max_namelen.  */			\
   6014   0,				/* match priority.  */			\
   6015   TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols.  */ \
   6016 									\
   6017   /* Data conversion functions.  */					\
   6018   bfd_getb64, bfd_getb_signed_64, bfd_putb64,				\
   6019   bfd_getb32, bfd_getb_signed_32, bfd_putb32,				\
   6020   bfd_getb16, bfd_getb_signed_16, bfd_putb16,				\
   6021 									\
   6022   /* Header conversion functions.  */					\
   6023   bfd_getb64, bfd_getb_signed_64, bfd_putb64,				\
   6024   bfd_getb32, bfd_getb_signed_32, bfd_putb32,				\
   6025   bfd_getb16, bfd_getb_signed_16, bfd_putb16,				\
   6026 									\
   6027   {				/* bfd_check_format.  */		\
   6028     _bfd_dummy_target,							\
   6029     coff_object_p,							\
   6030     bfd_generic_archive_p,						\
   6031     _bfd_dummy_target							\
   6032   },									\
   6033   {				/* bfd_set_format.  */			\
   6034     _bfd_bool_bfd_false_error,						\
   6035     coff_mkobject,							\
   6036     _bfd_generic_mkarchive,						\
   6037     _bfd_bool_bfd_false_error						\
   6038   },									\
   6039   {				/* bfd_write_contents.  */		\
   6040     _bfd_bool_bfd_false_error,						\
   6041     coff_write_object_contents,						\
   6042     _bfd_write_archive_contents,					\
   6043     _bfd_bool_bfd_false_error						\
   6044   },									\
   6045 									\
   6046   BFD_JUMP_TABLE_GENERIC (coff),					\
   6047   BFD_JUMP_TABLE_COPY (coff),						\
   6048   BFD_JUMP_TABLE_CORE (_bfd_nocore),					\
   6049   BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),				\
   6050   BFD_JUMP_TABLE_SYMBOLS (coff),					\
   6051   BFD_JUMP_TABLE_RELOCS (coff),						\
   6052   BFD_JUMP_TABLE_WRITE (coff),						\
   6053   BFD_JUMP_TABLE_LINK (coff),						\
   6054   BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),				\
   6055 									\
   6056   ALTERNATIVE,								\
   6057 									\
   6058   SWAP_TABLE								\
   6059 };
   6060 
   6061 #define CREATE_LITTLE_COFF_TARGET_VEC(VAR, NAME, EXTRA_O_FLAGS, EXTRA_S_FLAGS, UNDER, ALTERNATIVE, SWAP_TABLE)	\
   6062 const bfd_target VAR =							\
   6063 {									\
   6064   NAME ,								\
   6065   bfd_target_coff_flavour,						\
   6066   BFD_ENDIAN_LITTLE,		/* Data byte order is little.  */	\
   6067   BFD_ENDIAN_LITTLE,		/* Header byte order is little.  */	\
   6068 	/* object flags */						\
   6069   (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG |			\
   6070    HAS_SYMS | HAS_LOCALS | WP_TEXT | EXTRA_O_FLAGS),			\
   6071 	/* section flags */						\
   6072   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | EXTRA_S_FLAGS),\
   6073   UNDER,			/* Leading symbol underscore.  */	\
   6074   '/',				/* AR_pad_char.  */			\
   6075   15,				/* AR_max_namelen.  */			\
   6076   0,				/* match priority.  */			\
   6077   TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols.  */ \
   6078 									\
   6079   /* Data conversion functions.  */					\
   6080   bfd_getl64, bfd_getl_signed_64, bfd_putl64,				\
   6081   bfd_getl32, bfd_getl_signed_32, bfd_putl32,				\
   6082   bfd_getl16, bfd_getl_signed_16, bfd_putl16,				\
   6083   /* Header conversion functions.  */					\
   6084   bfd_getl64, bfd_getl_signed_64, bfd_putl64,				\
   6085   bfd_getl32, bfd_getl_signed_32, bfd_putl32,				\
   6086   bfd_getl16, bfd_getl_signed_16, bfd_putl16,				\
   6087 									\
   6088   {				/* bfd_check_format.  */		\
   6089     _bfd_dummy_target,							\
   6090     coff_object_p,							\
   6091     bfd_generic_archive_p,						\
   6092     _bfd_dummy_target							\
   6093   },									\
   6094   {				/* bfd_set_format.  */			\
   6095     _bfd_bool_bfd_false_error,						\
   6096     coff_mkobject,							\
   6097     _bfd_generic_mkarchive,						\
   6098     _bfd_bool_bfd_false_error						\
   6099   },									\
   6100   {				/* bfd_write_contents.  */		\
   6101     _bfd_bool_bfd_false_error,						\
   6102     coff_write_object_contents,						\
   6103     _bfd_write_archive_contents,					\
   6104     _bfd_bool_bfd_false_error						\
   6105   },									\
   6106 									\
   6107   BFD_JUMP_TABLE_GENERIC (coff),					\
   6108   BFD_JUMP_TABLE_COPY (coff),						\
   6109   BFD_JUMP_TABLE_CORE (_bfd_nocore),					\
   6110   BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),				\
   6111   BFD_JUMP_TABLE_SYMBOLS (coff),					\
   6112   BFD_JUMP_TABLE_RELOCS (coff),						\
   6113   BFD_JUMP_TABLE_WRITE (coff),						\
   6114   BFD_JUMP_TABLE_LINK (coff),						\
   6115   BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),				\
   6116 									\
   6117   ALTERNATIVE,								\
   6118 									\
   6119   SWAP_TABLE								\
   6120 };
   6121