* Lookup a symbol from the cache. If no match is found, attempt to find the * optimal coercion to an existing symbol and cache the result. */
| 319 | * optimal coercion to an existing symbol and cache the result. |
| 320 | */ |
| 321 | intptr_t lookupSymbol(const ELF *elf, const char *name, TypeSig sig) |
| 322 | { |
| 323 | Symbols &symbols = elf->symbols; |
| 324 | if (symbols.size() == 0) |
| 325 | { |
| 326 | // Build symbol cache: |
| 327 | for (const auto &entry: elf->dynsyms) |
| 328 | { |
| 329 | const Elf64_Sym *sym = entry.second; |
| 330 | if (ELF64_ST_TYPE(sym->st_info) != STT_FUNC) |
| 331 | continue; |
| 332 | intptr_t addr = elf->base + (intptr_t)sym->st_value; |
| 333 | parseSymbol(symbols, entry.first, addr); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | Symbol key(name, sig); |
| 338 | auto i = symbols.find(key); |
| 339 | if (i != symbols.end()) |
| 340 | { |
| 341 | intptr_t addr = i->second; |
| 342 | if (addr == INTPTR_MIN) |
| 343 | return addr; // Missing |
| 344 | if (addr < 0) |
| 345 | return -addr; // Derived |
| 346 | else |
| 347 | return addr; // Original |
| 348 | } |
| 349 | |
| 350 | // Attempt to find the optimal coercion: |
| 351 | Symbol min(name, TYPESIG_MIN), max(name, TYPESIG_MAX); |
| 352 | auto j = symbols.lower_bound(min); |
| 353 | auto jend = symbols.upper_bound(max); |
| 354 | long score = LONG_MAX; |
| 355 | intptr_t addr = INTPTR_MIN; |
| 356 | for (; j != jend; j++) |
| 357 | { |
| 358 | long nscore = coercible(sig, j->first.sig); |
| 359 | if (nscore < 0) |
| 360 | continue; // Not coercible. |
| 361 | if (j->second != INTPTR_MIN && nscore < score) |
| 362 | { |
| 363 | score = nscore; |
| 364 | addr = std::abs(j->second); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | // Add result to cache (note: INTPTR_MIN is a valid value): |
| 369 | insertSymbol(symbols, name, sig, (addr > 0? -addr: addr)); |
| 370 | return addr; |
| 371 | } |
| 372 | |
| 373 | /* |
| 374 | * Print warning for symbol mismatch. |
no test coverage detected