Skip to content

Commit 334d892

Browse files
authored
Merge pull request #27237 from jckarter/reflection-context-check-magic
ReflectionContext: Remove platform #ifs for image format checks.
2 parents 1d23ebf + 761b029 commit 334d892

File tree

1 file changed

+42
-25
lines changed

1 file changed

+42
-25
lines changed

include/swift/Reflection/ReflectionContext.h

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ class ReflectionContext
123123
return sizeof(StoredPointer) * 2;
124124
}
125125

126-
#if defined(__APPLE__) && defined(__MACH__)
127126
template <typename T> bool readMachOSections(RemoteAddress ImageStart) {
128127
auto Buf =
129128
this->getReader().readBytes(ImageStart, sizeof(typename T::Header));
@@ -277,22 +276,6 @@ class ReflectionContext
277276
return true;
278277
}
279278

280-
bool addImage(RemoteAddress ImageStart) {
281-
// We start reading 4 bytes. The first 4 bytes are supposed to be
282-
// the magic, so we understand whether this is a 32-bit executable or
283-
// a 64-bit one.
284-
auto Buf = this->getReader().readBytes(ImageStart, sizeof(uint32_t));
285-
if (!Buf)
286-
return false;
287-
auto HeaderMagic = reinterpret_cast<const uint32_t *>(Buf.get());
288-
if (*HeaderMagic == llvm::MachO::MH_MAGIC)
289-
return readMachOSections<MachOTraits<4>>(ImageStart);
290-
if (*HeaderMagic == llvm::MachO::MH_MAGIC_64)
291-
return readMachOSections<MachOTraits<8>>(ImageStart);
292-
return false;
293-
}
294-
295-
#elif defined(_WIN32)
296279
bool readPECOFFSections(RemoteAddress ImageStart) {
297280
auto DOSHdrBuf = this->getReader().readBytes(
298281
ImageStart, sizeof(llvm::object::dos_header));
@@ -384,15 +367,13 @@ class ReflectionContext
384367
return true;
385368
}
386369

387-
bool addImage(RemoteAddress ImageStart) {
370+
bool readPECOFF(RemoteAddress ImageStart) {
388371
auto Buf = this->getReader().readBytes(ImageStart,
389372
sizeof(llvm::object::dos_header));
390373
if (!Buf)
391374
return false;
392375

393376
auto DOSHdr = reinterpret_cast<const llvm::object::dos_header *>(Buf.get());
394-
if (!(DOSHdr->Magic[0] == 'M' && DOSHdr->Magic[1] == 'Z'))
395-
return false;
396377

397378
auto PEHeaderAddress =
398379
ImageStart.getAddressData() + DOSHdr->AddressOfNewExeHeader;
@@ -407,7 +388,7 @@ class ReflectionContext
407388

408389
return readPECOFFSections(ImageStart);
409390
}
410-
#else // ELF platforms.
391+
411392
template <typename T> bool readELFSections(RemoteAddress ImageStart) {
412393
auto Buf =
413394
this->getReader().readBytes(ImageStart, sizeof(typename T::Header));
@@ -506,8 +487,8 @@ class ReflectionContext
506487
savedBuffers.push_back(std::move(Buf));
507488
return true;
508489
}
509-
510-
bool addImage(RemoteAddress ImageStart) {
490+
491+
bool readELF(RemoteAddress ImageStart) {
511492
auto Buf =
512493
this->getReader().readBytes(ImageStart, sizeof(llvm::ELF::Elf64_Ehdr));
513494

@@ -527,12 +508,48 @@ class ReflectionContext
527508
return false;
528509
}
529510
}
530-
#endif
511+
512+
bool addImage(RemoteAddress ImageStart) {
513+
// Read the first few bytes to look for a magic header.
514+
auto Magic = this->getReader().readBytes(ImageStart, sizeof(uint32_t));
515+
if (!Magic)
516+
return false;
517+
518+
uint32_t MagicWord;
519+
memcpy(&MagicWord, Magic.get(), sizeof(MagicWord));
520+
521+
// 32- and 64-bit Mach-O.
522+
if (MagicWord == llvm::MachO::MH_MAGIC) {
523+
return readMachOSections<MachOTraits<4>>(ImageStart);
524+
}
525+
526+
if (MagicWord == llvm::MachO::MH_MAGIC_64) {
527+
return readMachOSections<MachOTraits<8>>(ImageStart);
528+
}
529+
530+
// PE. (This just checks for the DOS header; `readPECOFF` will further
531+
// validate the existence of the PE header.)
532+
auto MagicBytes = (const char*)Magic.get();
533+
if (MagicBytes[0] == 'M' && MagicBytes[1] == 'Z') {
534+
return readPECOFF(ImageStart);
535+
}
536+
537+
// ELF.
538+
if (MagicBytes[0] == llvm::ELF::ElfMagic[0]
539+
&& MagicBytes[1] == llvm::ELF::ElfMagic[1]
540+
&& MagicBytes[2] == llvm::ELF::ElfMagic[2]
541+
&& MagicBytes[3] == llvm::ELF::ElfMagic[3]) {
542+
return readELF(ImageStart);
543+
}
544+
545+
// We don't recognize the format.
546+
return false;
547+
}
531548

532549
void addReflectionInfo(ReflectionInfo I) {
533550
getBuilder().addReflectionInfo(I);
534551
}
535-
552+
536553
bool ownsObject(RemoteAddress ObjectAddress) {
537554
auto MetadataAddress = readMetadataFromInstance(ObjectAddress.getAddressData());
538555
if (!MetadataAddress)

0 commit comments

Comments
 (0)