Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | CHRPATH_BIN ?= "chrpath" |
| 2 | PREPROCESS_RELOCATE_DIRS ?= "" |
| 3 | |
Brad Bishop | acc069e | 2019-09-13 06:48:36 -0400 | [diff] [blame] | 4 | def process_file_linux(cmd, fpath, rootdir, baseprefix, tmpdir, d, break_hardlinks = False): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame^] | 5 | import subprocess, oe.qa |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 6 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame^] | 7 | with oe.qa.ELFFile(fpath) as elf: |
| 8 | try: |
| 9 | elf.open() |
| 10 | except oe.qa.NotELFFileError: |
| 11 | return |
| 12 | |
| 13 | try: |
| 14 | out = subprocess.check_output([cmd, "-l", fpath], universal_newlines=True) |
| 15 | except subprocess.CalledProcessError: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 16 | return |
| 17 | |
| 18 | # Handle RUNPATH as well as RPATH |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 19 | out = out.replace("RUNPATH=","RPATH=") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 20 | # Throw away everything other than the rpath list |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 21 | curr_rpath = out.partition("RPATH=")[2] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 22 | #bb.note("Current rpath for %s is %s" % (fpath, curr_rpath.strip())) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 23 | rpaths = curr_rpath.strip().split(":") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 24 | new_rpaths = [] |
| 25 | modified = False |
| 26 | for rpath in rpaths: |
| 27 | # If rpath is already dynamic copy it to new_rpath and continue |
| 28 | if rpath.find("$ORIGIN") != -1: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 29 | new_rpaths.append(rpath) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 30 | continue |
| 31 | rpath = os.path.normpath(rpath) |
| 32 | if baseprefix not in rpath and tmpdir not in rpath: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 33 | # Skip standard search paths |
| 34 | if rpath in ['/lib', '/usr/lib', '/lib64/', '/usr/lib64']: |
| 35 | bb.warn("Skipping RPATH %s as is a standard search path for %s" % (rpath, fpath)) |
| 36 | modified = True |
| 37 | continue |
| 38 | new_rpaths.append(rpath) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 39 | continue |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 40 | new_rpaths.append("$ORIGIN/" + os.path.relpath(rpath, os.path.dirname(fpath.replace(rootdir, "/")))) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 41 | modified = True |
| 42 | |
| 43 | # if we have modified some rpaths call chrpath to update the binary |
| 44 | if modified: |
Brad Bishop | acc069e | 2019-09-13 06:48:36 -0400 | [diff] [blame] | 45 | if break_hardlinks: |
| 46 | bb.utils.break_hardlinks(fpath) |
| 47 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 48 | args = ":".join(new_rpaths) |
| 49 | #bb.note("Setting rpath for %s to %s" %(fpath, args)) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame^] | 50 | try: |
| 51 | subprocess.check_output([cmd, "-r", args, fpath], |
| 52 | stderr=subprocess.PIPE, universal_newlines=True) |
| 53 | except subprocess.CalledProcessError as e: |
| 54 | bb.fatal("chrpath command failed with exit code %d:\n%s\n%s" % (e.returncode, e.stdout, e.stderr)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 55 | |
Brad Bishop | acc069e | 2019-09-13 06:48:36 -0400 | [diff] [blame] | 56 | def process_file_darwin(cmd, fpath, rootdir, baseprefix, tmpdir, d, break_hardlinks = False): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 57 | import subprocess as sub |
| 58 | |
| 59 | p = sub.Popen([d.expand("${HOST_PREFIX}otool"), '-L', fpath],stdout=sub.PIPE,stderr=sub.PIPE) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 60 | out, err = p.communicate() |
| 61 | # If returned successfully, process stdout for results |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 62 | if p.returncode != 0: |
| 63 | return |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 64 | for l in out.split("\n"): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 65 | if "(compatibility" not in l: |
| 66 | continue |
| 67 | rpath = l.partition("(compatibility")[0].strip() |
| 68 | if baseprefix not in rpath: |
| 69 | continue |
| 70 | |
Brad Bishop | acc069e | 2019-09-13 06:48:36 -0400 | [diff] [blame] | 71 | if break_hardlinks: |
| 72 | bb.utils.break_hardlinks(fpath) |
| 73 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 74 | newpath = "@loader_path/" + os.path.relpath(rpath, os.path.dirname(fpath.replace(rootdir, "/"))) |
| 75 | p = sub.Popen([d.expand("${HOST_PREFIX}install_name_tool"), '-change', rpath, newpath, fpath],stdout=sub.PIPE,stderr=sub.PIPE) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 76 | out, err = p.communicate() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 77 | |
Brad Bishop | acc069e | 2019-09-13 06:48:36 -0400 | [diff] [blame] | 78 | def process_dir(rootdir, directory, d, break_hardlinks = False): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame^] | 79 | bb.debug(2, "Checking %s for binaries to process" % directory) |
| 80 | if not os.path.exists(directory): |
| 81 | return |
| 82 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 83 | import stat |
| 84 | |
| 85 | rootdir = os.path.normpath(rootdir) |
| 86 | cmd = d.expand('${CHRPATH_BIN}') |
| 87 | tmpdir = os.path.normpath(d.getVar('TMPDIR', False)) |
| 88 | baseprefix = os.path.normpath(d.expand('${base_prefix}')) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 89 | hostos = d.getVar("HOST_OS") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 90 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 91 | if "linux" in hostos: |
| 92 | process_file = process_file_linux |
| 93 | elif "darwin" in hostos: |
| 94 | process_file = process_file_darwin |
| 95 | else: |
| 96 | # Relocations not supported |
| 97 | return |
| 98 | |
| 99 | dirs = os.listdir(directory) |
| 100 | for file in dirs: |
| 101 | fpath = directory + "/" + file |
| 102 | fpath = os.path.normpath(fpath) |
| 103 | if os.path.islink(fpath): |
| 104 | # Skip symlinks |
| 105 | continue |
| 106 | |
| 107 | if os.path.isdir(fpath): |
Brad Bishop | acc069e | 2019-09-13 06:48:36 -0400 | [diff] [blame] | 108 | process_dir(rootdir, fpath, d, break_hardlinks = break_hardlinks) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 109 | else: |
| 110 | #bb.note("Testing %s for relocatability" % fpath) |
| 111 | |
| 112 | # We need read and write permissions for chrpath, if we don't have |
| 113 | # them then set them temporarily. Take a copy of the files |
| 114 | # permissions so that we can restore them afterwards. |
| 115 | perms = os.stat(fpath)[stat.ST_MODE] |
| 116 | if os.access(fpath, os.W_OK|os.R_OK): |
| 117 | perms = None |
| 118 | else: |
| 119 | # Temporarily make the file writeable so we can chrpath it |
| 120 | os.chmod(fpath, perms|stat.S_IRWXU) |
Brad Bishop | acc069e | 2019-09-13 06:48:36 -0400 | [diff] [blame] | 121 | |
| 122 | process_file(cmd, fpath, rootdir, baseprefix, tmpdir, d, break_hardlinks = break_hardlinks) |
| 123 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 124 | if perms: |
| 125 | os.chmod(fpath, perms) |
| 126 | |
| 127 | def rpath_replace (path, d): |
| 128 | bindirs = d.expand("${bindir} ${sbindir} ${base_sbindir} ${base_bindir} ${libdir} ${base_libdir} ${libexecdir} ${PREPROCESS_RELOCATE_DIRS}").split() |
| 129 | |
| 130 | for bindir in bindirs: |
| 131 | #bb.note ("Processing directory " + bindir) |
| 132 | directory = path + "/" + bindir |
| 133 | process_dir (path, directory, d) |
| 134 | |