* mips.h (Elf32_Internal_Msym): New structure.
[binutils.git] / bfd / coffgen.c
bloba06ed663b3d264f3cfa359e100d97439749bac09
1 /* Support for the generic parts of COFF, for BFD.
2 Copyright 1990, 91, 92, 93, 94, 95, 96, 97, 1998
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->flags = bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name);
145 return_section->target_index = target_index;
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;
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)
590 syment->n_value = coff_symbol_ptr->symbol.value;
592 else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
594 syment->n_scnum = N_UNDEF;
595 syment->n_value = 0;
597 else
599 if (coff_symbol_ptr->symbol.section)
601 syment->n_scnum =
602 coff_symbol_ptr->symbol.section->output_section->target_index;
604 syment->n_value = (coff_symbol_ptr->symbol.value
605 + coff_symbol_ptr->symbol.section->output_offset);
606 if (! obj_pe (abfd))
607 syment->n_value +=
608 coff_symbol_ptr->symbol.section->output_section->vma;
610 else
612 BFD_ASSERT (0);
613 /* This can happen, but I don't know why yet (steve@cygnus.com) */
614 syment->n_scnum = N_ABS;
615 syment->n_value = coff_symbol_ptr->symbol.value;
620 /* Run through all the symbols in the symbol table and work out what
621 their indexes into the symbol table will be when output.
623 Coff requires that each C_FILE symbol points to the next one in the
624 chain, and that the last one points to the first external symbol. We
625 do that here too. */
627 boolean
628 coff_renumber_symbols (bfd_ptr, first_undef)
629 bfd *bfd_ptr;
630 int *first_undef;
632 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
633 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
634 unsigned int native_index = 0;
635 struct internal_syment *last_file = (struct internal_syment *) NULL;
636 unsigned int symbol_index;
638 /* COFF demands that undefined symbols come after all other symbols.
639 Since we don't need to impose this extra knowledge on all our
640 client programs, deal with that here. Sort the symbol table;
641 just move the undefined symbols to the end, leaving the rest
642 alone. The O'Reilly book says that defined global symbols come
643 at the end before the undefined symbols, so we do that here as
644 well. */
645 /* @@ Do we have some condition we could test for, so we don't always
646 have to do this? I don't think relocatability is quite right, but
647 I'm not certain. [raeburn:19920508.1711EST] */
649 asymbol **newsyms;
650 unsigned int i;
652 newsyms = (asymbol **) bfd_alloc (bfd_ptr,
653 sizeof (asymbol *) * (symbol_count + 1));
654 if (!newsyms)
655 return false;
656 bfd_ptr->outsymbols = newsyms;
657 for (i = 0; i < symbol_count; i++)
658 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
659 || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
660 && !bfd_is_com_section (symbol_ptr_ptr[i]->section)
661 && ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) != 0
662 || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
663 == 0))))
664 *newsyms++ = symbol_ptr_ptr[i];
666 for (i = 0; i < symbol_count; i++)
667 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
668 && !bfd_is_und_section (symbol_ptr_ptr[i]->section)
669 && (bfd_is_com_section (symbol_ptr_ptr[i]->section)
670 || ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) == 0
671 && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
672 != 0))))
673 *newsyms++ = symbol_ptr_ptr[i];
675 *first_undef = newsyms - bfd_ptr->outsymbols;
677 for (i = 0; i < symbol_count; i++)
678 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
679 && bfd_is_und_section (symbol_ptr_ptr[i]->section))
680 *newsyms++ = symbol_ptr_ptr[i];
681 *newsyms = (asymbol *) NULL;
682 symbol_ptr_ptr = bfd_ptr->outsymbols;
685 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
687 coff_symbol_type *coff_symbol_ptr = coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
688 symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
689 if (coff_symbol_ptr && coff_symbol_ptr->native)
691 combined_entry_type *s = coff_symbol_ptr->native;
692 int i;
694 if (s->u.syment.n_sclass == C_FILE)
696 if (last_file != (struct internal_syment *) NULL)
697 last_file->n_value = native_index;
698 last_file = &(s->u.syment);
700 else
703 /* Modify the symbol values according to their section and
704 type */
706 fixup_symbol_value (bfd_ptr, coff_symbol_ptr, &(s->u.syment));
708 for (i = 0; i < s->u.syment.n_numaux + 1; i++)
709 s[i].offset = native_index++;
711 else
713 native_index++;
716 obj_conv_table_size (bfd_ptr) = native_index;
718 return true;
721 /* Run thorough the symbol table again, and fix it so that all
722 pointers to entries are changed to the entries' index in the output
723 symbol table. */
725 void
726 coff_mangle_symbols (bfd_ptr)
727 bfd *bfd_ptr;
729 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
730 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
731 unsigned int symbol_index;
733 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
735 coff_symbol_type *coff_symbol_ptr =
736 coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
738 if (coff_symbol_ptr && coff_symbol_ptr->native)
740 int i;
741 combined_entry_type *s = coff_symbol_ptr->native;
743 if (s->fix_value)
745 /* FIXME: We should use a union here. */
746 s->u.syment.n_value =
747 ((combined_entry_type *) s->u.syment.n_value)->offset;
748 s->fix_value = 0;
750 if (s->fix_line)
752 /* The value is the offset into the line number entries
753 for the symbol's section. On output, the symbol's
754 section should be N_DEBUG. */
755 s->u.syment.n_value =
756 (coff_symbol_ptr->symbol.section->output_section->line_filepos
757 + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr));
758 coff_symbol_ptr->symbol.section =
759 coff_section_from_bfd_index (bfd_ptr, N_DEBUG);
760 BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
762 for (i = 0; i < s->u.syment.n_numaux; i++)
764 combined_entry_type *a = s + i + 1;
765 if (a->fix_tag)
767 a->u.auxent.x_sym.x_tagndx.l =
768 a->u.auxent.x_sym.x_tagndx.p->offset;
769 a->fix_tag = 0;
771 if (a->fix_end)
773 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
774 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
775 a->fix_end = 0;
777 if (a->fix_scnlen)
779 a->u.auxent.x_csect.x_scnlen.l =
780 a->u.auxent.x_csect.x_scnlen.p->offset;
781 a->fix_scnlen = 0;
788 static void
789 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
790 debug_string_section_p, debug_string_size_p)
791 bfd *abfd;
792 asymbol *symbol;
793 combined_entry_type *native;
794 bfd_size_type *string_size_p;
795 asection **debug_string_section_p;
796 bfd_size_type *debug_string_size_p;
798 unsigned int name_length;
799 union internal_auxent *auxent;
800 char *name = (char *) (symbol->name);
802 if (name == (char *) NULL)
804 /* coff symbols always have names, so we'll make one up */
805 symbol->name = "strange";
806 name = (char *) symbol->name;
808 name_length = strlen (name);
810 if (native->u.syment.n_sclass == C_FILE
811 && native->u.syment.n_numaux > 0)
813 strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
814 auxent = &(native + 1)->u.auxent;
816 if (bfd_coff_long_filenames (abfd))
818 if (name_length <= FILNMLEN)
820 strncpy (auxent->x_file.x_fname, name, FILNMLEN);
822 else
824 auxent->x_file.x_n.x_offset = *string_size_p + STRING_SIZE_SIZE;
825 auxent->x_file.x_n.x_zeroes = 0;
826 *string_size_p += name_length + 1;
829 else
831 strncpy (auxent->x_file.x_fname, name, FILNMLEN);
832 if (name_length > FILNMLEN)
834 name[FILNMLEN] = '\0';
838 else
840 if (name_length <= SYMNMLEN)
842 /* This name will fit into the symbol neatly */
843 strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
845 else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
847 native->u.syment._n._n_n._n_offset = (*string_size_p
848 + STRING_SIZE_SIZE);
849 native->u.syment._n._n_n._n_zeroes = 0;
850 *string_size_p += name_length + 1;
852 else
854 long filepos;
855 bfd_byte buf[2];
857 /* This name should be written into the .debug section. For
858 some reason each name is preceded by a two byte length
859 and also followed by a null byte. FIXME: We assume that
860 the .debug section has already been created, and that it
861 is large enough. */
862 if (*debug_string_section_p == (asection *) NULL)
863 *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
864 filepos = bfd_tell (abfd);
865 bfd_put_16 (abfd, name_length + 1, buf);
866 if (!bfd_set_section_contents (abfd,
867 *debug_string_section_p,
868 (PTR) buf,
869 (file_ptr) *debug_string_size_p,
870 (bfd_size_type) 2)
871 || !bfd_set_section_contents (abfd,
872 *debug_string_section_p,
873 (PTR) symbol->name,
874 ((file_ptr) *debug_string_size_p
875 + 2),
876 (bfd_size_type) name_length + 1))
877 abort ();
878 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
879 abort ();
880 native->u.syment._n._n_n._n_offset = *debug_string_size_p + 2;
881 native->u.syment._n._n_n._n_zeroes = 0;
882 *debug_string_size_p += name_length + 3;
887 /* We need to keep track of the symbol index so that when we write out
888 the relocs we can get the index for a symbol. This method is a
889 hack. FIXME. */
891 #define set_index(symbol, idx) ((symbol)->udata.i = (idx))
893 /* Write a symbol out to a COFF file. */
895 static boolean
896 coff_write_symbol (abfd, symbol, native, written, string_size_p,
897 debug_string_section_p, debug_string_size_p)
898 bfd *abfd;
899 asymbol *symbol;
900 combined_entry_type *native;
901 unsigned int *written;
902 bfd_size_type *string_size_p;
903 asection **debug_string_section_p;
904 bfd_size_type *debug_string_size_p;
906 unsigned int numaux = native->u.syment.n_numaux;
907 int type = native->u.syment.n_type;
908 int class = native->u.syment.n_sclass;
909 PTR buf;
910 bfd_size_type symesz;
912 if (native->u.syment.n_sclass == C_FILE)
913 symbol->flags |= BSF_DEBUGGING;
915 if (symbol->flags & BSF_DEBUGGING
916 && bfd_is_abs_section (symbol->section))
918 native->u.syment.n_scnum = N_DEBUG;
920 else if (bfd_is_abs_section (symbol->section))
922 native->u.syment.n_scnum = N_ABS;
924 else if (bfd_is_und_section (symbol->section))
926 native->u.syment.n_scnum = N_UNDEF;
928 else
930 native->u.syment.n_scnum =
931 symbol->section->output_section->target_index;
934 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
935 debug_string_section_p, debug_string_size_p);
937 symesz = bfd_coff_symesz (abfd);
938 buf = bfd_alloc (abfd, symesz);
939 if (!buf)
940 return false;
941 bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
942 if (bfd_write (buf, 1, symesz, abfd) != symesz)
943 return false;
944 bfd_release (abfd, buf);
946 if (native->u.syment.n_numaux > 0)
948 bfd_size_type auxesz;
949 unsigned int j;
951 auxesz = bfd_coff_auxesz (abfd);
952 buf = bfd_alloc (abfd, auxesz);
953 if (!buf)
954 return false;
955 for (j = 0; j < native->u.syment.n_numaux; j++)
957 bfd_coff_swap_aux_out (abfd,
958 &((native + j + 1)->u.auxent),
959 type,
960 class,
962 native->u.syment.n_numaux,
963 buf);
964 if (bfd_write (buf, 1, auxesz, abfd) != auxesz)
965 return false;
967 bfd_release (abfd, buf);
970 /* Store the index for use when we write out the relocs. */
971 set_index (symbol, *written);
973 *written += numaux + 1;
974 return true;
977 /* Write out a symbol to a COFF file that does not come from a COFF
978 file originally. This symbol may have been created by the linker,
979 or we may be linking a non COFF file to a COFF file. */
981 static boolean
982 coff_write_alien_symbol (abfd, symbol, written, string_size_p,
983 debug_string_section_p, debug_string_size_p)
984 bfd *abfd;
985 asymbol *symbol;
986 unsigned int *written;
987 bfd_size_type *string_size_p;
988 asection **debug_string_section_p;
989 bfd_size_type *debug_string_size_p;
991 combined_entry_type *native;
992 combined_entry_type dummy;
994 native = &dummy;
995 native->u.syment.n_type = T_NULL;
996 native->u.syment.n_flags = 0;
997 if (bfd_is_und_section (symbol->section))
999 native->u.syment.n_scnum = N_UNDEF;
1000 native->u.syment.n_value = symbol->value;
1002 else if (bfd_is_com_section (symbol->section))
1004 native->u.syment.n_scnum = N_UNDEF;
1005 native->u.syment.n_value = symbol->value;
1007 else if (symbol->flags & BSF_DEBUGGING)
1009 /* There isn't much point to writing out a debugging symbol
1010 unless we are prepared to convert it into COFF debugging
1011 format. So, we just ignore them. We must clobber the symbol
1012 name to keep it from being put in the string table. */
1013 symbol->name = "";
1014 return true;
1016 else
1018 native->u.syment.n_scnum =
1019 symbol->section->output_section->target_index;
1020 native->u.syment.n_value = (symbol->value
1021 + symbol->section->output_offset);
1022 if (! obj_pe (abfd))
1023 native->u.syment.n_value += symbol->section->output_section->vma;
1025 /* Copy the any flags from the the file header into the symbol.
1026 FIXME: Why? */
1028 coff_symbol_type *c = coff_symbol_from (abfd, symbol);
1029 if (c != (coff_symbol_type *) NULL)
1030 native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
1034 native->u.syment.n_type = 0;
1035 if (symbol->flags & BSF_LOCAL)
1036 native->u.syment.n_sclass = C_STAT;
1037 else if (symbol->flags & BSF_WEAK)
1038 native->u.syment.n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1039 else
1040 native->u.syment.n_sclass = C_EXT;
1041 native->u.syment.n_numaux = 0;
1043 return coff_write_symbol (abfd, symbol, native, written, string_size_p,
1044 debug_string_section_p, debug_string_size_p);
1047 /* Write a native symbol to a COFF file. */
1049 static boolean
1050 coff_write_native_symbol (abfd, symbol, written, string_size_p,
1051 debug_string_section_p, debug_string_size_p)
1052 bfd *abfd;
1053 coff_symbol_type *symbol;
1054 unsigned int *written;
1055 bfd_size_type *string_size_p;
1056 asection **debug_string_section_p;
1057 bfd_size_type *debug_string_size_p;
1059 combined_entry_type *native = symbol->native;
1060 alent *lineno = symbol->lineno;
1062 /* If this symbol has an associated line number, we must store the
1063 symbol index in the line number field. We also tag the auxent to
1064 point to the right place in the lineno table. */
1065 if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
1067 unsigned int count = 0;
1068 lineno[count].u.offset = *written;
1069 if (native->u.syment.n_numaux)
1071 union internal_auxent *a = &((native + 1)->u.auxent);
1073 a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
1074 symbol->symbol.section->output_section->moving_line_filepos;
1077 /* Count and relocate all other linenumbers. */
1078 count++;
1079 while (lineno[count].line_number != 0)
1081 #if 0
1082 /* 13 april 92. sac
1083 I've been told this, but still need proof:
1084 > The second bug is also in `bfd/coffcode.h'. This bug
1085 > causes the linker to screw up the pc-relocations for
1086 > all the line numbers in COFF code. This bug isn't only
1087 > specific to A29K implementations, but affects all
1088 > systems using COFF format binaries. Note that in COFF
1089 > object files, the line number core offsets output by
1090 > the assembler are relative to the start of each
1091 > procedure, not to the start of the .text section. This
1092 > patch relocates the line numbers relative to the
1093 > `native->u.syment.n_value' instead of the section
1094 > virtual address.
1095 > modular!olson@cs.arizona.edu (Jon Olson)
1097 lineno[count].u.offset += native->u.syment.n_value;
1098 #else
1099 lineno[count].u.offset +=
1100 (symbol->symbol.section->output_section->vma
1101 + symbol->symbol.section->output_offset);
1102 #endif
1103 count++;
1105 symbol->done_lineno = true;
1107 symbol->symbol.section->output_section->moving_line_filepos +=
1108 count * bfd_coff_linesz (abfd);
1111 return coff_write_symbol (abfd, &(symbol->symbol), native, written,
1112 string_size_p, debug_string_section_p,
1113 debug_string_size_p);
1116 /* Write out the COFF symbols. */
1118 boolean
1119 coff_write_symbols (abfd)
1120 bfd *abfd;
1122 bfd_size_type string_size;
1123 asection *debug_string_section;
1124 bfd_size_type debug_string_size;
1125 unsigned int i;
1126 unsigned int limit = bfd_get_symcount (abfd);
1127 unsigned int written = 0;
1128 asymbol **p;
1130 string_size = 0;
1131 debug_string_section = NULL;
1132 debug_string_size = 0;
1134 /* If this target supports long section names, they must be put into
1135 the string table. This is supported by PE. This code must
1136 handle section names just as they are handled in
1137 coff_write_object_contents. */
1138 if (bfd_coff_long_section_names (abfd))
1140 asection *o;
1142 for (o = abfd->sections; o != NULL; o = o->next)
1144 size_t len;
1146 len = strlen (o->name);
1147 if (len > SCNNMLEN)
1148 string_size += len + 1;
1152 /* Seek to the right place */
1153 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1154 return false;
1156 /* Output all the symbols we have */
1158 written = 0;
1159 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
1161 asymbol *symbol = *p;
1162 coff_symbol_type *c_symbol = coff_symbol_from (abfd, symbol);
1164 if (c_symbol == (coff_symbol_type *) NULL
1165 || c_symbol->native == (combined_entry_type *) NULL)
1167 if (!coff_write_alien_symbol (abfd, symbol, &written, &string_size,
1168 &debug_string_section,
1169 &debug_string_size))
1170 return false;
1172 else
1174 if (!coff_write_native_symbol (abfd, c_symbol, &written,
1175 &string_size, &debug_string_section,
1176 &debug_string_size))
1177 return false;
1181 obj_raw_syment_count (abfd) = written;
1183 /* Now write out strings */
1185 if (string_size != 0)
1187 unsigned int size = string_size + STRING_SIZE_SIZE;
1188 bfd_byte buffer[STRING_SIZE_SIZE];
1190 #if STRING_SIZE_SIZE == 4
1191 bfd_h_put_32 (abfd, size, buffer);
1192 #else
1193 #error Change bfd_h_put_32
1194 #endif
1195 if (bfd_write ((PTR) buffer, 1, sizeof (buffer), abfd) != sizeof (buffer))
1196 return false;
1198 /* Handle long section names. This code must handle section
1199 names just as they are handled in coff_write_object_contents. */
1200 if (bfd_coff_long_section_names (abfd))
1202 asection *o;
1204 for (o = abfd->sections; o != NULL; o = o->next)
1206 size_t len;
1208 len = strlen (o->name);
1209 if (len > SCNNMLEN)
1211 if (bfd_write (o->name, 1, len + 1, abfd) != len + 1)
1212 return false;
1217 for (p = abfd->outsymbols, i = 0;
1218 i < limit;
1219 i++, p++)
1221 asymbol *q = *p;
1222 size_t name_length = strlen (q->name);
1223 coff_symbol_type *c_symbol = coff_symbol_from (abfd, q);
1224 size_t maxlen;
1226 /* Figure out whether the symbol name should go in the string
1227 table. Symbol names that are short enough are stored
1228 directly in the syment structure. File names permit a
1229 different, longer, length in the syment structure. On
1230 XCOFF, some symbol names are stored in the .debug section
1231 rather than in the string table. */
1233 if (c_symbol == NULL
1234 || c_symbol->native == NULL)
1236 /* This is not a COFF symbol, so it certainly is not a
1237 file name, nor does it go in the .debug section. */
1238 maxlen = SYMNMLEN;
1240 else if (bfd_coff_symname_in_debug (abfd,
1241 &c_symbol->native->u.syment))
1243 /* This symbol name is in the XCOFF .debug section.
1244 Don't write it into the string table. */
1245 maxlen = name_length;
1247 else if (c_symbol->native->u.syment.n_sclass == C_FILE
1248 && c_symbol->native->u.syment.n_numaux > 0)
1249 maxlen = FILNMLEN;
1250 else
1251 maxlen = SYMNMLEN;
1253 if (name_length > maxlen)
1255 if (bfd_write ((PTR) (q->name), 1, name_length + 1, abfd)
1256 != name_length + 1)
1257 return false;
1261 else
1263 /* We would normally not write anything here, but we'll write
1264 out 4 so that any stupid coff reader which tries to read the
1265 string table even when there isn't one won't croak. */
1266 unsigned int size = STRING_SIZE_SIZE;
1267 bfd_byte buffer[STRING_SIZE_SIZE];
1269 #if STRING_SIZE_SIZE == 4
1270 bfd_h_put_32 (abfd, size, buffer);
1271 #else
1272 #error Change bfd_h_put_32
1273 #endif
1274 if (bfd_write ((PTR) buffer, 1, STRING_SIZE_SIZE, abfd)
1275 != STRING_SIZE_SIZE)
1276 return false;
1279 /* Make sure the .debug section was created to be the correct size.
1280 We should create it ourselves on the fly, but we don't because
1281 BFD won't let us write to any section until we know how large all
1282 the sections are. We could still do it by making another pass
1283 over the symbols. FIXME. */
1284 BFD_ASSERT (debug_string_size == 0
1285 || (debug_string_section != (asection *) NULL
1286 && (BFD_ALIGN (debug_string_size,
1287 1 << debug_string_section->alignment_power)
1288 == bfd_section_size (abfd, debug_string_section))));
1290 return true;
1293 boolean
1294 coff_write_linenumbers (abfd)
1295 bfd *abfd;
1297 asection *s;
1298 bfd_size_type linesz;
1299 PTR buff;
1301 linesz = bfd_coff_linesz (abfd);
1302 buff = bfd_alloc (abfd, linesz);
1303 if (!buff)
1304 return false;
1305 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1307 if (s->lineno_count)
1309 asymbol **q = abfd->outsymbols;
1310 if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
1311 return false;
1312 /* Find all the linenumbers in this section */
1313 while (*q)
1315 asymbol *p = *q;
1316 if (p->section->output_section == s)
1318 alent *l =
1319 BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
1320 (bfd_asymbol_bfd (p), p));
1321 if (l)
1323 /* Found a linenumber entry, output */
1324 struct internal_lineno out;
1325 memset ((PTR) & out, 0, sizeof (out));
1326 out.l_lnno = 0;
1327 out.l_addr.l_symndx = l->u.offset;
1328 bfd_coff_swap_lineno_out (abfd, &out, buff);
1329 if (bfd_write (buff, 1, linesz, abfd) != linesz)
1330 return false;
1331 l++;
1332 while (l->line_number)
1334 out.l_lnno = l->line_number;
1335 out.l_addr.l_symndx = l->u.offset;
1336 bfd_coff_swap_lineno_out (abfd, &out, buff);
1337 if (bfd_write (buff, 1, linesz, abfd) != linesz)
1338 return false;
1339 l++;
1343 q++;
1347 bfd_release (abfd, buff);
1348 return true;
1351 /*ARGSUSED */
1352 alent *
1353 coff_get_lineno (ignore_abfd, symbol)
1354 bfd *ignore_abfd;
1355 asymbol *symbol;
1357 return coffsymbol (symbol)->lineno;
1360 #if 0
1362 /* This is only called from coff_add_missing_symbols, which has been
1363 disabled. */
1365 asymbol *
1366 coff_section_symbol (abfd, name)
1367 bfd *abfd;
1368 char *name;
1370 asection *sec = bfd_make_section_old_way (abfd, name);
1371 asymbol *sym;
1372 combined_entry_type *csym;
1374 sym = sec->symbol;
1375 csym = coff_symbol_from (abfd, sym)->native;
1376 /* Make sure back-end COFF stuff is there. */
1377 if (csym == 0)
1379 struct foo
1381 coff_symbol_type sym;
1382 /* @@FIXME This shouldn't use a fixed size!! */
1383 combined_entry_type e[10];
1385 struct foo *f;
1386 f = (struct foo *) bfd_alloc (abfd, sizeof (*f));
1387 if (!f)
1389 bfd_set_error (bfd_error_no_error);
1390 return NULL;
1392 memset ((char *) f, 0, sizeof (*f));
1393 coff_symbol_from (abfd, sym)->native = csym = f->e;
1395 csym[0].u.syment.n_sclass = C_STAT;
1396 csym[0].u.syment.n_numaux = 1;
1397 /* SF_SET_STATICS (sym); @@ ??? */
1398 csym[1].u.auxent.x_scn.x_scnlen = sec->_raw_size;
1399 csym[1].u.auxent.x_scn.x_nreloc = sec->reloc_count;
1400 csym[1].u.auxent.x_scn.x_nlinno = sec->lineno_count;
1402 if (sec->output_section == NULL)
1404 sec->output_section = sec;
1405 sec->output_offset = 0;
1408 return sym;
1411 #endif /* 0 */
1413 /* This function transforms the offsets into the symbol table into
1414 pointers to syments. */
1416 static void
1417 coff_pointerize_aux (abfd, table_base, symbol, indaux, auxent)
1418 bfd *abfd;
1419 combined_entry_type *table_base;
1420 combined_entry_type *symbol;
1421 unsigned int indaux;
1422 combined_entry_type *auxent;
1424 unsigned int type = symbol->u.syment.n_type;
1425 unsigned int class = symbol->u.syment.n_sclass;
1427 if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1429 if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1430 (abfd, table_base, symbol, indaux, auxent))
1431 return;
1434 /* Don't bother if this is a file or a section */
1435 if (class == C_STAT && type == T_NULL)
1436 return;
1437 if (class == C_FILE)
1438 return;
1440 /* Otherwise patch up */
1441 #define N_TMASK coff_data (abfd)->local_n_tmask
1442 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1443 if ((ISFCN (type) || ISTAG (class) || class == C_BLOCK || class == C_FCN)
1444 && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l > 0)
1446 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
1447 table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
1448 auxent->fix_end = 1;
1450 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1451 generate one, so we must be careful to ignore it. */
1452 if (auxent->u.auxent.x_sym.x_tagndx.l > 0)
1454 auxent->u.auxent.x_sym.x_tagndx.p =
1455 table_base + auxent->u.auxent.x_sym.x_tagndx.l;
1456 auxent->fix_tag = 1;
1460 /* Allocate space for the ".debug" section, and read it.
1461 We did not read the debug section until now, because
1462 we didn't want to go to the trouble until someone needed it. */
1464 static char *
1465 build_debug_section (abfd)
1466 bfd *abfd;
1468 char *debug_section;
1469 long position;
1471 asection *sect = bfd_get_section_by_name (abfd, ".debug");
1473 if (!sect)
1475 bfd_set_error (bfd_error_no_debug_section);
1476 return NULL;
1479 debug_section = (PTR) bfd_alloc (abfd,
1480 bfd_get_section_size_before_reloc (sect));
1481 if (debug_section == NULL)
1482 return NULL;
1484 /* Seek to the beginning of the `.debug' section and read it.
1485 Save the current position first; it is needed by our caller.
1486 Then read debug section and reset the file pointer. */
1488 position = bfd_tell (abfd);
1489 if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0
1490 || (bfd_read (debug_section,
1491 bfd_get_section_size_before_reloc (sect), 1, abfd)
1492 != bfd_get_section_size_before_reloc (sect))
1493 || bfd_seek (abfd, position, SEEK_SET) != 0)
1494 return NULL;
1495 return debug_section;
1499 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1500 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1501 be \0-terminated. */
1502 static char *
1503 copy_name (abfd, name, maxlen)
1504 bfd *abfd;
1505 char *name;
1506 int maxlen;
1508 int len;
1509 char *newname;
1511 for (len = 0; len < maxlen; ++len)
1513 if (name[len] == '\0')
1515 break;
1519 if ((newname = (PTR) bfd_alloc (abfd, len + 1)) == NULL)
1520 return (NULL);
1521 strncpy (newname, name, len);
1522 newname[len] = '\0';
1523 return newname;
1526 /* Read in the external symbols. */
1528 boolean
1529 _bfd_coff_get_external_symbols (abfd)
1530 bfd *abfd;
1532 bfd_size_type symesz;
1533 size_t size;
1534 PTR syms;
1536 if (obj_coff_external_syms (abfd) != NULL)
1537 return true;
1539 symesz = bfd_coff_symesz (abfd);
1541 size = obj_raw_syment_count (abfd) * symesz;
1543 syms = (PTR) bfd_malloc (size);
1544 if (syms == NULL && size != 0)
1545 return false;
1547 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
1548 || bfd_read (syms, size, 1, abfd) != size)
1550 if (syms != NULL)
1551 free (syms);
1552 return false;
1555 obj_coff_external_syms (abfd) = syms;
1557 return true;
1560 /* Read in the external strings. The strings are not loaded until
1561 they are needed. This is because we have no simple way of
1562 detecting a missing string table in an archive. */
1564 const char *
1565 _bfd_coff_read_string_table (abfd)
1566 bfd *abfd;
1568 char extstrsize[STRING_SIZE_SIZE];
1569 size_t strsize;
1570 char *strings;
1572 if (obj_coff_strings (abfd) != NULL)
1573 return obj_coff_strings (abfd);
1575 if (obj_sym_filepos (abfd) == 0)
1577 bfd_set_error (bfd_error_no_symbols);
1578 return NULL;
1581 if (bfd_seek (abfd,
1582 (obj_sym_filepos (abfd)
1583 + obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd)),
1584 SEEK_SET) != 0)
1585 return NULL;
1587 if (bfd_read (extstrsize, sizeof extstrsize, 1, abfd) != sizeof extstrsize)
1589 if (bfd_get_error () != bfd_error_file_truncated)
1590 return NULL;
1592 /* There is no string table. */
1593 strsize = STRING_SIZE_SIZE;
1595 else
1597 #if STRING_SIZE_SIZE == 4
1598 strsize = bfd_h_get_32 (abfd, (bfd_byte *) extstrsize);
1599 #else
1600 #error Change bfd_h_get_32
1601 #endif
1604 if (strsize < STRING_SIZE_SIZE)
1606 (*_bfd_error_handler)
1607 (_("%s: bad string table size %lu"), bfd_get_filename (abfd),
1608 (unsigned long) strsize);
1609 bfd_set_error (bfd_error_bad_value);
1610 return NULL;
1613 strings = (char *) bfd_malloc (strsize);
1614 if (strings == NULL)
1615 return NULL;
1617 if (bfd_read (strings + STRING_SIZE_SIZE,
1618 strsize - STRING_SIZE_SIZE, 1, abfd)
1619 != strsize - STRING_SIZE_SIZE)
1621 free (strings);
1622 return NULL;
1625 obj_coff_strings (abfd) = strings;
1627 return strings;
1630 /* Free up the external symbols and strings read from a COFF file. */
1632 boolean
1633 _bfd_coff_free_symbols (abfd)
1634 bfd *abfd;
1636 if (obj_coff_external_syms (abfd) != NULL
1637 && ! obj_coff_keep_syms (abfd))
1639 free (obj_coff_external_syms (abfd));
1640 obj_coff_external_syms (abfd) = NULL;
1642 if (obj_coff_strings (abfd) != NULL
1643 && ! obj_coff_keep_strings (abfd))
1645 free (obj_coff_strings (abfd));
1646 obj_coff_strings (abfd) = NULL;
1648 return true;
1651 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1652 knit the symbol names into a normalized form. By normalized here I
1653 mean that all symbols have an n_offset pointer that points to a null-
1654 terminated string. */
1656 combined_entry_type *
1657 coff_get_normalized_symtab (abfd)
1658 bfd *abfd;
1660 combined_entry_type *internal;
1661 combined_entry_type *internal_ptr;
1662 combined_entry_type *symbol_ptr;
1663 combined_entry_type *internal_end;
1664 bfd_size_type symesz;
1665 char *raw_src;
1666 char *raw_end;
1667 const char *string_table = NULL;
1668 char *debug_section = NULL;
1669 unsigned long size;
1671 if (obj_raw_syments (abfd) != NULL)
1672 return obj_raw_syments (abfd);
1674 size = obj_raw_syment_count (abfd) * sizeof (combined_entry_type);
1675 internal = (combined_entry_type *) bfd_zalloc (abfd, size);
1676 if (internal == NULL && size != 0)
1677 return NULL;
1678 internal_end = internal + obj_raw_syment_count (abfd);
1680 if (! _bfd_coff_get_external_symbols (abfd))
1681 return NULL;
1683 raw_src = (char *) obj_coff_external_syms (abfd);
1685 /* mark the end of the symbols */
1686 symesz = bfd_coff_symesz (abfd);
1687 raw_end = (char *) raw_src + obj_raw_syment_count (abfd) * symesz;
1689 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1690 probably possible. If one shows up, it will probably kill us. */
1692 /* Swap all the raw entries */
1693 for (internal_ptr = internal;
1694 raw_src < raw_end;
1695 raw_src += symesz, internal_ptr++)
1698 unsigned int i;
1699 bfd_coff_swap_sym_in (abfd, (PTR) raw_src,
1700 (PTR) & internal_ptr->u.syment);
1701 symbol_ptr = internal_ptr;
1703 for (i = 0;
1704 i < symbol_ptr->u.syment.n_numaux;
1705 i++)
1707 internal_ptr++;
1708 raw_src += symesz;
1709 bfd_coff_swap_aux_in (abfd, (PTR) raw_src,
1710 symbol_ptr->u.syment.n_type,
1711 symbol_ptr->u.syment.n_sclass,
1712 i, symbol_ptr->u.syment.n_numaux,
1713 &(internal_ptr->u.auxent));
1714 coff_pointerize_aux (abfd, internal, symbol_ptr, i,
1715 internal_ptr);
1719 /* Free the raw symbols, but not the strings (if we have them). */
1720 obj_coff_keep_strings (abfd) = true;
1721 if (! _bfd_coff_free_symbols (abfd))
1722 return NULL;
1724 for (internal_ptr = internal; internal_ptr < internal_end;
1725 internal_ptr++)
1727 if (internal_ptr->u.syment.n_sclass == C_FILE
1728 && internal_ptr->u.syment.n_numaux > 0)
1730 /* make a file symbol point to the name in the auxent, since
1731 the text ".file" is redundant */
1732 if ((internal_ptr + 1)->u.auxent.x_file.x_n.x_zeroes == 0)
1734 /* the filename is a long one, point into the string table */
1735 if (string_table == NULL)
1737 string_table = _bfd_coff_read_string_table (abfd);
1738 if (string_table == NULL)
1739 return NULL;
1742 internal_ptr->u.syment._n._n_n._n_offset =
1743 ((long)
1744 (string_table
1745 + (internal_ptr + 1)->u.auxent.x_file.x_n.x_offset));
1747 else
1749 /* ordinary short filename, put into memory anyway */
1750 if (internal_ptr->u.syment.n_numaux > 1
1751 && coff_data (abfd)->pe)
1753 internal_ptr->u.syment._n._n_n._n_offset = (long)
1754 copy_name (abfd, (internal_ptr + 1)->u.auxent.x_file.x_fname,
1755 internal_ptr->u.syment.n_numaux * symesz);
1757 else
1759 internal_ptr->u.syment._n._n_n._n_offset = (long)
1760 copy_name (abfd, (internal_ptr + 1)->u.auxent.x_file.x_fname,
1761 FILNMLEN);
1765 else
1767 if (internal_ptr->u.syment._n._n_n._n_zeroes != 0)
1769 /* This is a "short" name. Make it long. */
1770 unsigned long i = 0;
1771 char *newstring = NULL;
1773 /* find the length of this string without walking into memory
1774 that isn't ours. */
1775 for (i = 0; i < 8; ++i)
1777 if (internal_ptr->u.syment._n._n_name[i] == '\0')
1779 break;
1780 } /* if end of string */
1781 } /* possible lengths of this string. */
1783 if ((newstring = (PTR) bfd_alloc (abfd, ++i)) == NULL)
1784 return (NULL);
1785 memset (newstring, 0, i);
1786 strncpy (newstring, internal_ptr->u.syment._n._n_name, i - 1);
1787 internal_ptr->u.syment._n._n_n._n_offset = (long int) newstring;
1788 internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1790 else if (internal_ptr->u.syment._n._n_n._n_offset == 0)
1791 internal_ptr->u.syment._n._n_n._n_offset = (long int) "";
1792 else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment))
1794 /* Long name already. Point symbol at the string in the
1795 table. */
1796 if (string_table == NULL)
1798 string_table = _bfd_coff_read_string_table (abfd);
1799 if (string_table == NULL)
1800 return NULL;
1802 internal_ptr->u.syment._n._n_n._n_offset =
1803 ((long int)
1804 (string_table
1805 + internal_ptr->u.syment._n._n_n._n_offset));
1807 else
1809 /* Long name in debug section. Very similar. */
1810 if (debug_section == NULL)
1811 debug_section = build_debug_section (abfd);
1812 internal_ptr->u.syment._n._n_n._n_offset = (long int)
1813 (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
1816 internal_ptr += internal_ptr->u.syment.n_numaux;
1819 obj_raw_syments (abfd) = internal;
1820 BFD_ASSERT (obj_raw_syment_count (abfd)
1821 == (unsigned int) (internal_ptr - internal));
1823 return (internal);
1824 } /* coff_get_normalized_symtab() */
1826 long
1827 coff_get_reloc_upper_bound (abfd, asect)
1828 bfd *abfd;
1829 sec_ptr asect;
1831 if (bfd_get_format (abfd) != bfd_object)
1833 bfd_set_error (bfd_error_invalid_operation);
1834 return -1;
1836 return (asect->reloc_count + 1) * sizeof (arelent *);
1839 asymbol *
1840 coff_make_empty_symbol (abfd)
1841 bfd *abfd;
1843 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, sizeof (coff_symbol_type));
1844 if (new == NULL)
1845 return (NULL);
1846 memset (new, 0, sizeof *new);
1847 new->symbol.section = 0;
1848 new->native = 0;
1849 new->lineno = (alent *) NULL;
1850 new->done_lineno = false;
1851 new->symbol.the_bfd = abfd;
1852 return &new->symbol;
1855 /* Make a debugging symbol. */
1857 asymbol *
1858 coff_bfd_make_debug_symbol (abfd, ptr, sz)
1859 bfd *abfd;
1860 PTR ptr;
1861 unsigned long sz;
1863 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, sizeof (coff_symbol_type));
1864 if (new == NULL)
1865 return (NULL);
1866 /* @@ The 10 is a guess at a plausible maximum number of aux entries
1867 (but shouldn't be a constant). */
1868 new->native = (combined_entry_type *) bfd_zalloc (abfd, sizeof (combined_entry_type) * 10);
1869 if (!new->native)
1870 return (NULL);
1871 new->symbol.section = bfd_abs_section_ptr;
1872 new->symbol.flags = BSF_DEBUGGING;
1873 new->lineno = (alent *) NULL;
1874 new->done_lineno = false;
1875 new->symbol.the_bfd = abfd;
1876 return &new->symbol;
1879 /*ARGSUSED */
1880 void
1881 coff_get_symbol_info (abfd, symbol, ret)
1882 bfd *abfd;
1883 asymbol *symbol;
1884 symbol_info *ret;
1886 bfd_symbol_info (symbol, ret);
1887 if (coffsymbol (symbol)->native != NULL
1888 && coffsymbol (symbol)->native->fix_value)
1890 combined_entry_type *psym;
1892 psym = ((combined_entry_type *)
1893 coffsymbol (symbol)->native->u.syment.n_value);
1894 ret->value = (bfd_vma) (psym - obj_raw_syments (abfd));
1898 /* Return the COFF syment for a symbol. */
1900 boolean
1901 bfd_coff_get_syment (abfd, symbol, psyment)
1902 bfd *abfd;
1903 asymbol *symbol;
1904 struct internal_syment *psyment;
1906 coff_symbol_type *csym;
1908 csym = coff_symbol_from (abfd, symbol);
1909 if (csym == NULL || csym->native == NULL)
1911 bfd_set_error (bfd_error_invalid_operation);
1912 return false;
1915 *psyment = csym->native->u.syment;
1917 if (csym->native->fix_value)
1918 psyment->n_value = ((combined_entry_type *) psyment->n_value
1919 - obj_raw_syments (abfd));
1921 /* FIXME: We should handle fix_line here. */
1923 return true;
1926 /* Return the COFF auxent for a symbol. */
1928 boolean
1929 bfd_coff_get_auxent (abfd, symbol, indx, pauxent)
1930 bfd *abfd;
1931 asymbol *symbol;
1932 int indx;
1933 union internal_auxent *pauxent;
1935 coff_symbol_type *csym;
1936 combined_entry_type *ent;
1938 csym = coff_symbol_from (abfd, symbol);
1940 if (csym == NULL
1941 || csym->native == NULL
1942 || indx >= csym->native->u.syment.n_numaux)
1944 bfd_set_error (bfd_error_invalid_operation);
1945 return false;
1948 ent = csym->native + indx + 1;
1950 *pauxent = ent->u.auxent;
1952 if (ent->fix_tag)
1953 pauxent->x_sym.x_tagndx.l =
1954 ((combined_entry_type *) pauxent->x_sym.x_tagndx.p
1955 - obj_raw_syments (abfd));
1957 if (ent->fix_end)
1958 pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l =
1959 ((combined_entry_type *) pauxent->x_sym.x_fcnary.x_fcn.x_endndx.p
1960 - obj_raw_syments (abfd));
1962 if (ent->fix_scnlen)
1963 pauxent->x_csect.x_scnlen.l =
1964 ((combined_entry_type *) pauxent->x_csect.x_scnlen.p
1965 - obj_raw_syments (abfd));
1967 return true;
1970 /* Print out information about COFF symbol. */
1972 void
1973 coff_print_symbol (abfd, filep, symbol, how)
1974 bfd *abfd;
1975 PTR filep;
1976 asymbol *symbol;
1977 bfd_print_symbol_type how;
1979 FILE *file = (FILE *) filep;
1981 switch (how)
1983 case bfd_print_symbol_name:
1984 fprintf (file, "%s", symbol->name);
1985 break;
1987 case bfd_print_symbol_more:
1988 fprintf (file, "coff %s %s",
1989 coffsymbol (symbol)->native ? "n" : "g",
1990 coffsymbol (symbol)->lineno ? "l" : " ");
1991 break;
1993 case bfd_print_symbol_all:
1994 if (coffsymbol (symbol)->native)
1996 unsigned long val;
1997 unsigned int aux;
1998 combined_entry_type *combined = coffsymbol (symbol)->native;
1999 combined_entry_type *root = obj_raw_syments (abfd);
2000 struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
2002 fprintf (file, "[%3ld]", (long) (combined - root));
2004 if (! combined->fix_value)
2005 val = (unsigned long) combined->u.syment.n_value;
2006 else
2007 val = ((unsigned long)
2008 ((combined_entry_type *) combined->u.syment.n_value
2009 - root));
2011 fprintf (file,
2012 "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x%08lx %s",
2013 combined->u.syment.n_scnum,
2014 combined->u.syment.n_flags,
2015 combined->u.syment.n_type,
2016 combined->u.syment.n_sclass,
2017 combined->u.syment.n_numaux,
2018 val,
2019 symbol->name);
2021 for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
2023 combined_entry_type *auxp = combined + aux + 1;
2024 long tagndx;
2026 if (auxp->fix_tag)
2027 tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
2028 else
2029 tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
2031 fprintf (file, "\n");
2033 if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux))
2034 continue;
2036 switch (combined->u.syment.n_sclass)
2038 case C_FILE:
2039 fprintf (file, "File ");
2040 break;
2042 case C_STAT:
2043 if (combined->u.syment.n_type == T_NULL)
2044 /* probably a section symbol? */
2046 fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
2047 (long) auxp->u.auxent.x_scn.x_scnlen,
2048 auxp->u.auxent.x_scn.x_nreloc,
2049 auxp->u.auxent.x_scn.x_nlinno);
2050 if (auxp->u.auxent.x_scn.x_checksum != 0
2051 || auxp->u.auxent.x_scn.x_associated != 0
2052 || auxp->u.auxent.x_scn.x_comdat != 0)
2053 fprintf (file, " checksum 0x%lx assoc %d comdat %d",
2054 auxp->u.auxent.x_scn.x_checksum,
2055 auxp->u.auxent.x_scn.x_associated,
2056 auxp->u.auxent.x_scn.x_comdat);
2057 break;
2059 /* else fall through */
2061 default:
2062 fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
2063 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
2064 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
2065 tagndx);
2066 if (auxp->fix_end)
2067 fprintf (file, " endndx %ld",
2068 ((long)
2069 (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2070 - root)));
2071 break;
2075 if (l)
2077 fprintf (file, "\n%s :", l->u.sym->name);
2078 l++;
2079 while (l->line_number)
2081 fprintf (file, "\n%4d : 0x%lx",
2082 l->line_number,
2083 ((unsigned long)
2084 (l->u.offset + symbol->section->vma)));
2085 l++;
2089 else
2091 bfd_print_symbol_vandf ((PTR) file, symbol);
2092 fprintf (file, " %-5s %s %s %s",
2093 symbol->section->name,
2094 coffsymbol (symbol)->native ? "n" : "g",
2095 coffsymbol (symbol)->lineno ? "l" : " ",
2096 symbol->name);
2101 /* Return whether a symbol name implies a local symbol. In COFF,
2102 local symbols generally start with ``.L''. Most targets use this
2103 function for the is_local_label_name entry point, but some may
2104 override it. */
2106 boolean
2107 _bfd_coff_is_local_label_name (abfd, name)
2108 bfd *abfd;
2109 const char *name;
2111 return name[0] == '.' && name[1] == 'L';
2114 /* Provided a BFD, a section and an offset into the section, calculate
2115 and return the name of the source file and the line nearest to the
2116 wanted location. */
2118 /*ARGSUSED*/
2119 boolean
2120 coff_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
2121 functionname_ptr, line_ptr)
2122 bfd *abfd;
2123 asection *section;
2124 asymbol **symbols;
2125 bfd_vma offset;
2126 CONST char **filename_ptr;
2127 CONST char **functionname_ptr;
2128 unsigned int *line_ptr;
2130 boolean found;
2131 unsigned int i;
2132 unsigned int line_base;
2133 coff_data_type *cof = coff_data (abfd);
2134 /* Run through the raw syments if available */
2135 combined_entry_type *p;
2136 combined_entry_type *pend;
2137 alent *l;
2138 struct coff_section_tdata *sec_data;
2140 /* Before looking through the symbol table, try to use a .stab
2141 section to find the information. */
2142 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
2143 &found, filename_ptr,
2144 functionname_ptr, line_ptr,
2145 &coff_data (abfd)->line_info))
2146 return false;
2147 if (found)
2148 return true;
2150 *filename_ptr = 0;
2151 *functionname_ptr = 0;
2152 *line_ptr = 0;
2154 /* Don't try and find line numbers in a non coff file */
2155 if (abfd->xvec->flavour != bfd_target_coff_flavour)
2156 return false;
2158 if (cof == NULL)
2159 return false;
2161 /* Find the first C_FILE symbol. */
2162 p = cof->raw_syments;
2163 if (!p)
2164 return false;
2166 pend = p + cof->raw_syment_count;
2167 while (p < pend)
2169 if (p->u.syment.n_sclass == C_FILE)
2170 break;
2171 p += 1 + p->u.syment.n_numaux;
2174 if (p < pend)
2176 bfd_vma sec_vma;
2177 bfd_vma maxdiff;
2179 /* Look through the C_FILE symbols to find the best one. */
2180 sec_vma = bfd_get_section_vma (abfd, section);
2181 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2182 maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
2183 while (1)
2185 combined_entry_type *p2;
2187 for (p2 = p + 1 + p->u.syment.n_numaux;
2188 p2 < pend;
2189 p2 += 1 + p2->u.syment.n_numaux)
2191 if (p2->u.syment.n_scnum > 0
2192 && (section
2193 == coff_section_from_bfd_index (abfd,
2194 p2->u.syment.n_scnum)))
2195 break;
2196 if (p2->u.syment.n_sclass == C_FILE)
2198 p2 = pend;
2199 break;
2203 if (p2 < pend
2204 && offset + sec_vma >= (bfd_vma) p2->u.syment.n_value
2205 && offset + sec_vma - (bfd_vma) p2->u.syment.n_value < maxdiff)
2207 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2208 maxdiff = offset + sec_vma - p2->u.syment.n_value;
2211 /* Avoid endless loops on erroneous files by ensuring that
2212 we always move forward in the file. */
2213 if (p - cof->raw_syments >= p->u.syment.n_value)
2214 break;
2216 p = cof->raw_syments + p->u.syment.n_value;
2217 if (p > pend || p->u.syment.n_sclass != C_FILE)
2218 break;
2222 /* Now wander though the raw linenumbers of the section */
2223 /* If we have been called on this section before, and the offset we
2224 want is further down then we can prime the lookup loop. */
2225 sec_data = coff_section_data (abfd, section);
2226 if (sec_data != NULL
2227 && sec_data->i > 0
2228 && offset >= sec_data->offset)
2230 i = sec_data->i;
2231 *functionname_ptr = sec_data->function;
2232 line_base = sec_data->line_base;
2234 else
2236 i = 0;
2237 line_base = 0;
2240 if (section->lineno != NULL)
2242 l = &section->lineno[i];
2244 for (; i < section->lineno_count; i++)
2246 if (l->line_number == 0)
2248 /* Get the symbol this line number points at */
2249 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
2250 if (coff->symbol.value > offset)
2251 break;
2252 *functionname_ptr = coff->symbol.name;
2253 if (coff->native)
2255 combined_entry_type *s = coff->native;
2256 s = s + 1 + s->u.syment.n_numaux;
2258 /* In XCOFF a debugging symbol can follow the
2259 function symbol. */
2260 if (s->u.syment.n_scnum == N_DEBUG)
2261 s = s + 1 + s->u.syment.n_numaux;
2263 /* S should now point to the .bf of the function. */
2264 if (s->u.syment.n_numaux)
2266 /* The linenumber is stored in the auxent. */
2267 union internal_auxent *a = &((s + 1)->u.auxent);
2268 line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
2269 *line_ptr = line_base;
2273 else
2275 if (l->u.offset > offset)
2276 break;
2277 *line_ptr = l->line_number + line_base - 1;
2279 l++;
2283 /* Cache the results for the next call. */
2284 if (sec_data == NULL && section->owner == abfd)
2286 section->used_by_bfd =
2287 ((PTR) bfd_zalloc (abfd,
2288 sizeof (struct coff_section_tdata)));
2289 sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2291 if (sec_data != NULL)
2293 sec_data->offset = offset;
2294 sec_data->i = i;
2295 sec_data->function = *functionname_ptr;
2296 sec_data->line_base = line_base;
2299 return true;
2303 coff_sizeof_headers (abfd, reloc)
2304 bfd *abfd;
2305 boolean reloc;
2307 size_t size;
2309 if (reloc == false)
2311 size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
2313 else
2315 size = bfd_coff_filhsz (abfd);
2318 size += abfd->section_count * bfd_coff_scnhsz (abfd);
2319 return size;
2322 /* Change the class of a coff symbol held by BFD. */
2323 boolean
2324 bfd_coff_set_symbol_class (abfd, symbol, class)
2325 bfd * abfd;
2326 asymbol * symbol;
2327 unsigned int class;
2329 coff_symbol_type * csym;
2331 csym = coff_symbol_from (abfd, symbol);
2332 if (csym == NULL)
2334 bfd_set_error (bfd_error_invalid_operation);
2335 return false;
2337 else if (csym->native == NULL)
2339 /* This is an alien symbol which no native coff backend data.
2340 We cheat here by creating a fake native entry for it and
2341 then filling in the class. This code is based on that in
2342 coff_write_alien_symbol(). */
2344 combined_entry_type * native;
2346 native = (combined_entry_type *) bfd_alloc (abfd, sizeof (* native));
2347 if (native == NULL)
2348 return false;
2350 memset (native, 0, sizeof (* native));
2352 native->u.syment.n_type = T_NULL;
2353 native->u.syment.n_sclass = class;
2355 if (bfd_is_und_section (symbol->section))
2357 native->u.syment.n_scnum = N_UNDEF;
2358 native->u.syment.n_value = symbol->value;
2360 else if (bfd_is_com_section (symbol->section))
2362 native->u.syment.n_scnum = N_UNDEF;
2363 native->u.syment.n_value = symbol->value;
2365 else
2367 native->u.syment.n_scnum =
2368 symbol->section->output_section->target_index;
2369 native->u.syment.n_value = (symbol->value
2370 + symbol->section->output_offset);
2371 if (! obj_pe (abfd))
2372 native->u.syment.n_value += symbol->section->output_section->vma;
2374 /* Copy the any flags from the the file header into the symbol.
2375 FIXME: Why? */
2376 native->u.syment.n_flags = bfd_asymbol_bfd (& csym->symbol)->flags;
2379 csym->native = native;
2381 else
2383 csym->native->u.syment.n_sclass = class;
2386 return true;