From 506857f4270005a57083535be1b694adcb9ae04c Mon Sep 17 00:00:00 2001 From: Chris Bieneman Date: Wed, 17 Aug 2016 21:46:04 +0000 Subject: [PATCH] [macho2yaml] Don't write empty linkedit data Since I stopped writing empty export tries it causes LinkEdit to potentially be completely empty which results in invalid yaml being generated. To prevent this we skip linkedit data if it is empty. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@278985 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/ObjectYAML/MachOYAML.h | 2 ++ lib/ObjectYAML/MachOYAML.cpp | 7 ++++++- test/ObjectYAML/MachO/bogus_load_command.yaml | 1 + 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/include/llvm/ObjectYAML/MachOYAML.h b/include/llvm/ObjectYAML/MachOYAML.h index 5a106efd05a..2fbd71f0bcd 100644 --- a/include/llvm/ObjectYAML/MachOYAML.h +++ b/include/llvm/ObjectYAML/MachOYAML.h @@ -100,6 +100,8 @@ struct LinkEditData { MachOYAML::ExportEntry ExportTrie; std::vector NameList; std::vector StringTable; + + bool isEmpty() const; }; struct Object { diff --git a/lib/ObjectYAML/MachOYAML.cpp b/lib/ObjectYAML/MachOYAML.cpp index 5825d0042eb..984ca0b9d01 100644 --- a/lib/ObjectYAML/MachOYAML.cpp +++ b/lib/ObjectYAML/MachOYAML.cpp @@ -22,6 +22,10 @@ namespace llvm { MachOYAML::LoadCommand::~LoadCommand() {} +bool MachOYAML::LinkEditData::isEmpty() const { + return 0 == RebaseOpcodes.size() + BindOpcodes.size() + WeakBindOpcodes.size() + LazyBindOpcodes.size() + ExportTrie.Children.size() + NameList.size() + StringTable.size(); +} + namespace yaml { void ScalarTraits::output(const char_16 &Val, void *, @@ -95,7 +99,8 @@ void MappingTraits::mapping(IO &IO, IO.mapTag("!mach-o", true); IO.mapRequired("FileHeader", Object.Header); IO.mapOptional("LoadCommands", Object.LoadCommands); - IO.mapOptional("LinkEditData", Object.LinkEdit); + if(!Object.LinkEdit.isEmpty() || !IO.outputting()) + IO.mapOptional("LinkEditData", Object.LinkEdit); if (IO.getContext() == &Object) IO.setContext(nullptr); diff --git a/test/ObjectYAML/MachO/bogus_load_command.yaml b/test/ObjectYAML/MachO/bogus_load_command.yaml index 69c57d055cb..f37f733b34c 100644 --- a/test/ObjectYAML/MachO/bogus_load_command.yaml +++ b/test/ObjectYAML/MachO/bogus_load_command.yaml @@ -49,3 +49,4 @@ LoadCommands: #CHECK: - 0x0A #CHECK: - 0x0B #CHECK: - 0x0C +#CHECK-NOT: LinkEditData: