[WebAssembly] Add version to object file metadata

Summary: See https://github.com/WebAssembly/tool-conventions/issues/54

Subscribers: jfb, dschuff, jgravelle-google, aheejin, sunfish, llvm-commits

Differential Revision: https://reviews.llvm.org/D46069

llvm-svn: 330969
This commit is contained in:
Sam Clegg 2018-04-26 18:15:32 +00:00
parent b31f918979
commit 6bb5a41f99
31 changed files with 57 additions and 22 deletions

View File

@ -24,6 +24,8 @@ namespace wasm {
const char WasmMagic[] = {'\0', 'a', 's', 'm'};
// Wasm binary format version
const uint32_t WasmVersion = 0x1;
// Wasm linking metadata version
const uint32_t WasmMetadataVersion = 0x1;
// Wasm uses a 64k page size
const uint32_t WasmPageSize = 65536;
@ -161,6 +163,7 @@ struct WasmFunctionName {
};
struct WasmLinkingData {
uint32_t Version;
std::vector<WasmInitFunc> InitFunctions;
std::vector<StringRef> Comdats;
std::vector<WasmSymbolInfo> SymbolTable;

View File

@ -195,6 +195,7 @@ struct LinkingSection : CustomSection {
return C && C->Name == "linking";
}
uint32_t Version;
std::vector<SymbolInfo> SymbolTable;
std::vector<SegmentInfo> SegmentInfos;
std::vector<InitFunction> InitFunctions;

View File

@ -857,8 +857,9 @@ void WasmObjectWriter::writeLinkingMetaDataSection(
const std::map<StringRef, std::vector<WasmComdatEntry>> &Comdats) {
SectionBookkeeping Section;
startCustomSection(Section, "linking");
SectionBookkeeping SubSection;
encodeULEB128(wasm::WasmMetadataVersion, getStream());
SectionBookkeeping SubSection;
if (SymbolInfos.size() != 0) {
startSection(SubSection, wasm::WASM_SYMBOL_TABLE);
encodeULEB128(SymbolInfos.size(), getStream());

View File

@ -313,6 +313,14 @@ Error WasmObjectFile::parseLinkingSection(const uint8_t *Ptr,
"Linking data must come after code section", object_error::parse_failed);
}
LinkingData.Version = readVaruint32(Ptr);
if (LinkingData.Version != wasm::WasmMetadataVersion) {
return make_error<GenericBinaryError>(
"Unexpected metadata version: " + Twine(LinkingData.Version) +
" (Expected: " + Twine(wasm::WasmMetadataVersion) + ")",
object_error::parse_failed);
}
while (Ptr < End) {
uint8_t Type = readUint8(Ptr);
uint32_t Size = readVaruint32(Ptr);

View File

@ -57,6 +57,7 @@ static void sectionMapping(IO &IO, WasmYAML::NameSection &Section) {
static void sectionMapping(IO &IO, WasmYAML::LinkingSection &Section) {
commonSectionMapping(IO, Section);
IO.mapRequired("Name", Section.Name);
IO.mapRequired("Version", Section.Version);
IO.mapOptional("SymbolTable", Section.SymbolTable);
IO.mapOptional("SegmentInfo", Section.SegmentInfos);
IO.mapOptional("InitFunctions", Section.InitFunctions);

View File

@ -12,9 +12,7 @@ target triple = "wasm32-unknown-unknown-wasm"
; CHECK: - Type: DATA
; CHECK: Content: '0102'
; CHECK: - Type: CUSTOM
; CHECK-NEXT: Name: linking
; CHECK-NEXT: SymbolTable:
; CHECK: SymbolTable:
; CHECK-NEXT: - Index: 0
; CHECK-NEXT: Kind: DATA
; CHECK-NEXT: Name: gBd

View File

@ -35,9 +35,8 @@ target triple = "wasm32-unknown-unknown-wasm"
; CHECK-NEXT: Opcode: I32_CONST
; CHECK-NEXT: Value: 8
; CHECK-NEXT: Content: ''
; CHECK-NEXT: - Type: CUSTOM
; CHECK-NEXT: Name: linking
; CHECK-NEXT: SymbolTable:
; CHECK: SymbolTable:
; CHECK-NEXT: - Index: 0
; CHECK-NEXT: Kind: DATA
; CHECK-NEXT: Name: g0

View File

@ -75,6 +75,7 @@ define linkonce_odr i32 @sharedFn() #1 comdat($sharedComdat) {
; CHECK-NEXT: Content: '616263'
; CHECK-NEXT: - Type: CUSTOM
; CHECK-NEXT: Name: linking
; CHECK-NEXT: Version: 1
; CHECK-NEXT: SymbolTable:
; CHECK-NEXT: - Index: 0
; CHECK-NEXT: Kind: FUNCTION

View File

@ -30,9 +30,8 @@ target triple = "wasm32-unknown-unknown-wasm"
; CHECK-NEXT: Opcode: I32_CONST
; CHECK-NEXT: Value: 24
; CHECK-NEXT: Content: '08000000'
; CHECK-NEXT: - Type: CUSTOM
; CHECK-NEXT: Name: linking
; CHECK-NEXT: SymbolTable:
; CHECK: SymbolTable:
; CHECK-NEXT: - Index: 0
; CHECK-NEXT: Kind: DATA
; CHECK-NEXT: Name: global0

View File

@ -110,6 +110,7 @@ declare void @func3()
; CHECK-NEXT: Content: '01040000'
; CHECK-NEXT: - Type: CUSTOM
; CHECK-NEXT: Name: linking
; CHECK-NEXT: Version: 1
; CHECK-NEXT: SymbolTable:
; CHECK-NEXT: - Index: 0
; CHECK-NEXT: Kind: FUNCTION

View File

@ -44,6 +44,7 @@ target triple = "wasm32-unknown-unknown-wasm"
; CHECK-NEXT: Content: '06000000'
; CHECK-NEXT: - Type: CUSTOM
; CHECK-NEXT: Name: linking
; CHECK-NEXT: Version: 1
; CHECK-NEXT: SymbolTable:
; CHECK-NEXT: - Index: 0
; CHECK-NEXT: Kind: DATA

View File

@ -14,9 +14,7 @@ entry:
ret void
}
; CHECK: - Type: CUSTOM
; CHECK-NEXT: Name: linking
; CHECK-NEXT: SymbolTable:
; CHECK: SymbolTable:
; CHECK-NEXT: - Index: 0
; CHECK-NEXT: Kind: FUNCTION
; CHECK-NEXT: Name: defaultVis

View File

@ -138,6 +138,7 @@ entry:
; CHECK-NEXT: Content: '01000000'
; CHECK-NEXT: - Type: CUSTOM
; CHECK-NEXT: Name: linking
; CHECK-NEXT: Version: 1
; CHECK-NEXT: SymbolTable:
; CHECK-NEXT: - Index: 0
; CHECK-NEXT: Kind: FUNCTION

View File

@ -20,9 +20,7 @@ entry:
; CHECK-NEXT: Field: __indirect_function_table
; CHECK: - Type: CUSTOM
; CHECK-NEXT: Name: linking
; CHECK-NEXT: SymbolTable:
; CHECK: SymbolTable:
; CHECK-NEXT: - Index: 0
; CHECK-NEXT: Kind: FUNCTION
; CHECK-NEXT: Name: weak_function

View File

@ -651,6 +651,7 @@ WASM-NEXT: FileHeader:
WASM-NEXT: Version: 0x00000001
WASM: - Type: CUSTOM
WASM-NEXT: Name: linking
WASM-NEXT: Version: 1
WASM-NEXT: SymbolTable:
WASM-NEXT: - Index: 0
WASM-NEXT: Kind: FUNCTION

View File

@ -0,0 +1,11 @@
# RUN: yaml2obj %s | not llvm-objdump -h - 2>&1 | FileCheck %s
!WASM
FileHeader:
Version: 0x00000001
Sections:
- Type: CUSTOM
Name: linking
Version: 0
# CHECK: {{.*}}: Unexpected metadata version: 0 (Expected: 1)

View File

@ -39,6 +39,7 @@ Sections:
Body: 108180808000210020000F0B
- Type: CUSTOM
Name: linking
Version: 1
SymbolTable:
- Index: 0
Kind: FUNCTION

View File

@ -24,6 +24,7 @@ Sections:
Addend: -6
- Type: CUSTOM
Name: linking
Version: 1
SymbolTable:
- Index: 0
Kind: DATA

View File

@ -13,6 +13,7 @@ Sections:
GlobalMutable: false
- Type: CUSTOM
Name: linking
Version: 1
SymbolTable:
- Index: 0
Kind: GLOBAL

View File

@ -29,6 +29,7 @@ Sections:
Content: '11110000'
- Type: CUSTOM
Name: linking
Version: 1
SymbolTable:
- Index: 0
Kind: FUNCTION
@ -48,9 +49,7 @@ Sections:
- Priority: 1
Symbol: 0
...
# CHECK: - Type: CUSTOM
# CHECK-NEXT: Name: linking
# CHECK-NEXT: SymbolTable:
# CHECK: SymbolTable:
# CHECK-NEXT: - Index: 0
# CHECK-NEXT: Kind: FUNCTION
# CHECK-NEXT: Name: bar

View File

@ -36,6 +36,7 @@ Sections:
Body: 00
- Type: CUSTOM
Name: linking
Version: 1
SymbolTable:
- Index: 0
Kind: FUNCTION

View File

@ -37,6 +37,7 @@ Sections:
Content: '616263'
- Type: CUSTOM
Name: linking
Version: 1
SymbolTable:
- Index: 0
Kind: FUNCTION

View File

@ -25,6 +25,7 @@ Sections:
GlobalMutable: false
- Type: CUSTOM
Name: linking
Version: 1
SymbolTable:
- Index: 0
Kind: FUNCTION

View File

@ -43,6 +43,7 @@ Sections:
Content: '616263'
- Type: CUSTOM
Name: linking
Version: 1
SymbolTable:
- Index: 0
Kind: DATA

View File

@ -7,7 +7,7 @@
# CHECK-NEXT: 2 FUNCTION 00000002 0000000000000000
# CHECK-NEXT: 3 CODE 00000019 0000000000000000 TEXT
# CHECK-NEXT: 4 DATA 0000001c 0000000000000000 DATA
# CHECK-NEXT: 5 linking 0000004d 0000000000000000
# CHECK-NEXT: 5 linking 00000051 0000000000000000
# CHECK-NEXT: 6 reloc.CODE 0000000c 0000000000000000
# RUN: llvm-objdump -p %p/Inputs/trivial.obj.wasm | FileCheck %s -check-prefix CHECK-HEADER

View File

@ -528,14 +528,14 @@ WASM-NEXT: ]
WASM-NEXT: }
WASM-NEXT: Section {
WASM-NEXT: Type: CUSTOM (0x0)
WASM-NEXT: Size: 57
WASM-NEXT: Size: 61
WASM-NEXT: Offset: 191
WASM-NEXT: Name: linking
WASM-NEXT: }
WASM-NEXT: Section {
WASM-NEXT: Type: CUSTOM (0x0)
WASM-NEXT: Size: 12
WASM-NEXT: Offset: 262
WASM-NEXT: Offset: 266
WASM-NEXT: Name: reloc.CODE
WASM-NEXT: }
WASM-NEXT: ]

View File

@ -62,6 +62,8 @@ std::unique_ptr<WasmYAML::CustomSection> WasmDumper::dumpCustomSection(const Was
CustomSec = std::move(NameSec);
} else if (WasmSec.Name == "linking") {
std::unique_ptr<WasmYAML::LinkingSection> LinkingSec = make_unique<WasmYAML::LinkingSection>();
LinkingSec->Version = Obj.linkingData().Version;
ArrayRef<StringRef> Comdats = Obj.linkingData().Comdats;
for (StringRef ComdatName : Comdats)
LinkingSec->Comdats.emplace_back(WasmYAML::Comdat{ComdatName, {}});
@ -71,6 +73,7 @@ std::unique_ptr<WasmYAML::CustomSection> WasmDumper::dumpCustomSection(const Was
WasmYAML::ComdatEntry{wasm::WASM_COMDAT_FUNCTION, Func.Index});
}
}
uint32_t SegmentIndex = 0;
for (const object::WasmSegment &Segment : Obj.dataSegments()) {
if (!Segment.Data.Name.empty()) {
@ -87,6 +90,7 @@ std::unique_ptr<WasmYAML::CustomSection> WasmDumper::dumpCustomSection(const Was
}
SegmentIndex++;
}
uint32_t SymbolIndex = 0;
for (const wasm::WasmSymbolInfo &Symbol : Obj.linkingData().SymbolTable) {
WasmYAML::SymbolInfo Info;
@ -105,10 +109,12 @@ std::unique_ptr<WasmYAML::CustomSection> WasmDumper::dumpCustomSection(const Was
}
LinkingSec->SymbolTable.emplace_back(Info);
}
for (const wasm::WasmInitFunc &Func : Obj.linkingData().InitFunctions) {
WasmYAML::InitFunction F{Func.Priority, Func.Symbol};
LinkingSec->InitFunctions.emplace_back(F);
}
CustomSec = std::move(LinkingSec);
} else {
CustomSec = make_unique<WasmYAML::CustomSection>(WasmSec.Name);

View File

@ -134,6 +134,7 @@ public:
int WasmWriter::writeSectionContent(raw_ostream &OS, WasmYAML::LinkingSection &Section) {
writeStringRef(Section.Name, OS);
encodeULEB128(Section.Version, OS);
SubSectionWriter SubSection(OS);