-
-
Notifications
You must be signed in to change notification settings - Fork 623
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding section to ARM ELF causes problem in file size #1085
Comments
Looking deeper into the library, I started compiling and debugging it to find out what really happens. Turns out that the PHDR is not found because in my ELFs there is not a specific segment with type PHDR. This means
|
Hi @Miniman2718 Sorry for the late reply. Actually the increased size is intended and if you can find an alternative that do not require to expand the
Because the kernel/loader is doing this.
Because this is what matter for the loader. I close the issue but feel free to re-open it if you find a better approach or have suggestions to fix it. |
Hi @romainthomas, thanks for the reply. However some points are still unclear to me.
Which kernel/loader are you referring to? I'm working in ARM embedded and I have no problem in loading and executing ELF files with no PHDR segment. I also used the
I cannot really understand what you mean by this. My question is about the relationship between virtual addresses and file offset. Line 2763 in ce3ff3f
last_offset to be unreasonably big. I'm now working on a solution using segment->file_offset() instead of segment->virtual_size() to keep the size of the binary small even if virtual addresses are very big but I'm afraid I'm missing the point of the current approach.
|
Hi @Miniman2718
Linux loader/kernel and yes I agree that it can works without a PHDR segment (in the case of static binary).
Actually the ELF loader requires that Could you attach the binary you are trying to modify? |
I'm not sure I understood what you meant here. Quoting the documentation referred to from the LSB,
which is different from what I think Line 2763 in ce3ff3f
because it was the easiest version to enforce the congruence? I wrote a new function to find the first free offset for which the congruence still holds instead of using virtual addresses, and it is working fine for me while keeping the size of the binary pretty small. May I also ask why PHDR is relocated at every new added segment, even if I open an already edited file? Is it done do avoid resizing problem which could exceed previously reserved space if too many segments are added? Here is the file I'm trying to modify: elf_file.tar.gz |
Yes you are right, I missed the modulus on the right hand side. I took a look at your branch and your update breaks this binary To observe the issue (which can run on a regular Linux x86-64) import lief
elf = lief.ELF.parse("docker-init.elf")
segment = lief.ELF.Segment()
segment.type = lief.ELF.Segment.TYPE.LOAD
segment.content = [1] * 12
elf.add(segment)
elf.write("/tmp/out.bin") With the current implementation in LIEF |
Hello @romainthomas, Line 2805 in 63d9e94
The newly computed offset is used to compute the new virtual address, and since I changed const uint64_t last_offset = virtual_size(); , it isn't safe to use anymore. The new address is in conflict with another LOAD segment.I would guess that the same problem can be found wherever va % pagesize() == offset has been used instead of the modulo congruence, but I didn't have the chance to take a look to other parts of the library.
I fixed it by changing the line in |
Indeed it works and it passes the test suite. I re-open the issue and I'll take a closer look a these updates. Thank you pushing your idea even though I was skeptical :) |
I'm trying to add a new section to an ARM ELF file. The section is added, but the ELF goes from about 300kB to more than 380MB.
Digging a little bit deeper, I noticed that the problems seems to be the
paddr
of the new segment to which the new section is mapped:paddr
is set to be equal to thevaddr
, and that causes the binary writing to create an unexpectedly big ELF file.I noticed that I have this problem only on ARM ELFs, where
vaddr
andpaddr
of all the other segments are not equals. In x86 examples I tried, there is no such problem asvaddr
andpaddr
are the same.Looking inside LIEF code, I noticed that physical_address is set to virtual_address, and I figured it could be the cause of my problem.
Am I missing something?
To Reproduce
Code to reproduce the behavior:
Expected behavior
A new ELF files, which slightly bigger than the original one (and not 380MB starting from 300kB)
Environment:
The text was updated successfully, but these errors were encountered: