Get global symbols from a library via nm -g Parameters ---------- path : str The library path nm: str The path to nm command Returns ------- symbol_section_map: Dict[str, str] A map from defined global symbol to their sections
(path, *, nm=None)
| 179 | |
| 180 | |
| 181 | def get_global_symbol_section_map(path, *, nm=None) -> dict[str, str]: |
| 182 | """Get global symbols from a library via nm -g |
| 183 | |
| 184 | Parameters |
| 185 | ---------- |
| 186 | path : str |
| 187 | The library path |
| 188 | |
| 189 | nm: str |
| 190 | The path to nm command |
| 191 | |
| 192 | Returns |
| 193 | ------- |
| 194 | symbol_section_map: Dict[str, str] |
| 195 | A map from defined global symbol to their sections |
| 196 | """ |
| 197 | if nm is None: |
| 198 | if not _is_linux_like(): |
| 199 | raise ValueError("Unsupported platform") |
| 200 | nm = "nm" |
| 201 | |
| 202 | symbol_section_map = {} |
| 203 | |
| 204 | if not os.path.isfile(path): |
| 205 | raise FileNotFoundError(f"{path} does not exist") |
| 206 | |
| 207 | cmd = [nm, "-gU", path] |
| 208 | proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 209 | (out, _) = proc.communicate() |
| 210 | |
| 211 | if proc.returncode != 0: |
| 212 | msg = "Runtime error:\n" |
| 213 | msg += out.decode("utf-8", errors="replace") |
| 214 | raise RuntimeError(msg) |
| 215 | |
| 216 | for line in out.decode("utf-8", errors="replace").split("\n"): |
| 217 | data = line.strip().split() |
| 218 | if len(data) != 3: |
| 219 | continue |
| 220 | symbol = data[-1] |
| 221 | section = data[-2] |
| 222 | symbol_section_map[symbol] = section |
| 223 | return symbol_section_map |
| 224 | |
| 225 | |
| 226 | def get_target_by_dump_machine(compiler): |
nothing calls this directly
no test coverage detected
searching dependent graphs…