OSDN Git Service

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