Skip to content

Commit

Permalink
Merge pull request #118 from JonathonReinhart/116-ld-linux-symlink
Browse files Browse the repository at this point in the history
Correct handling of absolute path symlink in ldd output
  • Loading branch information
JonathonReinhart authored Jan 29, 2020
2 parents b332b83 + 2a6c7c8 commit 2570aef
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
38 changes: 21 additions & 17 deletions staticx/elf.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ def _parse_ldd_output(output):
# linux-vdso.so.1 => (0x00007ffe53551000)
# libc.so.6 => /usr/lib64/libc.so.6 (0x00007f42ac010000)
# /lib64/ld-linux-x86-64.so.2 (0x0000557376e75000)
# or
# /lib64/ld-linux-x86-64.so.2 => /usr/lib64/ld-linux-x86-64.so.2 (0x00007f1de63ac000)
pat = re.compile(r'\t([\w./+-]*) (?:=> ([\w./+-]*) )?\((0x[0-9a-fA-F]*)\)')

for line in output.splitlines():
Expand All @@ -99,26 +101,28 @@ def _parse_ldd_output(output):
break
raise LddError("Unexpected line in ldd output: " + line)
libname = m.group(1)
libpath = m.group(2)
resolved = m.group(2)
baseaddr = int(m.group(3), 16)

if libname.startswith('/'):
# An absolute path here is probably INTERP
# and ldd shouldn't output the => /abs/path part.
if libpath:
raise LddError("Unexpected line in ldd output: " + line)
yield libname
if resolved:
# ldd outupt a resolved symlink, use that
libpath = resolved
elif libname.startswith('/'):
# The library directly referenced an absolute path, use that
libpath = libname
else:
# A short libname should come from a NEEDED tag
# and ldd should include the => /abs/path part.
# If it doesn't, then it's probably linux-vdso*.so
# or linux-gate.so
if not libpath:
# TODO: This check could be removed/relaxed
if libname.startswith('linux-'):
continue
raise LddError("Unexpected line in ldd output: " + line)
yield libpath
# Assume an unresolved non-absolute library name is synthetic
# like linux-vdso*.so or linux-gate.so which should be ignored
# TODO: This check could be removed/relaxed
if not libname.startswith('linux-'):
raise LddError("Unexpected unresolved libname: " + libname)
logging.debug("Ignoring synthetic library: " + libname)
continue

# The library path must be absolute
if not libpath.startswith('/'):
raise LddError("Unexpected non-absolute libpath: " + libpath)
yield libpath


def get_shobj_deps(path, libpath=[]):
Expand Down
17 changes: 17 additions & 0 deletions unittest/test_elf.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,20 @@ def test_debian_10(self):
"/lib64/ld-linux-x86-64.so.2",
]
self._test(output, exp)


def test_arch(self):
# https://github.com/JonathonReinhart/staticx/issues/116
output = (
"\tlinux-vdso.so.1 (0x00007ffc431fc000)\n"
"\tlibdl.so.2 => /usr/lib/libdl.so.2 (0x00007f1de6361000)\n"
"\tlibz.so.1 => /usr/lib/libz.so.1 (0x00007f1de6347000)\n"
"\tlibc.so.6 => /usr/lib/libc.so.6 (0x00007f1de6180000)\n"
"\t/lib64/ld-linux-x86-64.so.2 => /usr/lib64/ld-linux-x86-64.so.2 (0x00007f1de63ac000)\n")
exp = [
"/usr/lib/libdl.so.2",
"/usr/lib/libz.so.1",
"/usr/lib/libc.so.6",
"/usr/lib64/ld-linux-x86-64.so.2",
]
self._test(output, exp)

0 comments on commit 2570aef

Please sign in to comment.