Subversion Repositories Kolibri OS

Rev

Rev 5197 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5197 Rev 6324
Line 1... Line 1...
1
/* Support for the generic parts of COFF, for BFD.
1
/* Support for the generic parts of COFF, for BFD.
2
   Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
-
 
3
   2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
-
 
4
   Free Software Foundation, Inc.
2
   Copyright (C) 1990-2015 Free Software Foundation, Inc.
5
   Written by Cygnus Support.
3
   Written by Cygnus Support.
Line 6... Line 4...
6
 
4
 
Line 7... Line 5...
7
   This file is part of BFD, the Binary File Descriptor library.
5
   This file is part of BFD, the Binary File Descriptor library.
Line 84... Line 82...
84
      if (*p == '\0' && strindex >= 0)
82
      if (*p == '\0' && strindex >= 0)
85
	{
83
	{
86
	  strings = _bfd_coff_read_string_table (abfd);
84
	  strings = _bfd_coff_read_string_table (abfd);
87
	  if (strings == NULL)
85
	  if (strings == NULL)
88
	    return FALSE;
86
	    return FALSE;
89
	  /* FIXME: For extra safety, we should make sure that
-
 
90
             strindex does not run us past the end, but right now we
87
	  if ((bfd_size_type)(strindex + 2) >= obj_coff_strings_len (abfd))
91
             don't know the length of the string table.  */
88
	    return FALSE;
92
	  strings += strindex;
89
	  strings += strindex;
93
	  name = (char *) bfd_alloc (abfd,
90
	  name = (char *) bfd_alloc (abfd,
94
                                     (bfd_size_type) strlen (strings) + 1 + 1);
91
                                     (bfd_size_type) strlen (strings) + 1 + 1);
95
	  if (name == NULL)
92
	  if (name == NULL)
96
	    return FALSE;
93
	    return FALSE;
Line 147... Line 144...
147
    return_section->flags |= SEC_HAS_CONTENTS;
144
    return_section->flags |= SEC_HAS_CONTENTS;
Line 148... Line 145...
148
 
145
 
149
  /* Compress/decompress DWARF debug sections with names: .debug_* and
146
  /* Compress/decompress DWARF debug sections with names: .debug_* and
150
     .zdebug_*, after the section flags is set.  */
147
     .zdebug_*, after the section flags is set.  */
-
 
148
  if ((flags & SEC_DEBUGGING)
151
  if ((flags & SEC_DEBUGGING)
149
      && strlen (name) > 7
152
      && ((name[1] == 'd' && name[6] == '_')
150
      && ((name[1] == 'd' && name[6] == '_')
153
	  || (name[1] == 'z' && name[7] == '_')))
151
	  || (strlen (name) > 8 && name[1] == 'z' && name[7] == '_')))
154
    {
152
    {
155
      enum { nothing, compress, decompress } action = nothing;
153
      enum { nothing, compress, decompress } action = nothing;
Line 156... Line 154...
156
      char *new_name = NULL;
154
      char *new_name = NULL;
Line 178... Line 176...
178
	      (*_bfd_error_handler)
176
	      (*_bfd_error_handler)
179
		(_("%B: unable to initialize compress status for section %s"),
177
		(_("%B: unable to initialize compress status for section %s"),
180
		 abfd, name);
178
		 abfd, name);
181
	      return FALSE;
179
	      return FALSE;
182
	    }
180
	    }
-
 
181
	  if (return_section->compress_status == COMPRESS_SECTION_DONE)
-
 
182
	    {
183
	  if (name[1] != 'z')
183
	  if (name[1] != 'z')
184
	    {
184
	    {
185
	      unsigned int len = strlen (name);
185
	      unsigned int len = strlen (name);
Line 186... Line 186...
186
 
186
 
Line 189... Line 189...
189
		return FALSE;
189
		return FALSE;
190
	      new_name[0] = '.';
190
	      new_name[0] = '.';
191
	      new_name[1] = 'z';
191
	      new_name[1] = 'z';
192
	      memcpy (new_name + 2, name + 1, len);
192
	      memcpy (new_name + 2, name + 1, len);
193
	    }
193
	    }
-
 
194
	    }
194
	  break;
195
	  break;
195
	case decompress:
196
	case decompress:
196
	  if (!bfd_init_section_decompress_status (abfd, return_section))
197
	  if (!bfd_init_section_decompress_status (abfd, return_section))
197
	    {
198
	    {
198
	      (*_bfd_error_handler)
199
	      (*_bfd_error_handler)
Line 366... Line 367...
366
	  != internal_f.f_opthdr)
367
	  != internal_f.f_opthdr)
367
	{
368
	{
368
	  bfd_release (abfd, opthdr);
369
	  bfd_release (abfd, opthdr);
369
	  return NULL;
370
	  return NULL;
370
	}
371
	}
-
 
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
 
371
      bfd_coff_swap_aouthdr_in (abfd, opthdr, (void *) &internal_a);
376
      bfd_coff_swap_aouthdr_in (abfd, opthdr, (void *) &internal_a);
372
      bfd_release (abfd, opthdr);
377
      bfd_release (abfd, opthdr);
373
    }
378
    }
Line 374... Line 379...
374
 
379
 
Line 464... Line 469...
464
	{
469
	{
465
	  strings = _bfd_coff_read_string_table (abfd);
470
	  strings = _bfd_coff_read_string_table (abfd);
466
	  if (strings == NULL)
471
	  if (strings == NULL)
467
	    return NULL;
472
	    return NULL;
468
	}
473
	}
-
 
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;
469
      return strings + sym->_n._n_n._n_offset;
479
      return strings + sym->_n._n_n._n_offset;
470
    }
480
    }
471
}
481
}
Line 472... Line 482...
472
 
482
 
Line 627... Line 637...
627
    }
637
    }
Line 628... Line 638...
628
 
638
 
629
  return total;
639
  return total;
Line 630... Line -...
630
}
-
 
631
 
-
 
632
/* Takes a bfd and a symbol, returns a pointer to the coff specific
-
 
633
   area of the symbol if there is one.  */
-
 
634
 
-
 
635
coff_symbol_type *
-
 
636
coff_symbol_from (bfd *ignore_abfd ATTRIBUTE_UNUSED,
-
 
637
		  asymbol *symbol)
-
 
638
{
-
 
639
  if (!bfd_family_coff (bfd_asymbol_bfd (symbol)))
-
 
640
    return (coff_symbol_type *) NULL;
-
 
641
 
-
 
642
  if (bfd_asymbol_bfd (symbol)->tdata.coff_obj_data == (coff_data_type *) NULL)
-
 
643
    return (coff_symbol_type *) NULL;
-
 
644
 
-
 
645
  return (coff_symbol_type *) symbol;
-
 
646
}
640
}
647
 
641
 
648
static void
642
static void
649
fixup_symbol_value (bfd *abfd,
643
fixup_symbol_value (bfd *abfd,
650
		    coff_symbol_type *coff_symbol_ptr,
644
		    coff_symbol_type *coff_symbol_ptr,
Line 759... Line 753...
759
    symbol_ptr_ptr = bfd_ptr->outsymbols;
753
    symbol_ptr_ptr = bfd_ptr->outsymbols;
760
  }
754
  }
Line 761... Line 755...
761
 
755
 
762
  for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
756
  for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
-
 
757
    {
-
 
758
      coff_symbol_type *coff_symbol_ptr;
763
    {
759
 
764
      coff_symbol_type *coff_symbol_ptr = coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
760
      coff_symbol_ptr = coff_symbol_from (symbol_ptr_ptr[symbol_index]);
765
      symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
761
      symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
766
      if (coff_symbol_ptr && coff_symbol_ptr->native)
762
      if (coff_symbol_ptr && coff_symbol_ptr->native)
767
	{
763
	{
768
	  combined_entry_type *s = coff_symbol_ptr->native;
764
	  combined_entry_type *s = coff_symbol_ptr->native;
Line -... Line 765...
-
 
765
	  int i;
769
	  int i;
766
 
770
 
767
	  BFD_ASSERT (s->is_sym);
771
	  if (s->u.syment.n_sclass == C_FILE)
768
	  if (s->u.syment.n_sclass == C_FILE)
772
	    {
769
	    {
773
	      if (last_file != NULL)
770
	      if (last_file != NULL)
Line 802... Line 799...
802
  asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
799
  asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
803
  unsigned int symbol_index;
800
  unsigned int symbol_index;
Line 804... Line 801...
804
 
801
 
805
  for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
802
  for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
806
    {
803
    {
807
      coff_symbol_type *coff_symbol_ptr =
-
 
Line -... Line 804...
-
 
804
      coff_symbol_type *coff_symbol_ptr;
808
      coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
805
 
809
 
806
      coff_symbol_ptr = coff_symbol_from (symbol_ptr_ptr[symbol_index]);
810
      if (coff_symbol_ptr && coff_symbol_ptr->native)
807
      if (coff_symbol_ptr && coff_symbol_ptr->native)
811
	{
808
	{
Line -... Line 809...
-
 
809
	  int i;
812
	  int i;
810
	  combined_entry_type *s = coff_symbol_ptr->native;
813
	  combined_entry_type *s = coff_symbol_ptr->native;
811
 
814
 
812
	  BFD_ASSERT (s->is_sym);
815
	  if (s->fix_value)
813
	  if (s->fix_value)
816
	    {
814
	    {
Line 833... Line 831...
833
	      BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
831
	      BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
834
	    }
832
	    }
835
	  for (i = 0; i < s->u.syment.n_numaux; i++)
833
	  for (i = 0; i < s->u.syment.n_numaux; i++)
836
	    {
834
	    {
837
	      combined_entry_type *a = s + i + 1;
835
	      combined_entry_type *a = s + i + 1;
-
 
836
 
-
 
837
	      BFD_ASSERT (! a->is_sym);
838
	      if (a->fix_tag)
838
	      if (a->fix_tag)
839
		{
839
		{
840
		  a->u.auxent.x_sym.x_tagndx.l =
840
		  a->u.auxent.x_sym.x_tagndx.l =
841
		    a->u.auxent.x_sym.x_tagndx.p->offset;
841
		    a->u.auxent.x_sym.x_tagndx.p->offset;
842
		  a->fix_tag = 0;
842
		  a->fix_tag = 0;
Line 876... Line 876...
876
      symbol->name = "strange";
876
      symbol->name = "strange";
877
      name = (char *) symbol->name;
877
      name = (char *) symbol->name;
878
    }
878
    }
879
  name_length = strlen (name);
879
  name_length = strlen (name);
Line -... Line 880...
-
 
880
 
880
 
881
  BFD_ASSERT (native->is_sym);
881
  if (native->u.syment.n_sclass == C_FILE
882
  if (native->u.syment.n_sclass == C_FILE
882
      && native->u.syment.n_numaux > 0)
883
      && native->u.syment.n_numaux > 0)
883
    {
884
    {
Line 891... Line 892...
891
	  *string_size_p += 6;  /* strlen(".file") + 1 */
892
	  *string_size_p += 6;  /* strlen(".file") + 1 */
892
	}
893
	}
893
      else
894
      else
894
  	strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
895
  	strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
Line -... Line 896...
-
 
896
 
895
 
897
      BFD_ASSERT (! (native + 1)->is_sym);
Line 896... Line 898...
896
      auxent = &(native + 1)->u.auxent;
898
      auxent = &(native + 1)->u.auxent;
Line 897... Line 899...
897
 
899
 
Line 993... Line 995...
993
			       ? symbol->section->output_section
995
			       ? symbol->section->output_section
994
			       : symbol->section;
996
			       : symbol->section;
995
  void * buf;
997
  void * buf;
996
  bfd_size_type symesz;
998
  bfd_size_type symesz;
Line -... Line 999...
-
 
999
 
-
 
1000
  BFD_ASSERT (native->is_sym);
997
 
1001
 
998
  if (native->u.syment.n_sclass == C_FILE)
1002
  if (native->u.syment.n_sclass == C_FILE)
Line 999... Line 1003...
999
    symbol->flags |= BSF_DEBUGGING;
1003
    symbol->flags |= BSF_DEBUGGING;
1000
 
1004
 
Line 1033... Line 1037...
1033
      buf = bfd_alloc (abfd, auxesz);
1037
      buf = bfd_alloc (abfd, auxesz);
1034
      if (!buf)
1038
      if (!buf)
1035
	return FALSE;
1039
	return FALSE;
1036
      for (j = 0; j < native->u.syment.n_numaux; j++)
1040
      for (j = 0; j < native->u.syment.n_numaux; j++)
1037
	{
1041
	{
-
 
1042
	  BFD_ASSERT (! (native + j + 1)->is_sym);
1038
	  bfd_coff_swap_aux_out (abfd,
1043
	  bfd_coff_swap_aux_out (abfd,
1039
				 &((native + j + 1)->u.auxent),
1044
				 &((native + j + 1)->u.auxent),
1040
				 type, n_sclass, (int) j,
1045
				 type, n_sclass, (int) j,
1041
				 native->u.syment.n_numaux,
1046
				 native->u.syment.n_numaux,
1042
				 buf);
1047
				 buf);
Line 1082... Line 1087...
1082
      if (isym != NULL)
1087
      if (isym != NULL)
1083
        memset (isym, 0, sizeof(*isym));
1088
        memset (isym, 0, sizeof (*isym));
1084
      return TRUE;
1089
      return TRUE;
1085
    }
1090
    }
1086
  native = dummy;
1091
  native = dummy;
-
 
1092
  native->is_sym = TRUE;
-
 
1093
  native[1].is_sym = FALSE;
1087
  native->u.syment.n_type = T_NULL;
1094
  native->u.syment.n_type = T_NULL;
1088
  native->u.syment.n_flags = 0;
1095
  native->u.syment.n_flags = 0;
1089
  native->u.syment.n_numaux = 0;
1096
  native->u.syment.n_numaux = 0;
1090
  if (bfd_is_und_section (symbol->section))
1097
  if (bfd_is_und_section (symbol->section))
1091
    {
1098
    {
Line 1122... Line 1129...
1122
	native->u.syment.n_value += output_section->vma;
1129
	native->u.syment.n_value += output_section->vma;
Line 1123... Line 1130...
1123
 
1130
 
1124
      /* Copy the any flags from the file header into the symbol.
1131
      /* Copy the any flags from the file header into the symbol.
1125
         FIXME: Why?  */
1132
         FIXME: Why?  */
1126
      {
1133
      {
1127
	coff_symbol_type *c = coff_symbol_from (abfd, symbol);
1134
	coff_symbol_type *c = coff_symbol_from (symbol);
1128
	if (c != (coff_symbol_type *) NULL)
1135
	if (c != (coff_symbol_type *) NULL)
1129
	  native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
1136
	  native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
1130
      }
1137
      }
Line 1167... Line 1174...
1167
    {
1174
    {
1168
      symbol->symbol.name = "";
1175
      symbol->symbol.name = "";
1169
      return TRUE;
1176
      return TRUE;
1170
    }
1177
    }
Line -... Line 1178...
-
 
1178
 
1171
 
1179
  BFD_ASSERT (native->is_sym);
1172
  /* If this symbol has an associated line number, we must store the
1180
  /* If this symbol has an associated line number, we must store the
1173
     symbol index in the line number field.  We also tag the auxent to
1181
     symbol index in the line number field.  We also tag the auxent to
1174
     point to the right place in the lineno table.  */
1182
     point to the right place in the lineno table.  */
1175
  if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
1183
  if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
Line 1253... Line 1261...
1253
  /* Output all the symbols we have.  */
1261
  /* Output all the symbols we have.  */
1254
  written = 0;
1262
  written = 0;
1255
  for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
1263
  for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
1256
    {
1264
    {
1257
      asymbol *symbol = *p;
1265
      asymbol *symbol = *p;
1258
      coff_symbol_type *c_symbol = coff_symbol_from (abfd, symbol);
1266
      coff_symbol_type *c_symbol = coff_symbol_from (symbol);
Line 1259... Line 1267...
1259
 
1267
 
1260
      if (c_symbol == (coff_symbol_type *) NULL
1268
      if (c_symbol == (coff_symbol_type *) NULL
1261
	  || c_symbol->native == (combined_entry_type *) NULL)
1269
	  || c_symbol->native == (combined_entry_type *) NULL)
1262
	{
1270
	{
Line 1276... Line 1284...
1276
	      /* Suppress error reporting by bfd_coff_classify_symbol.
1284
	      /* Suppress error reporting by bfd_coff_classify_symbol.
1277
		 Error messages can be generated when we are processing a local
1285
		 Error messages can be generated when we are processing a local
1278
		 symbol which has no associated section and we do not have to
1286
		 symbol which has no associated section and we do not have to
1279
		 worry about this, all we need to know is that it is local.  */
1287
		 worry about this, all we need to know is that it is local.  */
1280
	      current_error_handler = bfd_set_error_handler (null_error_handler);
1288
	      current_error_handler = bfd_set_error_handler (null_error_handler);
-
 
1289
	      BFD_ASSERT (c_symbol->native->is_sym);
1281
	      sym_class = bfd_coff_classify_symbol (abfd,
1290
	      sym_class = bfd_coff_classify_symbol (abfd,
1282
                                                   &c_symbol->native->u.syment);
1291
                                                   &c_symbol->native->u.syment);
1283
	      (void) bfd_set_error_handler (current_error_handler);
1292
	      (void) bfd_set_error_handler (current_error_handler);
Line 1284... Line 1293...
1284
 
1293
 
Line 1352... Line 1361...
1352
	   i < limit;
1361
	   i < limit;
1353
	   i++, p++)
1362
	   i++, p++)
1354
	{
1363
	{
1355
	  asymbol *q = *p;
1364
	  asymbol *q = *p;
1356
	  size_t name_length = strlen (q->name);
1365
	  size_t name_length = strlen (q->name);
1357
	  coff_symbol_type *c_symbol = coff_symbol_from (abfd, q);
1366
	  coff_symbol_type *c_symbol = coff_symbol_from (q);
1358
	  size_t maxlen;
1367
	  size_t maxlen;
Line 1359... Line 1368...
1359
 
1368
 
1360
	  /* Figure out whether the symbol name should go in the string
1369
	  /* Figure out whether the symbol name should go in the string
1361
	     table.  Symbol names that are short enough are stored
1370
	     table.  Symbol names that are short enough are stored
Line 1368... Line 1377...
1368
	      || c_symbol->native == NULL)
1377
	      || c_symbol->native == NULL)
1369
	    /* This is not a COFF symbol, so it certainly is not a
1378
	    /* This is not a COFF symbol, so it certainly is not a
1370
	       file name, nor does it go in the .debug section.  */
1379
	       file name, nor does it go in the .debug section.  */
1371
	    maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
1380
	    maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
Line -... Line 1381...
-
 
1381
 
-
 
1382
	  else if (! c_symbol->native->is_sym)
-
 
1383
	    maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
1372
 
1384
 
1373
	  else if (bfd_coff_symname_in_debug (abfd,
1385
	  else if (bfd_coff_symname_in_debug (abfd,
1374
					      &c_symbol->native->u.syment))
1386
					      &c_symbol->native->u.syment))
1375
	    /* This symbol name is in the XCOFF .debug section.
1387
	    /* This symbol name is in the XCOFF .debug section.
1376
	       Don't write it into the string table.  */
1388
	       Don't write it into the string table.  */
Line 1458... Line 1470...
1458
			    (bfd_asymbol_bfd (p), p));
1470
			    (bfd_asymbol_bfd (p), p));
1459
		  if (l)
1471
		  if (l)
1460
		    {
1472
		    {
1461
		      /* Found a linenumber entry, output.  */
1473
		      /* Found a linenumber entry, output.  */
1462
		      struct internal_lineno out;
1474
		      struct internal_lineno out;
-
 
1475
 
1463
		      memset ((void *) & out, 0, sizeof (out));
1476
		      memset ((void *) & out, 0, sizeof (out));
1464
		      out.l_lnno = 0;
1477
		      out.l_lnno = 0;
1465
		      out.l_addr.l_symndx = l->u.offset;
1478
		      out.l_addr.l_symndx = l->u.offset;
1466
		      bfd_coff_swap_lineno_out (abfd, &out, buff);
1479
		      bfd_coff_swap_lineno_out (abfd, &out, buff);
1467
		      if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd)
1480
		      if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd)
Line 1505... Line 1518...
1505
		     combined_entry_type *auxent)
1518
		     combined_entry_type *auxent)
1506
{
1519
{
1507
  unsigned int type = symbol->u.syment.n_type;
1520
  unsigned int type = symbol->u.syment.n_type;
1508
  unsigned int n_sclass = symbol->u.syment.n_sclass;
1521
  unsigned int n_sclass = symbol->u.syment.n_sclass;
Line -... Line 1522...
-
 
1522
 
1509
 
1523
  BFD_ASSERT (symbol->is_sym);
1510
  if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1524
  if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1511
    {
1525
    {
1512
      if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1526
      if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1513
	  (abfd, table_base, symbol, indaux, auxent))
1527
	  (abfd, table_base, symbol, indaux, auxent))
Line 1518... Line 1532...
1518
  if (n_sclass == C_STAT && type == T_NULL)
1532
  if (n_sclass == C_STAT && type == T_NULL)
1519
    return;
1533
    return;
1520
  if (n_sclass == C_FILE)
1534
  if (n_sclass == C_FILE)
1521
    return;
1535
    return;
Line -... Line 1536...
-
 
1536
 
1522
 
1537
  BFD_ASSERT (! auxent->is_sym);
1523
  /* Otherwise patch up.  */
1538
  /* Otherwise patch up.  */
1524
#define N_TMASK coff_data  (abfd)->local_n_tmask
1539
#define N_TMASK coff_data  (abfd)->local_n_tmask
Line 1525... Line 1540...
1525
#define N_BTSHFT coff_data (abfd)->local_n_btshft
1540
#define N_BTSHFT coff_data (abfd)->local_n_btshft
Line 1545... Line 1560...
1545
/* Allocate space for the ".debug" section, and read it.
1560
/* Allocate space for the ".debug" section, and read it.
1546
   We did not read the debug section until now, because
1561
   We did not read the debug section until now, because
1547
   we didn't want to go to the trouble until someone needed it.  */
1562
   we didn't want to go to the trouble until someone needed it.  */
Line 1548... Line 1563...
1548
 
1563
 
1549
static char *
1564
static char *
1550
build_debug_section (bfd *abfd)
1565
build_debug_section (bfd *abfd, asection ** sect_return)
1551
{
1566
{
1552
  char *debug_section;
1567
  char *debug_section;
1553
  file_ptr position;
1568
  file_ptr position;
Line 1573... Line 1588...
1573
  position = bfd_tell (abfd);
1588
  position = bfd_tell (abfd);
1574
  if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0
1589
  if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0
1575
      || bfd_bread (debug_section, sec_size, abfd) != sec_size
1590
      || bfd_bread (debug_section, sec_size, abfd) != sec_size
1576
      || bfd_seek (abfd, position, SEEK_SET) != 0)
1591
      || bfd_seek (abfd, position, SEEK_SET) != 0)
1577
    return NULL;
1592
    return NULL;
-
 
1593
 
-
 
1594
  * sect_return = sect;
1578
  return debug_section;
1595
  return debug_section;
1579
}
1596
}
Line 1580... Line 1597...
1580
 
1597
 
1581
/* Return a pointer to a malloc'd copy of 'name'.  'name' may not be
1598
/* Return a pointer to a malloc'd copy of 'name'.  'name' may not be
Line 1635... Line 1652...
1635
  return TRUE;
1652
  return TRUE;
1636
}
1653
}
Line 1637... Line 1654...
1637
 
1654
 
1638
/* Read in the external strings.  The strings are not loaded until
1655
/* Read in the external strings.  The strings are not loaded until
1639
   they are needed.  This is because we have no simple way of
1656
   they are needed.  This is because we have no simple way of
-
 
1657
   detecting a missing string table in an archive.  If the strings
-
 
1658
   are loaded then the STRINGS and STRINGS_LEN fields in the
Line 1640... Line 1659...
1640
   detecting a missing string table in an archive.  */
1659
   coff_tdata structure will be set.  */
1641
 
1660
 
1642
const char *
1661
const char *
1643
_bfd_coff_read_string_table (bfd *abfd)
1662
_bfd_coff_read_string_table (bfd *abfd)
Line 1685... Line 1704...
1685
	(_("%B: bad string table size %lu"), abfd, (unsigned long) strsize);
1704
	(_("%B: bad string table size %lu"), abfd, (unsigned long) strsize);
1686
      bfd_set_error (bfd_error_bad_value);
1705
      bfd_set_error (bfd_error_bad_value);
1687
      return NULL;
1706
      return NULL;
1688
    }
1707
    }
Line 1689... Line 1708...
1689
 
1708
 
1690
  strings = (char *) bfd_malloc (strsize);
1709
  strings = (char *) bfd_malloc (strsize + 1);
1691
  if (strings == NULL)
1710
  if (strings == NULL)
Line -... Line 1711...
-
 
1711
    return NULL;
-
 
1712
 
-
 
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.  */
1692
    return NULL;
1717
  memset (strings, 0, STRING_SIZE_SIZE);
1693
 
1718
 
1694
  if (bfd_bread (strings + STRING_SIZE_SIZE, strsize - STRING_SIZE_SIZE, abfd)
1719
  if (bfd_bread (strings + STRING_SIZE_SIZE, strsize - STRING_SIZE_SIZE, abfd)
1695
      != strsize - STRING_SIZE_SIZE)
1720
      != strsize - STRING_SIZE_SIZE)
1696
    {
1721
    {
1697
      free (strings);
1722
      free (strings);
Line 1698... Line 1723...
1698
      return NULL;
1723
      return NULL;
1699
    }
-
 
-
 
1724
    }
-
 
1725
 
-
 
1726
  obj_coff_strings (abfd) = strings;
1700
 
1727
  obj_coff_strings_len (abfd) = strsize;
1701
  obj_coff_strings (abfd) = strings;
1728
  /* Terminate the string table, just in case.  */
Line 1702... Line 1729...
1702
 
1729
  strings[strsize] = 0;
Line 1717... Line 1744...
1717
  if (obj_coff_strings (abfd) != NULL
1744
  if (obj_coff_strings (abfd) != NULL
1718
      && ! obj_coff_keep_strings (abfd))
1745
      && ! obj_coff_keep_strings (abfd))
1719
    {
1746
    {
1720
      free (obj_coff_strings (abfd));
1747
      free (obj_coff_strings (abfd));
1721
      obj_coff_strings (abfd) = NULL;
1748
      obj_coff_strings (abfd) = NULL;
-
 
1749
      obj_coff_strings_len (abfd) = 0;
1722
    }
1750
    }
1723
  return TRUE;
1751
  return TRUE;
1724
}
1752
}
Line 1725... Line 1753...
1725
 
1753
 
Line 1737... Line 1765...
1737
  combined_entry_type *internal_end;
1765
  combined_entry_type *internal_end;
1738
  size_t symesz;
1766
  size_t symesz;
1739
  char *raw_src;
1767
  char *raw_src;
1740
  char *raw_end;
1768
  char *raw_end;
1741
  const char *string_table = NULL;
1769
  const char *string_table = NULL;
-
 
1770
  asection * debug_sec = NULL;
1742
  char *debug_section = NULL;
1771
  char *debug_sec_data = NULL;
1743
  bfd_size_type size;
1772
  bfd_size_type size;
Line 1744... Line 1773...
1744
 
1773
 
1745
  if (obj_raw_syments (abfd) != NULL)
1774
  if (obj_raw_syments (abfd) != NULL)
Line -... Line 1775...
-
 
1775
    return obj_raw_syments (abfd);
-
 
1776
 
-
 
1777
  if (! _bfd_coff_get_external_symbols (abfd))
1746
    return obj_raw_syments (abfd);
1778
    return NULL;
1747
 
1779
 
1748
  size = obj_raw_syment_count (abfd) * sizeof (combined_entry_type);
1780
  size = obj_raw_syment_count (abfd) * sizeof (combined_entry_type);
1749
  internal = (combined_entry_type *) bfd_zalloc (abfd, size);
1781
  internal = (combined_entry_type *) bfd_zalloc (abfd, size);
1750
  if (internal == NULL && size != 0)
1782
  if (internal == NULL && size != 0)
Line 1751... Line -...
1751
    return NULL;
-
 
1752
  internal_end = internal + obj_raw_syment_count (abfd);
-
 
1753
 
-
 
1754
  if (! _bfd_coff_get_external_symbols (abfd))
1783
    return NULL;
Line 1755... Line 1784...
1755
    return NULL;
1784
  internal_end = internal + obj_raw_syment_count (abfd);
1756
 
1785
 
1757
  raw_src = (char *) obj_coff_external_syms (abfd);
1786
  raw_src = (char *) obj_coff_external_syms (abfd);
Line 1766... Line 1795...
1766
  /* Swap all the raw entries.  */
1795
  /* Swap all the raw entries.  */
1767
  for (internal_ptr = internal;
1796
  for (internal_ptr = internal;
1768
       raw_src < raw_end;
1797
       raw_src < raw_end;
1769
       raw_src += symesz, internal_ptr++)
1798
       raw_src += symesz, internal_ptr++)
1770
    {
1799
    {
1771
 
-
 
1772
      unsigned int i;
1800
      unsigned int i;
-
 
1801
 
1773
      bfd_coff_swap_sym_in (abfd, (void *) raw_src,
1802
      bfd_coff_swap_sym_in (abfd, (void *) raw_src,
1774
			    (void *) & internal_ptr->u.syment);
1803
			    (void *) & internal_ptr->u.syment);
1775
      symbol_ptr = internal_ptr;
1804
      symbol_ptr = internal_ptr;
-
 
1805
      internal_ptr->is_sym = TRUE;
-
 
1806
 
-
 
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
	}
Line 1776... Line 1816...
1776
 
1816
 
1777
      for (i = 0;
1817
      for (i = 0;
1778
	   i < symbol_ptr->u.syment.n_numaux;
1818
	   i < symbol_ptr->u.syment.n_numaux;
1779
	   i++)
1819
	   i++)
1780
	{
1820
	{
-
 
1821
	  internal_ptr++;
-
 
1822
	  /* PR 17512: Prevent buffer overrun.  */
-
 
1823
	  if (internal_ptr >= internal_end)
-
 
1824
	    {
-
 
1825
	      bfd_release (abfd, internal);
-
 
1826
	      return NULL;
-
 
1827
	    }
1781
	  internal_ptr++;
1828
 
1782
	  raw_src += symesz;
1829
	  raw_src += symesz;
1783
	  bfd_coff_swap_aux_in (abfd, (void *) raw_src,
1830
	  bfd_coff_swap_aux_in (abfd, (void *) raw_src,
1784
				symbol_ptr->u.syment.n_type,
1831
				symbol_ptr->u.syment.n_type,
1785
				symbol_ptr->u.syment.n_sclass,
1832
				symbol_ptr->u.syment.n_sclass,
1786
				(int) i, symbol_ptr->u.syment.n_numaux,
1833
				(int) i, symbol_ptr->u.syment.n_numaux,
-
 
1834
				&(internal_ptr->u.auxent));
-
 
1835
 
1787
				&(internal_ptr->u.auxent));
1836
	  internal_ptr->is_sym = FALSE;
1788
	  coff_pointerize_aux (abfd, internal, symbol_ptr, i,
1837
	  coff_pointerize_aux (abfd, internal, symbol_ptr, i,
1789
			       internal_ptr);
1838
			       internal_ptr);
1790
	}
1839
	}
Line 1796... Line 1845...
1796
    return NULL;
1845
    return NULL;
Line 1797... Line 1846...
1797
 
1846
 
1798
  for (internal_ptr = internal; internal_ptr < internal_end;
1847
  for (internal_ptr = internal; internal_ptr < internal_end;
1799
       internal_ptr++)
1848
       internal_ptr++)
-
 
1849
    {
-
 
1850
      BFD_ASSERT (internal_ptr->is_sym);
1800
    {
1851
 
1801
      if (internal_ptr->u.syment.n_sclass == C_FILE
1852
      if (internal_ptr->u.syment.n_sclass == C_FILE
1802
	  && internal_ptr->u.syment.n_numaux > 0)
1853
	  && internal_ptr->u.syment.n_numaux > 0)
-
 
1854
	{
-
 
1855
	  combined_entry_type * aux = internal_ptr + 1;
1803
	{
1856
 
1804
	  /* Make a file symbol point to the name in the auxent, since
1857
	  /* Make a file symbol point to the name in the auxent, since
-
 
1858
	     the text ".file" is redundant.  */
-
 
1859
	  BFD_ASSERT (! aux->is_sym);
1805
	     the text ".file" is redundant.  */
1860
 
1806
	  if ((internal_ptr + 1)->u.auxent.x_file.x_n.x_zeroes == 0)
1861
	  if (aux->u.auxent.x_file.x_n.x_zeroes == 0)
1807
	    {
1862
	    {
1808
	      /* The filename is a long one, point into the string table.  */
1863
	      /* The filename is a long one, point into the string table.  */
1809
	      if (string_table == NULL)
1864
	      if (string_table == NULL)
1810
		{
1865
		{
1811
		  string_table = _bfd_coff_read_string_table (abfd);
1866
		  string_table = _bfd_coff_read_string_table (abfd);
1812
		  if (string_table == NULL)
1867
		  if (string_table == NULL)
1813
		    return NULL;
1868
		    return NULL;
Line -... Line 1869...
-
 
1869
		}
-
 
1870
 
-
 
1871
	      if ((bfd_size_type)(aux->u.auxent.x_file.x_n.x_offset)
-
 
1872
		  >= obj_coff_strings_len (abfd))
1814
		}
1873
		internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t) _("");
1815
 
-
 
1816
	      internal_ptr->u.syment._n._n_n._n_offset =
-
 
1817
		((bfd_hostptr_t)
1874
	      else
1818
		 (string_table
1875
	      internal_ptr->u.syment._n._n_n._n_offset =
1819
		  + (internal_ptr + 1)->u.auxent.x_file.x_n.x_offset));
1876
		  (bfd_hostptr_t) (string_table + (aux->u.auxent.x_file.x_n.x_offset));
1820
	    }
1877
	    }
1821
	  else
1878
	  else
1822
	    {
1879
	    {
1823
	      /* Ordinary short filename, put into memory anyway.  The
1880
	      /* Ordinary short filename, put into memory anyway.  The
1824
                 Microsoft PE tools sometimes store a filename in
1881
                 Microsoft PE tools sometimes store a filename in
1825
                 multiple AUX entries.  */
1882
                 multiple AUX entries.  */
1826
	      if (internal_ptr->u.syment.n_numaux > 1
1883
	      if (internal_ptr->u.syment.n_numaux > 1
1827
		  && coff_data (abfd)->pe)
1884
		  && coff_data (abfd)->pe)
1828
		internal_ptr->u.syment._n._n_n._n_offset =
1885
		internal_ptr->u.syment._n._n_n._n_offset =
1829
		  ((bfd_hostptr_t)
1886
		  (bfd_hostptr_t)
1830
		   copy_name (abfd,
1887
		   copy_name (abfd,
1831
			      (internal_ptr + 1)->u.auxent.x_file.x_fname,
1888
			     aux->u.auxent.x_file.x_fname,
1832
			      internal_ptr->u.syment.n_numaux * symesz));
1889
			     internal_ptr->u.syment.n_numaux * symesz);
1833
	      else
1890
	      else
1834
		internal_ptr->u.syment._n._n_n._n_offset =
1891
		internal_ptr->u.syment._n._n_n._n_offset =
1835
		  ((bfd_hostptr_t)
1892
		  ((bfd_hostptr_t)
1836
		   copy_name (abfd,
1893
		   copy_name (abfd,
1837
			      (internal_ptr + 1)->u.auxent.x_file.x_fname,
1894
			      aux->u.auxent.x_file.x_fname,
1838
			      (size_t) bfd_coff_filnmlen (abfd)));
1895
			      (size_t) bfd_coff_filnmlen (abfd)));
1839
	    }
1896
	    }
1840
	}
1897
	}
Line 1869... Line 1926...
1869
		{
1926
		{
1870
		  string_table = _bfd_coff_read_string_table (abfd);
1927
		  string_table = _bfd_coff_read_string_table (abfd);
1871
		  if (string_table == NULL)
1928
		  if (string_table == NULL)
1872
		    return NULL;
1929
		    return NULL;
1873
		}
1930
		}
-
 
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
1874
	      internal_ptr->u.syment._n._n_n._n_offset =
1935
	      internal_ptr->u.syment._n._n_n._n_offset =
1875
		((bfd_hostptr_t)
1936
		((bfd_hostptr_t)
1876
		 (string_table
1937
		 (string_table
1877
		  + internal_ptr->u.syment._n._n_n._n_offset));
1938
		  + internal_ptr->u.syment._n._n_n._n_offset));
1878
	    }
1939
	    }
1879
	  else
1940
	  else
1880
	    {
1941
	    {
1881
	      /* Long name in debug section.  Very similar.  */
1942
	      /* Long name in debug section.  Very similar.  */
1882
	      if (debug_section == NULL)
1943
	      if (debug_sec_data == NULL)
1883
		debug_section = build_debug_section (abfd);
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
1884
	      internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t)
1953
	      internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t)
1885
		(debug_section + internal_ptr->u.syment._n._n_n._n_offset);
1954
		      (debug_sec_data + internal_ptr->u.syment._n._n_n._n_offset);
-
 
1955
	    }
-
 
1956
	      else
-
 
1957
		internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t) "";
1886
	    }
1958
	    }
1887
	}
1959
	}
1888
      internal_ptr += internal_ptr->u.syment.n_numaux;
1960
      internal_ptr += internal_ptr->u.syment.n_numaux;
1889
    }
1961
    }
Line 1913... Line 1985...
1913
  coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_zalloc (abfd, amt);
1985
  coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_zalloc (abfd, amt);
Line 1914... Line 1986...
1914
 
1986
 
1915
  if (new_symbol == NULL)
1987
  if (new_symbol == NULL)
1916
    return NULL;
1988
    return NULL;
1917
  new_symbol->symbol.section = 0;
1989
  new_symbol->symbol.section = 0;
1918
  new_symbol->native = 0;
1990
  new_symbol->native = NULL;
1919
  new_symbol->lineno = NULL;
1991
  new_symbol->lineno = NULL;
1920
  new_symbol->done_lineno = FALSE;
1992
  new_symbol->done_lineno = FALSE;
Line 1921... Line 1993...
1921
  new_symbol->symbol.the_bfd = abfd;
1993
  new_symbol->symbol.the_bfd = abfd;
Line 1939... Line 2011...
1939
     (but shouldn't be a constant).  */
2011
     (but shouldn't be a constant).  */
1940
  amt = sizeof (combined_entry_type) * 10;
2012
  amt = sizeof (combined_entry_type) * 10;
1941
  new_symbol->native = (combined_entry_type *) bfd_zalloc (abfd, amt);
2013
  new_symbol->native = (combined_entry_type *) bfd_zalloc (abfd, amt);
1942
  if (!new_symbol->native)
2014
  if (!new_symbol->native)
1943
    return NULL;
2015
    return NULL;
-
 
2016
  new_symbol->native->is_sym = TRUE;
1944
  new_symbol->symbol.section = bfd_abs_section_ptr;
2017
  new_symbol->symbol.section = bfd_abs_section_ptr;
1945
  new_symbol->symbol.flags = BSF_DEBUGGING;
2018
  new_symbol->symbol.flags = BSF_DEBUGGING;
1946
  new_symbol->lineno = NULL;
2019
  new_symbol->lineno = NULL;
1947
  new_symbol->done_lineno = FALSE;
2020
  new_symbol->done_lineno = FALSE;
1948
  new_symbol->symbol.the_bfd = abfd;
2021
  new_symbol->symbol.the_bfd = abfd;
Line 1954... Line 2027...
1954
coff_get_symbol_info (bfd *abfd, asymbol *symbol, symbol_info *ret)
2027
coff_get_symbol_info (bfd *abfd, asymbol *symbol, symbol_info *ret)
1955
{
2028
{
1956
  bfd_symbol_info (symbol, ret);
2029
  bfd_symbol_info (symbol, ret);
Line 1957... Line 2030...
1957
 
2030
 
1958
  if (coffsymbol (symbol)->native != NULL
2031
  if (coffsymbol (symbol)->native != NULL
-
 
2032
      && coffsymbol (symbol)->native->fix_value
1959
      && coffsymbol (symbol)->native->fix_value)
2033
      && coffsymbol (symbol)->native->is_sym)
1960
    ret->value = coffsymbol (symbol)->native->u.syment.n_value -
2034
    ret->value = coffsymbol (symbol)->native->u.syment.n_value -
1961
      (bfd_hostptr_t) obj_raw_syments (abfd);
2035
      (bfd_hostptr_t) obj_raw_syments (abfd);
Line 1962... Line -...
1962
}
-
 
1963
 
-
 
1964
/* Return the COFF syment for a symbol.  */
-
 
1965
 
-
 
1966
bfd_boolean
-
 
1967
bfd_coff_get_syment (bfd *abfd,
-
 
1968
		     asymbol *symbol,
-
 
1969
		     struct internal_syment *psyment)
-
 
1970
{
-
 
1971
  coff_symbol_type *csym;
-
 
1972
 
-
 
1973
  csym = coff_symbol_from (abfd, symbol);
-
 
1974
  if (csym == NULL || csym->native == NULL)
-
 
1975
    {
-
 
1976
      bfd_set_error (bfd_error_invalid_operation);
-
 
1977
      return FALSE;
-
 
1978
    }
-
 
1979
 
-
 
1980
  *psyment = csym->native->u.syment;
-
 
1981
 
-
 
1982
  if (csym->native->fix_value)
-
 
1983
    psyment->n_value = psyment->n_value -
-
 
1984
      (bfd_hostptr_t) obj_raw_syments (abfd);
-
 
1985
 
-
 
1986
  /* FIXME: We should handle fix_line here.  */
-
 
1987
 
-
 
1988
  return TRUE;
-
 
1989
}
-
 
1990
 
-
 
1991
/* Return the COFF auxent for a symbol.  */
-
 
1992
 
-
 
1993
bfd_boolean
-
 
1994
bfd_coff_get_auxent (bfd *abfd,
-
 
1995
		     asymbol *symbol,
-
 
1996
		     int indx,
-
 
1997
		     union internal_auxent *pauxent)
-
 
1998
{
-
 
1999
  coff_symbol_type *csym;
-
 
2000
  combined_entry_type *ent;
-
 
2001
 
-
 
2002
  csym = coff_symbol_from (abfd, symbol);
-
 
2003
 
-
 
2004
  if (csym == NULL
-
 
2005
      || csym->native == NULL
-
 
2006
      || indx >= csym->native->u.syment.n_numaux)
-
 
2007
    {
-
 
2008
      bfd_set_error (bfd_error_invalid_operation);
-
 
2009
      return FALSE;
-
 
2010
    }
-
 
2011
 
-
 
2012
  ent = csym->native + indx + 1;
-
 
2013
 
-
 
2014
  *pauxent = ent->u.auxent;
-
 
2015
 
-
 
2016
  if (ent->fix_tag)
-
 
2017
    pauxent->x_sym.x_tagndx.l =
-
 
2018
      ((combined_entry_type *) pauxent->x_sym.x_tagndx.p
-
 
2019
       - obj_raw_syments (abfd));
-
 
2020
 
-
 
2021
  if (ent->fix_end)
-
 
2022
    pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l =
-
 
2023
      ((combined_entry_type *) pauxent->x_sym.x_fcnary.x_fcn.x_endndx.p
-
 
2024
       - obj_raw_syments (abfd));
-
 
2025
 
-
 
2026
  if (ent->fix_scnlen)
-
 
2027
    pauxent->x_csect.x_scnlen.l =
-
 
2028
      ((combined_entry_type *) pauxent->x_csect.x_scnlen.p
-
 
2029
       - obj_raw_syments (abfd));
-
 
2030
 
-
 
2031
  return TRUE;
-
 
2032
}
2036
}
Line 2033... Line 2037...
2033
 
2037
 
2034
/* Print out information about COFF symbol.  */
2038
/* Print out information about COFF symbol.  */
2035
 
2039
 
Line 2062... Line 2066...
2062
	  combined_entry_type *root = obj_raw_syments (abfd);
2066
	  combined_entry_type *root = obj_raw_syments (abfd);
2063
	  struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
2067
	  struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
Line 2064... Line 2068...
2064
 
2068
 
Line -... Line 2069...
-
 
2069
	  fprintf (file, "[%3ld]", (long) (combined - root));
-
 
2070
 
-
 
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
	    }
2065
	  fprintf (file, "[%3ld]", (long) (combined - root));
2078
 
2066
 
2079
	  BFD_ASSERT (combined->is_sym);
2067
	  if (! combined->fix_value)
2080
	  if (! combined->fix_value)
2068
	    val = (bfd_vma) combined->u.syment.n_value;
2081
	    val = (bfd_vma) combined->u.syment.n_value;
Line 2081... Line 2094...
2081
	  for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
2094
	  for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
2082
	    {
2095
	    {
2083
	      combined_entry_type *auxp = combined + aux + 1;
2096
	      combined_entry_type *auxp = combined + aux + 1;
2084
	      long tagndx;
2097
	      long tagndx;
Line -... Line 2098...
-
 
2098
 
2085
 
2099
	      BFD_ASSERT (! auxp->is_sym);
2086
	      if (auxp->fix_tag)
2100
	      if (auxp->fix_tag)
2087
		tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
2101
		tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
2088
	      else
2102
	      else
Line 2155... Line 2169...
2155
	    {
2169
	    {
2156
	      fprintf (file, "\n%s :", l->u.sym->name);
2170
	      fprintf (file, "\n%s :", l->u.sym->name);
2157
	      l++;
2171
	      l++;
2158
	      while (l->line_number)
2172
	      while (l->line_number)
2159
		{
2173
		{
-
 
2174
		  if (l->line_number > 0)
-
 
2175
		    {
2160
		  fprintf (file, "\n%4d : ", l->line_number);
2176
		  fprintf (file, "\n%4d : ", l->line_number);
2161
		  bfd_fprintf_vma (abfd, file, l->u.offset + symbol->section->vma);
2177
		  bfd_fprintf_vma (abfd, file, l->u.offset + symbol->section->vma);
-
 
2178
		    }
2162
		  l++;
2179
		  l++;
2163
		}
2180
		}
2164
	    }
2181
	    }
2165
	}
2182
	}
2166
      else
2183
      else
Line 2191... Line 2208...
2191
   section, calculate and return the name of the source file and the line
2208
   section, calculate and return the name of the source file and the line
2192
   nearest to the wanted location.  */
2209
   nearest to the wanted location.  */
Line 2193... Line 2210...
2193
 
2210
 
2194
bfd_boolean
2211
bfd_boolean
2195
coff_find_nearest_line_with_names (bfd *abfd,
-
 
2196
                                   const struct dwarf_debug_section *debug_sections,
-
 
2197
                                   asection *section,
2212
coff_find_nearest_line_with_names (bfd *abfd,
-
 
2213
                                   asymbol **symbols,
2198
                                   asymbol **symbols,
2214
                                   asection *section,
2199
                                   bfd_vma offset,
2215
                                   bfd_vma offset,
2200
                                   const char **filename_ptr,
2216
                                   const char **filename_ptr,
2201
                                   const char **functionname_ptr,
2217
                                   const char **functionname_ptr,
-
 
2218
                                   unsigned int *line_ptr,
2202
                                   unsigned int *line_ptr)
2219
                                   const struct dwarf_debug_section *debug_sections)
2203
{
2220
{
2204
  bfd_boolean found;
2221
  bfd_boolean found;
2205
  unsigned int i;
2222
  unsigned int i;
2206
  unsigned int line_base;
2223
  unsigned int line_base;
Line 2222... Line 2239...
2222
 
2239
 
2223
  if (found)
2240
  if (found)
Line 2224... Line 2241...
2224
    return TRUE;
2241
    return TRUE;
2225
 
2242
 
-
 
2243
  /* Also try examining DWARF2 debugging information.  */
-
 
2244
  if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
-
 
2245
				     filename_ptr, functionname_ptr,
-
 
2246
				     line_ptr, NULL, debug_sections, 0,
-
 
2247
				     &coff_data(abfd)->dwarf2_find_line_info))
-
 
2248
    return TRUE;
-
 
2249
 
-
 
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);
2226
  /* Also try examining DWARF2 debugging information.  */
2260
 
-
 
2261
      if (bias
2227
  if (_bfd_dwarf2_find_nearest_line (abfd, debug_sections,
2262
	  && _bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section,
2228
                                     section, symbols, offset,
2263
					    offset + bias,
2229
				     filename_ptr, functionname_ptr,
2264
					    filename_ptr, functionname_ptr,
2230
				     line_ptr, NULL, 0,
2265
					    line_ptr, NULL, debug_sections, 0,
-
 
2266
					    &coff_data(abfd)->dwarf2_find_line_info))
Line 2231... Line 2267...
2231
				     &coff_data(abfd)->dwarf2_find_line_info))
2267
	return TRUE;
2232
    return TRUE;
2268
    }
2233
 
2269
 
Line 2248... Line 2284...
2248
    return FALSE;
2284
    return FALSE;
Line 2249... Line 2285...
2249
 
2285
 
2250
  pend = p + cof->raw_syment_count;
2286
  pend = p + cof->raw_syment_count;
2251
  while (p < pend)
2287
  while (p < pend)
-
 
2288
    {
2252
    {
2289
      BFD_ASSERT (p->is_sym);
2253
      if (p->u.syment.n_sclass == C_FILE)
2290
      if (p->u.syment.n_sclass == C_FILE)
2254
	break;
2291
	break;
2255
      p += 1 + p->u.syment.n_numaux;
2292
      p += 1 + p->u.syment.n_numaux;
Line 2271... Line 2308...
2271
 
2308
 
2272
	  for (p2 = p + 1 + p->u.syment.n_numaux;
2309
	  for (p2 = p + 1 + p->u.syment.n_numaux;
2273
	       p2 < pend;
2310
	       p2 < pend;
2274
	       p2 += 1 + p2->u.syment.n_numaux)
2311
	       p2 += 1 + p2->u.syment.n_numaux)
-
 
2312
	    {
2275
	    {
2313
	      BFD_ASSERT (p2->is_sym);
2276
	      if (p2->u.syment.n_scnum > 0
2314
	      if (p2->u.syment.n_scnum > 0
2277
		  && (section
2315
		  && (section
2278
		      == coff_section_from_bfd_index (abfd,
2316
		      == coff_section_from_bfd_index (abfd,
2279
						      p2->u.syment.n_scnum)))
2317
						      p2->u.syment.n_scnum)))
Line 2282... Line 2320...
2282
		{
2320
		{
2283
		  p2 = pend;
2321
		  p2 = pend;
2284
		  break;
2322
		  break;
2285
		}
2323
		}
2286
	    }
2324
	    }
-
 
2325
	  if (p2 >= pend)
-
 
2326
	    break;
Line 2287... Line 2327...
2287
 
2327
 
2288
	  file_addr = (bfd_vma) p2->u.syment.n_value;
2328
	  file_addr = (bfd_vma) p2->u.syment.n_value;
2289
	  /* PR 11512: Include the section address of the function name symbol.  */
2329
	  /* PR 11512: Include the section address of the function name symbol.  */
2290
	  if (p2->u.syment.n_scnum > 0)
2330
	  if (p2->u.syment.n_scnum > 0)
Line 2346... Line 2386...
2346
	      *functionname_ptr = coff->symbol.name;
2386
	      *functionname_ptr = coff->symbol.name;
2347
	      last_value = coff->symbol.value;
2387
	      last_value = coff->symbol.value;
2348
	      if (coff->native)
2388
	      if (coff->native)
2349
		{
2389
		{
2350
		  combined_entry_type *s = coff->native;
2390
		  combined_entry_type *s = coff->native;
-
 
2391
 
-
 
2392
		  BFD_ASSERT (s->is_sym);
2351
		  s = s + 1 + s->u.syment.n_numaux;
2393
		  s = s + 1 + s->u.syment.n_numaux;
Line 2352... Line 2394...
2352
 
2394
 
2353
		  /* In XCOFF a debugging symbol can follow the
2395
		  /* In XCOFF a debugging symbol can follow the
2354
		     function symbol.  */
2396
		     function symbol.  */
Line 2358... Line 2400...
2358
		  /* S should now point to the .bf of the function.  */
2400
		  /* S should now point to the .bf of the function.  */
2359
		  if (s->u.syment.n_numaux)
2401
		  if (s->u.syment.n_numaux)
2360
		    {
2402
		    {
2361
		      /* The linenumber is stored in the auxent.  */
2403
		      /* The linenumber is stored in the auxent.  */
2362
		      union internal_auxent *a = &((s + 1)->u.auxent);
2404
		      union internal_auxent *a = &((s + 1)->u.auxent);
-
 
2405
 
2363
		      line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
2406
		      line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
2364
		      *line_ptr = line_base;
2407
		      *line_ptr = line_base;
2365
		    }
2408
		    }
2366
		}
2409
		}
2367
	    }
2410
	    }
Line 2407... Line 2450...
2407
  return TRUE;
2450
  return TRUE;
2408
}
2451
}
Line 2409... Line 2452...
2409
 
2452
 
2410
bfd_boolean
2453
bfd_boolean
2411
coff_find_nearest_line (bfd *abfd,
-
 
2412
			asection *section,
2454
coff_find_nearest_line (bfd *abfd,
2413
			asymbol **symbols,
-
 
2414
			bfd_vma offset,
-
 
2415
			const char **filename_ptr,
-
 
2416
			const char **functionname_ptr,
-
 
2417
			unsigned int *line_ptr)
-
 
2418
{
-
 
2419
  return coff_find_nearest_line_with_names (abfd, dwarf_debug_sections,
-
 
2420
                                            section, symbols, offset,
-
 
2421
                                            filename_ptr, functionname_ptr,
-
 
2422
                                            line_ptr);
-
 
2423
}
-
 
2424
 
-
 
2425
bfd_boolean
-
 
2426
coff_find_nearest_line_discriminator (bfd *abfd,
2455
			asymbol **symbols,
2427
				      asection *section,
-
 
2428
				      asymbol **symbols,
2456
				      asection *section,
2429
				      bfd_vma offset,
2457
				      bfd_vma offset,
2430
				      const char **filename_ptr,
2458
				      const char **filename_ptr,
2431
				      const char **functionname_ptr,
2459
				      const char **functionname_ptr,
2432
				      unsigned int *line_ptr,
2460
				      unsigned int *line_ptr,
2433
				      unsigned int *discriminator)
2461
			unsigned int *discriminator_ptr)
-
 
2462
{
2434
{
2463
  if (discriminator_ptr)
2435
  *discriminator = 0;
2464
    *discriminator_ptr = 0;
2436
  return coff_find_nearest_line_with_names (abfd, dwarf_debug_sections,
-
 
2437
                                            section, symbols, offset,
2465
  return coff_find_nearest_line_with_names (abfd, symbols, section, offset,
2438
                                            filename_ptr, functionname_ptr,
2466
                                            filename_ptr, functionname_ptr,
2439
                                            line_ptr);
2467
                                            line_ptr, dwarf_debug_sections);
Line 2440... Line -...
2440
}
-
 
2441
 
2468
}
2442
 
2469
 
2443
bfd_boolean
2470
bfd_boolean
2444
coff_find_inliner_info (bfd *abfd,
2471
coff_find_inliner_info (bfd *abfd,
2445
			const char **filename_ptr,
2472
			const char **filename_ptr,
Line 2457... Line 2484...
2457
int
2484
int
2458
coff_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
2485
coff_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
2459
{
2486
{
2460
  size_t size;
2487
  size_t size;
Line 2461... Line 2488...
2461
 
2488
 
2462
  if (!info->relocatable)
2489
  if (!bfd_link_relocatable (info))
2463
    size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
2490
    size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
2464
  else
2491
  else
Line 2465... Line 2492...
2465
    size = bfd_coff_filhsz (abfd);
2492
    size = bfd_coff_filhsz (abfd);
Line 2475... Line 2502...
2475
			   asymbol *     symbol,
2502
			   asymbol *     symbol,
2476
			   unsigned int  symbol_class)
2503
			   unsigned int  symbol_class)
2477
{
2504
{
2478
  coff_symbol_type * csym;
2505
  coff_symbol_type * csym;
Line 2479... Line 2506...
2479
 
2506
 
2480
  csym = coff_symbol_from (abfd, symbol);
2507
  csym = coff_symbol_from (symbol);
2481
  if (csym == NULL)
2508
  if (csym == NULL)
2482
    {
2509
    {
2483
      bfd_set_error (bfd_error_invalid_operation);
2510
      bfd_set_error (bfd_error_invalid_operation);
2484
      return FALSE;
2511
      return FALSE;
Line 2495... Line 2522...
2495
 
2522
 
2496
      native = (combined_entry_type *) bfd_zalloc (abfd, amt);
2523
      native = (combined_entry_type *) bfd_zalloc (abfd, amt);
2497
      if (native == NULL)
2524
      if (native == NULL)
Line -... Line 2525...
-
 
2525
	return FALSE;
2498
	return FALSE;
2526
 
2499
 
2527
      native->is_sym = TRUE;
Line 2500... Line 2528...
2500
      native->u.syment.n_type   = T_NULL;
2528
      native->u.syment.n_type   = T_NULL;
2501
      native->u.syment.n_sclass = symbol_class;
2529
      native->u.syment.n_sclass = symbol_class;
Line 2530... Line 2558...
2530
    csym->native->u.syment.n_sclass = symbol_class;
2558
    csym->native->u.syment.n_sclass = symbol_class;
Line 2531... Line 2559...
2531
 
2559
 
2532
  return TRUE;
2560
  return TRUE;
Line 2533... Line -...
2533
}
-
 
2534
 
-
 
2535
struct coff_comdat_info *
-
 
2536
bfd_coff_get_comdat_section (bfd *abfd, struct bfd_section *sec)
-
 
2537
{
-
 
2538
  if (bfd_get_flavour (abfd) == bfd_target_coff_flavour
-
 
2539
      && coff_section_data (abfd, sec) != NULL)
-
 
2540
    return coff_section_data (abfd, sec)->comdat;
-
 
2541
  else
-
 
2542
    return NULL;
-
 
2543
}
2561
}
2544
 
2562
 
2545
bfd_boolean
2563
bfd_boolean
2546
_bfd_coff_section_already_linked (bfd *abfd,
2564
_bfd_coff_section_already_linked (bfd *abfd,
2547
				  asection *sec,
2565
				  asection *sec,
Line 2606... Line 2624...
2606
  /* This is the first section with this name.  Record it.  */
2624
  /* This is the first section with this name.  Record it.  */
2607
  if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
2625
  if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
2608
    info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
2626
    info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
2609
  return FALSE;
2627
  return FALSE;
2610
}
2628
}
-
 
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
}