llvm.org GIT mirror llvm / c9c28d9
[WebAssembly] Expose the offset of each data segment Summary: This allows tools like lld that process relocations to apply data relocation correctly. This information is required because relocation are stored as section offset. Subscribers: jfb, dschuff, jgravelle-google, aheejin Differential Revision: https://reviews.llvm.org/D35234 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@307741 91177308-0d34-0410-b5e6-96231b3b80d8 Sam Clegg 3 years ago
10 changed file(s) with 31 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
9393 };
9494
9595 struct WasmDataSegment {
96 uint32_t Index;
96 uint32_t MemoryIndex;
9797 WasmInitExpr Offset;
9898 ArrayRef Content;
9999 };
6868 #endif
6969 };
7070
71 class WasmSection {
72 public:
71 struct WasmSection {
7372 WasmSection() = default;
7473
7574 uint32_t Type = 0; // Section type (See below)
7776 StringRef Name; // Section name (User-defined sections only)
7877 ArrayRef Content; // Section content
7978 std::vector Relocations; // Relocations for this section
79 };
80
81 struct WasmSegment {
82 uint32_t SectionOffset;
83 wasm::WasmDataSegment Data;
8084 };
8185
8286 class WasmObjectFile : public ObjectFile {
109113 return ElemSegments;
110114 }
111115
112 const std::vector<wasm::WasmDataSegment>& dataSegments() const {
116 const std::vector<WasmSegment>& dataSegments() const {
113117 return DataSegments;
114118 }
115119
209213 std::vector Imports;
210214 std::vector Exports;
211215 std::vector ElemSegments;
212 std::vector<wasm::WasmDataSegment> DataSegments;
216 std::vector<WasmSegment> DataSegments;
213217 std::vector Functions;
214218 std::vector Symbols;
215219 ArrayRef CodeSection;
9797 };
9898
9999 struct DataSegment {
100 uint32_t Index;
100 uint32_t MemoryIndex;
101 uint32_t SectionOffset;
101102 wasm::WasmInitExpr Offset;
102103 yaml::BinaryRef Content;
103104 };
674674 }
675675
676676 Error WasmObjectFile::parseDataSection(const uint8_t *Ptr, const uint8_t *End) {
677 const uint8_t *Start = Ptr;
677678 uint32_t Count = readVaruint32(Ptr);
678679 DataSegments.reserve(Count);
679680 while (Count--) {
680 wasm::WasmDataSegment Segment;
681 Segment.Index = readVaruint32(Ptr);
682 if (Error Err = readInitExpr(Segment.Offset, Ptr))
681 WasmSegment Segment;
682 Segment.Data.MemoryIndex = readVaruint32(Ptr);
683 if (Error Err = readInitExpr(Segment.Data.Offset, Ptr))
683684 return Err;
684685 uint32_t Size = readVaruint32(Ptr);
685 Segment.Content = ArrayRef(Ptr, Size);
686 Segment.Data.Content = ArrayRef(Ptr, Size);
687 Segment.SectionOffset = Ptr - Start;
686688 Ptr += Size;
687689 DataSegments.push_back(Segment);
688690 }
344344
345345 void MappingTraits::mapping(
346346 IO &IO, WasmYAML::DataSegment &Segment) {
347 IO.mapRequired("Index", Segment.Index);
347 IO.mapOptional("SectionOffset", Segment.SectionOffset);
348 IO.mapRequired("MemoryIndex", Segment.MemoryIndex);
348349 IO.mapRequired("Offset", Segment.Offset);
349350 IO.mapRequired("Content", Segment.Content);
350351 }
1212 ; CHECK: Index: 0
1313 ; CHECK: Offset: 0x0000000E
1414 ; CHECK: Segments:
15 ; CHECK: - Index: 0
15 ; CHECK: - SectionOffset: 6
16 ; CHECK: MemoryIndex: 0
1617 ; CHECK: Offset:
1718 ; CHECK: Opcode: I32_CONST
1819 ; CHECK: Value: 0
4545 ; CHECK-NEXT: Index: 1
4646 ; CHECK-NEXT: Offset: 0x0000001E
4747 ; CHECK-NEXT: Segments:
48 ; CHECK-NEXT: - Index: 0
48 ; CHECK-NEXT: - SectionOffset: 6
49 ; CHECK-NEXT: MemoryIndex: 0
4950 ; CHECK-NEXT: Offset:
5051 ; CHECK-NEXT: Opcode: I32_CONST
5152 ; CHECK-NEXT: Value: 0
77 - Initial: 0x00000003
88 - Type: DATA
99 Segments:
10 - Index: 0
10 - MemoryIndex: 0
1111 Offset:
1212 Opcode: I32_CONST
1313 Value: 4
3737 # CHECK-NEXT: Offset: 0x00000006
3838 # CHECK-NEXT: Addend: -6
3939 # CHECK-NEXT: Segments:
40 # CHECK-NEXT: - Index: 0
40 # CHECK-NEXT: - SectionOffset: 6
41 # CHECK-NEXT: MemoryIndex: 0
4142 # CHECK-NEXT: Offset:
4243 # CHECK-NEXT: Opcode: I32_CONST
4344 # CHECK-NEXT: Value: 4
235235 auto DataSec = make_unique();
236236 for (auto &Segment : Obj.dataSegments()) {
237237 WasmYAML::DataSegment Seg;
238 Seg.Index = Segment.Index;
239 Seg.Offset = Segment.Offset;
240 Seg.Content = yaml::BinaryRef(Segment.Content);
238 Seg.SectionOffset = Segment.SectionOffset;
239 Seg.MemoryIndex = Segment.Data.MemoryIndex;
240 Seg.Offset = Segment.Data.Offset;
241 Seg.Content = yaml::BinaryRef(Segment.Data.Content);
241242 DataSec->Segments.push_back(Seg);
242243 }
243244 S = std::move(DataSec);
337337 WasmYAML::DataSection &Section) {
338338 encodeULEB128(Section.Segments.size(), OS);
339339 for (auto &Segment : Section.Segments) {
340 encodeULEB128(Segment.Index, OS);
340 encodeULEB128(Segment.MemoryIndex, OS);
341341 writeInitExpr(Segment.Offset, OS);
342342 encodeULEB128(Segment.Content.binary_size(), OS);
343343 Segment.Content.writeAsBinary(OS);