]>
Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* Support for the generic parts of COFF, for BFD. |
7898deda NC |
2 | Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
3 | 2000, 2001 | |
252b5132 RH |
4 | Free Software Foundation, Inc. |
5 | Written by Cygnus Support. | |
6 | ||
7 | This file is part of BFD, the Binary File Descriptor library. | |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation; either version 2 of the License, or | |
12 | (at your option) any later version. | |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
20 | along with this program; if not, write to the Free Software | |
21 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
22 | ||
23 | /* Most of this hacked by Steve Chamberlain, [email protected]. | |
24 | Split out of coffcode.h by Ian Taylor, [email protected]. */ | |
25 | ||
26 | /* This file contains COFF code that is not dependent on any | |
27 | particular COFF target. There is only one version of this file in | |
28 | libbfd.a, so no target specific code may be put in here. Or, to | |
29 | put it another way, | |
30 | ||
31 | ********** DO NOT PUT TARGET SPECIFIC CODE IN THIS FILE ********** | |
32 | ||
33 | If you need to add some target specific behaviour, add a new hook | |
34 | function to bfd_coff_backend_data. | |
35 | ||
36 | Some of these functions are also called by the ECOFF routines. | |
37 | Those functions may not use any COFF specific information, such as | |
38 | coff_data (abfd). */ | |
39 | ||
40 | #include "bfd.h" | |
41 | #include "sysdep.h" | |
42 | #include "libbfd.h" | |
43 | #include "coff/internal.h" | |
44 | #include "libcoff.h" | |
45 | ||
46 | static void coff_fix_symbol_name | |
47 | PARAMS ((bfd *, asymbol *, combined_entry_type *, bfd_size_type *, | |
48 | asection **, bfd_size_type *)); | |
49 | static boolean coff_write_symbol | |
50 | PARAMS ((bfd *, asymbol *, combined_entry_type *, unsigned int *, | |
51 | bfd_size_type *, asection **, bfd_size_type *)); | |
52 | static boolean coff_write_alien_symbol | |
53 | PARAMS ((bfd *, asymbol *, unsigned int *, bfd_size_type *, | |
54 | asection **, bfd_size_type *)); | |
55 | static boolean coff_write_native_symbol | |
56 | PARAMS ((bfd *, coff_symbol_type *, unsigned int *, bfd_size_type *, | |
57 | asection **, bfd_size_type *)); | |
58 | static void coff_pointerize_aux | |
59 | PARAMS ((bfd *, combined_entry_type *, combined_entry_type *, | |
60 | unsigned int, combined_entry_type *)); | |
61 | static boolean make_a_section_from_file | |
62 | PARAMS ((bfd *, struct internal_scnhdr *, unsigned int)); | |
63 | static const bfd_target *coff_real_object_p | |
244148ad | 64 | PARAMS ((bfd *, unsigned, struct internal_filehdr *, |
252b5132 RH |
65 | struct internal_aouthdr *)); |
66 | static void fixup_symbol_value | |
67 | PARAMS ((bfd *, coff_symbol_type *, struct internal_syment *)); | |
68 | static char *build_debug_section | |
69 | PARAMS ((bfd *)); | |
70 | static char *copy_name | |
71 | PARAMS ((bfd *, char *, int)); | |
72 | ||
73 | #define STRING_SIZE_SIZE (4) | |
74 | ||
75 | /* Take a section header read from a coff file (in HOST byte order), | |
76 | and make a BFD "section" out of it. This is used by ECOFF. */ | |
77 | static boolean | |
78 | make_a_section_from_file (abfd, hdr, target_index) | |
79 | bfd *abfd; | |
80 | struct internal_scnhdr *hdr; | |
81 | unsigned int target_index; | |
82 | { | |
83 | asection *return_section; | |
84 | char *name; | |
85 | ||
86 | name = NULL; | |
87 | ||
88 | /* Handle long section names as in PE. */ | |
89 | if (bfd_coff_long_section_names (abfd) | |
90 | && hdr->s_name[0] == '/') | |
91 | { | |
92 | char buf[SCNNMLEN]; | |
93 | long strindex; | |
94 | char *p; | |
95 | const char *strings; | |
96 | ||
97 | memcpy (buf, hdr->s_name + 1, SCNNMLEN - 1); | |
98 | buf[SCNNMLEN - 1] = '\0'; | |
99 | strindex = strtol (buf, &p, 10); | |
100 | if (*p == '\0' && strindex >= 0) | |
101 | { | |
102 | strings = _bfd_coff_read_string_table (abfd); | |
103 | if (strings == NULL) | |
104 | return false; | |
105 | /* FIXME: For extra safety, we should make sure that | |
106 | strindex does not run us past the end, but right now we | |
107 | don't know the length of the string table. */ | |
108 | strings += strindex; | |
109 | name = bfd_alloc (abfd, strlen (strings) + 1); | |
110 | if (name == NULL) | |
111 | return false; | |
112 | strcpy (name, strings); | |
113 | } | |
114 | } | |
115 | ||
116 | if (name == NULL) | |
117 | { | |
118 | /* Assorted wastage to null-terminate the name, thanks AT&T! */ | |
119 | name = bfd_alloc (abfd, sizeof (hdr->s_name) + 1); | |
120 | if (name == NULL) | |
121 | return false; | |
122 | strncpy (name, (char *) &hdr->s_name[0], sizeof (hdr->s_name)); | |
123 | name[sizeof (hdr->s_name)] = 0; | |
124 | } | |
125 | ||
126 | return_section = bfd_make_section_anyway (abfd, name); | |
127 | if (return_section == NULL) | |
128 | return false; | |
129 | ||
130 | return_section->vma = hdr->s_vaddr; | |
131 | return_section->lma = hdr->s_paddr; | |
132 | return_section->_raw_size = hdr->s_size; | |
133 | return_section->filepos = hdr->s_scnptr; | |
134 | return_section->rel_filepos = hdr->s_relptr; | |
135 | return_section->reloc_count = hdr->s_nreloc; | |
136 | ||
137 | bfd_coff_set_alignment_hook (abfd, return_section, hdr); | |
138 | ||
139 | return_section->line_filepos = hdr->s_lnnoptr; | |
140 | ||
141 | return_section->lineno_count = hdr->s_nlnno; | |
142 | return_section->userdata = NULL; | |
143 | return_section->next = (asection *) NULL; | |
252b5132 | 144 | return_section->target_index = target_index; |
41733515 ILT |
145 | return_section->flags = bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name, |
146 | return_section); | |
252b5132 RH |
147 | |
148 | /* At least on i386-coff, the line number count for a shared library | |
149 | section must be ignored. */ | |
150 | if ((return_section->flags & SEC_COFF_SHARED_LIBRARY) != 0) | |
151 | return_section->lineno_count = 0; | |
152 | ||
153 | if (hdr->s_nreloc != 0) | |
154 | return_section->flags |= SEC_RELOC; | |
155 | /* FIXME: should this check 'hdr->s_size > 0' */ | |
156 | if (hdr->s_scnptr != 0) | |
157 | return_section->flags |= SEC_HAS_CONTENTS; | |
158 | return true; | |
159 | } | |
160 | ||
161 | /* Read in a COFF object and make it into a BFD. This is used by | |
162 | ECOFF as well. */ | |
163 | ||
164 | static const bfd_target * | |
165 | coff_real_object_p (abfd, nscns, internal_f, internal_a) | |
166 | bfd *abfd; | |
167 | unsigned nscns; | |
168 | struct internal_filehdr *internal_f; | |
169 | struct internal_aouthdr *internal_a; | |
170 | { | |
171 | flagword oflags = abfd->flags; | |
172 | bfd_vma ostart = bfd_get_start_address (abfd); | |
173 | PTR tdata; | |
174 | size_t readsize; /* length of file_info */ | |
175 | unsigned int scnhsz; | |
176 | char *external_sections; | |
177 | ||
178 | if (!(internal_f->f_flags & F_RELFLG)) | |
179 | abfd->flags |= HAS_RELOC; | |
180 | if ((internal_f->f_flags & F_EXEC)) | |
181 | abfd->flags |= EXEC_P; | |
182 | if (!(internal_f->f_flags & F_LNNO)) | |
183 | abfd->flags |= HAS_LINENO; | |
184 | if (!(internal_f->f_flags & F_LSYMS)) | |
185 | abfd->flags |= HAS_LOCALS; | |
186 | ||
187 | /* FIXME: How can we set D_PAGED correctly? */ | |
188 | if ((internal_f->f_flags & F_EXEC) != 0) | |
189 | abfd->flags |= D_PAGED; | |
190 | ||
191 | bfd_get_symcount (abfd) = internal_f->f_nsyms; | |
192 | if (internal_f->f_nsyms) | |
193 | abfd->flags |= HAS_SYMS; | |
194 | ||
195 | if (internal_a != (struct internal_aouthdr *) NULL) | |
196 | bfd_get_start_address (abfd) = internal_a->entry; | |
197 | else | |
198 | bfd_get_start_address (abfd) = 0; | |
199 | ||
200 | /* Set up the tdata area. ECOFF uses its own routine, and overrides | |
201 | abfd->flags. */ | |
202 | tdata = bfd_coff_mkobject_hook (abfd, (PTR) internal_f, (PTR) internal_a); | |
203 | if (tdata == NULL) | |
204 | return 0; | |
205 | ||
206 | scnhsz = bfd_coff_scnhsz (abfd); | |
207 | readsize = nscns * scnhsz; | |
208 | external_sections = (char *) bfd_alloc (abfd, readsize); | |
209 | if (!external_sections) | |
210 | goto fail; | |
211 | ||
212 | if (bfd_read ((PTR) external_sections, 1, readsize, abfd) != readsize) | |
213 | goto fail; | |
214 | ||
363d7b14 | 215 | /* Set the arch/mach *before* swapping in sections; section header swapping |
244148ad | 216 | may depend on arch/mach info. */ |
363d7b14 TW |
217 | if (bfd_coff_set_arch_mach_hook (abfd, (PTR) internal_f) == false) |
218 | goto fail; | |
219 | ||
252b5132 RH |
220 | /* Now copy data as required; construct all asections etc */ |
221 | if (nscns != 0) | |
222 | { | |
223 | unsigned int i; | |
224 | for (i = 0; i < nscns; i++) | |
225 | { | |
226 | struct internal_scnhdr tmp; | |
227 | bfd_coff_swap_scnhdr_in (abfd, | |
228 | (PTR) (external_sections + i * scnhsz), | |
229 | (PTR) & tmp); | |
230 | if (! make_a_section_from_file (abfd, &tmp, i + 1)) | |
231 | goto fail; | |
232 | } | |
233 | } | |
234 | ||
235 | /* make_abs_section (abfd); */ | |
236 | ||
252b5132 RH |
237 | return abfd->xvec; |
238 | ||
239 | fail: | |
240 | bfd_release (abfd, tdata); | |
241 | abfd->flags = oflags; | |
242 | bfd_get_start_address (abfd) = ostart; | |
243 | return (const bfd_target *) NULL; | |
244 | } | |
245 | ||
246 | /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is | |
247 | not a COFF file. This is also used by ECOFF. */ | |
248 | ||
249 | const bfd_target * | |
250 | coff_object_p (abfd) | |
251 | bfd *abfd; | |
252 | { | |
253 | unsigned int filhsz; | |
254 | unsigned int aoutsz; | |
255 | int nscns; | |
256 | PTR filehdr; | |
257 | struct internal_filehdr internal_f; | |
258 | struct internal_aouthdr internal_a; | |
259 | ||
260 | /* figure out how much to read */ | |
261 | filhsz = bfd_coff_filhsz (abfd); | |
262 | aoutsz = bfd_coff_aoutsz (abfd); | |
263 | ||
264 | filehdr = bfd_alloc (abfd, filhsz); | |
265 | if (filehdr == NULL) | |
266 | return 0; | |
267 | if (bfd_read (filehdr, 1, filhsz, abfd) != filhsz) | |
268 | { | |
269 | if (bfd_get_error () != bfd_error_system_call) | |
270 | bfd_set_error (bfd_error_wrong_format); | |
271 | return 0; | |
272 | } | |
273 | bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f); | |
274 | bfd_release (abfd, filehdr); | |
275 | ||
276 | if (bfd_coff_bad_format_hook (abfd, &internal_f) == false) | |
277 | { | |
278 | bfd_set_error (bfd_error_wrong_format); | |
279 | return 0; | |
280 | } | |
281 | nscns = internal_f.f_nscns; | |
282 | ||
283 | if (internal_f.f_opthdr) | |
284 | { | |
285 | PTR opthdr; | |
286 | ||
287 | opthdr = bfd_alloc (abfd, aoutsz); | |
288 | if (opthdr == NULL) | |
289 | return 0;; | |
290 | if (bfd_read (opthdr, 1, internal_f.f_opthdr, abfd) | |
291 | != internal_f.f_opthdr) | |
292 | { | |
293 | return 0; | |
294 | } | |
295 | bfd_coff_swap_aouthdr_in (abfd, opthdr, (PTR) &internal_a); | |
296 | } | |
297 | ||
298 | return coff_real_object_p (abfd, nscns, &internal_f, | |
299 | (internal_f.f_opthdr != 0 | |
300 | ? &internal_a | |
301 | : (struct internal_aouthdr *) NULL)); | |
302 | } | |
303 | ||
304 | /* Get the BFD section from a COFF symbol section number. */ | |
305 | ||
306 | asection * | |
307 | coff_section_from_bfd_index (abfd, index) | |
308 | bfd *abfd; | |
309 | int index; | |
310 | { | |
311 | struct sec *answer = abfd->sections; | |
312 | ||
313 | if (index == N_ABS) | |
314 | return bfd_abs_section_ptr; | |
315 | if (index == N_UNDEF) | |
316 | return bfd_und_section_ptr; | |
317 | if (index == N_DEBUG) | |
318 | return bfd_abs_section_ptr; | |
319 | ||
320 | while (answer) | |
321 | { | |
322 | if (answer->target_index == index) | |
323 | return answer; | |
324 | answer = answer->next; | |
325 | } | |
326 | ||
327 | /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a | |
328 | has a bad symbol table in biglitpow.o. */ | |
329 | return bfd_und_section_ptr; | |
330 | } | |
331 | ||
332 | /* Get the upper bound of a COFF symbol table. */ | |
333 | ||
334 | long | |
335 | coff_get_symtab_upper_bound (abfd) | |
336 | bfd *abfd; | |
337 | { | |
338 | if (!bfd_coff_slurp_symbol_table (abfd)) | |
339 | return -1; | |
340 | ||
341 | return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *)); | |
342 | } | |
343 | ||
252b5132 RH |
344 | /* Canonicalize a COFF symbol table. */ |
345 | ||
346 | long | |
347 | coff_get_symtab (abfd, alocation) | |
348 | bfd *abfd; | |
349 | asymbol **alocation; | |
350 | { | |
351 | unsigned int counter; | |
352 | coff_symbol_type *symbase; | |
353 | coff_symbol_type **location = (coff_symbol_type **) alocation; | |
354 | ||
355 | if (!bfd_coff_slurp_symbol_table (abfd)) | |
356 | return -1; | |
357 | ||
358 | symbase = obj_symbols (abfd); | |
359 | counter = bfd_get_symcount (abfd); | |
360 | while (counter-- > 0) | |
361 | *location++ = symbase++; | |
362 | ||
363 | *location = NULL; | |
364 | ||
365 | return bfd_get_symcount (abfd); | |
366 | } | |
367 | ||
368 | /* Get the name of a symbol. The caller must pass in a buffer of size | |
369 | >= SYMNMLEN + 1. */ | |
370 | ||
371 | const char * | |
372 | _bfd_coff_internal_syment_name (abfd, sym, buf) | |
373 | bfd *abfd; | |
374 | const struct internal_syment *sym; | |
375 | char *buf; | |
376 | { | |
377 | /* FIXME: It's not clear this will work correctly if sizeof | |
378 | (_n_zeroes) != 4. */ | |
379 | if (sym->_n._n_n._n_zeroes != 0 | |
380 | || sym->_n._n_n._n_offset == 0) | |
381 | { | |
382 | memcpy (buf, sym->_n._n_name, SYMNMLEN); | |
383 | buf[SYMNMLEN] = '\0'; | |
384 | return buf; | |
385 | } | |
386 | else | |
387 | { | |
388 | const char *strings; | |
389 | ||
390 | BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE); | |
391 | strings = obj_coff_strings (abfd); | |
392 | if (strings == NULL) | |
393 | { | |
394 | strings = _bfd_coff_read_string_table (abfd); | |
395 | if (strings == NULL) | |
396 | return NULL; | |
397 | } | |
398 | return strings + sym->_n._n_n._n_offset; | |
399 | } | |
400 | } | |
401 | ||
402 | /* Read in and swap the relocs. This returns a buffer holding the | |
403 | relocs for section SEC in file ABFD. If CACHE is true and | |
404 | INTERNAL_RELOCS is NULL, the relocs read in will be saved in case | |
405 | the function is called again. If EXTERNAL_RELOCS is not NULL, it | |
406 | is a buffer large enough to hold the unswapped relocs. If | |
407 | INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold | |
408 | the swapped relocs. If REQUIRE_INTERNAL is true, then the return | |
409 | value must be INTERNAL_RELOCS. The function returns NULL on error. */ | |
410 | ||
411 | struct internal_reloc * | |
412 | _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs, | |
413 | require_internal, internal_relocs) | |
414 | bfd *abfd; | |
415 | asection *sec; | |
416 | boolean cache; | |
417 | bfd_byte *external_relocs; | |
418 | boolean require_internal; | |
419 | struct internal_reloc *internal_relocs; | |
420 | { | |
421 | bfd_size_type relsz; | |
422 | bfd_byte *free_external = NULL; | |
423 | struct internal_reloc *free_internal = NULL; | |
424 | bfd_byte *erel; | |
425 | bfd_byte *erel_end; | |
426 | struct internal_reloc *irel; | |
427 | ||
428 | if (coff_section_data (abfd, sec) != NULL | |
429 | && coff_section_data (abfd, sec)->relocs != NULL) | |
430 | { | |
431 | if (! require_internal) | |
432 | return coff_section_data (abfd, sec)->relocs; | |
433 | memcpy (internal_relocs, coff_section_data (abfd, sec)->relocs, | |
434 | sec->reloc_count * sizeof (struct internal_reloc)); | |
435 | return internal_relocs; | |
436 | } | |
437 | ||
438 | relsz = bfd_coff_relsz (abfd); | |
439 | ||
440 | if (external_relocs == NULL) | |
441 | { | |
442 | free_external = (bfd_byte *) bfd_malloc (sec->reloc_count * relsz); | |
443 | if (free_external == NULL && sec->reloc_count > 0) | |
444 | goto error_return; | |
445 | external_relocs = free_external; | |
446 | } | |
447 | ||
448 | if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0 | |
449 | || (bfd_read (external_relocs, relsz, sec->reloc_count, abfd) | |
450 | != relsz * sec->reloc_count)) | |
451 | goto error_return; | |
452 | ||
453 | if (internal_relocs == NULL) | |
454 | { | |
455 | free_internal = ((struct internal_reloc *) | |
456 | bfd_malloc (sec->reloc_count | |
457 | * sizeof (struct internal_reloc))); | |
458 | if (free_internal == NULL && sec->reloc_count > 0) | |
459 | goto error_return; | |
460 | internal_relocs = free_internal; | |
461 | } | |
462 | ||
463 | /* Swap in the relocs. */ | |
464 | erel = external_relocs; | |
465 | erel_end = erel + relsz * sec->reloc_count; | |
466 | irel = internal_relocs; | |
467 | for (; erel < erel_end; erel += relsz, irel++) | |
468 | bfd_coff_swap_reloc_in (abfd, (PTR) erel, (PTR) irel); | |
469 | ||
470 | if (free_external != NULL) | |
471 | { | |
472 | free (free_external); | |
473 | free_external = NULL; | |
474 | } | |
475 | ||
476 | if (cache && free_internal != NULL) | |
477 | { | |
478 | if (coff_section_data (abfd, sec) == NULL) | |
479 | { | |
480 | sec->used_by_bfd = | |
481 | (PTR) bfd_zalloc (abfd, | |
482 | sizeof (struct coff_section_tdata)); | |
483 | if (sec->used_by_bfd == NULL) | |
484 | goto error_return; | |
485 | coff_section_data (abfd, sec)->contents = NULL; | |
486 | } | |
487 | coff_section_data (abfd, sec)->relocs = free_internal; | |
488 | } | |
489 | ||
490 | return internal_relocs; | |
491 | ||
492 | error_return: | |
493 | if (free_external != NULL) | |
494 | free (free_external); | |
495 | if (free_internal != NULL) | |
496 | free (free_internal); | |
497 | return NULL; | |
498 | } | |
499 | ||
500 | /* Set lineno_count for the output sections of a COFF file. */ | |
501 | ||
502 | int | |
503 | coff_count_linenumbers (abfd) | |
504 | bfd *abfd; | |
505 | { | |
506 | unsigned int limit = bfd_get_symcount (abfd); | |
507 | unsigned int i; | |
508 | int total = 0; | |
509 | asymbol **p; | |
510 | asection *s; | |
511 | ||
512 | if (limit == 0) | |
513 | { | |
514 | /* This may be from the backend linker, in which case the | |
515 | lineno_count in the sections is correct. */ | |
516 | for (s = abfd->sections; s != NULL; s = s->next) | |
517 | total += s->lineno_count; | |
518 | return total; | |
519 | } | |
520 | ||
521 | for (s = abfd->sections; s != NULL; s = s->next) | |
522 | BFD_ASSERT (s->lineno_count == 0); | |
523 | ||
524 | for (p = abfd->outsymbols, i = 0; i < limit; i++, p++) | |
525 | { | |
526 | asymbol *q_maybe = *p; | |
527 | ||
9bd09e22 | 528 | if (bfd_family_coff (bfd_asymbol_bfd (q_maybe))) |
252b5132 RH |
529 | { |
530 | coff_symbol_type *q = coffsymbol (q_maybe); | |
531 | ||
532 | /* The AIX 4.1 compiler can sometimes generate line numbers | |
533 | attached to debugging symbols. We try to simply ignore | |
534 | those here. */ | |
535 | if (q->lineno != NULL | |
536 | && q->symbol.section->owner != NULL) | |
537 | { | |
538 | /* This symbol has line numbers. Increment the owning | |
539 | section's linenumber count. */ | |
540 | alent *l = q->lineno; | |
541 | ||
542 | ++q->symbol.section->output_section->lineno_count; | |
543 | ++total; | |
544 | ++l; | |
545 | while (l->line_number != 0) | |
546 | { | |
547 | ++total; | |
548 | ++q->symbol.section->output_section->lineno_count; | |
549 | ++l; | |
550 | } | |
551 | } | |
552 | } | |
553 | } | |
554 | ||
555 | return total; | |
556 | } | |
557 | ||
558 | /* Takes a bfd and a symbol, returns a pointer to the coff specific | |
559 | area of the symbol if there is one. */ | |
560 | ||
252b5132 RH |
561 | coff_symbol_type * |
562 | coff_symbol_from (ignore_abfd, symbol) | |
7442e600 | 563 | bfd *ignore_abfd ATTRIBUTE_UNUSED; |
252b5132 RH |
564 | asymbol *symbol; |
565 | { | |
9bd09e22 | 566 | if (!bfd_family_coff (bfd_asymbol_bfd (symbol))) |
252b5132 RH |
567 | return (coff_symbol_type *) NULL; |
568 | ||
569 | if (bfd_asymbol_bfd (symbol)->tdata.coff_obj_data == (coff_data_type *) NULL) | |
570 | return (coff_symbol_type *) NULL; | |
571 | ||
572 | return (coff_symbol_type *) symbol; | |
573 | } | |
574 | ||
575 | static void | |
576 | fixup_symbol_value (abfd, coff_symbol_ptr, syment) | |
577 | bfd *abfd; | |
578 | coff_symbol_type *coff_symbol_ptr; | |
579 | struct internal_syment *syment; | |
580 | { | |
581 | ||
582 | /* Normalize the symbol flags */ | |
583 | if (bfd_is_com_section (coff_symbol_ptr->symbol.section)) | |
584 | { | |
585 | /* a common symbol is undefined with a value */ | |
586 | syment->n_scnum = N_UNDEF; | |
587 | syment->n_value = coff_symbol_ptr->symbol.value; | |
588 | } | |
703153b5 ILT |
589 | else if ((coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) != 0 |
590 | && (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING_RELOC) == 0) | |
252b5132 RH |
591 | { |
592 | syment->n_value = coff_symbol_ptr->symbol.value; | |
593 | } | |
594 | else if (bfd_is_und_section (coff_symbol_ptr->symbol.section)) | |
595 | { | |
596 | syment->n_scnum = N_UNDEF; | |
597 | syment->n_value = 0; | |
598 | } | |
7bb9db4d | 599 | /* FIXME: Do we need to handle the absolute section here? */ |
252b5132 RH |
600 | else |
601 | { | |
602 | if (coff_symbol_ptr->symbol.section) | |
603 | { | |
604 | syment->n_scnum = | |
605 | coff_symbol_ptr->symbol.section->output_section->target_index; | |
606 | ||
607 | syment->n_value = (coff_symbol_ptr->symbol.value | |
608 | + coff_symbol_ptr->symbol.section->output_offset); | |
609 | if (! obj_pe (abfd)) | |
34cbe64e TW |
610 | { |
611 | syment->n_value += (syment->n_sclass == C_STATLAB) | |
612 | ? coff_symbol_ptr->symbol.section->output_section->lma | |
613 | : coff_symbol_ptr->symbol.section->output_section->vma; | |
614 | } | |
252b5132 RH |
615 | } |
616 | else | |
617 | { | |
618 | BFD_ASSERT (0); | |
619 | /* This can happen, but I don't know why yet ([email protected]) */ | |
620 | syment->n_scnum = N_ABS; | |
621 | syment->n_value = coff_symbol_ptr->symbol.value; | |
622 | } | |
623 | } | |
624 | } | |
625 | ||
626 | /* Run through all the symbols in the symbol table and work out what | |
627 | their indexes into the symbol table will be when output. | |
628 | ||
629 | Coff requires that each C_FILE symbol points to the next one in the | |
630 | chain, and that the last one points to the first external symbol. We | |
631 | do that here too. */ | |
632 | ||
633 | boolean | |
634 | coff_renumber_symbols (bfd_ptr, first_undef) | |
635 | bfd *bfd_ptr; | |
636 | int *first_undef; | |
637 | { | |
638 | unsigned int symbol_count = bfd_get_symcount (bfd_ptr); | |
639 | asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols; | |
640 | unsigned int native_index = 0; | |
641 | struct internal_syment *last_file = (struct internal_syment *) NULL; | |
642 | unsigned int symbol_index; | |
643 | ||
644 | /* COFF demands that undefined symbols come after all other symbols. | |
645 | Since we don't need to impose this extra knowledge on all our | |
646 | client programs, deal with that here. Sort the symbol table; | |
647 | just move the undefined symbols to the end, leaving the rest | |
648 | alone. The O'Reilly book says that defined global symbols come | |
649 | at the end before the undefined symbols, so we do that here as | |
650 | well. */ | |
651 | /* @@ Do we have some condition we could test for, so we don't always | |
652 | have to do this? I don't think relocatability is quite right, but | |
653 | I'm not certain. [raeburn:19920508.1711EST] */ | |
654 | { | |
655 | asymbol **newsyms; | |
656 | unsigned int i; | |
657 | ||
658 | newsyms = (asymbol **) bfd_alloc (bfd_ptr, | |
659 | sizeof (asymbol *) * (symbol_count + 1)); | |
660 | if (!newsyms) | |
661 | return false; | |
662 | bfd_ptr->outsymbols = newsyms; | |
663 | for (i = 0; i < symbol_count; i++) | |
664 | if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0 | |
665 | || (!bfd_is_und_section (symbol_ptr_ptr[i]->section) | |
666 | && !bfd_is_com_section (symbol_ptr_ptr[i]->section) | |
667 | && ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) != 0 | |
668 | || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK)) | |
669 | == 0)))) | |
670 | *newsyms++ = symbol_ptr_ptr[i]; | |
671 | ||
672 | for (i = 0; i < symbol_count; i++) | |
673 | if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0 | |
674 | && !bfd_is_und_section (symbol_ptr_ptr[i]->section) | |
675 | && (bfd_is_com_section (symbol_ptr_ptr[i]->section) | |
676 | || ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) == 0 | |
677 | && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK)) | |
678 | != 0)))) | |
679 | *newsyms++ = symbol_ptr_ptr[i]; | |
680 | ||
681 | *first_undef = newsyms - bfd_ptr->outsymbols; | |
682 | ||
683 | for (i = 0; i < symbol_count; i++) | |
684 | if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0 | |
685 | && bfd_is_und_section (symbol_ptr_ptr[i]->section)) | |
686 | *newsyms++ = symbol_ptr_ptr[i]; | |
687 | *newsyms = (asymbol *) NULL; | |
688 | symbol_ptr_ptr = bfd_ptr->outsymbols; | |
689 | } | |
690 | ||
691 | for (symbol_index = 0; symbol_index < symbol_count; symbol_index++) | |
692 | { | |
693 | coff_symbol_type *coff_symbol_ptr = coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]); | |
244148ad | 694 | symbol_ptr_ptr[symbol_index]->udata.i = symbol_index; |
252b5132 RH |
695 | if (coff_symbol_ptr && coff_symbol_ptr->native) |
696 | { | |
697 | combined_entry_type *s = coff_symbol_ptr->native; | |
698 | int i; | |
699 | ||
700 | if (s->u.syment.n_sclass == C_FILE) | |
701 | { | |
702 | if (last_file != (struct internal_syment *) NULL) | |
703 | last_file->n_value = native_index; | |
704 | last_file = &(s->u.syment); | |
705 | } | |
706 | else | |
707 | { | |
708 | ||
709 | /* Modify the symbol values according to their section and | |
710 | type */ | |
711 | ||
712 | fixup_symbol_value (bfd_ptr, coff_symbol_ptr, &(s->u.syment)); | |
713 | } | |
714 | for (i = 0; i < s->u.syment.n_numaux + 1; i++) | |
715 | s[i].offset = native_index++; | |
716 | } | |
717 | else | |
718 | { | |
719 | native_index++; | |
720 | } | |
721 | } | |
722 | obj_conv_table_size (bfd_ptr) = native_index; | |
723 | ||
724 | return true; | |
725 | } | |
726 | ||
727 | /* Run thorough the symbol table again, and fix it so that all | |
728 | pointers to entries are changed to the entries' index in the output | |
729 | symbol table. */ | |
730 | ||
731 | void | |
732 | coff_mangle_symbols (bfd_ptr) | |
733 | bfd *bfd_ptr; | |
734 | { | |
735 | unsigned int symbol_count = bfd_get_symcount (bfd_ptr); | |
736 | asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols; | |
737 | unsigned int symbol_index; | |
738 | ||
739 | for (symbol_index = 0; symbol_index < symbol_count; symbol_index++) | |
740 | { | |
741 | coff_symbol_type *coff_symbol_ptr = | |
742 | coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]); | |
743 | ||
744 | if (coff_symbol_ptr && coff_symbol_ptr->native) | |
745 | { | |
746 | int i; | |
747 | combined_entry_type *s = coff_symbol_ptr->native; | |
748 | ||
749 | if (s->fix_value) | |
750 | { | |
751 | /* FIXME: We should use a union here. */ | |
752 | s->u.syment.n_value = | |
753 | ((combined_entry_type *) s->u.syment.n_value)->offset; | |
754 | s->fix_value = 0; | |
755 | } | |
756 | if (s->fix_line) | |
757 | { | |
758 | /* The value is the offset into the line number entries | |
759 | for the symbol's section. On output, the symbol's | |
760 | section should be N_DEBUG. */ | |
761 | s->u.syment.n_value = | |
762 | (coff_symbol_ptr->symbol.section->output_section->line_filepos | |
763 | + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr)); | |
764 | coff_symbol_ptr->symbol.section = | |
765 | coff_section_from_bfd_index (bfd_ptr, N_DEBUG); | |
766 | BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING); | |
767 | } | |
768 | for (i = 0; i < s->u.syment.n_numaux; i++) | |
769 | { | |
770 | combined_entry_type *a = s + i + 1; | |
771 | if (a->fix_tag) | |
772 | { | |
773 | a->u.auxent.x_sym.x_tagndx.l = | |
774 | a->u.auxent.x_sym.x_tagndx.p->offset; | |
775 | a->fix_tag = 0; | |
776 | } | |
777 | if (a->fix_end) | |
778 | { | |
779 | a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l = | |
780 | a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset; | |
781 | a->fix_end = 0; | |
782 | } | |
783 | if (a->fix_scnlen) | |
784 | { | |
785 | a->u.auxent.x_csect.x_scnlen.l = | |
786 | a->u.auxent.x_csect.x_scnlen.p->offset; | |
787 | a->fix_scnlen = 0; | |
788 | } | |
789 | } | |
790 | } | |
791 | } | |
792 | } | |
793 | ||
794 | static void | |
795 | coff_fix_symbol_name (abfd, symbol, native, string_size_p, | |
796 | debug_string_section_p, debug_string_size_p) | |
797 | bfd *abfd; | |
798 | asymbol *symbol; | |
799 | combined_entry_type *native; | |
800 | bfd_size_type *string_size_p; | |
801 | asection **debug_string_section_p; | |
802 | bfd_size_type *debug_string_size_p; | |
803 | { | |
804 | unsigned int name_length; | |
805 | union internal_auxent *auxent; | |
806 | char *name = (char *) (symbol->name); | |
807 | ||
808 | if (name == (char *) NULL) | |
809 | { | |
810 | /* coff symbols always have names, so we'll make one up */ | |
811 | symbol->name = "strange"; | |
812 | name = (char *) symbol->name; | |
813 | } | |
814 | name_length = strlen (name); | |
815 | ||
816 | if (native->u.syment.n_sclass == C_FILE | |
817 | && native->u.syment.n_numaux > 0) | |
818 | { | |
692b7d62 ILT |
819 | unsigned int filnmlen; |
820 | ||
7f6d05e8 CP |
821 | if (bfd_coff_force_symnames_in_strings (abfd)) |
822 | { | |
244148ad | 823 | native->u.syment._n._n_n._n_offset = |
7f6d05e8 CP |
824 | (*string_size_p + STRING_SIZE_SIZE); |
825 | native->u.syment._n._n_n._n_zeroes = 0; | |
826 | *string_size_p += 6; /* strlen(".file") + 1 */ | |
827 | } | |
828 | else | |
829 | strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN); | |
830 | ||
252b5132 RH |
831 | auxent = &(native + 1)->u.auxent; |
832 | ||
692b7d62 ILT |
833 | filnmlen = bfd_coff_filnmlen (abfd); |
834 | ||
252b5132 RH |
835 | if (bfd_coff_long_filenames (abfd)) |
836 | { | |
692b7d62 | 837 | if (name_length <= filnmlen) |
252b5132 | 838 | { |
692b7d62 | 839 | strncpy (auxent->x_file.x_fname, name, filnmlen); |
252b5132 RH |
840 | } |
841 | else | |
842 | { | |
843 | auxent->x_file.x_n.x_offset = *string_size_p + STRING_SIZE_SIZE; | |
844 | auxent->x_file.x_n.x_zeroes = 0; | |
845 | *string_size_p += name_length + 1; | |
846 | } | |
847 | } | |
848 | else | |
849 | { | |
692b7d62 ILT |
850 | strncpy (auxent->x_file.x_fname, name, filnmlen); |
851 | if (name_length > filnmlen) | |
852 | name[filnmlen] = '\0'; | |
252b5132 RH |
853 | } |
854 | } | |
855 | else | |
856 | { | |
7f6d05e8 | 857 | if (name_length <= SYMNMLEN && !bfd_coff_force_symnames_in_strings (abfd)) |
252b5132 RH |
858 | { |
859 | /* This name will fit into the symbol neatly */ | |
860 | strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN); | |
861 | } | |
862 | else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment)) | |
863 | { | |
864 | native->u.syment._n._n_n._n_offset = (*string_size_p | |
865 | + STRING_SIZE_SIZE); | |
866 | native->u.syment._n._n_n._n_zeroes = 0; | |
867 | *string_size_p += name_length + 1; | |
868 | } | |
869 | else | |
870 | { | |
871 | long filepos; | |
7f6d05e8 CP |
872 | bfd_byte buf[4]; |
873 | int prefix_len = bfd_coff_debug_string_prefix_length (abfd); | |
252b5132 RH |
874 | |
875 | /* This name should be written into the .debug section. For | |
876 | some reason each name is preceded by a two byte length | |
877 | and also followed by a null byte. FIXME: We assume that | |
878 | the .debug section has already been created, and that it | |
879 | is large enough. */ | |
880 | if (*debug_string_section_p == (asection *) NULL) | |
881 | *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug"); | |
882 | filepos = bfd_tell (abfd); | |
7f6d05e8 CP |
883 | if (prefix_len == 4) |
884 | bfd_put_32 (abfd, name_length + 1, buf); | |
885 | else | |
886 | bfd_put_16 (abfd, name_length + 1, buf); | |
887 | ||
252b5132 RH |
888 | if (!bfd_set_section_contents (abfd, |
889 | *debug_string_section_p, | |
890 | (PTR) buf, | |
891 | (file_ptr) *debug_string_size_p, | |
7f6d05e8 | 892 | (bfd_size_type) prefix_len) |
252b5132 RH |
893 | || !bfd_set_section_contents (abfd, |
894 | *debug_string_section_p, | |
895 | (PTR) symbol->name, | |
896 | ((file_ptr) *debug_string_size_p | |
7f6d05e8 | 897 | + prefix_len), |
252b5132 RH |
898 | (bfd_size_type) name_length + 1)) |
899 | abort (); | |
900 | if (bfd_seek (abfd, filepos, SEEK_SET) != 0) | |
901 | abort (); | |
244148ad | 902 | native->u.syment._n._n_n._n_offset = |
7f6d05e8 | 903 | *debug_string_size_p + prefix_len; |
252b5132 | 904 | native->u.syment._n._n_n._n_zeroes = 0; |
7f6d05e8 | 905 | *debug_string_size_p += name_length + 1 + prefix_len; |
252b5132 RH |
906 | } |
907 | } | |
908 | } | |
909 | ||
910 | /* We need to keep track of the symbol index so that when we write out | |
911 | the relocs we can get the index for a symbol. This method is a | |
912 | hack. FIXME. */ | |
913 | ||
914 | #define set_index(symbol, idx) ((symbol)->udata.i = (idx)) | |
915 | ||
916 | /* Write a symbol out to a COFF file. */ | |
917 | ||
918 | static boolean | |
919 | coff_write_symbol (abfd, symbol, native, written, string_size_p, | |
920 | debug_string_section_p, debug_string_size_p) | |
921 | bfd *abfd; | |
922 | asymbol *symbol; | |
923 | combined_entry_type *native; | |
924 | unsigned int *written; | |
925 | bfd_size_type *string_size_p; | |
926 | asection **debug_string_section_p; | |
927 | bfd_size_type *debug_string_size_p; | |
928 | { | |
929 | unsigned int numaux = native->u.syment.n_numaux; | |
930 | int type = native->u.syment.n_type; | |
931 | int class = native->u.syment.n_sclass; | |
932 | PTR buf; | |
933 | bfd_size_type symesz; | |
934 | ||
935 | if (native->u.syment.n_sclass == C_FILE) | |
936 | symbol->flags |= BSF_DEBUGGING; | |
937 | ||
938 | if (symbol->flags & BSF_DEBUGGING | |
939 | && bfd_is_abs_section (symbol->section)) | |
940 | { | |
941 | native->u.syment.n_scnum = N_DEBUG; | |
942 | } | |
943 | else if (bfd_is_abs_section (symbol->section)) | |
944 | { | |
945 | native->u.syment.n_scnum = N_ABS; | |
946 | } | |
947 | else if (bfd_is_und_section (symbol->section)) | |
948 | { | |
949 | native->u.syment.n_scnum = N_UNDEF; | |
950 | } | |
951 | else | |
952 | { | |
953 | native->u.syment.n_scnum = | |
954 | symbol->section->output_section->target_index; | |
955 | } | |
956 | ||
957 | coff_fix_symbol_name (abfd, symbol, native, string_size_p, | |
958 | debug_string_section_p, debug_string_size_p); | |
959 | ||
960 | symesz = bfd_coff_symesz (abfd); | |
961 | buf = bfd_alloc (abfd, symesz); | |
962 | if (!buf) | |
963 | return false; | |
964 | bfd_coff_swap_sym_out (abfd, &native->u.syment, buf); | |
965 | if (bfd_write (buf, 1, symesz, abfd) != symesz) | |
966 | return false; | |
967 | bfd_release (abfd, buf); | |
968 | ||
969 | if (native->u.syment.n_numaux > 0) | |
970 | { | |
971 | bfd_size_type auxesz; | |
972 | unsigned int j; | |
973 | ||
974 | auxesz = bfd_coff_auxesz (abfd); | |
975 | buf = bfd_alloc (abfd, auxesz); | |
976 | if (!buf) | |
977 | return false; | |
978 | for (j = 0; j < native->u.syment.n_numaux; j++) | |
979 | { | |
980 | bfd_coff_swap_aux_out (abfd, | |
981 | &((native + j + 1)->u.auxent), | |
982 | type, | |
983 | class, | |
984 | j, | |
985 | native->u.syment.n_numaux, | |
986 | buf); | |
987 | if (bfd_write (buf, 1, auxesz, abfd) != auxesz) | |
988 | return false; | |
989 | } | |
990 | bfd_release (abfd, buf); | |
991 | } | |
992 | ||
993 | /* Store the index for use when we write out the relocs. */ | |
994 | set_index (symbol, *written); | |
995 | ||
996 | *written += numaux + 1; | |
997 | return true; | |
998 | } | |
999 | ||
1000 | /* Write out a symbol to a COFF file that does not come from a COFF | |
1001 | file originally. This symbol may have been created by the linker, | |
1002 | or we may be linking a non COFF file to a COFF file. */ | |
1003 | ||
1004 | static boolean | |
1005 | coff_write_alien_symbol (abfd, symbol, written, string_size_p, | |
1006 | debug_string_section_p, debug_string_size_p) | |
1007 | bfd *abfd; | |
1008 | asymbol *symbol; | |
1009 | unsigned int *written; | |
1010 | bfd_size_type *string_size_p; | |
1011 | asection **debug_string_section_p; | |
1012 | bfd_size_type *debug_string_size_p; | |
1013 | { | |
1014 | combined_entry_type *native; | |
1015 | combined_entry_type dummy; | |
1016 | ||
1017 | native = &dummy; | |
1018 | native->u.syment.n_type = T_NULL; | |
1019 | native->u.syment.n_flags = 0; | |
1020 | if (bfd_is_und_section (symbol->section)) | |
1021 | { | |
1022 | native->u.syment.n_scnum = N_UNDEF; | |
1023 | native->u.syment.n_value = symbol->value; | |
1024 | } | |
1025 | else if (bfd_is_com_section (symbol->section)) | |
1026 | { | |
1027 | native->u.syment.n_scnum = N_UNDEF; | |
1028 | native->u.syment.n_value = symbol->value; | |
1029 | } | |
1030 | else if (symbol->flags & BSF_DEBUGGING) | |
1031 | { | |
1032 | /* There isn't much point to writing out a debugging symbol | |
1033 | unless we are prepared to convert it into COFF debugging | |
1034 | format. So, we just ignore them. We must clobber the symbol | |
1035 | name to keep it from being put in the string table. */ | |
1036 | symbol->name = ""; | |
1037 | return true; | |
1038 | } | |
1039 | else | |
1040 | { | |
1041 | native->u.syment.n_scnum = | |
1042 | symbol->section->output_section->target_index; | |
1043 | native->u.syment.n_value = (symbol->value | |
1044 | + symbol->section->output_offset); | |
1045 | if (! obj_pe (abfd)) | |
1046 | native->u.syment.n_value += symbol->section->output_section->vma; | |
1047 | ||
1048 | /* Copy the any flags from the the file header into the symbol. | |
1049 | FIXME: Why? */ | |
1050 | { | |
1051 | coff_symbol_type *c = coff_symbol_from (abfd, symbol); | |
1052 | if (c != (coff_symbol_type *) NULL) | |
1053 | native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags; | |
1054 | } | |
1055 | } | |
1056 | ||
1057 | native->u.syment.n_type = 0; | |
1058 | if (symbol->flags & BSF_LOCAL) | |
1059 | native->u.syment.n_sclass = C_STAT; | |
1060 | else if (symbol->flags & BSF_WEAK) | |
1061 | native->u.syment.n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT; | |
1062 | else | |
1063 | native->u.syment.n_sclass = C_EXT; | |
1064 | native->u.syment.n_numaux = 0; | |
1065 | ||
1066 | return coff_write_symbol (abfd, symbol, native, written, string_size_p, | |
1067 | debug_string_section_p, debug_string_size_p); | |
1068 | } | |
1069 | ||
1070 | /* Write a native symbol to a COFF file. */ | |
1071 | ||
1072 | static boolean | |
1073 | coff_write_native_symbol (abfd, symbol, written, string_size_p, | |
1074 | debug_string_section_p, debug_string_size_p) | |
1075 | bfd *abfd; | |
1076 | coff_symbol_type *symbol; | |
1077 | unsigned int *written; | |
1078 | bfd_size_type *string_size_p; | |
1079 | asection **debug_string_section_p; | |
1080 | bfd_size_type *debug_string_size_p; | |
1081 | { | |
1082 | combined_entry_type *native = symbol->native; | |
1083 | alent *lineno = symbol->lineno; | |
1084 | ||
1085 | /* If this symbol has an associated line number, we must store the | |
1086 | symbol index in the line number field. We also tag the auxent to | |
1087 | point to the right place in the lineno table. */ | |
1088 | if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL) | |
1089 | { | |
1090 | unsigned int count = 0; | |
1091 | lineno[count].u.offset = *written; | |
1092 | if (native->u.syment.n_numaux) | |
1093 | { | |
1094 | union internal_auxent *a = &((native + 1)->u.auxent); | |
1095 | ||
1096 | a->x_sym.x_fcnary.x_fcn.x_lnnoptr = | |
1097 | symbol->symbol.section->output_section->moving_line_filepos; | |
1098 | } | |
1099 | ||
1100 | /* Count and relocate all other linenumbers. */ | |
1101 | count++; | |
1102 | while (lineno[count].line_number != 0) | |
1103 | { | |
1104 | #if 0 | |
244148ad | 1105 | /* 13 april 92. sac |
252b5132 RH |
1106 | I've been told this, but still need proof: |
1107 | > The second bug is also in `bfd/coffcode.h'. This bug | |
1108 | > causes the linker to screw up the pc-relocations for | |
1109 | > all the line numbers in COFF code. This bug isn't only | |
1110 | > specific to A29K implementations, but affects all | |
1111 | > systems using COFF format binaries. Note that in COFF | |
1112 | > object files, the line number core offsets output by | |
1113 | > the assembler are relative to the start of each | |
1114 | > procedure, not to the start of the .text section. This | |
1115 | > patch relocates the line numbers relative to the | |
1116 | > `native->u.syment.n_value' instead of the section | |
1117 | > virtual address. | |
1118 | > [email protected] (Jon Olson) | |
1119 | */ | |
1120 | lineno[count].u.offset += native->u.syment.n_value; | |
1121 | #else | |
1122 | lineno[count].u.offset += | |
1123 | (symbol->symbol.section->output_section->vma | |
1124 | + symbol->symbol.section->output_offset); | |
1125 | #endif | |
1126 | count++; | |
1127 | } | |
1128 | symbol->done_lineno = true; | |
1129 | ||
1130 | symbol->symbol.section->output_section->moving_line_filepos += | |
1131 | count * bfd_coff_linesz (abfd); | |
1132 | } | |
1133 | ||
1134 | return coff_write_symbol (abfd, &(symbol->symbol), native, written, | |
1135 | string_size_p, debug_string_section_p, | |
1136 | debug_string_size_p); | |
1137 | } | |
1138 | ||
1139 | /* Write out the COFF symbols. */ | |
1140 | ||
1141 | boolean | |
1142 | coff_write_symbols (abfd) | |
1143 | bfd *abfd; | |
1144 | { | |
1145 | bfd_size_type string_size; | |
1146 | asection *debug_string_section; | |
1147 | bfd_size_type debug_string_size; | |
1148 | unsigned int i; | |
1149 | unsigned int limit = bfd_get_symcount (abfd); | |
1150 | unsigned int written = 0; | |
1151 | asymbol **p; | |
1152 | ||
1153 | string_size = 0; | |
1154 | debug_string_section = NULL; | |
1155 | debug_string_size = 0; | |
1156 | ||
1157 | /* If this target supports long section names, they must be put into | |
1158 | the string table. This is supported by PE. This code must | |
1159 | handle section names just as they are handled in | |
1160 | coff_write_object_contents. */ | |
1161 | if (bfd_coff_long_section_names (abfd)) | |
1162 | { | |
1163 | asection *o; | |
1164 | ||
1165 | for (o = abfd->sections; o != NULL; o = o->next) | |
1166 | { | |
1167 | size_t len; | |
1168 | ||
1169 | len = strlen (o->name); | |
1170 | if (len > SCNNMLEN) | |
1171 | string_size += len + 1; | |
1172 | } | |
1173 | } | |
1174 | ||
1175 | /* Seek to the right place */ | |
1176 | if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0) | |
1177 | return false; | |
1178 | ||
1179 | /* Output all the symbols we have */ | |
1180 | ||
1181 | written = 0; | |
1182 | for (p = abfd->outsymbols, i = 0; i < limit; i++, p++) | |
1183 | { | |
1184 | asymbol *symbol = *p; | |
1185 | coff_symbol_type *c_symbol = coff_symbol_from (abfd, symbol); | |
1186 | ||
1187 | if (c_symbol == (coff_symbol_type *) NULL | |
1188 | || c_symbol->native == (combined_entry_type *) NULL) | |
1189 | { | |
1190 | if (!coff_write_alien_symbol (abfd, symbol, &written, &string_size, | |
1191 | &debug_string_section, | |
1192 | &debug_string_size)) | |
1193 | return false; | |
1194 | } | |
1195 | else | |
1196 | { | |
1197 | if (!coff_write_native_symbol (abfd, c_symbol, &written, | |
1198 | &string_size, &debug_string_section, | |
1199 | &debug_string_size)) | |
1200 | return false; | |
1201 | } | |
1202 | } | |
1203 | ||
1204 | obj_raw_syment_count (abfd) = written; | |
1205 | ||
1206 | /* Now write out strings */ | |
1207 | ||
1208 | if (string_size != 0) | |
1209 | { | |
1210 | unsigned int size = string_size + STRING_SIZE_SIZE; | |
1211 | bfd_byte buffer[STRING_SIZE_SIZE]; | |
1212 | ||
1213 | #if STRING_SIZE_SIZE == 4 | |
1214 | bfd_h_put_32 (abfd, size, buffer); | |
1215 | #else | |
1216 | #error Change bfd_h_put_32 | |
1217 | #endif | |
1218 | if (bfd_write ((PTR) buffer, 1, sizeof (buffer), abfd) != sizeof (buffer)) | |
1219 | return false; | |
1220 | ||
1221 | /* Handle long section names. This code must handle section | |
1222 | names just as they are handled in coff_write_object_contents. */ | |
1223 | if (bfd_coff_long_section_names (abfd)) | |
1224 | { | |
1225 | asection *o; | |
1226 | ||
1227 | for (o = abfd->sections; o != NULL; o = o->next) | |
1228 | { | |
1229 | size_t len; | |
1230 | ||
1231 | len = strlen (o->name); | |
1232 | if (len > SCNNMLEN) | |
1233 | { | |
1234 | if (bfd_write (o->name, 1, len + 1, abfd) != len + 1) | |
1235 | return false; | |
1236 | } | |
1237 | } | |
1238 | } | |
1239 | ||
1240 | for (p = abfd->outsymbols, i = 0; | |
1241 | i < limit; | |
1242 | i++, p++) | |
1243 | { | |
1244 | asymbol *q = *p; | |
1245 | size_t name_length = strlen (q->name); | |
1246 | coff_symbol_type *c_symbol = coff_symbol_from (abfd, q); | |
1247 | size_t maxlen; | |
1248 | ||
1249 | /* Figure out whether the symbol name should go in the string | |
1250 | table. Symbol names that are short enough are stored | |
1251 | directly in the syment structure. File names permit a | |
1252 | different, longer, length in the syment structure. On | |
1253 | XCOFF, some symbol names are stored in the .debug section | |
1254 | rather than in the string table. */ | |
1255 | ||
1256 | if (c_symbol == NULL | |
1257 | || c_symbol->native == NULL) | |
1258 | { | |
1259 | /* This is not a COFF symbol, so it certainly is not a | |
1260 | file name, nor does it go in the .debug section. */ | |
7f6d05e8 | 1261 | maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN; |
252b5132 RH |
1262 | } |
1263 | else if (bfd_coff_symname_in_debug (abfd, | |
1264 | &c_symbol->native->u.syment)) | |
1265 | { | |
1266 | /* This symbol name is in the XCOFF .debug section. | |
1267 | Don't write it into the string table. */ | |
1268 | maxlen = name_length; | |
1269 | } | |
1270 | else if (c_symbol->native->u.syment.n_sclass == C_FILE | |
1271 | && c_symbol->native->u.syment.n_numaux > 0) | |
7f6d05e8 | 1272 | { |
244148ad | 1273 | if (bfd_coff_force_symnames_in_strings (abfd)) |
7f6d05e8 CP |
1274 | bfd_write (".file", 1, 6, abfd); |
1275 | maxlen = bfd_coff_filnmlen (abfd); | |
1276 | } | |
252b5132 | 1277 | else |
7f6d05e8 | 1278 | maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN; |
252b5132 RH |
1279 | |
1280 | if (name_length > maxlen) | |
1281 | { | |
1282 | if (bfd_write ((PTR) (q->name), 1, name_length + 1, abfd) | |
1283 | != name_length + 1) | |
1284 | return false; | |
1285 | } | |
1286 | } | |
1287 | } | |
1288 | else | |
1289 | { | |
1290 | /* We would normally not write anything here, but we'll write | |
1291 | out 4 so that any stupid coff reader which tries to read the | |
1292 | string table even when there isn't one won't croak. */ | |
1293 | unsigned int size = STRING_SIZE_SIZE; | |
1294 | bfd_byte buffer[STRING_SIZE_SIZE]; | |
1295 | ||
1296 | #if STRING_SIZE_SIZE == 4 | |
1297 | bfd_h_put_32 (abfd, size, buffer); | |
1298 | #else | |
1299 | #error Change bfd_h_put_32 | |
1300 | #endif | |
1301 | if (bfd_write ((PTR) buffer, 1, STRING_SIZE_SIZE, abfd) | |
1302 | != STRING_SIZE_SIZE) | |
1303 | return false; | |
1304 | } | |
1305 | ||
1306 | /* Make sure the .debug section was created to be the correct size. | |
1307 | We should create it ourselves on the fly, but we don't because | |
1308 | BFD won't let us write to any section until we know how large all | |
1309 | the sections are. We could still do it by making another pass | |
1310 | over the symbols. FIXME. */ | |
1311 | BFD_ASSERT (debug_string_size == 0 | |
1312 | || (debug_string_section != (asection *) NULL | |
1313 | && (BFD_ALIGN (debug_string_size, | |
1314 | 1 << debug_string_section->alignment_power) | |
1315 | == bfd_section_size (abfd, debug_string_section)))); | |
1316 | ||
1317 | return true; | |
1318 | } | |
1319 | ||
1320 | boolean | |
1321 | coff_write_linenumbers (abfd) | |
1322 | bfd *abfd; | |
1323 | { | |
1324 | asection *s; | |
1325 | bfd_size_type linesz; | |
1326 | PTR buff; | |
1327 | ||
1328 | linesz = bfd_coff_linesz (abfd); | |
1329 | buff = bfd_alloc (abfd, linesz); | |
1330 | if (!buff) | |
1331 | return false; | |
1332 | for (s = abfd->sections; s != (asection *) NULL; s = s->next) | |
1333 | { | |
1334 | if (s->lineno_count) | |
1335 | { | |
1336 | asymbol **q = abfd->outsymbols; | |
1337 | if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0) | |
1338 | return false; | |
1339 | /* Find all the linenumbers in this section */ | |
1340 | while (*q) | |
1341 | { | |
1342 | asymbol *p = *q; | |
1343 | if (p->section->output_section == s) | |
1344 | { | |
1345 | alent *l = | |
1346 | BFD_SEND (bfd_asymbol_bfd (p), _get_lineno, | |
1347 | (bfd_asymbol_bfd (p), p)); | |
1348 | if (l) | |
1349 | { | |
1350 | /* Found a linenumber entry, output */ | |
1351 | struct internal_lineno out; | |
1352 | memset ((PTR) & out, 0, sizeof (out)); | |
1353 | out.l_lnno = 0; | |
1354 | out.l_addr.l_symndx = l->u.offset; | |
1355 | bfd_coff_swap_lineno_out (abfd, &out, buff); | |
1356 | if (bfd_write (buff, 1, linesz, abfd) != linesz) | |
1357 | return false; | |
1358 | l++; | |
1359 | while (l->line_number) | |
1360 | { | |
1361 | out.l_lnno = l->line_number; | |
1362 | out.l_addr.l_symndx = l->u.offset; | |
1363 | bfd_coff_swap_lineno_out (abfd, &out, buff); | |
1364 | if (bfd_write (buff, 1, linesz, abfd) != linesz) | |
1365 | return false; | |
1366 | l++; | |
1367 | } | |
1368 | } | |
1369 | } | |
1370 | q++; | |
1371 | } | |
1372 | } | |
1373 | } | |
1374 | bfd_release (abfd, buff); | |
1375 | return true; | |
1376 | } | |
1377 | ||
252b5132 RH |
1378 | alent * |
1379 | coff_get_lineno (ignore_abfd, symbol) | |
7442e600 | 1380 | bfd *ignore_abfd ATTRIBUTE_UNUSED; |
252b5132 RH |
1381 | asymbol *symbol; |
1382 | { | |
1383 | return coffsymbol (symbol)->lineno; | |
1384 | } | |
1385 | ||
1386 | #if 0 | |
1387 | ||
1388 | /* This is only called from coff_add_missing_symbols, which has been | |
1389 | disabled. */ | |
1390 | ||
1391 | asymbol * | |
1392 | coff_section_symbol (abfd, name) | |
1393 | bfd *abfd; | |
1394 | char *name; | |
1395 | { | |
1396 | asection *sec = bfd_make_section_old_way (abfd, name); | |
1397 | asymbol *sym; | |
1398 | combined_entry_type *csym; | |
1399 | ||
1400 | sym = sec->symbol; | |
1401 | csym = coff_symbol_from (abfd, sym)->native; | |
1402 | /* Make sure back-end COFF stuff is there. */ | |
1403 | if (csym == 0) | |
1404 | { | |
1405 | struct foo | |
1406 | { | |
1407 | coff_symbol_type sym; | |
1408 | /* @@FIXME This shouldn't use a fixed size!! */ | |
1409 | combined_entry_type e[10]; | |
1410 | }; | |
1411 | struct foo *f; | |
1412 | f = (struct foo *) bfd_alloc (abfd, sizeof (*f)); | |
1413 | if (!f) | |
1414 | { | |
1415 | bfd_set_error (bfd_error_no_error); | |
1416 | return NULL; | |
1417 | } | |
1418 | memset ((char *) f, 0, sizeof (*f)); | |
1419 | coff_symbol_from (abfd, sym)->native = csym = f->e; | |
1420 | } | |
1421 | csym[0].u.syment.n_sclass = C_STAT; | |
1422 | csym[0].u.syment.n_numaux = 1; | |
1423 | /* SF_SET_STATICS (sym); @@ ??? */ | |
1424 | csym[1].u.auxent.x_scn.x_scnlen = sec->_raw_size; | |
1425 | csym[1].u.auxent.x_scn.x_nreloc = sec->reloc_count; | |
1426 | csym[1].u.auxent.x_scn.x_nlinno = sec->lineno_count; | |
1427 | ||
1428 | if (sec->output_section == NULL) | |
1429 | { | |
1430 | sec->output_section = sec; | |
1431 | sec->output_offset = 0; | |
1432 | } | |
1433 | ||
1434 | return sym; | |
1435 | } | |
1436 | ||
1437 | #endif /* 0 */ | |
1438 | ||
1439 | /* This function transforms the offsets into the symbol table into | |
1440 | pointers to syments. */ | |
1441 | ||
1442 | static void | |
1443 | coff_pointerize_aux (abfd, table_base, symbol, indaux, auxent) | |
1444 | bfd *abfd; | |
1445 | combined_entry_type *table_base; | |
1446 | combined_entry_type *symbol; | |
1447 | unsigned int indaux; | |
1448 | combined_entry_type *auxent; | |
1449 | { | |
1450 | unsigned int type = symbol->u.syment.n_type; | |
1451 | unsigned int class = symbol->u.syment.n_sclass; | |
1452 | ||
1453 | if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook) | |
1454 | { | |
1455 | if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook) | |
1456 | (abfd, table_base, symbol, indaux, auxent)) | |
1457 | return; | |
1458 | } | |
1459 | ||
1460 | /* Don't bother if this is a file or a section */ | |
1461 | if (class == C_STAT && type == T_NULL) | |
1462 | return; | |
1463 | if (class == C_FILE) | |
1464 | return; | |
1465 | ||
1466 | /* Otherwise patch up */ | |
1467 | #define N_TMASK coff_data (abfd)->local_n_tmask | |
1468 | #define N_BTSHFT coff_data (abfd)->local_n_btshft | |
1469 | if ((ISFCN (type) || ISTAG (class) || class == C_BLOCK || class == C_FCN) | |
1470 | && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l > 0) | |
1471 | { | |
1472 | auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p = | |
1473 | table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l; | |
1474 | auxent->fix_end = 1; | |
1475 | } | |
1476 | /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can | |
1477 | generate one, so we must be careful to ignore it. */ | |
1478 | if (auxent->u.auxent.x_sym.x_tagndx.l > 0) | |
1479 | { | |
1480 | auxent->u.auxent.x_sym.x_tagndx.p = | |
1481 | table_base + auxent->u.auxent.x_sym.x_tagndx.l; | |
1482 | auxent->fix_tag = 1; | |
1483 | } | |
1484 | } | |
1485 | ||
1486 | /* Allocate space for the ".debug" section, and read it. | |
1487 | We did not read the debug section until now, because | |
244148ad | 1488 | we didn't want to go to the trouble until someone needed it. */ |
252b5132 RH |
1489 | |
1490 | static char * | |
1491 | build_debug_section (abfd) | |
1492 | bfd *abfd; | |
1493 | { | |
1494 | char *debug_section; | |
1495 | long position; | |
1496 | ||
1497 | asection *sect = bfd_get_section_by_name (abfd, ".debug"); | |
1498 | ||
1499 | if (!sect) | |
1500 | { | |
1501 | bfd_set_error (bfd_error_no_debug_section); | |
1502 | return NULL; | |
1503 | } | |
1504 | ||
1505 | debug_section = (PTR) bfd_alloc (abfd, | |
1506 | bfd_get_section_size_before_reloc (sect)); | |
1507 | if (debug_section == NULL) | |
1508 | return NULL; | |
1509 | ||
244148ad | 1510 | /* Seek to the beginning of the `.debug' section and read it. |
252b5132 RH |
1511 | Save the current position first; it is needed by our caller. |
1512 | Then read debug section and reset the file pointer. */ | |
1513 | ||
1514 | position = bfd_tell (abfd); | |
1515 | if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0 | |
1516 | || (bfd_read (debug_section, | |
1517 | bfd_get_section_size_before_reloc (sect), 1, abfd) | |
1518 | != bfd_get_section_size_before_reloc (sect)) | |
1519 | || bfd_seek (abfd, position, SEEK_SET) != 0) | |
1520 | return NULL; | |
1521 | return debug_section; | |
1522 | } | |
1523 | ||
252b5132 RH |
1524 | /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be |
1525 | \0-terminated, but will not exceed 'maxlen' characters. The copy *will* | |
1526 | be \0-terminated. */ | |
1527 | static char * | |
1528 | copy_name (abfd, name, maxlen) | |
1529 | bfd *abfd; | |
1530 | char *name; | |
1531 | int maxlen; | |
1532 | { | |
1533 | int len; | |
1534 | char *newname; | |
1535 | ||
1536 | for (len = 0; len < maxlen; ++len) | |
1537 | { | |
1538 | if (name[len] == '\0') | |
1539 | { | |
1540 | break; | |
1541 | } | |
1542 | } | |
1543 | ||
1544 | if ((newname = (PTR) bfd_alloc (abfd, len + 1)) == NULL) | |
1545 | return (NULL); | |
1546 | strncpy (newname, name, len); | |
1547 | newname[len] = '\0'; | |
1548 | return newname; | |
1549 | } | |
1550 | ||
1551 | /* Read in the external symbols. */ | |
1552 | ||
1553 | boolean | |
1554 | _bfd_coff_get_external_symbols (abfd) | |
1555 | bfd *abfd; | |
1556 | { | |
1557 | bfd_size_type symesz; | |
1558 | size_t size; | |
1559 | PTR syms; | |
1560 | ||
1561 | if (obj_coff_external_syms (abfd) != NULL) | |
1562 | return true; | |
1563 | ||
1564 | symesz = bfd_coff_symesz (abfd); | |
1565 | ||
1566 | size = obj_raw_syment_count (abfd) * symesz; | |
1567 | ||
1568 | syms = (PTR) bfd_malloc (size); | |
1569 | if (syms == NULL && size != 0) | |
1570 | return false; | |
1571 | ||
1572 | if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0 | |
1573 | || bfd_read (syms, size, 1, abfd) != size) | |
1574 | { | |
1575 | if (syms != NULL) | |
1576 | free (syms); | |
1577 | return false; | |
1578 | } | |
1579 | ||
1580 | obj_coff_external_syms (abfd) = syms; | |
1581 | ||
1582 | return true; | |
1583 | } | |
1584 | ||
1585 | /* Read in the external strings. The strings are not loaded until | |
1586 | they are needed. This is because we have no simple way of | |
1587 | detecting a missing string table in an archive. */ | |
1588 | ||
1589 | const char * | |
1590 | _bfd_coff_read_string_table (abfd) | |
1591 | bfd *abfd; | |
1592 | { | |
1593 | char extstrsize[STRING_SIZE_SIZE]; | |
1594 | size_t strsize; | |
1595 | char *strings; | |
1596 | ||
1597 | if (obj_coff_strings (abfd) != NULL) | |
1598 | return obj_coff_strings (abfd); | |
1599 | ||
1600 | if (obj_sym_filepos (abfd) == 0) | |
1601 | { | |
1602 | bfd_set_error (bfd_error_no_symbols); | |
1603 | return NULL; | |
1604 | } | |
1605 | ||
1606 | if (bfd_seek (abfd, | |
1607 | (obj_sym_filepos (abfd) | |
1608 | + obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd)), | |
1609 | SEEK_SET) != 0) | |
1610 | return NULL; | |
244148ad | 1611 | |
252b5132 RH |
1612 | if (bfd_read (extstrsize, sizeof extstrsize, 1, abfd) != sizeof extstrsize) |
1613 | { | |
1614 | if (bfd_get_error () != bfd_error_file_truncated) | |
1615 | return NULL; | |
1616 | ||
1617 | /* There is no string table. */ | |
1618 | strsize = STRING_SIZE_SIZE; | |
1619 | } | |
1620 | else | |
1621 | { | |
1622 | #if STRING_SIZE_SIZE == 4 | |
1623 | strsize = bfd_h_get_32 (abfd, (bfd_byte *) extstrsize); | |
1624 | #else | |
1625 | #error Change bfd_h_get_32 | |
1626 | #endif | |
1627 | } | |
1628 | ||
1629 | if (strsize < STRING_SIZE_SIZE) | |
1630 | { | |
1631 | (*_bfd_error_handler) | |
1632 | (_("%s: bad string table size %lu"), bfd_get_filename (abfd), | |
1633 | (unsigned long) strsize); | |
1634 | bfd_set_error (bfd_error_bad_value); | |
1635 | return NULL; | |
1636 | } | |
1637 | ||
1638 | strings = (char *) bfd_malloc (strsize); | |
1639 | if (strings == NULL) | |
1640 | return NULL; | |
1641 | ||
1642 | if (bfd_read (strings + STRING_SIZE_SIZE, | |
1643 | strsize - STRING_SIZE_SIZE, 1, abfd) | |
1644 | != strsize - STRING_SIZE_SIZE) | |
1645 | { | |
1646 | free (strings); | |
1647 | return NULL; | |
1648 | } | |
1649 | ||
1650 | obj_coff_strings (abfd) = strings; | |
1651 | ||
1652 | return strings; | |
1653 | } | |
1654 | ||
1655 | /* Free up the external symbols and strings read from a COFF file. */ | |
1656 | ||
1657 | boolean | |
1658 | _bfd_coff_free_symbols (abfd) | |
1659 | bfd *abfd; | |
1660 | { | |
1661 | if (obj_coff_external_syms (abfd) != NULL | |
1662 | && ! obj_coff_keep_syms (abfd)) | |
1663 | { | |
1664 | free (obj_coff_external_syms (abfd)); | |
1665 | obj_coff_external_syms (abfd) = NULL; | |
1666 | } | |
1667 | if (obj_coff_strings (abfd) != NULL | |
1668 | && ! obj_coff_keep_strings (abfd)) | |
1669 | { | |
1670 | free (obj_coff_strings (abfd)); | |
1671 | obj_coff_strings (abfd) = NULL; | |
1672 | } | |
1673 | return true; | |
1674 | } | |
1675 | ||
1676 | /* Read a symbol table into freshly bfd_allocated memory, swap it, and | |
1677 | knit the symbol names into a normalized form. By normalized here I | |
1678 | mean that all symbols have an n_offset pointer that points to a null- | |
1679 | terminated string. */ | |
1680 | ||
1681 | combined_entry_type * | |
1682 | coff_get_normalized_symtab (abfd) | |
1683 | bfd *abfd; | |
1684 | { | |
1685 | combined_entry_type *internal; | |
1686 | combined_entry_type *internal_ptr; | |
1687 | combined_entry_type *symbol_ptr; | |
1688 | combined_entry_type *internal_end; | |
1689 | bfd_size_type symesz; | |
1690 | char *raw_src; | |
1691 | char *raw_end; | |
1692 | const char *string_table = NULL; | |
1693 | char *debug_section = NULL; | |
1694 | unsigned long size; | |
1695 | ||
1696 | if (obj_raw_syments (abfd) != NULL) | |
1697 | return obj_raw_syments (abfd); | |
1698 | ||
1699 | size = obj_raw_syment_count (abfd) * sizeof (combined_entry_type); | |
1700 | internal = (combined_entry_type *) bfd_zalloc (abfd, size); | |
1701 | if (internal == NULL && size != 0) | |
1702 | return NULL; | |
1703 | internal_end = internal + obj_raw_syment_count (abfd); | |
1704 | ||
1705 | if (! _bfd_coff_get_external_symbols (abfd)) | |
1706 | return NULL; | |
1707 | ||
1708 | raw_src = (char *) obj_coff_external_syms (abfd); | |
1709 | ||
1710 | /* mark the end of the symbols */ | |
1711 | symesz = bfd_coff_symesz (abfd); | |
1712 | raw_end = (char *) raw_src + obj_raw_syment_count (abfd) * symesz; | |
1713 | ||
1714 | /* FIXME SOMEDAY. A string table size of zero is very weird, but | |
1715 | probably possible. If one shows up, it will probably kill us. */ | |
1716 | ||
1717 | /* Swap all the raw entries */ | |
1718 | for (internal_ptr = internal; | |
1719 | raw_src < raw_end; | |
1720 | raw_src += symesz, internal_ptr++) | |
1721 | { | |
1722 | ||
1723 | unsigned int i; | |
1724 | bfd_coff_swap_sym_in (abfd, (PTR) raw_src, | |
1725 | (PTR) & internal_ptr->u.syment); | |
1726 | symbol_ptr = internal_ptr; | |
1727 | ||
1728 | for (i = 0; | |
1729 | i < symbol_ptr->u.syment.n_numaux; | |
1730 | i++) | |
1731 | { | |
1732 | internal_ptr++; | |
1733 | raw_src += symesz; | |
1734 | bfd_coff_swap_aux_in (abfd, (PTR) raw_src, | |
1735 | symbol_ptr->u.syment.n_type, | |
1736 | symbol_ptr->u.syment.n_sclass, | |
1737 | i, symbol_ptr->u.syment.n_numaux, | |
1738 | &(internal_ptr->u.auxent)); | |
1739 | coff_pointerize_aux (abfd, internal, symbol_ptr, i, | |
1740 | internal_ptr); | |
1741 | } | |
1742 | } | |
1743 | ||
1744 | /* Free the raw symbols, but not the strings (if we have them). */ | |
1745 | obj_coff_keep_strings (abfd) = true; | |
1746 | if (! _bfd_coff_free_symbols (abfd)) | |
1747 | return NULL; | |
1748 | ||
1749 | for (internal_ptr = internal; internal_ptr < internal_end; | |
1750 | internal_ptr++) | |
1751 | { | |
1752 | if (internal_ptr->u.syment.n_sclass == C_FILE | |
1753 | && internal_ptr->u.syment.n_numaux > 0) | |
1754 | { | |
1755 | /* make a file symbol point to the name in the auxent, since | |
1756 | the text ".file" is redundant */ | |
1757 | if ((internal_ptr + 1)->u.auxent.x_file.x_n.x_zeroes == 0) | |
1758 | { | |
1759 | /* the filename is a long one, point into the string table */ | |
1760 | if (string_table == NULL) | |
1761 | { | |
1762 | string_table = _bfd_coff_read_string_table (abfd); | |
1763 | if (string_table == NULL) | |
1764 | return NULL; | |
1765 | } | |
1766 | ||
1767 | internal_ptr->u.syment._n._n_n._n_offset = | |
1768 | ((long) | |
1769 | (string_table | |
1770 | + (internal_ptr + 1)->u.auxent.x_file.x_n.x_offset)); | |
1771 | } | |
1772 | else | |
1773 | { | |
7bb9db4d ILT |
1774 | /* Ordinary short filename, put into memory anyway. The |
1775 | Microsoft PE tools sometimes store a filename in | |
1776 | multiple AUX entries. */ | |
ec0ef80e DD |
1777 | if (internal_ptr->u.syment.n_numaux > 1 |
1778 | && coff_data (abfd)->pe) | |
1779 | { | |
7bb9db4d ILT |
1780 | internal_ptr->u.syment._n._n_n._n_offset = |
1781 | ((long) | |
1782 | copy_name (abfd, | |
1783 | (internal_ptr + 1)->u.auxent.x_file.x_fname, | |
1784 | internal_ptr->u.syment.n_numaux * symesz)); | |
ec0ef80e DD |
1785 | } |
1786 | else | |
1787 | { | |
7bb9db4d ILT |
1788 | internal_ptr->u.syment._n._n_n._n_offset = |
1789 | ((long) | |
1790 | copy_name (abfd, | |
1791 | (internal_ptr + 1)->u.auxent.x_file.x_fname, | |
692b7d62 | 1792 | bfd_coff_filnmlen (abfd))); |
ec0ef80e | 1793 | } |
252b5132 RH |
1794 | } |
1795 | } | |
1796 | else | |
1797 | { | |
1798 | if (internal_ptr->u.syment._n._n_n._n_zeroes != 0) | |
1799 | { | |
1800 | /* This is a "short" name. Make it long. */ | |
1801 | unsigned long i = 0; | |
1802 | char *newstring = NULL; | |
1803 | ||
1804 | /* find the length of this string without walking into memory | |
1805 | that isn't ours. */ | |
1806 | for (i = 0; i < 8; ++i) | |
1807 | { | |
1808 | if (internal_ptr->u.syment._n._n_name[i] == '\0') | |
1809 | { | |
1810 | break; | |
1811 | } /* if end of string */ | |
244148ad | 1812 | } /* possible lengths of this string. */ |
252b5132 RH |
1813 | |
1814 | if ((newstring = (PTR) bfd_alloc (abfd, ++i)) == NULL) | |
1815 | return (NULL); | |
1816 | memset (newstring, 0, i); | |
1817 | strncpy (newstring, internal_ptr->u.syment._n._n_name, i - 1); | |
1818 | internal_ptr->u.syment._n._n_n._n_offset = (long int) newstring; | |
1819 | internal_ptr->u.syment._n._n_n._n_zeroes = 0; | |
1820 | } | |
1821 | else if (internal_ptr->u.syment._n._n_n._n_offset == 0) | |
1822 | internal_ptr->u.syment._n._n_n._n_offset = (long int) ""; | |
1823 | else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment)) | |
1824 | { | |
1825 | /* Long name already. Point symbol at the string in the | |
1826 | table. */ | |
1827 | if (string_table == NULL) | |
1828 | { | |
1829 | string_table = _bfd_coff_read_string_table (abfd); | |
1830 | if (string_table == NULL) | |
1831 | return NULL; | |
1832 | } | |
1833 | internal_ptr->u.syment._n._n_n._n_offset = | |
1834 | ((long int) | |
1835 | (string_table | |
1836 | + internal_ptr->u.syment._n._n_n._n_offset)); | |
1837 | } | |
1838 | else | |
1839 | { | |
1840 | /* Long name in debug section. Very similar. */ | |
1841 | if (debug_section == NULL) | |
1842 | debug_section = build_debug_section (abfd); | |
1843 | internal_ptr->u.syment._n._n_n._n_offset = (long int) | |
1844 | (debug_section + internal_ptr->u.syment._n._n_n._n_offset); | |
1845 | } | |
1846 | } | |
1847 | internal_ptr += internal_ptr->u.syment.n_numaux; | |
1848 | } | |
1849 | ||
1850 | obj_raw_syments (abfd) = internal; | |
1851 | BFD_ASSERT (obj_raw_syment_count (abfd) | |
1852 | == (unsigned int) (internal_ptr - internal)); | |
1853 | ||
1854 | return (internal); | |
1855 | } /* coff_get_normalized_symtab() */ | |
1856 | ||
1857 | long | |
1858 | coff_get_reloc_upper_bound (abfd, asect) | |
1859 | bfd *abfd; | |
1860 | sec_ptr asect; | |
1861 | { | |
1862 | if (bfd_get_format (abfd) != bfd_object) | |
1863 | { | |
1864 | bfd_set_error (bfd_error_invalid_operation); | |
1865 | return -1; | |
1866 | } | |
1867 | return (asect->reloc_count + 1) * sizeof (arelent *); | |
1868 | } | |
1869 | ||
1870 | asymbol * | |
1871 | coff_make_empty_symbol (abfd) | |
1872 | bfd *abfd; | |
1873 | { | |
1874 | coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, sizeof (coff_symbol_type)); | |
1875 | if (new == NULL) | |
1876 | return (NULL); | |
1877 | memset (new, 0, sizeof *new); | |
1878 | new->symbol.section = 0; | |
1879 | new->native = 0; | |
1880 | new->lineno = (alent *) NULL; | |
1881 | new->done_lineno = false; | |
1882 | new->symbol.the_bfd = abfd; | |
1883 | return &new->symbol; | |
1884 | } | |
1885 | ||
1886 | /* Make a debugging symbol. */ | |
1887 | ||
1888 | asymbol * | |
1889 | coff_bfd_make_debug_symbol (abfd, ptr, sz) | |
1890 | bfd *abfd; | |
7442e600 ILT |
1891 | PTR ptr ATTRIBUTE_UNUSED; |
1892 | unsigned long sz ATTRIBUTE_UNUSED; | |
252b5132 RH |
1893 | { |
1894 | coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, sizeof (coff_symbol_type)); | |
1895 | if (new == NULL) | |
1896 | return (NULL); | |
1897 | /* @@ The 10 is a guess at a plausible maximum number of aux entries | |
1898 | (but shouldn't be a constant). */ | |
1899 | new->native = (combined_entry_type *) bfd_zalloc (abfd, sizeof (combined_entry_type) * 10); | |
1900 | if (!new->native) | |
1901 | return (NULL); | |
1902 | new->symbol.section = bfd_abs_section_ptr; | |
1903 | new->symbol.flags = BSF_DEBUGGING; | |
1904 | new->lineno = (alent *) NULL; | |
1905 | new->done_lineno = false; | |
1906 | new->symbol.the_bfd = abfd; | |
1907 | return &new->symbol; | |
1908 | } | |
1909 | ||
252b5132 RH |
1910 | void |
1911 | coff_get_symbol_info (abfd, symbol, ret) | |
1912 | bfd *abfd; | |
1913 | asymbol *symbol; | |
1914 | symbol_info *ret; | |
1915 | { | |
1916 | bfd_symbol_info (symbol, ret); | |
1917 | if (coffsymbol (symbol)->native != NULL | |
1918 | && coffsymbol (symbol)->native->fix_value) | |
1919 | { | |
1920 | combined_entry_type *psym; | |
1921 | ||
1922 | psym = ((combined_entry_type *) | |
1923 | coffsymbol (symbol)->native->u.syment.n_value); | |
1924 | ret->value = (bfd_vma) (psym - obj_raw_syments (abfd)); | |
1925 | } | |
1926 | } | |
1927 | ||
1928 | /* Return the COFF syment for a symbol. */ | |
1929 | ||
1930 | boolean | |
1931 | bfd_coff_get_syment (abfd, symbol, psyment) | |
1932 | bfd *abfd; | |
1933 | asymbol *symbol; | |
1934 | struct internal_syment *psyment; | |
1935 | { | |
1936 | coff_symbol_type *csym; | |
1937 | ||
1938 | csym = coff_symbol_from (abfd, symbol); | |
1939 | if (csym == NULL || csym->native == NULL) | |
1940 | { | |
1941 | bfd_set_error (bfd_error_invalid_operation); | |
1942 | return false; | |
1943 | } | |
1944 | ||
1945 | *psyment = csym->native->u.syment; | |
1946 | ||
1947 | if (csym->native->fix_value) | |
1948 | psyment->n_value = ((combined_entry_type *) psyment->n_value | |
1949 | - obj_raw_syments (abfd)); | |
1950 | ||
1951 | /* FIXME: We should handle fix_line here. */ | |
1952 | ||
1953 | return true; | |
1954 | } | |
1955 | ||
1956 | /* Return the COFF auxent for a symbol. */ | |
1957 | ||
1958 | boolean | |
1959 | bfd_coff_get_auxent (abfd, symbol, indx, pauxent) | |
1960 | bfd *abfd; | |
1961 | asymbol *symbol; | |
1962 | int indx; | |
1963 | union internal_auxent *pauxent; | |
1964 | { | |
1965 | coff_symbol_type *csym; | |
1966 | combined_entry_type *ent; | |
1967 | ||
1968 | csym = coff_symbol_from (abfd, symbol); | |
1969 | ||
1970 | if (csym == NULL | |
1971 | || csym->native == NULL | |
1972 | || indx >= csym->native->u.syment.n_numaux) | |
1973 | { | |
1974 | bfd_set_error (bfd_error_invalid_operation); | |
1975 | return false; | |
1976 | } | |
1977 | ||
1978 | ent = csym->native + indx + 1; | |
1979 | ||
1980 | *pauxent = ent->u.auxent; | |
1981 | ||
1982 | if (ent->fix_tag) | |
1983 | pauxent->x_sym.x_tagndx.l = | |
1984 | ((combined_entry_type *) pauxent->x_sym.x_tagndx.p | |
1985 | - obj_raw_syments (abfd)); | |
1986 | ||
1987 | if (ent->fix_end) | |
1988 | pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l = | |
1989 | ((combined_entry_type *) pauxent->x_sym.x_fcnary.x_fcn.x_endndx.p | |
1990 | - obj_raw_syments (abfd)); | |
1991 | ||
1992 | if (ent->fix_scnlen) | |
1993 | pauxent->x_csect.x_scnlen.l = | |
1994 | ((combined_entry_type *) pauxent->x_csect.x_scnlen.p | |
1995 | - obj_raw_syments (abfd)); | |
1996 | ||
1997 | return true; | |
1998 | } | |
1999 | ||
2000 | /* Print out information about COFF symbol. */ | |
2001 | ||
2002 | void | |
2003 | coff_print_symbol (abfd, filep, symbol, how) | |
2004 | bfd *abfd; | |
2005 | PTR filep; | |
2006 | asymbol *symbol; | |
2007 | bfd_print_symbol_type how; | |
2008 | { | |
2009 | FILE *file = (FILE *) filep; | |
2010 | ||
2011 | switch (how) | |
2012 | { | |
2013 | case bfd_print_symbol_name: | |
2014 | fprintf (file, "%s", symbol->name); | |
2015 | break; | |
2016 | ||
2017 | case bfd_print_symbol_more: | |
2018 | fprintf (file, "coff %s %s", | |
2019 | coffsymbol (symbol)->native ? "n" : "g", | |
2020 | coffsymbol (symbol)->lineno ? "l" : " "); | |
2021 | break; | |
2022 | ||
2023 | case bfd_print_symbol_all: | |
2024 | if (coffsymbol (symbol)->native) | |
2025 | { | |
2026 | unsigned long val; | |
2027 | unsigned int aux; | |
2028 | combined_entry_type *combined = coffsymbol (symbol)->native; | |
2029 | combined_entry_type *root = obj_raw_syments (abfd); | |
2030 | struct lineno_cache_entry *l = coffsymbol (symbol)->lineno; | |
2031 | ||
2032 | fprintf (file, "[%3ld]", (long) (combined - root)); | |
2033 | ||
2034 | if (! combined->fix_value) | |
2035 | val = (unsigned long) combined->u.syment.n_value; | |
2036 | else | |
2037 | val = ((unsigned long) | |
2038 | ((combined_entry_type *) combined->u.syment.n_value | |
2039 | - root)); | |
2040 | ||
2041 | fprintf (file, | |
2042 | "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x%08lx %s", | |
2043 | combined->u.syment.n_scnum, | |
2044 | combined->u.syment.n_flags, | |
2045 | combined->u.syment.n_type, | |
2046 | combined->u.syment.n_sclass, | |
2047 | combined->u.syment.n_numaux, | |
2048 | val, | |
2049 | symbol->name); | |
2050 | ||
2051 | for (aux = 0; aux < combined->u.syment.n_numaux; aux++) | |
2052 | { | |
2053 | combined_entry_type *auxp = combined + aux + 1; | |
2054 | long tagndx; | |
2055 | ||
2056 | if (auxp->fix_tag) | |
2057 | tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root; | |
2058 | else | |
2059 | tagndx = auxp->u.auxent.x_sym.x_tagndx.l; | |
2060 | ||
2061 | fprintf (file, "\n"); | |
2062 | ||
2063 | if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux)) | |
2064 | continue; | |
2065 | ||
2066 | switch (combined->u.syment.n_sclass) | |
2067 | { | |
2068 | case C_FILE: | |
2069 | fprintf (file, "File "); | |
2070 | break; | |
2071 | ||
2072 | case C_STAT: | |
2073 | if (combined->u.syment.n_type == T_NULL) | |
2074 | /* probably a section symbol? */ | |
2075 | { | |
2076 | fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d", | |
2077 | (long) auxp->u.auxent.x_scn.x_scnlen, | |
2078 | auxp->u.auxent.x_scn.x_nreloc, | |
2079 | auxp->u.auxent.x_scn.x_nlinno); | |
2080 | if (auxp->u.auxent.x_scn.x_checksum != 0 | |
2081 | || auxp->u.auxent.x_scn.x_associated != 0 | |
2082 | || auxp->u.auxent.x_scn.x_comdat != 0) | |
2083 | fprintf (file, " checksum 0x%lx assoc %d comdat %d", | |
2084 | auxp->u.auxent.x_scn.x_checksum, | |
2085 | auxp->u.auxent.x_scn.x_associated, | |
2086 | auxp->u.auxent.x_scn.x_comdat); | |
2087 | break; | |
2088 | } | |
312191a6 ILT |
2089 | /* else fall through */ |
2090 | case C_EXT: | |
2091 | if (ISFCN (combined->u.syment.n_type)) | |
2092 | { | |
2093 | fprintf (file, | |
2094 | _("AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld"), | |
2095 | tagndx, | |
2096 | auxp->u.auxent.x_sym.x_misc.x_fsize, | |
2097 | auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_lnnoptr, | |
2098 | (auxp->fix_end | |
2099 | ? ((long) | |
2100 | (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p | |
2101 | - root)) | |
2102 | : auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l)); | |
2103 | break; | |
2104 | } | |
252b5132 | 2105 | /* else fall through */ |
252b5132 RH |
2106 | default: |
2107 | fprintf (file, "AUX lnno %d size 0x%x tagndx %ld", | |
2108 | auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno, | |
2109 | auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size, | |
2110 | tagndx); | |
2111 | if (auxp->fix_end) | |
2112 | fprintf (file, " endndx %ld", | |
2113 | ((long) | |
2114 | (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p | |
2115 | - root))); | |
2116 | break; | |
2117 | } | |
2118 | } | |
2119 | ||
2120 | if (l) | |
2121 | { | |
2122 | fprintf (file, "\n%s :", l->u.sym->name); | |
2123 | l++; | |
2124 | while (l->line_number) | |
2125 | { | |
2126 | fprintf (file, "\n%4d : 0x%lx", | |
2127 | l->line_number, | |
2128 | ((unsigned long) | |
2129 | (l->u.offset + symbol->section->vma))); | |
2130 | l++; | |
2131 | } | |
2132 | } | |
2133 | } | |
2134 | else | |
2135 | { | |
2136 | bfd_print_symbol_vandf ((PTR) file, symbol); | |
2137 | fprintf (file, " %-5s %s %s %s", | |
2138 | symbol->section->name, | |
2139 | coffsymbol (symbol)->native ? "n" : "g", | |
2140 | coffsymbol (symbol)->lineno ? "l" : " ", | |
2141 | symbol->name); | |
2142 | } | |
2143 | } | |
2144 | } | |
2145 | ||
2146 | /* Return whether a symbol name implies a local symbol. In COFF, | |
2147 | local symbols generally start with ``.L''. Most targets use this | |
2148 | function for the is_local_label_name entry point, but some may | |
2149 | override it. */ | |
2150 | ||
2151 | boolean | |
2152 | _bfd_coff_is_local_label_name (abfd, name) | |
7442e600 | 2153 | bfd *abfd ATTRIBUTE_UNUSED; |
252b5132 RH |
2154 | const char *name; |
2155 | { | |
2156 | return name[0] == '.' && name[1] == 'L'; | |
2157 | } | |
2158 | ||
9a968f43 NC |
2159 | /* Provided a BFD, a section and an offset (in bytes, not octets) into the |
2160 | section, calculate and return the name of the source file and the line | |
2161 | nearest to the wanted location. */ | |
435b1e90 | 2162 | |
252b5132 RH |
2163 | boolean |
2164 | coff_find_nearest_line (abfd, section, symbols, offset, filename_ptr, | |
2165 | functionname_ptr, line_ptr) | |
2166 | bfd *abfd; | |
2167 | asection *section; | |
2168 | asymbol **symbols; | |
2169 | bfd_vma offset; | |
2170 | CONST char **filename_ptr; | |
2171 | CONST char **functionname_ptr; | |
2172 | unsigned int *line_ptr; | |
2173 | { | |
2174 | boolean found; | |
2175 | unsigned int i; | |
2176 | unsigned int line_base; | |
2177 | coff_data_type *cof = coff_data (abfd); | |
2178 | /* Run through the raw syments if available */ | |
2179 | combined_entry_type *p; | |
2180 | combined_entry_type *pend; | |
2181 | alent *l; | |
2182 | struct coff_section_tdata *sec_data; | |
2183 | ||
2184 | /* Before looking through the symbol table, try to use a .stab | |
2185 | section to find the information. */ | |
2186 | if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, | |
2187 | &found, filename_ptr, | |
2188 | functionname_ptr, line_ptr, | |
51db3708 | 2189 | &coff_data(abfd)->line_info)) |
252b5132 | 2190 | return false; |
51db3708 | 2191 | |
4ca29a6a NC |
2192 | if (found) |
2193 | return true; | |
2194 | ||
51db3708 NC |
2195 | /* Also try examining DWARF2 debugging information. */ |
2196 | if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset, | |
2197 | filename_ptr, functionname_ptr, | |
2198 | line_ptr, 0, | |
2199 | &coff_data(abfd)->dwarf2_find_line_info)) | |
2200 | return true; | |
2201 | ||
252b5132 RH |
2202 | *filename_ptr = 0; |
2203 | *functionname_ptr = 0; | |
2204 | *line_ptr = 0; | |
2205 | ||
2206 | /* Don't try and find line numbers in a non coff file */ | |
9bd09e22 | 2207 | if (!bfd_family_coff (abfd)) |
252b5132 RH |
2208 | return false; |
2209 | ||
2210 | if (cof == NULL) | |
2211 | return false; | |
2212 | ||
2213 | /* Find the first C_FILE symbol. */ | |
2214 | p = cof->raw_syments; | |
2215 | if (!p) | |
2216 | return false; | |
2217 | ||
2218 | pend = p + cof->raw_syment_count; | |
2219 | while (p < pend) | |
2220 | { | |
2221 | if (p->u.syment.n_sclass == C_FILE) | |
2222 | break; | |
2223 | p += 1 + p->u.syment.n_numaux; | |
2224 | } | |
2225 | ||
2226 | if (p < pend) | |
2227 | { | |
2228 | bfd_vma sec_vma; | |
2229 | bfd_vma maxdiff; | |
2230 | ||
2231 | /* Look through the C_FILE symbols to find the best one. */ | |
2232 | sec_vma = bfd_get_section_vma (abfd, section); | |
2233 | *filename_ptr = (char *) p->u.syment._n._n_n._n_offset; | |
2234 | maxdiff = (bfd_vma) 0 - (bfd_vma) 1; | |
2235 | while (1) | |
2236 | { | |
2237 | combined_entry_type *p2; | |
2238 | ||
2239 | for (p2 = p + 1 + p->u.syment.n_numaux; | |
2240 | p2 < pend; | |
2241 | p2 += 1 + p2->u.syment.n_numaux) | |
2242 | { | |
2243 | if (p2->u.syment.n_scnum > 0 | |
2244 | && (section | |
2245 | == coff_section_from_bfd_index (abfd, | |
2246 | p2->u.syment.n_scnum))) | |
2247 | break; | |
2248 | if (p2->u.syment.n_sclass == C_FILE) | |
2249 | { | |
2250 | p2 = pend; | |
2251 | break; | |
2252 | } | |
2253 | } | |
2254 | ||
798c1fb8 ILT |
2255 | /* We use <= MAXDIFF here so that if we get a zero length |
2256 | file, we actually use the next file entry. */ | |
252b5132 RH |
2257 | if (p2 < pend |
2258 | && offset + sec_vma >= (bfd_vma) p2->u.syment.n_value | |
798c1fb8 | 2259 | && offset + sec_vma - (bfd_vma) p2->u.syment.n_value <= maxdiff) |
252b5132 RH |
2260 | { |
2261 | *filename_ptr = (char *) p->u.syment._n._n_n._n_offset; | |
2262 | maxdiff = offset + sec_vma - p2->u.syment.n_value; | |
2263 | } | |
2264 | ||
2265 | /* Avoid endless loops on erroneous files by ensuring that | |
2266 | we always move forward in the file. */ | |
2267 | if (p - cof->raw_syments >= p->u.syment.n_value) | |
2268 | break; | |
2269 | ||
2270 | p = cof->raw_syments + p->u.syment.n_value; | |
2271 | if (p > pend || p->u.syment.n_sclass != C_FILE) | |
2272 | break; | |
2273 | } | |
2274 | } | |
2275 | ||
2276 | /* Now wander though the raw linenumbers of the section */ | |
2277 | /* If we have been called on this section before, and the offset we | |
2278 | want is further down then we can prime the lookup loop. */ | |
2279 | sec_data = coff_section_data (abfd, section); | |
2280 | if (sec_data != NULL | |
2281 | && sec_data->i > 0 | |
2282 | && offset >= sec_data->offset) | |
2283 | { | |
2284 | i = sec_data->i; | |
2285 | *functionname_ptr = sec_data->function; | |
2286 | line_base = sec_data->line_base; | |
2287 | } | |
2288 | else | |
2289 | { | |
2290 | i = 0; | |
2291 | line_base = 0; | |
2292 | } | |
2293 | ||
2294 | if (section->lineno != NULL) | |
2295 | { | |
798c1fb8 ILT |
2296 | bfd_vma last_value = 0; |
2297 | ||
252b5132 RH |
2298 | l = §ion->lineno[i]; |
2299 | ||
2300 | for (; i < section->lineno_count; i++) | |
2301 | { | |
2302 | if (l->line_number == 0) | |
2303 | { | |
2304 | /* Get the symbol this line number points at */ | |
2305 | coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym); | |
2306 | if (coff->symbol.value > offset) | |
2307 | break; | |
2308 | *functionname_ptr = coff->symbol.name; | |
798c1fb8 | 2309 | last_value = coff->symbol.value; |
252b5132 RH |
2310 | if (coff->native) |
2311 | { | |
2312 | combined_entry_type *s = coff->native; | |
2313 | s = s + 1 + s->u.syment.n_numaux; | |
2314 | ||
2315 | /* In XCOFF a debugging symbol can follow the | |
2316 | function symbol. */ | |
2317 | if (s->u.syment.n_scnum == N_DEBUG) | |
2318 | s = s + 1 + s->u.syment.n_numaux; | |
2319 | ||
2320 | /* S should now point to the .bf of the function. */ | |
2321 | if (s->u.syment.n_numaux) | |
2322 | { | |
2323 | /* The linenumber is stored in the auxent. */ | |
2324 | union internal_auxent *a = &((s + 1)->u.auxent); | |
2325 | line_base = a->x_sym.x_misc.x_lnsz.x_lnno; | |
2326 | *line_ptr = line_base; | |
2327 | } | |
2328 | } | |
2329 | } | |
2330 | else | |
2331 | { | |
2332 | if (l->u.offset > offset) | |
2333 | break; | |
2334 | *line_ptr = l->line_number + line_base - 1; | |
2335 | } | |
2336 | l++; | |
2337 | } | |
798c1fb8 ILT |
2338 | |
2339 | /* If we fell off the end of the loop, then assume that this | |
2340 | symbol has no line number info. Otherwise, symbols with no | |
2341 | line number info get reported with the line number of the | |
2342 | last line of the last symbol which does have line number | |
2343 | info. We use 0x100 as a slop to account for cases where the | |
2344 | last line has executable code. */ | |
2345 | if (i >= section->lineno_count | |
2346 | && last_value != 0 | |
2347 | && offset - last_value > 0x100) | |
2348 | { | |
2349 | *functionname_ptr = NULL; | |
2350 | *line_ptr = 0; | |
2351 | } | |
252b5132 RH |
2352 | } |
2353 | ||
2354 | /* Cache the results for the next call. */ | |
2355 | if (sec_data == NULL && section->owner == abfd) | |
2356 | { | |
2357 | section->used_by_bfd = | |
2358 | ((PTR) bfd_zalloc (abfd, | |
2359 | sizeof (struct coff_section_tdata))); | |
2360 | sec_data = (struct coff_section_tdata *) section->used_by_bfd; | |
2361 | } | |
2362 | if (sec_data != NULL) | |
2363 | { | |
2364 | sec_data->offset = offset; | |
2365 | sec_data->i = i; | |
2366 | sec_data->function = *functionname_ptr; | |
2367 | sec_data->line_base = line_base; | |
2368 | } | |
2369 | ||
2370 | return true; | |
2371 | } | |
2372 | ||
2373 | int | |
2374 | coff_sizeof_headers (abfd, reloc) | |
2375 | bfd *abfd; | |
2376 | boolean reloc; | |
2377 | { | |
2378 | size_t size; | |
2379 | ||
2380 | if (reloc == false) | |
2381 | { | |
2382 | size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd); | |
2383 | } | |
2384 | else | |
2385 | { | |
2386 | size = bfd_coff_filhsz (abfd); | |
2387 | } | |
2388 | ||
2389 | size += abfd->section_count * bfd_coff_scnhsz (abfd); | |
2390 | return size; | |
2391 | } | |
2392 | ||
2393 | /* Change the class of a coff symbol held by BFD. */ | |
2394 | boolean | |
2395 | bfd_coff_set_symbol_class (abfd, symbol, class) | |
2396 | bfd * abfd; | |
2397 | asymbol * symbol; | |
2398 | unsigned int class; | |
2399 | { | |
2400 | coff_symbol_type * csym; | |
2401 | ||
2402 | csym = coff_symbol_from (abfd, symbol); | |
2403 | if (csym == NULL) | |
2404 | { | |
2405 | bfd_set_error (bfd_error_invalid_operation); | |
2406 | return false; | |
2407 | } | |
2408 | else if (csym->native == NULL) | |
2409 | { | |
2410 | /* This is an alien symbol which no native coff backend data. | |
2411 | We cheat here by creating a fake native entry for it and | |
2412 | then filling in the class. This code is based on that in | |
2413 | coff_write_alien_symbol(). */ | |
244148ad | 2414 | |
252b5132 RH |
2415 | combined_entry_type * native; |
2416 | ||
2417 | native = (combined_entry_type *) bfd_alloc (abfd, sizeof (* native)); | |
2418 | if (native == NULL) | |
2419 | return false; | |
2420 | ||
2421 | memset (native, 0, sizeof (* native)); | |
244148ad | 2422 | |
252b5132 RH |
2423 | native->u.syment.n_type = T_NULL; |
2424 | native->u.syment.n_sclass = class; | |
244148ad | 2425 | |
252b5132 RH |
2426 | if (bfd_is_und_section (symbol->section)) |
2427 | { | |
2428 | native->u.syment.n_scnum = N_UNDEF; | |
2429 | native->u.syment.n_value = symbol->value; | |
2430 | } | |
2431 | else if (bfd_is_com_section (symbol->section)) | |
2432 | { | |
2433 | native->u.syment.n_scnum = N_UNDEF; | |
2434 | native->u.syment.n_value = symbol->value; | |
2435 | } | |
2436 | else | |
2437 | { | |
2438 | native->u.syment.n_scnum = | |
2439 | symbol->section->output_section->target_index; | |
2440 | native->u.syment.n_value = (symbol->value | |
2441 | + symbol->section->output_offset); | |
2442 | if (! obj_pe (abfd)) | |
2443 | native->u.syment.n_value += symbol->section->output_section->vma; | |
244148ad | 2444 | |
252b5132 RH |
2445 | /* Copy the any flags from the the file header into the symbol. |
2446 | FIXME: Why? */ | |
2447 | native->u.syment.n_flags = bfd_asymbol_bfd (& csym->symbol)->flags; | |
2448 | } | |
244148ad | 2449 | |
252b5132 RH |
2450 | csym->native = native; |
2451 | } | |
2452 | else | |
2453 | { | |
2454 | csym->native->u.syment.n_sclass = class; | |
2455 | } | |
244148ad | 2456 | |
252b5132 RH |
2457 | return true; |
2458 | } |