| 1 | /* |
1 | /* |
| 2 | * Copyright 2003 Ned Ludd <solar@gentoo.org> |
2 | * Copyright 2003 Ned Ludd <solar@gentoo.org> |
| 3 | * Copyright 1999-2005 Gentoo Foundation |
3 | * Copyright 1999-2005 Gentoo Foundation |
| 4 | * Distributed under the terms of the GNU General Public License v2 |
4 | * Distributed under the terms of the GNU General Public License v2 |
| 5 | * $Header: /var/cvsroot/gentoo-projects/pax-utils/scanelf.c,v 1.56 2005/05/21 00:34:07 vapier Exp $ |
5 | * $Header: /var/cvsroot/gentoo-projects/pax-utils/scanelf.c,v 1.57 2005/05/21 17:58:30 solar Exp $ |
| 6 | * |
6 | * |
| 7 | ******************************************************************** |
7 | ******************************************************************** |
| 8 | * This program is free software; you can redistribute it and/or |
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 |
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 |
10 | * published by the Free Software Foundation; either version 2 of the |
| … | |
… | |
| 33 | #include <getopt.h> |
33 | #include <getopt.h> |
| 34 | #include <assert.h> |
34 | #include <assert.h> |
| 35 | |
35 | |
| 36 | #include "paxelf.h" |
36 | #include "paxelf.h" |
| 37 | |
37 | |
| 38 | static const char *rcsid = "$Id: scanelf.c,v 1.56 2005/05/21 00:34:07 vapier Exp $"; |
38 | static const char *rcsid = "$Id: scanelf.c,v 1.57 2005/05/21 17:58:30 solar Exp $"; |
| 39 | #define argv0 "scanelf" |
39 | #define argv0 "scanelf" |
| 40 | |
40 | |
| 41 | |
41 | |
| 42 | |
42 | |
| 43 | /* prototypes */ |
43 | /* prototypes */ |
| … | |
… | |
| 89 | |
89 | |
| 90 | return NULL; |
90 | return NULL; |
| 91 | } |
91 | } |
| 92 | static char *scanelf_file_stack(elfobj *elf, char *found_stack, char *found_relro) |
92 | static char *scanelf_file_stack(elfobj *elf, char *found_stack, char *found_relro) |
| 93 | { |
93 | { |
| 94 | static char ret[8]; |
94 | static char ret[8] = "--- ---"; |
| 95 | char *found; |
95 | char *found; |
| 96 | unsigned long i, off, shown; |
96 | unsigned long i, off, shown; |
| 97 | |
97 | |
| 98 | if (!show_stack) return NULL; |
98 | if (!show_stack) return NULL; |
| 99 | |
99 | |
| 100 | shown = 0; |
100 | shown = 0; |
| 101 | strcpy(ret, "--- ---"); |
|
|
| 102 | |
101 | |
| 103 | if (elf->phdr) { |
102 | if (elf->phdr) { |
| 104 | #define SHOW_STACK(B) \ |
103 | #define SHOW_STACK(B) \ |
| 105 | if (elf->elf_class == ELFCLASS ## B) { \ |
104 | if (elf->elf_class == ELFCLASS ## B) { \ |
| 106 | Elf ## B ## _Ehdr *ehdr = EHDR ## B (elf->ehdr); \ |
105 | Elf ## B ## _Ehdr *ehdr = EHDR ## B (elf->ehdr); \ |
| … | |
… | |
| 389 | return NULL; |
388 | return NULL; |
| 390 | else |
389 | else |
| 391 | return " - "; |
390 | return " - "; |
| 392 | } |
391 | } |
| 393 | /* scan an elf file and show all the fun stuff */ |
392 | /* scan an elf file and show all the fun stuff */ |
| 394 | #define prints(str) fputs(str, stdout) |
393 | // #define prints(str) fputs(str, stdout) |
|
|
394 | #define prints(str) write(fileno(stdout), str, strlen(str)) |
| 395 | static void scanelf_file(const char *filename) |
395 | static void scanelf_file(const char *filename) |
| 396 | { |
396 | { |
| 397 | unsigned long i; |
397 | unsigned long i; |
| 398 | char found_pax, found_stack, found_relro, found_textrel, |
398 | char found_pax, found_stack, found_relro, found_textrel, |
| 399 | found_rpath, found_needed, found_interp, found_bind, |
399 | found_rpath, found_needed, found_interp, found_bind, |
| … | |
… | |
| 681 | {"nobanner", no_argument, NULL, 'B'}, |
681 | {"nobanner", no_argument, NULL, 'B'}, |
| 682 | {"help", no_argument, NULL, 'h'}, |
682 | {"help", no_argument, NULL, 'h'}, |
| 683 | {"version", no_argument, NULL, 'V'}, |
683 | {"version", no_argument, NULL, 'V'}, |
| 684 | {NULL, no_argument, NULL, 0x0} |
684 | {NULL, no_argument, NULL, 0x0} |
| 685 | }; |
685 | }; |
|
|
686 | |
| 686 | static char *opts_help[] = { |
687 | static char *opts_help[] = { |
| 687 | "Scan all directories in PATH environment", |
688 | "Scan all directories in PATH environment", |
| 688 | "Scan all directories in /etc/ld.so.conf", |
689 | "Scan all directories in /etc/ld.so.conf", |
| 689 | "Scan directories recursively", |
690 | "Scan directories recursively", |
| 690 | "Don't recursively cross mount points", |
691 | "Don't recursively cross mount points", |
| … | |
… | |
| 695 | "Print RPATH information", |
696 | "Print RPATH information", |
| 696 | "Print NEEDED information", |
697 | "Print NEEDED information", |
| 697 | "Print INTERP information", |
698 | "Print INTERP information", |
| 698 | "Print BIND information", |
699 | "Print BIND information", |
| 699 | "Find a specified symbol", |
700 | "Find a specified symbol", |
| 700 | "Print all scanned info (-x -e -t -r -n -i)\n", |
701 | "Print all scanned info (-x -e -t -r -n -i -b)\n", |
| 701 | "Only output 'bad' things", |
702 | "Only output 'bad' things", |
| 702 | "Be verbose (can be specified more than once)", |
703 | "Be verbose (can be specified more than once)", |
| 703 | "Use specified format for output", |
704 | "Use specified format for output", |
| 704 | "Read input stream from a filename", |
705 | "Read input stream from a filename", |
| 705 | "Write output stream to a filename", |
706 | "Write output stream to a filename", |
| … | |
… | |
| 879 | { |
880 | { |
| 880 | char *ret = strdup(s); |
881 | char *ret = strdup(s); |
| 881 | if (!ret) err("Could not strdup(): %s", strerror(errno)); |
882 | if (!ret) err("Could not strdup(): %s", strerror(errno)); |
| 882 | return ret; |
883 | return ret; |
| 883 | } |
884 | } |
|
|
885 | |
| 884 | static void *xmalloc(size_t size) |
886 | static void *xmalloc(size_t size) |
| 885 | { |
887 | { |
| 886 | void *ret = malloc(size); |
888 | void *ret = malloc(size); |
| 887 | if (!ret) err("Could not malloc() %li bytes", (unsigned long)size); |
889 | if (!ret) err("Could not malloc() %li bytes", (unsigned long)size); |
| 888 | return ret; |
890 | return ret; |
| 889 | } |
891 | } |
|
|
892 | |
| 890 | static void xstrcat(char **dst, const char *src, size_t *curr_len) |
893 | static void xstrcat(char **dst, const char *src, size_t *curr_len) |
| 891 | { |
894 | { |
| 892 | long new_len; |
895 | long new_len; |
| 893 | |
896 | |
| 894 | new_len = strlen(*dst) + strlen(src); |
897 | new_len = strlen(*dst) + strlen(src); |
| … | |
… | |
| 899 | err("could not realloc %li bytes", (unsigned long)*curr_len); |
902 | err("could not realloc %li bytes", (unsigned long)*curr_len); |
| 900 | } |
903 | } |
| 901 | |
904 | |
| 902 | strcat(*dst, src); |
905 | strcat(*dst, src); |
| 903 | } |
906 | } |
|
|
907 | |
| 904 | static inline void xchrcat(char **dst, const char append, size_t *curr_len) |
908 | static inline void xchrcat(char **dst, const char append, size_t *curr_len) |
| 905 | { |
909 | { |
| 906 | static char my_app[2]; |
910 | static char my_app[2]; |
| 907 | my_app[0] = append; |
911 | my_app[0] = append; |
| 908 | my_app[1] = '\0'; |
912 | my_app[1] = '\0'; |
| 909 | xstrcat(dst, my_app, curr_len); |
913 | xstrcat(dst, my_app, curr_len); |
| 910 | } |
914 | } |
| 911 | |
|
|
| 912 | |
915 | |
| 913 | |
916 | |
| 914 | int main(int argc, char *argv[]) |
917 | int main(int argc, char *argv[]) |
| 915 | { |
918 | { |
| 916 | if (argc < 2) |
919 | if (argc < 2) |