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