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