Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 2 | # Copyright OpenEmbedded Contributors |
| 3 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 4 | # SPDX-License-Identifier: GPL-2.0-only |
| 5 | # |
| 6 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 7 | import stat |
| 8 | import mmap |
| 9 | import subprocess |
| 10 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 11 | def runstrip(arg): |
| 12 | # Function to strip a single file, called from split_and_strip_files below |
| 13 | # A working 'file' (one which works on the target architecture) |
| 14 | # |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 15 | # The elftype is a bit pattern (explained in is_elf below) to tell |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 16 | # us what type of file we're processing... |
| 17 | # 4 - executable |
| 18 | # 8 - shared library |
| 19 | # 16 - kernel module |
| 20 | |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 21 | if len(arg) == 3: |
| 22 | (file, elftype, strip) = arg |
| 23 | extra_strip_sections = '' |
| 24 | else: |
| 25 | (file, elftype, strip, extra_strip_sections) = arg |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 26 | |
| 27 | newmode = None |
| 28 | if not os.access(file, os.W_OK) or os.access(file, os.R_OK): |
| 29 | origmode = os.stat(file)[stat.ST_MODE] |
| 30 | newmode = origmode | stat.S_IWRITE | stat.S_IREAD |
| 31 | os.chmod(file, newmode) |
| 32 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 33 | stripcmd = [strip] |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 34 | skip_strip = False |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 35 | # kernel module |
| 36 | if elftype & 16: |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 37 | if is_kernel_module_signed(file): |
| 38 | bb.debug(1, "Skip strip on signed module %s" % file) |
| 39 | skip_strip = True |
| 40 | else: |
| 41 | stripcmd.extend(["--strip-debug", "--remove-section=.comment", |
| 42 | "--remove-section=.note", "--preserve-dates"]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 43 | # .so and shared library |
| 44 | elif ".so" in file and elftype & 8: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 45 | stripcmd.extend(["--remove-section=.comment", "--remove-section=.note", "--strip-unneeded"]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 46 | # shared or executable: |
| 47 | elif elftype & 8 or elftype & 4: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 48 | stripcmd.extend(["--remove-section=.comment", "--remove-section=.note"]) |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 49 | if extra_strip_sections != '': |
| 50 | for section in extra_strip_sections.split(): |
| 51 | stripcmd.extend(["--remove-section=" + section]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 52 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 53 | stripcmd.append(file) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 54 | bb.debug(1, "runstrip: %s" % stripcmd) |
| 55 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 56 | if not skip_strip: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 57 | output = subprocess.check_output(stripcmd, stderr=subprocess.STDOUT) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 58 | |
| 59 | if newmode: |
| 60 | os.chmod(file, origmode) |
| 61 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 62 | # Detect .ko module by searching for "vermagic=" string |
| 63 | def is_kernel_module(path): |
| 64 | with open(path) as f: |
| 65 | return mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ).find(b"vermagic=") >= 0 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 66 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 67 | # Detect if .ko module is signed |
| 68 | def is_kernel_module_signed(path): |
| 69 | with open(path, "rb") as f: |
| 70 | f.seek(-28, 2) |
| 71 | module_tail = f.read() |
| 72 | return "Module signature appended" in "".join(chr(c) for c in bytearray(module_tail)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 73 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 74 | # Return type (bits): |
| 75 | # 0 - not elf |
| 76 | # 1 - ELF |
| 77 | # 2 - stripped |
| 78 | # 4 - executable |
| 79 | # 8 - shared library |
| 80 | # 16 - kernel module |
| 81 | def is_elf(path): |
| 82 | exec_type = 0 |
| 83 | result = subprocess.check_output(["file", "-b", path], stderr=subprocess.STDOUT).decode("utf-8") |
| 84 | |
| 85 | if "ELF" in result: |
| 86 | exec_type |= 1 |
| 87 | if "not stripped" not in result: |
| 88 | exec_type |= 2 |
| 89 | if "executable" in result: |
| 90 | exec_type |= 4 |
| 91 | if "shared" in result: |
| 92 | exec_type |= 8 |
| 93 | if "relocatable" in result: |
| 94 | if path.endswith(".ko") and path.find("/lib/modules/") != -1 and is_kernel_module(path): |
| 95 | exec_type |= 16 |
| 96 | return (path, exec_type) |
| 97 | |
| 98 | def is_static_lib(path): |
| 99 | if path.endswith('.a') and not os.path.islink(path): |
| 100 | with open(path, 'rb') as fh: |
| 101 | # The magic must include the first slash to avoid |
| 102 | # matching golang static libraries |
| 103 | magic = b'!<arch>\x0a/' |
| 104 | start = fh.read(len(magic)) |
| 105 | return start == magic |
| 106 | return False |
| 107 | |
| 108 | def strip_execs(pn, dstdir, strip_cmd, libdir, base_libdir, d, qa_already_stripped=False): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 109 | """ |
| 110 | Strip executable code (like executables, shared libraries) _in_place_ |
| 111 | - Based on sysroot_strip in staging.bbclass |
| 112 | :param dstdir: directory in which to strip files |
| 113 | :param strip_cmd: Strip command (usually ${STRIP}) |
| 114 | :param libdir: ${libdir} - strip .so files in this directory |
| 115 | :param base_libdir: ${base_libdir} - strip .so files in this directory |
| 116 | :param qa_already_stripped: Set to True if already-stripped' in ${INSANE_SKIP} |
| 117 | This is for proper logging and messages only. |
| 118 | """ |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 119 | import stat, errno, oe.path, oe.utils |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 120 | |
| 121 | elffiles = {} |
| 122 | inodes = {} |
| 123 | libdir = os.path.abspath(dstdir + os.sep + libdir) |
| 124 | base_libdir = os.path.abspath(dstdir + os.sep + base_libdir) |
| 125 | exec_mask = stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH |
| 126 | # |
| 127 | # First lets figure out all of the files we may have to process |
| 128 | # |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 129 | checkelf = [] |
| 130 | inodecache = {} |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 131 | for root, dirs, files in os.walk(dstdir): |
| 132 | for f in files: |
| 133 | file = os.path.join(root, f) |
| 134 | |
| 135 | try: |
| 136 | ltarget = oe.path.realpath(file, dstdir, False) |
| 137 | s = os.lstat(ltarget) |
| 138 | except OSError as e: |
| 139 | (err, strerror) = e.args |
| 140 | if err != errno.ENOENT: |
| 141 | raise |
| 142 | # Skip broken symlinks |
| 143 | continue |
| 144 | if not s: |
| 145 | continue |
| 146 | # Check its an excutable |
| 147 | if s[stat.ST_MODE] & exec_mask \ |
| 148 | or ((file.startswith(libdir) or file.startswith(base_libdir)) and ".so" in f) \ |
| 149 | or file.endswith('.ko'): |
| 150 | # If it's a symlink, and points to an ELF file, we capture the readlink target |
| 151 | if os.path.islink(file): |
| 152 | continue |
| 153 | |
| 154 | # It's a file (or hardlink), not a link |
| 155 | # ...but is it ELF, and is it already stripped? |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 156 | checkelf.append(file) |
| 157 | inodecache[file] = s.st_ino |
| 158 | results = oe.utils.multiprocess_launch(is_elf, checkelf, d) |
| 159 | for (file, elf_file) in results: |
| 160 | #elf_file = is_elf(file) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 161 | if elf_file & 1: |
| 162 | if elf_file & 2: |
| 163 | if qa_already_stripped: |
| 164 | bb.note("Skipping file %s from %s for already-stripped QA test" % (file[len(dstdir):], pn)) |
| 165 | else: |
| 166 | bb.warn("File '%s' from %s was already stripped, this will prevent future debugging!" % (file[len(dstdir):], pn)) |
| 167 | continue |
| 168 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 169 | if inodecache[file] in inodes: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 170 | os.unlink(file) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 171 | os.link(inodes[inodecache[file]], file) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 172 | else: |
| 173 | # break hardlinks so that we do not strip the original. |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 174 | inodes[inodecache[file]] = file |
| 175 | bb.utils.break_hardlinks(file) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 176 | elffiles[file] = elf_file |
| 177 | |
| 178 | # |
| 179 | # Now strip them (in parallel) |
| 180 | # |
| 181 | sfiles = [] |
| 182 | for file in elffiles: |
| 183 | elf_file = int(elffiles[file]) |
| 184 | sfiles.append((file, elf_file, strip_cmd)) |
| 185 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 186 | oe.utils.multiprocess_launch(runstrip, sfiles, d) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 187 | |
| 188 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 189 | def file_translate(file): |
| 190 | ft = file.replace("@", "@at@") |
| 191 | ft = ft.replace(" ", "@space@") |
| 192 | ft = ft.replace("\t", "@tab@") |
| 193 | ft = ft.replace("[", "@openbrace@") |
| 194 | ft = ft.replace("]", "@closebrace@") |
| 195 | ft = ft.replace("_", "@underscore@") |
| 196 | return ft |
| 197 | |
| 198 | def filedeprunner(arg): |
| 199 | import re, subprocess, shlex |
| 200 | |
| 201 | (pkg, pkgfiles, rpmdeps, pkgdest) = arg |
| 202 | provides = {} |
| 203 | requires = {} |
| 204 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 205 | file_re = re.compile(r'\s+\d+\s(.*)') |
| 206 | dep_re = re.compile(r'\s+(\S)\s+(.*)') |
| 207 | r = re.compile(r'[<>=]+\s+\S*') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 208 | |
| 209 | def process_deps(pipe, pkg, pkgdest, provides, requires): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 210 | file = None |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 211 | for line in pipe.split("\n"): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 212 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 213 | m = file_re.match(line) |
| 214 | if m: |
| 215 | file = m.group(1) |
| 216 | file = file.replace(pkgdest + "/" + pkg, "") |
| 217 | file = file_translate(file) |
| 218 | continue |
| 219 | |
| 220 | m = dep_re.match(line) |
| 221 | if not m or not file: |
| 222 | continue |
| 223 | |
| 224 | type, dep = m.groups() |
| 225 | |
| 226 | if type == 'R': |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 227 | i = requires |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 228 | elif type == 'P': |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 229 | i = provides |
| 230 | else: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 231 | continue |
| 232 | |
| 233 | if dep.startswith("python("): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 234 | continue |
| 235 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 236 | # Ignore all perl(VMS::...) and perl(Mac::...) dependencies. These |
| 237 | # are typically used conditionally from the Perl code, but are |
| 238 | # generated as unconditional dependencies. |
| 239 | if dep.startswith('perl(VMS::') or dep.startswith('perl(Mac::'): |
| 240 | continue |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 241 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 242 | # Ignore perl dependencies on .pl files. |
| 243 | if dep.startswith('perl(') and dep.endswith('.pl)'): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 244 | continue |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 245 | |
| 246 | # Remove perl versions and perl module versions since they typically |
| 247 | # do not make sense when used as package versions. |
| 248 | if dep.startswith('perl') and r.search(dep): |
| 249 | dep = dep.split()[0] |
| 250 | |
| 251 | # Put parentheses around any version specifications. |
| 252 | dep = r.sub(r'(\g<0>)',dep) |
| 253 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 254 | if file not in i: |
| 255 | i[file] = [] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 256 | i[file].append(dep) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 257 | |
| 258 | return provides, requires |
| 259 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 260 | output = subprocess.check_output(shlex.split(rpmdeps) + pkgfiles, stderr=subprocess.STDOUT).decode("utf-8") |
| 261 | provides, requires = process_deps(output, pkg, pkgdest, provides, requires) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 262 | |
| 263 | return (pkg, provides, requires) |
| 264 | |
| 265 | |
| 266 | def read_shlib_providers(d): |
| 267 | import re |
| 268 | |
| 269 | shlib_provider = {} |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 270 | shlibs_dirs = d.getVar('SHLIBSDIRS').split() |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 271 | list_re = re.compile(r'^(.*)\.list$') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 272 | # Go from least to most specific since the last one found wins |
| 273 | for dir in reversed(shlibs_dirs): |
| 274 | bb.debug(2, "Reading shlib providers in %s" % (dir)) |
| 275 | if not os.path.exists(dir): |
| 276 | continue |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 277 | for file in sorted(os.listdir(dir)): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 278 | m = list_re.match(file) |
| 279 | if m: |
| 280 | dep_pkg = m.group(1) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 281 | try: |
| 282 | fd = open(os.path.join(dir, file)) |
| 283 | except IOError: |
| 284 | # During a build unrelated shlib files may be deleted, so |
| 285 | # handle files disappearing between the listdirs and open. |
| 286 | continue |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 287 | lines = fd.readlines() |
| 288 | fd.close() |
| 289 | for l in lines: |
| 290 | s = l.strip().split(":") |
| 291 | if s[0] not in shlib_provider: |
| 292 | shlib_provider[s[0]] = {} |
| 293 | shlib_provider[s[0]][s[1]] = (dep_pkg, s[2]) |
| 294 | return shlib_provider |