Skip to content

Commit 7d2a9aa

Browse files
committed
[runtime] Abort if dlopen() fails when loading ELF section data
1 parent 5c993b4 commit 7d2a9aa

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

stdlib/public/runtime/ImageInspectionELF.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#if defined(__ELF__) || defined(__ANDROID__)
2020

2121
#include "ImageInspection.h"
22+
#include "swift/Runtime/Debug.h"
2223
#include <dlfcn.h>
2324
#include <elf.h>
2425
#include <link.h>
@@ -63,17 +64,19 @@ static InspectArgs TypeMetadataRecordArgs = {
6364
static SectionInfo getSectionInfo(const char *imageName,
6465
const char *sectionName) {
6566
SectionInfo sectionInfo = { 0, nullptr };
66-
void *handle = dlopen(imageName, RTLD_LAZY);
67-
if (handle) {
68-
void *symbol = dlsym(handle, sectionName);
69-
if (symbol) {
70-
// Extract the size of the section data from the head of the section.
71-
const char *section = reinterpret_cast<const char *>(symbol);
72-
memcpy(&sectionInfo.size, section, sizeof(uint64_t));
73-
sectionInfo.data = section + sizeof(uint64_t);
74-
}
75-
dlclose(handle);
67+
void *handle = dlopen(imageName, RTLD_LAZY | RTLD_NOLOAD);
68+
if (!handle) {
69+
fatalError(/* flags = */ 0, "dlopen() failed on `%s': %s", imageName,
70+
dlerror());
71+
}
72+
void *symbol = dlsym(handle, sectionName);
73+
if (symbol) {
74+
// Extract the size of the section data from the head of the section.
75+
const char *section = reinterpret_cast<const char *>(symbol);
76+
memcpy(&sectionInfo.size, section, sizeof(uint64_t));
77+
sectionInfo.data = section + sizeof(uint64_t);
7678
}
79+
dlclose(handle);
7780
return sectionInfo;
7881
}
7982

0 commit comments

Comments
 (0)