1 #define _POSIX_C_SOURCE 200112L
12 #define RELOCATABLE 0x1
13 #define EXECUTABLE 0x2
18 int readline(FILE *f, char *fn);
19 int checkfile(char *fn, unsigned int want, int perr, int quiet);
24 int main(int ac, char **av) {
26 unsigned int want = 0;
32 while ((option = getopt(ac, av, "redclaqp0f:")) != -1) {
34 case 'r': want |= RELOCATABLE; break;
35 case 'e': want |= EXECUTABLE; break;
36 case 'd': want |= DYNAMIC; break;
37 case 'c': want |= CORE; break;
38 case 'l': want |= SYMLINKS; break;
39 case 'a': want = (RELOCATABLE|EXECUTABLE|DYNAMIC|CORE); break;
40 case 'q': quiet++; break;
41 case 'p': perr++; break;
42 case '0': zeroterm = 1; break;
44 if (optarg[0] == '-' && optarg[1] == 0) {
47 list = fopen(optarg, "r");
58 while (readline(list, fn)) {
59 fprintf(stderr, "checking %s\n", fn);
60 if (checkfile(fn, want,perr,quiet)) {
65 fprintf(stderr, "checking %s\n", av[optind]);
66 return !checkfile(av[optind], want,perr,quiet);
68 return errors ? 1 : 0;
71 int readline(FILE *f, char *fn) {
92 /* TODO print an error */
101 int checkfile(char *fn, unsigned int want, int perr, int quiet) {
107 if (want & SYMLINKS) {
112 if (!S_ISREG(st.st_mode)) {
116 fd = open(fn, O_RDONLY);
122 bytes = read(fd, &hdr, sizeof hdr);
123 if (bytes < sizeof hdr) {
124 if (perr) fprintf(stderr, "could not read full elf header (wanted %zu got %d bytes)\n", sizeof hdr, bytes);
128 if (hdr.e_ident[EI_MAG0] != ELFMAG0
129 || hdr.e_ident[EI_MAG1] != ELFMAG1
130 || hdr.e_ident[EI_MAG2] != ELFMAG2
131 || hdr.e_ident[EI_MAG3] != ELFMAG3
133 if (perr) fprintf(stderr, "elf header magic wrong\n");
137 switch (hdr.e_ident[EI_CLASS]) {
143 if (perr) fprintf(stderr, "elf header unknown class\n");
148 endian = hdr.e_ident[EI_DATA];
150 case ELFDATA2LSB: break;
151 case ELFDATA2MSB: break;
152 /* TODO swap endian if needed */
154 if (perr) fprintf(stderr, "elf header unknown endian\n");
159 switch (hdr.e_type) {
161 if (!quiet) printf("relocatable\n");
163 return want & RELOCATABLE ? 1 : 0;
165 if (!quiet) printf("executable\n");
167 return want & EXECUTABLE ? 1 : 0;
169 if (!quiet) printf("dynamic\n");
171 return want & DYNAMIC ? 1 : 0;
173 if (!quiet) printf("core\n");
175 return want & CORE ? 1 : 0;
178 if (perr) fprintf(stderr, "unknown type\n");