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