Fully replace make-config.sh and its children
[sbcl/autoconf.git] / tools-for-build / grovel-object.c
blobb9ab44244cd274479955bdd2622a971e746f3a92
1 /*
2 Grovel a compiled object file for an integer constant.
4 Example source file:
5 #include <inttypes.h>
6 #include <signal.h>
7 uint64_t values[]={0x0123456789abcdefLL, FPE_INTDIV, 0xfedcba9876543210LL};
8 int dummy[]={5, 6, 7, 8};
9 void test() { char *s = (char *)values; s = (char *)dummy; }
12 #include <stdio.h>
13 #include <inttypes.h>
15 // search for a known pattern in the file
16 int match(char *buf, FILE *f)
18 int count=0;
19 char c;
20 while(fread(&c, 1, 1, f))
22 if(c==buf[count])
24 count++;
26 else
28 count=0;
30 if(count==8)
31 return 1;
33 return 0;
36 int main(int argc, char **argv)
38 if(argc!=3 || !(argv[1][0]=='0' || argv[1][0]=='1'))
40 printf("argc: %d\n", argc);
41 printf("argv[2]: %s\n", argv[2]);
42 // 0 = little-endian
43 // 1 = big-endian
44 printf("Usage: %s {0,1} <input-file>\n", argv[0]);
45 return 1;
48 FILE *f=fopen(argv[2], "r");
49 if(!f)
51 printf("Error: could not open '%s'", argv[1]);
52 return 2;
55 // constants can be up to 64 bits = 8 bytes long
56 // format: 0x01234567xxxxxxxx76543210
57 int count=0;
58 char buf;
59 // count up
60 uint64_t value=0;
61 if(argv[1][0]=='0')
63 // little endian
64 unsigned char prefix[]={0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0x01};
65 unsigned char suffix[]={0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe};
66 if(!match(prefix, f))
67 return 3;
68 unsigned char buffer[8];
69 if(fread(buffer, 8, 1, f)!=1)
70 return 4;
71 if(!match(suffix, f))
72 return 5;
74 // decode the value
75 int i;
76 for(i=0; i<8; i++)
78 value*=8;
79 value+=buffer[7-i];
82 else
84 // big endian
85 unsigned char prefix[]={0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
86 unsigned char suffix[]={0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10};
87 if(!match(prefix, f))
88 return 6;
89 unsigned char buffer[8];
90 if(fread(buffer, 8, 1, f)!=1)
91 return 7;
92 if(!match(suffix, f))
93 return 8;
95 // decode the value
96 int i;
97 for(i=0; i<8; i++)
99 value*=8;
100 value+=buffer[i];
104 if(fclose(f))
105 return 9;
107 printf("%llu\n", value);