Skip to content

Commit 33fa33f

Browse files
author
Alexander Shaposhnikov
committed
[Reflection] Fix several issues for COFF and ELF
Previously when ReflectionContext was parsing the image of a binary (for ELF or COFF) it was making some incorrect assumptions about the location of sections in the memory of a remote process. In particular, it was using the offsets rather than the virtual addresses and it was incorrectly calculating the references (relative pointers) inside the metadata. In this diff we address these issues and adjust swift-reflection-dump (used in tests) to emulate the runtime behavior more closely. This diff has been extensively tested, the reflection tests are green on OSX and Linux, on Windows it fixes 2 new tests: Reflection/typeref_decoding.swift stdlib/ReflectionHashing.swift So now (with some minor fixes to the lit testsing infrastructure) the following reflection tests pass: Reflection/box_descriptors.sil Reflection/capture_descriptors.sil Reflection/typeref_decoding.swift stdlib/ReflectionHashing.swift
1 parent d909dd1 commit 33fa33f

File tree

2 files changed

+197
-145
lines changed

2 files changed

+197
-145
lines changed

include/swift/Reflection/ReflectionContext.h

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -322,14 +322,21 @@ class ReflectionContext
322322
: llvm::StringRef(COFFSec->Name, llvm::COFF::NameSize);
323323
if (SectionName != Name)
324324
continue;
325-
auto Addr = ImageStart.getAddressData() + COFFSec->PointerToRawData;
325+
auto Addr = ImageStart.getAddressData() + COFFSec->VirtualAddress;
326326
auto Buf = this->getReader().readBytes(RemoteAddress(Addr),
327327
COFFSec->VirtualSize);
328328
const char *Begin = reinterpret_cast<const char *>(Buf.get());
329329
const char *End = Begin + COFFSec->VirtualSize;
330330
savedBuffers.push_back(std::move(Buf));
331-
return {{Begin, End},
332-
COFFSec->VirtualAddress - COFFSec->PointerToRawData};
331+
332+
// FIXME: This code needs to be cleaned up and updated
333+
// to make it work for 32 bit platforms.
334+
if (SectionName != ".sw5cptr") {
335+
Begin += 8;
336+
End -= 8;
337+
}
338+
339+
return {{Begin, End}, 0};
333340
}
334341
return {{nullptr, nullptr}, 0};
335342
};
@@ -338,20 +345,15 @@ class ReflectionContext
338345
findCOFFSectionByName(".sw5cptr");
339346
std::pair<std::pair<const char *, const char *>, uint32_t> TypeRefMdSec =
340347
findCOFFSectionByName(".sw5tyrf");
341-
342-
// FIXME: Make use of .sw5flmd section (the section content appears to be
343-
// incorrect on Windows at the moment).
344-
std::pair<std::pair<const char *, const char *>, uint32_t> FieldMdSec = {
345-
{nullptr, nullptr}, 0};
346-
// FIXME: Make use of .sw5asty.
347-
std::pair<std::pair<const char *, const char *>, uint32_t> AssocTySec = {
348-
{nullptr, nullptr}, 0};
349-
// FIXME: Make use of .sw5bltn.
350-
std::pair<std::pair<const char *, const char *>, uint32_t> BuiltinTySec = {
351-
{nullptr, nullptr}, 0};
352-
// FIXME: Make use of .sw5repl.
353-
std::pair<std::pair<const char *, const char *>, uint32_t> ReflStrMdSec = {
354-
{nullptr, nullptr}, 0};
348+
std::pair<std::pair<const char *, const char *>, uint32_t> FieldMdSec =
349+
findCOFFSectionByName(".sw5flmd");
350+
std::pair<std::pair<const char *, const char *>, uint32_t> AssocTySec =
351+
findCOFFSectionByName(".sw5asty");
352+
// FIXME: Use the section .sw5bltn instead.
353+
std::pair<std::pair<const char *, const char *>, uint32_t> BuiltinTySec =
354+
{{nullptr, nullptr}, 0};
355+
std::pair<std::pair<const char *, const char *>, uint32_t> ReflStrMdSec =
356+
findCOFFSectionByName(".sw5rfst");
355357

356358
if (FieldMdSec.first.first == nullptr &&
357359
AssocTySec.first.first == nullptr &&
@@ -457,12 +459,11 @@ class ReflectionContext
457459
if (SecName != Name)
458460
continue;
459461
auto SecStart =
460-
RemoteAddress(ImageStart.getAddressData() + Hdr->sh_offset);
462+
RemoteAddress(ImageStart.getAddressData() + Hdr->sh_addr);
461463
auto SecSize = Hdr->sh_size;
462464
auto SecBuf = this->getReader().readBytes(SecStart, SecSize);
463465
auto SecContents = reinterpret_cast<const char *>(SecBuf.get());
464-
return {{SecContents, SecContents + SecSize},
465-
Hdr->sh_addr - Hdr->sh_offset};
466+
return {{SecContents, SecContents + SecSize}, 0};
466467
}
467468
return {{nullptr, nullptr}, 0};
468469
};

0 commit comments

Comments
 (0)