Skip to content

Commit 30436f4

Browse files
committed
[runtime] ELF DSO binary section fixes
- Rename swift::addNewDSOImage() to swift_addNewDSOImage() and export using SWIFT_RUNTIME_EXPORT. - Move ELF specific parts of ImageInspection.h into ImageInspectionELF.h.
1 parent e07c0d2 commit 30436f4

File tree

5 files changed

+55
-32
lines changed

5 files changed

+55
-32
lines changed

stdlib/public/runtime/CMakeLists.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ if(SWIFT_RUNTIME_ENABLE_LEAK_CHECKER)
1818
set(swift_runtime_leaks_sources Leaks.mm)
1919
endif()
2020

21+
set(section_magic_compile_flags ${swift_runtime_compile_flags})
22+
2123
list(APPEND swift_runtime_compile_flags
2224
"-D__SWIFT_CURRENT_DYLIB=swiftCore")
2325

@@ -132,19 +134,19 @@ endforeach()
132134

133135
add_swift_library(section_magic_loader OBJECT_LIBRARY IS_STDLIB IS_STDLIB_CORE
134136
ImageInspectionInit.cpp
135-
C_COMPILE_FLAGS ${swift_runtime_compile_flags}
137+
C_COMPILE_FLAGS ${section_magic_compile_flags}
136138
TARGET_SDKS "${ELFISH_SDKS}"
137139
LINK_FLAGS ${swift_runtime_linker_flags}
138140
INSTALL_IN_COMPONENT never_install)
139141
add_swift_library(section_magic_begin OBJECT_LIBRARY IS_STDLIB IS_STDLIB_CORE
140142
swift_sections.S
141-
C_COMPILE_FLAGS ${swift_runtime_compile_flags} "-DSWIFT_BEGIN"
143+
C_COMPILE_FLAGS ${section_magic_compile_flags} "-DSWIFT_BEGIN"
142144
TARGET_SDKS "${ELFISH_SDKS}"
143145
LINK_FLAGS ${swift_runtime_linker_flags}
144146
INSTALL_IN_COMPONENT never_install)
145147
add_swift_library(section_magic_end OBJECT_LIBRARY IS_STDLIB IS_STDLIB_CORE
146148
swift_sections.S
147-
C_COMPILE_FLAGS ${swift_runtime_compile_flags} "-DSWIFT_END"
149+
C_COMPILE_FLAGS ${section_magic_compile_flags} "-DSWIFT_END"
148150
LINK_FLAGS ${swift_runtime_linker_flags}
149151
TARGET_SDKS "${ELFISH_SDKS}"
150152
INSTALL_IN_COMPONENT never_install)

stdlib/public/runtime/ImageInspection.h

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#ifndef SWIFT_RUNTIME_IMAGE_INSPECTION_H
2020
#define SWIFT_RUNTIME_IMAGE_INSPECTION_H
2121

22+
#include "ImageInspectionELF.h"
2223
#include <cstdint>
2324

2425
namespace swift {
@@ -30,18 +31,6 @@ namespace swift {
3031
void *symbolAddress;
3132
};
3233

33-
#if defined(__ELF__) || defined(__ANDROID__)
34-
35-
struct SectionInfo {
36-
uint64_t size;
37-
const char *data;
38-
};
39-
40-
// Called by injected constructors when a dynamic library is loaded.
41-
void addNewDSOImage(const void *addr);
42-
43-
#endif // defined(__ELF__) || defined(__ANDROID__)
44-
4534
/// Load the metadata from the image necessary to find a type's
4635
/// protocol conformance.
4736
void initializeProtocolConformanceLookup();

stdlib/public/runtime/ImageInspectionELF.cpp

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@
1919
#if defined(__ELF__) || defined(__ANDROID__)
2020

2121
#include "ImageInspection.h"
22-
#include "../SwiftShims/Visibility.h"
23-
#include <atomic>
22+
#include <dlfcn.h>
2423
#include <elf.h>
2524
#include <link.h>
26-
#include <dlfcn.h>
2725
#include <string.h>
2826

2927
using namespace swift;
@@ -37,29 +35,26 @@ static const char ProtocolConformancesSymbol[] =
3735
static const char TypeMetadataRecordsSymbol[] =
3836
".swift2_type_metadata_start";
3937

40-
static std::atomic<bool> didInitializeProtocolConformanceLookup;
41-
static std::atomic<bool> didInitializeTypeMetadataLookup;
42-
4338
/// Context arguments passed down from dl_iterate_phdr to its callback.
4439
struct InspectArgs {
4540
/// Symbol name to look up.
4641
const char *symbolName;
4742
/// Callback function to invoke with the metadata block.
4843
void (*addBlock)(const void *start, uintptr_t size);
4944
/// Set to true when initialize*Lookup() is called.
50-
std::atomic<bool> *didInitializeLookup;
45+
bool didInitializeLookup;
5146
};
5247

5348
static InspectArgs ProtocolConformanceArgs = {
5449
ProtocolConformancesSymbol,
5550
addImageProtocolConformanceBlockCallback,
56-
&didInitializeProtocolConformanceLookup
51+
false
5752
};
5853

5954
static InspectArgs TypeMetadataRecordArgs = {
6055
TypeMetadataRecordsSymbol,
6156
addImageTypeMetadataRecordBlockCallback,
62-
&didInitializeTypeMetadataLookup
57+
false
6358
};
6459

6560

@@ -84,15 +79,15 @@ static SectionInfo getSectionInfo(const char *imageName,
8479

8580
static int iteratePHDRCallback(struct dl_phdr_info *info,
8681
size_t size, void *data) {
87-
const InspectArgs *inspectArgs = reinterpret_cast<const InspectArgs *>(data);
82+
InspectArgs *inspectArgs = reinterpret_cast<InspectArgs *>(data);
8883
const char *fname = info->dlpi_name;
8984

9085
// While dl_iterate_phdr() is in progress it holds a lock to prevent other
9186
// images being loaded. The initialize flag is set here inside the callback so
9287
// that addNewDSOImage() sees a consistent state. If it was set outside the
9388
// dl_interate_phdr() call then it could result in images being missed or
9489
// added twice.
95-
inspectArgs->didInitializeLookup->store(true, std::memory_order_release);
90+
inspectArgs->didInitializeLookup = true;
9691

9792
if (fname == nullptr || fname[0] == '\0') {
9893
// The filename may be null for both the dynamic loader and main executable.
@@ -147,12 +142,12 @@ void swift::initializeTypeMetadataRecordLookup() {
147142
// dladdr() to dlopen() the image after the appropiate initialize*Lookup()
148143
// function has been called.
149144
SWIFT_RUNTIME_EXPORT
150-
void swift::addNewDSOImage(const void *addr) {
151-
if (didInitializeProtocolConformanceLookup.load(std::memory_order_acquire)) {
145+
void swift_addNewDSOImage(const void *addr) {
146+
if (ProtocolConformanceArgs.didInitializeLookup) {
152147
addBlockInImage(&ProtocolConformanceArgs, addr);
153148
}
154149

155-
if (didInitializeTypeMetadataLookup.load(std::memory_order_acquire)) {
150+
if (TypeMetadataRecordArgs.didInitializeLookup) {
156151
addBlockInImage(&TypeMetadataRecordArgs, addr);
157152
}
158153
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===--- ImageInspectionELF.h ---------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// ELF specific image inspection routines.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
#ifndef SWIFT_RUNTIME_IMAGE_INSPECTION_ELF_H
18+
#define SWIFT_RUNTIME_IMAGE_INSPECTION_ELF_H
19+
20+
#if defined(__ELF__) || defined(__ANDROID__)
21+
22+
#include "../SwiftShims/Visibility.h"
23+
#include <cstdint>
24+
25+
namespace swift {
26+
struct SectionInfo {
27+
uint64_t size;
28+
const char *data;
29+
};
30+
}
31+
32+
// Called by injected constructors when a dynamic library is loaded.
33+
SWIFT_RUNTIME_EXPORT
34+
void swift_addNewDSOImage(const void *addr);
35+
36+
#endif // defined(__ELF__) || defined(__ANDROID__)
37+
38+
#endif // SWIFT_RUNTIME_IMAGE_INSPECTION_ELF_H

stdlib/public/runtime/ImageInspectionInit.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@
2020
#include "ImageInspection.h"
2121
#include <memory>
2222

23-
using namespace swift;
24-
2523
// This is called at startup and by each shared object as it is dlopen()'d to
2624
// allow the section data for the object to be loaded.
2725
__attribute__((constructor))
2826
static void sectionDataInit() {
29-
addNewDSOImage(reinterpret_cast<void *>(std::addressof(sectionDataInit)));
27+
void *addr = reinterpret_cast<void *>(std::addressof(sectionDataInit));
28+
swift_addNewDSOImage(addr);
3029
}
3130

3231
#endif

0 commit comments

Comments
 (0)