Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
vshymanskyy committed Sep 12, 2024
1 parent f1760a6 commit 00b1b8c
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions findobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from elftools.elf import elffile
from collections import defaultdict


class PickleCache:
def __init__(self, path=".cache", prefix=""):
self.path = path
Expand All @@ -21,6 +22,7 @@ def load(self, key):
with open(self._get_fn(key), "rb") as f:
return pickle.load(f)


def cached(key, cache):
def decorator(func):
@functools.wraps(func)
Expand All @@ -31,14 +33,14 @@ def wrapper(*args, **kwargs):
if d["key"] != cache_key:
raise Exception("Cache key mismatch")
return d["data"]
except Exception as e:
except Exception:
res = func(*args, **kwargs)
try:
cache.store(cache_key, {
"key": cache_key,
"data": res,
})
except Exception as e:
except Exception:
pass
return res
return wrapper
Expand Down Expand Up @@ -80,7 +82,6 @@ def load_symbols(self):
for symbol in symtab.iter_symbols():
sym_name = symbol.name
sym_bind = symbol["st_info"]["bind"]
#info = symbol["st_info"]["type"]

if sym_bind in ("STB_GLOBAL", "STB_WEAK"):
if symbol.entry["st_shndx"] != "SHN_UNDEF":
Expand All @@ -90,7 +91,6 @@ def load_symbols(self):
obj["undef"].add(sym_name)

if sym_bind == "STB_WEAK":
#print("Weak", sym_name, symbol.entry["st_shndx"])
obj["weak"].add(sym_name)

return {"objs": dict(objs), "symbols": symbols}
Expand All @@ -114,14 +114,14 @@ def add_obj(archive, symbol):
resolved_objs.append(obj_tuple)

# Add the symbols this object defines
for defined_symbol in obj_info['def']:
for defined_symbol in obj_info["def"]:
if defined_symbol in provided_symbols:
raise RuntimeError(f"Multiple non-weak definitions for symbol: {defined_symbol}")
provided_symbols[defined_symbol] = obj_name # TODO: save if week

# Recursively add undefined symbols from this object
for undef_symbol in obj_info['undef']:
if undef_symbol in obj_info['weak']:
for undef_symbol in obj_info["undef"]:
if undef_symbol in obj_info["weak"]:
print(f"Skippping weak dependency: {undef_symbol}")
continue
if undef_symbol not in provided_symbols:
Expand All @@ -145,31 +145,35 @@ def add_obj(archive, symbol):

# At this point, all resolvable symbols are resolved
if unresolved_symbols:
raise RuntimeError(f"Unresolved symbols: {', '.join(unresolved_symbols)}")
raise RuntimeError("Unresolved symbols: " + ", ".join(unresolved_symbols))

return resolved_objs


if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description='Resolve dependencies from archive files.')
parser.add_argument('--arch', help='Target architecture to extract objects to')
parser.add_argument('-v', '--verbose', help='increase output verbosity', action='store_true')
parser.add_argument('inputs', nargs='+', help='AR archive files and symbols to resolve')
parser = argparse.ArgumentParser(description="Resolve dependencies from archive files.")
parser.add_argument("--arch", help="Target architecture to extract objects to")
parser.add_argument("-v", "--verbose", help="increase output verbosity", action="store_true")
parser.add_argument("inputs", nargs="+", help="AR archive files and symbols to resolve")
args = parser.parse_args()

# Separate files and symbols
archives = [CachedArFile(item) for item in args.inputs if item.endswith('.a')]
symbols = [item for item in args.inputs if not item.endswith('.a')]
archives = [CachedArFile(item) for item in args.inputs if item.endswith(".a")]
symbols = [item for item in args.inputs if not item.endswith(".a")]

result = resolve(archives, symbols)

# Extract files
for ar, obj in result:
print(os.path.basename(ar.fn) + ':' + obj)
print(Path(ar.fn).stem + "/" + obj)
if args.verbose:
print(' def:', ','.join(ar.objs[obj]['def']))
print(' req:', ','.join(ar.objs[obj]['undef']))
print(" def:", ", ".join(ar.objs[obj]["def"]))
print(" req:", ", ".join(ar.objs[obj]["undef"]))
weak = ar.objs[obj]["weak"]
if weak:
print(" weak:", ", ".join(weak))
if args.arch:
content = ar.open(obj).read()
with open(f'runtime/libgcc-{args.arch}/{obj}', 'wb') as output:
with open(f"runtime/libgcc-{args.arch}/{obj}", "wb") as output:
output.write(content)

0 comments on commit 00b1b8c

Please sign in to comment.