@@ -123,7 +123,6 @@ class ReflectionContext
123
123
return sizeof (StoredPointer) * 2 ;
124
124
}
125
125
126
- #if defined(__APPLE__) && defined(__MACH__)
127
126
template <typename T> bool readMachOSections (RemoteAddress ImageStart) {
128
127
auto Buf =
129
128
this ->getReader ().readBytes (ImageStart, sizeof (typename T::Header));
@@ -277,22 +276,6 @@ class ReflectionContext
277
276
return true ;
278
277
}
279
278
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)
296
279
bool readPECOFFSections (RemoteAddress ImageStart) {
297
280
auto DOSHdrBuf = this ->getReader ().readBytes (
298
281
ImageStart, sizeof (llvm::object::dos_header));
@@ -384,15 +367,13 @@ class ReflectionContext
384
367
return true ;
385
368
}
386
369
387
- bool addImage (RemoteAddress ImageStart) {
370
+ bool readPECOFF (RemoteAddress ImageStart) {
388
371
auto Buf = this ->getReader ().readBytes (ImageStart,
389
372
sizeof (llvm::object::dos_header));
390
373
if (!Buf)
391
374
return false ;
392
375
393
376
auto DOSHdr = reinterpret_cast <const llvm::object::dos_header *>(Buf.get ());
394
- if (!(DOSHdr->Magic [0 ] == ' M' && DOSHdr->Magic [1 ] == ' Z' ))
395
- return false ;
396
377
397
378
auto PEHeaderAddress =
398
379
ImageStart.getAddressData () + DOSHdr->AddressOfNewExeHeader ;
@@ -407,7 +388,7 @@ class ReflectionContext
407
388
408
389
return readPECOFFSections (ImageStart);
409
390
}
410
- # else // ELF platforms.
391
+
411
392
template <typename T> bool readELFSections (RemoteAddress ImageStart) {
412
393
auto Buf =
413
394
this ->getReader ().readBytes (ImageStart, sizeof (typename T::Header));
@@ -506,8 +487,8 @@ class ReflectionContext
506
487
savedBuffers.push_back (std::move (Buf));
507
488
return true ;
508
489
}
509
-
510
- bool addImage (RemoteAddress ImageStart) {
490
+
491
+ bool readELF (RemoteAddress ImageStart) {
511
492
auto Buf =
512
493
this ->getReader ().readBytes (ImageStart, sizeof (llvm::ELF::Elf64_Ehdr));
513
494
@@ -527,12 +508,48 @@ class ReflectionContext
527
508
return false ;
528
509
}
529
510
}
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
+ }
531
548
532
549
void addReflectionInfo (ReflectionInfo I) {
533
550
getBuilder ().addReflectionInfo (I);
534
551
}
535
-
552
+
536
553
bool ownsObject (RemoteAddress ObjectAddress) {
537
554
auto MetadataAddress = readMetadataFromInstance (ObjectAddress.getAddressData ());
538
555
if (!MetadataAddress)
0 commit comments