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