Skip to content

Commit 32fc8f1

Browse files
author
Davide Italiano
committed
[ReflectionContext] Take slide in account.
1 parent fcc74e8 commit 32fc8f1

File tree

1 file changed

+15
-25
lines changed

1 file changed

+15
-25
lines changed

include/swift/Reflection/ReflectionContext.h

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,8 @@ class ReflectionContext
139139
if (!Command)
140140
return false;
141141

142-
// Read everything including the __TEXT segment.
143-
// Buf = this->getReader().readBytes(ImageStart, Command->vmsize);
144-
auto Start = reinterpret_cast<const char *>(Buf.get());
145-
146-
// Find the load command offset.
147-
auto loadCmdOffset = Start + Offset + sizeof(typename T::Header);
142+
// Find the load command offset.
143+
auto loadCmdOffset = ImageStart.getAddressData() + Offset + sizeof(typename T::Header);
148144

149145
// Read the load command.
150146
auto LoadCmdAddress = reinterpret_cast<const char *>(loadCmdOffset);
@@ -159,49 +155,43 @@ class ReflectionContext
159155
auto Sections = this->getReader().readBytes(
160156
RemoteAddress(SectAddress), NumSect * sizeof(typename T::Section));
161157

158+
auto Slide = ImageStart.getAddressData() - Command->vmaddr;
162159
std::string Prefix = "__swift5";
163160
uint64_t RangeStart = UINT64_MAX;
164161
uint64_t RangeEnd = UINT64_MAX;
162+
auto SectionsBuf = reinterpret_cast<const char *>(Sections.get());
165163
for (unsigned I = 0; I < NumSect; ++I) {
166164
auto S = reinterpret_cast<typename T::Section *>(
167-
SectAddress + (I * sizeof(typename T::Section)));
165+
SectionsBuf + (I * sizeof(typename T::Section)));
168166
if (strncmp(S->sectname, Prefix.c_str(), strlen(Prefix.c_str())) != 0)
169167
continue;
170168
if (RangeStart == UINT64_MAX && RangeEnd == UINT64_MAX) {
171-
RangeStart = S->addr;
172-
RangeEnd = S->addr + S->size;
169+
RangeStart = S->addr + Slide;
170+
RangeEnd = S->addr + S->size + Slide;
173171
continue;
174172
}
175-
RangeStart = std::min(RangeStart, (uint64_t)S->addr);
176-
RangeEnd = std::max(RangeEnd, (uint64_t)(S->addr + S->size));
173+
RangeStart = std::min(RangeStart, (uint64_t)S->addr + Slide);
174+
RangeEnd = std::max(RangeEnd, (uint64_t)(S->addr + S->size + Slide));
177175
}
178-
176+
179177
if (RangeStart == UINT64_MAX && RangeEnd == UINT64_MAX)
180178
return false;
181179

182-
auto Slide = ImageStart.getAddressData() - Command->vmaddr;
183180
auto SectBuf = this->getReader().readBytes(RemoteAddress(RangeStart),
184181
RangeEnd - RangeStart);
185182

186183
auto findMachOSectionByName = [&](std::string Name)
187184
-> std::pair<std::pair<const char *, const char *>, uint32_t> {
188-
auto cmdOffset = Start + Offset + sizeof(typename T::Header);
189-
auto SegCmd = reinterpret_cast<typename T::SegmentCmd *>(cmdOffset);
190-
auto SectAddress = reinterpret_cast<const char *>(cmdOffset) +
191-
sizeof(typename T::SegmentCmd);
192-
for (unsigned I = 0; I < SegCmd->nsects; ++I) {
185+
for (unsigned I = 0; I < NumSect; ++I) {
193186
auto S = reinterpret_cast<typename T::Section *>(
194-
SectAddress + (I * sizeof(typename T::Section)));
187+
SectionsBuf + (I * sizeof(typename T::Section)));
195188
if (strncmp(S->sectname, Name.c_str(), strlen(Name.c_str())) != 0)
196189
continue;
197-
auto Slide = ImageStart.getAddressData() - Command->vmaddr;
198190
auto RemoteSecStart = S->addr + Slide;
199-
auto LocalSectBuf =
200-
this->getReader().readBytes(RemoteAddress(RemoteSecStart), S->size);
191+
auto SectBufData = reinterpret_cast<const char *>(SectBuf.get());
201192
auto LocalSectStart =
202-
reinterpret_cast<const char *>(LocalSectBuf.get());
203-
auto LocalSectEnd =
204-
reinterpret_cast<const char *>(LocalSectBuf.get()) + S->size;
193+
reinterpret_cast<const char *>(SectBufData + RemoteSecStart - RangeStart);
194+
auto LocalSectEnd = reinterpret_cast<const char *>(LocalSectStart + S->size);
205195
return {{LocalSectStart, LocalSectEnd}, 0};
206196
}
207197
return {{nullptr, nullptr}, 0};

0 commit comments

Comments
 (0)