You've already forked Magisk
mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-09-06 06:36:58 +00:00
Refactor Zygisk loading
Co-authored-by: topjohnwu <topjohnwu@gmail.com>
This commit is contained in:
+29
-13
@@ -12,9 +12,9 @@ struct map_info {
|
||||
uintptr_t end;
|
||||
uintptr_t off;
|
||||
int perms;
|
||||
const char *path;
|
||||
unsigned long inode;
|
||||
|
||||
map_info() : start(0), end(0), off(0), perms(0), path(nullptr) {}
|
||||
map_info() : start(0), end(0), off(0), perms(0), inode(0) {}
|
||||
};
|
||||
|
||||
} // namespace
|
||||
@@ -31,8 +31,8 @@ static void parse_maps(int pid, Func fn) {
|
||||
char perm[5];
|
||||
int path_off;
|
||||
|
||||
if (sscanf(line, "%" PRIxPTR "-%" PRIxPTR " %4s %" PRIxPTR " %*x:%*x %*d %n%*s",
|
||||
&info.start, &info.end, perm, &info.off, &path_off) != 4)
|
||||
if (sscanf(line, "%" PRIxPTR "-%" PRIxPTR " %4s %" PRIxPTR " %*x:%*x %lu %n%*s",
|
||||
&info.start, &info.end, perm, &info.off, &info.inode, &path_off) != 5)
|
||||
return true;
|
||||
|
||||
// Parse permissions
|
||||
@@ -43,22 +43,38 @@ static void parse_maps(int pid, Func fn) {
|
||||
if (perm[2] != '-')
|
||||
info.perms |= PROT_EXEC;
|
||||
|
||||
info.path = line + path_off;
|
||||
|
||||
return fn(info);
|
||||
return fn(info, line + path_off);
|
||||
});
|
||||
}
|
||||
|
||||
static vector<map_info> find_maps(const char *name) {
|
||||
vector<map_info> maps;
|
||||
parse_maps(getpid(), [=, &maps](map_info &info) -> bool {
|
||||
if (strcmp(info.path, name) == 0)
|
||||
parse_maps(getpid(), [=, &maps](const map_info &info, const char *path) -> bool {
|
||||
if (strcmp(path, name) == 0)
|
||||
maps.emplace_back(info);
|
||||
return true;
|
||||
});
|
||||
return maps;
|
||||
}
|
||||
|
||||
std::pair<void *, size_t> find_map_range(const char *name, unsigned long inode) {
|
||||
vector<map_info> maps = find_maps(name);
|
||||
uintptr_t start = 0u;
|
||||
uintptr_t end = 0u;
|
||||
for (const auto &map : maps) {
|
||||
if (map.inode == inode) {
|
||||
if (start == 0) {
|
||||
start = map.start;
|
||||
end = map.end;
|
||||
} else if (map.start == end) {
|
||||
end = map.end;
|
||||
}
|
||||
}
|
||||
}
|
||||
LOGD("found map %s with start = %zx, end = %zx\n", name, start, end);
|
||||
return make_pair(reinterpret_cast<void *>(start), end - start);
|
||||
}
|
||||
|
||||
void unmap_all(const char *name) {
|
||||
vector<map_info> maps = find_maps(name);
|
||||
for (map_info &info : maps) {
|
||||
@@ -91,10 +107,10 @@ void remap_all(const char *name) {
|
||||
|
||||
uintptr_t get_function_off(int pid, uintptr_t addr, char *lib) {
|
||||
uintptr_t off = 0;
|
||||
parse_maps(pid, [=, &off](map_info &info) -> bool {
|
||||
parse_maps(pid, [=, &off](const map_info &info, const char *path) -> bool {
|
||||
if (addr >= info.start && addr < info.end) {
|
||||
if (lib)
|
||||
strcpy(lib, info.path);
|
||||
strcpy(lib, path);
|
||||
off = addr - info.start + info.off;
|
||||
return false;
|
||||
}
|
||||
@@ -105,8 +121,8 @@ uintptr_t get_function_off(int pid, uintptr_t addr, char *lib) {
|
||||
|
||||
uintptr_t get_function_addr(int pid, const char *lib, uintptr_t off) {
|
||||
uintptr_t addr = 0;
|
||||
parse_maps(pid, [=, &addr](map_info &info) -> bool {
|
||||
if (strcmp(info.path, lib) == 0 && (info.perms & PROT_EXEC)) {
|
||||
parse_maps(pid, [=, &addr](const map_info &info, const char *path) -> bool {
|
||||
if (strcmp(path, lib) == 0 && (info.perms & PROT_EXEC)) {
|
||||
addr = info.start - info.off + off;
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user