Skip to content

Commit cc32df9

Browse files
authored
Merge pull request swiftlang#38663 from augusto2112/read-macho-sections-together
Revert "Read machO sections section by section"
2 parents d5ae718 + 104c69c commit cc32df9

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

include/swift/Reflection/ReflectionContext.h

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,36 @@ class ReflectionContext
205205
return false;
206206

207207
auto Slide = ImageStart.getAddressData() - Command->vmaddr;
208+
std::string Prefix = "__swift5";
209+
uint64_t RangeStart = UINT64_MAX;
210+
uint64_t RangeEnd = UINT64_MAX;
208211
auto SectionsBuf = reinterpret_cast<const char *>(Sections.get());
212+
for (unsigned I = 0; I < NumSect; ++I) {
213+
auto S = reinterpret_cast<typename T::Section *>(
214+
SectionsBuf + (I * sizeof(typename T::Section)));
215+
if (strncmp(S->sectname, Prefix.c_str(), strlen(Prefix.c_str())) != 0)
216+
continue;
217+
if (RangeStart == UINT64_MAX && RangeEnd == UINT64_MAX) {
218+
RangeStart = S->addr + Slide;
219+
RangeEnd = S->addr + S->size + Slide;
220+
continue;
221+
}
222+
RangeStart = std::min(RangeStart, (uint64_t)S->addr + Slide);
223+
RangeEnd = std::max(RangeEnd, (uint64_t)(S->addr + S->size + Slide));
224+
// Keep the range rounded to 8 byte alignment on both ends so we don't
225+
// introduce misaligned pointers mapping between local and remote
226+
// address space.
227+
RangeStart = RangeStart & ~7;
228+
RangeEnd = RangeEnd + 7 & ~7;
229+
}
230+
231+
if (RangeStart == UINT64_MAX && RangeEnd == UINT64_MAX)
232+
return false;
233+
234+
auto SectBuf = this->getReader().readBytes(RemoteAddress(RangeStart),
235+
RangeEnd - RangeStart);
236+
if (!SectBuf)
237+
return false;
209238

210239
auto findMachOSectionByName = [&](llvm::StringRef Name)
211240
-> std::pair<RemoteRef<void>, uint64_t> {
@@ -215,13 +244,11 @@ class ReflectionContext
215244
if (strncmp(S->sectname, Name.data(), strlen(Name.data())) != 0)
216245
continue;
217246
auto RemoteSecStart = S->addr + Slide;
218-
auto LocalSectBuf =
219-
this->getReader().readBytes(RemoteAddress(RemoteSecStart), S->size);
220-
if (!LocalSectBuf)
221-
return {nullptr, 0};
222-
223-
auto StartRef = RemoteRef<void>(RemoteSecStart, LocalSectBuf.get());
224-
savedBuffers.push_back(std::move(LocalSectBuf));
247+
auto SectBufData = reinterpret_cast<const char *>(SectBuf.get());
248+
auto LocalSectStart =
249+
reinterpret_cast<const char *>(SectBufData + RemoteSecStart - RangeStart);
250+
251+
auto StartRef = RemoteRef<void>(RemoteSecStart, LocalSectStart);
225252
return {StartRef, S->size};
226253
}
227254
return {nullptr, 0};
@@ -280,6 +307,7 @@ class ReflectionContext
280307
}
281308

282309
savedBuffers.push_back(std::move(Buf));
310+
savedBuffers.push_back(std::move(SectBuf));
283311
savedBuffers.push_back(std::move(Sections));
284312
return true;
285313
}

0 commit comments

Comments
 (0)