comparison paraspace/dex_deptracker.py @ 29:b0cc5da28141

pass parents to travel info
author Thinker K.F. Li <thinker@codemud.net>
date Thu, 09 Jun 2011 09:32:17 +0800
parents 5ce7ca1187b3
children 0c0a659187c2
comparison
equal deleted inserted replaced
28:5ce7ca1187b3 29:b0cc5da28141
182 182
183 183
184 def _travel_dex_relocatable(root_obj, parents=[]): 184 def _travel_dex_relocatable(root_obj, parents=[]):
185 stk = [(root_obj, parents, root_obj.__class__.__name__)] 185 stk = [(root_obj, parents, root_obj.__class__.__name__)]
186 186
187 def make_travel_info(obj, obj_name, child_name): 187 def make_travel_info(obj, obj_name, child_name, parents):
188 child_parents = parents + [obj] 188 child_parents = parents + [obj]
189 child_obj = _dex_tree_get_child(obj, child_name) 189 child_obj = _dex_tree_get_child(obj, child_name)
190 if isinstance(child_obj, dexfile.composite): 190 if isinstance(child_obj, dexfile.composite):
191 child_path = child_obj.__class__.__name__ 191 child_path = child_obj.__class__.__name__
192 else: 192 else:
197 while stk: 197 while stk:
198 obj, parents, obj_name = stk.pop(0) 198 obj, parents, obj_name = stk.pop(0)
199 yield (obj, parents, obj_name) 199 yield (obj, parents, obj_name)
200 200
201 if isinstance(obj, list): 201 if isinstance(obj, list):
202 children = [make_travel_info(obj, obj_name, repr(idx)) 202 children = [make_travel_info(obj, obj_name, repr(idx), parents)
203 for idx in range(len(obj))] 203 for idx in range(len(obj))]
204 stk.extend(children) 204 stk.extend(children)
205 continue 205 continue
206 206
207 if not isinstance(obj, dexfile.relocatable): 207 if not isinstance(obj, dexfile.relocatable):
208 continue 208 continue
209 209
210 children = [make_travel_info(obj, obj_name, child_name) 210 children = [make_travel_info(obj, obj_name, child_name, parents)
211 for child_name in obj.children()] 211 for child_name in obj.children()]
212 stk.extend(children) 212 stk.extend(children)
213 pass 213 pass
214 pass 214 pass
215 215
312 def link_prepare(self, obj, name_path, parents, markers_info): 312 def link_prepare(self, obj, name_path, parents, markers_info):
313 rev_parents = list(parents) 313 rev_parents = list(parents)
314 rev_parents.reverse() 314 rev_parents.reverse()
315 315
316 for parent in rev_parents: 316 for parent in rev_parents:
317 rel_marker_info = getattr(parent, 'rel_marker_info', {}) 317 try:
318 items = getattr(parent, name_path, []) 318 rel_marker_info = parent.rel_marker_info
319 items.append(obj) 319 except AttributeError:
320 try:
321 rel_marker_info = {}
322 parent.rel_marker_info = rel_marker_info
323 except AttributeError:
324 continue
325 pass
326 depons = rel_marker_info.setdefault(name_path, [])
327 depons.append(obj)
320 pass 328 pass
321 pass 329 pass
322 330
323 @staticmethod 331 @staticmethod
324 def find_depon(name_path, parents): 332 def find_depon(name_path, parents):
330 rel_marker_info = parent.rel_marker_info 338 rel_marker_info = parent.rel_marker_info
331 except: 339 except:
332 continue 340 continue
333 if name_path in rel_marker_info: 341 if name_path in rel_marker_info:
334 depons = rel_marker_info[name_path] 342 depons = rel_marker_info[name_path]
343 print parent, depons
335 assert len(depons) == 1 344 assert len(depons) == 1
336 depon = depons[0] 345 depon = depons[0]
337 return depon 346 return depon
338 pass 347 pass
339 348
495 # 504 #
496 # Link depend source to marked target 505 # Link depend source to marked target
497 # 506 #
498 for obj, parents, name_path in \ 507 for obj, parents, name_path in \
499 _travel_dex_relocatable(root_obj): 508 _travel_dex_relocatable(root_obj):
500 print name_path
501 if name_path not in all_dep_decls: 509 if name_path not in all_dep_decls:
502 continue 510 continue
503 511
504 rev_parents = list(parents)
505 rev_parents.reverse()
506
507 dep = all_dep_decls[name_path] 512 dep = all_dep_decls[name_path]
508 dep_type = dep[0] 513 dep_type = dep[0]
509 if dep_type == dexfile.depend_off_rel: 514 if dep_type == dexfile.depend_off_rel:
510 depon1 = _rel_offset_marker.find_depon(dep[1]) 515 depon1 = _rel_offset_marker.find_depon(dep[1], parents)
511 depon2 = _rel_offset_marker.find_depon(dep[2]) 516 depon2 = _rel_offset_marker.find_depon(dep[2], parents)
512 517
513 parent = parents[-1] 518 parent = parents[-1]
514 name = name_path.split('.')[-1] 519 name = name_path.split('.')[-1]
515 _dex_tree_set_child(parent, name, (depon1, depon2)) 520 _dex_tree_set_child(parent, name, (depon1, depon2))
516 elif dep_type == dexfile.depend_off: 521 elif dep_type == dexfile.depend_off: