Skip to content

Commit 80d20ca

Browse files
committed
[lldb] [ObjectFileMachO] BSS segments are loadable segments (llvm#96983)
ObjectFileMachO::SetLoadAddress sets the address of each segment in a binary in a Target, but it ignores segments that are not loaded in the virtual address space. It was marking segments that were purely BSS -- having no content in the file, but in zero-initialized memory when running in the virtual address space -- as not-loadable, unless they were named "DATA". This works pretty well for typical userland binaries, but in less Darwin environments, there may be BSS segments with other names, that ARE loadable. I looked at the origin of SectionIsLoadable's check for this, and it was a cleanup by Greg in 2018 where we had three different implementations of the idea in ObjectFileMachO and one of them skipped zero-file-size segments (BSS), which made it into the centralized SectionIsLoadable method. Also add some logging to the DynamicLoader log channel when loading a binary - it's the first place I look when debugging segment address setting bugs, and it wasn't emitting anything. rdar://129870649 (cherry picked from commit 91c0ef6)
1 parent 97d4829 commit 80d20ca

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

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

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6189,10 +6189,6 @@ Section *ObjectFileMachO::GetMachHeaderSection() {
61896189
bool ObjectFileMachO::SectionIsLoadable(const Section *section) {
61906190
if (!section)
61916191
return false;
6192-
const bool is_dsym = (m_header.filetype == MH_DSYM);
6193-
if (section->GetFileSize() == 0 && !is_dsym &&
6194-
section->GetName() != GetSegmentNameDATA())
6195-
return false;
61966192
if (section->IsThreadSpecific())
61976193
return false;
61986194
if (GetModule().get() != section->GetModule().get())
@@ -6232,6 +6228,7 @@ lldb::addr_t ObjectFileMachO::CalculateSectionLoadAddressForMemoryImage(
62326228

62336229
bool ObjectFileMachO::SetLoadAddress(Target &target, lldb::addr_t value,
62346230
bool value_is_offset) {
6231+
Log *log(GetLog(LLDBLog::DynamicLoader));
62356232
ModuleSP module_sp = GetModule();
62366233
if (!module_sp)
62376234
return false;
@@ -6247,17 +6244,33 @@ bool ObjectFileMachO::SetLoadAddress(Target &target, lldb::addr_t value,
62476244
// malformed.
62486245
const bool warn_multiple = true;
62496246

6247+
if (log) {
6248+
StreamString logmsg;
6249+
logmsg << "ObjectFileMachO::SetLoadAddress ";
6250+
if (GetFileSpec())
6251+
logmsg << "path='" << GetFileSpec().GetPath() << "' ";
6252+
if (GetUUID()) {
6253+
logmsg << "uuid=" << GetUUID().GetAsString();
6254+
}
6255+
LLDB_LOGF(log, "%s", logmsg.GetData());
6256+
}
62506257
if (value_is_offset) {
62516258
// "value" is an offset to apply to each top level segment
62526259
for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
62536260
// Iterate through the object file sections to find all of the
62546261
// sections that size on disk (to avoid __PAGEZERO) and load them
62556262
SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
6256-
if (SectionIsLoadable(section_sp.get()))
6263+
if (SectionIsLoadable(section_sp.get())) {
6264+
LLDB_LOGF(log,
6265+
"ObjectFileMachO::SetLoadAddress segment '%s' load addr is "
6266+
"0x%" PRIx64,
6267+
section_sp->GetName().AsCString(),
6268+
section_sp->GetFileAddress() + value);
62576269
if (target.GetSectionLoadList().SetSectionLoadAddress(
62586270
section_sp, section_sp->GetFileAddress() + value,
62596271
warn_multiple))
62606272
++num_loaded_sections;
6273+
}
62616274
}
62626275
} else {
62636276
// "value" is the new base address of the mach_header, adjust each
@@ -6272,6 +6285,10 @@ bool ObjectFileMachO::SetLoadAddress(Target &target, lldb::addr_t value,
62726285
CalculateSectionLoadAddressForMemoryImage(
62736286
value, mach_header_section, section_sp.get());
62746287
if (section_load_addr != LLDB_INVALID_ADDRESS) {
6288+
LLDB_LOGF(log,
6289+
"ObjectFileMachO::SetLoadAddress segment '%s' load addr is "
6290+
"0x%" PRIx64,
6291+
section_sp->GetName().AsCString(), section_load_addr);
62756292
if (target.GetSectionLoadList().SetSectionLoadAddress(
62766293
section_sp, section_load_addr, warn_multiple))
62776294
++num_loaded_sections;

0 commit comments

Comments
 (0)