Pavel Labath e0dbd02513 [lldb/test] Make TestLoadUnload compatible with windows
Summary:
This patch introduces a header "dylib.h" which can be used in tests to
handle shared libraries semi-portably. The shared library APIs on
windows and posix systems look very different, but their underlying
functionality is relatively similar, so the mapping is not difficult.

It also introduces two new macros to wrap the functinality necessary to
export/import function across the dll boundary on windows. Previously we
had the LLDB_TEST_API macro for this purpose, which automagically
changed meaning depending on whether we were building the shared library
or the executable. While convenient for simple cases, this approach was
not sufficient for the more complicated setups where one deals with
multiple shared libraries.

Lastly it rewrites TestLoadUnload, to make use of the new APIs. The
trickiest aspect there is the handling of DYLD_LIBRARY_PATH on macos --
previously setting this variable was not needed as the test used
@executable_path-relative dlopens, but the new generic api does not
support that. Other systems do not support such dlopens either so the
test already contained support for setting the appropriate path
variable, and this patch just makes that logic more generic. In doesn't
seem that the purpose of this test was to exercise @executable_path
imports, so this should not be a problem.

These changes are sufficient to make some of the TestLoadUnload tests
pass on windows. Two other tests will start to pass once D77287 lands.

Reviewers: amccarth, jingham, JDevlieghere, compnerd

Subscribers: lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D77662
2020-04-14 11:10:59 +02:00

56 lines
1.1 KiB
C

#ifndef LLDB_TEST_DYLIB_H
#define LLDB_TEST_DYLIB_H
#include <stdio.h>
#ifdef _WIN32
#include <Windows.h>
#define dylib_get_symbol(handle, name) GetProcAddress((HMODULE)handle, name)
#define dylib_close(handle) (!FreeLibrary((HMODULE)handle))
#else
#include <dlfcn.h>
#define dylib_get_symbol(handle, name) dlsym(handle, name)
#define dylib_close(handle) dlclose(handle)
#endif
inline void *dylib_open(const char *name) {
char dylib_prefix[] =
#ifdef _WIN32
"";
#else
"lib";
#endif
char dylib_suffix[] =
#ifdef _WIN32
".dll";
#elif defined(__APPLE__)
".dylib";
#else
".so";
#endif
char fullname[1024];
snprintf(fullname, sizeof(fullname), "%s%s%s", dylib_prefix, name, dylib_suffix);
#ifdef _WIN32
return LoadLibraryA(fullname);
#else
return dlopen(fullname, RTLD_NOW);
#endif
}
inline const char *dylib_last_error() {
#ifndef _WIN32
return dlerror();
#else
DWORD err = GetLastError();
char *msg;
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (char *)&msg, 0, NULL);
return msg;
#endif
}
#endif