| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
# mklibs.py: An automated way to create a minimal /lib/ directory. |
| 4 |
# |
| 5 |
# Copyright 2001 by Falk Hueffner <[email protected]> |
| 6 |
# & Goswin Brederlow <[email protected]> |
| 7 |
# |
| 8 |
# mklibs.sh by Marcus Brinkmann <[email protected]> |
| 9 |
# used as template |
| 10 |
# |
| 11 |
# This program is free software; you can redistribute it and/or modify |
| 12 |
# it under the terms of the GNU General Public License as published by |
| 13 |
# the Free Software Foundation; either version 2 of the License, or |
| 14 |
# (at your option) any later version. |
| 15 |
# |
| 16 |
# This program is distributed in the hope that it will be useful, |
| 17 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 |
# GNU General Public License for more details. |
| 20 |
# |
| 21 |
# You should have received a copy of the GNU General Public License |
| 22 |
# along with this program; if not, write to the Free Software |
| 23 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 24 |
|
| 25 |
import re |
| 26 |
import sys |
| 27 |
import os |
| 28 |
import glob |
| 29 |
import logging |
| 30 |
from stat import * |
| 31 |
import subprocess |
| 32 |
|
| 33 |
try: |
| 34 |
import mklibs |
| 35 |
except ImportError: |
| 36 |
sys.path.append('/usr/lib/mklibs/python') |
| 37 |
|
| 38 |
from mklibs.utils.main import MainBase |
| 39 |
|
| 40 |
class Main(MainBase): |
| 41 |
pass |
| 42 |
|
| 43 |
main = Main() |
| 44 |
|
| 45 |
logger = logging.getLogger(__name__) |
| 46 |
|
| 47 |
# return a list of lines of output of the command |
| 48 |
def command(command, *args): |
| 49 |
logger.debug("calling %s %s", command, ' '.join(args)) |
| 50 |
pipe = os.popen(command + ' ' + ' '.join(args), 'r') |
| 51 |
output = pipe.read().strip() |
| 52 |
status = pipe.close() |
| 53 |
if status is not None and os.WEXITSTATUS(status) != 0: |
| 54 |
print("Command failed with status", os.WEXITSTATUS(status), ":", \ |
| 55 |
command, ' '.join(args)) |
| 56 |
print("With output:", output) |
| 57 |
sys.exit(1) |
| 58 |
return output.split('\n') |
| 59 |
|
| 60 |
# Filter a list according to a regexp containing a () group. Return |
| 61 |
# a set. |
| 62 |
def regexpfilter(list, regexp, groupnr = 1): |
| 63 |
pattern = re.compile(regexp) |
| 64 |
result = set() |
| 65 |
for x in list: |
| 66 |
match = pattern.match(x) |
| 67 |
if match: |
| 68 |
result.add(match.group(groupnr)) |
| 69 |
|
| 70 |
return result |
| 71 |
|
| 72 |
def elf_header(obj): |
| 73 |
if not os.access(obj, os.F_OK): |
| 74 |
raise "Cannot find lib: " + obj |
| 75 |
output = command("mklibs-readelf", "--print-elf-header", obj) |
| 76 |
s = [int(i) for i in output[0].split()] |
| 77 |
return {'class': s[0], 'data': s[1], 'machine': s[2], 'flags': s[3]} |
| 78 |
|
| 79 |
# Return a set of rpath strings for the passed object |
| 80 |
def rpath(obj): |
| 81 |
if not os.access(obj, os.F_OK): |
| 82 |
raise "Cannot find lib: " + obj |
| 83 |
output = command("mklibs-readelf", "-R", obj) |
| 84 |
return [root + "/" + x for x in output if x] |
| 85 |
|
| 86 |
# Return a set of libraries the passed objects depend on. |
| 87 |
def library_depends(obj): |
| 88 |
if not os.access(obj, os.F_OK): |
| 89 |
raise "Cannot find lib: " + obj |
| 90 |
return [x for x in command("mklibs-readelf", "-n", obj) if x] |
| 91 |
|
| 92 |
# Return real target of a symlink |
| 93 |
def resolve_link(file): |
| 94 |
logger.debug("resolving %s", file) |
| 95 |
while S_ISLNK(os.lstat(file)[ST_MODE]): |
| 96 |
new_file = os.readlink(file) |
| 97 |
if new_file[0] != "/": |
| 98 |
file = os.path.join(os.path.dirname(file), new_file) |
| 99 |
else: |
| 100 |
file = new_file |
| 101 |
logger.debug("resolved to %s", file) |
| 102 |
return file |
| 103 |
|
| 104 |
# Find complete path of a library, by searching in lib_path |
| 105 |
def find_lib(lib): |
| 106 |
for path in lib_path: |
| 107 |
if os.access(path + "/" + lib, os.F_OK): |
| 108 |
return path + "/" + lib |
| 109 |
|
| 110 |
return "" |
| 111 |
|
| 112 |
def extract_soname(so_file): |
| 113 |
soname_data = command("mklibs-readelf", "-s", so_file) |
| 114 |
if soname_data: |
| 115 |
return soname_data.pop() |
| 116 |
return "" |
| 117 |
|
| 118 |
def multiarch(paths): |
| 119 |
devnull = open('/dev/null', 'w') |
| 120 |
dpkg_architecture = subprocess.Popen( |
| 121 |
['dpkg-architecture', '-qDEB_HOST_MULTIARCH'], |
| 122 |
stdout=subprocess.PIPE, stderr=devnull) |
| 123 |
devnull.close() |
| 124 |
deb_host_multiarch, _ = dpkg_architecture.communicate() |
| 125 |
if dpkg_architecture.returncode == 0: |
| 126 |
deb_host_multiarch = deb_host_multiarch.rstrip('\n') |
| 127 |
new_paths = [] |
| 128 |
for path in paths: |
| 129 |
new_paths.append( |
| 130 |
path.replace('/lib', '/lib/%s' % deb_host_multiarch, 1)) |
| 131 |
new_paths.append(path) |
| 132 |
return new_paths |
| 133 |
else: |
| 134 |
return paths |
| 135 |
|
| 136 |
def version(vers): |
| 137 |
print("mklibs: version ",vers) |
| 138 |
print("") |
| 139 |
|
| 140 |
# Clean the environment |
| 141 |
vers="0.12" |
| 142 |
os.environ['LC_ALL'] = "C" |
| 143 |
|
| 144 |
# some global variables |
| 145 |
lib_rpath = [] |
| 146 |
lib_path = main.options.library_dir |
| 147 |
dest_path = main.options.dest_dir |
| 148 |
ldlib = main.options.ldlib |
| 149 |
default_lib_path = multiarch(["/lib/", "/usr/lib/", "/usr/X11R6/lib/"]) |
| 150 |
target = "" |
| 151 |
root = main.options.root |
| 152 |
include_default_lib_path = not main.options.omit_library_dir |
| 153 |
proglist = main.files |
| 154 |
so_pattern = re.compile("((lib|ld).*)\.so(\..+)*") |
| 155 |
|
| 156 |
if include_default_lib_path: |
| 157 |
lib_path.extend(default_lib_path) |
| 158 |
|
| 159 |
objects = {} # map from inode to filename |
| 160 |
for prog in proglist: |
| 161 |
inode = os.stat(prog)[ST_INO] |
| 162 |
if objects.has_key(inode): |
| 163 |
logger.debug("%s is a hardlink to %s", prog, objects[inode]) |
| 164 |
elif so_pattern.match(prog): |
| 165 |
logger.debug("%s is a library", prog) |
| 166 |
elif open(prog).read(4)[1:] == 'ELF': |
| 167 |
objects[inode] = prog |
| 168 |
else: |
| 169 |
logger.debug("%s is no ELF", prog) |
| 170 |
|
| 171 |
if not ldlib: |
| 172 |
for obj in objects.values(): |
| 173 |
output = command("mklibs-readelf", "-i", obj) |
| 174 |
for x in output: |
| 175 |
ldlib = x |
| 176 |
if ldlib: |
| 177 |
break |
| 178 |
|
| 179 |
if not ldlib: |
| 180 |
sys.exit("E: Dynamic linker not found, aborting.") |
| 181 |
|
| 182 |
logger.info('Using %s as dynamic linker', ldlib) |
| 183 |
|
| 184 |
# Check for rpaths |
| 185 |
for obj in objects.values(): |
| 186 |
rpath_val = rpath(obj) |
| 187 |
if rpath_val: |
| 188 |
if root: |
| 189 |
for rpath_elem in rpath_val: |
| 190 |
if not rpath_elem in lib_rpath: |
| 191 |
logger.verbose('Adding rpath %s for %s', rpath_elem, obj) |
| 192 |
lib_rpath.append(rpath_elem) |
| 193 |
else: |
| 194 |
logger.warning('%s may need rpath, but --root not specified', obj) |
| 195 |
|
| 196 |
lib_path.extend(lib_rpath) |
| 197 |
|
| 198 |
passnr = 1 |
| 199 |
available_libs = [] |
| 200 |
previous_pass_libraries = set() |
| 201 |
while 1: |
| 202 |
logger.info("library reduction pass %d", passnr) |
| 203 |
|
| 204 |
passnr = passnr + 1 |
| 205 |
# Gather all already reduced libraries and treat them as objects as well |
| 206 |
small_libs = [] |
| 207 |
for lib in regexpfilter(os.listdir(dest_path), "(.*-so-stripped)$"): |
| 208 |
obj = dest_path + "/" + lib |
| 209 |
small_libs.append(obj) |
| 210 |
inode = os.stat(obj)[ST_INO] |
| 211 |
if objects.has_key(inode): |
| 212 |
logger.debug("%s is hardlink to %s", obj, objects[inode]) |
| 213 |
else: |
| 214 |
objects[inode] = obj |
| 215 |
|
| 216 |
for obj in objects.values(): |
| 217 |
small_libs.append(obj) |
| 218 |
|
| 219 |
logger.verbose('Objects: %r', ' '.join([i[i.rfind('/') + 1:] for i in objects.itervalues()])) |
| 220 |
|
| 221 |
libraries = set() |
| 222 |
for obj in objects.values(): |
| 223 |
libraries.update(library_depends(obj)) |
| 224 |
|
| 225 |
if libraries == previous_pass_libraries: |
| 226 |
# No progress in last pass. |
| 227 |
break |
| 228 |
|
| 229 |
previous_pass_libraries = libraries |
| 230 |
|
| 231 |
# WORKAROUND: Always add libgcc on old-abi arm |
| 232 |
header = elf_header(find_lib(libraries.copy().pop())) |
| 233 |
if header['machine'] == 40 and header['flags'] & 0xff000000 == 0: |
| 234 |
libraries.add('libgcc_s.so.1') |
| 235 |
|
| 236 |
# reduce libraries |
| 237 |
for library in libraries: |
| 238 |
so_file = find_lib(library) |
| 239 |
if root and (re.compile("^" + root).search(so_file)): |
| 240 |
logger.verbose("no action required for " + so_file) |
| 241 |
if not so_file in available_libs: |
| 242 |
logger.verbose("adding %s to available libs", so_file) |
| 243 |
available_libs.append(so_file) |
| 244 |
continue |
| 245 |
so_file_name = os.path.basename(so_file) |
| 246 |
if not so_file: |
| 247 |
sys.exit("File not found:" + library) |
| 248 |
command("cp", so_file, dest_path + "/" + so_file_name + "-so-stripped") |
| 249 |
|
| 250 |
# Finalising libs and cleaning up |
| 251 |
for lib in regexpfilter(os.listdir(dest_path), "(.*)-so-stripped$"): |
| 252 |
os.rename(dest_path + "/" + lib + "-so-stripped", dest_path + "/" + lib) |
| 253 |
for lib in regexpfilter(os.listdir(dest_path), "(.*-so)$"): |
| 254 |
os.remove(dest_path + "/" + lib) |
| 255 |
|
| 256 |
# Make sure the dynamic linker is present and is executable |
| 257 |
# at its correct location (and not duplicated in /lib) |
| 258 |
ld_file_name = os.path.basename(ldlib) |
| 259 |
ld_path_name = os.path.dirname(ldlib) |
| 260 |
ld_full_path = "../" + ldlib |
| 261 |
ld_file = find_lib(ld_file_name) |
| 262 |
|
| 263 |
if ld_path_name != "/lib": |
| 264 |
if os.access(dest_path + "/" + ld_file_name, os.F_OK): |
| 265 |
os.remove(dest_path + "/" + ld_file_name) |
| 266 |
|
| 267 |
if not os.path.exists(dest_path + "/../" + ld_path_name): |
| 268 |
os.mkdir(dest_path + "/../" + ld_path_name) |
| 269 |
|
| 270 |
if not os.access(dest_path + "/" + ld_full_path, os.F_OK): |
| 271 |
logger.info("stripping and copying dynamic linker to " + ld_full_path) |
| 272 |
command(target + "objcopy", "--strip-unneeded -R .note -R .comment", |
| 273 |
ld_file, dest_path + "/" + ld_full_path) |
| 274 |
|
| 275 |
os.chmod(dest_path + "/" + ld_full_path, 0o755) |