summaryrefslogtreecommitdiffstats
path: root/meta
diff options
context:
space:
mode:
authorRichard Purdie <richard.purdie@linuxfoundation.org>2018-07-20 08:45:28 +0000
committerRichard Purdie <richard.purdie@linuxfoundation.org>2018-07-24 11:52:27 +0100
commit069a1c4a1520914234dc58b506663fcfd80433e3 (patch)
tree2262a99b4945322847699389c08f2a923e650948 /meta
parente0ea2c73781800fa2e1c7d9d2fd540eca2f9f3e3 (diff)
downloadpoky-069a1c4a1520914234dc58b506663fcfd80433e3.tar.gz
package: Refactor to remove isElf/is_elf function duplication
There are probably further cleanups needed here but this at least removes the major code duplication between these two similar funcitons, keeping the kernel module ".ko" extension check for efficiency to avoid opening and reading file contents in the general case. (From OE-Core rev: 7ad0c0d6ab12bebeac097fc0f5210c876dcfe9be) Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'meta')
-rw-r--r--meta/classes/package.bbclass40
-rw-r--r--meta/lib/oe/package.py73
2 files changed, 47 insertions, 66 deletions
diff --git a/meta/classes/package.bbclass b/meta/classes/package.bbclass
index 94e4639a11..f121acccab 100644
--- a/meta/classes/package.bbclass
+++ b/meta/classes/package.bbclass
@@ -936,38 +936,6 @@ python split_and_strip_files () {
936 sourcefile = d.expand("${WORKDIR}/debugsources.list") 936 sourcefile = d.expand("${WORKDIR}/debugsources.list")
937 bb.utils.remove(sourcefile) 937 bb.utils.remove(sourcefile)
938 938
939 # Return type (bits):
940 # 0 - not elf
941 # 1 - ELF
942 # 2 - stripped
943 # 4 - executable
944 # 8 - shared library
945 # 16 - kernel module
946 def isELF(path):
947 type = 0
948 result = subprocess.check_output(["file", "-b", path], stderr=subprocess.STDOUT).decode("utf-8")
949
950 # Not stripped
951 if "ELF" in result:
952 type |= 1
953 if "not stripped" not in result:
954 type |= 2
955 if "executable" in result:
956 type |= 4
957 if "shared" in result:
958 type |= 8
959 return type
960
961 def isStaticLib(path):
962 if path.endswith('.a') and not os.path.islink(path):
963 with open(path, 'rb') as fh:
964 # The magic must include the first slash to avoid
965 # matching golang static libraries
966 magic = b'!<arch>\x0a/'
967 start = fh.read(len(magic))
968 return start == magic
969 return False
970
971 # 939 #
972 # First lets figure out all of the files we may have to process ... do this only once! 940 # First lets figure out all of the files we may have to process ... do this only once!
973 # 941 #
@@ -987,7 +955,7 @@ python split_and_strip_files () {
987 if file.endswith(".ko") and file.find("/lib/modules/") != -1: 955 if file.endswith(".ko") and file.find("/lib/modules/") != -1:
988 kernmods.append(file) 956 kernmods.append(file)
989 continue 957 continue
990 if isStaticLib(file): 958 if oe.package.is_static_lib(file):
991 staticlibs.append(file) 959 staticlibs.append(file)
992 continue 960 continue
993 961
@@ -1017,14 +985,14 @@ python split_and_strip_files () {
1017 # If it's a symlink, and points to an ELF file, we capture the readlink target 985 # If it's a symlink, and points to an ELF file, we capture the readlink target
1018 if cpath.islink(file): 986 if cpath.islink(file):
1019 target = os.readlink(file) 987 target = os.readlink(file)
1020 if isELF(ltarget): 988 if oe.package.is_elf(ltarget):
1021 #bb.note("Sym: %s (%d)" % (ltarget, isELF(ltarget))) 989 #bb.note("Sym: %s (%d)" % (ltarget, oe.package.is_elf(ltarget)))
1022 symlinks[file] = target 990 symlinks[file] = target
1023 continue 991 continue
1024 992
1025 # It's a file (or hardlink), not a link 993 # It's a file (or hardlink), not a link
1026 # ...but is it ELF, and is it already stripped? 994 # ...but is it ELF, and is it already stripped?
1027 elf_file = isELF(file) 995 elf_file = oe.package.is_elf(file)
1028 if elf_file & 1: 996 if elf_file & 1:
1029 if elf_file & 2: 997 if elf_file & 2:
1030 if 'already-stripped' in (d.getVar('INSANE_SKIP_' + pn) or "").split(): 998 if 'already-stripped' in (d.getVar('INSANE_SKIP_' + pn) or "").split():
diff --git a/meta/lib/oe/package.py b/meta/lib/oe/package.py
index 8a303106a9..a435d661a6 100644
--- a/meta/lib/oe/package.py
+++ b/meta/lib/oe/package.py
@@ -1,8 +1,11 @@
1import mmap
2import subprocess
3
1def runstrip(arg): 4def runstrip(arg):
2 # Function to strip a single file, called from split_and_strip_files below 5 # Function to strip a single file, called from split_and_strip_files below
3 # A working 'file' (one which works on the target architecture) 6 # A working 'file' (one which works on the target architecture)
4 # 7 #
5 # The elftype is a bit pattern (explained in split_and_strip_files) to tell 8 # The elftype is a bit pattern (explained in is_elf below) to tell
6 # us what type of file we're processing... 9 # us what type of file we're processing...
7 # 4 - executable 10 # 4 - executable
8 # 8 - shared library 11 # 8 - shared library
@@ -44,6 +47,44 @@ def runstrip(arg):
44 47
45 return 48 return
46 49
50# Detect .ko module by searching for "vermagic=" string
51def is_kernel_module(path):
52 with open(path) as f:
53 return mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ).find(b"vermagic=") >= 0
54
55# Return type (bits):
56# 0 - not elf
57# 1 - ELF
58# 2 - stripped
59# 4 - executable
60# 8 - shared library
61# 16 - kernel module
62def is_elf(path):
63 exec_type = 0
64 result = subprocess.check_output(["file", "-b", path], stderr=subprocess.STDOUT).decode("utf-8")
65
66 if "ELF" in result:
67 exec_type |= 1
68 if "not stripped" not in result:
69 exec_type |= 2
70 if "executable" in result:
71 exec_type |= 4
72 if "shared" in result:
73 exec_type |= 8
74 if "relocatable" in result:
75 if path.endswith(".ko") and path.find("/lib/modules/") != -1 and is_kernel_module(path):
76 exec_type |= 16
77 return exec_type
78
79def is_static_lib(path):
80 if path.endswith('.a') and not os.path.islink(path):
81 with open(path, 'rb') as fh:
82 # The magic must include the first slash to avoid
83 # matching golang static libraries
84 magic = b'!<arch>\x0a/'
85 start = fh.read(len(magic))
86 return start == magic
87 return False
47 88
48def strip_execs(pn, dstdir, strip_cmd, libdir, base_libdir, qa_already_stripped=False): 89def strip_execs(pn, dstdir, strip_cmd, libdir, base_libdir, qa_already_stripped=False):
49 """ 90 """
@@ -56,35 +97,7 @@ def strip_execs(pn, dstdir, strip_cmd, libdir, base_libdir, qa_already_stripped=
56 :param qa_already_stripped: Set to True if already-stripped' in ${INSANE_SKIP} 97 :param qa_already_stripped: Set to True if already-stripped' in ${INSANE_SKIP}
57 This is for proper logging and messages only. 98 This is for proper logging and messages only.
58 """ 99 """
59 import stat, errno, oe.path, oe.utils, mmap, subprocess 100 import stat, errno, oe.path, oe.utils
60
61 # Detect .ko module by searching for "vermagic=" string
62 def is_kernel_module(path):
63 with open(path) as f:
64 return mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ).find(b"vermagic=") >= 0
65
66 # Return type (bits):
67 # 0 - not elf
68 # 1 - ELF
69 # 2 - stripped
70 # 4 - executable
71 # 8 - shared library
72 # 16 - kernel module
73 def is_elf(path):
74 exec_type = 0
75 result = subprocess.check_output(["file", "-b", path], stderr=subprocess.STDOUT).decode("utf-8")
76
77 if "ELF" in result:
78 exec_type |= 1
79 if "not stripped" not in result:
80 exec_type |= 2
81 if "executable" in result:
82 exec_type |= 4
83 if "shared" in result:
84 exec_type |= 8
85 if "relocatable" in result and is_kernel_module(path):
86 exec_type |= 16
87 return exec_type
88 101
89 elffiles = {} 102 elffiles = {}
90 inodes = {} 103 inodes = {}