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