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