-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathImageInspectionCommon.cpp
185 lines (158 loc) · 5.47 KB
/
ImageInspectionCommon.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//===--- ImageInspectionCommon.cpp - Image inspection routines --*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
///
/// \file
///
/// This file unifies common ELF and COFF image inspection routines
///
//===----------------------------------------------------------------------===//
#ifndef SWIFT_RUNTIME_IMAGEINSPECTIONCOMMON_H
#define SWIFT_RUNTIME_IMAGEINSPECTIONCOMMON_H
#if !defined(__MACH__)
#include "../SwiftShims/Visibility.h"
#include "../SwiftShims/MetadataSections.h"
#include "ImageInspection.h"
namespace swift {
static swift::MetadataSections *registered = nullptr;
void record(swift::MetadataSections *sections) {
if (registered == nullptr) {
registered = sections;
sections->next = sections->prev = sections;
} else {
registered->prev->next = sections;
sections->next = registered;
sections->prev = registered->prev;
registered->prev = sections;
}
}
}
SWIFT_RUNTIME_EXPORT
void swift_addNewDSOImage(const void *addr) {
// We cast off the const in order to update the linked list
// data structure. This is safe to do since we don't touch
// any other fields.
swift::MetadataSections *sections =
static_cast<swift::MetadataSections *>(const_cast<void *>(addr));
record(sections);
const auto &protocols_section = sections->swift5_protocols;
const void *protocols = reinterpret_cast<void *>(protocols_section.start);
if (protocols_section.length)
swift::addImageProtocolsBlockCallback(protocols, protocols_section.length);
const auto &protocol_conformances = sections->swift5_protocol_conformances;
const void *conformances =
reinterpret_cast<void *>(protocol_conformances.start);
if (protocol_conformances.length)
swift::addImageProtocolConformanceBlockCallback(conformances,
protocol_conformances.length);
const auto &type_metadata = sections->swift5_type_metadata;
const void *metadata = reinterpret_cast<void *>(type_metadata.start);
if (type_metadata.length)
swift::addImageTypeMetadataRecordBlockCallback(metadata, type_metadata.length);
const auto &dynamic_replacements = sections->swift5_replace;
const auto *replacements =
reinterpret_cast<void *>(dynamic_replacements.start);
if (dynamic_replacements.length) {
const auto &dynamic_replacements_some = sections->swift5_replac2;
const auto *replacements_some =
reinterpret_cast<void *>(dynamic_replacements_some.start);
swift::addImageDynamicReplacementBlockCallback(
replacements, dynamic_replacements.length, replacements_some,
dynamic_replacements_some.length);
}
}
void swift::initializeProtocolLookup() {
const swift::MetadataSections *sections = registered;
while (true) {
const swift::MetadataSectionRange &protocols =
sections->swift5_protocols;
if (protocols.length)
addImageProtocolsBlockCallbackUnsafe(
reinterpret_cast<void *>(protocols.start), protocols.length);
if (sections->next == registered)
break;
sections = sections->next;
}
}
void swift::initializeProtocolConformanceLookup() {
const swift::MetadataSections *sections = registered;
while (true) {
const swift::MetadataSectionRange &conformances =
sections->swift5_protocol_conformances;
if (conformances.length)
addImageProtocolConformanceBlockCallbackUnsafe(
reinterpret_cast<void *>(conformances.start), conformances.length);
if (sections->next == registered)
break;
sections = sections->next;
}
}
void swift::initializeTypeMetadataRecordLookup() {
const swift::MetadataSections *sections = registered;
while (true) {
const swift::MetadataSectionRange &type_metadata =
sections->swift5_type_metadata;
if (type_metadata.length)
addImageTypeMetadataRecordBlockCallbackUnsafe(
reinterpret_cast<void *>(type_metadata.start), type_metadata.length);
if (sections->next == registered)
break;
sections = sections->next;
}
}
void swift::initializeDynamicReplacementLookup() {
}
SWIFT_RUNTIME_EXPORT
const swift::MetadataSections *swift_getMetadataSection(size_t index) {
#ifndef NDEBUG
if (swift::registered == nullptr) {
return nullptr;
}
auto selected = swift::registered;
while (index > 0) {
selected = selected->next;
if (selected == swift::registered) {
return nullptr;
}
--index;
}
return selected;
#else // NDEBUG
return nullptr;
#endif // else NDEBUG
}
SWIFT_RUNTIME_EXPORT
const char *swift_getMetadataSectionName(void *metadata_section) {
#ifndef NDEBUG
swift::SymbolInfo info;
if (lookupSymbol(metadata_section, &info)) {
if (info.fileName) {
return info.fileName;
}
}
#endif // NDEBUG
return "";
}
SWIFT_RUNTIME_EXPORT
size_t swift_getMetadataSectionCount() {
#ifndef NDEBUG
if (swift::registered == nullptr)
return 0;
size_t count = 1;
for (const auto *current = swift::registered->next;
current != swift::registered; current = current->next, ++count);
return count;
#else // NDEBUG
return 0;
#endif // else NDEBUG
}
#endif // !defined(__MACH__)
#endif // SWIFT_RUNTIME_IMAGEINSPECTIONCOMMON_H