annotate dynload/dynload_syms_mach-o.c @ 357:d982a00c2177

- PPC64 asm syntax fix, specifying explicitly comparison mode for cmpi (newer toolchains complain, older ones took optional field of instruction which happened to be same value)
author Tassilo Philipp
date Tue, 25 Feb 2020 18:16:13 +0100
parents 7ed642a9c3e9
children dd5d03483314
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>,
275
1e3617d8a951 - mach-o loader fix (discovered on High Sierra, not sure what other versions were affected)
Tassilo Philipp
parents: 242
diff changeset
10 2017-2018 refactored completely for stability, API
219
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
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
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
50 #define SEGMENT_COMMAND segment_command_64
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
51 #define NLIST_TYPE nlist_64
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
52 #else
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
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
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
57 #define NLIST_TYPE nlist
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
58 #endif
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
59
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
60
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
61 struct DLSyms_
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
62 {
217
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
63 DLLib* pLib;
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
64 const char* pStringTable;
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
65 const struct NLIST_TYPE* pSymbolTable;
217
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
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
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
68 };
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
69
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
70
217
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
71 DLSyms* dlSymsInit(const char* libPath)
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
72 {
217
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
73 DLLib* pLib;
223
7076f551faf5 - dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents: 220
diff changeset
74 DLSyms* pSyms = NULL;
219
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
75 uint32_t i, n;
217
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
76 struct stat st0;
219
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
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
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
79
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
80 if(stat(libPath, &st0) == -1)
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
81 return NULL;
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
82
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
83 pLib = dlLoadLibrary(libPath);
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
84 if(!pLib)
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
85 return NULL;
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
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
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
88 for(i = 0, n = _dyld_image_count(); i < n; ++i)
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
89 {
217
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
90 struct stat st1;
219
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
91 const char* name = _dyld_get_image_name(i);
217
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
92
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
93 if(name && (stat(name, &st1) != -1))
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
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
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
97 if(st0.st_ino == st1.st_ino/*!strcmp(name, libPath)*/)
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
98 {
219
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
99 pHeader = (const struct MACH_HEADER_TYPE*) _dyld_get_image_header(i);
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 226
diff changeset
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
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
102 }
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
103 }
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
104 }
219
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
105
282
7ed642a9c3e9 - removed some restriction in mach-o loader
Tassilo Philipp
parents: 275
diff changeset
106 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
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
107 {
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
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
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
110 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
111
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
112 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
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
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
115 {
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
116 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
117 /*@@@ 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
118 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
119 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
120
275
1e3617d8a951 - mach-o loader fix (discovered on High Sierra, not sure what other versions were affected)
Tassilo Philipp
parents: 242
diff changeset
121 /* 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
122 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
123 /* 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
124 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
125
1e3617d8a951 - mach-o loader fix (discovered on High Sierra, not sure what other versions were affected)
Tassilo Philipp
parents: 242
diff changeset
126 /*@@@ 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
127 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
128 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
129 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
130
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
131 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
132 }
219
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
133 }
223
7076f551faf5 - dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents: 220
diff changeset
134 else if(cmd->cmd == LC_SYMTAB && !pSyms/* only init once - just safety check */)
219
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
135 {
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
136 const struct symtab_command* scmd = (const struct symtab_command*)cmd;
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
137
220
94dc0bdd7dbe - make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents: 219
diff changeset
138 /* 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
139 if(cmd->cmdsize != sizeof(struct symtab_command))
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
140 break;
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
141
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
142 pSyms = (DLSyms*)dlAllocMem(sizeof(DLSyms));
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
143 pSyms->symbolCount = scmd->nsyms;
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
144 pSyms->pStringTable = pBase + scmd->stroff;
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
145 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
146 pSyms->symOffset = symOffset;
219
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
147 pSyms->pLib = pLib;
223
7076f551faf5 - dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents: 220
diff changeset
148 }
275
1e3617d8a951 - mach-o loader fix (discovered on High Sierra, not sure what other versions were affected)
Tassilo Philipp
parents: 242
diff changeset
149 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
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 /* @@@ 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
152 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
153 dlSymsCleanup(pSyms);
223
7076f551faf5 - dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents: 220
diff changeset
154 break;
275
1e3617d8a951 - mach-o loader fix (discovered on High Sierra, not sure what other versions were affected)
Tassilo Philipp
parents: 242
diff changeset
155 }
1e3617d8a951 - mach-o loader fix (discovered on High Sierra, not sure what other versions were affected)
Tassilo Philipp
parents: 242
diff changeset
156
1e3617d8a951 - mach-o loader fix (discovered on High Sierra, not sure what other versions were affected)
Tassilo Philipp
parents: 242
diff changeset
157 dysymtab_cmd = (const struct dysymtab_command*)cmd;*/
219
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
158 }
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 }
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
161
223
7076f551faf5 - dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents: 220
diff changeset
162 /* Got symbol table? */
7076f551faf5 - dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents: 220
diff changeset
163 if(pSyms) {
7076f551faf5 - dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents: 220
diff changeset
164 /* 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
165 /* Only use local ones in that case. */
7076f551faf5 - dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents: 220
diff changeset
166 /*@@@ 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
167 pSyms->pSymbolTable += dysymtab_cmd->ilocalsym;
7076f551faf5 - dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents: 220
diff changeset
168 pSyms->symbolCount = dysymtab_cmd->nlocalsym;
7076f551faf5 - dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents: 220
diff changeset
169 }*/
7076f551faf5 - dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents: 220
diff changeset
170 return pSyms;
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
220
94dc0bdd7dbe - make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents: 219
diff changeset
173 /* Couldn't init syms, so free lib and return error. */
219
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
174 dlFreeLibrary(pLib);
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
175 return NULL;
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
176 }
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
177
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
178
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
179 void dlSymsCleanup(DLSyms* pSyms)
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
180 {
217
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
181 if(pSyms) {
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
182 dlFreeLibrary(pSyms->pLib);
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
183 dlFreeMem(pSyms);
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
184 }
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
185 }
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
186
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
187 int dlSymsCount(DLSyms* pSyms)
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
188 {
217
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
189 return pSyms ? pSyms->symbolCount : 0;
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
190 }
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
191
219
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
192
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
193 const char* dlSymsName(DLSyms* pSyms, int index)
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
194 {
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
195 const struct NLIST_TYPE* nl;
219
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
196 unsigned char t;
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
197
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
198 if(!pSyms)
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
199 return NULL;
217
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
200
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
201 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
202 t = nl->n_type & N_TYPE;
219
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
203
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 /* 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
205 /* 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
206 /* 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
207
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 /* 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
209 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
210 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
211
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 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
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 #if 0
220
94dc0bdd7dbe - make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents: 219
diff changeset
215 /* 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
216 /* (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
217 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
218 return NULL; /*@@@ have return pointer to some static "" string? */
219
912efa3cfd4b - mach-o symbol iteration stability fixes
Tassilo Philipp
parents: 217
diff changeset
219
220
94dc0bdd7dbe - make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents: 219
diff changeset
220 /* 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
221 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
222 return NULL;
217
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
223
220
94dc0bdd7dbe - make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents: 219
diff changeset
224 /*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
225
220
94dc0bdd7dbe - make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents: 219
diff changeset
226 /* 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
227 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
228 #if defined(OS_Darwin)
94dc0bdd7dbe - make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents: 219
diff changeset
229 + 1 /* Skip '_'-prefix */
94dc0bdd7dbe - make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents: 219
diff changeset
230 #endif
94dc0bdd7dbe - make dynload symbol iteration return symbol names as in C code, on Darwin
Tassilo Philipp
parents: 219
diff changeset
231 ];
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
232 #endif
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
233 }
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
234
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
235
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
236 const char* dlSymsNameFromValue(DLSyms* pSyms, void* value)
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
237 {
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
238 Dl_info info;
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
239 if (!dladdr(value, &info) || (value != info.dli_saddr))
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
240 return NULL;
217
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
241
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
242 return info.dli_sname;
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
243 }
217
87c695673522 dynload darwin/mach-o update:
Tassilo Philipp
parents: 0
diff changeset
244