blob: f5df6eb0eb1b607da1d58093f9544fe45d13c2b7 [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"
Rui Ueyamabf450d92018-02-20 04:26:26 +000013#include "WriterUtils.h"
Sam Cleggd96d9352018-01-10 19:22:42 +000014#include "lld/Common/ErrorHandler.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000015#include "lld/Common/LLVM.h"
Sam Cleggd96d9352018-01-10 19:22:42 +000016#include "llvm/Support/LEB128.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000017
18#define DEBUG_TYPE "lld"
19
20using namespace llvm;
Sam Cleggd96d9352018-01-10 19:22:42 +000021using namespace llvm::wasm;
Rui Ueyamae351c3a2018-02-16 20:38:15 +000022using namespace llvm::support::endian;
Sam Cleggd96d9352018-01-10 19:22:42 +000023using namespace lld;
Sam Cleggc94d3932017-11-17 18:14:09 +000024using namespace lld::wasm;
25
Rui Ueyama81bee042018-02-19 22:29:48 +000026std::string lld::toString(const InputChunk *C) {
27 return (toString(C->File) + ":(" + C->getName() + ")").str();
28}
29
Sam Clegg5fa274b2018-01-10 01:13:34 +000030void InputChunk::copyRelocations(const WasmSection &Section) {
Sam Clegg50686852018-01-12 18:35:13 +000031 if (Section.Relocations.empty())
32 return;
Sam Clegg5fa274b2018-01-10 01:13:34 +000033 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
Rui Ueyamabf450d92018-02-20 04:26:26 +000040// Copy this input chunk to an mmap'ed output file and apply relocations.
41void InputChunk::writeTo(uint8_t *Buf) const {
42 // Copy contents
43 memcpy(Buf + OutputOffset, data().data(), data().size());
Rui Ueyamac06d94a2018-02-19 22:39:52 +000044
Rui Ueyamabf450d92018-02-20 04:26:26 +000045 // Apply relocations
46 if (Relocations.empty())
47 return;
Rui Ueyamac06d94a2018-02-19 22:39:52 +000048
Rui Ueyamabf450d92018-02-20 04:26:26 +000049 DEBUG(dbgs() << "applyRelocations: count=" << Relocations.size() << "\n");
50 int32_t Off = OutputOffset - getInputSectionOffset();
51
52 for (const WasmRelocation &Rel : Relocations) {
53 uint8_t *Loc = Buf + Rel.Offset + Off;
54 uint64_t Value = File->calcNewValue(Rel);
55
56 DEBUG(dbgs() << "write reloc: type=" << Rel.Type << " index=" << Rel.Index
57 << " value=" << Value << " offset=" << Rel.Offset << "\n");
58
59 switch (Rel.Type) {
60 case R_WEBASSEMBLY_TYPE_INDEX_LEB:
61 case R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
62 case R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
63 case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
64 encodeULEB128(Value, Loc, 5);
65 break;
66 case R_WEBASSEMBLY_TABLE_INDEX_SLEB:
67 case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
68 encodeSLEB128(static_cast<int32_t>(Value), Loc, 5);
69 break;
70 case R_WEBASSEMBLY_TABLE_INDEX_I32:
71 case R_WEBASSEMBLY_MEMORY_ADDR_I32:
72 write32le(Loc, Value);
73 break;
74 default:
75 llvm_unreachable("unknown relocation type");
Sam Cleggd96d9352018-01-10 19:22:42 +000076 }
Sam Cleggd96d9352018-01-10 19:22:42 +000077 }
78}
79
Rui Ueyamabf450d92018-02-20 04:26:26 +000080// Copy relocation entries to a given output stream.
81// This function is used only when a user passes "-r". For a regular link,
82// we consume relocations instead of copying them to an output file.
83void InputChunk::writeRelocations(raw_ostream &OS) const {
Sam Clegg50686852018-01-12 18:35:13 +000084 if (Relocations.empty())
85 return;
Rui Ueyamabf450d92018-02-20 04:26:26 +000086
87 int32_t Off = OutputOffset - getInputSectionOffset();
88 DEBUG(dbgs() << "writeRelocations: " << File->getName()
Sam Clegg7ed293e2018-01-12 00:34:04 +000089 << " offset=" << Twine(Off) << "\n");
Sam Cleggd96d9352018-01-10 19:22:42 +000090
Rui Ueyamabf450d92018-02-20 04:26:26 +000091 for (const WasmRelocation &Rel : Relocations) {
92 writeUleb128(OS, Rel.Type, "reloc type");
93 writeUleb128(OS, Rel.Offset + Off, "reloc offset");
94 writeUleb128(OS, File->calcNewIndex(Rel), "reloc index");
Sam Cleggd96d9352018-01-10 19:22:42 +000095
Rui Ueyamabf450d92018-02-20 04:26:26 +000096 switch (Rel.Type) {
97 case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
98 case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
99 case R_WEBASSEMBLY_MEMORY_ADDR_I32:
100 writeUleb128(OS, Rel.Addend, "reloc addend");
101 break;
102 }
Sam Cleggd96d9352018-01-10 19:22:42 +0000103 }
104}
Sam Clegg50686852018-01-12 18:35:13 +0000105
106void InputFunction::setOutputIndex(uint32_t Index) {
Nicholas Wilsondbd90bf2018-03-07 13:28:16 +0000107 DEBUG(dbgs() << "InputFunction::setOutputIndex: " << getName() << " -> "
108 << Index << "\n");
Sam Clegg50686852018-01-12 18:35:13 +0000109 assert(!hasOutputIndex());
110 OutputIndex = Index;
Eric Christopher9ea500b2018-01-13 00:44:45 +0000111}
Sam Clegg67abf532018-01-24 21:45:25 +0000112
113void InputFunction::setTableIndex(uint32_t Index) {
Nicholas Wilsondbd90bf2018-03-07 13:28:16 +0000114 DEBUG(dbgs() << "InputFunction::setTableIndex: " << getName() << " -> "
115 << Index << "\n");
Sam Clegg67abf532018-01-24 21:45:25 +0000116 assert(!hasTableIndex());
117 TableIndex = Index;
118}