Skip to content

Commit 373c034

Browse files
committed
Prefer using LC_SYMTAB over LC_DYLD_INFO / LC_DYLD_INFO_ONLY in macho parser
1 parent 12176ca commit 373c034

File tree

2 files changed

+65
-48
lines changed

2 files changed

+65
-48
lines changed

src/main/cpp/bridj/dyncall.diff

Lines changed: 65 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
diff -r bbefb8b8e74c dynload/dynload_syms_mach-o.c
22
--- a/dynload/dynload_syms_mach-o.c Thu Nov 24 23:47:31 2016 +0000
3-
+++ b/dynload/dynload_syms_mach-o.c Sun Dec 25 20:36:56 2016 +0100
3+
+++ b/dynload/dynload_syms_mach-o.c Thu Jan 05 09:36:39 2017 +0000
44
@@ -29,6 +29,7 @@
55
dynamic symbol resolver for Mach-O
66

@@ -27,7 +27,7 @@ diff -r bbefb8b8e74c dynload/dynload_syms_mach-o.c
2727
struct DLLib_
2828
{
2929
char* libPath;
30-
@@ -58,11 +60,139 @@
30+
@@ -58,11 +60,143 @@
3131

3232
struct DLSyms_
3333
{
@@ -37,6 +37,10 @@ diff -r bbefb8b8e74c dynload/dynload_syms_mach-o.c
3737
uint32_t symbolCount;
3838
};
3939

40+
+const struct load_command* get_next_command(const struct load_command* cmd) {
41+
+ return (const struct load_command*)(((char*)cmd) + cmd->cmdsize);
42+
+}
43+
+
4044
+int isSameMacImageName(const char* libPath, const char* systemLibPath) {
4145
+ if (!libPath || !systemLibPath)
4246
+ return 0;
@@ -167,7 +171,7 @@ diff -r bbefb8b8e74c dynload/dynload_syms_mach-o.c
167171

168172
DLSyms* dlSymsInit(const char* libPath)
169173
{
170-
@@ -71,27 +201,61 @@
174+
@@ -71,37 +205,71 @@
171175
for (iImage = 0, nImages = _dyld_image_count(); iImage < nImages; iImage++)
172176
{
173177
const char* name = _dyld_get_image_name(iImage);
@@ -193,57 +197,70 @@ diff -r bbefb8b8e74c dynload/dynload_syms_mach-o.c
193197
uint32_t iCmd, nCmds = pHeader->ncmds;
194198
const struct load_command* cmd = (const struct load_command*)(pBase + sizeof(struct MACH_HEADER_TYPE));
195199

200+
+ // First, try and find a LC_SYMTAB
201+
+ for (iCmd = 0; iCmd < nCmds; iCmd++)
202+
+ {
203+
+ if (cmd->cmd == LC_SYMTAB)
204+
+ {
205+
+ const struct symtab_command* scmd = (const struct symtab_command*)cmd;
206+
+
207+
+ pSyms = (DLSyms*)( dlAllocMem(sizeof(DLSyms)) );
208+
+ //memset(pSyms, sizeof(DLSyms), 0);
209+
+ pSyms->decompressedSymbols = NULL;
210+
+ pSyms->symbolCount = scmd->nsyms;
211+
+ pSyms->pStringTable = pBase + scmd->stroff;
212+
+ pSyms->pSymbolTable = (struct NLIST_TYPE*)(pBase + scmd->symoff);
213+
+
214+
+ return pSyms;
215+
+ }
216+
+ cmd = get_next_command(cmd);
217+
+ }
218+
+
219+
+ // Then, try and use LC_DYLD_INFO_ONLY or LC_DYLD_INFO and parse their trie.
196220
for (iCmd = 0; iCmd < nCmds; iCmd++)
197221
{
198222
- if (cmd->cmd == LC_SYMTAB)
199223
- {
200-
+ if (!cmd) continue;
201-
+
224+
- const struct symtab_command* scmd = (const struct symtab_command*)cmd;
225+
-
226+
- pSyms = (DLSyms*)( dlAllocMem(sizeof(DLSyms)) );
227+
- pSyms->symbolCount = scmd->nsyms;
228+
- pSyms->pStringTable = pBase + scmd->stroff;
229+
- pSyms->pSymbolTable = (struct NLIST_TYPE*)(pBase + scmd->symoff);
230+
-
231+
- return pSyms;
232+
- }
233+
- cmd = (const struct load_command*)(((char*)cmd) + cmd->cmdsize);
202234
+ if (cmd->cmd == LC_DYLD_INFO_ONLY || cmd->cmd == LC_DYLD_INFO) {
203-
+ const struct dyld_info_command* dcmd = (const struct dyld_info_command*)cmd;
204-
+ const unsigned char* trie = (pBase + dcmd->export_off);
205-
+
206-
+ pSyms = (DLSyms*)( dlAllocMem(sizeof(DLSyms)) );
207-
+
208-
+ // First, get the number of symbols
209-
+ pSyms->symbolCount = visitTrie(trie, trie, trie + dcmd->export_size, NULL, NULL, 0, NULL, 0);
210-
+
211-
+ if (pSyms->symbolCount) {
212-
+ // Now revisit and copy symbols to their destination
213-
+ DCString s;
214-
+ size_t decompSize = pSyms->symbolCount * sizeof(char*) * 2;
215-
+ pSyms->decompressedSymbols = dlAllocMem(decompSize);
216-
+ memset(pSyms->decompressedSymbols, decompSize, 0);
217-
+ initString(&s, 1024);
218-
+ visitTrie(trie, trie, trie + dcmd->export_size, &s, CopyToNthString, 0, pSyms->decompressedSymbols, 0);
219-
+ freeString(&s);
220-
+ }
221-
+
222-
+ return pSyms;
223-
+ }
224-
+
225-
+ if (cmd->cmd & LC_REQ_DYLD) {
226-
+ return NULL; // "unknown load command required for execution";
227-
+ }
228-
+ if (cmd->cmd == LC_SYMTAB)
229-
+ {
230-
const struct symtab_command* scmd = (const struct symtab_command*)cmd;
231-
232-
pSyms = (DLSyms*)( dlAllocMem(sizeof(DLSyms)) );
233-
+ //memset(pSyms, sizeof(DLSyms), 0);
234-
+ pSyms->decompressedSymbols = NULL;
235-
pSyms->symbolCount = scmd->nsyms;
236-
pSyms->pStringTable = pBase + scmd->stroff;
237-
pSyms->pSymbolTable = (struct NLIST_TYPE*)(pBase + scmd->symoff);
238-
@@ -101,7 +265,6 @@
239-
cmd = (const struct load_command*)(((char*)cmd) + cmd->cmdsize);
235+
+ const struct dyld_info_command* dcmd = (const struct dyld_info_command*)cmd;
236+
+ const unsigned char* trie = (pBase + dcmd->export_off);
237+
+
238+
+ pSyms = (DLSyms*)( dlAllocMem(sizeof(DLSyms)) );
239+
+
240+
+ // First, get the number of symbols
241+
+ pSyms->symbolCount = visitTrie(trie, trie, trie + dcmd->export_size, NULL, NULL, 0, NULL, 0);
242+
+
243+
+ if (pSyms->symbolCount) {
244+
+ // Now revisit and copy symbols to their destination
245+
+ DCString s;
246+
+ size_t decompSize = pSyms->symbolCount * sizeof(char*) * 2;
247+
+ pSyms->decompressedSymbols = dlAllocMem(decompSize);
248+
+ memset(pSyms->decompressedSymbols, decompSize, 0);
249+
+ initString(&s, 1024);
250+
+ visitTrie(trie, trie, trie + dcmd->export_size, &s, CopyToNthString, 0, pSyms->decompressedSymbols, 0);
251+
+ freeString(&s);
252+
+ }
253+
+
254+
+ return pSyms;
255+
+ }
256+
+ cmd = get_next_command(cmd);
240257
}
241258
}
242259
- break;
243260
}
244261
}
245262
return NULL;
246-
@@ -113,6 +276,18 @@
263+
@@ -113,6 +281,18 @@
247264
if (!pSyms)
248265
return;
249266

@@ -262,7 +279,7 @@ diff -r bbefb8b8e74c dynload/dynload_syms_mach-o.c
262279
dlFreeMem(pSyms);
263280
}
264281

265-
@@ -133,6 +308,11 @@
282+
@@ -133,6 +313,11 @@
266283
if (nl->n_un.n_strx <= 1)
267284
return NULL; // would be empty string anyway
268285

@@ -274,7 +291,7 @@ diff -r bbefb8b8e74c dynload/dynload_syms_mach-o.c
274291
//TODO skip more symbols based on nl->n_desc and nl->n_type ?
275292
return nl;
276293
}
277-
@@ -140,6 +320,12 @@
294+
@@ -140,6 +325,12 @@
278295

279296
const char* dlSymsName(DLSyms* pSyms, int index)
280297
{
@@ -287,7 +304,7 @@ diff -r bbefb8b8e74c dynload/dynload_syms_mach-o.c
287304
const struct NLIST_TYPE* nl = get_nlist(pSyms, index);
288305
if (!nl)
289306
return NULL;
290-
@@ -150,6 +336,12 @@
307+
@@ -150,6 +341,12 @@
291308

292309
void* dlSymsValue(DLSyms* pSyms, int index)
293310
{
@@ -302,7 +319,7 @@ diff -r bbefb8b8e74c dynload/dynload_syms_mach-o.c
302319
return NULL;
303320
diff -r bbefb8b8e74c dynload/dynload_unix.c
304321
--- a/dynload/dynload_unix.c Thu Nov 24 23:47:31 2016 +0000
305-
+++ b/dynload/dynload_unix.c Sun Dec 25 20:36:56 2016 +0100
322+
+++ b/dynload/dynload_unix.c Thu Jan 05 09:36:39 2017 +0000
306323
@@ -41,7 +41,7 @@
307324

308325
DLLib* dlLoadLibrary(const char* libPath)
@@ -314,7 +331,7 @@ diff -r bbefb8b8e74c dynload/dynload_unix.c
314331

315332
diff -r bbefb8b8e74c dynload/dynload_windows.c
316333
--- a/dynload/dynload_windows.c Thu Nov 24 23:47:31 2016 +0000
317-
+++ b/dynload/dynload_windows.c Sun Dec 25 20:36:56 2016 +0100
334+
+++ b/dynload/dynload_windows.c Thu Jan 05 09:36:39 2017 +0000
318335
@@ -41,7 +41,9 @@
319336
DLLib* dlLoadLibrary(const char* libPath)
320337
{
Binary file not shown.

0 commit comments

Comments
 (0)