Mercurial > pub > dyncall > dyncall
annotate dynload/dynload_syms_elf.c @ 313:73b5b9e224e2
- dynload_plain test: stability fix for picking right testing lib on build
author | Tassilo Philipp |
---|---|
date | Tue, 05 Nov 2019 15:19:16 +0100 |
parents | f5577f6bf97a |
children | 2c2b51819aad |
rev | line source |
---|---|
0 | 1 /* |
2 | |
3 Package: dyncall | |
4 Library: dynload | |
5 File: dynload/dynload_syms_elf.c | |
6 Description: | |
7 License: | |
8 | |
281 | 9 Copyright (c) 2007-2018 Daniel Adler <dadler@uni-goettingen.de>, |
0 | 10 Tassilo Philipp <tphilipp@potion-studios.com>, |
11 Olivier Chafik <olivier.chafik@gmail.com> | |
12 | |
13 Permission to use, copy, modify, and distribute this software for any | |
14 purpose with or without fee is hereby granted, provided that the above | |
15 copyright notice and this permission notice appear in all copies. | |
16 | |
17 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
18 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
19 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
20 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
21 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
22 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
23 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
24 | |
25 */ | |
26 | |
27 | |
28 | |
29 #include "../autovar/autovar_OS.h" | |
30 | |
31 /* | |
32 | |
33 dynamic symbol resolver for elf | |
34 | |
35 */ | |
36 | |
37 #include "dynload.h" | |
38 #if defined(OS_OpenBSD) | |
39 # include <stdint.h> | |
40 # include <elf_abi.h> | |
207
bff2b940ea39
- netbsd include fix (stddef.h needed before elf.h)
Tassilo Philipp
parents:
171
diff
changeset
|
41 #elif defined(OS_NetBSD) |
bff2b940ea39
- netbsd include fix (stddef.h needed before elf.h)
Tassilo Philipp
parents:
171
diff
changeset
|
42 # include <stddef.h> |
bff2b940ea39
- netbsd include fix (stddef.h needed before elf.h)
Tassilo Philipp
parents:
171
diff
changeset
|
43 # include <elf.h> |
0 | 44 #elif defined(OS_SunOS) |
45 # include <libelf.h> | |
46 #elif defined(OS_BeOS) | |
47 # include <elf32.h> | |
48 #else | |
49 # include <elf.h> | |
50 #endif | |
51 | |
52 #if defined(__GLIBC__) | |
53 # define _GNU_SOURCE | |
54 # define __USE_GNU | |
55 #endif | |
56 | |
57 #include "dynload_alloc.h" | |
58 | |
59 #include <assert.h> | |
60 #include <fcntl.h> | |
61 #include <dlfcn.h> | |
62 #include <stdio.h> | |
63 #include <string.h> | |
64 #include <sys/stat.h> | |
65 #include <sys/types.h> | |
66 #include <sys/mman.h> | |
67 #include <unistd.h> | |
68 | |
69 /* run-time configuration 64/32 */ | |
70 #if defined(OS_OpenBSD) | |
71 #else | |
72 # include "../autovar/autovar_ABI.h" | |
73 # ifdef ABI_ELF64 | |
74 | |
75 typedef Elf64_Ehdr Elf_Ehdr; | |
76 typedef Elf64_Phdr Elf_Phdr; | |
77 typedef Elf64_Shdr Elf_Shdr; | |
78 typedef Elf64_Sym Elf_Sym; | |
79 # ifndef OS_SunOS | |
80 typedef Elf64_Dyn Elf_Dyn; | |
81 # endif | |
82 typedef Elf64_Sxword Elf_tag; | |
83 typedef Elf64_Addr Elf_Addr; | |
84 | |
85 # else | |
86 # if defined(OS_BeOS) | |
87 typedef struct Elf32_Ehdr Elf_Ehdr; | |
88 typedef struct Elf32_Phdr Elf_Phdr; | |
89 typedef struct Elf32_Shdr Elf_Shdr; | |
90 typedef struct Elf32_Sym Elf_Sym; | |
91 typedef struct Elf32_Dyn Elf_Dyn; | |
92 typedef Elf32_Sword Elf_tag; | |
93 typedef Elf32_Addr Elf_Addr; | |
94 | |
95 # else | |
96 | |
97 typedef Elf32_Ehdr Elf_Ehdr; | |
98 typedef Elf32_Phdr Elf_Phdr; | |
99 typedef Elf32_Shdr Elf_Shdr; | |
100 typedef Elf32_Sym Elf_Sym; | |
101 # ifndef OS_SunOS | |
102 typedef Elf32_Dyn Elf_Dyn; | |
103 # endif | |
104 typedef Elf32_Sword Elf_tag; | |
105 typedef Elf32_Addr Elf_Addr; | |
106 | |
107 # endif | |
108 # endif | |
109 #endif | |
110 | |
111 | |
112 struct DLSyms_ | |
113 { | |
114 const char* pStrTab; | |
115 Elf_Sym* pSymTab; | |
116 size_t strTabSize; | |
117 size_t nSymbols; | |
118 Elf_Ehdr* pElf_Ehdr; /* pointer to elf header */ | |
119 int file; /* fd of lib */ | |
120 size_t fileSize; /* filesize of open lib */ | |
121 }; | |
122 | |
123 | |
124 DLSyms* dlSymsInit(const char* libPath) | |
125 { | |
126 unsigned char* pMem; | |
127 int i; | |
128 struct stat st; | |
129 Elf_Shdr* pS; | |
231 | 130 DLSyms* pSyms; |
131 | |
132 if(stat(libPath, &st) == -1) | |
133 return NULL; | |
134 | |
135 i = open(libPath, O_RDONLY); | |
136 if(i == -1) | |
137 return NULL; | |
138 | |
139 pSyms = (DLSyms*)dlAllocMem(sizeof(DLSyms)); | |
0 | 140 memset(pSyms, 0, sizeof(DLSyms)); |
231 | 141 pSyms->file = i; |
0 | 142 pSyms->fileSize = st.st_size; |
143 pSyms->pElf_Ehdr = (Elf_Ehdr*) mmap((void*) NULL, pSyms->fileSize, PROT_READ, MAP_SHARED, pSyms->file, 0); | |
144 | |
145 #ifdef ABI_ELF32 | |
146 assert(pSyms->pElf_Ehdr->e_ident[EI_CLASS] == ELFCLASS32); | |
147 #else | |
148 assert(pSyms->pElf_Ehdr->e_ident[EI_CLASS] == ELFCLASS64); | |
149 #endif | |
150 | |
151 assert(pSyms->pElf_Ehdr->e_phoff > 0); | |
152 assert(pSyms->pElf_Ehdr->e_shoff > 0); | |
153 | |
154 | |
155 pMem = (unsigned char*)pSyms->pElf_Ehdr; | |
156 | |
157 /* traverse section headers */ | |
158 pS = (Elf_Shdr*) ( pMem + pSyms->pElf_Ehdr->e_shoff ); | |
159 /* skip section 0 which is always zero due to the Elf standard. */ | |
160 for (i = 1; i < pSyms->pElf_Ehdr->e_shnum; i++) | |
161 { | |
162 Elf_Shdr* pSection = &pS[i]; | |
231 | 163 void* pSectionContent = ((char*)pMem) + pSection->sh_offset; |
0 | 164 switch (pSection->sh_type) |
165 { | |
166 case SHT_DYNSYM: | |
167 if (!pSyms->pSymTab) { | |
168 pSyms->pSymTab = (Elf_Sym*)pSectionContent; | |
169 pSyms->nSymbols = pSection->sh_size / pSection->sh_entsize; | |
170 } | |
171 break; | |
172 case SHT_STRTAB: | |
171
011b5e3a8548
- compatibility for older platforms - use ANSI comments
cslag
parents:
0
diff
changeset
|
173 /* Do not trust pSyms->pElf_Ehdr->e_shstrndx! */ |
0 | 174 if (!pSyms->pStrTab) { |
175 pSyms->pStrTab = (const char*)pSectionContent; | |
176 pSyms->strTabSize = pSection->sh_size; | |
177 } | |
178 break; | |
179 } | |
180 if (pSyms->pSymTab && pSyms->pStrTab) | |
181 break; | |
182 } | |
183 return pSyms; | |
184 } | |
185 | |
186 | |
187 void dlSymsCleanup(DLSyms* pSyms) | |
188 { | |
189 if(pSyms) { | |
190 munmap((void*) pSyms->pElf_Ehdr, pSyms->fileSize); | |
191 close(pSyms->file); | |
192 dlFreeMem(pSyms); | |
193 } | |
194 } | |
195 | |
196 | |
197 int dlSymsCount(DLSyms* pSyms) | |
198 { | |
231 | 199 return pSyms ? pSyms->nSymbols : 0; |
0 | 200 } |
201 | |
202 | |
203 const char* dlSymsName(DLSyms* pSyms, int index) | |
204 { | |
205 int str_index; | |
206 if(!pSyms || !pSyms->pSymTab || index < 0 || index >= pSyms->nSymbols) | |
207 return NULL; | |
231 | 208 |
0 | 209 str_index = pSyms->pSymTab[index].st_name; |
210 if (str_index < 0 || str_index >= pSyms->strTabSize) | |
211 return NULL; | |
212 return &pSyms->pStrTab[str_index]; | |
213 } | |
214 | |
215 | |
216 const char* dlSymsNameFromValue(DLSyms* pSyms, void* value) | |
217 { | |
218 Dl_info info; | |
219 return (dladdr(value, &info) && (value == info.dli_saddr)) | |
220 ? info.dli_sname | |
221 : NULL; | |
222 } | |
223 |