2001-02-17 Philip Blundell <philb@gnu.org>
[binutils.git] / bfd / coffgen.c
blob35e8f718e6a36396d2ff81ba173f0696fdf4c6ba
1 /* Support for the generic parts of COFF, for BFD.
2 Copyright 1990, 91, 92, 93, 94, 95, 96, 97, 98, 1999, 2000
3 Free Software Foundation, Inc.
4 Written by Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 /* Most of this hacked by Steve Chamberlain, sac@cygnus.com.
23 Split out of coffcode.h by Ian Taylor, ian@cygnus.com. */
25 /* This file contains COFF code that is not dependent on any
26 particular COFF target. There is only one version of this file in
27 libbfd.a, so no target specific code may be put in here. Or, to
28 put it another way,
30 ********** DO NOT PUT TARGET SPECIFIC CODE IN THIS FILE **********
32 If you need to add some target specific behaviour, add a new hook
33 function to bfd_coff_backend_data.
35 Some of these functions are also called by the ECOFF routines.
36 Those functions may not use any COFF specific information, such as
37 coff_data (abfd). */
39 #include "bfd.h"
40 #include "sysdep.h"
41 #include "libbfd.h"
42 #include "coff/internal.h"
43 #include "libcoff.h"
45 static void coff_fix_symbol_name
46 PARAMS ((bfd *, asymbol *, combined_entry_type *, bfd_size_type *,
47 asection **, bfd_size_type *));
48 static boolean coff_write_symbol
49 PARAMS ((bfd *, asymbol *, combined_entry_type *, unsigned int *,
50 bfd_size_type *, asection **, bfd_size_type *));
51 static boolean coff_write_alien_symbol
52 PARAMS ((bfd *, asymbol *, unsigned int *, bfd_size_type *,
53 asection **, bfd_size_type *));
54 static boolean coff_write_native_symbol
55 PARAMS ((bfd *, coff_symbol_type *, unsigned int *, bfd_size_type *,
56 asection **, bfd_size_type *));
57 static void coff_pointerize_aux
58 PARAMS ((bfd *, combined_entry_type *, combined_entry_type *,
59 unsigned int, combined_entry_type *));
60 static boolean make_a_section_from_file
61 PARAMS ((bfd *, struct internal_scnhdr *, unsigned int));
62 static const bfd_target *coff_real_object_p
63 PARAMS ((bfd *, unsigned, struct internal_filehdr *,
64 struct internal_aouthdr *));
65 static void fixup_symbol_value
66 PARAMS ((bfd *, coff_symbol_type *, struct internal_syment *));
67 static char *build_debug_section
68 PARAMS ((bfd *));
69 static char *copy_name
70 PARAMS ((bfd *, char *, int));
72 #define STRING_SIZE_SIZE (4)
74 /* Take a section header read from a coff file (in HOST byte order),
75 and make a BFD "section" out of it. This is used by ECOFF. */
76 static boolean
77 make_a_section_from_file (abfd, hdr, target_index)
78 bfd *abfd;
79 struct internal_scnhdr *hdr;
80 unsigned int target_index;
82 asection *return_section;
83 char *name;
85 name = NULL;
87 /* Handle long section names as in PE. */
88 if (bfd_coff_long_section_names (abfd)
89 && hdr->s_name[0] == '/')
91 char buf[SCNNMLEN];
92 long strindex;
93 char *p;
94 const char *strings;
96 memcpy (buf, hdr->s_name + 1, SCNNMLEN - 1);
97 buf[SCNNMLEN - 1] = '\0';
98 strindex = strtol (buf, &p, 10);
99 if (*p == '\0' && strindex >= 0)
101 strings = _bfd_coff_read_string_table (abfd);
102 if (strings == NULL)
103 return false;
104 /* FIXME: For extra safety, we should make sure that
105 strindex does not run us past the end, but right now we
106 don't know the length of the string table. */
107 strings += strindex;
108 name = bfd_alloc (abfd, strlen (strings) + 1);
109 if (name == NULL)
110 return false;
111 strcpy (name, strings);
115 if (name == NULL)
117 /* Assorted wastage to null-terminate the name, thanks AT&T! */
118 name = bfd_alloc (abfd, sizeof (hdr->s_name) + 1);
119 if (name == NULL)
120 return false;
121 strncpy (name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
122 name[sizeof (hdr->s_name)] = 0;
125 return_section = bfd_make_section_anyway (abfd, name);
126 if (return_section == NULL)
127 return false;
129 return_section->vma = hdr->s_vaddr;
130 return_section->lma = hdr->s_paddr;
131 return_section->_raw_size = hdr->s_size;
132 return_section->filepos = hdr->s_scnptr;
133 return_section->rel_filepos = hdr->s_relptr;
134 return_section->reloc_count = hdr->s_nreloc;
136 bfd_coff_set_alignment_hook (abfd, return_section, hdr);
138 return_section->line_filepos = hdr->s_lnnoptr;
140 return_section->lineno_count = hdr->s_nlnno;
141 return_section->userdata = NULL;
142 return_section->next = (asection *) NULL;
143 return_section->target_index = target_index;
144 return_section->flags = bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name,
145 return_section);
147 /* At least on i386-coff, the line number count for a shared library
148 section must be ignored. */
149 if ((return_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
150 return_section->lineno_count = 0;
152 if (hdr->s_nreloc != 0)
153 return_section->flags |= SEC_RELOC;
154 /* FIXME: should this check 'hdr->s_size > 0' */
155 if (hdr->s_scnptr != 0)
156 return_section->flags |= SEC_HAS_CONTENTS;
157 return true;
160 /* Read in a COFF object and make it into a BFD. This is used by
161 ECOFF as well. */
163 static const bfd_target *
164 coff_real_object_p (abfd, nscns, internal_f, internal_a)
165 bfd *abfd;
166 unsigned nscns;
167 struct internal_filehdr *internal_f;
168 struct internal_aouthdr *internal_a;
170 flagword oflags = abfd->flags;
171 bfd_vma ostart = bfd_get_start_address (abfd);
172 PTR tdata;
173 size_t readsize; /* length of file_info */
174 unsigned int scnhsz;
175 char *external_sections;
177 if (!(internal_f->f_flags & F_RELFLG))
178 abfd->flags |= HAS_RELOC;
179 if ((internal_f->f_flags & F_EXEC))
180 abfd->flags |= EXEC_P;
181 if (!(internal_f->f_flags & F_LNNO))
182 abfd->flags |= HAS_LINENO;
183 if (!(internal_f->f_flags & F_LSYMS))
184 abfd->flags |= HAS_LOCALS;
186 /* FIXME: How can we set D_PAGED correctly? */
187 if ((internal_f->f_flags & F_EXEC) != 0)
188 abfd->flags |= D_PAGED;
190 bfd_get_symcount (abfd) = internal_f->f_nsyms;
191 if (internal_f->f_nsyms)
192 abfd->flags |= HAS_SYMS;
194 if (internal_a != (struct internal_aouthdr *) NULL)
195 bfd_get_start_address (abfd) = internal_a->entry;
196 else
197 bfd_get_start_address (abfd) = 0;
199 /* Set up the tdata area. ECOFF uses its own routine, and overrides
200 abfd->flags. */
201 tdata = bfd_coff_mkobject_hook (abfd, (PTR) internal_f, (PTR) internal_a);
202 if (tdata == NULL)
203 return 0;
205 scnhsz = bfd_coff_scnhsz (abfd);
206 readsize = nscns * scnhsz;
207 external_sections = (char *) bfd_alloc (abfd, readsize);
208 if (!external_sections)
209 goto fail;
211 if (bfd_read ((PTR) external_sections, 1, readsize, abfd) != readsize)
212 goto fail;
214 /* Set the arch/mach *before* swapping in sections; section header swapping
215 may depend on arch/mach info. */
216 if (bfd_coff_set_arch_mach_hook (abfd, (PTR) internal_f) == false)
217 goto fail;
219 /* Now copy data as required; construct all asections etc */
220 if (nscns != 0)
222 unsigned int i;
223 for (i = 0; i < nscns; i++)
225 struct internal_scnhdr tmp;
226 bfd_coff_swap_scnhdr_in (abfd,
227 (PTR) (external_sections + i * scnhsz),
228 (PTR) & tmp);
229 if (! make_a_section_from_file (abfd, &tmp, i + 1))
230 goto fail;
234 /* make_abs_section (abfd); */
236 return abfd->xvec;
238 fail:
239 bfd_release (abfd, tdata);
240 abfd->flags = oflags;
241 bfd_get_start_address (abfd) = ostart;
242 return (const bfd_target *) NULL;
245 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
246 not a COFF file. This is also used by ECOFF. */
248 const bfd_target *
249 coff_object_p (abfd)
250 bfd *abfd;
252 unsigned int filhsz;
253 unsigned int aoutsz;
254 int nscns;
255 PTR filehdr;
256 struct internal_filehdr internal_f;
257 struct internal_aouthdr internal_a;
259 /* figure out how much to read */
260 filhsz = bfd_coff_filhsz (abfd);
261 aoutsz = bfd_coff_aoutsz (abfd);
263 filehdr = bfd_alloc (abfd, filhsz);
264 if (filehdr == NULL)
265 return 0;
266 if (bfd_read (filehdr, 1, filhsz, abfd) != filhsz)
268 if (bfd_get_error () != bfd_error_system_call)
269 bfd_set_error (bfd_error_wrong_format);
270 return 0;
272 bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f);
273 bfd_release (abfd, filehdr);
275 if (bfd_coff_bad_format_hook (abfd, &internal_f) == false)
277 bfd_set_error (bfd_error_wrong_format);
278 return 0;
280 nscns = internal_f.f_nscns;
282 if (internal_f.f_opthdr)
284 PTR opthdr;
286 opthdr = bfd_alloc (abfd, aoutsz);
287 if (opthdr == NULL)
288 return 0;;
289 if (bfd_read (opthdr, 1, internal_f.f_opthdr, abfd)
290 != internal_f.f_opthdr)
292 return 0;
294 bfd_coff_swap_aouthdr_in (abfd, opthdr, (PTR) &internal_a);
297 return coff_real_object_p (abfd, nscns, &internal_f,
298 (internal_f.f_opthdr != 0
299 ? &internal_a
300 : (struct internal_aouthdr *) NULL));
303 /* Get the BFD section from a COFF symbol section number. */
305 asection *
306 coff_section_from_bfd_index (abfd, index)
307 bfd *abfd;
308 int index;
310 struct sec *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 (abfd)
335 bfd *abfd;
337 if (!bfd_coff_slurp_symbol_table (abfd))
338 return -1;
340 return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *));
343 /* Canonicalize a COFF symbol table. */
345 long
346 coff_get_symtab (abfd, alocation)
347 bfd *abfd;
348 asymbol **alocation;
350 unsigned int counter;
351 coff_symbol_type *symbase;
352 coff_symbol_type **location = (coff_symbol_type **) alocation;
354 if (!bfd_coff_slurp_symbol_table (abfd))
355 return -1;
357 symbase = obj_symbols (abfd);
358 counter = bfd_get_symcount (abfd);
359 while (counter-- > 0)
360 *location++ = symbase++;
362 *location = NULL;
364 return bfd_get_symcount (abfd);
367 /* Get the name of a symbol. The caller must pass in a buffer of size
368 >= SYMNMLEN + 1. */
370 const char *
371 _bfd_coff_internal_syment_name (abfd, sym, buf)
372 bfd *abfd;
373 const struct internal_syment *sym;
374 char *buf;
376 /* FIXME: It's not clear this will work correctly if sizeof
377 (_n_zeroes) != 4. */
378 if (sym->_n._n_n._n_zeroes != 0
379 || sym->_n._n_n._n_offset == 0)
381 memcpy (buf, sym->_n._n_name, SYMNMLEN);
382 buf[SYMNMLEN] = '\0';
383 return buf;
385 else
387 const char *strings;
389 BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE);
390 strings = obj_coff_strings (abfd);
391 if (strings == NULL)
393 strings = _bfd_coff_read_string_table (abfd);
394 if (strings == NULL)
395 return NULL;
397 return strings + sym->_n._n_n._n_offset;
401 /* Read in and swap the relocs. This returns a buffer holding the
402 relocs for section SEC in file ABFD. If CACHE is true and
403 INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
404 the function is called again. If EXTERNAL_RELOCS is not NULL, it
405 is a buffer large enough to hold the unswapped relocs. If
406 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
407 the swapped relocs. If REQUIRE_INTERNAL is true, then the return
408 value must be INTERNAL_RELOCS. The function returns NULL on error. */
410 struct internal_reloc *
411 _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
412 require_internal, internal_relocs)
413 bfd *abfd;
414 asection *sec;
415 boolean cache;
416 bfd_byte *external_relocs;
417 boolean require_internal;
418 struct internal_reloc *internal_relocs;
420 bfd_size_type relsz;
421 bfd_byte *free_external = NULL;
422 struct internal_reloc *free_internal = NULL;
423 bfd_byte *erel;
424 bfd_byte *erel_end;
425 struct internal_reloc *irel;
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 if (external_relocs == NULL)
441 free_external = (bfd_byte *) bfd_malloc (sec->reloc_count * relsz);
442 if (free_external == NULL && sec->reloc_count > 0)
443 goto error_return;
444 external_relocs = free_external;
447 if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
448 || (bfd_read (external_relocs, relsz, sec->reloc_count, abfd)
449 != relsz * sec->reloc_count))
450 goto error_return;
452 if (internal_relocs == NULL)
454 free_internal = ((struct internal_reloc *)
455 bfd_malloc (sec->reloc_count
456 * sizeof (struct internal_reloc)));
457 if (free_internal == NULL && sec->reloc_count > 0)
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, (PTR) erel, (PTR) 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 sec->used_by_bfd =
480 (PTR) bfd_zalloc (abfd,
481 sizeof (struct coff_section_tdata));
482 if (sec->used_by_bfd == NULL)
483 goto error_return;
484 coff_section_data (abfd, sec)->contents = NULL;
486 coff_section_data (abfd, sec)->relocs = free_internal;
489 return internal_relocs;
491 error_return:
492 if (free_external != NULL)
493 free (free_external);
494 if (free_internal != NULL)
495 free (free_internal);
496 return NULL;
499 /* Set lineno_count for the output sections of a COFF file. */
502 coff_count_linenumbers (abfd)
503 bfd *abfd;
505 unsigned int limit = bfd_get_symcount (abfd);
506 unsigned int i;
507 int total = 0;
508 asymbol **p;
509 asection *s;
511 if (limit == 0)
513 /* This may be from the backend linker, in which case the
514 lineno_count in the sections is correct. */
515 for (s = abfd->sections; s != NULL; s = s->next)
516 total += s->lineno_count;
517 return total;
520 for (s = abfd->sections; s != NULL; s = s->next)
521 BFD_ASSERT (s->lineno_count == 0);
523 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
525 asymbol *q_maybe = *p;
527 if (bfd_family_coff (bfd_asymbol_bfd (q_maybe)))
529 coff_symbol_type *q = coffsymbol (q_maybe);
531 /* The AIX 4.1 compiler can sometimes generate line numbers
532 attached to debugging symbols. We try to simply ignore
533 those here. */
534 if (q->lineno != NULL
535 && q->symbol.section->owner != NULL)
537 /* This symbol has line numbers. Increment the owning
538 section's linenumber count. */
539 alent *l = q->lineno;
541 ++q->symbol.section->output_section->lineno_count;
542 ++total;
543 ++l;
544 while (l->line_number != 0)
546 ++total;
547 ++q->symbol.section->output_section->lineno_count;
548 ++l;
554 return total;
557 /* Takes a bfd and a symbol, returns a pointer to the coff specific
558 area of the symbol if there is one. */
560 coff_symbol_type *
561 coff_symbol_from (ignore_abfd, symbol)
562 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 (abfd, coff_symbol_ptr, syment)
576 bfd *abfd;
577 coff_symbol_type *coff_symbol_ptr;
578 struct internal_syment *syment;
581 /* Normalize the symbol flags */
582 if (bfd_is_com_section (coff_symbol_ptr->symbol.section))
584 /* a common symbol is undefined with a value */
585 syment->n_scnum = N_UNDEF;
586 syment->n_value = coff_symbol_ptr->symbol.value;
588 else if ((coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) != 0
589 && (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING_RELOC) == 0)
591 syment->n_value = coff_symbol_ptr->symbol.value;
593 else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
595 syment->n_scnum = N_UNDEF;
596 syment->n_value = 0;
598 /* FIXME: Do we need to handle the absolute section here? */
599 else
601 if (coff_symbol_ptr->symbol.section)
603 syment->n_scnum =
604 coff_symbol_ptr->symbol.section->output_section->target_index;
606 syment->n_value = (coff_symbol_ptr->symbol.value
607 + coff_symbol_ptr->symbol.section->output_offset);
608 if (! obj_pe (abfd))
610 syment->n_value += (syment->n_sclass == C_STATLAB)
611 ? coff_symbol_ptr->symbol.section->output_section->lma
612 : coff_symbol_ptr->symbol.section->output_section->vma;
615 else
617 BFD_ASSERT (0);
618 /* This can happen, but I don't know why yet (steve@cygnus.com) */
619 syment->n_scnum = N_ABS;
620 syment->n_value = coff_symbol_ptr->symbol.value;
625 /* Run through all the symbols in the symbol table and work out what
626 their indexes into the symbol table will be when output.
628 Coff requires that each C_FILE symbol points to the next one in the
629 chain, and that the last one points to the first external symbol. We
630 do that here too. */
632 boolean
633 coff_renumber_symbols (bfd_ptr, first_undef)
634 bfd *bfd_ptr;
635 int *first_undef;
637 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
638 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
639 unsigned int native_index = 0;
640 struct internal_syment *last_file = (struct internal_syment *) NULL;
641 unsigned int symbol_index;
643 /* COFF demands that undefined symbols come after all other symbols.
644 Since we don't need to impose this extra knowledge on all our
645 client programs, deal with that here. Sort the symbol table;
646 just move the undefined symbols to the end, leaving the rest
647 alone. The O'Reilly book says that defined global symbols come
648 at the end before the undefined symbols, so we do that here as
649 well. */
650 /* @@ Do we have some condition we could test for, so we don't always
651 have to do this? I don't think relocatability is quite right, but
652 I'm not certain. [raeburn:19920508.1711EST] */
654 asymbol **newsyms;
655 unsigned int i;
657 newsyms = (asymbol **) bfd_alloc (bfd_ptr,
658 sizeof (asymbol *) * (symbol_count + 1));
659 if (!newsyms)
660 return false;
661 bfd_ptr->outsymbols = newsyms;
662 for (i = 0; i < symbol_count; i++)
663 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
664 || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
665 && !bfd_is_com_section (symbol_ptr_ptr[i]->section)
666 && ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) != 0
667 || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
668 == 0))))
669 *newsyms++ = symbol_ptr_ptr[i];
671 for (i = 0; i < symbol_count; i++)
672 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
673 && !bfd_is_und_section (symbol_ptr_ptr[i]->section)
674 && (bfd_is_com_section (symbol_ptr_ptr[i]->section)
675 || ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) == 0
676 && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
677 != 0))))
678 *newsyms++ = symbol_ptr_ptr[i];
680 *first_undef = newsyms - bfd_ptr->outsymbols;
682 for (i = 0; i < symbol_count; i++)
683 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
684 && bfd_is_und_section (symbol_ptr_ptr[i]->section))
685 *newsyms++ = symbol_ptr_ptr[i];
686 *newsyms = (asymbol *) NULL;
687 symbol_ptr_ptr = bfd_ptr->outsymbols;
690 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
692 coff_symbol_type *coff_symbol_ptr = coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
693 symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
694 if (coff_symbol_ptr && coff_symbol_ptr->native)
696 combined_entry_type *s = coff_symbol_ptr->native;
697 int i;
699 if (s->u.syment.n_sclass == C_FILE)
701 if (last_file != (struct internal_syment *) NULL)
702 last_file->n_value = native_index;
703 last_file = &(s->u.syment);
705 else
708 /* Modify the symbol values according to their section and
709 type */
711 fixup_symbol_value (bfd_ptr, coff_symbol_ptr, &(s->u.syment));
713 for (i = 0; i < s->u.syment.n_numaux + 1; i++)
714 s[i].offset = native_index++;
716 else
718 native_index++;
721 obj_conv_table_size (bfd_ptr) = native_index;
723 return true;
726 /* Run thorough the symbol table again, and fix it so that all
727 pointers to entries are changed to the entries' index in the output
728 symbol table. */
730 void
731 coff_mangle_symbols (bfd_ptr)
732 bfd *bfd_ptr;
734 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
735 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
736 unsigned int symbol_index;
738 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
740 coff_symbol_type *coff_symbol_ptr =
741 coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
743 if (coff_symbol_ptr && coff_symbol_ptr->native)
745 int i;
746 combined_entry_type *s = coff_symbol_ptr->native;
748 if (s->fix_value)
750 /* FIXME: We should use a union here. */
751 s->u.syment.n_value =
752 ((combined_entry_type *) s->u.syment.n_value)->offset;
753 s->fix_value = 0;
755 if (s->fix_line)
757 /* The value is the offset into the line number entries
758 for the symbol's section. On output, the symbol's
759 section should be N_DEBUG. */
760 s->u.syment.n_value =
761 (coff_symbol_ptr->symbol.section->output_section->line_filepos
762 + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr));
763 coff_symbol_ptr->symbol.section =
764 coff_section_from_bfd_index (bfd_ptr, N_DEBUG);
765 BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
767 for (i = 0; i < s->u.syment.n_numaux; i++)
769 combined_entry_type *a = s + i + 1;
770 if (a->fix_tag)
772 a->u.auxent.x_sym.x_tagndx.l =
773 a->u.auxent.x_sym.x_tagndx.p->offset;
774 a->fix_tag = 0;
776 if (a->fix_end)
778 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
779 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
780 a->fix_end = 0;
782 if (a->fix_scnlen)
784 a->u.auxent.x_csect.x_scnlen.l =
785 a->u.auxent.x_csect.x_scnlen.p->offset;
786 a->fix_scnlen = 0;
793 static void
794 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
795 debug_string_section_p, debug_string_size_p)
796 bfd *abfd;
797 asymbol *symbol;
798 combined_entry_type *native;
799 bfd_size_type *string_size_p;
800 asection **debug_string_section_p;
801 bfd_size_type *debug_string_size_p;
803 unsigned int name_length;
804 union internal_auxent *auxent;
805 char *name = (char *) (symbol->name);
807 if (name == (char *) NULL)
809 /* coff symbols always have names, so we'll make one up */
810 symbol->name = "strange";
811 name = (char *) symbol->name;
813 name_length = strlen (name);
815 if (native->u.syment.n_sclass == C_FILE
816 && native->u.syment.n_numaux > 0)
818 unsigned int filnmlen;
820 if (bfd_coff_force_symnames_in_strings (abfd))
822 native->u.syment._n._n_n._n_offset =
823 (*string_size_p + STRING_SIZE_SIZE);
824 native->u.syment._n._n_n._n_zeroes = 0;
825 *string_size_p += 6; /* strlen(".file") + 1 */
827 else
828 strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
830 auxent = &(native + 1)->u.auxent;
832 filnmlen = bfd_coff_filnmlen (abfd);
834 if (bfd_coff_long_filenames (abfd))
836 if (name_length <= filnmlen)
838 strncpy (auxent->x_file.x_fname, name, filnmlen);
840 else
842 auxent->x_file.x_n.x_offset = *string_size_p + STRING_SIZE_SIZE;
843 auxent->x_file.x_n.x_zeroes = 0;
844 *string_size_p += name_length + 1;
847 else
849 strncpy (auxent->x_file.x_fname, name, filnmlen);
850 if (name_length > filnmlen)
851 name[filnmlen] = '\0';
854 else
856 if (name_length <= SYMNMLEN && !bfd_coff_force_symnames_in_strings (abfd))
858 /* This name will fit into the symbol neatly */
859 strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
861 else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
863 native->u.syment._n._n_n._n_offset = (*string_size_p
864 + STRING_SIZE_SIZE);
865 native->u.syment._n._n_n._n_zeroes = 0;
866 *string_size_p += name_length + 1;
868 else
870 long filepos;
871 bfd_byte buf[4];
872 int prefix_len = bfd_coff_debug_string_prefix_length (abfd);
874 /* This name should be written into the .debug section. For
875 some reason each name is preceded by a two byte length
876 and also followed by a null byte. FIXME: We assume that
877 the .debug section has already been created, and that it
878 is large enough. */
879 if (*debug_string_section_p == (asection *) NULL)
880 *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
881 filepos = bfd_tell (abfd);
882 if (prefix_len == 4)
883 bfd_put_32 (abfd, name_length + 1, buf);
884 else
885 bfd_put_16 (abfd, name_length + 1, buf);
887 if (!bfd_set_section_contents (abfd,
888 *debug_string_section_p,
889 (PTR) buf,
890 (file_ptr) *debug_string_size_p,
891 (bfd_size_type) prefix_len)
892 || !bfd_set_section_contents (abfd,
893 *debug_string_section_p,
894 (PTR) symbol->name,
895 ((file_ptr) *debug_string_size_p
896 + prefix_len),
897 (bfd_size_type) name_length + 1))
898 abort ();
899 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
900 abort ();
901 native->u.syment._n._n_n._n_offset =
902 *debug_string_size_p + prefix_len;
903 native->u.syment._n._n_n._n_zeroes = 0;
904 *debug_string_size_p += name_length + 1 + prefix_len;
909 /* We need to keep track of the symbol index so that when we write out
910 the relocs we can get the index for a symbol. This method is a
911 hack. FIXME. */
913 #define set_index(symbol, idx) ((symbol)->udata.i = (idx))
915 /* Write a symbol out to a COFF file. */
917 static boolean
918 coff_write_symbol (abfd, symbol, native, written, string_size_p,
919 debug_string_section_p, debug_string_size_p)
920 bfd *abfd;
921 asymbol *symbol;
922 combined_entry_type *native;
923 unsigned int *written;
924 bfd_size_type *string_size_p;
925 asection **debug_string_section_p;
926 bfd_size_type *debug_string_size_p;
928 unsigned int numaux = native->u.syment.n_numaux;
929 int type = native->u.syment.n_type;
930 int class = native->u.syment.n_sclass;
931 PTR buf;
932 bfd_size_type symesz;
934 if (native->u.syment.n_sclass == C_FILE)
935 symbol->flags |= BSF_DEBUGGING;
937 if (symbol->flags & BSF_DEBUGGING
938 && bfd_is_abs_section (symbol->section))
940 native->u.syment.n_scnum = N_DEBUG;
942 else if (bfd_is_abs_section (symbol->section))
944 native->u.syment.n_scnum = N_ABS;
946 else if (bfd_is_und_section (symbol->section))
948 native->u.syment.n_scnum = N_UNDEF;
950 else
952 native->u.syment.n_scnum =
953 symbol->section->output_section->target_index;
956 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
957 debug_string_section_p, debug_string_size_p);
959 symesz = bfd_coff_symesz (abfd);
960 buf = bfd_alloc (abfd, symesz);
961 if (!buf)
962 return false;
963 bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
964 if (bfd_write (buf, 1, symesz, abfd) != symesz)
965 return false;
966 bfd_release (abfd, buf);
968 if (native->u.syment.n_numaux > 0)
970 bfd_size_type auxesz;
971 unsigned int j;
973 auxesz = bfd_coff_auxesz (abfd);
974 buf = bfd_alloc (abfd, auxesz);
975 if (!buf)
976 return false;
977 for (j = 0; j < native->u.syment.n_numaux; j++)
979 bfd_coff_swap_aux_out (abfd,
980 &((native + j + 1)->u.auxent),
981 type,
982 class,
984 native->u.syment.n_numaux,
985 buf);
986 if (bfd_write (buf, 1, auxesz, abfd) != auxesz)
987 return false;
989 bfd_release (abfd, buf);
992 /* Store the index for use when we write out the relocs. */
993 set_index (symbol, *written);
995 *written += numaux + 1;
996 return true;
999 /* Write out a symbol to a COFF file that does not come from a COFF
1000 file originally. This symbol may have been created by the linker,
1001 or we may be linking a non COFF file to a COFF file. */
1003 static boolean
1004 coff_write_alien_symbol (abfd, symbol, written, string_size_p,
1005 debug_string_section_p, debug_string_size_p)
1006 bfd *abfd;
1007 asymbol *symbol;
1008 unsigned int *written;
1009 bfd_size_type *string_size_p;
1010 asection **debug_string_section_p;
1011 bfd_size_type *debug_string_size_p;
1013 combined_entry_type *native;
1014 combined_entry_type dummy;
1016 native = &dummy;
1017 native->u.syment.n_type = T_NULL;
1018 native->u.syment.n_flags = 0;
1019 if (bfd_is_und_section (symbol->section))
1021 native->u.syment.n_scnum = N_UNDEF;
1022 native->u.syment.n_value = symbol->value;
1024 else if (bfd_is_com_section (symbol->section))
1026 native->u.syment.n_scnum = N_UNDEF;
1027 native->u.syment.n_value = symbol->value;
1029 else if (symbol->flags & BSF_DEBUGGING)
1031 /* There isn't much point to writing out a debugging symbol
1032 unless we are prepared to convert it into COFF debugging
1033 format. So, we just ignore them. We must clobber the symbol
1034 name to keep it from being put in the string table. */
1035 symbol->name = "";
1036 return true;
1038 else
1040 native->u.syment.n_scnum =
1041 symbol->section->output_section->target_index;
1042 native->u.syment.n_value = (symbol->value
1043 + symbol->section->output_offset);
1044 if (! obj_pe (abfd))
1045 native->u.syment.n_value += symbol->section->output_section->vma;
1047 /* Copy the any flags from the the file header into the symbol.
1048 FIXME: Why? */
1050 coff_symbol_type *c = coff_symbol_from (abfd, symbol);
1051 if (c != (coff_symbol_type *) NULL)
1052 native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
1056 native->u.syment.n_type = 0;
1057 if (symbol->flags & BSF_LOCAL)
1058 native->u.syment.n_sclass = C_STAT;
1059 else if (symbol->flags & BSF_WEAK)
1060 native->u.syment.n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1061 else
1062 native->u.syment.n_sclass = C_EXT;
1063 native->u.syment.n_numaux = 0;
1065 return coff_write_symbol (abfd, symbol, native, written, string_size_p,
1066 debug_string_section_p, debug_string_size_p);
1069 /* Write a native symbol to a COFF file. */
1071 static boolean
1072 coff_write_native_symbol (abfd, symbol, written, string_size_p,
1073 debug_string_section_p, debug_string_size_p)
1074 bfd *abfd;
1075 coff_symbol_type *symbol;
1076 unsigned int *written;
1077 bfd_size_type *string_size_p;
1078 asection **debug_string_section_p;
1079 bfd_size_type *debug_string_size_p;
1081 combined_entry_type *native = symbol->native;
1082 alent *lineno = symbol->lineno;
1084 /* If this symbol has an associated line number, we must store the
1085 symbol index in the line number field. We also tag the auxent to
1086 point to the right place in the lineno table. */
1087 if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
1089 unsigned int count = 0;
1090 lineno[count].u.offset = *written;
1091 if (native->u.syment.n_numaux)
1093 union internal_auxent *a = &((native + 1)->u.auxent);
1095 a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
1096 symbol->symbol.section->output_section->moving_line_filepos;
1099 /* Count and relocate all other linenumbers. */
1100 count++;
1101 while (lineno[count].line_number != 0)
1103 #if 0
1104 /* 13 april 92. sac
1105 I've been told this, but still need proof:
1106 > The second bug is also in `bfd/coffcode.h'. This bug
1107 > causes the linker to screw up the pc-relocations for
1108 > all the line numbers in COFF code. This bug isn't only
1109 > specific to A29K implementations, but affects all
1110 > systems using COFF format binaries. Note that in COFF
1111 > object files, the line number core offsets output by
1112 > the assembler are relative to the start of each
1113 > procedure, not to the start of the .text section. This
1114 > patch relocates the line numbers relative to the
1115 > `native->u.syment.n_value' instead of the section
1116 > virtual address.
1117 > modular!olson@cs.arizona.edu (Jon Olson)
1119 lineno[count].u.offset += native->u.syment.n_value;
1120 #else
1121 lineno[count].u.offset +=
1122 (symbol->symbol.section->output_section->vma
1123 + symbol->symbol.section->output_offset);
1124 #endif
1125 count++;
1127 symbol->done_lineno = true;
1129 symbol->symbol.section->output_section->moving_line_filepos +=
1130 count * bfd_coff_linesz (abfd);
1133 return coff_write_symbol (abfd, &(symbol->symbol), native, written,
1134 string_size_p, debug_string_section_p,
1135 debug_string_size_p);
1138 /* Write out the COFF symbols. */
1140 boolean
1141 coff_write_symbols (abfd)
1142 bfd *abfd;
1144 bfd_size_type string_size;
1145 asection *debug_string_section;
1146 bfd_size_type debug_string_size;
1147 unsigned int i;
1148 unsigned int limit = bfd_get_symcount (abfd);
1149 unsigned int written = 0;
1150 asymbol **p;
1152 string_size = 0;
1153 debug_string_section = NULL;
1154 debug_string_size = 0;
1156 /* If this target supports long section names, they must be put into
1157 the string table. This is supported by PE. This code must
1158 handle section names just as they are handled in
1159 coff_write_object_contents. */
1160 if (bfd_coff_long_section_names (abfd))
1162 asection *o;
1164 for (o = abfd->sections; o != NULL; o = o->next)
1166 size_t len;
1168 len = strlen (o->name);
1169 if (len > SCNNMLEN)
1170 string_size += len + 1;
1174 /* Seek to the right place */
1175 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1176 return false;
1178 /* 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, &written, &string_size,
1190 &debug_string_section,
1191 &debug_string_size))
1192 return false;
1194 else
1196 if (!coff_write_native_symbol (abfd, c_symbol, &written,
1197 &string_size, &debug_string_section,
1198 &debug_string_size))
1199 return false;
1203 obj_raw_syment_count (abfd) = written;
1205 /* Now write out strings */
1207 if (string_size != 0)
1209 unsigned int size = string_size + STRING_SIZE_SIZE;
1210 bfd_byte buffer[STRING_SIZE_SIZE];
1212 #if STRING_SIZE_SIZE == 4
1213 bfd_h_put_32 (abfd, size, buffer);
1214 #else
1215 #error Change bfd_h_put_32
1216 #endif
1217 if (bfd_write ((PTR) buffer, 1, sizeof (buffer), abfd) != sizeof (buffer))
1218 return false;
1220 /* Handle long section names. This code must handle section
1221 names just as they are handled in coff_write_object_contents. */
1222 if (bfd_coff_long_section_names (abfd))
1224 asection *o;
1226 for (o = abfd->sections; o != NULL; o = o->next)
1228 size_t len;
1230 len = strlen (o->name);
1231 if (len > SCNNMLEN)
1233 if (bfd_write (o->name, 1, len + 1, abfd) != len + 1)
1234 return false;
1239 for (p = abfd->outsymbols, i = 0;
1240 i < limit;
1241 i++, p++)
1243 asymbol *q = *p;
1244 size_t name_length = strlen (q->name);
1245 coff_symbol_type *c_symbol = coff_symbol_from (abfd, q);
1246 size_t maxlen;
1248 /* Figure out whether the symbol name should go in the string
1249 table. Symbol names that are short enough are stored
1250 directly in the syment structure. File names permit a
1251 different, longer, length in the syment structure. On
1252 XCOFF, some symbol names are stored in the .debug section
1253 rather than in the string table. */
1255 if (c_symbol == NULL
1256 || c_symbol->native == NULL)
1258 /* This is not a COFF symbol, so it certainly is not a
1259 file name, nor does it go in the .debug section. */
1260 maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
1262 else if (bfd_coff_symname_in_debug (abfd,
1263 &c_symbol->native->u.syment))
1265 /* This symbol name is in the XCOFF .debug section.
1266 Don't write it into the string table. */
1267 maxlen = name_length;
1269 else if (c_symbol->native->u.syment.n_sclass == C_FILE
1270 && c_symbol->native->u.syment.n_numaux > 0)
1272 if (bfd_coff_force_symnames_in_strings (abfd))
1273 bfd_write (".file", 1, 6, abfd);
1274 maxlen = bfd_coff_filnmlen (abfd);
1276 else
1277 maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
1279 if (name_length > maxlen)
1281 if (bfd_write ((PTR) (q->name), 1, name_length + 1, abfd)
1282 != name_length + 1)
1283 return false;
1287 else
1289 /* We would normally not write anything here, but we'll write
1290 out 4 so that any stupid coff reader which tries to read the
1291 string table even when there isn't one won't croak. */
1292 unsigned int size = STRING_SIZE_SIZE;
1293 bfd_byte buffer[STRING_SIZE_SIZE];
1295 #if STRING_SIZE_SIZE == 4
1296 bfd_h_put_32 (abfd, size, buffer);
1297 #else
1298 #error Change bfd_h_put_32
1299 #endif
1300 if (bfd_write ((PTR) buffer, 1, STRING_SIZE_SIZE, abfd)
1301 != STRING_SIZE_SIZE)
1302 return false;
1305 /* Make sure the .debug section was created to be the correct size.
1306 We should create it ourselves on the fly, but we don't because
1307 BFD won't let us write to any section until we know how large all
1308 the sections are. We could still do it by making another pass
1309 over the symbols. FIXME. */
1310 BFD_ASSERT (debug_string_size == 0
1311 || (debug_string_section != (asection *) NULL
1312 && (BFD_ALIGN (debug_string_size,
1313 1 << debug_string_section->alignment_power)
1314 == bfd_section_size (abfd, debug_string_section))));
1316 return true;
1319 boolean
1320 coff_write_linenumbers (abfd)
1321 bfd *abfd;
1323 asection *s;
1324 bfd_size_type linesz;
1325 PTR buff;
1327 linesz = bfd_coff_linesz (abfd);
1328 buff = bfd_alloc (abfd, linesz);
1329 if (!buff)
1330 return false;
1331 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1333 if (s->lineno_count)
1335 asymbol **q = abfd->outsymbols;
1336 if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
1337 return false;
1338 /* Find all the linenumbers in this section */
1339 while (*q)
1341 asymbol *p = *q;
1342 if (p->section->output_section == s)
1344 alent *l =
1345 BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
1346 (bfd_asymbol_bfd (p), p));
1347 if (l)
1349 /* Found a linenumber entry, output */
1350 struct internal_lineno out;
1351 memset ((PTR) & out, 0, sizeof (out));
1352 out.l_lnno = 0;
1353 out.l_addr.l_symndx = l->u.offset;
1354 bfd_coff_swap_lineno_out (abfd, &out, buff);
1355 if (bfd_write (buff, 1, linesz, abfd) != linesz)
1356 return false;
1357 l++;
1358 while (l->line_number)
1360 out.l_lnno = l->line_number;
1361 out.l_addr.l_symndx = l->u.offset;
1362 bfd_coff_swap_lineno_out (abfd, &out, buff);
1363 if (bfd_write (buff, 1, linesz, abfd) != linesz)
1364 return false;
1365 l++;
1369 q++;
1373 bfd_release (abfd, buff);
1374 return true;
1377 alent *
1378 coff_get_lineno (ignore_abfd, symbol)
1379 bfd *ignore_abfd ATTRIBUTE_UNUSED;
1380 asymbol *symbol;
1382 return coffsymbol (symbol)->lineno;
1385 #if 0
1387 /* This is only called from coff_add_missing_symbols, which has been
1388 disabled. */
1390 asymbol *
1391 coff_section_symbol (abfd, name)
1392 bfd *abfd;
1393 char *name;
1395 asection *sec = bfd_make_section_old_way (abfd, name);
1396 asymbol *sym;
1397 combined_entry_type *csym;
1399 sym = sec->symbol;
1400 csym = coff_symbol_from (abfd, sym)->native;
1401 /* Make sure back-end COFF stuff is there. */
1402 if (csym == 0)
1404 struct foo
1406 coff_symbol_type sym;
1407 /* @@FIXME This shouldn't use a fixed size!! */
1408 combined_entry_type e[10];
1410 struct foo *f;
1411 f = (struct foo *) bfd_alloc (abfd, sizeof (*f));
1412 if (!f)
1414 bfd_set_error (bfd_error_no_error);
1415 return NULL;
1417 memset ((char *) f, 0, sizeof (*f));
1418 coff_symbol_from (abfd, sym)->native = csym = f->e;
1420 csym[0].u.syment.n_sclass = C_STAT;
1421 csym[0].u.syment.n_numaux = 1;
1422 /* SF_SET_STATICS (sym); @@ ??? */
1423 csym[1].u.auxent.x_scn.x_scnlen = sec->_raw_size;
1424 csym[1].u.auxent.x_scn.x_nreloc = sec->reloc_count;
1425 csym[1].u.auxent.x_scn.x_nlinno = sec->lineno_count;
1427 if (sec->output_section == NULL)
1429 sec->output_section = sec;
1430 sec->output_offset = 0;
1433 return sym;
1436 #endif /* 0 */
1438 /* This function transforms the offsets into the symbol table into
1439 pointers to syments. */
1441 static void
1442 coff_pointerize_aux (abfd, table_base, symbol, indaux, auxent)
1443 bfd *abfd;
1444 combined_entry_type *table_base;
1445 combined_entry_type *symbol;
1446 unsigned int indaux;
1447 combined_entry_type *auxent;
1449 unsigned int type = symbol->u.syment.n_type;
1450 unsigned int class = symbol->u.syment.n_sclass;
1452 if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1454 if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1455 (abfd, table_base, symbol, indaux, auxent))
1456 return;
1459 /* Don't bother if this is a file or a section */
1460 if (class == C_STAT && type == T_NULL)
1461 return;
1462 if (class == C_FILE)
1463 return;
1465 /* Otherwise patch up */
1466 #define N_TMASK coff_data (abfd)->local_n_tmask
1467 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1468 if ((ISFCN (type) || ISTAG (class) || class == C_BLOCK || class == C_FCN)
1469 && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l > 0)
1471 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
1472 table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
1473 auxent->fix_end = 1;
1475 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1476 generate one, so we must be careful to ignore it. */
1477 if (auxent->u.auxent.x_sym.x_tagndx.l > 0)
1479 auxent->u.auxent.x_sym.x_tagndx.p =
1480 table_base + auxent->u.auxent.x_sym.x_tagndx.l;
1481 auxent->fix_tag = 1;
1485 /* Allocate space for the ".debug" section, and read it.
1486 We did not read the debug section until now, because
1487 we didn't want to go to the trouble until someone needed it. */
1489 static char *
1490 build_debug_section (abfd)
1491 bfd *abfd;
1493 char *debug_section;
1494 long position;
1496 asection *sect = bfd_get_section_by_name (abfd, ".debug");
1498 if (!sect)
1500 bfd_set_error (bfd_error_no_debug_section);
1501 return NULL;
1504 debug_section = (PTR) bfd_alloc (abfd,
1505 bfd_get_section_size_before_reloc (sect));
1506 if (debug_section == NULL)
1507 return NULL;
1509 /* Seek to the beginning of the `.debug' section and read it.
1510 Save the current position first; it is needed by our caller.
1511 Then read debug section and reset the file pointer. */
1513 position = bfd_tell (abfd);
1514 if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0
1515 || (bfd_read (debug_section,
1516 bfd_get_section_size_before_reloc (sect), 1, abfd)
1517 != bfd_get_section_size_before_reloc (sect))
1518 || bfd_seek (abfd, position, SEEK_SET) != 0)
1519 return NULL;
1520 return debug_section;
1523 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1524 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1525 be \0-terminated. */
1526 static char *
1527 copy_name (abfd, name, maxlen)
1528 bfd *abfd;
1529 char *name;
1530 int maxlen;
1532 int len;
1533 char *newname;
1535 for (len = 0; len < maxlen; ++len)
1537 if (name[len] == '\0')
1539 break;
1543 if ((newname = (PTR) bfd_alloc (abfd, len + 1)) == NULL)
1544 return (NULL);
1545 strncpy (newname, name, len);
1546 newname[len] = '\0';
1547 return newname;
1550 /* Read in the external symbols. */
1552 boolean
1553 _bfd_coff_get_external_symbols (abfd)
1554 bfd *abfd;
1556 bfd_size_type symesz;
1557 size_t size;
1558 PTR syms;
1560 if (obj_coff_external_syms (abfd) != NULL)
1561 return true;
1563 symesz = bfd_coff_symesz (abfd);
1565 size = obj_raw_syment_count (abfd) * symesz;
1567 syms = (PTR) bfd_malloc (size);
1568 if (syms == NULL && size != 0)
1569 return false;
1571 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
1572 || bfd_read (syms, size, 1, abfd) != size)
1574 if (syms != NULL)
1575 free (syms);
1576 return false;
1579 obj_coff_external_syms (abfd) = syms;
1581 return true;
1584 /* Read in the external strings. The strings are not loaded until
1585 they are needed. This is because we have no simple way of
1586 detecting a missing string table in an archive. */
1588 const char *
1589 _bfd_coff_read_string_table (abfd)
1590 bfd *abfd;
1592 char extstrsize[STRING_SIZE_SIZE];
1593 size_t strsize;
1594 char *strings;
1596 if (obj_coff_strings (abfd) != NULL)
1597 return obj_coff_strings (abfd);
1599 if (obj_sym_filepos (abfd) == 0)
1601 bfd_set_error (bfd_error_no_symbols);
1602 return NULL;
1605 if (bfd_seek (abfd,
1606 (obj_sym_filepos (abfd)
1607 + obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd)),
1608 SEEK_SET) != 0)
1609 return NULL;
1611 if (bfd_read (extstrsize, sizeof extstrsize, 1, abfd) != sizeof extstrsize)
1613 if (bfd_get_error () != bfd_error_file_truncated)
1614 return NULL;
1616 /* There is no string table. */
1617 strsize = STRING_SIZE_SIZE;
1619 else
1621 #if STRING_SIZE_SIZE == 4
1622 strsize = bfd_h_get_32 (abfd, (bfd_byte *) extstrsize);
1623 #else
1624 #error Change bfd_h_get_32
1625 #endif
1628 if (strsize < STRING_SIZE_SIZE)
1630 (*_bfd_error_handler)
1631 (_("%s: bad string table size %lu"), bfd_get_filename (abfd),
1632 (unsigned long) strsize);
1633 bfd_set_error (bfd_error_bad_value);
1634 return NULL;
1637 strings = (char *) bfd_malloc (strsize);
1638 if (strings == NULL)
1639 return NULL;
1641 if (bfd_read (strings + STRING_SIZE_SIZE,
1642 strsize - STRING_SIZE_SIZE, 1, abfd)
1643 != strsize - STRING_SIZE_SIZE)
1645 free (strings);
1646 return NULL;
1649 obj_coff_strings (abfd) = strings;
1651 return strings;
1654 /* Free up the external symbols and strings read from a COFF file. */
1656 boolean
1657 _bfd_coff_free_symbols (abfd)
1658 bfd *abfd;
1660 if (obj_coff_external_syms (abfd) != NULL
1661 && ! obj_coff_keep_syms (abfd))
1663 free (obj_coff_external_syms (abfd));
1664 obj_coff_external_syms (abfd) = NULL;
1666 if (obj_coff_strings (abfd) != NULL
1667 && ! obj_coff_keep_strings (abfd))
1669 free (obj_coff_strings (abfd));
1670 obj_coff_strings (abfd) = NULL;
1672 return true;
1675 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1676 knit the symbol names into a normalized form. By normalized here I
1677 mean that all symbols have an n_offset pointer that points to a null-
1678 terminated string. */
1680 combined_entry_type *
1681 coff_get_normalized_symtab (abfd)
1682 bfd *abfd;
1684 combined_entry_type *internal;
1685 combined_entry_type *internal_ptr;
1686 combined_entry_type *symbol_ptr;
1687 combined_entry_type *internal_end;
1688 bfd_size_type symesz;
1689 char *raw_src;
1690 char *raw_end;
1691 const char *string_table = NULL;
1692 char *debug_section = NULL;
1693 unsigned long size;
1695 if (obj_raw_syments (abfd) != NULL)
1696 return obj_raw_syments (abfd);
1698 size = obj_raw_syment_count (abfd) * sizeof (combined_entry_type);
1699 internal = (combined_entry_type *) bfd_zalloc (abfd, size);
1700 if (internal == NULL && size != 0)
1701 return NULL;
1702 internal_end = internal + obj_raw_syment_count (abfd);
1704 if (! _bfd_coff_get_external_symbols (abfd))
1705 return NULL;
1707 raw_src = (char *) obj_coff_external_syms (abfd);
1709 /* mark the end of the symbols */
1710 symesz = bfd_coff_symesz (abfd);
1711 raw_end = (char *) raw_src + obj_raw_syment_count (abfd) * symesz;
1713 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1714 probably possible. If one shows up, it will probably kill us. */
1716 /* Swap all the raw entries */
1717 for (internal_ptr = internal;
1718 raw_src < raw_end;
1719 raw_src += symesz, internal_ptr++)
1722 unsigned int i;
1723 bfd_coff_swap_sym_in (abfd, (PTR) raw_src,
1724 (PTR) & internal_ptr->u.syment);
1725 symbol_ptr = internal_ptr;
1727 for (i = 0;
1728 i < symbol_ptr->u.syment.n_numaux;
1729 i++)
1731 internal_ptr++;
1732 raw_src += symesz;
1733 bfd_coff_swap_aux_in (abfd, (PTR) raw_src,
1734 symbol_ptr->u.syment.n_type,
1735 symbol_ptr->u.syment.n_sclass,
1736 i, symbol_ptr->u.syment.n_numaux,
1737 &(internal_ptr->u.auxent));
1738 coff_pointerize_aux (abfd, internal, symbol_ptr, i,
1739 internal_ptr);
1743 /* Free the raw symbols, but not the strings (if we have them). */
1744 obj_coff_keep_strings (abfd) = true;
1745 if (! _bfd_coff_free_symbols (abfd))
1746 return NULL;
1748 for (internal_ptr = internal; internal_ptr < internal_end;
1749 internal_ptr++)
1751 if (internal_ptr->u.syment.n_sclass == C_FILE
1752 && internal_ptr->u.syment.n_numaux > 0)
1754 /* make a file symbol point to the name in the auxent, since
1755 the text ".file" is redundant */
1756 if ((internal_ptr + 1)->u.auxent.x_file.x_n.x_zeroes == 0)
1758 /* the filename is a long one, point into the string table */
1759 if (string_table == NULL)
1761 string_table = _bfd_coff_read_string_table (abfd);
1762 if (string_table == NULL)
1763 return NULL;
1766 internal_ptr->u.syment._n._n_n._n_offset =
1767 ((long)
1768 (string_table
1769 + (internal_ptr + 1)->u.auxent.x_file.x_n.x_offset));
1771 else
1773 /* Ordinary short filename, put into memory anyway. The
1774 Microsoft PE tools sometimes store a filename in
1775 multiple AUX entries. */
1776 if (internal_ptr->u.syment.n_numaux > 1
1777 && coff_data (abfd)->pe)
1779 internal_ptr->u.syment._n._n_n._n_offset =
1780 ((long)
1781 copy_name (abfd,
1782 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1783 internal_ptr->u.syment.n_numaux * symesz));
1785 else
1787 internal_ptr->u.syment._n._n_n._n_offset =
1788 ((long)
1789 copy_name (abfd,
1790 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1791 bfd_coff_filnmlen (abfd)));
1795 else
1797 if (internal_ptr->u.syment._n._n_n._n_zeroes != 0)
1799 /* This is a "short" name. Make it long. */
1800 unsigned long i = 0;
1801 char *newstring = NULL;
1803 /* find the length of this string without walking into memory
1804 that isn't ours. */
1805 for (i = 0; i < 8; ++i)
1807 if (internal_ptr->u.syment._n._n_name[i] == '\0')
1809 break;
1810 } /* if end of string */
1811 } /* possible lengths of this string. */
1813 if ((newstring = (PTR) bfd_alloc (abfd, ++i)) == NULL)
1814 return (NULL);
1815 memset (newstring, 0, i);
1816 strncpy (newstring, internal_ptr->u.syment._n._n_name, i - 1);
1817 internal_ptr->u.syment._n._n_n._n_offset = (long int) newstring;
1818 internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1820 else if (internal_ptr->u.syment._n._n_n._n_offset == 0)
1821 internal_ptr->u.syment._n._n_n._n_offset = (long int) "";
1822 else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment))
1824 /* Long name already. Point symbol at the string in the
1825 table. */
1826 if (string_table == NULL)
1828 string_table = _bfd_coff_read_string_table (abfd);
1829 if (string_table == NULL)
1830 return NULL;
1832 internal_ptr->u.syment._n._n_n._n_offset =
1833 ((long int)
1834 (string_table
1835 + internal_ptr->u.syment._n._n_n._n_offset));
1837 else
1839 /* Long name in debug section. Very similar. */
1840 if (debug_section == NULL)
1841 debug_section = build_debug_section (abfd);
1842 internal_ptr->u.syment._n._n_n._n_offset = (long int)
1843 (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
1846 internal_ptr += internal_ptr->u.syment.n_numaux;
1849 obj_raw_syments (abfd) = internal;
1850 BFD_ASSERT (obj_raw_syment_count (abfd)
1851 == (unsigned int) (internal_ptr - internal));
1853 return (internal);
1854 } /* coff_get_normalized_symtab() */
1856 long
1857 coff_get_reloc_upper_bound (abfd, asect)
1858 bfd *abfd;
1859 sec_ptr asect;
1861 if (bfd_get_format (abfd) != bfd_object)
1863 bfd_set_error (bfd_error_invalid_operation);
1864 return -1;
1866 return (asect->reloc_count + 1) * sizeof (arelent *);
1869 asymbol *
1870 coff_make_empty_symbol (abfd)
1871 bfd *abfd;
1873 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, sizeof (coff_symbol_type));
1874 if (new == NULL)
1875 return (NULL);
1876 memset (new, 0, sizeof *new);
1877 new->symbol.section = 0;
1878 new->native = 0;
1879 new->lineno = (alent *) NULL;
1880 new->done_lineno = false;
1881 new->symbol.the_bfd = abfd;
1882 return &new->symbol;
1885 /* Make a debugging symbol. */
1887 asymbol *
1888 coff_bfd_make_debug_symbol (abfd, ptr, sz)
1889 bfd *abfd;
1890 PTR ptr ATTRIBUTE_UNUSED;
1891 unsigned long sz ATTRIBUTE_UNUSED;
1893 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, sizeof (coff_symbol_type));
1894 if (new == NULL)
1895 return (NULL);
1896 /* @@ The 10 is a guess at a plausible maximum number of aux entries
1897 (but shouldn't be a constant). */
1898 new->native = (combined_entry_type *) bfd_zalloc (abfd, sizeof (combined_entry_type) * 10);
1899 if (!new->native)
1900 return (NULL);
1901 new->symbol.section = bfd_abs_section_ptr;
1902 new->symbol.flags = BSF_DEBUGGING;
1903 new->lineno = (alent *) NULL;
1904 new->done_lineno = false;
1905 new->symbol.the_bfd = abfd;
1906 return &new->symbol;
1909 void
1910 coff_get_symbol_info (abfd, symbol, ret)
1911 bfd *abfd;
1912 asymbol *symbol;
1913 symbol_info *ret;
1915 bfd_symbol_info (symbol, ret);
1916 if (coffsymbol (symbol)->native != NULL
1917 && coffsymbol (symbol)->native->fix_value)
1919 combined_entry_type *psym;
1921 psym = ((combined_entry_type *)
1922 coffsymbol (symbol)->native->u.syment.n_value);
1923 ret->value = (bfd_vma) (psym - obj_raw_syments (abfd));
1927 /* Return the COFF syment for a symbol. */
1929 boolean
1930 bfd_coff_get_syment (abfd, symbol, psyment)
1931 bfd *abfd;
1932 asymbol *symbol;
1933 struct internal_syment *psyment;
1935 coff_symbol_type *csym;
1937 csym = coff_symbol_from (abfd, symbol);
1938 if (csym == NULL || csym->native == NULL)
1940 bfd_set_error (bfd_error_invalid_operation);
1941 return false;
1944 *psyment = csym->native->u.syment;
1946 if (csym->native->fix_value)
1947 psyment->n_value = ((combined_entry_type *) psyment->n_value
1948 - obj_raw_syments (abfd));
1950 /* FIXME: We should handle fix_line here. */
1952 return true;
1955 /* Return the COFF auxent for a symbol. */
1957 boolean
1958 bfd_coff_get_auxent (abfd, symbol, indx, pauxent)
1959 bfd *abfd;
1960 asymbol *symbol;
1961 int indx;
1962 union internal_auxent *pauxent;
1964 coff_symbol_type *csym;
1965 combined_entry_type *ent;
1967 csym = coff_symbol_from (abfd, symbol);
1969 if (csym == NULL
1970 || csym->native == NULL
1971 || indx >= csym->native->u.syment.n_numaux)
1973 bfd_set_error (bfd_error_invalid_operation);
1974 return false;
1977 ent = csym->native + indx + 1;
1979 *pauxent = ent->u.auxent;
1981 if (ent->fix_tag)
1982 pauxent->x_sym.x_tagndx.l =
1983 ((combined_entry_type *) pauxent->x_sym.x_tagndx.p
1984 - obj_raw_syments (abfd));
1986 if (ent->fix_end)
1987 pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l =
1988 ((combined_entry_type *) pauxent->x_sym.x_fcnary.x_fcn.x_endndx.p
1989 - obj_raw_syments (abfd));
1991 if (ent->fix_scnlen)
1992 pauxent->x_csect.x_scnlen.l =
1993 ((combined_entry_type *) pauxent->x_csect.x_scnlen.p
1994 - obj_raw_syments (abfd));
1996 return true;
1999 /* Print out information about COFF symbol. */
2001 void
2002 coff_print_symbol (abfd, filep, symbol, how)
2003 bfd *abfd;
2004 PTR filep;
2005 asymbol *symbol;
2006 bfd_print_symbol_type how;
2008 FILE *file = (FILE *) filep;
2010 switch (how)
2012 case bfd_print_symbol_name:
2013 fprintf (file, "%s", symbol->name);
2014 break;
2016 case bfd_print_symbol_more:
2017 fprintf (file, "coff %s %s",
2018 coffsymbol (symbol)->native ? "n" : "g",
2019 coffsymbol (symbol)->lineno ? "l" : " ");
2020 break;
2022 case bfd_print_symbol_all:
2023 if (coffsymbol (symbol)->native)
2025 unsigned long val;
2026 unsigned int aux;
2027 combined_entry_type *combined = coffsymbol (symbol)->native;
2028 combined_entry_type *root = obj_raw_syments (abfd);
2029 struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
2031 fprintf (file, "[%3ld]", (long) (combined - root));
2033 if (! combined->fix_value)
2034 val = (unsigned long) combined->u.syment.n_value;
2035 else
2036 val = ((unsigned long)
2037 ((combined_entry_type *) combined->u.syment.n_value
2038 - root));
2040 fprintf (file,
2041 "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x%08lx %s",
2042 combined->u.syment.n_scnum,
2043 combined->u.syment.n_flags,
2044 combined->u.syment.n_type,
2045 combined->u.syment.n_sclass,
2046 combined->u.syment.n_numaux,
2047 val,
2048 symbol->name);
2050 for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
2052 combined_entry_type *auxp = combined + aux + 1;
2053 long tagndx;
2055 if (auxp->fix_tag)
2056 tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
2057 else
2058 tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
2060 fprintf (file, "\n");
2062 if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux))
2063 continue;
2065 switch (combined->u.syment.n_sclass)
2067 case C_FILE:
2068 fprintf (file, "File ");
2069 break;
2071 case C_STAT:
2072 if (combined->u.syment.n_type == T_NULL)
2073 /* probably a section symbol? */
2075 fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
2076 (long) auxp->u.auxent.x_scn.x_scnlen,
2077 auxp->u.auxent.x_scn.x_nreloc,
2078 auxp->u.auxent.x_scn.x_nlinno);
2079 if (auxp->u.auxent.x_scn.x_checksum != 0
2080 || auxp->u.auxent.x_scn.x_associated != 0
2081 || auxp->u.auxent.x_scn.x_comdat != 0)
2082 fprintf (file, " checksum 0x%lx assoc %d comdat %d",
2083 auxp->u.auxent.x_scn.x_checksum,
2084 auxp->u.auxent.x_scn.x_associated,
2085 auxp->u.auxent.x_scn.x_comdat);
2086 break;
2088 /* else fall through */
2089 case C_EXT:
2090 if (ISFCN (combined->u.syment.n_type))
2092 fprintf (file,
2093 _("AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld"),
2094 tagndx,
2095 auxp->u.auxent.x_sym.x_misc.x_fsize,
2096 auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_lnnoptr,
2097 (auxp->fix_end
2098 ? ((long)
2099 (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2100 - root))
2101 : auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l));
2102 break;
2104 /* else fall through */
2105 default:
2106 fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
2107 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
2108 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
2109 tagndx);
2110 if (auxp->fix_end)
2111 fprintf (file, " endndx %ld",
2112 ((long)
2113 (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2114 - root)));
2115 break;
2119 if (l)
2121 fprintf (file, "\n%s :", l->u.sym->name);
2122 l++;
2123 while (l->line_number)
2125 fprintf (file, "\n%4d : 0x%lx",
2126 l->line_number,
2127 ((unsigned long)
2128 (l->u.offset + symbol->section->vma)));
2129 l++;
2133 else
2135 bfd_print_symbol_vandf ((PTR) file, symbol);
2136 fprintf (file, " %-5s %s %s %s",
2137 symbol->section->name,
2138 coffsymbol (symbol)->native ? "n" : "g",
2139 coffsymbol (symbol)->lineno ? "l" : " ",
2140 symbol->name);
2145 /* Return whether a symbol name implies a local symbol. In COFF,
2146 local symbols generally start with ``.L''. Most targets use this
2147 function for the is_local_label_name entry point, but some may
2148 override it. */
2150 boolean
2151 _bfd_coff_is_local_label_name (abfd, name)
2152 bfd *abfd ATTRIBUTE_UNUSED;
2153 const char *name;
2155 return name[0] == '.' && name[1] == 'L';
2158 /* Provided a BFD, a section and an offset (in bytes, not octets) into the
2159 section, calculate and return the name of the source file and the line
2160 nearest to the wanted location. */
2162 boolean
2163 coff_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
2164 functionname_ptr, line_ptr)
2165 bfd *abfd;
2166 asection *section;
2167 asymbol **symbols;
2168 bfd_vma offset;
2169 CONST char **filename_ptr;
2170 CONST char **functionname_ptr;
2171 unsigned int *line_ptr;
2173 boolean found;
2174 unsigned int i;
2175 unsigned int line_base;
2176 coff_data_type *cof = coff_data (abfd);
2177 /* Run through the raw syments if available */
2178 combined_entry_type *p;
2179 combined_entry_type *pend;
2180 alent *l;
2181 struct coff_section_tdata *sec_data;
2183 /* Before looking through the symbol table, try to use a .stab
2184 section to find the information. */
2185 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
2186 &found, filename_ptr,
2187 functionname_ptr, line_ptr,
2188 &coff_data(abfd)->line_info))
2189 return false;
2191 if (found)
2192 return true;
2194 /* Also try examining DWARF2 debugging information. */
2195 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
2196 filename_ptr, functionname_ptr,
2197 line_ptr, 0,
2198 &coff_data(abfd)->dwarf2_find_line_info))
2199 return true;
2201 *filename_ptr = 0;
2202 *functionname_ptr = 0;
2203 *line_ptr = 0;
2205 /* Don't try and find line numbers in a non coff file */
2206 if (!bfd_family_coff (abfd))
2207 return false;
2209 if (cof == NULL)
2210 return false;
2212 /* Find the first C_FILE symbol. */
2213 p = cof->raw_syments;
2214 if (!p)
2215 return false;
2217 pend = p + cof->raw_syment_count;
2218 while (p < pend)
2220 if (p->u.syment.n_sclass == C_FILE)
2221 break;
2222 p += 1 + p->u.syment.n_numaux;
2225 if (p < pend)
2227 bfd_vma sec_vma;
2228 bfd_vma maxdiff;
2230 /* Look through the C_FILE symbols to find the best one. */
2231 sec_vma = bfd_get_section_vma (abfd, section);
2232 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2233 maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
2234 while (1)
2236 combined_entry_type *p2;
2238 for (p2 = p + 1 + p->u.syment.n_numaux;
2239 p2 < pend;
2240 p2 += 1 + p2->u.syment.n_numaux)
2242 if (p2->u.syment.n_scnum > 0
2243 && (section
2244 == coff_section_from_bfd_index (abfd,
2245 p2->u.syment.n_scnum)))
2246 break;
2247 if (p2->u.syment.n_sclass == C_FILE)
2249 p2 = pend;
2250 break;
2254 /* We use <= MAXDIFF here so that if we get a zero length
2255 file, we actually use the next file entry. */
2256 if (p2 < pend
2257 && offset + sec_vma >= (bfd_vma) p2->u.syment.n_value
2258 && offset + sec_vma - (bfd_vma) p2->u.syment.n_value <= maxdiff)
2260 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2261 maxdiff = offset + sec_vma - p2->u.syment.n_value;
2264 /* Avoid endless loops on erroneous files by ensuring that
2265 we always move forward in the file. */
2266 if (p - cof->raw_syments >= p->u.syment.n_value)
2267 break;
2269 p = cof->raw_syments + p->u.syment.n_value;
2270 if (p > pend || p->u.syment.n_sclass != C_FILE)
2271 break;
2275 /* Now wander though the raw linenumbers of the section */
2276 /* If we have been called on this section before, and the offset we
2277 want is further down then we can prime the lookup loop. */
2278 sec_data = coff_section_data (abfd, section);
2279 if (sec_data != NULL
2280 && sec_data->i > 0
2281 && offset >= sec_data->offset)
2283 i = sec_data->i;
2284 *functionname_ptr = sec_data->function;
2285 line_base = sec_data->line_base;
2287 else
2289 i = 0;
2290 line_base = 0;
2293 if (section->lineno != NULL)
2295 bfd_vma last_value = 0;
2297 l = &section->lineno[i];
2299 for (; i < section->lineno_count; i++)
2301 if (l->line_number == 0)
2303 /* Get the symbol this line number points at */
2304 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
2305 if (coff->symbol.value > offset)
2306 break;
2307 *functionname_ptr = coff->symbol.name;
2308 last_value = coff->symbol.value;
2309 if (coff->native)
2311 combined_entry_type *s = coff->native;
2312 s = s + 1 + s->u.syment.n_numaux;
2314 /* In XCOFF a debugging symbol can follow the
2315 function symbol. */
2316 if (s->u.syment.n_scnum == N_DEBUG)
2317 s = s + 1 + s->u.syment.n_numaux;
2319 /* S should now point to the .bf of the function. */
2320 if (s->u.syment.n_numaux)
2322 /* The linenumber is stored in the auxent. */
2323 union internal_auxent *a = &((s + 1)->u.auxent);
2324 line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
2325 *line_ptr = line_base;
2329 else
2331 if (l->u.offset > offset)
2332 break;
2333 *line_ptr = l->line_number + line_base - 1;
2335 l++;
2338 /* If we fell off the end of the loop, then assume that this
2339 symbol has no line number info. Otherwise, symbols with no
2340 line number info get reported with the line number of the
2341 last line of the last symbol which does have line number
2342 info. We use 0x100 as a slop to account for cases where the
2343 last line has executable code. */
2344 if (i >= section->lineno_count
2345 && last_value != 0
2346 && offset - last_value > 0x100)
2348 *functionname_ptr = NULL;
2349 *line_ptr = 0;
2353 /* Cache the results for the next call. */
2354 if (sec_data == NULL && section->owner == abfd)
2356 section->used_by_bfd =
2357 ((PTR) bfd_zalloc (abfd,
2358 sizeof (struct coff_section_tdata)));
2359 sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2361 if (sec_data != NULL)
2363 sec_data->offset = offset;
2364 sec_data->i = i;
2365 sec_data->function = *functionname_ptr;
2366 sec_data->line_base = line_base;
2369 return true;
2373 coff_sizeof_headers (abfd, reloc)
2374 bfd *abfd;
2375 boolean reloc;
2377 size_t size;
2379 if (reloc == false)
2381 size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
2383 else
2385 size = bfd_coff_filhsz (abfd);
2388 size += abfd->section_count * bfd_coff_scnhsz (abfd);
2389 return size;
2392 /* Change the class of a coff symbol held by BFD. */
2393 boolean
2394 bfd_coff_set_symbol_class (abfd, symbol, class)
2395 bfd * abfd;
2396 asymbol * symbol;
2397 unsigned int class;
2399 coff_symbol_type * csym;
2401 csym = coff_symbol_from (abfd, symbol);
2402 if (csym == NULL)
2404 bfd_set_error (bfd_error_invalid_operation);
2405 return false;
2407 else if (csym->native == NULL)
2409 /* This is an alien symbol which no native coff backend data.
2410 We cheat here by creating a fake native entry for it and
2411 then filling in the class. This code is based on that in
2412 coff_write_alien_symbol(). */
2414 combined_entry_type * native;
2416 native = (combined_entry_type *) bfd_alloc (abfd, sizeof (* native));
2417 if (native == NULL)
2418 return false;
2420 memset (native, 0, sizeof (* native));
2422 native->u.syment.n_type = T_NULL;
2423 native->u.syment.n_sclass = class;
2425 if (bfd_is_und_section (symbol->section))
2427 native->u.syment.n_scnum = N_UNDEF;
2428 native->u.syment.n_value = symbol->value;
2430 else if (bfd_is_com_section (symbol->section))
2432 native->u.syment.n_scnum = N_UNDEF;
2433 native->u.syment.n_value = symbol->value;
2435 else
2437 native->u.syment.n_scnum =
2438 symbol->section->output_section->target_index;
2439 native->u.syment.n_value = (symbol->value
2440 + symbol->section->output_offset);
2441 if (! obj_pe (abfd))
2442 native->u.syment.n_value += symbol->section->output_section->vma;
2444 /* Copy the any flags from the the file header into the symbol.
2445 FIXME: Why? */
2446 native->u.syment.n_flags = bfd_asymbol_bfd (& csym->symbol)->flags;
2449 csym->native = native;
2451 else
2453 csym->native->u.syment.n_sclass = class;
2456 return true;