[llvm-objcopy] Fix --section-add when section contain empty bytes
authorGuilhem <guilhem@trail.gg>
Sat, 3 Dec 2022 00:48:27 +0000 (16:48 -0800)
committerDerek Schuff <dschuff@chromium.org>
Sat, 3 Dec 2022 00:59:57 +0000 (16:59 -0800)
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

# With wasm-objdump -h we can see that the name section is not totally copied in the new wasm file (if it actually contain empty bytes)

```

Reviewed By: dschuff

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

llvm/lib/ObjCopy/wasm/WasmObjcopy.cpp
llvm/test/tools/llvm-objcopy/wasm/add-section.test

index 6877cd6..19f10bf 100644 (file)
@@ -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());
index 2f32eac..46bf4e9 100644 (file)
 # 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 -en '\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