llvm.org GIT mirror llvm / 194a4e0
[WebAssembly] Use section index in relocation section header Rather than referring to sections my their code, use the absolute index of the target section within the module. See https://github.com/WebAssembly/tool-conventions/issues/52 Differential Revision: https://reviews.llvm.org/D45980 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@330749 91177308-0d34-0410-b5e6-96231b3b80d8 Sam Clegg 1 year, 4 months ago
7 changed file(s) with 51 addition(s) and 91 deletion(s). Raw diff Collapse all Expand all
210210
211211 const WasmSection &getWasmSection(DataRefImpl Ref) const;
212212 const wasm::WasmRelocation &getWasmRelocation(DataRefImpl Ref) const;
213
214 WasmSection* findCustomSectionByName(StringRef Name);
215 WasmSection* findSectionByType(uint32_t Type);
216213
217214 const uint8_t *getPtr(size_t Offset) const;
218215 Error parseSection(WasmSection &Sec);
4949 uint64_t SizeOffset;
5050 // Where the contents of the section starts (after the header).
5151 uint64_t ContentsOffset;
52 uint32_t Index;
5253 };
5354
5455 // The signature of a wasm function, in a struct capable of being used as a
187188
188189 // Relocations for fixing up references in the code section.
189190 std::vector CodeRelocations;
191 uint32_t CodeSectionIndex;
190192
191193 // Relocations for fixing up references in the data section.
192194 std::vector DataRelocations;
195 uint32_t DataSectionIndex;
193196
194197 // Index values to use for fixing up call_indirect type indices.
195198 // Maps function symbols to the index of the type of the function
212215 std::vector CustomSections;
213216 unsigned NumFunctionImports = 0;
214217 unsigned NumGlobalImports = 0;
218 uint32_t SectionCount;
215219
216220 // TargetObjectWriter wrappers.
217221 bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
279283 void writeCodeSection(const MCAssembler &Asm, const MCAsmLayout &Layout,
280284 ArrayRef Functions);
281285 void writeDataSection();
282 void writeCodeRelocSection();
283 void writeDataRelocSection();
286 void writeRelocSection(uint32_t SectionIndex, StringRef Name,
287 ArrayRef Relocations);
284288 void writeLinkingMetaDataSection(
285289 ArrayRef SymbolInfos,
286290 ArrayRef> InitFuncs,
291295 void applyRelocations(ArrayRef Relocations,
292296 uint64_t ContentsOffset);
293297
294 void writeRelocations(ArrayRef Relocations);
295298 uint32_t getRelocationIndexValue(const WasmRelocationEntry &RelEntry);
296 uint32_t getFunctionType(const MCSymbolWasm& Symbol);
297 uint32_t registerFunctionType(const MCSymbolWasm& Symbol);
299 uint32_t getFunctionType(const MCSymbolWasm &Symbol);
300 uint32_t registerFunctionType(const MCSymbolWasm &Symbol);
298301 };
299302
300303 } // end anonymous namespace
315318
316319 // The position where the section starts, for measuring its size.
317320 Section.ContentsOffset = getStream().tell();
321 Section.Index = SectionCount++;
318322 }
319323
320324 void WasmObjectWriter::startCustomSection(SectionBookkeeping &Section,
621625 }
622626 }
623627
624 // Write out the portions of the relocation records that the linker will
625 // need to handle.
626 void WasmObjectWriter::writeRelocations(
627 ArrayRef Relocations) {
628 raw_pwrite_stream &Stream = getStream();
629 for (const WasmRelocationEntry& RelEntry : Relocations) {
630
631 uint64_t Offset = RelEntry.Offset +
632 RelEntry.FixupSection->getSectionOffset();
633 uint32_t Index = getRelocationIndexValue(RelEntry);
634
635 write8(RelEntry.Type);
636 encodeULEB128(Offset, Stream);
637 encodeULEB128(Index, Stream);
638 if (RelEntry.hasAddend())
639 encodeSLEB128(RelEntry.Addend, Stream);
640 }
641 }
642
643628 void WasmObjectWriter::writeTypeSection(
644629 ArrayRef FunctionTypes) {
645630 if (FunctionTypes.empty())
786771
787772 SectionBookkeeping Section;
788773 startSection(Section, wasm::WASM_SEC_CODE);
774 CodeSectionIndex = Section.Index;
789775
790776 encodeULEB128(Functions.size(), getStream());
791777
813799
814800 SectionBookkeeping Section;
815801 startSection(Section, wasm::WASM_SEC_DATA);
802 DataSectionIndex = Section.Index;
816803
817804 encodeULEB128(DataSegments.size(), getStream()); // count
818805
832819 endSection(Section);
833820 }
834821
835 void WasmObjectWriter::writeCodeRelocSection() {
822 void WasmObjectWriter::writeRelocSection(
823 uint32_t SectionIndex, StringRef Name,
824 ArrayRef Relocations) {
836825 // See: https://github.com/WebAssembly/tool-conventions/blob/master/Linking.md
837826 // for descriptions of the reloc sections.
838827
839 if (CodeRelocations.empty())
828 if (Relocations.empty())
840829 return;
841830
842831 SectionBookkeeping Section;
843 startCustomSection(Section, "reloc.CODE");
844
845 encodeULEB128(wasm::WASM_SEC_CODE, getStream());
846 encodeULEB128(CodeRelocations.size(), getStream());
847
848 writeRelocations(CodeRelocations);
849
850 endSection(Section);
851 }
852
853 void WasmObjectWriter::writeDataRelocSection() {
854 // See: https://github.com/WebAssembly/tool-conventions/blob/master/Linking.md
855 // for descriptions of the reloc sections.
856
857 if (DataRelocations.empty())
858 return;
859
860 SectionBookkeeping Section;
861 startCustomSection(Section, "reloc.DATA");
862
863 encodeULEB128(wasm::WASM_SEC_DATA, getStream());
864 encodeULEB128(DataRelocations.size(), getStream());
865
866 writeRelocations(DataRelocations);
832 startCustomSection(Section, std::string("reloc.") + Name.str());
833
834 raw_pwrite_stream &Stream = getStream();
835
836 encodeULEB128(SectionIndex, Stream);
837 encodeULEB128(Relocations.size(), Stream);
838 for (const WasmRelocationEntry& RelEntry : Relocations) {
839 uint64_t Offset = RelEntry.Offset +
840 RelEntry.FixupSection->getSectionOffset();
841 uint32_t Index = getRelocationIndexValue(RelEntry);
842
843 write8(RelEntry.Type);
844 encodeULEB128(Offset, Stream);
845 encodeULEB128(Index, Stream);
846 if (RelEntry.hasAddend())
847 encodeSLEB128(RelEntry.Addend, Stream);
848 }
867849
868850 endSection(Section);
869851 }
13491331 writeDataSection();
13501332 writeUserCustomSections(CustomSections);
13511333 writeLinkingMetaDataSection(SymbolInfos, InitFuncs, Comdats);
1352 writeCodeRelocSection();
1353 writeDataRelocSection();
1334 writeRelocSection(CodeSectionIndex, "CODE", CodeRelocations);
1335 writeRelocSection(DataSectionIndex, "DATA", DataRelocations);
13541336
13551337 // TODO: Translate the .comment section to the output.
13561338 // TODO: Translate debug sections to the output.
523523 return Error::success();
524524 }
525525
526 WasmSection* WasmObjectFile::findCustomSectionByName(StringRef Name) {
527 for (WasmSection& Section : Sections) {
528 if (Section.Type == wasm::WASM_SEC_CUSTOM && Section.Name == Name)
529 return &Section;
530 }
531 return nullptr;
532 }
533
534 WasmSection* WasmObjectFile::findSectionByType(uint32_t Type) {
535 assert(Type != wasm::WASM_SEC_CUSTOM);
536 for (WasmSection& Section : Sections) {
537 if (Section.Type == Type)
538 return &Section;
539 }
540 return nullptr;
541 }
542
543526 Error WasmObjectFile::parseRelocSection(StringRef Name, const uint8_t *Ptr,
544527 const uint8_t *End) {
545 uint8_t SectionCode = readUint8(Ptr);
546 WasmSection* Section = nullptr;
547 if (SectionCode == wasm::WASM_SEC_CUSTOM) {
548 StringRef Name = readString(Ptr);
549 Section = findCustomSectionByName(Name);
550 } else {
551 Section = findSectionByType(SectionCode);
552 }
553 if (!Section)
554 return make_error("Invalid section code",
555 object_error::parse_failed);
528 uint32_t SectionIndex = readVaruint32(Ptr);
529 if (SectionIndex >= Sections.size())
530 return make_error("Invalid section index",
531 object_error::parse_failed);
532 WasmSection& Section = Sections[SectionIndex];
556533 uint32_t RelocCount = readVaruint32(Ptr);
557 uint32_t EndOffset = Section->Content.size();
534 uint32_t EndOffset = Section.Content.size();
558535 while (RelocCount--) {
559536 wasm::WasmRelocation Reloc = {};
560537 Reloc.Type = readVaruint32(Ptr);
603580 return make_error("Bad relocation offset",
604581 object_error::parse_failed);
605582
606 Section->Relocations.push_back(Reloc);
583 Section.Relocations.push_back(Reloc);
607584 }
608585 if (Ptr != End)
609586 return make_error("Reloc section ended prematurely",
2626 int writeWasm(raw_ostream &OS);
2727
2828 private:
29 int writeRelocSection(raw_ostream &OS, WasmYAML::Section &Sec);
29 int writeRelocSection(raw_ostream &OS, WasmYAML::Section &Sec,
30 uint32_t SectionIndex);
3031
3132 int writeSectionContent(raw_ostream &OS, WasmYAML::CustomSection &Section);
3233 int writeSectionContent(raw_ostream &OS, WasmYAML::TypeSection &Section);
419420 return 0;
420421 }
421422
422 int WasmWriter::writeRelocSection(raw_ostream &OS,
423 WasmYAML::Section &Sec) {
423 int WasmWriter::writeRelocSection(raw_ostream &OS, WasmYAML::Section &Sec,
424 uint32_t SectionIndex) {
424425 StringRef Name;
425426 switch (Sec.Type) {
426427 case wasm::WASM_SEC_CODE:
435436 }
436437
437438 writeStringRef(Name, OS);
438 writeUint8(OS, Sec.Type);
439 encodeULEB128(SectionIndex, OS);
439440 encodeULEB128(Sec.Relocations.size(), OS);
440441
441442 for (auto Reloc: Sec.Relocations) {
521522 }
522523
523524 // write reloc sections for any section that have relocations
525 uint32_t SectionIndex = 0;
524526 for (const std::unique_ptr &Sec : Obj.Sections) {
525 if (Sec->Relocations.empty())
527 if (Sec->Relocations.empty()) {
528 SectionIndex++;
526529 continue;
530 }
527531
528532 writeUint8(OS, wasm::WASM_SEC_CUSTOM);
529533 std::string OutString;
530534 raw_string_ostream StringStream(OutString);
531 writeRelocSection(StringStream, *Sec);
535 writeRelocSection(StringStream, *Sec, SectionIndex++);
532536 StringStream.flush();
533537
534538 encodeULEB128(OutString.size(), OS);