[llvm] 91d0618 - [llvm-objcopy] Reland "Fix --add-section when section contain empty bytes"
Derek Schuff via llvm-commits
llvm-commits at lists.llvm.org
Mon Dec 5 18:13:37 PST 2022
Author: Guilhem
Date: 2022-12-05T18:05:22-08:00
New Revision: 91d0618368c0248e200d90b367322f9c1f74e387
URL: https://github.com/llvm/llvm-project/commit/91d0618368c0248e200d90b367322f9c1f74e387
DIFF: https://github.com/llvm/llvm-project/commit/91d0618368c0248e200d90b367322f9c1f74e387.diff
LOG: [llvm-objcopy] Reland "Fix --add-section when section contain empty bytes"
Implicit cast between char* and StringRef when writing sections.
Reproduce:
```
$> llvm-objcopy --dump-section=name=name.data out.wasm
$> llvm-objcopy --remove-section=name out.wasm out_no_name.wasm
$> llvm-objcopy --add-section=name=name.data out_no_name.wasm out_new_name.wasm
```
Reviewed By: dschuff
Differential Revision: https://reviews.llvm.org/D139210
Added:
Modified:
llvm/lib/ObjCopy/wasm/WasmObjcopy.cpp
llvm/test/tools/llvm-objcopy/wasm/add-section.test
Removed:
################################################################################
diff --git a/llvm/lib/ObjCopy/wasm/WasmObjcopy.cpp b/llvm/lib/ObjCopy/wasm/WasmObjcopy.cpp
index 6877cd68bee47..19f10bf4c8180 100644
--- a/llvm/lib/ObjCopy/wasm/WasmObjcopy.cpp
+++ b/llvm/lib/ObjCopy/wasm/WasmObjcopy.cpp
@@ -126,9 +126,11 @@ static Error handleArgs(const CommonConfig &Config, Object &Obj) {
Sec.SectionType = llvm::wasm::WASM_SEC_CUSTOM;
Sec.Name = NewSection.SectionName;
+ llvm::StringRef InputData =
+ llvm::StringRef(NewSection.SectionData->getBufferStart(),
+ NewSection.SectionData->getBufferSize());
std::unique_ptr<MemoryBuffer> BufferCopy = MemoryBuffer::getMemBufferCopy(
- NewSection.SectionData->getBufferStart(),
- NewSection.SectionData->getBufferIdentifier());
+ InputData, NewSection.SectionData->getBufferIdentifier());
Sec.Contents = makeArrayRef<uint8_t>(
reinterpret_cast<const uint8_t *>(BufferCopy->getBufferStart()),
BufferCopy->getBufferSize());
diff --git a/llvm/test/tools/llvm-objcopy/wasm/add-section.test b/llvm/test/tools/llvm-objcopy/wasm/add-section.test
index 2f32eaca0ac46..1a0df092dc7c4 100644
--- a/llvm/test/tools/llvm-objcopy/wasm/add-section.test
+++ b/llvm/test/tools/llvm-objcopy/wasm/add-section.test
@@ -20,6 +20,17 @@
# REPLACE: Name: foo
# REPLACE: Payload: 3132330A
+## Check that raw data bytes can be imported and exported unchanged especially the ones containing empty bytes.
+# RUN: echo -e -n "\x02\x01\x00\x01\x02" > %t6
+# RUN: llvm-objcopy --add-section=bar=%t6 %t %t7
+# RUN: llvm-objcopy --dump-section=bar=%t8 %t7
+# RUN:
diff %t8 %t6
+# RUN: obj2yaml %t7 | FileCheck %s --check-prefix=RAW-DATA
+
+## Check that raw data is well formated in the file as well.
+# RAW-DATA: Name: bar
+# RAW-DATA-NEXT: Payload: '0201000102'
+
--- !WASM
FileHeader:
Version: 0x00000001
More information about the llvm-commits
mailing list