From 4c47434b25ea4683ff1794e2d1b24eaa16b5e30e Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Mon, 5 Jun 2017 21:26:39 +0000 Subject: [PATCH] CodeGen: add support for emitting ObjC image info This ensures that we can emit the ObjC Image Info structure on COFF and ELF as well. The frontend already would attempt to emit this information but would get dropped when generating assembly or an object file. llvm-svn: 304736 --- .../CodeGen/TargetLoweringObjectFileImpl.h | 8 +- .../CodeGen/TargetLoweringObjectFileImpl.cpp | 93 ++++++++++++++----- llvm/test/Object/objc-imageinfo-coff.ll | 14 +++ llvm/test/Object/objc-imageinfo-elf.ll | 14 +++ llvm/test/Object/objc-imageinfo-macho.ll | 14 +++ 5 files changed, 118 insertions(+), 25 deletions(-) create mode 100644 llvm/test/Object/objc-imageinfo-coff.ll create mode 100644 llvm/test/Object/objc-imageinfo-elf.ll create mode 100644 llvm/test/Object/objc-imageinfo-macho.ll diff --git a/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h b/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h index adf2b3ea1c9b..106a084a95c0 100644 --- a/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h +++ b/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h @@ -41,6 +41,11 @@ public: TargetLoweringObjectFileELF() = default; ~TargetLoweringObjectFileELF() override = default; + /// Emit Obj-C garbage collection and linker options. + void emitModuleFlags(MCStreamer &Streamer, + ArrayRef ModuleFlags, + const TargetMachine &TM) const override; + void emitPersonalityValue(MCStreamer &Streamer, const DataLayout &TM, const MCSymbol *Sym) const override; @@ -149,8 +154,7 @@ public: MCSection *getSectionForJumpTable(const Function &F, const TargetMachine &TM) const override; - /// Emit Obj-C garbage collection and linker options. Only linker option - /// emission is implemented for COFF. + /// Emit Obj-C garbage collection and linker options. void emitModuleFlags(MCStreamer &Streamer, ArrayRef ModuleFlags, const TargetMachine &TM) const override; diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp index 3ba4a3a29262..24baa59db5dc 100644 --- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp +++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp @@ -61,10 +61,53 @@ using namespace llvm; using namespace dwarf; +static void GetObjCImageInfo(ArrayRef ModuleFlags, + unsigned &Version, unsigned &Flags, + StringRef &Section) { + for (const auto &MFE: ModuleFlags) { + // Ignore flags with 'Require' behaviour. + if (MFE.Behavior == Module::Require) + continue; + + StringRef Key = MFE.Key->getString(); + if (Key == "Objective-C Image Info Version") { + Version = mdconst::extract(MFE.Val)->getZExtValue(); + } else if (Key == "Objective-C Garbage Collection" || + Key == "Objective-C GC Only" || + Key == "Objective-C Is Simulated" || + Key == "Objective-C Class Properties" || + Key == "Objective-C Image Swift Version") { + Flags |= mdconst::extract(MFE.Val)->getZExtValue(); + } else if (Key == "Objective-C Image Info Section") { + Section = cast(MFE.Val)->getString(); + } + } +} + //===----------------------------------------------------------------------===// // ELF //===----------------------------------------------------------------------===// +void TargetLoweringObjectFileELF::emitModuleFlags( + MCStreamer &Streamer, ArrayRef ModuleFlags, + const TargetMachine &TM) const { + unsigned Version = 0; + unsigned Flags = 0; + StringRef Section; + + GetObjCImageInfo(ModuleFlags, Version, Flags, Section); + if (Section.empty()) + return; + + auto &C = getContext(); + auto *S = C.getELFSection(Section, ELF::SHT_PROGBITS, ELF::SHF_ALLOC); + Streamer.SwitchSection(S); + Streamer.EmitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO"))); + Streamer.EmitIntValue(Version, 4); + Streamer.EmitIntValue(Flags, 4); + Streamer.AddBlankLine(); +} + MCSymbol *TargetLoweringObjectFileELF::getCFIPersonalitySymbol( const GlobalValue *GV, const TargetMachine &TM, MachineModuleInfo *MMI) const { @@ -579,32 +622,12 @@ void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx, void TargetLoweringObjectFileMachO::emitModuleFlags( MCStreamer &Streamer, ArrayRef ModuleFlags, const TargetMachine &TM) const { - unsigned VersionVal = 0; - unsigned ImageInfoFlags = 0; MDNode *LinkerOptions = nullptr; - StringRef SectionVal; for (const auto &MFE : ModuleFlags) { - // Ignore flags with 'Require' behavior. - if (MFE.Behavior == Module::Require) - continue; - StringRef Key = MFE.Key->getString(); - Metadata *Val = MFE.Val; - - if (Key == "Objective-C Image Info Version") { - VersionVal = mdconst::extract(Val)->getZExtValue(); - } else if (Key == "Objective-C Garbage Collection" || - Key == "Objective-C GC Only" || - Key == "Objective-C Is Simulated" || - Key == "Objective-C Class Properties" || - Key == "Objective-C Image Swift Version") { - ImageInfoFlags |= mdconst::extract(Val)->getZExtValue(); - } else if (Key == "Objective-C Image Info Section") { - SectionVal = cast(Val)->getString(); - } else if (Key == "Linker Options") { - LinkerOptions = cast(Val); - } + if (Key == "Linker Options") + LinkerOptions = cast(MFE.Val); } // Emit the linker options if present. @@ -617,8 +640,14 @@ void TargetLoweringObjectFileMachO::emitModuleFlags( } } + unsigned VersionVal = 0; + unsigned ImageInfoFlags = 0; + StringRef SectionVal; + GetObjCImageInfo(ModuleFlags, VersionVal, ImageInfoFlags, SectionVal); + // The section is mandatory. If we don't have it, then we don't have GC info. - if (SectionVal.empty()) return; + if (SectionVal.empty()) + return; StringRef Segment, Section; unsigned TAA = 0, StubSize = 0; @@ -1156,6 +1185,24 @@ void TargetLoweringObjectFileCOFF::emitModuleFlags( } } } + + unsigned Version = 0; + unsigned Flags = 0; + StringRef Section; + + GetObjCImageInfo(ModuleFlags, Version, Flags, Section); + if (Section.empty()) + return; + + auto &C = getContext(); + auto *S = C.getCOFFSection( + Section, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, + SectionKind::getReadOnly()); + Streamer.SwitchSection(S); + Streamer.EmitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO"))); + Streamer.EmitIntValue(Version, 4); + Streamer.EmitIntValue(Flags, 4); + Streamer.AddBlankLine(); } void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx, diff --git a/llvm/test/Object/objc-imageinfo-coff.ll b/llvm/test/Object/objc-imageinfo-coff.ll new file mode 100644 index 000000000000..cab0103b5f4c --- /dev/null +++ b/llvm/test/Object/objc-imageinfo-coff.ll @@ -0,0 +1,14 @@ +; RUN: llc -mtriple x86_64-unknown-windows-msvc -filetype asm -o - %s | FileCheck %s + +!llvm.module.flags = !{!0, !1, !2, !3} + +!0 = !{i32 1, !"Objective-C Version", i32 2} +!1 = !{i32 1, !"Objective-C Image Info Version", i32 0} +!2 = !{i32 1, !"Objective-C Image Info Section", !".objc_imageinfo$B"} +!3 = !{i32 1, !"Objective-C Garbage Collection", i32 2} + +; CHECK: .section .objc_imageinfo$B,"dr" +; CHECK: OBJC_IMAGE_INFO: +; CHECK: .long 0 +; CHECK: .long 2 + diff --git a/llvm/test/Object/objc-imageinfo-elf.ll b/llvm/test/Object/objc-imageinfo-elf.ll new file mode 100644 index 000000000000..7979e01457fb --- /dev/null +++ b/llvm/test/Object/objc-imageinfo-elf.ll @@ -0,0 +1,14 @@ +; RUN: llc -mtriple x86_64-unknown-linux-gnu -filetype asm -o - %s | FileCheck %s + +!llvm.module.flags = !{!0, !1, !2, !3} + +!0 = !{i32 1, !"Objective-C Version", i32 2} +!1 = !{i32 1, !"Objective-C Image Info Version", i32 0} +!2 = !{i32 1, !"Objective-C Image Info Section", !"objc_imageinfo"} +!3 = !{i32 1, !"Objective-C Garbage Collection", i32 2} + +; CHECK: .section objc_imageinfo +; CHECK: OBJC_IMAGE_INFO: +; CHECK: .long 0 +; CHECK: .long 2 + diff --git a/llvm/test/Object/objc-imageinfo-macho.ll b/llvm/test/Object/objc-imageinfo-macho.ll new file mode 100644 index 000000000000..90bc9d91a5d3 --- /dev/null +++ b/llvm/test/Object/objc-imageinfo-macho.ll @@ -0,0 +1,14 @@ +; RUN: llc -mtriple x86_64-apple-ios -filetype asm -o - %s | FileCheck %s + +!llvm.module.flags = !{!0, !1, !2, !3} + +!0 = !{i32 1, !"Objective-C Version", i32 2} +!1 = !{i32 1, !"Objective-C Image Info Version", i32 0} +!2 = !{i32 1, !"Objective-C Image Info Section", !"__DATA,__objc_imageinfo,regular,no_dead_strip"} +!3 = !{i32 1, !"Objective-C Garbage Collection", i32 2} + +; CHECK: .section __DATA,__objc_imageinfo,regular,no_dead_strip +; CHECK: L_OBJC_IMAGE_INFO: +; CHECK: .long 0 +; CHECK: .long 2 +