| 1220 | ElfScanner ElfScannerMgr::findElf(const std::string &path, EScanElfType type, EScanElfFilter filter) |
| 1221 | #else |
| 1222 | ElfScanner ElfScannerMgr::findElf(const std::string &path) |
| 1223 | #endif |
| 1224 | { |
| 1225 | ElfScanner ret{}; |
| 1226 | |
| 1227 | if (!_pMem || path.empty()) |
| 1228 | return ret; |
| 1229 | |
| 1230 | std::vector<ElfScanner> elfs; |
| 1231 | std::vector<ElfScanner> dyn_elfs; |
| 1232 | |
| 1233 | #ifdef __ANDROID__ |
| 1234 | const auto allElfs = getAllELFs(type, filter); |
| 1235 | #else |
| 1236 | const auto allElfs = getAllELFs(); |
| 1237 | #endif |
| 1238 | for (const auto &it : allElfs) |
| 1239 | { |
| 1240 | if (it.isValid() && KittyUtils::String::endsWith(it.realPath(), path)) |
| 1241 | { |
| 1242 | if (it.dynamic() && it.dynamics().size() > 0) |
| 1243 | dyn_elfs.push_back(it); |
| 1244 | else |
| 1245 | elfs.push_back(it); |
| 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | if (elfs.empty() && dyn_elfs.empty()) |
| 1250 | return ret; |
| 1251 | |
| 1252 | if (dyn_elfs.size() > 0) |
| 1253 | { |
| 1254 | if (dyn_elfs.size() == 1) |
| 1255 | return dyn_elfs[0]; |
| 1256 | |
| 1257 | int nMostSegments = 0; |
| 1258 | for (auto &it : dyn_elfs) |
| 1259 | { |
| 1260 | int numSegments = it.segments().size(); |
| 1261 | // >= to get latest |
| 1262 | if (numSegments >= nMostSegments) |
| 1263 | { |
| 1264 | ret = it; |
| 1265 | nMostSegments = numSegments; |
| 1266 | } |
| 1267 | } |
| 1268 | } |
| 1269 | else if (elfs.size() > 0) |
| 1270 | { |
| 1271 | if (elfs.size() == 1) |
| 1272 | return elfs[0]; |
| 1273 | |
| 1274 | int nMostSegments = 0; |
| 1275 | for (auto &it : elfs) |
| 1276 | { |
| 1277 | int numSegments = it.segments().size(); |
| 1278 | // >= to get latest |
| 1279 | if (numSegments >= nMostSegments) |