diff options
author | Joseph Hunkeler <jhunkeler@users.noreply.github.com> | 2020-04-24 15:29:05 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-24 15:29:05 -0400 |
commit | 338abde356efcae6cf0a000b71b781d21c8733b6 (patch) | |
tree | b275d303c93662ea4879fdca4597aa21e1c9421c /lib | |
parent | d78903961ee95376bccde7c1313be5b583911ab7 (diff) | |
parent | 24992f1f426111f0ce05df341087a55486ae48db (diff) | |
download | spmc-338abde356efcae6cf0a000b71b781d21c8733b6.tar.gz |
Merge pull request #30 from jhunkeler/shlib-macos
Shlib macos
Diffstat (limited to 'lib')
-rw-r--r-- | lib/config_global.c | 6 | ||||
-rw-r--r-- | lib/error_handler.c | 1 | ||||
-rw-r--r-- | lib/extern/url.c | 2 | ||||
-rw-r--r-- | lib/internal_cmd.c | 4 | ||||
-rw-r--r-- | lib/mime.c | 2 | ||||
-rw-r--r-- | lib/shlib.c | 84 |
6 files changed, 81 insertions, 18 deletions
diff --git a/lib/config_global.c b/lib/config_global.c index 52589d4..95302c3 100644 --- a/lib/config_global.c +++ b/lib/config_global.c @@ -129,11 +129,11 @@ void check_runtime_environment(void) { int bad_rt = 0; char *required[] = { "file", -#if defined(__linux) || defined(__linux__) +#if OS_LINUX "patchelf", -#elif defined(__APPLE__) && defined(__MACH__) +#elif OS_DARWIN "install_name_tool", -#elif defined(__WIN32__) +#elif OS_WINDOWS // TODO: Does windows provide some kind of equivalent? #endif "objdump", diff --git a/lib/error_handler.c b/lib/error_handler.c index cb2ef51..aa48274 100644 --- a/lib/error_handler.c +++ b/lib/error_handler.c @@ -13,6 +13,7 @@ const char *SPM_ERR_STRING[] = { "Failed to fetch package", "Manifest has no header", "Manifest has no data", + "Parsing error", NULL, }; diff --git a/lib/extern/url.c b/lib/extern/url.c index fe54db2..c39b314 100644 --- a/lib/extern/url.c +++ b/lib/extern/url.c @@ -137,7 +137,7 @@ static int fill_buffer(URL_FILE *file, size_t want) { curl_multi_fdset() doc. */ if (maxfd == -1) { -#ifdef _WIN32 +#ifdef _WIN32 // OS_WINDOWS not available at this level Sleep(100); rc = 0; #else diff --git a/lib/internal_cmd.c b/lib/internal_cmd.c index e5f875f..6f6b1c6 100644 --- a/lib/internal_cmd.c +++ b/lib/internal_cmd.c @@ -237,9 +237,9 @@ int mkruntime_interface(int argc, char **argv) { } runtime_set(rt, "CFLAGS", "-I$SPM_INCLUDE $CFLAGS"); -#if defined(__APPLE__) && defined (__MACH__) +#if OS_DARWIN runtime_set(rt, "LDFLAGS", "-rpath $SPM_LIB:$SPM_LIB64 -L$SPM_LIB -L$SPM_LIB64 $LDFLAGS"); -#elif defined(__linux) || defined (__linux__) +#elif OS_LINUX runtime_set(rt, "LDFLAGS", "-Wl,-rpath=$SPM_LIB:$SPM_LIB64 -L$SPM_LIB -L$SPM_LIB64 $LDFLAGS"); #else // TODO: Windows? @@ -14,7 +14,7 @@ Process *file_command(const char *_filename) { Process *proc_info = NULL; char sh_cmd[PATH_MAX]; sh_cmd[0] = '\0'; -#ifdef __APPLE__ +#if OS_DARWIN const char *fmt_cmd = "file -I \"%s\" 2>&1"; #else // GNU const char *fmt_cmd = "file -i \"%s\" 2>&1"; diff --git a/lib/shlib.c b/lib/shlib.c index a8222af..04dbb49 100644 --- a/lib/shlib.c +++ b/lib/shlib.c @@ -9,13 +9,19 @@ char *shlib_deps_objdump(const char *_filename) { char cmd[PATH_MAX]; memset(cmd, '\0', sizeof(cmd)); + if (_filename == NULL) { + spmerrno = EINVAL; + spmerrno_cause("_filename was NULL"); + return NULL; + } + if ((filename = strdup(_filename)) == NULL) { fprintf(SYSERROR); return NULL; } strchrdel(filename, SHELL_INVALID); - snprintf(cmd, sizeof(cmd), "%s %s '%s'", SPM_SHLIB_EXEC, "-p", filename); + snprintf(cmd, sizeof(cmd), "%s %s '%s'", SPM_SHLIB_EXEC, SPM_SHLIB_EXEC_ARGS, filename); shell(&proc, SHELL_OUTPUT, cmd); if (proc->returncode != 0) { @@ -32,41 +38,97 @@ char *shlib_deps_objdump(const char *_filename) { StrList *shlib_deps(const char *filename) { char **data = NULL; - char *output = NULL; + char *raw_data = NULL; StrList *result = NULL; + if (filename == NULL) { + spmerrno = EINVAL; + spmerrno_cause("filename was NULL"); + return NULL; + } + // Get output from objdump // TODO: use preprocessor or another function to select the correct shlib_deps_*() in the future - if ((output = shlib_deps_objdump(filename)) == NULL) { + if ((raw_data = shlib_deps_objdump(filename)) == NULL) { return NULL; } // Initialize list array if ((result = strlist_init()) == NULL) { - free(output); + free(raw_data); return NULL; } // Split output into individual lines - if ((data = split(output, "\n")) == NULL) { - free(output); + if ((data = split(raw_data, "\n")) == NULL) { + free(raw_data); strlist_free(result); return NULL; } - // Parse output: - // Collapse whitespace and extract the NEEDED libraries (second field) - // AFAIK when "NEEDED" is present, a string containing the library name is guaranteed to be there + // Collapse all whitespace in each line + // i.e. " stuff things" -> "stuff things" for (size_t i = 0; data[i] != NULL; i++) { data[i] = normalize_space(data[i]); + } + + for (size_t i = 0; data[i] != NULL; i++) { + char **field = NULL; + char reason[255] = {0,}; + +#if OS_LINUX + // Extract the NEEDED libraries (second field) + // AFAIK when "NEEDED" is present, a string containing the library name is guaranteed to be there if (startswith(data[i], "NEEDED")) { - char **field = split(data[i], " "); + if ((field = split(data[i], " ")) == NULL) { + strlist_free(result); + result = NULL; + break; + } + + // record library path + strlist_append(result, field[1]); + split_free(field); + } +#elif OS_DARWIN + size_t offset_name = i + 2; // how many lines to look ahead after reaching LC_LOAD_DYLIB + size_t numLines; + for (numLines = 0; data[numLines] != NULL; numLines++); // get line count + + // Find APPLE's equivalent to NEEDED on Linux + if (startswith(data[i], "cmd LC_LOAD_DYLIB")) { + // Don't overrun the data buffer + if (offset_name > numLines || data[offset_name] == NULL) { + break; + } + + // split on: "name /library/path" + if ((field = split(data[offset_name], " ")) == NULL) { + sprintf(reason, "'%s' produced unreadable output", SPM_SHLIB_EXEC, i, offset_name); + spmerrno = SPM_ERR_PARSE; + spmerrno_cause(reason); + + strlist_free(result); + result = NULL; + break; + } + + // verify it was actually "name ..." + if (strcmp(field[0], "name") != 0) { + sprintf(reason, "'%s' produced unexpected LC_LOAD_DYLIB format between lines %zu:%zu", SPM_SHLIB_EXEC, i, offset_name); + spmerrno = SPM_ERR_PARSE; + spmerrno_cause(reason); + break; + } + + // record library path strlist_append(result, field[1]); split_free(field); } +#endif } - free(output); + free(raw_data); split_free(data); return result; } |