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