[lld] r319813 - [WebAssembly] Improve support linker synthetic symbols

Sam Clegg via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 5 11:05:45 PST 2017


Author: sbc
Date: Tue Dec  5 11:05:45 2017
New Revision: 319813

URL: http://llvm.org/viewvc/llvm-project?rev=319813&view=rev
Log:
[WebAssembly] Improve support linker synthetic symbols

This is part of larger change to add synthetic symbols
for section start/end points and init/fini_array:
  https://reviews.llvm.org/D40760

Allows synthetic global symbols to have an explicitly set
virtual address.

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

Modified:
    lld/trunk/wasm/Config.h
    lld/trunk/wasm/Driver.cpp
    lld/trunk/wasm/Symbols.cpp
    lld/trunk/wasm/Symbols.h
    lld/trunk/wasm/Writer.cpp

Modified: lld/trunk/wasm/Config.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/Config.h?rev=319813&r1=319812&r2=319813&view=diff
==============================================================================
--- lld/trunk/wasm/Config.h (original)
+++ lld/trunk/wasm/Config.h Tue Dec  5 11:05:45 2017
@@ -39,7 +39,8 @@ struct Configuration {
 
   llvm::StringSet<> AllowUndefinedSymbols;
   std::vector<llvm::StringRef> SearchPaths;
-  std::vector<std::pair<Symbol *, WasmGlobal>> SyntheticGlobals;
+  std::vector<Symbol *> SyntheticGlobals;
+  Symbol *StackPointerSymbol = nullptr;
 };
 
 // The only instance of Configuration struct.

Modified: lld/trunk/wasm/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/Driver.cpp?rev=319813&r1=319812&r2=319813&view=diff
==============================================================================
--- lld/trunk/wasm/Driver.cpp (original)
+++ lld/trunk/wasm/Driver.cpp Tue Dec  5 11:05:45 2017
@@ -138,17 +138,12 @@ static Optional<std::string> findFile(St
 // Wasm global are used in relocatable object files to model symbol imports
 // and exports.  In the final executable the only use of wasm globals is
 // for the exlicit stack pointer (__stack_pointer).
-static void addSyntheticGlobal(StringRef Name, int32_t Value) {
+static Symbol* addSyntheticGlobal(StringRef Name, int32_t Value) {
   log("injecting global: " + Name);
   Symbol *S = Symtab->addDefinedGlobal(Name);
-  S->setOutputIndex(Config->SyntheticGlobals.size());
-
-  WasmGlobal Global;
-  Global.Mutable = true;
-  Global.Type = WASM_TYPE_I32;
-  Global.InitExpr.Opcode = WASM_OPCODE_I32_CONST;
-  Global.InitExpr.Value.Int32 = Value;
-  Config->SyntheticGlobals.emplace_back(S, Global);
+  S->setVirtualAddress(Value);
+  Config->SyntheticGlobals.emplace_back(S);
+  return S;
 }
 
 // Inject a new undefined symbol into the link.  This will cause the link to
@@ -285,7 +280,7 @@ void LinkerDriver::link(ArrayRef<const c
     static WasmSignature Signature = {{}, WASM_TYPE_NORESULT};
     addSyntheticUndefinedFunction(Config->Entry, &Signature);
 
-    addSyntheticGlobal("__stack_pointer", 0);
+    Config->StackPointerSymbol = addSyntheticGlobal("__stack_pointer", 0);
   }
 
   createFiles(Args);

Modified: lld/trunk/wasm/Symbols.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/Symbols.cpp?rev=319813&r1=319812&r2=319813&view=diff
==============================================================================
--- lld/trunk/wasm/Symbols.cpp (original)
+++ lld/trunk/wasm/Symbols.cpp Tue Dec  5 11:05:45 2017
@@ -41,6 +41,8 @@ uint32_t Symbol::getVirtualAddress() con
   DEBUG(dbgs() << "getVirtualAddress: " << getName() << "\n");
   if (isUndefined())
     return UINT32_MAX;
+  if (VirtualAddress.hasValue())
+    return VirtualAddress.getValue();
 
   assert(Sym != nullptr);
   ObjFile *Obj = cast<ObjFile>(File);
@@ -57,6 +59,11 @@ uint32_t Symbol::getOutputIndex() const
   return OutputIndex.getValue();
 }
 
+void Symbol::setVirtualAddress(uint32_t Value) {
+  DEBUG(dbgs() << "setVirtualAddress " << Name << " -> " << Value << "\n");
+  VirtualAddress = Value;
+}
+
 void Symbol::setOutputIndex(uint32_t Index) {
   DEBUG(dbgs() << "setOutputIndex " << Name << " -> " << Index << "\n");
   assert(!hasOutputIndex());

Modified: lld/trunk/wasm/Symbols.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/Symbols.h?rev=319813&r1=319812&r2=319813&view=diff
==============================================================================
--- lld/trunk/wasm/Symbols.h (original)
+++ lld/trunk/wasm/Symbols.h Tue Dec  5 11:05:45 2017
@@ -77,12 +77,14 @@ public:
   uint32_t getVirtualAddress() const;
 
   // Returns true if an output index has been set for this symbol
-  bool hasOutputIndex() { return OutputIndex.hasValue(); }
+  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);
 
+  void setVirtualAddress(uint32_t VA);
+
   void update(Kind K, InputFile *F = nullptr, const WasmSymbol *Sym = nullptr,
               const InputSegment *Segment = nullptr,
               const WasmSignature *Sig = nullptr);
@@ -105,6 +107,7 @@ protected:
   const WasmSymbol *Sym = nullptr;
   const InputSegment *Segment = nullptr;
   llvm::Optional<uint32_t> OutputIndex;
+  llvm::Optional<uint32_t> VirtualAddress;
   const WasmSignature *FunctionType;
 };
 

Modified: lld/trunk/wasm/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/Writer.cpp?rev=319813&r1=319812&r2=319813&view=diff
==============================================================================
--- lld/trunk/wasm/Writer.cpp (original)
+++ lld/trunk/wasm/Writer.cpp Tue Dec  5 11:05:45 2017
@@ -149,7 +149,7 @@ void Writer::createImportSection() {
 
   writeUleb128(OS, NumImports, "import count");
 
-  for (Symbol *Sym : FunctionImports) {
+  for (const Symbol *Sym : FunctionImports) {
     WasmImport Import;
     Import.Module = "env";
     Import.Field = Sym->getName();
@@ -169,7 +169,7 @@ void Writer::createImportSection() {
     writeImport(OS, Import);
   }
 
-  for (Symbol *Sym : GlobalImports) {
+  for (const Symbol *Sym : GlobalImports) {
     WasmImport Import;
     Import.Module = "env";
     Import.Field = Sym->getName();
@@ -221,8 +221,12 @@ void Writer::createGlobalSection() {
   raw_ostream &OS = Section->getStream();
 
   writeUleb128(OS, NumGlobals, "global count");
-  for (auto &Pair : Config->SyntheticGlobals) {
-    WasmGlobal &Global = Pair.second;
+  for (const Symbol *Sym : Config->SyntheticGlobals) {
+    WasmGlobal Global;
+    Global.Type = WASM_TYPE_I32;
+    Global.Mutable = Sym == Config->StackPointerSymbol;
+    Global.InitExpr.Opcode = WASM_OPCODE_I32_CONST;
+    Global.InitExpr.Value.Int32 = Sym->getVirtualAddress();
     writeGlobal(OS, Global);
   }
 
@@ -507,7 +511,7 @@ void Writer::layoutMemory() {
     debugPrint("mem: stack size  = %d\n", Config->ZStackSize);
     debugPrint("mem: stack base  = %d\n", MemoryPtr);
     MemoryPtr += Config->ZStackSize;
-    Config->SyntheticGlobals[0].second.InitExpr.Value.Int32 = MemoryPtr;
+    Config->StackPointerSymbol->setVirtualAddress(MemoryPtr);
     debugPrint("mem: stack top   = %d\n", MemoryPtr);
   }
 




More information about the llvm-commits mailing list