summaryrefslogtreecommitdiffstats
path: root/lld/wasm/Symbols.cpp
blob: 78b0de77cf02d7e3401e275a0dd9069964cbb451 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//===- Symbols.cpp --------------------------------------------------------===//
//
//                             The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "Symbols.h"

#include "Config.h"
#include "InputFiles.h"
#include "InputSegment.h"
#include "Strings.h"
#include "lld/Common/ErrorHandler.h"

#define DEBUG_TYPE "lld"

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

uint32_t Symbol::getGlobalIndex() const {
  assert(!Sym->isFunction());
  return Sym->ElementIndex;
}

uint32_t Symbol::getFunctionIndex() const {
  assert(Sym->isFunction());
  return Sym->ElementIndex;
}

const WasmSignature &Symbol::getFunctionType() const {
  assert(FunctionType != nullptr);
  return *FunctionType;
}

uint32_t Symbol::getVirtualAddress() const {
  assert(isGlobal());
  DEBUG(dbgs() << "getVirtualAddress: " << getName() << "\n");
  if (isUndefined())
    return UINT32_MAX;

  assert(Sym != nullptr);
  ObjFile *Obj = cast<ObjFile>(File);
  const WasmGlobal &Global =
      Obj->getWasmObj()->globals()[getGlobalIndex() - Obj->NumGlobalImports()];
  assert(Global.Type == llvm::wasm::WASM_TYPE_I32);
  assert(Segment);
  return Segment->translateVA(Global.InitExpr.Value.Int32);
}

uint32_t Symbol::getOutputIndex() const {
  if (isUndefined() && isWeak())
    return 0;
  return OutputIndex.getValue();
}

void Symbol::setOutputIndex(uint32_t Index) {
  DEBUG(dbgs() << "setOutputIndex " << Name << " -> " << Index << "\n");
  assert(!hasOutputIndex());
  OutputIndex = Index;
}

void Symbol::update(Kind K, InputFile *F, const WasmSymbol *WasmSym,
                    const InputSegment *Seg, const WasmSignature *Sig) {
  SymbolKind = K;
  File = F;
  Sym = WasmSym;
  Segment = Seg;
  FunctionType = Sig;
}

bool Symbol::isWeak() const { return Sym && Sym->isWeak(); }

std::string lld::toString(wasm::Symbol &Sym) {
  return wasm::displayName(Sym.getName());
}

std::string lld::toString(wasm::Symbol::Kind &Kind) {
  switch (Kind) {
  case wasm::Symbol::DefinedFunctionKind:
    return "DefinedFunction";
  case wasm::Symbol::DefinedGlobalKind:
    return "DefinedGlobal";
  case wasm::Symbol::UndefinedFunctionKind:
    return "UndefinedFunction";
  case wasm::Symbol::UndefinedGlobalKind:
    return "UndefinedGlobal";
  case wasm::Symbol::LazyKind:
    return "LazyKind";
  }
  llvm_unreachable("Invalid symbol kind!");
}
OpenPOWER on IntegriCloud