Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 *
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 {
21 OUTPUT_FMT_PLAIN,
22 OUTPUT_FMT_RVDS,
23 OUTPUT_FMT_GAS,
24 } output_fmt_t;
26 int log_msg(const char *fmt, ...) {
27 int res;
28 va_list ap;
29 va_start(ap, fmt);
30 res = vfprintf(stderr, fmt, ap);
31 va_end(ap);
32 return res;
33 }
35 #if defined(__GNUC__) && __GNUC__
36 #if defined(__MACH__)
38 #include <mach-o/loader.h>
39 #include <mach-o/nlist.h>
41 int print_macho_equ(output_fmt_t mode, uint8_t* name, int val) {
42 switch (mode) {
43 case OUTPUT_FMT_RVDS:
44 printf("%-40s EQU %5d\n", name, val);
45 return 0;
46 case OUTPUT_FMT_GAS:
47 printf(".set %-40s, %5d\n", name, val);
48 return 0;
49 default:
50 log_msg("Unsupported mode: %d", mode);
51 return 1;
52 }
53 }
55 int parse_macho(uint8_t *base_buf, size_t sz, output_fmt_t mode) {
56 int i, j;
57 struct mach_header header;
58 uint8_t *buf = base_buf;
59 int base_data_section = 0;
60 int bits = 0;
62 /* We can read in mach_header for 32 and 64 bit architectures
63 * because it's identical to mach_header_64 except for the last
64 * element (uint32_t reserved), which we don't use. Then, when
65 * we know which architecture we're looking at, increment buf
66 * appropriately.
67 */
68 memcpy(&header, buf, sizeof(struct mach_header));
70 if (header.magic == MH_MAGIC) {
71 if (header.cputype == CPU_TYPE_ARM
72 || header.cputype == CPU_TYPE_X86) {
73 bits = 32;
74 buf += sizeof(struct mach_header);
75 } else {
76 log_msg("Bad cputype for object file. Currently only tested for CPU_TYPE_[ARM|X86].\n");
77 goto bail;
78 }
79 } else if (header.magic == MH_MAGIC_64) {
80 if (header.cputype == CPU_TYPE_X86_64) {
81 bits = 64;
82 buf += sizeof(struct mach_header_64);
83 } else {
84 log_msg("Bad cputype for object file. Currently only tested for CPU_TYPE_X86_64.\n");
85 goto bail;
86 }
87 } 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;
91 }
93 if (header.filetype != MH_OBJECT) {
94 log_msg("Bad filetype for object file. Currently only tested for MH_OBJECT.\n");
95 goto bail;
96 }
98 for (i = 0; i < header.ncmds; i++) {
99 struct load_command lc;
101 memcpy(&lc, buf, sizeof(struct load_command));
103 if (lc.cmd == LC_SEGMENT) {
104 uint8_t *seg_buf = buf;
105 struct section s;
106 struct segment_command seg_c;
108 memcpy(&seg_c, seg_buf, sizeof(struct segment_command));
109 seg_buf += sizeof(struct segment_command);
111 /* Although each section is given it's own offset, nlist.n_value
112 * references the offset of the first section. This isn't
113 * apparent without debug information because the offset of the
114 * data section is the same as the first section. However, with
115 * debug sections mixed in, the offset of the debug section
116 * increases but n_value still references the first section.
117 */
118 if (seg_c.nsects < 1) {
119 log_msg("Not enough sections\n");
120 goto bail;
121 }
123 memcpy(&s, seg_buf, sizeof(struct section));
124 base_data_section = s.offset;
125 } else if (lc.cmd == LC_SEGMENT_64) {
126 uint8_t *seg_buf = buf;
127 struct section_64 s;
128 struct segment_command_64 seg_c;
130 memcpy(&seg_c, seg_buf, sizeof(struct segment_command_64));
131 seg_buf += sizeof(struct segment_command_64);
133 /* Explanation in LG_SEGMENT */
134 if (seg_c.nsects < 1) {
135 log_msg("Not enough sections\n");
136 goto bail;
137 }
139 memcpy(&s, seg_buf, sizeof(struct section_64));
140 base_data_section = s.offset;
141 } else if (lc.cmd == LC_SYMTAB) {
142 if (base_data_section != 0) {
143 struct symtab_command sc;
144 uint8_t *sym_buf = base_buf;
145 uint8_t *str_buf = base_buf;
147 memcpy(&sc, buf, sizeof(struct symtab_command));
149 if (sc.cmdsize != sizeof(struct symtab_command)) {
150 log_msg("Can't find symbol table!\n");
151 goto bail;
152 }
154 sym_buf += sc.symoff;
155 str_buf += sc.stroff;
157 for (j = 0; j < sc.nsyms; j++) {
158 /* Location of string is cacluated each time from the
159 * start of the string buffer. On darwin the symbols
160 * are prefixed by "_", so we bump the pointer by 1.
161 * The target value is defined as an int in *_asm_*_offsets.c,
162 * which is 4 bytes on all targets we currently use.
163 */
164 if (bits == 32) {
165 struct nlist nl;
166 int val;
168 memcpy(&nl, sym_buf, sizeof(struct nlist));
169 sym_buf += sizeof(struct nlist);
171 memcpy(&val, base_buf + base_data_section + nl.n_value,
172 sizeof(val));
173 print_macho_equ(mode, str_buf + nl.n_un.n_strx + 1, val);
174 } else { /* if (bits == 64) */
175 struct nlist_64 nl;
176 int val;
178 memcpy(&nl, sym_buf, sizeof(struct nlist_64));
179 sym_buf += sizeof(struct nlist_64);
181 memcpy(&val, base_buf + base_data_section + nl.n_value,
182 sizeof(val));
183 print_macho_equ(mode, str_buf + nl.n_un.n_strx + 1, val);
184 }
185 }
186 }
187 }
189 buf += lc.cmdsize;
190 }
192 return 0;
193 bail:
194 return 1;
196 }
198 #elif defined(__ELF__)
199 #include "elf.h"
201 #define COPY_STRUCT(dst, buf, ofst, sz) do {\
202 if(ofst + sizeof((*(dst))) > sz) goto bail;\
203 memcpy(dst, buf+ofst, sizeof((*(dst))));\
204 } while(0)
206 #define ENDIAN_ASSIGN(val, memb) do {\
207 if(!elf->le_data) {log_msg("Big Endian data not supported yet!\n");goto bail;}\
208 (val) = (memb);\
209 } while(0)
211 #define ENDIAN_ASSIGN_IN_PLACE(memb) do {\
212 ENDIAN_ASSIGN(memb, memb);\
213 } while(0)
215 typedef struct {
216 uint8_t *buf; /* Buffer containing ELF data */
217 size_t sz; /* Buffer size */
218 int le_data; /* Data is little-endian */
219 unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */
220 int bits; /* 32 or 64 */
221 Elf32_Ehdr hdr32;
222 Elf64_Ehdr hdr64;
223 } elf_obj_t;
225 int parse_elf_header(elf_obj_t *elf) {
226 int res;
227 /* Verify ELF Magic numbers */
228 COPY_STRUCT(&elf->e_ident, elf->buf, 0, elf->sz);
229 res = elf->e_ident[EI_MAG0] == ELFMAG0;
230 res &= elf->e_ident[EI_MAG1] == ELFMAG1;
231 res &= elf->e_ident[EI_MAG2] == ELFMAG2;
232 res &= elf->e_ident[EI_MAG3] == ELFMAG3;
233 res &= elf->e_ident[EI_CLASS] == ELFCLASS32
234 || elf->e_ident[EI_CLASS] == ELFCLASS64;
235 res &= elf->e_ident[EI_DATA] == ELFDATA2LSB;
237 if (!res) goto bail;
239 elf->le_data = elf->e_ident[EI_DATA] == ELFDATA2LSB;
241 /* Read in relevant values */
242 if (elf->e_ident[EI_CLASS] == ELFCLASS32) {
243 elf->bits = 32;
244 COPY_STRUCT(&elf->hdr32, elf->buf, 0, elf->sz);
246 ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_type);
247 ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_machine);
248 ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_version);
249 ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_entry);
250 ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_phoff);
251 ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_shoff);
252 ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_flags);
253 ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_ehsize);
254 ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_phentsize);
255 ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_phnum);
256 ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_shentsize);
257 ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_shnum);
258 ENDIAN_ASSIGN_IN_PLACE(elf->hdr32.e_shstrndx);
259 } else { /* if (elf->e_ident[EI_CLASS] == ELFCLASS64) */
260 elf->bits = 64;
261 COPY_STRUCT(&elf->hdr64, elf->buf, 0, elf->sz);
263 ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_type);
264 ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_machine);
265 ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_version);
266 ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_entry);
267 ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_phoff);
268 ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_shoff);
269 ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_flags);
270 ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_ehsize);
271 ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_phentsize);
272 ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_phnum);
273 ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_shentsize);
274 ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_shnum);
275 ENDIAN_ASSIGN_IN_PLACE(elf->hdr64.e_shstrndx);
276 }
278 return 0;
279 bail:
280 log_msg("Failed to parse ELF file header");
281 return 1;
282 }
284 int parse_elf_section(elf_obj_t *elf, int idx, Elf32_Shdr *hdr32, Elf64_Shdr *hdr64) {
285 if (hdr32) {
286 if (idx >= elf->hdr32.e_shnum)
287 goto bail;
289 COPY_STRUCT(hdr32, elf->buf, elf->hdr32.e_shoff + idx * elf->hdr32.e_shentsize,
290 elf->sz);
291 ENDIAN_ASSIGN_IN_PLACE(hdr32->sh_name);
292 ENDIAN_ASSIGN_IN_PLACE(hdr32->sh_type);
293 ENDIAN_ASSIGN_IN_PLACE(hdr32->sh_flags);
294 ENDIAN_ASSIGN_IN_PLACE(hdr32->sh_addr);
295 ENDIAN_ASSIGN_IN_PLACE(hdr32->sh_offset);
296 ENDIAN_ASSIGN_IN_PLACE(hdr32->sh_size);
297 ENDIAN_ASSIGN_IN_PLACE(hdr32->sh_link);
298 ENDIAN_ASSIGN_IN_PLACE(hdr32->sh_info);
299 ENDIAN_ASSIGN_IN_PLACE(hdr32->sh_addralign);
300 ENDIAN_ASSIGN_IN_PLACE(hdr32->sh_entsize);
301 } else { /* if (hdr64) */
302 if (idx >= elf->hdr64.e_shnum)
303 goto bail;
305 COPY_STRUCT(hdr64, elf->buf, elf->hdr64.e_shoff + idx * elf->hdr64.e_shentsize,
306 elf->sz);
307 ENDIAN_ASSIGN_IN_PLACE(hdr64->sh_name);
308 ENDIAN_ASSIGN_IN_PLACE(hdr64->sh_type);
309 ENDIAN_ASSIGN_IN_PLACE(hdr64->sh_flags);
310 ENDIAN_ASSIGN_IN_PLACE(hdr64->sh_addr);
311 ENDIAN_ASSIGN_IN_PLACE(hdr64->sh_offset);
312 ENDIAN_ASSIGN_IN_PLACE(hdr64->sh_size);
313 ENDIAN_ASSIGN_IN_PLACE(hdr64->sh_link);
314 ENDIAN_ASSIGN_IN_PLACE(hdr64->sh_info);
315 ENDIAN_ASSIGN_IN_PLACE(hdr64->sh_addralign);
316 ENDIAN_ASSIGN_IN_PLACE(hdr64->sh_entsize);
317 }
319 return 0;
320 bail:
321 return 1;
322 }
324 char *parse_elf_string_table(elf_obj_t *elf, int s_idx, int idx) {
325 if (elf->bits == 32) {
326 Elf32_Shdr shdr;
328 if (parse_elf_section(elf, s_idx, &shdr, NULL)) {
329 log_msg("Failed to parse ELF string table: section %d, index %d\n",
330 s_idx, idx);
331 return "";
332 }
334 return (char *)(elf->buf + shdr.sh_offset + idx);
335 } else { /* if (elf->bits == 64) */
336 Elf64_Shdr shdr;
338 if (parse_elf_section(elf, s_idx, NULL, &shdr)) {
339 log_msg("Failed to parse ELF string table: section %d, index %d\n",
340 s_idx, idx);
341 return "";
342 }
344 return (char *)(elf->buf + shdr.sh_offset + idx);
345 }
346 }
348 int parse_elf_symbol(elf_obj_t *elf, unsigned int ofst, Elf32_Sym *sym32, Elf64_Sym *sym64) {
349 if (sym32) {
350 COPY_STRUCT(sym32, elf->buf, ofst, elf->sz);
351 ENDIAN_ASSIGN_IN_PLACE(sym32->st_name);
352 ENDIAN_ASSIGN_IN_PLACE(sym32->st_value);
353 ENDIAN_ASSIGN_IN_PLACE(sym32->st_size);
354 ENDIAN_ASSIGN_IN_PLACE(sym32->st_info);
355 ENDIAN_ASSIGN_IN_PLACE(sym32->st_other);
356 ENDIAN_ASSIGN_IN_PLACE(sym32->st_shndx);
357 } else { /* if (sym64) */
358 COPY_STRUCT(sym64, elf->buf, ofst, elf->sz);
359 ENDIAN_ASSIGN_IN_PLACE(sym64->st_name);
360 ENDIAN_ASSIGN_IN_PLACE(sym64->st_value);
361 ENDIAN_ASSIGN_IN_PLACE(sym64->st_size);
362 ENDIAN_ASSIGN_IN_PLACE(sym64->st_info);
363 ENDIAN_ASSIGN_IN_PLACE(sym64->st_other);
364 ENDIAN_ASSIGN_IN_PLACE(sym64->st_shndx);
365 }
366 return 0;
367 bail:
368 return 1;
369 }
371 int parse_elf(uint8_t *buf, size_t sz, output_fmt_t mode) {
372 elf_obj_t elf;
373 unsigned int ofst;
374 int i;
375 Elf32_Off strtab_off32;
376 Elf64_Off strtab_off64; /* save String Table offset for later use */
378 memset(&elf, 0, sizeof(elf));
379 elf.buf = buf;
380 elf.sz = sz;
382 /* Parse Header */
383 if (parse_elf_header(&elf))
384 goto bail;
386 if (elf.bits == 32) {
387 Elf32_Shdr shdr;
388 for (i = 0; i < elf.hdr32.e_shnum; i++) {
389 parse_elf_section(&elf, i, &shdr, NULL);
391 if (shdr.sh_type == SHT_STRTAB) {
392 char strtsb_name[128];
394 strcpy(strtsb_name, (char *)(elf.buf + shdr.sh_offset + shdr.sh_name));
396 if (!(strcmp(strtsb_name, ".shstrtab"))) {
397 /* log_msg("found section: %s\n", strtsb_name); */
398 strtab_off32 = shdr.sh_offset;
399 break;
400 }
401 }
402 }
403 } else { /* if (elf.bits == 64) */
404 Elf64_Shdr shdr;
405 for (i = 0; i < elf.hdr64.e_shnum; i++) {
406 parse_elf_section(&elf, i, NULL, &shdr);
408 if (shdr.sh_type == SHT_STRTAB) {
409 char strtsb_name[128];
411 strcpy(strtsb_name, (char *)(elf.buf + shdr.sh_offset + shdr.sh_name));
413 if (!(strcmp(strtsb_name, ".shstrtab"))) {
414 /* log_msg("found section: %s\n", strtsb_name); */
415 strtab_off64 = shdr.sh_offset;
416 break;
417 }
418 }
419 }
420 }
422 /* Parse all Symbol Tables */
423 if (elf.bits == 32) {
424 Elf32_Shdr shdr;
425 for (i = 0; i < elf.hdr32.e_shnum; i++) {
426 parse_elf_section(&elf, i, &shdr, NULL);
428 if (shdr.sh_type == SHT_SYMTAB) {
429 for (ofst = shdr.sh_offset;
430 ofst < shdr.sh_offset + shdr.sh_size;
431 ofst += shdr.sh_entsize) {
432 Elf32_Sym sym;
434 parse_elf_symbol(&elf, ofst, &sym, NULL);
436 /* For all OBJECTS (data objects), extract the value from the
437 * proper data segment.
438 */
439 /* if (ELF32_ST_TYPE(sym.st_info) == STT_OBJECT && sym.st_name)
440 log_msg("found data object %s\n",
441 parse_elf_string_table(&elf,
442 shdr.sh_link,
443 sym.st_name));
444 */
446 if (ELF32_ST_TYPE(sym.st_info) == STT_OBJECT
447 && sym.st_size == 4) {
448 Elf32_Shdr dhdr;
449 int val = 0;
450 char section_name[128];
452 parse_elf_section(&elf, sym.st_shndx, &dhdr, NULL);
454 /* For explanition - refer to _MSC_VER version of code */
455 strcpy(section_name, (char *)(elf.buf + strtab_off32 + dhdr.sh_name));
456 /* log_msg("Section_name: %s, Section_type: %d\n", section_name, dhdr.sh_type); */
458 if (strcmp(section_name, ".bss")) {
459 if (sizeof(val) != sym.st_size) {
460 /* The target value is declared as an int in
461 * *_asm_*_offsets.c, which is 4 bytes on all
462 * targets we currently use. Complain loudly if
463 * this is not true.
464 */
465 log_msg("Symbol size is wrong\n");
466 goto bail;
467 }
469 memcpy(&val,
470 elf.buf + dhdr.sh_offset + sym.st_value,
471 sym.st_size);
472 }
474 if (!elf.le_data) {
475 log_msg("Big Endian data not supported yet!\n");
476 goto bail;
477 }
479 switch (mode) {
480 case OUTPUT_FMT_RVDS:
481 printf("%-40s EQU %5d\n",
482 parse_elf_string_table(&elf,
483 shdr.sh_link,
484 sym.st_name),
485 val);
486 break;
487 case OUTPUT_FMT_GAS:
488 printf(".equ %-40s, %5d\n",
489 parse_elf_string_table(&elf,
490 shdr.sh_link,
491 sym.st_name),
492 val);
493 break;
494 default:
495 printf("%s = %d\n",
496 parse_elf_string_table(&elf,
497 shdr.sh_link,
498 sym.st_name),
499 val);
500 }
501 }
502 }
503 }
504 }
505 } else { /* if (elf.bits == 64) */
506 Elf64_Shdr shdr;
507 for (i = 0; i < elf.hdr64.e_shnum; i++) {
508 parse_elf_section(&elf, i, NULL, &shdr);
510 if (shdr.sh_type == SHT_SYMTAB) {
511 for (ofst = shdr.sh_offset;
512 ofst < shdr.sh_offset + shdr.sh_size;
513 ofst += shdr.sh_entsize) {
514 Elf64_Sym sym;
516 parse_elf_symbol(&elf, ofst, NULL, &sym);
518 /* For all OBJECTS (data objects), extract the value from the
519 * proper data segment.
520 */
521 /* if (ELF64_ST_TYPE(sym.st_info) == STT_OBJECT && sym.st_name)
522 log_msg("found data object %s\n",
523 parse_elf_string_table(&elf,
524 shdr.sh_link,
525 sym.st_name));
526 */
528 if (ELF64_ST_TYPE(sym.st_info) == STT_OBJECT
529 && sym.st_size == 4) {
530 Elf64_Shdr dhdr;
531 int val = 0;
532 char section_name[128];
534 parse_elf_section(&elf, sym.st_shndx, NULL, &dhdr);
536 /* For explanition - refer to _MSC_VER version of code */
537 strcpy(section_name, (char *)(elf.buf + strtab_off64 + dhdr.sh_name));
538 /* log_msg("Section_name: %s, Section_type: %d\n", section_name, dhdr.sh_type); */
540 if ((strcmp(section_name, ".bss"))) {
541 if (sizeof(val) != sym.st_size) {
542 /* The target value is declared as an int in
543 * *_asm_*_offsets.c, which is 4 bytes on all
544 * targets we currently use. Complain loudly if
545 * this is not true.
546 */
547 log_msg("Symbol size is wrong\n");
548 goto bail;
549 }
551 memcpy(&val,
552 elf.buf + dhdr.sh_offset + sym.st_value,
553 sym.st_size);
554 }
556 if (!elf.le_data) {
557 log_msg("Big Endian data not supported yet!\n");
558 goto bail;
559 }
561 switch (mode) {
562 case OUTPUT_FMT_RVDS:
563 printf("%-40s EQU %5d\n",
564 parse_elf_string_table(&elf,
565 shdr.sh_link,
566 sym.st_name),
567 val);
568 break;
569 case OUTPUT_FMT_GAS:
570 printf(".equ %-40s, %5d\n",
571 parse_elf_string_table(&elf,
572 shdr.sh_link,
573 sym.st_name),
574 val);
575 break;
576 default:
577 printf("%s = %d\n",
578 parse_elf_string_table(&elf,
579 shdr.sh_link,
580 sym.st_name),
581 val);
582 }
583 }
584 }
585 }
586 }
587 }
589 if (mode == OUTPUT_FMT_RVDS)
590 printf(" END\n");
592 return 0;
593 bail:
594 log_msg("Parse error: File does not appear to be valid ELF32 or ELF64\n");
595 return 1;
596 }
598 #endif
599 #endif /* defined(__GNUC__) && __GNUC__ */
602 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN__)
603 /* See "Microsoft Portable Executable and Common Object File Format Specification"
604 for reference.
605 */
606 #define get_le32(x) ((*(x)) | (*(x+1)) << 8 |(*(x+2)) << 16 | (*(x+3)) << 24 )
607 #define get_le16(x) ((*(x)) | (*(x+1)) << 8)
609 int parse_coff(uint8_t *buf, size_t sz) {
610 unsigned int nsections, symtab_ptr, symtab_sz, strtab_ptr;
611 unsigned int sectionrawdata_ptr;
612 unsigned int i;
613 uint8_t *ptr;
614 uint32_t symoffset;
616 char **sectionlist; // this array holds all section names in their correct order.
617 // it is used to check if the symbol is in .bss or .rdata section.
619 nsections = get_le16(buf + 2);
620 symtab_ptr = get_le32(buf + 8);
621 symtab_sz = get_le32(buf + 12);
622 strtab_ptr = symtab_ptr + symtab_sz * 18;
624 if (nsections > 96) {
625 log_msg("Too many sections\n");
626 return 1;
627 }
629 sectionlist = malloc(nsections * sizeof(sectionlist));
631 if (sectionlist == NULL) {
632 log_msg("Allocating first level of section list failed\n");
633 return 1;
634 }
636 // log_msg("COFF: Found %u symbols in %u sections.\n", symtab_sz, nsections);
638 /*
639 The size of optional header is always zero for an obj file. So, the section header
640 follows the file header immediately.
641 */
643 ptr = buf + 20; // section header
645 for (i = 0; i < nsections; i++) {
646 char sectionname[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
647 strncpy(sectionname, ptr, 8);
648 // log_msg("COFF: Parsing section %s\n",sectionname);
650 sectionlist[i] = malloc(strlen(sectionname) + 1);
652 if (sectionlist[i] == NULL) {
653 log_msg("Allocating storage for %s failed\n", sectionname);
654 goto bail;
655 }
656 strcpy(sectionlist[i], sectionname);
658 if (!strcmp(sectionname, ".rdata")) sectionrawdata_ptr = get_le32(ptr + 20);
660 ptr += 40;
661 }
663 // log_msg("COFF: Symbol table at offset %u\n", symtab_ptr);
664 // log_msg("COFF: raw data pointer ofset for section .rdata is %u\n", sectionrawdata_ptr);
666 /* The compiler puts the data with non-zero offset in .rdata section, but puts the data with
667 zero offset in .bss section. So, if the data in in .bss section, set offset=0.
668 Note from Wiki: In an object module compiled from C, the bss section contains
669 the local variables (but not functions) that were declared with the static keyword,
670 except for those with non-zero initial values. (In C, static variables are initialized
671 to zero by default.) It also contains the non-local (both extern and static) variables
672 that are also initialized to zero (either explicitly or by default).
673 */
674 // move to symbol table
675 /* COFF symbol table:
676 offset field
677 0 Name(*)
678 8 Value
679 12 SectionNumber
680 14 Type
681 16 StorageClass
682 17 NumberOfAuxSymbols
683 */
684 ptr = buf + symtab_ptr;
686 for (i = 0; i < symtab_sz; i++) {
687 int16_t section = get_le16(ptr + 12); // section number
689 if (section > 0 && ptr[16] == 2) {
690 // if(section > 0 && ptr[16] == 3 && get_le32(ptr+8)) {
692 if (get_le32(ptr)) {
693 char name[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
694 strncpy(name, ptr, 8);
695 // log_msg("COFF: Parsing symbol %s\n",name);
696 /* The 64bit Windows compiler doesn't prefix with an _.
697 * Check what's there, and bump if necessary
698 */
699 if (name[0] == '_')
700 printf("%-40s EQU ", name + 1);
701 else
702 printf("%-40s EQU ", name);
703 } else {
704 // log_msg("COFF: Parsing symbol %s\n",
705 // buf + strtab_ptr + get_le32(ptr+4));
706 if ((buf + strtab_ptr + get_le32(ptr + 4))[0] == '_')
707 printf("%-40s EQU ",
708 buf + strtab_ptr + get_le32(ptr + 4) + 1);
709 else
710 printf("%-40s EQU ", buf + strtab_ptr + get_le32(ptr + 4));
711 }
713 if (!(strcmp(sectionlist[section - 1], ".bss"))) {
714 symoffset = 0;
715 } else {
716 symoffset = get_le32(buf + sectionrawdata_ptr + get_le32(ptr + 8));
717 }
719 // log_msg(" Section: %d\n",section);
720 // log_msg(" Class: %d\n",ptr[16]);
721 // log_msg(" Address: %u\n",get_le32(ptr+8));
722 // log_msg(" Offset: %u\n", symoffset);
724 printf("%5d\n", symoffset);
725 }
727 ptr += 18;
728 }
730 printf(" END\n");
732 for (i = 0; i < nsections; i++) {
733 free(sectionlist[i]);
734 }
736 free(sectionlist);
738 return 0;
739 bail:
741 for (i = 0; i < nsections; i++) {
742 free(sectionlist[i]);
743 }
745 free(sectionlist);
747 return 1;
748 }
749 #endif /* defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN__) */
751 int main(int argc, char **argv) {
752 output_fmt_t mode = OUTPUT_FMT_PLAIN;
753 const char *f;
754 uint8_t *file_buf;
755 int res;
756 FILE *fp;
757 long int file_size;
759 if (argc < 2 || argc > 3) {
760 fprintf(stderr, "Usage: %s [output format] <obj file>\n\n", argv[0]);
761 fprintf(stderr, " <obj file>\tobject file to parse\n");
762 fprintf(stderr, "Output Formats:\n");
763 fprintf(stderr, " gas - compatible with GNU assembler\n");
764 fprintf(stderr, " rvds - compatible with armasm\n");
765 goto bail;
766 }
768 f = argv[2];
770 if (!strcmp(argv[1], "rvds"))
771 mode = OUTPUT_FMT_RVDS;
772 else if (!strcmp(argv[1], "gas"))
773 mode = OUTPUT_FMT_GAS;
774 else
775 f = argv[1];
777 fp = fopen(f, "rb");
779 if (!fp) {
780 perror("Unable to open file");
781 goto bail;
782 }
784 if (fseek(fp, 0, SEEK_END)) {
785 perror("stat");
786 goto bail;
787 }
789 file_size = ftell(fp);
790 file_buf = malloc(file_size);
792 if (!file_buf) {
793 perror("malloc");
794 goto bail;
795 }
797 rewind(fp);
799 if (fread(file_buf, sizeof(char), file_size, fp) != file_size) {
800 perror("read");
801 goto bail;
802 }
804 if (fclose(fp)) {
805 perror("close");
806 goto bail;
807 }
809 #if defined(__GNUC__) && __GNUC__
810 #if defined(__MACH__)
811 res = parse_macho(file_buf, file_size, mode);
812 #elif defined(__ELF__)
813 res = parse_elf(file_buf, file_size, mode);
814 #endif
815 #endif
816 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN__)
817 res = parse_coff(file_buf, file_size);
818 #endif
820 free(file_buf);
822 if (!res)
823 return EXIT_SUCCESS;
825 bail:
826 return EXIT_FAILURE;
827 }