Skip to content

Commit

Permalink
Prefer using LC_SYMTAB over LC_DYLD_INFO / LC_DYLD_INFO_ONLY in macho…
Browse files Browse the repository at this point in the history
… parser
  • Loading branch information
ochafik committed Jan 5, 2017
1 parent 12176ca commit 373c034
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 48 deletions.
113 changes: 65 additions & 48 deletions src/main/cpp/bridj/dyncall.diff
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
diff -r bbefb8b8e74c dynload/dynload_syms_mach-o.c
--- a/dynload/dynload_syms_mach-o.c Thu Nov 24 23:47:31 2016 +0000
+++ b/dynload/dynload_syms_mach-o.c Sun Dec 25 20:36:56 2016 +0100
+++ b/dynload/dynload_syms_mach-o.c Thu Jan 05 09:36:39 2017 +0000
@@ -29,6 +29,7 @@
dynamic symbol resolver for Mach-O

Expand All @@ -27,7 +27,7 @@ diff -r bbefb8b8e74c dynload/dynload_syms_mach-o.c
struct DLLib_
{
char* libPath;
@@ -58,11 +60,139 @@
@@ -58,11 +60,143 @@

struct DLSyms_
{
Expand All @@ -37,6 +37,10 @@ diff -r bbefb8b8e74c dynload/dynload_syms_mach-o.c
uint32_t symbolCount;
};

+const struct load_command* get_next_command(const struct load_command* cmd) {
+ return (const struct load_command*)(((char*)cmd) + cmd->cmdsize);
+}
+
+int isSameMacImageName(const char* libPath, const char* systemLibPath) {
+ if (!libPath || !systemLibPath)
+ return 0;
Expand Down Expand Up @@ -167,7 +171,7 @@ diff -r bbefb8b8e74c dynload/dynload_syms_mach-o.c

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

+ // First, try and find a LC_SYMTAB
+ for (iCmd = 0; iCmd < nCmds; iCmd++)
+ {
+ if (cmd->cmd == LC_SYMTAB)
+ {
+ const struct symtab_command* scmd = (const struct symtab_command*)cmd;
+
+ pSyms = (DLSyms*)( dlAllocMem(sizeof(DLSyms)) );
+ //memset(pSyms, sizeof(DLSyms), 0);
+ pSyms->decompressedSymbols = NULL;
+ pSyms->symbolCount = scmd->nsyms;
+ pSyms->pStringTable = pBase + scmd->stroff;
+ pSyms->pSymbolTable = (struct NLIST_TYPE*)(pBase + scmd->symoff);
+
+ return pSyms;
+ }
+ cmd = get_next_command(cmd);
+ }
+
+ // Then, try and use LC_DYLD_INFO_ONLY or LC_DYLD_INFO and parse their trie.
for (iCmd = 0; iCmd < nCmds; iCmd++)
{
- if (cmd->cmd == LC_SYMTAB)
- {
+ if (!cmd) continue;
+
- const struct symtab_command* scmd = (const struct symtab_command*)cmd;
-
- pSyms = (DLSyms*)( dlAllocMem(sizeof(DLSyms)) );
- pSyms->symbolCount = scmd->nsyms;
- pSyms->pStringTable = pBase + scmd->stroff;
- pSyms->pSymbolTable = (struct NLIST_TYPE*)(pBase + scmd->symoff);
-
- return pSyms;
- }
- cmd = (const struct load_command*)(((char*)cmd) + cmd->cmdsize);
+ if (cmd->cmd == LC_DYLD_INFO_ONLY || cmd->cmd == LC_DYLD_INFO) {
+ const struct dyld_info_command* dcmd = (const struct dyld_info_command*)cmd;
+ const unsigned char* trie = (pBase + dcmd->export_off);
+
+ pSyms = (DLSyms*)( dlAllocMem(sizeof(DLSyms)) );
+
+ // First, get the number of symbols
+ pSyms->symbolCount = visitTrie(trie, trie, trie + dcmd->export_size, NULL, NULL, 0, NULL, 0);
+
+ if (pSyms->symbolCount) {
+ // Now revisit and copy symbols to their destination
+ DCString s;
+ size_t decompSize = pSyms->symbolCount * sizeof(char*) * 2;
+ pSyms->decompressedSymbols = dlAllocMem(decompSize);
+ memset(pSyms->decompressedSymbols, decompSize, 0);
+ initString(&s, 1024);
+ visitTrie(trie, trie, trie + dcmd->export_size, &s, CopyToNthString, 0, pSyms->decompressedSymbols, 0);
+ freeString(&s);
+ }
+
+ return pSyms;
+ }
+
+ if (cmd->cmd & LC_REQ_DYLD) {
+ return NULL; // "unknown load command required for execution";
+ }
+ if (cmd->cmd == LC_SYMTAB)
+ {
const struct symtab_command* scmd = (const struct symtab_command*)cmd;

pSyms = (DLSyms*)( dlAllocMem(sizeof(DLSyms)) );
+ //memset(pSyms, sizeof(DLSyms), 0);
+ pSyms->decompressedSymbols = NULL;
pSyms->symbolCount = scmd->nsyms;
pSyms->pStringTable = pBase + scmd->stroff;
pSyms->pSymbolTable = (struct NLIST_TYPE*)(pBase + scmd->symoff);
@@ -101,7 +265,6 @@
cmd = (const struct load_command*)(((char*)cmd) + cmd->cmdsize);
+ const struct dyld_info_command* dcmd = (const struct dyld_info_command*)cmd;
+ const unsigned char* trie = (pBase + dcmd->export_off);
+
+ pSyms = (DLSyms*)( dlAllocMem(sizeof(DLSyms)) );
+
+ // First, get the number of symbols
+ pSyms->symbolCount = visitTrie(trie, trie, trie + dcmd->export_size, NULL, NULL, 0, NULL, 0);
+
+ if (pSyms->symbolCount) {
+ // Now revisit and copy symbols to their destination
+ DCString s;
+ size_t decompSize = pSyms->symbolCount * sizeof(char*) * 2;
+ pSyms->decompressedSymbols = dlAllocMem(decompSize);
+ memset(pSyms->decompressedSymbols, decompSize, 0);
+ initString(&s, 1024);
+ visitTrie(trie, trie, trie + dcmd->export_size, &s, CopyToNthString, 0, pSyms->decompressedSymbols, 0);
+ freeString(&s);
+ }
+
+ return pSyms;
+ }
+ cmd = get_next_command(cmd);
}
}
- break;
}
}
return NULL;
@@ -113,6 +276,18 @@
@@ -113,6 +281,18 @@
if (!pSyms)
return;

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

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

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

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

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

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

diff -r bbefb8b8e74c dynload/dynload_windows.c
--- a/dynload/dynload_windows.c Thu Nov 24 23:47:31 2016 +0000
+++ b/dynload/dynload_windows.c Sun Dec 25 20:36:56 2016 +0100
+++ b/dynload/dynload_windows.c Thu Jan 05 09:36:39 2017 +0000
@@ -41,7 +41,9 @@
DLLib* dlLoadLibrary(const char* libPath)
{
Expand Down
Binary file modified src/main/resources/org/bridj/lib/darwin_universal/libbridj.dylib
Binary file not shown.

0 comments on commit 373c034

Please sign in to comment.