qa.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import os, struct, mmap
  2. class NotELFFileError(Exception):
  3. pass
  4. class ELFFile:
  5. EI_NIDENT = 16
  6. EI_CLASS = 4
  7. EI_DATA = 5
  8. EI_VERSION = 6
  9. EI_OSABI = 7
  10. EI_ABIVERSION = 8
  11. E_MACHINE = 0x12
  12. # possible values for EI_CLASS
  13. ELFCLASSNONE = 0
  14. ELFCLASS32 = 1
  15. ELFCLASS64 = 2
  16. # possible value for EI_VERSION
  17. EV_CURRENT = 1
  18. # possible values for EI_DATA
  19. EI_DATA_NONE = 0
  20. EI_DATA_LSB = 1
  21. EI_DATA_MSB = 2
  22. PT_INTERP = 3
  23. def my_assert(self, expectation, result):
  24. if not expectation == result:
  25. #print "'%x','%x' %s" % (ord(expectation), ord(result), self.name)
  26. raise NotELFFileError("%s is not an ELF" % self.name)
  27. def __init__(self, name):
  28. self.name = name
  29. self.objdump_output = {}
  30. # Context Manager functions to close the mmap explicitly
  31. def __enter__(self):
  32. return self
  33. def __exit__(self, exc_type, exc_value, traceback):
  34. self.data.close()
  35. def open(self):
  36. with open(self.name, "rb") as f:
  37. try:
  38. self.data = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
  39. except ValueError:
  40. # This means the file is empty
  41. raise NotELFFileError("%s is empty" % self.name)
  42. # Check the file has the minimum number of ELF table entries
  43. if len(self.data) < ELFFile.EI_NIDENT + 4:
  44. raise NotELFFileError("%s is not an ELF" % self.name)
  45. # ELF header
  46. self.my_assert(self.data[0], 0x7f)
  47. self.my_assert(self.data[1], ord('E'))
  48. self.my_assert(self.data[2], ord('L'))
  49. self.my_assert(self.data[3], ord('F'))
  50. if self.data[ELFFile.EI_CLASS] == ELFFile.ELFCLASS32:
  51. self.bits = 32
  52. elif self.data[ELFFile.EI_CLASS] == ELFFile.ELFCLASS64:
  53. self.bits = 64
  54. else:
  55. # Not 32-bit or 64.. lets assert
  56. raise NotELFFileError("ELF but not 32 or 64 bit.")
  57. self.my_assert(self.data[ELFFile.EI_VERSION], ELFFile.EV_CURRENT)
  58. self.endian = self.data[ELFFile.EI_DATA]
  59. if self.endian not in (ELFFile.EI_DATA_LSB, ELFFile.EI_DATA_MSB):
  60. raise NotELFFileError("Unexpected EI_DATA %x" % self.endian)
  61. def osAbi(self):
  62. return self.data[ELFFile.EI_OSABI]
  63. def abiVersion(self):
  64. return self.data[ELFFile.EI_ABIVERSION]
  65. def abiSize(self):
  66. return self.bits
  67. def isLittleEndian(self):
  68. return self.endian == ELFFile.EI_DATA_LSB
  69. def isBigEndian(self):
  70. return self.endian == ELFFile.EI_DATA_MSB
  71. def getStructEndian(self):
  72. return {ELFFile.EI_DATA_LSB: "<",
  73. ELFFile.EI_DATA_MSB: ">"}[self.endian]
  74. def getShort(self, offset):
  75. return struct.unpack_from(self.getStructEndian() + "H", self.data, offset)[0]
  76. def getWord(self, offset):
  77. return struct.unpack_from(self.getStructEndian() + "i", self.data, offset)[0]
  78. def isDynamic(self):
  79. """
  80. Return True if there is a .interp segment (therefore dynamically
  81. linked), otherwise False (statically linked).
  82. """
  83. offset = self.getWord(self.bits == 32 and 0x1C or 0x20)
  84. size = self.getShort(self.bits == 32 and 0x2A or 0x36)
  85. count = self.getShort(self.bits == 32 and 0x2C or 0x38)
  86. for i in range(0, count):
  87. p_type = self.getWord(offset + i * size)
  88. if p_type == ELFFile.PT_INTERP:
  89. return True
  90. return False
  91. def machine(self):
  92. """
  93. We know the endian stored in self.endian and we
  94. know the position
  95. """
  96. return self.getShort(ELFFile.E_MACHINE)
  97. def run_objdump(self, cmd, d):
  98. import bb.process
  99. import sys
  100. if cmd in self.objdump_output:
  101. return self.objdump_output[cmd]
  102. objdump = d.getVar('OBJDUMP')
  103. env = os.environ.copy()
  104. env["LC_ALL"] = "C"
  105. env["PATH"] = d.getVar('PATH')
  106. try:
  107. bb.note("%s %s %s" % (objdump, cmd, self.name))
  108. self.objdump_output[cmd] = bb.process.run([objdump, cmd, self.name], env=env, shell=False)[0]
  109. return self.objdump_output[cmd]
  110. except Exception as e:
  111. bb.note("%s %s %s failed: %s" % (objdump, cmd, self.name, e))
  112. return ""
  113. def elf_machine_to_string(machine):
  114. """
  115. Return the name of a given ELF e_machine field or the hex value as a string
  116. if it isn't recognised.
  117. """
  118. try:
  119. return {
  120. 0x02: "SPARC",
  121. 0x03: "x86",
  122. 0x08: "MIPS",
  123. 0x14: "PowerPC",
  124. 0x28: "ARM",
  125. 0x2A: "SuperH",
  126. 0x32: "IA-64",
  127. 0x3E: "x86-64",
  128. 0xB7: "AArch64",
  129. 0xF7: "BPF"
  130. }[machine]
  131. except:
  132. return "Unknown (%s)" % repr(machine)
  133. if __name__ == "__main__":
  134. import sys
  135. with ELFFile(sys.argv[1]) as elf:
  136. elf.open()
  137. print(elf.isDynamic())