| 1 | /* |
1 | /* |
| 2 | * Copyright 2008 Gentoo Foundation |
2 | * Copyright 2008 Gentoo Foundation |
| 3 | * Distributed under the terms of the GNU General Public License v2 |
3 | * Distributed under the terms of the GNU General Public License v2 |
| 4 | * $Header: /var/cvsroot/gentoo-projects/pax-utils/scanmacho.c,v 1.11 2008/12/30 13:13:15 vapier Exp $ |
4 | * $Header: /var/cvsroot/gentoo-projects/pax-utils/scanmacho.c,v 1.12 2008/12/30 13:27:09 vapier Exp $ |
| 5 | * |
5 | * |
| 6 | * based on scanelf by: |
6 | * based on scanelf by: |
| 7 | * Copyright 2003-2007 Ned Ludd - <solar@gentoo.org> |
7 | * Copyright 2003-2007 Ned Ludd - <solar@gentoo.org> |
| 8 | * Copyright 2004-2007 Mike Frysinger - <vapier@gentoo.org> |
8 | * Copyright 2004-2007 Mike Frysinger - <vapier@gentoo.org> |
| 9 | * for Darwin specific fun: |
9 | * for Darwin specific fun: |
| 10 | * 2008 Fabian Groffen - <grobian@gentoo.org> |
10 | * 2008 Fabian Groffen - <grobian@gentoo.org> |
| 11 | */ |
11 | */ |
| 12 | |
12 | |
| 13 | static const char *rcsid = "$Id: scanmacho.c,v 1.11 2008/12/30 13:13:15 vapier Exp $"; |
13 | static const char *rcsid = "$Id: scanmacho.c,v 1.12 2008/12/30 13:27:09 vapier Exp $"; |
| 14 | const char * const argv0 = "scanmacho"; |
14 | const char * const argv0 = "scanmacho"; |
| 15 | |
15 | |
| 16 | #include "paxinc.h" |
16 | #include "paxinc.h" |
| 17 | |
17 | |
| 18 | #define IS_MODIFIER(c) (c == '%' || c == '#' || c == '+') |
18 | #define IS_MODIFIER(c) (c == '%' || c == '#' || c == '+') |
| … | |
… | |
| 20 | /* prototypes */ |
20 | /* prototypes */ |
| 21 | static int scanmacho_fatobj(fatobj *fobj); |
21 | static int scanmacho_fatobj(fatobj *fobj); |
| 22 | static int scanmacho_file(const char *filename, const struct stat *st_cache); |
22 | static int scanmacho_file(const char *filename, const struct stat *st_cache); |
| 23 | static int scanmacho_from_file(const char *filename); |
23 | static int scanmacho_from_file(const char *filename); |
| 24 | static int scanmacho_dir(const char *path); |
24 | static int scanmacho_dir(const char *path); |
| 25 | static void scanelf_envpath(void); |
25 | static void scanmacho_envpath(void); |
| 26 | static void usage(int status); |
26 | static void usage(int status); |
| 27 | static int parseargs(int argc, char *argv[]); |
27 | static int parseargs(int argc, char *argv[]); |
| 28 | |
28 | |
| 29 | /* variables to control behavior */ |
29 | /* variables to control behavior */ |
| 30 | static char match_etypes[126] = ""; |
30 | static char match_etypes[126] = ""; |
| … | |
… | |
| 72 | char *found_lib, |
72 | char *found_lib, |
| 73 | int op, |
73 | int op, |
| 74 | char **ret, |
74 | char **ret, |
| 75 | size_t *ret_len |
75 | size_t *ret_len |
| 76 | ) { |
76 | ) { |
| 77 | char *needed; |
|
|
| 78 | loadcmd *lcmd; |
77 | loadcmd *lcmd; |
| 79 | struct dylib_command *dlcmd; |
|
|
| 80 | uint32_t lc_load_dylib = LC_LOAD_DYLIB; |
78 | uint32_t lc_load_dylib; |
| 81 | |
79 | |
| 82 | if ((op == 0 && !show_needed) || (op == 1 && !find_lib)) |
80 | if ((op == 0 && !show_needed) || (op == 1 && !find_lib)) |
| 83 | return NULL; |
81 | return NULL; |
| 84 | |
82 | |
| 85 | lcmd = firstloadcmd(fobj); |
83 | lcmd = firstloadcmd(fobj); |
| 86 | |
84 | lc_load_dylib = MGET(fobj->swapped, LC_LOAD_DYLIB); |
| 87 | if (fobj->swapped) |
|
|
| 88 | lc_load_dylib = bswap_32(lc_load_dylib); |
|
|
| 89 | |
85 | |
| 90 | do { |
86 | do { |
| 91 | if (lcmd->lcmd->cmd == lc_load_dylib) { |
87 | if (lcmd->lcmd->cmd == lc_load_dylib) { |
| 92 | dlcmd = (struct dylib_command*)lcmd->data; |
88 | struct dylib_command *dlcmd = (struct dylib_command*)lcmd->data; |
| 93 | if (fobj->swapped) { |
89 | char *needed; |
| 94 | needed = (char *)(lcmd->data + |
|
|
| 95 | bswap_32(dlcmd->dylib.name.offset)); |
|
|
| 96 | } else { |
|
|
| 97 | needed = (char *)(lcmd->data + dlcmd->dylib.name.offset); |
90 | needed = (char *)(lcmd->data + MGET(fobj->swapped, dlcmd->dylib.name.offset)); |
| 98 | } |
|
|
| 99 | if (op == 0) { |
91 | if (op == 0) { |
| 100 | if (!be_wewy_wewy_quiet) { |
92 | if (!be_wewy_wewy_quiet) { |
| 101 | if (*found_needed) |
93 | if (*found_needed) |
| 102 | xchrcat(ret, ',', ret_len); |
94 | xchrcat(ret, ',', ret_len); |
| 103 | xstrcat(ret, needed, ret_len); |
95 | xstrcat(ret, needed, ret_len); |
| … | |
… | |
| 122 | } |
114 | } |
| 123 | |
115 | |
| 124 | static char *macho_file_interp(fatobj *fobj, char *found_interp) |
116 | static char *macho_file_interp(fatobj *fobj, char *found_interp) |
| 125 | { |
117 | { |
| 126 | loadcmd *lcmd; |
118 | loadcmd *lcmd; |
| 127 | uint32_t lc_load_dylinker = LC_LOAD_DYLINKER; |
119 | uint32_t lc_load_dylinker; |
| 128 | |
120 | |
| 129 | if (!show_interp) |
121 | if (!show_interp) |
| 130 | return NULL; |
122 | return NULL; |
| 131 | |
123 | |
| 132 | lcmd = firstloadcmd(fobj); |
124 | lcmd = firstloadcmd(fobj); |
| 133 | |
125 | lc_load_dylinker = MGET(fobj->swapped, LC_LOAD_DYLINKER); |
| 134 | if (fobj->swapped) |
|
|
| 135 | lc_load_dylinker = bswap_32(lc_load_dylinker); |
|
|
| 136 | |
126 | |
| 137 | do { |
127 | do { |
| 138 | if (lcmd->lcmd->cmd == lc_load_dylinker) { |
128 | if (lcmd->lcmd->cmd == lc_load_dylinker) { |
| 139 | struct dylinker_command *dlcmd = |
129 | struct dylinker_command *dlcmd = |
| 140 | (struct dylinker_command*)lcmd->data; |
130 | (struct dylinker_command*)lcmd->data; |
| 141 | char *dylinker; |
131 | char *dylinker; |
| 142 | if (fobj->swapped) { |
|
|
| 143 | dylinker = (char *)(lcmd->data + |
|
|
| 144 | bswap_32(dlcmd->name.offset)); |
|
|
| 145 | } else { |
|
|
| 146 | dylinker = (char *)(lcmd->data + dlcmd->name.offset); |
132 | dylinker = (char *)(lcmd->data + MGET(fobj->swapped, dlcmd->name.offset)); |
| 147 | } |
|
|
| 148 | *found_interp = 1; |
133 | *found_interp = 1; |
| 149 | free(lcmd); |
134 | free(lcmd); |
| 150 | return (be_wewy_wewy_quiet ? NULL : dylinker); |
135 | return (be_wewy_wewy_quiet ? NULL : dylinker); |
| 151 | } |
136 | } |
| 152 | } while (nextloadcmd(lcmd)); |
137 | } while (nextloadcmd(lcmd)); |
| … | |
… | |
| 155 | } |
140 | } |
| 156 | |
141 | |
| 157 | static char *macho_file_soname(fatobj *fobj, char *found_soname) |
142 | static char *macho_file_soname(fatobj *fobj, char *found_soname) |
| 158 | { |
143 | { |
| 159 | loadcmd *lcmd; |
144 | loadcmd *lcmd; |
| 160 | char *soname; |
|
|
| 161 | uint32_t lc_id_dylib = LC_ID_DYLIB; |
145 | uint32_t lc_id_dylib; |
| 162 | |
146 | |
| 163 | if (!show_soname) |
147 | if (!show_soname) |
| 164 | return NULL; |
148 | return NULL; |
| 165 | |
149 | |
| 166 | lcmd = firstloadcmd(fobj); |
150 | lcmd = firstloadcmd(fobj); |
| 167 | |
151 | lc_id_dylib = MGET(fobj->swapped, LC_ID_DYLIB); |
| 168 | if (fobj->swapped) |
|
|
| 169 | lc_id_dylib = bswap_32(lc_id_dylib); |
|
|
| 170 | |
152 | |
| 171 | do { |
153 | do { |
| 172 | if (lcmd->lcmd->cmd == lc_id_dylib) { |
154 | if (lcmd->lcmd->cmd == lc_id_dylib) { |
| 173 | struct dylib_command *dlcmd = (struct dylib_command*)lcmd->data; |
155 | struct dylib_command *dlcmd = (struct dylib_command*)lcmd->data; |
| 174 | if (fobj->swapped) { |
156 | char *soname; |
| 175 | soname = (char *)(lcmd->data + |
|
|
| 176 | bswap_32(dlcmd->dylib.name.offset)); |
|
|
| 177 | } else { |
|
|
| 178 | soname = (char *)(lcmd->data + dlcmd->dylib.name.offset); |
157 | soname = (char *)(lcmd->data + MGET(fobj->swapped, dlcmd->dylib.name.offset)); |
| 179 | } |
|
|
| 180 | *found_soname = 1; |
158 | *found_soname = 1; |
| 181 | free(lcmd); |
159 | free(lcmd); |
| 182 | return (be_wewy_wewy_quiet ? NULL : soname); |
160 | return (be_wewy_wewy_quiet ? NULL : soname); |
| 183 | } |
161 | } |
| 184 | } while (nextloadcmd(lcmd)); |
162 | } while (nextloadcmd(lcmd)); |
| … | |
… | |
| 456 | if (be_verbose > 2) printf("%s: does not exist\n", path); |
434 | if (be_verbose > 2) printf("%s: does not exist\n", path); |
| 457 | return 1; |
435 | return 1; |
| 458 | } |
436 | } |
| 459 | |
437 | |
| 460 | /* ok, if it isn't a directory, assume we can open it */ |
438 | /* ok, if it isn't a directory, assume we can open it */ |
| 461 | if (!S_ISDIR(st_top.st_mode)) { |
439 | if (!S_ISDIR(st_top.st_mode)) |
| 462 | return scanmacho_file(path, &st_top); |
440 | return scanmacho_file(path, &st_top); |
| 463 | } |
|
|
| 464 | |
441 | |
| 465 | /* now scan the dir looking for fun stuff */ |
442 | /* now scan the dir looking for fun stuff */ |
| 466 | if ((dir = opendir(path)) == NULL) { |
443 | if ((dir = opendir(path)) == NULL) { |
| 467 | warnf("could not opendir %s: %s", path, strerror(errno)); |
444 | warnf("could not opendir %s: %s", path, strerror(errno)); |
| 468 | return 1; |
445 | return 1; |
| … | |
… | |
| 515 | fclose(fp); |
492 | fclose(fp); |
| 516 | return ret; |
493 | return ret; |
| 517 | } |
494 | } |
| 518 | |
495 | |
| 519 | /* scan env PATH for paths */ |
496 | /* scan env PATH for paths */ |
| 520 | static void scanelf_envpath(void) |
497 | static void scanmacho_envpath(void) |
| 521 | { |
498 | { |
| 522 | char *path, *p; |
499 | char *path, *p; |
| 523 | |
500 | |
| 524 | path = getenv("PATH"); |
501 | path = getenv("PATH"); |
| 525 | if (!path) |
502 | if (!path) |
| … | |
… | |
| 737 | } |
714 | } |
| 738 | if (be_verbose > 2) printf("Format: %s\n", out_format); |
715 | if (be_verbose > 2) printf("Format: %s\n", out_format); |
| 739 | |
716 | |
| 740 | /* now lets actually do the scanning */ |
717 | /* now lets actually do the scanning */ |
| 741 | if (scan_envpath) |
718 | if (scan_envpath) |
| 742 | scanelf_envpath(); |
719 | scanmacho_envpath(); |
| 743 | if (!from_file && optind == argc && ttyname(0) == NULL && !scan_envpath) |
720 | if (!from_file && optind == argc && ttyname(0) == NULL && !scan_envpath) |
| 744 | from_file = "-"; |
721 | from_file = "-"; |
| 745 | if (from_file) { |
722 | if (from_file) { |
| 746 | scanmacho_from_file(from_file); |
723 | scanmacho_from_file(from_file); |
| 747 | from_file = *argv; |
724 | from_file = *argv; |