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