[llvm] c9dd1cc - [Objcopy][Wasm] Allow selecting known sections by name

Derek Schuff via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 6 13:54:48 PDT 2022


Author: Derek Schuff
Date: 2022-06-06T13:54:01-07:00
New Revision: c9dd1cc6f0536c6547f322107cd27cd953913f5c

URL: https://github.com/llvm/llvm-project/commit/c9dd1cc6f0536c6547f322107cd27cd953913f5c
DIFF: https://github.com/llvm/llvm-project/commit/c9dd1cc6f0536c6547f322107cd27cd953913f5c.diff

LOG: [Objcopy][Wasm] Allow selecting known sections by name

Currently, only custom sections can be selected by operations that use section
names, because only custom sections have explicit names (whereas known sections
have names defined by the spec and only use their indices in the binary format).
This CL makes objdopy use the spec-defined names for these sections, allowing
them to be used in operations such as dumping and removal.

This is a prerequisite for fixing
https://github.com/emscripten-core/emscripten/issues/13084

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

Added: 
    

Modified: 
    llvm/lib/ObjCopy/wasm/WasmReader.cpp
    llvm/test/tools/llvm-objcopy/wasm/dump-section.test
    llvm/test/tools/llvm-objcopy/wasm/only-keep-debug.test
    llvm/test/tools/llvm-objcopy/wasm/remove-section.test

Removed: 
    


################################################################################
diff  --git a/llvm/lib/ObjCopy/wasm/WasmReader.cpp b/llvm/lib/ObjCopy/wasm/WasmReader.cpp
index b998571472656..3725c95626cf9 100644
--- a/llvm/lib/ObjCopy/wasm/WasmReader.cpp
+++ b/llvm/lib/ObjCopy/wasm/WasmReader.cpp
@@ -24,6 +24,15 @@ Expected<std::unique_ptr<Object>> Reader::create() const {
     const WasmSection &WS = WasmObj.getWasmSection(Sec);
     Obj->Sections.push_back(
         {static_cast<uint8_t>(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
index 3afcac27f5972..983a581e03fe2 100644
--- a/llvm/test/tools/llvm-objcopy/wasm/dump-section.test
+++ b/llvm/test/tools/llvm-objcopy/wasm/dump-section.test
@@ -13,6 +13,14 @@
 # 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
index 5db95a10e44b1..6f811371e9623 100644
--- 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
index 211b9565f8cc7..dced8d12d62cd 100644
--- 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


        


More information about the llvm-commits mailing list