* readelf.c (byte_get_signed): New.
[binutils.git] / bfd / coffgen.c
blobfcb00fc4737cb03b1cee6a91be9bdf715d81b5c0
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 bfd_boolean coff_write_symbol
50 PARAMS ((bfd *, asymbol *, combined_entry_type *, bfd_vma *,
51 bfd_size_type *, asection **, bfd_size_type *));
52 static bfd_boolean coff_write_alien_symbol
53 PARAMS ((bfd *, asymbol *, bfd_vma *, bfd_size_type *,
54 asection **, bfd_size_type *));
55 static bfd_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 bfd_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 bfd_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 bfd_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 PTR tdata_save;
182 bfd_size_type readsize; /* length of file_info */
183 unsigned int scnhsz;
184 char *external_sections;
186 if (!(internal_f->f_flags & F_RELFLG))
187 abfd->flags |= HAS_RELOC;
188 if ((internal_f->f_flags & F_EXEC))
189 abfd->flags |= EXEC_P;
190 if (!(internal_f->f_flags & F_LNNO))
191 abfd->flags |= HAS_LINENO;
192 if (!(internal_f->f_flags & F_LSYMS))
193 abfd->flags |= HAS_LOCALS;
195 /* FIXME: How can we set D_PAGED correctly? */
196 if ((internal_f->f_flags & F_EXEC) != 0)
197 abfd->flags |= D_PAGED;
199 bfd_get_symcount (abfd) = internal_f->f_nsyms;
200 if (internal_f->f_nsyms)
201 abfd->flags |= HAS_SYMS;
203 if (internal_a != (struct internal_aouthdr *) NULL)
204 bfd_get_start_address (abfd) = internal_a->entry;
205 else
206 bfd_get_start_address (abfd) = 0;
208 /* Set up the tdata area. ECOFF uses its own routine, and overrides
209 abfd->flags. */
210 tdata_save = abfd->tdata.any;
211 tdata = bfd_coff_mkobject_hook (abfd, (PTR) internal_f, (PTR) internal_a);
212 if (tdata == NULL)
213 goto fail2;
215 scnhsz = bfd_coff_scnhsz (abfd);
216 readsize = (bfd_size_type) nscns * scnhsz;
217 external_sections = (char *) bfd_alloc (abfd, readsize);
218 if (!external_sections)
219 goto fail;
221 if (bfd_bread ((PTR) external_sections, readsize, abfd) != readsize)
222 goto fail;
224 /* Set the arch/mach *before* swapping in sections; section header swapping
225 may depend on arch/mach info. */
226 if (! bfd_coff_set_arch_mach_hook (abfd, (PTR) internal_f))
227 goto fail;
229 /* Now copy data as required; construct all asections etc. */
230 if (nscns != 0)
232 unsigned int i;
233 for (i = 0; i < nscns; i++)
235 struct internal_scnhdr tmp;
236 bfd_coff_swap_scnhdr_in (abfd,
237 (PTR) (external_sections + i * scnhsz),
238 (PTR) & tmp);
239 if (! make_a_section_from_file (abfd, &tmp, i + 1))
240 goto fail;
244 return abfd->xvec;
246 fail:
247 bfd_release (abfd, tdata);
248 fail2:
249 abfd->tdata.any = tdata_save;
250 abfd->flags = oflags;
251 bfd_get_start_address (abfd) = ostart;
252 return (const bfd_target *) NULL;
255 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
256 not a COFF file. This is also used by ECOFF. */
258 const bfd_target *
259 coff_object_p (abfd)
260 bfd *abfd;
262 bfd_size_type filhsz;
263 bfd_size_type aoutsz;
264 unsigned int nscns;
265 PTR filehdr;
266 struct internal_filehdr internal_f;
267 struct internal_aouthdr internal_a;
269 /* figure out how much to read */
270 filhsz = bfd_coff_filhsz (abfd);
271 aoutsz = bfd_coff_aoutsz (abfd);
273 filehdr = bfd_alloc (abfd, filhsz);
274 if (filehdr == NULL)
275 return NULL;
276 if (bfd_bread (filehdr, filhsz, abfd) != filhsz)
278 if (bfd_get_error () != bfd_error_system_call)
279 bfd_set_error (bfd_error_wrong_format);
280 bfd_release (abfd, filehdr);
281 return NULL;
283 bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f);
284 bfd_release (abfd, filehdr);
286 /* The XCOFF format has two sizes for the f_opthdr. SMALL_AOUTSZ
287 (less than aoutsz) used in object files and AOUTSZ (equal to
288 aoutsz) in executables. The bfd_coff_swap_aouthdr_in function
289 expects this header to be aoutsz bytes in length, so we use that
290 value in the call to bfd_alloc below. But we must be careful to
291 only read in f_opthdr bytes in the call to bfd_bread. We should
292 also attempt to catch corrupt or non-COFF binaries with a strange
293 value for f_opthdr. */
294 if (! bfd_coff_bad_format_hook (abfd, &internal_f)
295 || internal_f.f_opthdr > aoutsz)
297 bfd_set_error (bfd_error_wrong_format);
298 return NULL;
300 nscns = internal_f.f_nscns;
302 if (internal_f.f_opthdr)
304 PTR opthdr;
306 opthdr = bfd_alloc (abfd, aoutsz);
307 if (opthdr == NULL)
308 return NULL;
309 if (bfd_bread (opthdr, (bfd_size_type) internal_f.f_opthdr, abfd)
310 != internal_f.f_opthdr)
312 bfd_release (abfd, opthdr);
313 return NULL;
315 bfd_coff_swap_aouthdr_in (abfd, opthdr, (PTR) &internal_a);
316 bfd_release (abfd, opthdr);
319 return coff_real_object_p (abfd, nscns, &internal_f,
320 (internal_f.f_opthdr != 0
321 ? &internal_a
322 : (struct internal_aouthdr *) NULL));
325 /* Get the BFD section from a COFF symbol section number. */
327 asection *
328 coff_section_from_bfd_index (abfd, index)
329 bfd *abfd;
330 int index;
332 struct sec *answer = abfd->sections;
334 if (index == N_ABS)
335 return bfd_abs_section_ptr;
336 if (index == N_UNDEF)
337 return bfd_und_section_ptr;
338 if (index == N_DEBUG)
339 return bfd_abs_section_ptr;
341 while (answer)
343 if (answer->target_index == index)
344 return answer;
345 answer = answer->next;
348 /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
349 has a bad symbol table in biglitpow.o. */
350 return bfd_und_section_ptr;
353 /* Get the upper bound of a COFF symbol table. */
355 long
356 coff_get_symtab_upper_bound (abfd)
357 bfd *abfd;
359 if (!bfd_coff_slurp_symbol_table (abfd))
360 return -1;
362 return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *));
365 /* Canonicalize a COFF symbol table. */
367 long
368 coff_get_symtab (abfd, alocation)
369 bfd *abfd;
370 asymbol **alocation;
372 unsigned int counter;
373 coff_symbol_type *symbase;
374 coff_symbol_type **location = (coff_symbol_type **) alocation;
376 if (!bfd_coff_slurp_symbol_table (abfd))
377 return -1;
379 symbase = obj_symbols (abfd);
380 counter = bfd_get_symcount (abfd);
381 while (counter-- > 0)
382 *location++ = symbase++;
384 *location = NULL;
386 return bfd_get_symcount (abfd);
389 /* Get the name of a symbol. The caller must pass in a buffer of size
390 >= SYMNMLEN + 1. */
392 const char *
393 _bfd_coff_internal_syment_name (abfd, sym, buf)
394 bfd *abfd;
395 const struct internal_syment *sym;
396 char *buf;
398 /* FIXME: It's not clear this will work correctly if sizeof
399 (_n_zeroes) != 4. */
400 if (sym->_n._n_n._n_zeroes != 0
401 || sym->_n._n_n._n_offset == 0)
403 memcpy (buf, sym->_n._n_name, SYMNMLEN);
404 buf[SYMNMLEN] = '\0';
405 return buf;
407 else
409 const char *strings;
411 BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE);
412 strings = obj_coff_strings (abfd);
413 if (strings == NULL)
415 strings = _bfd_coff_read_string_table (abfd);
416 if (strings == NULL)
417 return NULL;
419 return strings + sym->_n._n_n._n_offset;
423 /* Read in and swap the relocs. This returns a buffer holding the
424 relocs for section SEC in file ABFD. If CACHE is TRUE and
425 INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
426 the function is called again. If EXTERNAL_RELOCS is not NULL, it
427 is a buffer large enough to hold the unswapped relocs. If
428 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
429 the swapped relocs. If REQUIRE_INTERNAL is TRUE, then the return
430 value must be INTERNAL_RELOCS. The function returns NULL on error. */
432 struct internal_reloc *
433 _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
434 require_internal, internal_relocs)
435 bfd *abfd;
436 asection *sec;
437 bfd_boolean cache;
438 bfd_byte *external_relocs;
439 bfd_boolean require_internal;
440 struct internal_reloc *internal_relocs;
442 bfd_size_type relsz;
443 bfd_byte *free_external = NULL;
444 struct internal_reloc *free_internal = NULL;
445 bfd_byte *erel;
446 bfd_byte *erel_end;
447 struct internal_reloc *irel;
448 bfd_size_type amt;
450 if (coff_section_data (abfd, sec) != NULL
451 && coff_section_data (abfd, sec)->relocs != NULL)
453 if (! require_internal)
454 return coff_section_data (abfd, sec)->relocs;
455 memcpy (internal_relocs, coff_section_data (abfd, sec)->relocs,
456 sec->reloc_count * sizeof (struct internal_reloc));
457 return internal_relocs;
460 relsz = bfd_coff_relsz (abfd);
462 amt = sec->reloc_count * relsz;
463 if (external_relocs == NULL)
465 free_external = (bfd_byte *) bfd_malloc (amt);
466 if (free_external == NULL && sec->reloc_count > 0)
467 goto error_return;
468 external_relocs = free_external;
471 if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
472 || bfd_bread (external_relocs, amt, abfd) != amt)
473 goto error_return;
475 if (internal_relocs == NULL)
477 amt = sec->reloc_count;
478 amt *= sizeof (struct internal_reloc);
479 free_internal = (struct internal_reloc *) bfd_malloc (amt);
480 if (free_internal == NULL && sec->reloc_count > 0)
481 goto error_return;
482 internal_relocs = free_internal;
485 /* Swap in the relocs. */
486 erel = external_relocs;
487 erel_end = erel + relsz * sec->reloc_count;
488 irel = internal_relocs;
489 for (; erel < erel_end; erel += relsz, irel++)
490 bfd_coff_swap_reloc_in (abfd, (PTR) erel, (PTR) irel);
492 if (free_external != NULL)
494 free (free_external);
495 free_external = NULL;
498 if (cache && free_internal != NULL)
500 if (coff_section_data (abfd, sec) == NULL)
502 amt = sizeof (struct coff_section_tdata);
503 sec->used_by_bfd = (PTR) bfd_zalloc (abfd, amt);
504 if (sec->used_by_bfd == NULL)
505 goto error_return;
506 coff_section_data (abfd, sec)->contents = NULL;
508 coff_section_data (abfd, sec)->relocs = free_internal;
511 return internal_relocs;
513 error_return:
514 if (free_external != NULL)
515 free (free_external);
516 if (free_internal != NULL)
517 free (free_internal);
518 return NULL;
521 /* Set lineno_count for the output sections of a COFF file. */
524 coff_count_linenumbers (abfd)
525 bfd *abfd;
527 unsigned int limit = bfd_get_symcount (abfd);
528 unsigned int i;
529 int total = 0;
530 asymbol **p;
531 asection *s;
533 if (limit == 0)
535 /* This may be from the backend linker, in which case the
536 lineno_count in the sections is correct. */
537 for (s = abfd->sections; s != NULL; s = s->next)
538 total += s->lineno_count;
539 return total;
542 for (s = abfd->sections; s != NULL; s = s->next)
543 BFD_ASSERT (s->lineno_count == 0);
545 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
547 asymbol *q_maybe = *p;
549 if (bfd_family_coff (bfd_asymbol_bfd (q_maybe)))
551 coff_symbol_type *q = coffsymbol (q_maybe);
553 /* The AIX 4.1 compiler can sometimes generate line numbers
554 attached to debugging symbols. We try to simply ignore
555 those here. */
556 if (q->lineno != NULL
557 && q->symbol.section->owner != NULL)
559 /* This symbol has line numbers. Increment the owning
560 section's linenumber count. */
561 alent *l = q->lineno;
565 asection * sec = q->symbol.section->output_section;
567 /* Do not try to update fields in read-only sections. */
568 if (! bfd_is_const_section (sec))
569 sec->lineno_count ++;
571 ++total;
572 ++l;
574 while (l->line_number != 0);
579 return total;
582 /* Takes a bfd and a symbol, returns a pointer to the coff specific
583 area of the symbol if there is one. */
585 coff_symbol_type *
586 coff_symbol_from (ignore_abfd, symbol)
587 bfd *ignore_abfd ATTRIBUTE_UNUSED;
588 asymbol *symbol;
590 if (!bfd_family_coff (bfd_asymbol_bfd (symbol)))
591 return (coff_symbol_type *) NULL;
593 if (bfd_asymbol_bfd (symbol)->tdata.coff_obj_data == (coff_data_type *) NULL)
594 return (coff_symbol_type *) NULL;
596 return (coff_symbol_type *) symbol;
599 static void
600 fixup_symbol_value (abfd, coff_symbol_ptr, syment)
601 bfd *abfd;
602 coff_symbol_type *coff_symbol_ptr;
603 struct internal_syment *syment;
606 /* Normalize the symbol flags */
607 if (bfd_is_com_section (coff_symbol_ptr->symbol.section))
609 /* a common symbol is undefined with a value */
610 syment->n_scnum = N_UNDEF;
611 syment->n_value = coff_symbol_ptr->symbol.value;
613 else if ((coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) != 0
614 && (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING_RELOC) == 0)
616 syment->n_value = coff_symbol_ptr->symbol.value;
618 else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
620 syment->n_scnum = N_UNDEF;
621 syment->n_value = 0;
623 /* FIXME: Do we need to handle the absolute section here? */
624 else
626 if (coff_symbol_ptr->symbol.section)
628 syment->n_scnum =
629 coff_symbol_ptr->symbol.section->output_section->target_index;
631 syment->n_value = (coff_symbol_ptr->symbol.value
632 + coff_symbol_ptr->symbol.section->output_offset);
633 if (! obj_pe (abfd))
635 syment->n_value += (syment->n_sclass == C_STATLAB)
636 ? coff_symbol_ptr->symbol.section->output_section->lma
637 : coff_symbol_ptr->symbol.section->output_section->vma;
640 else
642 BFD_ASSERT (0);
643 /* This can happen, but I don't know why yet (steve@cygnus.com) */
644 syment->n_scnum = N_ABS;
645 syment->n_value = coff_symbol_ptr->symbol.value;
650 /* Run through all the symbols in the symbol table and work out what
651 their indexes into the symbol table will be when output.
653 Coff requires that each C_FILE symbol points to the next one in the
654 chain, and that the last one points to the first external symbol. We
655 do that here too. */
657 bfd_boolean
658 coff_renumber_symbols (bfd_ptr, first_undef)
659 bfd *bfd_ptr;
660 int *first_undef;
662 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
663 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
664 unsigned int native_index = 0;
665 struct internal_syment *last_file = (struct internal_syment *) NULL;
666 unsigned int symbol_index;
668 /* COFF demands that undefined symbols come after all other symbols.
669 Since we don't need to impose this extra knowledge on all our
670 client programs, deal with that here. Sort the symbol table;
671 just move the undefined symbols to the end, leaving the rest
672 alone. The O'Reilly book says that defined global symbols come
673 at the end before the undefined symbols, so we do that here as
674 well. */
675 /* @@ Do we have some condition we could test for, so we don't always
676 have to do this? I don't think relocatability is quite right, but
677 I'm not certain. [raeburn:19920508.1711EST] */
679 asymbol **newsyms;
680 unsigned int i;
681 bfd_size_type amt;
683 amt = sizeof (asymbol *) * ((bfd_size_type) symbol_count + 1);
684 newsyms = (asymbol **) bfd_alloc (bfd_ptr, amt);
685 if (!newsyms)
686 return FALSE;
687 bfd_ptr->outsymbols = newsyms;
688 for (i = 0; i < symbol_count; i++)
689 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
690 || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
691 && !bfd_is_com_section (symbol_ptr_ptr[i]->section)
692 && ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) != 0
693 || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
694 == 0))))
695 *newsyms++ = symbol_ptr_ptr[i];
697 for (i = 0; i < symbol_count; i++)
698 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
699 && !bfd_is_und_section (symbol_ptr_ptr[i]->section)
700 && (bfd_is_com_section (symbol_ptr_ptr[i]->section)
701 || ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) == 0
702 && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
703 != 0))))
704 *newsyms++ = symbol_ptr_ptr[i];
706 *first_undef = newsyms - bfd_ptr->outsymbols;
708 for (i = 0; i < symbol_count; i++)
709 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
710 && bfd_is_und_section (symbol_ptr_ptr[i]->section))
711 *newsyms++ = symbol_ptr_ptr[i];
712 *newsyms = (asymbol *) NULL;
713 symbol_ptr_ptr = bfd_ptr->outsymbols;
716 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
718 coff_symbol_type *coff_symbol_ptr = coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
719 symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
720 if (coff_symbol_ptr && coff_symbol_ptr->native)
722 combined_entry_type *s = coff_symbol_ptr->native;
723 int i;
725 if (s->u.syment.n_sclass == C_FILE)
727 if (last_file != (struct internal_syment *) NULL)
728 last_file->n_value = native_index;
729 last_file = &(s->u.syment);
731 else
734 /* Modify the symbol values according to their section and
735 type */
737 fixup_symbol_value (bfd_ptr, coff_symbol_ptr, &(s->u.syment));
739 for (i = 0; i < s->u.syment.n_numaux + 1; i++)
740 s[i].offset = native_index++;
742 else
744 native_index++;
747 obj_conv_table_size (bfd_ptr) = native_index;
749 return TRUE;
752 /* Run thorough the symbol table again, and fix it so that all
753 pointers to entries are changed to the entries' index in the output
754 symbol table. */
756 void
757 coff_mangle_symbols (bfd_ptr)
758 bfd *bfd_ptr;
760 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
761 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
762 unsigned int symbol_index;
764 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
766 coff_symbol_type *coff_symbol_ptr =
767 coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
769 if (coff_symbol_ptr && coff_symbol_ptr->native)
771 int i;
772 combined_entry_type *s = coff_symbol_ptr->native;
774 if (s->fix_value)
776 /* FIXME: We should use a union here. */
777 s->u.syment.n_value =
778 (bfd_vma)((combined_entry_type *)
779 ((unsigned long) s->u.syment.n_value))->offset;
780 s->fix_value = 0;
782 if (s->fix_line)
784 /* The value is the offset into the line number entries
785 for the symbol's section. On output, the symbol's
786 section should be N_DEBUG. */
787 s->u.syment.n_value =
788 (coff_symbol_ptr->symbol.section->output_section->line_filepos
789 + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr));
790 coff_symbol_ptr->symbol.section =
791 coff_section_from_bfd_index (bfd_ptr, N_DEBUG);
792 BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
794 for (i = 0; i < s->u.syment.n_numaux; i++)
796 combined_entry_type *a = s + i + 1;
797 if (a->fix_tag)
799 a->u.auxent.x_sym.x_tagndx.l =
800 a->u.auxent.x_sym.x_tagndx.p->offset;
801 a->fix_tag = 0;
803 if (a->fix_end)
805 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
806 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
807 a->fix_end = 0;
809 if (a->fix_scnlen)
811 a->u.auxent.x_csect.x_scnlen.l =
812 a->u.auxent.x_csect.x_scnlen.p->offset;
813 a->fix_scnlen = 0;
820 static void
821 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
822 debug_string_section_p, debug_string_size_p)
823 bfd *abfd;
824 asymbol *symbol;
825 combined_entry_type *native;
826 bfd_size_type *string_size_p;
827 asection **debug_string_section_p;
828 bfd_size_type *debug_string_size_p;
830 unsigned int name_length;
831 union internal_auxent *auxent;
832 char *name = (char *) (symbol->name);
834 if (name == (char *) NULL)
836 /* coff symbols always have names, so we'll make one up */
837 symbol->name = "strange";
838 name = (char *) symbol->name;
840 name_length = strlen (name);
842 if (native->u.syment.n_sclass == C_FILE
843 && native->u.syment.n_numaux > 0)
845 unsigned int filnmlen;
847 if (bfd_coff_force_symnames_in_strings (abfd))
849 native->u.syment._n._n_n._n_offset =
850 (*string_size_p + STRING_SIZE_SIZE);
851 native->u.syment._n._n_n._n_zeroes = 0;
852 *string_size_p += 6; /* strlen(".file") + 1 */
854 else
855 strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
857 auxent = &(native + 1)->u.auxent;
859 filnmlen = bfd_coff_filnmlen (abfd);
861 if (bfd_coff_long_filenames (abfd))
863 if (name_length <= filnmlen)
865 strncpy (auxent->x_file.x_fname, name, filnmlen);
867 else
869 auxent->x_file.x_n.x_offset = *string_size_p + STRING_SIZE_SIZE;
870 auxent->x_file.x_n.x_zeroes = 0;
871 *string_size_p += name_length + 1;
874 else
876 strncpy (auxent->x_file.x_fname, name, filnmlen);
877 if (name_length > filnmlen)
878 name[filnmlen] = '\0';
881 else
883 if (name_length <= SYMNMLEN && !bfd_coff_force_symnames_in_strings (abfd))
885 /* This name will fit into the symbol neatly */
886 strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
888 else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
890 native->u.syment._n._n_n._n_offset = (*string_size_p
891 + STRING_SIZE_SIZE);
892 native->u.syment._n._n_n._n_zeroes = 0;
893 *string_size_p += name_length + 1;
895 else
897 file_ptr filepos;
898 bfd_byte buf[4];
899 int prefix_len = bfd_coff_debug_string_prefix_length (abfd);
901 /* This name should be written into the .debug section. For
902 some reason each name is preceded by a two byte length
903 and also followed by a null byte. FIXME: We assume that
904 the .debug section has already been created, and that it
905 is large enough. */
906 if (*debug_string_section_p == (asection *) NULL)
907 *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
908 filepos = bfd_tell (abfd);
909 if (prefix_len == 4)
910 bfd_put_32 (abfd, (bfd_vma) (name_length + 1), buf);
911 else
912 bfd_put_16 (abfd, (bfd_vma) (name_length + 1), buf);
914 if (!bfd_set_section_contents (abfd,
915 *debug_string_section_p,
916 (PTR) buf,
917 (file_ptr) *debug_string_size_p,
918 (bfd_size_type) prefix_len)
919 || !bfd_set_section_contents (abfd,
920 *debug_string_section_p,
921 (PTR) symbol->name,
922 (file_ptr) (*debug_string_size_p
923 + prefix_len),
924 (bfd_size_type) name_length + 1))
925 abort ();
926 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
927 abort ();
928 native->u.syment._n._n_n._n_offset =
929 *debug_string_size_p + prefix_len;
930 native->u.syment._n._n_n._n_zeroes = 0;
931 *debug_string_size_p += name_length + 1 + prefix_len;
936 /* We need to keep track of the symbol index so that when we write out
937 the relocs we can get the index for a symbol. This method is a
938 hack. FIXME. */
940 #define set_index(symbol, idx) ((symbol)->udata.i = (idx))
942 /* Write a symbol out to a COFF file. */
944 static bfd_boolean
945 coff_write_symbol (abfd, symbol, native, written, string_size_p,
946 debug_string_section_p, debug_string_size_p)
947 bfd *abfd;
948 asymbol *symbol;
949 combined_entry_type *native;
950 bfd_vma *written;
951 bfd_size_type *string_size_p;
952 asection **debug_string_section_p;
953 bfd_size_type *debug_string_size_p;
955 unsigned int numaux = native->u.syment.n_numaux;
956 int type = native->u.syment.n_type;
957 int class = native->u.syment.n_sclass;
958 PTR buf;
959 bfd_size_type symesz;
961 if (native->u.syment.n_sclass == C_FILE)
962 symbol->flags |= BSF_DEBUGGING;
964 if (symbol->flags & BSF_DEBUGGING
965 && bfd_is_abs_section (symbol->section))
967 native->u.syment.n_scnum = N_DEBUG;
969 else if (bfd_is_abs_section (symbol->section))
971 native->u.syment.n_scnum = N_ABS;
973 else if (bfd_is_und_section (symbol->section))
975 native->u.syment.n_scnum = N_UNDEF;
977 else
979 native->u.syment.n_scnum =
980 symbol->section->output_section->target_index;
983 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
984 debug_string_section_p, debug_string_size_p);
986 symesz = bfd_coff_symesz (abfd);
987 buf = bfd_alloc (abfd, symesz);
988 if (!buf)
989 return FALSE;
990 bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
991 if (bfd_bwrite (buf, symesz, abfd) != symesz)
992 return FALSE;
993 bfd_release (abfd, buf);
995 if (native->u.syment.n_numaux > 0)
997 bfd_size_type auxesz;
998 unsigned int j;
1000 auxesz = bfd_coff_auxesz (abfd);
1001 buf = bfd_alloc (abfd, auxesz);
1002 if (!buf)
1003 return FALSE;
1004 for (j = 0; j < native->u.syment.n_numaux; j++)
1006 bfd_coff_swap_aux_out (abfd,
1007 &((native + j + 1)->u.auxent),
1008 type,
1009 class,
1010 (int) j,
1011 native->u.syment.n_numaux,
1012 buf);
1013 if (bfd_bwrite (buf, auxesz, abfd) != auxesz)
1014 return FALSE;
1016 bfd_release (abfd, buf);
1019 /* Store the index for use when we write out the relocs. */
1020 set_index (symbol, *written);
1022 *written += numaux + 1;
1023 return TRUE;
1026 /* Write out a symbol to a COFF file that does not come from a COFF
1027 file originally. This symbol may have been created by the linker,
1028 or we may be linking a non COFF file to a COFF file. */
1030 static bfd_boolean
1031 coff_write_alien_symbol (abfd, symbol, written, string_size_p,
1032 debug_string_section_p, debug_string_size_p)
1033 bfd *abfd;
1034 asymbol *symbol;
1035 bfd_vma *written;
1036 bfd_size_type *string_size_p;
1037 asection **debug_string_section_p;
1038 bfd_size_type *debug_string_size_p;
1040 combined_entry_type *native;
1041 combined_entry_type dummy;
1043 native = &dummy;
1044 native->u.syment.n_type = T_NULL;
1045 native->u.syment.n_flags = 0;
1046 if (bfd_is_und_section (symbol->section))
1048 native->u.syment.n_scnum = N_UNDEF;
1049 native->u.syment.n_value = symbol->value;
1051 else if (bfd_is_com_section (symbol->section))
1053 native->u.syment.n_scnum = N_UNDEF;
1054 native->u.syment.n_value = symbol->value;
1056 else if (symbol->flags & BSF_DEBUGGING)
1058 /* There isn't much point to writing out a debugging symbol
1059 unless we are prepared to convert it into COFF debugging
1060 format. So, we just ignore them. We must clobber the symbol
1061 name to keep it from being put in the string table. */
1062 symbol->name = "";
1063 return TRUE;
1065 else
1067 native->u.syment.n_scnum =
1068 symbol->section->output_section->target_index;
1069 native->u.syment.n_value = (symbol->value
1070 + symbol->section->output_offset);
1071 if (! obj_pe (abfd))
1072 native->u.syment.n_value += symbol->section->output_section->vma;
1074 /* Copy the any flags from the file header into the symbol.
1075 FIXME: Why? */
1077 coff_symbol_type *c = coff_symbol_from (abfd, symbol);
1078 if (c != (coff_symbol_type *) NULL)
1079 native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
1083 native->u.syment.n_type = 0;
1084 if (symbol->flags & BSF_LOCAL)
1085 native->u.syment.n_sclass = C_STAT;
1086 else if (symbol->flags & BSF_WEAK)
1087 native->u.syment.n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1088 else
1089 native->u.syment.n_sclass = C_EXT;
1090 native->u.syment.n_numaux = 0;
1092 return coff_write_symbol (abfd, symbol, native, written, string_size_p,
1093 debug_string_section_p, debug_string_size_p);
1096 /* Write a native symbol to a COFF file. */
1098 static bfd_boolean
1099 coff_write_native_symbol (abfd, symbol, written, string_size_p,
1100 debug_string_section_p, debug_string_size_p)
1101 bfd *abfd;
1102 coff_symbol_type *symbol;
1103 bfd_vma *written;
1104 bfd_size_type *string_size_p;
1105 asection **debug_string_section_p;
1106 bfd_size_type *debug_string_size_p;
1108 combined_entry_type *native = symbol->native;
1109 alent *lineno = symbol->lineno;
1111 /* If this symbol has an associated line number, we must store the
1112 symbol index in the line number field. We also tag the auxent to
1113 point to the right place in the lineno table. */
1114 if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
1116 unsigned int count = 0;
1117 lineno[count].u.offset = *written;
1118 if (native->u.syment.n_numaux)
1120 union internal_auxent *a = &((native + 1)->u.auxent);
1122 a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
1123 symbol->symbol.section->output_section->moving_line_filepos;
1126 /* Count and relocate all other linenumbers. */
1127 count++;
1128 while (lineno[count].line_number != 0)
1130 #if 0
1131 /* 13 april 92. sac
1132 I've been told this, but still need proof:
1133 > The second bug is also in `bfd/coffcode.h'. This bug
1134 > causes the linker to screw up the pc-relocations for
1135 > all the line numbers in COFF code. This bug isn't only
1136 > specific to A29K implementations, but affects all
1137 > systems using COFF format binaries. Note that in COFF
1138 > object files, the line number core offsets output by
1139 > the assembler are relative to the start of each
1140 > procedure, not to the start of the .text section. This
1141 > patch relocates the line numbers relative to the
1142 > `native->u.syment.n_value' instead of the section
1143 > virtual address.
1144 > modular!olson@cs.arizona.edu (Jon Olson)
1146 lineno[count].u.offset += native->u.syment.n_value;
1147 #else
1148 lineno[count].u.offset +=
1149 (symbol->symbol.section->output_section->vma
1150 + symbol->symbol.section->output_offset);
1151 #endif
1152 count++;
1154 symbol->done_lineno = TRUE;
1156 if (! bfd_is_const_section (symbol->symbol.section->output_section))
1157 symbol->symbol.section->output_section->moving_line_filepos +=
1158 count * bfd_coff_linesz (abfd);
1161 return coff_write_symbol (abfd, &(symbol->symbol), native, written,
1162 string_size_p, debug_string_section_p,
1163 debug_string_size_p);
1166 /* Write out the COFF symbols. */
1168 bfd_boolean
1169 coff_write_symbols (abfd)
1170 bfd *abfd;
1172 bfd_size_type string_size;
1173 asection *debug_string_section;
1174 bfd_size_type debug_string_size;
1175 unsigned int i;
1176 unsigned int limit = bfd_get_symcount (abfd);
1177 bfd_signed_vma written = 0;
1178 asymbol **p;
1180 string_size = 0;
1181 debug_string_section = NULL;
1182 debug_string_size = 0;
1184 /* If this target supports long section names, they must be put into
1185 the string table. This is supported by PE. This code must
1186 handle section names just as they are handled in
1187 coff_write_object_contents. */
1188 if (bfd_coff_long_section_names (abfd))
1190 asection *o;
1192 for (o = abfd->sections; o != NULL; o = o->next)
1194 size_t len;
1196 len = strlen (o->name);
1197 if (len > SCNNMLEN)
1198 string_size += len + 1;
1202 /* Seek to the right place */
1203 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1204 return FALSE;
1206 /* Output all the symbols we have */
1208 written = 0;
1209 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
1211 asymbol *symbol = *p;
1212 coff_symbol_type *c_symbol = coff_symbol_from (abfd, symbol);
1214 if (c_symbol == (coff_symbol_type *) NULL
1215 || c_symbol->native == (combined_entry_type *) NULL)
1217 if (!coff_write_alien_symbol (abfd, symbol, &written, &string_size,
1218 &debug_string_section,
1219 &debug_string_size))
1220 return FALSE;
1222 else
1224 if (!coff_write_native_symbol (abfd, c_symbol, &written,
1225 &string_size, &debug_string_section,
1226 &debug_string_size))
1227 return FALSE;
1231 obj_raw_syment_count (abfd) = written;
1233 /* Now write out strings */
1235 if (string_size != 0)
1237 unsigned int size = string_size + STRING_SIZE_SIZE;
1238 bfd_byte buffer[STRING_SIZE_SIZE];
1240 #if STRING_SIZE_SIZE == 4
1241 H_PUT_32 (abfd, size, buffer);
1242 #else
1243 #error Change H_PUT_32
1244 #endif
1245 if (bfd_bwrite ((PTR) buffer, (bfd_size_type) sizeof (buffer), abfd)
1246 != sizeof (buffer))
1247 return FALSE;
1249 /* Handle long section names. This code must handle section
1250 names just as they are handled in coff_write_object_contents. */
1251 if (bfd_coff_long_section_names (abfd))
1253 asection *o;
1255 for (o = abfd->sections; o != NULL; o = o->next)
1257 size_t len;
1259 len = strlen (o->name);
1260 if (len > SCNNMLEN)
1262 if (bfd_bwrite (o->name, (bfd_size_type) (len + 1), abfd)
1263 != len + 1)
1264 return FALSE;
1269 for (p = abfd->outsymbols, i = 0;
1270 i < limit;
1271 i++, p++)
1273 asymbol *q = *p;
1274 size_t name_length = strlen (q->name);
1275 coff_symbol_type *c_symbol = coff_symbol_from (abfd, q);
1276 size_t maxlen;
1278 /* Figure out whether the symbol name should go in the string
1279 table. Symbol names that are short enough are stored
1280 directly in the syment structure. File names permit a
1281 different, longer, length in the syment structure. On
1282 XCOFF, some symbol names are stored in the .debug section
1283 rather than in the string table. */
1285 if (c_symbol == NULL
1286 || c_symbol->native == NULL)
1288 /* This is not a COFF symbol, so it certainly is not a
1289 file name, nor does it go in the .debug section. */
1290 maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
1292 else if (bfd_coff_symname_in_debug (abfd,
1293 &c_symbol->native->u.syment))
1295 /* This symbol name is in the XCOFF .debug section.
1296 Don't write it into the string table. */
1297 maxlen = name_length;
1299 else if (c_symbol->native->u.syment.n_sclass == C_FILE
1300 && c_symbol->native->u.syment.n_numaux > 0)
1302 if (bfd_coff_force_symnames_in_strings (abfd))
1304 if (bfd_bwrite (".file", (bfd_size_type) 6, abfd) != 6)
1305 return FALSE;
1307 maxlen = bfd_coff_filnmlen (abfd);
1309 else
1310 maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
1312 if (name_length > maxlen)
1314 if (bfd_bwrite ((PTR) (q->name), (bfd_size_type) name_length + 1,
1315 abfd) != name_length + 1)
1316 return FALSE;
1320 else
1322 /* We would normally not write anything here, but we'll write
1323 out 4 so that any stupid coff reader which tries to read the
1324 string table even when there isn't one won't croak. */
1325 unsigned int size = STRING_SIZE_SIZE;
1326 bfd_byte buffer[STRING_SIZE_SIZE];
1328 #if STRING_SIZE_SIZE == 4
1329 H_PUT_32 (abfd, size, buffer);
1330 #else
1331 #error Change H_PUT_32
1332 #endif
1333 if (bfd_bwrite ((PTR) buffer, (bfd_size_type) STRING_SIZE_SIZE, abfd)
1334 != STRING_SIZE_SIZE)
1335 return FALSE;
1338 /* Make sure the .debug section was created to be the correct size.
1339 We should create it ourselves on the fly, but we don't because
1340 BFD won't let us write to any section until we know how large all
1341 the sections are. We could still do it by making another pass
1342 over the symbols. FIXME. */
1343 BFD_ASSERT (debug_string_size == 0
1344 || (debug_string_section != (asection *) NULL
1345 && (BFD_ALIGN (debug_string_size,
1346 1 << debug_string_section->alignment_power)
1347 == bfd_section_size (abfd, debug_string_section))));
1349 return TRUE;
1352 bfd_boolean
1353 coff_write_linenumbers (abfd)
1354 bfd *abfd;
1356 asection *s;
1357 bfd_size_type linesz;
1358 PTR buff;
1360 linesz = bfd_coff_linesz (abfd);
1361 buff = bfd_alloc (abfd, linesz);
1362 if (!buff)
1363 return FALSE;
1364 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1366 if (s->lineno_count)
1368 asymbol **q = abfd->outsymbols;
1369 if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
1370 return FALSE;
1371 /* Find all the linenumbers in this section */
1372 while (*q)
1374 asymbol *p = *q;
1375 if (p->section->output_section == s)
1377 alent *l =
1378 BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
1379 (bfd_asymbol_bfd (p), p));
1380 if (l)
1382 /* Found a linenumber entry, output */
1383 struct internal_lineno out;
1384 memset ((PTR) & out, 0, sizeof (out));
1385 out.l_lnno = 0;
1386 out.l_addr.l_symndx = l->u.offset;
1387 bfd_coff_swap_lineno_out (abfd, &out, buff);
1388 if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd)
1389 != linesz)
1390 return FALSE;
1391 l++;
1392 while (l->line_number)
1394 out.l_lnno = l->line_number;
1395 out.l_addr.l_symndx = l->u.offset;
1396 bfd_coff_swap_lineno_out (abfd, &out, buff);
1397 if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd)
1398 != linesz)
1399 return FALSE;
1400 l++;
1404 q++;
1408 bfd_release (abfd, buff);
1409 return TRUE;
1412 alent *
1413 coff_get_lineno (ignore_abfd, symbol)
1414 bfd *ignore_abfd ATTRIBUTE_UNUSED;
1415 asymbol *symbol;
1417 return coffsymbol (symbol)->lineno;
1420 #if 0
1422 /* This is only called from coff_add_missing_symbols, which has been
1423 disabled. */
1425 asymbol *
1426 coff_section_symbol (abfd, name)
1427 bfd *abfd;
1428 char *name;
1430 asection *sec = bfd_make_section_old_way (abfd, name);
1431 asymbol *sym;
1432 combined_entry_type *csym;
1434 sym = sec->symbol;
1435 csym = coff_symbol_from (abfd, sym)->native;
1436 /* Make sure back-end COFF stuff is there. */
1437 if (csym == 0)
1439 struct foo
1441 coff_symbol_type sym;
1442 /* @@FIXME This shouldn't use a fixed size!! */
1443 combined_entry_type e[10];
1445 struct foo *f;
1447 f = (struct foo *) bfd_zalloc (abfd, (bfd_size_type) sizeof (*f));
1448 if (!f)
1450 bfd_set_error (bfd_error_no_error);
1451 return NULL;
1453 coff_symbol_from (abfd, sym)->native = csym = f->e;
1455 csym[0].u.syment.n_sclass = C_STAT;
1456 csym[0].u.syment.n_numaux = 1;
1457 /* SF_SET_STATICS (sym); @@ ??? */
1458 csym[1].u.auxent.x_scn.x_scnlen = sec->_raw_size;
1459 csym[1].u.auxent.x_scn.x_nreloc = sec->reloc_count;
1460 csym[1].u.auxent.x_scn.x_nlinno = sec->lineno_count;
1462 if (sec->output_section == NULL)
1464 sec->output_section = sec;
1465 sec->output_offset = 0;
1468 return sym;
1471 #endif /* 0 */
1473 /* This function transforms the offsets into the symbol table into
1474 pointers to syments. */
1476 static void
1477 coff_pointerize_aux (abfd, table_base, symbol, indaux, auxent)
1478 bfd *abfd;
1479 combined_entry_type *table_base;
1480 combined_entry_type *symbol;
1481 unsigned int indaux;
1482 combined_entry_type *auxent;
1484 unsigned int type = symbol->u.syment.n_type;
1485 unsigned int class = symbol->u.syment.n_sclass;
1487 if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1489 if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1490 (abfd, table_base, symbol, indaux, auxent))
1491 return;
1494 /* Don't bother if this is a file or a section */
1495 if (class == C_STAT && type == T_NULL)
1496 return;
1497 if (class == C_FILE)
1498 return;
1500 /* Otherwise patch up */
1501 #define N_TMASK coff_data (abfd)->local_n_tmask
1502 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1503 if ((ISFCN (type) || ISTAG (class) || class == C_BLOCK || class == C_FCN)
1504 && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l > 0)
1506 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
1507 table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
1508 auxent->fix_end = 1;
1510 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1511 generate one, so we must be careful to ignore it. */
1512 if (auxent->u.auxent.x_sym.x_tagndx.l > 0)
1514 auxent->u.auxent.x_sym.x_tagndx.p =
1515 table_base + auxent->u.auxent.x_sym.x_tagndx.l;
1516 auxent->fix_tag = 1;
1520 /* Allocate space for the ".debug" section, and read it.
1521 We did not read the debug section until now, because
1522 we didn't want to go to the trouble until someone needed it. */
1524 static char *
1525 build_debug_section (abfd)
1526 bfd *abfd;
1528 char *debug_section;
1529 file_ptr position;
1530 bfd_size_type sec_size;
1532 asection *sect = bfd_get_section_by_name (abfd, ".debug");
1534 if (!sect)
1536 bfd_set_error (bfd_error_no_debug_section);
1537 return NULL;
1540 sec_size = bfd_get_section_size_before_reloc (sect);
1541 debug_section = (PTR) bfd_alloc (abfd, sec_size);
1542 if (debug_section == NULL)
1543 return NULL;
1545 /* Seek to the beginning of the `.debug' section and read it.
1546 Save the current position first; it is needed by our caller.
1547 Then read debug section and reset the file pointer. */
1549 position = bfd_tell (abfd);
1550 if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0
1551 || bfd_bread (debug_section, sec_size, abfd) != sec_size
1552 || bfd_seek (abfd, position, SEEK_SET) != 0)
1553 return NULL;
1554 return debug_section;
1557 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1558 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1559 be \0-terminated. */
1560 static char *
1561 copy_name (abfd, name, maxlen)
1562 bfd *abfd;
1563 char *name;
1564 size_t maxlen;
1566 size_t len;
1567 char *newname;
1569 for (len = 0; len < maxlen; ++len)
1571 if (name[len] == '\0')
1573 break;
1577 if ((newname = (PTR) bfd_alloc (abfd, (bfd_size_type) len + 1)) == NULL)
1578 return (NULL);
1579 strncpy (newname, name, len);
1580 newname[len] = '\0';
1581 return newname;
1584 /* Read in the external symbols. */
1586 bfd_boolean
1587 _bfd_coff_get_external_symbols (abfd)
1588 bfd *abfd;
1590 bfd_size_type symesz;
1591 bfd_size_type size;
1592 PTR syms;
1594 if (obj_coff_external_syms (abfd) != NULL)
1595 return TRUE;
1597 symesz = bfd_coff_symesz (abfd);
1599 size = obj_raw_syment_count (abfd) * symesz;
1601 syms = (PTR) bfd_malloc (size);
1602 if (syms == NULL && size != 0)
1603 return FALSE;
1605 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
1606 || bfd_bread (syms, size, abfd) != size)
1608 if (syms != NULL)
1609 free (syms);
1610 return FALSE;
1613 obj_coff_external_syms (abfd) = syms;
1615 return TRUE;
1618 /* Read in the external strings. The strings are not loaded until
1619 they are needed. This is because we have no simple way of
1620 detecting a missing string table in an archive. */
1622 const char *
1623 _bfd_coff_read_string_table (abfd)
1624 bfd *abfd;
1626 char extstrsize[STRING_SIZE_SIZE];
1627 bfd_size_type strsize;
1628 char *strings;
1629 file_ptr pos;
1631 if (obj_coff_strings (abfd) != NULL)
1632 return obj_coff_strings (abfd);
1634 if (obj_sym_filepos (abfd) == 0)
1636 bfd_set_error (bfd_error_no_symbols);
1637 return NULL;
1640 pos = obj_sym_filepos (abfd);
1641 pos += obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd);
1642 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
1643 return NULL;
1645 if (bfd_bread (extstrsize, (bfd_size_type) sizeof extstrsize, abfd)
1646 != sizeof extstrsize)
1648 if (bfd_get_error () != bfd_error_file_truncated)
1649 return NULL;
1651 /* There is no string table. */
1652 strsize = STRING_SIZE_SIZE;
1654 else
1656 #if STRING_SIZE_SIZE == 4
1657 strsize = H_GET_32 (abfd, extstrsize);
1658 #else
1659 #error Change H_GET_32
1660 #endif
1663 if (strsize < STRING_SIZE_SIZE)
1665 (*_bfd_error_handler)
1666 (_("%s: bad string table size %lu"), bfd_archive_filename (abfd),
1667 (unsigned long) strsize);
1668 bfd_set_error (bfd_error_bad_value);
1669 return NULL;
1672 strings = (char *) bfd_malloc (strsize);
1673 if (strings == NULL)
1674 return NULL;
1676 if (bfd_bread (strings + STRING_SIZE_SIZE, strsize - STRING_SIZE_SIZE, abfd)
1677 != strsize - STRING_SIZE_SIZE)
1679 free (strings);
1680 return NULL;
1683 obj_coff_strings (abfd) = strings;
1685 return strings;
1688 /* Free up the external symbols and strings read from a COFF file. */
1690 bfd_boolean
1691 _bfd_coff_free_symbols (abfd)
1692 bfd *abfd;
1694 if (obj_coff_external_syms (abfd) != NULL
1695 && ! obj_coff_keep_syms (abfd))
1697 free (obj_coff_external_syms (abfd));
1698 obj_coff_external_syms (abfd) = NULL;
1700 if (obj_coff_strings (abfd) != NULL
1701 && ! obj_coff_keep_strings (abfd))
1703 free (obj_coff_strings (abfd));
1704 obj_coff_strings (abfd) = NULL;
1706 return TRUE;
1709 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1710 knit the symbol names into a normalized form. By normalized here I
1711 mean that all symbols have an n_offset pointer that points to a null-
1712 terminated string. */
1714 combined_entry_type *
1715 coff_get_normalized_symtab (abfd)
1716 bfd *abfd;
1718 combined_entry_type *internal;
1719 combined_entry_type *internal_ptr;
1720 combined_entry_type *symbol_ptr;
1721 combined_entry_type *internal_end;
1722 size_t symesz;
1723 char *raw_src;
1724 char *raw_end;
1725 const char *string_table = NULL;
1726 char *debug_section = NULL;
1727 bfd_size_type size;
1729 if (obj_raw_syments (abfd) != NULL)
1730 return obj_raw_syments (abfd);
1732 size = obj_raw_syment_count (abfd) * sizeof (combined_entry_type);
1733 internal = (combined_entry_type *) bfd_zalloc (abfd, size);
1734 if (internal == NULL && size != 0)
1735 return NULL;
1736 internal_end = internal + obj_raw_syment_count (abfd);
1738 if (! _bfd_coff_get_external_symbols (abfd))
1739 return NULL;
1741 raw_src = (char *) obj_coff_external_syms (abfd);
1743 /* mark the end of the symbols */
1744 symesz = bfd_coff_symesz (abfd);
1745 raw_end = (char *) raw_src + obj_raw_syment_count (abfd) * symesz;
1747 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1748 probably possible. If one shows up, it will probably kill us. */
1750 /* Swap all the raw entries */
1751 for (internal_ptr = internal;
1752 raw_src < raw_end;
1753 raw_src += symesz, internal_ptr++)
1756 unsigned int i;
1757 bfd_coff_swap_sym_in (abfd, (PTR) raw_src,
1758 (PTR) & internal_ptr->u.syment);
1759 symbol_ptr = internal_ptr;
1761 for (i = 0;
1762 i < symbol_ptr->u.syment.n_numaux;
1763 i++)
1765 internal_ptr++;
1766 raw_src += symesz;
1767 bfd_coff_swap_aux_in (abfd, (PTR) raw_src,
1768 symbol_ptr->u.syment.n_type,
1769 symbol_ptr->u.syment.n_sclass,
1770 (int) i, symbol_ptr->u.syment.n_numaux,
1771 &(internal_ptr->u.auxent));
1772 coff_pointerize_aux (abfd, internal, symbol_ptr, i,
1773 internal_ptr);
1777 /* Free the raw symbols, but not the strings (if we have them). */
1778 obj_coff_keep_strings (abfd) = TRUE;
1779 if (! _bfd_coff_free_symbols (abfd))
1780 return NULL;
1782 for (internal_ptr = internal; internal_ptr < internal_end;
1783 internal_ptr++)
1785 if (internal_ptr->u.syment.n_sclass == C_FILE
1786 && internal_ptr->u.syment.n_numaux > 0)
1788 /* make a file symbol point to the name in the auxent, since
1789 the text ".file" is redundant */
1790 if ((internal_ptr + 1)->u.auxent.x_file.x_n.x_zeroes == 0)
1792 /* the filename is a long one, point into the string table */
1793 if (string_table == NULL)
1795 string_table = _bfd_coff_read_string_table (abfd);
1796 if (string_table == NULL)
1797 return NULL;
1800 internal_ptr->u.syment._n._n_n._n_offset =
1801 ((long)
1802 (string_table
1803 + (internal_ptr + 1)->u.auxent.x_file.x_n.x_offset));
1805 else
1807 /* Ordinary short filename, put into memory anyway. The
1808 Microsoft PE tools sometimes store a filename in
1809 multiple AUX entries. */
1810 if (internal_ptr->u.syment.n_numaux > 1
1811 && coff_data (abfd)->pe)
1813 internal_ptr->u.syment._n._n_n._n_offset =
1814 ((long)
1815 copy_name (abfd,
1816 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1817 internal_ptr->u.syment.n_numaux * symesz));
1819 else
1821 internal_ptr->u.syment._n._n_n._n_offset =
1822 ((long)
1823 copy_name (abfd,
1824 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1825 (size_t) bfd_coff_filnmlen (abfd)));
1829 else
1831 if (internal_ptr->u.syment._n._n_n._n_zeroes != 0)
1833 /* This is a "short" name. Make it long. */
1834 size_t i;
1835 char *newstring;
1837 /* find the length of this string without walking into memory
1838 that isn't ours. */
1839 for (i = 0; i < 8; ++i)
1840 if (internal_ptr->u.syment._n._n_name[i] == '\0')
1841 break;
1843 newstring = (PTR) bfd_zalloc (abfd, (bfd_size_type) (i + 1));
1844 if (newstring == NULL)
1845 return (NULL);
1846 strncpy (newstring, internal_ptr->u.syment._n._n_name, i);
1847 internal_ptr->u.syment._n._n_n._n_offset = (long int) newstring;
1848 internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1850 else if (internal_ptr->u.syment._n._n_n._n_offset == 0)
1851 internal_ptr->u.syment._n._n_n._n_offset = (long int) "";
1852 else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment))
1854 /* Long name already. Point symbol at the string in the
1855 table. */
1856 if (string_table == NULL)
1858 string_table = _bfd_coff_read_string_table (abfd);
1859 if (string_table == NULL)
1860 return NULL;
1862 internal_ptr->u.syment._n._n_n._n_offset =
1863 ((long int)
1864 (string_table
1865 + internal_ptr->u.syment._n._n_n._n_offset));
1867 else
1869 /* Long name in debug section. Very similar. */
1870 if (debug_section == NULL)
1871 debug_section = build_debug_section (abfd);
1872 internal_ptr->u.syment._n._n_n._n_offset = (long int)
1873 (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
1876 internal_ptr += internal_ptr->u.syment.n_numaux;
1879 obj_raw_syments (abfd) = internal;
1880 BFD_ASSERT (obj_raw_syment_count (abfd)
1881 == (unsigned int) (internal_ptr - internal));
1883 return (internal);
1884 } /* coff_get_normalized_symtab() */
1886 long
1887 coff_get_reloc_upper_bound (abfd, asect)
1888 bfd *abfd;
1889 sec_ptr asect;
1891 if (bfd_get_format (abfd) != bfd_object)
1893 bfd_set_error (bfd_error_invalid_operation);
1894 return -1;
1896 return (asect->reloc_count + 1) * sizeof (arelent *);
1899 asymbol *
1900 coff_make_empty_symbol (abfd)
1901 bfd *abfd;
1903 bfd_size_type amt = sizeof (coff_symbol_type);
1904 coff_symbol_type *new = (coff_symbol_type *) bfd_zalloc (abfd, amt);
1905 if (new == NULL)
1906 return (NULL);
1907 new->symbol.section = 0;
1908 new->native = 0;
1909 new->lineno = (alent *) NULL;
1910 new->done_lineno = FALSE;
1911 new->symbol.the_bfd = abfd;
1912 return &new->symbol;
1915 /* Make a debugging symbol. */
1917 asymbol *
1918 coff_bfd_make_debug_symbol (abfd, ptr, sz)
1919 bfd *abfd;
1920 PTR ptr ATTRIBUTE_UNUSED;
1921 unsigned long sz ATTRIBUTE_UNUSED;
1923 bfd_size_type amt = sizeof (coff_symbol_type);
1924 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, amt);
1925 if (new == NULL)
1926 return (NULL);
1927 /* @@ The 10 is a guess at a plausible maximum number of aux entries
1928 (but shouldn't be a constant). */
1929 amt = sizeof (combined_entry_type) * 10;
1930 new->native = (combined_entry_type *) bfd_zalloc (abfd, amt);
1931 if (!new->native)
1932 return (NULL);
1933 new->symbol.section = bfd_abs_section_ptr;
1934 new->symbol.flags = BSF_DEBUGGING;
1935 new->lineno = (alent *) NULL;
1936 new->done_lineno = FALSE;
1937 new->symbol.the_bfd = abfd;
1938 return &new->symbol;
1941 void
1942 coff_get_symbol_info (abfd, symbol, ret)
1943 bfd *abfd;
1944 asymbol *symbol;
1945 symbol_info *ret;
1947 bfd_symbol_info (symbol, ret);
1948 if (coffsymbol (symbol)->native != NULL
1949 && coffsymbol (symbol)->native->fix_value)
1951 ret->value = coffsymbol (symbol)->native->u.syment.n_value -
1952 (unsigned long) obj_raw_syments (abfd);
1956 /* Return the COFF syment for a symbol. */
1958 bfd_boolean
1959 bfd_coff_get_syment (abfd, symbol, psyment)
1960 bfd *abfd;
1961 asymbol *symbol;
1962 struct internal_syment *psyment;
1964 coff_symbol_type *csym;
1966 csym = coff_symbol_from (abfd, symbol);
1967 if (csym == NULL || csym->native == NULL)
1969 bfd_set_error (bfd_error_invalid_operation);
1970 return FALSE;
1973 *psyment = csym->native->u.syment;
1975 if (csym->native->fix_value)
1976 psyment->n_value = psyment->n_value -
1977 (unsigned long) obj_raw_syments (abfd);
1979 /* FIXME: We should handle fix_line here. */
1981 return TRUE;
1984 /* Return the COFF auxent for a symbol. */
1986 bfd_boolean
1987 bfd_coff_get_auxent (abfd, symbol, indx, pauxent)
1988 bfd *abfd;
1989 asymbol *symbol;
1990 int indx;
1991 union internal_auxent *pauxent;
1993 coff_symbol_type *csym;
1994 combined_entry_type *ent;
1996 csym = coff_symbol_from (abfd, symbol);
1998 if (csym == NULL
1999 || csym->native == NULL
2000 || indx >= csym->native->u.syment.n_numaux)
2002 bfd_set_error (bfd_error_invalid_operation);
2003 return FALSE;
2006 ent = csym->native + indx + 1;
2008 *pauxent = ent->u.auxent;
2010 if (ent->fix_tag)
2011 pauxent->x_sym.x_tagndx.l =
2012 ((combined_entry_type *) pauxent->x_sym.x_tagndx.p
2013 - obj_raw_syments (abfd));
2015 if (ent->fix_end)
2016 pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l =
2017 ((combined_entry_type *) pauxent->x_sym.x_fcnary.x_fcn.x_endndx.p
2018 - obj_raw_syments (abfd));
2020 if (ent->fix_scnlen)
2021 pauxent->x_csect.x_scnlen.l =
2022 ((combined_entry_type *) pauxent->x_csect.x_scnlen.p
2023 - obj_raw_syments (abfd));
2025 return TRUE;
2028 /* Print out information about COFF symbol. */
2030 void
2031 coff_print_symbol (abfd, filep, symbol, how)
2032 bfd *abfd;
2033 PTR filep;
2034 asymbol *symbol;
2035 bfd_print_symbol_type how;
2037 FILE *file = (FILE *) filep;
2039 switch (how)
2041 case bfd_print_symbol_name:
2042 fprintf (file, "%s", symbol->name);
2043 break;
2045 case bfd_print_symbol_more:
2046 fprintf (file, "coff %s %s",
2047 coffsymbol (symbol)->native ? "n" : "g",
2048 coffsymbol (symbol)->lineno ? "l" : " ");
2049 break;
2051 case bfd_print_symbol_all:
2052 if (coffsymbol (symbol)->native)
2054 bfd_vma val;
2055 unsigned int aux;
2056 combined_entry_type *combined = coffsymbol (symbol)->native;
2057 combined_entry_type *root = obj_raw_syments (abfd);
2058 struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
2060 fprintf (file, "[%3ld]", (long) (combined - root));
2062 if (! combined->fix_value)
2063 val = (bfd_vma) combined->u.syment.n_value;
2064 else
2065 val = combined->u.syment.n_value - (unsigned long) root;
2067 #ifndef XCOFF64
2068 fprintf (file,
2069 "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x%08lx %s",
2070 combined->u.syment.n_scnum,
2071 combined->u.syment.n_flags,
2072 combined->u.syment.n_type,
2073 combined->u.syment.n_sclass,
2074 combined->u.syment.n_numaux,
2075 (unsigned long) val,
2076 symbol->name);
2077 #else
2078 /* Print out the wide, 64 bit, symbol value */
2079 fprintf (file,
2080 "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x%016llx %s",
2081 combined->u.syment.n_scnum,
2082 combined->u.syment.n_flags,
2083 combined->u.syment.n_type,
2084 combined->u.syment.n_sclass,
2085 combined->u.syment.n_numaux,
2086 val,
2087 symbol->name);
2088 #endif
2090 for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
2092 combined_entry_type *auxp = combined + aux + 1;
2093 long tagndx;
2095 if (auxp->fix_tag)
2096 tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
2097 else
2098 tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
2100 fprintf (file, "\n");
2102 if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux))
2103 continue;
2105 switch (combined->u.syment.n_sclass)
2107 case C_FILE:
2108 fprintf (file, "File ");
2109 break;
2111 case C_STAT:
2112 if (combined->u.syment.n_type == T_NULL)
2113 /* probably a section symbol? */
2115 fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
2116 (long) auxp->u.auxent.x_scn.x_scnlen,
2117 auxp->u.auxent.x_scn.x_nreloc,
2118 auxp->u.auxent.x_scn.x_nlinno);
2119 if (auxp->u.auxent.x_scn.x_checksum != 0
2120 || auxp->u.auxent.x_scn.x_associated != 0
2121 || auxp->u.auxent.x_scn.x_comdat != 0)
2122 fprintf (file, " checksum 0x%lx assoc %d comdat %d",
2123 auxp->u.auxent.x_scn.x_checksum,
2124 auxp->u.auxent.x_scn.x_associated,
2125 auxp->u.auxent.x_scn.x_comdat);
2126 break;
2128 /* else fall through */
2129 case C_EXT:
2130 if (ISFCN (combined->u.syment.n_type))
2132 long next, llnos;
2134 if (auxp->fix_end)
2135 next = (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2136 - root);
2137 else
2138 next = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
2139 llnos = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_lnnoptr;
2140 fprintf (file,
2141 "AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld",
2142 tagndx, auxp->u.auxent.x_sym.x_misc.x_fsize,
2143 llnos, next);
2144 break;
2146 /* else fall through */
2147 default:
2148 fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
2149 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
2150 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
2151 tagndx);
2152 if (auxp->fix_end)
2153 fprintf (file, " endndx %ld",
2154 ((long)
2155 (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2156 - root)));
2157 break;
2161 if (l)
2163 fprintf (file, "\n%s :", l->u.sym->name);
2164 l++;
2165 while (l->line_number)
2167 fprintf (file, "\n%4d : 0x%lx",
2168 l->line_number,
2169 ((unsigned long)
2170 (l->u.offset + symbol->section->vma)));
2171 l++;
2175 else
2177 bfd_print_symbol_vandf (abfd, (PTR) file, symbol);
2178 fprintf (file, " %-5s %s %s %s",
2179 symbol->section->name,
2180 coffsymbol (symbol)->native ? "n" : "g",
2181 coffsymbol (symbol)->lineno ? "l" : " ",
2182 symbol->name);
2187 /* Return whether a symbol name implies a local symbol. In COFF,
2188 local symbols generally start with ``.L''. Most targets use this
2189 function for the is_local_label_name entry point, but some may
2190 override it. */
2192 bfd_boolean
2193 _bfd_coff_is_local_label_name (abfd, name)
2194 bfd *abfd ATTRIBUTE_UNUSED;
2195 const char *name;
2197 return name[0] == '.' && name[1] == 'L';
2200 /* Provided a BFD, a section and an offset (in bytes, not octets) into the
2201 section, calculate and return the name of the source file and the line
2202 nearest to the wanted location. */
2204 bfd_boolean
2205 coff_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
2206 functionname_ptr, line_ptr)
2207 bfd *abfd;
2208 asection *section;
2209 asymbol **symbols;
2210 bfd_vma offset;
2211 const char **filename_ptr;
2212 const char **functionname_ptr;
2213 unsigned int *line_ptr;
2215 bfd_boolean found;
2216 unsigned int i;
2217 unsigned int line_base;
2218 coff_data_type *cof = coff_data (abfd);
2219 /* Run through the raw syments if available */
2220 combined_entry_type *p;
2221 combined_entry_type *pend;
2222 alent *l;
2223 struct coff_section_tdata *sec_data;
2224 bfd_size_type amt;
2226 /* Before looking through the symbol table, try to use a .stab
2227 section to find the information. */
2228 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
2229 &found, filename_ptr,
2230 functionname_ptr, line_ptr,
2231 &coff_data(abfd)->line_info))
2232 return FALSE;
2234 if (found)
2235 return TRUE;
2237 /* Also try examining DWARF2 debugging information. */
2238 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
2239 filename_ptr, functionname_ptr,
2240 line_ptr, 0,
2241 &coff_data(abfd)->dwarf2_find_line_info))
2242 return TRUE;
2244 *filename_ptr = 0;
2245 *functionname_ptr = 0;
2246 *line_ptr = 0;
2248 /* Don't try and find line numbers in a non coff file */
2249 if (!bfd_family_coff (abfd))
2250 return FALSE;
2252 if (cof == NULL)
2253 return FALSE;
2255 /* Find the first C_FILE symbol. */
2256 p = cof->raw_syments;
2257 if (!p)
2258 return FALSE;
2260 pend = p + cof->raw_syment_count;
2261 while (p < pend)
2263 if (p->u.syment.n_sclass == C_FILE)
2264 break;
2265 p += 1 + p->u.syment.n_numaux;
2268 if (p < pend)
2270 bfd_vma sec_vma;
2271 bfd_vma maxdiff;
2273 /* Look through the C_FILE symbols to find the best one. */
2274 sec_vma = bfd_get_section_vma (abfd, section);
2275 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2276 maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
2277 while (1)
2279 combined_entry_type *p2;
2281 for (p2 = p + 1 + p->u.syment.n_numaux;
2282 p2 < pend;
2283 p2 += 1 + p2->u.syment.n_numaux)
2285 if (p2->u.syment.n_scnum > 0
2286 && (section
2287 == coff_section_from_bfd_index (abfd,
2288 p2->u.syment.n_scnum)))
2289 break;
2290 if (p2->u.syment.n_sclass == C_FILE)
2292 p2 = pend;
2293 break;
2297 /* We use <= MAXDIFF here so that if we get a zero length
2298 file, we actually use the next file entry. */
2299 if (p2 < pend
2300 && offset + sec_vma >= (bfd_vma) p2->u.syment.n_value
2301 && offset + sec_vma - (bfd_vma) p2->u.syment.n_value <= maxdiff)
2303 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2304 maxdiff = offset + sec_vma - p2->u.syment.n_value;
2307 /* Avoid endless loops on erroneous files by ensuring that
2308 we always move forward in the file. */
2309 if (p >= cof->raw_syments + p->u.syment.n_value)
2310 break;
2312 p = cof->raw_syments + p->u.syment.n_value;
2313 if (p > pend || p->u.syment.n_sclass != C_FILE)
2314 break;
2318 /* Now wander though the raw linenumbers of the section */
2319 /* If we have been called on this section before, and the offset we
2320 want is further down then we can prime the lookup loop. */
2321 sec_data = coff_section_data (abfd, section);
2322 if (sec_data != NULL
2323 && sec_data->i > 0
2324 && offset >= sec_data->offset)
2326 i = sec_data->i;
2327 *functionname_ptr = sec_data->function;
2328 line_base = sec_data->line_base;
2330 else
2332 i = 0;
2333 line_base = 0;
2336 if (section->lineno != NULL)
2338 bfd_vma last_value = 0;
2340 l = &section->lineno[i];
2342 for (; i < section->lineno_count; i++)
2344 if (l->line_number == 0)
2346 /* Get the symbol this line number points at */
2347 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
2348 if (coff->symbol.value > offset)
2349 break;
2350 *functionname_ptr = coff->symbol.name;
2351 last_value = coff->symbol.value;
2352 if (coff->native)
2354 combined_entry_type *s = coff->native;
2355 s = s + 1 + s->u.syment.n_numaux;
2357 /* In XCOFF a debugging symbol can follow the
2358 function symbol. */
2359 if (s->u.syment.n_scnum == N_DEBUG)
2360 s = s + 1 + s->u.syment.n_numaux;
2362 /* S should now point to the .bf of the function. */
2363 if (s->u.syment.n_numaux)
2365 /* The linenumber is stored in the auxent. */
2366 union internal_auxent *a = &((s + 1)->u.auxent);
2367 line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
2368 *line_ptr = line_base;
2372 else
2374 if (l->u.offset > offset)
2375 break;
2376 *line_ptr = l->line_number + line_base - 1;
2378 l++;
2381 /* If we fell off the end of the loop, then assume that this
2382 symbol has no line number info. Otherwise, symbols with no
2383 line number info get reported with the line number of the
2384 last line of the last symbol which does have line number
2385 info. We use 0x100 as a slop to account for cases where the
2386 last line has executable code. */
2387 if (i >= section->lineno_count
2388 && last_value != 0
2389 && offset - last_value > 0x100)
2391 *functionname_ptr = NULL;
2392 *line_ptr = 0;
2396 /* Cache the results for the next call. */
2397 if (sec_data == NULL && section->owner == abfd)
2399 amt = sizeof (struct coff_section_tdata);
2400 section->used_by_bfd = (PTR) bfd_zalloc (abfd, amt);
2401 sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2403 if (sec_data != NULL)
2405 sec_data->offset = offset;
2406 sec_data->i = i;
2407 sec_data->function = *functionname_ptr;
2408 sec_data->line_base = line_base;
2411 return TRUE;
2415 coff_sizeof_headers (abfd, reloc)
2416 bfd *abfd;
2417 bfd_boolean reloc;
2419 size_t size;
2421 if (! reloc)
2423 size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
2425 else
2427 size = bfd_coff_filhsz (abfd);
2430 size += abfd->section_count * bfd_coff_scnhsz (abfd);
2431 return size;
2434 /* Change the class of a coff symbol held by BFD. */
2435 bfd_boolean
2436 bfd_coff_set_symbol_class (abfd, symbol, class)
2437 bfd * abfd;
2438 asymbol * symbol;
2439 unsigned int class;
2441 coff_symbol_type * csym;
2443 csym = coff_symbol_from (abfd, symbol);
2444 if (csym == NULL)
2446 bfd_set_error (bfd_error_invalid_operation);
2447 return FALSE;
2449 else if (csym->native == NULL)
2451 /* This is an alien symbol which no native coff backend data.
2452 We cheat here by creating a fake native entry for it and
2453 then filling in the class. This code is based on that in
2454 coff_write_alien_symbol(). */
2456 combined_entry_type * native;
2457 bfd_size_type amt = sizeof (* native);
2459 native = (combined_entry_type *) bfd_zalloc (abfd, amt);
2460 if (native == NULL)
2461 return FALSE;
2463 native->u.syment.n_type = T_NULL;
2464 native->u.syment.n_sclass = class;
2466 if (bfd_is_und_section (symbol->section))
2468 native->u.syment.n_scnum = N_UNDEF;
2469 native->u.syment.n_value = symbol->value;
2471 else if (bfd_is_com_section (symbol->section))
2473 native->u.syment.n_scnum = N_UNDEF;
2474 native->u.syment.n_value = symbol->value;
2476 else
2478 native->u.syment.n_scnum =
2479 symbol->section->output_section->target_index;
2480 native->u.syment.n_value = (symbol->value
2481 + symbol->section->output_offset);
2482 if (! obj_pe (abfd))
2483 native->u.syment.n_value += symbol->section->output_section->vma;
2485 /* Copy the any flags from the file header into the symbol.
2486 FIXME: Why? */
2487 native->u.syment.n_flags = bfd_asymbol_bfd (& csym->symbol)->flags;
2490 csym->native = native;
2492 else
2494 csym->native->u.syment.n_sclass = class;
2497 return TRUE;