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.
15 #include "vpx_config.h"
17 #if defined(_MSC_VER) || defined(__MINGW32__)
20 #include "vpx/vpx_integer.h"
27 #include <sys/types.h>
39 int log_msg(const char *fmt
, ...)
44 res
= vfprintf(stderr
, fmt
, ap
);
49 #if defined(__GNUC__) && __GNUC__
53 #include <mach-o/loader.h>
54 #include <mach-o/nlist.h>
56 int parse_macho(uint8_t *base_buf
, size_t sz
)
59 struct mach_header header
;
60 uint8_t *buf
= base_buf
;
61 int base_data_section
= 0;
64 /* We can read in mach_header for 32 and 64 bit architectures
65 * because it's identical to mach_header_64 except for the last
66 * element (uint32_t reserved), which we don't use. Then, when
67 * we know which architecture we're looking at, increment buf
70 memcpy(&header
, buf
, sizeof(struct mach_header
));
72 if (header
.magic
== MH_MAGIC
)
74 if (header
.cputype
== CPU_TYPE_ARM
75 || header
.cputype
== CPU_TYPE_X86
)
78 buf
+= sizeof(struct mach_header
);
82 log_msg("Bad cputype for object file. Currently only tested for CPU_TYPE_[ARM|X86].\n");
86 else if (header
.magic
== MH_MAGIC_64
)
88 if (header
.cputype
== CPU_TYPE_X86_64
)
91 buf
+= sizeof(struct mach_header_64
);
95 log_msg("Bad cputype for object file. Currently only tested for CPU_TYPE_X86_64.\n");
101 log_msg("Bad magic number for object file. 0x%x or 0x%x expected, 0x%x found.\n",
102 MH_MAGIC
, MH_MAGIC_64
, header
.magic
);
106 if (header
.filetype
!= MH_OBJECT
)
108 log_msg("Bad filetype for object file. Currently only tested for MH_OBJECT.\n");
112 for (i
= 0; i
< header
.ncmds
; i
++)
114 struct load_command lc
;
116 memcpy(&lc
, buf
, sizeof(struct load_command
));
118 if (lc
.cmd
== LC_SEGMENT
)
120 uint8_t *seg_buf
= buf
;
122 struct segment_command seg_c
;
124 memcpy(&seg_c
, seg_buf
, sizeof(struct segment_command
));
125 seg_buf
+= sizeof(struct segment_command
);
127 /* Although each section is given it's own offset, nlist.n_value
128 * references the offset of the first section. This isn't
129 * apparent without debug information because the offset of the
130 * data section is the same as the first section. However, with
131 * debug sections mixed in, the offset of the debug section
132 * increases but n_value still references the first section.
134 if (seg_c
.nsects
< 1)
136 log_msg("Not enough sections\n");
140 memcpy(&s
, seg_buf
, sizeof(struct section
));
141 base_data_section
= s
.offset
;
143 else if (lc
.cmd
== LC_SEGMENT_64
)
145 uint8_t *seg_buf
= buf
;
147 struct segment_command_64 seg_c
;
149 memcpy(&seg_c
, seg_buf
, sizeof(struct segment_command_64
));
150 seg_buf
+= sizeof(struct segment_command_64
);
152 /* Explanation in LG_SEGMENT */
153 if (seg_c
.nsects
< 1)
155 log_msg("Not enough sections\n");
159 memcpy(&s
, seg_buf
, sizeof(struct section_64
));
160 base_data_section
= s
.offset
;
162 else if (lc
.cmd
== LC_SYMTAB
)
164 if (base_data_section
!= 0)
166 struct symtab_command sc
;
167 uint8_t *sym_buf
= base_buf
;
168 uint8_t *str_buf
= base_buf
;
170 memcpy(&sc
, buf
, sizeof(struct symtab_command
));
172 if (sc
.cmdsize
!= sizeof(struct symtab_command
))
174 log_msg("Can't find symbol table!\n");
178 sym_buf
+= sc
.symoff
;
179 str_buf
+= sc
.stroff
;
181 for (j
= 0; j
< sc
.nsyms
; j
++)
183 /* Location of string is cacluated each time from the
184 * start of the string buffer. On darwin the symbols
185 * are prefixed by "_", so we bump the pointer by 1.
186 * The target value is defined as an int in asm_*_offsets.c,
187 * which is 4 bytes on all targets we currently use.
194 memcpy(&nl
, sym_buf
, sizeof(struct nlist
));
195 sym_buf
+= sizeof(struct nlist
);
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
);
202 else /* if (bits == 64) */
207 memcpy(&nl
, sym_buf
, sizeof(struct nlist_64
));
208 sym_buf
+= sizeof(struct nlist_64
);
210 memcpy(&val
, base_buf
+ base_data_section
+ nl
.n_value
,
212 printf("%-40s EQU %5d\n",
213 str_buf
+ nl
.n_un
.n_strx
+ 1, val
);
228 int main(int argc
, char **argv
)
232 struct stat stat_buf
;
236 if (argc
< 2 || argc
> 3)
238 fprintf(stderr
, "Usage: %s [output format] <obj file>\n\n", argv
[0]);
239 fprintf(stderr
, " <obj file>\tMachO format object file to parse\n");
240 fprintf(stderr
, "Output Formats:\n");
241 fprintf(stderr
, " gas - compatible with GNU assembler\n");
242 fprintf(stderr
, " rvds - compatible with armasm\n");
248 if (!((!strcmp(argv
[1], "rvds")) || (!strcmp(argv
[1], "gas"))))
251 fd
= open(f
, O_RDONLY
);
255 perror("Unable to open file");
259 if (fstat(fd
, &stat_buf
))
265 file_buf
= malloc(stat_buf
.st_size
);
273 if (read(fd
, file_buf
, stat_buf
.st_size
) != stat_buf
.st_size
)
285 res
= parse_macho(file_buf
, stat_buf
.st_size
);
295 #elif defined(__ELF__)
298 #define COPY_STRUCT(dst, buf, ofst, sz) do {\
299 if(ofst + sizeof((*(dst))) > sz) goto bail;\
300 memcpy(dst, buf+ofst, sizeof((*(dst))));\
303 #define ENDIAN_ASSIGN(val, memb) do {\
304 if(!elf->le_data) {log_msg("Big Endian data not supported yet!\n");goto bail;}\
308 #define ENDIAN_ASSIGN_IN_PLACE(memb) do {\
309 ENDIAN_ASSIGN(memb, memb);\
314 uint8_t *buf
; /* Buffer containing ELF data */
315 size_t sz
; /* Buffer size */
316 int le_data
; /* Data is little-endian */
317 unsigned char e_ident
[EI_NIDENT
]; /* Magic number and other info */
318 int bits
; /* 32 or 64 */
323 int parse_elf_header(elf_obj_t
*elf
)
326 /* Verify ELF Magic numbers */
327 COPY_STRUCT(&elf
->e_ident
, elf
->buf
, 0, elf
->sz
);
328 res
= elf
->e_ident
[EI_MAG0
] == ELFMAG0
;
329 res
&= elf
->e_ident
[EI_MAG1
] == ELFMAG1
;
330 res
&= elf
->e_ident
[EI_MAG2
] == ELFMAG2
;
331 res
&= elf
->e_ident
[EI_MAG3
] == ELFMAG3
;
332 res
&= elf
->e_ident
[EI_CLASS
] == ELFCLASS32
333 || elf
->e_ident
[EI_CLASS
] == ELFCLASS64
;
334 res
&= elf
->e_ident
[EI_DATA
] == ELFDATA2LSB
;
338 elf
->le_data
= elf
->e_ident
[EI_DATA
] == ELFDATA2LSB
;
340 /* Read in relevant values */
341 if (elf
->e_ident
[EI_CLASS
] == ELFCLASS32
)
344 COPY_STRUCT(&elf
->hdr32
, elf
->buf
, 0, elf
->sz
);
346 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr32
.e_type
);
347 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr32
.e_machine
);
348 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr32
.e_version
);
349 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr32
.e_entry
);
350 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr32
.e_phoff
);
351 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr32
.e_shoff
);
352 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr32
.e_flags
);
353 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr32
.e_ehsize
);
354 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr32
.e_phentsize
);
355 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr32
.e_phnum
);
356 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr32
.e_shentsize
);
357 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr32
.e_shnum
);
358 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr32
.e_shstrndx
);
360 else /* if (elf->e_ident[EI_CLASS] == ELFCLASS64) */
363 COPY_STRUCT(&elf
->hdr64
, elf
->buf
, 0, elf
->sz
);
365 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr64
.e_type
);
366 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr64
.e_machine
);
367 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr64
.e_version
);
368 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr64
.e_entry
);
369 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr64
.e_phoff
);
370 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr64
.e_shoff
);
371 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr64
.e_flags
);
372 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr64
.e_ehsize
);
373 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr64
.e_phentsize
);
374 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr64
.e_phnum
);
375 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr64
.e_shentsize
);
376 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr64
.e_shnum
);
377 ENDIAN_ASSIGN_IN_PLACE(elf
->hdr64
.e_shstrndx
);
382 log_msg("Failed to parse ELF file header");
386 int parse_elf_section(elf_obj_t
*elf
, int idx
, Elf32_Shdr
*hdr32
, Elf64_Shdr
*hdr64
)
390 if (idx
>= elf
->hdr32
.e_shnum
)
393 COPY_STRUCT(hdr32
, elf
->buf
, elf
->hdr32
.e_shoff
+ idx
* elf
->hdr32
.e_shentsize
,
395 ENDIAN_ASSIGN_IN_PLACE(hdr32
->sh_name
);
396 ENDIAN_ASSIGN_IN_PLACE(hdr32
->sh_type
);
397 ENDIAN_ASSIGN_IN_PLACE(hdr32
->sh_flags
);
398 ENDIAN_ASSIGN_IN_PLACE(hdr32
->sh_addr
);
399 ENDIAN_ASSIGN_IN_PLACE(hdr32
->sh_offset
);
400 ENDIAN_ASSIGN_IN_PLACE(hdr32
->sh_size
);
401 ENDIAN_ASSIGN_IN_PLACE(hdr32
->sh_link
);
402 ENDIAN_ASSIGN_IN_PLACE(hdr32
->sh_info
);
403 ENDIAN_ASSIGN_IN_PLACE(hdr32
->sh_addralign
);
404 ENDIAN_ASSIGN_IN_PLACE(hdr32
->sh_entsize
);
406 else /* if (hdr64) */
408 if (idx
>= elf
->hdr64
.e_shnum
)
411 COPY_STRUCT(hdr64
, elf
->buf
, elf
->hdr64
.e_shoff
+ idx
* elf
->hdr64
.e_shentsize
,
413 ENDIAN_ASSIGN_IN_PLACE(hdr64
->sh_name
);
414 ENDIAN_ASSIGN_IN_PLACE(hdr64
->sh_type
);
415 ENDIAN_ASSIGN_IN_PLACE(hdr64
->sh_flags
);
416 ENDIAN_ASSIGN_IN_PLACE(hdr64
->sh_addr
);
417 ENDIAN_ASSIGN_IN_PLACE(hdr64
->sh_offset
);
418 ENDIAN_ASSIGN_IN_PLACE(hdr64
->sh_size
);
419 ENDIAN_ASSIGN_IN_PLACE(hdr64
->sh_link
);
420 ENDIAN_ASSIGN_IN_PLACE(hdr64
->sh_info
);
421 ENDIAN_ASSIGN_IN_PLACE(hdr64
->sh_addralign
);
422 ENDIAN_ASSIGN_IN_PLACE(hdr64
->sh_entsize
);
430 char *parse_elf_string_table(elf_obj_t
*elf
, int s_idx
, int idx
)
436 if (parse_elf_section(elf
, s_idx
, &shdr
, NULL
))
438 log_msg("Failed to parse ELF string table: section %d, index %d\n",
443 return (char *)(elf
->buf
+ shdr
.sh_offset
+ idx
);
445 else /* if (elf->bits == 64) */
449 if (parse_elf_section(elf
, s_idx
, NULL
, &shdr
))
451 log_msg("Failed to parse ELF string table: section %d, index %d\n",
456 return (char *)(elf
->buf
+ shdr
.sh_offset
+ idx
);
460 int parse_elf_symbol(elf_obj_t
*elf
, unsigned int ofst
, Elf32_Sym
*sym32
, Elf64_Sym
*sym64
)
464 COPY_STRUCT(sym32
, elf
->buf
, ofst
, elf
->sz
);
465 ENDIAN_ASSIGN_IN_PLACE(sym32
->st_name
);
466 ENDIAN_ASSIGN_IN_PLACE(sym32
->st_value
);
467 ENDIAN_ASSIGN_IN_PLACE(sym32
->st_size
);
468 ENDIAN_ASSIGN_IN_PLACE(sym32
->st_info
);
469 ENDIAN_ASSIGN_IN_PLACE(sym32
->st_other
);
470 ENDIAN_ASSIGN_IN_PLACE(sym32
->st_shndx
);
472 else /* if (sym64) */
474 COPY_STRUCT(sym64
, elf
->buf
, ofst
, elf
->sz
);
475 ENDIAN_ASSIGN_IN_PLACE(sym64
->st_name
);
476 ENDIAN_ASSIGN_IN_PLACE(sym64
->st_value
);
477 ENDIAN_ASSIGN_IN_PLACE(sym64
->st_size
);
478 ENDIAN_ASSIGN_IN_PLACE(sym64
->st_info
);
479 ENDIAN_ASSIGN_IN_PLACE(sym64
->st_other
);
480 ENDIAN_ASSIGN_IN_PLACE(sym64
->st_shndx
);
487 int parse_elf(uint8_t *buf
, size_t sz
, output_fmt_t mode
)
492 Elf32_Off strtab_off32
;
493 Elf64_Off strtab_off64
; /* save String Table offset for later use */
495 memset(&elf
, 0, sizeof(elf
));
500 if (parse_elf_header(&elf
))
506 for (i
= 0; i
< elf
.hdr32
.e_shnum
; i
++)
508 parse_elf_section(&elf
, i
, &shdr
, NULL
);
510 if (shdr
.sh_type
== SHT_STRTAB
)
512 char strtsb_name
[128];
514 strcpy(strtsb_name
, (char *)(elf
.buf
+ shdr
.sh_offset
+ shdr
.sh_name
));
516 if (!(strcmp(strtsb_name
, ".shstrtab")))
518 /* log_msg("found section: %s\n", strtsb_name); */
519 strtab_off32
= shdr
.sh_offset
;
525 else /* if (elf.bits == 64) */
528 for (i
= 0; i
< elf
.hdr64
.e_shnum
; i
++)
530 parse_elf_section(&elf
, i
, NULL
, &shdr
);
532 if (shdr
.sh_type
== SHT_STRTAB
)
534 char strtsb_name
[128];
536 strcpy(strtsb_name
, (char *)(elf
.buf
+ shdr
.sh_offset
+ shdr
.sh_name
));
538 if (!(strcmp(strtsb_name
, ".shstrtab")))
540 /* log_msg("found section: %s\n", strtsb_name); */
541 strtab_off64
= shdr
.sh_offset
;
548 /* Parse all Symbol Tables */
552 for (i
= 0; i
< elf
.hdr32
.e_shnum
; i
++)
554 parse_elf_section(&elf
, i
, &shdr
, NULL
);
556 if (shdr
.sh_type
== SHT_SYMTAB
)
558 for (ofst
= shdr
.sh_offset
;
559 ofst
< shdr
.sh_offset
+ shdr
.sh_size
;
560 ofst
+= shdr
.sh_entsize
)
564 parse_elf_symbol(&elf
, ofst
, &sym
, NULL
);
566 /* For all OBJECTS (data objects), extract the value from the
567 * proper data segment.
569 /* if (ELF32_ST_TYPE(sym.st_info) == STT_OBJECT && sym.st_name)
570 log_msg("found data object %s\n",
571 parse_elf_string_table(&elf,
576 if (ELF32_ST_TYPE(sym
.st_info
) == STT_OBJECT
581 char section_name
[128];
583 parse_elf_section(&elf
, sym
.st_shndx
, &dhdr
, NULL
);
585 /* For explanition - refer to _MSC_VER version of code */
586 strcpy(section_name
, (char *)(elf
.buf
+ strtab_off32
+ dhdr
.sh_name
));
587 /* log_msg("Section_name: %s, Section_type: %d\n", section_name, dhdr.sh_type); */
589 if (strcmp(section_name
, ".bss"))
591 if (sizeof(val
) != sym
.st_size
)
593 /* The target value is declared as an int in
594 * asm_*_offsets.c, which is 4 bytes on all
595 * targets we currently use. Complain loudly if
598 log_msg("Symbol size is wrong\n");
603 elf
.buf
+ dhdr
.sh_offset
+ sym
.st_value
,
609 log_msg("Big Endian data not supported yet!\n");
615 case OUTPUT_FMT_RVDS
:
616 printf("%-40s EQU %5d\n",
617 parse_elf_string_table(&elf
,
623 printf(".equ %-40s, %5d\n",
624 parse_elf_string_table(&elf
,
631 parse_elf_string_table(&elf
,
641 else /* if (elf.bits == 64) */
644 for (i
= 0; i
< elf
.hdr64
.e_shnum
; i
++)
646 parse_elf_section(&elf
, i
, NULL
, &shdr
);
648 if (shdr
.sh_type
== SHT_SYMTAB
)
650 for (ofst
= shdr
.sh_offset
;
651 ofst
< shdr
.sh_offset
+ shdr
.sh_size
;
652 ofst
+= shdr
.sh_entsize
)
656 parse_elf_symbol(&elf
, ofst
, NULL
, &sym
);
658 /* For all OBJECTS (data objects), extract the value from the
659 * proper data segment.
661 /* if (ELF64_ST_TYPE(sym.st_info) == STT_OBJECT && sym.st_name)
662 log_msg("found data object %s\n",
663 parse_elf_string_table(&elf,
668 if (ELF64_ST_TYPE(sym
.st_info
) == STT_OBJECT
673 char section_name
[128];
675 parse_elf_section(&elf
, sym
.st_shndx
, NULL
, &dhdr
);
677 /* For explanition - refer to _MSC_VER version of code */
678 strcpy(section_name
, (char *)(elf
.buf
+ strtab_off64
+ dhdr
.sh_name
));
679 /* log_msg("Section_name: %s, Section_type: %d\n", section_name, dhdr.sh_type); */
681 if ((strcmp(section_name
, ".bss")))
683 if (sizeof(val
) != sym
.st_size
)
685 /* The target value is declared as an int in
686 * asm_*_offsets.c, which is 4 bytes on all
687 * targets we currently use. Complain loudly if
690 log_msg("Symbol size is wrong\n");
695 elf
.buf
+ dhdr
.sh_offset
+ sym
.st_value
,
701 log_msg("Big Endian data not supported yet!\n");
707 case OUTPUT_FMT_RVDS
:
708 printf("%-40s EQU %5d\n",
709 parse_elf_string_table(&elf
,
715 printf(".equ %-40s, %5d\n",
716 parse_elf_string_table(&elf
,
723 parse_elf_string_table(&elf
,
734 if (mode
== OUTPUT_FMT_RVDS
)
739 log_msg("Parse error: File does not appear to be valid ELF32 or ELF64\n");
743 int main(int argc
, char **argv
)
748 struct stat stat_buf
;
752 if (argc
< 2 || argc
> 3)
754 fprintf(stderr
, "Usage: %s [output format] <obj file>\n\n", argv
[0]);
755 fprintf(stderr
, " <obj file>\tELF format object file to parse\n");
756 fprintf(stderr
, "Output Formats:\n");
757 fprintf(stderr
, " gas - compatible with GNU assembler\n");
758 fprintf(stderr
, " rvds - compatible with armasm\n");
764 if (!strcmp(argv
[1], "rvds"))
765 mode
= OUTPUT_FMT_RVDS
;
766 else if (!strcmp(argv
[1], "gas"))
767 mode
= OUTPUT_FMT_GAS
;
772 fd
= open(f
, O_RDONLY
);
776 perror("Unable to open file");
780 if (fstat(fd
, &stat_buf
))
786 file_buf
= malloc(stat_buf
.st_size
);
794 if (read(fd
, file_buf
, stat_buf
.st_size
) != stat_buf
.st_size
)
806 res
= parse_elf(file_buf
, stat_buf
.st_size
, mode
);
819 #if defined(_MSC_VER) || defined(__MINGW32__)
820 /* See "Microsoft Portable Executable and Common Object File Format Specification"
823 #define get_le32(x) ((*(x)) | (*(x+1)) << 8 |(*(x+2)) << 16 | (*(x+3)) << 24 )
824 #define get_le16(x) ((*(x)) | (*(x+1)) << 8)
826 int parse_coff(unsigned __int8
*buf
, size_t sz
)
828 unsigned int nsections
, symtab_ptr
, symtab_sz
, strtab_ptr
;
829 unsigned int sectionrawdata_ptr
;
831 unsigned __int8
*ptr
;
832 unsigned __int32 symoffset
;
834 char **sectionlist
; //this array holds all section names in their correct order.
835 //it is used to check if the symbol is in .bss or .data section.
837 nsections
= get_le16(buf
+ 2);
838 symtab_ptr
= get_le32(buf
+ 8);
839 symtab_sz
= get_le32(buf
+ 12);
840 strtab_ptr
= symtab_ptr
+ symtab_sz
* 18;
844 log_msg("Too many sections\n");
848 sectionlist
= malloc(nsections
* sizeof(sectionlist
));
850 if (sectionlist
== NULL
)
852 log_msg("Allocating first level of section list failed\n");
856 //log_msg("COFF: Found %u symbols in %u sections.\n", symtab_sz, nsections);
859 The size of optional header is always zero for an obj file. So, the section header
860 follows the file header immediately.
863 ptr
= buf
+ 20; //section header
865 for (i
= 0; i
< nsections
; i
++)
867 char sectionname
[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
868 strncpy(sectionname
, ptr
, 8);
869 //log_msg("COFF: Parsing section %s\n",sectionname);
871 sectionlist
[i
] = malloc(strlen(sectionname
) + 1);
873 if (sectionlist
[i
] == NULL
)
875 log_msg("Allocating storage for %s failed\n", sectionname
);
878 strcpy(sectionlist
[i
], sectionname
);
880 if (!strcmp(sectionname
, ".data")) sectionrawdata_ptr
= get_le32(ptr
+ 20);
885 //log_msg("COFF: Symbol table at offset %u\n", symtab_ptr);
886 //log_msg("COFF: raw data pointer ofset for section .data is %u\n", sectionrawdata_ptr);
888 /* The compiler puts the data with non-zero offset in .data section, but puts the data with
889 zero offset in .bss section. So, if the data in in .bss section, set offset=0.
890 Note from Wiki: In an object module compiled from C, the bss section contains
891 the local variables (but not functions) that were declared with the static keyword,
892 except for those with non-zero initial values. (In C, static variables are initialized
893 to zero by default.) It also contains the non-local (both extern and static) variables
894 that are also initialized to zero (either explicitly or by default).
896 //move to symbol table
897 /* COFF symbol table:
904 17 NumberOfAuxSymbols
906 ptr
= buf
+ symtab_ptr
;
908 for (i
= 0; i
< symtab_sz
; i
++)
910 __int16 section
= get_le16(ptr
+ 12); //section number
912 if (section
> 0 && ptr
[16] == 2)
914 //if(section > 0 && ptr[16] == 3 && get_le32(ptr+8)) {
918 char name
[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
919 strncpy(name
, ptr
, 8);
920 //log_msg("COFF: Parsing symbol %s\n",name);
921 /* +1 to avoid printing leading underscore */
922 printf("%-40s EQU ", name
+ 1);
926 //log_msg("COFF: Parsing symbol %s\n",
927 // buf + strtab_ptr + get_le32(ptr+4));
928 /* +1 to avoid printing leading underscore */
929 printf("%-40s EQU ", buf
+ strtab_ptr
+ get_le32(ptr
+ 4) + 1);
932 if (!(strcmp(sectionlist
[section
-1], ".bss")))
938 symoffset
= get_le32(buf
+ sectionrawdata_ptr
+ get_le32(ptr
+ 8));
941 //log_msg(" Section: %d\n",section);
942 //log_msg(" Class: %d\n",ptr[16]);
943 //log_msg(" Address: %u\n",get_le32(ptr+8));
944 //log_msg(" Offset: %u\n", symoffset);
946 printf("%5d\n", symoffset
);
954 for (i
= 0; i
< nsections
; i
++)
956 free(sectionlist
[i
]);
964 for (i
= 0; i
< nsections
; i
++)
966 free(sectionlist
[i
]);
974 int main(int argc
, char **argv
)
979 struct _stat stat_buf
;
980 unsigned __int8
*file_buf
;
983 if (argc
< 2 || argc
> 3)
985 fprintf(stderr
, "Usage: %s [output format] <obj file>\n\n", argv
[0]);
986 fprintf(stderr
, " <obj file>\tELF format object file to parse\n");
987 fprintf(stderr
, "Output Formats:\n");
988 fprintf(stderr
, " gas - compatible with GNU assembler\n");
989 fprintf(stderr
, " rvds - compatible with armasm\n");
995 if (!strcmp(argv
[1], "rvds"))
996 mode
= OUTPUT_FMT_RVDS
;
997 else if (!strcmp(argv
[1], "gas"))
998 mode
= OUTPUT_FMT_GAS
;
1002 fd
= _sopen(f
, _O_BINARY
, _SH_DENYNO
, _S_IREAD
| _S_IWRITE
);
1004 if (_fstat(fd
, &stat_buf
))
1010 file_buf
= malloc(stat_buf
.st_size
);
1018 if (_read(fd
, file_buf
, stat_buf
.st_size
) != stat_buf
.st_size
)
1030 res
= parse_coff(file_buf
, stat_buf
.st_size
);
1035 return EXIT_SUCCESS
;
1038 return EXIT_FAILURE
;