[PATCH] D40849: CodeGen: support an extension to pass linker options on ELF

Saleem Abdulrasool via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 11 16:38:43 PST 2017


compnerd updated this revision to Diff 126476.
compnerd added a comment.

Address implementation details pointed out by @jakehehrlich


Repository:
  rL LLVM

https://reviews.llvm.org/D40849

Files:
  lib/CodeGen/TargetLoweringObjectFileImpl.cpp
  test/Feature/elf-linker-options.ll


Index: test/Feature/elf-linker-options.ll
===================================================================
--- /dev/null
+++ test/Feature/elf-linker-options.ll
@@ -0,0 +1,16 @@
+; RUN: llc -mtriple i686-unknown-linux-gnu -filetype asm -o - %s | FileCheck %s
+
+!llvm.linker.options = !{!0, !1}
+
+!0 = !{!"spaced", !"option"}
+!1 = !{!"nospace"}
+
+; CHECK: .section ".note.linker-options","", at note
+; CHECK: .long 5
+; CHECK: .long 25
+; CHECK: .long 1
+; CHECK: .ascii "LLVM"
+; CHECK: .byte 0
+; CHECK: .p2align 2
+; CHECK: .asciz " spaced option\000 nospace"
+; CHECK: .byte 0
Index: lib/CodeGen/TargetLoweringObjectFileImpl.cpp
===================================================================
--- lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -93,6 +93,32 @@

 void TargetLoweringObjectFileELF::emitModuleMetadata(
     MCStreamer &Streamer, Module &M, const TargetMachine &TM) const {
+  if (NamedMDNode *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) {
+    Streamer.SwitchSection(getContext().getELFSection(".note.linker-options",
+                                                      ELF::SHT_NOTE, 0));
+
+    llvm::SmallString<128> Descriptor;
+    llvm::raw_svector_ostream DOS(Descriptor);
+    for (const auto &Operand : LinkerOptions->operands()) {
+      std::string Directive;
+      for (const auto &Option : cast<MDNode>(Operand)->operands()) {
+        Directive.push_back(' ');
+        Directive.append(cast<MDString>(Option)->getString());
+      }
+      Directive.push_back('\0');
+      DOS << Directive;
+    }
+
+    Streamer.EmitIntValue(5, 4);                      // namesz
+    Streamer.EmitIntValue(Descriptor.size() + 1, 4);  // descsz
+    Streamer.EmitIntValue(1, 4);                      // type
+    Streamer.EmitBytes("LLVM");                       // name
+    Streamer.EmitIntValue(0, 1);
+    Streamer.EmitValueToAlignment(4);
+    Streamer.EmitBytes(DOS.str().str());
+    Streamer.EmitIntValue(0, 1);
+  }
+
   unsigned Version = 0;
   unsigned Flags = 0;
   StringRef Section;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D40849.126476.patch
Type: text/x-patch
Size: 2092 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20171212/7d21bd00/attachment.bin>


More information about the llvm-commits mailing list