Skip to content

Commit dbd139b

Browse files
committed
Prioritize using a segment with the name TEXT instead off fileoff 0
lldb needs to find the virtual address of the mach header of a binary. It first scans for a segment which starts at file offset 0, and uses the vmaddr of that segment. If no segment starts at fileoff 0, it looks for a segment named __TEXT. This patch changes the order of those, to first search for the TEXT segment. We have a situation where binaries exist that have the DATA segment first, which does not have the vmaddr of the mach header, it merely happens to come first in the binary file. It's an unusual arrangement, but not breaking any rules of Mach-O. So lldb needs to handle this. Differential Revision: https://reviews.llvm.org/D150239 rdar://109128418 (cherry picked from commit f9759d0)
1 parent 7557a46 commit dbd139b

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6141,21 +6141,23 @@ Section *ObjectFileMachO::GetMachHeaderSection() {
61416141
SectionList *section_list = GetSectionList();
61426142
if (!section_list)
61436143
return nullptr;
6144+
6145+
// Some binaries can have a TEXT segment with a non-zero file offset.
6146+
// Binaries in the shared cache are one example. Some hand-generated
6147+
// binaries may not be laid out in the normal TEXT,DATA,LC_SYMTAB order
6148+
// in the file, even though they're laid out correctly in vmaddr terms.
6149+
SectionSP text_segment_sp =
6150+
section_list->FindSectionByName(GetSegmentNameTEXT());
6151+
if (text_segment_sp.get() && SectionIsLoadable(text_segment_sp.get()))
6152+
return text_segment_sp.get();
6153+
61446154
const size_t num_sections = section_list->GetSize();
61456155
for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
61466156
Section *section = section_list->GetSectionAtIndex(sect_idx).get();
61476157
if (section->GetFileOffset() == 0 && SectionIsLoadable(section))
61486158
return section;
61496159
}
61506160

6151-
// We may have a binary in the shared cache that has a non-zero
6152-
// file address for its first segment, traditionally the __TEXT segment.
6153-
// Search for it by name and return it as our next best guess.
6154-
SectionSP text_segment_sp =
6155-
GetSectionList()->FindSectionByName(GetSegmentNameTEXT());
6156-
if (text_segment_sp.get() && SectionIsLoadable(text_segment_sp.get()))
6157-
return text_segment_sp.get();
6158-
61596161
return nullptr;
61606162
}
61616163

0 commit comments

Comments
 (0)