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