[lld] r322045 - [WebAssembly] Write out undefined symbol addresses as zero
Sam Clegg via llvm-commits
llvm-commits at lists.llvm.org
Mon Jan 8 15:39:11 PST 2018
Author: sbc
Date: Mon Jan 8 15:39:11 2018
New Revision: 322045
URL: http://llvm.org/viewvc/llvm-project?rev=322045&view=rev
Log:
[WebAssembly] Write out undefined symbol addresses as zero
The addresses of undefined symbols that make it into the final
executable (i.e. weak references to non-existent symbols) should
resolve to zero.
Also, make sure to not include function in the indirect function
table if they are not included in the output.
Differential Revision: https://reviews.llvm.org/D41839
Added:
lld/trunk/test/wasm/weak-undefined.ll
- copied, changed from r322014, lld/trunk/test/wasm/weak-external.ll
Removed:
lld/trunk/test/wasm/weak-external.ll
Modified:
lld/trunk/test/wasm/relocatable.ll
lld/trunk/wasm/InputFiles.cpp
lld/trunk/wasm/OutputSections.cpp
lld/trunk/wasm/Symbols.cpp
lld/trunk/wasm/Symbols.h
lld/trunk/wasm/Writer.cpp
Modified: lld/trunk/test/wasm/relocatable.ll
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/wasm/relocatable.ll?rev=322045&r1=322044&r2=322045&view=diff
==============================================================================
--- lld/trunk/test/wasm/relocatable.ll (original)
+++ lld/trunk/test/wasm/relocatable.ll Mon Jan 8 15:39:11 2018
@@ -159,7 +159,7 @@ declare i32 @foo_import() local_unnamed_
; CHECK-NEXT: Offset:
; CHECK-NEXT: Opcode: I32_CONST
; CHECK-NEXT: Value: 16
-; CHECK-NEXT: Content: FFFFFFFF
+; CHECK-NEXT: Content: '00000000'
; CHECK-NEXT: - Type: CUSTOM
; CHECK-NEXT: Name: linking
; CHECK-NEXT: DataSize: 20
Removed: lld/trunk/test/wasm/weak-external.ll
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/wasm/weak-external.ll?rev=322044&view=auto
==============================================================================
--- lld/trunk/test/wasm/weak-external.ll (original)
+++ lld/trunk/test/wasm/weak-external.ll (removed)
@@ -1,86 +0,0 @@
-; RUN: llc -mtriple=wasm32-unknown-unknown-wasm -filetype=obj -o %t.o %s
-; RUN: lld -flavor wasm -strip-debug %t.o -o %t.wasm
-; RUN: obj2yaml %t.wasm | FileCheck %s
-
-; Test that undefined weak externals (global_var) and (foo) don't cause
-; link failures and resolve to zero.
-
- at global_var = extern_weak global i32, align 4
-
-declare extern_weak i32 @foo()
-
-define i8* @get_address_of_foo() #0 {
-entry:
- ret i8* bitcast (i32 ()* @foo to i8*)
-}
-
-define i32* @get_address_of_global_var() #0 {
- ret i32* @global_var
-}
-
-define i32 @_start() #0 {
-entry:
- %0 = load i32, i32* @global_var, align 4
- ret i32 %0
-}
-
-; CHECK: --- !WASM
-; CHECK-NEXT: FileHeader:
-; CHECK-NEXT: Version: 0x00000001
-; CHECK-NEXT: Sections:
-; CHECK-NEXT: - Type: TYPE
-; CHECK-NEXT: Signatures:
-; CHECK-NEXT: - Index: 0
-; CHECK-NEXT: ReturnType: I32
-; CHECK-NEXT: ParamTypes:
-; CHECK-NEXT: - Type: FUNCTION
-; CHECK-NEXT: FunctionTypes: [ 0, 0, 0 ]
-; CHECK-NEXT: - Type: TABLE
-; CHECK-NEXT: Tables:
-; CHECK-NEXT: - ElemType: ANYFUNC
-; CHECK-NEXT: Limits:
-; CHECK-NEXT: Flags: [ HAS_MAX ]
-; CHECK-NEXT: Initial: 0x00000002
-; CHECK-NEXT: Maximum: 0x00000002
-; CHECK-NEXT: - Type: MEMORY
-; CHECK-NEXT: Memories:
-; CHECK-NEXT: - Initial: 0x00000002
-; CHECK-NEXT: - Type: GLOBAL
-; CHECK-NEXT: Globals:
-; CHECK-NEXT: - Type: I32
-; CHECK-NEXT: Mutable: true
-; CHECK-NEXT: InitExpr:
-; CHECK-NEXT: Opcode: I32_CONST
-; CHECK-NEXT: Value: 66560
-; CHECK-NEXT: - Type: EXPORT
-; CHECK-NEXT: Exports:
-; CHECK-NEXT: - Name: memory
-; CHECK-NEXT: Kind: MEMORY
-; CHECK-NEXT: Index: 0
-; CHECK-NEXT: - Name: _start
-; CHECK-NEXT: Kind: FUNCTION
-; CHECK-NEXT: Index: 2
-; CHECK-NEXT: - Name: get_address_of_foo
-; CHECK-NEXT: Kind: FUNCTION
-; CHECK-NEXT: Index: 0
-; CHECK-NEXT: - Name: get_address_of_global_var
-; CHECK-NEXT: Kind: FUNCTION
-; CHECK-NEXT: Index: 1
-; CHECK-NEXT: - Type: ELEM
-; CHECK-NEXT: Segments:
-; CHECK-NEXT: - Offset:
-; CHECK-NEXT: Opcode: I32_CONST
-; CHECK-NEXT: Value: 1
-; CHECK-NEXT: Functions: [ 0 ]
-; CHECK-NEXT: - Type: CODE
-; CHECK-NEXT: Functions:
-; CHECK-NEXT: - Locals:
-; CHECK-NEXT: Body: 4181808080000B
-; CHECK-NEXT: - Locals:
-; CHECK-NEXT: Body: 41FFFFFFFF7F0B
-; CHECK-NEXT: - Locals:
-; CHECK-NEXT: Body: 41002802FFFFFFFF0F0B
-; CHECK-NEXT: - Type: CUSTOM
-; CHECK-NEXT: Name: linking
-; CHECK-NEXT: DataSize: 0
-; CHECK-NEXT: ...
Copied: lld/trunk/test/wasm/weak-undefined.ll (from r322014, lld/trunk/test/wasm/weak-external.ll)
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/wasm/weak-undefined.ll?p2=lld/trunk/test/wasm/weak-undefined.ll&p1=lld/trunk/test/wasm/weak-external.ll&r1=322014&r2=322045&rev=322045&view=diff
==============================================================================
--- lld/trunk/test/wasm/weak-external.ll (original)
+++ lld/trunk/test/wasm/weak-undefined.ll Mon Jan 8 15:39:11 2018
@@ -40,8 +40,8 @@ entry:
; CHECK-NEXT: - ElemType: ANYFUNC
; CHECK-NEXT: Limits:
; CHECK-NEXT: Flags: [ HAS_MAX ]
-; CHECK-NEXT: Initial: 0x00000002
-; CHECK-NEXT: Maximum: 0x00000002
+; CHECK-NEXT: Initial: 0x00000001
+; CHECK-NEXT: Maximum: 0x00000001
; CHECK-NEXT: - Type: MEMORY
; CHECK-NEXT: Memories:
; CHECK-NEXT: - Initial: 0x00000002
@@ -66,20 +66,14 @@ entry:
; CHECK-NEXT: - Name: get_address_of_global_var
; CHECK-NEXT: Kind: FUNCTION
; CHECK-NEXT: Index: 1
-; CHECK-NEXT: - Type: ELEM
-; CHECK-NEXT: Segments:
-; CHECK-NEXT: - Offset:
-; CHECK-NEXT: Opcode: I32_CONST
-; CHECK-NEXT: Value: 1
-; CHECK-NEXT: Functions: [ 0 ]
; CHECK-NEXT: - Type: CODE
; CHECK-NEXT: Functions:
; CHECK-NEXT: - Locals:
-; CHECK-NEXT: Body: 4181808080000B
+; CHECK-NEXT: Body: 4180808080000B
; CHECK-NEXT: - Locals:
-; CHECK-NEXT: Body: 41FFFFFFFF7F0B
+; CHECK-NEXT: Body: 4180808080000B
; CHECK-NEXT: - Locals:
-; CHECK-NEXT: Body: 41002802FFFFFFFF0F0B
+; CHECK-NEXT: Body: 4100280280808080000B
; CHECK-NEXT: - Type: CUSTOM
; CHECK-NEXT: Name: linking
; CHECK-NEXT: DataSize: 0
Modified: lld/trunk/wasm/InputFiles.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/InputFiles.cpp?rev=322045&r1=322044&r2=322045&view=diff
==============================================================================
--- lld/trunk/wasm/InputFiles.cpp (original)
+++ lld/trunk/wasm/InputFiles.cpp Mon Jan 8 15:39:11 2018
@@ -83,7 +83,7 @@ uint32_t ObjFile::relocateTypeIndex(uint
uint32_t ObjFile::relocateTableIndex(uint32_t Original) const {
Symbol *Sym = getTableSymbol(Original);
- uint32_t Index = Sym->getTableIndex();
+ uint32_t Index = Sym->hasTableIndex() ? Sym->getTableIndex() : 0;
DEBUG(dbgs() << "relocateTableIndex: " << toString(*Sym) << ": " << Original
<< " -> " << Index << "\n");
return Index;
@@ -91,7 +91,7 @@ uint32_t ObjFile::relocateTableIndex(uin
uint32_t ObjFile::relocateGlobalIndex(uint32_t Original) const {
Symbol *Sym = getGlobalSymbol(Original);
- uint32_t Index = Sym->getOutputIndex();
+ uint32_t Index = Sym->hasOutputIndex() ? Sym->getOutputIndex() : 0;
DEBUG(dbgs() << "relocateGlobalIndex: " << toString(*Sym) << ": " << Original
<< " -> " << Index << "\n");
return Index;
Modified: lld/trunk/wasm/OutputSections.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/OutputSections.cpp?rev=322045&r1=322044&r2=322045&view=diff
==============================================================================
--- lld/trunk/wasm/OutputSections.cpp (original)
+++ lld/trunk/wasm/OutputSections.cpp Mon Jan 8 15:39:11 2018
@@ -157,16 +157,12 @@ static void calcRelocations(const ObjFil
if (Config->EmitRelocs)
NewReloc.NewIndex = calcNewIndex(File, Reloc);
- else
- NewReloc.NewIndex = UINT32_MAX;
switch (Reloc.Type) {
case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
case R_WEBASSEMBLY_MEMORY_ADDR_I32:
case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
- NewReloc.Value = File.getRelocatedAddress(Reloc.Index);
- if (NewReloc.Value != UINT32_MAX)
- NewReloc.Value += Reloc.Addend;
+ NewReloc.Value = File.getRelocatedAddress(Reloc.Index) + Reloc.Addend;
break;
default:
NewReloc.Value = calcNewIndex(File, Reloc);
Modified: lld/trunk/wasm/Symbols.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/Symbols.cpp?rev=322045&r1=322044&r2=322045&view=diff
==============================================================================
--- lld/trunk/wasm/Symbols.cpp (original)
+++ lld/trunk/wasm/Symbols.cpp Mon Jan 8 15:39:11 2018
@@ -40,7 +40,7 @@ uint32_t Symbol::getVirtualAddress() con
assert(isGlobal());
DEBUG(dbgs() << "getVirtualAddress: " << getName() << "\n");
if (isUndefined())
- return UINT32_MAX;
+ return 0;
if (VirtualAddress.hasValue())
return VirtualAddress.getValue();
@@ -53,12 +53,6 @@ uint32_t Symbol::getVirtualAddress() con
return Segment->translateVA(Global.InitExpr.Value.Int32);
}
-uint32_t Symbol::getOutputIndex() const {
- if (isUndefined() && isWeak())
- return 0;
- return OutputIndex.getValue();
-}
-
void Symbol::setVirtualAddress(uint32_t Value) {
DEBUG(dbgs() << "setVirtualAddress " << Name << " -> " << Value << "\n");
assert(!VirtualAddress.hasValue());
Modified: lld/trunk/wasm/Symbols.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/Symbols.h?rev=322045&r1=322044&r2=322045&view=diff
==============================================================================
--- lld/trunk/wasm/Symbols.h (original)
+++ lld/trunk/wasm/Symbols.h Mon Jan 8 15:39:11 2018
@@ -71,23 +71,28 @@ public:
bool hasFunctionType() const { return FunctionType != nullptr; }
const WasmSignature &getFunctionType() const;
- uint32_t getOutputIndex() const;
- uint32_t getTableIndex() const { return TableIndex.getValue(); }
- // Returns the virtual address of a defined global.
- // Only works for globals, not functions.
- uint32_t getVirtualAddress() const;
+ uint32_t getOutputIndex() const { return OutputIndex.getValue(); }
+
+ // Returns true if an output index has been set for this symbol
+ bool hasOutputIndex() const { return OutputIndex.hasValue(); }
// Set the output index of the symbol (in the function or global index
// space of the output object.
void setOutputIndex(uint32_t Index);
+ uint32_t getTableIndex() const { return TableIndex.getValue(); }
+
// Returns true if a table index has been set for this symbol
bool hasTableIndex() const { return TableIndex.hasValue(); }
// Set the table index of the symbol
void setTableIndex(uint32_t Index);
+ // Returns the virtual address of a defined global.
+ // Only works for globals, not functions.
+ uint32_t getVirtualAddress() const;
+
void setVirtualAddress(uint32_t VA);
void update(Kind K, InputFile *F = nullptr, const WasmSymbol *Sym = nullptr,
Modified: lld/trunk/wasm/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/Writer.cpp?rev=322045&r1=322044&r2=322045&view=diff
==============================================================================
--- lld/trunk/wasm/Writer.cpp (original)
+++ lld/trunk/wasm/Writer.cpp Mon Jan 8 15:39:11 2018
@@ -629,12 +629,14 @@ void Writer::assignSymbolIndexes() {
Sym->setOutputIndex(GlobalIndex++);
}
}
+ }
+ for (ObjFile *File : Symtab->ObjectFiles) {
for (Symbol *Sym : File->getTableSymbols()) {
- if (!Sym->hasTableIndex()) {
- Sym->setTableIndex(TableIndex++);
- IndirectFunctions.emplace_back(Sym);
- }
+ if (Sym->hasTableIndex() || !Sym->hasOutputIndex())
+ continue;
+ Sym->setTableIndex(TableIndex++);
+ IndirectFunctions.emplace_back(Sym);
}
}
}
More information about the llvm-commits
mailing list