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