Mercurial > pub > dyncall > dyncall
annotate dynload/dynload_syms_elf.c @ 386:2c2b51819aad
- newer and portable beos/haiku uses elf.h instead of elf32.h (maybe older builds only used elf32.h in raw source? either way, just using elf.h now)
author | Tassilo Philipp |
---|---|
date | Thu, 21 Jan 2021 17:15:35 +0100 |
parents | f5577f6bf97a |
children | c7f879fef352 |
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 #else | |
47 # include <elf.h> | |
48 #endif | |
49 | |
50 #if defined(__GLIBC__) | |
51 # define _GNU_SOURCE | |
52 # define __USE_GNU | |
53 #endif | |
54 | |
55 #include "dynload_alloc.h" | |
56 | |
57 #include <assert.h> | |
58 #include <fcntl.h> | |
59 #include <dlfcn.h> | |
60 #include <stdio.h> | |
61 #include <string.h> | |
62 #include <sys/stat.h> | |
63 #include <sys/types.h> | |
64 #include <sys/mman.h> | |
65 #include <unistd.h> | |
66 | |
67 /* run-time configuration 64/32 */ | |
68 #if defined(OS_OpenBSD) | |
69 #else | |
70 # include "../autovar/autovar_ABI.h" | |
71 # ifdef ABI_ELF64 | |
72 | |
73 typedef Elf64_Ehdr Elf_Ehdr; | |
74 typedef Elf64_Phdr Elf_Phdr; | |
75 typedef Elf64_Shdr Elf_Shdr; | |
76 typedef Elf64_Sym Elf_Sym; | |
77 # ifndef OS_SunOS | |
78 typedef Elf64_Dyn Elf_Dyn; | |
79 # endif | |
80 typedef Elf64_Sxword Elf_tag; | |
81 typedef Elf64_Addr Elf_Addr; | |
82 | |
83 # else | |
84 # if defined(OS_BeOS) | |
85 typedef struct Elf32_Ehdr Elf_Ehdr; | |
86 typedef struct Elf32_Phdr Elf_Phdr; | |
87 typedef struct Elf32_Shdr Elf_Shdr; | |
88 typedef struct Elf32_Sym Elf_Sym; | |
89 typedef struct Elf32_Dyn Elf_Dyn; | |
90 typedef Elf32_Sword Elf_tag; | |
91 typedef Elf32_Addr Elf_Addr; | |
92 | |
93 # else | |
94 | |
95 typedef Elf32_Ehdr Elf_Ehdr; | |
96 typedef Elf32_Phdr Elf_Phdr; | |
97 typedef Elf32_Shdr Elf_Shdr; | |
98 typedef Elf32_Sym Elf_Sym; | |
99 # ifndef OS_SunOS | |
100 typedef Elf32_Dyn Elf_Dyn; | |
101 # endif | |
102 typedef Elf32_Sword Elf_tag; | |
103 typedef Elf32_Addr Elf_Addr; | |
104 | |
105 # endif | |
106 # endif | |
107 #endif | |
108 | |
109 | |
110 struct DLSyms_ | |
111 { | |
112 const char* pStrTab; | |
113 Elf_Sym* pSymTab; | |
114 size_t strTabSize; | |
115 size_t nSymbols; | |
116 Elf_Ehdr* pElf_Ehdr; /* pointer to elf header */ | |
117 int file; /* fd of lib */ | |
118 size_t fileSize; /* filesize of open lib */ | |
119 }; | |
120 | |
121 | |
122 DLSyms* dlSymsInit(const char* libPath) | |
123 { | |
124 unsigned char* pMem; | |
125 int i; | |
126 struct stat st; | |
127 Elf_Shdr* pS; | |
231 | 128 DLSyms* pSyms; |
129 | |
130 if(stat(libPath, &st) == -1) | |
131 return NULL; | |
132 | |
133 i = open(libPath, O_RDONLY); | |
134 if(i == -1) | |
135 return NULL; | |
136 | |
137 pSyms = (DLSyms*)dlAllocMem(sizeof(DLSyms)); | |
0 | 138 memset(pSyms, 0, sizeof(DLSyms)); |
231 | 139 pSyms->file = i; |
0 | 140 pSyms->fileSize = st.st_size; |
141 pSyms->pElf_Ehdr = (Elf_Ehdr*) mmap((void*) NULL, pSyms->fileSize, PROT_READ, MAP_SHARED, pSyms->file, 0); | |
142 | |
143 #ifdef ABI_ELF32 | |
144 assert(pSyms->pElf_Ehdr->e_ident[EI_CLASS] == ELFCLASS32); | |
145 #else | |
146 assert(pSyms->pElf_Ehdr->e_ident[EI_CLASS] == ELFCLASS64); | |
147 #endif | |
148 | |
149 assert(pSyms->pElf_Ehdr->e_phoff > 0); | |
150 assert(pSyms->pElf_Ehdr->e_shoff > 0); | |
151 | |
152 | |
153 pMem = (unsigned char*)pSyms->pElf_Ehdr; | |
154 | |
155 /* traverse section headers */ | |
156 pS = (Elf_Shdr*) ( pMem + pSyms->pElf_Ehdr->e_shoff ); | |
157 /* skip section 0 which is always zero due to the Elf standard. */ | |
158 for (i = 1; i < pSyms->pElf_Ehdr->e_shnum; i++) | |
159 { | |
160 Elf_Shdr* pSection = &pS[i]; | |
231 | 161 void* pSectionContent = ((char*)pMem) + pSection->sh_offset; |
0 | 162 switch (pSection->sh_type) |
163 { | |
164 case SHT_DYNSYM: | |
165 if (!pSyms->pSymTab) { | |
166 pSyms->pSymTab = (Elf_Sym*)pSectionContent; | |
167 pSyms->nSymbols = pSection->sh_size / pSection->sh_entsize; | |
168 } | |
169 break; | |
170 case SHT_STRTAB: | |
171
011b5e3a8548
- compatibility for older platforms - use ANSI comments
cslag
parents:
0
diff
changeset
|
171 /* Do not trust pSyms->pElf_Ehdr->e_shstrndx! */ |
0 | 172 if (!pSyms->pStrTab) { |
173 pSyms->pStrTab = (const char*)pSectionContent; | |
174 pSyms->strTabSize = pSection->sh_size; | |
175 } | |
176 break; | |
177 } | |
178 if (pSyms->pSymTab && pSyms->pStrTab) | |
179 break; | |
180 } | |
181 return pSyms; | |
182 } | |
183 | |
184 | |
185 void dlSymsCleanup(DLSyms* pSyms) | |
186 { | |
187 if(pSyms) { | |
188 munmap((void*) pSyms->pElf_Ehdr, pSyms->fileSize); | |
189 close(pSyms->file); | |
190 dlFreeMem(pSyms); | |
191 } | |
192 } | |
193 | |
194 | |
195 int dlSymsCount(DLSyms* pSyms) | |
196 { | |
231 | 197 return pSyms ? pSyms->nSymbols : 0; |
0 | 198 } |
199 | |
200 | |
201 const char* dlSymsName(DLSyms* pSyms, int index) | |
202 { | |
203 int str_index; | |
204 if(!pSyms || !pSyms->pSymTab || index < 0 || index >= pSyms->nSymbols) | |
205 return NULL; | |
231 | 206 |
0 | 207 str_index = pSyms->pSymTab[index].st_name; |
208 if (str_index < 0 || str_index >= pSyms->strTabSize) | |
209 return NULL; | |
210 return &pSyms->pStrTab[str_index]; | |
211 } | |
212 | |
213 | |
214 const char* dlSymsNameFromValue(DLSyms* pSyms, void* value) | |
215 { | |
216 Dl_info info; | |
217 return (dladdr(value, &info) && (value == info.dli_saddr)) | |
218 ? info.dli_sname | |
219 : NULL; | |
220 } | |
221 |