summaryrefslogtreecommitdiffstats
path: root/scripts
diff options
context:
space:
mode:
authorLaurentiu Palcu <laurentiu.palcu@intel.com>2013-09-25 09:32:33 +0000
committerRichard Purdie <richard.purdie@linuxfoundation.org>2013-09-26 16:37:55 +0100
commit5b5e1b90087bf4aab7b52fd0676fe7a30db84bfe (patch)
tree05d63617898d58a5d7f87f4ecd9b3eb2c9fc7437 /scripts
parentc54076ed8ae5c82d13a7edf4a14cbc1bb6eac658 (diff)
downloadpoky-5b5e1b90087bf4aab7b52fd0676fe7a30db84bfe.tar.gz
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>
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/relocate_sdk.py54
1 files changed, 35 insertions, 19 deletions
diff --git a/scripts/relocate_sdk.py b/scripts/relocate_sdk.py
index 1d7bbb3849..fe6e4e0320 100755
--- a/scripts/relocate_sdk.py
+++ b/scripts/relocate_sdk.py
@@ -31,7 +31,7 @@ import os
31import re 31import re
32import errno 32import errno
33 33
34old_prefix = re.compile("##DEFAULT_INSTALL_DIR##") 34old_prefix = re.compile(b"##DEFAULT_INSTALL_DIR##")
35 35
36def get_arch(): 36def get_arch():
37 f.seek(0) 37 f.seek(0)
@@ -92,12 +92,15 @@ def change_interpreter(elf_file_name):
92 # External SDKs with mixed pre-compiled binaries should not get 92 # External SDKs with mixed pre-compiled binaries should not get
93 # relocated so look for some variant of /lib 93 # relocated so look for some variant of /lib
94 fname = f.read(11) 94 fname = f.read(11)
95 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/"): 95 if fname.startswith(b"/lib/") or fname.startswith(b"/lib64/") or \
96 fname.startswith(b"/lib32/") or fname.startswith(b"/usr/lib32/") or \
97 fname.startswith(b"/usr/lib32/") or fname.startswith(b"/usr/lib64/"):
96 break 98 break
97 if (len(new_dl_path) >= p_filesz): 99 if (len(new_dl_path) >= p_filesz):
98 print "ERROR: could not relocate %s, interp size = %i and %i is needed." % (elf_file_name, p_memsz, len(new_dl_path) + 1) 100 print("ERROR: could not relocate %s, interp size = %i and %i is needed." \
101 % (elf_file_name, p_memsz, len(new_dl_path) + 1))
99 break 102 break
100 dl_path = new_dl_path + "\0" * (p_filesz - len(new_dl_path)) 103 dl_path = new_dl_path + b"\0" * (p_filesz - len(new_dl_path))
101 f.seek(p_offset) 104 f.seek(p_offset)
102 f.write(dl_path) 105 f.write(dl_path)
103 break 106 break
@@ -129,40 +132,40 @@ def change_dl_sysdirs():
129 sh_name, sh_type, sh_flags, sh_addr, sh_offset, sh_size, sh_link,\ 132 sh_name, sh_type, sh_flags, sh_addr, sh_offset, sh_size, sh_link,\
130 sh_info, sh_addralign, sh_entsize = struct.unpack(sh_fmt, sh_hdr) 133 sh_info, sh_addralign, sh_entsize = struct.unpack(sh_fmt, sh_hdr)
131 134
132 name = sh_strtab[sh_name:sh_strtab.find("\0", sh_name)] 135 name = sh_strtab[sh_name:sh_strtab.find(b"\0", sh_name)]
133 136
134 """ look only into SHT_PROGBITS sections """ 137 """ look only into SHT_PROGBITS sections """
135 if sh_type == 1: 138 if sh_type == 1:
136 f.seek(sh_offset) 139 f.seek(sh_offset)
137 """ default library paths cannot be changed on the fly because """ 140 """ default library paths cannot be changed on the fly because """
138 """ the string lengths have to be changed too. """ 141 """ the string lengths have to be changed too. """
139 if name == ".sysdirs": 142 if name == b".sysdirs":
140 sysdirs = f.read(sh_size) 143 sysdirs = f.read(sh_size)
141 sysdirs_off = sh_offset 144 sysdirs_off = sh_offset
142 sysdirs_sect_size = sh_size 145 sysdirs_sect_size = sh_size
143 elif name == ".sysdirslen": 146 elif name == b".sysdirslen":
144 sysdirslen = f.read(sh_size) 147 sysdirslen = f.read(sh_size)
145 sysdirslen_off = sh_offset 148 sysdirslen_off = sh_offset
146 elif name == ".ldsocache": 149 elif name == b".ldsocache":
147 ldsocache_path = f.read(sh_size) 150 ldsocache_path = f.read(sh_size)
148 new_ldsocache_path = old_prefix.sub(new_prefix, ldsocache_path) 151 new_ldsocache_path = old_prefix.sub(new_prefix, ldsocache_path)
149 # pad with zeros 152 # pad with zeros
150 new_ldsocache_path += "\0" * (sh_size - len(new_ldsocache_path)) 153 new_ldsocache_path += b"\0" * (sh_size - len(new_ldsocache_path))
151 # write it back 154 # write it back
152 f.seek(sh_offset) 155 f.seek(sh_offset)
153 f.write(new_ldsocache_path) 156 f.write(new_ldsocache_path)
154 157
155 if sysdirs != "" and sysdirslen != "": 158 if sysdirs != "" and sysdirslen != "":
156 paths = sysdirs.split("\0") 159 paths = sysdirs.split(b"\0")
157 sysdirs = "" 160 sysdirs = b""
158 sysdirslen = "" 161 sysdirslen = b""
159 for path in paths: 162 for path in paths:
160 """ exit the loop when we encounter first empty string """ 163 """ exit the loop when we encounter first empty string """
161 if path == "": 164 if path == b"":
162 break 165 break
163 166
164 new_path = old_prefix.sub(new_prefix, path) 167 new_path = old_prefix.sub(new_prefix, path)
165 sysdirs += new_path + "\0" 168 sysdirs += new_path + b"\0"
166 169
167 if arch == 32: 170 if arch == 32:
168 sysdirslen += struct.pack("<L", len(new_path)) 171 sysdirslen += struct.pack("<L", len(new_path))
@@ -170,7 +173,7 @@ def change_dl_sysdirs():
170 sysdirslen += struct.pack("<Q", len(new_path)) 173 sysdirslen += struct.pack("<Q", len(new_path))
171 174
172 """ pad with zeros """ 175 """ pad with zeros """
173 sysdirs += "\0" * (sysdirs_sect_size - len(sysdirs)) 176 sysdirs += b"\0" * (sysdirs_sect_size - len(sysdirs))
174 177
175 """ write the sections back """ 178 """ write the sections back """
176 f.seek(sysdirs_off) 179 f.seek(sysdirs_off)
@@ -178,13 +181,19 @@ def change_dl_sysdirs():
178 f.seek(sysdirslen_off) 181 f.seek(sysdirslen_off)
179 f.write(sysdirslen) 182 f.write(sysdirslen)
180 183
181
182# MAIN 184# MAIN
183if len(sys.argv) < 4: 185if len(sys.argv) < 4:
184 sys.exit(-1) 186 sys.exit(-1)
185 187
186new_prefix = sys.argv[1] 188# In python > 3, strings may also contain Unicode characters. So, convert
187new_dl_path = sys.argv[2] 189# them to bytes
190if sys.version_info < (3,):
191 new_prefix = sys.argv[1]
192 new_dl_path = sys.argv[2]
193else:
194 new_prefix = sys.argv[1].encode()
195 new_dl_path = sys.argv[2].encode()
196
188executables_list = sys.argv[3:] 197executables_list = sys.argv[3:]
189 198
190for e in executables_list: 199for e in executables_list:
@@ -196,7 +205,7 @@ for e in executables_list:
196 205
197 try: 206 try:
198 f = open(e, "r+b") 207 f = open(e, "r+b")
199 except IOError, ioex: 208 except IOError as ioex:
200 if ioex.errno == errno.ETXTBSY: 209 if ioex.errno == errno.ETXTBSY:
201 print("Could not open %s. File used by another process.\nPlease "\ 210 print("Could not open %s. File used by another process.\nPlease "\
202 "make sure you exit all processes that might use any SDK "\ 211 "make sure you exit all processes that might use any SDK "\
@@ -205,6 +214,9 @@ for e in executables_list:
205 print("Could not open %s: %s(%d)" % (e, ioex.strerror, ioex.errno)) 214 print("Could not open %s: %s(%d)" % (e, ioex.strerror, ioex.errno))
206 sys.exit(-1) 215 sys.exit(-1)
207 216
217 # Save old size and do a size check at the end. Just a safety measure.
218 old_size = os.path.getsize(e)
219
208 arch = get_arch() 220 arch = get_arch()
209 if arch: 221 if arch:
210 parse_elf_header() 222 parse_elf_header()
@@ -217,3 +229,7 @@ for e in executables_list:
217 229
218 f.close() 230 f.close()
219 231
232 if old_size != os.path.getsize(e):
233 print("New file size for %s is different. Looks like a relocation error!", e)
234 sys.exit(-1)
235