annotate dynload/dynload_syms_mach-o.c @ 220:94dc0bdd7dbe

- make dynload symbol iteration return symbol names as in C code, on Darwin - use C comments
author Tassilo Philipp
date Tue, 11 Apr 2017 15:13:32 +0200
parents 912efa3cfd4b
children 7076f551faf5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
1 /*
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
2
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
3 Package: dyncall
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
4 Library: dynload
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
5 File: dynload/dynload_syms_mach-o.c
219
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
6 Description:
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
7 License:
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
8
217
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
9 Copyright (c) 2007-2015 Olivier Chafik <olivier.chafik@gmail.com>,
219
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
10 2017 refactored completely for stability, API
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
11 consistency and portability by Tassilo Philipp.
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
12
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
13 Permission to use, copy, modify, and distribute this software for any
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
14 purpose with or without fee is hereby granted, provided that the above
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
15 copyright notice and this permission notice appear in all copies.
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
16
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
17 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
18 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
19 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
20 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
21 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
22 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
23 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
24
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
25 */
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
26
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
27
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
28
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
29 /*
219
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
30
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
31 dynamic symbol resolver for Mach-O
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
32
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
33 */
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
34
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
35 #include "dynload.h"
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
36 #include "dynload_alloc.h"
217
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
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
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
39
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
40 #include <mach-o/dyld.h>
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
41 #include <mach-o/nlist.h>
217
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
42 #include <sys/stat.h>
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
43 #include <dlfcn.h>
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
44 #include <string.h>
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
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
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
47 #define MACH_HEADER_TYPE mach_header_64
219
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
48 #define SEGMENT_COMMAND segment_command_64
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
49 #define NLIST_TYPE nlist_64
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
50 #else
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
51 #define MACH_HEADER_TYPE mach_header
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
52 #define NLIST_TYPE nlist
219
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
53 #define SEGMENT_COMMAND segment_command
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
54 #endif
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
55
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
56
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
57 struct DLSyms_
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
58 {
217
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
59 DLLib* pLib;
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
60 const char* pStringTable;
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
61 const struct NLIST_TYPE* pSymbolTable;
217
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
62 uint32_t symbolCount;
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
63 };
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
64
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
65
217
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
66 DLSyms* dlSymsInit(const char* libPath)
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
67 {
217
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
68 DLLib* pLib;
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
69 DLSyms* pSyms;
219
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
70 uint32_t i, n;
217
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
71 struct stat st0;
219
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
72 const struct MACH_HEADER_TYPE* pHeader = NULL;
217
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
73
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
74 if(stat(libPath, &st0) == -1)
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
75 return NULL;
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
76
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
77 pLib = dlLoadLibrary(libPath);
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
78 if(!pLib)
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
79 return NULL;
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
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
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
82 for(i = 0, n = _dyld_image_count(); i < n; ++i)
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
83 {
217
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
84 struct stat st1;
219
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
85 const char* name = _dyld_get_image_name(i);
217
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
86
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
87 if(name && (stat(name, &st1) != -1))
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
88 {
220
94dc0bdd7dbe - make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents: 219
diff changeset
89 /* 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
90 /* cased, etc., but compare inode number with the one of the mapped dyld image. */
219
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
91 if(st0.st_ino == st1.st_ino/*!strcmp(name, libPath)*/)
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
92 {
219
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
93 pHeader = (const struct MACH_HEADER_TYPE*) _dyld_get_image_header(i);
220
94dc0bdd7dbe - make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents: 219
diff changeset
94 break; /* found header */
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
95 }
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
96 }
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
97 }
219
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
98
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
99 if(pHeader && (pHeader->filetype == MH_DYLIB) && !(pHeader->flags & MH_SPLIT_SEGS))
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
100 {
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
101 const char* pBase = (const char*)pHeader;
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
102 uintptr_t slide = 0;
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
103 const struct load_command* cmd = (const struct load_command*)(pBase + sizeof(struct MACH_HEADER_TYPE));
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
104
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
105 for(i = 0, n = pHeader->ncmds; i < n; ++i, cmd = (const struct load_command*)((const char*)cmd + cmd->cmdsize))
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
106 {
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
107 if(cmd->cmd == LC_SEGMENT)
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
108 {
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
109 const struct SEGMENT_COMMAND* seg = (struct SEGMENT_COMMAND*)cmd;
220
94dc0bdd7dbe - make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents: 219
diff changeset
110 if((seg->fileoff == 0) && (seg->filesize != 0)) /* Count segment sizes to slide over...@@@? */
219
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
111 slide = (uintptr_t)pHeader - seg->vmaddr;
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
112 if(strcmp(seg->segname, "__LINKEDIT") == 0)
220
94dc0bdd7dbe - make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents: 219
diff changeset
113 pBase = (const char*)(seg->vmaddr - seg->fileoff + slide); /* Adjust pBase depending on __LINKEDIT segment */
219
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
114 }
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
115 else if(cmd->cmd == LC_SYMTAB)
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
116 {
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
117 const struct symtab_command* scmd = (const struct symtab_command*)cmd;
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
118
220
94dc0bdd7dbe - make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents: 219
diff changeset
119 /* cmd->cmdsize must be size of struct, otherwise something is off; abort */
219
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
120 if(cmd->cmdsize != sizeof(struct symtab_command))
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
121 break;
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
122
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
123 pSyms = (DLSyms*)dlAllocMem(sizeof(DLSyms));
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
124 pSyms->symbolCount = scmd->nsyms;
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
125 pSyms->pStringTable = pBase + scmd->stroff;
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
126 pSyms->pSymbolTable = (struct NLIST_TYPE*)(pBase + scmd->symoff);
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
127 pSyms->pLib = pLib;
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
128
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
129 return pSyms;
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
130 }
220
94dc0bdd7dbe - make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents: 219
diff changeset
131 /*@@@ handle also LC_DYSYMTAB */
219
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
132 }
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
133 }
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
134
220
94dc0bdd7dbe - make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents: 219
diff changeset
135 /* Couldn't init syms, so free lib and return error. */
219
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
136 dlFreeLibrary(pLib);
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
137 return NULL;
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
138 }
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
139
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
140
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
141 void dlSymsCleanup(DLSyms* pSyms)
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
142 {
217
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
143 if(pSyms) {
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
144 dlFreeLibrary(pSyms->pLib);
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
145 dlFreeMem(pSyms);
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
146 }
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
147 }
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
148
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
149 int dlSymsCount(DLSyms* pSyms)
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
150 {
217
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
151 return pSyms ? pSyms->symbolCount : 0;
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
152 }
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
153
219
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
154
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
155 const char* dlSymsName(DLSyms* pSyms, int index)
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
156 {
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
157 const struct NLIST_TYPE* nl;
219
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
158 unsigned char t;
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
159
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
160 if(!pSyms)
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
161 return NULL;
217
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
162
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
163 nl = pSyms->pSymbolTable + index;
219
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
164
220
94dc0bdd7dbe - make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents: 219
diff changeset
165 /* 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
166 /* (n_un.n_strx == 0) are defined to have a null ("") name. */
219
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
167 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
168 return NULL; /*@@@ have return pointer to some static "" string? */
219
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
169
220
94dc0bdd7dbe - make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents: 219
diff changeset
170 /* Skip undefined symbols. @@@ should we? */
219
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
171 t = nl->n_type & N_TYPE;
220
94dc0bdd7dbe - make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents: 219
diff changeset
172 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
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
173 return NULL;
217
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
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 /*TODO skip more symbols based on nl->n_desc and nl->n_type ? */
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
176
220
94dc0bdd7dbe - make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents: 219
diff changeset
177 /* 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
178 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
179 #if defined(OS_Darwin)
94dc0bdd7dbe - make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents: 219
diff changeset
180 + 1 /* Skip '_'-prefix */
94dc0bdd7dbe - make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents: 219
diff changeset
181 #endif
94dc0bdd7dbe - make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents: 219
diff changeset
182 ];
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
183 }
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
184
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
185
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
186 const char* dlSymsNameFromValue(DLSyms* pSyms, void* value)
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
187 {
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
188 Dl_info info;
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
189 if (!dladdr(value, &info) || (value != info.dli_saddr))
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
190 return NULL;
217
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
191
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
192 return info.dli_sname;
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
193 }
217
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
194