/src/binutils-gdb/bfd/coffgen.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Support for the generic parts of COFF, for BFD. |
2 | | Copyright (C) 1990-2023 Free Software Foundation, Inc. |
3 | | Written by Cygnus Support. |
4 | | |
5 | | This file is part of BFD, the Binary File Descriptor library. |
6 | | |
7 | | This program is free software; you can redistribute it and/or modify |
8 | | it under the terms of the GNU General Public License as published by |
9 | | the Free Software Foundation; either version 3 of the License, or |
10 | | (at your option) any later version. |
11 | | |
12 | | This program is distributed in the hope that it will be useful, |
13 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | GNU General Public License for more details. |
16 | | |
17 | | You should have received a copy of the GNU General Public License |
18 | | along with this program; if not, write to the Free Software |
19 | | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
20 | | MA 02110-1301, USA. */ |
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 | | |
39 | | #include "sysdep.h" |
40 | | #include <limits.h> |
41 | | #include "bfd.h" |
42 | | #include "libbfd.h" |
43 | | #include "coff/internal.h" |
44 | | #include "libcoff.h" |
45 | | #include "hashtab.h" |
46 | | |
47 | | /* Extract a long section name at STRINDEX and copy it to the bfd objstack. |
48 | | Return NULL in case of error. */ |
49 | | |
50 | | static char * |
51 | | extract_long_section_name(bfd *abfd, unsigned long strindex) |
52 | 4.73k | { |
53 | 4.73k | const char *strings; |
54 | 4.73k | char *name; |
55 | | |
56 | 4.73k | strings = _bfd_coff_read_string_table (abfd); |
57 | 4.73k | if (strings == NULL) |
58 | 2.50k | return NULL; |
59 | 2.22k | if ((bfd_size_type)(strindex + 2) >= obj_coff_strings_len (abfd)) |
60 | 1.04k | return NULL; |
61 | 1.18k | strings += strindex; |
62 | 1.18k | name = (char *) bfd_alloc (abfd, (bfd_size_type) strlen (strings) + 1); |
63 | 1.18k | if (name == NULL) |
64 | 0 | return NULL; |
65 | 1.18k | strcpy (name, strings); |
66 | | |
67 | 1.18k | return name; |
68 | 1.18k | } |
69 | | |
70 | | /* Decode a base 64 coded string at STR of length LEN, and write the result |
71 | | to RES. Return true on success. |
72 | | Return false in case of invalid character or overflow. */ |
73 | | |
74 | | static bool |
75 | | decode_base64 (const char *str, unsigned len, uint32_t *res) |
76 | 3.12k | { |
77 | 3.12k | unsigned i; |
78 | 3.12k | uint32_t val; |
79 | | |
80 | 3.12k | val = 0; |
81 | 10.1k | for (i = 0; i < len; i++) |
82 | 10.1k | { |
83 | 10.1k | char c = str[i]; |
84 | 10.1k | unsigned d; |
85 | | |
86 | 10.1k | if (c >= 'A' && c <= 'Z') |
87 | 815 | d = c - 'A'; |
88 | 9.28k | else if (c >= 'a' && c <= 'z') |
89 | 449 | d = c - 'a' + 26; |
90 | 8.83k | else if (c >= '0' && c <= '9') |
91 | 249 | d = c - '0' + 52; |
92 | 8.58k | else if (c == '+') |
93 | 2 | d = 62; |
94 | 8.58k | else if (c == '/') |
95 | 6.58k | d = 63; |
96 | 1.99k | else |
97 | 1.99k | return false; |
98 | | |
99 | | /* Check for overflow. */ |
100 | 8.10k | if ((val >> 26) != 0) |
101 | 1.12k | return false; |
102 | | |
103 | 6.97k | val = (val << 6) + d; |
104 | 6.97k | } |
105 | | |
106 | 0 | *res = val; |
107 | 0 | return true; |
108 | 3.12k | } |
109 | | |
110 | | /* Take a section header read from a coff file (in HOST byte order), |
111 | | and make a BFD "section" out of it. This is used by ECOFF. */ |
112 | | |
113 | | static bool |
114 | | make_a_section_from_file (bfd *abfd, |
115 | | struct internal_scnhdr *hdr, |
116 | | unsigned int target_index) |
117 | 95.6M | { |
118 | 95.6M | asection *newsect; |
119 | 95.6M | char *name; |
120 | 95.6M | bool result = true; |
121 | 95.6M | flagword flags; |
122 | | |
123 | 95.6M | name = NULL; |
124 | | |
125 | | /* Handle long section names as in PE. On reading, we want to |
126 | | accept long names if the format permits them at all, regardless |
127 | | of the current state of the flag that dictates if we would generate |
128 | | them in outputs; this construct checks if that is the case by |
129 | | attempting to set the flag, without changing its state; the call |
130 | | will fail for formats that do not support long names at all. */ |
131 | 95.6M | if (bfd_coff_set_long_section_names (abfd, bfd_coff_long_section_names (abfd)) |
132 | 95.6M | && hdr->s_name[0] == '/') |
133 | 28.0k | { |
134 | | /* Flag that this BFD uses long names, even though the format might |
135 | | expect them to be off by default. This won't directly affect the |
136 | | format of any output BFD created from this one, but the information |
137 | | can be used to decide what to do. */ |
138 | 28.0k | bfd_coff_set_long_section_names (abfd, true); |
139 | | |
140 | 28.0k | if (hdr->s_name[1] == '/') |
141 | 3.12k | { |
142 | | /* LLVM extension: the '/' is followed by another '/' and then by |
143 | | the index in the strtab encoded in base64 without NUL at the |
144 | | end. */ |
145 | 3.12k | uint32_t strindex; |
146 | | |
147 | | /* Decode the index. No overflow is expected as the string table |
148 | | length is at most 2^32 - 1 (the length is written on the first |
149 | | four bytes). |
150 | | Also, contrary to RFC 4648, all the characters must be decoded, |
151 | | there is no padding. */ |
152 | 3.12k | if (!decode_base64 (hdr->s_name + 2, SCNNMLEN - 2, &strindex)) |
153 | 3.12k | return false; |
154 | | |
155 | 0 | name = extract_long_section_name (abfd, strindex); |
156 | 0 | if (name == NULL) |
157 | 0 | return false; |
158 | 0 | } |
159 | 24.8k | else |
160 | 24.8k | { |
161 | | /* PE classic long section name. The '/' is followed by the index |
162 | | in the strtab. The index is formatted as a decimal string. */ |
163 | 24.8k | char buf[SCNNMLEN]; |
164 | 24.8k | long strindex; |
165 | 24.8k | char *p; |
166 | | |
167 | 24.8k | memcpy (buf, hdr->s_name + 1, SCNNMLEN - 1); |
168 | 24.8k | buf[SCNNMLEN - 1] = '\0'; |
169 | 24.8k | strindex = strtol (buf, &p, 10); |
170 | 24.8k | if (*p == '\0' && strindex >= 0) |
171 | 4.73k | { |
172 | 4.73k | name = extract_long_section_name (abfd, strindex); |
173 | 4.73k | if (name == NULL) |
174 | 3.55k | return false; |
175 | 4.73k | } |
176 | 24.8k | } |
177 | 28.0k | } |
178 | | |
179 | 95.6M | if (name == NULL) |
180 | 95.6M | { |
181 | | /* Assorted wastage to null-terminate the name, thanks AT&T! */ |
182 | 95.6M | name = (char *) bfd_alloc (abfd, |
183 | 95.6M | (bfd_size_type) sizeof (hdr->s_name) + 1 + 1); |
184 | 95.6M | if (name == NULL) |
185 | 0 | return false; |
186 | 95.6M | strncpy (name, (char *) &hdr->s_name[0], sizeof (hdr->s_name)); |
187 | 95.6M | name[sizeof (hdr->s_name)] = 0; |
188 | 95.6M | } |
189 | | |
190 | 95.6M | newsect = bfd_make_section_anyway (abfd, name); |
191 | 95.6M | if (newsect == NULL) |
192 | 0 | return false; |
193 | | |
194 | 95.6M | newsect->vma = hdr->s_vaddr; |
195 | 95.6M | newsect->lma = hdr->s_paddr; |
196 | 95.6M | newsect->size = hdr->s_size; |
197 | 95.6M | newsect->filepos = hdr->s_scnptr; |
198 | 95.6M | newsect->rel_filepos = hdr->s_relptr; |
199 | 95.6M | newsect->reloc_count = hdr->s_nreloc; |
200 | | |
201 | 95.6M | bfd_coff_set_alignment_hook (abfd, newsect, hdr); |
202 | | |
203 | 95.6M | newsect->line_filepos = hdr->s_lnnoptr; |
204 | | |
205 | 95.6M | newsect->lineno_count = hdr->s_nlnno; |
206 | 95.6M | newsect->userdata = NULL; |
207 | 95.6M | newsect->next = NULL; |
208 | 95.6M | newsect->target_index = target_index; |
209 | | |
210 | 95.6M | if (!bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name, newsect, &flags)) |
211 | 117k | result = false; |
212 | | |
213 | | /* At least on i386-coff, the line number count for a shared library |
214 | | section must be ignored. */ |
215 | 95.6M | if ((flags & SEC_COFF_SHARED_LIBRARY) != 0) |
216 | 20.3M | newsect->lineno_count = 0; |
217 | | |
218 | 95.6M | if (hdr->s_nreloc != 0) |
219 | 66.6M | flags |= SEC_RELOC; |
220 | | /* FIXME: should this check 'hdr->s_size > 0'. */ |
221 | 95.6M | if (hdr->s_scnptr != 0) |
222 | 74.4M | flags |= SEC_HAS_CONTENTS; |
223 | | |
224 | 95.6M | newsect->flags = flags; |
225 | | |
226 | | /* Compress/decompress DWARF debug sections. */ |
227 | 95.6M | if ((flags & SEC_DEBUGGING) != 0 |
228 | 95.6M | && (flags & SEC_HAS_CONTENTS) != 0 |
229 | 95.6M | && (startswith (name, ".debug_") |
230 | 1.69M | || startswith (name, ".zdebug_") |
231 | 1.69M | || startswith (name, ".gnu.debuglto_.debug_") |
232 | 1.69M | || startswith (name, ".gnu.linkonce.wi."))) |
233 | 11.3k | { |
234 | 11.3k | enum { nothing, compress, decompress } action = nothing; |
235 | | |
236 | 11.3k | if (bfd_is_section_compressed (abfd, newsect)) |
237 | 138 | { |
238 | | /* Compressed section. Check if we should decompress. */ |
239 | 138 | if ((abfd->flags & BFD_DECOMPRESS)) |
240 | 0 | action = decompress; |
241 | 138 | } |
242 | 11.1k | else |
243 | 11.1k | { |
244 | | /* Normal section. Check if we should compress. */ |
245 | 11.1k | if ((abfd->flags & BFD_COMPRESS) && newsect->size != 0) |
246 | 0 | action = compress; |
247 | 11.1k | } |
248 | | |
249 | 11.3k | if (action == compress) |
250 | 0 | { |
251 | 0 | if (!bfd_init_section_compress_status (abfd, newsect)) |
252 | 0 | { |
253 | 0 | _bfd_error_handler |
254 | | /* xgettext:c-format */ |
255 | 0 | (_("%pB: unable to compress section %s"), abfd, name); |
256 | 0 | return false; |
257 | 0 | } |
258 | 0 | } |
259 | 11.3k | else if (action == decompress) |
260 | 0 | { |
261 | 0 | if (!bfd_init_section_decompress_status (abfd, newsect)) |
262 | 0 | { |
263 | 0 | _bfd_error_handler |
264 | | /* xgettext:c-format */ |
265 | 0 | (_("%pB: unable to decompress section %s"), abfd, name); |
266 | 0 | return false; |
267 | 0 | } |
268 | 0 | if (abfd->is_linker_input |
269 | 0 | && name[1] == 'z') |
270 | 0 | { |
271 | | /* Rename section from .zdebug_* to .debug_* so that ld |
272 | | scripts will see this section as a debug section. */ |
273 | 0 | char *new_name = bfd_zdebug_name_to_debug (abfd, name); |
274 | 0 | if (new_name == NULL) |
275 | 0 | return false; |
276 | 0 | bfd_rename_section (newsect, new_name); |
277 | 0 | } |
278 | 0 | } |
279 | 11.3k | } |
280 | | |
281 | 95.6M | return result; |
282 | 95.6M | } |
283 | | |
284 | | void |
285 | | coff_object_cleanup (bfd *abfd) |
286 | 236k | { |
287 | 236k | if (bfd_family_coff (abfd) && bfd_get_format (abfd) == bfd_object) |
288 | 209k | { |
289 | 209k | struct coff_tdata *td = coff_data (abfd); |
290 | 209k | if (td != NULL) |
291 | 206k | { |
292 | 206k | if (td->section_by_index) |
293 | 0 | htab_delete (td->section_by_index); |
294 | 206k | if (td->section_by_target_index) |
295 | 0 | htab_delete (td->section_by_target_index); |
296 | 206k | } |
297 | 209k | } |
298 | 236k | } |
299 | | |
300 | | /* Read in a COFF object and make it into a BFD. This is used by |
301 | | ECOFF as well. */ |
302 | | bfd_cleanup |
303 | | coff_real_object_p (bfd *abfd, |
304 | | unsigned nscns, |
305 | | struct internal_filehdr *internal_f, |
306 | | struct internal_aouthdr *internal_a) |
307 | 483k | { |
308 | 483k | flagword oflags = abfd->flags; |
309 | 483k | bfd_vma ostart = bfd_get_start_address (abfd); |
310 | 483k | void * tdata; |
311 | 483k | void * tdata_save; |
312 | 483k | bfd_size_type readsize; /* Length of file_info. */ |
313 | 483k | unsigned int scnhsz; |
314 | 483k | char *external_sections; |
315 | | |
316 | 483k | if (!(internal_f->f_flags & F_RELFLG)) |
317 | 341k | abfd->flags |= HAS_RELOC; |
318 | 483k | if ((internal_f->f_flags & F_EXEC)) |
319 | 157k | abfd->flags |= EXEC_P; |
320 | 483k | if (!(internal_f->f_flags & F_LNNO)) |
321 | 377k | abfd->flags |= HAS_LINENO; |
322 | 483k | if (!(internal_f->f_flags & F_LSYMS)) |
323 | 323k | abfd->flags |= HAS_LOCALS; |
324 | | |
325 | | /* FIXME: How can we set D_PAGED correctly? */ |
326 | 483k | if ((internal_f->f_flags & F_EXEC) != 0) |
327 | 157k | abfd->flags |= D_PAGED; |
328 | | |
329 | 483k | abfd->symcount = internal_f->f_nsyms; |
330 | 483k | if (internal_f->f_nsyms) |
331 | 399k | abfd->flags |= HAS_SYMS; |
332 | | |
333 | 483k | if (internal_a != (struct internal_aouthdr *) NULL) |
334 | 174k | abfd->start_address = internal_a->entry; |
335 | 308k | else |
336 | 308k | abfd->start_address = 0; |
337 | | |
338 | | /* Set up the tdata area. ECOFF uses its own routine, and overrides |
339 | | abfd->flags. */ |
340 | 483k | tdata_save = abfd->tdata.any; |
341 | 483k | tdata = bfd_coff_mkobject_hook (abfd, (void *) internal_f, (void *) internal_a); |
342 | 483k | if (tdata == NULL) |
343 | 0 | goto fail2; |
344 | | |
345 | 483k | scnhsz = bfd_coff_scnhsz (abfd); |
346 | 483k | readsize = (bfd_size_type) nscns * scnhsz; |
347 | 483k | external_sections = (char *) _bfd_alloc_and_read (abfd, readsize, readsize); |
348 | 483k | if (!external_sections) |
349 | 19.2k | goto fail; |
350 | | |
351 | | /* Set the arch/mach *before* swapping in sections; section header swapping |
352 | | may depend on arch/mach info. */ |
353 | 464k | if (! bfd_coff_set_arch_mach_hook (abfd, (void *) internal_f)) |
354 | 548 | goto fail; |
355 | | |
356 | | /* Now copy data as required; construct all asections etc. */ |
357 | 463k | if (nscns != 0) |
358 | 457k | { |
359 | 457k | unsigned int i; |
360 | 95.9M | for (i = 0; i < nscns; i++) |
361 | 95.6M | { |
362 | 95.6M | struct internal_scnhdr tmp; |
363 | 95.6M | bfd_coff_swap_scnhdr_in (abfd, |
364 | 95.6M | (void *) (external_sections + i * scnhsz), |
365 | 95.6M | (void *) & tmp); |
366 | 95.6M | if (! make_a_section_from_file (abfd, &tmp, i + 1)) |
367 | 124k | goto fail; |
368 | 95.6M | } |
369 | 457k | } |
370 | | |
371 | 339k | _bfd_coff_free_symbols (abfd); |
372 | 339k | return coff_object_cleanup; |
373 | | |
374 | 144k | fail: |
375 | 144k | coff_object_cleanup (abfd); |
376 | 144k | _bfd_coff_free_symbols (abfd); |
377 | 144k | bfd_release (abfd, tdata); |
378 | 144k | fail2: |
379 | 144k | abfd->tdata.any = tdata_save; |
380 | 144k | abfd->flags = oflags; |
381 | 144k | abfd->start_address = ostart; |
382 | 144k | return NULL; |
383 | 144k | } |
384 | | |
385 | | /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is |
386 | | not a COFF file. This is also used by ECOFF. */ |
387 | | |
388 | | bfd_cleanup |
389 | | coff_object_p (bfd *abfd) |
390 | 21.2M | { |
391 | 21.2M | bfd_size_type filhsz; |
392 | 21.2M | bfd_size_type aoutsz; |
393 | 21.2M | unsigned int nscns; |
394 | 21.2M | void * filehdr; |
395 | 21.2M | struct internal_filehdr internal_f; |
396 | 21.2M | struct internal_aouthdr internal_a; |
397 | | |
398 | | /* Figure out how much to read. */ |
399 | 21.2M | filhsz = bfd_coff_filhsz (abfd); |
400 | 21.2M | aoutsz = bfd_coff_aoutsz (abfd); |
401 | | |
402 | 21.2M | filehdr = _bfd_alloc_and_read (abfd, filhsz, filhsz); |
403 | 21.2M | if (filehdr == NULL) |
404 | 367k | { |
405 | 367k | if (bfd_get_error () != bfd_error_system_call) |
406 | 363k | bfd_set_error (bfd_error_wrong_format); |
407 | 367k | return NULL; |
408 | 367k | } |
409 | 20.9M | bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f); |
410 | 20.9M | bfd_release (abfd, filehdr); |
411 | | |
412 | | /* The XCOFF format has two sizes for the f_opthdr. SMALL_AOUTSZ |
413 | | (less than aoutsz) used in object files and AOUTSZ (equal to |
414 | | aoutsz) in executables. The bfd_coff_swap_aouthdr_in function |
415 | | expects this header to be aoutsz bytes in length, so we use that |
416 | | value in the call to bfd_alloc below. But we must be careful to |
417 | | only read in f_opthdr bytes in the call to bfd_bread. We should |
418 | | also attempt to catch corrupt or non-COFF binaries with a strange |
419 | | value for f_opthdr. */ |
420 | 20.9M | if (! bfd_coff_bad_format_hook (abfd, &internal_f) |
421 | 20.9M | || internal_f.f_opthdr > aoutsz) |
422 | 20.6M | { |
423 | 20.6M | bfd_set_error (bfd_error_wrong_format); |
424 | 20.6M | return NULL; |
425 | 20.6M | } |
426 | 290k | nscns = internal_f.f_nscns; |
427 | | |
428 | 290k | if (internal_f.f_opthdr) |
429 | 68.1k | { |
430 | 68.1k | void * opthdr; |
431 | | |
432 | 68.1k | opthdr = _bfd_alloc_and_read (abfd, aoutsz, internal_f.f_opthdr); |
433 | 68.1k | if (opthdr == NULL) |
434 | 655 | return NULL; |
435 | | /* PR 17512: file: 11056-1136-0.004. */ |
436 | 67.5k | if (internal_f.f_opthdr < aoutsz) |
437 | 53.0k | memset (((char *) opthdr) + internal_f.f_opthdr, 0, |
438 | 53.0k | aoutsz - internal_f.f_opthdr); |
439 | | |
440 | 67.5k | bfd_coff_swap_aouthdr_in (abfd, opthdr, (void *) &internal_a); |
441 | 67.5k | bfd_release (abfd, opthdr); |
442 | 67.5k | } |
443 | | |
444 | 289k | return coff_real_object_p (abfd, nscns, &internal_f, |
445 | 289k | (internal_f.f_opthdr != 0 |
446 | 289k | ? &internal_a |
447 | 289k | : (struct internal_aouthdr *) NULL)); |
448 | 290k | } |
449 | | |
450 | | static hashval_t |
451 | | htab_hash_section_target_index (const void * entry) |
452 | 5.91M | { |
453 | 5.91M | const struct bfd_section * sec = entry; |
454 | 5.91M | return sec->target_index; |
455 | 5.91M | } |
456 | | |
457 | | static int |
458 | | htab_eq_section_target_index (const void * e1, const void * e2) |
459 | 5.91M | { |
460 | 5.91M | const struct bfd_section * sec1 = e1; |
461 | 5.91M | const struct bfd_section * sec2 = e2; |
462 | 5.91M | return sec1->target_index == sec2->target_index; |
463 | 5.91M | } |
464 | | |
465 | | /* Get the BFD section from a COFF symbol section number. */ |
466 | | |
467 | | asection * |
468 | | coff_section_from_bfd_index (bfd *abfd, int section_index) |
469 | 5.27M | { |
470 | 5.27M | if (section_index == N_ABS) |
471 | 118k | return bfd_abs_section_ptr; |
472 | 5.15M | if (section_index == N_UNDEF) |
473 | 532k | return bfd_und_section_ptr; |
474 | 4.62M | if (section_index == N_DEBUG) |
475 | 1.39k | return bfd_abs_section_ptr; |
476 | | |
477 | 4.62M | struct bfd_section *answer; |
478 | 4.62M | htab_t table = coff_data (abfd)->section_by_target_index; |
479 | | |
480 | 4.62M | if (!table) |
481 | 37.1k | { |
482 | 37.1k | table = htab_create (10, htab_hash_section_target_index, |
483 | 37.1k | htab_eq_section_target_index, NULL); |
484 | 37.1k | if (table == NULL) |
485 | 0 | return bfd_und_section_ptr; |
486 | 37.1k | coff_data (abfd)->section_by_target_index = table; |
487 | 37.1k | } |
488 | | |
489 | 4.62M | if (htab_elements (table) == 0) |
490 | 37.1k | { |
491 | 475k | for (answer = abfd->sections; answer; answer = answer->next) |
492 | 438k | { |
493 | 438k | void **slot = htab_find_slot (table, answer, INSERT); |
494 | 438k | if (slot == NULL) |
495 | 0 | return bfd_und_section_ptr; |
496 | 438k | *slot = answer; |
497 | 438k | } |
498 | 37.1k | } |
499 | | |
500 | 4.62M | struct bfd_section needle; |
501 | 4.62M | needle.target_index = section_index; |
502 | | |
503 | 4.62M | answer = htab_find (table, &needle); |
504 | 4.62M | if (answer != NULL) |
505 | 116k | return answer; |
506 | | |
507 | | /* Cover the unlikely case of sections added after the first call to |
508 | | this function. */ |
509 | 6.55G | for (answer = abfd->sections; answer; answer = answer->next) |
510 | 6.55G | if (answer->target_index == section_index) |
511 | 199k | { |
512 | 199k | void **slot = htab_find_slot (table, answer, INSERT); |
513 | 199k | if (slot != NULL) |
514 | 199k | *slot = answer; |
515 | 199k | return answer; |
516 | 199k | } |
517 | | |
518 | | /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a |
519 | | has a bad symbol table in biglitpow.o. */ |
520 | 4.30M | return bfd_und_section_ptr; |
521 | 4.50M | } |
522 | | |
523 | | /* Get the upper bound of a COFF symbol table. */ |
524 | | |
525 | | long |
526 | | coff_get_symtab_upper_bound (bfd *abfd) |
527 | 0 | { |
528 | 0 | if (!bfd_coff_slurp_symbol_table (abfd)) |
529 | 0 | return -1; |
530 | | |
531 | 0 | return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *)); |
532 | 0 | } |
533 | | |
534 | | /* Canonicalize a COFF symbol table. */ |
535 | | |
536 | | long |
537 | | coff_canonicalize_symtab (bfd *abfd, asymbol **alocation) |
538 | 0 | { |
539 | 0 | unsigned int counter; |
540 | 0 | coff_symbol_type *symbase; |
541 | 0 | coff_symbol_type **location = (coff_symbol_type **) alocation; |
542 | |
|
543 | 0 | if (!bfd_coff_slurp_symbol_table (abfd)) |
544 | 0 | return -1; |
545 | | |
546 | 0 | symbase = obj_symbols (abfd); |
547 | 0 | counter = bfd_get_symcount (abfd); |
548 | 0 | while (counter-- > 0) |
549 | 0 | *location++ = symbase++; |
550 | |
|
551 | 0 | *location = NULL; |
552 | |
|
553 | 0 | return bfd_get_symcount (abfd); |
554 | 0 | } |
555 | | |
556 | | /* Get the name of a symbol. The caller must pass in a buffer of size |
557 | | >= SYMNMLEN + 1. */ |
558 | | |
559 | | const char * |
560 | | _bfd_coff_internal_syment_name (bfd *abfd, |
561 | | const struct internal_syment *sym, |
562 | | char *buf) |
563 | 128k | { |
564 | | /* FIXME: It's not clear this will work correctly if sizeof |
565 | | (_n_zeroes) != 4. */ |
566 | 128k | if (sym->_n._n_n._n_zeroes != 0 |
567 | 128k | || sym->_n._n_n._n_offset == 0) |
568 | 107k | { |
569 | 107k | memcpy (buf, sym->_n._n_name, SYMNMLEN); |
570 | 107k | buf[SYMNMLEN] = '\0'; |
571 | 107k | return buf; |
572 | 107k | } |
573 | 20.9k | else |
574 | 20.9k | { |
575 | 20.9k | const char *strings; |
576 | | |
577 | 20.9k | BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE); |
578 | 20.9k | strings = obj_coff_strings (abfd); |
579 | 20.9k | if (strings == NULL) |
580 | 17.2k | { |
581 | 17.2k | strings = _bfd_coff_read_string_table (abfd); |
582 | 17.2k | if (strings == NULL) |
583 | 12.5k | return NULL; |
584 | 17.2k | } |
585 | 8.44k | if (sym->_n._n_n._n_offset >= obj_coff_strings_len (abfd)) |
586 | 6.54k | return NULL; |
587 | 1.89k | return strings + sym->_n._n_n._n_offset; |
588 | 8.44k | } |
589 | 128k | } |
590 | | |
591 | | /* Read in and swap the relocs. This returns a buffer holding the |
592 | | relocs for section SEC in file ABFD. If CACHE is TRUE and |
593 | | INTERNAL_RELOCS is NULL, the relocs read in will be saved in case |
594 | | the function is called again. If EXTERNAL_RELOCS is not NULL, it |
595 | | is a buffer large enough to hold the unswapped relocs. If |
596 | | INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold |
597 | | the swapped relocs. If REQUIRE_INTERNAL is TRUE, then the return |
598 | | value must be INTERNAL_RELOCS. The function returns NULL on error. */ |
599 | | |
600 | | struct internal_reloc * |
601 | | _bfd_coff_read_internal_relocs (bfd *abfd, |
602 | | asection *sec, |
603 | | bool cache, |
604 | | bfd_byte *external_relocs, |
605 | | bool require_internal, |
606 | | struct internal_reloc *internal_relocs) |
607 | 0 | { |
608 | 0 | bfd_size_type relsz; |
609 | 0 | bfd_byte *free_external = NULL; |
610 | 0 | struct internal_reloc *free_internal = NULL; |
611 | 0 | bfd_byte *erel; |
612 | 0 | bfd_byte *erel_end; |
613 | 0 | struct internal_reloc *irel; |
614 | 0 | bfd_size_type amt; |
615 | |
|
616 | 0 | if (sec->reloc_count == 0) |
617 | 0 | return internal_relocs; /* Nothing to do. */ |
618 | | |
619 | 0 | if (coff_section_data (abfd, sec) != NULL |
620 | 0 | && coff_section_data (abfd, sec)->relocs != NULL) |
621 | 0 | { |
622 | 0 | if (! require_internal) |
623 | 0 | return coff_section_data (abfd, sec)->relocs; |
624 | 0 | memcpy (internal_relocs, coff_section_data (abfd, sec)->relocs, |
625 | 0 | sec->reloc_count * sizeof (struct internal_reloc)); |
626 | 0 | return internal_relocs; |
627 | 0 | } |
628 | | |
629 | 0 | relsz = bfd_coff_relsz (abfd); |
630 | |
|
631 | 0 | amt = sec->reloc_count * relsz; |
632 | 0 | if (external_relocs == NULL) |
633 | 0 | { |
634 | 0 | free_external = (bfd_byte *) bfd_malloc (amt); |
635 | 0 | if (free_external == NULL) |
636 | 0 | goto error_return; |
637 | 0 | external_relocs = free_external; |
638 | 0 | } |
639 | | |
640 | 0 | if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0 |
641 | 0 | || bfd_bread (external_relocs, amt, abfd) != amt) |
642 | 0 | goto error_return; |
643 | | |
644 | 0 | if (internal_relocs == NULL) |
645 | 0 | { |
646 | 0 | amt = sec->reloc_count; |
647 | 0 | amt *= sizeof (struct internal_reloc); |
648 | 0 | free_internal = (struct internal_reloc *) bfd_malloc (amt); |
649 | 0 | if (free_internal == NULL) |
650 | 0 | goto error_return; |
651 | 0 | internal_relocs = free_internal; |
652 | 0 | } |
653 | | |
654 | | /* Swap in the relocs. */ |
655 | 0 | erel = external_relocs; |
656 | 0 | erel_end = erel + relsz * sec->reloc_count; |
657 | 0 | irel = internal_relocs; |
658 | 0 | for (; erel < erel_end; erel += relsz, irel++) |
659 | 0 | bfd_coff_swap_reloc_in (abfd, (void *) erel, (void *) irel); |
660 | |
|
661 | 0 | free (free_external); |
662 | 0 | free_external = NULL; |
663 | |
|
664 | 0 | if (cache && free_internal != NULL) |
665 | 0 | { |
666 | 0 | if (coff_section_data (abfd, sec) == NULL) |
667 | 0 | { |
668 | 0 | amt = sizeof (struct coff_section_tdata); |
669 | 0 | sec->used_by_bfd = bfd_zalloc (abfd, amt); |
670 | 0 | if (sec->used_by_bfd == NULL) |
671 | 0 | goto error_return; |
672 | 0 | coff_section_data (abfd, sec)->contents = NULL; |
673 | 0 | } |
674 | 0 | coff_section_data (abfd, sec)->relocs = free_internal; |
675 | 0 | } |
676 | | |
677 | 0 | return internal_relocs; |
678 | | |
679 | 0 | error_return: |
680 | 0 | free (free_external); |
681 | 0 | free (free_internal); |
682 | 0 | return NULL; |
683 | 0 | } |
684 | | |
685 | | /* Set lineno_count for the output sections of a COFF file. */ |
686 | | |
687 | | int |
688 | | coff_count_linenumbers (bfd *abfd) |
689 | 0 | { |
690 | 0 | unsigned int limit = bfd_get_symcount (abfd); |
691 | 0 | unsigned int i; |
692 | 0 | int total = 0; |
693 | 0 | asymbol **p; |
694 | 0 | asection *s; |
695 | |
|
696 | 0 | if (limit == 0) |
697 | 0 | { |
698 | | /* This may be from the backend linker, in which case the |
699 | | lineno_count in the sections is correct. */ |
700 | 0 | for (s = abfd->sections; s != NULL; s = s->next) |
701 | 0 | total += s->lineno_count; |
702 | 0 | return total; |
703 | 0 | } |
704 | | |
705 | 0 | for (s = abfd->sections; s != NULL; s = s->next) |
706 | 0 | BFD_ASSERT (s->lineno_count == 0); |
707 | |
|
708 | 0 | for (p = abfd->outsymbols, i = 0; i < limit; i++, p++) |
709 | 0 | { |
710 | 0 | asymbol *q_maybe = *p; |
711 | |
|
712 | 0 | if (bfd_asymbol_bfd (q_maybe) != NULL |
713 | 0 | && bfd_family_coff (bfd_asymbol_bfd (q_maybe))) |
714 | 0 | { |
715 | 0 | coff_symbol_type *q = coffsymbol (q_maybe); |
716 | | |
717 | | /* The AIX 4.1 compiler can sometimes generate line numbers |
718 | | attached to debugging symbols. We try to simply ignore |
719 | | those here. */ |
720 | 0 | if (q->lineno != NULL |
721 | 0 | && q->symbol.section->owner != NULL) |
722 | 0 | { |
723 | | /* This symbol has line numbers. Increment the owning |
724 | | section's linenumber count. */ |
725 | 0 | alent *l = q->lineno; |
726 | |
|
727 | 0 | do |
728 | 0 | { |
729 | 0 | asection * sec = q->symbol.section->output_section; |
730 | | |
731 | | /* Do not try to update fields in read-only sections. */ |
732 | 0 | if (! bfd_is_const_section (sec)) |
733 | 0 | sec->lineno_count ++; |
734 | |
|
735 | 0 | ++total; |
736 | 0 | ++l; |
737 | 0 | } |
738 | 0 | while (l->line_number != 0); |
739 | 0 | } |
740 | 0 | } |
741 | 0 | } |
742 | |
|
743 | 0 | return total; |
744 | 0 | } |
745 | | |
746 | | static void |
747 | | fixup_symbol_value (bfd *abfd, |
748 | | coff_symbol_type *coff_symbol_ptr, |
749 | | struct internal_syment *syment) |
750 | 0 | { |
751 | | /* Normalize the symbol flags. */ |
752 | 0 | if (coff_symbol_ptr->symbol.section |
753 | 0 | && bfd_is_com_section (coff_symbol_ptr->symbol.section)) |
754 | 0 | { |
755 | | /* A common symbol is undefined with a value. */ |
756 | 0 | syment->n_scnum = N_UNDEF; |
757 | 0 | syment->n_value = coff_symbol_ptr->symbol.value; |
758 | 0 | } |
759 | 0 | else if ((coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) != 0 |
760 | 0 | && (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING_RELOC) == 0) |
761 | 0 | { |
762 | 0 | syment->n_value = coff_symbol_ptr->symbol.value; |
763 | 0 | } |
764 | 0 | else if (bfd_is_und_section (coff_symbol_ptr->symbol.section)) |
765 | 0 | { |
766 | 0 | syment->n_scnum = N_UNDEF; |
767 | 0 | syment->n_value = 0; |
768 | 0 | } |
769 | | /* FIXME: Do we need to handle the absolute section here? */ |
770 | 0 | else |
771 | 0 | { |
772 | 0 | if (coff_symbol_ptr->symbol.section) |
773 | 0 | { |
774 | 0 | syment->n_scnum = |
775 | 0 | coff_symbol_ptr->symbol.section->output_section->target_index; |
776 | |
|
777 | 0 | syment->n_value = (coff_symbol_ptr->symbol.value |
778 | 0 | + coff_symbol_ptr->symbol.section->output_offset); |
779 | 0 | if (! obj_pe (abfd)) |
780 | 0 | { |
781 | 0 | syment->n_value += (syment->n_sclass == C_STATLAB) |
782 | 0 | ? coff_symbol_ptr->symbol.section->output_section->lma |
783 | 0 | : coff_symbol_ptr->symbol.section->output_section->vma; |
784 | 0 | } |
785 | 0 | } |
786 | 0 | else |
787 | 0 | { |
788 | 0 | BFD_ASSERT (0); |
789 | | /* This can happen, but I don't know why yet (steve@cygnus.com) */ |
790 | 0 | syment->n_scnum = N_ABS; |
791 | 0 | syment->n_value = coff_symbol_ptr->symbol.value; |
792 | 0 | } |
793 | 0 | } |
794 | 0 | } |
795 | | |
796 | | /* Run through all the symbols in the symbol table and work out what |
797 | | their indexes into the symbol table will be when output. |
798 | | |
799 | | Coff requires that each C_FILE symbol points to the next one in the |
800 | | chain, and that the last one points to the first external symbol. We |
801 | | do that here too. */ |
802 | | |
803 | | bool |
804 | | coff_renumber_symbols (bfd *bfd_ptr, int *first_undef) |
805 | 0 | { |
806 | 0 | unsigned int symbol_count = bfd_get_symcount (bfd_ptr); |
807 | 0 | asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols; |
808 | 0 | unsigned int native_index = 0; |
809 | 0 | struct internal_syment *last_file = NULL; |
810 | 0 | unsigned int symbol_index; |
811 | | |
812 | | /* COFF demands that undefined symbols come after all other symbols. |
813 | | Since we don't need to impose this extra knowledge on all our |
814 | | client programs, deal with that here. Sort the symbol table; |
815 | | just move the undefined symbols to the end, leaving the rest |
816 | | alone. The O'Reilly book says that defined global symbols come |
817 | | at the end before the undefined symbols, so we do that here as |
818 | | well. */ |
819 | | /* @@ Do we have some condition we could test for, so we don't always |
820 | | have to do this? I don't think relocatability is quite right, but |
821 | | I'm not certain. [raeburn:19920508.1711EST] */ |
822 | 0 | { |
823 | 0 | asymbol **newsyms; |
824 | 0 | unsigned int i; |
825 | 0 | bfd_size_type amt; |
826 | |
|
827 | 0 | amt = sizeof (asymbol *) * ((bfd_size_type) symbol_count + 1); |
828 | 0 | newsyms = (asymbol **) bfd_alloc (bfd_ptr, amt); |
829 | 0 | if (!newsyms) |
830 | 0 | return false; |
831 | 0 | bfd_ptr->outsymbols = newsyms; |
832 | 0 | for (i = 0; i < symbol_count; i++) |
833 | 0 | if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0 |
834 | 0 | || (!bfd_is_und_section (symbol_ptr_ptr[i]->section) |
835 | 0 | && !bfd_is_com_section (symbol_ptr_ptr[i]->section) |
836 | 0 | && ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) != 0 |
837 | 0 | || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK)) |
838 | 0 | == 0)))) |
839 | 0 | *newsyms++ = symbol_ptr_ptr[i]; |
840 | |
|
841 | 0 | for (i = 0; i < symbol_count; i++) |
842 | 0 | if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0 |
843 | 0 | && !bfd_is_und_section (symbol_ptr_ptr[i]->section) |
844 | 0 | && (bfd_is_com_section (symbol_ptr_ptr[i]->section) |
845 | 0 | || ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) == 0 |
846 | 0 | && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK)) |
847 | 0 | != 0)))) |
848 | 0 | *newsyms++ = symbol_ptr_ptr[i]; |
849 | |
|
850 | 0 | *first_undef = newsyms - bfd_ptr->outsymbols; |
851 | |
|
852 | 0 | for (i = 0; i < symbol_count; i++) |
853 | 0 | if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0 |
854 | 0 | && bfd_is_und_section (symbol_ptr_ptr[i]->section)) |
855 | 0 | *newsyms++ = symbol_ptr_ptr[i]; |
856 | 0 | *newsyms = (asymbol *) NULL; |
857 | 0 | symbol_ptr_ptr = bfd_ptr->outsymbols; |
858 | 0 | } |
859 | | |
860 | 0 | for (symbol_index = 0; symbol_index < symbol_count; symbol_index++) |
861 | 0 | { |
862 | 0 | coff_symbol_type *coff_symbol_ptr; |
863 | |
|
864 | 0 | coff_symbol_ptr = coff_symbol_from (symbol_ptr_ptr[symbol_index]); |
865 | 0 | symbol_ptr_ptr[symbol_index]->udata.i = symbol_index; |
866 | 0 | if (coff_symbol_ptr && coff_symbol_ptr->native) |
867 | 0 | { |
868 | 0 | combined_entry_type *s = coff_symbol_ptr->native; |
869 | 0 | int i; |
870 | |
|
871 | 0 | BFD_ASSERT (s->is_sym); |
872 | 0 | if (s->u.syment.n_sclass == C_FILE) |
873 | 0 | { |
874 | 0 | if (last_file != NULL) |
875 | 0 | last_file->n_value = native_index; |
876 | 0 | last_file = &(s->u.syment); |
877 | 0 | } |
878 | 0 | else |
879 | | /* Modify the symbol values according to their section and |
880 | | type. */ |
881 | 0 | fixup_symbol_value (bfd_ptr, coff_symbol_ptr, &(s->u.syment)); |
882 | |
|
883 | 0 | for (i = 0; i < s->u.syment.n_numaux + 1; i++) |
884 | 0 | s[i].offset = native_index++; |
885 | 0 | } |
886 | 0 | else |
887 | 0 | native_index++; |
888 | 0 | } |
889 | |
|
890 | 0 | obj_conv_table_size (bfd_ptr) = native_index; |
891 | |
|
892 | 0 | return true; |
893 | 0 | } |
894 | | |
895 | | /* Run thorough the symbol table again, and fix it so that all |
896 | | pointers to entries are changed to the entries' index in the output |
897 | | symbol table. */ |
898 | | |
899 | | void |
900 | | coff_mangle_symbols (bfd *bfd_ptr) |
901 | 0 | { |
902 | 0 | unsigned int symbol_count = bfd_get_symcount (bfd_ptr); |
903 | 0 | asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols; |
904 | 0 | unsigned int symbol_index; |
905 | |
|
906 | 0 | for (symbol_index = 0; symbol_index < symbol_count; symbol_index++) |
907 | 0 | { |
908 | 0 | coff_symbol_type *coff_symbol_ptr; |
909 | |
|
910 | 0 | coff_symbol_ptr = coff_symbol_from (symbol_ptr_ptr[symbol_index]); |
911 | 0 | if (coff_symbol_ptr && coff_symbol_ptr->native) |
912 | 0 | { |
913 | 0 | int i; |
914 | 0 | combined_entry_type *s = coff_symbol_ptr->native; |
915 | |
|
916 | 0 | BFD_ASSERT (s->is_sym); |
917 | 0 | if (s->fix_value) |
918 | 0 | { |
919 | | /* FIXME: We should use a union here. */ |
920 | 0 | s->u.syment.n_value = |
921 | 0 | (uintptr_t) ((combined_entry_type *) |
922 | 0 | (uintptr_t) s->u.syment.n_value)->offset; |
923 | 0 | s->fix_value = 0; |
924 | 0 | } |
925 | 0 | if (s->fix_line) |
926 | 0 | { |
927 | | /* The value is the offset into the line number entries |
928 | | for the symbol's section. On output, the symbol's |
929 | | section should be N_DEBUG. */ |
930 | 0 | s->u.syment.n_value = |
931 | 0 | (coff_symbol_ptr->symbol.section->output_section->line_filepos |
932 | 0 | + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr)); |
933 | 0 | coff_symbol_ptr->symbol.section = |
934 | 0 | coff_section_from_bfd_index (bfd_ptr, N_DEBUG); |
935 | 0 | BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING); |
936 | 0 | } |
937 | 0 | for (i = 0; i < s->u.syment.n_numaux; i++) |
938 | 0 | { |
939 | 0 | combined_entry_type *a = s + i + 1; |
940 | |
|
941 | 0 | BFD_ASSERT (! a->is_sym); |
942 | 0 | if (a->fix_tag) |
943 | 0 | { |
944 | 0 | a->u.auxent.x_sym.x_tagndx.u32 = |
945 | 0 | a->u.auxent.x_sym.x_tagndx.p->offset; |
946 | 0 | a->fix_tag = 0; |
947 | 0 | } |
948 | 0 | if (a->fix_end) |
949 | 0 | { |
950 | 0 | a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.u32 = |
951 | 0 | a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset; |
952 | 0 | a->fix_end = 0; |
953 | 0 | } |
954 | 0 | if (a->fix_scnlen) |
955 | 0 | { |
956 | 0 | a->u.auxent.x_csect.x_scnlen.u64 = |
957 | 0 | a->u.auxent.x_csect.x_scnlen.p->offset; |
958 | 0 | a->fix_scnlen = 0; |
959 | 0 | } |
960 | 0 | } |
961 | 0 | } |
962 | 0 | } |
963 | 0 | } |
964 | | |
965 | | static bool |
966 | | coff_write_auxent_fname (bfd *abfd, |
967 | | char *str, |
968 | | union internal_auxent *auxent, |
969 | | struct bfd_strtab_hash *strtab, |
970 | | bool hash) |
971 | 0 | { |
972 | 0 | unsigned int str_length = strlen (str); |
973 | 0 | unsigned int filnmlen = bfd_coff_filnmlen (abfd); |
974 | |
|
975 | 0 | if (bfd_coff_long_filenames (abfd)) |
976 | 0 | { |
977 | 0 | if (str_length <= filnmlen) |
978 | 0 | strncpy (auxent->x_file.x_n.x_fname, str, filnmlen); |
979 | 0 | else |
980 | 0 | { |
981 | 0 | bfd_size_type indx = _bfd_stringtab_add (strtab, str, hash, false); |
982 | |
|
983 | 0 | if (indx == (bfd_size_type) -1) |
984 | 0 | return false; |
985 | | |
986 | 0 | auxent->x_file.x_n.x_n.x_offset = STRING_SIZE_SIZE + indx; |
987 | 0 | auxent->x_file.x_n.x_n.x_zeroes = 0; |
988 | 0 | } |
989 | 0 | } |
990 | 0 | else |
991 | 0 | { |
992 | 0 | strncpy (auxent->x_file.x_n.x_fname, str, filnmlen); |
993 | 0 | if (str_length > filnmlen) |
994 | 0 | str[filnmlen] = '\0'; |
995 | 0 | } |
996 | | |
997 | 0 | return true; |
998 | 0 | } |
999 | | |
1000 | | static bool |
1001 | | coff_fix_symbol_name (bfd *abfd, |
1002 | | asymbol *symbol, |
1003 | | combined_entry_type *native, |
1004 | | struct bfd_strtab_hash *strtab, |
1005 | | bool hash, |
1006 | | asection **debug_string_section_p, |
1007 | | bfd_size_type *debug_string_size_p) |
1008 | 0 | { |
1009 | 0 | unsigned int name_length; |
1010 | 0 | char *name = (char *) (symbol->name); |
1011 | 0 | bfd_size_type indx; |
1012 | |
|
1013 | 0 | if (name == NULL) |
1014 | 0 | { |
1015 | | /* COFF symbols always have names, so we'll make one up. */ |
1016 | 0 | symbol->name = "strange"; |
1017 | 0 | name = (char *) symbol->name; |
1018 | 0 | } |
1019 | 0 | name_length = strlen (name); |
1020 | |
|
1021 | 0 | BFD_ASSERT (native->is_sym); |
1022 | 0 | if (native->u.syment.n_sclass == C_FILE |
1023 | 0 | && native->u.syment.n_numaux > 0) |
1024 | 0 | { |
1025 | 0 | if (bfd_coff_force_symnames_in_strings (abfd)) |
1026 | 0 | { |
1027 | 0 | indx = _bfd_stringtab_add (strtab, ".file", hash, false); |
1028 | 0 | if (indx == (bfd_size_type) -1) |
1029 | 0 | return false; |
1030 | | |
1031 | 0 | native->u.syment._n._n_n._n_offset = STRING_SIZE_SIZE + indx; |
1032 | 0 | native->u.syment._n._n_n._n_zeroes = 0; |
1033 | 0 | } |
1034 | 0 | else |
1035 | 0 | strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN); |
1036 | | |
1037 | 0 | BFD_ASSERT (! (native + 1)->is_sym); |
1038 | 0 | if (!coff_write_auxent_fname (abfd, name, &(native + 1)->u.auxent, |
1039 | 0 | strtab, hash)) |
1040 | 0 | return false; |
1041 | 0 | } |
1042 | 0 | else |
1043 | 0 | { |
1044 | 0 | if (name_length <= SYMNMLEN && !bfd_coff_force_symnames_in_strings (abfd)) |
1045 | | /* This name will fit into the symbol neatly. */ |
1046 | 0 | strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN); |
1047 | | |
1048 | 0 | else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment)) |
1049 | 0 | { |
1050 | 0 | indx = _bfd_stringtab_add (strtab, name, hash, false); |
1051 | 0 | if (indx == (bfd_size_type) -1) |
1052 | 0 | return false; |
1053 | | |
1054 | 0 | native->u.syment._n._n_n._n_offset = STRING_SIZE_SIZE + indx; |
1055 | 0 | native->u.syment._n._n_n._n_zeroes = 0; |
1056 | 0 | } |
1057 | 0 | else |
1058 | 0 | { |
1059 | 0 | file_ptr filepos; |
1060 | 0 | bfd_byte buf[4]; |
1061 | 0 | int prefix_len = bfd_coff_debug_string_prefix_length (abfd); |
1062 | | |
1063 | | /* This name should be written into the .debug section. For |
1064 | | some reason each name is preceded by a two byte length |
1065 | | and also followed by a null byte. FIXME: We assume that |
1066 | | the .debug section has already been created, and that it |
1067 | | is large enough. */ |
1068 | 0 | if (*debug_string_section_p == (asection *) NULL) |
1069 | 0 | *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug"); |
1070 | 0 | filepos = bfd_tell (abfd); |
1071 | 0 | if (prefix_len == 4) |
1072 | 0 | bfd_put_32 (abfd, (bfd_vma) (name_length + 1), buf); |
1073 | 0 | else |
1074 | 0 | bfd_put_16 (abfd, (bfd_vma) (name_length + 1), buf); |
1075 | |
|
1076 | 0 | if (!bfd_set_section_contents (abfd, |
1077 | 0 | *debug_string_section_p, |
1078 | 0 | (void *) buf, |
1079 | 0 | (file_ptr) *debug_string_size_p, |
1080 | 0 | (bfd_size_type) prefix_len) |
1081 | 0 | || !bfd_set_section_contents (abfd, |
1082 | 0 | *debug_string_section_p, |
1083 | 0 | (void *) symbol->name, |
1084 | 0 | (file_ptr) (*debug_string_size_p |
1085 | 0 | + prefix_len), |
1086 | 0 | (bfd_size_type) name_length + 1)) |
1087 | 0 | abort (); |
1088 | 0 | if (bfd_seek (abfd, filepos, SEEK_SET) != 0) |
1089 | 0 | abort (); |
1090 | 0 | native->u.syment._n._n_n._n_offset = |
1091 | 0 | *debug_string_size_p + prefix_len; |
1092 | 0 | native->u.syment._n._n_n._n_zeroes = 0; |
1093 | 0 | *debug_string_size_p += name_length + 1 + prefix_len; |
1094 | 0 | } |
1095 | 0 | } |
1096 | | |
1097 | 0 | return true; |
1098 | 0 | } |
1099 | | |
1100 | | /* We need to keep track of the symbol index so that when we write out |
1101 | | the relocs we can get the index for a symbol. This method is a |
1102 | | hack. FIXME. */ |
1103 | | |
1104 | 0 | #define set_index(symbol, idx) ((symbol)->udata.i = (idx)) |
1105 | | |
1106 | | /* Write a symbol out to a COFF file. */ |
1107 | | |
1108 | | static bool |
1109 | | coff_write_symbol (bfd *abfd, |
1110 | | asymbol *symbol, |
1111 | | combined_entry_type *native, |
1112 | | bfd_vma *written, |
1113 | | struct bfd_strtab_hash *strtab, |
1114 | | bool hash, |
1115 | | asection **debug_string_section_p, |
1116 | | bfd_size_type *debug_string_size_p) |
1117 | 0 | { |
1118 | 0 | unsigned int numaux = native->u.syment.n_numaux; |
1119 | 0 | int type = native->u.syment.n_type; |
1120 | 0 | int n_sclass = (int) native->u.syment.n_sclass; |
1121 | 0 | asection *output_section = symbol->section->output_section |
1122 | 0 | ? symbol->section->output_section |
1123 | 0 | : symbol->section; |
1124 | 0 | void * buf; |
1125 | 0 | bfd_size_type symesz; |
1126 | |
|
1127 | 0 | BFD_ASSERT (native->is_sym); |
1128 | |
|
1129 | 0 | if (native->u.syment.n_sclass == C_FILE) |
1130 | 0 | symbol->flags |= BSF_DEBUGGING; |
1131 | |
|
1132 | 0 | if (symbol->flags & BSF_DEBUGGING |
1133 | 0 | && bfd_is_abs_section (symbol->section)) |
1134 | 0 | native->u.syment.n_scnum = N_DEBUG; |
1135 | | |
1136 | 0 | else if (bfd_is_abs_section (symbol->section)) |
1137 | 0 | native->u.syment.n_scnum = N_ABS; |
1138 | | |
1139 | 0 | else if (bfd_is_und_section (symbol->section)) |
1140 | 0 | native->u.syment.n_scnum = N_UNDEF; |
1141 | | |
1142 | 0 | else |
1143 | 0 | native->u.syment.n_scnum = |
1144 | 0 | output_section->target_index; |
1145 | |
|
1146 | 0 | if (!coff_fix_symbol_name (abfd, symbol, native, strtab, hash, |
1147 | 0 | debug_string_section_p, debug_string_size_p)) |
1148 | 0 | return false; |
1149 | | |
1150 | 0 | symesz = bfd_coff_symesz (abfd); |
1151 | 0 | buf = bfd_alloc (abfd, symesz); |
1152 | 0 | if (!buf) |
1153 | 0 | return false; |
1154 | 0 | bfd_coff_swap_sym_out (abfd, &native->u.syment, buf); |
1155 | 0 | if (bfd_bwrite (buf, symesz, abfd) != symesz) |
1156 | 0 | return false; |
1157 | 0 | bfd_release (abfd, buf); |
1158 | |
|
1159 | 0 | if (native->u.syment.n_numaux > 0) |
1160 | 0 | { |
1161 | 0 | bfd_size_type auxesz; |
1162 | 0 | unsigned int j; |
1163 | |
|
1164 | 0 | auxesz = bfd_coff_auxesz (abfd); |
1165 | 0 | buf = bfd_alloc (abfd, auxesz); |
1166 | 0 | if (!buf) |
1167 | 0 | return false; |
1168 | 0 | for (j = 0; j < native->u.syment.n_numaux; j++) |
1169 | 0 | { |
1170 | 0 | BFD_ASSERT (! (native + j + 1)->is_sym); |
1171 | | |
1172 | | /* Adjust auxent only if this isn't the filename |
1173 | | auxiliary entry. */ |
1174 | 0 | if (native->u.syment.n_sclass == C_FILE |
1175 | 0 | && (native + j + 1)->u.auxent.x_file.x_ftype |
1176 | 0 | && (native + j + 1)->extrap) |
1177 | 0 | coff_write_auxent_fname (abfd, (char *) (native + j + 1)->extrap, |
1178 | 0 | &(native + j + 1)->u.auxent, strtab, hash); |
1179 | |
|
1180 | 0 | bfd_coff_swap_aux_out (abfd, |
1181 | 0 | &((native + j + 1)->u.auxent), |
1182 | 0 | type, n_sclass, (int) j, |
1183 | 0 | native->u.syment.n_numaux, |
1184 | 0 | buf); |
1185 | 0 | if (bfd_bwrite (buf, auxesz, abfd) != auxesz) |
1186 | 0 | return false; |
1187 | 0 | } |
1188 | 0 | bfd_release (abfd, buf); |
1189 | 0 | } |
1190 | | |
1191 | | /* Store the index for use when we write out the relocs. */ |
1192 | 0 | set_index (symbol, *written); |
1193 | |
|
1194 | 0 | *written += numaux + 1; |
1195 | 0 | return true; |
1196 | 0 | } |
1197 | | |
1198 | | /* Write out a symbol to a COFF file that does not come from a COFF |
1199 | | file originally. This symbol may have been created by the linker, |
1200 | | or we may be linking a non COFF file to a COFF file. */ |
1201 | | |
1202 | | bool |
1203 | | coff_write_alien_symbol (bfd *abfd, |
1204 | | asymbol *symbol, |
1205 | | struct internal_syment *isym, |
1206 | | bfd_vma *written, |
1207 | | struct bfd_strtab_hash *strtab, |
1208 | | bool hash, |
1209 | | asection **debug_string_section_p, |
1210 | | bfd_size_type *debug_string_size_p) |
1211 | 0 | { |
1212 | 0 | combined_entry_type *native; |
1213 | 0 | combined_entry_type dummy[2]; |
1214 | 0 | asection *output_section = symbol->section->output_section |
1215 | 0 | ? symbol->section->output_section |
1216 | 0 | : symbol->section; |
1217 | 0 | struct bfd_link_info *link_info = coff_data (abfd)->link_info; |
1218 | 0 | bool ret; |
1219 | |
|
1220 | 0 | if ((!link_info || link_info->strip_discarded) |
1221 | 0 | && !bfd_is_abs_section (symbol->section) |
1222 | 0 | && symbol->section->output_section == bfd_abs_section_ptr) |
1223 | 0 | { |
1224 | 0 | symbol->name = ""; |
1225 | 0 | if (isym != NULL) |
1226 | 0 | memset (isym, 0, sizeof (*isym)); |
1227 | 0 | return true; |
1228 | 0 | } |
1229 | 0 | memset (dummy, 0, sizeof dummy); |
1230 | 0 | native = dummy; |
1231 | 0 | native->is_sym = true; |
1232 | 0 | native[1].is_sym = false; |
1233 | 0 | native->u.syment.n_type = T_NULL; |
1234 | 0 | native->u.syment.n_flags = 0; |
1235 | 0 | native->u.syment.n_numaux = 0; |
1236 | 0 | if (bfd_is_und_section (symbol->section)) |
1237 | 0 | { |
1238 | 0 | native->u.syment.n_scnum = N_UNDEF; |
1239 | 0 | native->u.syment.n_value = symbol->value; |
1240 | 0 | } |
1241 | 0 | else if (bfd_is_com_section (symbol->section)) |
1242 | 0 | { |
1243 | 0 | native->u.syment.n_scnum = N_UNDEF; |
1244 | 0 | native->u.syment.n_value = symbol->value; |
1245 | 0 | } |
1246 | 0 | else if (symbol->flags & BSF_FILE) |
1247 | 0 | { |
1248 | 0 | native->u.syment.n_scnum = N_DEBUG; |
1249 | 0 | native->u.syment.n_numaux = 1; |
1250 | 0 | } |
1251 | 0 | else if (symbol->flags & BSF_DEBUGGING) |
1252 | 0 | { |
1253 | | /* There isn't much point to writing out a debugging symbol |
1254 | | unless we are prepared to convert it into COFF debugging |
1255 | | format. So, we just ignore them. We must clobber the symbol |
1256 | | name to keep it from being put in the string table. */ |
1257 | 0 | symbol->name = ""; |
1258 | 0 | if (isym != NULL) |
1259 | 0 | memset (isym, 0, sizeof (*isym)); |
1260 | 0 | return true; |
1261 | 0 | } |
1262 | 0 | else |
1263 | 0 | { |
1264 | 0 | native->u.syment.n_scnum = output_section->target_index; |
1265 | 0 | native->u.syment.n_value = (symbol->value |
1266 | 0 | + symbol->section->output_offset); |
1267 | 0 | if (! obj_pe (abfd)) |
1268 | 0 | native->u.syment.n_value += output_section->vma; |
1269 | | |
1270 | | /* Copy the any flags from the file header into the symbol. |
1271 | | FIXME: Why? */ |
1272 | 0 | { |
1273 | 0 | coff_symbol_type *c = coff_symbol_from (symbol); |
1274 | 0 | if (c != (coff_symbol_type *) NULL) |
1275 | 0 | native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags; |
1276 | 0 | } |
1277 | 0 | } |
1278 | | |
1279 | 0 | native->u.syment.n_type = 0; |
1280 | 0 | if (symbol->flags & BSF_FILE) |
1281 | 0 | native->u.syment.n_sclass = C_FILE; |
1282 | 0 | else if (symbol->flags & BSF_LOCAL) |
1283 | 0 | native->u.syment.n_sclass = C_STAT; |
1284 | 0 | else if (symbol->flags & BSF_WEAK) |
1285 | 0 | native->u.syment.n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT; |
1286 | 0 | else |
1287 | 0 | native->u.syment.n_sclass = C_EXT; |
1288 | |
|
1289 | 0 | ret = coff_write_symbol (abfd, symbol, native, written, strtab, hash, |
1290 | 0 | debug_string_section_p, debug_string_size_p); |
1291 | 0 | if (isym != NULL) |
1292 | 0 | *isym = native->u.syment; |
1293 | 0 | return ret; |
1294 | 0 | } |
1295 | | |
1296 | | /* Write a native symbol to a COFF file. */ |
1297 | | |
1298 | | static bool |
1299 | | coff_write_native_symbol (bfd *abfd, |
1300 | | coff_symbol_type *symbol, |
1301 | | bfd_vma *written, |
1302 | | struct bfd_strtab_hash *strtab, |
1303 | | asection **debug_string_section_p, |
1304 | | bfd_size_type *debug_string_size_p) |
1305 | 0 | { |
1306 | 0 | combined_entry_type *native = symbol->native; |
1307 | 0 | alent *lineno = symbol->lineno; |
1308 | 0 | struct bfd_link_info *link_info = coff_data (abfd)->link_info; |
1309 | |
|
1310 | 0 | if ((!link_info || link_info->strip_discarded) |
1311 | 0 | && !bfd_is_abs_section (symbol->symbol.section) |
1312 | 0 | && symbol->symbol.section->output_section == bfd_abs_section_ptr) |
1313 | 0 | { |
1314 | 0 | symbol->symbol.name = ""; |
1315 | 0 | return true; |
1316 | 0 | } |
1317 | | |
1318 | 0 | BFD_ASSERT (native->is_sym); |
1319 | | /* If this symbol has an associated line number, we must store the |
1320 | | symbol index in the line number field. We also tag the auxent to |
1321 | | point to the right place in the lineno table. */ |
1322 | 0 | if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL) |
1323 | 0 | { |
1324 | 0 | unsigned int count = 0; |
1325 | |
|
1326 | 0 | lineno[count].u.offset = *written; |
1327 | 0 | if (native->u.syment.n_numaux) |
1328 | 0 | { |
1329 | 0 | union internal_auxent *a = &((native + 1)->u.auxent); |
1330 | |
|
1331 | 0 | a->x_sym.x_fcnary.x_fcn.x_lnnoptr = |
1332 | 0 | symbol->symbol.section->output_section->moving_line_filepos; |
1333 | 0 | } |
1334 | | |
1335 | | /* Count and relocate all other linenumbers. */ |
1336 | 0 | count++; |
1337 | 0 | while (lineno[count].line_number != 0) |
1338 | 0 | { |
1339 | 0 | lineno[count].u.offset += |
1340 | 0 | (symbol->symbol.section->output_section->vma |
1341 | 0 | + symbol->symbol.section->output_offset); |
1342 | 0 | count++; |
1343 | 0 | } |
1344 | 0 | symbol->done_lineno = true; |
1345 | |
|
1346 | 0 | if (! bfd_is_const_section (symbol->symbol.section->output_section)) |
1347 | 0 | symbol->symbol.section->output_section->moving_line_filepos += |
1348 | 0 | count * bfd_coff_linesz (abfd); |
1349 | 0 | } |
1350 | |
|
1351 | 0 | return coff_write_symbol (abfd, &(symbol->symbol), native, written, |
1352 | 0 | strtab, true, debug_string_section_p, |
1353 | 0 | debug_string_size_p); |
1354 | 0 | } |
1355 | | |
1356 | | static void |
1357 | | null_error_handler (const char *fmt ATTRIBUTE_UNUSED, |
1358 | | va_list ap ATTRIBUTE_UNUSED) |
1359 | 0 | { |
1360 | 0 | } |
1361 | | |
1362 | | /* Write out the COFF symbols. */ |
1363 | | |
1364 | | bool |
1365 | | coff_write_symbols (bfd *abfd) |
1366 | 0 | { |
1367 | 0 | struct bfd_strtab_hash *strtab; |
1368 | 0 | asection *debug_string_section; |
1369 | 0 | bfd_size_type debug_string_size; |
1370 | 0 | unsigned int i; |
1371 | 0 | unsigned int limit = bfd_get_symcount (abfd); |
1372 | 0 | bfd_vma written = 0; |
1373 | 0 | asymbol **p; |
1374 | |
|
1375 | 0 | debug_string_section = NULL; |
1376 | 0 | debug_string_size = 0; |
1377 | |
|
1378 | 0 | strtab = _bfd_stringtab_init (); |
1379 | 0 | if (strtab == NULL) |
1380 | 0 | return false; |
1381 | | |
1382 | | /* If this target supports long section names, they must be put into |
1383 | | the string table. This is supported by PE. This code must |
1384 | | handle section names just as they are handled in |
1385 | | coff_write_object_contents. This is why we pass hash as FALSE below. */ |
1386 | 0 | if (bfd_coff_long_section_names (abfd)) |
1387 | 0 | { |
1388 | 0 | asection *o; |
1389 | |
|
1390 | 0 | for (o = abfd->sections; o != NULL; o = o->next) |
1391 | 0 | if (strlen (o->name) > SCNNMLEN |
1392 | 0 | && _bfd_stringtab_add (strtab, o->name, false, false) |
1393 | 0 | == (bfd_size_type) -1) |
1394 | 0 | return false; |
1395 | 0 | } |
1396 | | |
1397 | | /* Seek to the right place. */ |
1398 | 0 | if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0) |
1399 | 0 | return false; |
1400 | | |
1401 | | /* Output all the symbols we have. */ |
1402 | 0 | written = 0; |
1403 | 0 | for (p = abfd->outsymbols, i = 0; i < limit; i++, p++) |
1404 | 0 | { |
1405 | 0 | asymbol *symbol = *p; |
1406 | 0 | coff_symbol_type *c_symbol = coff_symbol_from (symbol); |
1407 | |
|
1408 | 0 | if (c_symbol == (coff_symbol_type *) NULL |
1409 | 0 | || c_symbol->native == (combined_entry_type *) NULL) |
1410 | 0 | { |
1411 | 0 | if (!coff_write_alien_symbol (abfd, symbol, NULL, &written, |
1412 | 0 | strtab, true, &debug_string_section, |
1413 | 0 | &debug_string_size)) |
1414 | 0 | return false; |
1415 | 0 | } |
1416 | 0 | else |
1417 | 0 | { |
1418 | 0 | if (coff_backend_info (abfd)->_bfd_coff_classify_symbol != NULL) |
1419 | 0 | { |
1420 | 0 | bfd_error_handler_type current_error_handler; |
1421 | 0 | enum coff_symbol_classification sym_class; |
1422 | 0 | unsigned char *n_sclass; |
1423 | | |
1424 | | /* Suppress error reporting by bfd_coff_classify_symbol. |
1425 | | Error messages can be generated when we are processing a local |
1426 | | symbol which has no associated section and we do not have to |
1427 | | worry about this, all we need to know is that it is local. */ |
1428 | 0 | current_error_handler = bfd_set_error_handler (null_error_handler); |
1429 | 0 | BFD_ASSERT (c_symbol->native->is_sym); |
1430 | 0 | sym_class = bfd_coff_classify_symbol (abfd, |
1431 | 0 | &c_symbol->native->u.syment); |
1432 | 0 | (void) bfd_set_error_handler (current_error_handler); |
1433 | |
|
1434 | 0 | n_sclass = &c_symbol->native->u.syment.n_sclass; |
1435 | | |
1436 | | /* If the symbol class has been changed (eg objcopy/ld script/etc) |
1437 | | we cannot retain the existing sclass from the original symbol. |
1438 | | Weak symbols only have one valid sclass, so just set it always. |
1439 | | If it is not local class and should be, set it C_STAT. |
1440 | | If it is global and not classified as global, or if it is |
1441 | | weak (which is also classified as global), set it C_EXT. */ |
1442 | |
|
1443 | 0 | if (symbol->flags & BSF_WEAK) |
1444 | 0 | *n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT; |
1445 | 0 | else if (symbol->flags & BSF_LOCAL && sym_class != COFF_SYMBOL_LOCAL) |
1446 | 0 | *n_sclass = C_STAT; |
1447 | 0 | else if (symbol->flags & BSF_GLOBAL |
1448 | 0 | && (sym_class != COFF_SYMBOL_GLOBAL |
1449 | | #ifdef COFF_WITH_PE |
1450 | | || *n_sclass == C_NT_WEAK |
1451 | | #endif |
1452 | 0 | || *n_sclass == C_WEAKEXT)) |
1453 | 0 | c_symbol->native->u.syment.n_sclass = C_EXT; |
1454 | 0 | } |
1455 | |
|
1456 | 0 | if (!coff_write_native_symbol (abfd, c_symbol, &written, |
1457 | 0 | strtab, &debug_string_section, |
1458 | 0 | &debug_string_size)) |
1459 | 0 | return false; |
1460 | 0 | } |
1461 | 0 | } |
1462 | | |
1463 | 0 | obj_raw_syment_count (abfd) = written; |
1464 | | |
1465 | | /* Now write out strings. |
1466 | | |
1467 | | We would normally not write anything here if there are no strings, but |
1468 | | we'll write out 4 so that any stupid coff reader which tries to read the |
1469 | | string table even when there isn't one won't croak. */ |
1470 | 0 | { |
1471 | 0 | bfd_byte buffer[STRING_SIZE_SIZE]; |
1472 | |
|
1473 | 0 | #if STRING_SIZE_SIZE == 4 |
1474 | 0 | H_PUT_32 (abfd, _bfd_stringtab_size (strtab) + STRING_SIZE_SIZE, buffer); |
1475 | | #else |
1476 | | #error Change H_PUT_32 |
1477 | | #endif |
1478 | 0 | if (bfd_bwrite ((void *) buffer, (bfd_size_type) sizeof (buffer), abfd) |
1479 | 0 | != sizeof (buffer)) |
1480 | 0 | return false; |
1481 | | |
1482 | 0 | if (! _bfd_stringtab_emit (abfd, strtab)) |
1483 | 0 | return false; |
1484 | 0 | } |
1485 | | |
1486 | 0 | _bfd_stringtab_free (strtab); |
1487 | | |
1488 | | /* Make sure the .debug section was created to be the correct size. |
1489 | | We should create it ourselves on the fly, but we don't because |
1490 | | BFD won't let us write to any section until we know how large all |
1491 | | the sections are. We could still do it by making another pass |
1492 | | over the symbols. FIXME. */ |
1493 | 0 | BFD_ASSERT (debug_string_size == 0 |
1494 | 0 | || (debug_string_section != (asection *) NULL |
1495 | 0 | && (BFD_ALIGN (debug_string_size, |
1496 | 0 | 1 << debug_string_section->alignment_power) |
1497 | 0 | == debug_string_section->size))); |
1498 | |
|
1499 | 0 | return true; |
1500 | 0 | } |
1501 | | |
1502 | | bool |
1503 | | coff_write_linenumbers (bfd *abfd) |
1504 | 0 | { |
1505 | 0 | asection *s; |
1506 | 0 | bfd_size_type linesz; |
1507 | 0 | void * buff; |
1508 | |
|
1509 | 0 | linesz = bfd_coff_linesz (abfd); |
1510 | 0 | buff = bfd_alloc (abfd, linesz); |
1511 | 0 | if (!buff) |
1512 | 0 | return false; |
1513 | 0 | for (s = abfd->sections; s != (asection *) NULL; s = s->next) |
1514 | 0 | { |
1515 | 0 | if (s->lineno_count) |
1516 | 0 | { |
1517 | 0 | asymbol **q = abfd->outsymbols; |
1518 | 0 | if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0) |
1519 | 0 | return false; |
1520 | | /* Find all the linenumbers in this section. */ |
1521 | 0 | while (*q) |
1522 | 0 | { |
1523 | 0 | asymbol *p = *q; |
1524 | 0 | if (p->section->output_section == s) |
1525 | 0 | { |
1526 | 0 | alent *l = |
1527 | 0 | BFD_SEND (bfd_asymbol_bfd (p), _get_lineno, |
1528 | 0 | (bfd_asymbol_bfd (p), p)); |
1529 | 0 | if (l) |
1530 | 0 | { |
1531 | | /* Found a linenumber entry, output. */ |
1532 | 0 | struct internal_lineno out; |
1533 | |
|
1534 | 0 | memset ((void *) & out, 0, sizeof (out)); |
1535 | 0 | out.l_lnno = 0; |
1536 | 0 | out.l_addr.l_symndx = l->u.offset; |
1537 | 0 | bfd_coff_swap_lineno_out (abfd, &out, buff); |
1538 | 0 | if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd) |
1539 | 0 | != linesz) |
1540 | 0 | return false; |
1541 | 0 | l++; |
1542 | 0 | while (l->line_number) |
1543 | 0 | { |
1544 | 0 | out.l_lnno = l->line_number; |
1545 | 0 | out.l_addr.l_symndx = l->u.offset; |
1546 | 0 | bfd_coff_swap_lineno_out (abfd, &out, buff); |
1547 | 0 | if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd) |
1548 | 0 | != linesz) |
1549 | 0 | return false; |
1550 | 0 | l++; |
1551 | 0 | } |
1552 | 0 | } |
1553 | 0 | } |
1554 | 0 | q++; |
1555 | 0 | } |
1556 | 0 | } |
1557 | 0 | } |
1558 | 0 | bfd_release (abfd, buff); |
1559 | 0 | return true; |
1560 | 0 | } |
1561 | | |
1562 | | alent * |
1563 | | coff_get_lineno (bfd *ignore_abfd ATTRIBUTE_UNUSED, asymbol *symbol) |
1564 | 0 | { |
1565 | 0 | return coffsymbol (symbol)->lineno; |
1566 | 0 | } |
1567 | | |
1568 | | /* This function transforms the offsets into the symbol table into |
1569 | | pointers to syments. */ |
1570 | | |
1571 | | static void |
1572 | | coff_pointerize_aux (bfd *abfd, |
1573 | | combined_entry_type *table_base, |
1574 | | combined_entry_type *symbol, |
1575 | | unsigned int indaux, |
1576 | | combined_entry_type *auxent) |
1577 | 0 | { |
1578 | 0 | unsigned int type = symbol->u.syment.n_type; |
1579 | 0 | unsigned int n_sclass = symbol->u.syment.n_sclass; |
1580 | |
|
1581 | 0 | BFD_ASSERT (symbol->is_sym); |
1582 | 0 | if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook) |
1583 | 0 | { |
1584 | 0 | if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook) |
1585 | 0 | (abfd, table_base, symbol, indaux, auxent)) |
1586 | 0 | return; |
1587 | 0 | } |
1588 | | |
1589 | | /* Don't bother if this is a file or a section. */ |
1590 | 0 | if (n_sclass == C_STAT && type == T_NULL) |
1591 | 0 | return; |
1592 | 0 | if (n_sclass == C_FILE) |
1593 | 0 | return; |
1594 | 0 | if (n_sclass == C_DWARF) |
1595 | 0 | return; |
1596 | | |
1597 | 0 | BFD_ASSERT (! auxent->is_sym); |
1598 | | /* Otherwise patch up. */ |
1599 | 0 | #define N_TMASK coff_data (abfd)->local_n_tmask |
1600 | 0 | #define N_BTSHFT coff_data (abfd)->local_n_btshft |
1601 | |
|
1602 | 0 | if ((ISFCN (type) || ISTAG (n_sclass) || n_sclass == C_BLOCK |
1603 | 0 | || n_sclass == C_FCN) |
1604 | 0 | && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.u32 > 0 |
1605 | 0 | && (auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.u32 |
1606 | 0 | < obj_raw_syment_count (abfd))) |
1607 | 0 | { |
1608 | 0 | auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p = |
1609 | 0 | table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.u32; |
1610 | 0 | auxent->fix_end = 1; |
1611 | 0 | } |
1612 | | |
1613 | | /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can |
1614 | | generate one, so we must be careful to ignore it. */ |
1615 | 0 | if (auxent->u.auxent.x_sym.x_tagndx.u32 < obj_raw_syment_count (abfd)) |
1616 | 0 | { |
1617 | 0 | auxent->u.auxent.x_sym.x_tagndx.p = |
1618 | 0 | table_base + auxent->u.auxent.x_sym.x_tagndx.u32; |
1619 | 0 | auxent->fix_tag = 1; |
1620 | 0 | } |
1621 | 0 | } |
1622 | | |
1623 | | /* Allocate space for the ".debug" section, and read it. |
1624 | | We did not read the debug section until now, because |
1625 | | we didn't want to go to the trouble until someone needed it. */ |
1626 | | |
1627 | | static char * |
1628 | | build_debug_section (bfd *abfd, asection ** sect_return) |
1629 | 0 | { |
1630 | 0 | char *debug_section; |
1631 | 0 | file_ptr position; |
1632 | 0 | bfd_size_type sec_size; |
1633 | |
|
1634 | 0 | asection *sect = bfd_get_section_by_name (abfd, ".debug"); |
1635 | |
|
1636 | 0 | if (!sect) |
1637 | 0 | { |
1638 | 0 | bfd_set_error (bfd_error_no_debug_section); |
1639 | 0 | return NULL; |
1640 | 0 | } |
1641 | | |
1642 | | /* Seek to the beginning of the `.debug' section and read it. |
1643 | | Save the current position first; it is needed by our caller. |
1644 | | Then read debug section and reset the file pointer. */ |
1645 | | |
1646 | 0 | position = bfd_tell (abfd); |
1647 | 0 | if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0) |
1648 | 0 | return NULL; |
1649 | | |
1650 | 0 | sec_size = sect->size; |
1651 | 0 | debug_section = (char *) _bfd_alloc_and_read (abfd, sec_size + 1, sec_size); |
1652 | 0 | if (debug_section == NULL) |
1653 | 0 | return NULL; |
1654 | 0 | debug_section[sec_size] = 0; |
1655 | |
|
1656 | 0 | if (bfd_seek (abfd, position, SEEK_SET) != 0) |
1657 | 0 | return NULL; |
1658 | | |
1659 | 0 | * sect_return = sect; |
1660 | 0 | return debug_section; |
1661 | 0 | } |
1662 | | |
1663 | | /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be |
1664 | | \0-terminated, but will not exceed 'maxlen' characters. The copy *will* |
1665 | | be \0-terminated. */ |
1666 | | |
1667 | | static char * |
1668 | | copy_name (bfd *abfd, char *name, size_t maxlen) |
1669 | 0 | { |
1670 | 0 | size_t len; |
1671 | 0 | char *newname; |
1672 | |
|
1673 | 0 | for (len = 0; len < maxlen; ++len) |
1674 | 0 | if (name[len] == '\0') |
1675 | 0 | break; |
1676 | |
|
1677 | 0 | if ((newname = (char *) bfd_alloc (abfd, (bfd_size_type) len + 1)) == NULL) |
1678 | 0 | return NULL; |
1679 | | |
1680 | 0 | strncpy (newname, name, len); |
1681 | 0 | newname[len] = '\0'; |
1682 | 0 | return newname; |
1683 | 0 | } |
1684 | | |
1685 | | /* Read in the external symbols. */ |
1686 | | |
1687 | | bool |
1688 | | _bfd_coff_get_external_symbols (bfd *abfd) |
1689 | 184k | { |
1690 | 184k | size_t symesz; |
1691 | 184k | size_t size; |
1692 | 184k | void * syms; |
1693 | 184k | ufile_ptr filesize; |
1694 | | |
1695 | 184k | if (obj_coff_external_syms (abfd) != NULL) |
1696 | 39.1k | return true; |
1697 | | |
1698 | 145k | symesz = bfd_coff_symesz (abfd); |
1699 | 145k | if (_bfd_mul_overflow (obj_raw_syment_count (abfd), symesz, &size)) |
1700 | 5.94k | { |
1701 | 5.94k | bfd_set_error (bfd_error_file_truncated); |
1702 | 5.94k | return false; |
1703 | 5.94k | } |
1704 | | |
1705 | 139k | if (size == 0) |
1706 | 24.7k | return true; |
1707 | | |
1708 | 114k | filesize = bfd_get_file_size (abfd); |
1709 | 114k | if (filesize != 0 |
1710 | 114k | && ((ufile_ptr) obj_sym_filepos (abfd) > filesize |
1711 | 114k | || size > filesize - obj_sym_filepos (abfd))) |
1712 | 48.1k | { |
1713 | 48.1k | bfd_set_error (bfd_error_file_truncated); |
1714 | 48.1k | return false; |
1715 | 48.1k | } |
1716 | | |
1717 | 66.5k | if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0) |
1718 | 0 | return false; |
1719 | 66.5k | syms = _bfd_malloc_and_read (abfd, size, size); |
1720 | 66.5k | obj_coff_external_syms (abfd) = syms; |
1721 | 66.5k | return syms != NULL; |
1722 | 66.5k | } |
1723 | | |
1724 | | /* Read in the external strings. The strings are not loaded until |
1725 | | they are needed. This is because we have no simple way of |
1726 | | detecting a missing string table in an archive. If the strings |
1727 | | are loaded then the STRINGS and STRINGS_LEN fields in the |
1728 | | coff_tdata structure will be set. */ |
1729 | | |
1730 | | const char * |
1731 | | _bfd_coff_read_string_table (bfd *abfd) |
1732 | 21.9k | { |
1733 | 21.9k | char extstrsize[STRING_SIZE_SIZE]; |
1734 | 21.9k | bfd_size_type strsize; |
1735 | 21.9k | char *strings; |
1736 | 21.9k | ufile_ptr pos; |
1737 | 21.9k | ufile_ptr filesize; |
1738 | 21.9k | size_t symesz; |
1739 | 21.9k | size_t size; |
1740 | | |
1741 | 21.9k | if (obj_coff_strings (abfd) != NULL) |
1742 | 879 | return obj_coff_strings (abfd); |
1743 | | |
1744 | 21.0k | if (obj_sym_filepos (abfd) == 0) |
1745 | 501 | { |
1746 | 501 | bfd_set_error (bfd_error_no_symbols); |
1747 | 501 | return NULL; |
1748 | 501 | } |
1749 | | |
1750 | 20.5k | symesz = bfd_coff_symesz (abfd); |
1751 | 20.5k | pos = obj_sym_filepos (abfd); |
1752 | 20.5k | if (_bfd_mul_overflow (obj_raw_syment_count (abfd), symesz, &size) |
1753 | 20.5k | || pos + size < pos) |
1754 | 139 | { |
1755 | 139 | bfd_set_error (bfd_error_file_truncated); |
1756 | 139 | return NULL; |
1757 | 139 | } |
1758 | | |
1759 | 20.4k | if (bfd_seek (abfd, pos + size, SEEK_SET) != 0) |
1760 | 0 | return NULL; |
1761 | | |
1762 | 20.4k | if (bfd_bread (extstrsize, (bfd_size_type) sizeof extstrsize, abfd) |
1763 | 20.4k | != sizeof extstrsize) |
1764 | 4.81k | { |
1765 | 4.81k | if (bfd_get_error () != bfd_error_file_truncated) |
1766 | 644 | return NULL; |
1767 | | |
1768 | | /* There is no string table. */ |
1769 | 4.17k | strsize = STRING_SIZE_SIZE; |
1770 | 4.17k | } |
1771 | 15.6k | else |
1772 | 15.6k | { |
1773 | 15.6k | #if STRING_SIZE_SIZE == 4 |
1774 | 15.6k | strsize = H_GET_32 (abfd, extstrsize); |
1775 | | #else |
1776 | | #error Change H_GET_32 |
1777 | | #endif |
1778 | 15.6k | } |
1779 | | |
1780 | 19.7k | filesize = bfd_get_file_size (abfd); |
1781 | 19.7k | if (strsize < STRING_SIZE_SIZE |
1782 | 19.7k | || (filesize != 0 && strsize > filesize)) |
1783 | 11.2k | { |
1784 | 11.2k | _bfd_error_handler |
1785 | | /* xgettext: c-format */ |
1786 | 11.2k | (_("%pB: bad string table size %" PRIu64), abfd, (uint64_t) strsize); |
1787 | 11.2k | bfd_set_error (bfd_error_bad_value); |
1788 | 11.2k | return NULL; |
1789 | 11.2k | } |
1790 | | |
1791 | 8.57k | strings = (char *) bfd_malloc (strsize + 1); |
1792 | 8.57k | if (strings == NULL) |
1793 | 0 | return NULL; |
1794 | | |
1795 | | /* PR 17521 file: 079-54929-0.004. |
1796 | | A corrupt file could contain an index that points into the first |
1797 | | STRING_SIZE_SIZE bytes of the string table, so make sure that |
1798 | | they are zero. */ |
1799 | 8.57k | memset (strings, 0, STRING_SIZE_SIZE); |
1800 | | |
1801 | 8.57k | if (bfd_bread (strings + STRING_SIZE_SIZE, strsize - STRING_SIZE_SIZE, abfd) |
1802 | 8.57k | != strsize - STRING_SIZE_SIZE) |
1803 | 2.53k | { |
1804 | 2.53k | free (strings); |
1805 | 2.53k | return NULL; |
1806 | 2.53k | } |
1807 | | |
1808 | 6.03k | obj_coff_strings (abfd) = strings; |
1809 | 6.03k | obj_coff_strings_len (abfd) = strsize; |
1810 | | /* Terminate the string table, just in case. */ |
1811 | 6.03k | strings[strsize] = 0; |
1812 | 6.03k | return strings; |
1813 | 8.57k | } |
1814 | | |
1815 | | /* Free up the external symbols and strings read from a COFF file. */ |
1816 | | |
1817 | | bool |
1818 | | _bfd_coff_free_symbols (bfd *abfd) |
1819 | 723k | { |
1820 | 723k | if (! bfd_family_coff (abfd)) |
1821 | 18.3k | return false; |
1822 | | |
1823 | 704k | if (obj_coff_external_syms (abfd) != NULL |
1824 | 704k | && ! obj_coff_keep_syms (abfd)) |
1825 | 59.9k | { |
1826 | 59.9k | free (obj_coff_external_syms (abfd)); |
1827 | 59.9k | obj_coff_external_syms (abfd) = NULL; |
1828 | 59.9k | } |
1829 | | |
1830 | 704k | if (obj_coff_strings (abfd) != NULL |
1831 | 704k | && ! obj_coff_keep_strings (abfd)) |
1832 | 6.03k | { |
1833 | 6.03k | free (obj_coff_strings (abfd)); |
1834 | 6.03k | obj_coff_strings (abfd) = NULL; |
1835 | 6.03k | obj_coff_strings_len (abfd) = 0; |
1836 | 6.03k | } |
1837 | | |
1838 | 704k | return true; |
1839 | 723k | } |
1840 | | |
1841 | | /* Read a symbol table into freshly bfd_allocated memory, swap it, and |
1842 | | knit the symbol names into a normalized form. By normalized here I |
1843 | | mean that all symbols have an n_offset pointer that points to a null- |
1844 | | terminated string. */ |
1845 | | |
1846 | | combined_entry_type * |
1847 | | coff_get_normalized_symtab (bfd *abfd) |
1848 | 0 | { |
1849 | 0 | combined_entry_type *internal; |
1850 | 0 | combined_entry_type *internal_ptr; |
1851 | 0 | combined_entry_type *symbol_ptr; |
1852 | 0 | combined_entry_type *internal_end; |
1853 | 0 | size_t symesz; |
1854 | 0 | char *raw_src; |
1855 | 0 | char *raw_end; |
1856 | 0 | const char *string_table = NULL; |
1857 | 0 | asection * debug_sec = NULL; |
1858 | 0 | char *debug_sec_data = NULL; |
1859 | 0 | bfd_size_type size; |
1860 | |
|
1861 | 0 | if (obj_raw_syments (abfd) != NULL) |
1862 | 0 | return obj_raw_syments (abfd); |
1863 | | |
1864 | 0 | if (! _bfd_coff_get_external_symbols (abfd)) |
1865 | 0 | return NULL; |
1866 | | |
1867 | 0 | size = obj_raw_syment_count (abfd); |
1868 | | /* Check for integer overflow. */ |
1869 | 0 | if (size > (bfd_size_type) -1 / sizeof (combined_entry_type)) |
1870 | 0 | return NULL; |
1871 | 0 | size *= sizeof (combined_entry_type); |
1872 | 0 | internal = (combined_entry_type *) bfd_zalloc (abfd, size); |
1873 | 0 | if (internal == NULL && size != 0) |
1874 | 0 | return NULL; |
1875 | 0 | internal_end = internal + obj_raw_syment_count (abfd); |
1876 | |
|
1877 | 0 | raw_src = (char *) obj_coff_external_syms (abfd); |
1878 | | |
1879 | | /* Mark the end of the symbols. */ |
1880 | 0 | symesz = bfd_coff_symesz (abfd); |
1881 | 0 | raw_end = PTR_ADD (raw_src, obj_raw_syment_count (abfd) * symesz); |
1882 | | |
1883 | | /* FIXME SOMEDAY. A string table size of zero is very weird, but |
1884 | | probably possible. If one shows up, it will probably kill us. */ |
1885 | | |
1886 | | /* Swap all the raw entries. */ |
1887 | 0 | for (internal_ptr = internal; |
1888 | 0 | raw_src < raw_end; |
1889 | 0 | raw_src += symesz, internal_ptr++) |
1890 | 0 | { |
1891 | 0 | unsigned int i; |
1892 | |
|
1893 | 0 | bfd_coff_swap_sym_in (abfd, (void *) raw_src, |
1894 | 0 | (void *) & internal_ptr->u.syment); |
1895 | 0 | symbol_ptr = internal_ptr; |
1896 | 0 | internal_ptr->is_sym = true; |
1897 | | |
1898 | | /* PR 17512: Prevent buffer overrun. */ |
1899 | 0 | if (symbol_ptr->u.syment.n_numaux > ((raw_end - 1) - raw_src) / symesz) |
1900 | 0 | return NULL; |
1901 | | |
1902 | 0 | for (i = 0; |
1903 | 0 | i < symbol_ptr->u.syment.n_numaux; |
1904 | 0 | i++) |
1905 | 0 | { |
1906 | 0 | internal_ptr++; |
1907 | 0 | raw_src += symesz; |
1908 | |
|
1909 | 0 | bfd_coff_swap_aux_in (abfd, (void *) raw_src, |
1910 | 0 | symbol_ptr->u.syment.n_type, |
1911 | 0 | symbol_ptr->u.syment.n_sclass, |
1912 | 0 | (int) i, symbol_ptr->u.syment.n_numaux, |
1913 | 0 | &(internal_ptr->u.auxent)); |
1914 | |
|
1915 | 0 | internal_ptr->is_sym = false; |
1916 | 0 | coff_pointerize_aux (abfd, internal, symbol_ptr, i, internal_ptr); |
1917 | 0 | } |
1918 | 0 | } |
1919 | | |
1920 | | /* Free the raw symbols. */ |
1921 | 0 | if (obj_coff_external_syms (abfd) != NULL |
1922 | 0 | && ! obj_coff_keep_syms (abfd)) |
1923 | 0 | { |
1924 | 0 | free (obj_coff_external_syms (abfd)); |
1925 | 0 | obj_coff_external_syms (abfd) = NULL; |
1926 | 0 | } |
1927 | |
|
1928 | 0 | for (internal_ptr = internal; internal_ptr < internal_end; |
1929 | 0 | internal_ptr++) |
1930 | 0 | { |
1931 | 0 | BFD_ASSERT (internal_ptr->is_sym); |
1932 | |
|
1933 | 0 | if (internal_ptr->u.syment.n_sclass == C_FILE |
1934 | 0 | && internal_ptr->u.syment.n_numaux > 0) |
1935 | 0 | { |
1936 | 0 | combined_entry_type * aux = internal_ptr + 1; |
1937 | | |
1938 | | /* Make a file symbol point to the name in the auxent, since |
1939 | | the text ".file" is redundant. */ |
1940 | 0 | BFD_ASSERT (! aux->is_sym); |
1941 | |
|
1942 | 0 | if (aux->u.auxent.x_file.x_n.x_n.x_zeroes == 0) |
1943 | 0 | { |
1944 | | /* The filename is a long one, point into the string table. */ |
1945 | 0 | if (string_table == NULL) |
1946 | 0 | { |
1947 | 0 | string_table = _bfd_coff_read_string_table (abfd); |
1948 | 0 | if (string_table == NULL) |
1949 | 0 | return NULL; |
1950 | 0 | } |
1951 | | |
1952 | 0 | if ((bfd_size_type)(aux->u.auxent.x_file.x_n.x_n.x_offset) |
1953 | 0 | >= obj_coff_strings_len (abfd)) |
1954 | 0 | internal_ptr->u.syment._n._n_n._n_offset = |
1955 | 0 | (uintptr_t) _("<corrupt>"); |
1956 | 0 | else |
1957 | 0 | internal_ptr->u.syment._n._n_n._n_offset = |
1958 | 0 | (uintptr_t) (string_table |
1959 | 0 | + aux->u.auxent.x_file.x_n.x_n.x_offset); |
1960 | 0 | } |
1961 | 0 | else |
1962 | 0 | { |
1963 | | /* Ordinary short filename, put into memory anyway. The |
1964 | | Microsoft PE tools sometimes store a filename in |
1965 | | multiple AUX entries. */ |
1966 | 0 | if (internal_ptr->u.syment.n_numaux > 1 && obj_pe (abfd)) |
1967 | 0 | internal_ptr->u.syment._n._n_n._n_offset = |
1968 | 0 | ((uintptr_t) |
1969 | 0 | copy_name (abfd, |
1970 | 0 | aux->u.auxent.x_file.x_n.x_fname, |
1971 | 0 | internal_ptr->u.syment.n_numaux * symesz)); |
1972 | 0 | else |
1973 | 0 | internal_ptr->u.syment._n._n_n._n_offset = |
1974 | 0 | ((uintptr_t) |
1975 | 0 | copy_name (abfd, |
1976 | 0 | aux->u.auxent.x_file.x_n.x_fname, |
1977 | 0 | (size_t) bfd_coff_filnmlen (abfd))); |
1978 | 0 | } |
1979 | | |
1980 | | /* Normalize other strings available in C_FILE aux entries. */ |
1981 | 0 | if (!obj_pe (abfd)) |
1982 | 0 | for (int numaux = 1; numaux < internal_ptr->u.syment.n_numaux; numaux++) |
1983 | 0 | { |
1984 | 0 | aux = internal_ptr + numaux + 1; |
1985 | 0 | BFD_ASSERT (! aux->is_sym); |
1986 | |
|
1987 | 0 | if (aux->u.auxent.x_file.x_n.x_n.x_zeroes == 0) |
1988 | 0 | { |
1989 | | /* The string information is a long one, point into the string table. */ |
1990 | 0 | if (string_table == NULL) |
1991 | 0 | { |
1992 | 0 | string_table = _bfd_coff_read_string_table (abfd); |
1993 | 0 | if (string_table == NULL) |
1994 | 0 | return NULL; |
1995 | 0 | } |
1996 | | |
1997 | 0 | if ((bfd_size_type)(aux->u.auxent.x_file.x_n.x_n.x_offset) |
1998 | 0 | >= obj_coff_strings_len (abfd)) |
1999 | 0 | aux->u.auxent.x_file.x_n.x_n.x_offset = |
2000 | 0 | (uintptr_t) _("<corrupt>"); |
2001 | 0 | else |
2002 | 0 | aux->u.auxent.x_file.x_n.x_n.x_offset = |
2003 | 0 | (uintptr_t) (string_table |
2004 | 0 | + (aux->u.auxent.x_file.x_n.x_n.x_offset)); |
2005 | 0 | } |
2006 | 0 | else |
2007 | 0 | aux->u.auxent.x_file.x_n.x_n.x_offset = |
2008 | 0 | ((uintptr_t) |
2009 | 0 | copy_name (abfd, |
2010 | 0 | aux->u.auxent.x_file.x_n.x_fname, |
2011 | 0 | (size_t) bfd_coff_filnmlen (abfd))); |
2012 | 0 | } |
2013 | |
|
2014 | 0 | } |
2015 | 0 | else |
2016 | 0 | { |
2017 | 0 | if (internal_ptr->u.syment._n._n_n._n_zeroes != 0) |
2018 | 0 | { |
2019 | | /* This is a "short" name. Make it long. */ |
2020 | 0 | size_t i; |
2021 | 0 | char *newstring; |
2022 | | |
2023 | | /* Find the length of this string without walking into memory |
2024 | | that isn't ours. */ |
2025 | 0 | for (i = 0; i < 8; ++i) |
2026 | 0 | if (internal_ptr->u.syment._n._n_name[i] == '\0') |
2027 | 0 | break; |
2028 | |
|
2029 | 0 | newstring = (char *) bfd_zalloc (abfd, (bfd_size_type) (i + 1)); |
2030 | 0 | if (newstring == NULL) |
2031 | 0 | return NULL; |
2032 | 0 | strncpy (newstring, internal_ptr->u.syment._n._n_name, i); |
2033 | 0 | internal_ptr->u.syment._n._n_n._n_offset = (uintptr_t) newstring; |
2034 | 0 | internal_ptr->u.syment._n._n_n._n_zeroes = 0; |
2035 | 0 | } |
2036 | 0 | else if (internal_ptr->u.syment._n._n_n._n_offset == 0) |
2037 | 0 | internal_ptr->u.syment._n._n_n._n_offset = (uintptr_t) ""; |
2038 | 0 | else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment)) |
2039 | 0 | { |
2040 | | /* Long name already. Point symbol at the string in the |
2041 | | table. */ |
2042 | 0 | if (string_table == NULL) |
2043 | 0 | { |
2044 | 0 | string_table = _bfd_coff_read_string_table (abfd); |
2045 | 0 | if (string_table == NULL) |
2046 | 0 | return NULL; |
2047 | 0 | } |
2048 | 0 | if (internal_ptr->u.syment._n._n_n._n_offset >= obj_coff_strings_len (abfd) |
2049 | 0 | || string_table + internal_ptr->u.syment._n._n_n._n_offset < string_table) |
2050 | 0 | internal_ptr->u.syment._n._n_n._n_offset = |
2051 | 0 | (uintptr_t) _("<corrupt>"); |
2052 | 0 | else |
2053 | 0 | internal_ptr->u.syment._n._n_n._n_offset = |
2054 | 0 | ((uintptr_t) (string_table |
2055 | 0 | + internal_ptr->u.syment._n._n_n._n_offset)); |
2056 | 0 | } |
2057 | 0 | else |
2058 | 0 | { |
2059 | | /* Long name in debug section. Very similar. */ |
2060 | 0 | if (debug_sec_data == NULL) |
2061 | 0 | debug_sec_data = build_debug_section (abfd, & debug_sec); |
2062 | 0 | if (debug_sec_data != NULL) |
2063 | 0 | { |
2064 | 0 | BFD_ASSERT (debug_sec != NULL); |
2065 | | /* PR binutils/17512: Catch out of range offsets into the debug data. */ |
2066 | 0 | if (internal_ptr->u.syment._n._n_n._n_offset > debug_sec->size |
2067 | 0 | || debug_sec_data + internal_ptr->u.syment._n._n_n._n_offset < debug_sec_data) |
2068 | 0 | internal_ptr->u.syment._n._n_n._n_offset = |
2069 | 0 | (uintptr_t) _("<corrupt>"); |
2070 | 0 | else |
2071 | 0 | internal_ptr->u.syment._n._n_n._n_offset = |
2072 | 0 | (uintptr_t) (debug_sec_data |
2073 | 0 | + internal_ptr->u.syment._n._n_n._n_offset); |
2074 | 0 | } |
2075 | 0 | else |
2076 | 0 | internal_ptr->u.syment._n._n_n._n_offset = (uintptr_t) ""; |
2077 | 0 | } |
2078 | 0 | } |
2079 | 0 | internal_ptr += internal_ptr->u.syment.n_numaux; |
2080 | 0 | } |
2081 | | |
2082 | 0 | obj_raw_syments (abfd) = internal; |
2083 | 0 | BFD_ASSERT (obj_raw_syment_count (abfd) |
2084 | 0 | == (unsigned int) (internal_ptr - internal)); |
2085 | |
|
2086 | 0 | return internal; |
2087 | 0 | } |
2088 | | |
2089 | | long |
2090 | | coff_get_reloc_upper_bound (bfd *abfd, sec_ptr asect) |
2091 | 0 | { |
2092 | 0 | size_t count, raw; |
2093 | |
|
2094 | 0 | count = asect->reloc_count; |
2095 | 0 | if (count >= LONG_MAX / sizeof (arelent *) |
2096 | 0 | || _bfd_mul_overflow (count, bfd_coff_relsz (abfd), &raw)) |
2097 | 0 | { |
2098 | 0 | bfd_set_error (bfd_error_file_too_big); |
2099 | 0 | return -1; |
2100 | 0 | } |
2101 | 0 | if (!bfd_write_p (abfd)) |
2102 | 0 | { |
2103 | 0 | ufile_ptr filesize = bfd_get_file_size (abfd); |
2104 | 0 | if (filesize != 0 && raw > filesize) |
2105 | 0 | { |
2106 | 0 | bfd_set_error (bfd_error_file_truncated); |
2107 | 0 | return -1; |
2108 | 0 | } |
2109 | 0 | } |
2110 | 0 | return (count + 1) * sizeof (arelent *); |
2111 | 0 | } |
2112 | | |
2113 | | asymbol * |
2114 | | coff_make_empty_symbol (bfd *abfd) |
2115 | 88.2M | { |
2116 | 88.2M | size_t amt = sizeof (coff_symbol_type); |
2117 | 88.2M | coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_zalloc (abfd, amt); |
2118 | | |
2119 | 88.2M | if (new_symbol == NULL) |
2120 | 0 | return NULL; |
2121 | 88.2M | new_symbol->symbol.section = 0; |
2122 | 88.2M | new_symbol->native = NULL; |
2123 | 88.2M | new_symbol->lineno = NULL; |
2124 | 88.2M | new_symbol->done_lineno = false; |
2125 | 88.2M | new_symbol->symbol.the_bfd = abfd; |
2126 | | |
2127 | 88.2M | return & new_symbol->symbol; |
2128 | 88.2M | } |
2129 | | |
2130 | | /* Make a debugging symbol. */ |
2131 | | |
2132 | | asymbol * |
2133 | | coff_bfd_make_debug_symbol (bfd *abfd) |
2134 | 0 | { |
2135 | 0 | size_t amt = sizeof (coff_symbol_type); |
2136 | 0 | coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_alloc (abfd, amt); |
2137 | |
|
2138 | 0 | if (new_symbol == NULL) |
2139 | 0 | return NULL; |
2140 | | /* @@ The 10 is a guess at a plausible maximum number of aux entries |
2141 | | (but shouldn't be a constant). */ |
2142 | 0 | amt = sizeof (combined_entry_type) * 10; |
2143 | 0 | new_symbol->native = (combined_entry_type *) bfd_zalloc (abfd, amt); |
2144 | 0 | if (!new_symbol->native) |
2145 | 0 | return NULL; |
2146 | 0 | new_symbol->native->is_sym = true; |
2147 | 0 | new_symbol->symbol.section = bfd_abs_section_ptr; |
2148 | 0 | new_symbol->symbol.flags = BSF_DEBUGGING; |
2149 | 0 | new_symbol->lineno = NULL; |
2150 | 0 | new_symbol->done_lineno = false; |
2151 | 0 | new_symbol->symbol.the_bfd = abfd; |
2152 | |
|
2153 | 0 | return & new_symbol->symbol; |
2154 | 0 | } |
2155 | | |
2156 | | void |
2157 | | coff_get_symbol_info (bfd *abfd, asymbol *symbol, symbol_info *ret) |
2158 | 0 | { |
2159 | 0 | bfd_symbol_info (symbol, ret); |
2160 | |
|
2161 | 0 | if (coffsymbol (symbol)->native != NULL |
2162 | 0 | && coffsymbol (symbol)->native->fix_value |
2163 | 0 | && coffsymbol (symbol)->native->is_sym) |
2164 | 0 | ret->value |
2165 | 0 | = (((uintptr_t) coffsymbol (symbol)->native->u.syment.n_value |
2166 | 0 | - (uintptr_t) obj_raw_syments (abfd)) |
2167 | 0 | / sizeof (combined_entry_type)); |
2168 | 0 | } |
2169 | | |
2170 | | /* Print out information about COFF symbol. */ |
2171 | | |
2172 | | void |
2173 | | coff_print_symbol (bfd *abfd, |
2174 | | void * filep, |
2175 | | asymbol *symbol, |
2176 | | bfd_print_symbol_type how) |
2177 | 0 | { |
2178 | 0 | FILE * file = (FILE *) filep; |
2179 | |
|
2180 | 0 | switch (how) |
2181 | 0 | { |
2182 | 0 | case bfd_print_symbol_name: |
2183 | 0 | fprintf (file, "%s", symbol->name); |
2184 | 0 | break; |
2185 | | |
2186 | 0 | case bfd_print_symbol_more: |
2187 | 0 | fprintf (file, "coff %s %s", |
2188 | 0 | coffsymbol (symbol)->native ? "n" : "g", |
2189 | 0 | coffsymbol (symbol)->lineno ? "l" : " "); |
2190 | 0 | break; |
2191 | | |
2192 | 0 | case bfd_print_symbol_all: |
2193 | 0 | if (coffsymbol (symbol)->native) |
2194 | 0 | { |
2195 | 0 | bfd_vma val; |
2196 | 0 | unsigned int aux; |
2197 | 0 | combined_entry_type *combined = coffsymbol (symbol)->native; |
2198 | 0 | combined_entry_type *root = obj_raw_syments (abfd); |
2199 | 0 | struct lineno_cache_entry *l = coffsymbol (symbol)->lineno; |
2200 | |
|
2201 | 0 | fprintf (file, "[%3ld]", (long) (combined - root)); |
2202 | | |
2203 | | /* PR 17512: file: 079-33786-0.001:0.1. */ |
2204 | 0 | if (combined < obj_raw_syments (abfd) |
2205 | 0 | || combined >= obj_raw_syments (abfd) + obj_raw_syment_count (abfd)) |
2206 | 0 | { |
2207 | 0 | fprintf (file, _("<corrupt info> %s"), symbol->name); |
2208 | 0 | break; |
2209 | 0 | } |
2210 | | |
2211 | 0 | BFD_ASSERT (combined->is_sym); |
2212 | 0 | if (! combined->fix_value) |
2213 | 0 | val = (bfd_vma) combined->u.syment.n_value; |
2214 | 0 | else |
2215 | 0 | val = (((uintptr_t) combined->u.syment.n_value - (uintptr_t) root) |
2216 | 0 | / sizeof (combined_entry_type)); |
2217 | |
|
2218 | 0 | fprintf (file, "(sec %2d)(fl 0x%02x)(ty %4x)(scl %3d) (nx %d) 0x", |
2219 | 0 | combined->u.syment.n_scnum, |
2220 | 0 | combined->u.syment.n_flags, |
2221 | 0 | combined->u.syment.n_type, |
2222 | 0 | combined->u.syment.n_sclass, |
2223 | 0 | combined->u.syment.n_numaux); |
2224 | 0 | bfd_fprintf_vma (abfd, file, val); |
2225 | 0 | fprintf (file, " %s", symbol->name); |
2226 | |
|
2227 | 0 | for (aux = 0; aux < combined->u.syment.n_numaux; aux++) |
2228 | 0 | { |
2229 | 0 | combined_entry_type *auxp = combined + aux + 1; |
2230 | 0 | long tagndx; |
2231 | |
|
2232 | 0 | BFD_ASSERT (! auxp->is_sym); |
2233 | 0 | if (auxp->fix_tag) |
2234 | 0 | tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root; |
2235 | 0 | else |
2236 | 0 | tagndx = auxp->u.auxent.x_sym.x_tagndx.u32; |
2237 | |
|
2238 | 0 | fprintf (file, "\n"); |
2239 | |
|
2240 | 0 | if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux)) |
2241 | 0 | continue; |
2242 | | |
2243 | 0 | switch (combined->u.syment.n_sclass) |
2244 | 0 | { |
2245 | 0 | case C_FILE: |
2246 | 0 | fprintf (file, "File "); |
2247 | | /* Add additional information if this isn't the filename |
2248 | | auxiliary entry. */ |
2249 | 0 | if (auxp->u.auxent.x_file.x_ftype) |
2250 | 0 | fprintf (file, "ftype %d fname \"%s\"", |
2251 | 0 | auxp->u.auxent.x_file.x_ftype, |
2252 | 0 | (char *) auxp->u.auxent.x_file.x_n.x_n.x_offset); |
2253 | 0 | break; |
2254 | | |
2255 | 0 | case C_DWARF: |
2256 | 0 | fprintf (file, "AUX scnlen %#" PRIx64 " nreloc %" PRId64, |
2257 | 0 | auxp->u.auxent.x_sect.x_scnlen, |
2258 | 0 | auxp->u.auxent.x_sect.x_nreloc); |
2259 | 0 | break; |
2260 | | |
2261 | 0 | case C_STAT: |
2262 | 0 | if (combined->u.syment.n_type == T_NULL) |
2263 | | /* Probably a section symbol ? */ |
2264 | 0 | { |
2265 | 0 | fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d", |
2266 | 0 | (unsigned long) auxp->u.auxent.x_scn.x_scnlen, |
2267 | 0 | auxp->u.auxent.x_scn.x_nreloc, |
2268 | 0 | auxp->u.auxent.x_scn.x_nlinno); |
2269 | 0 | if (auxp->u.auxent.x_scn.x_checksum != 0 |
2270 | 0 | || auxp->u.auxent.x_scn.x_associated != 0 |
2271 | 0 | || auxp->u.auxent.x_scn.x_comdat != 0) |
2272 | 0 | fprintf (file, " checksum 0x%x assoc %d comdat %d", |
2273 | 0 | auxp->u.auxent.x_scn.x_checksum, |
2274 | 0 | auxp->u.auxent.x_scn.x_associated, |
2275 | 0 | auxp->u.auxent.x_scn.x_comdat); |
2276 | 0 | break; |
2277 | 0 | } |
2278 | | /* Fall through. */ |
2279 | 0 | case C_EXT: |
2280 | 0 | case C_AIX_WEAKEXT: |
2281 | 0 | if (ISFCN (combined->u.syment.n_type)) |
2282 | 0 | { |
2283 | 0 | long next, llnos; |
2284 | |
|
2285 | 0 | if (auxp->fix_end) |
2286 | 0 | next = (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p |
2287 | 0 | - root); |
2288 | 0 | else |
2289 | 0 | next = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.u32; |
2290 | 0 | llnos = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_lnnoptr; |
2291 | 0 | fprintf (file, |
2292 | 0 | "AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld", |
2293 | 0 | tagndx, |
2294 | 0 | (unsigned long) auxp->u.auxent.x_sym.x_misc.x_fsize, |
2295 | 0 | llnos, next); |
2296 | 0 | break; |
2297 | 0 | } |
2298 | | /* Fall through. */ |
2299 | 0 | default: |
2300 | 0 | fprintf (file, "AUX lnno %d size 0x%x tagndx %ld", |
2301 | 0 | auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno, |
2302 | 0 | auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size, |
2303 | 0 | tagndx); |
2304 | 0 | if (auxp->fix_end) |
2305 | 0 | fprintf (file, " endndx %ld", |
2306 | 0 | ((long) |
2307 | 0 | (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p |
2308 | 0 | - root))); |
2309 | 0 | break; |
2310 | 0 | } |
2311 | 0 | } |
2312 | | |
2313 | 0 | if (l) |
2314 | 0 | { |
2315 | 0 | fprintf (file, "\n%s :", l->u.sym->name); |
2316 | 0 | l++; |
2317 | 0 | while (l->line_number) |
2318 | 0 | { |
2319 | 0 | if (l->line_number > 0) |
2320 | 0 | { |
2321 | 0 | fprintf (file, "\n%4d : ", l->line_number); |
2322 | 0 | bfd_fprintf_vma (abfd, file, l->u.offset + symbol->section->vma); |
2323 | 0 | } |
2324 | 0 | l++; |
2325 | 0 | } |
2326 | 0 | } |
2327 | 0 | } |
2328 | 0 | else |
2329 | 0 | { |
2330 | 0 | bfd_print_symbol_vandf (abfd, (void *) file, symbol); |
2331 | 0 | fprintf (file, " %-5s %s %s %s", |
2332 | 0 | symbol->section->name, |
2333 | 0 | coffsymbol (symbol)->native ? "n" : "g", |
2334 | 0 | coffsymbol (symbol)->lineno ? "l" : " ", |
2335 | 0 | symbol->name); |
2336 | 0 | } |
2337 | 0 | } |
2338 | 0 | } |
2339 | | |
2340 | | /* Return whether a symbol name implies a local symbol. In COFF, |
2341 | | local symbols generally start with ``.L''. Most targets use this |
2342 | | function for the is_local_label_name entry point, but some may |
2343 | | override it. */ |
2344 | | |
2345 | | bool |
2346 | | _bfd_coff_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, |
2347 | | const char *name) |
2348 | 0 | { |
2349 | 0 | return name[0] == '.' && name[1] == 'L'; |
2350 | 0 | } |
2351 | | |
2352 | | /* Provided a BFD, a section and an offset (in bytes, not octets) into the |
2353 | | section, calculate and return the name of the source file and the line |
2354 | | nearest to the wanted location. */ |
2355 | | |
2356 | | bool |
2357 | | coff_find_nearest_line_with_names (bfd *abfd, |
2358 | | asymbol **symbols, |
2359 | | asection *section, |
2360 | | bfd_vma offset, |
2361 | | const char **filename_ptr, |
2362 | | const char **functionname_ptr, |
2363 | | unsigned int *line_ptr, |
2364 | | const struct dwarf_debug_section *debug_sections) |
2365 | 0 | { |
2366 | 0 | bool found; |
2367 | 0 | unsigned int i; |
2368 | 0 | unsigned int line_base; |
2369 | 0 | coff_data_type *cof = coff_data (abfd); |
2370 | | /* Run through the raw syments if available. */ |
2371 | 0 | combined_entry_type *p; |
2372 | 0 | combined_entry_type *pend; |
2373 | 0 | alent *l; |
2374 | 0 | struct coff_section_tdata *sec_data; |
2375 | 0 | size_t amt; |
2376 | | |
2377 | | /* Before looking through the symbol table, try to use a .stab |
2378 | | section to find the information. */ |
2379 | 0 | if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, |
2380 | 0 | &found, filename_ptr, |
2381 | 0 | functionname_ptr, line_ptr, |
2382 | 0 | &coff_data(abfd)->line_info)) |
2383 | 0 | return false; |
2384 | | |
2385 | 0 | if (found) |
2386 | 0 | return true; |
2387 | | |
2388 | | /* Also try examining DWARF2 debugging information. */ |
2389 | 0 | if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset, |
2390 | 0 | filename_ptr, functionname_ptr, |
2391 | 0 | line_ptr, NULL, debug_sections, |
2392 | 0 | &coff_data(abfd)->dwarf2_find_line_info)) |
2393 | 0 | return true; |
2394 | | |
2395 | 0 | sec_data = coff_section_data (abfd, section); |
2396 | | |
2397 | | /* If the DWARF lookup failed, but there is DWARF information available |
2398 | | then the problem might be that the file has been rebased. This tool |
2399 | | changes the VMAs of all the sections, but it does not update the DWARF |
2400 | | information. So try again, using a bias against the address sought. */ |
2401 | 0 | if (coff_data (abfd)->dwarf2_find_line_info != NULL) |
2402 | 0 | { |
2403 | 0 | bfd_signed_vma bias = 0; |
2404 | | |
2405 | | /* Create a cache of the result for the next call. */ |
2406 | 0 | if (sec_data == NULL && section->owner == abfd) |
2407 | 0 | { |
2408 | 0 | amt = sizeof (struct coff_section_tdata); |
2409 | 0 | section->used_by_bfd = bfd_zalloc (abfd, amt); |
2410 | 0 | sec_data = (struct coff_section_tdata *) section->used_by_bfd; |
2411 | 0 | } |
2412 | |
|
2413 | 0 | if (sec_data != NULL && sec_data->saved_bias) |
2414 | 0 | bias = sec_data->bias; |
2415 | 0 | else if (symbols) |
2416 | 0 | { |
2417 | 0 | bias = _bfd_dwarf2_find_symbol_bias (symbols, |
2418 | 0 | & coff_data (abfd)->dwarf2_find_line_info); |
2419 | |
|
2420 | 0 | if (sec_data) |
2421 | 0 | { |
2422 | 0 | sec_data->saved_bias = true; |
2423 | 0 | sec_data->bias = bias; |
2424 | 0 | } |
2425 | 0 | } |
2426 | |
|
2427 | 0 | if (bias |
2428 | 0 | && _bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, |
2429 | 0 | offset + bias, |
2430 | 0 | filename_ptr, functionname_ptr, |
2431 | 0 | line_ptr, NULL, debug_sections, |
2432 | 0 | &coff_data(abfd)->dwarf2_find_line_info)) |
2433 | 0 | return true; |
2434 | 0 | } |
2435 | | |
2436 | 0 | *filename_ptr = 0; |
2437 | 0 | *functionname_ptr = 0; |
2438 | 0 | *line_ptr = 0; |
2439 | | |
2440 | | /* Don't try and find line numbers in a non coff file. */ |
2441 | 0 | if (!bfd_family_coff (abfd)) |
2442 | 0 | return false; |
2443 | | |
2444 | 0 | if (cof == NULL) |
2445 | 0 | return false; |
2446 | | |
2447 | | /* Find the first C_FILE symbol. */ |
2448 | 0 | p = cof->raw_syments; |
2449 | 0 | if (!p) |
2450 | 0 | return false; |
2451 | | |
2452 | 0 | pend = p + cof->raw_syment_count; |
2453 | 0 | while (p < pend) |
2454 | 0 | { |
2455 | 0 | BFD_ASSERT (p->is_sym); |
2456 | 0 | if (p->u.syment.n_sclass == C_FILE) |
2457 | 0 | break; |
2458 | 0 | p += 1 + p->u.syment.n_numaux; |
2459 | 0 | } |
2460 | |
|
2461 | 0 | if (p < pend) |
2462 | 0 | { |
2463 | 0 | bfd_vma sec_vma; |
2464 | 0 | bfd_vma maxdiff; |
2465 | | |
2466 | | /* Look through the C_FILE symbols to find the best one. */ |
2467 | 0 | sec_vma = bfd_section_vma (section); |
2468 | 0 | *filename_ptr = (char *) p->u.syment._n._n_n._n_offset; |
2469 | 0 | maxdiff = (bfd_vma) 0 - (bfd_vma) 1; |
2470 | 0 | while (1) |
2471 | 0 | { |
2472 | 0 | bfd_vma file_addr; |
2473 | 0 | combined_entry_type *p2; |
2474 | |
|
2475 | 0 | for (p2 = p + 1 + p->u.syment.n_numaux; |
2476 | 0 | p2 < pend; |
2477 | 0 | p2 += 1 + p2->u.syment.n_numaux) |
2478 | 0 | { |
2479 | 0 | BFD_ASSERT (p2->is_sym); |
2480 | 0 | if (p2->u.syment.n_scnum > 0 |
2481 | 0 | && (section |
2482 | 0 | == coff_section_from_bfd_index (abfd, |
2483 | 0 | p2->u.syment.n_scnum))) |
2484 | 0 | break; |
2485 | 0 | if (p2->u.syment.n_sclass == C_FILE) |
2486 | 0 | { |
2487 | 0 | p2 = pend; |
2488 | 0 | break; |
2489 | 0 | } |
2490 | 0 | } |
2491 | 0 | if (p2 >= pend) |
2492 | 0 | break; |
2493 | | |
2494 | 0 | file_addr = (bfd_vma) p2->u.syment.n_value; |
2495 | | /* PR 11512: Include the section address of the function name symbol. */ |
2496 | 0 | if (p2->u.syment.n_scnum > 0) |
2497 | 0 | file_addr += coff_section_from_bfd_index (abfd, |
2498 | 0 | p2->u.syment.n_scnum)->vma; |
2499 | | /* We use <= MAXDIFF here so that if we get a zero length |
2500 | | file, we actually use the next file entry. */ |
2501 | 0 | if (p2 < pend |
2502 | 0 | && offset + sec_vma >= file_addr |
2503 | 0 | && offset + sec_vma - file_addr <= maxdiff) |
2504 | 0 | { |
2505 | 0 | *filename_ptr = (char *) p->u.syment._n._n_n._n_offset; |
2506 | 0 | maxdiff = offset + sec_vma - p2->u.syment.n_value; |
2507 | 0 | } |
2508 | |
|
2509 | 0 | if (p->u.syment.n_value >= cof->raw_syment_count) |
2510 | 0 | break; |
2511 | | |
2512 | | /* Avoid endless loops on erroneous files by ensuring that |
2513 | | we always move forward in the file. */ |
2514 | 0 | if (p >= cof->raw_syments + p->u.syment.n_value) |
2515 | 0 | break; |
2516 | | |
2517 | 0 | p = cof->raw_syments + p->u.syment.n_value; |
2518 | 0 | if (!p->is_sym || p->u.syment.n_sclass != C_FILE) |
2519 | 0 | break; |
2520 | 0 | } |
2521 | 0 | } |
2522 | |
|
2523 | 0 | if (section->lineno_count == 0) |
2524 | 0 | { |
2525 | 0 | *functionname_ptr = NULL; |
2526 | 0 | *line_ptr = 0; |
2527 | 0 | return true; |
2528 | 0 | } |
2529 | | |
2530 | | /* Now wander though the raw linenumbers of the section. |
2531 | | If we have been called on this section before, and the offset |
2532 | | we want is further down then we can prime the lookup loop. */ |
2533 | 0 | if (sec_data != NULL |
2534 | 0 | && sec_data->i > 0 |
2535 | 0 | && offset >= sec_data->offset) |
2536 | 0 | { |
2537 | 0 | i = sec_data->i; |
2538 | 0 | *functionname_ptr = sec_data->function; |
2539 | 0 | line_base = sec_data->line_base; |
2540 | 0 | } |
2541 | 0 | else |
2542 | 0 | { |
2543 | 0 | i = 0; |
2544 | 0 | line_base = 0; |
2545 | 0 | } |
2546 | |
|
2547 | 0 | if (section->lineno != NULL) |
2548 | 0 | { |
2549 | 0 | bfd_vma last_value = 0; |
2550 | |
|
2551 | 0 | l = §ion->lineno[i]; |
2552 | |
|
2553 | 0 | for (; i < section->lineno_count; i++) |
2554 | 0 | { |
2555 | 0 | if (l->line_number == 0) |
2556 | 0 | { |
2557 | | /* Get the symbol this line number points at. */ |
2558 | 0 | coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym); |
2559 | 0 | if (coff->symbol.value > offset) |
2560 | 0 | break; |
2561 | | |
2562 | 0 | *functionname_ptr = coff->symbol.name; |
2563 | 0 | last_value = coff->symbol.value; |
2564 | 0 | if (coff->native) |
2565 | 0 | { |
2566 | 0 | combined_entry_type *s = coff->native; |
2567 | |
|
2568 | 0 | BFD_ASSERT (s->is_sym); |
2569 | 0 | s = s + 1 + s->u.syment.n_numaux; |
2570 | | |
2571 | | /* In XCOFF a debugging symbol can follow the |
2572 | | function symbol. */ |
2573 | 0 | if (((size_t) ((char *) s - (char *) obj_raw_syments (abfd)) |
2574 | 0 | < obj_raw_syment_count (abfd) * sizeof (*s)) |
2575 | 0 | && s->u.syment.n_scnum == N_DEBUG) |
2576 | 0 | s = s + 1 + s->u.syment.n_numaux; |
2577 | | |
2578 | | /* S should now point to the .bf of the function. */ |
2579 | 0 | if (((size_t) ((char *) s - (char *) obj_raw_syments (abfd)) |
2580 | 0 | < obj_raw_syment_count (abfd) * sizeof (*s)) |
2581 | 0 | && s->u.syment.n_numaux) |
2582 | 0 | { |
2583 | | /* The linenumber is stored in the auxent. */ |
2584 | 0 | union internal_auxent *a = &((s + 1)->u.auxent); |
2585 | |
|
2586 | 0 | line_base = a->x_sym.x_misc.x_lnsz.x_lnno; |
2587 | 0 | *line_ptr = line_base; |
2588 | 0 | } |
2589 | 0 | } |
2590 | 0 | } |
2591 | 0 | else |
2592 | 0 | { |
2593 | 0 | if (l->u.offset > offset) |
2594 | 0 | break; |
2595 | 0 | *line_ptr = l->line_number + line_base - 1; |
2596 | 0 | } |
2597 | 0 | l++; |
2598 | 0 | } |
2599 | | |
2600 | | /* If we fell off the end of the loop, then assume that this |
2601 | | symbol has no line number info. Otherwise, symbols with no |
2602 | | line number info get reported with the line number of the |
2603 | | last line of the last symbol which does have line number |
2604 | | info. We use 0x100 as a slop to account for cases where the |
2605 | | last line has executable code. */ |
2606 | 0 | if (i >= section->lineno_count |
2607 | 0 | && last_value != 0 |
2608 | 0 | && offset - last_value > 0x100) |
2609 | 0 | { |
2610 | 0 | *functionname_ptr = NULL; |
2611 | 0 | *line_ptr = 0; |
2612 | 0 | } |
2613 | 0 | } |
2614 | | |
2615 | | /* Cache the results for the next call. */ |
2616 | 0 | if (sec_data == NULL && section->owner == abfd) |
2617 | 0 | { |
2618 | 0 | amt = sizeof (struct coff_section_tdata); |
2619 | 0 | section->used_by_bfd = bfd_zalloc (abfd, amt); |
2620 | 0 | sec_data = (struct coff_section_tdata *) section->used_by_bfd; |
2621 | 0 | } |
2622 | |
|
2623 | 0 | if (sec_data != NULL) |
2624 | 0 | { |
2625 | 0 | sec_data->offset = offset; |
2626 | 0 | sec_data->i = i - 1; |
2627 | 0 | sec_data->function = *functionname_ptr; |
2628 | 0 | sec_data->line_base = line_base; |
2629 | 0 | } |
2630 | |
|
2631 | 0 | return true; |
2632 | 0 | } |
2633 | | |
2634 | | bool |
2635 | | coff_find_nearest_line (bfd *abfd, |
2636 | | asymbol **symbols, |
2637 | | asection *section, |
2638 | | bfd_vma offset, |
2639 | | const char **filename_ptr, |
2640 | | const char **functionname_ptr, |
2641 | | unsigned int *line_ptr, |
2642 | | unsigned int *discriminator_ptr) |
2643 | 0 | { |
2644 | 0 | if (discriminator_ptr) |
2645 | 0 | *discriminator_ptr = 0; |
2646 | 0 | return coff_find_nearest_line_with_names (abfd, symbols, section, offset, |
2647 | 0 | filename_ptr, functionname_ptr, |
2648 | 0 | line_ptr, dwarf_debug_sections); |
2649 | 0 | } |
2650 | | |
2651 | | bool |
2652 | | coff_find_inliner_info (bfd *abfd, |
2653 | | const char **filename_ptr, |
2654 | | const char **functionname_ptr, |
2655 | | unsigned int *line_ptr) |
2656 | 0 | { |
2657 | 0 | bool found; |
2658 | |
|
2659 | 0 | found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr, |
2660 | 0 | functionname_ptr, line_ptr, |
2661 | 0 | &coff_data(abfd)->dwarf2_find_line_info); |
2662 | 0 | return (found); |
2663 | 0 | } |
2664 | | |
2665 | | int |
2666 | | coff_sizeof_headers (bfd *abfd, struct bfd_link_info *info) |
2667 | 0 | { |
2668 | 0 | size_t size; |
2669 | |
|
2670 | 0 | if (!bfd_link_relocatable (info)) |
2671 | 0 | size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd); |
2672 | 0 | else |
2673 | 0 | size = bfd_coff_filhsz (abfd); |
2674 | |
|
2675 | 0 | size += abfd->section_count * bfd_coff_scnhsz (abfd); |
2676 | 0 | return size; |
2677 | 0 | } |
2678 | | |
2679 | | /* Change the class of a coff symbol held by BFD. */ |
2680 | | |
2681 | | bool |
2682 | | bfd_coff_set_symbol_class (bfd * abfd, |
2683 | | asymbol * symbol, |
2684 | | unsigned int symbol_class) |
2685 | 0 | { |
2686 | 0 | coff_symbol_type * csym; |
2687 | |
|
2688 | 0 | csym = coff_symbol_from (symbol); |
2689 | 0 | if (csym == NULL) |
2690 | 0 | { |
2691 | 0 | bfd_set_error (bfd_error_invalid_operation); |
2692 | 0 | return false; |
2693 | 0 | } |
2694 | 0 | else if (csym->native == NULL) |
2695 | 0 | { |
2696 | | /* This is an alien symbol which no native coff backend data. |
2697 | | We cheat here by creating a fake native entry for it and |
2698 | | then filling in the class. This code is based on that in |
2699 | | coff_write_alien_symbol(). */ |
2700 | |
|
2701 | 0 | combined_entry_type * native; |
2702 | 0 | size_t amt = sizeof (* native); |
2703 | |
|
2704 | 0 | native = (combined_entry_type *) bfd_zalloc (abfd, amt); |
2705 | 0 | if (native == NULL) |
2706 | 0 | return false; |
2707 | | |
2708 | 0 | native->is_sym = true; |
2709 | 0 | native->u.syment.n_type = T_NULL; |
2710 | 0 | native->u.syment.n_sclass = symbol_class; |
2711 | |
|
2712 | 0 | if (bfd_is_und_section (symbol->section)) |
2713 | 0 | { |
2714 | 0 | native->u.syment.n_scnum = N_UNDEF; |
2715 | 0 | native->u.syment.n_value = symbol->value; |
2716 | 0 | } |
2717 | 0 | else if (bfd_is_com_section (symbol->section)) |
2718 | 0 | { |
2719 | 0 | native->u.syment.n_scnum = N_UNDEF; |
2720 | 0 | native->u.syment.n_value = symbol->value; |
2721 | 0 | } |
2722 | 0 | else |
2723 | 0 | { |
2724 | 0 | native->u.syment.n_scnum = |
2725 | 0 | symbol->section->output_section->target_index; |
2726 | 0 | native->u.syment.n_value = (symbol->value |
2727 | 0 | + symbol->section->output_offset); |
2728 | 0 | if (! obj_pe (abfd)) |
2729 | 0 | native->u.syment.n_value += symbol->section->output_section->vma; |
2730 | | |
2731 | | /* Copy the any flags from the file header into the symbol. |
2732 | | FIXME: Why? */ |
2733 | 0 | native->u.syment.n_flags = bfd_asymbol_bfd (& csym->symbol)->flags; |
2734 | 0 | } |
2735 | |
|
2736 | 0 | csym->native = native; |
2737 | 0 | } |
2738 | 0 | else |
2739 | 0 | csym->native->u.syment.n_sclass = symbol_class; |
2740 | | |
2741 | 0 | return true; |
2742 | 0 | } |
2743 | | |
2744 | | bool |
2745 | | _bfd_coff_section_already_linked (bfd *abfd, |
2746 | | asection *sec, |
2747 | | struct bfd_link_info *info) |
2748 | 0 | { |
2749 | 0 | flagword flags; |
2750 | 0 | const char *name, *key; |
2751 | 0 | struct bfd_section_already_linked *l; |
2752 | 0 | struct bfd_section_already_linked_hash_entry *already_linked_list; |
2753 | 0 | struct coff_comdat_info *s_comdat; |
2754 | |
|
2755 | 0 | if (sec->output_section == bfd_abs_section_ptr) |
2756 | 0 | return false; |
2757 | | |
2758 | 0 | flags = sec->flags; |
2759 | 0 | if ((flags & SEC_LINK_ONCE) == 0) |
2760 | 0 | return false; |
2761 | | |
2762 | | /* The COFF backend linker doesn't support group sections. */ |
2763 | 0 | if ((flags & SEC_GROUP) != 0) |
2764 | 0 | return false; |
2765 | | |
2766 | 0 | name = bfd_section_name (sec); |
2767 | 0 | s_comdat = bfd_coff_get_comdat_section (abfd, sec); |
2768 | |
|
2769 | 0 | if (s_comdat != NULL) |
2770 | 0 | key = s_comdat->name; |
2771 | 0 | else |
2772 | 0 | { |
2773 | 0 | if (startswith (name, ".gnu.linkonce.") |
2774 | 0 | && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL) |
2775 | 0 | key++; |
2776 | 0 | else |
2777 | | /* FIXME: gcc as of 2011-09 emits sections like .text$<key>, |
2778 | | .xdata$<key> and .pdata$<key> only the first of which has a |
2779 | | comdat key. Should these all match the LTO IR key? */ |
2780 | 0 | key = name; |
2781 | 0 | } |
2782 | |
|
2783 | 0 | already_linked_list = bfd_section_already_linked_table_lookup (key); |
2784 | |
|
2785 | 0 | for (l = already_linked_list->entry; l != NULL; l = l->next) |
2786 | 0 | { |
2787 | 0 | struct coff_comdat_info *l_comdat; |
2788 | |
|
2789 | 0 | l_comdat = bfd_coff_get_comdat_section (l->sec->owner, l->sec); |
2790 | | |
2791 | | /* The section names must match, and both sections must be |
2792 | | comdat and have the same comdat name, or both sections must |
2793 | | be non-comdat. LTO IR plugin sections are an exception. They |
2794 | | are always named .gnu.linkonce.t.<key> (<key> is some string) |
2795 | | and match any comdat section with comdat name of <key>, and |
2796 | | any linkonce section with the same suffix, ie. |
2797 | | .gnu.linkonce.*.<key>. */ |
2798 | 0 | if (((s_comdat != NULL) == (l_comdat != NULL) |
2799 | 0 | && strcmp (name, l->sec->name) == 0) |
2800 | 0 | || (l->sec->owner->flags & BFD_PLUGIN) != 0 |
2801 | 0 | || (sec->owner->flags & BFD_PLUGIN) != 0) |
2802 | 0 | { |
2803 | | /* The section has already been linked. See if we should |
2804 | | issue a warning. */ |
2805 | 0 | return _bfd_handle_already_linked (sec, l, info); |
2806 | 0 | } |
2807 | 0 | } |
2808 | | |
2809 | | /* This is the first section with this name. Record it. */ |
2810 | 0 | if (!bfd_section_already_linked_table_insert (already_linked_list, sec)) |
2811 | 0 | info->callbacks->einfo (_("%F%P: already_linked_table: %E\n")); |
2812 | 0 | return false; |
2813 | 0 | } |
2814 | | |
2815 | | /* Initialize COOKIE for input bfd ABFD. */ |
2816 | | |
2817 | | static bool |
2818 | | init_reloc_cookie (struct coff_reloc_cookie *cookie, |
2819 | | struct bfd_link_info *info ATTRIBUTE_UNUSED, |
2820 | | bfd *abfd) |
2821 | 0 | { |
2822 | | /* Sometimes the symbol table does not yet have been loaded here. */ |
2823 | 0 | bfd_coff_slurp_symbol_table (abfd); |
2824 | |
|
2825 | 0 | cookie->abfd = abfd; |
2826 | 0 | cookie->sym_hashes = obj_coff_sym_hashes (abfd); |
2827 | |
|
2828 | 0 | cookie->symbols = obj_symbols (abfd); |
2829 | |
|
2830 | 0 | return true; |
2831 | 0 | } |
2832 | | |
2833 | | /* Free the memory allocated by init_reloc_cookie, if appropriate. */ |
2834 | | |
2835 | | static void |
2836 | | fini_reloc_cookie (struct coff_reloc_cookie *cookie ATTRIBUTE_UNUSED, |
2837 | | bfd *abfd ATTRIBUTE_UNUSED) |
2838 | 0 | { |
2839 | | /* Nothing to do. */ |
2840 | 0 | } |
2841 | | |
2842 | | /* Initialize the relocation information in COOKIE for input section SEC |
2843 | | of input bfd ABFD. */ |
2844 | | |
2845 | | static bool |
2846 | | init_reloc_cookie_rels (struct coff_reloc_cookie *cookie, |
2847 | | struct bfd_link_info *info ATTRIBUTE_UNUSED, |
2848 | | bfd *abfd, |
2849 | | asection *sec) |
2850 | 0 | { |
2851 | 0 | if (sec->reloc_count == 0) |
2852 | 0 | { |
2853 | 0 | cookie->rels = NULL; |
2854 | 0 | cookie->relend = NULL; |
2855 | 0 | cookie->rel = NULL; |
2856 | 0 | return true; |
2857 | 0 | } |
2858 | | |
2859 | 0 | cookie->rels = _bfd_coff_read_internal_relocs (abfd, sec, false, NULL, |
2860 | 0 | 0, NULL); |
2861 | |
|
2862 | 0 | if (cookie->rels == NULL) |
2863 | 0 | return false; |
2864 | | |
2865 | 0 | cookie->rel = cookie->rels; |
2866 | 0 | cookie->relend = (cookie->rels + sec->reloc_count); |
2867 | 0 | return true; |
2868 | 0 | } |
2869 | | |
2870 | | /* Free the memory allocated by init_reloc_cookie_rels, |
2871 | | if appropriate. */ |
2872 | | |
2873 | | static void |
2874 | | fini_reloc_cookie_rels (struct coff_reloc_cookie *cookie, |
2875 | | asection *sec) |
2876 | 0 | { |
2877 | 0 | if (cookie->rels |
2878 | | /* PR 20401. The relocs may not have been cached, so check first. |
2879 | | If the relocs were loaded by init_reloc_cookie_rels() then this |
2880 | | will be the case. FIXME: Would performance be improved if the |
2881 | | relocs *were* cached ? */ |
2882 | 0 | && coff_section_data (NULL, sec) |
2883 | 0 | && coff_section_data (NULL, sec)->relocs != cookie->rels) |
2884 | 0 | free (cookie->rels); |
2885 | 0 | } |
2886 | | |
2887 | | /* Initialize the whole of COOKIE for input section SEC. */ |
2888 | | |
2889 | | static bool |
2890 | | init_reloc_cookie_for_section (struct coff_reloc_cookie *cookie, |
2891 | | struct bfd_link_info *info, |
2892 | | asection *sec) |
2893 | 0 | { |
2894 | 0 | if (!init_reloc_cookie (cookie, info, sec->owner)) |
2895 | 0 | return false; |
2896 | | |
2897 | 0 | if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec)) |
2898 | 0 | { |
2899 | 0 | fini_reloc_cookie (cookie, sec->owner); |
2900 | 0 | return false; |
2901 | 0 | } |
2902 | 0 | return true; |
2903 | 0 | } |
2904 | | |
2905 | | /* Free the memory allocated by init_reloc_cookie_for_section, |
2906 | | if appropriate. */ |
2907 | | |
2908 | | static void |
2909 | | fini_reloc_cookie_for_section (struct coff_reloc_cookie *cookie, |
2910 | | asection *sec) |
2911 | 0 | { |
2912 | 0 | fini_reloc_cookie_rels (cookie, sec); |
2913 | 0 | fini_reloc_cookie (cookie, sec->owner); |
2914 | 0 | } |
2915 | | |
2916 | | static asection * |
2917 | | _bfd_coff_gc_mark_hook (asection *sec, |
2918 | | struct bfd_link_info *info ATTRIBUTE_UNUSED, |
2919 | | struct internal_reloc *rel ATTRIBUTE_UNUSED, |
2920 | | struct coff_link_hash_entry *h, |
2921 | | struct internal_syment *sym) |
2922 | 0 | { |
2923 | 0 | if (h != NULL) |
2924 | 0 | { |
2925 | 0 | switch (h->root.type) |
2926 | 0 | { |
2927 | 0 | case bfd_link_hash_defined: |
2928 | 0 | case bfd_link_hash_defweak: |
2929 | 0 | return h->root.u.def.section; |
2930 | | |
2931 | 0 | case bfd_link_hash_common: |
2932 | 0 | return h->root.u.c.p->section; |
2933 | | |
2934 | 0 | case bfd_link_hash_undefweak: |
2935 | 0 | if (h->symbol_class == C_NT_WEAK && h->numaux == 1) |
2936 | 0 | { |
2937 | | /* PE weak externals. A weak symbol may include an auxiliary |
2938 | | record indicating that if the weak symbol is not resolved, |
2939 | | another external symbol is used instead. */ |
2940 | 0 | struct coff_link_hash_entry *h2 = |
2941 | 0 | h->auxbfd->tdata.coff_obj_data->sym_hashes |
2942 | 0 | [h->aux->x_sym.x_tagndx.u32]; |
2943 | |
|
2944 | 0 | if (h2 && h2->root.type != bfd_link_hash_undefined) |
2945 | 0 | return h2->root.u.def.section; |
2946 | 0 | } |
2947 | 0 | break; |
2948 | | |
2949 | 0 | case bfd_link_hash_undefined: |
2950 | 0 | default: |
2951 | 0 | break; |
2952 | 0 | } |
2953 | 0 | return NULL; |
2954 | 0 | } |
2955 | | |
2956 | 0 | return coff_section_from_bfd_index (sec->owner, sym->n_scnum); |
2957 | 0 | } |
2958 | | |
2959 | | /* COOKIE->rel describes a relocation against section SEC, which is |
2960 | | a section we've decided to keep. Return the section that contains |
2961 | | the relocation symbol, or NULL if no section contains it. */ |
2962 | | |
2963 | | static asection * |
2964 | | _bfd_coff_gc_mark_rsec (struct bfd_link_info *info, asection *sec, |
2965 | | coff_gc_mark_hook_fn gc_mark_hook, |
2966 | | struct coff_reloc_cookie *cookie) |
2967 | 0 | { |
2968 | 0 | struct coff_link_hash_entry *h; |
2969 | |
|
2970 | 0 | h = cookie->sym_hashes[cookie->rel->r_symndx]; |
2971 | 0 | if (h != NULL) |
2972 | 0 | { |
2973 | 0 | while (h->root.type == bfd_link_hash_indirect |
2974 | 0 | || h->root.type == bfd_link_hash_warning) |
2975 | 0 | h = (struct coff_link_hash_entry *) h->root.u.i.link; |
2976 | |
|
2977 | 0 | return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL); |
2978 | 0 | } |
2979 | | |
2980 | 0 | return (*gc_mark_hook) (sec, info, cookie->rel, NULL, |
2981 | 0 | &(cookie->symbols |
2982 | 0 | + obj_convert (sec->owner)[cookie->rel->r_symndx])->native->u.syment); |
2983 | 0 | } |
2984 | | |
2985 | | static bool _bfd_coff_gc_mark |
2986 | | (struct bfd_link_info *, asection *, coff_gc_mark_hook_fn); |
2987 | | |
2988 | | /* COOKIE->rel describes a relocation against section SEC, which is |
2989 | | a section we've decided to keep. Mark the section that contains |
2990 | | the relocation symbol. */ |
2991 | | |
2992 | | static bool |
2993 | | _bfd_coff_gc_mark_reloc (struct bfd_link_info *info, |
2994 | | asection *sec, |
2995 | | coff_gc_mark_hook_fn gc_mark_hook, |
2996 | | struct coff_reloc_cookie *cookie) |
2997 | 0 | { |
2998 | 0 | asection *rsec; |
2999 | |
|
3000 | 0 | rsec = _bfd_coff_gc_mark_rsec (info, sec, gc_mark_hook, cookie); |
3001 | 0 | if (rsec && !rsec->gc_mark) |
3002 | 0 | { |
3003 | 0 | if (bfd_get_flavour (rsec->owner) != bfd_target_coff_flavour) |
3004 | 0 | rsec->gc_mark = 1; |
3005 | 0 | else if (!_bfd_coff_gc_mark (info, rsec, gc_mark_hook)) |
3006 | 0 | return false; |
3007 | 0 | } |
3008 | 0 | return true; |
3009 | 0 | } |
3010 | | |
3011 | | /* The mark phase of garbage collection. For a given section, mark |
3012 | | it and any sections in this section's group, and all the sections |
3013 | | which define symbols to which it refers. */ |
3014 | | |
3015 | | static bool |
3016 | | _bfd_coff_gc_mark (struct bfd_link_info *info, |
3017 | | asection *sec, |
3018 | | coff_gc_mark_hook_fn gc_mark_hook) |
3019 | 0 | { |
3020 | 0 | bool ret = true; |
3021 | |
|
3022 | 0 | sec->gc_mark = 1; |
3023 | | |
3024 | | /* Look through the section relocs. */ |
3025 | 0 | if ((sec->flags & SEC_RELOC) != 0 |
3026 | 0 | && sec->reloc_count > 0) |
3027 | 0 | { |
3028 | 0 | struct coff_reloc_cookie cookie; |
3029 | |
|
3030 | 0 | if (!init_reloc_cookie_for_section (&cookie, info, sec)) |
3031 | 0 | ret = false; |
3032 | 0 | else |
3033 | 0 | { |
3034 | 0 | for (; cookie.rel < cookie.relend; cookie.rel++) |
3035 | 0 | { |
3036 | 0 | if (!_bfd_coff_gc_mark_reloc (info, sec, gc_mark_hook, &cookie)) |
3037 | 0 | { |
3038 | 0 | ret = false; |
3039 | 0 | break; |
3040 | 0 | } |
3041 | 0 | } |
3042 | 0 | fini_reloc_cookie_for_section (&cookie, sec); |
3043 | 0 | } |
3044 | 0 | } |
3045 | |
|
3046 | 0 | return ret; |
3047 | 0 | } |
3048 | | |
3049 | | static bool |
3050 | | _bfd_coff_gc_mark_extra_sections (struct bfd_link_info *info, |
3051 | | coff_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED) |
3052 | 0 | { |
3053 | 0 | bfd *ibfd; |
3054 | |
|
3055 | 0 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
3056 | 0 | { |
3057 | 0 | asection *isec; |
3058 | 0 | bool some_kept; |
3059 | |
|
3060 | 0 | if (bfd_get_flavour (ibfd) != bfd_target_coff_flavour) |
3061 | 0 | continue; |
3062 | | |
3063 | | /* Ensure all linker created sections are kept, and see whether |
3064 | | any other section is already marked. */ |
3065 | 0 | some_kept = false; |
3066 | 0 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
3067 | 0 | { |
3068 | 0 | if ((isec->flags & SEC_LINKER_CREATED) != 0) |
3069 | 0 | isec->gc_mark = 1; |
3070 | 0 | else if (isec->gc_mark) |
3071 | 0 | some_kept = true; |
3072 | 0 | } |
3073 | | |
3074 | | /* If no section in this file will be kept, then we can |
3075 | | toss out debug sections. */ |
3076 | 0 | if (!some_kept) |
3077 | 0 | continue; |
3078 | | |
3079 | | /* Keep debug and special sections like .comment when they are |
3080 | | not part of a group, or when we have single-member groups. */ |
3081 | 0 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
3082 | 0 | if ((isec->flags & SEC_DEBUGGING) != 0 |
3083 | 0 | || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0) |
3084 | 0 | isec->gc_mark = 1; |
3085 | 0 | } |
3086 | 0 | return true; |
3087 | 0 | } |
3088 | | |
3089 | | /* Sweep symbols in swept sections. Called via coff_link_hash_traverse. */ |
3090 | | |
3091 | | static bool |
3092 | | coff_gc_sweep_symbol (struct coff_link_hash_entry *h, |
3093 | | void *data ATTRIBUTE_UNUSED) |
3094 | 0 | { |
3095 | 0 | if (h->root.type == bfd_link_hash_warning) |
3096 | 0 | h = (struct coff_link_hash_entry *) h->root.u.i.link; |
3097 | |
|
3098 | 0 | if ((h->root.type == bfd_link_hash_defined |
3099 | 0 | || h->root.type == bfd_link_hash_defweak) |
3100 | 0 | && !h->root.u.def.section->gc_mark |
3101 | 0 | && !(h->root.u.def.section->owner->flags & DYNAMIC)) |
3102 | 0 | { |
3103 | | /* Do our best to hide the symbol. */ |
3104 | 0 | h->root.u.def.section = bfd_und_section_ptr; |
3105 | 0 | h->symbol_class = C_HIDDEN; |
3106 | 0 | } |
3107 | |
|
3108 | 0 | return true; |
3109 | 0 | } |
3110 | | |
3111 | | /* The sweep phase of garbage collection. Remove all garbage sections. */ |
3112 | | |
3113 | | typedef bool (*gc_sweep_hook_fn) |
3114 | | (bfd *, struct bfd_link_info *, asection *, const struct internal_reloc *); |
3115 | | |
3116 | | static bool |
3117 | | coff_gc_sweep (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *info) |
3118 | 0 | { |
3119 | 0 | bfd *sub; |
3120 | |
|
3121 | 0 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
3122 | 0 | { |
3123 | 0 | asection *o; |
3124 | |
|
3125 | 0 | if (bfd_get_flavour (sub) != bfd_target_coff_flavour) |
3126 | 0 | continue; |
3127 | | |
3128 | 0 | for (o = sub->sections; o != NULL; o = o->next) |
3129 | 0 | { |
3130 | | /* Keep debug and special sections. */ |
3131 | 0 | if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0 |
3132 | 0 | || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0) |
3133 | 0 | o->gc_mark = 1; |
3134 | 0 | else if (startswith (o->name, ".idata") |
3135 | 0 | || startswith (o->name, ".pdata") |
3136 | 0 | || startswith (o->name, ".xdata") |
3137 | 0 | || startswith (o->name, ".rsrc")) |
3138 | 0 | o->gc_mark = 1; |
3139 | |
|
3140 | 0 | if (o->gc_mark) |
3141 | 0 | continue; |
3142 | | |
3143 | | /* Skip sweeping sections already excluded. */ |
3144 | 0 | if (o->flags & SEC_EXCLUDE) |
3145 | 0 | continue; |
3146 | | |
3147 | | /* Since this is early in the link process, it is simple |
3148 | | to remove a section from the output. */ |
3149 | 0 | o->flags |= SEC_EXCLUDE; |
3150 | |
|
3151 | 0 | if (info->print_gc_sections && o->size != 0) |
3152 | | /* xgettext: c-format */ |
3153 | 0 | _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"), |
3154 | 0 | o, sub); |
3155 | |
|
3156 | | #if 0 |
3157 | | /* But we also have to update some of the relocation |
3158 | | info we collected before. */ |
3159 | | if (gc_sweep_hook |
3160 | | && (o->flags & SEC_RELOC) != 0 |
3161 | | && o->reloc_count > 0 |
3162 | | && !bfd_is_abs_section (o->output_section)) |
3163 | | { |
3164 | | struct internal_reloc *internal_relocs; |
3165 | | bool r; |
3166 | | |
3167 | | internal_relocs |
3168 | | = _bfd_coff_link_read_relocs (o->owner, o, NULL, NULL, |
3169 | | info->keep_memory); |
3170 | | if (internal_relocs == NULL) |
3171 | | return false; |
3172 | | |
3173 | | r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs); |
3174 | | |
3175 | | if (coff_section_data (o)->relocs != internal_relocs) |
3176 | | free (internal_relocs); |
3177 | | |
3178 | | if (!r) |
3179 | | return false; |
3180 | | } |
3181 | | #endif |
3182 | 0 | } |
3183 | 0 | } |
3184 | | |
3185 | | /* Remove the symbols that were in the swept sections from the dynamic |
3186 | | symbol table. */ |
3187 | 0 | coff_link_hash_traverse (coff_hash_table (info), coff_gc_sweep_symbol, |
3188 | 0 | NULL); |
3189 | |
|
3190 | 0 | return true; |
3191 | 0 | } |
3192 | | |
3193 | | /* Keep all sections containing symbols undefined on the command-line, |
3194 | | and the section containing the entry symbol. */ |
3195 | | |
3196 | | static void |
3197 | | _bfd_coff_gc_keep (struct bfd_link_info *info) |
3198 | 0 | { |
3199 | 0 | struct bfd_sym_chain *sym; |
3200 | |
|
3201 | 0 | for (sym = info->gc_sym_list; sym != NULL; sym = sym->next) |
3202 | 0 | { |
3203 | 0 | struct coff_link_hash_entry *h; |
3204 | |
|
3205 | 0 | h = coff_link_hash_lookup (coff_hash_table (info), sym->name, |
3206 | 0 | false, false, false); |
3207 | |
|
3208 | 0 | if (h != NULL |
3209 | 0 | && (h->root.type == bfd_link_hash_defined |
3210 | 0 | || h->root.type == bfd_link_hash_defweak) |
3211 | 0 | && !bfd_is_abs_section (h->root.u.def.section)) |
3212 | 0 | h->root.u.def.section->flags |= SEC_KEEP; |
3213 | 0 | } |
3214 | 0 | } |
3215 | | |
3216 | | /* Do mark and sweep of unused sections. */ |
3217 | | |
3218 | | bool |
3219 | | bfd_coff_gc_sections (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *info) |
3220 | 0 | { |
3221 | 0 | bfd *sub; |
3222 | | |
3223 | | /* FIXME: Should we implement this? */ |
3224 | | #if 0 |
3225 | | const bfd_coff_backend_data *bed = coff_backend_info (abfd); |
3226 | | |
3227 | | if (!bed->can_gc_sections |
3228 | | || !is_coff_hash_table (info->hash)) |
3229 | | { |
3230 | | _bfd_error_handler(_("warning: gc-sections option ignored")); |
3231 | | return true; |
3232 | | } |
3233 | | #endif |
3234 | |
|
3235 | 0 | _bfd_coff_gc_keep (info); |
3236 | | |
3237 | | /* Grovel through relocs to find out who stays ... */ |
3238 | 0 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
3239 | 0 | { |
3240 | 0 | asection *o; |
3241 | |
|
3242 | 0 | if (bfd_get_flavour (sub) != bfd_target_coff_flavour) |
3243 | 0 | continue; |
3244 | | |
3245 | 0 | for (o = sub->sections; o != NULL; o = o->next) |
3246 | 0 | { |
3247 | 0 | if (((o->flags & (SEC_EXCLUDE | SEC_KEEP)) == SEC_KEEP |
3248 | 0 | || startswith (o->name, ".vectors") |
3249 | 0 | || startswith (o->name, ".ctors") |
3250 | 0 | || startswith (o->name, ".dtors")) |
3251 | 0 | && !o->gc_mark) |
3252 | 0 | { |
3253 | 0 | if (!_bfd_coff_gc_mark (info, o, _bfd_coff_gc_mark_hook)) |
3254 | 0 | return false; |
3255 | 0 | } |
3256 | 0 | } |
3257 | 0 | } |
3258 | | |
3259 | | /* Allow the backend to mark additional target specific sections. */ |
3260 | 0 | _bfd_coff_gc_mark_extra_sections (info, _bfd_coff_gc_mark_hook); |
3261 | | |
3262 | | /* ... and mark SEC_EXCLUDE for those that go. */ |
3263 | 0 | return coff_gc_sweep (abfd, info); |
3264 | 0 | } |
3265 | | |
3266 | | /* Return name used to identify a comdat group. */ |
3267 | | |
3268 | | const char * |
3269 | | bfd_coff_group_name (bfd *abfd, const asection *sec) |
3270 | 0 | { |
3271 | 0 | struct coff_comdat_info *ci = bfd_coff_get_comdat_section (abfd, sec); |
3272 | 0 | if (ci != NULL) |
3273 | 0 | return ci->name; |
3274 | 0 | return NULL; |
3275 | 0 | } |
3276 | | |
3277 | | bool |
3278 | | _bfd_coff_free_cached_info (bfd *abfd) |
3279 | 256k | { |
3280 | 256k | struct coff_tdata *tdata; |
3281 | | |
3282 | 256k | if (bfd_family_coff (abfd) |
3283 | 256k | && (bfd_get_format (abfd) == bfd_object |
3284 | 256k | || bfd_get_format (abfd) == bfd_core) |
3285 | 256k | && (tdata = coff_data (abfd)) != NULL) |
3286 | 239k | { |
3287 | 239k | if (tdata->section_by_index) |
3288 | 0 | { |
3289 | 0 | htab_delete (tdata->section_by_index); |
3290 | 0 | tdata->section_by_index = NULL; |
3291 | 0 | } |
3292 | | |
3293 | 239k | if (tdata->section_by_target_index) |
3294 | 37.1k | { |
3295 | 37.1k | htab_delete (tdata->section_by_target_index); |
3296 | 37.1k | tdata->section_by_target_index = NULL; |
3297 | 37.1k | } |
3298 | | |
3299 | 239k | _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info); |
3300 | 239k | _bfd_stab_cleanup (abfd, &tdata->line_info); |
3301 | | |
3302 | | /* PR 25447: |
3303 | | Do not clear the keep_syms and keep_strings flags. |
3304 | | These may have been set by pe_ILF_build_a_bfd() indicating |
3305 | | that the syms and strings pointers are not to be freed. */ |
3306 | 239k | if (!_bfd_coff_free_symbols (abfd)) |
3307 | 0 | return false; |
3308 | 239k | } |
3309 | | |
3310 | 256k | return _bfd_generic_bfd_free_cached_info (abfd); |
3311 | 256k | } |