blob: 29b1c43f06cfcaedf6ca77efec79ec40f35f6822 [file] [log] [blame]
Sam Cleggc94d3932017-11-17 18:14:09 +00001//===- OutputSections.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 "OutputSections.h"
Sam Clegg5fa274b2018-01-10 01:13:34 +000011#include "InputChunks.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000012#include "InputFiles.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000013#include "OutputSegment.h"
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +000014#include "WriterUtils.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000015#include "lld/Common/ErrorHandler.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000016#include "lld/Common/Threads.h"
17#include "llvm/ADT/Twine.h"
Rui Ueyama11842532018-02-16 20:38:00 +000018#include "llvm/Support/LEB128.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000019
20#define DEBUG_TYPE "lld"
21
22using namespace llvm;
23using namespace llvm::wasm;
24using namespace lld;
25using namespace lld::wasm;
26
Sam Cleggc94d3932017-11-17 18:14:09 +000027static StringRef sectionTypeToString(uint32_t SectionType) {
28 switch (SectionType) {
29 case WASM_SEC_CUSTOM:
30 return "CUSTOM";
31 case WASM_SEC_TYPE:
32 return "TYPE";
33 case WASM_SEC_IMPORT:
34 return "IMPORT";
35 case WASM_SEC_FUNCTION:
36 return "FUNCTION";
37 case WASM_SEC_TABLE:
38 return "TABLE";
39 case WASM_SEC_MEMORY:
40 return "MEMORY";
41 case WASM_SEC_GLOBAL:
42 return "GLOBAL";
43 case WASM_SEC_EXPORT:
44 return "EXPORT";
45 case WASM_SEC_START:
46 return "START";
47 case WASM_SEC_ELEM:
48 return "ELEM";
49 case WASM_SEC_CODE:
50 return "CODE";
51 case WASM_SEC_DATA:
52 return "DATA";
53 default:
54 fatal("invalid section type");
55 }
56}
57
Rui Ueyama22c8f332018-02-28 17:33:04 +000058// Returns a string, e.g. "FUNCTION(.text)".
59std::string lld::toString(const OutputSection &Sec) {
60 if (!Sec.Name.empty())
61 return (Sec.getSectionName() + "(" + Sec.Name + ")").str();
62 return Sec.getSectionName();
Sam Cleggc94d3932017-11-17 18:14:09 +000063}
64
Rui Ueyama22c8f332018-02-28 17:33:04 +000065StringRef OutputSection::getSectionName() const {
Sam Clegg0d0dd392017-12-19 17:09:45 +000066 return sectionTypeToString(Type);
67}
68
Sam Cleggc94d3932017-11-17 18:14:09 +000069void OutputSection::createHeader(size_t BodySize) {
70 raw_string_ostream OS(Header);
Rui Ueyama22c8f332018-02-28 17:33:04 +000071 debugWrite(OS.tell(), "section type [" + getSectionName() + "]");
Rui Ueyama11842532018-02-16 20:38:00 +000072 encodeULEB128(Type, OS);
Sam Cleggc94d3932017-11-17 18:14:09 +000073 writeUleb128(OS, BodySize, "section size");
74 OS.flush();
Sam Cleggab2ac292017-12-20 05:14:48 +000075 log("createHeader: " + toString(*this) + " body=" + Twine(BodySize) +
Sam Cleggc94d3932017-11-17 18:14:09 +000076 " total=" + Twine(getSize()));
77}
78
Sam Clegg8d146bb2018-01-09 23:56:44 +000079CodeSection::CodeSection(ArrayRef<InputFunction *> Functions)
80 : OutputSection(WASM_SEC_CODE), Functions(Functions) {
81 assert(Functions.size() > 0);
82
Sam Cleggc94d3932017-11-17 18:14:09 +000083 raw_string_ostream OS(CodeSectionHeader);
Sam Clegg8d146bb2018-01-09 23:56:44 +000084 writeUleb128(OS, Functions.size(), "function count");
Sam Cleggc94d3932017-11-17 18:14:09 +000085 OS.flush();
86 BodySize = CodeSectionHeader.size();
87
Sam Clegg5fa274b2018-01-10 01:13:34 +000088 for (InputChunk *Func : Functions) {
Rui Ueyamabf450d92018-02-20 04:26:26 +000089 Func->OutputOffset = BodySize;
Sam Clegg5fa274b2018-01-10 01:13:34 +000090 BodySize += Func->getSize();
Sam Cleggc94d3932017-11-17 18:14:09 +000091 }
92
93 createHeader(BodySize);
94}
95
96void CodeSection::writeTo(uint8_t *Buf) {
Sam Cleggab2ac292017-12-20 05:14:48 +000097 log("writing " + toString(*this));
Sam Cleggc94d3932017-11-17 18:14:09 +000098 log(" size=" + Twine(getSize()));
Sam Clegg8d146bb2018-01-09 23:56:44 +000099 log(" headersize=" + Twine(Header.size()));
100 log(" codeheadersize=" + Twine(CodeSectionHeader.size()));
Sam Cleggc94d3932017-11-17 18:14:09 +0000101 Buf += Offset;
102
103 // Write section header
104 memcpy(Buf, Header.data(), Header.size());
105 Buf += Header.size();
106
Sam Cleggc94d3932017-11-17 18:14:09 +0000107 // Write code section headers
108 memcpy(Buf, CodeSectionHeader.data(), CodeSectionHeader.size());
Sam Cleggc94d3932017-11-17 18:14:09 +0000109
110 // Write code section bodies
Nicholas Wilsondbd90bf2018-03-07 13:28:16 +0000111 parallelForEach(Functions,
112 [&](const InputChunk *Chunk) { Chunk->writeTo(Buf); });
Sam Cleggc94d3932017-11-17 18:14:09 +0000113}
114
115uint32_t CodeSection::numRelocations() const {
116 uint32_t Count = 0;
Sam Clegg5fa274b2018-01-10 01:13:34 +0000117 for (const InputChunk *Func : Functions)
Rui Ueyamabf450d92018-02-20 04:26:26 +0000118 Count += Func->NumRelocations();
Sam Cleggc94d3932017-11-17 18:14:09 +0000119 return Count;
120}
121
122void CodeSection::writeRelocations(raw_ostream &OS) const {
Rui Ueyamabf450d92018-02-20 04:26:26 +0000123 for (const InputChunk *C : Functions)
124 C->writeRelocations(OS);
Sam Cleggc94d3932017-11-17 18:14:09 +0000125}
126
Sam Clegg0fb6faa2017-12-08 01:09:21 +0000127DataSection::DataSection(ArrayRef<OutputSegment *> Segments)
Sam Cleggc94d3932017-11-17 18:14:09 +0000128 : OutputSection(WASM_SEC_DATA), Segments(Segments) {
129 raw_string_ostream OS(DataSectionHeader);
130
131 writeUleb128(OS, Segments.size(), "data segment count");
132 OS.flush();
133 BodySize = DataSectionHeader.size();
134
135 for (OutputSegment *Segment : Segments) {
136 raw_string_ostream OS(Segment->Header);
137 writeUleb128(OS, 0, "memory index");
138 writeUleb128(OS, WASM_OPCODE_I32_CONST, "opcode:i32const");
139 writeSleb128(OS, Segment->StartVA, "memory offset");
140 writeUleb128(OS, WASM_OPCODE_END, "opcode:end");
141 writeUleb128(OS, Segment->Size, "segment size");
142 OS.flush();
Rui Ueyamaac95bb12018-04-05 19:37:31 +0000143
144 Segment->SectionOffset = BodySize;
Sam Cleggd96d9352018-01-10 19:22:42 +0000145 BodySize += Segment->Header.size() + Segment->Size;
Sam Cleggc94d3932017-11-17 18:14:09 +0000146 log("Data segment: size=" + Twine(Segment->Size));
Rui Ueyamaac95bb12018-04-05 19:37:31 +0000147
Sam Cleggd96d9352018-01-10 19:22:42 +0000148 for (InputSegment *InputSeg : Segment->InputSegments)
Rui Ueyamaac95bb12018-04-05 19:37:31 +0000149 InputSeg->OutputOffset = Segment->SectionOffset + Segment->Header.size() +
Rui Ueyamabf450d92018-02-20 04:26:26 +0000150 InputSeg->OutputSegmentOffset;
Sam Cleggc94d3932017-11-17 18:14:09 +0000151 }
152
153 createHeader(BodySize);
154}
155
156void DataSection::writeTo(uint8_t *Buf) {
Sam Cleggab2ac292017-12-20 05:14:48 +0000157 log("writing " + toString(*this) + " size=" + Twine(getSize()) +
Sam Cleggc94d3932017-11-17 18:14:09 +0000158 " body=" + Twine(BodySize));
159 Buf += Offset;
160
161 // Write section header
162 memcpy(Buf, Header.data(), Header.size());
163 Buf += Header.size();
164
Sam Cleggc94d3932017-11-17 18:14:09 +0000165 // Write data section headers
166 memcpy(Buf, DataSectionHeader.data(), DataSectionHeader.size());
167
Rui Ueyama319eb8b2018-02-28 00:31:16 +0000168 parallelForEach(Segments, [&](const OutputSegment *Segment) {
Sam Cleggc94d3932017-11-17 18:14:09 +0000169 // Write data segment header
Rui Ueyamaac95bb12018-04-05 19:37:31 +0000170 uint8_t *SegStart = Buf + Segment->SectionOffset;
Sam Cleggc94d3932017-11-17 18:14:09 +0000171 memcpy(SegStart, Segment->Header.data(), Segment->Header.size());
172
173 // Write segment data payload
Sam Cleggd96d9352018-01-10 19:22:42 +0000174 for (const InputChunk *Chunk : Segment->InputSegments)
Rui Ueyama319eb8b2018-02-28 00:31:16 +0000175 Chunk->writeTo(Buf);
Sam Cleggc94d3932017-11-17 18:14:09 +0000176 });
Sam Clegg5e8cba92017-12-19 20:45:15 +0000177}
Sam Cleggc94d3932017-11-17 18:14:09 +0000178
Sam Clegg5e8cba92017-12-19 20:45:15 +0000179uint32_t DataSection::numRelocations() const {
180 uint32_t Count = 0;
181 for (const OutputSegment *Seg : Segments)
Sam Clegg5fa274b2018-01-10 01:13:34 +0000182 for (const InputChunk *InputSeg : Seg->InputSegments)
Rui Ueyamabf450d92018-02-20 04:26:26 +0000183 Count += InputSeg->NumRelocations();
Sam Clegg5e8cba92017-12-19 20:45:15 +0000184 return Count;
Sam Cleggc94d3932017-11-17 18:14:09 +0000185}
186
187void DataSection::writeRelocations(raw_ostream &OS) const {
Sam Clegg5e8cba92017-12-19 20:45:15 +0000188 for (const OutputSegment *Seg : Segments)
Rui Ueyamabf450d92018-02-20 04:26:26 +0000189 for (const InputChunk *C : Seg->InputSegments)
190 C->writeRelocations(OS);
Sam Cleggc94d3932017-11-17 18:14:09 +0000191}