glibcelf.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. #!/usr/bin/python3
  2. # ELF support functionality for Python.
  3. # Copyright (C) 2022-2026 Free Software Foundation, Inc.
  4. # This file is part of the GNU C Library.
  5. #
  6. # The GNU C Library is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU Lesser General Public
  8. # License as published by the Free Software Foundation; either
  9. # version 2.1 of the License, or (at your option) any later version.
  10. #
  11. # The GNU C Library is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. # Lesser General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Lesser General Public
  17. # License along with the GNU C Library; if not, see
  18. # <https://www.gnu.org/licenses/>.
  19. """Basic ELF parser.
  20. Use Image.readfile(path) to read an ELF file into memory and begin
  21. parsing it.
  22. """
  23. import collections
  24. import functools
  25. import os
  26. import struct
  27. import glibcpp
  28. class _MetaNamedValue(type):
  29. """Used to set up _NamedValue subclasses."""
  30. @classmethod
  31. def __prepare__(metacls, cls, bases, **kwds):
  32. # Indicates an int-based class. Needed for types like Shn.
  33. int_based = False
  34. for base in bases:
  35. if issubclass(base, int):
  36. int_based = int
  37. break
  38. return dict(by_value={},
  39. by_name={},
  40. prefix=None,
  41. _int_based=int_based)
  42. def __contains__(self, other):
  43. return other in self.by_value
  44. class _NamedValue(metaclass=_MetaNamedValue):
  45. """Typed, named integer constants.
  46. Constants have the following instance attributes:
  47. name: The full name of the constant (e.g., "PT_NULL").
  48. short_name: The name with of the constant without the prefix ("NULL").
  49. value: The integer value of the constant.
  50. The following class attributes are available:
  51. by_value: A dict mapping integers to constants.
  52. by_name: A dict mapping strings to constants.
  53. prefix: A string that is removed from the start of short names, or None.
  54. """
  55. def __new__(cls, arg0, arg1=None):
  56. """Instance creation.
  57. For the one-argument form, the argument must be a string, an
  58. int, or an instance of this class. Strings are looked up via
  59. by_name. Values are looked up via by_value; if value lookup
  60. fails, a new unnamed instance is returned. Instances of this
  61. class a re returned as-is.
  62. The two-argument form expects the name (a string) and the
  63. value (an integer). A new instance is created in this case.
  64. The instance is not registered in the by_value/by_name
  65. dictionaries (but the caller can do that).
  66. """
  67. typ0 = type(arg0)
  68. if arg1 is None:
  69. if isinstance(typ0, cls):
  70. # Re-use the existing object.
  71. return arg0
  72. if typ0 is int:
  73. by_value = cls.by_value
  74. try:
  75. return by_value[arg0]
  76. except KeyError:
  77. # Create a new object of the requested value.
  78. if cls._int_based:
  79. result = int.__new__(cls, arg0)
  80. else:
  81. result = object.__new__(cls)
  82. result.value = arg0
  83. result.name = None
  84. return result
  85. if typ0 is str:
  86. by_name = cls.by_name
  87. try:
  88. return by_name[arg0]
  89. except KeyError:
  90. raise ValueError('unknown {} constant: {!r}'.format(
  91. cls.__name__, arg0))
  92. else:
  93. # Types for the two-argument form are rigid.
  94. if typ0 is not str and typ0 is not None:
  95. raise ValueError('type {} of name {!r} should be str'.format(
  96. typ0.__name__, arg0))
  97. if type(arg1) is not int:
  98. raise ValueError('type {} of value {!r} should be int'.format(
  99. type(arg1).__name__, arg1))
  100. # Create a new named constants.
  101. if cls._int_based:
  102. result = int.__new__(cls, arg1)
  103. else:
  104. result = object.__new__(cls)
  105. result.value = arg1
  106. result.name = arg0
  107. # Set up the short_name attribute.
  108. prefix = cls.prefix
  109. if prefix and arg0.startswith(prefix):
  110. result.short_name = arg0[len(prefix):]
  111. else:
  112. result.short_name = arg0
  113. return result
  114. def __str__(self):
  115. name = self.name
  116. if name:
  117. return name
  118. else:
  119. return str(self.value)
  120. def __repr__(self):
  121. name = self.name
  122. if name:
  123. return name
  124. else:
  125. return '{}({})'.format(self.__class__.__name__, self.value)
  126. def __setattr__(self, name, value):
  127. # Prevent modification of the critical attributes once they
  128. # have been set.
  129. if name in ('name', 'value', 'short_name') and hasattr(self, name):
  130. raise AttributeError('can\'t set attribute {}'.format(name))
  131. object.__setattr__(self, name, value)
  132. @functools.total_ordering
  133. class _TypedConstant(_NamedValue):
  134. """Base class for integer-valued optionally named constants.
  135. This type is not an integer type.
  136. """
  137. def __eq__(self, other):
  138. return isinstance(other, self.__class__) and self.value == other.value
  139. def __lt__(self, other):
  140. return isinstance(other, self.__class__) and self.value <= other.value
  141. def __hash__(self):
  142. return hash(self.value)
  143. class _IntConstant(_NamedValue, int):
  144. """Base class for integer-like optionally named constants.
  145. Instances compare equal to the integer of the same value, and can
  146. be used in integer arithmetic.
  147. """
  148. pass
  149. class _FlagConstant(_TypedConstant, int):
  150. pass
  151. def _parse_elf_h():
  152. """Read ../elf/elf.h and return a dict with the constants in it."""
  153. path = os.path.join(os.path.dirname(os.path.realpath(__file__)),
  154. '..', 'elf', 'elf.h')
  155. class TokenizerReporter:
  156. """Report tokenizer errors to standard output."""
  157. def __init__(self):
  158. self.errors = 0
  159. def error(self, token, message):
  160. self.errors += 1
  161. print('{}:{}:{}: error: {}'.format(
  162. path, token.line, token.column, message))
  163. reporter = TokenizerReporter()
  164. with open(path) as inp:
  165. tokens = glibcpp.tokenize_c(inp.read(), reporter)
  166. if reporter.errors:
  167. raise IOError('parse error in elf.h')
  168. class MacroReporter:
  169. """Report macro errors to standard output."""
  170. def __init__(self):
  171. self.errors = 0
  172. def error(self, line, message):
  173. self.errors += 1
  174. print('{}:{}: error: {}'.format(path, line, message))
  175. def note(self, line, message):
  176. print('{}:{}: note: {}'.format(path, line, message))
  177. reporter = MacroReporter()
  178. result = glibcpp.macro_eval(glibcpp.macro_definitions(tokens), reporter)
  179. if reporter.errors:
  180. raise IOError('parse error in elf.h')
  181. return result
  182. _elf_h = _parse_elf_h()
  183. del _parse_elf_h
  184. _elf_h_processed = set()
  185. def _register_elf_h(cls, prefix=None, skip=(), ranges=False, parent=None):
  186. prefix = prefix or cls.prefix
  187. if not prefix:
  188. raise ValueError('missing prefix for {}'.format(cls.__name__))
  189. by_value = cls.by_value
  190. by_name = cls.by_name
  191. processed = _elf_h_processed
  192. skip = set(skip)
  193. skip.add(prefix + 'NUM')
  194. if ranges:
  195. skip.add(prefix + 'LOOS')
  196. skip.add(prefix + 'HIOS')
  197. skip.add(prefix + 'LOPROC')
  198. skip.add(prefix + 'HIPROC')
  199. cls.os_range = (_elf_h[prefix + 'LOOS'], _elf_h[prefix + 'HIOS'])
  200. cls.proc_range = (_elf_h[prefix + 'LOPROC'], _elf_h[prefix + 'HIPROC'])
  201. # Inherit the prefix from the parent if not set.
  202. if parent and cls.prefix is None and parent.prefix is not None:
  203. cls.prefix = parent.prefix
  204. processed_len_start = len(processed)
  205. for name, value in _elf_h.items():
  206. if name in skip or name in processed:
  207. continue
  208. if name.startswith(prefix):
  209. processed.add(name)
  210. if value in by_value:
  211. raise ValueError('duplicate value {}: {}, {}'.format(
  212. value, name, by_value[value]))
  213. obj = cls(name, value)
  214. by_value[value] = obj
  215. by_name[name] = obj
  216. setattr(cls, name, obj)
  217. if parent:
  218. # Make the symbolic name available through the parent as well.
  219. parent.by_name[name] = obj
  220. setattr(parent, name, obj)
  221. if len(processed) == processed_len_start:
  222. raise ValueError('nothing matched prefix {!r}'.format(prefix))
  223. class ElfClass(_TypedConstant):
  224. """ELF word size. Type of EI_CLASS values."""
  225. _register_elf_h(ElfClass, prefix='ELFCLASS')
  226. class ElfData(_TypedConstant):
  227. """ELF endianness. Type of EI_DATA values."""
  228. _register_elf_h(ElfData, prefix='ELFDATA')
  229. class Machine(_TypedConstant):
  230. """ELF machine type. Type of values in Ehdr.e_machine field."""
  231. prefix = 'EM_'
  232. _register_elf_h(Machine, skip=('EM_ARC_A5',))
  233. class Et(_TypedConstant):
  234. """ELF file type. Type of ET_* values and the Ehdr.e_type field."""
  235. prefix = 'ET_'
  236. _register_elf_h(Et, ranges=True)
  237. class Shn(_IntConstant):
  238. """ELF reserved section indices."""
  239. prefix = 'SHN_'
  240. class ShnMIPS(Shn):
  241. """Supplemental SHN_* constants for EM_MIPS."""
  242. class ShnPARISC(Shn):
  243. """Supplemental SHN_* constants for EM_PARISC."""
  244. _register_elf_h(ShnMIPS, prefix='SHN_MIPS_', parent=Shn)
  245. _register_elf_h(ShnPARISC, prefix='SHN_PARISC_', parent=Shn)
  246. _register_elf_h(Shn, skip='SHN_LORESERVE SHN_HIRESERVE'.split(), ranges=True)
  247. class Sht(_TypedConstant):
  248. """ELF section types. Type of SHT_* values."""
  249. prefix = 'SHT_'
  250. class ShtALPHA(Sht):
  251. """Supplemental SHT_* constants for EM_ALPHA."""
  252. class ShtARC(Sht):
  253. """Supplemental SHT_* constants for EM_ARC."""
  254. class ShtARM(Sht):
  255. """Supplemental SHT_* constants for EM_ARM."""
  256. class ShtCSKY(Sht):
  257. """Supplemental SHT_* constants for EM_CSKY."""
  258. class ShtIA_64(Sht):
  259. """Supplemental SHT_* constants for EM_IA_64."""
  260. class ShtMIPS(Sht):
  261. """Supplemental SHT_* constants for EM_MIPS."""
  262. class ShtPARISC(Sht):
  263. """Supplemental SHT_* constants for EM_PARISC."""
  264. class ShtRISCV(Sht):
  265. """Supplemental SHT_* constants for EM_RISCV."""
  266. _register_elf_h(ShtALPHA, prefix='SHT_ALPHA_', parent=Sht)
  267. _register_elf_h(ShtARC, prefix='SHT_ARC_', parent=Sht)
  268. _register_elf_h(ShtARM, prefix='SHT_ARM_', parent=Sht)
  269. _register_elf_h(ShtCSKY, prefix='SHT_CSKY_', parent=Sht)
  270. _register_elf_h(ShtIA_64, prefix='SHT_IA_64_', parent=Sht)
  271. _register_elf_h(ShtMIPS, prefix='SHT_MIPS_', parent=Sht)
  272. _register_elf_h(ShtPARISC, prefix='SHT_PARISC_', parent=Sht)
  273. _register_elf_h(ShtRISCV, prefix='SHT_RISCV_', parent=Sht)
  274. _register_elf_h(Sht, ranges=True,
  275. skip='SHT_LOSUNW SHT_HISUNW SHT_LOUSER SHT_HIUSER'.split())
  276. class Pf(_FlagConstant):
  277. """Program header flags. Type of Phdr.p_flags values."""
  278. prefix = 'PF_'
  279. class PfARM(Pf):
  280. """Supplemental PF_* flags for EM_ARM."""
  281. class PfHP(Pf):
  282. """Supplemental PF_* flags for HP-UX."""
  283. class PfIA_64(Pf):
  284. """Supplemental PF_* flags for EM_IA_64."""
  285. class PfMIPS(Pf):
  286. """Supplemental PF_* flags for EM_MIPS."""
  287. class PfPARISC(Pf):
  288. """Supplemental PF_* flags for EM_PARISC."""
  289. _register_elf_h(PfARM, prefix='PF_ARM_', parent=Pf)
  290. _register_elf_h(PfHP, prefix='PF_HP_', parent=Pf)
  291. _register_elf_h(PfIA_64, prefix='PF_IA_64_', parent=Pf)
  292. _register_elf_h(PfMIPS, prefix='PF_MIPS_', parent=Pf)
  293. _register_elf_h(PfPARISC, prefix='PF_PARISC_', parent=Pf)
  294. _register_elf_h(Pf, skip='PF_MASKOS PF_MASKPROC'.split())
  295. class Shf(_FlagConstant):
  296. """Section flags. Type of Shdr.sh_type values."""
  297. prefix = 'SHF_'
  298. class ShfALPHA(Shf):
  299. """Supplemental SHF_* constants for EM_ALPHA."""
  300. class ShfARM(Shf):
  301. """Supplemental SHF_* constants for EM_ARM."""
  302. class ShfIA_64(Shf):
  303. """Supplemental SHF_* constants for EM_IA_64."""
  304. class ShfMIPS(Shf):
  305. """Supplemental SHF_* constants for EM_MIPS."""
  306. class ShfPARISC(Shf):
  307. """Supplemental SHF_* constants for EM_PARISC."""
  308. _register_elf_h(ShfALPHA, prefix='SHF_ALPHA_', parent=Shf)
  309. _register_elf_h(ShfARM, prefix='SHF_ARM_', parent=Shf)
  310. _register_elf_h(ShfIA_64, prefix='SHF_IA_64_', parent=Shf)
  311. _register_elf_h(ShfMIPS, prefix='SHF_MIPS_', parent=Shf)
  312. _register_elf_h(ShfPARISC, prefix='SHF_PARISC_', parent=Shf)
  313. _register_elf_h(Shf, skip='SHF_MASKOS SHF_MASKPROC'.split())
  314. class Stb(_TypedConstant):
  315. """ELF symbol binding type."""
  316. prefix = 'STB_'
  317. _register_elf_h(Stb, ranges=True)
  318. class Stt(_TypedConstant):
  319. """ELF symbol type."""
  320. prefix = 'STT_'
  321. class SttARM(Sht):
  322. """Supplemental STT_* constants for EM_ARM."""
  323. class SttPARISC(Sht):
  324. """Supplemental STT_* constants for EM_PARISC."""
  325. class SttSPARC(Sht):
  326. """Supplemental STT_* constants for EM_SPARC."""
  327. STT_SPARC_REGISTER = 13
  328. class SttX86_64(Sht):
  329. """Supplemental STT_* constants for EM_X86_64."""
  330. _register_elf_h(SttARM, prefix='STT_ARM_', parent=Stt)
  331. _register_elf_h(SttPARISC, prefix='STT_PARISC_', parent=Stt)
  332. _register_elf_h(SttSPARC, prefix='STT_SPARC_', parent=Stt)
  333. _register_elf_h(Stt, ranges=True)
  334. class Pt(_TypedConstant):
  335. """ELF program header types. Type of Phdr.p_type."""
  336. prefix = 'PT_'
  337. class PtAARCH64(Pt):
  338. """Supplemental PT_* constants for EM_AARCH64."""
  339. class PtARM(Pt):
  340. """Supplemental PT_* constants for EM_ARM."""
  341. class PtHP(Pt):
  342. """Supplemental PT_* constants for HP-U."""
  343. class PtIA_64(Pt):
  344. """Supplemental PT_* constants for EM_IA_64."""
  345. class PtMIPS(Pt):
  346. """Supplemental PT_* constants for EM_MIPS."""
  347. class PtPARISC(Pt):
  348. """Supplemental PT_* constants for EM_PARISC."""
  349. class PtRISCV(Pt):
  350. """Supplemental PT_* constants for EM_RISCV."""
  351. _register_elf_h(PtAARCH64, prefix='PT_AARCH64_', parent=Pt)
  352. _register_elf_h(PtARM, prefix='PT_ARM_', parent=Pt)
  353. _register_elf_h(PtHP, prefix='PT_HP_', parent=Pt)
  354. _register_elf_h(PtIA_64, prefix='PT_IA_64_', parent=Pt)
  355. _register_elf_h(PtMIPS, prefix='PT_MIPS_', parent=Pt)
  356. _register_elf_h(PtPARISC, prefix='PT_PARISC_', parent=Pt)
  357. _register_elf_h(PtRISCV, prefix='PT_RISCV_', parent=Pt)
  358. _register_elf_h(Pt, skip='PT_LOSUNW PT_HISUNW'.split(), ranges=True)
  359. class Dt(_TypedConstant):
  360. """ELF dynamic segment tags. Type of Dyn.d_val."""
  361. prefix = 'DT_'
  362. class DtAARCH64(Dt):
  363. """Supplemental DT_* constants for EM_AARCH64."""
  364. class DtALPHA(Dt):
  365. """Supplemental DT_* constants for EM_ALPHA."""
  366. class DtALTERA_NIOS2(Dt):
  367. """Supplemental DT_* constants for EM_ALTERA_NIOS2."""
  368. class DtIA_64(Dt):
  369. """Supplemental DT_* constants for EM_IA_64."""
  370. class DtMIPS(Dt):
  371. """Supplemental DT_* constants for EM_MIPS."""
  372. class DtPPC(Dt):
  373. """Supplemental DT_* constants for EM_PPC."""
  374. class DtPPC64(Dt):
  375. """Supplemental DT_* constants for EM_PPC64."""
  376. class DtRISCV(Dt):
  377. """Supplemental DT_* constants for EM_RISCV."""
  378. class DtSPARC(Dt):
  379. """Supplemental DT_* constants for EM_SPARC."""
  380. class DtX86_64(Dt):
  381. """Supplemental DT_* constants for EM_X86_64."""
  382. _dt_skip = '''
  383. DT_ENCODING DT_PROCNUM
  384. DT_ADDRRNGLO DT_ADDRRNGHI DT_ADDRNUM
  385. DT_VALRNGLO DT_VALRNGHI DT_VALNUM
  386. DT_VERSIONTAGNUM DT_EXTRANUM
  387. DT_AARCH64_NUM
  388. DT_ALPHA_NUM
  389. DT_IA_64_NUM
  390. DT_MIPS_NUM
  391. DT_PPC_NUM
  392. DT_PPC64_NUM
  393. DT_SPARC_NUM
  394. DT_X86_64_NUM
  395. '''.strip().split()
  396. _register_elf_h(DtAARCH64, prefix='DT_AARCH64_', skip=_dt_skip, parent=Dt)
  397. _register_elf_h(DtALPHA, prefix='DT_ALPHA_', skip=_dt_skip, parent=Dt)
  398. _register_elf_h(DtALTERA_NIOS2, prefix='DT_NIOS2_', skip=_dt_skip, parent=Dt)
  399. _register_elf_h(DtIA_64, prefix='DT_IA_64_', skip=_dt_skip, parent=Dt)
  400. _register_elf_h(DtMIPS, prefix='DT_MIPS_', skip=_dt_skip, parent=Dt)
  401. _register_elf_h(DtPPC, prefix='DT_PPC_', skip=_dt_skip, parent=Dt)
  402. _register_elf_h(DtPPC64, prefix='DT_PPC64_', skip=_dt_skip, parent=Dt)
  403. _register_elf_h(DtRISCV, prefix='DT_RISCV_', skip=_dt_skip, parent=Dt)
  404. _register_elf_h(DtSPARC, prefix='DT_SPARC_', skip=_dt_skip, parent=Dt)
  405. _register_elf_h(DtX86_64, prefix='DT_X86_64_', skip=_dt_skip, parent=Dt)
  406. _register_elf_h(Dt, skip=_dt_skip, ranges=True)
  407. del _dt_skip
  408. # Constant extraction is complete.
  409. del _register_elf_h
  410. del _elf_h
  411. class StInfo:
  412. """ELF symbol binding and type. Type of the Sym.st_info field."""
  413. def __init__(self, arg0, arg1=None):
  414. if isinstance(arg0, int) and arg1 is None:
  415. self.bind = Stb(arg0 >> 4)
  416. self.type = Stt(arg0 & 15)
  417. else:
  418. self.bind = Stb(arg0)
  419. self.type = Stt(arg1)
  420. def value(self):
  421. """Returns the raw value for the bind/type combination."""
  422. return (self.bind.value() << 4) | (self.type.value())
  423. # Type in an ELF file. Used for deserialization.
  424. _Layout = collections.namedtuple('_Layout', 'unpack size')
  425. def _define_layouts(baseclass: type, layout32: str, layout64: str,
  426. types=None, fields32=None):
  427. """Assign variants dict to baseclass.
  428. The variants dict is indexed by (ElfClass, ElfData) pairs, and its
  429. values are _Layout instances.
  430. """
  431. struct32 = struct.Struct(layout32)
  432. struct64 = struct.Struct(layout64)
  433. # Check that the struct formats yield the right number of components.
  434. for s in (struct32, struct64):
  435. example = s.unpack(b' ' * s.size)
  436. if len(example) != len(baseclass._fields):
  437. raise ValueError('{!r} yields wrong field count: {} != {}'.format(
  438. s.format, len(example), len(baseclass._fields)))
  439. # Check that field names in types are correct.
  440. if types is None:
  441. types = ()
  442. for n in types:
  443. if n not in baseclass._fields:
  444. raise ValueError('{} does not have field {!r}'.format(
  445. baseclass.__name__, n))
  446. if fields32 is not None \
  447. and set(fields32) != set(baseclass._fields):
  448. raise ValueError('{!r} is not a permutation of the fields {!r}'.format(
  449. fields32, baseclass._fields))
  450. def unique_name(name, used_names = (set((baseclass.__name__,))
  451. | set(baseclass._fields)
  452. | {n.__name__
  453. for n in (types or {}).values()})):
  454. """Find a name that is not used for a class or field name."""
  455. candidate = name
  456. n = 0
  457. while candidate in used_names:
  458. n += 1
  459. candidate = '{}{}'.format(name, n)
  460. used_names.add(candidate)
  461. return candidate
  462. blob_name = unique_name('blob')
  463. struct_unpack_name = unique_name('struct_unpack')
  464. comps_name = unique_name('comps')
  465. layouts = {}
  466. for (bits, elfclass, layout, fields) in (
  467. (32, ElfClass.ELFCLASS32, layout32, fields32),
  468. (64, ElfClass.ELFCLASS64, layout64, None),
  469. ):
  470. for (elfdata, structprefix, funcsuffix) in (
  471. (ElfData.ELFDATA2LSB, '<', 'LE'),
  472. (ElfData.ELFDATA2MSB, '>', 'BE'),
  473. ):
  474. env = {
  475. baseclass.__name__: baseclass,
  476. struct_unpack_name: struct.unpack,
  477. }
  478. # Add the type converters.
  479. if types:
  480. for cls in types.values():
  481. env[cls.__name__] = cls
  482. funcname = ''.join(
  483. ('unpack_', baseclass.__name__, str(bits), funcsuffix))
  484. code = '''
  485. def {funcname}({blob_name}):
  486. '''.format(funcname=funcname, blob_name=blob_name)
  487. indent = ' ' * 4
  488. unpack_call = '{}({!r}, {})'.format(
  489. struct_unpack_name, structprefix + layout, blob_name)
  490. field_names = ', '.join(baseclass._fields)
  491. if types is None and fields is None:
  492. code += '{}return {}({})\n'.format(
  493. indent, baseclass.__name__, unpack_call)
  494. else:
  495. # Destructuring tuple assignment.
  496. if fields is None:
  497. code += '{}{} = {}\n'.format(
  498. indent, field_names, unpack_call)
  499. else:
  500. # Use custom field order.
  501. code += '{}{} = {}\n'.format(
  502. indent, ', '.join(fields), unpack_call)
  503. # Perform the type conversions.
  504. for n in baseclass._fields:
  505. if n in types:
  506. code += '{}{} = {}({})\n'.format(
  507. indent, n, types[n].__name__, n)
  508. # Create the named tuple.
  509. code += '{}return {}({})\n'.format(
  510. indent, baseclass.__name__, field_names)
  511. exec(code, env)
  512. layouts[(elfclass, elfdata)] = _Layout(
  513. env[funcname], struct.calcsize(layout))
  514. baseclass.layouts = layouts
  515. # Corresponds to EI_* indices into Elf*_Ehdr.e_indent.
  516. class Ident(collections.namedtuple('Ident',
  517. 'ei_mag ei_class ei_data ei_version ei_osabi ei_abiversion ei_pad')):
  518. def __new__(cls, *args):
  519. """Construct an object from a blob or its constituent fields."""
  520. if len(args) == 1:
  521. return cls.unpack(args[0])
  522. return cls.__base__.__new__(cls, *args)
  523. @staticmethod
  524. def unpack(blob: memoryview) -> 'Ident':
  525. """Parse raws data into a tuple."""
  526. ei_mag, ei_class, ei_data, ei_version, ei_osabi, ei_abiversion, \
  527. ei_pad = struct.unpack('4s5B7s', blob)
  528. return Ident(ei_mag, ElfClass(ei_class), ElfData(ei_data),
  529. ei_version, ei_osabi, ei_abiversion, ei_pad)
  530. size = 16
  531. # Corresponds to Elf32_Ehdr and Elf64_Ehdr.
  532. Ehdr = collections.namedtuple('Ehdr',
  533. 'e_ident e_type e_machine e_version e_entry e_phoff e_shoff e_flags'
  534. + ' e_ehsize e_phentsize e_phnum e_shentsize e_shnum e_shstrndx')
  535. _define_layouts(Ehdr,
  536. layout32='16s2H5I6H',
  537. layout64='16s2HI3QI6H',
  538. types=dict(e_ident=Ident,
  539. e_machine=Machine,
  540. e_type=Et,
  541. e_shstrndx=Shn))
  542. # Corresponds to Elf32_Phdr and Elf64_Pdhr. Order follows the latter.
  543. Phdr = collections.namedtuple('Phdr',
  544. 'p_type p_flags p_offset p_vaddr p_paddr p_filesz p_memsz p_align')
  545. _define_layouts(Phdr,
  546. layout32='8I',
  547. fields32=('p_type', 'p_offset', 'p_vaddr', 'p_paddr',
  548. 'p_filesz', 'p_memsz', 'p_flags', 'p_align'),
  549. layout64='2I6Q',
  550. types=dict(p_type=Pt, p_flags=Pf))
  551. # Corresponds to Elf32_Shdr and Elf64_Shdr.
  552. class Shdr(collections.namedtuple('Shdr',
  553. 'sh_name sh_type sh_flags sh_addr sh_offset sh_size sh_link sh_info'
  554. + ' sh_addralign sh_entsize')):
  555. def resolve(self, strtab: 'StringTable') -> 'Shdr':
  556. """Resolve sh_name using a string table."""
  557. return self.__class__(strtab.get(self[0]), *self[1:])
  558. _define_layouts(Shdr,
  559. layout32='10I',
  560. layout64='2I4Q2I2Q',
  561. types=dict(sh_type=Sht,
  562. sh_flags=Shf,
  563. sh_link=Shn))
  564. # Corresponds to Elf32_Dyn and Elf64_Dyn. The nesting through the
  565. # d_un union is skipped, and d_ptr is missing (its representation in
  566. # Python would be identical to d_val).
  567. Dyn = collections.namedtuple('Dyn', 'd_tag d_val')
  568. _define_layouts(Dyn,
  569. layout32='2i',
  570. layout64='2q',
  571. types=dict(d_tag=Dt))
  572. # Corresponds to Elf32_Sym and Elf64_Sym.
  573. class Sym(collections.namedtuple('Sym',
  574. 'st_name st_info st_other st_shndx st_value st_size')):
  575. def resolve(self, strtab: 'StringTable') -> 'Sym':
  576. """Resolve st_name using a string table."""
  577. return self.__class__(strtab.get(self[0]), *self[1:])
  578. _define_layouts(Sym,
  579. layout32='3I2BH',
  580. layout64='I2BH2Q',
  581. fields32=('st_name', 'st_value', 'st_size', 'st_info',
  582. 'st_other', 'st_shndx'),
  583. types=dict(st_shndx=Shn,
  584. st_info=StInfo))
  585. # Corresponds to Elf32_Rel and Elf64_Rel.
  586. Rel = collections.namedtuple('Rel', 'r_offset r_info')
  587. _define_layouts(Rel,
  588. layout32='2I',
  589. layout64='2Q')
  590. # Corresponds to Elf32_Rel and Elf64_Rel.
  591. Rela = collections.namedtuple('Rela', 'r_offset r_info r_addend')
  592. _define_layouts(Rela,
  593. layout32='3I',
  594. layout64='3Q')
  595. class StringTable:
  596. """ELF string table."""
  597. def __init__(self, blob):
  598. """Create a new string table backed by the data in the blob.
  599. blob: a memoryview-like object
  600. """
  601. self.blob = blob
  602. def get(self, index) -> bytes:
  603. """Returns the null-terminated byte string at the index."""
  604. blob = self.blob
  605. endindex = index
  606. while True:
  607. if blob[endindex] == 0:
  608. return bytes(blob[index:endindex])
  609. endindex += 1
  610. class Image:
  611. """ELF image parser."""
  612. def __init__(self, image):
  613. """Create an ELF image from binary image data.
  614. image: a memoryview-like object that supports efficient range
  615. subscripting.
  616. """
  617. self.image = image
  618. ident = self.read(Ident, 0)
  619. classdata = (ident.ei_class, ident.ei_data)
  620. # Set self.Ehdr etc. to the subtypes with the right parsers.
  621. for typ in (Ehdr, Phdr, Shdr, Dyn, Sym, Rel, Rela):
  622. setattr(self, typ.__name__, typ.layouts.get(classdata, None))
  623. if self.Ehdr is not None:
  624. self.ehdr = self.read(self.Ehdr, 0)
  625. self._shdr_num = self._compute_shdr_num()
  626. else:
  627. self.ehdr = None
  628. self._shdr_num = 0
  629. self._section = {}
  630. self._stringtab = {}
  631. if self._shdr_num > 0:
  632. self._shdr_strtab = self._find_shdr_strtab()
  633. else:
  634. self._shdr_strtab = None
  635. @staticmethod
  636. def readfile(path: str) -> 'Image':
  637. """Reads the ELF file at the specified path."""
  638. with open(path, 'rb') as inp:
  639. return Image(memoryview(inp.read()))
  640. def _compute_shdr_num(self) -> int:
  641. """Computes the actual number of section headers."""
  642. shnum = self.ehdr.e_shnum
  643. if shnum == 0:
  644. if self.ehdr.e_shoff == 0 or self.ehdr.e_shentsize == 0:
  645. # No section headers.
  646. return 0
  647. # Otherwise the extension mechanism is used (which may be
  648. # needed because e_shnum is just 16 bits).
  649. return self.read(self.Shdr, self.ehdr.e_shoff).sh_size
  650. return shnum
  651. def _find_shdr_strtab(self) -> StringTable:
  652. """Finds the section header string table (maybe via extensions)."""
  653. shstrndx = self.ehdr.e_shstrndx
  654. if shstrndx == Shn.SHN_XINDEX:
  655. shstrndx = self.read(self.Shdr, self.ehdr.e_shoff).sh_link
  656. return self._find_stringtab(shstrndx)
  657. def read(self, typ: type, offset:int ):
  658. """Reads an object at a specific offset.
  659. The type must have been enhanced using _define_variants.
  660. """
  661. return typ.unpack(self.image[offset: offset + typ.size])
  662. def phdrs(self) -> Phdr:
  663. """Generator iterating over the program headers."""
  664. if self.ehdr is None:
  665. return
  666. size = self.ehdr.e_phentsize
  667. if size != self.Phdr.size:
  668. raise ValueError('Unexpected Phdr size in ELF header: {} != {}'
  669. .format(size, self.Phdr.size))
  670. offset = self.ehdr.e_phoff
  671. for _ in range(self.ehdr.e_phnum):
  672. yield self.read(self.Phdr, offset)
  673. offset += size
  674. def shdrs(self, resolve: bool=True) -> Shdr:
  675. """Generator iterating over the section headers.
  676. If resolve, section names are automatically translated
  677. using the section header string table.
  678. """
  679. if self._shdr_num == 0:
  680. return
  681. size = self.ehdr.e_shentsize
  682. if size != self.Shdr.size:
  683. raise ValueError('Unexpected Shdr size in ELF header: {} != {}'
  684. .format(size, self.Shdr.size))
  685. offset = self.ehdr.e_shoff
  686. for _ in range(self._shdr_num):
  687. shdr = self.read(self.Shdr, offset)
  688. if resolve:
  689. shdr = shdr.resolve(self._shdr_strtab)
  690. yield shdr
  691. offset += size
  692. def dynamic(self) -> Dyn:
  693. """Generator iterating over the dynamic segment."""
  694. for phdr in self.phdrs():
  695. if phdr.p_type == Pt.PT_DYNAMIC:
  696. # Pick the first dynamic segment, like the loader.
  697. if phdr.p_filesz == 0:
  698. # Probably separated debuginfo.
  699. return
  700. offset = phdr.p_offset
  701. end = offset + phdr.p_memsz
  702. size = self.Dyn.size
  703. while True:
  704. next_offset = offset + size
  705. if next_offset > end:
  706. raise ValueError(
  707. 'Dynamic segment size {} is not a multiple of Dyn size {}'.format(
  708. phdr.p_memsz, size))
  709. yield self.read(self.Dyn, offset)
  710. if next_offset == end:
  711. return
  712. offset = next_offset
  713. def syms(self, shdr: Shdr, resolve: bool=True) -> Sym:
  714. """A generator iterating over a symbol table.
  715. If resolve, symbol names are automatically translated using
  716. the string table for the symbol table.
  717. """
  718. assert shdr.sh_type == Sht.SHT_SYMTAB
  719. size = shdr.sh_entsize
  720. if size != self.Sym.size:
  721. raise ValueError('Invalid symbol table entry size {}'.format(size))
  722. offset = shdr.sh_offset
  723. end = shdr.sh_offset + shdr.sh_size
  724. if resolve:
  725. strtab = self._find_stringtab(shdr.sh_link)
  726. while offset < end:
  727. sym = self.read(self.Sym, offset)
  728. if resolve:
  729. sym = sym.resolve(strtab)
  730. yield sym
  731. offset += size
  732. if offset != end:
  733. raise ValueError('Symbol table is not a multiple of entry size')
  734. def lookup_string(self, strtab_index: int, strtab_offset: int) -> bytes:
  735. """Looks up a string in a string table identified by its link index."""
  736. try:
  737. strtab = self._stringtab[strtab_index]
  738. except KeyError:
  739. strtab = self._find_stringtab(strtab_index)
  740. return strtab.get(strtab_offset)
  741. def find_section(self, shndx: Shn) -> Shdr:
  742. """Returns the section header for the indexed section.
  743. The section name is not resolved.
  744. """
  745. try:
  746. return self._section[shndx]
  747. except KeyError:
  748. pass
  749. if shndx in Shn:
  750. raise ValueError('Reserved section index {}'.format(shndx))
  751. idx = shndx.value
  752. if idx < 0 or idx > self._shdr_num:
  753. raise ValueError('Section index {} out of range [0, {})'.format(
  754. idx, self._shdr_num))
  755. shdr = self.read(
  756. self.Shdr, self.ehdr.e_shoff + idx * self.Shdr.size)
  757. self._section[shndx] = shdr
  758. return shdr
  759. def _find_stringtab(self, sh_link: int) -> StringTable:
  760. if sh_link in self._stringtab:
  761. return self._stringtab
  762. if sh_link < 0 or sh_link >= self._shdr_num:
  763. raise ValueError('Section index {} out of range [0, {})'.format(
  764. sh_link, self._shdr_num))
  765. shdr = self.read(
  766. self.Shdr, self.ehdr.e_shoff + sh_link * self.Shdr.size)
  767. if shdr.sh_type != Sht.SHT_STRTAB:
  768. raise ValueError(
  769. 'Section {} is not a string table: {}'.format(
  770. sh_link, shdr.sh_type))
  771. strtab = StringTable(
  772. self.image[shdr.sh_offset:shdr.sh_offset + shdr.sh_size])
  773. # This could retrain essentially arbitrary amounts of data,
  774. # but caching string tables seems important for performance.
  775. self._stringtab[sh_link] = strtab
  776. return strtab
  777. def elf_hash(s):
  778. """Computes the ELF hash of the string."""
  779. acc = 0
  780. for ch in s:
  781. if type(ch) is not int:
  782. ch = ord(ch)
  783. acc = ((acc << 4) + ch) & 0xffffffff
  784. top = acc & 0xf0000000
  785. acc = (acc ^ (top >> 24)) & ~top
  786. return acc
  787. def gnu_hash(s):
  788. """Computes the GNU hash of the string."""
  789. h = 5381
  790. for ch in s:
  791. if type(ch) is not int:
  792. ch = ord(ch)
  793. h = (h * 33 + ch) & 0xffffffff
  794. return h
  795. __all__ = [name for name in dir() if name[0].isupper()]