Skip to content

Commit f2f9d70

Browse files
authored
Merge pull request #6417 from jasonmolenda/r106343477-dont-load-non-kexts-in-darwin-kernel-kextlist
Don't load non-kexts in darwin kernel debug; handle unslid segs
2 parents 83636c0 + 2700f63 commit f2f9d70

File tree

4 files changed

+75
-10
lines changed

4 files changed

+75
-10
lines changed

lldb/source/Plugins/DynamicLoader/Darwin-Kernel/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ add_lldb_library(lldbPluginDynamicLoaderDarwinKernel PLUGIN
1717
lldbSymbol
1818
lldbTarget
1919
lldbUtility
20+
lldbPluginObjectFileMachO
2021
)
2122

2223
add_dependencies(lldbPluginDynamicLoaderDarwinKernel

lldb/source/Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.cpp

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include "Plugins/ObjectFile/Mach-O/ObjectFileMachO.h"
910
#include "Plugins/Platform/MacOSX/PlatformDarwinKernel.h"
1011
#include "lldb/Breakpoint/StoppointCallbackContext.h"
1112
#include "lldb/Core/Debugger.h"
@@ -713,6 +714,7 @@ void DynamicLoaderDarwinKernel::KextImageInfo::SetIsKernel(bool is_kernel) {
713714

714715
bool DynamicLoaderDarwinKernel::KextImageInfo::LoadImageUsingMemoryModule(
715716
Process *process) {
717+
Log *log = GetLog(LLDBLog::DynamicLoader);
716718
if (IsLoaded())
717719
return true;
718720

@@ -810,6 +812,28 @@ bool DynamicLoaderDarwinKernel::KextImageInfo::LoadImageUsingMemoryModule(
810812
}
811813
}
812814

815+
if (m_module_sp && m_uuid.IsValid() && m_module_sp->GetUUID() == m_uuid) {
816+
ObjectFileMachO *ondisk_objfile_macho =
817+
llvm::dyn_cast_or_null<ObjectFileMachO>(
818+
m_module_sp ? m_module_sp->GetObjectFile() : nullptr);
819+
if (ondisk_objfile_macho) {
820+
if (!IsKernel() && !ondisk_objfile_macho->IsKext()) {
821+
// We have a non-kext, non-kernel binary. If we already have this
822+
// loaded in the Target with load addresses, don't re-load it again.
823+
ModuleSP existing_module_sp = target.GetImages().FindModule(m_uuid);
824+
if (existing_module_sp &&
825+
existing_module_sp->IsLoadedInTarget(&target)) {
826+
LLDB_LOGF(log,
827+
"'%s' with UUID %s is not a kext or kernel, and is "
828+
"already registered in target, not loading.",
829+
m_name.c_str(), m_uuid.GetAsString().c_str());
830+
// It's already loaded, return true.
831+
return true;
832+
}
833+
}
834+
}
835+
}
836+
813837
// If we managed to find a module, append it to the target's list of
814838
// images. If we also have a memory module, require that they have matching
815839
// UUIDs
@@ -837,6 +861,34 @@ bool DynamicLoaderDarwinKernel::KextImageInfo::LoadImageUsingMemoryModule(
837861
// it.
838862
const bool ignore_linkedit = !IsKernel();
839863

864+
// Normally a kext will have its segment load commands
865+
// (LC_SEGMENT vmaddrs) corrected in memory to have their
866+
// actual segment addresses.
867+
// Userland proceses have their libraries updated the same way
868+
// by dyld. The Mach-O load commands in memory are the canonical
869+
// addresses.
870+
//
871+
// If the kernel gives us a binary where the in-memory segment
872+
// vmaddr is incorrect, then this binary was put in memory without
873+
// updating its Mach-O load commands. We should assume a static
874+
// slide value will be applied to every segment; we don't have the
875+
// correct addresses for each individual segment.
876+
addr_t fixed_slide = LLDB_INVALID_ADDRESS;
877+
if (ObjectFileMachO *memory_objfile_macho =
878+
llvm::dyn_cast<ObjectFileMachO>(memory_object_file)) {
879+
if (Section *header_sect =
880+
memory_objfile_macho->GetMachHeaderSection()) {
881+
if (header_sect->GetFileAddress() != m_load_address) {
882+
fixed_slide = m_load_address - header_sect->GetFileAddress();
883+
LLDB_LOGF(
884+
log,
885+
"kext %s in-memory LC_SEGMENT vmaddr is not correct, using a "
886+
"fixed slide of 0x%" PRIx64,
887+
m_name.c_str(), fixed_slide);
888+
}
889+
}
890+
}
891+
840892
SectionList *ondisk_section_list = ondisk_object_file->GetSectionList();
841893
SectionList *memory_section_list = memory_object_file->GetSectionList();
842894
if (memory_section_list && ondisk_section_list) {
@@ -865,14 +917,20 @@ bool DynamicLoaderDarwinKernel::KextImageInfo::LoadImageUsingMemoryModule(
865917
ondisk_section_sp->GetName() == g_section_name_LINKEDIT)
866918
continue;
867919

868-
const Section *memory_section =
869-
memory_section_list
870-
->FindSectionByName(ondisk_section_sp->GetName())
871-
.get();
872-
if (memory_section) {
873-
target.SetSectionLoadAddress(ondisk_section_sp,
874-
memory_section->GetFileAddress());
875-
++num_sections_loaded;
920+
if (fixed_slide != LLDB_INVALID_ADDRESS) {
921+
target.SetSectionLoadAddress(
922+
ondisk_section_sp,
923+
ondisk_section_sp->GetFileAddress() + fixed_slide);
924+
} else {
925+
const Section *memory_section =
926+
memory_section_list
927+
->FindSectionByName(ondisk_section_sp->GetName())
928+
.get();
929+
if (memory_section) {
930+
target.SetSectionLoadAddress(
931+
ondisk_section_sp, memory_section->GetFileAddress());
932+
++num_sections_loaded;
933+
}
876934
}
877935
}
878936
}

lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,10 @@ bool ObjectFileMachO::IsSharedCacheBinary() const {
11441144
return m_header.flags & MH_DYLIB_IN_CACHE;
11451145
}
11461146

1147+
bool ObjectFileMachO::IsKext() const {
1148+
return m_header.filetype == MH_KEXT_BUNDLE;
1149+
}
1150+
11471151
uint32_t ObjectFileMachO::GetAddressByteSize() const {
11481152
return m_data.GetAddressByteSize();
11491153
}

lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ class ObjectFileMachO : public lldb_private::ObjectFile {
8989

9090
bool IsSharedCacheBinary() const;
9191

92+
bool IsKext() const;
93+
9294
uint32_t GetAddressByteSize() const override;
9395

9496
lldb_private::AddressClass GetAddressClass(lldb::addr_t file_addr) override;
@@ -148,6 +150,8 @@ class ObjectFileMachO : public lldb_private::ObjectFile {
148150

149151
bool AllowAssemblyEmulationUnwindPlans() override;
150152

153+
lldb_private::Section *GetMachHeaderSection();
154+
151155
// PluginInterface protocol
152156
llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
153157

@@ -190,8 +194,6 @@ class ObjectFileMachO : public lldb_private::ObjectFile {
190194
/// should not be used.
191195
void GetLLDBSharedCacheUUID(lldb::addr_t &base_addir, lldb_private::UUID &uuid);
192196

193-
lldb_private::Section *GetMachHeaderSection();
194-
195197
lldb::addr_t CalculateSectionLoadAddressForMemoryImage(
196198
lldb::addr_t mach_header_load_address,
197199
const lldb_private::Section *mach_header_section,

0 commit comments

Comments
 (0)