blob: 286afcdccc2f08956eb699358a509ea83b94a92b [file] [log] [blame]
Sam Cleggc94d3932017-11-17 18:14:09 +00001//===- InputSegment.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
Sam Clegg5fa274b2018-01-10 01:13:34 +000010#include "InputChunks.h"
Sam Cleggd96d9352018-01-10 19:22:42 +000011#include "Config.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000012#include "OutputSegment.h"
Sam Cleggd96d9352018-01-10 19:22:42 +000013#include "lld/Common/ErrorHandler.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000014#include "lld/Common/LLVM.h"
Sam Cleggd96d9352018-01-10 19:22:42 +000015#include "llvm/Support/LEB128.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000016
17#define DEBUG_TYPE "lld"
18
19using namespace llvm;
Sam Cleggd96d9352018-01-10 19:22:42 +000020using namespace llvm::wasm;
21using namespace lld;
Sam Cleggc94d3932017-11-17 18:14:09 +000022using namespace lld::wasm;
23
24uint32_t InputSegment::translateVA(uint32_t Address) const {
25 assert(Address >= startVA() && Address < endVA());
Sam Cleggd96d9352018-01-10 19:22:42 +000026 int32_t Delta = OutputSeg->StartVA + OutputSegmentOffset - startVA();
Sam Cleggc94d3932017-11-17 18:14:09 +000027 DEBUG(dbgs() << "translateVA: " << getName() << " Delta=" << Delta
28 << " Address=" << Address << "\n");
29 return Address + Delta;
30}
Sam Clegg5fa274b2018-01-10 01:13:34 +000031
32void InputChunk::copyRelocations(const WasmSection &Section) {
33 size_t Start = getInputSectionOffset();
34 size_t Size = getSize();
35 for (const WasmRelocation &R : Section.Relocations)
36 if (R.Offset >= Start && R.Offset < Start + Size)
37 Relocations.push_back(R);
38}
Sam Cleggd96d9352018-01-10 19:22:42 +000039
40static void applyRelocation(uint8_t *Buf, const OutputRelocation &Reloc) {
41 DEBUG(dbgs() << "write reloc: type=" << Reloc.Reloc.Type
42 << " index=" << Reloc.Reloc.Index << " value=" << Reloc.Value
43 << " offset=" << Reloc.Reloc.Offset << "\n");
44 Buf += Reloc.Reloc.Offset;
45 int64_t ExistingValue;
46 switch (Reloc.Reloc.Type) {
47 case R_WEBASSEMBLY_TYPE_INDEX_LEB:
48 case R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
49 ExistingValue = decodeULEB128(Buf);
50 if (ExistingValue != Reloc.Reloc.Index) {
51 DEBUG(dbgs() << "existing value: " << decodeULEB128(Buf) << "\n");
52 assert(decodeULEB128(Buf) == Reloc.Reloc.Index);
53 }
54 LLVM_FALLTHROUGH;
55 case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
56 case R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
57 encodeULEB128(Reloc.Value, Buf, 5);
58 break;
59 case R_WEBASSEMBLY_TABLE_INDEX_SLEB:
60 ExistingValue = decodeSLEB128(Buf);
61 if (ExistingValue != Reloc.Reloc.Index) {
62 DEBUG(dbgs() << "existing value: " << decodeSLEB128(Buf) << "\n");
63 assert(decodeSLEB128(Buf) == Reloc.Reloc.Index);
64 }
65 LLVM_FALLTHROUGH;
66 case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
67 encodeSLEB128(static_cast<int32_t>(Reloc.Value), Buf, 5);
68 break;
69 case R_WEBASSEMBLY_TABLE_INDEX_I32:
70 case R_WEBASSEMBLY_MEMORY_ADDR_I32:
71 support::endian::write32<support::little>(Buf, Reloc.Value);
72 break;
73 default:
74 llvm_unreachable("unknown relocation type");
75 }
76}
77
78static void applyRelocations(uint8_t *Buf, ArrayRef<OutputRelocation> Relocs) {
79 if (!Relocs.size())
80 return;
Sam Clegg7ed293e2018-01-12 00:34:04 +000081 DEBUG(dbgs() << "applyRelocations: count=" << Relocs.size() << "\n");
Sam Cleggd96d9352018-01-10 19:22:42 +000082 for (const OutputRelocation &Reloc : Relocs)
83 applyRelocation(Buf, Reloc);
84}
85
86void InputChunk::writeTo(uint8_t *SectionStart) const {
87 memcpy(SectionStart + getOutputOffset(), getData(), getSize());
88 applyRelocations(SectionStart, OutRelocations);
89}
90
91// Populate OutRelocations based on the input relocations and offset within the
92// output section. Calculates the updated index and offset for each relocation
93// as well as the value to write out in the final binary.
94void InputChunk::calcRelocations() {
95 int32_t Off = getOutputOffset() - getInputSectionOffset();
Sam Clegg7ed293e2018-01-12 00:34:04 +000096 DEBUG(dbgs() << "calcRelocations: " << File.getName()
97 << " offset=" << Twine(Off) << "\n");
Sam Cleggd96d9352018-01-10 19:22:42 +000098 for (const WasmRelocation &Reloc : Relocations) {
99 OutputRelocation NewReloc;
100 NewReloc.Reloc = Reloc;
101 assert(Reloc.Offset + Off > 0);
102 NewReloc.Reloc.Offset += Off;
103 DEBUG(dbgs() << "reloc: type=" << Reloc.Type << " index=" << Reloc.Index
104 << " offset=" << Reloc.Offset
105 << " newOffset=" << NewReloc.Reloc.Offset << "\n");
106
107 if (Config->EmitRelocs)
108 NewReloc.NewIndex = File.calcNewIndex(Reloc);
109
110 switch (Reloc.Type) {
111 case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
112 case R_WEBASSEMBLY_MEMORY_ADDR_I32:
113 case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
114 NewReloc.Value = File.getRelocatedAddress(Reloc.Index) + Reloc.Addend;
115 break;
116 default:
117 NewReloc.Value = File.calcNewIndex(Reloc);
118 break;
119 }
120
121 OutRelocations.emplace_back(NewReloc);
122 }
123}