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