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