nickdesaulniers created this revision.
nickdesaulniers added a reviewer: MaskRay.
Herald added subscribers: StephenFan, dexonsmith, hiraditya.
Herald added a project: All.
nickdesaulniers requested review of this revision.
Herald added projects: clang, LLVM.
Herald added subscribers: llvm-commits, cfe-commits.

GNU binutils has added support for explicitly retaining (yes option) or
dropping (no option) STT_SECTION symbols from the symbols table via
assembler option -generate-unused-section-symbols={yes|no}.

Add support to clang (`-Wa,-generate-unused-section-symbols={yes|no}`)
and llvm-mc.

This feature might be used by the Linux kernel.
https://lore.kernel.org/linux-toolchains/ylluqpk4cwzeh...@hirez.programming.kicks-ass.net/
This has been supported by GNU binutils since 2.36.
https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=d1bcae833b32f1408485ce69f844dcd7ded093a8


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D123874

Files:
  clang/include/clang/Basic/CodeGenOptions.def
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/test/Driver/as-options.s
  clang/test/Misc/cc1as-keep-section-symbols.s
  clang/tools/driver/cc1as_main.cpp
  llvm/include/llvm/MC/MCAsmInfo.h
  llvm/lib/MC/ELFObjectWriter.cpp
  llvm/test/MC/X86/keep-section-symbols.s
  llvm/tools/llvm-mc/llvm-mc.cpp

Index: llvm/tools/llvm-mc/llvm-mc.cpp
===================================================================
--- llvm/tools/llvm-mc/llvm-mc.cpp
+++ llvm/tools/llvm-mc/llvm-mc.cpp
@@ -212,6 +212,10 @@
 static cl::opt<bool> NoExecStack("no-exec-stack",
                                  cl::desc("File doesn't need an exec stack"),
                                  cl::cat(MCCategory));
+static cl::opt<bool>
+    KeepSectionSymbols("keep-section-symbols",
+                       cl::desc("Do not discard section symbols"),
+                       cl::cat(MCCategory));
 
 enum ActionType {
   AC_AsLex,
@@ -401,6 +405,7 @@
   assert(MAI && "Unable to create target asm info!");
 
   MAI->setRelaxELFRelocations(RelaxELFRel);
+  MAI->setKeepSectionSymbols(KeepSectionSymbols);
 
   if (CompressDebugSections != DebugCompressionType::None) {
     if (!zlib::isAvailable()) {
Index: llvm/test/MC/X86/keep-section-symbols.s
===================================================================
--- /dev/null
+++ llvm/test/MC/X86/keep-section-symbols.s
@@ -0,0 +1,10 @@
+# RUN: llvm-mc -keep-section-symbols %s -triple x86_64-linux-gnu -filetype=obj -o %t
+# RUN: llvm-readelf -s %t | FileCheck %s --check-prefix=CHECK-KEEP
+
+# RUN: llvm-mc %s -triple x86_64-linux-gnu -filetype=obj -o %t
+# RUN: llvm-readelf -s %t | FileCheck %s --check-prefix=CHECK-DROP
+
+# CHECK-KEEP:     SECTION LOCAL DEFAULT {{[0-9]+}} .text
+# CHECK-DROP-NOT: SECTION LOCAL DEFAULT {{[0-9]+}} .text
+.text
+  nop
Index: llvm/lib/MC/ELFObjectWriter.cpp
===================================================================
--- llvm/lib/MC/ELFObjectWriter.cpp
+++ llvm/lib/MC/ELFObjectWriter.cpp
@@ -610,7 +610,10 @@
     return false;
 
   if (Symbol.getType() == ELF::STT_SECTION)
-    return false;
+    return Layout.getAssembler()
+        .getContext()
+        .getAsmInfo()
+        ->getKeepSectionSymbols();
 
   return true;
 }
Index: llvm/include/llvm/MC/MCAsmInfo.h
===================================================================
--- llvm/include/llvm/MC/MCAsmInfo.h
+++ llvm/include/llvm/MC/MCAsmInfo.h
@@ -518,6 +518,9 @@
   // X86_64 ELF.
   bool RelaxELFRelocations = true;
 
+  // If true, emit ELF STT_SECTION symbols.
+  bool KeepSectionSymbols = false;
+
   // If true, then the lexer and expression parser will support %neg(),
   // %hi(), and similar unary operators.
   bool HasMipsExpressions = false;
@@ -853,6 +856,8 @@
 
   bool canRelaxRelocations() const { return RelaxELFRelocations; }
   void setRelaxELFRelocations(bool V) { RelaxELFRelocations = V; }
+  bool getKeepSectionSymbols() const { return KeepSectionSymbols; }
+  void setKeepSectionSymbols(bool V) { KeepSectionSymbols = V; }
   bool hasMipsExpressions() const { return HasMipsExpressions; }
   bool needsFunctionDescriptors() const { return NeedsFunctionDescriptors; }
   bool shouldUseMotorolaIntegers() const { return UseMotorolaIntegers; }
Index: clang/tools/driver/cc1as_main.cpp
===================================================================
--- clang/tools/driver/cc1as_main.cpp
+++ clang/tools/driver/cc1as_main.cpp
@@ -136,6 +136,7 @@
   unsigned NoWarn : 1;
   unsigned IncrementalLinkerCompatible : 1;
   unsigned EmbedBitcode : 1;
+  unsigned KeepSectionSymbols : 1;
 
   /// The name of the relocation model to use.
   std::string RelocationModel;
@@ -300,6 +301,7 @@
   Opts.IncrementalLinkerCompatible =
       Args.hasArg(OPT_mincremental_linker_compatible);
   Opts.SymbolDefs = Args.getAllArgValues(OPT_defsym);
+  Opts.KeepSectionSymbols = Args.hasArg(OPT_keep_section_symbols);
 
   // EmbedBitcode Option. If -fembed-bitcode is enabled, set the flag.
   // EmbedBitcode behaves the same for all embed options for assembly files.
@@ -370,6 +372,7 @@
   MAI->setCompressDebugSections(Opts.CompressDebugSections);
 
   MAI->setRelaxELFRelocations(Opts.RelaxELFRelocations);
+  MAI->setKeepSectionSymbols(Opts.KeepSectionSymbols);
 
   bool IsBinary = Opts.OutputType == AssemblerInvocation::FT_Obj;
   if (Opts.OutputPath.empty())
Index: clang/test/Misc/cc1as-keep-section-symbols.s
===================================================================
--- /dev/null
+++ clang/test/Misc/cc1as-keep-section-symbols.s
@@ -0,0 +1,12 @@
+// REQUIRES: x86-registered-target
+
+// RUN: %clang -cc1as -triple x86_64-pc-linux-gnu -filetype obj -keep-section-symbols %s -o %t
+// RUN: llvm-readelf -s %t | FileCheck %s --check-prefix=CHECK-KEEP
+
+// RUN: %clang -cc1as -triple x86_64-pc-linux-gnu -filetype obj %s -o %t
+// RUN: llvm-readelf -s %t | FileCheck %s --check-prefix=CHECK-DROP
+
+// CHECK-KEEP:     SECTION LOCAL DEFAULT {{[0-9]+}} .text
+// CHECK-DROP-NOT: SECTION LOCAL DEFAULT {{[0-9]+}} .text
+.text
+  nop
Index: clang/test/Driver/as-options.s
===================================================================
--- clang/test/Driver/as-options.s
+++ clang/test/Driver/as-options.s
@@ -116,3 +116,12 @@
 // RUN: %clang -mrelax-all -fno-integrated-as -x c++ %s -S -o /dev/null 2>&1 \
 // RUN:   | FileCheck --check-prefix=WARN --allow-empty %s
 // WARN: unused
+
+// RUN: %clang -Wa,-generate-unused-section-symbols=yes %s -c -o /dev/null -v 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=CHECK-KEEP
+// RUN: %clang -Wa,-generate-unused-section-symbols=no %s -c -o /dev/null -v 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=CHECK-DROP
+// RUN: %clang %s -c -o /dev/null -v 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=CHECK-DROP
+// CHECK-KEEP: -keep-section-symbols
+// CHECK-DROP-NOT: -keep-section-symbols
Index: clang/lib/Driver/ToolChains/Clang.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -2688,6 +2688,10 @@
         CmdArgs.push_back(Value.data());
       } else if (Value == "--version") {
         D.PrintVersion(C, llvm::outs());
+      } else if (Value.consume_front("-generate-unused-section-symbols=") &&
+                 (Value == "yes" || Value == "no")) {
+        if (Value == "yes")
+          CmdArgs.push_back("-keep-section-symbols");
       } else {
         D.Diag(diag::err_drv_unsupported_option_argument)
             << A->getOption().getName() << Value;
Index: clang/include/clang/Driver/Options.td
===================================================================
--- clang/include/clang/Driver/Options.td
+++ clang/include/clang/Driver/Options.td
@@ -6184,6 +6184,10 @@
 def defsym : Separate<["-"], "defsym">,
   HelpText<"Define a value for a symbol">;
 
+def keep_section_symbols : Separate<["-"], "keep-section-symbols">,
+  HelpText<"(integrated-as) Retain section symbols">,
+  MarshallingInfoFlag<CodeGenOpts<"KeepSectionSymbols">>;
+
 } // let Flags = [CC1AsOption]
 
 //===----------------------------------------------------------------------===//
Index: clang/include/clang/Basic/CodeGenOptions.def
===================================================================
--- clang/include/clang/Basic/CodeGenOptions.def
+++ clang/include/clang/Basic/CodeGenOptions.def
@@ -31,6 +31,7 @@
 ENUM_CODEGENOPT(CompressDebugSections, llvm::DebugCompressionType, 2,
                 llvm::DebugCompressionType::None)
 CODEGENOPT(RelaxELFRelocations, 1, 0) ///< -Wa,--mrelax-relocations
+CODEGENOPT(KeepSectionSymbols, 1, 0) ///< -Wa,-generate-unused-section-symbols=yes
 CODEGENOPT(AsmVerbose        , 1, 0) ///< -dA, -fverbose-asm.
 CODEGENOPT(Dwarf64           , 1, 0) ///< -gdwarf64.
 CODEGENOPT(Dwarf32           , 1, 1) ///< -gdwarf32.
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to