https://github.com/royitaqi created https://github.com/llvm/llvm-project/pull/142704
The fact that `ObjectFileMachO` can parse the object file means that it's an Mach-O format. So it should just be set set the default format, before going through the rest of the code, which looks at load commands like `LC_VERSION_MIN_*` and `LC_BUILD_VERSION`. For **production usage** where Mach-O files have the said load commands, this patch won't change anything. * **Important note**: It's not clear to me if there are legitimate production use cases where the Mach-O files don't have said load commands. If there is, then what should be their object format? Without this patch, they are currently `ELF`. With this patch, they will be `MachO`. For **unit tests**, this patch will simplify the yaml data by not requiring the said load commands. A unit test is added to verify the patch, i.e. it fails without the patch and succeeds with. ``` $ ninja lldb-unit-test-deps $ tools/lldb/unittests/ObjectFile/MachO/ObjectFileMachOTests --gtest_filter="*ObjectFileFormatWithoutLoadCommand*" [==========] Running 1 test from 1 test suite. ... [ RUN ] ObjectFileMachOTest.ObjectFileFormatWithoutLoadCommand [ OK ] ObjectFileMachOTest.ObjectFileFormatWithoutLoadCommand (3 ms) ... ``` >From d01f75f4b8f75676ae15730f0035cca685dc5195 Mon Sep 17 00:00:00 2001 From: Roy Shi <roy...@meta.com> Date: Tue, 3 Jun 2025 17:04:29 -0700 Subject: [PATCH 1/2] Set default object format. Add test. --- .../ObjectFile/Mach-O/ObjectFileMachO.cpp | 1 + .../ObjectFile/MachO/TestObjectFileMachO.cpp | 54 +++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp b/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp index 3950454b7c90e..0079672c5cbd0 100644 --- a/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp +++ b/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp @@ -5148,6 +5148,7 @@ void ObjectFileMachO::GetAllArchSpecs(const llvm::MachO::mach_header &header, llvm::Triple base_triple = base_arch.GetTriple(); base_triple.setOS(llvm::Triple::UnknownOS); base_triple.setOSName(llvm::StringRef()); + base_triple.setObjectFormat(llvm::Triple::MachO); if (header.filetype == MH_PRELOAD) { if (header.cputype == CPU_TYPE_ARM) { diff --git a/lldb/unittests/ObjectFile/MachO/TestObjectFileMachO.cpp b/lldb/unittests/ObjectFile/MachO/TestObjectFileMachO.cpp index 0ef2d0b85fd36..33aaba5d1055f 100644 --- a/lldb/unittests/ObjectFile/MachO/TestObjectFileMachO.cpp +++ b/lldb/unittests/ObjectFile/MachO/TestObjectFileMachO.cpp @@ -94,4 +94,58 @@ TEST_F(ObjectFileMachOTest, IndirectSymbolsInTheSharedCache) { for (size_t i = 0; i < 10; i++) OF->ParseSymtab(symtab); } + +TEST_F(ObjectFileMachOTest, ObjectFileFormatWithoutLoadCommand) { + // A Mach-O file without the load command LC_BUILD_VERSION. + const char *yamldata = R"( +--- !mach-o +FileHeader: + magic: 0xFEEDFACF + cputype: 0x0100000C + cpusubtype: 0x00000000 + filetype: 0x00000001 + ncmds: 1 + sizeofcmds: 152 + flags: 0x00002000 + reserved: 0x00000000 +LoadCommands: + - cmd: LC_SEGMENT_64 + cmdsize: 152 + segname: __TEXT + vmaddr: 0 + vmsize: 4 + fileoff: 184 + filesize: 4 + maxprot: 7 + initprot: 7 + nsects: 1 + flags: 0 + Sections: + - sectname: __text + segname: __TEXT + addr: 0x0000000000000000 + content: 'AABBCCDD' + size: 4 + offset: 184 + align: 0 + reloff: 0x00000000 + nreloc: 0 + flags: 0x80000400 + reserved1: 0x00000000 + reserved2: 0x00000000 + reserved3: 0x00000000 +... +)"; + + // Perform setup. + llvm::Expected<TestFile> file = TestFile::fromYaml(yamldata); + EXPECT_THAT_EXPECTED(file, llvm::Succeeded()); + auto module_sp = std::make_shared<Module>(file->moduleSpec()); + ASSERT_NE(module_sp, nullptr); + auto object_file = module_sp->GetObjectFile(); + ASSERT_NE(object_file, nullptr); + + // Verify that the object file is recognized as Mach-O. + ASSERT_EQ(object_file->GetArchitecture().GetTriple().getObjectFormat(), llvm::Triple::MachO); +} #endif >From 119833ef21bb21e580280811fcf16afcf4be4bb2 Mon Sep 17 00:00:00 2001 From: Roy Shi <roy...@meta.com> Date: Tue, 3 Jun 2025 17:05:09 -0700 Subject: [PATCH 2/2] Fix format --- lldb/unittests/ObjectFile/MachO/TestObjectFileMachO.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lldb/unittests/ObjectFile/MachO/TestObjectFileMachO.cpp b/lldb/unittests/ObjectFile/MachO/TestObjectFileMachO.cpp index 33aaba5d1055f..aab3e996343b3 100644 --- a/lldb/unittests/ObjectFile/MachO/TestObjectFileMachO.cpp +++ b/lldb/unittests/ObjectFile/MachO/TestObjectFileMachO.cpp @@ -136,7 +136,7 @@ TEST_F(ObjectFileMachOTest, ObjectFileFormatWithoutLoadCommand) { reserved3: 0x00000000 ... )"; - + // Perform setup. llvm::Expected<TestFile> file = TestFile::fromYaml(yamldata); EXPECT_THAT_EXPECTED(file, llvm::Succeeded()); @@ -146,6 +146,7 @@ TEST_F(ObjectFileMachOTest, ObjectFileFormatWithoutLoadCommand) { ASSERT_NE(object_file, nullptr); // Verify that the object file is recognized as Mach-O. - ASSERT_EQ(object_file->GetArchitecture().GetTriple().getObjectFormat(), llvm::Triple::MachO); + ASSERT_EQ(object_file->GetArchitecture().GetTriple().getObjectFormat(), + llvm::Triple::MachO); } #endif _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits