diff --git a/llvm/include/llvm/BinaryFormat/Wasm.h b/llvm/include/llvm/BinaryFormat/Wasm.h --- a/llvm/include/llvm/BinaryFormat/Wasm.h +++ b/llvm/include/llvm/BinaryFormat/Wasm.h @@ -472,6 +472,7 @@ std::string toString(WasmSymbolType type); std::string relocTypetoString(uint32_t type); +llvm::StringRef sectionTypeToString(uint32_t type); bool relocTypeHasAddend(uint32_t type); } // end namespace wasm diff --git a/llvm/lib/BinaryFormat/Wasm.cpp b/llvm/lib/BinaryFormat/Wasm.cpp --- a/llvm/lib/BinaryFormat/Wasm.cpp +++ b/llvm/lib/BinaryFormat/Wasm.cpp @@ -38,6 +38,41 @@ } } +llvm::StringRef llvm::wasm::sectionTypeToString(uint32_t Type) { + switch (Type) { + case WASM_SEC_CUSTOM: + return "CUSTOM"; + case WASM_SEC_TYPE: + return "TYPE"; + case WASM_SEC_IMPORT: + return "IMPORT"; + case WASM_SEC_FUNCTION: + return "FUNCTION"; + case WASM_SEC_TABLE: + return "TABLE"; + case WASM_SEC_MEMORY: + return "MEMORY"; + case WASM_SEC_GLOBAL: + return "GLBOAL"; + case WASM_SEC_EXPORT: + return "EXPORT"; + case WASM_SEC_START: + return "START"; + case WASM_SEC_ELEM: + return "ELEM"; + case WASM_SEC_CODE: + return "CODE"; + case WASM_SEC_DATA: + return "DATA"; + case WASM_SEC_DATACOUNT: + return "DATACOUNT"; + case WASM_SEC_TAG: + return "TAG"; + default: + llvm_unreachable("unknown section type"); + } +} + bool llvm::wasm::relocTypeHasAddend(uint32_t Type) { switch (Type) { case R_WASM_MEMORY_ADDR_LEB: diff --git a/llvm/lib/ObjCopy/wasm/WasmReader.cpp b/llvm/lib/ObjCopy/wasm/WasmReader.cpp --- a/llvm/lib/ObjCopy/wasm/WasmReader.cpp +++ b/llvm/lib/ObjCopy/wasm/WasmReader.cpp @@ -24,6 +24,15 @@ const WasmSection &WS = WasmObj.getWasmSection(Sec); Obj->Sections.push_back( {static_cast(WS.Type), WS.Name, WS.Content}); + // Give known sections standard names to allow them to be selected. + Section &ReaderSec = Obj->Sections.back(); + if (ReaderSec.SectionType > WASM_SEC_CUSTOM && + ReaderSec.SectionType <= WASM_SEC_TAG) + ReaderSec.Name = sectionTypeToString(ReaderSec.SectionType); + // If the section type is CUSTOM, it has a name already. If it's a new type + // of section that we don't explicitly handle here, it will have an empty + // name and objcopy won't be able to select it by name (e.g. for removal + // or dumping) but it will still be valid and able to be copied. } return std::move(Obj); } diff --git a/llvm/test/tools/llvm-objcopy/wasm/dump-section.test b/llvm/test/tools/llvm-objcopy/wasm/dump-section.test --- a/llvm/test/tools/llvm-objcopy/wasm/dump-section.test +++ b/llvm/test/tools/llvm-objcopy/wasm/dump-section.test @@ -13,6 +13,15 @@ # NONEXISTENT: section 'nonexistent' not found # DIROUT: error: {{.*}}/bar': [[MSG]] +## Test dumping the type section (a known section) +# RUN: llvm-objcopy --dump-section=TYPE=%t.sec %t +# RUN: od -t x1 %t.sec | FileCheck %s --check-prefix=TYPESEC + +## Raw contents of the type section. +# TYPESEC: 000000 01 60 01 7f 01 7d +# TYPESEC: 000006 + + ## Check that dumping and removing a section works in the same invocation # RUN: llvm-objcopy --dump-section=producers=%t.sec --remove-section=producers %t %t2 # RUN: od -t x1 %t.sec | FileCheck %s diff --git a/llvm/test/tools/llvm-objcopy/wasm/only-keep-debug.test b/llvm/test/tools/llvm-objcopy/wasm/only-keep-debug.test --- a/llvm/test/tools/llvm-objcopy/wasm/only-keep-debug.test +++ b/llvm/test/tools/llvm-objcopy/wasm/only-keep-debug.test @@ -8,7 +8,13 @@ # RUN: llvm-strip --only-keep-debug --keep-section=foo %t # RUN: obj2yaml %t | FileCheck --implicit-check-not=Name --check-prefix=CHECK --check-prefix=KEEP %s +## Test that keep-section overrides only-keep-debug, even for known sections. +# RUN: yaml2obj %s -o %t +# RUN: llvm-strip --only-keep-debug --keep-section=TYPE %t +# RUN: obj2yaml %t | FileCheck --implicit-check-not=Name --check-prefix=CHECK --check-prefix=KEEPTYPE %s + # CHECK: Sections: +# KEEPTYPE: Type: TYPE # CHECK: - Type: CUSTOM # CHECK-NEXT: Name: .debug_info # CHECK: - Type: CUSTOM diff --git a/llvm/test/tools/llvm-objcopy/wasm/remove-section.test b/llvm/test/tools/llvm-objcopy/wasm/remove-section.test --- a/llvm/test/tools/llvm-objcopy/wasm/remove-section.test +++ b/llvm/test/tools/llvm-objcopy/wasm/remove-section.test @@ -8,6 +8,12 @@ ## Requests to remove nonexistent sections are silently ignored. # RUN: llvm-objcopy --remove-section=nonexistent=%t.sec %t 2>&1 | count 0 +## Remove the type section. +# RUN: llvm-objcopy -R TYPE %t %t3 +# RUN: obj2yaml %t3 | FileCheck --check-prefix=REMOVETYPE --implicit-check-not=TYPE %s +## Check that the producers section is still there. +# REMOVETYPE: producers + --- !WASM FileHeader: Version: 0x00000001