summaryrefslogtreecommitdiffstats
path: root/meta/lib/oe/package.py
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/lib/oe/package.py
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/lib/oe/package.py')
-rw-r--r--meta/lib/oe/package.py73
1 files changed, 43 insertions, 30 deletions
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 = {}