Currently there is no good way to set a physical offset for a section:
- We have the ShOffset that allows to override the sh_offset, but it does not affect the real data written.
- We can use a Filler to create an artificial gap, but it is more like a hack rather than a proper solution for this problem.
This patch adds the Offset property which allows setting physical
offsets for sections.
It also generalizes the code, so that we set sh_offset field in one place.
FTR, after commiting this patch I faced LLD test failture and fixed it:
https://github.com/llvm/llvm-project/commit/969c63a2ecfb536062ff2174645abe31e4036067
(it started to report "section [index 1] has a sh_offset (0x1000000000000000) + sh_size (0x4) that is greater than the file size (0x100000120)" instead of "section sh_addralign is too large" LLD's error)
The issue was related to "AddressAlign: 0x1000000000000000" line in the YAML:
Previously, we had this piece of code which used unsigned, so AddressAlign was truncated.
As a result a little binary with sh_addrallign==0x1000000000000000 was produced.
After this patch, this piece does not exist and currently for that test a 500mb binary with with sh_addrallign==0x1000000000000000 is produced. I.e. it works a bit more correct, but
the binary is is still truncated.
It happens because of ELFState<ELFT>::alignToOffset, which has the following lines:
AlignedOffset and CurrectOffset are uint64_t, but raw_ostream::write_zeros takes unsigned:
Probably we want to change it to take uint64_t.
I do not think there is a way to write a test for it though as the result output would be huge.