| OLD | NEW | 
|---|
| 1 /* | 1 /* | 
| 2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved. | 2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved. | 
| 3  * | 3  * | 
| 4  *  Use of this source code is governed by a BSD-style license | 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 | 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 | 6  *  tree. An additional intellectual property rights grant can be found | 
| 7  *  in the file PATENTS.  All contributing project authors may | 7  *  in the file PATENTS.  All contributing project authors may | 
| 8  *  be found in the AUTHORS file in the root of the source tree. | 8  *  be found in the AUTHORS file in the root of the source tree. | 
| 9  */ | 9  */ | 
| 10 | 10 | 
| 11 | 11 | 
|  | 12 #include <stdarg.h> | 
| 12 #include <stdio.h> | 13 #include <stdio.h> | 
| 13 #include <stdlib.h> | 14 #include <stdlib.h> | 
|  | 15 #include <string.h> | 
| 14 | 16 | 
| 15 #include "vpx_config.h" | 17 #include "vpx_config.h" | 
| 16 |  | 
| 17 #if defined(_MSC_VER) || defined(__MINGW32__) |  | 
| 18 #include <io.h> |  | 
| 19 #include <share.h> |  | 
| 20 #include "vpx/vpx_integer.h" | 18 #include "vpx/vpx_integer.h" | 
| 21 #else |  | 
| 22 #include <stdint.h> |  | 
| 23 #include <unistd.h> |  | 
| 24 #endif |  | 
| 25 |  | 
| 26 #include <string.h> |  | 
| 27 #include <sys/types.h> |  | 
| 28 #include <sys/stat.h> |  | 
| 29 #include <fcntl.h> |  | 
| 30 #include <stdarg.h> |  | 
| 31 | 19 | 
| 32 typedef enum | 20 typedef enum | 
| 33 { | 21 { | 
| 34     OUTPUT_FMT_PLAIN, | 22     OUTPUT_FMT_PLAIN, | 
| 35     OUTPUT_FMT_RVDS, | 23     OUTPUT_FMT_RVDS, | 
| 36     OUTPUT_FMT_GAS, | 24     OUTPUT_FMT_GAS, | 
| 37 } output_fmt_t; | 25 } output_fmt_t; | 
| 38 | 26 | 
| 39 int log_msg(const char *fmt, ...) | 27 int log_msg(const char *fmt, ...) | 
| 40 { | 28 { | 
| 41     int res; | 29     int res; | 
| 42     va_list ap; | 30     va_list ap; | 
| 43     va_start(ap, fmt); | 31     va_start(ap, fmt); | 
| 44     res = vfprintf(stderr, fmt, ap); | 32     res = vfprintf(stderr, fmt, ap); | 
| 45     va_end(ap); | 33     va_end(ap); | 
| 46     return res; | 34     return res; | 
| 47 } | 35 } | 
| 48 | 36 | 
| 49 #if defined(__GNUC__) && __GNUC__ | 37 #if defined(__GNUC__) && __GNUC__ | 
| 50 |  | 
| 51 #if defined(__MACH__) | 38 #if defined(__MACH__) | 
| 52 | 39 | 
| 53 #include <mach-o/loader.h> | 40 #include <mach-o/loader.h> | 
| 54 #include <mach-o/nlist.h> | 41 #include <mach-o/nlist.h> | 
| 55 | 42 | 
| 56 int parse_macho(uint8_t *base_buf, size_t sz) | 43 int parse_macho(uint8_t *base_buf, size_t sz) | 
| 57 { | 44 { | 
| 58     int i, j; | 45     int i, j; | 
| 59     struct mach_header header; | 46     struct mach_header header; | 
| 60     uint8_t *buf = base_buf; | 47     uint8_t *buf = base_buf; | 
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 218 | 205 | 
| 219         buf += lc.cmdsize; | 206         buf += lc.cmdsize; | 
| 220     } | 207     } | 
| 221 | 208 | 
| 222     return 0; | 209     return 0; | 
| 223 bail: | 210 bail: | 
| 224     return 1; | 211     return 1; | 
| 225 | 212 | 
| 226 } | 213 } | 
| 227 | 214 | 
| 228 int main(int argc, char **argv) |  | 
| 229 { |  | 
| 230     int fd; |  | 
| 231     char *f; |  | 
| 232     struct stat stat_buf; |  | 
| 233     uint8_t *file_buf; |  | 
| 234     int res; |  | 
| 235 |  | 
| 236     if (argc < 2 || argc > 3) |  | 
| 237     { |  | 
| 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"); |  | 
| 243         goto bail; |  | 
| 244     } |  | 
| 245 |  | 
| 246     f = argv[2]; |  | 
| 247 |  | 
| 248     if (!((!strcmp(argv[1], "rvds")) || (!strcmp(argv[1], "gas")))) |  | 
| 249         f = argv[1]; |  | 
| 250 |  | 
| 251     fd = open(f, O_RDONLY); |  | 
| 252 |  | 
| 253     if (fd < 0) |  | 
| 254     { |  | 
| 255         perror("Unable to open file"); |  | 
| 256         goto bail; |  | 
| 257     } |  | 
| 258 |  | 
| 259     if (fstat(fd, &stat_buf)) |  | 
| 260     { |  | 
| 261         perror("stat"); |  | 
| 262         goto bail; |  | 
| 263     } |  | 
| 264 |  | 
| 265     file_buf = malloc(stat_buf.st_size); |  | 
| 266 |  | 
| 267     if (!file_buf) |  | 
| 268     { |  | 
| 269         perror("malloc"); |  | 
| 270         goto bail; |  | 
| 271     } |  | 
| 272 |  | 
| 273     if (read(fd, file_buf, stat_buf.st_size) != stat_buf.st_size) |  | 
| 274     { |  | 
| 275         perror("read"); |  | 
| 276         goto bail; |  | 
| 277     } |  | 
| 278 |  | 
| 279     if (close(fd)) |  | 
| 280     { |  | 
| 281         perror("close"); |  | 
| 282         goto bail; |  | 
| 283     } |  | 
| 284 |  | 
| 285     res = parse_macho(file_buf, stat_buf.st_size); |  | 
| 286     free(file_buf); |  | 
| 287 |  | 
| 288     if (!res) |  | 
| 289         return EXIT_SUCCESS; |  | 
| 290 |  | 
| 291 bail: |  | 
| 292     return EXIT_FAILURE; |  | 
| 293 } |  | 
| 294 |  | 
| 295 #elif defined(__ELF__) | 215 #elif defined(__ELF__) | 
| 296 #include "elf.h" | 216 #include "elf.h" | 
| 297 | 217 | 
| 298 #define COPY_STRUCT(dst, buf, ofst, sz) do {\ | 218 #define COPY_STRUCT(dst, buf, ofst, sz) do {\ | 
| 299         if(ofst + sizeof((*(dst))) > sz) goto bail;\ | 219         if(ofst + sizeof((*(dst))) > sz) goto bail;\ | 
| 300         memcpy(dst, buf+ofst, sizeof((*(dst))));\ | 220         memcpy(dst, buf+ofst, sizeof((*(dst))));\ | 
| 301     } while(0) | 221     } while(0) | 
| 302 | 222 | 
| 303 #define ENDIAN_ASSIGN(val, memb) do {\ | 223 #define ENDIAN_ASSIGN(val, memb) do {\ | 
| 304         if(!elf->le_data) {log_msg("Big Endian data not supported yet!\n");goto 
      bail;}\ | 224         if(!elf->le_data) {log_msg("Big Endian data not supported yet!\n");goto 
      bail;}\ | 
| (...skipping 428 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 733 | 653 | 
| 734     if (mode == OUTPUT_FMT_RVDS) | 654     if (mode == OUTPUT_FMT_RVDS) | 
| 735         printf("    END\n"); | 655         printf("    END\n"); | 
| 736 | 656 | 
| 737     return 0; | 657     return 0; | 
| 738 bail: | 658 bail: | 
| 739     log_msg("Parse error: File does not appear to be valid ELF32 or ELF64\n"); | 659     log_msg("Parse error: File does not appear to be valid ELF32 or ELF64\n"); | 
| 740     return 1; | 660     return 1; | 
| 741 } | 661 } | 
| 742 | 662 | 
| 743 int main(int argc, char **argv) | 663 #endif | 
| 744 { | 664 #endif /* defined(__GNUC__) && __GNUC__ */ | 
| 745     int fd; |  | 
| 746     output_fmt_t mode; |  | 
| 747     char *f; |  | 
| 748     struct stat stat_buf; |  | 
| 749     uint8_t *file_buf; |  | 
| 750     int res; |  | 
| 751 |  | 
| 752     if (argc < 2 || argc > 3) |  | 
| 753     { |  | 
| 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"); |  | 
| 759         goto bail; |  | 
| 760     } |  | 
| 761 |  | 
| 762     f = argv[2]; |  | 
| 763 |  | 
| 764     if (!strcmp(argv[1], "rvds")) |  | 
| 765         mode = OUTPUT_FMT_RVDS; |  | 
| 766     else if (!strcmp(argv[1], "gas")) |  | 
| 767         mode = OUTPUT_FMT_GAS; |  | 
| 768     else |  | 
| 769         f = argv[1]; |  | 
| 770 | 665 | 
| 771 | 666 | 
| 772     fd = open(f, O_RDONLY); | 667 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN__) | 
| 773 |  | 
| 774     if (fd < 0) |  | 
| 775     { |  | 
| 776         perror("Unable to open file"); |  | 
| 777         goto bail; |  | 
| 778     } |  | 
| 779 |  | 
| 780     if (fstat(fd, &stat_buf)) |  | 
| 781     { |  | 
| 782         perror("stat"); |  | 
| 783         goto bail; |  | 
| 784     } |  | 
| 785 |  | 
| 786     file_buf = malloc(stat_buf.st_size); |  | 
| 787 |  | 
| 788     if (!file_buf) |  | 
| 789     { |  | 
| 790         perror("malloc"); |  | 
| 791         goto bail; |  | 
| 792     } |  | 
| 793 |  | 
| 794     if (read(fd, file_buf, stat_buf.st_size) != stat_buf.st_size) |  | 
| 795     { |  | 
| 796         perror("read"); |  | 
| 797         goto bail; |  | 
| 798     } |  | 
| 799 |  | 
| 800     if (close(fd)) |  | 
| 801     { |  | 
| 802         perror("close"); |  | 
| 803         goto bail; |  | 
| 804     } |  | 
| 805 |  | 
| 806     res = parse_elf(file_buf, stat_buf.st_size, mode); |  | 
| 807     free(file_buf); |  | 
| 808 |  | 
| 809     if (!res) |  | 
| 810         return EXIT_SUCCESS; |  | 
| 811 |  | 
| 812 bail: |  | 
| 813     return EXIT_FAILURE; |  | 
| 814 } |  | 
| 815 #endif |  | 
| 816 #endif |  | 
| 817 |  | 
| 818 |  | 
| 819 #if defined(_MSC_VER) || defined(__MINGW32__) |  | 
| 820 /*  See "Microsoft Portable Executable and Common Object File Format Specificati
      on" | 668 /*  See "Microsoft Portable Executable and Common Object File Format Specificati
      on" | 
| 821     for reference. | 669     for reference. | 
| 822 */ | 670 */ | 
| 823 #define get_le32(x) ((*(x)) | (*(x+1)) << 8 |(*(x+2)) << 16 | (*(x+3)) << 24 ) | 671 #define get_le32(x) ((*(x)) | (*(x+1)) << 8 |(*(x+2)) << 16 | (*(x+3)) << 24 ) | 
| 824 #define get_le16(x) ((*(x)) | (*(x+1)) << 8) | 672 #define get_le16(x) ((*(x)) | (*(x+1)) << 8) | 
| 825 | 673 | 
| 826 int parse_coff(unsigned __int8 *buf, size_t sz) | 674 int parse_coff(uint8_t *buf, size_t sz) | 
| 827 { | 675 { | 
| 828     unsigned int nsections, symtab_ptr, symtab_sz, strtab_ptr; | 676     unsigned int nsections, symtab_ptr, symtab_sz, strtab_ptr; | 
| 829     unsigned int sectionrawdata_ptr; | 677     unsigned int sectionrawdata_ptr; | 
| 830     unsigned int i; | 678     unsigned int i; | 
| 831     unsigned __int8 *ptr; | 679     uint8_t *ptr; | 
| 832     unsigned __int32 symoffset; | 680     uint32_t symoffset; | 
| 833 | 681 | 
| 834     char **sectionlist;  //this array holds all section names in their correct o
      rder. | 682     char **sectionlist;  //this array holds all section names in their correct o
      rder. | 
| 835     //it is used to check if the symbol is in .bss or .data section. | 683     //it is used to check if the symbol is in .bss or .data section. | 
| 836 | 684 | 
| 837     nsections = get_le16(buf + 2); | 685     nsections = get_le16(buf + 2); | 
| 838     symtab_ptr = get_le32(buf + 8); | 686     symtab_ptr = get_le32(buf + 8); | 
| 839     symtab_sz = get_le32(buf + 12); | 687     symtab_sz = get_le32(buf + 12); | 
| 840     strtab_ptr = symtab_ptr + symtab_sz * 18; | 688     strtab_ptr = symtab_ptr + symtab_sz * 18; | 
| 841 | 689 | 
| 842     if (nsections > 96) | 690     if (nsections > 96) | 
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 900         8           Value | 748         8           Value | 
| 901         12          SectionNumber | 749         12          SectionNumber | 
| 902         14          Type | 750         14          Type | 
| 903         16          StorageClass | 751         16          StorageClass | 
| 904         17          NumberOfAuxSymbols | 752         17          NumberOfAuxSymbols | 
| 905         */ | 753         */ | 
| 906     ptr = buf + symtab_ptr; | 754     ptr = buf + symtab_ptr; | 
| 907 | 755 | 
| 908     for (i = 0; i < symtab_sz; i++) | 756     for (i = 0; i < symtab_sz; i++) | 
| 909     { | 757     { | 
| 910         __int16 section = get_le16(ptr + 12); //section number | 758         int16_t section = get_le16(ptr + 12); //section number | 
| 911 | 759 | 
| 912         if (section > 0 && ptr[16] == 2) | 760         if (section > 0 && ptr[16] == 2) | 
| 913         { | 761         { | 
| 914             //if(section > 0 && ptr[16] == 3 && get_le32(ptr+8)) { | 762             //if(section > 0 && ptr[16] == 3 && get_le32(ptr+8)) { | 
| 915 | 763 | 
| 916             if (get_le32(ptr)) | 764             if (get_le32(ptr)) | 
| 917             { | 765             { | 
| 918                 char name[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; | 766                 char name[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; | 
| 919                 strncpy(name, ptr, 8); | 767                 strncpy(name, ptr, 8); | 
| 920                 //log_msg("COFF: Parsing symbol %s\n",name); | 768                 //log_msg("COFF: Parsing symbol %s\n",name); | 
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 971 | 819 | 
| 972     for (i = 0; i < nsections; i++) | 820     for (i = 0; i < nsections; i++) | 
| 973     { | 821     { | 
| 974         free(sectionlist[i]); | 822         free(sectionlist[i]); | 
| 975     } | 823     } | 
| 976 | 824 | 
| 977     free(sectionlist); | 825     free(sectionlist); | 
| 978 | 826 | 
| 979     return 1; | 827     return 1; | 
| 980 } | 828 } | 
|  | 829 #endif /* defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN__) */ | 
| 981 | 830 | 
| 982 int main(int argc, char **argv) | 831 int main(int argc, char **argv) | 
| 983 { | 832 { | 
| 984     int fd; | 833     output_fmt_t mode = OUTPUT_FMT_PLAIN; | 
| 985     output_fmt_t mode; |  | 
| 986     const char *f; | 834     const char *f; | 
| 987     struct _stat stat_buf; | 835     uint8_t *file_buf; | 
| 988     unsigned __int8 *file_buf; |  | 
| 989     int res; | 836     int res; | 
|  | 837     FILE *fp; | 
|  | 838     long int file_size; | 
| 990 | 839 | 
| 991     if (argc < 2 || argc > 3) | 840     if (argc < 2 || argc > 3) | 
| 992     { | 841     { | 
| 993         fprintf(stderr, "Usage: %s [output format] <obj file>\n\n", argv[0]); | 842         fprintf(stderr, "Usage: %s [output format] <obj file>\n\n", argv[0]); | 
| 994         fprintf(stderr, "  <obj file>\tELF format object file to parse\n"); | 843         fprintf(stderr, "  <obj file>\tobject file to parse\n"); | 
| 995         fprintf(stderr, "Output Formats:\n"); | 844         fprintf(stderr, "Output Formats:\n"); | 
| 996         fprintf(stderr, "  gas  - compatible with GNU assembler\n"); | 845         fprintf(stderr, "  gas  - compatible with GNU assembler\n"); | 
| 997         fprintf(stderr, "  rvds - compatible with armasm\n"); | 846         fprintf(stderr, "  rvds - compatible with armasm\n"); | 
| 998         goto bail; | 847         goto bail; | 
| 999     } | 848     } | 
| 1000 | 849 | 
| 1001     f = argv[2]; | 850     f = argv[2]; | 
| 1002 | 851 | 
| 1003     if (!strcmp(argv[1], "rvds")) | 852     if (!strcmp(argv[1], "rvds")) | 
| 1004         mode = OUTPUT_FMT_RVDS; | 853         mode = OUTPUT_FMT_RVDS; | 
| 1005     else if (!strcmp(argv[1], "gas")) | 854     else if (!strcmp(argv[1], "gas")) | 
| 1006         mode = OUTPUT_FMT_GAS; | 855         mode = OUTPUT_FMT_GAS; | 
| 1007     else | 856     else | 
| 1008         f = argv[1]; | 857         f = argv[1]; | 
| 1009 | 858 | 
| 1010     fd = _sopen(f, _O_BINARY, _SH_DENYNO, _S_IREAD | _S_IWRITE); | 859     fp = fopen(f, "rb"); | 
| 1011 | 860 | 
| 1012     if (_fstat(fd, &stat_buf)) | 861     if (!fp) | 
|  | 862     { | 
|  | 863         perror("Unable to open file"); | 
|  | 864         goto bail; | 
|  | 865     } | 
|  | 866 | 
|  | 867     if (fseek(fp, 0, SEEK_END)) | 
| 1013     { | 868     { | 
| 1014         perror("stat"); | 869         perror("stat"); | 
| 1015         goto bail; | 870         goto bail; | 
| 1016     } | 871     } | 
| 1017 | 872 | 
| 1018     file_buf = malloc(stat_buf.st_size); | 873     file_size = ftell(fp); | 
|  | 874     file_buf = malloc(file_size); | 
| 1019 | 875 | 
| 1020     if (!file_buf) | 876     if (!file_buf) | 
| 1021     { | 877     { | 
| 1022         perror("malloc"); | 878         perror("malloc"); | 
| 1023         goto bail; | 879         goto bail; | 
| 1024     } | 880     } | 
| 1025 | 881 | 
| 1026     if (_read(fd, file_buf, stat_buf.st_size) != stat_buf.st_size) | 882     rewind(fp); | 
|  | 883 | 
|  | 884     if (fread(file_buf, sizeof(char), file_size, fp) != file_size) | 
| 1027     { | 885     { | 
| 1028         perror("read"); | 886         perror("read"); | 
| 1029         goto bail; | 887         goto bail; | 
| 1030     } | 888     } | 
| 1031 | 889 | 
| 1032     if (_close(fd)) | 890     if (fclose(fp)) | 
| 1033     { | 891     { | 
| 1034         perror("close"); | 892         perror("close"); | 
| 1035         goto bail; | 893         goto bail; | 
| 1036     } | 894     } | 
| 1037 | 895 | 
| 1038     res = parse_coff(file_buf, stat_buf.st_size); | 896 #if defined(__GNUC__) && __GNUC__ | 
|  | 897 #if defined(__MACH__) | 
|  | 898     res = parse_macho(file_buf, file_size); | 
|  | 899 #elif defined(__ELF__) | 
|  | 900     res = parse_elf(file_buf, file_size, mode); | 
|  | 901 #endif | 
|  | 902 #endif | 
|  | 903 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN__) | 
|  | 904     res = parse_coff(file_buf, file_size); | 
|  | 905 #endif | 
| 1039 | 906 | 
| 1040     free(file_buf); | 907     free(file_buf); | 
| 1041 | 908 | 
| 1042     if (!res) | 909     if (!res) | 
| 1043         return EXIT_SUCCESS; | 910         return EXIT_SUCCESS; | 
| 1044 | 911 | 
| 1045 bail: | 912 bail: | 
| 1046     return EXIT_FAILURE; | 913     return EXIT_FAILURE; | 
| 1047 } | 914 } | 
| 1048 #endif |  | 
| OLD | NEW | 
|---|