@@ -126,7 +126,8 @@ class ReflectionContext
126
126
// / All buffers we need to keep around long term. This will automatically free them
127
127
// / when this object is destroyed.
128
128
std::vector<MemoryReader::ReadBytesResult> savedBuffers;
129
- std::vector<std::tuple<RemoteAddress, RemoteAddress>> imageRanges;
129
+ std::vector<std::tuple<RemoteAddress, RemoteAddress>> textRanges;
130
+ std::vector<std::tuple<RemoteAddress, RemoteAddress>> dataRanges;
130
131
131
132
bool setupTargetPointers = false ;
132
133
typename super::StoredPointer target_non_future_adapter = 0 ;
@@ -251,7 +252,7 @@ class ReflectionContext
251
252
uint64_t Offset = 0 ;
252
253
253
254
// Find the __TEXT segment.
254
- typename T::SegmentCmd *Command = nullptr ;
255
+ typename T::SegmentCmd *TextCommand = nullptr ;
255
256
for (unsigned I = 0 ; I < NumCommands; ++I) {
256
257
auto CmdBuf = this ->getReader ().readBytes (
257
258
RemoteAddress (CmdStartAddress.getAddressData () + Offset),
@@ -260,15 +261,15 @@ class ReflectionContext
260
261
return false ;
261
262
auto CmdHdr = reinterpret_cast <typename T::SegmentCmd *>(CmdBuf.get ());
262
263
if (strncmp (CmdHdr->segname , " __TEXT" , sizeof (CmdHdr->segname )) == 0 ) {
263
- Command = CmdHdr;
264
+ TextCommand = CmdHdr;
264
265
savedBuffers.push_back (std::move (CmdBuf));
265
266
break ;
266
267
}
267
268
Offset += CmdHdr->cmdsize ;
268
269
}
269
270
270
271
// No __TEXT segment, bail out.
271
- if (!Command )
272
+ if (!TextCommand )
272
273
return false ;
273
274
274
275
// Find the load command offset.
@@ -291,7 +292,7 @@ class ReflectionContext
291
292
if (!Sections)
292
293
return false ;
293
294
294
- auto Slide = ImageStart.getAddressData () - Command ->vmaddr ;
295
+ auto Slide = ImageStart.getAddressData () - TextCommand ->vmaddr ;
295
296
auto SectionsBuf = reinterpret_cast <const char *>(Sections.get ());
296
297
297
298
auto findMachOSectionByName = [&](llvm::StringRef Name)
@@ -355,22 +356,27 @@ class ReflectionContext
355
356
356
357
auto InfoID = this ->addReflectionInfo (info);
357
358
358
- // Find the __DATA segment.
359
+ auto TextSegmentStart = Slide + TextCommand->vmaddr ;
360
+ auto TextSegmentEnd = TextSegmentStart + TextCommand->vmsize ;
361
+ textRanges.push_back (std::make_tuple (RemoteAddress (TextSegmentStart),
362
+ RemoteAddress (TextSegmentEnd)));
363
+
364
+ // Find the __DATA segments.
359
365
for (unsigned I = 0 ; I < NumCommands; ++I) {
360
366
auto CmdBuf = this ->getReader ().readBytes (
361
367
RemoteAddress (CmdStartAddress.getAddressData () + Offset),
362
368
SegmentCmdHdrSize);
363
369
if (!CmdBuf)
364
370
return false ;
365
371
auto CmdHdr = reinterpret_cast <typename T::SegmentCmd *>(CmdBuf.get ());
366
- if (strncmp (CmdHdr->segname , " __DATA" , sizeof (CmdHdr->segname )) == 0 ) {
367
- auto DataSegmentEnd =
368
- ImageStart.getAddressData () + CmdHdr->vmaddr + CmdHdr->vmsize ;
369
- assert (DataSegmentEnd > ImageStart.getAddressData () &&
372
+ // Look for any segment name starting with __DATA.
373
+ if (strncmp (CmdHdr->segname , " __DATA" , 6 ) == 0 ) {
374
+ auto DataSegmentStart = Slide + CmdHdr->vmaddr ;
375
+ auto DataSegmentEnd = DataSegmentStart + CmdHdr->vmsize ;
376
+ assert (DataSegmentStart > ImageStart.getAddressData () &&
370
377
" invalid range for __DATA" );
371
- imageRanges.push_back (
372
- std::make_tuple (ImageStart, RemoteAddress (DataSegmentEnd)));
373
- break ;
378
+ dataRanges.push_back (std::make_tuple (RemoteAddress (DataSegmentStart),
379
+ RemoteAddress (DataSegmentEnd)));
374
380
}
375
381
Offset += CmdHdr->cmdsize ;
376
382
}
@@ -829,9 +835,11 @@ class ReflectionContext
829
835
return ownsAddress (RemoteAddress (*MetadataAddress));
830
836
}
831
837
832
- // / Returns true if the address falls within a registered image.
833
- bool ownsAddressRaw (RemoteAddress Address) {
834
- for (auto Range : imageRanges) {
838
+ // / Returns true if the address falls within the given address ranges.
839
+ bool ownsAddress (
840
+ RemoteAddress Address,
841
+ const std::vector<std::tuple<RemoteAddress, RemoteAddress>> &ranges) {
842
+ for (auto Range : ranges) {
835
843
auto Start = std::get<0 >(Range);
836
844
auto End = std::get<1 >(Range);
837
845
if (Start.getAddressData () <= Address.getAddressData ()
@@ -843,11 +851,14 @@ class ReflectionContext
843
851
}
844
852
845
853
// / Returns true if the address is known to the reflection context.
846
- // / Currently, that means that either the address falls within a registered
847
- // / image, or the address points to a Metadata whose type context descriptor
848
- // / is within a registered image.
854
+ // / Currently, that means that either the address falls within the text or
855
+ // / data segments of a registered image, or the address points to a Metadata
856
+ // / whose type context descriptor is within the text segment of a registered
857
+ // / image.
849
858
bool ownsAddress (RemoteAddress Address) {
850
- if (ownsAddressRaw (Address))
859
+ if (ownsAddress (Address, textRanges))
860
+ return true ;
861
+ if (ownsAddress (Address, dataRanges))
851
862
return true ;
852
863
853
864
// This is usually called on a Metadata address which might have been
@@ -856,7 +867,7 @@ class ReflectionContext
856
867
if (auto Metadata = readMetadata (Address.getAddressData ()))
857
868
if (auto DescriptorAddress =
858
869
super::readAddressOfNominalTypeDescriptor (Metadata, true ))
859
- if (ownsAddressRaw (RemoteAddress (DescriptorAddress)))
870
+ if (ownsAddress (RemoteAddress (DescriptorAddress), textRanges ))
860
871
return true ;
861
872
862
873
return false ;
@@ -1206,6 +1217,8 @@ class ReflectionContext
1206
1217
super::readAddressOfNominalTypeDescriptor (Metadata);
1207
1218
if (!DescriptorAddress)
1208
1219
return false ;
1220
+ if (!ownsAddress (RemoteAddress (DescriptorAddress), textRanges))
1221
+ return false ;
1209
1222
1210
1223
auto DescriptorBytes =
1211
1224
getReader ().readBytes (RemoteAddress (DescriptorAddress),
0 commit comments