Revert accidental checkin.
[binutils.git] / bfd / coffcode.h
blob1dac3c3ac1b496012075dd09d045d5875e261b65
1 /* Support for the generic parts of most COFF variants, for BFD.
2 Copyright 1990, 91, 92, 93, 94, 95, 96, 97, 98, 99, 2000
3 Free Software Foundation, Inc.
4 Written by Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 Most of this hacked by Steve Chamberlain,
24 sac@cygnus.com
28 SECTION
29 coff backends
31 BFD supports a number of different flavours of coff format.
32 The major differences between formats are the sizes and
33 alignments of fields in structures on disk, and the occasional
34 extra field.
36 Coff in all its varieties is implemented with a few common
37 files and a number of implementation specific files. For
38 example, The 88k bcs coff format is implemented in the file
39 @file{coff-m88k.c}. This file @code{#include}s
40 @file{coff/m88k.h} which defines the external structure of the
41 coff format for the 88k, and @file{coff/internal.h} which
42 defines the internal structure. @file{coff-m88k.c} also
43 defines the relocations used by the 88k format
44 @xref{Relocations}.
46 The Intel i960 processor version of coff is implemented in
47 @file{coff-i960.c}. This file has the same structure as
48 @file{coff-m88k.c}, except that it includes @file{coff/i960.h}
49 rather than @file{coff-m88k.h}.
51 SUBSECTION
52 Porting to a new version of coff
54 The recommended method is to select from the existing
55 implementations the version of coff which is most like the one
56 you want to use. For example, we'll say that i386 coff is
57 the one you select, and that your coff flavour is called foo.
58 Copy @file{i386coff.c} to @file{foocoff.c}, copy
59 @file{../include/coff/i386.h} to @file{../include/coff/foo.h},
60 and add the lines to @file{targets.c} and @file{Makefile.in}
61 so that your new back end is used. Alter the shapes of the
62 structures in @file{../include/coff/foo.h} so that they match
63 what you need. You will probably also have to add
64 @code{#ifdef}s to the code in @file{coff/internal.h} and
65 @file{coffcode.h} if your version of coff is too wild.
67 You can verify that your new BFD backend works quite simply by
68 building @file{objdump} from the @file{binutils} directory,
69 and making sure that its version of what's going on and your
70 host system's idea (assuming it has the pretty standard coff
71 dump utility, usually called @code{att-dump} or just
72 @code{dump}) are the same. Then clean up your code, and send
73 what you've done to Cygnus. Then your stuff will be in the
74 next release, and you won't have to keep integrating it.
76 SUBSECTION
77 How the coff backend works
79 SUBSUBSECTION
80 File layout
82 The Coff backend is split into generic routines that are
83 applicable to any Coff target and routines that are specific
84 to a particular target. The target-specific routines are
85 further split into ones which are basically the same for all
86 Coff targets except that they use the external symbol format
87 or use different values for certain constants.
89 The generic routines are in @file{coffgen.c}. These routines
90 work for any Coff target. They use some hooks into the target
91 specific code; the hooks are in a @code{bfd_coff_backend_data}
92 structure, one of which exists for each target.
94 The essentially similar target-specific routines are in
95 @file{coffcode.h}. This header file includes executable C code.
96 The various Coff targets first include the appropriate Coff
97 header file, make any special defines that are needed, and
98 then include @file{coffcode.h}.
100 Some of the Coff targets then also have additional routines in
101 the target source file itself.
103 For example, @file{coff-i960.c} includes
104 @file{coff/internal.h} and @file{coff/i960.h}. It then
105 defines a few constants, such as @code{I960}, and includes
106 @file{coffcode.h}. Since the i960 has complex relocation
107 types, @file{coff-i960.c} also includes some code to
108 manipulate the i960 relocs. This code is not in
109 @file{coffcode.h} because it would not be used by any other
110 target.
112 SUBSUBSECTION
113 Bit twiddling
115 Each flavour of coff supported in BFD has its own header file
116 describing the external layout of the structures. There is also
117 an internal description of the coff layout, in
118 @file{coff/internal.h}. A major function of the
119 coff backend is swapping the bytes and twiddling the bits to
120 translate the external form of the structures into the normal
121 internal form. This is all performed in the
122 @code{bfd_swap}_@i{thing}_@i{direction} routines. Some
123 elements are different sizes between different versions of
124 coff; it is the duty of the coff version specific include file
125 to override the definitions of various packing routines in
126 @file{coffcode.h}. E.g., the size of line number entry in coff is
127 sometimes 16 bits, and sometimes 32 bits. @code{#define}ing
128 @code{PUT_LNSZ_LNNO} and @code{GET_LNSZ_LNNO} will select the
129 correct one. No doubt, some day someone will find a version of
130 coff which has a varying field size not catered to at the
131 moment. To port BFD, that person will have to add more @code{#defines}.
132 Three of the bit twiddling routines are exported to
133 @code{gdb}; @code{coff_swap_aux_in}, @code{coff_swap_sym_in}
134 and @code{coff_swap_lineno_in}. @code{GDB} reads the symbol
135 table on its own, but uses BFD to fix things up. More of the
136 bit twiddlers are exported for @code{gas};
137 @code{coff_swap_aux_out}, @code{coff_swap_sym_out},
138 @code{coff_swap_lineno_out}, @code{coff_swap_reloc_out},
139 @code{coff_swap_filehdr_out}, @code{coff_swap_aouthdr_out},
140 @code{coff_swap_scnhdr_out}. @code{Gas} currently keeps track
141 of all the symbol table and reloc drudgery itself, thereby
142 saving the internal BFD overhead, but uses BFD to swap things
143 on the way out, making cross ports much safer. Doing so also
144 allows BFD (and thus the linker) to use the same header files
145 as @code{gas}, which makes one avenue to disaster disappear.
147 SUBSUBSECTION
148 Symbol reading
150 The simple canonical form for symbols used by BFD is not rich
151 enough to keep all the information available in a coff symbol
152 table. The back end gets around this problem by keeping the original
153 symbol table around, "behind the scenes".
155 When a symbol table is requested (through a call to
156 @code{bfd_canonicalize_symtab}), a request gets through to
157 @code{coff_get_normalized_symtab}. This reads the symbol table from
158 the coff file and swaps all the structures inside into the
159 internal form. It also fixes up all the pointers in the table
160 (represented in the file by offsets from the first symbol in
161 the table) into physical pointers to elements in the new
162 internal table. This involves some work since the meanings of
163 fields change depending upon context: a field that is a
164 pointer to another structure in the symbol table at one moment
165 may be the size in bytes of a structure at the next. Another
166 pass is made over the table. All symbols which mark file names
167 (<<C_FILE>> symbols) are modified so that the internal
168 string points to the value in the auxent (the real filename)
169 rather than the normal text associated with the symbol
170 (@code{".file"}).
172 At this time the symbol names are moved around. Coff stores
173 all symbols less than nine characters long physically
174 within the symbol table; longer strings are kept at the end of
175 the file in the string table. This pass moves all strings
176 into memory and replaces them with pointers to the strings.
178 The symbol table is massaged once again, this time to create
179 the canonical table used by the BFD application. Each symbol
180 is inspected in turn, and a decision made (using the
181 @code{sclass} field) about the various flags to set in the
182 @code{asymbol}. @xref{Symbols}. The generated canonical table
183 shares strings with the hidden internal symbol table.
185 Any linenumbers are read from the coff file too, and attached
186 to the symbols which own the functions the linenumbers belong to.
188 SUBSUBSECTION
189 Symbol writing
191 Writing a symbol to a coff file which didn't come from a coff
192 file will lose any debugging information. The @code{asymbol}
193 structure remembers the BFD from which the symbol was taken, and on
194 output the back end makes sure that the same destination target as
195 source target is present.
197 When the symbols have come from a coff file then all the
198 debugging information is preserved.
200 Symbol tables are provided for writing to the back end in a
201 vector of pointers to pointers. This allows applications like
202 the linker to accumulate and output large symbol tables
203 without having to do too much byte copying.
205 This function runs through the provided symbol table and
206 patches each symbol marked as a file place holder
207 (@code{C_FILE}) to point to the next file place holder in the
208 list. It also marks each @code{offset} field in the list with
209 the offset from the first symbol of the current symbol.
211 Another function of this procedure is to turn the canonical
212 value form of BFD into the form used by coff. Internally, BFD
213 expects symbol values to be offsets from a section base; so a
214 symbol physically at 0x120, but in a section starting at
215 0x100, would have the value 0x20. Coff expects symbols to
216 contain their final value, so symbols have their values
217 changed at this point to reflect their sum with their owning
218 section. This transformation uses the
219 <<output_section>> field of the @code{asymbol}'s
220 @code{asection} @xref{Sections}.
222 o <<coff_mangle_symbols>>
224 This routine runs though the provided symbol table and uses
225 the offsets generated by the previous pass and the pointers
226 generated when the symbol table was read in to create the
227 structured hierachy required by coff. It changes each pointer
228 to a symbol into the index into the symbol table of the asymbol.
230 o <<coff_write_symbols>>
232 This routine runs through the symbol table and patches up the
233 symbols from their internal form into the coff way, calls the
234 bit twiddlers, and writes out the table to the file.
239 INTERNAL_DEFINITION
240 coff_symbol_type
242 DESCRIPTION
243 The hidden information for an <<asymbol>> is described in a
244 <<combined_entry_type>>:
246 CODE_FRAGMENT
248 .typedef struct coff_ptr_struct
251 . {* Remembers the offset from the first symbol in the file for
252 . this symbol. Generated by coff_renumber_symbols. *}
253 .unsigned int offset;
255 . {* Should the value of this symbol be renumbered. Used for
256 . XCOFF C_BSTAT symbols. Set by coff_slurp_symbol_table. *}
257 .unsigned int fix_value : 1;
259 . {* Should the tag field of this symbol be renumbered.
260 . Created by coff_pointerize_aux. *}
261 .unsigned int fix_tag : 1;
263 . {* Should the endidx field of this symbol be renumbered.
264 . Created by coff_pointerize_aux. *}
265 .unsigned int fix_end : 1;
267 . {* Should the x_csect.x_scnlen field be renumbered.
268 . Created by coff_pointerize_aux. *}
269 .unsigned int fix_scnlen : 1;
271 . {* Fix up an XCOFF C_BINCL/C_EINCL symbol. The value is the
272 . index into the line number entries. Set by
273 . coff_slurp_symbol_table. *}
274 .unsigned int fix_line : 1;
276 . {* The container for the symbol structure as read and translated
277 . from the file. *}
279 .union {
280 . union internal_auxent auxent;
281 . struct internal_syment syment;
282 . } u;
283 .} combined_entry_type;
286 .{* Each canonical asymbol really looks like this: *}
288 .typedef struct coff_symbol_struct
290 . {* The actual symbol which the rest of BFD works with *}
291 .asymbol symbol;
293 . {* A pointer to the hidden information for this symbol *}
294 .combined_entry_type *native;
296 . {* A pointer to the linenumber information for this symbol *}
297 .struct lineno_cache_entry *lineno;
299 . {* Have the line numbers been relocated yet ? *}
300 .boolean done_lineno;
301 .} coff_symbol_type;
305 #ifdef COFF_WITH_PE
306 #include "peicode.h"
307 #else
308 #include "coffswap.h"
309 #endif
311 #define STRING_SIZE_SIZE (4)
313 static long sec_to_styp_flags PARAMS ((const char *, flagword));
314 static flagword styp_to_sec_flags
315 PARAMS ((bfd *, PTR, const char *, asection *));
316 static boolean coff_bad_format_hook PARAMS ((bfd *, PTR));
317 static void coff_set_custom_section_alignment
318 PARAMS ((bfd *, asection *, const struct coff_section_alignment_entry *,
319 const unsigned int));
320 static boolean coff_new_section_hook PARAMS ((bfd *, asection *));
321 static boolean coff_set_arch_mach_hook PARAMS ((bfd *, PTR));
322 static boolean coff_write_relocs PARAMS ((bfd *, int));
323 static boolean coff_set_flags
324 PARAMS ((bfd *, unsigned int *, unsigned short *));
325 static boolean coff_set_arch_mach
326 PARAMS ((bfd *, enum bfd_architecture, unsigned long));
327 static boolean coff_compute_section_file_positions PARAMS ((bfd *));
328 static boolean coff_write_object_contents PARAMS ((bfd *));
329 static boolean coff_set_section_contents
330 PARAMS ((bfd *, asection *, PTR, file_ptr, bfd_size_type));
331 static PTR buy_and_read PARAMS ((bfd *, file_ptr, int, size_t));
332 static boolean coff_slurp_line_table PARAMS ((bfd *, asection *));
333 static boolean coff_slurp_symbol_table PARAMS ((bfd *));
334 static enum coff_symbol_classification coff_classify_symbol
335 PARAMS ((bfd *, struct internal_syment *));
336 static boolean coff_slurp_reloc_table PARAMS ((bfd *, asection *, asymbol **));
337 static long coff_canonicalize_reloc
338 PARAMS ((bfd *, asection *, arelent **, asymbol **));
339 #ifndef coff_mkobject_hook
340 static PTR coff_mkobject_hook PARAMS ((bfd *, PTR, PTR));
341 #endif
343 /* void warning(); */
345 /* Return a word with STYP_* (scnhdr.s_flags) flags set to represent
346 the incoming SEC_* flags. The inverse of this function is
347 styp_to_sec_flags(). NOTE: If you add to/change this routine, you
348 should probably mirror the changes in styp_to_sec_flags(). */
350 #ifndef COFF_WITH_PE
352 /* Macros for setting debugging flags. */
353 #ifdef STYP_DEBUG
354 #define STYP_XCOFF_DEBUG STYP_DEBUG
355 #else
356 #define STYP_XCOFF_DEBUG STYP_INFO
357 #endif
359 #ifdef COFF_ALIGN_IN_S_FLAGS
360 #define STYP_DEBUG_INFO STYP_DSECT
361 #else
362 #define STYP_DEBUG_INFO STYP_INFO
363 #endif
365 static long
366 sec_to_styp_flags (sec_name, sec_flags)
367 CONST char *sec_name;
368 flagword sec_flags;
370 long styp_flags = 0;
372 if (!strcmp (sec_name, _TEXT))
374 styp_flags = STYP_TEXT;
376 else if (!strcmp (sec_name, _DATA))
378 styp_flags = STYP_DATA;
380 else if (!strcmp (sec_name, _BSS))
382 styp_flags = STYP_BSS;
383 #ifdef _COMMENT
385 else if (!strcmp (sec_name, _COMMENT))
387 styp_flags = STYP_INFO;
388 #endif /* _COMMENT */
389 #ifdef _LIB
391 else if (!strcmp (sec_name, _LIB))
393 styp_flags = STYP_LIB;
394 #endif /* _LIB */
395 #ifdef _LIT
397 else if (!strcmp (sec_name, _LIT))
399 styp_flags = STYP_LIT;
400 #endif /* _LIT */
402 else if (!strncmp (sec_name, ".debug", 6))
404 /* Handle the XCOFF debug section and DWARF2 debug sections. */
405 if (!sec_name[6])
406 styp_flags = STYP_XCOFF_DEBUG;
407 else
408 styp_flags = STYP_DEBUG_INFO;
410 else if (!strncmp (sec_name, ".stab", 5))
412 styp_flags = STYP_DEBUG_INFO;
414 #ifdef COFF_LONG_SECTION_NAMES
415 else if (!strncmp (sec_name, ".gnu.linkonce.wi.", 17))
417 styp_flags = STYP_DEBUG_INFO;
419 #endif
420 #ifdef RS6000COFF_C
421 else if (!strcmp (sec_name, _PAD))
423 styp_flags = STYP_PAD;
425 else if (!strcmp (sec_name, _LOADER))
427 styp_flags = STYP_LOADER;
429 #endif
430 /* Try and figure out what it should be */
431 else if (sec_flags & SEC_CODE)
433 styp_flags = STYP_TEXT;
435 else if (sec_flags & SEC_DATA)
437 styp_flags = STYP_DATA;
439 else if (sec_flags & SEC_READONLY)
441 #ifdef STYP_LIT /* 29k readonly text/data section */
442 styp_flags = STYP_LIT;
443 #else
444 styp_flags = STYP_TEXT;
445 #endif /* STYP_LIT */
447 else if (sec_flags & SEC_LOAD)
449 styp_flags = STYP_TEXT;
451 else if (sec_flags & SEC_ALLOC)
453 styp_flags = STYP_BSS;
456 #ifdef STYP_CLINK
457 if (sec_flags & SEC_CLINK)
458 styp_flags |= STYP_CLINK;
459 #endif
461 #ifdef STYP_BLOCK
462 if (sec_flags & SEC_BLOCK)
463 styp_flags |= STYP_BLOCK;
464 #endif
466 #ifdef STYP_NOLOAD
467 if ((sec_flags & (SEC_NEVER_LOAD | SEC_COFF_SHARED_LIBRARY)) != 0)
468 styp_flags |= STYP_NOLOAD;
469 #endif
471 return styp_flags;
474 #else /* COFF_WITH_PE */
476 /* The PE version; see above for the general comments. The non-PE
477 case seems to be more guessing, and breaks PE format; specifically,
478 .rdata is readonly, but it sure ain't text. Really, all this
479 should be set up properly in gas (or whatever assembler is in use),
480 and honor whatever objcopy/strip, etc. sent us as input. */
482 static long
483 sec_to_styp_flags (sec_name, sec_flags)
484 const char *sec_name ATTRIBUTE_UNUSED;
485 flagword sec_flags;
487 long styp_flags = 0;
489 /* caution: there are at least three groups of symbols that have
490 very similar bits and meanings: IMAGE_SCN*, SEC_*, and STYP_*.
491 SEC_* are the BFD internal flags, used for generic BFD
492 information. STYP_* are the COFF section flags which appear in
493 COFF files. IMAGE_SCN_* are the PE section flags which appear in
494 PE files. The STYP_* flags and the IMAGE_SCN_* flags overlap,
495 but there are more IMAGE_SCN_* flags. */
497 /* skip LOAD */
498 /* READONLY later */
499 /* skip RELOC */
500 if ((sec_flags & SEC_CODE) != 0)
501 styp_flags |= IMAGE_SCN_CNT_CODE;
502 if ((sec_flags & SEC_DATA) != 0)
503 styp_flags |= IMAGE_SCN_CNT_INITIALIZED_DATA;
504 if ((sec_flags & SEC_ALLOC) != 0 && (sec_flags & SEC_LOAD) == 0)
505 styp_flags |= IMAGE_SCN_CNT_UNINITIALIZED_DATA; /* ==STYP_BSS */
506 /* skip ROM */
507 /* skip CONSTRUCTOR */
508 /* skip CONTENTS */
509 #ifdef STYP_NOLOAD
510 if ((sec_flags & (SEC_NEVER_LOAD | SEC_COFF_SHARED_LIBRARY)) != 0)
511 styp_flags |= STYP_NOLOAD;
512 #endif
513 if ((sec_flags & SEC_IS_COMMON) != 0)
514 styp_flags |= IMAGE_SCN_LNK_COMDAT;
515 if ((sec_flags & SEC_DEBUGGING) != 0)
516 styp_flags |= IMAGE_SCN_MEM_DISCARDABLE;
517 if ((sec_flags & SEC_EXCLUDE) != 0)
518 styp_flags |= IMAGE_SCN_LNK_REMOVE;
519 if ((sec_flags & SEC_NEVER_LOAD) != 0)
520 styp_flags |= IMAGE_SCN_LNK_REMOVE;
521 /* skip IN_MEMORY */
522 /* skip SORT */
523 if (sec_flags & SEC_LINK_ONCE)
524 styp_flags |= IMAGE_SCN_LNK_COMDAT;
525 /* skip LINK_DUPLICATES */
526 /* skip LINKER_CREATED */
528 /* For now, the read/write bits are mapped onto SEC_READONLY, even
529 though the semantics don't quite match. The bits from the input
530 are retained in pei_section_data(abfd, section)->pe_flags */
532 styp_flags |= IMAGE_SCN_MEM_READ; /* always readable. */
533 if ((sec_flags & SEC_READONLY) == 0)
534 styp_flags |= IMAGE_SCN_MEM_WRITE; /* Invert READONLY for write */
535 if (sec_flags & SEC_CODE)
536 styp_flags |= IMAGE_SCN_MEM_EXECUTE; /* CODE->EXECUTE */
537 if (sec_flags & SEC_SHARED)
538 styp_flags |= IMAGE_SCN_MEM_SHARED; /* Shared remains meaningful */
540 return styp_flags;
543 #endif /* COFF_WITH_PE */
545 /* Return a word with SEC_* flags set to represent the incoming STYP_*
546 flags (from scnhdr.s_flags). The inverse of this function is
547 sec_to_styp_flags(). NOTE: If you add to/change this routine, you
548 should probably mirror the changes in sec_to_styp_flags(). */
550 #ifndef COFF_WITH_PE
552 static flagword
553 styp_to_sec_flags (abfd, hdr, name, section)
554 bfd *abfd ATTRIBUTE_UNUSED;
555 PTR hdr;
556 const char *name;
557 asection *section ATTRIBUTE_UNUSED;
559 struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
560 long styp_flags = internal_s->s_flags;
561 flagword sec_flags = 0;
563 #ifdef STYP_BLOCK
564 if (styp_flags & STYP_BLOCK)
565 sec_flags |= SEC_BLOCK;
566 #endif
568 #ifdef STYP_CLINK
569 if (styp_flags & STYP_CLINK)
570 sec_flags |= SEC_CLINK;
571 #endif
573 #ifdef STYP_NOLOAD
574 if (styp_flags & STYP_NOLOAD)
576 sec_flags |= SEC_NEVER_LOAD;
578 #endif /* STYP_NOLOAD */
580 /* For 386 COFF, at least, an unloadable text or data section is
581 actually a shared library section. */
582 if (styp_flags & STYP_TEXT)
584 if (sec_flags & SEC_NEVER_LOAD)
585 sec_flags |= SEC_CODE | SEC_COFF_SHARED_LIBRARY;
586 else
587 sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
589 else if (styp_flags & STYP_DATA)
591 if (sec_flags & SEC_NEVER_LOAD)
592 sec_flags |= SEC_DATA | SEC_COFF_SHARED_LIBRARY;
593 else
594 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
596 else if (styp_flags & STYP_BSS)
598 #ifdef BSS_NOLOAD_IS_SHARED_LIBRARY
599 if (sec_flags & SEC_NEVER_LOAD)
600 sec_flags |= SEC_ALLOC | SEC_COFF_SHARED_LIBRARY;
601 else
602 #endif
603 sec_flags |= SEC_ALLOC;
605 else if (styp_flags & STYP_INFO)
607 /* We mark these as SEC_DEBUGGING, but only if COFF_PAGE_SIZE is
608 defined. coff_compute_section_file_positions uses
609 COFF_PAGE_SIZE to ensure that the low order bits of the
610 section VMA and the file offset match. If we don't know
611 COFF_PAGE_SIZE, we can't ensure the correct correspondence,
612 and demand page loading of the file will fail. */
613 #if defined (COFF_PAGE_SIZE) && !defined (COFF_ALIGN_IN_S_FLAGS)
614 sec_flags |= SEC_DEBUGGING;
615 #endif
617 else if (styp_flags & STYP_PAD)
619 sec_flags = 0;
621 else if (strcmp (name, _TEXT) == 0)
623 if (sec_flags & SEC_NEVER_LOAD)
624 sec_flags |= SEC_CODE | SEC_COFF_SHARED_LIBRARY;
625 else
626 sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
628 else if (strcmp (name, _DATA) == 0)
630 if (sec_flags & SEC_NEVER_LOAD)
631 sec_flags |= SEC_DATA | SEC_COFF_SHARED_LIBRARY;
632 else
633 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
635 else if (strcmp (name, _BSS) == 0)
637 #ifdef BSS_NOLOAD_IS_SHARED_LIBRARY
638 if (sec_flags & SEC_NEVER_LOAD)
639 sec_flags |= SEC_ALLOC | SEC_COFF_SHARED_LIBRARY;
640 else
641 #endif
642 sec_flags |= SEC_ALLOC;
644 else if (strncmp (name, ".debug", 6) == 0
645 #ifdef _COMMENT
646 || strcmp (name, _COMMENT) == 0
647 #endif
648 #ifdef COFF_LONG_SECTION_NAMES
649 || strncmp (name, ".gnu.linkonce.wi.", 17) == 0
650 #endif
651 || strncmp (name, ".stab", 5) == 0)
653 #ifdef COFF_PAGE_SIZE
654 sec_flags |= SEC_DEBUGGING;
655 #endif
657 #ifdef _LIB
658 else if (strcmp (name, _LIB) == 0)
660 #endif
661 #ifdef _LIT
662 else if (strcmp (name, _LIT) == 0)
664 sec_flags = SEC_LOAD | SEC_ALLOC | SEC_READONLY;
666 #endif
667 else
669 sec_flags |= SEC_ALLOC | SEC_LOAD;
672 #ifdef STYP_LIT /* A29k readonly text/data section type */
673 if ((styp_flags & STYP_LIT) == STYP_LIT)
675 sec_flags = (SEC_LOAD | SEC_ALLOC | SEC_READONLY);
677 #endif /* STYP_LIT */
678 #ifdef STYP_OTHER_LOAD /* Other loaded sections */
679 if (styp_flags & STYP_OTHER_LOAD)
681 sec_flags = (SEC_LOAD | SEC_ALLOC);
683 #endif /* STYP_SDATA */
685 #if defined (COFF_LONG_SECTION_NAMES) && defined (COFF_SUPPORT_GNU_LINKONCE)
686 /* As a GNU extension, if the name begins with .gnu.linkonce, we
687 only link a single copy of the section. This is used to support
688 g++. g++ will emit each template expansion in its own section.
689 The symbols will be defined as weak, so that multiple definitions
690 are permitted. The GNU linker extension is to actually discard
691 all but one of the sections. */
692 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
693 sec_flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
694 #endif
696 return sec_flags;
699 #else /* COFF_WITH_PE */
701 /* The PE version; see above for the general comments.
703 Since to set the SEC_LINK_ONCE and associated flags, we have to
704 look at the symbol table anyway, we return the symbol table index
705 of the symbol being used as the COMDAT symbol. This is admittedly
706 ugly, but there's really nowhere else that we have access to the
707 required information. FIXME: Is the COMDAT symbol index used for
708 any purpose other than objdump? */
710 static flagword
711 styp_to_sec_flags (abfd, hdr, name, section)
712 bfd *abfd ATTRIBUTE_UNUSED;
713 PTR hdr;
714 const char *name;
715 asection *section;
717 struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
718 long styp_flags = internal_s->s_flags;
719 flagword sec_flags = 0;
721 if (styp_flags & STYP_DSECT)
722 abort (); /* Don't know what to do */
723 #ifdef SEC_NEVER_LOAD
724 if (styp_flags & STYP_NOLOAD)
725 sec_flags |= SEC_NEVER_LOAD;
726 #endif
727 if (styp_flags & STYP_GROUP)
728 abort (); /* Don't know what to do */
729 /* skip IMAGE_SCN_TYPE_NO_PAD */
730 if (styp_flags & STYP_COPY)
731 abort (); /* Don't know what to do */
732 if (styp_flags & IMAGE_SCN_CNT_CODE)
733 sec_flags |= SEC_CODE | SEC_ALLOC | SEC_LOAD;
734 if (styp_flags & IMAGE_SCN_CNT_INITIALIZED_DATA)
735 sec_flags |= SEC_DATA | SEC_ALLOC | SEC_LOAD;
736 if (styp_flags & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
737 sec_flags |= SEC_ALLOC;
738 if (styp_flags & IMAGE_SCN_LNK_OTHER)
739 abort (); /* Don't know what to do */
740 if (styp_flags & IMAGE_SCN_LNK_INFO)
742 /* We mark these as SEC_DEBUGGING, but only if COFF_PAGE_SIZE is
743 defined. coff_compute_section_file_positions uses
744 COFF_PAGE_SIZE to ensure that the low order bits of the
745 section VMA and the file offset match. If we don't know
746 COFF_PAGE_SIZE, we can't ensure the correct correspondence,
747 and demand page loading of the file will fail. */
748 #ifdef COFF_PAGE_SIZE
749 sec_flags |= SEC_DEBUGGING;
750 #endif
752 if (styp_flags & STYP_OVER)
753 abort (); /* Don't know what to do */
754 if (styp_flags & IMAGE_SCN_LNK_REMOVE)
755 sec_flags |= SEC_EXCLUDE;
757 if (styp_flags & IMAGE_SCN_MEM_SHARED)
758 sec_flags |= SEC_SHARED;
759 /* COMDAT: see below */
760 if (styp_flags & IMAGE_SCN_MEM_DISCARDABLE)
761 sec_flags |= SEC_DEBUGGING;
762 if (styp_flags & IMAGE_SCN_MEM_NOT_CACHED)
763 abort ();/* Don't know what to do */
764 if (styp_flags & IMAGE_SCN_MEM_NOT_PAGED)
765 abort (); /* Don't know what to do */
767 /* We infer from the distinct read/write/execute bits the settings
768 of some of the bfd flags; the actual values, should we need them,
769 are also in pei_section_data (abfd, section)->pe_flags. */
771 if (styp_flags & IMAGE_SCN_MEM_EXECUTE)
772 sec_flags |= SEC_CODE; /* Probably redundant */
773 /* IMAGE_SCN_MEM_READ is simply ignored, assuming it always to be true. */
774 if ((styp_flags & IMAGE_SCN_MEM_WRITE) == 0)
775 sec_flags |= SEC_READONLY;
777 /* COMDAT gets very special treatment. */
778 if (styp_flags & IMAGE_SCN_LNK_COMDAT)
780 sec_flags |= SEC_LINK_ONCE;
782 /* Unfortunately, the PE format stores essential information in
783 the symbol table, of all places. We need to extract that
784 information now, so that objdump and the linker will know how
785 to handle the section without worrying about the symbols. We
786 can't call slurp_symtab, because the linker doesn't want the
787 swapped symbols. */
789 /* COMDAT sections are special. The first symbol is the section
790 symbol, which tells what kind of COMDAT section it is. The
791 second symbol is the "comdat symbol" - the one with the
792 unique name. GNU uses the section symbol for the unique
793 name; MS uses ".text" for every comdat section. Sigh. - DJ */
795 /* This is not mirrored in sec_to_styp_flags(), but there
796 doesn't seem to be a need to, either, and it would at best be
797 rather messy. */
799 if (_bfd_coff_get_external_symbols (abfd))
801 bfd_byte *esymstart, *esym, *esymend;
802 int seen_state = 0;
803 char *target_name = NULL;
805 esymstart = esym = (bfd_byte *) obj_coff_external_syms (abfd);
806 esymend = esym + obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd);
808 while (esym < esymend)
810 struct internal_syment isym;
811 char buf[SYMNMLEN + 1];
812 const char *symname;
814 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &isym);
816 if (sizeof (internal_s->s_name) > SYMNMLEN)
818 /* This case implies that the matching symbol name
819 will be in the string table. */
820 abort ();
823 if (isym.n_scnum == section->target_index)
825 /* According to the MSVC documentation, the first
826 TWO entries with the section # are both of
827 interest to us. The first one is the "section
828 symbol" (section name). The second is the comdat
829 symbol name. Here, we've found the first
830 qualifying entry; we distinguish it from the
831 second with a state flag.
833 In the case of gas-generated (at least until that
834 is fixed) .o files, it isn't necessarily the
835 second one. It may be some other later symbol.
837 Since gas also doesn't follow MS conventions and
838 emits the section similar to .text$<name>, where
839 <something> is the name we're looking for, we
840 distinguish the two as follows:
842 If the section name is simply a section name (no
843 $) we presume it's MS-generated, and look at
844 precisely the second symbol for the comdat name.
845 If the section name has a $, we assume it's
846 gas-generated, and look for <something> (whatever
847 follows the $) as the comdat symbol. */
849 /* All 3 branches use this */
850 symname = _bfd_coff_internal_syment_name (abfd, &isym, buf);
852 if (symname == NULL)
853 abort ();
855 switch (seen_state)
857 case 0:
859 /* The first time we've seen the symbol. */
860 union internal_auxent aux;
862 seen_state = 1;
864 /* If it isn't the stuff we're expecting, die;
865 The MS documentation is vague, but it
866 appears that the second entry serves BOTH
867 as the comdat symbol and the defining
868 symbol record (either C_STAT or C_EXT,
869 possibly with an aux entry with debug
870 information if it's a function.) It
871 appears the only way to find the second one
872 is to count. (On Intel, they appear to be
873 adjacent, but on Alpha, they have been
874 found separated.)
876 Here, we think we've found the first one,
877 but there's some checking we can do to be
878 sure. */
880 if (! (isym.n_sclass == C_STAT
881 && isym.n_type == T_NULL
882 && isym.n_value == 0))
883 abort ();
885 /* FIXME LATER: MSVC generates section names
886 like .text for comdats. Gas generates
887 names like .text$foo__Fv (in the case of a
888 function). See comment above for more. */
890 if (strcmp (name, symname) != 0)
891 abort ();
893 /* This is the section symbol. */
895 bfd_coff_swap_aux_in (abfd, (PTR) (esym + bfd_coff_symesz (abfd)),
896 isym.n_type, isym.n_sclass,
897 0, isym.n_numaux, (PTR) &aux);
899 target_name = strchr (name, '$');
900 if (target_name != NULL)
902 /* Gas mode. */
903 seen_state = 2;
904 /* Skip the `$'. */
905 target_name += 1;
908 /* FIXME: Microsoft uses NODUPLICATES and
909 ASSOCIATIVE, but gnu uses ANY and
910 SAME_SIZE. Unfortunately, gnu doesn't do
911 the comdat symbols right. So, until we can
912 fix it to do the right thing, we are
913 temporarily disabling comdats for the MS
914 types (they're used in DLLs and C++, but we
915 don't support *their* C++ libraries anyway
916 - DJ. */
918 /* Cygwin does not follow the MS style, and
919 uses ANY and SAME_SIZE where NODUPLICATES
920 and ASSOCIATIVE should be used. For
921 Interix, we just do the right thing up
922 front. */
924 switch (aux.x_scn.x_comdat)
926 case IMAGE_COMDAT_SELECT_NODUPLICATES:
927 #ifdef STRICT_PE_FORMAT
928 sec_flags |= SEC_LINK_DUPLICATES_ONE_ONLY;
929 #else
930 sec_flags &= ~SEC_LINK_ONCE;
931 #endif
932 break;
934 case IMAGE_COMDAT_SELECT_ANY:
935 sec_flags |= SEC_LINK_DUPLICATES_DISCARD;
936 break;
938 case IMAGE_COMDAT_SELECT_SAME_SIZE:
939 sec_flags |= SEC_LINK_DUPLICATES_SAME_SIZE;
940 break;
942 case IMAGE_COMDAT_SELECT_EXACT_MATCH:
943 /* Not yet fully implemented ??? */
944 sec_flags |= SEC_LINK_DUPLICATES_SAME_CONTENTS;
945 break;
947 /* debug$S gets this case; other
948 implications ??? */
950 /* There may be no symbol... we'll search
951 the whole table... Is this the right
952 place to play this game? Or should we do
953 it when reading it in. */
954 case IMAGE_COMDAT_SELECT_ASSOCIATIVE:
955 #ifdef STRICT_PE_FORMAT
956 /* FIXME: This is not currently implemented. */
957 sec_flags |= SEC_LINK_DUPLICATES_DISCARD;
958 #else
959 sec_flags &= ~SEC_LINK_ONCE;
960 #endif
961 break;
963 default: /* 0 means "no symbol" */
964 /* debug$F gets this case; other
965 implications ??? */
966 sec_flags |= SEC_LINK_DUPLICATES_DISCARD;
967 break;
970 break;
972 case 2:
973 /* Gas mode: the first matching on partial name. */
975 #ifndef TARGET_UNDERSCORE
976 #define TARGET_UNDERSCORE 0
977 #endif
978 /* Is this the name we're looking for? */
979 if (strcmp (target_name,
980 symname + (TARGET_UNDERSCORE ? 1 : 0)) != 0)
982 /* Not the name we're looking for */
983 esym += (isym.n_numaux + 1) * bfd_coff_symesz (abfd);
984 continue;
986 /* Fall through. */
987 case 1:
988 /* MSVC mode: the lexically second symbol (or
989 drop through from the above). */
991 char *newname;
993 /* This must the the second symbol with the
994 section #. It is the actual symbol name.
995 Intel puts the two adjacent, but Alpha (at
996 least) spreads them out. */
998 section->comdat =
999 bfd_alloc (abfd, sizeof (struct bfd_comdat_info));
1000 if (section->comdat == NULL)
1001 abort ();
1002 section->comdat->symbol =
1003 (esym - esymstart) / bfd_coff_symesz (abfd);
1005 newname = bfd_alloc (abfd, strlen (symname) + 1);
1006 if (newname == NULL)
1007 abort ();
1009 strcpy (newname, symname);
1010 section->comdat->name = newname;
1014 goto breakloop;
1018 esym += (isym.n_numaux + 1) * bfd_coff_symesz (abfd);
1020 breakloop:
1024 #if defined (COFF_LONG_SECTION_NAMES) && defined (COFF_SUPPORT_GNU_LINKONCE)
1025 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1026 only link a single copy of the section. This is used to support
1027 g++. g++ will emit each template expansion in its own section.
1028 The symbols will be defined as weak, so that multiple definitions
1029 are permitted. The GNU linker extension is to actually discard
1030 all but one of the sections. */
1031 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
1032 sec_flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1033 #endif
1035 return sec_flags;
1038 #endif /* COFF_WITH_PE */
1040 #define get_index(symbol) ((symbol)->udata.i)
1043 INTERNAL_DEFINITION
1044 bfd_coff_backend_data
1046 CODE_FRAGMENT
1048 .{* COFF symbol classifications. *}
1050 .enum coff_symbol_classification
1052 . {* Global symbol. *}
1053 . COFF_SYMBOL_GLOBAL,
1054 . {* Common symbol. *}
1055 . COFF_SYMBOL_COMMON,
1056 . {* Undefined symbol. *}
1057 . COFF_SYMBOL_UNDEFINED,
1058 . {* Local symbol. *}
1059 . COFF_SYMBOL_LOCAL,
1060 . {* PE section symbol. *}
1061 . COFF_SYMBOL_PE_SECTION
1064 Special entry points for gdb to swap in coff symbol table parts:
1065 .typedef struct
1067 . void (*_bfd_coff_swap_aux_in) PARAMS ((
1068 . bfd *abfd,
1069 . PTR ext,
1070 . int type,
1071 . int class,
1072 . int indaux,
1073 . int numaux,
1074 . PTR in));
1076 . void (*_bfd_coff_swap_sym_in) PARAMS ((
1077 . bfd *abfd ,
1078 . PTR ext,
1079 . PTR in));
1081 . void (*_bfd_coff_swap_lineno_in) PARAMS ((
1082 . bfd *abfd,
1083 . PTR ext,
1084 . PTR in));
1087 Special entry points for gas to swap out coff parts:
1089 . unsigned int (*_bfd_coff_swap_aux_out) PARAMS ((
1090 . bfd *abfd,
1091 . PTR in,
1092 . int type,
1093 . int class,
1094 . int indaux,
1095 . int numaux,
1096 . PTR ext));
1098 . unsigned int (*_bfd_coff_swap_sym_out) PARAMS ((
1099 . bfd *abfd,
1100 . PTR in,
1101 . PTR ext));
1103 . unsigned int (*_bfd_coff_swap_lineno_out) PARAMS ((
1104 . bfd *abfd,
1105 . PTR in,
1106 . PTR ext));
1108 . unsigned int (*_bfd_coff_swap_reloc_out) PARAMS ((
1109 . bfd *abfd,
1110 . PTR src,
1111 . PTR dst));
1113 . unsigned int (*_bfd_coff_swap_filehdr_out) PARAMS ((
1114 . bfd *abfd,
1115 . PTR in,
1116 . PTR out));
1118 . unsigned int (*_bfd_coff_swap_aouthdr_out) PARAMS ((
1119 . bfd *abfd,
1120 . PTR in,
1121 . PTR out));
1123 . unsigned int (*_bfd_coff_swap_scnhdr_out) PARAMS ((
1124 . bfd *abfd,
1125 . PTR in,
1126 . PTR out));
1129 Special entry points for generic COFF routines to call target
1130 dependent COFF routines:
1132 . unsigned int _bfd_filhsz;
1133 . unsigned int _bfd_aoutsz;
1134 . unsigned int _bfd_scnhsz;
1135 . unsigned int _bfd_symesz;
1136 . unsigned int _bfd_auxesz;
1137 . unsigned int _bfd_relsz;
1138 . unsigned int _bfd_linesz;
1139 . unsigned int _bfd_filnmlen;
1140 . boolean _bfd_coff_long_filenames;
1141 . boolean _bfd_coff_long_section_names;
1142 . unsigned int _bfd_coff_default_section_alignment_power;
1143 . boolean _bfd_coff_force_symnames_in_strings;
1144 . unsigned int _bfd_coff_debug_string_prefix_length;
1145 . void (*_bfd_coff_swap_filehdr_in) PARAMS ((
1146 . bfd *abfd,
1147 . PTR ext,
1148 . PTR in));
1149 . void (*_bfd_coff_swap_aouthdr_in) PARAMS ((
1150 . bfd *abfd,
1151 . PTR ext,
1152 . PTR in));
1153 . void (*_bfd_coff_swap_scnhdr_in) PARAMS ((
1154 . bfd *abfd,
1155 . PTR ext,
1156 . PTR in));
1157 . void (*_bfd_coff_swap_reloc_in) PARAMS ((
1158 . bfd *abfd,
1159 . PTR ext,
1160 . PTR in));
1161 . boolean (*_bfd_coff_bad_format_hook) PARAMS ((
1162 . bfd *abfd,
1163 . PTR internal_filehdr));
1164 . boolean (*_bfd_coff_set_arch_mach_hook) PARAMS ((
1165 . bfd *abfd,
1166 . PTR internal_filehdr));
1167 . PTR (*_bfd_coff_mkobject_hook) PARAMS ((
1168 . bfd *abfd,
1169 . PTR internal_filehdr,
1170 . PTR internal_aouthdr));
1171 . flagword (*_bfd_styp_to_sec_flags_hook) PARAMS ((
1172 . bfd *abfd,
1173 . PTR internal_scnhdr,
1174 . const char *name,
1175 . asection *section));
1176 . void (*_bfd_set_alignment_hook) PARAMS ((
1177 . bfd *abfd,
1178 . asection *sec,
1179 . PTR internal_scnhdr));
1180 . boolean (*_bfd_coff_slurp_symbol_table) PARAMS ((
1181 . bfd *abfd));
1182 . boolean (*_bfd_coff_symname_in_debug) PARAMS ((
1183 . bfd *abfd,
1184 . struct internal_syment *sym));
1185 . boolean (*_bfd_coff_pointerize_aux_hook) PARAMS ((
1186 . bfd *abfd,
1187 . combined_entry_type *table_base,
1188 . combined_entry_type *symbol,
1189 . unsigned int indaux,
1190 . combined_entry_type *aux));
1191 . boolean (*_bfd_coff_print_aux) PARAMS ((
1192 . bfd *abfd,
1193 . FILE *file,
1194 . combined_entry_type *table_base,
1195 . combined_entry_type *symbol,
1196 . combined_entry_type *aux,
1197 . unsigned int indaux));
1198 . void (*_bfd_coff_reloc16_extra_cases) PARAMS ((
1199 . bfd *abfd,
1200 . struct bfd_link_info *link_info,
1201 . struct bfd_link_order *link_order,
1202 . arelent *reloc,
1203 . bfd_byte *data,
1204 . unsigned int *src_ptr,
1205 . unsigned int *dst_ptr));
1206 . int (*_bfd_coff_reloc16_estimate) PARAMS ((
1207 . bfd *abfd,
1208 . asection *input_section,
1209 . arelent *r,
1210 . unsigned int shrink,
1211 . struct bfd_link_info *link_info));
1212 . enum coff_symbol_classification (*_bfd_coff_classify_symbol) PARAMS ((
1213 . bfd *abfd,
1214 . struct internal_syment *));
1215 . boolean (*_bfd_coff_compute_section_file_positions) PARAMS ((
1216 . bfd *abfd));
1217 . boolean (*_bfd_coff_start_final_link) PARAMS ((
1218 . bfd *output_bfd,
1219 . struct bfd_link_info *info));
1220 . boolean (*_bfd_coff_relocate_section) PARAMS ((
1221 . bfd *output_bfd,
1222 . struct bfd_link_info *info,
1223 . bfd *input_bfd,
1224 . asection *input_section,
1225 . bfd_byte *contents,
1226 . struct internal_reloc *relocs,
1227 . struct internal_syment *syms,
1228 . asection **sections));
1229 . reloc_howto_type *(*_bfd_coff_rtype_to_howto) PARAMS ((
1230 . bfd *abfd,
1231 . asection *sec,
1232 . struct internal_reloc *rel,
1233 . struct coff_link_hash_entry *h,
1234 . struct internal_syment *sym,
1235 . bfd_vma *addendp));
1236 . boolean (*_bfd_coff_adjust_symndx) PARAMS ((
1237 . bfd *obfd,
1238 . struct bfd_link_info *info,
1239 . bfd *ibfd,
1240 . asection *sec,
1241 . struct internal_reloc *reloc,
1242 . boolean *adjustedp));
1243 . boolean (*_bfd_coff_link_add_one_symbol) PARAMS ((
1244 . struct bfd_link_info *info,
1245 . bfd *abfd,
1246 . const char *name,
1247 . flagword flags,
1248 . asection *section,
1249 . bfd_vma value,
1250 . const char *string,
1251 . boolean copy,
1252 . boolean collect,
1253 . struct bfd_link_hash_entry **hashp));
1255 . boolean (*_bfd_coff_link_output_has_begun) PARAMS ((
1256 . bfd * abfd,
1257 . struct coff_final_link_info * pfinfo));
1258 . boolean (*_bfd_coff_final_link_postscript) PARAMS ((
1259 . bfd * abfd,
1260 . struct coff_final_link_info * pfinfo));
1262 .} bfd_coff_backend_data;
1264 .#define coff_backend_info(abfd) ((bfd_coff_backend_data *) (abfd)->xvec->backend_data)
1266 .#define bfd_coff_swap_aux_in(a,e,t,c,ind,num,i) \
1267 . ((coff_backend_info (a)->_bfd_coff_swap_aux_in) (a,e,t,c,ind,num,i))
1269 .#define bfd_coff_swap_sym_in(a,e,i) \
1270 . ((coff_backend_info (a)->_bfd_coff_swap_sym_in) (a,e,i))
1272 .#define bfd_coff_swap_lineno_in(a,e,i) \
1273 . ((coff_backend_info ( a)->_bfd_coff_swap_lineno_in) (a,e,i))
1275 .#define bfd_coff_swap_reloc_out(abfd, i, o) \
1276 . ((coff_backend_info (abfd)->_bfd_coff_swap_reloc_out) (abfd, i, o))
1278 .#define bfd_coff_swap_lineno_out(abfd, i, o) \
1279 . ((coff_backend_info (abfd)->_bfd_coff_swap_lineno_out) (abfd, i, o))
1281 .#define bfd_coff_swap_aux_out(a,i,t,c,ind,num,o) \
1282 . ((coff_backend_info (a)->_bfd_coff_swap_aux_out) (a,i,t,c,ind,num,o))
1284 .#define bfd_coff_swap_sym_out(abfd, i,o) \
1285 . ((coff_backend_info (abfd)->_bfd_coff_swap_sym_out) (abfd, i, o))
1287 .#define bfd_coff_swap_scnhdr_out(abfd, i,o) \
1288 . ((coff_backend_info (abfd)->_bfd_coff_swap_scnhdr_out) (abfd, i, o))
1290 .#define bfd_coff_swap_filehdr_out(abfd, i,o) \
1291 . ((coff_backend_info (abfd)->_bfd_coff_swap_filehdr_out) (abfd, i, o))
1293 .#define bfd_coff_swap_aouthdr_out(abfd, i,o) \
1294 . ((coff_backend_info (abfd)->_bfd_coff_swap_aouthdr_out) (abfd, i, o))
1296 .#define bfd_coff_filhsz(abfd) (coff_backend_info (abfd)->_bfd_filhsz)
1297 .#define bfd_coff_aoutsz(abfd) (coff_backend_info (abfd)->_bfd_aoutsz)
1298 .#define bfd_coff_scnhsz(abfd) (coff_backend_info (abfd)->_bfd_scnhsz)
1299 .#define bfd_coff_symesz(abfd) (coff_backend_info (abfd)->_bfd_symesz)
1300 .#define bfd_coff_auxesz(abfd) (coff_backend_info (abfd)->_bfd_auxesz)
1301 .#define bfd_coff_relsz(abfd) (coff_backend_info (abfd)->_bfd_relsz)
1302 .#define bfd_coff_linesz(abfd) (coff_backend_info (abfd)->_bfd_linesz)
1303 .#define bfd_coff_filnmlen(abfd) (coff_backend_info (abfd)->_bfd_filnmlen)
1304 .#define bfd_coff_long_filenames(abfd) (coff_backend_info (abfd)->_bfd_coff_long_filenames)
1305 .#define bfd_coff_long_section_names(abfd) \
1306 . (coff_backend_info (abfd)->_bfd_coff_long_section_names)
1307 .#define bfd_coff_default_section_alignment_power(abfd) \
1308 . (coff_backend_info (abfd)->_bfd_coff_default_section_alignment_power)
1309 .#define bfd_coff_swap_filehdr_in(abfd, i,o) \
1310 . ((coff_backend_info (abfd)->_bfd_coff_swap_filehdr_in) (abfd, i, o))
1312 .#define bfd_coff_swap_aouthdr_in(abfd, i,o) \
1313 . ((coff_backend_info (abfd)->_bfd_coff_swap_aouthdr_in) (abfd, i, o))
1315 .#define bfd_coff_swap_scnhdr_in(abfd, i,o) \
1316 . ((coff_backend_info (abfd)->_bfd_coff_swap_scnhdr_in) (abfd, i, o))
1318 .#define bfd_coff_swap_reloc_in(abfd, i, o) \
1319 . ((coff_backend_info (abfd)->_bfd_coff_swap_reloc_in) (abfd, i, o))
1321 .#define bfd_coff_bad_format_hook(abfd, filehdr) \
1322 . ((coff_backend_info (abfd)->_bfd_coff_bad_format_hook) (abfd, filehdr))
1324 .#define bfd_coff_set_arch_mach_hook(abfd, filehdr)\
1325 . ((coff_backend_info (abfd)->_bfd_coff_set_arch_mach_hook) (abfd, filehdr))
1326 .#define bfd_coff_mkobject_hook(abfd, filehdr, aouthdr)\
1327 . ((coff_backend_info (abfd)->_bfd_coff_mkobject_hook) (abfd, filehdr, aouthdr))
1329 .#define bfd_coff_styp_to_sec_flags_hook(abfd, scnhdr, name, section)\
1330 . ((coff_backend_info (abfd)->_bfd_styp_to_sec_flags_hook)\
1331 . (abfd, scnhdr, name, section))
1333 .#define bfd_coff_set_alignment_hook(abfd, sec, scnhdr)\
1334 . ((coff_backend_info (abfd)->_bfd_set_alignment_hook) (abfd, sec, scnhdr))
1336 .#define bfd_coff_slurp_symbol_table(abfd)\
1337 . ((coff_backend_info (abfd)->_bfd_coff_slurp_symbol_table) (abfd))
1339 .#define bfd_coff_symname_in_debug(abfd, sym)\
1340 . ((coff_backend_info (abfd)->_bfd_coff_symname_in_debug) (abfd, sym))
1342 .#define bfd_coff_force_symnames_in_strings(abfd)\
1343 . (coff_backend_info (abfd)->_bfd_coff_force_symnames_in_strings)
1345 .#define bfd_coff_debug_string_prefix_length(abfd)\
1346 . (coff_backend_info (abfd)->_bfd_coff_debug_string_prefix_length)
1348 .#define bfd_coff_print_aux(abfd, file, base, symbol, aux, indaux)\
1349 . ((coff_backend_info (abfd)->_bfd_coff_print_aux)\
1350 . (abfd, file, base, symbol, aux, indaux))
1352 .#define bfd_coff_reloc16_extra_cases(abfd, link_info, link_order, reloc, data, src_ptr, dst_ptr)\
1353 . ((coff_backend_info (abfd)->_bfd_coff_reloc16_extra_cases)\
1354 . (abfd, link_info, link_order, reloc, data, src_ptr, dst_ptr))
1356 .#define bfd_coff_reloc16_estimate(abfd, section, reloc, shrink, link_info)\
1357 . ((coff_backend_info (abfd)->_bfd_coff_reloc16_estimate)\
1358 . (abfd, section, reloc, shrink, link_info))
1360 .#define bfd_coff_classify_symbol(abfd, sym)\
1361 . ((coff_backend_info (abfd)->_bfd_coff_classify_symbol)\
1362 . (abfd, sym))
1364 .#define bfd_coff_compute_section_file_positions(abfd)\
1365 . ((coff_backend_info (abfd)->_bfd_coff_compute_section_file_positions)\
1366 . (abfd))
1368 .#define bfd_coff_start_final_link(obfd, info)\
1369 . ((coff_backend_info (obfd)->_bfd_coff_start_final_link)\
1370 . (obfd, info))
1371 .#define bfd_coff_relocate_section(obfd,info,ibfd,o,con,rel,isyms,secs)\
1372 . ((coff_backend_info (ibfd)->_bfd_coff_relocate_section)\
1373 . (obfd, info, ibfd, o, con, rel, isyms, secs))
1374 .#define bfd_coff_rtype_to_howto(abfd, sec, rel, h, sym, addendp)\
1375 . ((coff_backend_info (abfd)->_bfd_coff_rtype_to_howto)\
1376 . (abfd, sec, rel, h, sym, addendp))
1377 .#define bfd_coff_adjust_symndx(obfd, info, ibfd, sec, rel, adjustedp)\
1378 . ((coff_backend_info (abfd)->_bfd_coff_adjust_symndx)\
1379 . (obfd, info, ibfd, sec, rel, adjustedp))
1380 .#define bfd_coff_link_add_one_symbol(info,abfd,name,flags,section,value,string,cp,coll,hashp)\
1381 . ((coff_backend_info (abfd)->_bfd_coff_link_add_one_symbol)\
1382 . (info, abfd, name, flags, section, value, string, cp, coll, hashp))
1384 .#define bfd_coff_link_output_has_begun(a,p) \
1385 . ((coff_backend_info (a)->_bfd_coff_link_output_has_begun) (a,p))
1386 .#define bfd_coff_final_link_postscript(a,p) \
1387 . ((coff_backend_info (a)->_bfd_coff_final_link_postscript) (a,p))
1391 /* See whether the magic number matches. */
1393 static boolean
1394 coff_bad_format_hook (abfd, filehdr)
1395 bfd * abfd ATTRIBUTE_UNUSED;
1396 PTR filehdr;
1398 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
1400 if (BADMAG (*internal_f))
1401 return false;
1403 /* if the optional header is NULL or not the correct size then
1404 quit; the only difference I can see between m88k dgux headers (MC88DMAGIC)
1405 and Intel 960 readwrite headers (I960WRMAGIC) is that the
1406 optional header is of a different size.
1408 But the mips keeps extra stuff in it's opthdr, so dont check
1409 when doing that
1412 #if defined(M88) || defined(I960)
1413 if (internal_f->f_opthdr != 0 && bfd_coff_aoutsz (abfd) != internal_f->f_opthdr)
1414 return false;
1415 #endif
1417 return true;
1420 /* Check whether this section uses an alignment other than the
1421 default. */
1423 static void
1424 coff_set_custom_section_alignment (abfd, section, alignment_table, table_size)
1425 bfd *abfd ATTRIBUTE_UNUSED;
1426 asection *section;
1427 const struct coff_section_alignment_entry *alignment_table;
1428 const unsigned int table_size;
1430 const unsigned int default_alignment = COFF_DEFAULT_SECTION_ALIGNMENT_POWER;
1431 unsigned int i;
1433 for (i = 0; i < table_size; ++i)
1435 const char *secname = bfd_get_section_name (abfd, section);
1436 if (alignment_table[i].comparison_length == (unsigned int) -1
1437 ? strcmp (alignment_table[i].name, secname) == 0
1438 : strncmp (alignment_table[i].name, secname,
1439 alignment_table[i].comparison_length) == 0)
1440 break;
1442 if (i >= table_size)
1443 return;
1445 if (alignment_table[i].default_alignment_min != COFF_ALIGNMENT_FIELD_EMPTY
1446 && default_alignment < alignment_table[i].default_alignment_min)
1447 return;
1449 if (alignment_table[i].default_alignment_max != COFF_ALIGNMENT_FIELD_EMPTY
1450 && default_alignment > alignment_table[i].default_alignment_max)
1451 return;
1453 section->alignment_power = alignment_table[i].alignment_power;
1456 /* Custom section alignment records. */
1458 static const struct coff_section_alignment_entry
1459 coff_section_alignment_table[] =
1461 #ifdef COFF_SECTION_ALIGNMENT_ENTRIES
1462 COFF_SECTION_ALIGNMENT_ENTRIES,
1463 #endif
1464 /* There must not be any gaps between .stabstr sections. */
1465 { COFF_SECTION_NAME_PARTIAL_MATCH (".stabstr"),
1466 1, COFF_ALIGNMENT_FIELD_EMPTY, 0 },
1467 /* The .stab section must be aligned to 2**2 at most, to avoid gaps. */
1468 { COFF_SECTION_NAME_PARTIAL_MATCH (".stab"),
1469 3, COFF_ALIGNMENT_FIELD_EMPTY, 2 },
1470 /* Similarly for the .ctors and .dtors sections. */
1471 { COFF_SECTION_NAME_EXACT_MATCH (".ctors"),
1472 3, COFF_ALIGNMENT_FIELD_EMPTY, 2 },
1473 { COFF_SECTION_NAME_EXACT_MATCH (".dtors"),
1474 3, COFF_ALIGNMENT_FIELD_EMPTY, 2 }
1477 static const unsigned int coff_section_alignment_table_size =
1478 sizeof coff_section_alignment_table / sizeof coff_section_alignment_table[0];
1480 /* Initialize a section structure with information peculiar to this
1481 particular implementation of COFF. */
1483 static boolean
1484 coff_new_section_hook (abfd, section)
1485 bfd * abfd;
1486 asection * section;
1488 combined_entry_type *native;
1490 section->alignment_power = COFF_DEFAULT_SECTION_ALIGNMENT_POWER;
1492 #ifdef RS6000COFF_C
1493 if (xcoff_data (abfd)->text_align_power != 0
1494 && strcmp (bfd_get_section_name (abfd, section), ".text") == 0)
1495 section->alignment_power = xcoff_data (abfd)->text_align_power;
1496 if (xcoff_data (abfd)->data_align_power != 0
1497 && strcmp (bfd_get_section_name (abfd, section), ".data") == 0)
1498 section->alignment_power = xcoff_data (abfd)->data_align_power;
1499 #endif
1501 /* Allocate aux records for section symbols, to store size and
1502 related info.
1504 @@ The 10 is a guess at a plausible maximum number of aux entries
1505 (but shouldn't be a constant). */
1506 native = ((combined_entry_type *)
1507 bfd_zalloc (abfd, sizeof (combined_entry_type) * 10));
1508 if (native == NULL)
1509 return false;
1511 /* We don't need to set up n_name, n_value, or n_scnum in the native
1512 symbol information, since they'll be overriden by the BFD symbol
1513 anyhow. However, we do need to set the type and storage class,
1514 in case this symbol winds up getting written out. The value 0
1515 for n_numaux is already correct. */
1517 native->u.syment.n_type = T_NULL;
1518 native->u.syment.n_sclass = C_STAT;
1520 coffsymbol (section->symbol)->native = native;
1522 coff_set_custom_section_alignment (abfd, section,
1523 coff_section_alignment_table,
1524 coff_section_alignment_table_size);
1526 return true;
1529 #ifdef COFF_ALIGN_IN_SECTION_HEADER
1531 /* Set the alignment of a BFD section. */
1533 static void coff_set_alignment_hook PARAMS ((bfd *, asection *, PTR));
1535 static void
1536 coff_set_alignment_hook (abfd, section, scnhdr)
1537 bfd * abfd ATTRIBUTE_UNUSED;
1538 asection * section;
1539 PTR scnhdr;
1541 struct internal_scnhdr *hdr = (struct internal_scnhdr *) scnhdr;
1542 unsigned int i;
1544 #ifdef I960
1545 /* Extract ALIGN from 2**ALIGN stored in section header */
1546 for (i = 0; i < 32; i++)
1547 if ((1 << i) >= hdr->s_align)
1548 break;
1549 #endif
1550 #ifdef TIC80COFF
1551 /* TI tools puts the alignment power in bits 8-11 */
1552 i = (hdr->s_flags >> 8) & 0xF ;
1553 #endif
1554 #ifdef COFF_DECODE_ALIGNMENT
1555 i = COFF_DECODE_ALIGNMENT(hdr->s_flags);
1556 #endif
1557 section->alignment_power = i;
1559 #ifdef coff_set_section_load_page
1560 coff_set_section_load_page (section, hdr->s_page);
1561 #endif
1564 #else /* ! COFF_ALIGN_IN_SECTION_HEADER */
1565 #ifdef COFF_WITH_PE
1567 /* a couple of macros to help setting the alignment power field */
1568 #define ALIGN_SET(field,x,y) \
1569 if (((field) & IMAGE_SCN_ALIGN_64BYTES) == x )\
1571 section->alignment_power = y;\
1574 #define ELIFALIGN_SET(field,x,y) \
1575 else if (( (field) & IMAGE_SCN_ALIGN_64BYTES) == x ) \
1577 section->alignment_power = y;\
1580 static void coff_set_alignment_hook PARAMS ((bfd *, asection *, PTR));
1582 static void
1583 coff_set_alignment_hook (abfd, section, scnhdr)
1584 bfd * abfd ATTRIBUTE_UNUSED;
1585 asection * section;
1586 PTR scnhdr;
1588 struct internal_scnhdr *hdr = (struct internal_scnhdr *) scnhdr;
1590 ALIGN_SET (hdr->s_flags, IMAGE_SCN_ALIGN_64BYTES, 6)
1591 ELIFALIGN_SET (hdr->s_flags, IMAGE_SCN_ALIGN_32BYTES, 5)
1592 ELIFALIGN_SET (hdr->s_flags, IMAGE_SCN_ALIGN_16BYTES, 4)
1593 ELIFALIGN_SET (hdr->s_flags, IMAGE_SCN_ALIGN_8BYTES, 3)
1594 ELIFALIGN_SET (hdr->s_flags, IMAGE_SCN_ALIGN_4BYTES, 2)
1595 ELIFALIGN_SET (hdr->s_flags, IMAGE_SCN_ALIGN_2BYTES, 1)
1596 ELIFALIGN_SET (hdr->s_flags, IMAGE_SCN_ALIGN_1BYTES, 0)
1598 /* In a PE image file, the s_paddr field holds the virtual size of a
1599 section, while the s_size field holds the raw size. We also keep
1600 the original section flag value, since not every bit can be
1601 mapped onto a generic BFD section bit. */
1602 if (coff_section_data (abfd, section) == NULL)
1604 section->used_by_bfd =
1605 (PTR) bfd_zalloc (abfd, sizeof (struct coff_section_tdata));
1606 if (section->used_by_bfd == NULL)
1608 /* FIXME: Return error. */
1609 abort ();
1612 if (pei_section_data (abfd, section) == NULL)
1614 coff_section_data (abfd, section)->tdata =
1615 (PTR) bfd_zalloc (abfd, sizeof (struct pei_section_tdata));
1616 if (coff_section_data (abfd, section)->tdata == NULL)
1618 /* FIXME: Return error. */
1619 abort ();
1622 pei_section_data (abfd, section)->virt_size = hdr->s_paddr;
1623 pei_section_data (abfd, section)->pe_flags = hdr->s_flags;
1625 section->lma = hdr->s_vaddr;
1627 /* check for extended relocs */
1628 if (hdr->s_flags & IMAGE_SCN_LNK_NRELOC_OVFL)
1630 struct external_reloc dst;
1631 struct internal_reloc n;
1632 int oldpos = bfd_tell (abfd);
1633 bfd_seek (abfd, hdr->s_relptr, 0);
1634 if (bfd_read ((PTR) & dst, 1, bfd_coff_relsz (abfd), abfd)
1635 != bfd_coff_relsz (abfd))
1636 return;
1638 coff_swap_reloc_in (abfd, &dst, &n);
1639 bfd_seek (abfd, oldpos, 0);
1640 section->reloc_count =
1641 hdr->s_nreloc = n.r_vaddr;
1644 #undef ALIGN_SET
1645 #undef ELIFALIGN_SET
1647 #else /* ! COFF_WITH_PE */
1648 #ifdef RS6000COFF_C
1650 /* We grossly abuse this function to handle XCOFF overflow headers.
1651 When we see one, we correct the reloc and line number counts in the
1652 real header, and remove the section we just created. */
1654 static void coff_set_alignment_hook PARAMS ((bfd *, asection *, PTR));
1656 static void
1657 coff_set_alignment_hook (abfd, section, scnhdr)
1658 bfd *abfd;
1659 asection *section;
1660 PTR scnhdr;
1662 struct internal_scnhdr *hdr = (struct internal_scnhdr *) scnhdr;
1663 asection *real_sec;
1664 asection **ps;
1666 if ((hdr->s_flags & STYP_OVRFLO) == 0)
1667 return;
1669 real_sec = coff_section_from_bfd_index (abfd, hdr->s_nreloc);
1670 if (real_sec == NULL)
1671 return;
1673 real_sec->reloc_count = hdr->s_paddr;
1674 real_sec->lineno_count = hdr->s_vaddr;
1676 for (ps = &abfd->sections; *ps != NULL; ps = &(*ps)->next)
1678 if (*ps == section)
1680 *ps = (*ps)->next;
1681 --abfd->section_count;
1682 break;
1687 #else /* ! RS6000COFF_C */
1689 #define coff_set_alignment_hook \
1690 ((void (*) PARAMS ((bfd *, asection *, PTR))) bfd_void)
1692 #endif /* ! RS6000COFF_C */
1693 #endif /* ! COFF_WITH_PE */
1694 #endif /* ! COFF_ALIGN_IN_SECTION_HEADER */
1696 #ifndef coff_mkobject
1698 static boolean coff_mkobject PARAMS ((bfd *));
1700 static boolean
1701 coff_mkobject (abfd)
1702 bfd * abfd;
1704 coff_data_type *coff;
1706 abfd->tdata.coff_obj_data = (struct coff_tdata *) bfd_zalloc (abfd, sizeof (coff_data_type));
1707 if (abfd->tdata.coff_obj_data == 0)
1708 return false;
1709 coff = coff_data (abfd);
1710 coff->symbols = (coff_symbol_type *) NULL;
1711 coff->conversion_table = (unsigned int *) NULL;
1712 coff->raw_syments = (struct coff_ptr_struct *) NULL;
1713 coff->relocbase = 0;
1714 coff->local_toc_sym_map = 0;
1716 /* make_abs_section(abfd);*/
1718 return true;
1720 #endif
1722 /* Create the COFF backend specific information. */
1723 #ifndef coff_mkobject_hook
1724 static PTR
1725 coff_mkobject_hook (abfd, filehdr, aouthdr)
1726 bfd * abfd;
1727 PTR filehdr;
1728 PTR aouthdr ATTRIBUTE_UNUSED;
1730 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
1731 coff_data_type *coff;
1733 if (coff_mkobject (abfd) == false)
1734 return NULL;
1736 coff = coff_data (abfd);
1738 coff->sym_filepos = internal_f->f_symptr;
1740 /* These members communicate important constants about the symbol
1741 table to GDB's symbol-reading code. These `constants'
1742 unfortunately vary among coff implementations... */
1743 coff->local_n_btmask = N_BTMASK;
1744 coff->local_n_btshft = N_BTSHFT;
1745 coff->local_n_tmask = N_TMASK;
1746 coff->local_n_tshift = N_TSHIFT;
1747 coff->local_symesz = bfd_coff_symesz (abfd);
1748 coff->local_auxesz = bfd_coff_auxesz (abfd);
1749 coff->local_linesz = bfd_coff_linesz (abfd);
1751 coff->timestamp = internal_f->f_timdat;
1753 obj_raw_syment_count (abfd) =
1754 obj_conv_table_size (abfd) =
1755 internal_f->f_nsyms;
1757 #ifdef RS6000COFF_C
1758 if ((internal_f->f_flags & F_SHROBJ) != 0)
1759 abfd->flags |= DYNAMIC;
1760 if (aouthdr != NULL && internal_f->f_opthdr >= bfd_coff_aoutsz (abfd))
1762 struct internal_aouthdr *internal_a =
1763 (struct internal_aouthdr *) aouthdr;
1764 struct xcoff_tdata *xcoff;
1766 xcoff = xcoff_data (abfd);
1767 # ifdef U803XTOCMAGIC
1768 xcoff->xcoff64 = internal_f->f_magic == U803XTOCMAGIC;
1769 # else
1770 xcoff->xcoff64 = 0;
1771 # endif
1772 xcoff->full_aouthdr = true;
1773 xcoff->toc = internal_a->o_toc;
1774 xcoff->sntoc = internal_a->o_sntoc;
1775 xcoff->snentry = internal_a->o_snentry;
1776 xcoff->text_align_power = internal_a->o_algntext;
1777 xcoff->data_align_power = internal_a->o_algndata;
1778 xcoff->modtype = internal_a->o_modtype;
1779 xcoff->cputype = internal_a->o_cputype;
1780 xcoff->maxdata = internal_a->o_maxdata;
1781 xcoff->maxstack = internal_a->o_maxstack;
1783 #endif
1785 #ifdef ARM
1786 /* Set the flags field from the COFF header read in */
1787 if (! _bfd_coff_arm_set_private_flags (abfd, internal_f->f_flags))
1788 coff->flags = 0;
1789 #endif
1791 #ifdef COFF_WITH_PE
1792 /* FIXME: I'm not sure this is ever executed, since peicode.h
1793 defines coff_mkobject_hook. */
1794 if ((internal_f->f_flags & IMAGE_FILE_DEBUG_STRIPPED) == 0)
1795 abfd->flags |= HAS_DEBUG;
1796 #endif
1798 return (PTR) coff;
1800 #endif
1802 /* Determine the machine architecture and type. FIXME: This is target
1803 dependent because the magic numbers are defined in the target
1804 dependent header files. But there is no particular need for this.
1805 If the magic numbers were moved to a separate file, this function
1806 would be target independent and would also be much more successful
1807 at linking together COFF files for different architectures. */
1809 static boolean
1810 coff_set_arch_mach_hook (abfd, filehdr)
1811 bfd *abfd;
1812 PTR filehdr;
1814 long machine;
1815 enum bfd_architecture arch;
1816 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
1818 machine = 0;
1819 switch (internal_f->f_magic)
1821 #ifdef PPCMAGIC
1822 case PPCMAGIC:
1823 arch = bfd_arch_powerpc;
1824 machine = 0; /* what does this mean? (krk) */
1825 break;
1826 #endif
1827 #ifdef I386MAGIC
1828 case I386MAGIC:
1829 case I386PTXMAGIC:
1830 case I386AIXMAGIC: /* Danbury PS/2 AIX C Compiler */
1831 case LYNXCOFFMAGIC: /* shadows the m68k Lynx number below, sigh */
1832 arch = bfd_arch_i386;
1833 machine = 0;
1834 break;
1835 #endif
1836 #ifdef IA64MAGIC
1837 case IA64MAGIC:
1838 arch = bfd_arch_ia64;
1839 machine = 0;
1840 break;
1841 #endif
1842 #ifdef A29K_MAGIC_BIG
1843 case A29K_MAGIC_BIG:
1844 case A29K_MAGIC_LITTLE:
1845 arch = bfd_arch_a29k;
1846 machine = 0;
1847 break;
1848 #endif
1849 #ifdef ARMMAGIC
1850 case ARMMAGIC:
1851 case ARMPEMAGIC:
1852 case THUMBPEMAGIC:
1853 arch = bfd_arch_arm;
1854 switch (internal_f->f_flags & F_ARM_ARCHITECTURE_MASK)
1856 case F_ARM_2: machine = bfd_mach_arm_2; break;
1857 case F_ARM_2a: machine = bfd_mach_arm_2a; break;
1858 case F_ARM_3: machine = bfd_mach_arm_3; break;
1859 default:
1860 case F_ARM_3M: machine = bfd_mach_arm_3M; break;
1861 case F_ARM_4: machine = bfd_mach_arm_4; break;
1862 case F_ARM_4T: machine = bfd_mach_arm_4T; break;
1863 case F_ARM_5: machine = bfd_mach_arm_5; break;
1865 break;
1866 #endif
1867 #ifdef MC68MAGIC
1868 case MC68MAGIC:
1869 case M68MAGIC:
1870 #ifdef MC68KBCSMAGIC
1871 case MC68KBCSMAGIC:
1872 #endif
1873 #ifdef APOLLOM68KMAGIC
1874 case APOLLOM68KMAGIC:
1875 #endif
1876 #ifdef LYNXCOFFMAGIC
1877 case LYNXCOFFMAGIC:
1878 #endif
1879 arch = bfd_arch_m68k;
1880 machine = bfd_mach_m68020;
1881 break;
1882 #endif
1883 #ifdef MC88MAGIC
1884 case MC88MAGIC:
1885 case MC88DMAGIC:
1886 case MC88OMAGIC:
1887 arch = bfd_arch_m88k;
1888 machine = 88100;
1889 break;
1890 #endif
1891 #ifdef Z8KMAGIC
1892 case Z8KMAGIC:
1893 arch = bfd_arch_z8k;
1894 switch (internal_f->f_flags & F_MACHMASK)
1896 case F_Z8001:
1897 machine = bfd_mach_z8001;
1898 break;
1899 case F_Z8002:
1900 machine = bfd_mach_z8002;
1901 break;
1902 default:
1903 return false;
1905 break;
1906 #endif
1907 #ifdef I860
1908 case I860MAGIC:
1909 arch = bfd_arch_i860;
1910 break;
1911 #endif
1912 #ifdef I960
1913 #ifdef I960ROMAGIC
1914 case I960ROMAGIC:
1915 case I960RWMAGIC:
1916 arch = bfd_arch_i960;
1917 switch (F_I960TYPE & internal_f->f_flags)
1919 default:
1920 case F_I960CORE:
1921 machine = bfd_mach_i960_core;
1922 break;
1923 case F_I960KB:
1924 machine = bfd_mach_i960_kb_sb;
1925 break;
1926 case F_I960MC:
1927 machine = bfd_mach_i960_mc;
1928 break;
1929 case F_I960XA:
1930 machine = bfd_mach_i960_xa;
1931 break;
1932 case F_I960CA:
1933 machine = bfd_mach_i960_ca;
1934 break;
1935 case F_I960KA:
1936 machine = bfd_mach_i960_ka_sa;
1937 break;
1938 case F_I960JX:
1939 machine = bfd_mach_i960_jx;
1940 break;
1941 case F_I960HX:
1942 machine = bfd_mach_i960_hx;
1943 break;
1945 break;
1946 #endif
1947 #endif
1949 #ifdef RS6000COFF_C
1950 #ifdef XCOFF64
1951 case U803XTOCMAGIC:
1952 #else
1953 case U802ROMAGIC:
1954 case U802WRMAGIC:
1955 case U802TOCMAGIC:
1956 #endif
1958 int cputype;
1960 if (xcoff_data (abfd)->cputype != -1)
1961 cputype = xcoff_data (abfd)->cputype & 0xff;
1962 else
1964 /* We did not get a value from the a.out header. If the
1965 file has not been stripped, we may be able to get the
1966 architecture information from the first symbol, if it
1967 is a .file symbol. */
1968 if (obj_raw_syment_count (abfd) == 0)
1969 cputype = 0;
1970 else
1972 bfd_byte *buf;
1973 struct internal_syment sym;
1975 buf = (bfd_byte *) bfd_malloc (bfd_coff_symesz (abfd));
1976 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
1977 || (bfd_read (buf, 1, bfd_coff_symesz (abfd), abfd)
1978 != bfd_coff_symesz (abfd)))
1980 free (buf);
1981 return false;
1983 bfd_coff_swap_sym_in (abfd, (PTR) buf, (PTR) &sym);
1984 if (sym.n_sclass == C_FILE)
1985 cputype = sym.n_type & 0xff;
1986 else
1987 cputype = 0;
1988 free (buf);
1992 /* FIXME: We don't handle all cases here. */
1993 switch (cputype)
1995 default:
1996 case 0:
1997 #ifdef POWERMAC
1998 /* PowerPC Macs use the same magic numbers as RS/6000
1999 (because that's how they were bootstrapped originally),
2000 but they are always PowerPC architecture. */
2001 arch = bfd_arch_powerpc;
2002 machine = bfd_mach_ppc;
2003 #else
2004 #ifdef XCOFF64
2005 arch = bfd_arch_powerpc;
2006 machine = bfd_mach_ppc_620;
2007 #else
2008 arch = bfd_arch_rs6000;
2009 machine = bfd_mach_rs6k;
2010 #endif
2011 #endif /* POWERMAC */
2012 break;
2014 case 1:
2015 arch = bfd_arch_powerpc;
2016 machine = bfd_mach_ppc_601;
2017 break;
2018 case 2: /* 64 bit PowerPC */
2019 arch = bfd_arch_powerpc;
2020 machine = bfd_mach_ppc_620;
2021 break;
2022 case 3:
2023 arch = bfd_arch_powerpc;
2024 machine = bfd_mach_ppc;
2025 break;
2026 case 4:
2027 arch = bfd_arch_rs6000;
2028 machine = bfd_mach_rs6k;
2029 break;
2032 break;
2033 #endif
2035 #ifdef WE32KMAGIC
2036 case WE32KMAGIC:
2037 arch = bfd_arch_we32k;
2038 machine = 0;
2039 break;
2040 #endif
2042 #ifdef H8300MAGIC
2043 case H8300MAGIC:
2044 arch = bfd_arch_h8300;
2045 machine = bfd_mach_h8300;
2046 /* !! FIXME this probably isn't the right place for this */
2047 abfd->flags |= BFD_IS_RELAXABLE;
2048 break;
2049 #endif
2051 #ifdef H8300HMAGIC
2052 case H8300HMAGIC:
2053 arch = bfd_arch_h8300;
2054 machine = bfd_mach_h8300h;
2055 /* !! FIXME this probably isn't the right place for this */
2056 abfd->flags |= BFD_IS_RELAXABLE;
2057 break;
2058 #endif
2060 #ifdef H8300SMAGIC
2061 case H8300SMAGIC:
2062 arch = bfd_arch_h8300;
2063 machine = bfd_mach_h8300s;
2064 /* !! FIXME this probably isn't the right place for this */
2065 abfd->flags |= BFD_IS_RELAXABLE;
2066 break;
2067 #endif
2069 #ifdef SH_ARCH_MAGIC_BIG
2070 case SH_ARCH_MAGIC_BIG:
2071 case SH_ARCH_MAGIC_LITTLE:
2072 #ifdef COFF_WITH_PE
2073 case SH_ARCH_MAGIC_WINCE:
2074 #endif
2075 arch = bfd_arch_sh;
2076 machine = 0;
2077 break;
2078 #endif
2080 #ifdef MIPS_ARCH_MAGIC_WINCE
2081 case MIPS_ARCH_MAGIC_WINCE:
2082 arch = bfd_arch_mips;
2083 machine = 0;
2084 break;
2085 #endif
2087 #ifdef H8500MAGIC
2088 case H8500MAGIC:
2089 arch = bfd_arch_h8500;
2090 machine = 0;
2091 break;
2092 #endif
2094 #ifdef SPARCMAGIC
2095 case SPARCMAGIC:
2096 #ifdef LYNXCOFFMAGIC
2097 case LYNXCOFFMAGIC:
2098 #endif
2099 arch = bfd_arch_sparc;
2100 machine = 0;
2101 break;
2102 #endif
2104 #ifdef TIC30MAGIC
2105 case TIC30MAGIC:
2106 arch = bfd_arch_tic30;
2107 break;
2108 #endif
2110 #ifdef TICOFF0MAGIC
2111 #ifdef TICOFF_TARGET_ARCH
2112 /* this TI COFF section should be used by all new TI COFF v0 targets */
2113 case TICOFF0MAGIC:
2114 arch = TICOFF_TARGET_ARCH;
2115 break;
2116 #endif
2117 #endif
2119 #ifdef TICOFF1MAGIC
2120 /* this TI COFF section should be used by all new TI COFF v1/2 targets */
2121 /* TI COFF1 and COFF2 use the target_id field to specify which arch */
2122 case TICOFF1MAGIC:
2123 case TICOFF2MAGIC:
2124 switch (internal_f->f_target_id)
2126 #ifdef TI_TARGET_ID
2127 case TI_TARGET_ID:
2128 arch = TICOFF_TARGET_ARCH;
2129 break;
2130 #endif
2131 default:
2132 arch = bfd_arch_obscure;
2133 (*_bfd_error_handler)
2134 (_("Unrecognized TI COFF target id '0x%x'"),
2135 internal_f->f_target_id);
2136 break;
2138 break;
2139 #endif
2141 #ifdef TIC80_ARCH_MAGIC
2142 case TIC80_ARCH_MAGIC:
2143 arch = bfd_arch_tic80;
2144 break;
2145 #endif
2147 #ifdef MCOREMAGIC
2148 case MCOREMAGIC:
2149 arch = bfd_arch_mcore;
2150 break;
2151 #endif
2152 default: /* Unreadable input file type */
2153 arch = bfd_arch_obscure;
2154 break;
2157 bfd_default_set_arch_mach (abfd, arch, machine);
2158 return true;
2161 #ifdef SYMNAME_IN_DEBUG
2163 static boolean symname_in_debug_hook
2164 PARAMS ((bfd *, struct internal_syment *));
2166 static boolean
2167 symname_in_debug_hook (abfd, sym)
2168 bfd * abfd ATTRIBUTE_UNUSED;
2169 struct internal_syment *sym;
2171 return SYMNAME_IN_DEBUG (sym) ? true : false;
2174 #else
2176 #define symname_in_debug_hook \
2177 (boolean (*) PARAMS ((bfd *, struct internal_syment *))) bfd_false
2179 #endif
2181 #ifdef RS6000COFF_C
2183 #ifdef XCOFF64
2184 #define FORCE_SYMNAMES_IN_STRINGS
2185 #endif
2187 /* Handle the csect auxent of a C_EXT or C_HIDEXT symbol. */
2189 static boolean coff_pointerize_aux_hook
2190 PARAMS ((bfd *, combined_entry_type *, combined_entry_type *,
2191 unsigned int, combined_entry_type *));
2193 /*ARGSUSED*/
2194 static boolean
2195 coff_pointerize_aux_hook (abfd, table_base, symbol, indaux, aux)
2196 bfd *abfd ATTRIBUTE_UNUSED;
2197 combined_entry_type *table_base;
2198 combined_entry_type *symbol;
2199 unsigned int indaux;
2200 combined_entry_type *aux;
2202 int class = symbol->u.syment.n_sclass;
2204 if ((class == C_EXT || class == C_HIDEXT)
2205 && indaux + 1 == symbol->u.syment.n_numaux)
2207 if (SMTYP_SMTYP (aux->u.auxent.x_csect.x_smtyp) == XTY_LD)
2209 aux->u.auxent.x_csect.x_scnlen.p =
2210 table_base + aux->u.auxent.x_csect.x_scnlen.l;
2211 aux->fix_scnlen = 1;
2214 /* Return true to indicate that the caller should not do any
2215 further work on this auxent. */
2216 return true;
2219 /* Return false to indicate that this auxent should be handled by
2220 the caller. */
2221 return false;
2224 #else
2225 #ifdef I960
2227 /* We don't want to pointerize bal entries. */
2229 static boolean coff_pointerize_aux_hook
2230 PARAMS ((bfd *, combined_entry_type *, combined_entry_type *,
2231 unsigned int, combined_entry_type *));
2233 /*ARGSUSED*/
2234 static boolean
2235 coff_pointerize_aux_hook (abfd, table_base, symbol, indaux, aux)
2236 bfd *abfd ATTRIBUTE_UNUSED;
2237 combined_entry_type *table_base ATTRIBUTE_UNUSED;
2238 combined_entry_type *symbol;
2239 unsigned int indaux;
2240 combined_entry_type *aux ATTRIBUTE_UNUSED;
2242 /* Return true if we don't want to pointerize this aux entry, which
2243 is the case for the lastfirst aux entry for a C_LEAFPROC symbol. */
2244 return (indaux == 1
2245 && (symbol->u.syment.n_sclass == C_LEAFPROC
2246 || symbol->u.syment.n_sclass == C_LEAFSTAT
2247 || symbol->u.syment.n_sclass == C_LEAFEXT));
2250 #else /* ! I960 */
2252 #define coff_pointerize_aux_hook 0
2254 #endif /* ! I960 */
2255 #endif /* ! RS6000COFF_C */
2257 /* Print an aux entry. This returns true if it has printed it. */
2259 static boolean coff_print_aux
2260 PARAMS ((bfd *, FILE *, combined_entry_type *, combined_entry_type *,
2261 combined_entry_type *, unsigned int));
2263 static boolean
2264 coff_print_aux (abfd, file, table_base, symbol, aux, indaux)
2265 bfd *abfd ATTRIBUTE_UNUSED;
2266 FILE *file ATTRIBUTE_UNUSED;
2267 combined_entry_type *table_base ATTRIBUTE_UNUSED;
2268 combined_entry_type *symbol ATTRIBUTE_UNUSED;
2269 combined_entry_type *aux ATTRIBUTE_UNUSED;
2270 unsigned int indaux ATTRIBUTE_UNUSED;
2272 #ifdef RS6000COFF_C
2273 if ((symbol->u.syment.n_sclass == C_EXT
2274 || symbol->u.syment.n_sclass == C_HIDEXT)
2275 && indaux + 1 == symbol->u.syment.n_numaux)
2277 /* This is a csect entry. */
2278 fprintf (file, "AUX ");
2279 if (SMTYP_SMTYP (aux->u.auxent.x_csect.x_smtyp) != XTY_LD)
2281 BFD_ASSERT (! aux->fix_scnlen);
2282 fprintf (file, "val %5ld", aux->u.auxent.x_csect.x_scnlen.l);
2284 else
2286 fprintf (file, "indx ");
2287 if (! aux->fix_scnlen)
2288 fprintf (file, "%4ld", aux->u.auxent.x_csect.x_scnlen.l);
2289 else
2290 fprintf (file, "%4ld",
2291 (long) (aux->u.auxent.x_csect.x_scnlen.p - table_base));
2293 fprintf (file,
2294 " prmhsh %ld snhsh %u typ %d algn %d clss %u stb %ld snstb %u",
2295 aux->u.auxent.x_csect.x_parmhash,
2296 (unsigned int) aux->u.auxent.x_csect.x_snhash,
2297 SMTYP_SMTYP (aux->u.auxent.x_csect.x_smtyp),
2298 SMTYP_ALIGN (aux->u.auxent.x_csect.x_smtyp),
2299 (unsigned int) aux->u.auxent.x_csect.x_smclas,
2300 aux->u.auxent.x_csect.x_stab,
2301 (unsigned int) aux->u.auxent.x_csect.x_snstab);
2302 return true;
2304 #endif
2306 /* Return false to indicate that no special action was taken. */
2307 return false;
2311 SUBSUBSECTION
2312 Writing relocations
2314 To write relocations, the back end steps though the
2315 canonical relocation table and create an
2316 @code{internal_reloc}. The symbol index to use is removed from
2317 the @code{offset} field in the symbol table supplied. The
2318 address comes directly from the sum of the section base
2319 address and the relocation offset; the type is dug directly
2320 from the howto field. Then the @code{internal_reloc} is
2321 swapped into the shape of an @code{external_reloc} and written
2322 out to disk.
2326 #ifdef TARG_AUX
2328 static int compare_arelent_ptr PARAMS ((const PTR, const PTR));
2330 /* AUX's ld wants relocations to be sorted */
2331 static int
2332 compare_arelent_ptr (x, y)
2333 const PTR x;
2334 const PTR y;
2336 const arelent **a = (const arelent **) x;
2337 const arelent **b = (const arelent **) y;
2338 bfd_size_type aadr = (*a)->address;
2339 bfd_size_type badr = (*b)->address;
2341 return (aadr < badr ? -1 : badr < aadr ? 1 : 0);
2344 #endif /* TARG_AUX */
2346 static boolean
2347 coff_write_relocs (abfd, first_undef)
2348 bfd * abfd;
2349 int first_undef;
2351 asection *s;
2353 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
2355 unsigned int i;
2356 struct external_reloc dst;
2357 arelent **p;
2359 #ifndef TARG_AUX
2360 p = s->orelocation;
2361 #else
2362 /* sort relocations before we write them out */
2363 p = (arelent **) bfd_malloc (s->reloc_count * sizeof (arelent *));
2364 if (p == NULL && s->reloc_count > 0)
2365 return false;
2366 memcpy (p, s->orelocation, s->reloc_count * sizeof (arelent *));
2367 qsort (p, s->reloc_count, sizeof (arelent *), compare_arelent_ptr);
2368 #endif
2370 if (bfd_seek (abfd, s->rel_filepos, SEEK_SET) != 0)
2371 return false;
2373 #ifdef COFF_WITH_PE
2374 if (s->reloc_count > 0xffff)
2376 /* encode real count here as first reloc */
2377 struct internal_reloc n;
2378 memset ((PTR) & n, 0, sizeof (n));
2379 /* add one to count *this* reloc (grr) */
2380 n.r_vaddr = s->reloc_count + 1;
2381 coff_swap_reloc_out (abfd, &n, &dst);
2382 if (bfd_write ((PTR) & dst, 1, bfd_coff_relsz (abfd), abfd)
2383 != bfd_coff_relsz (abfd))
2384 return false;
2386 #endif
2388 for (i = 0; i < s->reloc_count; i++)
2390 struct internal_reloc n;
2391 arelent *q = p[i];
2392 memset ((PTR) & n, 0, sizeof (n));
2394 /* Now we've renumbered the symbols we know where the
2395 undefined symbols live in the table. Check the reloc
2396 entries for symbols who's output bfd isn't the right one.
2397 This is because the symbol was undefined (which means
2398 that all the pointers are never made to point to the same
2399 place). This is a bad thing,'cause the symbols attached
2400 to the output bfd are indexed, so that the relocation
2401 entries know which symbol index they point to. So we
2402 have to look up the output symbol here. */
2404 if (q->sym_ptr_ptr[0]->the_bfd != abfd)
2406 int i;
2407 const char *sname = q->sym_ptr_ptr[0]->name;
2408 asymbol **outsyms = abfd->outsymbols;
2409 for (i = first_undef; outsyms[i]; i++)
2411 const char *intable = outsyms[i]->name;
2412 if (strcmp (intable, sname) == 0) {
2413 /* got a hit, so repoint the reloc */
2414 q->sym_ptr_ptr = outsyms + i;
2415 break;
2420 n.r_vaddr = q->address + s->vma;
2422 #ifdef R_IHCONST
2423 /* The 29k const/consth reloc pair is a real kludge. The consth
2424 part doesn't have a symbol; it has an offset. So rebuilt
2425 that here. */
2426 if (q->howto->type == R_IHCONST)
2427 n.r_symndx = q->addend;
2428 else
2429 #endif
2430 if (q->sym_ptr_ptr)
2432 #ifdef SECTION_RELATIVE_ABSOLUTE_SYMBOL_P
2433 if (SECTION_RELATIVE_ABSOLUTE_SYMBOL_P (q,s))
2434 #else
2435 if (q->sym_ptr_ptr == bfd_abs_section_ptr->symbol_ptr_ptr)
2436 #endif
2437 /* This is a relocation relative to the absolute symbol. */
2438 n.r_symndx = -1;
2439 else
2441 n.r_symndx = get_index ((*(q->sym_ptr_ptr)));
2442 /* Take notice if the symbol reloc points to a symbol
2443 we don't have in our symbol table. What should we
2444 do for this?? */
2445 if (n.r_symndx > obj_conv_table_size (abfd))
2446 abort ();
2450 #ifdef SWAP_OUT_RELOC_OFFSET
2451 n.r_offset = q->addend;
2452 #endif
2454 #ifdef SELECT_RELOC
2455 /* Work out reloc type from what is required */
2456 SELECT_RELOC (n, q->howto);
2457 #else
2458 n.r_type = q->howto->type;
2459 #endif
2460 coff_swap_reloc_out (abfd, &n, &dst);
2461 if (bfd_write ((PTR) & dst, 1, bfd_coff_relsz (abfd), abfd)
2462 != bfd_coff_relsz (abfd))
2463 return false;
2466 #ifdef TARG_AUX
2467 if (p != NULL)
2468 free (p);
2469 #endif
2472 return true;
2475 /* Set flags and magic number of a coff file from architecture and machine
2476 type. Result is true if we can represent the arch&type, false if not. */
2478 static boolean
2479 coff_set_flags (abfd, magicp, flagsp)
2480 bfd * abfd;
2481 unsigned int *magicp ATTRIBUTE_UNUSED;
2482 unsigned short *flagsp ATTRIBUTE_UNUSED;
2484 switch (bfd_get_arch (abfd))
2486 #ifdef Z8KMAGIC
2487 case bfd_arch_z8k:
2488 *magicp = Z8KMAGIC;
2489 switch (bfd_get_mach (abfd))
2491 case bfd_mach_z8001:
2492 *flagsp = F_Z8001;
2493 break;
2494 case bfd_mach_z8002:
2495 *flagsp = F_Z8002;
2496 break;
2497 default:
2498 return false;
2500 return true;
2501 #endif
2502 #ifdef I960ROMAGIC
2504 case bfd_arch_i960:
2507 unsigned flags;
2508 *magicp = I960ROMAGIC;
2510 ((bfd_get_file_flags(abfd) & WP_TEXT) ? I960ROMAGIC :
2511 I960RWMAGIC); FIXME???
2513 switch (bfd_get_mach (abfd))
2515 case bfd_mach_i960_core:
2516 flags = F_I960CORE;
2517 break;
2518 case bfd_mach_i960_kb_sb:
2519 flags = F_I960KB;
2520 break;
2521 case bfd_mach_i960_mc:
2522 flags = F_I960MC;
2523 break;
2524 case bfd_mach_i960_xa:
2525 flags = F_I960XA;
2526 break;
2527 case bfd_mach_i960_ca:
2528 flags = F_I960CA;
2529 break;
2530 case bfd_mach_i960_ka_sa:
2531 flags = F_I960KA;
2532 break;
2533 case bfd_mach_i960_jx:
2534 flags = F_I960JX;
2535 break;
2536 case bfd_mach_i960_hx:
2537 flags = F_I960HX;
2538 break;
2539 default:
2540 return false;
2542 *flagsp = flags;
2543 return true;
2545 break;
2546 #endif
2548 #ifdef TIC30MAGIC
2549 case bfd_arch_tic30:
2550 *magicp = TIC30MAGIC;
2551 return true;
2552 #endif
2554 #ifdef TICOFF_DEFAULT_MAGIC
2555 case TICOFF_TARGET_ARCH:
2556 /* if there's no indication of which version we want, use the default */
2557 if (!abfd->xvec )
2558 *magicp = TICOFF_DEFAULT_MAGIC;
2559 else
2561 /* we may want to output in a different COFF version */
2562 switch (abfd->xvec->name[4])
2564 case '0':
2565 *magicp = TICOFF0MAGIC;
2566 break;
2567 case '1':
2568 *magicp = TICOFF1MAGIC;
2569 break;
2570 case '2':
2571 *magicp = TICOFF2MAGIC;
2572 break;
2573 default:
2574 return false;
2577 return true;
2578 #endif
2580 #ifdef TIC80_ARCH_MAGIC
2581 case bfd_arch_tic80:
2582 *magicp = TIC80_ARCH_MAGIC;
2583 return true;
2584 #endif
2585 #ifdef ARMMAGIC
2586 case bfd_arch_arm:
2587 #ifdef ARM_WINCE
2588 * magicp = ARMPEMAGIC;
2589 #else
2590 * magicp = ARMMAGIC;
2591 #endif
2592 * flagsp = 0;
2593 if (APCS_SET (abfd))
2595 if (APCS_26_FLAG (abfd))
2596 * flagsp |= F_APCS26;
2598 if (APCS_FLOAT_FLAG (abfd))
2599 * flagsp |= F_APCS_FLOAT;
2601 if (PIC_FLAG (abfd))
2602 * flagsp |= F_PIC;
2604 if (INTERWORK_SET (abfd) && INTERWORK_FLAG (abfd))
2605 * flagsp |= F_INTERWORK;
2606 switch (bfd_get_mach (abfd))
2608 case bfd_mach_arm_2: * flagsp |= F_ARM_2; break;
2609 case bfd_mach_arm_2a: * flagsp |= F_ARM_2a; break;
2610 case bfd_mach_arm_3: * flagsp |= F_ARM_3; break;
2611 case bfd_mach_arm_3M: * flagsp |= F_ARM_3M; break;
2612 case bfd_mach_arm_4: * flagsp |= F_ARM_4; break;
2613 case bfd_mach_arm_4T: * flagsp |= F_ARM_4T; break;
2614 case bfd_mach_arm_5: * flagsp |= F_ARM_5; break;
2615 /* FIXME: we do not have F_ARM vaues greater than F_ARM_5. */
2616 case bfd_mach_arm_5T: * flagsp |= F_ARM_5; break;
2617 case bfd_mach_arm_5TE: * flagsp |= F_ARM_5; break;
2618 case bfd_mach_arm_XScale: * flagsp |= F_ARM_5; break;
2620 return true;
2621 #endif
2622 #ifdef PPCMAGIC
2623 case bfd_arch_powerpc:
2624 *magicp = PPCMAGIC;
2625 return true;
2626 break;
2627 #endif
2628 #ifdef I386MAGIC
2629 case bfd_arch_i386:
2630 *magicp = I386MAGIC;
2631 #ifdef LYNXOS
2632 /* Just overwrite the usual value if we're doing Lynx. */
2633 *magicp = LYNXCOFFMAGIC;
2634 #endif
2635 return true;
2636 break;
2637 #endif
2638 #ifdef I860MAGIC
2639 case bfd_arch_i860:
2640 *magicp = I860MAGIC;
2641 return true;
2642 break;
2643 #endif
2644 #ifdef IA64MAGIC
2645 case bfd_arch_ia64:
2646 *magicp = IA64MAGIC;
2647 return true;
2648 break;
2649 #endif
2650 #ifdef MC68MAGIC
2651 case bfd_arch_m68k:
2652 #ifdef APOLLOM68KMAGIC
2653 *magicp = APOLLO_COFF_VERSION_NUMBER;
2654 #else
2655 /* NAMES_HAVE_UNDERSCORE may be defined by coff-u68k.c. */
2656 #ifdef NAMES_HAVE_UNDERSCORE
2657 *magicp = MC68KBCSMAGIC;
2658 #else
2659 *magicp = MC68MAGIC;
2660 #endif
2661 #endif
2662 #ifdef LYNXOS
2663 /* Just overwrite the usual value if we're doing Lynx. */
2664 *magicp = LYNXCOFFMAGIC;
2665 #endif
2666 return true;
2667 break;
2668 #endif
2670 #ifdef MC88MAGIC
2671 case bfd_arch_m88k:
2672 *magicp = MC88OMAGIC;
2673 return true;
2674 break;
2675 #endif
2676 #ifdef H8300MAGIC
2677 case bfd_arch_h8300:
2678 switch (bfd_get_mach (abfd))
2680 case bfd_mach_h8300:
2681 *magicp = H8300MAGIC;
2682 return true;
2683 case bfd_mach_h8300h:
2684 *magicp = H8300HMAGIC;
2685 return true;
2686 case bfd_mach_h8300s:
2687 *magicp = H8300SMAGIC;
2688 return true;
2690 break;
2691 #endif
2693 #ifdef SH_ARCH_MAGIC_BIG
2694 case bfd_arch_sh:
2695 #ifdef COFF_IMAGE_WITH_PE
2696 *magicp = SH_ARCH_MAGIC_WINCE;
2697 #else
2698 if (bfd_big_endian (abfd))
2699 *magicp = SH_ARCH_MAGIC_BIG;
2700 else
2701 *magicp = SH_ARCH_MAGIC_LITTLE;
2702 #endif
2703 return true;
2704 break;
2705 #endif
2707 #ifdef MIPS_ARCH_MAGIC_WINCE
2708 case bfd_arch_mips:
2709 *magicp = MIPS_ARCH_MAGIC_WINCE;
2710 return true;
2711 break;
2712 #endif
2714 #ifdef SPARCMAGIC
2715 case bfd_arch_sparc:
2716 *magicp = SPARCMAGIC;
2717 #ifdef LYNXOS
2718 /* Just overwrite the usual value if we're doing Lynx. */
2719 *magicp = LYNXCOFFMAGIC;
2720 #endif
2721 return true;
2722 break;
2723 #endif
2725 #ifdef H8500MAGIC
2726 case bfd_arch_h8500:
2727 *magicp = H8500MAGIC;
2728 return true;
2729 break;
2730 #endif
2731 #ifdef A29K_MAGIC_BIG
2732 case bfd_arch_a29k:
2733 if (bfd_big_endian (abfd))
2734 *magicp = A29K_MAGIC_BIG;
2735 else
2736 *magicp = A29K_MAGIC_LITTLE;
2737 return true;
2738 break;
2739 #endif
2741 #ifdef WE32KMAGIC
2742 case bfd_arch_we32k:
2743 *magicp = WE32KMAGIC;
2744 return true;
2745 break;
2746 #endif
2748 #ifdef RS6000COFF_C
2749 case bfd_arch_rs6000:
2750 #ifndef PPCMAGIC
2751 case bfd_arch_powerpc:
2752 #endif
2753 #ifdef XCOFF64
2754 if (bfd_get_mach (abfd) == bfd_mach_ppc_620
2755 && !strncmp (abfd->xvec->name,"aix", 3))
2756 *magicp = U803XTOCMAGIC;
2757 else
2758 #else
2759 *magicp = U802TOCMAGIC;
2760 #endif
2761 return true;
2762 break;
2763 #endif
2765 #ifdef MCOREMAGIC
2766 case bfd_arch_mcore:
2767 * magicp = MCOREMAGIC;
2768 return true;
2769 #endif
2771 #ifdef W65MAGIC
2772 case bfd_arch_w65:
2773 *magicp = W65MAGIC;
2774 return true;
2775 #endif
2777 default: /* Unknown architecture. */
2778 /* Fall through to "return false" below, to avoid
2779 "statement never reached" errors on the one below. */
2780 break;
2783 return false;
2786 static boolean
2787 coff_set_arch_mach (abfd, arch, machine)
2788 bfd * abfd;
2789 enum bfd_architecture arch;
2790 unsigned long machine;
2792 unsigned dummy1;
2793 unsigned short dummy2;
2795 if (! bfd_default_set_arch_mach (abfd, arch, machine))
2796 return false;
2798 if (arch != bfd_arch_unknown &&
2799 coff_set_flags (abfd, &dummy1, &dummy2) != true)
2800 return false; /* We can't represent this type */
2802 return true; /* We're easy ... */
2805 #ifdef COFF_IMAGE_WITH_PE
2807 /* This is used to sort sections by VMA, as required by PE image
2808 files. */
2810 static int sort_by_secaddr PARAMS ((const PTR, const PTR));
2812 static int
2813 sort_by_secaddr (arg1, arg2)
2814 const PTR arg1;
2815 const PTR arg2;
2817 const asection *a = *(const asection **) arg1;
2818 const asection *b = *(const asection **) arg2;
2820 if (a->vma < b->vma)
2821 return -1;
2822 else if (a->vma > b->vma)
2823 return 1;
2824 else
2825 return 0;
2828 #endif /* COFF_IMAGE_WITH_PE */
2830 /* Calculate the file position for each section. */
2832 #ifndef I960
2833 #define ALIGN_SECTIONS_IN_FILE
2834 #endif
2835 #if defined(TIC80COFF) || defined(TICOFF)
2836 #undef ALIGN_SECTIONS_IN_FILE
2837 #endif
2839 static boolean
2840 coff_compute_section_file_positions (abfd)
2841 bfd * abfd;
2843 asection *current;
2844 asection *previous = (asection *) NULL;
2845 file_ptr sofar = bfd_coff_filhsz (abfd);
2846 boolean align_adjust;
2847 #ifdef ALIGN_SECTIONS_IN_FILE
2848 file_ptr old_sofar;
2849 #endif
2851 #ifdef RS6000COFF_C
2852 /* On XCOFF, if we have symbols, set up the .debug section. */
2853 if (bfd_get_symcount (abfd) > 0)
2855 bfd_size_type sz;
2856 bfd_size_type i, symcount;
2857 asymbol **symp;
2859 sz = 0;
2860 symcount = bfd_get_symcount (abfd);
2861 for (symp = abfd->outsymbols, i = 0; i < symcount; symp++, i++)
2863 coff_symbol_type *cf;
2865 cf = coff_symbol_from (abfd, *symp);
2866 if (cf != NULL
2867 && cf->native != NULL
2868 && SYMNAME_IN_DEBUG (&cf->native->u.syment))
2870 size_t len;
2872 len = strlen (bfd_asymbol_name (*symp));
2873 if (len > SYMNMLEN || bfd_coff_force_symnames_in_strings (abfd))
2874 sz += len + 1 + bfd_coff_debug_string_prefix_length (abfd);
2877 if (sz > 0)
2879 asection *dsec;
2881 dsec = bfd_make_section_old_way (abfd, ".debug");
2882 if (dsec == NULL)
2883 abort ();
2884 dsec->_raw_size = sz;
2885 dsec->flags |= SEC_HAS_CONTENTS;
2888 #endif
2890 #ifdef COFF_IMAGE_WITH_PE
2891 int page_size;
2892 if (coff_data (abfd)->link_info)
2894 page_size = pe_data (abfd)->pe_opthdr.FileAlignment;
2896 else
2897 page_size = PE_DEF_FILE_ALIGNMENT;
2898 #else
2899 #ifdef COFF_PAGE_SIZE
2900 int page_size = COFF_PAGE_SIZE;
2901 #endif
2902 #endif
2904 if (bfd_get_start_address (abfd))
2906 /* A start address may have been added to the original file. In this
2907 case it will need an optional header to record it. */
2908 abfd->flags |= EXEC_P;
2911 if (abfd->flags & EXEC_P)
2912 sofar += bfd_coff_aoutsz (abfd);
2913 #ifdef RS6000COFF_C
2914 else if (xcoff_data (abfd)->full_aouthdr)
2915 sofar += bfd_coff_aoutsz (abfd);
2916 else
2917 sofar += SMALL_AOUTSZ;
2918 #endif
2920 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
2922 #ifdef RS6000COFF_C
2923 /* XCOFF handles overflows in the reloc and line number count fields
2924 by allocating a new section header to hold the correct counts. */
2925 for (current = abfd->sections; current != NULL; current = current->next)
2926 if (current->reloc_count >= 0xffff || current->lineno_count >= 0xffff)
2927 sofar += bfd_coff_scnhsz (abfd);
2928 #endif
2930 #ifdef COFF_IMAGE_WITH_PE
2932 /* PE requires the sections to be in memory order when listed in
2933 the section headers. It also does not like empty loadable
2934 sections. The sections apparently do not have to be in the
2935 right order in the image file itself, but we do need to get the
2936 target_index values right. */
2938 int count;
2939 asection **section_list;
2940 int i;
2941 int target_index;
2943 count = 0;
2944 for (current = abfd->sections; current != NULL; current = current->next)
2945 ++count;
2947 /* We allocate an extra cell to simplify the final loop. */
2948 section_list = bfd_malloc (sizeof (struct asection *) * (count + 1));
2949 if (section_list == NULL)
2950 return false;
2952 i = 0;
2953 for (current = abfd->sections; current != NULL; current = current->next)
2955 section_list[i] = current;
2956 ++i;
2958 section_list[i] = NULL;
2960 qsort (section_list, count, sizeof (asection *), sort_by_secaddr);
2962 /* Rethread the linked list into sorted order; at the same time,
2963 assign target_index values. */
2964 target_index = 1;
2965 abfd->sections = section_list[0];
2966 for (i = 0; i < count; i++)
2968 current = section_list[i];
2969 current->next = section_list[i + 1];
2971 /* Later, if the section has zero size, we'll be throwing it
2972 away, so we don't want to number it now. Note that having
2973 a zero size and having real contents are different
2974 concepts: .bss has no contents, but (usually) non-zero
2975 size. */
2976 if (current->_raw_size == 0)
2978 /* Discard. However, it still might have (valid) symbols
2979 in it, so arbitrarily set it to section 1 (indexing is
2980 1-based here; usually .text). __end__ and other
2981 contents of .endsection really have this happen.
2982 FIXME: This seems somewhat dubious. */
2983 current->target_index = 1;
2985 else
2986 current->target_index = target_index++;
2989 free (section_list);
2991 #else /* ! COFF_IMAGE_WITH_PE */
2993 /* Set the target_index field. */
2994 int target_index;
2996 target_index = 1;
2997 for (current = abfd->sections; current != NULL; current = current->next)
2998 current->target_index = target_index++;
3000 #endif /* ! COFF_IMAGE_WITH_PE */
3002 align_adjust = false;
3003 for (current = abfd->sections;
3004 current != (asection *) NULL;
3005 current = current->next)
3007 #ifdef COFF_IMAGE_WITH_PE
3008 /* With PE we have to pad each section to be a multiple of its
3009 page size too, and remember both sizes. */
3010 if (coff_section_data (abfd, current) == NULL)
3012 current->used_by_bfd =
3013 (PTR) bfd_zalloc (abfd, sizeof (struct coff_section_tdata));
3014 if (current->used_by_bfd == NULL)
3015 return false;
3017 if (pei_section_data (abfd, current) == NULL)
3019 coff_section_data (abfd, current)->tdata =
3020 (PTR) bfd_zalloc (abfd, sizeof (struct pei_section_tdata));
3021 if (coff_section_data (abfd, current)->tdata == NULL)
3022 return false;
3024 if (pei_section_data (abfd, current)->virt_size == 0)
3025 pei_section_data (abfd, current)->virt_size = current->_raw_size;
3026 #endif
3028 /* Only deal with sections which have contents. */
3029 if (!(current->flags & SEC_HAS_CONTENTS))
3030 continue;
3032 #ifdef COFF_IMAGE_WITH_PE
3033 /* Make sure we skip empty sections in a PE image. */
3034 if (current->_raw_size == 0)
3035 continue;
3036 #endif
3038 /* Align the sections in the file to the same boundary on
3039 which they are aligned in virtual memory. I960 doesn't
3040 do this (FIXME) so we can stay in sync with Intel. 960
3041 doesn't yet page from files... */
3042 #ifdef ALIGN_SECTIONS_IN_FILE
3043 if ((abfd->flags & EXEC_P) != 0)
3045 /* make sure this section is aligned on the right boundary - by
3046 padding the previous section up if necessary */
3048 old_sofar = sofar;
3049 sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
3050 if (previous != (asection *) NULL)
3052 previous->_raw_size += sofar - old_sofar;
3056 #endif
3058 /* In demand paged files the low order bits of the file offset
3059 must match the low order bits of the virtual address. */
3060 #ifdef COFF_PAGE_SIZE
3061 if ((abfd->flags & D_PAGED) != 0
3062 && (current->flags & SEC_ALLOC) != 0)
3063 sofar += (current->vma - sofar) % page_size;
3064 #endif
3065 current->filepos = sofar;
3067 #ifdef COFF_IMAGE_WITH_PE
3068 /* Set the padded size. */
3069 current->_raw_size = (current->_raw_size + page_size -1) & -page_size;
3070 #endif
3072 sofar += current->_raw_size;
3074 #ifdef ALIGN_SECTIONS_IN_FILE
3075 /* make sure that this section is of the right size too */
3076 if ((abfd->flags & EXEC_P) == 0)
3078 bfd_size_type old_size;
3080 old_size = current->_raw_size;
3081 current->_raw_size = BFD_ALIGN (current->_raw_size,
3082 1 << current->alignment_power);
3083 align_adjust = current->_raw_size != old_size;
3084 sofar += current->_raw_size - old_size;
3086 else
3088 old_sofar = sofar;
3089 sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
3090 align_adjust = sofar != old_sofar;
3091 current->_raw_size += sofar - old_sofar;
3093 #endif
3095 #ifdef COFF_IMAGE_WITH_PE
3096 /* For PE we need to make sure we pad out to the aligned
3097 _raw_size, in case the caller only writes out data to the
3098 unaligned _raw_size. */
3099 if (pei_section_data (abfd, current)->virt_size < current->_raw_size)
3100 align_adjust = true;
3101 #endif
3103 #ifdef _LIB
3104 /* Force .lib sections to start at zero. The vma is then
3105 incremented in coff_set_section_contents. This is right for
3106 SVR3.2. */
3107 if (strcmp (current->name, _LIB) == 0)
3108 bfd_set_section_vma (abfd, current, 0);
3109 #endif
3111 previous = current;
3114 /* It is now safe to write to the output file. If we needed an
3115 alignment adjustment for the last section, then make sure that
3116 there is a byte at offset sofar. If there are no symbols and no
3117 relocs, then nothing follows the last section. If we don't force
3118 the last byte out, then the file may appear to be truncated. */
3119 if (align_adjust)
3121 bfd_byte b;
3123 b = 0;
3124 if (bfd_seek (abfd, sofar - 1, SEEK_SET) != 0
3125 || bfd_write (&b, 1, 1, abfd) != 1)
3126 return false;
3129 /* Make sure the relocations are aligned. We don't need to make
3130 sure that this byte exists, because it will only matter if there
3131 really are relocs. */
3132 sofar = BFD_ALIGN (sofar, 1 << COFF_DEFAULT_SECTION_ALIGNMENT_POWER);
3134 obj_relocbase (abfd) = sofar;
3135 abfd->output_has_begun = true;
3137 return true;
3140 #if 0
3142 /* This can never work, because it is called too late--after the
3143 section positions have been set. I can't figure out what it is
3144 for, so I am going to disable it--Ian Taylor 20 March 1996. */
3146 /* If .file, .text, .data, .bss symbols are missing, add them. */
3147 /* @@ Should we only be adding missing symbols, or overriding the aux
3148 values for existing section symbols? */
3149 static boolean
3150 coff_add_missing_symbols (abfd)
3151 bfd *abfd;
3153 unsigned int nsyms = bfd_get_symcount (abfd);
3154 asymbol **sympp = abfd->outsymbols;
3155 asymbol **sympp2;
3156 unsigned int i;
3157 int need_text = 1, need_data = 1, need_bss = 1, need_file = 1;
3159 for (i = 0; i < nsyms; i++)
3161 coff_symbol_type *csym = coff_symbol_from (abfd, sympp[i]);
3162 CONST char *name;
3163 if (csym)
3165 /* only do this if there is a coff representation of the input
3166 symbol */
3167 if (csym->native && csym->native->u.syment.n_sclass == C_FILE)
3169 need_file = 0;
3170 continue;
3172 name = csym->symbol.name;
3173 if (!name)
3174 continue;
3175 if (!strcmp (name, _TEXT))
3176 need_text = 0;
3177 #ifdef APOLLO_M68
3178 else if (!strcmp (name, ".wtext"))
3179 need_text = 0;
3180 #endif
3181 else if (!strcmp (name, _DATA))
3182 need_data = 0;
3183 else if (!strcmp (name, _BSS))
3184 need_bss = 0;
3187 /* Now i == bfd_get_symcount (abfd). */
3188 /* @@ For now, don't deal with .file symbol. */
3189 need_file = 0;
3191 if (!need_text && !need_data && !need_bss && !need_file)
3192 return true;
3193 nsyms += need_text + need_data + need_bss + need_file;
3194 sympp2 = (asymbol **) bfd_alloc (abfd, nsyms * sizeof (asymbol *));
3195 if (!sympp2)
3196 return false;
3197 memcpy (sympp2, sympp, i * sizeof (asymbol *));
3198 if (need_file)
3200 /* @@ Generate fake .file symbol, in sympp2[i], and increment i. */
3201 abort ();
3203 if (need_text)
3204 sympp2[i++] = coff_section_symbol (abfd, _TEXT);
3205 if (need_data)
3206 sympp2[i++] = coff_section_symbol (abfd, _DATA);
3207 if (need_bss)
3208 sympp2[i++] = coff_section_symbol (abfd, _BSS);
3209 BFD_ASSERT (i == nsyms);
3210 bfd_set_symtab (abfd, sympp2, nsyms);
3211 return true;
3214 #endif /* 0 */
3216 /* SUPPRESS 558 */
3217 /* SUPPRESS 529 */
3218 static boolean
3219 coff_write_object_contents (abfd)
3220 bfd * abfd;
3222 asection *current;
3223 boolean hasrelocs = false;
3224 boolean haslinno = false;
3225 boolean hasdebug = false;
3226 file_ptr scn_base;
3227 file_ptr reloc_base;
3228 file_ptr lineno_base;
3229 file_ptr sym_base;
3230 unsigned long reloc_size = 0, reloc_count = 0;
3231 unsigned long lnno_size = 0;
3232 boolean long_section_names;
3233 asection *text_sec = NULL;
3234 asection *data_sec = NULL;
3235 asection *bss_sec = NULL;
3236 struct internal_filehdr internal_f;
3237 struct internal_aouthdr internal_a;
3238 #ifdef COFF_LONG_SECTION_NAMES
3239 size_t string_size = STRING_SIZE_SIZE;
3240 #endif
3242 bfd_set_error (bfd_error_system_call);
3244 /* Make a pass through the symbol table to count line number entries and
3245 put them into the correct asections */
3247 lnno_size = coff_count_linenumbers (abfd) * bfd_coff_linesz (abfd);
3249 if (abfd->output_has_begun == false)
3251 if (! coff_compute_section_file_positions (abfd))
3252 return false;
3255 reloc_base = obj_relocbase (abfd);
3257 /* Work out the size of the reloc and linno areas */
3259 for (current = abfd->sections; current != NULL; current =
3260 current->next)
3262 #ifdef COFF_WITH_PE
3263 /* we store the actual reloc count in the first reloc's addr */
3264 if (current->reloc_count > 0xffff)
3265 reloc_count ++;
3266 #endif
3267 reloc_count += current->reloc_count;
3270 reloc_size = reloc_count * bfd_coff_relsz (abfd);
3272 lineno_base = reloc_base + reloc_size;
3273 sym_base = lineno_base + lnno_size;
3275 /* Indicate in each section->line_filepos its actual file address */
3276 for (current = abfd->sections; current != NULL; current =
3277 current->next)
3279 if (current->lineno_count)
3281 current->line_filepos = lineno_base;
3282 current->moving_line_filepos = lineno_base;
3283 lineno_base += current->lineno_count * bfd_coff_linesz (abfd);
3285 else
3287 current->line_filepos = 0;
3289 if (current->reloc_count)
3291 current->rel_filepos = reloc_base;
3292 reloc_base += current->reloc_count * bfd_coff_relsz (abfd);
3293 #ifdef COFF_WITH_PE
3294 /* extra reloc to hold real count */
3295 if (current->reloc_count > 0xffff)
3296 reloc_base += bfd_coff_relsz (abfd);
3297 #endif
3299 else
3301 current->rel_filepos = 0;
3305 /* Write section headers to the file. */
3306 internal_f.f_nscns = 0;
3308 if ((abfd->flags & EXEC_P) != 0)
3309 scn_base = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
3310 else
3312 scn_base = bfd_coff_filhsz (abfd);
3313 #ifdef RS6000COFF_C
3314 if (xcoff_data (abfd)->full_aouthdr)
3315 scn_base += bfd_coff_aoutsz (abfd);
3316 else
3317 scn_base += SMALL_AOUTSZ;
3318 #endif
3321 if (bfd_seek (abfd, scn_base, SEEK_SET) != 0)
3322 return false;
3324 long_section_names = false;
3325 for (current = abfd->sections;
3326 current != NULL;
3327 current = current->next)
3329 struct internal_scnhdr section;
3330 boolean is_reloc_section = false;
3332 #ifdef COFF_IMAGE_WITH_PE
3333 if (strcmp (current->name, ".reloc") == 0)
3335 is_reloc_section = true;
3336 hasrelocs = true;
3337 pe_data (abfd)->has_reloc_section = 1;
3339 #endif
3341 internal_f.f_nscns++;
3343 strncpy (section.s_name, current->name, SCNNMLEN);
3345 #ifdef COFF_LONG_SECTION_NAMES
3346 /* Handle long section names as in PE. This must be compatible
3347 with the code in coff_write_symbols and _bfd_coff_final_link. */
3349 size_t len;
3351 len = strlen (current->name);
3352 if (len > SCNNMLEN)
3354 memset (section.s_name, 0, SCNNMLEN);
3355 sprintf (section.s_name, "/%lu", (unsigned long) string_size);
3356 string_size += len + 1;
3357 long_section_names = true;
3360 #endif
3362 #ifdef _LIB
3363 /* Always set s_vaddr of .lib to 0. This is right for SVR3.2
3364 Ian Taylor <ian@cygnus.com>. */
3365 if (strcmp (current->name, _LIB) == 0)
3366 section.s_vaddr = 0;
3367 else
3368 #endif
3369 section.s_vaddr = current->vma;
3370 section.s_paddr = current->lma;
3371 section.s_size = current->_raw_size;
3372 #ifdef coff_get_section_load_page
3373 section.s_page = coff_get_section_load_page (current);
3374 #endif
3376 #ifdef COFF_WITH_PE
3377 section.s_paddr = 0;
3378 #endif
3379 #ifdef COFF_IMAGE_WITH_PE
3380 /* Reminder: s_paddr holds the virtual size of the section. */
3381 if (coff_section_data (abfd, current) != NULL
3382 && pei_section_data (abfd, current) != NULL)
3383 section.s_paddr = pei_section_data (abfd, current)->virt_size;
3384 else
3385 section.s_paddr = 0;
3386 #endif
3389 If this section has no size or is unloadable then the scnptr
3390 will be 0 too
3392 if (current->_raw_size == 0 ||
3393 (current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3395 section.s_scnptr = 0;
3397 else
3399 section.s_scnptr = current->filepos;
3401 section.s_relptr = current->rel_filepos;
3402 section.s_lnnoptr = current->line_filepos;
3403 section.s_nreloc = current->reloc_count;
3404 section.s_nlnno = current->lineno_count;
3405 #ifndef COFF_IMAGE_WITH_PE
3406 /* In PEI, relocs come in the .reloc section. */
3407 if (current->reloc_count != 0)
3408 hasrelocs = true;
3409 #endif
3410 if (current->lineno_count != 0)
3411 haslinno = true;
3412 if ((current->flags & SEC_DEBUGGING) != 0
3413 && ! is_reloc_section)
3414 hasdebug = true;
3416 #ifdef RS6000COFF_C
3417 #ifndef XCOFF64
3418 /* Indicate the use of an XCOFF overflow section header. */
3419 if (current->reloc_count >= 0xffff || current->lineno_count >= 0xffff)
3421 section.s_nreloc = 0xffff;
3422 section.s_nlnno = 0xffff;
3424 #endif
3425 #endif
3427 section.s_flags = sec_to_styp_flags (current->name, current->flags);
3429 if (!strcmp (current->name, _TEXT))
3431 text_sec = current;
3433 else if (!strcmp (current->name, _DATA))
3435 data_sec = current;
3437 else if (!strcmp (current->name, _BSS))
3439 bss_sec = current;
3442 #ifdef I960
3443 section.s_align = (current->alignment_power
3444 ? 1 << current->alignment_power
3445 : 0);
3446 #endif
3447 #ifdef TIC80COFF
3448 /* TI COFF puts the alignment power in bits 8-11 of the flags */
3449 section.s_flags |= (current->alignment_power & 0xF) << 8;
3450 #endif
3451 #ifdef COFF_ENCODE_ALIGNMENT
3452 COFF_ENCODE_ALIGNMENT(section, current->alignment_power);
3453 #endif
3455 #ifdef COFF_IMAGE_WITH_PE
3456 /* Suppress output of the sections if they are null. ld
3457 includes the bss and data sections even if there is no size
3458 assigned to them. NT loader doesn't like it if these section
3459 headers are included if the sections themselves are not
3460 needed. See also coff_compute_section_file_positions. */
3461 if (section.s_size == 0)
3462 internal_f.f_nscns--;
3463 else
3464 #endif
3466 SCNHDR buff;
3467 if (coff_swap_scnhdr_out (abfd, &section, &buff) == 0
3468 || bfd_write ((PTR) (&buff), 1, bfd_coff_scnhsz (abfd), abfd)
3469 != bfd_coff_scnhsz (abfd))
3470 return false;
3473 #ifdef COFF_WITH_PE
3474 /* PE stores COMDAT section information in the symbol table. If
3475 this section is supposed to have some COMDAT info, track down
3476 the symbol in the symbol table and modify it. */
3477 if ((current->flags & SEC_LINK_ONCE) != 0)
3479 unsigned int i, count;
3480 asymbol **psym;
3481 coff_symbol_type *csym = NULL;
3482 asymbol **psymsec;
3484 psymsec = NULL;
3485 count = bfd_get_symcount (abfd);
3486 for (i = 0, psym = abfd->outsymbols; i < count; i++, psym++)
3488 if ((*psym)->section != current)
3489 continue;
3491 /* Remember the location of the first symbol in this
3492 section. */
3493 if (psymsec == NULL)
3494 psymsec = psym;
3496 /* See if this is the section symbol. */
3497 if (strcmp ((*psym)->name, current->name) == 0)
3499 csym = coff_symbol_from (abfd, *psym);
3500 if (csym == NULL
3501 || csym->native == NULL
3502 || csym->native->u.syment.n_numaux < 1
3503 || csym->native->u.syment.n_sclass != C_STAT
3504 || csym->native->u.syment.n_type != T_NULL)
3505 continue;
3507 /* Here *PSYM is the section symbol for CURRENT. */
3509 break;
3513 /* Did we find it?
3514 Note that we might not if we're converting the file from
3515 some other object file format. */
3516 if (i < count)
3518 combined_entry_type *aux;
3520 /* We don't touch the x_checksum field. The
3521 x_associated field is not currently supported. */
3523 aux = csym->native + 1;
3524 switch (current->flags & SEC_LINK_DUPLICATES)
3526 case SEC_LINK_DUPLICATES_DISCARD:
3527 aux->u.auxent.x_scn.x_comdat = IMAGE_COMDAT_SELECT_ANY;
3528 break;
3530 case SEC_LINK_DUPLICATES_ONE_ONLY:
3531 aux->u.auxent.x_scn.x_comdat =
3532 IMAGE_COMDAT_SELECT_NODUPLICATES;
3533 break;
3535 case SEC_LINK_DUPLICATES_SAME_SIZE:
3536 aux->u.auxent.x_scn.x_comdat =
3537 IMAGE_COMDAT_SELECT_SAME_SIZE;
3538 break;
3540 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
3541 aux->u.auxent.x_scn.x_comdat =
3542 IMAGE_COMDAT_SELECT_EXACT_MATCH;
3543 break;
3546 /* The COMDAT symbol must be the first symbol from this
3547 section in the symbol table. In order to make this
3548 work, we move the COMDAT symbol before the first
3549 symbol we found in the search above. It's OK to
3550 rearrange the symbol table at this point, because
3551 coff_renumber_symbols is going to rearrange it
3552 further and fix up all the aux entries. */
3553 if (psym != psymsec)
3555 asymbol *hold;
3556 asymbol **pcopy;
3558 hold = *psym;
3559 for (pcopy = psym; pcopy > psymsec; pcopy--)
3560 pcopy[0] = pcopy[-1];
3561 *psymsec = hold;
3565 #endif /* COFF_WITH_PE */
3568 #ifdef RS6000COFF_C
3569 /* XCOFF handles overflows in the reloc and line number count fields
3570 by creating a new section header to hold the correct values. */
3571 for (current = abfd->sections; current != NULL; current = current->next)
3573 if (current->reloc_count >= 0xffff || current->lineno_count >= 0xffff)
3575 struct internal_scnhdr scnhdr;
3576 SCNHDR buff;
3578 internal_f.f_nscns++;
3579 strncpy (&(scnhdr.s_name[0]), current->name, 8);
3580 scnhdr.s_paddr = current->reloc_count;
3581 scnhdr.s_vaddr = current->lineno_count;
3582 scnhdr.s_size = 0;
3583 scnhdr.s_scnptr = 0;
3584 scnhdr.s_relptr = current->rel_filepos;
3585 scnhdr.s_lnnoptr = current->line_filepos;
3586 scnhdr.s_nreloc = current->target_index;
3587 scnhdr.s_nlnno = current->target_index;
3588 scnhdr.s_flags = STYP_OVRFLO;
3589 if (coff_swap_scnhdr_out (abfd, &scnhdr, &buff) == 0
3590 || bfd_write ((PTR) &buff, 1, bfd_coff_scnhsz (abfd), abfd)
3591 != bfd_coff_scnhsz (abfd))
3592 return false;
3595 #endif
3597 /* OK, now set up the filehdr... */
3599 /* Don't include the internal abs section in the section count */
3602 We will NOT put a fucking timestamp in the header here. Every time you
3603 put it back, I will come in and take it out again. I'm sorry. This
3604 field does not belong here. We fill it with a 0 so it compares the
3605 same but is not a reasonable time. -- gnu@cygnus.com
3607 internal_f.f_timdat = 0;
3609 internal_f.f_flags = 0;
3611 if (abfd->flags & EXEC_P)
3612 internal_f.f_opthdr = bfd_coff_aoutsz (abfd);
3613 else
3615 internal_f.f_opthdr = 0;
3616 #ifdef RS6000COFF_C
3617 if (xcoff_data (abfd)->full_aouthdr)
3618 internal_f.f_opthdr = bfd_coff_aoutsz (abfd);
3619 else
3620 internal_f.f_opthdr = SMALL_AOUTSZ;
3621 #endif
3624 if (!hasrelocs)
3625 internal_f.f_flags |= F_RELFLG;
3626 if (!haslinno)
3627 internal_f.f_flags |= F_LNNO;
3628 if (abfd->flags & EXEC_P)
3629 internal_f.f_flags |= F_EXEC;
3630 #ifdef COFF_IMAGE_WITH_PE
3631 if (! hasdebug)
3632 internal_f.f_flags |= IMAGE_FILE_DEBUG_STRIPPED;
3633 #endif
3635 #ifndef COFF_WITH_PE
3636 if (bfd_little_endian (abfd))
3637 internal_f.f_flags |= F_AR32WR;
3638 else
3639 internal_f.f_flags |= F_AR32W;
3640 #endif
3642 #ifdef TI_TARGET_ID
3643 /* target id is used in TI COFF v1 and later; COFF0 won't use this field,
3644 but it doesn't hurt to set it internally */
3645 internal_f.f_target_id = TI_TARGET_ID;
3646 #endif
3647 #ifdef TIC80_TARGET_ID
3648 internal_f.f_target_id = TIC80_TARGET_ID;
3649 #endif
3652 FIXME, should do something about the other byte orders and
3653 architectures.
3656 #ifdef RS6000COFF_C
3657 if ((abfd->flags & DYNAMIC) != 0)
3658 internal_f.f_flags |= F_SHROBJ;
3659 if (bfd_get_section_by_name (abfd, _LOADER) != NULL)
3660 internal_f.f_flags |= F_DYNLOAD;
3661 #endif
3663 memset (&internal_a, 0, sizeof internal_a);
3665 /* Set up architecture-dependent stuff */
3668 unsigned int magic = 0;
3669 unsigned short flags = 0;
3670 coff_set_flags (abfd, &magic, &flags);
3671 internal_f.f_magic = magic;
3672 internal_f.f_flags |= flags;
3673 /* ...and the "opt"hdr... */
3675 #ifdef A29K
3676 #ifdef ULTRA3 /* NYU's machine */
3677 /* FIXME: This is a bogus check. I really want to see if there
3678 * is a .shbss or a .shdata section, if so then set the magic
3679 * number to indicate a shared data executable.
3681 if (internal_f.f_nscns >= 7)
3682 internal_a.magic = SHMAGIC; /* Shared magic */
3683 else
3684 #endif /* ULTRA3 */
3685 internal_a.magic = NMAGIC; /* Assume separate i/d */
3686 #define __A_MAGIC_SET__
3687 #endif /* A29K */
3688 #ifdef TICOFF_AOUT_MAGIC
3689 internal_a.magic = TICOFF_AOUT_MAGIC;
3690 #define __A_MAGIC_SET__
3691 #endif
3692 #ifdef TIC80COFF
3693 internal_a.magic = TIC80_ARCH_MAGIC;
3694 #define __A_MAGIC_SET__
3695 #endif /* TIC80 */
3696 #ifdef I860
3697 /* FIXME: What are the a.out magic numbers for the i860? */
3698 internal_a.magic = 0;
3699 #define __A_MAGIC_SET__
3700 #endif /* I860 */
3701 #ifdef I960
3702 internal_a.magic = (magic == I960ROMAGIC ? NMAGIC : OMAGIC);
3703 #define __A_MAGIC_SET__
3704 #endif /* I960 */
3705 #if M88
3706 #define __A_MAGIC_SET__
3707 internal_a.magic = PAGEMAGICBCS;
3708 #endif /* M88 */
3710 #if APOLLO_M68
3711 #define __A_MAGIC_SET__
3712 internal_a.magic = APOLLO_COFF_VERSION_NUMBER;
3713 #endif
3715 #if defined(M68) || defined(WE32K) || defined(M68K)
3716 #define __A_MAGIC_SET__
3717 #if defined(LYNXOS)
3718 internal_a.magic = LYNXCOFFMAGIC;
3719 #else
3720 #if defined(TARG_AUX)
3721 internal_a.magic = (abfd->flags & D_PAGED ? PAGEMAGICPEXECPAGED :
3722 abfd->flags & WP_TEXT ? PAGEMAGICPEXECSWAPPED :
3723 PAGEMAGICEXECSWAPPED);
3724 #else
3725 #if defined (PAGEMAGICPEXECPAGED)
3726 internal_a.magic = PAGEMAGICPEXECPAGED;
3727 #endif
3728 #endif /* TARG_AUX */
3729 #endif /* LYNXOS */
3730 #endif /* M68 || WE32K || M68K */
3732 #if defined(ARM)
3733 #define __A_MAGIC_SET__
3734 internal_a.magic = ZMAGIC;
3735 #endif
3737 #if defined(PPC_PE)
3738 #define __A_MAGIC_SET__
3739 internal_a.magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
3740 #endif
3742 #if defined MCORE_PE
3743 #define __A_MAGIC_SET__
3744 internal_a.magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
3745 #endif
3747 #if defined(I386)
3748 #define __A_MAGIC_SET__
3749 #if defined(LYNXOS)
3750 internal_a.magic = LYNXCOFFMAGIC;
3751 #else /* LYNXOS */
3752 internal_a.magic = ZMAGIC;
3753 #endif /* LYNXOS */
3754 #endif /* I386 */
3756 #if defined(IA64)
3757 #define __A_MAGIC_SET__
3758 internal_a.magic = ZMAGIC;
3759 #endif /* IA64 */
3761 #if defined(SPARC)
3762 #define __A_MAGIC_SET__
3763 #if defined(LYNXOS)
3764 internal_a.magic = LYNXCOFFMAGIC;
3765 #endif /* LYNXOS */
3766 #endif /* SPARC */
3768 #ifdef RS6000COFF_C
3769 #define __A_MAGIC_SET__
3770 internal_a.magic = (abfd->flags & D_PAGED) ? RS6K_AOUTHDR_ZMAGIC :
3771 (abfd->flags & WP_TEXT) ? RS6K_AOUTHDR_NMAGIC :
3772 RS6K_AOUTHDR_OMAGIC;
3773 #endif
3775 #if defined(SH) && defined(COFF_WITH_PE)
3776 #define __A_MAGIC_SET__
3777 internal_a.magic = SH_PE_MAGIC;
3778 #endif
3780 #if defined(MIPS) && defined(COFF_WITH_PE)
3781 #define __A_MAGIC_SET__
3782 internal_a.magic = MIPS_PE_MAGIC;
3783 #endif
3785 #ifndef __A_MAGIC_SET__
3786 #include "Your aouthdr magic number is not being set!"
3787 #else
3788 #undef __A_MAGIC_SET__
3789 #endif
3792 /* FIXME: Does anybody ever set this to another value? */
3793 internal_a.vstamp = 0;
3795 /* Now should write relocs, strings, syms */
3796 obj_sym_filepos (abfd) = sym_base;
3798 if (bfd_get_symcount (abfd) != 0)
3800 int firstundef;
3801 #if 0
3802 if (!coff_add_missing_symbols (abfd))
3803 return false;
3804 #endif
3805 if (!coff_renumber_symbols (abfd, &firstundef))
3806 return false;
3807 coff_mangle_symbols (abfd);
3808 if (! coff_write_symbols (abfd))
3809 return false;
3810 if (! coff_write_linenumbers (abfd))
3811 return false;
3812 if (! coff_write_relocs (abfd, firstundef))
3813 return false;
3815 #ifdef COFF_LONG_SECTION_NAMES
3816 else if (long_section_names && ! obj_coff_strings_written (abfd))
3818 /* If we have long section names we have to write out the string
3819 table even if there are no symbols. */
3820 if (! coff_write_symbols (abfd))
3821 return false;
3823 #endif
3824 #ifdef COFF_IMAGE_WITH_PE
3825 #ifdef PPC_PE
3826 else if ((abfd->flags & EXEC_P) != 0)
3828 bfd_byte b;
3830 /* PowerPC PE appears to require that all executable files be
3831 rounded up to the page size. */
3832 b = 0;
3833 if (bfd_seek (abfd,
3834 BFD_ALIGN (sym_base, COFF_PAGE_SIZE) - 1,
3835 SEEK_SET) != 0
3836 || bfd_write (&b, 1, 1, abfd) != 1)
3837 return false;
3839 #endif
3840 #endif
3842 /* If bfd_get_symcount (abfd) != 0, then we are not using the COFF
3843 backend linker, and obj_raw_syment_count is not valid until after
3844 coff_write_symbols is called. */
3845 if (obj_raw_syment_count (abfd) != 0)
3847 internal_f.f_symptr = sym_base;
3848 #ifdef RS6000COFF_C
3849 /* AIX appears to require that F_RELFLG not be set if there are
3850 local symbols but no relocations. */
3851 internal_f.f_flags &=~ F_RELFLG;
3852 #endif
3854 else
3856 if (long_section_names)
3857 internal_f.f_symptr = sym_base;
3858 else
3859 internal_f.f_symptr = 0;
3860 internal_f.f_flags |= F_LSYMS;
3863 if (text_sec)
3865 internal_a.tsize = bfd_get_section_size_before_reloc (text_sec);
3866 internal_a.text_start = internal_a.tsize ? text_sec->vma : 0;
3868 if (data_sec)
3870 internal_a.dsize = bfd_get_section_size_before_reloc (data_sec);
3871 internal_a.data_start = internal_a.dsize ? data_sec->vma : 0;
3873 if (bss_sec)
3875 internal_a.bsize = bfd_get_section_size_before_reloc (bss_sec);
3876 if (internal_a.bsize && bss_sec->vma < internal_a.data_start)
3877 internal_a.data_start = bss_sec->vma;
3880 internal_a.entry = bfd_get_start_address (abfd);
3881 internal_f.f_nsyms = obj_raw_syment_count (abfd);
3883 #ifdef RS6000COFF_C
3884 if (xcoff_data (abfd)->full_aouthdr)
3886 bfd_vma toc;
3887 asection *loader_sec;
3889 internal_a.vstamp = 1;
3891 internal_a.o_snentry = xcoff_data (abfd)->snentry;
3892 if (internal_a.o_snentry == 0)
3893 internal_a.entry = (bfd_vma) -1;
3895 if (text_sec != NULL)
3897 internal_a.o_sntext = text_sec->target_index;
3898 internal_a.o_algntext = bfd_get_section_alignment (abfd, text_sec);
3900 else
3902 internal_a.o_sntext = 0;
3903 internal_a.o_algntext = 0;
3905 if (data_sec != NULL)
3907 internal_a.o_sndata = data_sec->target_index;
3908 internal_a.o_algndata = bfd_get_section_alignment (abfd, data_sec);
3910 else
3912 internal_a.o_sndata = 0;
3913 internal_a.o_algndata = 0;
3915 loader_sec = bfd_get_section_by_name (abfd, ".loader");
3916 if (loader_sec != NULL)
3917 internal_a.o_snloader = loader_sec->target_index;
3918 else
3919 internal_a.o_snloader = 0;
3920 if (bss_sec != NULL)
3921 internal_a.o_snbss = bss_sec->target_index;
3922 else
3923 internal_a.o_snbss = 0;
3925 toc = xcoff_data (abfd)->toc;
3926 internal_a.o_toc = toc;
3927 internal_a.o_sntoc = xcoff_data (abfd)->sntoc;
3929 internal_a.o_modtype = xcoff_data (abfd)->modtype;
3930 if (xcoff_data (abfd)->cputype != -1)
3931 internal_a.o_cputype = xcoff_data (abfd)->cputype;
3932 else
3934 switch (bfd_get_arch (abfd))
3936 case bfd_arch_rs6000:
3937 internal_a.o_cputype = 4;
3938 break;
3939 case bfd_arch_powerpc:
3940 if (bfd_get_mach (abfd) == 0)
3941 internal_a.o_cputype = 3;
3942 else
3943 internal_a.o_cputype = 1;
3944 break;
3945 default:
3946 abort ();
3949 internal_a.o_maxstack = xcoff_data (abfd)->maxstack;
3950 internal_a.o_maxdata = xcoff_data (abfd)->maxdata;
3952 #endif
3954 /* now write them */
3955 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
3956 return false;
3959 char * buff;
3960 bfd_size_type amount;
3962 buff = bfd_malloc (bfd_coff_filhsz (abfd));
3963 if (buff == NULL)
3964 return false;
3966 coff_swap_filehdr_out (abfd, (PTR) & internal_f, (PTR) buff);
3967 amount = bfd_write ((PTR) buff, 1, bfd_coff_filhsz (abfd), abfd);
3969 free (buff);
3971 if (amount != bfd_coff_filhsz (abfd))
3972 return false;
3975 if (abfd->flags & EXEC_P)
3977 /* Note that peicode.h fills in a PEAOUTHDR, not an AOUTHDR.
3978 include/coff/pe.h sets AOUTSZ == sizeof (PEAOUTHDR)) */
3979 char * buff;
3980 bfd_size_type amount;
3982 buff = bfd_malloc (bfd_coff_aoutsz (abfd));
3983 if (buff == NULL)
3984 return false;
3986 coff_swap_aouthdr_out (abfd, (PTR) & internal_a, (PTR) buff);
3987 amount = bfd_write ((PTR) buff, 1, bfd_coff_aoutsz (abfd), abfd);
3989 free (buff);
3991 if (amount != bfd_coff_aoutsz (abfd))
3992 return false;
3994 #ifdef RS6000COFF_C
3995 else
3997 AOUTHDR buff;
3998 size_t size;
4000 /* XCOFF seems to always write at least a small a.out header. */
4001 coff_swap_aouthdr_out (abfd, (PTR) &internal_a, (PTR) &buff);
4002 if (xcoff_data (abfd)->full_aouthdr)
4003 size = bfd_coff_aoutsz (abfd);
4004 else
4005 size = SMALL_AOUTSZ;
4006 if (bfd_write ((PTR) &buff, 1, size, abfd) != size)
4007 return false;
4009 #endif
4011 return true;
4014 static boolean
4015 coff_set_section_contents (abfd, section, location, offset, count)
4016 bfd * abfd;
4017 sec_ptr section;
4018 PTR location;
4019 file_ptr offset;
4020 bfd_size_type count;
4022 if (abfd->output_has_begun == false) /* set by bfd.c handler */
4024 if (! coff_compute_section_file_positions (abfd))
4025 return false;
4028 #if defined(_LIB) && !defined(TARG_AUX)
4030 /* The physical address field of a .lib section is used to hold the
4031 number of shared libraries in the section. This code counts the
4032 number of sections being written, and increments the lma field
4033 with the number.
4035 I have found no documentation on the contents of this section.
4036 Experimentation indicates that the section contains zero or more
4037 records, each of which has the following structure:
4039 - a (four byte) word holding the length of this record, in words,
4040 - a word that always seems to be set to "2",
4041 - the path to a shared library, null-terminated and then padded
4042 to a whole word boundary.
4044 bfd_assert calls have been added to alert if an attempt is made
4045 to write a section which doesn't follow these assumptions. The
4046 code has been tested on ISC 4.1 by me, and on SCO by Robert Lipe
4047 <robertl@arnet.com> (Thanks!).
4049 Gvran Uddeborg <gvran@uddeborg.pp.se> */
4051 if (strcmp (section->name, _LIB) == 0)
4053 bfd_byte *rec, *recend;
4055 rec = (bfd_byte *) location;
4056 recend = rec + count;
4057 while (rec < recend)
4059 ++section->lma;
4060 rec += bfd_get_32 (abfd, rec) * 4;
4063 BFD_ASSERT (rec == recend);
4066 #endif
4068 /* Don't write out bss sections - one way to do this is to
4069 see if the filepos has not been set. */
4070 if (section->filepos == 0)
4071 return true;
4073 if (bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET) != 0)
4074 return false;
4076 if (count != 0)
4078 return (bfd_write (location, 1, count, abfd) == count) ? true : false;
4080 return true;
4082 #if 0
4083 static boolean
4084 coff_close_and_cleanup (abfd)
4085 bfd *abfd;
4087 if (!bfd_read_p (abfd))
4088 switch (abfd->format)
4090 case bfd_archive:
4091 if (!_bfd_write_archive_contents (abfd))
4092 return false;
4093 break;
4094 case bfd_object:
4095 if (!coff_write_object_contents (abfd))
4096 return false;
4097 break;
4098 default:
4099 bfd_set_error (bfd_error_invalid_operation);
4100 return false;
4103 /* We depend on bfd_close to free all the memory on the objalloc. */
4104 return true;
4107 #endif
4109 static PTR
4110 buy_and_read (abfd, where, seek_direction, size)
4111 bfd *abfd;
4112 file_ptr where;
4113 int seek_direction;
4114 size_t size;
4116 PTR area = (PTR) bfd_alloc (abfd, size);
4117 if (!area)
4118 return (NULL);
4119 if (bfd_seek (abfd, where, seek_direction) != 0
4120 || bfd_read (area, 1, size, abfd) != size)
4121 return (NULL);
4122 return (area);
4123 } /* buy_and_read() */
4126 SUBSUBSECTION
4127 Reading linenumbers
4129 Creating the linenumber table is done by reading in the entire
4130 coff linenumber table, and creating another table for internal use.
4132 A coff linenumber table is structured so that each function
4133 is marked as having a line number of 0. Each line within the
4134 function is an offset from the first line in the function. The
4135 base of the line number information for the table is stored in
4136 the symbol associated with the function.
4138 Note: The PE format uses line number 0 for a flag indicating a
4139 new source file.
4141 The information is copied from the external to the internal
4142 table, and each symbol which marks a function is marked by
4143 pointing its...
4145 How does this work ?
4149 static boolean
4150 coff_slurp_line_table (abfd, asect)
4151 bfd *abfd;
4152 asection *asect;
4154 LINENO *native_lineno;
4155 alent *lineno_cache;
4157 BFD_ASSERT (asect->lineno == (alent *) NULL);
4159 native_lineno = (LINENO *) buy_and_read (abfd,
4160 asect->line_filepos,
4161 SEEK_SET,
4162 (size_t) (bfd_coff_linesz (abfd) *
4163 asect->lineno_count));
4164 lineno_cache =
4165 (alent *) bfd_alloc (abfd, (size_t) ((asect->lineno_count + 1) * sizeof (alent)));
4166 if (lineno_cache == NULL)
4167 return false;
4168 else
4170 unsigned int counter = 0;
4171 alent *cache_ptr = lineno_cache;
4172 LINENO *src = native_lineno;
4174 while (counter < asect->lineno_count)
4176 struct internal_lineno dst;
4177 bfd_coff_swap_lineno_in (abfd, src, &dst);
4178 cache_ptr->line_number = dst.l_lnno;
4180 if (cache_ptr->line_number == 0)
4182 boolean warned;
4183 long symndx;
4184 coff_symbol_type *sym;
4186 warned = false;
4187 symndx = dst.l_addr.l_symndx;
4188 if (symndx < 0
4189 || (unsigned long) symndx >= obj_raw_syment_count (abfd))
4191 (*_bfd_error_handler)
4192 (_("%s: warning: illegal symbol index %ld in line numbers"),
4193 bfd_get_filename (abfd), dst.l_addr.l_symndx);
4194 symndx = 0;
4195 warned = true;
4197 /* FIXME: We should not be casting between ints and
4198 pointers like this. */
4199 sym = ((coff_symbol_type *)
4200 ((symndx + obj_raw_syments (abfd))
4201 ->u.syment._n._n_n._n_zeroes));
4202 cache_ptr->u.sym = (asymbol *) sym;
4203 if (sym->lineno != NULL && ! warned)
4205 (*_bfd_error_handler)
4206 (_("%s: warning: duplicate line number information for `%s'"),
4207 bfd_get_filename (abfd),
4208 bfd_asymbol_name (&sym->symbol));
4210 sym->lineno = cache_ptr;
4212 else
4214 cache_ptr->u.offset = dst.l_addr.l_paddr
4215 - bfd_section_vma (abfd, asect);
4216 } /* If no linenumber expect a symbol index */
4218 cache_ptr++;
4219 src++;
4220 counter++;
4222 cache_ptr->line_number = 0;
4225 asect->lineno = lineno_cache;
4226 /* FIXME, free native_lineno here, or use alloca or something. */
4227 return true;
4230 /* Slurp in the symbol table, converting it to generic form. Note
4231 that if coff_relocate_section is defined, the linker will read
4232 symbols via coff_link_add_symbols, rather than via this routine. */
4234 static boolean
4235 coff_slurp_symbol_table (abfd)
4236 bfd * abfd;
4238 combined_entry_type *native_symbols;
4239 coff_symbol_type *cached_area;
4240 unsigned int *table_ptr;
4242 unsigned int number_of_symbols = 0;
4244 if (obj_symbols (abfd))
4245 return true;
4247 /* Read in the symbol table */
4248 if ((native_symbols = coff_get_normalized_symtab (abfd)) == NULL)
4250 return (false);
4251 } /* on error */
4253 /* Allocate enough room for all the symbols in cached form */
4254 cached_area = ((coff_symbol_type *)
4255 bfd_alloc (abfd,
4256 (obj_raw_syment_count (abfd)
4257 * sizeof (coff_symbol_type))));
4259 if (cached_area == NULL)
4260 return false;
4261 table_ptr = ((unsigned int *)
4262 bfd_alloc (abfd,
4263 (obj_raw_syment_count (abfd)
4264 * sizeof (unsigned int))));
4266 if (table_ptr == NULL)
4267 return false;
4268 else
4270 coff_symbol_type *dst = cached_area;
4271 unsigned int last_native_index = obj_raw_syment_count (abfd);
4272 unsigned int this_index = 0;
4273 while (this_index < last_native_index)
4275 combined_entry_type *src = native_symbols + this_index;
4276 table_ptr[this_index] = number_of_symbols;
4277 dst->symbol.the_bfd = abfd;
4279 dst->symbol.name = (char *) (src->u.syment._n._n_n._n_offset);
4280 /* We use the native name field to point to the cached field. */
4281 src->u.syment._n._n_n._n_zeroes = (long) dst;
4282 dst->symbol.section = coff_section_from_bfd_index (abfd,
4283 src->u.syment.n_scnum);
4284 dst->symbol.flags = 0;
4285 dst->done_lineno = false;
4287 switch (src->u.syment.n_sclass)
4289 #ifdef I960
4290 case C_LEAFEXT:
4291 #if 0
4292 dst->symbol.value = src->u.syment.n_value - dst->symbol.section->vma;
4293 dst->symbol.flags = BSF_EXPORT | BSF_GLOBAL;
4294 dst->symbol.flags |= BSF_NOT_AT_END | BSF_FUNCTION;
4295 #endif
4296 /* Fall through to next case */
4298 #endif
4300 case C_EXT:
4301 case C_WEAKEXT:
4302 #if defined ARM
4303 case C_THUMBEXT:
4304 case C_THUMBEXTFUNC:
4305 #endif
4306 #ifdef RS6000COFF_C
4307 case C_HIDEXT:
4308 #endif
4309 #ifdef C_SYSTEM
4310 case C_SYSTEM: /* System Wide variable */
4311 #endif
4312 #ifdef COFF_WITH_PE
4313 /* In PE, 0x68 (104) denotes a section symbol */
4314 case C_SECTION:
4315 /* In PE, 0x69 (105) denotes a weak external symbol. */
4316 case C_NT_WEAK:
4317 #endif
4318 switch (coff_classify_symbol (abfd, &src->u.syment))
4320 case COFF_SYMBOL_GLOBAL:
4321 dst->symbol.flags = BSF_EXPORT | BSF_GLOBAL;
4322 #if defined COFF_WITH_PE
4323 /* PE sets the symbol to a value relative to the
4324 start of the section. */
4325 dst->symbol.value = src->u.syment.n_value;
4326 #else
4327 dst->symbol.value = (src->u.syment.n_value
4328 - dst->symbol.section->vma);
4329 #endif
4330 if (ISFCN ((src->u.syment.n_type)))
4332 /* A function ext does not go at the end of a
4333 file. */
4334 dst->symbol.flags |= BSF_NOT_AT_END | BSF_FUNCTION;
4336 break;
4338 case COFF_SYMBOL_COMMON:
4339 dst->symbol.section = bfd_com_section_ptr;
4340 dst->symbol.value = src->u.syment.n_value;
4341 break;
4343 case COFF_SYMBOL_UNDEFINED:
4344 dst->symbol.section = bfd_und_section_ptr;
4345 dst->symbol.value = 0;
4346 break;
4348 case COFF_SYMBOL_PE_SECTION:
4349 dst->symbol.flags |= BSF_EXPORT | BSF_SECTION_SYM;
4350 dst->symbol.value = 0;
4351 break;
4353 case COFF_SYMBOL_LOCAL:
4354 dst->symbol.flags = BSF_LOCAL;
4355 #if defined COFF_WITH_PE
4356 /* PE sets the symbol to a value relative to the
4357 start of the section. */
4358 dst->symbol.value = src->u.syment.n_value;
4359 #else
4360 dst->symbol.value = (src->u.syment.n_value
4361 - dst->symbol.section->vma);
4362 #endif
4363 if (ISFCN ((src->u.syment.n_type)))
4364 dst->symbol.flags |= BSF_NOT_AT_END | BSF_FUNCTION;
4365 break;
4368 #ifdef RS6000COFF_C
4369 /* A symbol with a csect entry should not go at the end. */
4370 if (src->u.syment.n_numaux > 0)
4371 dst->symbol.flags |= BSF_NOT_AT_END;
4372 #endif
4374 #ifdef COFF_WITH_PE
4375 if (src->u.syment.n_sclass == C_NT_WEAK)
4376 dst->symbol.flags = BSF_WEAK;
4377 if (src->u.syment.n_sclass == C_SECTION
4378 && src->u.syment.n_scnum > 0)
4380 dst->symbol.flags = BSF_LOCAL;
4382 #endif
4384 if (src->u.syment.n_sclass == C_WEAKEXT)
4385 dst->symbol.flags = BSF_WEAK;
4387 break;
4389 case C_STAT: /* static */
4390 #ifdef I960
4391 case C_LEAFSTAT: /* static leaf procedure */
4392 #endif
4393 #if defined ARM
4394 case C_THUMBSTAT: /* Thumb static */
4395 case C_THUMBLABEL: /* Thumb label */
4396 case C_THUMBSTATFUNC:/* Thumb static function */
4397 #endif
4398 case C_LABEL: /* label */
4399 if (src->u.syment.n_scnum == N_DEBUG)
4400 dst->symbol.flags = BSF_DEBUGGING;
4401 else
4402 dst->symbol.flags = BSF_LOCAL;
4404 /* Base the value as an index from the base of the
4405 section, if there is one. */
4406 if (dst->symbol.section)
4408 #if defined COFF_WITH_PE
4409 /* PE sets the symbol to a value relative to the
4410 start of the section. */
4411 dst->symbol.value = src->u.syment.n_value;
4412 #else
4413 dst->symbol.value = (src->u.syment.n_value
4414 - dst->symbol.section->vma);
4415 #endif
4417 else
4418 dst->symbol.value = src->u.syment.n_value;
4419 break;
4421 case C_MOS: /* member of structure */
4422 case C_EOS: /* end of structure */
4423 #ifdef NOTDEF /* C_AUTOARG has the same value */
4424 #ifdef C_GLBLREG
4425 case C_GLBLREG: /* A29k-specific storage class */
4426 #endif
4427 #endif
4428 case C_REGPARM: /* register parameter */
4429 case C_REG: /* register variable */
4430 /* C_AUTOARG conflictes with TI COFF C_UEXT */
4431 #if !defined (TIC80COFF) && !defined (TICOFF)
4432 #ifdef C_AUTOARG
4433 case C_AUTOARG: /* 960-specific storage class */
4434 #endif
4435 #endif
4436 case C_TPDEF: /* type definition */
4437 case C_ARG:
4438 case C_AUTO: /* automatic variable */
4439 case C_FIELD: /* bit field */
4440 case C_ENTAG: /* enumeration tag */
4441 case C_MOE: /* member of enumeration */
4442 case C_MOU: /* member of union */
4443 case C_UNTAG: /* union tag */
4444 dst->symbol.flags = BSF_DEBUGGING;
4445 dst->symbol.value = (src->u.syment.n_value);
4446 break;
4448 case C_FILE: /* file name */
4449 case C_STRTAG: /* structure tag */
4450 #ifdef RS6000COFF_C
4451 case C_GSYM:
4452 case C_LSYM:
4453 case C_PSYM:
4454 case C_RSYM:
4455 case C_RPSYM:
4456 case C_STSYM:
4457 case C_BCOMM:
4458 case C_ECOMM:
4459 case C_DECL:
4460 case C_ENTRY:
4461 case C_FUN:
4462 case C_ESTAT:
4463 #endif
4464 dst->symbol.flags = BSF_DEBUGGING;
4465 dst->symbol.value = (src->u.syment.n_value);
4466 break;
4468 #ifdef RS6000COFF_C
4469 case C_BINCL: /* beginning of include file */
4470 case C_EINCL: /* ending of include file */
4471 /* The value is actually a pointer into the line numbers
4472 of the file. We locate the line number entry, and
4473 set the section to the section which contains it, and
4474 the value to the index in that section. */
4476 asection *sec;
4478 dst->symbol.flags = BSF_DEBUGGING;
4479 for (sec = abfd->sections; sec != NULL; sec = sec->next)
4480 if (sec->line_filepos <= (file_ptr) src->u.syment.n_value
4481 && ((file_ptr) (sec->line_filepos
4482 + sec->lineno_count * bfd_coff_linesz (abfd))
4483 > (file_ptr) src->u.syment.n_value))
4484 break;
4485 if (sec == NULL)
4486 dst->symbol.value = 0;
4487 else
4489 dst->symbol.section = sec;
4490 dst->symbol.value = ((src->u.syment.n_value
4491 - sec->line_filepos)
4492 / bfd_coff_linesz (abfd));
4493 src->fix_line = 1;
4496 break;
4498 case C_BSTAT:
4499 dst->symbol.flags = BSF_DEBUGGING;
4501 /* The value is actually a symbol index. Save a pointer
4502 to the symbol instead of the index. FIXME: This
4503 should use a union. */
4504 src->u.syment.n_value =
4505 (long) (native_symbols + src->u.syment.n_value);
4506 dst->symbol.value = src->u.syment.n_value;
4507 src->fix_value = 1;
4508 break;
4509 #endif
4511 case C_BLOCK: /* ".bb" or ".eb" */
4512 case C_FCN: /* ".bf" or ".ef" (or PE ".lf") */
4513 case C_EFCN: /* physical end of function */
4514 #if defined COFF_WITH_PE
4515 /* PE sets the symbol to a value relative to the start
4516 of the section. */
4517 dst->symbol.value = src->u.syment.n_value;
4518 if (strcmp (dst->symbol.name, ".bf") != 0)
4520 /* PE uses funny values for .ef and .lf; don't
4521 relocate them. */
4522 dst->symbol.flags = BSF_DEBUGGING;
4524 else
4525 dst->symbol.flags = BSF_DEBUGGING | BSF_DEBUGGING_RELOC;
4526 #else
4527 /* Base the value as an index from the base of the
4528 section. */
4529 dst->symbol.flags = BSF_LOCAL;
4530 dst->symbol.value = (src->u.syment.n_value
4531 - dst->symbol.section->vma);
4532 #endif
4533 break;
4535 case C_STATLAB: /* Static load time label */
4536 dst->symbol.value = src->u.syment.n_value;
4537 dst->symbol.flags = BSF_GLOBAL;
4538 break;
4540 case C_NULL:
4541 /* PE DLLs sometimes have zeroed out symbols for some
4542 reason. Just ignore them without a warning. */
4543 if (src->u.syment.n_type == 0
4544 && src->u.syment.n_value == 0
4545 && src->u.syment.n_scnum == 0)
4546 break;
4547 /* Fall through. */
4548 case C_EXTDEF: /* external definition */
4549 case C_ULABEL: /* undefined label */
4550 case C_USTATIC: /* undefined static */
4551 #ifndef COFF_WITH_PE
4552 /* C_LINE in regular coff is 0x68. NT has taken over this storage
4553 class to represent a section symbol */
4554 case C_LINE: /* line # reformatted as symbol table entry */
4555 /* NT uses 0x67 for a weak symbol, not C_ALIAS. */
4556 case C_ALIAS: /* duplicate tag */
4557 #endif
4558 /* New storage classes for TI COFF */
4559 #if defined(TIC80COFF) || defined(TICOFF)
4560 case C_UEXT: /* Tentative external definition */
4561 #endif
4562 case C_EXTLAB: /* External load time label */
4563 case C_HIDDEN: /* ext symbol in dmert public lib */
4564 default:
4565 (*_bfd_error_handler)
4566 (_("%s: Unrecognized storage class %d for %s symbol `%s'"),
4567 bfd_get_filename (abfd), src->u.syment.n_sclass,
4568 dst->symbol.section->name, dst->symbol.name);
4569 dst->symbol.flags = BSF_DEBUGGING;
4570 dst->symbol.value = (src->u.syment.n_value);
4571 break;
4574 /* BFD_ASSERT(dst->symbol.flags != 0);*/
4576 dst->native = src;
4578 dst->symbol.udata.i = 0;
4579 dst->lineno = (alent *) NULL;
4580 this_index += (src->u.syment.n_numaux) + 1;
4581 dst++;
4582 number_of_symbols++;
4583 } /* walk the native symtab */
4584 } /* bfdize the native symtab */
4586 obj_symbols (abfd) = cached_area;
4587 obj_raw_syments (abfd) = native_symbols;
4589 bfd_get_symcount (abfd) = number_of_symbols;
4590 obj_convert (abfd) = table_ptr;
4591 /* Slurp the line tables for each section too */
4593 asection *p;
4594 p = abfd->sections;
4595 while (p)
4597 coff_slurp_line_table (abfd, p);
4598 p = p->next;
4601 return true;
4602 } /* coff_slurp_symbol_table() */
4604 /* Classify a COFF symbol. A couple of targets have globally visible
4605 symbols which are not class C_EXT, and this handles those. It also
4606 recognizes some special PE cases. */
4608 static enum coff_symbol_classification
4609 coff_classify_symbol (abfd, syment)
4610 bfd *abfd;
4611 struct internal_syment *syment;
4613 /* FIXME: This partially duplicates the switch in
4614 coff_slurp_symbol_table. */
4615 switch (syment->n_sclass)
4617 case C_EXT:
4618 case C_WEAKEXT:
4619 #ifdef I960
4620 case C_LEAFEXT:
4621 #endif
4622 #ifdef ARM
4623 case C_THUMBEXT:
4624 case C_THUMBEXTFUNC:
4625 #endif
4626 #ifdef C_SYSTEM
4627 case C_SYSTEM:
4628 #endif
4629 #ifdef COFF_WITH_PE
4630 case C_NT_WEAK:
4631 #endif
4632 if (syment->n_scnum == 0)
4634 if (syment->n_value == 0)
4635 return COFF_SYMBOL_UNDEFINED;
4636 else
4637 return COFF_SYMBOL_COMMON;
4639 return COFF_SYMBOL_GLOBAL;
4641 default:
4642 break;
4645 #ifdef COFF_WITH_PE
4646 if (syment->n_sclass == C_STAT)
4648 if (syment->n_scnum == 0)
4650 /* The Microsoft compiler sometimes generates these if a
4651 small static function is inlined every time it is used.
4652 The function is discarded, but the symbol table entry
4653 remains. */
4654 return COFF_SYMBOL_LOCAL;
4657 #ifdef STRICT_PE_FORMAT
4658 /* This is correct for Microsoft generated objects, but it
4659 breaks gas generated objects. */
4661 if (syment->n_value == 0)
4663 asection *sec;
4664 char buf[SYMNMLEN + 1];
4666 sec = coff_section_from_bfd_index (abfd, syment->n_scnum);
4667 if (sec != NULL
4668 && (strcmp (bfd_get_section_name (abfd, sec),
4669 _bfd_coff_internal_syment_name (abfd, syment, buf))
4670 == 0))
4671 return COFF_SYMBOL_PE_SECTION;
4673 #endif
4675 return COFF_SYMBOL_LOCAL;
4678 if (syment->n_sclass == C_SECTION)
4680 /* In some cases in a DLL generated by the Microsoft linker, the
4681 n_value field will contain garbage. FIXME: This should
4682 probably be handled by the swapping function instead. */
4683 syment->n_value = 0;
4684 if (syment->n_scnum == 0)
4685 return COFF_SYMBOL_UNDEFINED;
4686 return COFF_SYMBOL_PE_SECTION;
4688 #endif /* COFF_WITH_PE */
4690 /* If it is not a global symbol, we presume it is a local symbol. */
4692 if (syment->n_scnum == 0)
4694 char buf[SYMNMLEN + 1];
4696 (*_bfd_error_handler)
4697 (_("warning: %s: local symbol `%s' has no section"),
4698 bfd_get_filename (abfd),
4699 _bfd_coff_internal_syment_name (abfd, syment, buf));
4702 return COFF_SYMBOL_LOCAL;
4706 SUBSUBSECTION
4707 Reading relocations
4709 Coff relocations are easily transformed into the internal BFD form
4710 (@code{arelent}).
4712 Reading a coff relocation table is done in the following stages:
4714 o Read the entire coff relocation table into memory.
4716 o Process each relocation in turn; first swap it from the
4717 external to the internal form.
4719 o Turn the symbol referenced in the relocation's symbol index
4720 into a pointer into the canonical symbol table.
4721 This table is the same as the one returned by a call to
4722 @code{bfd_canonicalize_symtab}. The back end will call that
4723 routine and save the result if a canonicalization hasn't been done.
4725 o The reloc index is turned into a pointer to a howto
4726 structure, in a back end specific way. For instance, the 386
4727 and 960 use the @code{r_type} to directly produce an index
4728 into a howto table vector; the 88k subtracts a number from the
4729 @code{r_type} field and creates an addend field.
4733 #ifndef CALC_ADDEND
4734 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
4736 coff_symbol_type *coffsym = (coff_symbol_type *) NULL; \
4737 if (ptr && bfd_asymbol_bfd (ptr) != abfd) \
4738 coffsym = (obj_symbols (abfd) \
4739 + (cache_ptr->sym_ptr_ptr - symbols)); \
4740 else if (ptr) \
4741 coffsym = coff_symbol_from (abfd, ptr); \
4742 if (coffsym != (coff_symbol_type *) NULL \
4743 && coffsym->native->u.syment.n_scnum == 0) \
4744 cache_ptr->addend = 0; \
4745 else if (ptr && bfd_asymbol_bfd (ptr) == abfd \
4746 && ptr->section != (asection *) NULL) \
4747 cache_ptr->addend = - (ptr->section->vma + ptr->value); \
4748 else \
4749 cache_ptr->addend = 0; \
4751 #endif
4753 static boolean
4754 coff_slurp_reloc_table (abfd, asect, symbols)
4755 bfd * abfd;
4756 sec_ptr asect;
4757 asymbol ** symbols;
4759 RELOC *native_relocs;
4760 arelent *reloc_cache;
4761 arelent *cache_ptr;
4763 unsigned int idx;
4765 if (asect->relocation)
4766 return true;
4767 if (asect->reloc_count == 0)
4768 return true;
4769 if (asect->flags & SEC_CONSTRUCTOR)
4770 return true;
4771 if (!coff_slurp_symbol_table (abfd))
4772 return false;
4773 native_relocs =
4774 (RELOC *) buy_and_read (abfd,
4775 asect->rel_filepos,
4776 SEEK_SET,
4777 (size_t) (bfd_coff_relsz (abfd) *
4778 asect->reloc_count));
4779 reloc_cache = (arelent *)
4780 bfd_alloc (abfd, (size_t) (asect->reloc_count * sizeof (arelent)));
4782 if (reloc_cache == NULL)
4783 return false;
4785 for (idx = 0; idx < asect->reloc_count; idx++)
4787 struct internal_reloc dst;
4788 struct external_reloc *src;
4789 #ifndef RELOC_PROCESSING
4790 asymbol *ptr;
4791 #endif
4793 cache_ptr = reloc_cache + idx;
4794 src = native_relocs + idx;
4796 coff_swap_reloc_in (abfd, src, &dst);
4798 #ifdef RELOC_PROCESSING
4799 RELOC_PROCESSING (cache_ptr, &dst, symbols, abfd, asect);
4800 #else
4801 cache_ptr->address = dst.r_vaddr;
4803 if (dst.r_symndx != -1)
4805 if (dst.r_symndx < 0 || dst.r_symndx >= obj_conv_table_size (abfd))
4807 (*_bfd_error_handler)
4808 (_("%s: warning: illegal symbol index %ld in relocs"),
4809 bfd_get_filename (abfd), dst.r_symndx);
4810 cache_ptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
4811 ptr = NULL;
4813 else
4815 cache_ptr->sym_ptr_ptr = (symbols
4816 + obj_convert (abfd)[dst.r_symndx]);
4817 ptr = *(cache_ptr->sym_ptr_ptr);
4820 else
4822 cache_ptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
4823 ptr = NULL;
4826 /* The symbols definitions that we have read in have been
4827 relocated as if their sections started at 0. But the offsets
4828 refering to the symbols in the raw data have not been
4829 modified, so we have to have a negative addend to compensate.
4831 Note that symbols which used to be common must be left alone */
4833 /* Calculate any reloc addend by looking at the symbol */
4834 CALC_ADDEND (abfd, ptr, dst, cache_ptr);
4836 cache_ptr->address -= asect->vma;
4837 /* !! cache_ptr->section = (asection *) NULL;*/
4839 /* Fill in the cache_ptr->howto field from dst.r_type */
4840 RTYPE2HOWTO (cache_ptr, &dst);
4841 #endif /* RELOC_PROCESSING */
4843 if (cache_ptr->howto == NULL)
4845 (*_bfd_error_handler)
4846 (_("%s: illegal relocation type %d at address 0x%lx"),
4847 bfd_get_filename (abfd), dst.r_type, (long) dst.r_vaddr);
4848 bfd_set_error (bfd_error_bad_value);
4849 return false;
4853 asect->relocation = reloc_cache;
4854 return true;
4857 #ifndef coff_rtype_to_howto
4858 #ifdef RTYPE2HOWTO
4860 /* Get the howto structure for a reloc. This is only used if the file
4861 including this one defines coff_relocate_section to be
4862 _bfd_coff_generic_relocate_section, so it is OK if it does not
4863 always work. It is the responsibility of the including file to
4864 make sure it is reasonable if it is needed. */
4866 static reloc_howto_type *coff_rtype_to_howto
4867 PARAMS ((bfd *, asection *, struct internal_reloc *,
4868 struct coff_link_hash_entry *, struct internal_syment *,
4869 bfd_vma *));
4871 /*ARGSUSED*/
4872 static reloc_howto_type *
4873 coff_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
4874 bfd *abfd ATTRIBUTE_UNUSED;
4875 asection *sec ATTRIBUTE_UNUSED;
4876 struct internal_reloc *rel;
4877 struct coff_link_hash_entry *h ATTRIBUTE_UNUSED;
4878 struct internal_syment *sym ATTRIBUTE_UNUSED;
4879 bfd_vma *addendp ATTRIBUTE_UNUSED;
4881 arelent genrel;
4883 RTYPE2HOWTO (&genrel, rel);
4884 return genrel.howto;
4887 #else /* ! defined (RTYPE2HOWTO) */
4889 #define coff_rtype_to_howto NULL
4891 #endif /* ! defined (RTYPE2HOWTO) */
4892 #endif /* ! defined (coff_rtype_to_howto) */
4894 /* This is stupid. This function should be a boolean predicate. */
4895 static long
4896 coff_canonicalize_reloc (abfd, section, relptr, symbols)
4897 bfd * abfd;
4898 sec_ptr section;
4899 arelent ** relptr;
4900 asymbol ** symbols;
4902 arelent *tblptr = section->relocation;
4903 unsigned int count = 0;
4905 if (section->flags & SEC_CONSTRUCTOR)
4907 /* this section has relocs made up by us, they are not in the
4908 file, so take them out of their chain and place them into
4909 the data area provided */
4910 arelent_chain *chain = section->constructor_chain;
4911 for (count = 0; count < section->reloc_count; count++)
4913 *relptr++ = &chain->relent;
4914 chain = chain->next;
4918 else
4920 if (! coff_slurp_reloc_table (abfd, section, symbols))
4921 return -1;
4923 tblptr = section->relocation;
4925 for (; count++ < section->reloc_count;)
4926 *relptr++ = tblptr++;
4929 *relptr = 0;
4930 return section->reloc_count;
4933 #ifdef GNU960
4934 file_ptr
4935 coff_sym_filepos (abfd)
4936 bfd *abfd;
4938 return obj_sym_filepos (abfd);
4940 #endif
4942 #ifndef coff_reloc16_estimate
4943 #define coff_reloc16_estimate dummy_reloc16_estimate
4945 static int dummy_reloc16_estimate
4946 PARAMS ((bfd *, asection *, arelent *, unsigned int,
4947 struct bfd_link_info *));
4949 static int
4950 dummy_reloc16_estimate (abfd, input_section, reloc, shrink, link_info)
4951 bfd *abfd ATTRIBUTE_UNUSED;
4952 asection *input_section ATTRIBUTE_UNUSED;
4953 arelent *reloc ATTRIBUTE_UNUSED;
4954 unsigned int shrink ATTRIBUTE_UNUSED;
4955 struct bfd_link_info *link_info ATTRIBUTE_UNUSED;
4957 abort ();
4958 return 0;
4961 #endif
4963 #ifndef coff_reloc16_extra_cases
4965 #define coff_reloc16_extra_cases dummy_reloc16_extra_cases
4967 /* This works even if abort is not declared in any header file. */
4969 static void dummy_reloc16_extra_cases
4970 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *, arelent *,
4971 bfd_byte *, unsigned int *, unsigned int *));
4973 static void
4974 dummy_reloc16_extra_cases (abfd, link_info, link_order, reloc, data, src_ptr,
4975 dst_ptr)
4976 bfd *abfd ATTRIBUTE_UNUSED;
4977 struct bfd_link_info *link_info ATTRIBUTE_UNUSED;
4978 struct bfd_link_order *link_order ATTRIBUTE_UNUSED;
4979 arelent *reloc ATTRIBUTE_UNUSED;
4980 bfd_byte *data ATTRIBUTE_UNUSED;
4981 unsigned int *src_ptr ATTRIBUTE_UNUSED;
4982 unsigned int *dst_ptr ATTRIBUTE_UNUSED;
4984 abort ();
4986 #endif
4988 /* If coff_relocate_section is defined, we can use the optimized COFF
4989 backend linker. Otherwise we must continue to use the old linker. */
4990 #ifdef coff_relocate_section
4991 #ifndef coff_bfd_link_hash_table_create
4992 #define coff_bfd_link_hash_table_create _bfd_coff_link_hash_table_create
4993 #endif
4994 #ifndef coff_bfd_link_add_symbols
4995 #define coff_bfd_link_add_symbols _bfd_coff_link_add_symbols
4996 #endif
4997 #ifndef coff_bfd_final_link
4998 #define coff_bfd_final_link _bfd_coff_final_link
4999 #endif
5000 #else /* ! defined (coff_relocate_section) */
5001 #define coff_relocate_section NULL
5002 #ifndef coff_bfd_link_hash_table_create
5003 #define coff_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
5004 #endif
5005 #ifndef coff_bfd_link_add_symbols
5006 #define coff_bfd_link_add_symbols _bfd_generic_link_add_symbols
5007 #endif
5008 #define coff_bfd_final_link _bfd_generic_final_link
5009 #endif /* ! defined (coff_relocate_section) */
5011 #define coff_bfd_link_split_section _bfd_generic_link_split_section
5013 #ifndef coff_start_final_link
5014 #define coff_start_final_link NULL
5015 #endif
5017 #ifndef coff_adjust_symndx
5018 #define coff_adjust_symndx NULL
5019 #endif
5021 #ifndef coff_link_add_one_symbol
5022 #define coff_link_add_one_symbol _bfd_generic_link_add_one_symbol
5023 #endif
5025 #ifndef coff_link_output_has_begun
5027 static boolean coff_link_output_has_begun
5028 PARAMS ((bfd *, struct coff_final_link_info *));
5030 static boolean
5031 coff_link_output_has_begun (abfd, info)
5032 bfd * abfd;
5033 struct coff_final_link_info * info ATTRIBUTE_UNUSED;
5035 return abfd->output_has_begun;
5037 #endif
5039 #ifndef coff_final_link_postscript
5041 static boolean coff_final_link_postscript
5042 PARAMS ((bfd *, struct coff_final_link_info *));
5044 static boolean
5045 coff_final_link_postscript (abfd, pfinfo)
5046 bfd * abfd ATTRIBUTE_UNUSED;
5047 struct coff_final_link_info * pfinfo ATTRIBUTE_UNUSED;
5049 return true;
5051 #endif
5053 #ifndef coff_SWAP_aux_in
5054 #define coff_SWAP_aux_in coff_swap_aux_in
5055 #endif
5056 #ifndef coff_SWAP_sym_in
5057 #define coff_SWAP_sym_in coff_swap_sym_in
5058 #endif
5059 #ifndef coff_SWAP_lineno_in
5060 #define coff_SWAP_lineno_in coff_swap_lineno_in
5061 #endif
5062 #ifndef coff_SWAP_aux_out
5063 #define coff_SWAP_aux_out coff_swap_aux_out
5064 #endif
5065 #ifndef coff_SWAP_sym_out
5066 #define coff_SWAP_sym_out coff_swap_sym_out
5067 #endif
5068 #ifndef coff_SWAP_lineno_out
5069 #define coff_SWAP_lineno_out coff_swap_lineno_out
5070 #endif
5071 #ifndef coff_SWAP_reloc_out
5072 #define coff_SWAP_reloc_out coff_swap_reloc_out
5073 #endif
5074 #ifndef coff_SWAP_filehdr_out
5075 #define coff_SWAP_filehdr_out coff_swap_filehdr_out
5076 #endif
5077 #ifndef coff_SWAP_aouthdr_out
5078 #define coff_SWAP_aouthdr_out coff_swap_aouthdr_out
5079 #endif
5080 #ifndef coff_SWAP_scnhdr_out
5081 #define coff_SWAP_scnhdr_out coff_swap_scnhdr_out
5082 #endif
5083 #ifndef coff_SWAP_reloc_in
5084 #define coff_SWAP_reloc_in coff_swap_reloc_in
5085 #endif
5086 #ifndef coff_SWAP_filehdr_in
5087 #define coff_SWAP_filehdr_in coff_swap_filehdr_in
5088 #endif
5089 #ifndef coff_SWAP_aouthdr_in
5090 #define coff_SWAP_aouthdr_in coff_swap_aouthdr_in
5091 #endif
5092 #ifndef coff_SWAP_scnhdr_in
5093 #define coff_SWAP_scnhdr_in coff_swap_scnhdr_in
5094 #endif
5096 static const bfd_coff_backend_data bfd_coff_std_swap_table =
5098 coff_SWAP_aux_in, coff_SWAP_sym_in, coff_SWAP_lineno_in,
5099 coff_SWAP_aux_out, coff_SWAP_sym_out,
5100 coff_SWAP_lineno_out, coff_SWAP_reloc_out,
5101 coff_SWAP_filehdr_out, coff_SWAP_aouthdr_out,
5102 coff_SWAP_scnhdr_out,
5103 FILHSZ, AOUTSZ, SCNHSZ, SYMESZ, AUXESZ, RELSZ, LINESZ, FILNMLEN,
5104 #ifdef COFF_LONG_FILENAMES
5105 true,
5106 #else
5107 false,
5108 #endif
5109 #ifdef COFF_LONG_SECTION_NAMES
5110 true,
5111 #else
5112 false,
5113 #endif
5114 COFF_DEFAULT_SECTION_ALIGNMENT_POWER,
5115 #ifdef COFF_FORCE_SYMBOLS_IN_STRINGS
5116 true,
5117 #else
5118 false,
5119 #endif
5120 #ifdef COFF_DEBUG_STRING_WIDE_PREFIX
5122 #else
5124 #endif
5125 coff_SWAP_filehdr_in, coff_SWAP_aouthdr_in, coff_SWAP_scnhdr_in,
5126 coff_SWAP_reloc_in, coff_bad_format_hook, coff_set_arch_mach_hook,
5127 coff_mkobject_hook, styp_to_sec_flags, coff_set_alignment_hook,
5128 coff_slurp_symbol_table, symname_in_debug_hook, coff_pointerize_aux_hook,
5129 coff_print_aux, coff_reloc16_extra_cases, coff_reloc16_estimate,
5130 coff_classify_symbol, coff_compute_section_file_positions,
5131 coff_start_final_link, coff_relocate_section, coff_rtype_to_howto,
5132 coff_adjust_symndx, coff_link_add_one_symbol,
5133 coff_link_output_has_begun, coff_final_link_postscript
5136 #ifndef coff_close_and_cleanup
5137 #define coff_close_and_cleanup _bfd_generic_close_and_cleanup
5138 #endif
5140 #ifndef coff_bfd_free_cached_info
5141 #define coff_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
5142 #endif
5144 #ifndef coff_get_section_contents
5145 #define coff_get_section_contents _bfd_generic_get_section_contents
5146 #endif
5148 #ifndef coff_bfd_copy_private_symbol_data
5149 #define coff_bfd_copy_private_symbol_data _bfd_generic_bfd_copy_private_symbol_data
5150 #endif
5152 #ifndef coff_bfd_copy_private_section_data
5153 #define coff_bfd_copy_private_section_data _bfd_generic_bfd_copy_private_section_data
5154 #endif
5156 #ifndef coff_bfd_copy_private_bfd_data
5157 #define coff_bfd_copy_private_bfd_data _bfd_generic_bfd_copy_private_bfd_data
5158 #endif
5160 #ifndef coff_bfd_merge_private_bfd_data
5161 #define coff_bfd_merge_private_bfd_data _bfd_generic_bfd_merge_private_bfd_data
5162 #endif
5164 #ifndef coff_bfd_set_private_flags
5165 #define coff_bfd_set_private_flags _bfd_generic_bfd_set_private_flags
5166 #endif
5168 #ifndef coff_bfd_print_private_bfd_data
5169 #define coff_bfd_print_private_bfd_data _bfd_generic_bfd_print_private_bfd_data
5170 #endif
5172 #ifndef coff_bfd_is_local_label_name
5173 #define coff_bfd_is_local_label_name _bfd_coff_is_local_label_name
5174 #endif
5176 #ifndef coff_read_minisymbols
5177 #define coff_read_minisymbols _bfd_generic_read_minisymbols
5178 #endif
5180 #ifndef coff_minisymbol_to_symbol
5181 #define coff_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
5182 #endif
5184 /* The reloc lookup routine must be supplied by each individual COFF
5185 backend. */
5186 #ifndef coff_bfd_reloc_type_lookup
5187 #define coff_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
5188 #endif
5190 #ifndef coff_bfd_get_relocated_section_contents
5191 #define coff_bfd_get_relocated_section_contents \
5192 bfd_generic_get_relocated_section_contents
5193 #endif
5195 #ifndef coff_bfd_relax_section
5196 #define coff_bfd_relax_section bfd_generic_relax_section
5197 #endif
5199 #ifndef coff_bfd_gc_sections
5200 #define coff_bfd_gc_sections bfd_generic_gc_sections
5201 #endif
5203 #define CREATE_BIG_COFF_TARGET_VEC(VAR, NAME, EXTRA_O_FLAGS, EXTRA_S_FLAGS, UNDER, ALTERNATIVE) \
5204 const bfd_target VAR = \
5206 NAME , \
5207 bfd_target_coff_flavour, \
5208 BFD_ENDIAN_BIG, /* data byte order is big */ \
5209 BFD_ENDIAN_BIG, /* header byte order is big */ \
5210 /* object flags */ \
5211 (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | \
5212 HAS_SYMS | HAS_LOCALS | WP_TEXT | EXTRA_O_FLAGS), \
5213 /* section flags */ \
5214 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | EXTRA_S_FLAGS),\
5215 UNDER, /* leading symbol underscore */ \
5216 '/', /* ar_pad_char */ \
5217 15, /* ar_max_namelen */ \
5219 /* Data conversion functions. */ \
5220 bfd_getb64, bfd_getb_signed_64, bfd_putb64, \
5221 bfd_getb32, bfd_getb_signed_32, bfd_putb32, \
5222 bfd_getb16, bfd_getb_signed_16, bfd_putb16, \
5224 /* Header conversion functions. */ \
5225 bfd_getb64, bfd_getb_signed_64, bfd_putb64, \
5226 bfd_getb32, bfd_getb_signed_32, bfd_putb32, \
5227 bfd_getb16, bfd_getb_signed_16, bfd_putb16, \
5229 /* bfd_check_format */ \
5230 { _bfd_dummy_target, coff_object_p, bfd_generic_archive_p, \
5231 _bfd_dummy_target }, \
5232 /* bfd_set_format */ \
5233 { bfd_false, coff_mkobject, _bfd_generic_mkarchive, bfd_false }, \
5234 /* bfd_write_contents */ \
5235 { bfd_false, coff_write_object_contents, _bfd_write_archive_contents, \
5236 bfd_false }, \
5238 BFD_JUMP_TABLE_GENERIC (coff), \
5239 BFD_JUMP_TABLE_COPY (coff), \
5240 BFD_JUMP_TABLE_CORE (_bfd_nocore), \
5241 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff), \
5242 BFD_JUMP_TABLE_SYMBOLS (coff), \
5243 BFD_JUMP_TABLE_RELOCS (coff), \
5244 BFD_JUMP_TABLE_WRITE (coff), \
5245 BFD_JUMP_TABLE_LINK (coff), \
5246 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), \
5248 ALTERNATIVE, \
5250 COFF_SWAP_TABLE \
5253 #define CREATE_LITTLE_COFF_TARGET_VEC(VAR, NAME, EXTRA_O_FLAGS, EXTRA_S_FLAGS, UNDER, ALTERNATIVE) \
5254 const bfd_target VAR = \
5256 NAME , \
5257 bfd_target_coff_flavour, \
5258 BFD_ENDIAN_LITTLE, /* data byte order is little */ \
5259 BFD_ENDIAN_LITTLE, /* header byte order is little */ \
5260 /* object flags */ \
5261 (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | \
5262 HAS_SYMS | HAS_LOCALS | WP_TEXT | EXTRA_O_FLAGS), \
5263 /* section flags */ \
5264 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | EXTRA_S_FLAGS),\
5265 UNDER, /* leading symbol underscore */ \
5266 '/', /* ar_pad_char */ \
5267 15, /* ar_max_namelen */ \
5269 /* Data conversion functions. */ \
5270 bfd_getl64, bfd_getl_signed_64, bfd_putl64, \
5271 bfd_getl32, bfd_getl_signed_32, bfd_putl32, \
5272 bfd_getl16, bfd_getl_signed_16, bfd_putl16, \
5273 /* Header conversion functions. */ \
5274 bfd_getl64, bfd_getl_signed_64, bfd_putl64, \
5275 bfd_getl32, bfd_getl_signed_32, bfd_putl32, \
5276 bfd_getl16, bfd_getl_signed_16, bfd_putl16, \
5277 /* bfd_check_format */ \
5278 { _bfd_dummy_target, coff_object_p, bfd_generic_archive_p, \
5279 _bfd_dummy_target }, \
5280 /* bfd_set_format */ \
5281 { bfd_false, coff_mkobject, _bfd_generic_mkarchive, bfd_false }, \
5282 /* bfd_write_contents */ \
5283 { bfd_false, coff_write_object_contents, _bfd_write_archive_contents, \
5284 bfd_false }, \
5286 BFD_JUMP_TABLE_GENERIC (coff), \
5287 BFD_JUMP_TABLE_COPY (coff), \
5288 BFD_JUMP_TABLE_CORE (_bfd_nocore), \
5289 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff), \
5290 BFD_JUMP_TABLE_SYMBOLS (coff), \
5291 BFD_JUMP_TABLE_RELOCS (coff), \
5292 BFD_JUMP_TABLE_WRITE (coff), \
5293 BFD_JUMP_TABLE_LINK (coff), \
5294 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), \
5296 ALTERNATIVE, \
5298 COFF_SWAP_TABLE \