Mercurial > pub > dyncall > dyncall
annotate dynload/dynload_syms_mach-o.c @ 250:7cb8a0aaf638
- note about c99 (+ anon struct/union) requirements in doc
author | Tassilo Philipp |
---|---|
date | Sun, 14 May 2017 00:19:15 +0200 |
parents | 85b61e8facfe |
children | 1e3617d8a951 |
rev | line source |
---|---|
0 | 1 /* |
2 | |
3 Package: dyncall | |
4 Library: dynload | |
5 File: dynload/dynload_syms_mach-o.c | |
219 | 6 Description: |
0 | 7 License: |
8 | |
217 | 9 Copyright (c) 2007-2015 Olivier Chafik <olivier.chafik@gmail.com>, |
219 | 10 2017 refactored completely for stability, API |
11 consistency and portability by Tassilo Philipp. | |
0 | 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 /* | |
219 | 30 |
0 | 31 dynamic symbol resolver for Mach-O |
32 | |
33 */ | |
34 | |
35 #include "dynload.h" | |
36 #include "dynload_alloc.h" | |
217 | 37 #include "../autovar/autovar_ARCH.h" |
220
94dc0bdd7dbe
- make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents:
219
diff
changeset
|
38 #include "../autovar/autovar_OS.h" |
0 | 39 |
40 #include <mach-o/dyld.h> | |
41 #include <mach-o/nlist.h> | |
217 | 42 #include <sys/stat.h> |
0 | 43 #include <dlfcn.h> |
44 #include <string.h> | |
45 | |
220
94dc0bdd7dbe
- make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents:
219
diff
changeset
|
46 #if defined(ARCH_X64) || defined(ARCH_PPC64) || defined(ARCH_ARM64) /*@@@ use dyncall_macros.h*/ |
0 | 47 #define MACH_HEADER_TYPE mach_header_64 |
223
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
48 #define MACH_HEADER_MAGIC_NR MH_MAGIC_64 |
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
49 #define SEGMEND_COMMAND_ID LC_SEGMENT_64 |
219 | 50 #define SEGMENT_COMMAND segment_command_64 |
0 | 51 #define NLIST_TYPE nlist_64 |
52 #else | |
53 #define MACH_HEADER_TYPE mach_header | |
223
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
54 #define MACH_HEADER_MAGIC_NR MH_MAGIC |
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
55 #define SEGMEND_COMMAND_ID LC_SEGMENT |
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
56 #define SEGMENT_COMMAND segment_command |
0 | 57 #define NLIST_TYPE nlist |
58 #endif | |
59 | |
60 | |
61 struct DLSyms_ | |
62 { | |
217 | 63 DLLib* pLib; |
64 const char* pStringTable; | |
0 | 65 const struct NLIST_TYPE* pSymbolTable; |
217 | 66 uint32_t symbolCount; |
226
6e40eb9fb261
- different strategy for mach-o files to lookup symbol names, in order to be consistent with other functions (that use dladdr() and dlsym() calls, which mostly don't use '_' prefixes)
Tassilo Philipp
parents:
223
diff
changeset
|
67 uintptr_t symOffset; |
0 | 68 }; |
69 | |
70 | |
217 | 71 DLSyms* dlSymsInit(const char* libPath) |
0 | 72 { |
217 | 73 DLLib* pLib; |
223
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
74 DLSyms* pSyms = NULL; |
219 | 75 uint32_t i, n; |
217 | 76 struct stat st0; |
219 | 77 const struct MACH_HEADER_TYPE* pHeader = NULL; |
223
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
78 const struct dysymtab_command* dysymtab_cmd = NULL; |
217 | 79 |
80 if(stat(libPath, &st0) == -1) | |
81 return NULL; | |
82 | |
83 pLib = dlLoadLibrary(libPath); | |
84 if(!pLib) | |
85 return NULL; | |
86 | |
220
94dc0bdd7dbe
- make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents:
219
diff
changeset
|
87 /* Loop over all dynamically linked images to find ours. */ |
219 | 88 for(i = 0, n = _dyld_image_count(); i < n; ++i) |
0 | 89 { |
217 | 90 struct stat st1; |
219 | 91 const char* name = _dyld_get_image_name(i); |
217 | 92 |
93 if(name && (stat(name, &st1) != -1)) | |
0 | 94 { |
220
94dc0bdd7dbe
- make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents:
219
diff
changeset
|
95 /* Don't rely on name comparison alone, as libPath might be relative, symlink, differently */ |
94dc0bdd7dbe
- make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents:
219
diff
changeset
|
96 /* cased, etc., but compare inode number with the one of the mapped dyld image. */ |
219 | 97 if(st0.st_ino == st1.st_ino/*!strcmp(name, libPath)*/) |
0 | 98 { |
219 | 99 pHeader = (const struct MACH_HEADER_TYPE*) _dyld_get_image_header(i); |
242 | 100 //@@@ slide = _dyld_get_image_vmaddr_slide(i); |
220
94dc0bdd7dbe
- make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents:
219
diff
changeset
|
101 break; /* found header */ |
0 | 102 } |
103 } | |
104 } | |
219 | 105 |
223
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
106 if(pHeader && (pHeader->magic == MACH_HEADER_MAGIC_NR) && (pHeader->filetype == MH_DYLIB) && !(pHeader->flags & MH_SPLIT_SEGS)) |
219 | 107 { |
108 const char* pBase = (const char*)pHeader; | |
226
6e40eb9fb261
- different strategy for mach-o files to lookup symbol names, in order to be consistent with other functions (that use dladdr() and dlsym() calls, which mostly don't use '_' prefixes)
Tassilo Philipp
parents:
223
diff
changeset
|
109 uintptr_t slide = 0, symOffset = 0; |
219 | 110 const struct load_command* cmd = (const struct load_command*)(pBase + sizeof(struct MACH_HEADER_TYPE)); |
111 | |
112 for(i = 0, n = pHeader->ncmds; i < n; ++i, cmd = (const struct load_command*)((const char*)cmd + cmd->cmdsize)) | |
113 { | |
223
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
114 if(cmd->cmd == SEGMEND_COMMAND_ID) |
219 | 115 { |
116 const struct SEGMENT_COMMAND* seg = (struct SEGMENT_COMMAND*)cmd; | |
226
6e40eb9fb261
- different strategy for mach-o files to lookup symbol names, in order to be consistent with other functions (that use dladdr() and dlsym() calls, which mostly don't use '_' prefixes)
Tassilo Philipp
parents:
223
diff
changeset
|
117 if((seg->fileoff == 0) && (seg->filesize != 0)) |
6e40eb9fb261
- different strategy for mach-o files to lookup symbol names, in order to be consistent with other functions (that use dladdr() and dlsym() calls, which mostly don't use '_' prefixes)
Tassilo Philipp
parents:
223
diff
changeset
|
118 slide = (uintptr_t)pHeader - seg->vmaddr; /* effective offset of segment from header */ |
6e40eb9fb261
- different strategy for mach-o files to lookup symbol names, in order to be consistent with other functions (that use dladdr() and dlsym() calls, which mostly don't use '_' prefixes)
Tassilo Philipp
parents:
223
diff
changeset
|
119 |
6e40eb9fb261
- different strategy for mach-o files to lookup symbol names, in order to be consistent with other functions (that use dladdr() and dlsym() calls, which mostly don't use '_' prefixes)
Tassilo Philipp
parents:
223
diff
changeset
|
120 if(strcmp(seg->segname, "__LINKEDIT") == 0) { |
6e40eb9fb261
- different strategy for mach-o files to lookup symbol names, in order to be consistent with other functions (that use dladdr() and dlsym() calls, which mostly don't use '_' prefixes)
Tassilo Philipp
parents:
223
diff
changeset
|
121 /* Adjust pBase depending on where __LINKEDIT segment is */ |
6e40eb9fb261
- different strategy for mach-o files to lookup symbol names, in order to be consistent with other functions (that use dladdr() and dlsym() calls, which mostly don't use '_' prefixes)
Tassilo Philipp
parents:
223
diff
changeset
|
122 pBase = (const char*)(seg->vmaddr - seg->fileoff) + slide; |
6e40eb9fb261
- different strategy for mach-o files to lookup symbol names, in order to be consistent with other functions (that use dladdr() and dlsym() calls, which mostly don't use '_' prefixes)
Tassilo Philipp
parents:
223
diff
changeset
|
123 symOffset = slide; /* this is also offset of symbols */ |
6e40eb9fb261
- different strategy for mach-o files to lookup symbol names, in order to be consistent with other functions (that use dladdr() and dlsym() calls, which mostly don't use '_' prefixes)
Tassilo Philipp
parents:
223
diff
changeset
|
124 } |
219 | 125 } |
223
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
126 else if(cmd->cmd == LC_SYMTAB && !pSyms/* only init once - just safety check */) |
219 | 127 { |
128 const struct symtab_command* scmd = (const struct symtab_command*)cmd; | |
129 | |
220
94dc0bdd7dbe
- make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents:
219
diff
changeset
|
130 /* cmd->cmdsize must be size of struct, otherwise something is off; abort */ |
219 | 131 if(cmd->cmdsize != sizeof(struct symtab_command)) |
132 break; | |
133 | |
134 pSyms = (DLSyms*)dlAllocMem(sizeof(DLSyms)); | |
135 pSyms->symbolCount = scmd->nsyms; | |
136 pSyms->pStringTable = pBase + scmd->stroff; | |
137 pSyms->pSymbolTable = (struct NLIST_TYPE*)(pBase + scmd->symoff); | |
226
6e40eb9fb261
- different strategy for mach-o files to lookup symbol names, in order to be consistent with other functions (that use dladdr() and dlsym() calls, which mostly don't use '_' prefixes)
Tassilo Philipp
parents:
223
diff
changeset
|
138 pSyms->symOffset = symOffset; |
219 | 139 pSyms->pLib = pLib; |
223
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
140 } |
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
141 else if(cmd->cmd == LC_DYSYMTAB) |
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
142 { |
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
143 dysymtab_cmd = (const struct dysymtab_command*)cmd; |
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
144 /*@@@ check if(cmd->cmdsize != sizeof(struct dysymtab_command)) { |
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
145 dlFreeMem.... |
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
146 break; |
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
147 }*/ |
219 | 148 } |
149 } | |
150 } | |
151 | |
223
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
152 /* Got symbol table? */ |
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
153 if(pSyms) { |
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
154 /* Alter symtable info if we got symbols organized in local/defined/undefined groups. */ |
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
155 /* Only use local ones in that case. */ |
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
156 /*@@@ don't restrict to only local symbols if(dysymtab_cmd) { |
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
157 pSyms->pSymbolTable += dysymtab_cmd->ilocalsym; |
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
158 pSyms->symbolCount = dysymtab_cmd->nlocalsym; |
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
159 }*/ |
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
160 return pSyms; |
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
161 } |
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
162 |
220
94dc0bdd7dbe
- make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents:
219
diff
changeset
|
163 /* Couldn't init syms, so free lib and return error. */ |
219 | 164 dlFreeLibrary(pLib); |
0 | 165 return NULL; |
166 } | |
167 | |
168 | |
169 void dlSymsCleanup(DLSyms* pSyms) | |
170 { | |
217 | 171 if(pSyms) { |
172 dlFreeLibrary(pSyms->pLib); | |
173 dlFreeMem(pSyms); | |
174 } | |
0 | 175 } |
176 | |
177 int dlSymsCount(DLSyms* pSyms) | |
178 { | |
217 | 179 return pSyms ? pSyms->symbolCount : 0; |
0 | 180 } |
181 | |
219 | 182 |
183 const char* dlSymsName(DLSyms* pSyms, int index) | |
0 | 184 { |
185 const struct NLIST_TYPE* nl; | |
219 | 186 unsigned char t; |
187 | |
188 if(!pSyms) | |
0 | 189 return NULL; |
217 | 190 |
0 | 191 nl = pSyms->pSymbolTable + index; |
226
6e40eb9fb261
- different strategy for mach-o files to lookup symbol names, in order to be consistent with other functions (that use dladdr() and dlsym() calls, which mostly don't use '_' prefixes)
Tassilo Philipp
parents:
223
diff
changeset
|
192 t = nl->n_type & N_TYPE; |
219 | 193 |
226
6e40eb9fb261
- different strategy for mach-o files to lookup symbol names, in order to be consistent with other functions (that use dladdr() and dlsym() calls, which mostly don't use '_' prefixes)
Tassilo Philipp
parents:
223
diff
changeset
|
194 /* Return name by lookup through it's address. This guarantees to be consistent with dlsym and dladdr */ |
6e40eb9fb261
- different strategy for mach-o files to lookup symbol names, in order to be consistent with other functions (that use dladdr() and dlsym() calls, which mostly don't use '_' prefixes)
Tassilo Philipp
parents:
223
diff
changeset
|
195 /* calls as used in dlFindAddress and dlSymsNameFromValue - the "#if 0"-ed code below returns the */ |
6e40eb9fb261
- different strategy for mach-o files to lookup symbol names, in order to be consistent with other functions (that use dladdr() and dlsym() calls, which mostly don't use '_' prefixes)
Tassilo Philipp
parents:
223
diff
changeset
|
196 /* name directly, but assumes wrongly that everything is prefixed with an underscore on Darwin. */ |
6e40eb9fb261
- different strategy for mach-o files to lookup symbol names, in order to be consistent with other functions (that use dladdr() and dlsym() calls, which mostly don't use '_' prefixes)
Tassilo Philipp
parents:
223
diff
changeset
|
197 |
6e40eb9fb261
- different strategy for mach-o files to lookup symbol names, in order to be consistent with other functions (that use dladdr() and dlsym() calls, which mostly don't use '_' prefixes)
Tassilo Philipp
parents:
223
diff
changeset
|
198 /* only handle symbols that are in a section and aren't symbolic debug entries */ |
6e40eb9fb261
- different strategy for mach-o files to lookup symbol names, in order to be consistent with other functions (that use dladdr() and dlsym() calls, which mostly don't use '_' prefixes)
Tassilo Philipp
parents:
223
diff
changeset
|
199 if((t == N_SECT) && (nl->n_type & N_STAB) == 0) |
6e40eb9fb261
- different strategy for mach-o files to lookup symbol names, in order to be consistent with other functions (that use dladdr() and dlsym() calls, which mostly don't use '_' prefixes)
Tassilo Philipp
parents:
223
diff
changeset
|
200 return dlSymsNameFromValue(pSyms, (void*)(nl->n_value + pSyms->symOffset)); |
6e40eb9fb261
- different strategy for mach-o files to lookup symbol names, in order to be consistent with other functions (that use dladdr() and dlsym() calls, which mostly don't use '_' prefixes)
Tassilo Philipp
parents:
223
diff
changeset
|
201 |
6e40eb9fb261
- different strategy for mach-o files to lookup symbol names, in order to be consistent with other functions (that use dladdr() and dlsym() calls, which mostly don't use '_' prefixes)
Tassilo Philipp
parents:
223
diff
changeset
|
202 return NULL; /* @@@ handle N_INDR, etc.? */ |
6e40eb9fb261
- different strategy for mach-o files to lookup symbol names, in order to be consistent with other functions (that use dladdr() and dlsym() calls, which mostly don't use '_' prefixes)
Tassilo Philipp
parents:
223
diff
changeset
|
203 |
6e40eb9fb261
- different strategy for mach-o files to lookup symbol names, in order to be consistent with other functions (that use dladdr() and dlsym() calls, which mostly don't use '_' prefixes)
Tassilo Philipp
parents:
223
diff
changeset
|
204 #if 0 |
220
94dc0bdd7dbe
- make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents:
219
diff
changeset
|
205 /* Mach-O manual: Symbols with an index into the string table of zero */ |
94dc0bdd7dbe
- make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents:
219
diff
changeset
|
206 /* (n_un.n_strx == 0) are defined to have a null ("") name. */ |
219 | 207 if(nl->n_un.n_strx == 0) |
220
94dc0bdd7dbe
- make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents:
219
diff
changeset
|
208 return NULL; /*@@@ have return pointer to some static "" string? */ |
219 | 209 |
220
94dc0bdd7dbe
- make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents:
219
diff
changeset
|
210 /* Skip undefined symbols. @@@ should we? */ |
94dc0bdd7dbe
- make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents:
219
diff
changeset
|
211 if(t == N_UNDF || t == N_PBUD) /* @@@ check if N_PBUD is defined, it's not in the NeXT manual, but on Darwin 8.0.1 */ |
219 | 212 return NULL; |
217 | 213 |
220
94dc0bdd7dbe
- make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents:
219
diff
changeset
|
214 /*TODO skip more symbols based on nl->n_desc and nl->n_type ? */ |
0 | 215 |
220
94dc0bdd7dbe
- make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents:
219
diff
changeset
|
216 /* Return name - handles lookup of indirect names. */ |
94dc0bdd7dbe
- make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents:
219
diff
changeset
|
217 return &pSyms->pStringTable[(t == N_INDR ? nl->n_value : nl->n_un.n_strx) |
94dc0bdd7dbe
- make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents:
219
diff
changeset
|
218 #if defined(OS_Darwin) |
94dc0bdd7dbe
- make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents:
219
diff
changeset
|
219 + 1 /* Skip '_'-prefix */ |
94dc0bdd7dbe
- make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents:
219
diff
changeset
|
220 #endif |
94dc0bdd7dbe
- make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents:
219
diff
changeset
|
221 ]; |
226
6e40eb9fb261
- different strategy for mach-o files to lookup symbol names, in order to be consistent with other functions (that use dladdr() and dlsym() calls, which mostly don't use '_' prefixes)
Tassilo Philipp
parents:
223
diff
changeset
|
222 #endif |
0 | 223 } |
224 | |
225 | |
226 const char* dlSymsNameFromValue(DLSyms* pSyms, void* value) | |
227 { | |
228 Dl_info info; | |
229 if (!dladdr(value, &info) || (value != info.dli_saddr)) | |
230 return NULL; | |
217 | 231 |
0 | 232 return info.dli_sname; |
233 } | |
217 | 234 |