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