summaryrefslogtreecommitdiff
path: root/scripts/relocate_sdk.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/relocate_sdk.py')
-rwxr-xr-xscripts/relocate_sdk.py132
1 files changed, 94 insertions, 38 deletions
diff --git a/scripts/relocate_sdk.py b/scripts/relocate_sdk.py
index 74bb7a5fdd..c752fa2c61 100755
--- a/scripts/relocate_sdk.py
+++ b/scripts/relocate_sdk.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
#
# Copyright (c) 2012 Intel Corporation
#
@@ -31,7 +31,14 @@ import os
import re
import errno
-old_prefix = re.compile("##DEFAULT_INSTALL_DIR##")
+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)
@@ -55,22 +62,22 @@ def parse_elf_header():
if arch == 32:
# 32bit
- hdr_struct = struct.Struct("<HHILLLIHHHHHH")
+ hdr_fmt = "<HHILLLIHHHHHH"
hdr_size = 52
else:
# 64bit
- hdr_struct = struct.Struct("<HHIQQQIHHHHHH")
+ hdr_fmt = "<HHIQQQIHHHHHH"
hdr_size = 64
e_type, e_machine, e_version, e_entry, e_phoff, e_shoff, e_flags,\
e_ehsize, e_phentsize, e_phnum, e_shentsize, e_shnum, e_shstrndx =\
- hdr_struct.unpack(elf_header[16:hdr_size])
+ struct.unpack(hdr_fmt, elf_header[16:hdr_size])
-def change_interpreter():
+def change_interpreter(elf_file_name):
if arch == 32:
- ph_struct = struct.Struct("<IIIIIIII")
+ ph_fmt = "<IIIIIIII"
else:
- ph_struct = struct.Struct("<IIQQQQQQ")
+ ph_fmt = "<IIQQQQQQ"
""" look for PT_INTERP section """
for i in range(0,e_phnum):
@@ -79,25 +86,39 @@ def change_interpreter():
if arch == 32:
# 32bit
p_type, p_offset, p_vaddr, p_paddr, p_filesz,\
- p_memsz, p_flags, p_align = ph_struct.unpack(ph_hdr)
+ p_memsz, p_flags, p_align = struct.unpack(ph_fmt, ph_hdr)
else:
# 64bit
p_type, p_flags, p_offset, p_vaddr, p_paddr, \
- p_filesz, p_memsz, p_align = ph_struct.unpack(ph_hdr)
+ p_filesz, p_memsz, p_align = struct.unpack(ph_fmt, ph_hdr)
""" change interpreter """
if p_type == 3:
# PT_INTERP section
f.seek(p_offset)
- dl_path = new_dl_path + "\0" * (p_filesz - len(new_dl_path))
+ # External SDKs with mixed pre-compiled binaries should not get
+ # relocated so look for some variant of /lib
+ fname = f.read(11)
+ if fname.startswith(b("/lib/")) or fname.startswith(b("/lib64/")) or \
+ fname.startswith(b("/lib32/")) or fname.startswith(b("/usr/lib32/")) or \
+ fname.startswith(b("/usr/lib32/")) or fname.startswith(b("/usr/lib64/")):
+ break
+ if p_filesz == 0:
+ break
+ if (len(new_dl_path) >= 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():
+def change_dl_sysdirs(elf_file_name):
if arch == 32:
- sh_struct = struct.Struct("<IIIIIIIIII")
+ sh_fmt = "<IIIIIIIIII"
else:
- sh_struct = struct.Struct("<IIQQQQIIQQ")
+ sh_fmt = "<IIQQQQIIQQ"
""" read section string table """
f.seek(e_shoff + e_shstrndx * e_shentsize)
@@ -118,42 +139,63 @@ def change_dl_sysdirs():
sh_hdr = f.read(e_shentsize)
sh_name, sh_type, sh_flags, sh_addr, sh_offset, sh_size, sh_link,\
- sh_info, sh_addralign, sh_entsize = sh_struct.unpack(sh_hdr)
+ sh_info, sh_addralign, sh_entsize = struct.unpack(sh_fmt, sh_hdr)
- name = sh_strtab[sh_name:sh_strtab.find("\0", sh_name)]
+ name = sh_strtab[sh_name:sh_strtab.find(b("\0"), sh_name)]
""" look only into SHT_PROGBITS sections """
if sh_type == 1:
f.seek(sh_offset)
""" default library paths cannot be changed on the fly because """
""" the string lengths have to be changed too. """
- if name == ".sysdirs":
+ if name == b(".sysdirs"):
sysdirs = f.read(sh_size)
sysdirs_off = sh_offset
sysdirs_sect_size = sh_size
- elif name == ".sysdirslen":
+ elif name == b(".sysdirslen"):
sysdirslen = f.read(sh_size)
sysdirslen_off = sh_offset
- elif name == ".ldsocache":
+ elif name == b(".ldsocache"):
ldsocache_path = f.read(sh_size)
new_ldsocache_path = old_prefix.sub(new_prefix, ldsocache_path)
+ new_ldsocache_path = new_ldsocache_path.rstrip(b("\0"))
+ if (len(new_ldsocache_path) >= 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 += "\0" * (sh_size - len(new_ldsocache_path))
+ 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("\0")
- sysdirs = ""
- 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 == "":
+ if path == b(""):
break
new_path = old_prefix.sub(new_prefix, path)
- sysdirs += new_path + "\0"
+ sysdirs += new_path + b("\0")
if arch == 32:
sysdirslen += struct.pack("<L", len(new_path))
@@ -161,7 +203,7 @@ def change_dl_sysdirs():
sysdirslen += struct.pack("<Q", len(new_path))
""" pad with zeros """
- sysdirs += "\0" * (sysdirs_sect_size - len(sysdirs))
+ sysdirs += b("\0") * (sysdirs_sect_size - len(sysdirs))
""" write the sections back """
f.seek(sysdirs_off)
@@ -169,13 +211,19 @@ def change_dl_sysdirs():
f.seek(sysdirslen_off)
f.write(sysdirslen)
-
# MAIN
if len(sys.argv) < 4:
- exit(-1)
+ sys.exit(-1)
+
+# In python > 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()
-new_prefix = sys.argv[1]
-new_dl_path = sys.argv[2]
executables_list = sys.argv[3:]
for e in executables_list:
@@ -187,20 +235,24 @@ for e in executables_list:
try:
f = open(e, "r+b")
- except IOError as ioex:
+ 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))
- exit(-1)
+ sys.exit(-1)
- arch = get_arch()
- if arch:
- parse_elf_header()
- change_interpreter()
- change_dl_sysdirs()
+ # 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:
@@ -208,3 +260,7 @@ for e in executables_list:
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)
+