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