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