xref: /netbsd-src/external/gpl3/binutils.old/dist/bfd/coffgen.c (revision e992f068c547fd6e84b3f104dc2340adcc955732)
1 /* Support for the generic parts of COFF, for BFD.
2    Copyright (C) 1990-2022 Free Software Foundation, Inc.
3    Written by Cygnus Support.
4 
5    This file is part of BFD, the Binary File Descriptor library.
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20    MA 02110-1301, USA.  */
21 
22 /* Most of this hacked by  Steve Chamberlain, sac@cygnus.com.
23    Split out of coffcode.h by Ian Taylor, ian@cygnus.com.  */
24 
25 /* This file contains COFF code that is not dependent on any
26    particular COFF target.  There is only one version of this file in
27    libbfd.a, so no target specific code may be put in here.  Or, to
28    put it another way,
29 
30    ********** DO NOT PUT TARGET SPECIFIC CODE IN THIS FILE **********
31 
32    If you need to add some target specific behaviour, add a new hook
33    function to bfd_coff_backend_data.
34 
35    Some of these functions are also called by the ECOFF routines.
36    Those functions may not use any COFF specific information, such as
37    coff_data (abfd).  */
38 
39 #include "sysdep.h"
40 #include <limits.h>
41 #include "bfd.h"
42 #include "libbfd.h"
43 #include "coff/internal.h"
44 #include "libcoff.h"
45 
46 /* Take a section header read from a coff file (in HOST byte order),
47    and make a BFD "section" out of it.  This is used by ECOFF.  */
48 
49 static bool
make_a_section_from_file(bfd * abfd,struct internal_scnhdr * hdr,unsigned int target_index)50 make_a_section_from_file (bfd *abfd,
51 			  struct internal_scnhdr *hdr,
52 			  unsigned int target_index)
53 {
54   asection *return_section;
55   char *name;
56   bool result = true;
57   flagword flags;
58 
59   name = NULL;
60 
61   /* Handle long section names as in PE.  On reading, we want to
62     accept long names if the format permits them at all, regardless
63     of the current state of the flag that dictates if we would generate
64     them in outputs; this construct checks if that is the case by
65     attempting to set the flag, without changing its state; the call
66     will fail for formats that do not support long names at all.  */
67   if (bfd_coff_set_long_section_names (abfd, bfd_coff_long_section_names (abfd))
68       && hdr->s_name[0] == '/')
69     {
70       char buf[SCNNMLEN];
71       long strindex;
72       char *p;
73       const char *strings;
74 
75       /* Flag that this BFD uses long names, even though the format might
76 	 expect them to be off by default.  This won't directly affect the
77 	 format of any output BFD created from this one, but the information
78 	 can be used to decide what to do.  */
79       bfd_coff_set_long_section_names (abfd, true);
80       memcpy (buf, hdr->s_name + 1, SCNNMLEN - 1);
81       buf[SCNNMLEN - 1] = '\0';
82       strindex = strtol (buf, &p, 10);
83       if (*p == '\0' && strindex >= 0)
84 	{
85 	  strings = _bfd_coff_read_string_table (abfd);
86 	  if (strings == NULL)
87 	    return false;
88 	  if ((bfd_size_type)(strindex + 2) >= obj_coff_strings_len (abfd))
89 	    return false;
90 	  strings += strindex;
91 	  name = (char *) bfd_alloc (abfd,
92 				     (bfd_size_type) strlen (strings) + 1 + 1);
93 	  if (name == NULL)
94 	    return false;
95 	  strcpy (name, strings);
96 	}
97     }
98 
99   if (name == NULL)
100     {
101       /* Assorted wastage to null-terminate the name, thanks AT&T! */
102       name = (char *) bfd_alloc (abfd,
103 				 (bfd_size_type) sizeof (hdr->s_name) + 1 + 1);
104       if (name == NULL)
105 	return false;
106       strncpy (name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
107       name[sizeof (hdr->s_name)] = 0;
108     }
109 
110   return_section = bfd_make_section_anyway (abfd, name);
111   if (return_section == NULL)
112     return false;
113 
114   return_section->vma = hdr->s_vaddr;
115   return_section->lma = hdr->s_paddr;
116   return_section->size = hdr->s_size;
117   return_section->filepos = hdr->s_scnptr;
118   return_section->rel_filepos = hdr->s_relptr;
119   return_section->reloc_count = hdr->s_nreloc;
120 
121   bfd_coff_set_alignment_hook (abfd, return_section, hdr);
122 
123   return_section->line_filepos = hdr->s_lnnoptr;
124 
125   return_section->lineno_count = hdr->s_nlnno;
126   return_section->userdata = NULL;
127   return_section->next = NULL;
128   return_section->target_index = target_index;
129 
130   if (! bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name, return_section,
131 					 & flags))
132     result = false;
133 
134   return_section->flags = flags;
135 
136   /* At least on i386-coff, the line number count for a shared library
137      section must be ignored.  */
138   if ((return_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
139     return_section->lineno_count = 0;
140 
141   if (hdr->s_nreloc != 0)
142     return_section->flags |= SEC_RELOC;
143   /* FIXME: should this check 'hdr->s_size > 0'.  */
144   if (hdr->s_scnptr != 0)
145     return_section->flags |= SEC_HAS_CONTENTS;
146 
147   /* Compress/decompress DWARF debug sections with names: .debug_* and
148      .zdebug_*, after the section flags is set.  */
149   if ((flags & SEC_DEBUGGING)
150       && strlen (name) > 7
151       && ((name[1] == 'd' && name[6] == '_')
152 	  || (strlen (name) > 8 && name[1] == 'z' && name[7] == '_')))
153     {
154       enum { nothing, compress, decompress } action = nothing;
155       char *new_name = NULL;
156 
157       if (bfd_is_section_compressed (abfd, return_section))
158 	{
159 	  /* Compressed section.  Check if we should decompress.  */
160 	  if ((abfd->flags & BFD_DECOMPRESS))
161 	    action = decompress;
162 	}
163       else if (!bfd_is_section_compressed (abfd, return_section))
164 	{
165 	  /* Normal section.  Check if we should compress.  */
166 	  if ((abfd->flags & BFD_COMPRESS) && return_section->size != 0)
167 	    action = compress;
168 	}
169 
170       switch (action)
171 	{
172 	case nothing:
173 	  break;
174 	case compress:
175 	  if (!bfd_init_section_compress_status (abfd, return_section))
176 	    {
177 	      _bfd_error_handler
178 		/* xgettext: c-format */
179 		(_("%pB: unable to initialize compress status for section %s"),
180 		 abfd, name);
181 	      return false;
182 	    }
183 	  if (return_section->compress_status == COMPRESS_SECTION_DONE)
184 	    {
185 	      if (name[1] != 'z')
186 		{
187 		  unsigned int len = strlen (name);
188 
189 		  new_name = bfd_alloc (abfd, len + 2);
190 		  if (new_name == NULL)
191 		    return false;
192 		  new_name[0] = '.';
193 		  new_name[1] = 'z';
194 		  memcpy (new_name + 2, name + 1, len);
195 		}
196 	    }
197 	 break;
198 	case decompress:
199 	  if (!bfd_init_section_decompress_status (abfd, return_section))
200 	    {
201 	      _bfd_error_handler
202 		/* xgettext: c-format */
203 		(_("%pB: unable to initialize decompress status for section %s"),
204 		 abfd, name);
205 	      return false;
206 	    }
207 	  if (name[1] == 'z')
208 	    {
209 	      unsigned int len = strlen (name);
210 
211 	      new_name = bfd_alloc (abfd, len);
212 	      if (new_name == NULL)
213 		return false;
214 	      new_name[0] = '.';
215 	      memcpy (new_name + 1, name + 2, len - 1);
216 	    }
217 	  break;
218 	}
219       if (new_name != NULL)
220 	bfd_rename_section (return_section, new_name);
221     }
222 
223   return result;
224 }
225 
226 /* Read in a COFF object and make it into a BFD.  This is used by
227    ECOFF as well.  */
228 bfd_cleanup
229 coff_real_object_p (bfd *,
230 		    unsigned,
231 		    struct internal_filehdr *,
232 		    struct internal_aouthdr *);
233 bfd_cleanup
coff_real_object_p(bfd * abfd,unsigned nscns,struct internal_filehdr * internal_f,struct internal_aouthdr * internal_a)234 coff_real_object_p (bfd *abfd,
235 		    unsigned nscns,
236 		    struct internal_filehdr *internal_f,
237 		    struct internal_aouthdr *internal_a)
238 {
239   flagword oflags = abfd->flags;
240   bfd_vma ostart = bfd_get_start_address (abfd);
241   void * tdata;
242   void * tdata_save;
243   bfd_size_type readsize;	/* Length of file_info.  */
244   unsigned int scnhsz;
245   char *external_sections;
246 
247   if (!(internal_f->f_flags & F_RELFLG))
248     abfd->flags |= HAS_RELOC;
249   if ((internal_f->f_flags & F_EXEC))
250     abfd->flags |= EXEC_P;
251   if (!(internal_f->f_flags & F_LNNO))
252     abfd->flags |= HAS_LINENO;
253   if (!(internal_f->f_flags & F_LSYMS))
254     abfd->flags |= HAS_LOCALS;
255 
256   /* FIXME: How can we set D_PAGED correctly?  */
257   if ((internal_f->f_flags & F_EXEC) != 0)
258     abfd->flags |= D_PAGED;
259 
260   abfd->symcount = internal_f->f_nsyms;
261   if (internal_f->f_nsyms)
262     abfd->flags |= HAS_SYMS;
263 
264   if (internal_a != (struct internal_aouthdr *) NULL)
265     abfd->start_address = internal_a->entry;
266   else
267     abfd->start_address = 0;
268 
269   /* Set up the tdata area.  ECOFF uses its own routine, and overrides
270      abfd->flags.  */
271   tdata_save = abfd->tdata.any;
272   tdata = bfd_coff_mkobject_hook (abfd, (void *) internal_f, (void *) internal_a);
273   if (tdata == NULL)
274     goto fail2;
275 
276   scnhsz = bfd_coff_scnhsz (abfd);
277   readsize = (bfd_size_type) nscns * scnhsz;
278   external_sections = (char *) _bfd_alloc_and_read (abfd, readsize, readsize);
279   if (!external_sections)
280     goto fail;
281 
282   /* Set the arch/mach *before* swapping in sections; section header swapping
283      may depend on arch/mach info.  */
284   if (! bfd_coff_set_arch_mach_hook (abfd, (void *) internal_f))
285     goto fail;
286 
287   /* Now copy data as required; construct all asections etc.  */
288   if (nscns != 0)
289     {
290       unsigned int i;
291       for (i = 0; i < nscns; i++)
292 	{
293 	  struct internal_scnhdr tmp;
294 	  bfd_coff_swap_scnhdr_in (abfd,
295 				   (void *) (external_sections + i * scnhsz),
296 				   (void *) & tmp);
297 	  if (! make_a_section_from_file (abfd, &tmp, i + 1))
298 	    goto fail;
299 	}
300     }
301 
302   _bfd_coff_free_symbols (abfd);
303   return _bfd_no_cleanup;
304 
305  fail:
306   _bfd_coff_free_symbols (abfd);
307   bfd_release (abfd, tdata);
308  fail2:
309   abfd->tdata.any = tdata_save;
310   abfd->flags = oflags;
311   abfd->start_address = ostart;
312   return NULL;
313 }
314 
315 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
316    not a COFF file.  This is also used by ECOFF.  */
317 
318 bfd_cleanup
coff_object_p(bfd * abfd)319 coff_object_p (bfd *abfd)
320 {
321   bfd_size_type filhsz;
322   bfd_size_type aoutsz;
323   unsigned int nscns;
324   void * filehdr;
325   struct internal_filehdr internal_f;
326   struct internal_aouthdr internal_a;
327 
328   /* Figure out how much to read.  */
329   filhsz = bfd_coff_filhsz (abfd);
330   aoutsz = bfd_coff_aoutsz (abfd);
331 
332   filehdr = _bfd_alloc_and_read (abfd, filhsz, filhsz);
333   if (filehdr == NULL)
334     {
335       if (bfd_get_error () != bfd_error_system_call)
336 	bfd_set_error (bfd_error_wrong_format);
337       return NULL;
338     }
339   bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f);
340   bfd_release (abfd, filehdr);
341 
342   /* The XCOFF format has two sizes for the f_opthdr.  SMALL_AOUTSZ
343      (less than aoutsz) used in object files and AOUTSZ (equal to
344      aoutsz) in executables.  The bfd_coff_swap_aouthdr_in function
345      expects this header to be aoutsz bytes in length, so we use that
346      value in the call to bfd_alloc below.  But we must be careful to
347      only read in f_opthdr bytes in the call to bfd_bread.  We should
348      also attempt to catch corrupt or non-COFF binaries with a strange
349      value for f_opthdr.  */
350   if (! bfd_coff_bad_format_hook (abfd, &internal_f)
351       || internal_f.f_opthdr > aoutsz)
352     {
353       bfd_set_error (bfd_error_wrong_format);
354       return NULL;
355     }
356   nscns = internal_f.f_nscns;
357 
358   if (internal_f.f_opthdr)
359     {
360       void * opthdr;
361 
362       opthdr = _bfd_alloc_and_read (abfd, aoutsz, internal_f.f_opthdr);
363       if (opthdr == NULL)
364 	return NULL;
365       /* PR 17512: file: 11056-1136-0.004.  */
366       if (internal_f.f_opthdr < aoutsz)
367 	memset (((char *) opthdr) + internal_f.f_opthdr, 0,
368 		aoutsz - internal_f.f_opthdr);
369 
370       bfd_coff_swap_aouthdr_in (abfd, opthdr, (void *) &internal_a);
371       bfd_release (abfd, opthdr);
372     }
373 
374   return coff_real_object_p (abfd, nscns, &internal_f,
375 			     (internal_f.f_opthdr != 0
376 			      ? &internal_a
377 			      : (struct internal_aouthdr *) NULL));
378 }
379 
380 /* Get the BFD section from a COFF symbol section number.  */
381 
382 asection *
coff_section_from_bfd_index(bfd * abfd,int section_index)383 coff_section_from_bfd_index (bfd *abfd, int section_index)
384 {
385   struct bfd_section *answer = abfd->sections;
386 
387   if (section_index == N_ABS)
388     return bfd_abs_section_ptr;
389   if (section_index == N_UNDEF)
390     return bfd_und_section_ptr;
391   if (section_index == N_DEBUG)
392     return bfd_abs_section_ptr;
393 
394   while (answer)
395     {
396       if (answer->target_index == section_index)
397 	return answer;
398       answer = answer->next;
399     }
400 
401   /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
402      has a bad symbol table in biglitpow.o.  */
403   return bfd_und_section_ptr;
404 }
405 
406 /* Get the upper bound of a COFF symbol table.  */
407 
408 long
coff_get_symtab_upper_bound(bfd * abfd)409 coff_get_symtab_upper_bound (bfd *abfd)
410 {
411   if (!bfd_coff_slurp_symbol_table (abfd))
412     return -1;
413 
414   return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *));
415 }
416 
417 /* Canonicalize a COFF symbol table.  */
418 
419 long
coff_canonicalize_symtab(bfd * abfd,asymbol ** alocation)420 coff_canonicalize_symtab (bfd *abfd, asymbol **alocation)
421 {
422   unsigned int counter;
423   coff_symbol_type *symbase;
424   coff_symbol_type **location = (coff_symbol_type **) alocation;
425 
426   if (!bfd_coff_slurp_symbol_table (abfd))
427     return -1;
428 
429   symbase = obj_symbols (abfd);
430   counter = bfd_get_symcount (abfd);
431   while (counter-- > 0)
432     *location++ = symbase++;
433 
434   *location = NULL;
435 
436   return bfd_get_symcount (abfd);
437 }
438 
439 /* Get the name of a symbol.  The caller must pass in a buffer of size
440    >= SYMNMLEN + 1.  */
441 
442 const char *
_bfd_coff_internal_syment_name(bfd * abfd,const struct internal_syment * sym,char * buf)443 _bfd_coff_internal_syment_name (bfd *abfd,
444 				const struct internal_syment *sym,
445 				char *buf)
446 {
447   /* FIXME: It's not clear this will work correctly if sizeof
448      (_n_zeroes) != 4.  */
449   if (sym->_n._n_n._n_zeroes != 0
450       || sym->_n._n_n._n_offset == 0)
451     {
452       memcpy (buf, sym->_n._n_name, SYMNMLEN);
453       buf[SYMNMLEN] = '\0';
454       return buf;
455     }
456   else
457     {
458       const char *strings;
459 
460       BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE);
461       strings = obj_coff_strings (abfd);
462       if (strings == NULL)
463 	{
464 	  strings = _bfd_coff_read_string_table (abfd);
465 	  if (strings == NULL)
466 	    return NULL;
467 	}
468       /* PR 17910: Only check for string overflow if the length has been set.
469 	 Some DLLs, eg those produced by Visual Studio, may not set the length field.  */
470       if (obj_coff_strings_len (abfd) > 0
471 	  && sym->_n._n_n._n_offset >= obj_coff_strings_len (abfd))
472 	return NULL;
473       return strings + sym->_n._n_n._n_offset;
474     }
475 }
476 
477 /* Read in and swap the relocs.  This returns a buffer holding the
478    relocs for section SEC in file ABFD.  If CACHE is TRUE and
479    INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
480    the function is called again.  If EXTERNAL_RELOCS is not NULL, it
481    is a buffer large enough to hold the unswapped relocs.  If
482    INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
483    the swapped relocs.  If REQUIRE_INTERNAL is TRUE, then the return
484    value must be INTERNAL_RELOCS.  The function returns NULL on error.  */
485 
486 struct internal_reloc *
_bfd_coff_read_internal_relocs(bfd * abfd,asection * sec,bool cache,bfd_byte * external_relocs,bool require_internal,struct internal_reloc * internal_relocs)487 _bfd_coff_read_internal_relocs (bfd *abfd,
488 				asection *sec,
489 				bool cache,
490 				bfd_byte *external_relocs,
491 				bool require_internal,
492 				struct internal_reloc *internal_relocs)
493 {
494   bfd_size_type relsz;
495   bfd_byte *free_external = NULL;
496   struct internal_reloc *free_internal = NULL;
497   bfd_byte *erel;
498   bfd_byte *erel_end;
499   struct internal_reloc *irel;
500   bfd_size_type amt;
501 
502   if (sec->reloc_count == 0)
503     return internal_relocs;	/* Nothing to do.  */
504 
505   if (coff_section_data (abfd, sec) != NULL
506       && coff_section_data (abfd, sec)->relocs != NULL)
507     {
508       if (! require_internal)
509 	return coff_section_data (abfd, sec)->relocs;
510       memcpy (internal_relocs, coff_section_data (abfd, sec)->relocs,
511 	      sec->reloc_count * sizeof (struct internal_reloc));
512       return internal_relocs;
513     }
514 
515   relsz = bfd_coff_relsz (abfd);
516 
517   amt = sec->reloc_count * relsz;
518   if (external_relocs == NULL)
519     {
520       free_external = (bfd_byte *) bfd_malloc (amt);
521       if (free_external == NULL)
522 	goto error_return;
523       external_relocs = free_external;
524     }
525 
526   if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
527       || bfd_bread (external_relocs, amt, abfd) != amt)
528     goto error_return;
529 
530   if (internal_relocs == NULL)
531     {
532       amt = sec->reloc_count;
533       amt *= sizeof (struct internal_reloc);
534       free_internal = (struct internal_reloc *) bfd_malloc (amt);
535       if (free_internal == NULL)
536 	goto error_return;
537       internal_relocs = free_internal;
538     }
539 
540   /* Swap in the relocs.  */
541   erel = external_relocs;
542   erel_end = erel + relsz * sec->reloc_count;
543   irel = internal_relocs;
544   for (; erel < erel_end; erel += relsz, irel++)
545     bfd_coff_swap_reloc_in (abfd, (void *) erel, (void *) irel);
546 
547   free (free_external);
548   free_external = NULL;
549 
550   if (cache && free_internal != NULL)
551     {
552       if (coff_section_data (abfd, sec) == NULL)
553 	{
554 	  amt = sizeof (struct coff_section_tdata);
555 	  sec->used_by_bfd = bfd_zalloc (abfd, amt);
556 	  if (sec->used_by_bfd == NULL)
557 	    goto error_return;
558 	  coff_section_data (abfd, sec)->contents = NULL;
559 	}
560       coff_section_data (abfd, sec)->relocs = free_internal;
561     }
562 
563   return internal_relocs;
564 
565  error_return:
566   free (free_external);
567   free (free_internal);
568   return NULL;
569 }
570 
571 /* Set lineno_count for the output sections of a COFF file.  */
572 
573 int
coff_count_linenumbers(bfd * abfd)574 coff_count_linenumbers (bfd *abfd)
575 {
576   unsigned int limit = bfd_get_symcount (abfd);
577   unsigned int i;
578   int total = 0;
579   asymbol **p;
580   asection *s;
581 
582   if (limit == 0)
583     {
584       /* This may be from the backend linker, in which case the
585 	 lineno_count in the sections is correct.  */
586       for (s = abfd->sections; s != NULL; s = s->next)
587 	total += s->lineno_count;
588       return total;
589     }
590 
591   for (s = abfd->sections; s != NULL; s = s->next)
592     BFD_ASSERT (s->lineno_count == 0);
593 
594   for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
595     {
596       asymbol *q_maybe = *p;
597 
598       if (bfd_asymbol_bfd (q_maybe) != NULL
599 	  && bfd_family_coff (bfd_asymbol_bfd (q_maybe)))
600 	{
601 	  coff_symbol_type *q = coffsymbol (q_maybe);
602 
603 	  /* The AIX 4.1 compiler can sometimes generate line numbers
604 	     attached to debugging symbols.  We try to simply ignore
605 	     those here.  */
606 	  if (q->lineno != NULL
607 	      && q->symbol.section->owner != NULL)
608 	    {
609 	      /* This symbol has line numbers.  Increment the owning
610 		 section's linenumber count.  */
611 	      alent *l = q->lineno;
612 
613 	      do
614 		{
615 		  asection * sec = q->symbol.section->output_section;
616 
617 		  /* Do not try to update fields in read-only sections.  */
618 		  if (! bfd_is_const_section (sec))
619 		    sec->lineno_count ++;
620 
621 		  ++total;
622 		  ++l;
623 		}
624 	      while (l->line_number != 0);
625 	    }
626 	}
627     }
628 
629   return total;
630 }
631 
632 static void
fixup_symbol_value(bfd * abfd,coff_symbol_type * coff_symbol_ptr,struct internal_syment * syment)633 fixup_symbol_value (bfd *abfd,
634 		    coff_symbol_type *coff_symbol_ptr,
635 		    struct internal_syment *syment)
636 {
637   /* Normalize the symbol flags.  */
638   if (coff_symbol_ptr->symbol.section
639       && bfd_is_com_section (coff_symbol_ptr->symbol.section))
640     {
641       /* A common symbol is undefined with a value.  */
642       syment->n_scnum = N_UNDEF;
643       syment->n_value = coff_symbol_ptr->symbol.value;
644     }
645   else if ((coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) != 0
646 	   && (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING_RELOC) == 0)
647     {
648       syment->n_value = coff_symbol_ptr->symbol.value;
649     }
650   else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
651     {
652       syment->n_scnum = N_UNDEF;
653       syment->n_value = 0;
654     }
655   /* FIXME: Do we need to handle the absolute section here?  */
656   else
657     {
658       if (coff_symbol_ptr->symbol.section)
659 	{
660 	  syment->n_scnum =
661 	    coff_symbol_ptr->symbol.section->output_section->target_index;
662 
663 	  syment->n_value = (coff_symbol_ptr->symbol.value
664 			     + coff_symbol_ptr->symbol.section->output_offset);
665 	  if (! obj_pe (abfd))
666 	    {
667 	      syment->n_value += (syment->n_sclass == C_STATLAB)
668 		? coff_symbol_ptr->symbol.section->output_section->lma
669 		: coff_symbol_ptr->symbol.section->output_section->vma;
670 	    }
671 	}
672       else
673 	{
674 	  BFD_ASSERT (0);
675 	  /* This can happen, but I don't know why yet (steve@cygnus.com) */
676 	  syment->n_scnum = N_ABS;
677 	  syment->n_value = coff_symbol_ptr->symbol.value;
678 	}
679     }
680 }
681 
682 /* Run through all the symbols in the symbol table and work out what
683    their indexes into the symbol table will be when output.
684 
685    Coff requires that each C_FILE symbol points to the next one in the
686    chain, and that the last one points to the first external symbol. We
687    do that here too.  */
688 
689 bool
coff_renumber_symbols(bfd * bfd_ptr,int * first_undef)690 coff_renumber_symbols (bfd *bfd_ptr, int *first_undef)
691 {
692   unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
693   asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
694   unsigned int native_index = 0;
695   struct internal_syment *last_file = NULL;
696   unsigned int symbol_index;
697 
698   /* COFF demands that undefined symbols come after all other symbols.
699      Since we don't need to impose this extra knowledge on all our
700      client programs, deal with that here.  Sort the symbol table;
701      just move the undefined symbols to the end, leaving the rest
702      alone.  The O'Reilly book says that defined global symbols come
703      at the end before the undefined symbols, so we do that here as
704      well.  */
705   /* @@ Do we have some condition we could test for, so we don't always
706      have to do this?  I don't think relocatability is quite right, but
707      I'm not certain.  [raeburn:19920508.1711EST]  */
708   {
709     asymbol **newsyms;
710     unsigned int i;
711     bfd_size_type amt;
712 
713     amt = sizeof (asymbol *) * ((bfd_size_type) symbol_count + 1);
714     newsyms = (asymbol **) bfd_alloc (bfd_ptr, amt);
715     if (!newsyms)
716       return false;
717     bfd_ptr->outsymbols = newsyms;
718     for (i = 0; i < symbol_count; i++)
719       if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
720 	  || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
721 	      && !bfd_is_com_section (symbol_ptr_ptr[i]->section)
722 	      && ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) != 0
723 		  || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
724 		      == 0))))
725 	*newsyms++ = symbol_ptr_ptr[i];
726 
727     for (i = 0; i < symbol_count; i++)
728       if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
729 	  && !bfd_is_und_section (symbol_ptr_ptr[i]->section)
730 	  && (bfd_is_com_section (symbol_ptr_ptr[i]->section)
731 	      || ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) == 0
732 		  && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
733 		      != 0))))
734 	*newsyms++ = symbol_ptr_ptr[i];
735 
736     *first_undef = newsyms - bfd_ptr->outsymbols;
737 
738     for (i = 0; i < symbol_count; i++)
739       if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
740 	  && bfd_is_und_section (symbol_ptr_ptr[i]->section))
741 	*newsyms++ = symbol_ptr_ptr[i];
742     *newsyms = (asymbol *) NULL;
743     symbol_ptr_ptr = bfd_ptr->outsymbols;
744   }
745 
746   for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
747     {
748       coff_symbol_type *coff_symbol_ptr;
749 
750       coff_symbol_ptr = coff_symbol_from (symbol_ptr_ptr[symbol_index]);
751       symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
752       if (coff_symbol_ptr && coff_symbol_ptr->native)
753 	{
754 	  combined_entry_type *s = coff_symbol_ptr->native;
755 	  int i;
756 
757 	  BFD_ASSERT (s->is_sym);
758 	  if (s->u.syment.n_sclass == C_FILE)
759 	    {
760 	      if (last_file != NULL)
761 		last_file->n_value = native_index;
762 	      last_file = &(s->u.syment);
763 	    }
764 	  else
765 	    /* Modify the symbol values according to their section and
766 	       type.  */
767 	    fixup_symbol_value (bfd_ptr, coff_symbol_ptr, &(s->u.syment));
768 
769 	  for (i = 0; i < s->u.syment.n_numaux + 1; i++)
770 	    s[i].offset = native_index++;
771 	}
772       else
773 	native_index++;
774     }
775 
776   obj_conv_table_size (bfd_ptr) = native_index;
777 
778   return true;
779 }
780 
781 /* Run thorough the symbol table again, and fix it so that all
782    pointers to entries are changed to the entries' index in the output
783    symbol table.  */
784 
785 void
coff_mangle_symbols(bfd * bfd_ptr)786 coff_mangle_symbols (bfd *bfd_ptr)
787 {
788   unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
789   asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
790   unsigned int symbol_index;
791 
792   for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
793     {
794       coff_symbol_type *coff_symbol_ptr;
795 
796       coff_symbol_ptr = coff_symbol_from (symbol_ptr_ptr[symbol_index]);
797       if (coff_symbol_ptr && coff_symbol_ptr->native)
798 	{
799 	  int i;
800 	  combined_entry_type *s = coff_symbol_ptr->native;
801 
802 	  BFD_ASSERT (s->is_sym);
803 	  if (s->fix_value)
804 	    {
805 	      /* FIXME: We should use a union here.  */
806 	      s->u.syment.n_value =
807 		(uintptr_t) ((combined_entry_type *)
808 			     (uintptr_t) s->u.syment.n_value)->offset;
809 	      s->fix_value = 0;
810 	    }
811 	  if (s->fix_line)
812 	    {
813 	      /* The value is the offset into the line number entries
814 		 for the symbol's section.  On output, the symbol's
815 		 section should be N_DEBUG.  */
816 	      s->u.syment.n_value =
817 		(coff_symbol_ptr->symbol.section->output_section->line_filepos
818 		 + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr));
819 	      coff_symbol_ptr->symbol.section =
820 		coff_section_from_bfd_index (bfd_ptr, N_DEBUG);
821 	      BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
822 	    }
823 	  for (i = 0; i < s->u.syment.n_numaux; i++)
824 	    {
825 	      combined_entry_type *a = s + i + 1;
826 
827 	      BFD_ASSERT (! a->is_sym);
828 	      if (a->fix_tag)
829 		{
830 		  a->u.auxent.x_sym.x_tagndx.l =
831 		    a->u.auxent.x_sym.x_tagndx.p->offset;
832 		  a->fix_tag = 0;
833 		}
834 	      if (a->fix_end)
835 		{
836 		  a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
837 		    a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
838 		  a->fix_end = 0;
839 		}
840 	      if (a->fix_scnlen)
841 		{
842 		  a->u.auxent.x_csect.x_scnlen.l =
843 		    a->u.auxent.x_csect.x_scnlen.p->offset;
844 		  a->fix_scnlen = 0;
845 		}
846 	    }
847 	}
848     }
849 }
850 
851 static bool
coff_write_auxent_fname(bfd * abfd,char * str,union internal_auxent * auxent,struct bfd_strtab_hash * strtab,bool hash)852 coff_write_auxent_fname (bfd *abfd,
853 			 char *str,
854 			 union internal_auxent *auxent,
855 			 struct bfd_strtab_hash *strtab,
856 			 bool hash)
857 {
858   unsigned int str_length = strlen (str);
859   unsigned int filnmlen = bfd_coff_filnmlen (abfd);
860 
861   if (bfd_coff_long_filenames (abfd))
862     {
863       if (str_length <= filnmlen)
864 	strncpy (auxent->x_file.x_n.x_fname, str, filnmlen);
865       else
866 	{
867 	  bfd_size_type indx = _bfd_stringtab_add (strtab, str, hash, false);
868 
869 	  if (indx == (bfd_size_type) -1)
870 	    return false;
871 
872 	  auxent->x_file.x_n.x_n.x_offset = STRING_SIZE_SIZE + indx;
873 	  auxent->x_file.x_n.x_n.x_zeroes = 0;
874 	}
875     }
876   else
877     {
878       strncpy (auxent->x_file.x_n.x_fname, str, filnmlen);
879       if (str_length > filnmlen)
880 	str[filnmlen] = '\0';
881     }
882 
883   return true;
884 }
885 
886 static bool
coff_fix_symbol_name(bfd * abfd,asymbol * symbol,combined_entry_type * native,struct bfd_strtab_hash * strtab,bool hash,asection ** debug_string_section_p,bfd_size_type * debug_string_size_p)887 coff_fix_symbol_name (bfd *abfd,
888 		      asymbol *symbol,
889 		      combined_entry_type *native,
890 		      struct bfd_strtab_hash *strtab,
891 		      bool hash,
892 		      asection **debug_string_section_p,
893 		      bfd_size_type *debug_string_size_p)
894 {
895   unsigned int name_length;
896   char *name = (char *) (symbol->name);
897   bfd_size_type indx;
898 
899   if (name == NULL)
900     {
901       /* COFF symbols always have names, so we'll make one up.  */
902       symbol->name = "strange";
903       name = (char *) symbol->name;
904     }
905   name_length = strlen (name);
906 
907   BFD_ASSERT (native->is_sym);
908   if (native->u.syment.n_sclass == C_FILE
909       && native->u.syment.n_numaux > 0)
910     {
911       if (bfd_coff_force_symnames_in_strings (abfd))
912 	{
913 	  indx = _bfd_stringtab_add (strtab, ".file", hash, false);
914 	  if (indx == (bfd_size_type) -1)
915 	    return false;
916 
917 	  native->u.syment._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
918 	  native->u.syment._n._n_n._n_zeroes = 0;
919 	}
920       else
921 	strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
922 
923       BFD_ASSERT (! (native + 1)->is_sym);
924       if (!coff_write_auxent_fname (abfd, name, &(native + 1)->u.auxent,
925 			       strtab, hash))
926 	return false;
927     }
928   else
929     {
930       if (name_length <= SYMNMLEN && !bfd_coff_force_symnames_in_strings (abfd))
931 	/* This name will fit into the symbol neatly.  */
932 	strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
933 
934       else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
935 	{
936 	  indx = _bfd_stringtab_add (strtab, name, hash, false);
937 	  if (indx == (bfd_size_type) -1)
938 	    return false;
939 
940 	  native->u.syment._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
941 	  native->u.syment._n._n_n._n_zeroes = 0;
942 	}
943       else
944 	{
945 	  file_ptr filepos;
946 	  bfd_byte buf[4];
947 	  int prefix_len = bfd_coff_debug_string_prefix_length (abfd);
948 
949 	  /* This name should be written into the .debug section.  For
950 	     some reason each name is preceded by a two byte length
951 	     and also followed by a null byte.  FIXME: We assume that
952 	     the .debug section has already been created, and that it
953 	     is large enough.  */
954 	  if (*debug_string_section_p == (asection *) NULL)
955 	    *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
956 	  filepos = bfd_tell (abfd);
957 	  if (prefix_len == 4)
958 	    bfd_put_32 (abfd, (bfd_vma) (name_length + 1), buf);
959 	  else
960 	    bfd_put_16 (abfd, (bfd_vma) (name_length + 1), buf);
961 
962 	  if (!bfd_set_section_contents (abfd,
963 					 *debug_string_section_p,
964 					 (void *) buf,
965 					 (file_ptr) *debug_string_size_p,
966 					 (bfd_size_type) prefix_len)
967 	      || !bfd_set_section_contents (abfd,
968 					    *debug_string_section_p,
969 					    (void *) symbol->name,
970 					    (file_ptr) (*debug_string_size_p
971 							+ prefix_len),
972 					    (bfd_size_type) name_length + 1))
973 	    abort ();
974 	  if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
975 	    abort ();
976 	  native->u.syment._n._n_n._n_offset =
977 	      *debug_string_size_p + prefix_len;
978 	  native->u.syment._n._n_n._n_zeroes = 0;
979 	  *debug_string_size_p += name_length + 1 + prefix_len;
980 	}
981     }
982 
983   return true;
984 }
985 
986 /* We need to keep track of the symbol index so that when we write out
987    the relocs we can get the index for a symbol.  This method is a
988    hack.  FIXME.  */
989 
990 #define set_index(symbol, idx)	((symbol)->udata.i = (idx))
991 
992 /* Write a symbol out to a COFF file.  */
993 
994 static bool
coff_write_symbol(bfd * abfd,asymbol * symbol,combined_entry_type * native,bfd_vma * written,struct bfd_strtab_hash * strtab,bool hash,asection ** debug_string_section_p,bfd_size_type * debug_string_size_p)995 coff_write_symbol (bfd *abfd,
996 		   asymbol *symbol,
997 		   combined_entry_type *native,
998 		   bfd_vma *written,
999 		   struct bfd_strtab_hash *strtab,
1000 		   bool hash,
1001 		   asection **debug_string_section_p,
1002 		   bfd_size_type *debug_string_size_p)
1003 {
1004   unsigned int numaux = native->u.syment.n_numaux;
1005   int type = native->u.syment.n_type;
1006   int n_sclass = (int) native->u.syment.n_sclass;
1007   asection *output_section = symbol->section->output_section
1008 			       ? symbol->section->output_section
1009 			       : symbol->section;
1010   void * buf;
1011   bfd_size_type symesz;
1012 
1013   BFD_ASSERT (native->is_sym);
1014 
1015   if (native->u.syment.n_sclass == C_FILE)
1016     symbol->flags |= BSF_DEBUGGING;
1017 
1018   if (symbol->flags & BSF_DEBUGGING
1019       && bfd_is_abs_section (symbol->section))
1020     native->u.syment.n_scnum = N_DEBUG;
1021 
1022   else if (bfd_is_abs_section (symbol->section))
1023     native->u.syment.n_scnum = N_ABS;
1024 
1025   else if (bfd_is_und_section (symbol->section))
1026     native->u.syment.n_scnum = N_UNDEF;
1027 
1028   else
1029     native->u.syment.n_scnum =
1030       output_section->target_index;
1031 
1032   if (!coff_fix_symbol_name (abfd, symbol, native, strtab, hash,
1033 			     debug_string_section_p, debug_string_size_p))
1034     return false;
1035 
1036   symesz = bfd_coff_symesz (abfd);
1037   buf = bfd_alloc (abfd, symesz);
1038   if (!buf)
1039     return false;
1040   bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
1041   if (bfd_bwrite (buf, symesz, abfd) != symesz)
1042     return false;
1043   bfd_release (abfd, buf);
1044 
1045   if (native->u.syment.n_numaux > 0)
1046     {
1047       bfd_size_type auxesz;
1048       unsigned int j;
1049 
1050       auxesz = bfd_coff_auxesz (abfd);
1051       buf = bfd_alloc (abfd, auxesz);
1052       if (!buf)
1053 	return false;
1054       for (j = 0; j < native->u.syment.n_numaux; j++)
1055 	{
1056 	  BFD_ASSERT (! (native + j + 1)->is_sym);
1057 
1058 	  /* Adjust auxent only if this isn't the filename
1059 	     auxiliary entry.  */
1060 	  if (native->u.syment.n_sclass == C_FILE
1061 	      && (native + j + 1)->u.auxent.x_file.x_ftype)
1062 	    coff_write_auxent_fname (abfd, (char *) (native + j + 1)->extrap,
1063 				     &(native + j + 1)->u.auxent, strtab, hash);
1064 
1065 	  bfd_coff_swap_aux_out (abfd,
1066 				 &((native + j + 1)->u.auxent),
1067 				 type, n_sclass, (int) j,
1068 				 native->u.syment.n_numaux,
1069 				 buf);
1070 	  if (bfd_bwrite (buf, auxesz, abfd) != auxesz)
1071 	    return false;
1072 	}
1073       bfd_release (abfd, buf);
1074     }
1075 
1076   /* Store the index for use when we write out the relocs.  */
1077   set_index (symbol, *written);
1078 
1079   *written += numaux + 1;
1080   return true;
1081 }
1082 
1083 /* Write out a symbol to a COFF file that does not come from a COFF
1084    file originally.  This symbol may have been created by the linker,
1085    or we may be linking a non COFF file to a COFF file.  */
1086 
1087 bool
coff_write_alien_symbol(bfd * abfd,asymbol * symbol,struct internal_syment * isym,bfd_vma * written,struct bfd_strtab_hash * strtab,bool hash,asection ** debug_string_section_p,bfd_size_type * debug_string_size_p)1088 coff_write_alien_symbol (bfd *abfd,
1089 			 asymbol *symbol,
1090 			 struct internal_syment *isym,
1091 			 bfd_vma *written,
1092 			 struct bfd_strtab_hash *strtab,
1093 			 bool hash,
1094 			 asection **debug_string_section_p,
1095 			 bfd_size_type *debug_string_size_p)
1096 {
1097   combined_entry_type *native;
1098   combined_entry_type dummy[2];
1099   asection *output_section = symbol->section->output_section
1100 			       ? symbol->section->output_section
1101 			       : symbol->section;
1102   struct bfd_link_info *link_info = coff_data (abfd)->link_info;
1103   bool ret;
1104 
1105   if ((!link_info || link_info->strip_discarded)
1106       && !bfd_is_abs_section (symbol->section)
1107       && symbol->section->output_section == bfd_abs_section_ptr)
1108     {
1109       symbol->name = "";
1110       if (isym != NULL)
1111 	memset (isym, 0, sizeof (*isym));
1112       return true;
1113     }
1114   memset (dummy, 0, sizeof dummy);
1115   native = dummy;
1116   native->is_sym = true;
1117   native[1].is_sym = false;
1118   native->u.syment.n_type = T_NULL;
1119   native->u.syment.n_flags = 0;
1120   native->u.syment.n_numaux = 0;
1121   if (bfd_is_und_section (symbol->section))
1122     {
1123       native->u.syment.n_scnum = N_UNDEF;
1124       native->u.syment.n_value = symbol->value;
1125     }
1126   else if (bfd_is_com_section (symbol->section))
1127     {
1128       native->u.syment.n_scnum = N_UNDEF;
1129       native->u.syment.n_value = symbol->value;
1130     }
1131   else if (symbol->flags & BSF_FILE)
1132     {
1133       native->u.syment.n_scnum = N_DEBUG;
1134       native->u.syment.n_numaux = 1;
1135     }
1136   else if (symbol->flags & BSF_DEBUGGING)
1137     {
1138       /* There isn't much point to writing out a debugging symbol
1139 	 unless we are prepared to convert it into COFF debugging
1140 	 format.  So, we just ignore them.  We must clobber the symbol
1141 	 name to keep it from being put in the string table.  */
1142       symbol->name = "";
1143       if (isym != NULL)
1144 	memset (isym, 0, sizeof (*isym));
1145       return true;
1146     }
1147   else
1148     {
1149       native->u.syment.n_scnum = output_section->target_index;
1150       native->u.syment.n_value = (symbol->value
1151 				  + symbol->section->output_offset);
1152       if (! obj_pe (abfd))
1153 	native->u.syment.n_value += output_section->vma;
1154 
1155       /* Copy the any flags from the file header into the symbol.
1156 	 FIXME: Why?  */
1157       {
1158 	coff_symbol_type *c = coff_symbol_from (symbol);
1159 	if (c != (coff_symbol_type *) NULL)
1160 	  native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
1161       }
1162     }
1163 
1164   native->u.syment.n_type = 0;
1165   if (symbol->flags & BSF_FILE)
1166     native->u.syment.n_sclass = C_FILE;
1167   else if (symbol->flags & BSF_LOCAL)
1168     native->u.syment.n_sclass = C_STAT;
1169   else if (symbol->flags & BSF_WEAK)
1170     native->u.syment.n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1171   else
1172     native->u.syment.n_sclass = C_EXT;
1173 
1174   ret = coff_write_symbol (abfd, symbol, native, written, strtab, hash,
1175 			   debug_string_section_p, debug_string_size_p);
1176   if (isym != NULL)
1177     *isym = native->u.syment;
1178   return ret;
1179 }
1180 
1181 /* Write a native symbol to a COFF file.  */
1182 
1183 static bool
coff_write_native_symbol(bfd * abfd,coff_symbol_type * symbol,bfd_vma * written,struct bfd_strtab_hash * strtab,asection ** debug_string_section_p,bfd_size_type * debug_string_size_p)1184 coff_write_native_symbol (bfd *abfd,
1185 			  coff_symbol_type *symbol,
1186 			  bfd_vma *written,
1187 			  struct bfd_strtab_hash *strtab,
1188 			  asection **debug_string_section_p,
1189 			  bfd_size_type *debug_string_size_p)
1190 {
1191   combined_entry_type *native = symbol->native;
1192   alent *lineno = symbol->lineno;
1193   struct bfd_link_info *link_info = coff_data (abfd)->link_info;
1194 
1195   if ((!link_info || link_info->strip_discarded)
1196       && !bfd_is_abs_section (symbol->symbol.section)
1197       && symbol->symbol.section->output_section == bfd_abs_section_ptr)
1198     {
1199       symbol->symbol.name = "";
1200       return true;
1201     }
1202 
1203   BFD_ASSERT (native->is_sym);
1204   /* If this symbol has an associated line number, we must store the
1205      symbol index in the line number field.  We also tag the auxent to
1206      point to the right place in the lineno table.  */
1207   if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
1208     {
1209       unsigned int count = 0;
1210 
1211       lineno[count].u.offset = *written;
1212       if (native->u.syment.n_numaux)
1213 	{
1214 	  union internal_auxent *a = &((native + 1)->u.auxent);
1215 
1216 	  a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
1217 	    symbol->symbol.section->output_section->moving_line_filepos;
1218 	}
1219 
1220       /* Count and relocate all other linenumbers.  */
1221       count++;
1222       while (lineno[count].line_number != 0)
1223 	{
1224 	  lineno[count].u.offset +=
1225 	    (symbol->symbol.section->output_section->vma
1226 	     + symbol->symbol.section->output_offset);
1227 	  count++;
1228 	}
1229       symbol->done_lineno = true;
1230 
1231       if (! bfd_is_const_section (symbol->symbol.section->output_section))
1232 	symbol->symbol.section->output_section->moving_line_filepos +=
1233 	  count * bfd_coff_linesz (abfd);
1234     }
1235 
1236   return coff_write_symbol (abfd, &(symbol->symbol), native, written,
1237 			    strtab, true, debug_string_section_p,
1238 			    debug_string_size_p);
1239 }
1240 
1241 static void
null_error_handler(const char * fmt ATTRIBUTE_UNUSED,va_list ap ATTRIBUTE_UNUSED)1242 null_error_handler (const char *fmt ATTRIBUTE_UNUSED,
1243 		    va_list ap ATTRIBUTE_UNUSED)
1244 {
1245 }
1246 
1247 /* Write out the COFF symbols.  */
1248 
1249 bool
coff_write_symbols(bfd * abfd)1250 coff_write_symbols (bfd *abfd)
1251 {
1252   struct bfd_strtab_hash *strtab;
1253   asection *debug_string_section;
1254   bfd_size_type debug_string_size;
1255   unsigned int i;
1256   unsigned int limit = bfd_get_symcount (abfd);
1257   bfd_vma written = 0;
1258   asymbol **p;
1259 
1260   debug_string_section = NULL;
1261   debug_string_size = 0;
1262 
1263   strtab = _bfd_stringtab_init ();
1264   if (strtab == NULL)
1265     return false;
1266 
1267   /* If this target supports long section names, they must be put into
1268      the string table.  This is supported by PE.  This code must
1269      handle section names just as they are handled in
1270      coff_write_object_contents.  This is why we pass hash as FALSE below.  */
1271   if (bfd_coff_long_section_names (abfd))
1272     {
1273       asection *o;
1274 
1275       for (o = abfd->sections; o != NULL; o = o->next)
1276 	if (strlen (o->name) > SCNNMLEN
1277 	    && _bfd_stringtab_add (strtab, o->name, false, false)
1278 	       == (bfd_size_type) -1)
1279 	  return false;
1280     }
1281 
1282   /* Seek to the right place.  */
1283   if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1284     return false;
1285 
1286   /* Output all the symbols we have.  */
1287   written = 0;
1288   for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
1289     {
1290       asymbol *symbol = *p;
1291       coff_symbol_type *c_symbol = coff_symbol_from (symbol);
1292 
1293       if (c_symbol == (coff_symbol_type *) NULL
1294 	  || c_symbol->native == (combined_entry_type *) NULL)
1295 	{
1296 	  if (!coff_write_alien_symbol (abfd, symbol, NULL, &written,
1297 					strtab, true, &debug_string_section,
1298 					&debug_string_size))
1299 	    return false;
1300 	}
1301       else
1302 	{
1303 	  if (coff_backend_info (abfd)->_bfd_coff_classify_symbol != NULL)
1304 	    {
1305 	      bfd_error_handler_type current_error_handler;
1306 	      enum coff_symbol_classification sym_class;
1307 	      unsigned char *n_sclass;
1308 
1309 	      /* Suppress error reporting by bfd_coff_classify_symbol.
1310 		 Error messages can be generated when we are processing a local
1311 		 symbol which has no associated section and we do not have to
1312 		 worry about this, all we need to know is that it is local.  */
1313 	      current_error_handler = bfd_set_error_handler (null_error_handler);
1314 	      BFD_ASSERT (c_symbol->native->is_sym);
1315 	      sym_class = bfd_coff_classify_symbol (abfd,
1316 						    &c_symbol->native->u.syment);
1317 	      (void) bfd_set_error_handler (current_error_handler);
1318 
1319 	      n_sclass = &c_symbol->native->u.syment.n_sclass;
1320 
1321 	      /* If the symbol class has been changed (eg objcopy/ld script/etc)
1322 		 we cannot retain the existing sclass from the original symbol.
1323 		 Weak symbols only have one valid sclass, so just set it always.
1324 		 If it is not local class and should be, set it C_STAT.
1325 		 If it is global and not classified as global, or if it is
1326 		 weak (which is also classified as global), set it C_EXT.  */
1327 
1328 	      if (symbol->flags & BSF_WEAK)
1329 		*n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1330 	      else if (symbol->flags & BSF_LOCAL && sym_class != COFF_SYMBOL_LOCAL)
1331 		*n_sclass = C_STAT;
1332 	      else if (symbol->flags & BSF_GLOBAL
1333 		       && (sym_class != COFF_SYMBOL_GLOBAL
1334 #ifdef COFF_WITH_PE
1335 			   || *n_sclass == C_NT_WEAK
1336 #endif
1337 			   || *n_sclass == C_WEAKEXT))
1338 		c_symbol->native->u.syment.n_sclass = C_EXT;
1339 	    }
1340 
1341 	  if (!coff_write_native_symbol (abfd, c_symbol, &written,
1342 					 strtab, &debug_string_section,
1343 					 &debug_string_size))
1344 	    return false;
1345 	}
1346     }
1347 
1348   obj_raw_syment_count (abfd) = written;
1349 
1350   /* Now write out strings.
1351 
1352      We would normally not write anything here if there are no strings, but
1353      we'll write out 4 so that any stupid coff reader which tries to read the
1354      string table even when there isn't one won't croak.  */
1355   {
1356     bfd_byte buffer[STRING_SIZE_SIZE];
1357 
1358 #if STRING_SIZE_SIZE == 4
1359     H_PUT_32 (abfd, _bfd_stringtab_size (strtab) + STRING_SIZE_SIZE, buffer);
1360 #else
1361  #error Change H_PUT_32
1362 #endif
1363     if (bfd_bwrite ((void *) buffer, (bfd_size_type) sizeof (buffer), abfd)
1364 	!= sizeof (buffer))
1365       return false;
1366 
1367     if (! _bfd_stringtab_emit (abfd, strtab))
1368       return false;
1369   }
1370 
1371   _bfd_stringtab_free (strtab);
1372 
1373   /* Make sure the .debug section was created to be the correct size.
1374      We should create it ourselves on the fly, but we don't because
1375      BFD won't let us write to any section until we know how large all
1376      the sections are.  We could still do it by making another pass
1377      over the symbols.  FIXME.  */
1378   BFD_ASSERT (debug_string_size == 0
1379 	      || (debug_string_section != (asection *) NULL
1380 		  && (BFD_ALIGN (debug_string_size,
1381 				 1 << debug_string_section->alignment_power)
1382 		      == debug_string_section->size)));
1383 
1384   return true;
1385 }
1386 
1387 bool
coff_write_linenumbers(bfd * abfd)1388 coff_write_linenumbers (bfd *abfd)
1389 {
1390   asection *s;
1391   bfd_size_type linesz;
1392   void * buff;
1393 
1394   linesz = bfd_coff_linesz (abfd);
1395   buff = bfd_alloc (abfd, linesz);
1396   if (!buff)
1397     return false;
1398   for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1399     {
1400       if (s->lineno_count)
1401 	{
1402 	  asymbol **q = abfd->outsymbols;
1403 	  if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
1404 	    return false;
1405 	  /* Find all the linenumbers in this section.  */
1406 	  while (*q)
1407 	    {
1408 	      asymbol *p = *q;
1409 	      if (p->section->output_section == s)
1410 		{
1411 		  alent *l =
1412 		  BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
1413 			    (bfd_asymbol_bfd (p), p));
1414 		  if (l)
1415 		    {
1416 		      /* Found a linenumber entry, output.  */
1417 		      struct internal_lineno out;
1418 
1419 		      memset ((void *) & out, 0, sizeof (out));
1420 		      out.l_lnno = 0;
1421 		      out.l_addr.l_symndx = l->u.offset;
1422 		      bfd_coff_swap_lineno_out (abfd, &out, buff);
1423 		      if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd)
1424 			  != linesz)
1425 			return false;
1426 		      l++;
1427 		      while (l->line_number)
1428 			{
1429 			  out.l_lnno = l->line_number;
1430 			  out.l_addr.l_symndx = l->u.offset;
1431 			  bfd_coff_swap_lineno_out (abfd, &out, buff);
1432 			  if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd)
1433 			      != linesz)
1434 			    return false;
1435 			  l++;
1436 			}
1437 		    }
1438 		}
1439 	      q++;
1440 	    }
1441 	}
1442     }
1443   bfd_release (abfd, buff);
1444   return true;
1445 }
1446 
1447 alent *
coff_get_lineno(bfd * ignore_abfd ATTRIBUTE_UNUSED,asymbol * symbol)1448 coff_get_lineno (bfd *ignore_abfd ATTRIBUTE_UNUSED, asymbol *symbol)
1449 {
1450   return coffsymbol (symbol)->lineno;
1451 }
1452 
1453 /* This function transforms the offsets into the symbol table into
1454    pointers to syments.  */
1455 
1456 static void
coff_pointerize_aux(bfd * abfd,combined_entry_type * table_base,combined_entry_type * symbol,unsigned int indaux,combined_entry_type * auxent,combined_entry_type * table_end)1457 coff_pointerize_aux (bfd *abfd,
1458 		     combined_entry_type *table_base,
1459 		     combined_entry_type *symbol,
1460 		     unsigned int indaux,
1461 		     combined_entry_type *auxent,
1462 		     combined_entry_type *table_end)
1463 {
1464   unsigned int type = symbol->u.syment.n_type;
1465   unsigned int n_sclass = symbol->u.syment.n_sclass;
1466 
1467   BFD_ASSERT (symbol->is_sym);
1468   if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1469     {
1470       if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1471 	  (abfd, table_base, symbol, indaux, auxent))
1472 	return;
1473     }
1474 
1475   /* Don't bother if this is a file or a section.  */
1476   if (n_sclass == C_STAT && type == T_NULL)
1477     return;
1478   if (n_sclass == C_FILE)
1479     return;
1480   if (n_sclass == C_DWARF)
1481     return;
1482 
1483   BFD_ASSERT (! auxent->is_sym);
1484   /* Otherwise patch up.  */
1485 #define N_TMASK coff_data  (abfd)->local_n_tmask
1486 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1487 
1488   if ((ISFCN (type) || ISTAG (n_sclass) || n_sclass == C_BLOCK
1489        || n_sclass == C_FCN)
1490       && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l > 0
1491       && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l
1492       < (long) obj_raw_syment_count (abfd)
1493       && table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l
1494       < table_end)
1495     {
1496       auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
1497 	table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
1498       auxent->fix_end = 1;
1499     }
1500 
1501   /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1502      generate one, so we must be careful to ignore it.  */
1503   if ((unsigned long) auxent->u.auxent.x_sym.x_tagndx.l
1504       < obj_raw_syment_count (abfd)
1505       && table_base + auxent->u.auxent.x_sym.x_tagndx.l < table_end)
1506     {
1507       auxent->u.auxent.x_sym.x_tagndx.p =
1508 	table_base + auxent->u.auxent.x_sym.x_tagndx.l;
1509       auxent->fix_tag = 1;
1510     }
1511 }
1512 
1513 /* Allocate space for the ".debug" section, and read it.
1514    We did not read the debug section until now, because
1515    we didn't want to go to the trouble until someone needed it.  */
1516 
1517 static char *
build_debug_section(bfd * abfd,asection ** sect_return)1518 build_debug_section (bfd *abfd, asection ** sect_return)
1519 {
1520   char *debug_section;
1521   file_ptr position;
1522   bfd_size_type sec_size;
1523 
1524   asection *sect = bfd_get_section_by_name (abfd, ".debug");
1525 
1526   if (!sect)
1527     {
1528       bfd_set_error (bfd_error_no_debug_section);
1529       return NULL;
1530     }
1531 
1532   /* Seek to the beginning of the `.debug' section and read it.
1533      Save the current position first; it is needed by our caller.
1534      Then read debug section and reset the file pointer.  */
1535 
1536   position = bfd_tell (abfd);
1537   if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0)
1538     return NULL;
1539 
1540   sec_size = sect->size;
1541   debug_section = (char *) _bfd_alloc_and_read (abfd, sec_size, sec_size);
1542   if (debug_section == NULL)
1543     return NULL;
1544 
1545   if (bfd_seek (abfd, position, SEEK_SET) != 0)
1546     return NULL;
1547 
1548   * sect_return = sect;
1549   return debug_section;
1550 }
1551 
1552 /* Return a pointer to a malloc'd copy of 'name'.  'name' may not be
1553    \0-terminated, but will not exceed 'maxlen' characters.  The copy *will*
1554    be \0-terminated.  */
1555 
1556 static char *
copy_name(bfd * abfd,char * name,size_t maxlen)1557 copy_name (bfd *abfd, char *name, size_t maxlen)
1558 {
1559   size_t len;
1560   char *newname;
1561 
1562   for (len = 0; len < maxlen; ++len)
1563     if (name[len] == '\0')
1564       break;
1565 
1566   if ((newname = (char *) bfd_alloc (abfd, (bfd_size_type) len + 1)) == NULL)
1567     return NULL;
1568 
1569   strncpy (newname, name, len);
1570   newname[len] = '\0';
1571   return newname;
1572 }
1573 
1574 /* Read in the external symbols.  */
1575 
1576 bool
_bfd_coff_get_external_symbols(bfd * abfd)1577 _bfd_coff_get_external_symbols (bfd *abfd)
1578 {
1579   size_t symesz;
1580   size_t size;
1581   void * syms;
1582 
1583   if (obj_coff_external_syms (abfd) != NULL)
1584     return true;
1585 
1586   symesz = bfd_coff_symesz (abfd);
1587   if (_bfd_mul_overflow (obj_raw_syment_count (abfd), symesz, &size))
1588     {
1589       bfd_set_error (bfd_error_file_truncated);
1590       return false;
1591     }
1592 
1593   if (size == 0)
1594     return true;
1595 
1596   if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1597     return false;
1598   syms = _bfd_malloc_and_read (abfd, size, size);
1599   obj_coff_external_syms (abfd) = syms;
1600   return syms != NULL;
1601 }
1602 
1603 /* Read in the external strings.  The strings are not loaded until
1604    they are needed.  This is because we have no simple way of
1605    detecting a missing string table in an archive.  If the strings
1606    are loaded then the STRINGS and STRINGS_LEN fields in the
1607    coff_tdata structure will be set.  */
1608 
1609 const char *
_bfd_coff_read_string_table(bfd * abfd)1610 _bfd_coff_read_string_table (bfd *abfd)
1611 {
1612   char extstrsize[STRING_SIZE_SIZE];
1613   bfd_size_type strsize;
1614   char *strings;
1615   ufile_ptr pos;
1616   ufile_ptr filesize;
1617   size_t symesz;
1618   size_t size;
1619 
1620   if (obj_coff_strings (abfd) != NULL)
1621     return obj_coff_strings (abfd);
1622 
1623   if (obj_sym_filepos (abfd) == 0)
1624     {
1625       bfd_set_error (bfd_error_no_symbols);
1626       return NULL;
1627     }
1628 
1629   symesz = bfd_coff_symesz (abfd);
1630   pos = obj_sym_filepos (abfd);
1631   if (_bfd_mul_overflow (obj_raw_syment_count (abfd), symesz, &size)
1632       || pos + size < pos)
1633     {
1634       bfd_set_error (bfd_error_file_truncated);
1635       return NULL;
1636     }
1637 
1638   if (bfd_seek (abfd, pos + size, SEEK_SET) != 0)
1639     return NULL;
1640 
1641   if (bfd_bread (extstrsize, (bfd_size_type) sizeof extstrsize, abfd)
1642       != sizeof extstrsize)
1643     {
1644       if (bfd_get_error () != bfd_error_file_truncated)
1645 	return NULL;
1646 
1647       /* There is no string table.  */
1648       strsize = STRING_SIZE_SIZE;
1649     }
1650   else
1651     {
1652 #if STRING_SIZE_SIZE == 4
1653       strsize = H_GET_32 (abfd, extstrsize);
1654 #else
1655  #error Change H_GET_32
1656 #endif
1657     }
1658 
1659   filesize = bfd_get_file_size (abfd);
1660   if (strsize < STRING_SIZE_SIZE
1661       || (filesize != 0 && strsize > filesize))
1662     {
1663       _bfd_error_handler
1664 	/* xgettext: c-format */
1665 	(_("%pB: bad string table size %" PRIu64), abfd, (uint64_t) strsize);
1666       bfd_set_error (bfd_error_bad_value);
1667       return NULL;
1668     }
1669 
1670   strings = (char *) bfd_malloc (strsize + 1);
1671   if (strings == NULL)
1672     return NULL;
1673 
1674   /* PR 17521 file: 079-54929-0.004.
1675      A corrupt file could contain an index that points into the first
1676      STRING_SIZE_SIZE bytes of the string table, so make sure that
1677      they are zero.  */
1678   memset (strings, 0, STRING_SIZE_SIZE);
1679 
1680   if (bfd_bread (strings + STRING_SIZE_SIZE, strsize - STRING_SIZE_SIZE, abfd)
1681       != strsize - STRING_SIZE_SIZE)
1682     {
1683       free (strings);
1684       return NULL;
1685     }
1686 
1687   obj_coff_strings (abfd) = strings;
1688   obj_coff_strings_len (abfd) = strsize;
1689   /* Terminate the string table, just in case.  */
1690   strings[strsize] = 0;
1691   return strings;
1692 }
1693 
1694 /* Free up the external symbols and strings read from a COFF file.  */
1695 
1696 bool
_bfd_coff_free_symbols(bfd * abfd)1697 _bfd_coff_free_symbols (bfd *abfd)
1698 {
1699   if (! bfd_family_coff (abfd))
1700     return false;
1701 
1702   if (obj_coff_external_syms (abfd) != NULL
1703       && ! obj_coff_keep_syms (abfd))
1704     {
1705       free (obj_coff_external_syms (abfd));
1706       obj_coff_external_syms (abfd) = NULL;
1707     }
1708 
1709   if (obj_coff_strings (abfd) != NULL
1710       && ! obj_coff_keep_strings (abfd))
1711     {
1712       free (obj_coff_strings (abfd));
1713       obj_coff_strings (abfd) = NULL;
1714       obj_coff_strings_len (abfd) = 0;
1715     }
1716 
1717   return true;
1718 }
1719 
1720 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1721    knit the symbol names into a normalized form.  By normalized here I
1722    mean that all symbols have an n_offset pointer that points to a null-
1723    terminated string.  */
1724 
1725 combined_entry_type *
coff_get_normalized_symtab(bfd * abfd)1726 coff_get_normalized_symtab (bfd *abfd)
1727 {
1728   combined_entry_type *internal;
1729   combined_entry_type *internal_ptr;
1730   combined_entry_type *symbol_ptr;
1731   combined_entry_type *internal_end;
1732   size_t symesz;
1733   char *raw_src;
1734   char *raw_end;
1735   const char *string_table = NULL;
1736   asection * debug_sec = NULL;
1737   char *debug_sec_data = NULL;
1738   bfd_size_type size;
1739 
1740   if (obj_raw_syments (abfd) != NULL)
1741     return obj_raw_syments (abfd);
1742 
1743   if (! _bfd_coff_get_external_symbols (abfd))
1744     return NULL;
1745 
1746   size = obj_raw_syment_count (abfd);
1747   /* Check for integer overflow.  */
1748   if (size > (bfd_size_type) -1 / sizeof (combined_entry_type))
1749     return NULL;
1750   size *= sizeof (combined_entry_type);
1751   internal = (combined_entry_type *) bfd_zalloc (abfd, size);
1752   if (internal == NULL && size != 0)
1753     return NULL;
1754   internal_end = internal + obj_raw_syment_count (abfd);
1755 
1756   raw_src = (char *) obj_coff_external_syms (abfd);
1757 
1758   /* Mark the end of the symbols.  */
1759   symesz = bfd_coff_symesz (abfd);
1760   raw_end = PTR_ADD (raw_src, obj_raw_syment_count (abfd) * symesz);
1761 
1762   /* FIXME SOMEDAY.  A string table size of zero is very weird, but
1763      probably possible.  If one shows up, it will probably kill us.  */
1764 
1765   /* Swap all the raw entries.  */
1766   for (internal_ptr = internal;
1767        raw_src < raw_end;
1768        raw_src += symesz, internal_ptr++)
1769     {
1770       unsigned int i;
1771 
1772       bfd_coff_swap_sym_in (abfd, (void *) raw_src,
1773 			    (void *) & internal_ptr->u.syment);
1774       symbol_ptr = internal_ptr;
1775       internal_ptr->is_sym = true;
1776 
1777       /* PR 17512: Prevent buffer overrun.  */
1778       if (symbol_ptr->u.syment.n_numaux > ((raw_end - 1) - raw_src) / symesz)
1779 	{
1780 	  bfd_release (abfd, internal);
1781 	  return NULL;
1782 	}
1783 
1784       for (i = 0;
1785 	   i < symbol_ptr->u.syment.n_numaux;
1786 	   i++)
1787 	{
1788 	  internal_ptr++;
1789 	  raw_src += symesz;
1790 
1791 	  bfd_coff_swap_aux_in (abfd, (void *) raw_src,
1792 				symbol_ptr->u.syment.n_type,
1793 				symbol_ptr->u.syment.n_sclass,
1794 				(int) i, symbol_ptr->u.syment.n_numaux,
1795 				&(internal_ptr->u.auxent));
1796 
1797 	  internal_ptr->is_sym = false;
1798 	  coff_pointerize_aux (abfd, internal, symbol_ptr, i,
1799 			       internal_ptr, internal_end);
1800 	}
1801     }
1802 
1803   /* Free the raw symbols.  */
1804   if (obj_coff_external_syms (abfd) != NULL
1805       && ! obj_coff_keep_syms (abfd))
1806     {
1807       free (obj_coff_external_syms (abfd));
1808       obj_coff_external_syms (abfd) = NULL;
1809     }
1810 
1811   for (internal_ptr = internal; internal_ptr < internal_end;
1812        internal_ptr++)
1813     {
1814       BFD_ASSERT (internal_ptr->is_sym);
1815 
1816       if (internal_ptr->u.syment.n_sclass == C_FILE
1817 	  && internal_ptr->u.syment.n_numaux > 0)
1818 	{
1819 	  combined_entry_type * aux = internal_ptr + 1;
1820 
1821 	  /* Make a file symbol point to the name in the auxent, since
1822 	     the text ".file" is redundant.  */
1823 	  BFD_ASSERT (! aux->is_sym);
1824 
1825 	  if (aux->u.auxent.x_file.x_n.x_n.x_zeroes == 0)
1826 	    {
1827 	      /* The filename is a long one, point into the string table.  */
1828 	      if (string_table == NULL)
1829 		{
1830 		  string_table = _bfd_coff_read_string_table (abfd);
1831 		  if (string_table == NULL)
1832 		    return NULL;
1833 		}
1834 
1835 	      if ((bfd_size_type)(aux->u.auxent.x_file.x_n.x_n.x_offset)
1836 		  >= obj_coff_strings_len (abfd))
1837 		internal_ptr->u.syment._n._n_n._n_offset =
1838 		  (uintptr_t) _("<corrupt>");
1839 	      else
1840 		internal_ptr->u.syment._n._n_n._n_offset =
1841 		  (uintptr_t) (string_table
1842 			       + aux->u.auxent.x_file.x_n.x_n.x_offset);
1843 	    }
1844 	  else
1845 	    {
1846 	      /* Ordinary short filename, put into memory anyway.  The
1847 		 Microsoft PE tools sometimes store a filename in
1848 		 multiple AUX entries.  */
1849 	      if (internal_ptr->u.syment.n_numaux > 1
1850 		  && coff_data (abfd)->pe)
1851 		internal_ptr->u.syment._n._n_n._n_offset =
1852 		  ((uintptr_t)
1853 		   copy_name (abfd,
1854 			      aux->u.auxent.x_file.x_n.x_fname,
1855 			      internal_ptr->u.syment.n_numaux * symesz));
1856 	      else
1857 		internal_ptr->u.syment._n._n_n._n_offset =
1858 		  ((uintptr_t)
1859 		   copy_name (abfd,
1860 			      aux->u.auxent.x_file.x_n.x_fname,
1861 			      (size_t) bfd_coff_filnmlen (abfd)));
1862 	    }
1863 
1864 	  /* Normalize other strings available in C_FILE aux entries.  */
1865 	  if (!coff_data (abfd)->pe)
1866 	    for (int numaux = 1; numaux < internal_ptr->u.syment.n_numaux; numaux++)
1867 	      {
1868 		aux = internal_ptr + numaux + 1;
1869 		BFD_ASSERT (! aux->is_sym);
1870 
1871 		if (aux->u.auxent.x_file.x_n.x_n.x_zeroes == 0)
1872 		  {
1873 		    /* The string information is a long one, point into the string table.  */
1874 		    if (string_table == NULL)
1875 		      {
1876 			string_table = _bfd_coff_read_string_table (abfd);
1877 			if (string_table == NULL)
1878 			  return NULL;
1879 		      }
1880 
1881 		    if ((bfd_size_type)(aux->u.auxent.x_file.x_n.x_n.x_offset)
1882 			>= obj_coff_strings_len (abfd))
1883 		      aux->u.auxent.x_file.x_n.x_n.x_offset =
1884 			(uintptr_t) _("<corrupt>");
1885 		    else
1886 		      aux->u.auxent.x_file.x_n.x_n.x_offset =
1887 			(uintptr_t) (string_table
1888 				     + (aux->u.auxent.x_file.x_n.x_n.x_offset));
1889 		  }
1890 		else
1891 		  aux->u.auxent.x_file.x_n.x_n.x_offset =
1892 		    ((uintptr_t)
1893 		     copy_name (abfd,
1894 				aux->u.auxent.x_file.x_n.x_fname,
1895 				(size_t) bfd_coff_filnmlen (abfd)));
1896 	      }
1897 
1898 	}
1899       else
1900 	{
1901 	  if (internal_ptr->u.syment._n._n_n._n_zeroes != 0)
1902 	    {
1903 	      /* This is a "short" name.  Make it long.  */
1904 	      size_t i;
1905 	      char *newstring;
1906 
1907 	      /* Find the length of this string without walking into memory
1908 		 that isn't ours.  */
1909 	      for (i = 0; i < 8; ++i)
1910 		if (internal_ptr->u.syment._n._n_name[i] == '\0')
1911 		  break;
1912 
1913 	      newstring = (char *) bfd_zalloc (abfd, (bfd_size_type) (i + 1));
1914 	      if (newstring == NULL)
1915 		return NULL;
1916 	      strncpy (newstring, internal_ptr->u.syment._n._n_name, i);
1917 	      internal_ptr->u.syment._n._n_n._n_offset = (uintptr_t) newstring;
1918 	      internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1919 	    }
1920 	  else if (internal_ptr->u.syment._n._n_n._n_offset == 0)
1921 	    internal_ptr->u.syment._n._n_n._n_offset = (uintptr_t) "";
1922 	  else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment))
1923 	    {
1924 	      /* Long name already.  Point symbol at the string in the
1925 		 table.  */
1926 	      if (string_table == NULL)
1927 		{
1928 		  string_table = _bfd_coff_read_string_table (abfd);
1929 		  if (string_table == NULL)
1930 		    return NULL;
1931 		}
1932 	      if (internal_ptr->u.syment._n._n_n._n_offset >= obj_coff_strings_len (abfd)
1933 		  || string_table + internal_ptr->u.syment._n._n_n._n_offset < string_table)
1934 		internal_ptr->u.syment._n._n_n._n_offset =
1935 		  (uintptr_t) _("<corrupt>");
1936 	      else
1937 		internal_ptr->u.syment._n._n_n._n_offset =
1938 		  ((uintptr_t) (string_table
1939 				+ internal_ptr->u.syment._n._n_n._n_offset));
1940 	    }
1941 	  else
1942 	    {
1943 	      /* Long name in debug section.  Very similar.  */
1944 	      if (debug_sec_data == NULL)
1945 		debug_sec_data = build_debug_section (abfd, & debug_sec);
1946 	      if (debug_sec_data != NULL)
1947 		{
1948 		  BFD_ASSERT (debug_sec != NULL);
1949 		  /* PR binutils/17512: Catch out of range offsets into the debug data.  */
1950 		  if (internal_ptr->u.syment._n._n_n._n_offset > debug_sec->size
1951 		      || debug_sec_data + internal_ptr->u.syment._n._n_n._n_offset < debug_sec_data)
1952 		    internal_ptr->u.syment._n._n_n._n_offset =
1953 		      (uintptr_t) _("<corrupt>");
1954 		  else
1955 		    internal_ptr->u.syment._n._n_n._n_offset =
1956 		      (uintptr_t) (debug_sec_data
1957 				   + internal_ptr->u.syment._n._n_n._n_offset);
1958 		}
1959 	      else
1960 		internal_ptr->u.syment._n._n_n._n_offset = (uintptr_t) "";
1961 	    }
1962 	}
1963       internal_ptr += internal_ptr->u.syment.n_numaux;
1964     }
1965 
1966   obj_raw_syments (abfd) = internal;
1967   BFD_ASSERT (obj_raw_syment_count (abfd)
1968 	      == (unsigned int) (internal_ptr - internal));
1969 
1970   return internal;
1971 }
1972 
1973 long
coff_get_reloc_upper_bound(bfd * abfd,sec_ptr asect)1974 coff_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
1975 {
1976   if (bfd_get_format (abfd) != bfd_object)
1977     {
1978       bfd_set_error (bfd_error_invalid_operation);
1979       return -1;
1980     }
1981 #if SIZEOF_LONG == SIZEOF_INT
1982   if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
1983     {
1984       bfd_set_error (bfd_error_file_too_big);
1985       return -1;
1986     }
1987 #endif
1988   return (asect->reloc_count + 1L) * sizeof (arelent *);
1989 }
1990 
1991 asymbol *
coff_make_empty_symbol(bfd * abfd)1992 coff_make_empty_symbol (bfd *abfd)
1993 {
1994   size_t amt = sizeof (coff_symbol_type);
1995   coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_zalloc (abfd, amt);
1996 
1997   if (new_symbol == NULL)
1998     return NULL;
1999   new_symbol->symbol.section = 0;
2000   new_symbol->native = NULL;
2001   new_symbol->lineno = NULL;
2002   new_symbol->done_lineno = false;
2003   new_symbol->symbol.the_bfd = abfd;
2004 
2005   return & new_symbol->symbol;
2006 }
2007 
2008 /* Make a debugging symbol.  */
2009 
2010 asymbol *
coff_bfd_make_debug_symbol(bfd * abfd,void * ptr ATTRIBUTE_UNUSED,unsigned long sz ATTRIBUTE_UNUSED)2011 coff_bfd_make_debug_symbol (bfd *abfd,
2012 			    void * ptr ATTRIBUTE_UNUSED,
2013 			    unsigned long sz ATTRIBUTE_UNUSED)
2014 {
2015   size_t amt = sizeof (coff_symbol_type);
2016   coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_alloc (abfd, amt);
2017 
2018   if (new_symbol == NULL)
2019     return NULL;
2020   /* @@ The 10 is a guess at a plausible maximum number of aux entries
2021      (but shouldn't be a constant).  */
2022   amt = sizeof (combined_entry_type) * 10;
2023   new_symbol->native = (combined_entry_type *) bfd_zalloc (abfd, amt);
2024   if (!new_symbol->native)
2025     return NULL;
2026   new_symbol->native->is_sym = true;
2027   new_symbol->symbol.section = bfd_abs_section_ptr;
2028   new_symbol->symbol.flags = BSF_DEBUGGING;
2029   new_symbol->lineno = NULL;
2030   new_symbol->done_lineno = false;
2031   new_symbol->symbol.the_bfd = abfd;
2032 
2033   return & new_symbol->symbol;
2034 }
2035 
2036 void
coff_get_symbol_info(bfd * abfd,asymbol * symbol,symbol_info * ret)2037 coff_get_symbol_info (bfd *abfd, asymbol *symbol, symbol_info *ret)
2038 {
2039   bfd_symbol_info (symbol, ret);
2040 
2041   if (coffsymbol (symbol)->native != NULL
2042       && coffsymbol (symbol)->native->fix_value
2043       && coffsymbol (symbol)->native->is_sym)
2044     ret->value
2045       = (((uintptr_t) coffsymbol (symbol)->native->u.syment.n_value
2046 	  - (uintptr_t) obj_raw_syments (abfd))
2047 	 / sizeof (combined_entry_type));
2048 }
2049 
2050 /* Print out information about COFF symbol.  */
2051 
2052 void
coff_print_symbol(bfd * abfd,void * filep,asymbol * symbol,bfd_print_symbol_type how)2053 coff_print_symbol (bfd *abfd,
2054 		   void * filep,
2055 		   asymbol *symbol,
2056 		   bfd_print_symbol_type how)
2057 {
2058   FILE * file = (FILE *) filep;
2059 
2060   switch (how)
2061     {
2062     case bfd_print_symbol_name:
2063       fprintf (file, "%s", symbol->name);
2064       break;
2065 
2066     case bfd_print_symbol_more:
2067       fprintf (file, "coff %s %s",
2068 	       coffsymbol (symbol)->native ? "n" : "g",
2069 	       coffsymbol (symbol)->lineno ? "l" : " ");
2070       break;
2071 
2072     case bfd_print_symbol_all:
2073       if (coffsymbol (symbol)->native)
2074 	{
2075 	  bfd_vma val;
2076 	  unsigned int aux;
2077 	  combined_entry_type *combined = coffsymbol (symbol)->native;
2078 	  combined_entry_type *root = obj_raw_syments (abfd);
2079 	  struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
2080 
2081 	  fprintf (file, "[%3ld]", (long) (combined - root));
2082 
2083 	  /* PR 17512: file: 079-33786-0.001:0.1.  */
2084 	  if (combined < obj_raw_syments (abfd)
2085 	      || combined >= obj_raw_syments (abfd) + obj_raw_syment_count (abfd))
2086 	    {
2087 	      fprintf (file, _("<corrupt info> %s"), symbol->name);
2088 	      break;
2089 	    }
2090 
2091 	  BFD_ASSERT (combined->is_sym);
2092 	  if (! combined->fix_value)
2093 	    val = (bfd_vma) combined->u.syment.n_value;
2094 	  else
2095 	    val = (((uintptr_t) combined->u.syment.n_value - (uintptr_t) root)
2096 		   / sizeof (combined_entry_type));
2097 
2098 	  fprintf (file, "(sec %2d)(fl 0x%02x)(ty %4x)(scl %3d) (nx %d) 0x",
2099 		   combined->u.syment.n_scnum,
2100 		   combined->u.syment.n_flags,
2101 		   combined->u.syment.n_type,
2102 		   combined->u.syment.n_sclass,
2103 		   combined->u.syment.n_numaux);
2104 	  bfd_fprintf_vma (abfd, file, val);
2105 	  fprintf (file, " %s", symbol->name);
2106 
2107 	  for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
2108 	    {
2109 	      combined_entry_type *auxp = combined + aux + 1;
2110 	      long tagndx;
2111 
2112 	      BFD_ASSERT (! auxp->is_sym);
2113 	      if (auxp->fix_tag)
2114 		tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
2115 	      else
2116 		tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
2117 
2118 	      fprintf (file, "\n");
2119 
2120 	      if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux))
2121 		continue;
2122 
2123 	      switch (combined->u.syment.n_sclass)
2124 		{
2125 		case C_FILE:
2126 		  fprintf (file, "File ");
2127 		  /* Add additional information if this isn't the filename
2128 		     auxiliary entry.  */
2129 		  if (auxp->u.auxent.x_file.x_ftype)
2130 		    fprintf (file, "ftype %d fname \"%s\"",
2131 			     auxp->u.auxent.x_file.x_ftype,
2132 			     (char *) auxp->u.auxent.x_file.x_n.x_n.x_offset);
2133 		  break;
2134 
2135 		case C_DWARF:
2136 		  fprintf (file, "AUX scnlen 0x%lx nreloc %ld",
2137 			   (unsigned long) auxp->u.auxent.x_sect.x_scnlen,
2138 			   auxp->u.auxent.x_sect.x_nreloc);
2139 		  break;
2140 
2141 		case C_STAT:
2142 		  if (combined->u.syment.n_type == T_NULL)
2143 		    /* Probably a section symbol ?  */
2144 		    {
2145 		      fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
2146 			       (unsigned long) auxp->u.auxent.x_scn.x_scnlen,
2147 			       auxp->u.auxent.x_scn.x_nreloc,
2148 			       auxp->u.auxent.x_scn.x_nlinno);
2149 		      if (auxp->u.auxent.x_scn.x_checksum != 0
2150 			  || auxp->u.auxent.x_scn.x_associated != 0
2151 			  || auxp->u.auxent.x_scn.x_comdat != 0)
2152 			fprintf (file, " checksum 0x%lx assoc %d comdat %d",
2153 				 auxp->u.auxent.x_scn.x_checksum,
2154 				 auxp->u.auxent.x_scn.x_associated,
2155 				 auxp->u.auxent.x_scn.x_comdat);
2156 		      break;
2157 		    }
2158 		  /* Fall through.  */
2159 		case C_EXT:
2160 		case C_AIX_WEAKEXT:
2161 		  if (ISFCN (combined->u.syment.n_type))
2162 		    {
2163 		      long next, llnos;
2164 
2165 		      if (auxp->fix_end)
2166 			next = (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2167 			       - root);
2168 		      else
2169 			next = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
2170 		      llnos = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_lnnoptr;
2171 		      fprintf (file,
2172 			       "AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld",
2173 			       tagndx,
2174 			       (unsigned long) auxp->u.auxent.x_sym.x_misc.x_fsize,
2175 			       llnos, next);
2176 		      break;
2177 		    }
2178 		  /* Fall through.  */
2179 		default:
2180 		  fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
2181 			   auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
2182 			   auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
2183 			   tagndx);
2184 		  if (auxp->fix_end)
2185 		    fprintf (file, " endndx %ld",
2186 			     ((long)
2187 			      (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2188 			       - root)));
2189 		  break;
2190 		}
2191 	    }
2192 
2193 	  if (l)
2194 	    {
2195 	      fprintf (file, "\n%s :", l->u.sym->name);
2196 	      l++;
2197 	      while (l->line_number)
2198 		{
2199 		  if (l->line_number > 0)
2200 		    {
2201 		      fprintf (file, "\n%4d : ", l->line_number);
2202 		      bfd_fprintf_vma (abfd, file, l->u.offset + symbol->section->vma);
2203 		    }
2204 		  l++;
2205 		}
2206 	    }
2207 	}
2208       else
2209 	{
2210 	  bfd_print_symbol_vandf (abfd, (void *) file, symbol);
2211 	  fprintf (file, " %-5s %s %s %s",
2212 		   symbol->section->name,
2213 		   coffsymbol (symbol)->native ? "n" : "g",
2214 		   coffsymbol (symbol)->lineno ? "l" : " ",
2215 		   symbol->name);
2216 	}
2217     }
2218 }
2219 
2220 /* Return whether a symbol name implies a local symbol.  In COFF,
2221    local symbols generally start with ``.L''.  Most targets use this
2222    function for the is_local_label_name entry point, but some may
2223    override it.  */
2224 
2225 bool
_bfd_coff_is_local_label_name(bfd * abfd ATTRIBUTE_UNUSED,const char * name)2226 _bfd_coff_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
2227 			       const char *name)
2228 {
2229   return name[0] == '.' && name[1] == 'L';
2230 }
2231 
2232 /* Provided a BFD, a section and an offset (in bytes, not octets) into the
2233    section, calculate and return the name of the source file and the line
2234    nearest to the wanted location.  */
2235 
2236 bool
coff_find_nearest_line_with_names(bfd * abfd,asymbol ** symbols,asection * section,bfd_vma offset,const char ** filename_ptr,const char ** functionname_ptr,unsigned int * line_ptr,const struct dwarf_debug_section * debug_sections)2237 coff_find_nearest_line_with_names (bfd *abfd,
2238 				   asymbol **symbols,
2239 				   asection *section,
2240 				   bfd_vma offset,
2241 				   const char **filename_ptr,
2242 				   const char **functionname_ptr,
2243 				   unsigned int *line_ptr,
2244 				   const struct dwarf_debug_section *debug_sections)
2245 {
2246   bool found;
2247   unsigned int i;
2248   unsigned int line_base;
2249   coff_data_type *cof = coff_data (abfd);
2250   /* Run through the raw syments if available.  */
2251   combined_entry_type *p;
2252   combined_entry_type *pend;
2253   alent *l;
2254   struct coff_section_tdata *sec_data;
2255   size_t amt;
2256 
2257   /* Before looking through the symbol table, try to use a .stab
2258      section to find the information.  */
2259   if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
2260 					     &found, filename_ptr,
2261 					     functionname_ptr, line_ptr,
2262 					     &coff_data(abfd)->line_info))
2263     return false;
2264 
2265   if (found)
2266     return true;
2267 
2268   /* Also try examining DWARF2 debugging information.  */
2269   if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
2270 				     filename_ptr, functionname_ptr,
2271 				     line_ptr, NULL, debug_sections,
2272 				     &coff_data(abfd)->dwarf2_find_line_info))
2273     return true;
2274 
2275   sec_data = coff_section_data (abfd, section);
2276 
2277   /* If the DWARF lookup failed, but there is DWARF information available
2278      then the problem might be that the file has been rebased.  This tool
2279      changes the VMAs of all the sections, but it does not update the DWARF
2280      information.  So try again, using a bias against the address sought.  */
2281   if (coff_data (abfd)->dwarf2_find_line_info != NULL)
2282     {
2283       bfd_signed_vma bias = 0;
2284 
2285       /* Create a cache of the result for the next call.  */
2286       if (sec_data == NULL && section->owner == abfd)
2287 	{
2288 	  amt = sizeof (struct coff_section_tdata);
2289 	  section->used_by_bfd = bfd_zalloc (abfd, amt);
2290 	  sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2291 	}
2292 
2293       if (sec_data != NULL && sec_data->saved_bias)
2294 	bias = sec_data->bias;
2295       else if (symbols)
2296 	{
2297 	  bias = _bfd_dwarf2_find_symbol_bias (symbols,
2298 					       & coff_data (abfd)->dwarf2_find_line_info);
2299 
2300 	  if (sec_data)
2301 	    {
2302 	      sec_data->saved_bias = true;
2303 	      sec_data->bias = bias;
2304 	    }
2305 	}
2306 
2307       if (bias
2308 	  && _bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section,
2309 					    offset + bias,
2310 					    filename_ptr, functionname_ptr,
2311 					    line_ptr, NULL, debug_sections,
2312 					    &coff_data(abfd)->dwarf2_find_line_info))
2313 	return true;
2314     }
2315 
2316   *filename_ptr = 0;
2317   *functionname_ptr = 0;
2318   *line_ptr = 0;
2319 
2320   /* Don't try and find line numbers in a non coff file.  */
2321   if (!bfd_family_coff (abfd))
2322     return false;
2323 
2324   if (cof == NULL)
2325     return false;
2326 
2327   /* Find the first C_FILE symbol.  */
2328   p = cof->raw_syments;
2329   if (!p)
2330     return false;
2331 
2332   pend = p + cof->raw_syment_count;
2333   while (p < pend)
2334     {
2335       BFD_ASSERT (p->is_sym);
2336       if (p->u.syment.n_sclass == C_FILE)
2337 	break;
2338       p += 1 + p->u.syment.n_numaux;
2339     }
2340 
2341   if (p < pend)
2342     {
2343       bfd_vma sec_vma;
2344       bfd_vma maxdiff;
2345 
2346       /* Look through the C_FILE symbols to find the best one.  */
2347       sec_vma = bfd_section_vma (section);
2348       *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2349       maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
2350       while (1)
2351 	{
2352 	  bfd_vma file_addr;
2353 	  combined_entry_type *p2;
2354 
2355 	  for (p2 = p + 1 + p->u.syment.n_numaux;
2356 	       p2 < pend;
2357 	       p2 += 1 + p2->u.syment.n_numaux)
2358 	    {
2359 	      BFD_ASSERT (p2->is_sym);
2360 	      if (p2->u.syment.n_scnum > 0
2361 		  && (section
2362 		      == coff_section_from_bfd_index (abfd,
2363 						      p2->u.syment.n_scnum)))
2364 		break;
2365 	      if (p2->u.syment.n_sclass == C_FILE)
2366 		{
2367 		  p2 = pend;
2368 		  break;
2369 		}
2370 	    }
2371 	  if (p2 >= pend)
2372 	    break;
2373 
2374 	  file_addr = (bfd_vma) p2->u.syment.n_value;
2375 	  /* PR 11512: Include the section address of the function name symbol.  */
2376 	  if (p2->u.syment.n_scnum > 0)
2377 	    file_addr += coff_section_from_bfd_index (abfd,
2378 						      p2->u.syment.n_scnum)->vma;
2379 	  /* We use <= MAXDIFF here so that if we get a zero length
2380 	     file, we actually use the next file entry.  */
2381 	  if (p2 < pend
2382 	      && offset + sec_vma >= file_addr
2383 	      && offset + sec_vma - file_addr <= maxdiff)
2384 	    {
2385 	      *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2386 	      maxdiff = offset + sec_vma - p2->u.syment.n_value;
2387 	    }
2388 
2389 	  if (p->u.syment.n_value >= cof->raw_syment_count)
2390 	    break;
2391 
2392 	  /* Avoid endless loops on erroneous files by ensuring that
2393 	     we always move forward in the file.  */
2394 	  if (p >= cof->raw_syments + p->u.syment.n_value)
2395 	    break;
2396 
2397 	  p = cof->raw_syments + p->u.syment.n_value;
2398 	  if (!p->is_sym || p->u.syment.n_sclass != C_FILE)
2399 	    break;
2400 	}
2401     }
2402 
2403   if (section->lineno_count == 0)
2404     {
2405       *functionname_ptr = NULL;
2406       *line_ptr = 0;
2407       return true;
2408     }
2409 
2410   /* Now wander though the raw linenumbers of the section.
2411      If we have been called on this section before, and the offset
2412      we want is further down then we can prime the lookup loop.  */
2413   if (sec_data != NULL
2414       && sec_data->i > 0
2415       && offset >= sec_data->offset)
2416     {
2417       i = sec_data->i;
2418       *functionname_ptr = sec_data->function;
2419       line_base = sec_data->line_base;
2420     }
2421   else
2422     {
2423       i = 0;
2424       line_base = 0;
2425     }
2426 
2427   if (section->lineno != NULL)
2428     {
2429       bfd_vma last_value = 0;
2430 
2431       l = &section->lineno[i];
2432 
2433       for (; i < section->lineno_count; i++)
2434 	{
2435 	  if (l->line_number == 0)
2436 	    {
2437 	      /* Get the symbol this line number points at.  */
2438 	      coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
2439 	      if (coff->symbol.value > offset)
2440 		break;
2441 
2442 	      *functionname_ptr = coff->symbol.name;
2443 	      last_value = coff->symbol.value;
2444 	      if (coff->native)
2445 		{
2446 		  combined_entry_type *s = coff->native;
2447 
2448 		  BFD_ASSERT (s->is_sym);
2449 		  s = s + 1 + s->u.syment.n_numaux;
2450 
2451 		  /* In XCOFF a debugging symbol can follow the
2452 		     function symbol.  */
2453 		  if (((size_t) ((char *) s - (char *) obj_raw_syments (abfd))
2454 		       < obj_raw_syment_count (abfd) * sizeof (*s))
2455 		      && s->u.syment.n_scnum == N_DEBUG)
2456 		    s = s + 1 + s->u.syment.n_numaux;
2457 
2458 		  /* S should now point to the .bf of the function.  */
2459 		  if (((size_t) ((char *) s - (char *) obj_raw_syments (abfd))
2460 		       < obj_raw_syment_count (abfd) * sizeof (*s))
2461 		      && s->u.syment.n_numaux)
2462 		    {
2463 		      /* The linenumber is stored in the auxent.  */
2464 		      union internal_auxent *a = &((s + 1)->u.auxent);
2465 
2466 		      line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
2467 		      *line_ptr = line_base;
2468 		    }
2469 		}
2470 	    }
2471 	  else
2472 	    {
2473 	      if (l->u.offset > offset)
2474 		break;
2475 	      *line_ptr = l->line_number + line_base - 1;
2476 	    }
2477 	  l++;
2478 	}
2479 
2480       /* If we fell off the end of the loop, then assume that this
2481 	 symbol has no line number info.  Otherwise, symbols with no
2482 	 line number info get reported with the line number of the
2483 	 last line of the last symbol which does have line number
2484 	 info.  We use 0x100 as a slop to account for cases where the
2485 	 last line has executable code.  */
2486       if (i >= section->lineno_count
2487 	  && last_value != 0
2488 	  && offset - last_value > 0x100)
2489 	{
2490 	  *functionname_ptr = NULL;
2491 	  *line_ptr = 0;
2492 	}
2493     }
2494 
2495   /* Cache the results for the next call.  */
2496   if (sec_data == NULL && section->owner == abfd)
2497     {
2498       amt = sizeof (struct coff_section_tdata);
2499       section->used_by_bfd = bfd_zalloc (abfd, amt);
2500       sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2501     }
2502 
2503   if (sec_data != NULL)
2504     {
2505       sec_data->offset = offset;
2506       sec_data->i = i - 1;
2507       sec_data->function = *functionname_ptr;
2508       sec_data->line_base = line_base;
2509     }
2510 
2511   return true;
2512 }
2513 
2514 bool
coff_find_nearest_line(bfd * abfd,asymbol ** symbols,asection * section,bfd_vma offset,const char ** filename_ptr,const char ** functionname_ptr,unsigned int * line_ptr,unsigned int * discriminator_ptr)2515 coff_find_nearest_line (bfd *abfd,
2516 			asymbol **symbols,
2517 			asection *section,
2518 			bfd_vma offset,
2519 			const char **filename_ptr,
2520 			const char **functionname_ptr,
2521 			unsigned int *line_ptr,
2522 			unsigned int *discriminator_ptr)
2523 {
2524   if (discriminator_ptr)
2525     *discriminator_ptr = 0;
2526   return coff_find_nearest_line_with_names (abfd, symbols, section, offset,
2527 					    filename_ptr, functionname_ptr,
2528 					    line_ptr, dwarf_debug_sections);
2529 }
2530 
2531 bool
coff_find_inliner_info(bfd * abfd,const char ** filename_ptr,const char ** functionname_ptr,unsigned int * line_ptr)2532 coff_find_inliner_info (bfd *abfd,
2533 			const char **filename_ptr,
2534 			const char **functionname_ptr,
2535 			unsigned int *line_ptr)
2536 {
2537   bool found;
2538 
2539   found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
2540 					 functionname_ptr, line_ptr,
2541 					 &coff_data(abfd)->dwarf2_find_line_info);
2542   return (found);
2543 }
2544 
2545 int
coff_sizeof_headers(bfd * abfd,struct bfd_link_info * info)2546 coff_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
2547 {
2548   size_t size;
2549 
2550   if (!bfd_link_relocatable (info))
2551     size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
2552   else
2553     size = bfd_coff_filhsz (abfd);
2554 
2555   size += abfd->section_count * bfd_coff_scnhsz (abfd);
2556   return size;
2557 }
2558 
2559 /* Change the class of a coff symbol held by BFD.  */
2560 
2561 bool
bfd_coff_set_symbol_class(bfd * abfd,asymbol * symbol,unsigned int symbol_class)2562 bfd_coff_set_symbol_class (bfd *	 abfd,
2563 			   asymbol *	 symbol,
2564 			   unsigned int	 symbol_class)
2565 {
2566   coff_symbol_type * csym;
2567 
2568   csym = coff_symbol_from (symbol);
2569   if (csym == NULL)
2570     {
2571       bfd_set_error (bfd_error_invalid_operation);
2572       return false;
2573     }
2574   else if (csym->native == NULL)
2575     {
2576       /* This is an alien symbol which no native coff backend data.
2577 	 We cheat here by creating a fake native entry for it and
2578 	 then filling in the class.  This code is based on that in
2579 	 coff_write_alien_symbol().  */
2580 
2581       combined_entry_type * native;
2582       size_t amt = sizeof (* native);
2583 
2584       native = (combined_entry_type *) bfd_zalloc (abfd, amt);
2585       if (native == NULL)
2586 	return false;
2587 
2588       native->is_sym = true;
2589       native->u.syment.n_type   = T_NULL;
2590       native->u.syment.n_sclass = symbol_class;
2591 
2592       if (bfd_is_und_section (symbol->section))
2593 	{
2594 	  native->u.syment.n_scnum = N_UNDEF;
2595 	  native->u.syment.n_value = symbol->value;
2596 	}
2597       else if (bfd_is_com_section (symbol->section))
2598 	{
2599 	  native->u.syment.n_scnum = N_UNDEF;
2600 	  native->u.syment.n_value = symbol->value;
2601 	}
2602       else
2603 	{
2604 	  native->u.syment.n_scnum =
2605 	    symbol->section->output_section->target_index;
2606 	  native->u.syment.n_value = (symbol->value
2607 				      + symbol->section->output_offset);
2608 	  if (! obj_pe (abfd))
2609 	    native->u.syment.n_value += symbol->section->output_section->vma;
2610 
2611 	  /* Copy the any flags from the file header into the symbol.
2612 	     FIXME: Why?  */
2613 	  native->u.syment.n_flags = bfd_asymbol_bfd (& csym->symbol)->flags;
2614 	}
2615 
2616       csym->native = native;
2617     }
2618   else
2619     csym->native->u.syment.n_sclass = symbol_class;
2620 
2621   return true;
2622 }
2623 
2624 bool
_bfd_coff_section_already_linked(bfd * abfd,asection * sec,struct bfd_link_info * info)2625 _bfd_coff_section_already_linked (bfd *abfd,
2626 				  asection *sec,
2627 				  struct bfd_link_info *info)
2628 {
2629   flagword flags;
2630   const char *name, *key;
2631   struct bfd_section_already_linked *l;
2632   struct bfd_section_already_linked_hash_entry *already_linked_list;
2633   struct coff_comdat_info *s_comdat;
2634 
2635   if (sec->output_section == bfd_abs_section_ptr)
2636     return false;
2637 
2638   flags = sec->flags;
2639   if ((flags & SEC_LINK_ONCE) == 0)
2640     return false;
2641 
2642   /* The COFF backend linker doesn't support group sections.  */
2643   if ((flags & SEC_GROUP) != 0)
2644     return false;
2645 
2646   name = bfd_section_name (sec);
2647   s_comdat = bfd_coff_get_comdat_section (abfd, sec);
2648 
2649   if (s_comdat != NULL)
2650     key = s_comdat->name;
2651   else
2652     {
2653       if (startswith (name, ".gnu.linkonce.")
2654 	  && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
2655 	key++;
2656       else
2657 	/* FIXME: gcc as of 2011-09 emits sections like .text$<key>,
2658 	   .xdata$<key> and .pdata$<key> only the first of which has a
2659 	   comdat key.  Should these all match the LTO IR key?  */
2660 	key = name;
2661     }
2662 
2663   already_linked_list = bfd_section_already_linked_table_lookup (key);
2664 
2665   for (l = already_linked_list->entry; l != NULL; l = l->next)
2666     {
2667       struct coff_comdat_info *l_comdat;
2668 
2669       l_comdat = bfd_coff_get_comdat_section (l->sec->owner, l->sec);
2670 
2671       /* The section names must match, and both sections must be
2672 	 comdat and have the same comdat name, or both sections must
2673 	 be non-comdat.  LTO IR plugin sections are an exception.  They
2674 	 are always named .gnu.linkonce.t.<key> (<key> is some string)
2675 	 and match any comdat section with comdat name of <key>, and
2676 	 any linkonce section with the same suffix, ie.
2677 	 .gnu.linkonce.*.<key>.  */
2678       if (((s_comdat != NULL) == (l_comdat != NULL)
2679 	   && strcmp (name, l->sec->name) == 0)
2680 	  || (l->sec->owner->flags & BFD_PLUGIN) != 0
2681 	  || (sec->owner->flags & BFD_PLUGIN) != 0)
2682 	{
2683 	  /* The section has already been linked.  See if we should
2684 	     issue a warning.  */
2685 	  return _bfd_handle_already_linked (sec, l, info);
2686 	}
2687     }
2688 
2689   /* This is the first section with this name.  Record it.  */
2690   if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
2691     info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
2692   return false;
2693 }
2694 
2695 /* Initialize COOKIE for input bfd ABFD. */
2696 
2697 static bool
init_reloc_cookie(struct coff_reloc_cookie * cookie,struct bfd_link_info * info ATTRIBUTE_UNUSED,bfd * abfd)2698 init_reloc_cookie (struct coff_reloc_cookie *cookie,
2699 		   struct bfd_link_info *info ATTRIBUTE_UNUSED,
2700 		   bfd *abfd)
2701 {
2702   /* Sometimes the symbol table does not yet have been loaded here.  */
2703   bfd_coff_slurp_symbol_table (abfd);
2704 
2705   cookie->abfd = abfd;
2706   cookie->sym_hashes = obj_coff_sym_hashes (abfd);
2707 
2708   cookie->symbols = obj_symbols (abfd);
2709 
2710   return true;
2711 }
2712 
2713 /* Free the memory allocated by init_reloc_cookie, if appropriate.  */
2714 
2715 static void
fini_reloc_cookie(struct coff_reloc_cookie * cookie ATTRIBUTE_UNUSED,bfd * abfd ATTRIBUTE_UNUSED)2716 fini_reloc_cookie (struct coff_reloc_cookie *cookie ATTRIBUTE_UNUSED,
2717 		   bfd *abfd ATTRIBUTE_UNUSED)
2718 {
2719   /* Nothing to do.  */
2720 }
2721 
2722 /* Initialize the relocation information in COOKIE for input section SEC
2723    of input bfd ABFD.  */
2724 
2725 static bool
init_reloc_cookie_rels(struct coff_reloc_cookie * cookie,struct bfd_link_info * info ATTRIBUTE_UNUSED,bfd * abfd,asection * sec)2726 init_reloc_cookie_rels (struct coff_reloc_cookie *cookie,
2727 			struct bfd_link_info *info ATTRIBUTE_UNUSED,
2728 			bfd *abfd,
2729 			asection *sec)
2730 {
2731   if (sec->reloc_count == 0)
2732     {
2733       cookie->rels = NULL;
2734       cookie->relend = NULL;
2735       cookie->rel = NULL;
2736       return true;
2737     }
2738 
2739   cookie->rels = _bfd_coff_read_internal_relocs (abfd, sec, false, NULL,
2740 						 0, NULL);
2741 
2742   if (cookie->rels == NULL)
2743     return false;
2744 
2745   cookie->rel = cookie->rels;
2746   cookie->relend = (cookie->rels + sec->reloc_count);
2747   return true;
2748 }
2749 
2750 /* Free the memory allocated by init_reloc_cookie_rels,
2751    if appropriate.  */
2752 
2753 static void
fini_reloc_cookie_rels(struct coff_reloc_cookie * cookie,asection * sec)2754 fini_reloc_cookie_rels (struct coff_reloc_cookie *cookie,
2755 			asection *sec)
2756 {
2757   if (cookie->rels
2758       /* PR 20401.  The relocs may not have been cached, so check first.
2759 	 If the relocs were loaded by init_reloc_cookie_rels() then this
2760 	 will be the case.  FIXME: Would performance be improved if the
2761 	 relocs *were* cached ?  */
2762       && coff_section_data (NULL, sec)
2763       && coff_section_data (NULL, sec)->relocs != cookie->rels)
2764     free (cookie->rels);
2765 }
2766 
2767 /* Initialize the whole of COOKIE for input section SEC.  */
2768 
2769 static bool
init_reloc_cookie_for_section(struct coff_reloc_cookie * cookie,struct bfd_link_info * info,asection * sec)2770 init_reloc_cookie_for_section (struct coff_reloc_cookie *cookie,
2771 			       struct bfd_link_info *info,
2772 			       asection *sec)
2773 {
2774   if (!init_reloc_cookie (cookie, info, sec->owner))
2775     return false;
2776 
2777   if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
2778     {
2779       fini_reloc_cookie (cookie, sec->owner);
2780       return false;
2781     }
2782   return true;
2783 }
2784 
2785 /* Free the memory allocated by init_reloc_cookie_for_section,
2786    if appropriate.  */
2787 
2788 static void
fini_reloc_cookie_for_section(struct coff_reloc_cookie * cookie,asection * sec)2789 fini_reloc_cookie_for_section (struct coff_reloc_cookie *cookie,
2790 			       asection *sec)
2791 {
2792   fini_reloc_cookie_rels (cookie, sec);
2793   fini_reloc_cookie (cookie, sec->owner);
2794 }
2795 
2796 static asection *
_bfd_coff_gc_mark_hook(asection * sec,struct bfd_link_info * info ATTRIBUTE_UNUSED,struct internal_reloc * rel ATTRIBUTE_UNUSED,struct coff_link_hash_entry * h,struct internal_syment * sym)2797 _bfd_coff_gc_mark_hook (asection *sec,
2798 			struct bfd_link_info *info ATTRIBUTE_UNUSED,
2799 			struct internal_reloc *rel ATTRIBUTE_UNUSED,
2800 			struct coff_link_hash_entry *h,
2801 			struct internal_syment *sym)
2802 {
2803   if (h != NULL)
2804     {
2805       switch (h->root.type)
2806 	{
2807 	case bfd_link_hash_defined:
2808 	case bfd_link_hash_defweak:
2809 	  return h->root.u.def.section;
2810 
2811 	case bfd_link_hash_common:
2812 	  return h->root.u.c.p->section;
2813 
2814 	case bfd_link_hash_undefweak:
2815 	  if (h->symbol_class == C_NT_WEAK && h->numaux == 1)
2816 	    {
2817 	      /* PE weak externals.  A weak symbol may include an auxiliary
2818 		 record indicating that if the weak symbol is not resolved,
2819 		 another external symbol is used instead.  */
2820 	      struct coff_link_hash_entry *h2 =
2821 		h->auxbfd->tdata.coff_obj_data->sym_hashes[
2822 		    h->aux->x_sym.x_tagndx.l];
2823 
2824 	      if (h2 && h2->root.type != bfd_link_hash_undefined)
2825 		return  h2->root.u.def.section;
2826 	    }
2827 	  break;
2828 
2829 	case bfd_link_hash_undefined:
2830 	default:
2831 	  break;
2832 	}
2833       return NULL;
2834     }
2835 
2836   return coff_section_from_bfd_index (sec->owner, sym->n_scnum);
2837 }
2838 
2839 /* COOKIE->rel describes a relocation against section SEC, which is
2840    a section we've decided to keep.  Return the section that contains
2841    the relocation symbol, or NULL if no section contains it.  */
2842 
2843 static asection *
_bfd_coff_gc_mark_rsec(struct bfd_link_info * info,asection * sec,coff_gc_mark_hook_fn gc_mark_hook,struct coff_reloc_cookie * cookie)2844 _bfd_coff_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
2845 			coff_gc_mark_hook_fn gc_mark_hook,
2846 			struct coff_reloc_cookie *cookie)
2847 {
2848   struct coff_link_hash_entry *h;
2849 
2850   h = cookie->sym_hashes[cookie->rel->r_symndx];
2851   if (h != NULL)
2852     {
2853       while (h->root.type == bfd_link_hash_indirect
2854 	     || h->root.type == bfd_link_hash_warning)
2855 	h = (struct coff_link_hash_entry *) h->root.u.i.link;
2856 
2857       return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
2858     }
2859 
2860   return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
2861 			  &(cookie->symbols
2862 			    + obj_convert (sec->owner)[cookie->rel->r_symndx])->native->u.syment);
2863 }
2864 
2865 static bool _bfd_coff_gc_mark
2866   (struct bfd_link_info *, asection *, coff_gc_mark_hook_fn);
2867 
2868 /* COOKIE->rel describes a relocation against section SEC, which is
2869    a section we've decided to keep.  Mark the section that contains
2870    the relocation symbol.  */
2871 
2872 static bool
_bfd_coff_gc_mark_reloc(struct bfd_link_info * info,asection * sec,coff_gc_mark_hook_fn gc_mark_hook,struct coff_reloc_cookie * cookie)2873 _bfd_coff_gc_mark_reloc (struct bfd_link_info *info,
2874 			 asection *sec,
2875 			 coff_gc_mark_hook_fn gc_mark_hook,
2876 			 struct coff_reloc_cookie *cookie)
2877 {
2878   asection *rsec;
2879 
2880   rsec = _bfd_coff_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
2881   if (rsec && !rsec->gc_mark)
2882     {
2883       if (bfd_get_flavour (rsec->owner) != bfd_target_coff_flavour)
2884 	rsec->gc_mark = 1;
2885       else if (!_bfd_coff_gc_mark (info, rsec, gc_mark_hook))
2886 	return false;
2887     }
2888   return true;
2889 }
2890 
2891 /* The mark phase of garbage collection.  For a given section, mark
2892    it and any sections in this section's group, and all the sections
2893    which define symbols to which it refers.  */
2894 
2895 static bool
_bfd_coff_gc_mark(struct bfd_link_info * info,asection * sec,coff_gc_mark_hook_fn gc_mark_hook)2896 _bfd_coff_gc_mark (struct bfd_link_info *info,
2897 		   asection *sec,
2898 		   coff_gc_mark_hook_fn gc_mark_hook)
2899 {
2900   bool ret = true;
2901 
2902   sec->gc_mark = 1;
2903 
2904   /* Look through the section relocs.  */
2905   if ((sec->flags & SEC_RELOC) != 0
2906       && sec->reloc_count > 0)
2907     {
2908       struct coff_reloc_cookie cookie;
2909 
2910       if (!init_reloc_cookie_for_section (&cookie, info, sec))
2911 	ret = false;
2912       else
2913 	{
2914 	  for (; cookie.rel < cookie.relend; cookie.rel++)
2915 	    {
2916 	      if (!_bfd_coff_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
2917 		{
2918 		  ret = false;
2919 		  break;
2920 		}
2921 	    }
2922 	  fini_reloc_cookie_for_section (&cookie, sec);
2923 	}
2924     }
2925 
2926   return ret;
2927 }
2928 
2929 static bool
_bfd_coff_gc_mark_extra_sections(struct bfd_link_info * info,coff_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)2930 _bfd_coff_gc_mark_extra_sections (struct bfd_link_info *info,
2931 				  coff_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
2932 {
2933   bfd *ibfd;
2934 
2935   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
2936     {
2937       asection *isec;
2938       bool some_kept;
2939 
2940       if (bfd_get_flavour (ibfd) != bfd_target_coff_flavour)
2941 	continue;
2942 
2943       /* Ensure all linker created sections are kept, and see whether
2944 	 any other section is already marked.  */
2945       some_kept = false;
2946       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
2947 	{
2948 	  if ((isec->flags & SEC_LINKER_CREATED) != 0)
2949 	    isec->gc_mark = 1;
2950 	  else if (isec->gc_mark)
2951 	    some_kept = true;
2952 	}
2953 
2954       /* If no section in this file will be kept, then we can
2955 	 toss out debug sections.  */
2956       if (!some_kept)
2957 	continue;
2958 
2959       /* Keep debug and special sections like .comment when they are
2960 	 not part of a group, or when we have single-member groups.  */
2961       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
2962 	if ((isec->flags & SEC_DEBUGGING) != 0
2963 	    || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
2964 	  isec->gc_mark = 1;
2965     }
2966   return true;
2967 }
2968 
2969 /* Sweep symbols in swept sections.  Called via coff_link_hash_traverse.  */
2970 
2971 static bool
coff_gc_sweep_symbol(struct coff_link_hash_entry * h,void * data ATTRIBUTE_UNUSED)2972 coff_gc_sweep_symbol (struct coff_link_hash_entry *h,
2973 		      void *data ATTRIBUTE_UNUSED)
2974 {
2975   if (h->root.type == bfd_link_hash_warning)
2976     h = (struct coff_link_hash_entry *) h->root.u.i.link;
2977 
2978   if ((h->root.type == bfd_link_hash_defined
2979        || h->root.type == bfd_link_hash_defweak)
2980       && !h->root.u.def.section->gc_mark
2981       && !(h->root.u.def.section->owner->flags & DYNAMIC))
2982     {
2983       /* Do our best to hide the symbol.  */
2984       h->root.u.def.section = bfd_und_section_ptr;
2985       h->symbol_class = C_HIDDEN;
2986     }
2987 
2988   return true;
2989 }
2990 
2991 /* The sweep phase of garbage collection.  Remove all garbage sections.  */
2992 
2993 typedef bool (*gc_sweep_hook_fn)
2994   (bfd *, struct bfd_link_info *, asection *, const struct internal_reloc *);
2995 
2996 static bool
coff_gc_sweep(bfd * abfd ATTRIBUTE_UNUSED,struct bfd_link_info * info)2997 coff_gc_sweep (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
2998 {
2999   bfd *sub;
3000 
3001   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3002     {
3003       asection *o;
3004 
3005       if (bfd_get_flavour (sub) != bfd_target_coff_flavour)
3006 	continue;
3007 
3008       for (o = sub->sections; o != NULL; o = o->next)
3009 	{
3010 	    /* Keep debug and special sections.  */
3011 	  if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
3012 	      || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
3013 	    o->gc_mark = 1;
3014 	  else if (startswith (o->name, ".idata")
3015 		   || startswith (o->name, ".pdata")
3016 		   || startswith (o->name, ".xdata")
3017 		   || startswith (o->name, ".rsrc"))
3018 	    o->gc_mark = 1;
3019 
3020 	  if (o->gc_mark)
3021 	    continue;
3022 
3023 	  /* Skip sweeping sections already excluded.  */
3024 	  if (o->flags & SEC_EXCLUDE)
3025 	    continue;
3026 
3027 	  /* Since this is early in the link process, it is simple
3028 	     to remove a section from the output.  */
3029 	  o->flags |= SEC_EXCLUDE;
3030 
3031 	  if (info->print_gc_sections && o->size != 0)
3032 	    /* xgettext: c-format */
3033 	    _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
3034 				o, sub);
3035 
3036 #if 0
3037 	  /* But we also have to update some of the relocation
3038 	     info we collected before.  */
3039 	  if (gc_sweep_hook
3040 	      && (o->flags & SEC_RELOC) != 0
3041 	      && o->reloc_count > 0
3042 	      && !bfd_is_abs_section (o->output_section))
3043 	    {
3044 	      struct internal_reloc *internal_relocs;
3045 	      bool r;
3046 
3047 	      internal_relocs
3048 		= _bfd_coff_link_read_relocs (o->owner, o, NULL, NULL,
3049 					     info->keep_memory);
3050 	      if (internal_relocs == NULL)
3051 		return false;
3052 
3053 	      r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
3054 
3055 	      if (coff_section_data (o)->relocs != internal_relocs)
3056 		free (internal_relocs);
3057 
3058 	      if (!r)
3059 		return false;
3060 	    }
3061 #endif
3062 	}
3063     }
3064 
3065   /* Remove the symbols that were in the swept sections from the dynamic
3066      symbol table.  */
3067   coff_link_hash_traverse (coff_hash_table (info), coff_gc_sweep_symbol,
3068 			   NULL);
3069 
3070   return true;
3071 }
3072 
3073 /* Keep all sections containing symbols undefined on the command-line,
3074    and the section containing the entry symbol.  */
3075 
3076 static void
_bfd_coff_gc_keep(struct bfd_link_info * info)3077 _bfd_coff_gc_keep (struct bfd_link_info *info)
3078 {
3079   struct bfd_sym_chain *sym;
3080 
3081   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
3082     {
3083       struct coff_link_hash_entry *h;
3084 
3085       h = coff_link_hash_lookup (coff_hash_table (info), sym->name,
3086 				false, false, false);
3087 
3088       if (h != NULL
3089 	  && (h->root.type == bfd_link_hash_defined
3090 	      || h->root.type == bfd_link_hash_defweak)
3091 	  && !bfd_is_abs_section (h->root.u.def.section))
3092 	h->root.u.def.section->flags |= SEC_KEEP;
3093     }
3094 }
3095 
3096 /* Do mark and sweep of unused sections.  */
3097 
3098 bool
bfd_coff_gc_sections(bfd * abfd ATTRIBUTE_UNUSED,struct bfd_link_info * info)3099 bfd_coff_gc_sections (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
3100 {
3101   bfd *sub;
3102 
3103   /* FIXME: Should we implement this? */
3104 #if 0
3105   const bfd_coff_backend_data *bed = coff_backend_info (abfd);
3106 
3107   if (!bed->can_gc_sections
3108       || !is_coff_hash_table (info->hash))
3109     {
3110       _bfd_error_handler(_("warning: gc-sections option ignored"));
3111       return true;
3112     }
3113 #endif
3114 
3115   _bfd_coff_gc_keep (info);
3116 
3117   /* Grovel through relocs to find out who stays ...  */
3118   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3119     {
3120       asection *o;
3121 
3122       if (bfd_get_flavour (sub) != bfd_target_coff_flavour)
3123 	continue;
3124 
3125       for (o = sub->sections; o != NULL; o = o->next)
3126 	{
3127 	  if (((o->flags & (SEC_EXCLUDE | SEC_KEEP)) == SEC_KEEP
3128 	       || startswith (o->name, ".vectors")
3129 	       || startswith (o->name, ".ctors")
3130 	       || startswith (o->name, ".dtors"))
3131 	      && !o->gc_mark)
3132 	    {
3133 	      if (!_bfd_coff_gc_mark (info, o, _bfd_coff_gc_mark_hook))
3134 		return false;
3135 	    }
3136 	}
3137     }
3138 
3139   /* Allow the backend to mark additional target specific sections.  */
3140   _bfd_coff_gc_mark_extra_sections (info, _bfd_coff_gc_mark_hook);
3141 
3142   /* ... and mark SEC_EXCLUDE for those that go.  */
3143   return coff_gc_sweep (abfd, info);
3144 }
3145 
3146 /* Return name used to identify a comdat group.  */
3147 
3148 const char *
bfd_coff_group_name(bfd * abfd,const asection * sec)3149 bfd_coff_group_name (bfd *abfd, const asection *sec)
3150 {
3151   struct coff_comdat_info *ci = bfd_coff_get_comdat_section (abfd, sec);
3152   if (ci != NULL)
3153     return ci->name;
3154   return NULL;
3155 }
3156 
3157 bool
_bfd_coff_close_and_cleanup(bfd * abfd)3158 _bfd_coff_close_and_cleanup (bfd *abfd)
3159 {
3160   struct coff_tdata *tdata = coff_data (abfd);
3161 
3162   if (tdata != NULL)
3163     {
3164       /* PR 25447:
3165 	 Do not clear the keep_syms and keep_strings flags.
3166 	 These may have been set by pe_ILF_build_a_bfd() indicating
3167 	 that the syms and strings pointers are not to be freed.  */
3168       if (bfd_get_format (abfd) == bfd_object
3169 	  && bfd_family_coff (abfd)
3170 	  && !_bfd_coff_free_symbols (abfd))
3171 	return false;
3172 
3173       if (bfd_get_format (abfd) == bfd_object
3174 	  || bfd_get_format (abfd) == bfd_core)
3175 	_bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
3176     }
3177   return _bfd_generic_close_and_cleanup (abfd);
3178 }
3179