2011-12-16 Shinichiro Hamaji <shinichiro.hamaji@gmail.com>
[binutils.git] / bfd / coffgen.c
blobc0505c6c02b810adbea251c5db1d550c454d914a
1 /* Support for the generic parts of COFF, for BFD.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
5 Written by Cygnus Support.
7 This file is part of BFD, the Binary File Descriptor library.
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
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
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.
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
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
24 /* Most of this hacked by Steve Chamberlain, sac@cygnus.com.
25 Split out of coffcode.h by Ian Taylor, ian@cygnus.com. */
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,
32 ********** DO NOT PUT TARGET SPECIFIC CODE IN THIS FILE **********
34 If you need to add some target specific behaviour, add a new hook
35 function to bfd_coff_backend_data.
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). */
41 #include "sysdep.h"
42 #include "bfd.h"
43 #include "libbfd.h"
44 #include "coff/internal.h"
45 #include "libcoff.h"
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. */
50 static bfd_boolean
51 make_a_section_from_file (bfd *abfd,
52 struct internal_scnhdr *hdr,
53 unsigned int target_index)
55 asection *return_section;
56 char *name;
57 bfd_boolean result = TRUE;
58 flagword flags;
60 name = NULL;
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))
69 && hdr->s_name[0] == '/')
71 char buf[SCNNMLEN];
72 long strindex;
73 char *p;
74 const char *strings;
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);
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)
86 strings = _bfd_coff_read_string_table (abfd);
87 if (strings == NULL)
88 return FALSE;
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;
93 name = (char *) bfd_alloc (abfd,
94 (bfd_size_type) strlen (strings) + 1);
95 if (name == NULL)
96 return FALSE;
97 strcpy (name, strings);
101 if (name == NULL)
103 /* Assorted wastage to null-terminate the name, thanks AT&T! */
104 name = (char *) bfd_alloc (abfd,
105 (bfd_size_type) sizeof (hdr->s_name) + 1);
106 if (name == NULL)
107 return FALSE;
108 strncpy (name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
109 name[sizeof (hdr->s_name)] = 0;
112 return_section = bfd_make_section_anyway (abfd, name);
113 if (return_section == NULL)
114 return FALSE;
116 return_section->vma = hdr->s_vaddr;
117 return_section->lma = hdr->s_paddr;
118 return_section->size = hdr->s_size;
119 return_section->filepos = hdr->s_scnptr;
120 return_section->rel_filepos = hdr->s_relptr;
121 return_section->reloc_count = hdr->s_nreloc;
123 bfd_coff_set_alignment_hook (abfd, return_section, hdr);
125 return_section->line_filepos = hdr->s_lnnoptr;
127 return_section->lineno_count = hdr->s_nlnno;
128 return_section->userdata = NULL;
129 return_section->next = NULL;
130 return_section->target_index = target_index;
132 if (! bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name, return_section,
133 & flags))
134 result = FALSE;
136 return_section->flags = flags;
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;
143 if (hdr->s_nreloc != 0)
144 return_section->flags |= SEC_RELOC;
145 /* FIXME: should this check 'hdr->s_size > 0'. */
146 if (hdr->s_scnptr != 0)
147 return_section->flags |= SEC_HAS_CONTENTS;
149 return result;
152 /* Read in a COFF object and make it into a BFD. This is used by
153 ECOFF as well. */
155 static const bfd_target *
156 coff_real_object_p (bfd *abfd,
157 unsigned nscns,
158 struct internal_filehdr *internal_f,
159 struct internal_aouthdr *internal_a)
161 flagword oflags = abfd->flags;
162 bfd_vma ostart = bfd_get_start_address (abfd);
163 void * tdata;
164 void * tdata_save;
165 bfd_size_type readsize; /* Length of file_info. */
166 unsigned int scnhsz;
167 char *external_sections;
169 if (!(internal_f->f_flags & F_RELFLG))
170 abfd->flags |= HAS_RELOC;
171 if ((internal_f->f_flags & F_EXEC))
172 abfd->flags |= EXEC_P;
173 if (!(internal_f->f_flags & F_LNNO))
174 abfd->flags |= HAS_LINENO;
175 if (!(internal_f->f_flags & F_LSYMS))
176 abfd->flags |= HAS_LOCALS;
178 /* FIXME: How can we set D_PAGED correctly? */
179 if ((internal_f->f_flags & F_EXEC) != 0)
180 abfd->flags |= D_PAGED;
182 bfd_get_symcount (abfd) = internal_f->f_nsyms;
183 if (internal_f->f_nsyms)
184 abfd->flags |= HAS_SYMS;
186 if (internal_a != (struct internal_aouthdr *) NULL)
187 bfd_get_start_address (abfd) = internal_a->entry;
188 else
189 bfd_get_start_address (abfd) = 0;
191 /* Set up the tdata area. ECOFF uses its own routine, and overrides
192 abfd->flags. */
193 tdata_save = abfd->tdata.any;
194 tdata = bfd_coff_mkobject_hook (abfd, (void *) internal_f, (void *) internal_a);
195 if (tdata == NULL)
196 goto fail2;
198 scnhsz = bfd_coff_scnhsz (abfd);
199 readsize = (bfd_size_type) nscns * scnhsz;
200 external_sections = (char *) bfd_alloc (abfd, readsize);
201 if (!external_sections)
202 goto fail;
204 if (bfd_bread ((void *) external_sections, readsize, abfd) != readsize)
205 goto fail;
207 /* Set the arch/mach *before* swapping in sections; section header swapping
208 may depend on arch/mach info. */
209 if (! bfd_coff_set_arch_mach_hook (abfd, (void *) internal_f))
210 goto fail;
212 /* Now copy data as required; construct all asections etc. */
213 if (nscns != 0)
215 unsigned int i;
216 for (i = 0; i < nscns; i++)
218 struct internal_scnhdr tmp;
219 bfd_coff_swap_scnhdr_in (abfd,
220 (void *) (external_sections + i * scnhsz),
221 (void *) & tmp);
222 if (! make_a_section_from_file (abfd, &tmp, i + 1))
223 goto fail;
227 return abfd->xvec;
229 fail:
230 bfd_release (abfd, tdata);
231 fail2:
232 abfd->tdata.any = tdata_save;
233 abfd->flags = oflags;
234 bfd_get_start_address (abfd) = ostart;
235 return (const bfd_target *) NULL;
238 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
239 not a COFF file. This is also used by ECOFF. */
241 const bfd_target *
242 coff_object_p (bfd *abfd)
244 bfd_size_type filhsz;
245 bfd_size_type aoutsz;
246 unsigned int nscns;
247 void * filehdr;
248 struct internal_filehdr internal_f;
249 struct internal_aouthdr internal_a;
251 /* Figure out how much to read. */
252 filhsz = bfd_coff_filhsz (abfd);
253 aoutsz = bfd_coff_aoutsz (abfd);
255 filehdr = bfd_alloc (abfd, filhsz);
256 if (filehdr == NULL)
257 return NULL;
258 if (bfd_bread (filehdr, filhsz, abfd) != filhsz)
260 if (bfd_get_error () != bfd_error_system_call)
261 bfd_set_error (bfd_error_wrong_format);
262 bfd_release (abfd, filehdr);
263 return NULL;
265 bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f);
266 bfd_release (abfd, filehdr);
268 /* The XCOFF format has two sizes for the f_opthdr. SMALL_AOUTSZ
269 (less than aoutsz) used in object files and AOUTSZ (equal to
270 aoutsz) in executables. The bfd_coff_swap_aouthdr_in function
271 expects this header to be aoutsz bytes in length, so we use that
272 value in the call to bfd_alloc below. But we must be careful to
273 only read in f_opthdr bytes in the call to bfd_bread. We should
274 also attempt to catch corrupt or non-COFF binaries with a strange
275 value for f_opthdr. */
276 if (! bfd_coff_bad_format_hook (abfd, &internal_f)
277 || internal_f.f_opthdr > aoutsz)
279 bfd_set_error (bfd_error_wrong_format);
280 return NULL;
282 nscns = internal_f.f_nscns;
284 if (internal_f.f_opthdr)
286 void * opthdr;
288 opthdr = bfd_alloc (abfd, aoutsz);
289 if (opthdr == NULL)
290 return NULL;
291 if (bfd_bread (opthdr, (bfd_size_type) internal_f.f_opthdr, abfd)
292 != internal_f.f_opthdr)
294 bfd_release (abfd, opthdr);
295 return NULL;
297 bfd_coff_swap_aouthdr_in (abfd, opthdr, (void *) &internal_a);
298 bfd_release (abfd, opthdr);
301 return coff_real_object_p (abfd, nscns, &internal_f,
302 (internal_f.f_opthdr != 0
303 ? &internal_a
304 : (struct internal_aouthdr *) NULL));
307 /* Get the BFD section from a COFF symbol section number. */
309 asection *
310 coff_section_from_bfd_index (bfd *abfd, int section_index)
312 struct bfd_section *answer = abfd->sections;
314 if (section_index == N_ABS)
315 return bfd_abs_section_ptr;
316 if (section_index == N_UNDEF)
317 return bfd_und_section_ptr;
318 if (section_index == N_DEBUG)
319 return bfd_abs_section_ptr;
321 while (answer)
323 if (answer->target_index == section_index)
324 return answer;
325 answer = answer->next;
328 /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
329 has a bad symbol table in biglitpow.o. */
330 return bfd_und_section_ptr;
333 /* Get the upper bound of a COFF symbol table. */
335 long
336 coff_get_symtab_upper_bound (bfd *abfd)
338 if (!bfd_coff_slurp_symbol_table (abfd))
339 return -1;
341 return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *));
344 /* Canonicalize a COFF symbol table. */
346 long
347 coff_canonicalize_symtab (bfd *abfd, asymbol **alocation)
349 unsigned int counter;
350 coff_symbol_type *symbase;
351 coff_symbol_type **location = (coff_symbol_type **) alocation;
353 if (!bfd_coff_slurp_symbol_table (abfd))
354 return -1;
356 symbase = obj_symbols (abfd);
357 counter = bfd_get_symcount (abfd);
358 while (counter-- > 0)
359 *location++ = symbase++;
361 *location = NULL;
363 return bfd_get_symcount (abfd);
366 /* Get the name of a symbol. The caller must pass in a buffer of size
367 >= SYMNMLEN + 1. */
369 const char *
370 _bfd_coff_internal_syment_name (bfd *abfd,
371 const struct internal_syment *sym,
372 char *buf)
374 /* FIXME: It's not clear this will work correctly if sizeof
375 (_n_zeroes) != 4. */
376 if (sym->_n._n_n._n_zeroes != 0
377 || sym->_n._n_n._n_offset == 0)
379 memcpy (buf, sym->_n._n_name, SYMNMLEN);
380 buf[SYMNMLEN] = '\0';
381 return buf;
383 else
385 const char *strings;
387 BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE);
388 strings = obj_coff_strings (abfd);
389 if (strings == NULL)
391 strings = _bfd_coff_read_string_table (abfd);
392 if (strings == NULL)
393 return NULL;
395 return strings + sym->_n._n_n._n_offset;
399 /* Read in and swap the relocs. This returns a buffer holding the
400 relocs for section SEC in file ABFD. If CACHE is TRUE and
401 INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
402 the function is called again. If EXTERNAL_RELOCS is not NULL, it
403 is a buffer large enough to hold the unswapped relocs. If
404 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
405 the swapped relocs. If REQUIRE_INTERNAL is TRUE, then the return
406 value must be INTERNAL_RELOCS. The function returns NULL on error. */
408 struct internal_reloc *
409 _bfd_coff_read_internal_relocs (bfd *abfd,
410 asection *sec,
411 bfd_boolean cache,
412 bfd_byte *external_relocs,
413 bfd_boolean require_internal,
414 struct internal_reloc *internal_relocs)
416 bfd_size_type relsz;
417 bfd_byte *free_external = NULL;
418 struct internal_reloc *free_internal = NULL;
419 bfd_byte *erel;
420 bfd_byte *erel_end;
421 struct internal_reloc *irel;
422 bfd_size_type amt;
424 if (sec->reloc_count == 0)
425 return internal_relocs; /* Nothing to do. */
427 if (coff_section_data (abfd, sec) != NULL
428 && coff_section_data (abfd, sec)->relocs != NULL)
430 if (! require_internal)
431 return coff_section_data (abfd, sec)->relocs;
432 memcpy (internal_relocs, coff_section_data (abfd, sec)->relocs,
433 sec->reloc_count * sizeof (struct internal_reloc));
434 return internal_relocs;
437 relsz = bfd_coff_relsz (abfd);
439 amt = sec->reloc_count * relsz;
440 if (external_relocs == NULL)
442 free_external = (bfd_byte *) bfd_malloc (amt);
443 if (free_external == NULL)
444 goto error_return;
445 external_relocs = free_external;
448 if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
449 || bfd_bread (external_relocs, amt, abfd) != amt)
450 goto error_return;
452 if (internal_relocs == NULL)
454 amt = sec->reloc_count;
455 amt *= sizeof (struct internal_reloc);
456 free_internal = (struct internal_reloc *) bfd_malloc (amt);
457 if (free_internal == NULL)
458 goto error_return;
459 internal_relocs = free_internal;
462 /* Swap in the relocs. */
463 erel = external_relocs;
464 erel_end = erel + relsz * sec->reloc_count;
465 irel = internal_relocs;
466 for (; erel < erel_end; erel += relsz, irel++)
467 bfd_coff_swap_reloc_in (abfd, (void *) erel, (void *) irel);
469 if (free_external != NULL)
471 free (free_external);
472 free_external = NULL;
475 if (cache && free_internal != NULL)
477 if (coff_section_data (abfd, sec) == NULL)
479 amt = sizeof (struct coff_section_tdata);
480 sec->used_by_bfd = bfd_zalloc (abfd, amt);
481 if (sec->used_by_bfd == NULL)
482 goto error_return;
483 coff_section_data (abfd, sec)->contents = NULL;
485 coff_section_data (abfd, sec)->relocs = free_internal;
488 return internal_relocs;
490 error_return:
491 if (free_external != NULL)
492 free (free_external);
493 if (free_internal != NULL)
494 free (free_internal);
495 return NULL;
498 /* Set lineno_count for the output sections of a COFF file. */
501 coff_count_linenumbers (bfd *abfd)
503 unsigned int limit = bfd_get_symcount (abfd);
504 unsigned int i;
505 int total = 0;
506 asymbol **p;
507 asection *s;
509 if (limit == 0)
511 /* This may be from the backend linker, in which case the
512 lineno_count in the sections is correct. */
513 for (s = abfd->sections; s != NULL; s = s->next)
514 total += s->lineno_count;
515 return total;
518 for (s = abfd->sections; s != NULL; s = s->next)
519 BFD_ASSERT (s->lineno_count == 0);
521 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
523 asymbol *q_maybe = *p;
525 if (bfd_family_coff (bfd_asymbol_bfd (q_maybe)))
527 coff_symbol_type *q = coffsymbol (q_maybe);
529 /* The AIX 4.1 compiler can sometimes generate line numbers
530 attached to debugging symbols. We try to simply ignore
531 those here. */
532 if (q->lineno != NULL
533 && q->symbol.section->owner != NULL)
535 /* This symbol has line numbers. Increment the owning
536 section's linenumber count. */
537 alent *l = q->lineno;
541 asection * sec = q->symbol.section->output_section;
543 /* Do not try to update fields in read-only sections. */
544 if (! bfd_is_const_section (sec))
545 sec->lineno_count ++;
547 ++total;
548 ++l;
550 while (l->line_number != 0);
555 return total;
558 /* Takes a bfd and a symbol, returns a pointer to the coff specific
559 area of the symbol if there is one. */
561 coff_symbol_type *
562 coff_symbol_from (bfd *ignore_abfd ATTRIBUTE_UNUSED,
563 asymbol *symbol)
565 if (!bfd_family_coff (bfd_asymbol_bfd (symbol)))
566 return (coff_symbol_type *) NULL;
568 if (bfd_asymbol_bfd (symbol)->tdata.coff_obj_data == (coff_data_type *) NULL)
569 return (coff_symbol_type *) NULL;
571 return (coff_symbol_type *) symbol;
574 static void
575 fixup_symbol_value (bfd *abfd,
576 coff_symbol_type *coff_symbol_ptr,
577 struct internal_syment *syment)
579 /* Normalize the symbol flags. */
580 if (coff_symbol_ptr->symbol.section
581 && bfd_is_com_section (coff_symbol_ptr->symbol.section))
583 /* A common symbol is undefined with a value. */
584 syment->n_scnum = N_UNDEF;
585 syment->n_value = coff_symbol_ptr->symbol.value;
587 else if ((coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) != 0
588 && (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING_RELOC) == 0)
590 syment->n_value = coff_symbol_ptr->symbol.value;
592 else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
594 syment->n_scnum = N_UNDEF;
595 syment->n_value = 0;
597 /* FIXME: Do we need to handle the absolute section here? */
598 else
600 if (coff_symbol_ptr->symbol.section)
602 syment->n_scnum =
603 coff_symbol_ptr->symbol.section->output_section->target_index;
605 syment->n_value = (coff_symbol_ptr->symbol.value
606 + coff_symbol_ptr->symbol.section->output_offset);
607 if (! obj_pe (abfd))
609 syment->n_value += (syment->n_sclass == C_STATLAB)
610 ? coff_symbol_ptr->symbol.section->output_section->lma
611 : coff_symbol_ptr->symbol.section->output_section->vma;
614 else
616 BFD_ASSERT (0);
617 /* This can happen, but I don't know why yet (steve@cygnus.com) */
618 syment->n_scnum = N_ABS;
619 syment->n_value = coff_symbol_ptr->symbol.value;
624 /* Run through all the symbols in the symbol table and work out what
625 their indexes into the symbol table will be when output.
627 Coff requires that each C_FILE symbol points to the next one in the
628 chain, and that the last one points to the first external symbol. We
629 do that here too. */
631 bfd_boolean
632 coff_renumber_symbols (bfd *bfd_ptr, int *first_undef)
634 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
635 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
636 unsigned int native_index = 0;
637 struct internal_syment *last_file = NULL;
638 unsigned int symbol_index;
640 /* COFF demands that undefined symbols come after all other symbols.
641 Since we don't need to impose this extra knowledge on all our
642 client programs, deal with that here. Sort the symbol table;
643 just move the undefined symbols to the end, leaving the rest
644 alone. The O'Reilly book says that defined global symbols come
645 at the end before the undefined symbols, so we do that here as
646 well. */
647 /* @@ Do we have some condition we could test for, so we don't always
648 have to do this? I don't think relocatability is quite right, but
649 I'm not certain. [raeburn:19920508.1711EST] */
651 asymbol **newsyms;
652 unsigned int i;
653 bfd_size_type amt;
655 amt = sizeof (asymbol *) * ((bfd_size_type) symbol_count + 1);
656 newsyms = (asymbol **) bfd_alloc (bfd_ptr, amt);
657 if (!newsyms)
658 return FALSE;
659 bfd_ptr->outsymbols = newsyms;
660 for (i = 0; i < symbol_count; i++)
661 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
662 || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
663 && !bfd_is_com_section (symbol_ptr_ptr[i]->section)
664 && ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) != 0
665 || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
666 == 0))))
667 *newsyms++ = symbol_ptr_ptr[i];
669 for (i = 0; i < symbol_count; i++)
670 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
671 && !bfd_is_und_section (symbol_ptr_ptr[i]->section)
672 && (bfd_is_com_section (symbol_ptr_ptr[i]->section)
673 || ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) == 0
674 && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
675 != 0))))
676 *newsyms++ = symbol_ptr_ptr[i];
678 *first_undef = newsyms - bfd_ptr->outsymbols;
680 for (i = 0; i < symbol_count; i++)
681 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
682 && bfd_is_und_section (symbol_ptr_ptr[i]->section))
683 *newsyms++ = symbol_ptr_ptr[i];
684 *newsyms = (asymbol *) NULL;
685 symbol_ptr_ptr = bfd_ptr->outsymbols;
688 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
690 coff_symbol_type *coff_symbol_ptr = coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
691 symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
692 if (coff_symbol_ptr && coff_symbol_ptr->native)
694 combined_entry_type *s = coff_symbol_ptr->native;
695 int i;
697 if (s->u.syment.n_sclass == C_FILE)
699 if (last_file != NULL)
700 last_file->n_value = native_index;
701 last_file = &(s->u.syment);
703 else
704 /* Modify the symbol values according to their section and
705 type. */
706 fixup_symbol_value (bfd_ptr, coff_symbol_ptr, &(s->u.syment));
708 for (i = 0; i < s->u.syment.n_numaux + 1; i++)
709 s[i].offset = native_index++;
711 else
712 native_index++;
715 obj_conv_table_size (bfd_ptr) = native_index;
717 return TRUE;
720 /* Run thorough the symbol table again, and fix it so that all
721 pointers to entries are changed to the entries' index in the output
722 symbol table. */
724 void
725 coff_mangle_symbols (bfd *bfd_ptr)
727 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
728 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
729 unsigned int symbol_index;
731 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
733 coff_symbol_type *coff_symbol_ptr =
734 coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
736 if (coff_symbol_ptr && coff_symbol_ptr->native)
738 int i;
739 combined_entry_type *s = coff_symbol_ptr->native;
741 if (s->fix_value)
743 /* FIXME: We should use a union here. */
744 s->u.syment.n_value =
745 (bfd_hostptr_t) ((combined_entry_type *)
746 ((bfd_hostptr_t) s->u.syment.n_value))->offset;
747 s->fix_value = 0;
749 if (s->fix_line)
751 /* The value is the offset into the line number entries
752 for the symbol's section. On output, the symbol's
753 section should be N_DEBUG. */
754 s->u.syment.n_value =
755 (coff_symbol_ptr->symbol.section->output_section->line_filepos
756 + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr));
757 coff_symbol_ptr->symbol.section =
758 coff_section_from_bfd_index (bfd_ptr, N_DEBUG);
759 BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
761 for (i = 0; i < s->u.syment.n_numaux; i++)
763 combined_entry_type *a = s + i + 1;
764 if (a->fix_tag)
766 a->u.auxent.x_sym.x_tagndx.l =
767 a->u.auxent.x_sym.x_tagndx.p->offset;
768 a->fix_tag = 0;
770 if (a->fix_end)
772 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
773 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
774 a->fix_end = 0;
776 if (a->fix_scnlen)
778 a->u.auxent.x_csect.x_scnlen.l =
779 a->u.auxent.x_csect.x_scnlen.p->offset;
780 a->fix_scnlen = 0;
787 static void
788 coff_fix_symbol_name (bfd *abfd,
789 asymbol *symbol,
790 combined_entry_type *native,
791 bfd_size_type *string_size_p,
792 asection **debug_string_section_p,
793 bfd_size_type *debug_string_size_p)
795 unsigned int name_length;
796 union internal_auxent *auxent;
797 char *name = (char *) (symbol->name);
799 if (name == NULL)
801 /* COFF symbols always have names, so we'll make one up. */
802 symbol->name = "strange";
803 name = (char *) symbol->name;
805 name_length = strlen (name);
807 if (native->u.syment.n_sclass == C_FILE
808 && native->u.syment.n_numaux > 0)
810 unsigned int filnmlen;
812 if (bfd_coff_force_symnames_in_strings (abfd))
814 native->u.syment._n._n_n._n_offset =
815 (*string_size_p + STRING_SIZE_SIZE);
816 native->u.syment._n._n_n._n_zeroes = 0;
817 *string_size_p += 6; /* strlen(".file") + 1 */
819 else
820 strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
822 auxent = &(native + 1)->u.auxent;
824 filnmlen = bfd_coff_filnmlen (abfd);
826 if (bfd_coff_long_filenames (abfd))
828 if (name_length <= filnmlen)
829 strncpy (auxent->x_file.x_fname, name, filnmlen);
830 else
832 auxent->x_file.x_n.x_offset = *string_size_p + STRING_SIZE_SIZE;
833 auxent->x_file.x_n.x_zeroes = 0;
834 *string_size_p += name_length + 1;
837 else
839 strncpy (auxent->x_file.x_fname, name, filnmlen);
840 if (name_length > filnmlen)
841 name[filnmlen] = '\0';
844 else
846 if (name_length <= SYMNMLEN && !bfd_coff_force_symnames_in_strings (abfd))
847 /* This name will fit into the symbol neatly. */
848 strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
850 else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
852 native->u.syment._n._n_n._n_offset = (*string_size_p
853 + STRING_SIZE_SIZE);
854 native->u.syment._n._n_n._n_zeroes = 0;
855 *string_size_p += name_length + 1;
857 else
859 file_ptr filepos;
860 bfd_byte buf[4];
861 int prefix_len = bfd_coff_debug_string_prefix_length (abfd);
863 /* This name should be written into the .debug section. For
864 some reason each name is preceded by a two byte length
865 and also followed by a null byte. FIXME: We assume that
866 the .debug section has already been created, and that it
867 is large enough. */
868 if (*debug_string_section_p == (asection *) NULL)
869 *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
870 filepos = bfd_tell (abfd);
871 if (prefix_len == 4)
872 bfd_put_32 (abfd, (bfd_vma) (name_length + 1), buf);
873 else
874 bfd_put_16 (abfd, (bfd_vma) (name_length + 1), buf);
876 if (!bfd_set_section_contents (abfd,
877 *debug_string_section_p,
878 (void *) buf,
879 (file_ptr) *debug_string_size_p,
880 (bfd_size_type) prefix_len)
881 || !bfd_set_section_contents (abfd,
882 *debug_string_section_p,
883 (void *) symbol->name,
884 (file_ptr) (*debug_string_size_p
885 + prefix_len),
886 (bfd_size_type) name_length + 1))
887 abort ();
888 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
889 abort ();
890 native->u.syment._n._n_n._n_offset =
891 *debug_string_size_p + prefix_len;
892 native->u.syment._n._n_n._n_zeroes = 0;
893 *debug_string_size_p += name_length + 1 + prefix_len;
898 /* We need to keep track of the symbol index so that when we write out
899 the relocs we can get the index for a symbol. This method is a
900 hack. FIXME. */
902 #define set_index(symbol, idx) ((symbol)->udata.i = (idx))
904 /* Write a symbol out to a COFF file. */
906 static bfd_boolean
907 coff_write_symbol (bfd *abfd,
908 asymbol *symbol,
909 combined_entry_type *native,
910 bfd_vma *written,
911 bfd_size_type *string_size_p,
912 asection **debug_string_section_p,
913 bfd_size_type *debug_string_size_p)
915 unsigned int numaux = native->u.syment.n_numaux;
916 int type = native->u.syment.n_type;
917 int n_sclass = (int) native->u.syment.n_sclass;
918 asection *output_section = symbol->section->output_section
919 ? symbol->section->output_section
920 : symbol->section;
921 void * buf;
922 bfd_size_type symesz;
924 if (native->u.syment.n_sclass == C_FILE)
925 symbol->flags |= BSF_DEBUGGING;
927 if (symbol->flags & BSF_DEBUGGING
928 && bfd_is_abs_section (symbol->section))
929 native->u.syment.n_scnum = N_DEBUG;
931 else if (bfd_is_abs_section (symbol->section))
932 native->u.syment.n_scnum = N_ABS;
934 else if (bfd_is_und_section (symbol->section))
935 native->u.syment.n_scnum = N_UNDEF;
937 else
938 native->u.syment.n_scnum =
939 output_section->target_index;
941 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
942 debug_string_section_p, debug_string_size_p);
944 symesz = bfd_coff_symesz (abfd);
945 buf = bfd_alloc (abfd, symesz);
946 if (!buf)
947 return FALSE;
948 bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
949 if (bfd_bwrite (buf, symesz, abfd) != symesz)
950 return FALSE;
951 bfd_release (abfd, buf);
953 if (native->u.syment.n_numaux > 0)
955 bfd_size_type auxesz;
956 unsigned int j;
958 auxesz = bfd_coff_auxesz (abfd);
959 buf = bfd_alloc (abfd, auxesz);
960 if (!buf)
961 return FALSE;
962 for (j = 0; j < native->u.syment.n_numaux; j++)
964 bfd_coff_swap_aux_out (abfd,
965 &((native + j + 1)->u.auxent),
966 type, n_sclass, (int) j,
967 native->u.syment.n_numaux,
968 buf);
969 if (bfd_bwrite (buf, auxesz, abfd) != auxesz)
970 return FALSE;
972 bfd_release (abfd, buf);
975 /* Store the index for use when we write out the relocs. */
976 set_index (symbol, *written);
978 *written += numaux + 1;
979 return TRUE;
982 /* Write out a symbol to a COFF file that does not come from a COFF
983 file originally. This symbol may have been created by the linker,
984 or we may be linking a non COFF file to a COFF file. */
986 bfd_boolean
987 coff_write_alien_symbol (bfd *abfd,
988 asymbol *symbol,
989 struct internal_syment *isym,
990 bfd_vma *written,
991 bfd_size_type *string_size_p,
992 asection **debug_string_section_p,
993 bfd_size_type *debug_string_size_p)
995 combined_entry_type *native;
996 combined_entry_type dummy[2];
997 asection *output_section = symbol->section->output_section
998 ? symbol->section->output_section
999 : symbol->section;
1000 struct bfd_link_info *link_info = coff_data (abfd)->link_info;
1001 bfd_boolean ret;
1003 if ((!link_info || link_info->strip_discarded)
1004 && !bfd_is_abs_section (symbol->section)
1005 && symbol->section->output_section == bfd_abs_section_ptr)
1007 symbol->name = "";
1008 if (isym != NULL)
1009 memset (isym, 0, sizeof(*isym));
1010 return TRUE;
1012 native = dummy;
1013 native->u.syment.n_type = T_NULL;
1014 native->u.syment.n_flags = 0;
1015 native->u.syment.n_numaux = 0;
1016 if (bfd_is_und_section (symbol->section))
1018 native->u.syment.n_scnum = N_UNDEF;
1019 native->u.syment.n_value = symbol->value;
1021 else if (bfd_is_com_section (symbol->section))
1023 native->u.syment.n_scnum = N_UNDEF;
1024 native->u.syment.n_value = symbol->value;
1026 else if (symbol->flags & BSF_FILE)
1028 native->u.syment.n_scnum = N_DEBUG;
1029 native->u.syment.n_numaux = 1;
1031 else if (symbol->flags & BSF_DEBUGGING)
1033 /* There isn't much point to writing out a debugging symbol
1034 unless we are prepared to convert it into COFF debugging
1035 format. So, we just ignore them. We must clobber the symbol
1036 name to keep it from being put in the string table. */
1037 symbol->name = "";
1038 if (isym != NULL)
1039 memset (isym, 0, sizeof(*isym));
1040 return TRUE;
1042 else
1044 native->u.syment.n_scnum = output_section->target_index;
1045 native->u.syment.n_value = (symbol->value
1046 + symbol->section->output_offset);
1047 if (! obj_pe (abfd))
1048 native->u.syment.n_value += output_section->vma;
1050 /* Copy the any flags from the file header into the symbol.
1051 FIXME: Why? */
1053 coff_symbol_type *c = coff_symbol_from (abfd, symbol);
1054 if (c != (coff_symbol_type *) NULL)
1055 native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
1059 native->u.syment.n_type = 0;
1060 if (symbol->flags & BSF_FILE)
1061 native->u.syment.n_sclass = C_FILE;
1062 else if (symbol->flags & BSF_LOCAL)
1063 native->u.syment.n_sclass = C_STAT;
1064 else if (symbol->flags & BSF_WEAK)
1065 native->u.syment.n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1066 else
1067 native->u.syment.n_sclass = C_EXT;
1069 ret = coff_write_symbol (abfd, symbol, native, written, string_size_p,
1070 debug_string_section_p, debug_string_size_p);
1071 if (isym != NULL)
1072 *isym = native->u.syment;
1073 return ret;
1076 /* Write a native symbol to a COFF file. */
1078 static bfd_boolean
1079 coff_write_native_symbol (bfd *abfd,
1080 coff_symbol_type *symbol,
1081 bfd_vma *written,
1082 bfd_size_type *string_size_p,
1083 asection **debug_string_section_p,
1084 bfd_size_type *debug_string_size_p)
1086 combined_entry_type *native = symbol->native;
1087 alent *lineno = symbol->lineno;
1088 struct bfd_link_info *link_info = coff_data (abfd)->link_info;
1090 if ((!link_info || link_info->strip_discarded)
1091 && !bfd_is_abs_section (symbol->symbol.section)
1092 && symbol->symbol.section->output_section == bfd_abs_section_ptr)
1094 symbol->symbol.name = "";
1095 return TRUE;
1098 /* If this symbol has an associated line number, we must store the
1099 symbol index in the line number field. We also tag the auxent to
1100 point to the right place in the lineno table. */
1101 if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
1103 unsigned int count = 0;
1105 lineno[count].u.offset = *written;
1106 if (native->u.syment.n_numaux)
1108 union internal_auxent *a = &((native + 1)->u.auxent);
1110 a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
1111 symbol->symbol.section->output_section->moving_line_filepos;
1114 /* Count and relocate all other linenumbers. */
1115 count++;
1116 while (lineno[count].line_number != 0)
1118 lineno[count].u.offset +=
1119 (symbol->symbol.section->output_section->vma
1120 + symbol->symbol.section->output_offset);
1121 count++;
1123 symbol->done_lineno = TRUE;
1125 if (! bfd_is_const_section (symbol->symbol.section->output_section))
1126 symbol->symbol.section->output_section->moving_line_filepos +=
1127 count * bfd_coff_linesz (abfd);
1130 return coff_write_symbol (abfd, &(symbol->symbol), native, written,
1131 string_size_p, debug_string_section_p,
1132 debug_string_size_p);
1135 static void
1136 null_error_handler (const char * fmt ATTRIBUTE_UNUSED, ...)
1140 /* Write out the COFF symbols. */
1142 bfd_boolean
1143 coff_write_symbols (bfd *abfd)
1145 bfd_size_type string_size;
1146 asection *debug_string_section;
1147 bfd_size_type debug_string_size;
1148 unsigned int i;
1149 unsigned int limit = bfd_get_symcount (abfd);
1150 bfd_vma written = 0;
1151 asymbol **p;
1153 string_size = 0;
1154 debug_string_section = NULL;
1155 debug_string_size = 0;
1157 /* If this target supports long section names, they must be put into
1158 the string table. This is supported by PE. This code must
1159 handle section names just as they are handled in
1160 coff_write_object_contents. */
1161 if (bfd_coff_long_section_names (abfd))
1163 asection *o;
1165 for (o = abfd->sections; o != NULL; o = o->next)
1167 size_t len;
1169 len = strlen (o->name);
1170 if (len > SCNNMLEN)
1171 string_size += len + 1;
1175 /* Seek to the right place. */
1176 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1177 return FALSE;
1179 /* Output all the symbols we have. */
1180 written = 0;
1181 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
1183 asymbol *symbol = *p;
1184 coff_symbol_type *c_symbol = coff_symbol_from (abfd, symbol);
1186 if (c_symbol == (coff_symbol_type *) NULL
1187 || c_symbol->native == (combined_entry_type *) NULL)
1189 if (!coff_write_alien_symbol (abfd, symbol, NULL, &written,
1190 &string_size, &debug_string_section,
1191 &debug_string_size))
1192 return FALSE;
1194 else
1196 if (coff_backend_info (abfd)->_bfd_coff_classify_symbol != NULL)
1198 bfd_error_handler_type current_error_handler;
1199 enum coff_symbol_classification sym_class;
1200 unsigned char *n_sclass;
1202 /* Suppress error reporting by bfd_coff_classify_symbol.
1203 Error messages can be generated when we are processing a local
1204 symbol which has no associated section and we do not have to
1205 worry about this, all we need to know is that it is local. */
1206 current_error_handler = bfd_set_error_handler (null_error_handler);
1207 sym_class = bfd_coff_classify_symbol (abfd,
1208 &c_symbol->native->u.syment);
1209 (void) bfd_set_error_handler (current_error_handler);
1211 n_sclass = &c_symbol->native->u.syment.n_sclass;
1213 /* If the symbol class has been changed (eg objcopy/ld script/etc)
1214 we cannot retain the existing sclass from the original symbol.
1215 Weak symbols only have one valid sclass, so just set it always.
1216 If it is not local class and should be, set it C_STAT.
1217 If it is global and not classified as global, or if it is
1218 weak (which is also classified as global), set it C_EXT. */
1220 if (symbol->flags & BSF_WEAK)
1221 *n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1222 else if (symbol->flags & BSF_LOCAL && sym_class != COFF_SYMBOL_LOCAL)
1223 *n_sclass = C_STAT;
1224 else if (symbol->flags & BSF_GLOBAL
1225 && (sym_class != COFF_SYMBOL_GLOBAL
1226 #ifdef COFF_WITH_PE
1227 || *n_sclass == C_NT_WEAK
1228 #endif
1229 || *n_sclass == C_WEAKEXT))
1230 c_symbol->native->u.syment.n_sclass = C_EXT;
1233 if (!coff_write_native_symbol (abfd, c_symbol, &written,
1234 &string_size, &debug_string_section,
1235 &debug_string_size))
1236 return FALSE;
1240 obj_raw_syment_count (abfd) = written;
1242 /* Now write out strings. */
1243 if (string_size != 0)
1245 unsigned int size = string_size + STRING_SIZE_SIZE;
1246 bfd_byte buffer[STRING_SIZE_SIZE];
1248 #if STRING_SIZE_SIZE == 4
1249 H_PUT_32 (abfd, size, buffer);
1250 #else
1251 #error Change H_PUT_32
1252 #endif
1253 if (bfd_bwrite ((void *) buffer, (bfd_size_type) sizeof (buffer), abfd)
1254 != sizeof (buffer))
1255 return FALSE;
1257 /* Handle long section names. This code must handle section
1258 names just as they are handled in coff_write_object_contents. */
1259 if (bfd_coff_long_section_names (abfd))
1261 asection *o;
1263 for (o = abfd->sections; o != NULL; o = o->next)
1265 size_t len;
1267 len = strlen (o->name);
1268 if (len > SCNNMLEN)
1270 if (bfd_bwrite (o->name, (bfd_size_type) (len + 1), abfd)
1271 != len + 1)
1272 return FALSE;
1277 for (p = abfd->outsymbols, i = 0;
1278 i < limit;
1279 i++, p++)
1281 asymbol *q = *p;
1282 size_t name_length = strlen (q->name);
1283 coff_symbol_type *c_symbol = coff_symbol_from (abfd, q);
1284 size_t maxlen;
1286 /* Figure out whether the symbol name should go in the string
1287 table. Symbol names that are short enough are stored
1288 directly in the syment structure. File names permit a
1289 different, longer, length in the syment structure. On
1290 XCOFF, some symbol names are stored in the .debug section
1291 rather than in the string table. */
1293 if (c_symbol == NULL
1294 || c_symbol->native == NULL)
1295 /* This is not a COFF symbol, so it certainly is not a
1296 file name, nor does it go in the .debug section. */
1297 maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
1299 else if (bfd_coff_symname_in_debug (abfd,
1300 &c_symbol->native->u.syment))
1301 /* This symbol name is in the XCOFF .debug section.
1302 Don't write it into the string table. */
1303 maxlen = name_length;
1305 else if (c_symbol->native->u.syment.n_sclass == C_FILE
1306 && c_symbol->native->u.syment.n_numaux > 0)
1308 if (bfd_coff_force_symnames_in_strings (abfd))
1310 if (bfd_bwrite (".file", (bfd_size_type) 6, abfd) != 6)
1311 return FALSE;
1313 maxlen = bfd_coff_filnmlen (abfd);
1315 else
1316 maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
1318 if (name_length > maxlen)
1320 if (bfd_bwrite ((void *) (q->name), (bfd_size_type) name_length + 1,
1321 abfd) != name_length + 1)
1322 return FALSE;
1326 else
1328 /* We would normally not write anything here, but we'll write
1329 out 4 so that any stupid coff reader which tries to read the
1330 string table even when there isn't one won't croak. */
1331 unsigned int size = STRING_SIZE_SIZE;
1332 bfd_byte buffer[STRING_SIZE_SIZE];
1334 #if STRING_SIZE_SIZE == 4
1335 H_PUT_32 (abfd, size, buffer);
1336 #else
1337 #error Change H_PUT_32
1338 #endif
1339 if (bfd_bwrite ((void *) buffer, (bfd_size_type) STRING_SIZE_SIZE, abfd)
1340 != STRING_SIZE_SIZE)
1341 return FALSE;
1344 /* Make sure the .debug section was created to be the correct size.
1345 We should create it ourselves on the fly, but we don't because
1346 BFD won't let us write to any section until we know how large all
1347 the sections are. We could still do it by making another pass
1348 over the symbols. FIXME. */
1349 BFD_ASSERT (debug_string_size == 0
1350 || (debug_string_section != (asection *) NULL
1351 && (BFD_ALIGN (debug_string_size,
1352 1 << debug_string_section->alignment_power)
1353 == debug_string_section->size)));
1355 return TRUE;
1358 bfd_boolean
1359 coff_write_linenumbers (bfd *abfd)
1361 asection *s;
1362 bfd_size_type linesz;
1363 void * buff;
1365 linesz = bfd_coff_linesz (abfd);
1366 buff = bfd_alloc (abfd, linesz);
1367 if (!buff)
1368 return FALSE;
1369 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1371 if (s->lineno_count)
1373 asymbol **q = abfd->outsymbols;
1374 if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
1375 return FALSE;
1376 /* Find all the linenumbers in this section. */
1377 while (*q)
1379 asymbol *p = *q;
1380 if (p->section->output_section == s)
1382 alent *l =
1383 BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
1384 (bfd_asymbol_bfd (p), p));
1385 if (l)
1387 /* Found a linenumber entry, output. */
1388 struct internal_lineno out;
1389 memset ((void *) & out, 0, sizeof (out));
1390 out.l_lnno = 0;
1391 out.l_addr.l_symndx = l->u.offset;
1392 bfd_coff_swap_lineno_out (abfd, &out, buff);
1393 if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd)
1394 != linesz)
1395 return FALSE;
1396 l++;
1397 while (l->line_number)
1399 out.l_lnno = l->line_number;
1400 out.l_addr.l_symndx = l->u.offset;
1401 bfd_coff_swap_lineno_out (abfd, &out, buff);
1402 if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd)
1403 != linesz)
1404 return FALSE;
1405 l++;
1409 q++;
1413 bfd_release (abfd, buff);
1414 return TRUE;
1417 alent *
1418 coff_get_lineno (bfd *ignore_abfd ATTRIBUTE_UNUSED, asymbol *symbol)
1420 return coffsymbol (symbol)->lineno;
1423 /* This function transforms the offsets into the symbol table into
1424 pointers to syments. */
1426 static void
1427 coff_pointerize_aux (bfd *abfd,
1428 combined_entry_type *table_base,
1429 combined_entry_type *symbol,
1430 unsigned int indaux,
1431 combined_entry_type *auxent)
1433 unsigned int type = symbol->u.syment.n_type;
1434 unsigned int n_sclass = symbol->u.syment.n_sclass;
1436 if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1438 if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1439 (abfd, table_base, symbol, indaux, auxent))
1440 return;
1443 /* Don't bother if this is a file or a section. */
1444 if (n_sclass == C_STAT && type == T_NULL)
1445 return;
1446 if (n_sclass == C_FILE)
1447 return;
1449 /* Otherwise patch up. */
1450 #define N_TMASK coff_data (abfd)->local_n_tmask
1451 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1453 if ((ISFCN (type) || ISTAG (n_sclass) || n_sclass == C_BLOCK
1454 || n_sclass == C_FCN)
1455 && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l > 0)
1457 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
1458 table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
1459 auxent->fix_end = 1;
1461 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1462 generate one, so we must be careful to ignore it. */
1463 if (auxent->u.auxent.x_sym.x_tagndx.l > 0)
1465 auxent->u.auxent.x_sym.x_tagndx.p =
1466 table_base + auxent->u.auxent.x_sym.x_tagndx.l;
1467 auxent->fix_tag = 1;
1471 /* Allocate space for the ".debug" section, and read it.
1472 We did not read the debug section until now, because
1473 we didn't want to go to the trouble until someone needed it. */
1475 static char *
1476 build_debug_section (bfd *abfd)
1478 char *debug_section;
1479 file_ptr position;
1480 bfd_size_type sec_size;
1482 asection *sect = bfd_get_section_by_name (abfd, ".debug");
1484 if (!sect)
1486 bfd_set_error (bfd_error_no_debug_section);
1487 return NULL;
1490 sec_size = sect->size;
1491 debug_section = (char *) bfd_alloc (abfd, sec_size);
1492 if (debug_section == NULL)
1493 return NULL;
1495 /* Seek to the beginning of the `.debug' section and read it.
1496 Save the current position first; it is needed by our caller.
1497 Then read debug section and reset the file pointer. */
1499 position = bfd_tell (abfd);
1500 if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0
1501 || bfd_bread (debug_section, sec_size, abfd) != sec_size
1502 || bfd_seek (abfd, position, SEEK_SET) != 0)
1503 return NULL;
1504 return debug_section;
1507 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1508 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1509 be \0-terminated. */
1511 static char *
1512 copy_name (bfd *abfd, char *name, size_t maxlen)
1514 size_t len;
1515 char *newname;
1517 for (len = 0; len < maxlen; ++len)
1518 if (name[len] == '\0')
1519 break;
1521 if ((newname = (char *) bfd_alloc (abfd, (bfd_size_type) len + 1)) == NULL)
1522 return NULL;
1524 strncpy (newname, name, len);
1525 newname[len] = '\0';
1526 return newname;
1529 /* Read in the external symbols. */
1531 bfd_boolean
1532 _bfd_coff_get_external_symbols (bfd *abfd)
1534 bfd_size_type symesz;
1535 bfd_size_type size;
1536 void * syms;
1538 if (obj_coff_external_syms (abfd) != NULL)
1539 return TRUE;
1541 symesz = bfd_coff_symesz (abfd);
1543 size = obj_raw_syment_count (abfd) * symesz;
1544 if (size == 0)
1545 return TRUE;
1547 syms = bfd_malloc (size);
1548 if (syms == NULL)
1549 return FALSE;
1551 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
1552 || bfd_bread (syms, size, abfd) != size)
1554 if (syms != NULL)
1555 free (syms);
1556 return FALSE;
1559 obj_coff_external_syms (abfd) = syms;
1561 return TRUE;
1564 /* Read in the external strings. The strings are not loaded until
1565 they are needed. This is because we have no simple way of
1566 detecting a missing string table in an archive. */
1568 const char *
1569 _bfd_coff_read_string_table (bfd *abfd)
1571 char extstrsize[STRING_SIZE_SIZE];
1572 bfd_size_type strsize;
1573 char *strings;
1574 file_ptr pos;
1576 if (obj_coff_strings (abfd) != NULL)
1577 return obj_coff_strings (abfd);
1579 if (obj_sym_filepos (abfd) == 0)
1581 bfd_set_error (bfd_error_no_symbols);
1582 return NULL;
1585 pos = obj_sym_filepos (abfd);
1586 pos += obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd);
1587 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
1588 return NULL;
1590 if (bfd_bread (extstrsize, (bfd_size_type) sizeof extstrsize, abfd)
1591 != sizeof extstrsize)
1593 if (bfd_get_error () != bfd_error_file_truncated)
1594 return NULL;
1596 /* There is no string table. */
1597 strsize = STRING_SIZE_SIZE;
1599 else
1601 #if STRING_SIZE_SIZE == 4
1602 strsize = H_GET_32 (abfd, extstrsize);
1603 #else
1604 #error Change H_GET_32
1605 #endif
1608 if (strsize < STRING_SIZE_SIZE)
1610 (*_bfd_error_handler)
1611 (_("%B: bad string table size %lu"), abfd, (unsigned long) strsize);
1612 bfd_set_error (bfd_error_bad_value);
1613 return NULL;
1616 strings = (char *) bfd_malloc (strsize);
1617 if (strings == NULL)
1618 return NULL;
1620 if (bfd_bread (strings + STRING_SIZE_SIZE, strsize - STRING_SIZE_SIZE, abfd)
1621 != strsize - STRING_SIZE_SIZE)
1623 free (strings);
1624 return NULL;
1627 obj_coff_strings (abfd) = strings;
1629 return strings;
1632 /* Free up the external symbols and strings read from a COFF file. */
1634 bfd_boolean
1635 _bfd_coff_free_symbols (bfd *abfd)
1637 if (obj_coff_external_syms (abfd) != NULL
1638 && ! obj_coff_keep_syms (abfd))
1640 free (obj_coff_external_syms (abfd));
1641 obj_coff_external_syms (abfd) = NULL;
1643 if (obj_coff_strings (abfd) != NULL
1644 && ! obj_coff_keep_strings (abfd))
1646 free (obj_coff_strings (abfd));
1647 obj_coff_strings (abfd) = NULL;
1649 return TRUE;
1652 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1653 knit the symbol names into a normalized form. By normalized here I
1654 mean that all symbols have an n_offset pointer that points to a null-
1655 terminated string. */
1657 combined_entry_type *
1658 coff_get_normalized_symtab (bfd *abfd)
1660 combined_entry_type *internal;
1661 combined_entry_type *internal_ptr;
1662 combined_entry_type *symbol_ptr;
1663 combined_entry_type *internal_end;
1664 size_t symesz;
1665 char *raw_src;
1666 char *raw_end;
1667 const char *string_table = NULL;
1668 char *debug_section = NULL;
1669 bfd_size_type size;
1671 if (obj_raw_syments (abfd) != NULL)
1672 return obj_raw_syments (abfd);
1674 size = obj_raw_syment_count (abfd) * sizeof (combined_entry_type);
1675 internal = (combined_entry_type *) bfd_zalloc (abfd, size);
1676 if (internal == NULL && size != 0)
1677 return NULL;
1678 internal_end = internal + obj_raw_syment_count (abfd);
1680 if (! _bfd_coff_get_external_symbols (abfd))
1681 return NULL;
1683 raw_src = (char *) obj_coff_external_syms (abfd);
1685 /* Mark the end of the symbols. */
1686 symesz = bfd_coff_symesz (abfd);
1687 raw_end = (char *) raw_src + obj_raw_syment_count (abfd) * symesz;
1689 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1690 probably possible. If one shows up, it will probably kill us. */
1692 /* Swap all the raw entries. */
1693 for (internal_ptr = internal;
1694 raw_src < raw_end;
1695 raw_src += symesz, internal_ptr++)
1698 unsigned int i;
1699 bfd_coff_swap_sym_in (abfd, (void *) raw_src,
1700 (void *) & internal_ptr->u.syment);
1701 symbol_ptr = internal_ptr;
1703 for (i = 0;
1704 i < symbol_ptr->u.syment.n_numaux;
1705 i++)
1707 internal_ptr++;
1708 raw_src += symesz;
1709 bfd_coff_swap_aux_in (abfd, (void *) raw_src,
1710 symbol_ptr->u.syment.n_type,
1711 symbol_ptr->u.syment.n_sclass,
1712 (int) i, symbol_ptr->u.syment.n_numaux,
1713 &(internal_ptr->u.auxent));
1714 coff_pointerize_aux (abfd, internal, symbol_ptr, i,
1715 internal_ptr);
1719 /* Free the raw symbols, but not the strings (if we have them). */
1720 obj_coff_keep_strings (abfd) = TRUE;
1721 if (! _bfd_coff_free_symbols (abfd))
1722 return NULL;
1724 for (internal_ptr = internal; internal_ptr < internal_end;
1725 internal_ptr++)
1727 if (internal_ptr->u.syment.n_sclass == C_FILE
1728 && internal_ptr->u.syment.n_numaux > 0)
1730 /* Make a file symbol point to the name in the auxent, since
1731 the text ".file" is redundant. */
1732 if ((internal_ptr + 1)->u.auxent.x_file.x_n.x_zeroes == 0)
1734 /* The filename is a long one, point into the string table. */
1735 if (string_table == NULL)
1737 string_table = _bfd_coff_read_string_table (abfd);
1738 if (string_table == NULL)
1739 return NULL;
1742 internal_ptr->u.syment._n._n_n._n_offset =
1743 ((bfd_hostptr_t)
1744 (string_table
1745 + (internal_ptr + 1)->u.auxent.x_file.x_n.x_offset));
1747 else
1749 /* Ordinary short filename, put into memory anyway. The
1750 Microsoft PE tools sometimes store a filename in
1751 multiple AUX entries. */
1752 if (internal_ptr->u.syment.n_numaux > 1
1753 && coff_data (abfd)->pe)
1754 internal_ptr->u.syment._n._n_n._n_offset =
1755 ((bfd_hostptr_t)
1756 copy_name (abfd,
1757 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1758 internal_ptr->u.syment.n_numaux * symesz));
1759 else
1760 internal_ptr->u.syment._n._n_n._n_offset =
1761 ((bfd_hostptr_t)
1762 copy_name (abfd,
1763 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1764 (size_t) bfd_coff_filnmlen (abfd)));
1767 else
1769 if (internal_ptr->u.syment._n._n_n._n_zeroes != 0)
1771 /* This is a "short" name. Make it long. */
1772 size_t i;
1773 char *newstring;
1775 /* Find the length of this string without walking into memory
1776 that isn't ours. */
1777 for (i = 0; i < 8; ++i)
1778 if (internal_ptr->u.syment._n._n_name[i] == '\0')
1779 break;
1781 newstring = (char *) bfd_zalloc (abfd, (bfd_size_type) (i + 1));
1782 if (newstring == NULL)
1783 return NULL;
1784 strncpy (newstring, internal_ptr->u.syment._n._n_name, i);
1785 internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t) newstring;
1786 internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1788 else if (internal_ptr->u.syment._n._n_n._n_offset == 0)
1789 internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t) "";
1790 else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment))
1792 /* Long name already. Point symbol at the string in the
1793 table. */
1794 if (string_table == NULL)
1796 string_table = _bfd_coff_read_string_table (abfd);
1797 if (string_table == NULL)
1798 return NULL;
1800 internal_ptr->u.syment._n._n_n._n_offset =
1801 ((bfd_hostptr_t)
1802 (string_table
1803 + internal_ptr->u.syment._n._n_n._n_offset));
1805 else
1807 /* Long name in debug section. Very similar. */
1808 if (debug_section == NULL)
1809 debug_section = build_debug_section (abfd);
1810 internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t)
1811 (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
1814 internal_ptr += internal_ptr->u.syment.n_numaux;
1817 obj_raw_syments (abfd) = internal;
1818 BFD_ASSERT (obj_raw_syment_count (abfd)
1819 == (unsigned int) (internal_ptr - internal));
1821 return internal;
1824 long
1825 coff_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
1827 if (bfd_get_format (abfd) != bfd_object)
1829 bfd_set_error (bfd_error_invalid_operation);
1830 return -1;
1832 return (asect->reloc_count + 1) * sizeof (arelent *);
1835 asymbol *
1836 coff_make_empty_symbol (bfd *abfd)
1838 bfd_size_type amt = sizeof (coff_symbol_type);
1839 coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_zalloc (abfd, amt);
1841 if (new_symbol == NULL)
1842 return NULL;
1843 new_symbol->symbol.section = 0;
1844 new_symbol->native = 0;
1845 new_symbol->lineno = NULL;
1846 new_symbol->done_lineno = FALSE;
1847 new_symbol->symbol.the_bfd = abfd;
1849 return & new_symbol->symbol;
1852 /* Make a debugging symbol. */
1854 asymbol *
1855 coff_bfd_make_debug_symbol (bfd *abfd,
1856 void * ptr ATTRIBUTE_UNUSED,
1857 unsigned long sz ATTRIBUTE_UNUSED)
1859 bfd_size_type amt = sizeof (coff_symbol_type);
1860 coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_alloc (abfd, amt);
1862 if (new_symbol == NULL)
1863 return NULL;
1864 /* @@ The 10 is a guess at a plausible maximum number of aux entries
1865 (but shouldn't be a constant). */
1866 amt = sizeof (combined_entry_type) * 10;
1867 new_symbol->native = (combined_entry_type *) bfd_zalloc (abfd, amt);
1868 if (!new_symbol->native)
1869 return NULL;
1870 new_symbol->symbol.section = bfd_abs_section_ptr;
1871 new_symbol->symbol.flags = BSF_DEBUGGING;
1872 new_symbol->lineno = NULL;
1873 new_symbol->done_lineno = FALSE;
1874 new_symbol->symbol.the_bfd = abfd;
1876 return & new_symbol->symbol;
1879 void
1880 coff_get_symbol_info (bfd *abfd, asymbol *symbol, symbol_info *ret)
1882 bfd_symbol_info (symbol, ret);
1884 if (coffsymbol (symbol)->native != NULL
1885 && coffsymbol (symbol)->native->fix_value)
1886 ret->value = coffsymbol (symbol)->native->u.syment.n_value -
1887 (bfd_hostptr_t) obj_raw_syments (abfd);
1890 /* Return the COFF syment for a symbol. */
1892 bfd_boolean
1893 bfd_coff_get_syment (bfd *abfd,
1894 asymbol *symbol,
1895 struct internal_syment *psyment)
1897 coff_symbol_type *csym;
1899 csym = coff_symbol_from (abfd, symbol);
1900 if (csym == NULL || csym->native == NULL)
1902 bfd_set_error (bfd_error_invalid_operation);
1903 return FALSE;
1906 *psyment = csym->native->u.syment;
1908 if (csym->native->fix_value)
1909 psyment->n_value = psyment->n_value -
1910 (bfd_hostptr_t) obj_raw_syments (abfd);
1912 /* FIXME: We should handle fix_line here. */
1914 return TRUE;
1917 /* Return the COFF auxent for a symbol. */
1919 bfd_boolean
1920 bfd_coff_get_auxent (bfd *abfd,
1921 asymbol *symbol,
1922 int indx,
1923 union internal_auxent *pauxent)
1925 coff_symbol_type *csym;
1926 combined_entry_type *ent;
1928 csym = coff_symbol_from (abfd, symbol);
1930 if (csym == NULL
1931 || csym->native == NULL
1932 || indx >= csym->native->u.syment.n_numaux)
1934 bfd_set_error (bfd_error_invalid_operation);
1935 return FALSE;
1938 ent = csym->native + indx + 1;
1940 *pauxent = ent->u.auxent;
1942 if (ent->fix_tag)
1943 pauxent->x_sym.x_tagndx.l =
1944 ((combined_entry_type *) pauxent->x_sym.x_tagndx.p
1945 - obj_raw_syments (abfd));
1947 if (ent->fix_end)
1948 pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l =
1949 ((combined_entry_type *) pauxent->x_sym.x_fcnary.x_fcn.x_endndx.p
1950 - obj_raw_syments (abfd));
1952 if (ent->fix_scnlen)
1953 pauxent->x_csect.x_scnlen.l =
1954 ((combined_entry_type *) pauxent->x_csect.x_scnlen.p
1955 - obj_raw_syments (abfd));
1957 return TRUE;
1960 /* Print out information about COFF symbol. */
1962 void
1963 coff_print_symbol (bfd *abfd,
1964 void * filep,
1965 asymbol *symbol,
1966 bfd_print_symbol_type how)
1968 FILE * file = (FILE *) filep;
1970 switch (how)
1972 case bfd_print_symbol_name:
1973 fprintf (file, "%s", symbol->name);
1974 break;
1976 case bfd_print_symbol_more:
1977 fprintf (file, "coff %s %s",
1978 coffsymbol (symbol)->native ? "n" : "g",
1979 coffsymbol (symbol)->lineno ? "l" : " ");
1980 break;
1982 case bfd_print_symbol_all:
1983 if (coffsymbol (symbol)->native)
1985 bfd_vma val;
1986 unsigned int aux;
1987 combined_entry_type *combined = coffsymbol (symbol)->native;
1988 combined_entry_type *root = obj_raw_syments (abfd);
1989 struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
1991 fprintf (file, "[%3ld]", (long) (combined - root));
1993 if (! combined->fix_value)
1994 val = (bfd_vma) combined->u.syment.n_value;
1995 else
1996 val = combined->u.syment.n_value - (bfd_hostptr_t) root;
1998 fprintf (file, "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x",
1999 combined->u.syment.n_scnum,
2000 combined->u.syment.n_flags,
2001 combined->u.syment.n_type,
2002 combined->u.syment.n_sclass,
2003 combined->u.syment.n_numaux);
2004 bfd_fprintf_vma (abfd, file, val);
2005 fprintf (file, " %s", symbol->name);
2007 for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
2009 combined_entry_type *auxp = combined + aux + 1;
2010 long tagndx;
2012 if (auxp->fix_tag)
2013 tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
2014 else
2015 tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
2017 fprintf (file, "\n");
2019 if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux))
2020 continue;
2022 switch (combined->u.syment.n_sclass)
2024 case C_FILE:
2025 fprintf (file, "File ");
2026 break;
2028 case C_STAT:
2029 if (combined->u.syment.n_type == T_NULL)
2030 /* Probably a section symbol ? */
2032 fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
2033 (unsigned long) auxp->u.auxent.x_scn.x_scnlen,
2034 auxp->u.auxent.x_scn.x_nreloc,
2035 auxp->u.auxent.x_scn.x_nlinno);
2036 if (auxp->u.auxent.x_scn.x_checksum != 0
2037 || auxp->u.auxent.x_scn.x_associated != 0
2038 || auxp->u.auxent.x_scn.x_comdat != 0)
2039 fprintf (file, " checksum 0x%lx assoc %d comdat %d",
2040 auxp->u.auxent.x_scn.x_checksum,
2041 auxp->u.auxent.x_scn.x_associated,
2042 auxp->u.auxent.x_scn.x_comdat);
2043 break;
2045 /* Otherwise fall through. */
2046 case C_EXT:
2047 case C_AIX_WEAKEXT:
2048 if (ISFCN (combined->u.syment.n_type))
2050 long next, llnos;
2052 if (auxp->fix_end)
2053 next = (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2054 - root);
2055 else
2056 next = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
2057 llnos = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_lnnoptr;
2058 fprintf (file,
2059 "AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld",
2060 tagndx,
2061 (unsigned long) auxp->u.auxent.x_sym.x_misc.x_fsize,
2062 llnos, next);
2063 break;
2065 /* Otherwise fall through. */
2066 default:
2067 fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
2068 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
2069 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
2070 tagndx);
2071 if (auxp->fix_end)
2072 fprintf (file, " endndx %ld",
2073 ((long)
2074 (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2075 - root)));
2076 break;
2080 if (l)
2082 fprintf (file, "\n%s :", l->u.sym->name);
2083 l++;
2084 while (l->line_number)
2086 fprintf (file, "\n%4d : ", l->line_number);
2087 bfd_fprintf_vma (abfd, file, l->u.offset + symbol->section->vma);
2088 l++;
2092 else
2094 bfd_print_symbol_vandf (abfd, (void *) file, symbol);
2095 fprintf (file, " %-5s %s %s %s",
2096 symbol->section->name,
2097 coffsymbol (symbol)->native ? "n" : "g",
2098 coffsymbol (symbol)->lineno ? "l" : " ",
2099 symbol->name);
2104 /* Return whether a symbol name implies a local symbol. In COFF,
2105 local symbols generally start with ``.L''. Most targets use this
2106 function for the is_local_label_name entry point, but some may
2107 override it. */
2109 bfd_boolean
2110 _bfd_coff_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
2111 const char *name)
2113 return name[0] == '.' && name[1] == 'L';
2116 /* Provided a BFD, a section and an offset (in bytes, not octets) into the
2117 section, calculate and return the name of the source file and the line
2118 nearest to the wanted location. */
2120 bfd_boolean
2121 coff_find_nearest_line_with_names (bfd *abfd,
2122 const struct dwarf_debug_section *debug_sections,
2123 asection *section,
2124 asymbol **symbols,
2125 bfd_vma offset,
2126 const char **filename_ptr,
2127 const char **functionname_ptr,
2128 unsigned int *line_ptr)
2130 bfd_boolean found;
2131 unsigned int i;
2132 unsigned int line_base;
2133 coff_data_type *cof = coff_data (abfd);
2134 /* Run through the raw syments if available. */
2135 combined_entry_type *p;
2136 combined_entry_type *pend;
2137 alent *l;
2138 struct coff_section_tdata *sec_data;
2139 bfd_size_type amt;
2141 /* Before looking through the symbol table, try to use a .stab
2142 section to find the information. */
2143 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
2144 &found, filename_ptr,
2145 functionname_ptr, line_ptr,
2146 &coff_data(abfd)->line_info))
2147 return FALSE;
2149 if (found)
2150 return TRUE;
2152 /* Also try examining DWARF2 debugging information. */
2153 if (_bfd_dwarf2_find_nearest_line (abfd, debug_sections,
2154 section, symbols, offset,
2155 filename_ptr, functionname_ptr,
2156 line_ptr, 0,
2157 &coff_data(abfd)->dwarf2_find_line_info))
2158 return TRUE;
2160 *filename_ptr = 0;
2161 *functionname_ptr = 0;
2162 *line_ptr = 0;
2164 /* Don't try and find line numbers in a non coff file. */
2165 if (!bfd_family_coff (abfd))
2166 return FALSE;
2168 if (cof == NULL)
2169 return FALSE;
2171 /* Find the first C_FILE symbol. */
2172 p = cof->raw_syments;
2173 if (!p)
2174 return FALSE;
2176 pend = p + cof->raw_syment_count;
2177 while (p < pend)
2179 if (p->u.syment.n_sclass == C_FILE)
2180 break;
2181 p += 1 + p->u.syment.n_numaux;
2184 if (p < pend)
2186 bfd_vma sec_vma;
2187 bfd_vma maxdiff;
2189 /* Look through the C_FILE symbols to find the best one. */
2190 sec_vma = bfd_get_section_vma (abfd, section);
2191 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2192 maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
2193 while (1)
2195 bfd_vma file_addr;
2196 combined_entry_type *p2;
2198 for (p2 = p + 1 + p->u.syment.n_numaux;
2199 p2 < pend;
2200 p2 += 1 + p2->u.syment.n_numaux)
2202 if (p2->u.syment.n_scnum > 0
2203 && (section
2204 == coff_section_from_bfd_index (abfd,
2205 p2->u.syment.n_scnum)))
2206 break;
2207 if (p2->u.syment.n_sclass == C_FILE)
2209 p2 = pend;
2210 break;
2214 file_addr = (bfd_vma) p2->u.syment.n_value;
2215 /* PR 11512: Include the section address of the function name symbol. */
2216 if (p2->u.syment.n_scnum > 0)
2217 file_addr += coff_section_from_bfd_index (abfd,
2218 p2->u.syment.n_scnum)->vma;
2219 /* We use <= MAXDIFF here so that if we get a zero length
2220 file, we actually use the next file entry. */
2221 if (p2 < pend
2222 && offset + sec_vma >= file_addr
2223 && offset + sec_vma - file_addr <= maxdiff)
2225 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2226 maxdiff = offset + sec_vma - p2->u.syment.n_value;
2229 /* Avoid endless loops on erroneous files by ensuring that
2230 we always move forward in the file. */
2231 if (p >= cof->raw_syments + p->u.syment.n_value)
2232 break;
2234 p = cof->raw_syments + p->u.syment.n_value;
2235 if (p > pend || p->u.syment.n_sclass != C_FILE)
2236 break;
2240 /* Now wander though the raw linenumbers of the section. */
2241 /* If we have been called on this section before, and the offset we
2242 want is further down then we can prime the lookup loop. */
2243 sec_data = coff_section_data (abfd, section);
2244 if (sec_data != NULL
2245 && sec_data->i > 0
2246 && offset >= sec_data->offset)
2248 i = sec_data->i;
2249 *functionname_ptr = sec_data->function;
2250 line_base = sec_data->line_base;
2252 else
2254 i = 0;
2255 line_base = 0;
2258 if (section->lineno != NULL)
2260 bfd_vma last_value = 0;
2262 l = &section->lineno[i];
2264 for (; i < section->lineno_count; i++)
2266 if (l->line_number == 0)
2268 /* Get the symbol this line number points at. */
2269 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
2270 if (coff->symbol.value > offset)
2271 break;
2272 *functionname_ptr = coff->symbol.name;
2273 last_value = coff->symbol.value;
2274 if (coff->native)
2276 combined_entry_type *s = coff->native;
2277 s = s + 1 + s->u.syment.n_numaux;
2279 /* In XCOFF a debugging symbol can follow the
2280 function symbol. */
2281 if (s->u.syment.n_scnum == N_DEBUG)
2282 s = s + 1 + s->u.syment.n_numaux;
2284 /* S should now point to the .bf of the function. */
2285 if (s->u.syment.n_numaux)
2287 /* The linenumber is stored in the auxent. */
2288 union internal_auxent *a = &((s + 1)->u.auxent);
2289 line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
2290 *line_ptr = line_base;
2294 else
2296 if (l->u.offset > offset)
2297 break;
2298 *line_ptr = l->line_number + line_base - 1;
2300 l++;
2303 /* If we fell off the end of the loop, then assume that this
2304 symbol has no line number info. Otherwise, symbols with no
2305 line number info get reported with the line number of the
2306 last line of the last symbol which does have line number
2307 info. We use 0x100 as a slop to account for cases where the
2308 last line has executable code. */
2309 if (i >= section->lineno_count
2310 && last_value != 0
2311 && offset - last_value > 0x100)
2313 *functionname_ptr = NULL;
2314 *line_ptr = 0;
2318 /* Cache the results for the next call. */
2319 if (sec_data == NULL && section->owner == abfd)
2321 amt = sizeof (struct coff_section_tdata);
2322 section->used_by_bfd = bfd_zalloc (abfd, amt);
2323 sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2325 if (sec_data != NULL)
2327 sec_data->offset = offset;
2328 sec_data->i = i - 1;
2329 sec_data->function = *functionname_ptr;
2330 sec_data->line_base = line_base;
2333 return TRUE;
2336 bfd_boolean
2337 coff_find_nearest_line (bfd *abfd,
2338 asection *section,
2339 asymbol **symbols,
2340 bfd_vma offset,
2341 const char **filename_ptr,
2342 const char **functionname_ptr,
2343 unsigned int *line_ptr)
2345 return coff_find_nearest_line_with_names (abfd, dwarf_debug_sections,
2346 section, symbols, offset,
2347 filename_ptr, functionname_ptr,
2348 line_ptr);
2351 bfd_boolean
2352 coff_find_inliner_info (bfd *abfd,
2353 const char **filename_ptr,
2354 const char **functionname_ptr,
2355 unsigned int *line_ptr)
2357 bfd_boolean found;
2359 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
2360 functionname_ptr, line_ptr,
2361 &coff_data(abfd)->dwarf2_find_line_info);
2362 return (found);
2366 coff_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
2368 size_t size;
2370 if (!info->relocatable)
2371 size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
2372 else
2373 size = bfd_coff_filhsz (abfd);
2375 size += abfd->section_count * bfd_coff_scnhsz (abfd);
2376 return size;
2379 /* Change the class of a coff symbol held by BFD. */
2381 bfd_boolean
2382 bfd_coff_set_symbol_class (bfd * abfd,
2383 asymbol * symbol,
2384 unsigned int symbol_class)
2386 coff_symbol_type * csym;
2388 csym = coff_symbol_from (abfd, symbol);
2389 if (csym == NULL)
2391 bfd_set_error (bfd_error_invalid_operation);
2392 return FALSE;
2394 else if (csym->native == NULL)
2396 /* This is an alien symbol which no native coff backend data.
2397 We cheat here by creating a fake native entry for it and
2398 then filling in the class. This code is based on that in
2399 coff_write_alien_symbol(). */
2401 combined_entry_type * native;
2402 bfd_size_type amt = sizeof (* native);
2404 native = (combined_entry_type *) bfd_zalloc (abfd, amt);
2405 if (native == NULL)
2406 return FALSE;
2408 native->u.syment.n_type = T_NULL;
2409 native->u.syment.n_sclass = symbol_class;
2411 if (bfd_is_und_section (symbol->section))
2413 native->u.syment.n_scnum = N_UNDEF;
2414 native->u.syment.n_value = symbol->value;
2416 else if (bfd_is_com_section (symbol->section))
2418 native->u.syment.n_scnum = N_UNDEF;
2419 native->u.syment.n_value = symbol->value;
2421 else
2423 native->u.syment.n_scnum =
2424 symbol->section->output_section->target_index;
2425 native->u.syment.n_value = (symbol->value
2426 + symbol->section->output_offset);
2427 if (! obj_pe (abfd))
2428 native->u.syment.n_value += symbol->section->output_section->vma;
2430 /* Copy the any flags from the file header into the symbol.
2431 FIXME: Why? */
2432 native->u.syment.n_flags = bfd_asymbol_bfd (& csym->symbol)->flags;
2435 csym->native = native;
2437 else
2438 csym->native->u.syment.n_sclass = symbol_class;
2440 return TRUE;
2443 struct coff_comdat_info *
2444 bfd_coff_get_comdat_section (bfd *abfd, struct bfd_section *sec)
2446 if (bfd_get_flavour (abfd) == bfd_target_coff_flavour
2447 && coff_section_data (abfd, sec) != NULL)
2448 return coff_section_data (abfd, sec)->comdat;
2449 else
2450 return NULL;
2453 bfd_boolean
2454 _bfd_coff_section_already_linked (bfd *abfd,
2455 asection *sec,
2456 struct bfd_link_info *info)
2458 flagword flags;
2459 const char *name, *key;
2460 struct bfd_section_already_linked *l;
2461 struct bfd_section_already_linked_hash_entry *already_linked_list;
2462 struct coff_comdat_info *s_comdat;
2464 flags = sec->flags;
2465 if ((flags & SEC_LINK_ONCE) == 0)
2466 return FALSE;
2468 /* The COFF backend linker doesn't support group sections. */
2469 if ((flags & SEC_GROUP) != 0)
2470 return FALSE;
2472 name = bfd_get_section_name (abfd, sec);
2473 s_comdat = bfd_coff_get_comdat_section (abfd, sec);
2475 if (s_comdat != NULL)
2476 key = s_comdat->name;
2477 else
2479 if (CONST_STRNEQ (name, ".gnu.linkonce.")
2480 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
2481 key++;
2482 else
2483 /* FIXME: gcc as of 2011-09 emits sections like .text$<key>,
2484 .xdata$<key> and .pdata$<key> only the first of which has a
2485 comdat key. Should these all match the LTO IR key? */
2486 key = name;
2489 already_linked_list = bfd_section_already_linked_table_lookup (key);
2491 for (l = already_linked_list->entry; l != NULL; l = l->next)
2493 struct coff_comdat_info *l_comdat;
2495 l_comdat = bfd_coff_get_comdat_section (l->sec->owner, l->sec);
2497 /* The section names must match, and both sections must be
2498 comdat and have the same comdat name, or both sections must
2499 be non-comdat. LTO IR plugin sections are an exception. They
2500 are always named .gnu.linkonce.t.<key> (<key> is some string)
2501 and match any comdat section with comdat name of <key>, and
2502 any linkonce section with the same suffix, ie.
2503 .gnu.linkonce.*.<key>. */
2504 if (((s_comdat != NULL) == (l_comdat != NULL)
2505 && strcmp (name, l->sec->name) == 0)
2506 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
2508 /* The section has already been linked. See if we should
2509 issue a warning. */
2510 return _bfd_handle_already_linked (sec, l, info);
2514 /* This is the first section with this name. Record it. */
2515 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
2516 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
2517 return FALSE;