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