summaryrefslogtreecommitdiff
path: root/elf.c
blob: e0d66bfcf72352166ce966ecf0d7d745c0d2a56a (plain)
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
 * revdep - reverse dependency checker for rawnix
 * elf.c - ELF parsing, hash-table cache, library resolution
 *
 * Copyright (c) 2025 zorz@gmx.com
 * Licensed under the BSD 2-Clause License. See revdep.h.
 *
 * x86_64-only.  musl-only.  No ld.so.conf (musl uses DT_RUNPATH).
 */

#define _POSIX_C_SOURCE 200809L
#define _DEFAULT_SOURCE

#include "revdep.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <libgen.h>
#include <limits.h>
#include <gelf.h>

/* -------------------------------------------------------------------- */
/*  ELF inspection                                                      */
/* -------------------------------------------------------------------- */

static bool
elf_is_valid(Elf *e)
{
	GElf_Ehdr ehdr;

	if (!e || elf_kind(e) != ELF_K_ELF)
		return false;

	if (!gelf_getehdr(e, &ehdr))
		return false;

	/* x86_64 only */
	if (ehdr.e_machine != EM_X86_64)
		return false;

	/* executables and shared objects */
	if (ehdr.e_type != ET_EXEC && ehdr.e_type != ET_DYN)
		return false;

	/* Linux or unspecified */
	if (ehdr.e_ident[EI_OSABI] != ELFOSABI_NONE &&
	    ehdr.e_ident[EI_OSABI] != ELFOSABI_LINUX)
		return false;

	return true;
}

static bool
elf_read_dynamic(Elf *e, elfinfo_t *info)
{
	size_t phnum, i, j, count;
	GElf_Phdr phdr;
	Elf_Scn *scn;
	GElf_Shdr shdr;
	Elf_Data *data;
	GElf_Dyn dyn;
	const char *str;

	if (elf_getphdrnum(e, &phnum) == -1)
		return false;

	for (i = 0; i < phnum; i++) {
		if (!gelf_getphdr(e, (int)i, &phdr))
			continue;
		if (phdr.p_type != PT_DYNAMIC)
			continue;

		scn = gelf_offscn(e, phdr.p_offset);
		if (!scn)
			continue;
		if (!gelf_getshdr(scn, &shdr))
			continue;
		if (shdr.sh_type != SHT_DYNAMIC)
			continue;

		data = elf_getdata(scn, NULL);
		if (!data)
			return false;

		count = shdr.sh_size /
		        gelf_fsize(e, ELF_T_DYN, 1, EV_CURRENT);

		for (j = 0; j < count; j++) {
			if (!gelf_getdyn(data, (int)j, &dyn))
				break;

			str = elf_strptr(e, shdr.sh_link, dyn.d_un.d_val);
			if (!str)
				continue;

			switch (dyn.d_tag) {
			case DT_NEEDED:
				strvec_push(&info->needed, str);
				break;
			case DT_RPATH:
				strsplit(str, ':', &info->rpath);
				break;
			case DT_RUNPATH:
				strsplit(str, ':', &info->runpath);
				break;
			default:
				break;
			}
		}
		return true;
	}

	return false;
}

elfinfo_t *
elf_inspect(const char *path)
{
	elfinfo_t *info;
	int fd;
	Elf *e;

	if (!path)
		return NULL;

	info = calloc(1, sizeof(*info));
	if (!info)
		return NULL;

	info->path = xstrdup(path);
	strvec_init(&info->needed);
	strvec_init(&info->rpath);
	strvec_init(&info->runpath);
	info->valid = false;

	fd = open(path, O_RDONLY);
	if (fd == -1) {
		elf_free(info);
		return NULL;
	}

	e = elf_begin(fd, ELF_C_READ_MMAP, NULL);
	if (!e) {
		close(fd);
		elf_free(info);
		return NULL;
	}

	if (!elf_is_valid(e) || !elf_read_dynamic(e, info)) {
		elf_end(e);
		close(fd);
		elf_free(info);
		return NULL;
	}

	info->valid = true;
	elf_end(e);
	close(fd);
	return info;
}

void
elf_free(elfinfo_t *e)
{
	if (!e)
		return;
	free(e->path);
	strvec_free(&e->needed);
	strvec_free(&e->rpath);
	strvec_free(&e->runpath);
	free(e);
}

/* -------------------------------------------------------------------- */
/*  Hash-table ELF cache                                                */
/*                                                                      */
/*  Many packages share the same libraries (libz, libc++, etc).         */
/*  Without caching, we'd re-open and re-parse the same .so hundreds    */
/*  of times.  The hash table gives O(1) lookup by path.                */
/* -------------------------------------------------------------------- */

#define CACHE_BUCKETS 2048

typedef struct cache_node {
	char              *key;
	elfinfo_t         *val;
	struct cache_node *next;
} cache_node_t;

static cache_node_t *cache_table[CACHE_BUCKETS];

static unsigned long
hash_djb2(const char *s)
{
	unsigned long h = 5381;
	int c;

	while ((c = *s++))
		h = ((h << 5) + h) + (unsigned long)c;
	return h % CACHE_BUCKETS;
}

elfinfo_t *
cache_lookup(const char *path)
{
	unsigned long h;
	cache_node_t *n;
	elfinfo_t *info;

	if (!path)
		return NULL;

	h = hash_djb2(path);

	for (n = cache_table[h]; n; n = n->next) {
		if (strcmp(n->key, path) == 0)
			return n->val;
	}

	/* not cached — inspect and store */
	info = elf_inspect(path);
	if (!info || !info->valid) {
		elf_free(info);
		return NULL;
	}

	n = malloc(sizeof(*n));
	if (!n) {
		elf_free(info);
		return NULL;
	}

	n->key  = xstrdup(path);
	n->val  = info;
	n->next = cache_table[h];
	cache_table[h] = n;

	return info;
}

void
cache_clear(void)
{
	size_t i;
	cache_node_t *n, *next;

	for (i = 0; i < CACHE_BUCKETS; i++) {
		for (n = cache_table[i]; n; n = next) {
			next = n->next;
			free(n->key);
			elf_free(n->val);
			free(n);
		}
		cache_table[i] = NULL;
	}
}

/* -------------------------------------------------------------------- */
/*  $ORIGIN expansion                                                   */
/*                                                                      */
/*  musl only needs $ORIGIN (and $LIB = "lib").                          */
/*  No $PLATFORM, no multi-lib.                                         */
/* -------------------------------------------------------------------- */

static char *
expand_origin(const elfinfo_t *elf, const char *dir)
{
	char origin[PATH_MAX];
	char result[PATH_MAX];
	const char *src;
	char *dst, *end, *d;
	size_t len, dlen;

	if (!elf || !dir)
		return NULL;

	/* get directory of the ELF file */
	len = strlen(elf->path);
	if (len >= PATH_MAX)
		return NULL;
	memcpy(origin, elf->path, len + 1);
	d = dirname(origin);
	dlen = strlen(d);

	src = dir;
	dst = result;
	end = result + PATH_MAX - 1;

	while (*src && dst < end) {
		if (*src == '$') {
			if (strncmp(src, "$ORIGIN", 7) == 0 ||
			    strncmp(src, "${ORIGIN}", 9) == 0) {
				size_t skip = (src[1] == '{') ? 9 : 7;
				if (dst + dlen >= end)
					return NULL;
				memcpy(dst, d, dlen);
				dst += dlen;
				src += skip;
				continue;
			}
			if (strncmp(src, "$LIB", 4) == 0 ||
			    strncmp(src, "${LIB}", 6) == 0) {
				size_t skip = (src[1] == '{') ? 6 : 4;
				if (dst + 3 >= end)
					return NULL;
				memcpy(dst, "lib", 3);
				dst += 3;
				src += skip;
				continue;
			}
		}
		*dst++ = *src++;
	}
	*dst = '\0';

	return xstrdup(result);
}

/* -------------------------------------------------------------------- */
/*  Library resolution — follows ld.so search order                     */
/* -------------------------------------------------------------------- */

static bool
try_lib_in_dirs(const elfinfo_t *elf, const char *lib,
                const strvec_t *dirs, bool do_expand)
{
	size_t i;
	char *dir, *full, *real;
	elfinfo_t *libelf;

	if (!elf || !lib || !dirs)
		return false;

	for (i = 0; i < dirs->count; i++) {
		if (do_expand) {
			dir = expand_origin(elf, dirs->items[i]);
			if (!dir)
				continue;
		} else {
			dir = dirs->items[i];
		}

		full = xpathjoin(dir, lib);
		if (do_expand)
			free(dir);
		if (!full)
			continue;

		real = realpath_safe(full);
		free(full);
		if (!real)
			continue;

		libelf = cache_lookup(real);
		free(real);

		if (libelf)
			return true;
	}

	return false;
}

/*
 * Search order (simplified for musl — no ld.so.conf, no ldconfig cache):
 *
 *  1. DT_RPATH   (only if DT_RUNPATH absent)
 *  2. DT_RUNPATH
 *  3. System dirs (/lib, /usr/lib)
 *  4. Package-specific dirs (/etc/revdep.d/<pkg>)
 */
bool
lib_find(const elfinfo_t *elf, const pkg_t *pkg,
         const char *lib, const strvec_t *sys_dirs)
{
	if (!elf || !lib || !sys_dirs)
		return false;

	/* absolute or relative path in DT_NEEDED — resolve directly */
	if (strchr(lib, '/')) {
		char *real = realpath_safe(lib);
		if (!real)
			return false;
		elfinfo_t *e = cache_lookup(real);
		free(real);
		return e != NULL;
	}

	/* 1. DT_RPATH (deprecated, but still used by some software) */
	if (elf->rpath.count > 0 && elf->runpath.count == 0) {
		if (try_lib_in_dirs(elf, lib, &elf->rpath, true))
			return true;
	}

	/* 2. DT_RUNPATH */
	if (elf->runpath.count > 0) {
		if (try_lib_in_dirs(elf, lib, &elf->runpath, true))
			return true;
	}

	/* 3. system directories */
	if (try_lib_in_dirs(elf, lib, sys_dirs, false))
		return true;

	/* 4. per-package directories from /etc/revdep.d */
	if (pkg && pkg->dirs.count > 0) {
		if (try_lib_in_dirs(elf, lib, &pkg->dirs, false))
			return true;
	}

	return false;
}