OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2014 The Chromium Authors. All rights reserved. | |
3 * Use of this source code is governed by a BSD-style license that can be | |
4 * found in the LICENSE file. | |
5 */ | |
bulach
2014/05/14 12:57:05
nit: I think we use // instead of multiline commen
Primiano Tucci (use gerrit)
2014/05/14 13:21:23
OMG, you're asking me to be non ANSI-C compliant?
| |
6 | |
7 #include <dlfcn.h> | |
8 #include <stdio.h> | |
9 #include <sys/prctl.h> | |
10 #include <unistd.h> | |
11 | |
12 /* | |
13 * This is a wrapper to run position independent executables on Android ICS, | |
14 * where the linker doesn't support PIE. This requires the PIE binaries to be | |
15 * built with CFLAGS +=-fvisibility=default -fPIE, and LDFLAGS += -rdynamic -pie | |
16 * such that the main() symbol remains exported and can be dlsym-ed. | |
17 */ | |
18 | |
19 #define ERR_PREFIX "[PIE Loader] " | |
20 | |
21 typedef int (*main_t)(int, char**); | |
22 | |
23 | |
24 int main(int argc, char** argv) | |
bulach
2014/05/14 12:57:05
nit: { on this line
Primiano Tucci (use gerrit)
2014/05/14 13:21:23
Done.
| |
25 { | |
26 if (argc < 2) { | |
27 printf("Usage: %s path_to_pie_executable [args]\n", argv[0]); | |
28 return -1; | |
29 } | |
30 | |
31 /* Shift left the argv[]. argv is what /proc/PID/cmdline prints out. In turn | |
32 * cmdline is what Android "ps" prints out. In turn "ps" is what many scripts | |
33 * look for to decide which processes to kill / killall. | |
34 */ | |
35 int i; | |
36 char* next_argv_start = argv[0]; | |
37 for (i = 1; i < argc; ++i) { | |
38 const size_t argv_len = strlen(argv[i]) + 1; | |
39 memcpy(argv[i-1], argv[i], argv_len); | |
bulach
2014/05/14 12:57:05
nit: s/i-1/i - 1/
Primiano Tucci (use gerrit)
2014/05/14 13:21:23
Done.
| |
40 next_argv_start += argv_len; | |
41 argv[i] = next_argv_start; | |
42 } | |
43 argv[argc - 1] = NULL; /* The last argv must be a NULL ptr. */ | |
44 | |
45 /* Set also the proc name accordingly (/proc/PID/comm). */ | |
46 prctl(PR_SET_NAME, (long) argv[0]); | |
47 | |
48 /* dlopen should not fail, unless: | |
49 * - The target binary does not exists: | |
50 * - The dependent .so libs cannot be loaded. | |
51 * In both cases, just bail out with an explicit error message. | |
52 */ | |
53 void* handle = dlopen(argv[0], RTLD_NOW); | |
54 if (handle == NULL) { | |
55 printf(ERR_PREFIX "dlopen() failed: %s.\n", dlerror()); | |
56 return -1; | |
57 } | |
58 | |
59 main_t pie_main = (main_t) dlsym(handle, "main"); | |
60 if (pie_main) { | |
61 return pie_main(argc - 1, argv); | |
62 } | |
63 | |
64 /* If we reached this point dlsym failed, very likely because the target | |
65 * binary has not been compiled with the proper CFLAGS / LDFLAGS. | |
66 * At this point the most sensible thing to do is running that normally | |
67 * via exec and hope that the target binary wasn't a PIE. | |
68 */ | |
69 execv(argv[0], argv); | |
70 | |
71 /* exevc is supposed to never return, unless it fails. */ | |
72 printf(ERR_PREFIX "Both dlsym() and the execv() fallback failed.\n"); | |
73 perror("execv"); | |
74 return -1; | |
75 } | |
OLD | NEW |