[llvm] dad7f79 - [ARM] Set STT_TLS for a few relocation specifiers

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 21 22:39:59 PDT 2025


Author: Fangrui Song
Date: 2025-03-21T22:39:55-07:00
New Revision: dad7f799b4b7ad85472cebf7e0b433e8067894ae

URL: https://github.com/llvm/llvm-project/commit/dad7f799b4b7ad85472cebf7e0b433e8067894ae
DIFF: https://github.com/llvm/llvm-project/commit/dad7f799b4b7ad85472cebf7e0b433e8067894ae.diff

LOG: [ARM] Set STT_TLS for a few relocation specifiers

Symbols referenced through TLSLDO and FDPIC TLS-specific specifiers
should be set to STT_TLS as well.

Added: 
    

Modified: 
    llvm/lib/Target/ARM/MCTargetDesc/ARMELFObjectWriter.cpp
    llvm/test/MC/ARM/fdpic.s
    llvm/test/MC/ARM/symbol-variants.s

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/ARM/MCTargetDesc/ARMELFObjectWriter.cpp b/llvm/lib/Target/ARM/MCTargetDesc/ARMELFObjectWriter.cpp
index ad7e27979dd90..6ae5705783f9b 100644
--- a/llvm/lib/Target/ARM/MCTargetDesc/ARMELFObjectWriter.cpp
+++ b/llvm/lib/Target/ARM/MCTargetDesc/ARMELFObjectWriter.cpp
@@ -81,7 +81,7 @@ unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
   unsigned Kind = Fixup.getTargetKind();
   if (Kind >= FirstLiteralRelocationKind)
     return Kind - FirstLiteralRelocationKind;
-  MCSymbolRefExpr::VariantKind Modifier = Target.getAccessVariant();
+  MCSymbolRefExpr::VariantKind Specifier = Target.getAccessVariant();
   auto CheckFDPIC = [&](uint32_t Type) {
     if (getOSABI() != ELF::ELFOSABI_ARM_FDPIC)
       Ctx.reportError(Fixup.getLoc(),
@@ -91,13 +91,31 @@ unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
     return Type;
   };
 
+  switch (Specifier) {
+  case MCSymbolRefExpr::VK_GOTTPOFF:
+  case MCSymbolRefExpr::VK_GOTTPOFF_FDPIC:
+  case MCSymbolRefExpr::VK_TLSCALL:
+  case MCSymbolRefExpr::VK_TLSDESC:
+  case MCSymbolRefExpr::VK_TLSGD:
+  case MCSymbolRefExpr::VK_TLSGD_FDPIC:
+  case MCSymbolRefExpr::VK_TLSLDM:
+  case MCSymbolRefExpr::VK_TLSLDM_FDPIC:
+  case MCSymbolRefExpr::VK_ARM_TLSLDO:
+  case MCSymbolRefExpr::VK_TPOFF:
+    if (auto *S = Target.getSymA())
+      cast<MCSymbolELF>(S->getSymbol()).setType(ELF::STT_TLS);
+    break;
+  default:
+    break;
+  }
+
   if (IsPCRel) {
     switch (Fixup.getTargetKind()) {
     default:
       Ctx.reportError(Fixup.getLoc(), "unsupported relocation type");
       return ELF::R_ARM_NONE;
     case FK_Data_4:
-      switch (Modifier) {
+      switch (Specifier) {
       default:
         Ctx.reportError(Fixup.getLoc(),
                         "invalid fixup for 4-byte pc-relative data relocation");
@@ -120,7 +138,7 @@ unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
       }
     case ARM::fixup_arm_blx:
     case ARM::fixup_arm_uncondbl:
-      switch (Modifier) {
+      switch (Specifier) {
       case MCSymbolRefExpr::VK_PLT:
         return ELF::R_ARM_CALL;
       case MCSymbolRefExpr::VK_TLSCALL:
@@ -158,7 +176,7 @@ unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
       return ELF::R_ARM_THM_JUMP8;
     case ARM::fixup_arm_thumb_bl:
     case ARM::fixup_arm_thumb_blx:
-      switch (Modifier) {
+      switch (Specifier) {
       case MCSymbolRefExpr::VK_TLSCALL:
         return ELF::R_ARM_THM_TLS_CALL;
       default:
@@ -189,7 +207,7 @@ unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
     Ctx.reportError(Fixup.getLoc(), "unsupported relocation type");
     return ELF::R_ARM_NONE;
   case FK_Data_1:
-    switch (Modifier) {
+    switch (Specifier) {
     default:
       Ctx.reportError(Fixup.getLoc(),
                       "invalid fixup for 1-byte data relocation");
@@ -198,7 +216,7 @@ unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
       return ELF::R_ARM_ABS8;
     }
   case FK_Data_2:
-    switch (Modifier) {
+    switch (Specifier) {
     default:
       Ctx.reportError(Fixup.getLoc(),
                       "invalid fixup for 2-byte data relocation");
@@ -207,7 +225,7 @@ unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
       return ELF::R_ARM_ABS16;
     }
   case FK_Data_4:
-    switch (Modifier) {
+    switch (Specifier) {
     default:
       Ctx.reportError(Fixup.getLoc(),
                       "invalid fixup for 4-byte data relocation");
@@ -263,7 +281,7 @@ unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
   case ARM::fixup_arm_uncondbranch:
     return ELF::R_ARM_JUMP24;
   case ARM::fixup_arm_movt_hi16:
-    switch (Modifier) {
+    switch (Specifier) {
     default:
       Ctx.reportError(Fixup.getLoc(), "invalid fixup for ARM MOVT instruction");
       return ELF::R_ARM_NONE;
@@ -273,7 +291,7 @@ unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
       return ELF::R_ARM_MOVT_BREL;
     }
   case ARM::fixup_arm_movw_lo16:
-    switch (Modifier) {
+    switch (Specifier) {
     default:
       Ctx.reportError(Fixup.getLoc(), "invalid fixup for ARM MOVW instruction");
       return ELF::R_ARM_NONE;
@@ -283,7 +301,7 @@ unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
       return ELF::R_ARM_MOVW_BREL_NC;
     }
   case ARM::fixup_t2_movt_hi16:
-    switch (Modifier) {
+    switch (Specifier) {
     default:
       Ctx.reportError(Fixup.getLoc(),
                       "invalid fixup for Thumb MOVT instruction");
@@ -294,7 +312,7 @@ unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
       return ELF::R_ARM_THM_MOVT_BREL;
     }
   case ARM::fixup_t2_movw_lo16:
-    switch (Modifier) {
+    switch (Specifier) {
     default:
       Ctx.reportError(Fixup.getLoc(),
                       "invalid fixup for Thumb MOVW instruction");

diff  --git a/llvm/test/MC/ARM/fdpic.s b/llvm/test/MC/ARM/fdpic.s
index 1dfedc59ada61..eb3cc614b6c31 100644
--- a/llvm/test/MC/ARM/fdpic.s
+++ b/llvm/test/MC/ARM/fdpic.s
@@ -1,5 +1,5 @@
 # RUN: llvm-mc -triple=armv7-linux-gnueabi %s | FileCheck %s --check-prefix=ASM
-# RUN: llvm-mc -filetype=obj -triple=armv7-linux-gnueabi --fdpic %s | llvm-readelf -h -r - | FileCheck %s
+# RUN: llvm-mc -filetype=obj -triple=armv7-linux-gnueabi --fdpic %s | llvm-readelf -h -r -s - | FileCheck %s --implicit-check-not=TLS
 
 # RUN: not llvm-mc -filetype=obj -triple=armv7-linux-gnueabi %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR
 
@@ -14,9 +14,13 @@
 # CHECK:      R_ARM_FUNCDESC        00000000 f
 # CHECK-NEXT: R_ARM_GOTFUNCDESC     00000000 f
 # CHECK-NEXT: R_ARM_GOTOFFFUNCDESC  00000000 f
-# CHECK-NEXT: R_ARM_TLS_GD32_FDPIC  00000000 tls
-# CHECK-NEXT: R_ARM_TLS_LDM32_FDPIC 00000000 tls
-# CHECK-NEXT: R_ARM_TLS_IE32_FDPIC  00000000 tls
+# CHECK-NEXT: R_ARM_TLS_GD32_FDPIC  00000000 tls0
+# CHECK-NEXT: R_ARM_TLS_LDM32_FDPIC 00000000 tls1
+# CHECK-NEXT: R_ARM_TLS_IE32_FDPIC  00000000 tls2
+
+# CHECK:      TLS GLOBAL DEFAULT UND tls0
+# CHECK:      TLS GLOBAL DEFAULT UND tls1
+# CHECK:      TLS GLOBAL DEFAULT UND tls2
 
 .data
 # ERR: [[#@LINE+1]]:7: error: relocation R_ARM_FUNCDESC only supported in FDPIC mode
@@ -26,8 +30,8 @@
 # ERR: [[#@LINE+1]]:7: error: relocation R_ARM_GOTOFFFUNCDESC only supported in FDPIC mode
 .long f(GOTOFFFUNCDESC)
 # ERR: [[#@LINE+1]]:7: error: relocation R_ARM_TLS_GD32_FDPIC only supported in FDPIC mode
-.long tls(tlsgd_fdpic)
+.long tls0(tlsgd_fdpic)
 # ERR: [[#@LINE+1]]:7: error: relocation R_ARM_TLS_LDM32_FDPIC only supported in FDPIC mode
-.long tls(tlsldm_fdpic)
+.long tls1(tlsldm_fdpic)
 # ERR: [[#@LINE+1]]:7: error: relocation R_ARM_TLS_IE32_FDPIC only supported in FDPIC mode
-.long tls(gottpoff_fdpic)
+.long tls2(gottpoff_fdpic)

diff  --git a/llvm/test/MC/ARM/symbol-variants.s b/llvm/test/MC/ARM/symbol-variants.s
index ea8f2ec47d403..90e22df00f706 100644
--- a/llvm/test/MC/ARM/symbol-variants.s
+++ b/llvm/test/MC/ARM/symbol-variants.s
@@ -77,8 +77,8 @@ bl f05(plt)
 .word f21(tlsldo)
 @CHECK: 50 R_ARM_TLS_LDO32 f20
 @CHECK: 54 R_ARM_TLS_LDO32 f21
- at READELF: NOTYPE GLOBAL DEFAULT UND f20
- at READELF: NOTYPE GLOBAL DEFAULT UND f21
+ at READELF: TLS GLOBAL DEFAULT UND f20
+ at READELF: TLS GLOBAL DEFAULT UND f21
 
 @ tlscall
 .word f22(TLSCALL)


        


More information about the llvm-commits mailing list