Mercurial > pub > dyncall > dyncall
annotate dynload/dynload_syms_mach-o.c @ 645:6a8aac9b2bcf
- removal of `autovar` macro detection lib:
* was a good idea, but never fully pushed through, so code was duplicating most of dyncall_macros.h
* didn't find any outside use and plans to make it a standalone lib never materialized
* so basically removing code to avoid code duplication that risks to never stay in sync
author | Tassilo Philipp |
---|---|
date | Mon, 05 Feb 2024 16:43:59 +0100 |
parents | 61c485f8cc06 |
children |
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>, |
384 | 10 2017-2021 refactored completely for stability, API |
219 | 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" | |
645
6a8aac9b2bcf
- removal of `autovar` macro detection lib:
Tassilo Philipp
parents:
552
diff
changeset
|
37 #include "../dyncall/dyncall_macros.h" |
0 | 38 |
39 #include <mach-o/dyld.h> | |
40 #include <mach-o/nlist.h> | |
41 #include <dlfcn.h> | |
42 #include <string.h> | |
43 | |
645
6a8aac9b2bcf
- removal of `autovar` macro detection lib:
Tassilo Philipp
parents:
552
diff
changeset
|
44 #if defined(DC__Arch_AMD64) || defined(DC__Arch_PPC64) || defined(DC__Arch_ARM64) |
0 | 45 #define MACH_HEADER_TYPE mach_header_64 |
223
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
46 #define MACH_HEADER_MAGIC_NR MH_MAGIC_64 |
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
47 #define SEGMEND_COMMAND_ID LC_SEGMENT_64 |
219 | 48 #define SEGMENT_COMMAND segment_command_64 |
0 | 49 #define NLIST_TYPE nlist_64 |
50 #else | |
51 #define MACH_HEADER_TYPE mach_header | |
223
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
52 #define MACH_HEADER_MAGIC_NR MH_MAGIC |
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
53 #define SEGMEND_COMMAND_ID LC_SEGMENT |
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
54 #define SEGMENT_COMMAND segment_command |
0 | 55 #define NLIST_TYPE nlist |
56 #endif | |
57 | |
58 | |
59 struct DLSyms_ | |
60 { | |
217 | 61 DLLib* pLib; |
62 const char* pStringTable; | |
0 | 63 const struct NLIST_TYPE* pSymbolTable; |
217 | 64 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
|
65 uintptr_t symOffset; |
0 | 66 }; |
67 | |
68 | |
217 | 69 DLSyms* dlSymsInit(const char* libPath) |
0 | 70 { |
217 | 71 DLLib* pLib; |
223
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
72 DLSyms* pSyms = NULL; |
219 | 73 uint32_t i, n; |
74 const struct MACH_HEADER_TYPE* pHeader = NULL; | |
223
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
75 const struct dysymtab_command* dysymtab_cmd = NULL; |
217 | 76 |
77 pLib = dlLoadLibrary(libPath); | |
78 if(!pLib) | |
79 return NULL; | |
80 | |
220
94dc0bdd7dbe
- make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents:
219
diff
changeset
|
81 /* Loop over all dynamically linked images to find ours. */ |
219 | 82 for(i = 0, n = _dyld_image_count(); i < n; ++i) |
0 | 83 { |
219 | 84 const char* name = _dyld_get_image_name(i); |
217 | 85 |
382
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
282
diff
changeset
|
86 if(name) |
0 | 87 { |
220
94dc0bdd7dbe
- make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents:
219
diff
changeset
|
88 /* Don't rely on name comparison alone, as libPath might be relative, symlink, differently */ |
382
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
282
diff
changeset
|
89 /* cased, use weird osx path placeholders, etc., but compare inode number with the one of the mapped dyld image. */ |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
282
diff
changeset
|
90 |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
282
diff
changeset
|
91 /* reload already loaded lib to get handle to compare with, should be lightweight and only increase ref count */ |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
282
diff
changeset
|
92 DLLib* pLib_ = dlLoadLibrary(name); |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
282
diff
changeset
|
93 if(pLib_) |
0 | 94 { |
382
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
282
diff
changeset
|
95 /* free / refcount-- */ |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
282
diff
changeset
|
96 dlFreeLibrary(pLib_); |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
282
diff
changeset
|
97 |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
282
diff
changeset
|
98 if(pLib == pLib_) |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
282
diff
changeset
|
99 { |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
282
diff
changeset
|
100 pHeader = (const struct MACH_HEADER_TYPE*) _dyld_get_image_header(i); |
552 | 101 /*@@@ slide = _dyld_get_image_vmaddr_slide(i);*/ |
382
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
282
diff
changeset
|
102 break; /* found header */ |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
282
diff
changeset
|
103 } |
0 | 104 } |
105 } | |
106 } | |
219 | 107 |
282
7ed642a9c3e9
- removed some restriction in mach-o loader
Tassilo Philipp
parents:
275
diff
changeset
|
108 if(pHeader && (pHeader->magic == MACH_HEADER_MAGIC_NR) && (pHeader->filetype == MH_DYLIB)/*@@@ ignore for now, seems to work without it on El Capitan && !(pHeader->flags & MH_SPLIT_SEGS)*/) |
219 | 109 { |
110 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
|
111 uintptr_t slide = 0, symOffset = 0; |
219 | 112 const struct load_command* cmd = (const struct load_command*)(pBase + sizeof(struct MACH_HEADER_TYPE)); |
113 | |
114 for(i = 0, n = pHeader->ncmds; i < n; ++i, cmd = (const struct load_command*)((const char*)cmd + cmd->cmdsize)) | |
115 { | |
223
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
116 if(cmd->cmd == SEGMEND_COMMAND_ID) |
219 | 117 { |
118 const struct SEGMENT_COMMAND* seg = (struct SEGMENT_COMMAND*)cmd; | |
275
1e3617d8a951
- mach-o loader fix (discovered on High Sierra, not sure what other versions were affected)
Tassilo Philipp
parents:
242
diff
changeset
|
119 /*@@@ unsure why I used this instead of checking __TEXT: if((seg->fileoff == 0) && (seg->filesize != 0))*/ |
1e3617d8a951
- mach-o loader fix (discovered on High Sierra, not sure what other versions were affected)
Tassilo Philipp
parents:
242
diff
changeset
|
120 if(strcmp(seg->segname, "__TEXT") == 0) |
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
|
121 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
|
122 |
275
1e3617d8a951
- mach-o loader fix (discovered on High Sierra, not sure what other versions were affected)
Tassilo Philipp
parents:
242
diff
changeset
|
123 /* If we have __LINKEDIT segment (= raw data for dynamic linkers), use that one to find symbal table address. */ |
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
|
124 if(strcmp(seg->segname, "__LINKEDIT") == 0) { |
275
1e3617d8a951
- mach-o loader fix (discovered on High Sierra, not sure what other versions were affected)
Tassilo Philipp
parents:
242
diff
changeset
|
125 /* Recompute pBase relative to where __LINKEDIT segment is in memory. */ |
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
|
126 pBase = (const char*)(seg->vmaddr - seg->fileoff) + slide; |
275
1e3617d8a951
- mach-o loader fix (discovered on High Sierra, not sure what other versions were affected)
Tassilo Philipp
parents:
242
diff
changeset
|
127 |
1e3617d8a951
- mach-o loader fix (discovered on High Sierra, not sure what other versions were affected)
Tassilo Philipp
parents:
242
diff
changeset
|
128 /*@@@ we might want to also check maxprot and initprot here: |
1e3617d8a951
- mach-o loader fix (discovered on High Sierra, not sure what other versions were affected)
Tassilo Philipp
parents:
242
diff
changeset
|
129 VM_PROT_READ ((vm_prot_t) 0x01) |
1e3617d8a951
- mach-o loader fix (discovered on High Sierra, not sure what other versions were affected)
Tassilo Philipp
parents:
242
diff
changeset
|
130 VM_PROT_WRITE ((vm_prot_t) 0x02) |
1e3617d8a951
- mach-o loader fix (discovered on High Sierra, not sure what other versions were affected)
Tassilo Philipp
parents:
242
diff
changeset
|
131 VM_PROT_EXECUTE ((vm_prot_t) 0x04)*/ |
1e3617d8a951
- mach-o loader fix (discovered on High Sierra, not sure what other versions were affected)
Tassilo Philipp
parents:
242
diff
changeset
|
132 |
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
|
133 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
|
134 } |
219 | 135 } |
223
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
136 else if(cmd->cmd == LC_SYMTAB && !pSyms/* only init once - just safety check */) |
219 | 137 { |
138 const struct symtab_command* scmd = (const struct symtab_command*)cmd; | |
139 | |
220
94dc0bdd7dbe
- make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents:
219
diff
changeset
|
140 /* cmd->cmdsize must be size of struct, otherwise something is off; abort */ |
219 | 141 if(cmd->cmdsize != sizeof(struct symtab_command)) |
142 break; | |
143 | |
144 pSyms = (DLSyms*)dlAllocMem(sizeof(DLSyms)); | |
145 pSyms->symbolCount = scmd->nsyms; | |
146 pSyms->pStringTable = pBase + scmd->stroff; | |
147 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
|
148 pSyms->symOffset = symOffset; |
219 | 149 pSyms->pLib = pLib; |
223
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
150 } |
275
1e3617d8a951
- mach-o loader fix (discovered on High Sierra, not sure what other versions were affected)
Tassilo Philipp
parents:
242
diff
changeset
|
151 else if(cmd->cmd == LC_DYSYMTAB && !dysymtab_cmd/* only init once - just safety check */) |
223
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
152 { |
275
1e3617d8a951
- mach-o loader fix (discovered on High Sierra, not sure what other versions were affected)
Tassilo Philipp
parents:
242
diff
changeset
|
153 /* @@@ unused, we'll always run over all symbols, and not check locals, globals, etc. |
1e3617d8a951
- mach-o loader fix (discovered on High Sierra, not sure what other versions were affected)
Tassilo Philipp
parents:
242
diff
changeset
|
154 if(cmd->cmdsize != sizeof(struct symtab_command)) { |
1e3617d8a951
- mach-o loader fix (discovered on High Sierra, not sure what other versions were affected)
Tassilo Philipp
parents:
242
diff
changeset
|
155 dlSymsCleanup(pSyms); |
223
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
156 break; |
275
1e3617d8a951
- mach-o loader fix (discovered on High Sierra, not sure what other versions were affected)
Tassilo Philipp
parents:
242
diff
changeset
|
157 } |
1e3617d8a951
- mach-o loader fix (discovered on High Sierra, not sure what other versions were affected)
Tassilo Philipp
parents:
242
diff
changeset
|
158 |
1e3617d8a951
- mach-o loader fix (discovered on High Sierra, not sure what other versions were affected)
Tassilo Philipp
parents:
242
diff
changeset
|
159 dysymtab_cmd = (const struct dysymtab_command*)cmd;*/ |
219 | 160 } |
161 } | |
162 } | |
163 | |
223
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
164 /* Got symbol table? */ |
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
165 if(pSyms) { |
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
166 /* 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
|
167 /* Only use local ones in that case. */ |
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
168 /*@@@ 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
|
169 pSyms->pSymbolTable += dysymtab_cmd->ilocalsym; |
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
170 pSyms->symbolCount = dysymtab_cmd->nlocalsym; |
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
171 }*/ |
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
172 return pSyms; |
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
173 } |
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
220
diff
changeset
|
174 |
220
94dc0bdd7dbe
- make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents:
219
diff
changeset
|
175 /* Couldn't init syms, so free lib and return error. */ |
219 | 176 dlFreeLibrary(pLib); |
0 | 177 return NULL; |
178 } | |
179 | |
180 | |
181 void dlSymsCleanup(DLSyms* pSyms) | |
182 { | |
217 | 183 if(pSyms) { |
184 dlFreeLibrary(pSyms->pLib); | |
185 dlFreeMem(pSyms); | |
186 } | |
0 | 187 } |
188 | |
189 int dlSymsCount(DLSyms* pSyms) | |
190 { | |
217 | 191 return pSyms ? pSyms->symbolCount : 0; |
0 | 192 } |
193 | |
219 | 194 |
195 const char* dlSymsName(DLSyms* pSyms, int index) | |
0 | 196 { |
197 const struct NLIST_TYPE* nl; | |
219 | 198 unsigned char t; |
199 | |
200 if(!pSyms) | |
0 | 201 return NULL; |
217 | 202 |
0 | 203 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
|
204 t = nl->n_type & N_TYPE; |
219 | 205 |
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
|
206 /* 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
|
207 /* 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
|
208 /* 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
|
209 |
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
|
210 /* 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
|
211 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
|
212 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
|
213 |
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
|
214 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
|
215 |
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
|
216 #if 0 |
220
94dc0bdd7dbe
- make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents:
219
diff
changeset
|
217 /* 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
|
218 /* (n_un.n_strx == 0) are defined to have a null ("") name. */ |
219 | 219 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
|
220 return NULL; /*@@@ have return pointer to some static "" string? */ |
219 | 221 |
220
94dc0bdd7dbe
- make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents:
219
diff
changeset
|
222 /* 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
|
223 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 | 224 return NULL; |
217 | 225 |
220
94dc0bdd7dbe
- make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents:
219
diff
changeset
|
226 /*TODO skip more symbols based on nl->n_desc and nl->n_type ? */ |
0 | 227 |
220
94dc0bdd7dbe
- make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents:
219
diff
changeset
|
228 /* 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
|
229 return &pSyms->pStringTable[(t == N_INDR ? nl->n_value : nl->n_un.n_strx) |
645
6a8aac9b2bcf
- removal of `autovar` macro detection lib:
Tassilo Philipp
parents:
552
diff
changeset
|
230 #if defined(DC__OS_Darwin) |
220
94dc0bdd7dbe
- make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents:
219
diff
changeset
|
231 + 1 /* Skip '_'-prefix */ |
94dc0bdd7dbe
- make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents:
219
diff
changeset
|
232 #endif |
94dc0bdd7dbe
- make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents:
219
diff
changeset
|
233 ]; |
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
|
234 #endif |
0 | 235 } |
236 | |
237 | |
238 const char* dlSymsNameFromValue(DLSyms* pSyms, void* value) | |
239 { | |
240 Dl_info info; | |
241 if (!dladdr(value, &info) || (value != info.dli_saddr)) | |
242 return NULL; | |
217 | 243 |
0 | 244 return info.dli_sname; |
245 } | |
217 | 246 |