Fix windows build: obj_int_extract to extract numbrs from .rdata
[profile/ivi/libvpx.git] / build / make / obj_int_extract.c
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  */
10
11
12 #include <stdarg.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16
17 #include "vpx_config.h"
18 #include "vpx/vpx_integer.h"
19
20 typedef enum
21 {
22     OUTPUT_FMT_PLAIN,
23     OUTPUT_FMT_RVDS,
24     OUTPUT_FMT_GAS,
25 } output_fmt_t;
26
27 int log_msg(const char *fmt, ...)
28 {
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;
35 }
36
37 #if defined(__GNUC__) && __GNUC__
38 #if defined(__MACH__)
39
40 #include <mach-o/loader.h>
41 #include <mach-o/nlist.h>
42
43 int parse_macho(uint8_t *base_buf, size_t sz)
44 {
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;
50
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.
56      */
57     memcpy(&header, buf, sizeof(struct mach_header));
58
59     if (header.magic == MH_MAGIC)
60     {
61         if (header.cputype == CPU_TYPE_ARM
62             || header.cputype == CPU_TYPE_X86)
63         {
64             bits = 32;
65             buf += sizeof(struct mach_header);
66         }
67         else
68         {
69             log_msg("Bad cputype for object file. Currently only tested for CPU_TYPE_[ARM|X86].\n");
70             goto bail;
71         }
72     }
73     else if (header.magic == MH_MAGIC_64)
74     {
75         if (header.cputype == CPU_TYPE_X86_64)
76         {
77             bits = 64;
78             buf += sizeof(struct mach_header_64);
79         }
80         else
81         {
82             log_msg("Bad cputype for object file. Currently only tested for CPU_TYPE_X86_64.\n");
83             goto bail;
84         }
85     }
86     else
87     {
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     }
92
93     if (header.filetype != MH_OBJECT)
94     {
95         log_msg("Bad filetype for object file. Currently only tested for MH_OBJECT.\n");
96         goto bail;
97     }
98
99     for (i = 0; i < header.ncmds; i++)
100     {
101         struct load_command lc;
102
103         memcpy(&lc, buf, sizeof(struct load_command));
104
105         if (lc.cmd == LC_SEGMENT)
106         {
107             uint8_t *seg_buf = buf;
108             struct section s;
109             struct segment_command seg_c;
110
111             memcpy(&seg_c, seg_buf, sizeof(struct segment_command));
112             seg_buf += sizeof(struct segment_command);
113
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.
120              */
121             if (seg_c.nsects < 1)
122             {
123                 log_msg("Not enough sections\n");
124                 goto bail;
125             }
126
127             memcpy(&s, seg_buf, sizeof(struct section));
128             base_data_section = s.offset;
129         }
130         else if (lc.cmd == LC_SEGMENT_64)
131         {
132             uint8_t *seg_buf = buf;
133             struct section_64 s;
134             struct segment_command_64 seg_c;
135
136             memcpy(&seg_c, seg_buf, sizeof(struct segment_command_64));
137             seg_buf += sizeof(struct segment_command_64);
138
139             /* Explanation in LG_SEGMENT */
140             if (seg_c.nsects < 1)
141             {
142                 log_msg("Not enough sections\n");
143                 goto bail;
144             }
145
146             memcpy(&s, seg_buf, sizeof(struct section_64));
147             base_data_section = s.offset;
148         }
149         else if (lc.cmd == LC_SYMTAB)
150         {
151             if (base_data_section != 0)
152             {
153                 struct symtab_command sc;
154                 uint8_t *sym_buf = base_buf;
155                 uint8_t *str_buf = base_buf;
156
157                 memcpy(&sc, buf, sizeof(struct symtab_command));
158
159                 if (sc.cmdsize != sizeof(struct symtab_command))
160                 {
161                     log_msg("Can't find symbol table!\n");
162                     goto bail;
163                 }
164
165                 sym_buf += sc.symoff;
166                 str_buf += sc.stroff;
167
168                 for (j = 0; j < sc.nsyms; j++)
169                 {
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.
175                      */
176                     if (bits == 32)
177                     {
178                         struct nlist nl;
179                         int val;
180
181                         memcpy(&nl, sym_buf, sizeof(struct nlist));
182                         sym_buf += sizeof(struct nlist);
183
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);
188                     }
189                     else /* if (bits == 64) */
190                     {
191                         struct nlist_64 nl;
192                         int val;
193
194                         memcpy(&nl, sym_buf, sizeof(struct nlist_64));
195                         sym_buf += sizeof(struct nlist_64);
196
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);
201                     }
202                 }
203             }
204         }
205
206         buf += lc.cmdsize;
207     }
208
209     return 0;
210 bail:
211     return 1;
212
213 }
214
215 #elif defined(__ELF__)
216 #include "elf.h"
217
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)
222
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)
227
228 #define ENDIAN_ASSIGN_IN_PLACE(memb) do {\
229         ENDIAN_ASSIGN(memb, memb);\
230     } while(0)
231
232 typedef struct
233 {
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;
242
243 int parse_elf_header(elf_obj_t *elf)
244 {
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;
255
256     if (!res) goto bail;
257
258     elf->le_data = elf->e_ident[EI_DATA] == ELFDATA2LSB;
259
260     /* Read in relevant values */
261     if (elf->e_ident[EI_CLASS] == ELFCLASS32)
262     {
263         elf->bits = 32;
264         COPY_STRUCT(&elf->hdr32, elf->buf, 0, elf->sz);
265
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);
279     }
280     else /* if (elf->e_ident[EI_CLASS] == ELFCLASS64) */
281     {
282         elf->bits = 64;
283         COPY_STRUCT(&elf->hdr64, elf->buf, 0, elf->sz);
284
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);
298     }
299
300     return 0;
301 bail:
302     log_msg("Failed to parse ELF file header");
303     return 1;
304 }
305
306 int parse_elf_section(elf_obj_t *elf, int idx, Elf32_Shdr *hdr32, Elf64_Shdr *hdr64)
307 {
308     if (hdr32)
309     {
310         if (idx >= elf->hdr32.e_shnum)
311             goto bail;
312
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);
325     }
326     else /* if (hdr64) */
327     {
328         if (idx >= elf->hdr64.e_shnum)
329             goto bail;
330
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);
343     }
344
345     return 0;
346 bail:
347     return 1;
348 }
349
350 char *parse_elf_string_table(elf_obj_t *elf, int s_idx, int idx)
351 {
352     if (elf->bits == 32)
353     {
354         Elf32_Shdr shdr;
355
356         if (parse_elf_section(elf, s_idx, &shdr, NULL))
357         {
358             log_msg("Failed to parse ELF string table: section %d, index %d\n",
359                     s_idx, idx);
360             return "";
361         }
362
363         return (char *)(elf->buf + shdr.sh_offset + idx);
364     }
365     else /* if (elf->bits == 64) */
366     {
367         Elf64_Shdr shdr;
368
369         if (parse_elf_section(elf, s_idx, NULL, &shdr))
370         {
371             log_msg("Failed to parse ELF string table: section %d, index %d\n",
372                     s_idx, idx);
373             return "";
374         }
375
376         return (char *)(elf->buf + shdr.sh_offset + idx);
377     }
378 }
379
380 int parse_elf_symbol(elf_obj_t *elf, unsigned int ofst, Elf32_Sym *sym32, Elf64_Sym *sym64)
381 {
382     if (sym32)
383     {
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);
391     }
392     else /* if (sym64) */
393     {
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);
401     }
402     return 0;
403 bail:
404     return 1;
405 }
406
407 int parse_elf(uint8_t *buf, size_t sz, output_fmt_t mode)
408 {
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 */
414
415     memset(&elf, 0, sizeof(elf));
416     elf.buf = buf;
417     elf.sz = sz;
418
419     /* Parse Header */
420     if (parse_elf_header(&elf))
421       goto bail;
422
423     if (elf.bits == 32)
424     {
425         Elf32_Shdr shdr;
426         for (i = 0; i < elf.hdr32.e_shnum; i++)
427         {
428             parse_elf_section(&elf, i, &shdr, NULL);
429
430             if (shdr.sh_type == SHT_STRTAB)
431             {
432                 char strtsb_name[128];
433
434                 strcpy(strtsb_name, (char *)(elf.buf + shdr.sh_offset + shdr.sh_name));
435
436                 if (!(strcmp(strtsb_name, ".shstrtab")))
437                 {
438                     /* log_msg("found section: %s\n", strtsb_name); */
439                     strtab_off32 = shdr.sh_offset;
440                     break;
441                 }
442             }
443         }
444     }
445     else /* if (elf.bits == 64) */
446     {
447         Elf64_Shdr shdr;
448         for (i = 0; i < elf.hdr64.e_shnum; i++)
449         {
450             parse_elf_section(&elf, i, NULL, &shdr);
451
452             if (shdr.sh_type == SHT_STRTAB)
453             {
454                 char strtsb_name[128];
455
456                 strcpy(strtsb_name, (char *)(elf.buf + shdr.sh_offset + shdr.sh_name));
457
458                 if (!(strcmp(strtsb_name, ".shstrtab")))
459                 {
460                     /* log_msg("found section: %s\n", strtsb_name); */
461                     strtab_off64 = shdr.sh_offset;
462                     break;
463                 }
464             }
465         }
466     }
467
468     /* Parse all Symbol Tables */
469     if (elf.bits == 32)
470     {
471         Elf32_Shdr shdr;
472         for (i = 0; i < elf.hdr32.e_shnum; i++)
473         {
474             parse_elf_section(&elf, i, &shdr, NULL);
475
476             if (shdr.sh_type == SHT_SYMTAB)
477             {
478                 for (ofst = shdr.sh_offset;
479                      ofst < shdr.sh_offset + shdr.sh_size;
480                      ofst += shdr.sh_entsize)
481                 {
482                     Elf32_Sym sym;
483
484                     parse_elf_symbol(&elf, ofst, &sym, NULL);
485
486                     /* For all OBJECTS (data objects), extract the value from the
487                      * proper data segment.
488                      */
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));
494                      */
495
496                     if (ELF32_ST_TYPE(sym.st_info) == STT_OBJECT
497                         && sym.st_size == 4)
498                     {
499                         Elf32_Shdr dhdr;
500                         int val = 0;
501                         char section_name[128];
502
503                         parse_elf_section(&elf, sym.st_shndx, &dhdr, NULL);
504
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); */
508
509                         if (strcmp(section_name, ".bss"))
510                         {
511                             if (sizeof(val) != sym.st_size)
512                             {
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.
517                                  */
518                                 log_msg("Symbol size is wrong\n");
519                                 goto bail;
520                             }
521
522                             memcpy(&val,
523                                    elf.buf + dhdr.sh_offset + sym.st_value,
524                                    sym.st_size);
525                         }
526
527                         if (!elf.le_data)
528                         {
529                             log_msg("Big Endian data not supported yet!\n");
530                             goto bail;
531                         }
532
533                         switch (mode)
534                         {
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);
555                         }
556                     }
557                 }
558             }
559         }
560     }
561     else /* if (elf.bits == 64) */
562     {
563         Elf64_Shdr shdr;
564         for (i = 0; i < elf.hdr64.e_shnum; i++)
565         {
566             parse_elf_section(&elf, i, NULL, &shdr);
567
568             if (shdr.sh_type == SHT_SYMTAB)
569             {
570                 for (ofst = shdr.sh_offset;
571                      ofst < shdr.sh_offset + shdr.sh_size;
572                      ofst += shdr.sh_entsize)
573                 {
574                     Elf64_Sym sym;
575
576                     parse_elf_symbol(&elf, ofst, NULL, &sym);
577
578                     /* For all OBJECTS (data objects), extract the value from the
579                      * proper data segment.
580                      */
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));
586                      */
587
588                     if (ELF64_ST_TYPE(sym.st_info) == STT_OBJECT
589                         && sym.st_size == 4)
590                     {
591                         Elf64_Shdr dhdr;
592                         int val = 0;
593                         char section_name[128];
594
595                         parse_elf_section(&elf, sym.st_shndx, NULL, &dhdr);
596
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); */
600
601                         if ((strcmp(section_name, ".bss")))
602                         {
603                             if (sizeof(val) != sym.st_size)
604                             {
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.
609                                  */
610                                 log_msg("Symbol size is wrong\n");
611                                 goto bail;
612                             }
613
614                             memcpy(&val,
615                                    elf.buf + dhdr.sh_offset + sym.st_value,
616                                    sym.st_size);
617                         }
618
619                         if (!elf.le_data)
620                         {
621                             log_msg("Big Endian data not supported yet!\n");
622                             goto bail;
623                         }
624
625                         switch (mode)
626                         {
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);
647                         }
648                     }
649                 }
650             }
651         }
652     }
653
654     if (mode == OUTPUT_FMT_RVDS)
655         printf("    END\n");
656
657     return 0;
658 bail:
659     log_msg("Parse error: File does not appear to be valid ELF32 or ELF64\n");
660     return 1;
661 }
662
663 #endif
664 #endif /* defined(__GNUC__) && __GNUC__ */
665
666
667 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN__)
668 /*  See "Microsoft Portable Executable and Common Object File Format Specification"
669     for reference.
670 */
671 #define get_le32(x) ((*(x)) | (*(x+1)) << 8 |(*(x+2)) << 16 | (*(x+3)) << 24 )
672 #define get_le16(x) ((*(x)) | (*(x+1)) << 8)
673
674 int parse_coff(uint8_t *buf, size_t sz)
675 {
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;
681
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 .rdata section.
684
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;
689
690     if (nsections > 96)
691     {
692         log_msg("Too many sections\n");
693         return 1;
694     }
695
696     sectionlist = malloc(nsections * sizeof(sectionlist));
697
698     if (sectionlist == NULL)
699     {
700         log_msg("Allocating first level of section list failed\n");
701         return 1;
702     }
703
704     //log_msg("COFF: Found %u symbols in %u sections.\n", symtab_sz, nsections);
705
706     /*
707     The size of optional header is always zero for an obj file. So, the section header
708     follows the file header immediately.
709     */
710
711     ptr = buf + 20;     //section header
712
713     for (i = 0; i < nsections; i++)
714     {
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);
718
719         sectionlist[i] = malloc(strlen(sectionname) + 1);
720
721         if (sectionlist[i] == NULL)
722         {
723             log_msg("Allocating storage for %s failed\n", sectionname);
724             goto bail;
725         }
726         strcpy(sectionlist[i], sectionname);
727
728         if (!strcmp(sectionname, ".rdata")) sectionrawdata_ptr = get_le32(ptr + 20);
729
730         ptr += 40;
731     }
732
733     //log_msg("COFF: Symbol table at offset %u\n", symtab_ptr);
734     //log_msg("COFF: raw data pointer ofset for section .rdata is %u\n", sectionrawdata_ptr);
735
736     /*  The compiler puts the data with non-zero offset in .rdata 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).
743         */
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
753         */
754     ptr = buf + symtab_ptr;
755
756     for (i = 0; i < symtab_sz; i++)
757     {
758         int16_t section = get_le16(ptr + 12); //section number
759
760         if (section > 0 && ptr[16] == 2)
761         {
762             //if(section > 0 && ptr[16] == 3 && get_le32(ptr+8)) {
763
764             if (get_le32(ptr))
765             {
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
771                  */
772                 if (name[0] == '_')
773                     printf("%-40s EQU ", name + 1);
774                 else
775                     printf("%-40s EQU ", name);
776             }
777             else
778             {
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));
786             }
787
788             if (!(strcmp(sectionlist[section-1], ".bss")))
789             {
790                 symoffset = 0;
791             }
792             else
793             {
794                 symoffset = get_le32(buf + sectionrawdata_ptr + get_le32(ptr + 8));
795             }
796
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);
801
802             printf("%5d\n", symoffset);
803         }
804
805         ptr += 18;
806     }
807
808     printf("    END\n");
809
810     for (i = 0; i < nsections; i++)
811     {
812         free(sectionlist[i]);
813     }
814
815     free(sectionlist);
816
817     return 0;
818 bail:
819
820     for (i = 0; i < nsections; i++)
821     {
822         free(sectionlist[i]);
823     }
824
825     free(sectionlist);
826
827     return 1;
828 }
829 #endif /* defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN__) */
830
831 int main(int argc, char **argv)
832 {
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;
839
840     if (argc < 2 || argc > 3)
841     {
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;
848     }
849
850     f = argv[2];
851
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];
858
859     fp = fopen(f, "rb");
860
861     if (!fp)
862     {
863         perror("Unable to open file");
864         goto bail;
865     }
866
867     if (fseek(fp, 0, SEEK_END))
868     {
869         perror("stat");
870         goto bail;
871     }
872
873     file_size = ftell(fp);
874     file_buf = malloc(file_size);
875
876     if (!file_buf)
877     {
878         perror("malloc");
879         goto bail;
880     }
881
882     rewind(fp);
883
884     if (fread(file_buf, sizeof(char), file_size, fp) != file_size)
885     {
886         perror("read");
887         goto bail;
888     }
889
890     if (fclose(fp))
891     {
892         perror("close");
893         goto bail;
894     }
895
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
906
907     free(file_buf);
908
909     if (!res)
910         return EXIT_SUCCESS;
911
912 bail:
913     return EXIT_FAILURE;
914 }