summaryrefslogtreecommitdiffstats
path: root/scripts/relocate_sdk.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/relocate_sdk.py')
-rwxr-xr-xscripts/relocate_sdk.py42
1 files changed, 25 insertions, 17 deletions
diff --git a/scripts/relocate_sdk.py b/scripts/relocate_sdk.py
index fe6e4e0320..05d9fd65ed 100755
--- a/scripts/relocate_sdk.py
+++ b/scripts/relocate_sdk.py
@@ -31,7 +31,14 @@ import os
31import re 31import re
32import errno 32import errno
33 33
34old_prefix = re.compile(b"##DEFAULT_INSTALL_DIR##") 34if sys.version < '3':
35 def b(x):
36 return x
37else:
38 def b(x):
39 return x.encode(sys.getfilesystemencoding())
40
41old_prefix = re.compile(b("##DEFAULT_INSTALL_DIR##"))
35 42
36def get_arch(): 43def get_arch():
37 f.seek(0) 44 f.seek(0)
@@ -92,15 +99,15 @@ def change_interpreter(elf_file_name):
92 # External SDKs with mixed pre-compiled binaries should not get 99 # External SDKs with mixed pre-compiled binaries should not get
93 # relocated so look for some variant of /lib 100 # relocated so look for some variant of /lib
94 fname = f.read(11) 101 fname = f.read(11)
95 if fname.startswith(b"/lib/") or fname.startswith(b"/lib64/") or \ 102 if fname.startswith(b("/lib/")) or fname.startswith(b("/lib64/")) or \
96 fname.startswith(b"/lib32/") or fname.startswith(b"/usr/lib32/") or \ 103 fname.startswith(b("/lib32/")) or fname.startswith(b("/usr/lib32/")) or \
97 fname.startswith(b"/usr/lib32/") or fname.startswith(b"/usr/lib64/"): 104 fname.startswith(b("/usr/lib32/")) or fname.startswith(b("/usr/lib64/")):
98 break 105 break
99 if (len(new_dl_path) >= p_filesz): 106 if (len(new_dl_path) >= p_filesz):
100 print("ERROR: could not relocate %s, interp size = %i and %i is needed." \ 107 print("ERROR: could not relocate %s, interp size = %i and %i is needed." \
101 % (elf_file_name, p_memsz, len(new_dl_path) + 1)) 108 % (elf_file_name, p_memsz, len(new_dl_path) + 1))
102 break 109 break
103 dl_path = new_dl_path + b"\0" * (p_filesz - len(new_dl_path)) 110 dl_path = new_dl_path + b("\0") * (p_filesz - len(new_dl_path))
104 f.seek(p_offset) 111 f.seek(p_offset)
105 f.write(dl_path) 112 f.write(dl_path)
106 break 113 break
@@ -132,40 +139,40 @@ def change_dl_sysdirs():
132 sh_name, sh_type, sh_flags, sh_addr, sh_offset, sh_size, sh_link,\ 139 sh_name, sh_type, sh_flags, sh_addr, sh_offset, sh_size, sh_link,\
133 sh_info, sh_addralign, sh_entsize = struct.unpack(sh_fmt, sh_hdr) 140 sh_info, sh_addralign, sh_entsize = struct.unpack(sh_fmt, sh_hdr)
134 141
135 name = sh_strtab[sh_name:sh_strtab.find(b"\0", sh_name)] 142 name = sh_strtab[sh_name:sh_strtab.find(b("\0"), sh_name)]
136 143
137 """ look only into SHT_PROGBITS sections """ 144 """ look only into SHT_PROGBITS sections """
138 if sh_type == 1: 145 if sh_type == 1:
139 f.seek(sh_offset) 146 f.seek(sh_offset)
140 """ default library paths cannot be changed on the fly because """ 147 """ default library paths cannot be changed on the fly because """
141 """ the string lengths have to be changed too. """ 148 """ the string lengths have to be changed too. """
142 if name == b".sysdirs": 149 if name == b(".sysdirs"):
143 sysdirs = f.read(sh_size) 150 sysdirs = f.read(sh_size)
144 sysdirs_off = sh_offset 151 sysdirs_off = sh_offset
145 sysdirs_sect_size = sh_size 152 sysdirs_sect_size = sh_size
146 elif name == b".sysdirslen": 153 elif name == b(".sysdirslen"):
147 sysdirslen = f.read(sh_size) 154 sysdirslen = f.read(sh_size)
148 sysdirslen_off = sh_offset 155 sysdirslen_off = sh_offset
149 elif name == b".ldsocache": 156 elif name == b(".ldsocache"):
150 ldsocache_path = f.read(sh_size) 157 ldsocache_path = f.read(sh_size)
151 new_ldsocache_path = old_prefix.sub(new_prefix, ldsocache_path) 158 new_ldsocache_path = old_prefix.sub(new_prefix, ldsocache_path)
152 # pad with zeros 159 # pad with zeros
153 new_ldsocache_path += b"\0" * (sh_size - len(new_ldsocache_path)) 160 new_ldsocache_path += b("\0") * (sh_size - len(new_ldsocache_path))
154 # write it back 161 # write it back
155 f.seek(sh_offset) 162 f.seek(sh_offset)
156 f.write(new_ldsocache_path) 163 f.write(new_ldsocache_path)
157 164
158 if sysdirs != "" and sysdirslen != "": 165 if sysdirs != "" and sysdirslen != "":
159 paths = sysdirs.split(b"\0") 166 paths = sysdirs.split(b("\0"))
160 sysdirs = b"" 167 sysdirs = b("")
161 sysdirslen = b"" 168 sysdirslen = b("")
162 for path in paths: 169 for path in paths:
163 """ exit the loop when we encounter first empty string """ 170 """ exit the loop when we encounter first empty string """
164 if path == b"": 171 if path == b(""):
165 break 172 break
166 173
167 new_path = old_prefix.sub(new_prefix, path) 174 new_path = old_prefix.sub(new_prefix, path)
168 sysdirs += new_path + b"\0" 175 sysdirs += new_path + b("\0")
169 176
170 if arch == 32: 177 if arch == 32:
171 sysdirslen += struct.pack("<L", len(new_path)) 178 sysdirslen += struct.pack("<L", len(new_path))
@@ -173,7 +180,7 @@ def change_dl_sysdirs():
173 sysdirslen += struct.pack("<Q", len(new_path)) 180 sysdirslen += struct.pack("<Q", len(new_path))
174 181
175 """ pad with zeros """ 182 """ pad with zeros """
176 sysdirs += b"\0" * (sysdirs_sect_size - len(sysdirs)) 183 sysdirs += b("\0") * (sysdirs_sect_size - len(sysdirs))
177 184
178 """ write the sections back """ 185 """ write the sections back """
179 f.seek(sysdirs_off) 186 f.seek(sysdirs_off)
@@ -205,7 +212,8 @@ for e in executables_list:
205 212
206 try: 213 try:
207 f = open(e, "r+b") 214 f = open(e, "r+b")
208 except IOError as ioex: 215 except IOError:
216 exctype, ioex = sys.exc_info()[:2]
209 if ioex.errno == errno.ETXTBSY: 217 if ioex.errno == errno.ETXTBSY:
210 print("Could not open %s. File used by another process.\nPlease "\ 218 print("Could not open %s. File used by another process.\nPlease "\
211 "make sure you exit all processes that might use any SDK "\ 219 "make sure you exit all processes that might use any SDK "\