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