blob: 054a50039fe8542219daff078eab56ead5ee5754 [file] [log] [blame]
Sam Cleggf61910d2018-01-12 22:18:22 +00001//===- InputChunks.cpp ----------------------------------------------------===//
Sam Cleggc94d3932017-11-17 18:14:09 +00002//
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) {
Sam Clegg50686852018-01-12 18:35:13 +000033 if (Section.Relocations.empty())
34 return;
Sam Clegg5fa274b2018-01-10 01:13:34 +000035 size_t Start = getInputSectionOffset();
36 size_t Size = getSize();
37 for (const WasmRelocation &R : Section.Relocations)
38 if (R.Offset >= Start && R.Offset < Start + Size)
39 Relocations.push_back(R);
40}
Sam Cleggd96d9352018-01-10 19:22:42 +000041
42static void applyRelocation(uint8_t *Buf, const OutputRelocation &Reloc) {
43 DEBUG(dbgs() << "write reloc: type=" << Reloc.Reloc.Type
44 << " index=" << Reloc.Reloc.Index << " value=" << Reloc.Value
45 << " offset=" << Reloc.Reloc.Offset << "\n");
46 Buf += Reloc.Reloc.Offset;
47 int64_t ExistingValue;
48 switch (Reloc.Reloc.Type) {
49 case R_WEBASSEMBLY_TYPE_INDEX_LEB:
50 case R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
Sam Cleggab604a92018-01-23 01:25:56 +000051 case R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
Sam Cleggd96d9352018-01-10 19:22:42 +000052 ExistingValue = decodeULEB128(Buf);
Sam Cleggab604a92018-01-23 01:25:56 +000053 // Additional check to verify that the existing value that the location
54 // matches our expectations.
Sam Cleggd96d9352018-01-10 19:22:42 +000055 if (ExistingValue != Reloc.Reloc.Index) {
56 DEBUG(dbgs() << "existing value: " << decodeULEB128(Buf) << "\n");
57 assert(decodeULEB128(Buf) == Reloc.Reloc.Index);
58 }
59 LLVM_FALLTHROUGH;
60 case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
Sam Cleggd96d9352018-01-10 19:22:42 +000061 encodeULEB128(Reloc.Value, Buf, 5);
62 break;
63 case R_WEBASSEMBLY_TABLE_INDEX_SLEB:
Sam Cleggd96d9352018-01-10 19:22:42 +000064 case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
65 encodeSLEB128(static_cast<int32_t>(Reloc.Value), Buf, 5);
66 break;
67 case R_WEBASSEMBLY_TABLE_INDEX_I32:
68 case R_WEBASSEMBLY_MEMORY_ADDR_I32:
69 support::endian::write32<support::little>(Buf, Reloc.Value);
70 break;
71 default:
72 llvm_unreachable("unknown relocation type");
73 }
74}
75
76static void applyRelocations(uint8_t *Buf, ArrayRef<OutputRelocation> Relocs) {
77 if (!Relocs.size())
78 return;
Sam Clegg7ed293e2018-01-12 00:34:04 +000079 DEBUG(dbgs() << "applyRelocations: count=" << Relocs.size() << "\n");
Sam Cleggd96d9352018-01-10 19:22:42 +000080 for (const OutputRelocation &Reloc : Relocs)
81 applyRelocation(Buf, Reloc);
82}
83
84void InputChunk::writeTo(uint8_t *SectionStart) const {
Sam Clegg4a379c32018-01-13 00:22:00 +000085 memcpy(SectionStart + getOutputOffset(), data().data(), data().size());
Sam Cleggd96d9352018-01-10 19:22:42 +000086 applyRelocations(SectionStart, OutRelocations);
87}
88
89// Populate OutRelocations based on the input relocations and offset within the
90// output section. Calculates the updated index and offset for each relocation
91// as well as the value to write out in the final binary.
92void InputChunk::calcRelocations() {
Sam Clegg50686852018-01-12 18:35:13 +000093 if (Relocations.empty())
94 return;
Sam Cleggd96d9352018-01-10 19:22:42 +000095 int32_t Off = getOutputOffset() - getInputSectionOffset();
Sam Clegg50686852018-01-12 18:35:13 +000096 DEBUG(dbgs() << "calcRelocations: " << File->getName()
Sam Clegg7ed293e2018-01-12 00:34:04 +000097 << " 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
Sam Cleggff2b1222018-01-22 21:55:43 +0000107 if (Config->Relocatable)
Sam Clegg50686852018-01-12 18:35:13 +0000108 NewReloc.NewIndex = File->calcNewIndex(Reloc);
Sam Cleggd96d9352018-01-10 19:22:42 +0000109
Sam Cleggab604a92018-01-23 01:25:56 +0000110 NewReloc.Value = File->calcNewValue(Reloc);
Sam Cleggd96d9352018-01-10 19:22:42 +0000111 OutRelocations.emplace_back(NewReloc);
112 }
113}
Sam Clegg50686852018-01-12 18:35:13 +0000114
115void InputFunction::setOutputIndex(uint32_t Index) {
116 DEBUG(dbgs() << "InputFunction::setOutputIndex: " << Index << "\n");
117 assert(!hasOutputIndex());
118 OutputIndex = Index;
Eric Christopher9ea500b2018-01-13 00:44:45 +0000119}
Sam Clegg67abf532018-01-24 21:45:25 +0000120
121void InputFunction::setTableIndex(uint32_t Index) {
122 DEBUG(dbgs() << "InputFunction::setTableIndex " << Index << "\n");
123 assert(!hasTableIndex());
124 TableIndex = Index;
125}