-
Notifications
You must be signed in to change notification settings - Fork 153
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
Support parsing loaded elf? #557
Comments
I'm interested in it. This sounds similar to #548. I had thought that this sort of thing wasn't possible for ELF, so it'd be good to see what you have done. |
Cool. Since loaded elfs don't have sections, some API changes might be necessary . Mostly returning Could also work without the changes, by returning Additionally, I believe we should somehow differentiate between parsing a regular and loaded elf. What are your thoughts? |
Returning empty iterators seems ideal. It's what we already do for file formats that don't support some features. Errors should only be if something is actually wrong, not when it's simply not supported. Adding |
So |
Hmm, not sure I like changing those APIs either. How about try to do it without API changes and we can see what it looks like. |
Sure I'll have some time to work on this on the weekend. |
I have an update, When parsing the dynamic table, reading an entry that contains an address (for example
I believe it has something to do with android having a custom dynamic linker than regular Linux. I'm wondering what would be the correct way to handle this. Perhaps, since this is only useful when a process is analyzing itself/other processes on the same system, it would make sense to use some kind of cfg depending on the compiled target, and add a workaround for all Android targets. Or some kind of heuristic on the returned value, i.e. if it exceeds the size of I'm not sure if there's a sure way to know :/ |
This may be getting into the kind of reason why I thought this wasn't possible for ELF. If you're relying on undocumented internals of dynamic linkers, then that's an area that I'm not sure I want to support in the high level API of this crate. It may be better to adapt the low level API to suit your needs instead. Can you provide code to show how you are using this? |
You can find a very rough draft here It successfully finds all the relevant dynamic sections. It's useful for me when trying to parse the dynamic relocations and PLT of another .so loaded into my process, I can find its base address using This allows me to get a slice that encapsulates the entire elf. |
Thanks.
Are you able to share the code for that too, and ideally an example of the sort of thing you want to do with the resulting parsed file. We'll need to have a test of some kind in this crate. Getting back to your question in #557 (comment), my feeling is that the ideal solution would result in the compiled executable including code that only supports the compiled target, and thus can work reliably instead of using heuristics. If I were to write this code myself, instead of using the higher level API (things like |
I'll see what I can share
Yeah that's what I thought as well, use cfgs that use offsets when compiling an Android target, and cfgs that use absolute addresses on Linux targets, and disable this entirely on untested targets
That's basically what I already have implemented, unfortunately I can't share that code as is. I was hoping to implement some of that functionality directly into this library for easier access for everyone. |
I think adding code in the lower level API that is similar to For your use case, are you doing things that are specific to ELF? If we added |
I'm not familiar enough with PE, but from a short search it seems like PEs IAT is very similar to ELFs PLT, and can be modified to the same effect. I imagine the dynamic relocations abstraction gives access to the same information. So I think there's value to be had in the abstraction. Although improving the low level access code will make the |
dl_iterate_phdr returns a program header for every loaded DSO. According to the man page you have to calculate absolute addresses of segments using |
I'm getting absolute addresses in both cases: Linux
Android:
also 32bit android:
|
So I decided to start slow, added some lower level logic for parsing the dynamic segment. This should work both if the segment was found using This also works with regular elf files (not loaded). let me know what you guys think. |
Parsing the dynamic segment is as easy as calling Edit: Just saw your PR. Looks like you were thinking about a high level parsing interface, while I was thinking about a low level one. A high level one is of course nicer for most use cases. |
Yeah, by low level I meant it was lower level than parsing the entire elf from scratch. It's probably closer to high level than low level though 😅 |
Hello,
I have a use case where I want to parse an elf that's already loaded into my processes memory, as the elf is loaded it's missing sections - which the current elf parser is heavily reliant upon.
Some use could still be had from parsing the dynamic segment (PT_DYNAMIC) to find dynamic symbols, relocations, etc...
I have something similar implemented, using object's type system, but I would like to add that as a built-in feature of this project.
Is this something that could interest you?
Thank you!
The text was updated successfully, but these errors were encountered: