Home | History | Annotate | Line # | Download | only in ld
      1      1.1  christos /* Support for generating PDB CodeView debugging files.
      2  1.1.1.2  christos    Copyright (C) 2022-2025 Free Software Foundation, Inc.
      3      1.1  christos 
      4      1.1  christos    This file is part of the GNU Binutils.
      5      1.1  christos 
      6      1.1  christos    This program is free software; you can redistribute it and/or modify
      7      1.1  christos    it under the terms of the GNU General Public License as published by
      8      1.1  christos    the Free Software Foundation; either version 3 of the License, or
      9      1.1  christos    (at your option) any later version.
     10      1.1  christos 
     11      1.1  christos    This program is distributed in the hope that it will be useful,
     12      1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     13      1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14      1.1  christos    GNU General Public License for more details.
     15      1.1  christos 
     16      1.1  christos    You should have received a copy of the GNU General Public License
     17      1.1  christos    along with this program; if not, write to the Free Software
     18      1.1  christos    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     19      1.1  christos    MA 02110-1301, USA.  */
     20      1.1  christos 
     21      1.1  christos #include "pdb.h"
     22      1.1  christos #include "bfdlink.h"
     23      1.1  christos #include "ld.h"
     24      1.1  christos #include "ldmain.h"
     25      1.1  christos #include "ldmisc.h"
     26      1.1  christos #include "libbfd.h"
     27      1.1  christos #include "libiberty.h"
     28      1.1  christos #include "coff/i386.h"
     29      1.1  christos #include "coff/external.h"
     30      1.1  christos #include "coff/internal.h"
     31      1.1  christos #include "coff/pe.h"
     32      1.1  christos #include "libcoff.h"
     33      1.1  christos #include <time.h>
     34      1.1  christos 
     35      1.1  christos struct public
     36      1.1  christos {
     37      1.1  christos   struct public *next;
     38      1.1  christos   uint32_t offset;
     39      1.1  christos   uint32_t hash;
     40      1.1  christos   unsigned int index;
     41      1.1  christos   uint16_t section;
     42      1.1  christos   uint32_t address;
     43      1.1  christos };
     44      1.1  christos 
     45      1.1  christos struct string
     46      1.1  christos {
     47      1.1  christos   struct string *next;
     48      1.1  christos   uint32_t hash;
     49      1.1  christos   uint32_t offset;
     50      1.1  christos   uint32_t source_file_offset;
     51      1.1  christos   size_t len;
     52      1.1  christos   char s[];
     53      1.1  christos };
     54      1.1  christos 
     55      1.1  christos struct string_table
     56      1.1  christos {
     57      1.1  christos   struct string *strings_head;
     58      1.1  christos   struct string *strings_tail;
     59      1.1  christos   uint32_t strings_len;
     60      1.1  christos   htab_t hashmap;
     61      1.1  christos };
     62      1.1  christos 
     63      1.1  christos struct mod_source_files
     64      1.1  christos {
     65      1.1  christos   uint16_t files_count;
     66      1.1  christos   struct string **files;
     67      1.1  christos };
     68      1.1  christos 
     69      1.1  christos struct source_files_info
     70      1.1  christos {
     71      1.1  christos   uint16_t mod_count;
     72      1.1  christos   struct mod_source_files *mods;
     73      1.1  christos };
     74      1.1  christos 
     75      1.1  christos struct type_entry
     76      1.1  christos {
     77      1.1  christos   struct type_entry *next;
     78      1.1  christos   uint32_t index;
     79      1.1  christos   uint32_t cv_hash;
     80      1.1  christos   bool has_udt_src_line;
     81      1.1  christos   uint8_t data[];
     82      1.1  christos };
     83      1.1  christos 
     84      1.1  christos struct types
     85      1.1  christos {
     86      1.1  christos   htab_t hashmap;
     87      1.1  christos   uint32_t num_types;
     88      1.1  christos   struct type_entry *first;
     89      1.1  christos   struct type_entry *last;
     90      1.1  christos };
     91      1.1  christos 
     92      1.1  christos struct global
     93      1.1  christos {
     94      1.1  christos   struct global *next;
     95      1.1  christos   uint32_t offset;
     96      1.1  christos   uint32_t hash;
     97      1.1  christos   uint32_t refcount;
     98      1.1  christos   unsigned int index;
     99      1.1  christos   uint8_t data[];
    100      1.1  christos };
    101      1.1  christos 
    102      1.1  christos struct globals
    103      1.1  christos {
    104      1.1  christos   uint32_t num_entries;
    105      1.1  christos   struct global *first;
    106      1.1  christos   struct global *last;
    107      1.1  christos   htab_t hashmap;
    108      1.1  christos };
    109      1.1  christos 
    110      1.1  christos struct in_sc
    111      1.1  christos {
    112      1.1  christos   asection *s;
    113      1.1  christos   uint16_t sect_num;
    114      1.1  christos   uint16_t mod_index;
    115      1.1  christos };
    116      1.1  christos 
    117      1.1  christos static const uint32_t crc_table[] =
    118      1.1  christos {
    119      1.1  christos   0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
    120      1.1  christos   0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
    121      1.1  christos   0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
    122      1.1  christos   0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
    123      1.1  christos   0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
    124      1.1  christos   0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
    125      1.1  christos   0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
    126      1.1  christos   0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
    127      1.1  christos   0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
    128      1.1  christos   0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
    129      1.1  christos   0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
    130      1.1  christos   0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
    131      1.1  christos   0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
    132      1.1  christos   0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
    133      1.1  christos   0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
    134      1.1  christos   0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
    135      1.1  christos   0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
    136      1.1  christos   0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
    137      1.1  christos   0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
    138      1.1  christos   0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
    139      1.1  christos   0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
    140      1.1  christos   0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
    141      1.1  christos   0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
    142      1.1  christos   0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
    143      1.1  christos   0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
    144      1.1  christos   0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
    145      1.1  christos   0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
    146      1.1  christos   0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
    147      1.1  christos   0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
    148      1.1  christos   0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
    149      1.1  christos   0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
    150      1.1  christos   0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
    151      1.1  christos   0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
    152      1.1  christos   0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
    153      1.1  christos   0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
    154      1.1  christos   0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
    155      1.1  christos   0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
    156      1.1  christos   0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
    157      1.1  christos   0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
    158      1.1  christos   0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
    159      1.1  christos   0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
    160      1.1  christos   0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
    161      1.1  christos   0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
    162      1.1  christos };
    163      1.1  christos 
    164  1.1.1.2  christos static bool remap_type (void *data, struct type_entry **map,
    165  1.1.1.2  christos 			uint32_t type_num, uint32_t num_types);
    166  1.1.1.2  christos 
    167      1.1  christos /* Add a new stream to the PDB archive, and return its BFD.  */
    168      1.1  christos static bfd *
    169      1.1  christos add_stream (bfd *pdb, const char *name, uint16_t *stream_num)
    170      1.1  christos {
    171      1.1  christos   bfd *stream;
    172      1.1  christos   uint16_t num;
    173      1.1  christos 
    174      1.1  christos   stream = bfd_create (name ? name : "", pdb);
    175      1.1  christos   if (!stream)
    176      1.1  christos     return NULL;
    177      1.1  christos 
    178      1.1  christos   if (!bfd_make_writable (stream))
    179      1.1  christos     {
    180      1.1  christos       bfd_close (stream);
    181  1.1.1.2  christos       return NULL;
    182      1.1  christos     }
    183      1.1  christos 
    184      1.1  christos   if (!pdb->archive_head)
    185      1.1  christos     {
    186      1.1  christos       bfd_set_archive_head (pdb, stream);
    187      1.1  christos       num = 0;
    188      1.1  christos     }
    189      1.1  christos   else
    190      1.1  christos     {
    191      1.1  christos       bfd *b = pdb->archive_head;
    192      1.1  christos 
    193      1.1  christos       num = 1;
    194      1.1  christos 
    195      1.1  christos       while (b->archive_next)
    196      1.1  christos 	{
    197      1.1  christos 	  num++;
    198      1.1  christos 	  b = b->archive_next;
    199      1.1  christos 	}
    200      1.1  christos 
    201      1.1  christos       b->archive_next = stream;
    202      1.1  christos     }
    203      1.1  christos 
    204      1.1  christos   if (stream_num)
    205      1.1  christos     *stream_num = num;
    206      1.1  christos 
    207      1.1  christos   return stream;
    208      1.1  christos }
    209      1.1  christos 
    210      1.1  christos /* Stream 0 ought to be a copy of the MSF directory from the last
    211      1.1  christos    time the PDB file was written.  Because we don't do incremental
    212      1.1  christos    writes this isn't applicable to us, but we fill it with a dummy
    213      1.1  christos    value so as not to confuse radare.  */
    214      1.1  christos static bool
    215      1.1  christos create_old_directory_stream (bfd *pdb)
    216      1.1  christos {
    217      1.1  christos   bfd *stream;
    218      1.1  christos   char buf[sizeof (uint32_t)];
    219      1.1  christos 
    220      1.1  christos   stream = add_stream (pdb, NULL, NULL);
    221      1.1  christos   if (!stream)
    222      1.1  christos     return false;
    223      1.1  christos 
    224      1.1  christos   bfd_putl32 (0, buf);
    225      1.1  christos 
    226      1.1  christos   return bfd_write (buf, sizeof (uint32_t), stream) == sizeof (uint32_t);
    227      1.1  christos }
    228      1.1  christos 
    229      1.1  christos /* Calculate the hash of a given string.  */
    230      1.1  christos static uint32_t
    231      1.1  christos calc_hash (const char *data, size_t len)
    232      1.1  christos {
    233      1.1  christos   uint32_t hash = 0;
    234      1.1  christos 
    235      1.1  christos   while (len >= 4)
    236      1.1  christos     {
    237      1.1  christos       hash ^= data[0];
    238      1.1  christos       hash ^= data[1] << 8;
    239      1.1  christos       hash ^= data[2] << 16;
    240      1.1  christos       hash ^= data[3] << 24;
    241      1.1  christos 
    242      1.1  christos       data += 4;
    243      1.1  christos       len -= 4;
    244      1.1  christos     }
    245      1.1  christos 
    246      1.1  christos   if (len >= 2)
    247      1.1  christos     {
    248      1.1  christos       hash ^= data[0];
    249      1.1  christos       hash ^= data[1] << 8;
    250      1.1  christos 
    251      1.1  christos       data += 2;
    252      1.1  christos       len -= 2;
    253      1.1  christos     }
    254      1.1  christos 
    255      1.1  christos   if (len != 0)
    256      1.1  christos     hash ^= *data;
    257      1.1  christos 
    258      1.1  christos   hash |= 0x20202020;
    259      1.1  christos   hash ^= (hash >> 11);
    260      1.1  christos 
    261      1.1  christos   return hash ^ (hash >> 16);
    262      1.1  christos }
    263      1.1  christos 
    264      1.1  christos /* Stream 1 is the PDB info stream - see
    265      1.1  christos    https://llvm.org/docs/PDB/PdbStream.html.  */
    266      1.1  christos static bool
    267      1.1  christos populate_info_stream (bfd *pdb, bfd *info_stream, const unsigned char *guid)
    268      1.1  christos {
    269      1.1  christos   bool ret = false;
    270      1.1  christos   struct pdb_stream_70 h;
    271      1.1  christos   uint32_t num_entries, num_buckets;
    272      1.1  christos   uint32_t names_length, stream_num;
    273      1.1  christos   char int_buf[sizeof (uint32_t)];
    274      1.1  christos 
    275      1.1  christos   struct hash_entry
    276      1.1  christos   {
    277      1.1  christos     uint32_t offset;
    278      1.1  christos     uint32_t value;
    279      1.1  christos   };
    280      1.1  christos 
    281      1.1  christos   struct hash_entry **buckets = NULL;
    282      1.1  christos 
    283      1.1  christos   /* Write header.  */
    284      1.1  christos 
    285      1.1  christos   bfd_putl32 (PDB_STREAM_VERSION_VC70, &h.version);
    286      1.1  christos   bfd_putl32 (time (NULL), &h.signature);
    287      1.1  christos   bfd_putl32 (1, &h.age);
    288      1.1  christos 
    289      1.1  christos   bfd_putl32 (bfd_getb32 (guid), h.guid);
    290      1.1  christos   bfd_putl16 (bfd_getb16 (&guid[4]), &h.guid[4]);
    291      1.1  christos   bfd_putl16 (bfd_getb16 (&guid[6]), &h.guid[6]);
    292      1.1  christos   memcpy (&h.guid[8], &guid[8], 8);
    293      1.1  christos 
    294      1.1  christos   if (bfd_write (&h, sizeof (h), info_stream) != sizeof (h))
    295      1.1  christos     return false;
    296      1.1  christos 
    297      1.1  christos   /* Write hash list of named streams.  This is a "rollover" hash, i.e.
    298      1.1  christos      if a bucket is filled an entry gets placed in the next free
    299      1.1  christos      slot.  */
    300      1.1  christos 
    301      1.1  christos   num_entries = 0;
    302      1.1  christos   for (bfd *b = pdb->archive_head; b; b = b->archive_next)
    303      1.1  christos     {
    304      1.1  christos       if (strcmp (b->filename, ""))
    305      1.1  christos 	num_entries++;
    306      1.1  christos     }
    307      1.1  christos 
    308      1.1  christos   num_buckets = num_entries * 2;
    309      1.1  christos 
    310      1.1  christos   names_length = 0;
    311      1.1  christos   stream_num = 0;
    312      1.1  christos 
    313      1.1  christos   if (num_buckets > 0)
    314      1.1  christos     {
    315      1.1  christos       buckets = xmalloc (sizeof (struct hash_entry *) * num_buckets);
    316      1.1  christos       memset (buckets, 0, sizeof (struct hash_entry *) * num_buckets);
    317      1.1  christos 
    318      1.1  christos       for (bfd *b = pdb->archive_head; b; b = b->archive_next)
    319      1.1  christos 	{
    320      1.1  christos 	  if (strcmp (b->filename, ""))
    321      1.1  christos 	    {
    322      1.1  christos 	      size_t len = strlen (b->filename);
    323      1.1  christos 	      uint32_t hash = (uint16_t) calc_hash (b->filename, len);
    324      1.1  christos 	      uint32_t bucket_num = hash % num_buckets;
    325      1.1  christos 
    326      1.1  christos 	      while (buckets[bucket_num])
    327      1.1  christos 		{
    328      1.1  christos 		  bucket_num++;
    329      1.1  christos 
    330      1.1  christos 		  if (bucket_num == num_buckets)
    331      1.1  christos 		    bucket_num = 0;
    332      1.1  christos 		}
    333      1.1  christos 
    334      1.1  christos 	      buckets[bucket_num] = xmalloc (sizeof (struct hash_entry));
    335      1.1  christos 
    336      1.1  christos 	      buckets[bucket_num]->offset = names_length;
    337      1.1  christos 	      buckets[bucket_num]->value = stream_num;
    338      1.1  christos 
    339      1.1  christos 	      names_length += len + 1;
    340      1.1  christos 	    }
    341      1.1  christos 
    342      1.1  christos 	  stream_num++;
    343      1.1  christos 	}
    344      1.1  christos     }
    345      1.1  christos 
    346      1.1  christos   /* Write the strings list - the hash keys are indexes into this.  */
    347      1.1  christos 
    348      1.1  christos   bfd_putl32 (names_length, int_buf);
    349      1.1  christos 
    350      1.1  christos   if (bfd_write (int_buf, sizeof (uint32_t), info_stream) !=
    351      1.1  christos       sizeof (uint32_t))
    352      1.1  christos     goto end;
    353      1.1  christos 
    354      1.1  christos   for (bfd *b = pdb->archive_head; b; b = b->archive_next)
    355      1.1  christos     {
    356      1.1  christos       if (!strcmp (b->filename, ""))
    357      1.1  christos 	continue;
    358      1.1  christos 
    359      1.1  christos       size_t len = strlen (b->filename) + 1;
    360      1.1  christos 
    361      1.1  christos       if (bfd_write (b->filename, len, info_stream) != len)
    362      1.1  christos 	goto end;
    363      1.1  christos     }
    364      1.1  christos 
    365      1.1  christos   /* Write the number of entries and buckets.  */
    366      1.1  christos 
    367      1.1  christos   bfd_putl32 (num_entries, int_buf);
    368      1.1  christos 
    369      1.1  christos   if (bfd_write (int_buf, sizeof (uint32_t), info_stream) !=
    370      1.1  christos       sizeof (uint32_t))
    371      1.1  christos     goto end;
    372      1.1  christos 
    373      1.1  christos   bfd_putl32 (num_buckets, int_buf);
    374      1.1  christos 
    375      1.1  christos   if (bfd_write (int_buf, sizeof (uint32_t), info_stream) !=
    376      1.1  christos       sizeof (uint32_t))
    377      1.1  christos     goto end;
    378      1.1  christos 
    379      1.1  christos   /* Write the present bitmap.  */
    380      1.1  christos 
    381      1.1  christos   bfd_putl32 ((num_buckets + 31) / 32, int_buf);
    382      1.1  christos 
    383      1.1  christos   if (bfd_write (int_buf, sizeof (uint32_t), info_stream) !=
    384      1.1  christos       sizeof (uint32_t))
    385      1.1  christos     goto end;
    386      1.1  christos 
    387      1.1  christos   for (unsigned int i = 0; i < num_buckets; i += 32)
    388      1.1  christos     {
    389      1.1  christos       uint32_t v = 0;
    390      1.1  christos 
    391      1.1  christos       for (unsigned int j = 0; j < 32; j++)
    392      1.1  christos 	{
    393      1.1  christos 	  if (i + j >= num_buckets)
    394      1.1  christos 	    break;
    395      1.1  christos 
    396      1.1  christos 	  if (buckets[i + j])
    397      1.1  christos 	    v |= 1 << j;
    398      1.1  christos 	}
    399      1.1  christos 
    400      1.1  christos       bfd_putl32 (v, int_buf);
    401      1.1  christos 
    402      1.1  christos       if (bfd_write (int_buf, sizeof (uint32_t), info_stream) !=
    403      1.1  christos 	  sizeof (uint32_t))
    404      1.1  christos 	goto end;
    405      1.1  christos     }
    406      1.1  christos 
    407      1.1  christos   /* Write the (empty) deleted bitmap.  */
    408      1.1  christos 
    409      1.1  christos   bfd_putl32 (0, int_buf);
    410      1.1  christos 
    411      1.1  christos   if (bfd_write (int_buf, sizeof (uint32_t), info_stream) !=
    412      1.1  christos       sizeof (uint32_t))
    413      1.1  christos     goto end;
    414      1.1  christos 
    415      1.1  christos   /* Write the buckets.  */
    416      1.1  christos 
    417      1.1  christos   for (unsigned int i = 0; i < num_buckets; i++)
    418      1.1  christos     {
    419      1.1  christos       if (buckets[i])
    420      1.1  christos 	{
    421      1.1  christos 	  bfd_putl32 (buckets[i]->offset, int_buf);
    422      1.1  christos 
    423      1.1  christos 	  if (bfd_write (int_buf, sizeof (uint32_t), info_stream) !=
    424      1.1  christos 	      sizeof (uint32_t))
    425      1.1  christos 	    goto end;
    426      1.1  christos 
    427      1.1  christos 	  bfd_putl32 (buckets[i]->value, int_buf);
    428      1.1  christos 
    429      1.1  christos 	  if (bfd_write (int_buf, sizeof (uint32_t), info_stream) !=
    430      1.1  christos 	      sizeof (uint32_t))
    431      1.1  christos 	    goto end;
    432      1.1  christos 	}
    433      1.1  christos     }
    434      1.1  christos 
    435      1.1  christos   bfd_putl32 (0, int_buf);
    436      1.1  christos 
    437      1.1  christos   if (bfd_write (int_buf, sizeof (uint32_t), info_stream) !=
    438      1.1  christos       sizeof (uint32_t))
    439      1.1  christos     goto end;
    440      1.1  christos 
    441      1.1  christos   bfd_putl32 (PDB_STREAM_VERSION_VC140, int_buf);
    442      1.1  christos 
    443      1.1  christos   if (bfd_write (int_buf, sizeof (uint32_t), info_stream) !=
    444      1.1  christos       sizeof (uint32_t))
    445      1.1  christos     goto end;
    446      1.1  christos 
    447      1.1  christos   ret = true;
    448      1.1  christos 
    449      1.1  christos end:
    450      1.1  christos   for (unsigned int i = 0; i < num_buckets; i++)
    451      1.1  christos     {
    452      1.1  christos       if (buckets[i])
    453      1.1  christos 	free (buckets[i]);
    454      1.1  christos     }
    455      1.1  christos 
    456      1.1  christos   free (buckets);
    457      1.1  christos 
    458      1.1  christos   return ret;
    459      1.1  christos }
    460      1.1  christos 
    461      1.1  christos /* Calculate the CRC32 used for type hashes.  */
    462      1.1  christos static uint32_t
    463      1.1  christos crc32 (const uint8_t *data, size_t len)
    464      1.1  christos {
    465      1.1  christos   uint32_t crc = 0;
    466      1.1  christos 
    467      1.1  christos   while (len > 0)
    468      1.1  christos     {
    469      1.1  christos       crc = (crc >> 8) ^ crc_table[(crc & 0xff) ^ *data];
    470      1.1  christos 
    471      1.1  christos       data++;
    472      1.1  christos       len--;
    473      1.1  christos     }
    474      1.1  christos 
    475      1.1  christos   return crc;
    476      1.1  christos }
    477      1.1  christos 
    478      1.1  christos /* Stream 2 is the type information (TPI) stream, and stream 4 is
    479      1.1  christos    the ID information (IPI) stream.  They differ only in which records
    480      1.1  christos    go in which stream. */
    481      1.1  christos static bool
    482      1.1  christos populate_type_stream (bfd *pdb, bfd *stream, struct types *types)
    483      1.1  christos {
    484      1.1  christos   struct pdb_tpi_stream_header h;
    485      1.1  christos   struct type_entry *e;
    486      1.1  christos   uint32_t len = 0, index_offset_len, off;
    487      1.1  christos   struct bfd *hash_stream = NULL;
    488      1.1  christos   uint16_t hash_stream_index;
    489      1.1  christos 
    490      1.1  christos   static const uint32_t index_skip = 0x2000;
    491      1.1  christos 
    492      1.1  christos   e = types->first;
    493      1.1  christos 
    494      1.1  christos   index_offset_len = 0;
    495      1.1  christos 
    496      1.1  christos   while (e)
    497      1.1  christos     {
    498      1.1  christos       uint32_t old_len = len;
    499      1.1  christos 
    500      1.1  christos       len += sizeof (uint16_t) + bfd_getl16 (e->data);
    501      1.1  christos 
    502      1.1  christos       if (old_len == 0 || old_len / index_skip != len / index_skip)
    503      1.1  christos 	index_offset_len += sizeof (uint32_t) * 2;
    504      1.1  christos 
    505      1.1  christos       e = e->next;
    506      1.1  christos     }
    507      1.1  christos 
    508      1.1  christos   /* Each type stream also has a stream which holds the hash value for each
    509      1.1  christos      type, along with a skip list to speed up searching.  */
    510      1.1  christos 
    511      1.1  christos   hash_stream = add_stream (pdb, "", &hash_stream_index);
    512      1.1  christos 
    513      1.1  christos   if (!hash_stream)
    514      1.1  christos     return false;
    515      1.1  christos 
    516      1.1  christos   bfd_putl32 (TPI_STREAM_VERSION_80, &h.version);
    517      1.1  christos   bfd_putl32 (sizeof (h), &h.header_size);
    518      1.1  christos   bfd_putl32 (TPI_FIRST_INDEX, &h.type_index_begin);
    519      1.1  christos   bfd_putl32 (TPI_FIRST_INDEX + types->num_types, &h.type_index_end);
    520      1.1  christos   bfd_putl32 (len, &h.type_record_bytes);
    521      1.1  christos   bfd_putl16 (hash_stream_index, &h.hash_stream_index);
    522      1.1  christos   bfd_putl16 (0xffff, &h.hash_aux_stream_index);
    523      1.1  christos   bfd_putl32 (sizeof (uint32_t), &h.hash_key_size);
    524      1.1  christos   bfd_putl32 (NUM_TPI_HASH_BUCKETS, &h.num_hash_buckets);
    525      1.1  christos   bfd_putl32 (0, &h.hash_value_buffer_offset);
    526      1.1  christos   bfd_putl32 (types->num_types * sizeof (uint32_t),
    527      1.1  christos 	      &h.hash_value_buffer_length);
    528      1.1  christos   bfd_putl32 (types->num_types * sizeof (uint32_t),
    529      1.1  christos 	      &h.index_offset_buffer_offset);
    530      1.1  christos   bfd_putl32 (index_offset_len, &h.index_offset_buffer_length);
    531      1.1  christos   bfd_putl32 ((types->num_types * sizeof (uint32_t)) + index_offset_len,
    532      1.1  christos 	      &h.hash_adj_buffer_offset);
    533      1.1  christos   bfd_putl32 (0, &h.hash_adj_buffer_length);
    534      1.1  christos 
    535      1.1  christos   if (bfd_write (&h, sizeof (h), stream) != sizeof (h))
    536      1.1  christos     return false;
    537      1.1  christos 
    538      1.1  christos   /* Write the type definitions into the main stream, and the hashes
    539      1.1  christos      into the hash stream.  The hashes have already been calculated
    540      1.1  christos      in handle_type.  */
    541      1.1  christos 
    542      1.1  christos   e = types->first;
    543      1.1  christos 
    544      1.1  christos   while (e)
    545      1.1  christos     {
    546      1.1  christos       uint8_t buf[sizeof (uint32_t)];
    547      1.1  christos       uint16_t size;
    548      1.1  christos 
    549      1.1  christos       size = bfd_getl16 (e->data);
    550      1.1  christos 
    551      1.1  christos       if (bfd_write (e->data, size + sizeof (uint16_t), stream)
    552      1.1  christos 	  != size + sizeof (uint16_t))
    553      1.1  christos 	return false;
    554      1.1  christos 
    555      1.1  christos       bfd_putl32 (e->cv_hash % NUM_TPI_HASH_BUCKETS, buf);
    556      1.1  christos 
    557      1.1  christos       if (bfd_write (buf, sizeof (uint32_t), hash_stream)
    558      1.1  christos 	  != sizeof (uint32_t))
    559      1.1  christos 	return false;
    560      1.1  christos 
    561      1.1  christos       e = e->next;
    562      1.1  christos     }
    563      1.1  christos 
    564      1.1  christos   /* Write the index offsets, i.e. the skip list, into the hash stream.  We
    565      1.1  christos      copy MSVC here by writing a new entry for every 8192 bytes.  */
    566      1.1  christos 
    567      1.1  christos   e = types->first;
    568      1.1  christos   off = 0;
    569      1.1  christos 
    570      1.1  christos   while (e)
    571      1.1  christos     {
    572      1.1  christos       uint32_t old_off = off;
    573      1.1  christos       uint16_t size = bfd_getl16 (e->data);
    574      1.1  christos 
    575      1.1  christos       off += size + sizeof (uint16_t);
    576      1.1  christos 
    577      1.1  christos       if (old_off == 0 || old_off / index_skip != len / index_skip)
    578      1.1  christos 	{
    579      1.1  christos 	  uint8_t buf[sizeof (uint32_t)];
    580      1.1  christos 
    581      1.1  christos 	  bfd_putl32 (TPI_FIRST_INDEX + e->index, buf);
    582      1.1  christos 
    583      1.1  christos 	  if (bfd_write (buf, sizeof (uint32_t), hash_stream)
    584      1.1  christos 	      != sizeof (uint32_t))
    585      1.1  christos 	    return false;
    586      1.1  christos 
    587      1.1  christos 	  bfd_putl32 (old_off, buf);
    588      1.1  christos 
    589      1.1  christos 	  if (bfd_write (buf, sizeof (uint32_t), hash_stream)
    590      1.1  christos 	      != sizeof (uint32_t))
    591      1.1  christos 	    return false;
    592      1.1  christos 	}
    593      1.1  christos 
    594      1.1  christos       e = e->next;
    595      1.1  christos     }
    596      1.1  christos 
    597      1.1  christos   return true;
    598      1.1  christos }
    599      1.1  christos 
    600      1.1  christos /* Return the PE architecture number for the image.  */
    601      1.1  christos static uint16_t
    602      1.1  christos get_arch_number (bfd *abfd)
    603      1.1  christos {
    604      1.1  christos   switch (abfd->arch_info->arch)
    605      1.1  christos     {
    606      1.1  christos     case bfd_arch_i386:
    607      1.1  christos       if (abfd->arch_info->mach & bfd_mach_x86_64)
    608      1.1  christos 	return IMAGE_FILE_MACHINE_AMD64;
    609      1.1  christos       else
    610      1.1  christos 	return IMAGE_FILE_MACHINE_I386;
    611      1.1  christos 
    612      1.1  christos     case bfd_arch_aarch64:
    613      1.1  christos       return IMAGE_FILE_MACHINE_ARM64;
    614      1.1  christos 
    615      1.1  christos     default:
    616      1.1  christos       return 0;
    617      1.1  christos     }
    618      1.1  christos }
    619      1.1  christos 
    620      1.1  christos /* Validate the DEBUG_S_FILECHKSMS entry within a module's .debug$S
    621      1.1  christos    section, and copy it to the module's symbol stream.  */
    622      1.1  christos static bool
    623      1.1  christos copy_filechksms (uint8_t *data, uint32_t size, char *string_table,
    624      1.1  christos 		 struct string_table *strings, uint8_t *out,
    625      1.1  christos 		 struct mod_source_files *mod_source)
    626      1.1  christos {
    627      1.1  christos   uint8_t *orig_data = data;
    628      1.1  christos   uint32_t orig_size = size;
    629      1.1  christos   uint16_t num_files = 0;
    630      1.1  christos   struct string **strptr;
    631      1.1  christos 
    632      1.1  christos   bfd_putl32 (DEBUG_S_FILECHKSMS, out);
    633      1.1  christos   out += sizeof (uint32_t);
    634      1.1  christos 
    635      1.1  christos   bfd_putl32 (size, out);
    636      1.1  christos   out += sizeof (uint32_t);
    637      1.1  christos 
    638      1.1  christos   /* Calculate the number of files, and check for any overflows.  */
    639      1.1  christos 
    640      1.1  christos   while (size > 0)
    641      1.1  christos     {
    642      1.1  christos       struct file_checksum *fc = (struct file_checksum *) data;
    643      1.1  christos       uint8_t padding;
    644      1.1  christos       size_t len;
    645      1.1  christos 
    646      1.1  christos       if (size < sizeof (struct file_checksum))
    647      1.1  christos 	{
    648      1.1  christos 	  bfd_set_error (bfd_error_bad_value);
    649      1.1  christos 	  return false;
    650      1.1  christos 	}
    651      1.1  christos 
    652      1.1  christos       len = sizeof (struct file_checksum) + fc->checksum_length;
    653      1.1  christos 
    654      1.1  christos       if (size < len)
    655      1.1  christos 	{
    656      1.1  christos 	  bfd_set_error (bfd_error_bad_value);
    657      1.1  christos 	  return false;
    658      1.1  christos 	}
    659      1.1  christos 
    660      1.1  christos       data += len;
    661      1.1  christos       size -= len;
    662      1.1  christos 
    663      1.1  christos       if (len % sizeof (uint32_t))
    664      1.1  christos 	padding = sizeof (uint32_t) - (len % sizeof (uint32_t));
    665      1.1  christos       else
    666      1.1  christos 	padding = 0;
    667      1.1  christos 
    668      1.1  christos       if (size < padding)
    669      1.1  christos 	{
    670      1.1  christos 	  bfd_set_error (bfd_error_bad_value);
    671      1.1  christos 	  return false;
    672      1.1  christos 	}
    673      1.1  christos 
    674      1.1  christos       num_files++;
    675      1.1  christos 
    676      1.1  christos       data += padding;
    677      1.1  christos       size -= padding;
    678      1.1  christos     }
    679      1.1  christos 
    680      1.1  christos   /* Add the files to mod_source, so that they'll appear in the source
    681      1.1  christos      info substream.  */
    682      1.1  christos 
    683      1.1  christos   strptr = NULL;
    684      1.1  christos   if (num_files > 0)
    685      1.1  christos     {
    686      1.1  christos       uint16_t new_count = num_files + mod_source->files_count;
    687      1.1  christos 
    688      1.1  christos       mod_source->files = xrealloc (mod_source->files,
    689      1.1  christos 				    sizeof (struct string *) * new_count);
    690      1.1  christos 
    691      1.1  christos       strptr = mod_source->files + mod_source->files_count;
    692      1.1  christos 
    693      1.1  christos       mod_source->files_count += num_files;
    694      1.1  christos     }
    695      1.1  christos 
    696      1.1  christos   /* Actually copy the data.  */
    697      1.1  christos 
    698      1.1  christos   data = orig_data;
    699      1.1  christos   size = orig_size;
    700      1.1  christos 
    701      1.1  christos   while (size > 0)
    702      1.1  christos     {
    703      1.1  christos       struct file_checksum *fc = (struct file_checksum *) data;
    704      1.1  christos       uint32_t string_off;
    705      1.1  christos       uint8_t padding;
    706      1.1  christos       size_t len;
    707      1.1  christos       struct string *str = NULL;
    708      1.1  christos 
    709      1.1  christos       string_off = bfd_getl32 (&fc->file_id);
    710      1.1  christos       len = sizeof (struct file_checksum) + fc->checksum_length;
    711      1.1  christos 
    712      1.1  christos       if (len % sizeof (uint32_t))
    713      1.1  christos 	padding = sizeof (uint32_t) - (len % sizeof (uint32_t));
    714      1.1  christos       else
    715      1.1  christos 	padding = 0;
    716      1.1  christos 
    717      1.1  christos       /* Remap the "file ID", i.e. the offset in the module's string table,
    718      1.1  christos 	 so it points to the right place in the main string table.  */
    719      1.1  christos 
    720      1.1  christos       if (string_table)
    721      1.1  christos 	{
    722      1.1  christos 	  char *fn = string_table + string_off;
    723      1.1  christos 	  size_t fn_len = strlen (fn);
    724      1.1  christos 	  uint32_t hash = calc_hash (fn, fn_len);
    725      1.1  christos 	  void **slot;
    726      1.1  christos 
    727      1.1  christos 	  slot = htab_find_slot_with_hash (strings->hashmap, fn, hash,
    728      1.1  christos 					   NO_INSERT);
    729      1.1  christos 
    730      1.1  christos 	  if (slot)
    731      1.1  christos 	    str = (struct string *) *slot;
    732      1.1  christos 	}
    733      1.1  christos 
    734      1.1  christos       *strptr = str;
    735      1.1  christos       strptr++;
    736      1.1  christos 
    737      1.1  christos       bfd_putl32 (str ? str->offset : 0, &fc->file_id);
    738      1.1  christos 
    739      1.1  christos       memcpy (out, data, len + padding);
    740      1.1  christos 
    741      1.1  christos       data += len + padding;
    742      1.1  christos       size -= len + padding;
    743      1.1  christos       out += len + padding;
    744      1.1  christos     }
    745      1.1  christos 
    746      1.1  christos   return true;
    747      1.1  christos }
    748      1.1  christos 
    749      1.1  christos /* Add a string to the strings table, if it's not already there.  Returns its
    750      1.1  christos    offset within the string table.  */
    751      1.1  christos static uint32_t
    752      1.1  christos add_string (char *str, size_t len, struct string_table *strings)
    753      1.1  christos {
    754      1.1  christos   uint32_t hash = calc_hash (str, len);
    755      1.1  christos   struct string *s;
    756      1.1  christos   void **slot;
    757      1.1  christos 
    758      1.1  christos   slot = htab_find_slot_with_hash (strings->hashmap, str, hash, INSERT);
    759      1.1  christos 
    760      1.1  christos   if (!*slot)
    761      1.1  christos     {
    762      1.1  christos       *slot = xmalloc (offsetof (struct string, s) + len);
    763      1.1  christos 
    764      1.1  christos       s = (struct string *) *slot;
    765      1.1  christos 
    766      1.1  christos       s->next = NULL;
    767      1.1  christos       s->hash = hash;
    768      1.1  christos       s->offset = strings->strings_len;
    769      1.1  christos       s->source_file_offset = 0xffffffff;
    770      1.1  christos       s->len = len;
    771      1.1  christos       memcpy (s->s, str, len);
    772      1.1  christos 
    773      1.1  christos       if (strings->strings_tail)
    774      1.1  christos 	strings->strings_tail->next = s;
    775      1.1  christos       else
    776      1.1  christos 	strings->strings_head = s;
    777      1.1  christos 
    778      1.1  christos       strings->strings_tail = s;
    779      1.1  christos 
    780      1.1  christos       strings->strings_len += len + 1;
    781      1.1  christos     }
    782      1.1  christos   else
    783      1.1  christos     {
    784      1.1  christos       s = (struct string *) *slot;
    785      1.1  christos     }
    786      1.1  christos 
    787      1.1  christos   return s->offset;
    788      1.1  christos }
    789      1.1  christos 
    790      1.1  christos /* Return the hash of an entry in the string table.  */
    791      1.1  christos static hashval_t
    792      1.1  christos hash_string_table_entry (const void *p)
    793      1.1  christos {
    794      1.1  christos   const struct string *s = (const struct string *) p;
    795      1.1  christos 
    796      1.1  christos   return s->hash;
    797      1.1  christos }
    798      1.1  christos 
    799      1.1  christos /* Compare an entry in the string table with a string.  */
    800      1.1  christos static int
    801      1.1  christos eq_string_table_entry (const void *a, const void *b)
    802      1.1  christos {
    803      1.1  christos   const struct string *s1 = (const struct string *) a;
    804      1.1  christos   const char *s2 = (const char *) b;
    805      1.1  christos   size_t s2_len = strlen (s2);
    806      1.1  christos 
    807      1.1  christos   if (s2_len != s1->len)
    808      1.1  christos     return 0;
    809      1.1  christos 
    810      1.1  christos   return memcmp (s1->s, s2, s2_len) == 0;
    811      1.1  christos }
    812      1.1  christos 
    813      1.1  christos /* Parse the string table within the .debug$S section.  */
    814      1.1  christos static void
    815      1.1  christos parse_string_table (bfd_byte *data, size_t size,
    816      1.1  christos 		    struct string_table *strings)
    817      1.1  christos {
    818      1.1  christos   while (true)
    819      1.1  christos     {
    820      1.1  christos       size_t len = strnlen ((char *) data, size);
    821      1.1  christos 
    822      1.1  christos       add_string ((char *) data, len, strings);
    823      1.1  christos 
    824      1.1  christos       data += len + 1;
    825      1.1  christos 
    826      1.1  christos       if (size <= len + 1)
    827      1.1  christos 	break;
    828      1.1  christos 
    829      1.1  christos       size -= len + 1;
    830      1.1  christos     }
    831      1.1  christos }
    832      1.1  christos 
    833      1.1  christos /* Remap a type reference within a CodeView symbol.  */
    834      1.1  christos static bool
    835      1.1  christos remap_symbol_type (void *data, struct type_entry **map, uint32_t num_types)
    836      1.1  christos {
    837      1.1  christos   uint32_t type = bfd_getl32 (data);
    838      1.1  christos 
    839      1.1  christos   /* Ignore builtin types (those with IDs below 0x1000).  */
    840      1.1  christos   if (type < TPI_FIRST_INDEX)
    841      1.1  christos     return true;
    842      1.1  christos 
    843      1.1  christos   if (type >= TPI_FIRST_INDEX + num_types)
    844      1.1  christos     {
    845      1.1  christos       einfo (_("%P: CodeView symbol references out of range type %v\n"),
    846      1.1  christos 	       type);
    847      1.1  christos       return false;
    848      1.1  christos     }
    849      1.1  christos 
    850      1.1  christos   type = TPI_FIRST_INDEX + map[type - TPI_FIRST_INDEX]->index;
    851      1.1  christos   bfd_putl32 (type, data);
    852      1.1  christos 
    853      1.1  christos   return true;
    854      1.1  christos }
    855      1.1  christos 
    856      1.1  christos /* Add an entry into the globals stream.  If it already exists, increase
    857      1.1  christos    the refcount.  */
    858      1.1  christos static bool
    859      1.1  christos add_globals_ref (struct globals *glob, bfd *sym_rec_stream, const char *name,
    860      1.1  christos 		 size_t name_len, uint8_t *data, size_t len)
    861      1.1  christos {
    862      1.1  christos   void **slot;
    863      1.1  christos   uint32_t hash;
    864      1.1  christos   struct global *g;
    865      1.1  christos 
    866      1.1  christos   slot = htab_find_slot_with_hash (glob->hashmap, data,
    867      1.1  christos 				   iterative_hash (data, len, 0), INSERT);
    868      1.1  christos 
    869      1.1  christos   if (*slot)
    870      1.1  christos     {
    871      1.1  christos       g = *slot;
    872      1.1  christos       g->refcount++;
    873      1.1  christos       return true;
    874      1.1  christos     }
    875      1.1  christos 
    876      1.1  christos   *slot = xmalloc (offsetof (struct global, data) + len);
    877      1.1  christos 
    878  1.1.1.2  christos   hash = calc_hash (name, name_len);
    879      1.1  christos   hash %= NUM_GLOBALS_HASH_BUCKETS;
    880      1.1  christos 
    881      1.1  christos   g = *slot;
    882      1.1  christos   g->next = NULL;
    883      1.1  christos   g->offset = bfd_tell (sym_rec_stream);
    884      1.1  christos   g->hash = hash;
    885      1.1  christos   g->refcount = 1;
    886      1.1  christos   memcpy (g->data, data, len);
    887      1.1  christos 
    888      1.1  christos   glob->num_entries++;
    889      1.1  christos 
    890      1.1  christos   if (glob->last)
    891      1.1  christos     glob->last->next = g;
    892      1.1  christos   else
    893      1.1  christos     glob->first = g;
    894      1.1  christos 
    895      1.1  christos   glob->last = g;
    896      1.1  christos 
    897      1.1  christos   return bfd_write (data, len, sym_rec_stream) == len;
    898      1.1  christos }
    899      1.1  christos 
    900      1.1  christos /* Find the end of the current scope within symbols data.  */
    901      1.1  christos static uint8_t *
    902      1.1  christos find_end_of_scope (uint8_t *data, uint32_t size)
    903      1.1  christos {
    904      1.1  christos   unsigned int scope_level = 1;
    905      1.1  christos   uint16_t len;
    906      1.1  christos 
    907      1.1  christos   len = bfd_getl16 (data) + sizeof (uint16_t);
    908      1.1  christos 
    909      1.1  christos   data += len;
    910      1.1  christos   size -= len;
    911      1.1  christos 
    912      1.1  christos   while (true)
    913      1.1  christos     {
    914      1.1  christos       uint16_t type;
    915      1.1  christos 
    916      1.1  christos       if (size < sizeof (uint32_t))
    917      1.1  christos 	return NULL;
    918      1.1  christos 
    919      1.1  christos       len = bfd_getl16 (data) + sizeof (uint16_t);
    920      1.1  christos       type = bfd_getl16 (data + sizeof (uint16_t));
    921      1.1  christos 
    922      1.1  christos       if (size < len)
    923      1.1  christos 	return NULL;
    924      1.1  christos 
    925      1.1  christos       switch (type)
    926      1.1  christos 	{
    927      1.1  christos 	case S_GPROC32:
    928      1.1  christos 	case S_LPROC32:
    929      1.1  christos 	case S_BLOCK32:
    930      1.1  christos 	case S_INLINESITE:
    931      1.1  christos 	case S_THUNK32:
    932      1.1  christos 	  scope_level++;
    933      1.1  christos 	  break;
    934      1.1  christos 
    935      1.1  christos 	case S_END:
    936      1.1  christos 	case S_PROC_ID_END:
    937      1.1  christos 	case S_INLINESITE_END:
    938      1.1  christos 	  scope_level--;
    939      1.1  christos 
    940      1.1  christos 	  if (scope_level == 0)
    941      1.1  christos 	    return data;
    942      1.1  christos 
    943      1.1  christos 	  break;
    944      1.1  christos 	}
    945      1.1  christos 
    946      1.1  christos       data += len;
    947      1.1  christos       size -= len;
    948      1.1  christos     }
    949      1.1  christos }
    950      1.1  christos 
    951      1.1  christos /* Return the size of an extended value parameter, as used in
    952      1.1  christos    LF_ENUMERATE etc.  */
    953      1.1  christos static unsigned int
    954      1.1  christos extended_value_len (uint16_t type)
    955      1.1  christos {
    956      1.1  christos   switch (type)
    957      1.1  christos     {
    958      1.1  christos     case LF_CHAR:
    959      1.1  christos       return 1;
    960      1.1  christos 
    961      1.1  christos     case LF_SHORT:
    962      1.1  christos     case LF_USHORT:
    963      1.1  christos       return 2;
    964      1.1  christos 
    965      1.1  christos     case LF_LONG:
    966      1.1  christos     case LF_ULONG:
    967      1.1  christos       return 4;
    968      1.1  christos 
    969      1.1  christos     case LF_QUADWORD:
    970      1.1  christos     case LF_UQUADWORD:
    971      1.1  christos       return 8;
    972      1.1  christos     }
    973      1.1  christos 
    974      1.1  christos   return 0;
    975      1.1  christos }
    976      1.1  christos 
    977      1.1  christos /* Parse the symbols in a .debug$S section, and copy them to the module's
    978      1.1  christos    symbol stream.  */
    979      1.1  christos static bool
    980      1.1  christos parse_symbols (uint8_t *data, uint32_t size, uint8_t **buf,
    981      1.1  christos 	       struct type_entry **map, uint32_t num_types,
    982      1.1  christos 	       bfd *sym_rec_stream, struct globals *glob, uint16_t mod_num)
    983      1.1  christos {
    984      1.1  christos   uint8_t *orig_buf = *buf;
    985      1.1  christos   unsigned int scope_level = 0;
    986      1.1  christos   uint8_t *scope = NULL;
    987      1.1  christos 
    988      1.1  christos   while (size >= sizeof (uint16_t))
    989      1.1  christos     {
    990      1.1  christos       uint16_t len, type;
    991      1.1  christos 
    992      1.1  christos       len = bfd_getl16 (data) + sizeof (uint16_t);
    993      1.1  christos 
    994      1.1  christos       if (len > size)
    995      1.1  christos 	{
    996      1.1  christos 	  bfd_set_error (bfd_error_bad_value);
    997      1.1  christos 	  return false;
    998      1.1  christos 	}
    999      1.1  christos 
   1000      1.1  christos       type = bfd_getl16 (data + sizeof (uint16_t));
   1001      1.1  christos 
   1002      1.1  christos       switch (type)
   1003      1.1  christos 	{
   1004      1.1  christos 	case S_LDATA32:
   1005      1.1  christos 	case S_GDATA32:
   1006      1.1  christos 	case S_LTHREAD32:
   1007      1.1  christos 	case S_GTHREAD32:
   1008      1.1  christos 	  {
   1009      1.1  christos 	    struct datasym *d = (struct datasym *) data;
   1010      1.1  christos 	    size_t name_len;
   1011      1.1  christos 
   1012      1.1  christos 	    if (len < offsetof (struct datasym, name))
   1013      1.1  christos 	      {
   1014      1.1  christos 		einfo (_("%P: warning: truncated CodeView record"
   1015      1.1  christos 			 " S_LDATA32/S_GDATA32/S_LTHREAD32/S_GTHREAD32\n"));
   1016      1.1  christos 		bfd_set_error (bfd_error_bad_value);
   1017      1.1  christos 		return false;
   1018      1.1  christos 	      }
   1019      1.1  christos 
   1020      1.1  christos 	    if (scope_level == 0)
   1021      1.1  christos 	      {
   1022      1.1  christos 		uint16_t section = bfd_getl16 (&d->section);
   1023      1.1  christos 
   1024      1.1  christos 		if (section == 0) /* GC'd, ignore */
   1025      1.1  christos 		  break;
   1026      1.1  christos 	      }
   1027      1.1  christos 
   1028      1.1  christos 	    name_len =
   1029      1.1  christos 	      strnlen (d->name, len - offsetof (struct datasym, name));
   1030      1.1  christos 
   1031      1.1  christos 	    if (name_len == len - offsetof (struct datasym, name))
   1032      1.1  christos 	      {
   1033      1.1  christos 		einfo (_("%P: warning: name for S_LDATA32/S_GDATA32/"
   1034      1.1  christos 			 "S_LTHREAD32/S_GTHREAD32 has no terminating"
   1035      1.1  christos 			 " zero\n"));
   1036      1.1  christos 		bfd_set_error (bfd_error_bad_value);
   1037      1.1  christos 		return false;
   1038      1.1  christos 	      }
   1039      1.1  christos 
   1040      1.1  christos 	    if (!remap_symbol_type (&d->type, map, num_types))
   1041      1.1  christos 	      {
   1042      1.1  christos 		bfd_set_error (bfd_error_bad_value);
   1043      1.1  christos 		return false;
   1044      1.1  christos 	      }
   1045      1.1  christos 
   1046      1.1  christos 	    /* If S_LDATA32 or S_LTHREAD32, copy into module symbols.  */
   1047      1.1  christos 
   1048      1.1  christos 	    if (type == S_LDATA32 || type == S_LTHREAD32)
   1049      1.1  christos 	      {
   1050      1.1  christos 		memcpy (*buf, d, len);
   1051      1.1  christos 		*buf += len;
   1052      1.1  christos 	      }
   1053      1.1  christos 
   1054      1.1  christos 	    /* S_LDATA32 and S_LTHREAD32 only go in globals if
   1055      1.1  christos 	       not in function scope.  */
   1056      1.1  christos 	    if (type == S_GDATA32 || type == S_GTHREAD32 || scope_level == 0)
   1057      1.1  christos 	      {
   1058      1.1  christos 		if (!add_globals_ref (glob, sym_rec_stream, d->name,
   1059      1.1  christos 				      name_len, data, len))
   1060      1.1  christos 		  return false;
   1061      1.1  christos 	      }
   1062      1.1  christos 
   1063      1.1  christos 	    break;
   1064      1.1  christos 	  }
   1065      1.1  christos 
   1066      1.1  christos 	case S_GPROC32:
   1067      1.1  christos 	case S_LPROC32:
   1068      1.1  christos 	case S_GPROC32_ID:
   1069      1.1  christos 	case S_LPROC32_ID:
   1070      1.1  christos 	  {
   1071      1.1  christos 	    struct procsym *proc = (struct procsym *) data;
   1072      1.1  christos 	    size_t name_len;
   1073      1.1  christos 	    uint16_t section;
   1074      1.1  christos 	    uint32_t end;
   1075      1.1  christos 	    uint8_t *endptr;
   1076      1.1  christos 	    size_t ref_size, padding;
   1077      1.1  christos 	    struct refsym *ref;
   1078      1.1  christos 
   1079      1.1  christos 	    if (len < offsetof (struct procsym, name))
   1080      1.1  christos 	      {
   1081      1.1  christos 		einfo (_("%P: warning: truncated CodeView record"
   1082      1.1  christos 			 " S_GPROC32/S_LPROC32\n"));
   1083      1.1  christos 		bfd_set_error (bfd_error_bad_value);
   1084      1.1  christos 		return false;
   1085      1.1  christos 	      }
   1086      1.1  christos 
   1087      1.1  christos 	    section = bfd_getl16 (&proc->section);
   1088      1.1  christos 
   1089      1.1  christos 	    endptr = find_end_of_scope (data, size);
   1090      1.1  christos 
   1091      1.1  christos 	    if (!endptr)
   1092      1.1  christos 	      {
   1093      1.1  christos 		einfo (_("%P: warning: could not find end of"
   1094      1.1  christos 			 " S_GPROC32/S_LPROC32 record\n"));
   1095      1.1  christos 		bfd_set_error (bfd_error_bad_value);
   1096      1.1  christos 		return false;
   1097      1.1  christos 	      }
   1098      1.1  christos 
   1099      1.1  christos 	    if (section == 0) /* skip if GC'd */
   1100      1.1  christos 	      {
   1101      1.1  christos 		/* Skip to after S_END.  */
   1102      1.1  christos 
   1103      1.1  christos 		size -= endptr - data;
   1104      1.1  christos 		data = endptr;
   1105      1.1  christos 
   1106      1.1  christos 		len = bfd_getl16 (data) + sizeof (uint16_t);
   1107      1.1  christos 
   1108      1.1  christos 		data += len;
   1109      1.1  christos 		size -= len;
   1110      1.1  christos 
   1111      1.1  christos 		continue;
   1112      1.1  christos 	      }
   1113      1.1  christos 
   1114      1.1  christos 	    name_len =
   1115      1.1  christos 	      strnlen (proc->name, len - offsetof (struct procsym, name));
   1116      1.1  christos 
   1117      1.1  christos 	    if (name_len == len - offsetof (struct procsym, name))
   1118      1.1  christos 	      {
   1119      1.1  christos 		einfo (_("%P: warning: name for S_GPROC32/S_LPROC32 has no"
   1120      1.1  christos 			 " terminating zero\n"));
   1121      1.1  christos 		bfd_set_error (bfd_error_bad_value);
   1122      1.1  christos 		return false;
   1123      1.1  christos 	      }
   1124      1.1  christos 
   1125      1.1  christos 	    if (type == S_GPROC32_ID || type == S_LPROC32_ID)
   1126      1.1  christos 	      {
   1127      1.1  christos 		/* Transform into S_GPROC32 / S_LPROC32.  */
   1128      1.1  christos 
   1129      1.1  christos 		uint32_t t_idx = bfd_getl32 (&proc->type);
   1130      1.1  christos 		struct type_entry *t;
   1131      1.1  christos 		uint16_t t_type;
   1132      1.1  christos 
   1133      1.1  christos 		if (t_idx < TPI_FIRST_INDEX
   1134      1.1  christos 		    || t_idx >= TPI_FIRST_INDEX + num_types)
   1135      1.1  christos 		  {
   1136      1.1  christos 		    einfo (_("%P: CodeView symbol references out of range"
   1137      1.1  christos 			     " type %v\n"), type);
   1138      1.1  christos 		    bfd_set_error (bfd_error_bad_value);
   1139      1.1  christos 		    return false;
   1140      1.1  christos 		  }
   1141      1.1  christos 
   1142      1.1  christos 		t = map[t_idx - TPI_FIRST_INDEX];
   1143      1.1  christos 
   1144      1.1  christos 		t_type = bfd_getl16 (t->data + sizeof (uint16_t));
   1145      1.1  christos 
   1146      1.1  christos 		switch (t_type)
   1147      1.1  christos 		  {
   1148      1.1  christos 		  case LF_FUNC_ID:
   1149      1.1  christos 		    {
   1150      1.1  christos 		      struct lf_func_id *t_data =
   1151      1.1  christos 			(struct lf_func_id *) t->data;
   1152      1.1  christos 
   1153      1.1  christos 		      /* Replace proc->type with function type.  */
   1154      1.1  christos 
   1155      1.1  christos 		      memcpy (&proc->type, &t_data->function_type,
   1156      1.1  christos 			      sizeof (uint32_t));
   1157      1.1  christos 
   1158      1.1  christos 		      break;
   1159      1.1  christos 		    }
   1160      1.1  christos 
   1161      1.1  christos 		  case LF_MFUNC_ID:
   1162      1.1  christos 		    {
   1163      1.1  christos 		      struct lf_mfunc_id *t_data =
   1164      1.1  christos 			(struct lf_mfunc_id *) t->data;
   1165      1.1  christos 
   1166      1.1  christos 		      /* Replace proc->type with function type.  */
   1167      1.1  christos 
   1168      1.1  christos 		      memcpy (&proc->type, &t_data->function_type,
   1169      1.1  christos 			      sizeof (uint32_t));
   1170      1.1  christos 
   1171      1.1  christos 		      break;
   1172      1.1  christos 		    }
   1173      1.1  christos 
   1174      1.1  christos 		  default:
   1175      1.1  christos 		    einfo (_("%P: CodeView S_GPROC32_ID/S_LPROC32_ID symbol"
   1176      1.1  christos 			     " referenced unknown type as ID\n"));
   1177      1.1  christos 		    bfd_set_error (bfd_error_bad_value);
   1178      1.1  christos 		    return false;
   1179      1.1  christos 		  }
   1180      1.1  christos 
   1181      1.1  christos 		/* Change record type.  */
   1182      1.1  christos 
   1183      1.1  christos 		if (type == S_GPROC32_ID)
   1184      1.1  christos 		  bfd_putl32 (S_GPROC32, &proc->kind);
   1185      1.1  christos 		else
   1186      1.1  christos 		  bfd_putl32 (S_LPROC32, &proc->kind);
   1187      1.1  christos 	      }
   1188      1.1  christos 	    else
   1189      1.1  christos 	      {
   1190      1.1  christos 		if (!remap_symbol_type (&proc->type, map, num_types))
   1191      1.1  christos 		  {
   1192      1.1  christos 		    bfd_set_error (bfd_error_bad_value);
   1193      1.1  christos 		    return false;
   1194      1.1  christos 		  }
   1195      1.1  christos 	      }
   1196      1.1  christos 
   1197      1.1  christos 	    end = *buf - orig_buf + sizeof (uint32_t) + endptr - data;
   1198      1.1  christos 	    bfd_putl32 (end, &proc->end);
   1199      1.1  christos 
   1200      1.1  christos 	    /* Add S_PROCREF / S_LPROCREF to globals stream.  */
   1201      1.1  christos 
   1202      1.1  christos 	    ref_size = offsetof (struct refsym, name) + name_len + 1;
   1203      1.1  christos 
   1204      1.1  christos 	    if (ref_size % sizeof (uint32_t))
   1205      1.1  christos 	      padding = sizeof (uint32_t) - (ref_size % sizeof (uint32_t));
   1206      1.1  christos 	    else
   1207      1.1  christos 	      padding = 0;
   1208      1.1  christos 
   1209      1.1  christos 	    ref = xmalloc (ref_size + padding);
   1210      1.1  christos 
   1211      1.1  christos 	    bfd_putl16 (ref_size + padding - sizeof (uint16_t), &ref->size);
   1212      1.1  christos 	    bfd_putl16 (type == S_GPROC32 || type == S_GPROC32_ID ?
   1213      1.1  christos 			S_PROCREF : S_LPROCREF, &ref->kind);
   1214      1.1  christos 	    bfd_putl32 (0, &ref->sum_name);
   1215      1.1  christos 	    bfd_putl32 (*buf - orig_buf + sizeof (uint32_t),
   1216      1.1  christos 			&ref->symbol_offset);
   1217      1.1  christos 	    bfd_putl16 (mod_num + 1, &ref->mod);
   1218      1.1  christos 
   1219      1.1  christos 	    memcpy (ref->name, proc->name, name_len + 1);
   1220      1.1  christos 
   1221      1.1  christos 	    memset (ref->name + name_len + 1, 0, padding);
   1222      1.1  christos 
   1223      1.1  christos 	    if (!add_globals_ref (glob, sym_rec_stream, proc->name, name_len,
   1224      1.1  christos 				  (uint8_t *) ref, ref_size + padding))
   1225      1.1  christos 	      {
   1226      1.1  christos 		free (ref);
   1227      1.1  christos 		return false;
   1228      1.1  christos 	      }
   1229      1.1  christos 
   1230      1.1  christos 	    free (ref);
   1231      1.1  christos 
   1232      1.1  christos 	    scope = *buf;
   1233      1.1  christos 
   1234      1.1  christos 	    memcpy (*buf, proc, len);
   1235      1.1  christos 	    *buf += len;
   1236      1.1  christos 
   1237      1.1  christos 	    scope_level++;
   1238      1.1  christos 
   1239      1.1  christos 	    break;
   1240      1.1  christos 	  }
   1241      1.1  christos 
   1242      1.1  christos 	case S_UDT:
   1243      1.1  christos 	  {
   1244      1.1  christos 	    struct udtsym *udt = (struct udtsym *) data;
   1245      1.1  christos 	    size_t name_len;
   1246      1.1  christos 
   1247      1.1  christos 	    if (len < offsetof (struct udtsym, name))
   1248      1.1  christos 	      {
   1249      1.1  christos 		einfo (_("%P: warning: truncated CodeView record"
   1250      1.1  christos 			 " S_UDT\n"));
   1251      1.1  christos 		bfd_set_error (bfd_error_bad_value);
   1252      1.1  christos 		return false;
   1253      1.1  christos 	      }
   1254      1.1  christos 
   1255      1.1  christos 	    name_len =
   1256      1.1  christos 	      strnlen (udt->name, len - offsetof (struct udtsym, name));
   1257      1.1  christos 
   1258      1.1  christos 	    if (name_len == len - offsetof (struct udtsym, name))
   1259      1.1  christos 	      {
   1260      1.1  christos 		einfo (_("%P: warning: name for S_UDT has no"
   1261      1.1  christos 			 " terminating zero\n"));
   1262      1.1  christos 		bfd_set_error (bfd_error_bad_value);
   1263      1.1  christos 		return false;
   1264      1.1  christos 	      }
   1265      1.1  christos 
   1266      1.1  christos 	    if (!remap_symbol_type (&udt->type, map, num_types))
   1267      1.1  christos 	      {
   1268      1.1  christos 		bfd_set_error (bfd_error_bad_value);
   1269      1.1  christos 		return false;
   1270      1.1  christos 	      }
   1271      1.1  christos 
   1272      1.1  christos 	    /* S_UDT goes in the symbols stream if within a procedure,
   1273      1.1  christos 	       otherwise it goes in the globals stream.  */
   1274      1.1  christos 	    if (scope_level == 0)
   1275      1.1  christos 	      {
   1276      1.1  christos 		if (!add_globals_ref (glob, sym_rec_stream, udt->name,
   1277      1.1  christos 				      name_len, data, len))
   1278      1.1  christos 		  return false;
   1279      1.1  christos 	      }
   1280      1.1  christos 	    else
   1281      1.1  christos 	      {
   1282      1.1  christos 		memcpy (*buf, udt, len);
   1283      1.1  christos 		*buf += len;
   1284      1.1  christos 	      }
   1285      1.1  christos 
   1286      1.1  christos 	    break;
   1287      1.1  christos 	  }
   1288      1.1  christos 
   1289      1.1  christos 	case S_CONSTANT:
   1290      1.1  christos 	  {
   1291      1.1  christos 	    struct constsym *c = (struct constsym *) data;
   1292      1.1  christos 	    size_t name_len, rec_size;
   1293      1.1  christos 	    uint16_t val;
   1294      1.1  christos 
   1295      1.1  christos 	    if (len < offsetof (struct constsym, name))
   1296      1.1  christos 	      {
   1297      1.1  christos 		einfo (_("%P: warning: truncated CodeView record"
   1298      1.1  christos 			 " S_CONSTANT\n"));
   1299      1.1  christos 		bfd_set_error (bfd_error_bad_value);
   1300      1.1  christos 		return false;
   1301      1.1  christos 	      }
   1302      1.1  christos 
   1303      1.1  christos 	    rec_size = offsetof (struct constsym, name);
   1304      1.1  christos 
   1305      1.1  christos 	    val = bfd_getl16 (&c->value);
   1306      1.1  christos 
   1307      1.1  christos 	    /* If val >= 0x8000, actual value follows.  */
   1308      1.1  christos 	    if (val >= 0x8000)
   1309      1.1  christos 	      {
   1310      1.1  christos 		unsigned int param_len = extended_value_len (val);
   1311      1.1  christos 
   1312      1.1  christos 		if (param_len == 0)
   1313      1.1  christos 		  {
   1314      1.1  christos 		    einfo (_("%P: warning: unhandled type %v within"
   1315      1.1  christos 			     " S_CONSTANT\n"), val);
   1316      1.1  christos 		    bfd_set_error (bfd_error_bad_value);
   1317      1.1  christos 		    return false;
   1318      1.1  christos 		  }
   1319      1.1  christos 
   1320      1.1  christos 		rec_size += param_len;
   1321      1.1  christos 	      }
   1322      1.1  christos 
   1323      1.1  christos 	    name_len =
   1324      1.1  christos 	      strnlen ((const char *) data + rec_size, len - rec_size);
   1325      1.1  christos 
   1326      1.1  christos 	    if (name_len == len - rec_size)
   1327      1.1  christos 	      {
   1328      1.1  christos 		einfo (_("%P: warning: name for S_CONSTANT has no"
   1329      1.1  christos 			 " terminating zero\n"));
   1330      1.1  christos 		bfd_set_error (bfd_error_bad_value);
   1331      1.1  christos 		return false;
   1332      1.1  christos 	      }
   1333      1.1  christos 
   1334      1.1  christos 	    if (!remap_symbol_type (&c->type, map, num_types))
   1335      1.1  christos 	      {
   1336      1.1  christos 		bfd_set_error (bfd_error_bad_value);
   1337      1.1  christos 		return false;
   1338      1.1  christos 	      }
   1339      1.1  christos 
   1340      1.1  christos 	    if (!add_globals_ref (glob, sym_rec_stream,
   1341      1.1  christos 				  (const char *) data + rec_size, name_len,
   1342      1.1  christos 				  data, len))
   1343      1.1  christos 	      return false;
   1344      1.1  christos 
   1345      1.1  christos 	    break;
   1346      1.1  christos 	  }
   1347      1.1  christos 
   1348      1.1  christos 	case S_END:
   1349      1.1  christos 	case S_INLINESITE_END:
   1350      1.1  christos 	case S_PROC_ID_END:
   1351      1.1  christos 	  memcpy (*buf, data, len);
   1352      1.1  christos 
   1353      1.1  christos 	  if (type == S_PROC_ID_END) /* transform to S_END */
   1354      1.1  christos 	    bfd_putl16 (S_END, *buf + sizeof (uint16_t));
   1355      1.1  christos 
   1356      1.1  christos 	  /* Reset scope variable back to the address of the previous
   1357      1.1  christos 	     scope start.  */
   1358      1.1  christos 	  if (scope)
   1359      1.1  christos 	    {
   1360      1.1  christos 	      uint32_t parent;
   1361      1.1  christos 	      uint16_t scope_start_type =
   1362      1.1  christos 		bfd_getl16 (scope + sizeof (uint16_t));
   1363      1.1  christos 
   1364      1.1  christos 	      switch (scope_start_type)
   1365      1.1  christos 		{
   1366      1.1  christos 		case S_GPROC32:
   1367      1.1  christos 		case S_LPROC32:
   1368      1.1  christos 		  parent = bfd_getl32 (scope + offsetof (struct procsym,
   1369      1.1  christos 							 parent));
   1370      1.1  christos 		  break;
   1371      1.1  christos 
   1372      1.1  christos 		case S_BLOCK32:
   1373      1.1  christos 		  parent = bfd_getl32 (scope + offsetof (struct blocksym,
   1374      1.1  christos 							 parent));
   1375      1.1  christos 		  break;
   1376      1.1  christos 
   1377      1.1  christos 		case S_INLINESITE:
   1378      1.1  christos 		  parent = bfd_getl32 (scope + offsetof (struct inline_site,
   1379      1.1  christos 							 parent));
   1380      1.1  christos 		  break;
   1381      1.1  christos 
   1382      1.1  christos 		case S_THUNK32:
   1383      1.1  christos 		  parent = bfd_getl32 (scope + offsetof (struct thunk,
   1384      1.1  christos 							 parent));
   1385      1.1  christos 		  break;
   1386      1.1  christos 
   1387      1.1  christos 		default:
   1388      1.1  christos 		  einfo (_("%P: warning: unexpected CodeView scope start"
   1389      1.1  christos 			   " record %v\n"), scope_start_type);
   1390      1.1  christos 		  bfd_set_error (bfd_error_bad_value);
   1391      1.1  christos 		  return false;
   1392      1.1  christos 		}
   1393      1.1  christos 
   1394      1.1  christos 	      if (parent == 0)
   1395      1.1  christos 		scope = NULL;
   1396      1.1  christos 	      else
   1397      1.1  christos 		scope = orig_buf + parent - sizeof (uint32_t);
   1398      1.1  christos 	    }
   1399      1.1  christos 
   1400      1.1  christos 	  *buf += len;
   1401      1.1  christos 	  scope_level--;
   1402      1.1  christos 	  break;
   1403      1.1  christos 
   1404      1.1  christos 	case S_BUILDINFO:
   1405      1.1  christos 	  {
   1406      1.1  christos 	    struct buildinfosym *bi = (struct buildinfosym *) data;
   1407      1.1  christos 
   1408      1.1  christos 	    if (len < sizeof (struct buildinfosym))
   1409      1.1  christos 	      {
   1410      1.1  christos 		einfo (_("%P: warning: truncated CodeView record"
   1411      1.1  christos 			 " S_BUILDINFO\n"));
   1412      1.1  christos 		bfd_set_error (bfd_error_bad_value);
   1413      1.1  christos 		return false;
   1414      1.1  christos 	      }
   1415      1.1  christos 
   1416      1.1  christos 	    if (!remap_symbol_type (&bi->type, map, num_types))
   1417      1.1  christos 	      {
   1418      1.1  christos 		bfd_set_error (bfd_error_bad_value);
   1419      1.1  christos 		return false;
   1420      1.1  christos 	      }
   1421      1.1  christos 
   1422      1.1  christos 	    memcpy (*buf, data, len);
   1423      1.1  christos 	    *buf += len;
   1424      1.1  christos 
   1425      1.1  christos 	    break;
   1426      1.1  christos 	  }
   1427      1.1  christos 
   1428      1.1  christos 	case S_BLOCK32:
   1429      1.1  christos 	  {
   1430      1.1  christos 	    struct blocksym *bl = (struct blocksym *) data;
   1431      1.1  christos 	    uint8_t *endptr;
   1432      1.1  christos 	    uint32_t end;
   1433      1.1  christos 
   1434      1.1  christos 	    if (len < offsetof (struct blocksym, name))
   1435      1.1  christos 	      {
   1436      1.1  christos 		einfo (_("%P: warning: truncated CodeView record"
   1437      1.1  christos 			 " S_BLOCK32\n"));
   1438      1.1  christos 		bfd_set_error (bfd_error_bad_value);
   1439      1.1  christos 		return false;
   1440      1.1  christos 	      }
   1441      1.1  christos 
   1442      1.1  christos 	    bfd_putl32 (scope - orig_buf + sizeof (uint32_t), &bl->parent);
   1443      1.1  christos 
   1444      1.1  christos 	    endptr = find_end_of_scope (data, size);
   1445      1.1  christos 
   1446      1.1  christos 	    if (!endptr)
   1447      1.1  christos 	      {
   1448      1.1  christos 		einfo (_("%P: warning: could not find end of"
   1449      1.1  christos 			 " S_BLOCK32 record\n"));
   1450      1.1  christos 		bfd_set_error (bfd_error_bad_value);
   1451      1.1  christos 		return false;
   1452      1.1  christos 	      }
   1453      1.1  christos 
   1454      1.1  christos 	    end = *buf - orig_buf + sizeof (uint32_t) + endptr - data;
   1455      1.1  christos 	    bfd_putl32 (end, &bl->end);
   1456      1.1  christos 
   1457      1.1  christos 	    scope = *buf;
   1458      1.1  christos 
   1459      1.1  christos 	    memcpy (*buf, data, len);
   1460      1.1  christos 	    *buf += len;
   1461      1.1  christos 
   1462      1.1  christos 	    scope_level++;
   1463      1.1  christos 
   1464      1.1  christos 	    break;
   1465      1.1  christos 	  }
   1466      1.1  christos 
   1467      1.1  christos 	case S_BPREL32:
   1468      1.1  christos 	  {
   1469      1.1  christos 	    struct bprelsym *bp = (struct bprelsym *) data;
   1470      1.1  christos 
   1471      1.1  christos 	    if (len < offsetof (struct bprelsym, name))
   1472      1.1  christos 	      {
   1473      1.1  christos 		einfo (_("%P: warning: truncated CodeView record"
   1474      1.1  christos 			 " S_BPREL32\n"));
   1475      1.1  christos 		bfd_set_error (bfd_error_bad_value);
   1476      1.1  christos 		return false;
   1477      1.1  christos 	      }
   1478      1.1  christos 
   1479      1.1  christos 	    if (!remap_symbol_type (&bp->type, map, num_types))
   1480      1.1  christos 	      {
   1481      1.1  christos 		bfd_set_error (bfd_error_bad_value);
   1482      1.1  christos 		return false;
   1483      1.1  christos 	      }
   1484      1.1  christos 
   1485      1.1  christos 	    memcpy (*buf, data, len);
   1486      1.1  christos 	    *buf += len;
   1487      1.1  christos 
   1488      1.1  christos 	    break;
   1489      1.1  christos 	  }
   1490      1.1  christos 
   1491      1.1  christos 	case S_REGISTER:
   1492      1.1  christos 	  {
   1493      1.1  christos 	    struct regsym *reg = (struct regsym *) data;
   1494      1.1  christos 
   1495      1.1  christos 	    if (len < offsetof (struct regsym, name))
   1496      1.1  christos 	      {
   1497      1.1  christos 		einfo (_("%P: warning: truncated CodeView record"
   1498      1.1  christos 			 " S_REGISTER\n"));
   1499      1.1  christos 		bfd_set_error (bfd_error_bad_value);
   1500      1.1  christos 		return false;
   1501      1.1  christos 	      }
   1502      1.1  christos 
   1503      1.1  christos 	    if (!remap_symbol_type (&reg->type, map, num_types))
   1504      1.1  christos 	      {
   1505      1.1  christos 		bfd_set_error (bfd_error_bad_value);
   1506      1.1  christos 		return false;
   1507      1.1  christos 	      }
   1508      1.1  christos 
   1509      1.1  christos 	    memcpy (*buf, data, len);
   1510      1.1  christos 	    *buf += len;
   1511      1.1  christos 
   1512      1.1  christos 	    break;
   1513      1.1  christos 	  }
   1514      1.1  christos 
   1515      1.1  christos 	case S_REGREL32:
   1516      1.1  christos 	  {
   1517      1.1  christos 	    struct regrel *rr = (struct regrel *) data;
   1518      1.1  christos 
   1519      1.1  christos 	    if (len < offsetof (struct regrel, name))
   1520      1.1  christos 	      {
   1521      1.1  christos 		einfo (_("%P: warning: truncated CodeView record"
   1522      1.1  christos 			 " S_REGREL32\n"));
   1523      1.1  christos 		bfd_set_error (bfd_error_bad_value);
   1524      1.1  christos 		return false;
   1525      1.1  christos 	      }
   1526      1.1  christos 
   1527      1.1  christos 	    if (!remap_symbol_type (&rr->type, map, num_types))
   1528      1.1  christos 	      {
   1529      1.1  christos 		bfd_set_error (bfd_error_bad_value);
   1530      1.1  christos 		return false;
   1531      1.1  christos 	      }
   1532      1.1  christos 
   1533      1.1  christos 	    memcpy (*buf, data, len);
   1534      1.1  christos 	    *buf += len;
   1535      1.1  christos 
   1536      1.1  christos 	    break;
   1537      1.1  christos 	  }
   1538      1.1  christos 
   1539      1.1  christos 	case S_LOCAL:
   1540      1.1  christos 	  {
   1541      1.1  christos 	    struct localsym *l = (struct localsym *) data;
   1542      1.1  christos 
   1543      1.1  christos 	    if (len < offsetof (struct localsym, name))
   1544      1.1  christos 	      {
   1545      1.1  christos 		einfo (_("%P: warning: truncated CodeView record"
   1546      1.1  christos 			 " S_LOCAL\n"));
   1547      1.1  christos 		bfd_set_error (bfd_error_bad_value);
   1548      1.1  christos 		return false;
   1549      1.1  christos 	      }
   1550      1.1  christos 
   1551      1.1  christos 	    if (!remap_symbol_type (&l->type, map, num_types))
   1552      1.1  christos 	      {
   1553      1.1  christos 		bfd_set_error (bfd_error_bad_value);
   1554      1.1  christos 		return false;
   1555      1.1  christos 	      }
   1556      1.1  christos 
   1557      1.1  christos 	    memcpy (*buf, data, len);
   1558      1.1  christos 	    *buf += len;
   1559      1.1  christos 
   1560      1.1  christos 	    break;
   1561      1.1  christos 	  }
   1562      1.1  christos 
   1563      1.1  christos 	case S_INLINESITE:
   1564      1.1  christos 	  {
   1565      1.1  christos 	    struct inline_site *is = (struct inline_site *) data;
   1566      1.1  christos 	    uint8_t *endptr;
   1567      1.1  christos 	    uint32_t end;
   1568      1.1  christos 
   1569      1.1  christos 	    if (len < offsetof (struct inline_site, binary_annotations))
   1570      1.1  christos 	      {
   1571      1.1  christos 		einfo (_("%P: warning: truncated CodeView record"
   1572      1.1  christos 			 " S_INLINESITE\n"));
   1573      1.1  christos 		bfd_set_error (bfd_error_bad_value);
   1574      1.1  christos 		return false;
   1575      1.1  christos 	      }
   1576      1.1  christos 
   1577      1.1  christos 	    bfd_putl32 (scope - orig_buf + sizeof (uint32_t), &is->parent);
   1578      1.1  christos 
   1579      1.1  christos 	    endptr = find_end_of_scope (data, size);
   1580      1.1  christos 
   1581      1.1  christos 	    if (!endptr)
   1582      1.1  christos 	      {
   1583      1.1  christos 		einfo (_("%P: warning: could not find end of"
   1584      1.1  christos 			 " S_INLINESITE record\n"));
   1585      1.1  christos 		bfd_set_error (bfd_error_bad_value);
   1586      1.1  christos 		return false;
   1587      1.1  christos 	      }
   1588      1.1  christos 
   1589      1.1  christos 	    end = *buf - orig_buf + sizeof (uint32_t) + endptr - data;
   1590      1.1  christos 	    bfd_putl32 (end, &is->end);
   1591      1.1  christos 
   1592      1.1  christos 	    if (!remap_symbol_type (&is->inlinee, map, num_types))
   1593      1.1  christos 	      {
   1594      1.1  christos 		bfd_set_error (bfd_error_bad_value);
   1595      1.1  christos 		return false;
   1596      1.1  christos 	      }
   1597      1.1  christos 
   1598      1.1  christos 	    scope = *buf;
   1599      1.1  christos 
   1600      1.1  christos 	    memcpy (*buf, data, len);
   1601      1.1  christos 	    *buf += len;
   1602      1.1  christos 
   1603      1.1  christos 	    scope_level++;
   1604      1.1  christos 
   1605      1.1  christos 	    break;
   1606      1.1  christos 	  }
   1607      1.1  christos 
   1608      1.1  christos 	case S_THUNK32:
   1609      1.1  christos 	  {
   1610      1.1  christos 	    struct thunk *th = (struct thunk *) data;
   1611      1.1  christos 	    uint8_t *endptr;
   1612      1.1  christos 	    uint32_t end;
   1613      1.1  christos 
   1614      1.1  christos 	    if (len < offsetof (struct thunk, name))
   1615      1.1  christos 	      {
   1616      1.1  christos 		einfo (_("%P: warning: truncated CodeView record"
   1617      1.1  christos 			 " S_THUNK32\n"));
   1618      1.1  christos 		bfd_set_error (bfd_error_bad_value);
   1619      1.1  christos 		return false;
   1620      1.1  christos 	      }
   1621      1.1  christos 
   1622      1.1  christos 	    bfd_putl32 (scope - orig_buf + sizeof (uint32_t), &th->parent);
   1623      1.1  christos 
   1624      1.1  christos 	    endptr = find_end_of_scope (data, size);
   1625      1.1  christos 
   1626      1.1  christos 	    if (!endptr)
   1627      1.1  christos 	      {
   1628      1.1  christos 		einfo (_("%P: warning: could not find end of"
   1629      1.1  christos 			 " S_THUNK32 record\n"));
   1630      1.1  christos 		bfd_set_error (bfd_error_bad_value);
   1631      1.1  christos 		return false;
   1632      1.1  christos 	      }
   1633      1.1  christos 
   1634      1.1  christos 	    end = *buf - orig_buf + sizeof (uint32_t) + endptr - data;
   1635      1.1  christos 	    bfd_putl32 (end, &th->end);
   1636      1.1  christos 
   1637      1.1  christos 	    scope = *buf;
   1638      1.1  christos 
   1639      1.1  christos 	    memcpy (*buf, data, len);
   1640      1.1  christos 	    *buf += len;
   1641      1.1  christos 
   1642      1.1  christos 	    scope_level++;
   1643      1.1  christos 
   1644      1.1  christos 	    break;
   1645      1.1  christos 	  }
   1646      1.1  christos 
   1647      1.1  christos 	case S_HEAPALLOCSITE:
   1648      1.1  christos 	  {
   1649      1.1  christos 	    struct heap_alloc_site *has = (struct heap_alloc_site *) data;
   1650      1.1  christos 
   1651      1.1  christos 	    if (len < sizeof (struct heap_alloc_site))
   1652      1.1  christos 	      {
   1653      1.1  christos 		einfo (_("%P: warning: truncated CodeView record"
   1654      1.1  christos 			 " S_HEAPALLOCSITE\n"));
   1655      1.1  christos 		bfd_set_error (bfd_error_bad_value);
   1656      1.1  christos 		return false;
   1657      1.1  christos 	      }
   1658      1.1  christos 
   1659      1.1  christos 	    if (!remap_symbol_type (&has->type, map, num_types))
   1660      1.1  christos 	      {
   1661      1.1  christos 		bfd_set_error (bfd_error_bad_value);
   1662      1.1  christos 		return false;
   1663      1.1  christos 	      }
   1664      1.1  christos 
   1665      1.1  christos 	    memcpy (*buf, data, len);
   1666      1.1  christos 	    *buf += len;
   1667      1.1  christos 
   1668      1.1  christos 	    break;
   1669      1.1  christos 	  }
   1670      1.1  christos 
   1671      1.1  christos 	case S_OBJNAME: /* just copy */
   1672      1.1  christos 	case S_COMPILE3:
   1673      1.1  christos 	case S_UNAMESPACE:
   1674      1.1  christos 	case S_FRAMEPROC:
   1675      1.1  christos 	case S_FRAMECOOKIE:
   1676      1.1  christos 	case S_LABEL32:
   1677      1.1  christos 	case S_DEFRANGE_REGISTER_REL:
   1678      1.1  christos 	case S_DEFRANGE_FRAMEPOINTER_REL:
   1679      1.1  christos 	case S_DEFRANGE_SUBFIELD_REGISTER:
   1680      1.1  christos 	case S_DEFRANGE_FRAMEPOINTER_REL_FULL_SCOPE:
   1681      1.1  christos 	case S_DEFRANGE_REGISTER:
   1682      1.1  christos 	  memcpy (*buf, data, len);
   1683      1.1  christos 	  *buf += len;
   1684      1.1  christos 	  break;
   1685      1.1  christos 
   1686      1.1  christos 	default:
   1687      1.1  christos 	  einfo (_("%P: warning: unrecognized CodeView record %v\n"), type);
   1688      1.1  christos 	  bfd_set_error (bfd_error_bad_value);
   1689      1.1  christos 	  return false;
   1690      1.1  christos 	}
   1691      1.1  christos 
   1692      1.1  christos       data += len;
   1693      1.1  christos       size -= len;
   1694      1.1  christos     }
   1695      1.1  christos 
   1696      1.1  christos   return true;
   1697      1.1  christos }
   1698      1.1  christos 
   1699      1.1  christos /* For a given symbol subsection, work out how much space to allocate in the
   1700      1.1  christos    result module stream.  This is different because we don't copy certain
   1701      1.1  christos    symbols, such as S_CONSTANT, and we skip over any procedures or data that
   1702      1.1  christos    have been GC'd out.  */
   1703      1.1  christos static bool
   1704      1.1  christos calculate_symbols_size (uint8_t *data, uint32_t size, uint32_t *sym_size)
   1705      1.1  christos {
   1706      1.1  christos   unsigned int scope_level = 0;
   1707      1.1  christos 
   1708      1.1  christos   while (size >= sizeof (uint32_t))
   1709      1.1  christos     {
   1710      1.1  christos       uint16_t len = bfd_getl16 (data) + sizeof (uint16_t);
   1711      1.1  christos       uint16_t type = bfd_getl16 (data + sizeof (uint16_t));
   1712      1.1  christos 
   1713      1.1  christos       switch (type)
   1714      1.1  christos 	{
   1715      1.1  christos 	case S_LDATA32:
   1716      1.1  christos 	case S_LTHREAD32:
   1717      1.1  christos 	  {
   1718      1.1  christos 	    struct datasym *d = (struct datasym *) data;
   1719      1.1  christos 	    uint16_t section;
   1720      1.1  christos 
   1721      1.1  christos 	    if (len < offsetof (struct datasym, name))
   1722      1.1  christos 	      {
   1723      1.1  christos 		einfo (_("%P: warning: truncated CodeView record"
   1724      1.1  christos 			 " S_LDATA32/S_LTHREAD32\n"));
   1725      1.1  christos 		return false;
   1726      1.1  christos 	      }
   1727      1.1  christos 
   1728      1.1  christos 	    section = bfd_getl16 (&d->section);
   1729      1.1  christos 
   1730      1.1  christos 	    /* copy if not GC'd or within function */
   1731      1.1  christos 	    if (scope_level != 0 || section != 0)
   1732      1.1  christos 	      *sym_size += len;
   1733      1.1  christos 	  }
   1734      1.1  christos 
   1735      1.1  christos 	case S_GDATA32:
   1736      1.1  christos 	case S_GTHREAD32:
   1737      1.1  christos 	case S_CONSTANT:
   1738      1.1  christos 	  /* Not copied into symbols stream.  */
   1739      1.1  christos 	  break;
   1740      1.1  christos 
   1741      1.1  christos 	case S_GPROC32:
   1742      1.1  christos 	case S_LPROC32:
   1743      1.1  christos 	case S_GPROC32_ID:
   1744      1.1  christos 	case S_LPROC32_ID:
   1745      1.1  christos 	  {
   1746      1.1  christos 	    struct procsym *proc = (struct procsym *) data;
   1747      1.1  christos 	    uint16_t section;
   1748      1.1  christos 
   1749      1.1  christos 	    if (len < offsetof (struct procsym, name))
   1750      1.1  christos 	      {
   1751      1.1  christos 		einfo (_("%P: warning: truncated CodeView record"
   1752      1.1  christos 			 " S_GPROC32/S_LPROC32\n"));
   1753      1.1  christos 		return false;
   1754      1.1  christos 	      }
   1755      1.1  christos 
   1756      1.1  christos 	    section = bfd_getl16 (&proc->section);
   1757      1.1  christos 
   1758      1.1  christos 	    if (section != 0)
   1759      1.1  christos 	      {
   1760      1.1  christos 		*sym_size += len;
   1761      1.1  christos 	      }
   1762      1.1  christos 	    else
   1763      1.1  christos 	      {
   1764      1.1  christos 		uint8_t *endptr = find_end_of_scope (data, size);
   1765      1.1  christos 
   1766      1.1  christos 		if (!endptr)
   1767      1.1  christos 		  {
   1768      1.1  christos 		    einfo (_("%P: warning: could not find end of"
   1769      1.1  christos 			     " S_GPROC32/S_LPROC32 record\n"));
   1770      1.1  christos 		    return false;
   1771      1.1  christos 		  }
   1772      1.1  christos 
   1773      1.1  christos 		/* Skip to after S_END.  */
   1774      1.1  christos 
   1775      1.1  christos 		size -= endptr - data;
   1776      1.1  christos 		data = endptr;
   1777      1.1  christos 
   1778      1.1  christos 		len = bfd_getl16 (data) + sizeof (uint16_t);
   1779      1.1  christos 
   1780      1.1  christos 		data += len;
   1781      1.1  christos 		size -= len;
   1782      1.1  christos 
   1783      1.1  christos 		continue;
   1784      1.1  christos 	      }
   1785      1.1  christos 
   1786      1.1  christos 	    scope_level++;
   1787      1.1  christos 
   1788      1.1  christos 	    break;
   1789      1.1  christos 	  }
   1790      1.1  christos 
   1791      1.1  christos 	case S_UDT:
   1792      1.1  christos 	  if (scope_level != 0) /* only goes in symbols if local */
   1793      1.1  christos 	    *sym_size += len;
   1794      1.1  christos 	  break;
   1795      1.1  christos 
   1796      1.1  christos 	case S_BLOCK32: /* always copied */
   1797      1.1  christos 	case S_INLINESITE:
   1798      1.1  christos 	case S_THUNK32:
   1799      1.1  christos 	  *sym_size += len;
   1800      1.1  christos 	  scope_level++;
   1801      1.1  christos 	  break;
   1802      1.1  christos 
   1803      1.1  christos 	case S_END: /* always copied */
   1804      1.1  christos 	case S_PROC_ID_END:
   1805      1.1  christos 	case S_INLINESITE_END:
   1806      1.1  christos 	  *sym_size += len;
   1807      1.1  christos 	  scope_level--;
   1808      1.1  christos 	  break;
   1809      1.1  christos 
   1810      1.1  christos 	case S_OBJNAME: /* always copied */
   1811      1.1  christos 	case S_COMPILE3:
   1812      1.1  christos 	case S_UNAMESPACE:
   1813      1.1  christos 	case S_FRAMEPROC:
   1814      1.1  christos 	case S_FRAMECOOKIE:
   1815      1.1  christos 	case S_LABEL32:
   1816      1.1  christos 	case S_BUILDINFO:
   1817      1.1  christos 	case S_BPREL32:
   1818      1.1  christos 	case S_REGISTER:
   1819      1.1  christos 	case S_REGREL32:
   1820      1.1  christos 	case S_LOCAL:
   1821      1.1  christos 	case S_DEFRANGE_REGISTER_REL:
   1822      1.1  christos 	case S_DEFRANGE_FRAMEPOINTER_REL:
   1823      1.1  christos 	case S_DEFRANGE_SUBFIELD_REGISTER:
   1824      1.1  christos 	case S_DEFRANGE_FRAMEPOINTER_REL_FULL_SCOPE:
   1825      1.1  christos 	case S_DEFRANGE_REGISTER:
   1826      1.1  christos 	case S_HEAPALLOCSITE:
   1827      1.1  christos 	  *sym_size += len;
   1828      1.1  christos 	  break;
   1829      1.1  christos 
   1830      1.1  christos 	default:
   1831      1.1  christos 	  einfo (_("%P: warning: unrecognized CodeView record %v\n"), type);
   1832      1.1  christos 	  return false;
   1833      1.1  christos 	}
   1834      1.1  christos 
   1835      1.1  christos       data += len;
   1836      1.1  christos       size -= len;
   1837      1.1  christos     }
   1838      1.1  christos 
   1839      1.1  christos   return true;
   1840      1.1  christos }
   1841      1.1  christos 
   1842  1.1.1.2  christos /* Parse the DEBUG_S_INLINEELINES data, which records the line numbers that
   1843  1.1.1.2  christos    correspond to inlined functions.  This is similar to DEBUG_S_LINES (see
   1844  1.1.1.2  christos    handle_debugs_section), but rather than just copying we also need to remap
   1845  1.1.1.2  christos    the numbers of the referenced LF_FUNC_ID types.  */
   1846  1.1.1.2  christos 
   1847  1.1.1.2  christos static bool
   1848  1.1.1.2  christos parse_inlinee_lines (uint8_t *data, uint32_t size, uint8_t **bufptr,
   1849  1.1.1.2  christos 		     struct type_entry **map, uint32_t num_types)
   1850  1.1.1.2  christos {
   1851  1.1.1.2  christos   uint32_t version;
   1852  1.1.1.2  christos   uint8_t *ptr;
   1853  1.1.1.2  christos   unsigned int num_entries;
   1854  1.1.1.2  christos 
   1855  1.1.1.2  christos   bfd_putl32 (DEBUG_S_INLINEELINES, *bufptr);
   1856  1.1.1.2  christos   *bufptr += sizeof (uint32_t);
   1857  1.1.1.2  christos 
   1858  1.1.1.2  christos   bfd_putl32 (size, *bufptr);
   1859  1.1.1.2  christos   *bufptr += sizeof (uint32_t);
   1860  1.1.1.2  christos 
   1861  1.1.1.2  christos   /* The inlinee lines data consists of a version uint32_t (0), followed by an
   1862  1.1.1.2  christos      array of struct inlinee_source_line:
   1863  1.1.1.2  christos 
   1864  1.1.1.2  christos      struct inlinee_source_line
   1865  1.1.1.2  christos      {
   1866  1.1.1.2  christos 	uint32_t function_id;
   1867  1.1.1.2  christos 	uint32_t file_id;
   1868  1.1.1.2  christos 	uint32_t line_no;
   1869  1.1.1.2  christos      };
   1870  1.1.1.2  christos 
   1871  1.1.1.2  christos      (see InlineeSourceLine in cvinfo.h)
   1872  1.1.1.2  christos 
   1873  1.1.1.2  christos      We're only interested here in the function_id, as we need to remap its
   1874  1.1.1.2  christos      type number.
   1875  1.1.1.2  christos   */
   1876  1.1.1.2  christos 
   1877  1.1.1.2  christos   if (size < sizeof (uint32_t))
   1878  1.1.1.2  christos     {
   1879  1.1.1.2  christos       einfo (_("%P: warning: truncated DEBUG_S_INLINEELINES data\n"));
   1880  1.1.1.2  christos       return false;
   1881  1.1.1.2  christos     }
   1882  1.1.1.2  christos 
   1883  1.1.1.2  christos   version = bfd_getl32 (data + sizeof (uint32_t) + sizeof (uint32_t));
   1884  1.1.1.2  christos   if (version != CV_INLINEE_SOURCE_LINE_SIGNATURE)
   1885  1.1.1.2  christos     {
   1886  1.1.1.2  christos       einfo (_("%P: warning: unexpected DEBUG_S_INLINEELINES version %u\n"),
   1887  1.1.1.2  christos 	     version);
   1888  1.1.1.2  christos       return false;
   1889  1.1.1.2  christos     }
   1890  1.1.1.2  christos 
   1891  1.1.1.2  christos   memcpy (*bufptr, data, size);
   1892  1.1.1.2  christos   ptr = *bufptr + sizeof (uint32_t);
   1893  1.1.1.2  christos   *bufptr += size;
   1894  1.1.1.2  christos 
   1895  1.1.1.2  christos   num_entries = (size - sizeof (uint32_t)) / (3 * sizeof (uint32_t));
   1896  1.1.1.2  christos 
   1897  1.1.1.2  christos   for (unsigned int i = 0; i < num_entries; i++)
   1898  1.1.1.2  christos     {
   1899  1.1.1.2  christos       uint32_t func_id;
   1900  1.1.1.2  christos 
   1901  1.1.1.2  christos       func_id = bfd_getl32 (ptr);
   1902  1.1.1.2  christos 
   1903  1.1.1.2  christos       if (!remap_type (ptr, map, func_id, num_types))
   1904  1.1.1.2  christos 	return false;
   1905  1.1.1.2  christos 
   1906  1.1.1.2  christos       ptr += 3 * sizeof (uint32_t);
   1907  1.1.1.2  christos     }
   1908  1.1.1.2  christos 
   1909  1.1.1.2  christos   return true;
   1910  1.1.1.2  christos }
   1911  1.1.1.2  christos 
   1912      1.1  christos /* Parse the .debug$S section within an object file.  */
   1913      1.1  christos static bool
   1914      1.1  christos handle_debugs_section (asection *s, bfd *mod, struct string_table *strings,
   1915      1.1  christos 		       uint8_t **dataptr, uint32_t *sizeptr,
   1916      1.1  christos 		       struct mod_source_files *mod_source,
   1917      1.1  christos 		       bfd *abfd, uint8_t **syms, uint32_t *sym_byte_size,
   1918      1.1  christos 		       struct type_entry **map, uint32_t num_types,
   1919      1.1  christos 		       bfd *sym_rec_stream, struct globals *glob,
   1920      1.1  christos 		       uint16_t mod_num)
   1921      1.1  christos {
   1922      1.1  christos   bfd_byte *data = NULL;
   1923      1.1  christos   size_t off;
   1924      1.1  christos   uint32_t c13_size = 0;
   1925      1.1  christos   char *string_table = NULL;
   1926      1.1  christos   uint8_t *buf, *bufptr, *symbuf, *symbufptr;
   1927      1.1  christos   uint32_t sym_size = 0;
   1928      1.1  christos 
   1929      1.1  christos   if (!bfd_get_full_section_contents (mod, s, &data))
   1930      1.1  christos     return false;
   1931      1.1  christos 
   1932      1.1  christos   if (!data)
   1933      1.1  christos     return false;
   1934      1.1  christos 
   1935      1.1  christos   /* Resolve relocations.  Addresses are stored within the .debug$S section as
   1936      1.1  christos      a .secidx, .secrel32 pair.  */
   1937      1.1  christos 
   1938      1.1  christos   if (s->flags & SEC_RELOC)
   1939      1.1  christos     {
   1940      1.1  christos       struct internal_reloc *relocs;
   1941      1.1  christos       struct internal_syment *symbols;
   1942      1.1  christos       asection **sectlist;
   1943      1.1  christos       unsigned int syment_count;
   1944      1.1  christos       int sect_num;
   1945      1.1  christos       struct external_syment *ext;
   1946      1.1  christos 
   1947      1.1  christos       syment_count = obj_raw_syment_count (mod);
   1948      1.1  christos 
   1949      1.1  christos       relocs =
   1950      1.1  christos 	_bfd_coff_read_internal_relocs (mod, s, false, NULL, true, NULL);
   1951      1.1  christos 
   1952      1.1  christos       symbols = xmalloc (sizeof (struct internal_syment) * syment_count);
   1953      1.1  christos       sectlist = xmalloc (sizeof (asection *) * syment_count);
   1954      1.1  christos 
   1955      1.1  christos       ext = (struct external_syment *) (coff_data (mod)->external_syms);
   1956      1.1  christos 
   1957      1.1  christos       for (unsigned int i = 0; i < syment_count; i++)
   1958      1.1  christos 	{
   1959      1.1  christos 	  bfd_coff_swap_sym_in (mod, &ext[i], &symbols[i]);
   1960      1.1  christos 	}
   1961      1.1  christos 
   1962      1.1  christos       sect_num = 1;
   1963      1.1  christos 
   1964      1.1  christos       for (asection *sect = mod->sections; sect; sect = sect->next)
   1965      1.1  christos 	{
   1966      1.1  christos 	  for (unsigned int i = 0; i < syment_count; i++)
   1967      1.1  christos 	    {
   1968      1.1  christos 	      if (symbols[i].n_scnum == sect_num)
   1969      1.1  christos 		sectlist[i] = sect;
   1970      1.1  christos 	    }
   1971      1.1  christos 
   1972      1.1  christos 	  sect_num++;
   1973      1.1  christos 	}
   1974      1.1  christos 
   1975      1.1  christos       if (!bfd_coff_relocate_section (abfd, coff_data (abfd)->link_info, mod,
   1976      1.1  christos 				      s, data, relocs, symbols, sectlist))
   1977      1.1  christos 	{
   1978      1.1  christos 	  free (sectlist);
   1979      1.1  christos 	  free (symbols);
   1980      1.1  christos 	  free (data);
   1981      1.1  christos 	  return false;
   1982      1.1  christos 	}
   1983      1.1  christos 
   1984      1.1  christos       free (sectlist);
   1985      1.1  christos       free (symbols);
   1986      1.1  christos     }
   1987      1.1  christos 
   1988      1.1  christos   if (bfd_getl32 (data) != CV_SIGNATURE_C13)
   1989      1.1  christos     {
   1990      1.1  christos       free (data);
   1991      1.1  christos       return true;
   1992      1.1  christos     }
   1993      1.1  christos 
   1994      1.1  christos   off = sizeof (uint32_t);
   1995      1.1  christos 
   1996      1.1  christos   /* calculate size */
   1997      1.1  christos 
   1998      1.1  christos   while (off + sizeof (uint32_t) <= s->size)
   1999      1.1  christos     {
   2000      1.1  christos       uint32_t type, size;
   2001      1.1  christos 
   2002      1.1  christos       type = bfd_getl32 (data + off);
   2003      1.1  christos 
   2004      1.1  christos       off += sizeof (uint32_t);
   2005      1.1  christos 
   2006      1.1  christos       if (off + sizeof (uint32_t) > s->size)
   2007      1.1  christos 	{
   2008      1.1  christos 	  free (data);
   2009      1.1  christos 	  bfd_set_error (bfd_error_bad_value);
   2010      1.1  christos 	  return false;
   2011      1.1  christos 	}
   2012      1.1  christos 
   2013      1.1  christos       size = bfd_getl32 (data + off);
   2014      1.1  christos 
   2015      1.1  christos       off += sizeof (uint32_t);
   2016      1.1  christos 
   2017      1.1  christos       if (off + size > s->size)
   2018      1.1  christos 	{
   2019      1.1  christos 	  free (data);
   2020      1.1  christos 	  bfd_set_error (bfd_error_bad_value);
   2021      1.1  christos 	  return false;
   2022      1.1  christos 	}
   2023      1.1  christos 
   2024      1.1  christos       switch (type)
   2025      1.1  christos 	{
   2026      1.1  christos 	case DEBUG_S_FILECHKSMS:
   2027  1.1.1.2  christos 	case DEBUG_S_INLINEELINES:
   2028      1.1  christos 	  c13_size += sizeof (uint32_t) + sizeof (uint32_t) + size;
   2029      1.1  christos 
   2030      1.1  christos 	  if (c13_size % sizeof (uint32_t))
   2031      1.1  christos 	    c13_size += sizeof (uint32_t) - (c13_size % sizeof (uint32_t));
   2032      1.1  christos 
   2033      1.1  christos 	  break;
   2034      1.1  christos 
   2035      1.1  christos 	case DEBUG_S_STRINGTABLE:
   2036      1.1  christos 	  parse_string_table (data + off, size, strings);
   2037      1.1  christos 
   2038      1.1  christos 	  string_table = (char *) data + off;
   2039      1.1  christos 
   2040      1.1  christos 	  break;
   2041      1.1  christos 
   2042      1.1  christos 	case DEBUG_S_LINES:
   2043      1.1  christos 	  {
   2044      1.1  christos 	    uint16_t sect;
   2045      1.1  christos 
   2046      1.1  christos 	    if (size < sizeof (uint32_t) + sizeof (uint16_t))
   2047      1.1  christos 	      {
   2048      1.1  christos 		free (data);
   2049      1.1  christos 		bfd_set_error (bfd_error_bad_value);
   2050      1.1  christos 		return false;
   2051      1.1  christos 	      }
   2052      1.1  christos 
   2053      1.1  christos 	    sect = bfd_getl16 (data + off + sizeof (uint32_t));
   2054      1.1  christos 
   2055      1.1  christos 	    /* Skip GC'd symbols.  */
   2056      1.1  christos 	    if (sect != 0)
   2057      1.1  christos 	      {
   2058      1.1  christos 		c13_size += sizeof (uint32_t) + sizeof (uint32_t) + size;
   2059      1.1  christos 
   2060      1.1  christos 		if (c13_size % sizeof (uint32_t))
   2061      1.1  christos 		  c13_size +=
   2062      1.1  christos 		    sizeof (uint32_t) - (c13_size % sizeof (uint32_t));
   2063      1.1  christos 	      }
   2064      1.1  christos 
   2065      1.1  christos 	    break;
   2066      1.1  christos 	  }
   2067      1.1  christos 
   2068      1.1  christos 	case DEBUG_S_SYMBOLS:
   2069      1.1  christos 	  if (!calculate_symbols_size (data + off, size, &sym_size))
   2070      1.1  christos 	    {
   2071      1.1  christos 	      free (data);
   2072      1.1  christos 	      bfd_set_error (bfd_error_bad_value);
   2073      1.1  christos 	      return false;
   2074      1.1  christos 	    }
   2075      1.1  christos 
   2076      1.1  christos 	  break;
   2077      1.1  christos 	}
   2078      1.1  christos 
   2079      1.1  christos       off += size;
   2080      1.1  christos 
   2081      1.1  christos       if (off % sizeof (uint32_t))
   2082      1.1  christos 	off += sizeof (uint32_t) - (off % sizeof (uint32_t));
   2083      1.1  christos     }
   2084      1.1  christos 
   2085      1.1  christos   if (sym_size % sizeof (uint32_t))
   2086      1.1  christos     sym_size += sizeof (uint32_t) - (sym_size % sizeof (uint32_t));
   2087      1.1  christos 
   2088      1.1  christos   if (c13_size == 0 && sym_size == 0)
   2089      1.1  christos     {
   2090      1.1  christos       free (data);
   2091      1.1  christos       return true;
   2092      1.1  christos     }
   2093      1.1  christos 
   2094      1.1  christos   /* copy data */
   2095      1.1  christos 
   2096      1.1  christos   buf = NULL;
   2097      1.1  christos   if (c13_size != 0)
   2098      1.1  christos     buf = xmalloc (c13_size);
   2099      1.1  christos   bufptr = buf;
   2100      1.1  christos 
   2101      1.1  christos   symbuf = NULL;
   2102      1.1  christos   if (sym_size != 0)
   2103      1.1  christos     symbuf = xmalloc (sym_size);
   2104      1.1  christos   symbufptr = symbuf;
   2105      1.1  christos 
   2106      1.1  christos   off = sizeof (uint32_t);
   2107      1.1  christos 
   2108      1.1  christos   while (off + sizeof (uint32_t) <= s->size)
   2109      1.1  christos     {
   2110      1.1  christos       uint32_t type, size;
   2111      1.1  christos 
   2112      1.1  christos       type = bfd_getl32 (data + off);
   2113      1.1  christos       off += sizeof (uint32_t);
   2114      1.1  christos 
   2115      1.1  christos       size = bfd_getl32 (data + off);
   2116      1.1  christos       off += sizeof (uint32_t);
   2117      1.1  christos 
   2118      1.1  christos       switch (type)
   2119      1.1  christos 	{
   2120      1.1  christos 	case DEBUG_S_FILECHKSMS:
   2121      1.1  christos 	  if (!copy_filechksms (data + off, size, string_table,
   2122      1.1  christos 				strings, bufptr, mod_source))
   2123      1.1  christos 	    {
   2124      1.1  christos 	      free (data);
   2125      1.1  christos 	      free (symbuf);
   2126      1.1  christos 	      return false;
   2127      1.1  christos 	    }
   2128      1.1  christos 
   2129      1.1  christos 	  bufptr += sizeof (uint32_t) + sizeof (uint32_t) + size;
   2130      1.1  christos 
   2131      1.1  christos 	  break;
   2132      1.1  christos 
   2133      1.1  christos 	case DEBUG_S_LINES:
   2134      1.1  christos 	  {
   2135      1.1  christos 	    uint16_t sect;
   2136      1.1  christos 
   2137      1.1  christos 	    sect = bfd_getl16 (data + off + sizeof (uint32_t));
   2138      1.1  christos 
   2139      1.1  christos 	    /* Skip if GC'd.  */
   2140      1.1  christos 	    if (sect != 0)
   2141      1.1  christos 	      {
   2142      1.1  christos 		bfd_putl32 (type, bufptr);
   2143      1.1  christos 		bufptr += sizeof (uint32_t);
   2144      1.1  christos 
   2145      1.1  christos 		bfd_putl32 (size, bufptr);
   2146      1.1  christos 		bufptr += sizeof (uint32_t);
   2147      1.1  christos 
   2148      1.1  christos 		memcpy (bufptr, data + off, size);
   2149      1.1  christos 		bufptr += size;
   2150      1.1  christos 	      }
   2151      1.1  christos 
   2152      1.1  christos 	    break;
   2153      1.1  christos 	  }
   2154      1.1  christos 
   2155      1.1  christos 	case DEBUG_S_SYMBOLS:
   2156      1.1  christos 	  if (!parse_symbols (data + off, size, &symbufptr, map, num_types,
   2157      1.1  christos 			      sym_rec_stream, glob, mod_num))
   2158      1.1  christos 	    {
   2159      1.1  christos 	      free (data);
   2160      1.1  christos 	      free (symbuf);
   2161      1.1  christos 	      return false;
   2162      1.1  christos 	    }
   2163      1.1  christos 
   2164      1.1  christos 	  break;
   2165  1.1.1.2  christos 
   2166  1.1.1.2  christos 	case DEBUG_S_INLINEELINES:
   2167  1.1.1.2  christos 	  if (!parse_inlinee_lines (data + off, size, &bufptr, map, num_types))
   2168  1.1.1.2  christos 	    {
   2169  1.1.1.2  christos 	      free (data);
   2170  1.1.1.2  christos 	      free (symbuf);
   2171  1.1.1.2  christos 	      return false;
   2172  1.1.1.2  christos 	    }
   2173  1.1.1.2  christos 
   2174  1.1.1.2  christos 	  break;
   2175      1.1  christos 	}
   2176      1.1  christos 
   2177      1.1  christos       off += size;
   2178      1.1  christos 
   2179      1.1  christos       if (off % sizeof (uint32_t))
   2180      1.1  christos 	off += sizeof (uint32_t) - (off % sizeof (uint32_t));
   2181      1.1  christos     }
   2182      1.1  christos 
   2183      1.1  christos   free (data);
   2184      1.1  christos 
   2185      1.1  christos   if (buf)
   2186      1.1  christos     {
   2187      1.1  christos       if (*dataptr)
   2188      1.1  christos 	{
   2189      1.1  christos 	  /* Append the C13 info to what's already there, if the module has
   2190      1.1  christos 	     multiple .debug$S sections.  */
   2191      1.1  christos 
   2192      1.1  christos 	  *dataptr = xrealloc (*dataptr, *sizeptr + c13_size);
   2193      1.1  christos 	  memcpy (*dataptr + *sizeptr, buf, c13_size);
   2194      1.1  christos 
   2195      1.1  christos 	  free (buf);
   2196      1.1  christos 	}
   2197      1.1  christos       else
   2198      1.1  christos 	{
   2199      1.1  christos 	  *dataptr = buf;
   2200      1.1  christos 	}
   2201      1.1  christos 
   2202      1.1  christos       *sizeptr += c13_size;
   2203      1.1  christos     }
   2204      1.1  christos 
   2205      1.1  christos   if (symbuf)
   2206      1.1  christos     {
   2207      1.1  christos       if (*syms)
   2208      1.1  christos 	{
   2209      1.1  christos 	  *syms = xrealloc (*syms, *sym_byte_size + sym_size);
   2210      1.1  christos 	  memcpy (*syms + *sym_byte_size, symbuf, sym_size);
   2211      1.1  christos 
   2212      1.1  christos 	  free (symbuf);
   2213      1.1  christos 	}
   2214      1.1  christos       else
   2215      1.1  christos 	{
   2216      1.1  christos 	  *syms = symbuf;
   2217      1.1  christos 	}
   2218      1.1  christos 
   2219      1.1  christos       *sym_byte_size += sym_size;
   2220      1.1  christos     }
   2221      1.1  christos 
   2222      1.1  christos   return true;
   2223      1.1  christos }
   2224      1.1  christos 
   2225      1.1  christos /* Remap the type number stored in data from the per-module numbering to
   2226      1.1  christos    that of the deduplicated output list.  */
   2227      1.1  christos static bool
   2228      1.1  christos remap_type (void *data, struct type_entry **map,
   2229      1.1  christos 	    uint32_t type_num, uint32_t num_types)
   2230      1.1  christos {
   2231      1.1  christos   uint32_t type = bfd_getl32 (data);
   2232      1.1  christos 
   2233      1.1  christos   /* Ignore builtin types (those with IDs below 0x1000).  */
   2234      1.1  christos   if (type < TPI_FIRST_INDEX)
   2235      1.1  christos     return true;
   2236      1.1  christos 
   2237      1.1  christos   if (type >= TPI_FIRST_INDEX + type_num)
   2238      1.1  christos     {
   2239      1.1  christos       einfo (_("%P: CodeView type %v references other type %v not yet "
   2240      1.1  christos 	       "declared\n"), TPI_FIRST_INDEX + type_num, type);
   2241      1.1  christos       return false;
   2242      1.1  christos     }
   2243      1.1  christos 
   2244      1.1  christos   if (type >= TPI_FIRST_INDEX + num_types)
   2245      1.1  christos     {
   2246      1.1  christos       einfo (_("%P: CodeView type %v references out of range type %v\n"),
   2247      1.1  christos 	     TPI_FIRST_INDEX + type_num, type);
   2248      1.1  christos       return false;
   2249      1.1  christos     }
   2250      1.1  christos 
   2251      1.1  christos   type = TPI_FIRST_INDEX + map[type - TPI_FIRST_INDEX]->index;
   2252      1.1  christos   bfd_putl32 (type, data);
   2253      1.1  christos 
   2254      1.1  christos   return true;
   2255      1.1  christos }
   2256      1.1  christos 
   2257      1.1  christos /* Determines whether the name of a struct, class, or union counts as
   2258      1.1  christos    "anonymous".  Non-anonymous types have a hash based on just the name,
   2259      1.1  christos    rather than the whole structure.  */
   2260      1.1  christos static bool
   2261      1.1  christos is_name_anonymous (char *name, size_t len)
   2262      1.1  christos {
   2263      1.1  christos   static const char tag1[] = "<unnamed-tag>";
   2264      1.1  christos   static const char tag2[] = "__unnamed";
   2265      1.1  christos   static const char tag3[] = "::<unnamed-tag>";
   2266      1.1  christos   static const char tag4[] = "::__unnamed";
   2267      1.1  christos 
   2268      1.1  christos   if (len == sizeof (tag1) - 1 && !memcmp (name, tag1, sizeof (tag1) - 1))
   2269      1.1  christos     return true;
   2270      1.1  christos 
   2271      1.1  christos   if (len == sizeof (tag2) - 1 && !memcmp (name, tag2, sizeof (tag2) - 1))
   2272      1.1  christos     return true;
   2273      1.1  christos 
   2274      1.1  christos   if (len >= sizeof (tag3) - 1
   2275      1.1  christos       && !memcmp (name + len - sizeof (tag3) + 1, tag3, sizeof (tag3) - 1))
   2276      1.1  christos     return true;
   2277      1.1  christos 
   2278      1.1  christos   if (len >= sizeof (tag4) - 1
   2279      1.1  christos       && !memcmp (name + len - sizeof (tag4) + 1, tag4, sizeof (tag4) - 1))
   2280      1.1  christos     return true;
   2281      1.1  christos 
   2282      1.1  christos   return false;
   2283      1.1  christos }
   2284      1.1  christos 
   2285      1.1  christos /* Handle LF_UDT_SRC_LINE type entries, which are a special case.  These
   2286      1.1  christos    give the source file and line number for each user-defined type that is
   2287      1.1  christos    declared.  We parse these and emit instead an LF_UDT_MOD_SRC_LINE entry,
   2288      1.1  christos    which also includes the module number.  */
   2289      1.1  christos static bool
   2290      1.1  christos handle_udt_src_line (uint8_t *data, uint16_t size, struct type_entry **map,
   2291      1.1  christos 		     uint32_t type_num, uint32_t num_types,
   2292      1.1  christos 		     struct types *ids, uint16_t mod_num,
   2293      1.1  christos 		     struct string_table *strings)
   2294      1.1  christos {
   2295      1.1  christos   struct lf_udt_src_line *usl = (struct lf_udt_src_line *) data;
   2296      1.1  christos   uint32_t orig_type, source_file_type;
   2297      1.1  christos   void **slot;
   2298      1.1  christos   hashval_t hash;
   2299      1.1  christos   struct type_entry *e, *type_e, *str_e;
   2300      1.1  christos   struct lf_udt_mod_src_line *umsl;
   2301      1.1  christos   struct lf_string_id *str;
   2302      1.1  christos   uint32_t source_file_offset;
   2303      1.1  christos 
   2304      1.1  christos   if (size < sizeof (struct lf_udt_src_line))
   2305      1.1  christos     {
   2306      1.1  christos       einfo (_("%P: warning: truncated CodeView type record"
   2307      1.1  christos 	       " LF_UDT_SRC_LINE\n"));
   2308      1.1  christos       return false;
   2309      1.1  christos     }
   2310      1.1  christos 
   2311      1.1  christos   /* Check if LF_UDT_MOD_SRC_LINE already present for type, and return.  */
   2312      1.1  christos 
   2313      1.1  christos   orig_type = bfd_getl32 (&usl->type);
   2314      1.1  christos 
   2315      1.1  christos   if (orig_type < TPI_FIRST_INDEX ||
   2316      1.1  christos       orig_type >= TPI_FIRST_INDEX + num_types ||
   2317      1.1  christos       !map[orig_type - TPI_FIRST_INDEX])
   2318      1.1  christos     {
   2319      1.1  christos       einfo (_("%P: warning: CodeView type record LF_UDT_SRC_LINE"
   2320      1.1  christos 	       " referred to unknown type %v\n"), orig_type);
   2321      1.1  christos       return false;
   2322      1.1  christos     }
   2323      1.1  christos 
   2324      1.1  christos   type_e = map[orig_type - TPI_FIRST_INDEX];
   2325      1.1  christos 
   2326      1.1  christos   /* Skip if type already declared in other module.  */
   2327      1.1  christos   if (type_e->has_udt_src_line)
   2328      1.1  christos     return true;
   2329      1.1  christos 
   2330      1.1  christos   if (!remap_type (&usl->type, map, type_num, num_types))
   2331      1.1  christos     return false;
   2332      1.1  christos 
   2333      1.1  christos   /* Extract string from source_file_type.  */
   2334      1.1  christos 
   2335      1.1  christos   source_file_type = bfd_getl32 (&usl->source_file_type);
   2336      1.1  christos 
   2337      1.1  christos   if (source_file_type < TPI_FIRST_INDEX ||
   2338      1.1  christos       source_file_type >= TPI_FIRST_INDEX + num_types ||
   2339      1.1  christos       !map[source_file_type - TPI_FIRST_INDEX])
   2340      1.1  christos     {
   2341      1.1  christos       einfo (_("%P: warning: CodeView type record LF_UDT_SRC_LINE"
   2342      1.1  christos 	       " referred to unknown string %v\n"), source_file_type);
   2343      1.1  christos       return false;
   2344      1.1  christos     }
   2345      1.1  christos 
   2346      1.1  christos   str_e = map[source_file_type - TPI_FIRST_INDEX];
   2347      1.1  christos 
   2348      1.1  christos   if (bfd_getl16 (str_e->data + sizeof (uint16_t)) != LF_STRING_ID)
   2349      1.1  christos     {
   2350      1.1  christos       einfo (_("%P: warning: CodeView type record LF_UDT_SRC_LINE"
   2351      1.1  christos 	       " pointed to unexpected record type\n"));
   2352      1.1  christos       return false;
   2353      1.1  christos     }
   2354      1.1  christos 
   2355      1.1  christos   str = (struct lf_string_id *) str_e->data;
   2356      1.1  christos 
   2357      1.1  christos   /* Add string to string table.  */
   2358      1.1  christos 
   2359      1.1  christos   source_file_offset = add_string (str->string, strlen (str->string),
   2360      1.1  christos 				   strings);
   2361      1.1  christos 
   2362      1.1  christos   /* Add LF_UDT_MOD_SRC_LINE entry.  */
   2363      1.1  christos 
   2364      1.1  christos   size = sizeof (struct lf_udt_mod_src_line);
   2365      1.1  christos 
   2366      1.1  christos   e = xmalloc (offsetof (struct type_entry, data) + size);
   2367      1.1  christos 
   2368      1.1  christos   e->next = NULL;
   2369      1.1  christos   e->index = ids->num_types;
   2370      1.1  christos   e->has_udt_src_line = false;
   2371      1.1  christos 
   2372      1.1  christos   /* LF_UDT_MOD_SRC_LINE use calc_hash on the type number, rather than
   2373      1.1  christos      the crc32 used for type hashes elsewhere.  */
   2374      1.1  christos   e->cv_hash = calc_hash ((char *) &usl->type, sizeof (uint32_t));
   2375      1.1  christos 
   2376      1.1  christos   type_e->has_udt_src_line = true;
   2377      1.1  christos 
   2378      1.1  christos   umsl = (struct lf_udt_mod_src_line *) e->data;
   2379      1.1  christos 
   2380      1.1  christos   bfd_putl16 (size - sizeof (uint16_t), &umsl->size);
   2381      1.1  christos   bfd_putl16 (LF_UDT_MOD_SRC_LINE, &umsl->kind);
   2382      1.1  christos   memcpy (&umsl->type, &usl->type, sizeof (uint32_t));
   2383      1.1  christos   bfd_putl32 (source_file_offset, &umsl->source_file_string);
   2384      1.1  christos   memcpy (&umsl->line_no, &usl->line_no, sizeof (uint32_t));
   2385      1.1  christos   bfd_putl16 (mod_num + 1, &umsl->module_no);
   2386      1.1  christos 
   2387      1.1  christos   hash = iterative_hash (e->data, size, 0);
   2388      1.1  christos 
   2389      1.1  christos   slot = htab_find_slot_with_hash (ids->hashmap, data, hash, INSERT);
   2390      1.1  christos   if (!slot)
   2391      1.1  christos     {
   2392      1.1  christos       free (e);
   2393      1.1  christos       return false;
   2394      1.1  christos     }
   2395      1.1  christos 
   2396      1.1  christos   if (*slot)
   2397      1.1  christos     {
   2398      1.1  christos       free (e);
   2399      1.1  christos       einfo (_("%P: warning: duplicate CodeView type record "
   2400      1.1  christos 	       "LF_UDT_MOD_SRC_LINE\n"));
   2401      1.1  christos       return false;
   2402      1.1  christos     }
   2403      1.1  christos 
   2404      1.1  christos   *slot = e;
   2405      1.1  christos 
   2406      1.1  christos   if (ids->last)
   2407      1.1  christos     ids->last->next = e;
   2408      1.1  christos   else
   2409      1.1  christos     ids->first = e;
   2410      1.1  christos 
   2411      1.1  christos   ids->last = e;
   2412      1.1  christos 
   2413      1.1  christos   map[type_num] = e;
   2414      1.1  christos 
   2415      1.1  christos   ids->num_types++;
   2416      1.1  christos 
   2417      1.1  christos   return true;
   2418      1.1  christos }
   2419      1.1  christos 
   2420      1.1  christos /* Parse a type definition in the .debug$T section.  We remap the numbers
   2421      1.1  christos    of any referenced types, and if the type is not a duplicate of one
   2422      1.1  christos    already seen add it to types (for TPI types) or ids (for IPI types).  */
   2423      1.1  christos static bool
   2424      1.1  christos handle_type (uint8_t *data, struct type_entry **map, uint32_t type_num,
   2425      1.1  christos 	     uint32_t num_types, struct types *types,
   2426      1.1  christos 	     struct types *ids, uint16_t mod_num,
   2427      1.1  christos 	     struct string_table *strings)
   2428      1.1  christos {
   2429      1.1  christos   uint16_t size, type;
   2430      1.1  christos   void **slot;
   2431      1.1  christos   hashval_t hash;
   2432      1.1  christos   bool other_hash = false;
   2433      1.1  christos   uint32_t cv_hash;
   2434      1.1  christos   struct types *t;
   2435      1.1  christos   bool ipi = false;
   2436      1.1  christos 
   2437      1.1  christos   size = bfd_getl16 (data) + sizeof (uint16_t);
   2438      1.1  christos   type = bfd_getl16 (data + sizeof (uint16_t));
   2439      1.1  christos 
   2440      1.1  christos   switch (type)
   2441      1.1  christos     {
   2442      1.1  christos     case LF_MODIFIER:
   2443      1.1  christos       {
   2444      1.1  christos 	struct lf_modifier *mod = (struct lf_modifier *) data;
   2445      1.1  christos 
   2446      1.1  christos 	if (size < offsetof (struct lf_modifier, modifier))
   2447      1.1  christos 	  {
   2448      1.1  christos 	    einfo (_("%P: warning: truncated CodeView type record "
   2449      1.1  christos 		     "LF_MODIFIER\n"));
   2450      1.1  christos 	    return false;
   2451      1.1  christos 	  }
   2452      1.1  christos 
   2453      1.1  christos 	if (!remap_type (&mod->base_type, map, type_num, num_types))
   2454      1.1  christos 	  return false;
   2455      1.1  christos 
   2456      1.1  christos 	break;
   2457      1.1  christos       }
   2458      1.1  christos 
   2459      1.1  christos     case LF_POINTER:
   2460      1.1  christos       {
   2461      1.1  christos 	struct lf_pointer *ptr = (struct lf_pointer *) data;
   2462  1.1.1.2  christos 	uint32_t attributes;
   2463      1.1  christos 
   2464      1.1  christos 	if (size < offsetof (struct lf_pointer, attributes))
   2465      1.1  christos 	  {
   2466      1.1  christos 	    einfo (_("%P: warning: truncated CodeView type record"
   2467      1.1  christos 		     " LF_POINTER\n"));
   2468      1.1  christos 	    return false;
   2469      1.1  christos 	  }
   2470      1.1  christos 
   2471      1.1  christos 	if (!remap_type (&ptr->base_type, map, type_num, num_types))
   2472      1.1  christos 	  return false;
   2473      1.1  christos 
   2474  1.1.1.2  christos 	attributes = bfd_getl32 (&ptr->attributes);
   2475  1.1.1.2  christos 
   2476  1.1.1.2  christos 	if ((attributes & CV_PTR_MODE_MASK) == CV_PTR_MODE_PMEM
   2477  1.1.1.2  christos 	    || (attributes & CV_PTR_MODE_MASK) == CV_PTR_MODE_PMFUNC)
   2478  1.1.1.2  christos 	  {
   2479  1.1.1.2  christos 	    if (size < offsetof (struct lf_pointer, ptr_to_mem_type))
   2480  1.1.1.2  christos 	      {
   2481  1.1.1.2  christos 		einfo (_("%P: warning: truncated CodeView type record"
   2482  1.1.1.2  christos 			 " LF_POINTER\n"));
   2483  1.1.1.2  christos 		return false;
   2484  1.1.1.2  christos 	      }
   2485  1.1.1.2  christos 
   2486  1.1.1.2  christos 	    if (!remap_type (&ptr->containing_class, map, type_num, num_types))
   2487  1.1.1.2  christos 	      return false;
   2488  1.1.1.2  christos 	  }
   2489  1.1.1.2  christos 
   2490      1.1  christos 	break;
   2491      1.1  christos       }
   2492      1.1  christos 
   2493      1.1  christos     case LF_PROCEDURE:
   2494      1.1  christos       {
   2495      1.1  christos 	struct lf_procedure *proc = (struct lf_procedure *) data;
   2496      1.1  christos 
   2497      1.1  christos 	if (size < sizeof (struct lf_procedure))
   2498      1.1  christos 	  {
   2499      1.1  christos 	    einfo (_("%P: warning: truncated CodeView type record"
   2500      1.1  christos 		     " LF_PROCEDURE\n"));
   2501      1.1  christos 	    return false;
   2502      1.1  christos 	  }
   2503      1.1  christos 
   2504      1.1  christos 	if (!remap_type (&proc->return_type, map, type_num, num_types))
   2505      1.1  christos 	  return false;
   2506      1.1  christos 
   2507      1.1  christos 	if (!remap_type (&proc->arglist, map, type_num, num_types))
   2508      1.1  christos 	  return false;
   2509      1.1  christos 
   2510      1.1  christos 	break;
   2511      1.1  christos       }
   2512      1.1  christos 
   2513      1.1  christos     case LF_MFUNCTION:
   2514      1.1  christos       {
   2515      1.1  christos 	struct lf_mfunction *func = (struct lf_mfunction *) data;
   2516      1.1  christos 
   2517      1.1  christos 	if (size < sizeof (struct lf_procedure))
   2518      1.1  christos 	  {
   2519      1.1  christos 	    einfo (_("%P: warning: truncated CodeView type record"
   2520      1.1  christos 		     " LF_MFUNCTION\n"));
   2521      1.1  christos 	    return false;
   2522      1.1  christos 	  }
   2523      1.1  christos 
   2524      1.1  christos 	if (!remap_type (&func->return_type, map, type_num, num_types))
   2525      1.1  christos 	  return false;
   2526      1.1  christos 
   2527      1.1  christos 	if (!remap_type (&func->containing_class_type, map, type_num,
   2528      1.1  christos 			 num_types))
   2529      1.1  christos 	  return false;
   2530      1.1  christos 
   2531      1.1  christos 	if (!remap_type (&func->this_type, map, type_num, num_types))
   2532      1.1  christos 	  return false;
   2533      1.1  christos 
   2534      1.1  christos 	if (!remap_type (&func->arglist, map, type_num, num_types))
   2535      1.1  christos 	  return false;
   2536      1.1  christos 
   2537      1.1  christos 	break;
   2538      1.1  christos       }
   2539      1.1  christos 
   2540      1.1  christos     case LF_ARGLIST:
   2541      1.1  christos       {
   2542      1.1  christos 	uint32_t num_entries;
   2543      1.1  christos 	struct lf_arglist *al = (struct lf_arglist *) data;
   2544      1.1  christos 
   2545      1.1  christos 	if (size < offsetof (struct lf_arglist, args))
   2546      1.1  christos 	  {
   2547      1.1  christos 	    einfo (_("%P: warning: truncated CodeView type record"
   2548      1.1  christos 		     " LF_ARGLIST\n"));
   2549      1.1  christos 	    return false;
   2550      1.1  christos 	  }
   2551      1.1  christos 
   2552      1.1  christos 	num_entries = bfd_getl32 (&al->num_entries);
   2553      1.1  christos 
   2554      1.1  christos 	if (size < offsetof (struct lf_arglist, args)
   2555      1.1  christos 		   + (num_entries * sizeof (uint32_t)))
   2556      1.1  christos 	  {
   2557      1.1  christos 	    einfo (_("%P: warning: truncated CodeView type record"
   2558      1.1  christos 		     " LF_ARGLIST\n"));
   2559      1.1  christos 	    return false;
   2560      1.1  christos 	  }
   2561      1.1  christos 
   2562      1.1  christos 	for (uint32_t i = 0; i < num_entries; i++)
   2563      1.1  christos 	  {
   2564      1.1  christos 	    if (!remap_type (&al->args[i], map, type_num, num_types))
   2565      1.1  christos 	      return false;
   2566      1.1  christos 	  }
   2567      1.1  christos 
   2568      1.1  christos 	break;
   2569      1.1  christos       }
   2570      1.1  christos 
   2571      1.1  christos     case LF_FIELDLIST:
   2572      1.1  christos       {
   2573      1.1  christos 	uint16_t left = size - sizeof (uint16_t) - sizeof (uint16_t);
   2574      1.1  christos 	uint8_t *ptr = data + sizeof (uint16_t) + sizeof (uint16_t);
   2575      1.1  christos 
   2576      1.1  christos 	while (left > 0)
   2577      1.1  christos 	  {
   2578      1.1  christos 	    uint16_t subtype;
   2579      1.1  christos 
   2580      1.1  christos 	    if (left < sizeof (uint16_t))
   2581      1.1  christos 	      {
   2582      1.1  christos 		einfo (_("%P: warning: truncated CodeView type record"
   2583      1.1  christos 			 " LF_FIELDLIST\n"));
   2584      1.1  christos 		return false;
   2585      1.1  christos 	      }
   2586      1.1  christos 
   2587      1.1  christos 	    subtype = bfd_getl16 (ptr);
   2588      1.1  christos 
   2589      1.1  christos 	    switch (subtype)
   2590      1.1  christos 	      {
   2591      1.1  christos 	      case LF_MEMBER:
   2592      1.1  christos 		{
   2593      1.1  christos 		  struct lf_member *mem = (struct lf_member *) ptr;
   2594      1.1  christos 		  uint16_t offset;
   2595      1.1  christos 		  size_t name_len, subtype_len;
   2596      1.1  christos 
   2597      1.1  christos 		  if (left < offsetof (struct lf_member, name))
   2598      1.1  christos 		    {
   2599      1.1  christos 		      einfo (_("%P: warning: truncated CodeView type record"
   2600      1.1  christos 			       " LF_MEMBER\n"));
   2601      1.1  christos 		      return false;
   2602      1.1  christos 		    }
   2603      1.1  christos 
   2604      1.1  christos 		  if (!remap_type (&mem->type, map, type_num, num_types))
   2605      1.1  christos 		    return false;
   2606      1.1  christos 
   2607      1.1  christos 		  subtype_len = offsetof (struct lf_member, name);
   2608      1.1  christos 
   2609      1.1  christos 		  offset = bfd_getl16 (&mem->offset);
   2610      1.1  christos 
   2611      1.1  christos 		  /* If offset >= 0x8000, actual value follows.  */
   2612      1.1  christos 		  if (offset >= 0x8000)
   2613      1.1  christos 		    {
   2614      1.1  christos 		      unsigned int param_len = extended_value_len (offset);
   2615      1.1  christos 
   2616      1.1  christos 		      if (param_len == 0)
   2617      1.1  christos 			{
   2618      1.1  christos 			  einfo (_("%P: warning: unhandled type %v within"
   2619      1.1  christos 				   " LF_MEMBER\n"), offset);
   2620      1.1  christos 			  return false;
   2621      1.1  christos 			}
   2622      1.1  christos 
   2623      1.1  christos 		      subtype_len += param_len;
   2624      1.1  christos 
   2625      1.1  christos 		      if (left < subtype_len)
   2626      1.1  christos 			{
   2627      1.1  christos 			  einfo (_("%P: warning: truncated CodeView type record"
   2628      1.1  christos 				  " LF_MEMBER\n"));
   2629      1.1  christos 			  return false;
   2630      1.1  christos 			}
   2631      1.1  christos 		    }
   2632      1.1  christos 
   2633      1.1  christos 		  name_len =
   2634      1.1  christos 		    strnlen ((char *) mem + subtype_len, left - subtype_len);
   2635      1.1  christos 
   2636      1.1  christos 		  if (name_len == left - offsetof (struct lf_member, name))
   2637      1.1  christos 		    {
   2638      1.1  christos 		      einfo (_("%P: warning: name for LF_MEMBER has no"
   2639      1.1  christos 			       " terminating zero\n"));
   2640      1.1  christos 		      return false;
   2641      1.1  christos 		    }
   2642      1.1  christos 
   2643      1.1  christos 		  name_len++;
   2644      1.1  christos 
   2645      1.1  christos 		  subtype_len += name_len;
   2646      1.1  christos 
   2647      1.1  christos 		  if (subtype_len % 4 != 0)
   2648      1.1  christos 		    subtype_len += 4 - (subtype_len % 4);
   2649      1.1  christos 
   2650      1.1  christos 		  if (left < subtype_len)
   2651      1.1  christos 		    {
   2652      1.1  christos 		      einfo (_("%P: warning: truncated CodeView type record"
   2653      1.1  christos 			       " LF_FIELDLIST\n"));
   2654      1.1  christos 		      return false;
   2655      1.1  christos 		    }
   2656      1.1  christos 
   2657      1.1  christos 		  ptr += subtype_len;
   2658      1.1  christos 		  left -= subtype_len;
   2659      1.1  christos 
   2660      1.1  christos 		  break;
   2661      1.1  christos 		}
   2662      1.1  christos 
   2663      1.1  christos 	      case LF_ENUMERATE:
   2664      1.1  christos 		{
   2665      1.1  christos 		  struct lf_enumerate *en = (struct lf_enumerate *) ptr;
   2666      1.1  christos 		  size_t name_len, subtype_len;
   2667      1.1  christos 		  uint16_t val;
   2668      1.1  christos 
   2669      1.1  christos 		  if (left < offsetof (struct lf_enumerate, name))
   2670      1.1  christos 		    {
   2671      1.1  christos 		      einfo (_("%P: warning: truncated CodeView type record"
   2672      1.1  christos 			       " LF_ENUMERATE\n"));
   2673      1.1  christos 		      return false;
   2674      1.1  christos 		    }
   2675      1.1  christos 
   2676      1.1  christos 		  subtype_len = offsetof (struct lf_enumerate, name);
   2677      1.1  christos 
   2678      1.1  christos 		  val = bfd_getl16 (&en->value);
   2679      1.1  christos 
   2680      1.1  christos 		  /* If val >= 0x8000, the actual value immediately follows.  */
   2681      1.1  christos 		  if (val >= 0x8000)
   2682      1.1  christos 		    {
   2683      1.1  christos 		      unsigned int param_len = extended_value_len (val);
   2684      1.1  christos 
   2685      1.1  christos 		      if (param_len == 0)
   2686      1.1  christos 			{
   2687      1.1  christos 			  einfo (_("%P: warning: unhandled type %v within"
   2688      1.1  christos 				   " LF_ENUMERATE\n"), val);
   2689      1.1  christos 			  return false;
   2690      1.1  christos 			}
   2691      1.1  christos 
   2692      1.1  christos 		      if (left < subtype_len + param_len)
   2693      1.1  christos 			{
   2694      1.1  christos 			  einfo (_("%P: warning: truncated CodeView type"
   2695      1.1  christos 				   " record LF_ENUMERATE\n"));
   2696      1.1  christos 			  return false;
   2697      1.1  christos 			}
   2698      1.1  christos 
   2699      1.1  christos 		      subtype_len += param_len;
   2700      1.1  christos 		    }
   2701      1.1  christos 
   2702      1.1  christos 		  name_len = strnlen ((char *) ptr + subtype_len,
   2703      1.1  christos 				      left - subtype_len);
   2704      1.1  christos 
   2705      1.1  christos 		  if (name_len == left - offsetof (struct lf_enumerate, name))
   2706      1.1  christos 		    {
   2707      1.1  christos 		      einfo (_("%P: warning: name for LF_ENUMERATE has no"
   2708      1.1  christos 			       " terminating zero\n"));
   2709      1.1  christos 		      return false;
   2710      1.1  christos 		    }
   2711      1.1  christos 
   2712      1.1  christos 		  name_len++;
   2713      1.1  christos 
   2714      1.1  christos 		  subtype_len += name_len;
   2715      1.1  christos 
   2716      1.1  christos 		  if (subtype_len % 4 != 0)
   2717      1.1  christos 		    subtype_len += 4 - (subtype_len % 4);
   2718      1.1  christos 
   2719      1.1  christos 		  if (left < subtype_len)
   2720      1.1  christos 		    {
   2721      1.1  christos 		      einfo (_("%P: warning: truncated CodeView type record"
   2722      1.1  christos 			       " LF_ENUMERATE\n"));
   2723      1.1  christos 		      return false;
   2724      1.1  christos 		    }
   2725      1.1  christos 
   2726      1.1  christos 		  ptr += subtype_len;
   2727      1.1  christos 		  left -= subtype_len;
   2728      1.1  christos 
   2729      1.1  christos 		  break;
   2730      1.1  christos 		}
   2731      1.1  christos 
   2732      1.1  christos 	      case LF_INDEX:
   2733      1.1  christos 		{
   2734      1.1  christos 		  struct lf_index *ind = (struct lf_index *) ptr;
   2735      1.1  christos 
   2736      1.1  christos 		  if (left < sizeof (struct lf_index))
   2737      1.1  christos 		    {
   2738      1.1  christos 		      einfo (_("%P: warning: truncated CodeView type record"
   2739      1.1  christos 			       " LF_INDEX\n"));
   2740      1.1  christos 		      return false;
   2741      1.1  christos 		    }
   2742      1.1  christos 
   2743      1.1  christos 		  if (!remap_type (&ind->index, map, type_num, num_types))
   2744      1.1  christos 		    return false;
   2745      1.1  christos 
   2746      1.1  christos 		  ptr += sizeof (struct lf_index);
   2747      1.1  christos 		  left -= sizeof (struct lf_index);
   2748      1.1  christos 
   2749      1.1  christos 		  break;
   2750      1.1  christos 		}
   2751      1.1  christos 
   2752      1.1  christos 	      case LF_ONEMETHOD:
   2753      1.1  christos 		{
   2754      1.1  christos 		  struct lf_onemethod *meth = (struct lf_onemethod *) ptr;
   2755      1.1  christos 		  size_t name_len, subtype_len;
   2756      1.1  christos 
   2757      1.1  christos 		  if (left < offsetof (struct lf_onemethod, name))
   2758      1.1  christos 		    {
   2759      1.1  christos 		      einfo (_("%P: warning: truncated CodeView type record"
   2760      1.1  christos 			       " LF_ONEMETHOD\n"));
   2761      1.1  christos 		      return false;
   2762      1.1  christos 		    }
   2763      1.1  christos 
   2764      1.1  christos 		  if (!remap_type (&meth->method_type, map, type_num,
   2765      1.1  christos 				   num_types))
   2766      1.1  christos 		    return false;
   2767      1.1  christos 
   2768      1.1  christos 		  name_len =
   2769      1.1  christos 		    strnlen (meth->name,
   2770      1.1  christos 			     left - offsetof (struct lf_onemethod, name));
   2771      1.1  christos 
   2772      1.1  christos 		  if (name_len == left - offsetof (struct lf_onemethod, name))
   2773      1.1  christos 		    {
   2774      1.1  christos 		      einfo (_("%P: warning: name for LF_ONEMETHOD has no"
   2775      1.1  christos 			       " terminating zero\n"));
   2776      1.1  christos 		      return false;
   2777      1.1  christos 		    }
   2778      1.1  christos 
   2779      1.1  christos 		  name_len++;
   2780      1.1  christos 
   2781      1.1  christos 		  subtype_len = offsetof (struct lf_onemethod, name)
   2782      1.1  christos 				+ name_len;
   2783      1.1  christos 
   2784      1.1  christos 		  if (subtype_len % 4 != 0)
   2785      1.1  christos 		    subtype_len += 4 - (subtype_len % 4);
   2786      1.1  christos 
   2787      1.1  christos 		  if (left < subtype_len)
   2788      1.1  christos 		    {
   2789      1.1  christos 		      einfo (_("%P: warning: truncated CodeView type record"
   2790      1.1  christos 			       " LF_FIELDLIST\n"));
   2791      1.1  christos 		      return false;
   2792      1.1  christos 		    }
   2793      1.1  christos 
   2794      1.1  christos 		  ptr += subtype_len;
   2795      1.1  christos 		  left -= subtype_len;
   2796      1.1  christos 
   2797      1.1  christos 		  break;
   2798      1.1  christos 		}
   2799      1.1  christos 
   2800      1.1  christos 	      case LF_METHOD:
   2801      1.1  christos 		{
   2802      1.1  christos 		  struct lf_method *meth = (struct lf_method *) ptr;
   2803      1.1  christos 		  size_t name_len, subtype_len;
   2804      1.1  christos 
   2805      1.1  christos 		  if (left < offsetof (struct lf_method, name))
   2806      1.1  christos 		    {
   2807      1.1  christos 		      einfo (_("%P: warning: truncated CodeView type record"
   2808      1.1  christos 			       " LF_METHOD\n"));
   2809      1.1  christos 		      return false;
   2810      1.1  christos 		    }
   2811      1.1  christos 
   2812      1.1  christos 		  if (!remap_type (&meth->method_list, map, type_num,
   2813      1.1  christos 				   num_types))
   2814      1.1  christos 		    return false;
   2815      1.1  christos 
   2816      1.1  christos 		  name_len =
   2817      1.1  christos 		    strnlen (meth->name,
   2818      1.1  christos 			     left - offsetof (struct lf_method, name));
   2819      1.1  christos 
   2820      1.1  christos 		  if (name_len == left - offsetof (struct lf_method, name))
   2821      1.1  christos 		    {
   2822      1.1  christos 		      einfo (_("%P: warning: name for LF_METHOD has no"
   2823      1.1  christos 			       " terminating zero\n"));
   2824      1.1  christos 		      return false;
   2825      1.1  christos 		    }
   2826      1.1  christos 
   2827      1.1  christos 		  name_len++;
   2828      1.1  christos 
   2829      1.1  christos 		  subtype_len = offsetof (struct lf_method, name) + name_len;
   2830      1.1  christos 
   2831      1.1  christos 		  if (subtype_len % 4 != 0)
   2832      1.1  christos 		    subtype_len += 4 - (subtype_len % 4);
   2833      1.1  christos 
   2834      1.1  christos 		  if (left < subtype_len)
   2835      1.1  christos 		    {
   2836      1.1  christos 		      einfo (_("%P: warning: truncated CodeView type record"
   2837      1.1  christos 			       " LF_FIELDLIST\n"));
   2838      1.1  christos 		      return false;
   2839      1.1  christos 		    }
   2840      1.1  christos 
   2841      1.1  christos 		  ptr += subtype_len;
   2842      1.1  christos 		  left -= subtype_len;
   2843      1.1  christos 
   2844      1.1  christos 		  break;
   2845      1.1  christos 		}
   2846      1.1  christos 
   2847      1.1  christos 	      case LF_BCLASS:
   2848      1.1  christos 		{
   2849      1.1  christos 		  struct lf_bclass *bc = (struct lf_bclass *) ptr;
   2850      1.1  christos 		  size_t subtype_len;
   2851      1.1  christos 		  uint16_t offset;
   2852      1.1  christos 
   2853      1.1  christos 		  if (left < sizeof (struct lf_bclass))
   2854      1.1  christos 		    {
   2855      1.1  christos 		      einfo (_("%P: warning: truncated CodeView type record"
   2856      1.1  christos 			       " LF_BCLASS\n"));
   2857      1.1  christos 		      return false;
   2858      1.1  christos 		    }
   2859      1.1  christos 
   2860      1.1  christos 		  if (!remap_type (&bc->base_class_type, map, type_num,
   2861      1.1  christos 				   num_types))
   2862      1.1  christos 		    return false;
   2863      1.1  christos 
   2864      1.1  christos 		  subtype_len = sizeof (struct lf_bclass);
   2865      1.1  christos 
   2866      1.1  christos 		  offset = bfd_getl16 (&bc->offset);
   2867      1.1  christos 
   2868      1.1  christos 		  /* If offset >= 0x8000, actual value follows.  */
   2869      1.1  christos 		  if (offset >= 0x8000)
   2870      1.1  christos 		    {
   2871      1.1  christos 		      unsigned int param_len = extended_value_len (offset);
   2872      1.1  christos 
   2873      1.1  christos 		      if (param_len == 0)
   2874      1.1  christos 			{
   2875      1.1  christos 			  einfo (_("%P: warning: unhandled type %v within"
   2876      1.1  christos 				   " LF_BCLASS\n"), offset);
   2877      1.1  christos 			  return false;
   2878      1.1  christos 			}
   2879      1.1  christos 
   2880      1.1  christos 		      subtype_len += param_len;
   2881      1.1  christos 
   2882      1.1  christos 		      if (left < subtype_len)
   2883      1.1  christos 			{
   2884      1.1  christos 			  einfo (_("%P: warning: truncated CodeView type record"
   2885      1.1  christos 				   " LF_BCLASS\n"));
   2886      1.1  christos 			  return false;
   2887      1.1  christos 			}
   2888      1.1  christos 		    }
   2889      1.1  christos 
   2890      1.1  christos 		  if (subtype_len % 4 != 0)
   2891      1.1  christos 		    subtype_len += 4 - (subtype_len % 4);
   2892      1.1  christos 
   2893      1.1  christos 		  if (left < subtype_len)
   2894      1.1  christos 		    {
   2895      1.1  christos 		      einfo (_("%P: warning: truncated CodeView type record"
   2896      1.1  christos 			       " LF_BCLASS\n"));
   2897      1.1  christos 		      return false;
   2898      1.1  christos 		    }
   2899      1.1  christos 
   2900      1.1  christos 		  ptr += subtype_len;
   2901      1.1  christos 		  left -= subtype_len;
   2902      1.1  christos 
   2903      1.1  christos 		  break;
   2904      1.1  christos 		}
   2905      1.1  christos 
   2906      1.1  christos 	      case LF_VFUNCTAB:
   2907      1.1  christos 		{
   2908      1.1  christos 		  struct lf_vfunctab *vft = (struct lf_vfunctab *) ptr;
   2909      1.1  christos 
   2910      1.1  christos 		  if (left < sizeof (struct lf_vfunctab))
   2911      1.1  christos 		    {
   2912      1.1  christos 		      einfo (_("%P: warning: truncated CodeView type record"
   2913      1.1  christos 			       " LF_VFUNCTAB\n"));
   2914      1.1  christos 		      return false;
   2915      1.1  christos 		    }
   2916      1.1  christos 
   2917      1.1  christos 		  if (!remap_type (&vft->type, map, type_num, num_types))
   2918      1.1  christos 		    return false;
   2919      1.1  christos 
   2920      1.1  christos 		  ptr += sizeof (struct lf_vfunctab);
   2921      1.1  christos 		  left -= sizeof (struct lf_vfunctab);
   2922      1.1  christos 
   2923      1.1  christos 		  break;
   2924      1.1  christos 		}
   2925      1.1  christos 
   2926      1.1  christos 	      case LF_VBCLASS:
   2927      1.1  christos 	      case LF_IVBCLASS:
   2928      1.1  christos 		{
   2929      1.1  christos 		  struct lf_vbclass *vbc = (struct lf_vbclass *) ptr;
   2930      1.1  christos 		  size_t subtype_len;
   2931      1.1  christos 		  uint16_t offset;
   2932      1.1  christos 
   2933      1.1  christos 		  if (left < sizeof (struct lf_vbclass))
   2934      1.1  christos 		    {
   2935      1.1  christos 		      einfo (_("%P: warning: truncated CodeView type record"
   2936      1.1  christos 			       " LF_VBCLASS/LF_IVBCLASS\n"));
   2937      1.1  christos 		      return false;
   2938      1.1  christos 		    }
   2939      1.1  christos 
   2940      1.1  christos 		  if (!remap_type (&vbc->base_class_type, map, type_num,
   2941      1.1  christos 				   num_types))
   2942      1.1  christos 		    return false;
   2943      1.1  christos 
   2944      1.1  christos 		  if (!remap_type (&vbc->virtual_base_pointer_type, map,
   2945      1.1  christos 				   type_num, num_types))
   2946      1.1  christos 		    return false;
   2947      1.1  christos 
   2948      1.1  christos 		  subtype_len = offsetof (struct lf_vbclass,
   2949      1.1  christos 					  virtual_base_vbtable_offset);
   2950      1.1  christos 
   2951      1.1  christos 		  offset = bfd_getl16 (&vbc->virtual_base_pointer_offset);
   2952      1.1  christos 
   2953      1.1  christos 		  /* If offset >= 0x8000, actual value follows.  */
   2954      1.1  christos 		  if (offset >= 0x8000)
   2955      1.1  christos 		    {
   2956      1.1  christos 		      unsigned int param_len = extended_value_len (offset);
   2957      1.1  christos 
   2958      1.1  christos 		      if (param_len == 0)
   2959      1.1  christos 			{
   2960      1.1  christos 			  einfo (_("%P: warning: unhandled type %v within"
   2961      1.1  christos 				   " LF_VBCLASS/LF_IVBCLASS\n"), offset);
   2962      1.1  christos 			  return false;
   2963      1.1  christos 			}
   2964      1.1  christos 
   2965      1.1  christos 		      subtype_len += param_len;
   2966      1.1  christos 
   2967      1.1  christos 		      if (left < subtype_len)
   2968      1.1  christos 			{
   2969      1.1  christos 			  einfo (_("%P: warning: truncated CodeView type record"
   2970      1.1  christos 				   " LF_VBCLASS/LF_IVBCLASS\n"));
   2971      1.1  christos 			  return false;
   2972      1.1  christos 			}
   2973      1.1  christos 		    }
   2974      1.1  christos 
   2975      1.1  christos 		  offset = bfd_getl16 ((char *)vbc + subtype_len);
   2976      1.1  christos 		  subtype_len += sizeof (uint16_t);
   2977      1.1  christos 
   2978      1.1  christos 		  /* If offset >= 0x8000, actual value follows.  */
   2979      1.1  christos 		  if (offset >= 0x8000)
   2980      1.1  christos 		    {
   2981      1.1  christos 		      unsigned int param_len = extended_value_len (offset);
   2982      1.1  christos 
   2983      1.1  christos 		      if (param_len == 0)
   2984      1.1  christos 			{
   2985      1.1  christos 			  einfo (_("%P: warning: unhandled type %v within"
   2986      1.1  christos 				   " LF_VBCLASS/LF_IVBCLASS\n"), offset);
   2987      1.1  christos 			  return false;
   2988      1.1  christos 			}
   2989      1.1  christos 
   2990      1.1  christos 		      subtype_len += param_len;
   2991      1.1  christos 
   2992      1.1  christos 		      if (left < subtype_len)
   2993      1.1  christos 			{
   2994      1.1  christos 			  einfo (_("%P: warning: truncated CodeView type record"
   2995      1.1  christos 				   " LF_VBCLASS/LF_IVBCLASS\n"));
   2996      1.1  christos 			  return false;
   2997      1.1  christos 			}
   2998      1.1  christos 		    }
   2999      1.1  christos 
   3000      1.1  christos 		  if (subtype_len % 4 != 0)
   3001      1.1  christos 		    subtype_len += 4 - (subtype_len % 4);
   3002      1.1  christos 
   3003      1.1  christos 		  if (left < subtype_len)
   3004      1.1  christos 		    {
   3005      1.1  christos 		      einfo (_("%P: warning: truncated CodeView type record"
   3006      1.1  christos 			       " LF_VBCLASS/LF_IVBCLASS\n"));
   3007      1.1  christos 		      return false;
   3008      1.1  christos 		    }
   3009      1.1  christos 
   3010      1.1  christos 		  ptr += subtype_len;
   3011      1.1  christos 		  left -= subtype_len;
   3012      1.1  christos 
   3013      1.1  christos 		  break;
   3014      1.1  christos 		}
   3015      1.1  christos 
   3016      1.1  christos 	      case LF_STMEMBER:
   3017      1.1  christos 		{
   3018      1.1  christos 		  struct lf_static_member *st =
   3019      1.1  christos 		    (struct lf_static_member *) ptr;
   3020      1.1  christos 		  size_t name_len, subtype_len;
   3021      1.1  christos 
   3022      1.1  christos 		  if (left < offsetof (struct lf_static_member, name))
   3023      1.1  christos 		    {
   3024      1.1  christos 		      einfo (_("%P: warning: truncated CodeView type record"
   3025      1.1  christos 			       " LF_STMEMBER\n"));
   3026      1.1  christos 		      return false;
   3027      1.1  christos 		    }
   3028      1.1  christos 
   3029      1.1  christos 		  if (!remap_type (&st->type, map, type_num, num_types))
   3030      1.1  christos 		    return false;
   3031      1.1  christos 
   3032      1.1  christos 		  name_len =
   3033      1.1  christos 		    strnlen (st->name,
   3034      1.1  christos 			     left - offsetof (struct lf_static_member, name));
   3035      1.1  christos 
   3036      1.1  christos 		  if (name_len == left
   3037      1.1  christos 				  - offsetof (struct lf_static_member, name))
   3038      1.1  christos 		    {
   3039      1.1  christos 		      einfo (_("%P: warning: name for LF_STMEMBER has no"
   3040      1.1  christos 			       " terminating zero\n"));
   3041      1.1  christos 		      return false;
   3042      1.1  christos 		    }
   3043      1.1  christos 
   3044      1.1  christos 		  name_len++;
   3045      1.1  christos 
   3046      1.1  christos 		  subtype_len = offsetof (struct lf_static_member, name)
   3047      1.1  christos 				+ name_len;
   3048      1.1  christos 
   3049      1.1  christos 		  if (subtype_len % 4 != 0)
   3050      1.1  christos 		    subtype_len += 4 - (subtype_len % 4);
   3051      1.1  christos 
   3052      1.1  christos 		  if (left < subtype_len)
   3053      1.1  christos 		    {
   3054      1.1  christos 		      einfo (_("%P: warning: truncated CodeView type record"
   3055      1.1  christos 			       " LF_FIELDLIST\n"));
   3056      1.1  christos 		      return false;
   3057      1.1  christos 		    }
   3058      1.1  christos 
   3059      1.1  christos 		  ptr += subtype_len;
   3060      1.1  christos 		  left -= subtype_len;
   3061      1.1  christos 
   3062      1.1  christos 		  break;
   3063      1.1  christos 		}
   3064      1.1  christos 
   3065      1.1  christos 	      case LF_NESTTYPE:
   3066      1.1  christos 		{
   3067      1.1  christos 		  struct lf_nest_type *nest = (struct lf_nest_type *) ptr;
   3068      1.1  christos 		  size_t name_len, subtype_len;
   3069      1.1  christos 
   3070      1.1  christos 		  if (left < offsetof (struct lf_nest_type, name))
   3071      1.1  christos 		    {
   3072      1.1  christos 		      einfo (_("%P: warning: truncated CodeView type record"
   3073      1.1  christos 			       " LF_NESTTYPE\n"));
   3074      1.1  christos 		      return false;
   3075      1.1  christos 		    }
   3076      1.1  christos 
   3077      1.1  christos 		  if (!remap_type (&nest->type, map, type_num, num_types))
   3078      1.1  christos 		    return false;
   3079      1.1  christos 
   3080      1.1  christos 		  name_len =
   3081      1.1  christos 		    strnlen (nest->name,
   3082      1.1  christos 			     left - offsetof (struct lf_nest_type, name));
   3083      1.1  christos 
   3084      1.1  christos 		  if (name_len == left - offsetof (struct lf_nest_type, name))
   3085      1.1  christos 		    {
   3086      1.1  christos 		      einfo (_("%P: warning: name for LF_NESTTYPE has no"
   3087      1.1  christos 			       " terminating zero\n"));
   3088      1.1  christos 		      return false;
   3089      1.1  christos 		    }
   3090      1.1  christos 
   3091      1.1  christos 		  name_len++;
   3092      1.1  christos 
   3093      1.1  christos 		  subtype_len = offsetof (struct lf_nest_type, name)
   3094      1.1  christos 				+ name_len;
   3095      1.1  christos 
   3096      1.1  christos 		  if (subtype_len % 4 != 0)
   3097      1.1  christos 		    subtype_len += 4 - (subtype_len % 4);
   3098      1.1  christos 
   3099      1.1  christos 		  if (left < subtype_len)
   3100      1.1  christos 		    {
   3101      1.1  christos 		      einfo (_("%P: warning: truncated CodeView type record"
   3102      1.1  christos 			       " LF_FIELDLIST\n"));
   3103      1.1  christos 		      return false;
   3104      1.1  christos 		    }
   3105      1.1  christos 
   3106      1.1  christos 		  ptr += subtype_len;
   3107      1.1  christos 		  left -= subtype_len;
   3108      1.1  christos 
   3109      1.1  christos 		  break;
   3110      1.1  christos 		}
   3111      1.1  christos 
   3112      1.1  christos 	      default:
   3113      1.1  christos 		einfo (_("%P: warning: unrecognized CodeView subtype %v\n"),
   3114      1.1  christos 		       subtype);
   3115      1.1  christos 		return false;
   3116      1.1  christos 	      }
   3117      1.1  christos 	  }
   3118      1.1  christos 
   3119      1.1  christos 	break;
   3120      1.1  christos       }
   3121      1.1  christos 
   3122      1.1  christos     case LF_BITFIELD:
   3123      1.1  christos       {
   3124      1.1  christos 	struct lf_bitfield *bf = (struct lf_bitfield *) data;
   3125      1.1  christos 
   3126      1.1  christos 	if (size < offsetof (struct lf_bitfield, length))
   3127      1.1  christos 	  {
   3128      1.1  christos 	    einfo (_("%P: warning: truncated CodeView type record"
   3129      1.1  christos 		     " LF_BITFIELD\n"));
   3130      1.1  christos 	    return false;
   3131      1.1  christos 	  }
   3132      1.1  christos 
   3133      1.1  christos 	if (!remap_type (&bf->base_type, map, type_num, num_types))
   3134      1.1  christos 	  return false;
   3135      1.1  christos 
   3136      1.1  christos 	break;
   3137      1.1  christos       }
   3138      1.1  christos 
   3139      1.1  christos     case LF_METHODLIST:
   3140      1.1  christos       {
   3141      1.1  christos 	struct lf_methodlist *ml = (struct lf_methodlist *) data;
   3142      1.1  christos 	unsigned int num_entries;
   3143      1.1  christos 
   3144      1.1  christos 	if (size < offsetof (struct lf_methodlist, entries))
   3145      1.1  christos 	  {
   3146      1.1  christos 	    einfo (_("%P: warning: truncated CodeView type record"
   3147      1.1  christos 		     " LF_METHODLIST\n"));
   3148      1.1  christos 	    return false;
   3149      1.1  christos 	  }
   3150      1.1  christos 
   3151      1.1  christos 	if ((size - offsetof (struct lf_methodlist, entries))
   3152      1.1  christos 	    % sizeof (struct lf_methodlist_entry))
   3153      1.1  christos 	  {
   3154      1.1  christos 	    einfo (_("%P: warning: malformed CodeView type record"
   3155      1.1  christos 		     " LF_METHODLIST\n"));
   3156      1.1  christos 	    return false;
   3157      1.1  christos 	  }
   3158      1.1  christos 
   3159      1.1  christos 	num_entries = (size - offsetof (struct lf_methodlist, entries))
   3160      1.1  christos 		      / sizeof (struct lf_methodlist_entry);
   3161      1.1  christos 
   3162      1.1  christos 	for (unsigned int i = 0; i < num_entries; i++)
   3163      1.1  christos 	  {
   3164      1.1  christos 	    if (!remap_type (&ml->entries[i].method_type, map,
   3165      1.1  christos 			     type_num, num_types))
   3166      1.1  christos 	      return false;
   3167      1.1  christos 	  }
   3168      1.1  christos 
   3169      1.1  christos 	break;
   3170      1.1  christos       }
   3171      1.1  christos 
   3172      1.1  christos     case LF_ARRAY:
   3173      1.1  christos       {
   3174      1.1  christos 	struct lf_array *arr = (struct lf_array *) data;
   3175      1.1  christos 
   3176      1.1  christos 	if (size < offsetof (struct lf_array, length_in_bytes))
   3177      1.1  christos 	  {
   3178      1.1  christos 	    einfo (_("%P: warning: truncated CodeView type record"
   3179      1.1  christos 		     " LF_ARRAY\n"));
   3180      1.1  christos 	    return false;
   3181      1.1  christos 	  }
   3182      1.1  christos 
   3183      1.1  christos 	if (!remap_type (&arr->element_type, map, type_num, num_types))
   3184      1.1  christos 	  return false;
   3185      1.1  christos 
   3186      1.1  christos 	if (!remap_type (&arr->index_type, map, type_num, num_types))
   3187      1.1  christos 	  return false;
   3188      1.1  christos 
   3189      1.1  christos 	break;
   3190      1.1  christos       }
   3191      1.1  christos 
   3192      1.1  christos     case LF_CLASS:
   3193      1.1  christos     case LF_STRUCTURE:
   3194      1.1  christos       {
   3195      1.1  christos 	struct lf_class *cl = (struct lf_class *) data;
   3196      1.1  christos 	uint16_t prop, num_bytes;
   3197      1.1  christos 	size_t name_len, name_off;
   3198      1.1  christos 
   3199      1.1  christos 	if (size < offsetof (struct lf_class, name))
   3200      1.1  christos 	  {
   3201      1.1  christos 	    einfo (_("%P: warning: truncated CodeView type record"
   3202      1.1  christos 		     " LF_CLASS/LF_STRUCTURE\n"));
   3203      1.1  christos 	    return false;
   3204      1.1  christos 	  }
   3205      1.1  christos 
   3206      1.1  christos 	if (!remap_type (&cl->field_list, map, type_num, num_types))
   3207      1.1  christos 	  return false;
   3208      1.1  christos 
   3209      1.1  christos 	if (!remap_type (&cl->derived_from, map, type_num, num_types))
   3210      1.1  christos 	  return false;
   3211      1.1  christos 
   3212      1.1  christos 	if (!remap_type (&cl->vshape, map, type_num, num_types))
   3213      1.1  christos 	  return false;
   3214      1.1  christos 
   3215      1.1  christos 	name_off = offsetof (struct lf_class, name);
   3216      1.1  christos 
   3217      1.1  christos 	num_bytes = bfd_getl16 (&cl->length);
   3218      1.1  christos 
   3219      1.1  christos 	/* If num_bytes >= 0x8000, actual value follows.  */
   3220      1.1  christos 	if (num_bytes >= 0x8000)
   3221      1.1  christos 	  {
   3222      1.1  christos 	    unsigned int param_len = extended_value_len (num_bytes);
   3223      1.1  christos 
   3224      1.1  christos 	    if (param_len == 0)
   3225      1.1  christos 	      {
   3226      1.1  christos 		einfo (_("%P: warning: unhandled type %v within"
   3227      1.1  christos 			 " LF_CLASS/LF_STRUCTURE\n"), num_bytes);
   3228      1.1  christos 		return false;
   3229      1.1  christos 	      }
   3230      1.1  christos 
   3231      1.1  christos 	    name_off += param_len;
   3232      1.1  christos 
   3233      1.1  christos 	    if (size < name_off)
   3234      1.1  christos 	      {
   3235      1.1  christos 		einfo (_("%P: warning: truncated CodeView type record"
   3236      1.1  christos 			 " LF_CLASS/LF_STRUCTURE\n"));
   3237      1.1  christos 		return false;
   3238      1.1  christos 	      }
   3239      1.1  christos 	  }
   3240      1.1  christos 
   3241      1.1  christos 	name_len = strnlen ((char *) cl + name_off, size - name_off);
   3242      1.1  christos 
   3243      1.1  christos 	if (name_len == size - name_off)
   3244      1.1  christos 	  {
   3245      1.1  christos 	    einfo (_("%P: warning: name for LF_CLASS/LF_STRUCTURE has no"
   3246      1.1  christos 		     " terminating zero\n"));
   3247      1.1  christos 	    return false;
   3248      1.1  christos 	  }
   3249      1.1  christos 
   3250      1.1  christos 	prop = bfd_getl16 (&cl->properties);
   3251      1.1  christos 
   3252      1.1  christos 	if (prop & CV_PROP_HAS_UNIQUE_NAME)
   3253      1.1  christos 	  {
   3254      1.1  christos 	    /* Structure has another name following first one.  */
   3255      1.1  christos 
   3256      1.1  christos 	    size_t len = name_off + name_len + 1;
   3257      1.1  christos 	    size_t unique_name_len;
   3258      1.1  christos 
   3259      1.1  christos 	    unique_name_len = strnlen ((char *) cl + name_off + name_len + 1,
   3260      1.1  christos 				       size - len);
   3261      1.1  christos 
   3262      1.1  christos 	    if (unique_name_len == size - len)
   3263      1.1  christos 	      {
   3264      1.1  christos 		einfo (_("%P: warning: unique name for LF_CLASS/LF_STRUCTURE"
   3265      1.1  christos 			 " has no terminating zero\n"));
   3266      1.1  christos 		return false;
   3267      1.1  christos 	      }
   3268      1.1  christos 	  }
   3269      1.1  christos 
   3270      1.1  christos 	if (!(prop & (CV_PROP_FORWARD_REF | CV_PROP_SCOPED))
   3271      1.1  christos 	    && !is_name_anonymous ((char *) cl + name_off, name_len))
   3272      1.1  christos 	  {
   3273      1.1  christos 	    other_hash = true;
   3274      1.1  christos 	    cv_hash = crc32 ((uint8_t *) cl + name_off, name_len);
   3275      1.1  christos 	  }
   3276      1.1  christos 
   3277      1.1  christos 	break;
   3278      1.1  christos       }
   3279      1.1  christos 
   3280      1.1  christos     case LF_UNION:
   3281      1.1  christos       {
   3282      1.1  christos 	struct lf_union *un = (struct lf_union *) data;
   3283      1.1  christos 	uint16_t prop, num_bytes;
   3284      1.1  christos 	size_t name_len, name_off;
   3285      1.1  christos 
   3286      1.1  christos 	if (size < offsetof (struct lf_union, name))
   3287      1.1  christos 	  {
   3288      1.1  christos 	    einfo (_("%P: warning: truncated CodeView type record"
   3289      1.1  christos 		     " LF_UNION\n"));
   3290      1.1  christos 	    return false;
   3291      1.1  christos 	  }
   3292      1.1  christos 
   3293      1.1  christos 	if (!remap_type (&un->field_list, map, type_num, num_types))
   3294      1.1  christos 	  return false;
   3295      1.1  christos 
   3296      1.1  christos 	name_off = offsetof (struct lf_union, name);
   3297      1.1  christos 
   3298      1.1  christos 	num_bytes = bfd_getl16 (&un->length);
   3299      1.1  christos 
   3300      1.1  christos 	/* If num_bytes >= 0x8000, actual value follows.  */
   3301      1.1  christos 	if (num_bytes >= 0x8000)
   3302      1.1  christos 	  {
   3303      1.1  christos 	    unsigned int param_len = extended_value_len (num_bytes);
   3304      1.1  christos 
   3305      1.1  christos 	    if (param_len == 0)
   3306      1.1  christos 	      {
   3307      1.1  christos 		einfo (_("%P: warning: unhandled type %v within"
   3308      1.1  christos 			 " LF_UNION\n"), num_bytes);
   3309      1.1  christos 		return false;
   3310      1.1  christos 	      }
   3311      1.1  christos 
   3312      1.1  christos 	    name_off += param_len;
   3313      1.1  christos 
   3314      1.1  christos 	    if (size < name_off)
   3315      1.1  christos 	      {
   3316      1.1  christos 		einfo (_("%P: warning: truncated CodeView type record"
   3317      1.1  christos 			 " LF_UNION\n"));
   3318      1.1  christos 		return false;
   3319      1.1  christos 	      }
   3320      1.1  christos 	  }
   3321      1.1  christos 
   3322      1.1  christos 	name_len = strnlen ((char *) un + name_off, size - name_off);
   3323      1.1  christos 
   3324      1.1  christos 	if (name_len == size - name_off)
   3325      1.1  christos 	  {
   3326      1.1  christos 	    einfo (_("%P: warning: name for LF_UNION has no"
   3327      1.1  christos 		     " terminating zero\n"));
   3328      1.1  christos 	    return false;
   3329      1.1  christos 	  }
   3330      1.1  christos 
   3331      1.1  christos 	prop = bfd_getl16 (&un->properties);
   3332      1.1  christos 
   3333      1.1  christos 	if (prop & CV_PROP_HAS_UNIQUE_NAME)
   3334      1.1  christos 	  {
   3335      1.1  christos 	    /* Structure has another name following first one.  */
   3336      1.1  christos 
   3337      1.1  christos 	    size_t len = name_off + name_len + 1;
   3338      1.1  christos 	    size_t unique_name_len;
   3339      1.1  christos 
   3340      1.1  christos 	    unique_name_len = strnlen ((char *) un + name_off + name_len + 1,
   3341      1.1  christos 				       size - len);
   3342      1.1  christos 
   3343      1.1  christos 	    if (unique_name_len == size - len)
   3344      1.1  christos 	      {
   3345      1.1  christos 		einfo (_("%P: warning: unique name for LF_UNION has"
   3346      1.1  christos 			 " no terminating zero\n"));
   3347      1.1  christos 		return false;
   3348      1.1  christos 	      }
   3349      1.1  christos 	  }
   3350      1.1  christos 
   3351      1.1  christos 	if (!(prop & (CV_PROP_FORWARD_REF | CV_PROP_SCOPED))
   3352      1.1  christos 	    && !is_name_anonymous ((char *) un + name_off, name_len))
   3353      1.1  christos 	  {
   3354      1.1  christos 	    other_hash = true;
   3355      1.1  christos 	    cv_hash = crc32 ((uint8_t *) un + name_off, name_len);
   3356      1.1  christos 	  }
   3357      1.1  christos 
   3358      1.1  christos 	break;
   3359      1.1  christos       }
   3360      1.1  christos 
   3361      1.1  christos     case LF_ENUM:
   3362      1.1  christos       {
   3363      1.1  christos 	struct lf_enum *en = (struct lf_enum *) data;
   3364      1.1  christos 	uint16_t prop;
   3365      1.1  christos 	size_t name_len;
   3366      1.1  christos 
   3367      1.1  christos 	if (size < offsetof (struct lf_enum, name))
   3368      1.1  christos 	  {
   3369      1.1  christos 	    einfo (_("%P: warning: truncated CodeView type record"
   3370      1.1  christos 		     " LF_ENUM\n"));
   3371      1.1  christos 	    return false;
   3372      1.1  christos 	  }
   3373      1.1  christos 
   3374      1.1  christos 	if (!remap_type (&en->underlying_type, map, type_num, num_types))
   3375      1.1  christos 	  return false;
   3376      1.1  christos 
   3377      1.1  christos 	if (!remap_type (&en->field_list, map, type_num, num_types))
   3378      1.1  christos 	  return false;
   3379      1.1  christos 
   3380      1.1  christos 	name_len = strnlen (en->name, size - offsetof (struct lf_enum, name));
   3381      1.1  christos 
   3382      1.1  christos 	if (name_len == size - offsetof (struct lf_enum, name))
   3383      1.1  christos 	  {
   3384      1.1  christos 	    einfo (_("%P: warning: name for LF_ENUM has no"
   3385      1.1  christos 		     " terminating zero\n"));
   3386      1.1  christos 	    return false;
   3387      1.1  christos 	  }
   3388      1.1  christos 
   3389      1.1  christos 	prop = bfd_getl16 (&en->properties);
   3390      1.1  christos 
   3391      1.1  christos 	if (prop & CV_PROP_HAS_UNIQUE_NAME)
   3392      1.1  christos 	  {
   3393      1.1  christos 	    /* Structure has another name following first one.  */
   3394      1.1  christos 
   3395      1.1  christos 	    size_t len = offsetof (struct lf_enum, name) + name_len + 1;
   3396      1.1  christos 	    size_t unique_name_len;
   3397      1.1  christos 
   3398      1.1  christos 	    unique_name_len = strnlen (en->name + name_len + 1, size - len);
   3399      1.1  christos 
   3400      1.1  christos 	    if (unique_name_len == size - len)
   3401      1.1  christos 	      {
   3402      1.1  christos 		einfo (_("%P: warning: unique name for LF_ENUM has"
   3403      1.1  christos 			 " no terminating zero\n"));
   3404      1.1  christos 		return false;
   3405      1.1  christos 	      }
   3406      1.1  christos 	  }
   3407      1.1  christos 
   3408      1.1  christos 	break;
   3409      1.1  christos       }
   3410      1.1  christos 
   3411      1.1  christos     case LF_VTSHAPE:
   3412      1.1  christos       /* Does not reference any types, nothing to be done.  */
   3413      1.1  christos       break;
   3414      1.1  christos 
   3415      1.1  christos     case LF_VFTABLE:
   3416      1.1  christos       {
   3417      1.1  christos 	struct lf_vftable *vft = (struct lf_vftable *) data;
   3418      1.1  christos 
   3419      1.1  christos 	if (size < offsetof (struct lf_vftable, names))
   3420      1.1  christos 	  {
   3421      1.1  christos 	    einfo (_("%P: warning: truncated CodeView type record"
   3422      1.1  christos 		     " LF_VFTABLE\n"));
   3423      1.1  christos 	    return false;
   3424      1.1  christos 	  }
   3425      1.1  christos 
   3426      1.1  christos 	if (!remap_type (&vft->type, map, type_num, num_types))
   3427      1.1  christos 	  return false;
   3428      1.1  christos 
   3429      1.1  christos 	if (!remap_type (&vft->base_vftable, map, type_num, num_types))
   3430      1.1  christos 	  return false;
   3431      1.1  christos 
   3432      1.1  christos 	break;
   3433      1.1  christos       }
   3434      1.1  christos 
   3435      1.1  christos     case LF_STRING_ID:
   3436      1.1  christos       {
   3437      1.1  christos 	struct lf_string_id *str = (struct lf_string_id *) data;
   3438      1.1  christos 	size_t string_len;
   3439      1.1  christos 
   3440      1.1  christos 	if (size < offsetof (struct lf_string_id, string))
   3441      1.1  christos 	  {
   3442      1.1  christos 	    einfo (_("%P: warning: truncated CodeView type record"
   3443      1.1  christos 		     " LF_STRING_ID\n"));
   3444      1.1  christos 	    return false;
   3445      1.1  christos 	  }
   3446      1.1  christos 
   3447      1.1  christos 	if (!remap_type (&str->substring, map, type_num, num_types))
   3448      1.1  christos 	  return false;
   3449      1.1  christos 
   3450      1.1  christos 	string_len = strnlen (str->string,
   3451      1.1  christos 			      size - offsetof (struct lf_string_id, string));
   3452      1.1  christos 
   3453      1.1  christos 	if (string_len == size - offsetof (struct lf_string_id, string))
   3454      1.1  christos 	  {
   3455      1.1  christos 	    einfo (_("%P: warning: string for LF_STRING_ID has no"
   3456      1.1  christos 		     " terminating zero\n"));
   3457      1.1  christos 	    return false;
   3458      1.1  christos 	  }
   3459      1.1  christos 
   3460      1.1  christos 	ipi = true;
   3461      1.1  christos 
   3462      1.1  christos 	break;
   3463      1.1  christos       }
   3464      1.1  christos 
   3465      1.1  christos     case LF_SUBSTR_LIST:
   3466      1.1  christos       {
   3467      1.1  christos 	uint32_t num_entries;
   3468      1.1  christos 	struct lf_arglist *ssl = (struct lf_arglist *) data;
   3469      1.1  christos 
   3470      1.1  christos 	if (size < offsetof (struct lf_arglist, args))
   3471      1.1  christos 	  {
   3472      1.1  christos 	    einfo (_("%P: warning: truncated CodeView type record"
   3473      1.1  christos 		     " LF_SUBSTR_LIST\n"));
   3474      1.1  christos 	    return false;
   3475      1.1  christos 	  }
   3476      1.1  christos 
   3477      1.1  christos 	num_entries = bfd_getl32 (&ssl->num_entries);
   3478      1.1  christos 
   3479      1.1  christos 	if (size < offsetof (struct lf_arglist, args)
   3480      1.1  christos 		   + (num_entries * sizeof (uint32_t)))
   3481      1.1  christos 	  {
   3482      1.1  christos 	    einfo (_("%P: warning: truncated CodeView type record"
   3483      1.1  christos 		     " LF_SUBSTR_LIST\n"));
   3484      1.1  christos 	    return false;
   3485      1.1  christos 	  }
   3486      1.1  christos 
   3487      1.1  christos 	for (uint32_t i = 0; i < num_entries; i++)
   3488      1.1  christos 	  {
   3489      1.1  christos 	    if (!remap_type (&ssl->args[i], map, type_num, num_types))
   3490      1.1  christos 	      return false;
   3491      1.1  christos 	  }
   3492      1.1  christos 
   3493      1.1  christos 	ipi = true;
   3494      1.1  christos 
   3495      1.1  christos 	break;
   3496      1.1  christos       }
   3497      1.1  christos 
   3498      1.1  christos     case LF_BUILDINFO:
   3499      1.1  christos       {
   3500      1.1  christos 	uint16_t num_entries;
   3501      1.1  christos 	struct lf_build_info *bi = (struct lf_build_info *) data;
   3502      1.1  christos 
   3503      1.1  christos 	if (size < offsetof (struct lf_build_info, strings))
   3504      1.1  christos 	  {
   3505      1.1  christos 	    einfo (_("%P: warning: truncated CodeView type record"
   3506      1.1  christos 		     " LF_BUILDINFO\n"));
   3507      1.1  christos 	    return false;
   3508      1.1  christos 	  }
   3509      1.1  christos 
   3510      1.1  christos 	num_entries = bfd_getl16 (&bi->count);
   3511      1.1  christos 
   3512      1.1  christos 	if (size < offsetof (struct lf_build_info, strings)
   3513      1.1  christos 		   + (num_entries * sizeof (uint32_t)))
   3514      1.1  christos 	  {
   3515      1.1  christos 	    einfo (_("%P: warning: truncated CodeView type record"
   3516      1.1  christos 		     " LF_BUILDINFO\n"));
   3517      1.1  christos 	    return false;
   3518      1.1  christos 	  }
   3519      1.1  christos 
   3520      1.1  christos 	for (uint32_t i = 0; i < num_entries; i++)
   3521      1.1  christos 	  {
   3522      1.1  christos 	    if (!remap_type (&bi->strings[i], map, type_num, num_types))
   3523      1.1  christos 	      return false;
   3524      1.1  christos 	  }
   3525      1.1  christos 
   3526      1.1  christos 	ipi = true;
   3527      1.1  christos 
   3528      1.1  christos 	break;
   3529      1.1  christos       }
   3530      1.1  christos 
   3531      1.1  christos     case LF_FUNC_ID:
   3532      1.1  christos       {
   3533      1.1  christos 	struct lf_func_id *func = (struct lf_func_id *) data;
   3534      1.1  christos 	size_t name_len;
   3535      1.1  christos 
   3536      1.1  christos 	if (size < offsetof (struct lf_func_id, name))
   3537      1.1  christos 	  {
   3538      1.1  christos 	    einfo (_("%P: warning: truncated CodeView type record"
   3539      1.1  christos 		     " LF_FUNC_ID\n"));
   3540      1.1  christos 	    return false;
   3541      1.1  christos 	  }
   3542      1.1  christos 
   3543      1.1  christos 	if (!remap_type (&func->parent_scope, map, type_num, num_types))
   3544      1.1  christos 	  return false;
   3545      1.1  christos 
   3546      1.1  christos 	if (!remap_type (&func->function_type, map, type_num, num_types))
   3547      1.1  christos 	  return false;
   3548      1.1  christos 
   3549      1.1  christos 	name_len = strnlen (func->name,
   3550      1.1  christos 			    size - offsetof (struct lf_func_id, name));
   3551      1.1  christos 
   3552      1.1  christos 	if (name_len == size - offsetof (struct lf_func_id, name))
   3553      1.1  christos 	  {
   3554      1.1  christos 	    einfo (_("%P: warning: string for LF_FUNC_ID has no"
   3555      1.1  christos 		     " terminating zero\n"));
   3556      1.1  christos 	    return false;
   3557      1.1  christos 	  }
   3558      1.1  christos 
   3559      1.1  christos 	ipi = true;
   3560      1.1  christos 
   3561      1.1  christos 	break;
   3562      1.1  christos       }
   3563      1.1  christos 
   3564      1.1  christos     case LF_MFUNC_ID:
   3565      1.1  christos       {
   3566      1.1  christos 	struct lf_mfunc_id *mfunc = (struct lf_mfunc_id *) data;
   3567      1.1  christos 	size_t name_len;
   3568      1.1  christos 
   3569      1.1  christos 	if (size < offsetof (struct lf_mfunc_id, name))
   3570      1.1  christos 	  {
   3571      1.1  christos 	    einfo (_("%P: warning: truncated CodeView type record"
   3572      1.1  christos 		     " LF_MFUNC_ID\n"));
   3573      1.1  christos 	    return false;
   3574      1.1  christos 	  }
   3575      1.1  christos 
   3576      1.1  christos 	if (!remap_type (&mfunc->parent_type, map, type_num, num_types))
   3577      1.1  christos 	  return false;
   3578      1.1  christos 
   3579      1.1  christos 	if (!remap_type (&mfunc->function_type, map, type_num, num_types))
   3580      1.1  christos 	  return false;
   3581      1.1  christos 
   3582      1.1  christos 	name_len = strnlen (mfunc->name,
   3583      1.1  christos 			    size - offsetof (struct lf_mfunc_id, name));
   3584      1.1  christos 
   3585      1.1  christos 	if (name_len == size - offsetof (struct lf_mfunc_id, name))
   3586      1.1  christos 	  {
   3587      1.1  christos 	    einfo (_("%P: warning: string for LF_MFUNC_ID has no"
   3588      1.1  christos 		     " terminating zero\n"));
   3589      1.1  christos 	    return false;
   3590      1.1  christos 	  }
   3591      1.1  christos 
   3592      1.1  christos 	ipi = true;
   3593      1.1  christos 
   3594      1.1  christos 	break;
   3595      1.1  christos       }
   3596      1.1  christos 
   3597      1.1  christos     case LF_UDT_SRC_LINE:
   3598      1.1  christos       return handle_udt_src_line (data, size, map, type_num, num_types,
   3599      1.1  christos 				  ids, mod_num, strings);
   3600      1.1  christos 
   3601      1.1  christos     default:
   3602      1.1  christos       einfo (_("%P: warning: unrecognized CodeView type %v\n"), type);
   3603      1.1  christos       return false;
   3604      1.1  christos     }
   3605      1.1  christos 
   3606      1.1  christos   hash = iterative_hash (data, size, 0);
   3607      1.1  christos 
   3608      1.1  christos   t = ipi ? ids : types;
   3609      1.1  christos 
   3610      1.1  christos   slot = htab_find_slot_with_hash (t->hashmap, data, hash, INSERT);
   3611      1.1  christos   if (!slot)
   3612      1.1  christos     return false;
   3613      1.1  christos 
   3614      1.1  christos   if (!*slot) /* new entry */
   3615      1.1  christos     {
   3616      1.1  christos       struct type_entry *e;
   3617      1.1  christos 
   3618      1.1  christos       *slot = xmalloc (offsetof (struct type_entry, data) + size);
   3619      1.1  christos 
   3620      1.1  christos       e = (struct type_entry *) *slot;
   3621      1.1  christos 
   3622      1.1  christos       e->next = NULL;
   3623      1.1  christos       e->index = t->num_types;
   3624      1.1  christos 
   3625      1.1  christos       if (other_hash)
   3626      1.1  christos 	e->cv_hash = cv_hash;
   3627      1.1  christos       else
   3628      1.1  christos 	e->cv_hash = crc32 (data, size);
   3629      1.1  christos 
   3630      1.1  christos       e->has_udt_src_line = false;
   3631      1.1  christos 
   3632      1.1  christos       memcpy (e->data, data, size);
   3633      1.1  christos 
   3634      1.1  christos       if (t->last)
   3635      1.1  christos 	t->last->next = e;
   3636      1.1  christos       else
   3637      1.1  christos 	t->first = e;
   3638      1.1  christos 
   3639      1.1  christos       t->last = e;
   3640      1.1  christos 
   3641      1.1  christos       map[type_num] = e;
   3642      1.1  christos 
   3643      1.1  christos       t->num_types++;
   3644      1.1  christos     }
   3645      1.1  christos   else /* duplicate */
   3646      1.1  christos     {
   3647      1.1  christos       map[type_num] = (struct type_entry *) *slot;
   3648      1.1  christos     }
   3649      1.1  christos 
   3650      1.1  christos   return true;
   3651      1.1  christos }
   3652      1.1  christos 
   3653      1.1  christos /* Parse the .debug$T section of a module, and pass any type definitions
   3654      1.1  christos    found to handle_type.  */
   3655      1.1  christos static bool
   3656      1.1  christos handle_debugt_section (asection *s, bfd *mod, struct types *types,
   3657      1.1  christos 		       struct types *ids, uint16_t mod_num,
   3658      1.1  christos 		       struct string_table *strings,
   3659      1.1  christos 		       struct type_entry ***map, uint32_t *num_types)
   3660      1.1  christos {
   3661      1.1  christos   bfd_byte *data = NULL;
   3662      1.1  christos   size_t off;
   3663      1.1  christos   uint32_t type_num;
   3664      1.1  christos 
   3665      1.1  christos   if (!bfd_get_full_section_contents (mod, s, &data))
   3666      1.1  christos     return false;
   3667      1.1  christos 
   3668      1.1  christos   if (!data)
   3669      1.1  christos     return false;
   3670      1.1  christos 
   3671      1.1  christos   if (bfd_getl32 (data) != CV_SIGNATURE_C13)
   3672      1.1  christos     {
   3673      1.1  christos       free (data);
   3674      1.1  christos       return true;
   3675      1.1  christos     }
   3676      1.1  christos 
   3677      1.1  christos   off = sizeof (uint32_t);
   3678      1.1  christos 
   3679      1.1  christos   while (off + sizeof (uint16_t) <= s->size)
   3680      1.1  christos     {
   3681      1.1  christos       uint16_t size;
   3682      1.1  christos 
   3683      1.1  christos       size = bfd_getl16 (data + off);
   3684      1.1  christos       off += sizeof (uint16_t);
   3685      1.1  christos 
   3686  1.1.1.2  christos       if (size + off > s->size || size < sizeof (uint16_t))
   3687      1.1  christos 	{
   3688      1.1  christos 	  free (data);
   3689      1.1  christos 	  bfd_set_error (bfd_error_bad_value);
   3690      1.1  christos 	  return false;
   3691      1.1  christos 	}
   3692      1.1  christos 
   3693      1.1  christos       (*num_types)++;
   3694      1.1  christos       off += size;
   3695      1.1  christos     }
   3696      1.1  christos 
   3697      1.1  christos   if (*num_types == 0)
   3698      1.1  christos     {
   3699      1.1  christos       free (data);
   3700      1.1  christos       return true;
   3701      1.1  christos     }
   3702      1.1  christos 
   3703      1.1  christos   *map = xcalloc (*num_types, sizeof (struct type_entry *));
   3704      1.1  christos 
   3705      1.1  christos   off = sizeof (uint32_t);
   3706      1.1  christos   type_num = 0;
   3707      1.1  christos 
   3708      1.1  christos   while (off + sizeof (uint16_t) <= s->size)
   3709      1.1  christos     {
   3710      1.1  christos       uint16_t size;
   3711      1.1  christos 
   3712      1.1  christos       size = bfd_getl16 (data + off);
   3713      1.1  christos 
   3714      1.1  christos       if (!handle_type (data + off, *map, type_num, *num_types, types, ids,
   3715      1.1  christos 			mod_num, strings))
   3716      1.1  christos 	{
   3717      1.1  christos 	  free (data);
   3718      1.1  christos 	  free (*map);
   3719      1.1  christos 	  bfd_set_error (bfd_error_bad_value);
   3720      1.1  christos 	  return false;
   3721      1.1  christos 	}
   3722      1.1  christos 
   3723      1.1  christos       off += sizeof (uint16_t) + size;
   3724      1.1  christos       type_num++;
   3725      1.1  christos     }
   3726      1.1  christos 
   3727      1.1  christos   free (data);
   3728      1.1  christos 
   3729      1.1  christos   return true;
   3730      1.1  christos }
   3731      1.1  christos 
   3732      1.1  christos /* Return the CodeView constant for the selected architecture.  */
   3733      1.1  christos static uint16_t
   3734      1.1  christos target_processor (bfd *abfd)
   3735      1.1  christos {
   3736      1.1  christos   switch (abfd->arch_info->arch)
   3737      1.1  christos     {
   3738      1.1  christos     case bfd_arch_i386:
   3739      1.1  christos       if (abfd->arch_info->mach & bfd_mach_x86_64)
   3740      1.1  christos 	return CV_CFL_X64;
   3741      1.1  christos       else
   3742      1.1  christos 	return CV_CFL_80386;
   3743      1.1  christos 
   3744      1.1  christos     case bfd_arch_aarch64:
   3745      1.1  christos       return CV_CFL_ARM64;
   3746      1.1  christos 
   3747      1.1  christos     default:
   3748      1.1  christos       return 0;
   3749      1.1  christos     }
   3750      1.1  christos }
   3751      1.1  christos 
   3752      1.1  christos /* Create the symbols that go in "* Linker *", the dummy module created
   3753      1.1  christos    for the linker itself.  */
   3754      1.1  christos static bool
   3755      1.1  christos create_linker_symbols (bfd *abfd, uint8_t **syms, uint32_t *sym_byte_size,
   3756      1.1  christos 		       const char *pdb_name)
   3757      1.1  christos {
   3758      1.1  christos   uint8_t *ptr;
   3759      1.1  christos   struct objname *name;
   3760      1.1  christos   struct compile3 *comp;
   3761      1.1  christos   struct envblock *env;
   3762      1.1  christos   size_t padding1, padding2, env_size;
   3763      1.1  christos   char *cwdval, *exeval, *pdbval;
   3764      1.1  christos 
   3765      1.1  christos   /* extra NUL for padding */
   3766      1.1  christos   static const char linker_fn[] = "* Linker *\0";
   3767      1.1  christos   static const char linker_name[] = "GNU LD " VERSION;
   3768      1.1  christos 
   3769      1.1  christos   static const char cwd[] = "cwd";
   3770      1.1  christos   static const char exe[] = "exe";
   3771      1.1  christos   static const char pdb[] = "pdb";
   3772      1.1  christos 
   3773      1.1  christos   cwdval = getcwd (NULL, 0);
   3774      1.1  christos   if (!cwdval)
   3775      1.1  christos     {
   3776      1.1  christos       einfo (_("%P: warning: unable to get working directory\n"));
   3777      1.1  christos       return false;
   3778      1.1  christos     }
   3779      1.1  christos 
   3780      1.1  christos   exeval = lrealpath (program_name);
   3781      1.1  christos 
   3782      1.1  christos   if (!exeval)
   3783      1.1  christos     {
   3784      1.1  christos       einfo (_("%P: warning: unable to get program name\n"));
   3785      1.1  christos       free (cwdval);
   3786      1.1  christos       return false;
   3787      1.1  christos     }
   3788      1.1  christos 
   3789      1.1  christos   pdbval = lrealpath (pdb_name);
   3790      1.1  christos 
   3791      1.1  christos   if (!pdbval)
   3792      1.1  christos     {
   3793      1.1  christos       einfo (_("%P: warning: unable to get full path to PDB\n"));
   3794      1.1  christos       free (exeval);
   3795      1.1  christos       free (cwdval);
   3796      1.1  christos       return false;
   3797      1.1  christos     }
   3798      1.1  christos 
   3799      1.1  christos   *sym_byte_size += offsetof (struct objname, name) + sizeof (linker_fn);
   3800      1.1  christos   *sym_byte_size += offsetof (struct compile3, compiler) + sizeof (linker_name);
   3801      1.1  christos 
   3802      1.1  christos   if (*sym_byte_size % 4)
   3803      1.1  christos     padding1 = 4 - (*sym_byte_size % 4);
   3804      1.1  christos   else
   3805      1.1  christos     padding1 = 0;
   3806      1.1  christos 
   3807      1.1  christos   *sym_byte_size += padding1;
   3808      1.1  christos 
   3809      1.1  christos   env_size = offsetof (struct envblock, strings);
   3810      1.1  christos   env_size += sizeof (cwd);
   3811      1.1  christos   env_size += strlen (cwdval) + 1;
   3812      1.1  christos   env_size += sizeof (exe);
   3813      1.1  christos   env_size += strlen (exeval) + 1;
   3814      1.1  christos   env_size += sizeof (pdb);
   3815      1.1  christos   env_size += strlen (pdbval) + 1;
   3816      1.1  christos 
   3817      1.1  christos   if (env_size % 4)
   3818      1.1  christos     padding2 = 4 - (env_size % 4);
   3819      1.1  christos   else
   3820      1.1  christos     padding2 = 0;
   3821      1.1  christos 
   3822      1.1  christos   env_size += padding2;
   3823      1.1  christos 
   3824      1.1  christos   *sym_byte_size += env_size;
   3825      1.1  christos 
   3826      1.1  christos   *syms = xmalloc (*sym_byte_size);
   3827      1.1  christos   ptr = *syms;
   3828      1.1  christos 
   3829      1.1  christos   /* Write S_OBJNAME */
   3830      1.1  christos 
   3831      1.1  christos   name = (struct objname *) ptr;
   3832      1.1  christos   bfd_putl16 (offsetof (struct objname, name)
   3833      1.1  christos 	      + sizeof (linker_fn) - sizeof (uint16_t), &name->size);
   3834      1.1  christos   bfd_putl16 (S_OBJNAME, &name->kind);
   3835      1.1  christos   bfd_putl32 (0, &name->signature);
   3836      1.1  christos   memcpy (name->name, linker_fn, sizeof (linker_fn));
   3837      1.1  christos 
   3838      1.1  christos   ptr += offsetof (struct objname, name) + sizeof (linker_fn);
   3839      1.1  christos 
   3840      1.1  christos   /* Write S_COMPILE3 */
   3841      1.1  christos 
   3842      1.1  christos   comp = (struct compile3 *) ptr;
   3843      1.1  christos 
   3844      1.1  christos   bfd_putl16 (offsetof (struct compile3, compiler) + sizeof (linker_name)
   3845      1.1  christos 	      + padding1 - sizeof (uint16_t), &comp->size);
   3846      1.1  christos   bfd_putl16 (S_COMPILE3, &comp->kind);
   3847      1.1  christos   bfd_putl32 (CV_CFL_LINK, &comp->flags);
   3848      1.1  christos   bfd_putl16 (target_processor (abfd), &comp->machine);
   3849      1.1  christos   bfd_putl16 (0, &comp->frontend_major);
   3850      1.1  christos   bfd_putl16 (0, &comp->frontend_minor);
   3851      1.1  christos   bfd_putl16 (0, &comp->frontend_build);
   3852      1.1  christos   bfd_putl16 (0, &comp->frontend_qfe);
   3853      1.1  christos   bfd_putl16 (0, &comp->backend_major);
   3854      1.1  christos   bfd_putl16 (0, &comp->backend_minor);
   3855      1.1  christos   bfd_putl16 (0, &comp->backend_build);
   3856      1.1  christos   bfd_putl16 (0, &comp->backend_qfe);
   3857      1.1  christos   memcpy (comp->compiler, linker_name, sizeof (linker_name));
   3858      1.1  christos 
   3859      1.1  christos   memset (comp->compiler + sizeof (linker_name), 0, padding1);
   3860      1.1  christos 
   3861      1.1  christos   ptr += offsetof (struct compile3, compiler) + sizeof (linker_name) + padding1;
   3862      1.1  christos 
   3863      1.1  christos   /* Write S_ENVBLOCK */
   3864      1.1  christos 
   3865      1.1  christos   env = (struct envblock *) ptr;
   3866      1.1  christos 
   3867      1.1  christos   bfd_putl16 (env_size - sizeof (uint16_t), &env->size);
   3868      1.1  christos   bfd_putl16 (S_ENVBLOCK, &env->kind);
   3869      1.1  christos   env->flags = 0;
   3870      1.1  christos 
   3871      1.1  christos   ptr += offsetof (struct envblock, strings);
   3872      1.1  christos 
   3873      1.1  christos   memcpy (ptr, cwd, sizeof (cwd));
   3874      1.1  christos   ptr += sizeof (cwd);
   3875      1.1  christos   memcpy (ptr, cwdval, strlen (cwdval) + 1);
   3876      1.1  christos   ptr += strlen (cwdval) + 1;
   3877      1.1  christos 
   3878      1.1  christos   memcpy (ptr, exe, sizeof (exe));
   3879      1.1  christos   ptr += sizeof (exe);
   3880      1.1  christos   memcpy (ptr, exeval, strlen (exeval) + 1);
   3881      1.1  christos   ptr += strlen (exeval) + 1;
   3882      1.1  christos 
   3883      1.1  christos   memcpy (ptr, pdb, sizeof (pdb));
   3884      1.1  christos   ptr += sizeof (pdb);
   3885      1.1  christos   memcpy (ptr, pdbval, strlen (pdbval) + 1);
   3886      1.1  christos   ptr += strlen (pdbval) + 1;
   3887      1.1  christos 
   3888      1.1  christos   /* Microsoft's LINK also includes "cmd", the command-line options passed
   3889      1.1  christos      to the linker, but unfortunately we don't have access to argc and argv
   3890      1.1  christos      at this stage.  */
   3891      1.1  christos 
   3892      1.1  christos   memset (ptr, 0, padding2);
   3893      1.1  christos 
   3894      1.1  christos   free (pdbval);
   3895      1.1  christos   free (exeval);
   3896      1.1  christos   free (cwdval);
   3897      1.1  christos 
   3898      1.1  christos   return true;
   3899      1.1  christos }
   3900      1.1  christos 
   3901      1.1  christos /* Populate the module stream, which consists of the transformed .debug$S
   3902      1.1  christos    data for each object file.  */
   3903      1.1  christos static bool
   3904      1.1  christos populate_module_stream (bfd *stream, bfd *mod, uint32_t *sym_byte_size,
   3905      1.1  christos 			struct string_table *strings,
   3906      1.1  christos 			uint32_t *c13_info_size,
   3907      1.1  christos 			struct mod_source_files *mod_source,
   3908      1.1  christos 			bfd *abfd, struct types *types,
   3909      1.1  christos 			struct types *ids, uint16_t mod_num,
   3910      1.1  christos 			bfd *sym_rec_stream, struct globals *glob,
   3911      1.1  christos 			const char *pdb_name)
   3912      1.1  christos {
   3913      1.1  christos   uint8_t int_buf[sizeof (uint32_t)];
   3914      1.1  christos   uint8_t *c13_info = NULL;
   3915      1.1  christos   uint8_t *syms = NULL;
   3916      1.1  christos 
   3917      1.1  christos   *sym_byte_size = 0;
   3918      1.1  christos   *c13_info_size = 0;
   3919      1.1  christos 
   3920      1.1  christos   if (!strcmp (bfd_get_filename (mod), "dll stuff"))
   3921      1.1  christos     {
   3922      1.1  christos       if (!create_linker_symbols (mod, &syms, sym_byte_size, pdb_name))
   3923      1.1  christos 	return false;
   3924      1.1  christos     }
   3925      1.1  christos   else
   3926      1.1  christos     {
   3927      1.1  christos       struct type_entry **map = NULL;
   3928      1.1  christos       uint32_t num_types = 0;
   3929      1.1  christos 
   3930      1.1  christos       /* Process .debug$T section.  */
   3931      1.1  christos 
   3932      1.1  christos       for (asection *s = mod->sections; s; s = s->next)
   3933      1.1  christos 	{
   3934      1.1  christos 	  if (!strcmp (s->name, ".debug$T") && s->size >= sizeof (uint32_t))
   3935      1.1  christos 	    {
   3936      1.1  christos 	      if (!handle_debugt_section (s, mod, types, ids, mod_num, strings,
   3937      1.1  christos 					  &map, &num_types))
   3938      1.1  christos 		{
   3939      1.1  christos 		  free (mod_source->files);
   3940      1.1  christos 		  return false;
   3941      1.1  christos 		}
   3942      1.1  christos 
   3943      1.1  christos 	      break;
   3944      1.1  christos 	    }
   3945      1.1  christos 	}
   3946      1.1  christos 
   3947      1.1  christos       /* Process .debug$S section(s).  */
   3948      1.1  christos 
   3949      1.1  christos       for (asection *s = mod->sections; s; s = s->next)
   3950      1.1  christos 	{
   3951      1.1  christos 	  if (!strcmp (s->name, ".debug$S") && s->size >= sizeof (uint32_t))
   3952      1.1  christos 	    {
   3953      1.1  christos 	      if (!handle_debugs_section (s, mod, strings, &c13_info,
   3954      1.1  christos 					  c13_info_size, mod_source, abfd,
   3955      1.1  christos 					  &syms, sym_byte_size, map, num_types,
   3956      1.1  christos 					  sym_rec_stream, glob, mod_num))
   3957      1.1  christos 		{
   3958      1.1  christos 		  free (c13_info);
   3959      1.1  christos 		  free (syms);
   3960      1.1  christos 		  free (mod_source->files);
   3961      1.1  christos 		  free (map);
   3962      1.1  christos 		  return false;
   3963      1.1  christos 		}
   3964      1.1  christos 	    }
   3965      1.1  christos 	}
   3966      1.1  christos 
   3967      1.1  christos       free (map);
   3968      1.1  christos     }
   3969      1.1  christos 
   3970      1.1  christos   /* Write the signature.  */
   3971      1.1  christos 
   3972      1.1  christos   bfd_putl32 (CV_SIGNATURE_C13, int_buf);
   3973      1.1  christos 
   3974      1.1  christos   if (bfd_write (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
   3975      1.1  christos     {
   3976      1.1  christos       free (c13_info);
   3977      1.1  christos       free (syms);
   3978      1.1  christos       return false;
   3979      1.1  christos     }
   3980      1.1  christos 
   3981      1.1  christos   if (syms)
   3982      1.1  christos     {
   3983      1.1  christos       if (bfd_write (syms, *sym_byte_size, stream) != *sym_byte_size)
   3984      1.1  christos 	{
   3985      1.1  christos 	  free (c13_info);
   3986      1.1  christos 	  free (syms);
   3987      1.1  christos 	  return false;
   3988      1.1  christos 	}
   3989      1.1  christos 
   3990      1.1  christos       free (syms);
   3991      1.1  christos     }
   3992      1.1  christos 
   3993      1.1  christos   if (c13_info)
   3994      1.1  christos     {
   3995      1.1  christos       if (bfd_write (c13_info, *c13_info_size, stream) != *c13_info_size)
   3996      1.1  christos 	{
   3997      1.1  christos 	  free (c13_info);
   3998      1.1  christos 	  return false;
   3999      1.1  christos 	}
   4000      1.1  christos 
   4001      1.1  christos       free (c13_info);
   4002      1.1  christos     }
   4003      1.1  christos 
   4004      1.1  christos   /* Write the global refs size.  */
   4005      1.1  christos 
   4006      1.1  christos   bfd_putl32 (0, int_buf);
   4007      1.1  christos 
   4008      1.1  christos   if (bfd_write (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
   4009      1.1  christos     return false;
   4010      1.1  christos 
   4011      1.1  christos   return true;
   4012      1.1  christos }
   4013      1.1  christos 
   4014      1.1  christos /* Create the module info substream within the DBI.  */
   4015      1.1  christos static bool
   4016      1.1  christos create_module_info_substream (bfd *abfd, bfd *pdb, void **data,
   4017      1.1  christos 			      uint32_t *size, struct string_table *strings,
   4018      1.1  christos 			      struct source_files_info *source,
   4019      1.1  christos 			      struct types *types, struct types *ids,
   4020      1.1  christos 			      bfd *sym_rec_stream, struct globals *glob,
   4021      1.1  christos 			      const char *pdb_name)
   4022      1.1  christos {
   4023      1.1  christos   uint8_t *ptr;
   4024      1.1  christos   unsigned int mod_num;
   4025      1.1  christos 
   4026      1.1  christos   static const char linker_fn[] = "* Linker *";
   4027      1.1  christos 
   4028      1.1  christos   *size = 0;
   4029      1.1  christos 
   4030      1.1  christos   for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
   4031      1.1  christos        in = in->link.next)
   4032      1.1  christos     {
   4033      1.1  christos       size_t len = sizeof (struct module_info);
   4034      1.1  christos 
   4035      1.1  christos       if (!strcmp (bfd_get_filename (in), "dll stuff"))
   4036      1.1  christos 	{
   4037      1.1  christos 	  len += sizeof (linker_fn); /* Object name.  */
   4038      1.1  christos 	  len++; /* Empty module name.  */
   4039      1.1  christos 	}
   4040      1.1  christos       else if (in->my_archive)
   4041      1.1  christos 	{
   4042      1.1  christos 	  char *name = lrealpath (bfd_get_filename (in));
   4043      1.1  christos 
   4044      1.1  christos 	  len += strlen (name) + 1; /* Object name.  */
   4045      1.1  christos 
   4046      1.1  christos 	  free (name);
   4047      1.1  christos 
   4048      1.1  christos 	  name = lrealpath (bfd_get_filename (in->my_archive));
   4049      1.1  christos 
   4050      1.1  christos 	  len += strlen (name) + 1; /* Archive name.  */
   4051      1.1  christos 
   4052      1.1  christos 	  free (name);
   4053      1.1  christos 	}
   4054      1.1  christos       else
   4055      1.1  christos 	{
   4056      1.1  christos 	  char *name = lrealpath (bfd_get_filename (in));
   4057      1.1  christos 	  size_t name_len = strlen (name) + 1;
   4058      1.1  christos 
   4059      1.1  christos 	  len += name_len; /* Object name.  */
   4060      1.1  christos 	  len += name_len; /* And again as the archive name.  */
   4061      1.1  christos 
   4062      1.1  christos 	  free (name);
   4063      1.1  christos 	}
   4064      1.1  christos 
   4065      1.1  christos       if (len % 4)
   4066      1.1  christos 	len += 4 - (len % 4);
   4067      1.1  christos 
   4068      1.1  christos       *size += len;
   4069      1.1  christos 
   4070      1.1  christos       source->mod_count++;
   4071      1.1  christos     }
   4072      1.1  christos 
   4073      1.1  christos   *data = xmalloc (*size);
   4074      1.1  christos 
   4075      1.1  christos   ptr = *data;
   4076      1.1  christos 
   4077      1.1  christos   source->mods = xmalloc (source->mod_count
   4078      1.1  christos 			  * sizeof (struct mod_source_files));
   4079      1.1  christos   memset (source->mods, 0,
   4080      1.1  christos 	  source->mod_count * sizeof (struct mod_source_files));
   4081      1.1  christos 
   4082      1.1  christos   mod_num = 0;
   4083      1.1  christos 
   4084      1.1  christos   for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
   4085      1.1  christos        in = in->link.next)
   4086      1.1  christos     {
   4087      1.1  christos       struct module_info *mod = (struct module_info *) ptr;
   4088      1.1  christos       uint16_t stream_num;
   4089      1.1  christos       bfd *stream;
   4090      1.1  christos       uint32_t sym_byte_size, c13_info_size;
   4091      1.1  christos       uint8_t *start = ptr;
   4092      1.1  christos 
   4093      1.1  christos       stream = add_stream (pdb, NULL, &stream_num);
   4094      1.1  christos 
   4095      1.1  christos       if (!stream)
   4096      1.1  christos 	{
   4097      1.1  christos 	  for (unsigned int i = 0; i < source->mod_count; i++)
   4098      1.1  christos 	    {
   4099      1.1  christos 	      free (source->mods[i].files);
   4100      1.1  christos 	    }
   4101      1.1  christos 
   4102      1.1  christos 	  free (source->mods);
   4103      1.1  christos 	  free (*data);
   4104      1.1  christos 	  return false;
   4105      1.1  christos 	}
   4106      1.1  christos 
   4107      1.1  christos       if (!populate_module_stream (stream, in, &sym_byte_size,
   4108      1.1  christos 				   strings, &c13_info_size,
   4109      1.1  christos 				   &source->mods[mod_num], abfd,
   4110      1.1  christos 				   types, ids, mod_num,
   4111      1.1  christos 				   sym_rec_stream, glob, pdb_name))
   4112      1.1  christos 	{
   4113      1.1  christos 	  for (unsigned int i = 0; i < source->mod_count; i++)
   4114      1.1  christos 	    {
   4115      1.1  christos 	      free (source->mods[i].files);
   4116      1.1  christos 	    }
   4117      1.1  christos 
   4118      1.1  christos 	  free (source->mods);
   4119      1.1  christos 	  free (*data);
   4120      1.1  christos 	  return false;
   4121      1.1  christos 	}
   4122      1.1  christos 
   4123      1.1  christos       bfd_putl32 (0, &mod->unused1);
   4124      1.1  christos 
   4125      1.1  christos       /* These are dummy values - MSVC copies the first section contribution
   4126      1.1  christos 	 entry here, but doesn't seem to use it for anything.  */
   4127      1.1  christos       bfd_putl16 (0xffff, &mod->sc.section);
   4128      1.1  christos       bfd_putl16 (0, &mod->sc.padding1);
   4129      1.1  christos       bfd_putl32 (0, &mod->sc.offset);
   4130      1.1  christos       bfd_putl32 (0xffffffff, &mod->sc.size);
   4131      1.1  christos       bfd_putl32 (0, &mod->sc.characteristics);
   4132      1.1  christos       bfd_putl16 (0xffff, &mod->sc.module_index);
   4133      1.1  christos       bfd_putl16 (0, &mod->sc.padding2);
   4134      1.1  christos       bfd_putl32 (0, &mod->sc.data_crc);
   4135      1.1  christos       bfd_putl32 (0, &mod->sc.reloc_crc);
   4136      1.1  christos 
   4137      1.1  christos       bfd_putl16 (0, &mod->flags);
   4138      1.1  christos       bfd_putl16 (stream_num, &mod->module_sym_stream);
   4139      1.1  christos       bfd_putl32 (sizeof (uint32_t) + sym_byte_size, &mod->sym_byte_size);
   4140      1.1  christos       bfd_putl32 (0, &mod->c11_byte_size);
   4141      1.1  christos       bfd_putl32 (c13_info_size, &mod->c13_byte_size);
   4142      1.1  christos       bfd_putl16 (0, &mod->source_file_count);
   4143      1.1  christos       bfd_putl16 (0, &mod->padding);
   4144      1.1  christos       bfd_putl32 (0, &mod->unused2);
   4145      1.1  christos       bfd_putl32 (0, &mod->source_file_name_index);
   4146      1.1  christos       bfd_putl32 (0, &mod->pdb_file_path_name_index);
   4147      1.1  christos 
   4148      1.1  christos       ptr += sizeof (struct module_info);
   4149      1.1  christos 
   4150      1.1  christos       if (!strcmp (bfd_get_filename (in), "dll stuff"))
   4151      1.1  christos 	{
   4152      1.1  christos 	  /* Object name.  */
   4153      1.1  christos 	  memcpy (ptr, linker_fn, sizeof (linker_fn));
   4154      1.1  christos 	  ptr += sizeof (linker_fn);
   4155      1.1  christos 
   4156      1.1  christos 	  /* Empty module name.  */
   4157      1.1  christos 	  *ptr = 0;
   4158      1.1  christos 	  ptr++;
   4159      1.1  christos 	}
   4160      1.1  christos       else if (in->my_archive)
   4161      1.1  christos 	{
   4162      1.1  christos 	  char *name = lrealpath (bfd_get_filename (in));
   4163      1.1  christos 	  size_t name_len = strlen (name) + 1;
   4164      1.1  christos 
   4165      1.1  christos 	  /* Object name.  */
   4166      1.1  christos 	  memcpy (ptr, name, name_len);
   4167      1.1  christos 	  ptr += name_len;
   4168      1.1  christos 
   4169      1.1  christos 	  free (name);
   4170      1.1  christos 
   4171      1.1  christos 	  name = lrealpath (bfd_get_filename (in->my_archive));
   4172      1.1  christos 	  name_len = strlen (name) + 1;
   4173      1.1  christos 
   4174      1.1  christos 	  /* Archive name.  */
   4175      1.1  christos 	  memcpy (ptr, name, name_len);
   4176      1.1  christos 	  ptr += name_len;
   4177      1.1  christos 
   4178      1.1  christos 	  free (name);
   4179      1.1  christos 	}
   4180      1.1  christos       else
   4181      1.1  christos 	{
   4182      1.1  christos 	  char *name = lrealpath (bfd_get_filename (in));
   4183      1.1  christos 	  size_t name_len = strlen (name) + 1;
   4184      1.1  christos 
   4185      1.1  christos 	  /* Object name.  */
   4186      1.1  christos 	  memcpy (ptr, name, name_len);
   4187      1.1  christos 	  ptr += name_len;
   4188      1.1  christos 
   4189      1.1  christos 	  /* Object name again as archive name.  */
   4190      1.1  christos 	  memcpy (ptr, name, name_len);
   4191      1.1  christos 	  ptr += name_len;
   4192      1.1  christos 
   4193      1.1  christos 	  free (name);
   4194      1.1  christos 	}
   4195      1.1  christos 
   4196      1.1  christos       /* Pad to next four-byte boundary.  */
   4197      1.1  christos 
   4198      1.1  christos       if ((ptr - start) % 4)
   4199      1.1  christos 	{
   4200      1.1  christos 	  memset (ptr, 0, 4 - ((ptr - start) % 4));
   4201      1.1  christos 	  ptr += 4 - ((ptr - start) % 4);
   4202      1.1  christos 	}
   4203      1.1  christos 
   4204      1.1  christos       mod_num++;
   4205      1.1  christos     }
   4206      1.1  christos 
   4207      1.1  christos   return true;
   4208      1.1  christos }
   4209      1.1  christos 
   4210      1.1  christos /* Return the index of a given output section.  */
   4211      1.1  christos static uint16_t
   4212      1.1  christos find_section_number (bfd *abfd, asection *sect)
   4213      1.1  christos {
   4214      1.1  christos   uint16_t i = 1;
   4215      1.1  christos 
   4216      1.1  christos   for (asection *s = abfd->sections; s; s = s->next)
   4217      1.1  christos     {
   4218      1.1  christos       if (s == sect)
   4219      1.1  christos 	return i;
   4220      1.1  christos 
   4221      1.1  christos       /* Empty sections aren't output.  */
   4222      1.1  christos       if (s->size != 0)
   4223      1.1  christos 	i++;
   4224      1.1  christos     }
   4225      1.1  christos 
   4226      1.1  christos   return 0;
   4227      1.1  christos }
   4228      1.1  christos 
   4229      1.1  christos /* Used as parameter to qsort, to sort section contributions by section and
   4230      1.1  christos    offset.  */
   4231      1.1  christos static int
   4232      1.1  christos section_contribs_compare (const void *p1, const void *p2)
   4233      1.1  christos {
   4234      1.1  christos   const struct in_sc *sc1 = p1;
   4235      1.1  christos   const struct in_sc *sc2 = p2;
   4236      1.1  christos 
   4237      1.1  christos   if (sc1->sect_num < sc2->sect_num)
   4238      1.1  christos     return -1;
   4239      1.1  christos   if (sc1->sect_num > sc2->sect_num)
   4240      1.1  christos     return 1;
   4241      1.1  christos 
   4242      1.1  christos   if (sc1->s->output_offset < sc2->s->output_offset)
   4243      1.1  christos     return -1;
   4244      1.1  christos   if (sc1->s->output_offset > sc2->s->output_offset)
   4245      1.1  christos     return 1;
   4246      1.1  christos 
   4247      1.1  christos   return 0;
   4248      1.1  christos }
   4249      1.1  christos 
   4250      1.1  christos /* Create the substream which maps addresses in the image file to locations
   4251      1.1  christos    in the original object files.  */
   4252      1.1  christos static bool
   4253      1.1  christos create_section_contrib_substream (bfd *abfd, void **data, uint32_t *size)
   4254      1.1  christos {
   4255      1.1  christos   unsigned int num_sc = 0;
   4256      1.1  christos   struct section_contribution *sc;
   4257      1.1  christos   uint16_t mod_index;
   4258      1.1  christos   char *sect_flags;
   4259      1.1  christos   file_ptr offset;
   4260      1.1  christos   struct in_sc *sc_in, *sc2;
   4261      1.1  christos   uint32_t *ptr;
   4262      1.1  christos 
   4263      1.1  christos   for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
   4264      1.1  christos        in = in->link.next)
   4265      1.1  christos     {
   4266      1.1  christos       for (asection *s = in->sections; s; s = s->next)
   4267      1.1  christos 	{
   4268      1.1  christos 	  if (s->size == 0 || discarded_section (s))
   4269      1.1  christos 	    continue;
   4270      1.1  christos 
   4271      1.1  christos 	  num_sc++;
   4272      1.1  christos 	}
   4273      1.1  christos     }
   4274      1.1  christos 
   4275      1.1  christos   *size = sizeof (uint32_t) + (num_sc * sizeof (struct section_contribution));
   4276      1.1  christos   *data = xmalloc (*size);
   4277      1.1  christos 
   4278      1.1  christos   bfd_putl32 (SECTION_CONTRIB_VERSION_60, *data);
   4279      1.1  christos 
   4280      1.1  christos   /* Read characteristics of outputted sections.  */
   4281      1.1  christos 
   4282      1.1  christos   sect_flags = xmalloc (sizeof (uint32_t) * abfd->section_count);
   4283      1.1  christos 
   4284      1.1  christos   offset = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
   4285      1.1  christos   offset += offsetof (struct external_scnhdr, s_flags);
   4286      1.1  christos 
   4287      1.1  christos   for (unsigned int i = 0; i < abfd->section_count; i++)
   4288      1.1  christos     {
   4289      1.1  christos       if (bfd_seek (abfd, offset, SEEK_SET) != 0
   4290      1.1  christos 	  || bfd_read (sect_flags + (i * sizeof (uint32_t)), sizeof (uint32_t),
   4291      1.1  christos 		       abfd) != sizeof (uint32_t))
   4292      1.1  christos 	{
   4293      1.1  christos 	  free (*data);
   4294      1.1  christos 	  free (sect_flags);
   4295      1.1  christos 	  return false;
   4296      1.1  christos 	}
   4297      1.1  christos 
   4298      1.1  christos       offset += sizeof (struct external_scnhdr);
   4299      1.1  christos     }
   4300      1.1  christos 
   4301      1.1  christos   /* Microsoft's DIA expects section contributions to be sorted by section
   4302      1.1  christos      number and offset, otherwise it will be unable to resolve line numbers.  */
   4303      1.1  christos 
   4304      1.1  christos   sc_in = xmalloc (num_sc * sizeof (* sc_in));
   4305      1.1  christos   sc2 = sc_in;
   4306      1.1  christos 
   4307      1.1  christos   mod_index = 0;
   4308      1.1  christos   for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
   4309      1.1  christos        in = in->link.next)
   4310      1.1  christos     {
   4311      1.1  christos       for (asection *s = in->sections; s; s = s->next)
   4312      1.1  christos 	{
   4313      1.1  christos 	  if (s->size == 0 || discarded_section (s))
   4314      1.1  christos 	    continue;
   4315      1.1  christos 
   4316      1.1  christos 	  sc2->s = s;
   4317      1.1  christos 	  sc2->sect_num = find_section_number (abfd, s->output_section);
   4318      1.1  christos 	  sc2->mod_index = mod_index;
   4319      1.1  christos 
   4320      1.1  christos 	  sc2++;
   4321      1.1  christos 	}
   4322      1.1  christos 
   4323      1.1  christos       mod_index++;
   4324      1.1  christos     }
   4325      1.1  christos 
   4326      1.1  christos   qsort (sc_in, num_sc, sizeof (* sc_in), section_contribs_compare);
   4327      1.1  christos 
   4328      1.1  christos   ptr = *data;
   4329      1.1  christos   sc = (struct section_contribution *) (ptr + 1); /* Skip the version word.  */
   4330      1.1  christos 
   4331      1.1  christos   for (unsigned int i = 0; i < num_sc; i++)
   4332      1.1  christos     {
   4333      1.1  christos       memcpy (&sc->characteristics,
   4334      1.1  christos 	      sect_flags + ((sc_in[i].sect_num - 1) * sizeof (uint32_t)),
   4335      1.1  christos 	      sizeof (uint32_t));
   4336      1.1  christos 
   4337      1.1  christos       bfd_putl16 (sc_in[i].sect_num, &sc->section);
   4338      1.1  christos       bfd_putl16 (0, &sc->padding1);
   4339      1.1  christos       bfd_putl32 (sc_in[i].s->output_offset, &sc->offset);
   4340      1.1  christos       bfd_putl32 (sc_in[i].s->size, &sc->size);
   4341      1.1  christos       bfd_putl16 (sc_in[i].mod_index, &sc->module_index);
   4342      1.1  christos       bfd_putl16 (0, &sc->padding2);
   4343      1.1  christos       bfd_putl32 (0, &sc->data_crc);
   4344      1.1  christos       bfd_putl32 (0, &sc->reloc_crc);
   4345      1.1  christos 
   4346      1.1  christos       sc++;
   4347      1.1  christos     }
   4348      1.1  christos 
   4349      1.1  christos   free (sc_in);
   4350      1.1  christos   free (sect_flags);
   4351      1.1  christos 
   4352      1.1  christos   return true;
   4353      1.1  christos }
   4354      1.1  christos 
   4355      1.1  christos /* The source info substream lives within the DBI stream, and lists the
   4356      1.1  christos    source files for each object file (i.e. it's derived from the
   4357      1.1  christos    DEBUG_S_FILECHKSMS parts of the .debug$S sections).  This is a bit
   4358      1.1  christos    superfluous, as the filenames are also available in the C13 parts of
   4359      1.1  christos    the module streams, but MSVC relies on it to work properly.  */
   4360      1.1  christos static void
   4361      1.1  christos create_source_info_substream (void **data, uint32_t *size,
   4362      1.1  christos 			      struct source_files_info *source)
   4363      1.1  christos {
   4364      1.1  christos   uint16_t dedupe_source_files_count = 0;
   4365      1.1  christos   uint16_t source_files_count = 0;
   4366      1.1  christos   uint32_t strings_len = 0;
   4367      1.1  christos   uint8_t *ptr;
   4368      1.1  christos 
   4369      1.1  christos   /* Loop through the source files, marking unique filenames.  The pointers
   4370      1.1  christos      here are for entries in the main string table, and so have already
   4371      1.1  christos      been deduplicated.  */
   4372      1.1  christos 
   4373      1.1  christos   for (uint16_t i = 0; i < source->mod_count; i++)
   4374      1.1  christos     {
   4375      1.1  christos       for (uint16_t j = 0; j < source->mods[i].files_count; j++)
   4376      1.1  christos 	{
   4377      1.1  christos 	  if (source->mods[i].files[j])
   4378      1.1  christos 	    {
   4379      1.1  christos 	      if (source->mods[i].files[j]->source_file_offset == 0xffffffff)
   4380      1.1  christos 		{
   4381      1.1  christos 		  source->mods[i].files[j]->source_file_offset = strings_len;
   4382      1.1  christos 		  strings_len += source->mods[i].files[j]->len + 1;
   4383      1.1  christos 		  dedupe_source_files_count++;
   4384      1.1  christos 		}
   4385      1.1  christos 
   4386      1.1  christos 	      source_files_count++;
   4387      1.1  christos 	    }
   4388      1.1  christos 	}
   4389      1.1  christos     }
   4390      1.1  christos 
   4391      1.1  christos   *size = sizeof (uint16_t) + sizeof (uint16_t);
   4392      1.1  christos   *size += (sizeof (uint16_t) + sizeof (uint16_t)) * source->mod_count;
   4393      1.1  christos   *size += sizeof (uint32_t) * source_files_count;
   4394      1.1  christos   *size += strings_len;
   4395      1.1  christos 
   4396      1.1  christos   *data = xmalloc (*size);
   4397      1.1  christos 
   4398      1.1  christos   ptr = (uint8_t *) *data;
   4399      1.1  christos 
   4400      1.1  christos   /* Write header (module count and source file count).  */
   4401      1.1  christos 
   4402      1.1  christos   bfd_putl16 (source->mod_count, ptr);
   4403      1.1  christos   ptr += sizeof (uint16_t);
   4404      1.1  christos 
   4405      1.1  christos   bfd_putl16 (dedupe_source_files_count, ptr);
   4406      1.1  christos   ptr += sizeof (uint16_t);
   4407      1.1  christos 
   4408      1.1  christos   /* Write "ModIndices".  As the LLVM documentation puts it, "this array is
   4409      1.1  christos      present, but does not appear to be useful".  */
   4410      1.1  christos 
   4411      1.1  christos   for (uint16_t i = 0; i < source->mod_count; i++)
   4412      1.1  christos     {
   4413      1.1  christos       bfd_putl16 (i, ptr);
   4414      1.1  christos       ptr += sizeof (uint16_t);
   4415      1.1  christos     }
   4416      1.1  christos 
   4417      1.1  christos   /* Write source file count for each module.  */
   4418      1.1  christos 
   4419      1.1  christos   for (uint16_t i = 0; i < source->mod_count; i++)
   4420      1.1  christos     {
   4421      1.1  christos       bfd_putl16 (source->mods[i].files_count, ptr);
   4422      1.1  christos       ptr += sizeof (uint16_t);
   4423      1.1  christos     }
   4424      1.1  christos 
   4425      1.1  christos   /* For each module, write the offsets within the string table
   4426      1.1  christos      for each source file.  */
   4427      1.1  christos 
   4428      1.1  christos   for (uint16_t i = 0; i < source->mod_count; i++)
   4429      1.1  christos     {
   4430      1.1  christos       for (uint16_t j = 0; j < source->mods[i].files_count; j++)
   4431      1.1  christos 	{
   4432      1.1  christos 	  if (source->mods[i].files[j])
   4433      1.1  christos 	    {
   4434      1.1  christos 	      bfd_putl32 (source->mods[i].files[j]->source_file_offset, ptr);
   4435      1.1  christos 	      ptr += sizeof (uint32_t);
   4436      1.1  christos 	    }
   4437      1.1  christos 	}
   4438      1.1  christos     }
   4439      1.1  christos 
   4440      1.1  christos   /* Write the string table.  We set source_file_offset to a dummy value for
   4441      1.1  christos      each entry we write, so we don't write duplicate filenames.  */
   4442      1.1  christos 
   4443      1.1  christos   for (uint16_t i = 0; i < source->mod_count; i++)
   4444      1.1  christos     {
   4445      1.1  christos       for (uint16_t j = 0; j < source->mods[i].files_count; j++)
   4446      1.1  christos 	{
   4447      1.1  christos 	  if (source->mods[i].files[j]
   4448      1.1  christos 	      && source->mods[i].files[j]->source_file_offset != 0xffffffff)
   4449      1.1  christos 	    {
   4450      1.1  christos 	      memcpy (ptr, source->mods[i].files[j]->s,
   4451      1.1  christos 		      source->mods[i].files[j]->len);
   4452      1.1  christos 	      ptr += source->mods[i].files[j]->len;
   4453      1.1  christos 
   4454      1.1  christos 	      *ptr = 0;
   4455      1.1  christos 	      ptr++;
   4456      1.1  christos 
   4457      1.1  christos 	      source->mods[i].files[j]->source_file_offset = 0xffffffff;
   4458      1.1  christos 	    }
   4459      1.1  christos 	}
   4460      1.1  christos     }
   4461      1.1  christos }
   4462      1.1  christos 
   4463      1.1  christos /* Used as parameter to qsort, to sort globals by hash.  */
   4464      1.1  christos static int
   4465      1.1  christos global_compare_hash (const void *s1, const void *s2)
   4466      1.1  christos {
   4467      1.1  christos   const struct global *g1 = *(const struct global **) s1;
   4468      1.1  christos   const struct global *g2 = *(const struct global **) s2;
   4469      1.1  christos 
   4470      1.1  christos   if (g1->hash < g2->hash)
   4471      1.1  christos     return -1;
   4472      1.1  christos   if (g1->hash > g2->hash)
   4473      1.1  christos     return 1;
   4474      1.1  christos 
   4475      1.1  christos   return 0;
   4476      1.1  christos }
   4477      1.1  christos 
   4478      1.1  christos /* Create the globals stream, which contains the unmangled symbol names.  */
   4479      1.1  christos static bool
   4480      1.1  christos create_globals_stream (bfd *pdb, struct globals *glob, uint16_t *stream_num)
   4481      1.1  christos {
   4482      1.1  christos   bfd *stream;
   4483      1.1  christos   struct globals_hash_header h;
   4484      1.1  christos   uint32_t buckets_size, filled_buckets = 0;
   4485      1.1  christos   struct global **sorted = NULL;
   4486      1.1  christos   bool ret = false;
   4487      1.1  christos   struct global *buckets[NUM_GLOBALS_HASH_BUCKETS];
   4488      1.1  christos   char int_buf[sizeof (uint32_t)];
   4489      1.1  christos 
   4490      1.1  christos   stream = add_stream (pdb, NULL, stream_num);
   4491      1.1  christos   if (!stream)
   4492      1.1  christos     return false;
   4493      1.1  christos 
   4494      1.1  christos   memset (buckets, 0, sizeof (buckets));
   4495      1.1  christos 
   4496      1.1  christos   if (glob->num_entries > 0)
   4497      1.1  christos     {
   4498      1.1  christos       struct global *g;
   4499      1.1  christos 
   4500      1.1  christos       /* Create an array of pointers, sorted by hash value.  */
   4501      1.1  christos 
   4502      1.1  christos       sorted = xmalloc (sizeof (struct global *) * glob->num_entries);
   4503      1.1  christos 
   4504      1.1  christos       g = glob->first;
   4505      1.1  christos       for (unsigned int i = 0; i < glob->num_entries; i++)
   4506      1.1  christos 	{
   4507      1.1  christos 	  sorted[i] = g;
   4508      1.1  christos 	  g = g->next;
   4509      1.1  christos 	}
   4510      1.1  christos 
   4511      1.1  christos       qsort (sorted, glob->num_entries, sizeof (struct global *),
   4512      1.1  christos 	     global_compare_hash);
   4513      1.1  christos 
   4514      1.1  christos       /* Populate the buckets.  */
   4515      1.1  christos 
   4516      1.1  christos       for (unsigned int i = 0; i < glob->num_entries; i++)
   4517      1.1  christos 	{
   4518      1.1  christos 	  if (!buckets[sorted[i]->hash])
   4519      1.1  christos 	    {
   4520      1.1  christos 	      buckets[sorted[i]->hash] = sorted[i];
   4521      1.1  christos 	      filled_buckets++;
   4522      1.1  christos 	    }
   4523      1.1  christos 
   4524      1.1  christos 	  sorted[i]->index = i;
   4525      1.1  christos 	}
   4526      1.1  christos     }
   4527      1.1  christos 
   4528      1.1  christos   buckets_size = NUM_GLOBALS_HASH_BUCKETS / 8;
   4529      1.1  christos   buckets_size += sizeof (uint32_t);
   4530      1.1  christos   buckets_size += filled_buckets * sizeof (uint32_t);
   4531      1.1  christos 
   4532      1.1  christos   bfd_putl32 (GLOBALS_HASH_SIGNATURE, &h.signature);
   4533      1.1  christos   bfd_putl32 (GLOBALS_HASH_VERSION_70, &h.version);
   4534      1.1  christos   bfd_putl32 (glob->num_entries * sizeof (struct hash_record),
   4535      1.1  christos 	      &h.entries_size);
   4536      1.1  christos   bfd_putl32 (buckets_size, &h.buckets_size);
   4537      1.1  christos 
   4538      1.1  christos   if (bfd_write (&h, sizeof (h), stream) != sizeof (h))
   4539      1.1  christos     return false;
   4540      1.1  christos 
   4541      1.1  christos   /* Write hash entries, sorted by hash.  */
   4542      1.1  christos 
   4543      1.1  christos   for (unsigned int i = 0; i < glob->num_entries; i++)
   4544      1.1  christos     {
   4545      1.1  christos       struct hash_record hr;
   4546      1.1  christos 
   4547      1.1  christos       bfd_putl32 (sorted[i]->offset + 1, &hr.offset);
   4548      1.1  christos       bfd_putl32 (sorted[i]->refcount, &hr.reference);
   4549      1.1  christos 
   4550      1.1  christos       if (bfd_write (&hr, sizeof (hr), stream) != sizeof (hr))
   4551      1.1  christos 	goto end;
   4552      1.1  christos     }
   4553      1.1  christos 
   4554      1.1  christos   /* Write the bitmap for filled and unfilled buckets.  */
   4555      1.1  christos 
   4556      1.1  christos   for (unsigned int i = 0; i < NUM_GLOBALS_HASH_BUCKETS; i += 8)
   4557      1.1  christos     {
   4558      1.1  christos       uint8_t v = 0;
   4559      1.1  christos 
   4560      1.1  christos       for (unsigned int j = 0; j < 8; j++)
   4561      1.1  christos 	{
   4562      1.1  christos 	  if (buckets[i + j])
   4563      1.1  christos 	    v |= 1 << j;
   4564      1.1  christos 	}
   4565      1.1  christos 
   4566      1.1  christos       if (bfd_write (&v, sizeof (v), stream) != sizeof (v))
   4567      1.1  christos 	goto end;
   4568      1.1  christos     }
   4569      1.1  christos 
   4570      1.1  christos   /* Add a 4-byte gap.  */
   4571      1.1  christos 
   4572      1.1  christos   bfd_putl32 (0, int_buf);
   4573      1.1  christos 
   4574      1.1  christos   if (bfd_write (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
   4575      1.1  christos     goto end;
   4576      1.1  christos 
   4577      1.1  christos   /* Write the bucket offsets.  */
   4578      1.1  christos 
   4579      1.1  christos   for (unsigned int i = 0; i < NUM_GLOBALS_HASH_BUCKETS; i++)
   4580      1.1  christos     {
   4581      1.1  christos       if (buckets[i])
   4582      1.1  christos 	{
   4583      1.1  christos 	  /* 0xc is size of internal hash_record structure in
   4584      1.1  christos 	     Microsoft's parser.  */
   4585      1.1  christos 	  bfd_putl32 (buckets[i]->index * 0xc, int_buf);
   4586      1.1  christos 
   4587      1.1  christos 	  if (bfd_write (int_buf, sizeof (uint32_t), stream) !=
   4588      1.1  christos 	      sizeof (uint32_t))
   4589      1.1  christos 	    goto end;
   4590      1.1  christos 	}
   4591      1.1  christos     }
   4592      1.1  christos 
   4593      1.1  christos   ret = true;
   4594      1.1  christos 
   4595      1.1  christos end:
   4596      1.1  christos   free (sorted);
   4597      1.1  christos 
   4598      1.1  christos   return ret;
   4599      1.1  christos }
   4600      1.1  christos 
   4601      1.1  christos /* Hash an entry in the globals list.  */
   4602      1.1  christos static hashval_t
   4603      1.1  christos hash_global_entry (const void *p)
   4604      1.1  christos {
   4605      1.1  christos   const struct global *g = (const struct global *) p;
   4606      1.1  christos   uint16_t len = bfd_getl16 (g->data);
   4607      1.1  christos 
   4608      1.1  christos   return iterative_hash (g->data, len, 0);
   4609      1.1  christos }
   4610      1.1  christos 
   4611      1.1  christos /* Compare an entry in the globals list with a symbol.  */
   4612      1.1  christos static int
   4613      1.1  christos eq_global_entry (const void *a, const void *b)
   4614      1.1  christos {
   4615      1.1  christos   const struct global *g = (const struct global *) a;
   4616      1.1  christos   uint16_t len1, len2;
   4617      1.1  christos 
   4618      1.1  christos   len1 = bfd_getl16 (g->data) + sizeof (uint16_t);
   4619      1.1  christos   len2 = bfd_getl16 (b) + sizeof (uint16_t);
   4620      1.1  christos 
   4621      1.1  christos   if (len1 != len2)
   4622      1.1  christos     return 0;
   4623      1.1  christos 
   4624      1.1  christos   return !memcmp (g->data, b, len1);
   4625      1.1  christos }
   4626      1.1  christos 
   4627      1.1  christos /* Stream 4 is the debug information (DBI) stream.  */
   4628      1.1  christos static bool
   4629      1.1  christos populate_dbi_stream (bfd *stream, bfd *abfd, bfd *pdb,
   4630      1.1  christos 		     uint16_t section_header_stream_num,
   4631      1.1  christos 		     uint16_t sym_rec_stream_num,
   4632      1.1  christos 		     uint16_t publics_stream_num,
   4633      1.1  christos 		     struct string_table *strings,
   4634      1.1  christos 		     struct types *types,
   4635      1.1  christos 		     struct types *ids,
   4636      1.1  christos 		     bfd *sym_rec_stream, const char *pdb_name)
   4637      1.1  christos {
   4638      1.1  christos   struct pdb_dbi_stream_header h;
   4639      1.1  christos   struct optional_dbg_header opt;
   4640      1.1  christos   void *mod_info, *sc, *source_info;
   4641      1.1  christos   uint32_t mod_info_size, sc_size, source_info_size;
   4642      1.1  christos   struct source_files_info source;
   4643      1.1  christos   struct globals glob;
   4644      1.1  christos   uint16_t globals_stream_num;
   4645      1.1  christos 
   4646      1.1  christos   source.mod_count = 0;
   4647      1.1  christos   source.mods = NULL;
   4648      1.1  christos 
   4649      1.1  christos   glob.num_entries = 0;
   4650      1.1  christos   glob.first = NULL;
   4651      1.1  christos   glob.last = NULL;
   4652      1.1  christos 
   4653      1.1  christos   glob.hashmap = htab_create_alloc (0, hash_global_entry,
   4654      1.1  christos 				    eq_global_entry, free, xcalloc, free);
   4655      1.1  christos 
   4656      1.1  christos   if (!create_module_info_substream (abfd, pdb, &mod_info, &mod_info_size,
   4657      1.1  christos 				     strings, &source, types, ids,
   4658      1.1  christos 				     sym_rec_stream, &glob, pdb_name))
   4659      1.1  christos     {
   4660      1.1  christos       htab_delete (glob.hashmap);
   4661      1.1  christos       return false;
   4662      1.1  christos     }
   4663      1.1  christos 
   4664      1.1  christos   if (!create_globals_stream (pdb, &glob, &globals_stream_num))
   4665      1.1  christos     {
   4666      1.1  christos       htab_delete (glob.hashmap);
   4667      1.1  christos 
   4668      1.1  christos       for (unsigned int i = 0; i < source.mod_count; i++)
   4669      1.1  christos 	{
   4670      1.1  christos 	  free (source.mods[i].files);
   4671      1.1  christos 	}
   4672      1.1  christos       free (source.mods);
   4673      1.1  christos 
   4674      1.1  christos       free (mod_info);
   4675      1.1  christos       return false;
   4676      1.1  christos     }
   4677      1.1  christos 
   4678      1.1  christos   htab_delete (glob.hashmap);
   4679      1.1  christos 
   4680      1.1  christos   if (!create_section_contrib_substream (abfd, &sc, &sc_size))
   4681      1.1  christos     {
   4682      1.1  christos       for (unsigned int i = 0; i < source.mod_count; i++)
   4683      1.1  christos 	{
   4684      1.1  christos 	  free (source.mods[i].files);
   4685      1.1  christos 	}
   4686      1.1  christos       free (source.mods);
   4687      1.1  christos 
   4688      1.1  christos       free (mod_info);
   4689      1.1  christos       return false;
   4690      1.1  christos     }
   4691      1.1  christos 
   4692      1.1  christos   create_source_info_substream (&source_info, &source_info_size, &source);
   4693      1.1  christos 
   4694      1.1  christos   for (unsigned int i = 0; i < source.mod_count; i++)
   4695      1.1  christos     {
   4696      1.1  christos       free (source.mods[i].files);
   4697      1.1  christos     }
   4698      1.1  christos   free (source.mods);
   4699      1.1  christos 
   4700      1.1  christos   bfd_putl32 (0xffffffff, &h.version_signature);
   4701      1.1  christos   bfd_putl32 (DBI_STREAM_VERSION_70, &h.version_header);
   4702      1.1  christos   bfd_putl32 (1, &h.age);
   4703      1.1  christos   bfd_putl16 (globals_stream_num, &h.global_stream_index);
   4704      1.1  christos   bfd_putl16 (0x8e1d, &h.build_number); // MSVC 14.29
   4705      1.1  christos   bfd_putl16 (publics_stream_num, &h.public_stream_index);
   4706      1.1  christos   bfd_putl16 (0, &h.pdb_dll_version);
   4707      1.1  christos   bfd_putl16 (sym_rec_stream_num, &h.sym_record_stream);
   4708      1.1  christos   bfd_putl16 (0, &h.pdb_dll_rbld);
   4709      1.1  christos   bfd_putl32 (mod_info_size, &h.mod_info_size);
   4710      1.1  christos   bfd_putl32 (sc_size, &h.section_contribution_size);
   4711      1.1  christos   bfd_putl32 (0, &h.section_map_size);
   4712      1.1  christos   bfd_putl32 (source_info_size, &h.source_info_size);
   4713      1.1  christos   bfd_putl32 (0, &h.type_server_map_size);
   4714      1.1  christos   bfd_putl32 (0, &h.mfc_type_server_index);
   4715      1.1  christos   bfd_putl32 (sizeof (opt), &h.optional_dbg_header_size);
   4716      1.1  christos   bfd_putl32 (0, &h.ec_substream_size);
   4717      1.1  christos   bfd_putl16 (0, &h.flags);
   4718      1.1  christos   bfd_putl16 (get_arch_number (abfd), &h.machine);
   4719      1.1  christos   bfd_putl32 (0, &h.padding);
   4720      1.1  christos 
   4721      1.1  christos   if (bfd_write (&h, sizeof (h), stream) != sizeof (h))
   4722      1.1  christos     {
   4723      1.1  christos       free (source_info);
   4724      1.1  christos       free (sc);
   4725      1.1  christos       free (mod_info);
   4726      1.1  christos       return false;
   4727      1.1  christos     }
   4728      1.1  christos 
   4729      1.1  christos   if (bfd_write (mod_info, mod_info_size, stream) != mod_info_size)
   4730      1.1  christos     {
   4731      1.1  christos       free (source_info);
   4732      1.1  christos       free (sc);
   4733      1.1  christos       free (mod_info);
   4734      1.1  christos       return false;
   4735      1.1  christos     }
   4736      1.1  christos 
   4737      1.1  christos   free (mod_info);
   4738      1.1  christos 
   4739      1.1  christos   if (bfd_write (sc, sc_size, stream) != sc_size)
   4740      1.1  christos     {
   4741      1.1  christos       free (source_info);
   4742      1.1  christos       free (sc);
   4743      1.1  christos       return false;
   4744      1.1  christos     }
   4745      1.1  christos 
   4746      1.1  christos   free (sc);
   4747      1.1  christos 
   4748      1.1  christos   if (bfd_write (source_info, source_info_size, stream) != source_info_size)
   4749      1.1  christos     {
   4750      1.1  christos       free (source_info);
   4751      1.1  christos       return false;
   4752      1.1  christos     }
   4753      1.1  christos 
   4754      1.1  christos   free (source_info);
   4755      1.1  christos 
   4756      1.1  christos   bfd_putl16 (0xffff, &opt.fpo_stream);
   4757      1.1  christos   bfd_putl16 (0xffff, &opt.exception_stream);
   4758      1.1  christos   bfd_putl16 (0xffff, &opt.fixup_stream);
   4759      1.1  christos   bfd_putl16 (0xffff, &opt.omap_to_src_stream);
   4760      1.1  christos   bfd_putl16 (0xffff, &opt.omap_from_src_stream);
   4761      1.1  christos   bfd_putl16 (section_header_stream_num, &opt.section_header_stream);
   4762      1.1  christos   bfd_putl16 (0xffff, &opt.token_map_stream);
   4763      1.1  christos   bfd_putl16 (0xffff, &opt.xdata_stream);
   4764      1.1  christos   bfd_putl16 (0xffff, &opt.pdata_stream);
   4765      1.1  christos   bfd_putl16 (0xffff, &opt.new_fpo_stream);
   4766      1.1  christos   bfd_putl16 (0xffff, &opt.orig_section_header_stream);
   4767      1.1  christos 
   4768      1.1  christos   if (bfd_write (&opt, sizeof (opt), stream) != sizeof (opt))
   4769      1.1  christos     return false;
   4770      1.1  christos 
   4771      1.1  christos   return true;
   4772      1.1  christos }
   4773      1.1  christos 
   4774      1.1  christos /* Used as parameter to qsort, to sort publics by hash.  */
   4775      1.1  christos static int
   4776      1.1  christos public_compare_hash (const void *s1, const void *s2)
   4777      1.1  christos {
   4778      1.1  christos   const struct public *p1 = *(const struct public **) s1;
   4779      1.1  christos   const struct public *p2 = *(const struct public **) s2;
   4780      1.1  christos 
   4781      1.1  christos   if (p1->hash < p2->hash)
   4782      1.1  christos     return -1;
   4783      1.1  christos   if (p1->hash > p2->hash)
   4784      1.1  christos     return 1;
   4785      1.1  christos 
   4786      1.1  christos   return 0;
   4787      1.1  christos }
   4788      1.1  christos 
   4789      1.1  christos /* Used as parameter to qsort, to sort publics by address.  */
   4790      1.1  christos static int
   4791      1.1  christos public_compare_addr (const void *s1, const void *s2)
   4792      1.1  christos {
   4793      1.1  christos   const struct public *p1 = *(const struct public **) s1;
   4794      1.1  christos   const struct public *p2 = *(const struct public **) s2;
   4795      1.1  christos 
   4796      1.1  christos   if (p1->section < p2->section)
   4797      1.1  christos     return -1;
   4798      1.1  christos   if (p1->section > p2->section)
   4799      1.1  christos     return 1;
   4800      1.1  christos 
   4801      1.1  christos   if (p1->address < p2->address)
   4802      1.1  christos     return -1;
   4803      1.1  christos   if (p1->address > p2->address)
   4804      1.1  christos     return 1;
   4805      1.1  christos 
   4806      1.1  christos   return 0;
   4807      1.1  christos }
   4808      1.1  christos 
   4809      1.1  christos /* The publics stream is a hash map of S_PUB32 records, which are stored
   4810      1.1  christos    in the symbol record stream.  Each S_PUB32 entry represents a symbol
   4811      1.1  christos    from the point of view of the linker: a section index, an offset within
   4812      1.1  christos    the section, and a mangled name.  Compare with S_GDATA32 and S_GPROC32,
   4813      1.1  christos    which are the same thing but generated by the compiler.  */
   4814      1.1  christos static bool
   4815      1.1  christos populate_publics_stream (bfd *stream, bfd *abfd, bfd *sym_rec_stream)
   4816      1.1  christos {
   4817      1.1  christos   struct publics_header header;
   4818      1.1  christos   struct globals_hash_header hash_header;
   4819      1.1  christos   const unsigned int num_buckets = 4096;
   4820      1.1  christos   unsigned int num_entries = 0, filled_buckets = 0;
   4821      1.1  christos   unsigned int buckets_size, sym_hash_size;
   4822      1.1  christos   char int_buf[sizeof (uint32_t)];
   4823      1.1  christos   struct public *publics_head = NULL, *publics_tail = NULL;
   4824      1.1  christos   struct public **buckets;
   4825      1.1  christos   struct public **sorted = NULL;
   4826      1.1  christos   bool ret = false;
   4827      1.1  christos 
   4828      1.1  christos   buckets = xmalloc (sizeof (struct public *) * num_buckets);
   4829      1.1  christos   memset (buckets, 0, sizeof (struct public *) * num_buckets);
   4830      1.1  christos 
   4831      1.1  christos   /* Loop through the global symbols in our input files, and write S_PUB32
   4832      1.1  christos      records in the symbol record stream for those that make it into the
   4833      1.1  christos      final image.  */
   4834      1.1  christos   for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
   4835      1.1  christos        in = in->link.next)
   4836      1.1  christos     {
   4837      1.1  christos       if (!in->outsymbols)
   4838      1.1  christos 	continue;
   4839      1.1  christos 
   4840      1.1  christos       for (unsigned int i = 0; i < in->symcount; i++)
   4841      1.1  christos 	{
   4842      1.1  christos 	  struct bfd_symbol *sym = in->outsymbols[i];
   4843      1.1  christos 
   4844      1.1  christos 	  if (sym->flags & BSF_GLOBAL)
   4845      1.1  christos 	    {
   4846      1.1  christos 	      struct pubsym ps;
   4847      1.1  christos 	      uint16_t record_length;
   4848      1.1  christos 	      const char *name = sym->name;
   4849      1.1  christos 	      size_t name_len = strlen (name);
   4850      1.1  christos 	      struct public *p = xmalloc (sizeof (struct public));
   4851      1.1  christos 	      unsigned int padding = 0;
   4852      1.1  christos 	      uint16_t section;
   4853      1.1  christos 	      uint32_t flags = 0;
   4854      1.1  christos 
   4855      1.1  christos 	      section =
   4856      1.1  christos 		find_section_number (abfd, sym->section->output_section);
   4857      1.1  christos 
   4858      1.1  christos 	      if (section == 0)
   4859      1.1  christos 		continue;
   4860      1.1  christos 
   4861      1.1  christos 	      p->next = NULL;
   4862      1.1  christos 	      p->offset = bfd_tell (sym_rec_stream);
   4863      1.1  christos 	      p->hash = calc_hash (name, name_len) % num_buckets;
   4864      1.1  christos 	      p->section = section;
   4865      1.1  christos 	      p->address = sym->section->output_offset + sym->value;
   4866      1.1  christos 
   4867      1.1  christos 	      record_length = sizeof (struct pubsym) + name_len + 1;
   4868      1.1  christos 
   4869      1.1  christos 	      if (record_length % 4)
   4870      1.1  christos 		padding = 4 - (record_length % 4);
   4871      1.1  christos 
   4872      1.1  christos 	      /* Assume that all global symbols in executable sections
   4873      1.1  christos 		 are functions.  */
   4874      1.1  christos 	      if (sym->section->flags & SEC_CODE)
   4875      1.1  christos 		flags = PUBSYM_FUNCTION;
   4876      1.1  christos 
   4877      1.1  christos 	      bfd_putl16 (record_length + padding - sizeof (uint16_t),
   4878      1.1  christos 			  &ps.record_length);
   4879      1.1  christos 	      bfd_putl16 (S_PUB32, &ps.record_type);
   4880      1.1  christos 	      bfd_putl32 (flags, &ps.flags);
   4881      1.1  christos 	      bfd_putl32 (p->address, &ps.offset);
   4882      1.1  christos 	      bfd_putl16 (p->section, &ps.section);
   4883      1.1  christos 
   4884      1.1  christos 	      if (bfd_write (&ps, sizeof (struct pubsym), sym_rec_stream) !=
   4885      1.1  christos 		  sizeof (struct pubsym))
   4886      1.1  christos 		goto end;
   4887      1.1  christos 
   4888      1.1  christos 	      if (bfd_write (name, name_len + 1, sym_rec_stream) !=
   4889      1.1  christos 		  name_len + 1)
   4890      1.1  christos 		goto end;
   4891      1.1  christos 
   4892      1.1  christos 	      for (unsigned int j = 0; j < padding; j++)
   4893      1.1  christos 		{
   4894      1.1  christos 		  uint8_t b = 0;
   4895      1.1  christos 
   4896      1.1  christos 		  if (bfd_write (&b, sizeof (uint8_t), sym_rec_stream) !=
   4897      1.1  christos 		      sizeof (uint8_t))
   4898      1.1  christos 		    goto end;
   4899      1.1  christos 		}
   4900      1.1  christos 
   4901      1.1  christos 	      if (!publics_head)
   4902      1.1  christos 		publics_head = p;
   4903      1.1  christos 	      else
   4904      1.1  christos 		publics_tail->next = p;
   4905      1.1  christos 
   4906      1.1  christos 	      publics_tail = p;
   4907      1.1  christos 	      num_entries++;
   4908      1.1  christos 	    }
   4909      1.1  christos 	}
   4910      1.1  christos     }
   4911      1.1  christos 
   4912      1.1  christos 
   4913      1.1  christos   if (num_entries > 0)
   4914      1.1  christos     {
   4915      1.1  christos       /* Create an array of pointers, sorted by hash value.  */
   4916      1.1  christos 
   4917      1.1  christos       sorted = xmalloc (sizeof (struct public *) * num_entries);
   4918      1.1  christos 
   4919      1.1  christos       struct public *p = publics_head;
   4920      1.1  christos       for (unsigned int i = 0; i < num_entries; i++)
   4921      1.1  christos 	{
   4922      1.1  christos 	  sorted[i] = p;
   4923      1.1  christos 	  p = p->next;
   4924      1.1  christos 	}
   4925      1.1  christos 
   4926      1.1  christos       qsort (sorted, num_entries, sizeof (struct public *),
   4927      1.1  christos 	     public_compare_hash);
   4928      1.1  christos 
   4929      1.1  christos       /* Populate the buckets.  */
   4930      1.1  christos 
   4931      1.1  christos       for (unsigned int i = 0; i < num_entries; i++)
   4932      1.1  christos 	{
   4933      1.1  christos 	  if (!buckets[sorted[i]->hash])
   4934      1.1  christos 	    {
   4935      1.1  christos 	      buckets[sorted[i]->hash] = sorted[i];
   4936      1.1  christos 	      filled_buckets++;
   4937      1.1  christos 	    }
   4938      1.1  christos 
   4939      1.1  christos 	  sorted[i]->index = i;
   4940      1.1  christos 	}
   4941      1.1  christos     }
   4942      1.1  christos 
   4943      1.1  christos   buckets_size = num_buckets / 8;
   4944      1.1  christos   buckets_size += sizeof (uint32_t);
   4945      1.1  christos   buckets_size += filled_buckets * sizeof (uint32_t);
   4946      1.1  christos 
   4947      1.1  christos   sym_hash_size = sizeof (hash_header);
   4948      1.1  christos   sym_hash_size += num_entries * sizeof (struct hash_record);
   4949      1.1  christos   sym_hash_size += buckets_size;
   4950      1.1  christos 
   4951      1.1  christos   /* Output the publics header.  */
   4952      1.1  christos 
   4953      1.1  christos   bfd_putl32 (sym_hash_size, &header.sym_hash_size);
   4954      1.1  christos   bfd_putl32 (num_entries * sizeof (uint32_t), &header.addr_map_size);
   4955      1.1  christos   bfd_putl32 (0, &header.num_thunks);
   4956      1.1  christos   bfd_putl32 (0, &header.thunks_size);
   4957      1.1  christos   bfd_putl32 (0, &header.thunk_table);
   4958      1.1  christos   bfd_putl32 (0, &header.thunk_table_offset);
   4959      1.1  christos   bfd_putl32 (0, &header.num_sects);
   4960      1.1  christos 
   4961      1.1  christos   if (bfd_write (&header, sizeof (header), stream) != sizeof (header))
   4962      1.1  christos     goto end;
   4963      1.1  christos 
   4964      1.1  christos   /* Output the global hash header.  */
   4965      1.1  christos 
   4966      1.1  christos   bfd_putl32 (GLOBALS_HASH_SIGNATURE, &hash_header.signature);
   4967      1.1  christos   bfd_putl32 (GLOBALS_HASH_VERSION_70, &hash_header.version);
   4968      1.1  christos   bfd_putl32 (num_entries * sizeof (struct hash_record),
   4969      1.1  christos 	      &hash_header.entries_size);
   4970      1.1  christos   bfd_putl32 (buckets_size, &hash_header.buckets_size);
   4971      1.1  christos 
   4972      1.1  christos   if (bfd_write (&hash_header, sizeof (hash_header), stream) !=
   4973      1.1  christos       sizeof (hash_header))
   4974      1.1  christos     goto end;
   4975      1.1  christos 
   4976      1.1  christos   /* Write the entries in hash order.  */
   4977      1.1  christos 
   4978      1.1  christos   for (unsigned int i = 0; i < num_entries; i++)
   4979      1.1  christos     {
   4980      1.1  christos       struct hash_record hr;
   4981      1.1  christos 
   4982      1.1  christos       bfd_putl32 (sorted[i]->offset + 1, &hr.offset);
   4983      1.1  christos       bfd_putl32 (1, &hr.reference);
   4984      1.1  christos 
   4985      1.1  christos       if (bfd_write (&hr, sizeof (hr), stream) != sizeof (hr))
   4986      1.1  christos 	goto end;
   4987      1.1  christos     }
   4988      1.1  christos 
   4989      1.1  christos   /* Write the bitmap for filled and unfilled buckets.  */
   4990      1.1  christos 
   4991      1.1  christos   for (unsigned int i = 0; i < num_buckets; i += 8)
   4992      1.1  christos     {
   4993      1.1  christos       uint8_t v = 0;
   4994      1.1  christos 
   4995      1.1  christos       for (unsigned int j = 0; j < 8; j++)
   4996      1.1  christos 	{
   4997      1.1  christos 	  if (buckets[i + j])
   4998      1.1  christos 	    v |= 1 << j;
   4999      1.1  christos 	}
   5000      1.1  christos 
   5001      1.1  christos       if (bfd_write (&v, sizeof (v), stream) != sizeof (v))
   5002      1.1  christos 	goto end;
   5003      1.1  christos     }
   5004      1.1  christos 
   5005      1.1  christos   /* Add a 4-byte gap.  */
   5006      1.1  christos 
   5007      1.1  christos   bfd_putl32 (0, int_buf);
   5008      1.1  christos 
   5009      1.1  christos   if (bfd_write (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
   5010      1.1  christos     goto end;
   5011      1.1  christos 
   5012      1.1  christos   /* Write the bucket offsets.  */
   5013      1.1  christos 
   5014      1.1  christos   for (unsigned int i = 0; i < num_buckets; i++)
   5015      1.1  christos     {
   5016      1.1  christos       if (buckets[i])
   5017      1.1  christos 	{
   5018      1.1  christos 	  /* 0xc is size of internal hash_record structure in
   5019      1.1  christos 	     Microsoft's parser.  */
   5020      1.1  christos 	  bfd_putl32 (buckets[i]->index * 0xc, int_buf);
   5021      1.1  christos 
   5022      1.1  christos 	  if (bfd_write (int_buf, sizeof (uint32_t), stream) !=
   5023      1.1  christos 	      sizeof (uint32_t))
   5024      1.1  christos 	    goto end;
   5025      1.1  christos 	}
   5026      1.1  christos     }
   5027      1.1  christos 
   5028      1.1  christos   /* Write the address map: offsets into the symbol record stream of
   5029      1.1  christos      S_PUB32 records, ordered by address.  */
   5030      1.1  christos 
   5031      1.1  christos   if (num_entries > 0)
   5032      1.1  christos     {
   5033      1.1  christos       qsort (sorted, num_entries, sizeof (struct public *),
   5034      1.1  christos 	     public_compare_addr);
   5035      1.1  christos 
   5036      1.1  christos       for (unsigned int i = 0; i < num_entries; i++)
   5037      1.1  christos 	{
   5038      1.1  christos 	  bfd_putl32 (sorted[i]->offset, int_buf);
   5039      1.1  christos 
   5040      1.1  christos 	  if (bfd_write (int_buf, sizeof (uint32_t), stream) !=
   5041      1.1  christos 	      sizeof (uint32_t))
   5042      1.1  christos 	    goto end;
   5043      1.1  christos 	}
   5044      1.1  christos     }
   5045      1.1  christos 
   5046      1.1  christos   ret = true;
   5047      1.1  christos 
   5048      1.1  christos end:
   5049      1.1  christos   free (buckets);
   5050      1.1  christos 
   5051      1.1  christos   while (publics_head)
   5052      1.1  christos     {
   5053      1.1  christos       struct public *p = publics_head->next;
   5054      1.1  christos 
   5055      1.1  christos       free (publics_head);
   5056      1.1  christos       publics_head = p;
   5057      1.1  christos     }
   5058      1.1  christos 
   5059      1.1  christos   free (sorted);
   5060      1.1  christos 
   5061      1.1  christos   return ret;
   5062      1.1  christos }
   5063      1.1  christos 
   5064      1.1  christos /* The section header stream contains a copy of the section headers
   5065      1.1  christos    from the PE file, in the same format.  */
   5066      1.1  christos static bool
   5067      1.1  christos create_section_header_stream (bfd *pdb, bfd *abfd, uint16_t *num)
   5068      1.1  christos {
   5069      1.1  christos   bfd *stream;
   5070      1.1  christos   unsigned int section_count;
   5071      1.1  christos   file_ptr scn_base;
   5072      1.1  christos   size_t len;
   5073      1.1  christos   char *buf;
   5074      1.1  christos 
   5075      1.1  christos   stream = add_stream (pdb, NULL, num);
   5076      1.1  christos   if (!stream)
   5077      1.1  christos     return false;
   5078      1.1  christos 
   5079      1.1  christos   section_count = abfd->section_count;
   5080      1.1  christos 
   5081      1.1  christos   /* Empty sections aren't output.  */
   5082      1.1  christos   for (asection *sect = abfd->sections; sect; sect = sect->next)
   5083      1.1  christos     {
   5084      1.1  christos       if (sect->size == 0)
   5085      1.1  christos 	section_count--;
   5086      1.1  christos     }
   5087      1.1  christos 
   5088      1.1  christos   if (section_count == 0)
   5089      1.1  christos     return true;
   5090      1.1  christos 
   5091      1.1  christos   /* Copy section table from output - it's already been written at this
   5092      1.1  christos      point.  */
   5093      1.1  christos 
   5094      1.1  christos   scn_base = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
   5095      1.1  christos 
   5096      1.1  christos   if (bfd_seek (abfd, scn_base, SEEK_SET) != 0)
   5097      1.1  christos     return false;
   5098      1.1  christos 
   5099      1.1  christos   len = section_count * sizeof (struct external_scnhdr);
   5100      1.1  christos   buf = xmalloc (len);
   5101      1.1  christos 
   5102      1.1  christos   if (bfd_read (buf, len, abfd) != len)
   5103      1.1  christos     {
   5104      1.1  christos       free (buf);
   5105      1.1  christos       return false;
   5106      1.1  christos     }
   5107      1.1  christos 
   5108      1.1  christos   if (bfd_write (buf, len, stream) != len)
   5109      1.1  christos     {
   5110      1.1  christos       free (buf);
   5111      1.1  christos       return false;
   5112      1.1  christos     }
   5113      1.1  christos 
   5114      1.1  christos   free (buf);
   5115      1.1  christos 
   5116      1.1  christos   return true;
   5117      1.1  christos }
   5118      1.1  christos 
   5119      1.1  christos /* Populate the "/names" named stream, which contains the string table.  */
   5120      1.1  christos static bool
   5121      1.1  christos populate_names_stream (bfd *stream, struct string_table *strings)
   5122      1.1  christos {
   5123      1.1  christos   char int_buf[sizeof (uint32_t)];
   5124      1.1  christos   struct string_table_header h;
   5125      1.1  christos   uint32_t num_strings = 0, num_buckets;
   5126      1.1  christos   struct string **buckets;
   5127      1.1  christos 
   5128      1.1  christos   bfd_putl32 (STRING_TABLE_SIGNATURE, &h.signature);
   5129      1.1  christos   bfd_putl32 (STRING_TABLE_VERSION, &h.version);
   5130      1.1  christos 
   5131      1.1  christos   if (bfd_write (&h, sizeof (h), stream) != sizeof (h))
   5132      1.1  christos     return false;
   5133      1.1  christos 
   5134      1.1  christos   bfd_putl32 (strings->strings_len, int_buf);
   5135      1.1  christos 
   5136      1.1  christos   if (bfd_write (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
   5137      1.1  christos     return false;
   5138      1.1  christos 
   5139      1.1  christos   int_buf[0] = 0;
   5140      1.1  christos 
   5141      1.1  christos   if (bfd_write (int_buf, 1, stream) != 1)
   5142      1.1  christos     return false;
   5143      1.1  christos 
   5144      1.1  christos   for (struct string *s = strings->strings_head; s; s = s->next)
   5145      1.1  christos     {
   5146      1.1  christos       if (bfd_write (s->s, s->len, stream) != s->len)
   5147      1.1  christos 	return false;
   5148      1.1  christos 
   5149      1.1  christos       if (bfd_write (int_buf, 1, stream) != 1)
   5150      1.1  christos 	return false;
   5151      1.1  christos 
   5152      1.1  christos       num_strings++;
   5153      1.1  christos     }
   5154      1.1  christos 
   5155      1.1  christos   num_buckets = num_strings * 2;
   5156      1.1  christos 
   5157      1.1  christos   buckets = xmalloc (sizeof (struct string *) * num_buckets);
   5158      1.1  christos   memset (buckets, 0, sizeof (struct string *) * num_buckets);
   5159      1.1  christos 
   5160      1.1  christos   for (struct string *s = strings->strings_head; s; s = s->next)
   5161      1.1  christos     {
   5162      1.1  christos       uint32_t bucket_num = s->hash % num_buckets;
   5163      1.1  christos 
   5164      1.1  christos       while (buckets[bucket_num])
   5165      1.1  christos 	{
   5166      1.1  christos 	  bucket_num++;
   5167      1.1  christos 
   5168      1.1  christos 	  if (bucket_num == num_buckets)
   5169      1.1  christos 	    bucket_num = 0;
   5170      1.1  christos 	}
   5171      1.1  christos 
   5172      1.1  christos       buckets[bucket_num] = s;
   5173      1.1  christos     }
   5174      1.1  christos 
   5175      1.1  christos   bfd_putl32 (num_buckets, int_buf);
   5176      1.1  christos 
   5177      1.1  christos   if (bfd_write (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
   5178      1.1  christos     {
   5179      1.1  christos       free (buckets);
   5180      1.1  christos       return false;
   5181      1.1  christos     }
   5182      1.1  christos 
   5183      1.1  christos   for (unsigned int i = 0; i < num_buckets; i++)
   5184      1.1  christos     {
   5185      1.1  christos       if (buckets[i])
   5186      1.1  christos 	bfd_putl32 (buckets[i]->offset, int_buf);
   5187      1.1  christos       else
   5188      1.1  christos 	bfd_putl32 (0, int_buf);
   5189      1.1  christos 
   5190      1.1  christos       if (bfd_write (int_buf, sizeof (uint32_t), stream) !=
   5191      1.1  christos 	  sizeof (uint32_t))
   5192      1.1  christos 	{
   5193      1.1  christos 	  free (buckets);
   5194      1.1  christos 	  return false;
   5195      1.1  christos 	}
   5196      1.1  christos     }
   5197      1.1  christos 
   5198      1.1  christos   free (buckets);
   5199      1.1  christos 
   5200      1.1  christos   bfd_putl32 (num_strings, int_buf);
   5201      1.1  christos 
   5202      1.1  christos   if (bfd_write (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
   5203      1.1  christos     return false;
   5204      1.1  christos 
   5205      1.1  christos   return true;
   5206      1.1  christos }
   5207      1.1  christos 
   5208      1.1  christos /* Calculate the hash of a type_entry.  */
   5209      1.1  christos static hashval_t
   5210      1.1  christos hash_type_entry (const void *p)
   5211      1.1  christos {
   5212      1.1  christos   const struct type_entry *e = (const struct type_entry *) p;
   5213      1.1  christos   uint16_t size = bfd_getl16 (e->data) + sizeof (uint16_t);
   5214      1.1  christos 
   5215      1.1  christos   return iterative_hash (e->data, size, 0);
   5216      1.1  christos }
   5217      1.1  christos 
   5218      1.1  christos /* Compare a type_entry with a type.  */
   5219      1.1  christos static int
   5220      1.1  christos eq_type_entry (const void *a, const void *b)
   5221      1.1  christos {
   5222      1.1  christos   const struct type_entry *e = (const struct type_entry *) a;
   5223      1.1  christos   uint16_t size_a = bfd_getl16 (e->data);
   5224      1.1  christos   uint16_t size_b = bfd_getl16 (b);
   5225      1.1  christos 
   5226      1.1  christos   if (size_a != size_b)
   5227      1.1  christos     return 0;
   5228      1.1  christos 
   5229      1.1  christos   return memcmp (e->data + sizeof (uint16_t),
   5230      1.1  christos 		 (const uint8_t *) b + sizeof (uint16_t), size_a) == 0;
   5231      1.1  christos }
   5232      1.1  christos 
   5233      1.1  christos /* Create a PDB debugging file for the PE image file abfd with the build ID
   5234      1.1  christos    guid, stored at pdb_name.  */
   5235      1.1  christos bool
   5236      1.1  christos create_pdb_file (bfd *abfd, const char *pdb_name, const unsigned char *guid)
   5237      1.1  christos {
   5238      1.1  christos   bfd *pdb;
   5239      1.1  christos   bool ret = false;
   5240      1.1  christos   bfd *info_stream, *dbi_stream, *names_stream, *sym_rec_stream,
   5241      1.1  christos     *publics_stream, *tpi_stream, *ipi_stream;
   5242      1.1  christos   uint16_t section_header_stream_num, sym_rec_stream_num, publics_stream_num;
   5243      1.1  christos   struct string_table strings;
   5244      1.1  christos   struct types types, ids;
   5245      1.1  christos 
   5246      1.1  christos   pdb = bfd_openw (pdb_name, "pdb");
   5247      1.1  christos   if (!pdb)
   5248      1.1  christos     {
   5249      1.1  christos       einfo (_("%P: warning: cannot create PDB file: %E\n"));
   5250      1.1  christos       return false;
   5251      1.1  christos     }
   5252      1.1  christos 
   5253      1.1  christos   strings.strings_head = NULL;
   5254      1.1  christos   strings.strings_tail = NULL;
   5255      1.1  christos   strings.strings_len = 1;
   5256      1.1  christos   strings.hashmap = htab_create_alloc (0, hash_string_table_entry,
   5257      1.1  christos 				       eq_string_table_entry, free,
   5258      1.1  christos 				       xcalloc, free);
   5259      1.1  christos 
   5260      1.1  christos   bfd_set_format (pdb, bfd_archive);
   5261      1.1  christos 
   5262      1.1  christos   if (!create_old_directory_stream (pdb))
   5263      1.1  christos     {
   5264      1.1  christos       einfo (_("%P: warning: cannot create old directory stream "
   5265      1.1  christos 	       "in PDB file: %E\n"));
   5266      1.1  christos       goto end;
   5267      1.1  christos     }
   5268      1.1  christos 
   5269      1.1  christos   info_stream = add_stream (pdb, NULL, NULL);
   5270      1.1  christos 
   5271      1.1  christos   if (!info_stream)
   5272      1.1  christos     {
   5273      1.1  christos       einfo (_("%P: warning: cannot create info stream "
   5274      1.1  christos 	       "in PDB file: %E\n"));
   5275      1.1  christos       goto end;
   5276      1.1  christos     }
   5277      1.1  christos 
   5278      1.1  christos   tpi_stream = add_stream (pdb, NULL, NULL);
   5279      1.1  christos 
   5280      1.1  christos   if (!tpi_stream)
   5281      1.1  christos     {
   5282      1.1  christos       einfo (_("%P: warning: cannot create TPI stream "
   5283      1.1  christos 	       "in PDB file: %E\n"));
   5284      1.1  christos       goto end;
   5285      1.1  christos     }
   5286      1.1  christos 
   5287      1.1  christos   dbi_stream = add_stream (pdb, NULL, NULL);
   5288      1.1  christos 
   5289      1.1  christos   if (!dbi_stream)
   5290      1.1  christos     {
   5291      1.1  christos       einfo (_("%P: warning: cannot create DBI stream "
   5292      1.1  christos 	       "in PDB file: %E\n"));
   5293      1.1  christos       goto end;
   5294      1.1  christos     }
   5295      1.1  christos 
   5296      1.1  christos   ipi_stream = add_stream (pdb, NULL, NULL);
   5297      1.1  christos 
   5298      1.1  christos   if (!ipi_stream)
   5299      1.1  christos     {
   5300      1.1  christos       einfo (_("%P: warning: cannot create IPI stream "
   5301      1.1  christos 	       "in PDB file: %E\n"));
   5302      1.1  christos       goto end;
   5303      1.1  christos     }
   5304      1.1  christos 
   5305      1.1  christos   names_stream = add_stream (pdb, "/names", NULL);
   5306      1.1  christos 
   5307      1.1  christos   if (!names_stream)
   5308      1.1  christos     {
   5309      1.1  christos       einfo (_("%P: warning: cannot create /names stream "
   5310      1.1  christos 	       "in PDB file: %E\n"));
   5311      1.1  christos       goto end;
   5312      1.1  christos     }
   5313      1.1  christos 
   5314      1.1  christos   sym_rec_stream = add_stream (pdb, NULL, &sym_rec_stream_num);
   5315      1.1  christos 
   5316      1.1  christos   if (!sym_rec_stream)
   5317      1.1  christos     {
   5318      1.1  christos       einfo (_("%P: warning: cannot create symbol record stream "
   5319      1.1  christos 	       "in PDB file: %E\n"));
   5320      1.1  christos       goto end;
   5321      1.1  christos     }
   5322      1.1  christos 
   5323      1.1  christos   publics_stream = add_stream (pdb, NULL, &publics_stream_num);
   5324      1.1  christos 
   5325      1.1  christos   if (!publics_stream)
   5326      1.1  christos     {
   5327      1.1  christos       einfo (_("%P: warning: cannot create publics stream "
   5328      1.1  christos 	       "in PDB file: %E\n"));
   5329      1.1  christos       goto end;
   5330      1.1  christos     }
   5331      1.1  christos 
   5332      1.1  christos   if (!create_section_header_stream (pdb, abfd, &section_header_stream_num))
   5333      1.1  christos     {
   5334      1.1  christos       einfo (_("%P: warning: cannot create section header stream "
   5335      1.1  christos 	       "in PDB file: %E\n"));
   5336      1.1  christos       goto end;
   5337      1.1  christos     }
   5338      1.1  christos 
   5339      1.1  christos   types.num_types = 0;
   5340      1.1  christos   types.hashmap = htab_create_alloc (0, hash_type_entry, eq_type_entry,
   5341      1.1  christos 				     free, xcalloc, free);
   5342      1.1  christos   types.first = types.last = NULL;
   5343      1.1  christos 
   5344      1.1  christos   ids.num_types = 0;
   5345      1.1  christos   ids.hashmap = htab_create_alloc (0, hash_type_entry, eq_type_entry,
   5346      1.1  christos 				   free, xcalloc, free);
   5347      1.1  christos   ids.first = ids.last = NULL;
   5348      1.1  christos 
   5349      1.1  christos   if (!populate_dbi_stream (dbi_stream, abfd, pdb, section_header_stream_num,
   5350      1.1  christos 			    sym_rec_stream_num, publics_stream_num,
   5351      1.1  christos 			    &strings, &types, &ids, sym_rec_stream, pdb_name))
   5352      1.1  christos     {
   5353      1.1  christos       einfo (_("%P: warning: cannot populate DBI stream "
   5354      1.1  christos 	       "in PDB file: %E\n"));
   5355      1.1  christos       htab_delete (types.hashmap);
   5356      1.1  christos       htab_delete (ids.hashmap);
   5357      1.1  christos       goto end;
   5358      1.1  christos     }
   5359      1.1  christos 
   5360      1.1  christos   if (!populate_type_stream (pdb, tpi_stream, &types))
   5361      1.1  christos     {
   5362      1.1  christos       einfo (_("%P: warning: cannot populate TPI stream "
   5363      1.1  christos 	       "in PDB file: %E\n"));
   5364      1.1  christos       htab_delete (types.hashmap);
   5365      1.1  christos       htab_delete (ids.hashmap);
   5366      1.1  christos       goto end;
   5367      1.1  christos     }
   5368      1.1  christos 
   5369      1.1  christos   htab_delete (types.hashmap);
   5370      1.1  christos 
   5371      1.1  christos   if (!populate_type_stream (pdb, ipi_stream, &ids))
   5372      1.1  christos     {
   5373      1.1  christos       einfo (_("%P: warning: cannot populate IPI stream "
   5374      1.1  christos 	       "in PDB file: %E\n"));
   5375      1.1  christos       htab_delete (ids.hashmap);
   5376      1.1  christos       goto end;
   5377      1.1  christos     }
   5378      1.1  christos 
   5379      1.1  christos   htab_delete (ids.hashmap);
   5380      1.1  christos 
   5381      1.1  christos   add_string ("", 0, &strings);
   5382      1.1  christos 
   5383      1.1  christos   if (!populate_names_stream (names_stream, &strings))
   5384      1.1  christos     {
   5385      1.1  christos       einfo (_("%P: warning: cannot populate names stream "
   5386      1.1  christos 	       "in PDB file: %E\n"));
   5387      1.1  christos       goto end;
   5388      1.1  christos     }
   5389      1.1  christos 
   5390      1.1  christos   if (!populate_publics_stream (publics_stream, abfd, sym_rec_stream))
   5391      1.1  christos     {
   5392      1.1  christos       einfo (_("%P: warning: cannot populate publics stream "
   5393      1.1  christos 	       "in PDB file: %E\n"));
   5394      1.1  christos       goto end;
   5395      1.1  christos     }
   5396      1.1  christos 
   5397      1.1  christos   if (!populate_info_stream (pdb, info_stream, guid))
   5398      1.1  christos     {
   5399      1.1  christos       einfo (_("%P: warning: cannot populate info stream "
   5400      1.1  christos 	       "in PDB file: %E\n"));
   5401      1.1  christos       goto end;
   5402      1.1  christos     }
   5403      1.1  christos 
   5404      1.1  christos   ret = true;
   5405      1.1  christos 
   5406      1.1  christos end:
   5407      1.1  christos   bfd_close (pdb);
   5408      1.1  christos 
   5409      1.1  christos   htab_delete (strings.hashmap);
   5410      1.1  christos 
   5411      1.1  christos   return ret;
   5412      1.1  christos }
   5413