blob: 272cd03c98dbafce4cf4f3acd5b8dbbdf8d5e6f7 [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;
Rui Ueyamae351c3a2018-02-16 20:38:15 +000021using namespace llvm::support::endian;
Sam Cleggd96d9352018-01-10 19:22:42 +000022using namespace lld;
Sam Cleggc94d3932017-11-17 18:14:09 +000023using namespace lld::wasm;
24
Rui Ueyama81bee042018-02-19 22:29:48 +000025std::string lld::toString(const InputChunk *C) {
26 return (toString(C->File) + ":(" + C->getName() + ")").str();
27}
28
Sam Cleggc94d3932017-11-17 18:14:09 +000029uint32_t InputSegment::translateVA(uint32_t Address) const {
30 assert(Address >= startVA() && Address < endVA());
Sam Cleggd96d9352018-01-10 19:22:42 +000031 int32_t Delta = OutputSeg->StartVA + OutputSegmentOffset - startVA();
Sam Cleggc94d3932017-11-17 18:14:09 +000032 DEBUG(dbgs() << "translateVA: " << getName() << " Delta=" << Delta
33 << " Address=" << Address << "\n");
34 return Address + Delta;
35}
Sam Clegg5fa274b2018-01-10 01:13:34 +000036
37void InputChunk::copyRelocations(const WasmSection &Section) {
Sam Clegg50686852018-01-12 18:35:13 +000038 if (Section.Relocations.empty())
39 return;
Sam Clegg5fa274b2018-01-10 01:13:34 +000040 size_t Start = getInputSectionOffset();
41 size_t Size = getSize();
42 for (const WasmRelocation &R : Section.Relocations)
43 if (R.Offset >= Start && R.Offset < Start + Size)
44 Relocations.push_back(R);
45}
Sam Cleggd96d9352018-01-10 19:22:42 +000046
47static void applyRelocation(uint8_t *Buf, const OutputRelocation &Reloc) {
48 DEBUG(dbgs() << "write reloc: type=" << Reloc.Reloc.Type
49 << " index=" << Reloc.Reloc.Index << " value=" << Reloc.Value
50 << " offset=" << Reloc.Reloc.Offset << "\n");
Rui Ueyamac06d94a2018-02-19 22:39:52 +000051
Sam Cleggd96d9352018-01-10 19:22:42 +000052 Buf += Reloc.Reloc.Offset;
Rui Ueyamac06d94a2018-02-19 22:39:52 +000053
Sam Cleggd96d9352018-01-10 19:22:42 +000054 switch (Reloc.Reloc.Type) {
55 case R_WEBASSEMBLY_TYPE_INDEX_LEB:
56 case R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
Sam Cleggab604a92018-01-23 01:25:56 +000057 case R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
Sam Cleggab604a92018-01-23 01:25:56 +000058 // Additional check to verify that the existing value that the location
59 // matches our expectations.
Rui Ueyamac06d94a2018-02-19 22:39:52 +000060 if (decodeULEB128(Buf) != Reloc.Reloc.Index) {
Sam Cleggd96d9352018-01-10 19:22:42 +000061 DEBUG(dbgs() << "existing value: " << decodeULEB128(Buf) << "\n");
62 assert(decodeULEB128(Buf) == Reloc.Reloc.Index);
63 }
64 LLVM_FALLTHROUGH;
65 case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
Sam Cleggd96d9352018-01-10 19:22:42 +000066 encodeULEB128(Reloc.Value, Buf, 5);
67 break;
68 case R_WEBASSEMBLY_TABLE_INDEX_SLEB:
Sam Cleggd96d9352018-01-10 19:22:42 +000069 case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
70 encodeSLEB128(static_cast<int32_t>(Reloc.Value), Buf, 5);
71 break;
72 case R_WEBASSEMBLY_TABLE_INDEX_I32:
73 case R_WEBASSEMBLY_MEMORY_ADDR_I32:
Rui Ueyamae351c3a2018-02-16 20:38:15 +000074 write32le(Buf, Reloc.Value);
Sam Cleggd96d9352018-01-10 19:22:42 +000075 break;
76 default:
77 llvm_unreachable("unknown relocation type");
78 }
79}
80
Rui Ueyamac1e5c212018-02-19 22:44:18 +000081// Copy this input chunk to an mmap'ed output file.
82void InputChunk::writeTo(uint8_t *Buf) const {
83 // Copy contents
84 memcpy(Buf + getOutputOffset(), data().data(), data().size());
Sam Cleggd96d9352018-01-10 19:22:42 +000085
Rui Ueyamac1e5c212018-02-19 22:44:18 +000086 // Apply relocations
87 if (OutRelocations.empty())
88 return;
89 DEBUG(dbgs() << "applyRelocations: count=" << OutRelocations.size() << "\n");
90 for (const OutputRelocation &Reloc : OutRelocations)
91 applyRelocation(Buf, Reloc);
Sam Cleggd96d9352018-01-10 19:22:42 +000092}
93
94// Populate OutRelocations based on the input relocations and offset within the
95// output section. Calculates the updated index and offset for each relocation
96// as well as the value to write out in the final binary.
97void InputChunk::calcRelocations() {
Sam Clegg50686852018-01-12 18:35:13 +000098 if (Relocations.empty())
99 return;
Sam Cleggd96d9352018-01-10 19:22:42 +0000100 int32_t Off = getOutputOffset() - getInputSectionOffset();
Sam Clegg50686852018-01-12 18:35:13 +0000101 DEBUG(dbgs() << "calcRelocations: " << File->getName()
Sam Clegg7ed293e2018-01-12 00:34:04 +0000102 << " offset=" << Twine(Off) << "\n");
Sam Cleggd96d9352018-01-10 19:22:42 +0000103 for (const WasmRelocation &Reloc : Relocations) {
104 OutputRelocation NewReloc;
105 NewReloc.Reloc = Reloc;
106 assert(Reloc.Offset + Off > 0);
107 NewReloc.Reloc.Offset += Off;
108 DEBUG(dbgs() << "reloc: type=" << Reloc.Type << " index=" << Reloc.Index
109 << " offset=" << Reloc.Offset
110 << " newOffset=" << NewReloc.Reloc.Offset << "\n");
111
Sam Cleggff2b1222018-01-22 21:55:43 +0000112 if (Config->Relocatable)
Sam Clegg50686852018-01-12 18:35:13 +0000113 NewReloc.NewIndex = File->calcNewIndex(Reloc);
Sam Cleggd96d9352018-01-10 19:22:42 +0000114
Sam Cleggab604a92018-01-23 01:25:56 +0000115 NewReloc.Value = File->calcNewValue(Reloc);
Sam Cleggd96d9352018-01-10 19:22:42 +0000116 OutRelocations.emplace_back(NewReloc);
117 }
118}
Sam Clegg50686852018-01-12 18:35:13 +0000119
120void InputFunction::setOutputIndex(uint32_t Index) {
Sam Cleggfadf5182018-01-28 19:57:03 +0000121 DEBUG(dbgs() << "InputFunction::setOutputIndex: " << getName() << " -> " << Index << "\n");
Sam Clegg50686852018-01-12 18:35:13 +0000122 assert(!hasOutputIndex());
123 OutputIndex = Index;
Eric Christopher9ea500b2018-01-13 00:44:45 +0000124}
Sam Clegg67abf532018-01-24 21:45:25 +0000125
126void InputFunction::setTableIndex(uint32_t Index) {
Sam Cleggfadf5182018-01-28 19:57:03 +0000127 DEBUG(dbgs() << "InputFunction::setTableIndex: " << getName() << " -> " << Index << "\n");
Sam Clegg67abf532018-01-24 21:45:25 +0000128 assert(!hasTableIndex());
129 TableIndex = Index;
130}