relocate_sdk.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #!/usr/bin/env python
  2. #
  3. # Copyright (c) 2012 Intel Corporation
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License version 2 as
  7. # published by the Free Software Foundation.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. # See the GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. #
  18. # DESCRIPTION
  19. # This script is called by the SDK installer script. It replaces the dynamic
  20. # loader path in all binaries and also fixes the SYSDIR paths/lengths and the
  21. # location of ld.so.cache in the dynamic loader binary
  22. #
  23. # AUTHORS
  24. # Laurentiu Palcu <laurentiu.palcu@intel.com>
  25. #
  26. import struct
  27. import sys
  28. import stat
  29. import os
  30. import re
  31. import errno
  32. old_prefix = re.compile("##DEFAULT_INSTALL_DIR##")
  33. def get_arch():
  34. f.seek(0)
  35. e_ident =f.read(16)
  36. ei_mag0,ei_mag1_3,ei_class = struct.unpack("<B3sB11x", e_ident)
  37. if (ei_mag0 != 0x7f and ei_mag1_3 != "ELF") or ei_class == 0:
  38. return 0
  39. if ei_class == 1:
  40. return 32
  41. elif ei_class == 2:
  42. return 64
  43. def parse_elf_header():
  44. global e_type, e_machine, e_version, e_entry, e_phoff, e_shoff, e_flags,\
  45. e_ehsize, e_phentsize, e_phnum, e_shentsize, e_shnum, e_shstrndx
  46. f.seek(0)
  47. elf_header = f.read(64)
  48. if arch == 32:
  49. # 32bit
  50. hdr_struct = struct.Struct("<HHILLLIHHHHHH")
  51. hdr_size = 52
  52. else:
  53. # 64bit
  54. hdr_struct = struct.Struct("<HHIQQQIHHHHHH")
  55. hdr_size = 64
  56. e_type, e_machine, e_version, e_entry, e_phoff, e_shoff, e_flags,\
  57. e_ehsize, e_phentsize, e_phnum, e_shentsize, e_shnum, e_shstrndx =\
  58. hdr_struct.unpack(elf_header[16:hdr_size])
  59. def change_interpreter():
  60. if arch == 32:
  61. ph_struct = struct.Struct("<IIIIIIII")
  62. else:
  63. ph_struct = struct.Struct("<IIQQQQQQ")
  64. """ look for PT_INTERP section """
  65. for i in range(0,e_phnum):
  66. f.seek(e_phoff + i * e_phentsize)
  67. ph_hdr = f.read(e_phentsize)
  68. if arch == 32:
  69. # 32bit
  70. p_type, p_offset, p_vaddr, p_paddr, p_filesz,\
  71. p_memsz, p_flags, p_align = ph_struct.unpack(ph_hdr)
  72. else:
  73. # 64bit
  74. p_type, p_flags, p_offset, p_vaddr, p_paddr, \
  75. p_filesz, p_memsz, p_align = ph_struct.unpack(ph_hdr)
  76. """ change interpreter """
  77. if p_type == 3:
  78. # PT_INTERP section
  79. f.seek(p_offset)
  80. dl_path = new_dl_path + "\0" * (p_filesz - len(new_dl_path))
  81. f.write(dl_path)
  82. break
  83. def change_dl_sysdirs():
  84. if arch == 32:
  85. sh_struct = struct.Struct("<IIIIIIIIII")
  86. else:
  87. sh_struct = struct.Struct("<IIQQQQIIQQ")
  88. """ read section string table """
  89. f.seek(e_shoff + e_shstrndx * e_shentsize)
  90. sh_hdr = f.read(e_shentsize)
  91. if arch == 32:
  92. sh_offset, sh_size = struct.unpack("<16xII16x", sh_hdr)
  93. else:
  94. sh_offset, sh_size = struct.unpack("<24xQQ24x", sh_hdr)
  95. f.seek(sh_offset)
  96. sh_strtab = f.read(sh_size)
  97. sysdirs = sysdirs_len = ""
  98. """ change ld.so.cache path and default libs path for dynamic loader """
  99. for i in range(0,e_shnum):
  100. f.seek(e_shoff + i * e_shentsize)
  101. sh_hdr = f.read(e_shentsize)
  102. sh_name, sh_type, sh_flags, sh_addr, sh_offset, sh_size, sh_link,\
  103. sh_info, sh_addralign, sh_entsize = sh_struct.unpack(sh_hdr)
  104. name = sh_strtab[sh_name:sh_strtab.find("\0", sh_name)]
  105. """ look only into SHT_PROGBITS sections """
  106. if sh_type == 1:
  107. f.seek(sh_offset)
  108. """ default library paths cannot be changed on the fly because """
  109. """ the string lengths have to be changed too. """
  110. if name == ".sysdirs":
  111. sysdirs = f.read(sh_size)
  112. sysdirs_off = sh_offset
  113. sysdirs_sect_size = sh_size
  114. elif name == ".sysdirslen":
  115. sysdirslen = f.read(sh_size)
  116. sysdirslen_off = sh_offset
  117. elif name == ".ldsocache":
  118. ldsocache_path = f.read(sh_size)
  119. new_ldsocache_path = old_prefix.sub(new_prefix, ldsocache_path)
  120. # pad with zeros
  121. new_ldsocache_path += "\0" * (sh_size - len(new_ldsocache_path))
  122. # write it back
  123. f.seek(sh_offset)
  124. f.write(new_ldsocache_path)
  125. if sysdirs != "" and sysdirslen != "":
  126. paths = sysdirs.split("\0")
  127. sysdirs = ""
  128. sysdirslen = ""
  129. for path in paths:
  130. """ exit the loop when we encounter first empty string """
  131. if path == "":
  132. break
  133. new_path = old_prefix.sub(new_prefix, path)
  134. sysdirs += new_path + "\0"
  135. if arch == 32:
  136. sysdirslen += struct.pack("<L", len(new_path))
  137. else:
  138. sysdirslen += struct.pack("<Q", len(new_path))
  139. """ pad with zeros """
  140. sysdirs += "\0" * (sysdirs_sect_size - len(sysdirs))
  141. """ write the sections back """
  142. f.seek(sysdirs_off)
  143. f.write(sysdirs)
  144. f.seek(sysdirslen_off)
  145. f.write(sysdirslen)
  146. # MAIN
  147. if len(sys.argv) < 4:
  148. exit(-1)
  149. new_prefix = sys.argv[1]
  150. new_dl_path = sys.argv[2]
  151. executables_list = sys.argv[3:]
  152. for e in executables_list:
  153. perms = os.stat(e)[stat.ST_MODE]
  154. if os.access(e, os.W_OK|os.R_OK):
  155. perms = None
  156. else:
  157. os.chmod(e, perms|stat.S_IRWXU)
  158. try:
  159. f = open(e, "r+b")
  160. except IOError as ioex:
  161. if ioex.errno == errno.ETXTBSY:
  162. print("Could not open %s. File used by another process.\nPlease "\
  163. "make sure you exit all processes that might use any SDK "\
  164. "binaries." % e)
  165. else:
  166. print("Could not open %s: %s(%d)" % (e, ioex.strerror, ioex.errno))
  167. exit(-1)
  168. arch = get_arch()
  169. if arch:
  170. parse_elf_header()
  171. change_interpreter()
  172. change_dl_sysdirs()
  173. """ change permissions back """
  174. if perms:
  175. os.chmod(e, perms)
  176. f.close()