//===- SymbolTable.cpp ----------------------------------------------------===//
//
//                             The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "SymbolTable.h"

#include "Config.h"
#include "InputChunks.h"
#include "WriterUtils.h"
#include "lld/Common/ErrorHandler.h"
#include "lld/Common/Memory.h"
#include "llvm/ADT/SetVector.h"

#define DEBUG_TYPE "lld"

using namespace llvm;
using namespace llvm::wasm;
using namespace lld;
using namespace lld::wasm;

SymbolTable *lld::wasm::Symtab;

void SymbolTable::addFile(InputFile *File) {
  log("Processing: " + toString(File));
  File->parse();

  if (auto *F = dyn_cast<ObjFile>(File))
    ObjectFiles.push_back(F);
}

void SymbolTable::reportRemainingUndefines() {
  SetVector<Symbol *> Undefs;
  for (Symbol *Sym : SymVector) {
    if (Sym->isUndefined() && !Sym->isWeak() &&
        Config->AllowUndefinedSymbols.count(Sym->getName()) == 0) {
      Undefs.insert(Sym);
    }
  }

  if (Undefs.empty())
    return;

  for (ObjFile *File : ObjectFiles)
    for (Symbol *Sym : File->getSymbols())
      if (Undefs.count(Sym))
        error(toString(File) + ": undefined symbol: " + toString(*Sym));

  for (Symbol *Sym : Undefs)
    if (!Sym->getFile())
      error("undefined symbol: " + toString(*Sym));
}

Symbol *SymbolTable::find(StringRef Name) {
  auto It = SymMap.find(CachedHashStringRef(Name));
  if (It == SymMap.end())
    return nullptr;
  return It->second;
}

std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name) {
  Symbol *&Sym = SymMap[CachedHashStringRef(Name)];
  if (Sym)
    return {Sym, false};
  Sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
  SymVector.emplace_back(Sym);
  return {Sym, true};
}

// Check the type of new symbol matches that of the symbol is replacing.
// For functions this can also involve verifying that the signatures match.
static void checkSymbolTypes(const Symbol &Existing, const InputFile &F,
                             bool NewIsFunction, const WasmSignature *NewSig) {
  if (Existing.isLazy())
    return;

  // First check the symbol types match (i.e. either both are function
  // symbols or both are data symbols).
  if (isa<FunctionSymbol>(Existing) != NewIsFunction) {
    error("symbol type mismatch: " + Existing.getName() + "\n>>> defined as " +
          (isa<FunctionSymbol>(Existing) ? "Function" : "Global") + " in " +
          toString(Existing.getFile()) + "\n>>> defined as " +
          (NewIsFunction ? "Function" : "Global") + " in " + F.getName());
    return;
  }

  // For function symbols, optionally check the function signature matches too.
  auto *ExistingFunc = dyn_cast<FunctionSymbol>(&Existing);
  if (!ExistingFunc || !Config->CheckSignatures)
    return;

  const WasmSignature *OldSig = ExistingFunc->getFunctionType();

  // Skip the signature check if the existing function has no signature (e.g.
  // if it is an undefined symbol generated by --undefined command line flag).
  if (OldSig == nullptr)
    return;

  DEBUG(dbgs() << "checkSymbolTypes: " << ExistingFunc->getName() << "\n");
  assert(NewSig);

  if (*NewSig == *OldSig)
    return;

  error("function signature mismatch: " + ExistingFunc->getName() +
        "\n>>> defined as " + toString(*OldSig) + " in " +
        toString(ExistingFunc->getFile()) + "\n>>> defined as " +
        toString(*NewSig) + " in " + F.getName());
}

static void checkSymbolTypes(const Symbol &Existing, const InputFile &F,
                             bool IsFunction, const InputChunk *Chunk) {
  const WasmSignature *Sig = nullptr;
  if (auto *F = dyn_cast_or_null<InputFunction>(Chunk))
    Sig = &F->Signature;
  return checkSymbolTypes(Existing, F, IsFunction, Sig);
}

DefinedFunction *SymbolTable::addSyntheticFunction(StringRef Name,
                                                   const WasmSignature *Type,
                                                   uint32_t Flags) {
  DEBUG(dbgs() << "addSyntheticFunction: " << Name << "\n");
  Symbol *S;
  bool WasInserted;
  std::tie(S, WasInserted) = insert(Name);
  assert(WasInserted);
  return replaceSymbol<DefinedFunction>(S, Name, Flags, Type);
}

DefinedGlobal *SymbolTable::addSyntheticGlobal(StringRef Name, uint32_t Flags) {
  DEBUG(dbgs() << "addSyntheticGlobal: " << Name << "\n");
  Symbol *S;
  bool WasInserted;
  std::tie(S, WasInserted) = insert(Name);
  assert(WasInserted);
  return replaceSymbol<DefinedGlobal>(S, Name, Flags);
}

static bool shouldReplace(const Symbol &Existing, InputFile *NewFile,
                          uint32_t NewFlags, InputChunk *NewChunk,
                          bool NewIsFunction) {
  // If existing symbol is lazy, replace it without checking types since
  // lazy symbols don't have any type information.
  if (Existing.isLazy()) {
    DEBUG(dbgs() << "replacing existing lazy symbol: " << Existing.getName()
                 << "\n");
    return true;
  }

  // Now we have two wasm symbols, and all wasm symbols that have the same
  // symbol name must have the same type, even if they are undefined. This
  // is different from ELF because symbol types are not that significant
  // in ELF, and undefined symbols in ELF don't have type in the first place.
  checkSymbolTypes(Existing, *NewFile, NewIsFunction, NewChunk);

  // If existing symbol is undefined, replace it.
  if (!Existing.isDefined()) {
    DEBUG(dbgs() << "resolving existing undefined symbol: "
                 << Existing.getName() << "\n");
    return true;
  }

  // Now we have two defined symbols. If the new one is weak, we can ignore it.
  if ((NewFlags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) {
    DEBUG(dbgs() << "existing symbol takes precedence\n");
    return false;
  }

  // If the existing symbol is weak, we should replace it.
  if (Existing.isWeak()) {
    DEBUG(dbgs() << "replacing existing weak symbol\n");
    return true;
  }

  // Neither symbol is week. They conflict.
  error("duplicate symbol: " + toString(Existing) + "\n>>> defined in " +
        toString(Existing.getFile()) + "\n>>> defined in " + toString(NewFile));
  return true;
}

Symbol *SymbolTable::addDefinedFunction(StringRef Name, uint32_t Flags,
                                        InputFile *F, InputFunction *Function) {
  DEBUG(dbgs() << "addDefinedFunction: " << Name << "\n");
  Symbol *S;
  bool WasInserted;
  std::tie(S, WasInserted) = insert(Name);
  if (WasInserted || shouldReplace(*S, F, Flags, Function, true))
    replaceSymbol<DefinedFunction>(S, Name, Flags, F, Function);
  return S;
}

Symbol *SymbolTable::addDefinedGlobal(StringRef Name, uint32_t Flags,
                                      InputFile *F, InputSegment *Segment,
                                      uint32_t Address) {
  DEBUG(dbgs() << "addDefinedGlobal:" << Name << " addr:" << Address << "\n");
  Symbol *S;
  bool WasInserted;
  std::tie(S, WasInserted) = insert(Name);
  if (WasInserted || shouldReplace(*S, F, Flags, Segment, false))
    replaceSymbol<DefinedGlobal>(S, Name, Flags, F, Segment, Address);
  return S;
}

Symbol *SymbolTable::addUndefined(StringRef Name, Symbol::Kind Kind,
                                  uint32_t Flags, InputFile *F,
                                  const WasmSignature *Type) {
  DEBUG(dbgs() << "addUndefined: " << Name << "\n");

  Symbol *S;
  bool WasInserted;
  std::tie(S, WasInserted) = insert(Name);

  bool IsFunction = Kind == Symbol::UndefinedFunctionKind;
  if (WasInserted) {
    if (IsFunction)
      replaceSymbol<UndefinedFunction>(S, Name, Flags, F, Type);
    else
      replaceSymbol<UndefinedGlobal>(S, Name, Flags, F);
    return S;
  }

  if (auto *Lazy = dyn_cast<LazySymbol>(S)) {
    DEBUG(dbgs() << "resolved by existing lazy\n");
    cast<ArchiveFile>(Lazy->getFile())->addMember(&Lazy->getArchiveSymbol());
    return S;
  }

  if (S->isDefined()) {
    DEBUG(dbgs() << "resolved by existing\n");
    checkSymbolTypes(*S, *F, IsFunction, Type);
  }
  return S;
}

void SymbolTable::addLazy(ArchiveFile *F, const Archive::Symbol *Sym) {
  DEBUG(dbgs() << "addLazy: " << Sym->getName() << "\n");
  StringRef Name = Sym->getName();

  Symbol *S;
  bool WasInserted;
  std::tie(S, WasInserted) = insert(Name);

  if (WasInserted) {
    replaceSymbol<LazySymbol>(S, Name, F, *Sym);
    return;
  }

  // If there is an existing undefined symbol, load a new one from the archive.
  if (S->isUndefined()) {
    DEBUG(dbgs() << "replacing existing undefined\n");
    F->addMember(Sym);
  }
}

bool SymbolTable::addComdat(StringRef Name, ObjFile *F) {
  DEBUG(dbgs() << "addComdat: " << Name << "\n");
  ObjFile *&File = ComdatMap[CachedHashStringRef(Name)];
  if (File) {
    DEBUG(dbgs() << "COMDAT already defined\n");
    return false;
  }
  File = F;
  return true;
}

ObjFile *SymbolTable::findComdat(StringRef Name) const {
  auto It = ComdatMap.find(CachedHashStringRef(Name));
  return It == ComdatMap.end() ? nullptr : It->second;
}
