[ELF] --oformat=binary: use LMA to compute file offsets
authorFangrui Song <i@maskray.me>
Wed, 5 Aug 2020 16:04:56 +0000 (09:04 -0700)
committerFangrui Song <i@maskray.me>
Wed, 5 Aug 2020 16:10:01 +0000 (09:10 -0700)
commitacb66b9111ba793509b5468a58107108317b7cf5
tree4bca8192074a53c28f77a2a4b1e8e10552a3ca08
parent98d91aecb26a51225242332e73ed454c0f6cac5e
[ELF] --oformat=binary: use LMA to compute file offsets

--oformat=binary is rare (used in a few places in FreeBSD, see `stand/i386/mbr/Makefile` `LDFLAGS_BIN`)
The result should be identical to a normal output transformed by `objcopy -O binary`.

The current implementation ignores addresses and lays out sections by
respecting output section alignments. It can fail when an output section
address is specified, e.g. `.rodata ALIGN(16) :` (PR33651).

Fix PR33651 by respecting LMA. The code is similar to
`tools/llvm-objcop/ELF/Object.cpp` BinaryWriter::finalize after D71035 and D79229.
Unforunately for an output section without PT_LOAD, we assume its LMA is equal
to its VMA. So the result is still incorrect when an output section LMA
(`AT(...)`) is specified

Also drop `alignTo(off, config->wordsize)`. GNU ld does not round up the file size.

Differential Revision: https://reviews.llvm.org/D85086
lld/ELF/Writer.cpp
lld/test/ELF/oformat-binary-ttext.s
lld/test/ELF/oformat-binary.s