Skip to content

Commit c5f8110

Browse files
committed
When ProcessMachCore has metadata for a binary, don't scan
Mach-O corefiles have several possible types of metadata for the binaries that were running when the corefile was written. ProcessMachCore will try to find these binaries, and load them. When we have a hint, but could not find the binary, this change makes ProcessMachCore not fall back to scanning the corefile looking for ANY binary that it could load. We sometimes have multiple binaries present in the memory in a corefile, but only the correct binary should be loaded, the others are data. Differential Revision: https://reviews.llvm.org/D157168 rdar://112602508
1 parent 9cb7250 commit c5f8110

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

lldb/source/Plugins/Process/mach-core/ProcessMachCore.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -223,16 +223,20 @@ void ProcessMachCore::CreateMemoryRegions() {
223223
}
224224
}
225225

226-
void ProcessMachCore::LoadBinariesViaMetadata() {
226+
bool ProcessMachCore::LoadBinariesViaMetadata() {
227227
Log *log(GetLog(LLDBLog::DynamicLoader | LLDBLog::Process));
228228
ObjectFile *core_objfile = m_core_module_sp->GetObjectFile();
229-
bool found_main_binary_definitively = false;
230229

231230
addr_t objfile_binary_value;
232231
bool objfile_binary_value_is_offset;
233232
UUID objfile_binary_uuid;
234233
ObjectFile::BinaryType type;
235234

235+
// This will be set to true if we had a metadata hint
236+
// specifying a UUID or address -- and we should not fall back
237+
// to doing an exhaustive search.
238+
bool found_binary_spec_in_metadata = false;
239+
236240
if (core_objfile->GetCorefileMainBinaryInfo(objfile_binary_value,
237241
objfile_binary_value_is_offset,
238242
objfile_binary_uuid, type)) {
@@ -244,14 +248,14 @@ void ProcessMachCore::LoadBinariesViaMetadata() {
244248
objfile_binary_uuid.GetAsString().c_str(),
245249
objfile_binary_value, objfile_binary_value_is_offset, type);
246250
}
251+
found_binary_spec_in_metadata = true;
247252

248253
// If this is the xnu kernel, don't load it now. Note the correct
249254
// DynamicLoader plugin to use, and the address of the kernel, and
250255
// let the DynamicLoader handle the finding & loading of the binary.
251256
if (type == ObjectFile::eBinaryTypeKernel) {
252257
m_mach_kernel_addr = objfile_binary_value;
253258
m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic();
254-
found_main_binary_definitively = true;
255259
} else if (type == ObjectFile::eBinaryTypeUser) {
256260
m_dyld_addr = objfile_binary_value;
257261
m_dyld_plugin_name = DynamicLoaderMacOSXDYLD::GetPluginNameStatic();
@@ -265,7 +269,6 @@ void ProcessMachCore::LoadBinariesViaMetadata() {
265269
objfile_binary_value, objfile_binary_value_is_offset,
266270
force_symbol_search, notify, set_address_in_target,
267271
allow_memory_image_last_resort)) {
268-
found_main_binary_definitively = true;
269272
m_dyld_plugin_name = DynamicLoaderStatic::GetPluginNameStatic();
270273
}
271274
}
@@ -276,7 +279,6 @@ void ProcessMachCore::LoadBinariesViaMetadata() {
276279
// load command is present, let's use the contents.
277280
UUID ident_uuid;
278281
addr_t ident_binary_addr = LLDB_INVALID_ADDRESS;
279-
if (!found_main_binary_definitively) {
280282
std::string corefile_identifier = core_objfile->GetIdentifierString();
281283

282284
// Search for UUID= and stext= strings in the identifier str.
@@ -287,6 +289,7 @@ void ProcessMachCore::LoadBinariesViaMetadata() {
287289
if (log)
288290
log->Printf("Got a UUID from LC_IDENT/kern ver str LC_NOTE: %s",
289291
ident_uuid.GetAsString().c_str());
292+
found_binary_spec_in_metadata = true;
290293
}
291294
if (corefile_identifier.find("stext=") != std::string::npos) {
292295
size_t p = corefile_identifier.find("stext=") + strlen("stext=");
@@ -297,6 +300,7 @@ void ProcessMachCore::LoadBinariesViaMetadata() {
297300
log->Printf("Got a load address from LC_IDENT/kern ver str "
298301
"LC_NOTE: 0x%" PRIx64,
299302
ident_binary_addr);
303+
found_binary_spec_in_metadata = true;
300304
}
301305
}
302306

@@ -309,7 +313,7 @@ void ProcessMachCore::LoadBinariesViaMetadata() {
309313
"ProcessMachCore::LoadBinariesViaMetadata: Found kernel binary via "
310314
"LC_IDENT/kern ver str LC_NOTE");
311315
m_mach_kernel_addr = ident_binary_addr;
312-
found_main_binary_definitively = true;
316+
found_binary_spec_in_metadata = true;
313317
} else if (ident_uuid.IsValid()) {
314318
// We have no address specified, only a UUID. Load it at the file
315319
// address.
@@ -322,16 +326,15 @@ void ProcessMachCore::LoadBinariesViaMetadata() {
322326
this, llvm::StringRef(), ident_uuid, ident_binary_addr,
323327
value_is_offset, force_symbol_search, notify,
324328
set_address_in_target, allow_memory_image_last_resort)) {
325-
found_main_binary_definitively = true;
329+
found_binary_spec_in_metadata = true;
326330
m_dyld_plugin_name = DynamicLoaderStatic::GetPluginNameStatic();
327331
}
328332
}
329-
}
330333

331334
// Finally, load any binaries noted by "load binary" LC_NOTEs in the
332335
// corefile
333336
if (core_objfile->LoadCoreFileImages(*this)) {
334-
found_main_binary_definitively = true;
337+
found_binary_spec_in_metadata = true;
335338
m_dyld_plugin_name = DynamicLoaderStatic::GetPluginNameStatic();
336339
}
337340

@@ -341,6 +344,8 @@ void ProcessMachCore::LoadBinariesViaMetadata() {
341344
// un-set it later.
342345
if (m_dyld_up)
343346
m_dyld_plugin_name = GetDynamicLoader()->GetPluginName();
347+
348+
return found_binary_spec_in_metadata;
344349
}
345350

346351
void ProcessMachCore::LoadBinariesViaExhaustiveSearch() {
@@ -417,8 +422,8 @@ void ProcessMachCore::LoadBinariesViaExhaustiveSearch() {
417422
void ProcessMachCore::LoadBinariesAndSetDYLD() {
418423
Log *log(GetLog(LLDBLog::DynamicLoader | LLDBLog::Process));
419424

420-
LoadBinariesViaMetadata();
421-
if (m_dyld_plugin_name.empty())
425+
bool found_binary_spec_in_metadata = LoadBinariesViaMetadata();
426+
if (!found_binary_spec_in_metadata)
422427
LoadBinariesViaExhaustiveSearch();
423428

424429
if (m_dyld_plugin_name.empty()) {

lldb/source/Plugins/Process/mach-core/ProcessMachCore.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,13 @@ class ProcessMachCore : public lldb_private::PostMortemProcess {
8686

8787
private:
8888
void CreateMemoryRegions();
89-
void LoadBinariesViaMetadata();
89+
90+
/// \return
91+
/// True if any metadata were found indicating the binary that should
92+
/// be loaded, regardless of whether the specified binary could be found.
93+
/// False if no metadata were present.
94+
bool LoadBinariesViaMetadata();
95+
9096
void LoadBinariesViaExhaustiveSearch();
9197
void LoadBinariesAndSetDYLD();
9298
void CleanupMemoryRegionPermissions();

0 commit comments

Comments
 (0)