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