Skip to content

Commit 56d0ccd

Browse files
committed
Replace the usage of dlopen with llvm::sys::DynamicLibrary
This will be helpful in order to support environments where dlfcn.h is not available, e.g. Windows + mingw.
1 parent a8b6900 commit 56d0ccd

File tree

3 files changed

+8
-13
lines changed

3 files changed

+8
-13
lines changed

lld/ELF/CMakeLists.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ endif()
2121
set(GNULTO_INCLUDE_DIR "" CACHE PATH "Additional directory, where CMake should search for plugin-api.h")
2222
if(LLD_ENABLE_GNU_LTO)
2323
find_package(GNULTO REQUIRED)
24-
include(CheckSymbolExists)
25-
check_symbol_exists(dlopen "dlfcn.h" HAVE_DLOPEN)
26-
if(NOT HAVE_DLOPEN)
27-
message(FATAL_ERROR "Could not find the definition of dlopen(). Is dlfcn.h available?")
28-
endif()
2924
endif()
3025

3126
configure_file(

lld/ELF/LTO.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -427,8 +427,6 @@ void BitcodeCompiler::addObject(IRFile &f,
427427
}
428428

429429
#if LLD_ENABLE_GNU_LTO
430-
#include <dlfcn.h>
431-
432430
GccIRCompiler *GccIRCompiler::singleton = nullptr;
433431

434432
GccIRCompiler *GccIRCompiler::getInstance() {
@@ -460,13 +458,14 @@ GccIRCompiler::~GccIRCompiler() {
460458
}
461459

462460
void GccIRCompiler::loadPlugin() {
463-
plugin = dlopen(ctx.arg.plugin.data(), RTLD_NOW);
464-
if (plugin == NULL) {
465-
error(dlerror());
461+
std::string Error;
462+
plugin = llvm::sys::DynamicLibrary::getPermanentLibrary(ctx.arg.plugin.data(), &Error);
463+
if (!plugin.isValid()) {
464+
error(Error);
466465
return;
467466
}
468-
void *tmp = dlsym(plugin, "onload");
469-
if (tmp == NULL) {
467+
void *tmp = plugin.getAddressOfSymbol("onload");
468+
if (!tmp) {
470469
error("Plugin does not provide onload()");
471470
return;
472471
}

lld/ELF/LTO.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "llvm/ADT/DenseSet.h"
2626
#include "llvm/ADT/SmallString.h"
2727
#include "llvm/Support/raw_ostream.h"
28+
#include "llvm/Support/DynamicLibrary.h"
2829
#include <memory>
2930
#include <vector>
3031

@@ -114,7 +115,7 @@ class GccIRCompiler : public IRCompiler {
114115
#endif
115116
ld_plugin_all_symbols_read_handler allSymbolsReadHandler;
116117
// Handle for the shared library created via dlopen().
117-
void *plugin;
118+
llvm::sys::DynamicLibrary plugin;
118119

119120
void initializeTv();
120121
};

0 commit comments

Comments
 (0)