1999-09-11 Donn Terry <donn@interix.com>
[binutils.git] / bfd / coffgen.c
blob547a15f883cadc79e4303d60c9f2676dfc600f86
1 /* Support for the generic parts of COFF, for BFD.
2 Copyright 1990, 91, 92, 93, 94, 95, 96, 97, 98, 1999
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 /* Now copy data as required; construct all asections etc */
215 if (nscns != 0)
217 unsigned int i;
218 for (i = 0; i < nscns; i++)
220 struct internal_scnhdr tmp;
221 bfd_coff_swap_scnhdr_in (abfd,
222 (PTR) (external_sections + i * scnhsz),
223 (PTR) & tmp);
224 if (! make_a_section_from_file (abfd, &tmp, i + 1))
225 goto fail;
229 /* make_abs_section (abfd); */
231 if (bfd_coff_set_arch_mach_hook (abfd, (PTR) internal_f) == false)
232 goto fail;
234 return abfd->xvec;
236 fail:
237 bfd_release (abfd, tdata);
238 abfd->flags = oflags;
239 bfd_get_start_address (abfd) = ostart;
240 return (const bfd_target *) NULL;
243 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
244 not a COFF file. This is also used by ECOFF. */
246 const bfd_target *
247 coff_object_p (abfd)
248 bfd *abfd;
250 unsigned int filhsz;
251 unsigned int aoutsz;
252 int nscns;
253 PTR filehdr;
254 struct internal_filehdr internal_f;
255 struct internal_aouthdr internal_a;
257 /* figure out how much to read */
258 filhsz = bfd_coff_filhsz (abfd);
259 aoutsz = bfd_coff_aoutsz (abfd);
261 filehdr = bfd_alloc (abfd, filhsz);
262 if (filehdr == NULL)
263 return 0;
264 if (bfd_read (filehdr, 1, filhsz, abfd) != filhsz)
266 if (bfd_get_error () != bfd_error_system_call)
267 bfd_set_error (bfd_error_wrong_format);
268 return 0;
270 bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f);
271 bfd_release (abfd, filehdr);
273 if (bfd_coff_bad_format_hook (abfd, &internal_f) == false)
275 bfd_set_error (bfd_error_wrong_format);
276 return 0;
278 nscns = internal_f.f_nscns;
280 if (internal_f.f_opthdr)
282 PTR opthdr;
284 opthdr = bfd_alloc (abfd, aoutsz);
285 if (opthdr == NULL)
286 return 0;;
287 if (bfd_read (opthdr, 1, internal_f.f_opthdr, abfd)
288 != internal_f.f_opthdr)
290 return 0;
292 bfd_coff_swap_aouthdr_in (abfd, opthdr, (PTR) &internal_a);
295 return coff_real_object_p (abfd, nscns, &internal_f,
296 (internal_f.f_opthdr != 0
297 ? &internal_a
298 : (struct internal_aouthdr *) NULL));
301 /* Get the BFD section from a COFF symbol section number. */
303 asection *
304 coff_section_from_bfd_index (abfd, index)
305 bfd *abfd;
306 int index;
308 struct sec *answer = abfd->sections;
310 if (index == N_ABS)
311 return bfd_abs_section_ptr;
312 if (index == N_UNDEF)
313 return bfd_und_section_ptr;
314 if (index == N_DEBUG)
315 return bfd_abs_section_ptr;
317 while (answer)
319 if (answer->target_index == index)
320 return answer;
321 answer = answer->next;
324 /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
325 has a bad symbol table in biglitpow.o. */
326 return bfd_und_section_ptr;
329 /* Get the upper bound of a COFF symbol table. */
331 long
332 coff_get_symtab_upper_bound (abfd)
333 bfd *abfd;
335 if (!bfd_coff_slurp_symbol_table (abfd))
336 return -1;
338 return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *));
342 /* Canonicalize a COFF symbol table. */
344 long
345 coff_get_symtab (abfd, alocation)
346 bfd *abfd;
347 asymbol **alocation;
349 unsigned int counter;
350 coff_symbol_type *symbase;
351 coff_symbol_type **location = (coff_symbol_type **) alocation;
353 if (!bfd_coff_slurp_symbol_table (abfd))
354 return -1;
356 symbase = obj_symbols (abfd);
357 counter = bfd_get_symcount (abfd);
358 while (counter-- > 0)
359 *location++ = symbase++;
361 *location = NULL;
363 return bfd_get_symcount (abfd);
366 /* Get the name of a symbol. The caller must pass in a buffer of size
367 >= SYMNMLEN + 1. */
369 const char *
370 _bfd_coff_internal_syment_name (abfd, sym, buf)
371 bfd *abfd;
372 const struct internal_syment *sym;
373 char *buf;
375 /* FIXME: It's not clear this will work correctly if sizeof
376 (_n_zeroes) != 4. */
377 if (sym->_n._n_n._n_zeroes != 0
378 || sym->_n._n_n._n_offset == 0)
380 memcpy (buf, sym->_n._n_name, SYMNMLEN);
381 buf[SYMNMLEN] = '\0';
382 return buf;
384 else
386 const char *strings;
388 BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE);
389 strings = obj_coff_strings (abfd);
390 if (strings == NULL)
392 strings = _bfd_coff_read_string_table (abfd);
393 if (strings == NULL)
394 return NULL;
396 return strings + sym->_n._n_n._n_offset;
400 /* Read in and swap the relocs. This returns a buffer holding the
401 relocs for section SEC in file ABFD. If CACHE is true and
402 INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
403 the function is called again. If EXTERNAL_RELOCS is not NULL, it
404 is a buffer large enough to hold the unswapped relocs. If
405 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
406 the swapped relocs. If REQUIRE_INTERNAL is true, then the return
407 value must be INTERNAL_RELOCS. The function returns NULL on error. */
409 struct internal_reloc *
410 _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
411 require_internal, internal_relocs)
412 bfd *abfd;
413 asection *sec;
414 boolean cache;
415 bfd_byte *external_relocs;
416 boolean require_internal;
417 struct internal_reloc *internal_relocs;
419 bfd_size_type relsz;
420 bfd_byte *free_external = NULL;
421 struct internal_reloc *free_internal = NULL;
422 bfd_byte *erel;
423 bfd_byte *erel_end;
424 struct internal_reloc *irel;
426 if (coff_section_data (abfd, sec) != NULL
427 && coff_section_data (abfd, sec)->relocs != NULL)
429 if (! require_internal)
430 return coff_section_data (abfd, sec)->relocs;
431 memcpy (internal_relocs, coff_section_data (abfd, sec)->relocs,
432 sec->reloc_count * sizeof (struct internal_reloc));
433 return internal_relocs;
436 relsz = bfd_coff_relsz (abfd);
438 if (external_relocs == NULL)
440 free_external = (bfd_byte *) bfd_malloc (sec->reloc_count * relsz);
441 if (free_external == NULL && sec->reloc_count > 0)
442 goto error_return;
443 external_relocs = free_external;
446 if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
447 || (bfd_read (external_relocs, relsz, sec->reloc_count, abfd)
448 != relsz * sec->reloc_count))
449 goto error_return;
451 if (internal_relocs == NULL)
453 free_internal = ((struct internal_reloc *)
454 bfd_malloc (sec->reloc_count
455 * sizeof (struct internal_reloc)));
456 if (free_internal == NULL && sec->reloc_count > 0)
457 goto error_return;
458 internal_relocs = free_internal;
461 /* Swap in the relocs. */
462 erel = external_relocs;
463 erel_end = erel + relsz * sec->reloc_count;
464 irel = internal_relocs;
465 for (; erel < erel_end; erel += relsz, irel++)
466 bfd_coff_swap_reloc_in (abfd, (PTR) erel, (PTR) irel);
468 if (free_external != NULL)
470 free (free_external);
471 free_external = NULL;
474 if (cache && free_internal != NULL)
476 if (coff_section_data (abfd, sec) == NULL)
478 sec->used_by_bfd =
479 (PTR) bfd_zalloc (abfd,
480 sizeof (struct coff_section_tdata));
481 if (sec->used_by_bfd == NULL)
482 goto error_return;
483 coff_section_data (abfd, sec)->contents = NULL;
485 coff_section_data (abfd, sec)->relocs = free_internal;
488 return internal_relocs;
490 error_return:
491 if (free_external != NULL)
492 free (free_external);
493 if (free_internal != NULL)
494 free (free_internal);
495 return NULL;
498 /* Set lineno_count for the output sections of a COFF file. */
501 coff_count_linenumbers (abfd)
502 bfd *abfd;
504 unsigned int limit = bfd_get_symcount (abfd);
505 unsigned int i;
506 int total = 0;
507 asymbol **p;
508 asection *s;
510 if (limit == 0)
512 /* This may be from the backend linker, in which case the
513 lineno_count in the sections is correct. */
514 for (s = abfd->sections; s != NULL; s = s->next)
515 total += s->lineno_count;
516 return total;
519 for (s = abfd->sections; s != NULL; s = s->next)
520 BFD_ASSERT (s->lineno_count == 0);
522 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
524 asymbol *q_maybe = *p;
526 if (bfd_asymbol_flavour (q_maybe) == bfd_target_coff_flavour)
528 coff_symbol_type *q = coffsymbol (q_maybe);
530 /* The AIX 4.1 compiler can sometimes generate line numbers
531 attached to debugging symbols. We try to simply ignore
532 those here. */
533 if (q->lineno != NULL
534 && q->symbol.section->owner != NULL)
536 /* This symbol has line numbers. Increment the owning
537 section's linenumber count. */
538 alent *l = q->lineno;
540 ++q->symbol.section->output_section->lineno_count;
541 ++total;
542 ++l;
543 while (l->line_number != 0)
545 ++total;
546 ++q->symbol.section->output_section->lineno_count;
547 ++l;
553 return total;
556 /* Takes a bfd and a symbol, returns a pointer to the coff specific
557 area of the symbol if there is one. */
559 /*ARGSUSED*/
560 coff_symbol_type *
561 coff_symbol_from (ignore_abfd, symbol)
562 bfd *ignore_abfd ATTRIBUTE_UNUSED;
563 asymbol *symbol;
565 if (bfd_asymbol_flavour (symbol) != bfd_target_coff_flavour)
566 return (coff_symbol_type *) NULL;
568 if (bfd_asymbol_bfd (symbol)->tdata.coff_obj_data == (coff_data_type *) NULL)
569 return (coff_symbol_type *) NULL;
571 return (coff_symbol_type *) symbol;
574 static void
575 fixup_symbol_value (abfd, coff_symbol_ptr, syment)
576 bfd *abfd;
577 coff_symbol_type *coff_symbol_ptr;
578 struct internal_syment *syment;
581 /* Normalize the symbol flags */
582 if (bfd_is_com_section (coff_symbol_ptr->symbol.section))
584 /* a common symbol is undefined with a value */
585 syment->n_scnum = N_UNDEF;
586 syment->n_value = coff_symbol_ptr->symbol.value;
588 else if ((coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) != 0
589 && (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING_RELOC) == 0)
591 syment->n_value = coff_symbol_ptr->symbol.value;
593 else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
595 syment->n_scnum = N_UNDEF;
596 syment->n_value = 0;
598 /* FIXME: Do we need to handle the absolute section here? */
599 else
601 if (coff_symbol_ptr->symbol.section)
603 syment->n_scnum =
604 coff_symbol_ptr->symbol.section->output_section->target_index;
606 syment->n_value = (coff_symbol_ptr->symbol.value
607 + coff_symbol_ptr->symbol.section->output_offset);
608 if (! obj_pe (abfd))
609 syment->n_value +=
610 coff_symbol_ptr->symbol.section->output_section->vma;
612 else
614 BFD_ASSERT (0);
615 /* This can happen, but I don't know why yet (steve@cygnus.com) */
616 syment->n_scnum = N_ABS;
617 syment->n_value = coff_symbol_ptr->symbol.value;
622 /* Run through all the symbols in the symbol table and work out what
623 their indexes into the symbol table will be when output.
625 Coff requires that each C_FILE symbol points to the next one in the
626 chain, and that the last one points to the first external symbol. We
627 do that here too. */
629 boolean
630 coff_renumber_symbols (bfd_ptr, first_undef)
631 bfd *bfd_ptr;
632 int *first_undef;
634 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
635 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
636 unsigned int native_index = 0;
637 struct internal_syment *last_file = (struct internal_syment *) NULL;
638 unsigned int symbol_index;
640 /* COFF demands that undefined symbols come after all other symbols.
641 Since we don't need to impose this extra knowledge on all our
642 client programs, deal with that here. Sort the symbol table;
643 just move the undefined symbols to the end, leaving the rest
644 alone. The O'Reilly book says that defined global symbols come
645 at the end before the undefined symbols, so we do that here as
646 well. */
647 /* @@ Do we have some condition we could test for, so we don't always
648 have to do this? I don't think relocatability is quite right, but
649 I'm not certain. [raeburn:19920508.1711EST] */
651 asymbol **newsyms;
652 unsigned int i;
654 newsyms = (asymbol **) bfd_alloc (bfd_ptr,
655 sizeof (asymbol *) * (symbol_count + 1));
656 if (!newsyms)
657 return false;
658 bfd_ptr->outsymbols = newsyms;
659 for (i = 0; i < symbol_count; i++)
660 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
661 || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
662 && !bfd_is_com_section (symbol_ptr_ptr[i]->section)
663 && ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) != 0
664 || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
665 == 0))))
666 *newsyms++ = symbol_ptr_ptr[i];
668 for (i = 0; i < symbol_count; i++)
669 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
670 && !bfd_is_und_section (symbol_ptr_ptr[i]->section)
671 && (bfd_is_com_section (symbol_ptr_ptr[i]->section)
672 || ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) == 0
673 && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
674 != 0))))
675 *newsyms++ = symbol_ptr_ptr[i];
677 *first_undef = newsyms - bfd_ptr->outsymbols;
679 for (i = 0; i < symbol_count; i++)
680 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
681 && bfd_is_und_section (symbol_ptr_ptr[i]->section))
682 *newsyms++ = symbol_ptr_ptr[i];
683 *newsyms = (asymbol *) NULL;
684 symbol_ptr_ptr = bfd_ptr->outsymbols;
687 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
689 coff_symbol_type *coff_symbol_ptr = coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
690 symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
691 if (coff_symbol_ptr && coff_symbol_ptr->native)
693 combined_entry_type *s = coff_symbol_ptr->native;
694 int i;
696 if (s->u.syment.n_sclass == C_FILE)
698 if (last_file != (struct internal_syment *) NULL)
699 last_file->n_value = native_index;
700 last_file = &(s->u.syment);
702 else
705 /* Modify the symbol values according to their section and
706 type */
708 fixup_symbol_value (bfd_ptr, coff_symbol_ptr, &(s->u.syment));
710 for (i = 0; i < s->u.syment.n_numaux + 1; i++)
711 s[i].offset = native_index++;
713 else
715 native_index++;
718 obj_conv_table_size (bfd_ptr) = native_index;
720 return true;
723 /* Run thorough the symbol table again, and fix it so that all
724 pointers to entries are changed to the entries' index in the output
725 symbol table. */
727 void
728 coff_mangle_symbols (bfd_ptr)
729 bfd *bfd_ptr;
731 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
732 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
733 unsigned int symbol_index;
735 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
737 coff_symbol_type *coff_symbol_ptr =
738 coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
740 if (coff_symbol_ptr && coff_symbol_ptr->native)
742 int i;
743 combined_entry_type *s = coff_symbol_ptr->native;
745 if (s->fix_value)
747 /* FIXME: We should use a union here. */
748 s->u.syment.n_value =
749 ((combined_entry_type *) s->u.syment.n_value)->offset;
750 s->fix_value = 0;
752 if (s->fix_line)
754 /* The value is the offset into the line number entries
755 for the symbol's section. On output, the symbol's
756 section should be N_DEBUG. */
757 s->u.syment.n_value =
758 (coff_symbol_ptr->symbol.section->output_section->line_filepos
759 + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr));
760 coff_symbol_ptr->symbol.section =
761 coff_section_from_bfd_index (bfd_ptr, N_DEBUG);
762 BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
764 for (i = 0; i < s->u.syment.n_numaux; i++)
766 combined_entry_type *a = s + i + 1;
767 if (a->fix_tag)
769 a->u.auxent.x_sym.x_tagndx.l =
770 a->u.auxent.x_sym.x_tagndx.p->offset;
771 a->fix_tag = 0;
773 if (a->fix_end)
775 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
776 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
777 a->fix_end = 0;
779 if (a->fix_scnlen)
781 a->u.auxent.x_csect.x_scnlen.l =
782 a->u.auxent.x_csect.x_scnlen.p->offset;
783 a->fix_scnlen = 0;
790 static void
791 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
792 debug_string_section_p, debug_string_size_p)
793 bfd *abfd;
794 asymbol *symbol;
795 combined_entry_type *native;
796 bfd_size_type *string_size_p;
797 asection **debug_string_section_p;
798 bfd_size_type *debug_string_size_p;
800 unsigned int name_length;
801 union internal_auxent *auxent;
802 char *name = (char *) (symbol->name);
804 if (name == (char *) NULL)
806 /* coff symbols always have names, so we'll make one up */
807 symbol->name = "strange";
808 name = (char *) symbol->name;
810 name_length = strlen (name);
812 if (native->u.syment.n_sclass == C_FILE
813 && native->u.syment.n_numaux > 0)
815 unsigned int filnmlen;
817 strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
818 auxent = &(native + 1)->u.auxent;
820 filnmlen = bfd_coff_filnmlen (abfd);
822 if (bfd_coff_long_filenames (abfd))
824 if (name_length <= filnmlen)
826 strncpy (auxent->x_file.x_fname, name, filnmlen);
828 else
830 auxent->x_file.x_n.x_offset = *string_size_p + STRING_SIZE_SIZE;
831 auxent->x_file.x_n.x_zeroes = 0;
832 *string_size_p += name_length + 1;
835 else
837 strncpy (auxent->x_file.x_fname, name, filnmlen);
838 if (name_length > filnmlen)
839 name[filnmlen] = '\0';
842 else
844 if (name_length <= SYMNMLEN)
846 /* This name will fit into the symbol neatly */
847 strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
849 else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
851 native->u.syment._n._n_n._n_offset = (*string_size_p
852 + STRING_SIZE_SIZE);
853 native->u.syment._n._n_n._n_zeroes = 0;
854 *string_size_p += name_length + 1;
856 else
858 long filepos;
859 bfd_byte buf[2];
861 /* This name should be written into the .debug section. For
862 some reason each name is preceded by a two byte length
863 and also followed by a null byte. FIXME: We assume that
864 the .debug section has already been created, and that it
865 is large enough. */
866 if (*debug_string_section_p == (asection *) NULL)
867 *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
868 filepos = bfd_tell (abfd);
869 bfd_put_16 (abfd, name_length + 1, buf);
870 if (!bfd_set_section_contents (abfd,
871 *debug_string_section_p,
872 (PTR) buf,
873 (file_ptr) *debug_string_size_p,
874 (bfd_size_type) 2)
875 || !bfd_set_section_contents (abfd,
876 *debug_string_section_p,
877 (PTR) symbol->name,
878 ((file_ptr) *debug_string_size_p
879 + 2),
880 (bfd_size_type) name_length + 1))
881 abort ();
882 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
883 abort ();
884 native->u.syment._n._n_n._n_offset = *debug_string_size_p + 2;
885 native->u.syment._n._n_n._n_zeroes = 0;
886 *debug_string_size_p += name_length + 3;
891 /* We need to keep track of the symbol index so that when we write out
892 the relocs we can get the index for a symbol. This method is a
893 hack. FIXME. */
895 #define set_index(symbol, idx) ((symbol)->udata.i = (idx))
897 /* Write a symbol out to a COFF file. */
899 static boolean
900 coff_write_symbol (abfd, symbol, native, written, string_size_p,
901 debug_string_section_p, debug_string_size_p)
902 bfd *abfd;
903 asymbol *symbol;
904 combined_entry_type *native;
905 unsigned int *written;
906 bfd_size_type *string_size_p;
907 asection **debug_string_section_p;
908 bfd_size_type *debug_string_size_p;
910 unsigned int numaux = native->u.syment.n_numaux;
911 int type = native->u.syment.n_type;
912 int class = native->u.syment.n_sclass;
913 PTR buf;
914 bfd_size_type symesz;
916 if (native->u.syment.n_sclass == C_FILE)
917 symbol->flags |= BSF_DEBUGGING;
919 if (symbol->flags & BSF_DEBUGGING
920 && bfd_is_abs_section (symbol->section))
922 native->u.syment.n_scnum = N_DEBUG;
924 else if (bfd_is_abs_section (symbol->section))
926 native->u.syment.n_scnum = N_ABS;
928 else if (bfd_is_und_section (symbol->section))
930 native->u.syment.n_scnum = N_UNDEF;
932 else
934 native->u.syment.n_scnum =
935 symbol->section->output_section->target_index;
938 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
939 debug_string_section_p, debug_string_size_p);
941 symesz = bfd_coff_symesz (abfd);
942 buf = bfd_alloc (abfd, symesz);
943 if (!buf)
944 return false;
945 bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
946 if (bfd_write (buf, 1, symesz, abfd) != symesz)
947 return false;
948 bfd_release (abfd, buf);
950 if (native->u.syment.n_numaux > 0)
952 bfd_size_type auxesz;
953 unsigned int j;
955 auxesz = bfd_coff_auxesz (abfd);
956 buf = bfd_alloc (abfd, auxesz);
957 if (!buf)
958 return false;
959 for (j = 0; j < native->u.syment.n_numaux; j++)
961 bfd_coff_swap_aux_out (abfd,
962 &((native + j + 1)->u.auxent),
963 type,
964 class,
966 native->u.syment.n_numaux,
967 buf);
968 if (bfd_write (buf, 1, auxesz, abfd) != auxesz)
969 return false;
971 bfd_release (abfd, buf);
974 /* Store the index for use when we write out the relocs. */
975 set_index (symbol, *written);
977 *written += numaux + 1;
978 return true;
981 /* Write out a symbol to a COFF file that does not come from a COFF
982 file originally. This symbol may have been created by the linker,
983 or we may be linking a non COFF file to a COFF file. */
985 static boolean
986 coff_write_alien_symbol (abfd, symbol, written, string_size_p,
987 debug_string_section_p, debug_string_size_p)
988 bfd *abfd;
989 asymbol *symbol;
990 unsigned int *written;
991 bfd_size_type *string_size_p;
992 asection **debug_string_section_p;
993 bfd_size_type *debug_string_size_p;
995 combined_entry_type *native;
996 combined_entry_type dummy;
998 native = &dummy;
999 native->u.syment.n_type = T_NULL;
1000 native->u.syment.n_flags = 0;
1001 if (bfd_is_und_section (symbol->section))
1003 native->u.syment.n_scnum = N_UNDEF;
1004 native->u.syment.n_value = symbol->value;
1006 else if (bfd_is_com_section (symbol->section))
1008 native->u.syment.n_scnum = N_UNDEF;
1009 native->u.syment.n_value = symbol->value;
1011 else if (symbol->flags & BSF_DEBUGGING)
1013 /* There isn't much point to writing out a debugging symbol
1014 unless we are prepared to convert it into COFF debugging
1015 format. So, we just ignore them. We must clobber the symbol
1016 name to keep it from being put in the string table. */
1017 symbol->name = "";
1018 return true;
1020 else
1022 native->u.syment.n_scnum =
1023 symbol->section->output_section->target_index;
1024 native->u.syment.n_value = (symbol->value
1025 + symbol->section->output_offset);
1026 if (! obj_pe (abfd))
1027 native->u.syment.n_value += symbol->section->output_section->vma;
1029 /* Copy the any flags from the the file header into the symbol.
1030 FIXME: Why? */
1032 coff_symbol_type *c = coff_symbol_from (abfd, symbol);
1033 if (c != (coff_symbol_type *) NULL)
1034 native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
1038 native->u.syment.n_type = 0;
1039 if (symbol->flags & BSF_LOCAL)
1040 native->u.syment.n_sclass = C_STAT;
1041 else if (symbol->flags & BSF_WEAK)
1042 native->u.syment.n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1043 else
1044 native->u.syment.n_sclass = C_EXT;
1045 native->u.syment.n_numaux = 0;
1047 return coff_write_symbol (abfd, symbol, native, written, string_size_p,
1048 debug_string_section_p, debug_string_size_p);
1051 /* Write a native symbol to a COFF file. */
1053 static boolean
1054 coff_write_native_symbol (abfd, symbol, written, string_size_p,
1055 debug_string_section_p, debug_string_size_p)
1056 bfd *abfd;
1057 coff_symbol_type *symbol;
1058 unsigned int *written;
1059 bfd_size_type *string_size_p;
1060 asection **debug_string_section_p;
1061 bfd_size_type *debug_string_size_p;
1063 combined_entry_type *native = symbol->native;
1064 alent *lineno = symbol->lineno;
1066 /* If this symbol has an associated line number, we must store the
1067 symbol index in the line number field. We also tag the auxent to
1068 point to the right place in the lineno table. */
1069 if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
1071 unsigned int count = 0;
1072 lineno[count].u.offset = *written;
1073 if (native->u.syment.n_numaux)
1075 union internal_auxent *a = &((native + 1)->u.auxent);
1077 a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
1078 symbol->symbol.section->output_section->moving_line_filepos;
1081 /* Count and relocate all other linenumbers. */
1082 count++;
1083 while (lineno[count].line_number != 0)
1085 #if 0
1086 /* 13 april 92. sac
1087 I've been told this, but still need proof:
1088 > The second bug is also in `bfd/coffcode.h'. This bug
1089 > causes the linker to screw up the pc-relocations for
1090 > all the line numbers in COFF code. This bug isn't only
1091 > specific to A29K implementations, but affects all
1092 > systems using COFF format binaries. Note that in COFF
1093 > object files, the line number core offsets output by
1094 > the assembler are relative to the start of each
1095 > procedure, not to the start of the .text section. This
1096 > patch relocates the line numbers relative to the
1097 > `native->u.syment.n_value' instead of the section
1098 > virtual address.
1099 > modular!olson@cs.arizona.edu (Jon Olson)
1101 lineno[count].u.offset += native->u.syment.n_value;
1102 #else
1103 lineno[count].u.offset +=
1104 (symbol->symbol.section->output_section->vma
1105 + symbol->symbol.section->output_offset);
1106 #endif
1107 count++;
1109 symbol->done_lineno = true;
1111 symbol->symbol.section->output_section->moving_line_filepos +=
1112 count * bfd_coff_linesz (abfd);
1115 return coff_write_symbol (abfd, &(symbol->symbol), native, written,
1116 string_size_p, debug_string_section_p,
1117 debug_string_size_p);
1120 /* Write out the COFF symbols. */
1122 boolean
1123 coff_write_symbols (abfd)
1124 bfd *abfd;
1126 bfd_size_type string_size;
1127 asection *debug_string_section;
1128 bfd_size_type debug_string_size;
1129 unsigned int i;
1130 unsigned int limit = bfd_get_symcount (abfd);
1131 unsigned int written = 0;
1132 asymbol **p;
1134 string_size = 0;
1135 debug_string_section = NULL;
1136 debug_string_size = 0;
1138 /* If this target supports long section names, they must be put into
1139 the string table. This is supported by PE. This code must
1140 handle section names just as they are handled in
1141 coff_write_object_contents. */
1142 if (bfd_coff_long_section_names (abfd))
1144 asection *o;
1146 for (o = abfd->sections; o != NULL; o = o->next)
1148 size_t len;
1150 len = strlen (o->name);
1151 if (len > SCNNMLEN)
1152 string_size += len + 1;
1156 /* Seek to the right place */
1157 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1158 return false;
1160 /* Output all the symbols we have */
1162 written = 0;
1163 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
1165 asymbol *symbol = *p;
1166 coff_symbol_type *c_symbol = coff_symbol_from (abfd, symbol);
1168 if (c_symbol == (coff_symbol_type *) NULL
1169 || c_symbol->native == (combined_entry_type *) NULL)
1171 if (!coff_write_alien_symbol (abfd, symbol, &written, &string_size,
1172 &debug_string_section,
1173 &debug_string_size))
1174 return false;
1176 else
1178 if (!coff_write_native_symbol (abfd, c_symbol, &written,
1179 &string_size, &debug_string_section,
1180 &debug_string_size))
1181 return false;
1185 obj_raw_syment_count (abfd) = written;
1187 /* Now write out strings */
1189 if (string_size != 0)
1191 unsigned int size = string_size + STRING_SIZE_SIZE;
1192 bfd_byte buffer[STRING_SIZE_SIZE];
1194 #if STRING_SIZE_SIZE == 4
1195 bfd_h_put_32 (abfd, size, buffer);
1196 #else
1197 #error Change bfd_h_put_32
1198 #endif
1199 if (bfd_write ((PTR) buffer, 1, sizeof (buffer), abfd) != sizeof (buffer))
1200 return false;
1202 /* Handle long section names. This code must handle section
1203 names just as they are handled in coff_write_object_contents. */
1204 if (bfd_coff_long_section_names (abfd))
1206 asection *o;
1208 for (o = abfd->sections; o != NULL; o = o->next)
1210 size_t len;
1212 len = strlen (o->name);
1213 if (len > SCNNMLEN)
1215 if (bfd_write (o->name, 1, len + 1, abfd) != len + 1)
1216 return false;
1221 for (p = abfd->outsymbols, i = 0;
1222 i < limit;
1223 i++, p++)
1225 asymbol *q = *p;
1226 size_t name_length = strlen (q->name);
1227 coff_symbol_type *c_symbol = coff_symbol_from (abfd, q);
1228 size_t maxlen;
1230 /* Figure out whether the symbol name should go in the string
1231 table. Symbol names that are short enough are stored
1232 directly in the syment structure. File names permit a
1233 different, longer, length in the syment structure. On
1234 XCOFF, some symbol names are stored in the .debug section
1235 rather than in the string table. */
1237 if (c_symbol == NULL
1238 || c_symbol->native == NULL)
1240 /* This is not a COFF symbol, so it certainly is not a
1241 file name, nor does it go in the .debug section. */
1242 maxlen = SYMNMLEN;
1244 else if (bfd_coff_symname_in_debug (abfd,
1245 &c_symbol->native->u.syment))
1247 /* This symbol name is in the XCOFF .debug section.
1248 Don't write it into the string table. */
1249 maxlen = name_length;
1251 else if (c_symbol->native->u.syment.n_sclass == C_FILE
1252 && c_symbol->native->u.syment.n_numaux > 0)
1253 maxlen = bfd_coff_filnmlen (abfd);
1254 else
1255 maxlen = SYMNMLEN;
1257 if (name_length > maxlen)
1259 if (bfd_write ((PTR) (q->name), 1, name_length + 1, abfd)
1260 != name_length + 1)
1261 return false;
1265 else
1267 /* We would normally not write anything here, but we'll write
1268 out 4 so that any stupid coff reader which tries to read the
1269 string table even when there isn't one won't croak. */
1270 unsigned int size = STRING_SIZE_SIZE;
1271 bfd_byte buffer[STRING_SIZE_SIZE];
1273 #if STRING_SIZE_SIZE == 4
1274 bfd_h_put_32 (abfd, size, buffer);
1275 #else
1276 #error Change bfd_h_put_32
1277 #endif
1278 if (bfd_write ((PTR) buffer, 1, STRING_SIZE_SIZE, abfd)
1279 != STRING_SIZE_SIZE)
1280 return false;
1283 /* Make sure the .debug section was created to be the correct size.
1284 We should create it ourselves on the fly, but we don't because
1285 BFD won't let us write to any section until we know how large all
1286 the sections are. We could still do it by making another pass
1287 over the symbols. FIXME. */
1288 BFD_ASSERT (debug_string_size == 0
1289 || (debug_string_section != (asection *) NULL
1290 && (BFD_ALIGN (debug_string_size,
1291 1 << debug_string_section->alignment_power)
1292 == bfd_section_size (abfd, debug_string_section))));
1294 return true;
1297 boolean
1298 coff_write_linenumbers (abfd)
1299 bfd *abfd;
1301 asection *s;
1302 bfd_size_type linesz;
1303 PTR buff;
1305 linesz = bfd_coff_linesz (abfd);
1306 buff = bfd_alloc (abfd, linesz);
1307 if (!buff)
1308 return false;
1309 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1311 if (s->lineno_count)
1313 asymbol **q = abfd->outsymbols;
1314 if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
1315 return false;
1316 /* Find all the linenumbers in this section */
1317 while (*q)
1319 asymbol *p = *q;
1320 if (p->section->output_section == s)
1322 alent *l =
1323 BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
1324 (bfd_asymbol_bfd (p), p));
1325 if (l)
1327 /* Found a linenumber entry, output */
1328 struct internal_lineno out;
1329 memset ((PTR) & out, 0, sizeof (out));
1330 out.l_lnno = 0;
1331 out.l_addr.l_symndx = l->u.offset;
1332 bfd_coff_swap_lineno_out (abfd, &out, buff);
1333 if (bfd_write (buff, 1, linesz, abfd) != linesz)
1334 return false;
1335 l++;
1336 while (l->line_number)
1338 out.l_lnno = l->line_number;
1339 out.l_addr.l_symndx = l->u.offset;
1340 bfd_coff_swap_lineno_out (abfd, &out, buff);
1341 if (bfd_write (buff, 1, linesz, abfd) != linesz)
1342 return false;
1343 l++;
1347 q++;
1351 bfd_release (abfd, buff);
1352 return true;
1355 /*ARGSUSED */
1356 alent *
1357 coff_get_lineno (ignore_abfd, symbol)
1358 bfd *ignore_abfd ATTRIBUTE_UNUSED;
1359 asymbol *symbol;
1361 return coffsymbol (symbol)->lineno;
1364 #if 0
1366 /* This is only called from coff_add_missing_symbols, which has been
1367 disabled. */
1369 asymbol *
1370 coff_section_symbol (abfd, name)
1371 bfd *abfd;
1372 char *name;
1374 asection *sec = bfd_make_section_old_way (abfd, name);
1375 asymbol *sym;
1376 combined_entry_type *csym;
1378 sym = sec->symbol;
1379 csym = coff_symbol_from (abfd, sym)->native;
1380 /* Make sure back-end COFF stuff is there. */
1381 if (csym == 0)
1383 struct foo
1385 coff_symbol_type sym;
1386 /* @@FIXME This shouldn't use a fixed size!! */
1387 combined_entry_type e[10];
1389 struct foo *f;
1390 f = (struct foo *) bfd_alloc (abfd, sizeof (*f));
1391 if (!f)
1393 bfd_set_error (bfd_error_no_error);
1394 return NULL;
1396 memset ((char *) f, 0, sizeof (*f));
1397 coff_symbol_from (abfd, sym)->native = csym = f->e;
1399 csym[0].u.syment.n_sclass = C_STAT;
1400 csym[0].u.syment.n_numaux = 1;
1401 /* SF_SET_STATICS (sym); @@ ??? */
1402 csym[1].u.auxent.x_scn.x_scnlen = sec->_raw_size;
1403 csym[1].u.auxent.x_scn.x_nreloc = sec->reloc_count;
1404 csym[1].u.auxent.x_scn.x_nlinno = sec->lineno_count;
1406 if (sec->output_section == NULL)
1408 sec->output_section = sec;
1409 sec->output_offset = 0;
1412 return sym;
1415 #endif /* 0 */
1417 /* This function transforms the offsets into the symbol table into
1418 pointers to syments. */
1420 static void
1421 coff_pointerize_aux (abfd, table_base, symbol, indaux, auxent)
1422 bfd *abfd;
1423 combined_entry_type *table_base;
1424 combined_entry_type *symbol;
1425 unsigned int indaux;
1426 combined_entry_type *auxent;
1428 unsigned int type = symbol->u.syment.n_type;
1429 unsigned int class = symbol->u.syment.n_sclass;
1431 if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1433 if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1434 (abfd, table_base, symbol, indaux, auxent))
1435 return;
1438 /* Don't bother if this is a file or a section */
1439 if (class == C_STAT && type == T_NULL)
1440 return;
1441 if (class == C_FILE)
1442 return;
1444 /* Otherwise patch up */
1445 #define N_TMASK coff_data (abfd)->local_n_tmask
1446 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1447 if ((ISFCN (type) || ISTAG (class) || class == C_BLOCK || class == C_FCN)
1448 && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l > 0)
1450 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
1451 table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
1452 auxent->fix_end = 1;
1454 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1455 generate one, so we must be careful to ignore it. */
1456 if (auxent->u.auxent.x_sym.x_tagndx.l > 0)
1458 auxent->u.auxent.x_sym.x_tagndx.p =
1459 table_base + auxent->u.auxent.x_sym.x_tagndx.l;
1460 auxent->fix_tag = 1;
1464 /* Allocate space for the ".debug" section, and read it.
1465 We did not read the debug section until now, because
1466 we didn't want to go to the trouble until someone needed it. */
1468 static char *
1469 build_debug_section (abfd)
1470 bfd *abfd;
1472 char *debug_section;
1473 long position;
1475 asection *sect = bfd_get_section_by_name (abfd, ".debug");
1477 if (!sect)
1479 bfd_set_error (bfd_error_no_debug_section);
1480 return NULL;
1483 debug_section = (PTR) bfd_alloc (abfd,
1484 bfd_get_section_size_before_reloc (sect));
1485 if (debug_section == NULL)
1486 return NULL;
1488 /* Seek to the beginning of the `.debug' section and read it.
1489 Save the current position first; it is needed by our caller.
1490 Then read debug section and reset the file pointer. */
1492 position = bfd_tell (abfd);
1493 if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0
1494 || (bfd_read (debug_section,
1495 bfd_get_section_size_before_reloc (sect), 1, abfd)
1496 != bfd_get_section_size_before_reloc (sect))
1497 || bfd_seek (abfd, position, SEEK_SET) != 0)
1498 return NULL;
1499 return debug_section;
1503 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1504 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1505 be \0-terminated. */
1506 static char *
1507 copy_name (abfd, name, maxlen)
1508 bfd *abfd;
1509 char *name;
1510 int maxlen;
1512 int len;
1513 char *newname;
1515 for (len = 0; len < maxlen; ++len)
1517 if (name[len] == '\0')
1519 break;
1523 if ((newname = (PTR) bfd_alloc (abfd, len + 1)) == NULL)
1524 return (NULL);
1525 strncpy (newname, name, len);
1526 newname[len] = '\0';
1527 return newname;
1530 /* Read in the external symbols. */
1532 boolean
1533 _bfd_coff_get_external_symbols (abfd)
1534 bfd *abfd;
1536 bfd_size_type symesz;
1537 size_t size;
1538 PTR syms;
1540 if (obj_coff_external_syms (abfd) != NULL)
1541 return true;
1543 symesz = bfd_coff_symesz (abfd);
1545 size = obj_raw_syment_count (abfd) * symesz;
1547 syms = (PTR) bfd_malloc (size);
1548 if (syms == NULL && size != 0)
1549 return false;
1551 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
1552 || bfd_read (syms, size, 1, abfd) != size)
1554 if (syms != NULL)
1555 free (syms);
1556 return false;
1559 obj_coff_external_syms (abfd) = syms;
1561 return true;
1564 /* Read in the external strings. The strings are not loaded until
1565 they are needed. This is because we have no simple way of
1566 detecting a missing string table in an archive. */
1568 const char *
1569 _bfd_coff_read_string_table (abfd)
1570 bfd *abfd;
1572 char extstrsize[STRING_SIZE_SIZE];
1573 size_t strsize;
1574 char *strings;
1576 if (obj_coff_strings (abfd) != NULL)
1577 return obj_coff_strings (abfd);
1579 if (obj_sym_filepos (abfd) == 0)
1581 bfd_set_error (bfd_error_no_symbols);
1582 return NULL;
1585 if (bfd_seek (abfd,
1586 (obj_sym_filepos (abfd)
1587 + obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd)),
1588 SEEK_SET) != 0)
1589 return NULL;
1591 if (bfd_read (extstrsize, sizeof extstrsize, 1, abfd) != sizeof extstrsize)
1593 if (bfd_get_error () != bfd_error_file_truncated)
1594 return NULL;
1596 /* There is no string table. */
1597 strsize = STRING_SIZE_SIZE;
1599 else
1601 #if STRING_SIZE_SIZE == 4
1602 strsize = bfd_h_get_32 (abfd, (bfd_byte *) extstrsize);
1603 #else
1604 #error Change bfd_h_get_32
1605 #endif
1608 if (strsize < STRING_SIZE_SIZE)
1610 (*_bfd_error_handler)
1611 (_("%s: bad string table size %lu"), bfd_get_filename (abfd),
1612 (unsigned long) strsize);
1613 bfd_set_error (bfd_error_bad_value);
1614 return NULL;
1617 strings = (char *) bfd_malloc (strsize);
1618 if (strings == NULL)
1619 return NULL;
1621 if (bfd_read (strings + STRING_SIZE_SIZE,
1622 strsize - STRING_SIZE_SIZE, 1, abfd)
1623 != strsize - STRING_SIZE_SIZE)
1625 free (strings);
1626 return NULL;
1629 obj_coff_strings (abfd) = strings;
1631 return strings;
1634 /* Free up the external symbols and strings read from a COFF file. */
1636 boolean
1637 _bfd_coff_free_symbols (abfd)
1638 bfd *abfd;
1640 if (obj_coff_external_syms (abfd) != NULL
1641 && ! obj_coff_keep_syms (abfd))
1643 free (obj_coff_external_syms (abfd));
1644 obj_coff_external_syms (abfd) = NULL;
1646 if (obj_coff_strings (abfd) != NULL
1647 && ! obj_coff_keep_strings (abfd))
1649 free (obj_coff_strings (abfd));
1650 obj_coff_strings (abfd) = NULL;
1652 return true;
1655 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1656 knit the symbol names into a normalized form. By normalized here I
1657 mean that all symbols have an n_offset pointer that points to a null-
1658 terminated string. */
1660 combined_entry_type *
1661 coff_get_normalized_symtab (abfd)
1662 bfd *abfd;
1664 combined_entry_type *internal;
1665 combined_entry_type *internal_ptr;
1666 combined_entry_type *symbol_ptr;
1667 combined_entry_type *internal_end;
1668 bfd_size_type symesz;
1669 char *raw_src;
1670 char *raw_end;
1671 const char *string_table = NULL;
1672 char *debug_section = NULL;
1673 unsigned long size;
1675 if (obj_raw_syments (abfd) != NULL)
1676 return obj_raw_syments (abfd);
1678 size = obj_raw_syment_count (abfd) * sizeof (combined_entry_type);
1679 internal = (combined_entry_type *) bfd_zalloc (abfd, size);
1680 if (internal == NULL && size != 0)
1681 return NULL;
1682 internal_end = internal + obj_raw_syment_count (abfd);
1684 if (! _bfd_coff_get_external_symbols (abfd))
1685 return NULL;
1687 raw_src = (char *) obj_coff_external_syms (abfd);
1689 /* mark the end of the symbols */
1690 symesz = bfd_coff_symesz (abfd);
1691 raw_end = (char *) raw_src + obj_raw_syment_count (abfd) * symesz;
1693 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1694 probably possible. If one shows up, it will probably kill us. */
1696 /* Swap all the raw entries */
1697 for (internal_ptr = internal;
1698 raw_src < raw_end;
1699 raw_src += symesz, internal_ptr++)
1702 unsigned int i;
1703 bfd_coff_swap_sym_in (abfd, (PTR) raw_src,
1704 (PTR) & internal_ptr->u.syment);
1705 symbol_ptr = internal_ptr;
1707 for (i = 0;
1708 i < symbol_ptr->u.syment.n_numaux;
1709 i++)
1711 internal_ptr++;
1712 raw_src += symesz;
1713 bfd_coff_swap_aux_in (abfd, (PTR) raw_src,
1714 symbol_ptr->u.syment.n_type,
1715 symbol_ptr->u.syment.n_sclass,
1716 i, symbol_ptr->u.syment.n_numaux,
1717 &(internal_ptr->u.auxent));
1718 coff_pointerize_aux (abfd, internal, symbol_ptr, i,
1719 internal_ptr);
1723 /* Free the raw symbols, but not the strings (if we have them). */
1724 obj_coff_keep_strings (abfd) = true;
1725 if (! _bfd_coff_free_symbols (abfd))
1726 return NULL;
1728 for (internal_ptr = internal; internal_ptr < internal_end;
1729 internal_ptr++)
1731 if (internal_ptr->u.syment.n_sclass == C_FILE
1732 && internal_ptr->u.syment.n_numaux > 0)
1734 /* make a file symbol point to the name in the auxent, since
1735 the text ".file" is redundant */
1736 if ((internal_ptr + 1)->u.auxent.x_file.x_n.x_zeroes == 0)
1738 /* the filename is a long one, point into the string table */
1739 if (string_table == NULL)
1741 string_table = _bfd_coff_read_string_table (abfd);
1742 if (string_table == NULL)
1743 return NULL;
1746 internal_ptr->u.syment._n._n_n._n_offset =
1747 ((long)
1748 (string_table
1749 + (internal_ptr + 1)->u.auxent.x_file.x_n.x_offset));
1751 else
1753 /* Ordinary short filename, put into memory anyway. The
1754 Microsoft PE tools sometimes store a filename in
1755 multiple AUX entries. */
1756 if (internal_ptr->u.syment.n_numaux > 1
1757 && coff_data (abfd)->pe)
1759 internal_ptr->u.syment._n._n_n._n_offset =
1760 ((long)
1761 copy_name (abfd,
1762 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1763 internal_ptr->u.syment.n_numaux * symesz));
1765 else
1767 internal_ptr->u.syment._n._n_n._n_offset =
1768 ((long)
1769 copy_name (abfd,
1770 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1771 bfd_coff_filnmlen (abfd)));
1775 else
1777 if (internal_ptr->u.syment._n._n_n._n_zeroes != 0)
1779 /* This is a "short" name. Make it long. */
1780 unsigned long i = 0;
1781 char *newstring = NULL;
1783 /* find the length of this string without walking into memory
1784 that isn't ours. */
1785 for (i = 0; i < 8; ++i)
1787 if (internal_ptr->u.syment._n._n_name[i] == '\0')
1789 break;
1790 } /* if end of string */
1791 } /* possible lengths of this string. */
1793 if ((newstring = (PTR) bfd_alloc (abfd, ++i)) == NULL)
1794 return (NULL);
1795 memset (newstring, 0, i);
1796 strncpy (newstring, internal_ptr->u.syment._n._n_name, i - 1);
1797 internal_ptr->u.syment._n._n_n._n_offset = (long int) newstring;
1798 internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1800 else if (internal_ptr->u.syment._n._n_n._n_offset == 0)
1801 internal_ptr->u.syment._n._n_n._n_offset = (long int) "";
1802 else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment))
1804 /* Long name already. Point symbol at the string in the
1805 table. */
1806 if (string_table == NULL)
1808 string_table = _bfd_coff_read_string_table (abfd);
1809 if (string_table == NULL)
1810 return NULL;
1812 internal_ptr->u.syment._n._n_n._n_offset =
1813 ((long int)
1814 (string_table
1815 + internal_ptr->u.syment._n._n_n._n_offset));
1817 else
1819 /* Long name in debug section. Very similar. */
1820 if (debug_section == NULL)
1821 debug_section = build_debug_section (abfd);
1822 internal_ptr->u.syment._n._n_n._n_offset = (long int)
1823 (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
1826 internal_ptr += internal_ptr->u.syment.n_numaux;
1829 obj_raw_syments (abfd) = internal;
1830 BFD_ASSERT (obj_raw_syment_count (abfd)
1831 == (unsigned int) (internal_ptr - internal));
1833 return (internal);
1834 } /* coff_get_normalized_symtab() */
1836 long
1837 coff_get_reloc_upper_bound (abfd, asect)
1838 bfd *abfd;
1839 sec_ptr asect;
1841 if (bfd_get_format (abfd) != bfd_object)
1843 bfd_set_error (bfd_error_invalid_operation);
1844 return -1;
1846 return (asect->reloc_count + 1) * sizeof (arelent *);
1849 asymbol *
1850 coff_make_empty_symbol (abfd)
1851 bfd *abfd;
1853 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, sizeof (coff_symbol_type));
1854 if (new == NULL)
1855 return (NULL);
1856 memset (new, 0, sizeof *new);
1857 new->symbol.section = 0;
1858 new->native = 0;
1859 new->lineno = (alent *) NULL;
1860 new->done_lineno = false;
1861 new->symbol.the_bfd = abfd;
1862 return &new->symbol;
1865 /* Make a debugging symbol. */
1867 asymbol *
1868 coff_bfd_make_debug_symbol (abfd, ptr, sz)
1869 bfd *abfd;
1870 PTR ptr ATTRIBUTE_UNUSED;
1871 unsigned long sz ATTRIBUTE_UNUSED;
1873 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, sizeof (coff_symbol_type));
1874 if (new == NULL)
1875 return (NULL);
1876 /* @@ The 10 is a guess at a plausible maximum number of aux entries
1877 (but shouldn't be a constant). */
1878 new->native = (combined_entry_type *) bfd_zalloc (abfd, sizeof (combined_entry_type) * 10);
1879 if (!new->native)
1880 return (NULL);
1881 new->symbol.section = bfd_abs_section_ptr;
1882 new->symbol.flags = BSF_DEBUGGING;
1883 new->lineno = (alent *) NULL;
1884 new->done_lineno = false;
1885 new->symbol.the_bfd = abfd;
1886 return &new->symbol;
1889 /*ARGSUSED */
1890 void
1891 coff_get_symbol_info (abfd, symbol, ret)
1892 bfd *abfd;
1893 asymbol *symbol;
1894 symbol_info *ret;
1896 bfd_symbol_info (symbol, ret);
1897 if (coffsymbol (symbol)->native != NULL
1898 && coffsymbol (symbol)->native->fix_value)
1900 combined_entry_type *psym;
1902 psym = ((combined_entry_type *)
1903 coffsymbol (symbol)->native->u.syment.n_value);
1904 ret->value = (bfd_vma) (psym - obj_raw_syments (abfd));
1908 /* Return the COFF syment for a symbol. */
1910 boolean
1911 bfd_coff_get_syment (abfd, symbol, psyment)
1912 bfd *abfd;
1913 asymbol *symbol;
1914 struct internal_syment *psyment;
1916 coff_symbol_type *csym;
1918 csym = coff_symbol_from (abfd, symbol);
1919 if (csym == NULL || csym->native == NULL)
1921 bfd_set_error (bfd_error_invalid_operation);
1922 return false;
1925 *psyment = csym->native->u.syment;
1927 if (csym->native->fix_value)
1928 psyment->n_value = ((combined_entry_type *) psyment->n_value
1929 - obj_raw_syments (abfd));
1931 /* FIXME: We should handle fix_line here. */
1933 return true;
1936 /* Return the COFF auxent for a symbol. */
1938 boolean
1939 bfd_coff_get_auxent (abfd, symbol, indx, pauxent)
1940 bfd *abfd;
1941 asymbol *symbol;
1942 int indx;
1943 union internal_auxent *pauxent;
1945 coff_symbol_type *csym;
1946 combined_entry_type *ent;
1948 csym = coff_symbol_from (abfd, symbol);
1950 if (csym == NULL
1951 || csym->native == NULL
1952 || indx >= csym->native->u.syment.n_numaux)
1954 bfd_set_error (bfd_error_invalid_operation);
1955 return false;
1958 ent = csym->native + indx + 1;
1960 *pauxent = ent->u.auxent;
1962 if (ent->fix_tag)
1963 pauxent->x_sym.x_tagndx.l =
1964 ((combined_entry_type *) pauxent->x_sym.x_tagndx.p
1965 - obj_raw_syments (abfd));
1967 if (ent->fix_end)
1968 pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l =
1969 ((combined_entry_type *) pauxent->x_sym.x_fcnary.x_fcn.x_endndx.p
1970 - obj_raw_syments (abfd));
1972 if (ent->fix_scnlen)
1973 pauxent->x_csect.x_scnlen.l =
1974 ((combined_entry_type *) pauxent->x_csect.x_scnlen.p
1975 - obj_raw_syments (abfd));
1977 return true;
1980 /* Print out information about COFF symbol. */
1982 void
1983 coff_print_symbol (abfd, filep, symbol, how)
1984 bfd *abfd;
1985 PTR filep;
1986 asymbol *symbol;
1987 bfd_print_symbol_type how;
1989 FILE *file = (FILE *) filep;
1991 switch (how)
1993 case bfd_print_symbol_name:
1994 fprintf (file, "%s", symbol->name);
1995 break;
1997 case bfd_print_symbol_more:
1998 fprintf (file, "coff %s %s",
1999 coffsymbol (symbol)->native ? "n" : "g",
2000 coffsymbol (symbol)->lineno ? "l" : " ");
2001 break;
2003 case bfd_print_symbol_all:
2004 if (coffsymbol (symbol)->native)
2006 unsigned long val;
2007 unsigned int aux;
2008 combined_entry_type *combined = coffsymbol (symbol)->native;
2009 combined_entry_type *root = obj_raw_syments (abfd);
2010 struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
2012 fprintf (file, "[%3ld]", (long) (combined - root));
2014 if (! combined->fix_value)
2015 val = (unsigned long) combined->u.syment.n_value;
2016 else
2017 val = ((unsigned long)
2018 ((combined_entry_type *) combined->u.syment.n_value
2019 - root));
2021 fprintf (file,
2022 "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x%08lx %s",
2023 combined->u.syment.n_scnum,
2024 combined->u.syment.n_flags,
2025 combined->u.syment.n_type,
2026 combined->u.syment.n_sclass,
2027 combined->u.syment.n_numaux,
2028 val,
2029 symbol->name);
2031 for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
2033 combined_entry_type *auxp = combined + aux + 1;
2034 long tagndx;
2036 if (auxp->fix_tag)
2037 tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
2038 else
2039 tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
2041 fprintf (file, "\n");
2043 if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux))
2044 continue;
2046 switch (combined->u.syment.n_sclass)
2048 case C_FILE:
2049 fprintf (file, "File ");
2050 break;
2052 case C_STAT:
2053 if (combined->u.syment.n_type == T_NULL)
2054 /* probably a section symbol? */
2056 fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
2057 (long) auxp->u.auxent.x_scn.x_scnlen,
2058 auxp->u.auxent.x_scn.x_nreloc,
2059 auxp->u.auxent.x_scn.x_nlinno);
2060 if (auxp->u.auxent.x_scn.x_checksum != 0
2061 || auxp->u.auxent.x_scn.x_associated != 0
2062 || auxp->u.auxent.x_scn.x_comdat != 0)
2063 fprintf (file, " checksum 0x%lx assoc %d comdat %d",
2064 auxp->u.auxent.x_scn.x_checksum,
2065 auxp->u.auxent.x_scn.x_associated,
2066 auxp->u.auxent.x_scn.x_comdat);
2067 break;
2069 /* else fall through */
2070 case C_EXT:
2071 if (ISFCN (combined->u.syment.n_type))
2073 fprintf (file,
2074 _("AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld"),
2075 tagndx,
2076 auxp->u.auxent.x_sym.x_misc.x_fsize,
2077 auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_lnnoptr,
2078 (auxp->fix_end
2079 ? ((long)
2080 (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2081 - root))
2082 : auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l));
2083 break;
2085 /* else fall through */
2086 default:
2087 fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
2088 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
2089 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
2090 tagndx);
2091 if (auxp->fix_end)
2092 fprintf (file, " endndx %ld",
2093 ((long)
2094 (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2095 - root)));
2096 break;
2100 if (l)
2102 fprintf (file, "\n%s :", l->u.sym->name);
2103 l++;
2104 while (l->line_number)
2106 fprintf (file, "\n%4d : 0x%lx",
2107 l->line_number,
2108 ((unsigned long)
2109 (l->u.offset + symbol->section->vma)));
2110 l++;
2114 else
2116 bfd_print_symbol_vandf ((PTR) file, symbol);
2117 fprintf (file, " %-5s %s %s %s",
2118 symbol->section->name,
2119 coffsymbol (symbol)->native ? "n" : "g",
2120 coffsymbol (symbol)->lineno ? "l" : " ",
2121 symbol->name);
2126 /* Return whether a symbol name implies a local symbol. In COFF,
2127 local symbols generally start with ``.L''. Most targets use this
2128 function for the is_local_label_name entry point, but some may
2129 override it. */
2131 boolean
2132 _bfd_coff_is_local_label_name (abfd, name)
2133 bfd *abfd ATTRIBUTE_UNUSED;
2134 const char *name;
2136 return name[0] == '.' && name[1] == 'L';
2139 /* Provided a BFD, a section and an offset into the section, calculate
2140 and return the name of the source file and the line nearest to the
2141 wanted location. */
2143 /*ARGSUSED*/
2144 boolean
2145 coff_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
2146 functionname_ptr, line_ptr)
2147 bfd *abfd;
2148 asection *section;
2149 asymbol **symbols;
2150 bfd_vma offset;
2151 CONST char **filename_ptr;
2152 CONST char **functionname_ptr;
2153 unsigned int *line_ptr;
2155 boolean found;
2156 unsigned int i;
2157 unsigned int line_base;
2158 coff_data_type *cof = coff_data (abfd);
2159 /* Run through the raw syments if available */
2160 combined_entry_type *p;
2161 combined_entry_type *pend;
2162 alent *l;
2163 struct coff_section_tdata *sec_data;
2165 /* Before looking through the symbol table, try to use a .stab
2166 section to find the information. */
2167 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
2168 &found, filename_ptr,
2169 functionname_ptr, line_ptr,
2170 &coff_data (abfd)->line_info))
2171 return false;
2172 if (found)
2173 return true;
2175 *filename_ptr = 0;
2176 *functionname_ptr = 0;
2177 *line_ptr = 0;
2179 /* Don't try and find line numbers in a non coff file */
2180 if (abfd->xvec->flavour != bfd_target_coff_flavour)
2181 return false;
2183 if (cof == NULL)
2184 return false;
2186 /* Find the first C_FILE symbol. */
2187 p = cof->raw_syments;
2188 if (!p)
2189 return false;
2191 pend = p + cof->raw_syment_count;
2192 while (p < pend)
2194 if (p->u.syment.n_sclass == C_FILE)
2195 break;
2196 p += 1 + p->u.syment.n_numaux;
2199 if (p < pend)
2201 bfd_vma sec_vma;
2202 bfd_vma maxdiff;
2204 /* Look through the C_FILE symbols to find the best one. */
2205 sec_vma = bfd_get_section_vma (abfd, section);
2206 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2207 maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
2208 while (1)
2210 combined_entry_type *p2;
2212 for (p2 = p + 1 + p->u.syment.n_numaux;
2213 p2 < pend;
2214 p2 += 1 + p2->u.syment.n_numaux)
2216 if (p2->u.syment.n_scnum > 0
2217 && (section
2218 == coff_section_from_bfd_index (abfd,
2219 p2->u.syment.n_scnum)))
2220 break;
2221 if (p2->u.syment.n_sclass == C_FILE)
2223 p2 = pend;
2224 break;
2228 /* We use <= MAXDIFF here so that if we get a zero length
2229 file, we actually use the next file entry. */
2230 if (p2 < pend
2231 && offset + sec_vma >= (bfd_vma) p2->u.syment.n_value
2232 && offset + sec_vma - (bfd_vma) p2->u.syment.n_value <= maxdiff)
2234 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2235 maxdiff = offset + sec_vma - p2->u.syment.n_value;
2238 /* Avoid endless loops on erroneous files by ensuring that
2239 we always move forward in the file. */
2240 if (p - cof->raw_syments >= p->u.syment.n_value)
2241 break;
2243 p = cof->raw_syments + p->u.syment.n_value;
2244 if (p > pend || p->u.syment.n_sclass != C_FILE)
2245 break;
2249 /* Now wander though the raw linenumbers of the section */
2250 /* If we have been called on this section before, and the offset we
2251 want is further down then we can prime the lookup loop. */
2252 sec_data = coff_section_data (abfd, section);
2253 if (sec_data != NULL
2254 && sec_data->i > 0
2255 && offset >= sec_data->offset)
2257 i = sec_data->i;
2258 *functionname_ptr = sec_data->function;
2259 line_base = sec_data->line_base;
2261 else
2263 i = 0;
2264 line_base = 0;
2267 if (section->lineno != NULL)
2269 bfd_vma last_value = 0;
2271 l = &section->lineno[i];
2273 for (; i < section->lineno_count; i++)
2275 if (l->line_number == 0)
2277 /* Get the symbol this line number points at */
2278 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
2279 if (coff->symbol.value > offset)
2280 break;
2281 *functionname_ptr = coff->symbol.name;
2282 last_value = coff->symbol.value;
2283 if (coff->native)
2285 combined_entry_type *s = coff->native;
2286 s = s + 1 + s->u.syment.n_numaux;
2288 /* In XCOFF a debugging symbol can follow the
2289 function symbol. */
2290 if (s->u.syment.n_scnum == N_DEBUG)
2291 s = s + 1 + s->u.syment.n_numaux;
2293 /* S should now point to the .bf of the function. */
2294 if (s->u.syment.n_numaux)
2296 /* The linenumber is stored in the auxent. */
2297 union internal_auxent *a = &((s + 1)->u.auxent);
2298 line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
2299 *line_ptr = line_base;
2303 else
2305 if (l->u.offset > offset)
2306 break;
2307 *line_ptr = l->line_number + line_base - 1;
2309 l++;
2312 /* If we fell off the end of the loop, then assume that this
2313 symbol has no line number info. Otherwise, symbols with no
2314 line number info get reported with the line number of the
2315 last line of the last symbol which does have line number
2316 info. We use 0x100 as a slop to account for cases where the
2317 last line has executable code. */
2318 if (i >= section->lineno_count
2319 && last_value != 0
2320 && offset - last_value > 0x100)
2322 *functionname_ptr = NULL;
2323 *line_ptr = 0;
2327 /* Cache the results for the next call. */
2328 if (sec_data == NULL && section->owner == abfd)
2330 section->used_by_bfd =
2331 ((PTR) bfd_zalloc (abfd,
2332 sizeof (struct coff_section_tdata)));
2333 sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2335 if (sec_data != NULL)
2337 sec_data->offset = offset;
2338 sec_data->i = i;
2339 sec_data->function = *functionname_ptr;
2340 sec_data->line_base = line_base;
2343 return true;
2347 coff_sizeof_headers (abfd, reloc)
2348 bfd *abfd;
2349 boolean reloc;
2351 size_t size;
2353 if (reloc == false)
2355 size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
2357 else
2359 size = bfd_coff_filhsz (abfd);
2362 size += abfd->section_count * bfd_coff_scnhsz (abfd);
2363 return size;
2366 /* Change the class of a coff symbol held by BFD. */
2367 boolean
2368 bfd_coff_set_symbol_class (abfd, symbol, class)
2369 bfd * abfd;
2370 asymbol * symbol;
2371 unsigned int class;
2373 coff_symbol_type * csym;
2375 csym = coff_symbol_from (abfd, symbol);
2376 if (csym == NULL)
2378 bfd_set_error (bfd_error_invalid_operation);
2379 return false;
2381 else if (csym->native == NULL)
2383 /* This is an alien symbol which no native coff backend data.
2384 We cheat here by creating a fake native entry for it and
2385 then filling in the class. This code is based on that in
2386 coff_write_alien_symbol(). */
2388 combined_entry_type * native;
2390 native = (combined_entry_type *) bfd_alloc (abfd, sizeof (* native));
2391 if (native == NULL)
2392 return false;
2394 memset (native, 0, sizeof (* native));
2396 native->u.syment.n_type = T_NULL;
2397 native->u.syment.n_sclass = class;
2399 if (bfd_is_und_section (symbol->section))
2401 native->u.syment.n_scnum = N_UNDEF;
2402 native->u.syment.n_value = symbol->value;
2404 else if (bfd_is_com_section (symbol->section))
2406 native->u.syment.n_scnum = N_UNDEF;
2407 native->u.syment.n_value = symbol->value;
2409 else
2411 native->u.syment.n_scnum =
2412 symbol->section->output_section->target_index;
2413 native->u.syment.n_value = (symbol->value
2414 + symbol->section->output_offset);
2415 if (! obj_pe (abfd))
2416 native->u.syment.n_value += symbol->section->output_section->vma;
2418 /* Copy the any flags from the the file header into the symbol.
2419 FIXME: Why? */
2420 native->u.syment.n_flags = bfd_asymbol_bfd (& csym->symbol)->flags;
2423 csym->native = native;
2425 else
2427 csym->native->u.syment.n_sclass = class;
2430 return true;