2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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.
17 #include "vpx_config.h"
18 #include "vpx/vpx_integer.h"
27 int log_msg(const char *fmt
, ...)
32 res
= vfprintf(stderr
, fmt
, ap
);
37 #if defined(__GNUC__) && __GNUC__
40 #include <mach-o/loader.h>
41 #include <mach-o/nlist.h>
43 int parse_macho(uint8_t *base_buf
, size_t sz
)
46 struct mach_header header
;
47 uint8_t *buf
= base_buf
;
48 int base_data_section
= 0;
51 /* We can read in mach_header for 32 and 64 bit architectures
52 * because it's identical to mach_header_64 except for the last
53 * element (uint32_t reserved), which we don't use. Then, when
54 * we know which architecture we're looking at, increment buf
57 memcpy(&header
, buf
, sizeof(struct mach_header
));
59 if (header
.magic
== MH_MAGIC
)
61 if (header
.cputype
== CPU_TYPE_ARM
62 || header
.cputype
== CPU_TYPE_X86
)
65 buf
+= sizeof(struct mach_header
);
69 log_msg("Bad cputype for object file. Currently only tested for CPU_TYPE_[ARM|X86].\n");
73 else if (header
.magic
== MH_MAGIC_64
)
75 if (header
.cputype
== CPU_TYPE_X86_64
)
78 buf
+= sizeof(struct mach_header_64
);
82 log_msg("Bad cputype for object file. Currently only tested for CPU_TYPE_X86_64.\n");
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
);
93 if (header
.filetype
!= MH_OBJECT
)
95 log_msg("Bad filetype for object file. Currently only tested for MH_OBJECT.\n");
99 for (i
= 0; i
< header
.ncmds
; i
++)
101 struct load_command lc
;
103 memcpy(&lc
, buf
, sizeof(struct load_command
));
105 if (lc
.cmd
== LC_SEGMENT
)
107 uint8_t *seg_buf
= buf
;
109 struct segment_command seg_c
;
111 memcpy(&seg_c
, seg_buf
, sizeof(struct segment_command
));
112 seg_buf
+= sizeof(struct segment_command
);
114 /* Although each section is given it's own offset, nlist.n_value
115 * references the offset of the first section. This isn't
116 * apparent without debug information because the offset of the
117 * data section is the same as the first section. However, with
118 * debug sections mixed in, the offset of the debug section
119 * increases but n_value still references the first section.
121 if (seg_c
.nsects
< 1)
123 log_msg("Not enough sections\n");
127 memcpy(&s
, seg_buf
, sizeof(struct section
));
128 base_data_section
= s
.offset
;
130 else if (lc
.cmd
== LC_SEGMENT_64
)
132 uint8_t *seg_buf
= buf
;
134 struct segment_command_64 seg_c
;
136 memcpy(&seg_c
, seg_buf
, sizeof(struct segment_command_64
));
137 seg_buf
+= sizeof(struct segment_command_64
);
139 /* Explanation in LG_SEGMENT */
140 if (seg_c
.nsects
< 1)
142 log_msg("Not enough sections\n");
146 memcpy(&s
, seg_buf
, sizeof(struct section_64
));
147 base_data_section
= s
.offset
;
149 else if (lc
.cmd
== LC_SYMTAB
)
151 if (base_data_section
!= 0)
153 struct symtab_command sc
;
154 uint8_t *sym_buf
= base_buf
;
155 uint8_t *str_buf
= base_buf
;
157 memcpy(&sc
, buf
, sizeof(struct symtab_command
));
159 if (sc
.cmdsize
!= sizeof(struct symtab_command
))
161 log_msg("Can't find symbol table!\n");
165 sym_buf
+= sc
.symoff
;
166 str_buf
+= sc
.stroff
;
168 for (j
= 0; j
< sc
.nsyms
; j
++)
170 /* Location of string is cacluated each time from the
171 * start of the string buffer. On darwin the symbols
172 * are prefixed by "_", so we bump the pointer by 1.
173 * The target value is defined as an int in asm_*_offsets.c,
174 * which is 4 bytes on all targets we currently use.
181 memcpy(&nl
, sym_buf
, sizeof(struct nlist
));
182 sym_buf
+= sizeof(struct nlist
);
184 memcpy(&val
, base_buf
+ base_data_section
+ nl
.n_value
,
186 printf("%-40s EQU %5d\n",
187 str_buf
+ nl
.n_un
.n_strx
+ 1, val
);
189 else /* if (bits == 64) */
194 memcpy(&nl
, sym_buf
, sizeof(struct nlist_64
));
195 sym_buf
+= sizeof(struct nlist_64
);
197 memcpy(&val
, base_buf
+ base_data_section
+ nl
.n_value
,
199 printf("%-40s EQU %5d\n",
200 str_buf
+ nl
.n_un
.n_strx
+ 1, val
);
215 #elif defined(__ELF__)
218 #define COPY_STRUCT(dst, buf, ofst, sz) do {\
219 if(ofst + sizeof((*(dst))) > sz) goto bail;\
220 memcpy(dst, buf+ofst, sizeof((*(dst))));\
223 #define ENDIAN_ASSIGN(val, memb) do {\
224 if(!elf->le_data) {log_msg("Big Endian data not supported yet!\n");goto bail;}\
228 #define ENDIAN_ASSIGN_IN_PLACE(memb) do {\
229 ENDIAN_ASSIGN(memb, memb);\
234 uint8_t *buf
; /* Buffer containing ELF data */
235 size_t sz
; /* Buffer size */
236 int le_data
; /* Data is little-endian */
237 unsigned char e_ident
[EI_NIDENT
]; /* Magic number and other info */
238 int bits
; /* 32 or 64 */
243 int parse_elf_header(elf_obj_t
*elf
)
246 /* Verify ELF Magic numbers */
247 COPY_STRUCT(&elf
->e_ident
, elf
->buf
, 0, elf
->sz
);
248 res
= elf
->e_ident
[EI_MAG0
] == ELFMAG0
;
249 res
&= elf
->e_ident
[EI_MAG1
] == ELFMAG1
;
250 res
&= elf
->e_ident
[EI_MAG2
] == ELFMAG2
;
251 res
&= elf
->e_ident
[EI_MAG3
] == ELFMAG3
;
252 res
&= elf
->e_ident
[EI_CLASS
] == ELFCLASS32
253 || elf
->e_ident
[EI_CLASS
] == ELFCLASS64
;
254 res
&= elf
->e_ident
[EI_DATA
] == ELFDATA2LSB
;
258 elf
->le_data
= elf
->e_ident
[EI_DATA
] == ELFDATA2LSB
;
260 /* Read in relevant values */
261 if (elf
->e_ident
[EI_CLASS
] == ELFCLASS32
)
264 COPY_STRUCT(&elf
->hdr32
, elf
->buf
, 0, elf
->sz
);
266 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr32
.e_type
);
267 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr32
.e_machine
);
268 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr32
.e_version
);
269 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr32
.e_entry
);
270 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr32
.e_phoff
);
271 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr32
.e_shoff
);
272 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr32
.e_flags
);
273 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr32
.e_ehsize
);
274 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr32
.e_phentsize
);
275 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr32
.e_phnum
);
276 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr32
.e_shentsize
);
277 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr32
.e_shnum
);
278 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr32
.e_shstrndx
);
280 else /* if (elf->e_ident[EI_CLASS] == ELFCLASS64) */
283 COPY_STRUCT(&elf
->hdr64
, elf
->buf
, 0, elf
->sz
);
285 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr64
.e_type
);
286 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr64
.e_machine
);
287 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr64
.e_version
);
288 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr64
.e_entry
);
289 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr64
.e_phoff
);
290 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr64
.e_shoff
);
291 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr64
.e_flags
);
292 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr64
.e_ehsize
);
293 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr64
.e_phentsize
);
294 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr64
.e_phnum
);
295 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr64
.e_shentsize
);
296 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr64
.e_shnum
);
297 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr64
.e_shstrndx
);
302 log_msg("Failed to parse ELF file header");
306 int parse_elf_section(elf_obj_t
*elf
, int idx
, Elf32_Shdr
*hdr32
, Elf64_Shdr
*hdr64
)
310 if (idx
>= elf
->hdr32
.e_shnum
)
313 COPY_STRUCT(hdr32
, elf
->buf
, elf
->hdr32
.e_shoff
+ idx
* elf
->hdr32
.e_shentsize
,
315 ENDIAN_ASSIGN_IN_PLACE(hdr32
->sh_name
);
316 ENDIAN_ASSIGN_IN_PLACE(hdr32
->sh_type
);
317 ENDIAN_ASSIGN_IN_PLACE(hdr32
->sh_flags
);
318 ENDIAN_ASSIGN_IN_PLACE(hdr32
->sh_addr
);
319 ENDIAN_ASSIGN_IN_PLACE(hdr32
->sh_offset
);
320 ENDIAN_ASSIGN_IN_PLACE(hdr32
->sh_size
);
321 ENDIAN_ASSIGN_IN_PLACE(hdr32
->sh_link
);
322 ENDIAN_ASSIGN_IN_PLACE(hdr32
->sh_info
);
323 ENDIAN_ASSIGN_IN_PLACE(hdr32
->sh_addralign
);
324 ENDIAN_ASSIGN_IN_PLACE(hdr32
->sh_entsize
);
326 else /* if (hdr64) */
328 if (idx
>= elf
->hdr64
.e_shnum
)
331 COPY_STRUCT(hdr64
, elf
->buf
, elf
->hdr64
.e_shoff
+ idx
* elf
->hdr64
.e_shentsize
,
333 ENDIAN_ASSIGN_IN_PLACE(hdr64
->sh_name
);
334 ENDIAN_ASSIGN_IN_PLACE(hdr64
->sh_type
);
335 ENDIAN_ASSIGN_IN_PLACE(hdr64
->sh_flags
);
336 ENDIAN_ASSIGN_IN_PLACE(hdr64
->sh_addr
);
337 ENDIAN_ASSIGN_IN_PLACE(hdr64
->sh_offset
);
338 ENDIAN_ASSIGN_IN_PLACE(hdr64
->sh_size
);
339 ENDIAN_ASSIGN_IN_PLACE(hdr64
->sh_link
);
340 ENDIAN_ASSIGN_IN_PLACE(hdr64
->sh_info
);
341 ENDIAN_ASSIGN_IN_PLACE(hdr64
->sh_addralign
);
342 ENDIAN_ASSIGN_IN_PLACE(hdr64
->sh_entsize
);
350 char *parse_elf_string_table(elf_obj_t
*elf
, int s_idx
, int idx
)
356 if (parse_elf_section(elf
, s_idx
, &shdr
, NULL
))
358 log_msg("Failed to parse ELF string table: section %d, index %d\n",
363 return (char *)(elf
->buf
+ shdr
.sh_offset
+ idx
);
365 else /* if (elf->bits == 64) */
369 if (parse_elf_section(elf
, s_idx
, NULL
, &shdr
))
371 log_msg("Failed to parse ELF string table: section %d, index %d\n",
376 return (char *)(elf
->buf
+ shdr
.sh_offset
+ idx
);
380 int parse_elf_symbol(elf_obj_t
*elf
, unsigned int ofst
, Elf32_Sym
*sym32
, Elf64_Sym
*sym64
)
384 COPY_STRUCT(sym32
, elf
->buf
, ofst
, elf
->sz
);
385 ENDIAN_ASSIGN_IN_PLACE(sym32
->st_name
);
386 ENDIAN_ASSIGN_IN_PLACE(sym32
->st_value
);
387 ENDIAN_ASSIGN_IN_PLACE(sym32
->st_size
);
388 ENDIAN_ASSIGN_IN_PLACE(sym32
->st_info
);
389 ENDIAN_ASSIGN_IN_PLACE(sym32
->st_other
);
390 ENDIAN_ASSIGN_IN_PLACE(sym32
->st_shndx
);
392 else /* if (sym64) */
394 COPY_STRUCT(sym64
, elf
->buf
, ofst
, elf
->sz
);
395 ENDIAN_ASSIGN_IN_PLACE(sym64
->st_name
);
396 ENDIAN_ASSIGN_IN_PLACE(sym64
->st_value
);
397 ENDIAN_ASSIGN_IN_PLACE(sym64
->st_size
);
398 ENDIAN_ASSIGN_IN_PLACE(sym64
->st_info
);
399 ENDIAN_ASSIGN_IN_PLACE(sym64
->st_other
);
400 ENDIAN_ASSIGN_IN_PLACE(sym64
->st_shndx
);
407 int parse_elf(uint8_t *buf
, size_t sz
, output_fmt_t mode
)
412 Elf32_Off strtab_off32
;
413 Elf64_Off strtab_off64
; /* save String Table offset for later use */
415 memset(&elf
, 0, sizeof(elf
));
420 if (parse_elf_header(&elf
))
426 for (i
= 0; i
< elf
.hdr32
.e_shnum
; i
++)
428 parse_elf_section(&elf
, i
, &shdr
, NULL
);
430 if (shdr
.sh_type
== SHT_STRTAB
)
432 char strtsb_name
[128];
434 strcpy(strtsb_name
, (char *)(elf
.buf
+ shdr
.sh_offset
+ shdr
.sh_name
));
436 if (!(strcmp(strtsb_name
, ".shstrtab")))
438 /* log_msg("found section: %s\n", strtsb_name); */
439 strtab_off32
= shdr
.sh_offset
;
445 else /* if (elf.bits == 64) */
448 for (i
= 0; i
< elf
.hdr64
.e_shnum
; i
++)
450 parse_elf_section(&elf
, i
, NULL
, &shdr
);
452 if (shdr
.sh_type
== SHT_STRTAB
)
454 char strtsb_name
[128];
456 strcpy(strtsb_name
, (char *)(elf
.buf
+ shdr
.sh_offset
+ shdr
.sh_name
));
458 if (!(strcmp(strtsb_name
, ".shstrtab")))
460 /* log_msg("found section: %s\n", strtsb_name); */
461 strtab_off64
= shdr
.sh_offset
;
468 /* Parse all Symbol Tables */
472 for (i
= 0; i
< elf
.hdr32
.e_shnum
; i
++)
474 parse_elf_section(&elf
, i
, &shdr
, NULL
);
476 if (shdr
.sh_type
== SHT_SYMTAB
)
478 for (ofst
= shdr
.sh_offset
;
479 ofst
< shdr
.sh_offset
+ shdr
.sh_size
;
480 ofst
+= shdr
.sh_entsize
)
484 parse_elf_symbol(&elf
, ofst
, &sym
, NULL
);
486 /* For all OBJECTS (data objects), extract the value from the
487 * proper data segment.
489 /* if (ELF32_ST_TYPE(sym.st_info) == STT_OBJECT && sym.st_name)
490 log_msg("found data object %s\n",
491 parse_elf_string_table(&elf,
496 if (ELF32_ST_TYPE(sym
.st_info
) == STT_OBJECT
501 char section_name
[128];
503 parse_elf_section(&elf
, sym
.st_shndx
, &dhdr
, NULL
);
505 /* For explanition - refer to _MSC_VER version of code */
506 strcpy(section_name
, (char *)(elf
.buf
+ strtab_off32
+ dhdr
.sh_name
));
507 /* log_msg("Section_name: %s, Section_type: %d\n", section_name, dhdr.sh_type); */
509 if (strcmp(section_name
, ".bss"))
511 if (sizeof(val
) != sym
.st_size
)
513 /* The target value is declared as an int in
514 * asm_*_offsets.c, which is 4 bytes on all
515 * targets we currently use. Complain loudly if
518 log_msg("Symbol size is wrong\n");
523 elf
.buf
+ dhdr
.sh_offset
+ sym
.st_value
,
529 log_msg("Big Endian data not supported yet!\n");
535 case OUTPUT_FMT_RVDS
:
536 printf("%-40s EQU %5d\n",
537 parse_elf_string_table(&elf
,
543 printf(".equ %-40s, %5d\n",
544 parse_elf_string_table(&elf
,
551 parse_elf_string_table(&elf
,
561 else /* if (elf.bits == 64) */
564 for (i
= 0; i
< elf
.hdr64
.e_shnum
; i
++)
566 parse_elf_section(&elf
, i
, NULL
, &shdr
);
568 if (shdr
.sh_type
== SHT_SYMTAB
)
570 for (ofst
= shdr
.sh_offset
;
571 ofst
< shdr
.sh_offset
+ shdr
.sh_size
;
572 ofst
+= shdr
.sh_entsize
)
576 parse_elf_symbol(&elf
, ofst
, NULL
, &sym
);
578 /* For all OBJECTS (data objects), extract the value from the
579 * proper data segment.
581 /* if (ELF64_ST_TYPE(sym.st_info) == STT_OBJECT && sym.st_name)
582 log_msg("found data object %s\n",
583 parse_elf_string_table(&elf,
588 if (ELF64_ST_TYPE(sym
.st_info
) == STT_OBJECT
593 char section_name
[128];
595 parse_elf_section(&elf
, sym
.st_shndx
, NULL
, &dhdr
);
597 /* For explanition - refer to _MSC_VER version of code */
598 strcpy(section_name
, (char *)(elf
.buf
+ strtab_off64
+ dhdr
.sh_name
));
599 /* log_msg("Section_name: %s, Section_type: %d\n", section_name, dhdr.sh_type); */
601 if ((strcmp(section_name
, ".bss")))
603 if (sizeof(val
) != sym
.st_size
)
605 /* The target value is declared as an int in
606 * asm_*_offsets.c, which is 4 bytes on all
607 * targets we currently use. Complain loudly if
610 log_msg("Symbol size is wrong\n");
615 elf
.buf
+ dhdr
.sh_offset
+ sym
.st_value
,
621 log_msg("Big Endian data not supported yet!\n");
627 case OUTPUT_FMT_RVDS
:
628 printf("%-40s EQU %5d\n",
629 parse_elf_string_table(&elf
,
635 printf(".equ %-40s, %5d\n",
636 parse_elf_string_table(&elf
,
643 parse_elf_string_table(&elf
,
654 if (mode
== OUTPUT_FMT_RVDS
)
659 log_msg("Parse error: File does not appear to be valid ELF32 or ELF64\n");
664 #endif /* defined(__GNUC__) && __GNUC__ */
667 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN__)
668 /* See "Microsoft Portable Executable and Common Object File Format Specification"
671 #define get_le32(x) ((*(x)) | (*(x+1)) << 8 |(*(x+2)) << 16 | (*(x+3)) << 24 )
672 #define get_le16(x) ((*(x)) | (*(x+1)) << 8)
674 int parse_coff(uint8_t *buf
, size_t sz
)
676 unsigned int nsections
, symtab_ptr
, symtab_sz
, strtab_ptr
;
677 unsigned int sectionrawdata_ptr
;
682 char **sectionlist
; //this array holds all section names in their correct order.
683 //it is used to check if the symbol is in .bss or .data section.
685 nsections
= get_le16(buf
+ 2);
686 symtab_ptr
= get_le32(buf
+ 8);
687 symtab_sz
= get_le32(buf
+ 12);
688 strtab_ptr
= symtab_ptr
+ symtab_sz
* 18;
692 log_msg("Too many sections\n");
696 sectionlist
= malloc(nsections
* sizeof(sectionlist
));
698 if (sectionlist
== NULL
)
700 log_msg("Allocating first level of section list failed\n");
704 //log_msg("COFF: Found %u symbols in %u sections.\n", symtab_sz, nsections);
707 The size of optional header is always zero for an obj file. So, the section header
708 follows the file header immediately.
711 ptr
= buf
+ 20; //section header
713 for (i
= 0; i
< nsections
; i
++)
715 char sectionname
[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
716 strncpy(sectionname
, ptr
, 8);
717 //log_msg("COFF: Parsing section %s\n",sectionname);
719 sectionlist
[i
] = malloc(strlen(sectionname
) + 1);
721 if (sectionlist
[i
] == NULL
)
723 log_msg("Allocating storage for %s failed\n", sectionname
);
726 strcpy(sectionlist
[i
], sectionname
);
728 if (!strcmp(sectionname
, ".data")) sectionrawdata_ptr
= get_le32(ptr
+ 20);
733 //log_msg("COFF: Symbol table at offset %u\n", symtab_ptr);
734 //log_msg("COFF: raw data pointer ofset for section .data is %u\n", sectionrawdata_ptr);
736 /* The compiler puts the data with non-zero offset in .data section, but puts the data with
737 zero offset in .bss section. So, if the data in in .bss section, set offset=0.
738 Note from Wiki: In an object module compiled from C, the bss section contains
739 the local variables (but not functions) that were declared with the static keyword,
740 except for those with non-zero initial values. (In C, static variables are initialized
741 to zero by default.) It also contains the non-local (both extern and static) variables
742 that are also initialized to zero (either explicitly or by default).
744 //move to symbol table
745 /* COFF symbol table:
752 17 NumberOfAuxSymbols
754 ptr
= buf
+ symtab_ptr
;
756 for (i
= 0; i
< symtab_sz
; i
++)
758 int16_t section
= get_le16(ptr
+ 12); //section number
760 if (section
> 0 && ptr
[16] == 2)
762 //if(section > 0 && ptr[16] == 3 && get_le32(ptr+8)) {
766 char name
[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
767 strncpy(name
, ptr
, 8);
768 //log_msg("COFF: Parsing symbol %s\n",name);
769 /* The 64bit Windows compiler doesn't prefix with an _.
770 * Check what's there, and bump if necessary
773 printf("%-40s EQU ", name
+ 1);
775 printf("%-40s EQU ", name
);
779 //log_msg("COFF: Parsing symbol %s\n",
780 // buf + strtab_ptr + get_le32(ptr+4));
781 if ((buf
+ strtab_ptr
+ get_le32(ptr
+ 4))[0] == '_')
783 buf
+ strtab_ptr
+ get_le32(ptr
+ 4) + 1);
785 printf("%-40s EQU ", buf
+ strtab_ptr
+ get_le32(ptr
+ 4));
788 if (!(strcmp(sectionlist
[section
-1], ".bss")))
794 symoffset
= get_le32(buf
+ sectionrawdata_ptr
+ get_le32(ptr
+ 8));
797 //log_msg(" Section: %d\n",section);
798 //log_msg(" Class: %d\n",ptr[16]);
799 //log_msg(" Address: %u\n",get_le32(ptr+8));
800 //log_msg(" Offset: %u\n", symoffset);
802 printf("%5d\n", symoffset
);
810 for (i
= 0; i
< nsections
; i
++)
812 free(sectionlist
[i
]);
820 for (i
= 0; i
< nsections
; i
++)
822 free(sectionlist
[i
]);
829 #endif /* defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN__) */
831 int main(int argc
, char **argv
)
833 output_fmt_t mode
= OUTPUT_FMT_PLAIN
;
840 if (argc
< 2 || argc
> 3)
842 fprintf(stderr
, "Usage: %s [output format] <obj file>\n\n", argv
[0]);
843 fprintf(stderr
, " <obj file>\tobject file to parse\n");
844 fprintf(stderr
, "Output Formats:\n");
845 fprintf(stderr
, " gas - compatible with GNU assembler\n");
846 fprintf(stderr
, " rvds - compatible with armasm\n");
852 if (!strcmp(argv
[1], "rvds"))
853 mode
= OUTPUT_FMT_RVDS
;
854 else if (!strcmp(argv
[1], "gas"))
855 mode
= OUTPUT_FMT_GAS
;
863 perror("Unable to open file");
867 if (fseek(fp
, 0, SEEK_END
))
873 file_size
= ftell(fp
);
874 file_buf
= malloc(file_size
);
884 if (fread(file_buf
, sizeof(char), file_size
, fp
) != file_size
)
896 #if defined(__GNUC__) && __GNUC__
897 #if defined(__MACH__)
898 res
= parse_macho(file_buf
, file_size
);
899 #elif defined(__ELF__)
900 res
= parse_elf(file_buf
, file_size
, mode
);
903 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN__)
904 res
= parse_coff(file_buf
, file_size
);