2007-07-26 Michael Snyder <msnyder@access-company.com>
[external/binutils.git] / bfd / coffgen.c
1 /* Support for the generic parts of COFF, for BFD.
2    Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3    2000, 2001, 2002, 2003, 2004, 2005, 2007
4    Free Software Foundation, Inc.
5    Written by Cygnus Support.
6
7    This file is part of BFD, the Binary File Descriptor library.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22    MA 02110-1301, USA.  */
23
24 /* Most of this hacked by  Steve Chamberlain, sac@cygnus.com.
25    Split out of coffcode.h by Ian Taylor, ian@cygnus.com.  */
26
27 /* This file contains COFF code that is not dependent on any
28    particular COFF target.  There is only one version of this file in
29    libbfd.a, so no target specific code may be put in here.  Or, to
30    put it another way,
31
32    ********** DO NOT PUT TARGET SPECIFIC CODE IN THIS FILE **********
33
34    If you need to add some target specific behaviour, add a new hook
35    function to bfd_coff_backend_data.
36
37    Some of these functions are also called by the ECOFF routines.
38    Those functions may not use any COFF specific information, such as
39    coff_data (abfd).  */
40
41 #include "sysdep.h"
42 #include "bfd.h"
43 #include "libbfd.h"
44 #include "coff/internal.h"
45 #include "libcoff.h"
46
47 /* Take a section header read from a coff file (in HOST byte order),
48    and make a BFD "section" out of it.  This is used by ECOFF.  */
49
50 static bfd_boolean
51 make_a_section_from_file (bfd *abfd,
52                           struct internal_scnhdr *hdr,
53                           unsigned int target_index)
54 {
55   asection *return_section;
56   char *name;
57   bfd_boolean result = TRUE;
58   flagword flags;
59
60   name = NULL;
61
62   /* Handle long section names as in PE.  */
63   if (bfd_coff_long_section_names (abfd)
64       && hdr->s_name[0] == '/')
65     {
66       char buf[SCNNMLEN];
67       long strindex;
68       char *p;
69       const char *strings;
70
71       memcpy (buf, hdr->s_name + 1, SCNNMLEN - 1);
72       buf[SCNNMLEN - 1] = '\0';
73       strindex = strtol (buf, &p, 10);
74       if (*p == '\0' && strindex >= 0)
75         {
76           strings = _bfd_coff_read_string_table (abfd);
77           if (strings == NULL)
78             return FALSE;
79           /* FIXME: For extra safety, we should make sure that
80              strindex does not run us past the end, but right now we
81              don't know the length of the string table.  */
82           strings += strindex;
83           name = bfd_alloc (abfd, (bfd_size_type) strlen (strings) + 1);
84           if (name == NULL)
85             return FALSE;
86           strcpy (name, strings);
87         }
88     }
89
90   if (name == NULL)
91     {
92       /* Assorted wastage to null-terminate the name, thanks AT&T! */
93       name = bfd_alloc (abfd, (bfd_size_type) sizeof (hdr->s_name) + 1);
94       if (name == NULL)
95         return FALSE;
96       strncpy (name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
97       name[sizeof (hdr->s_name)] = 0;
98     }
99
100   return_section = bfd_make_section_anyway (abfd, name);
101   if (return_section == NULL)
102     return FALSE;
103
104   return_section->vma = hdr->s_vaddr;
105   return_section->lma = hdr->s_paddr;
106   return_section->size = hdr->s_size;
107   return_section->filepos = hdr->s_scnptr;
108   return_section->rel_filepos = hdr->s_relptr;
109   return_section->reloc_count = hdr->s_nreloc;
110
111   bfd_coff_set_alignment_hook (abfd, return_section, hdr);
112
113   return_section->line_filepos = hdr->s_lnnoptr;
114
115   return_section->lineno_count = hdr->s_nlnno;
116   return_section->userdata = NULL;
117   return_section->next = NULL;
118   return_section->target_index = target_index;
119
120   if (! bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name, return_section,
121                                          & flags))
122     result = FALSE;
123
124   return_section->flags = flags;
125
126   /* At least on i386-coff, the line number count for a shared library
127      section must be ignored.  */
128   if ((return_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
129     return_section->lineno_count = 0;
130
131   if (hdr->s_nreloc != 0)
132     return_section->flags |= SEC_RELOC;
133   /* FIXME: should this check 'hdr->s_size > 0'.  */
134   if (hdr->s_scnptr != 0)
135     return_section->flags |= SEC_HAS_CONTENTS;
136
137   return result;
138 }
139
140 /* Read in a COFF object and make it into a BFD.  This is used by
141    ECOFF as well.  */
142
143 static const bfd_target *
144 coff_real_object_p (bfd *abfd,
145                     unsigned nscns,
146                     struct internal_filehdr *internal_f,
147                     struct internal_aouthdr *internal_a)
148 {
149   flagword oflags = abfd->flags;
150   bfd_vma ostart = bfd_get_start_address (abfd);
151   void * tdata;
152   void * tdata_save;
153   bfd_size_type readsize;       /* Length of file_info.  */
154   unsigned int scnhsz;
155   char *external_sections;
156
157   if (!(internal_f->f_flags & F_RELFLG))
158     abfd->flags |= HAS_RELOC;
159   if ((internal_f->f_flags & F_EXEC))
160     abfd->flags |= EXEC_P;
161   if (!(internal_f->f_flags & F_LNNO))
162     abfd->flags |= HAS_LINENO;
163   if (!(internal_f->f_flags & F_LSYMS))
164     abfd->flags |= HAS_LOCALS;
165
166   /* FIXME: How can we set D_PAGED correctly?  */
167   if ((internal_f->f_flags & F_EXEC) != 0)
168     abfd->flags |= D_PAGED;
169
170   bfd_get_symcount (abfd) = internal_f->f_nsyms;
171   if (internal_f->f_nsyms)
172     abfd->flags |= HAS_SYMS;
173
174   if (internal_a != (struct internal_aouthdr *) NULL)
175     bfd_get_start_address (abfd) = internal_a->entry;
176   else
177     bfd_get_start_address (abfd) = 0;
178
179   /* Set up the tdata area.  ECOFF uses its own routine, and overrides
180      abfd->flags.  */
181   tdata_save = abfd->tdata.any;
182   tdata = bfd_coff_mkobject_hook (abfd, (void *) internal_f, (void *) internal_a);
183   if (tdata == NULL)
184     goto fail2;
185
186   scnhsz = bfd_coff_scnhsz (abfd);
187   readsize = (bfd_size_type) nscns * scnhsz;
188   external_sections = bfd_alloc (abfd, readsize);
189   if (!external_sections)
190     goto fail;
191
192   if (bfd_bread ((void *) external_sections, readsize, abfd) != readsize)
193     goto fail;
194
195   /* Set the arch/mach *before* swapping in sections; section header swapping
196      may depend on arch/mach info.  */
197   if (! bfd_coff_set_arch_mach_hook (abfd, (void *) internal_f))
198     goto fail;
199
200   /* Now copy data as required; construct all asections etc.  */
201   if (nscns != 0)
202     {
203       unsigned int i;
204       for (i = 0; i < nscns; i++)
205         {
206           struct internal_scnhdr tmp;
207           bfd_coff_swap_scnhdr_in (abfd,
208                                    (void *) (external_sections + i * scnhsz),
209                                    (void *) & tmp);
210           if (! make_a_section_from_file (abfd, &tmp, i + 1))
211             goto fail;
212         }
213     }
214
215   return abfd->xvec;
216
217  fail:
218   bfd_release (abfd, tdata);
219  fail2:
220   abfd->tdata.any = tdata_save;
221   abfd->flags = oflags;
222   bfd_get_start_address (abfd) = ostart;
223   return (const bfd_target *) NULL;
224 }
225
226 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
227    not a COFF file.  This is also used by ECOFF.  */
228
229 const bfd_target *
230 coff_object_p (bfd *abfd)
231 {
232   bfd_size_type filhsz;
233   bfd_size_type aoutsz;
234   unsigned int nscns;
235   void * filehdr;
236   struct internal_filehdr internal_f;
237   struct internal_aouthdr internal_a;
238
239   /* Figure out how much to read.  */
240   filhsz = bfd_coff_filhsz (abfd);
241   aoutsz = bfd_coff_aoutsz (abfd);
242
243   filehdr = bfd_alloc (abfd, filhsz);
244   if (filehdr == NULL)
245     return NULL;
246   if (bfd_bread (filehdr, filhsz, abfd) != filhsz)
247     {
248       if (bfd_get_error () != bfd_error_system_call)
249         bfd_set_error (bfd_error_wrong_format);
250       bfd_release (abfd, filehdr);
251       return NULL;
252     }
253   bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f);
254   bfd_release (abfd, filehdr);
255
256   /* The XCOFF format has two sizes for the f_opthdr.  SMALL_AOUTSZ
257      (less than aoutsz) used in object files and AOUTSZ (equal to
258      aoutsz) in executables.  The bfd_coff_swap_aouthdr_in function
259      expects this header to be aoutsz bytes in length, so we use that
260      value in the call to bfd_alloc below.  But we must be careful to
261      only read in f_opthdr bytes in the call to bfd_bread.  We should
262      also attempt to catch corrupt or non-COFF binaries with a strange
263      value for f_opthdr.  */
264   if (! bfd_coff_bad_format_hook (abfd, &internal_f)
265       || internal_f.f_opthdr > aoutsz)
266     {
267       bfd_set_error (bfd_error_wrong_format);
268       return NULL;
269     }
270   nscns = internal_f.f_nscns;
271
272   if (internal_f.f_opthdr)
273     {
274       void * opthdr;
275
276       opthdr = bfd_alloc (abfd, aoutsz);
277       if (opthdr == NULL)
278         return NULL;
279       if (bfd_bread (opthdr, (bfd_size_type) internal_f.f_opthdr, abfd)
280           != internal_f.f_opthdr)
281         {
282           bfd_release (abfd, opthdr);
283           return NULL;
284         }
285       bfd_coff_swap_aouthdr_in (abfd, opthdr, (void *) &internal_a);
286       bfd_release (abfd, opthdr);
287     }
288
289   return coff_real_object_p (abfd, nscns, &internal_f,
290                              (internal_f.f_opthdr != 0
291                               ? &internal_a
292                               : (struct internal_aouthdr *) NULL));
293 }
294
295 /* Get the BFD section from a COFF symbol section number.  */
296
297 asection *
298 coff_section_from_bfd_index (bfd *abfd, int index)
299 {
300   struct bfd_section *answer = abfd->sections;
301
302   if (index == N_ABS)
303     return bfd_abs_section_ptr;
304   if (index == N_UNDEF)
305     return bfd_und_section_ptr;
306   if (index == N_DEBUG)
307     return bfd_abs_section_ptr;
308
309   while (answer)
310     {
311       if (answer->target_index == index)
312         return answer;
313       answer = answer->next;
314     }
315
316   /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
317      has a bad symbol table in biglitpow.o.  */
318   return bfd_und_section_ptr;
319 }
320
321 /* Get the upper bound of a COFF symbol table.  */
322
323 long
324 coff_get_symtab_upper_bound (bfd *abfd)
325 {
326   if (!bfd_coff_slurp_symbol_table (abfd))
327     return -1;
328
329   return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *));
330 }
331
332 /* Canonicalize a COFF symbol table.  */
333
334 long
335 coff_canonicalize_symtab (bfd *abfd, asymbol **alocation)
336 {
337   unsigned int counter;
338   coff_symbol_type *symbase;
339   coff_symbol_type **location = (coff_symbol_type **) alocation;
340
341   if (!bfd_coff_slurp_symbol_table (abfd))
342     return -1;
343
344   symbase = obj_symbols (abfd);
345   counter = bfd_get_symcount (abfd);
346   while (counter-- > 0)
347     *location++ = symbase++;
348
349   *location = NULL;
350
351   return bfd_get_symcount (abfd);
352 }
353
354 /* Get the name of a symbol.  The caller must pass in a buffer of size
355    >= SYMNMLEN + 1.  */
356
357 const char *
358 _bfd_coff_internal_syment_name (bfd *abfd,
359                                 const struct internal_syment *sym,
360                                 char *buf)
361 {
362   /* FIXME: It's not clear this will work correctly if sizeof
363      (_n_zeroes) != 4.  */
364   if (sym->_n._n_n._n_zeroes != 0
365       || sym->_n._n_n._n_offset == 0)
366     {
367       memcpy (buf, sym->_n._n_name, SYMNMLEN);
368       buf[SYMNMLEN] = '\0';
369       return buf;
370     }
371   else
372     {
373       const char *strings;
374
375       BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE);
376       strings = obj_coff_strings (abfd);
377       if (strings == NULL)
378         {
379           strings = _bfd_coff_read_string_table (abfd);
380           if (strings == NULL)
381             return NULL;
382         }
383       return strings + sym->_n._n_n._n_offset;
384     }
385 }
386
387 /* Read in and swap the relocs.  This returns a buffer holding the
388    relocs for section SEC in file ABFD.  If CACHE is TRUE and
389    INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
390    the function is called again.  If EXTERNAL_RELOCS is not NULL, it
391    is a buffer large enough to hold the unswapped relocs.  If
392    INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
393    the swapped relocs.  If REQUIRE_INTERNAL is TRUE, then the return
394    value must be INTERNAL_RELOCS.  The function returns NULL on error.  */
395
396 struct internal_reloc *
397 _bfd_coff_read_internal_relocs (bfd *abfd,
398                                 asection *sec,
399                                 bfd_boolean cache,
400                                 bfd_byte *external_relocs,
401                                 bfd_boolean require_internal,
402                                 struct internal_reloc *internal_relocs)
403 {
404   bfd_size_type relsz;
405   bfd_byte *free_external = NULL;
406   struct internal_reloc *free_internal = NULL;
407   bfd_byte *erel;
408   bfd_byte *erel_end;
409   struct internal_reloc *irel;
410   bfd_size_type amt;
411
412   if (coff_section_data (abfd, sec) != NULL
413       && coff_section_data (abfd, sec)->relocs != NULL)
414     {
415       if (! require_internal)
416         return coff_section_data (abfd, sec)->relocs;
417       memcpy (internal_relocs, coff_section_data (abfd, sec)->relocs,
418               sec->reloc_count * sizeof (struct internal_reloc));
419       return internal_relocs;
420     }
421
422   relsz = bfd_coff_relsz (abfd);
423
424   amt = sec->reloc_count * relsz;
425   if (external_relocs == NULL)
426     {
427       free_external = bfd_malloc (amt);
428       if (free_external == NULL && sec->reloc_count > 0)
429         goto error_return;
430       external_relocs = free_external;
431     }
432
433   if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
434       || bfd_bread (external_relocs, amt, abfd) != amt)
435     goto error_return;
436
437   if (internal_relocs == NULL)
438     {
439       amt = sec->reloc_count;
440       amt *= sizeof (struct internal_reloc);
441       free_internal = bfd_malloc (amt);
442       if (free_internal == NULL && sec->reloc_count > 0)
443         goto error_return;
444       internal_relocs = free_internal;
445     }
446
447   /* Swap in the relocs.  */
448   erel = external_relocs;
449   erel_end = erel + relsz * sec->reloc_count;
450   irel = internal_relocs;
451   for (; erel < erel_end; erel += relsz, irel++)
452     bfd_coff_swap_reloc_in (abfd, (void *) erel, (void *) irel);
453
454   if (free_external != NULL)
455     {
456       free (free_external);
457       free_external = NULL;
458     }
459
460   if (free_internal != NULL)
461     {
462       if (cache)
463         free (free_internal);
464       else
465         {
466           if (coff_section_data (abfd, sec) == NULL)
467             {
468               amt = sizeof (struct coff_section_tdata);
469               sec->used_by_bfd = bfd_zalloc (abfd, amt);
470               if (sec->used_by_bfd == NULL)
471                 goto error_return;
472               coff_section_data (abfd, sec)->contents = NULL;
473             }
474           coff_section_data (abfd, sec)->relocs = free_internal;
475         }
476     }
477
478   return internal_relocs;
479
480  error_return:
481   if (free_external != NULL)
482     free (free_external);
483   if (free_internal != NULL)
484     free (free_internal);
485   return NULL;
486 }
487
488 /* Set lineno_count for the output sections of a COFF file.  */
489
490 int
491 coff_count_linenumbers (bfd *abfd)
492 {
493   unsigned int limit = bfd_get_symcount (abfd);
494   unsigned int i;
495   int total = 0;
496   asymbol **p;
497   asection *s;
498
499   if (limit == 0)
500     {
501       /* This may be from the backend linker, in which case the
502          lineno_count in the sections is correct.  */
503       for (s = abfd->sections; s != NULL; s = s->next)
504         total += s->lineno_count;
505       return total;
506     }
507
508   for (s = abfd->sections; s != NULL; s = s->next)
509     BFD_ASSERT (s->lineno_count == 0);
510
511   for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
512     {
513       asymbol *q_maybe = *p;
514
515       if (bfd_family_coff (bfd_asymbol_bfd (q_maybe)))
516         {
517           coff_symbol_type *q = coffsymbol (q_maybe);
518
519           /* The AIX 4.1 compiler can sometimes generate line numbers
520              attached to debugging symbols.  We try to simply ignore
521              those here.  */
522           if (q->lineno != NULL
523               && q->symbol.section->owner != NULL)
524             {
525               /* This symbol has line numbers.  Increment the owning
526                  section's linenumber count.  */
527               alent *l = q->lineno;
528
529               do
530                 {
531                   asection * sec = q->symbol.section->output_section;
532
533                   /* Do not try to update fields in read-only sections.  */
534                   if (! bfd_is_const_section (sec))
535                     sec->lineno_count ++;
536
537                   ++total;
538                   ++l;
539                 }
540               while (l->line_number != 0);
541             }
542         }
543     }
544
545   return total;
546 }
547
548 /* Takes a bfd and a symbol, returns a pointer to the coff specific
549    area of the symbol if there is one.  */
550
551 coff_symbol_type *
552 coff_symbol_from (bfd *ignore_abfd ATTRIBUTE_UNUSED,
553                   asymbol *symbol)
554 {
555   if (!bfd_family_coff (bfd_asymbol_bfd (symbol)))
556     return (coff_symbol_type *) NULL;
557
558   if (bfd_asymbol_bfd (symbol)->tdata.coff_obj_data == (coff_data_type *) NULL)
559     return (coff_symbol_type *) NULL;
560
561   return (coff_symbol_type *) symbol;
562 }
563
564 static void
565 fixup_symbol_value (bfd *abfd,
566                     coff_symbol_type *coff_symbol_ptr,
567                     struct internal_syment *syment)
568 {
569   /* Normalize the symbol flags.  */
570   if (coff_symbol_ptr->symbol.section 
571       && bfd_is_com_section (coff_symbol_ptr->symbol.section))
572     {
573       /* A common symbol is undefined with a value.  */
574       syment->n_scnum = N_UNDEF;
575       syment->n_value = coff_symbol_ptr->symbol.value;
576     }
577   else if ((coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) != 0
578            && (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING_RELOC) == 0)
579     {
580       syment->n_value = coff_symbol_ptr->symbol.value;
581     }
582   else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
583     {
584       syment->n_scnum = N_UNDEF;
585       syment->n_value = 0;
586     }
587   /* FIXME: Do we need to handle the absolute section here?  */
588   else
589     {
590       if (coff_symbol_ptr->symbol.section)
591         {
592           syment->n_scnum =
593             coff_symbol_ptr->symbol.section->output_section->target_index;
594
595           syment->n_value = (coff_symbol_ptr->symbol.value
596                              + coff_symbol_ptr->symbol.section->output_offset);
597           if (! obj_pe (abfd))
598             {
599               syment->n_value += (syment->n_sclass == C_STATLAB)
600                 ? coff_symbol_ptr->symbol.section->output_section->lma
601                 : coff_symbol_ptr->symbol.section->output_section->vma;
602             }
603         }
604       else
605         {
606           BFD_ASSERT (0);
607           /* This can happen, but I don't know why yet (steve@cygnus.com) */
608           syment->n_scnum = N_ABS;
609           syment->n_value = coff_symbol_ptr->symbol.value;
610         }
611     }
612 }
613
614 /* Run through all the symbols in the symbol table and work out what
615    their indexes into the symbol table will be when output.
616
617    Coff requires that each C_FILE symbol points to the next one in the
618    chain, and that the last one points to the first external symbol. We
619    do that here too.  */
620
621 bfd_boolean
622 coff_renumber_symbols (bfd *bfd_ptr, int *first_undef)
623 {
624   unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
625   asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
626   unsigned int native_index = 0;
627   struct internal_syment *last_file = NULL;
628   unsigned int symbol_index;
629
630   /* COFF demands that undefined symbols come after all other symbols.
631      Since we don't need to impose this extra knowledge on all our
632      client programs, deal with that here.  Sort the symbol table;
633      just move the undefined symbols to the end, leaving the rest
634      alone.  The O'Reilly book says that defined global symbols come
635      at the end before the undefined symbols, so we do that here as
636      well.  */
637   /* @@ Do we have some condition we could test for, so we don't always
638      have to do this?  I don't think relocatability is quite right, but
639      I'm not certain.  [raeburn:19920508.1711EST]  */
640   {
641     asymbol **newsyms;
642     unsigned int i;
643     bfd_size_type amt;
644
645     amt = sizeof (asymbol *) * ((bfd_size_type) symbol_count + 1);
646     newsyms = bfd_alloc (bfd_ptr, amt);
647     if (!newsyms)
648       return FALSE;
649     bfd_ptr->outsymbols = newsyms;
650     for (i = 0; i < symbol_count; i++)
651       if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
652           || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
653               && !bfd_is_com_section (symbol_ptr_ptr[i]->section)
654               && ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) != 0
655                   || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
656                       == 0))))
657         *newsyms++ = symbol_ptr_ptr[i];
658
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];
667
668     *first_undef = newsyms - bfd_ptr->outsymbols;
669
670     for (i = 0; i < symbol_count; i++)
671       if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
672           && bfd_is_und_section (symbol_ptr_ptr[i]->section))
673         *newsyms++ = symbol_ptr_ptr[i];
674     *newsyms = (asymbol *) NULL;
675     symbol_ptr_ptr = bfd_ptr->outsymbols;
676   }
677
678   for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
679     {
680       coff_symbol_type *coff_symbol_ptr = coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
681       symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
682       if (coff_symbol_ptr && coff_symbol_ptr->native)
683         {
684           combined_entry_type *s = coff_symbol_ptr->native;
685           int i;
686
687           if (s->u.syment.n_sclass == C_FILE)
688             {
689               if (last_file != NULL)
690                 last_file->n_value = native_index;
691               last_file = &(s->u.syment);
692             }
693           else
694             /* Modify the symbol values according to their section and
695                type.  */
696             fixup_symbol_value (bfd_ptr, coff_symbol_ptr, &(s->u.syment));
697
698           for (i = 0; i < s->u.syment.n_numaux + 1; i++)
699             s[i].offset = native_index++;
700         }
701       else
702         native_index++;
703     }
704
705   obj_conv_table_size (bfd_ptr) = native_index;
706
707   return TRUE;
708 }
709
710 /* Run thorough the symbol table again, and fix it so that all
711    pointers to entries are changed to the entries' index in the output
712    symbol table.  */
713
714 void
715 coff_mangle_symbols (bfd *bfd_ptr)
716 {
717   unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
718   asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
719   unsigned int symbol_index;
720
721   for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
722     {
723       coff_symbol_type *coff_symbol_ptr =
724       coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
725
726       if (coff_symbol_ptr && coff_symbol_ptr->native)
727         {
728           int i;
729           combined_entry_type *s = coff_symbol_ptr->native;
730
731           if (s->fix_value)
732             {
733               /* FIXME: We should use a union here.  */
734               s->u.syment.n_value =
735                 (bfd_hostptr_t) ((combined_entry_type *)
736                           ((bfd_hostptr_t) s->u.syment.n_value))->offset;
737               s->fix_value = 0;
738             }
739           if (s->fix_line)
740             {
741               /* The value is the offset into the line number entries
742                  for the symbol's section.  On output, the symbol's
743                  section should be N_DEBUG.  */
744               s->u.syment.n_value =
745                 (coff_symbol_ptr->symbol.section->output_section->line_filepos
746                  + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr));
747               coff_symbol_ptr->symbol.section =
748                 coff_section_from_bfd_index (bfd_ptr, N_DEBUG);
749               BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
750             }
751           for (i = 0; i < s->u.syment.n_numaux; i++)
752             {
753               combined_entry_type *a = s + i + 1;
754               if (a->fix_tag)
755                 {
756                   a->u.auxent.x_sym.x_tagndx.l =
757                     a->u.auxent.x_sym.x_tagndx.p->offset;
758                   a->fix_tag = 0;
759                 }
760               if (a->fix_end)
761                 {
762                   a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
763                     a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
764                   a->fix_end = 0;
765                 }
766               if (a->fix_scnlen)
767                 {
768                   a->u.auxent.x_csect.x_scnlen.l =
769                     a->u.auxent.x_csect.x_scnlen.p->offset;
770                   a->fix_scnlen = 0;
771                 }
772             }
773         }
774     }
775 }
776
777 static void
778 coff_fix_symbol_name (bfd *abfd,
779                       asymbol *symbol,
780                       combined_entry_type *native,
781                       bfd_size_type *string_size_p,
782                       asection **debug_string_section_p,
783                       bfd_size_type *debug_string_size_p)
784 {
785   unsigned int name_length;
786   union internal_auxent *auxent;
787   char *name = (char *) (symbol->name);
788
789   if (name == NULL)
790     {
791       /* COFF symbols always have names, so we'll make one up.  */
792       symbol->name = "strange";
793       name = (char *) symbol->name;
794     }
795   name_length = strlen (name);
796
797   if (native->u.syment.n_sclass == C_FILE
798       && native->u.syment.n_numaux > 0)
799     {
800       unsigned int filnmlen;
801
802       if (bfd_coff_force_symnames_in_strings (abfd))
803         {
804           native->u.syment._n._n_n._n_offset =
805               (*string_size_p + STRING_SIZE_SIZE);
806           native->u.syment._n._n_n._n_zeroes = 0;
807           *string_size_p += 6;  /* strlen(".file") + 1 */
808         }
809       else
810         strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
811
812       auxent = &(native + 1)->u.auxent;
813
814       filnmlen = bfd_coff_filnmlen (abfd);
815
816       if (bfd_coff_long_filenames (abfd))
817         {
818           if (name_length <= filnmlen)
819             strncpy (auxent->x_file.x_fname, name, filnmlen);
820           else
821             {
822               auxent->x_file.x_n.x_offset = *string_size_p + STRING_SIZE_SIZE;
823               auxent->x_file.x_n.x_zeroes = 0;
824               *string_size_p += name_length + 1;
825             }
826         }
827       else
828         {
829           strncpy (auxent->x_file.x_fname, name, filnmlen);
830           if (name_length > filnmlen)
831             name[filnmlen] = '\0';
832         }
833     }
834   else
835     {
836       if (name_length <= SYMNMLEN && !bfd_coff_force_symnames_in_strings (abfd))
837         /* This name will fit into the symbol neatly.  */
838         strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
839
840       else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
841         {
842           native->u.syment._n._n_n._n_offset = (*string_size_p
843                                                 + STRING_SIZE_SIZE);
844           native->u.syment._n._n_n._n_zeroes = 0;
845           *string_size_p += name_length + 1;
846         }
847       else
848         {
849           file_ptr filepos;
850           bfd_byte buf[4];
851           int prefix_len = bfd_coff_debug_string_prefix_length (abfd);
852
853           /* This name should be written into the .debug section.  For
854              some reason each name is preceded by a two byte length
855              and also followed by a null byte.  FIXME: We assume that
856              the .debug section has already been created, and that it
857              is large enough.  */
858           if (*debug_string_section_p == (asection *) NULL)
859             *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
860           filepos = bfd_tell (abfd);
861           if (prefix_len == 4)
862             bfd_put_32 (abfd, (bfd_vma) (name_length + 1), buf);
863           else
864             bfd_put_16 (abfd, (bfd_vma) (name_length + 1), buf);
865
866           if (!bfd_set_section_contents (abfd,
867                                          *debug_string_section_p,
868                                          (void *) buf,
869                                          (file_ptr) *debug_string_size_p,
870                                          (bfd_size_type) prefix_len)
871               || !bfd_set_section_contents (abfd,
872                                             *debug_string_section_p,
873                                             (void *) symbol->name,
874                                             (file_ptr) (*debug_string_size_p
875                                                         + prefix_len),
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 =
881               *debug_string_size_p + prefix_len;
882           native->u.syment._n._n_n._n_zeroes = 0;
883           *debug_string_size_p += name_length + 1 + prefix_len;
884         }
885     }
886 }
887
888 /* We need to keep track of the symbol index so that when we write out
889    the relocs we can get the index for a symbol.  This method is a
890    hack.  FIXME.  */
891
892 #define set_index(symbol, idx)  ((symbol)->udata.i = (idx))
893
894 /* Write a symbol out to a COFF file.  */
895
896 static bfd_boolean
897 coff_write_symbol (bfd *abfd,
898                    asymbol *symbol,
899                    combined_entry_type *native,
900                    bfd_vma *written,
901                    bfd_size_type *string_size_p,
902                    asection **debug_string_section_p,
903                    bfd_size_type *debug_string_size_p)
904 {
905   unsigned int numaux = native->u.syment.n_numaux;
906   int type = native->u.syment.n_type;
907   int class = native->u.syment.n_sclass;
908   void * buf;
909   bfd_size_type symesz;
910
911   if (native->u.syment.n_sclass == C_FILE)
912     symbol->flags |= BSF_DEBUGGING;
913
914   if (symbol->flags & BSF_DEBUGGING
915       && bfd_is_abs_section (symbol->section))
916     native->u.syment.n_scnum = N_DEBUG;
917
918   else if (bfd_is_abs_section (symbol->section))
919     native->u.syment.n_scnum = N_ABS;
920
921   else if (bfd_is_und_section (symbol->section))
922     native->u.syment.n_scnum = N_UNDEF;
923
924   else
925     native->u.syment.n_scnum =
926       symbol->section->output_section->target_index;
927
928   coff_fix_symbol_name (abfd, symbol, native, string_size_p,
929                         debug_string_section_p, debug_string_size_p);
930
931   symesz = bfd_coff_symesz (abfd);
932   buf = bfd_alloc (abfd, symesz);
933   if (!buf)
934     return FALSE;
935   bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
936   if (bfd_bwrite (buf, symesz, abfd) != symesz)
937     return FALSE;
938   bfd_release (abfd, buf);
939
940   if (native->u.syment.n_numaux > 0)
941     {
942       bfd_size_type auxesz;
943       unsigned int j;
944
945       auxesz = bfd_coff_auxesz (abfd);
946       buf = bfd_alloc (abfd, auxesz);
947       if (!buf)
948         return FALSE;
949       for (j = 0; j < native->u.syment.n_numaux; j++)
950         {
951           bfd_coff_swap_aux_out (abfd,
952                                  &((native + j + 1)->u.auxent),
953                                  type, class, (int) j,
954                                  native->u.syment.n_numaux,
955                                  buf);
956           if (bfd_bwrite (buf, auxesz, abfd) != auxesz)
957             return FALSE;
958         }
959       bfd_release (abfd, buf);
960     }
961
962   /* Store the index for use when we write out the relocs.  */
963   set_index (symbol, *written);
964
965   *written += numaux + 1;
966   return TRUE;
967 }
968
969 /* Write out a symbol to a COFF file that does not come from a COFF
970    file originally.  This symbol may have been created by the linker,
971    or we may be linking a non COFF file to a COFF file.  */
972
973 static bfd_boolean
974 coff_write_alien_symbol (bfd *abfd,
975                          asymbol *symbol,
976                          bfd_vma *written,
977                          bfd_size_type *string_size_p,
978                          asection **debug_string_section_p,
979                          bfd_size_type *debug_string_size_p)
980 {
981   combined_entry_type *native;
982   combined_entry_type dummy;
983
984   native = &dummy;
985   native->u.syment.n_type = T_NULL;
986   native->u.syment.n_flags = 0;
987   if (bfd_is_und_section (symbol->section))
988     {
989       native->u.syment.n_scnum = N_UNDEF;
990       native->u.syment.n_value = symbol->value;
991     }
992   else if (bfd_is_com_section (symbol->section))
993     {
994       native->u.syment.n_scnum = N_UNDEF;
995       native->u.syment.n_value = symbol->value;
996     }
997   else if (symbol->flags & BSF_DEBUGGING)
998     {
999       /* There isn't much point to writing out a debugging symbol
1000          unless we are prepared to convert it into COFF debugging
1001          format.  So, we just ignore them.  We must clobber the symbol
1002          name to keep it from being put in the string table.  */
1003       symbol->name = "";
1004       return TRUE;
1005     }
1006   else
1007     {
1008       native->u.syment.n_scnum =
1009         symbol->section->output_section->target_index;
1010       native->u.syment.n_value = (symbol->value
1011                                   + symbol->section->output_offset);
1012       if (! obj_pe (abfd))
1013         native->u.syment.n_value += symbol->section->output_section->vma;
1014
1015       /* Copy the any flags from the file header into the symbol.
1016          FIXME: Why?  */
1017       {
1018         coff_symbol_type *c = coff_symbol_from (abfd, symbol);
1019         if (c != (coff_symbol_type *) NULL)
1020           native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
1021       }
1022     }
1023
1024   native->u.syment.n_type = 0;
1025   if (symbol->flags & BSF_LOCAL)
1026     native->u.syment.n_sclass = C_STAT;
1027   else if (symbol->flags & BSF_WEAK)
1028     native->u.syment.n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1029   else
1030     native->u.syment.n_sclass = C_EXT;
1031   native->u.syment.n_numaux = 0;
1032
1033   return coff_write_symbol (abfd, symbol, native, written, string_size_p,
1034                             debug_string_section_p, debug_string_size_p);
1035 }
1036
1037 /* Write a native symbol to a COFF file.  */
1038
1039 static bfd_boolean
1040 coff_write_native_symbol (bfd *abfd,
1041                           coff_symbol_type *symbol,
1042                           bfd_vma *written,
1043                           bfd_size_type *string_size_p,
1044                           asection **debug_string_section_p,
1045                           bfd_size_type *debug_string_size_p)
1046 {
1047   combined_entry_type *native = symbol->native;
1048   alent *lineno = symbol->lineno;
1049
1050   /* If this symbol has an associated line number, we must store the
1051      symbol index in the line number field.  We also tag the auxent to
1052      point to the right place in the lineno table.  */
1053   if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
1054     {
1055       unsigned int count = 0;
1056
1057       lineno[count].u.offset = *written;
1058       if (native->u.syment.n_numaux)
1059         {
1060           union internal_auxent *a = &((native + 1)->u.auxent);
1061
1062           a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
1063             symbol->symbol.section->output_section->moving_line_filepos;
1064         }
1065
1066       /* Count and relocate all other linenumbers.  */
1067       count++;
1068       while (lineno[count].line_number != 0)
1069         {
1070           lineno[count].u.offset +=
1071             (symbol->symbol.section->output_section->vma
1072              + symbol->symbol.section->output_offset);
1073           count++;
1074         }
1075       symbol->done_lineno = TRUE;
1076
1077       if (! bfd_is_const_section (symbol->symbol.section->output_section))
1078         symbol->symbol.section->output_section->moving_line_filepos +=
1079           count * bfd_coff_linesz (abfd);
1080     }
1081
1082   return coff_write_symbol (abfd, &(symbol->symbol), native, written,
1083                             string_size_p, debug_string_section_p,
1084                             debug_string_size_p);
1085 }
1086
1087 /* Write out the COFF symbols.  */
1088
1089 bfd_boolean
1090 coff_write_symbols (bfd *abfd)
1091 {
1092   bfd_size_type string_size;
1093   asection *debug_string_section;
1094   bfd_size_type debug_string_size;
1095   unsigned int i;
1096   unsigned int limit = bfd_get_symcount (abfd);
1097   bfd_vma written = 0;
1098   asymbol **p;
1099
1100   string_size = 0;
1101   debug_string_section = NULL;
1102   debug_string_size = 0;
1103
1104   /* If this target supports long section names, they must be put into
1105      the string table.  This is supported by PE.  This code must
1106      handle section names just as they are handled in
1107      coff_write_object_contents.  */
1108   if (bfd_coff_long_section_names (abfd))
1109     {
1110       asection *o;
1111
1112       for (o = abfd->sections; o != NULL; o = o->next)
1113         {
1114           size_t len;
1115
1116           len = strlen (o->name);
1117           if (len > SCNNMLEN)
1118             string_size += len + 1;
1119         }
1120     }
1121
1122   /* Seek to the right place.  */
1123   if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1124     return FALSE;
1125
1126   /* Output all the symbols we have.  */
1127   written = 0;
1128   for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
1129     {
1130       asymbol *symbol = *p;
1131       coff_symbol_type *c_symbol = coff_symbol_from (abfd, symbol);
1132
1133       if (c_symbol == (coff_symbol_type *) NULL
1134           || c_symbol->native == (combined_entry_type *) NULL)
1135         {
1136           if (!coff_write_alien_symbol (abfd, symbol, &written, &string_size,
1137                                         &debug_string_section,
1138                                         &debug_string_size))
1139             return FALSE;
1140         }
1141       else
1142         {
1143           if (!coff_write_native_symbol (abfd, c_symbol, &written,
1144                                          &string_size, &debug_string_section,
1145                                          &debug_string_size))
1146             return FALSE;
1147         }
1148     }
1149
1150   obj_raw_syment_count (abfd) = written;
1151
1152   /* Now write out strings.  */
1153   if (string_size != 0)
1154     {
1155       unsigned int size = string_size + STRING_SIZE_SIZE;
1156       bfd_byte buffer[STRING_SIZE_SIZE];
1157
1158 #if STRING_SIZE_SIZE == 4
1159       H_PUT_32 (abfd, size, buffer);
1160 #else
1161  #error Change H_PUT_32
1162 #endif
1163       if (bfd_bwrite ((void *) buffer, (bfd_size_type) sizeof (buffer), abfd)
1164           != sizeof (buffer))
1165         return FALSE;
1166
1167       /* Handle long section names.  This code must handle section
1168          names just as they are handled in coff_write_object_contents.  */
1169       if (bfd_coff_long_section_names (abfd))
1170         {
1171           asection *o;
1172
1173           for (o = abfd->sections; o != NULL; o = o->next)
1174             {
1175               size_t len;
1176
1177               len = strlen (o->name);
1178               if (len > SCNNMLEN)
1179                 {
1180                   if (bfd_bwrite (o->name, (bfd_size_type) (len + 1), abfd)
1181                       != len + 1)
1182                     return FALSE;
1183                 }
1184             }
1185         }
1186
1187       for (p = abfd->outsymbols, i = 0;
1188            i < limit;
1189            i++, p++)
1190         {
1191           asymbol *q = *p;
1192           size_t name_length = strlen (q->name);
1193           coff_symbol_type *c_symbol = coff_symbol_from (abfd, q);
1194           size_t maxlen;
1195
1196           /* Figure out whether the symbol name should go in the string
1197              table.  Symbol names that are short enough are stored
1198              directly in the syment structure.  File names permit a
1199              different, longer, length in the syment structure.  On
1200              XCOFF, some symbol names are stored in the .debug section
1201              rather than in the string table.  */
1202
1203           if (c_symbol == NULL
1204               || c_symbol->native == NULL)
1205             /* This is not a COFF symbol, so it certainly is not a
1206                file name, nor does it go in the .debug section.  */
1207             maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
1208
1209           else if (bfd_coff_symname_in_debug (abfd,
1210                                               &c_symbol->native->u.syment))
1211             /* This symbol name is in the XCOFF .debug section.
1212                Don't write it into the string table.  */
1213             maxlen = name_length;
1214
1215           else if (c_symbol->native->u.syment.n_sclass == C_FILE
1216                    && c_symbol->native->u.syment.n_numaux > 0)
1217             {
1218               if (bfd_coff_force_symnames_in_strings (abfd))
1219                 {
1220                   if (bfd_bwrite (".file", (bfd_size_type) 6, abfd) != 6)
1221                     return FALSE;
1222                 }
1223               maxlen = bfd_coff_filnmlen (abfd);
1224             }
1225           else
1226             maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
1227
1228           if (name_length > maxlen)
1229             {
1230               if (bfd_bwrite ((void *) (q->name), (bfd_size_type) name_length + 1,
1231                              abfd) != name_length + 1)
1232                 return FALSE;
1233             }
1234         }
1235     }
1236   else
1237     {
1238       /* We would normally not write anything here, but we'll write
1239          out 4 so that any stupid coff reader which tries to read the
1240          string table even when there isn't one won't croak.  */
1241       unsigned int size = STRING_SIZE_SIZE;
1242       bfd_byte buffer[STRING_SIZE_SIZE];
1243
1244 #if STRING_SIZE_SIZE == 4
1245       H_PUT_32 (abfd, size, buffer);
1246 #else
1247  #error Change H_PUT_32
1248 #endif
1249       if (bfd_bwrite ((void *) buffer, (bfd_size_type) STRING_SIZE_SIZE, abfd)
1250           != STRING_SIZE_SIZE)
1251         return FALSE;
1252     }
1253
1254   /* Make sure the .debug section was created to be the correct size.
1255      We should create it ourselves on the fly, but we don't because
1256      BFD won't let us write to any section until we know how large all
1257      the sections are.  We could still do it by making another pass
1258      over the symbols.  FIXME.  */
1259   BFD_ASSERT (debug_string_size == 0
1260               || (debug_string_section != (asection *) NULL
1261                   && (BFD_ALIGN (debug_string_size,
1262                                  1 << debug_string_section->alignment_power)
1263                       == debug_string_section->size)));
1264
1265   return TRUE;
1266 }
1267
1268 bfd_boolean
1269 coff_write_linenumbers (bfd *abfd)
1270 {
1271   asection *s;
1272   bfd_size_type linesz;
1273   void * buff;
1274
1275   linesz = bfd_coff_linesz (abfd);
1276   buff = bfd_alloc (abfd, linesz);
1277   if (!buff)
1278     return FALSE;
1279   for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1280     {
1281       if (s->lineno_count)
1282         {
1283           asymbol **q = abfd->outsymbols;
1284           if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
1285             return FALSE;
1286           /* Find all the linenumbers in this section.  */
1287           while (*q)
1288             {
1289               asymbol *p = *q;
1290               if (p->section->output_section == s)
1291                 {
1292                   alent *l =
1293                   BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
1294                             (bfd_asymbol_bfd (p), p));
1295                   if (l)
1296                     {
1297                       /* Found a linenumber entry, output.  */
1298                       struct internal_lineno out;
1299                       memset ((void *) & out, 0, sizeof (out));
1300                       out.l_lnno = 0;
1301                       out.l_addr.l_symndx = l->u.offset;
1302                       bfd_coff_swap_lineno_out (abfd, &out, buff);
1303                       if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd)
1304                           != linesz)
1305                         return FALSE;
1306                       l++;
1307                       while (l->line_number)
1308                         {
1309                           out.l_lnno = l->line_number;
1310                           out.l_addr.l_symndx = l->u.offset;
1311                           bfd_coff_swap_lineno_out (abfd, &out, buff);
1312                           if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd)
1313                               != linesz)
1314                             return FALSE;
1315                           l++;
1316                         }
1317                     }
1318                 }
1319               q++;
1320             }
1321         }
1322     }
1323   bfd_release (abfd, buff);
1324   return TRUE;
1325 }
1326
1327 alent *
1328 coff_get_lineno (bfd *ignore_abfd ATTRIBUTE_UNUSED, asymbol *symbol)
1329 {
1330   return coffsymbol (symbol)->lineno;
1331 }
1332
1333 /* This function transforms the offsets into the symbol table into
1334    pointers to syments.  */
1335
1336 static void
1337 coff_pointerize_aux (bfd *abfd,
1338                      combined_entry_type *table_base,
1339                      combined_entry_type *symbol,
1340                      unsigned int indaux,
1341                      combined_entry_type *auxent)
1342 {
1343   unsigned int type = symbol->u.syment.n_type;
1344   unsigned int class = symbol->u.syment.n_sclass;
1345
1346   if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1347     {
1348       if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1349           (abfd, table_base, symbol, indaux, auxent))
1350         return;
1351     }
1352
1353   /* Don't bother if this is a file or a section.  */
1354   if (class == C_STAT && type == T_NULL)
1355     return;
1356   if (class == C_FILE)
1357     return;
1358
1359   /* Otherwise patch up.  */
1360 #define N_TMASK coff_data  (abfd)->local_n_tmask
1361 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1362   
1363   if ((ISFCN (type) || ISTAG (class) || class == C_BLOCK || class == C_FCN)
1364       && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l > 0)
1365     {
1366       auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
1367         table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
1368       auxent->fix_end = 1;
1369     }
1370   /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1371      generate one, so we must be careful to ignore it.  */
1372   if (auxent->u.auxent.x_sym.x_tagndx.l > 0)
1373     {
1374       auxent->u.auxent.x_sym.x_tagndx.p =
1375         table_base + auxent->u.auxent.x_sym.x_tagndx.l;
1376       auxent->fix_tag = 1;
1377     }
1378 }
1379
1380 /* Allocate space for the ".debug" section, and read it.
1381    We did not read the debug section until now, because
1382    we didn't want to go to the trouble until someone needed it.  */
1383
1384 static char *
1385 build_debug_section (bfd *abfd)
1386 {
1387   char *debug_section;
1388   file_ptr position;
1389   bfd_size_type sec_size;
1390
1391   asection *sect = bfd_get_section_by_name (abfd, ".debug");
1392
1393   if (!sect)
1394     {
1395       bfd_set_error (bfd_error_no_debug_section);
1396       return NULL;
1397     }
1398
1399   sec_size = sect->size;
1400   debug_section = bfd_alloc (abfd, sec_size);
1401   if (debug_section == NULL)
1402     return NULL;
1403
1404   /* Seek to the beginning of the `.debug' section and read it.
1405      Save the current position first; it is needed by our caller.
1406      Then read debug section and reset the file pointer.  */
1407
1408   position = bfd_tell (abfd);
1409   if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0
1410       || bfd_bread (debug_section, sec_size, abfd) != sec_size
1411       || bfd_seek (abfd, position, SEEK_SET) != 0)
1412     return NULL;
1413   return debug_section;
1414 }
1415
1416 /* Return a pointer to a malloc'd copy of 'name'.  'name' may not be
1417    \0-terminated, but will not exceed 'maxlen' characters.  The copy *will*
1418    be \0-terminated.  */
1419
1420 static char *
1421 copy_name (bfd *abfd, char *name, size_t maxlen)
1422 {
1423   size_t len;
1424   char *newname;
1425
1426   for (len = 0; len < maxlen; ++len)
1427     if (name[len] == '\0')
1428       break;
1429
1430   if ((newname = bfd_alloc (abfd, (bfd_size_type) len + 1)) == NULL)
1431     return NULL;
1432
1433   strncpy (newname, name, len);
1434   newname[len] = '\0';
1435   return newname;
1436 }
1437
1438 /* Read in the external symbols.  */
1439
1440 bfd_boolean
1441 _bfd_coff_get_external_symbols (bfd *abfd)
1442 {
1443   bfd_size_type symesz;
1444   bfd_size_type size;
1445   void * syms;
1446
1447   if (obj_coff_external_syms (abfd) != NULL)
1448     return TRUE;
1449
1450   symesz = bfd_coff_symesz (abfd);
1451
1452   size = obj_raw_syment_count (abfd) * symesz;
1453   if (size == 0)
1454     return TRUE;
1455
1456   syms = bfd_malloc (size);
1457   if (syms == NULL)
1458     return FALSE;
1459
1460   if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
1461       || bfd_bread (syms, size, abfd) != size)
1462     {
1463       if (syms != NULL)
1464         free (syms);
1465       return FALSE;
1466     }
1467
1468   obj_coff_external_syms (abfd) = syms;
1469
1470   return TRUE;
1471 }
1472
1473 /* Read in the external strings.  The strings are not loaded until
1474    they are needed.  This is because we have no simple way of
1475    detecting a missing string table in an archive.  */
1476
1477 const char *
1478 _bfd_coff_read_string_table (bfd *abfd)
1479 {
1480   char extstrsize[STRING_SIZE_SIZE];
1481   bfd_size_type strsize;
1482   char *strings;
1483   file_ptr pos;
1484
1485   if (obj_coff_strings (abfd) != NULL)
1486     return obj_coff_strings (abfd);
1487
1488   if (obj_sym_filepos (abfd) == 0)
1489     {
1490       bfd_set_error (bfd_error_no_symbols);
1491       return NULL;
1492     }
1493
1494   pos = obj_sym_filepos (abfd);
1495   pos += obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd);
1496   if (bfd_seek (abfd, pos, SEEK_SET) != 0)
1497     return NULL;
1498
1499   if (bfd_bread (extstrsize, (bfd_size_type) sizeof extstrsize, abfd)
1500       != sizeof extstrsize)
1501     {
1502       if (bfd_get_error () != bfd_error_file_truncated)
1503         return NULL;
1504
1505       /* There is no string table.  */
1506       strsize = STRING_SIZE_SIZE;
1507     }
1508   else
1509     {
1510 #if STRING_SIZE_SIZE == 4
1511       strsize = H_GET_32 (abfd, extstrsize);
1512 #else
1513  #error Change H_GET_32
1514 #endif
1515     }
1516
1517   if (strsize < STRING_SIZE_SIZE)
1518     {
1519       (*_bfd_error_handler)
1520         (_("%B: bad string table size %lu"), abfd, (unsigned long) strsize);
1521       bfd_set_error (bfd_error_bad_value);
1522       return NULL;
1523     }
1524
1525   strings = bfd_malloc (strsize);
1526   if (strings == NULL)
1527     return NULL;
1528
1529   if (bfd_bread (strings + STRING_SIZE_SIZE, strsize - STRING_SIZE_SIZE, abfd)
1530       != strsize - STRING_SIZE_SIZE)
1531     {
1532       free (strings);
1533       return NULL;
1534     }
1535
1536   obj_coff_strings (abfd) = strings;
1537
1538   return strings;
1539 }
1540
1541 /* Free up the external symbols and strings read from a COFF file.  */
1542
1543 bfd_boolean
1544 _bfd_coff_free_symbols (bfd *abfd)
1545 {
1546   if (obj_coff_external_syms (abfd) != NULL
1547       && ! obj_coff_keep_syms (abfd))
1548     {
1549       free (obj_coff_external_syms (abfd));
1550       obj_coff_external_syms (abfd) = NULL;
1551     }
1552   if (obj_coff_strings (abfd) != NULL
1553       && ! obj_coff_keep_strings (abfd))
1554     {
1555       free (obj_coff_strings (abfd));
1556       obj_coff_strings (abfd) = NULL;
1557     }
1558   return TRUE;
1559 }
1560
1561 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1562    knit the symbol names into a normalized form.  By normalized here I
1563    mean that all symbols have an n_offset pointer that points to a null-
1564    terminated string.  */
1565
1566 combined_entry_type *
1567 coff_get_normalized_symtab (bfd *abfd)
1568 {
1569   combined_entry_type *internal;
1570   combined_entry_type *internal_ptr;
1571   combined_entry_type *symbol_ptr;
1572   combined_entry_type *internal_end;
1573   size_t symesz;
1574   char *raw_src;
1575   char *raw_end;
1576   const char *string_table = NULL;
1577   char *debug_section = NULL;
1578   bfd_size_type size;
1579
1580   if (obj_raw_syments (abfd) != NULL)
1581     return obj_raw_syments (abfd);
1582
1583   size = obj_raw_syment_count (abfd) * sizeof (combined_entry_type);
1584   internal = bfd_zalloc (abfd, size);
1585   if (internal == NULL && size != 0)
1586     return NULL;
1587   internal_end = internal + obj_raw_syment_count (abfd);
1588
1589   if (! _bfd_coff_get_external_symbols (abfd))
1590     return NULL;
1591
1592   raw_src = (char *) obj_coff_external_syms (abfd);
1593
1594   /* Mark the end of the symbols.  */
1595   symesz = bfd_coff_symesz (abfd);
1596   raw_end = (char *) raw_src + obj_raw_syment_count (abfd) * symesz;
1597
1598   /* FIXME SOMEDAY.  A string table size of zero is very weird, but
1599      probably possible.  If one shows up, it will probably kill us.  */
1600
1601   /* Swap all the raw entries.  */
1602   for (internal_ptr = internal;
1603        raw_src < raw_end;
1604        raw_src += symesz, internal_ptr++)
1605     {
1606
1607       unsigned int i;
1608       bfd_coff_swap_sym_in (abfd, (void *) raw_src,
1609                             (void *) & internal_ptr->u.syment);
1610       symbol_ptr = internal_ptr;
1611
1612       for (i = 0;
1613            i < symbol_ptr->u.syment.n_numaux;
1614            i++)
1615         {
1616           internal_ptr++;
1617           raw_src += symesz;
1618           bfd_coff_swap_aux_in (abfd, (void *) raw_src,
1619                                 symbol_ptr->u.syment.n_type,
1620                                 symbol_ptr->u.syment.n_sclass,
1621                                 (int) i, symbol_ptr->u.syment.n_numaux,
1622                                 &(internal_ptr->u.auxent));
1623           coff_pointerize_aux (abfd, internal, symbol_ptr, i,
1624                                internal_ptr);
1625         }
1626     }
1627
1628   /* Free the raw symbols, but not the strings (if we have them).  */
1629   obj_coff_keep_strings (abfd) = TRUE;
1630   if (! _bfd_coff_free_symbols (abfd))
1631     return NULL;
1632
1633   for (internal_ptr = internal; internal_ptr < internal_end;
1634        internal_ptr++)
1635     {
1636       if (internal_ptr->u.syment.n_sclass == C_FILE
1637           && internal_ptr->u.syment.n_numaux > 0)
1638         {
1639           /* Make a file symbol point to the name in the auxent, since
1640              the text ".file" is redundant.  */
1641           if ((internal_ptr + 1)->u.auxent.x_file.x_n.x_zeroes == 0)
1642             {
1643               /* The filename is a long one, point into the string table.  */
1644               if (string_table == NULL)
1645                 {
1646                   string_table = _bfd_coff_read_string_table (abfd);
1647                   if (string_table == NULL)
1648                     return NULL;
1649                 }
1650
1651               internal_ptr->u.syment._n._n_n._n_offset =
1652                 ((bfd_hostptr_t)
1653                  (string_table
1654                   + (internal_ptr + 1)->u.auxent.x_file.x_n.x_offset));
1655             }
1656           else
1657             {
1658               /* Ordinary short filename, put into memory anyway.  The
1659                  Microsoft PE tools sometimes store a filename in
1660                  multiple AUX entries.  */
1661               if (internal_ptr->u.syment.n_numaux > 1
1662                   && coff_data (abfd)->pe)
1663                 internal_ptr->u.syment._n._n_n._n_offset =
1664                   ((bfd_hostptr_t)
1665                    copy_name (abfd,
1666                               (internal_ptr + 1)->u.auxent.x_file.x_fname,
1667                               internal_ptr->u.syment.n_numaux * symesz));
1668               else
1669                 internal_ptr->u.syment._n._n_n._n_offset =
1670                   ((bfd_hostptr_t)
1671                    copy_name (abfd,
1672                               (internal_ptr + 1)->u.auxent.x_file.x_fname,
1673                               (size_t) bfd_coff_filnmlen (abfd)));
1674             }
1675         }
1676       else
1677         {
1678           if (internal_ptr->u.syment._n._n_n._n_zeroes != 0)
1679             {
1680               /* This is a "short" name.  Make it long.  */
1681               size_t i;
1682               char *newstring;
1683
1684               /* Find the length of this string without walking into memory
1685                  that isn't ours.  */
1686               for (i = 0; i < 8; ++i)
1687                 if (internal_ptr->u.syment._n._n_name[i] == '\0')
1688                   break;
1689
1690               newstring = bfd_zalloc (abfd, (bfd_size_type) (i + 1));
1691               if (newstring == NULL)
1692                 return NULL;
1693               strncpy (newstring, internal_ptr->u.syment._n._n_name, i);
1694               internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t) newstring;
1695               internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1696             }
1697           else if (internal_ptr->u.syment._n._n_n._n_offset == 0)
1698             internal_ptr->u.syment._n._n_n._n_offset = (bfd_vma) "";
1699           else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment))
1700             {
1701               /* Long name already.  Point symbol at the string in the
1702                  table.  */
1703               if (string_table == NULL)
1704                 {
1705                   string_table = _bfd_coff_read_string_table (abfd);
1706                   if (string_table == NULL)
1707                     return NULL;
1708                 }
1709               internal_ptr->u.syment._n._n_n._n_offset =
1710                 ((bfd_hostptr_t)
1711                  (string_table
1712                   + internal_ptr->u.syment._n._n_n._n_offset));
1713             }
1714           else
1715             {
1716               /* Long name in debug section.  Very similar.  */
1717               if (debug_section == NULL)
1718                 debug_section = build_debug_section (abfd);
1719               internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t)
1720                 (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
1721             }
1722         }
1723       internal_ptr += internal_ptr->u.syment.n_numaux;
1724     }
1725
1726   obj_raw_syments (abfd) = internal;
1727   BFD_ASSERT (obj_raw_syment_count (abfd)
1728               == (unsigned int) (internal_ptr - internal));
1729
1730   return internal;
1731 }
1732
1733 long
1734 coff_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
1735 {
1736   if (bfd_get_format (abfd) != bfd_object)
1737     {
1738       bfd_set_error (bfd_error_invalid_operation);
1739       return -1;
1740     }
1741   return (asect->reloc_count + 1) * sizeof (arelent *);
1742 }
1743
1744 asymbol *
1745 coff_make_empty_symbol (bfd *abfd)
1746 {
1747   bfd_size_type amt = sizeof (coff_symbol_type);
1748   coff_symbol_type *new = bfd_zalloc (abfd, amt);
1749
1750   if (new == NULL)
1751     return NULL;
1752   new->symbol.section = 0;
1753   new->native = 0;
1754   new->lineno = NULL;
1755   new->done_lineno = FALSE;
1756   new->symbol.the_bfd = abfd;
1757
1758   return & new->symbol;
1759 }
1760
1761 /* Make a debugging symbol.  */
1762
1763 asymbol *
1764 coff_bfd_make_debug_symbol (bfd *abfd,
1765                             void * ptr ATTRIBUTE_UNUSED,
1766                             unsigned long sz ATTRIBUTE_UNUSED)
1767 {
1768   bfd_size_type amt = sizeof (coff_symbol_type);
1769   coff_symbol_type *new = bfd_alloc (abfd, amt);
1770
1771   if (new == NULL)
1772     return NULL;
1773   /* @@ The 10 is a guess at a plausible maximum number of aux entries
1774      (but shouldn't be a constant).  */
1775   amt = sizeof (combined_entry_type) * 10;
1776   new->native = bfd_zalloc (abfd, amt);
1777   if (!new->native)
1778     return NULL;
1779   new->symbol.section = bfd_abs_section_ptr;
1780   new->symbol.flags = BSF_DEBUGGING;
1781   new->lineno = NULL;
1782   new->done_lineno = FALSE;
1783   new->symbol.the_bfd = abfd;
1784   
1785   return & new->symbol;
1786 }
1787
1788 void
1789 coff_get_symbol_info (bfd *abfd, asymbol *symbol, symbol_info *ret)
1790 {
1791   bfd_symbol_info (symbol, ret);
1792
1793   if (coffsymbol (symbol)->native != NULL
1794       && coffsymbol (symbol)->native->fix_value)
1795     ret->value = coffsymbol (symbol)->native->u.syment.n_value -
1796       (bfd_hostptr_t) obj_raw_syments (abfd);
1797 }
1798
1799 /* Return the COFF syment for a symbol.  */
1800
1801 bfd_boolean
1802 bfd_coff_get_syment (bfd *abfd,
1803                      asymbol *symbol,
1804                      struct internal_syment *psyment)
1805 {
1806   coff_symbol_type *csym;
1807
1808   csym = coff_symbol_from (abfd, symbol);
1809   if (csym == NULL || csym->native == NULL)
1810     {
1811       bfd_set_error (bfd_error_invalid_operation);
1812       return FALSE;
1813     }
1814
1815   *psyment = csym->native->u.syment;
1816
1817   if (csym->native->fix_value)
1818     psyment->n_value = psyment->n_value -
1819       (bfd_hostptr_t) obj_raw_syments (abfd);
1820
1821   /* FIXME: We should handle fix_line here.  */
1822
1823   return TRUE;
1824 }
1825
1826 /* Return the COFF auxent for a symbol.  */
1827
1828 bfd_boolean
1829 bfd_coff_get_auxent (bfd *abfd,
1830                      asymbol *symbol,
1831                      int indx,
1832                      union internal_auxent *pauxent)
1833 {
1834   coff_symbol_type *csym;
1835   combined_entry_type *ent;
1836
1837   csym = coff_symbol_from (abfd, symbol);
1838
1839   if (csym == NULL
1840       || csym->native == NULL
1841       || indx >= csym->native->u.syment.n_numaux)
1842     {
1843       bfd_set_error (bfd_error_invalid_operation);
1844       return FALSE;
1845     }
1846
1847   ent = csym->native + indx + 1;
1848
1849   *pauxent = ent->u.auxent;
1850
1851   if (ent->fix_tag)
1852     pauxent->x_sym.x_tagndx.l =
1853       ((combined_entry_type *) pauxent->x_sym.x_tagndx.p
1854        - obj_raw_syments (abfd));
1855
1856   if (ent->fix_end)
1857     pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l =
1858       ((combined_entry_type *) pauxent->x_sym.x_fcnary.x_fcn.x_endndx.p
1859        - obj_raw_syments (abfd));
1860
1861   if (ent->fix_scnlen)
1862     pauxent->x_csect.x_scnlen.l =
1863       ((combined_entry_type *) pauxent->x_csect.x_scnlen.p
1864        - obj_raw_syments (abfd));
1865
1866   return TRUE;
1867 }
1868
1869 /* Print out information about COFF symbol.  */
1870
1871 void
1872 coff_print_symbol (bfd *abfd,
1873                    void * filep,
1874                    asymbol *symbol,
1875                    bfd_print_symbol_type how)
1876 {
1877   FILE * file = (FILE *) filep;
1878
1879   switch (how)
1880     {
1881     case bfd_print_symbol_name:
1882       fprintf (file, "%s", symbol->name);
1883       break;
1884
1885     case bfd_print_symbol_more:
1886       fprintf (file, "coff %s %s",
1887                coffsymbol (symbol)->native ? "n" : "g",
1888                coffsymbol (symbol)->lineno ? "l" : " ");
1889       break;
1890
1891     case bfd_print_symbol_all:
1892       if (coffsymbol (symbol)->native)
1893         {
1894           bfd_vma val;
1895           unsigned int aux;
1896           combined_entry_type *combined = coffsymbol (symbol)->native;
1897           combined_entry_type *root = obj_raw_syments (abfd);
1898           struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
1899
1900           fprintf (file, "[%3ld]", (long) (combined - root));
1901
1902           if (! combined->fix_value)
1903             val = (bfd_vma) combined->u.syment.n_value;
1904           else
1905             val = combined->u.syment.n_value - (bfd_hostptr_t) root;
1906
1907           fprintf (file, "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x",
1908                    combined->u.syment.n_scnum,
1909                    combined->u.syment.n_flags,
1910                    combined->u.syment.n_type,
1911                    combined->u.syment.n_sclass,
1912                    combined->u.syment.n_numaux);
1913 #ifdef BFD64
1914           /* fprintf_vma() on a 64-bit enabled host will always print a 64-bit
1915              value, but really we want to display the address in the target's
1916              address size.  Since we do not have a field in the bfd structure
1917              to tell us this, we take a guess, based on the target's name.  */
1918           if (strstr (bfd_get_target (abfd), "64") == NULL)
1919             fprintf (file, "%08lx", (unsigned long) (val & 0xffffffff));
1920           else
1921 #endif
1922             fprintf_vma (file, val);
1923           fprintf (file, " %s", symbol->name);
1924
1925           for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
1926             {
1927               combined_entry_type *auxp = combined + aux + 1;
1928               long tagndx;
1929
1930               if (auxp->fix_tag)
1931                 tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
1932               else
1933                 tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
1934
1935               fprintf (file, "\n");
1936
1937               if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux))
1938                 continue;
1939
1940               switch (combined->u.syment.n_sclass)
1941                 {
1942                 case C_FILE:
1943                   fprintf (file, "File ");
1944                   break;
1945
1946                 case C_STAT:
1947                   if (combined->u.syment.n_type == T_NULL)
1948                     /* Probably a section symbol ?  */
1949                     {
1950                       fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
1951                                (long) auxp->u.auxent.x_scn.x_scnlen,
1952                                auxp->u.auxent.x_scn.x_nreloc,
1953                                auxp->u.auxent.x_scn.x_nlinno);
1954                       if (auxp->u.auxent.x_scn.x_checksum != 0
1955                           || auxp->u.auxent.x_scn.x_associated != 0
1956                           || auxp->u.auxent.x_scn.x_comdat != 0)
1957                         fprintf (file, " checksum 0x%lx assoc %d comdat %d",
1958                                  auxp->u.auxent.x_scn.x_checksum,
1959                                  auxp->u.auxent.x_scn.x_associated,
1960                                  auxp->u.auxent.x_scn.x_comdat);
1961                       break;
1962                     }
1963                     /* Otherwise fall through.  */
1964                 case C_EXT:
1965                   if (ISFCN (combined->u.syment.n_type))
1966                     {
1967                       long next, llnos;
1968
1969                       if (auxp->fix_end)
1970                         next = (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
1971                                - root);
1972                       else
1973                         next = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
1974                       llnos = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_lnnoptr;
1975                       fprintf (file,
1976                                "AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld",
1977                                tagndx, auxp->u.auxent.x_sym.x_misc.x_fsize,
1978                                llnos, next);
1979                       break;
1980                     }
1981                   /* Otherwise fall through.  */
1982                 default:
1983                   fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
1984                            auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
1985                            auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
1986                            tagndx);
1987                   if (auxp->fix_end)
1988                     fprintf (file, " endndx %ld",
1989                              ((long)
1990                               (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
1991                                - root)));
1992                   break;
1993                 }
1994             }
1995
1996           if (l)
1997             {
1998               fprintf (file, "\n%s :", l->u.sym->name);
1999               l++;
2000               while (l->line_number)
2001                 {
2002                   fprintf (file, "\n%4d : ", l->line_number);
2003                   fprintf_vma (file, l->u.offset + symbol->section->vma);
2004                   l++;
2005                 }
2006             }
2007         }
2008       else
2009         {
2010           bfd_print_symbol_vandf (abfd, (void *) file, symbol);
2011           fprintf (file, " %-5s %s %s %s",
2012                    symbol->section->name,
2013                    coffsymbol (symbol)->native ? "n" : "g",
2014                    coffsymbol (symbol)->lineno ? "l" : " ",
2015                    symbol->name);
2016         }
2017     }
2018 }
2019
2020 /* Return whether a symbol name implies a local symbol.  In COFF,
2021    local symbols generally start with ``.L''.  Most targets use this
2022    function for the is_local_label_name entry point, but some may
2023    override it.  */
2024
2025 bfd_boolean
2026 _bfd_coff_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
2027                                const char *name)
2028 {
2029   return name[0] == '.' && name[1] == 'L';
2030 }
2031
2032 /* Provided a BFD, a section and an offset (in bytes, not octets) into the
2033    section, calculate and return the name of the source file and the line
2034    nearest to the wanted location.  */
2035
2036 bfd_boolean
2037 coff_find_nearest_line (bfd *abfd,
2038                         asection *section,
2039                         asymbol **symbols,
2040                         bfd_vma offset,
2041                         const char **filename_ptr,
2042                         const char **functionname_ptr,
2043                         unsigned int *line_ptr)
2044 {
2045   bfd_boolean found;
2046   unsigned int i;
2047   unsigned int line_base;
2048   coff_data_type *cof = coff_data (abfd);
2049   /* Run through the raw syments if available.  */
2050   combined_entry_type *p;
2051   combined_entry_type *pend;
2052   alent *l;
2053   struct coff_section_tdata *sec_data;
2054   bfd_size_type amt;
2055
2056   /* Before looking through the symbol table, try to use a .stab
2057      section to find the information.  */
2058   if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
2059                                              &found, filename_ptr,
2060                                              functionname_ptr, line_ptr,
2061                                              &coff_data(abfd)->line_info))
2062     return FALSE;
2063
2064   if (found)
2065     return TRUE;
2066
2067   /* Also try examining DWARF2 debugging information.  */
2068   if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
2069                                      filename_ptr, functionname_ptr,
2070                                      line_ptr, 0,
2071                                      &coff_data(abfd)->dwarf2_find_line_info))
2072     return TRUE;
2073
2074   *filename_ptr = 0;
2075   *functionname_ptr = 0;
2076   *line_ptr = 0;
2077
2078   /* Don't try and find line numbers in a non coff file.  */
2079   if (!bfd_family_coff (abfd))
2080     return FALSE;
2081
2082   if (cof == NULL)
2083     return FALSE;
2084
2085   /* Find the first C_FILE symbol.  */
2086   p = cof->raw_syments;
2087   if (!p)
2088     return FALSE;
2089
2090   pend = p + cof->raw_syment_count;
2091   while (p < pend)
2092     {
2093       if (p->u.syment.n_sclass == C_FILE)
2094         break;
2095       p += 1 + p->u.syment.n_numaux;
2096     }
2097
2098   if (p < pend)
2099     {
2100       bfd_vma sec_vma;
2101       bfd_vma maxdiff;
2102
2103       /* Look through the C_FILE symbols to find the best one.  */
2104       sec_vma = bfd_get_section_vma (abfd, section);
2105       *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2106       maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
2107       while (1)
2108         {
2109           combined_entry_type *p2;
2110
2111           for (p2 = p + 1 + p->u.syment.n_numaux;
2112                p2 < pend;
2113                p2 += 1 + p2->u.syment.n_numaux)
2114             {
2115               if (p2->u.syment.n_scnum > 0
2116                   && (section
2117                       == coff_section_from_bfd_index (abfd,
2118                                                       p2->u.syment.n_scnum)))
2119                 break;
2120               if (p2->u.syment.n_sclass == C_FILE)
2121                 {
2122                   p2 = pend;
2123                   break;
2124                 }
2125             }
2126
2127           /* We use <= MAXDIFF here so that if we get a zero length
2128              file, we actually use the next file entry.  */
2129           if (p2 < pend
2130               && offset + sec_vma >= (bfd_vma) p2->u.syment.n_value
2131               && offset + sec_vma - (bfd_vma) p2->u.syment.n_value <= maxdiff)
2132             {
2133               *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2134               maxdiff = offset + sec_vma - p2->u.syment.n_value;
2135             }
2136
2137           /* Avoid endless loops on erroneous files by ensuring that
2138              we always move forward in the file.  */
2139           if (p >= cof->raw_syments + p->u.syment.n_value)
2140             break;
2141
2142           p = cof->raw_syments + p->u.syment.n_value;
2143           if (p > pend || p->u.syment.n_sclass != C_FILE)
2144             break;
2145         }
2146     }
2147
2148   /* Now wander though the raw linenumbers of the section.  */
2149   /* If we have been called on this section before, and th. e offset we
2150      want is further down then we can prime the lookup loop.  */
2151   sec_data = coff_section_data (abfd, section);
2152   if (sec_data != NULL
2153       && sec_data->i > 0
2154       && offset >= sec_data->offset)
2155     {
2156       i = sec_data->i;
2157       *functionname_ptr = sec_data->function;
2158       line_base = sec_data->line_base;
2159     }
2160   else
2161     {
2162       i = 0;
2163       line_base = 0;
2164     }
2165
2166   if (section->lineno != NULL)
2167     {
2168       bfd_vma last_value = 0;
2169
2170       l = &section->lineno[i];
2171
2172       for (; i < section->lineno_count; i++)
2173         {
2174           if (l->line_number == 0)
2175             {
2176               /* Get the symbol this line number points at.  */
2177               coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
2178               if (coff->symbol.value > offset)
2179                 break;
2180               *functionname_ptr = coff->symbol.name;
2181               last_value = coff->symbol.value;
2182               if (coff->native)
2183                 {
2184                   combined_entry_type *s = coff->native;
2185                   s = s + 1 + s->u.syment.n_numaux;
2186
2187                   /* In XCOFF a debugging symbol can follow the
2188                      function symbol.  */
2189                   if (s->u.syment.n_scnum == N_DEBUG)
2190                     s = s + 1 + s->u.syment.n_numaux;
2191
2192                   /* S should now point to the .bf of the function.  */
2193                   if (s->u.syment.n_numaux)
2194                     {
2195                       /* The linenumber is stored in the auxent.  */
2196                       union internal_auxent *a = &((s + 1)->u.auxent);
2197                       line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
2198                       *line_ptr = line_base;
2199                     }
2200                 }
2201             }
2202           else
2203             {
2204               if (l->u.offset > offset)
2205                 break;
2206               *line_ptr = l->line_number + line_base - 1;
2207             }
2208           l++;
2209         }
2210
2211       /* If we fell off the end of the loop, then assume that this
2212          symbol has no line number info.  Otherwise, symbols with no
2213          line number info get reported with the line number of the
2214          last line of the last symbol which does have line number
2215          info.  We use 0x100 as a slop to account for cases where the
2216          last line has executable code.  */
2217       if (i >= section->lineno_count
2218           && last_value != 0
2219           && offset - last_value > 0x100)
2220         {
2221           *functionname_ptr = NULL;
2222           *line_ptr = 0;
2223         }
2224     }
2225
2226   /* Cache the results for the next call.  */
2227   if (sec_data == NULL && section->owner == abfd)
2228     {
2229       amt = sizeof (struct coff_section_tdata);
2230       section->used_by_bfd = bfd_zalloc (abfd, amt);
2231       sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2232     }
2233   if (sec_data != NULL)
2234     {
2235       sec_data->offset = offset;
2236       sec_data->i = i;
2237       sec_data->function = *functionname_ptr;
2238       sec_data->line_base = line_base;
2239     }
2240
2241   return TRUE;
2242 }
2243
2244 bfd_boolean
2245 coff_find_inliner_info (bfd *abfd,
2246                         const char **filename_ptr,
2247                         const char **functionname_ptr,
2248                         unsigned int *line_ptr)
2249 {
2250   bfd_boolean found;
2251
2252   found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
2253                                          functionname_ptr, line_ptr,
2254                                          &coff_data(abfd)->dwarf2_find_line_info);
2255   return (found);
2256 }
2257
2258 int
2259 coff_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
2260 {
2261   size_t size;
2262
2263   if (!info->relocatable)
2264     size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
2265   else
2266     size = bfd_coff_filhsz (abfd);
2267
2268   size += abfd->section_count * bfd_coff_scnhsz (abfd);
2269   return size;
2270 }
2271
2272 /* Change the class of a coff symbol held by BFD.  */
2273
2274 bfd_boolean
2275 bfd_coff_set_symbol_class (bfd *         abfd,
2276                            asymbol *     symbol,
2277                            unsigned int  class)
2278 {
2279   coff_symbol_type * csym;
2280
2281   csym = coff_symbol_from (abfd, symbol);
2282   if (csym == NULL)
2283     {
2284       bfd_set_error (bfd_error_invalid_operation);
2285       return FALSE;
2286     }
2287   else if (csym->native == NULL)
2288     {
2289       /* This is an alien symbol which no native coff backend data.
2290          We cheat here by creating a fake native entry for it and
2291          then filling in the class.  This code is based on that in
2292          coff_write_alien_symbol().  */
2293
2294       combined_entry_type * native;
2295       bfd_size_type amt = sizeof (* native);
2296
2297       native = bfd_zalloc (abfd, amt);
2298       if (native == NULL)
2299         return FALSE;
2300
2301       native->u.syment.n_type   = T_NULL;
2302       native->u.syment.n_sclass = class;
2303
2304       if (bfd_is_und_section (symbol->section))
2305         {
2306           native->u.syment.n_scnum = N_UNDEF;
2307           native->u.syment.n_value = symbol->value;
2308         }
2309       else if (bfd_is_com_section (symbol->section))
2310         {
2311           native->u.syment.n_scnum = N_UNDEF;
2312           native->u.syment.n_value = symbol->value;
2313         }
2314       else
2315         {
2316           native->u.syment.n_scnum =
2317             symbol->section->output_section->target_index;
2318           native->u.syment.n_value = (symbol->value
2319                                       + symbol->section->output_offset);
2320           if (! obj_pe (abfd))
2321             native->u.syment.n_value += symbol->section->output_section->vma;
2322
2323           /* Copy the any flags from the file header into the symbol.
2324              FIXME: Why?  */
2325           native->u.syment.n_flags = bfd_asymbol_bfd (& csym->symbol)->flags;
2326         }
2327
2328       csym->native = native;
2329     }
2330   else
2331     csym->native->u.syment.n_sclass = class;
2332
2333   return TRUE;
2334 }
2335
2336 struct coff_comdat_info *
2337 bfd_coff_get_comdat_section (bfd *abfd, struct bfd_section *sec)
2338 {
2339   if (bfd_get_flavour (abfd) == bfd_target_coff_flavour
2340       && coff_section_data (abfd, sec) != NULL)
2341     return coff_section_data (abfd, sec)->comdat;
2342   else
2343     return NULL;
2344 }