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

Introduce framework for various link strategies #3

Merged
merged 3 commits into from
Jan 3, 2022
Merged

Conversation

fzakaria
Copy link
Owner

@fzakaria fzakaria commented Dec 24, 2021

  • Add some wiring and framework to support various link strategies
    (unsure how pythonic I am being...)
  • Moved things to a new elf.py file
  • Added a new CLI optional argument to select strategy (native vs.
    virtual)
./result/bin/shrinkwrap /usr/bin/sed --link-strategy native./sed_stamped --help
Usage: ./sed_stamped [OPTION]... {script-only-if-no-other-script} [input-file]...

  -n, --quiet, --silent
ldd ./sed_stamped
	linux-vdso.so.1 (0x00007ffdabedb000)
	/lib/x86_64-linux-gnu/libnss_cache.so.2 (0x00007f999aef7000)
	/lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f999aed6000)
	/lib/x86_64-linux-gnu/libdl.so.2 (0x00007f999aed0000)
	/lib/x86_64-linux-gnu/libpcre2-8.so.0 (0x00007f999ae38000)
	/lib/x86_64-linux-gnu/libc.so.6 (0x00007f999ac73000)
	/lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f999ac47000)
	/lib/x86_64-linux-gnu/libacl.so.1 (0x00007f999ac3a000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f999afa3000)
readelf -a ./sed_stamped| grep "Version needs section '.gnu.version" -A 10
Version needs section '.gnu.version_r' contains 3 entries:
 Addr: 0x0000000000003860  Offset: 0x003860  Link: 6 (.dynstr)
  000000: Version: 1  File: /lib/x86_64-linux-gnu/libacl.so.1  Cnt: 1
  0x0010:   Name: ACL_1.0  Flags: none  Version: 5
  0x0020: Version: 1  File: /lib/x86_64-linux-gnu/libselinux.so.1  Cnt: 1
  0x0030:   Name: LIBSELINUX_1.0  Flags: none  Version: 4
  0x0040: Version: 1  File: /lib/x86_64-linux-gnu/libc.so.6  Cnt: 6
  0x0050:   Name: GLIBC_2.14  Flags: none  Version: 9
  0x0060:   Name: GLIBC_2.7  Flags: none  Version: 8
  0x0070:   Name: GLIBC_2.4  Flags: none  Version: 7
  0x0080:   Name: GLIBC_2.3.4  Flags: none  Version: 6

- Add some wiring and framework to support various link strategies
(unsure how pthonic I am being...)

- Moved things to a new elf.py file

- Added a new CLI optional argument to select strategy (native vs.
virtual)
class NativeLinkStrategy(LinkStrategy):
"""Uses the native interpreter in the binary to determine the linking"""

def explore(self, binary: lief.Binary, filename: str) -> Dict[str, str]:
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kind of annoying that the binary doesn't give the original filename and just the name portion :/
Could move a bunch of the lief usage into an ELF friendly class maybe in the future.

@fzakaria
Copy link
Owner Author

CC @trws -- just the framework for it.
Going to try and read what @haampie is doing in libtree and replicate it in python.
(Was browsing V2 source code of his which was in C++ hoping it was easier to read 🥲 )

Add a native strategy similar to that of `libtree`.
There are some gaps at the moment such as skipping libc.
When the NEEDED is updated to be absolute, the VERNEEDED
(whatever that is) needs to be updated as well for glibc
otherwise they do not resolve
Comment on lines +44 to +50
# we need to update the VERNEED entries now to match
verneeded = binary.symbols_version_requirement
for verneed in verneeded:
if verneed.name in resolution:
# we want to map the possible shortname soname
# to the absolute one we generate
verneed.name = resolution.get(verneed.name)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@trws lief makes this very easy although finding that I had to this was not ;)

Comment on lines +39 to +42
# TODO: Figure out why `--list` and `ldd` produce different outcomes
# specifically for the interpreter.
# https://gist.github.com/fzakaria/3dc42a039401598d8e0fdbc57f5e7eae
for line in resolution:
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@trws let me know if you have some knowledge here of this.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a new one on me. Maybe the loader just prints itself instead of looking it up?

@fzakaria fzakaria merged commit 3fcf6a1 into main Jan 3, 2022
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