|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# This file is part of the MicroPython project, http://micropython.org/ |
| 4 | +# |
| 5 | +# The MIT License (MIT) |
| 6 | +# |
| 7 | +# Copyright (c) 2024 Volodymyr Shymanskyy |
| 8 | +# |
| 9 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | +# of this software and associated documentation files (the "Software"), to deal |
| 11 | +# in the Software without restriction, including without limitation the rights |
| 12 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +# copies of the Software, and to permit persons to whom the Software is |
| 14 | +# furnished to do so, subject to the following conditions: |
| 15 | +# |
| 16 | +# The above copyright notice and this permission notice shall be included in |
| 17 | +# all copies or substantial portions of the Software. |
| 18 | +# |
| 19 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | +# THE SOFTWARE. |
| 26 | + |
| 27 | +import os |
| 28 | +import hashlib |
| 29 | +import functools |
| 30 | +import pickle |
| 31 | + |
| 32 | +from elftools.elf import elffile |
| 33 | +from collections import defaultdict |
| 34 | + |
| 35 | +try: |
| 36 | + from ar import Archive |
| 37 | +except: |
| 38 | + Archive = None |
| 39 | + |
| 40 | + |
| 41 | +class PickleCache: |
| 42 | + def __init__(self, path=".cache", prefix=""): |
| 43 | + self.path = path |
| 44 | + self._get_fn = lambda key: os.path.join(path, prefix + key[:24]) |
| 45 | + |
| 46 | + def store(self, key, data): |
| 47 | + os.makedirs(self.path, exist_ok=True) |
| 48 | + with open(self._get_fn(key), "wb") as f: |
| 49 | + pickle.dump(data, f) |
| 50 | + |
| 51 | + def load(self, key): |
| 52 | + with open(self._get_fn(key), "rb") as f: |
| 53 | + return pickle.load(f) |
| 54 | + |
| 55 | + |
| 56 | +def cached(key, cache): |
| 57 | + def decorator(func): |
| 58 | + @functools.wraps(func) |
| 59 | + def wrapper(*args, **kwargs): |
| 60 | + cache_key = key(*args, **kwargs) |
| 61 | + try: |
| 62 | + d = cache.load(cache_key) |
| 63 | + if d["key"] != cache_key: |
| 64 | + raise Exception("Cache key mismatch") |
| 65 | + return d["data"] |
| 66 | + except Exception: |
| 67 | + res = func(*args, **kwargs) |
| 68 | + try: |
| 69 | + cache.store( |
| 70 | + cache_key, |
| 71 | + { |
| 72 | + "key": cache_key, |
| 73 | + "data": res, |
| 74 | + }, |
| 75 | + ) |
| 76 | + except Exception: |
| 77 | + pass |
| 78 | + return res |
| 79 | + |
| 80 | + return wrapper |
| 81 | + |
| 82 | + return decorator |
| 83 | + |
| 84 | + |
| 85 | +class CachedArFile: |
| 86 | + def __init__(self, fn): |
| 87 | + if not Archive: |
| 88 | + raise RuntimeError("Please run 'pip install ar' to link .a files") |
| 89 | + self.fn = fn |
| 90 | + self._archive = Archive(open(fn, "rb")) |
| 91 | + info = self.load_symbols() |
| 92 | + self.objs = info["objs"] |
| 93 | + self.symbols = info["symbols"] |
| 94 | + |
| 95 | + def open(self, obj): |
| 96 | + return self._archive.open(obj, "rb") |
| 97 | + |
| 98 | + def _cache_key(self): |
| 99 | + with open(self.fn, "rb") as f: |
| 100 | + digest = hashlib.file_digest(f, "sha3_256") |
| 101 | + # Change this salt if the cache data format changes |
| 102 | + digest.update(bytes.fromhex("45155db4bc868fa78cb99c3448b2bf2b")) |
| 103 | + return digest.hexdigest() |
| 104 | + |
| 105 | + @cached(key=_cache_key, cache=PickleCache(prefix="ar_")) |
| 106 | + def load_symbols(self): |
| 107 | + print("Loading", self.fn) |
| 108 | + objs = defaultdict(lambda: {"def": set(), "undef": set(), "weak": set()}) |
| 109 | + symbols = {} |
| 110 | + for entry in self._archive: |
| 111 | + obj_name = entry.name |
| 112 | + elf = elffile.ELFFile(self.open(obj_name)) |
| 113 | + symtab = elf.get_section_by_name(".symtab") |
| 114 | + if not symtab: |
| 115 | + continue |
| 116 | + |
| 117 | + obj = objs[obj_name] |
| 118 | + |
| 119 | + for symbol in symtab.iter_symbols(): |
| 120 | + sym_name = symbol.name |
| 121 | + sym_bind = symbol["st_info"]["bind"] |
| 122 | + |
| 123 | + if sym_bind in ("STB_GLOBAL", "STB_WEAK"): |
| 124 | + if symbol.entry["st_shndx"] != "SHN_UNDEF": |
| 125 | + obj["def"].add(sym_name) |
| 126 | + symbols[sym_name] = obj_name |
| 127 | + else: |
| 128 | + obj["undef"].add(sym_name) |
| 129 | + |
| 130 | + if sym_bind == "STB_WEAK": |
| 131 | + obj["weak"].add(sym_name) |
| 132 | + |
| 133 | + return {"objs": dict(objs), "symbols": symbols} |
| 134 | + |
| 135 | + |
| 136 | +def resolve(archives, symbols): |
| 137 | + resolved_objs = [] # Object files needed to resolve symbols |
| 138 | + unresolved_symbols = set() |
| 139 | + provided_symbols = {} # Which symbol is provided by which object |
| 140 | + symbol_stack = list(symbols) |
| 141 | + |
| 142 | + # A helper function to handle symbol resolution from a particular object |
| 143 | + def add_obj(archive, symbol): |
| 144 | + obj_name = archive.symbols[symbol] |
| 145 | + obj_info = archive.objs[obj_name] |
| 146 | + |
| 147 | + obj_tuple = (archive, obj_name) |
| 148 | + if obj_tuple in resolved_objs: |
| 149 | + return # Already processed this object |
| 150 | + |
| 151 | + resolved_objs.append(obj_tuple) |
| 152 | + |
| 153 | + # Add the symbols this object defines |
| 154 | + for defined_symbol in obj_info["def"]: |
| 155 | + if defined_symbol in provided_symbols: |
| 156 | + raise RuntimeError(f"Multiple definitions for {defined_symbol}") |
| 157 | + provided_symbols[defined_symbol] = obj_name # TODO: save if week |
| 158 | + |
| 159 | + # Recursively add undefined symbols from this object |
| 160 | + for undef_symbol in obj_info["undef"]: |
| 161 | + if undef_symbol in obj_info["weak"]: |
| 162 | + print(f"Skippping weak dependency: {undef_symbol}") |
| 163 | + continue |
| 164 | + if undef_symbol not in provided_symbols: |
| 165 | + symbol_stack.append(undef_symbol) # Add undefined symbol to resolve |
| 166 | + |
| 167 | + while symbol_stack: |
| 168 | + symbol = symbol_stack.pop(0) |
| 169 | + |
| 170 | + if symbol in provided_symbols: |
| 171 | + continue # Symbol is already resolved |
| 172 | + |
| 173 | + found = False |
| 174 | + for archive in archives: |
| 175 | + if symbol in archive.symbols: |
| 176 | + add_obj(archive, symbol) |
| 177 | + found = True |
| 178 | + break |
| 179 | + |
| 180 | + if not found: |
| 181 | + unresolved_symbols.add(symbol) |
| 182 | + |
| 183 | + return resolved_objs, list(unresolved_symbols) |
| 184 | + |
| 185 | + |
| 186 | +if __name__ == "__main__": |
| 187 | + import argparse |
| 188 | + from pathlib import Path |
| 189 | + |
| 190 | + parser = argparse.ArgumentParser(description="Resolve symbols from AR files.") |
| 191 | + parser.add_argument("--arch", help="Target architecture to extract objects to") |
| 192 | + parser.add_argument("-v", "--verbose", help="Verbose logging", action="store_true") |
| 193 | + parser.add_argument("inputs", nargs="+", help="AR files and symbols to resolve") |
| 194 | + args = parser.parse_args() |
| 195 | + |
| 196 | + # Separate files and symbols |
| 197 | + archives = [CachedArFile(item) for item in args.inputs if item.endswith(".a")] |
| 198 | + symbols = [item for item in args.inputs if not item.endswith(".a")] |
| 199 | + |
| 200 | + result, unresolved = resolve(archives, symbols) |
| 201 | + |
| 202 | + if unresolved: |
| 203 | + raise RuntimeError("Unresolved symbols: " + ", ".join(unresolved)) |
| 204 | + |
| 205 | + # Extract files |
| 206 | + for ar, obj in result: |
| 207 | + print(Path(ar.fn).stem + "/" + obj) |
| 208 | + if args.verbose: |
| 209 | + print(" def:", ", ".join(ar.objs[obj]["def"])) |
| 210 | + print(" req:", ", ".join(ar.objs[obj]["undef"])) |
| 211 | + weak = ar.objs[obj]["weak"] |
| 212 | + if weak: |
| 213 | + print(" weak:", ", ".join(weak)) |
| 214 | + if args.arch: |
| 215 | + content = ar.open(obj).read() |
| 216 | + with open(f"runtime/libgcc-{args.arch}/{obj}", "wb") as output: |
| 217 | + output.write(content) |
0 commit comments