michael@0: /* michael@0: * Copyright (c) 2010 The WebM project authors. All Rights Reserved. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license michael@0: * that can be found in the LICENSE file in the root of the source michael@0: * tree. An additional intellectual property rights grant can be found michael@0: * in the file PATENTS. All contributing project authors may michael@0: * be found in the AUTHORS file in the root of the source tree. michael@0: */ michael@0: michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "vpx_config.h" michael@0: #include "vpx/vpx_integer.h" michael@0: michael@0: typedef enum { michael@0: OUTPUT_FMT_PLAIN, michael@0: OUTPUT_FMT_RVDS, michael@0: OUTPUT_FMT_GAS, michael@0: } output_fmt_t; michael@0: michael@0: int log_msg(const char *fmt, ...) { michael@0: int res; michael@0: va_list ap; michael@0: va_start(ap, fmt); michael@0: res = vfprintf(stderr, fmt, ap); michael@0: va_end(ap); michael@0: return res; michael@0: } michael@0: michael@0: #if defined(__GNUC__) && __GNUC__ michael@0: #if defined(__MACH__) michael@0: michael@0: #include michael@0: #include michael@0: michael@0: int print_macho_equ(output_fmt_t mode, uint8_t* name, int val) { michael@0: switch (mode) { michael@0: case OUTPUT_FMT_RVDS: michael@0: printf("%-40s EQU %5d\n", name, val); michael@0: return 0; michael@0: case OUTPUT_FMT_GAS: michael@0: printf(".set %-40s, %5d\n", name, val); michael@0: return 0; michael@0: default: michael@0: log_msg("Unsupported mode: %d", mode); michael@0: return 1; michael@0: } michael@0: } michael@0: michael@0: int parse_macho(uint8_t *base_buf, size_t sz, output_fmt_t mode) { michael@0: int i, j; michael@0: struct mach_header header; michael@0: uint8_t *buf = base_buf; michael@0: int base_data_section = 0; michael@0: int bits = 0; michael@0: michael@0: /* We can read in mach_header for 32 and 64 bit architectures michael@0: * because it's identical to mach_header_64 except for the last michael@0: * element (uint32_t reserved), which we don't use. Then, when michael@0: * we know which architecture we're looking at, increment buf michael@0: * appropriately. michael@0: */ michael@0: memcpy(&header, buf, sizeof(struct mach_header)); michael@0: michael@0: if (header.magic == MH_MAGIC) { michael@0: if (header.cputype == CPU_TYPE_ARM michael@0: || header.cputype == CPU_TYPE_X86) { michael@0: bits = 32; michael@0: buf += sizeof(struct mach_header); michael@0: } else { michael@0: log_msg("Bad cputype for object file. Currently only tested for CPU_TYPE_[ARM|X86].\n"); michael@0: goto bail; michael@0: } michael@0: } else if (header.magic == MH_MAGIC_64) { michael@0: if (header.cputype == CPU_TYPE_X86_64) { michael@0: bits = 64; michael@0: buf += sizeof(struct mach_header_64); michael@0: } else { michael@0: log_msg("Bad cputype for object file. Currently only tested for CPU_TYPE_X86_64.\n"); michael@0: goto bail; michael@0: } michael@0: } else { michael@0: log_msg("Bad magic number for object file. 0x%x or 0x%x expected, 0x%x found.\n", michael@0: MH_MAGIC, MH_MAGIC_64, header.magic); michael@0: goto bail; michael@0: } michael@0: michael@0: if (header.filetype != MH_OBJECT) { michael@0: log_msg("Bad filetype for object file. Currently only tested for MH_OBJECT.\n"); michael@0: goto bail; michael@0: } michael@0: michael@0: for (i = 0; i < header.ncmds; i++) { michael@0: struct load_command lc; michael@0: michael@0: memcpy(&lc, buf, sizeof(struct load_command)); michael@0: michael@0: if (lc.cmd == LC_SEGMENT) { michael@0: uint8_t *seg_buf = buf; michael@0: struct section s; michael@0: struct segment_command seg_c; michael@0: michael@0: memcpy(&seg_c, seg_buf, sizeof(struct segment_command)); michael@0: seg_buf += sizeof(struct segment_command); michael@0: michael@0: /* Although each section is given it's own offset, nlist.n_value michael@0: * references the offset of the first section. This isn't michael@0: * apparent without debug information because the offset of the michael@0: * data section is the same as the first section. However, with michael@0: * debug sections mixed in, the offset of the debug section michael@0: * increases but n_value still references the first section. michael@0: */ michael@0: if (seg_c.nsects < 1) { michael@0: log_msg("Not enough sections\n"); michael@0: goto bail; michael@0: } michael@0: michael@0: memcpy(&s, seg_buf, sizeof(struct section)); michael@0: base_data_section = s.offset; michael@0: } else if (lc.cmd == LC_SEGMENT_64) { michael@0: uint8_t *seg_buf = buf; michael@0: struct section_64 s; michael@0: struct segment_command_64 seg_c; michael@0: michael@0: memcpy(&seg_c, seg_buf, sizeof(struct segment_command_64)); michael@0: seg_buf += sizeof(struct segment_command_64); michael@0: michael@0: /* Explanation in LG_SEGMENT */ michael@0: if (seg_c.nsects < 1) { michael@0: log_msg("Not enough sections\n"); michael@0: goto bail; michael@0: } michael@0: michael@0: memcpy(&s, seg_buf, sizeof(struct section_64)); michael@0: base_data_section = s.offset; michael@0: } else if (lc.cmd == LC_SYMTAB) { michael@0: if (base_data_section != 0) { michael@0: struct symtab_command sc; michael@0: uint8_t *sym_buf = base_buf; michael@0: uint8_t *str_buf = base_buf; michael@0: michael@0: memcpy(&sc, buf, sizeof(struct symtab_command)); michael@0: michael@0: if (sc.cmdsize != sizeof(struct symtab_command)) { michael@0: log_msg("Can't find symbol table!\n"); michael@0: goto bail; michael@0: } michael@0: michael@0: sym_buf += sc.symoff; michael@0: str_buf += sc.stroff; michael@0: michael@0: for (j = 0; j < sc.nsyms; j++) { michael@0: /* Location of string is cacluated each time from the michael@0: * start of the string buffer. On darwin the symbols michael@0: * are prefixed by "_", so we bump the pointer by 1. michael@0: * The target value is defined as an int in *_asm_*_offsets.c, michael@0: * which is 4 bytes on all targets we currently use. michael@0: */ michael@0: if (bits == 32) { michael@0: struct nlist nl; michael@0: int val; michael@0: michael@0: memcpy(&nl, sym_buf, sizeof(struct nlist)); michael@0: sym_buf += sizeof(struct nlist); michael@0: michael@0: memcpy(&val, base_buf + base_data_section + nl.n_value, michael@0: sizeof(val)); michael@0: print_macho_equ(mode, str_buf + nl.n_un.n_strx + 1, val); michael@0: } else { /* if (bits == 64) */ michael@0: struct nlist_64 nl; michael@0: int val; michael@0: michael@0: memcpy(&nl, sym_buf, sizeof(struct nlist_64)); michael@0: sym_buf += sizeof(struct nlist_64); michael@0: michael@0: memcpy(&val, base_buf + base_data_section + nl.n_value, michael@0: sizeof(val)); michael@0: print_macho_equ(mode, str_buf + nl.n_un.n_strx + 1, val); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: buf += lc.cmdsize; michael@0: } michael@0: michael@0: return 0; michael@0: bail: michael@0: return 1; michael@0: michael@0: } michael@0: michael@0: #elif defined(__ELF__) michael@0: #include "elf.h" michael@0: michael@0: #define COPY_STRUCT(dst, buf, ofst, sz) do {\ michael@0: if(ofst + sizeof((*(dst))) > sz) goto bail;\ michael@0: memcpy(dst, buf+ofst, sizeof((*(dst))));\ michael@0: } while(0) michael@0: michael@0: #define ENDIAN_ASSIGN(val, memb) do {\ michael@0: if(!elf->le_data) {log_msg("Big Endian data not supported yet!\n");goto bail;}\ michael@0: (val) = (memb);\ michael@0: } while(0) michael@0: michael@0: #define ENDIAN_ASSIGN_IN_PLACE(memb) do {\ michael@0: ENDIAN_ASSIGN(memb, memb);\ michael@0: } while(0) michael@0: michael@0: typedef struct { michael@0: uint8_t *buf; /* Buffer containing ELF data */ michael@0: size_t sz; /* Buffer size */ michael@0: int le_data; /* Data is little-endian */ michael@0: unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */ michael@0: int bits; /* 32 or 64 */ michael@0: Elf32_Ehdr hdr32; michael@0: Elf64_Ehdr hdr64; michael@0: } elf_obj_t; michael@0: michael@0: int parse_elf_header(elf_obj_t *elf) { michael@0: int res; michael@0: /* Verify ELF Magic numbers */ michael@0: COPY_STRUCT(&elf->e_ident, elf->buf, 0, elf->sz); michael@0: res = elf->e_ident[EI_MAG0] == ELFMAG0; michael@0: res &= elf->e_ident[EI_MAG1] == ELFMAG1; michael@0: res &= elf->e_ident[EI_MAG2] == ELFMAG2; michael@0: res &= elf->e_ident[EI_MAG3] == ELFMAG3; michael@0: res &= elf->e_ident[EI_CLASS] == ELFCLASS32 michael@0: || elf->e_ident[EI_CLASS] == ELFCLASS64; michael@0: res &= elf->e_ident[EI_DATA] == ELFDATA2LSB; michael@0: michael@0: if (!res) goto bail; michael@0: michael@0: elf->le_data = elf->e_ident[EI_DATA] == ELFDATA2LSB; michael@0: michael@0: /* Read in relevant values */ michael@0: if (elf->e_ident[EI_CLASS] == ELFCLASS32) { michael@0: elf->bits = 32; michael@0: COPY_STRUCT(&elf->hdr32, elf->buf, 0, elf->sz); michael@0: michael@0: ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_type); michael@0: ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_machine); michael@0: ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_version); michael@0: ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_entry); michael@0: ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_phoff); michael@0: ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_shoff); michael@0: ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_flags); michael@0: ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_ehsize); michael@0: ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_phentsize); michael@0: ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_phnum); michael@0: ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_shentsize); michael@0: ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_shnum); michael@0: ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_shstrndx); michael@0: } else { /* if (elf->e_ident[EI_CLASS] == ELFCLASS64) */ michael@0: elf->bits = 64; michael@0: COPY_STRUCT(&elf->hdr64, elf->buf, 0, elf->sz); michael@0: michael@0: ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_type); michael@0: ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_machine); michael@0: ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_version); michael@0: ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_entry); michael@0: ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_phoff); michael@0: ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_shoff); michael@0: ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_flags); michael@0: ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_ehsize); michael@0: ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_phentsize); michael@0: ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_phnum); michael@0: ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_shentsize); michael@0: ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_shnum); michael@0: ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_shstrndx); michael@0: } michael@0: michael@0: return 0; michael@0: bail: michael@0: log_msg("Failed to parse ELF file header"); michael@0: return 1; michael@0: } michael@0: michael@0: int parse_elf_section(elf_obj_t *elf, int idx, Elf32_Shdr *hdr32, Elf64_Shdr *hdr64) { michael@0: if (hdr32) { michael@0: if (idx >= elf->hdr32.e_shnum) michael@0: goto bail; michael@0: michael@0: COPY_STRUCT(hdr32, elf->buf, elf->hdr32.e_shoff + idx * elf->hdr32.e_shentsize, michael@0: elf->sz); michael@0: ENDIAN_ASSIGN_IN_PLACE(hdr32->sh_name); michael@0: ENDIAN_ASSIGN_IN_PLACE(hdr32->sh_type); michael@0: ENDIAN_ASSIGN_IN_PLACE(hdr32->sh_flags); michael@0: ENDIAN_ASSIGN_IN_PLACE(hdr32->sh_addr); michael@0: ENDIAN_ASSIGN_IN_PLACE(hdr32->sh_offset); michael@0: ENDIAN_ASSIGN_IN_PLACE(hdr32->sh_size); michael@0: ENDIAN_ASSIGN_IN_PLACE(hdr32->sh_link); michael@0: ENDIAN_ASSIGN_IN_PLACE(hdr32->sh_info); michael@0: ENDIAN_ASSIGN_IN_PLACE(hdr32->sh_addralign); michael@0: ENDIAN_ASSIGN_IN_PLACE(hdr32->sh_entsize); michael@0: } else { /* if (hdr64) */ michael@0: if (idx >= elf->hdr64.e_shnum) michael@0: goto bail; michael@0: michael@0: COPY_STRUCT(hdr64, elf->buf, elf->hdr64.e_shoff + idx * elf->hdr64.e_shentsize, michael@0: elf->sz); michael@0: ENDIAN_ASSIGN_IN_PLACE(hdr64->sh_name); michael@0: ENDIAN_ASSIGN_IN_PLACE(hdr64->sh_type); michael@0: ENDIAN_ASSIGN_IN_PLACE(hdr64->sh_flags); michael@0: ENDIAN_ASSIGN_IN_PLACE(hdr64->sh_addr); michael@0: ENDIAN_ASSIGN_IN_PLACE(hdr64->sh_offset); michael@0: ENDIAN_ASSIGN_IN_PLACE(hdr64->sh_size); michael@0: ENDIAN_ASSIGN_IN_PLACE(hdr64->sh_link); michael@0: ENDIAN_ASSIGN_IN_PLACE(hdr64->sh_info); michael@0: ENDIAN_ASSIGN_IN_PLACE(hdr64->sh_addralign); michael@0: ENDIAN_ASSIGN_IN_PLACE(hdr64->sh_entsize); michael@0: } michael@0: michael@0: return 0; michael@0: bail: michael@0: return 1; michael@0: } michael@0: michael@0: char *parse_elf_string_table(elf_obj_t *elf, int s_idx, int idx) { michael@0: if (elf->bits == 32) { michael@0: Elf32_Shdr shdr; michael@0: michael@0: if (parse_elf_section(elf, s_idx, &shdr, NULL)) { michael@0: log_msg("Failed to parse ELF string table: section %d, index %d\n", michael@0: s_idx, idx); michael@0: return ""; michael@0: } michael@0: michael@0: return (char *)(elf->buf + shdr.sh_offset + idx); michael@0: } else { /* if (elf->bits == 64) */ michael@0: Elf64_Shdr shdr; michael@0: michael@0: if (parse_elf_section(elf, s_idx, NULL, &shdr)) { michael@0: log_msg("Failed to parse ELF string table: section %d, index %d\n", michael@0: s_idx, idx); michael@0: return ""; michael@0: } michael@0: michael@0: return (char *)(elf->buf + shdr.sh_offset + idx); michael@0: } michael@0: } michael@0: michael@0: int parse_elf_symbol(elf_obj_t *elf, unsigned int ofst, Elf32_Sym *sym32, Elf64_Sym *sym64) { michael@0: if (sym32) { michael@0: COPY_STRUCT(sym32, elf->buf, ofst, elf->sz); michael@0: ENDIAN_ASSIGN_IN_PLACE(sym32->st_name); michael@0: ENDIAN_ASSIGN_IN_PLACE(sym32->st_value); michael@0: ENDIAN_ASSIGN_IN_PLACE(sym32->st_size); michael@0: ENDIAN_ASSIGN_IN_PLACE(sym32->st_info); michael@0: ENDIAN_ASSIGN_IN_PLACE(sym32->st_other); michael@0: ENDIAN_ASSIGN_IN_PLACE(sym32->st_shndx); michael@0: } else { /* if (sym64) */ michael@0: COPY_STRUCT(sym64, elf->buf, ofst, elf->sz); michael@0: ENDIAN_ASSIGN_IN_PLACE(sym64->st_name); michael@0: ENDIAN_ASSIGN_IN_PLACE(sym64->st_value); michael@0: ENDIAN_ASSIGN_IN_PLACE(sym64->st_size); michael@0: ENDIAN_ASSIGN_IN_PLACE(sym64->st_info); michael@0: ENDIAN_ASSIGN_IN_PLACE(sym64->st_other); michael@0: ENDIAN_ASSIGN_IN_PLACE(sym64->st_shndx); michael@0: } michael@0: return 0; michael@0: bail: michael@0: return 1; michael@0: } michael@0: michael@0: int parse_elf(uint8_t *buf, size_t sz, output_fmt_t mode) { michael@0: elf_obj_t elf; michael@0: unsigned int ofst; michael@0: int i; michael@0: Elf32_Off strtab_off32; michael@0: Elf64_Off strtab_off64; /* save String Table offset for later use */ michael@0: michael@0: memset(&elf, 0, sizeof(elf)); michael@0: elf.buf = buf; michael@0: elf.sz = sz; michael@0: michael@0: /* Parse Header */ michael@0: if (parse_elf_header(&elf)) michael@0: goto bail; michael@0: michael@0: if (elf.bits == 32) { michael@0: Elf32_Shdr shdr; michael@0: for (i = 0; i < elf.hdr32.e_shnum; i++) { michael@0: parse_elf_section(&elf, i, &shdr, NULL); michael@0: michael@0: if (shdr.sh_type == SHT_STRTAB) { michael@0: char strtsb_name[128]; michael@0: michael@0: strcpy(strtsb_name, (char *)(elf.buf + shdr.sh_offset + shdr.sh_name)); michael@0: michael@0: if (!(strcmp(strtsb_name, ".shstrtab"))) { michael@0: /* log_msg("found section: %s\n", strtsb_name); */ michael@0: strtab_off32 = shdr.sh_offset; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: } else { /* if (elf.bits == 64) */ michael@0: Elf64_Shdr shdr; michael@0: for (i = 0; i < elf.hdr64.e_shnum; i++) { michael@0: parse_elf_section(&elf, i, NULL, &shdr); michael@0: michael@0: if (shdr.sh_type == SHT_STRTAB) { michael@0: char strtsb_name[128]; michael@0: michael@0: strcpy(strtsb_name, (char *)(elf.buf + shdr.sh_offset + shdr.sh_name)); michael@0: michael@0: if (!(strcmp(strtsb_name, ".shstrtab"))) { michael@0: /* log_msg("found section: %s\n", strtsb_name); */ michael@0: strtab_off64 = shdr.sh_offset; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* Parse all Symbol Tables */ michael@0: if (elf.bits == 32) { michael@0: Elf32_Shdr shdr; michael@0: for (i = 0; i < elf.hdr32.e_shnum; i++) { michael@0: parse_elf_section(&elf, i, &shdr, NULL); michael@0: michael@0: if (shdr.sh_type == SHT_SYMTAB) { michael@0: for (ofst = shdr.sh_offset; michael@0: ofst < shdr.sh_offset + shdr.sh_size; michael@0: ofst += shdr.sh_entsize) { michael@0: Elf32_Sym sym; michael@0: michael@0: parse_elf_symbol(&elf, ofst, &sym, NULL); michael@0: michael@0: /* For all OBJECTS (data objects), extract the value from the michael@0: * proper data segment. michael@0: */ michael@0: /* if (ELF32_ST_TYPE(sym.st_info) == STT_OBJECT && sym.st_name) michael@0: log_msg("found data object %s\n", michael@0: parse_elf_string_table(&elf, michael@0: shdr.sh_link, michael@0: sym.st_name)); michael@0: */ michael@0: michael@0: if (ELF32_ST_TYPE(sym.st_info) == STT_OBJECT michael@0: && sym.st_size == 4) { michael@0: Elf32_Shdr dhdr; michael@0: int val = 0; michael@0: char section_name[128]; michael@0: michael@0: parse_elf_section(&elf, sym.st_shndx, &dhdr, NULL); michael@0: michael@0: /* For explanition - refer to _MSC_VER version of code */ michael@0: strcpy(section_name, (char *)(elf.buf + strtab_off32 + dhdr.sh_name)); michael@0: /* log_msg("Section_name: %s, Section_type: %d\n", section_name, dhdr.sh_type); */ michael@0: michael@0: if (strcmp(section_name, ".bss")) { michael@0: if (sizeof(val) != sym.st_size) { michael@0: /* The target value is declared as an int in michael@0: * *_asm_*_offsets.c, which is 4 bytes on all michael@0: * targets we currently use. Complain loudly if michael@0: * this is not true. michael@0: */ michael@0: log_msg("Symbol size is wrong\n"); michael@0: goto bail; michael@0: } michael@0: michael@0: memcpy(&val, michael@0: elf.buf + dhdr.sh_offset + sym.st_value, michael@0: sym.st_size); michael@0: } michael@0: michael@0: if (!elf.le_data) { michael@0: log_msg("Big Endian data not supported yet!\n"); michael@0: goto bail; michael@0: } michael@0: michael@0: switch (mode) { michael@0: case OUTPUT_FMT_RVDS: michael@0: printf("%-40s EQU %5d\n", michael@0: parse_elf_string_table(&elf, michael@0: shdr.sh_link, michael@0: sym.st_name), michael@0: val); michael@0: break; michael@0: case OUTPUT_FMT_GAS: michael@0: printf(".equ %-40s, %5d\n", michael@0: parse_elf_string_table(&elf, michael@0: shdr.sh_link, michael@0: sym.st_name), michael@0: val); michael@0: break; michael@0: default: michael@0: printf("%s = %d\n", michael@0: parse_elf_string_table(&elf, michael@0: shdr.sh_link, michael@0: sym.st_name), michael@0: val); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } else { /* if (elf.bits == 64) */ michael@0: Elf64_Shdr shdr; michael@0: for (i = 0; i < elf.hdr64.e_shnum; i++) { michael@0: parse_elf_section(&elf, i, NULL, &shdr); michael@0: michael@0: if (shdr.sh_type == SHT_SYMTAB) { michael@0: for (ofst = shdr.sh_offset; michael@0: ofst < shdr.sh_offset + shdr.sh_size; michael@0: ofst += shdr.sh_entsize) { michael@0: Elf64_Sym sym; michael@0: michael@0: parse_elf_symbol(&elf, ofst, NULL, &sym); michael@0: michael@0: /* For all OBJECTS (data objects), extract the value from the michael@0: * proper data segment. michael@0: */ michael@0: /* if (ELF64_ST_TYPE(sym.st_info) == STT_OBJECT && sym.st_name) michael@0: log_msg("found data object %s\n", michael@0: parse_elf_string_table(&elf, michael@0: shdr.sh_link, michael@0: sym.st_name)); michael@0: */ michael@0: michael@0: if (ELF64_ST_TYPE(sym.st_info) == STT_OBJECT michael@0: && sym.st_size == 4) { michael@0: Elf64_Shdr dhdr; michael@0: int val = 0; michael@0: char section_name[128]; michael@0: michael@0: parse_elf_section(&elf, sym.st_shndx, NULL, &dhdr); michael@0: michael@0: /* For explanition - refer to _MSC_VER version of code */ michael@0: strcpy(section_name, (char *)(elf.buf + strtab_off64 + dhdr.sh_name)); michael@0: /* log_msg("Section_name: %s, Section_type: %d\n", section_name, dhdr.sh_type); */ michael@0: michael@0: if ((strcmp(section_name, ".bss"))) { michael@0: if (sizeof(val) != sym.st_size) { michael@0: /* The target value is declared as an int in michael@0: * *_asm_*_offsets.c, which is 4 bytes on all michael@0: * targets we currently use. Complain loudly if michael@0: * this is not true. michael@0: */ michael@0: log_msg("Symbol size is wrong\n"); michael@0: goto bail; michael@0: } michael@0: michael@0: memcpy(&val, michael@0: elf.buf + dhdr.sh_offset + sym.st_value, michael@0: sym.st_size); michael@0: } michael@0: michael@0: if (!elf.le_data) { michael@0: log_msg("Big Endian data not supported yet!\n"); michael@0: goto bail; michael@0: } michael@0: michael@0: switch (mode) { michael@0: case OUTPUT_FMT_RVDS: michael@0: printf("%-40s EQU %5d\n", michael@0: parse_elf_string_table(&elf, michael@0: shdr.sh_link, michael@0: sym.st_name), michael@0: val); michael@0: break; michael@0: case OUTPUT_FMT_GAS: michael@0: printf(".equ %-40s, %5d\n", michael@0: parse_elf_string_table(&elf, michael@0: shdr.sh_link, michael@0: sym.st_name), michael@0: val); michael@0: break; michael@0: default: michael@0: printf("%s = %d\n", michael@0: parse_elf_string_table(&elf, michael@0: shdr.sh_link, michael@0: sym.st_name), michael@0: val); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (mode == OUTPUT_FMT_RVDS) michael@0: printf(" END\n"); michael@0: michael@0: return 0; michael@0: bail: michael@0: log_msg("Parse error: File does not appear to be valid ELF32 or ELF64\n"); michael@0: return 1; michael@0: } michael@0: michael@0: #endif michael@0: #endif /* defined(__GNUC__) && __GNUC__ */ michael@0: michael@0: michael@0: #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN__) michael@0: /* See "Microsoft Portable Executable and Common Object File Format Specification" michael@0: for reference. michael@0: */ michael@0: #define get_le32(x) ((*(x)) | (*(x+1)) << 8 |(*(x+2)) << 16 | (*(x+3)) << 24 ) michael@0: #define get_le16(x) ((*(x)) | (*(x+1)) << 8) michael@0: michael@0: int parse_coff(uint8_t *buf, size_t sz) { michael@0: unsigned int nsections, symtab_ptr, symtab_sz, strtab_ptr; michael@0: unsigned int sectionrawdata_ptr; michael@0: unsigned int i; michael@0: uint8_t *ptr; michael@0: uint32_t symoffset; michael@0: michael@0: char **sectionlist; // this array holds all section names in their correct order. michael@0: // it is used to check if the symbol is in .bss or .rdata section. michael@0: michael@0: nsections = get_le16(buf + 2); michael@0: symtab_ptr = get_le32(buf + 8); michael@0: symtab_sz = get_le32(buf + 12); michael@0: strtab_ptr = symtab_ptr + symtab_sz * 18; michael@0: michael@0: if (nsections > 96) { michael@0: log_msg("Too many sections\n"); michael@0: return 1; michael@0: } michael@0: michael@0: sectionlist = malloc(nsections * sizeof(sectionlist)); michael@0: michael@0: if (sectionlist == NULL) { michael@0: log_msg("Allocating first level of section list failed\n"); michael@0: return 1; michael@0: } michael@0: michael@0: // log_msg("COFF: Found %u symbols in %u sections.\n", symtab_sz, nsections); michael@0: michael@0: /* michael@0: The size of optional header is always zero for an obj file. So, the section header michael@0: follows the file header immediately. michael@0: */ michael@0: michael@0: ptr = buf + 20; // section header michael@0: michael@0: for (i = 0; i < nsections; i++) { michael@0: char sectionname[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; michael@0: strncpy(sectionname, ptr, 8); michael@0: // log_msg("COFF: Parsing section %s\n",sectionname); michael@0: michael@0: sectionlist[i] = malloc(strlen(sectionname) + 1); michael@0: michael@0: if (sectionlist[i] == NULL) { michael@0: log_msg("Allocating storage for %s failed\n", sectionname); michael@0: goto bail; michael@0: } michael@0: strcpy(sectionlist[i], sectionname); michael@0: michael@0: if (!strcmp(sectionname, ".rdata")) sectionrawdata_ptr = get_le32(ptr + 20); michael@0: michael@0: ptr += 40; michael@0: } michael@0: michael@0: // log_msg("COFF: Symbol table at offset %u\n", symtab_ptr); michael@0: // log_msg("COFF: raw data pointer ofset for section .rdata is %u\n", sectionrawdata_ptr); michael@0: michael@0: /* The compiler puts the data with non-zero offset in .rdata section, but puts the data with michael@0: zero offset in .bss section. So, if the data in in .bss section, set offset=0. michael@0: Note from Wiki: In an object module compiled from C, the bss section contains michael@0: the local variables (but not functions) that were declared with the static keyword, michael@0: except for those with non-zero initial values. (In C, static variables are initialized michael@0: to zero by default.) It also contains the non-local (both extern and static) variables michael@0: that are also initialized to zero (either explicitly or by default). michael@0: */ michael@0: // move to symbol table michael@0: /* COFF symbol table: michael@0: offset field michael@0: 0 Name(*) michael@0: 8 Value michael@0: 12 SectionNumber michael@0: 14 Type michael@0: 16 StorageClass michael@0: 17 NumberOfAuxSymbols michael@0: */ michael@0: ptr = buf + symtab_ptr; michael@0: michael@0: for (i = 0; i < symtab_sz; i++) { michael@0: int16_t section = get_le16(ptr + 12); // section number michael@0: michael@0: if (section > 0 && ptr[16] == 2) { michael@0: // if(section > 0 && ptr[16] == 3 && get_le32(ptr+8)) { michael@0: michael@0: if (get_le32(ptr)) { michael@0: char name[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; michael@0: strncpy(name, ptr, 8); michael@0: // log_msg("COFF: Parsing symbol %s\n",name); michael@0: /* The 64bit Windows compiler doesn't prefix with an _. michael@0: * Check what's there, and bump if necessary michael@0: */ michael@0: if (name[0] == '_') michael@0: printf("%-40s EQU ", name + 1); michael@0: else michael@0: printf("%-40s EQU ", name); michael@0: } else { michael@0: // log_msg("COFF: Parsing symbol %s\n", michael@0: // buf + strtab_ptr + get_le32(ptr+4)); michael@0: if ((buf + strtab_ptr + get_le32(ptr + 4))[0] == '_') michael@0: printf("%-40s EQU ", michael@0: buf + strtab_ptr + get_le32(ptr + 4) + 1); michael@0: else michael@0: printf("%-40s EQU ", buf + strtab_ptr + get_le32(ptr + 4)); michael@0: } michael@0: michael@0: if (!(strcmp(sectionlist[section - 1], ".bss"))) { michael@0: symoffset = 0; michael@0: } else { michael@0: symoffset = get_le32(buf + sectionrawdata_ptr + get_le32(ptr + 8)); michael@0: } michael@0: michael@0: // log_msg(" Section: %d\n",section); michael@0: // log_msg(" Class: %d\n",ptr[16]); michael@0: // log_msg(" Address: %u\n",get_le32(ptr+8)); michael@0: // log_msg(" Offset: %u\n", symoffset); michael@0: michael@0: printf("%5d\n", symoffset); michael@0: } michael@0: michael@0: ptr += 18; michael@0: } michael@0: michael@0: printf(" END\n"); michael@0: michael@0: for (i = 0; i < nsections; i++) { michael@0: free(sectionlist[i]); michael@0: } michael@0: michael@0: free(sectionlist); michael@0: michael@0: return 0; michael@0: bail: michael@0: michael@0: for (i = 0; i < nsections; i++) { michael@0: free(sectionlist[i]); michael@0: } michael@0: michael@0: free(sectionlist); michael@0: michael@0: return 1; michael@0: } michael@0: #endif /* defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN__) */ michael@0: michael@0: int main(int argc, char **argv) { michael@0: output_fmt_t mode = OUTPUT_FMT_PLAIN; michael@0: const char *f; michael@0: uint8_t *file_buf; michael@0: int res; michael@0: FILE *fp; michael@0: long int file_size; michael@0: michael@0: if (argc < 2 || argc > 3) { michael@0: fprintf(stderr, "Usage: %s [output format] \n\n", argv[0]); michael@0: fprintf(stderr, " \tobject file to parse\n"); michael@0: fprintf(stderr, "Output Formats:\n"); michael@0: fprintf(stderr, " gas - compatible with GNU assembler\n"); michael@0: fprintf(stderr, " rvds - compatible with armasm\n"); michael@0: goto bail; michael@0: } michael@0: michael@0: f = argv[2]; michael@0: michael@0: if (!strcmp(argv[1], "rvds")) michael@0: mode = OUTPUT_FMT_RVDS; michael@0: else if (!strcmp(argv[1], "gas")) michael@0: mode = OUTPUT_FMT_GAS; michael@0: else michael@0: f = argv[1]; michael@0: michael@0: fp = fopen(f, "rb"); michael@0: michael@0: if (!fp) { michael@0: perror("Unable to open file"); michael@0: goto bail; michael@0: } michael@0: michael@0: if (fseek(fp, 0, SEEK_END)) { michael@0: perror("stat"); michael@0: goto bail; michael@0: } michael@0: michael@0: file_size = ftell(fp); michael@0: file_buf = malloc(file_size); michael@0: michael@0: if (!file_buf) { michael@0: perror("malloc"); michael@0: goto bail; michael@0: } michael@0: michael@0: rewind(fp); michael@0: michael@0: if (fread(file_buf, sizeof(char), file_size, fp) != file_size) { michael@0: perror("read"); michael@0: goto bail; michael@0: } michael@0: michael@0: if (fclose(fp)) { michael@0: perror("close"); michael@0: goto bail; michael@0: } michael@0: michael@0: #if defined(__GNUC__) && __GNUC__ michael@0: #if defined(__MACH__) michael@0: res = parse_macho(file_buf, file_size, mode); michael@0: #elif defined(__ELF__) michael@0: res = parse_elf(file_buf, file_size, mode); michael@0: #endif michael@0: #endif michael@0: #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN__) michael@0: res = parse_coff(file_buf, file_size); michael@0: #endif michael@0: michael@0: free(file_buf); michael@0: michael@0: if (!res) michael@0: return EXIT_SUCCESS; michael@0: michael@0: bail: michael@0: return EXIT_FAILURE; michael@0: }