Skip to content

[lldb] Fix object format of some mach-o files by using vendor info in getDefaultFormat() #143633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions lldb/unittests/ObjectFile/MachO/TestObjectFileMachO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,59 @@ TEST_F(ObjectFileMachOTest, IndirectSymbolsInTheSharedCache) {
for (size_t i = 0; i < 10; i++)
OF->ParseSymtab(symtab);
}

TEST_F(ObjectFileMachOTest, ObjectFormatWithoutVersionLoadCommand) {
// 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
2 changes: 2 additions & 0 deletions llvm/lib/TargetParser/Triple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,8 @@ static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
case Triple::Win32:
case Triple::UEFI:
return Triple::COFF;
case Triple::UnknownOS:
return T.getVendor() == Triple::Apple ? Triple::MachO : Triple::ELF;
default:
return T.isOSDarwin() ? Triple::MachO : Triple::ELF;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we fold the above check into this line?

Suggested change
return T.isOSDarwin() ? Triple::MachO : Triple::ELF;
return T.isOSDarwin() || (T.getVendor() == Triple::Apple) ? Triple::MachO : Triple::ELF;

Or do we really only want it when the OS is unknown?

Copy link
Contributor Author

@royitaqi royitaqi Jun 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Off the top of my head: For the sake of future proof, e.g. say Apple ships their own Linux, I assume their Linux will use ELF format, and so for case where "vendor == Apple && os == Linux", we probably want to return ELF.

WDYT?

(BTW the command of "Intentional leak" is outdated. Will remove in next update.)

}
Expand Down
7 changes: 7 additions & 0 deletions llvm/unittests/TargetParser/TripleTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2579,6 +2579,13 @@ TEST(TripleTest, FileFormat) {
EXPECT_EQ(Triple::SPIRV, Triple("spirv32-apple-macosx").getObjectFormat());
EXPECT_EQ(Triple::SPIRV, Triple("spirv64-apple-macosx").getObjectFormat());
EXPECT_EQ(Triple::DXContainer, Triple("dxil-apple-macosx").getObjectFormat());

EXPECT_EQ(Triple::MachO, Triple("aarch64-apple-macosx").getObjectFormat());
EXPECT_EQ(Triple::MachO, Triple("x86_64-apple-macosx").getObjectFormat());
EXPECT_EQ(Triple::MachO, Triple("aarch64-apple").getObjectFormat());
EXPECT_EQ(Triple::MachO, Triple("x86_64-apple").getObjectFormat());
EXPECT_EQ(Triple::ELF, Triple("aarch64").getObjectFormat());
EXPECT_EQ(Triple::ELF, Triple("x86_64").getObjectFormat());
}

TEST(TripleTest, NormalizeWindows) {
Expand Down
Loading