[llvm] r305263 - [WebAssembly] Fix symbol type for addresses of external functions

Sam Clegg via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 12 18:42:21 PDT 2017


Author: sbc
Date: Mon Jun 12 20:42:21 2017
New Revision: 305263

URL: http://llvm.org/viewvc/llvm-project?rev=305263&view=rev
Log:
[WebAssembly] Fix symbol type for addresses of external functions

These symbols were previously not being marked as functions
so were appearing as globals instead, and with the incorrect
relocation type.

Without this fix, objects that take address of external
functions include them as global imports rather than function
imports which then fails at link time.

Differential Revision: https://reviews.llvm.org/D34068

Added:
    llvm/trunk/test/MC/WebAssembly/external-func-address.ll
Modified:
    llvm/trunk/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
    llvm/trunk/test/MC/WebAssembly/external-data.ll

Modified: llvm/trunk/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp?rev=305263&r1=305262&r2=305263&view=diff
==============================================================================
--- llvm/trunk/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp (original)
+++ llvm/trunk/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp Mon Jun 12 20:42:21 2017
@@ -33,6 +33,8 @@
 #include "llvm/MC/MCContext.h"
 #include "llvm/MC/MCStreamer.h"
 #include "llvm/MC/MCSymbol.h"
+#include "llvm/MC/MCSymbolWasm.h"
+#include "llvm/MC/MCSymbolELF.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/TargetRegistry.h"
 #include "llvm/Support/raw_ostream.h"
@@ -218,9 +220,13 @@ void WebAssemblyAsmPrinter::EmitInstruct
 
 const MCExpr *WebAssemblyAsmPrinter::lowerConstant(const Constant *CV) {
   if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
-    if (GV->getValueType()->isFunctionTy())
+    if (GV->getValueType()->isFunctionTy()) {
+      MCSymbol* Sym = getSymbol(GV);
+      if (!isa<MCSymbolELF>(Sym))
+        cast<MCSymbolWasm>(Sym)->setIsFunction(true);
       return MCSymbolRefExpr::create(
-          getSymbol(GV), MCSymbolRefExpr::VK_WebAssembly_FUNCTION, OutContext);
+          Sym, MCSymbolRefExpr::VK_WebAssembly_FUNCTION, OutContext);
+    }
   return AsmPrinter::lowerConstant(CV);
 }
 

Modified: llvm/trunk/test/MC/WebAssembly/external-data.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/WebAssembly/external-data.ll?rev=305263&r1=305262&r2=305263&view=diff
==============================================================================
--- llvm/trunk/test/MC/WebAssembly/external-data.ll (original)
+++ llvm/trunk/test/MC/WebAssembly/external-data.ll Mon Jun 12 20:42:21 2017
@@ -2,10 +2,10 @@
 ; Verify relocations are correctly generated for addresses of externals
 ; in the data section.
 
-declare i32 @f1(...)
+ at myimport = external global i32, align 4
 
 @foo = global i64 7, align 4
- at far = local_unnamed_addr global i32 (...)* @f1, align 4
+ at bar = hidden global i32* @myimport, align 4
 
 ; CHECK:   - Type:            DATA
 ; CHECK:     Relocations:

Added: llvm/trunk/test/MC/WebAssembly/external-func-address.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/WebAssembly/external-func-address.ll?rev=305263&view=auto
==============================================================================
--- llvm/trunk/test/MC/WebAssembly/external-func-address.ll (added)
+++ llvm/trunk/test/MC/WebAssembly/external-func-address.ll Mon Jun 12 20:42:21 2017
@@ -0,0 +1,25 @@
+; RUN: llc -mtriple wasm32-unknown-unknown-wasm -filetype=obj %s -o - | obj2yaml | FileCheck %s
+; Verify that addresses of external functions generate correctly typed
+; imports and relocations or type R_TABLE_INDEX_I32.
+
+declare void @f1() #1
+ at ptr_to_f1 = hidden global void ()* @f1, align 4
+
+
+; CHECK:   - Type:            IMPORT
+; CHECK:     Imports:
+; CHECK:       - Module:          env
+; CHECK:         Field:           f1
+; CHECK:         Kind:            FUNCTION
+; CHECK:         SigIndex:        0
+; CHECK:   - Type:            ELEM
+; CHECK:     Segments:
+; CHECK:       - Offset:
+; CHECK:           Opcode:          I32_CONST
+; CHECK:           Value:           0
+; CHECK:         Functions:       [ 0 ]
+; CHECK:   - Type:            DATA
+; CHECK:     Relocations:
+; CHECK:       - Type:            R_WEBASSEMBLY_TABLE_INDEX_I32
+; CHECK:         Index:           0
+; CHECK:         Offset:          0x00000006




More information about the llvm-commits mailing list