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
|
#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;
typedef struct elfInfo_s {
Elf *elf;
int isDSO;
int isElf64; /* is 64bit marker needed in dependencies */
int isExec; /* requires are only added to executables */
int gotDEBUG;
int gotHASH;
int gotGNUHASH;
int gotSONAME;
ARGV_t requires;
ARGV_t provides;
} elfInfo;
static int skipPrivate(const char *s)
{
return (filter_private && rstreq(s, "GLIBC_PRIVATE"));
}
static void processVerDef(Elf_Scn *scn, GElf_Shdr *shdr, elfInfo *ei)
{
Elf_Data *data;
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;
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 && !skipPrivate(s)) {
const char *marker = ei->isElf64 ? "(64bit)" : "";
char *dep = NULL;
rasprintf(&dep, "%s(%s)%s", soname, s, marker);
argvAdd(&ei->provides, dep);
rfree(dep);
}
}
}
}
rfree(soname);
}
static void processVerNeed(Elf_Scn *scn, GElf_Shdr *shdr, elfInfo *ei)
{
Elf_Data *data;
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 && !skipPrivate(s)) {
const char *marker = ei->isElf64 ? "(64bit)" : "";
char *dep = NULL;
rasprintf(&dep, "%s(%s)%s", soname, s, marker);
argvAdd(&ei->requires, dep);
rfree(dep);
}
auxoffset += aux->vna_next;
}
offset += need->vn_next;
}
}
rfree(soname);
}
static void processDynamic(Elf_Scn *scn, GElf_Shdr *shdr, elfInfo *ei)
{
Elf_Data *data;
while ((data = elf_getdata(scn, data)) != NULL) {
for (int i = 0; i < (shdr->sh_size / shdr->sh_entsize); i++) {
ARGV_t *deptype = NULL;
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:
ei->gotSONAME = 1;
s = elf_strptr(ei->elf, shdr->sh_link, dyn->d_un.d_val);
deptype = &ei->provides;
break;
case DT_NEEDED:
if (ei->isExec) {
s = elf_strptr(ei->elf, shdr->sh_link, dyn->d_un.d_val);
deptype = &ei->requires;
}
break;
default:
break;
}
if (s && deptype) {
const char *marker = ei->isElf64 ? "()(64bit)" : "";
char *dep = rstrscat(NULL, s, marker, NULL);
argvAdd(deptype, dep);
rfree(dep);
}
}
}
}
static void processSections(elfInfo *ei)
{
Elf_Scn * scn;
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) {
/* on alpha, everything is 64bit but we dont want the (64bit) markers */
#if !defined(__alpha__)
ei->isElf64 = (ehdr->e_ident[EI_CLASS] == ELFCLASS64);
#else
ei->isElf64 = 0;
#endif
ei->isDSO = (ehdr->e_type == ET_DYN);
ei->isExec = (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) {
argvAdd(&ei->requires, "rtld(GNU_HASH)");
}
/* For DSO's, provide the basename of the file if DT_SONAME not found. */
if (ei->isDSO && !ei->gotDEBUG && !ei->gotSONAME) {
const char *marker = ei->isElf64 ? "()(64bit)" : "";
const char *bn = strrchr(fn, '/');
char *dep;
bn = bn ? bn + 1 : fn;
dep = rstrscat(NULL, bn, marker, NULL);
argvAdd(&ei->provides, dep);
rfree(dep);
}
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);
if (ei->elf) elf_end(ei->elf);
rfree(ei);
}
return rc;
}
int main(int argc, char *argv[])
{
char fn[BUFSIZ];
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 },
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);
}
while (fgets(fn, sizeof(fn), stdin) != NULL) {
fn[strlen(fn)-1] = '\0';
(void) processFile(fn, requires);
}
poptFreeContext(optCon);
return 0;
}
|