qa.py 5.0 KB

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