llvm.org GIT mirror llvm / 45c215a
[WebAssembly] Add support for dylink section in object format See https://github.com/WebAssembly/tool-conventions/blob/master/DynamicLinking.md. Differential Revision: https://reviews.llvm.org/D54490 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@346880 91177308-0d34-0410-b5e6-96231b3b80d8 Sam Clegg 9 months ago
8 changed file(s) with 103 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
3232 struct WasmObjectHeader {
3333 StringRef Magic;
3434 uint32_t Version;
35 };
36
37 struct WasmDylinkInfo {
38 uint32_t MemorySize; // Memory size in bytes
39 uint32_t MemoryAlignment; // P2 alignment of memory
40 uint32_t TableSize; // Table size in elements
41 uint32_t TableAlignment; // P2 alignment of table
3542 };
3643
3744 struct WasmExport {
128128
129129 static bool classof(const Binary *v) { return v->isWasm(); }
130130
131 const wasm::WasmDylinkInfo &dylinkInfo() const { return DylinkInfo; }
131132 ArrayRef types() const { return Signatures; }
132133 ArrayRef functionTypes() const { return FunctionTypes; }
133134 ArrayRef imports() const { return Imports; }
244245 Error parseDataSection(ReadContext &Ctx);
245246
246247 // Custom section types
248 Error parseDylinkSection(ReadContext &Ctx);
247249 Error parseNameSection(ReadContext &Ctx);
248250 Error parseLinkingSection(ReadContext &Ctx);
249251 Error parseLinkingSectionSymtab(ReadContext &Ctx);
252254
253255 wasm::WasmObjectHeader Header;
254256 std::vector Sections;
257 wasm::WasmDylinkInfo DylinkInfo;
255258 std::vector Signatures;
256259 std::vector FunctionTypes;
257260 std::vector Tables;
180180
181181 StringRef Name;
182182 yaml::BinaryRef Payload;
183 };
184
185 struct DylinkSection : CustomSection {
186 DylinkSection() : CustomSection("dylink") {}
187
188 static bool classof(const Section *S) {
189 auto C = dyn_cast(S);
190 return C && C->Name == "dylink";
191 }
192
193 uint32_t MemorySize;
194 uint32_t MemoryAlignment;
195 uint32_t TableSize;
196 uint32_t TableAlignment;
183197 };
184198
185199 struct NameSection : CustomSection {
312312 }
313313 }
314314
315 Error WasmObjectFile::parseDylinkSection(ReadContext &Ctx) {
316 // See https://github.com/WebAssembly/tool-conventions/blob/master/DynamicLinking.md
317 DylinkInfo.MemorySize = readVaruint32(Ctx);
318 DylinkInfo.MemoryAlignment = readVaruint32(Ctx);
319 DylinkInfo.TableSize = readVaruint32(Ctx);
320 DylinkInfo.TableAlignment = readVaruint32(Ctx);
321 if (Ctx.Ptr != Ctx.End)
322 return make_error("dylink section ended prematurely",
323 object_error::parse_failed);
324 return Error::success();
325 }
326
315327 Error WasmObjectFile::parseNameSection(ReadContext &Ctx) {
316328 llvm::DenseSet Seen;
317329 if (Functions.size() != FunctionTypes.size()) {
720732 }
721733
722734 Error WasmObjectFile::parseCustomSection(WasmSection &Sec, ReadContext &Ctx) {
723 if (Sec.Name == "name") {
735 if (Sec.Name == "dylink") {
736 if (Error Err = parseDylinkSection(Ctx))
737 return Err;
738 } else if (Sec.Name == "name") {
724739 if (Error Err = parseNameSection(Ctx))
725740 return Err;
726741 } else if (Sec.Name == "linking") {
4747 IO.mapOptional("Relocations", Section.Relocations);
4848 }
4949
50 static void sectionMapping(IO &IO, WasmYAML::DylinkSection &Section) {
51 commonSectionMapping(IO, Section);
52 IO.mapRequired("Name", Section.Name);
53 IO.mapRequired("MemorySize", Section.MemorySize);
54 IO.mapRequired("MemoryAlignment", Section.MemoryAlignment);
55 IO.mapRequired("TableSize", Section.TableSize);
56 IO.mapRequired("TableAlignment", Section.TableAlignment);
57 }
58
5059 static void sectionMapping(IO &IO, WasmYAML::NameSection &Section) {
5160 commonSectionMapping(IO, Section);
5261 IO.mapRequired("Name", Section.Name);
146155 } else {
147156 IO.mapRequired("Name", SectionName);
148157 }
149 if (SectionName == "linking") {
158 if (SectionName == "dylink") {
159 if (!IO.outputting())
160 Section.reset(new WasmYAML::DylinkSection());
161 sectionMapping(IO, *cast(Section.get()));
162 } else if (SectionName == "linking") {
150163 if (!IO.outputting())
151164 Section.reset(new WasmYAML::LinkingSection());
152165 sectionMapping(IO, *cast(Section.get()));
0 # RUN: yaml2obj %s | obj2yaml | FileCheck %s
1 --- !WASM
2 FileHeader:
3 Version: 0x00000001
4
5 Sections:
6 - Type: CUSTOM
7 Name: dylink
8 MemorySize: 4
9 MemoryAlignment: 2
10 TableSize: 1
11 TableAlignment: 0
12 ...
13 # CHECK: --- !WASM
14 # CHECK: FileHeader:
15 # CHECK: Version: 0x00000001
16 # CHECK: Sections:
17 # CHECK: - Type: CUSTOM
18 # CHECK: Name: dylink
19 # CHECK: MemorySize: 4
20 # CHECK: MemoryAlignment: 2
21 # CHECK: TableSize: 1
22 # CHECK: TableAlignment: 0
23 # CHECK: ...
5151 std::unique_ptr
5252 WasmDumper::dumpCustomSection(const WasmSection &WasmSec) {
5353 std::unique_ptr CustomSec;
54 if (WasmSec.Name == "name") {
54 if (WasmSec.Name == "dylink") {
55 std::unique_ptr DylinkSec =
56 make_unique();
57 const wasm::WasmDylinkInfo& Info = Obj.dylinkInfo();
58 DylinkSec->MemorySize = Info.MemorySize;
59 DylinkSec->MemoryAlignment = Info.MemoryAlignment;
60 DylinkSec->TableSize = Info.TableSize;
61 DylinkSec->TableAlignment = Info.TableAlignment;
62 CustomSec = std::move(DylinkSec);
63 } else if (WasmSec.Name == "name") {
5564 std::unique_ptr NameSec =
5665 make_unique();
5766 for (const llvm::wasm::WasmFunctionName &Func : Obj.debugNames()) {
4444 int writeSectionContent(raw_ostream &OS, WasmYAML::DataSection &Section);
4545
4646 // Custom section types
47 int writeSectionContent(raw_ostream &OS, WasmYAML::DylinkSection &Section);
4748 int writeSectionContent(raw_ostream &OS, WasmYAML::NameSection &Section);
4849 int writeSectionContent(raw_ostream &OS, WasmYAML::LinkingSection &Section);
4950 WasmYAML::Object &Obj;
130131
131132 raw_ostream &GetStream() { return StringStream; }
132133 };
134
135 int WasmWriter::writeSectionContent(raw_ostream &OS,
136 WasmYAML::DylinkSection &Section) {
137 writeStringRef(Section.Name, OS);
138 encodeULEB128(Section.MemorySize, OS);
139 encodeULEB128(Section.MemoryAlignment, OS);
140 encodeULEB128(Section.TableSize, OS);
141 encodeULEB128(Section.TableAlignment, OS);
142 return 0;
143 }
133144
134145 int WasmWriter::writeSectionContent(raw_ostream &OS,
135146 WasmYAML::LinkingSection &Section) {
240251
241252 int WasmWriter::writeSectionContent(raw_ostream &OS,
242253 WasmYAML::CustomSection &Section) {
243 if (auto S = dyn_castNameSection>(&Section)) {
254 if (auto S = dyn_castDylinkSection>(&Section)) {
255 if (auto Err = writeSectionContent(OS, *S))
256 return Err;
257 } else if (auto S = dyn_cast(&Section)) {
244258 if (auto Err = writeSectionContent(OS, *S))
245259 return Err;
246260 } else if (auto S = dyn_cast(&Section)) {