+ fprintf (file, "Subsystem\t\t%08x", i->Subsystem);
+ if (subsystem_name)
+ fprintf (file, "\t(%s)", subsystem_name);
+ fprintf (file, "\nDllCharacteristics\t%08x\n", i->DllCharacteristics);
+ fprintf (file, "SizeOfStackReserve\t");
+ bfd_fprintf_vma (abfd, file, i->SizeOfStackReserve);
+ fprintf (file, "\nSizeOfStackCommit\t");
+ bfd_fprintf_vma (abfd, file, i->SizeOfStackCommit);
+ fprintf (file, "\nSizeOfHeapReserve\t");
+ bfd_fprintf_vma (abfd, file, i->SizeOfHeapReserve);
+ fprintf (file, "\nSizeOfHeapCommit\t");
+ bfd_fprintf_vma (abfd, file, i->SizeOfHeapCommit);
+ fprintf (file, "\nLoaderFlags\t\t%08lx\n", (unsigned long) i->LoaderFlags);
+ fprintf (file, "NumberOfRvaAndSizes\t%08lx\n",
+ (unsigned long) i->NumberOfRvaAndSizes);
+
+ fprintf (file, "\nThe Data Directory\n");
+ for (j = 0; j < IMAGE_NUMBEROF_DIRECTORY_ENTRIES; j++)
+ {
+ fprintf (file, "Entry %1x ", j);
+ bfd_fprintf_vma (abfd, file, i->DataDirectory[j].VirtualAddress);
+ fprintf (file, " %08lx ", (unsigned long) i->DataDirectory[j].Size);
+ fprintf (file, "%s\n", dir_names[j]);
+ }
+
+ pe_print_idata (abfd, vfile);
+ pe_print_edata (abfd, vfile);
+ if (bfd_coff_have_print_pdata (abfd))
+ bfd_coff_print_pdata (abfd, vfile);
+ else
+ pe_print_pdata (abfd, vfile);
+ pe_print_reloc (abfd, vfile);
+ pe_print_debugdata (abfd, file);
+
+ rsrc_print_section (abfd, vfile);
+
+ return TRUE;
+}
+
+static bfd_boolean
+is_vma_in_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sect, void *obj)
+{
+ bfd_vma addr = * (bfd_vma *) obj;
+ return (addr >= sect->vma) && (addr < (sect->vma + sect->size));
+}
+
+static asection *
+find_section_by_vma (bfd *abfd, bfd_vma addr)
+{
+ return bfd_sections_find_if (abfd, is_vma_in_section, (void *) & addr);
+}
+
+/* Copy any private info we understand from the input bfd
+ to the output bfd. */
+
+bfd_boolean
+_bfd_XX_bfd_copy_private_bfd_data_common (bfd * ibfd, bfd * obfd)
+{
+ pe_data_type *ipe, *ope;
+
+ /* One day we may try to grok other private data. */
+ if (ibfd->xvec->flavour != bfd_target_coff_flavour
+ || obfd->xvec->flavour != bfd_target_coff_flavour)
+ return TRUE;
+
+ ipe = pe_data (ibfd);
+ ope = pe_data (obfd);
+
+ /* pe_opthdr is copied in copy_object. */
+ ope->dll = ipe->dll;
+
+ /* Don't copy input subsystem if output is different from input. */
+ if (obfd->xvec != ibfd->xvec)
+ ope->pe_opthdr.Subsystem = IMAGE_SUBSYSTEM_UNKNOWN;
+
+ /* For strip: if we removed .reloc, we'll make a real mess of things
+ if we don't remove this entry as well. */
+ if (! pe_data (obfd)->has_reloc_section)
+ {
+ pe_data (obfd)->pe_opthdr.DataDirectory[PE_BASE_RELOCATION_TABLE].VirtualAddress = 0;
+ pe_data (obfd)->pe_opthdr.DataDirectory[PE_BASE_RELOCATION_TABLE].Size = 0;
+ }
+
+ /* For PIE, if there is .reloc, we won't add IMAGE_FILE_RELOCS_STRIPPED.
+ But there is no .reloc, we make sure that IMAGE_FILE_RELOCS_STRIPPED
+ won't be added. */
+ if (! pe_data (ibfd)->has_reloc_section
+ && ! (pe_data (ibfd)->real_flags & IMAGE_FILE_RELOCS_STRIPPED))
+ pe_data (obfd)->dont_strip_reloc = 1;
+
+ memcpy (ope->dos_message, ipe->dos_message, sizeof (ope->dos_message));
+
+ /* The file offsets contained in the debug directory need rewriting. */
+ if (ope->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size != 0)
+ {
+ bfd_vma addr = ope->pe_opthdr.DataDirectory[PE_DEBUG_DATA].VirtualAddress
+ + ope->pe_opthdr.ImageBase;
+ asection *section = find_section_by_vma (obfd, addr);
+ bfd_byte *data;
+
+ if (section && bfd_malloc_and_get_section (obfd, section, &data))
+ {
+ unsigned int i;
+ struct external_IMAGE_DEBUG_DIRECTORY *dd =
+ (struct external_IMAGE_DEBUG_DIRECTORY *)(data + (addr - section->vma));
+
+ /* PR 17512: file: 0f15796a. */
+ if ((unsigned long) ope->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size
+ > section->size - (addr - section->vma))
+ {
+ /* xgettext:c-format */
+ _bfd_error_handler
+ (_("%pB: Data Directory size (%lx) "
+ "exceeds space left in section (%" PRIx64 ")"),
+ obfd, ope->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size,
+ (uint64_t) (section->size - (addr - section->vma)));
+ free (data);
+ return FALSE;
+ }
+
+ for (i = 0; i < ope->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size
+ / sizeof (struct external_IMAGE_DEBUG_DIRECTORY); i++)
+ {
+ asection *ddsection;
+ struct external_IMAGE_DEBUG_DIRECTORY *edd = &(dd[i]);
+ struct internal_IMAGE_DEBUG_DIRECTORY idd;
+
+ _bfd_XXi_swap_debugdir_in (obfd, edd, &idd);
+
+ if (idd.AddressOfRawData == 0)
+ continue; /* RVA 0 means only offset is valid, not handled yet. */
+
+ ddsection = find_section_by_vma (obfd, idd.AddressOfRawData + ope->pe_opthdr.ImageBase);
+ if (!ddsection)
+ continue; /* Not in a section! */
+
+ idd.PointerToRawData = ddsection->filepos + (idd.AddressOfRawData
+ + ope->pe_opthdr.ImageBase) - ddsection->vma;
+
+ _bfd_XXi_swap_debugdir_out (obfd, &idd, edd);
+ }
+
+ if (!bfd_set_section_contents (obfd, section, data, 0, section->size))
+ {
+ _bfd_error_handler (_("failed to update file offsets in debug directory"));
+ free (data);
+ return FALSE;
+ }
+ free (data);
+ }
+ else if (section)
+ {
+ _bfd_error_handler (_("%pB: failed to read debug data section"), obfd);
+ return FALSE;
+ }
+ }
+
+ return TRUE;
+}
+
+/* Copy private section data. */
+
+bfd_boolean
+_bfd_XX_bfd_copy_private_section_data (bfd *ibfd,
+ asection *isec,
+ bfd *obfd,
+ asection *osec)
+{
+ if (bfd_get_flavour (ibfd) != bfd_target_coff_flavour
+ || bfd_get_flavour (obfd) != bfd_target_coff_flavour)
+ return TRUE;
+
+ if (coff_section_data (ibfd, isec) != NULL
+ && pei_section_data (ibfd, isec) != NULL)
+ {
+ if (coff_section_data (obfd, osec) == NULL)
+ {
+ size_t amt = sizeof (struct coff_section_tdata);
+ osec->used_by_bfd = bfd_zalloc (obfd, amt);
+ if (osec->used_by_bfd == NULL)
+ return FALSE;
+ }
+
+ if (pei_section_data (obfd, osec) == NULL)
+ {
+ size_t amt = sizeof (struct pei_section_tdata);
+ coff_section_data (obfd, osec)->tdata = bfd_zalloc (obfd, amt);
+ if (coff_section_data (obfd, osec)->tdata == NULL)
+ return FALSE;
+ }
+
+ pei_section_data (obfd, osec)->virt_size =
+ pei_section_data (ibfd, isec)->virt_size;
+ pei_section_data (obfd, osec)->pe_flags =
+ pei_section_data (ibfd, isec)->pe_flags;
+ }
+
+ return TRUE;
+}
+
+void
+_bfd_XX_get_symbol_info (bfd * abfd, asymbol *symbol, symbol_info *ret)
+{
+ coff_get_symbol_info (abfd, symbol, ret);
+}
+
+#if !defined(COFF_WITH_pep) && defined(COFF_WITH_pex64)
+static int
+sort_x64_pdata (const void *l, const void *r)
+{
+ const char *lp = (const char *) l;
+ const char *rp = (const char *) r;
+ bfd_vma vl, vr;
+ vl = bfd_getl32 (lp); vr = bfd_getl32 (rp);
+ if (vl != vr)
+ return (vl < vr ? -1 : 1);
+ /* We compare just begin address. */
+ return 0;
+}
+#endif
+\f
+/* Functions to process a .rsrc section. */
+
+static unsigned int sizeof_leaves;
+static unsigned int sizeof_strings;
+static unsigned int sizeof_tables_and_entries;
+
+static bfd_byte *
+rsrc_count_directory (bfd *, bfd_byte *, bfd_byte *, bfd_byte *, bfd_vma);
+
+static bfd_byte *
+rsrc_count_entries (bfd * abfd,
+ bfd_boolean is_name,
+ bfd_byte * datastart,
+ bfd_byte * data,
+ bfd_byte * dataend,
+ bfd_vma rva_bias)
+{
+ unsigned long entry, addr, size;
+
+ if (data + 8 >= dataend)
+ return dataend + 1;
+
+ if (is_name)
+ {
+ bfd_byte * name;
+
+ entry = (long) bfd_get_32 (abfd, data);
+
+ if (HighBitSet (entry))
+ name = datastart + WithoutHighBit (entry);
+ else
+ name = datastart + entry - rva_bias;
+
+ if (name + 2 >= dataend || name < datastart)
+ return dataend + 1;
+
+ unsigned int len = bfd_get_16 (abfd, name);
+ if (len == 0 || len > 256)
+ return dataend + 1;
+ }
+
+ entry = (long) bfd_get_32 (abfd, data + 4);
+
+ if (HighBitSet (entry))
+ {
+ data = datastart + WithoutHighBit (entry);
+
+ if (data <= datastart || data >= dataend)
+ return dataend + 1;
+
+ return rsrc_count_directory (abfd, datastart, data, dataend, rva_bias);
+ }
+
+ if (datastart + entry + 16 >= dataend)
+ return dataend + 1;
+
+ addr = (long) bfd_get_32 (abfd, datastart + entry);
+ size = (long) bfd_get_32 (abfd, datastart + entry + 4);
+
+ return datastart + addr - rva_bias + size;
+}
+
+static bfd_byte *
+rsrc_count_directory (bfd * abfd,
+ bfd_byte * datastart,
+ bfd_byte * data,
+ bfd_byte * dataend,
+ bfd_vma rva_bias)
+{
+ unsigned int num_entries, num_ids;
+ bfd_byte * highest_data = data;
+
+ if (data + 16 >= dataend)
+ return dataend + 1;
+
+ num_entries = (int) bfd_get_16 (abfd, data + 12);
+ num_ids = (int) bfd_get_16 (abfd, data + 14);
+
+ num_entries += num_ids;
+
+ data += 16;
+
+ while (num_entries --)
+ {
+ bfd_byte * entry_end;
+
+ entry_end = rsrc_count_entries (abfd, num_entries >= num_ids,
+ datastart, data, dataend, rva_bias);
+ data += 8;
+ highest_data = max (highest_data, entry_end);
+ if (entry_end >= dataend)
+ break;
+ }
+
+ return max (highest_data, data);
+}
+
+typedef struct rsrc_dir_chain
+{
+ unsigned int num_entries;
+ struct rsrc_entry * first_entry;
+ struct rsrc_entry * last_entry;
+} rsrc_dir_chain;
+
+typedef struct rsrc_directory
+{
+ unsigned int characteristics;
+ unsigned int time;
+ unsigned int major;
+ unsigned int minor;
+
+ rsrc_dir_chain names;
+ rsrc_dir_chain ids;
+
+ struct rsrc_entry * entry;
+} rsrc_directory;
+
+typedef struct rsrc_string
+{
+ unsigned int len;
+ bfd_byte * string;
+} rsrc_string;
+
+typedef struct rsrc_leaf
+{
+ unsigned int size;
+ unsigned int codepage;
+ bfd_byte * data;
+} rsrc_leaf;
+
+typedef struct rsrc_entry
+{
+ bfd_boolean is_name;
+ union
+ {
+ unsigned int id;
+ struct rsrc_string name;
+ } name_id;
+
+ bfd_boolean is_dir;
+ union
+ {
+ struct rsrc_directory * directory;
+ struct rsrc_leaf * leaf;
+ } value;
+
+ struct rsrc_entry * next_entry;
+ struct rsrc_directory * parent;
+} rsrc_entry;
+
+static bfd_byte *
+rsrc_parse_directory (bfd *, rsrc_directory *, bfd_byte *,
+ bfd_byte *, bfd_byte *, bfd_vma, rsrc_entry *);
+
+static bfd_byte *
+rsrc_parse_entry (bfd * abfd,
+ bfd_boolean is_name,
+ rsrc_entry * entry,
+ bfd_byte * datastart,
+ bfd_byte * data,
+ bfd_byte * dataend,
+ bfd_vma rva_bias,
+ rsrc_directory * parent)
+{
+ unsigned long val, addr, size;
+
+ val = bfd_get_32 (abfd, data);
+
+ entry->parent = parent;
+ entry->is_name = is_name;
+
+ if (is_name)
+ {
+ bfd_byte * address;
+
+ if (HighBitSet (val))
+ {
+ val = WithoutHighBit (val);
+
+ address = datastart + val;
+ }
+ else
+ {
+ address = datastart + val - rva_bias;
+ }
+
+ if (address + 3 > dataend)
+ return dataend;
+
+ entry->name_id.name.len = bfd_get_16 (abfd, address);
+ entry->name_id.name.string = address + 2;
+ }
+ else
+ entry->name_id.id = val;
+
+ val = bfd_get_32 (abfd, data + 4);
+
+ if (HighBitSet (val))
+ {
+ entry->is_dir = TRUE;
+ entry->value.directory = bfd_malloc (sizeof * entry->value.directory);
+ if (entry->value.directory == NULL)
+ return dataend;
+
+ return rsrc_parse_directory (abfd, entry->value.directory,
+ datastart,
+ datastart + WithoutHighBit (val),
+ dataend, rva_bias, entry);
+ }
+
+ entry->is_dir = FALSE;
+ entry->value.leaf = bfd_malloc (sizeof * entry->value.leaf);
+ if (entry->value.leaf == NULL)
+ return dataend;
+
+ data = datastart + val;
+ if (data < datastart || data >= dataend)
+ return dataend;
+
+ addr = bfd_get_32 (abfd, data);
+ size = entry->value.leaf->size = bfd_get_32 (abfd, data + 4);
+ entry->value.leaf->codepage = bfd_get_32 (abfd, data + 8);
+ /* FIXME: We assume that the reserved field (data + 12) is OK. */
+
+ entry->value.leaf->data = bfd_malloc (size);
+ if (entry->value.leaf->data == NULL)
+ return dataend;
+
+ memcpy (entry->value.leaf->data, datastart + addr - rva_bias, size);
+ return datastart + (addr - rva_bias) + size;
+}
+
+static bfd_byte *
+rsrc_parse_entries (bfd * abfd,
+ rsrc_dir_chain * chain,
+ bfd_boolean is_name,
+ bfd_byte * highest_data,
+ bfd_byte * datastart,
+ bfd_byte * data,
+ bfd_byte * dataend,
+ bfd_vma rva_bias,
+ rsrc_directory * parent)
+{
+ unsigned int i;
+ rsrc_entry * entry;
+
+ if (chain->num_entries == 0)
+ {
+ chain->first_entry = chain->last_entry = NULL;
+ return highest_data;
+ }
+
+ entry = bfd_malloc (sizeof * entry);
+ if (entry == NULL)
+ return dataend;
+
+ chain->first_entry = entry;
+
+ for (i = chain->num_entries; i--;)
+ {
+ bfd_byte * entry_end;
+
+ entry_end = rsrc_parse_entry (abfd, is_name, entry, datastart,
+ data, dataend, rva_bias, parent);
+ data += 8;
+ highest_data = max (entry_end, highest_data);
+ if (entry_end > dataend)
+ return dataend;
+
+ if (i)
+ {
+ entry->next_entry = bfd_malloc (sizeof * entry);
+ entry = entry->next_entry;
+ if (entry == NULL)
+ return dataend;
+ }
+ else
+ entry->next_entry = NULL;
+ }
+
+ chain->last_entry = entry;
+
+ return highest_data;
+}
+
+static bfd_byte *
+rsrc_parse_directory (bfd * abfd,
+ rsrc_directory * table,
+ bfd_byte * datastart,
+ bfd_byte * data,
+ bfd_byte * dataend,
+ bfd_vma rva_bias,
+ rsrc_entry * entry)
+{
+ bfd_byte * highest_data = data;
+
+ if (table == NULL)
+ return dataend;
+
+ table->characteristics = bfd_get_32 (abfd, data);
+ table->time = bfd_get_32 (abfd, data + 4);
+ table->major = bfd_get_16 (abfd, data + 8);
+ table->minor = bfd_get_16 (abfd, data + 10);
+ table->names.num_entries = bfd_get_16 (abfd, data + 12);
+ table->ids.num_entries = bfd_get_16 (abfd, data + 14);
+ table->entry = entry;
+
+ data += 16;
+
+ highest_data = rsrc_parse_entries (abfd, & table->names, TRUE, data,
+ datastart, data, dataend, rva_bias, table);
+ data += table->names.num_entries * 8;
+
+ highest_data = rsrc_parse_entries (abfd, & table->ids, FALSE, highest_data,
+ datastart, data, dataend, rva_bias, table);
+ data += table->ids.num_entries * 8;
+
+ return max (highest_data, data);
+}
+
+typedef struct rsrc_write_data
+{
+ bfd * abfd;
+ bfd_byte * datastart;
+ bfd_byte * next_table;
+ bfd_byte * next_leaf;
+ bfd_byte * next_string;
+ bfd_byte * next_data;
+ bfd_vma rva_bias;
+} rsrc_write_data;
+
+static void
+rsrc_write_string (rsrc_write_data * data,
+ rsrc_string * string)
+{
+ bfd_put_16 (data->abfd, string->len, data->next_string);
+ memcpy (data->next_string + 2, string->string, string->len * 2);
+ data->next_string += (string->len + 1) * 2;
+}
+
+static inline unsigned int
+rsrc_compute_rva (rsrc_write_data * data,
+ bfd_byte * addr)
+{
+ return (addr - data->datastart) + data->rva_bias;
+}
+
+static void
+rsrc_write_leaf (rsrc_write_data * data,
+ rsrc_leaf * leaf)
+{
+ bfd_put_32 (data->abfd, rsrc_compute_rva (data, data->next_data),
+ data->next_leaf);
+ bfd_put_32 (data->abfd, leaf->size, data->next_leaf + 4);
+ bfd_put_32 (data->abfd, leaf->codepage, data->next_leaf + 8);
+ bfd_put_32 (data->abfd, 0 /*reserved*/, data->next_leaf + 12);
+ data->next_leaf += 16;
+
+ memcpy (data->next_data, leaf->data, leaf->size);
+ /* An undocumented feature of Windows resources is that each unit
+ of raw data is 8-byte aligned... */
+ data->next_data += ((leaf->size + 7) & ~7);
+}
+
+static void rsrc_write_directory (rsrc_write_data *, rsrc_directory *);
+
+static void
+rsrc_write_entry (rsrc_write_data * data,
+ bfd_byte * where,
+ rsrc_entry * entry)
+{
+ if (entry->is_name)
+ {
+ bfd_put_32 (data->abfd,
+ SetHighBit (data->next_string - data->datastart),
+ where);
+ rsrc_write_string (data, & entry->name_id.name);
+ }
+ else
+ bfd_put_32 (data->abfd, entry->name_id.id, where);
+
+ if (entry->is_dir)
+ {
+ bfd_put_32 (data->abfd,
+ SetHighBit (data->next_table - data->datastart),
+ where + 4);
+ rsrc_write_directory (data, entry->value.directory);
+ }
+ else
+ {
+ bfd_put_32 (data->abfd, data->next_leaf - data->datastart, where + 4);
+ rsrc_write_leaf (data, entry->value.leaf);
+ }
+}
+
+static void
+rsrc_compute_region_sizes (rsrc_directory * dir)
+{
+ struct rsrc_entry * entry;
+
+ if (dir == NULL)
+ return;
+
+ sizeof_tables_and_entries += 16;
+
+ for (entry = dir->names.first_entry; entry != NULL; entry = entry->next_entry)
+ {
+ sizeof_tables_and_entries += 8;
+
+ sizeof_strings += (entry->name_id.name.len + 1) * 2;
+
+ if (entry->is_dir)
+ rsrc_compute_region_sizes (entry->value.directory);
+ else
+ sizeof_leaves += 16;
+ }
+
+ for (entry = dir->ids.first_entry; entry != NULL; entry = entry->next_entry)
+ {
+ sizeof_tables_and_entries += 8;
+
+ if (entry->is_dir)
+ rsrc_compute_region_sizes (entry->value.directory);
+ else
+ sizeof_leaves += 16;
+ }
+}
+
+static void
+rsrc_write_directory (rsrc_write_data * data,
+ rsrc_directory * dir)
+{
+ rsrc_entry * entry;
+ unsigned int i;
+ bfd_byte * next_entry;
+ bfd_byte * nt;
+
+ bfd_put_32 (data->abfd, dir->characteristics, data->next_table);
+ bfd_put_32 (data->abfd, 0 /*dir->time*/, data->next_table + 4);
+ bfd_put_16 (data->abfd, dir->major, data->next_table + 8);
+ bfd_put_16 (data->abfd, dir->minor, data->next_table + 10);
+ bfd_put_16 (data->abfd, dir->names.num_entries, data->next_table + 12);
+ bfd_put_16 (data->abfd, dir->ids.num_entries, data->next_table + 14);
+
+ /* Compute where the entries and the next table will be placed. */
+ next_entry = data->next_table + 16;
+ data->next_table = next_entry + (dir->names.num_entries * 8)
+ + (dir->ids.num_entries * 8);
+ nt = data->next_table;
+
+ /* Write the entries. */
+ for (i = dir->names.num_entries, entry = dir->names.first_entry;
+ i > 0 && entry != NULL;
+ i--, entry = entry->next_entry)
+ {
+ BFD_ASSERT (entry->is_name);
+ rsrc_write_entry (data, next_entry, entry);
+ next_entry += 8;
+ }
+ BFD_ASSERT (i == 0);
+ BFD_ASSERT (entry == NULL);
+
+ for (i = dir->ids.num_entries, entry = dir->ids.first_entry;
+ i > 0 && entry != NULL;
+ i--, entry = entry->next_entry)
+ {
+ BFD_ASSERT (! entry->is_name);
+ rsrc_write_entry (data, next_entry, entry);
+ next_entry += 8;
+ }
+ BFD_ASSERT (i == 0);
+ BFD_ASSERT (entry == NULL);
+ BFD_ASSERT (nt == next_entry);
+}
+
+#if defined HAVE_WCHAR_H && ! defined __CYGWIN__ && ! defined __MINGW32__
+/* Return the length (number of units) of the first character in S,
+ putting its 'ucs4_t' representation in *PUC. */
+
+static unsigned int
+#if defined HAVE_WCTYPE_H
+u16_mbtouc (wint_t * puc, const unsigned short * s, unsigned int n)
+#else
+u16_mbtouc (wchar_t * puc, const unsigned short * s, unsigned int n)
+#endif
+{
+ unsigned short c = * s;
+
+ if (c < 0xd800 || c >= 0xe000)
+ {
+ *puc = c;
+ return 1;
+ }
+
+ if (c < 0xdc00)
+ {
+ if (n >= 2)
+ {
+ if (s[1] >= 0xdc00 && s[1] < 0xe000)
+ {
+ *puc = 0x10000 + ((c - 0xd800) << 10) + (s[1] - 0xdc00);
+ return 2;
+ }
+ }
+ else
+ {
+ /* Incomplete multibyte character. */
+ *puc = 0xfffd;
+ return n;
+ }
+ }
+
+ /* Invalid multibyte character. */
+ *puc = 0xfffd;
+ return 1;
+}
+#endif /* HAVE_WCHAR_H and not Cygwin/Mingw */
+
+/* Perform a comparison of two entries. */
+static signed int
+rsrc_cmp (bfd_boolean is_name, rsrc_entry * a, rsrc_entry * b)
+{
+ signed int res;
+ bfd_byte * astring;
+ unsigned int alen;
+ bfd_byte * bstring;
+ unsigned int blen;
+
+ if (! is_name)
+ return a->name_id.id - b->name_id.id;
+
+ /* We have to perform a case insenstive, unicode string comparison... */
+ astring = a->name_id.name.string;
+ alen = a->name_id.name.len;
+ bstring = b->name_id.name.string;
+ blen = b->name_id.name.len;
+
+#if defined __CYGWIN__ || defined __MINGW32__
+ /* Under Windows hosts (both Cygwin and Mingw types),
+ unicode == UTF-16 == wchar_t. The case insensitive string comparison
+ function however goes by different names in the two environments... */
+
+#undef rscpcmp
+#ifdef __CYGWIN__
+#define rscpcmp wcsncasecmp
+#endif
+#ifdef __MINGW32__
+#define rscpcmp wcsnicmp
+#endif
+
+ res = rscpcmp ((const wchar_t *) astring, (const wchar_t *) bstring,
+ min (alen, blen));
+
+#elif defined HAVE_WCHAR_H
+ {
+ unsigned int i;
+
+ res = 0;
+ for (i = min (alen, blen); i--; astring += 2, bstring += 2)
+ {
+#if defined HAVE_WCTYPE_H
+ wint_t awc;
+ wint_t bwc;
+#else
+ wchar_t awc;
+ wchar_t bwc;
+#endif
+
+ /* Convert UTF-16 unicode characters into wchar_t characters
+ so that we can then perform a case insensitive comparison. */
+ unsigned int Alen = u16_mbtouc (& awc, (const unsigned short *) astring, 2);
+ unsigned int Blen = u16_mbtouc (& bwc, (const unsigned short *) bstring, 2);
+
+ if (Alen != Blen)
+ return Alen - Blen;