Add field ``name'' to floatformat.
[binutils.git] / bfd / coffgen.c
blobbb7f86205dcf2ee297fecd6ac854c9431f9c23a5
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 *));
344 /* Canonicalize a COFF symbol table. */
346 long
347 coff_get_symtab (abfd, alocation)
348 bfd *abfd;
349 asymbol **alocation;
351 unsigned int counter;
352 coff_symbol_type *symbase;
353 coff_symbol_type **location = (coff_symbol_type **) alocation;
355 if (!bfd_coff_slurp_symbol_table (abfd))
356 return -1;
358 symbase = obj_symbols (abfd);
359 counter = bfd_get_symcount (abfd);
360 while (counter-- > 0)
361 *location++ = symbase++;
363 *location = NULL;
365 return bfd_get_symcount (abfd);
368 /* Get the name of a symbol. The caller must pass in a buffer of size
369 >= SYMNMLEN + 1. */
371 const char *
372 _bfd_coff_internal_syment_name (abfd, sym, buf)
373 bfd *abfd;
374 const struct internal_syment *sym;
375 char *buf;
377 /* FIXME: It's not clear this will work correctly if sizeof
378 (_n_zeroes) != 4. */
379 if (sym->_n._n_n._n_zeroes != 0
380 || sym->_n._n_n._n_offset == 0)
382 memcpy (buf, sym->_n._n_name, SYMNMLEN);
383 buf[SYMNMLEN] = '\0';
384 return buf;
386 else
388 const char *strings;
390 BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE);
391 strings = obj_coff_strings (abfd);
392 if (strings == NULL)
394 strings = _bfd_coff_read_string_table (abfd);
395 if (strings == NULL)
396 return NULL;
398 return strings + sym->_n._n_n._n_offset;
402 /* Read in and swap the relocs. This returns a buffer holding the
403 relocs for section SEC in file ABFD. If CACHE is true and
404 INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
405 the function is called again. If EXTERNAL_RELOCS is not NULL, it
406 is a buffer large enough to hold the unswapped relocs. If
407 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
408 the swapped relocs. If REQUIRE_INTERNAL is true, then the return
409 value must be INTERNAL_RELOCS. The function returns NULL on error. */
411 struct internal_reloc *
412 _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
413 require_internal, internal_relocs)
414 bfd *abfd;
415 asection *sec;
416 boolean cache;
417 bfd_byte *external_relocs;
418 boolean require_internal;
419 struct internal_reloc *internal_relocs;
421 bfd_size_type relsz;
422 bfd_byte *free_external = NULL;
423 struct internal_reloc *free_internal = NULL;
424 bfd_byte *erel;
425 bfd_byte *erel_end;
426 struct internal_reloc *irel;
428 if (coff_section_data (abfd, sec) != NULL
429 && coff_section_data (abfd, sec)->relocs != NULL)
431 if (! require_internal)
432 return coff_section_data (abfd, sec)->relocs;
433 memcpy (internal_relocs, coff_section_data (abfd, sec)->relocs,
434 sec->reloc_count * sizeof (struct internal_reloc));
435 return internal_relocs;
438 relsz = bfd_coff_relsz (abfd);
440 if (external_relocs == NULL)
442 free_external = (bfd_byte *) bfd_malloc (sec->reloc_count * relsz);
443 if (free_external == NULL && sec->reloc_count > 0)
444 goto error_return;
445 external_relocs = free_external;
448 if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
449 || (bfd_read (external_relocs, relsz, sec->reloc_count, abfd)
450 != relsz * sec->reloc_count))
451 goto error_return;
453 if (internal_relocs == NULL)
455 free_internal = ((struct internal_reloc *)
456 bfd_malloc (sec->reloc_count
457 * sizeof (struct internal_reloc)));
458 if (free_internal == NULL && sec->reloc_count > 0)
459 goto error_return;
460 internal_relocs = free_internal;
463 /* Swap in the relocs. */
464 erel = external_relocs;
465 erel_end = erel + relsz * sec->reloc_count;
466 irel = internal_relocs;
467 for (; erel < erel_end; erel += relsz, irel++)
468 bfd_coff_swap_reloc_in (abfd, (PTR) erel, (PTR) irel);
470 if (free_external != NULL)
472 free (free_external);
473 free_external = NULL;
476 if (cache && free_internal != NULL)
478 if (coff_section_data (abfd, sec) == NULL)
480 sec->used_by_bfd =
481 (PTR) bfd_zalloc (abfd,
482 sizeof (struct coff_section_tdata));
483 if (sec->used_by_bfd == NULL)
484 goto error_return;
485 coff_section_data (abfd, sec)->contents = NULL;
487 coff_section_data (abfd, sec)->relocs = free_internal;
490 return internal_relocs;
492 error_return:
493 if (free_external != NULL)
494 free (free_external);
495 if (free_internal != NULL)
496 free (free_internal);
497 return NULL;
500 /* Set lineno_count for the output sections of a COFF file. */
503 coff_count_linenumbers (abfd)
504 bfd *abfd;
506 unsigned int limit = bfd_get_symcount (abfd);
507 unsigned int i;
508 int total = 0;
509 asymbol **p;
510 asection *s;
512 if (limit == 0)
514 /* This may be from the backend linker, in which case the
515 lineno_count in the sections is correct. */
516 for (s = abfd->sections; s != NULL; s = s->next)
517 total += s->lineno_count;
518 return total;
521 for (s = abfd->sections; s != NULL; s = s->next)
522 BFD_ASSERT (s->lineno_count == 0);
524 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
526 asymbol *q_maybe = *p;
528 if (bfd_asymbol_flavour (q_maybe) == bfd_target_coff_flavour)
530 coff_symbol_type *q = coffsymbol (q_maybe);
532 /* The AIX 4.1 compiler can sometimes generate line numbers
533 attached to debugging symbols. We try to simply ignore
534 those here. */
535 if (q->lineno != NULL
536 && q->symbol.section->owner != NULL)
538 /* This symbol has line numbers. Increment the owning
539 section's linenumber count. */
540 alent *l = q->lineno;
542 ++q->symbol.section->output_section->lineno_count;
543 ++total;
544 ++l;
545 while (l->line_number != 0)
547 ++total;
548 ++q->symbol.section->output_section->lineno_count;
549 ++l;
555 return total;
558 /* Takes a bfd and a symbol, returns a pointer to the coff specific
559 area of the symbol if there is one. */
561 /*ARGSUSED*/
562 coff_symbol_type *
563 coff_symbol_from (ignore_abfd, symbol)
564 bfd *ignore_abfd ATTRIBUTE_UNUSED;
565 asymbol *symbol;
567 if (bfd_asymbol_flavour (symbol) != bfd_target_coff_flavour)
568 return (coff_symbol_type *) NULL;
570 if (bfd_asymbol_bfd (symbol)->tdata.coff_obj_data == (coff_data_type *) NULL)
571 return (coff_symbol_type *) NULL;
573 return (coff_symbol_type *) symbol;
576 static void
577 fixup_symbol_value (abfd, coff_symbol_ptr, syment)
578 bfd *abfd;
579 coff_symbol_type *coff_symbol_ptr;
580 struct internal_syment *syment;
583 /* Normalize the symbol flags */
584 if (bfd_is_com_section (coff_symbol_ptr->symbol.section))
586 /* a common symbol is undefined with a value */
587 syment->n_scnum = N_UNDEF;
588 syment->n_value = coff_symbol_ptr->symbol.value;
590 else if ((coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) != 0
591 && (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING_RELOC) == 0)
593 syment->n_value = coff_symbol_ptr->symbol.value;
595 else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
597 syment->n_scnum = N_UNDEF;
598 syment->n_value = 0;
600 /* FIXME: Do we need to handle the absolute section here? */
601 else
603 if (coff_symbol_ptr->symbol.section)
605 syment->n_scnum =
606 coff_symbol_ptr->symbol.section->output_section->target_index;
608 syment->n_value = (coff_symbol_ptr->symbol.value
609 + coff_symbol_ptr->symbol.section->output_offset);
610 if (! obj_pe (abfd))
612 syment->n_value += (syment->n_sclass == C_STATLAB)
613 ? coff_symbol_ptr->symbol.section->output_section->lma
614 : coff_symbol_ptr->symbol.section->output_section->vma;
617 else
619 BFD_ASSERT (0);
620 /* This can happen, but I don't know why yet (steve@cygnus.com) */
621 syment->n_scnum = N_ABS;
622 syment->n_value = coff_symbol_ptr->symbol.value;
627 /* Run through all the symbols in the symbol table and work out what
628 their indexes into the symbol table will be when output.
630 Coff requires that each C_FILE symbol points to the next one in the
631 chain, and that the last one points to the first external symbol. We
632 do that here too. */
634 boolean
635 coff_renumber_symbols (bfd_ptr, first_undef)
636 bfd *bfd_ptr;
637 int *first_undef;
639 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
640 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
641 unsigned int native_index = 0;
642 struct internal_syment *last_file = (struct internal_syment *) NULL;
643 unsigned int symbol_index;
645 /* COFF demands that undefined symbols come after all other symbols.
646 Since we don't need to impose this extra knowledge on all our
647 client programs, deal with that here. Sort the symbol table;
648 just move the undefined symbols to the end, leaving the rest
649 alone. The O'Reilly book says that defined global symbols come
650 at the end before the undefined symbols, so we do that here as
651 well. */
652 /* @@ Do we have some condition we could test for, so we don't always
653 have to do this? I don't think relocatability is quite right, but
654 I'm not certain. [raeburn:19920508.1711EST] */
656 asymbol **newsyms;
657 unsigned int i;
659 newsyms = (asymbol **) bfd_alloc (bfd_ptr,
660 sizeof (asymbol *) * (symbol_count + 1));
661 if (!newsyms)
662 return false;
663 bfd_ptr->outsymbols = newsyms;
664 for (i = 0; i < symbol_count; i++)
665 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
666 || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
667 && !bfd_is_com_section (symbol_ptr_ptr[i]->section)
668 && ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) != 0
669 || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
670 == 0))))
671 *newsyms++ = symbol_ptr_ptr[i];
673 for (i = 0; i < symbol_count; i++)
674 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
675 && !bfd_is_und_section (symbol_ptr_ptr[i]->section)
676 && (bfd_is_com_section (symbol_ptr_ptr[i]->section)
677 || ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) == 0
678 && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
679 != 0))))
680 *newsyms++ = symbol_ptr_ptr[i];
682 *first_undef = newsyms - bfd_ptr->outsymbols;
684 for (i = 0; i < symbol_count; i++)
685 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
686 && bfd_is_und_section (symbol_ptr_ptr[i]->section))
687 *newsyms++ = symbol_ptr_ptr[i];
688 *newsyms = (asymbol *) NULL;
689 symbol_ptr_ptr = bfd_ptr->outsymbols;
692 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
694 coff_symbol_type *coff_symbol_ptr = coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
695 symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
696 if (coff_symbol_ptr && coff_symbol_ptr->native)
698 combined_entry_type *s = coff_symbol_ptr->native;
699 int i;
701 if (s->u.syment.n_sclass == C_FILE)
703 if (last_file != (struct internal_syment *) NULL)
704 last_file->n_value = native_index;
705 last_file = &(s->u.syment);
707 else
710 /* Modify the symbol values according to their section and
711 type */
713 fixup_symbol_value (bfd_ptr, coff_symbol_ptr, &(s->u.syment));
715 for (i = 0; i < s->u.syment.n_numaux + 1; i++)
716 s[i].offset = native_index++;
718 else
720 native_index++;
723 obj_conv_table_size (bfd_ptr) = native_index;
725 return true;
728 /* Run thorough the symbol table again, and fix it so that all
729 pointers to entries are changed to the entries' index in the output
730 symbol table. */
732 void
733 coff_mangle_symbols (bfd_ptr)
734 bfd *bfd_ptr;
736 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
737 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
738 unsigned int symbol_index;
740 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
742 coff_symbol_type *coff_symbol_ptr =
743 coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
745 if (coff_symbol_ptr && coff_symbol_ptr->native)
747 int i;
748 combined_entry_type *s = coff_symbol_ptr->native;
750 if (s->fix_value)
752 /* FIXME: We should use a union here. */
753 s->u.syment.n_value =
754 ((combined_entry_type *) s->u.syment.n_value)->offset;
755 s->fix_value = 0;
757 if (s->fix_line)
759 /* The value is the offset into the line number entries
760 for the symbol's section. On output, the symbol's
761 section should be N_DEBUG. */
762 s->u.syment.n_value =
763 (coff_symbol_ptr->symbol.section->output_section->line_filepos
764 + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr));
765 coff_symbol_ptr->symbol.section =
766 coff_section_from_bfd_index (bfd_ptr, N_DEBUG);
767 BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
769 for (i = 0; i < s->u.syment.n_numaux; i++)
771 combined_entry_type *a = s + i + 1;
772 if (a->fix_tag)
774 a->u.auxent.x_sym.x_tagndx.l =
775 a->u.auxent.x_sym.x_tagndx.p->offset;
776 a->fix_tag = 0;
778 if (a->fix_end)
780 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
781 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
782 a->fix_end = 0;
784 if (a->fix_scnlen)
786 a->u.auxent.x_csect.x_scnlen.l =
787 a->u.auxent.x_csect.x_scnlen.p->offset;
788 a->fix_scnlen = 0;
795 static void
796 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
797 debug_string_section_p, debug_string_size_p)
798 bfd *abfd;
799 asymbol *symbol;
800 combined_entry_type *native;
801 bfd_size_type *string_size_p;
802 asection **debug_string_section_p;
803 bfd_size_type *debug_string_size_p;
805 unsigned int name_length;
806 union internal_auxent *auxent;
807 char *name = (char *) (symbol->name);
809 if (name == (char *) NULL)
811 /* coff symbols always have names, so we'll make one up */
812 symbol->name = "strange";
813 name = (char *) symbol->name;
815 name_length = strlen (name);
817 if (native->u.syment.n_sclass == C_FILE
818 && native->u.syment.n_numaux > 0)
820 unsigned int filnmlen;
822 if (bfd_coff_force_symnames_in_strings (abfd))
824 native->u.syment._n._n_n._n_offset =
825 (*string_size_p + STRING_SIZE_SIZE);
826 native->u.syment._n._n_n._n_zeroes = 0;
827 *string_size_p += 6; /* strlen(".file") + 1 */
829 else
830 strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
832 auxent = &(native + 1)->u.auxent;
834 filnmlen = bfd_coff_filnmlen (abfd);
836 if (bfd_coff_long_filenames (abfd))
838 if (name_length <= filnmlen)
840 strncpy (auxent->x_file.x_fname, name, filnmlen);
842 else
844 auxent->x_file.x_n.x_offset = *string_size_p + STRING_SIZE_SIZE;
845 auxent->x_file.x_n.x_zeroes = 0;
846 *string_size_p += name_length + 1;
849 else
851 strncpy (auxent->x_file.x_fname, name, filnmlen);
852 if (name_length > filnmlen)
853 name[filnmlen] = '\0';
856 else
858 if (name_length <= SYMNMLEN && !bfd_coff_force_symnames_in_strings (abfd))
860 /* This name will fit into the symbol neatly */
861 strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
863 else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
865 native->u.syment._n._n_n._n_offset = (*string_size_p
866 + STRING_SIZE_SIZE);
867 native->u.syment._n._n_n._n_zeroes = 0;
868 *string_size_p += name_length + 1;
870 else
872 long filepos;
873 bfd_byte buf[4];
874 int prefix_len = bfd_coff_debug_string_prefix_length (abfd);
876 /* This name should be written into the .debug section. For
877 some reason each name is preceded by a two byte length
878 and also followed by a null byte. FIXME: We assume that
879 the .debug section has already been created, and that it
880 is large enough. */
881 if (*debug_string_section_p == (asection *) NULL)
882 *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
883 filepos = bfd_tell (abfd);
884 if (prefix_len == 4)
885 bfd_put_32 (abfd, name_length + 1, buf);
886 else
887 bfd_put_16 (abfd, name_length + 1, buf);
889 if (!bfd_set_section_contents (abfd,
890 *debug_string_section_p,
891 (PTR) buf,
892 (file_ptr) *debug_string_size_p,
893 (bfd_size_type) prefix_len)
894 || !bfd_set_section_contents (abfd,
895 *debug_string_section_p,
896 (PTR) symbol->name,
897 ((file_ptr) *debug_string_size_p
898 + prefix_len),
899 (bfd_size_type) name_length + 1))
900 abort ();
901 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
902 abort ();
903 native->u.syment._n._n_n._n_offset =
904 *debug_string_size_p + prefix_len;
905 native->u.syment._n._n_n._n_zeroes = 0;
906 *debug_string_size_p += name_length + 1 + prefix_len;
911 /* We need to keep track of the symbol index so that when we write out
912 the relocs we can get the index for a symbol. This method is a
913 hack. FIXME. */
915 #define set_index(symbol, idx) ((symbol)->udata.i = (idx))
917 /* Write a symbol out to a COFF file. */
919 static boolean
920 coff_write_symbol (abfd, symbol, native, written, string_size_p,
921 debug_string_section_p, debug_string_size_p)
922 bfd *abfd;
923 asymbol *symbol;
924 combined_entry_type *native;
925 unsigned int *written;
926 bfd_size_type *string_size_p;
927 asection **debug_string_section_p;
928 bfd_size_type *debug_string_size_p;
930 unsigned int numaux = native->u.syment.n_numaux;
931 int type = native->u.syment.n_type;
932 int class = native->u.syment.n_sclass;
933 PTR buf;
934 bfd_size_type symesz;
936 if (native->u.syment.n_sclass == C_FILE)
937 symbol->flags |= BSF_DEBUGGING;
939 if (symbol->flags & BSF_DEBUGGING
940 && bfd_is_abs_section (symbol->section))
942 native->u.syment.n_scnum = N_DEBUG;
944 else if (bfd_is_abs_section (symbol->section))
946 native->u.syment.n_scnum = N_ABS;
948 else if (bfd_is_und_section (symbol->section))
950 native->u.syment.n_scnum = N_UNDEF;
952 else
954 native->u.syment.n_scnum =
955 symbol->section->output_section->target_index;
958 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
959 debug_string_section_p, debug_string_size_p);
961 symesz = bfd_coff_symesz (abfd);
962 buf = bfd_alloc (abfd, symesz);
963 if (!buf)
964 return false;
965 bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
966 if (bfd_write (buf, 1, symesz, abfd) != symesz)
967 return false;
968 bfd_release (abfd, buf);
970 if (native->u.syment.n_numaux > 0)
972 bfd_size_type auxesz;
973 unsigned int j;
975 auxesz = bfd_coff_auxesz (abfd);
976 buf = bfd_alloc (abfd, auxesz);
977 if (!buf)
978 return false;
979 for (j = 0; j < native->u.syment.n_numaux; j++)
981 bfd_coff_swap_aux_out (abfd,
982 &((native + j + 1)->u.auxent),
983 type,
984 class,
986 native->u.syment.n_numaux,
987 buf);
988 if (bfd_write (buf, 1, auxesz, abfd) != auxesz)
989 return false;
991 bfd_release (abfd, buf);
994 /* Store the index for use when we write out the relocs. */
995 set_index (symbol, *written);
997 *written += numaux + 1;
998 return true;
1001 /* Write out a symbol to a COFF file that does not come from a COFF
1002 file originally. This symbol may have been created by the linker,
1003 or we may be linking a non COFF file to a COFF file. */
1005 static boolean
1006 coff_write_alien_symbol (abfd, symbol, written, string_size_p,
1007 debug_string_section_p, debug_string_size_p)
1008 bfd *abfd;
1009 asymbol *symbol;
1010 unsigned int *written;
1011 bfd_size_type *string_size_p;
1012 asection **debug_string_section_p;
1013 bfd_size_type *debug_string_size_p;
1015 combined_entry_type *native;
1016 combined_entry_type dummy;
1018 native = &dummy;
1019 native->u.syment.n_type = T_NULL;
1020 native->u.syment.n_flags = 0;
1021 if (bfd_is_und_section (symbol->section))
1023 native->u.syment.n_scnum = N_UNDEF;
1024 native->u.syment.n_value = symbol->value;
1026 else if (bfd_is_com_section (symbol->section))
1028 native->u.syment.n_scnum = N_UNDEF;
1029 native->u.syment.n_value = symbol->value;
1031 else if (symbol->flags & BSF_DEBUGGING)
1033 /* There isn't much point to writing out a debugging symbol
1034 unless we are prepared to convert it into COFF debugging
1035 format. So, we just ignore them. We must clobber the symbol
1036 name to keep it from being put in the string table. */
1037 symbol->name = "";
1038 return true;
1040 else
1042 native->u.syment.n_scnum =
1043 symbol->section->output_section->target_index;
1044 native->u.syment.n_value = (symbol->value
1045 + symbol->section->output_offset);
1046 if (! obj_pe (abfd))
1047 native->u.syment.n_value += symbol->section->output_section->vma;
1049 /* Copy the any flags from the the file header into the symbol.
1050 FIXME: Why? */
1052 coff_symbol_type *c = coff_symbol_from (abfd, symbol);
1053 if (c != (coff_symbol_type *) NULL)
1054 native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
1058 native->u.syment.n_type = 0;
1059 if (symbol->flags & BSF_LOCAL)
1060 native->u.syment.n_sclass = C_STAT;
1061 else if (symbol->flags & BSF_WEAK)
1062 native->u.syment.n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1063 else
1064 native->u.syment.n_sclass = C_EXT;
1065 native->u.syment.n_numaux = 0;
1067 return coff_write_symbol (abfd, symbol, native, written, string_size_p,
1068 debug_string_section_p, debug_string_size_p);
1071 /* Write a native symbol to a COFF file. */
1073 static boolean
1074 coff_write_native_symbol (abfd, symbol, written, string_size_p,
1075 debug_string_section_p, debug_string_size_p)
1076 bfd *abfd;
1077 coff_symbol_type *symbol;
1078 unsigned int *written;
1079 bfd_size_type *string_size_p;
1080 asection **debug_string_section_p;
1081 bfd_size_type *debug_string_size_p;
1083 combined_entry_type *native = symbol->native;
1084 alent *lineno = symbol->lineno;
1086 /* If this symbol has an associated line number, we must store the
1087 symbol index in the line number field. We also tag the auxent to
1088 point to the right place in the lineno table. */
1089 if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
1091 unsigned int count = 0;
1092 lineno[count].u.offset = *written;
1093 if (native->u.syment.n_numaux)
1095 union internal_auxent *a = &((native + 1)->u.auxent);
1097 a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
1098 symbol->symbol.section->output_section->moving_line_filepos;
1101 /* Count and relocate all other linenumbers. */
1102 count++;
1103 while (lineno[count].line_number != 0)
1105 #if 0
1106 /* 13 april 92. sac
1107 I've been told this, but still need proof:
1108 > The second bug is also in `bfd/coffcode.h'. This bug
1109 > causes the linker to screw up the pc-relocations for
1110 > all the line numbers in COFF code. This bug isn't only
1111 > specific to A29K implementations, but affects all
1112 > systems using COFF format binaries. Note that in COFF
1113 > object files, the line number core offsets output by
1114 > the assembler are relative to the start of each
1115 > procedure, not to the start of the .text section. This
1116 > patch relocates the line numbers relative to the
1117 > `native->u.syment.n_value' instead of the section
1118 > virtual address.
1119 > modular!olson@cs.arizona.edu (Jon Olson)
1121 lineno[count].u.offset += native->u.syment.n_value;
1122 #else
1123 lineno[count].u.offset +=
1124 (symbol->symbol.section->output_section->vma
1125 + symbol->symbol.section->output_offset);
1126 #endif
1127 count++;
1129 symbol->done_lineno = true;
1131 symbol->symbol.section->output_section->moving_line_filepos +=
1132 count * bfd_coff_linesz (abfd);
1135 return coff_write_symbol (abfd, &(symbol->symbol), native, written,
1136 string_size_p, debug_string_section_p,
1137 debug_string_size_p);
1140 /* Write out the COFF symbols. */
1142 boolean
1143 coff_write_symbols (abfd)
1144 bfd *abfd;
1146 bfd_size_type string_size;
1147 asection *debug_string_section;
1148 bfd_size_type debug_string_size;
1149 unsigned int i;
1150 unsigned int limit = bfd_get_symcount (abfd);
1151 unsigned int written = 0;
1152 asymbol **p;
1154 string_size = 0;
1155 debug_string_section = NULL;
1156 debug_string_size = 0;
1158 /* If this target supports long section names, they must be put into
1159 the string table. This is supported by PE. This code must
1160 handle section names just as they are handled in
1161 coff_write_object_contents. */
1162 if (bfd_coff_long_section_names (abfd))
1164 asection *o;
1166 for (o = abfd->sections; o != NULL; o = o->next)
1168 size_t len;
1170 len = strlen (o->name);
1171 if (len > SCNNMLEN)
1172 string_size += len + 1;
1176 /* Seek to the right place */
1177 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1178 return false;
1180 /* Output all the symbols we have */
1182 written = 0;
1183 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
1185 asymbol *symbol = *p;
1186 coff_symbol_type *c_symbol = coff_symbol_from (abfd, symbol);
1188 if (c_symbol == (coff_symbol_type *) NULL
1189 || c_symbol->native == (combined_entry_type *) NULL)
1191 if (!coff_write_alien_symbol (abfd, symbol, &written, &string_size,
1192 &debug_string_section,
1193 &debug_string_size))
1194 return false;
1196 else
1198 if (!coff_write_native_symbol (abfd, c_symbol, &written,
1199 &string_size, &debug_string_section,
1200 &debug_string_size))
1201 return false;
1205 obj_raw_syment_count (abfd) = written;
1207 /* Now write out strings */
1209 if (string_size != 0)
1211 unsigned int size = string_size + STRING_SIZE_SIZE;
1212 bfd_byte buffer[STRING_SIZE_SIZE];
1214 #if STRING_SIZE_SIZE == 4
1215 bfd_h_put_32 (abfd, size, buffer);
1216 #else
1217 #error Change bfd_h_put_32
1218 #endif
1219 if (bfd_write ((PTR) buffer, 1, sizeof (buffer), abfd) != sizeof (buffer))
1220 return false;
1222 /* Handle long section names. This code must handle section
1223 names just as they are handled in coff_write_object_contents. */
1224 if (bfd_coff_long_section_names (abfd))
1226 asection *o;
1228 for (o = abfd->sections; o != NULL; o = o->next)
1230 size_t len;
1232 len = strlen (o->name);
1233 if (len > SCNNMLEN)
1235 if (bfd_write (o->name, 1, len + 1, abfd) != len + 1)
1236 return false;
1241 for (p = abfd->outsymbols, i = 0;
1242 i < limit;
1243 i++, p++)
1245 asymbol *q = *p;
1246 size_t name_length = strlen (q->name);
1247 coff_symbol_type *c_symbol = coff_symbol_from (abfd, q);
1248 size_t maxlen;
1250 /* Figure out whether the symbol name should go in the string
1251 table. Symbol names that are short enough are stored
1252 directly in the syment structure. File names permit a
1253 different, longer, length in the syment structure. On
1254 XCOFF, some symbol names are stored in the .debug section
1255 rather than in the string table. */
1257 if (c_symbol == NULL
1258 || c_symbol->native == NULL)
1260 /* This is not a COFF symbol, so it certainly is not a
1261 file name, nor does it go in the .debug section. */
1262 maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
1264 else if (bfd_coff_symname_in_debug (abfd,
1265 &c_symbol->native->u.syment))
1267 /* This symbol name is in the XCOFF .debug section.
1268 Don't write it into the string table. */
1269 maxlen = name_length;
1271 else if (c_symbol->native->u.syment.n_sclass == C_FILE
1272 && c_symbol->native->u.syment.n_numaux > 0)
1274 if (bfd_coff_force_symnames_in_strings (abfd))
1275 bfd_write (".file", 1, 6, abfd);
1276 maxlen = bfd_coff_filnmlen (abfd);
1278 else
1279 maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
1281 if (name_length > maxlen)
1283 if (bfd_write ((PTR) (q->name), 1, name_length + 1, abfd)
1284 != name_length + 1)
1285 return false;
1289 else
1291 /* We would normally not write anything here, but we'll write
1292 out 4 so that any stupid coff reader which tries to read the
1293 string table even when there isn't one won't croak. */
1294 unsigned int size = STRING_SIZE_SIZE;
1295 bfd_byte buffer[STRING_SIZE_SIZE];
1297 #if STRING_SIZE_SIZE == 4
1298 bfd_h_put_32 (abfd, size, buffer);
1299 #else
1300 #error Change bfd_h_put_32
1301 #endif
1302 if (bfd_write ((PTR) buffer, 1, STRING_SIZE_SIZE, abfd)
1303 != STRING_SIZE_SIZE)
1304 return false;
1307 /* Make sure the .debug section was created to be the correct size.
1308 We should create it ourselves on the fly, but we don't because
1309 BFD won't let us write to any section until we know how large all
1310 the sections are. We could still do it by making another pass
1311 over the symbols. FIXME. */
1312 BFD_ASSERT (debug_string_size == 0
1313 || (debug_string_section != (asection *) NULL
1314 && (BFD_ALIGN (debug_string_size,
1315 1 << debug_string_section->alignment_power)
1316 == bfd_section_size (abfd, debug_string_section))));
1318 return true;
1321 boolean
1322 coff_write_linenumbers (abfd)
1323 bfd *abfd;
1325 asection *s;
1326 bfd_size_type linesz;
1327 PTR buff;
1329 linesz = bfd_coff_linesz (abfd);
1330 buff = bfd_alloc (abfd, linesz);
1331 if (!buff)
1332 return false;
1333 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1335 if (s->lineno_count)
1337 asymbol **q = abfd->outsymbols;
1338 if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
1339 return false;
1340 /* Find all the linenumbers in this section */
1341 while (*q)
1343 asymbol *p = *q;
1344 if (p->section->output_section == s)
1346 alent *l =
1347 BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
1348 (bfd_asymbol_bfd (p), p));
1349 if (l)
1351 /* Found a linenumber entry, output */
1352 struct internal_lineno out;
1353 memset ((PTR) & out, 0, sizeof (out));
1354 out.l_lnno = 0;
1355 out.l_addr.l_symndx = l->u.offset;
1356 bfd_coff_swap_lineno_out (abfd, &out, buff);
1357 if (bfd_write (buff, 1, linesz, abfd) != linesz)
1358 return false;
1359 l++;
1360 while (l->line_number)
1362 out.l_lnno = l->line_number;
1363 out.l_addr.l_symndx = l->u.offset;
1364 bfd_coff_swap_lineno_out (abfd, &out, buff);
1365 if (bfd_write (buff, 1, linesz, abfd) != linesz)
1366 return false;
1367 l++;
1371 q++;
1375 bfd_release (abfd, buff);
1376 return true;
1379 /*ARGSUSED */
1380 alent *
1381 coff_get_lineno (ignore_abfd, symbol)
1382 bfd *ignore_abfd ATTRIBUTE_UNUSED;
1383 asymbol *symbol;
1385 return coffsymbol (symbol)->lineno;
1388 #if 0
1390 /* This is only called from coff_add_missing_symbols, which has been
1391 disabled. */
1393 asymbol *
1394 coff_section_symbol (abfd, name)
1395 bfd *abfd;
1396 char *name;
1398 asection *sec = bfd_make_section_old_way (abfd, name);
1399 asymbol *sym;
1400 combined_entry_type *csym;
1402 sym = sec->symbol;
1403 csym = coff_symbol_from (abfd, sym)->native;
1404 /* Make sure back-end COFF stuff is there. */
1405 if (csym == 0)
1407 struct foo
1409 coff_symbol_type sym;
1410 /* @@FIXME This shouldn't use a fixed size!! */
1411 combined_entry_type e[10];
1413 struct foo *f;
1414 f = (struct foo *) bfd_alloc (abfd, sizeof (*f));
1415 if (!f)
1417 bfd_set_error (bfd_error_no_error);
1418 return NULL;
1420 memset ((char *) f, 0, sizeof (*f));
1421 coff_symbol_from (abfd, sym)->native = csym = f->e;
1423 csym[0].u.syment.n_sclass = C_STAT;
1424 csym[0].u.syment.n_numaux = 1;
1425 /* SF_SET_STATICS (sym); @@ ??? */
1426 csym[1].u.auxent.x_scn.x_scnlen = sec->_raw_size;
1427 csym[1].u.auxent.x_scn.x_nreloc = sec->reloc_count;
1428 csym[1].u.auxent.x_scn.x_nlinno = sec->lineno_count;
1430 if (sec->output_section == NULL)
1432 sec->output_section = sec;
1433 sec->output_offset = 0;
1436 return sym;
1439 #endif /* 0 */
1441 /* This function transforms the offsets into the symbol table into
1442 pointers to syments. */
1444 static void
1445 coff_pointerize_aux (abfd, table_base, symbol, indaux, auxent)
1446 bfd *abfd;
1447 combined_entry_type *table_base;
1448 combined_entry_type *symbol;
1449 unsigned int indaux;
1450 combined_entry_type *auxent;
1452 unsigned int type = symbol->u.syment.n_type;
1453 unsigned int class = symbol->u.syment.n_sclass;
1455 if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1457 if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1458 (abfd, table_base, symbol, indaux, auxent))
1459 return;
1462 /* Don't bother if this is a file or a section */
1463 if (class == C_STAT && type == T_NULL)
1464 return;
1465 if (class == C_FILE)
1466 return;
1468 /* Otherwise patch up */
1469 #define N_TMASK coff_data (abfd)->local_n_tmask
1470 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1471 if ((ISFCN (type) || ISTAG (class) || class == C_BLOCK || class == C_FCN)
1472 && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l > 0)
1474 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
1475 table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
1476 auxent->fix_end = 1;
1478 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1479 generate one, so we must be careful to ignore it. */
1480 if (auxent->u.auxent.x_sym.x_tagndx.l > 0)
1482 auxent->u.auxent.x_sym.x_tagndx.p =
1483 table_base + auxent->u.auxent.x_sym.x_tagndx.l;
1484 auxent->fix_tag = 1;
1488 /* Allocate space for the ".debug" section, and read it.
1489 We did not read the debug section until now, because
1490 we didn't want to go to the trouble until someone needed it. */
1492 static char *
1493 build_debug_section (abfd)
1494 bfd *abfd;
1496 char *debug_section;
1497 long position;
1499 asection *sect = bfd_get_section_by_name (abfd, ".debug");
1501 if (!sect)
1503 bfd_set_error (bfd_error_no_debug_section);
1504 return NULL;
1507 debug_section = (PTR) bfd_alloc (abfd,
1508 bfd_get_section_size_before_reloc (sect));
1509 if (debug_section == NULL)
1510 return NULL;
1512 /* Seek to the beginning of the `.debug' section and read it.
1513 Save the current position first; it is needed by our caller.
1514 Then read debug section and reset the file pointer. */
1516 position = bfd_tell (abfd);
1517 if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0
1518 || (bfd_read (debug_section,
1519 bfd_get_section_size_before_reloc (sect), 1, abfd)
1520 != bfd_get_section_size_before_reloc (sect))
1521 || bfd_seek (abfd, position, SEEK_SET) != 0)
1522 return NULL;
1523 return debug_section;
1527 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1528 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1529 be \0-terminated. */
1530 static char *
1531 copy_name (abfd, name, maxlen)
1532 bfd *abfd;
1533 char *name;
1534 int maxlen;
1536 int len;
1537 char *newname;
1539 for (len = 0; len < maxlen; ++len)
1541 if (name[len] == '\0')
1543 break;
1547 if ((newname = (PTR) bfd_alloc (abfd, len + 1)) == NULL)
1548 return (NULL);
1549 strncpy (newname, name, len);
1550 newname[len] = '\0';
1551 return newname;
1554 /* Read in the external symbols. */
1556 boolean
1557 _bfd_coff_get_external_symbols (abfd)
1558 bfd *abfd;
1560 bfd_size_type symesz;
1561 size_t size;
1562 PTR syms;
1564 if (obj_coff_external_syms (abfd) != NULL)
1565 return true;
1567 symesz = bfd_coff_symesz (abfd);
1569 size = obj_raw_syment_count (abfd) * symesz;
1571 syms = (PTR) bfd_malloc (size);
1572 if (syms == NULL && size != 0)
1573 return false;
1575 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
1576 || bfd_read (syms, size, 1, abfd) != size)
1578 if (syms != NULL)
1579 free (syms);
1580 return false;
1583 obj_coff_external_syms (abfd) = syms;
1585 return true;
1588 /* Read in the external strings. The strings are not loaded until
1589 they are needed. This is because we have no simple way of
1590 detecting a missing string table in an archive. */
1592 const char *
1593 _bfd_coff_read_string_table (abfd)
1594 bfd *abfd;
1596 char extstrsize[STRING_SIZE_SIZE];
1597 size_t strsize;
1598 char *strings;
1600 if (obj_coff_strings (abfd) != NULL)
1601 return obj_coff_strings (abfd);
1603 if (obj_sym_filepos (abfd) == 0)
1605 bfd_set_error (bfd_error_no_symbols);
1606 return NULL;
1609 if (bfd_seek (abfd,
1610 (obj_sym_filepos (abfd)
1611 + obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd)),
1612 SEEK_SET) != 0)
1613 return NULL;
1615 if (bfd_read (extstrsize, sizeof extstrsize, 1, abfd) != sizeof extstrsize)
1617 if (bfd_get_error () != bfd_error_file_truncated)
1618 return NULL;
1620 /* There is no string table. */
1621 strsize = STRING_SIZE_SIZE;
1623 else
1625 #if STRING_SIZE_SIZE == 4
1626 strsize = bfd_h_get_32 (abfd, (bfd_byte *) extstrsize);
1627 #else
1628 #error Change bfd_h_get_32
1629 #endif
1632 if (strsize < STRING_SIZE_SIZE)
1634 (*_bfd_error_handler)
1635 (_("%s: bad string table size %lu"), bfd_get_filename (abfd),
1636 (unsigned long) strsize);
1637 bfd_set_error (bfd_error_bad_value);
1638 return NULL;
1641 strings = (char *) bfd_malloc (strsize);
1642 if (strings == NULL)
1643 return NULL;
1645 if (bfd_read (strings + STRING_SIZE_SIZE,
1646 strsize - STRING_SIZE_SIZE, 1, abfd)
1647 != strsize - STRING_SIZE_SIZE)
1649 free (strings);
1650 return NULL;
1653 obj_coff_strings (abfd) = strings;
1655 return strings;
1658 /* Free up the external symbols and strings read from a COFF file. */
1660 boolean
1661 _bfd_coff_free_symbols (abfd)
1662 bfd *abfd;
1664 if (obj_coff_external_syms (abfd) != NULL
1665 && ! obj_coff_keep_syms (abfd))
1667 free (obj_coff_external_syms (abfd));
1668 obj_coff_external_syms (abfd) = NULL;
1670 if (obj_coff_strings (abfd) != NULL
1671 && ! obj_coff_keep_strings (abfd))
1673 free (obj_coff_strings (abfd));
1674 obj_coff_strings (abfd) = NULL;
1676 return true;
1679 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1680 knit the symbol names into a normalized form. By normalized here I
1681 mean that all symbols have an n_offset pointer that points to a null-
1682 terminated string. */
1684 combined_entry_type *
1685 coff_get_normalized_symtab (abfd)
1686 bfd *abfd;
1688 combined_entry_type *internal;
1689 combined_entry_type *internal_ptr;
1690 combined_entry_type *symbol_ptr;
1691 combined_entry_type *internal_end;
1692 bfd_size_type symesz;
1693 char *raw_src;
1694 char *raw_end;
1695 const char *string_table = NULL;
1696 char *debug_section = NULL;
1697 unsigned long size;
1699 if (obj_raw_syments (abfd) != NULL)
1700 return obj_raw_syments (abfd);
1702 size = obj_raw_syment_count (abfd) * sizeof (combined_entry_type);
1703 internal = (combined_entry_type *) bfd_zalloc (abfd, size);
1704 if (internal == NULL && size != 0)
1705 return NULL;
1706 internal_end = internal + obj_raw_syment_count (abfd);
1708 if (! _bfd_coff_get_external_symbols (abfd))
1709 return NULL;
1711 raw_src = (char *) obj_coff_external_syms (abfd);
1713 /* mark the end of the symbols */
1714 symesz = bfd_coff_symesz (abfd);
1715 raw_end = (char *) raw_src + obj_raw_syment_count (abfd) * symesz;
1717 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1718 probably possible. If one shows up, it will probably kill us. */
1720 /* Swap all the raw entries */
1721 for (internal_ptr = internal;
1722 raw_src < raw_end;
1723 raw_src += symesz, internal_ptr++)
1726 unsigned int i;
1727 bfd_coff_swap_sym_in (abfd, (PTR) raw_src,
1728 (PTR) & internal_ptr->u.syment);
1729 symbol_ptr = internal_ptr;
1731 for (i = 0;
1732 i < symbol_ptr->u.syment.n_numaux;
1733 i++)
1735 internal_ptr++;
1736 raw_src += symesz;
1737 bfd_coff_swap_aux_in (abfd, (PTR) raw_src,
1738 symbol_ptr->u.syment.n_type,
1739 symbol_ptr->u.syment.n_sclass,
1740 i, symbol_ptr->u.syment.n_numaux,
1741 &(internal_ptr->u.auxent));
1742 coff_pointerize_aux (abfd, internal, symbol_ptr, i,
1743 internal_ptr);
1747 /* Free the raw symbols, but not the strings (if we have them). */
1748 obj_coff_keep_strings (abfd) = true;
1749 if (! _bfd_coff_free_symbols (abfd))
1750 return NULL;
1752 for (internal_ptr = internal; internal_ptr < internal_end;
1753 internal_ptr++)
1755 if (internal_ptr->u.syment.n_sclass == C_FILE
1756 && internal_ptr->u.syment.n_numaux > 0)
1758 /* make a file symbol point to the name in the auxent, since
1759 the text ".file" is redundant */
1760 if ((internal_ptr + 1)->u.auxent.x_file.x_n.x_zeroes == 0)
1762 /* the filename is a long one, point into the string table */
1763 if (string_table == NULL)
1765 string_table = _bfd_coff_read_string_table (abfd);
1766 if (string_table == NULL)
1767 return NULL;
1770 internal_ptr->u.syment._n._n_n._n_offset =
1771 ((long)
1772 (string_table
1773 + (internal_ptr + 1)->u.auxent.x_file.x_n.x_offset));
1775 else
1777 /* Ordinary short filename, put into memory anyway. The
1778 Microsoft PE tools sometimes store a filename in
1779 multiple AUX entries. */
1780 if (internal_ptr->u.syment.n_numaux > 1
1781 && coff_data (abfd)->pe)
1783 internal_ptr->u.syment._n._n_n._n_offset =
1784 ((long)
1785 copy_name (abfd,
1786 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1787 internal_ptr->u.syment.n_numaux * symesz));
1789 else
1791 internal_ptr->u.syment._n._n_n._n_offset =
1792 ((long)
1793 copy_name (abfd,
1794 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1795 bfd_coff_filnmlen (abfd)));
1799 else
1801 if (internal_ptr->u.syment._n._n_n._n_zeroes != 0)
1803 /* This is a "short" name. Make it long. */
1804 unsigned long i = 0;
1805 char *newstring = NULL;
1807 /* find the length of this string without walking into memory
1808 that isn't ours. */
1809 for (i = 0; i < 8; ++i)
1811 if (internal_ptr->u.syment._n._n_name[i] == '\0')
1813 break;
1814 } /* if end of string */
1815 } /* possible lengths of this string. */
1817 if ((newstring = (PTR) bfd_alloc (abfd, ++i)) == NULL)
1818 return (NULL);
1819 memset (newstring, 0, i);
1820 strncpy (newstring, internal_ptr->u.syment._n._n_name, i - 1);
1821 internal_ptr->u.syment._n._n_n._n_offset = (long int) newstring;
1822 internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1824 else if (internal_ptr->u.syment._n._n_n._n_offset == 0)
1825 internal_ptr->u.syment._n._n_n._n_offset = (long int) "";
1826 else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment))
1828 /* Long name already. Point symbol at the string in the
1829 table. */
1830 if (string_table == NULL)
1832 string_table = _bfd_coff_read_string_table (abfd);
1833 if (string_table == NULL)
1834 return NULL;
1836 internal_ptr->u.syment._n._n_n._n_offset =
1837 ((long int)
1838 (string_table
1839 + internal_ptr->u.syment._n._n_n._n_offset));
1841 else
1843 /* Long name in debug section. Very similar. */
1844 if (debug_section == NULL)
1845 debug_section = build_debug_section (abfd);
1846 internal_ptr->u.syment._n._n_n._n_offset = (long int)
1847 (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
1850 internal_ptr += internal_ptr->u.syment.n_numaux;
1853 obj_raw_syments (abfd) = internal;
1854 BFD_ASSERT (obj_raw_syment_count (abfd)
1855 == (unsigned int) (internal_ptr - internal));
1857 return (internal);
1858 } /* coff_get_normalized_symtab() */
1860 long
1861 coff_get_reloc_upper_bound (abfd, asect)
1862 bfd *abfd;
1863 sec_ptr asect;
1865 if (bfd_get_format (abfd) != bfd_object)
1867 bfd_set_error (bfd_error_invalid_operation);
1868 return -1;
1870 return (asect->reloc_count + 1) * sizeof (arelent *);
1873 asymbol *
1874 coff_make_empty_symbol (abfd)
1875 bfd *abfd;
1877 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, sizeof (coff_symbol_type));
1878 if (new == NULL)
1879 return (NULL);
1880 memset (new, 0, sizeof *new);
1881 new->symbol.section = 0;
1882 new->native = 0;
1883 new->lineno = (alent *) NULL;
1884 new->done_lineno = false;
1885 new->symbol.the_bfd = abfd;
1886 return &new->symbol;
1889 /* Make a debugging symbol. */
1891 asymbol *
1892 coff_bfd_make_debug_symbol (abfd, ptr, sz)
1893 bfd *abfd;
1894 PTR ptr ATTRIBUTE_UNUSED;
1895 unsigned long sz ATTRIBUTE_UNUSED;
1897 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, sizeof (coff_symbol_type));
1898 if (new == NULL)
1899 return (NULL);
1900 /* @@ The 10 is a guess at a plausible maximum number of aux entries
1901 (but shouldn't be a constant). */
1902 new->native = (combined_entry_type *) bfd_zalloc (abfd, sizeof (combined_entry_type) * 10);
1903 if (!new->native)
1904 return (NULL);
1905 new->symbol.section = bfd_abs_section_ptr;
1906 new->symbol.flags = BSF_DEBUGGING;
1907 new->lineno = (alent *) NULL;
1908 new->done_lineno = false;
1909 new->symbol.the_bfd = abfd;
1910 return &new->symbol;
1913 /*ARGSUSED */
1914 void
1915 coff_get_symbol_info (abfd, symbol, ret)
1916 bfd *abfd;
1917 asymbol *symbol;
1918 symbol_info *ret;
1920 bfd_symbol_info (symbol, ret);
1921 if (coffsymbol (symbol)->native != NULL
1922 && coffsymbol (symbol)->native->fix_value)
1924 combined_entry_type *psym;
1926 psym = ((combined_entry_type *)
1927 coffsymbol (symbol)->native->u.syment.n_value);
1928 ret->value = (bfd_vma) (psym - obj_raw_syments (abfd));
1932 /* Return the COFF syment for a symbol. */
1934 boolean
1935 bfd_coff_get_syment (abfd, symbol, psyment)
1936 bfd *abfd;
1937 asymbol *symbol;
1938 struct internal_syment *psyment;
1940 coff_symbol_type *csym;
1942 csym = coff_symbol_from (abfd, symbol);
1943 if (csym == NULL || csym->native == NULL)
1945 bfd_set_error (bfd_error_invalid_operation);
1946 return false;
1949 *psyment = csym->native->u.syment;
1951 if (csym->native->fix_value)
1952 psyment->n_value = ((combined_entry_type *) psyment->n_value
1953 - obj_raw_syments (abfd));
1955 /* FIXME: We should handle fix_line here. */
1957 return true;
1960 /* Return the COFF auxent for a symbol. */
1962 boolean
1963 bfd_coff_get_auxent (abfd, symbol, indx, pauxent)
1964 bfd *abfd;
1965 asymbol *symbol;
1966 int indx;
1967 union internal_auxent *pauxent;
1969 coff_symbol_type *csym;
1970 combined_entry_type *ent;
1972 csym = coff_symbol_from (abfd, symbol);
1974 if (csym == NULL
1975 || csym->native == NULL
1976 || indx >= csym->native->u.syment.n_numaux)
1978 bfd_set_error (bfd_error_invalid_operation);
1979 return false;
1982 ent = csym->native + indx + 1;
1984 *pauxent = ent->u.auxent;
1986 if (ent->fix_tag)
1987 pauxent->x_sym.x_tagndx.l =
1988 ((combined_entry_type *) pauxent->x_sym.x_tagndx.p
1989 - obj_raw_syments (abfd));
1991 if (ent->fix_end)
1992 pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l =
1993 ((combined_entry_type *) pauxent->x_sym.x_fcnary.x_fcn.x_endndx.p
1994 - obj_raw_syments (abfd));
1996 if (ent->fix_scnlen)
1997 pauxent->x_csect.x_scnlen.l =
1998 ((combined_entry_type *) pauxent->x_csect.x_scnlen.p
1999 - obj_raw_syments (abfd));
2001 return true;
2004 /* Print out information about COFF symbol. */
2006 void
2007 coff_print_symbol (abfd, filep, symbol, how)
2008 bfd *abfd;
2009 PTR filep;
2010 asymbol *symbol;
2011 bfd_print_symbol_type how;
2013 FILE *file = (FILE *) filep;
2015 switch (how)
2017 case bfd_print_symbol_name:
2018 fprintf (file, "%s", symbol->name);
2019 break;
2021 case bfd_print_symbol_more:
2022 fprintf (file, "coff %s %s",
2023 coffsymbol (symbol)->native ? "n" : "g",
2024 coffsymbol (symbol)->lineno ? "l" : " ");
2025 break;
2027 case bfd_print_symbol_all:
2028 if (coffsymbol (symbol)->native)
2030 unsigned long val;
2031 unsigned int aux;
2032 combined_entry_type *combined = coffsymbol (symbol)->native;
2033 combined_entry_type *root = obj_raw_syments (abfd);
2034 struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
2036 fprintf (file, "[%3ld]", (long) (combined - root));
2038 if (! combined->fix_value)
2039 val = (unsigned long) combined->u.syment.n_value;
2040 else
2041 val = ((unsigned long)
2042 ((combined_entry_type *) combined->u.syment.n_value
2043 - root));
2045 fprintf (file,
2046 "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x%08lx %s",
2047 combined->u.syment.n_scnum,
2048 combined->u.syment.n_flags,
2049 combined->u.syment.n_type,
2050 combined->u.syment.n_sclass,
2051 combined->u.syment.n_numaux,
2052 val,
2053 symbol->name);
2055 for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
2057 combined_entry_type *auxp = combined + aux + 1;
2058 long tagndx;
2060 if (auxp->fix_tag)
2061 tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
2062 else
2063 tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
2065 fprintf (file, "\n");
2067 if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux))
2068 continue;
2070 switch (combined->u.syment.n_sclass)
2072 case C_FILE:
2073 fprintf (file, "File ");
2074 break;
2076 case C_STAT:
2077 if (combined->u.syment.n_type == T_NULL)
2078 /* probably a section symbol? */
2080 fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
2081 (long) auxp->u.auxent.x_scn.x_scnlen,
2082 auxp->u.auxent.x_scn.x_nreloc,
2083 auxp->u.auxent.x_scn.x_nlinno);
2084 if (auxp->u.auxent.x_scn.x_checksum != 0
2085 || auxp->u.auxent.x_scn.x_associated != 0
2086 || auxp->u.auxent.x_scn.x_comdat != 0)
2087 fprintf (file, " checksum 0x%lx assoc %d comdat %d",
2088 auxp->u.auxent.x_scn.x_checksum,
2089 auxp->u.auxent.x_scn.x_associated,
2090 auxp->u.auxent.x_scn.x_comdat);
2091 break;
2093 /* else fall through */
2094 case C_EXT:
2095 if (ISFCN (combined->u.syment.n_type))
2097 fprintf (file,
2098 _("AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld"),
2099 tagndx,
2100 auxp->u.auxent.x_sym.x_misc.x_fsize,
2101 auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_lnnoptr,
2102 (auxp->fix_end
2103 ? ((long)
2104 (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2105 - root))
2106 : auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l));
2107 break;
2109 /* else fall through */
2110 default:
2111 fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
2112 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
2113 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
2114 tagndx);
2115 if (auxp->fix_end)
2116 fprintf (file, " endndx %ld",
2117 ((long)
2118 (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2119 - root)));
2120 break;
2124 if (l)
2126 fprintf (file, "\n%s :", l->u.sym->name);
2127 l++;
2128 while (l->line_number)
2130 fprintf (file, "\n%4d : 0x%lx",
2131 l->line_number,
2132 ((unsigned long)
2133 (l->u.offset + symbol->section->vma)));
2134 l++;
2138 else
2140 bfd_print_symbol_vandf ((PTR) file, symbol);
2141 fprintf (file, " %-5s %s %s %s",
2142 symbol->section->name,
2143 coffsymbol (symbol)->native ? "n" : "g",
2144 coffsymbol (symbol)->lineno ? "l" : " ",
2145 symbol->name);
2150 /* Return whether a symbol name implies a local symbol. In COFF,
2151 local symbols generally start with ``.L''. Most targets use this
2152 function for the is_local_label_name entry point, but some may
2153 override it. */
2155 boolean
2156 _bfd_coff_is_local_label_name (abfd, name)
2157 bfd *abfd ATTRIBUTE_UNUSED;
2158 const char *name;
2160 return name[0] == '.' && name[1] == 'L';
2163 /* Provided a BFD, a section and an offset (in bytes, not octets) into the
2164 section, calculate and return the name of the source file and the line
2165 nearest to the wanted location. */
2166 /*ARGSUSED*/
2167 boolean
2168 coff_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
2169 functionname_ptr, line_ptr)
2170 bfd *abfd;
2171 asection *section;
2172 asymbol **symbols;
2173 bfd_vma offset;
2174 CONST char **filename_ptr;
2175 CONST char **functionname_ptr;
2176 unsigned int *line_ptr;
2178 boolean found;
2179 unsigned int i;
2180 unsigned int line_base;
2181 coff_data_type *cof = coff_data (abfd);
2182 /* Run through the raw syments if available */
2183 combined_entry_type *p;
2184 combined_entry_type *pend;
2185 alent *l;
2186 struct coff_section_tdata *sec_data;
2188 /* Before looking through the symbol table, try to use a .stab
2189 section to find the information. */
2190 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
2191 &found, filename_ptr,
2192 functionname_ptr, line_ptr,
2193 &coff_data (abfd)->line_info))
2194 return false;
2195 if (found)
2196 return true;
2198 *filename_ptr = 0;
2199 *functionname_ptr = 0;
2200 *line_ptr = 0;
2202 /* Don't try and find line numbers in a non coff file */
2203 if (abfd->xvec->flavour != bfd_target_coff_flavour)
2204 return false;
2206 if (cof == NULL)
2207 return false;
2209 /* Find the first C_FILE symbol. */
2210 p = cof->raw_syments;
2211 if (!p)
2212 return false;
2214 pend = p + cof->raw_syment_count;
2215 while (p < pend)
2217 if (p->u.syment.n_sclass == C_FILE)
2218 break;
2219 p += 1 + p->u.syment.n_numaux;
2222 if (p < pend)
2224 bfd_vma sec_vma;
2225 bfd_vma maxdiff;
2227 /* Look through the C_FILE symbols to find the best one. */
2228 sec_vma = bfd_get_section_vma (abfd, section);
2229 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2230 maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
2231 while (1)
2233 combined_entry_type *p2;
2235 for (p2 = p + 1 + p->u.syment.n_numaux;
2236 p2 < pend;
2237 p2 += 1 + p2->u.syment.n_numaux)
2239 if (p2->u.syment.n_scnum > 0
2240 && (section
2241 == coff_section_from_bfd_index (abfd,
2242 p2->u.syment.n_scnum)))
2243 break;
2244 if (p2->u.syment.n_sclass == C_FILE)
2246 p2 = pend;
2247 break;
2251 /* We use <= MAXDIFF here so that if we get a zero length
2252 file, we actually use the next file entry. */
2253 if (p2 < pend
2254 && offset + sec_vma >= (bfd_vma) p2->u.syment.n_value
2255 && offset + sec_vma - (bfd_vma) p2->u.syment.n_value <= maxdiff)
2257 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2258 maxdiff = offset + sec_vma - p2->u.syment.n_value;
2261 /* Avoid endless loops on erroneous files by ensuring that
2262 we always move forward in the file. */
2263 if (p - cof->raw_syments >= p->u.syment.n_value)
2264 break;
2266 p = cof->raw_syments + p->u.syment.n_value;
2267 if (p > pend || p->u.syment.n_sclass != C_FILE)
2268 break;
2272 /* Now wander though the raw linenumbers of the section */
2273 /* If we have been called on this section before, and the offset we
2274 want is further down then we can prime the lookup loop. */
2275 sec_data = coff_section_data (abfd, section);
2276 if (sec_data != NULL
2277 && sec_data->i > 0
2278 && offset >= sec_data->offset)
2280 i = sec_data->i;
2281 *functionname_ptr = sec_data->function;
2282 line_base = sec_data->line_base;
2284 else
2286 i = 0;
2287 line_base = 0;
2290 if (section->lineno != NULL)
2292 bfd_vma last_value = 0;
2294 l = &section->lineno[i];
2296 for (; i < section->lineno_count; i++)
2298 if (l->line_number == 0)
2300 /* Get the symbol this line number points at */
2301 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
2302 if (coff->symbol.value > offset)
2303 break;
2304 *functionname_ptr = coff->symbol.name;
2305 last_value = coff->symbol.value;
2306 if (coff->native)
2308 combined_entry_type *s = coff->native;
2309 s = s + 1 + s->u.syment.n_numaux;
2311 /* In XCOFF a debugging symbol can follow the
2312 function symbol. */
2313 if (s->u.syment.n_scnum == N_DEBUG)
2314 s = s + 1 + s->u.syment.n_numaux;
2316 /* S should now point to the .bf of the function. */
2317 if (s->u.syment.n_numaux)
2319 /* The linenumber is stored in the auxent. */
2320 union internal_auxent *a = &((s + 1)->u.auxent);
2321 line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
2322 *line_ptr = line_base;
2326 else
2328 if (l->u.offset > offset)
2329 break;
2330 *line_ptr = l->line_number + line_base - 1;
2332 l++;
2335 /* If we fell off the end of the loop, then assume that this
2336 symbol has no line number info. Otherwise, symbols with no
2337 line number info get reported with the line number of the
2338 last line of the last symbol which does have line number
2339 info. We use 0x100 as a slop to account for cases where the
2340 last line has executable code. */
2341 if (i >= section->lineno_count
2342 && last_value != 0
2343 && offset - last_value > 0x100)
2345 *functionname_ptr = NULL;
2346 *line_ptr = 0;
2350 /* Cache the results for the next call. */
2351 if (sec_data == NULL && section->owner == abfd)
2353 section->used_by_bfd =
2354 ((PTR) bfd_zalloc (abfd,
2355 sizeof (struct coff_section_tdata)));
2356 sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2358 if (sec_data != NULL)
2360 sec_data->offset = offset;
2361 sec_data->i = i;
2362 sec_data->function = *functionname_ptr;
2363 sec_data->line_base = line_base;
2366 return true;
2370 coff_sizeof_headers (abfd, reloc)
2371 bfd *abfd;
2372 boolean reloc;
2374 size_t size;
2376 if (reloc == false)
2378 size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
2380 else
2382 size = bfd_coff_filhsz (abfd);
2385 size += abfd->section_count * bfd_coff_scnhsz (abfd);
2386 return size;
2389 /* Change the class of a coff symbol held by BFD. */
2390 boolean
2391 bfd_coff_set_symbol_class (abfd, symbol, class)
2392 bfd * abfd;
2393 asymbol * symbol;
2394 unsigned int class;
2396 coff_symbol_type * csym;
2398 csym = coff_symbol_from (abfd, symbol);
2399 if (csym == NULL)
2401 bfd_set_error (bfd_error_invalid_operation);
2402 return false;
2404 else if (csym->native == NULL)
2406 /* This is an alien symbol which no native coff backend data.
2407 We cheat here by creating a fake native entry for it and
2408 then filling in the class. This code is based on that in
2409 coff_write_alien_symbol(). */
2411 combined_entry_type * native;
2413 native = (combined_entry_type *) bfd_alloc (abfd, sizeof (* native));
2414 if (native == NULL)
2415 return false;
2417 memset (native, 0, sizeof (* native));
2419 native->u.syment.n_type = T_NULL;
2420 native->u.syment.n_sclass = class;
2422 if (bfd_is_und_section (symbol->section))
2424 native->u.syment.n_scnum = N_UNDEF;
2425 native->u.syment.n_value = symbol->value;
2427 else if (bfd_is_com_section (symbol->section))
2429 native->u.syment.n_scnum = N_UNDEF;
2430 native->u.syment.n_value = symbol->value;
2432 else
2434 native->u.syment.n_scnum =
2435 symbol->section->output_section->target_index;
2436 native->u.syment.n_value = (symbol->value
2437 + symbol->section->output_offset);
2438 if (! obj_pe (abfd))
2439 native->u.syment.n_value += symbol->section->output_section->vma;
2441 /* Copy the any flags from the the file header into the symbol.
2442 FIXME: Why? */
2443 native->u.syment.n_flags = bfd_asymbol_bfd (& csym->symbol)->flags;
2446 csym->native = native;
2448 else
2450 csym->native->u.syment.n_sclass = class;
2453 return true;