read section headers table from file contents

This commit is contained in:
justiceadams
2025-09-09 15:27:36 -07:00
parent f244c8f5b4
commit 07ed0823fb
2 changed files with 24 additions and 13 deletions

View File

@@ -528,22 +528,26 @@ public:
// until the end of the function, so we store it inside ReadDataBuffer.
// We do this so in both cases we can return a simple pointer.
std::vector<MemoryReader::ReadBytesResult> ReadDataBuffer;
auto readData = [&](uint64_t Offset, uint64_t Size) -> const void * {
auto readFileData = [&](uint64_t FileOffset, uint64_t Size) -> const void * {
if (FileBuffer.has_value()) {
auto Buffer = FileBuffer.value();
if (Offset + Size > Buffer.allocatedSize())
if (FileOffset + Size > Buffer.allocatedSize())
return nullptr;
return (const void *)((uint64_t)Buffer.base() + Offset);
return (const void *)((uint64_t)Buffer.base() + FileOffset);
} else {
MemoryReader::ReadBytesResult Buf =
this->getReader().readBytes(ImageStart + Offset, Size);
if (!Buf)
return nullptr;
ReadDataBuffer.push_back(std::move(Buf));
return ReadDataBuffer.back().get();
return nullptr;
}
};
auto readData = [&](uint64_t VirtualAddr, uint64_t Size) -> const void * {
MemoryReader::ReadBytesResult Buf =
this->getReader().readBytes(ImageStart + VirtualAddr, Size);
if (!Buf)
return nullptr;
ReadDataBuffer.push_back(std::move(Buf));
return ReadDataBuffer.back().get();
};
const void *Buf = readData(0, sizeof(typename T::Header));
if (!Buf)
return {};
@@ -591,7 +595,7 @@ public:
typename T::Offset StrTabOffset = SecHdrStrTab->sh_offset;
typename T::Size StrTabSize = SecHdrStrTab->sh_size;
auto StrTabBuf = readData(StrTabOffset, StrTabSize);
auto StrTabBuf = readFileData(StrTabOffset, StrTabSize);
if (!StrTabBuf)
return {};
auto StrTab = reinterpret_cast<const char *>(StrTabBuf);
@@ -802,7 +806,8 @@ public:
/// On success returns the ID of the newly registered Reflection Info.
std::optional<uint32_t>
addImage(RemoteAddress ImageStart,
llvm::SmallVector<llvm::StringRef, 1> PotentialModuleNames = {}) {
llvm::SmallVector<llvm::StringRef, 1> PotentialModuleNames = {},
std::optional<llvm::sys::MemoryBlock> FileBuffer = {}) {
// Read the first few bytes to look for a magic header.
auto Magic = this->getReader().readBytes(ImageStart, sizeof(uint32_t));
if (!Magic)
@@ -833,7 +838,7 @@ public:
&& MagicBytes[1] == llvm::ELF::ElfMagic[1]
&& MagicBytes[2] == llvm::ELF::ElfMagic[2]
&& MagicBytes[3] == llvm::ELF::ElfMagic[3]) {
return readELF(ImageStart, std::optional<llvm::sys::MemoryBlock>(),
return readELF(ImageStart, FileBuffer,
PotentialModuleNames);
}

View File

@@ -581,7 +581,13 @@ std::unique_ptr<ReflectionContextHolder> makeReflectionContextForMetadataReader(
auto context = new ReflectionContext(reader);
auto &builder = context->getBuilder();
for (unsigned i = 0, e = reader->getImages().size(); i < e; ++i) {
context->addImage(reader->getImageStartAddress(i));
// Create a file buffer for the image in case anything needs to slice the file contents directly
StringRef fileData = reader->getImages()[i].TheImage.getObjectFile()->getData();
llvm::sys::MemoryBlock memBlock(const_cast<void*>(static_cast<const void*>(fileData.data())),
fileData.size());
context->addImage(reader->getImageStartAddress(i), {}, memBlock);
}
ReflectionContextHolder *holder = new ReflectionContextHolder{