| 1220 | (shdr[shdr[i].sh_link].sh_offset + shdr[shdr[i].sh_link].sh_size) > mmap_info.size) |
| 1221 | continue; |
| 1222 | |
| 1223 | const KT_ElfW(Sym) *symtab = reinterpret_cast<KT_ElfW(Sym) *>(reinterpret_cast<char *>(mmap_info.data) + |
| 1224 | shdr[i].sh_offset); |
| 1225 | const size_t symCount = shdr[i].sh_size / shdr[i].sh_entsize; |
| 1226 | const KT_ElfW(Shdr) *strtabShdr = &shdr[shdr[i].sh_link]; |
| 1227 | const char *strtab = reinterpret_cast<char *>(reinterpret_cast<char *>(mmap_info.data) + |
| 1228 | strtabShdr->sh_offset); |
| 1229 | |
| 1230 | for (size_t j = 0; j < symCount; ++j) |
| 1231 | { |
| 1232 | const KT_ElfW(Sym) *curr_sym = &symtab[j]; |
| 1233 | if (!curr_sym || curr_sym->st_name >= strtabShdr->sh_size) |
| 1234 | continue; |
| 1235 | |
| 1236 | if (intptr_t(curr_sym->st_value) <= 0 || intptr_t(curr_sym->st_size) <= 0) |
| 1237 | continue; |
| 1238 | |
| 1239 | if (KT_ELF_ST_TYPE(curr_sym->st_info) != STT_OBJECT && KT_ELF_ST_TYPE(curr_sym->st_info) != STT_FUNC) |
| 1240 | continue; |
| 1241 | |
| 1242 | std::string sym_str = std::string(reinterpret_cast<const char *>(strtab + curr_sym->st_name)); |
| 1243 | if (!sym_str.empty() && sym_str.data()) |
| 1244 | _dsymbolsMap[sym_str] = get_sym_address(curr_sym); |
| 1245 | } |
| 1246 | } |
| 1247 | cleanup(); |
| 1248 | } |
| 1249 | return _dsymbolsMap; |
| 1250 | } |
| 1251 | |
| 1252 | uintptr_t ElfScanner::findDebugSymbol(const std::string &symbolName) |
| 1253 | { |
| 1254 | const auto &syms = dsymbols(); |
| 1255 | auto it = syms.find(symbolName); |
| 1256 | return it != syms.end() ? it->second : 0; |
| 1257 | } |
| 1258 | |
| 1259 | bool ElfScannerMgr::isValidELF(uintptr_t elfBase) const |
| 1260 | { |
| 1261 | if (!_pMem || !elfBase) |
| 1262 | return false; |
| 1263 | |
| 1264 | char magic[4] = {0}; |
| 1265 | return _pMem->Read(elfBase, magic, sizeof(magic)) && memcmp(magic, "\177ELF", 4) == 0; |
| 1266 | } |
| 1267 | |
| 1268 | ElfScanner &ElfScannerMgr::getProgramElf() |
| 1269 | { |
| 1270 | if (!_pMem) |
| 1271 | return _programElf; |
| 1272 | |
| 1273 | if (!_programElf.isValid() || !_programElf.dynamic()) |
| 1274 | { |
| 1275 | std::string path = KittyUtils::String::fmt("/proc/%d/exe", _pMem->processID()); |
| 1276 | char exePath[0xff] = {}; |
| 1277 | int ret = int(readlink(path.c_str(), exePath, 0xff)); |
| 1278 | if (ret == -1 || exePath[0] == '\0') |
| 1279 | { |