elf_mem_map 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. #!/usr/bin/env python3
  2. import argparse
  3. import elftools.elf.elffile
  4. import elftools.dwarf.descriptions
  5. from collections import namedtuple
  6. from struct import unpack
  7. import re
  8. from lib.dump import decode_dump
  9. from lib.avr import *
  10. Entry = namedtuple('Entry', ['name', 'loc', 'size', 'declpos'])
  11. Member = namedtuple('Member', ['name', 'off', 'size'])
  12. def array_inc(loc, dim, idx=0):
  13. if idx == len(dim):
  14. return True
  15. loc[idx] += 1
  16. if loc[idx] == dim[idx]:
  17. loc[idx] = 0
  18. return array_inc(loc, dim, idx+1)
  19. return False
  20. def get_type_size(type_DIE):
  21. while True:
  22. if 'DW_AT_byte_size' in type_DIE.attributes:
  23. return type_DIE, type_DIE.attributes.get('DW_AT_byte_size').value
  24. if 'DW_AT_type' not in type_DIE.attributes:
  25. return None
  26. type_DIE = type_DIE.get_DIE_from_attribute('DW_AT_type')
  27. def get_type_arrsize(type_DIE):
  28. size = get_type_size(type_DIE)
  29. if size is None:
  30. return None
  31. byte_size = size[1]
  32. if size[0].tag != 'DW_TAG_pointer_type':
  33. array_DIE = get_type_def(type_DIE, 'DW_TAG_array_type')
  34. if array_DIE is not None:
  35. for range_DIE in array_DIE.iter_children():
  36. if range_DIE.tag == 'DW_TAG_subrange_type' and \
  37. 'DW_AT_upper_bound' in range_DIE.attributes:
  38. dim = range_DIE.attributes['DW_AT_upper_bound'].value + 1
  39. byte_size *= dim
  40. return byte_size
  41. def get_type_def(type_DIE, type_tag):
  42. while True:
  43. if type_DIE.tag == type_tag:
  44. return type_DIE
  45. if 'DW_AT_type' not in type_DIE.attributes:
  46. return None
  47. type_DIE = type_DIE.get_DIE_from_attribute('DW_AT_type')
  48. def get_FORM_block1(attr):
  49. if attr.form != 'DW_FORM_block1':
  50. return None
  51. if attr.value[0] == 3: # OP_addr
  52. return int.from_bytes(attr.value[1:], 'little')
  53. if attr.value[0] == 35: # OP_plus_uconst (ULEB128)
  54. v = 0
  55. s = 0
  56. for b in attr.value[1:]:
  57. v |= (b & 0x7f) << s
  58. if b & 0x80 == 0:
  59. break
  60. s += 7
  61. return v
  62. return None
  63. def get_array_dims(DIE):
  64. array_DIE = get_type_def(DIE, 'DW_TAG_array_type')
  65. if array_DIE is None:
  66. return []
  67. array_dim = []
  68. for range_DIE in array_DIE.iter_children():
  69. if range_DIE.tag == 'DW_TAG_subrange_type' and \
  70. 'DW_AT_upper_bound' in range_DIE.attributes:
  71. array_dim.append(range_DIE.attributes['DW_AT_upper_bound'].value + 1)
  72. return array_dim
  73. def get_struct_members(DIE, entry, expand_structs, struct_gaps):
  74. struct_DIE = get_type_def(DIE, 'DW_TAG_structure_type')
  75. if struct_DIE is None:
  76. return []
  77. members = []
  78. for member_DIE in struct_DIE.iter_children():
  79. if member_DIE.tag == 'DW_TAG_member' and 'DW_AT_name' in member_DIE.attributes:
  80. m_name = member_DIE.attributes['DW_AT_name'].value.decode('ascii')
  81. m_off = get_FORM_block1(member_DIE.attributes['DW_AT_data_member_location'])
  82. m_byte_size = get_type_size(member_DIE)[1]
  83. # still expand member arrays
  84. m_array_dim = get_array_dims(member_DIE)
  85. if m_byte_size == 1 and len(m_array_dim) > 1:
  86. # likely string, remove one dimension
  87. m_byte_size *= m_array_dim.pop()
  88. if len(m_array_dim) == 0 or (len(m_array_dim) == 1 and m_array_dim[0] == 1):
  89. # plain entry
  90. members.append(Member(m_name, m_off, m_byte_size))
  91. elif len(m_array_dim) == 1 and m_byte_size == 1:
  92. # likely string, avoid expansion
  93. members.append(Member(m_name + '[]', m_off, m_array_dim[0]))
  94. else:
  95. # expand array entries
  96. m_array_pos = m_off
  97. m_array_loc = [0] * len(m_array_dim)
  98. while True:
  99. # location index
  100. sfx = ''
  101. for d in range(len(m_array_dim)):
  102. sfx += '[{}]'.format(str(m_array_loc[d]).rjust(len(str(m_array_dim[d]-1)), '0'))
  103. members.append(Member(m_name + sfx, m_array_pos, m_byte_size))
  104. # advance
  105. if array_inc(m_array_loc, m_array_dim):
  106. break
  107. m_array_pos += m_byte_size
  108. if struct_gaps and len(members):
  109. # fill gaps in the middle
  110. members = list(sorted(members, key=lambda x: x.off))
  111. last_end = 0
  112. for n in range(len(members)):
  113. member = members[n]
  114. if member.off > last_end:
  115. members.append(Member('*UNKNOWN*', last_end, member.off - last_end))
  116. last_end = member.off + member.size
  117. if struct_gaps and len(members):
  118. # fill gap at the end
  119. members = list(sorted(members, key=lambda x: x.off))
  120. last = members[-1]
  121. last_end = last.off + last.size
  122. if entry.size > last_end:
  123. members.append(Member('*UNKNOWN*', last_end, entry.size - last_end))
  124. return members
  125. def get_elf_globals(path, expand_structs, struct_gaps=True):
  126. fd = open(path, "rb")
  127. if fd is None:
  128. return
  129. elffile = elftools.elf.elffile.ELFFile(fd)
  130. if elffile is None or not elffile.has_dwarf_info():
  131. return
  132. # probably not needed, since we're decoding expressions manually
  133. elftools.dwarf.descriptions.set_global_machine_arch(elffile.get_machine_arch())
  134. dwarfinfo = elffile.get_dwarf_info()
  135. grefs = []
  136. for CU in dwarfinfo.iter_CUs():
  137. file_entries = dwarfinfo.line_program_for_CU(CU).header["file_entry"]
  138. for DIE in CU.iter_DIEs():
  139. # handle only variable types
  140. if DIE.tag != 'DW_TAG_variable':
  141. continue
  142. if 'DW_AT_location' not in DIE.attributes:
  143. continue
  144. if 'DW_AT_name' not in DIE.attributes and \
  145. 'DW_AT_abstract_origin' not in DIE.attributes:
  146. continue
  147. # handle locations encoded directly as DW_OP_addr (leaf globals)
  148. loc = get_FORM_block1(DIE.attributes['DW_AT_location'])
  149. if loc is None or loc < SRAM_OFFSET or loc >= EEPROM_OFFSET:
  150. continue
  151. loc -= SRAM_OFFSET
  152. # variable name/type
  153. if 'DW_AT_name' not in DIE.attributes and \
  154. 'DW_AT_abstract_origin' in DIE.attributes:
  155. DIE = DIE.get_DIE_from_attribute('DW_AT_abstract_origin')
  156. if 'DW_AT_location' in DIE.attributes:
  157. # duplicate reference (handled directly), skip
  158. continue
  159. if 'DW_AT_name' not in DIE.attributes:
  160. continue
  161. if 'DW_AT_type' not in DIE.attributes:
  162. continue
  163. name = DIE.attributes['DW_AT_name'].value.decode('ascii')
  164. # get final storage size
  165. size = get_type_size(DIE)
  166. if size is None:
  167. continue
  168. byte_size = size[1]
  169. # location of main definition
  170. declpos = ''
  171. if 'DW_AT_decl_file' in DIE.attributes and \
  172. 'DW_AT_decl_line' in DIE.attributes:
  173. line = DIE.attributes['DW_AT_decl_line'].value
  174. fname = DIE.attributes['DW_AT_decl_file'].value
  175. if fname and fname - 1 < len(file_entries):
  176. fname = file_entries[fname-1].name.decode('ascii')
  177. declpos = '{}:{}'.format(fname, line)
  178. # fetch array dimensions (if known)
  179. array_dim = get_array_dims(DIE)
  180. # fetch structure members (one level only)
  181. entry = Entry(name, loc, byte_size, declpos)
  182. if not expand_structs or size[0].tag == 'DW_TAG_pointer_type':
  183. members = []
  184. else:
  185. members = get_struct_members(DIE, entry, expand_structs, struct_gaps)
  186. def expand_members(entry, members):
  187. if len(members) == 0:
  188. grefs.append(entry)
  189. else:
  190. for member in members:
  191. grefs.append(Entry(entry.name + '.' + member.name,
  192. entry.loc + member.off, member.size,
  193. entry.declpos))
  194. if byte_size == 1 and len(array_dim) > 1:
  195. # likely string, remove one dimension
  196. byte_size *= array_dim.pop()
  197. if len(array_dim) == 0 or (len(array_dim) == 1 and array_dim[0] == 1):
  198. # plain entry
  199. expand_members(entry, members)
  200. elif len(array_dim) == 1 and byte_size == 1:
  201. # likely string, avoid expansion
  202. grefs.append(Entry(entry.name + '[]', entry.loc,
  203. array_dim[0], entry.declpos))
  204. else:
  205. # expand array entries
  206. array_pos = loc
  207. array_loc = [0] * len(array_dim)
  208. while True:
  209. # location index
  210. sfx = ''
  211. for d in range(len(array_dim)):
  212. sfx += '[{}]'.format(str(array_loc[d]).rjust(len(str(array_dim[d]-1)), '0'))
  213. expand_members(Entry(entry.name + sfx, array_pos,
  214. byte_size, entry.declpos), members)
  215. # advance
  216. if array_inc(array_loc, array_dim):
  217. break
  218. array_pos += byte_size
  219. return grefs
  220. def annotate_refs(grefs, addr, data, width, gaps=True, overlaps=True):
  221. last_end = None
  222. for entry in grefs:
  223. if entry.loc < addr:
  224. continue
  225. if entry.loc + entry.size > addr + len(data):
  226. continue
  227. pos = entry.loc-addr
  228. end_pos = pos + entry.size
  229. buf = data[pos:end_pos]
  230. buf_repr = ''
  231. if len(buf) in [1, 2, 4]:
  232. # attempt to decode as integers
  233. buf_repr += ' I:' + str(int.from_bytes(buf, 'little')).rjust(10)
  234. if len(buf) in [4, 8]:
  235. # attempt to decode as floats
  236. typ = 'f' if len(buf) == 4 else 'd'
  237. buf_repr += ' F:' + '{:10.3f}'.format(unpack(typ, buf)[0])
  238. if last_end is not None:
  239. if gaps and last_end < pos:
  240. # decode gaps
  241. gap_size = pos - last_end
  242. gap_buf = data[last_end:pos]
  243. print('{:04x} {} {:4} R:{}'.format(addr+last_end, "*UNKNOWN*".ljust(width),
  244. gap_size, gap_buf.hex()))
  245. if overlaps and last_end > pos + 1:
  246. gap_size = pos - last_end
  247. print('{:04x} {} {:4}'.format(addr+last_end, "*OVERLAP*".ljust(width), gap_size))
  248. print('{:04x} {} {:4}{} R:{}'.format(entry.loc, entry.name.ljust(width),
  249. entry.size, buf_repr, buf.hex()))
  250. last_end = end_pos
  251. def print_map(grefs):
  252. print('OFFSET\tSIZE\tNAME\tDECLPOS')
  253. for entry in grefs:
  254. print('{:x}\t{}\t{}\t{}'.format(entry.loc, entry.size, entry.name, entry.declpos))
  255. def print_qdirstat(grefs):
  256. print('[qdirstat 1.0 cache file]')
  257. entries = {}
  258. for entry in grefs:
  259. # do not output registers when looking at space usage
  260. if entry.loc < SRAM_START:
  261. continue
  262. paths = list(filter(None, re.split(r'[\[\].]', entry.name)))
  263. base = entries
  264. for i in range(len(paths) - 1):
  265. name = paths[i]
  266. if name not in base:
  267. base[name] = {}
  268. base = base[name]
  269. name = paths[-1]
  270. if name in base:
  271. name = '{}_{:x}'.format(entry.name, entry.loc)
  272. base[name] = entry.size
  273. def walker(root, prefix):
  274. files = []
  275. dirs = []
  276. for name, entries in root.items():
  277. if type(entries) == int:
  278. files.append([name, entries])
  279. else:
  280. dirs.append([name, entries])
  281. # print files
  282. print('D\t{}\t{}\t0x0'.format(prefix, 0))
  283. for name, size in files:
  284. print('F\t{}\t{}\t0x0'.format(name, size))
  285. # recurse directories
  286. for name, entries in dirs:
  287. walker(entries, prefix + '/' + name)
  288. walker(entries, '/')
  289. def main():
  290. ap = argparse.ArgumentParser(description="""
  291. Generate a symbol table map starting directly from an ELF
  292. firmware with DWARF3 debugging information.
  293. When used along with a memory dump obtained from the D2/D21/D23 g-code,
  294. show the value of each symbol which is within the address range.
  295. """)
  296. ap.add_argument('elf', help='ELF file containing DWARF debugging information')
  297. ap.add_argument('--no-gaps', action='store_true',
  298. help='do not dump memory inbetween known symbols')
  299. ap.add_argument('--no-expand-structs', action='store_true',
  300. help='do not decode structure data')
  301. ap.add_argument('--overlaps', action='store_true',
  302. help='annotate overlaps greater than 1 byte')
  303. ap.add_argument('--name-width', type=int, default=50,
  304. help='set name column width')
  305. g = ap.add_mutually_exclusive_group(required=True)
  306. g.add_argument('dump', nargs='?', help='RAM dump obtained from D2 g-code')
  307. g.add_argument('--map', action='store_true', help='dump global memory map')
  308. g.add_argument('--qdirstat', action='store_true',
  309. help='dump qdirstat-compatible size usage map')
  310. args = ap.parse_args()
  311. grefs = get_elf_globals(args.elf, expand_structs=not args.no_expand_structs)
  312. grefs = list(sorted(grefs, key=lambda x: x.loc))
  313. if args.map:
  314. print_map(grefs)
  315. elif args.qdirstat:
  316. print_qdirstat(grefs)
  317. else:
  318. # fetch the memory data
  319. dump = decode_dump(args.dump)
  320. if dump is None:
  321. return 1
  322. # strip padding, if present
  323. addr_start = dump.ranges[0][0]
  324. addr_end = dump.ranges[-1][0]+dump.ranges[-1][1]
  325. data = dump.data[addr_start:addr_end]
  326. annotate_refs(grefs, addr_start, data,
  327. width=args.name_width,
  328. gaps=not args.no_gaps,
  329. overlaps=args.overlaps)
  330. if __name__ == '__main__':
  331. exit(main())