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