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