[llvm-objcopy] [COFF] Always set PointerToRawData when writing a COFF file
authorMartin Storsjö <martin@martin.st>
Mon, 28 Nov 2022 10:36:36 +0000 (12:36 +0200)
committerMartin Storsjö <martin@martin.st>
Mon, 28 Nov 2022 20:40:00 +0000 (22:40 +0200)
commit30d5b755ea4f07ca014ed7e89b40054fb2b29eda
tree0db83f2738782a50ffaba07153ed996706a5969f
parent94f73fd6f834b3c67dc29249f54931c54dbe14c7
[llvm-objcopy] [COFF] Always set PointerToRawData when writing a COFF file

If we don't want to set PointerToRawData, for an empty section,
we do must set it to zero explicitly. Some object file generators
do set it to zero for empty sections, while others set a nonzero
value pointing at the end of the previous section.

If the value was nonzero on input, we need to update it - either
setting it to zero, or to a valid offset in the output file (not
out of bounds)

This fixes https://github.com/mstorsjo/llvm-mingw/issues/313.

Testing this is tricky, because we can't use yaml2obj, since that
doesn't produce object files with nonzero PointerToRawData for
empty sections. We can use llvm-mc to assemble a small file
(assuming that LLVM's MC layer keeps this behaviour), or bundle
a small binary object file. I opted for using llvm-mc for now here
(with a test that it actually does keep this property), but I don't
mind changing it to a canned object file to make the test less brittle.

Differential Revision: https://reviews.llvm.org/D138783
llvm/lib/ObjCopy/COFF/COFFWriter.cpp
llvm/test/tools/llvm-objcopy/COFF/empty-sections.s [new file with mode: 0644]