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