-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
163 lines (151 loc) · 4.94 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* Author: Matthieu Carteron <[email protected]>
* date: 2024-07-17
*
* Alter ELF needed dependencies information (shrink to remove version).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dynamic.h"
static void usage(char *progname)
{
printf("Usage: %s [<options>] <elf-file>\n\n\
Options:\n\
-s,--soname : Replace (or remove) the soname\n\
-r,--rpath : Replace (or remove) the run-time path\n\
-n,--replace : Replace needed dependency by one with another name\n\
--replace-deps : Perform replacement searching for missing dependencies\n\
--repair-deps : Perform repair on dependencies (don't run on system packages)\n\
--priority-low : Change the run-time path priority: system libs are above\n\
--priority-high : Change the run-time path priority: system libs are below \n\
-d,--query-depends : Query the dependencies needed (non-recursive)\n\
--query-soname : Query the soname\n\
--query-rpath : Query the run-time path\n\
-o,--output : Output file\n\
-h,--help : Show help usage\n\n\
In order to replace needed dependency, supply two names:\n Example:\n\
-n <old-name> <new-name>\n\n\
In order to remove soname or run-time path, don't supply a name after the parameter.\n", progname);
}
int main(int argc, char *const argv[])
{
int i = 1, fix = 0;
const char *output = NULL;
const char *soname = NULL;
const char *needOld = NULL;
const char *needNew = NULL;
const char *rpath = NULL;
const char *filename = NULL;
LD_Cache *ldcache = NULL;
Priority priority = PRI_UNCHANGED;
Query query = QU_NOTHING;
/* Checks the arguments */
while (i < argc)
{
const char *arg = argv[i++];
if (strcmp(arg, "-?") == 0 ||
strcmp(arg, "-h") == 0 ||
strcmp(arg, "--help") == 0)
{
usage(argv[0]);
return 0;
}
else if (strcmp(arg, "-s") == 0 ||
strcmp(arg, "--soname") == 0)
{
if (i >= argc || argv[i][0] == '-')
{
soname = REMOVAL;
continue;
}
soname = argv[i++];
}
else if (strcmp(arg, "-r") == 0 ||
strcmp(arg, "--rpath") == 0)
{
if (i >= argc || argv[i][0] == '-')
{
rpath = REMOVAL;
continue;
}
rpath = argv[i++];
}
else if (strcmp(arg, "-n") == 0 ||
strcmp(arg, "--replace") == 0)
{
if (i+1 >= argc || argv[i][0] == '-' || argv[i+1][0] == '-')
{
fputs("Missing two names after the parameter!\n", stderr);
return 1;
}
needOld = argv[i++];
needNew = argv[i++];
}
else if (strcmp(arg, "-o") == 0 ||
strcmp(arg, "--output") == 0)
{
if (i >= argc || argv[i][0] == '-')
{
fputs("Missing output after parameter!\n", stderr);
return 1;
}
else
output = argv[i++];
}
else if (strcmp(arg, "-d") == 0 ||
strcmp(arg, "--query-depends") == 0)
query = QU_NEEDED;
else if (strcmp(arg, "--query-soname") == 0)
query = QU_SONAME;
else if (strcmp(arg, "--query-rpath") == 0)
query = QU_RPATH;
else if (strcmp(arg, "--priority-low") == 0)
priority = PRI_RUNPATH;
else if (strcmp(arg, "--priority-high") == 0)
priority = PRI_RPATH;
else if (strcmp(arg, "--replace-deps") == 0)
fix = 1;
else if (strcmp(arg, "--repair-deps") == 0)
fix = 2;
else if (arg[0] == '-')
{
fprintf(stderr, "Unrecognized parameter: %s\n", arg);
return 1;
}
else
{
if (filename != NULL)
{
fputs("Only one file can be supplied!\n", stderr);
return 1;
}
filename = arg;
}
}
/* If no files are specified */
if (filename == NULL)
{
usage(argv[0]);
return 2;
}
/* Check the input and the output are not the same */
if (output != NULL)
{
if (strcmp(filename, output) == 0)
{
fputs("The input and the output can't be the same!\n", stderr);
return 2;
}
}
/* If a simple query is selected */
if (query != QU_NOTHING)
return dynamics_query(filename, query);
/* Read the LD cache, to warn the user about whether a library is found or not */
if (needNew != NULL || fix != 0)
ldcache = ldcache_parse("/etc/ld.so.cache");
i = dynamics_process(ldcache, priority, filename, output, needOld, needNew, soname, rpath, fix);
if (ldcache != NULL)
ldcache_free(ldcache);
return i;
}