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
|
diff -crB -N src/opengl/find-dlysm.h src/opengl.2/find-dlysm.h
*** src/opengl/find-dlysm.h 1970-01-01 01:00:00.000000000 +0100
--- src/opengl.2/find-dlysm.h 2023-08-19 18:14:06.273497867 +0200
***************
*** 0 ****
--- 1,175 ----
+ #ifndef _FIND_DLSYM_H_
+ #define _FIND_DLSYM_H_
+
+ //
+ // find-dlsym.h - dlsym locator for Linux - written by Hunter Kvalevog
+ // https://github.com/hkva/find-dlsym
+ //
+ // How to use:
+ // Just include this file and call find_dlsym(). The function will return
+ // a pointer to dlsym or 0 if it cannot be located.
+ //
+ // License:
+ // This is free and unencumbered software released into the public domain.
+ //
+ // Anyone is free to copy, modify, publish, use, compile, sell, or
+ // distribute this software, either in source code form or as a compiled
+ // binary, for any purpose, commercial or non-commercial, and by any
+ // means.
+ //
+ // In jurisdictions that recognize copyright laws, the author or authors
+ // of this software dedicate any and all copyright interest in the
+ // software to the public domain. We make this dedication for the benefit
+ // of the public at large and to the detriment of our heirs and
+ // successors. We intend this dedication to be an overt act of
+ // relinquishment in perpetuity of all present and future rights to this
+ // software under copyright law.
+ //
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ // IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ // OTHER DEALINGS IN THE SOFTWARE.
+ //
+ // For more information, please refer to <http://unlicense.org/>
+ //
+
+ #ifndef _GNU_SOURCE
+ #define _GNU_SOURCE
+ #endif
+
+ #ifndef FIND_DLSYM_LIBC_NAME
+ #define FIND_DLSYM_LIBC_NAME "libc.so"
+ #endif
+
+ #include <elf.h>
+ #include <fcntl.h>
+ #include <link.h>
+ #include <linux/limits.h>
+ #include <string.h>
+ #include <sys/mman.h>
+ #include <sys/stat.h>
+ #include <unistd.h>
+
+ //
+ // Types
+ //
+
+ typedef void*(*find_dlsym_t)(void* handle, const char* symbol);
+
+ typedef struct {
+ unsigned long base_address;
+ char path[PATH_MAX];
+ } find_dlsym__libc_result_t;
+
+ //
+ // Private functions
+ //
+
+ static const char* find_dlsym__basename(const char* path) {
+ const char* result = path;
+ for (int i = 0; path[i] != '\0'; ++i) {
+ if (path[i] == '/' && path[i + 1] != '\0') {
+ result = &path[i + 1];
+ }
+ }
+ return result;
+ }
+
+ static int find_dlsym__cb(struct dl_phdr_info* info, size_t sz, void* user) {
+ (void)sz;
+ find_dlsym__libc_result_t* result = (find_dlsym__libc_result_t*)user;
+
+ // Ignore bad names
+ if (!info->dlpi_name || info->dlpi_name[0] == '\0') {
+ return 0; // continue
+ }
+
+ const char* filepath = info->dlpi_name;
+ const char* filename = find_dlsym__basename(filepath);
+ if (strstr(filename, FIND_DLSYM_LIBC_NAME) != NULL) {
+ // Found libc
+ result->base_address = info->dlpi_addr;
+ strncpy(result->path, filepath, sizeof(result->path));
+ return 1; // done
+ }
+
+ return 0; // continue
+ }
+
+ //
+ // Public API
+ //
+
+ find_dlsym_t find_dlsym(void) {
+ // Need to find the libc shared library
+ find_dlsym__libc_result_t libc;
+ if (dl_iterate_phdr(find_dlsym__cb, &libc) == 0) {
+ return 0; // couldn't find libc
+ }
+
+ // Need to map file in from disk
+ int fd = open(libc.path, O_RDONLY);
+ if (fd == -1) {
+ return 0; // couldn't open shared object
+ }
+
+ struct stat st;
+ if (fstat(fd, &st) == -1) {
+ close(fd);
+ return 0; // couldn't get shared object length
+ }
+
+ void* elf_map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
+ close(fd);
+ if (elf_map == MAP_FAILED) {
+ return 0; // couldn't load shared object
+ }
+
+ // Parse ELF file
+ unsigned long dlsym_offset = 0;
+ unsigned long elf_base = (unsigned long)elf_map;
+ ElfW(Ehdr)* ehdr = (ElfW(Ehdr)*)elf_base;
+ ElfW(Shdr)* shdr = (ElfW(Shdr)*)(elf_base + ehdr->e_shoff);
+
+ // Section header string table
+ const char* shstrtab =
+ (const char*)(elf_base + shdr[ehdr->e_shstrndx].sh_offset);
+
+ // Find .dynsym and .dynstr section headers by name
+ ElfW(Shdr)* dynsymhdr = NULL;
+ ElfW(Shdr)* dynstrhdr = NULL;
+ for (int i = 0; i < ehdr->e_shnum; ++i) {
+ const char* shname = &shstrtab[shdr[i].sh_name];
+ if (strcmp(shname, ".dynsym") == 0) {
+ dynsymhdr = &shdr[i];
+ } else if (strcmp(shname, ".dynstr") == 0) {
+ dynstrhdr = &shdr[i];
+ }
+ }
+
+ // Iterate over .dynsym entries and find dlsym
+ ElfW(Sym)* syms = (ElfW(Sym*))(elf_base + dynsymhdr->sh_offset);
+ unsigned long syms_count = dynsymhdr->sh_size / dynsymhdr->sh_entsize;
+ const char* names = (const char*)(elf_base + dynstrhdr->sh_offset);
+ for (unsigned long i = 0; i < syms_count; ++i) {
+ if (strcmp(&names[syms[i].st_name], "dlsym") == 0) {
+ // found it :)
+ dlsym_offset = syms[i].st_value;
+ break;
+ }
+ }
+
+ // Done
+ munmap(elf_map, st.st_size);
+
+ if (dlsym_offset != 0) {
+ return (find_dlsym_t)(libc.base_address + dlsym_offset);
+ } else {
+ return 0; // dlsym couldn't be located
+ }
+ }
+
+ #endif // _FIND_DLSYM_H_
|