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