From 3f1fb88afb75613b9e600c6fbce1a628154044b6 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Mon, 11 Dec 2017 21:52:21 +0000 Subject: [PATCH] [WebAssembly] Preserve ordering of global symbols This change restores the behavior that global indexes are assigned in object file order. This was accidentally changed in https://reviews.llvm.org/D40859. Differential Revision: https://reviews.llvm.org/D41038 llvm-svn: 320426 --- lld/test/wasm/data-layout.ll | 12 ++++++------ lld/wasm/Writer.cpp | 12 +++++------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/lld/test/wasm/data-layout.ll b/lld/test/wasm/data-layout.ll index dad8ea3466c3..1da4ff0710cb 100644 --- a/lld/test/wasm/data-layout.ll +++ b/lld/test/wasm/data-layout.ll @@ -20,11 +20,6 @@ ; CHECK-NEXT: Mutable: false ; CHECK-NEXT: InitExpr: ; CHECK-NEXT: Opcode: I32_CONST -; CHECK-NEXT: Value: 1052 -; CHECK-NEXT: - Type: I32 -; CHECK-NEXT: Mutable: false -; CHECK-NEXT: InitExpr: -; CHECK-NEXT: Opcode: I32_CONST ; CHECK-NEXT: Value: 1024 ; CHECK-NEXT: - Type: I32 ; CHECK-NEXT: Mutable: false @@ -36,11 +31,16 @@ ; CHECK-NEXT: InitExpr: ; CHECK-NEXT: Opcode: I32_CONST ; CHECK-NEXT: Value: 1048 +; CHECK-NEXT: - Type: I32 +; CHECK-NEXT: Mutable: false +; CHECK-NEXT: InitExpr: +; CHECK-NEXT: Opcode: I32_CONST +; CHECK-NEXT: Value: 1052 ; CHECK: - Type: DATA ; CHECK-NEXT: Relocations: ; CHECK-NEXT: - Type: R_WEBASSEMBLY_MEMORY_ADDR_I32 -; CHECK-NEXT: Index: 1 +; CHECK-NEXT: Index: 4 ; CHECK-NEXT: Offset: 0x0000001F ; CHECK-NEXT: Segments: ; CHECK-NEXT: - SectionOffset: 7 diff --git a/lld/wasm/Writer.cpp b/lld/wasm/Writer.cpp index bc7d969bb75e..7af5df3736cc 100644 --- a/lld/wasm/Writer.cpp +++ b/lld/wasm/Writer.cpp @@ -614,15 +614,13 @@ void Writer::assignSymbolIndexes() { for (ObjFile *File : Symtab->ObjectFiles) { DEBUG(dbgs() << "assignSymbolIndexes: " << File->getName() << "\n"); for (Symbol *Sym : File->getSymbols()) { - if (Sym->hasOutputIndex() || !Sym->isDefined()) + // Assign indexes for symbols defined with this file. + if (!Sym->isDefined() || File != Sym->getFile()) continue; - if (Sym->isFunction()) { - if (Sym->getFile() && isa(Sym->getFile())) { - auto *Obj = cast(Sym->getFile()); - Sym->setOutputIndex(Obj->FunctionIndexOffset + - Sym->getFunctionIndex()); - } + auto *Obj = cast(Sym->getFile()); + Sym->setOutputIndex(Obj->FunctionIndexOffset + + Sym->getFunctionIndex()); } else if (Config->EmitRelocs) { DefinedGlobals.emplace_back(Sym); Sym->setOutputIndex(GlobalIndex++);