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