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