[PATCH] D82536: [AVR] Use correct relocation for function pointers in globals

Ayke via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 25 03:44:50 PDT 2020


aykevl updated this revision to Diff 273289.
aykevl added a comment.

- fixed MC test failure


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D82536/new/

https://reviews.llvm.org/D82536

Files:
  llvm/lib/Target/AVR/MCTargetDesc/AVRELFObjectWriter.cpp
  llvm/test/CodeGen/AVR/global-relocs.ll
  llvm/test/MC/AVR/relocations-abs.s


Index: llvm/test/MC/AVR/relocations-abs.s
===================================================================
--- llvm/test/MC/AVR/relocations-abs.s
+++ llvm/test/MC/AVR/relocations-abs.s
@@ -1,8 +1,11 @@
 ; RUN: llvm-mc -filetype=obj -triple=avr %s | llvm-objdump -d -r - | FileCheck %s
 
+; WARNING: the behavior below differs from avr-gcc, avr-gcc will use R_AVR_16
+; instead of R_AVR_16_PM. I think this is a bug in avr-gcc.
+
 ; CHECK: <bar>:
 ; CHECK-NEXT: 00 00 nop
-; CHECK-NEXT: R_AVR_16 .text+0x2
+; CHECK-NEXT: R_AVR_16_PM .text+0x2
 bar:
     .short 1f
 1:
Index: llvm/test/CodeGen/AVR/global-relocs.ll
===================================================================
--- /dev/null
+++ llvm/test/CodeGen/AVR/global-relocs.ll
@@ -0,0 +1,42 @@
+; RUN: llc < %s -march=avr -filetype=obj | llvm-objdump -Dr - | FileCheck %s
+
+; This test checks whether the correct relocation type (R_AVR_16 or
+; R_AVR_16_PM) is used for relocations in global variables (such as function
+; and global variable pointers). Function pointers need a special relocation
+; type so that the linker will use an address in program words (a pointer to
+; program space shifted left by one).
+
+target datalayout = "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8"
+target triple = "avr"
+
+; Some globals to refer to.
+ at someGlobal = dso_local constant i32 5, align 2
+ at someProgmemGlobal = dso_local addrspace(1) constant i16 5, align 2
+
+; Reduced testcase from Rust.
+ at MY_VTABLE = internal constant <{ i8 addrspace(1)* }> <{ i8 addrspace(1)* bitcast (void () addrspace(1)* @wake to i8 addrspace(1)*) }>, align 1
+; CHECK-LABEL: <MY_VTABLE>:
+; CHECK: R_AVR_16_PM wake
+
+ at ptrToFunction = dso_local constant i8 addrspace(1)* bitcast (void () addrspace(1)* @wake to i8 addrspace(1)*), align 1
+; CHECK-LABEL: <ptrToFunction>:
+; CHECK: R_AVR_16_PM wake
+
+ at ptrToGlobal = dso_local constant i32* @someGlobal, align 1
+; CHECK-LABEL: <ptrToGlobal>:
+; CHECK: R_AVR_16 someGlobal
+
+ at ptrToProgmemGlobal = dso_local constant i16 addrspace(1)* @someProgmemGlobal, align 1
+; CHECK-LABEL: <ptrToProgmemGlobal>:
+; CHECK: R_AVR_16 someProgmemGlobal
+
+ at ptrToProgmemGlobal2 = dso_local constant i8 addrspace(1)* bitcast (i16 addrspace(1)* @someProgmemGlobal to i8 addrspace(1)*), align 1
+; CHECK-LABEL: <ptrToProgmemGlobal2>:
+; CHECK: R_AVR_16 someProgmemGlobal
+
+ at ptrToFunction2 = dso_local global void () addrspace(1)* @wake, align 1
+; CHECK-LABEL: <ptrToFunction2>:
+; CHECK: R_AVR_16_PM wake
+
+; A function to refer to.
+declare dso_local void @wake() addrspace(1)
Index: llvm/lib/Target/AVR/MCTargetDesc/AVRELFObjectWriter.cpp
===================================================================
--- llvm/lib/Target/AVR/MCTargetDesc/AVRELFObjectWriter.cpp
+++ llvm/lib/Target/AVR/MCTargetDesc/AVRELFObjectWriter.cpp
@@ -69,8 +69,13 @@
     switch (Modifier) {
     default:
       llvm_unreachable("Unsupported Modifier");
-    case MCSymbolRefExpr::VK_None:
+    case MCSymbolRefExpr::VK_None: {
+      auto &symbol = cast<MCSymbolELF>(Target.getSymA()->getSymbol());
+      if (symbol.getType() == ELF::STT_NOTYPE)
+        // Not a global variable, so probably a function pointer.
+        return ELF::R_AVR_16_PM;
       return ELF::R_AVR_16;
+    }
     case MCSymbolRefExpr::VK_AVR_NONE:
       return ELF::R_AVR_16_PM;
     case MCSymbolRefExpr::VK_AVR_DIFF16:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D82536.273289.patch
Type: text/x-patch
Size: 3364 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200625/c28356db/attachment.bin>


More information about the llvm-commits mailing list