1 //===- elfnix_platform.cpp ------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file contains code required to load the rest of the ELF-on-*IX runtime.
11 //===----------------------------------------------------------------------===//
13 #include "elfnix_platform.h"
17 #include "wrapper_function_utils.h"
23 #include <string_view>
24 #include <unordered_map>
27 using namespace __orc_rt
;
28 using namespace __orc_rt::elfnix
;
30 // Declare function tags for functions in the JIT process.
31 ORC_RT_JIT_DISPATCH_TAG(__orc_rt_elfnix_get_initializers_tag
)
32 ORC_RT_JIT_DISPATCH_TAG(__orc_rt_elfnix_get_deinitializers_tag
)
33 ORC_RT_JIT_DISPATCH_TAG(__orc_rt_elfnix_symbol_lookup_tag
)
35 // eh-frame registration functions, made available via aliases
36 // installed by the Platform
37 extern "C" void __register_frame(const void *);
38 extern "C" void __deregister_frame(const void *);
41 __unw_add_dynamic_eh_frame_section(const void *) ORC_RT_WEAK_IMPORT
;
43 __unw_remove_dynamic_eh_frame_section(const void *) ORC_RT_WEAK_IMPORT
;
47 Error
validatePointerSectionExtent(const char *SectionName
,
48 const ExecutorAddrRange
&SE
) {
49 if (SE
.size() % sizeof(uintptr_t)) {
50 std::ostringstream ErrMsg
;
51 ErrMsg
<< std::hex
<< "Size of " << SectionName
<< " 0x"
52 << SE
.Start
.getValue() << " -- 0x" << SE
.End
.getValue()
53 << " is not a pointer multiple";
54 return make_error
<StringError
>(ErrMsg
.str());
56 return Error::success();
59 Error
runInitArray(const std::vector
<ExecutorAddrRange
> &InitArraySections
,
60 const ELFNixJITDylibInitializers
&MOJDIs
) {
62 for (const auto &ModInits
: InitArraySections
) {
63 if (auto Err
= validatePointerSectionExtent(".init_array", ModInits
))
66 using InitFunc
= void (*)();
67 for (auto *Init
: ModInits
.toSpan
<InitFunc
>())
71 return Error::success();
75 unsigned long Key
= 0;
76 unsigned long DataAddress
= 0;
79 struct TLSDescriptor
{
80 void (*Resolver
)(void *);
81 TLSInfoEntry
*InfoEntry
;
84 class ELFNixPlatformRuntimeState
{
91 using AtExitsVector
= std::vector
<AtExitEntry
>;
93 struct PerJITDylibState
{
94 void *Header
= nullptr;
96 bool AllowReinitialization
= false;
97 AtExitsVector AtExits
;
101 static void initialize(void *DSOHandle
);
102 static ELFNixPlatformRuntimeState
&get();
103 static void destroy();
105 ELFNixPlatformRuntimeState(void *DSOHandle
);
107 // Delete copy and move constructors.
108 ELFNixPlatformRuntimeState(const ELFNixPlatformRuntimeState
&) = delete;
109 ELFNixPlatformRuntimeState
&
110 operator=(const ELFNixPlatformRuntimeState
&) = delete;
111 ELFNixPlatformRuntimeState(ELFNixPlatformRuntimeState
&&) = delete;
112 ELFNixPlatformRuntimeState
&operator=(ELFNixPlatformRuntimeState
&&) = delete;
114 Error
registerObjectSections(ELFNixPerObjectSectionsToRegister POSR
);
115 Error
deregisterObjectSections(ELFNixPerObjectSectionsToRegister POSR
);
117 const char *dlerror();
118 void *dlopen(std::string_view Name
, int Mode
);
119 int dlclose(void *DSOHandle
);
120 void *dlsym(void *DSOHandle
, std::string_view Symbol
);
122 int registerAtExit(void (*F
)(void *), void *Arg
, void *DSOHandle
);
123 void runAtExits(void *DSOHandle
);
125 /// Returns the base address of the section containing ThreadData.
126 Expected
<std::pair
<const char *, size_t>>
127 getThreadDataSectionFor(const char *ThreadData
);
129 void *getPlatformJDDSOHandle() { return PlatformJDDSOHandle
; }
132 PerJITDylibState
*getJITDylibStateByHeaderAddr(void *DSOHandle
);
133 PerJITDylibState
*getJITDylibStateByName(std::string_view Path
);
135 getOrCreateJITDylibState(ELFNixJITDylibInitializers
&MOJDIs
);
137 Error
registerThreadDataSection(span
<const char> ThreadDataSection
);
139 Expected
<ExecutorAddr
> lookupSymbolInJITDylib(void *DSOHandle
,
140 std::string_view Symbol
);
142 Expected
<ELFNixJITDylibInitializerSequence
>
143 getJITDylibInitializersByName(std::string_view Path
);
144 Expected
<void *> dlopenInitialize(std::string_view Path
, int Mode
);
145 Error
initializeJITDylib(ELFNixJITDylibInitializers
&MOJDIs
);
147 static ELFNixPlatformRuntimeState
*MOPS
;
149 void *PlatformJDDSOHandle
;
151 // Frame registration functions:
152 void (*registerEHFrameSection
)(const void *) = nullptr;
153 void (*deregisterEHFrameSection
)(const void *) = nullptr;
155 // FIXME: Move to thread-state.
156 std::string DLFcnError
;
158 std::recursive_mutex JDStatesMutex
;
159 std::unordered_map
<void *, PerJITDylibState
> JDStates
;
160 std::unordered_map
<std::string
, void *> JDNameToHeader
;
162 std::mutex ThreadDataSectionsMutex
;
163 std::map
<const char *, size_t> ThreadDataSections
;
166 ELFNixPlatformRuntimeState
*ELFNixPlatformRuntimeState::MOPS
= nullptr;
168 void ELFNixPlatformRuntimeState::initialize(void *DSOHandle
) {
169 assert(!MOPS
&& "ELFNixPlatformRuntimeState should be null");
170 MOPS
= new ELFNixPlatformRuntimeState(DSOHandle
);
173 ELFNixPlatformRuntimeState
&ELFNixPlatformRuntimeState::get() {
174 assert(MOPS
&& "ELFNixPlatformRuntimeState not initialized");
178 void ELFNixPlatformRuntimeState::destroy() {
179 assert(MOPS
&& "ELFNixPlatformRuntimeState not initialized");
183 ELFNixPlatformRuntimeState::ELFNixPlatformRuntimeState(void *DSOHandle
)
184 : PlatformJDDSOHandle(DSOHandle
) {
185 if (__unw_add_dynamic_eh_frame_section
&&
186 __unw_remove_dynamic_eh_frame_section
) {
187 registerEHFrameSection
= __unw_add_dynamic_eh_frame_section
;
188 deregisterEHFrameSection
= __unw_remove_dynamic_eh_frame_section
;
190 registerEHFrameSection
= __register_frame
;
191 deregisterEHFrameSection
= __deregister_frame
;
195 Error
ELFNixPlatformRuntimeState::registerObjectSections(
196 ELFNixPerObjectSectionsToRegister POSR
) {
197 if (POSR
.EHFrameSection
.Start
)
198 registerEHFrameSection(POSR
.EHFrameSection
.Start
.toPtr
<const char *>());
200 if (POSR
.ThreadDataSection
.Start
) {
201 if (auto Err
= registerThreadDataSection(
202 POSR
.ThreadDataSection
.toSpan
<const char>()))
206 return Error::success();
209 Error
ELFNixPlatformRuntimeState::deregisterObjectSections(
210 ELFNixPerObjectSectionsToRegister POSR
) {
211 if (POSR
.EHFrameSection
.Start
)
212 deregisterEHFrameSection(POSR
.EHFrameSection
.Start
.toPtr
<const char *>());
214 return Error::success();
217 const char *ELFNixPlatformRuntimeState::dlerror() { return DLFcnError
.c_str(); }
219 void *ELFNixPlatformRuntimeState::dlopen(std::string_view Path
, int Mode
) {
220 std::lock_guard
<std::recursive_mutex
> Lock(JDStatesMutex
);
222 // Use fast path if all JITDylibs are already loaded and don't require
223 // re-running initializers.
224 if (auto *JDS
= getJITDylibStateByName(Path
)) {
225 if (!JDS
->AllowReinitialization
) {
231 auto H
= dlopenInitialize(Path
, Mode
);
233 DLFcnError
= toString(H
.takeError());
240 int ELFNixPlatformRuntimeState::dlclose(void *DSOHandle
) {
241 runAtExits(DSOHandle
);
245 void *ELFNixPlatformRuntimeState::dlsym(void *DSOHandle
,
246 std::string_view Symbol
) {
247 auto Addr
= lookupSymbolInJITDylib(DSOHandle
, Symbol
);
249 DLFcnError
= toString(Addr
.takeError());
253 return Addr
->toPtr
<void *>();
256 int ELFNixPlatformRuntimeState::registerAtExit(void (*F
)(void *), void *Arg
,
258 // FIXME: Handle out-of-memory errors, returning -1 if OOM.
259 std::lock_guard
<std::recursive_mutex
> Lock(JDStatesMutex
);
260 auto *JDS
= getJITDylibStateByHeaderAddr(DSOHandle
);
261 assert(JDS
&& "JITDylib state not initialized");
262 JDS
->AtExits
.push_back({F
, Arg
});
266 void ELFNixPlatformRuntimeState::runAtExits(void *DSOHandle
) {
267 // FIXME: Should atexits be allowed to run concurrently with access to
271 std::lock_guard
<std::recursive_mutex
> Lock(JDStatesMutex
);
272 auto *JDS
= getJITDylibStateByHeaderAddr(DSOHandle
);
273 assert(JDS
&& "JITDlybi state not initialized");
274 std::swap(V
, JDS
->AtExits
);
284 Expected
<std::pair
<const char *, size_t>>
285 ELFNixPlatformRuntimeState::getThreadDataSectionFor(const char *ThreadData
) {
286 std::lock_guard
<std::mutex
> Lock(ThreadDataSectionsMutex
);
287 auto I
= ThreadDataSections
.upper_bound(ThreadData
);
288 // Check that we have a valid entry conovering this address.
289 if (I
== ThreadDataSections
.begin())
290 return make_error
<StringError
>("No thread local data section for key");
292 if (ThreadData
>= I
->first
+ I
->second
)
293 return make_error
<StringError
>("No thread local data section for key");
297 ELFNixPlatformRuntimeState::PerJITDylibState
*
298 ELFNixPlatformRuntimeState::getJITDylibStateByHeaderAddr(void *DSOHandle
) {
299 auto I
= JDStates
.find(DSOHandle
);
300 if (I
== JDStates
.end())
305 ELFNixPlatformRuntimeState::PerJITDylibState
*
306 ELFNixPlatformRuntimeState::getJITDylibStateByName(std::string_view Name
) {
307 // FIXME: Avoid creating string copy here.
308 auto I
= JDNameToHeader
.find(std::string(Name
.data(), Name
.size()));
309 if (I
== JDNameToHeader
.end())
312 auto J
= JDStates
.find(H
);
313 assert(J
!= JDStates
.end() &&
314 "JITDylib has name map entry but no header map entry");
318 ELFNixPlatformRuntimeState::PerJITDylibState
&
319 ELFNixPlatformRuntimeState::getOrCreateJITDylibState(
320 ELFNixJITDylibInitializers
&MOJDIs
) {
321 void *Header
= MOJDIs
.DSOHandleAddress
.toPtr
<void *>();
323 auto &JDS
= JDStates
[Header
];
325 // If this entry hasn't been created yet.
327 assert(!JDNameToHeader
.count(MOJDIs
.Name
) &&
328 "JITDylib has header map entry but no name map entry");
329 JDNameToHeader
[MOJDIs
.Name
] = Header
;
336 Error
ELFNixPlatformRuntimeState::registerThreadDataSection(
337 span
<const char> ThreadDataSection
) {
338 std::lock_guard
<std::mutex
> Lock(ThreadDataSectionsMutex
);
339 auto I
= ThreadDataSections
.upper_bound(ThreadDataSection
.data());
340 if (I
!= ThreadDataSections
.begin()) {
341 auto J
= std::prev(I
);
342 if (J
->first
+ J
->second
> ThreadDataSection
.data())
343 return make_error
<StringError
>("Overlapping .tdata sections");
345 ThreadDataSections
.insert(
346 I
, std::make_pair(ThreadDataSection
.data(), ThreadDataSection
.size()));
347 return Error::success();
350 Expected
<ExecutorAddr
>
351 ELFNixPlatformRuntimeState::lookupSymbolInJITDylib(void *DSOHandle
,
352 std::string_view Sym
) {
353 Expected
<ExecutorAddr
> Result((ExecutorAddr()));
354 if (auto Err
= WrapperFunction
<SPSExpected
<SPSExecutorAddr
>(
355 SPSExecutorAddr
, SPSString
)>::call(&__orc_rt_elfnix_symbol_lookup_tag
,
357 ExecutorAddr::fromPtr(DSOHandle
),
359 return std::move(Err
);
363 Expected
<ELFNixJITDylibInitializerSequence
>
364 ELFNixPlatformRuntimeState::getJITDylibInitializersByName(
365 std::string_view Path
) {
366 Expected
<ELFNixJITDylibInitializerSequence
> Result(
367 (ELFNixJITDylibInitializerSequence()));
368 std::string
PathStr(Path
.data(), Path
.size());
370 WrapperFunction
<SPSExpected
<SPSELFNixJITDylibInitializerSequence
>(
371 SPSString
)>::call(&__orc_rt_elfnix_get_initializers_tag
, Result
,
373 return std::move(Err
);
378 ELFNixPlatformRuntimeState::dlopenInitialize(std::string_view Path
, int Mode
) {
379 // Either our JITDylib wasn't loaded, or it or one of its dependencies allows
380 // reinitialization. We need to call in to the JIT to see if there's any new
382 auto InitSeq
= getJITDylibInitializersByName(Path
);
384 return InitSeq
.takeError();
386 // Init sequences should be non-empty.
387 if (InitSeq
->empty())
388 return make_error
<StringError
>(
389 "__orc_rt_elfnix_get_initializers returned an "
390 "empty init sequence");
392 // Otherwise register and run initializers for each JITDylib.
393 for (auto &MOJDIs
: *InitSeq
)
394 if (auto Err
= initializeJITDylib(MOJDIs
))
395 return std::move(Err
);
397 // Return the header for the last item in the list.
398 auto *JDS
= getJITDylibStateByHeaderAddr(
399 InitSeq
->back().DSOHandleAddress
.toPtr
<void *>());
400 assert(JDS
&& "Missing state entry for JD");
404 long getPriority(const std::string
&name
) {
405 auto pos
= name
.find_last_not_of("0123456789");
406 if (pos
== name
.size() - 1)
409 return std::strtol(name
.c_str() + pos
+ 1, nullptr, 10);
412 Error
ELFNixPlatformRuntimeState::initializeJITDylib(
413 ELFNixJITDylibInitializers
&MOJDIs
) {
415 auto &JDS
= getOrCreateJITDylibState(MOJDIs
);
418 using SectionList
= std::vector
<ExecutorAddrRange
>;
419 std::sort(MOJDIs
.InitSections
.begin(), MOJDIs
.InitSections
.end(),
420 [](const std::pair
<std::string
, SectionList
> &LHS
,
421 const std::pair
<std::string
, SectionList
> &RHS
) -> bool {
422 return getPriority(LHS
.first
) < getPriority(RHS
.first
);
424 for (auto &Entry
: MOJDIs
.InitSections
)
425 if (auto Err
= runInitArray(Entry
.second
, MOJDIs
))
428 return Error::success();
430 class ELFNixPlatformRuntimeTLVManager
{
432 void *getInstance(const char *ThreadData
);
435 std::unordered_map
<const char *, char *> Instances
;
436 std::unordered_map
<const char *, std::unique_ptr
<char[]>> AllocatedSections
;
439 void *ELFNixPlatformRuntimeTLVManager::getInstance(const char *ThreadData
) {
440 auto I
= Instances
.find(ThreadData
);
441 if (I
!= Instances
.end())
444 ELFNixPlatformRuntimeState::get().getThreadDataSectionFor(ThreadData
);
446 __orc_rt_log_error(toString(TDS
.takeError()).c_str());
450 auto &Allocated
= AllocatedSections
[TDS
->first
];
452 Allocated
= std::make_unique
<char[]>(TDS
->second
);
453 memcpy(Allocated
.get(), TDS
->first
, TDS
->second
);
455 size_t ThreadDataDelta
= ThreadData
- TDS
->first
;
456 assert(ThreadDataDelta
<= TDS
->second
&& "ThreadData outside section bounds");
458 char *Instance
= Allocated
.get() + ThreadDataDelta
;
459 Instances
[ThreadData
] = Instance
;
463 void destroyELFNixTLVMgr(void *ELFNixTLVMgr
) {
464 delete static_cast<ELFNixPlatformRuntimeTLVManager
*>(ELFNixTLVMgr
);
467 } // end anonymous namespace
469 //------------------------------------------------------------------------------
471 //------------------------------------------------------------------------------
473 ORC_RT_INTERFACE orc_rt_CWrapperFunctionResult
474 __orc_rt_elfnix_platform_bootstrap(char *ArgData
, size_t ArgSize
) {
475 return WrapperFunction
<void(uint64_t)>::handle(
477 [](uint64_t &DSOHandle
) {
478 ELFNixPlatformRuntimeState::initialize(
479 reinterpret_cast<void *>(DSOHandle
));
484 ORC_RT_INTERFACE orc_rt_CWrapperFunctionResult
485 __orc_rt_elfnix_platform_shutdown(char *ArgData
, size_t ArgSize
) {
486 ELFNixPlatformRuntimeState::destroy();
487 return WrapperFunctionResult().release();
490 /// Wrapper function for registering metadata on a per-object basis.
491 ORC_RT_INTERFACE orc_rt_CWrapperFunctionResult
492 __orc_rt_elfnix_register_object_sections(char *ArgData
, size_t ArgSize
) {
493 return WrapperFunction
<SPSError(SPSELFNixPerObjectSectionsToRegister
)>::
494 handle(ArgData
, ArgSize
,
495 [](ELFNixPerObjectSectionsToRegister
&POSR
) {
496 return ELFNixPlatformRuntimeState::get().registerObjectSections(
502 /// Wrapper for releasing per-object metadat.
503 ORC_RT_INTERFACE orc_rt_CWrapperFunctionResult
504 __orc_rt_elfnix_deregister_object_sections(char *ArgData
, size_t ArgSize
) {
505 return WrapperFunction
<SPSError(SPSELFNixPerObjectSectionsToRegister
)>::
506 handle(ArgData
, ArgSize
,
507 [](ELFNixPerObjectSectionsToRegister
&POSR
) {
508 return ELFNixPlatformRuntimeState::get()
509 .deregisterObjectSections(std::move(POSR
));
514 //------------------------------------------------------------------------------
516 //------------------------------------------------------------------------------
518 ORC_RT_INTERFACE
void *__orc_rt_elfnix_tls_get_addr_impl(TLSInfoEntry
*D
) {
519 auto *TLVMgr
= static_cast<ELFNixPlatformRuntimeTLVManager
*>(
520 pthread_getspecific(D
->Key
));
522 TLVMgr
= new ELFNixPlatformRuntimeTLVManager();
523 if (pthread_setspecific(D
->Key
, TLVMgr
)) {
524 __orc_rt_log_error("Call to pthread_setspecific failed");
528 return TLVMgr
->getInstance(
529 reinterpret_cast<char *>(static_cast<uintptr_t>(D
->DataAddress
)));
532 ORC_RT_INTERFACE
ptrdiff_t ___orc_rt_elfnix_tlsdesc_resolver_impl(
533 TLSDescriptor
*D
, const char *ThreadPointer
) {
534 const char *TLVPtr
= reinterpret_cast<const char *>(
535 __orc_rt_elfnix_tls_get_addr_impl(D
->InfoEntry
));
536 return TLVPtr
- ThreadPointer
;
539 ORC_RT_INTERFACE orc_rt_CWrapperFunctionResult
540 __orc_rt_elfnix_create_pthread_key(char *ArgData
, size_t ArgSize
) {
541 return WrapperFunction
<SPSExpected
<uint64_t>(void)>::handle(
543 []() -> Expected
<uint64_t> {
545 if (int Err
= pthread_key_create(&Key
, destroyELFNixTLVMgr
)) {
546 __orc_rt_log_error("Call to pthread_key_create failed");
547 return make_error
<StringError
>(strerror(Err
));
549 return static_cast<uint64_t>(Key
);
554 //------------------------------------------------------------------------------
555 // cxa_atexit support
556 //------------------------------------------------------------------------------
558 int __orc_rt_elfnix_cxa_atexit(void (*func
)(void *), void *arg
,
560 return ELFNixPlatformRuntimeState::get().registerAtExit(func
, arg
,
564 int __orc_rt_elfnix_atexit(void (*func
)(void *)) {
565 auto &PlatformRTState
= ELFNixPlatformRuntimeState::get();
566 return ELFNixPlatformRuntimeState::get().registerAtExit(
567 func
, NULL
, PlatformRTState
.getPlatformJDDSOHandle());
570 void __orc_rt_elfnix_cxa_finalize(void *dso_handle
) {
571 ELFNixPlatformRuntimeState::get().runAtExits(dso_handle
);
574 //------------------------------------------------------------------------------
575 // JIT'd dlfcn alternatives.
576 //------------------------------------------------------------------------------
578 const char *__orc_rt_elfnix_jit_dlerror() {
579 return ELFNixPlatformRuntimeState::get().dlerror();
582 void *__orc_rt_elfnix_jit_dlopen(const char *path
, int mode
) {
583 return ELFNixPlatformRuntimeState::get().dlopen(path
, mode
);
586 int __orc_rt_elfnix_jit_dlclose(void *dso_handle
) {
587 return ELFNixPlatformRuntimeState::get().dlclose(dso_handle
);
590 void *__orc_rt_elfnix_jit_dlsym(void *dso_handle
, const char *symbol
) {
591 return ELFNixPlatformRuntimeState::get().dlsym(dso_handle
, symbol
);
594 //------------------------------------------------------------------------------
595 // ELFNix Run Program
596 //------------------------------------------------------------------------------
598 ORC_RT_INTERFACE
int64_t __orc_rt_elfnix_run_program(
599 const char *JITDylibName
, const char *EntrySymbolName
, int argc
,
601 using MainTy
= int (*)(int, char *[]);
603 void *H
= __orc_rt_elfnix_jit_dlopen(JITDylibName
,
604 __orc_rt::elfnix::ORC_RT_RTLD_LAZY
);
606 __orc_rt_log_error(__orc_rt_elfnix_jit_dlerror());
611 reinterpret_cast<MainTy
>(__orc_rt_elfnix_jit_dlsym(H
, EntrySymbolName
));
614 __orc_rt_log_error(__orc_rt_elfnix_jit_dlerror());
618 int Result
= Main(argc
, argv
);
620 if (__orc_rt_elfnix_jit_dlclose(H
) == -1)
621 __orc_rt_log_error(__orc_rt_elfnix_jit_dlerror());