[PATCH] D81359: [ELF] Add --[no-]relax to disable x86-64 GOTPCRELX relaxation

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 7 21:20:55 PDT 2020


MaskRay created this revision.
MaskRay added reviewers: grimar, ruiu.
Herald added subscribers: llvm-commits, luismarques, s.egerton, lenary, PkmX, simoncook, arichardson, emaste.
Herald added a reviewer: espindola.
Herald added a project: LLVM.

to match GNU ld. TLS relaxations are not affected.

For RISC-V, --no-relax similarly disables linker relaxations.
Linux kernel specifies --no-relax when CONFIG_DYNAMIC_FTRACE is specified
(since http://git.kernel.org/linus/a1d2a6b4cee858a2f27eebce731fbf1dfd72cb4e ).
LLD does not implement the relaxations, so this option is a no-op.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D81359

Files:
  lld/ELF/Arch/X86_64.cpp
  lld/ELF/Config.h
  lld/ELF/Driver.cpp
  lld/ELF/Options.td
  lld/docs/ld.lld.1
  lld/test/ELF/x86-64-gotpc-relax.s


Index: lld/test/ELF/x86-64-gotpc-relax.s
===================================================================
--- lld/test/ELF/x86-64-gotpc-relax.s
+++ lld/test/ELF/x86-64-gotpc-relax.s
@@ -4,6 +4,13 @@
 # RUN: llvm-readobj -r %t1 | FileCheck --check-prefix=RELOC %s
 # RUN: llvm-objdump -d %t1 | FileCheck --check-prefix=DISASM %s
 
+# RUN: ld.lld --relax %t.o -o %t2
+# RUN: llvm-objdump -d %t2 | FileCheck --check-prefix=DISASM %s
+
+## --no-relax can disable relaxations.
+# RUN: ld.lld --no-relax %t.o -o %t3
+# RUN: llvm-objdump -d %t3 | FileCheck --check-prefix=NORELAX %s
+
 ## There is no relocations.
 # RELOC:    Relocations [
 # RELOC:    ]
@@ -50,6 +57,11 @@
 # DISASM-NEXT: jmpq  *4119(%rip)
 # DISASM-NEXT: jmpq  *4113(%rip)
 
+# NORELAX-LABEL: <foo>:
+# NORELAX-COUNT-12: movq
+# NORELAX-COUNT-6:  callq *
+# NORELAX-COUNT-6:  jmpq *
+
 .text
 .globl foo
 .type foo, @function
Index: lld/docs/ld.lld.1
===================================================================
--- lld/docs/ld.lld.1
+++ lld/docs/ld.lld.1
@@ -308,6 +308,8 @@
 Page align sections.
 .It Fl -no-omagic
 Do not set the text data sections to be writable, page align sections.
+.It Fl -no-relax
+Disable target-specific relaxations. This currently disables GOTPCRELX relaxation on x86-64.
 .It Fl -no-rosegment
 Do not put read-only non-executable sections in their own segment.
 .It Fl -no-undefined-version
Index: lld/ELF/Options.td
===================================================================
--- lld/ELF/Options.td
+++ lld/ELF/Options.td
@@ -337,6 +337,10 @@
 def print_map: F<"print-map">,
   HelpText<"Print a link map to the standard output">;
 
+defm relax: BB<"relax",
+  "Enable target-specific relaxations (default)",
+  "Disable target-specific relaxations">;
+
 defm reproduce: Eq<"reproduce", "Write a tar file containing input files and command line options to reproduce link">;
 
 defm rosegment: BB<"rosegment",
Index: lld/ELF/Driver.cpp
===================================================================
--- lld/ELF/Driver.cpp
+++ lld/ELF/Driver.cpp
@@ -974,6 +974,7 @@
   config->printArchiveStats = args.getLastArgValue(OPT_print_archive_stats);
   config->printSymbolOrder =
       args.getLastArgValue(OPT_print_symbol_order);
+  config->relax = args.hasFlag(OPT_relax, OPT_no_relax, true);
   config->rpath = getRpath(args);
   config->relocatable = args.hasArg(OPT_relocatable);
   config->saveTemps = args.hasArg(OPT_save_temps);
Index: lld/ELF/Config.h
===================================================================
--- lld/ELF/Config.h
+++ lld/ELF/Config.h
@@ -185,6 +185,7 @@
   bool pie;
   bool printGcSections;
   bool printIcfSections;
+  bool relax;
   bool relocatable;
   bool relrPackDynRelocs;
   bool saveTemps;
Index: lld/ELF/Arch/X86_64.cpp
===================================================================
--- lld/ELF/Arch/X86_64.cpp
+++ lld/ELF/Arch/X86_64.cpp
@@ -730,7 +730,8 @@
 
 RelExpr X86_64::adjustRelaxExpr(RelType type, const uint8_t *data,
                                 RelExpr relExpr) const {
-  if (type != R_X86_64_GOTPCRELX && type != R_X86_64_REX_GOTPCRELX)
+  if ((type != R_X86_64_GOTPCRELX && type != R_X86_64_REX_GOTPCRELX) ||
+      !config->relax)
     return relExpr;
   const uint8_t op = data[-2];
   const uint8_t modRm = data[-1];


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D81359.269101.patch
Type: text/x-patch
Size: 3304 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200608/9e3d664d/attachment.bin>


More information about the llvm-commits mailing list