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