Coverage Report

Created: 2018-01-17 21:32

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/wasm/InputChunks.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- InputChunks.cpp ----------------------------------------------------===//
2
//
3
//                             The LLVM Linker
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
10
#include "InputChunks.h"
11
#include "Config.h"
12
#include "OutputSegment.h"
13
#include "lld/Common/ErrorHandler.h"
14
#include "lld/Common/LLVM.h"
15
#include "llvm/Support/LEB128.h"
16
17
#define DEBUG_TYPE "lld"
18
19
using namespace llvm;
20
using namespace llvm::wasm;
21
using namespace lld;
22
using namespace lld::wasm;
23
24
0
uint32_t InputSegment::translateVA(uint32_t Address) const {
25
0
  assert(Address >= startVA() && Address < endVA());
26
0
  int32_t Delta = OutputSeg->StartVA + OutputSegmentOffset - startVA();
27
0
  DEBUG(dbgs() << "translateVA: " << getName() << " Delta=" << Delta
28
0
               << " Address=" << Address << "\n");
29
0
  return Address + Delta;
30
0
}
31
32
0
void InputChunk::copyRelocations(const WasmSection &Section) {
33
0
  if (Section.Relocations.empty())
34
0
    return;
35
0
  size_t Start = getInputSectionOffset();
36
0
  size_t Size = getSize();
37
0
  for (const WasmRelocation &R : Section.Relocations)
38
0
    if (R.Offset >= Start && R.Offset < Start + Size)
39
0
      Relocations.push_back(R);
40
0
}
41
42
0
static void applyRelocation(uint8_t *Buf, const OutputRelocation &Reloc) {
43
0
  DEBUG(dbgs() << "write reloc: type=" << Reloc.Reloc.Type
44
0
               << " index=" << Reloc.Reloc.Index << " value=" << Reloc.Value
45
0
               << " offset=" << Reloc.Reloc.Offset << "\n");
46
0
  Buf += Reloc.Reloc.Offset;
47
0
  int64_t ExistingValue;
48
0
  switch (Reloc.Reloc.Type) {
49
0
  case R_WEBASSEMBLY_TYPE_INDEX_LEB:
50
0
  case R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
51
0
    ExistingValue = decodeULEB128(Buf);
52
0
    if (ExistingValue != Reloc.Reloc.Index) {
53
0
      DEBUG(dbgs() << "existing value: " << decodeULEB128(Buf) << "\n");
54
0
      assert(decodeULEB128(Buf) == Reloc.Reloc.Index);
55
0
    }
56
0
    LLVM_FALLTHROUGH;
57
0
  case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
58
0
  case R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
59
0
    encodeULEB128(Reloc.Value, Buf, 5);
60
0
    break;
61
0
  case R_WEBASSEMBLY_TABLE_INDEX_SLEB:
62
0
    ExistingValue = decodeSLEB128(Buf);
63
0
    if (ExistingValue != Reloc.Reloc.Index) {
64
0
      DEBUG(dbgs() << "existing value: " << decodeSLEB128(Buf) << "\n");
65
0
      assert(decodeSLEB128(Buf) == Reloc.Reloc.Index);
66
0
    }
67
0
    LLVM_FALLTHROUGH;
68
0
  case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
69
0
    encodeSLEB128(static_cast<int32_t>(Reloc.Value), Buf, 5);
70
0
    break;
71
0
  case R_WEBASSEMBLY_TABLE_INDEX_I32:
72
0
  case R_WEBASSEMBLY_MEMORY_ADDR_I32:
73
0
    support::endian::write32<support::little>(Buf, Reloc.Value);
74
0
    break;
75
0
  default:
76
0
    llvm_unreachable("unknown relocation type");
77
0
  }
78
0
}
79
80
0
static void applyRelocations(uint8_t *Buf, ArrayRef<OutputRelocation> Relocs) {
81
0
  if (!Relocs.size())
82
0
    return;
83
0
  DEBUG(dbgs() << "applyRelocations: count=" << Relocs.size() << "\n");
84
0
  for (const OutputRelocation &Reloc : Relocs)
85
0
    applyRelocation(Buf, Reloc);
86
0
}
87
88
0
void InputChunk::writeTo(uint8_t *SectionStart) const {
89
0
  memcpy(SectionStart + getOutputOffset(), data().data(), data().size());
90
0
  applyRelocations(SectionStart, OutRelocations);
91
0
}
92
93
// Populate OutRelocations based on the input relocations and offset within the
94
// output section.  Calculates the updated index and offset for each relocation
95
// as well as the value to write out in the final binary.
96
0
void InputChunk::calcRelocations() {
97
0
  if (Relocations.empty())
98
0
    return;
99
0
  int32_t Off = getOutputOffset() - getInputSectionOffset();
100
0
  DEBUG(dbgs() << "calcRelocations: " << File->getName()
101
0
               << " offset=" << Twine(Off) << "\n");
102
0
  for (const WasmRelocation &Reloc : Relocations) {
103
0
    OutputRelocation NewReloc;
104
0
    NewReloc.Reloc = Reloc;
105
0
    assert(Reloc.Offset + Off > 0);
106
0
    NewReloc.Reloc.Offset += Off;
107
0
    DEBUG(dbgs() << "reloc: type=" << Reloc.Type << " index=" << Reloc.Index
108
0
                 << " offset=" << Reloc.Offset
109
0
                 << " newOffset=" << NewReloc.Reloc.Offset << "\n");
110
0
111
0
    if (Config->EmitRelocs)
112
0
      NewReloc.NewIndex = File->calcNewIndex(Reloc);
113
0
114
0
    switch (Reloc.Type) {
115
0
    case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
116
0
    case R_WEBASSEMBLY_MEMORY_ADDR_I32:
117
0
    case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
118
0
      NewReloc.Value = File->getRelocatedAddress(Reloc.Index) + Reloc.Addend;
119
0
      break;
120
0
    default:
121
0
      NewReloc.Value = File->calcNewIndex(Reloc);
122
0
      break;
123
0
    }
124
0
125
0
    OutRelocations.emplace_back(NewReloc);
126
0
  }
127
0
}
128
129
0
void InputFunction::setOutputIndex(uint32_t Index) {
130
0
  DEBUG(dbgs() << "InputFunction::setOutputIndex: " << Index << "\n");
131
0
  assert(!hasOutputIndex());
132
0
  OutputIndex = Index;
133
0
}