Adding a section results in shifted sections by 0x1000
Hi, Adding a section to a simple hello world binary causes a shift of all sections by 0x1000. Our question is why is this happening? Is this intended? Looking into the LIEF code I didn't find anything obvious. Our problem with this is that it invalidates debug symbols which are stored in a different file...
To Reproduce import lief target = lief.parse("sample.bin") section = lief.ELF.Section() section.name = "nsec" section.type = lief.ELF.SECTION_TYPES.PROGBITS section.alignment = 8 section.flags = 6 section.content = [0] * 4096 target.add(section, True)
These results in the moved sections from: nth paddr size vaddr vsize perm name ――――――――――――――――――――――――――――――――――――――――――――――――― ... 9 0x00000410 0xc0 0x00000410 0xc0 -r-- .rela.dyn 10 0x000004d0 0x18 0x000004d0 0x18 -r-- .rela.plt 11 0x000004e8 0x17 0x000004e8 0x17 -r-x .init 12 0x00000500 0x20 0x00000500 0x20 -r-x .plt 13 0x00000520 0x8 0x00000520 0x8 -r-x .plt.got 14 0x00000530 0x182 0x00000530 0x182 -r-x .text ... to ... 9 0x00001410 0xc0 0x00001410 0xc0 -r-- .rela.dyn 10 0x000014d0 0x18 0x000014d0 0x18 -r-- .rela.plt 11 0x000014e8 0x17 0x000014e8 0x17 -r-x .init 12 0x00001500 0x20 0x00001500 0x20 -r-x .plt 13 0x00001520 0x8 0x00001520 0x8 -r-x .plt.got 14 0x00001530 0x182 0x00001530 0x182 -r-x .text ... 27 0x00003000 0x1000 0x00403000 0x1000 -r-x nsec ... I understand that space is needed for the extra program header entry, but the virtual addresses can stay the same right?
Environment (please complete the following information): lief==0.11.0 Ubuntu 18.04 Target ELF
Hello @fbures
Since in your script you add a loaded section, it implicitly adds a segment. Adding a segment causes the extension of the phdr table which requires to shift the binary by a page's size.