relocate_sdk.py: make it work also with python 3

Strings in Python 3, by default, are assumed to contain unicode
characters. In previous versions of python (<3), unicode strings are
explicitly declared with u"abc". If not, than they're automatically
converted to bytes. This doesn't happen anymore in Python 3.

Since we're dealing with binary files, opened in byte mode, make sure
that we explicitly convert all strings to bytes to make both python 2
and 3 happy.

Other changes:
 * add a safety check to make sure relocation did not change the file
   size;
 * a couple of cosmetic changes (wrap long lines so that we don't have
   to scroll to reach the end of them);

(From OE-Core rev: 175f20e27eadc79df16109961f5ce6232705e96f)

Signed-off-by: Laurentiu Palcu <laurentiu.palcu@intel.com>
Signed-off-by: Saul Wold <sgw@linux.intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
This commit is contained in:
Laurentiu Palcu 2013-09-25 09:32:33 +00:00 committed by Richard Purdie
parent c54076ed8a
commit 5b5e1b9008
1 changed files with 35 additions and 19 deletions

View File

@ -31,7 +31,7 @@ import os
import re import re
import errno import errno
old_prefix = re.compile("##DEFAULT_INSTALL_DIR##") old_prefix = re.compile(b"##DEFAULT_INSTALL_DIR##")
def get_arch(): def get_arch():
f.seek(0) f.seek(0)
@ -92,12 +92,15 @@ def change_interpreter(elf_file_name):
# External SDKs with mixed pre-compiled binaries should not get # External SDKs with mixed pre-compiled binaries should not get
# relocated so look for some variant of /lib # relocated so look for some variant of /lib
fname = f.read(11) fname = f.read(11)
if fname.startswith("/lib/") or fname.startswith("/lib64/") or fname.startswith("/lib32/") or fname.startswith("/usr/lib32/") or fname.startswith("/usr/lib32/") or fname.startswith("/usr/lib64/"): 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 break
if (len(new_dl_path) >= p_filesz): 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) print("ERROR: could not relocate %s, interp size = %i and %i is needed." \
% (elf_file_name, p_memsz, len(new_dl_path) + 1))
break break
dl_path = new_dl_path + "\0" * (p_filesz - len(new_dl_path)) dl_path = new_dl_path + b"\0" * (p_filesz - len(new_dl_path))
f.seek(p_offset) f.seek(p_offset)
f.write(dl_path) f.write(dl_path)
break break
@ -129,40 +132,40 @@ def change_dl_sysdirs():
sh_name, sh_type, sh_flags, sh_addr, sh_offset, sh_size, sh_link,\ sh_name, sh_type, sh_flags, sh_addr, sh_offset, sh_size, sh_link,\
sh_info, sh_addralign, sh_entsize = struct.unpack(sh_fmt, 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 """ """ look only into SHT_PROGBITS sections """
if sh_type == 1: if sh_type == 1:
f.seek(sh_offset) f.seek(sh_offset)
""" default library paths cannot be changed on the fly because """ """ default library paths cannot be changed on the fly because """
""" the string lengths have to be changed too. """ """ the string lengths have to be changed too. """
if name == ".sysdirs": if name == b".sysdirs":
sysdirs = f.read(sh_size) sysdirs = f.read(sh_size)
sysdirs_off = sh_offset sysdirs_off = sh_offset
sysdirs_sect_size = sh_size sysdirs_sect_size = sh_size
elif name == ".sysdirslen": elif name == b".sysdirslen":
sysdirslen = f.read(sh_size) sysdirslen = f.read(sh_size)
sysdirslen_off = sh_offset sysdirslen_off = sh_offset
elif name == ".ldsocache": elif name == b".ldsocache":
ldsocache_path = f.read(sh_size) ldsocache_path = f.read(sh_size)
new_ldsocache_path = old_prefix.sub(new_prefix, ldsocache_path) new_ldsocache_path = old_prefix.sub(new_prefix, ldsocache_path)
# pad with zeros # 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 # write it back
f.seek(sh_offset) f.seek(sh_offset)
f.write(new_ldsocache_path) f.write(new_ldsocache_path)
if sysdirs != "" and sysdirslen != "": if sysdirs != "" and sysdirslen != "":
paths = sysdirs.split("\0") paths = sysdirs.split(b"\0")
sysdirs = "" sysdirs = b""
sysdirslen = "" sysdirslen = b""
for path in paths: for path in paths:
""" exit the loop when we encounter first empty string """ """ exit the loop when we encounter first empty string """
if path == "": if path == b"":
break break
new_path = old_prefix.sub(new_prefix, path) new_path = old_prefix.sub(new_prefix, path)
sysdirs += new_path + "\0" sysdirs += new_path + b"\0"
if arch == 32: if arch == 32:
sysdirslen += struct.pack("<L", len(new_path)) sysdirslen += struct.pack("<L", len(new_path))
@ -170,7 +173,7 @@ def change_dl_sysdirs():
sysdirslen += struct.pack("<Q", len(new_path)) sysdirslen += struct.pack("<Q", len(new_path))
""" pad with zeros """ """ pad with zeros """
sysdirs += "\0" * (sysdirs_sect_size - len(sysdirs)) sysdirs += b"\0" * (sysdirs_sect_size - len(sysdirs))
""" write the sections back """ """ write the sections back """
f.seek(sysdirs_off) f.seek(sysdirs_off)
@ -178,13 +181,19 @@ def change_dl_sysdirs():
f.seek(sysdirslen_off) f.seek(sysdirslen_off)
f.write(sysdirslen) f.write(sysdirslen)
# MAIN # MAIN
if len(sys.argv) < 4: if len(sys.argv) < 4:
sys.exit(-1) sys.exit(-1)
new_prefix = sys.argv[1] # In python > 3, strings may also contain Unicode characters. So, convert
new_dl_path = sys.argv[2] # 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:] executables_list = sys.argv[3:]
for e in executables_list: for e in executables_list:
@ -196,7 +205,7 @@ for e in executables_list:
try: try:
f = open(e, "r+b") f = open(e, "r+b")
except IOError, ioex: except IOError as ioex:
if ioex.errno == errno.ETXTBSY: if ioex.errno == errno.ETXTBSY:
print("Could not open %s. File used by another process.\nPlease "\ print("Could not open %s. File used by another process.\nPlease "\
"make sure you exit all processes that might use any SDK "\ "make sure you exit all processes that might use any SDK "\
@ -205,6 +214,9 @@ for e in executables_list:
print("Could not open %s: %s(%d)" % (e, ioex.strerror, ioex.errno)) print("Could not open %s: %s(%d)" % (e, ioex.strerror, ioex.errno))
sys.exit(-1) sys.exit(-1)
# Save old size and do a size check at the end. Just a safety measure.
old_size = os.path.getsize(e)
arch = get_arch() arch = get_arch()
if arch: if arch:
parse_elf_header() parse_elf_header()
@ -217,3 +229,7 @@ for e in executables_list:
f.close() 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)