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 PE/PEI; the common executable parts.
1
/* Support for the generic parts of PE/PEI; the common executable parts.
2
   Copyright 1995-2013 Free Software Foundation, Inc.
2
   Copyright (C) 1995-2015 Free Software Foundation, Inc.
3
   Written by Cygnus Solutions.
3
   Written by Cygnus Solutions.
Line 4... Line 4...
4
 
4
 
Line 5... Line 5...
5
   This file is part of BFD, the Binary File Descriptor library.
5
   This file is part of BFD, the Binary File Descriptor library.
Line 78... Line 78...
78
#endif
78
#endif
Line 79... Line 79...
79
 
79
 
80
#include "coff/pe.h"
80
#include "coff/pe.h"
81
#include "libcoff.h"
81
#include "libcoff.h"
-
 
82
#include "libpei.h"
Line 82... Line 83...
82
#include "libpei.h"
83
#include "safe-ctype.h"
83
 
84
 
84
#if defined COFF_WITH_pep || defined COFF_WITH_pex64
85
#if defined COFF_WITH_pep || defined COFF_WITH_pex64
85
# undef AOUTSZ
86
# undef AOUTSZ
86
# define AOUTSZ		PEPAOUTSZ
87
# define AOUTSZ		PEPAOUTSZ
Line -... Line 88...
-
 
88
# define PEAOUTHDR	PEPAOUTHDR
-
 
89
#endif
-
 
90
 
-
 
91
#define HighBitSet(val)      ((val) & 0x80000000)
87
# define PEAOUTHDR	PEPAOUTHDR
92
#define SetHighBit(val)      ((val) | 0x80000000)
88
#endif
93
#define WithoutHighBit(val)  ((val) & 0x7fffffff)
89
 
94
 
90
/* FIXME: This file has various tests of POWERPC_LE_PE.  Those tests
95
/* FIXME: This file has various tests of POWERPC_LE_PE.  Those tests
91
   worked when the code was in peicode.h, but no longer work now that
96
   worked when the code was in peicode.h, but no longer work now that
Line 139... Line 144...
139
	{
144
	{
140
	  asection *sec;
145
	  asection *sec;
Line 141... Line 146...
141
 
146
 
142
	  name = _bfd_coff_internal_syment_name (abfd, in, namebuf);
147
	  name = _bfd_coff_internal_syment_name (abfd, in, namebuf);
-
 
148
	  if (name == NULL)
-
 
149
	    {
-
 
150
	      _bfd_error_handler (_("%B: unable to find name for empty section"),
143
	  if (name == NULL)
151
				  abfd);
144
	    /* FIXME: Return error.  */
152
	      bfd_set_error (bfd_error_invalid_target);
-
 
153
	      return;
-
 
154
	    }
145
	    abort ();
155
 
146
	  sec = bfd_get_section_by_name (abfd, name);
156
	  sec = bfd_get_section_by_name (abfd, name);
147
	  if (sec != NULL)
157
	  if (sec != NULL)
148
	    in->n_scnum = sec->target_index;
158
	    in->n_scnum = sec->target_index;
Line 160... Line 170...
160
 
170
 
161
	  if (name == namebuf)
171
	  if (name == namebuf)
162
	    {
172
	    {
163
	      name = (const char *) bfd_alloc (abfd, strlen (namebuf) + 1);
173
	      name = (const char *) bfd_alloc (abfd, strlen (namebuf) + 1);
-
 
174
	      if (name == NULL)
-
 
175
		{
164
	      if (name == NULL)
176
		  _bfd_error_handler (_("%B: out of memory creating name for empty section"),
165
		/* FIXME: Return error.  */
177
				      abfd);
-
 
178
		  return;
166
		abort ();
179
		}
167
	      strcpy ((char *) name, namebuf);
180
	      strcpy ((char *) name, namebuf);
-
 
181
	    }
168
	    }
182
 
169
	  flags = SEC_HAS_CONTENTS | SEC_ALLOC | SEC_DATA | SEC_LOAD;
183
	  flags = SEC_HAS_CONTENTS | SEC_ALLOC | SEC_DATA | SEC_LOAD;
170
	  sec = bfd_make_section_anyway_with_flags (abfd, name, flags);
184
	  sec = bfd_make_section_anyway_with_flags (abfd, name, flags);
-
 
185
	  if (sec == NULL)
171
	  if (sec == NULL)
186
	    {
-
 
187
	      _bfd_error_handler (_("%B: unable to create fake empty section"),
172
	    /* FIXME: Return error.  */
188
				  abfd);
-
 
189
	      return;
Line 173... Line 190...
173
	    abort ();
190
	    }
174
 
191
 
175
	  sec->vma = 0;
192
	  sec->vma = 0;
176
	  sec->lma = 0;
193
	  sec->lma = 0;
Line 197... Line 214...
197
     worth fixing.  */
214
     worth fixing.  */
198
  coff_swap_sym_in_hook (abfd, ext1, in1);
215
  coff_swap_sym_in_hook (abfd, ext1, in1);
199
#endif
216
#endif
200
}
217
}
Line -... Line 218...
-
 
218
 
-
 
219
static bfd_boolean
-
 
220
abs_finder (bfd * abfd ATTRIBUTE_UNUSED, asection * sec, void * data)
-
 
221
{
-
 
222
  bfd_vma abs_val = * (bfd_vma *) data;
-
 
223
 
-
 
224
  return (sec->vma <= abs_val) && ((sec->vma + (1ULL << 32)) > abs_val);
-
 
225
}
201
 
226
 
202
unsigned int
227
unsigned int
203
_bfd_pei_swap_sym_out (bfd * abfd, void * inp, void * extp)
228
_bfd_pei_swap_sym_out (bfd * abfd, void * inp, void * extp)
204
{
229
{
205
  struct internal_syment *in = (struct internal_syment *) inp;
230
  struct internal_syment *in = (struct internal_syment *) inp;
Line 211... Line 236...
211
      H_PUT_32 (abfd, in->_n._n_n._n_offset, ext->e.e.e_offset);
236
      H_PUT_32 (abfd, in->_n._n_n._n_offset, ext->e.e.e_offset);
212
    }
237
    }
213
  else
238
  else
214
    memcpy (ext->e.e_name, in->_n._n_name, SYMNMLEN);
239
    memcpy (ext->e.e_name, in->_n._n_name, SYMNMLEN);
Line -... Line 240...
-
 
240
 
-
 
241
  /* The PE32 and PE32+ formats only use 4 bytes to hold the value of a
-
 
242
     symbol.  This is a problem on 64-bit targets where we can generate
-
 
243
     absolute symbols with values >= 1^32.  We try to work around this
-
 
244
     problem by finding a section whose base address is sufficient to
-
 
245
     reduce the absolute value to < 1^32, and then transforming the
-
 
246
     symbol into a section relative symbol.  This of course is a hack.  */
-
 
247
  if (sizeof (in->n_value) > 4
-
 
248
      /* The strange computation of the shift amount is here in order to
-
 
249
	 avoid a compile time warning about the comparison always being
-
 
250
	 false.  It does not matter if this test fails to work as expected
-
 
251
	 as the worst that can happen is that some absolute symbols are
-
 
252
	 needlessly converted into section relative symbols.  */
-
 
253
      && in->n_value > ((1ULL << (sizeof (in->n_value) > 4 ? 32 : 31)) - 1)
-
 
254
      && in->n_scnum == -1)
-
 
255
    {
-
 
256
      asection * sec;
-
 
257
 
-
 
258
      sec = bfd_sections_find_if (abfd, abs_finder, & in->n_value);
-
 
259
      if (sec)
-
 
260
	{
-
 
261
	  in->n_value -= sec->vma;
-
 
262
	  in->n_scnum = sec->target_index;
-
 
263
	}
-
 
264
      /* else: FIXME: The value is outside the range of any section.  This
-
 
265
	 happens for __image_base__ and __ImageBase and maybe some other
-
 
266
	 symbols as well.  We should find a way to handle these values.  */
-
 
267
    }
215
 
268
 
216
  H_PUT_32 (abfd, in->n_value, ext->e_value);
269
  H_PUT_32 (abfd, in->n_value, ext->e_value);
Line 217... Line 270...
217
  H_PUT_16 (abfd, in->n_scnum, ext->e_scnum);
270
  H_PUT_16 (abfd, in->n_scnum, ext->e_scnum);
218
 
271
 
Line 237... Line 290...
237
		      void * 	in1)
290
		      void * 	in1)
238
{
291
{
239
  AUXENT *ext = (AUXENT *) ext1;
292
  AUXENT *ext = (AUXENT *) ext1;
240
  union internal_auxent *in = (union internal_auxent *) in1;
293
  union internal_auxent *in = (union internal_auxent *) in1;
Line -... Line 294...
-
 
294
 
-
 
295
  /* PR 17521: Make sure that all fields in the aux structure
-
 
296
     are initialised.  */
241
 
297
  memset (in, 0, sizeof * in);
242
  switch (in_class)
298
  switch (in_class)
243
    {
299
    {
244
    case C_FILE:
300
    case C_FILE:
245
      if (ext->x_file.x_fname[0] == 0)
301
      if (ext->x_file.x_fname[0] == 0)
Line 412... Line 468...
412
  aouthdr_int->dsize = GET_AOUTHDR_DSIZE (abfd, aouthdr_ext->dsize);
468
  aouthdr_int->dsize = GET_AOUTHDR_DSIZE (abfd, aouthdr_ext->dsize);
413
  aouthdr_int->bsize = GET_AOUTHDR_BSIZE (abfd, aouthdr_ext->bsize);
469
  aouthdr_int->bsize = GET_AOUTHDR_BSIZE (abfd, aouthdr_ext->bsize);
414
  aouthdr_int->entry = GET_AOUTHDR_ENTRY (abfd, aouthdr_ext->entry);
470
  aouthdr_int->entry = GET_AOUTHDR_ENTRY (abfd, aouthdr_ext->entry);
415
  aouthdr_int->text_start =
471
  aouthdr_int->text_start =
416
    GET_AOUTHDR_TEXT_START (abfd, aouthdr_ext->text_start);
472
    GET_AOUTHDR_TEXT_START (abfd, aouthdr_ext->text_start);
-
 
473
 
417
#if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
474
#if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
418
  /* PE32+ does not have data_start member!  */
475
  /* PE32+ does not have data_start member!  */
419
  aouthdr_int->data_start =
476
  aouthdr_int->data_start =
420
    GET_AOUTHDR_DATA_START (abfd, aouthdr_ext->data_start);
477
    GET_AOUTHDR_DATA_START (abfd, aouthdr_ext->data_start);
421
  a->BaseOfData = aouthdr_int->data_start;
478
  a->BaseOfData = aouthdr_int->data_start;
Line 458... Line 515...
458
  a->NumberOfRvaAndSizes = H_GET_32 (abfd, src->NumberOfRvaAndSizes);
515
  a->NumberOfRvaAndSizes = H_GET_32 (abfd, src->NumberOfRvaAndSizes);
Line 459... Line 516...
459
 
516
 
460
  {
517
  {
Line -... Line 518...
-
 
518
    int idx;
-
 
519
 
-
 
520
    /* PR 17512: Corrupt PE binaries can cause seg-faults.  */
-
 
521
    if (a->NumberOfRvaAndSizes > IMAGE_NUMBEROF_DIRECTORY_ENTRIES)
-
 
522
      {
-
 
523
	(*_bfd_error_handler)
-
 
524
	  (_("%B: aout header specifies an invalid number of data-directory entries: %d"),
-
 
525
	   abfd, a->NumberOfRvaAndSizes);
-
 
526
	bfd_set_error (bfd_error_bad_value);
-
 
527
 
-
 
528
	/* Paranoia: If the number is corrupt, then assume that the
-
 
529
	   actual entries themselves might be corrupt as well.  */
-
 
530
	a->NumberOfRvaAndSizes = 0;
461
    int idx;
531
      }
462
 
532
 
463
    for (idx = 0; idx < a->NumberOfRvaAndSizes; idx++)
533
    for (idx = 0; idx < a->NumberOfRvaAndSizes; idx++)
464
      {
534
      {
465
        /* If data directory is empty, rva also should be 0.  */
535
        /* If data directory is empty, rva also should be 0.  */
Line 472... Line 542...
472
	  a->DataDirectory[idx].VirtualAddress =
542
	  a->DataDirectory[idx].VirtualAddress =
473
	    H_GET_32 (abfd, src->DataDirectory[idx][0]);
543
	    H_GET_32 (abfd, src->DataDirectory[idx][0]);
474
	else
544
	else
475
	  a->DataDirectory[idx].VirtualAddress = 0;
545
	  a->DataDirectory[idx].VirtualAddress = 0;
476
      }
546
      }
-
 
547
 
-
 
548
    while (idx < IMAGE_NUMBEROF_DIRECTORY_ENTRIES)
-
 
549
      {
-
 
550
	a->DataDirectory[idx].Size = 0;
-
 
551
	a->DataDirectory[idx].VirtualAddress = 0;
-
 
552
	idx ++;
-
 
553
      }
477
  }
554
  }
Line 478... Line 555...
478
 
555
 
479
  if (aouthdr_int->entry)
556
  if (aouthdr_int->entry)
480
    {
557
    {
Line 715... Line 792...
715
  H_PUT_32 (abfd, extra->NumberOfRvaAndSizes,
792
  H_PUT_32 (abfd, extra->NumberOfRvaAndSizes,
716
	    aouthdr_out->NumberOfRvaAndSizes);
793
	    aouthdr_out->NumberOfRvaAndSizes);
717
  {
794
  {
718
    int idx;
795
    int idx;
Line 719... Line 796...
719
 
796
 
720
    for (idx = 0; idx < 16; idx++)
797
    for (idx = 0; idx < IMAGE_NUMBEROF_DIRECTORY_ENTRIES; idx++)
721
      {
798
      {
722
	H_PUT_32 (abfd, extra->DataDirectory[idx].VirtualAddress,
799
	H_PUT_32 (abfd, extra->DataDirectory[idx].VirtualAddress,
723
		  aouthdr_out->DataDirectory[idx][0]);
800
		  aouthdr_out->DataDirectory[idx][0]);
724
	H_PUT_32 (abfd, extra->DataDirectory[idx].Size,
801
	H_PUT_32 (abfd, extra->DataDirectory[idx].Size,
Line 975... Line 1052...
975
 
1052
 
976
    H_PUT_32 (abfd, scnhdr_int->s_flags, scnhdr_ext->s_flags);
1053
    H_PUT_32 (abfd, scnhdr_int->s_flags, scnhdr_ext->s_flags);
Line 977... Line 1054...
977
  }
1054
  }
978
 
1055
 
979
  if (coff_data (abfd)->link_info
1056
  if (coff_data (abfd)->link_info
980
      && ! coff_data (abfd)->link_info->relocatable
1057
      && ! bfd_link_relocatable (coff_data (abfd)->link_info)
981
      && ! coff_data (abfd)->link_info->shared
1058
      && ! bfd_link_pic (coff_data (abfd)->link_info)
982
      && strcmp (scnhdr_int->s_name, ".text") == 0)
1059
      && strcmp (scnhdr_int->s_name, ".text") == 0)
983
    {
1060
    {
984
      /* By inference from looking at MS output, the 32 bit field
1061
      /* By inference from looking at MS output, the 32 bit field
Line 1021... Line 1098...
1021
	}
1098
	}
1022
    }
1099
    }
1023
  return ret;
1100
  return ret;
1024
}
1101
}
Line -... Line 1102...
-
 
1102
 
-
 
1103
void
-
 
1104
_bfd_pei_swap_debugdir_in (bfd * abfd, void * ext1, void * in1)
-
 
1105
{
-
 
1106
  struct external_IMAGE_DEBUG_DIRECTORY *ext = (struct external_IMAGE_DEBUG_DIRECTORY *) ext1;
-
 
1107
  struct internal_IMAGE_DEBUG_DIRECTORY *in = (struct internal_IMAGE_DEBUG_DIRECTORY *) in1;
-
 
1108
 
-
 
1109
  in->Characteristics = H_GET_32(abfd, ext->Characteristics);
-
 
1110
  in->TimeDateStamp = H_GET_32(abfd, ext->TimeDateStamp);
-
 
1111
  in->MajorVersion = H_GET_16(abfd, ext->MajorVersion);
-
 
1112
  in->MinorVersion = H_GET_16(abfd, ext->MinorVersion);
-
 
1113
  in->Type = H_GET_32(abfd, ext->Type);
-
 
1114
  in->SizeOfData = H_GET_32(abfd, ext->SizeOfData);
-
 
1115
  in->AddressOfRawData = H_GET_32(abfd, ext->AddressOfRawData);
-
 
1116
  in->PointerToRawData = H_GET_32(abfd, ext->PointerToRawData);
-
 
1117
}
-
 
1118
 
-
 
1119
unsigned int
-
 
1120
_bfd_pei_swap_debugdir_out (bfd * abfd, void * inp, void * extp)
-
 
1121
{
-
 
1122
  struct external_IMAGE_DEBUG_DIRECTORY *ext = (struct external_IMAGE_DEBUG_DIRECTORY *) extp;
-
 
1123
  struct internal_IMAGE_DEBUG_DIRECTORY *in = (struct internal_IMAGE_DEBUG_DIRECTORY *) inp;
-
 
1124
 
-
 
1125
  H_PUT_32(abfd, in->Characteristics, ext->Characteristics);
-
 
1126
  H_PUT_32(abfd, in->TimeDateStamp, ext->TimeDateStamp);
-
 
1127
  H_PUT_16(abfd, in->MajorVersion, ext->MajorVersion);
-
 
1128
  H_PUT_16(abfd, in->MinorVersion, ext->MinorVersion);
-
 
1129
  H_PUT_32(abfd, in->Type, ext->Type);
-
 
1130
  H_PUT_32(abfd, in->SizeOfData, ext->SizeOfData);
-
 
1131
  H_PUT_32(abfd, in->AddressOfRawData, ext->AddressOfRawData);
-
 
1132
  H_PUT_32(abfd, in->PointerToRawData, ext->PointerToRawData);
-
 
1133
 
-
 
1134
  return sizeof (struct external_IMAGE_DEBUG_DIRECTORY);
-
 
1135
}
-
 
1136
 
-
 
1137
CODEVIEW_INFO *
-
 
1138
_bfd_pei_slurp_codeview_record (bfd * abfd, file_ptr where, unsigned long length, CODEVIEW_INFO *cvinfo)
-
 
1139
{
-
 
1140
  char buffer[256+1];
-
 
1141
 
-
 
1142
  if (bfd_seek (abfd, where, SEEK_SET) != 0)
-
 
1143
    return NULL;
-
 
1144
 
-
 
1145
  if (bfd_bread (buffer, 256, abfd) < 4)
-
 
1146
    return NULL;
-
 
1147
 
-
 
1148
  /* Ensure null termination of filename.  */
-
 
1149
  buffer[256] = '\0';
-
 
1150
 
-
 
1151
  cvinfo->CVSignature = H_GET_32 (abfd, buffer);
-
 
1152
  cvinfo->Age = 0;
-
 
1153
 
-
 
1154
  if ((cvinfo->CVSignature == CVINFO_PDB70_CVSIGNATURE)
-
 
1155
      && (length > sizeof (CV_INFO_PDB70)))
-
 
1156
    {
-
 
1157
      CV_INFO_PDB70 *cvinfo70 = (CV_INFO_PDB70 *)(buffer);
-
 
1158
 
-
 
1159
      cvinfo->Age = H_GET_32(abfd, cvinfo70->Age);
-
 
1160
 
-
 
1161
      /* A GUID consists of 4,2,2 byte values in little-endian order, followed
-
 
1162
         by 8 single bytes.  Byte swap them so we can conveniently treat the GUID
-
 
1163
         as 16 bytes in big-endian order.  */
-
 
1164
      bfd_putb32 (bfd_getl32 (cvinfo70->Signature), cvinfo->Signature);
-
 
1165
      bfd_putb16 (bfd_getl16 (&(cvinfo70->Signature[4])), &(cvinfo->Signature[4]));
-
 
1166
      bfd_putb16 (bfd_getl16 (&(cvinfo70->Signature[6])), &(cvinfo->Signature[6]));
-
 
1167
      memcpy (&(cvinfo->Signature[8]), &(cvinfo70->Signature[8]), 8);
-
 
1168
 
-
 
1169
      cvinfo->SignatureLength = CV_INFO_SIGNATURE_LENGTH;
-
 
1170
      // cvinfo->PdbFileName = cvinfo70->PdbFileName;
-
 
1171
 
-
 
1172
      return cvinfo;
-
 
1173
    }
-
 
1174
  else if ((cvinfo->CVSignature == CVINFO_PDB20_CVSIGNATURE)
-
 
1175
           && (length > sizeof (CV_INFO_PDB20)))
-
 
1176
    {
-
 
1177
      CV_INFO_PDB20 *cvinfo20 = (CV_INFO_PDB20 *)(buffer);
-
 
1178
      cvinfo->Age = H_GET_32(abfd, cvinfo20->Age);
-
 
1179
      memcpy (cvinfo->Signature, cvinfo20->Signature, 4);
-
 
1180
      cvinfo->SignatureLength = 4;
-
 
1181
      // cvinfo->PdbFileName = cvinfo20->PdbFileName;
-
 
1182
 
-
 
1183
      return cvinfo;
-
 
1184
    }
-
 
1185
 
-
 
1186
  return NULL;
-
 
1187
}
-
 
1188
 
-
 
1189
unsigned int
-
 
1190
_bfd_pei_write_codeview_record (bfd * abfd, file_ptr where, CODEVIEW_INFO *cvinfo)
-
 
1191
{
-
 
1192
  unsigned int size = sizeof (CV_INFO_PDB70) + 1;
-
 
1193
  CV_INFO_PDB70 *cvinfo70;
-
 
1194
  char buffer[size];
-
 
1195
 
-
 
1196
  if (bfd_seek (abfd, where, SEEK_SET) != 0)
-
 
1197
    return 0;
-
 
1198
 
-
 
1199
  cvinfo70 = (CV_INFO_PDB70 *) buffer;
-
 
1200
  H_PUT_32 (abfd, CVINFO_PDB70_CVSIGNATURE, cvinfo70->CvSignature);
-
 
1201
 
-
 
1202
  /* Byte swap the GUID from 16 bytes in big-endian order to 4,2,2 byte values
-
 
1203
     in little-endian order, followed by 8 single bytes.  */
-
 
1204
  bfd_putl32 (bfd_getb32 (cvinfo->Signature), cvinfo70->Signature);
-
 
1205
  bfd_putl16 (bfd_getb16 (&(cvinfo->Signature[4])), &(cvinfo70->Signature[4]));
-
 
1206
  bfd_putl16 (bfd_getb16 (&(cvinfo->Signature[6])), &(cvinfo70->Signature[6]));
-
 
1207
  memcpy (&(cvinfo70->Signature[8]), &(cvinfo->Signature[8]), 8);
-
 
1208
 
-
 
1209
  H_PUT_32 (abfd, cvinfo->Age, cvinfo70->Age);
-
 
1210
  cvinfo70->PdbFileName[0] = '\0';
-
 
1211
 
-
 
1212
  if (bfd_bwrite (buffer, size, abfd) != size)
-
 
1213
    return 0;
-
 
1214
 
-
 
1215
  return size;
-
 
1216
}
1025
 
1217
 
1026
static char * dir_names[IMAGE_NUMBEROF_DIRECTORY_ENTRIES] =
1218
static char * dir_names[IMAGE_NUMBEROF_DIRECTORY_ENTRIES] =
1027
{
1219
{
1028
  N_("Export Directory [.edata (or where ever we found it)]"),
1220
  N_("Export Directory [.edata (or where ever we found it)]"),
1029
  N_("Import Directory [parts of .idata]"),
1221
  N_("Import Directory [parts of .idata]"),
Line 1101... Line 1293...
1101
	{
1293
	{
1102
	  fprintf (file,
1294
	  fprintf (file,
1103
		   _("\nThere is an import table, but the section containing it could not be found\n"));
1295
		   _("\nThere is an import table, but the section containing it could not be found\n"));
1104
	  return TRUE;
1296
	  return TRUE;
1105
	}
1297
	}
-
 
1298
      else if (!(section->flags & SEC_HAS_CONTENTS))
-
 
1299
        {
-
 
1300
	  fprintf (file,
-
 
1301
		   _("\nThere is an import table in %s, but that section has no contents\n"),
-
 
1302
		   section->name);
-
 
1303
	  return TRUE;
-
 
1304
        }
1106
    }
1305
    }
Line 1107... Line 1306...
1107
 
1306
 
1108
  fprintf (file, _("\nThere is an import table in %s at 0x%lx\n"),
1307
  fprintf (file, _("\nThere is an import table in %s at 0x%lx\n"),
Line 1212... Line 1411...
1212
 
1411
 
1213
      if (dll_name - adj >= section->size)
1412
      if (dll_name - adj >= section->size)
Line 1214... Line 1413...
1214
        break;
1413
        break;
-
 
1414
 
-
 
1415
      dll = (char *) data + dll_name - adj;
1215
 
1416
      /* PR 17512 file: 078-12277-0.004.  */
Line 1216... Line 1417...
1216
      dll = (char *) data + dll_name - adj;
1417
      bfd_size_type maxlen = (char *)(data + datasize) - dll - 1;
1217
      fprintf (file, _("\n\tDLL Name: %s\n"), dll);
1418
      fprintf (file, _("\n\tDLL Name: %.*s\n"), (int) maxlen, dll);
1218
 
1419
 
1219
      if (hint_addr != 0)
1420
      if (hint_addr != 0)
Line 1277... Line 1478...
1277
 
1478
 
1278
	  /* Print HintName vector entries.  */
1479
	  /* Print HintName vector entries.  */
1279
#ifdef COFF_WITH_pex64
1480
#ifdef COFF_WITH_pex64
1280
	  for (j = 0; idx + j + 8 <= datasize; j += 8)
1481
	  for (j = 0; idx + j + 8 <= datasize; j += 8)
-
 
1482
	    {
1281
	    {
1483
	      bfd_size_type amt;
1282
	      unsigned long member = bfd_get_32 (abfd, data + idx + j);
1484
	      unsigned long member = bfd_get_32 (abfd, data + idx + j);
Line 1283... Line 1485...
1283
	      unsigned long member_high = bfd_get_32 (abfd, data + idx + j + 4);
1485
	      unsigned long member_high = bfd_get_32 (abfd, data + idx + j + 4);
1284
 
1486
 
Line -... Line 1487...
-
 
1487
	      if (!member && !member_high)
-
 
1488
		break;
1285
	      if (!member && !member_high)
1489
 
1286
		break;
1490
	      amt = member - adj;
-
 
1491
 
1287
 
1492
	      if (HighBitSet (member_high))
-
 
1493
		fprintf (file, "\t%lx%08lx\t %4lx%08lx  ",
-
 
1494
			 member_high, member,
-
 
1495
			 WithoutHighBit (member_high), member);
1288
	      if (member_high & 0x80000000)
1496
	      /* PR binutils/17512: Handle corrupt PE data.  */
1289
		fprintf (file, "\t%lx%08lx\t %4lx%08lx  ",
1497
	      else if (amt + 2 >= datasize)
1290
			 member_high,member, member_high & 0x7fffffff, member);
1498
		fprintf (file, _("\t"), member);
1291
	      else
1499
	      else
Line 1292... Line 1500...
1292
		{
1500
		{
1293
		  int ordinal;
1501
		  int ordinal;
1294
		  char *member_name;
1502
		  char *member_name;
-
 
1503
 
1295
 
1504
		  ordinal = bfd_get_16 (abfd, data + amt);
Line 1296... Line 1505...
1296
		  ordinal = bfd_get_16 (abfd, data + member - adj);
1505
		  member_name = (char *) data + amt + 2;
1297
		  member_name = (char *) data + member - adj + 2;
1506
		  fprintf (file, "\t%04lx\t %4d  %.*s",member, ordinal,
1298
		  fprintf (file, "\t%04lx\t %4d  %s",member, ordinal, member_name);
1507
			   (int) (datasize - (amt + 2)), member_name);
Line 1309... Line 1518...
1309
	      fprintf (file, "\n");
1518
	      fprintf (file, "\n");
1310
	    }
1519
	    }
1311
#else
1520
#else
1312
	  for (j = 0; idx + j + 4 <= datasize; j += 4)
1521
	  for (j = 0; idx + j + 4 <= datasize; j += 4)
1313
	    {
1522
	    {
-
 
1523
	      bfd_size_type amt;
1314
	      unsigned long member = bfd_get_32 (abfd, data + idx + j);
1524
	      unsigned long member = bfd_get_32 (abfd, data + idx + j);
Line 1315... Line 1525...
1315
 
1525
 
1316
	      /* Print single IMAGE_IMPORT_BY_NAME vector.  */
1526
	      /* Print single IMAGE_IMPORT_BY_NAME vector.  */
1317
	      if (member == 0)
1527
	      if (member == 0)
Line 1318... Line 1528...
1318
		break;
1528
		break;
-
 
1529
 
1319
 
1530
	      amt = member - adj;
1320
	      if (member & 0x80000000)
1531
	      if (HighBitSet (member))
-
 
1532
		fprintf (file, "\t%04lx\t %4lu  ",
-
 
1533
			 member, WithoutHighBit (member));
-
 
1534
	      /* PR binutils/17512: Handle corrupt PE data.  */
1321
		fprintf (file, "\t%04lx\t %4lu  ",
1535
	      else if (amt + 2 >= datasize)
1322
			 member, member & 0x7fffffff);
1536
		fprintf (file, _("\t"), member);
1323
	      else
1537
	      else
1324
		{
1538
		{
Line 1325... Line 1539...
1325
		  int ordinal;
1539
		  int ordinal;
1326
		  char *member_name;
1540
		  char *member_name;
1327
 
1541
 
1328
		  ordinal = bfd_get_16 (abfd, data + member - adj);
1542
		  ordinal = bfd_get_16 (abfd, data + amt);
-
 
1543
		  member_name = (char *) data + amt + 2;
1329
		  member_name = (char *) data + member - adj + 2;
1544
		  fprintf (file, "\t%04lx\t %4d  %.*s",
Line 1330... Line 1545...
1330
		  fprintf (file, "\t%04lx\t %4d  %s",
1545
			   member, ordinal,
1331
			   member, ordinal, member_name);
1546
			   (int) (datasize - (amt + 2)), member_name);
1332
		}
1547
		}
Line 1362... Line 1577...
1362
  bfd_byte *data;
1577
  bfd_byte *data;
1363
  asection *section;
1578
  asection *section;
1364
  bfd_size_type datasize = 0;
1579
  bfd_size_type datasize = 0;
1365
  bfd_size_type dataoff;
1580
  bfd_size_type dataoff;
1366
  bfd_size_type i;
1581
  bfd_size_type i;
1367
  bfd_signed_vma adj;
1582
  bfd_vma       adj;
1368
  struct EDT_type
1583
  struct EDT_type
1369
  {
1584
  {
1370
    long export_flags;          /* Reserved - should be zero.  */
1585
    long export_flags;          /* Reserved - should be zero.  */
1371
    long time_stamp;
1586
    long time_stamp;
1372
    short major_ver;
1587
    short major_ver;
Line 1412... Line 1627...
1412
	{
1627
	{
1413
	  fprintf (file,
1628
	  fprintf (file,
1414
		   _("\nThere is an export table, but the section containing it could not be found\n"));
1629
		   _("\nThere is an export table, but the section containing it could not be found\n"));
1415
	  return TRUE;
1630
	  return TRUE;
1416
	}
1631
	}
-
 
1632
      else if (!(section->flags & SEC_HAS_CONTENTS))
-
 
1633
        {
-
 
1634
	  fprintf (file,
-
 
1635
		   _("\nThere is an export table in %s, but that section has no contents\n"),
-
 
1636
		   section->name);
-
 
1637
	  return TRUE;
-
 
1638
        }
Line 1417... Line 1639...
1417
 
1639
 
1418
      dataoff = addr - section->vma;
1640
      dataoff = addr - section->vma;
1419
      datasize = extra->DataDirectory[PE_EXPORT_TABLE].Size;
1641
      datasize = extra->DataDirectory[PE_EXPORT_TABLE].Size;
1420
      if (datasize > section->size - dataoff)
1642
      if (datasize > section->size - dataoff)
Line 1424... Line 1646...
1424
		   section->name);
1646
		   section->name);
1425
	  return TRUE;
1647
	  return TRUE;
1426
	}
1648
	}
1427
    }
1649
    }
Line -... Line 1650...
-
 
1650
 
-
 
1651
  /* PR 17512: Handle corrupt PE binaries.  */
-
 
1652
  if (datasize < 36)
-
 
1653
    {
-
 
1654
      fprintf (file,
-
 
1655
	       _("\nThere is an export table in %s, but it is too small (%d)\n"),
-
 
1656
	       section->name, (int) datasize);
-
 
1657
      return TRUE;
-
 
1658
    }
1428
 
1659
 
1429
  fprintf (file, _("\nThere is an export table in %s at 0x%lx\n"),
1660
  fprintf (file, _("\nThere is an export table in %s at 0x%lx\n"),
Line 1430... Line 1661...
1430
	   section->name, (unsigned long) addr);
1661
	   section->name, (unsigned long) addr);
1431
 
1662
 
Line 1467... Line 1698...
1467
	   _("Major/Minor \t\t\t%d/%d\n"), edt.major_ver, edt.minor_ver);
1698
	   _("Major/Minor \t\t\t%d/%d\n"), edt.major_ver, edt.minor_ver);
Line 1468... Line 1699...
1468
 
1699
 
1469
  fprintf (file,
1700
  fprintf (file,
1470
	   _("Name \t\t\t\t"));
1701
	   _("Name \t\t\t\t"));
-
 
1702
  bfd_fprintf_vma (abfd, file, edt.name);
-
 
1703
 
1471
  bfd_fprintf_vma (abfd, file, edt.name);
1704
  if ((edt.name >= adj) && (edt.name < adj + datasize))
-
 
1705
    fprintf (file, " %.*s\n",
1472
  fprintf (file,
1706
	     (int) (datasize - (edt.name - adj)),
-
 
1707
	     data + edt.name - adj);
-
 
1708
  else
Line 1473... Line 1709...
1473
	   " %s\n", data + edt.name - adj);
1709
    fprintf (file, "(outside .edata section)\n");
1474
 
1710
 
Line 1475... Line 1711...
1475
  fprintf (file,
1711
  fprintf (file,
Line 1514... Line 1750...
1514
 
1750
 
1515
  fprintf (file,
1751
  fprintf (file,
1516
	  _("\nExport Address Table -- Ordinal Base %ld\n"),
1752
	  _("\nExport Address Table -- Ordinal Base %ld\n"),
Line -... Line 1753...
-
 
1753
	  edt.base);
-
 
1754
 
-
 
1755
  /* PR 17512: Handle corrupt PE binaries.  */
-
 
1756
  if (edt.eat_addr + (edt.num_functions * 4) - adj >= datasize
-
 
1757
      /* PR 17512: file: 092b1829 */
-
 
1758
      || (edt.num_functions * 4) < edt.num_functions
-
 
1759
      /* PR 17512 file: 140-165018-0.004.  */
-
 
1760
      || data + edt.eat_addr - adj < data)
-
 
1761
    fprintf (file, _("\tInvalid Export Address Table rva (0x%lx) or entry count (0x%lx)\n"),
1517
	  edt.base);
1762
	     (long) edt.eat_addr,
1518
 
1763
	     (long) edt.num_functions);
1519
  for (i = 0; i < edt.num_functions; ++i)
1764
  else for (i = 0; i < edt.num_functions; ++i)
1520
    {
1765
    {
1521
      bfd_vma eat_member = bfd_get_32 (abfd,
1766
      bfd_vma eat_member = bfd_get_32 (abfd,
1522
				       data + edt.eat_addr + (i * 4) - adj);
1767
				       data + edt.eat_addr + (i * 4) - adj);
Line 1526... Line 1771...
1526
      if (eat_member - adj <= datasize)
1771
      if (eat_member - adj <= datasize)
1527
	{
1772
	{
1528
	  /* This rva is to a name (forwarding function) in our section.  */
1773
	  /* This rva is to a name (forwarding function) in our section.  */
1529
	  /* Should locate a function descriptor.  */
1774
	  /* Should locate a function descriptor.  */
1530
	  fprintf (file,
1775
	  fprintf (file,
1531
		   "\t[%4ld] +base[%4ld] %04lx %s -- %s\n",
1776
		   "\t[%4ld] +base[%4ld] %04lx %s -- %.*s\n",
1532
		   (long) i,
1777
		   (long) i,
1533
		   (long) (i + edt.base),
1778
		   (long) (i + edt.base),
1534
		   (unsigned long) eat_member,
1779
		   (unsigned long) eat_member,
1535
		   _("Forwarder RVA"),
1780
		   _("Forwarder RVA"),
-
 
1781
		   (int)(datasize - (eat_member - adj)),
1536
		   data + eat_member - adj);
1782
		   data + eat_member - adj);
1537
	}
1783
	}
1538
      else
1784
      else
1539
	{
1785
	{
1540
	  /* Should locate a function descriptor in the reldata section.  */
1786
	  /* Should locate a function descriptor in the reldata section.  */
Line 1550... Line 1796...
1550
  /* The Export Name Pointer Table is paired with the Export Ordinal Table.  */
1796
  /* The Export Name Pointer Table is paired with the Export Ordinal Table.  */
1551
  /* Dump them in parallel for clarity.  */
1797
  /* Dump them in parallel for clarity.  */
1552
  fprintf (file,
1798
  fprintf (file,
1553
	   _("\n[Ordinal/Name Pointer] Table\n"));
1799
	   _("\n[Ordinal/Name Pointer] Table\n"));
Line -... Line 1800...
-
 
1800
 
-
 
1801
  /* PR 17512: Handle corrupt PE binaries.  */
-
 
1802
  if (edt.npt_addr + (edt.num_names * 4) - adj >= datasize
-
 
1803
      /* PR 17512: file: bb68816e.  */
-
 
1804
      || edt.num_names * 4 < edt.num_names
-
 
1805
      || (data + edt.npt_addr - adj) < data)
-
 
1806
    fprintf (file, _("\tInvalid Name Pointer Table rva (0x%lx) or entry count (0x%lx)\n"),
-
 
1807
	     (long) edt.npt_addr,
-
 
1808
	     (long) edt.num_names);
-
 
1809
  /* PR 17512: file: 140-147171-0.004.  */
-
 
1810
  else if (edt.ot_addr + (edt.num_names * 2) - adj >= datasize
-
 
1811
	   || data + edt.ot_addr - adj < data)
-
 
1812
    fprintf (file, _("\tInvalid Ordinal Table rva (0x%lx) or entry count (0x%lx)\n"),
-
 
1813
	     (long) edt.ot_addr,
1554
 
1814
	     (long) edt.num_names);
1555
  for (i = 0; i < edt.num_names; ++i)
1815
  else for (i = 0; i < edt.num_names; ++i)
1556
    {
1816
    {
1557
      bfd_vma name_ptr = bfd_get_32 (abfd,
1817
      bfd_vma  name_ptr;
-
 
1818
      bfd_vma  ord;
1558
				    data +
1819
 
1559
				    edt.npt_addr
1820
      ord = bfd_get_16 (abfd, data + edt.ot_addr + (i * 2) - adj);
Line -... Line 1821...
-
 
1821
      name_ptr = bfd_get_32 (abfd, data + edt.npt_addr + (i * 4) - adj);
-
 
1822
 
-
 
1823
      if ((name_ptr - adj) >= datasize)
-
 
1824
	{
-
 
1825
	  fprintf (file, _("\t[%4ld] \n"),
-
 
1826
		   (long) ord, (long) name_ptr);
-
 
1827
	}
1560
				    + (i*4) - adj);
1828
      else
Line 1561... Line 1829...
1561
 
1829
    {
1562
      char *name = (char *) data + name_ptr - adj;
-
 
1563
 
-
 
1564
      bfd_vma ord = bfd_get_16 (abfd,
-
 
1565
				    data +
-
 
1566
				    edt.ot_addr
1830
	  char * name = (char *) data + name_ptr - adj;
-
 
1831
 
1567
				    + (i*2) - adj);
1832
	  fprintf (file, "\t[%4ld] %.*s\n", (long) ord,
Line 1568... Line 1833...
1568
      fprintf (file,
1833
		   (int)((char *)(data + datasize) - name), name);
Line 1569... Line 1834...
1569
	      "\t[%4ld] %s\n", (long) ord, name);
1834
	}
Line 1626... Line 1891...
1626
 
1891
 
1627
  datasize = section->size;
1892
  datasize = section->size;
1628
  if (datasize == 0)
1893
  if (datasize == 0)
Line -... Line 1894...
-
 
1894
    return TRUE;
-
 
1895
 
-
 
1896
  /* PR 17512: file: 002-193900-0.004.  */
-
 
1897
  if (datasize < stop)
-
 
1898
    {
-
 
1899
      fprintf (file, _("Virtual size of .pdata section (%ld) larger than real size (%ld)\n"),
-
 
1900
	       (long) stop, (long) datasize);
-
 
1901
      return FALSE;
1629
    return TRUE;
1902
    }
1630
 
1903
 
1631
  if (! bfd_malloc_and_get_section (abfd, section, &data))
1904
  if (! bfd_malloc_and_get_section (abfd, section, &data))
1632
    {
1905
    {
1633
      if (data != NULL)
1906
      if (data != NULL)
Line 1735... Line 2008...
1735
 
2008
 
1736
  storage = bfd_get_symtab_upper_bound (abfd);
2009
  storage = bfd_get_symtab_upper_bound (abfd);
1737
  if (storage < 0)
2010
  if (storage < 0)
1738
    return NULL;
2011
    return NULL;
-
 
2012
  if (storage)
1739
  if (storage)
2013
    {
-
 
2014
    sy = (asymbol **) bfd_malloc (storage);
-
 
2015
      if (sy == NULL)
-
 
2016
	return NULL;
Line 1740... Line 2017...
1740
    sy = (asymbol **) bfd_malloc (storage);
2017
    }
1741
 
2018
 
1742
  psc->symcount = bfd_canonicalize_symtab (abfd, sy);
2019
  psc->symcount = bfd_canonicalize_symtab (abfd, sy);
1743
  if (psc->symcount < 0)
2020
  if (psc->symcount < 0)
Line 1913... Line 2190...
1913
pe_print_reloc (bfd * abfd, void * vfile)
2190
pe_print_reloc (bfd * abfd, void * vfile)
1914
{
2191
{
1915
  FILE *file = (FILE *) vfile;
2192
  FILE *file = (FILE *) vfile;
1916
  bfd_byte *data = 0;
2193
  bfd_byte *data = 0;
1917
  asection *section = bfd_get_section_by_name (abfd, ".reloc");
2194
  asection *section = bfd_get_section_by_name (abfd, ".reloc");
1918
  bfd_size_type i;
2195
  bfd_byte *p, *end;
1919
  bfd_size_type start, stop;
-
 
1920
 
-
 
1921
  if (section == NULL)
-
 
1922
    return TRUE;
-
 
Line 1923... Line 2196...
1923
 
2196
 
1924
  if (section->size == 0)
2197
  if (section == NULL || section->size == 0 || !(section->flags & SEC_HAS_CONTENTS))
Line 1925... Line 2198...
1925
    return TRUE;
2198
    return TRUE;
1926
 
2199
 
Line 1932... Line 2205...
1932
      if (data != NULL)
2205
      if (data != NULL)
1933
	free (data);
2206
	free (data);
1934
      return FALSE;
2207
      return FALSE;
1935
    }
2208
    }
Line 1936... Line 2209...
1936
 
2209
 
1937
  start = 0;
-
 
1938
 
2210
  p = data;
1939
  stop = section->size;
-
 
1940
 
2211
  end = data + section->size;
1941
  for (i = start; i < stop;)
2212
  while (p + 8 <= end)
1942
    {
2213
    {
1943
      int j;
2214
      int j;
1944
      bfd_vma virtual_address;
2215
      bfd_vma virtual_address;
-
 
2216
      unsigned long number, size;
Line 1945... Line 2217...
1945
      long number, size;
2217
      bfd_byte *chunk_end;
1946
 
2218
 
1947
      /* The .reloc section is a sequence of blocks, with a header consisting
2219
      /* The .reloc section is a sequence of blocks, with a header consisting
1948
	 of two 32 bit quantities, followed by a number of 16 bit entries.  */
2220
	 of two 32 bit quantities, followed by a number of 16 bit entries.  */
-
 
2221
      virtual_address = bfd_get_32 (abfd, p);
1949
      virtual_address = bfd_get_32 (abfd, data+i);
2222
      size = bfd_get_32 (abfd, p + 4);
Line 1950... Line 2223...
1950
      size = bfd_get_32 (abfd, data+i+4);
2223
      p += 8;
1951
      number = (size - 8) / 2;
2224
      number = (size - 8) / 2;
Line 1952... Line 2225...
1952
 
2225
 
1953
      if (size == 0)
2226
      if (size == 0)
1954
	break;
2227
	break;
Line -... Line 2228...
-
 
2228
 
-
 
2229
      fprintf (file,
-
 
2230
	       _("\nVirtual Address: %08lx Chunk size %ld (0x%lx) Number of fixups %ld\n"),
-
 
2231
	       (unsigned long) virtual_address, size, size, number);
1955
 
2232
 
1956
      fprintf (file,
2233
      chunk_end = p + size;
1957
	       _("\nVirtual Address: %08lx Chunk size %ld (0x%lx) Number of fixups %ld\n"),
2234
      if (chunk_end > end)
1958
	       (unsigned long) virtual_address, size, (unsigned long) size, number);
2235
	chunk_end = end;
1959
 
2236
      j = 0;
Line 1960... Line 2237...
1960
      for (j = 0; j < number; ++j)
2237
      while (p + 2 <= chunk_end)
1961
	{
2238
	{
Line 1962... Line 2239...
1962
	  unsigned short e = bfd_get_16 (abfd, data + i + 8 + j * 2);
2239
	  unsigned short e = bfd_get_16 (abfd, p);
1963
	  unsigned int t = (e & 0xF000) >> 12;
2240
	  unsigned int t = (e & 0xF000) >> 12;
1964
	  int off = e & 0x0FFF;
2241
	  int off = e & 0x0FFF;
Line -... Line 2242...
-
 
2242
 
-
 
2243
	  if (t >= sizeof (tbl) / sizeof (tbl[0]))
-
 
2244
	    t = (sizeof (tbl) / sizeof (tbl[0])) - 1;
1965
 
2245
 
1966
	  if (t >= sizeof (tbl) / sizeof (tbl[0]))
2246
	  fprintf (file,
1967
	    t = (sizeof (tbl) / sizeof (tbl[0])) - 1;
2247
		   _("\treloc %4d offset %4x [%4lx] %s"),
1968
 
2248
		   j, off, (unsigned long) (off + virtual_address), tbl[t]);
1969
	  fprintf (file,
2249
 
1970
		   _("\treloc %4d offset %4x [%4lx] %s"),
2250
	  p += 2;
1971
		   j, off, (unsigned long) (off + virtual_address), tbl[t]);
-
 
1972
 
2251
	  j++;
1973
	  /* HIGHADJ takes an argument, - the next record *is* the
2252
 
Line 1974... Line 2253...
1974
	     low 16 bits of addend.  */
2253
	  /* HIGHADJ takes an argument, - the next record *is* the
1975
	  if (t == IMAGE_REL_BASED_HIGHADJ)
2254
	     low 16 bits of addend.  */
-
 
2255
	  if (t == IMAGE_REL_BASED_HIGHADJ && p + 2 <= chunk_end)
-
 
2256
	    {
-
 
2257
	      fprintf (file, " (%4x)", (unsigned int) bfd_get_16 (abfd, p));
Line 1976... Line 2258...
1976
	    {
2258
	      p += 2;
1977
	      fprintf (file, " (%4x)",
2259
	      j++;
Line -... Line 2260...
-
 
2260
	    }
-
 
2261
 
-
 
2262
	  fprintf (file, "\n");
-
 
2263
	}
-
 
2264
    }
-
 
2265
 
-
 
2266
  free (data);
-
 
2267
 
-
 
2268
  return TRUE;
-
 
2269
}
-
 
2270

-
 
2271
/* A data structure describing the regions of a .rsrc section.
-
 
2272
   Some fields are filled in as the section is parsed.  */
-
 
2273
 
-
 
2274
typedef struct rsrc_regions
-
 
2275
{
-
 
2276
  bfd_byte * section_start;
-
 
2277
  bfd_byte * section_end;
-
 
2278
  bfd_byte * strings_start;
-
 
2279
  bfd_byte * resource_start;
-
 
2280
} rsrc_regions;
-
 
2281
 
-
 
2282
static bfd_byte *
-
 
2283
rsrc_print_resource_directory (FILE * , bfd *, unsigned int, bfd_byte *,
-
 
2284
			       rsrc_regions *, bfd_vma);
-
 
2285
 
-
 
2286
/* Print the resource entry at DATA, with the text indented by INDENT.
-
 
2287
   Recusively calls rsrc_print_resource_directory to print the contents
-
 
2288
   of directory entries.
-
 
2289
   Returns the address of the end of the data associated with the entry
-
 
2290
   or section_end + 1 upon failure.  */
-
 
2291
 
-
 
2292
static bfd_byte *
-
 
2293
rsrc_print_resource_entries (FILE *         file,
-
 
2294
			     bfd *          abfd,
-
 
2295
			     unsigned int   indent,
-
 
2296
			     bfd_boolean    is_name,
-
 
2297
			     bfd_byte *     data,
-
 
2298
			     rsrc_regions * regions,
-
 
2299
			     bfd_vma        rva_bias)
-
 
2300
{
-
 
2301
  unsigned long entry, addr, size;
-
 
2302
  bfd_byte * leaf;
-
 
2303
 
-
 
2304
  if (data + 8 >= regions->section_end)
-
 
2305
    return regions->section_end + 1;
-
 
2306
 
-
 
2307
  fprintf (file, _("%03x %*.s Entry: "), (int)(data - regions->section_start), indent, " ");
-
 
2308
 
-
 
2309
  entry = (unsigned long) bfd_get_32 (abfd, data);
-
 
2310
  if (is_name)
-
 
2311
    {
-
 
2312
      bfd_byte * name;
-
 
2313
 
-
 
2314
      /* Note - the documentation says that this field is an RVA value
-
 
2315
	 but windres appears to produce a section relative offset with
-
 
2316
	 the top bit set.  Support both styles for now.  */
-
 
2317
      if (HighBitSet (entry))
-
 
2318
	name = regions->section_start + WithoutHighBit (entry);
-
 
2319
      else
-
 
2320
	name = regions->section_start + entry - rva_bias;
-
 
2321
 
-
 
2322
      if (name + 2 < regions->section_end && name > regions->section_start)
-
 
2323
	{
-
 
2324
	  unsigned int len;
-
 
2325
 
-
 
2326
	  if (regions->strings_start == NULL)
-
 
2327
	    regions->strings_start = name;
-
 
2328
 
-
 
2329
	  len = bfd_get_16 (abfd, name);
-
 
2330
 
-
 
2331
	  fprintf (file, _("name: [val: %08lx len %d]: "), entry, len);
-
 
2332
 
-
 
2333
	  if (name + 2 + len * 2 < regions->section_end)
-
 
2334
	    {
-
 
2335
	      /* This strange loop is to cope with multibyte characters.  */
-
 
2336
	      while (len --)
-
 
2337
		{
-
 
2338
		  char c;
-
 
2339
 
-
 
2340
		  name += 2;
-
 
2341
		  c = * name;
-
 
2342
		  /* Avoid printing control characters.  */
-
 
2343
		  if (c > 0 && c < 32)
-
 
2344
		    fprintf (file, "^%c", c + 64);
-
 
2345
		  else
-
 
2346
		    fprintf (file, "%.1s", name);
-
 
2347
		}
-
 
2348
	    }
-
 
2349
	  else
-
 
2350
	    {
-
 
2351
	      fprintf (file, _("\n"), len);
-
 
2352
	      /* PR binutils/17512: Do not try to continue decoding a
-
 
2353
		 corrupted resource section.  It is likely to end up with
-
 
2354
		 reams of extraneous output.  FIXME: We could probably
-
 
2355
		 continue if we disable the printing of strings...  */
-
 
2356
	      return regions->section_end + 1;
-
 
2357
	    }
-
 
2358
	}
-
 
2359
      else
-
 
2360
	{
-
 
2361
	  fprintf (file, _("\n"), entry);
-
 
2362
	  return regions->section_end + 1;
-
 
2363
	}
-
 
2364
    }
-
 
2365
  else
-
 
2366
    fprintf (file, _("ID: %#08lx"), entry);
-
 
2367
 
-
 
2368
  entry = (long) bfd_get_32 (abfd, data + 4);
-
 
2369
  fprintf (file, _(", Value: %#08lx\n"), entry);
-
 
2370
 
-
 
2371
  if (HighBitSet  (entry))
-
 
2372
    {
-
 
2373
      data = regions->section_start + WithoutHighBit (entry);
-
 
2374
      if (data <= regions->section_start || data > regions->section_end)
-
 
2375
	return regions->section_end + 1;
-
 
2376
 
-
 
2377
      /* FIXME: PR binutils/17512: A corrupt file could contain a loop
-
 
2378
	 in the resource table.  We need some way to detect this.  */
-
 
2379
      return rsrc_print_resource_directory (file, abfd, indent + 1, data,
-
 
2380
					    regions, rva_bias);
-
 
2381
    }
-
 
2382
 
-
 
2383
  leaf = regions->section_start + entry;
-
 
2384
 
-
 
2385
  if (leaf + 16 >= regions->section_end
-
 
2386
      /* PR 17512: file: 055dff7e.  */
-
 
2387
      || leaf < regions->section_start)
-
 
2388
    return regions->section_end + 1;
-
 
2389
 
-
 
2390
  fprintf (file, _("%03x %*.s  Leaf: Addr: %#08lx, Size: %#08lx, Codepage: %d\n"),
-
 
2391
	   (int) (entry), indent, " ",
-
 
2392
	   addr = (long) bfd_get_32 (abfd, leaf),
-
 
2393
	   size = (long) bfd_get_32 (abfd, leaf + 4),
-
 
2394
	   (int) bfd_get_32 (abfd, leaf + 8));
-
 
2395
 
-
 
2396
  /* Check that the reserved entry is 0.  */
-
 
2397
  if (bfd_get_32 (abfd, leaf + 12) != 0
-
 
2398
      /* And that the data address/size is valid too.  */
-
 
2399
      || (regions->section_start + (addr - rva_bias) + size > regions->section_end))
-
 
2400
    return regions->section_end + 1;
-
 
2401
 
-
 
2402
  if (regions->resource_start == NULL)
-
 
2403
    regions->resource_start = regions->section_start + (addr - rva_bias);
-
 
2404
 
-
 
2405
  return regions->section_start + (addr - rva_bias) + size;
-
 
2406
}
-
 
2407
 
-
 
2408
#define max(a,b) ((a) > (b) ? (a) : (b))
-
 
2409
#define min(a,b) ((a) < (b) ? (a) : (b))
-
 
2410
 
-
 
2411
static bfd_byte *
-
 
2412
rsrc_print_resource_directory (FILE *         file,
-
 
2413
			       bfd *          abfd,
-
 
2414
			       unsigned int   indent,
-
 
2415
			       bfd_byte *     data,
-
 
2416
			       rsrc_regions * regions,
-
 
2417
			       bfd_vma        rva_bias)
-
 
2418
{
-
 
2419
  unsigned int num_names, num_ids;
-
 
2420
  bfd_byte * highest_data = data;
-
 
2421
 
-
 
2422
  if (data + 16 >= regions->section_end)
-
 
2423
    return regions->section_end + 1;
-
 
2424
 
-
 
2425
  fprintf (file, "%03x %*.s ", (int)(data - regions->section_start), indent, " ");
-
 
2426
  switch (indent)
-
 
2427
    {
-
 
2428
    case 0: fprintf (file, "Type"); break;
-
 
2429
    case 2: fprintf (file, "Name"); break;
-
 
2430
    case 4: fprintf (file, "Language"); break;
-
 
2431
    default:
-
 
2432
      fprintf (file, _("\n"), indent);
-
 
2433
      /* FIXME: For now we end the printing here.  If in the
-
 
2434
	 future more directory types are added to the RSRC spec
-
 
2435
	 then we will need to change this.  */
-
 
2436
      return regions->section_end + 1;
-
 
2437
    }
-
 
2438
 
-
 
2439
  fprintf (file, _(" Table: Char: %d, Time: %08lx, Ver: %d/%d, Num Names: %d, IDs: %d\n"),
-
 
2440
	   (int) bfd_get_32 (abfd, data),
-
 
2441
	   (long) bfd_get_32 (abfd, data + 4),
-
 
2442
	   (int)  bfd_get_16 (abfd, data + 8),
-
 
2443
	   (int)  bfd_get_16 (abfd, data + 10),
-
 
2444
	   num_names = (int) bfd_get_16 (abfd, data + 12),
-
 
2445
	   num_ids =   (int) bfd_get_16 (abfd, data + 14));
-
 
2446
  data += 16;
-
 
2447
 
-
 
2448
  while (num_names --)
-
 
2449
    {
-
 
2450
      bfd_byte * entry_end;
-
 
2451
 
-
 
2452
      entry_end = rsrc_print_resource_entries (file, abfd, indent + 1, TRUE,
-
 
2453
					       data, regions, rva_bias);
-
 
2454
      data += 8;
-
 
2455
      highest_data = max (highest_data, entry_end);
-
 
2456
      if (entry_end >= regions->section_end)
-
 
2457
	return entry_end;
-
 
2458
    }
-
 
2459
 
-
 
2460
  while (num_ids --)
-
 
2461
    {
-
 
2462
      bfd_byte * entry_end;
-
 
2463
 
-
 
2464
      entry_end = rsrc_print_resource_entries (file, abfd, indent + 1, FALSE,
-
 
2465
					       data, regions, rva_bias);
-
 
2466
      data += 8;
-
 
2467
      highest_data = max (highest_data, entry_end);
-
 
2468
      if (entry_end >= regions->section_end)
-
 
2469
	return entry_end;
-
 
2470
    }
-
 
2471
 
-
 
2472
  return max (highest_data, data);
-
 
2473
}
-
 
2474
 
-
 
2475
/* Display the contents of a .rsrc section.  We do not try to
-
 
2476
   reproduce the resources, windres does that.  Instead we dump
-
 
2477
   the tables in a human readable format.  */
-
 
2478
 
-
 
2479
static bfd_boolean
-
 
2480
rsrc_print_section (bfd * abfd, void * vfile)
-
 
2481
{
-
 
2482
  bfd_vma rva_bias;
-
 
2483
  pe_data_type * pe;
-
 
2484
  FILE * file = (FILE *) vfile;
-
 
2485
  bfd_size_type datasize;
-
 
2486
  asection * section;
-
 
2487
  bfd_byte * data;
-
 
2488
  rsrc_regions regions;
-
 
2489
 
-
 
2490
  pe = pe_data (abfd);
-
 
2491
  if (pe == NULL)
-
 
2492
    return TRUE;
-
 
2493
 
-
 
2494
  section = bfd_get_section_by_name (abfd, ".rsrc");
-
 
2495
  if (section == NULL)
-
 
2496
    return TRUE;
-
 
2497
  if (!(section->flags & SEC_HAS_CONTENTS))
1978
		       ((unsigned int)
2498
    return TRUE;
-
 
2499
 
-
 
2500
  datasize = section->size;
-
 
2501
  if (datasize == 0)
-
 
2502
    return TRUE;
-
 
2503
 
-
 
2504
  rva_bias = section->vma - pe->pe_opthdr.ImageBase;
-
 
2505
 
-
 
2506
  if (! bfd_malloc_and_get_section (abfd, section, & data))
-
 
2507
    {
-
 
2508
      if (data != NULL)
-
 
2509
	free (data);
-
 
2510
      return FALSE;
-
 
2511
    }
-
 
2512
 
-
 
2513
  regions.section_start = data;
-
 
2514
  regions.section_end = data + datasize;
-
 
2515
  regions.strings_start = NULL;
-
 
2516
  regions.resource_start = NULL;
-
 
2517
 
-
 
2518
  fflush (file);
-
 
2519
  fprintf (file, "\nThe .rsrc Resource Directory section:\n");
-
 
2520
 
-
 
2521
  while (data < regions.section_end)
-
 
2522
    {
-
 
2523
      bfd_byte * p = data;
-
 
2524
 
-
 
2525
      data = rsrc_print_resource_directory (file, abfd, 0, data, & regions, rva_bias);
-
 
2526
 
-
 
2527
      if (data == regions.section_end + 1)
-
 
2528
	fprintf (file, _("Corrupt .rsrc section detected!\n"));
-
 
2529
      else
-
 
2530
	{
-
 
2531
	  /* Align data before continuing.  */
-
 
2532
	  int align = (1 << section->alignment_power) - 1;
-
 
2533
 
-
 
2534
	  data = (bfd_byte *) (((ptrdiff_t) (data + align)) & ~ align);
-
 
2535
	  rva_bias += data - p;
-
 
2536
 
-
 
2537
	  /* For reasons that are unclear .rsrc sections are sometimes created
-
 
2538
	     aligned to a 1^3 boundary even when their alignment is set at
-
 
2539
	     1^2.  Catch that case here before we issue a spurious warning
-
 
2540
	     message.  */
-
 
2541
	  if (data == (regions.section_end - 4))
-
 
2542
	    data = regions.section_end;
-
 
2543
	  else if (data < regions.section_end)
-
 
2544
	    {
-
 
2545
	      /* If the extra data is all zeros then do not complain.
-
 
2546
		 This is just padding so that the section meets the
-
 
2547
		 page size requirements.  */
-
 
2548
	      while (++ data < regions.section_end)
-
 
2549
		if (*data != 0)
-
 
2550
		  break;
-
 
2551
	      if (data < regions.section_end)
-
 
2552
		fprintf (file, _("\nWARNING: Extra data in .rsrc section - it will be ignored by Windows:\n"));
-
 
2553
	    }
-
 
2554
	}
-
 
2555
    }
-
 
2556
 
-
 
2557
  if (regions.strings_start != NULL)
-
 
2558
    fprintf (file, " String table starts at offset: %#03x\n",
-
 
2559
	     (int) (regions.strings_start - regions.section_start));
-
 
2560
  if (regions.resource_start != NULL)
-
 
2561
    fprintf (file, " Resources start at offset: %#03x\n",
-
 
2562
	     (int) (regions.resource_start - regions.section_start));
-
 
2563
 
-
 
2564
  free (regions.section_start);
-
 
2565
  return TRUE;
-
 
2566
}
-
 
2567
 
-
 
2568
#define IMAGE_NUMBEROF_DEBUG_TYPES 12
-
 
2569
 
-
 
2570
static char * debug_type_names[IMAGE_NUMBEROF_DEBUG_TYPES] =
-
 
2571
{
-
 
2572
  "Unknown",
-
 
2573
  "COFF",
-
 
2574
  "CodeView",
-
 
2575
  "FPO",
-
 
2576
  "Misc",
-
 
2577
  "Exception",
-
 
2578
  "Fixup",
-
 
2579
  "OMAP-to-SRC",
-
 
2580
  "OMAP-from-SRC",
-
 
2581
  "Borland",
-
 
2582
  "Reserved",
-
 
2583
  "CLSID",
-
 
2584
};
-
 
2585
 
-
 
2586
static bfd_boolean
-
 
2587
pe_print_debugdata (bfd * abfd, void * vfile)
-
 
2588
{
-
 
2589
  FILE *file = (FILE *) vfile;
-
 
2590
  pe_data_type *pe = pe_data (abfd);
-
 
2591
  struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
-
 
2592
  asection *section;
-
 
2593
  bfd_byte *data = 0;
-
 
2594
  bfd_size_type dataoff;
-
 
2595
  unsigned int i;
-
 
2596
 
-
 
2597
  bfd_vma addr = extra->DataDirectory[PE_DEBUG_DATA].VirtualAddress;
-
 
2598
  bfd_size_type size = extra->DataDirectory[PE_DEBUG_DATA].Size;
-
 
2599
 
-
 
2600
  if (size == 0)
-
 
2601
    return TRUE;
-
 
2602
 
-
 
2603
  addr += extra->ImageBase;
-
 
2604
  for (section = abfd->sections; section != NULL; section = section->next)
-
 
2605
    {
-
 
2606
      if ((addr >= section->vma) && (addr < (section->vma + section->size)))
-
 
2607
        break;
-
 
2608
    }
-
 
2609
 
-
 
2610
  if (section == NULL)
-
 
2611
    {
-
 
2612
      fprintf (file,
-
 
2613
               _("\nThere is a debug directory, but the section containing it could not be found\n"));
-
 
2614
      return TRUE;
-
 
2615
    }
-
 
2616
  else if (!(section->flags & SEC_HAS_CONTENTS))
-
 
2617
    {
-
 
2618
      fprintf (file,
-
 
2619
               _("\nThere is a debug directory in %s, but that section has no contents\n"),
-
 
2620
               section->name);
-
 
2621
      return TRUE;
-
 
2622
    }
-
 
2623
  else if (section->size < size)
-
 
2624
    {
-
 
2625
      fprintf (file,
-
 
2626
               _("\nError: section %s contains the debug data starting address but it is too small\n"),
-
 
2627
               section->name);
-
 
2628
      return FALSE;
-
 
2629
    }
-
 
2630
 
-
 
2631
  fprintf (file, _("\nThere is a debug directory in %s at 0x%lx\n\n"),
-
 
2632
	   section->name, (unsigned long) addr);
-
 
2633
 
-
 
2634
  dataoff = addr - section->vma;
-
 
2635
 
-
 
2636
  if (size > (section->size - dataoff))
-
 
2637
    {
-
 
2638
      fprintf (file, _("The debug data size field in the data directory is too big for the section"));
-
 
2639
      return FALSE;
-
 
2640
    }
-
 
2641
 
-
 
2642
  fprintf (file,
-
 
2643
	   _("Type                Size     Rva      Offset\n"));
-
 
2644
 
-
 
2645
  /* Read the whole section.  */
-
 
2646
  if (!bfd_malloc_and_get_section (abfd, section, &data))
-
 
2647
    {
-
 
2648
      if (data != NULL)
-
 
2649
  free (data);
-
 
2650
      return FALSE;
-
 
2651
    }
-
 
2652
 
-
 
2653
  for (i = 0; i < size / sizeof (struct external_IMAGE_DEBUG_DIRECTORY); i++)
-
 
2654
    {
-
 
2655
      const char *type_name;
-
 
2656
      struct external_IMAGE_DEBUG_DIRECTORY *ext
-
 
2657
	= &((struct external_IMAGE_DEBUG_DIRECTORY *)(data + dataoff))[i];
-
 
2658
      struct internal_IMAGE_DEBUG_DIRECTORY idd;
-
 
2659
 
-
 
2660
      _bfd_pei_swap_debugdir_in (abfd, ext, &idd);
-
 
2661
 
-
 
2662
      if ((idd.Type) >= IMAGE_NUMBEROF_DEBUG_TYPES)
-
 
2663
        type_name = debug_type_names[0];
-
 
2664
      else
-
 
2665
        type_name = debug_type_names[idd.Type];
-
 
2666
 
-
 
2667
      fprintf (file, " %2ld  %14s %08lx %08lx %08lx\n",
-
 
2668
	       idd.Type, type_name, idd.SizeOfData,
-
 
2669
	       idd.AddressOfRawData, idd.PointerToRawData);
-
 
2670
 
-
 
2671
      if (idd.Type == PE_IMAGE_DEBUG_TYPE_CODEVIEW)
-
 
2672
        {
-
 
2673
          char signature[CV_INFO_SIGNATURE_LENGTH * 2 + 1];
-
 
2674
	  /* PR 17512: file: 065-29434-0.001:0.1
-
 
2675
	     We need to use a 32-bit aligned buffer
-
 
2676
	     to safely read in a codeview record.  */
-
 
2677
          char buffer[256 + 1] ATTRIBUTE_ALIGNED_ALIGNOF (CODEVIEW_INFO);
-
 
2678
 
-
 
2679
          CODEVIEW_INFO *cvinfo = (CODEVIEW_INFO *) buffer;
-
 
2680
 
-
 
2681
          /* The debug entry doesn't have to have to be in a section,
-
 
2682
	     in which case AddressOfRawData is 0, so always use PointerToRawData.  */
-
 
2683
          if (!_bfd_pei_slurp_codeview_record (abfd, (file_ptr) idd.PointerToRawData,
-
 
2684
					       idd.SizeOfData, cvinfo))
-
 
2685
            continue;
-
 
2686
 
-
 
2687
          for (i = 0; i < cvinfo->SignatureLength; i++)
Line 1979... Line 2688...
1979
			bfd_get_16 (abfd, data + i + 8 + j * 2 + 2)));
2688
            sprintf (&signature[i*2], "%02x", cvinfo->Signature[i] & 0xff);
1980
	      j++;
2689
 
Line 1981... Line 2690...
1981
	    }
2690
          fprintf (file, "(format %c%c%c%c signature %s age %ld)\n",
Line 2162... Line 2871...
2162
  if (bfd_coff_have_print_pdata (abfd))
2871
  if (bfd_coff_have_print_pdata (abfd))
2163
    bfd_coff_print_pdata (abfd, vfile);
2872
    bfd_coff_print_pdata (abfd, vfile);
2164
  else
2873
  else
2165
    pe_print_pdata (abfd, vfile);
2874
    pe_print_pdata (abfd, vfile);
2166
  pe_print_reloc (abfd, vfile);
2875
  pe_print_reloc (abfd, vfile);
-
 
2876
  pe_print_debugdata (abfd, file);
-
 
2877
 
-
 
2878
  rsrc_print_section (abfd, vfile);
Line 2167... Line 2879...
2167
 
2879
 
2168
  return TRUE;
2880
  return TRUE;
Line -... Line 2881...
-
 
2881
}
-
 
2882
 
-
 
2883
static bfd_boolean
-
 
2884
is_vma_in_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sect, void *obj)
-
 
2885
{
-
 
2886
  bfd_vma addr = * (bfd_vma *) obj;
-
 
2887
  return (addr >= sect->vma) && (addr < (sect->vma + sect->size));
-
 
2888
}
-
 
2889
 
-
 
2890
static asection *
-
 
2891
find_section_by_vma (bfd *abfd, bfd_vma addr)
-
 
2892
{
-
 
2893
  return bfd_sections_find_if (abfd, is_vma_in_section, (void *) & addr);
2169
}
2894
}
2170
 
2895
 
Line 2171... Line 2896...
2171
/* Copy any private info we understand from the input bfd
2896
/* Copy any private info we understand from the input bfd
2172
   to the output bfd.  */
2897
   to the output bfd.  */
Line 2204... Line 2929...
2204
     won't be added.  */
2929
     won't be added.  */
2205
  if (! pe_data (ibfd)->has_reloc_section
2930
  if (! pe_data (ibfd)->has_reloc_section
2206
      && ! (pe_data (ibfd)->real_flags & IMAGE_FILE_RELOCS_STRIPPED))
2931
      && ! (pe_data (ibfd)->real_flags & IMAGE_FILE_RELOCS_STRIPPED))
2207
    pe_data (obfd)->dont_strip_reloc = 1;
2932
    pe_data (obfd)->dont_strip_reloc = 1;
Line -... Line 2933...
-
 
2933
 
-
 
2934
  /* The file offsets contained in the debug directory need rewriting.  */
-
 
2935
  if (ope->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size != 0)
-
 
2936
    {
-
 
2937
      bfd_vma addr = ope->pe_opthdr.DataDirectory[PE_DEBUG_DATA].VirtualAddress
-
 
2938
	+ ope->pe_opthdr.ImageBase;
-
 
2939
      asection *section = find_section_by_vma (obfd, addr);
-
 
2940
      bfd_byte *data;
-
 
2941
 
-
 
2942
      if (section && bfd_malloc_and_get_section (obfd, section, &data))
-
 
2943
        {
-
 
2944
          unsigned int i;
-
 
2945
          struct external_IMAGE_DEBUG_DIRECTORY *dd =
-
 
2946
	    (struct external_IMAGE_DEBUG_DIRECTORY *)(data + (addr - section->vma));
-
 
2947
 
-
 
2948
	  /* PR 17512: file: 0f15796a.  */
-
 
2949
	  if (ope->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size + (addr - section->vma)
-
 
2950
	      > bfd_get_section_size (section))
-
 
2951
	    {
-
 
2952
	      _bfd_error_handler (_("%B: Data Directory size (%lx) exceeds space left in section (%lx)"),
-
 
2953
				  obfd, ope->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size,
-
 
2954
				  bfd_get_section_size (section) - (addr - section->vma));
-
 
2955
	      return FALSE;
-
 
2956
	    }
-
 
2957
 
-
 
2958
          for (i = 0; i < ope->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size
-
 
2959
		 / sizeof (struct external_IMAGE_DEBUG_DIRECTORY); i++)
-
 
2960
            {
-
 
2961
              asection *ddsection;
-
 
2962
              struct external_IMAGE_DEBUG_DIRECTORY *edd = &(dd[i]);
-
 
2963
              struct internal_IMAGE_DEBUG_DIRECTORY idd;
-
 
2964
 
-
 
2965
              _bfd_pei_swap_debugdir_in (obfd, edd, &idd);
-
 
2966
 
-
 
2967
              if (idd.AddressOfRawData == 0)
-
 
2968
                continue; /* RVA 0 means only offset is valid, not handled yet.  */
-
 
2969
 
-
 
2970
              ddsection = find_section_by_vma (obfd, idd.AddressOfRawData + ope->pe_opthdr.ImageBase);
-
 
2971
              if (!ddsection)
-
 
2972
                continue; /* Not in a section! */
-
 
2973
 
-
 
2974
              idd.PointerToRawData = ddsection->filepos + (idd.AddressOfRawData
-
 
2975
							   + ope->pe_opthdr.ImageBase) - ddsection->vma;
-
 
2976
 
-
 
2977
              _bfd_pei_swap_debugdir_out (obfd, &idd, edd);
-
 
2978
            }
-
 
2979
 
-
 
2980
          if (!bfd_set_section_contents (obfd, section, data, 0, section->size))
-
 
2981
	    {
-
 
2982
	      _bfd_error_handler (_("Failed to update file offsets in debug directory"));
-
 
2983
	      return FALSE;
-
 
2984
	    }
-
 
2985
        }
-
 
2986
      else if (section)
-
 
2987
	{
-
 
2988
	  _bfd_error_handler (_("%B: Failed to read debug data section"), obfd);
-
 
2989
	  return FALSE;
-
 
2990
	}
-
 
2991
    }
2208
 
2992
 
2209
  return TRUE;
2993
  return TRUE;
Line 2210... Line 2994...
2210
}
2994
}
Line 2268... Line 3052...
2268
  /* We compare just begin address.  */
3052
  /* We compare just begin address.  */
2269
  return 0;
3053
  return 0;
2270
}
3054
}
2271
#endif
3055
#endif
Line -... Line 3056...
-
 
3056

-
 
3057
/* Functions to process a .rsrc section.  */
-
 
3058
 
-
 
3059
static unsigned int sizeof_leaves;
-
 
3060
static unsigned int sizeof_strings;
-
 
3061
static unsigned int sizeof_tables_and_entries;
-
 
3062
 
-
 
3063
static bfd_byte *
-
 
3064
rsrc_count_directory (bfd *, bfd_byte *, bfd_byte *, bfd_byte *, bfd_vma);
-
 
3065
 
-
 
3066
static bfd_byte *
-
 
3067
rsrc_count_entries (bfd *          abfd,
-
 
3068
		    bfd_boolean    is_name,
-
 
3069
		    bfd_byte *     datastart,
-
 
3070
		    bfd_byte *     data,
-
 
3071
		    bfd_byte *     dataend,
-
 
3072
		    bfd_vma        rva_bias)
-
 
3073
{
-
 
3074
  unsigned long entry, addr, size;
-
 
3075
 
-
 
3076
  if (data + 8 >= dataend)
-
 
3077
    return dataend + 1;
-
 
3078
 
-
 
3079
  if (is_name)
-
 
3080
    {
-
 
3081
      bfd_byte * name;
-
 
3082
 
-
 
3083
      entry = (long) bfd_get_32 (abfd, data);
-
 
3084
 
-
 
3085
      if (HighBitSet (entry))
-
 
3086
	name = datastart + WithoutHighBit (entry);
-
 
3087
      else
-
 
3088
	name = datastart + entry - rva_bias;
-
 
3089
 
-
 
3090
      if (name + 2 >= dataend || name < datastart)
-
 
3091
	return dataend + 1;
-
 
3092
 
-
 
3093
      unsigned int len = bfd_get_16 (abfd, name);
-
 
3094
      if (len == 0 || len > 256)
-
 
3095
	return dataend + 1;
-
 
3096
    }
-
 
3097
 
-
 
3098
  entry = (long) bfd_get_32 (abfd, data + 4);
-
 
3099
 
-
 
3100
  if (HighBitSet (entry))
-
 
3101
    {
-
 
3102
      data = datastart + WithoutHighBit (entry);
-
 
3103
 
-
 
3104
      if (data <= datastart || data >= dataend)
-
 
3105
	return dataend + 1;
-
 
3106
 
-
 
3107
      return rsrc_count_directory (abfd, datastart, data, dataend, rva_bias);
-
 
3108
    }
-
 
3109
 
-
 
3110
  if (datastart + entry + 16 >= dataend)
-
 
3111
    return dataend + 1;
-
 
3112
 
-
 
3113
  addr = (long) bfd_get_32 (abfd, datastart + entry);
-
 
3114
  size = (long) bfd_get_32 (abfd, datastart + entry + 4);
-
 
3115
 
-
 
3116
  return datastart + addr - rva_bias + size;
-
 
3117
}
-
 
3118
 
-
 
3119
static bfd_byte *
-
 
3120
rsrc_count_directory (bfd *          abfd,
-
 
3121
		      bfd_byte *     datastart,
-
 
3122
		      bfd_byte *     data,
-
 
3123
		      bfd_byte *     dataend,
-
 
3124
		      bfd_vma        rva_bias)
-
 
3125
{
-
 
3126
  unsigned int  num_entries, num_ids;
-
 
3127
  bfd_byte *    highest_data = data;
-
 
3128
 
-
 
3129
  if (data + 16 >= dataend)
-
 
3130
    return dataend + 1;
-
 
3131
 
-
 
3132
  num_entries  = (int) bfd_get_16 (abfd, data + 12);
-
 
3133
  num_ids      = (int) bfd_get_16 (abfd, data + 14);
-
 
3134
 
-
 
3135
  num_entries += num_ids;
-
 
3136
 
-
 
3137
  data += 16;
-
 
3138
 
-
 
3139
  while (num_entries --)
-
 
3140
    {
-
 
3141
      bfd_byte * entry_end;
-
 
3142
 
-
 
3143
      entry_end = rsrc_count_entries (abfd, num_entries >= num_ids,
-
 
3144
				      datastart, data, dataend, rva_bias);
-
 
3145
      data += 8;
-
 
3146
      highest_data = max (highest_data, entry_end);
-
 
3147
      if (entry_end >= dataend)
-
 
3148
	break;
-
 
3149
    }
-
 
3150
 
-
 
3151
  return max (highest_data, data);
-
 
3152
}
-
 
3153
 
-
 
3154
typedef struct rsrc_dir_chain
-
 
3155
{
-
 
3156
  unsigned int         num_entries;
-
 
3157
  struct rsrc_entry *  first_entry;
-
 
3158
  struct rsrc_entry *  last_entry;
-
 
3159
} rsrc_dir_chain;
-
 
3160
 
-
 
3161
typedef struct rsrc_directory
-
 
3162
{
-
 
3163
  unsigned int characteristics;
-
 
3164
  unsigned int time;
-
 
3165
  unsigned int major;
-
 
3166
  unsigned int minor;
-
 
3167
 
-
 
3168
  rsrc_dir_chain names;
-
 
3169
  rsrc_dir_chain ids;
-
 
3170
 
-
 
3171
  struct rsrc_entry * entry;
-
 
3172
} rsrc_directory;
-
 
3173
 
-
 
3174
typedef struct rsrc_string
-
 
3175
{
-
 
3176
  unsigned int  len;
-
 
3177
  bfd_byte *    string;
-
 
3178
} rsrc_string;
-
 
3179
 
-
 
3180
typedef struct rsrc_leaf
-
 
3181
{
-
 
3182
  unsigned int  size;
-
 
3183
  unsigned int  codepage;
-
 
3184
  bfd_byte *    data;
-
 
3185
} rsrc_leaf;
-
 
3186
 
-
 
3187
typedef struct rsrc_entry
-
 
3188
{
-
 
3189
  bfd_boolean is_name;
-
 
3190
  union
-
 
3191
  {
-
 
3192
    unsigned int          id;
-
 
3193
    struct rsrc_string    name;
-
 
3194
  } name_id;
-
 
3195
 
-
 
3196
  bfd_boolean is_dir;
-
 
3197
  union
-
 
3198
  {
-
 
3199
    struct rsrc_directory * directory;
-
 
3200
    struct rsrc_leaf *      leaf;
-
 
3201
  } value;
-
 
3202
 
-
 
3203
  struct rsrc_entry *     next_entry;
-
 
3204
  struct rsrc_directory * parent;
-
 
3205
} rsrc_entry;
-
 
3206
 
-
 
3207
static bfd_byte *
-
 
3208
rsrc_parse_directory (bfd *, rsrc_directory *, bfd_byte *,
-
 
3209
		      bfd_byte *, bfd_byte *, bfd_vma, rsrc_entry *);
-
 
3210
 
-
 
3211
static bfd_byte *
-
 
3212
rsrc_parse_entry (bfd *            abfd,
-
 
3213
		  bfd_boolean      is_name,
-
 
3214
		  rsrc_entry *     entry,
-
 
3215
		  bfd_byte *       datastart,
-
 
3216
		  bfd_byte *       data,
-
 
3217
		  bfd_byte *       dataend,
-
 
3218
		  bfd_vma          rva_bias,
-
 
3219
		  rsrc_directory * parent)
-
 
3220
{
-
 
3221
  unsigned long val, addr, size;
-
 
3222
 
-
 
3223
  val = bfd_get_32 (abfd, data);
-
 
3224
 
-
 
3225
  entry->parent = parent;
-
 
3226
  entry->is_name = is_name;
-
 
3227
 
-
 
3228
  if (is_name)
-
 
3229
    {
-
 
3230
      bfd_byte * address;
-
 
3231
 
-
 
3232
      if (HighBitSet (val))
-
 
3233
	{
-
 
3234
	  val = WithoutHighBit (val);
-
 
3235
 
-
 
3236
	  address = datastart + val;
-
 
3237
	}
-
 
3238
      else
-
 
3239
	{
-
 
3240
	  address = datastart + val - rva_bias;
-
 
3241
	}
-
 
3242
 
-
 
3243
      if (address + 3 > dataend)
-
 
3244
	return dataend;
-
 
3245
 
-
 
3246
      entry->name_id.name.len    = bfd_get_16 (abfd, address);
-
 
3247
      entry->name_id.name.string = address + 2;
-
 
3248
    }
-
 
3249
  else
-
 
3250
    entry->name_id.id = val;
-
 
3251
 
-
 
3252
  val = bfd_get_32 (abfd, data + 4);
-
 
3253
 
-
 
3254
  if (HighBitSet (val))
-
 
3255
    {
-
 
3256
      entry->is_dir = TRUE;
-
 
3257
      entry->value.directory = bfd_malloc (sizeof * entry->value.directory);
-
 
3258
      if (entry->value.directory == NULL)
-
 
3259
	return dataend;
-
 
3260
 
-
 
3261
      return rsrc_parse_directory (abfd, entry->value.directory,
-
 
3262
				   datastart,
-
 
3263
				   datastart + WithoutHighBit (val),
-
 
3264
				   dataend, rva_bias, entry);
-
 
3265
    }
-
 
3266
 
-
 
3267
  entry->is_dir = FALSE;
-
 
3268
  entry->value.leaf = bfd_malloc (sizeof * entry->value.leaf);
-
 
3269
  if (entry->value.leaf == NULL)
-
 
3270
    return dataend;
-
 
3271
 
-
 
3272
  data = datastart + val;
-
 
3273
  if (data < datastart || data >= dataend)
-
 
3274
    return dataend;
-
 
3275
 
-
 
3276
  addr = bfd_get_32 (abfd, data);
-
 
3277
  size = entry->value.leaf->size = bfd_get_32 (abfd, data + 4);
-
 
3278
  entry->value.leaf->codepage = bfd_get_32 (abfd, data + 8);
-
 
3279
  /* FIXME: We assume that the reserved field (data + 12) is OK.  */
-
 
3280
 
-
 
3281
  entry->value.leaf->data = bfd_malloc (size);
-
 
3282
  if (entry->value.leaf->data == NULL)
-
 
3283
    return dataend;
-
 
3284
 
-
 
3285
  memcpy (entry->value.leaf->data, datastart + addr - rva_bias, size);
-
 
3286
  return datastart + (addr - rva_bias) + size;
-
 
3287
}
-
 
3288
 
-
 
3289
static bfd_byte *
-
 
3290
rsrc_parse_entries (bfd *            abfd,
-
 
3291
		    rsrc_dir_chain * chain,
-
 
3292
		    bfd_boolean      is_name,
-
 
3293
		    bfd_byte *       highest_data,
-
 
3294
		    bfd_byte *       datastart,
-
 
3295
		    bfd_byte *       data,
-
 
3296
		    bfd_byte *       dataend,
-
 
3297
		    bfd_vma          rva_bias,
-
 
3298
		    rsrc_directory * parent)
-
 
3299
{
-
 
3300
  unsigned int i;
-
 
3301
  rsrc_entry * entry;
-
 
3302
 
-
 
3303
  if (chain->num_entries == 0)
-
 
3304
    {
-
 
3305
      chain->first_entry = chain->last_entry = NULL;
-
 
3306
      return highest_data;
-
 
3307
    }
-
 
3308
 
-
 
3309
  entry = bfd_malloc (sizeof * entry);
-
 
3310
  if (entry == NULL)
-
 
3311
    return dataend;
-
 
3312
 
-
 
3313
  chain->first_entry = entry;
-
 
3314
 
-
 
3315
  for (i = chain->num_entries; i--;)
-
 
3316
    {
-
 
3317
      bfd_byte * entry_end;
-
 
3318
 
-
 
3319
      entry_end = rsrc_parse_entry (abfd, is_name, entry, datastart,
-
 
3320
				    data, dataend, rva_bias, parent);
-
 
3321
      data += 8;
-
 
3322
      highest_data = max (entry_end, highest_data);
-
 
3323
      if (entry_end > dataend)
-
 
3324
	return dataend;
-
 
3325
 
-
 
3326
      if (i)
-
 
3327
	{
-
 
3328
	  entry->next_entry = bfd_malloc (sizeof * entry);
-
 
3329
	  entry = entry->next_entry;
-
 
3330
	  if (entry == NULL)
-
 
3331
	    return dataend;
-
 
3332
	}
-
 
3333
      else
-
 
3334
	entry->next_entry = NULL;
-
 
3335
    }
-
 
3336
 
-
 
3337
  chain->last_entry = entry;
-
 
3338
 
-
 
3339
  return highest_data;
-
 
3340
}
-
 
3341
 
-
 
3342
static bfd_byte *
-
 
3343
rsrc_parse_directory (bfd *            abfd,
-
 
3344
		      rsrc_directory * table,
-
 
3345
		      bfd_byte *       datastart,
-
 
3346
		      bfd_byte *       data,
-
 
3347
		      bfd_byte *       dataend,
-
 
3348
		      bfd_vma          rva_bias,
-
 
3349
		      rsrc_entry *     entry)
-
 
3350
{
-
 
3351
  bfd_byte * highest_data = data;
-
 
3352
 
-
 
3353
  if (table == NULL)
-
 
3354
    return dataend;
-
 
3355
 
-
 
3356
  table->characteristics = bfd_get_32 (abfd, data);
-
 
3357
  table->time = bfd_get_32 (abfd, data + 4);
-
 
3358
  table->major = bfd_get_16 (abfd, data + 8);
-
 
3359
  table->minor = bfd_get_16 (abfd, data + 10);
-
 
3360
  table->names.num_entries = bfd_get_16 (abfd, data + 12);
-
 
3361
  table->ids.num_entries = bfd_get_16 (abfd, data + 14);
-
 
3362
  table->entry = entry;
-
 
3363
 
-
 
3364
  data += 16;
-
 
3365
 
-
 
3366
  highest_data = rsrc_parse_entries (abfd, & table->names, TRUE, data,
-
 
3367
				     datastart, data, dataend, rva_bias, table);
-
 
3368
  data += table->names.num_entries * 8;
-
 
3369
 
-
 
3370
  highest_data = rsrc_parse_entries (abfd, & table->ids, FALSE, highest_data,
-
 
3371
				     datastart, data, dataend, rva_bias, table);
-
 
3372
  data += table->ids.num_entries * 8;
-
 
3373
 
-
 
3374
  return max (highest_data, data);
-
 
3375
}
-
 
3376
 
-
 
3377
typedef struct rsrc_write_data
-
 
3378
{
-
 
3379
  bfd *      abfd;
-
 
3380
  bfd_byte * datastart;
-
 
3381
  bfd_byte * next_table;
-
 
3382
  bfd_byte * next_leaf;
-
 
3383
  bfd_byte * next_string;
-
 
3384
  bfd_byte * next_data;
-
 
3385
  bfd_vma    rva_bias;
-
 
3386
} rsrc_write_data;
-
 
3387
 
-
 
3388
static void
-
 
3389
rsrc_write_string (rsrc_write_data * data,
-
 
3390
		   rsrc_string *     string)
-
 
3391
{
-
 
3392
  bfd_put_16 (data->abfd, string->len, data->next_string);
-
 
3393
  memcpy (data->next_string + 2, string->string, string->len * 2);
-
 
3394
  data->next_string += (string->len + 1) * 2;
-
 
3395
}
-
 
3396
 
-
 
3397
static inline unsigned int
-
 
3398
rsrc_compute_rva (rsrc_write_data * data,
-
 
3399
		  bfd_byte *        addr)
-
 
3400
{
-
 
3401
  return (addr - data->datastart) + data->rva_bias;
-
 
3402
}
-
 
3403
 
-
 
3404
static void
-
 
3405
rsrc_write_leaf (rsrc_write_data * data,
-
 
3406
		 rsrc_leaf *       leaf)
-
 
3407
{
-
 
3408
  bfd_put_32 (data->abfd, rsrc_compute_rva (data, data->next_data),
-
 
3409
	      data->next_leaf);
-
 
3410
  bfd_put_32 (data->abfd, leaf->size,     data->next_leaf + 4);
-
 
3411
  bfd_put_32 (data->abfd, leaf->codepage, data->next_leaf + 8);
-
 
3412
  bfd_put_32 (data->abfd, 0 /*reserved*/, data->next_leaf + 12);
-
 
3413
  data->next_leaf += 16;
-
 
3414
 
-
 
3415
  memcpy (data->next_data, leaf->data, leaf->size);
-
 
3416
  /* An undocumented feature of Windows resources is that each unit
-
 
3417
     of raw data is 8-byte aligned...  */
-
 
3418
  data->next_data += ((leaf->size + 7) & ~7);
-
 
3419
}
-
 
3420
 
-
 
3421
static void rsrc_write_directory (rsrc_write_data *, rsrc_directory *);
-
 
3422
 
-
 
3423
static void
-
 
3424
rsrc_write_entry (rsrc_write_data *  data,
-
 
3425
		  bfd_byte *         where,
-
 
3426
		  rsrc_entry *       entry)
-
 
3427
{
-
 
3428
  if (entry->is_name)
-
 
3429
    {
-
 
3430
      bfd_put_32 (data->abfd,
-
 
3431
		  SetHighBit (data->next_string - data->datastart),
-
 
3432
		  where);
-
 
3433
      rsrc_write_string (data, & entry->name_id.name);
-
 
3434
    }
-
 
3435
  else
-
 
3436
    bfd_put_32 (data->abfd, entry->name_id.id, where);
-
 
3437
 
-
 
3438
  if (entry->is_dir)
-
 
3439
    {
-
 
3440
      bfd_put_32 (data->abfd,
-
 
3441
		  SetHighBit (data->next_table - data->datastart),
-
 
3442
		  where + 4);
-
 
3443
      rsrc_write_directory (data, entry->value.directory);
-
 
3444
    }
-
 
3445
  else
-
 
3446
    {
-
 
3447
      bfd_put_32 (data->abfd, data->next_leaf - data->datastart, where + 4);
-
 
3448
      rsrc_write_leaf (data, entry->value.leaf);
-
 
3449
    }
-
 
3450
}
-
 
3451
 
-
 
3452
static void
-
 
3453
rsrc_compute_region_sizes (rsrc_directory * dir)
-
 
3454
{
-
 
3455
  struct rsrc_entry * entry;
-
 
3456
 
-
 
3457
  if (dir == NULL)
-
 
3458
    return;
-
 
3459
 
-
 
3460
  sizeof_tables_and_entries += 16;
-
 
3461
 
-
 
3462
  for (entry = dir->names.first_entry; entry != NULL; entry = entry->next_entry)
-
 
3463
    {
-
 
3464
      sizeof_tables_and_entries += 8;
-
 
3465
 
-
 
3466
      sizeof_strings += (entry->name_id.name.len + 1) * 2;
-
 
3467
 
-
 
3468
      if (entry->is_dir)
-
 
3469
	rsrc_compute_region_sizes (entry->value.directory);
-
 
3470
      else
-
 
3471
	sizeof_leaves += 16;
-
 
3472
    }
-
 
3473
 
-
 
3474
  for (entry = dir->ids.first_entry; entry != NULL; entry = entry->next_entry)
-
 
3475
    {
-
 
3476
      sizeof_tables_and_entries += 8;
-
 
3477
 
-
 
3478
      if (entry->is_dir)
-
 
3479
	rsrc_compute_region_sizes (entry->value.directory);
-
 
3480
      else
-
 
3481
	sizeof_leaves += 16;
-
 
3482
    }
-
 
3483
}
-
 
3484
 
-
 
3485
static void
-
 
3486
rsrc_write_directory (rsrc_write_data * data,
-
 
3487
		      rsrc_directory *  dir)
-
 
3488
{
-
 
3489
  rsrc_entry * entry;
-
 
3490
  unsigned int i;
-
 
3491
  bfd_byte * next_entry;
-
 
3492
  bfd_byte * nt;
-
 
3493
 
-
 
3494
  bfd_put_32 (data->abfd, dir->characteristics, data->next_table);
-
 
3495
  bfd_put_32 (data->abfd, 0 /*dir->time*/, data->next_table + 4);
-
 
3496
  bfd_put_16 (data->abfd, dir->major, data->next_table + 8);
-
 
3497
  bfd_put_16 (data->abfd, dir->minor, data->next_table + 10);
-
 
3498
  bfd_put_16 (data->abfd, dir->names.num_entries, data->next_table + 12);
-
 
3499
  bfd_put_16 (data->abfd, dir->ids.num_entries, data->next_table + 14);
-
 
3500
 
-
 
3501
  /* Compute where the entries and the next table will be placed.  */
-
 
3502
  next_entry = data->next_table + 16;
-
 
3503
  data->next_table = next_entry + (dir->names.num_entries * 8)
-
 
3504
    + (dir->ids.num_entries * 8);
-
 
3505
  nt = data->next_table;
-
 
3506
 
-
 
3507
  /* Write the entries.  */
-
 
3508
  for (i = dir->names.num_entries, entry = dir->names.first_entry;
-
 
3509
       i > 0 && entry != NULL;
-
 
3510
       i--, entry = entry->next_entry)
-
 
3511
    {
-
 
3512
      BFD_ASSERT (entry->is_name);
-
 
3513
      rsrc_write_entry (data, next_entry, entry);
-
 
3514
      next_entry += 8;
-
 
3515
    }
-
 
3516
  BFD_ASSERT (i == 0);
-
 
3517
  BFD_ASSERT (entry == NULL);
-
 
3518
 
-
 
3519
  for (i = dir->ids.num_entries, entry = dir->ids.first_entry;
-
 
3520
       i > 0 && entry != NULL;
-
 
3521
       i--, entry = entry->next_entry)
-
 
3522
    {
-
 
3523
      BFD_ASSERT (! entry->is_name);
-
 
3524
      rsrc_write_entry (data, next_entry, entry);
-
 
3525
      next_entry += 8;
-
 
3526
    }
-
 
3527
  BFD_ASSERT (i == 0);
-
 
3528
  BFD_ASSERT (entry == NULL);
-
 
3529
  BFD_ASSERT (nt == next_entry);
-
 
3530
}
-
 
3531
 
-
 
3532
#if defined HAVE_WCHAR_H && ! defined __CYGWIN__ && ! defined __MINGW32__
-
 
3533
/* Return the length (number of units) of the first character in S,
-
 
3534
   putting its 'ucs4_t' representation in *PUC.  */
-
 
3535
 
-
 
3536
static unsigned int
-
 
3537
#if defined HAVE_WCTYPE_H
-
 
3538
u16_mbtouc (wint_t * puc, const unsigned short * s, unsigned int n)
-
 
3539
#else
-
 
3540
u16_mbtouc (wchar_t * puc, const unsigned short * s, unsigned int n)
-
 
3541
#endif
-
 
3542
{
-
 
3543
  unsigned short c = * s;
-
 
3544
 
-
 
3545
  if (c < 0xd800 || c >= 0xe000)
-
 
3546
    {
-
 
3547
      *puc = c;
-
 
3548
      return 1;
-
 
3549
    }
-
 
3550
 
-
 
3551
  if (c < 0xdc00)
-
 
3552
    {
-
 
3553
      if (n >= 2)
-
 
3554
        {
-
 
3555
          if (s[1] >= 0xdc00 && s[1] < 0xe000)
-
 
3556
            {
-
 
3557
              *puc = 0x10000 + ((c - 0xd800) << 10) + (s[1] - 0xdc00);
-
 
3558
              return 2;
-
 
3559
            }
-
 
3560
        }
-
 
3561
      else
-
 
3562
        {
-
 
3563
          /* Incomplete multibyte character.  */
-
 
3564
          *puc = 0xfffd;
-
 
3565
          return n;
-
 
3566
        }
-
 
3567
    }
-
 
3568
 
-
 
3569
  /* Invalid multibyte character.  */
-
 
3570
  *puc = 0xfffd;
-
 
3571
  return 1;
-
 
3572
}
-
 
3573
#endif /* HAVE_WCHAR_H and not Cygwin/Mingw */
-
 
3574
 
-
 
3575
/* Perform a comparison of two entries.  */
-
 
3576
static signed int
-
 
3577
rsrc_cmp (bfd_boolean is_name, rsrc_entry * a, rsrc_entry * b)
-
 
3578
{
-
 
3579
  signed int    res;
-
 
3580
  bfd_byte *    astring;
-
 
3581
  unsigned int  alen;
-
 
3582
  bfd_byte *    bstring;
-
 
3583
  unsigned int  blen;
-
 
3584
 
-
 
3585
  if (! is_name)
-
 
3586
    return a->name_id.id - b->name_id.id;
-
 
3587
 
-
 
3588
  /* We have to perform a case insenstive, unicode string comparison...  */
-
 
3589
  astring = a->name_id.name.string;
-
 
3590
  alen    = a->name_id.name.len;
-
 
3591
  bstring = b->name_id.name.string;
-
 
3592
  blen    = b->name_id.name.len;
-
 
3593
 
-
 
3594
#if defined  __CYGWIN__ || defined __MINGW32__
-
 
3595
  /* Under Windows hosts (both Cygwin and Mingw types),
-
 
3596
     unicode == UTF-16 == wchar_t.  The case insensitive string comparison
-
 
3597
     function however goes by different names in the two environments...  */
-
 
3598
 
-
 
3599
#undef rscpcmp
-
 
3600
#ifdef __CYGWIN__
-
 
3601
#define rscpcmp wcsncasecmp
-
 
3602
#endif
-
 
3603
#ifdef __MINGW32__
-
 
3604
#define rscpcmp wcsnicmp
-
 
3605
#endif
-
 
3606
 
-
 
3607
  res = rscpcmp ((const wchar_t *) astring, (const wchar_t *) bstring,
-
 
3608
		 min (alen, blen));
-
 
3609
 
-
 
3610
#elif defined HAVE_WCHAR_H
-
 
3611
  {
-
 
3612
    unsigned int  i;
-
 
3613
 
-
 
3614
    res = 0;
-
 
3615
    for (i = min (alen, blen); i--; astring += 2, bstring += 2)
-
 
3616
      {
-
 
3617
#if defined HAVE_WCTYPE_H
-
 
3618
	wint_t awc;
-
 
3619
	wint_t bwc;
-
 
3620
#else
-
 
3621
	wchar_t awc;
-
 
3622
	wchar_t bwc;
-
 
3623
#endif
-
 
3624
 
-
 
3625
	/* Convert UTF-16 unicode characters into wchar_t characters
-
 
3626
	   so that we can then perform a case insensitive comparison.  */
-
 
3627
	unsigned int Alen = u16_mbtouc (& awc, (const unsigned short *) astring, 2);
-
 
3628
	unsigned int Blen = u16_mbtouc (& bwc, (const unsigned short *) bstring, 2);
-
 
3629
 
-
 
3630
	if (Alen != Blen)
-
 
3631
	  return Alen - Blen;
-
 
3632
 
-
 
3633
#ifdef HAVE_WCTYPE_H
-
 
3634
	awc = towlower (awc);
-
 
3635
	bwc = towlower (bwc);
-
 
3636
 
-
 
3637
	res = awc - bwc;
-
 
3638
#else
-
 
3639
	res = wcsncasecmp (& awc, & bwc, 1);
-
 
3640
#endif
-
 
3641
	if (res)
-
 
3642
	  break;
-
 
3643
      }
-
 
3644
  }
-
 
3645
#else
-
 
3646
  /* Do the best we can - a case sensitive, untranslated comparison.  */
-
 
3647
  res = memcmp (astring, bstring, min (alen, blen) * 2);
-
 
3648
#endif
-
 
3649
 
-
 
3650
  if (res == 0)
-
 
3651
    res = alen - blen;
-
 
3652
 
-
 
3653
  return res;
-
 
3654
}
-
 
3655
 
-
 
3656
static void
-
 
3657
rsrc_print_name (char * buffer, rsrc_string string)
-
 
3658
{
-
 
3659
  unsigned int  i;
-
 
3660
  bfd_byte *    name = string.string;
-
 
3661
 
-
 
3662
  for (i = string.len; i--; name += 2)
-
 
3663
    sprintf (buffer + strlen (buffer), "%.1s", name);
-
 
3664
}
-
 
3665
 
-
 
3666
static const char *
-
 
3667
rsrc_resource_name (rsrc_entry * entry, rsrc_directory * dir)
-
 
3668
{
-
 
3669
  static char buffer [256];
-
 
3670
  bfd_boolean is_string = FALSE;
-
 
3671
 
-
 
3672
  buffer[0] = 0;
-
 
3673
 
-
 
3674
  if (dir != NULL && dir->entry != NULL && dir->entry->parent != NULL
-
 
3675
      && dir->entry->parent->entry != NULL)
-
 
3676
    {
-
 
3677
      strcpy (buffer, "type: ");
-
 
3678
      if (dir->entry->parent->entry->is_name)
-
 
3679
	rsrc_print_name (buffer + strlen (buffer),
-
 
3680
			 dir->entry->parent->entry->name_id.name);
-
 
3681
      else
-
 
3682
	{
-
 
3683
	  unsigned int id = dir->entry->parent->entry->name_id.id;
-
 
3684
 
-
 
3685
	  sprintf (buffer + strlen (buffer), "%x", id);
-
 
3686
	  switch (id)
-
 
3687
	    {
-
 
3688
	    case 1: strcat (buffer, " (CURSOR)"); break;
-
 
3689
	    case 2: strcat (buffer, " (BITMAP)"); break;
-
 
3690
	    case 3: strcat (buffer, " (ICON)"); break;
-
 
3691
            case 4: strcat (buffer, " (MENU)"); break;
-
 
3692
	    case 5: strcat (buffer, " (DIALOG)"); break;
-
 
3693
	    case 6: strcat (buffer, " (STRING)"); is_string = TRUE; break;
-
 
3694
	    case 7: strcat (buffer, " (FONTDIR)"); break;
-
 
3695
	    case 8: strcat (buffer, " (FONT)"); break;
-
 
3696
	    case 9: strcat (buffer, " (ACCELERATOR)"); break;
-
 
3697
	    case 10: strcat (buffer, " (RCDATA)"); break;
-
 
3698
	    case 11: strcat (buffer, " (MESSAGETABLE)"); break;
-
 
3699
	    case 12: strcat (buffer, " (GROUP_CURSOR)"); break;
-
 
3700
	    case 14: strcat (buffer, " (GROUP_ICON)"); break;
-
 
3701
	    case 16: strcat (buffer, " (VERSION)"); break;
-
 
3702
	    case 17: strcat (buffer, " (DLGINCLUDE)"); break;
-
 
3703
	    case 19: strcat (buffer, " (PLUGPLAY)"); break;
-
 
3704
	    case 20: strcat (buffer, " (VXD)"); break;
-
 
3705
	    case 21: strcat (buffer, " (ANICURSOR)"); break;
-
 
3706
	    case 22: strcat (buffer, " (ANIICON)"); break;
-
 
3707
	    case 23: strcat (buffer, " (HTML)"); break;
-
 
3708
	    case 24: strcat (buffer, " (MANIFEST)"); break;
-
 
3709
	    case 240: strcat (buffer, " (DLGINIT)"); break;
-
 
3710
	    case 241: strcat (buffer, " (TOOLBAR)"); break;
-
 
3711
	    }
-
 
3712
	}
-
 
3713
    }
-
 
3714
 
-
 
3715
  if (dir != NULL && dir->entry != NULL)
-
 
3716
    {
-
 
3717
      strcat (buffer, " name: ");
-
 
3718
      if (dir->entry->is_name)
-
 
3719
	rsrc_print_name (buffer + strlen (buffer), dir->entry->name_id.name);
-
 
3720
      else
-
 
3721
	{
-
 
3722
	  unsigned int id = dir->entry->name_id.id;
-
 
3723
 
-
 
3724
	  sprintf (buffer + strlen (buffer), "%x", id);
-
 
3725
 
-
 
3726
	  if (is_string)
-
 
3727
	    sprintf (buffer + strlen (buffer), " (resource id range: %d - %d)",
-
 
3728
		     (id - 1) << 4, (id << 4) - 1);
-
 
3729
	}
-
 
3730
    }
-
 
3731
 
-
 
3732
  if (entry != NULL)
-
 
3733
    {
-
 
3734
      strcat (buffer, " lang: ");
-
 
3735
 
-
 
3736
      if (entry->is_name)
-
 
3737
	rsrc_print_name (buffer + strlen (buffer), entry->name_id.name);
-
 
3738
      else
-
 
3739
	sprintf (buffer + strlen (buffer), "%x", entry->name_id.id);
-
 
3740
    }
-
 
3741
 
-
 
3742
  return buffer;
-
 
3743
}
-
 
3744
 
-
 
3745
/* *sigh* Windows resource strings are special.  Only the top 28-bits of
-
 
3746
   their ID is stored in the NAME entry.  The bottom four bits are used as
-
 
3747
   an index into unicode string table that makes up the data of the leaf.
-
 
3748
   So identical type-name-lang string resources may not actually be
-
 
3749
   identical at all.
-
 
3750
 
-
 
3751
   This function is called when we have detected two string resources with
-
 
3752
   match top-28-bit IDs.  We have to scan the string tables inside the leaves
-
 
3753
   and discover if there are any real collisions.  If there are then we report
-
 
3754
   them and return FALSE.  Otherwise we copy any strings from B into A and
-
 
3755
   then return TRUE.  */
-
 
3756
 
-
 
3757
static bfd_boolean
-
 
3758
rsrc_merge_string_entries (rsrc_entry * a ATTRIBUTE_UNUSED,
-
 
3759
			   rsrc_entry * b ATTRIBUTE_UNUSED)
-
 
3760
{
-
 
3761
  unsigned int copy_needed = 0;
-
 
3762
  unsigned int i;
-
 
3763
  bfd_byte * astring;
-
 
3764
  bfd_byte * bstring;
-
 
3765
  bfd_byte * new_data;
-
 
3766
  bfd_byte * nstring;
-
 
3767
 
-
 
3768
  /* Step one: Find out what we have to do.  */
-
 
3769
  BFD_ASSERT (! a->is_dir);
-
 
3770
  astring = a->value.leaf->data;
-
 
3771
 
-
 
3772
  BFD_ASSERT (! b->is_dir);
-
 
3773
  bstring = b->value.leaf->data;
-
 
3774
 
-
 
3775
  for (i = 0; i < 16; i++)
-
 
3776
    {
-
 
3777
      unsigned int alen = astring[0] + (astring[1] << 8);
-
 
3778
      unsigned int blen = bstring[0] + (bstring[1] << 8);
-
 
3779
 
-
 
3780
      if (alen == 0)
-
 
3781
	{
-
 
3782
	  copy_needed += blen * 2;
-
 
3783
	}
-
 
3784
      else if (blen == 0)
-
 
3785
	;
-
 
3786
      else if (alen != blen)
-
 
3787
	/* FIXME: Should we continue the loop in order to report other duplicates ?  */
-
 
3788
	break;
-
 
3789
      /* alen == blen != 0.  We might have two identical strings.  If so we
-
 
3790
	 can ignore the second one.  There is no need for wchar_t vs UTF-16
-
 
3791
	 theatrics here - we are only interested in (case sensitive) equality.  */
-
 
3792
      else if (memcmp (astring + 2, bstring + 2, alen * 2) != 0)
-
 
3793
	break;
-
 
3794
 
-
 
3795
      astring += (alen + 1) * 2;
-
 
3796
      bstring += (blen + 1) * 2;
-
 
3797
    }
-
 
3798
 
-
 
3799
  if (i != 16)
-
 
3800
    {
-
 
3801
      if (a->parent != NULL
-
 
3802
	  && a->parent->entry != NULL
-
 
3803
	  && a->parent->entry->is_name == FALSE)
-
 
3804
	_bfd_error_handler (_(".rsrc merge failure: duplicate string resource: %d"),
-
 
3805
			    ((a->parent->entry->name_id.id - 1) << 4) + i);
-
 
3806
      return FALSE;
-
 
3807
    }
-
 
3808
 
-
 
3809
  if (copy_needed == 0)
-
 
3810
    return TRUE;
-
 
3811
 
-
 
3812
  /* If we reach here then A and B must both have non-colliding strings.
-
 
3813
     (We never get string resources with fully empty string tables).
-
 
3814
     We need to allocate an extra COPY_NEEDED bytes in A and then bring
-
 
3815
     in B's strings.  */
-
 
3816
  new_data = bfd_malloc (a->value.leaf->size + copy_needed);
-
 
3817
  if (new_data == NULL)
-
 
3818
    return FALSE;
-
 
3819
 
-
 
3820
  nstring = new_data;
-
 
3821
  astring = a->value.leaf->data;
-
 
3822
  bstring = b->value.leaf->data;
-
 
3823
 
-
 
3824
  for (i = 0; i < 16; i++)
-
 
3825
    {
-
 
3826
      unsigned int alen = astring[0] + (astring[1] << 8);
-
 
3827
      unsigned int blen = bstring[0] + (bstring[1] << 8);
-
 
3828
 
-
 
3829
      if (alen != 0)
-
 
3830
	{
-
 
3831
	  memcpy (nstring, astring, (alen + 1) * 2);
-
 
3832
	  nstring += (alen + 1) * 2;
-
 
3833
	}
-
 
3834
      else if (blen != 0)
-
 
3835
	{
-
 
3836
	  memcpy (nstring, bstring, (blen + 1) * 2);
-
 
3837
	  nstring += (blen + 1) * 2;
-
 
3838
	}
-
 
3839
      else
-
 
3840
	{
-
 
3841
	  * nstring++ = 0;
-
 
3842
	  * nstring++ = 0;
-
 
3843
	}
-
 
3844
 
-
 
3845
      astring += (alen + 1) * 2;
-
 
3846
      bstring += (blen + 1) * 2;
-
 
3847
    }
-
 
3848
 
-
 
3849
  BFD_ASSERT (nstring - new_data == (signed) (a->value.leaf->size + copy_needed));
-
 
3850
 
-
 
3851
  free (a->value.leaf->data);
-
 
3852
  a->value.leaf->data = new_data;
-
 
3853
  a->value.leaf->size += copy_needed;
-
 
3854
 
-
 
3855
  return TRUE;
-
 
3856
}
-
 
3857
 
-
 
3858
static void rsrc_merge (rsrc_entry *, rsrc_entry *);
-
 
3859
 
-
 
3860
/* Sort the entries in given part of the directory.
-
 
3861
   We use an old fashioned bubble sort because we are dealing
-
 
3862
   with lists and we want to handle matches specially.  */
-
 
3863
 
-
 
3864
static void
-
 
3865
rsrc_sort_entries (rsrc_dir_chain *  chain,
-
 
3866
		   bfd_boolean       is_name,
-
 
3867
		   rsrc_directory *  dir)
-
 
3868
{
-
 
3869
  rsrc_entry * entry;
-
 
3870
  rsrc_entry * next;
-
 
3871
  rsrc_entry ** points_to_entry;
-
 
3872
  bfd_boolean swapped;
-
 
3873
 
-
 
3874
  if (chain->num_entries < 2)
-
 
3875
    return;
-
 
3876
 
-
 
3877
  do
-
 
3878
    {
-
 
3879
      swapped = FALSE;
-
 
3880
      points_to_entry = & chain->first_entry;
-
 
3881
      entry = * points_to_entry;
-
 
3882
      next  = entry->next_entry;
-
 
3883
 
-
 
3884
      do
-
 
3885
	{
-
 
3886
	  signed int cmp = rsrc_cmp (is_name, entry, next);
-
 
3887
 
-
 
3888
	  if (cmp > 0)
-
 
3889
	    {
-
 
3890
	      entry->next_entry = next->next_entry;
-
 
3891
	      next->next_entry = entry;
-
 
3892
	      * points_to_entry = next;
-
 
3893
	      points_to_entry = & next->next_entry;
-
 
3894
	      next = entry->next_entry;
-
 
3895
	      swapped = TRUE;
-
 
3896
	    }
-
 
3897
	  else if (cmp == 0)
-
 
3898
	    {
-
 
3899
	      if (entry->is_dir && next->is_dir)
-
 
3900
		{
-
 
3901
		  /* When we encounter identical directory entries we have to
-
 
3902
		     merge them together.  The exception to this rule is for
-
 
3903
		     resource manifests - there can only be one of these,
-
 
3904
		     even if they differ in language.  Zero-language manifests
-
 
3905
		     are assumed to be default manifests (provided by the
-
 
3906
		     Cygwin/MinGW build system) and these can be silently dropped,
-
 
3907
		     unless that would reduce the number of manifests to zero.
-
 
3908
		     There should only ever be one non-zero lang manifest -
-
 
3909
		     if there are more it is an error.  A non-zero lang
-
 
3910
		     manifest takes precedence over a default manifest.  */
-
 
3911
		  if (entry->is_name == FALSE
-
 
3912
		      && entry->name_id.id == 1
-
 
3913
		      && dir != NULL
-
 
3914
		      && dir->entry != NULL
-
 
3915
		      && dir->entry->is_name == FALSE
-
 
3916
		      && dir->entry->name_id.id == 0x18)
-
 
3917
		    {
-
 
3918
		      if (next->value.directory->names.num_entries == 0
-
 
3919
			  && next->value.directory->ids.num_entries == 1
-
 
3920
			  && next->value.directory->ids.first_entry->is_name == FALSE
-
 
3921
			  && next->value.directory->ids.first_entry->name_id.id == 0)
-
 
3922
			/* Fall through so that NEXT is dropped.  */
-
 
3923
			;
-
 
3924
		      else if (entry->value.directory->names.num_entries == 0
-
 
3925
			       && entry->value.directory->ids.num_entries == 1
-
 
3926
			       && entry->value.directory->ids.first_entry->is_name == FALSE
-
 
3927
			       && entry->value.directory->ids.first_entry->name_id.id == 0)
-
 
3928
			{
-
 
3929
			  /* Swap ENTRY and NEXT.  Then fall through so that the old ENTRY is dropped.  */
-
 
3930
			  entry->next_entry = next->next_entry;
-
 
3931
			  next->next_entry = entry;
-
 
3932
			  * points_to_entry = next;
-
 
3933
			  points_to_entry = & next->next_entry;
-
 
3934
			  next = entry->next_entry;
-
 
3935
			  swapped = TRUE;
-
 
3936
			}
-
 
3937
		      else
-
 
3938
			{
-
 
3939
			  _bfd_error_handler (_(".rsrc merge failure: multiple non-default manifests"));
-
 
3940
			  bfd_set_error (bfd_error_file_truncated);
-
 
3941
			  return;
-
 
3942
			}
-
 
3943
 
-
 
3944
		      /* Unhook NEXT from the chain.  */
-
 
3945
		      /* FIXME: memory loss here.  */
-
 
3946
		      entry->next_entry = next->next_entry;
-
 
3947
		      chain->num_entries --;
-
 
3948
		      if (chain->num_entries < 2)
-
 
3949
			return;
-
 
3950
		      next = next->next_entry;
-
 
3951
		    }
-
 
3952
		  else
-
 
3953
		    rsrc_merge (entry, next);
-
 
3954
		}
-
 
3955
	      else if (entry->is_dir != next->is_dir)
-
 
3956
		{
-
 
3957
		  _bfd_error_handler (_(".rsrc merge failure: a directory matches a leaf"));
-
 
3958
		  bfd_set_error (bfd_error_file_truncated);
-
 
3959
		  return;
-
 
3960
		}
-
 
3961
	      else
-
 
3962
		{
-
 
3963
		  /* Otherwise with identical leaves we issue an error
-
 
3964
		     message - because there should never be duplicates.
-
 
3965
		     The exception is Type 18/Name 1/Lang 0 which is the
-
 
3966
		     defaul manifest - this can just be dropped.  */
-
 
3967
		  if (entry->is_name == FALSE
-
 
3968
		      && entry->name_id.id == 0
-
 
3969
		      && dir != NULL
-
 
3970
		      && dir->entry != NULL
-
 
3971
		      && dir->entry->is_name == FALSE
-
 
3972
		      && dir->entry->name_id.id == 1
-
 
3973
		      && dir->entry->parent != NULL
-
 
3974
		      && dir->entry->parent->entry != NULL
-
 
3975
		      && dir->entry->parent->entry->is_name == FALSE
-
 
3976
		      && dir->entry->parent->entry->name_id.id == 0x18 /* RT_MANIFEST */)
-
 
3977
		    ;
-
 
3978
		  else if (dir != NULL
-
 
3979
			   && dir->entry != NULL
-
 
3980
			   && dir->entry->parent != NULL
-
 
3981
			   && dir->entry->parent->entry != NULL
-
 
3982
			   && dir->entry->parent->entry->is_name == FALSE
-
 
3983
			   && dir->entry->parent->entry->name_id.id == 0x6 /* RT_STRING */)
-
 
3984
		    {
-
 
3985
		      /* Strings need special handling.  */
-
 
3986
		      if (! rsrc_merge_string_entries (entry, next))
-
 
3987
			{
-
 
3988
			  /* _bfd_error_handler should have been called inside merge_strings.  */
-
 
3989
			  bfd_set_error (bfd_error_file_truncated);
-
 
3990
			  return;
-
 
3991
			}
-
 
3992
		    }
-
 
3993
		  else
-
 
3994
		    {
-
 
3995
		      if (dir == NULL
-
 
3996
			  || dir->entry == NULL
-
 
3997
			  || dir->entry->parent == NULL
-
 
3998
			  || dir->entry->parent->entry == NULL)
-
 
3999
			_bfd_error_handler (_(".rsrc merge failure: duplicate leaf"));
-
 
4000
		      else
-
 
4001
			_bfd_error_handler (_(".rsrc merge failure: duplicate leaf: %s"),
-
 
4002
					    rsrc_resource_name (entry, dir));
-
 
4003
		      bfd_set_error (bfd_error_file_truncated);
-
 
4004
		      return;
-
 
4005
		    }
-
 
4006
		}
-
 
4007
 
-
 
4008
	      /* Unhook NEXT from the chain.  */
-
 
4009
	      entry->next_entry = next->next_entry;
-
 
4010
	      chain->num_entries --;
-
 
4011
	      if (chain->num_entries < 2)
-
 
4012
		return;
-
 
4013
	      next = next->next_entry;
-
 
4014
	    }
-
 
4015
	  else
-
 
4016
	    {
-
 
4017
	      points_to_entry = & entry->next_entry;
-
 
4018
	      entry = next;
-
 
4019
	      next = next->next_entry;
-
 
4020
	    }
-
 
4021
	}
-
 
4022
      while (next);
-
 
4023
 
-
 
4024
      chain->last_entry = entry;
-
 
4025
    }
-
 
4026
  while (swapped);
-
 
4027
}
-
 
4028
 
-
 
4029
/* Attach B's chain onto A.  */
-
 
4030
static void
-
 
4031
rsrc_attach_chain (rsrc_dir_chain * achain, rsrc_dir_chain * bchain)
-
 
4032
{
-
 
4033
  if (bchain->num_entries == 0)
-
 
4034
    return;
-
 
4035
 
-
 
4036
  achain->num_entries += bchain->num_entries;
-
 
4037
 
-
 
4038
  if (achain->first_entry == NULL)
-
 
4039
    {
-
 
4040
      achain->first_entry = bchain->first_entry;
-
 
4041
      achain->last_entry  = bchain->last_entry;
-
 
4042
    }
-
 
4043
  else
-
 
4044
    {
-
 
4045
      achain->last_entry->next_entry = bchain->first_entry;
-
 
4046
      achain->last_entry = bchain->last_entry;
-
 
4047
    }
-
 
4048
 
-
 
4049
  bchain->num_entries = 0;
-
 
4050
  bchain->first_entry = bchain->last_entry = NULL;
-
 
4051
}
-
 
4052
 
-
 
4053
static void
-
 
4054
rsrc_merge (struct rsrc_entry * a, struct rsrc_entry * b)
-
 
4055
{
-
 
4056
  rsrc_directory * adir;
-
 
4057
  rsrc_directory * bdir;
-
 
4058
 
-
 
4059
  BFD_ASSERT (a->is_dir);
-
 
4060
  BFD_ASSERT (b->is_dir);
-
 
4061
 
-
 
4062
  adir = a->value.directory;
-
 
4063
  bdir = b->value.directory;
-
 
4064
 
-
 
4065
  if (adir->characteristics != bdir->characteristics)
-
 
4066
    {
-
 
4067
      _bfd_error_handler (_(".rsrc merge failure: dirs with differing characteristics\n"));
-
 
4068
      bfd_set_error (bfd_error_file_truncated);
-
 
4069
      return;
-
 
4070
    }
-
 
4071
 
-
 
4072
  if (adir->major != bdir->major || adir->minor != bdir->minor)
-
 
4073
    {
-
 
4074
      _bfd_error_handler (_(".rsrc merge failure: differing directory versions\n"));
-
 
4075
      bfd_set_error (bfd_error_file_truncated);
-
 
4076
      return;
-
 
4077
    }
-
 
4078
 
-
 
4079
  /* Attach B's name chain to A.  */
-
 
4080
  rsrc_attach_chain (& adir->names, & bdir->names);
-
 
4081
 
-
 
4082
  /* Attach B's ID chain to A.  */
-
 
4083
  rsrc_attach_chain (& adir->ids, & bdir->ids);
-
 
4084
 
-
 
4085
  /* Now sort A's entries.  */
-
 
4086
  rsrc_sort_entries (& adir->names, TRUE, adir);
-
 
4087
  rsrc_sort_entries (& adir->ids, FALSE, adir);
-
 
4088
}
-
 
4089
 
-
 
4090
/* Check the .rsrc section.  If it contains multiple concatenated
-
 
4091
   resources then we must merge them properly.  Otherwise Windows
-
 
4092
   will ignore all but the first set.  */
-
 
4093
 
-
 
4094
static void
-
 
4095
rsrc_process_section (bfd * abfd,
-
 
4096
		      struct coff_final_link_info * pfinfo)
-
 
4097
{
-
 
4098
  rsrc_directory    new_table;
-
 
4099
  bfd_size_type     size;
-
 
4100
  asection *        sec;
-
 
4101
  pe_data_type *    pe;
-
 
4102
  bfd_vma           rva_bias;
-
 
4103
  bfd_byte *        data;
-
 
4104
  bfd_byte *        datastart;
-
 
4105
  bfd_byte *        dataend;
-
 
4106
  bfd_byte *        new_data;
-
 
4107
  unsigned int      num_resource_sets;
-
 
4108
  rsrc_directory *  type_tables;
-
 
4109
  rsrc_write_data   write_data;
-
 
4110
  unsigned int      indx;
-
 
4111
  bfd *             input;
-
 
4112
  unsigned int      num_input_rsrc = 0;
-
 
4113
  unsigned int      max_num_input_rsrc = 4;
-
 
4114
  ptrdiff_t *       rsrc_sizes = NULL;
-
 
4115
 
-
 
4116
  new_table.names.num_entries = 0;
-
 
4117
  new_table.ids.num_entries = 0;
-
 
4118
 
-
 
4119
  sec = bfd_get_section_by_name (abfd, ".rsrc");
-
 
4120
  if (sec == NULL || (size = sec->rawsize) == 0)
-
 
4121
    return;
-
 
4122
 
-
 
4123
  pe = pe_data (abfd);
-
 
4124
  if (pe == NULL)
-
 
4125
    return;
-
 
4126
 
-
 
4127
  rva_bias = sec->vma - pe->pe_opthdr.ImageBase;
-
 
4128
 
-
 
4129
  data = bfd_malloc (size);
-
 
4130
  if (data == NULL)
-
 
4131
    return;
-
 
4132
 
-
 
4133
  datastart = data;
-
 
4134
 
-
 
4135
  if (! bfd_get_section_contents (abfd, sec, data, 0, size))
-
 
4136
    goto end;
-
 
4137
 
-
 
4138
  /* Step zero: Scan the input bfds looking for .rsrc sections and record
-
 
4139
     their lengths.  Note - we rely upon the fact that the linker script
-
 
4140
     does *not* sort the input .rsrc sections, so that the order in the
-
 
4141
     linkinfo list matches the order in the output .rsrc section.
-
 
4142
 
-
 
4143
     We need to know the lengths because each input .rsrc section has padding
-
 
4144
     at the end of a variable amount.  (It does not appear to be based upon
-
 
4145
     the section alignment or the file alignment).  We need to skip any
-
 
4146
     padding bytes when parsing the input .rsrc sections.  */
-
 
4147
  rsrc_sizes = bfd_malloc (max_num_input_rsrc * sizeof * rsrc_sizes);
-
 
4148
  if (rsrc_sizes == NULL)
-
 
4149
    goto end;
-
 
4150
 
-
 
4151
  for (input = pfinfo->info->input_bfds;
-
 
4152
       input != NULL;
-
 
4153
       input = input->link.next)
-
 
4154
    {
-
 
4155
      asection * rsrc_sec = bfd_get_section_by_name (input, ".rsrc");
-
 
4156
 
-
 
4157
      /* PR 18372 - skip discarded .rsrc sections.  */
-
 
4158
      if (rsrc_sec != NULL && !discarded_section (rsrc_sec))
-
 
4159
	{
-
 
4160
	  if (num_input_rsrc == max_num_input_rsrc)
-
 
4161
	    {
-
 
4162
	      max_num_input_rsrc += 10;
-
 
4163
	      rsrc_sizes = bfd_realloc (rsrc_sizes, max_num_input_rsrc
-
 
4164
					* sizeof * rsrc_sizes);
-
 
4165
	      if (rsrc_sizes == NULL)
-
 
4166
		goto end;
-
 
4167
	    }
-
 
4168
 
-
 
4169
	  BFD_ASSERT (rsrc_sec->size > 0);
-
 
4170
	  rsrc_sizes [num_input_rsrc ++] = rsrc_sec->size;
-
 
4171
	}
-
 
4172
    }
-
 
4173
 
-
 
4174
  if (num_input_rsrc < 2)
-
 
4175
    goto end;
-
 
4176
 
-
 
4177
  /* Step one: Walk the section, computing the size of the tables,
-
 
4178
     leaves and data and decide if we need to do anything.  */
-
 
4179
  dataend = data + size;
-
 
4180
  num_resource_sets = 0;
-
 
4181
 
-
 
4182
  while (data < dataend)
-
 
4183
    {
-
 
4184
      bfd_byte * p = data;
-
 
4185
 
-
 
4186
      data = rsrc_count_directory (abfd, data, data, dataend, rva_bias);
-
 
4187
 
-
 
4188
      if (data > dataend)
-
 
4189
	{
-
 
4190
	  /* Corrupted .rsrc section - cannot merge.  */
-
 
4191
	  _bfd_error_handler (_("%s: .rsrc merge failure: corrupt .rsrc section"),
-
 
4192
			      bfd_get_filename (abfd));
-
 
4193
	  bfd_set_error (bfd_error_file_truncated);
-
 
4194
	  goto end;
-
 
4195
	}
-
 
4196
 
-
 
4197
      if ((data - p) > rsrc_sizes [num_resource_sets])
-
 
4198
	{
-
 
4199
	  _bfd_error_handler (_("%s: .rsrc merge failure: unexpected .rsrc size"),
-
 
4200
			      bfd_get_filename (abfd));
-
 
4201
	  bfd_set_error (bfd_error_file_truncated);
-
 
4202
	  goto end;
-
 
4203
	}
-
 
4204
      /* FIXME: Should we add a check for "data - p" being much smaller
-
 
4205
	 than rsrc_sizes[num_resource_sets] ?  */
-
 
4206
 
-
 
4207
      data = p + rsrc_sizes[num_resource_sets];
-
 
4208
      rva_bias += data - p;
-
 
4209
      ++ num_resource_sets;
-
 
4210
    }
-
 
4211
  BFD_ASSERT (num_resource_sets == num_input_rsrc);
-
 
4212
 
-
 
4213
  /* Step two: Walk the data again, building trees of the resources.  */
-
 
4214
  data = datastart;
-
 
4215
  rva_bias = sec->vma - pe->pe_opthdr.ImageBase;
-
 
4216
 
-
 
4217
  type_tables = bfd_malloc (num_resource_sets * sizeof * type_tables);
-
 
4218
  if (type_tables == NULL)
-
 
4219
    goto end;
-
 
4220
 
-
 
4221
  indx = 0;
-
 
4222
  while (data < dataend)
-
 
4223
    {
-
 
4224
      bfd_byte * p = data;
-
 
4225
 
-
 
4226
      (void) rsrc_parse_directory (abfd, type_tables + indx, data, data,
-
 
4227
				   dataend, rva_bias, NULL);
-
 
4228
      data = p + rsrc_sizes[indx];
-
 
4229
      rva_bias += data - p;
-
 
4230
      ++ indx;
-
 
4231
    }
-
 
4232
  BFD_ASSERT (indx == num_resource_sets);
-
 
4233
 
-
 
4234
  /* Step three: Merge the top level tables (there can be only one).
-
 
4235
 
-
 
4236
     We must ensure that the merged entries are in ascending order.
-
 
4237
 
-
 
4238
     We also thread the top level table entries from the old tree onto
-
 
4239
     the new table, so that they can be pulled off later.  */
-
 
4240
 
-
 
4241
  /* FIXME: Should we verify that all type tables are the same ?  */
-
 
4242
  new_table.characteristics = type_tables[0].characteristics;
-
 
4243
  new_table.time            = type_tables[0].time;
-
 
4244
  new_table.major           = type_tables[0].major;
-
 
4245
  new_table.minor           = type_tables[0].minor;
-
 
4246
 
-
 
4247
  /* Chain the NAME entries onto the table.  */
-
 
4248
  new_table.names.first_entry = NULL;
-
 
4249
  new_table.names.last_entry = NULL;
-
 
4250
 
-
 
4251
  for (indx = 0; indx < num_resource_sets; indx++)
-
 
4252
    rsrc_attach_chain (& new_table.names, & type_tables[indx].names);
-
 
4253
 
-
 
4254
  rsrc_sort_entries (& new_table.names, TRUE, & new_table);
-
 
4255
 
-
 
4256
  /* Chain the ID entries onto the table.  */
-
 
4257
  new_table.ids.first_entry = NULL;
-
 
4258
  new_table.ids.last_entry = NULL;
-
 
4259
 
-
 
4260
  for (indx = 0; indx < num_resource_sets; indx++)
-
 
4261
    rsrc_attach_chain (& new_table.ids, & type_tables[indx].ids);
-
 
4262
 
-
 
4263
  rsrc_sort_entries (& new_table.ids, FALSE, & new_table);
-
 
4264
 
-
 
4265
  /* Step four: Create new contents for the .rsrc section.  */
-
 
4266
  /* Step four point one: Compute the size of each region of the .rsrc section.
-
 
4267
     We do this now, rather than earlier, as the merging above may have dropped
-
 
4268
     some entries.  */
-
 
4269
  sizeof_leaves = sizeof_strings = sizeof_tables_and_entries = 0;
-
 
4270
  rsrc_compute_region_sizes (& new_table);
-
 
4271
  /* We increment sizeof_strings to make sure that resource data
-
 
4272
     starts on an 8-byte boundary.  FIXME: Is this correct ?  */
-
 
4273
  sizeof_strings = (sizeof_strings + 7) & ~ 7;
-
 
4274
 
-
 
4275
  new_data = bfd_zalloc (abfd, size);
-
 
4276
  if (new_data == NULL)
-
 
4277
    goto end;
-
 
4278
 
-
 
4279
  write_data.abfd        = abfd;
-
 
4280
  write_data.datastart   = new_data;
-
 
4281
  write_data.next_table  = new_data;
-
 
4282
  write_data.next_leaf   = new_data + sizeof_tables_and_entries;
-
 
4283
  write_data.next_string = write_data.next_leaf + sizeof_leaves;
-
 
4284
  write_data.next_data   = write_data.next_string + sizeof_strings;
-
 
4285
  write_data.rva_bias    = sec->vma - pe->pe_opthdr.ImageBase;
-
 
4286
 
-
 
4287
  rsrc_write_directory (& write_data, & new_table);
-
 
4288
 
-
 
4289
  /* Step five: Replace the old contents with the new.
-
 
4290
     We recompute the size as we may have lost entries due to mergeing.  */
-
 
4291
  size = ((write_data.next_data - new_data) + 3) & ~ 3;
-
 
4292
 
-
 
4293
  {
-
 
4294
    int page_size;
-
 
4295
 
-
 
4296
    if (coff_data (abfd)->link_info)
-
 
4297
      {
-
 
4298
	page_size = pe_data (abfd)->pe_opthdr.FileAlignment;
-
 
4299
 
-
 
4300
	/* If no file alignment has been set, default to one.
-
 
4301
	   This repairs 'ld -r' for arm-wince-pe target.  */
-
 
4302
	if (page_size == 0)
-
 
4303
	  page_size = 1;
-
 
4304
      }
-
 
4305
    else
-
 
4306
      page_size = PE_DEF_FILE_ALIGNMENT;
-
 
4307
    size = (size + page_size - 1) & - page_size;
-
 
4308
  }
-
 
4309
 
-
 
4310
  bfd_set_section_contents (pfinfo->output_bfd, sec, new_data, 0, size);
-
 
4311
  sec->size = sec->rawsize = size;
-
 
4312
 
-
 
4313
 end:
-
 
4314
  /* Step six: Free all the memory that we have used.  */
-
 
4315
  /* FIXME: Free the resource tree, if we have one.  */
-
 
4316
  free (datastart);
-
 
4317
  free (rsrc_sizes);
-
 
4318
}
2272
 
4319
 
2273
/* Handle the .idata section and other things that need symbol table
4320
/* Handle the .idata section and other things that need symbol table
Line 2274... Line 4321...
2274
   access.  */
4321
   access.  */
2275
 
4322
 
Line 2473... Line 4520...
2473
		bfd_set_section_contents (pfinfo->output_bfd, sec,
4520
		bfd_set_section_contents (pfinfo->output_bfd, sec,
2474
					  tmp_data, 0, x);
4521
					  tmp_data, 0, x);
2475
	      }
4522
	      }
2476
	    free (tmp_data);
4523
	    free (tmp_data);
2477
	  }
4524
	  }
-
 
4525
	else
-
 
4526
	  result = FALSE;
2478
      }
4527
      }
2479
  }
4528
  }
2480
#endif
4529
#endif
Line -... Line 4530...
-
 
4530
 
-
 
4531
  rsrc_process_section (abfd, pfinfo);
2481
 
4532
 
2482
  /* If we couldn't find idata$2, we either have an excessively
4533
  /* If we couldn't find idata$2, we either have an excessively
2483
     trivial program or are in DEEP trouble; we have to assume trivial
4534
     trivial program or are in DEEP trouble; we have to assume trivial
2484
     program....  */
4535
     program....  */
2485
  return result;
4536
  return result;