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