-
Notifications
You must be signed in to change notification settings - Fork 42
conversion from ElfFile to ObjectFile #141
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
base: master
Are you sure you want to change the base?
Conversation
|
this is totally not ready (it breaks existing unit tests, there are no unit tests for this conversion ...) |
|
ElfFile to ObjectFile conversion works now. (all tests pass) |
3d70415 to
f453769
Compare
ppci/binutils/objectfile.py
Outdated
| def load(input_file): | ||
| """Load object file from file""" | ||
| return deserialize(json.load(input_file)) | ||
| # if is_json(input_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.
This commented code can go.
ppci/binutils/objectfile.py
Outdated
|
|
||
| start = input_file.read(4) | ||
| input_file.seek(0) | ||
| if b"ELF" in start: |
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 would rather not include ELF file specific logic in this objectfile class. If we want to make the switch between ELF file and json object file, it should be outside the objectfile.load function.
ppci/binutils/objectfile.py
Outdated
| start = input_file.read(4) | ||
| input_file.seek(0) | ||
| if b"ELF" in start: | ||
| return deserialize(ElfFile.load(input_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.
Allthough it is a nifty idea to re-use the deserialize function together with getitem calls, I would rather refactor this code such that there is a function introduced called: elf_to_object, which takes an ELF file, and creates an objectfile instance of this elf 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.
the elf directory has a read file for reading elf files (currently just a wrapper for the load function) we could put this function into there into there
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 would also add, that the refactoring would also need to change all the places like the linker and objdump/copy ....
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.
Why would we need to change the objdump / copy code? If we first convert the ELF file into generic objectfile, then objdump can operate on the ObjectFile class.
|
|
||
| @classmethod | ||
| def _missing_(cls, value): | ||
| return cls.NULL |
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.
Is using NULL when we encounter an unknown section type ok? It would also be fine to me to raise an error in this case.
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.
it broke when loading bash, because it uses special gnu section types which we can't map to this enum
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.
Can we add the gnu section types to the enum? Or are there too many?
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 don't know i can't seam to find any documentation (like official ones) about these.
ppci/format/elf/headers.py
Outdated
|
|
||
| if bits == 64: | ||
| self.RelocationTableEntryWA = header.mk_header( | ||
| "RelocationTableEntryWA", |
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.
WA == with addend? Could we think of better naming here? I see in ELF terminology there is REL and RELA, maybe we can turn the two options in RelTableEntry and RelaTableEntry, or even RelEntry and RelaEntry? What do you think?
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.
that's a good idea the WA was just a quick fix.
ppci/format/elf/file.py
Outdated
| def __init__(self, header): | ||
| self.header = header | ||
|
|
||
| def __getitem__(self, key): |
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.
Rather than making the conversion here to objectfile, I would make this conversion in a separate function.
| def read_symbol_table(self, f): | ||
| return [s.header for s in self.symbole_table] | ||
|
|
||
| def get_str(self, offset, *strtab): |
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.
Hmm, I see the purpose of passing the strtab along here. Maybe we could introduce a StringTable class, which has the get_str method? Then we can modify read_strtab such that it is cached, using functools.cache decorate. read_strtab should then return a new StringTable class.
|
The refactoring seams to be done now. |
d612a21 to
246d862
Compare
This enables the conversion from ElfFile to ObjectFile which then can be used to link against elf files.