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