diff options
| author | Derek Schuff <dschuff@chromium.org> | 2023-07-25 14:13:05 -0700 |
|---|---|---|
| committer | Heejin Ahn <aheejin@gmail.com> | 2023-07-27 15:43:51 -0700 |
| commit | 1b21067cf247c62c2442daa7ee2d3915249d1ee2 (patch) | |
| tree | 799411b3f5b269dc5f896fe80b295e8036110558 /llvm/lib/ObjectYAML | |
| parent | edf2e0e075e8562ca6a57a3614fbb2b65077e375 (diff) | |
| download | llvm-1b21067cf247c62c2442daa7ee2d3915249d1ee2.tar.gz llvm-1b21067cf247c62c2442daa7ee2d3915249d1ee2.tar.bz2 llvm-1b21067cf247c62c2442daa7ee2d3915249d1ee2.zip | |
[WebAssembly][Objcopy] Write output section headers identically to inputs
Previously when objcopy generated section headers, it padded the LEB
that encodes the section size out to 5 bytes, matching the behavior of
clang. This is correct, but results in a binary that differs from the
input. This can sometimes have undesirable consequences (e.g. breaking
source maps).
This change makes the object reader remember the size of the LEB
encoding in the section header, so that llvm-objcopy can reproduce it
exactly. For sections not read from an object file (e.g. that
llvm-objcopy is adding itself), pad to 5 bytes.
Reviewed By: jhenderson
Differential Revision: https://reviews.llvm.org/D155535
Diffstat (limited to 'llvm/lib/ObjectYAML')
| -rw-r--r-- | llvm/lib/ObjectYAML/WasmEmitter.cpp | 12 | ||||
| -rw-r--r-- | llvm/lib/ObjectYAML/WasmYAML.cpp | 1 |
2 files changed, 12 insertions, 1 deletions
diff --git a/llvm/lib/ObjectYAML/WasmEmitter.cpp b/llvm/lib/ObjectYAML/WasmEmitter.cpp index 6230312eff7b..9b8fd11f8543 100644 --- a/llvm/lib/ObjectYAML/WasmEmitter.cpp +++ b/llvm/lib/ObjectYAML/WasmEmitter.cpp @@ -646,8 +646,18 @@ bool WasmWriter::writeWasm(raw_ostream &OS) { StringStream.flush(); + unsigned HeaderSecSizeEncodingLen = + Sec->HeaderSecSizeEncodingLen ? *Sec->HeaderSecSizeEncodingLen : 5; + unsigned RequiredLen = getULEB128Size(OutString.size()); + // Wasm spec does not allow LEBs larger than 5 bytes + assert(RequiredLen <= 5); + if (HeaderSecSizeEncodingLen < RequiredLen) { + reportError("section header length can't be encoded in a LEB of size " + + Twine(HeaderSecSizeEncodingLen)); + return false; + } // Write the section size followed by the content - encodeULEB128(OutString.size(), OS); + encodeULEB128(OutString.size(), OS, HeaderSecSizeEncodingLen); OS << OutString; } diff --git a/llvm/lib/ObjectYAML/WasmYAML.cpp b/llvm/lib/ObjectYAML/WasmYAML.cpp index 7ca422487df2..ef47766a2394 100644 --- a/llvm/lib/ObjectYAML/WasmYAML.cpp +++ b/llvm/lib/ObjectYAML/WasmYAML.cpp @@ -45,6 +45,7 @@ void MappingTraits<WasmYAML::Object>::mapping(IO &IO, static void commonSectionMapping(IO &IO, WasmYAML::Section &Section) { IO.mapRequired("Type", Section.Type); IO.mapOptional("Relocations", Section.Relocations); + IO.mapOptional("HeaderSecSizeEncodingLen", Section.HeaderSecSizeEncodingLen); } static void sectionMapping(IO &IO, WasmYAML::DylinkSection &Section) { |
