-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[lldb] Set default object format to MachO
in ObjectFileMachO
#142704
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
Conversation
@llvm/pr-subscribers-lldb Author: None (royitaqi) ChangesThe fact that For production usage where Mach-O files have the said load commands, this patch won't change anything.
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.
Full diff: https://github.com/llvm/llvm-project/pull/142704.diff 2 Files Affected:
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..aab3e996343b3 100644
--- a/lldb/unittests/ObjectFile/MachO/TestObjectFileMachO.cpp
+++ b/lldb/unittests/ObjectFile/MachO/TestObjectFileMachO.cpp
@@ -94,4 +94,59 @@ 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
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with one nit.
At the point in the code where you made your change we already assume that we have a Mach-O file because we are passing in and examining the header. I think it makes sense to set the triple to be Mach-O here. Even if we have Mach-O files in the wild that don't have the version load commands, it seems those still must be treated as Mach-O files to make any sense of them. |
Thanks for pointing that out. I agree. This patch is fixing this bug for those Mach-O files (if they exist). Updated the PR's description accordingly. -- FWIW, I will run |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test is ok. Mach-o files have LC_SEGMENT_64 load commands, so no need to change the test really. I mean, we can probably remove it and still have a mach-o file, but not sure we need to.
nb this PR is causing a failure on the x86_64 macOS CI bot https://green.lab.llvm.org/job/llvm.org/view/LLDB/job/lldb-cmake/ in TestUniversal.py where |
In
given a
Then in llvm's
And that's where the object file format is being set to ELF,
The triple being passed in here is just "arm64" at this point, so the T.isOSDarwin() fails, ObjectFile is set to ELF and it is never reset. But I don't see why the ModuleArchSpec being set to ObjectFileELF (which I'd never noticed before, ever) was not showing up in the triples, but -macho is, when it's set in ObjectFileMachO. Maybe it's an ordering thing, where now that the object file is being set in It seems like it would be better to set the ObjectFile format in |
Aha, I see. Any call to llvm's
Well that's not great. I really don't want |
In
although that's not very prettily done. UPDATE: ah my bad, this isn't sufficient for llvm |
I've reverted this commit on main for now,
Until we can find a way to set the ObjectFile correctly without adding "-macho" to each Module's triple string. TestUniversal.py failing is the specific failure caused by this change, but it's also an undesirable change to have "-macho" added to triple strings from a UI point of view. |
…bugMap` for non-Mach-O files (#139170)" This reverts commit 3096f87. Reverting this commit because it depends on another PR that was reverted, llvm/llvm-project#142704 Both can be reapplied once we find a correct fix for that.
…m#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.
…hO` (llvm#142704)" This reverts commit d4d2f06. Temporarily reverting until we can find a way to get the correct ObjectFile set in Module's Triples without adding "-macho" to the triple string for each Module. This is breaking TestUniversal.py on the x86_64 macOS CI bots.
… non-Mach-O files (llvm#139170)" This reverts commit 3096f87. Reverting this commit because it depends on another PR that was reverted, llvm#142704 Both can be reapplied once we find a correct fix for that.
…m#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.
…hO` (llvm#142704)" This reverts commit d4d2f06. Temporarily reverting until we can find a way to get the correct ObjectFile set in Module's Triples without adding "-macho" to the triple string for each Module. This is breaking TestUniversal.py on the x86_64 macOS CI bots.
… non-Mach-O files (llvm#139170)" This reverts commit 3096f87. Reverting this commit because it depends on another PR that was reverted, llvm#142704 Both can be reapplied once we find a correct fix for that.
The Change
This patch sets the default object format of
ObjectFileMachO
to beMachO
(instead of what currently ends up to beELF
, 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:
ObjectFileMachO
, the object format is unassigned by default. So it'sUnknownObjectFormat
(see code).LC_VERSION_MIN_*
(code) andLC_BUILD_VERSION
(code) and assign the Triple's OS and Environment if they exist.MachO
; otherwise it isELF
(code)Impact
For production usage where Mach-O files have the said load commands (which is expected), this patch won't change anything.
ELF
. This patch changes it toMachO
. 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
A unit test is added to verify the patch, i.e. it fails without the patch and succeeds with.
Double-checked that this patch doesn't break any existing tests on both macOS (result) and Linux (result).