summaryrefslogtreecommitdiffstats
path: root/scripts/relocate_sdk.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/relocate_sdk.py')
-rwxr-xr-xscripts/relocate_sdk.py35
1 files changed, 24 insertions, 11 deletions
diff --git a/scripts/relocate_sdk.py b/scripts/relocate_sdk.py
index 8c0fdb986a..8a728720ba 100755
--- a/scripts/relocate_sdk.py
+++ b/scripts/relocate_sdk.py
@@ -30,9 +30,16 @@ else:
30old_prefix = re.compile(b("##DEFAULT_INSTALL_DIR##")) 30old_prefix = re.compile(b("##DEFAULT_INSTALL_DIR##"))
31 31
32def get_arch(): 32def get_arch():
33 global endian_prefix
33 f.seek(0) 34 f.seek(0)
34 e_ident =f.read(16) 35 e_ident =f.read(16)
35 ei_mag0,ei_mag1_3,ei_class = struct.unpack("<B3sB11x", e_ident) 36 ei_mag0,ei_mag1_3,ei_class,ei_data,ei_version = struct.unpack("<B3sBBB9x", e_ident)
37
38 # ei_data = 1 for little-endian & 0 for big-endian
39 if ei_data == 1:
40 endian_prefix = '<'
41 else:
42 endian_prefix = '>'
36 43
37 if (ei_mag0 != 0x7f and ei_mag1_3 != "ELF") or ei_class == 0: 44 if (ei_mag0 != 0x7f and ei_mag1_3 != "ELF") or ei_class == 0:
38 return 0 45 return 0
@@ -51,11 +58,11 @@ def parse_elf_header():
51 58
52 if arch == 32: 59 if arch == 32:
53 # 32bit 60 # 32bit
54 hdr_fmt = "<HHILLLIHHHHHH" 61 hdr_fmt = endian_prefix + "HHILLLIHHHHHH"
55 hdr_size = 52 62 hdr_size = 52
56 else: 63 else:
57 # 64bit 64 # 64bit
58 hdr_fmt = "<HHIQQQIHHHHHH" 65 hdr_fmt = endian_prefix + "HHIQQQIHHHHHH"
59 hdr_size = 64 66 hdr_size = 64
60 67
61 e_type, e_machine, e_version, e_entry, e_phoff, e_shoff, e_flags,\ 68 e_type, e_machine, e_version, e_entry, e_phoff, e_shoff, e_flags,\
@@ -64,9 +71,9 @@ def parse_elf_header():
64 71
65def change_interpreter(elf_file_name): 72def change_interpreter(elf_file_name):
66 if arch == 32: 73 if arch == 32:
67 ph_fmt = "<IIIIIIII" 74 ph_fmt = endian_prefix + "IIIIIIII"
68 else: 75 else:
69 ph_fmt = "<IIQQQQQQ" 76 ph_fmt = endian_prefix + "IIQQQQQQ"
70 77
71 """ look for PT_INTERP section """ 78 """ look for PT_INTERP section """
72 for i in range(0,e_phnum): 79 for i in range(0,e_phnum):
@@ -97,25 +104,26 @@ def change_interpreter(elf_file_name):
97 if (len(new_dl_path) >= p_filesz): 104 if (len(new_dl_path) >= p_filesz):
98 print("ERROR: could not relocate %s, interp size = %i and %i is needed." \ 105 print("ERROR: could not relocate %s, interp size = %i and %i is needed." \
99 % (elf_file_name, p_memsz, len(new_dl_path) + 1)) 106 % (elf_file_name, p_memsz, len(new_dl_path) + 1))
100 break 107 return False
101 dl_path = new_dl_path + b("\0") * (p_filesz - len(new_dl_path)) 108 dl_path = new_dl_path + b("\0") * (p_filesz - len(new_dl_path))
102 f.seek(p_offset) 109 f.seek(p_offset)
103 f.write(dl_path) 110 f.write(dl_path)
104 break 111 break
112 return True
105 113
106def change_dl_sysdirs(elf_file_name): 114def change_dl_sysdirs(elf_file_name):
107 if arch == 32: 115 if arch == 32:
108 sh_fmt = "<IIIIIIIIII" 116 sh_fmt = endian_prefix + "IIIIIIIIII"
109 else: 117 else:
110 sh_fmt = "<IIQQQQIIQQ" 118 sh_fmt = endian_prefix + "IIQQQQIIQQ"
111 119
112 """ read section string table """ 120 """ read section string table """
113 f.seek(e_shoff + e_shstrndx * e_shentsize) 121 f.seek(e_shoff + e_shstrndx * e_shentsize)
114 sh_hdr = f.read(e_shentsize) 122 sh_hdr = f.read(e_shentsize)
115 if arch == 32: 123 if arch == 32:
116 sh_offset, sh_size = struct.unpack("<16xII16x", sh_hdr) 124 sh_offset, sh_size = struct.unpack(endian_prefix + "16xII16x", sh_hdr)
117 else: 125 else:
118 sh_offset, sh_size = struct.unpack("<24xQQ24x", sh_hdr) 126 sh_offset, sh_size = struct.unpack(endian_prefix + "24xQQ24x", sh_hdr)
119 127
120 f.seek(sh_offset) 128 f.seek(sh_offset)
121 sh_strtab = f.read(sh_size) 129 sh_strtab = f.read(sh_size)
@@ -215,6 +223,7 @@ else:
215 223
216executables_list = sys.argv[3:] 224executables_list = sys.argv[3:]
217 225
226errors = False
218for e in executables_list: 227for e in executables_list:
219 perms = os.stat(e)[stat.ST_MODE] 228 perms = os.stat(e)[stat.ST_MODE]
220 if os.access(e, os.W_OK|os.R_OK): 229 if os.access(e, os.W_OK|os.R_OK):
@@ -240,7 +249,8 @@ for e in executables_list:
240 arch = get_arch() 249 arch = get_arch()
241 if arch: 250 if arch:
242 parse_elf_header() 251 parse_elf_header()
243 change_interpreter(e) 252 if not change_interpreter(e):
253 errors = True
244 change_dl_sysdirs(e) 254 change_dl_sysdirs(e)
245 255
246 """ change permissions back """ 256 """ change permissions back """
@@ -253,3 +263,6 @@ for e in executables_list:
253 print("New file size for %s is different. Looks like a relocation error!", e) 263 print("New file size for %s is different. Looks like a relocation error!", e)
254 sys.exit(-1) 264 sys.exit(-1)
255 265
266if errors:
267 print("Relocation of one or more executables failed.")
268 sys.exit(-1)