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