Wed Aug 30 20:41:27 1995 steve chamberlain <sac@slash.cygnus.com>
[external/binutils.git] / bfd / coffcode.h
1 /* Support for the generic parts of most COFF variants, for BFD.
2    Copyright 1990, 91, 92, 93, 94, 1995 Free Software Foundation, Inc.
3    Written by Cygnus Support.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 /*
22 Most of this hacked by  Steve Chamberlain,
23                         sac@cygnus.com
24 */
25 /*
26
27 SECTION
28         coff backends
29
30         BFD supports a number of different flavours of coff format.
31         The major differences between formats are the sizes and
32         alignments of fields in structures on disk, and the occasional
33         extra field.
34
35         Coff in all its varieties is implemented with a few common
36         files and a number of implementation specific files. For
37         example, The 88k bcs coff format is implemented in the file
38         @file{coff-m88k.c}. This file @code{#include}s
39         @file{coff/m88k.h} which defines the external structure of the
40         coff format for the 88k, and @file{coff/internal.h} which
41         defines the internal structure. @file{coff-m88k.c} also
42         defines the relocations used by the 88k format
43         @xref{Relocations}.
44
45         The Intel i960 processor version of coff is implemented in
46         @file{coff-i960.c}. This file has the same structure as
47         @file{coff-m88k.c}, except that it includes @file{coff/i960.h}
48         rather than @file{coff-m88k.h}.
49
50 SUBSECTION
51         Porting to a new version of coff
52
53         The recommended method is to select from the existing
54         implementations the version of coff which is most like the one
55         you want to use.  For example, we'll say that i386 coff is
56         the one you select, and that your coff flavour is called foo.
57         Copy @file{i386coff.c} to @file{foocoff.c}, copy
58         @file{../include/coff/i386.h} to @file{../include/coff/foo.h},
59         and add the lines to @file{targets.c} and @file{Makefile.in}
60         so that your new back end is used. Alter the shapes of the
61         structures in @file{../include/coff/foo.h} so that they match
62         what you need. You will probably also have to add
63         @code{#ifdef}s to the code in @file{coff/internal.h} and
64         @file{coffcode.h} if your version of coff is too wild.
65
66         You can verify that your new BFD backend works quite simply by
67         building @file{objdump} from the @file{binutils} directory,
68         and making sure that its version of what's going on and your
69         host system's idea (assuming it has the pretty standard coff
70         dump utility, usually called @code{att-dump} or just
71         @code{dump}) are the same.  Then clean up your code, and send
72         what you've done to Cygnus. Then your stuff will be in the
73         next release, and you won't have to keep integrating it.
74
75 SUBSECTION
76         How the coff backend works
77
78 SUBSUBSECTION
79         File layout
80
81         The Coff backend is split into generic routines that are
82         applicable to any Coff target and routines that are specific
83         to a particular target.  The target-specific routines are
84         further split into ones which are basically the same for all
85         Coff targets except that they use the external symbol format
86         or use different values for certain constants.
87
88         The generic routines are in @file{coffgen.c}.  These routines
89         work for any Coff target.  They use some hooks into the target
90         specific code; the hooks are in a @code{bfd_coff_backend_data}
91         structure, one of which exists for each target.
92
93         The essentially similar target-specific routines are in
94         @file{coffcode.h}.  This header file includes executable C code.
95         The various Coff targets first include the appropriate Coff
96         header file, make any special defines that are needed, and
97         then include @file{coffcode.h}.
98
99         Some of the Coff targets then also have additional routines in
100         the target source file itself.
101
102         For example, @file{coff-i960.c} includes
103         @file{coff/internal.h} and @file{coff/i960.h}.  It then
104         defines a few constants, such as @code{I960}, and includes
105         @file{coffcode.h}.  Since the i960 has complex relocation
106         types, @file{coff-i960.c} also includes some code to
107         manipulate the i960 relocs.  This code is not in
108         @file{coffcode.h} because it would not be used by any other
109         target.
110
111 SUBSUBSECTION
112         Bit twiddling
113
114         Each flavour of coff supported in BFD has its own header file
115         describing the external layout of the structures. There is also
116         an internal description of the coff layout, in
117         @file{coff/internal.h}. A major function of the
118         coff backend is swapping the bytes and twiddling the bits to
119         translate the external form of the structures into the normal
120         internal form. This is all performed in the
121         @code{bfd_swap}_@i{thing}_@i{direction} routines. Some
122         elements are different sizes between different versions of
123         coff; it is the duty of the coff version specific include file
124         to override the definitions of various packing routines in
125         @file{coffcode.h}. E.g., the size of line number entry in coff is
126         sometimes 16 bits, and sometimes 32 bits. @code{#define}ing
127         @code{PUT_LNSZ_LNNO} and @code{GET_LNSZ_LNNO} will select the
128         correct one. No doubt, some day someone will find a version of
129         coff which has a varying field size not catered to at the
130         moment. To port BFD, that person will have to add more @code{#defines}.
131         Three of the bit twiddling routines are exported to
132         @code{gdb}; @code{coff_swap_aux_in}, @code{coff_swap_sym_in}
133         and @code{coff_swap_linno_in}. @code{GDB} reads the symbol
134         table on its own, but uses BFD to fix things up.  More of the
135         bit twiddlers are exported for @code{gas};
136         @code{coff_swap_aux_out}, @code{coff_swap_sym_out},
137         @code{coff_swap_lineno_out}, @code{coff_swap_reloc_out},
138         @code{coff_swap_filehdr_out}, @code{coff_swap_aouthdr_out},
139         @code{coff_swap_scnhdr_out}. @code{Gas} currently keeps track
140         of all the symbol table and reloc drudgery itself, thereby
141         saving the internal BFD overhead, but uses BFD to swap things
142         on the way out, making cross ports much safer.  Doing so also
143         allows BFD (and thus the linker) to use the same header files
144         as @code{gas}, which makes one avenue to disaster disappear.
145
146 SUBSUBSECTION
147         Symbol reading
148
149         The simple canonical form for symbols used by BFD is not rich
150         enough to keep all the information available in a coff symbol
151         table. The back end gets around this problem by keeping the original
152         symbol table around, "behind the scenes".
153
154         When a symbol table is requested (through a call to
155         @code{bfd_canonicalize_symtab}), a request gets through to
156         @code{coff_get_normalized_symtab}. This reads the symbol table from
157         the coff file and swaps all the structures inside into the
158         internal form. It also fixes up all the pointers in the table
159         (represented in the file by offsets from the first symbol in
160         the table) into physical pointers to elements in the new
161         internal table. This involves some work since the meanings of
162         fields change depending upon context: a field that is a
163         pointer to another structure in the symbol table at one moment
164         may be the size in bytes of a structure at the next.  Another
165         pass is made over the table. All symbols which mark file names
166         (<<C_FILE>> symbols) are modified so that the internal
167         string points to the value in the auxent (the real filename)
168         rather than the normal text associated with the symbol
169         (@code{".file"}).
170
171         At this time the symbol names are moved around. Coff stores
172         all symbols less than nine characters long physically
173         within the symbol table; longer strings are kept at the end of
174         the file in the string  table. This pass moves all strings
175         into memory and replaces them with pointers to the strings.
176
177
178         The symbol table is massaged once again, this time to create
179         the canonical table used by the BFD application. Each symbol
180         is inspected in turn, and a decision made (using the
181         @code{sclass} field) about the various flags to set in the
182         @code{asymbol}.  @xref{Symbols}. The generated canonical table
183         shares strings with the hidden internal symbol table.
184
185         Any linenumbers are read from the coff file too, and attached
186         to the symbols which own the functions the linenumbers belong to.
187
188 SUBSUBSECTION
189         Symbol writing
190
191         Writing a symbol to a coff file which didn't come from a coff
192         file will lose any debugging information. The @code{asymbol}
193         structure remembers the BFD from which the symbol was taken, and on
194         output the back end makes sure that the same destination target as
195         source target is present.
196
197         When the symbols have come from a coff file then all the
198         debugging information is preserved.
199
200         Symbol tables are provided for writing to the back end in a
201         vector of pointers to pointers. This allows applications like
202         the linker to accumulate and output large symbol tables
203         without having to do too much byte copying.
204
205         This function runs through the provided symbol table and
206         patches each symbol marked as a file place holder
207         (@code{C_FILE}) to point to the next file place holder in the
208         list. It also marks each @code{offset} field in the list with
209         the offset from the first symbol of the current symbol.
210
211         Another function of this procedure is to turn the canonical
212         value form of BFD into the form used by coff. Internally, BFD
213         expects symbol values to be offsets from a section base; so a
214         symbol physically at 0x120, but in a section starting at
215         0x100, would have the value 0x20. Coff expects symbols to
216         contain their final value, so symbols have their values
217         changed at this point to reflect their sum with their owning
218         section.  This transformation uses the
219         <<output_section>> field of the @code{asymbol}'s
220         @code{asection} @xref{Sections}.
221
222         o <<coff_mangle_symbols>>
223
224         This routine runs though the provided symbol table and uses
225         the offsets generated by the previous pass and the pointers
226         generated when the symbol table was read in to create the
227         structured hierachy required by coff. It changes each pointer
228         to a symbol into the index into the symbol table of the asymbol.
229
230         o <<coff_write_symbols>>
231
232         This routine runs through the symbol table and patches up the
233         symbols from their internal form into the coff way, calls the
234         bit twiddlers, and writes out the table to the file.
235
236 */
237
238 /*
239 INTERNAL_DEFINITION
240         coff_symbol_type
241
242 DESCRIPTION
243         The hidden information for an <<asymbol>> is described in a
244         <<combined_entry_type>>:
245
246 CODE_FRAGMENT
247 .
248 .typedef struct coff_ptr_struct
249 .{
250 .
251 .       {* Remembers the offset from the first symbol in the file for
252 .          this symbol. Generated by coff_renumber_symbols. *}
253 .unsigned int offset;
254 .
255 .       {* Should the value of this symbol be renumbered.  Used for
256 .          XCOFF C_BSTAT symbols.  Set by coff_slurp_symbol_table.  *}
257 .unsigned int fix_value : 1;
258 .
259 .       {* Should the tag field of this symbol be renumbered.
260 .          Created by coff_pointerize_aux. *}
261 .unsigned int fix_tag : 1;
262 .
263 .       {* Should the endidx field of this symbol be renumbered.
264 .          Created by coff_pointerize_aux. *}
265 .unsigned int fix_end : 1;
266 .
267 .       {* Should the x_csect.x_scnlen field be renumbered.
268 .          Created by coff_slurp_symbol_table. *}
269 .unsigned int fix_scnlen : 1;
270 .
271 .       {* The container for the symbol structure as read and translated
272 .           from the file. *}
273 .
274 .union {
275 .   union internal_auxent auxent;
276 .   struct internal_syment syment;
277 . } u;
278 .} combined_entry_type;
279 .
280 .
281 .{* Each canonical asymbol really looks like this: *}
282 .
283 .typedef struct coff_symbol_struct
284 .{
285 .   {* The actual symbol which the rest of BFD works with *}
286 .asymbol symbol;
287 .
288 .   {* A pointer to the hidden information for this symbol *}
289 .combined_entry_type *native;
290 .
291 .   {* A pointer to the linenumber information for this symbol *}
292 .struct lineno_cache_entry *lineno;
293 .
294 .   {* Have the line numbers been relocated yet ? *}
295 .boolean done_lineno;
296 .} coff_symbol_type;
297
298
299 */
300
301 #ifndef IMAGE_BASE
302 #define IMAGE_BASE 0
303 #endif
304
305
306 static bfd_vma
307 pe_value(ptr, def)
308      bfd_link_pe_info_dval *ptr;
309      bfd_vma def;
310 {
311   if (ptr && ptr->defined)
312     return ptr->value;
313   return def;
314 }
315
316
317 #include "coffswap.h"
318 \f
319 /* void warning(); */
320
321 /*
322  * Return a word with STYP_* (scnhdr.s_flags) flags set to represent the
323  * incoming SEC_* flags.  The inverse of this function is styp_to_sec_flags().
324  * NOTE: If you add to/change this routine, you should mirror the changes
325  *      in styp_to_sec_flags().
326  */
327 static long
328 sec_to_styp_flags (sec_name, sec_flags)
329      CONST char *sec_name;
330      flagword sec_flags;
331 {
332   long styp_flags = 0;
333
334   if (!strcmp (sec_name, _TEXT))
335     {
336       styp_flags = STYP_TEXT;
337     }
338   else if (!strcmp (sec_name, _DATA))
339     {
340       styp_flags = STYP_DATA;
341 #ifdef TWO_DATA_SECS
342     }
343   else if (!strcmp (sec_name, ".data2"))
344     {
345       styp_flags = STYP_DATA;
346 #endif /* TWO_DATA_SECS */
347     }
348   else if (!strcmp (sec_name, _BSS))
349     {
350       styp_flags = STYP_BSS;
351 #ifdef _COMMENT
352     }
353   else if (!strcmp (sec_name, _COMMENT))
354     {
355       styp_flags = STYP_INFO;
356 #endif /* _COMMENT */
357 #ifdef _LIB
358     }
359   else if (!strcmp (sec_name, _LIB))
360     {
361       styp_flags = STYP_LIB;
362 #endif /* _LIB */
363 #ifdef _LIT
364     }
365   else if (!strcmp (sec_name, _LIT))
366     {
367       styp_flags = STYP_LIT;
368 #endif /* _LIT */
369     }
370   else if (!strcmp (sec_name, ".debug"))
371     {
372 #ifdef STYP_DEBUG
373       styp_flags = STYP_DEBUG;
374 #else
375       styp_flags = STYP_INFO;
376 #endif
377     }
378   else if (!strncmp (sec_name, ".stab", 5))
379     {
380       styp_flags = STYP_INFO;
381     }
382 #ifdef COFF_WITH_PE
383   else if (!strcmp (sec_name, ".edata"))
384     {
385       styp_flags = STYP_DATA;
386     }
387 #endif
388   /* Try and figure out what it should be */
389   else if (sec_flags & SEC_CODE)
390     {
391       styp_flags = STYP_TEXT;
392     }
393   else if (sec_flags & SEC_DATA)
394     {
395       styp_flags = STYP_DATA;
396     }
397   else if (sec_flags & SEC_READONLY)
398     {
399 #ifdef STYP_LIT                 /* 29k readonly text/data section */
400       styp_flags = STYP_LIT;
401 #else
402       styp_flags = STYP_TEXT;
403 #endif /* STYP_LIT */
404     }
405   else if (sec_flags & SEC_LOAD)
406     {
407       styp_flags = STYP_TEXT;
408     }
409   else if (sec_flags & SEC_ALLOC)
410     {
411       styp_flags = STYP_BSS;
412     }
413
414 #ifdef STYP_NOLOAD
415   if ((sec_flags & (SEC_NEVER_LOAD | SEC_COFF_SHARED_LIBRARY)) != 0)
416     styp_flags |= STYP_NOLOAD;
417 #endif
418
419   return (styp_flags);
420 }
421 /*
422  * Return a word with SEC_* flags set to represent the incoming
423  * STYP_* flags (from scnhdr.s_flags).   The inverse of this
424  * function is sec_to_styp_flags().
425  * NOTE: If you add to/change this routine, you should mirror the changes
426  *      in sec_to_styp_flags().
427  */
428 static flagword
429 styp_to_sec_flags (abfd, hdr, name)
430      bfd *abfd;
431      PTR hdr;
432      const char *name;
433 {
434   struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
435   long styp_flags = internal_s->s_flags;
436   flagword sec_flags = 0;
437
438 #ifdef STYP_NOLOAD
439   if (styp_flags & STYP_NOLOAD)
440     {
441       sec_flags |= SEC_NEVER_LOAD;
442     }
443 #endif /* STYP_NOLOAD */
444
445   /* For 386 COFF, at least, an unloadable text or data section is
446      actually a shared library section.  */
447   if (styp_flags & STYP_TEXT)
448     {
449       if (sec_flags & SEC_NEVER_LOAD)
450         sec_flags |= SEC_CODE | SEC_COFF_SHARED_LIBRARY;
451       else
452         sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
453     }
454   else if (styp_flags & STYP_DATA)
455     {
456       if (sec_flags & SEC_NEVER_LOAD)
457         sec_flags |= SEC_DATA | SEC_COFF_SHARED_LIBRARY;
458       else
459         sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
460     }
461   else if (styp_flags & STYP_BSS)
462     {
463 #ifdef BSS_NOLOAD_IS_SHARED_LIBRARY
464       if (sec_flags & SEC_NEVER_LOAD)
465         sec_flags |= SEC_ALLOC | SEC_COFF_SHARED_LIBRARY;
466       else
467 #endif
468         sec_flags |= SEC_ALLOC;
469     }
470   else if (styp_flags & STYP_INFO)
471     {
472       /* We mark these as SEC_DEBUGGING, but only if COFF_PAGE_SIZE is
473          defined.  coff_compute_section_file_positions uses
474          COFF_PAGE_SIZE to ensure that the low order bits of the
475          section VMA and the file offset match.  If we don't know
476          COFF_PAGE_SIZE, we can't ensure the correct correspondence,
477          and demand page loading of the file will fail.  */
478 #ifdef COFF_PAGE_SIZE
479       sec_flags |= SEC_DEBUGGING;
480 #endif
481     }
482   else if (strcmp (name, _TEXT) == 0)
483     {
484       if (sec_flags & SEC_NEVER_LOAD)
485         sec_flags |= SEC_CODE | SEC_COFF_SHARED_LIBRARY;
486       else
487         sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
488     }
489   else if (strcmp (name, _DATA) == 0
490 #ifdef TWO_DATA_SECS
491            || strcmp (name, ".data2") == 0
492 #endif
493            )
494     {
495       if (sec_flags & SEC_NEVER_LOAD)
496         sec_flags |= SEC_DATA | SEC_COFF_SHARED_LIBRARY;
497       else
498         sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
499     }
500   else if (strcmp (name, _BSS) == 0)
501     {
502 #ifdef BSS_NOLOAD_IS_SHARED_LIBRARY
503       if (sec_flags & SEC_NEVER_LOAD)
504         sec_flags |= SEC_ALLOC | SEC_COFF_SHARED_LIBRARY;
505       else
506 #endif
507         sec_flags |= SEC_ALLOC;
508     }
509   else if (strcmp (name, ".debug") == 0
510 #ifdef _COMMENT
511            || strcmp (name, _COMMENT) == 0
512 #endif
513            || strncmp (name, ".stab", 5) == 0)
514     {
515 #ifdef COFF_PAGE_SIZE
516       sec_flags |= SEC_DEBUGGING;
517 #endif
518     }
519 #ifdef _LIB
520   else if (strcmp (name, _LIB) == 0)
521     ;
522 #endif
523 #ifdef _LIT
524   else if (strcmp (name, _LIT) == 0)
525     {
526       sec_flags = SEC_LOAD | SEC_ALLOC | SEC_READONLY;
527     }
528 #endif
529   else
530     {
531       sec_flags |= SEC_ALLOC | SEC_LOAD;
532     }
533
534 #ifdef STYP_LIT                 /* A29k readonly text/data section type */
535   if ((styp_flags & STYP_LIT) == STYP_LIT)
536     {
537       sec_flags = (SEC_LOAD | SEC_ALLOC | SEC_READONLY);
538     }
539 #endif /* STYP_LIT */
540 #ifdef STYP_OTHER_LOAD          /* Other loaded sections */
541   if (styp_flags & STYP_OTHER_LOAD)
542     {
543       sec_flags = (SEC_LOAD | SEC_ALLOC);
544     }
545 #endif /* STYP_SDATA */
546
547   return (sec_flags);
548 }
549
550 #define get_index(symbol)       ((symbol)->udata.i)
551
552 /*
553 INTERNAL_DEFINITION
554         bfd_coff_backend_data
555
556 CODE_FRAGMENT
557
558 Special entry points for gdb to swap in coff symbol table parts:
559 .typedef struct
560 .{
561 .  void (*_bfd_coff_swap_aux_in) PARAMS ((
562 .       bfd            *abfd,
563 .       PTR             ext,
564 .       int             type,
565 .       int             class,
566 .       int             indaux,
567 .       int             numaux,
568 .       PTR             in));
569 .
570 .  void (*_bfd_coff_swap_sym_in) PARAMS ((
571 .       bfd            *abfd ,
572 .       PTR             ext,
573 .       PTR             in));
574 .
575 .  void (*_bfd_coff_swap_lineno_in) PARAMS ((
576 .       bfd            *abfd,
577 .       PTR            ext,
578 .       PTR             in));
579 .
580
581 Special entry points for gas to swap out coff parts:
582
583 . unsigned int (*_bfd_coff_swap_aux_out) PARAMS ((
584 .       bfd     *abfd,
585 .       PTR     in,
586 .       int     type,
587 .       int     class,
588 .       int     indaux,
589 .       int     numaux,
590 .       PTR     ext));
591 .
592 . unsigned int (*_bfd_coff_swap_sym_out) PARAMS ((
593 .      bfd      *abfd,
594 .      PTR      in,
595 .      PTR      ext));
596 .
597 . unsigned int (*_bfd_coff_swap_lineno_out) PARAMS ((
598 .       bfd     *abfd,
599 .       PTR     in,
600 .       PTR     ext));
601 .
602 . unsigned int (*_bfd_coff_swap_reloc_out) PARAMS ((
603 .       bfd     *abfd,
604 .       PTR     src,
605 .       PTR     dst));
606 .
607 . unsigned int (*_bfd_coff_swap_filehdr_out) PARAMS ((
608 .       bfd     *abfd,
609 .       PTR     in,
610 .       PTR     out));
611 .
612 . unsigned int (*_bfd_coff_swap_aouthdr_out) PARAMS ((
613 .       bfd     *abfd,
614 .       PTR     in,
615 .       PTR     out));
616 .
617 . unsigned int (*_bfd_coff_swap_scnhdr_out) PARAMS ((
618 .       bfd     *abfd,
619 .       PTR     in,
620 .       PTR     out));
621 .
622
623 Special entry points for generic COFF routines to call target
624 dependent COFF routines:
625
626 . unsigned int _bfd_filhsz;
627 . unsigned int _bfd_aoutsz;
628 . unsigned int _bfd_scnhsz;
629 . unsigned int _bfd_symesz;
630 . unsigned int _bfd_auxesz;
631 . unsigned int _bfd_relsz;
632 . unsigned int _bfd_linesz;
633 . boolean _bfd_coff_long_filenames;
634 . void (*_bfd_coff_swap_filehdr_in) PARAMS ((
635 .       bfd     *abfd,
636 .       PTR     ext,
637 .       PTR     in));
638 . void (*_bfd_coff_swap_aouthdr_in) PARAMS ((
639 .       bfd     *abfd,
640 .       PTR     ext,
641 .       PTR     in));
642 . void (*_bfd_coff_swap_scnhdr_in) PARAMS ((
643 .       bfd     *abfd,
644 .       PTR     ext,
645 .       PTR     in));
646 . void (*_bfd_coff_swap_reloc_in) PARAMS ((
647 .       bfd     *abfd,
648 .       PTR     ext,
649 .       PTR     in));
650 . boolean (*_bfd_coff_bad_format_hook) PARAMS ((
651 .       bfd     *abfd,
652 .       PTR     internal_filehdr));
653 . boolean (*_bfd_coff_set_arch_mach_hook) PARAMS ((
654 .       bfd     *abfd,
655 .       PTR     internal_filehdr));
656 . PTR (*_bfd_coff_mkobject_hook) PARAMS ((
657 .       bfd     *abfd,
658 .       PTR     internal_filehdr,
659 .       PTR     internal_aouthdr));
660 . flagword (*_bfd_styp_to_sec_flags_hook) PARAMS ((
661 .       bfd     *abfd,
662 .       PTR     internal_scnhdr,
663 .       const char *name));
664 . asection *(*_bfd_make_section_hook) PARAMS ((
665 .       bfd     *abfd,
666 .       char    *name));
667 . void (*_bfd_set_alignment_hook) PARAMS ((
668 .       bfd     *abfd,
669 .       asection *sec,
670 .       PTR     internal_scnhdr));
671 . boolean (*_bfd_coff_slurp_symbol_table) PARAMS ((
672 .       bfd     *abfd));
673 . boolean (*_bfd_coff_symname_in_debug) PARAMS ((
674 .       bfd     *abfd,
675 .       struct internal_syment *sym));
676 . void (*_bfd_coff_reloc16_extra_cases) PARAMS ((
677 .       bfd     *abfd,
678 .       struct bfd_link_info *link_info,
679 .       struct bfd_link_order *link_order,
680 .       arelent *reloc,
681 .       bfd_byte *data,
682 .       unsigned int *src_ptr,
683 .       unsigned int *dst_ptr));
684 . int (*_bfd_coff_reloc16_estimate) PARAMS ((
685 .       bfd *abfd,
686 .       asection *input_section,
687 .       arelent *r,
688 .       unsigned int shrink,
689 .       struct bfd_link_info *link_info));
690 . boolean (*_bfd_coff_sym_is_global) PARAMS ((
691 .       bfd *abfd,
692 .       struct internal_syment *));
693 . void (*_bfd_coff_compute_section_file_positions) PARAMS ((
694 .       bfd *abfd));
695 . boolean (*_bfd_coff_relocate_section) PARAMS ((
696 .       bfd *output_bfd,
697 .       struct bfd_link_info *info,
698 .       bfd *input_bfd,
699 .       asection *input_section,
700 .       bfd_byte *contents,
701 .       struct internal_reloc *relocs,
702 .       struct internal_syment *syms,
703 .       asection **sections));
704 . reloc_howto_type *(*_bfd_coff_rtype_to_howto) PARAMS ((
705 .       bfd *abfd,
706 .       asection *sec,
707 .       struct internal_reloc *rel,
708 .       struct coff_link_hash_entry *h,
709 .       struct internal_syment *sym,
710 .       bfd_vma *addendp));
711 . boolean (*_bfd_coff_adjust_symndx) PARAMS ((
712 .       bfd *obfd,
713 .       struct bfd_link_info *info,
714 .       bfd *ibfd,
715 .       asection *sec,
716 .       struct internal_reloc *reloc,
717 .       boolean *adjustedp));
718 .
719 .} bfd_coff_backend_data;
720 .
721 .#define coff_backend_info(abfd) ((bfd_coff_backend_data *) (abfd)->xvec->backend_data)
722 .
723 .#define bfd_coff_swap_aux_in(a,e,t,c,ind,num,i) \
724 .        ((coff_backend_info (a)->_bfd_coff_swap_aux_in) (a,e,t,c,ind,num,i))
725 .
726 .#define bfd_coff_swap_sym_in(a,e,i) \
727 .        ((coff_backend_info (a)->_bfd_coff_swap_sym_in) (a,e,i))
728 .
729 .#define bfd_coff_swap_lineno_in(a,e,i) \
730 .        ((coff_backend_info ( a)->_bfd_coff_swap_lineno_in) (a,e,i))
731 .
732 .#define bfd_coff_swap_reloc_out(abfd, i, o) \
733 .        ((coff_backend_info (abfd)->_bfd_coff_swap_reloc_out) (abfd, i, o))
734 .
735 .#define bfd_coff_swap_lineno_out(abfd, i, o) \
736 .        ((coff_backend_info (abfd)->_bfd_coff_swap_lineno_out) (abfd, i, o))
737 .
738 .#define bfd_coff_swap_aux_out(a,i,t,c,ind,num,o) \
739 .        ((coff_backend_info (a)->_bfd_coff_swap_aux_out) (a,i,t,c,ind,num,o))
740 .
741 .#define bfd_coff_swap_sym_out(abfd, i,o) \
742 .        ((coff_backend_info (abfd)->_bfd_coff_swap_sym_out) (abfd, i, o))
743 .
744 .#define bfd_coff_swap_scnhdr_out(abfd, i,o) \
745 .        ((coff_backend_info (abfd)->_bfd_coff_swap_scnhdr_out) (abfd, i, o))
746 .
747 .#define bfd_coff_swap_filehdr_out(abfd, i,o) \
748 .        ((coff_backend_info (abfd)->_bfd_coff_swap_filehdr_out) (abfd, i, o))
749 .
750 .#define bfd_coff_swap_aouthdr_out(abfd, i,o) \
751 .        ((coff_backend_info (abfd)->_bfd_coff_swap_aouthdr_out) (abfd, i, o))
752 .
753 .#define bfd_coff_filhsz(abfd) (coff_backend_info (abfd)->_bfd_filhsz)
754 .#define bfd_coff_aoutsz(abfd) (coff_backend_info (abfd)->_bfd_aoutsz)
755 .#define bfd_coff_scnhsz(abfd) (coff_backend_info (abfd)->_bfd_scnhsz)
756 .#define bfd_coff_symesz(abfd) (coff_backend_info (abfd)->_bfd_symesz)
757 .#define bfd_coff_auxesz(abfd) (coff_backend_info (abfd)->_bfd_auxesz)
758 .#define bfd_coff_relsz(abfd)  (coff_backend_info (abfd)->_bfd_relsz)
759 .#define bfd_coff_linesz(abfd) (coff_backend_info (abfd)->_bfd_linesz)
760 .#define bfd_coff_long_filenames(abfd) (coff_backend_info (abfd)->_bfd_coff_long_filenames)
761 .#define bfd_coff_swap_filehdr_in(abfd, i,o) \
762 .        ((coff_backend_info (abfd)->_bfd_coff_swap_filehdr_in) (abfd, i, o))
763 .
764 .#define bfd_coff_swap_aouthdr_in(abfd, i,o) \
765 .        ((coff_backend_info (abfd)->_bfd_coff_swap_aouthdr_in) (abfd, i, o))
766 .
767 .#define bfd_coff_swap_scnhdr_in(abfd, i,o) \
768 .        ((coff_backend_info (abfd)->_bfd_coff_swap_scnhdr_in) (abfd, i, o))
769 .
770 .#define bfd_coff_swap_reloc_in(abfd, i, o) \
771 .        ((coff_backend_info (abfd)->_bfd_coff_swap_reloc_in) (abfd, i, o))
772 .
773 .#define bfd_coff_bad_format_hook(abfd, filehdr) \
774 .        ((coff_backend_info (abfd)->_bfd_coff_bad_format_hook) (abfd, filehdr))
775 .
776 .#define bfd_coff_set_arch_mach_hook(abfd, filehdr)\
777 .        ((coff_backend_info (abfd)->_bfd_coff_set_arch_mach_hook) (abfd, filehdr))
778 .#define bfd_coff_mkobject_hook(abfd, filehdr, aouthdr)\
779 .        ((coff_backend_info (abfd)->_bfd_coff_mkobject_hook) (abfd, filehdr, aouthdr))
780 .
781 .#define bfd_coff_styp_to_sec_flags_hook(abfd, scnhdr, name)\
782 .        ((coff_backend_info (abfd)->_bfd_styp_to_sec_flags_hook) (abfd, scnhdr, name))
783 .
784 .#define bfd_coff_make_section_hook(abfd, name)\
785 .        ((coff_backend_info (abfd)->_bfd_make_section_hook) (abfd, name))
786 .
787 .#define bfd_coff_set_alignment_hook(abfd, sec, scnhdr)\
788 .        ((coff_backend_info (abfd)->_bfd_set_alignment_hook) (abfd, sec, scnhdr))
789 .
790 .#define bfd_coff_slurp_symbol_table(abfd)\
791 .        ((coff_backend_info (abfd)->_bfd_coff_slurp_symbol_table) (abfd))
792 .
793 .#define bfd_coff_symname_in_debug(abfd, sym)\
794 .        ((coff_backend_info (abfd)->_bfd_coff_symname_in_debug) (abfd, sym))
795 .
796 .#define bfd_coff_reloc16_extra_cases(abfd, link_info, link_order, reloc, data, src_ptr, dst_ptr)\
797 .        ((coff_backend_info (abfd)->_bfd_coff_reloc16_extra_cases)\
798 .         (abfd, link_info, link_order, reloc, data, src_ptr, dst_ptr))
799 .
800 .#define bfd_coff_reloc16_estimate(abfd, section, reloc, shrink, link_info)\
801 .        ((coff_backend_info (abfd)->_bfd_coff_reloc16_estimate)\
802 .         (abfd, section, reloc, shrink, link_info))
803 .
804 .#define bfd_coff_sym_is_global(abfd, sym)\
805 .        ((coff_backend_info (abfd)->_bfd_coff_sym_is_global)\
806 .         (abfd, sym))
807 .
808 .#define bfd_coff_compute_section_file_positions(abfd)\
809 .        ((coff_backend_info (abfd)->_bfd_coff_compute_section_file_positions)\
810 .         (abfd))
811 .
812 .#define bfd_coff_relocate_section(obfd,info,ibfd,o,con,rel,isyms,secs)\
813 .        ((coff_backend_info (ibfd)->_bfd_coff_relocate_section)\
814 .         (obfd, info, ibfd, o, con, rel, isyms, secs))
815 .#define bfd_coff_rtype_to_howto(abfd, sec, rel, h, sym, addendp)\
816 .        ((coff_backend_info (abfd)->_bfd_coff_rtype_to_howto)\
817 .         (abfd, sec, rel, h, sym, addendp))
818 .#define bfd_coff_adjust_symndx(obfd, info, ibfd, sec, rel, adjustedp)\
819 .        ((coff_backend_info (abfd)->_bfd_coff_adjust_symndx)\
820 .         (obfd, info, ibfd, sec, rel, adjustedp))
821 .
822 */
823
824 /* See whether the magic number matches.  */
825
826 static boolean
827 coff_bad_format_hook (abfd, filehdr)
828      bfd * abfd;
829      PTR filehdr;
830 {
831   struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
832
833   if (BADMAG (*internal_f))
834     return false;
835
836   /* if the optional header is NULL or not the correct size then
837      quit; the only difference I can see between m88k dgux headers (MC88DMAGIC)
838      and Intel 960 readwrite headers (I960WRMAGIC) is that the
839      optional header is of a different size.
840
841      But the mips keeps extra stuff in it's opthdr, so dont check
842      when doing that
843      */
844
845 #if defined(M88) || defined(I960)
846   if (internal_f->f_opthdr != 0 && AOUTSZ != internal_f->f_opthdr)
847     return false;
848 #endif
849
850   return true;
851 }
852
853 static asection *
854 coff_make_section_hook (abfd, name)
855      bfd * abfd;
856      char *name;
857 {
858 #ifdef TWO_DATA_SECS
859   /* FIXME: This predates the call to bfd_make_section_anyway
860      in make_a_section_from_file, and can probably go away.  */
861   /* On SCO a file created by the Microsoft assembler can have two
862      .data sections.  We use .data2 for the second one.  */
863   if (strcmp (name, _DATA) == 0)
864     return bfd_make_section (abfd, ".data2");
865 #endif
866   return (asection *) NULL;
867 }
868
869 /*
870    initialize a section structure with information peculiar to this
871    particular implementation of coff
872 */
873
874 static boolean
875 coff_new_section_hook (abfd, section)
876      bfd * abfd;
877      asection * section;
878 {
879   section->alignment_power = COFF_DEFAULT_SECTION_ALIGNMENT_POWER;
880
881   /* Allocate aux records for section symbols, to store size and
882      related info.
883
884      @@ Shouldn't use constant multiplier here!  */
885   coffsymbol (section->symbol)->native =
886     (combined_entry_type *) bfd_zalloc (abfd,
887                                         sizeof (combined_entry_type) * 10);
888
889   /* The .stab section must be aligned to 2**2 at most, because
890      otherwise there may be gaps in the section which gdb will not
891      know how to interpret.  Examining the section name is a hack, but
892      that is also how gdb locates the section.  We also align the
893      .stabstr section this way for backward compatibility, although I
894      believe it would work anyhow.  */
895   if (COFF_DEFAULT_SECTION_ALIGNMENT_POWER > 2
896       && (strncmp (section->name, ".stab", 5) == 0))
897     section->alignment_power = 2;
898
899   return true;
900 }
901
902 #ifdef I960
903
904 /* Set the alignment of a BFD section.  */
905
906 static void
907 coff_set_alignment_hook (abfd, section, scnhdr)
908      bfd * abfd;
909      asection * section;
910      PTR scnhdr;
911 {
912   struct internal_scnhdr *hdr = (struct internal_scnhdr *) scnhdr;
913   unsigned int i;
914
915   for (i = 0; i < 32; i++)
916     if ((1 << i) >= hdr->s_align)
917       break;
918   section->alignment_power = i;
919 }
920
921 #else /* ! I960 */
922
923 #define coff_set_alignment_hook \
924   ((void (*) PARAMS ((bfd *, asection *, PTR))) bfd_void)
925
926 #endif /* ! I960 */
927
928 static boolean
929 coff_mkobject (abfd)
930      bfd * abfd;
931 {
932   coff_data_type *coff;
933
934   abfd->tdata.coff_obj_data = (struct coff_tdata *) bfd_zalloc (abfd, sizeof (coff_data_type));
935   if (abfd->tdata.coff_obj_data == 0)
936     {
937       bfd_set_error (bfd_error_no_memory);
938       return false;
939     }
940   coff = coff_data (abfd);
941   coff->symbols = (coff_symbol_type *) NULL;
942   coff->conversion_table = (unsigned int *) NULL;
943   coff->raw_syments = (struct coff_ptr_struct *) NULL;
944   coff->relocbase = 0;
945 /*  make_abs_section(abfd);*/
946
947 #ifdef COFF_WITH_PE
948   obj_pe (abfd) = 1;
949 #endif
950   return true;
951 }
952
953 /* Create the COFF backend specific information.  */
954
955 static PTR
956 coff_mkobject_hook (abfd, filehdr, aouthdr)
957      bfd * abfd;
958      PTR filehdr;
959      PTR aouthdr;
960 {
961   struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
962   coff_data_type *coff;
963
964   if (coff_mkobject (abfd) == false)
965     return NULL;
966
967   coff = coff_data (abfd);
968
969   coff->sym_filepos = internal_f->f_symptr;
970
971   /* These members communicate important constants about the symbol
972      table to GDB's symbol-reading code.  These `constants'
973      unfortunately vary among coff implementations...  */
974   coff->local_n_btmask = N_BTMASK;
975   coff->local_n_btshft = N_BTSHFT;
976   coff->local_n_tmask = N_TMASK;
977   coff->local_n_tshift = N_TSHIFT;
978   coff->local_symesz = SYMESZ;
979   coff->local_auxesz = AUXESZ;
980   coff->local_linesz = LINESZ;
981
982   obj_raw_syment_count (abfd) =
983     obj_conv_table_size (abfd) =
984       internal_f->f_nsyms;
985
986   return (PTR) coff;
987 }
988
989 /* Determine the machine architecture and type.  FIXME: This is target
990    dependent because the magic numbers are defined in the target
991    dependent header files.  But there is no particular need for this.
992    If the magic numbers were moved to a separate file, this function
993    would be target independent and would also be much more successful
994    at linking together COFF files for different architectures.  */
995
996 static boolean
997 coff_set_arch_mach_hook (abfd, filehdr)
998      bfd *abfd;
999      PTR filehdr;
1000 {
1001   long machine;
1002   enum bfd_architecture arch;
1003   struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
1004
1005   machine = 0;
1006   switch (internal_f->f_magic)
1007     {
1008 #ifdef I386MAGIC
1009     case I386MAGIC:
1010     case I386PTXMAGIC:
1011     case I386AIXMAGIC:          /* Danbury PS/2 AIX C Compiler */
1012     case LYNXCOFFMAGIC: /* shadows the m68k Lynx number below, sigh */
1013       arch = bfd_arch_i386;
1014       machine = 0;
1015       break;
1016 #endif
1017 #ifdef A29K_MAGIC_BIG
1018     case A29K_MAGIC_BIG:
1019     case A29K_MAGIC_LITTLE:
1020       arch = bfd_arch_a29k;
1021       machine = 0;
1022       break;
1023 #endif
1024 #ifdef ARMMAGIC
1025     case ARMMAGIC:
1026       arch = bfd_arch_arm;
1027       machine =0;
1028       break;
1029 #endif
1030 #ifdef MC68MAGIC
1031     case MC68MAGIC:
1032     case M68MAGIC:
1033 #ifdef MC68KBCSMAGIC
1034     case MC68KBCSMAGIC:
1035 #endif
1036 #ifdef APOLLOM68KMAGIC
1037     case APOLLOM68KMAGIC:
1038 #endif
1039 #ifdef LYNXCOFFMAGIC
1040     case LYNXCOFFMAGIC:
1041 #endif
1042       arch = bfd_arch_m68k;
1043       machine = 68020;
1044       break;
1045 #endif
1046 #ifdef MC88MAGIC
1047     case MC88MAGIC:
1048     case MC88DMAGIC:
1049     case MC88OMAGIC:
1050       arch = bfd_arch_m88k;
1051       machine = 88100;
1052       break;
1053 #endif
1054 #ifdef Z8KMAGIC
1055     case Z8KMAGIC:
1056       arch = bfd_arch_z8k;
1057       switch (internal_f->f_flags & F_MACHMASK)
1058         {
1059         case F_Z8001:
1060           machine = bfd_mach_z8001;
1061           break;
1062         case F_Z8002:
1063           machine = bfd_mach_z8002;
1064           break;
1065         default:
1066           return false;
1067         }
1068       break;
1069 #endif
1070 #ifdef I960
1071 #ifdef I960ROMAGIC
1072     case I960ROMAGIC:
1073     case I960RWMAGIC:
1074       arch = bfd_arch_i960;
1075       switch (F_I960TYPE & internal_f->f_flags)
1076         {
1077         default:
1078         case F_I960CORE:
1079           machine = bfd_mach_i960_core;
1080           break;
1081         case F_I960KB:
1082           machine = bfd_mach_i960_kb_sb;
1083           break;
1084         case F_I960MC:
1085           machine = bfd_mach_i960_mc;
1086           break;
1087         case F_I960XA:
1088           machine = bfd_mach_i960_xa;
1089           break;
1090         case F_I960CA:
1091           machine = bfd_mach_i960_ca;
1092           break;
1093         case F_I960KA:
1094           machine = bfd_mach_i960_ka_sa;
1095           break;
1096           /* start-sanitize-i960xl */
1097         case F_I960XL:
1098           machine = bfd_mach_i960_xl;
1099           break;
1100           /* end-sanitize-i960xl */
1101         }
1102       break;
1103 #endif
1104 #endif
1105
1106 #ifdef U802ROMAGIC
1107     case U802ROMAGIC:
1108     case U802WRMAGIC:
1109     case U802TOCMAGIC:
1110       arch = bfd_arch_rs6000;
1111       machine = 6000;
1112       break;
1113 #endif
1114
1115 #ifdef WE32KMAGIC
1116     case WE32KMAGIC:
1117       arch = bfd_arch_we32k;
1118       machine = 0;
1119       break;
1120 #endif
1121
1122 #ifdef H8300MAGIC
1123     case H8300MAGIC:
1124       arch = bfd_arch_h8300;
1125       machine = bfd_mach_h8300;
1126       /* !! FIXME this probably isn't the right place for this */
1127       abfd->flags |= BFD_IS_RELAXABLE;
1128       break;
1129 #endif
1130
1131 #ifdef H8300HMAGIC
1132     case H8300HMAGIC:
1133       arch = bfd_arch_h8300;
1134       machine = bfd_mach_h8300h;
1135       /* !! FIXME this probably isn't the right place for this */
1136       abfd->flags |= BFD_IS_RELAXABLE;
1137       break;
1138 #endif
1139
1140 #ifdef SH_ARCH_MAGIC_BIG
1141     case SH_ARCH_MAGIC_BIG:
1142     case SH_ARCH_MAGIC_LITTLE:
1143       arch = bfd_arch_sh;
1144       machine = 0;
1145       break;
1146 #endif
1147
1148 #ifdef H8500MAGIC
1149     case H8500MAGIC:
1150       arch = bfd_arch_h8500;
1151       machine = 0;
1152       break;
1153 #endif
1154
1155 #ifdef SPARCMAGIC
1156     case SPARCMAGIC:
1157 #ifdef LYNXCOFFMAGIC
1158     case LYNXCOFFMAGIC:
1159 #endif
1160       arch = bfd_arch_sparc;
1161       machine = 0;
1162       break;
1163 #endif
1164
1165     default:                    /* Unreadable input file type */
1166       arch = bfd_arch_obscure;
1167       break;
1168     }
1169
1170   bfd_default_set_arch_mach (abfd, arch, machine);
1171   return true;
1172 }
1173
1174 #ifdef SYMNAME_IN_DEBUG
1175
1176 static boolean
1177 symname_in_debug_hook (abfd, sym)
1178      bfd * abfd;
1179      struct internal_syment *sym;
1180 {
1181   return SYMNAME_IN_DEBUG (sym) ? true : false;
1182 }
1183
1184 #else
1185
1186 #define symname_in_debug_hook \
1187   (boolean (*) PARAMS ((bfd *, struct internal_syment *))) bfd_false
1188
1189 #endif
1190
1191 /*
1192 SUBSUBSECTION
1193         Writing relocations
1194
1195         To write relocations, the back end steps though the
1196         canonical relocation table and create an
1197         @code{internal_reloc}. The symbol index to use is removed from
1198         the @code{offset} field in the symbol table supplied.  The
1199         address comes directly from the sum of the section base
1200         address and the relocation offset; the type is dug directly
1201         from the howto field.  Then the @code{internal_reloc} is
1202         swapped into the shape of an @code{external_reloc} and written
1203         out to disk.
1204
1205 */
1206
1207 static boolean
1208 coff_write_relocs (abfd, first_undef)
1209      bfd * abfd;
1210      int first_undef;
1211 {
1212   asection *s;
1213   for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1214     {
1215       unsigned int i;
1216       struct external_reloc dst;
1217
1218       arelent **p = s->orelocation;
1219       if (bfd_seek (abfd, s->rel_filepos, SEEK_SET) != 0)
1220         return false;
1221       for (i = 0; i < s->reloc_count; i++)
1222         {
1223           struct internal_reloc n;
1224           arelent *q = p[i];
1225           memset ((PTR) & n, 0, sizeof (n));
1226
1227           /* Now we've renumbered the symbols we know where the
1228              undefined symbols live in the table.  Check the reloc
1229              entries for symbols who's output bfd isn't the right one.
1230              This is because the symbol was undefined (which means
1231              that all the pointers are never made to point to the same
1232              place). This is a bad thing,'cause the symbols attached
1233              to the output bfd are indexed, so that the relocation
1234              entries know which symbol index they point to.  So we
1235              have to look up the output symbol here. */
1236
1237           if (q->sym_ptr_ptr[0]->the_bfd != abfd)
1238             {
1239               int i;
1240               const char *sname = q->sym_ptr_ptr[0]->name;
1241               asymbol **outsyms = abfd->outsymbols;
1242               for (i = first_undef; outsyms[i]; i++)
1243                 {
1244                   const char *intable = outsyms[i]->name;
1245                   if (strcmp (intable, sname) == 0) {
1246                     /* got a hit, so repoint the reloc */
1247                     q->sym_ptr_ptr = outsyms + i;
1248                     break;
1249                   }
1250                 }
1251             }
1252
1253           n.r_vaddr = q->address + s->vma;
1254
1255 #ifdef R_IHCONST
1256           /* The 29k const/consth reloc pair is a real kludge.  The consth
1257              part doesn't have a symbol; it has an offset.  So rebuilt
1258              that here.  */
1259           if (q->howto->type == R_IHCONST)
1260             n.r_symndx = q->addend;
1261           else
1262 #endif
1263             if (q->sym_ptr_ptr)
1264               {
1265                 if (q->sym_ptr_ptr == bfd_abs_section_ptr->symbol_ptr_ptr)
1266                   /* This is a relocation relative to the absolute symbol.  */
1267                   n.r_symndx = -1;
1268                 else
1269                   {
1270                     n.r_symndx = get_index ((*(q->sym_ptr_ptr)));
1271                     /* Take notice if the symbol reloc points to a symbol
1272                        we don't have in our symbol table.  What should we
1273                        do for this??  */
1274                     if (n.r_symndx > obj_conv_table_size (abfd))
1275                       abort ();
1276                   }
1277               }
1278
1279 #ifdef SWAP_OUT_RELOC_OFFSET
1280           n.r_offset = q->addend;
1281 #endif
1282
1283 #ifdef SELECT_RELOC
1284           /* Work out reloc type from what is required */
1285           SELECT_RELOC (n, q->howto);
1286 #else
1287           n.r_type = q->howto->type;
1288 #endif
1289           coff_swap_reloc_out (abfd, &n, &dst);
1290           if (bfd_write ((PTR) & dst, 1, RELSZ, abfd) != RELSZ)
1291             return false;
1292         }
1293     }
1294
1295   return true;
1296 }
1297
1298 /* Set flags and magic number of a coff file from architecture and machine
1299    type.  Result is true if we can represent the arch&type, false if not.  */
1300
1301 static boolean
1302 coff_set_flags (abfd, magicp, flagsp)
1303      bfd * abfd;
1304      unsigned *magicp;
1305      unsigned short *flagsp;
1306 {
1307   switch (bfd_get_arch (abfd))
1308     {
1309 #ifdef Z8KMAGIC
1310     case bfd_arch_z8k:
1311       *magicp = Z8KMAGIC;
1312       switch (bfd_get_mach (abfd))
1313         {
1314         case bfd_mach_z8001:
1315           *flagsp = F_Z8001;
1316           break;
1317         case bfd_mach_z8002:
1318           *flagsp = F_Z8002;
1319           break;
1320         default:
1321           return false;
1322         }
1323       return true;
1324 #endif
1325 #ifdef I960ROMAGIC
1326
1327     case bfd_arch_i960:
1328
1329       {
1330         unsigned flags;
1331         *magicp = I960ROMAGIC;
1332         /*
1333           ((bfd_get_file_flags(abfd) & WP_TEXT) ? I960ROMAGIC :
1334           I960RWMAGIC);   FIXME???
1335           */
1336         switch (bfd_get_mach (abfd))
1337           {
1338           case bfd_mach_i960_core:
1339             flags = F_I960CORE;
1340             break;
1341           case bfd_mach_i960_kb_sb:
1342             flags = F_I960KB;
1343             break;
1344           case bfd_mach_i960_mc:
1345             flags = F_I960MC;
1346             break;
1347           case bfd_mach_i960_xa:
1348             flags = F_I960XA;
1349             break;
1350           case bfd_mach_i960_ca:
1351             flags = F_I960CA;
1352             break;
1353           case bfd_mach_i960_ka_sa:
1354             flags = F_I960KA;
1355             break;
1356             /* start-sanitize-i960xl */
1357           case bfd_mach_i960_xl:
1358             flags = F_I960XL;
1359             break;
1360             /* end-sanitize-i960xl */
1361           default:
1362             return false;
1363           }
1364         *flagsp = flags;
1365         return true;
1366       }
1367       break;
1368 #endif
1369 #ifdef ARMMAGIC
1370     case bfd_arch_arm:
1371       *magicp = ARMMAGIC;
1372       return true;
1373 #endif
1374 #ifdef I386MAGIC
1375     case bfd_arch_i386:
1376       *magicp = I386MAGIC;
1377 #ifdef LYNXOS
1378       /* Just overwrite the usual value if we're doing Lynx. */
1379       *magicp = LYNXCOFFMAGIC;
1380 #endif
1381       return true;
1382       break;
1383 #endif
1384 #ifdef MC68MAGIC
1385     case bfd_arch_m68k:
1386 #ifdef APOLLOM68KMAGIC
1387       *magicp = APOLLO_COFF_VERSION_NUMBER;
1388 #else
1389       *magicp = MC68MAGIC;
1390 #endif
1391 #ifdef LYNXOS
1392       /* Just overwrite the usual value if we're doing Lynx. */
1393       *magicp = LYNXCOFFMAGIC;
1394 #endif
1395       return true;
1396       break;
1397 #endif
1398
1399 #ifdef MC88MAGIC
1400     case bfd_arch_m88k:
1401       *magicp = MC88OMAGIC;
1402       return true;
1403       break;
1404 #endif
1405 #ifdef H8300MAGIC
1406     case bfd_arch_h8300:
1407       switch (bfd_get_mach (abfd))
1408         {
1409         case bfd_mach_h8300:
1410           *magicp = H8300MAGIC;
1411           return true;
1412         case bfd_mach_h8300h:
1413           *magicp = H8300HMAGIC;
1414           return true;
1415         }
1416       break;
1417 #endif
1418
1419 #ifdef SH_ARCH_MAGIC_BIG
1420     case bfd_arch_sh:
1421       if (abfd->xvec->byteorder_big_p)
1422         *magicp = SH_ARCH_MAGIC_BIG;
1423       else
1424         *magicp = SH_ARCH_MAGIC_LITTLE;
1425       return true;
1426       break;
1427 #endif
1428
1429 #ifdef SPARCMAGIC
1430     case bfd_arch_sparc:
1431       *magicp = SPARCMAGIC;
1432 #ifdef LYNXOS
1433       /* Just overwrite the usual value if we're doing Lynx. */
1434       *magicp = LYNXCOFFMAGIC;
1435 #endif
1436       return true;
1437       break;
1438 #endif
1439
1440 #ifdef H8500MAGIC
1441     case bfd_arch_h8500:
1442       *magicp = H8500MAGIC;
1443       return true;
1444       break;
1445 #endif
1446 #ifdef A29K_MAGIC_BIG
1447     case bfd_arch_a29k:
1448       if (abfd->xvec->byteorder_big_p)
1449         *magicp = A29K_MAGIC_BIG;
1450       else
1451         *magicp = A29K_MAGIC_LITTLE;
1452       return true;
1453       break;
1454 #endif
1455
1456 #ifdef WE32KMAGIC
1457     case bfd_arch_we32k:
1458       *magicp = WE32KMAGIC;
1459       return true;
1460       break;
1461 #endif
1462
1463 #ifdef U802TOCMAGIC
1464     case bfd_arch_rs6000:
1465     case bfd_arch_powerpc:
1466       *magicp = U802TOCMAGIC;
1467       return true;
1468       break;
1469 #endif
1470
1471     default:                    /* Unknown architecture */
1472       /* return false;  -- fall through to "return false" below, to avoid
1473        "statement never reached" errors on the one below. */
1474       break;
1475     }
1476
1477   return false;
1478 }
1479
1480
1481 static boolean
1482 coff_set_arch_mach (abfd, arch, machine)
1483      bfd * abfd;
1484      enum bfd_architecture arch;
1485      unsigned long machine;
1486 {
1487   unsigned dummy1;
1488   unsigned short dummy2;
1489
1490   if (! bfd_default_set_arch_mach (abfd, arch, machine))
1491     return false;
1492
1493   if (arch != bfd_arch_unknown &&
1494       coff_set_flags (abfd, &dummy1, &dummy2) != true)
1495     return false;               /* We can't represent this type */
1496
1497   return true;                  /* We're easy ... */
1498 }
1499
1500
1501 /* Calculate the file position for each section. */
1502
1503 static void
1504 coff_compute_section_file_positions (abfd)
1505      bfd * abfd;
1506 {
1507   asection *current;
1508   asection *previous = (asection *) NULL;
1509   file_ptr sofar = FILHSZ;
1510
1511 #ifndef I960
1512   file_ptr old_sofar;
1513 #endif
1514   unsigned int count;
1515
1516
1517 #ifdef COFF_IMAGE_WITH_PE
1518   int page_size;
1519   if (coff_data (abfd)->link_info) 
1520     {
1521       page_size = pe_value (&(coff_data (abfd)->link_info->pe_info->file_alignment),
1522                             PE_DEF_FILE_ALIGNMENT);
1523     }
1524   else
1525     page_size = PE_DEF_FILE_ALIGNMENT;
1526 #elif defined (COFF_PAGE_SIZE)
1527   int page_size = COFF_PAGE_SIZE;
1528 #endif
1529
1530   if (bfd_get_start_address (abfd))
1531     {
1532       /*  A start address may have been added to the original file. In this
1533           case it will need an optional header to record it.  */
1534       abfd->flags |= EXEC_P;
1535     }
1536
1537   if (abfd->flags & EXEC_P)
1538     sofar += AOUTSZ;
1539
1540   sofar += abfd->section_count * SCNHSZ;
1541   for (current = abfd->sections, count = 1;
1542        current != (asection *) NULL;
1543        current = current->next, ++count)
1544     {
1545       current->target_index = count;
1546
1547       /* Only deal with sections which have contents */
1548       if (!(current->flags & SEC_HAS_CONTENTS))
1549         continue;
1550
1551 #ifdef COFF_WITH_PE
1552       /* Do not include the .junk section.  This is where we collect section
1553          data which we don't need.  This is mainly the MS .debug$ data which
1554          stores codeview debug data. */
1555       if (strcmp (current->name, ".junk") == 0)
1556         {
1557           continue;
1558         }
1559 #endif
1560
1561       /* Align the sections in the file to the same boundary on
1562          which they are aligned in virtual memory.  I960 doesn't
1563          do this (FIXME) so we can stay in sync with Intel.  960
1564          doesn't yet page from files... */
1565 #ifndef I960
1566       {
1567         /* make sure this section is aligned on the right boundary - by
1568            padding the previous section up if necessary */
1569
1570         old_sofar = sofar;
1571         sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
1572         if (previous != (asection *) NULL)
1573           {
1574             previous->_raw_size += sofar - old_sofar;
1575           }
1576       }
1577
1578 #endif
1579
1580       /* In demand paged files the low order bits of the file offset
1581          must match the low order bits of the virtual address.  */
1582 #ifdef COFF_PAGE_SIZE
1583       if ((abfd->flags & D_PAGED) != 0
1584           && (current->flags & SEC_ALLOC) != 0)
1585         sofar += (current->vma - sofar) % page_size;
1586 #endif
1587
1588       current->filepos = sofar;
1589
1590       sofar += current->_raw_size;
1591 #ifndef I960
1592       /* make sure that this section is of the right size too */
1593       old_sofar = sofar;
1594       sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
1595       current->_raw_size += sofar - old_sofar;
1596 #endif
1597
1598 #ifdef _LIB
1599       /* Force .lib sections to start at zero.  The vma is then
1600          incremented in coff_set_section_contents.  This is right for
1601          SVR3.2.  */
1602       if (strcmp (current->name, _LIB) == 0)
1603         bfd_set_section_vma (abfd, current, 0);
1604 #endif
1605
1606       previous = current;
1607     }
1608 #ifdef COFF_IMAGE_WITH_PE
1609   /* Normally, the starting location for the symbol table will be at the end
1610      of the last section.  However, when dealing with NT, the last section
1611      must be as long as its size rounded up to the next page (0x1000). */
1612   sofar = (sofar + page_size - 1) & -page_size;
1613
1614   if (previous)
1615     previous->_raw_size = (previous->_raw_size + page_size -1) & -page_size;
1616 #endif
1617
1618   obj_relocbase (abfd) = sofar;
1619   abfd->output_has_begun = true;
1620
1621 }
1622
1623 #ifndef RS6000COFF_C
1624
1625 /* If .file, .text, .data, .bss symbols are missing, add them.  */
1626 /* @@ Should we only be adding missing symbols, or overriding the aux
1627    values for existing section symbols?  */
1628 static boolean
1629 coff_add_missing_symbols (abfd)
1630      bfd *abfd;
1631 {
1632   unsigned int nsyms = bfd_get_symcount (abfd);
1633   asymbol **sympp = abfd->outsymbols;
1634   asymbol **sympp2;
1635   unsigned int i;
1636   int need_text = 1, need_data = 1, need_bss = 1, need_file = 1;
1637
1638   for (i = 0; i < nsyms; i++)
1639     {
1640       coff_symbol_type *csym = coff_symbol_from (abfd, sympp[i]);
1641       CONST char *name;
1642       if (csym)
1643         {
1644           /* only do this if there is a coff representation of the input
1645            symbol */
1646           if (csym->native && csym->native->u.syment.n_sclass == C_FILE)
1647             {
1648               need_file = 0;
1649               continue;
1650             }
1651           name = csym->symbol.name;
1652           if (!name)
1653             continue;
1654           if (!strcmp (name, _TEXT))
1655             need_text = 0;
1656 #ifdef APOLLO_M68
1657           else if (!strcmp (name, ".wtext"))
1658             need_text = 0;
1659 #endif
1660           else if (!strcmp (name, _DATA))
1661             need_data = 0;
1662           else if (!strcmp (name, _BSS))
1663             need_bss = 0;
1664         }
1665     }
1666   /* Now i == bfd_get_symcount (abfd).  */
1667   /* @@ For now, don't deal with .file symbol.  */
1668   need_file = 0;
1669
1670   if (!need_text && !need_data && !need_bss && !need_file)
1671     return true;
1672   nsyms += need_text + need_data + need_bss + need_file;
1673   sympp2 = (asymbol **) bfd_alloc_by_size_t (abfd, nsyms * sizeof (asymbol *));
1674   if (!sympp2)
1675     {
1676       bfd_set_error (bfd_error_no_memory);
1677       return false;
1678     }
1679   memcpy (sympp2, sympp, i * sizeof (asymbol *));
1680   if (need_file)
1681     {
1682       /* @@ Generate fake .file symbol, in sympp2[i], and increment i.  */
1683       abort ();
1684     }
1685   if (need_text)
1686     sympp2[i++] = coff_section_symbol (abfd, _TEXT);
1687   if (need_data)
1688     sympp2[i++] = coff_section_symbol (abfd, _DATA);
1689   if (need_bss)
1690     sympp2[i++] = coff_section_symbol (abfd, _BSS);
1691   BFD_ASSERT (i == nsyms);
1692   bfd_set_symtab (abfd, sympp2, nsyms);
1693   return true;
1694 }
1695
1696 #endif /* ! defined (RS6000COFF_C) */
1697
1698 #ifdef COFF_WITH_PE
1699 static void add_data_entry (abfd, aout, idx, name, base)
1700      bfd *abfd;
1701      struct internal_aouthdr *aout;
1702      int idx;
1703      char *name;
1704      bfd_vma base;
1705 {
1706   asection *sec = bfd_get_section_by_name (abfd, name);
1707
1708   /* add import directory information if it exists */
1709   if (sec != NULL)
1710     {
1711       aout->pe->DataDirectory[idx].VirtualAddress = sec->lma - base;
1712       aout->pe->DataDirectory[idx].Size = sec->_raw_size;
1713       sec->flags |= SEC_DATA;
1714     }
1715 }
1716
1717
1718 static void 
1719 fill_pe_header_info (abfd, internal_f, internal_a)
1720      bfd *abfd;
1721      struct internal_filehdr *internal_f;
1722      struct internal_aouthdr *internal_a;
1723 {
1724   /* assign other filehdr fields for DOS header and NT signature */
1725
1726   int sa;
1727   int fa;
1728   bfd_vma ib;
1729   bfd_link_pe_info *pe_info = coff_data (abfd)->link_info->pe_info;
1730
1731   internal_f->f_timdat = time (0);
1732
1733   if (pe_value  (&pe_info->dll, 0))
1734     internal_f->f_flags |=  F_DLL ;
1735
1736
1737   if (bfd_get_section_by_name (abfd, ".reloc"))
1738     internal_f->f_flags &= ~F_RELFLG;
1739
1740
1741   memset (internal_f->pe, 0, sizeof (struct internal_extra_pe_filehdr));
1742   memset (internal_a->pe, 0, sizeof (struct internal_extra_pe_aouthdr));
1743
1744
1745   ib =  internal_a->pe->ImageBase =  pe_value (&pe_info->image_base, NT_EXE_IMAGE_BASE);
1746
1747   if (internal_a->tsize) 
1748     internal_a->text_start -= ib;
1749   if (internal_a->dsize) 
1750     internal_a->data_start -= ib;
1751   if (internal_a->entry) 
1752     internal_a->entry -= ib;
1753
1754
1755   sa = internal_a->pe->SectionAlignment = pe_value (&pe_info->section_alignment,
1756                                                     NT_SECTION_ALIGNMENT);
1757
1758   fa = internal_a->pe->FileAlignment = pe_value (&pe_info->file_alignment, 
1759                                                  NT_FILE_ALIGNMENT);
1760
1761 #define FA(x)  (((x) + fa -1 ) & (- fa))
1762 #define SA(x)  (((x) + sa -1 ) & (- sa))
1763
1764   /* We like to have the sizes aligned */
1765
1766   internal_a->bsize = FA (internal_a->bsize);
1767
1768   internal_f->pe->e_magic    = DOSMAGIC;
1769   internal_f->pe->e_cblp     = 0x90;
1770   internal_f->pe->e_cp       = 0x3;
1771   internal_f->pe->e_crlc     = 0x0;
1772   internal_f->pe->e_cparhdr  = 0x4;
1773   internal_f->pe->e_minalloc = 0x0;
1774   internal_f->pe->e_maxalloc = 0xffff;
1775   internal_f->pe->e_ss       = 0x0;
1776   internal_f->pe->e_sp       = 0xb8;
1777   internal_f->pe->e_csum     = 0x0;
1778   internal_f->pe->e_ip       = 0x0;
1779   internal_f->pe->e_cs       = 0x0;
1780   internal_f->pe->e_lfarlc   = 0x40;
1781   internal_f->pe->e_ovno     = 0x0;
1782   {
1783     int idx;
1784     for (idx=0; idx < 4; idx++)
1785       internal_f->pe->e_res[idx] = 0x0;
1786   }
1787   internal_f->pe->e_oemid   = 0x0;
1788   internal_f->pe->e_oeminfo = 0x0;
1789   {
1790     int idx;
1791     for (idx=0; idx < 10; idx++)
1792       internal_f->pe->e_res2[idx] = 0x0;
1793   }
1794   internal_f->pe->e_lfanew = 0x80;
1795
1796   /* this next collection of data are mostly just characters.  It appears
1797      to be constant within the headers put on NT exes */
1798   internal_f->pe->dos_message[0]  = 0x0eba1f0e;
1799   internal_f->pe->dos_message[1]  = 0xcd09b400;
1800   internal_f->pe->dos_message[2]  = 0x4c01b821;
1801   internal_f->pe->dos_message[3]  = 0x685421cd;
1802   internal_f->pe->dos_message[4]  = 0x70207369;
1803   internal_f->pe->dos_message[5]  = 0x72676f72;
1804   internal_f->pe->dos_message[6]  = 0x63206d61;
1805   internal_f->pe->dos_message[7]  = 0x6f6e6e61;
1806   internal_f->pe->dos_message[8]  = 0x65622074;
1807   internal_f->pe->dos_message[9]  = 0x6e757220;
1808   internal_f->pe->dos_message[10] = 0x206e6920;
1809   internal_f->pe->dos_message[11] = 0x20534f44;
1810   internal_f->pe->dos_message[12] = 0x65646f6d;
1811   internal_f->pe->dos_message[13] = 0x0a0d0d2e;
1812   internal_f->pe->dos_message[14] = 0x24;
1813   internal_f->pe->dos_message[15] = 0x0;
1814   internal_f->pe->nt_signature = NT_SIGNATURE;
1815
1816
1817   /* write all of the other optional header data */
1818
1819   internal_a->pe->MajorOperatingSystemVersion =
1820     pe_value (&pe_info->major_os_version, 1);
1821
1822   internal_a->pe->MinorOperatingSystemVersion =
1823     pe_value (&pe_info->minor_os_version, 0);
1824
1825   internal_a->pe->MajorImageVersion =
1826     pe_value (&pe_info->major_image_version, 1);
1827
1828   internal_a->pe->MinorImageVersion =
1829     pe_value (&pe_info->minor_image_version, 0);
1830
1831
1832   internal_a->pe->MajorSubsystemVersion =
1833     pe_value (&pe_info->major_subsystem_version, 3);
1834
1835
1836   internal_a->pe->MinorSubsystemVersion =
1837     pe_value (&pe_info->minor_subsystem_version, 10);
1838
1839   internal_a->pe->Subsystem =
1840     pe_value (&pe_info->subsystem, BFD_PE_CONSOLE);
1841
1842   /* Virtual start address, take virtual start address of last section, 
1843      add its physical size and round up the next page (NT_SECTION_ALIGNMENT).
1844      An assumption has been made that the sections stored in the abfd
1845      structure are in order and that I have successfully saved the last
1846      section's address and size. */
1847
1848   /* The headers go up to where the first section starts. */
1849
1850   internal_a->pe->SizeOfHeaders = abfd->sections->filepos;
1851   internal_a->pe->CheckSum = 0;
1852   internal_a->pe->DllCharacteristics = 0;
1853
1854   internal_a->pe->SizeOfStackReserve = pe_value (&pe_info->stack_reserve,
1855                                                  NT_DEF_RESERVE);
1856   internal_a->pe->SizeOfStackCommit = pe_value (&pe_info->stack_commit,
1857                                                 NT_DEF_COMMIT);
1858
1859   internal_a->pe->SizeOfHeapReserve = pe_value (&pe_info->heap_reserve,
1860                                                 NT_DEF_RESERVE);
1861   internal_a->pe->SizeOfHeapCommit = pe_value (&pe_info->heap_commit,
1862                                                NT_DEF_COMMIT);
1863
1864   internal_a->pe->LoaderFlags = 0;
1865   internal_a->pe->NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES; /* 0x10 */
1866
1867   /* first null out all data directory entries .. */
1868   memset (internal_a->pe->DataDirectory, sizeof (internal_a->pe->DataDirectory), 0);
1869
1870   add_data_entry (abfd, internal_a, 0, ".edata", ib);
1871   add_data_entry (abfd, internal_a, 1, ".idata", ib);
1872   add_data_entry (abfd, internal_a, 2, ".rsrc" ,ib);
1873   add_data_entry (abfd, internal_a, 5, ".reloc", ib);
1874   {
1875     asection *sec;
1876     bfd_vma dsize= 0;
1877     bfd_vma isize = SA(abfd->sections->filepos);
1878     bfd_vma tsize= 0;
1879     for (sec = abfd->sections; sec; sec = sec->next)
1880       {
1881         int rounded = FA(sec->_raw_size);
1882         if (sec->flags & SEC_DATA) 
1883           dsize += rounded;
1884         if (sec->flags & SEC_CODE)
1885           tsize += rounded;
1886         isize += SA(rounded);
1887       }
1888
1889     internal_a->dsize = dsize;
1890     internal_a->tsize = tsize;
1891     internal_a->pe->SizeOfImage = isize;
1892   }
1893
1894 }
1895 #endif
1896
1897 /* SUPPRESS 558 */
1898 /* SUPPRESS 529 */
1899 static boolean
1900 coff_write_object_contents (abfd)
1901      bfd * abfd;
1902 {
1903   asection *current;
1904   boolean hasrelocs = false;
1905   boolean haslinno = false;
1906   file_ptr reloc_base;
1907   file_ptr lineno_base;
1908   file_ptr sym_base;
1909   unsigned long reloc_size = 0;
1910   unsigned long lnno_size = 0;
1911   asection *text_sec = NULL;
1912   asection *data_sec = NULL;
1913   asection *bss_sec = NULL;
1914
1915   struct internal_filehdr internal_f;
1916   struct internal_aouthdr internal_a;
1917
1918 #ifdef COFF_IMAGE_WITH_PE
1919   struct internal_extra_pe_aouthdr extra_a;
1920   struct internal_extra_pe_filehdr extra_f;
1921   bfd_link_pe_info defs;
1922   struct bfd_link_info dummy_info;
1923   struct bfd_link_info *info ;
1924   struct bfd_link_pe_info *pe_info;
1925
1926   if (coff_data (abfd)->link_info)
1927     info =coff_data (abfd)->link_info;
1928   else 
1929     {
1930       coff_data (abfd)->link_info = info = &dummy_info;
1931       info->pe_info = 0;
1932     }
1933   pe_info = (struct bfd_link_pe_info *)(info->pe_info);
1934
1935   if (!pe_info)
1936     {
1937       /* Just use sensible defaults */
1938       memset (&defs, 0, sizeof (defs));
1939       coff_data (abfd)->link_info->pe_info = &defs;
1940     }
1941 #endif
1942
1943   bfd_set_error (bfd_error_system_call);
1944
1945   if (abfd->output_has_begun == false)
1946     coff_compute_section_file_positions (abfd);
1947
1948   reloc_base = obj_relocbase (abfd);
1949
1950   /* Make a pass through the symbol table to count line number entries and
1951      put them into the correct asections */
1952
1953   lnno_size = coff_count_linenumbers (abfd) * LINESZ;
1954
1955   /* Work out the size of the reloc and linno areas */
1956
1957   for (current = abfd->sections; current != NULL; current =
1958        current->next)
1959     reloc_size += current->reloc_count * RELSZ;
1960
1961   lineno_base = reloc_base + reloc_size;
1962   sym_base = lineno_base + lnno_size;
1963
1964   /* Indicate in each section->line_filepos its actual file address */
1965   for (current = abfd->sections; current != NULL; current =
1966        current->next)
1967     {
1968       if (current->lineno_count)
1969         {
1970           current->line_filepos = lineno_base;
1971           current->moving_line_filepos = lineno_base;
1972           lineno_base += current->lineno_count * LINESZ;
1973         }
1974       else
1975         {
1976           current->line_filepos = 0;
1977         }
1978       if (current->reloc_count)
1979         {
1980           current->rel_filepos = reloc_base;
1981           reloc_base += current->reloc_count * RELSZ;
1982         }
1983       else
1984         {
1985           current->rel_filepos = 0;
1986         }
1987     }
1988
1989   /* Write section headers to the file.  */
1990   internal_f.f_nscns = 0;
1991
1992   if (bfd_seek (abfd,
1993                 (file_ptr) ((abfd->flags & EXEC_P) ?
1994                             (FILHSZ + AOUTSZ) : FILHSZ),
1995                 SEEK_SET) != 0)
1996     return false;
1997
1998   for (current = abfd->sections;
1999        current != NULL;
2000        current = current->next)
2001     {
2002       struct internal_scnhdr section;
2003
2004 #ifdef COFF_WITH_PE
2005       /* Do not include the .junk section.  This is where we collect section
2006          data which we don't need.  This is mainly the MS .debug$ data which
2007          stores codeview debug data. */
2008       if (strcmp (current->name, ".junk") == 0)
2009         {
2010           continue;
2011         }
2012 #endif
2013       internal_f.f_nscns++;
2014       strncpy (&(section.s_name[0]), current->name, 8);
2015 #ifdef _LIB
2016       /* Always set s_vaddr of .lib to 0.  This is right for SVR3.2
2017          Ian Taylor <ian@cygnus.com>.  */
2018       if (strcmp (current->name, _LIB) == 0)
2019         section.s_vaddr = 0;
2020       else
2021 #endif
2022         section.s_vaddr = current->lma;
2023       section.s_paddr = current->lma;
2024       section.s_size = current->_raw_size;
2025
2026       /*
2027          If this section has no size or is unloadable then the scnptr
2028          will be 0 too
2029          */
2030       if (current->_raw_size == 0 ||
2031           (current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2032         {
2033           section.s_scnptr = 0;
2034         }
2035       else
2036         {
2037           section.s_scnptr = current->filepos;
2038         }
2039       section.s_relptr = current->rel_filepos;
2040       section.s_lnnoptr = current->line_filepos;
2041       section.s_nreloc = current->reloc_count;
2042       section.s_nlnno = current->lineno_count;
2043       if (current->reloc_count != 0)
2044         hasrelocs = true;
2045       if (current->lineno_count != 0)
2046         haslinno = true;
2047
2048       section.s_flags = sec_to_styp_flags (current->name, current->flags);
2049
2050       if (!strcmp (current->name, _TEXT))
2051         {
2052           text_sec = current;
2053         }
2054       else if (!strcmp (current->name, _DATA))
2055         {
2056           data_sec = current;
2057 #ifdef TWO_DATA_SECS
2058         }
2059       else if (!strcmp (current->name, ".data2"))
2060         {
2061           data_sec = current;
2062 #endif /* TWO_DATA_SECS */
2063         }
2064       else if (!strcmp (current->name, _BSS))
2065         {
2066           bss_sec = current;
2067         }
2068
2069 #ifdef I960
2070       section.s_align = (current->alignment_power
2071                          ? 1 << current->alignment_power
2072                          : 0);
2073
2074 #endif
2075
2076 #ifdef COFF_IMAGE_WITH_PE
2077       /* suppress output of the sections if they are null.  ld includes
2078          the bss and data sections even if there is no size assigned
2079          to them.  NT loader doesn't like it if these section headers are
2080          included if the sections themselves are not needed */
2081       if (section.s_size == 0)
2082         internal_f.f_nscns--;
2083       else
2084 #endif
2085         {
2086           SCNHDR buff;
2087           if (coff_swap_scnhdr_out (abfd, &section, &buff) == 0
2088               || bfd_write ((PTR) (&buff), 1, SCNHSZ, abfd) != SCNHSZ)
2089             return false;
2090         }
2091     }
2092
2093
2094
2095   /* OK, now set up the filehdr... */
2096
2097   /* Don't include the internal abs section in the section count */
2098
2099   /*
2100      We will NOT put a fucking timestamp in the header here. Every time you
2101      put it back, I will come in and take it out again.  I'm sorry.  This
2102      field does not belong here.  We fill it with a 0 so it compares the
2103      same but is not a reasonable time. -- gnu@cygnus.com
2104      */
2105
2106   internal_f.f_flags = 0;
2107
2108   if (abfd->flags & EXEC_P)
2109     internal_f.f_opthdr = AOUTSZ;
2110   else
2111     internal_f.f_opthdr = 0;
2112
2113   if (!hasrelocs)
2114     internal_f.f_flags |= F_RELFLG;
2115   if (!haslinno)
2116     internal_f.f_flags |= F_LNNO;
2117   if (abfd->flags & EXEC_P)
2118     internal_f.f_flags |= F_EXEC;
2119
2120   if (!abfd->xvec->byteorder_big_p)
2121     internal_f.f_flags |= F_AR32WR;
2122   else
2123     internal_f.f_flags |= F_AR32W;
2124
2125   /*
2126      FIXME, should do something about the other byte orders and
2127      architectures.
2128      */
2129
2130   memset (&internal_a, 0, sizeof internal_a);
2131
2132   /* Set up architecture-dependent stuff */
2133
2134   {
2135     unsigned int magic = 0;
2136     unsigned short flags = 0;
2137     coff_set_flags (abfd, &magic, &flags);
2138     internal_f.f_magic = magic;
2139     internal_f.f_flags |= flags;
2140     /* ...and the "opt"hdr... */
2141
2142 #ifdef A29K
2143 #ifdef ULTRA3                   /* NYU's machine */
2144     /* FIXME: This is a bogus check.  I really want to see if there
2145      * is a .shbss or a .shdata section, if so then set the magic
2146      * number to indicate a shared data executable.
2147      */
2148     if (internal_f.f_nscns >= 7)
2149       internal_a.magic = SHMAGIC; /* Shared magic */
2150     else
2151 #endif /* ULTRA3 */
2152       internal_a.magic = NMAGIC; /* Assume separate i/d */
2153 #define __A_MAGIC_SET__
2154 #endif /* A29K */
2155 #ifdef I960
2156     internal_a.magic = (magic == I960ROMAGIC ? NMAGIC : OMAGIC);
2157 #define __A_MAGIC_SET__
2158 #endif /* I960 */
2159 #if M88
2160 #define __A_MAGIC_SET__
2161     internal_a.magic = PAGEMAGICBCS;
2162 #endif /* M88 */
2163
2164 #if APOLLO_M68
2165 #define __A_MAGIC_SET__
2166     internal_a.magic = APOLLO_COFF_VERSION_NUMBER;
2167 #endif
2168
2169 #if defined(M68) || defined(WE32K) || defined(M68K)
2170 #define __A_MAGIC_SET__
2171 #if defined(LYNXOS)
2172     internal_a.magic = LYNXCOFFMAGIC;
2173 #endif /* LYNXOS */
2174 #endif /* M68 || WE32K || M68K */
2175
2176 #if defined(ARM)
2177 #define __A_MAGIC_SET__
2178     internal_a.magic = ZMAGIC;
2179 #endif 
2180 #if defined(I386)
2181 #define __A_MAGIC_SET__
2182 #if defined(LYNXOS)
2183     internal_a.magic = LYNXCOFFMAGIC;
2184 #else  /* LYNXOS */
2185     internal_a.magic = ZMAGIC;
2186 #endif /* LYNXOS */
2187 #endif /* I386 */
2188
2189 #if defined(SPARC)
2190 #define __A_MAGIC_SET__
2191 #if defined(LYNXOS)
2192     internal_a.magic = LYNXCOFFMAGIC;
2193 #endif /* LYNXOS */
2194 #endif /* SPARC */
2195
2196 #if RS6000COFF_C
2197 #define __A_MAGIC_SET__
2198     internal_a.magic = (abfd->flags & D_PAGED) ? RS6K_AOUTHDR_ZMAGIC :
2199     (abfd->flags & WP_TEXT) ? RS6K_AOUTHDR_NMAGIC :
2200     RS6K_AOUTHDR_OMAGIC;
2201 #endif
2202
2203 #ifndef __A_MAGIC_SET__
2204 #include "Your aouthdr magic number is not being set!"
2205 #else
2206 #undef __A_MAGIC_SET__
2207 #endif
2208   }
2209   /* Now should write relocs, strings, syms */
2210   obj_sym_filepos (abfd) = sym_base;
2211
2212   if (bfd_get_symcount (abfd) != 0)
2213     {
2214       int firstundef;
2215 #ifndef RS6000COFF_C
2216       if (!coff_add_missing_symbols (abfd))
2217         return false;
2218 #endif
2219       if (!coff_renumber_symbols (abfd, &firstundef))
2220         return false;
2221       coff_mangle_symbols (abfd);
2222       if (! coff_write_symbols (abfd))
2223         return false;
2224       if (! coff_write_linenumbers (abfd))
2225         return false;
2226       if (! coff_write_relocs (abfd, firstundef))
2227         return false;
2228     }
2229
2230   /* If bfd_get_symcount (abfd) != 0, then we are not using the COFF
2231      backend linker, and obj_raw_syment_count is not valid until after
2232      coff_write_symbols is called.  */
2233   if (obj_raw_syment_count (abfd) != 0)
2234     internal_f.f_symptr = sym_base;
2235   else
2236     {
2237       internal_f.f_symptr = 0;
2238       internal_f.f_flags |= F_LSYMS;
2239     }
2240
2241   if (text_sec)
2242     {
2243       internal_a.tsize = bfd_get_section_size_before_reloc (text_sec);
2244       internal_a.text_start = internal_a.tsize ? text_sec->vma : 0;
2245     }
2246   if (data_sec)
2247     {
2248       internal_a.dsize = bfd_get_section_size_before_reloc (data_sec);
2249       internal_a.data_start = internal_a.dsize ? data_sec->vma : 0;
2250     }
2251   if (bss_sec)
2252     {
2253       internal_a.bsize = bfd_get_section_size_before_reloc (bss_sec);
2254     }
2255
2256   internal_a.entry = bfd_get_start_address (abfd);
2257   internal_f.f_nsyms = obj_raw_syment_count (abfd);
2258
2259
2260
2261 #ifdef COFF_IMAGE_WITH_PE
2262
2263   internal_f.pe = & extra_f;
2264   internal_a.pe = & extra_a;
2265
2266   fill_pe_header_info (abfd, &internal_f, &internal_a);
2267 #endif
2268
2269   /* now write them */
2270   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
2271     return false;
2272   {
2273     FILHDR buff;
2274     coff_swap_filehdr_out (abfd, (PTR) & internal_f, (PTR) & buff);
2275     if (bfd_write ((PTR) & buff, 1, FILHSZ, abfd) != FILHSZ)
2276       return false;
2277   }
2278   if (abfd->flags & EXEC_P)
2279     {
2280       AOUTHDR buff;
2281       coff_swap_aouthdr_out (abfd, (PTR) & internal_a, (PTR) & buff);
2282       if (bfd_write ((PTR) & buff, 1, AOUTSZ, abfd) != AOUTSZ)
2283         return false;
2284     }
2285
2286   return true;
2287 }
2288
2289 static boolean
2290 coff_set_section_contents (abfd, section, location, offset, count)
2291      bfd * abfd;
2292      sec_ptr section;
2293      PTR location;
2294      file_ptr offset;
2295      bfd_size_type count;
2296 {
2297   if (abfd->output_has_begun == false)  /* set by bfd.c handler */
2298     coff_compute_section_file_positions (abfd);
2299
2300 #ifdef _LIB
2301   /* If this is a .lib section, bump the vma address so that it
2302        winds up being the number of .lib sections output.  This is
2303        right for SVR3.2.  Shared libraries should probably get more
2304        generic support.  Ian Taylor <ian@cygnus.com>.  */
2305   if (strcmp (section->name, _LIB) == 0)
2306     ++section->lma;
2307 #endif
2308
2309   /* Don't write out bss sections - one way to do this is to
2310        see if the filepos has not been set. */
2311   if (section->filepos == 0)
2312     return true;
2313
2314   if (bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET) != 0)
2315     return false;
2316
2317   if (count != 0)
2318     {
2319       return (bfd_write (location, 1, count, abfd) == count) ? true : false;
2320     }
2321   return true;
2322 }
2323 #if 0
2324 static boolean
2325 coff_close_and_cleanup (abfd)
2326      bfd *abfd;
2327 {
2328   if (!bfd_read_p (abfd))
2329     switch (abfd->format)
2330       {
2331       case bfd_archive:
2332         if (!_bfd_write_archive_contents (abfd))
2333           return false;
2334         break;
2335       case bfd_object:
2336         if (!coff_write_object_contents (abfd))
2337           return false;
2338         break;
2339       default:
2340         bfd_set_error (bfd_error_invalid_operation);
2341         return false;
2342       }
2343
2344   /* We depend on bfd_close to free all the memory on the obstack.  */
2345   /* FIXME if bfd_release is not using obstacks! */
2346   return true;
2347 }
2348
2349 #endif
2350
2351 static PTR
2352 buy_and_read (abfd, where, seek_direction, size)
2353      bfd *abfd;
2354      file_ptr where;
2355      int seek_direction;
2356      size_t size;
2357 {
2358   PTR area = (PTR) bfd_alloc (abfd, size);
2359   if (!area)
2360     {
2361       bfd_set_error (bfd_error_no_memory);
2362       return (NULL);
2363     }
2364   if (bfd_seek (abfd, where, seek_direction) != 0
2365       || bfd_read (area, 1, size, abfd) != size)
2366     return (NULL);
2367   return (area);
2368 }                               /* buy_and_read() */
2369
2370 /*
2371 SUBSUBSECTION
2372         Reading linenumbers
2373
2374         Creating the linenumber table is done by reading in the entire
2375         coff linenumber table, and creating another table for internal use.
2376
2377         A coff linenumber table is structured so that each function
2378         is marked as having a line number of 0. Each line within the
2379         function is an offset from the first line in the function. The
2380         base of the line number information for the table is stored in
2381         the symbol associated with the function.
2382
2383         The information is copied from the external to the internal
2384         table, and each symbol which marks a function is marked by
2385         pointing its...
2386
2387         How does this work ?
2388
2389 */
2390
2391 static boolean
2392 coff_slurp_line_table (abfd, asect)
2393      bfd *abfd;
2394      asection *asect;
2395 {
2396   LINENO *native_lineno;
2397   alent *lineno_cache;
2398
2399   BFD_ASSERT (asect->lineno == (alent *) NULL);
2400
2401   native_lineno = (LINENO *) buy_and_read (abfd,
2402                                            asect->line_filepos,
2403                                            SEEK_SET,
2404                                            (size_t) (LINESZ *
2405                                                      asect->lineno_count));
2406   lineno_cache =
2407     (alent *) bfd_alloc (abfd, (size_t) ((asect->lineno_count + 1) * sizeof (alent)));
2408   if (lineno_cache == NULL)
2409     {
2410       bfd_set_error (bfd_error_no_memory);
2411       return false;
2412     }
2413   else
2414     {
2415       unsigned int counter = 0;
2416       alent *cache_ptr = lineno_cache;
2417       LINENO *src = native_lineno;
2418
2419       while (counter < asect->lineno_count)
2420         {
2421           struct internal_lineno dst;
2422           coff_swap_lineno_in (abfd, src, &dst);
2423           cache_ptr->line_number = dst.l_lnno;
2424
2425           if (cache_ptr->line_number == 0)
2426             {
2427               coff_symbol_type *sym =
2428               (coff_symbol_type *) (dst.l_addr.l_symndx
2429                       + obj_raw_syments (abfd))->u.syment._n._n_n._n_zeroes;
2430               cache_ptr->u.sym = (asymbol *) sym;
2431               sym->lineno = cache_ptr;
2432             }
2433           else
2434             {
2435               cache_ptr->u.offset = dst.l_addr.l_paddr
2436                 - bfd_section_vma (abfd, asect);
2437             }                   /* If no linenumber expect a symbol index */
2438
2439           cache_ptr++;
2440           src++;
2441           counter++;
2442         }
2443       cache_ptr->line_number = 0;
2444
2445     }
2446   asect->lineno = lineno_cache;
2447   /* FIXME, free native_lineno here, or use alloca or something. */
2448   return true;
2449 }
2450
2451 static boolean
2452 coff_slurp_symbol_table (abfd)
2453      bfd * abfd;
2454 {
2455   combined_entry_type *native_symbols;
2456   coff_symbol_type *cached_area;
2457   unsigned int *table_ptr;
2458
2459   unsigned int number_of_symbols = 0;
2460
2461   if (obj_symbols (abfd))
2462     return true;
2463
2464   /* Read in the symbol table */
2465   if ((native_symbols = coff_get_normalized_symtab (abfd)) == NULL)
2466     {
2467       return (false);
2468     }                           /* on error */
2469
2470   /* Allocate enough room for all the symbols in cached form */
2471   cached_area = ((coff_symbol_type *)
2472                  bfd_alloc (abfd,
2473                             (obj_raw_syment_count (abfd)
2474                              * sizeof (coff_symbol_type))));
2475
2476   if (cached_area == NULL)
2477     {
2478       bfd_set_error (bfd_error_no_memory);
2479       return false;
2480     }                           /* on error */
2481   table_ptr = ((unsigned int *)
2482                bfd_alloc (abfd,
2483                           (obj_raw_syment_count (abfd)
2484                            * sizeof (unsigned int))));
2485
2486   if (table_ptr == NULL)
2487     {
2488       bfd_set_error (bfd_error_no_memory);
2489       return false;
2490     }
2491   else
2492     {
2493       coff_symbol_type *dst = cached_area;
2494       unsigned int last_native_index = obj_raw_syment_count (abfd);
2495       unsigned int this_index = 0;
2496       while (this_index < last_native_index)
2497         {
2498           combined_entry_type *src = native_symbols + this_index;
2499           table_ptr[this_index] = number_of_symbols;
2500           dst->symbol.the_bfd = abfd;
2501
2502           dst->symbol.name = (char *) (src->u.syment._n._n_n._n_offset);
2503           /* We use the native name field to point to the cached field.  */
2504           src->u.syment._n._n_n._n_zeroes = (long) dst;
2505           dst->symbol.section = coff_section_from_bfd_index (abfd,
2506                                                      src->u.syment.n_scnum);
2507           dst->symbol.flags = 0;
2508           dst->done_lineno = false;
2509
2510           switch (src->u.syment.n_sclass)
2511             {
2512 #ifdef I960
2513             case C_LEAFEXT:
2514 #if 0
2515               dst->symbol.value = src->u.syment.n_value - dst->symbol.section->vma;
2516               dst->symbol.flags = BSF_EXPORT | BSF_GLOBAL;
2517               dst->symbol.flags |= BSF_NOT_AT_END | BSF_FUNCTION;
2518 #endif
2519               /* Fall through to next case */
2520
2521 #endif
2522
2523             case C_EXT:
2524 #ifdef RS6000COFF_C
2525             case C_HIDEXT:
2526 #endif
2527 #ifdef COFF_WITH_PE
2528             /* PE uses storage class 0x68 to denote a section symbol */
2529             case C_SECTION:
2530 #endif
2531               if ((src->u.syment.n_scnum) == 0)
2532                 {
2533                   if ((src->u.syment.n_value) == 0)
2534                     {
2535                       dst->symbol.section = bfd_und_section_ptr;
2536                       dst->symbol.value = 0;
2537                     }
2538                   else
2539                     {
2540                       dst->symbol.section = bfd_com_section_ptr;
2541                       dst->symbol.value = (src->u.syment.n_value);
2542                     }
2543                 }
2544               else
2545                 {
2546                   /*
2547             Base the value as an index from the base of the
2548             section
2549             */
2550
2551                   dst->symbol.flags = BSF_EXPORT | BSF_GLOBAL;
2552                   dst->symbol.value = src->u.syment.n_value - dst->symbol.section->vma;
2553
2554                   if (ISFCN ((src->u.syment.n_type)))
2555                     {
2556                       /*
2557               A function ext does not go at the end of a file
2558               */
2559                       dst->symbol.flags |= BSF_NOT_AT_END | BSF_FUNCTION;
2560                     }
2561                 }
2562
2563 #ifdef RS6000COFF_C
2564               /* If this symbol has a csect aux of type LD, the scnlen field
2565            is actually the index of the containing csect symbol.  We
2566            need to pointerize it.  */
2567               if (src->u.syment.n_numaux > 0)
2568                 {
2569                   combined_entry_type *aux;
2570
2571                   aux = src + src->u.syment.n_numaux - 1;
2572                   if (SMTYP_SMTYP (aux->u.auxent.x_csect.x_smtyp) == XTY_LD)
2573                     {
2574                       aux->u.auxent.x_csect.x_scnlen.p =
2575                         native_symbols + aux->u.auxent.x_csect.x_scnlen.l;
2576                       aux->fix_scnlen = 1;
2577                     }
2578                 }
2579 #endif
2580
2581               break;
2582
2583             case C_STAT:        /* static                        */
2584 #ifdef I960
2585             case C_LEAFSTAT:    /* static leaf procedure        */
2586 #endif
2587             case C_LABEL:       /* label                         */
2588               if (src->u.syment.n_scnum == -2)
2589                 dst->symbol.flags = BSF_DEBUGGING;
2590               else
2591                 dst->symbol.flags = BSF_LOCAL;
2592               /*
2593           Base the value as an index from the base of the section, if
2594           there is one
2595           */
2596               if (dst->symbol.section)
2597                 dst->symbol.value = (src->u.syment.n_value) -
2598                   dst->symbol.section->vma;
2599               else
2600                 dst->symbol.value = (src->u.syment.n_value);
2601               break;
2602
2603             case C_MOS: /* member of structure   */
2604             case C_EOS: /* end of structure              */
2605 #ifdef NOTDEF                   /* C_AUTOARG has the same value */
2606 #ifdef C_GLBLREG
2607             case C_GLBLREG:     /* A29k-specific storage class */
2608 #endif
2609 #endif
2610             case C_REGPARM:     /* register parameter            */
2611             case C_REG: /* register variable             */
2612 #ifdef C_AUTOARG
2613             case C_AUTOARG:     /* 960-specific storage class */
2614 #endif
2615             case C_TPDEF:       /* type definition               */
2616             case C_ARG:
2617             case C_AUTO:        /* automatic variable */
2618             case C_FIELD:       /* bit field */
2619             case C_ENTAG:       /* enumeration tag               */
2620             case C_MOE: /* member of enumeration         */
2621             case C_MOU: /* member of union               */
2622             case C_UNTAG:       /* union tag                     */
2623               dst->symbol.flags = BSF_DEBUGGING;
2624               dst->symbol.value = (src->u.syment.n_value);
2625               break;
2626
2627             case C_FILE:        /* file name                     */
2628             case C_STRTAG:      /* structure tag                 */
2629 #ifdef RS6000COFF_C
2630             case C_BINCL:       /* beginning of include file     */
2631             case C_EINCL:       /* ending of include file        */
2632             case C_GSYM:
2633             case C_LSYM:
2634             case C_PSYM:
2635             case C_RSYM:
2636             case C_RPSYM:
2637             case C_STSYM:
2638             case C_DECL:
2639             case C_ENTRY:
2640             case C_FUN:
2641             case C_ESTAT:
2642 #endif
2643               dst->symbol.flags = BSF_DEBUGGING;
2644               dst->symbol.value = (src->u.syment.n_value);
2645               break;
2646
2647 #ifdef RS6000COFF_C
2648             case C_BSTAT:
2649               dst->symbol.flags = BSF_DEBUGGING;
2650               dst->symbol.value = src->u.syment.n_value;
2651
2652               /* The value is actually a symbol index.  Save a pointer to
2653            the symbol instead of the index.  FIXME: This should use a
2654            union.  */
2655               src->u.syment.n_value =
2656                 (long) (native_symbols + src->u.syment.n_value);
2657               src->fix_value = 1;
2658               break;
2659 #endif
2660
2661             case C_BLOCK:       /* ".bb" or ".eb"                */
2662             case C_FCN: /* ".bf" or ".ef"                */
2663             case C_EFCN:        /* physical end of function      */
2664               dst->symbol.flags = BSF_LOCAL;
2665               /*
2666           Base the value as an index from the base of the section
2667           */
2668               dst->symbol.value = (src->u.syment.n_value) - dst->symbol.section->vma;
2669               break;
2670
2671             case C_NULL:
2672             case C_EXTDEF:      /* external definition           */
2673             case C_ULABEL:      /* undefined label               */
2674             case C_USTATIC:     /* undefined static              */
2675 #ifndef COFF_WITH_PE
2676             /* C_LINE in regular coff is 0x68.  NT has taken over this storage
2677                class to represent a section symbol */
2678             case C_LINE:        /* line # reformatted as symbol table entry */
2679 #endif
2680             case C_ALIAS:       /* duplicate tag                 */
2681             case C_HIDDEN:      /* ext symbol in dmert public lib */
2682             default:
2683
2684               fprintf (stderr, "Unrecognized storage class %d (assuming debugging)\n  for %s symbol `%s'\n",
2685                        src->u.syment.n_sclass, dst->symbol.section->name,
2686                        dst->symbol.name);
2687 /*      abort();*/
2688               dst->symbol.flags = BSF_DEBUGGING;
2689               dst->symbol.value = (src->u.syment.n_value);
2690               break;
2691             }
2692
2693 /*      BFD_ASSERT(dst->symbol.flags != 0);*/
2694
2695           dst->native = src;
2696
2697           dst->symbol.udata.i = 0;
2698           dst->lineno = (alent *) NULL;
2699           this_index += (src->u.syment.n_numaux) + 1;
2700           dst++;
2701           number_of_symbols++;
2702         }                       /* walk the native symtab */
2703     }                           /* bfdize the native symtab */
2704
2705   obj_symbols (abfd) = cached_area;
2706   obj_raw_syments (abfd) = native_symbols;
2707
2708   bfd_get_symcount (abfd) = number_of_symbols;
2709   obj_convert (abfd) = table_ptr;
2710   /* Slurp the line tables for each section too */
2711   {
2712     asection *p;
2713     p = abfd->sections;
2714     while (p)
2715       {
2716         coff_slurp_line_table (abfd, p);
2717         p = p->next;
2718       }
2719   }
2720   return true;
2721 }                               /* coff_slurp_symbol_table() */
2722
2723 /* Check whether a symbol is globally visible.  This is used by the
2724    COFF backend linker code in cofflink.c, since a couple of targets
2725    have globally visible symbols which are not class C_EXT.  This
2726    function need not handle the case of n_class == C_EXT.  */
2727
2728 #undef OTHER_GLOBAL_CLASS
2729
2730 #ifdef I960
2731 #define OTHER_GLOBAL_CLASS C_LEAFEXT
2732 #endif
2733
2734 #ifdef RS6000COFF_C
2735 #define OTHER_GLOBAL_CLASS C_HIDEXT
2736 #endif
2737
2738 #ifdef COFF_WITH_PE
2739 #define OTHER_GLOBAL_CLASS C_SECTION
2740 #endif
2741
2742 #ifdef OTHER_GLOBAL_CLASS
2743
2744 static boolean
2745 coff_sym_is_global (abfd, syment)
2746      bfd *abfd;
2747      struct internal_syment *syment;
2748 {
2749   if (syment->n_sclass == OTHER_GLOBAL_CLASS)
2750     return true;
2751   return false;
2752 }
2753
2754 #undef OTHER_GLOBAL_CLASS
2755
2756 #else /* ! defined (OTHER_GLOBAL_CLASS) */
2757
2758 /* sym_is_global should not be defined if it has nothing to do.  */
2759
2760 #define coff_sym_is_global 0
2761
2762 #endif /* ! defined (OTHER_GLOBAL_CLASS) */
2763
2764 /*
2765 SUBSUBSECTION
2766         Reading relocations
2767
2768         Coff relocations are easily transformed into the internal BFD form
2769         (@code{arelent}).
2770
2771         Reading a coff relocation table is done in the following stages:
2772
2773         o Read the entire coff relocation table into memory.
2774
2775         o Process each relocation in turn; first swap it from the
2776         external to the internal form.
2777
2778         o Turn the symbol referenced in the relocation's symbol index
2779         into a pointer into the canonical symbol table.
2780         This table is the same as the one returned by a call to
2781         @code{bfd_canonicalize_symtab}. The back end will call that
2782         routine and save the result if a canonicalization hasn't been done.
2783
2784         o The reloc index is turned into a pointer to a howto
2785         structure, in a back end specific way. For instance, the 386
2786         and 960 use the @code{r_type} to directly produce an index
2787         into a howto table vector; the 88k subtracts a number from the
2788         @code{r_type} field and creates an addend field.
2789
2790
2791 */
2792
2793 #ifndef CALC_ADDEND
2794 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr)                \
2795   {                                                             \
2796     coff_symbol_type *coffsym = (coff_symbol_type *) NULL;      \
2797     if (ptr && bfd_asymbol_bfd (ptr) != abfd)                   \
2798       coffsym = (obj_symbols (abfd)                             \
2799                  + (cache_ptr->sym_ptr_ptr - symbols));         \
2800     else if (ptr)                                               \
2801       coffsym = coff_symbol_from (abfd, ptr);                   \
2802     if (coffsym != (coff_symbol_type *) NULL                    \
2803         && coffsym->native->u.syment.n_scnum == 0)              \
2804       cache_ptr->addend = 0;                                    \
2805     else if (ptr && bfd_asymbol_bfd (ptr) == abfd               \
2806              && ptr->section != (asection *) NULL)              \
2807       cache_ptr->addend = - (ptr->section->vma + ptr->value);   \
2808     else                                                        \
2809       cache_ptr->addend = 0;                                    \
2810   }
2811 #endif
2812
2813 static boolean
2814 coff_slurp_reloc_table (abfd, asect, symbols)
2815      bfd * abfd;
2816      sec_ptr asect;
2817      asymbol ** symbols;
2818 {
2819   RELOC *native_relocs;
2820   arelent *reloc_cache;
2821   arelent *cache_ptr;
2822
2823   unsigned int idx;
2824
2825   if (asect->relocation)
2826     return true;
2827   if (asect->reloc_count == 0)
2828     return true;
2829   if (asect->flags & SEC_CONSTRUCTOR)
2830     return true;
2831   if (!coff_slurp_symbol_table (abfd))
2832     return false;
2833   native_relocs =
2834     (RELOC *) buy_and_read (abfd,
2835                             asect->rel_filepos,
2836                             SEEK_SET,
2837                             (size_t) (RELSZ *
2838                                       asect->reloc_count));
2839   reloc_cache = (arelent *)
2840     bfd_alloc (abfd, (size_t) (asect->reloc_count * sizeof (arelent)));
2841
2842   if (reloc_cache == NULL)
2843     {
2844       bfd_set_error (bfd_error_no_memory);
2845       return false;
2846     }
2847
2848
2849   for (idx = 0; idx < asect->reloc_count; idx++)
2850     {
2851 #ifdef RELOC_PROCESSING
2852       struct internal_reloc dst;
2853       struct external_reloc *src;
2854
2855       cache_ptr = reloc_cache + idx;
2856       src = native_relocs + idx;
2857       coff_swap_reloc_in (abfd, src, &dst);
2858
2859       RELOC_PROCESSING (cache_ptr, &dst, symbols, abfd, asect);
2860 #else
2861       struct internal_reloc dst;
2862       asymbol *ptr;
2863       struct external_reloc *src;
2864
2865       cache_ptr = reloc_cache + idx;
2866       src = native_relocs + idx;
2867
2868       coff_swap_reloc_in (abfd, src, &dst);
2869
2870
2871       cache_ptr->address = dst.r_vaddr;
2872
2873       if (dst.r_symndx != -1)
2874         {
2875           /* @@ Should never be greater than count of symbols!  */
2876           if (dst.r_symndx >= obj_conv_table_size (abfd))
2877             abort ();
2878           cache_ptr->sym_ptr_ptr = symbols + obj_convert (abfd)[dst.r_symndx];
2879           ptr = *(cache_ptr->sym_ptr_ptr);
2880         }
2881       else
2882         {
2883           cache_ptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
2884           ptr = 0;
2885         }
2886
2887       /* The symbols definitions that we have read in have been
2888          relocated as if their sections started at 0. But the offsets
2889          refering to the symbols in the raw data have not been
2890          modified, so we have to have a negative addend to compensate.
2891
2892          Note that symbols which used to be common must be left alone */
2893
2894       /* Calculate any reloc addend by looking at the symbol */
2895       CALC_ADDEND (abfd, ptr, dst, cache_ptr);
2896
2897       cache_ptr->address -= asect->vma;
2898 /* !!     cache_ptr->section = (asection *) NULL;*/
2899
2900       /* Fill in the cache_ptr->howto field from dst.r_type */
2901       RTYPE2HOWTO (cache_ptr, &dst);
2902 #endif
2903
2904     }
2905
2906   asect->relocation = reloc_cache;
2907   return true;
2908 }
2909
2910 #ifndef coff_rtype_to_howto
2911 #ifdef RTYPE2HOWTO
2912
2913 /* Get the howto structure for a reloc.  This is only used if the file
2914    including this one defines coff_relocate_section to be
2915    _bfd_coff_generic_relocate_section, so it is OK if it does not
2916    always work.  It is the responsibility of the including file to
2917    make sure it is reasonable if it is needed.  */
2918
2919 static reloc_howto_type *coff_rtype_to_howto
2920   PARAMS ((bfd *, asection *, struct internal_reloc *,
2921            struct coff_link_hash_entry *, struct internal_syment *,
2922            bfd_vma *));
2923
2924 /*ARGSUSED*/
2925 static reloc_howto_type *
2926 coff_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
2927      bfd *abfd;
2928      asection *sec;
2929      struct internal_reloc *rel;
2930      struct coff_link_hash_entry *h;
2931      struct internal_syment *sym;
2932      bfd_vma *addendp;
2933 {
2934   arelent genrel;
2935
2936   RTYPE2HOWTO (&genrel, rel);
2937   return genrel.howto;
2938 }
2939
2940 #else /* ! defined (RTYPE2HOWTO) */
2941
2942 #define coff_rtype_to_howto NULL
2943
2944 #endif /* ! defined (RTYPE2HOWTO) */
2945 #endif /* ! defined (coff_rtype_to_howto) */
2946
2947 /* This is stupid.  This function should be a boolean predicate.  */
2948 static long
2949 coff_canonicalize_reloc (abfd, section, relptr, symbols)
2950      bfd * abfd;
2951      sec_ptr section;
2952      arelent ** relptr;
2953      asymbol ** symbols;
2954 {
2955   arelent *tblptr = section->relocation;
2956   unsigned int count = 0;
2957
2958
2959   if (section->flags & SEC_CONSTRUCTOR)
2960     {
2961       /* this section has relocs made up by us, they are not in the
2962        file, so take them out of their chain and place them into
2963        the data area provided */
2964       arelent_chain *chain = section->constructor_chain;
2965       for (count = 0; count < section->reloc_count; count++)
2966         {
2967           *relptr++ = &chain->relent;
2968           chain = chain->next;
2969         }
2970
2971     }
2972   else
2973     {
2974       if (! coff_slurp_reloc_table (abfd, section, symbols))
2975         return -1;
2976
2977       tblptr = section->relocation;
2978
2979       for (; count++ < section->reloc_count;)
2980         *relptr++ = tblptr++;
2981
2982
2983     }
2984   *relptr = 0;
2985   return section->reloc_count;
2986 }
2987
2988 #ifdef GNU960
2989 file_ptr
2990 coff_sym_filepos (abfd)
2991      bfd *abfd;
2992 {
2993   return obj_sym_filepos (abfd);
2994 }
2995 #endif
2996
2997 #ifndef coff_reloc16_estimate
2998 #define coff_reloc16_estimate dummy_reloc16_estimate
2999
3000 static int
3001 dummy_reloc16_estimate (abfd, input_section, reloc, shrink, link_info)
3002      bfd *abfd;
3003      asection *input_section;
3004      arelent *reloc;
3005      unsigned int shrink;
3006      struct bfd_link_info *link_info;
3007 {
3008   abort ();
3009 }
3010
3011 #endif
3012
3013 #ifndef coff_reloc16_extra_cases
3014 #define coff_reloc16_extra_cases dummy_reloc16_extra_cases
3015 /* This works even if abort is not declared in any header file.  */
3016 static void
3017 dummy_reloc16_extra_cases (abfd, link_info, link_order, reloc, data, src_ptr,
3018                            dst_ptr)
3019      bfd *abfd;
3020      struct bfd_link_info *link_info;
3021      struct bfd_link_order *link_order;
3022      arelent *reloc;
3023      bfd_byte *data;
3024      unsigned int *src_ptr;
3025      unsigned int *dst_ptr;
3026 {
3027   fprintf (stderr, "%s\n", reloc->howto->name);
3028   abort ();
3029 }
3030 #endif
3031
3032 /* If coff_relocate_section is defined, we can use the optimized COFF
3033    backend linker.  Otherwise we must continue to use the old linker.  */
3034 #ifdef coff_relocate_section
3035 #ifndef coff_bfd_link_hash_table_create
3036 #define coff_bfd_link_hash_table_create _bfd_coff_link_hash_table_create
3037 #endif
3038 #ifndef coff_bfd_link_add_symbols
3039 #define coff_bfd_link_add_symbols _bfd_coff_link_add_symbols
3040 #endif
3041 #ifndef coff_bfd_final_link
3042 #define coff_bfd_final_link _bfd_coff_final_link
3043 #endif
3044 #else /* ! defined (coff_relocate_section) */
3045 #define coff_relocate_section NULL
3046 #define coff_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
3047 #define coff_bfd_link_add_symbols _bfd_generic_link_add_symbols
3048 #define coff_bfd_final_link _bfd_generic_final_link
3049 #endif /* ! defined (coff_relocate_section) */
3050 #define coff_bfd_link_split_section  _bfd_generic_link_split_section
3051
3052 #ifndef coff_adjust_symndx
3053 #define coff_adjust_symndx NULL
3054 #endif
3055
3056 static CONST bfd_coff_backend_data bfd_coff_std_swap_table =
3057 {
3058   coff_swap_aux_in, coff_swap_sym_in, coff_swap_lineno_in,
3059   coff_swap_aux_out, coff_swap_sym_out,
3060   coff_swap_lineno_out, coff_swap_reloc_out,
3061   coff_swap_filehdr_out, coff_swap_aouthdr_out,
3062   coff_swap_scnhdr_out,
3063   FILHSZ, AOUTSZ, SCNHSZ, SYMESZ, AUXESZ, RELSZ, LINESZ,
3064 #ifdef COFF_LONG_FILENAMES
3065   true,
3066 #else
3067   false,
3068 #endif
3069   coff_swap_filehdr_in, coff_swap_aouthdr_in, coff_swap_scnhdr_in,
3070   coff_swap_reloc_in, coff_bad_format_hook, coff_set_arch_mach_hook,
3071   coff_mkobject_hook, styp_to_sec_flags, coff_make_section_hook,
3072   coff_set_alignment_hook, coff_slurp_symbol_table, symname_in_debug_hook,
3073   coff_reloc16_extra_cases, coff_reloc16_estimate,
3074   coff_sym_is_global, coff_compute_section_file_positions,
3075   coff_relocate_section, coff_rtype_to_howto, coff_adjust_symndx
3076 };
3077
3078 #define coff_close_and_cleanup _bfd_generic_close_and_cleanup
3079 #define coff_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
3080 #define coff_get_section_contents _bfd_generic_get_section_contents
3081
3082 #define coff_bfd_copy_private_symbol_data \
3083   _bfd_generic_bfd_copy_private_symbol_data
3084 #define coff_bfd_copy_private_section_data \
3085   _bfd_generic_bfd_copy_private_section_data
3086 #define coff_bfd_copy_private_bfd_data _bfd_generic_bfd_copy_private_bfd_data
3087 #define coff_bfd_merge_private_bfd_data _bfd_generic_bfd_merge_private_bfd_data
3088 #define coff_bfd_set_private_flags _bfd_generic_bfd_set_private_flags
3089
3090 #ifndef coff_bfd_is_local_label
3091 #define coff_bfd_is_local_label bfd_generic_is_local_label
3092 #endif
3093 #ifndef coff_read_minisymbols
3094 #define coff_read_minisymbols _bfd_generic_read_minisymbols
3095 #endif
3096 #ifndef coff_minisymbol_to_symbol
3097 #define coff_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
3098 #endif
3099
3100 /* The reloc lookup routine must be supplied by each individual COFF
3101    backend.  */
3102 #ifndef coff_bfd_reloc_type_lookup
3103 #define coff_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
3104 #endif
3105
3106 #ifndef coff_bfd_get_relocated_section_contents
3107 #define coff_bfd_get_relocated_section_contents \
3108   bfd_generic_get_relocated_section_contents
3109 #endif
3110 #ifndef coff_bfd_relax_section
3111 #define coff_bfd_relax_section bfd_generic_relax_section
3112 #endif