Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

martinKupec
Copy link

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.

@v3l0c1r4pt0r
Copy link
Owner

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?

@martinKupec
Copy link
Author

Adding makeelf folder to files contained in this zip:
test.zip
will provide example. Launching patch.sh should work and create patched.elf file.
It is stripped down version of script we are using.

Copy link
Owner

@v3l0c1r4pt0r v3l0c1r4pt0r left a 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
Copy link
Owner

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):
Copy link
Owner

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):
Copy link
Owner

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):
Copy link
Owner

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
Copy link
Owner

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:
Copy link
Owner

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
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants