merge from gcc
[binutils.git] / bfd / coffgen.c
blobd7909cb0e7d2e750793446f47da201432bb21223
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, 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 = bfd_alloc (abfd, (bfd_size_type) strlen (strings) + 1);
94 if (name == NULL)
95 return FALSE;
96 strcpy (name, strings);
100 if (name == NULL)
102 /* Assorted wastage to null-terminate the name, thanks AT&T! */
103 name = bfd_alloc (abfd, (bfd_size_type) sizeof (hdr->s_name) + 1);
104 if (name == NULL)
105 return FALSE;
106 strncpy (name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
107 name[sizeof (hdr->s_name)] = 0;
110 return_section = bfd_make_section_anyway (abfd, name);
111 if (return_section == NULL)
112 return FALSE;
114 return_section->vma = hdr->s_vaddr;
115 return_section->lma = hdr->s_paddr;
116 return_section->size = hdr->s_size;
117 return_section->filepos = hdr->s_scnptr;
118 return_section->rel_filepos = hdr->s_relptr;
119 return_section->reloc_count = hdr->s_nreloc;
121 bfd_coff_set_alignment_hook (abfd, return_section, hdr);
123 return_section->line_filepos = hdr->s_lnnoptr;
125 return_section->lineno_count = hdr->s_nlnno;
126 return_section->userdata = NULL;
127 return_section->next = NULL;
128 return_section->target_index = target_index;
130 if (! bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name, return_section,
131 & flags))
132 result = FALSE;
134 return_section->flags = flags;
136 /* At least on i386-coff, the line number count for a shared library
137 section must be ignored. */
138 if ((return_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
139 return_section->lineno_count = 0;
141 if (hdr->s_nreloc != 0)
142 return_section->flags |= SEC_RELOC;
143 /* FIXME: should this check 'hdr->s_size > 0'. */
144 if (hdr->s_scnptr != 0)
145 return_section->flags |= SEC_HAS_CONTENTS;
147 return result;
150 /* Read in a COFF object and make it into a BFD. This is used by
151 ECOFF as well. */
153 static const bfd_target *
154 coff_real_object_p (bfd *abfd,
155 unsigned nscns,
156 struct internal_filehdr *internal_f,
157 struct internal_aouthdr *internal_a)
159 flagword oflags = abfd->flags;
160 bfd_vma ostart = bfd_get_start_address (abfd);
161 void * tdata;
162 void * tdata_save;
163 bfd_size_type readsize; /* Length of file_info. */
164 unsigned int scnhsz;
165 char *external_sections;
167 if (!(internal_f->f_flags & F_RELFLG))
168 abfd->flags |= HAS_RELOC;
169 if ((internal_f->f_flags & F_EXEC))
170 abfd->flags |= EXEC_P;
171 if (!(internal_f->f_flags & F_LNNO))
172 abfd->flags |= HAS_LINENO;
173 if (!(internal_f->f_flags & F_LSYMS))
174 abfd->flags |= HAS_LOCALS;
176 /* FIXME: How can we set D_PAGED correctly? */
177 if ((internal_f->f_flags & F_EXEC) != 0)
178 abfd->flags |= D_PAGED;
180 bfd_get_symcount (abfd) = internal_f->f_nsyms;
181 if (internal_f->f_nsyms)
182 abfd->flags |= HAS_SYMS;
184 if (internal_a != (struct internal_aouthdr *) NULL)
185 bfd_get_start_address (abfd) = internal_a->entry;
186 else
187 bfd_get_start_address (abfd) = 0;
189 /* Set up the tdata area. ECOFF uses its own routine, and overrides
190 abfd->flags. */
191 tdata_save = abfd->tdata.any;
192 tdata = bfd_coff_mkobject_hook (abfd, (void *) internal_f, (void *) internal_a);
193 if (tdata == NULL)
194 goto fail2;
196 scnhsz = bfd_coff_scnhsz (abfd);
197 readsize = (bfd_size_type) nscns * scnhsz;
198 external_sections = bfd_alloc (abfd, readsize);
199 if (!external_sections)
200 goto fail;
202 if (bfd_bread ((void *) external_sections, readsize, abfd) != readsize)
203 goto fail;
205 /* Set the arch/mach *before* swapping in sections; section header swapping
206 may depend on arch/mach info. */
207 if (! bfd_coff_set_arch_mach_hook (abfd, (void *) internal_f))
208 goto fail;
210 /* Now copy data as required; construct all asections etc. */
211 if (nscns != 0)
213 unsigned int i;
214 for (i = 0; i < nscns; i++)
216 struct internal_scnhdr tmp;
217 bfd_coff_swap_scnhdr_in (abfd,
218 (void *) (external_sections + i * scnhsz),
219 (void *) & tmp);
220 if (! make_a_section_from_file (abfd, &tmp, i + 1))
221 goto fail;
225 return abfd->xvec;
227 fail:
228 bfd_release (abfd, tdata);
229 fail2:
230 abfd->tdata.any = tdata_save;
231 abfd->flags = oflags;
232 bfd_get_start_address (abfd) = ostart;
233 return (const bfd_target *) NULL;
236 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
237 not a COFF file. This is also used by ECOFF. */
239 const bfd_target *
240 coff_object_p (bfd *abfd)
242 bfd_size_type filhsz;
243 bfd_size_type aoutsz;
244 unsigned int nscns;
245 void * filehdr;
246 struct internal_filehdr internal_f;
247 struct internal_aouthdr internal_a;
249 /* Figure out how much to read. */
250 filhsz = bfd_coff_filhsz (abfd);
251 aoutsz = bfd_coff_aoutsz (abfd);
253 filehdr = bfd_alloc (abfd, filhsz);
254 if (filehdr == NULL)
255 return NULL;
256 if (bfd_bread (filehdr, filhsz, abfd) != filhsz)
258 if (bfd_get_error () != bfd_error_system_call)
259 bfd_set_error (bfd_error_wrong_format);
260 bfd_release (abfd, filehdr);
261 return NULL;
263 bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f);
264 bfd_release (abfd, filehdr);
266 /* The XCOFF format has two sizes for the f_opthdr. SMALL_AOUTSZ
267 (less than aoutsz) used in object files and AOUTSZ (equal to
268 aoutsz) in executables. The bfd_coff_swap_aouthdr_in function
269 expects this header to be aoutsz bytes in length, so we use that
270 value in the call to bfd_alloc below. But we must be careful to
271 only read in f_opthdr bytes in the call to bfd_bread. We should
272 also attempt to catch corrupt or non-COFF binaries with a strange
273 value for f_opthdr. */
274 if (! bfd_coff_bad_format_hook (abfd, &internal_f)
275 || internal_f.f_opthdr > aoutsz)
277 bfd_set_error (bfd_error_wrong_format);
278 return NULL;
280 nscns = internal_f.f_nscns;
282 if (internal_f.f_opthdr)
284 void * opthdr;
286 opthdr = bfd_alloc (abfd, aoutsz);
287 if (opthdr == NULL)
288 return NULL;
289 if (bfd_bread (opthdr, (bfd_size_type) internal_f.f_opthdr, abfd)
290 != internal_f.f_opthdr)
292 bfd_release (abfd, opthdr);
293 return NULL;
295 bfd_coff_swap_aouthdr_in (abfd, opthdr, (void *) &internal_a);
296 bfd_release (abfd, opthdr);
299 return coff_real_object_p (abfd, nscns, &internal_f,
300 (internal_f.f_opthdr != 0
301 ? &internal_a
302 : (struct internal_aouthdr *) NULL));
305 /* Get the BFD section from a COFF symbol section number. */
307 asection *
308 coff_section_from_bfd_index (bfd *abfd, int index)
310 struct bfd_section *answer = abfd->sections;
312 if (index == N_ABS)
313 return bfd_abs_section_ptr;
314 if (index == N_UNDEF)
315 return bfd_und_section_ptr;
316 if (index == N_DEBUG)
317 return bfd_abs_section_ptr;
319 while (answer)
321 if (answer->target_index == index)
322 return answer;
323 answer = answer->next;
326 /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
327 has a bad symbol table in biglitpow.o. */
328 return bfd_und_section_ptr;
331 /* Get the upper bound of a COFF symbol table. */
333 long
334 coff_get_symtab_upper_bound (bfd *abfd)
336 if (!bfd_coff_slurp_symbol_table (abfd))
337 return -1;
339 return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *));
342 /* Canonicalize a COFF symbol table. */
344 long
345 coff_canonicalize_symtab (bfd *abfd, asymbol **alocation)
347 unsigned int counter;
348 coff_symbol_type *symbase;
349 coff_symbol_type **location = (coff_symbol_type **) alocation;
351 if (!bfd_coff_slurp_symbol_table (abfd))
352 return -1;
354 symbase = obj_symbols (abfd);
355 counter = bfd_get_symcount (abfd);
356 while (counter-- > 0)
357 *location++ = symbase++;
359 *location = NULL;
361 return bfd_get_symcount (abfd);
364 /* Get the name of a symbol. The caller must pass in a buffer of size
365 >= SYMNMLEN + 1. */
367 const char *
368 _bfd_coff_internal_syment_name (bfd *abfd,
369 const struct internal_syment *sym,
370 char *buf)
372 /* FIXME: It's not clear this will work correctly if sizeof
373 (_n_zeroes) != 4. */
374 if (sym->_n._n_n._n_zeroes != 0
375 || sym->_n._n_n._n_offset == 0)
377 memcpy (buf, sym->_n._n_name, SYMNMLEN);
378 buf[SYMNMLEN] = '\0';
379 return buf;
381 else
383 const char *strings;
385 BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE);
386 strings = obj_coff_strings (abfd);
387 if (strings == NULL)
389 strings = _bfd_coff_read_string_table (abfd);
390 if (strings == NULL)
391 return NULL;
393 return strings + sym->_n._n_n._n_offset;
397 /* Read in and swap the relocs. This returns a buffer holding the
398 relocs for section SEC in file ABFD. If CACHE is TRUE and
399 INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
400 the function is called again. If EXTERNAL_RELOCS is not NULL, it
401 is a buffer large enough to hold the unswapped relocs. If
402 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
403 the swapped relocs. If REQUIRE_INTERNAL is TRUE, then the return
404 value must be INTERNAL_RELOCS. The function returns NULL on error. */
406 struct internal_reloc *
407 _bfd_coff_read_internal_relocs (bfd *abfd,
408 asection *sec,
409 bfd_boolean cache,
410 bfd_byte *external_relocs,
411 bfd_boolean require_internal,
412 struct internal_reloc *internal_relocs)
414 bfd_size_type relsz;
415 bfd_byte *free_external = NULL;
416 struct internal_reloc *free_internal = NULL;
417 bfd_byte *erel;
418 bfd_byte *erel_end;
419 struct internal_reloc *irel;
420 bfd_size_type amt;
422 if (sec->reloc_count == 0)
423 return internal_relocs; /* Nothing to do. */
425 if (coff_section_data (abfd, sec) != NULL
426 && coff_section_data (abfd, sec)->relocs != NULL)
428 if (! require_internal)
429 return coff_section_data (abfd, sec)->relocs;
430 memcpy (internal_relocs, coff_section_data (abfd, sec)->relocs,
431 sec->reloc_count * sizeof (struct internal_reloc));
432 return internal_relocs;
435 relsz = bfd_coff_relsz (abfd);
437 amt = sec->reloc_count * relsz;
438 if (external_relocs == NULL)
440 free_external = bfd_malloc (amt);
441 if (free_external == NULL)
442 goto error_return;
443 external_relocs = free_external;
446 if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
447 || bfd_bread (external_relocs, amt, abfd) != amt)
448 goto error_return;
450 if (internal_relocs == NULL)
452 amt = sec->reloc_count;
453 amt *= sizeof (struct internal_reloc);
454 free_internal = bfd_malloc (amt);
455 if (free_internal == NULL)
456 goto error_return;
457 internal_relocs = free_internal;
460 /* Swap in the relocs. */
461 erel = external_relocs;
462 erel_end = erel + relsz * sec->reloc_count;
463 irel = internal_relocs;
464 for (; erel < erel_end; erel += relsz, irel++)
465 bfd_coff_swap_reloc_in (abfd, (void *) erel, (void *) irel);
467 if (free_external != NULL)
469 free (free_external);
470 free_external = NULL;
473 if (cache && free_internal != NULL)
475 if (coff_section_data (abfd, sec) == NULL)
477 amt = sizeof (struct coff_section_tdata);
478 sec->used_by_bfd = bfd_zalloc (abfd, amt);
479 if (sec->used_by_bfd == NULL)
480 goto error_return;
481 coff_section_data (abfd, sec)->contents = NULL;
483 coff_section_data (abfd, sec)->relocs = free_internal;
486 return internal_relocs;
488 error_return:
489 if (free_external != NULL)
490 free (free_external);
491 if (free_internal != NULL)
492 free (free_internal);
493 return NULL;
496 /* Set lineno_count for the output sections of a COFF file. */
499 coff_count_linenumbers (bfd *abfd)
501 unsigned int limit = bfd_get_symcount (abfd);
502 unsigned int i;
503 int total = 0;
504 asymbol **p;
505 asection *s;
507 if (limit == 0)
509 /* This may be from the backend linker, in which case the
510 lineno_count in the sections is correct. */
511 for (s = abfd->sections; s != NULL; s = s->next)
512 total += s->lineno_count;
513 return total;
516 for (s = abfd->sections; s != NULL; s = s->next)
517 BFD_ASSERT (s->lineno_count == 0);
519 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
521 asymbol *q_maybe = *p;
523 if (bfd_family_coff (bfd_asymbol_bfd (q_maybe)))
525 coff_symbol_type *q = coffsymbol (q_maybe);
527 /* The AIX 4.1 compiler can sometimes generate line numbers
528 attached to debugging symbols. We try to simply ignore
529 those here. */
530 if (q->lineno != NULL
531 && q->symbol.section->owner != NULL)
533 /* This symbol has line numbers. Increment the owning
534 section's linenumber count. */
535 alent *l = q->lineno;
539 asection * sec = q->symbol.section->output_section;
541 /* Do not try to update fields in read-only sections. */
542 if (! bfd_is_const_section (sec))
543 sec->lineno_count ++;
545 ++total;
546 ++l;
548 while (l->line_number != 0);
553 return total;
556 /* Takes a bfd and a symbol, returns a pointer to the coff specific
557 area of the symbol if there is one. */
559 coff_symbol_type *
560 coff_symbol_from (bfd *ignore_abfd ATTRIBUTE_UNUSED,
561 asymbol *symbol)
563 if (!bfd_family_coff (bfd_asymbol_bfd (symbol)))
564 return (coff_symbol_type *) NULL;
566 if (bfd_asymbol_bfd (symbol)->tdata.coff_obj_data == (coff_data_type *) NULL)
567 return (coff_symbol_type *) NULL;
569 return (coff_symbol_type *) symbol;
572 static void
573 fixup_symbol_value (bfd *abfd,
574 coff_symbol_type *coff_symbol_ptr,
575 struct internal_syment *syment)
577 /* Normalize the symbol flags. */
578 if (coff_symbol_ptr->symbol.section
579 && bfd_is_com_section (coff_symbol_ptr->symbol.section))
581 /* A common symbol is undefined with a value. */
582 syment->n_scnum = N_UNDEF;
583 syment->n_value = coff_symbol_ptr->symbol.value;
585 else if ((coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) != 0
586 && (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING_RELOC) == 0)
588 syment->n_value = coff_symbol_ptr->symbol.value;
590 else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
592 syment->n_scnum = N_UNDEF;
593 syment->n_value = 0;
595 /* FIXME: Do we need to handle the absolute section here? */
596 else
598 if (coff_symbol_ptr->symbol.section)
600 syment->n_scnum =
601 coff_symbol_ptr->symbol.section->output_section->target_index;
603 syment->n_value = (coff_symbol_ptr->symbol.value
604 + coff_symbol_ptr->symbol.section->output_offset);
605 if (! obj_pe (abfd))
607 syment->n_value += (syment->n_sclass == C_STATLAB)
608 ? coff_symbol_ptr->symbol.section->output_section->lma
609 : coff_symbol_ptr->symbol.section->output_section->vma;
612 else
614 BFD_ASSERT (0);
615 /* This can happen, but I don't know why yet (steve@cygnus.com) */
616 syment->n_scnum = N_ABS;
617 syment->n_value = coff_symbol_ptr->symbol.value;
622 /* Run through all the symbols in the symbol table and work out what
623 their indexes into the symbol table will be when output.
625 Coff requires that each C_FILE symbol points to the next one in the
626 chain, and that the last one points to the first external symbol. We
627 do that here too. */
629 bfd_boolean
630 coff_renumber_symbols (bfd *bfd_ptr, int *first_undef)
632 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
633 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
634 unsigned int native_index = 0;
635 struct internal_syment *last_file = NULL;
636 unsigned int symbol_index;
638 /* COFF demands that undefined symbols come after all other symbols.
639 Since we don't need to impose this extra knowledge on all our
640 client programs, deal with that here. Sort the symbol table;
641 just move the undefined symbols to the end, leaving the rest
642 alone. The O'Reilly book says that defined global symbols come
643 at the end before the undefined symbols, so we do that here as
644 well. */
645 /* @@ Do we have some condition we could test for, so we don't always
646 have to do this? I don't think relocatability is quite right, but
647 I'm not certain. [raeburn:19920508.1711EST] */
649 asymbol **newsyms;
650 unsigned int i;
651 bfd_size_type amt;
653 amt = sizeof (asymbol *) * ((bfd_size_type) symbol_count + 1);
654 newsyms = bfd_alloc (bfd_ptr, amt);
655 if (!newsyms)
656 return FALSE;
657 bfd_ptr->outsymbols = newsyms;
658 for (i = 0; i < symbol_count; i++)
659 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
660 || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
661 && !bfd_is_com_section (symbol_ptr_ptr[i]->section)
662 && ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) != 0
663 || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
664 == 0))))
665 *newsyms++ = symbol_ptr_ptr[i];
667 for (i = 0; i < symbol_count; i++)
668 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
669 && !bfd_is_und_section (symbol_ptr_ptr[i]->section)
670 && (bfd_is_com_section (symbol_ptr_ptr[i]->section)
671 || ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) == 0
672 && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
673 != 0))))
674 *newsyms++ = symbol_ptr_ptr[i];
676 *first_undef = newsyms - bfd_ptr->outsymbols;
678 for (i = 0; i < symbol_count; i++)
679 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
680 && bfd_is_und_section (symbol_ptr_ptr[i]->section))
681 *newsyms++ = symbol_ptr_ptr[i];
682 *newsyms = (asymbol *) NULL;
683 symbol_ptr_ptr = bfd_ptr->outsymbols;
686 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
688 coff_symbol_type *coff_symbol_ptr = coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
689 symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
690 if (coff_symbol_ptr && coff_symbol_ptr->native)
692 combined_entry_type *s = coff_symbol_ptr->native;
693 int i;
695 if (s->u.syment.n_sclass == C_FILE)
697 if (last_file != NULL)
698 last_file->n_value = native_index;
699 last_file = &(s->u.syment);
701 else
702 /* Modify the symbol values according to their section and
703 type. */
704 fixup_symbol_value (bfd_ptr, coff_symbol_ptr, &(s->u.syment));
706 for (i = 0; i < s->u.syment.n_numaux + 1; i++)
707 s[i].offset = native_index++;
709 else
710 native_index++;
713 obj_conv_table_size (bfd_ptr) = native_index;
715 return TRUE;
718 /* Run thorough the symbol table again, and fix it so that all
719 pointers to entries are changed to the entries' index in the output
720 symbol table. */
722 void
723 coff_mangle_symbols (bfd *bfd_ptr)
725 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
726 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
727 unsigned int symbol_index;
729 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
731 coff_symbol_type *coff_symbol_ptr =
732 coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
734 if (coff_symbol_ptr && coff_symbol_ptr->native)
736 int i;
737 combined_entry_type *s = coff_symbol_ptr->native;
739 if (s->fix_value)
741 /* FIXME: We should use a union here. */
742 s->u.syment.n_value =
743 (bfd_hostptr_t) ((combined_entry_type *)
744 ((bfd_hostptr_t) s->u.syment.n_value))->offset;
745 s->fix_value = 0;
747 if (s->fix_line)
749 /* The value is the offset into the line number entries
750 for the symbol's section. On output, the symbol's
751 section should be N_DEBUG. */
752 s->u.syment.n_value =
753 (coff_symbol_ptr->symbol.section->output_section->line_filepos
754 + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr));
755 coff_symbol_ptr->symbol.section =
756 coff_section_from_bfd_index (bfd_ptr, N_DEBUG);
757 BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
759 for (i = 0; i < s->u.syment.n_numaux; i++)
761 combined_entry_type *a = s + i + 1;
762 if (a->fix_tag)
764 a->u.auxent.x_sym.x_tagndx.l =
765 a->u.auxent.x_sym.x_tagndx.p->offset;
766 a->fix_tag = 0;
768 if (a->fix_end)
770 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
771 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
772 a->fix_end = 0;
774 if (a->fix_scnlen)
776 a->u.auxent.x_csect.x_scnlen.l =
777 a->u.auxent.x_csect.x_scnlen.p->offset;
778 a->fix_scnlen = 0;
785 static void
786 coff_fix_symbol_name (bfd *abfd,
787 asymbol *symbol,
788 combined_entry_type *native,
789 bfd_size_type *string_size_p,
790 asection **debug_string_section_p,
791 bfd_size_type *debug_string_size_p)
793 unsigned int name_length;
794 union internal_auxent *auxent;
795 char *name = (char *) (symbol->name);
797 if (name == NULL)
799 /* COFF symbols always have names, so we'll make one up. */
800 symbol->name = "strange";
801 name = (char *) symbol->name;
803 name_length = strlen (name);
805 if (native->u.syment.n_sclass == C_FILE
806 && native->u.syment.n_numaux > 0)
808 unsigned int filnmlen;
810 if (bfd_coff_force_symnames_in_strings (abfd))
812 native->u.syment._n._n_n._n_offset =
813 (*string_size_p + STRING_SIZE_SIZE);
814 native->u.syment._n._n_n._n_zeroes = 0;
815 *string_size_p += 6; /* strlen(".file") + 1 */
817 else
818 strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
820 auxent = &(native + 1)->u.auxent;
822 filnmlen = bfd_coff_filnmlen (abfd);
824 if (bfd_coff_long_filenames (abfd))
826 if (name_length <= filnmlen)
827 strncpy (auxent->x_file.x_fname, name, filnmlen);
828 else
830 auxent->x_file.x_n.x_offset = *string_size_p + STRING_SIZE_SIZE;
831 auxent->x_file.x_n.x_zeroes = 0;
832 *string_size_p += name_length + 1;
835 else
837 strncpy (auxent->x_file.x_fname, name, filnmlen);
838 if (name_length > filnmlen)
839 name[filnmlen] = '\0';
842 else
844 if (name_length <= SYMNMLEN && !bfd_coff_force_symnames_in_strings (abfd))
845 /* This name will fit into the symbol neatly. */
846 strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
848 else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
850 native->u.syment._n._n_n._n_offset = (*string_size_p
851 + STRING_SIZE_SIZE);
852 native->u.syment._n._n_n._n_zeroes = 0;
853 *string_size_p += name_length + 1;
855 else
857 file_ptr filepos;
858 bfd_byte buf[4];
859 int prefix_len = bfd_coff_debug_string_prefix_length (abfd);
861 /* This name should be written into the .debug section. For
862 some reason each name is preceded by a two byte length
863 and also followed by a null byte. FIXME: We assume that
864 the .debug section has already been created, and that it
865 is large enough. */
866 if (*debug_string_section_p == (asection *) NULL)
867 *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
868 filepos = bfd_tell (abfd);
869 if (prefix_len == 4)
870 bfd_put_32 (abfd, (bfd_vma) (name_length + 1), buf);
871 else
872 bfd_put_16 (abfd, (bfd_vma) (name_length + 1), buf);
874 if (!bfd_set_section_contents (abfd,
875 *debug_string_section_p,
876 (void *) buf,
877 (file_ptr) *debug_string_size_p,
878 (bfd_size_type) prefix_len)
879 || !bfd_set_section_contents (abfd,
880 *debug_string_section_p,
881 (void *) symbol->name,
882 (file_ptr) (*debug_string_size_p
883 + prefix_len),
884 (bfd_size_type) name_length + 1))
885 abort ();
886 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
887 abort ();
888 native->u.syment._n._n_n._n_offset =
889 *debug_string_size_p + prefix_len;
890 native->u.syment._n._n_n._n_zeroes = 0;
891 *debug_string_size_p += name_length + 1 + prefix_len;
896 /* We need to keep track of the symbol index so that when we write out
897 the relocs we can get the index for a symbol. This method is a
898 hack. FIXME. */
900 #define set_index(symbol, idx) ((symbol)->udata.i = (idx))
902 /* Write a symbol out to a COFF file. */
904 static bfd_boolean
905 coff_write_symbol (bfd *abfd,
906 asymbol *symbol,
907 combined_entry_type *native,
908 bfd_vma *written,
909 bfd_size_type *string_size_p,
910 asection **debug_string_section_p,
911 bfd_size_type *debug_string_size_p)
913 unsigned int numaux = native->u.syment.n_numaux;
914 int type = native->u.syment.n_type;
915 int class = native->u.syment.n_sclass;
916 void * buf;
917 bfd_size_type symesz;
919 if (native->u.syment.n_sclass == C_FILE)
920 symbol->flags |= BSF_DEBUGGING;
922 if (symbol->flags & BSF_DEBUGGING
923 && bfd_is_abs_section (symbol->section))
924 native->u.syment.n_scnum = N_DEBUG;
926 else if (bfd_is_abs_section (symbol->section))
927 native->u.syment.n_scnum = N_ABS;
929 else if (bfd_is_und_section (symbol->section))
930 native->u.syment.n_scnum = N_UNDEF;
932 else
933 native->u.syment.n_scnum =
934 symbol->section->output_section->target_index;
936 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
937 debug_string_section_p, debug_string_size_p);
939 symesz = bfd_coff_symesz (abfd);
940 buf = bfd_alloc (abfd, symesz);
941 if (!buf)
942 return FALSE;
943 bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
944 if (bfd_bwrite (buf, symesz, abfd) != symesz)
945 return FALSE;
946 bfd_release (abfd, buf);
948 if (native->u.syment.n_numaux > 0)
950 bfd_size_type auxesz;
951 unsigned int j;
953 auxesz = bfd_coff_auxesz (abfd);
954 buf = bfd_alloc (abfd, auxesz);
955 if (!buf)
956 return FALSE;
957 for (j = 0; j < native->u.syment.n_numaux; j++)
959 bfd_coff_swap_aux_out (abfd,
960 &((native + j + 1)->u.auxent),
961 type, class, (int) j,
962 native->u.syment.n_numaux,
963 buf);
964 if (bfd_bwrite (buf, auxesz, abfd) != auxesz)
965 return FALSE;
967 bfd_release (abfd, buf);
970 /* Store the index for use when we write out the relocs. */
971 set_index (symbol, *written);
973 *written += numaux + 1;
974 return TRUE;
977 /* Write out a symbol to a COFF file that does not come from a COFF
978 file originally. This symbol may have been created by the linker,
979 or we may be linking a non COFF file to a COFF file. */
981 static bfd_boolean
982 coff_write_alien_symbol (bfd *abfd,
983 asymbol *symbol,
984 bfd_vma *written,
985 bfd_size_type *string_size_p,
986 asection **debug_string_section_p,
987 bfd_size_type *debug_string_size_p)
989 combined_entry_type *native;
990 combined_entry_type dummy;
992 native = &dummy;
993 native->u.syment.n_type = T_NULL;
994 native->u.syment.n_flags = 0;
995 if (bfd_is_und_section (symbol->section))
997 native->u.syment.n_scnum = N_UNDEF;
998 native->u.syment.n_value = symbol->value;
1000 else if (bfd_is_com_section (symbol->section))
1002 native->u.syment.n_scnum = N_UNDEF;
1003 native->u.syment.n_value = symbol->value;
1005 else if (symbol->flags & BSF_DEBUGGING)
1007 /* There isn't much point to writing out a debugging symbol
1008 unless we are prepared to convert it into COFF debugging
1009 format. So, we just ignore them. We must clobber the symbol
1010 name to keep it from being put in the string table. */
1011 symbol->name = "";
1012 return TRUE;
1014 else
1016 native->u.syment.n_scnum =
1017 symbol->section->output_section->target_index;
1018 native->u.syment.n_value = (symbol->value
1019 + symbol->section->output_offset);
1020 if (! obj_pe (abfd))
1021 native->u.syment.n_value += symbol->section->output_section->vma;
1023 /* Copy the any flags from the file header into the symbol.
1024 FIXME: Why? */
1026 coff_symbol_type *c = coff_symbol_from (abfd, symbol);
1027 if (c != (coff_symbol_type *) NULL)
1028 native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
1032 native->u.syment.n_type = 0;
1033 if (symbol->flags & BSF_LOCAL)
1034 native->u.syment.n_sclass = C_STAT;
1035 else if (symbol->flags & BSF_WEAK)
1036 native->u.syment.n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1037 else
1038 native->u.syment.n_sclass = C_EXT;
1039 native->u.syment.n_numaux = 0;
1041 return coff_write_symbol (abfd, symbol, native, written, string_size_p,
1042 debug_string_section_p, debug_string_size_p);
1045 /* Write a native symbol to a COFF file. */
1047 static bfd_boolean
1048 coff_write_native_symbol (bfd *abfd,
1049 coff_symbol_type *symbol,
1050 bfd_vma *written,
1051 bfd_size_type *string_size_p,
1052 asection **debug_string_section_p,
1053 bfd_size_type *debug_string_size_p)
1055 combined_entry_type *native = symbol->native;
1056 alent *lineno = symbol->lineno;
1058 /* If this symbol has an associated line number, we must store the
1059 symbol index in the line number field. We also tag the auxent to
1060 point to the right place in the lineno table. */
1061 if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
1063 unsigned int count = 0;
1065 lineno[count].u.offset = *written;
1066 if (native->u.syment.n_numaux)
1068 union internal_auxent *a = &((native + 1)->u.auxent);
1070 a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
1071 symbol->symbol.section->output_section->moving_line_filepos;
1074 /* Count and relocate all other linenumbers. */
1075 count++;
1076 while (lineno[count].line_number != 0)
1078 lineno[count].u.offset +=
1079 (symbol->symbol.section->output_section->vma
1080 + symbol->symbol.section->output_offset);
1081 count++;
1083 symbol->done_lineno = TRUE;
1085 if (! bfd_is_const_section (symbol->symbol.section->output_section))
1086 symbol->symbol.section->output_section->moving_line_filepos +=
1087 count * bfd_coff_linesz (abfd);
1090 return coff_write_symbol (abfd, &(symbol->symbol), native, written,
1091 string_size_p, debug_string_section_p,
1092 debug_string_size_p);
1095 static void
1096 null_error_handler (const char * fmt ATTRIBUTE_UNUSED, ...)
1100 /* Write out the COFF symbols. */
1102 bfd_boolean
1103 coff_write_symbols (bfd *abfd)
1105 bfd_size_type string_size;
1106 asection *debug_string_section;
1107 bfd_size_type debug_string_size;
1108 unsigned int i;
1109 unsigned int limit = bfd_get_symcount (abfd);
1110 bfd_vma written = 0;
1111 asymbol **p;
1113 string_size = 0;
1114 debug_string_section = NULL;
1115 debug_string_size = 0;
1117 /* If this target supports long section names, they must be put into
1118 the string table. This is supported by PE. This code must
1119 handle section names just as they are handled in
1120 coff_write_object_contents. */
1121 if (bfd_coff_long_section_names (abfd))
1123 asection *o;
1125 for (o = abfd->sections; o != NULL; o = o->next)
1127 size_t len;
1129 len = strlen (o->name);
1130 if (len > SCNNMLEN)
1131 string_size += len + 1;
1135 /* Seek to the right place. */
1136 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1137 return FALSE;
1139 /* Output all the symbols we have. */
1140 written = 0;
1141 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
1143 asymbol *symbol = *p;
1144 coff_symbol_type *c_symbol = coff_symbol_from (abfd, symbol);
1146 if (c_symbol == (coff_symbol_type *) NULL
1147 || c_symbol->native == (combined_entry_type *) NULL)
1149 if (!coff_write_alien_symbol (abfd, symbol, &written, &string_size,
1150 &debug_string_section,
1151 &debug_string_size))
1152 return FALSE;
1154 else
1156 if (coff_backend_info (abfd)->_bfd_coff_classify_symbol != NULL)
1158 bfd_error_handler_type current_error_handler;
1159 enum coff_symbol_classification class;
1160 unsigned char *n_sclass;
1162 /* Suppress error reporting by bfd_coff_classify_symbol.
1163 Error messages can be generated when we are processing a local
1164 symbol which has no associated section and we do not have to
1165 worry about this, all we need to know is that it is local. */
1166 current_error_handler = bfd_set_error_handler (null_error_handler);
1167 class = bfd_coff_classify_symbol (abfd, &c_symbol->native->u.syment);
1168 (void) bfd_set_error_handler (current_error_handler);
1170 n_sclass = &c_symbol->native->u.syment.n_sclass;
1172 /* If the symbol class has been changed (eg objcopy/ld script/etc)
1173 we cannot retain the existing sclass from the original symbol.
1174 Weak symbols only have one valid sclass, so just set it always.
1175 If it is not local class and should be, set it C_STAT.
1176 If it is global and not classified as global, or if it is
1177 weak (which is also classified as global), set it C_EXT. */
1179 if (symbol->flags & BSF_WEAK)
1180 *n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1181 else if (symbol->flags & BSF_LOCAL && class != COFF_SYMBOL_LOCAL)
1182 *n_sclass = C_STAT;
1183 else if (symbol->flags & BSF_GLOBAL
1184 && (class != COFF_SYMBOL_GLOBAL
1185 #ifdef COFF_WITH_PE
1186 || *n_sclass == C_NT_WEAK
1187 #endif
1188 || *n_sclass == C_WEAKEXT))
1189 c_symbol->native->u.syment.n_sclass = C_EXT;
1192 if (!coff_write_native_symbol (abfd, c_symbol, &written,
1193 &string_size, &debug_string_section,
1194 &debug_string_size))
1195 return FALSE;
1199 obj_raw_syment_count (abfd) = written;
1201 /* Now write out strings. */
1202 if (string_size != 0)
1204 unsigned int size = string_size + STRING_SIZE_SIZE;
1205 bfd_byte buffer[STRING_SIZE_SIZE];
1207 #if STRING_SIZE_SIZE == 4
1208 H_PUT_32 (abfd, size, buffer);
1209 #else
1210 #error Change H_PUT_32
1211 #endif
1212 if (bfd_bwrite ((void *) buffer, (bfd_size_type) sizeof (buffer), abfd)
1213 != sizeof (buffer))
1214 return FALSE;
1216 /* Handle long section names. This code must handle section
1217 names just as they are handled in coff_write_object_contents. */
1218 if (bfd_coff_long_section_names (abfd))
1220 asection *o;
1222 for (o = abfd->sections; o != NULL; o = o->next)
1224 size_t len;
1226 len = strlen (o->name);
1227 if (len > SCNNMLEN)
1229 if (bfd_bwrite (o->name, (bfd_size_type) (len + 1), abfd)
1230 != len + 1)
1231 return FALSE;
1236 for (p = abfd->outsymbols, i = 0;
1237 i < limit;
1238 i++, p++)
1240 asymbol *q = *p;
1241 size_t name_length = strlen (q->name);
1242 coff_symbol_type *c_symbol = coff_symbol_from (abfd, q);
1243 size_t maxlen;
1245 /* Figure out whether the symbol name should go in the string
1246 table. Symbol names that are short enough are stored
1247 directly in the syment structure. File names permit a
1248 different, longer, length in the syment structure. On
1249 XCOFF, some symbol names are stored in the .debug section
1250 rather than in the string table. */
1252 if (c_symbol == NULL
1253 || c_symbol->native == NULL)
1254 /* This is not a COFF symbol, so it certainly is not a
1255 file name, nor does it go in the .debug section. */
1256 maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
1258 else if (bfd_coff_symname_in_debug (abfd,
1259 &c_symbol->native->u.syment))
1260 /* This symbol name is in the XCOFF .debug section.
1261 Don't write it into the string table. */
1262 maxlen = name_length;
1264 else if (c_symbol->native->u.syment.n_sclass == C_FILE
1265 && c_symbol->native->u.syment.n_numaux > 0)
1267 if (bfd_coff_force_symnames_in_strings (abfd))
1269 if (bfd_bwrite (".file", (bfd_size_type) 6, abfd) != 6)
1270 return FALSE;
1272 maxlen = bfd_coff_filnmlen (abfd);
1274 else
1275 maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
1277 if (name_length > maxlen)
1279 if (bfd_bwrite ((void *) (q->name), (bfd_size_type) name_length + 1,
1280 abfd) != name_length + 1)
1281 return FALSE;
1285 else
1287 /* We would normally not write anything here, but we'll write
1288 out 4 so that any stupid coff reader which tries to read the
1289 string table even when there isn't one won't croak. */
1290 unsigned int size = STRING_SIZE_SIZE;
1291 bfd_byte buffer[STRING_SIZE_SIZE];
1293 #if STRING_SIZE_SIZE == 4
1294 H_PUT_32 (abfd, size, buffer);
1295 #else
1296 #error Change H_PUT_32
1297 #endif
1298 if (bfd_bwrite ((void *) buffer, (bfd_size_type) STRING_SIZE_SIZE, abfd)
1299 != STRING_SIZE_SIZE)
1300 return FALSE;
1303 /* Make sure the .debug section was created to be the correct size.
1304 We should create it ourselves on the fly, but we don't because
1305 BFD won't let us write to any section until we know how large all
1306 the sections are. We could still do it by making another pass
1307 over the symbols. FIXME. */
1308 BFD_ASSERT (debug_string_size == 0
1309 || (debug_string_section != (asection *) NULL
1310 && (BFD_ALIGN (debug_string_size,
1311 1 << debug_string_section->alignment_power)
1312 == debug_string_section->size)));
1314 return TRUE;
1317 bfd_boolean
1318 coff_write_linenumbers (bfd *abfd)
1320 asection *s;
1321 bfd_size_type linesz;
1322 void * buff;
1324 linesz = bfd_coff_linesz (abfd);
1325 buff = bfd_alloc (abfd, linesz);
1326 if (!buff)
1327 return FALSE;
1328 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1330 if (s->lineno_count)
1332 asymbol **q = abfd->outsymbols;
1333 if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
1334 return FALSE;
1335 /* Find all the linenumbers in this section. */
1336 while (*q)
1338 asymbol *p = *q;
1339 if (p->section->output_section == s)
1341 alent *l =
1342 BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
1343 (bfd_asymbol_bfd (p), p));
1344 if (l)
1346 /* Found a linenumber entry, output. */
1347 struct internal_lineno out;
1348 memset ((void *) & out, 0, sizeof (out));
1349 out.l_lnno = 0;
1350 out.l_addr.l_symndx = l->u.offset;
1351 bfd_coff_swap_lineno_out (abfd, &out, buff);
1352 if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd)
1353 != linesz)
1354 return FALSE;
1355 l++;
1356 while (l->line_number)
1358 out.l_lnno = l->line_number;
1359 out.l_addr.l_symndx = l->u.offset;
1360 bfd_coff_swap_lineno_out (abfd, &out, buff);
1361 if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd)
1362 != linesz)
1363 return FALSE;
1364 l++;
1368 q++;
1372 bfd_release (abfd, buff);
1373 return TRUE;
1376 alent *
1377 coff_get_lineno (bfd *ignore_abfd ATTRIBUTE_UNUSED, asymbol *symbol)
1379 return coffsymbol (symbol)->lineno;
1382 /* This function transforms the offsets into the symbol table into
1383 pointers to syments. */
1385 static void
1386 coff_pointerize_aux (bfd *abfd,
1387 combined_entry_type *table_base,
1388 combined_entry_type *symbol,
1389 unsigned int indaux,
1390 combined_entry_type *auxent)
1392 unsigned int type = symbol->u.syment.n_type;
1393 unsigned int class = symbol->u.syment.n_sclass;
1395 if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1397 if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1398 (abfd, table_base, symbol, indaux, auxent))
1399 return;
1402 /* Don't bother if this is a file or a section. */
1403 if (class == C_STAT && type == T_NULL)
1404 return;
1405 if (class == C_FILE)
1406 return;
1408 /* Otherwise patch up. */
1409 #define N_TMASK coff_data (abfd)->local_n_tmask
1410 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1412 if ((ISFCN (type) || ISTAG (class) || class == C_BLOCK || class == C_FCN)
1413 && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l > 0)
1415 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
1416 table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
1417 auxent->fix_end = 1;
1419 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1420 generate one, so we must be careful to ignore it. */
1421 if (auxent->u.auxent.x_sym.x_tagndx.l > 0)
1423 auxent->u.auxent.x_sym.x_tagndx.p =
1424 table_base + auxent->u.auxent.x_sym.x_tagndx.l;
1425 auxent->fix_tag = 1;
1429 /* Allocate space for the ".debug" section, and read it.
1430 We did not read the debug section until now, because
1431 we didn't want to go to the trouble until someone needed it. */
1433 static char *
1434 build_debug_section (bfd *abfd)
1436 char *debug_section;
1437 file_ptr position;
1438 bfd_size_type sec_size;
1440 asection *sect = bfd_get_section_by_name (abfd, ".debug");
1442 if (!sect)
1444 bfd_set_error (bfd_error_no_debug_section);
1445 return NULL;
1448 sec_size = sect->size;
1449 debug_section = bfd_alloc (abfd, sec_size);
1450 if (debug_section == NULL)
1451 return NULL;
1453 /* Seek to the beginning of the `.debug' section and read it.
1454 Save the current position first; it is needed by our caller.
1455 Then read debug section and reset the file pointer. */
1457 position = bfd_tell (abfd);
1458 if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0
1459 || bfd_bread (debug_section, sec_size, abfd) != sec_size
1460 || bfd_seek (abfd, position, SEEK_SET) != 0)
1461 return NULL;
1462 return debug_section;
1465 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1466 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1467 be \0-terminated. */
1469 static char *
1470 copy_name (bfd *abfd, char *name, size_t maxlen)
1472 size_t len;
1473 char *newname;
1475 for (len = 0; len < maxlen; ++len)
1476 if (name[len] == '\0')
1477 break;
1479 if ((newname = bfd_alloc (abfd, (bfd_size_type) len + 1)) == NULL)
1480 return NULL;
1482 strncpy (newname, name, len);
1483 newname[len] = '\0';
1484 return newname;
1487 /* Read in the external symbols. */
1489 bfd_boolean
1490 _bfd_coff_get_external_symbols (bfd *abfd)
1492 bfd_size_type symesz;
1493 bfd_size_type size;
1494 void * syms;
1496 if (obj_coff_external_syms (abfd) != NULL)
1497 return TRUE;
1499 symesz = bfd_coff_symesz (abfd);
1501 size = obj_raw_syment_count (abfd) * symesz;
1502 if (size == 0)
1503 return TRUE;
1505 syms = bfd_malloc (size);
1506 if (syms == NULL)
1507 return FALSE;
1509 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
1510 || bfd_bread (syms, size, abfd) != size)
1512 if (syms != NULL)
1513 free (syms);
1514 return FALSE;
1517 obj_coff_external_syms (abfd) = syms;
1519 return TRUE;
1522 /* Read in the external strings. The strings are not loaded until
1523 they are needed. This is because we have no simple way of
1524 detecting a missing string table in an archive. */
1526 const char *
1527 _bfd_coff_read_string_table (bfd *abfd)
1529 char extstrsize[STRING_SIZE_SIZE];
1530 bfd_size_type strsize;
1531 char *strings;
1532 file_ptr pos;
1534 if (obj_coff_strings (abfd) != NULL)
1535 return obj_coff_strings (abfd);
1537 if (obj_sym_filepos (abfd) == 0)
1539 bfd_set_error (bfd_error_no_symbols);
1540 return NULL;
1543 pos = obj_sym_filepos (abfd);
1544 pos += obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd);
1545 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
1546 return NULL;
1548 if (bfd_bread (extstrsize, (bfd_size_type) sizeof extstrsize, abfd)
1549 != sizeof extstrsize)
1551 if (bfd_get_error () != bfd_error_file_truncated)
1552 return NULL;
1554 /* There is no string table. */
1555 strsize = STRING_SIZE_SIZE;
1557 else
1559 #if STRING_SIZE_SIZE == 4
1560 strsize = H_GET_32 (abfd, extstrsize);
1561 #else
1562 #error Change H_GET_32
1563 #endif
1566 if (strsize < STRING_SIZE_SIZE)
1568 (*_bfd_error_handler)
1569 (_("%B: bad string table size %lu"), abfd, (unsigned long) strsize);
1570 bfd_set_error (bfd_error_bad_value);
1571 return NULL;
1574 strings = bfd_malloc (strsize);
1575 if (strings == NULL)
1576 return NULL;
1578 if (bfd_bread (strings + STRING_SIZE_SIZE, strsize - STRING_SIZE_SIZE, abfd)
1579 != strsize - STRING_SIZE_SIZE)
1581 free (strings);
1582 return NULL;
1585 obj_coff_strings (abfd) = strings;
1587 return strings;
1590 /* Free up the external symbols and strings read from a COFF file. */
1592 bfd_boolean
1593 _bfd_coff_free_symbols (bfd *abfd)
1595 if (obj_coff_external_syms (abfd) != NULL
1596 && ! obj_coff_keep_syms (abfd))
1598 free (obj_coff_external_syms (abfd));
1599 obj_coff_external_syms (abfd) = NULL;
1601 if (obj_coff_strings (abfd) != NULL
1602 && ! obj_coff_keep_strings (abfd))
1604 free (obj_coff_strings (abfd));
1605 obj_coff_strings (abfd) = NULL;
1607 return TRUE;
1610 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1611 knit the symbol names into a normalized form. By normalized here I
1612 mean that all symbols have an n_offset pointer that points to a null-
1613 terminated string. */
1615 combined_entry_type *
1616 coff_get_normalized_symtab (bfd *abfd)
1618 combined_entry_type *internal;
1619 combined_entry_type *internal_ptr;
1620 combined_entry_type *symbol_ptr;
1621 combined_entry_type *internal_end;
1622 size_t symesz;
1623 char *raw_src;
1624 char *raw_end;
1625 const char *string_table = NULL;
1626 char *debug_section = NULL;
1627 bfd_size_type size;
1629 if (obj_raw_syments (abfd) != NULL)
1630 return obj_raw_syments (abfd);
1632 size = obj_raw_syment_count (abfd) * sizeof (combined_entry_type);
1633 internal = bfd_zalloc (abfd, size);
1634 if (internal == NULL && size != 0)
1635 return NULL;
1636 internal_end = internal + obj_raw_syment_count (abfd);
1638 if (! _bfd_coff_get_external_symbols (abfd))
1639 return NULL;
1641 raw_src = (char *) obj_coff_external_syms (abfd);
1643 /* Mark the end of the symbols. */
1644 symesz = bfd_coff_symesz (abfd);
1645 raw_end = (char *) raw_src + obj_raw_syment_count (abfd) * symesz;
1647 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1648 probably possible. If one shows up, it will probably kill us. */
1650 /* Swap all the raw entries. */
1651 for (internal_ptr = internal;
1652 raw_src < raw_end;
1653 raw_src += symesz, internal_ptr++)
1656 unsigned int i;
1657 bfd_coff_swap_sym_in (abfd, (void *) raw_src,
1658 (void *) & internal_ptr->u.syment);
1659 symbol_ptr = internal_ptr;
1661 for (i = 0;
1662 i < symbol_ptr->u.syment.n_numaux;
1663 i++)
1665 internal_ptr++;
1666 raw_src += symesz;
1667 bfd_coff_swap_aux_in (abfd, (void *) raw_src,
1668 symbol_ptr->u.syment.n_type,
1669 symbol_ptr->u.syment.n_sclass,
1670 (int) i, symbol_ptr->u.syment.n_numaux,
1671 &(internal_ptr->u.auxent));
1672 coff_pointerize_aux (abfd, internal, symbol_ptr, i,
1673 internal_ptr);
1677 /* Free the raw symbols, but not the strings (if we have them). */
1678 obj_coff_keep_strings (abfd) = TRUE;
1679 if (! _bfd_coff_free_symbols (abfd))
1680 return NULL;
1682 for (internal_ptr = internal; internal_ptr < internal_end;
1683 internal_ptr++)
1685 if (internal_ptr->u.syment.n_sclass == C_FILE
1686 && internal_ptr->u.syment.n_numaux > 0)
1688 /* Make a file symbol point to the name in the auxent, since
1689 the text ".file" is redundant. */
1690 if ((internal_ptr + 1)->u.auxent.x_file.x_n.x_zeroes == 0)
1692 /* The filename is a long one, point into the string table. */
1693 if (string_table == NULL)
1695 string_table = _bfd_coff_read_string_table (abfd);
1696 if (string_table == NULL)
1697 return NULL;
1700 internal_ptr->u.syment._n._n_n._n_offset =
1701 ((bfd_hostptr_t)
1702 (string_table
1703 + (internal_ptr + 1)->u.auxent.x_file.x_n.x_offset));
1705 else
1707 /* Ordinary short filename, put into memory anyway. The
1708 Microsoft PE tools sometimes store a filename in
1709 multiple AUX entries. */
1710 if (internal_ptr->u.syment.n_numaux > 1
1711 && coff_data (abfd)->pe)
1712 internal_ptr->u.syment._n._n_n._n_offset =
1713 ((bfd_hostptr_t)
1714 copy_name (abfd,
1715 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1716 internal_ptr->u.syment.n_numaux * symesz));
1717 else
1718 internal_ptr->u.syment._n._n_n._n_offset =
1719 ((bfd_hostptr_t)
1720 copy_name (abfd,
1721 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1722 (size_t) bfd_coff_filnmlen (abfd)));
1725 else
1727 if (internal_ptr->u.syment._n._n_n._n_zeroes != 0)
1729 /* This is a "short" name. Make it long. */
1730 size_t i;
1731 char *newstring;
1733 /* Find the length of this string without walking into memory
1734 that isn't ours. */
1735 for (i = 0; i < 8; ++i)
1736 if (internal_ptr->u.syment._n._n_name[i] == '\0')
1737 break;
1739 newstring = bfd_zalloc (abfd, (bfd_size_type) (i + 1));
1740 if (newstring == NULL)
1741 return NULL;
1742 strncpy (newstring, internal_ptr->u.syment._n._n_name, i);
1743 internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t) newstring;
1744 internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1746 else if (internal_ptr->u.syment._n._n_n._n_offset == 0)
1747 internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t) "";
1748 else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment))
1750 /* Long name already. Point symbol at the string in the
1751 table. */
1752 if (string_table == NULL)
1754 string_table = _bfd_coff_read_string_table (abfd);
1755 if (string_table == NULL)
1756 return NULL;
1758 internal_ptr->u.syment._n._n_n._n_offset =
1759 ((bfd_hostptr_t)
1760 (string_table
1761 + internal_ptr->u.syment._n._n_n._n_offset));
1763 else
1765 /* Long name in debug section. Very similar. */
1766 if (debug_section == NULL)
1767 debug_section = build_debug_section (abfd);
1768 internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t)
1769 (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
1772 internal_ptr += internal_ptr->u.syment.n_numaux;
1775 obj_raw_syments (abfd) = internal;
1776 BFD_ASSERT (obj_raw_syment_count (abfd)
1777 == (unsigned int) (internal_ptr - internal));
1779 return internal;
1782 long
1783 coff_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
1785 if (bfd_get_format (abfd) != bfd_object)
1787 bfd_set_error (bfd_error_invalid_operation);
1788 return -1;
1790 return (asect->reloc_count + 1) * sizeof (arelent *);
1793 asymbol *
1794 coff_make_empty_symbol (bfd *abfd)
1796 bfd_size_type amt = sizeof (coff_symbol_type);
1797 coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_zalloc (abfd, amt);
1799 if (new_symbol == NULL)
1800 return NULL;
1801 new_symbol->symbol.section = 0;
1802 new_symbol->native = 0;
1803 new_symbol->lineno = NULL;
1804 new_symbol->done_lineno = FALSE;
1805 new_symbol->symbol.the_bfd = abfd;
1807 return & new_symbol->symbol;
1810 /* Make a debugging symbol. */
1812 asymbol *
1813 coff_bfd_make_debug_symbol (bfd *abfd,
1814 void * ptr ATTRIBUTE_UNUSED,
1815 unsigned long sz ATTRIBUTE_UNUSED)
1817 bfd_size_type amt = sizeof (coff_symbol_type);
1818 coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_alloc (abfd, amt);
1820 if (new_symbol == NULL)
1821 return NULL;
1822 /* @@ The 10 is a guess at a plausible maximum number of aux entries
1823 (but shouldn't be a constant). */
1824 amt = sizeof (combined_entry_type) * 10;
1825 new_symbol->native = (combined_entry_type *) bfd_zalloc (abfd, amt);
1826 if (!new_symbol->native)
1827 return NULL;
1828 new_symbol->symbol.section = bfd_abs_section_ptr;
1829 new_symbol->symbol.flags = BSF_DEBUGGING;
1830 new_symbol->lineno = NULL;
1831 new_symbol->done_lineno = FALSE;
1832 new_symbol->symbol.the_bfd = abfd;
1834 return & new_symbol->symbol;
1837 void
1838 coff_get_symbol_info (bfd *abfd, asymbol *symbol, symbol_info *ret)
1840 bfd_symbol_info (symbol, ret);
1842 if (coffsymbol (symbol)->native != NULL
1843 && coffsymbol (symbol)->native->fix_value)
1844 ret->value = coffsymbol (symbol)->native->u.syment.n_value -
1845 (bfd_hostptr_t) obj_raw_syments (abfd);
1848 /* Return the COFF syment for a symbol. */
1850 bfd_boolean
1851 bfd_coff_get_syment (bfd *abfd,
1852 asymbol *symbol,
1853 struct internal_syment *psyment)
1855 coff_symbol_type *csym;
1857 csym = coff_symbol_from (abfd, symbol);
1858 if (csym == NULL || csym->native == NULL)
1860 bfd_set_error (bfd_error_invalid_operation);
1861 return FALSE;
1864 *psyment = csym->native->u.syment;
1866 if (csym->native->fix_value)
1867 psyment->n_value = psyment->n_value -
1868 (bfd_hostptr_t) obj_raw_syments (abfd);
1870 /* FIXME: We should handle fix_line here. */
1872 return TRUE;
1875 /* Return the COFF auxent for a symbol. */
1877 bfd_boolean
1878 bfd_coff_get_auxent (bfd *abfd,
1879 asymbol *symbol,
1880 int indx,
1881 union internal_auxent *pauxent)
1883 coff_symbol_type *csym;
1884 combined_entry_type *ent;
1886 csym = coff_symbol_from (abfd, symbol);
1888 if (csym == NULL
1889 || csym->native == NULL
1890 || indx >= csym->native->u.syment.n_numaux)
1892 bfd_set_error (bfd_error_invalid_operation);
1893 return FALSE;
1896 ent = csym->native + indx + 1;
1898 *pauxent = ent->u.auxent;
1900 if (ent->fix_tag)
1901 pauxent->x_sym.x_tagndx.l =
1902 ((combined_entry_type *) pauxent->x_sym.x_tagndx.p
1903 - obj_raw_syments (abfd));
1905 if (ent->fix_end)
1906 pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l =
1907 ((combined_entry_type *) pauxent->x_sym.x_fcnary.x_fcn.x_endndx.p
1908 - obj_raw_syments (abfd));
1910 if (ent->fix_scnlen)
1911 pauxent->x_csect.x_scnlen.l =
1912 ((combined_entry_type *) pauxent->x_csect.x_scnlen.p
1913 - obj_raw_syments (abfd));
1915 return TRUE;
1918 /* Print out information about COFF symbol. */
1920 void
1921 coff_print_symbol (bfd *abfd,
1922 void * filep,
1923 asymbol *symbol,
1924 bfd_print_symbol_type how)
1926 FILE * file = (FILE *) filep;
1928 switch (how)
1930 case bfd_print_symbol_name:
1931 fprintf (file, "%s", symbol->name);
1932 break;
1934 case bfd_print_symbol_more:
1935 fprintf (file, "coff %s %s",
1936 coffsymbol (symbol)->native ? "n" : "g",
1937 coffsymbol (symbol)->lineno ? "l" : " ");
1938 break;
1940 case bfd_print_symbol_all:
1941 if (coffsymbol (symbol)->native)
1943 bfd_vma val;
1944 unsigned int aux;
1945 combined_entry_type *combined = coffsymbol (symbol)->native;
1946 combined_entry_type *root = obj_raw_syments (abfd);
1947 struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
1949 fprintf (file, "[%3ld]", (long) (combined - root));
1951 if (! combined->fix_value)
1952 val = (bfd_vma) combined->u.syment.n_value;
1953 else
1954 val = combined->u.syment.n_value - (bfd_hostptr_t) root;
1956 fprintf (file, "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x",
1957 combined->u.syment.n_scnum,
1958 combined->u.syment.n_flags,
1959 combined->u.syment.n_type,
1960 combined->u.syment.n_sclass,
1961 combined->u.syment.n_numaux);
1962 bfd_fprintf_vma (abfd, file, val);
1963 fprintf (file, " %s", symbol->name);
1965 for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
1967 combined_entry_type *auxp = combined + aux + 1;
1968 long tagndx;
1970 if (auxp->fix_tag)
1971 tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
1972 else
1973 tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
1975 fprintf (file, "\n");
1977 if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux))
1978 continue;
1980 switch (combined->u.syment.n_sclass)
1982 case C_FILE:
1983 fprintf (file, "File ");
1984 break;
1986 case C_STAT:
1987 if (combined->u.syment.n_type == T_NULL)
1988 /* Probably a section symbol ? */
1990 fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
1991 (unsigned long) auxp->u.auxent.x_scn.x_scnlen,
1992 auxp->u.auxent.x_scn.x_nreloc,
1993 auxp->u.auxent.x_scn.x_nlinno);
1994 if (auxp->u.auxent.x_scn.x_checksum != 0
1995 || auxp->u.auxent.x_scn.x_associated != 0
1996 || auxp->u.auxent.x_scn.x_comdat != 0)
1997 fprintf (file, " checksum 0x%lx assoc %d comdat %d",
1998 auxp->u.auxent.x_scn.x_checksum,
1999 auxp->u.auxent.x_scn.x_associated,
2000 auxp->u.auxent.x_scn.x_comdat);
2001 break;
2003 /* Otherwise fall through. */
2004 case C_EXT:
2005 case C_AIX_WEAKEXT:
2006 if (ISFCN (combined->u.syment.n_type))
2008 long next, llnos;
2010 if (auxp->fix_end)
2011 next = (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2012 - root);
2013 else
2014 next = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
2015 llnos = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_lnnoptr;
2016 fprintf (file,
2017 "AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld",
2018 tagndx,
2019 (unsigned long) auxp->u.auxent.x_sym.x_misc.x_fsize,
2020 llnos, next);
2021 break;
2023 /* Otherwise fall through. */
2024 default:
2025 fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
2026 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
2027 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
2028 tagndx);
2029 if (auxp->fix_end)
2030 fprintf (file, " endndx %ld",
2031 ((long)
2032 (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2033 - root)));
2034 break;
2038 if (l)
2040 fprintf (file, "\n%s :", l->u.sym->name);
2041 l++;
2042 while (l->line_number)
2044 fprintf (file, "\n%4d : ", l->line_number);
2045 bfd_fprintf_vma (abfd, file, l->u.offset + symbol->section->vma);
2046 l++;
2050 else
2052 bfd_print_symbol_vandf (abfd, (void *) file, symbol);
2053 fprintf (file, " %-5s %s %s %s",
2054 symbol->section->name,
2055 coffsymbol (symbol)->native ? "n" : "g",
2056 coffsymbol (symbol)->lineno ? "l" : " ",
2057 symbol->name);
2062 /* Return whether a symbol name implies a local symbol. In COFF,
2063 local symbols generally start with ``.L''. Most targets use this
2064 function for the is_local_label_name entry point, but some may
2065 override it. */
2067 bfd_boolean
2068 _bfd_coff_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
2069 const char *name)
2071 return name[0] == '.' && name[1] == 'L';
2074 /* Provided a BFD, a section and an offset (in bytes, not octets) into the
2075 section, calculate and return the name of the source file and the line
2076 nearest to the wanted location. */
2078 bfd_boolean
2079 coff_find_nearest_line (bfd *abfd,
2080 asection *section,
2081 asymbol **symbols,
2082 bfd_vma offset,
2083 const char **filename_ptr,
2084 const char **functionname_ptr,
2085 unsigned int *line_ptr)
2087 bfd_boolean found;
2088 unsigned int i;
2089 unsigned int line_base;
2090 coff_data_type *cof = coff_data (abfd);
2091 /* Run through the raw syments if available. */
2092 combined_entry_type *p;
2093 combined_entry_type *pend;
2094 alent *l;
2095 struct coff_section_tdata *sec_data;
2096 bfd_size_type amt;
2098 /* Before looking through the symbol table, try to use a .stab
2099 section to find the information. */
2100 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
2101 &found, filename_ptr,
2102 functionname_ptr, line_ptr,
2103 &coff_data(abfd)->line_info))
2104 return FALSE;
2106 if (found)
2107 return TRUE;
2109 /* Also try examining DWARF2 debugging information. */
2110 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
2111 filename_ptr, functionname_ptr,
2112 line_ptr, 0,
2113 &coff_data(abfd)->dwarf2_find_line_info))
2114 return TRUE;
2116 *filename_ptr = 0;
2117 *functionname_ptr = 0;
2118 *line_ptr = 0;
2120 /* Don't try and find line numbers in a non coff file. */
2121 if (!bfd_family_coff (abfd))
2122 return FALSE;
2124 if (cof == NULL)
2125 return FALSE;
2127 /* Find the first C_FILE symbol. */
2128 p = cof->raw_syments;
2129 if (!p)
2130 return FALSE;
2132 pend = p + cof->raw_syment_count;
2133 while (p < pend)
2135 if (p->u.syment.n_sclass == C_FILE)
2136 break;
2137 p += 1 + p->u.syment.n_numaux;
2140 if (p < pend)
2142 bfd_vma sec_vma;
2143 bfd_vma maxdiff;
2145 /* Look through the C_FILE symbols to find the best one. */
2146 sec_vma = bfd_get_section_vma (abfd, section);
2147 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2148 maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
2149 while (1)
2151 combined_entry_type *p2;
2153 for (p2 = p + 1 + p->u.syment.n_numaux;
2154 p2 < pend;
2155 p2 += 1 + p2->u.syment.n_numaux)
2157 if (p2->u.syment.n_scnum > 0
2158 && (section
2159 == coff_section_from_bfd_index (abfd,
2160 p2->u.syment.n_scnum)))
2161 break;
2162 if (p2->u.syment.n_sclass == C_FILE)
2164 p2 = pend;
2165 break;
2169 /* We use <= MAXDIFF here so that if we get a zero length
2170 file, we actually use the next file entry. */
2171 if (p2 < pend
2172 && offset + sec_vma >= (bfd_vma) p2->u.syment.n_value
2173 && offset + sec_vma - (bfd_vma) p2->u.syment.n_value <= maxdiff)
2175 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2176 maxdiff = offset + sec_vma - p2->u.syment.n_value;
2179 /* Avoid endless loops on erroneous files by ensuring that
2180 we always move forward in the file. */
2181 if (p >= cof->raw_syments + p->u.syment.n_value)
2182 break;
2184 p = cof->raw_syments + p->u.syment.n_value;
2185 if (p > pend || p->u.syment.n_sclass != C_FILE)
2186 break;
2190 /* Now wander though the raw linenumbers of the section. */
2191 /* If we have been called on this section before, and the offset we
2192 want is further down then we can prime the lookup loop. */
2193 sec_data = coff_section_data (abfd, section);
2194 if (sec_data != NULL
2195 && sec_data->i > 0
2196 && offset >= sec_data->offset)
2198 i = sec_data->i;
2199 *functionname_ptr = sec_data->function;
2200 line_base = sec_data->line_base;
2202 else
2204 i = 0;
2205 line_base = 0;
2208 if (section->lineno != NULL)
2210 bfd_vma last_value = 0;
2212 l = &section->lineno[i];
2214 for (; i < section->lineno_count; i++)
2216 if (l->line_number == 0)
2218 /* Get the symbol this line number points at. */
2219 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
2220 if (coff->symbol.value > offset)
2221 break;
2222 *functionname_ptr = coff->symbol.name;
2223 last_value = coff->symbol.value;
2224 if (coff->native)
2226 combined_entry_type *s = coff->native;
2227 s = s + 1 + s->u.syment.n_numaux;
2229 /* In XCOFF a debugging symbol can follow the
2230 function symbol. */
2231 if (s->u.syment.n_scnum == N_DEBUG)
2232 s = s + 1 + s->u.syment.n_numaux;
2234 /* S should now point to the .bf of the function. */
2235 if (s->u.syment.n_numaux)
2237 /* The linenumber is stored in the auxent. */
2238 union internal_auxent *a = &((s + 1)->u.auxent);
2239 line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
2240 *line_ptr = line_base;
2244 else
2246 if (l->u.offset > offset)
2247 break;
2248 *line_ptr = l->line_number + line_base - 1;
2250 l++;
2253 /* If we fell off the end of the loop, then assume that this
2254 symbol has no line number info. Otherwise, symbols with no
2255 line number info get reported with the line number of the
2256 last line of the last symbol which does have line number
2257 info. We use 0x100 as a slop to account for cases where the
2258 last line has executable code. */
2259 if (i >= section->lineno_count
2260 && last_value != 0
2261 && offset - last_value > 0x100)
2263 *functionname_ptr = NULL;
2264 *line_ptr = 0;
2268 /* Cache the results for the next call. */
2269 if (sec_data == NULL && section->owner == abfd)
2271 amt = sizeof (struct coff_section_tdata);
2272 section->used_by_bfd = bfd_zalloc (abfd, amt);
2273 sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2275 if (sec_data != NULL)
2277 sec_data->offset = offset;
2278 sec_data->i = i - 1;
2279 sec_data->function = *functionname_ptr;
2280 sec_data->line_base = line_base;
2283 return TRUE;
2286 bfd_boolean
2287 coff_find_inliner_info (bfd *abfd,
2288 const char **filename_ptr,
2289 const char **functionname_ptr,
2290 unsigned int *line_ptr)
2292 bfd_boolean found;
2294 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
2295 functionname_ptr, line_ptr,
2296 &coff_data(abfd)->dwarf2_find_line_info);
2297 return (found);
2301 coff_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
2303 size_t size;
2305 if (!info->relocatable)
2306 size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
2307 else
2308 size = bfd_coff_filhsz (abfd);
2310 size += abfd->section_count * bfd_coff_scnhsz (abfd);
2311 return size;
2314 /* Change the class of a coff symbol held by BFD. */
2316 bfd_boolean
2317 bfd_coff_set_symbol_class (bfd * abfd,
2318 asymbol * symbol,
2319 unsigned int class)
2321 coff_symbol_type * csym;
2323 csym = coff_symbol_from (abfd, symbol);
2324 if (csym == NULL)
2326 bfd_set_error (bfd_error_invalid_operation);
2327 return FALSE;
2329 else if (csym->native == NULL)
2331 /* This is an alien symbol which no native coff backend data.
2332 We cheat here by creating a fake native entry for it and
2333 then filling in the class. This code is based on that in
2334 coff_write_alien_symbol(). */
2336 combined_entry_type * native;
2337 bfd_size_type amt = sizeof (* native);
2339 native = bfd_zalloc (abfd, amt);
2340 if (native == NULL)
2341 return FALSE;
2343 native->u.syment.n_type = T_NULL;
2344 native->u.syment.n_sclass = class;
2346 if (bfd_is_und_section (symbol->section))
2348 native->u.syment.n_scnum = N_UNDEF;
2349 native->u.syment.n_value = symbol->value;
2351 else if (bfd_is_com_section (symbol->section))
2353 native->u.syment.n_scnum = N_UNDEF;
2354 native->u.syment.n_value = symbol->value;
2356 else
2358 native->u.syment.n_scnum =
2359 symbol->section->output_section->target_index;
2360 native->u.syment.n_value = (symbol->value
2361 + symbol->section->output_offset);
2362 if (! obj_pe (abfd))
2363 native->u.syment.n_value += symbol->section->output_section->vma;
2365 /* Copy the any flags from the file header into the symbol.
2366 FIXME: Why? */
2367 native->u.syment.n_flags = bfd_asymbol_bfd (& csym->symbol)->flags;
2370 csym->native = native;
2372 else
2373 csym->native->u.syment.n_sclass = class;
2375 return TRUE;
2378 struct coff_comdat_info *
2379 bfd_coff_get_comdat_section (bfd *abfd, struct bfd_section *sec)
2381 if (bfd_get_flavour (abfd) == bfd_target_coff_flavour
2382 && coff_section_data (abfd, sec) != NULL)
2383 return coff_section_data (abfd, sec)->comdat;
2384 else
2385 return NULL;