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