Commit | Line | Data |
---|---|---|
075caafd | 1 | /* Support for the generic parts of COFF, for BFD. |
9783e04a | 2 | Copyright 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc. |
075caafd ILT |
3 | Written by Cygnus Support. |
4 | ||
5 | This file is part of BFD, the Binary File Descriptor library. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
20 | ||
21 | /* Most of this hacked by Steve Chamberlain, sac@cygnus.com. | |
22 | Split out of coffcode.h by Ian Taylor, ian@cygnus.com. */ | |
23 | ||
24 | /* This file contains COFF code that is not dependent on any | |
25 | particular COFF target. There is only one version of this file in | |
26 | libbfd.a, so no target specific code may be put in here. Or, to | |
27 | put it another way, | |
28 | ||
29 | ********** DO NOT PUT TARGET SPECIFIC CODE IN THIS FILE ********** | |
30 | ||
31 | If you need to add some target specific behaviour, add a new hook | |
32 | function to bfd_coff_backend_data. | |
33 | ||
34 | Some of these functions are also called by the ECOFF routines. | |
35 | Those functions may not use any COFF specific information, such as | |
36 | coff_data (abfd). */ | |
37 | ||
38 | #include "bfd.h" | |
39 | #include "sysdep.h" | |
40 | #include "libbfd.h" | |
41 | #include "coff/internal.h" | |
42 | #include "libcoff.h" | |
43 | ||
bfe8224f ILT |
44 | static boolean coff_write_symbol PARAMS ((bfd *, asymbol *, |
45 | combined_entry_type *, | |
46 | unsigned int *)); | |
47 | static boolean coff_write_alien_symbol PARAMS ((bfd *, asymbol *, | |
48 | unsigned int *)); | |
49 | static boolean coff_write_native_symbol PARAMS ((bfd *, coff_symbol_type *, | |
50 | unsigned int *)); | |
51 | ||
075caafd ILT |
52 | static asection bfd_debug_section = { "*DEBUG*" }; |
53 | ||
9fbe895a ILT |
54 | #define STRING_SIZE_SIZE (4) |
55 | ||
075caafd ILT |
56 | /* Take a section header read from a coff file (in HOST byte order), |
57 | and make a BFD "section" out of it. This is used by ECOFF. */ | |
58 | static boolean | |
25057836 JL |
59 | make_a_section_from_file (abfd, hdr, target_index) |
60 | bfd *abfd; | |
61 | struct internal_scnhdr *hdr; | |
62 | unsigned int target_index; | |
075caafd ILT |
63 | { |
64 | asection *return_section; | |
65 | char *name; | |
66 | ||
67 | /* Assorted wastage to null-terminate the name, thanks AT&T! */ | |
68 | name = bfd_alloc(abfd, sizeof (hdr->s_name)+1); | |
69 | if (name == NULL) { | |
d1ad85a6 | 70 | bfd_set_error (bfd_error_no_memory); |
075caafd ILT |
71 | return false; |
72 | } | |
73 | strncpy(name, (char *) &hdr->s_name[0], sizeof (hdr->s_name)); | |
74 | name[sizeof (hdr->s_name)] = 0; | |
75 | ||
76 | return_section = bfd_make_section(abfd, name); | |
77 | if (return_section == NULL) | |
78 | return_section = bfd_coff_make_section_hook (abfd, name); | |
4c3721d5 ILT |
79 | |
80 | /* Handle several sections of the same name. For example, if an executable | |
81 | has two .bss sections, GDB better be able to find both of them | |
82 | (PR 3562). */ | |
83 | if (return_section == NULL) | |
84 | return_section = bfd_make_section_anyway (abfd, name); | |
85 | ||
075caafd ILT |
86 | if (return_section == NULL) |
87 | return false; | |
88 | ||
89 | /* s_paddr is presumed to be = to s_vaddr */ | |
90 | ||
91 | return_section->vma = hdr->s_vaddr; | |
92 | return_section->_raw_size = hdr->s_size; | |
93 | return_section->filepos = hdr->s_scnptr; | |
94 | return_section->rel_filepos = hdr->s_relptr; | |
95 | return_section->reloc_count = hdr->s_nreloc; | |
96 | ||
97 | bfd_coff_set_alignment_hook (abfd, return_section, hdr); | |
98 | ||
99 | return_section->line_filepos = hdr->s_lnnoptr; | |
100 | ||
101 | return_section->lineno_count = hdr->s_nlnno; | |
102 | return_section->userdata = NULL; | |
103 | return_section->next = (asection *) NULL; | |
e8fbe6d9 | 104 | return_section->flags = bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name); |
075caafd ILT |
105 | |
106 | return_section->target_index = target_index; | |
107 | ||
2d1e6c9c KR |
108 | /* At least on i386-coff, the line number count for a shared library |
109 | section must be ignored. */ | |
c16313f0 | 110 | if ((return_section->flags & SEC_COFF_SHARED_LIBRARY) != 0) |
2d1e6c9c KR |
111 | return_section->lineno_count = 0; |
112 | ||
075caafd ILT |
113 | if (hdr->s_nreloc != 0) |
114 | return_section->flags |= SEC_RELOC; | |
115 | /* FIXME: should this check 'hdr->s_size > 0' */ | |
116 | if (hdr->s_scnptr != 0) | |
117 | return_section->flags |= SEC_HAS_CONTENTS; | |
118 | return true; | |
119 | } | |
120 | ||
121 | /* Read in a COFF object and make it into a BFD. This is used by | |
122 | ECOFF as well. */ | |
123 | ||
2f3508ad | 124 | static const bfd_target * |
25057836 JL |
125 | coff_real_object_p (abfd, nscns, internal_f, internal_a) |
126 | bfd *abfd; | |
127 | unsigned nscns; | |
128 | struct internal_filehdr *internal_f; | |
129 | struct internal_aouthdr *internal_a; | |
075caafd ILT |
130 | { |
131 | PTR tdata; | |
132 | size_t readsize; /* length of file_info */ | |
133 | unsigned int scnhsz; | |
134 | char *external_sections; | |
135 | ||
136 | /* Build a play area */ | |
27f524a3 | 137 | tdata = bfd_coff_mkobject_hook (abfd, (PTR) internal_f, (PTR) internal_a); |
075caafd ILT |
138 | if (tdata == NULL) |
139 | return 0; | |
140 | ||
141 | scnhsz = bfd_coff_scnhsz (abfd); | |
142 | readsize = nscns * scnhsz; | |
143 | external_sections = (char *)bfd_alloc(abfd, readsize); | |
9783e04a DM |
144 | if (!external_sections) |
145 | { | |
d1ad85a6 | 146 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
147 | goto fail; |
148 | } | |
075caafd ILT |
149 | |
150 | if (bfd_read((PTR)external_sections, 1, readsize, abfd) != readsize) { | |
151 | goto fail; | |
152 | } | |
153 | ||
154 | /* Now copy data as required; construct all asections etc */ | |
155 | if (nscns != 0) { | |
156 | unsigned int i; | |
157 | for (i = 0; i < nscns; i++) { | |
158 | struct internal_scnhdr tmp; | |
159 | bfd_coff_swap_scnhdr_in(abfd, (PTR) (external_sections + i * scnhsz), | |
160 | (PTR) &tmp); | |
161 | make_a_section_from_file(abfd,&tmp, i+1); | |
162 | } | |
163 | } | |
164 | ||
165 | /* make_abs_section(abfd);*/ | |
166 | ||
167 | if (bfd_coff_set_arch_mach_hook (abfd, (PTR) internal_f) == false) | |
168 | goto fail; | |
169 | ||
170 | if (!(internal_f->f_flags & F_RELFLG)) | |
171 | abfd->flags |= HAS_RELOC; | |
172 | if ((internal_f->f_flags & F_EXEC)) | |
173 | abfd->flags |= EXEC_P; | |
174 | if (!(internal_f->f_flags & F_LNNO)) | |
175 | abfd->flags |= HAS_LINENO; | |
176 | if (!(internal_f->f_flags & F_LSYMS)) | |
177 | abfd->flags |= HAS_LOCALS; | |
178 | ||
2f3508ad ILT |
179 | /* FIXME: How can we set D_PAGED correctly? */ |
180 | if ((internal_f->f_flags & F_EXEC) != 0) | |
181 | abfd->flags |= D_PAGED; | |
075caafd | 182 | |
a74d1517 | 183 | obj_conv_table_size (abfd) = bfd_get_symcount(abfd) = internal_f->f_nsyms; |
075caafd ILT |
184 | if (internal_f->f_nsyms) |
185 | abfd->flags |= HAS_SYMS; | |
186 | ||
27f524a3 ILT |
187 | if (internal_a != (struct internal_aouthdr *) NULL) |
188 | bfd_get_start_address (abfd) = internal_a->entry; | |
189 | else | |
190 | bfd_get_start_address (abfd) = 0; | |
075caafd ILT |
191 | |
192 | return abfd->xvec; | |
193 | fail: | |
194 | bfd_release(abfd, tdata); | |
2f3508ad | 195 | return (const bfd_target *)NULL; |
075caafd ILT |
196 | } |
197 | ||
d1ad85a6 | 198 | /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is |
075caafd ILT |
199 | not a COFF file. This is also used by ECOFF. */ |
200 | ||
2f3508ad | 201 | const bfd_target * |
25057836 JL |
202 | coff_object_p (abfd) |
203 | bfd *abfd; | |
075caafd ILT |
204 | { |
205 | unsigned int filhsz; | |
206 | unsigned int aoutsz; | |
207 | int nscns; | |
208 | PTR filehdr; | |
209 | struct internal_filehdr internal_f; | |
210 | struct internal_aouthdr internal_a; | |
211 | ||
075caafd ILT |
212 | /* figure out how much to read */ |
213 | filhsz = bfd_coff_filhsz (abfd); | |
214 | aoutsz = bfd_coff_aoutsz (abfd); | |
215 | ||
216 | filehdr = bfd_alloc (abfd, filhsz); | |
217 | if (filehdr == NULL) | |
218 | return 0; | |
219 | if (bfd_read(filehdr, 1, filhsz, abfd) != filhsz) | |
25057836 JL |
220 | { |
221 | if (bfd_get_error () != bfd_error_system_call) | |
222 | bfd_set_error (bfd_error_wrong_format); | |
223 | return 0; | |
224 | } | |
075caafd ILT |
225 | bfd_coff_swap_filehdr_in(abfd, filehdr, &internal_f); |
226 | bfd_release (abfd, filehdr); | |
227 | ||
228 | if (bfd_coff_bad_format_hook (abfd, &internal_f) == false) { | |
d1ad85a6 | 229 | bfd_set_error (bfd_error_wrong_format); |
075caafd ILT |
230 | return 0; |
231 | } | |
232 | nscns =internal_f.f_nscns; | |
233 | ||
234 | if (internal_f.f_opthdr) { | |
235 | PTR opthdr; | |
236 | ||
237 | opthdr = bfd_alloc (abfd, aoutsz); | |
238 | if (opthdr == NULL) | |
239 | return 0;; | |
240 | if (bfd_read(opthdr, 1,aoutsz, abfd) != aoutsz) { | |
241 | return 0; | |
242 | } | |
243 | bfd_coff_swap_aouthdr_in(abfd, opthdr, (PTR)&internal_a); | |
244 | } | |
245 | ||
246 | /* Seek past the opt hdr stuff */ | |
c16313f0 ILT |
247 | if (bfd_seek(abfd, (file_ptr) (internal_f.f_opthdr + filhsz), SEEK_SET) |
248 | != 0) | |
249 | return NULL; | |
075caafd | 250 | |
27f524a3 ILT |
251 | return coff_real_object_p(abfd, nscns, &internal_f, |
252 | (internal_f.f_opthdr != 0 | |
253 | ? &internal_a | |
254 | : (struct internal_aouthdr *) NULL)); | |
075caafd ILT |
255 | } |
256 | ||
257 | /* Get the BFD section from a COFF symbol section number. */ | |
258 | ||
e8fbe6d9 | 259 | asection * |
25057836 JL |
260 | coff_section_from_bfd_index (abfd, index) |
261 | bfd *abfd; | |
262 | int index; | |
075caafd ILT |
263 | { |
264 | struct sec *answer = abfd->sections; | |
265 | ||
266 | if (index == N_ABS) | |
267 | { | |
e8fbe6d9 | 268 | return bfd_abs_section_ptr; |
075caafd ILT |
269 | } |
270 | if (index == N_UNDEF) | |
271 | { | |
e8fbe6d9 | 272 | return bfd_und_section_ptr; |
075caafd ILT |
273 | } |
274 | if(index == N_DEBUG) | |
275 | { | |
276 | return &bfd_debug_section; | |
277 | ||
278 | } | |
279 | ||
280 | while (answer) { | |
281 | if (answer->target_index == index) | |
282 | return answer; | |
283 | answer = answer->next; | |
284 | } | |
c16313f0 ILT |
285 | |
286 | /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a | |
287 | has a bad symbol table in biglitpow.o. */ | |
e8fbe6d9 | 288 | return bfd_und_section_ptr; |
075caafd ILT |
289 | } |
290 | ||
291 | /* Get the upper bound of a COFF symbol table. */ | |
292 | ||
326e32d7 | 293 | long |
075caafd ILT |
294 | coff_get_symtab_upper_bound(abfd) |
295 | bfd *abfd; | |
296 | { | |
297 | if (!bfd_coff_slurp_symbol_table(abfd)) | |
326e32d7 | 298 | return -1; |
075caafd ILT |
299 | |
300 | return (bfd_get_symcount(abfd) + 1) * (sizeof(coff_symbol_type *)); | |
301 | } | |
302 | ||
303 | ||
304 | /* Canonicalize a COFF symbol table. */ | |
305 | ||
326e32d7 | 306 | long |
25057836 JL |
307 | coff_get_symtab (abfd, alocation) |
308 | bfd *abfd; | |
309 | asymbol **alocation; | |
075caafd ILT |
310 | { |
311 | unsigned int counter = 0; | |
312 | coff_symbol_type *symbase; | |
313 | coff_symbol_type **location = (coff_symbol_type **) (alocation); | |
314 | if (!bfd_coff_slurp_symbol_table(abfd)) | |
326e32d7 | 315 | return -1; |
075caafd ILT |
316 | |
317 | symbase = obj_symbols(abfd); | |
318 | while (counter < bfd_get_symcount(abfd)) | |
319 | { | |
320 | /* This nasty code looks at the symbol to decide whether or | |
321 | not it is descibes a constructor/destructor entry point. It | |
322 | is structured this way to (hopefully) speed non matches */ | |
323 | #if 0 | |
324 | if (0 && symbase->symbol.name[9] == '$') | |
325 | { | |
326 | bfd_constructor_entry(abfd, | |
327 | (asymbol **)location, | |
328 | symbase->symbol.name[10] == 'I' ? | |
329 | "CTOR" : "DTOR"); | |
330 | } | |
331 | #endif | |
332 | *(location++) = symbase++; | |
333 | counter++; | |
334 | } | |
335 | *location++ = 0; | |
336 | return bfd_get_symcount(abfd); | |
337 | } | |
338 | ||
339 | /* Set lineno_count for the output sections of a COFF file. */ | |
340 | ||
27f524a3 | 341 | int |
25057836 JL |
342 | coff_count_linenumbers (abfd) |
343 | bfd *abfd; | |
075caafd ILT |
344 | { |
345 | unsigned int limit = bfd_get_symcount(abfd); | |
346 | unsigned int i; | |
27f524a3 | 347 | int total = 0; |
075caafd | 348 | asymbol **p; |
27f524a3 ILT |
349 | { |
350 | asection *s = abfd->sections->output_section; | |
351 | while (s) { | |
352 | BFD_ASSERT(s->lineno_count == 0); | |
353 | s = s->next; | |
354 | } | |
355 | } | |
075caafd ILT |
356 | |
357 | ||
358 | for (p = abfd->outsymbols, i = 0; i < limit; i++, p++) { | |
359 | asymbol *q_maybe = *p; | |
360 | if (bfd_asymbol_flavour(q_maybe) == bfd_target_coff_flavour) { | |
361 | coff_symbol_type *q = coffsymbol(q_maybe); | |
362 | if (q->lineno) { | |
363 | /* | |
364 | This symbol has a linenumber, increment the owning | |
365 | section's linenumber count | |
366 | */ | |
367 | alent *l = q->lineno; | |
368 | q->symbol.section->output_section->lineno_count++; | |
27f524a3 | 369 | total ++; |
075caafd ILT |
370 | l++; |
371 | while (l->line_number) { | |
27f524a3 | 372 | total ++; |
075caafd ILT |
373 | q->symbol.section->output_section->lineno_count++; |
374 | l++; | |
375 | } | |
376 | } | |
377 | } | |
378 | } | |
27f524a3 | 379 | return total; |
075caafd ILT |
380 | } |
381 | ||
382 | /* Takes a bfd and a symbol, returns a pointer to the coff specific | |
383 | area of the symbol if there is one. */ | |
384 | ||
330595d0 | 385 | /*ARGSUSED*/ |
075caafd | 386 | coff_symbol_type * |
25057836 JL |
387 | coff_symbol_from (ignore_abfd, symbol) |
388 | bfd *ignore_abfd; | |
389 | asymbol *symbol; | |
075caafd ILT |
390 | { |
391 | if (bfd_asymbol_flavour(symbol) != bfd_target_coff_flavour) | |
392 | return (coff_symbol_type *)NULL; | |
393 | ||
394 | if (bfd_asymbol_bfd(symbol)->tdata.coff_obj_data == (coff_data_type*)NULL) | |
395 | return (coff_symbol_type *)NULL; | |
396 | ||
397 | return (coff_symbol_type *) symbol; | |
398 | } | |
399 | ||
400 | static void | |
25057836 JL |
401 | fixup_symbol_value (coff_symbol_ptr, syment) |
402 | coff_symbol_type *coff_symbol_ptr; | |
403 | struct internal_syment *syment; | |
075caafd ILT |
404 | { |
405 | ||
406 | /* Normalize the symbol flags */ | |
e61cfdf8 | 407 | if (bfd_is_com_section (coff_symbol_ptr->symbol.section)) { |
075caafd ILT |
408 | /* a common symbol is undefined with a value */ |
409 | syment->n_scnum = N_UNDEF; | |
410 | syment->n_value = coff_symbol_ptr->symbol.value; | |
411 | } | |
412 | else if (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) { | |
413 | syment->n_value = coff_symbol_ptr->symbol.value; | |
414 | } | |
e8fbe6d9 | 415 | else if (bfd_is_und_section (coff_symbol_ptr->symbol.section)) { |
075caafd ILT |
416 | syment->n_scnum = N_UNDEF; |
417 | syment->n_value = 0; | |
418 | } | |
419 | else { | |
420 | if (coff_symbol_ptr->symbol.section) { | |
421 | syment->n_scnum = | |
422 | coff_symbol_ptr->symbol.section->output_section->target_index; | |
423 | ||
424 | syment->n_value = | |
425 | coff_symbol_ptr->symbol.value + | |
426 | coff_symbol_ptr->symbol.section->output_offset + | |
427 | coff_symbol_ptr->symbol.section->output_section->vma; | |
428 | } | |
429 | else { | |
430 | BFD_ASSERT(0); | |
431 | /* This can happen, but I don't know why yet (steve@cygnus.com) */ | |
432 | syment->n_scnum = N_ABS; | |
433 | syment->n_value = coff_symbol_ptr->symbol.value; | |
434 | } | |
435 | } | |
436 | } | |
437 | ||
438 | /* run through all the symbols in the symbol table and work out what | |
439 | their indexes into the symbol table will be when output | |
440 | ||
441 | Coff requires that each C_FILE symbol points to the next one in the | |
442 | chain, and that the last one points to the first external symbol. We | |
443 | do that here too. | |
444 | ||
445 | */ | |
9783e04a | 446 | boolean |
25057836 JL |
447 | coff_renumber_symbols (bfd_ptr) |
448 | bfd *bfd_ptr; | |
075caafd ILT |
449 | { |
450 | unsigned int symbol_count = bfd_get_symcount(bfd_ptr); | |
451 | asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols; | |
452 | unsigned int native_index = 0; | |
453 | struct internal_syment *last_file = (struct internal_syment *)NULL; | |
454 | unsigned int symbol_index; | |
455 | ||
456 | /* COFF demands that undefined symbols come after all other symbols. | |
457 | Since we don't need to impose this extra knowledge on all our client | |
458 | programs, deal with that here. Sort the symbol table; just move the | |
459 | undefined symbols to the end, leaving the rest alone. */ | |
460 | /* @@ Do we have some condition we could test for, so we don't always | |
461 | have to do this? I don't think relocatability is quite right, but | |
462 | I'm not certain. [raeburn:19920508.1711EST] */ | |
463 | { | |
464 | asymbol **newsyms; | |
465 | int i; | |
466 | ||
467 | newsyms = (asymbol **) bfd_alloc_by_size_t (bfd_ptr, | |
468 | sizeof (asymbol *) | |
469 | * (symbol_count + 1)); | |
9783e04a DM |
470 | if (!newsyms) |
471 | { | |
d1ad85a6 | 472 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
473 | return false; |
474 | } | |
075caafd ILT |
475 | bfd_ptr->outsymbols = newsyms; |
476 | for (i = 0; i < symbol_count; i++) | |
e8fbe6d9 | 477 | if (! bfd_is_und_section (symbol_ptr_ptr[i]->section)) |
075caafd ILT |
478 | *newsyms++ = symbol_ptr_ptr[i]; |
479 | for (i = 0; i < symbol_count; i++) | |
e8fbe6d9 | 480 | if (bfd_is_und_section (symbol_ptr_ptr[i]->section)) |
075caafd ILT |
481 | *newsyms++ = symbol_ptr_ptr[i]; |
482 | *newsyms = (asymbol *) NULL; | |
483 | symbol_ptr_ptr = bfd_ptr->outsymbols; | |
484 | } | |
485 | ||
486 | for (symbol_index = 0; symbol_index < symbol_count; symbol_index++) | |
487 | { | |
488 | coff_symbol_type *coff_symbol_ptr = coff_symbol_from(bfd_ptr, symbol_ptr_ptr[symbol_index]); | |
489 | if (coff_symbol_ptr && coff_symbol_ptr->native) { | |
490 | combined_entry_type *s = coff_symbol_ptr->native; | |
491 | int i; | |
492 | ||
493 | if (s->u.syment.n_sclass == C_FILE) | |
494 | { | |
495 | if (last_file != (struct internal_syment *)NULL) { | |
496 | last_file->n_value = native_index; | |
497 | } | |
498 | last_file = &(s->u.syment); | |
499 | } | |
500 | else { | |
501 | ||
502 | /* Modify the symbol values according to their section and | |
503 | type */ | |
504 | ||
505 | fixup_symbol_value(coff_symbol_ptr, &(s->u.syment)); | |
506 | } | |
507 | for (i = 0; i < s->u.syment.n_numaux + 1; i++) { | |
508 | s[i].offset = native_index ++; | |
509 | } | |
510 | } | |
511 | else { | |
512 | native_index++; | |
513 | } | |
514 | } | |
515 | obj_conv_table_size (bfd_ptr) = native_index; | |
9783e04a | 516 | return true; |
075caafd ILT |
517 | } |
518 | ||
519 | /* | |
520 | Run thorough the symbol table again, and fix it so that all pointers to | |
521 | entries are changed to the entries' index in the output symbol table. | |
522 | ||
523 | */ | |
524 | void | |
9783e04a DM |
525 | coff_mangle_symbols (bfd_ptr) |
526 | bfd *bfd_ptr; | |
075caafd | 527 | { |
9783e04a | 528 | unsigned int symbol_count = bfd_get_symcount (bfd_ptr); |
075caafd ILT |
529 | asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols; |
530 | unsigned int symbol_index; | |
531 | ||
532 | for (symbol_index = 0; symbol_index < symbol_count; symbol_index++) | |
9783e04a DM |
533 | { |
534 | coff_symbol_type *coff_symbol_ptr = | |
535 | coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]); | |
075caafd | 536 | |
9783e04a DM |
537 | if (coff_symbol_ptr && coff_symbol_ptr->native) |
538 | { | |
075caafd ILT |
539 | int i; |
540 | combined_entry_type *s = coff_symbol_ptr->native; | |
541 | ||
9783e04a DM |
542 | if (s->fix_value) |
543 | { | |
544 | /* FIXME: We should use a union here. */ | |
545 | s->u.syment.n_value = | |
546 | ((combined_entry_type *) s->u.syment.n_value)->offset; | |
547 | s->fix_value = 0; | |
075caafd | 548 | } |
9783e04a DM |
549 | for (i = 0; i < s->u.syment.n_numaux ; i++) |
550 | { | |
551 | combined_entry_type *a = s + i + 1; | |
552 | if (a->fix_tag) | |
553 | { | |
554 | a->u.auxent.x_sym.x_tagndx.l = | |
555 | a->u.auxent.x_sym.x_tagndx.p->offset; | |
556 | a->fix_tag = 0; | |
557 | } | |
558 | if (a->fix_end) | |
559 | { | |
560 | a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l = | |
561 | a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset; | |
562 | a->fix_end = 0; | |
563 | } | |
564 | if (a->fix_scnlen) | |
565 | { | |
566 | a->u.auxent.x_csect.x_scnlen.l = | |
567 | a->u.auxent.x_csect.x_scnlen.p->offset; | |
568 | a->fix_scnlen = 0; | |
569 | } | |
075caafd | 570 | } |
075caafd | 571 | } |
9783e04a | 572 | } |
075caafd ILT |
573 | } |
574 | ||
9783e04a DM |
575 | static bfd_size_type string_size; |
576 | static bfd_size_type debug_string_size; | |
577 | static asection *debug_string_section; | |
075caafd ILT |
578 | |
579 | static void | |
25057836 JL |
580 | coff_fix_symbol_name (abfd, symbol, native) |
581 | bfd *abfd; | |
582 | asymbol *symbol; | |
583 | combined_entry_type *native; | |
075caafd ILT |
584 | { |
585 | unsigned int name_length; | |
586 | union internal_auxent *auxent; | |
587 | char * name = ( char *)(symbol->name); | |
588 | ||
589 | if (name == (char *) NULL) { | |
590 | /* coff symbols always have names, so we'll make one up */ | |
591 | symbol->name = "strange"; | |
592 | name = (char *)symbol->name; | |
593 | } | |
594 | name_length = strlen(name); | |
595 | ||
596 | if (native->u.syment.n_sclass == C_FILE) { | |
597 | strncpy(native->u.syment._n._n_name, ".file", SYMNMLEN); | |
598 | auxent = &(native+1)->u.auxent; | |
599 | ||
600 | if (bfd_coff_long_filenames (abfd)) { | |
601 | if (name_length <= FILNMLEN) { | |
602 | strncpy(auxent->x_file.x_fname, name, FILNMLEN); | |
603 | } | |
604 | else { | |
9fbe895a | 605 | auxent->x_file.x_n.x_offset = string_size + STRING_SIZE_SIZE; |
075caafd ILT |
606 | auxent->x_file.x_n.x_zeroes = 0; |
607 | string_size += name_length + 1; | |
608 | } | |
609 | } | |
610 | else { | |
611 | strncpy(auxent->x_file.x_fname, name, FILNMLEN); | |
612 | if (name_length > FILNMLEN) { | |
613 | name[FILNMLEN] = '\0'; | |
614 | } | |
615 | } | |
616 | } | |
617 | else | |
9783e04a DM |
618 | { /* NOT A C_FILE SYMBOL */ |
619 | if (name_length <= SYMNMLEN) | |
620 | { | |
075caafd ILT |
621 | /* This name will fit into the symbol neatly */ |
622 | strncpy(native->u.syment._n._n_name, symbol->name, SYMNMLEN); | |
623 | } | |
9783e04a DM |
624 | else if (! bfd_coff_symname_in_debug (abfd, &native->u.syment)) |
625 | { | |
9fbe895a | 626 | native->u.syment._n._n_n._n_offset = string_size + STRING_SIZE_SIZE; |
075caafd ILT |
627 | native->u.syment._n._n_n._n_zeroes = 0; |
628 | string_size += name_length + 1; | |
629 | } | |
9783e04a DM |
630 | else |
631 | { | |
632 | long filepos; | |
633 | bfd_byte buf[2]; | |
634 | ||
635 | /* This name should be written into the .debug section. For | |
636 | some reason each name is preceded by a two byte length | |
637 | and also followed by a null byte. FIXME: We assume that | |
638 | the .debug section has already been created, and that it | |
639 | is large enough. */ | |
640 | if (debug_string_section == (asection *) NULL) | |
641 | debug_string_section = bfd_get_section_by_name (abfd, ".debug"); | |
642 | filepos = bfd_tell (abfd); | |
643 | bfd_put_16 (abfd, name_length + 1, buf); | |
644 | if (! bfd_set_section_contents (abfd, | |
645 | debug_string_section, | |
646 | (PTR) buf, | |
647 | (file_ptr) debug_string_size, | |
648 | (bfd_size_type) 2) | |
649 | || ! bfd_set_section_contents (abfd, | |
650 | debug_string_section, | |
651 | (PTR) symbol->name, | |
652 | (file_ptr) debug_string_size + 2, | |
653 | (bfd_size_type) name_length + 1)) | |
654 | abort (); | |
655 | if (bfd_seek (abfd, filepos, SEEK_SET) != 0) | |
656 | abort (); | |
657 | native->u.syment._n._n_n._n_offset = debug_string_size + 2; | |
658 | native->u.syment._n._n_n._n_zeroes = 0; | |
659 | debug_string_size += name_length + 3; | |
660 | } | |
661 | } | |
075caafd ILT |
662 | } |
663 | ||
bfe8224f ILT |
664 | /* We need to keep track of the symbol index so that when we write out |
665 | the relocs we can get the index for a symbol. This method is a | |
666 | hack. FIXME. */ | |
667 | ||
668 | #define set_index(symbol, idx) ((symbol)->udata = (PTR) (idx)) | |
669 | ||
670 | /* Write a symbol out to a COFF file. */ | |
075caafd | 671 | |
bfe8224f | 672 | static boolean |
25057836 JL |
673 | coff_write_symbol (abfd, symbol, native, written) |
674 | bfd *abfd; | |
675 | asymbol *symbol; | |
676 | combined_entry_type *native; | |
bfe8224f | 677 | unsigned int *written; |
075caafd | 678 | { |
bfe8224f ILT |
679 | unsigned int numaux = native->u.syment.n_numaux; |
680 | int type = native->u.syment.n_type; | |
681 | int class = native->u.syment.n_sclass; | |
075caafd ILT |
682 | PTR buf; |
683 | bfd_size_type symesz; | |
684 | ||
bfe8224f ILT |
685 | /* @@ bfd_debug_section isn't accessible outside this file, but we |
686 | know that C_FILE symbols belong there. So move them. */ | |
075caafd ILT |
687 | if (native->u.syment.n_sclass == C_FILE) |
688 | symbol->section = &bfd_debug_section; | |
689 | ||
e8fbe6d9 | 690 | if (bfd_is_abs_section (symbol->section)) |
bfe8224f ILT |
691 | { |
692 | native->u.syment.n_scnum = N_ABS; | |
693 | } | |
075caafd | 694 | else if (symbol->section == &bfd_debug_section) |
bfe8224f ILT |
695 | { |
696 | native->u.syment.n_scnum = N_DEBUG; | |
697 | } | |
e8fbe6d9 | 698 | else if (bfd_is_und_section (symbol->section)) |
bfe8224f ILT |
699 | { |
700 | native->u.syment.n_scnum = N_UNDEF; | |
701 | } | |
075caafd | 702 | else |
bfe8224f ILT |
703 | { |
704 | native->u.syment.n_scnum = | |
705 | symbol->section->output_section->target_index; | |
706 | } | |
075caafd | 707 | |
bfe8224f | 708 | coff_fix_symbol_name (abfd, symbol, native); |
075caafd ILT |
709 | |
710 | symesz = bfd_coff_symesz (abfd); | |
711 | buf = bfd_alloc (abfd, symesz); | |
9783e04a DM |
712 | if (!buf) |
713 | { | |
d1ad85a6 | 714 | bfd_set_error (bfd_error_no_memory); |
bfe8224f | 715 | return false; |
9783e04a | 716 | } |
bfe8224f ILT |
717 | bfd_coff_swap_sym_out (abfd, &native->u.syment, buf); |
718 | if (bfd_write (buf, 1, symesz, abfd) != symesz) | |
719 | return false; | |
075caafd ILT |
720 | bfd_release (abfd, buf); |
721 | ||
722 | if (native->u.syment.n_numaux > 0) | |
723 | { | |
724 | bfd_size_type auxesz; | |
725 | unsigned int j; | |
726 | ||
727 | auxesz = bfd_coff_auxesz (abfd); | |
728 | buf = bfd_alloc (abfd, auxesz); | |
9783e04a DM |
729 | if (!buf) |
730 | { | |
d1ad85a6 | 731 | bfd_set_error (bfd_error_no_memory); |
bfe8224f | 732 | return false; |
9783e04a | 733 | } |
075caafd ILT |
734 | for (j = 0; j < native->u.syment.n_numaux; j++) |
735 | { | |
bfe8224f ILT |
736 | bfd_coff_swap_aux_out (abfd, |
737 | &((native + j + 1)->u.auxent), | |
738 | type, | |
739 | class, | |
740 | j, | |
741 | native->u.syment.n_numaux, | |
742 | buf); | |
743 | if (bfd_write (buf, 1, auxesz, abfd)!= auxesz) | |
744 | return false; | |
075caafd ILT |
745 | } |
746 | bfd_release (abfd, buf); | |
747 | } | |
bfe8224f ILT |
748 | |
749 | /* Store the index for use when we write out the relocs. */ | |
750 | set_index (symbol, *written); | |
751 | ||
752 | *written += numaux + 1; | |
753 | return true; | |
075caafd ILT |
754 | } |
755 | ||
bfe8224f ILT |
756 | /* Write out a symbol to a COFF file that does not come from a COFF |
757 | file originally. This symbol may have been created by the linker, | |
758 | or we may be linking a non COFF file to a COFF file. */ | |
075caafd | 759 | |
bfe8224f | 760 | static boolean |
25057836 JL |
761 | coff_write_alien_symbol (abfd, symbol, written) |
762 | bfd *abfd; | |
763 | asymbol *symbol; | |
bfe8224f | 764 | unsigned int *written; |
075caafd | 765 | { |
075caafd ILT |
766 | combined_entry_type *native; |
767 | combined_entry_type dummy; | |
bfe8224f | 768 | |
075caafd ILT |
769 | native = &dummy; |
770 | native->u.syment.n_type = T_NULL; | |
771 | native->u.syment.n_flags = 0; | |
e8fbe6d9 | 772 | if (bfd_is_und_section (symbol->section)) |
bfe8224f ILT |
773 | { |
774 | native->u.syment.n_scnum = N_UNDEF; | |
775 | native->u.syment.n_value = symbol->value; | |
075caafd | 776 | } |
e61cfdf8 | 777 | else if (bfd_is_com_section (symbol->section)) |
bfe8224f ILT |
778 | { |
779 | native->u.syment.n_scnum = N_UNDEF; | |
780 | native->u.syment.n_value = symbol->value; | |
075caafd | 781 | } |
bfe8224f | 782 | else if (symbol->flags & BSF_DEBUGGING) |
075caafd | 783 | { |
a74d1517 ILT |
784 | /* There isn't much point to writing out a debugging symbol |
785 | unless we are prepared to convert it into COFF debugging | |
715cde57 ILT |
786 | format. So, we just ignore them. We must clobber the symbol |
787 | name to keep it from being put in the string table. */ | |
788 | symbol->name = ""; | |
a74d1517 | 789 | return true; |
075caafd | 790 | } |
bfe8224f ILT |
791 | else |
792 | { | |
793 | native->u.syment.n_scnum = | |
794 | symbol->section->output_section->target_index; | |
795 | native->u.syment.n_value = (symbol->value | |
796 | + symbol->section->output_section->vma | |
797 | + symbol->section->output_offset); | |
798 | ||
799 | /* Copy the any flags from the the file header into the symbol. | |
800 | FIXME: Why? */ | |
801 | { | |
802 | coff_symbol_type *c = coff_symbol_from (abfd, symbol); | |
803 | if (c != (coff_symbol_type *) NULL) | |
804 | native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags; | |
805 | } | |
075caafd ILT |
806 | } |
807 | ||
bfe8224f | 808 | native->u.syment.n_type = 0; |
075caafd | 809 | if (symbol->flags & BSF_LOCAL) |
bfe8224f | 810 | native->u.syment.n_sclass = C_STAT; |
075caafd | 811 | else |
bfe8224f ILT |
812 | native->u.syment.n_sclass = C_EXT; |
813 | native->u.syment.n_numaux = 0; | |
075caafd | 814 | |
bfe8224f | 815 | return coff_write_symbol (abfd, symbol, native, written); |
075caafd ILT |
816 | } |
817 | ||
bfe8224f ILT |
818 | /* Write a native symbol to a COFF file. */ |
819 | ||
820 | static boolean | |
25057836 JL |
821 | coff_write_native_symbol (abfd, symbol, written) |
822 | bfd *abfd; | |
823 | coff_symbol_type *symbol; | |
bfe8224f | 824 | unsigned int *written; |
075caafd | 825 | { |
075caafd | 826 | combined_entry_type *native = symbol->native; |
bfe8224f | 827 | alent *lineno = symbol->lineno; |
075caafd | 828 | |
bfe8224f ILT |
829 | /* If this symbol has an associated line number, we must store the |
830 | symbol index in the line number field. We also tag the auxent to | |
831 | point to the right place in the lineno table. */ | |
832 | if (lineno && !symbol->done_lineno) | |
833 | { | |
834 | unsigned int count = 0; | |
835 | lineno[count].u.offset = *written; | |
836 | if (native->u.syment.n_numaux) | |
837 | { | |
838 | union internal_auxent *a = &((native+1)->u.auxent); | |
075caafd | 839 | |
bfe8224f ILT |
840 | a->x_sym.x_fcnary.x_fcn.x_lnnoptr = |
841 | symbol->symbol.section->output_section->moving_line_filepos; | |
842 | } | |
075caafd | 843 | |
bfe8224f ILT |
844 | /* Count and relocate all other linenumbers. */ |
845 | count++; | |
846 | while (lineno[count].line_number != 0) | |
847 | { | |
075caafd | 848 | #if 0 |
bfe8224f ILT |
849 | /* 13 april 92. sac |
850 | I've been told this, but still need proof: | |
851 | > The second bug is also in `bfd/coffcode.h'. This bug | |
852 | > causes the linker to screw up the pc-relocations for | |
853 | > all the line numbers in COFF code. This bug isn't only | |
854 | > specific to A29K implementations, but affects all | |
855 | > systems using COFF format binaries. Note that in COFF | |
856 | > object files, the line number core offsets output by | |
857 | > the assembler are relative to the start of each | |
858 | > procedure, not to the start of the .text section. This | |
859 | > patch relocates the line numbers relative to the | |
860 | > `native->u.syment.n_value' instead of the section | |
861 | > virtual address. | |
862 | > modular!olson@cs.arizona.edu (Jon Olson) | |
863 | */ | |
864 | lineno[count].u.offset += native->u.syment.n_value; | |
075caafd | 865 | #else |
bfe8224f ILT |
866 | lineno[count].u.offset += |
867 | (symbol->symbol.section->output_section->vma | |
868 | + symbol->symbol.section->output_offset); | |
075caafd | 869 | #endif |
bfe8224f ILT |
870 | count++; |
871 | } | |
872 | symbol->done_lineno = true; | |
075caafd | 873 | |
bfe8224f ILT |
874 | symbol->symbol.section->output_section->moving_line_filepos += |
875 | count * bfd_coff_linesz (abfd); | |
876 | } | |
877 | ||
878 | return coff_write_symbol (abfd, &(symbol->symbol), native, written); | |
075caafd ILT |
879 | } |
880 | ||
bfe8224f ILT |
881 | /* Write out the COFF symbols. */ |
882 | ||
883 | boolean | |
25057836 | 884 | coff_write_symbols (abfd) |
bfe8224f | 885 | bfd *abfd; |
075caafd | 886 | { |
bfe8224f ILT |
887 | unsigned int i; |
888 | unsigned int limit = bfd_get_symcount(abfd); | |
889 | unsigned int written = 0; | |
890 | asymbol **p; | |
075caafd ILT |
891 | |
892 | string_size = 0; | |
9783e04a | 893 | debug_string_size = 0; |
075caafd ILT |
894 | |
895 | /* Seek to the right place */ | |
bfe8224f ILT |
896 | if (bfd_seek (abfd, obj_sym_filepos(abfd), SEEK_SET) != 0) |
897 | return false; | |
075caafd ILT |
898 | |
899 | /* Output all the symbols we have */ | |
900 | ||
901 | written = 0; | |
902 | for (p = abfd->outsymbols, i = 0; i < limit; i++, p++) | |
bfe8224f ILT |
903 | { |
904 | asymbol *symbol = *p; | |
905 | coff_symbol_type *c_symbol = coff_symbol_from (abfd, symbol); | |
075caafd | 906 | |
bfe8224f ILT |
907 | if (c_symbol == (coff_symbol_type *) NULL |
908 | || c_symbol->native == (combined_entry_type *)NULL) | |
909 | { | |
910 | if (! coff_write_alien_symbol (abfd, symbol, &written)) | |
911 | return false; | |
912 | } | |
913 | else | |
914 | { | |
915 | if (! coff_write_native_symbol (abfd, c_symbol, &written)) | |
916 | return false; | |
917 | } | |
918 | } | |
075caafd | 919 | |
bfe8224f | 920 | bfd_get_symcount (abfd) = written; |
075caafd ILT |
921 | |
922 | /* Now write out strings */ | |
923 | ||
924 | if (string_size != 0) | |
925 | { | |
9fbe895a ILT |
926 | unsigned int size = string_size + STRING_SIZE_SIZE; |
927 | bfd_byte buffer[STRING_SIZE_SIZE]; | |
075caafd | 928 | |
9fbe895a | 929 | #if STRING_SIZE_SIZE == 4 |
bfe8224f | 930 | bfd_h_put_32 (abfd, size, buffer); |
9fbe895a ILT |
931 | #else |
932 | #error Change bfd_h_put_32 | |
933 | #endif | |
c16313f0 ILT |
934 | if (bfd_write ((PTR) buffer, 1, sizeof (buffer), abfd) != sizeof (buffer)) |
935 | return false; | |
075caafd ILT |
936 | for (p = abfd->outsymbols, i = 0; |
937 | i < limit; | |
938 | i++, p++) | |
bfe8224f ILT |
939 | { |
940 | asymbol *q = *p; | |
941 | size_t name_length = strlen (q->name); | |
942 | coff_symbol_type *c_symbol = coff_symbol_from (abfd, q); | |
943 | size_t maxlen; | |
944 | ||
945 | /* Figure out whether the symbol name should go in the string | |
946 | table. Symbol names that are short enough are stored | |
947 | directly in the syment structure. File names permit a | |
948 | different, longer, length in the syment structure. On | |
949 | XCOFF, some symbol names are stored in the .debug section | |
950 | rather than in the string table. */ | |
951 | ||
952 | if (c_symbol == NULL | |
953 | || c_symbol->native == NULL) | |
954 | { | |
955 | /* This is not a COFF symbol, so it certainly is not a | |
956 | file name, nor does it go in the .debug section. */ | |
957 | maxlen = SYMNMLEN; | |
958 | } | |
959 | else if (bfd_coff_symname_in_debug (abfd, | |
960 | &c_symbol->native->u.syment)) | |
961 | { | |
962 | /* This symbol name is in the XCOFF .debug section. | |
963 | Don't write it into the string table. */ | |
964 | maxlen = name_length; | |
965 | } | |
966 | else if (c_symbol->native->u.syment.n_sclass == C_FILE) | |
967 | maxlen = FILNMLEN; | |
968 | else | |
969 | maxlen = SYMNMLEN; | |
970 | ||
971 | if (name_length > maxlen) | |
972 | { | |
973 | if (bfd_write ((PTR) (q->name), 1, name_length + 1, abfd) | |
974 | != name_length + 1) | |
975 | return false; | |
976 | } | |
977 | } | |
075caafd | 978 | } |
bfe8224f ILT |
979 | else |
980 | { | |
981 | /* We would normally not write anything here, but we'll write | |
982 | out 4 so that any stupid coff reader which tries to read the | |
983 | string table even when there isn't one won't croak. */ | |
9fbe895a ILT |
984 | unsigned int size = STRING_SIZE_SIZE; |
985 | bfd_byte buffer[STRING_SIZE_SIZE]; | |
bfe8224f | 986 | |
9fbe895a | 987 | #if STRING_SIZE_SIZE == 4 |
bfe8224f | 988 | bfd_h_put_32 (abfd, size, buffer); |
9fbe895a ILT |
989 | #else |
990 | #error Change bfd_h_put_32 | |
991 | #endif | |
992 | if (bfd_write ((PTR) buffer, 1, STRING_SIZE_SIZE, abfd) | |
993 | != STRING_SIZE_SIZE) | |
bfe8224f ILT |
994 | return false; |
995 | } | |
9783e04a | 996 | |
bfe8224f ILT |
997 | /* Make sure the .debug section was created to be the correct size. |
998 | We should create it ourselves on the fly, but we don't because | |
999 | BFD won't let us write to any section until we know how large all | |
1000 | the sections are. We could still do it by making another pass | |
1001 | over the symbols. FIXME. */ | |
9783e04a DM |
1002 | BFD_ASSERT (debug_string_size == 0 |
1003 | || (debug_string_section != (asection *) NULL | |
1004 | && (BFD_ALIGN (debug_string_size, | |
1005 | 1 << debug_string_section->alignment_power) | |
1006 | == bfd_section_size (abfd, debug_string_section)))); | |
bfe8224f ILT |
1007 | |
1008 | return true; | |
075caafd ILT |
1009 | } |
1010 | ||
9783e04a | 1011 | boolean |
25057836 JL |
1012 | coff_write_linenumbers (abfd) |
1013 | bfd *abfd; | |
075caafd ILT |
1014 | { |
1015 | asection *s; | |
1016 | bfd_size_type linesz; | |
1017 | PTR buff; | |
1018 | ||
1019 | linesz = bfd_coff_linesz (abfd); | |
1020 | buff = bfd_alloc (abfd, linesz); | |
9783e04a DM |
1021 | if (!buff) |
1022 | { | |
d1ad85a6 | 1023 | bfd_set_error (bfd_error_no_memory); |
25057836 | 1024 | return false; |
9783e04a | 1025 | } |
075caafd ILT |
1026 | for (s = abfd->sections; s != (asection *) NULL; s = s->next) { |
1027 | if (s->lineno_count) { | |
1028 | asymbol **q = abfd->outsymbols; | |
c16313f0 ILT |
1029 | if (bfd_seek(abfd, s->line_filepos, SEEK_SET) != 0) |
1030 | return false; | |
075caafd ILT |
1031 | /* Find all the linenumbers in this section */ |
1032 | while (*q) { | |
1033 | asymbol *p = *q; | |
27f524a3 ILT |
1034 | if (p->section->output_section == s) { |
1035 | alent *l = | |
1036 | BFD_SEND(bfd_asymbol_bfd(p), _get_lineno, (bfd_asymbol_bfd(p), p)); | |
1037 | if (l) { | |
1038 | /* Found a linenumber entry, output */ | |
1039 | struct internal_lineno out; | |
1040 | memset( (PTR)&out, 0, sizeof(out)); | |
1041 | out.l_lnno = 0; | |
075caafd ILT |
1042 | out.l_addr.l_symndx = l->u.offset; |
1043 | bfd_coff_swap_lineno_out(abfd, &out, buff); | |
c16313f0 ILT |
1044 | if (bfd_write(buff, 1, linesz, abfd) != linesz) |
1045 | return false; | |
075caafd | 1046 | l++; |
27f524a3 ILT |
1047 | while (l->line_number) { |
1048 | out.l_lnno = l->line_number; | |
1049 | out.l_addr.l_symndx = l->u.offset; | |
1050 | bfd_coff_swap_lineno_out(abfd, &out, buff); | |
c16313f0 ILT |
1051 | if (bfd_write(buff, 1, linesz, abfd) != linesz) |
1052 | return false; | |
27f524a3 ILT |
1053 | l++; |
1054 | } | |
075caafd ILT |
1055 | } |
1056 | } | |
1057 | q++; | |
1058 | } | |
1059 | } | |
1060 | } | |
1061 | bfd_release (abfd, buff); | |
9783e04a | 1062 | return true; |
075caafd ILT |
1063 | } |
1064 | ||
330595d0 | 1065 | /*ARGSUSED*/ |
075caafd | 1066 | alent * |
25057836 JL |
1067 | coff_get_lineno (ignore_abfd, symbol) |
1068 | bfd *ignore_abfd; | |
1069 | asymbol *symbol; | |
075caafd ILT |
1070 | { |
1071 | return coffsymbol(symbol)->lineno; | |
1072 | } | |
1073 | ||
1074 | asymbol * | |
1075 | coff_section_symbol (abfd, name) | |
1076 | bfd *abfd; | |
1077 | char *name; | |
1078 | { | |
1079 | asection *sec = bfd_make_section_old_way (abfd, name); | |
1080 | asymbol *sym; | |
1081 | combined_entry_type *csym; | |
1082 | ||
1083 | sym = sec->symbol; | |
4c3721d5 | 1084 | csym = coff_symbol_from (abfd, sym)->native; |
075caafd ILT |
1085 | /* Make sure back-end COFF stuff is there. */ |
1086 | if (csym == 0) | |
1087 | { | |
1088 | struct foo { | |
1089 | coff_symbol_type sym; | |
1090 | /* @@FIXME This shouldn't use a fixed size!! */ | |
1091 | combined_entry_type e[10]; | |
1092 | }; | |
1093 | struct foo *f; | |
1094 | f = (struct foo *) bfd_alloc_by_size_t (abfd, sizeof (*f)); | |
9783e04a DM |
1095 | if (!f) |
1096 | { | |
d1ad85a6 | 1097 | bfd_set_error (bfd_error_no_error); |
9783e04a DM |
1098 | return NULL; |
1099 | } | |
075caafd ILT |
1100 | memset ((char *) f, 0, sizeof (*f)); |
1101 | coff_symbol_from (abfd, sym)->native = csym = f->e; | |
1102 | } | |
1103 | csym[0].u.syment.n_sclass = C_STAT; | |
1104 | csym[0].u.syment.n_numaux = 1; | |
1105 | /* SF_SET_STATICS (sym); @@ ??? */ | |
4c3721d5 ILT |
1106 | csym[1].u.auxent.x_scn.x_scnlen = sec->_raw_size; |
1107 | csym[1].u.auxent.x_scn.x_nreloc = sec->reloc_count; | |
1108 | csym[1].u.auxent.x_scn.x_nlinno = sec->lineno_count; | |
1109 | ||
1110 | if (sec->output_section == NULL) | |
075caafd | 1111 | { |
4c3721d5 ILT |
1112 | sec->output_section = sec; |
1113 | sec->output_offset = 0; | |
075caafd | 1114 | } |
4c3721d5 | 1115 | |
075caafd ILT |
1116 | return sym; |
1117 | } | |
1118 | ||
1119 | /* This function transforms the offsets into the symbol table into | |
1120 | pointers to syments. */ | |
1121 | ||
1122 | static void | |
25057836 JL |
1123 | coff_pointerize_aux (abfd, table_base, type, class, auxent) |
1124 | bfd *abfd; | |
1125 | combined_entry_type *table_base; | |
1126 | int type; | |
1127 | int class; | |
1128 | combined_entry_type *auxent; | |
075caafd ILT |
1129 | { |
1130 | /* Don't bother if this is a file or a section */ | |
1131 | if (class == C_STAT && type == T_NULL) return; | |
1132 | if (class == C_FILE) return; | |
1133 | ||
1134 | /* Otherwise patch up */ | |
1135 | #define N_TMASK coff_data (abfd)->local_n_tmask | |
1136 | #define N_BTSHFT coff_data (abfd)->local_n_btshft | |
1137 | if (ISFCN(type) || ISTAG(class) || class == C_BLOCK) { | |
1138 | auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p = table_base + | |
1139 | auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l; | |
1140 | auxent->fix_end = 1; | |
1141 | } | |
1142 | /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can | |
1143 | generate one, so we must be careful to ignore it. */ | |
1144 | if (auxent->u.auxent.x_sym.x_tagndx.l > 0) { | |
1145 | auxent->u.auxent.x_sym.x_tagndx.p = | |
1146 | table_base + auxent->u.auxent.x_sym.x_tagndx.l; | |
1147 | auxent->fix_tag = 1; | |
1148 | } | |
1149 | } | |
1150 | ||
1151 | static char * | |
25057836 JL |
1152 | build_string_table (abfd) |
1153 | bfd *abfd; | |
075caafd | 1154 | { |
9fbe895a | 1155 | char string_table_size_buffer[STRING_SIZE_SIZE]; |
075caafd ILT |
1156 | unsigned int string_table_size; |
1157 | char *string_table; | |
1158 | ||
1159 | /* At this point we should be "seek"'d to the end of the | |
1160 | symbols === the symbol table size. */ | |
1161 | if (bfd_read((char *) string_table_size_buffer, | |
1162 | sizeof(string_table_size_buffer), | |
25057836 | 1163 | 1, abfd) != sizeof(string_table_size)) |
075caafd | 1164 | return (NULL); |
075caafd | 1165 | |
9fbe895a | 1166 | #if STRING_SIZE_SIZE == 4 |
075caafd | 1167 | string_table_size = bfd_h_get_32(abfd, (bfd_byte *) string_table_size_buffer); |
9fbe895a ILT |
1168 | #else |
1169 | #error Change bfd_h_get_32 | |
1170 | #endif | |
075caafd | 1171 | |
9fbe895a ILT |
1172 | if ((string_table = (PTR) bfd_alloc(abfd, |
1173 | string_table_size -= STRING_SIZE_SIZE)) | |
1174 | == NULL) | |
1175 | { | |
1176 | bfd_set_error (bfd_error_no_memory); | |
1177 | return (NULL); | |
1178 | } /* on mallocation error */ | |
25057836 | 1179 | if (bfd_read(string_table, string_table_size, 1, abfd) != string_table_size) |
075caafd | 1180 | return (NULL); |
075caafd ILT |
1181 | return string_table; |
1182 | } | |
1183 | ||
1184 | /* Allocate space for the ".debug" section, and read it. | |
1185 | We did not read the debug section until now, because | |
1186 | we didn't want to go to the trouble until someone needed it. */ | |
1187 | ||
1188 | static char * | |
25057836 JL |
1189 | build_debug_section (abfd) |
1190 | bfd *abfd; | |
075caafd ILT |
1191 | { |
1192 | char *debug_section; | |
1193 | long position; | |
1194 | ||
1195 | asection *sect = bfd_get_section_by_name (abfd, ".debug"); | |
1196 | ||
1197 | if (!sect) { | |
d1ad85a6 | 1198 | bfd_set_error (bfd_error_no_debug_section); |
075caafd ILT |
1199 | return NULL; |
1200 | } | |
1201 | ||
1202 | debug_section = (PTR) bfd_alloc (abfd, | |
1203 | bfd_get_section_size_before_reloc (sect)); | |
1204 | if (debug_section == NULL) { | |
d1ad85a6 | 1205 | bfd_set_error (bfd_error_no_memory); |
075caafd ILT |
1206 | return NULL; |
1207 | } | |
1208 | ||
1209 | /* Seek to the beginning of the `.debug' section and read it. | |
1210 | Save the current position first; it is needed by our caller. | |
1211 | Then read debug section and reset the file pointer. */ | |
1212 | ||
1213 | position = bfd_tell (abfd); | |
c16313f0 ILT |
1214 | if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0 |
1215 | || (bfd_read (debug_section, | |
1216 | bfd_get_section_size_before_reloc (sect), 1, abfd) | |
1217 | != bfd_get_section_size_before_reloc(sect)) | |
1218 | || bfd_seek (abfd, position, SEEK_SET) != 0) | |
075caafd | 1219 | return NULL; |
075caafd ILT |
1220 | return debug_section; |
1221 | } | |
1222 | ||
1223 | ||
1224 | /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be | |
1225 | \0-terminated, but will not exceed 'maxlen' characters. The copy *will* | |
1226 | be \0-terminated. */ | |
1227 | static char * | |
25057836 JL |
1228 | copy_name (abfd, name, maxlen) |
1229 | bfd *abfd; | |
1230 | char *name; | |
1231 | int maxlen; | |
075caafd ILT |
1232 | { |
1233 | int len; | |
1234 | char *newname; | |
1235 | ||
1236 | for (len = 0; len < maxlen; ++len) { | |
1237 | if (name[len] == '\0') { | |
1238 | break; | |
1239 | } | |
1240 | } | |
1241 | ||
1242 | if ((newname = (PTR) bfd_alloc(abfd, len+1)) == NULL) { | |
d1ad85a6 | 1243 | bfd_set_error (bfd_error_no_memory); |
075caafd ILT |
1244 | return (NULL); |
1245 | } | |
1246 | strncpy(newname, name, len); | |
1247 | newname[len] = '\0'; | |
1248 | return newname; | |
1249 | } | |
1250 | ||
1251 | /* Read a symbol table into freshly bfd_allocated memory, swap it, and | |
1252 | knit the symbol names into a normalized form. By normalized here I | |
1253 | mean that all symbols have an n_offset pointer that points to a null- | |
1254 | terminated string. */ | |
1255 | ||
1256 | combined_entry_type * | |
25057836 JL |
1257 | coff_get_normalized_symtab (abfd) |
1258 | bfd *abfd; | |
075caafd ILT |
1259 | { |
1260 | combined_entry_type *internal; | |
1261 | combined_entry_type *internal_ptr; | |
1262 | combined_entry_type *symbol_ptr; | |
1263 | combined_entry_type *internal_end; | |
1264 | bfd_size_type symesz; | |
1265 | PTR raw; | |
1266 | char *raw_src; | |
1267 | char *raw_end; | |
1268 | char *string_table = NULL; | |
1269 | char *debug_section = NULL; | |
1270 | unsigned long size; | |
1271 | ||
1272 | unsigned int raw_size; | |
1273 | if (obj_raw_syments(abfd) != (combined_entry_type *)NULL) { | |
1274 | return obj_raw_syments(abfd); | |
1275 | } | |
1276 | if ((size = bfd_get_symcount(abfd) * sizeof(combined_entry_type)) == 0) { | |
d1ad85a6 | 1277 | bfd_set_error (bfd_error_no_symbols); |
075caafd ILT |
1278 | return (NULL); |
1279 | } | |
1280 | ||
1281 | internal = (combined_entry_type *)bfd_alloc(abfd, size); | |
9783e04a DM |
1282 | if (!internal) |
1283 | { | |
d1ad85a6 | 1284 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
1285 | return NULL; |
1286 | } | |
075caafd ILT |
1287 | internal_end = internal + bfd_get_symcount(abfd); |
1288 | ||
1289 | symesz = bfd_coff_symesz (abfd); | |
1290 | raw_size = bfd_get_symcount(abfd) * symesz; | |
1291 | raw = bfd_alloc(abfd,raw_size); | |
9783e04a DM |
1292 | if (!raw) |
1293 | { | |
d1ad85a6 | 1294 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
1295 | return NULL; |
1296 | } | |
075caafd ILT |
1297 | |
1298 | if (bfd_seek(abfd, obj_sym_filepos(abfd), SEEK_SET) == -1 | |
25057836 | 1299 | || bfd_read(raw, raw_size, 1, abfd) != raw_size) |
075caafd | 1300 | return (NULL); |
075caafd ILT |
1301 | /* mark the end of the symbols */ |
1302 | raw_end = (char *) raw + bfd_get_symcount(abfd) * symesz; | |
1303 | /* | |
1304 | FIXME SOMEDAY. A string table size of zero is very weird, but | |
1305 | probably possible. If one shows up, it will probably kill us. | |
1306 | */ | |
1307 | ||
1308 | /* Swap all the raw entries */ | |
1309 | for (raw_src = (char *) raw, internal_ptr = internal; | |
1310 | raw_src < raw_end; | |
1311 | raw_src += symesz, internal_ptr++) { | |
1312 | ||
1313 | unsigned int i; | |
1314 | bfd_coff_swap_sym_in(abfd, (PTR)raw_src, (PTR)&internal_ptr->u.syment); | |
9783e04a | 1315 | internal_ptr->fix_value = 0; |
075caafd ILT |
1316 | internal_ptr->fix_tag = 0; |
1317 | internal_ptr->fix_end = 0; | |
9783e04a | 1318 | internal_ptr->fix_scnlen = 0; |
075caafd | 1319 | symbol_ptr = internal_ptr; |
85fe7cff | 1320 | |
075caafd ILT |
1321 | for (i = 0; |
1322 | i < symbol_ptr->u.syment.n_numaux; | |
1323 | i++) | |
1324 | { | |
1325 | internal_ptr++; | |
1326 | raw_src += symesz; | |
1327 | ||
9783e04a | 1328 | internal_ptr->fix_value = 0; |
075caafd ILT |
1329 | internal_ptr->fix_tag = 0; |
1330 | internal_ptr->fix_end = 0; | |
9783e04a | 1331 | internal_ptr->fix_scnlen = 0; |
075caafd ILT |
1332 | bfd_coff_swap_aux_in(abfd, (PTR) raw_src, |
1333 | symbol_ptr->u.syment.n_type, | |
1334 | symbol_ptr->u.syment.n_sclass, | |
330595d0 | 1335 | i, symbol_ptr->u.syment.n_numaux, |
075caafd ILT |
1336 | &(internal_ptr->u.auxent)); |
1337 | /* Remember that bal entries arn't pointerized */ | |
1338 | if (i != 1 || symbol_ptr->u.syment.n_sclass != C_LEAFPROC) | |
1339 | { | |
1340 | ||
1341 | coff_pointerize_aux(abfd, | |
1342 | internal, | |
1343 | symbol_ptr->u.syment.n_type, | |
1344 | symbol_ptr->u.syment.n_sclass, | |
1345 | internal_ptr); | |
1346 | } | |
1347 | ||
1348 | } | |
1349 | } | |
1350 | ||
1351 | /* Free all the raw stuff */ | |
1352 | bfd_release(abfd, raw); | |
1353 | ||
1354 | for (internal_ptr = internal; internal_ptr < internal_end; | |
1355 | internal_ptr ++) | |
1356 | { | |
a74d1517 ILT |
1357 | if (internal_ptr->u.syment.n_sclass == C_FILE |
1358 | && internal_ptr->u.syment.n_numaux > 0) { | |
075caafd ILT |
1359 | /* make a file symbol point to the name in the auxent, since |
1360 | the text ".file" is redundant */ | |
1361 | if ((internal_ptr+1)->u.auxent.x_file.x_n.x_zeroes == 0) { | |
1362 | /* the filename is a long one, point into the string table */ | |
1363 | if (string_table == NULL) { | |
1364 | string_table = build_string_table(abfd); | |
1365 | } | |
1366 | ||
1367 | internal_ptr->u.syment._n._n_n._n_offset = | |
9fbe895a | 1368 | (long) (string_table - STRING_SIZE_SIZE + |
075caafd ILT |
1369 | (internal_ptr+1)->u.auxent.x_file.x_n.x_offset); |
1370 | } | |
1371 | else { | |
1372 | /* ordinary short filename, put into memory anyway */ | |
e4b6b3e7 | 1373 | internal_ptr->u.syment._n._n_n._n_offset = (long) |
075caafd ILT |
1374 | copy_name(abfd, (internal_ptr+1)->u.auxent.x_file.x_fname, |
1375 | FILNMLEN); | |
1376 | } | |
1377 | } | |
1378 | else { | |
1379 | if (internal_ptr->u.syment._n._n_n._n_zeroes != 0) { | |
1380 | /* This is a "short" name. Make it long. */ | |
1381 | unsigned long i = 0; | |
1382 | char *newstring = NULL; | |
1383 | ||
1384 | /* find the length of this string without walking into memory | |
1385 | that isn't ours. */ | |
1386 | for (i = 0; i < 8; ++i) { | |
1387 | if (internal_ptr->u.syment._n._n_name[i] == '\0') { | |
1388 | break; | |
1389 | } /* if end of string */ | |
1390 | } /* possible lengths of this string. */ | |
1391 | ||
1392 | if ((newstring = (PTR) bfd_alloc(abfd, ++i)) == NULL) { | |
d1ad85a6 | 1393 | bfd_set_error (bfd_error_no_memory); |
075caafd ILT |
1394 | return (NULL); |
1395 | } /* on error */ | |
1396 | memset(newstring, 0, i); | |
1397 | strncpy(newstring, internal_ptr->u.syment._n._n_name, i-1); | |
e4b6b3e7 | 1398 | internal_ptr->u.syment._n._n_n._n_offset = (long int) newstring; |
075caafd ILT |
1399 | internal_ptr->u.syment._n._n_n._n_zeroes = 0; |
1400 | } | |
9783e04a DM |
1401 | else if (internal_ptr->u.syment._n._n_n._n_offset == 0) |
1402 | internal_ptr->u.syment._n._n_n._n_offset = (long int) ""; | |
075caafd ILT |
1403 | else if (!bfd_coff_symname_in_debug(abfd, &internal_ptr->u.syment)) { |
1404 | /* Long name already. Point symbol at the string in the table. */ | |
1405 | if (string_table == NULL) { | |
1406 | string_table = build_string_table(abfd); | |
1407 | } | |
e4b6b3e7 | 1408 | internal_ptr->u.syment._n._n_n._n_offset = (long int) |
9fbe895a ILT |
1409 | (string_table |
1410 | - STRING_SIZE_SIZE | |
1411 | + internal_ptr->u.syment._n._n_n._n_offset); | |
075caafd ILT |
1412 | } |
1413 | else { | |
1414 | /* Long name in debug section. Very similar. */ | |
1415 | if (debug_section == NULL) { | |
1416 | debug_section = build_debug_section(abfd); | |
1417 | } | |
e4b6b3e7 | 1418 | internal_ptr->u.syment._n._n_n._n_offset = (long int) |
075caafd ILT |
1419 | (debug_section + internal_ptr->u.syment._n._n_n._n_offset); |
1420 | } | |
1421 | } | |
1422 | internal_ptr += internal_ptr->u.syment.n_numaux; | |
1423 | } | |
1424 | ||
1425 | obj_raw_syments(abfd) = internal; | |
85fe7cff | 1426 | obj_raw_syment_count(abfd) = internal_ptr - internal; |
075caafd ILT |
1427 | |
1428 | return (internal); | |
1429 | } /* coff_get_normalized_symtab() */ | |
1430 | ||
326e32d7 | 1431 | long |
25057836 JL |
1432 | coff_get_reloc_upper_bound (abfd, asect) |
1433 | bfd *abfd; | |
1434 | sec_ptr asect; | |
075caafd ILT |
1435 | { |
1436 | if (bfd_get_format(abfd) != bfd_object) { | |
d1ad85a6 | 1437 | bfd_set_error (bfd_error_invalid_operation); |
326e32d7 | 1438 | return -1; |
075caafd ILT |
1439 | } |
1440 | return (asect->reloc_count + 1) * sizeof(arelent *); | |
1441 | } | |
1442 | ||
1443 | asymbol * | |
25057836 JL |
1444 | coff_make_empty_symbol (abfd) |
1445 | bfd *abfd; | |
075caafd ILT |
1446 | { |
1447 | coff_symbol_type *new = (coff_symbol_type *) bfd_alloc(abfd, sizeof(coff_symbol_type)); | |
1448 | if (new == NULL) { | |
d1ad85a6 | 1449 | bfd_set_error (bfd_error_no_memory); |
075caafd ILT |
1450 | return (NULL); |
1451 | } /* on error */ | |
9783e04a | 1452 | memset (new, 0, sizeof *new); |
075caafd ILT |
1453 | new->symbol.section = 0; |
1454 | new->native = 0; | |
1455 | new->lineno = (alent *) NULL; | |
1456 | new->done_lineno = false; | |
1457 | new->symbol.the_bfd = abfd; | |
1458 | return &new->symbol; | |
1459 | } | |
1460 | ||
2d1e6c9c KR |
1461 | /* Make a debugging symbol. */ |
1462 | ||
075caafd | 1463 | asymbol * |
2d1e6c9c KR |
1464 | coff_bfd_make_debug_symbol (abfd, ptr, sz) |
1465 | bfd *abfd; | |
1466 | PTR ptr; | |
1467 | unsigned long sz; | |
075caafd ILT |
1468 | { |
1469 | coff_symbol_type *new = (coff_symbol_type *) bfd_alloc(abfd, sizeof(coff_symbol_type)); | |
1470 | if (new == NULL) { | |
d1ad85a6 | 1471 | bfd_set_error (bfd_error_no_memory); |
075caafd ILT |
1472 | return (NULL); |
1473 | } /* on error */ | |
1474 | /* @@ This shouldn't be using a constant multiplier. */ | |
1475 | new->native = (combined_entry_type *) bfd_zalloc (abfd, sizeof (combined_entry_type) * 10); | |
9783e04a DM |
1476 | if (!new->native) |
1477 | { | |
d1ad85a6 | 1478 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
1479 | return (NULL); |
1480 | } /* on error */ | |
075caafd ILT |
1481 | new->symbol.section = &bfd_debug_section; |
1482 | new->lineno = (alent *) NULL; | |
1483 | new->done_lineno = false; | |
1484 | new->symbol.the_bfd = abfd; | |
1485 | return &new->symbol; | |
1486 | } | |
1487 | ||
330595d0 | 1488 | /*ARGSUSED*/ |
2d1e6c9c KR |
1489 | void |
1490 | coff_get_symbol_info (abfd, symbol, ret) | |
1491 | bfd *abfd; | |
1492 | asymbol *symbol; | |
1493 | symbol_info *ret; | |
1494 | { | |
1495 | bfd_symbol_info (symbol, ret); | |
1496 | } | |
1497 | ||
e61cfdf8 ILT |
1498 | /* Print out information about COFF symbol. */ |
1499 | ||
075caafd | 1500 | void |
e61cfdf8 ILT |
1501 | coff_print_symbol (abfd, filep, symbol, how) |
1502 | bfd *abfd; | |
1503 | PTR filep; | |
1504 | asymbol *symbol; | |
1505 | bfd_print_symbol_type how; | |
075caafd | 1506 | { |
e61cfdf8 ILT |
1507 | FILE *file = (FILE *) filep; |
1508 | ||
1509 | switch (how) | |
1510 | { | |
075caafd | 1511 | case bfd_print_symbol_name: |
e61cfdf8 | 1512 | fprintf (file, "%s", symbol->name); |
075caafd | 1513 | break; |
e61cfdf8 | 1514 | |
075caafd | 1515 | case bfd_print_symbol_more: |
e61cfdf8 ILT |
1516 | fprintf (file, "coff %s %s", |
1517 | coffsymbol(symbol)->native ? "n" : "g", | |
1518 | coffsymbol(symbol)->lineno ? "l" : " "); | |
075caafd | 1519 | break; |
075caafd | 1520 | |
e61cfdf8 | 1521 | case bfd_print_symbol_all: |
075caafd | 1522 | if (coffsymbol(symbol)->native) |
075caafd | 1523 | { |
e61cfdf8 ILT |
1524 | unsigned int aux; |
1525 | combined_entry_type *combined = coffsymbol (symbol)->native; | |
1526 | combined_entry_type *root = obj_raw_syments (abfd); | |
1527 | struct lineno_cache_entry *l = coffsymbol(symbol)->lineno; | |
1528 | ||
5a53b1c1 | 1529 | fprintf (file,"[%3ld]", (long) (combined - root)); |
e61cfdf8 ILT |
1530 | |
1531 | fprintf (file, | |
4c3721d5 | 1532 | "(sc %2d)(fl 0x%02x)(ty %3x)(sc %3d) (nx %d) 0x%08lx %s", |
e61cfdf8 ILT |
1533 | combined->u.syment.n_scnum, |
1534 | combined->u.syment.n_flags, | |
1535 | combined->u.syment.n_type, | |
1536 | combined->u.syment.n_sclass, | |
1537 | combined->u.syment.n_numaux, | |
4c3721d5 | 1538 | (unsigned long) combined->u.syment.n_value, |
e61cfdf8 ILT |
1539 | symbol->name); |
1540 | ||
1541 | for (aux = 0; aux < combined->u.syment.n_numaux; aux++) | |
1542 | { | |
1543 | combined_entry_type *auxp = combined + aux + 1; | |
1544 | long tagndx; | |
1545 | ||
1546 | if (auxp->fix_tag) | |
1547 | tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root; | |
1548 | else | |
1549 | tagndx = auxp->u.auxent.x_sym.x_tagndx.l; | |
1550 | ||
1551 | fprintf (file, "\n"); | |
1552 | switch (combined->u.syment.n_sclass) | |
1553 | { | |
1554 | case C_FILE: | |
1555 | fprintf (file, "File "); | |
1556 | break; | |
1557 | default: | |
1558 | ||
4c3721d5 | 1559 | fprintf (file, "AUX lnno %d size 0x%x tagndx %ld", |
e61cfdf8 ILT |
1560 | auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno, |
1561 | auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size, | |
1562 | tagndx); | |
1563 | break; | |
1564 | } | |
075caafd | 1565 | } |
075caafd | 1566 | |
e61cfdf8 ILT |
1567 | if (l) |
1568 | { | |
e4b6b3e7 | 1569 | fprintf (file, "\n%s :", l->u.sym->name); |
e61cfdf8 ILT |
1570 | l++; |
1571 | while (l->line_number) | |
1572 | { | |
4c3721d5 ILT |
1573 | fprintf (file, "\n%4d : 0x%lx", |
1574 | l->line_number, | |
1575 | ((unsigned long) | |
1576 | (l->u.offset + symbol->section->vma))); | |
e61cfdf8 ILT |
1577 | l++; |
1578 | } | |
1579 | } | |
1580 | } | |
1581 | else | |
075caafd | 1582 | { |
e61cfdf8 ILT |
1583 | bfd_print_symbol_vandf ((PTR) file, symbol); |
1584 | fprintf (file, " %-5s %s %s %s", | |
1585 | symbol->section->name, | |
1586 | coffsymbol(symbol)->native ? "n" : "g", | |
1587 | coffsymbol(symbol)->lineno ? "l" : " ", | |
1588 | symbol->name); | |
075caafd | 1589 | } |
075caafd ILT |
1590 | } |
1591 | } | |
1592 | ||
1593 | /* Provided a BFD, a section and an offset into the section, calculate | |
1594 | and return the name of the source file and the line nearest to the | |
1595 | wanted location. */ | |
1596 | ||
330595d0 | 1597 | /*ARGSUSED*/ |
075caafd | 1598 | boolean |
25057836 JL |
1599 | coff_find_nearest_line (abfd, section, ignore_symbols, offset, filename_ptr, |
1600 | functionname_ptr, line_ptr) | |
1601 | bfd *abfd; | |
1602 | asection *section; | |
1603 | asymbol **ignore_symbols; | |
1604 | bfd_vma offset; | |
1605 | CONST char **filename_ptr; | |
1606 | CONST char **functionname_ptr; | |
1607 | unsigned int *line_ptr; | |
075caafd ILT |
1608 | { |
1609 | static bfd *cache_abfd; | |
1610 | static asection *cache_section; | |
1611 | static bfd_vma cache_offset; | |
1612 | static unsigned int cache_i; | |
85fe7cff PB |
1613 | static CONST char *cache_function; |
1614 | static unsigned int line_base = 0; | |
075caafd | 1615 | |
a74d1517 | 1616 | unsigned int i; |
075caafd ILT |
1617 | coff_data_type *cof = coff_data(abfd); |
1618 | /* Run through the raw syments if available */ | |
1619 | combined_entry_type *p; | |
a74d1517 | 1620 | combined_entry_type *pend; |
075caafd | 1621 | alent *l; |
075caafd ILT |
1622 | |
1623 | ||
1624 | *filename_ptr = 0; | |
1625 | *functionname_ptr = 0; | |
1626 | *line_ptr = 0; | |
1627 | ||
1628 | /* Don't try and find line numbers in a non coff file */ | |
1629 | if (abfd->xvec->flavour != bfd_target_coff_flavour) | |
1630 | return false; | |
1631 | ||
1632 | if (cof == NULL) | |
1633 | return false; | |
1634 | ||
a74d1517 | 1635 | /* Find the first C_FILE symbol. */ |
075caafd | 1636 | p = cof->raw_syments; |
a74d1517 ILT |
1637 | pend = p + cof->raw_syment_count; |
1638 | while (p < pend) | |
1639 | { | |
1640 | if (p->u.syment.n_sclass == C_FILE) | |
1641 | break; | |
1642 | p += 1 + p->u.syment.n_numaux; | |
1643 | } | |
075caafd | 1644 | |
a74d1517 ILT |
1645 | if (p < pend) |
1646 | { | |
6d04c6d4 ILT |
1647 | bfd_vma maxdiff; |
1648 | ||
a74d1517 | 1649 | /* Look through the C_FILE symbols to find the best one. */ |
075caafd | 1650 | *filename_ptr = (char *) p->u.syment._n._n_n._n_offset; |
6d04c6d4 | 1651 | maxdiff = (bfd_vma) 0 - (bfd_vma) 1; |
a74d1517 ILT |
1652 | while (1) |
1653 | { | |
1654 | combined_entry_type *p2; | |
1655 | ||
6d04c6d4 ILT |
1656 | for (p2 = p + 1 + p->u.syment.n_numaux; |
1657 | p2 < pend; | |
1658 | p2 += 1 + p2->u.syment.n_numaux) | |
a74d1517 | 1659 | { |
6d04c6d4 ILT |
1660 | if (p2->u.syment.n_scnum > 0 |
1661 | && (section | |
1662 | == coff_section_from_bfd_index (abfd, | |
1663 | p2->u.syment.n_scnum))) | |
a74d1517 | 1664 | break; |
6d04c6d4 ILT |
1665 | if (p2->u.syment.n_sclass == C_FILE) |
1666 | { | |
1667 | p2 = pend; | |
1668 | break; | |
1669 | } | |
a74d1517 | 1670 | } |
6d04c6d4 | 1671 | |
a74d1517 | 1672 | if (p2 < pend |
6d04c6d4 ILT |
1673 | && offset >= p2->u.syment.n_value |
1674 | && offset - p2->u.syment.n_value < maxdiff) | |
1675 | { | |
1676 | *filename_ptr = (char *) p->u.syment._n._n_n._n_offset; | |
1677 | maxdiff = offset - p2->u.syment.n_value; | |
1678 | } | |
1679 | ||
1680 | /* Avoid endless loops on erroneous files by ensuring that | |
1681 | we always move forward in the file. */ | |
1682 | if (p - cof->raw_syments >= p->u.syment.n_value) | |
a74d1517 ILT |
1683 | break; |
1684 | ||
6d04c6d4 ILT |
1685 | p = cof->raw_syments + p->u.syment.n_value; |
1686 | if (p > pend || p->u.syment.n_sclass != C_FILE) | |
1687 | break; | |
a74d1517 | 1688 | } |
075caafd | 1689 | } |
a74d1517 | 1690 | |
075caafd ILT |
1691 | /* Now wander though the raw linenumbers of the section */ |
1692 | /* | |
1693 | If this is the same BFD as we were previously called with and this is | |
1694 | the same section, and the offset we want is further down then we can | |
1695 | prime the lookup loop | |
1696 | */ | |
1697 | if (abfd == cache_abfd && | |
1698 | section == cache_section && | |
1699 | offset >= cache_offset) { | |
1700 | i = cache_i; | |
85fe7cff | 1701 | *functionname_ptr = cache_function; |
075caafd ILT |
1702 | } |
1703 | else { | |
1704 | i = 0; | |
075caafd | 1705 | } |
85fe7cff | 1706 | l = §ion->lineno[i]; |
075caafd ILT |
1707 | |
1708 | for (; i < section->lineno_count; i++) { | |
1709 | if (l->line_number == 0) { | |
1710 | /* Get the symbol this line number points at */ | |
1711 | coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym); | |
85fe7cff PB |
1712 | if (coff->symbol.value > offset) |
1713 | break; | |
075caafd ILT |
1714 | *functionname_ptr = coff->symbol.name; |
1715 | if (coff->native) { | |
1716 | combined_entry_type *s = coff->native; | |
1717 | s = s + 1 + s->u.syment.n_numaux; | |
a74d1517 ILT |
1718 | |
1719 | /* In XCOFF a debugging symbol can follow the function | |
1720 | symbol. */ | |
1721 | if (s->u.syment.n_scnum == N_DEBUG) | |
1722 | s = s + 1 + s->u.syment.n_numaux; | |
1723 | ||
075caafd ILT |
1724 | /* |
1725 | S should now point to the .bf of the function | |
1726 | */ | |
1727 | if (s->u.syment.n_numaux) { | |
1728 | /* | |
1729 | The linenumber is stored in the auxent | |
1730 | */ | |
1731 | union internal_auxent *a = &((s + 1)->u.auxent); | |
1732 | line_base = a->x_sym.x_misc.x_lnsz.x_lnno; | |
85fe7cff | 1733 | *line_ptr = line_base; |
075caafd ILT |
1734 | } |
1735 | } | |
1736 | } | |
1737 | else { | |
a74d1517 | 1738 | if (l->u.offset + bfd_get_section_vma (abfd, section) > offset) |
075caafd | 1739 | break; |
85fe7cff | 1740 | *line_ptr = l->line_number + line_base - 1; |
075caafd ILT |
1741 | } |
1742 | l++; | |
1743 | } | |
1744 | ||
1745 | cache_abfd = abfd; | |
1746 | cache_section = section; | |
1747 | cache_offset = offset; | |
1748 | cache_i = i; | |
85fe7cff | 1749 | cache_function = *functionname_ptr; |
075caafd ILT |
1750 | |
1751 | return true; | |
1752 | } | |
1753 | ||
1754 | int | |
25057836 JL |
1755 | coff_sizeof_headers (abfd, reloc) |
1756 | bfd *abfd; | |
1757 | boolean reloc; | |
075caafd ILT |
1758 | { |
1759 | size_t size; | |
1760 | ||
1761 | if (reloc == false) { | |
1762 | size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd); | |
1763 | } | |
1764 | else { | |
1765 | size = bfd_coff_filhsz (abfd); | |
1766 | } | |
1767 | ||
1768 | size += abfd->section_count * bfd_coff_scnhsz (abfd); | |
1769 | return size; | |
1770 | } |