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