Home | History | Annotate | Download | only in make
      1 /*
      2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 
     12 #include <stdarg.h>
     13 #include <stdio.h>
     14 #include <stdlib.h>
     15 #include <string.h>
     16 
     17 #include "vpx_config.h"
     18 #include "vpx/vpx_integer.h"
     19 
     20 typedef enum {
     21   OUTPUT_FMT_PLAIN,
     22   OUTPUT_FMT_RVDS,
     23   OUTPUT_FMT_GAS,
     24 } output_fmt_t;
     25 
     26 int log_msg(const char *fmt, ...) {
     27   int res;
     28   va_list ap;
     29   va_start(ap, fmt);
     30   res = vfprintf(stderr, fmt, ap);
     31   va_end(ap);
     32   return res;
     33 }
     34 
     35 #if defined(__GNUC__) && __GNUC__
     36 #if defined(__MACH__)
     37 
     38 #include <mach-o/loader.h>
     39 #include <mach-o/nlist.h>
     40 
     41 int print_macho_equ(output_fmt_t mode, uint8_t* name, int val) {
     42   switch (mode) {
     43     case OUTPUT_FMT_RVDS:
     44       printf("%-40s EQU %5d\n", name, val);
     45       return 0;
     46     case  OUTPUT_FMT_GAS:
     47       printf(".set %-40s, %5d\n", name, val);
     48       return 0;
     49     default:
     50       log_msg("Unsupported mode: %d", mode);
     51       return 1;
     52   }
     53 }
     54 
     55 int parse_macho(uint8_t *base_buf, size_t sz, output_fmt_t mode) {
     56   int i, j;
     57   struct mach_header header;
     58   uint8_t *buf = base_buf;
     59   int base_data_section = 0;
     60   int bits = 0;
     61 
     62   /* We can read in mach_header for 32 and 64 bit architectures
     63    * because it's identical to mach_header_64 except for the last
     64    * element (uint32_t reserved), which we don't use. Then, when
     65    * we know which architecture we're looking at, increment buf
     66    * appropriately.
     67    */
     68   memcpy(&header, buf, sizeof(struct mach_header));
     69 
     70   if (header.magic == MH_MAGIC) {
     71     if (header.cputype == CPU_TYPE_ARM
     72         || header.cputype == CPU_TYPE_X86) {
     73       bits = 32;
     74       buf += sizeof(struct mach_header);
     75     } else {
     76       log_msg("Bad cputype for object file. Currently only tested for CPU_TYPE_[ARM|X86].\n");
     77       goto bail;
     78     }
     79   } else if (header.magic == MH_MAGIC_64) {
     80     if (header.cputype == CPU_TYPE_X86_64) {
     81       bits = 64;
     82       buf += sizeof(struct mach_header_64);
     83     } else {
     84       log_msg("Bad cputype for object file. Currently only tested for CPU_TYPE_X86_64.\n");
     85       goto bail;
     86     }
     87   } else {
     88     log_msg("Bad magic number for object file. 0x%x or 0x%x expected, 0x%x found.\n",
     89             MH_MAGIC, MH_MAGIC_64, header.magic);
     90     goto bail;
     91   }
     92 
     93   if (header.filetype != MH_OBJECT) {
     94     log_msg("Bad filetype for object file. Currently only tested for MH_OBJECT.\n");
     95     goto bail;
     96   }
     97 
     98   for (i = 0; i < header.ncmds; i++) {
     99     struct load_command lc;
    100 
    101     memcpy(&lc, buf, sizeof(struct load_command));
    102 
    103     if (lc.cmd == LC_SEGMENT) {
    104       uint8_t *seg_buf = buf;
    105       struct section s;
    106       struct segment_command seg_c;
    107 
    108       memcpy(&seg_c, seg_buf, sizeof(struct segment_command));
    109       seg_buf += sizeof(struct segment_command);
    110 
    111       /* Although each section is given it's own offset, nlist.n_value
    112        * references the offset of the first section. This isn't
    113        * apparent without debug information because the offset of the
    114        * data section is the same as the first section. However, with
    115        * debug sections mixed in, the offset of the debug section
    116        * increases but n_value still references the first section.
    117        */
    118       if (seg_c.nsects < 1) {
    119         log_msg("Not enough sections\n");
    120         goto bail;
    121       }
    122 
    123       memcpy(&s, seg_buf, sizeof(struct section));
    124       base_data_section = s.offset;
    125     } else if (lc.cmd == LC_SEGMENT_64) {
    126       uint8_t *seg_buf = buf;
    127       struct section_64 s;
    128       struct segment_command_64 seg_c;
    129 
    130       memcpy(&seg_c, seg_buf, sizeof(struct segment_command_64));
    131       seg_buf += sizeof(struct segment_command_64);
    132 
    133       /* Explanation in LG_SEGMENT */
    134       if (seg_c.nsects < 1) {
    135         log_msg("Not enough sections\n");
    136         goto bail;
    137       }
    138 
    139       memcpy(&s, seg_buf, sizeof(struct section_64));
    140       base_data_section = s.offset;
    141     } else if (lc.cmd == LC_SYMTAB) {
    142       if (base_data_section != 0) {
    143         struct symtab_command sc;
    144         uint8_t *sym_buf = base_buf;
    145         uint8_t *str_buf = base_buf;
    146 
    147         memcpy(&sc, buf, sizeof(struct symtab_command));
    148 
    149         if (sc.cmdsize != sizeof(struct symtab_command)) {
    150           log_msg("Can't find symbol table!\n");
    151           goto bail;
    152         }
    153 
    154         sym_buf += sc.symoff;
    155         str_buf += sc.stroff;
    156 
    157         for (j = 0; j < sc.nsyms; j++) {
    158           /* Location of string is cacluated each time from the
    159            * start of the string buffer.  On darwin the symbols
    160            * are prefixed by "_", so we bump the pointer by 1.
    161            * The target value is defined as an int in *_asm_*_offsets.c,
    162            * which is 4 bytes on all targets we currently use.
    163            */
    164           if (bits == 32) {
    165             struct nlist nl;
    166             int val;
    167 
    168             memcpy(&nl, sym_buf, sizeof(struct nlist));
    169             sym_buf += sizeof(struct nlist);
    170 
    171             memcpy(&val, base_buf + base_data_section + nl.n_value,
    172                    sizeof(val));
    173             print_macho_equ(mode, str_buf + nl.n_un.n_strx + 1, val);
    174           } else { /* if (bits == 64) */
    175             struct nlist_64 nl;
    176             int val;
    177 
    178             memcpy(&nl, sym_buf, sizeof(struct nlist_64));
    179             sym_buf += sizeof(struct nlist_64);
    180 
    181             memcpy(&val, base_buf + base_data_section + nl.n_value,
    182                    sizeof(val));
    183             print_macho_equ(mode, str_buf + nl.n_un.n_strx + 1, val);
    184           }
    185         }
    186       }
    187     }
    188 
    189     buf += lc.cmdsize;
    190   }
    191 
    192   return 0;
    193 bail:
    194   return 1;
    195 
    196 }
    197 
    198 #elif defined(__ELF__)
    199 #include "elf.h"
    200 
    201 #define COPY_STRUCT(dst, buf, ofst, sz) do {\
    202     if(ofst + sizeof((*(dst))) > sz) goto bail;\
    203     memcpy(dst, buf+ofst, sizeof((*(dst))));\
    204   } while(0)
    205 
    206 #define ENDIAN_ASSIGN(val, memb) do {\
    207     if(!elf->le_data) {log_msg("Big Endian data not supported yet!\n");goto bail;}\
    208     (val) = (memb);\
    209   } while(0)
    210 
    211 #define ENDIAN_ASSIGN_IN_PLACE(memb) do {\
    212     ENDIAN_ASSIGN(memb, memb);\
    213   } while(0)
    214 
    215 typedef struct {
    216   uint8_t      *buf; /* Buffer containing ELF data */
    217   size_t        sz;  /* Buffer size */
    218   int           le_data; /* Data is little-endian */
    219   unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */
    220   int           bits; /* 32 or 64 */
    221   Elf32_Ehdr    hdr32;
    222   Elf64_Ehdr    hdr64;
    223 } elf_obj_t;
    224 
    225 int parse_elf_header(elf_obj_t *elf) {
    226   int res;
    227   /* Verify ELF Magic numbers */
    228   COPY_STRUCT(&elf->e_ident, elf->buf, 0, elf->sz);
    229   res = elf->e_ident[EI_MAG0] == ELFMAG0;
    230   res &= elf->e_ident[EI_MAG1] == ELFMAG1;
    231   res &= elf->e_ident[EI_MAG2] == ELFMAG2;
    232   res &= elf->e_ident[EI_MAG3] == ELFMAG3;
    233   res &= elf->e_ident[EI_CLASS] == ELFCLASS32
    234          || elf->e_ident[EI_CLASS] == ELFCLASS64;
    235   res &= elf->e_ident[EI_DATA] == ELFDATA2LSB;
    236 
    237   if (!res) goto bail;
    238 
    239   elf->le_data = elf->e_ident[EI_DATA] == ELFDATA2LSB;
    240 
    241   /* Read in relevant values */
    242   if (elf->e_ident[EI_CLASS] == ELFCLASS32) {
    243     elf->bits = 32;
    244     COPY_STRUCT(&elf->hdr32, elf->buf, 0, elf->sz);
    245 
    246     ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_type);
    247     ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_machine);
    248     ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_version);
    249     ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_entry);
    250     ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_phoff);
    251     ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_shoff);
    252     ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_flags);
    253     ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_ehsize);
    254     ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_phentsize);
    255     ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_phnum);
    256     ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_shentsize);
    257     ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_shnum);
    258     ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_shstrndx);
    259   } else { /* if (elf->e_ident[EI_CLASS] == ELFCLASS64) */
    260     elf->bits = 64;
    261     COPY_STRUCT(&elf->hdr64, elf->buf, 0, elf->sz);
    262 
    263     ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_type);
    264     ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_machine);
    265     ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_version);
    266     ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_entry);
    267     ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_phoff);
    268     ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_shoff);
    269     ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_flags);
    270     ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_ehsize);
    271     ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_phentsize);
    272     ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_phnum);
    273     ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_shentsize);
    274     ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_shnum);
    275     ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_shstrndx);
    276   }
    277 
    278   return 0;
    279 bail:
    280   log_msg("Failed to parse ELF file header");
    281   return 1;
    282 }
    283 
    284 int parse_elf_section(elf_obj_t *elf, int idx, Elf32_Shdr *hdr32, Elf64_Shdr *hdr64) {
    285   if (hdr32) {
    286     if (idx >= elf->hdr32.e_shnum)
    287       goto bail;
    288 
    289     COPY_STRUCT(hdr32, elf->buf, elf->hdr32.e_shoff + idx * elf->hdr32.e_shentsize,
    290                 elf->sz);
    291     ENDIAN_ASSIGN_IN_PLACE(hdr32->sh_name);
    292     ENDIAN_ASSIGN_IN_PLACE(hdr32->sh_type);
    293     ENDIAN_ASSIGN_IN_PLACE(hdr32->sh_flags);
    294     ENDIAN_ASSIGN_IN_PLACE(hdr32->sh_addr);
    295     ENDIAN_ASSIGN_IN_PLACE(hdr32->sh_offset);
    296     ENDIAN_ASSIGN_IN_PLACE(hdr32->sh_size);
    297     ENDIAN_ASSIGN_IN_PLACE(hdr32->sh_link);
    298     ENDIAN_ASSIGN_IN_PLACE(hdr32->sh_info);
    299     ENDIAN_ASSIGN_IN_PLACE(hdr32->sh_addralign);
    300     ENDIAN_ASSIGN_IN_PLACE(hdr32->sh_entsize);
    301   } else { /* if (hdr64) */
    302     if (idx >= elf->hdr64.e_shnum)
    303       goto bail;
    304 
    305     COPY_STRUCT(hdr64, elf->buf, elf->hdr64.e_shoff + idx * elf->hdr64.e_shentsize,
    306                 elf->sz);
    307     ENDIAN_ASSIGN_IN_PLACE(hdr64->sh_name);
    308     ENDIAN_ASSIGN_IN_PLACE(hdr64->sh_type);
    309     ENDIAN_ASSIGN_IN_PLACE(hdr64->sh_flags);
    310     ENDIAN_ASSIGN_IN_PLACE(hdr64->sh_addr);
    311     ENDIAN_ASSIGN_IN_PLACE(hdr64->sh_offset);
    312     ENDIAN_ASSIGN_IN_PLACE(hdr64->sh_size);
    313     ENDIAN_ASSIGN_IN_PLACE(hdr64->sh_link);
    314     ENDIAN_ASSIGN_IN_PLACE(hdr64->sh_info);
    315     ENDIAN_ASSIGN_IN_PLACE(hdr64->sh_addralign);
    316     ENDIAN_ASSIGN_IN_PLACE(hdr64->sh_entsize);
    317   }
    318 
    319   return 0;
    320 bail:
    321   return 1;
    322 }
    323 
    324 char *parse_elf_string_table(elf_obj_t *elf, int s_idx, int idx) {
    325   if (elf->bits == 32) {
    326     Elf32_Shdr shdr;
    327 
    328     if (parse_elf_section(elf, s_idx, &shdr, NULL)) {
    329       log_msg("Failed to parse ELF string table: section %d, index %d\n",
    330               s_idx, idx);
    331       return "";
    332     }
    333 
    334     return (char *)(elf->buf + shdr.sh_offset + idx);
    335   } else { /* if (elf->bits == 64) */
    336     Elf64_Shdr shdr;
    337 
    338     if (parse_elf_section(elf, s_idx, NULL, &shdr)) {
    339       log_msg("Failed to parse ELF string table: section %d, index %d\n",
    340               s_idx, idx);
    341       return "";
    342     }
    343 
    344     return (char *)(elf->buf + shdr.sh_offset + idx);
    345   }
    346 }
    347 
    348 int parse_elf_symbol(elf_obj_t *elf, unsigned int ofst, Elf32_Sym *sym32, Elf64_Sym *sym64) {
    349   if (sym32) {
    350     COPY_STRUCT(sym32, elf->buf, ofst, elf->sz);
    351     ENDIAN_ASSIGN_IN_PLACE(sym32->st_name);
    352     ENDIAN_ASSIGN_IN_PLACE(sym32->st_value);
    353     ENDIAN_ASSIGN_IN_PLACE(sym32->st_size);
    354     ENDIAN_ASSIGN_IN_PLACE(sym32->st_info);
    355     ENDIAN_ASSIGN_IN_PLACE(sym32->st_other);
    356     ENDIAN_ASSIGN_IN_PLACE(sym32->st_shndx);
    357   } else { /* if (sym64) */
    358     COPY_STRUCT(sym64, elf->buf, ofst, elf->sz);
    359     ENDIAN_ASSIGN_IN_PLACE(sym64->st_name);
    360     ENDIAN_ASSIGN_IN_PLACE(sym64->st_value);
    361     ENDIAN_ASSIGN_IN_PLACE(sym64->st_size);
    362     ENDIAN_ASSIGN_IN_PLACE(sym64->st_info);
    363     ENDIAN_ASSIGN_IN_PLACE(sym64->st_other);
    364     ENDIAN_ASSIGN_IN_PLACE(sym64->st_shndx);
    365   }
    366   return 0;
    367 bail:
    368   return 1;
    369 }
    370 
    371 int parse_elf(uint8_t *buf, size_t sz, output_fmt_t mode) {
    372   elf_obj_t    elf;
    373   unsigned int ofst;
    374   int          i;
    375   Elf32_Off    strtab_off32;
    376   Elf64_Off    strtab_off64; /* save String Table offset for later use */
    377 
    378   memset(&elf, 0, sizeof(elf));
    379   elf.buf = buf;
    380   elf.sz = sz;
    381 
    382   /* Parse Header */
    383   if (parse_elf_header(&elf))
    384     goto bail;
    385 
    386   if (elf.bits == 32) {
    387     Elf32_Shdr shdr;
    388     for (i = 0; i < elf.hdr32.e_shnum; i++) {
    389       parse_elf_section(&elf, i, &shdr, NULL);
    390 
    391       if (shdr.sh_type == SHT_STRTAB) {
    392         char strtsb_name[128];
    393 
    394         strcpy(strtsb_name, (char *)(elf.buf + shdr.sh_offset + shdr.sh_name));
    395 
    396         if (!(strcmp(strtsb_name, ".shstrtab"))) {
    397           /* log_msg("found section: %s\n", strtsb_name); */
    398           strtab_off32 = shdr.sh_offset;
    399           break;
    400         }
    401       }
    402     }
    403   } else { /* if (elf.bits == 64) */
    404     Elf64_Shdr shdr;
    405     for (i = 0; i < elf.hdr64.e_shnum; i++) {
    406       parse_elf_section(&elf, i, NULL, &shdr);
    407 
    408       if (shdr.sh_type == SHT_STRTAB) {
    409         char strtsb_name[128];
    410 
    411         strcpy(strtsb_name, (char *)(elf.buf + shdr.sh_offset + shdr.sh_name));
    412 
    413         if (!(strcmp(strtsb_name, ".shstrtab"))) {
    414           /* log_msg("found section: %s\n", strtsb_name); */
    415           strtab_off64 = shdr.sh_offset;
    416           break;
    417         }
    418       }
    419     }
    420   }
    421 
    422   /* Parse all Symbol Tables */
    423   if (elf.bits == 32) {
    424     Elf32_Shdr shdr;
    425     for (i = 0; i < elf.hdr32.e_shnum; i++) {
    426       parse_elf_section(&elf, i, &shdr, NULL);
    427 
    428       if (shdr.sh_type == SHT_SYMTAB) {
    429         for (ofst = shdr.sh_offset;
    430              ofst < shdr.sh_offset + shdr.sh_size;
    431              ofst += shdr.sh_entsize) {
    432           Elf32_Sym sym;
    433 
    434           parse_elf_symbol(&elf, ofst, &sym, NULL);
    435 
    436           /* For all OBJECTS (data objects), extract the value from the
    437            * proper data segment.
    438            */
    439           /* if (ELF32_ST_TYPE(sym.st_info) == STT_OBJECT && sym.st_name)
    440               log_msg("found data object %s\n",
    441                       parse_elf_string_table(&elf,
    442                                              shdr.sh_link,
    443                                              sym.st_name));
    444            */
    445 
    446           if (ELF32_ST_TYPE(sym.st_info) == STT_OBJECT
    447               && sym.st_size == 4) {
    448             Elf32_Shdr dhdr;
    449             int val = 0;
    450             char section_name[128];
    451 
    452             parse_elf_section(&elf, sym.st_shndx, &dhdr, NULL);
    453 
    454             /* For explanition - refer to _MSC_VER version of code */
    455             strcpy(section_name, (char *)(elf.buf + strtab_off32 + dhdr.sh_name));
    456             /* log_msg("Section_name: %s, Section_type: %d\n", section_name, dhdr.sh_type); */
    457 
    458             if (strcmp(section_name, ".bss")) {
    459               if (sizeof(val) != sym.st_size) {
    460                 /* The target value is declared as an int in
    461                  * *_asm_*_offsets.c, which is 4 bytes on all
    462                  * targets we currently use. Complain loudly if
    463                  * this is not true.
    464                  */
    465                 log_msg("Symbol size is wrong\n");
    466                 goto bail;
    467               }
    468 
    469               memcpy(&val,
    470                      elf.buf + dhdr.sh_offset + sym.st_value,
    471                      sym.st_size);
    472             }
    473 
    474             if (!elf.le_data) {
    475               log_msg("Big Endian data not supported yet!\n");
    476               goto bail;
    477             }
    478 
    479             switch (mode) {
    480               case OUTPUT_FMT_RVDS:
    481                 printf("%-40s EQU %5d\n",
    482                        parse_elf_string_table(&elf,
    483                                               shdr.sh_link,
    484                                               sym.st_name),
    485                        val);
    486                 break;
    487               case OUTPUT_FMT_GAS:
    488                 printf(".equ %-40s, %5d\n",
    489                        parse_elf_string_table(&elf,
    490                                               shdr.sh_link,
    491                                               sym.st_name),
    492                        val);
    493                 break;
    494               default:
    495                 printf("%s = %d\n",
    496                        parse_elf_string_table(&elf,
    497                                               shdr.sh_link,
    498                                               sym.st_name),
    499                        val);
    500             }
    501           }
    502         }
    503       }
    504     }
    505   } else { /* if (elf.bits == 64) */
    506     Elf64_Shdr shdr;
    507     for (i = 0; i < elf.hdr64.e_shnum; i++) {
    508       parse_elf_section(&elf, i, NULL, &shdr);
    509 
    510       if (shdr.sh_type == SHT_SYMTAB) {
    511         for (ofst = shdr.sh_offset;
    512              ofst < shdr.sh_offset + shdr.sh_size;
    513              ofst += shdr.sh_entsize) {
    514           Elf64_Sym sym;
    515 
    516           parse_elf_symbol(&elf, ofst, NULL, &sym);
    517 
    518           /* For all OBJECTS (data objects), extract the value from the
    519            * proper data segment.
    520            */
    521           /* if (ELF64_ST_TYPE(sym.st_info) == STT_OBJECT && sym.st_name)
    522               log_msg("found data object %s\n",
    523                       parse_elf_string_table(&elf,
    524                                              shdr.sh_link,
    525                                              sym.st_name));
    526            */
    527 
    528           if (ELF64_ST_TYPE(sym.st_info) == STT_OBJECT
    529               && sym.st_size == 4) {
    530             Elf64_Shdr dhdr;
    531             int val = 0;
    532             char section_name[128];
    533 
    534             parse_elf_section(&elf, sym.st_shndx, NULL, &dhdr);
    535 
    536             /* For explanition - refer to _MSC_VER version of code */
    537             strcpy(section_name, (char *)(elf.buf + strtab_off64 + dhdr.sh_name));
    538             /* log_msg("Section_name: %s, Section_type: %d\n", section_name, dhdr.sh_type); */
    539 
    540             if ((strcmp(section_name, ".bss"))) {
    541               if (sizeof(val) != sym.st_size) {
    542                 /* The target value is declared as an int in
    543                  * *_asm_*_offsets.c, which is 4 bytes on all
    544                  * targets we currently use. Complain loudly if
    545                  * this is not true.
    546                  */
    547                 log_msg("Symbol size is wrong\n");
    548                 goto bail;
    549               }
    550 
    551               memcpy(&val,
    552                      elf.buf + dhdr.sh_offset + sym.st_value,
    553                      sym.st_size);
    554             }
    555 
    556             if (!elf.le_data) {
    557               log_msg("Big Endian data not supported yet!\n");
    558               goto bail;
    559             }
    560 
    561             switch (mode) {
    562               case OUTPUT_FMT_RVDS:
    563                 printf("%-40s EQU %5d\n",
    564                        parse_elf_string_table(&elf,
    565                                               shdr.sh_link,
    566                                               sym.st_name),
    567                        val);
    568                 break;
    569               case OUTPUT_FMT_GAS:
    570                 printf(".equ %-40s, %5d\n",
    571                        parse_elf_string_table(&elf,
    572                                               shdr.sh_link,
    573                                               sym.st_name),
    574                        val);
    575                 break;
    576               default:
    577                 printf("%s = %d\n",
    578                        parse_elf_string_table(&elf,
    579                                               shdr.sh_link,
    580                                               sym.st_name),
    581                        val);
    582             }
    583           }
    584         }
    585       }
    586     }
    587   }
    588 
    589   if (mode == OUTPUT_FMT_RVDS)
    590     printf("    END\n");
    591 
    592   return 0;
    593 bail:
    594   log_msg("Parse error: File does not appear to be valid ELF32 or ELF64\n");
    595   return 1;
    596 }
    597 
    598 #endif
    599 #endif /* defined(__GNUC__) && __GNUC__ */
    600 
    601 
    602 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN__)
    603 /*  See "Microsoft Portable Executable and Common Object File Format Specification"
    604     for reference.
    605 */
    606 #define get_le32(x) ((*(x)) | (*(x+1)) << 8 |(*(x+2)) << 16 | (*(x+3)) << 24 )
    607 #define get_le16(x) ((*(x)) | (*(x+1)) << 8)
    608 
    609 int parse_coff(uint8_t *buf, size_t sz) {
    610   unsigned int nsections, symtab_ptr, symtab_sz, strtab_ptr;
    611   unsigned int sectionrawdata_ptr;
    612   unsigned int i;
    613   uint8_t *ptr;
    614   uint32_t symoffset;
    615 
    616   char **sectionlist;  // this array holds all section names in their correct order.
    617   // it is used to check if the symbol is in .bss or .rdata section.
    618 
    619   nsections = get_le16(buf + 2);
    620   symtab_ptr = get_le32(buf + 8);
    621   symtab_sz = get_le32(buf + 12);
    622   strtab_ptr = symtab_ptr + symtab_sz * 18;
    623 
    624   if (nsections > 96) {
    625     log_msg("Too many sections\n");
    626     return 1;
    627   }
    628 
    629   sectionlist = malloc(nsections * sizeof(sectionlist));
    630 
    631   if (sectionlist == NULL) {
    632     log_msg("Allocating first level of section list failed\n");
    633     return 1;
    634   }
    635 
    636   // log_msg("COFF: Found %u symbols in %u sections.\n", symtab_sz, nsections);
    637 
    638   /*
    639   The size of optional header is always zero for an obj file. So, the section header
    640   follows the file header immediately.
    641   */
    642 
    643   ptr = buf + 20;     // section header
    644 
    645   for (i = 0; i < nsections; i++) {
    646     char sectionname[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
    647     strncpy(sectionname, ptr, 8);
    648     // log_msg("COFF: Parsing section %s\n",sectionname);
    649 
    650     sectionlist[i] = malloc(strlen(sectionname) + 1);
    651 
    652     if (sectionlist[i] == NULL) {
    653       log_msg("Allocating storage for %s failed\n", sectionname);
    654       goto bail;
    655     }
    656     strcpy(sectionlist[i], sectionname);
    657 
    658     if (!strcmp(sectionname, ".rdata")) sectionrawdata_ptr = get_le32(ptr + 20);
    659 
    660     ptr += 40;
    661   }
    662 
    663   // log_msg("COFF: Symbol table at offset %u\n", symtab_ptr);
    664   // log_msg("COFF: raw data pointer ofset for section .rdata is %u\n", sectionrawdata_ptr);
    665 
    666   /*  The compiler puts the data with non-zero offset in .rdata section, but puts the data with
    667       zero offset in .bss section. So, if the data in in .bss section, set offset=0.
    668       Note from Wiki: In an object module compiled from C, the bss section contains
    669       the local variables (but not functions) that were declared with the static keyword,
    670       except for those with non-zero initial values. (In C, static variables are initialized
    671       to zero by default.) It also contains the non-local (both extern and static) variables
    672       that are also initialized to zero (either explicitly or by default).
    673       */
    674   // move to symbol table
    675   /* COFF symbol table:
    676       offset      field
    677       0           Name(*)
    678       8           Value
    679       12          SectionNumber
    680       14          Type
    681       16          StorageClass
    682       17          NumberOfAuxSymbols
    683       */
    684   ptr = buf + symtab_ptr;
    685 
    686   for (i = 0; i < symtab_sz; i++) {
    687     int16_t section = get_le16(ptr + 12); // section number
    688 
    689     if (section > 0 && ptr[16] == 2) {
    690       // if(section > 0 && ptr[16] == 3 && get_le32(ptr+8)) {
    691 
    692       if (get_le32(ptr)) {
    693         char name[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
    694         strncpy(name, ptr, 8);
    695         // log_msg("COFF: Parsing symbol %s\n",name);
    696         /* The 64bit Windows compiler doesn't prefix with an _.
    697          * Check what's there, and bump if necessary
    698          */
    699         if (name[0] == '_')
    700           printf("%-40s EQU ", name + 1);
    701         else
    702           printf("%-40s EQU ", name);
    703       } else {
    704         // log_msg("COFF: Parsing symbol %s\n",
    705         //        buf + strtab_ptr + get_le32(ptr+4));
    706         if ((buf + strtab_ptr + get_le32(ptr + 4))[0] == '_')
    707           printf("%-40s EQU ",
    708                  buf + strtab_ptr + get_le32(ptr + 4) + 1);
    709         else
    710           printf("%-40s EQU ", buf + strtab_ptr + get_le32(ptr + 4));
    711       }
    712 
    713       if (!(strcmp(sectionlist[section - 1], ".bss"))) {
    714         symoffset = 0;
    715       } else {
    716         symoffset = get_le32(buf + sectionrawdata_ptr + get_le32(ptr + 8));
    717       }
    718 
    719       // log_msg("      Section: %d\n",section);
    720       // log_msg("      Class:   %d\n",ptr[16]);
    721       // log_msg("      Address: %u\n",get_le32(ptr+8));
    722       // log_msg("      Offset: %u\n", symoffset);
    723 
    724       printf("%5d\n", symoffset);
    725     }
    726 
    727     ptr += 18;
    728   }
    729 
    730   printf("    END\n");
    731 
    732   for (i = 0; i < nsections; i++) {
    733     free(sectionlist[i]);
    734   }
    735 
    736   free(sectionlist);
    737 
    738   return 0;
    739 bail:
    740 
    741   for (i = 0; i < nsections; i++) {
    742     free(sectionlist[i]);
    743   }
    744 
    745   free(sectionlist);
    746 
    747   return 1;
    748 }
    749 #endif /* defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN__) */
    750 
    751 int main(int argc, char **argv) {
    752   output_fmt_t mode = OUTPUT_FMT_PLAIN;
    753   const char *f;
    754   uint8_t *file_buf;
    755   int res;
    756   FILE *fp;
    757   long int file_size;
    758 
    759   if (argc < 2 || argc > 3) {
    760     fprintf(stderr, "Usage: %s [output format] <obj file>\n\n", argv[0]);
    761     fprintf(stderr, "  <obj file>\tobject file to parse\n");
    762     fprintf(stderr, "Output Formats:\n");
    763     fprintf(stderr, "  gas  - compatible with GNU assembler\n");
    764     fprintf(stderr, "  rvds - compatible with armasm\n");
    765     goto bail;
    766   }
    767 
    768   f = argv[2];
    769 
    770   if (!strcmp(argv[1], "rvds"))
    771     mode = OUTPUT_FMT_RVDS;
    772   else if (!strcmp(argv[1], "gas"))
    773     mode = OUTPUT_FMT_GAS;
    774   else
    775     f = argv[1];
    776 
    777   fp = fopen(f, "rb");
    778 
    779   if (!fp) {
    780     perror("Unable to open file");
    781     goto bail;
    782   }
    783 
    784   if (fseek(fp, 0, SEEK_END)) {
    785     perror("stat");
    786     goto bail;
    787   }
    788 
    789   file_size = ftell(fp);
    790   file_buf = malloc(file_size);
    791 
    792   if (!file_buf) {
    793     perror("malloc");
    794     goto bail;
    795   }
    796 
    797   rewind(fp);
    798 
    799   if (fread(file_buf, sizeof(char), file_size, fp) != file_size) {
    800     perror("read");
    801     goto bail;
    802   }
    803 
    804   if (fclose(fp)) {
    805     perror("close");
    806     goto bail;
    807   }
    808 
    809 #if defined(__GNUC__) && __GNUC__
    810 #if defined(__MACH__)
    811   res = parse_macho(file_buf, file_size, mode);
    812 #elif defined(__ELF__)
    813   res = parse_elf(file_buf, file_size, mode);
    814 #endif
    815 #endif
    816 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN__)
    817   res = parse_coff(file_buf, file_size);
    818 #endif
    819 
    820   free(file_buf);
    821 
    822   if (!res)
    823     return EXIT_SUCCESS;
    824 
    825 bail:
    826   return EXIT_FAILURE;
    827 }
    828