[WebAssembly] Always start table index at 1, even for relocatable output

Previously llvm was using 0 as the first table index for wasm object
files but now that has switched to 1 we can have the output of lld
do the same and simplify the code.

Patch by Nicholas Wilson!

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

llvm-svn: 323378
This commit is contained in:
Sam Clegg 2018-01-24 21:37:30 +00:00
parent 60c13c7712
commit 48bbd63fea
5 changed files with 33 additions and 36 deletions

View File

@ -232,8 +232,8 @@
; RELOC-NEXT: - ElemType: ANYFUNC
; RELOC-NEXT: Limits:
; RELOC-NEXT: Flags: [ HAS_MAX ]
; RELOC-NEXT: Initial: 0x00000006
; RELOC-NEXT: Maximum: 0x00000006
; RELOC-NEXT: Initial: 0x00000007
; RELOC-NEXT: Maximum: 0x00000007
; RELOC-NEXT: - Type: MEMORY
; RELOC-NEXT: Memories:
; RELOC-NEXT: - Initial: 0x00000001
@ -353,7 +353,7 @@
; RELOC-NEXT: Segments:
; RELOC-NEXT: - Offset:
; RELOC-NEXT: Opcode: I32_CONST
; RELOC-NEXT: Value: 0
; RELOC-NEXT: Value: 1
; RELOC-NEXT: Functions: [ 0, 1, 2, 9, 10, 11 ]
; RELOC-NEXT: - Type: CODE
; RELOC-NEXT: Relocations:
@ -413,14 +413,14 @@
; RELOC-NEXT: Locals:
; RELOC-NEXT: Body: 4190808080000B
; RELOC-NEXT: - Index: 6
; RELOC-NEXT: Locals:
; RELOC-NEXT: Body: 4180808080000B
; RELOC-NEXT: - Index: 7
; RELOC-NEXT: Locals:
; RELOC-NEXT: Locals:
; RELOC-NEXT: Body: 4181808080000B
; RELOC-NEXT: - Index: 8
; RELOC-NEXT: Locals:
; RELOC-NEXT: - Index: 7
; RELOC-NEXT: Locals:
; RELOC-NEXT: Body: 4182808080000B
; RELOC-NEXT: - Index: 8
; RELOC-NEXT: Locals:
; RELOC-NEXT: Body: 4183808080000B
; RELOC-NEXT: - Index: 9
; RELOC-NEXT: Locals:
; RELOC-NEXT: Body: 41020B
@ -440,14 +440,14 @@
; RELOC-NEXT: Locals:
; RELOC-NEXT: Body: 4194808080000B
; RELOC-NEXT: - Index: 15
; RELOC-NEXT: Locals:
; RELOC-NEXT: Body: 4183808080000B
; RELOC-NEXT: - Index: 16
; RELOC-NEXT: Locals:
; RELOC-NEXT: Locals:
; RELOC-NEXT: Body: 4184808080000B
; RELOC-NEXT: - Index: 17
; RELOC-NEXT: Locals:
; RELOC-NEXT: - Index: 16
; RELOC-NEXT: Locals:
; RELOC-NEXT: Body: 4185808080000B
; RELOC-NEXT: - Index: 17
; RELOC-NEXT: Locals:
; RELOC-NEXT: Body: 4186808080000B
; RELOC-NEXT: - Type: DATA
; RELOC-NEXT: Segments:
; RELOC-NEXT: - SectionOffset: 6

View File

@ -71,8 +71,8 @@ entry:
; CHECK-NEXT: - ElemType: ANYFUNC
; CHECK-NEXT: Limits:
; CHECK-NEXT: Flags: [ HAS_MAX ]
; CHECK-NEXT: Initial: 0x00000003
; CHECK-NEXT: Maximum: 0x00000003
; CHECK-NEXT: Initial: 0x00000004
; CHECK-NEXT: Maximum: 0x00000004
; CHECK-NEXT: - Type: MEMORY
; CHECK-NEXT: Memories:
; CHECK-NEXT: - Initial: 0x00000001
@ -147,7 +147,7 @@ entry:
; CHECK-NEXT: Segments:
; CHECK-NEXT: - Offset:
; CHECK-NEXT: Opcode: I32_CONST
; CHECK-NEXT: Value: 0
; CHECK-NEXT: Value: 1
; CHECK-NEXT: Functions: [ 4, 1, 2 ]
; CHECK-NEXT: - Type: CODE
; CHECK-NEXT: Relocations:
@ -202,19 +202,19 @@ entry:
; CHECK-NEXT: Offset:
; CHECK-NEXT: Opcode: I32_CONST
; CHECK-NEXT: Value: 8
; CHECK-NEXT: Content: '00000000'
; CHECK-NEXT: Content: '01000000'
; CHECK-NEXT: - SectionOffset: 27
; CHECK-NEXT: MemoryIndex: 0
; CHECK-NEXT: Offset:
; CHECK-NEXT: Opcode: I32_CONST
; CHECK-NEXT: Value: 12
; CHECK-NEXT: Content: '01000000'
; CHECK-NEXT: Content: '02000000'
; CHECK-NEXT: - SectionOffset: 36
; CHECK-NEXT: MemoryIndex: 0
; CHECK-NEXT: Offset:
; CHECK-NEXT: Opcode: I32_CONST
; CHECK-NEXT: Value: 16
; CHECK-NEXT: Content: '02000000'
; CHECK-NEXT: Content: '03000000'
; CHECK-NEXT: - SectionOffset: 45
; CHECK-NEXT: MemoryIndex: 0
; CHECK-NEXT: Offset:

View File

@ -34,8 +34,8 @@ entry:
; CHECK-NEXT: - ElemType: ANYFUNC
; CHECK-NEXT: Limits:
; CHECK-NEXT: Flags: [ HAS_MAX ]
; CHECK-NEXT: Initial: 0x00000000
; CHECK-NEXT: Maximum: 0x00000000
; CHECK-NEXT: Initial: 0x00000001
; CHECK-NEXT: Maximum: 0x00000001
; CHECK-NEXT: - Type: MEMORY
; CHECK-NEXT: Memories:
; CHECK-NEXT: - Initial: 0x00000000

View File

@ -164,8 +164,8 @@ entry:
; RELOC-NEXT: - ElemType: ANYFUNC
; RELOC-NEXT: Limits:
; RELOC-NEXT: Flags: [ HAS_MAX ]
; RELOC-NEXT: Initial: 0x00000002
; RELOC-NEXT: Maximum: 0x00000002
; RELOC-NEXT: Initial: 0x00000003
; RELOC-NEXT: Maximum: 0x00000003
; RELOC-NEXT: - Type: MEMORY
; RELOC-NEXT: Memories:
; RELOC-NEXT: - Initial: 0x00000000
@ -196,7 +196,7 @@ entry:
; RELOC-NEXT: Segments:
; RELOC-NEXT: - Offset:
; RELOC-NEXT: Opcode: I32_CONST
; RELOC-NEXT: Value: 0
; RELOC-NEXT: Value: 1
; RELOC-NEXT: Functions: [ 1, 1 ]
; RELOC-NEXT: - Type: CODE
; RELOC-NEXT: Relocations:
@ -256,12 +256,12 @@ entry:
; RELOC-NEXT: Locals:
; RELOC-NEXT: - Type: I32
; RELOC-NEXT: Count: 2
; RELOC-NEXT: Body: 23808080800041106B220024808080800020004180808080003602081081808080002101200041106A24808080800020010B
; RELOC-NEXT: Body: 23808080800041106B220024808080800020004181808080003602081081808080002101200041106A24808080800020010B
; RELOC-NEXT: - Index: 5
; RELOC-NEXT: Locals:
; RELOC-NEXT: - Type: I32
; RELOC-NEXT: Count: 2
; RELOC-NEXT: Body: 23808080800041106B220024808080800020004181808080003602081081808080002101200041106A24808080800020010B
; RELOC-NEXT: Body: 23808080800041106B220024808080800020004182808080003602081081808080002101200041106A24808080800020010B
; RELOC-NEXT: - Type: CUSTOM
; RELOC-NEXT: Name: linking
; RELOC-NEXT: DataSize: 0

View File

@ -35,6 +35,7 @@ using namespace lld;
using namespace lld::wasm;
static constexpr int kStackAlignment = 16;
static constexpr int kInitialTableOffset = 1;
namespace {
@ -115,7 +116,6 @@ private:
uint64_t FileSize = 0;
uint32_t DataSize = 0;
uint32_t NumMemoryPages = 0;
uint32_t InitialTableOffset = 0;
std::vector<const WasmSignature *> Types;
DenseMap<WasmSignature, int32_t, WasmSignatureDenseMapInfo> TypeIndices;
@ -253,7 +253,7 @@ void Writer::createTableSection() {
// no address-taken function will fail at validation time since it is
// a validation error to include a call_indirect instruction if there
// is not table.
uint32_t TableSize = InitialTableOffset + IndirectFunctions.size();
uint32_t TableSize = kInitialTableOffset + IndirectFunctions.size();
SyntheticSection *Section = createSyntheticSection(WASM_SEC_TABLE);
raw_ostream &OS = Section->getStream();
@ -311,11 +311,11 @@ void Writer::createElemSection() {
writeUleb128(OS, 0, "table index");
WasmInitExpr InitExpr;
InitExpr.Opcode = WASM_OPCODE_I32_CONST;
InitExpr.Value.Int32 = InitialTableOffset;
InitExpr.Value.Int32 = kInitialTableOffset;
writeInitExpr(OS, InitExpr);
writeUleb128(OS, IndirectFunctions.size(), "elem count");
uint32_t TableIndex = InitialTableOffset;
uint32_t TableIndex = kInitialTableOffset;
for (const Symbol *Sym : IndirectFunctions) {
assert(Sym->getTableIndex() == TableIndex);
writeUleb128(OS, Sym->getOutputIndex(), "function index");
@ -714,7 +714,7 @@ void Writer::assignIndexes() {
if (Config->Relocatable)
DefinedGlobals.reserve(Symtab->getSymbols().size());
uint32_t TableIndex = InitialTableOffset;
uint32_t TableIndex = kInitialTableOffset;
for (ObjFile *File : Symtab->ObjectFiles) {
if (Config->Relocatable) {
@ -855,9 +855,6 @@ void Writer::calculateInitFunctions() {
}
void Writer::run() {
if (!Config->Relocatable)
InitialTableOffset = 1;
log("-- calculateTypes");
calculateTypes();
log("-- calculateImports");