Make .ifdef treat a referenced but not yet defined symbol as if it were
[binutils.git] / bfd / coffgen.c
blob31788e85451cc562ea6923d958d80d0d30c14ed5
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
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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 /* Most of this hacked by Steve Chamberlain, sac@cygnus.com.
24 Split out of coffcode.h by Ian Taylor, ian@cygnus.com. */
26 /* This file contains COFF code that is not dependent on any
27 particular COFF target. There is only one version of this file in
28 libbfd.a, so no target specific code may be put in here. Or, to
29 put it another way,
31 ********** DO NOT PUT TARGET SPECIFIC CODE IN THIS FILE **********
33 If you need to add some target specific behaviour, add a new hook
34 function to bfd_coff_backend_data.
36 Some of these functions are also called by the ECOFF routines.
37 Those functions may not use any COFF specific information, such as
38 coff_data (abfd). */
40 #include "bfd.h"
41 #include "sysdep.h"
42 #include "libbfd.h"
43 #include "coff/internal.h"
44 #include "libcoff.h"
46 static void coff_fix_symbol_name
47 PARAMS ((bfd *, asymbol *, combined_entry_type *, bfd_size_type *,
48 asection **, bfd_size_type *));
49 static boolean coff_write_symbol
50 PARAMS ((bfd *, asymbol *, combined_entry_type *, bfd_vma *,
51 bfd_size_type *, asection **, bfd_size_type *));
52 static boolean coff_write_alien_symbol
53 PARAMS ((bfd *, asymbol *, bfd_vma *, bfd_size_type *,
54 asection **, bfd_size_type *));
55 static boolean coff_write_native_symbol
56 PARAMS ((bfd *, coff_symbol_type *, bfd_vma *, bfd_size_type *,
57 asection **, bfd_size_type *));
58 static void coff_pointerize_aux
59 PARAMS ((bfd *, combined_entry_type *, combined_entry_type *,
60 unsigned int, combined_entry_type *));
61 static boolean make_a_section_from_file
62 PARAMS ((bfd *, struct internal_scnhdr *, unsigned int));
63 static const bfd_target *coff_real_object_p
64 PARAMS ((bfd *, unsigned, struct internal_filehdr *,
65 struct internal_aouthdr *));
66 static void fixup_symbol_value
67 PARAMS ((bfd *, coff_symbol_type *, struct internal_syment *));
68 static char *build_debug_section
69 PARAMS ((bfd *));
70 static char *copy_name
71 PARAMS ((bfd *, char *, size_t));
73 #define STRING_SIZE_SIZE (4)
75 /* Take a section header read from a coff file (in HOST byte order),
76 and make a BFD "section" out of it. This is used by ECOFF. */
77 static boolean
78 make_a_section_from_file (abfd, hdr, target_index)
79 bfd *abfd;
80 struct internal_scnhdr *hdr;
81 unsigned int target_index;
83 asection *return_section;
84 char *name;
85 boolean result = true;
86 flagword flags;
88 name = NULL;
90 /* Handle long section names as in PE. */
91 if (bfd_coff_long_section_names (abfd)
92 && hdr->s_name[0] == '/')
94 char buf[SCNNMLEN];
95 long strindex;
96 char *p;
97 const char *strings;
99 memcpy (buf, hdr->s_name + 1, SCNNMLEN - 1);
100 buf[SCNNMLEN - 1] = '\0';
101 strindex = strtol (buf, &p, 10);
102 if (*p == '\0' && strindex >= 0)
104 strings = _bfd_coff_read_string_table (abfd);
105 if (strings == NULL)
106 return false;
107 /* FIXME: For extra safety, we should make sure that
108 strindex does not run us past the end, but right now we
109 don't know the length of the string table. */
110 strings += strindex;
111 name = bfd_alloc (abfd, (bfd_size_type) strlen (strings) + 1);
112 if (name == NULL)
113 return false;
114 strcpy (name, strings);
118 if (name == NULL)
120 /* Assorted wastage to null-terminate the name, thanks AT&T! */
121 name = bfd_alloc (abfd, (bfd_size_type) sizeof (hdr->s_name) + 1);
122 if (name == NULL)
123 return false;
124 strncpy (name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
125 name[sizeof (hdr->s_name)] = 0;
128 return_section = bfd_make_section_anyway (abfd, name);
129 if (return_section == NULL)
130 return false;
132 return_section->vma = hdr->s_vaddr;
133 return_section->lma = hdr->s_paddr;
134 return_section->_raw_size = hdr->s_size;
135 return_section->filepos = hdr->s_scnptr;
136 return_section->rel_filepos = hdr->s_relptr;
137 return_section->reloc_count = hdr->s_nreloc;
139 bfd_coff_set_alignment_hook (abfd, return_section, hdr);
141 return_section->line_filepos = hdr->s_lnnoptr;
143 return_section->lineno_count = hdr->s_nlnno;
144 return_section->userdata = NULL;
145 return_section->next = (asection *) NULL;
146 return_section->target_index = target_index;
148 if (! bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name, return_section,
149 & flags))
150 result = false;
152 return_section->flags = flags;
154 /* At least on i386-coff, the line number count for a shared library
155 section must be ignored. */
156 if ((return_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
157 return_section->lineno_count = 0;
159 if (hdr->s_nreloc != 0)
160 return_section->flags |= SEC_RELOC;
161 /* FIXME: should this check 'hdr->s_size > 0' */
162 if (hdr->s_scnptr != 0)
163 return_section->flags |= SEC_HAS_CONTENTS;
165 return result;
168 /* Read in a COFF object and make it into a BFD. This is used by
169 ECOFF as well. */
171 static const bfd_target *
172 coff_real_object_p (abfd, nscns, internal_f, internal_a)
173 bfd *abfd;
174 unsigned nscns;
175 struct internal_filehdr *internal_f;
176 struct internal_aouthdr *internal_a;
178 flagword oflags = abfd->flags;
179 bfd_vma ostart = bfd_get_start_address (abfd);
180 PTR tdata;
181 bfd_size_type readsize; /* length of file_info */
182 unsigned int scnhsz;
183 char *external_sections;
185 if (!(internal_f->f_flags & F_RELFLG))
186 abfd->flags |= HAS_RELOC;
187 if ((internal_f->f_flags & F_EXEC))
188 abfd->flags |= EXEC_P;
189 if (!(internal_f->f_flags & F_LNNO))
190 abfd->flags |= HAS_LINENO;
191 if (!(internal_f->f_flags & F_LSYMS))
192 abfd->flags |= HAS_LOCALS;
194 /* FIXME: How can we set D_PAGED correctly? */
195 if ((internal_f->f_flags & F_EXEC) != 0)
196 abfd->flags |= D_PAGED;
198 bfd_get_symcount (abfd) = internal_f->f_nsyms;
199 if (internal_f->f_nsyms)
200 abfd->flags |= HAS_SYMS;
202 if (internal_a != (struct internal_aouthdr *) NULL)
203 bfd_get_start_address (abfd) = internal_a->entry;
204 else
205 bfd_get_start_address (abfd) = 0;
207 /* Set up the tdata area. ECOFF uses its own routine, and overrides
208 abfd->flags. */
209 tdata = bfd_coff_mkobject_hook (abfd, (PTR) internal_f, (PTR) internal_a);
210 if (tdata == NULL)
211 return 0;
213 scnhsz = bfd_coff_scnhsz (abfd);
214 readsize = (bfd_size_type) nscns * scnhsz;
215 external_sections = (char *) bfd_alloc (abfd, readsize);
216 if (!external_sections)
217 goto fail;
219 if (bfd_bread ((PTR) external_sections, readsize, abfd) != readsize)
220 goto fail;
222 /* Set the arch/mach *before* swapping in sections; section header swapping
223 may depend on arch/mach info. */
224 if (! bfd_coff_set_arch_mach_hook (abfd, (PTR) internal_f))
225 goto fail;
227 /* Now copy data as required; construct all asections etc */
228 if (nscns != 0)
230 unsigned int i;
231 for (i = 0; i < nscns; i++)
233 struct internal_scnhdr tmp;
234 bfd_coff_swap_scnhdr_in (abfd,
235 (PTR) (external_sections + i * scnhsz),
236 (PTR) & tmp);
237 if (! make_a_section_from_file (abfd, &tmp, i + 1))
238 goto fail;
242 /* make_abs_section (abfd); */
244 return abfd->xvec;
246 fail:
247 bfd_release (abfd, tdata);
248 abfd->flags = oflags;
249 bfd_get_start_address (abfd) = ostart;
250 return (const bfd_target *) NULL;
253 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
254 not a COFF file. This is also used by ECOFF. */
256 const bfd_target *
257 coff_object_p (abfd)
258 bfd *abfd;
260 bfd_size_type filhsz;
261 bfd_size_type aoutsz;
262 unsigned int nscns;
263 PTR filehdr;
264 struct internal_filehdr internal_f;
265 struct internal_aouthdr internal_a;
267 /* figure out how much to read */
268 filhsz = bfd_coff_filhsz (abfd);
269 aoutsz = bfd_coff_aoutsz (abfd);
271 filehdr = bfd_alloc (abfd, filhsz);
272 if (filehdr == NULL)
273 return 0;
274 if (bfd_bread (filehdr, filhsz, abfd) != filhsz)
276 if (bfd_get_error () != bfd_error_system_call)
277 bfd_set_error (bfd_error_wrong_format);
278 return 0;
280 bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f);
281 bfd_release (abfd, filehdr);
283 /* The XCOFF format has two sizes for the f_opthdr. SMALL_AOUTSZ
284 (less than aoutsz) used in object files and AOUTSZ (equal to
285 aoutsz) in executables. The bfd_coff_swap_aouthdr_in function
286 expects this header to be aoutsz bytes in length, so we use that
287 value in the call to bfd_alloc below. But we must be careful to
288 only read in f_opthdr bytes in the call to bfd_bread. We should
289 also attempt to catch corrupt or non-COFF binaries with a strange
290 value for f_opthdr. */
291 if (! bfd_coff_bad_format_hook (abfd, &internal_f)
292 || internal_f.f_opthdr > aoutsz)
294 bfd_set_error (bfd_error_wrong_format);
295 return 0;
297 nscns = internal_f.f_nscns;
299 if (internal_f.f_opthdr)
301 PTR opthdr;
303 opthdr = bfd_alloc (abfd, aoutsz);
304 if (opthdr == NULL)
305 return 0;
306 if (bfd_bread (opthdr, (bfd_size_type) internal_f.f_opthdr, abfd)
307 != internal_f.f_opthdr)
309 return 0;
311 bfd_coff_swap_aouthdr_in (abfd, opthdr, (PTR) &internal_a);
314 return coff_real_object_p (abfd, nscns, &internal_f,
315 (internal_f.f_opthdr != 0
316 ? &internal_a
317 : (struct internal_aouthdr *) NULL));
320 /* Get the BFD section from a COFF symbol section number. */
322 asection *
323 coff_section_from_bfd_index (abfd, index)
324 bfd *abfd;
325 int index;
327 struct sec *answer = abfd->sections;
329 if (index == N_ABS)
330 return bfd_abs_section_ptr;
331 if (index == N_UNDEF)
332 return bfd_und_section_ptr;
333 if (index == N_DEBUG)
334 return bfd_abs_section_ptr;
336 while (answer)
338 if (answer->target_index == index)
339 return answer;
340 answer = answer->next;
343 /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
344 has a bad symbol table in biglitpow.o. */
345 return bfd_und_section_ptr;
348 /* Get the upper bound of a COFF symbol table. */
350 long
351 coff_get_symtab_upper_bound (abfd)
352 bfd *abfd;
354 if (!bfd_coff_slurp_symbol_table (abfd))
355 return -1;
357 return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *));
360 /* Canonicalize a COFF symbol table. */
362 long
363 coff_get_symtab (abfd, alocation)
364 bfd *abfd;
365 asymbol **alocation;
367 unsigned int counter;
368 coff_symbol_type *symbase;
369 coff_symbol_type **location = (coff_symbol_type **) alocation;
371 if (!bfd_coff_slurp_symbol_table (abfd))
372 return -1;
374 symbase = obj_symbols (abfd);
375 counter = bfd_get_symcount (abfd);
376 while (counter-- > 0)
377 *location++ = symbase++;
379 *location = NULL;
381 return bfd_get_symcount (abfd);
384 /* Get the name of a symbol. The caller must pass in a buffer of size
385 >= SYMNMLEN + 1. */
387 const char *
388 _bfd_coff_internal_syment_name (abfd, sym, buf)
389 bfd *abfd;
390 const struct internal_syment *sym;
391 char *buf;
393 /* FIXME: It's not clear this will work correctly if sizeof
394 (_n_zeroes) != 4. */
395 if (sym->_n._n_n._n_zeroes != 0
396 || sym->_n._n_n._n_offset == 0)
398 memcpy (buf, sym->_n._n_name, SYMNMLEN);
399 buf[SYMNMLEN] = '\0';
400 return buf;
402 else
404 const char *strings;
406 BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE);
407 strings = obj_coff_strings (abfd);
408 if (strings == NULL)
410 strings = _bfd_coff_read_string_table (abfd);
411 if (strings == NULL)
412 return NULL;
414 return strings + sym->_n._n_n._n_offset;
418 /* Read in and swap the relocs. This returns a buffer holding the
419 relocs for section SEC in file ABFD. If CACHE is true and
420 INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
421 the function is called again. If EXTERNAL_RELOCS is not NULL, it
422 is a buffer large enough to hold the unswapped relocs. If
423 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
424 the swapped relocs. If REQUIRE_INTERNAL is true, then the return
425 value must be INTERNAL_RELOCS. The function returns NULL on error. */
427 struct internal_reloc *
428 _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
429 require_internal, internal_relocs)
430 bfd *abfd;
431 asection *sec;
432 boolean cache;
433 bfd_byte *external_relocs;
434 boolean require_internal;
435 struct internal_reloc *internal_relocs;
437 bfd_size_type relsz;
438 bfd_byte *free_external = NULL;
439 struct internal_reloc *free_internal = NULL;
440 bfd_byte *erel;
441 bfd_byte *erel_end;
442 struct internal_reloc *irel;
443 bfd_size_type amt;
445 if (coff_section_data (abfd, sec) != NULL
446 && coff_section_data (abfd, sec)->relocs != NULL)
448 if (! require_internal)
449 return coff_section_data (abfd, sec)->relocs;
450 memcpy (internal_relocs, coff_section_data (abfd, sec)->relocs,
451 sec->reloc_count * sizeof (struct internal_reloc));
452 return internal_relocs;
455 relsz = bfd_coff_relsz (abfd);
457 amt = sec->reloc_count * relsz;
458 if (external_relocs == NULL)
460 free_external = (bfd_byte *) bfd_malloc (amt);
461 if (free_external == NULL && sec->reloc_count > 0)
462 goto error_return;
463 external_relocs = free_external;
466 if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
467 || bfd_bread (external_relocs, amt, abfd) != amt)
468 goto error_return;
470 if (internal_relocs == NULL)
472 amt = sec->reloc_count;
473 amt *= sizeof (struct internal_reloc);
474 free_internal = (struct internal_reloc *) bfd_malloc (amt);
475 if (free_internal == NULL && sec->reloc_count > 0)
476 goto error_return;
477 internal_relocs = free_internal;
480 /* Swap in the relocs. */
481 erel = external_relocs;
482 erel_end = erel + relsz * sec->reloc_count;
483 irel = internal_relocs;
484 for (; erel < erel_end; erel += relsz, irel++)
485 bfd_coff_swap_reloc_in (abfd, (PTR) erel, (PTR) irel);
487 if (free_external != NULL)
489 free (free_external);
490 free_external = NULL;
493 if (cache && free_internal != NULL)
495 if (coff_section_data (abfd, sec) == NULL)
497 amt = sizeof (struct coff_section_tdata);
498 sec->used_by_bfd = (PTR) bfd_zalloc (abfd, amt);
499 if (sec->used_by_bfd == NULL)
500 goto error_return;
501 coff_section_data (abfd, sec)->contents = NULL;
503 coff_section_data (abfd, sec)->relocs = free_internal;
506 return internal_relocs;
508 error_return:
509 if (free_external != NULL)
510 free (free_external);
511 if (free_internal != NULL)
512 free (free_internal);
513 return NULL;
516 /* Set lineno_count for the output sections of a COFF file. */
519 coff_count_linenumbers (abfd)
520 bfd *abfd;
522 unsigned int limit = bfd_get_symcount (abfd);
523 unsigned int i;
524 int total = 0;
525 asymbol **p;
526 asection *s;
528 if (limit == 0)
530 /* This may be from the backend linker, in which case the
531 lineno_count in the sections is correct. */
532 for (s = abfd->sections; s != NULL; s = s->next)
533 total += s->lineno_count;
534 return total;
537 for (s = abfd->sections; s != NULL; s = s->next)
538 BFD_ASSERT (s->lineno_count == 0);
540 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
542 asymbol *q_maybe = *p;
544 if (bfd_family_coff (bfd_asymbol_bfd (q_maybe)))
546 coff_symbol_type *q = coffsymbol (q_maybe);
548 /* The AIX 4.1 compiler can sometimes generate line numbers
549 attached to debugging symbols. We try to simply ignore
550 those here. */
551 if (q->lineno != NULL
552 && q->symbol.section->owner != NULL)
554 /* This symbol has line numbers. Increment the owning
555 section's linenumber count. */
556 alent *l = q->lineno;
560 asection * sec = q->symbol.section->output_section;
562 /* Do not try to update fields in read-only sections. */
563 if (! bfd_is_const_section (sec))
564 sec->lineno_count ++;
566 ++total;
567 ++l;
569 while (l->line_number != 0);
574 return total;
577 /* Takes a bfd and a symbol, returns a pointer to the coff specific
578 area of the symbol if there is one. */
580 coff_symbol_type *
581 coff_symbol_from (ignore_abfd, symbol)
582 bfd *ignore_abfd ATTRIBUTE_UNUSED;
583 asymbol *symbol;
585 if (!bfd_family_coff (bfd_asymbol_bfd (symbol)))
586 return (coff_symbol_type *) NULL;
588 if (bfd_asymbol_bfd (symbol)->tdata.coff_obj_data == (coff_data_type *) NULL)
589 return (coff_symbol_type *) NULL;
591 return (coff_symbol_type *) symbol;
594 static void
595 fixup_symbol_value (abfd, coff_symbol_ptr, syment)
596 bfd *abfd;
597 coff_symbol_type *coff_symbol_ptr;
598 struct internal_syment *syment;
601 /* Normalize the symbol flags */
602 if (bfd_is_com_section (coff_symbol_ptr->symbol.section))
604 /* a common symbol is undefined with a value */
605 syment->n_scnum = N_UNDEF;
606 syment->n_value = coff_symbol_ptr->symbol.value;
608 else if ((coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) != 0
609 && (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING_RELOC) == 0)
611 syment->n_value = coff_symbol_ptr->symbol.value;
613 else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
615 syment->n_scnum = N_UNDEF;
616 syment->n_value = 0;
618 /* FIXME: Do we need to handle the absolute section here? */
619 else
621 if (coff_symbol_ptr->symbol.section)
623 syment->n_scnum =
624 coff_symbol_ptr->symbol.section->output_section->target_index;
626 syment->n_value = (coff_symbol_ptr->symbol.value
627 + coff_symbol_ptr->symbol.section->output_offset);
628 if (! obj_pe (abfd))
630 syment->n_value += (syment->n_sclass == C_STATLAB)
631 ? coff_symbol_ptr->symbol.section->output_section->lma
632 : coff_symbol_ptr->symbol.section->output_section->vma;
635 else
637 BFD_ASSERT (0);
638 /* This can happen, but I don't know why yet (steve@cygnus.com) */
639 syment->n_scnum = N_ABS;
640 syment->n_value = coff_symbol_ptr->symbol.value;
645 /* Run through all the symbols in the symbol table and work out what
646 their indexes into the symbol table will be when output.
648 Coff requires that each C_FILE symbol points to the next one in the
649 chain, and that the last one points to the first external symbol. We
650 do that here too. */
652 boolean
653 coff_renumber_symbols (bfd_ptr, first_undef)
654 bfd *bfd_ptr;
655 int *first_undef;
657 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
658 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
659 unsigned int native_index = 0;
660 struct internal_syment *last_file = (struct internal_syment *) NULL;
661 unsigned int symbol_index;
663 /* COFF demands that undefined symbols come after all other symbols.
664 Since we don't need to impose this extra knowledge on all our
665 client programs, deal with that here. Sort the symbol table;
666 just move the undefined symbols to the end, leaving the rest
667 alone. The O'Reilly book says that defined global symbols come
668 at the end before the undefined symbols, so we do that here as
669 well. */
670 /* @@ Do we have some condition we could test for, so we don't always
671 have to do this? I don't think relocatability is quite right, but
672 I'm not certain. [raeburn:19920508.1711EST] */
674 asymbol **newsyms;
675 unsigned int i;
676 bfd_size_type amt;
678 amt = sizeof (asymbol *) * ((bfd_size_type) symbol_count + 1);
679 newsyms = (asymbol **) bfd_alloc (bfd_ptr, amt);
680 if (!newsyms)
681 return false;
682 bfd_ptr->outsymbols = newsyms;
683 for (i = 0; i < symbol_count; i++)
684 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
685 || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
686 && !bfd_is_com_section (symbol_ptr_ptr[i]->section)
687 && ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) != 0
688 || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
689 == 0))))
690 *newsyms++ = symbol_ptr_ptr[i];
692 for (i = 0; i < symbol_count; i++)
693 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
694 && !bfd_is_und_section (symbol_ptr_ptr[i]->section)
695 && (bfd_is_com_section (symbol_ptr_ptr[i]->section)
696 || ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) == 0
697 && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
698 != 0))))
699 *newsyms++ = symbol_ptr_ptr[i];
701 *first_undef = newsyms - bfd_ptr->outsymbols;
703 for (i = 0; i < symbol_count; i++)
704 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
705 && bfd_is_und_section (symbol_ptr_ptr[i]->section))
706 *newsyms++ = symbol_ptr_ptr[i];
707 *newsyms = (asymbol *) NULL;
708 symbol_ptr_ptr = bfd_ptr->outsymbols;
711 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
713 coff_symbol_type *coff_symbol_ptr = coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
714 symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
715 if (coff_symbol_ptr && coff_symbol_ptr->native)
717 combined_entry_type *s = coff_symbol_ptr->native;
718 int i;
720 if (s->u.syment.n_sclass == C_FILE)
722 if (last_file != (struct internal_syment *) NULL)
723 last_file->n_value = native_index;
724 last_file = &(s->u.syment);
726 else
729 /* Modify the symbol values according to their section and
730 type */
732 fixup_symbol_value (bfd_ptr, coff_symbol_ptr, &(s->u.syment));
734 for (i = 0; i < s->u.syment.n_numaux + 1; i++)
735 s[i].offset = native_index++;
737 else
739 native_index++;
742 obj_conv_table_size (bfd_ptr) = native_index;
744 return true;
747 /* Run thorough the symbol table again, and fix it so that all
748 pointers to entries are changed to the entries' index in the output
749 symbol table. */
751 void
752 coff_mangle_symbols (bfd_ptr)
753 bfd *bfd_ptr;
755 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
756 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
757 unsigned int symbol_index;
759 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
761 coff_symbol_type *coff_symbol_ptr =
762 coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
764 if (coff_symbol_ptr && coff_symbol_ptr->native)
766 int i;
767 combined_entry_type *s = coff_symbol_ptr->native;
769 if (s->fix_value)
771 /* FIXME: We should use a union here. */
772 s->u.syment.n_value =
773 (bfd_vma)((combined_entry_type *)
774 ((unsigned long) s->u.syment.n_value))->offset;
775 s->fix_value = 0;
777 if (s->fix_line)
779 /* The value is the offset into the line number entries
780 for the symbol's section. On output, the symbol's
781 section should be N_DEBUG. */
782 s->u.syment.n_value =
783 (coff_symbol_ptr->symbol.section->output_section->line_filepos
784 + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr));
785 coff_symbol_ptr->symbol.section =
786 coff_section_from_bfd_index (bfd_ptr, N_DEBUG);
787 BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
789 for (i = 0; i < s->u.syment.n_numaux; i++)
791 combined_entry_type *a = s + i + 1;
792 if (a->fix_tag)
794 a->u.auxent.x_sym.x_tagndx.l =
795 a->u.auxent.x_sym.x_tagndx.p->offset;
796 a->fix_tag = 0;
798 if (a->fix_end)
800 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
801 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
802 a->fix_end = 0;
804 if (a->fix_scnlen)
806 a->u.auxent.x_csect.x_scnlen.l =
807 a->u.auxent.x_csect.x_scnlen.p->offset;
808 a->fix_scnlen = 0;
815 static void
816 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
817 debug_string_section_p, debug_string_size_p)
818 bfd *abfd;
819 asymbol *symbol;
820 combined_entry_type *native;
821 bfd_size_type *string_size_p;
822 asection **debug_string_section_p;
823 bfd_size_type *debug_string_size_p;
825 unsigned int name_length;
826 union internal_auxent *auxent;
827 char *name = (char *) (symbol->name);
829 if (name == (char *) NULL)
831 /* coff symbols always have names, so we'll make one up */
832 symbol->name = "strange";
833 name = (char *) symbol->name;
835 name_length = strlen (name);
837 if (native->u.syment.n_sclass == C_FILE
838 && native->u.syment.n_numaux > 0)
840 unsigned int filnmlen;
842 if (bfd_coff_force_symnames_in_strings (abfd))
844 native->u.syment._n._n_n._n_offset =
845 (*string_size_p + STRING_SIZE_SIZE);
846 native->u.syment._n._n_n._n_zeroes = 0;
847 *string_size_p += 6; /* strlen(".file") + 1 */
849 else
850 strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
852 auxent = &(native + 1)->u.auxent;
854 filnmlen = bfd_coff_filnmlen (abfd);
856 if (bfd_coff_long_filenames (abfd))
858 if (name_length <= filnmlen)
860 strncpy (auxent->x_file.x_fname, name, filnmlen);
862 else
864 auxent->x_file.x_n.x_offset = *string_size_p + STRING_SIZE_SIZE;
865 auxent->x_file.x_n.x_zeroes = 0;
866 *string_size_p += name_length + 1;
869 else
871 strncpy (auxent->x_file.x_fname, name, filnmlen);
872 if (name_length > filnmlen)
873 name[filnmlen] = '\0';
876 else
878 if (name_length <= SYMNMLEN && !bfd_coff_force_symnames_in_strings (abfd))
880 /* This name will fit into the symbol neatly */
881 strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
883 else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
885 native->u.syment._n._n_n._n_offset = (*string_size_p
886 + STRING_SIZE_SIZE);
887 native->u.syment._n._n_n._n_zeroes = 0;
888 *string_size_p += name_length + 1;
890 else
892 file_ptr filepos;
893 bfd_byte buf[4];
894 int prefix_len = bfd_coff_debug_string_prefix_length (abfd);
896 /* This name should be written into the .debug section. For
897 some reason each name is preceded by a two byte length
898 and also followed by a null byte. FIXME: We assume that
899 the .debug section has already been created, and that it
900 is large enough. */
901 if (*debug_string_section_p == (asection *) NULL)
902 *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
903 filepos = bfd_tell (abfd);
904 if (prefix_len == 4)
905 bfd_put_32 (abfd, (bfd_vma) (name_length + 1), buf);
906 else
907 bfd_put_16 (abfd, (bfd_vma) (name_length + 1), buf);
909 if (!bfd_set_section_contents (abfd,
910 *debug_string_section_p,
911 (PTR) buf,
912 (file_ptr) *debug_string_size_p,
913 (bfd_size_type) prefix_len)
914 || !bfd_set_section_contents (abfd,
915 *debug_string_section_p,
916 (PTR) symbol->name,
917 (file_ptr) (*debug_string_size_p
918 + prefix_len),
919 (bfd_size_type) name_length + 1))
920 abort ();
921 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
922 abort ();
923 native->u.syment._n._n_n._n_offset =
924 *debug_string_size_p + prefix_len;
925 native->u.syment._n._n_n._n_zeroes = 0;
926 *debug_string_size_p += name_length + 1 + prefix_len;
931 /* We need to keep track of the symbol index so that when we write out
932 the relocs we can get the index for a symbol. This method is a
933 hack. FIXME. */
935 #define set_index(symbol, idx) ((symbol)->udata.i = (idx))
937 /* Write a symbol out to a COFF file. */
939 static boolean
940 coff_write_symbol (abfd, symbol, native, written, string_size_p,
941 debug_string_section_p, debug_string_size_p)
942 bfd *abfd;
943 asymbol *symbol;
944 combined_entry_type *native;
945 bfd_vma *written;
946 bfd_size_type *string_size_p;
947 asection **debug_string_section_p;
948 bfd_size_type *debug_string_size_p;
950 unsigned int numaux = native->u.syment.n_numaux;
951 int type = native->u.syment.n_type;
952 int class = native->u.syment.n_sclass;
953 PTR buf;
954 bfd_size_type symesz;
956 if (native->u.syment.n_sclass == C_FILE)
957 symbol->flags |= BSF_DEBUGGING;
959 if (symbol->flags & BSF_DEBUGGING
960 && bfd_is_abs_section (symbol->section))
962 native->u.syment.n_scnum = N_DEBUG;
964 else if (bfd_is_abs_section (symbol->section))
966 native->u.syment.n_scnum = N_ABS;
968 else if (bfd_is_und_section (symbol->section))
970 native->u.syment.n_scnum = N_UNDEF;
972 else
974 native->u.syment.n_scnum =
975 symbol->section->output_section->target_index;
978 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
979 debug_string_section_p, debug_string_size_p);
981 symesz = bfd_coff_symesz (abfd);
982 buf = bfd_alloc (abfd, symesz);
983 if (!buf)
984 return false;
985 bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
986 if (bfd_bwrite (buf, symesz, abfd) != symesz)
987 return false;
988 bfd_release (abfd, buf);
990 if (native->u.syment.n_numaux > 0)
992 bfd_size_type auxesz;
993 unsigned int j;
995 auxesz = bfd_coff_auxesz (abfd);
996 buf = bfd_alloc (abfd, auxesz);
997 if (!buf)
998 return false;
999 for (j = 0; j < native->u.syment.n_numaux; j++)
1001 bfd_coff_swap_aux_out (abfd,
1002 &((native + j + 1)->u.auxent),
1003 type,
1004 class,
1005 (int) j,
1006 native->u.syment.n_numaux,
1007 buf);
1008 if (bfd_bwrite (buf, auxesz, abfd) != auxesz)
1009 return false;
1011 bfd_release (abfd, buf);
1014 /* Store the index for use when we write out the relocs. */
1015 set_index (symbol, *written);
1017 *written += numaux + 1;
1018 return true;
1021 /* Write out a symbol to a COFF file that does not come from a COFF
1022 file originally. This symbol may have been created by the linker,
1023 or we may be linking a non COFF file to a COFF file. */
1025 static boolean
1026 coff_write_alien_symbol (abfd, symbol, written, string_size_p,
1027 debug_string_section_p, debug_string_size_p)
1028 bfd *abfd;
1029 asymbol *symbol;
1030 bfd_vma *written;
1031 bfd_size_type *string_size_p;
1032 asection **debug_string_section_p;
1033 bfd_size_type *debug_string_size_p;
1035 combined_entry_type *native;
1036 combined_entry_type dummy;
1038 native = &dummy;
1039 native->u.syment.n_type = T_NULL;
1040 native->u.syment.n_flags = 0;
1041 if (bfd_is_und_section (symbol->section))
1043 native->u.syment.n_scnum = N_UNDEF;
1044 native->u.syment.n_value = symbol->value;
1046 else if (bfd_is_com_section (symbol->section))
1048 native->u.syment.n_scnum = N_UNDEF;
1049 native->u.syment.n_value = symbol->value;
1051 else if (symbol->flags & BSF_DEBUGGING)
1053 /* There isn't much point to writing out a debugging symbol
1054 unless we are prepared to convert it into COFF debugging
1055 format. So, we just ignore them. We must clobber the symbol
1056 name to keep it from being put in the string table. */
1057 symbol->name = "";
1058 return true;
1060 else
1062 native->u.syment.n_scnum =
1063 symbol->section->output_section->target_index;
1064 native->u.syment.n_value = (symbol->value
1065 + symbol->section->output_offset);
1066 if (! obj_pe (abfd))
1067 native->u.syment.n_value += symbol->section->output_section->vma;
1069 /* Copy the any flags from the file header into the symbol.
1070 FIXME: Why? */
1072 coff_symbol_type *c = coff_symbol_from (abfd, symbol);
1073 if (c != (coff_symbol_type *) NULL)
1074 native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
1078 native->u.syment.n_type = 0;
1079 if (symbol->flags & BSF_LOCAL)
1080 native->u.syment.n_sclass = C_STAT;
1081 else if (symbol->flags & BSF_WEAK)
1082 native->u.syment.n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1083 else
1084 native->u.syment.n_sclass = C_EXT;
1085 native->u.syment.n_numaux = 0;
1087 return coff_write_symbol (abfd, symbol, native, written, string_size_p,
1088 debug_string_section_p, debug_string_size_p);
1091 /* Write a native symbol to a COFF file. */
1093 static boolean
1094 coff_write_native_symbol (abfd, symbol, written, string_size_p,
1095 debug_string_section_p, debug_string_size_p)
1096 bfd *abfd;
1097 coff_symbol_type *symbol;
1098 bfd_vma *written;
1099 bfd_size_type *string_size_p;
1100 asection **debug_string_section_p;
1101 bfd_size_type *debug_string_size_p;
1103 combined_entry_type *native = symbol->native;
1104 alent *lineno = symbol->lineno;
1106 /* If this symbol has an associated line number, we must store the
1107 symbol index in the line number field. We also tag the auxent to
1108 point to the right place in the lineno table. */
1109 if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
1111 unsigned int count = 0;
1112 lineno[count].u.offset = *written;
1113 if (native->u.syment.n_numaux)
1115 union internal_auxent *a = &((native + 1)->u.auxent);
1117 a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
1118 symbol->symbol.section->output_section->moving_line_filepos;
1121 /* Count and relocate all other linenumbers. */
1122 count++;
1123 while (lineno[count].line_number != 0)
1125 #if 0
1126 /* 13 april 92. sac
1127 I've been told this, but still need proof:
1128 > The second bug is also in `bfd/coffcode.h'. This bug
1129 > causes the linker to screw up the pc-relocations for
1130 > all the line numbers in COFF code. This bug isn't only
1131 > specific to A29K implementations, but affects all
1132 > systems using COFF format binaries. Note that in COFF
1133 > object files, the line number core offsets output by
1134 > the assembler are relative to the start of each
1135 > procedure, not to the start of the .text section. This
1136 > patch relocates the line numbers relative to the
1137 > `native->u.syment.n_value' instead of the section
1138 > virtual address.
1139 > modular!olson@cs.arizona.edu (Jon Olson)
1141 lineno[count].u.offset += native->u.syment.n_value;
1142 #else
1143 lineno[count].u.offset +=
1144 (symbol->symbol.section->output_section->vma
1145 + symbol->symbol.section->output_offset);
1146 #endif
1147 count++;
1149 symbol->done_lineno = true;
1151 if (! bfd_is_const_section (symbol->symbol.section->output_section))
1152 symbol->symbol.section->output_section->moving_line_filepos +=
1153 count * bfd_coff_linesz (abfd);
1156 return coff_write_symbol (abfd, &(symbol->symbol), native, written,
1157 string_size_p, debug_string_section_p,
1158 debug_string_size_p);
1161 /* Write out the COFF symbols. */
1163 boolean
1164 coff_write_symbols (abfd)
1165 bfd *abfd;
1167 bfd_size_type string_size;
1168 asection *debug_string_section;
1169 bfd_size_type debug_string_size;
1170 unsigned int i;
1171 unsigned int limit = bfd_get_symcount (abfd);
1172 bfd_signed_vma written = 0;
1173 asymbol **p;
1175 string_size = 0;
1176 debug_string_section = NULL;
1177 debug_string_size = 0;
1179 /* If this target supports long section names, they must be put into
1180 the string table. This is supported by PE. This code must
1181 handle section names just as they are handled in
1182 coff_write_object_contents. */
1183 if (bfd_coff_long_section_names (abfd))
1185 asection *o;
1187 for (o = abfd->sections; o != NULL; o = o->next)
1189 size_t len;
1191 len = strlen (o->name);
1192 if (len > SCNNMLEN)
1193 string_size += len + 1;
1197 /* Seek to the right place */
1198 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1199 return false;
1201 /* Output all the symbols we have */
1203 written = 0;
1204 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
1206 asymbol *symbol = *p;
1207 coff_symbol_type *c_symbol = coff_symbol_from (abfd, symbol);
1209 if (c_symbol == (coff_symbol_type *) NULL
1210 || c_symbol->native == (combined_entry_type *) NULL)
1212 if (!coff_write_alien_symbol (abfd, symbol, &written, &string_size,
1213 &debug_string_section,
1214 &debug_string_size))
1215 return false;
1217 else
1219 if (!coff_write_native_symbol (abfd, c_symbol, &written,
1220 &string_size, &debug_string_section,
1221 &debug_string_size))
1222 return false;
1226 obj_raw_syment_count (abfd) = written;
1228 /* Now write out strings */
1230 if (string_size != 0)
1232 unsigned int size = string_size + STRING_SIZE_SIZE;
1233 bfd_byte buffer[STRING_SIZE_SIZE];
1235 #if STRING_SIZE_SIZE == 4
1236 H_PUT_32 (abfd, size, buffer);
1237 #else
1238 #error Change H_PUT_32
1239 #endif
1240 if (bfd_bwrite ((PTR) buffer, (bfd_size_type) sizeof (buffer), abfd)
1241 != sizeof (buffer))
1242 return false;
1244 /* Handle long section names. This code must handle section
1245 names just as they are handled in coff_write_object_contents. */
1246 if (bfd_coff_long_section_names (abfd))
1248 asection *o;
1250 for (o = abfd->sections; o != NULL; o = o->next)
1252 size_t len;
1254 len = strlen (o->name);
1255 if (len > SCNNMLEN)
1257 if (bfd_bwrite (o->name, (bfd_size_type) (len + 1), abfd)
1258 != len + 1)
1259 return false;
1264 for (p = abfd->outsymbols, i = 0;
1265 i < limit;
1266 i++, p++)
1268 asymbol *q = *p;
1269 size_t name_length = strlen (q->name);
1270 coff_symbol_type *c_symbol = coff_symbol_from (abfd, q);
1271 size_t maxlen;
1273 /* Figure out whether the symbol name should go in the string
1274 table. Symbol names that are short enough are stored
1275 directly in the syment structure. File names permit a
1276 different, longer, length in the syment structure. On
1277 XCOFF, some symbol names are stored in the .debug section
1278 rather than in the string table. */
1280 if (c_symbol == NULL
1281 || c_symbol->native == NULL)
1283 /* This is not a COFF symbol, so it certainly is not a
1284 file name, nor does it go in the .debug section. */
1285 maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
1287 else if (bfd_coff_symname_in_debug (abfd,
1288 &c_symbol->native->u.syment))
1290 /* This symbol name is in the XCOFF .debug section.
1291 Don't write it into the string table. */
1292 maxlen = name_length;
1294 else if (c_symbol->native->u.syment.n_sclass == C_FILE
1295 && c_symbol->native->u.syment.n_numaux > 0)
1297 if (bfd_coff_force_symnames_in_strings (abfd))
1299 if (bfd_bwrite (".file", (bfd_size_type) 6, abfd) != 6)
1300 return false;
1302 maxlen = bfd_coff_filnmlen (abfd);
1304 else
1305 maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
1307 if (name_length > maxlen)
1309 if (bfd_bwrite ((PTR) (q->name), (bfd_size_type) name_length + 1,
1310 abfd) != name_length + 1)
1311 return false;
1315 else
1317 /* We would normally not write anything here, but we'll write
1318 out 4 so that any stupid coff reader which tries to read the
1319 string table even when there isn't one won't croak. */
1320 unsigned int size = STRING_SIZE_SIZE;
1321 bfd_byte buffer[STRING_SIZE_SIZE];
1323 #if STRING_SIZE_SIZE == 4
1324 H_PUT_32 (abfd, size, buffer);
1325 #else
1326 #error Change H_PUT_32
1327 #endif
1328 if (bfd_bwrite ((PTR) buffer, (bfd_size_type) STRING_SIZE_SIZE, abfd)
1329 != STRING_SIZE_SIZE)
1330 return false;
1333 /* Make sure the .debug section was created to be the correct size.
1334 We should create it ourselves on the fly, but we don't because
1335 BFD won't let us write to any section until we know how large all
1336 the sections are. We could still do it by making another pass
1337 over the symbols. FIXME. */
1338 BFD_ASSERT (debug_string_size == 0
1339 || (debug_string_section != (asection *) NULL
1340 && (BFD_ALIGN (debug_string_size,
1341 1 << debug_string_section->alignment_power)
1342 == bfd_section_size (abfd, debug_string_section))));
1344 return true;
1347 boolean
1348 coff_write_linenumbers (abfd)
1349 bfd *abfd;
1351 asection *s;
1352 bfd_size_type linesz;
1353 PTR buff;
1355 linesz = bfd_coff_linesz (abfd);
1356 buff = bfd_alloc (abfd, linesz);
1357 if (!buff)
1358 return false;
1359 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1361 if (s->lineno_count)
1363 asymbol **q = abfd->outsymbols;
1364 if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
1365 return false;
1366 /* Find all the linenumbers in this section */
1367 while (*q)
1369 asymbol *p = *q;
1370 if (p->section->output_section == s)
1372 alent *l =
1373 BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
1374 (bfd_asymbol_bfd (p), p));
1375 if (l)
1377 /* Found a linenumber entry, output */
1378 struct internal_lineno out;
1379 memset ((PTR) & out, 0, sizeof (out));
1380 out.l_lnno = 0;
1381 out.l_addr.l_symndx = l->u.offset;
1382 bfd_coff_swap_lineno_out (abfd, &out, buff);
1383 if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd)
1384 != linesz)
1385 return false;
1386 l++;
1387 while (l->line_number)
1389 out.l_lnno = l->line_number;
1390 out.l_addr.l_symndx = l->u.offset;
1391 bfd_coff_swap_lineno_out (abfd, &out, buff);
1392 if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd)
1393 != linesz)
1394 return false;
1395 l++;
1399 q++;
1403 bfd_release (abfd, buff);
1404 return true;
1407 alent *
1408 coff_get_lineno (ignore_abfd, symbol)
1409 bfd *ignore_abfd ATTRIBUTE_UNUSED;
1410 asymbol *symbol;
1412 return coffsymbol (symbol)->lineno;
1415 #if 0
1417 /* This is only called from coff_add_missing_symbols, which has been
1418 disabled. */
1420 asymbol *
1421 coff_section_symbol (abfd, name)
1422 bfd *abfd;
1423 char *name;
1425 asection *sec = bfd_make_section_old_way (abfd, name);
1426 asymbol *sym;
1427 combined_entry_type *csym;
1429 sym = sec->symbol;
1430 csym = coff_symbol_from (abfd, sym)->native;
1431 /* Make sure back-end COFF stuff is there. */
1432 if (csym == 0)
1434 struct foo
1436 coff_symbol_type sym;
1437 /* @@FIXME This shouldn't use a fixed size!! */
1438 combined_entry_type e[10];
1440 struct foo *f;
1442 f = (struct foo *) bfd_zalloc (abfd, (bfd_size_type) sizeof (*f));
1443 if (!f)
1445 bfd_set_error (bfd_error_no_error);
1446 return NULL;
1448 coff_symbol_from (abfd, sym)->native = csym = f->e;
1450 csym[0].u.syment.n_sclass = C_STAT;
1451 csym[0].u.syment.n_numaux = 1;
1452 /* SF_SET_STATICS (sym); @@ ??? */
1453 csym[1].u.auxent.x_scn.x_scnlen = sec->_raw_size;
1454 csym[1].u.auxent.x_scn.x_nreloc = sec->reloc_count;
1455 csym[1].u.auxent.x_scn.x_nlinno = sec->lineno_count;
1457 if (sec->output_section == NULL)
1459 sec->output_section = sec;
1460 sec->output_offset = 0;
1463 return sym;
1466 #endif /* 0 */
1468 /* This function transforms the offsets into the symbol table into
1469 pointers to syments. */
1471 static void
1472 coff_pointerize_aux (abfd, table_base, symbol, indaux, auxent)
1473 bfd *abfd;
1474 combined_entry_type *table_base;
1475 combined_entry_type *symbol;
1476 unsigned int indaux;
1477 combined_entry_type *auxent;
1479 unsigned int type = symbol->u.syment.n_type;
1480 unsigned int class = symbol->u.syment.n_sclass;
1482 if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1484 if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1485 (abfd, table_base, symbol, indaux, auxent))
1486 return;
1489 /* Don't bother if this is a file or a section */
1490 if (class == C_STAT && type == T_NULL)
1491 return;
1492 if (class == C_FILE)
1493 return;
1495 /* Otherwise patch up */
1496 #define N_TMASK coff_data (abfd)->local_n_tmask
1497 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1498 if ((ISFCN (type) || ISTAG (class) || class == C_BLOCK || class == C_FCN)
1499 && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l > 0)
1501 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
1502 table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
1503 auxent->fix_end = 1;
1505 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1506 generate one, so we must be careful to ignore it. */
1507 if (auxent->u.auxent.x_sym.x_tagndx.l > 0)
1509 auxent->u.auxent.x_sym.x_tagndx.p =
1510 table_base + auxent->u.auxent.x_sym.x_tagndx.l;
1511 auxent->fix_tag = 1;
1515 /* Allocate space for the ".debug" section, and read it.
1516 We did not read the debug section until now, because
1517 we didn't want to go to the trouble until someone needed it. */
1519 static char *
1520 build_debug_section (abfd)
1521 bfd *abfd;
1523 char *debug_section;
1524 file_ptr position;
1525 bfd_size_type sec_size;
1527 asection *sect = bfd_get_section_by_name (abfd, ".debug");
1529 if (!sect)
1531 bfd_set_error (bfd_error_no_debug_section);
1532 return NULL;
1535 sec_size = bfd_get_section_size_before_reloc (sect);
1536 debug_section = (PTR) bfd_alloc (abfd, sec_size);
1537 if (debug_section == NULL)
1538 return NULL;
1540 /* Seek to the beginning of the `.debug' section and read it.
1541 Save the current position first; it is needed by our caller.
1542 Then read debug section and reset the file pointer. */
1544 position = bfd_tell (abfd);
1545 if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0
1546 || bfd_bread (debug_section, sec_size, abfd) != sec_size
1547 || bfd_seek (abfd, position, SEEK_SET) != 0)
1548 return NULL;
1549 return debug_section;
1552 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1553 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1554 be \0-terminated. */
1555 static char *
1556 copy_name (abfd, name, maxlen)
1557 bfd *abfd;
1558 char *name;
1559 size_t maxlen;
1561 size_t len;
1562 char *newname;
1564 for (len = 0; len < maxlen; ++len)
1566 if (name[len] == '\0')
1568 break;
1572 if ((newname = (PTR) bfd_alloc (abfd, (bfd_size_type) len + 1)) == NULL)
1573 return (NULL);
1574 strncpy (newname, name, len);
1575 newname[len] = '\0';
1576 return newname;
1579 /* Read in the external symbols. */
1581 boolean
1582 _bfd_coff_get_external_symbols (abfd)
1583 bfd *abfd;
1585 bfd_size_type symesz;
1586 bfd_size_type size;
1587 PTR syms;
1589 if (obj_coff_external_syms (abfd) != NULL)
1590 return true;
1592 symesz = bfd_coff_symesz (abfd);
1594 size = obj_raw_syment_count (abfd) * symesz;
1596 syms = (PTR) bfd_malloc (size);
1597 if (syms == NULL && size != 0)
1598 return false;
1600 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
1601 || bfd_bread (syms, size, abfd) != size)
1603 if (syms != NULL)
1604 free (syms);
1605 return false;
1608 obj_coff_external_syms (abfd) = syms;
1610 return true;
1613 /* Read in the external strings. The strings are not loaded until
1614 they are needed. This is because we have no simple way of
1615 detecting a missing string table in an archive. */
1617 const char *
1618 _bfd_coff_read_string_table (abfd)
1619 bfd *abfd;
1621 char extstrsize[STRING_SIZE_SIZE];
1622 bfd_size_type strsize;
1623 char *strings;
1624 file_ptr pos;
1626 if (obj_coff_strings (abfd) != NULL)
1627 return obj_coff_strings (abfd);
1629 if (obj_sym_filepos (abfd) == 0)
1631 bfd_set_error (bfd_error_no_symbols);
1632 return NULL;
1635 pos = obj_sym_filepos (abfd);
1636 pos += obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd);
1637 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
1638 return NULL;
1640 if (bfd_bread (extstrsize, (bfd_size_type) sizeof extstrsize, abfd)
1641 != sizeof extstrsize)
1643 if (bfd_get_error () != bfd_error_file_truncated)
1644 return NULL;
1646 /* There is no string table. */
1647 strsize = STRING_SIZE_SIZE;
1649 else
1651 #if STRING_SIZE_SIZE == 4
1652 strsize = H_GET_32 (abfd, extstrsize);
1653 #else
1654 #error Change H_GET_32
1655 #endif
1658 if (strsize < STRING_SIZE_SIZE)
1660 (*_bfd_error_handler)
1661 (_("%s: bad string table size %lu"), bfd_archive_filename (abfd),
1662 (unsigned long) strsize);
1663 bfd_set_error (bfd_error_bad_value);
1664 return NULL;
1667 strings = (char *) bfd_malloc (strsize);
1668 if (strings == NULL)
1669 return NULL;
1671 if (bfd_bread (strings + STRING_SIZE_SIZE, strsize - STRING_SIZE_SIZE, abfd)
1672 != strsize - STRING_SIZE_SIZE)
1674 free (strings);
1675 return NULL;
1678 obj_coff_strings (abfd) = strings;
1680 return strings;
1683 /* Free up the external symbols and strings read from a COFF file. */
1685 boolean
1686 _bfd_coff_free_symbols (abfd)
1687 bfd *abfd;
1689 if (obj_coff_external_syms (abfd) != NULL
1690 && ! obj_coff_keep_syms (abfd))
1692 free (obj_coff_external_syms (abfd));
1693 obj_coff_external_syms (abfd) = NULL;
1695 if (obj_coff_strings (abfd) != NULL
1696 && ! obj_coff_keep_strings (abfd))
1698 free (obj_coff_strings (abfd));
1699 obj_coff_strings (abfd) = NULL;
1701 return true;
1704 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1705 knit the symbol names into a normalized form. By normalized here I
1706 mean that all symbols have an n_offset pointer that points to a null-
1707 terminated string. */
1709 combined_entry_type *
1710 coff_get_normalized_symtab (abfd)
1711 bfd *abfd;
1713 combined_entry_type *internal;
1714 combined_entry_type *internal_ptr;
1715 combined_entry_type *symbol_ptr;
1716 combined_entry_type *internal_end;
1717 size_t symesz;
1718 char *raw_src;
1719 char *raw_end;
1720 const char *string_table = NULL;
1721 char *debug_section = NULL;
1722 bfd_size_type size;
1724 if (obj_raw_syments (abfd) != NULL)
1725 return obj_raw_syments (abfd);
1727 size = obj_raw_syment_count (abfd) * sizeof (combined_entry_type);
1728 internal = (combined_entry_type *) bfd_zalloc (abfd, size);
1729 if (internal == NULL && size != 0)
1730 return NULL;
1731 internal_end = internal + obj_raw_syment_count (abfd);
1733 if (! _bfd_coff_get_external_symbols (abfd))
1734 return NULL;
1736 raw_src = (char *) obj_coff_external_syms (abfd);
1738 /* mark the end of the symbols */
1739 symesz = bfd_coff_symesz (abfd);
1740 raw_end = (char *) raw_src + obj_raw_syment_count (abfd) * symesz;
1742 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1743 probably possible. If one shows up, it will probably kill us. */
1745 /* Swap all the raw entries */
1746 for (internal_ptr = internal;
1747 raw_src < raw_end;
1748 raw_src += symesz, internal_ptr++)
1751 unsigned int i;
1752 bfd_coff_swap_sym_in (abfd, (PTR) raw_src,
1753 (PTR) & internal_ptr->u.syment);
1754 symbol_ptr = internal_ptr;
1756 for (i = 0;
1757 i < symbol_ptr->u.syment.n_numaux;
1758 i++)
1760 internal_ptr++;
1761 raw_src += symesz;
1762 bfd_coff_swap_aux_in (abfd, (PTR) raw_src,
1763 symbol_ptr->u.syment.n_type,
1764 symbol_ptr->u.syment.n_sclass,
1765 (int) i, symbol_ptr->u.syment.n_numaux,
1766 &(internal_ptr->u.auxent));
1767 coff_pointerize_aux (abfd, internal, symbol_ptr, i,
1768 internal_ptr);
1772 /* Free the raw symbols, but not the strings (if we have them). */
1773 obj_coff_keep_strings (abfd) = true;
1774 if (! _bfd_coff_free_symbols (abfd))
1775 return NULL;
1777 for (internal_ptr = internal; internal_ptr < internal_end;
1778 internal_ptr++)
1780 if (internal_ptr->u.syment.n_sclass == C_FILE
1781 && internal_ptr->u.syment.n_numaux > 0)
1783 /* make a file symbol point to the name in the auxent, since
1784 the text ".file" is redundant */
1785 if ((internal_ptr + 1)->u.auxent.x_file.x_n.x_zeroes == 0)
1787 /* the filename is a long one, point into the string table */
1788 if (string_table == NULL)
1790 string_table = _bfd_coff_read_string_table (abfd);
1791 if (string_table == NULL)
1792 return NULL;
1795 internal_ptr->u.syment._n._n_n._n_offset =
1796 ((long)
1797 (string_table
1798 + (internal_ptr + 1)->u.auxent.x_file.x_n.x_offset));
1800 else
1802 /* Ordinary short filename, put into memory anyway. The
1803 Microsoft PE tools sometimes store a filename in
1804 multiple AUX entries. */
1805 if (internal_ptr->u.syment.n_numaux > 1
1806 && coff_data (abfd)->pe)
1808 internal_ptr->u.syment._n._n_n._n_offset =
1809 ((long)
1810 copy_name (abfd,
1811 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1812 internal_ptr->u.syment.n_numaux * symesz));
1814 else
1816 internal_ptr->u.syment._n._n_n._n_offset =
1817 ((long)
1818 copy_name (abfd,
1819 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1820 (size_t) bfd_coff_filnmlen (abfd)));
1824 else
1826 if (internal_ptr->u.syment._n._n_n._n_zeroes != 0)
1828 /* This is a "short" name. Make it long. */
1829 size_t i;
1830 char *newstring;
1832 /* find the length of this string without walking into memory
1833 that isn't ours. */
1834 for (i = 0; i < 8; ++i)
1835 if (internal_ptr->u.syment._n._n_name[i] == '\0')
1836 break;
1838 newstring = (PTR) bfd_zalloc (abfd, (bfd_size_type) (i + 1));
1839 if (newstring == NULL)
1840 return (NULL);
1841 strncpy (newstring, internal_ptr->u.syment._n._n_name, i);
1842 internal_ptr->u.syment._n._n_n._n_offset = (long int) newstring;
1843 internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1845 else if (internal_ptr->u.syment._n._n_n._n_offset == 0)
1846 internal_ptr->u.syment._n._n_n._n_offset = (long int) "";
1847 else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment))
1849 /* Long name already. Point symbol at the string in the
1850 table. */
1851 if (string_table == NULL)
1853 string_table = _bfd_coff_read_string_table (abfd);
1854 if (string_table == NULL)
1855 return NULL;
1857 internal_ptr->u.syment._n._n_n._n_offset =
1858 ((long int)
1859 (string_table
1860 + internal_ptr->u.syment._n._n_n._n_offset));
1862 else
1864 /* Long name in debug section. Very similar. */
1865 if (debug_section == NULL)
1866 debug_section = build_debug_section (abfd);
1867 internal_ptr->u.syment._n._n_n._n_offset = (long int)
1868 (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
1871 internal_ptr += internal_ptr->u.syment.n_numaux;
1874 obj_raw_syments (abfd) = internal;
1875 BFD_ASSERT (obj_raw_syment_count (abfd)
1876 == (unsigned int) (internal_ptr - internal));
1878 return (internal);
1879 } /* coff_get_normalized_symtab() */
1881 long
1882 coff_get_reloc_upper_bound (abfd, asect)
1883 bfd *abfd;
1884 sec_ptr asect;
1886 if (bfd_get_format (abfd) != bfd_object)
1888 bfd_set_error (bfd_error_invalid_operation);
1889 return -1;
1891 return (asect->reloc_count + 1) * sizeof (arelent *);
1894 asymbol *
1895 coff_make_empty_symbol (abfd)
1896 bfd *abfd;
1898 bfd_size_type amt = sizeof (coff_symbol_type);
1899 coff_symbol_type *new = (coff_symbol_type *) bfd_zalloc (abfd, amt);
1900 if (new == NULL)
1901 return (NULL);
1902 new->symbol.section = 0;
1903 new->native = 0;
1904 new->lineno = (alent *) NULL;
1905 new->done_lineno = false;
1906 new->symbol.the_bfd = abfd;
1907 return &new->symbol;
1910 /* Make a debugging symbol. */
1912 asymbol *
1913 coff_bfd_make_debug_symbol (abfd, ptr, sz)
1914 bfd *abfd;
1915 PTR ptr ATTRIBUTE_UNUSED;
1916 unsigned long sz ATTRIBUTE_UNUSED;
1918 bfd_size_type amt = sizeof (coff_symbol_type);
1919 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, amt);
1920 if (new == NULL)
1921 return (NULL);
1922 /* @@ The 10 is a guess at a plausible maximum number of aux entries
1923 (but shouldn't be a constant). */
1924 amt = sizeof (combined_entry_type) * 10;
1925 new->native = (combined_entry_type *) bfd_zalloc (abfd, amt);
1926 if (!new->native)
1927 return (NULL);
1928 new->symbol.section = bfd_abs_section_ptr;
1929 new->symbol.flags = BSF_DEBUGGING;
1930 new->lineno = (alent *) NULL;
1931 new->done_lineno = false;
1932 new->symbol.the_bfd = abfd;
1933 return &new->symbol;
1936 void
1937 coff_get_symbol_info (abfd, symbol, ret)
1938 bfd *abfd;
1939 asymbol *symbol;
1940 symbol_info *ret;
1942 bfd_symbol_info (symbol, ret);
1943 if (coffsymbol (symbol)->native != NULL
1944 && coffsymbol (symbol)->native->fix_value)
1946 ret->value = coffsymbol (symbol)->native->u.syment.n_value -
1947 (unsigned long) obj_raw_syments (abfd);
1951 /* Return the COFF syment for a symbol. */
1953 boolean
1954 bfd_coff_get_syment (abfd, symbol, psyment)
1955 bfd *abfd;
1956 asymbol *symbol;
1957 struct internal_syment *psyment;
1959 coff_symbol_type *csym;
1961 csym = coff_symbol_from (abfd, symbol);
1962 if (csym == NULL || csym->native == NULL)
1964 bfd_set_error (bfd_error_invalid_operation);
1965 return false;
1968 *psyment = csym->native->u.syment;
1970 if (csym->native->fix_value)
1971 psyment->n_value = psyment->n_value -
1972 (unsigned long) obj_raw_syments (abfd);
1974 /* FIXME: We should handle fix_line here. */
1976 return true;
1979 /* Return the COFF auxent for a symbol. */
1981 boolean
1982 bfd_coff_get_auxent (abfd, symbol, indx, pauxent)
1983 bfd *abfd;
1984 asymbol *symbol;
1985 int indx;
1986 union internal_auxent *pauxent;
1988 coff_symbol_type *csym;
1989 combined_entry_type *ent;
1991 csym = coff_symbol_from (abfd, symbol);
1993 if (csym == NULL
1994 || csym->native == NULL
1995 || indx >= csym->native->u.syment.n_numaux)
1997 bfd_set_error (bfd_error_invalid_operation);
1998 return false;
2001 ent = csym->native + indx + 1;
2003 *pauxent = ent->u.auxent;
2005 if (ent->fix_tag)
2006 pauxent->x_sym.x_tagndx.l =
2007 ((combined_entry_type *) pauxent->x_sym.x_tagndx.p
2008 - obj_raw_syments (abfd));
2010 if (ent->fix_end)
2011 pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l =
2012 ((combined_entry_type *) pauxent->x_sym.x_fcnary.x_fcn.x_endndx.p
2013 - obj_raw_syments (abfd));
2015 if (ent->fix_scnlen)
2016 pauxent->x_csect.x_scnlen.l =
2017 ((combined_entry_type *) pauxent->x_csect.x_scnlen.p
2018 - obj_raw_syments (abfd));
2020 return true;
2023 /* Print out information about COFF symbol. */
2025 void
2026 coff_print_symbol (abfd, filep, symbol, how)
2027 bfd *abfd;
2028 PTR filep;
2029 asymbol *symbol;
2030 bfd_print_symbol_type how;
2032 FILE *file = (FILE *) filep;
2034 switch (how)
2036 case bfd_print_symbol_name:
2037 fprintf (file, "%s", symbol->name);
2038 break;
2040 case bfd_print_symbol_more:
2041 fprintf (file, "coff %s %s",
2042 coffsymbol (symbol)->native ? "n" : "g",
2043 coffsymbol (symbol)->lineno ? "l" : " ");
2044 break;
2046 case bfd_print_symbol_all:
2047 if (coffsymbol (symbol)->native)
2049 bfd_vma val;
2050 unsigned int aux;
2051 combined_entry_type *combined = coffsymbol (symbol)->native;
2052 combined_entry_type *root = obj_raw_syments (abfd);
2053 struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
2055 fprintf (file, "[%3ld]", (long) (combined - root));
2057 if (! combined->fix_value)
2058 val = (bfd_vma) combined->u.syment.n_value;
2059 else
2060 val = combined->u.syment.n_value - (unsigned long) root;
2062 #ifndef XCOFF64
2063 fprintf (file,
2064 "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x%08lx %s",
2065 combined->u.syment.n_scnum,
2066 combined->u.syment.n_flags,
2067 combined->u.syment.n_type,
2068 combined->u.syment.n_sclass,
2069 combined->u.syment.n_numaux,
2070 (unsigned long) val,
2071 symbol->name);
2072 #else
2073 /* Print out the wide, 64 bit, symbol value */
2074 fprintf (file,
2075 "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x%016llx %s",
2076 combined->u.syment.n_scnum,
2077 combined->u.syment.n_flags,
2078 combined->u.syment.n_type,
2079 combined->u.syment.n_sclass,
2080 combined->u.syment.n_numaux,
2081 val,
2082 symbol->name);
2083 #endif
2085 for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
2087 combined_entry_type *auxp = combined + aux + 1;
2088 long tagndx;
2090 if (auxp->fix_tag)
2091 tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
2092 else
2093 tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
2095 fprintf (file, "\n");
2097 if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux))
2098 continue;
2100 switch (combined->u.syment.n_sclass)
2102 case C_FILE:
2103 fprintf (file, "File ");
2104 break;
2106 case C_STAT:
2107 if (combined->u.syment.n_type == T_NULL)
2108 /* probably a section symbol? */
2110 fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
2111 (long) auxp->u.auxent.x_scn.x_scnlen,
2112 auxp->u.auxent.x_scn.x_nreloc,
2113 auxp->u.auxent.x_scn.x_nlinno);
2114 if (auxp->u.auxent.x_scn.x_checksum != 0
2115 || auxp->u.auxent.x_scn.x_associated != 0
2116 || auxp->u.auxent.x_scn.x_comdat != 0)
2117 fprintf (file, " checksum 0x%lx assoc %d comdat %d",
2118 auxp->u.auxent.x_scn.x_checksum,
2119 auxp->u.auxent.x_scn.x_associated,
2120 auxp->u.auxent.x_scn.x_comdat);
2121 break;
2123 /* else fall through */
2124 case C_EXT:
2125 if (ISFCN (combined->u.syment.n_type))
2127 long next, llnos;
2129 if (auxp->fix_end)
2130 next = (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2131 - root);
2132 else
2133 next = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
2134 llnos = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_lnnoptr;
2135 fprintf (file,
2136 "AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld",
2137 tagndx, auxp->u.auxent.x_sym.x_misc.x_fsize,
2138 llnos, next);
2139 break;
2141 /* else fall through */
2142 default:
2143 fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
2144 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
2145 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
2146 tagndx);
2147 if (auxp->fix_end)
2148 fprintf (file, " endndx %ld",
2149 ((long)
2150 (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2151 - root)));
2152 break;
2156 if (l)
2158 fprintf (file, "\n%s :", l->u.sym->name);
2159 l++;
2160 while (l->line_number)
2162 fprintf (file, "\n%4d : 0x%lx",
2163 l->line_number,
2164 ((unsigned long)
2165 (l->u.offset + symbol->section->vma)));
2166 l++;
2170 else
2172 bfd_print_symbol_vandf (abfd, (PTR) file, symbol);
2173 fprintf (file, " %-5s %s %s %s",
2174 symbol->section->name,
2175 coffsymbol (symbol)->native ? "n" : "g",
2176 coffsymbol (symbol)->lineno ? "l" : " ",
2177 symbol->name);
2182 /* Return whether a symbol name implies a local symbol. In COFF,
2183 local symbols generally start with ``.L''. Most targets use this
2184 function for the is_local_label_name entry point, but some may
2185 override it. */
2187 boolean
2188 _bfd_coff_is_local_label_name (abfd, name)
2189 bfd *abfd ATTRIBUTE_UNUSED;
2190 const char *name;
2192 return name[0] == '.' && name[1] == 'L';
2195 /* Provided a BFD, a section and an offset (in bytes, not octets) into the
2196 section, calculate and return the name of the source file and the line
2197 nearest to the wanted location. */
2199 boolean
2200 coff_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
2201 functionname_ptr, line_ptr)
2202 bfd *abfd;
2203 asection *section;
2204 asymbol **symbols;
2205 bfd_vma offset;
2206 const char **filename_ptr;
2207 const char **functionname_ptr;
2208 unsigned int *line_ptr;
2210 boolean found;
2211 unsigned int i;
2212 unsigned int line_base;
2213 coff_data_type *cof = coff_data (abfd);
2214 /* Run through the raw syments if available */
2215 combined_entry_type *p;
2216 combined_entry_type *pend;
2217 alent *l;
2218 struct coff_section_tdata *sec_data;
2219 bfd_size_type amt;
2221 /* Before looking through the symbol table, try to use a .stab
2222 section to find the information. */
2223 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
2224 &found, filename_ptr,
2225 functionname_ptr, line_ptr,
2226 &coff_data(abfd)->line_info))
2227 return false;
2229 if (found)
2230 return true;
2232 /* Also try examining DWARF2 debugging information. */
2233 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
2234 filename_ptr, functionname_ptr,
2235 line_ptr, 0,
2236 &coff_data(abfd)->dwarf2_find_line_info))
2237 return true;
2239 *filename_ptr = 0;
2240 *functionname_ptr = 0;
2241 *line_ptr = 0;
2243 /* Don't try and find line numbers in a non coff file */
2244 if (!bfd_family_coff (abfd))
2245 return false;
2247 if (cof == NULL)
2248 return false;
2250 /* Find the first C_FILE symbol. */
2251 p = cof->raw_syments;
2252 if (!p)
2253 return false;
2255 pend = p + cof->raw_syment_count;
2256 while (p < pend)
2258 if (p->u.syment.n_sclass == C_FILE)
2259 break;
2260 p += 1 + p->u.syment.n_numaux;
2263 if (p < pend)
2265 bfd_vma sec_vma;
2266 bfd_vma maxdiff;
2268 /* Look through the C_FILE symbols to find the best one. */
2269 sec_vma = bfd_get_section_vma (abfd, section);
2270 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2271 maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
2272 while (1)
2274 combined_entry_type *p2;
2276 for (p2 = p + 1 + p->u.syment.n_numaux;
2277 p2 < pend;
2278 p2 += 1 + p2->u.syment.n_numaux)
2280 if (p2->u.syment.n_scnum > 0
2281 && (section
2282 == coff_section_from_bfd_index (abfd,
2283 p2->u.syment.n_scnum)))
2284 break;
2285 if (p2->u.syment.n_sclass == C_FILE)
2287 p2 = pend;
2288 break;
2292 /* We use <= MAXDIFF here so that if we get a zero length
2293 file, we actually use the next file entry. */
2294 if (p2 < pend
2295 && offset + sec_vma >= (bfd_vma) p2->u.syment.n_value
2296 && offset + sec_vma - (bfd_vma) p2->u.syment.n_value <= maxdiff)
2298 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2299 maxdiff = offset + sec_vma - p2->u.syment.n_value;
2302 /* Avoid endless loops on erroneous files by ensuring that
2303 we always move forward in the file. */
2304 if (p >= cof->raw_syments + p->u.syment.n_value)
2305 break;
2307 p = cof->raw_syments + p->u.syment.n_value;
2308 if (p > pend || p->u.syment.n_sclass != C_FILE)
2309 break;
2313 /* Now wander though the raw linenumbers of the section */
2314 /* If we have been called on this section before, and the offset we
2315 want is further down then we can prime the lookup loop. */
2316 sec_data = coff_section_data (abfd, section);
2317 if (sec_data != NULL
2318 && sec_data->i > 0
2319 && offset >= sec_data->offset)
2321 i = sec_data->i;
2322 *functionname_ptr = sec_data->function;
2323 line_base = sec_data->line_base;
2325 else
2327 i = 0;
2328 line_base = 0;
2331 if (section->lineno != NULL)
2333 bfd_vma last_value = 0;
2335 l = &section->lineno[i];
2337 for (; i < section->lineno_count; i++)
2339 if (l->line_number == 0)
2341 /* Get the symbol this line number points at */
2342 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
2343 if (coff->symbol.value > offset)
2344 break;
2345 *functionname_ptr = coff->symbol.name;
2346 last_value = coff->symbol.value;
2347 if (coff->native)
2349 combined_entry_type *s = coff->native;
2350 s = s + 1 + s->u.syment.n_numaux;
2352 /* In XCOFF a debugging symbol can follow the
2353 function symbol. */
2354 if (s->u.syment.n_scnum == N_DEBUG)
2355 s = s + 1 + s->u.syment.n_numaux;
2357 /* S should now point to the .bf of the function. */
2358 if (s->u.syment.n_numaux)
2360 /* The linenumber is stored in the auxent. */
2361 union internal_auxent *a = &((s + 1)->u.auxent);
2362 line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
2363 *line_ptr = line_base;
2367 else
2369 if (l->u.offset > offset)
2370 break;
2371 *line_ptr = l->line_number + line_base - 1;
2373 l++;
2376 /* If we fell off the end of the loop, then assume that this
2377 symbol has no line number info. Otherwise, symbols with no
2378 line number info get reported with the line number of the
2379 last line of the last symbol which does have line number
2380 info. We use 0x100 as a slop to account for cases where the
2381 last line has executable code. */
2382 if (i >= section->lineno_count
2383 && last_value != 0
2384 && offset - last_value > 0x100)
2386 *functionname_ptr = NULL;
2387 *line_ptr = 0;
2391 /* Cache the results for the next call. */
2392 if (sec_data == NULL && section->owner == abfd)
2394 amt = sizeof (struct coff_section_tdata);
2395 section->used_by_bfd = (PTR) bfd_zalloc (abfd, amt);
2396 sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2398 if (sec_data != NULL)
2400 sec_data->offset = offset;
2401 sec_data->i = i;
2402 sec_data->function = *functionname_ptr;
2403 sec_data->line_base = line_base;
2406 return true;
2410 coff_sizeof_headers (abfd, reloc)
2411 bfd *abfd;
2412 boolean reloc;
2414 size_t size;
2416 if (! reloc)
2418 size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
2420 else
2422 size = bfd_coff_filhsz (abfd);
2425 size += abfd->section_count * bfd_coff_scnhsz (abfd);
2426 return size;
2429 /* Change the class of a coff symbol held by BFD. */
2430 boolean
2431 bfd_coff_set_symbol_class (abfd, symbol, class)
2432 bfd * abfd;
2433 asymbol * symbol;
2434 unsigned int class;
2436 coff_symbol_type * csym;
2438 csym = coff_symbol_from (abfd, symbol);
2439 if (csym == NULL)
2441 bfd_set_error (bfd_error_invalid_operation);
2442 return false;
2444 else if (csym->native == NULL)
2446 /* This is an alien symbol which no native coff backend data.
2447 We cheat here by creating a fake native entry for it and
2448 then filling in the class. This code is based on that in
2449 coff_write_alien_symbol(). */
2451 combined_entry_type * native;
2452 bfd_size_type amt = sizeof (* native);
2454 native = (combined_entry_type *) bfd_zalloc (abfd, amt);
2455 if (native == NULL)
2456 return false;
2458 native->u.syment.n_type = T_NULL;
2459 native->u.syment.n_sclass = class;
2461 if (bfd_is_und_section (symbol->section))
2463 native->u.syment.n_scnum = N_UNDEF;
2464 native->u.syment.n_value = symbol->value;
2466 else if (bfd_is_com_section (symbol->section))
2468 native->u.syment.n_scnum = N_UNDEF;
2469 native->u.syment.n_value = symbol->value;
2471 else
2473 native->u.syment.n_scnum =
2474 symbol->section->output_section->target_index;
2475 native->u.syment.n_value = (symbol->value
2476 + symbol->section->output_offset);
2477 if (! obj_pe (abfd))
2478 native->u.syment.n_value += symbol->section->output_section->vma;
2480 /* Copy the any flags from the file header into the symbol.
2481 FIXME: Why? */
2482 native->u.syment.n_flags = bfd_asymbol_bfd (& csym->symbol)->flags;
2485 csym->native = native;
2487 else
2489 csym->native->u.syment.n_sclass = class;
2492 return true;