-
Notifications
You must be signed in to change notification settings - Fork 28
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
Added handling of program segments and overlapping parts #9
base: master
Are you sure you want to change the base?
Conversation
Thanks for your contribution. Could you send some use case for your changes? Be it a code snippet that generates ELF file handled by your code or just generated ELF, that illustrates this specific case, it depends on what is easier for you. By simply looking at the code, it is hard for me to imagine what its all about. If I understand correctly this is all about situation, where ELF file has, let's say, two sections, where, looking from address space point of view, one section is contained within a bigger one. Am I right? |
Adding makeelf folder to files contained in this zip: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't fully understood the overlapping sections problem, but generally the changes seem to be reasonable and useful. I put some comments about things to improve to keep your code consistent with currently existing one.
@@ -208,12 +208,34 @@ def __bytes__(self): | |||
self.Elf.Ehdr.e_shentsize = 0 | |||
self.Elf.Ehdr.e_shnum = 0 | |||
|
|||
# update section offsets in section headers |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess you meant program headers, as you're changing Phdr
return Elf32(Ehdr, Phdr_a, Shdr_a, sections, little=Ehdr.little), None | ||
return Elf32(Ehdr, Phdr_a, Shdr_a, programs, sections, little=Ehdr.little), None | ||
|
||
def extend_program(self, idx, extension): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, move this function to elf module. elfstruct's purpose is to provide a kind of a model for storing data in ELF file. Any manipulations should be done outside of it.
self.sections[i] = memoryview(program)[first:last] | ||
return program | ||
|
||
def resize_section(self, section, size): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same situation here
@@ -878,6 +900,18 @@ def __bytes__(self): | |||
b = makeelf.utils.bytes_xor(b, aligned) | |||
return b | |||
|
|||
def create_section_from_hdr(blob, programs, phdrs, hdr): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try to document new functions you're adding. It is hard for anyone else to use such functions. This is even more important to other two functions, as there is no single call to them.
# Set all invalid offsets to end of file | ||
for i, Shdr in enumerate(self.Shdr_table): | ||
if Shdr.sh_offset < 0: | ||
Shdr.sh_offset = end_of_file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't do any magic in this file. If for some reason, this kind of stuff is required, please do it in ELF() class.
In here, invalid structures must be treated as perfectly fine.
# sections | ||
for i, Shdr in enumerate(self.Shdr_table): | ||
if len(self.sections[i]) != 0: | ||
if Shdr.sh_offset < 0: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sh_offset of negative value is impossible to serialize. Maybe it should be forbidden on shdr object creation? It is going to result in exception, if just shdr object serialization is performed, so sounds like a better place to prevent such situation.
section_len = len(section) | ||
# Check for all zeros - strip such sections | ||
if next((False for b in section if b != 0x00), True): | ||
Shdr.sh_offset = -1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, now I understand why you are checking sh_offset for negative values. Don't do it that way. Someone might try to serialize Shdr in the meantime and will get exception, because of that. If the goal is to strip a section, just do it here. You have access to section, so it could be assigned to None, empty array or anything that can unambiguously mark it as empty.
This is basic implementation of program segment handling with sections overlaps in mind. It was created for the purpose of injecting device dependent data to firmware file during manufacture process. It might come handy to someone.