Skip to content

Commit 1b686e1

Browse files
royitaqirorth
authored andcommitted
[lldb] Set default object format to MachO in ObjectFileMachO (llvm#142704)
# The Change This patch sets the **default** object format of `ObjectFileMachO` to be `MachO` (instead of what currently ends up to be `ELF`, see below). This should be **the correct thing to do**, because the code before the line of change has already verified the Mach-O header. The existing logic: * In `ObjectFileMachO`, the object format is unassigned by default. So it's `UnknownObjectFormat` (see [code](https://github.com/llvm/llvm-project/blob/54d544b83141dc0b20727673f68793728ed54793/llvm/lib/TargetParser/Triple.cpp#L1024)). * The code then looks at load commands like `LC_VERSION_MIN_*` ([code](https://github.com/llvm/llvm-project/blob/54d544b83141dc0b20727673f68793728ed54793/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp#L5180-L5217)) and `LC_BUILD_VERSION` ([code](https://github.com/llvm/llvm-project/blob/54d544b83141dc0b20727673f68793728ed54793/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp#L5231-L5252)) and assign the Triple's OS and Environment if they exist. * If the above sets the Triple's OS to macOS, then the object format defaults to `MachO`; otherwise it is `ELF` ([code](https://github.com/llvm/llvm-project/blob/54d544b83141dc0b20727673f68793728ed54793/llvm/lib/TargetParser/Triple.cpp#L936-L937)) # Impact For **production usage** where Mach-O files have the said load commands (which is [expected](https://www.google.com/search?q=Are+mach-o+files+expected+to+have+the+LC_BUILD_VERSION+load+command%3F)), this patch won't change anything. * **Important note**: It's not clear if there are legitimate production use cases where the Mach-O files don't have said load commands. If there is, the exiting code think they are `ELF`. This patch changes it to `MachO`. This is considered a fix for such files. For **unit tests**, this patch will simplify the yaml data by not requiring the said load commands. # Test See PR.
1 parent 6adebd9 commit 1b686e1

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5148,6 +5148,7 @@ void ObjectFileMachO::GetAllArchSpecs(const llvm::MachO::mach_header &header,
51485148
llvm::Triple base_triple = base_arch.GetTriple();
51495149
base_triple.setOS(llvm::Triple::UnknownOS);
51505150
base_triple.setOSName(llvm::StringRef());
5151+
base_triple.setObjectFormat(llvm::Triple::MachO);
51515152

51525153
if (header.filetype == MH_PRELOAD) {
51535154
if (header.cputype == CPU_TYPE_ARM) {

lldb/unittests/ObjectFile/MachO/TestObjectFileMachO.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,59 @@ TEST_F(ObjectFileMachOTest, IndirectSymbolsInTheSharedCache) {
9494
for (size_t i = 0; i < 10; i++)
9595
OF->ParseSymtab(symtab);
9696
}
97+
98+
TEST_F(ObjectFileMachOTest, ObjectFormatWithoutVersionLoadCommand) {
99+
// A Mach-O file without the load command LC_BUILD_VERSION.
100+
const char *yamldata = R"(
101+
--- !mach-o
102+
FileHeader:
103+
magic: 0xFEEDFACF
104+
cputype: 0x0100000C
105+
cpusubtype: 0x00000000
106+
filetype: 0x00000001
107+
ncmds: 1
108+
sizeofcmds: 152
109+
flags: 0x00002000
110+
reserved: 0x00000000
111+
LoadCommands:
112+
- cmd: LC_SEGMENT_64
113+
cmdsize: 152
114+
segname: __TEXT
115+
vmaddr: 0
116+
vmsize: 4
117+
fileoff: 184
118+
filesize: 4
119+
maxprot: 7
120+
initprot: 7
121+
nsects: 1
122+
flags: 0
123+
Sections:
124+
- sectname: __text
125+
segname: __TEXT
126+
addr: 0x0000000000000000
127+
content: 'AABBCCDD'
128+
size: 4
129+
offset: 184
130+
align: 0
131+
reloff: 0x00000000
132+
nreloc: 0
133+
flags: 0x80000400
134+
reserved1: 0x00000000
135+
reserved2: 0x00000000
136+
reserved3: 0x00000000
137+
...
138+
)";
139+
140+
// Perform setup.
141+
llvm::Expected<TestFile> file = TestFile::fromYaml(yamldata);
142+
EXPECT_THAT_EXPECTED(file, llvm::Succeeded());
143+
auto module_sp = std::make_shared<Module>(file->moduleSpec());
144+
ASSERT_NE(module_sp, nullptr);
145+
auto object_file = module_sp->GetObjectFile();
146+
ASSERT_NE(object_file, nullptr);
147+
148+
// Verify that the object file is recognized as Mach-O.
149+
ASSERT_EQ(object_file->GetArchitecture().GetTriple().getObjectFormat(),
150+
llvm::Triple::MachO);
151+
}
97152
#endif

0 commit comments

Comments
 (0)