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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <error.h>
#include <errno.h>
#include <popt.h>
#include <gelf.h>
#include <rpm/rpmstring.h>
#include <rpm/argv.h>
int filter_private = 0;
int soname_only = 0;
int fake_soname = 1;
int assume_exec = 0;
typedef struct elfInfo_s {
Elf *elf;
int isDSO;
int isExec; /* requires are only added to executables */
int gotDEBUG;
int gotHASH;
int gotGNUHASH;
char *soname;
const char *marker; /* elf class marker or NULL */
ARGV_t requires;
ARGV_t provides;
} elfInfo;
static int skipPrivate(const char *s)
{
return (filter_private && rstreq(s, "GLIBC_PRIVATE"));
}
static const char *mkmarker(GElf_Ehdr *ehdr)
{
const char *marker = NULL;
if (ehdr->e_ident[EI_CLASS] == ELFCLASS64) {
switch (ehdr->e_machine) {
case EM_ALPHA:
case EM_FAKE_ALPHA:
/* alpha doesn't traditionally have 64bit markers */
break;
default:
marker = "(64bit)";
break;
}
}
return marker;
}
static void addDep(ARGV_t *deps,
const char *soname, const char *ver, const char *marker)
{
char *dep = NULL;
if (ver || marker) {
rasprintf(&dep,
"%s(%s)%s", soname, ver ? ver : "", marker ? marker : "");
}
argvAdd(deps, dep ? dep : soname);
free(dep);
}
static void processVerDef(Elf_Scn *scn, GElf_Shdr *shdr, elfInfo *ei)
{
Elf_Data *data = NULL;
unsigned int offset, auxoffset;
char *soname = NULL;
while ((data = elf_getdata(scn, data)) != NULL) {
offset = 0;
for (int i = shdr->sh_info; --i >= 0; ) {
GElf_Verdef def_mem, *def;
def = gelf_getverdef (data, offset, &def_mem);
if (def == NULL)
break;
auxoffset = offset + def->vd_aux;
offset += def->vd_next;
for (int j = def->vd_cnt; --j >= 0; ) {
GElf_Verdaux aux_mem, * aux;
const char *s;
aux = gelf_getverdaux (data, auxoffset, &aux_mem);
if (aux == NULL)
break;
s = elf_strptr(ei->elf, shdr->sh_link, aux->vda_name);
if (s == NULL)
break;
if (def->vd_flags & VER_FLG_BASE) {
rfree(soname);
soname = rstrdup(s);
auxoffset += aux->vda_next;
continue;
} else if (soname && !soname_only && !skipPrivate(s)) {
addDep(&ei->provides, soname, s, ei->marker);
}
}
}
}
rfree(soname);
}
static void processVerNeed(Elf_Scn *scn, GElf_Shdr *shdr, elfInfo *ei)
{
Elf_Data *data = NULL;
char *soname = NULL;
while ((data = elf_getdata(scn, data)) != NULL) {
unsigned int offset = 0, auxoffset;
for (int i = shdr->sh_info; --i >= 0; ) {
const char *s = NULL;
GElf_Verneed need_mem, *need;
need = gelf_getverneed (data, offset, &need_mem);
if (need == NULL)
break;
s = elf_strptr(ei->elf, shdr->sh_link, need->vn_file);
if (s == NULL)
break;
rfree(soname);
soname = rstrdup(s);
auxoffset = offset + need->vn_aux;
for (int j = need->vn_cnt; --j >= 0; ) {
GElf_Vernaux aux_mem, * aux;
aux = gelf_getvernaux (data, auxoffset, &aux_mem);
if (aux == NULL)
break;
s = elf_strptr(ei->elf, shdr->sh_link, aux->vna_name);
if (s == NULL)
break;
if (ei->isExec && soname && !soname_only && !skipPrivate(s)) {
addDep(&ei->requires, soname, s, ei->marker);
}
auxoffset += aux->vna_next;
}
offset += need->vn_next;
}
}
rfree(soname);
}
static void processDynamic(Elf_Scn *scn, GElf_Shdr *shdr, elfInfo *ei)
{
Elf_Data *data = NULL;
while ((data = elf_getdata(scn, data)) != NULL) {
for (int i = 0; i < (shdr->sh_size / shdr->sh_entsize); i++) {
const char *s = NULL;
GElf_Dyn dyn_mem, *dyn;
dyn = gelf_getdyn (data, i, &dyn_mem);
if (dyn == NULL)
break;
switch (dyn->d_tag) {
case DT_HASH:
ei->gotHASH = 1;
break;
case DT_GNU_HASH:
ei->gotGNUHASH = 1;
break;
case DT_DEBUG:
ei->gotDEBUG = 1;
break;
case DT_SONAME:
s = elf_strptr(ei->elf, shdr->sh_link, dyn->d_un.d_val);
if (s)
ei->soname = rstrdup(s);
break;
case DT_NEEDED:
if (ei->isExec) {
s = elf_strptr(ei->elf, shdr->sh_link, dyn->d_un.d_val);
if (s)
addDep(&ei->requires, s, NULL, ei->marker);
}
break;
}
}
}
}
static void processSections(elfInfo *ei)
{
Elf_Scn * scn = NULL;
while ((scn = elf_nextscn(ei->elf, scn)) != NULL) {
GElf_Shdr shdr_mem, *shdr;
shdr = gelf_getshdr(scn, &shdr_mem);
if (shdr == NULL)
break;
switch (shdr->sh_type) {
case SHT_GNU_verdef:
processVerDef(scn, shdr, ei);
break;
case SHT_GNU_verneed:
processVerNeed(scn, shdr, ei);
break;
case SHT_DYNAMIC:
processDynamic(scn, shdr, ei);
break;
default:
break;
}
}
}
static int processFile(const char *fn, int dtype)
{
int rc = 1;
int fdno = -1;
struct stat st;
GElf_Ehdr *ehdr, ehdr_mem;
elfInfo *ei = rcalloc(1, sizeof(*ei));
fdno = open(fn, O_RDONLY);
if (fdno < 0 || fstat(fdno, &st) < 0)
goto exit;
(void) elf_version(EV_CURRENT);
ei->elf = elf_begin(fdno, ELF_C_READ, NULL);
if (ei->elf == NULL || elf_kind(ei->elf) != ELF_K_ELF)
goto exit;
ehdr = gelf_getehdr(ei->elf, &ehdr_mem);
if (ehdr == NULL)
goto exit;
if (ehdr->e_type == ET_DYN || ehdr->e_type == ET_EXEC) {
ei->marker = mkmarker(ehdr);
ei->isDSO = (ehdr->e_type == ET_DYN);
ei->isExec = assume_exec || (st.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH));
processSections(ei);
}
/*
* For DSOs which use the .gnu_hash section and don't have a .hash
* section, we need to ensure that we have a new enough glibc.
*/
if (ei->isExec && ei->gotGNUHASH && !ei->gotHASH && !soname_only) {
argvAdd(&ei->requires, "rtld(GNU_HASH)");
}
/*
* For DSOs, add DT_SONAME as provide. If its missing, we can fake
* it from the basename if requested. The bizarre looking DT_DEBUG
* check is used to avoid adding basename provides for PIE executables.
*/
if (ei->isDSO && !ei->gotDEBUG) {
if (!ei->soname && fake_soname) {
const char *bn = strrchr(fn, '/');
ei->soname = rstrdup(bn ? bn + 1 : fn);
}
if (ei->soname)
addDep(&ei->provides, ei->soname, NULL, ei->marker);
}
rc = 0;
/* dump the requested dependencies for this file */
for (ARGV_t dep = dtype ? ei->requires : ei->provides; dep && *dep; dep++) {
fprintf(stdout, "%s\n", *dep);
}
exit:
if (fdno >= 0) close(fdno);
if (ei) {
argvFree(ei->provides);
argvFree(ei->requires);
free(ei->soname);
if (ei->elf) elf_end(ei->elf);
rfree(ei);
}
return rc;
}
int main(int argc, char *argv[])
{
int provides = 0;
int requires = 0;
poptContext optCon;
struct poptOption opts[] = {
{ "provides", 'P', POPT_ARG_VAL, &provides, -1, NULL, NULL },
{ "requires", 'R', POPT_ARG_VAL, &requires, -1, NULL, NULL },
{ "filter-private", 0, POPT_ARG_VAL, &filter_private, -1, NULL, NULL },
{ "soname-only", 0, POPT_ARG_VAL, &soname_only, -1, NULL, NULL },
{ "no-fake-soname", 0, POPT_ARG_VAL, &fake_soname, 0, NULL, NULL },
{ "assume-exec", 0, POPT_ARG_VAL, &assume_exec, -1, NULL, NULL },
POPT_AUTOHELP
POPT_TABLEEND
};
optCon = poptGetContext(argv[0], argc, (const char **) argv, opts, 0);
if (argc < 2 || poptGetNextOpt(optCon) == 0) {
poptPrintUsage(optCon, stderr, 0);
exit(EXIT_FAILURE);
}
/* Normally our data comes from stdin, but permit args too */
if (poptPeekArg(optCon)) {
const char *fn;
while ((fn = poptGetArg(optCon)) != NULL) {
(void) processFile(fn, requires);
}
} else {
char fn[BUFSIZ];
while (fgets(fn, sizeof(fn), stdin) != NULL) {
fn[strlen(fn)-1] = '\0';
(void) processFile(fn, requires);
}
}
poptFreeContext(optCon);
return 0;
}
|