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