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