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