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