comparison python/ppci/objectfile.py @ 385:d056b552d3f4

Made better use of layout
author Windel Bouwman
date Thu, 01 May 2014 14:03:12 +0200
parents 94f5b719ad0b
children
comparison
equal deleted inserted replaced
384:94f5b719ad0b 385:d056b552d3f4
65 Also contains symbols and relocation entries """ 65 Also contains symbols and relocation entries """
66 def __init__(self): 66 def __init__(self):
67 self.symbols = {} 67 self.symbols = {}
68 self.sections = {} 68 self.sections = {}
69 self.relocations = [] 69 self.relocations = []
70 self.images = {}
70 71
71 def find_symbol(self, name): 72 def find_symbol(self, name):
72 return self.symbols[name] 73 return self.symbols[name]
73 74
74 def add_symbol(self, name, value, section): 75 def add_symbol(self, name, value, section):
90 def get_section(self, name): 91 def get_section(self, name):
91 if not name in self.sections: 92 if not name in self.sections:
92 self.sections[name] = Section(name) 93 self.sections[name] = Section(name)
93 return self.sections[name] 94 return self.sections[name]
94 95
96 def get_image(self, name):
97 return self.images[name]
98
99 def get_symbol_value(self, name):
100 symbol = self.find_symbol(name)
101 section = self.get_section(symbol.section)
102 return symbol.value + section.address
103
95 def __eq__(self, other): 104 def __eq__(self, other):
96 return (self.symbols == other.symbols) and \ 105 return (self.symbols == other.symbols) and \
97 (self.sections == other.sections) and \ 106 (self.sections == other.sections) and \
98 (self.relocations == other.relocations) 107 (self.relocations == other.relocations)
99 108
107 116
108 def load_object(f): 117 def load_object(f):
109 return deserialize(json.load(f)) 118 return deserialize(json.load(f))
110 119
111 120
121 def bin2asc(data):
122 return binascii.hexlify(data).decode('ascii')
123
124 def asc2bin(data):
125 return bytearray(binascii.unhexlify(data.encode('ascii')))
126
127
112 def serialize(x): 128 def serialize(x):
113 res = {} 129 res = {}
114 if isinstance(x, ObjectFile): 130 if isinstance(x, ObjectFile):
115 res['sections'] = [] 131 res['sections'] = []
116 for sname in sorted(x.sections.keys()): 132 for sname in sorted(x.sections.keys()):
121 s = x.symbols[sname] 137 s = x.symbols[sname]
122 res['symbols'].append(serialize(s)) 138 res['symbols'].append(serialize(s))
123 res['relocations'] = [] 139 res['relocations'] = []
124 for reloc in x.relocations: 140 for reloc in x.relocations:
125 res['relocations'].append(serialize(reloc)) 141 res['relocations'].append(serialize(reloc))
142 res['images'] = {}
143 for image_name in x.images:
144 res['images'][image_name] = bin2asc(x.images[image_name])
126 elif isinstance(x, Section): 145 elif isinstance(x, Section):
127 res['name'] = x.name 146 res['name'] = x.name
128 res['address'] = hex(x.address) 147 res['address'] = hex(x.address)
129 res['data'] = binascii.hexlify(x.data).decode('ascii') 148 res['data'] = bin2asc(x.data)
130 elif isinstance(x, Symbol): 149 elif isinstance(x, Symbol):
131 res['name'] = x.name 150 res['name'] = x.name
132 res['value'] = hex(x.value) 151 res['value'] = hex(x.value)
133 res['section'] = x.section 152 res['section'] = x.section
134 elif isinstance(x, Relocation): 153 elif isinstance(x, Relocation):
142 def deserialize(d): 161 def deserialize(d):
143 obj = ObjectFile() 162 obj = ObjectFile()
144 for section in d['sections']: 163 for section in d['sections']:
145 so = obj.get_section(section['name']) 164 so = obj.get_section(section['name'])
146 so.address = make_num(section['address']) 165 so.address = make_num(section['address'])
147 so.data = bytearray(binascii.unhexlify(section['data'].encode('ascii'))) 166 so.data = asc2bin(section['data'])
148 for reloc in d['relocations']: 167 for reloc in d['relocations']:
149 obj.add_relocation(reloc['symbol'], make_num(reloc['offset']), 168 obj.add_relocation(reloc['symbol'], make_num(reloc['offset']),
150 reloc['type'], reloc['section']) 169 reloc['type'], reloc['section'])
151 for sym in d['symbols']: 170 for sym in d['symbols']:
152 obj.add_symbol(sym['name'], make_num(sym['value']), sym['section']) 171 obj.add_symbol(sym['name'], make_num(sym['value']), sym['section'])
172 for image_name in d['images']:
173 obj.images[image_name] = asc2bin(d['images'][image_name])
153 return obj 174 return obj
154 175