1 |
solar |
1.1 |
/* |
2 |
|
|
* Copyright 2003 Ned Ludd <solar@gentoo.org> |
3 |
vapier |
1.8 |
* Copyright 1999-2005 Gentoo Foundation |
4 |
solar |
1.1 |
* Distributed under the terms of the GNU General Public License v2 |
5 |
solar |
1.30 |
* $Header: /var/cvsroot/gentoo-projects/pax-utils/scanelf.c,v 1.29 2005/04/05 04:25:54 vapier Exp $ |
6 |
solar |
1.1 |
* |
7 |
|
|
******************************************************************** |
8 |
|
|
* This program is free software; you can redistribute it and/or |
9 |
|
|
* modify it under the terms of the GNU General Public License as |
10 |
|
|
* published by the Free Software Foundation; either version 2 of the |
11 |
|
|
* License, or (at your option) any later version. |
12 |
|
|
* |
13 |
|
|
* This program is distributed in the hope that it will be useful, but |
14 |
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of |
15 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 |
|
|
* General Public License for more details. |
17 |
|
|
* |
18 |
|
|
* You should have received a copy of the GNU General Public License |
19 |
|
|
* along with this program; if not, write to the Free Software |
20 |
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
21 |
|
|
* MA 02111-1307, USA. |
22 |
|
|
*/ |
23 |
|
|
|
24 |
|
|
#include <stdio.h> |
25 |
|
|
#include <stdlib.h> |
26 |
|
|
#include <sys/types.h> |
27 |
solar |
1.28 |
#define __USE_GNU |
28 |
solar |
1.1 |
#include <string.h> |
29 |
vapier |
1.10 |
#include <errno.h> |
30 |
solar |
1.1 |
#include <unistd.h> |
31 |
|
|
#include <sys/stat.h> |
32 |
|
|
#include <dirent.h> |
33 |
|
|
#include <getopt.h> |
34 |
solar |
1.20 |
#include <assert.h> |
35 |
solar |
1.21 |
|
36 |
solar |
1.1 |
#include "paxelf.h" |
37 |
|
|
|
38 |
solar |
1.30 |
static const char *rcsid = "$Id: scanelf.c,v 1.29 2005/04/05 04:25:54 vapier Exp $"; |
39 |
vapier |
1.10 |
|
40 |
|
|
|
41 |
|
|
/* helper functions for showing errors */ |
42 |
vapier |
1.18 |
#define argv0 "scanelf" /*((*argv != NULL) ? argv[0] : __FILE__ "\b\b")*/ |
43 |
vapier |
1.10 |
#define warn(fmt, args...) \ |
44 |
|
|
fprintf(stderr, "%s: " fmt "\n", argv0, ## args) |
45 |
|
|
#define warnf(fmt, args...) warn("%s(): " fmt, __FUNCTION__, ## args) |
46 |
|
|
#define err(fmt, args...) \ |
47 |
|
|
do { \ |
48 |
|
|
warn(fmt, ## args); \ |
49 |
|
|
exit(EXIT_FAILURE); \ |
50 |
|
|
} while (0) |
51 |
|
|
|
52 |
|
|
|
53 |
|
|
|
54 |
|
|
/* prototypes */ |
55 |
|
|
static void scanelf_file(const char *filename); |
56 |
|
|
static void scanelf_dir(const char *path); |
57 |
|
|
static void scanelf_ldpath(); |
58 |
|
|
static void scanelf_envpath(); |
59 |
|
|
static void usage(int status); |
60 |
|
|
static void parseargs(int argc, char *argv[]); |
61 |
|
|
|
62 |
|
|
/* variables to control behavior */ |
63 |
|
|
static char scan_ldpath = 0; |
64 |
|
|
static char scan_envpath = 0; |
65 |
|
|
static char dir_recurse = 0; |
66 |
vapier |
1.14 |
static char dir_crossmount = 1; |
67 |
vapier |
1.10 |
static char show_pax = 0; |
68 |
|
|
static char show_stack = 0; |
69 |
|
|
static char show_textrel = 0; |
70 |
|
|
static char show_rpath = 0; |
71 |
solar |
1.16 |
static char show_banner = 1; |
72 |
vapier |
1.10 |
static char be_quiet = 0; |
73 |
vapier |
1.14 |
static char be_verbose = 0; |
74 |
vapier |
1.27 |
static char *find_sym = NULL; |
75 |
vapier |
1.10 |
|
76 |
solar |
1.1 |
|
77 |
|
|
|
78 |
vapier |
1.10 |
/* scan an elf file and show all the fun stuff */ |
79 |
|
|
static void scanelf_file(const char *filename) |
80 |
solar |
1.6 |
{ |
81 |
vapier |
1.10 |
int i; |
82 |
vapier |
1.27 |
char found_pax, found_stack, found_relro, found_textrel, found_rpath, found_sym; |
83 |
vapier |
1.26 |
elfobj *elf; |
84 |
vapier |
1.10 |
|
85 |
vapier |
1.27 |
found_pax = found_stack = found_relro = found_textrel = found_rpath = found_sym = 0; |
86 |
solar |
1.12 |
|
87 |
vapier |
1.10 |
/* verify this is real ELF */ |
88 |
vapier |
1.14 |
if ((elf = readelf(filename)) == NULL) { |
89 |
vapier |
1.26 |
if (be_verbose > 2) printf("%s: not an ELF\n", filename); |
90 |
vapier |
1.10 |
return; |
91 |
vapier |
1.14 |
} |
92 |
|
|
|
93 |
vapier |
1.26 |
if (be_verbose > 1) |
94 |
|
|
printf("%s: {%s,%s} scanning file\n", filename, |
95 |
|
|
get_elfeitype(elf, EI_CLASS, elf->elf_class), |
96 |
|
|
get_elfeitype(elf, EI_DATA, elf->data[EI_DATA])); |
97 |
|
|
else if (be_verbose) |
98 |
|
|
printf("%s: scanning file\n", filename); |
99 |
vapier |
1.10 |
|
100 |
|
|
/* show the header */ |
101 |
solar |
1.16 |
if (!be_quiet && show_banner) { |
102 |
vapier |
1.23 |
printf(" TYPE "); |
103 |
|
|
if (show_pax) printf(" PAX "); |
104 |
|
|
if (show_stack) printf(" STK/REL "); |
105 |
|
|
if (show_textrel) printf("TEXTREL "); |
106 |
|
|
if (show_rpath) printf("RPATH "); |
107 |
|
|
printf(" FILE\n"); |
108 |
solar |
1.16 |
show_banner = 0; |
109 |
vapier |
1.10 |
} |
110 |
|
|
|
111 |
|
|
/* dump all the good stuff */ |
112 |
|
|
if (!be_quiet) |
113 |
vapier |
1.26 |
printf("%-7s ", get_elfetype(elf)); |
114 |
vapier |
1.10 |
|
115 |
vapier |
1.14 |
if (show_pax) { |
116 |
|
|
char *paxflags = pax_short_hf_flags(PAX_FLAGS(elf)); |
117 |
|
|
if (!be_quiet || (be_quiet && strncmp(paxflags, "PeMRxS", 6))) { |
118 |
|
|
found_pax = 1; |
119 |
|
|
printf("%s ", pax_short_hf_flags(PAX_FLAGS(elf))); |
120 |
|
|
} |
121 |
|
|
} |
122 |
vapier |
1.10 |
|
123 |
|
|
/* stack fun */ |
124 |
|
|
if (show_stack) { |
125 |
vapier |
1.26 |
#define SHOW_STACK(B) \ |
126 |
|
|
if (elf->elf_class == ELFCLASS ## B) { \ |
127 |
|
|
Elf ## B ## _Ehdr *ehdr = EHDR ## B (elf->ehdr); \ |
128 |
|
|
Elf ## B ## _Phdr *phdr = PHDR ## B (elf->phdr); \ |
129 |
|
|
for (i = 0; i < EGET(ehdr->e_phnum); i++) { \ |
130 |
|
|
if (EGET(phdr[i].p_type) != PT_GNU_STACK && \ |
131 |
|
|
EGET(phdr[i].p_type) != PT_GNU_RELRO) continue; \ |
132 |
|
|
if (be_quiet && !(EGET(phdr[i].p_flags) & PF_X)) \ |
133 |
|
|
continue; \ |
134 |
|
|
if (EGET(phdr[i].p_type) == PT_GNU_STACK) \ |
135 |
|
|
found_stack = 1; \ |
136 |
|
|
if (EGET(phdr[i].p_type) == PT_GNU_RELRO) \ |
137 |
|
|
found_relro = 1; \ |
138 |
|
|
printf("%s ", gnu_short_stack_flags(EGET(phdr[i].p_flags))); \ |
139 |
|
|
} \ |
140 |
vapier |
1.10 |
} |
141 |
vapier |
1.26 |
SHOW_STACK(32) |
142 |
|
|
SHOW_STACK(64) |
143 |
vapier |
1.23 |
if (!be_quiet && !found_stack) printf("--- "); |
144 |
|
|
if (!be_quiet && !found_relro) printf("--- "); |
145 |
vapier |
1.10 |
} |
146 |
|
|
|
147 |
|
|
/* textrel fun */ |
148 |
|
|
if (show_textrel) { |
149 |
vapier |
1.26 |
#define SHOW_TEXTREL(B) \ |
150 |
|
|
if (elf->elf_class == ELFCLASS ## B) { \ |
151 |
|
|
Elf ## B ## _Dyn *dyn; \ |
152 |
|
|
Elf ## B ## _Ehdr *ehdr = EHDR ## B (elf->ehdr); \ |
153 |
|
|
Elf ## B ## _Phdr *phdr = PHDR ## B (elf->phdr); \ |
154 |
|
|
for (i = 0; i < EGET(ehdr->e_phnum); i++) { \ |
155 |
|
|
if (phdr[i].p_type != PT_DYNAMIC) continue; \ |
156 |
|
|
dyn = DYN ## B (elf->data + EGET(phdr[i].p_offset)); \ |
157 |
|
|
while (EGET(dyn->d_tag) != DT_NULL) { \ |
158 |
|
|
if (EGET(dyn->d_tag) == DT_TEXTREL) { /*dyn->d_tag != DT_FLAGS)*/ \ |
159 |
|
|
found_textrel = 1; \ |
160 |
|
|
/*if (dyn->d_un.d_val & DF_TEXTREL)*/ \ |
161 |
|
|
printf("TEXTREL "); \ |
162 |
|
|
} \ |
163 |
|
|
++dyn; \ |
164 |
|
|
} \ |
165 |
|
|
} } |
166 |
|
|
SHOW_TEXTREL(32) |
167 |
|
|
SHOW_TEXTREL(64) |
168 |
vapier |
1.23 |
if (!be_quiet && !found_textrel) printf("------- "); |
169 |
vapier |
1.10 |
} |
170 |
|
|
|
171 |
|
|
/* rpath fun */ |
172 |
|
|
/* TODO: if be_quiet, only output RPATH's which aren't in /etc/ld.so.conf */ |
173 |
|
|
if (show_rpath) { |
174 |
vapier |
1.23 |
char *rpath, *runpath; |
175 |
vapier |
1.26 |
void *strtbl_void = elf_findsecbyname(elf, ".dynstr"); |
176 |
vapier |
1.23 |
rpath = runpath = NULL; |
177 |
vapier |
1.10 |
|
178 |
vapier |
1.26 |
if (strtbl_void) { |
179 |
|
|
#define SHOW_RPATH(B) \ |
180 |
|
|
if (elf->elf_class == ELFCLASS ## B) { \ |
181 |
|
|
Elf ## B ## _Dyn *dyn; \ |
182 |
|
|
Elf ## B ## _Ehdr *ehdr = EHDR ## B (elf->ehdr); \ |
183 |
|
|
Elf ## B ## _Phdr *phdr = PHDR ## B (elf->phdr); \ |
184 |
|
|
Elf ## B ## _Shdr *strtbl = SHDR ## B (strtbl_void); \ |
185 |
|
|
for (i = 0; i < EGET(ehdr->e_phnum); i++) { \ |
186 |
|
|
if (EGET(phdr[i].p_type) != PT_DYNAMIC) continue; \ |
187 |
|
|
dyn = DYN ## B (elf->data + EGET(phdr[i].p_offset)); \ |
188 |
|
|
while (EGET(dyn->d_tag) != DT_NULL) { \ |
189 |
|
|
if (EGET(dyn->d_tag) == DT_RPATH) { \ |
190 |
|
|
rpath = elf->data + EGET(strtbl->sh_offset) + EGET(dyn->d_un.d_ptr); \ |
191 |
|
|
found_rpath = 1; \ |
192 |
|
|
} else if (EGET(dyn->d_tag) == DT_RUNPATH) { \ |
193 |
|
|
runpath = elf->data + EGET(strtbl->sh_offset) + EGET(dyn->d_un.d_ptr); \ |
194 |
|
|
found_rpath = 1; \ |
195 |
|
|
} \ |
196 |
|
|
++dyn; \ |
197 |
|
|
} \ |
198 |
|
|
} } |
199 |
|
|
SHOW_RPATH(32) |
200 |
|
|
SHOW_RPATH(64) |
201 |
vapier |
1.10 |
} |
202 |
vapier |
1.23 |
if (rpath && runpath) { |
203 |
|
|
if (!strcmp(rpath, runpath)) |
204 |
|
|
printf("%-5s ", runpath); |
205 |
|
|
else { |
206 |
|
|
fprintf(stderr, "%s's RPATH [%s] != RUNPATH [%s]\n", filename, rpath, runpath); |
207 |
|
|
printf("{%s,%s} ", rpath, runpath); |
208 |
|
|
} |
209 |
|
|
} else if (rpath || runpath) |
210 |
|
|
printf("%-5s ", (runpath ? runpath : rpath)); |
211 |
|
|
else if (!be_quiet && !found_rpath) |
212 |
|
|
printf(" - "); |
213 |
vapier |
1.10 |
} |
214 |
|
|
|
215 |
vapier |
1.27 |
if (find_sym) { |
216 |
|
|
void *symtab_void, *strtab_void; |
217 |
vapier |
1.29 |
char *versioned_symname = malloc(strlen(find_sym)+2); |
218 |
solar |
1.28 |
|
219 |
|
|
sprintf(versioned_symname, "%s@", find_sym); |
220 |
vapier |
1.27 |
symtab_void = elf_findsecbyname(elf, ".symtab"); |
221 |
|
|
strtab_void = elf_findsecbyname(elf, ".strtab"); |
222 |
|
|
|
223 |
|
|
if (symtab_void && strtab_void) { |
224 |
|
|
#define FIND_SYM(B) \ |
225 |
|
|
if (elf->elf_class == ELFCLASS ## B) { \ |
226 |
|
|
Elf ## B ## _Shdr *symtab = SHDR ## B (symtab_void); \ |
227 |
|
|
Elf ## B ## _Shdr *strtab = SHDR ## B (strtab_void); \ |
228 |
|
|
Elf ## B ## _Sym *sym = SYM ## B (elf->data + EGET(symtab->sh_offset)); \ |
229 |
|
|
int cnt = EGET(symtab->sh_size) / EGET(symtab->sh_entsize); \ |
230 |
|
|
char *symname; \ |
231 |
|
|
for (i = 0; i < cnt; ++i) { \ |
232 |
|
|
if (sym->st_name) { \ |
233 |
|
|
symname = (char *)(elf->data + EGET(strtab->sh_offset) + EGET(sym->st_name)); \ |
234 |
solar |
1.28 |
if (*find_sym == '*') { \ |
235 |
|
|
printf("%s(%s) %5lX %15s %s\n", ((found_sym == 0) ? "\n\t" : "\t"), \ |
236 |
|
|
(char *) basename(filename), \ |
237 |
|
|
(long)sym->st_size, (char *) get_elfstttype(sym->st_info & 0xF), \ |
238 |
|
|
symname); \ |
239 |
|
|
found_sym = 1; \ |
240 |
|
|
} \ |
241 |
|
|
if ((strcmp(find_sym, symname) == 0) || \ |
242 |
|
|
(strncmp(symname, versioned_symname, strlen(versioned_symname)) == 0)) \ |
243 |
|
|
found_sym++; \ |
244 |
vapier |
1.27 |
} \ |
245 |
|
|
++sym; \ |
246 |
|
|
} } |
247 |
|
|
FIND_SYM(32) |
248 |
|
|
FIND_SYM(64) |
249 |
|
|
} |
250 |
solar |
1.28 |
free(versioned_symname); |
251 |
vapier |
1.29 |
if (*find_sym != '*') { |
252 |
|
|
if (found_sym) |
253 |
|
|
printf(" %s ", find_sym); |
254 |
|
|
else if (!be_quiet) |
255 |
|
|
printf(" - "); |
256 |
|
|
} |
257 |
vapier |
1.27 |
} |
258 |
vapier |
1.10 |
|
259 |
vapier |
1.29 |
if (!be_quiet || found_pax || found_stack || found_textrel || found_rpath || found_sym) |
260 |
solar |
1.28 |
printf("%s\n", filename); |
261 |
|
|
|
262 |
vapier |
1.10 |
unreadelf(elf); |
263 |
solar |
1.6 |
} |
264 |
|
|
|
265 |
solar |
1.1 |
/* scan a directory for ET_EXEC files and print when we find one */ |
266 |
vapier |
1.10 |
static void scanelf_dir(const char *path) |
267 |
solar |
1.1 |
{ |
268 |
vapier |
1.10 |
register DIR *dir; |
269 |
|
|
register struct dirent *dentry; |
270 |
vapier |
1.14 |
struct stat st_top, st; |
271 |
solar |
1.21 |
char buf[_POSIX_PATH_MAX]; |
272 |
solar |
1.20 |
size_t len = 0; |
273 |
vapier |
1.10 |
|
274 |
|
|
/* make sure path exists */ |
275 |
vapier |
1.14 |
if (lstat(path, &st_top) == -1) |
276 |
vapier |
1.10 |
return; |
277 |
solar |
1.11 |
|
278 |
vapier |
1.10 |
/* ok, if it isn't a directory, assume we can open it */ |
279 |
vapier |
1.14 |
if (!S_ISDIR(st_top.st_mode)) { |
280 |
vapier |
1.10 |
scanelf_file(path); |
281 |
|
|
return; |
282 |
|
|
} |
283 |
|
|
|
284 |
|
|
/* now scan the dir looking for fun stuff */ |
285 |
|
|
if ((dir = opendir(path)) == NULL) { |
286 |
|
|
warnf("could not opendir %s: %s", path, strerror(errno)); |
287 |
|
|
return; |
288 |
|
|
} |
289 |
vapier |
1.15 |
if (be_verbose) printf("%s: scanning dir\n", path); |
290 |
solar |
1.11 |
|
291 |
vapier |
1.10 |
while ((dentry = readdir(dir))) { |
292 |
|
|
if (!strcmp(dentry->d_name, ".") || !strcmp(dentry->d_name, "..")) |
293 |
|
|
continue; |
294 |
|
|
len = (strlen(path) + 2 + strlen(dentry->d_name)); |
295 |
solar |
1.20 |
assert(len < sizeof(buf)); |
296 |
|
|
strncpy(buf, path, len); |
297 |
|
|
strncat(buf, "/", len); |
298 |
|
|
strncat(buf, dentry->d_name, len); |
299 |
|
|
buf[sizeof(buf)] = 0; |
300 |
|
|
if (lstat(buf, &st) != -1) { |
301 |
vapier |
1.10 |
if (S_ISREG(st.st_mode)) |
302 |
solar |
1.20 |
scanelf_file(buf); |
303 |
vapier |
1.10 |
else if (dir_recurse && S_ISDIR(st.st_mode)) { |
304 |
vapier |
1.14 |
if (dir_crossmount || (st_top.st_dev == st.st_dev)) |
305 |
solar |
1.20 |
scanelf_dir(buf); |
306 |
vapier |
1.10 |
} |
307 |
|
|
} |
308 |
|
|
} |
309 |
|
|
closedir(dir); |
310 |
solar |
1.1 |
} |
311 |
|
|
|
312 |
vapier |
1.10 |
/* scan /etc/ld.so.conf for paths */ |
313 |
|
|
static void scanelf_ldpath() |
314 |
|
|
{ |
315 |
vapier |
1.17 |
char scan_l, scan_ul, scan_ull; |
316 |
vapier |
1.10 |
char *path, *p; |
317 |
|
|
FILE *fp; |
318 |
|
|
|
319 |
|
|
if ((fp = fopen("/etc/ld.so.conf", "r")) == NULL) |
320 |
|
|
err("Unable to open ld.so.conf: %s", strerror(errno)); |
321 |
|
|
|
322 |
vapier |
1.17 |
scan_l = scan_ul = scan_ull = 0; |
323 |
|
|
|
324 |
solar |
1.30 |
if ((path = malloc(_POSIX_PATH_MAX)) == NULL) { |
325 |
|
|
warn("Can not malloc() memory for ldpath scanning"); |
326 |
|
|
return; |
327 |
|
|
} |
328 |
vapier |
1.10 |
while ((fgets(path, _POSIX_PATH_MAX, fp)) != NULL) |
329 |
|
|
if (*path == '/') { |
330 |
|
|
if ((p = strrchr(path, '\r')) != NULL) |
331 |
|
|
*p = 0; |
332 |
|
|
if ((p = strrchr(path, '\n')) != NULL) |
333 |
|
|
*p = 0; |
334 |
vapier |
1.17 |
if (!scan_l && !strcmp(path, "/lib")) scan_l = 1; |
335 |
|
|
if (!scan_ul && !strcmp(path, "/usr/lib")) scan_ul = 1; |
336 |
|
|
if (!scan_ull && !strcmp(path, "/usr/local/lib")) scan_ull = 1; |
337 |
vapier |
1.10 |
scanelf_dir(path); |
338 |
|
|
} |
339 |
|
|
free(path); |
340 |
|
|
|
341 |
vapier |
1.17 |
if (!scan_l) scanelf_dir("/lib"); |
342 |
|
|
if (!scan_ul) scanelf_dir("/usr/lib"); |
343 |
|
|
if (!scan_ull) scanelf_dir("/usr/local/lib"); |
344 |
|
|
|
345 |
vapier |
1.10 |
fclose(fp); |
346 |
|
|
} |
347 |
solar |
1.1 |
|
348 |
vapier |
1.10 |
/* scan env PATH for paths */ |
349 |
|
|
static void scanelf_envpath() |
350 |
solar |
1.1 |
{ |
351 |
vapier |
1.10 |
char *path, *p; |
352 |
|
|
|
353 |
|
|
path = getenv("PATH"); |
354 |
|
|
if (!path) |
355 |
|
|
err("PATH is not set in your env !"); |
356 |
|
|
|
357 |
|
|
if ((path = strdup(path)) == NULL) |
358 |
|
|
err("stdup failed: %s", strerror(errno)); |
359 |
|
|
|
360 |
|
|
while ((p = strrchr(path, ':')) != NULL) { |
361 |
|
|
scanelf_dir(p + 1); |
362 |
|
|
*p = 0; |
363 |
|
|
} |
364 |
vapier |
1.17 |
|
365 |
vapier |
1.10 |
free(path); |
366 |
solar |
1.1 |
} |
367 |
|
|
|
368 |
|
|
|
369 |
vapier |
1.10 |
|
370 |
|
|
/* usage / invocation handling functions */ |
371 |
vapier |
1.27 |
#define PARSE_FLAGS "plRmxetrs:aqvo:BhV" |
372 |
|
|
#define a_argument required_argument |
373 |
vapier |
1.10 |
static struct option const long_opts[] = { |
374 |
|
|
{"path", no_argument, NULL, 'p'}, |
375 |
|
|
{"ldpath", no_argument, NULL, 'l'}, |
376 |
|
|
{"recursive", no_argument, NULL, 'R'}, |
377 |
vapier |
1.14 |
{"mount", no_argument, NULL, 'm'}, |
378 |
vapier |
1.10 |
{"pax", no_argument, NULL, 'x'}, |
379 |
solar |
1.16 |
{"header", no_argument, NULL, 'e'}, |
380 |
vapier |
1.10 |
{"textrel", no_argument, NULL, 't'}, |
381 |
|
|
{"rpath", no_argument, NULL, 'r'}, |
382 |
vapier |
1.27 |
{"symbol", a_argument, NULL, 's'}, |
383 |
vapier |
1.10 |
{"all", no_argument, NULL, 'a'}, |
384 |
|
|
{"quiet", no_argument, NULL, 'q'}, |
385 |
vapier |
1.14 |
{"verbose", no_argument, NULL, 'v'}, |
386 |
vapier |
1.27 |
{"file", a_argument, NULL, 'o'}, |
387 |
solar |
1.16 |
{"nobanner", no_argument, NULL, 'B'}, |
388 |
vapier |
1.10 |
{"help", no_argument, NULL, 'h'}, |
389 |
|
|
{"version", no_argument, NULL, 'V'}, |
390 |
|
|
{NULL, no_argument, NULL, 0x0} |
391 |
|
|
}; |
392 |
|
|
static char *opts_help[] = { |
393 |
|
|
"Scan all directories in PATH environment", |
394 |
|
|
"Scan all directories in /etc/ld.so.conf", |
395 |
vapier |
1.14 |
"Scan directories recursively", |
396 |
|
|
"Don't recursively cross mount points\n", |
397 |
vapier |
1.10 |
"Print PaX markings", |
398 |
|
|
"Print GNU_STACK markings", |
399 |
|
|
"Print TEXTREL information", |
400 |
|
|
"Print RPATH information", |
401 |
vapier |
1.27 |
"Find a specified symbol", |
402 |
solar |
1.16 |
"Print all scanned info (-x -e -t -r)\n", |
403 |
vapier |
1.14 |
"Only output 'bad' things", |
404 |
|
|
"Be verbose (can be specified more than once)", |
405 |
vapier |
1.24 |
"Write output stream to a filename", |
406 |
vapier |
1.14 |
"Don't display the header", |
407 |
vapier |
1.10 |
"Print this help and exit", |
408 |
|
|
"Print version and exit", |
409 |
|
|
NULL |
410 |
|
|
}; |
411 |
|
|
|
412 |
|
|
/* display usage and exit */ |
413 |
|
|
static void usage(int status) |
414 |
solar |
1.1 |
{ |
415 |
vapier |
1.10 |
int i; |
416 |
vapier |
1.27 |
printf("¤ Scan ELF binaries for stuff\n" |
417 |
vapier |
1.10 |
"Usage: %s [options] <dir1> [dir2 dirN ...]\n\n", argv0); |
418 |
vapier |
1.23 |
printf("Options:\n"); |
419 |
vapier |
1.10 |
for (i = 0; long_opts[i].name; ++i) |
420 |
vapier |
1.27 |
if (long_opts[i].has_arg == no_argument) |
421 |
|
|
printf(" -%c, --%-13s× %s\n", long_opts[i].val, |
422 |
|
|
long_opts[i].name, opts_help[i]); |
423 |
|
|
else |
424 |
|
|
printf(" -%c, --%-6s <arg> × %s\n", long_opts[i].val, |
425 |
|
|
long_opts[i].name, opts_help[i]); |
426 |
vapier |
1.10 |
exit(status); |
427 |
solar |
1.1 |
} |
428 |
|
|
|
429 |
|
|
/* parse command line arguments and preform needed actions */ |
430 |
vapier |
1.10 |
static void parseargs(int argc, char *argv[]) |
431 |
|
|
{ |
432 |
|
|
int flag; |
433 |
|
|
|
434 |
|
|
opterr = 0; |
435 |
|
|
while ((flag=getopt_long(argc, argv, PARSE_FLAGS, long_opts, NULL)) != -1) { |
436 |
|
|
switch (flag) { |
437 |
|
|
|
438 |
|
|
case 'V': /* version info */ |
439 |
solar |
1.19 |
printf("%s compiled %s\n%s\n" |
440 |
|
|
"%s written for Gentoo Linux by <solar and vapier @ gentoo.org>\n", |
441 |
|
|
__FILE__, __DATE__, rcsid, argv0); |
442 |
vapier |
1.10 |
exit(EXIT_SUCCESS); |
443 |
|
|
break; |
444 |
|
|
case 'h': usage(EXIT_SUCCESS); break; |
445 |
|
|
|
446 |
vapier |
1.24 |
case 'o': { |
447 |
solar |
1.21 |
FILE *fp = NULL; |
448 |
|
|
fp = freopen(optarg, "w", stdout); |
449 |
vapier |
1.24 |
if (fp == NULL) |
450 |
|
|
err("Could not open output stream '%s': %s", optarg, strerror(errno)); |
451 |
|
|
stdout = fp; |
452 |
solar |
1.21 |
break; |
453 |
|
|
} |
454 |
vapier |
1.24 |
|
455 |
vapier |
1.27 |
case 's': find_sym = strdup(optarg); break; |
456 |
|
|
|
457 |
solar |
1.16 |
case 'B': show_banner = 0; break; |
458 |
vapier |
1.10 |
case 'l': scan_ldpath = 1; break; |
459 |
|
|
case 'p': scan_envpath = 1; break; |
460 |
|
|
case 'R': dir_recurse = 1; break; |
461 |
vapier |
1.14 |
case 'm': dir_crossmount = 0; break; |
462 |
vapier |
1.10 |
case 'x': show_pax = 1; break; |
463 |
solar |
1.16 |
case 'e': show_stack = 1; break; |
464 |
vapier |
1.10 |
case 't': show_textrel = 1; break; |
465 |
|
|
case 'r': show_rpath = 1; break; |
466 |
|
|
case 'q': be_quiet = 1; break; |
467 |
vapier |
1.14 |
case 'v': be_verbose = (be_verbose % 20) + 1; break; |
468 |
vapier |
1.10 |
case 'a': show_pax = show_stack = show_textrel = show_rpath = 1; break; |
469 |
|
|
|
470 |
|
|
case ':': |
471 |
|
|
warn("Option missing parameter"); |
472 |
|
|
usage(EXIT_FAILURE); |
473 |
|
|
break; |
474 |
|
|
case '?': |
475 |
|
|
warn("Unknown option"); |
476 |
|
|
usage(EXIT_FAILURE); |
477 |
|
|
break; |
478 |
|
|
default: |
479 |
|
|
err("Unhandled option '%c'", flag); |
480 |
|
|
break; |
481 |
|
|
} |
482 |
|
|
} |
483 |
|
|
|
484 |
vapier |
1.14 |
if (be_quiet && be_verbose) |
485 |
|
|
err("You can be quiet or you can be verbose, not both, stupid"); |
486 |
|
|
|
487 |
vapier |
1.10 |
if (scan_ldpath) scanelf_ldpath(); |
488 |
|
|
if (scan_envpath) scanelf_envpath(); |
489 |
vapier |
1.25 |
if (optind == argc && !scan_ldpath && !scan_envpath) |
490 |
|
|
err("Nothing to scan !?"); |
491 |
vapier |
1.10 |
while (optind < argc) |
492 |
|
|
scanelf_dir(argv[optind++]); |
493 |
vapier |
1.27 |
|
494 |
|
|
if (find_sym) free(find_sym); |
495 |
vapier |
1.10 |
} |
496 |
|
|
|
497 |
|
|
|
498 |
|
|
|
499 |
|
|
int main(int argc, char *argv[]) |
500 |
solar |
1.1 |
{ |
501 |
vapier |
1.10 |
if (argc < 2) |
502 |
|
|
usage(EXIT_FAILURE); |
503 |
|
|
parseargs(argc, argv); |
504 |
solar |
1.21 |
fclose(stdout); |
505 |
vapier |
1.10 |
return EXIT_SUCCESS; |
506 |
solar |
1.1 |
} |