support obj_int_extract on cygwin
[libvpx.git] / build / make / obj_int_extract.c
blob04e14a6c8134cf205f0792ac232708bdfb899d91
1 /*
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.
9 */
12 #include <stdarg.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
17 #include "vpx_config.h"
18 #include "vpx/vpx_integer.h"
20 typedef enum
22 OUTPUT_FMT_PLAIN,
23 OUTPUT_FMT_RVDS,
24 OUTPUT_FMT_GAS,
25 } output_fmt_t;
27 int log_msg(const char *fmt, ...)
29 int res;
30 va_list ap;
31 va_start(ap, fmt);
32 res = vfprintf(stderr, fmt, ap);
33 va_end(ap);
34 return res;
37 #if defined(__GNUC__) && __GNUC__
38 #if defined(__MACH__)
40 #include <mach-o/loader.h>
41 #include <mach-o/nlist.h>
43 int parse_macho(uint8_t *base_buf, size_t sz)
45 int i, j;
46 struct mach_header header;
47 uint8_t *buf = base_buf;
48 int base_data_section = 0;
49 int bits = 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
55 * appropriately.
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)
64 bits = 32;
65 buf += sizeof(struct mach_header);
67 else
69 log_msg("Bad cputype for object file. Currently only tested for CPU_TYPE_[ARM|X86].\n");
70 goto bail;
73 else if (header.magic == MH_MAGIC_64)
75 if (header.cputype == CPU_TYPE_X86_64)
77 bits = 64;
78 buf += sizeof(struct mach_header_64);
80 else
82 log_msg("Bad cputype for object file. Currently only tested for CPU_TYPE_X86_64.\n");
83 goto bail;
86 else
88 log_msg("Bad magic number for object file. 0x%x or 0x%x expected, 0x%x found.\n",
89 MH_MAGIC, MH_MAGIC_64, header.magic);
90 goto bail;
93 if (header.filetype != MH_OBJECT)
95 log_msg("Bad filetype for object file. Currently only tested for MH_OBJECT.\n");
96 goto bail;
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;
108 struct section s;
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");
124 goto bail;
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;
133 struct section_64 s;
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");
143 goto bail;
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");
162 goto bail;
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.
176 if (bits == 32)
178 struct nlist nl;
179 int val;
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,
185 sizeof(val));
186 printf("%-40s EQU %5d\n",
187 str_buf + nl.n_un.n_strx + 1, val);
189 else /* if (bits == 64) */
191 struct nlist_64 nl;
192 int val;
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,
198 sizeof(val));
199 printf("%-40s EQU %5d\n",
200 str_buf + nl.n_un.n_strx + 1, val);
206 buf += lc.cmdsize;
209 return 0;
210 bail:
211 return 1;
215 #elif defined(__ELF__)
216 #include "elf.h"
218 #define COPY_STRUCT(dst, buf, ofst, sz) do {\
219 if(ofst + sizeof((*(dst))) > sz) goto bail;\
220 memcpy(dst, buf+ofst, sizeof((*(dst))));\
221 } while(0)
223 #define ENDIAN_ASSIGN(val, memb) do {\
224 if(!elf->le_data) {log_msg("Big Endian data not supported yet!\n");goto bail;}\
225 (val) = (memb);\
226 } while(0)
228 #define ENDIAN_ASSIGN_IN_PLACE(memb) do {\
229 ENDIAN_ASSIGN(memb, memb);\
230 } while(0)
232 typedef struct
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 */
239 Elf32_Ehdr hdr32;
240 Elf64_Ehdr hdr64;
241 } elf_obj_t;
243 int parse_elf_header(elf_obj_t *elf)
245 int res;
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;
256 if (!res) goto bail;
258 elf->le_data = elf->e_ident[EI_DATA] == ELFDATA2LSB;
260 /* Read in relevant values */
261 if (elf->e_ident[EI_CLASS] == ELFCLASS32)
263 elf->bits = 32;
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) */
282 elf->bits = 64;
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);
300 return 0;
301 bail:
302 log_msg("Failed to parse ELF file header");
303 return 1;
306 int parse_elf_section(elf_obj_t *elf, int idx, Elf32_Shdr *hdr32, Elf64_Shdr *hdr64)
308 if (hdr32)
310 if (idx >= elf->hdr32.e_shnum)
311 goto bail;
313 COPY_STRUCT(hdr32, elf->buf, elf->hdr32.e_shoff + idx * elf->hdr32.e_shentsize,
314 elf->sz);
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)
329 goto bail;
331 COPY_STRUCT(hdr64, elf->buf, elf->hdr64.e_shoff + idx * elf->hdr64.e_shentsize,
332 elf->sz);
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);
345 return 0;
346 bail:
347 return 1;
350 char *parse_elf_string_table(elf_obj_t *elf, int s_idx, int idx)
352 if (elf->bits == 32)
354 Elf32_Shdr shdr;
356 if (parse_elf_section(elf, s_idx, &shdr, NULL))
358 log_msg("Failed to parse ELF string table: section %d, index %d\n",
359 s_idx, idx);
360 return "";
363 return (char *)(elf->buf + shdr.sh_offset + idx);
365 else /* if (elf->bits == 64) */
367 Elf64_Shdr shdr;
369 if (parse_elf_section(elf, s_idx, NULL, &shdr))
371 log_msg("Failed to parse ELF string table: section %d, index %d\n",
372 s_idx, idx);
373 return "";
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)
382 if (sym32)
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);
402 return 0;
403 bail:
404 return 1;
407 int parse_elf(uint8_t *buf, size_t sz, output_fmt_t mode)
409 elf_obj_t elf;
410 unsigned int ofst;
411 int i;
412 Elf32_Off strtab_off32;
413 Elf64_Off strtab_off64; /* save String Table offset for later use */
415 memset(&elf, 0, sizeof(elf));
416 elf.buf = buf;
417 elf.sz = sz;
419 /* Parse Header */
420 if (parse_elf_header(&elf))
421 goto bail;
423 if (elf.bits == 32)
425 Elf32_Shdr shdr;
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;
440 break;
445 else /* if (elf.bits == 64) */
447 Elf64_Shdr shdr;
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;
462 break;
468 /* Parse all Symbol Tables */
469 if (elf.bits == 32)
471 Elf32_Shdr shdr;
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)
482 Elf32_Sym sym;
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,
492 shdr.sh_link,
493 sym.st_name));
496 if (ELF32_ST_TYPE(sym.st_info) == STT_OBJECT
497 && sym.st_size == 4)
499 Elf32_Shdr dhdr;
500 int val = 0;
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
516 * this is not true.
518 log_msg("Symbol size is wrong\n");
519 goto bail;
522 memcpy(&val,
523 elf.buf + dhdr.sh_offset + sym.st_value,
524 sym.st_size);
527 if (!elf.le_data)
529 log_msg("Big Endian data not supported yet!\n");
530 goto bail;
533 switch (mode)
535 case OUTPUT_FMT_RVDS:
536 printf("%-40s EQU %5d\n",
537 parse_elf_string_table(&elf,
538 shdr.sh_link,
539 sym.st_name),
540 val);
541 break;
542 case OUTPUT_FMT_GAS:
543 printf(".equ %-40s, %5d\n",
544 parse_elf_string_table(&elf,
545 shdr.sh_link,
546 sym.st_name),
547 val);
548 break;
549 default:
550 printf("%s = %d\n",
551 parse_elf_string_table(&elf,
552 shdr.sh_link,
553 sym.st_name),
554 val);
561 else /* if (elf.bits == 64) */
563 Elf64_Shdr shdr;
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)
574 Elf64_Sym sym;
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,
584 shdr.sh_link,
585 sym.st_name));
588 if (ELF64_ST_TYPE(sym.st_info) == STT_OBJECT
589 && sym.st_size == 4)
591 Elf64_Shdr dhdr;
592 int val = 0;
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
608 * this is not true.
610 log_msg("Symbol size is wrong\n");
611 goto bail;
614 memcpy(&val,
615 elf.buf + dhdr.sh_offset + sym.st_value,
616 sym.st_size);
619 if (!elf.le_data)
621 log_msg("Big Endian data not supported yet!\n");
622 goto bail;
625 switch (mode)
627 case OUTPUT_FMT_RVDS:
628 printf("%-40s EQU %5d\n",
629 parse_elf_string_table(&elf,
630 shdr.sh_link,
631 sym.st_name),
632 val);
633 break;
634 case OUTPUT_FMT_GAS:
635 printf(".equ %-40s, %5d\n",
636 parse_elf_string_table(&elf,
637 shdr.sh_link,
638 sym.st_name),
639 val);
640 break;
641 default:
642 printf("%s = %d\n",
643 parse_elf_string_table(&elf,
644 shdr.sh_link,
645 sym.st_name),
646 val);
654 if (mode == OUTPUT_FMT_RVDS)
655 printf(" END\n");
657 return 0;
658 bail:
659 log_msg("Parse error: File does not appear to be valid ELF32 or ELF64\n");
660 return 1;
663 #endif
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"
669 for reference.
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;
678 unsigned int i;
679 uint8_t *ptr;
680 uint32_t symoffset;
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;
690 if (nsections > 96)
692 log_msg("Too many sections\n");
693 return 1;
696 sectionlist = malloc(nsections * sizeof(sectionlist));
698 if (sectionlist == NULL)
700 log_msg("Allocating first level of section list failed\n");
701 return 1;
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);
724 goto bail;
726 strcpy(sectionlist[i], sectionname);
728 if (!strcmp(sectionname, ".data")) sectionrawdata_ptr = get_le32(ptr + 20);
730 ptr += 40;
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:
746 offset field
747 0 Name(*)
748 8 Value
749 12 SectionNumber
750 14 Type
751 16 StorageClass
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)) {
764 if (get_le32(ptr))
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
772 if (name[0] == '_')
773 printf("%-40s EQU ", name + 1);
774 else
775 printf("%-40s EQU ", name);
777 else
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] == '_')
782 printf("%-40s EQU ",
783 buf + strtab_ptr + get_le32(ptr + 4) + 1);
784 else
785 printf("%-40s EQU ", buf + strtab_ptr + get_le32(ptr + 4));
788 if (!(strcmp(sectionlist[section-1], ".bss")))
790 symoffset = 0;
792 else
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);
805 ptr += 18;
808 printf(" END\n");
810 for (i = 0; i < nsections; i++)
812 free(sectionlist[i]);
815 free(sectionlist);
817 return 0;
818 bail:
820 for (i = 0; i < nsections; i++)
822 free(sectionlist[i]);
825 free(sectionlist);
827 return 1;
829 #endif /* defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN__) */
831 int main(int argc, char **argv)
833 output_fmt_t mode = OUTPUT_FMT_PLAIN;
834 const char *f;
835 uint8_t *file_buf;
836 int res;
837 FILE *fp;
838 long int file_size;
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");
847 goto bail;
850 f = argv[2];
852 if (!strcmp(argv[1], "rvds"))
853 mode = OUTPUT_FMT_RVDS;
854 else if (!strcmp(argv[1], "gas"))
855 mode = OUTPUT_FMT_GAS;
856 else
857 f = argv[1];
859 fp = fopen(f, "rb");
861 if (!fp)
863 perror("Unable to open file");
864 goto bail;
867 if (fseek(fp, 0, SEEK_END))
869 perror("stat");
870 goto bail;
873 file_size = ftell(fp);
874 file_buf = malloc(file_size);
876 if (!file_buf)
878 perror("malloc");
879 goto bail;
882 rewind(fp);
884 if (fread(file_buf, sizeof(char), file_size, fp) != file_size)
886 perror("read");
887 goto bail;
890 if (fclose(fp))
892 perror("close");
893 goto bail;
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);
901 #endif
902 #endif
903 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN__)
904 res = parse_coff(file_buf, file_size);
905 #endif
907 free(file_buf);
909 if (!res)
910 return EXIT_SUCCESS;
912 bail:
913 return EXIT_FAILURE;