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