Subversion Repositories Kolibri OS

Rev

Rev 5197 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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