From 7971cb0aa3e517a53f0ce6d3ee9bc3179041ccb8 Mon Sep 17 00:00:00 2001 From: John Klug Date: Wed, 25 May 2022 17:12:18 -0500 Subject: mLinux 6 --- scripts/relocate_sdk.py | 264 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 264 insertions(+) create mode 100755 scripts/relocate_sdk.py (limited to 'scripts/relocate_sdk.py') diff --git a/scripts/relocate_sdk.py b/scripts/relocate_sdk.py new file mode 100755 index 0000000..99fca86 --- /dev/null +++ b/scripts/relocate_sdk.py @@ -0,0 +1,264 @@ +#!/usr/bin/env python +# +# Copyright (c) 2012 Intel Corporation +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# DESCRIPTION +# This script is called by the SDK installer script. It replaces the dynamic +# loader path in all binaries and also fixes the SYSDIR paths/lengths and the +# location of ld.so.cache in the dynamic loader binary +# +# AUTHORS +# Laurentiu Palcu +# + +import struct +import sys +import stat +import os +import re +import errno + +if sys.version < '3': + def b(x): + return x +else: + def b(x): + return x.encode(sys.getfilesystemencoding()) + +old_prefix = re.compile(b("##DEFAULT_INSTALL_DIR##")) + +def get_arch(): + f.seek(0) + e_ident =f.read(16) + ei_mag0,ei_mag1_3,ei_class = struct.unpack("= p_filesz): + print("ERROR: could not relocate %s, interp size = %i and %i is needed." \ + % (elf_file_name, p_memsz, len(new_dl_path) + 1)) + break + dl_path = new_dl_path + b("\0") * (p_filesz - len(new_dl_path)) + f.seek(p_offset) + f.write(dl_path) + break + +def change_dl_sysdirs(elf_file_name): + if arch == 32: + sh_fmt = "= sh_size): + print("ERROR: could not relocate %s, .ldsocache section size = %i and %i is needed." \ + % (elf_file_name, sh_size, len(new_ldsocache_path))) + sys.exit(-1) + # pad with zeros + new_ldsocache_path += b("\0") * (sh_size - len(new_ldsocache_path)) + # write it back + f.seek(sh_offset) + f.write(new_ldsocache_path) + elif name == b(".gccrelocprefix"): + offset = 0 + while (offset + 4096) <= sh_size: + path = f.read(4096) + new_path = old_prefix.sub(new_prefix, path) + new_path = new_path.rstrip(b("\0")) + if (len(new_path) >= 4096): + print("ERROR: could not relocate %s, max path size = 4096 and %i is needed." \ + % (elf_file_name, len(new_path))) + sys.exit(-1) + # pad with zeros + new_path += b("\0") * (4096 - len(new_path)) + #print "Changing %s to %s at %s" % (str(path), str(new_path), str(offset)) + # write it back + f.seek(sh_offset + offset) + f.write(new_path) + offset = offset + 4096 + if sysdirs != "" and sysdirslen != "": + paths = sysdirs.split(b("\0")) + sysdirs = b("") + sysdirslen = b("") + for path in paths: + """ exit the loop when we encounter first empty string """ + if path == b(""): + break + + new_path = old_prefix.sub(new_prefix, path) + sysdirs += new_path + b("\0") + + if arch == 32: + sysdirslen += struct.pack(" 3, strings may also contain Unicode characters. So, convert +# them to bytes +if sys.version_info < (3,): + new_prefix = sys.argv[1] + new_dl_path = sys.argv[2] +else: + new_prefix = sys.argv[1].encode() + new_dl_path = sys.argv[2].encode() + +executables_list = sys.argv[3:] + +for e in executables_list: + perms = os.stat(e)[stat.ST_MODE] + if os.access(e, os.W_OK|os.R_OK): + perms = None + else: + os.chmod(e, perms|stat.S_IRWXU) + + try: + f = open(e, "r+b") + except IOError: + exctype, ioex = sys.exc_info()[:2] + if ioex.errno == errno.ETXTBSY: + print("Could not open %s. File used by another process.\nPlease "\ + "make sure you exit all processes that might use any SDK "\ + "binaries." % e) + else: + print("Could not open %s: %s(%d)" % (e, ioex.strerror, ioex.errno)) + sys.exit(-1) + + # Save old size and do a size check at the end. Just a safety measure. + old_size = os.path.getsize(e) + if old_size >= 64: + arch = get_arch() + if arch: + parse_elf_header() + change_interpreter(e) + change_dl_sysdirs(e) + + """ change permissions back """ + if perms: + os.chmod(e, perms) + + f.close() + + if old_size != os.path.getsize(e): + print("New file size for %s is different. Looks like a relocation error!", e) + sys.exit(-1) + -- cgit v1.2.3