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