Skip to content

Instantly share code, notes, and snippets.

@Learath2
Created April 14, 2020 20:27
Show Gist options
  • Save Learath2/c77c9b32e8ea6de86d72928404617525 to your computer and use it in GitHub Desktop.
Save Learath2/c77c9b32e8ea6de86d72928404617525 to your computer and use it in GitHub Desktop.
?
import subprocess
import re
def splitCmds(lines):
cmds = []
current = []
for line in lines:
if re.fullmatch(r"Load command \d+", line):
cmds.append(current)
current = []
continue
current.append(line.strip())
return cmds[1:]
def main():
import argparse
p = argparse.ArgumentParser(description="Strip LC_RPATH commands from executable")
p.add_argument('otool', help="Path to otool")
p.add_argument('install_name_tool', help="Path to install_name_tool")
p.add_argument('executable', metavar="EXECUTABLE", help="The executable to strip")
args = p.parse_args()
otool = args.otool
install_name_tool = args.install_name_tool
executable = args.executable
cmds = splitCmds(subprocess.check_output([otool, "-l", executable]).decode().splitlines())
lc_rpath_cmds = [cmd for cmd in cmds if cmd[0] == "cmd LC_RPATH"]
path_regex = re.compile("path (.*) \(offset \d+\)")
rpaths = [k[0] for k in [[path_regex.match(part).group(1) for part in cmd if path_regex.fullmatch(part)] for cmd in lc_rpath_cmds]]
print("Found paths:")
for path in rpaths:
print("\t" + path)
subprocess.check_call([install_name_tool, "-delete_rpath", path, executable])
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment