z8kgen: temp file to generate z8k-opc.h from
[external/binutils.git] / bfd / coffcode.h
1 /* Support for the generic parts of most COFF variants, for BFD.
2    Copyright (C) 1990-1991 Free Software Foundation, Inc.
3    Written by Cygnus Support.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
20
21 /* 
22 Most of this hacked by  Steve Chamberlain,
23                         sac@cygnus.com 
24 */
25 /*
26
27 SECTION
28         coff backends
29
30         BFD supports a number of different flavours of coff format.
31         The major difference between formats are the sizes and
32         alignments of fields in structures on disk, and the occasional
33         extra field.  
34
35         Coff in all its varieties is implimented with a few common
36         files and a number of implementation specific files. For
37         example, The 88k bcs coff format is implemented in the file
38         @code{coff-m88k.c}. This file @code{#include}s
39         @code{coff-m88k.h} which defines the external structure of the
40         coff format for the 88k, and @code{internalcoff.h} which
41         defines the internal structure. @code{coff-m88k.c} also
42         defines pthe relocations used by the 88k format
43         @xref{Relocations}. Then the major portion of coff code is
44         included (@code{coffcode.h}) which defines the methods used to
45         act upon the types defined in @code{coff-m88k.h} and
46         @code{internalcoff.h}.
47
48
49         The Intel i960 processor version of coff is implemented in
50         @code{coff-i960.c}. This file has the same structure as
51         @code{coff-m88k.c}, except that it includes @code{coff-i960.h}
52         rather than @code{coff-m88k.h}. 
53
54 SUBSECTION
55         Porting To A New Version of Coff
56
57         The recommended method is to select from the existing
58         implimentations the version of coff which is most like the one
59         you want to use, for our purposes, we'll say that i386 coff is
60         the one you select, and that your coff flavour is called foo.
61         Copy the @code{i386coff.c} to @code{foocoff.c}, copy
62         @code{../include/i386coff.h} to @code{../include/foocoff.h}
63         and add the lines to @code{targets.c} and @code{Makefile.in}
64         so that your new back end is used. Alter the shapes of the
65         structures in @code{../include/foocoff.h} so that they match
66         what you need. You will probably also have to add
67         @code{#ifdef}s to the code in @code{internalcoff.h} and
68         @code{coffcode.h} if your version of coff is too wild. 
69
70         You can verify that your new BFD backend works quite simply by
71         building @code{objdump} from the @code{binutils} directory,
72         and making sure that its version of what's going on at your
73         host systems idea (assuming it has the pretty standard coff
74         dump utility (usually called @code{att-dump} or just
75         @code{dump})) are the same.  Then clean up your code, and send
76         what you've done to Cygnus. Then your stuff will be in the
77         next release, and you won't have to keep integrating it.
78
79 SUBSECTION
80         How The Coff Backend Works
81
82 SUBSUBSECTION
83         Bit Twiddling
84
85         Each flavour of coff supported in BFD has its own header file
86         descibing the external layout of the structures. There is also
87         an internal description of the coff layout (in
88         @code{internalcoff.h}) file (@code{}). A major function of the
89         coff backend is swapping the bytes and twiddling the bits to
90         translate the external form of the structures into the normal
91         internal form. This is all performed in the
92         @code{bfd_swap}_@i{thing}_@i{direction} routines. Some
93         elements are different sizes between different versions of
94         coff, it is the duty of the coff version specific include file
95         to override the definitions of various packing routines in
96         @code{coffcode.h}. Eg the size of line number entry in coff is
97         sometimes 16 bits, and sometimes 32 bits. @code{#define}ing
98         @code{PUT_LNSZ_LNNO} and @code{GET_LNSZ_LNNO} will select the
99         correct one. No doubt, some day someone will find a version of
100         coff which has a varying field size not catered for at the
101         moment. To port BFD, that person will have to add more @code{#defines}.  
102         Three of the bit twiddling routines are exported to
103         @code{gdb}; @code{coff_swap_aux_in}, @code{coff_swap_sym_in}
104         and @code{coff_swap_linno_in}. @code{GDB} reads the symbol
105         table on its own, but uses BFD to fix things up.  More of the
106         bit twiddlers are exported for @code{gas};
107         @code{coff_swap_aux_out}, @code{coff_swap_sym_out},
108         @code{coff_swap_lineno_out}, @code{coff_swap_reloc_out},
109         @code{coff_swap_filehdr_out}, @code{coff_swap_aouthdr_out},
110         @code{coff_swap_scnhdr_out}. @code{Gas} currently keeps track
111         of all the symbol table and reloc drudgery itself, thereby
112         saving the internal BFD overhead, but uses BFD to swap things
113         on the way out, making cross ports much safer.  This also
114         allows BFD (and thus the linker) to use the same header files
115         as @code{gas}, which makes one avenue to disaster disappear.
116
117 SUBSUBSECTION
118         Symbol Reading
119
120         The simple canonical form for symbols used by BFD is not rich
121         enough to keep all the information available in a coff symbol
122         table. The back end gets around this by keeping the original
123         symbol table around, "behind the scenes". 
124
125         When a symbol table is requested (through a call to
126         @code{bfd_canonicalize_symtab}, a request gets through to
127         @code{get_normalized_symtab}. This reads the symbol table from
128         the coff file and swaps all the structures inside into the
129         internal form. It also fixes up all the pointers in the table
130         (represented in the file by offsets from the first symbol in
131         the table) into physical pointers to elements in the new
132         internal table. This involves some work since the meanings of
133         fields changes depending upon context; a field that is a
134         pointer to another structure in the symbol table at one moment
135         may be the size in bytes of a structure in the next.  Another
136         pass is made over the table. All symbols which mark file names
137         (<<C_FILE>> symbols) are modified so that the internal
138         string points to the value in the auxent (the real filename)
139         rather than the normal text associated with the symbol
140         (@code{".file"}). 
141
142         At this time the symbol names are moved around. Coff stores
143         all symbols less than nine characters long physically
144         within the symbol table, longer strings are kept at the end of
145         the file in the string  table. This pass moves all strings
146         into memory, and replaces them with pointers to the strings.
147
148
149         The symbol table is massaged once again, this time to create
150         the canonical table used by the BFD application. Each symbol
151         is inspected in turn, and a decision made (using the
152         @code{sclass} field) about the various flags to set in the
153         @code{asymbol} @xref{Symbols}. The generated canonical table
154         shares strings with the hidden internal symbol table. 
155
156         Any linenumbers are read from the coff file too, and attached
157         to the symbols which own the functions the linenumbers belong to. 
158
159 SUBSUBSECTION
160         Symbol Writing
161
162         Writing a symbol to a coff file which didn't come from a coff
163         file will lose any debugging information. The @code{asymbol}
164         structure remembers the BFD from which was born, and on output
165         the back end makes sure that the same destination target as
166         source target is present.
167
168         When the symbols have come from a coff file then all the
169         debugging information is preserved.
170
171         Symbol tables are provided for writing to the back end in a
172         vector of pointers to pointers. This allows applications like
173         the linker to accumulate and output large symbol tables
174         without having to do too much byte copying.
175
176         This function runs through the provided symbol table and
177         patches each symbol marked as a file place holder
178         (@code{C_FILE}) to point to the next file place holder in the
179         list. It also marks each @code{offset} field in the list with
180         the offset from the first symbol of the current symbol.
181
182         Another function of this procedure is to turn the canonical
183         value form of BFD into the form used by coff. Internally, BFD
184         expects symbol values to be offsets from a section base; so a
185         symbol physically at 0x120, but in a section starting at
186         0x100, would have the value 0x20. Coff expects symbols to
187         contain their final value, so symbols have their values
188         changed at this point to reflect their sum with their owning
189         section. Note that this transformation uses the
190         <<output_section>> field of the @code{asymbol}'s
191         @code{asection} @xref{Sections}. 
192
193         o coff_mangle_symbols
194
195         This routine runs though the provided symbol table and uses
196         the offsets generated by the previous pass and the pointers
197         generated when the symbol table was read in to create the
198         structured hierachy required by coff. It changes each pointer
199         to a symbol to an index into the symbol table of the symbol
200         being referenced. 
201
202         o coff_write_symbols
203
204         This routine runs through the symbol table and patches up the
205         symbols from their internal form into the coff way, calls the
206         bit twiddlers and writes out the tabel to the file. 
207
208 */
209
210 /*
211 INTERNAL_DEFINITION
212         coff_symbol_type
213
214 DESCRIPTION
215         The hidden information for an asymbol is described in a
216         coff_ptr_struct, which is typedefed to a combined_entry_type
217
218 CODE_FRAGMENT
219 .
220 .typedef struct coff_ptr_struct 
221 .{
222 .
223 .       {* Remembers the offset from the first symbol in the file for
224 .          this symbol. Generated by coff_renumber_symbols. *}
225 .unsigned int offset;
226 .
227 .       {* Should the tag field of this symbol be renumbered.
228 .          Created by coff_pointerize_aux. *}
229 .char fix_tag;
230 .
231 .       {* Should the endidx field of this symbol be renumbered.
232 .          Created by coff_pointerize_aux. *}
233 .char fix_end;
234 .
235 .       {* The container for the symbol structure as read and translated
236 .           from the file. *}
237 .
238 .union {
239 .   union internal_auxent auxent;
240 .   struct internal_syment syment;
241 . } u;
242 .} combined_entry_type;
243 .
244 .
245 .{* Each canonical asymbol really looks like this: *}
246 .
247 .typedef struct coff_symbol_struct
248 .{
249 .   {* The actual symbol which the rest of BFD works with *}
250 .asymbol symbol;
251 .
252 .   {* A pointer to the hidden information for this symbol *}
253 .combined_entry_type *native;
254 .
255 .   {* A pointer to the linenumber information for this symbol *}
256 .struct lineno_cache_entry *lineno;
257 .
258 .   {* Have the line numbers been relocated yet ? *}
259 .boolean done_lineno;
260 .} coff_symbol_type;
261
262
263 */
264
265 #include "seclet.h"
266 extern bfd_error_vector_type bfd_error_vector;
267
268
269
270
271 #define PUTWORD bfd_h_put_32
272 #define PUTHALF bfd_h_put_16
273 #define PUTBYTE bfd_h_put_8
274
275 #ifndef GET_FCN_LNNOPTR
276 #define GET_FCN_LNNOPTR(abfd, ext)  bfd_h_get_32(abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_fcn.x_lnnoptr)
277 #endif
278
279 #ifndef GET_FCN_ENDNDX
280 #define GET_FCN_ENDNDX(abfd, ext)  bfd_h_get_32(abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_fcn.x_endndx)
281 #endif
282
283 #ifndef PUT_FCN_LNNOPTR
284 #define PUT_FCN_LNNOPTR(abfd, in, ext)  PUTWORD(abfd,  in, (bfd_byte *) ext->x_sym.x_fcnary.x_fcn.x_lnnoptr)
285 #endif
286 #ifndef PUT_FCN_ENDNDX
287 #define PUT_FCN_ENDNDX(abfd, in, ext) PUTWORD(abfd, in, (bfd_byte *) ext->x_sym.x_fcnary.x_fcn.x_endndx)
288 #endif
289 #ifndef GET_LNSZ_LNNO
290 #define GET_LNSZ_LNNO(abfd, ext) bfd_h_get_16(abfd, (bfd_byte *) ext->x_sym.x_misc.x_lnsz.x_lnno)
291 #endif
292 #ifndef GET_LNSZ_SIZE
293 #define GET_LNSZ_SIZE(abfd, ext) bfd_h_get_16(abfd, (bfd_byte *) ext->x_sym.x_misc.x_lnsz.x_size)
294 #endif
295 #ifndef PUT_LNSZ_LNNO
296 #define PUT_LNSZ_LNNO(abfd, in, ext) bfd_h_put_16(abfd, in, (bfd_byte *)ext->x_sym.x_misc.x_lnsz.x_lnno)
297 #endif
298 #ifndef PUT_LNSZ_SIZE
299 #define PUT_LNSZ_SIZE(abfd, in, ext) bfd_h_put_16(abfd, in, (bfd_byte*) ext->x_sym.x_misc.x_lnsz.x_size)
300 #endif
301 #ifndef GET_SCN_SCNLEN
302 #define GET_SCN_SCNLEN(abfd,  ext) bfd_h_get_32(abfd, (bfd_byte *) ext->x_scn.x_scnlen)
303 #endif
304 #ifndef GET_SCN_NRELOC
305 #define GET_SCN_NRELOC(abfd,  ext) bfd_h_get_16(abfd, (bfd_byte *)ext->x_scn.x_nreloc)
306 #endif
307 #ifndef GET_SCN_NLINNO
308 #define GET_SCN_NLINNO(abfd, ext)  bfd_h_get_16(abfd, (bfd_byte *)ext->x_scn.x_nlinno)
309 #endif
310 #ifndef PUT_SCN_SCNLEN
311 #define PUT_SCN_SCNLEN(abfd,in, ext) bfd_h_put_32(abfd, in, (bfd_byte *) ext->x_scn.x_scnlen)
312 #endif
313 #ifndef PUT_SCN_NRELOC
314 #define PUT_SCN_NRELOC(abfd,in, ext) bfd_h_put_16(abfd, in, (bfd_byte *)ext->x_scn.x_nreloc)
315 #endif
316 #ifndef PUT_SCN_NLINNO
317 #define PUT_SCN_NLINNO(abfd,in, ext)  bfd_h_put_16(abfd,in, (bfd_byte  *) ext->x_scn.x_nlinno)
318 #endif
319 #ifndef GET_LINENO_LNNO
320 #define GET_LINENO_LNNO(abfd, ext) bfd_h_get_16(abfd, (bfd_byte *) (ext->l_lnno));
321 #endif
322 #ifndef PUT_LINENO_LNNO
323 #define PUT_LINENO_LNNO(abfd,val, ext) bfd_h_put_16(abfd,val,  (bfd_byte *) (ext->l_lnno));
324 #endif
325
326 \f
327 /* void warning(); */
328
329 /*
330  * Return a word with STYP_* (scnhdr.s_flags) flags set to represent the
331  * incoming SEC_* flags.  The inverse of this function is styp_to_sec_flags().
332  * NOTE: If you add to/change this routine, you should mirror the changes
333  *      in styp_to_sec_flags().
334  */
335 static long
336 DEFUN(sec_to_styp_flags, (sec_name, sec_flags),
337         CONST char *            sec_name        AND
338         flagword        sec_flags)
339 {
340     long styp_flags = 0;
341
342     if (!strcmp(sec_name, _TEXT)) {
343         return((long)STYP_TEXT);
344     } else if (!strcmp(sec_name, _DATA)) {
345         return((long)STYP_DATA);
346     } else if (!strcmp(sec_name, _BSS)) {
347         return((long)STYP_BSS);
348 #ifdef _COMMENT
349     } else if (!strcmp(sec_name, _COMMENT)) {
350         return((long)STYP_INFO);
351 #endif /* _COMMENT */
352     }
353
354 /* Try and figure out what it should be */
355    if (sec_flags & SEC_CODE) styp_flags = STYP_TEXT;
356    if (sec_flags & SEC_DATA) styp_flags = STYP_DATA;
357    else if (sec_flags & SEC_READONLY)
358 #ifdef STYP_LIT /* 29k readonly text/data section */
359         styp_flags = STYP_LIT;
360 #else
361         styp_flags = STYP_TEXT;
362 #endif  /* STYP_LIT */
363    else if (sec_flags & SEC_LOAD) styp_flags = STYP_TEXT;
364
365    if (styp_flags == 0) styp_flags = STYP_BSS;
366
367    return(styp_flags);
368 }
369 /*
370  * Return a word with SEC_* flags set to represent the incoming
371  * STYP_* flags (from scnhdr.s_flags).   The inverse of this
372  * function is sec_to_styp_flags().
373  * NOTE: If you add to/change this routine, you should mirror the changes
374  *      in sec_to_styp_flags().
375  */
376 static flagword
377 DEFUN(styp_to_sec_flags, (styp_flags),
378         long    styp_flags)
379 {
380   flagword      sec_flags=0;
381
382   if ((styp_flags & STYP_TEXT) || (styp_flags & STYP_DATA)) 
383   {
384     sec_flags = SEC_LOAD | SEC_ALLOC;
385   }
386   else if (styp_flags & STYP_BSS) 
387   {
388     sec_flags = SEC_ALLOC;
389   }
390   else if (styp_flags & STYP_INFO) 
391   {
392     sec_flags = SEC_NEVER_LOAD;
393   }
394   else
395   {
396     sec_flags = SEC_ALLOC | SEC_LOAD;
397   }
398 #ifdef STYP_LIT                 /* A29k readonly text/data section type */
399   if ((styp_flags & STYP_LIT) == STYP_LIT)
400   {
401     sec_flags = (SEC_LOAD | SEC_ALLOC | SEC_READONLY);
402   }
403 #endif                          /* STYP_LIT */
404 #ifdef STYP_OTHER_LOAD          /* Other loaded sections */
405   if (styp_flags & STYP_OTHER_LOAD)
406   {
407     sec_flags = (SEC_LOAD | SEC_ALLOC);
408   }
409 #endif                          /* STYP_SDATA */
410
411   return(sec_flags);
412 }
413
414 #define get_index(symbol)       ((int) (symbol)->udata)
415 #define set_index(symbol, idx)  ((symbol)->udata =(PTR) (idx))
416
417 /*  **********************************************************************
418 Here are all the routines for swapping the structures seen in the
419 outside world into the internal forms.
420 */
421
422
423 static void
424 DEFUN(bfd_swap_reloc_in,(abfd, reloc_src, reloc_dst),
425       bfd            *abfd AND
426       RELOC *reloc_src AND
427       struct internal_reloc *reloc_dst)
428 {
429   reloc_dst->r_vaddr = bfd_h_get_32(abfd, (bfd_byte *)reloc_src->r_vaddr);
430   reloc_dst->r_symndx = bfd_h_get_32(abfd, (bfd_byte *) reloc_src->r_symndx);
431
432 #ifdef RS6000COFF_C
433   reloc_dst->r_type = bfd_h_get_8(abfd, reloc_src->r_type);
434   reloc_dst->r_size = bfd_h_get_8(abfd, reloc_src->r_size);
435 #else
436   reloc_dst->r_type = bfd_h_get_16(abfd, (bfd_byte *) reloc_src->r_type);
437 #endif
438
439 #ifdef SWAP_IN_RELOC_OFFSET
440   reloc_dst->r_offset = SWAP_IN_RELOC_OFFSET(abfd,
441                                              (bfd_byte *) reloc_src->r_offset);
442 #endif
443 }
444
445
446 static unsigned int
447 DEFUN(coff_swap_reloc_out,(abfd, src, dst),
448       bfd       *abfd AND
449       PTR       src AND
450       PTR       dst)
451 {
452   struct internal_reloc *reloc_src = (struct internal_reloc *)src;
453   struct external_reloc *reloc_dst = (struct external_reloc *)dst;
454   bfd_h_put_32(abfd, reloc_src->r_vaddr, (bfd_byte *) reloc_dst->r_vaddr);
455   bfd_h_put_32(abfd, reloc_src->r_symndx, (bfd_byte *) reloc_dst->r_symndx);
456   bfd_h_put_16(abfd, reloc_src->r_type, (bfd_byte *)
457                reloc_dst->r_type);
458
459 #ifdef SWAP_OUT_RELOC_OFFSET
460   SWAP_OUT_RELOC_OFFSET(abfd,
461                         reloc_src->r_offset,
462                         (bfd_byte *) reloc_dst->r_offset);
463 #endif
464 #ifdef SWAP_OUT_RELOC_EXTRA
465   SWAP_OUT_RELOC_EXTRA(abfd,reloc_src, reloc_dst);
466 #endif
467
468   return sizeof(struct external_reloc);
469 }
470
471 static void
472 DEFUN(bfd_swap_filehdr_in,(abfd, filehdr_src, filehdr_dst),
473       bfd            *abfd AND
474       FILHDR         *filehdr_src AND
475       struct internal_filehdr *filehdr_dst)
476 {
477   filehdr_dst->f_magic = bfd_h_get_16(abfd, (bfd_byte *) filehdr_src->f_magic);
478   filehdr_dst->f_nscns = bfd_h_get_16(abfd, (bfd_byte *)filehdr_src-> f_nscns);
479   filehdr_dst->f_timdat = bfd_h_get_32(abfd, (bfd_byte *)filehdr_src-> f_timdat);
480   filehdr_dst->f_symptr = bfd_h_get_32(abfd, (bfd_byte *)filehdr_src-> f_symptr);
481   filehdr_dst->f_nsyms = bfd_h_get_32(abfd, (bfd_byte *)filehdr_src-> f_nsyms);
482   filehdr_dst->f_opthdr = bfd_h_get_16(abfd, (bfd_byte *)filehdr_src-> f_opthdr);
483   filehdr_dst->f_flags = bfd_h_get_16(abfd, (bfd_byte *)filehdr_src-> f_flags);
484 }
485
486 static  unsigned int
487 DEFUN(coff_swap_filehdr_out,(abfd, in, out),
488       bfd       *abfd AND
489       PTR       in AND
490       PTR       out)
491 {
492   struct internal_filehdr *filehdr_in = (struct internal_filehdr *)in;
493   FILHDR *filehdr_out = (FILHDR *)out;
494   bfd_h_put_16(abfd, filehdr_in->f_magic, (bfd_byte *) filehdr_out->f_magic);
495   bfd_h_put_16(abfd, filehdr_in->f_nscns, (bfd_byte *) filehdr_out->f_nscns);
496   bfd_h_put_32(abfd, filehdr_in->f_timdat, (bfd_byte *) filehdr_out->f_timdat);
497   bfd_h_put_32(abfd, filehdr_in->f_symptr, (bfd_byte *) filehdr_out->f_symptr);
498   bfd_h_put_32(abfd, filehdr_in->f_nsyms, (bfd_byte *) filehdr_out->f_nsyms);
499   bfd_h_put_16(abfd, filehdr_in->f_opthdr, (bfd_byte *) filehdr_out->f_opthdr);
500   bfd_h_put_16(abfd, filehdr_in->f_flags, (bfd_byte *) filehdr_out->f_flags);
501   return sizeof(FILHDR);
502 }
503
504
505 #ifndef NO_COFF_SYMBOLS
506
507 static void
508 DEFUN(coff_swap_sym_in,(abfd, ext1, in1),
509       bfd            *abfd AND
510       PTR ext1 AND
511       PTR in1)
512 {
513   SYMENT *ext = (SYMENT *)ext1;
514   struct internal_syment      *in = (struct internal_syment *)in1;
515
516   if( ext->e.e_name[0] == 0) {
517     in->_n._n_n._n_zeroes = 0;
518     in->_n._n_n._n_offset = bfd_h_get_32(abfd, (bfd_byte *) ext->e.e.e_offset);
519   }
520   else {
521 #if SYMNMLEN != E_SYMNMLEN
522    -> Error, we need to cope with truncating or extending SYMNMLEN!;
523 #else
524     memcpy(in->_n._n_name, ext->e.e_name, SYMNMLEN);
525 #endif
526   }
527   in->n_value = bfd_h_get_32(abfd, (bfd_byte *) ext->e_value);
528   in->n_scnum = bfd_h_get_16(abfd, (bfd_byte *) ext->e_scnum);
529   if (sizeof(ext->e_type) == 2){
530     in->n_type = bfd_h_get_16(abfd, (bfd_byte *) ext->e_type);
531   }
532   else {
533     in->n_type = bfd_h_get_32(abfd, (bfd_byte *) ext->e_type);
534   }
535   in->n_sclass = bfd_h_get_8(abfd, ext->e_sclass);
536   in->n_numaux = bfd_h_get_8(abfd, ext->e_numaux);
537 }
538
539 static unsigned int
540 DEFUN(coff_swap_sym_out,(abfd, inp, extp),
541       bfd       *abfd AND
542       PTR       inp AND
543       PTR       extp)
544 {
545   struct internal_syment *in = (struct internal_syment *)inp;
546   SYMENT *ext =(SYMENT *)extp;
547   if(in->_n._n_name[0] == 0) {
548     bfd_h_put_32(abfd, 0, (bfd_byte *) ext->e.e.e_zeroes);
549     bfd_h_put_32(abfd, in->_n._n_n._n_offset, (bfd_byte *)  ext->e.e.e_offset);
550   }
551   else {
552 #if SYMNMLEN != E_SYMNMLEN
553     -> Error, we need to cope with truncating or extending SYMNMLEN!;
554 #else
555     memcpy(ext->e.e_name, in->_n._n_name, SYMNMLEN);
556 #endif
557   }
558   bfd_h_put_32(abfd,  in->n_value , (bfd_byte *) ext->e_value);
559   bfd_h_put_16(abfd,  in->n_scnum , (bfd_byte *) ext->e_scnum);
560   if (sizeof(ext->e_type) == 2)
561       {
562         bfd_h_put_16(abfd,  in->n_type , (bfd_byte *) ext->e_type);
563       }
564   else
565       {
566         bfd_h_put_32(abfd,  in->n_type , (bfd_byte *) ext->e_type);
567       }
568   bfd_h_put_8(abfd,  in->n_sclass , ext->e_sclass);
569   bfd_h_put_8(abfd,  in->n_numaux , ext->e_numaux);
570   return sizeof(SYMENT);
571 }
572
573 static void
574 DEFUN(coff_swap_aux_in,(abfd, ext1, type, class, in1),
575       bfd            *abfd AND
576       PTR             ext1 AND
577       int             type AND
578       int             class AND
579       PTR             in1)
580 {
581   AUXENT    *ext = (AUXENT *)ext1;
582   union internal_auxent *in = (union internal_auxent *)in1;
583
584   switch (class) {
585     case C_FILE:
586       if (ext->x_file.x_fname[0] == 0) {
587           in->x_file.x_n.x_zeroes = 0;
588           in->x_file.x_n.x_offset = 
589            bfd_h_get_32(abfd, (bfd_byte *) ext->x_file.x_n.x_offset);
590         } else {
591 #if FILNMLEN != E_FILNMLEN
592             -> Error, we need to cope with truncating or extending FILNMLEN!;
593 #else
594             memcpy (in->x_file.x_fname, ext->x_file.x_fname, FILNMLEN);
595 #endif
596           }
597       break;
598
599       /* RS/6000 "csect" auxents */
600 #ifdef RS6000COFF_C
601     case C_EXT:
602     case C_HIDEXT:
603       in->x_csect.x_scnlen   = bfd_h_get_32 (abfd, (bfd_byte *) ext->x_csect.x_scnlen);
604       in->x_csect.x_parmhash = bfd_h_get_32 (abfd, (bfd_byte *) ext->x_csect.x_parmhash);
605       in->x_csect.x_snhash   = bfd_h_get_16 (abfd, (bfd_byte *) ext->x_csect.x_snhash);
606       /* We don't have to hack bitfields in x_smtyp because it's defined by
607          shifts-and-ands, which are equivalent on all byte orders.  */
608       in->x_csect.x_smtyp    = bfd_h_get_8  (abfd, (bfd_byte *) ext->x_csect.x_smtyp);
609       in->x_csect.x_smclas   = bfd_h_get_8  (abfd, (bfd_byte *) ext->x_csect.x_smclas);
610       in->x_csect.x_stab     = bfd_h_get_32 (abfd, (bfd_byte *) ext->x_csect.x_stab);
611       in->x_csect.x_snstab   = bfd_h_get_16 (abfd, (bfd_byte *) ext->x_csect.x_snstab);
612       break;
613 #endif
614
615     case C_STAT:
616 #ifdef C_LEAFSTAT
617     case C_LEAFSTAT:
618 #endif
619     case C_HIDDEN:
620       if (type == T_NULL) {
621           in->x_scn.x_scnlen = GET_SCN_SCNLEN(abfd, ext);
622           in->x_scn.x_nreloc = GET_SCN_NRELOC(abfd, ext);
623           in->x_scn.x_nlinno = GET_SCN_NLINNO(abfd, ext);
624           break;
625         }
626     default:
627       in->x_sym.x_tagndx.l = bfd_h_get_32(abfd, (bfd_byte *) ext->x_sym.x_tagndx);
628 #ifndef NO_TVNDX
629       in->x_sym.x_tvndx = bfd_h_get_16(abfd, (bfd_byte *) ext->x_sym.x_tvndx);
630 #endif
631
632       if (ISARY(type) || class == C_BLOCK) {
633 #if DIMNUM != E_DIMNUM
634           -> Error, we need to cope with truncating or extending DIMNUM!;
635 #else
636           in->x_sym.x_fcnary.x_ary.x_dimen[0] = bfd_h_get_16(abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
637           in->x_sym.x_fcnary.x_ary.x_dimen[1] = bfd_h_get_16(abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
638           in->x_sym.x_fcnary.x_ary.x_dimen[2] = bfd_h_get_16(abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
639           in->x_sym.x_fcnary.x_ary.x_dimen[3] = bfd_h_get_16(abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
640 #endif
641         }
642       in->x_sym.x_fcnary.x_fcn.x_lnnoptr = GET_FCN_LNNOPTR(abfd, ext);
643       in->x_sym.x_fcnary.x_fcn.x_endndx.l = GET_FCN_ENDNDX(abfd, ext);
644
645       if (ISFCN(type)) {
646           in->x_sym.x_misc.x_fsize = bfd_h_get_32(abfd, (bfd_byte *) ext->x_sym.x_misc.x_fsize);
647         }
648       else {
649           in->x_sym.x_misc.x_lnsz.x_lnno = GET_LNSZ_LNNO(abfd, ext);
650           in->x_sym.x_misc.x_lnsz.x_size = GET_LNSZ_SIZE(abfd, ext);
651         }
652     }
653 }
654
655 static unsigned int
656 DEFUN(coff_swap_aux_out,(abfd, inp, type, class, extp),
657   bfd   *abfd AND
658   PTR   inp AND
659   int   type AND
660   int   class AND
661   PTR   extp)
662 {
663   union internal_auxent *in = (union internal_auxent *)inp;
664   AUXENT *ext = (AUXENT *)extp;
665   switch (class) {
666   case C_FILE:
667     if (in->x_file.x_fname[0] == 0) {
668       PUTWORD(abfd, 0, (bfd_byte *) ext->x_file.x_n.x_zeroes);
669       PUTWORD(abfd,
670               in->x_file.x_n.x_offset,
671               (bfd_byte *) ext->x_file.x_n.x_offset);
672     }
673     else {
674 #if FILNMLEN != E_FILNMLEN
675       -> Error, we need to cope with truncating or extending FILNMLEN!;
676 #else
677       memcpy (ext->x_file.x_fname, in->x_file.x_fname, FILNMLEN);
678 #endif
679     }
680     break;
681
682 #ifdef RS6000COFF_C
683   /* RS/6000 "csect" auxents */
684   case C_EXT:
685   case C_HIDEXT:
686     PUTWORD (abfd, in->x_csect.x_scnlen,        ext->x_csect.x_scnlen);
687     PUTWORD (abfd, in->x_csect.x_parmhash,      ext->x_csect.x_parmhash);
688     PUTHALF (abfd, in->x_csect.x_snhash,        ext->x_csect.x_snhash);
689     /* We don't have to hack bitfields in x_smtyp because it's defined by
690        shifts-and-ands, which are equivalent on all byte orders.  */
691     PUTBYTE (abfd, in->x_csect.x_smtyp,         ext->x_csect.x_smtyp);
692     PUTBYTE (abfd, in->x_csect.x_smclas,        ext->x_csect.x_smclas);
693     PUTWORD (abfd, in->x_csect.x_stab,          ext->x_csect.x_stab);
694     PUTHALF (abfd, in->x_csect.x_snstab,        ext->x_csect.x_snstab);
695     break;
696 #endif
697
698   case C_STAT:
699 #ifdef C_LEAFSTAT
700   case C_LEAFSTAT:
701 #endif
702   case C_HIDDEN:
703     if (type == T_NULL) {
704       PUT_SCN_SCNLEN(abfd, in->x_scn.x_scnlen, ext);
705       PUT_SCN_NRELOC(abfd, in->x_scn.x_nreloc, ext);
706       PUT_SCN_NLINNO(abfd, in->x_scn.x_nlinno, ext);
707       break;
708     }
709   default:
710     PUTWORD(abfd, in->x_sym.x_tagndx.l, (bfd_byte *) ext->x_sym.x_tagndx);
711 #ifndef NO_TVNDX
712     bfd_h_put_16(abfd, in->x_sym.x_tvndx , (bfd_byte *) ext->x_sym.x_tvndx);
713 #endif
714
715     if (ISFCN(type)) {
716       PUTWORD(abfd, in->x_sym.x_misc.x_fsize, (bfd_byte *)  ext->x_sym.x_misc.x_fsize);
717       PUT_FCN_LNNOPTR(abfd,  in->x_sym.x_fcnary.x_fcn.x_lnnoptr, ext);
718       PUT_FCN_ENDNDX(abfd,  in->x_sym.x_fcnary.x_fcn.x_endndx.l, ext);
719     }
720     else {
721
722       if (ISARY(type) || class == C_BLOCK) {
723 #if DIMNUM != E_DIMNUM
724         -> Error, we need to cope with truncating or extending DIMNUM!;
725 #else
726         bfd_h_put_16(abfd, in->x_sym.x_fcnary.x_ary.x_dimen[0], (bfd_byte *)ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
727         bfd_h_put_16(abfd, in->x_sym.x_fcnary.x_ary.x_dimen[1], (bfd_byte *)ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
728         bfd_h_put_16(abfd, in->x_sym.x_fcnary.x_ary.x_dimen[2], (bfd_byte *)ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
729         bfd_h_put_16(abfd, in->x_sym.x_fcnary.x_ary.x_dimen[3], (bfd_byte *)ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
730 #endif
731       }
732       PUT_LNSZ_LNNO(abfd, in->x_sym.x_misc.x_lnsz.x_lnno, ext);
733       PUT_LNSZ_SIZE(abfd, in->x_sym.x_misc.x_lnsz.x_size, ext);
734
735       PUT_FCN_LNNOPTR(abfd,  in->x_sym.x_fcnary.x_fcn.x_lnnoptr, ext);
736       PUT_FCN_ENDNDX(abfd,  in->x_sym.x_fcnary.x_fcn.x_endndx.l, ext);
737
738
739     }
740   }
741 return sizeof(AUXENT);
742 }
743
744 #endif /* NO_COFF_SYMBOLS */
745
746 #ifndef NO_COFF_LINENOS
747
748 static void
749 DEFUN(coff_swap_lineno_in,(abfd, ext1, in1),
750       bfd            *abfd AND
751       PTR ext1 AND
752       PTR in1)
753 {
754   LINENO *ext = (LINENO *)ext1;
755   struct internal_lineno      *in = (struct internal_lineno *)in1;
756
757   in->l_addr.l_symndx = bfd_h_get_32(abfd, (bfd_byte *) ext->l_addr.l_symndx);
758   in->l_lnno = GET_LINENO_LNNO(abfd, ext);
759 }
760
761 static unsigned int
762 DEFUN(coff_swap_lineno_out,(abfd, inp, outp),
763       bfd       *abfd AND
764       PTR       inp AND
765       PTR       outp)
766 {
767   struct internal_lineno *in = (struct internal_lineno *)inp;
768   struct external_lineno *ext = (struct external_lineno *)outp;
769   PUTWORD(abfd, in->l_addr.l_symndx, (bfd_byte *)
770           ext->l_addr.l_symndx);
771
772   PUT_LINENO_LNNO (abfd, in->l_lnno, ext);
773   return sizeof(struct external_lineno);
774 }
775
776 #endif /* NO_COFF_LINENOS */
777
778
779 static void
780 DEFUN(bfd_swap_aouthdr_in,(abfd, aouthdr_ext1, aouthdr_int1),
781       bfd            *abfd AND
782       PTR aouthdr_ext1 AND
783       PTR aouthdr_int1)
784 {
785   AOUTHDR        *aouthdr_ext = (AOUTHDR *) aouthdr_ext1;
786   struct internal_aouthdr *aouthdr_int = (struct internal_aouthdr *)aouthdr_int1;
787
788   aouthdr_int->magic = bfd_h_get_16(abfd, (bfd_byte *) aouthdr_ext->magic);
789   aouthdr_int->vstamp = bfd_h_get_16(abfd, (bfd_byte *) aouthdr_ext->vstamp);
790   aouthdr_int->tsize = bfd_h_get_32(abfd, (bfd_byte *) aouthdr_ext->tsize);
791   aouthdr_int->dsize = bfd_h_get_32(abfd, (bfd_byte *) aouthdr_ext->dsize);
792   aouthdr_int->bsize = bfd_h_get_32(abfd, (bfd_byte *) aouthdr_ext->bsize);
793   aouthdr_int->entry = bfd_h_get_32(abfd, (bfd_byte *) aouthdr_ext->entry);
794   aouthdr_int->text_start = bfd_h_get_32(abfd, (bfd_byte *) aouthdr_ext->text_start);
795   aouthdr_int->data_start = bfd_h_get_32(abfd, (bfd_byte *) aouthdr_ext->data_start);
796 #ifdef I960
797   aouthdr_int->tagentries = bfd_h_get_32(abfd, (bfd_byte *) aouthdr_ext->tagentries);
798 #endif
799
800 #ifdef RS6000COFF_C
801   aouthdr_int->o_toc = bfd_h_get_32(abfd, aouthdr_ext->o_toc);
802   aouthdr_int->o_snentry = bfd_h_get_16(abfd, aouthdr_ext->o_snentry);
803   aouthdr_int->o_sntext = bfd_h_get_16(abfd, aouthdr_ext->o_sntext);
804   aouthdr_int->o_sndata = bfd_h_get_16(abfd, aouthdr_ext->o_sndata);
805   aouthdr_int->o_sntoc = bfd_h_get_16(abfd, aouthdr_ext->o_sntoc);
806   aouthdr_int->o_snloader = bfd_h_get_16(abfd, aouthdr_ext->o_snloader);
807   aouthdr_int->o_snbss = bfd_h_get_16(abfd, aouthdr_ext->o_snbss);
808   aouthdr_int->o_algntext = bfd_h_get_16(abfd, aouthdr_ext->o_algntext);
809   aouthdr_int->o_algndata = bfd_h_get_16(abfd, aouthdr_ext->o_algndata);
810   aouthdr_int->o_modtype = bfd_h_get_16(abfd, aouthdr_ext->o_modtype);
811   aouthdr_int->o_maxstack = bfd_h_get_32(abfd, aouthdr_ext->o_maxstack);
812 #endif
813 }
814
815 static unsigned int
816 DEFUN(coff_swap_aouthdr_out,(abfd, in, out),
817       bfd       *abfd AND
818       PTR       in AND
819       PTR       out)
820 {
821   struct internal_aouthdr *aouthdr_in = (struct internal_aouthdr *)in;
822   AOUTHDR *aouthdr_out = (AOUTHDR *)out;
823   bfd_h_put_16(abfd, aouthdr_in->magic, (bfd_byte *) aouthdr_out->magic);
824   bfd_h_put_16(abfd, aouthdr_in->vstamp, (bfd_byte *) aouthdr_out->vstamp);
825   bfd_h_put_32(abfd, aouthdr_in->tsize, (bfd_byte *) aouthdr_out->tsize);
826   bfd_h_put_32(abfd, aouthdr_in->dsize, (bfd_byte *) aouthdr_out->dsize);
827   bfd_h_put_32(abfd, aouthdr_in->bsize, (bfd_byte *) aouthdr_out->bsize);
828   bfd_h_put_32(abfd, aouthdr_in->entry, (bfd_byte *) aouthdr_out->entry);
829   bfd_h_put_32(abfd, aouthdr_in->text_start,
830                (bfd_byte *) aouthdr_out->text_start);
831   bfd_h_put_32(abfd, aouthdr_in->data_start, (bfd_byte *) aouthdr_out->data_start);
832 #ifdef I960
833   bfd_h_put_32(abfd, aouthdr_in->tagentries, (bfd_byte *) aouthdr_out->tagentries);
834 #endif
835   return sizeof(AOUTHDR);
836 }
837
838 static void
839 DEFUN(coff_swap_scnhdr_in,(abfd, scnhdr_ext, scnhdr_int),
840       bfd            *abfd AND
841       SCNHDR         *scnhdr_ext AND
842       struct internal_scnhdr *scnhdr_int)
843 {
844   memcpy(scnhdr_int->s_name, scnhdr_ext->s_name, sizeof(scnhdr_int->s_name));
845   scnhdr_int->s_vaddr = bfd_h_get_32(abfd, (bfd_byte *) scnhdr_ext->s_vaddr);
846   scnhdr_int->s_paddr = bfd_h_get_32(abfd, (bfd_byte *) scnhdr_ext->s_paddr);
847   scnhdr_int->s_size = bfd_h_get_32(abfd, (bfd_byte *)  scnhdr_ext->s_size);
848
849   scnhdr_int->s_scnptr = bfd_h_get_32(abfd, (bfd_byte *) scnhdr_ext->s_scnptr);
850   scnhdr_int->s_relptr = bfd_h_get_32(abfd, (bfd_byte *) scnhdr_ext->s_relptr);
851   scnhdr_int->s_lnnoptr = bfd_h_get_32(abfd, (bfd_byte *) scnhdr_ext->s_lnnoptr);
852   scnhdr_int->s_flags = bfd_h_get_32(abfd, (bfd_byte *) scnhdr_ext->s_flags);
853 #if defined(M88)
854   scnhdr_int->s_nreloc = bfd_h_get_32(abfd, (bfd_byte *) scnhdr_ext->s_nreloc);
855   scnhdr_int->s_nlnno = bfd_h_get_32(abfd, (bfd_byte *) scnhdr_ext->s_nlnno);
856 #else
857   scnhdr_int->s_nreloc = bfd_h_get_16(abfd, (bfd_byte *) scnhdr_ext->s_nreloc);
858   scnhdr_int->s_nlnno = bfd_h_get_16(abfd, (bfd_byte *) scnhdr_ext->s_nlnno);
859 #endif
860 #ifdef I960
861   scnhdr_int->s_align = bfd_h_get_32(abfd, (bfd_byte *) scnhdr_ext->s_align);
862 #endif
863 }
864
865 static unsigned int
866 DEFUN(coff_swap_scnhdr_out,(abfd, in, out),
867       bfd       *abfd AND
868       PTR       in AND
869       PTR       out)
870 {
871   struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *)in;
872   SCNHDR *scnhdr_ext = (SCNHDR *)out;
873   memcpy(scnhdr_ext->s_name, scnhdr_int->s_name, sizeof(scnhdr_int->s_name));
874   PUTWORD(abfd, scnhdr_int->s_vaddr, (bfd_byte *) scnhdr_ext->s_vaddr);
875   PUTWORD(abfd, scnhdr_int->s_paddr, (bfd_byte *) scnhdr_ext->s_paddr);
876   PUTWORD(abfd, scnhdr_int->s_size, (bfd_byte *) scnhdr_ext->s_size);
877   PUTWORD(abfd, scnhdr_int->s_scnptr, (bfd_byte *) scnhdr_ext->s_scnptr);
878   PUTWORD(abfd, scnhdr_int->s_relptr, (bfd_byte *) scnhdr_ext->s_relptr);
879   PUTWORD(abfd, scnhdr_int->s_lnnoptr, (bfd_byte *) scnhdr_ext->s_lnnoptr);
880   PUTWORD(abfd, scnhdr_int->s_flags, (bfd_byte *) scnhdr_ext->s_flags);
881 #if defined(M88)
882   PUTWORD(abfd, scnhdr_int->s_nlnno, (bfd_byte *) scnhdr_ext->s_nlnno);
883   PUTWORD(abfd, scnhdr_int->s_nreloc, (bfd_byte *) scnhdr_ext->s_nreloc);
884 #else
885   PUTHALF(abfd, scnhdr_int->s_nlnno, (bfd_byte *) scnhdr_ext->s_nlnno);
886   PUTHALF(abfd, scnhdr_int->s_nreloc, (bfd_byte *) scnhdr_ext->s_nreloc);
887 #endif
888
889 #if defined(I960)
890   PUTWORD(abfd, scnhdr_int->s_align, (bfd_byte *) scnhdr_ext->s_align);
891 #endif
892   return sizeof(SCNHDR);
893 }
894
895
896 /*
897    initialize a section structure with information peculiar to this
898    particular implementation of coff
899 */
900
901 static          boolean
902 DEFUN(coff_new_section_hook,(abfd, section),
903       bfd            *abfd AND
904       asection       *section)
905 {
906   section->alignment_power = abfd->xvec->align_power_min;
907   /* Allocate aux records for section symbols, to store size and
908      related info.
909
910      @@ Shouldn't use constant multiplier here!  */
911   coffsymbol (section->symbol)->native =
912     (combined_entry_type *) bfd_zalloc (abfd,
913                                         sizeof (combined_entry_type) * 10);
914   return true;
915 }
916
917 static asection bfd_debug_section = { "*DEBUG*" };
918
919 /* Take a section header read from a coff file (in HOST byte order),
920    and make a BFD "section" out of it.  */
921 static          boolean
922 DEFUN(make_a_section_from_file,(abfd, hdr, target_index),
923       bfd            *abfd AND
924       struct internal_scnhdr  *hdr AND
925       unsigned int target_index)
926 {
927   asection       *return_section;
928   char *name;
929     
930   /* Assorted wastage to null-terminate the name, thanks AT&T! */
931   name = bfd_alloc(abfd, sizeof (hdr->s_name)+1);
932   if (name == NULL) {
933       bfd_error = no_memory;
934       return false;
935     }
936   strncpy(name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
937   name[sizeof (hdr->s_name)] = 0;
938
939   return_section = bfd_make_section(abfd, name);
940   if (return_section == NULL)
941    return false;
942
943   /* s_paddr is presumed to be = to s_vaddr */
944
945   return_section->vma = hdr->s_vaddr;
946   return_section->_raw_size = hdr->s_size;
947   return_section->filepos = hdr->s_scnptr;
948   return_section->rel_filepos =  hdr->s_relptr;
949   return_section->reloc_count = hdr->s_nreloc;
950 #ifdef I960
951
952   /* FIXME, use a temp var rather than alignment_power */
953   return_section->alignment_power = hdr->s_align;
954 {
955   unsigned int    i;
956   for (i = 0; i < 32; i++) {
957       if ((1 << i) >= (int) (return_section->alignment_power)) {
958           return_section->alignment_power = i;
959           break;
960         }
961     }
962 }
963
964 #endif
965 return_section->line_filepos =  hdr->s_lnnoptr;
966   /*
967     return_section->linesize =   hdr->s_nlnno * sizeof (struct lineno);
968     */
969
970   return_section->lineno_count = hdr->s_nlnno;
971   return_section->userdata = NULL;
972   return_section->next = (asection *) NULL;
973   return_section->flags = styp_to_sec_flags(hdr->s_flags);
974
975   return_section->target_index = target_index;
976
977   if (hdr->s_nreloc != 0)
978    return_section->flags |= SEC_RELOC;
979   /* FIXME: should this check 'hdr->s_size > 0' */
980   if (hdr->s_scnptr != 0)
981    return_section->flags |= SEC_HAS_CONTENTS;
982   return true;
983 }
984 static          boolean
985 DEFUN(coff_mkobject,(abfd),
986       bfd            *abfd)
987 {
988  abfd->tdata.coff_obj_data = (struct coff_tdata *)bfd_zalloc (abfd,sizeof(coff_data_type));
989   if (abfd->tdata.coff_obj_data == 0){
990     bfd_error = no_memory;
991     return false;
992   }
993   coff_data(abfd)->relocbase = 0;
994 /*  make_abs_section(abfd);*/
995   return true;
996 }
997
998 static
999 bfd_target     *
1000 DEFUN(coff_real_object_p,(abfd, nscns, internal_f, internal_a),
1001     bfd            *abfd AND
1002     unsigned        nscns AND
1003   struct internal_filehdr *internal_f AND
1004   struct internal_aouthdr *internal_a)
1005 {
1006   coff_data_type *coff;
1007   enum bfd_architecture arch;
1008   long machine;
1009   size_t          readsize;     /* length of file_info */
1010   SCNHDR *external_sections;
1011
1012   /* Build a play area */
1013   if (coff_mkobject(abfd) != true)
1014     return 0;
1015
1016   coff = coff_data(abfd);
1017
1018
1019   external_sections = (SCNHDR *)bfd_alloc(abfd, readsize = (nscns * SCNHSZ));
1020
1021   if (bfd_read((PTR)external_sections, 1, readsize, abfd) != readsize) {
1022     goto fail;
1023   }
1024
1025
1026   /* Now copy data as required; construct all asections etc */
1027   coff->symbol_index_slew = 0;
1028   coff->relocbase =0;
1029   coff->raw_syment_count = 0;
1030   coff->raw_linenos = 0;
1031   coff->raw_syments = 0;
1032   coff->sym_filepos =0;
1033   coff->flags = internal_f->f_flags;
1034   if (nscns != 0) {
1035     unsigned int    i;
1036     for (i = 0; i < nscns; i++) {
1037       struct internal_scnhdr tmp;
1038       coff_swap_scnhdr_in(abfd, external_sections + i, &tmp);
1039       make_a_section_from_file(abfd,&tmp, i+1);
1040     }
1041   }
1042
1043 /*  make_abs_section(abfd);*/
1044   
1045   /* Determine the machine architecture and type.  */
1046 machine = 0;
1047   switch (internal_f->f_magic) {
1048 #ifdef I386MAGIC
1049   case I386MAGIC:
1050     arch = bfd_arch_i386;
1051     machine = 0;
1052     break;
1053 #endif
1054
1055 #ifdef A29K_MAGIC_BIG
1056   case  A29K_MAGIC_BIG:
1057   case  A29K_MAGIC_LITTLE:
1058     arch = bfd_arch_a29k;
1059     machine = 0;
1060     break;
1061 #endif
1062
1063 #ifdef MIPS
1064   case  MIPS_MAGIC_1:
1065   case  MIPS_MAGIC_2:
1066   case  MIPS_MAGIC_3:
1067     arch = bfd_arch_mips;
1068     machine = 0;
1069     break;
1070 #endif
1071
1072 #ifdef MC68MAGIC
1073   case MC68MAGIC:
1074   case M68MAGIC:
1075     arch = bfd_arch_m68k;
1076     machine = 68020;
1077     break;
1078 #endif
1079 #ifdef MC88MAGIC
1080   case MC88MAGIC:
1081   case MC88DMAGIC:
1082   case MC88OMAGIC:
1083     arch = bfd_arch_m88k;
1084     machine = 88100;
1085     break;
1086 #endif
1087 #ifdef Z8KMAGIC
1088    case Z8KMAGIC:
1089     arch = bfd_arch_z8k;
1090     switch (internal_f->f_flags & F_MACHMASK) 
1091 {
1092      case F_Z8001:
1093       machine = bfd_mach_z8001;
1094       break;
1095      case F_Z8002:
1096       machine = bfd_mach_z8002;
1097       break;
1098      default:
1099       goto fail;
1100     }
1101     break;
1102 #endif
1103 #ifdef I960
1104 #ifdef I960ROMAGIC
1105   case I960ROMAGIC:
1106   case I960RWMAGIC:
1107     arch = bfd_arch_i960;
1108     switch (F_I960TYPE & internal_f->f_flags)
1109         {
1110         default:
1111         case F_I960CORE:
1112           machine = bfd_mach_i960_core;
1113           break;
1114         case F_I960KB:
1115           machine = bfd_mach_i960_kb_sb;
1116           break;
1117         case  F_I960MC:
1118           machine = bfd_mach_i960_mc;
1119           break;
1120         case F_I960XA:
1121           machine = bfd_mach_i960_xa;
1122           break;
1123         case F_I960CA:
1124           machine = bfd_mach_i960_ca;
1125           break;
1126         case F_I960KA:
1127           machine = bfd_mach_i960_ka_sa;
1128           break;
1129         }
1130     break;
1131 #endif
1132 #endif
1133
1134 #ifdef U802ROMAGIC
1135   case U802ROMAGIC:
1136   case U802WRMAGIC:
1137   case U802TOCMAGIC:
1138     arch = bfd_arch_rs6000;
1139     machine = 6000;
1140     break;
1141 #endif
1142
1143 #ifdef WE32KMAGIC
1144   case WE32KMAGIC:
1145     arch = bfd_arch_we32k;
1146     machine = 0;
1147     break;
1148 #endif
1149
1150 #ifdef H8300MAGIC
1151   case H8300MAGIC:
1152     arch = bfd_arch_h8300;
1153     machine = 0;
1154     break;
1155 #endif
1156
1157   default:                      /* Unreadable input file type */
1158  arch = bfd_arch_obscure;
1159     break;
1160   }
1161
1162   bfd_default_set_arch_mach(abfd, arch, machine);
1163   if (!(internal_f->f_flags & F_RELFLG))
1164     abfd->flags |= HAS_RELOC;
1165   if ((internal_f->f_flags & F_EXEC))
1166     abfd->flags |= EXEC_P;
1167   if (!(internal_f->f_flags & F_LNNO))
1168     abfd->flags |= HAS_LINENO;
1169   if (!(internal_f->f_flags & F_LSYMS))
1170     abfd->flags |= HAS_LOCALS;
1171
1172
1173   bfd_get_symcount(abfd) = internal_f->f_nsyms;
1174   if (internal_f->f_nsyms)
1175     abfd->flags |= HAS_SYMS;
1176
1177   coff->sym_filepos = internal_f->f_symptr;
1178
1179   /* These members communicate important constants about the symbol table
1180     to GDB's symbol-reading code.  These `constants' unfortunately vary
1181       from coff implementation to implementation...  */
1182 #ifndef NO_COFF_SYMBOLS
1183   coff->local_n_btmask = N_BTMASK;
1184   coff->local_n_btshft = N_BTSHFT;
1185   coff->local_n_tmask  = N_TMASK;
1186   coff->local_n_tshift = N_TSHIFT;
1187   coff->local_symesz   = SYMESZ;
1188   coff->local_auxesz   = AUXESZ;
1189   coff->local_linesz   = LINESZ;
1190 #endif
1191
1192   coff->symbols = (coff_symbol_type *) NULL;
1193   bfd_get_start_address(abfd) = internal_f->f_opthdr ? internal_a->entry : 0;
1194
1195   return abfd->xvec;
1196  fail:
1197   bfd_release(abfd, coff);
1198   return (bfd_target *)NULL;
1199 }
1200
1201 static bfd_target *
1202 DEFUN(coff_object_p,(abfd),
1203       bfd            *abfd)
1204 {
1205   int   nscns;
1206   FILHDR filehdr;
1207   AOUTHDR opthdr;
1208   struct internal_filehdr internal_f;
1209   struct internal_aouthdr internal_a;
1210
1211   bfd_error = system_call_error;
1212
1213   /* figure out how much to read */
1214   if (bfd_read((PTR) &filehdr, 1, FILHSZ, abfd) != FILHSZ)
1215     return 0;
1216
1217   bfd_swap_filehdr_in(abfd, &filehdr, &internal_f);
1218
1219   if (BADMAG(internal_f)) {
1220     bfd_error = wrong_format;
1221     return 0;
1222   }
1223   nscns =internal_f.f_nscns;
1224
1225   if (internal_f.f_opthdr) {
1226     if (bfd_read((PTR) &opthdr, 1,AOUTSZ, abfd) != AOUTSZ) {
1227       return 0;
1228     }
1229     bfd_swap_aouthdr_in(abfd, (char *)&opthdr, (char *)&internal_a);
1230   }
1231
1232   /* Seek past the opt hdr stuff */
1233   bfd_seek(abfd, internal_f.f_opthdr + FILHSZ, SEEK_SET);
1234
1235   /* if the optional header is NULL or not the correct size then
1236      quit; the only difference I can see between m88k dgux headers (MC88DMAGIC)
1237      and Intel 960 readwrite headers (I960WRMAGIC) is that the
1238      optional header is of a different size.
1239
1240      But the mips keeps extra stuff in it's opthdr, so dont check
1241      when doing that
1242      */
1243
1244 #if defined(M88) || defined(I960)
1245   if (internal_f.f_opthdr != 0 && AOUTSZ != internal_f.f_opthdr)
1246     return (bfd_target *)NULL;
1247 #endif
1248
1249   return coff_real_object_p(abfd, nscns, &internal_f, &internal_a);
1250 }
1251
1252
1253
1254 #ifndef NO_COFF_LINENOS
1255
1256 static void
1257 DEFUN(coff_count_linenumbers,(abfd),
1258       bfd            *abfd)
1259 {
1260   unsigned int    limit = bfd_get_symcount(abfd);
1261   unsigned int    i;
1262   asymbol       **p;
1263     {
1264       asection       *s = abfd->sections->output_section;
1265       while (s) {
1266         BFD_ASSERT(s->lineno_count == 0);
1267         s = s->next;
1268       }
1269     }
1270
1271
1272   for (p = abfd->outsymbols, i = 0; i < limit; i++, p++) {
1273     asymbol        *q_maybe = *p;
1274     if (q_maybe->the_bfd->xvec->flavour == bfd_target_coff_flavour) {
1275       coff_symbol_type *q = coffsymbol(q_maybe);
1276       if (q->lineno) {
1277         /*
1278           This symbol has a linenumber, increment the owning
1279           section's linenumber count
1280           */
1281         alent          *l = q->lineno;
1282         q->symbol.section->output_section->lineno_count++;
1283         l++;
1284         while (l->line_number) {
1285           q->symbol.section->output_section->lineno_count++;
1286           l++;
1287         }
1288       }
1289     }
1290   }
1291 }
1292
1293 #endif /* NO_COFF_LINENOS */
1294
1295 #ifndef NO_COFF_SYMBOLS
1296
1297 /*
1298   Takes a bfd and a symbol, returns a pointer to the coff specific area
1299   of the symbol if there is one.
1300   */
1301 static coff_symbol_type *
1302 DEFUN(coff_symbol_from,(ignore_abfd, symbol),
1303       bfd            *ignore_abfd AND
1304       asymbol        *symbol)
1305 {
1306   if (symbol->the_bfd->xvec->flavour != bfd_target_coff_flavour)
1307     return (coff_symbol_type *)NULL;
1308
1309   if (symbol->the_bfd->tdata.coff_obj_data == (coff_data_type*)NULL)
1310     return (coff_symbol_type *)NULL;
1311
1312   return  (coff_symbol_type *) symbol;
1313 }
1314
1315
1316
1317 static void
1318 DEFUN(fixup_symbol_value,(coff_symbol_ptr, syment),
1319 coff_symbol_type *coff_symbol_ptr AND
1320 struct internal_syment *syment)
1321 {
1322
1323   /* Normalize the symbol flags */
1324   if (coff_symbol_ptr->symbol.section == &bfd_com_section) {
1325     /* a common symbol is undefined with a value */
1326     syment->n_scnum = N_UNDEF;
1327     syment->n_value = coff_symbol_ptr->symbol.value;
1328   }
1329   else if (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) {
1330     syment->n_value = coff_symbol_ptr->symbol.value;
1331   }
1332   else if (coff_symbol_ptr->symbol.section == & bfd_und_section) {
1333     syment->n_scnum = N_UNDEF;
1334     syment->n_value = 0;
1335   }
1336   else {
1337     if (coff_symbol_ptr->symbol.section) {
1338       syment->n_scnum    =
1339        coff_symbol_ptr->symbol.section->output_section->target_index;
1340
1341       syment->n_value =
1342        coff_symbol_ptr->symbol.value +
1343         coff_symbol_ptr->symbol.section->output_offset +
1344          coff_symbol_ptr->symbol.section->output_section->vma;
1345     }
1346     else {
1347         BFD_ASSERT(0);
1348       /* This can happen, but I don't know why yet (steve@cygnus.com) */
1349       syment->n_scnum = N_ABS;
1350       syment->n_value = coff_symbol_ptr->symbol.value;
1351     }
1352   }
1353 }
1354
1355 /* run through all the symbols in the symbol table and work out what
1356    their indexes into the symbol table will be when output
1357
1358  Coff requires that each C_FILE symbol points to the next one in the
1359  chain, and that the last one points to the first external symbol. We
1360  do that here too.
1361
1362 */
1363 static void
1364 DEFUN(coff_renumber_symbols,(bfd_ptr),
1365       bfd *bfd_ptr)
1366 {
1367   unsigned int symbol_count = bfd_get_symcount(bfd_ptr);
1368   asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
1369   unsigned int native_index = 0;
1370   struct internal_syment *last_file = (struct internal_syment *)NULL;
1371   unsigned int symbol_index;
1372
1373   /* COFF demands that undefined symbols come after all other symbols.
1374      Since we don't need to impose this extra knowledge on all our client
1375      programs, deal with that here.  Sort the symbol table; just move the
1376      undefined symbols to the end, leaving the rest alone.  */
1377   /* @@ Do we have some condition we could test for, so we don't always
1378      have to do this?  I don't think relocatability is quite right, but
1379      I'm not certain.  [raeburn:19920508.1711EST]  */
1380   {
1381     asymbol **newsyms;
1382     int i;
1383
1384     newsyms = (asymbol **) bfd_alloc_by_size_t (bfd_ptr,
1385                                                 sizeof (asymbol *)
1386                                                 * (symbol_count + 1));
1387     bfd_ptr->outsymbols = newsyms;
1388     for (i = 0; i < symbol_count; i++)
1389       if (symbol_ptr_ptr[i]->section != &bfd_und_section)
1390         *newsyms++ = symbol_ptr_ptr[i];
1391     for (i = 0; i < symbol_count; i++)
1392       if (symbol_ptr_ptr[i]->section == &bfd_und_section)
1393         *newsyms++ = symbol_ptr_ptr[i];
1394     *newsyms = (asymbol *) NULL;
1395     symbol_ptr_ptr = bfd_ptr->outsymbols;
1396   }
1397
1398   for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
1399       {
1400         coff_symbol_type *coff_symbol_ptr = coff_symbol_from(bfd_ptr, symbol_ptr_ptr[symbol_index]);
1401         if (coff_symbol_ptr && coff_symbol_ptr->native) {
1402           combined_entry_type *s = coff_symbol_ptr->native;
1403           int i;
1404
1405           if (s->u.syment.n_sclass == C_FILE)
1406               {
1407                 if (last_file != (struct internal_syment *)NULL) {
1408                   last_file->n_value = native_index;
1409                 }
1410                 last_file = &(s->u.syment);
1411               }
1412           else {
1413
1414             /* Modify the symbol values according to their section and
1415                type */
1416
1417             fixup_symbol_value(coff_symbol_ptr, &(s->u.syment));
1418           }
1419           for (i = 0; i < s->u.syment.n_numaux + 1; i++) {
1420             s[i].offset = native_index ++;
1421           }
1422         }
1423         else {
1424           native_index++;
1425         }
1426       }
1427   obj_conv_table_size (bfd_ptr) = native_index;
1428 }
1429
1430
1431 /*
1432  Run thorough the symbol table again, and fix it so that all pointers to
1433  entries are changed to the entries' index in the output symbol table.
1434
1435 */
1436 static void
1437 DEFUN(coff_mangle_symbols,(bfd_ptr),
1438       bfd *bfd_ptr)
1439 {
1440   unsigned int symbol_count = bfd_get_symcount(bfd_ptr);
1441   asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
1442   unsigned int symbol_index;
1443
1444   for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
1445       {
1446         coff_symbol_type *coff_symbol_ptr =
1447           coff_symbol_from(bfd_ptr, symbol_ptr_ptr[symbol_index]);
1448
1449         if (coff_symbol_ptr && coff_symbol_ptr->native) {
1450           int i;
1451           combined_entry_type *s = coff_symbol_ptr->native;
1452
1453           for (i = 0; i < s->u.syment.n_numaux ; i++) {
1454             combined_entry_type *a = s + i + 1;
1455             if (a->fix_tag) {
1456               a->u.auxent.x_sym.x_tagndx.l =
1457                 a->u.auxent.x_sym.x_tagndx.p->offset;
1458               a->fix_tag = 0;
1459             }
1460             if (a->fix_end) {
1461               a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
1462                 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
1463               a->fix_end = 0;
1464               
1465             }
1466
1467           }
1468         }
1469       }
1470 }
1471
1472 static int string_size;
1473 static void
1474 DEFUN(coff_fix_symbol_name,(ignore_abfd, symbol, native),
1475   bfd *ignore_abfd AND
1476   asymbol *symbol AND
1477   combined_entry_type *native)
1478 {
1479   unsigned int    name_length;
1480   union internal_auxent *auxent;
1481   char *  name = ( char *)(symbol->name);
1482
1483   if (name == (char *) NULL) {
1484     /* coff symbols always have names, so we'll make one up */
1485     symbol->name = "strange";
1486     name = (char *)symbol->name;
1487   }
1488   name_length = strlen(name);
1489
1490   if (native->u.syment.n_sclass == C_FILE) {
1491     strncpy(native->u.syment._n._n_name, ".file", SYMNMLEN);
1492     auxent = &(native+1)->u.auxent;
1493
1494 #ifdef COFF_LONG_FILENAMES
1495     if (name_length <= FILNMLEN) {
1496       strncpy(auxent->x_file.x_fname, name, FILNMLEN);
1497     }
1498     else {
1499       auxent->x_file.x_n.x_offset = string_size + 4;
1500       auxent->x_file.x_n.x_zeroes = 0;
1501       string_size += name_length + 1;
1502     }
1503 #else
1504     strncpy(auxent->x_file.x_fname, name, FILNMLEN);
1505     if (name_length > FILNMLEN) {
1506       name[FILNMLEN] = '\0';
1507     }
1508 #endif
1509   }
1510   else
1511       {                         /* NOT A C_FILE SYMBOL */
1512         if (name_length <= SYMNMLEN) {
1513           /* This name will fit into the symbol neatly */
1514           strncpy(native->u.syment._n._n_name, symbol->name, SYMNMLEN);
1515         }
1516         else {
1517           native->u.syment._n._n_n._n_offset =  string_size + 4;
1518           native->u.syment._n._n_n._n_zeroes = 0;
1519           string_size += name_length + 1;
1520         }
1521       }
1522 }
1523
1524
1525
1526 static unsigned int
1527 DEFUN(coff_write_symbol,(abfd, symbol, native, written),
1528 bfd *abfd AND
1529 asymbol *symbol AND
1530 combined_entry_type *native AND
1531 unsigned int written)
1532 {
1533   unsigned int    numaux = native->u.syment.n_numaux;
1534   int             type = native->u.syment.n_type;
1535   int             class =  native->u.syment.n_sclass;
1536   SYMENT buf;
1537   unsigned int j;
1538
1539   /* @@ bfd_debug_section isn't accessible outside this file, but we know
1540      that C_FILE symbols belong there.  So move them.  */
1541   if (native->u.syment.n_sclass == C_FILE)
1542     symbol->section = &bfd_debug_section;
1543
1544   if (symbol->section == &bfd_abs_section) 
1545   {
1546     native->u.syment.n_scnum = N_ABS;
1547   }
1548   else if (symbol->section == &bfd_debug_section) 
1549   {
1550     native->u.syment.n_scnum = N_DEBUG;
1551   }
1552   else if (symbol->section == &bfd_und_section)   
1553   {
1554     native->u.syment.n_scnum = N_UNDEF;
1555   }
1556   else 
1557   {
1558     native->u.syment.n_scnum =
1559      symbol->section->output_section->target_index;
1560   }
1561   
1562   
1563   coff_fix_symbol_name(abfd, symbol, native);
1564
1565   coff_swap_sym_out(abfd, &native->u.syment, &buf);
1566   bfd_write((PTR)& buf, 1, SYMESZ, abfd);
1567   for (j = 0; j < native->u.syment.n_numaux;  j++)
1568   {
1569     AUXENT buf1;
1570     bzero((PTR)&buf, AUXESZ);
1571     coff_swap_aux_out(abfd,
1572                       &( (native + j + 1)->u.auxent), type, class, &buf1);
1573     bfd_write((PTR) (&buf1), 1, AUXESZ, abfd);
1574   }
1575   /*
1576     Reuse somewhere in the symbol to keep the index
1577     */
1578   set_index(symbol, written);
1579   return   written + 1 + numaux;
1580 }
1581
1582
1583 static unsigned int
1584 DEFUN(coff_write_alien_symbol,(abfd, symbol, written),
1585       bfd *abfd AND
1586       asymbol *symbol AND
1587       unsigned int written)
1588 {
1589   /*
1590     This symbol has been created by the loader, or come from a non
1591     coff format. It  has no native element to inherit, make our
1592     own
1593     */
1594   combined_entry_type *native;
1595   combined_entry_type dummy;
1596   native = &dummy;
1597   native->u.syment.n_type =  T_NULL;
1598 #ifdef I960
1599   native->u.syment.n_flags =  0;
1600 #endif
1601   if (symbol->section == &bfd_und_section) 
1602   {
1603       native->u.syment.n_scnum =  N_UNDEF;
1604       native->u.syment.n_value =  symbol->value;
1605     }
1606   else if (symbol->section == &bfd_com_section) 
1607   {
1608       native->u.syment.n_scnum =  N_UNDEF;
1609       native->u.syment.n_value =  symbol->value;
1610
1611   }
1612   
1613   else if (symbol->flags & BSF_DEBUGGING) {
1614       /*
1615         remove name so it doesn't take up any space
1616         */
1617       symbol->name = "";
1618     }
1619   else {
1620       native->u.syment.n_scnum  =   symbol->section->output_section->target_index;
1621       native->u.syment.n_value =   symbol->value +
1622        symbol->section->output_section->vma +
1623         symbol->section->output_offset;
1624 #ifdef I960
1625       /* Copy the any flags from the the file hdr into the symbol  */
1626     {
1627       coff_symbol_type *c = coff_symbol_from(abfd, symbol);
1628       if (c != (coff_symbol_type *)NULL) {
1629           native->u.syment.n_flags =   c->symbol.the_bfd->flags;
1630         }
1631     }
1632 #endif
1633     }
1634
1635 #ifdef HASPAD1
1636   native->u.syment.pad1[0] = 0;
1637   native->u.syment.pad1[0] = 0;
1638 #endif
1639
1640   native->u.syment.n_type =  0;
1641   if (symbol->flags & BSF_LOCAL)
1642    native->u.syment.n_sclass =  C_STAT;
1643   else
1644    native->u.syment.n_sclass =  C_EXT;
1645   native->u.syment.n_numaux =  0;
1646
1647   return   coff_write_symbol(abfd, symbol, native, written);
1648 }
1649
1650 static unsigned int
1651 DEFUN(coff_write_native_symbol,(abfd, symbol,   written),
1652 bfd *abfd AND
1653 coff_symbol_type *symbol AND
1654 unsigned int written)
1655 {
1656   /*
1657     Does this symbol have an ascociated line number - if so then
1658     make it remember this symbol index. Also tag the auxent of
1659     this symbol to point to the right place in the lineno table
1660     */
1661   combined_entry_type *native = symbol->native;
1662
1663   alent          *lineno = symbol->lineno;
1664
1665   if (lineno && !symbol->done_lineno) {
1666     unsigned int    count = 0;
1667     lineno[count].u.offset = written;
1668     if (native->u.syment.n_numaux) {
1669       union internal_auxent  *a = &((native+1)->u.auxent);
1670
1671       a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
1672         symbol->symbol.section->output_section->moving_line_filepos;
1673     }
1674     /*
1675       And count and relocate all other linenumbers
1676       */
1677
1678     count++;
1679     while (lineno[count].line_number) {
1680 #if 0
1681 /* 13 april 92. sac 
1682 I've been told this, but still need proof:
1683 > The second bug is also in `bfd/coffcode.h'.  This bug causes the linker to screw
1684 > up the pc-relocations for all the line numbers in COFF code.  This bug isn't
1685 > only specific to A29K implementations, but affects all systems using COFF
1686 > format binaries.  Note that in COFF object files, the line number core offsets
1687 > output by the assembler are relative to the start of each procedure, not
1688 > to the start of the .text section.  This patch relocates the line numbers
1689 > relative to the `native->u.syment.n_value' instead of the section virtual
1690 > address.  modular!olson@cs.arizona.edu (Jon Olson)
1691 */
1692        lineno[count].u.offset += native->u.syment.n_value;
1693
1694 #else
1695       lineno[count].u.offset +=
1696         symbol->symbol.section->output_section->vma +
1697           symbol->symbol.section->output_offset;
1698 #endif
1699       count++;
1700     }
1701     symbol->done_lineno = true;
1702     
1703     symbol->symbol.section->output_section->moving_line_filepos +=
1704       count * LINESZ;
1705   }
1706   return coff_write_symbol(abfd, &( symbol->symbol), native,written);
1707 }
1708
1709 static void
1710 DEFUN(coff_write_symbols,(abfd),
1711       bfd            *abfd)
1712 {
1713   unsigned int    i;
1714   unsigned int    limit = bfd_get_symcount(abfd);
1715   unsigned int    written = 0;
1716
1717   asymbol       **p;
1718
1719   string_size = 0;
1720
1721
1722   /* Seek to the right place */
1723   bfd_seek(abfd, obj_sym_filepos(abfd), SEEK_SET);
1724
1725   /* Output all the symbols we have */
1726
1727   written = 0;
1728   for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
1729       {
1730         asymbol        *symbol = *p;
1731         coff_symbol_type *c_symbol = coff_symbol_from(abfd, symbol);
1732
1733         if (c_symbol == (coff_symbol_type *) NULL ||
1734             c_symbol->native == (combined_entry_type *)NULL)
1735             {
1736               written = coff_write_alien_symbol(abfd, symbol, written);
1737             }
1738         else
1739             {
1740               written = coff_write_native_symbol(abfd, c_symbol, written);
1741             }
1742
1743       }
1744
1745   bfd_get_symcount(abfd) = written;
1746
1747   /* Now write out strings */
1748
1749   if (string_size != 0)
1750    {
1751      unsigned int    size = string_size + 4;
1752      bfd_byte buffer[4];
1753
1754      bfd_h_put_32(abfd, size, buffer);
1755      bfd_write((PTR) buffer, 1, sizeof(buffer), abfd);
1756      for (p = abfd->outsymbols, i = 0;
1757           i < limit;
1758           i++, p++)
1759          {
1760            asymbol        *q = *p;
1761            size_t          name_length = strlen(q->name);
1762            int maxlen;
1763            coff_symbol_type*       c_symbol = coff_symbol_from(abfd, q);
1764            maxlen = ((c_symbol != NULL && c_symbol->native != NULL) &&
1765                      (c_symbol->native->u.syment.n_sclass == C_FILE)) ?
1766              FILNMLEN : SYMNMLEN;
1767
1768            if (name_length > maxlen) {
1769              bfd_write((PTR) (q->name), 1, name_length + 1, abfd);
1770            }
1771          }
1772    }
1773   else {
1774     /* We would normally not write anything here, but we'll write
1775        out 4 so that any stupid coff reader which tries to read
1776        the string table even when there isn't one won't croak.
1777        */
1778
1779     uint32e_type size = 4;
1780     size =  size;
1781     bfd_write((PTR)&size, 1, sizeof(size), abfd);
1782
1783   }
1784 }
1785
1786 /*
1787 SUBSUBSECTION
1788         Writing Relocations
1789
1790         To write relocations, all the back end does is step though the
1791         canonical relocation table, and create an
1792         @code{internal_reloc}. The symbol index to use is removed from
1793         the @code{offset} field in the symbol table supplied, the
1794         address comes directly from the sum of the section base
1795         address and the relocation offset and the type is dug directly
1796         from the howto field.  Then the @code{internal_reloc} is
1797         swapped into the shape of an @code{external_reloc} and written
1798         out to disk. 
1799
1800 */
1801
1802 static void
1803 DEFUN(coff_write_relocs,(abfd),
1804       bfd            *abfd)
1805 {
1806   asection       *s;
1807   for (s = abfd->sections; s != (asection *) NULL; s = s->next) {
1808     unsigned int    i;
1809     struct external_reloc dst;
1810
1811     arelent       **p = s->orelocation;
1812     bfd_seek(abfd, s->rel_filepos, SEEK_SET);
1813     for (i = 0; i < s->reloc_count; i++) {
1814       struct internal_reloc    n;
1815       arelent        *q = p[i];
1816       memset((PTR)&n, 0, sizeof(n));
1817
1818
1819 #ifndef SWAP_OUT_RELOC_OFFSET
1820       /* @@FIXME COFF relocs don't support addends.  Code should probably be
1821          in the target-independent code, using a target flag to decide whether
1822          to fold the addend into the section contents.  */
1823
1824       if (q->addend != 0)
1825 #ifdef R_IHCONST
1826         if (q->howto->type != R_IHCONST)
1827 #endif
1828           abort ();
1829 #endif
1830
1831       n.r_vaddr = q->address + s->vma;
1832       /* The 29k const/consth reloc pair is a real kludge - the consth
1833          part doesn't have a symbol - it has an offset. So rebuilt
1834          that here */
1835 #ifdef R_IHCONST                       
1836       if (q->howto->type == R_IHCONST)
1837         n.r_symndx = q->addend;
1838       else
1839 #endif
1840
1841       if (q->sym_ptr_ptr) {
1842         n.r_symndx = get_index((*(q->sym_ptr_ptr)));
1843         /* Take notice if the symbol reloc points to a symbol we don't have
1844            in our symbol table.  What should we do for this??  */
1845         if (n.r_symndx > obj_conv_table_size (abfd))
1846           abort ();
1847       }
1848 #ifdef SELECT_RELOC
1849       /* Work out reloc type from what is required */
1850       SELECT_RELOC(n.r_type, q->howto);
1851 #else
1852       n.r_type = q->howto->type;
1853 #endif
1854       coff_swap_reloc_out(abfd, &n, &dst);
1855       bfd_write((PTR) &dst, 1, RELSZ, abfd);
1856     }
1857   }
1858 }
1859 #endif /* NO_COFF_SYMBOLS */
1860
1861 #ifndef NO_COFF_LINENOS
1862
1863 static void
1864 DEFUN(coff_write_linenumbers,(abfd),
1865       bfd            *abfd)
1866 {
1867   asection       *s;
1868   for (s = abfd->sections; s != (asection *) NULL; s = s->next) {
1869     if (s->lineno_count) {
1870       asymbol       **q = abfd->outsymbols;
1871       bfd_seek(abfd, s->line_filepos, SEEK_SET);
1872       /* Find all the linenumbers in this section */
1873       while (*q) {
1874         asymbol        *p = *q;
1875         alent          *l = BFD_SEND(p->the_bfd, _get_lineno, (p->the_bfd, p));
1876         if (l) {
1877           /* Found a linenumber entry, output */
1878           struct internal_lineno  out;
1879           LINENO buff;
1880           memset( (PTR)&out, 0, sizeof(out));
1881           out.l_lnno = 0;
1882           out.l_addr.l_symndx = l->u.offset;
1883           coff_swap_lineno_out(abfd, &out, &buff);
1884           bfd_write((PTR) &buff, 1, LINESZ, abfd);
1885           l++;
1886           while (l->line_number) {
1887             out.l_lnno = l->line_number;
1888             out.l_addr.l_symndx = l->u.offset;
1889             coff_swap_lineno_out(abfd, &out, &buff);
1890             bfd_write((PTR) &buff, 1, LINESZ, abfd);
1891             l++;
1892           }
1893         }
1894         q++;
1895       }
1896     }
1897   }
1898 }
1899
1900 static alent   *
1901 DEFUN(coff_get_lineno,(ignore_abfd, symbol),
1902       bfd            *ignore_abfd AND
1903       asymbol        *symbol)
1904 {
1905   return coffsymbol(symbol)->lineno;
1906 }
1907
1908 #endif /* NO_COFF_LINENOS */
1909
1910 static asymbol *
1911 coff_make_empty_symbol(abfd)
1912 bfd            *abfd;
1913 {
1914   coff_symbol_type *new = (coff_symbol_type *) bfd_alloc(abfd, sizeof(coff_symbol_type));
1915   if (new == NULL) {
1916     bfd_error = no_memory;
1917     return (NULL);
1918   }                             /* on error */
1919   new->symbol.section = 0;
1920   new->native = 0;
1921   new->lineno = (alent *) NULL;
1922   new->done_lineno = false;
1923   new->symbol.the_bfd = abfd;
1924   return &new->symbol;
1925 }
1926
1927 #ifndef NO_COFF_SYMBOLS
1928
1929 static asymbol *
1930 DEFUN (coff_make_debug_symbol, (abfd, ptr, sz),
1931        bfd *abfd AND
1932        PTR ptr AND
1933        unsigned long sz)
1934 {
1935   coff_symbol_type *new = (coff_symbol_type *) bfd_alloc(abfd, sizeof(coff_symbol_type));
1936   if (new == NULL) {
1937     bfd_error = no_memory;
1938     return (NULL);
1939   }                             /* on error */
1940   /* @@ This shouldn't be using a constant multiplier.  */
1941   new->native = (combined_entry_type *) bfd_zalloc (abfd, sizeof (combined_entry_type) * 10);
1942   new->symbol.section = &bfd_debug_section;
1943   new->lineno = (alent *) NULL;
1944   new->done_lineno = false;
1945   new->symbol.the_bfd = abfd;
1946   return &new->symbol;
1947 }
1948
1949 static void
1950 DEFUN(coff_print_symbol,(ignore_abfd, filep, symbol, how),
1951       bfd            *ignore_abfd AND
1952       PTR           filep AND
1953       asymbol        *symbol AND
1954       bfd_print_symbol_type how)
1955 {
1956   FILE *file = (FILE *)filep;
1957   switch (how) {
1958     case bfd_print_symbol_name:
1959       fprintf(file, "%s", symbol->name);
1960       break;
1961     case bfd_print_symbol_more:
1962       fprintf(file, "coff %lx %lx", (unsigned long) coffsymbol(symbol)->native,
1963               (unsigned long) coffsymbol(symbol)->lineno);
1964       break;
1965     case bfd_print_symbol_nm:
1966
1967     {
1968       CONST char *section_name = symbol->section->name;
1969       bfd_print_symbol_vandf((PTR) file, symbol);
1970
1971         
1972       fprintf(file, " %-5s %s %s %s",
1973               section_name,
1974               coffsymbol(symbol)->native ? "n" : "g",
1975               coffsymbol(symbol)->lineno ? "l" : " ",
1976               symbol->name);
1977     }
1978
1979
1980       break;
1981     case bfd_print_symbol_all:
1982       /* Print out the symbols in a reasonable way */
1983     {
1984       CONST char *section_name = symbol->section->name;
1985
1986
1987       if (coffsymbol(symbol)->native) 
1988       {
1989         unsigned int aux;
1990         combined_entry_type *combined = coffsymbol(symbol)->native;
1991         combined_entry_type *root = obj_raw_syments(ignore_abfd);
1992         
1993         fprintf(file,"[%3d]",
1994                 combined - root);
1995         
1996
1997         fprintf(file, "(sc %2d)(fl%4x)(ty%3x)(sc%3d) nx(%d) %08x %s",
1998                 combined->u.syment.n_scnum,
1999                 combined->u.syment.n_flags,
2000                 combined->u.syment.n_type,
2001                 combined->u.syment.n_sclass,
2002                 combined->u.syment.n_numaux,
2003                 combined->u.syment.n_value,
2004                 symbol->name
2005                 );
2006         for (aux = 0; aux < combined->u.syment.n_numaux; aux++) 
2007         {
2008           fprintf(file,"\n");
2009           switch (combined->u.syment.n_sclass) {
2010             case C_FILE:
2011               fprintf(file, "File ");
2012               break;
2013             default:
2014               fprintf(file, "AUX lnno %x size %x tagndx %x",
2015                       combined[aux+1].u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
2016                       combined[aux+1].u.auxent.x_sym.x_misc.x_lnsz.x_size,
2017                       combined[aux+1].u.auxent.x_sym.x_tagndx.l);
2018               break;
2019     
2020             }
2021
2022         }
2023         
2024       {
2025         struct lineno_cache_entry *l = coffsymbol(symbol)->lineno;
2026         if (l) 
2027         {
2028           printf("\n%s :", l->u.sym->name);
2029           l++;
2030           while (l->line_number) 
2031           {
2032             printf("\n%4d : %x", 
2033                    l->line_number,
2034                    l->u.offset);
2035             l++;
2036             
2037           }
2038         }
2039       }
2040
2041     
2042
2043       } 
2044
2045       else {
2046           bfd_print_symbol_vandf((PTR) file, symbol);
2047           fprintf(file, " %-5s %s %s %s",
2048                   section_name,
2049                   coffsymbol(symbol)->native ? "n" : "g",
2050                   coffsymbol(symbol)->lineno ? "l" : " ",
2051                   symbol->name);
2052         }
2053
2054     }
2055         
2056     }
2057 }
2058
2059 #endif /* NO_COFF_SYMBOLS */
2060
2061 /* Set flags and magic number of a coff file from architecture and machine
2062    type.  Result is true if we can represent the arch&type, false if not.  */
2063
2064 static          boolean
2065 DEFUN(coff_set_flags,(abfd, magicp, flagsp),
2066       bfd            *abfd AND
2067       unsigned       *magicp AND
2068       unsigned short *flagsp)
2069 {
2070   switch (bfd_get_arch(abfd)) {
2071 #ifdef Z8KMAGIC
2072    case bfd_arch_z8k:
2073     *magicp = Z8KMAGIC;
2074     switch (bfd_get_mach(abfd)) 
2075     {
2076      case bfd_mach_z8001:
2077       *flagsp = F_Z8001;
2078       break;
2079      case bfd_mach_z8002:
2080       *flagsp = F_Z8002;
2081       break;
2082      default:
2083       return false;
2084     }
2085     return true;
2086 #endif
2087 #ifdef I960ROMAGIC
2088
2089     case bfd_arch_i960:
2090
2091       {
2092         unsigned        flags;
2093         *magicp = I960ROMAGIC;
2094         /*
2095           ((bfd_get_file_flags(abfd) & WP_TEXT) ? I960ROMAGIC :
2096           I960RWMAGIC);   FIXME???
2097           */
2098         switch (bfd_get_mach(abfd)) {
2099         case bfd_mach_i960_core:
2100           flags = F_I960CORE;
2101           break;
2102         case bfd_mach_i960_kb_sb:
2103           flags = F_I960KB;
2104           break;
2105         case bfd_mach_i960_mc:
2106           flags = F_I960MC;
2107           break;
2108         case bfd_mach_i960_xa:
2109           flags = F_I960XA;
2110           break;
2111         case bfd_mach_i960_ca:
2112           flags = F_I960CA;
2113           break;
2114         case bfd_mach_i960_ka_sa:
2115           flags = F_I960KA;
2116           break;
2117         default:
2118           return false;
2119         }
2120         *flagsp = flags;
2121         return true;
2122       }
2123     break;
2124 #endif
2125 #ifdef MIPS
2126   case bfd_arch_mips:
2127     *magicp = MIPS_MAGIC_2;
2128     return true;
2129     break;
2130 #endif
2131 #ifdef I386MAGIC
2132   case bfd_arch_i386:
2133     *magicp = I386MAGIC;
2134     return true;
2135     break;
2136 #endif
2137 #ifdef MC68MAGIC
2138   case bfd_arch_m68k:
2139     *magicp = MC68MAGIC;
2140     return true;
2141     break;
2142 #endif
2143
2144 #ifdef MC88MAGIC
2145     case bfd_arch_m88k:
2146       *magicp = MC88OMAGIC;
2147       return true;
2148       break;
2149 #endif
2150 #ifdef H8300MAGIC
2151     case bfd_arch_h8300:
2152       *magicp = H8300MAGIC;
2153       return true;
2154       break;
2155 #endif
2156 #ifdef A29K_MAGIC_BIG
2157     case bfd_arch_a29k:
2158       if (abfd->xvec->byteorder_big_p)
2159        *magicp = A29K_MAGIC_BIG;
2160       else
2161        *magicp = A29K_MAGIC_LITTLE;
2162       return true;
2163       break;
2164 #endif
2165
2166 #ifdef WE32KMAGIC
2167   case bfd_arch_we32k:
2168     *magicp = WE32KMAGIC;
2169     return true;
2170     break;
2171 #endif
2172
2173 #ifdef U802TOCMAGIC
2174   case bfd_arch_rs6000:
2175     *magicp = U802TOCMAGIC;
2176     return true;
2177     break;
2178 #endif
2179
2180   default:                      /* Unknown architecture */
2181     /* return false;  -- fall through to "return false" below, to avoid
2182        "statement never reached" errors on the one below. */
2183     break;
2184   }
2185
2186   return false;
2187 }
2188
2189
2190 static          boolean
2191 DEFUN(coff_set_arch_mach,(abfd, arch, machine),
2192       bfd            *abfd AND
2193       enum bfd_architecture arch AND
2194       unsigned long   machine)
2195 {
2196   unsigned        dummy1;
2197   unsigned     short dummy2;
2198   bfd_default_set_arch_mach(abfd, arch, machine);
2199
2200   if (arch != bfd_arch_unknown &&
2201       coff_set_flags(abfd, &dummy1, &dummy2) != true)
2202     return false;               /* We can't represent this type */
2203   return true;                  /* We're easy ... */
2204 }
2205
2206
2207 /* Calculate the file position for each section. */
2208
2209 static void
2210 DEFUN(coff_compute_section_file_positions,(abfd),
2211       bfd            *abfd)
2212 {
2213   asection       *current;
2214   asection      *previous = (asection *)NULL;
2215   file_ptr        sofar = FILHSZ;
2216   file_ptr      old_sofar;
2217   if (bfd_get_start_address(abfd)) 
2218   {
2219     /*  A start address may have been added to the original file. In this
2220         case it will need an optional header to record it.  */
2221     abfd->flags |= EXEC_P;
2222   }
2223
2224   if (abfd->flags & EXEC_P)
2225    sofar += AOUTSZ;
2226
2227   sofar += abfd->section_count * SCNHSZ;
2228   for (current = abfd->sections;
2229        current != (asection *)NULL;
2230        current = current->next) {
2231
2232       /* Only deal with sections which have contents */
2233       if (!(current->flags & SEC_HAS_CONTENTS))
2234        continue;
2235
2236       /* Align the sections in the file to the same boundary on
2237          which they are aligned in virtual memory.  I960 doesn't
2238          do this (FIXME) so we can stay in sync with Intel.  960
2239          doesn't yet page from files... */
2240 #ifndef I960
2241     {
2242       /* make sure this section is aligned on the right boundary - by
2243          padding the previous section up if necessary */
2244
2245       old_sofar= sofar;
2246       sofar = BFD_ALIGN(sofar, 1 << current->alignment_power);
2247       if (previous != (asection *)NULL) {
2248           previous->_raw_size += sofar - old_sofar;
2249         }
2250     }
2251
2252 #endif
2253       /* FIXME, in demand paged files, the low order bits of the file
2254          offset must match the low order bits of the virtual address.
2255          "Low order" is apparently implementation defined.  Add code
2256          here to round sofar up to match the virtual address.  */
2257
2258       current->filepos = sofar;
2259
2260       sofar += current->_raw_size;
2261 #ifndef I960
2262       /* make sure that this section is of the right size too */
2263       old_sofar =  sofar;
2264       sofar = BFD_ALIGN(sofar, 1 << current->alignment_power);
2265       current->_raw_size += sofar - old_sofar ;
2266 #endif
2267
2268       previous = current;
2269     }
2270   obj_relocbase(abfd) = sofar;
2271 }
2272
2273 #ifndef NO_COFF_SYMBOLS
2274 static asymbol *
2275 coff_section_symbol (abfd, name)
2276      bfd *abfd;
2277      char *name;
2278 {
2279   asection *sec = bfd_get_section_by_name (abfd, name);
2280   asymbol *sym;
2281   combined_entry_type *csym;
2282
2283   if (!sec)
2284     {
2285       /* create empty symbol */
2286       abort ();
2287     }
2288   sym = sec->symbol;
2289   if (coff_symbol_from (abfd, sym))
2290     csym = coff_symbol_from (abfd, sym)->native;
2291   else
2292     csym = 0;
2293   /* Make sure back-end COFF stuff is there.  */
2294   if (csym == 0)
2295     {
2296       struct foo {
2297         coff_symbol_type sym;
2298         /* @@FIXME This shouldn't use a fixed size!!  */
2299         combined_entry_type e[10];
2300       };
2301       struct foo *f;
2302       f = (struct foo *) bfd_alloc_by_size_t (abfd, sizeof (*f));
2303       bzero ((char *) f, sizeof (*f));
2304       coff_symbol_from (abfd, sym)->native = csym = f->e;
2305     }
2306   csym[0].u.syment.n_sclass = C_STAT;
2307   csym[0].u.syment.n_numaux = 1;
2308 /*  SF_SET_STATICS (sym);       @@ ??? */
2309   if (sec)
2310     {
2311       csym[1].u.auxent.x_scn.x_scnlen = sec->_raw_size;
2312       csym[1].u.auxent.x_scn.x_nreloc = sec->reloc_count;
2313       csym[1].u.auxent.x_scn.x_nlinno = sec->lineno_count;
2314     }
2315   else
2316     {
2317       csym[1].u.auxent.x_scn.x_scnlen = 0;
2318       csym[1].u.auxent.x_scn.x_nreloc = 0;
2319       csym[1].u.auxent.x_scn.x_nlinno = 0;
2320     }
2321   return sym;
2322 }
2323
2324 /* If .file, .text, .data, .bss symbols are missing, add them.  */
2325 /* @@ Should we only be adding missing symbols, or overriding the aux
2326    values for existing section symbols?  */
2327 static void
2328 coff_add_missing_symbols (abfd)
2329      bfd *abfd;
2330 {
2331   unsigned int nsyms = bfd_get_symcount (abfd);
2332   asymbol **sympp = abfd->outsymbols;
2333   asymbol **sympp2;
2334   unsigned int i;
2335   int need_text = 1, need_data = 1, need_bss = 1, need_file = 1;
2336   coff_data_type *cdata = coff_data (abfd);
2337
2338   for (i = 0; i < nsyms; i++)
2339     {
2340       coff_symbol_type *csym = coff_symbol_from (abfd, sympp[i]);
2341       CONST char *name;
2342       if (csym) 
2343       {
2344         /* only do this if there is a coff representation of the input
2345            symbol */
2346         if (csym->native && csym->native->u.syment.n_sclass == C_FILE)
2347         {
2348           need_file = 0;
2349           continue;
2350         }
2351         name = csym->symbol.name;
2352         if (!name)
2353          continue;
2354         if (!strcmp (name, _TEXT))
2355          need_text = 0;
2356         else if (!strcmp (name, _DATA))
2357          need_data = 0;
2358         else if (!strcmp (name, _BSS))
2359          need_bss = 0;
2360       }
2361     }
2362   /* Now i == bfd_get_symcount (abfd).  */
2363   /* @@ For now, don't deal with .file symbol.  */
2364   need_file = 0;
2365
2366   if (!need_text && !need_data && !need_bss && !need_file)
2367     return;
2368   nsyms += need_text + need_data + need_bss + need_file;
2369   sympp2 = (asymbol**) bfd_alloc_by_size_t (abfd, nsyms * sizeof (asymbol *));
2370   memcpy (sympp2, sympp, i * sizeof (asymbol *));
2371   if (need_file)
2372     {
2373       /* @@ Generate fake .file symbol, in sympp2[i], and increment i.  */
2374       abort ();
2375     }
2376   if (need_text)
2377     sympp2[i++] = coff_section_symbol (abfd, _TEXT);
2378   if (need_data)
2379     sympp2[i++] = coff_section_symbol (abfd, _DATA);
2380   if (need_bss)
2381     sympp2[i++] = coff_section_symbol (abfd, _BSS);
2382   BFD_ASSERT (i == nsyms);
2383   bfd_set_symtab (abfd, sympp2, nsyms);
2384 }
2385 #endif /* NO_COFF_SYMBOLS */
2386
2387 /* SUPPRESS 558 */
2388 /* SUPPRESS 529 */
2389 static          boolean
2390 DEFUN(coff_write_object_contents,(abfd),
2391       bfd            *abfd)
2392 {
2393   asection       *current;
2394   unsigned int count;
2395   
2396   boolean         hasrelocs = false;
2397   boolean         haslinno = false;
2398   file_ptr        reloc_base;
2399   file_ptr        lineno_base;
2400   file_ptr        sym_base;
2401   file_ptr        scn_base;
2402   file_ptr        data_base;
2403   unsigned long   reloc_size = 0;
2404   unsigned long   lnno_size = 0;
2405   asection       *text_sec = NULL;
2406   asection       *data_sec = NULL;
2407   asection       *bss_sec = NULL;
2408
2409   struct internal_filehdr internal_f;
2410   struct internal_aouthdr internal_a;
2411
2412
2413   bfd_error = system_call_error;
2414   /* Number the output sections, starting from one on the first section
2415      with a name which doesn't start with a *.
2416      @@ The code doesn't make this check.  Is it supposed to be done,
2417      or isn't it??  */
2418   count = 1;
2419   for (current = abfd->sections; current != (asection *)NULL; 
2420        current = current->next) 
2421   {
2422       current->target_index = count;
2423       count++;
2424   }
2425   
2426     
2427
2428
2429
2430   if(abfd->output_has_begun == false) {
2431       coff_compute_section_file_positions(abfd);
2432     }
2433
2434   if (abfd->sections != (asection *)NULL) {
2435       scn_base = abfd->sections->filepos;
2436     }
2437   else {
2438       scn_base = 0;
2439     }
2440   if (bfd_seek(abfd, scn_base, SEEK_SET) != 0)
2441    return false;
2442   reloc_base = obj_relocbase(abfd);
2443
2444   /* Make a pass through the symbol table to count line number entries and
2445      put them into the correct asections */
2446
2447 #ifndef NO_COFF_LINENOS
2448   coff_count_linenumbers(abfd);
2449 #endif
2450   data_base = scn_base;
2451
2452   /* Work out the size of the reloc and linno areas */
2453
2454   for (current = abfd->sections; current != NULL; current =
2455        current->next) 
2456   {
2457     /* We give section headers to +ve indexes */
2458     if (current->target_index > 0) 
2459     {
2460         
2461       reloc_size += current->reloc_count * RELSZ;
2462 #ifndef NO_COFF_LINENOS
2463       lnno_size += current->lineno_count * LINESZ;
2464 #endif
2465       data_base += SCNHSZ;
2466     }
2467       
2468   }
2469
2470   lineno_base = reloc_base + reloc_size;
2471   sym_base = lineno_base + lnno_size;
2472
2473   /* Indicate in each section->line_filepos its actual file address */
2474   for (current = abfd->sections; current != NULL; current =
2475        current->next) 
2476   {
2477     if (current->target_index > 0) 
2478     {
2479         
2480       if (current->lineno_count) {
2481           current->line_filepos = lineno_base;
2482           current->moving_line_filepos = lineno_base;
2483 #ifndef NO_COFF_LINENOS
2484           lineno_base += current->lineno_count * LINESZ;
2485 #endif
2486         }
2487       else {
2488           current->line_filepos = 0;
2489         }
2490       if (current->reloc_count) {
2491           current->rel_filepos = reloc_base;
2492           reloc_base += current->reloc_count * RELSZ;
2493         }
2494       else {
2495           current->rel_filepos = 0;
2496         }
2497     }
2498   }    
2499       
2500
2501
2502   /* Write section headers to the file.  */
2503   internal_f.f_nscns = 0;
2504   bfd_seek(abfd,
2505            (file_ptr) ((abfd->flags & EXEC_P) ?
2506                        (FILHSZ + AOUTSZ) : FILHSZ),
2507            SEEK_SET);
2508
2509 {
2510 #if 0
2511   unsigned int    pad = abfd->flags & D_PAGED ? data_base : 0;
2512 #endif
2513   unsigned int    pad = 0;
2514
2515   for (current = abfd->sections;
2516        current != NULL;
2517        current = current->next) {
2518       struct internal_scnhdr section;
2519       if (current->target_index > 0) 
2520       {
2521         internal_f.f_nscns ++;
2522         strncpy(&(section.s_name[0]), current->name, 8);
2523         section.s_vaddr = current->vma + pad;
2524         section.s_paddr = current->vma + pad;
2525         section.s_size = current->_raw_size - pad;
2526         /*
2527           If this section has no size or is unloadable then the scnptr
2528           will be 0 too
2529           */
2530         if (current->_raw_size - pad == 0 ||
2531             (current->flags & SEC_LOAD) == 0) {
2532             section.s_scnptr = 0;
2533           }
2534         else {
2535             section.s_scnptr = current->filepos;
2536           }
2537         section.s_relptr = current->rel_filepos;
2538         section.s_lnnoptr = current->line_filepos;
2539         section.s_nreloc = current->reloc_count;
2540         section.s_nlnno = current->lineno_count;
2541         if (current->reloc_count != 0)
2542          hasrelocs = true;
2543         if (current->lineno_count != 0)
2544          haslinno = true;
2545
2546         section.s_flags = sec_to_styp_flags(current->name,current->flags);
2547
2548         if (!strcmp(current->name, _TEXT)) {
2549             text_sec = current;
2550           } else if (!strcmp(current->name, _DATA)) {
2551               data_sec = current;
2552             } else if (!strcmp(current->name, _BSS)) {
2553                 bss_sec = current;
2554               }
2555
2556 #ifdef I960
2557         section.s_align = (current->alignment_power
2558                            ? 1 << current->alignment_power
2559                            : 0);
2560
2561 #endif
2562       {
2563         SCNHDR          buff;
2564
2565         coff_swap_scnhdr_out(abfd, &section, &buff);
2566         bfd_write((PTR) (&buff), 1, SCNHSZ, abfd);
2567
2568       }
2569
2570         pad = 0;
2571       }
2572     }   
2573 }
2574
2575
2576   /* OK, now set up the filehdr... */
2577
2578   /* Don't include the internal abs section in the section count */
2579
2580   /*
2581     We will NOT put a fucking timestamp in the header here. Every time you
2582     put it back, I will come in and take it out again. I'm sorry. This
2583     field does not belong here.  We fill it with a 0 so it compares the
2584     same but is not a reasonable time. -- gnu@cygnus.com
2585     */
2586   /*
2587     Well, I like it, and now we have *customers* who have requested it,
2588     so I'm conditionally compiling it in.
2589
2590     sac@cygnus.com
2591     */
2592 #ifndef NOCOFF_TIMESTAMP
2593   internal_f.f_timdat = time(0);
2594 #else
2595   internal_f.f_timdat = 0;
2596 #endif
2597
2598   if (bfd_get_symcount(abfd) != 0)
2599    internal_f.f_symptr = sym_base;
2600   else
2601    internal_f.f_symptr = 0;
2602
2603   internal_f.f_flags = 0;
2604
2605   if (abfd->flags & EXEC_P)
2606    internal_f.f_opthdr = AOUTSZ;
2607   else
2608    internal_f.f_opthdr = 0;
2609
2610   if (!hasrelocs)
2611    internal_f.f_flags |= F_RELFLG;
2612   if (!haslinno)
2613    internal_f.f_flags |= F_LNNO;
2614   if (0 == bfd_get_symcount(abfd))
2615    internal_f.f_flags |= F_LSYMS;
2616   if (abfd->flags & EXEC_P)
2617    internal_f.f_flags |= F_EXEC;
2618
2619   if (!abfd->xvec->byteorder_big_p)
2620    internal_f.f_flags |= F_AR32WR;
2621   else 
2622   internal_f.f_flags |= F_AR32W;
2623
2624   /*
2625     FIXME, should do something about the other byte orders and
2626     architectures.
2627     */
2628
2629   /* Set up architecture-dependent stuff */
2630
2631 { unsigned int   magic = 0;
2632   unsigned short    flags = 0;
2633   coff_set_flags(abfd, &magic, &flags);
2634   internal_f.f_magic = magic;
2635   internal_f.f_flags |= flags;
2636   /* ...and the "opt"hdr... */
2637
2638 #ifdef A29K
2639 # ifdef ULTRA3                  /* NYU's machine */
2640   /* FIXME: This is a bogus check.  I really want to see if there
2641    * is a .shbss or a .shdata section, if so then set the magic
2642    * number to indicate a shared data executable.
2643    */
2644   if (internal_f.f_nscns >= 7)
2645    internal_a.magic = SHMAGIC;  /* Shared magic */
2646   else
2647 # endif                         /* ULTRA3 */
2648    internal_a.magic = NMAGIC;   /* Assume separate i/d */
2649 #define __A_MAGIC_SET__
2650 #endif                          /* A29K */
2651 #ifdef I960
2652   internal_a.magic = (magic == I960ROMAGIC ? NMAGIC : OMAGIC);
2653 #define __A_MAGIC_SET__
2654 #endif                          /* I960 */
2655 #if M88
2656 #define __A_MAGIC_SET__
2657   internal_a.magic = PAGEMAGICBCS;
2658 #endif                          /* M88 */
2659
2660 #if M68 || I386 || MIPS || WE32K
2661 #define __A_MAGIC_SET__
2662   /* Never was anything here for the 68k */
2663 #endif                          /* M68 || I386 || MIPS || WE32K */
2664
2665 #if RS6000COFF_C
2666 #define __A_MAGIC_SET__
2667   internal_a.magic = (abfd->flags & D_PAGED)? RS6K_AOUTHDR_ZMAGIC:
2668   (abfd->flags & WP_TEXT)? RS6K_AOUTHDR_NMAGIC:
2669   RS6K_AOUTHDR_OMAGIC;
2670 #endif
2671
2672 #ifndef __A_MAGIC_SET__
2673 # include "Your aouthdr magic number is not being set!"
2674 #else
2675 # undef __A_MAGIC_SET__
2676 #endif
2677 }
2678   /* Now should write relocs, strings, syms */
2679   obj_sym_filepos(abfd) = sym_base;
2680
2681 #ifndef NO_COFF_SYMBOLS
2682   if (bfd_get_symcount(abfd) != 0) {
2683       coff_add_missing_symbols (abfd);
2684       coff_renumber_symbols(abfd);
2685       coff_mangle_symbols(abfd);
2686       coff_write_symbols(abfd);
2687       coff_write_linenumbers(abfd);
2688       coff_write_relocs(abfd);
2689     }
2690 #endif                          /* NO_COFF_SYMBOLS */
2691   if (text_sec) {
2692       internal_a.tsize = bfd_get_section_size_before_reloc(text_sec);
2693       internal_a.text_start = internal_a.tsize ? text_sec->vma : 0;
2694     }
2695   if (data_sec) {
2696       internal_a.dsize = bfd_get_section_size_before_reloc(data_sec);
2697       internal_a.data_start = internal_a.dsize ? data_sec->vma      : 0;
2698     }
2699   if (bss_sec) {
2700       internal_a.bsize = bfd_get_section_size_before_reloc(bss_sec);
2701     }
2702
2703   internal_a.entry = bfd_get_start_address(abfd);
2704   internal_f.f_nsyms =  bfd_get_symcount(abfd);
2705
2706   /* now write them */
2707   if (bfd_seek(abfd, 0L, SEEK_SET) != 0)
2708    return false;
2709 {
2710   FILHDR buff;
2711   coff_swap_filehdr_out(abfd, (PTR)&internal_f, (PTR)&buff);
2712   bfd_write((PTR) &buff, 1, FILHSZ, abfd);
2713 }
2714   if (abfd->flags & EXEC_P) {
2715       AOUTHDR buff;
2716       coff_swap_aouthdr_out(abfd, (PTR)&internal_a, (PTR)&buff);
2717       bfd_write((PTR) &buff, 1, AOUTSZ, abfd);
2718     }
2719   return true;
2720 }
2721
2722 #ifndef NO_COFF_SYMBOLS
2723
2724 /*
2725 this function transforms the offsets into the symbol table into
2726 pointers to syments.
2727 */
2728
2729
2730 static void
2731 DEFUN(coff_pointerize_aux,(ignore_abfd, table_base, type, class, auxent),
2732 bfd *ignore_abfd AND
2733 combined_entry_type *table_base AND
2734 int type AND
2735 int class AND
2736 combined_entry_type *auxent)
2737 {
2738   /* Don't bother if this is a file or a section */
2739   if (class == C_STAT && type == T_NULL) return;
2740   if (class == C_FILE) return;
2741
2742   /* Otherwise patch up */
2743   if (ISFCN(type) || ISTAG(class) || class == C_BLOCK) {
2744       auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p = table_base +
2745        auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
2746       auxent->fix_end = 1;
2747     }
2748   if (auxent->u.auxent.x_sym.x_tagndx.l != 0) {
2749       auxent->u.auxent.x_sym.x_tagndx.p =
2750        table_base +  auxent->u.auxent.x_sym.x_tagndx.l;
2751       auxent->fix_tag = 1;
2752     }
2753 }
2754
2755 #endif /* NO_COFF_SYMBOLS */
2756
2757 static          boolean
2758 DEFUN(coff_set_section_contents,(abfd, section, location, offset, count),
2759       bfd            *abfd AND
2760       sec_ptr         section AND
2761       PTR             location AND
2762       file_ptr        offset AND
2763       bfd_size_type   count)
2764 {
2765     if (abfd->output_has_begun == false)        /* set by bfd.c handler */
2766         coff_compute_section_file_positions(abfd);
2767
2768     bfd_seek(abfd, (file_ptr) (section->filepos + offset), SEEK_SET);
2769
2770     if (count != 0) {
2771         return (bfd_write(location, 1, count, abfd) == count) ? true : false;
2772     }
2773     return true;
2774 }
2775 #if 0
2776 static          boolean
2777 coff_close_and_cleanup(abfd)
2778     bfd            *abfd;
2779 {
2780   if (!bfd_read_p(abfd))
2781     switch (abfd->format) {
2782     case bfd_archive:
2783       if (!_bfd_write_archive_contents(abfd))
2784         return false;
2785       break;
2786     case bfd_object:
2787       if (!coff_write_object_contents(abfd))
2788         return false;
2789       break;
2790     default:
2791       bfd_error = invalid_operation;
2792       return false;
2793     }
2794
2795   /* We depend on bfd_close to free all the memory on the obstack.  */
2796   /* FIXME if bfd_release is not using obstacks! */
2797   return true;
2798 }
2799
2800 #endif
2801 static PTR
2802 buy_and_read(abfd, where, seek_direction, size)
2803     bfd            *abfd;
2804     file_ptr        where;
2805     int             seek_direction;
2806     size_t          size;
2807 {
2808     PTR             area = (PTR) bfd_alloc(abfd, size);
2809     if (!area) {
2810         bfd_error = no_memory;
2811         return (NULL);
2812     }
2813     bfd_seek(abfd, where, seek_direction);
2814     if (bfd_read(area, 1, size, abfd) != size) {
2815         bfd_error = system_call_error;
2816         return (NULL);
2817     }                           /* on error */
2818     return (area);
2819 }                               /* buy_and_read() */
2820
2821
2822 #ifndef NO_COFF_SYMBOLS
2823
2824 static char *
2825 DEFUN(build_string_table,(abfd),
2826 bfd *abfd)
2827 {
2828   char string_table_size_buffer[4];
2829   unsigned int string_table_size;
2830   char *string_table;
2831
2832   /* At this point we should be "seek"'d to the end of the
2833      symbols === the symbol table size.  */
2834   if (bfd_read((char *) string_table_size_buffer,
2835                sizeof(string_table_size_buffer),
2836                1, abfd) != sizeof(string_table_size)) {
2837     bfd_error = system_call_error;
2838     return (NULL);
2839   }                             /* on error */
2840
2841   string_table_size = bfd_h_get_32(abfd, (bfd_byte *) string_table_size_buffer);
2842
2843   if ((string_table = (PTR) bfd_alloc(abfd, string_table_size -= 4)) == NULL) {
2844     bfd_error = no_memory;
2845     return (NULL);
2846   }                             /* on mallocation error */
2847   if (bfd_read(string_table, string_table_size, 1, abfd) != string_table_size) {
2848     bfd_error = system_call_error;
2849     return (NULL);
2850   }
2851   return string_table;
2852 }
2853
2854 /* Allocate space for the ".debug" section, and read it.
2855    We did not read the debug section until now, because
2856    we didn't want to go to the trouble until someone needed it. */
2857
2858 static char *
2859 DEFUN(build_debug_section,(abfd),
2860         bfd *abfd)
2861 {
2862   char *debug_section;
2863   long position;
2864
2865   asection *sect = bfd_get_section_by_name (abfd, ".debug");
2866
2867   if (!sect) {
2868      bfd_error = no_debug_section;
2869      return NULL;
2870   }
2871
2872   debug_section = (PTR) bfd_alloc (abfd,
2873                                    bfd_get_section_size_before_reloc (sect));
2874   if (debug_section == NULL) {
2875     bfd_error = no_memory;
2876     return NULL;
2877   }
2878
2879   /* Seek to the beginning of the `.debug' section and read it. 
2880      Save the current position first; it is needed by our caller.
2881      Then read debug section and reset the file pointer.  */
2882
2883   position = bfd_tell (abfd);
2884   bfd_seek (abfd, sect->filepos, SEEK_SET);
2885   if (bfd_read (debug_section, 
2886                 bfd_get_section_size_before_reloc (sect), 1, abfd)
2887       != bfd_get_section_size_before_reloc(sect)) {
2888     bfd_error = system_call_error;
2889     return NULL;
2890   }
2891   bfd_seek (abfd, position, SEEK_SET);
2892   return debug_section;
2893 }
2894
2895
2896 /* Return a pointer to a malloc'd copy of 'name'.  'name' may not be
2897  \0-terminated, but will not exceed 'maxlen' characters.  The copy *will*
2898  be \0-terminated.  */
2899 static char *
2900 DEFUN(copy_name,(abfd, name, maxlen),
2901       bfd *abfd AND
2902       char *name AND
2903       int maxlen)
2904 {
2905   int  len;
2906   char *newname;
2907
2908   for (len = 0; len < maxlen; ++len) {
2909     if (name[len] == '\0') {
2910       break;
2911     }
2912   }
2913
2914   if ((newname = (PTR) bfd_alloc(abfd, len+1)) == NULL) {
2915     bfd_error = no_memory;
2916     return (NULL);
2917   }
2918   strncpy(newname, name, len);
2919   newname[len] = '\0';
2920   return newname;
2921 }
2922
2923
2924 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
2925    knit the symbol names into a normalized form.  By normalized here I
2926    mean that all symbols have an n_offset pointer that points to a null-
2927    terminated string.  */
2928
2929 #ifndef SYMNAME_IN_DEBUG
2930 #define SYMNAME_IN_DEBUG(x)  0
2931 #endif
2932
2933 static combined_entry_type *
2934 DEFUN(get_normalized_symtab,(abfd),
2935 bfd            *abfd)
2936 {
2937   combined_entry_type          *internal;
2938   combined_entry_type          *internal_ptr;
2939   combined_entry_type          *symbol_ptr;
2940   combined_entry_type         *internal_end;
2941   SYMENT *raw;
2942   SYMENT *raw_src;
2943   SYMENT *raw_end;
2944   char           *string_table = NULL;
2945   char           *debug_section = NULL;
2946   unsigned long   size;
2947
2948   unsigned int raw_size;
2949   if (obj_raw_syments(abfd) != (combined_entry_type *)NULL) {
2950       return obj_raw_syments(abfd);
2951     }
2952   if ((size = bfd_get_symcount(abfd) * sizeof(combined_entry_type)) == 0) {
2953       bfd_error = no_symbols;
2954       return (NULL);
2955     }
2956
2957   internal = (combined_entry_type *)bfd_alloc(abfd, size);
2958   internal_end = internal + bfd_get_symcount(abfd);
2959
2960   raw_size =      bfd_get_symcount(abfd) * SYMESZ;
2961   raw = (SYMENT *)bfd_alloc(abfd,raw_size);
2962
2963   if (bfd_seek(abfd, obj_sym_filepos(abfd), SEEK_SET) == -1
2964       || bfd_read((PTR)raw, raw_size, 1, abfd) != raw_size) {
2965       bfd_error = system_call_error;
2966       return (NULL);
2967     }
2968   /* mark the end of the symbols */
2969   raw_end = raw + bfd_get_symcount(abfd);
2970   /*
2971     FIXME SOMEDAY.  A string table size of zero is very weird, but
2972     probably possible.  If one shows up, it will probably kill us.
2973     */
2974
2975   /* Swap all the raw entries */
2976   for (raw_src = raw, internal_ptr = internal;
2977        raw_src < raw_end;
2978        raw_src++, internal_ptr++) {
2979
2980       unsigned int i;
2981       coff_swap_sym_in(abfd, (PTR)raw_src, (PTR)&internal_ptr->u.syment);
2982       internal_ptr->fix_tag = 0;
2983       internal_ptr->fix_end = 0;
2984       symbol_ptr = internal_ptr;
2985     
2986       for (i = 0;
2987            i < symbol_ptr->u.syment.n_numaux;
2988            i++) 
2989       {
2990         internal_ptr++;
2991         raw_src++;
2992       
2993         internal_ptr->fix_tag = 0;
2994         internal_ptr->fix_end = 0;
2995         coff_swap_aux_in(abfd, (char *)(raw_src),
2996                          symbol_ptr->u.syment.n_type,
2997                          symbol_ptr->u.syment.n_sclass,
2998                          &(internal_ptr->u.auxent));
2999         /* Remember that bal entries arn't pointerized */
3000         if (i != 1 || symbol_ptr->u.syment.n_sclass != C_LEAFPROC)
3001         {
3002           
3003         coff_pointerize_aux(abfd,
3004                             internal,
3005                             symbol_ptr->u.syment.n_type,
3006                             symbol_ptr->u.syment.n_sclass,
3007                             internal_ptr);
3008       }
3009         
3010       }
3011     }
3012
3013   /* Free all the raw stuff */
3014   bfd_release(abfd, raw);
3015
3016   for (internal_ptr = internal; internal_ptr < internal_end;
3017        internal_ptr ++)
3018   {
3019     if (internal_ptr->u.syment.n_sclass == C_FILE) {
3020         /* make a file symbol point to the name in the auxent, since
3021            the text ".file" is redundant */
3022         if ((internal_ptr+1)->u.auxent.x_file.x_n.x_zeroes == 0) {
3023             /* the filename is a long one, point into the string table */
3024             if (string_table == NULL) {
3025                 string_table = build_string_table(abfd);
3026               }
3027
3028             internal_ptr->u.syment._n._n_n._n_offset =
3029              (int) (string_table - 4 +
3030                     (internal_ptr+1)->u.auxent.x_file.x_n.x_offset);
3031           }
3032         else {
3033             /* ordinary short filename, put into memory anyway */
3034             internal_ptr->u.syment._n._n_n._n_offset = (int)
3035              copy_name(abfd, (internal_ptr+1)->u.auxent.x_file.x_fname,
3036                        FILNMLEN);
3037           }
3038       }
3039     else {
3040         if (internal_ptr->u.syment._n._n_n._n_zeroes != 0) {
3041             /* This is a "short" name.  Make it long.  */
3042             unsigned long   i = 0;
3043             char           *newstring = NULL;
3044
3045             /* find the length of this string without walking into memory
3046                that isn't ours.  */
3047             for (i = 0; i < 8; ++i) {
3048                 if (internal_ptr->u.syment._n._n_name[i] == '\0') {
3049                     break;
3050                   }             /* if end of string */
3051               }                 /* possible lengths of this string. */
3052
3053             if ((newstring = (PTR) bfd_alloc(abfd, ++i)) == NULL) {
3054                 bfd_error = no_memory;
3055                 return (NULL);
3056               }                 /* on error */
3057             bzero(newstring, i);
3058             strncpy(newstring, internal_ptr->u.syment._n._n_name, i-1);
3059             internal_ptr->u.syment._n._n_n._n_offset =  (int) newstring;
3060             internal_ptr->u.syment._n._n_n._n_zeroes = 0;
3061           }
3062         else if (!SYMNAME_IN_DEBUG(&internal_ptr->u.syment)) {
3063             /* Long name already.  Point symbol at the string in the table.  */
3064             if (string_table == NULL) {
3065                 string_table = build_string_table(abfd);
3066               }
3067             internal_ptr->u.syment._n._n_n._n_offset = (int)
3068              (string_table - 4 + internal_ptr->u.syment._n._n_n._n_offset);
3069           }
3070         else {
3071             /* Long name in debug section.  Very similar.  */
3072             if (debug_section == NULL) {
3073                 debug_section = build_debug_section(abfd);
3074               }
3075             internal_ptr->u.syment._n._n_n._n_offset = (int)
3076              (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
3077           }
3078       }
3079     internal_ptr += internal_ptr->u.syment.n_numaux;
3080   }
3081
3082   obj_raw_syments(abfd) = internal;
3083
3084   return (internal);
3085 }                               /* get_normalized_symtab() */
3086
3087 #endif /* NO_COFF_SYMBOLS */
3088
3089 static
3090 struct sec *
3091 DEFUN(section_from_bfd_index,(abfd, index),
3092       bfd            *abfd AND
3093       int             index)
3094 {
3095   struct sec *answer = abfd->sections;
3096
3097   if (index == N_ABS) 
3098   {
3099     return &bfd_abs_section;
3100   }
3101   if (index == N_UNDEF)
3102   {
3103     return &bfd_und_section;
3104   }
3105   if(index == N_DEBUG)
3106   {
3107     return &bfd_debug_section;
3108     
3109   }
3110   
3111   while (answer) {
3112       if (answer->target_index == index)
3113        return answer;
3114       answer = answer->next;
3115     }
3116   BFD_ASSERT(0);
3117   return &bfd_und_section;      /* For gcc -W and lint.  Never executed. */
3118 }
3119
3120 #ifndef NO_COFF_LINENOS
3121
3122 /*
3123 SUBSUBSECTION 
3124         Reading Linenumbers
3125
3126         Creating the linenumber table is done by reading in the entire
3127         coff linenumber table, and creating another table for internal use.
3128
3129         A coff line number table is structured so that each function
3130         is marked as having a line number of 0. Each line within the
3131         function is an offset from the first line in the function. The
3132         base of the line number information for the table is stored in
3133         the symbol associated with the function. 
3134
3135         The information is copied from the external to the internal
3136         table, and each symbol which marks a function is marked by
3137         pointing its...
3138
3139         How does this work ?
3140
3141 */
3142
3143 static boolean
3144 coff_slurp_line_table(abfd, asect)
3145 bfd            *abfd;
3146 asection       *asect;
3147   {
3148     LINENO  *native_lineno;
3149     alent          *lineno_cache;
3150
3151     BFD_ASSERT(asect->lineno == (alent *) NULL);
3152
3153     native_lineno = (LINENO *) buy_and_read(abfd,
3154                                             asect->line_filepos,
3155                                             SEEK_SET,
3156                                             (size_t) (LINESZ *
3157                                                       asect->lineno_count));
3158     lineno_cache =
3159       (alent *) bfd_alloc(abfd, (size_t) ((asect->lineno_count + 1) * sizeof(alent)));
3160     if (lineno_cache == NULL) {
3161       bfd_error = no_memory;
3162       return false;
3163     } else {
3164       unsigned int    counter = 0;
3165       alent          *cache_ptr = lineno_cache;
3166       LINENO  *src = native_lineno;
3167
3168       while (counter < asect->lineno_count) {
3169         struct internal_lineno dst;
3170         coff_swap_lineno_in(abfd, src, &dst);
3171         cache_ptr->line_number = dst.l_lnno;
3172
3173         if (cache_ptr->line_number == 0) {
3174           coff_symbol_type *sym =
3175             (coff_symbol_type *) (dst.l_addr.l_symndx
3176                                   + obj_raw_syments(abfd))->u.syment._n._n_n._n_zeroes;
3177           cache_ptr->u.sym = (asymbol *) sym;
3178           sym->lineno = cache_ptr;
3179         }
3180         else {
3181           cache_ptr->u.offset = dst.l_addr.l_paddr
3182             - bfd_section_vma(abfd, asect);
3183         }                               /* If no linenumber expect a symbol index */
3184
3185         cache_ptr++;
3186         src++;
3187         counter++;
3188       }
3189       cache_ptr->line_number = 0;
3190
3191     }
3192     asect->lineno = lineno_cache;
3193     /* FIXME, free native_lineno here, or use alloca or something. */
3194     return true;
3195   }                             /* coff_slurp_line_table() */
3196
3197 #endif /* NO_COFF_LINENOS */
3198
3199 #ifndef NO_COFF_LINENOS
3200
3201 static          boolean
3202 DEFUN(coff_slurp_symbol_table,(abfd),
3203       bfd            *abfd)
3204 {
3205   combined_entry_type         *native_symbols;
3206   coff_symbol_type *cached_area;
3207   unsigned int   *table_ptr;
3208
3209   unsigned int    number_of_symbols = 0;
3210   if (obj_symbols(abfd))
3211     return true;
3212   bfd_seek(abfd, obj_sym_filepos(abfd), SEEK_SET);
3213
3214   /* Read in the symbol table */
3215   if ((native_symbols = get_normalized_symtab(abfd)) == NULL) {
3216     return (false);
3217   }                             /* on error */
3218
3219   /* Allocate enough room for all the symbols in cached form */
3220   cached_area =
3221     (coff_symbol_type *)
3222       bfd_alloc(abfd, (size_t) (bfd_get_symcount(abfd) * sizeof(coff_symbol_type)));
3223
3224   if (cached_area == NULL) {
3225     bfd_error = no_memory;
3226     return false;
3227   }                             /* on error */
3228   table_ptr =
3229     (unsigned int *)
3230       bfd_alloc(abfd, (size_t) (bfd_get_symcount(abfd) * sizeof(unsigned int)));
3231
3232   if (table_ptr == NULL) {
3233     bfd_error = no_memory;
3234     return false;
3235   } 
3236   else 
3237   {
3238     coff_symbol_type *dst = cached_area;
3239     unsigned int    last_native_index = bfd_get_symcount(abfd);
3240     unsigned int    this_index = 0;
3241     while (this_index < last_native_index) {
3242       combined_entry_type         *src = native_symbols + this_index;
3243       table_ptr[this_index] = number_of_symbols;
3244       dst->symbol.the_bfd = abfd;
3245
3246       dst->symbol.name = (char *)(src->u.syment._n._n_n._n_offset);
3247       /*
3248         We use the native name field to point to the cached field
3249         */
3250       src->u.syment._n._n_n._n_zeroes = (int) dst;
3251       dst->symbol.section = section_from_bfd_index(abfd,
3252                                                    src->u.syment.n_scnum);
3253       dst->symbol.flags = 0;
3254       dst->done_lineno = false;
3255       
3256       switch (src->u.syment.n_sclass) {
3257 #ifdef I960
3258       case C_LEAFEXT:
3259 #if 0
3260         dst->symbol.value = src->u.syment.n_value - dst->symbol.section->vma;
3261         dst->symbol.flags = BSF_EXPORT | BSF_GLOBAL;
3262         dst->symbol.flags |= BSF_NOT_AT_END;
3263 #endif
3264         /* Fall through to next case */
3265
3266 #endif
3267
3268       case C_EXT:
3269 #ifdef RS6000COFF_C
3270       case C_HIDEXT:
3271 #endif
3272         if ((src->u.syment.n_scnum) == 0) {
3273           if ((src->u.syment.n_value) == 0) {
3274             dst->symbol.section = &bfd_und_section;
3275             dst->symbol.value= 0;
3276           }
3277           else {
3278             dst->symbol.section = &bfd_com_section;
3279             dst->symbol.value = (src->u.syment.n_value);
3280           }
3281         }
3282         else {
3283           /*
3284             Base the value as an index from the base of the
3285             section
3286             */
3287
3288             dst->symbol.flags = BSF_EXPORT | BSF_GLOBAL;
3289             dst->symbol.value = src->u.syment.n_value - dst->symbol.section->vma;
3290
3291           if (ISFCN((src->u.syment.n_type))) {
3292             /*
3293               A function ext does not go at the end of a file
3294               */
3295             dst->symbol.flags |= BSF_NOT_AT_END;
3296           }
3297         }
3298
3299
3300         break;
3301
3302       case C_STAT:              /* static                        */
3303 #ifdef I960
3304       case C_LEAFSTAT:          /* static leaf procedure        */
3305 #endif
3306       case C_LABEL:             /* label                         */
3307         if (src->u.syment.n_scnum == -2)
3308           dst->symbol.flags = BSF_DEBUGGING;
3309         else
3310           dst->symbol.flags = BSF_LOCAL;
3311         /*
3312           Base the value as an index from the base of the section, if
3313           there is one
3314           */
3315         if (dst->symbol.section)
3316           dst->symbol.value = (src->u.syment.n_value) -
3317             dst->symbol.section->vma;
3318         else
3319           dst->symbol.value = (src->u.syment.n_value) ;
3320         break;
3321
3322       case C_MOS:               /* member of structure   */
3323       case C_EOS:               /* end of structure              */
3324 #ifdef NOTDEF   /* C_AUTOARG has the same value */
3325 #ifdef C_GLBLREG
3326       case C_GLBLREG:           /* A29k-specific storage class */
3327 #endif
3328 #endif
3329       case C_REGPARM:           /* register parameter            */
3330       case C_REG:               /* register variable             */
3331 #ifdef C_AUTOARG
3332       case C_AUTOARG:           /* 960-specific storage class */
3333 #endif
3334       case C_TPDEF:             /* type definition               */
3335       case C_ARG:
3336       case C_AUTO:              /* automatic variable */
3337       case C_FIELD:             /* bit field */
3338       case C_ENTAG:             /* enumeration tag               */
3339       case C_MOE:               /* member of enumeration         */
3340       case C_MOU:               /* member of union               */
3341       case C_UNTAG:             /* union tag                     */
3342         dst->symbol.flags = BSF_DEBUGGING;
3343         dst->symbol.value = (src->u.syment.n_value);
3344         break;
3345
3346       case C_FILE:              /* file name                     */
3347       case C_STRTAG:            /* structure tag                 */
3348 #ifdef RS6000COFF_C
3349       case C_BINCL:             /* beginning of include file     */
3350       case C_EINCL:             /* ending of include file        */
3351       case C_GSYM:
3352       case C_LSYM:
3353       case C_PSYM:
3354       case C_RSYM:
3355       case C_RPSYM:
3356       case C_STSYM:
3357       case C_DECL:
3358       case C_ENTRY:
3359       case C_FUN:
3360       case C_BSTAT:
3361       case C_ESTAT:
3362 #endif
3363         dst->symbol.flags = BSF_DEBUGGING;
3364         dst->symbol.value = (src->u.syment.n_value);
3365         break;
3366
3367       case C_BLOCK:             /* ".bb" or ".eb"                */
3368       case C_FCN:               /* ".bf" or ".ef"                */
3369       case C_EFCN:              /* physical end of function      */
3370         dst->symbol.flags = BSF_LOCAL;
3371         /*
3372           Base the value as an index from the base of the section
3373           */
3374         dst->symbol.value = (src->u.syment.n_value) - dst->symbol.section->vma;
3375         break;
3376
3377       case C_NULL:
3378       case C_EXTDEF:            /* external definition           */
3379       case C_ULABEL:            /* undefined label               */
3380       case C_USTATIC:           /* undefined static              */
3381       case C_LINE:              /* line # reformatted as symbol table entry */
3382       case C_ALIAS:             /* duplicate tag                 */
3383       case C_HIDDEN:            /* ext symbol in dmert public lib */
3384       default:
3385
3386         fprintf(stderr,"Unrecognized storage class %d\n",
3387                                 src->u.syment.n_sclass);
3388 /*      abort();*/
3389         dst->symbol.flags = BSF_DEBUGGING;
3390         dst->symbol.value = (src->u.syment.n_value);
3391         break;
3392       }
3393
3394 /*      BFD_ASSERT(dst->symbol.flags != 0);*/
3395
3396       dst->native = src;
3397
3398       dst->symbol.udata = 0;
3399       dst->lineno = (alent *) NULL;
3400       this_index += (src->u.syment.n_numaux) + 1;
3401       dst++;
3402       number_of_symbols++;
3403     }                           /* walk the native symtab */
3404   }                             /* bfdize the native symtab */
3405
3406   obj_symbols(abfd) = cached_area;
3407   obj_raw_syments(abfd) = native_symbols;
3408
3409   obj_conv_table_size (abfd) = bfd_get_symcount (abfd);
3410   bfd_get_symcount(abfd) = number_of_symbols;
3411   obj_convert(abfd) = table_ptr;
3412   /* Slurp the line tables for each section too */
3413     {
3414       asection       *p;
3415       p = abfd->sections;
3416       while (p) {
3417         coff_slurp_line_table(abfd, p);
3418         p = p->next;
3419       }
3420     }
3421   return true;
3422 }                               /* coff_slurp_symbol_table() */
3423
3424 static unsigned int
3425 coff_get_symtab_upper_bound(abfd)
3426 bfd            *abfd;
3427   {
3428     if (!coff_slurp_symbol_table(abfd))
3429       return 0;
3430
3431     return (bfd_get_symcount(abfd) + 1) * (sizeof(coff_symbol_type *));
3432   }
3433
3434
3435 static unsigned int
3436 DEFUN(coff_get_symtab, (abfd, alocation),
3437       bfd            *abfd AND
3438       asymbol       **alocation)
3439 {
3440     unsigned int    counter = 0;
3441     coff_symbol_type *symbase;
3442     coff_symbol_type **location = (coff_symbol_type **) (alocation);
3443     if (!coff_slurp_symbol_table(abfd))
3444      return 0;
3445
3446     symbase = obj_symbols(abfd);
3447     while (counter <  bfd_get_symcount(abfd))
3448     {
3449         /* This nasty code looks at the symbol to decide whether or
3450            not it is descibes a constructor/destructor entry point. It
3451            is structured this way to (hopefully) speed non matches */
3452 #if 0   
3453         if (0 && symbase->symbol.name[9] == '$') 
3454         {
3455             bfd_constructor_entry(abfd, 
3456                                  (asymbol **)location,
3457                                   symbase->symbol.name[10] == 'I' ?
3458                                   "CTOR" : "DTOR");
3459         }
3460 #endif
3461         *(location++) = symbase++;
3462         counter++;
3463     }
3464     *location++ = 0;
3465     return bfd_get_symcount(abfd);
3466 }
3467
3468 #endif /* NO_COFF_SYMBOLS */
3469
3470 static unsigned int
3471 coff_get_reloc_upper_bound(abfd, asect)
3472 bfd            *abfd;
3473 sec_ptr         asect;
3474   {
3475     if (bfd_get_format(abfd) != bfd_object) {
3476       bfd_error = invalid_operation;
3477       return 0;
3478     }
3479     return (asect->reloc_count + 1) * sizeof(arelent *);
3480   }
3481
3482 /*
3483 SUBSUBSECTION 
3484         Reading Relocations
3485
3486         Coff relocations are easily transformed into the internal BFD form
3487         (@code{arelent}).
3488
3489         Reading a coff relocation table is done in the following stages:
3490
3491         o The entire coff relocation table is read into memory.
3492
3493         o Each relocation is processed in turn, first it is swapped from the
3494         external to the internal form.
3495
3496         o The symbol referenced in the relocation's symbol index is
3497         turned intoa pointer into the canonical symbol table. Note
3498         that this table is the same as the one returned by a call to
3499         @code{bfd_canonicalize_symtab}. The back end will call the
3500         routine and save the result if a canonicalization hasn't been done.
3501
3502         o The reloc index is turned into a pointer to a howto
3503         structure, in a back end specific way. For instance, the 386
3504         and 960 use the @code{r_type} to directly produce an index
3505         into a howto table vector; the 88k subtracts a number from the
3506         @code{r_type} field and creates an addend field.
3507
3508
3509 */
3510
3511 #ifndef CALC_ADDEND
3512 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr)        \
3513             if (ptr && ptr->the_bfd == abfd             \
3514                 && ((ptr->flags & BSF_OLD_COMMON)== 0)) \
3515             {                                           \
3516                 cache_ptr->addend = -(ptr->section->vma + ptr->value);  \
3517             }                                           \
3518             else {                                      \
3519                 cache_ptr->addend = 0;                  \
3520             }                   
3521 #endif
3522
3523 static          boolean
3524 DEFUN(coff_slurp_reloc_table,(abfd, asect, symbols),
3525       bfd            *abfd AND
3526       sec_ptr         asect AND
3527       asymbol       **symbols)
3528 {
3529   RELOC   *native_relocs;
3530   arelent        *reloc_cache;
3531   arelent        *cache_ptr;
3532
3533   unsigned int idx;
3534   
3535   if (asect->relocation)
3536    return true;
3537   if (asect->reloc_count == 0)
3538    return true;
3539   if (asect->flags & SEC_CONSTRUCTOR)
3540    return true;
3541 #ifndef NO_COFF_SYMBOLS
3542   if (!coff_slurp_symbol_table(abfd))
3543    return false;
3544 #endif
3545   native_relocs =
3546    (RELOC *) buy_and_read(abfd,
3547                           asect->rel_filepos,
3548                           SEEK_SET,
3549                           (size_t) (RELSZ *
3550                                     asect->reloc_count));
3551   reloc_cache = (arelent *)
3552    bfd_alloc(abfd, (size_t) (asect->reloc_count * sizeof(arelent)));
3553
3554   if (reloc_cache == NULL) {
3555       bfd_error = no_memory;
3556       return false;
3557     } 
3558
3559   
3560   for (idx = 0; idx < asect->reloc_count; idx ++) 
3561   {
3562 #ifdef RELOC_PROCESSING
3563       struct internal_reloc dst;
3564       struct external_reloc  *src;
3565
3566       cache_ptr = reloc_cache + idx;
3567       src = native_relocs + idx;
3568       bfd_swap_reloc_in(abfd, src, &dst);
3569
3570       RELOC_PROCESSING(cache_ptr, &dst, symbols, abfd, asect);
3571 #else
3572       struct internal_reloc dst;
3573       asymbol        *ptr;
3574       struct external_reloc  *src;
3575
3576       cache_ptr = reloc_cache + idx;
3577       src = native_relocs + idx;
3578
3579       bfd_swap_reloc_in(abfd, src, &dst);
3580
3581
3582       cache_ptr->address = dst.r_vaddr;
3583
3584       if (dst.r_symndx != -1) 
3585         {
3586           /* @@ Should never be greater than count of symbols!  */
3587           if (dst.r_symndx >= obj_conv_table_size (abfd))
3588             abort ();
3589           cache_ptr->sym_ptr_ptr = symbols + obj_convert(abfd)[dst.r_symndx];
3590           ptr = *(cache_ptr->sym_ptr_ptr);
3591         }
3592       else 
3593         {
3594           cache_ptr->sym_ptr_ptr= bfd_abs_section.symbol_ptr_ptr;
3595           ptr = 0;
3596         }
3597
3598       /*
3599         The symbols definitions that we have read in have been
3600         relocated as if their sections started at 0. But the offsets
3601         refering to the symbols in the raw data have not been
3602         modified, so we have to have a negative addend to compensate.
3603         
3604         Note that symbols which used to be common must be left alone */
3605
3606       /* Calculate any reloc addend by looking at the symbol */
3607       CALC_ADDEND(abfd, ptr, dst, cache_ptr);
3608
3609       cache_ptr->address -= asect->vma;
3610 /* !!     cache_ptr->section = (asection *) NULL;*/
3611
3612       /* Fill in the cache_ptr->howto field from dst.r_type */
3613       RTYPE2HOWTO(cache_ptr, &dst);
3614 #endif
3615
3616   }
3617
3618   asect->relocation = reloc_cache;
3619   return true;
3620 }
3621
3622
3623 /* This is stupid.  This function should be a boolean predicate */
3624 static unsigned int
3625 DEFUN(coff_canonicalize_reloc, (abfd, section, relptr, symbols),
3626 bfd            *abfd AND
3627 sec_ptr         section AND
3628 arelent       **relptr AND
3629 asymbol       **symbols)
3630 {
3631   arelent        *tblptr = section->relocation;
3632   unsigned int    count = 0;
3633
3634
3635   if (section->flags & SEC_CONSTRUCTOR) 
3636   {
3637     /* this section has relocs made up by us, they are not in the
3638        file, so take them out of their chain and place them into
3639        the data area provided */
3640     arelent_chain *chain = section->constructor_chain;
3641     for (count = 0; count < section->reloc_count; count ++) 
3642     {
3643       *relptr ++ = &chain->relent;
3644       chain = chain->next;
3645     }
3646
3647   }
3648   else 
3649   { 
3650     coff_slurp_reloc_table(abfd, section, symbols);
3651
3652
3653     tblptr = section->relocation;
3654     if (!tblptr)
3655      return 0;
3656
3657     for (; count++ < section->reloc_count;)
3658      *relptr++ = tblptr++;
3659
3660
3661   }
3662   *relptr = 0;
3663   return section->reloc_count;
3664 }
3665
3666 #ifndef NO_COFF_SYMBOLS
3667
3668 /*
3669 provided a BFD, a section and an offset into the section, calculate and
3670 return the name of the source file and the line nearest to the wanted
3671 location.
3672 */
3673
3674 static          boolean
3675 DEFUN(coff_find_nearest_line,(abfd,
3676                               section,
3677                               ignore_symbols,
3678                               offset,
3679                               filename_ptr,
3680                               functionname_ptr,
3681                               line_ptr),
3682       bfd            *abfd AND
3683       asection       *section AND
3684       asymbol       **ignore_symbols AND
3685       bfd_vma         offset AND
3686       CONST char      **filename_ptr AND
3687       CONST char       **functionname_ptr AND
3688       unsigned int   *line_ptr)
3689 {
3690   static bfd     *cache_abfd;
3691   static asection *cache_section;
3692   static bfd_vma  cache_offset;
3693   static unsigned int cache_i;
3694   static alent   *cache_l;
3695
3696   unsigned int    i = 0;
3697   coff_data_type *cof = coff_data(abfd);
3698   /* Run through the raw syments if available */
3699   combined_entry_type *p;
3700   alent          *l;
3701   unsigned int    line_base = 0;
3702
3703
3704   *filename_ptr = 0;
3705   *functionname_ptr = 0;
3706   *line_ptr = 0;
3707
3708   /* Don't try and find line numbers in a non coff file */
3709   if (abfd->xvec->flavour != bfd_target_coff_flavour)
3710     return false;
3711
3712   if (cof == NULL)
3713     return false;
3714
3715   p = cof->raw_syments;
3716
3717   for (i = 0; i < cof->raw_syment_count; i++) {
3718     if (p->u.syment.n_sclass == C_FILE) {
3719       /* File name has been moved into symbol */
3720       *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
3721       break;
3722     }
3723     p += 1 +  p->u.syment.n_numaux;
3724   }
3725   /* Now wander though the raw linenumbers of the section */
3726   /*
3727     If this is the same BFD as we were previously called with and this is
3728     the same section, and the offset we want is further down then we can
3729     prime the lookup loop
3730     */
3731   if (abfd == cache_abfd &&
3732       section == cache_section &&
3733       offset >= cache_offset) {
3734     i = cache_i;
3735     l = cache_l;
3736   }
3737   else {
3738     i = 0;
3739     l = section->lineno;
3740   }
3741
3742   for (; i < section->lineno_count; i++) {
3743     if (l->line_number == 0) {
3744       /* Get the symbol this line number points at */
3745       coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
3746       *functionname_ptr = coff->symbol.name;
3747       if (coff->native) {
3748         combined_entry_type  *s = coff->native;
3749         s = s + 1 + s->u.syment.n_numaux;
3750         /*
3751           S should now point to the .bf of the function
3752           */
3753         if (s->u.syment.n_numaux) {
3754           /*
3755             The linenumber is stored in the auxent
3756             */
3757           union internal_auxent   *a = &((s + 1)->u.auxent);
3758           line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
3759         }
3760       }
3761     }
3762     else {
3763       if (l->u.offset > offset)
3764         break;
3765       *line_ptr = l->line_number + line_base + 1;
3766     }
3767     l++;
3768   }
3769
3770   cache_abfd = abfd;
3771   cache_section = section;
3772   cache_offset = offset;
3773   cache_i = i;
3774   cache_l = l;
3775
3776   return true;
3777 }
3778
3779 #ifdef GNU960
3780 file_ptr
3781 coff_sym_filepos(abfd)
3782 bfd *abfd;
3783   {
3784     return obj_sym_filepos(abfd);
3785   }
3786 #endif
3787
3788 #endif /* NO_COFF_SYMBOLS */
3789
3790
3791 static int
3792 DEFUN(coff_sizeof_headers,(abfd, reloc),
3793       bfd *abfd AND
3794       boolean reloc)
3795 {
3796     size_t size;
3797
3798     if (reloc == false) {
3799         size = FILHSZ + AOUTSZ;
3800     }
3801     else {
3802         size = FILHSZ;
3803     }
3804
3805     size +=  abfd->section_count * SCNHSZ;
3806     return size;
3807 }
3808
3809 static bfd_vma 
3810 DEFUN(get_value,(reloc, seclet),
3811       arelent  *reloc AND
3812       bfd_seclet_type *seclet)
3813 {
3814   bfd_vma value;
3815   asymbol *symbol = *(reloc->sym_ptr_ptr);
3816   /* A symbol holds a pointer to a section, and an offset from the
3817      base of the section.  To relocate, we find where the section will
3818      live in the output and add that in */
3819
3820   if (symbol->section == &bfd_und_section)
3821   {
3822     /* Ouch, this is an undefined symbol.. */
3823     bfd_error_vector.undefined_symbol(reloc, seclet);
3824     value = symbol->value;
3825   }
3826   else 
3827   {
3828     value = symbol->value +
3829      symbol->section->output_offset +
3830       symbol->section->output_section->vma;
3831   }
3832   
3833   
3834   /* Add the value contained in the relocation */
3835   value += (short)((reloc->addend) & 0xffff);
3836   
3837   return value;
3838 }
3839
3840 static void
3841 DEFUN(perform_slip,(s, slip, input_section, value),
3842       asymbol **s AND
3843       unsigned int slip AND
3844       asection *input_section AND
3845       bfd_vma value)
3846 {
3847   
3848   /* Find all symbols past this point, and make them know
3849      what's happened */
3850   while (*s) 
3851   {
3852     asymbol *p = *s;
3853     if (p->section == input_section) 
3854     {
3855       /* This was pointing into this section, so mangle it */
3856       if (p->value > value)
3857       {
3858         p->value -= slip;
3859       }
3860     }
3861     s++;
3862         
3863   }    
3864 }
3865 static int 
3866 DEFUN(movb1,(input_section, symbols, r, shrink),
3867       asection *input_section AND
3868       asymbol **symbols AND
3869       arelent *r AND
3870       unsigned int shrink) 
3871 {
3872   bfd_vma value = get_value(r,0);
3873         
3874   if (value >= 0xff00)
3875   { 
3876
3877     /* Change the reloc type from 16bit, possible 8 to 8bit
3878        possible 16 */
3879     r->howto = r->howto + 1;      
3880     /* The place to relc moves back by one */
3881     r->address -=1;
3882           
3883     /* This will be two bytes smaller in the long run */
3884     shrink +=2 ;
3885     perform_slip(symbols, 2, input_section, r->address - shrink +1);
3886
3887           
3888   }      
3889   return shrink;      
3890 }
3891
3892 static int 
3893 DEFUN(jmp1,(input_section, symbols, r, shrink),
3894       asection *input_section AND
3895       asymbol **symbols AND
3896       arelent *r AND
3897       unsigned int shrink) 
3898 {
3899
3900   
3901   bfd_vma value = get_value(r, 0);
3902         
3903   bfd_vma dot = input_section->output_section->vma +
3904    input_section->output_offset + r->address;   
3905   bfd_vma gap;
3906   
3907   /* See if the address we're looking at within 127 bytes of where
3908      we are, if so then we can use a small branch rather than the
3909      jump we were going to */
3910
3911   gap = value - (dot - shrink);
3912   
3913
3914   if (-120 < (long)gap && (long)gap < 120 )
3915   { 
3916
3917     /* Change the reloc type from 16bit, possible 8 to 8bit
3918        possible 16 */
3919     r->howto = r->howto + 1;      
3920     /* The place to relc moves back by one */
3921     r->address -=1;
3922           
3923     /* This will be two bytes smaller in the long run */
3924     shrink +=2 ;
3925     perform_slip(symbols, 2, input_section, r->address-shrink +1);
3926
3927           
3928   }      
3929   return shrink;      
3930 }
3931
3932 static boolean 
3933 DEFUN(bfd_coff_relax_section,(abfd, i, symbols),
3934       bfd *abfd AND
3935       asection *i AND
3936       asymbol **symbols)
3937 {
3938   
3939   /* Get enough memory to hold the stuff */
3940   bfd *input_bfd = i->owner;
3941   asection *input_section = i;
3942   int shrink = 0 ;
3943   boolean new = false;
3944   
3945   bfd_size_type reloc_size = bfd_get_reloc_upper_bound(input_bfd,
3946                                                        input_section);
3947   arelent **reloc_vector = (arelent **)bfd_xmalloc(reloc_size);
3948
3949   /* Get the relocs and think about them */
3950   if (bfd_canonicalize_reloc(input_bfd, 
3951                              input_section,
3952                              reloc_vector,
3953                              symbols))
3954   {
3955     arelent **parent;
3956     for (parent = reloc_vector; *parent; parent++) 
3957     {
3958       arelent *r = *parent;
3959       switch (r->howto->type) {
3960         case R_MOVB2:
3961         case R_JMP2:
3962           
3963           shrink+=2;
3964           break;
3965           
3966         case R_MOVB1:
3967           shrink = movb1(input_section, symbols, r, shrink);
3968           new = true;
3969           
3970           break;
3971         case R_JMP1:
3972           shrink = jmp1(input_section, symbols, r, shrink);
3973           new = true;
3974           
3975           break;
3976         }
3977     }
3978
3979   }
3980   input_section->_cooked_size -= shrink;  
3981   free((char *)reloc_vector);
3982   return new;
3983 }
3984
3985 static bfd_byte *
3986 DEFUN(bfd_coff_get_relocated_section_contents,(in_abfd, seclet, data),
3987       bfd *in_abfd AND
3988       bfd_seclet_type *seclet AND
3989       bfd_byte *data)
3990
3991 {
3992   /* Get enough memory to hold the stuff */
3993   bfd *input_bfd = seclet->u.indirect.section->owner;
3994   asection *input_section = seclet->u.indirect.section;
3995   bfd_size_type reloc_size = bfd_get_reloc_upper_bound(input_bfd,
3996                                                        input_section);
3997   arelent **reloc_vector = (arelent **)bfd_xmalloc(reloc_size);
3998   
3999   /* read in the section */
4000   bfd_get_section_contents(input_bfd,
4001                            input_section,
4002                            data,
4003                            0,
4004                            input_section->_raw_size);
4005   
4006   
4007   if (bfd_canonicalize_reloc(input_bfd, 
4008                              input_section,
4009                              reloc_vector,
4010                              seclet->u.indirect.symbols) )
4011   {
4012     arelent **parent = reloc_vector;
4013     arelent *reloc ;
4014     
4015
4016
4017     unsigned int dst_address = 0;
4018     unsigned int src_address = 0;
4019     unsigned int run;
4020     unsigned int idx;
4021     
4022     /* Find how long a run we can do */
4023     while (dst_address < seclet->size) 
4024     {
4025       
4026       reloc = *parent;
4027       if (reloc) 
4028       {
4029         /* Note that the relaxing didn't tie up the addresses in the
4030            relocation, so we use the original address to work out the
4031            run of non-relocated data */
4032         run = reloc->address - src_address;
4033         parent++;
4034         
4035       }
4036       else 
4037       {
4038         run = seclet->size - dst_address;
4039       }
4040       /* Copy the bytes */
4041       for (idx = 0; idx < run; idx++)
4042       {
4043         data[dst_address++] = data[src_address++];
4044       }
4045     
4046       /* Now do the relocation */
4047     
4048       if (reloc) 
4049       {
4050         switch (reloc->howto->type) 
4051         {
4052         case R_JMP2:
4053           /* Speciial relaxed type */
4054         {
4055           bfd_vma dot = seclet->offset + dst_address + seclet->u.indirect.section->output_section->vma;   
4056           int   gap = get_value(reloc,seclet)-dot-1;
4057           if ((gap & ~0xff  ) != 0 &&((gap & 0xff00)!= 0xff00)) abort();
4058
4059           bfd_put_8(in_abfd,gap,   data+dst_address);
4060
4061           switch (data[dst_address-1]) 
4062           {
4063             
4064           case 0x5e:
4065             /* jsr -> bsr */
4066             bfd_put_8(in_abfd, 0x55, data+dst_address-1);
4067             break;
4068           case 0x5a:     
4069             /* jmp ->bra */
4070             bfd_put_8(in_abfd, 0x40, data+dst_address-1);
4071             break;
4072           
4073           default:
4074             abort();
4075           
4076           }
4077         
4078         
4079
4080              
4081           dst_address++;
4082           src_address+=3;
4083   
4084           break;
4085         }
4086
4087              
4088         case R_MOVB2:
4089           /* Special relaxed type, there will be a gap between where we
4090              get stuff from and where we put stuff to now 
4091              
4092              for a mov.b @aa:16 -> mov.b @aa:8
4093              opcode 0x6a 0x0y offset
4094              ->     0x2y off
4095              */
4096           if (data[dst_address-1] != 0x6a)
4097            abort();
4098           switch (data[src_address] & 0xf0) 
4099           {
4100           case 0x00:
4101             /* Src is memory */
4102             data[dst_address-1] = (data[src_address] & 0xf) | 0x20;
4103             break;
4104           case 0x80:
4105             /* Src is reg */
4106             data[dst_address-1] = (data[src_address] & 0xf) | 0x30;
4107             break;
4108           default:
4109             abort();
4110           }
4111         
4112           /* the offset must fit ! after all, what was all the relaxing
4113              about ? */
4114
4115           bfd_put_8(in_abfd, get_value(reloc, seclet), data + dst_address);
4116
4117           /* Note the magic - src goes up by two bytes, but dst by only
4118              one */
4119           dst_address+=1;
4120           src_address+=3;
4121         
4122           break;
4123           /* PCrel 8 bits */
4124         case R_PCRBYTE:   
4125         {
4126           bfd_vma dot = seclet->offset + dst_address + seclet->u.indirect.section->output_section->vma;   
4127           int   gap = get_value(reloc,seclet)-dot;
4128           if (gap > 127 || gap < -128) 
4129           {
4130             bfd_error_vector.reloc_value_truncated(reloc, seclet);
4131           }
4132           
4133           bfd_put_8(in_abfd,gap,   data+dst_address);
4134           dst_address++;
4135           src_address++;
4136   
4137           break;
4138         }
4139
4140         case R_RELBYTE:
4141         {
4142           unsigned  int gap =get_value(reloc,seclet);
4143           if (gap > 0xff && gap < ~0xff)
4144           {
4145             bfd_error_vector.reloc_value_truncated(reloc, seclet);
4146           }
4147           
4148           bfd_put_8(in_abfd, gap, data+dst_address);
4149           dst_address+=1;
4150           src_address+=1;
4151
4152
4153         }
4154           break; 
4155         case R_JMP1:
4156           /* A relword which would have like to have been a pcrel */
4157         case R_MOVB1:   
4158           /* A relword which would like to have been modified but
4159              didn't make it */
4160         case R_RELWORD:
4161           bfd_put_16(in_abfd, get_value(reloc,seclet), data+dst_address);
4162           dst_address+=2;
4163           src_address+=2;
4164           break;
4165         
4166         default:
4167           abort();
4168         }
4169       }    
4170     }
4171   }
4172   free((char *)reloc_vector);
4173   return data;
4174   
4175 }
4176
4177
4178 #define coff_core_file_failing_command  _bfd_dummy_core_file_failing_command
4179 #define coff_core_file_failing_signal   _bfd_dummy_core_file_failing_signal
4180 #define coff_core_file_matches_executable_p     _bfd_dummy_core_file_matches_executable_p
4181 #define coff_slurp_armap                bfd_slurp_coff_armap
4182 #define coff_slurp_extended_name_table  _bfd_slurp_extended_name_table
4183 #define coff_truncate_arname            bfd_dont_truncate_arname
4184 #define coff_openr_next_archived_file   bfd_generic_openr_next_archived_file
4185 #define coff_generic_stat_arch_elt      bfd_generic_stat_arch_elt
4186 #define coff_get_section_contents       bfd_generic_get_section_contents
4187 #define coff_close_and_cleanup          bfd_generic_close_and_cleanup
4188
4189 #define coff_bfd_debug_info_start               bfd_void
4190 #define coff_bfd_debug_info_end         bfd_void
4191 #define coff_bfd_debug_info_accumulate  (PROTO(void,(*),(bfd*, struct sec *))) bfd_void
4192 #define coff_bfd_get_relocated_section_contents  bfd_generic_get_relocated_section_contents
4193 #define coff_bfd_relax_section bfd_generic_relax_section
4194