OSDN Git Service

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