[WebAssembly] MC: Fix for outputing wasm object to /dev/null
authorSam Clegg <sbc@chromium.org>
Thu, 31 Jan 2019 22:38:22 +0000 (22:38 +0000)
committerSam Clegg <sbc@chromium.org>
Thu, 31 Jan 2019 22:38:22 +0000 (22:38 +0000)
Subscribers: dschuff, jgravelle-google, aheejin, sunfish, llvm-commits

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

llvm-svn: 352806

llvm/lib/MC/WasmObjectWriter.cpp
llvm/test/MC/WebAssembly/null-output.s [new file with mode: 0644]

index d0ffe7c..83ac9a4 100644 (file)
@@ -398,7 +398,13 @@ void WasmObjectWriter::startCustomSection(SectionBookkeeping &Section,
 // Now that the section is complete and we know how big it is, patch up the
 // section size field at the start of the section.
 void WasmObjectWriter::endSection(SectionBookkeeping &Section) {
-  uint64_t Size = W.OS.tell() - Section.PayloadOffset;
+  uint64_t Size = W.OS.tell();
+  // /dev/null doesn't support seek/tell and can report offset of 0.
+  // Simply skip this patching in that case.
+  if (!Size)
+    return;
+
+  Size -= Section.PayloadOffset;
   if (uint32_t(Size) != Size)
     report_fatal_error("section size does not fit in a uint32_t");
 
diff --git a/llvm/test/MC/WebAssembly/null-output.s b/llvm/test/MC/WebAssembly/null-output.s
new file mode 100644 (file)
index 0000000..a25d095
--- /dev/null
@@ -0,0 +1,10 @@
+# RUN: llvm-mc -triple=wasm32-unknown-unknown -filetype=obj -o /dev/null < %s
+
+    .text
+    .section .text.main,"",@
+    .type    main,@function
+main:
+    .functype   main (i32, i32) -> (i32)
+    end_function
+.Lfunc_end0:
+    .size main, .Lfunc_end0-main