What Android Library Hiding Leaves Behind.
Remapping, manual mapping, and the limits of detection.
An exploration of native library loading on Android and what an in-process defender can still observe when a library tries to hide.
The code accompanying this article is available in the android-library-hiding repository.
Introduction
I started this project after seeing cheats use remapping to hide the shared libraries containing their code. I wanted to understand what the technique concealed and how a defender could detect it.
That question soon grew into a broader investigation: how else could an attacker load code while avoiding pathname checks and the dynamic linker’s records? And how much could a defender reliably detect from inside the same process?
This article follows that investigation through two techniques: remapping a library loaded through dlopen, and manually mapping a library without registering it with Android’s dynamic linker.
Environment
I built a small Android application with two text labels whose contents come from a native library, libdefend.so. The labels display the results of the detection checks and refresh whenever I tap either label or return to the application.
Figure 1. Detection results in the test application.

The upper label shows the process ID, which I used for debugging, followed by the results of scanning /proc/self/maps. In this screenshot, that check identifies libdefend.so.
The lower label combines linker enumeration with a memory-permission check. It reports libinjector.so and libremap.so through the linker’s records and flags them as unexpected application libraries. These entries do not establish that either library still has a pathname in /proc/self/maps; the two labels draw on different sources. The final rwxp entry comes from the permission check and points to a weakness in my manual loader, discussed later.
The output uses the following labels:
app - a library associated with the application's native library path
blacklisted - a library whose name matches the blacklist
illegitimate - an application library absent from the allowlist
rwxp - a readable, writable, executable private mapping
For this experiment, the allowlist contains only libdefend.so. I added three more native libraries to simulate code introduced by a modified APK: an injector and two payloads. The detector treats those libraries as unauthorized even though the test application deliberately includes them.
Figure 2. Test application structure.
flowchart TD
application["Test application"]
libdefend.so["libdefend.so<br/>Native detection engine"]
check["Application resumed or label tapped<br/>Run all checks"]
report["Report<br/>Display results in the text labels"]
libinjector.so["libinjector.so<br/>Loads both experimental payloads"]
ctor["Injector constructor"]
libremap.so["libremap.so<br/>Payload loaded through dlopen"]
libmanmap.so["libmanmap.so<br/>Payload loaded by the manual loader"]
remap["Remap<br/>Injector remaps libremap.so"]
application --> libdefend.so
application --> libinjector.so
libdefend.so --> check --> report
libinjector.so --> ctor
ctor --> |Loads| libremap.so --> remap
ctor --> |Loads| libmanmap.so
These four libraries formed my test environment. I ran the experiments in an ARM64 Android emulator on a MacBook, using 4 KiB (4096-byte) memory pages.
Scope
The application loads both the defender and the injector, so the experiment begins with code execution already available inside the process. Obtaining that initial execution is outside the scope of this article.
My focus is on how the payloads are loaded and what each loading technique leaves visible. Detecting the injector is also a valid defensive approach, but I do not explore attempts to remove its linker registration, tamper with the detection functions, or unload it after its work is complete.
The checks assume that the scanner and the information returned to it have not been tampered with. This matters when the code being examined runs inside the same process as the defender.
The injector
Both experiments use libinjector.so to coordinate loading. Its constructor runs before the application requests the detector output, allowing the payloads to be loaded before the checks begin.
The constructor first loads libmanmap.so with my experimental manual loader. It then loads libremap.so through dlopen and applies the remapping helper to that library. One payload therefore bypasses the normal loading path, while the other goes through Android’s dynamic linker before its memory mappings are replaced.
Both payload constructors also call the remapping helper against libinjector.so. Once its file-backed mappings have been replaced, a pathname scan loses that identifying information. Its linker registration remains available to the second check.
Remapping
Remapping hides a library’s pathname by replacing its file-backed mappings with anonymous ones. In this implementation, the replacement mappings end up at the same virtual addresses as the originals. The useful effect comes from removing the file association, not from scattering the library across the address space.
For each target mapping, the helper allocates anonymous memory and copies the original contents into it. It then uses mremap to move that anonymous mapping to the original address, replacing the file-backed mapping, and restores the original permissions.
The code and data remain at the addresses the library expects, but the replaced mappings no longer carry the library’s pathname in /proc/self/maps.
I first encountered this technique in reveny’s Android-Library-Remap-Hide repository. Since then, I have seen it used in several mobile cheat clients and have experimented with it myself.
Weaknesses of remapping
The first concern is where the remapping code executes. In this implementation, I keep the helper outside the library whose mappings it is replacing: the injector remaps its payload, and the payload remaps the injector. That removes their pathnames from the replaced mappings, but it does not remove every trace of either library.
It also introduces an execution hazard. A thread can attempt to execute, read, or write a region while the helper is replacing its mapping or changing its permissions. If the required permissions are temporarily unavailable, that access can fault. Concurrent writes can also make the copied contents inconsistent with the original.
Whether these races occur depends on what else is using the target memory during remapping. They are a reliability concern, but addressing them is outside this experiment’s scope.
The second weakness is more useful to the defender: remapping leaves the dynamic linker’s metadata intact. A library loaded through dlopen is recorded by Android’s Bionic linker. Bionic’s implementation of linker enumeration walks its list of loaded objects and reports their names, load addresses, and program headers.
The public interface for that enumeration is dl_iterate_phdr. It provides another way to identify the library even after its pathname disappears from the memory map.
Figure 3. What remapping changes and what it leaves intact.
flowchart TB
subgraph maps["Memory mappings - /proc/self/maps"]
direction LR
M0["Before remapping<br/>Address range: A–B<br/>Permissions: r-xp<br/>Path: /…/libremap.so"]
M1["After remapping<br/>Address range: A–B<br/>Permissions: r-xp<br/>Anonymous; no pathname"]
M0 -->|"File backing replaced"| M1
end
subgraph linker["Linker metadata - dl_iterate_phdr"]
direction LR
L0["Before remapping<br/>Name: libremap.so<br/>Base + program headers<br/>describe range A–B"]
L1["After remapping<br/>Name: libremap.so<br/>Base + program headers<br/>still describe range A–B"]
L0 -->|"Record unchanged"| L1
end
maps ~~~ linker
The illustrated segment keeps its virtual addresses and, once remapping is complete, its original permissions. Its pathname disappears from /proc/self/maps, while the linker can still report the object and describe its loaded segments.
Manual mapping
This led to the next question: how could a payload avoid identification through both of those views? Within the scope of these checks, the requirements were:
- Its memory mappings must not expose the library’s pathname.
- The payload must not appear in the linker’s list of loaded objects.
Remapping addressed the first requirement. To address the second, I needed a way to load the payload without registering it through the normal dynamic linker path. I chose to write a loader myself.
This technique is usually called manual mapping, a term I had encountered in the Windows context. I did not find a Linux example that suited this experiment, so I built a minimal ELF64 loader while learning which parts of the linker’s work I needed to reproduce.
That is where a day and a half of my weekend went.
The result supports enough of the ARM64 ELF loading process to run my test payload. Its broad structure is shown below.
Figure 4. Structure of the experimental manual loader.
flowchart TD
Allocation["Allocate memory<br/>Reserve the span of the loadable segments"]
Segments["Copy segments<br/>Copy file contents and zero the remaining data"]
Dynamic["Read ELF metadata<br/>Locate symbol and relocation tables"]
Relocations["Apply relocations<br/>Process RELA and PLT relocation entries"]
Symbols["Symbol relocations<br/>Resolve symbols in the image or through dlsym"]
Relatives["Relative relocations<br/>Apply the load bias"]
Indirects["Indirect relocations<br/>Call the resolver functions"]
Header["Clear the mapped ELF header"]
Protection["mprotect<br/>Attempt to apply segment permissions"]
ctors[".init_array<br/>Run constructors"]
Allocation --> Segments --> Dynamic --> Relocations
Relocations --> Symbols
Relocations --> Relatives
Relocations --> Indirects
Symbols --> Header
Relatives --> Header
Indirects --> Header
Header --> Protection --> ctors
There are several moving parts, but the allocation step is straightforward. I first calculate the page-aligned address span covering all PT_LOAD segments:
// Calculate the memory span covering the ELF's loadable segments.
auto page_size = sysconf(_SC_PAGESIZE);
bool first_load{true};
Elf64_Addr min_vaddr = UINT64_MAX, max_vaddr = 0, mapping_size = 0;
for (int i = 0; i < ehdr->e_phnum; ++i)
{
Elf64_Phdr *phdr = reinterpret_cast<Elf64_Phdr *>(cursor);
if (phdr->p_type == PT_LOAD)
{
auto segment_min = phdr->p_vaddr & ~(page_size - 1);
auto segment_max = (phdr->p_vaddr + phdr->p_memsz + page_size - 1) & ~(page_size - 1);
log("segment_min: %p, segment_max: %p", reinterpret_cast<void *>(segment_min), reinterpret_cast<void *>(segment_max));
if (first_load)
{
min_vaddr = segment_min;
first_load = false;
}
else if (segment_min < min_vaddr)
min_vaddr = segment_min;
if (segment_max > max_vaddr)
max_vaddr = segment_max;
}
cursor += sizeof(Elf64_Phdr);
}
mapping_size = max_vaddr - min_vaddr;
I then allocate that entire span as one anonymous mapping with read, write, and execute permissions:
auto mapped_lib = mmap(nullptr, mapping_size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (mapped_lib == MAP_FAILED)
return nullptr;
auto base = static_cast<uint8_t *>(mapped_lib);
uintptr_t load_bias = reinterpret_cast<uintptr_t>(base) - min_vaddr;
Using one contiguous allocation is not itself the problem. The shortcut that matters here is starting with RWX permissions and failing to restrict them correctly across the resulting image.
Next, I copy each loadable segment’s file contents into the allocation and zero the portion that exists only in memory, including .bss. The loader reads the symbol tables and dynamic metadata to locate and apply the supported RELA and procedure linkage table (PLT) relocations.
It also clears the mapped ELF header. This removes the ELF magic and the information stored in that header, making simple header-based scans less useful. It does not erase all ELF metadata or make the allocation invisible, and clearing the header can affect code or tools that expect it to remain available.
Finally, the loader attempts to apply the segment permissions with mprotect and invokes the constructors listed in .init_array.
That is enough for this test payload. The loaded image has anonymous mappings and no entry in the linker’s list, satisfying the two requirements above. It is still an experimental loader, with incomplete support for features such as thread-local storage (TLS) and exception handling.
A weakness in this implementation
The pathname and linker checks do not directly identify this manually mapped payload. That does not establish that manual mapping is universally undetectable: the memory remains visible, and a particular loader or payload may leave other recognizable evidence.
My implementation leaves an especially obvious clue. Its handling of page permissions is incomplete, so part of the allocation remains readable, writable, and executable. I kept the loader minimal for this experiment; the published example is not a complete replacement for Android’s dynamic linker.
The following memory-map excerpt shows the relevant region:
6fdb5e8000-6fdb5f1000 r--p 00000000 00:00 0
6fdb5f1000-6fdb5f4000 ---p 00000000 00:00 0
6fdb5f4000-6fdb5f5000 rw-p 00000000 00:00 0
6fdb5f5000-6fdb5f7000 rw-p 00000000 00:00 0 [anon:.bss]
6fdb620000-6fdb6f6000 r-xp 00000000 00:00 0
6fdb6f6000-6fdb708000 rwxp 00000000 00:00 0
6fdb708000-6fdb7e4000 r-xp 00000000 00:00 0
6fdb7e4000-6fdb7e7000 ---p 00000000 00:00 0
6fdb7e7000-6fdb7f1000 r--p 00000000 00:00 0
6fdb7f1000-6fdb7f4000 ---p 00000000 00:00 0
6fdb7f4000-6fdb7f5000 rw-p 00000000 00:00 0
The range from 6fdb6f6000 to 6fdb708000 is 72 KiB and has rwxp permissions. In this experiment, it belongs to the manually mapped payload. This gives the defender a useful lead, with some important limits.
Defending
The first check takes the familiar approach of reading /proc/self/maps. Each entry describes a mapped memory range, including its permissions and, where available, its backing pathname.
The scan_maps() function in defend/src/anti.cpp uses those pathnames to look for blacklisted names and unexpected libraries associated with the application’s native library path. In this controlled setup, those checks flag libraries that a modified APK might introduce. A name or path match is a detection rule for the experiment, not proof of malicious behavior by itself.
Figure 5. Structure of the pathname check.
flowchart TD
maps["Open /proc/self/maps"]
parse["Parse entries line by line"]
name["Inspect library pathname"]
blacklist["Name matches the blacklist?"]
isapp["Application library path<br/>but absent from the allowlist?"]
detect["Report a finding"]
maps --> parse --> name
name --> blacklist
name --> isapp
isapp --> |Yes| detect
blacklist --> |Yes| detect
Remapping removes the pathname from the replaced mappings, so this check loses the information it uses to identify them.
The linker’s records offer a second source. The scan_objs() function applies similar rules to the names returned by dl_iterate_phdr. In pseudocode:
for each object reported by dl_iterate_phdr:
path = object.dlpi_name
name = library_name(path)
if name is blacklisted:
report blacklisted
if path belongs to the application's library directory
and name is absent from the allowlist:
report illegitimate
This catches the remapped payload because replacing its memory mappings does not remove its linker registration. The records belong to Android’s userspace linker; they are distinct from the memory mappings tracked by the kernel.
The manual loader avoids that registration as well as the payload’s file-backed pathname. Even so, /proc/self/maps still shows the anonymous memory it allocates. What is missing is the direct association between those ranges and the payload’s library name.
Further investigation therefore needs evidence beyond these two name-based checks. A particular implementation might leave recognizable strings, byte patterns, ELF metadata, or unusual permissions. Those signals need to be evaluated individually; anonymous executable memory alone does not identify an injected library.
Interpreting the RWX finding
For this example, I use the permission mistake in my manual loader. The scan_anonymous() function reports entries with rwxp permissions. Despite its name, the current function checks permissions on every maps entry; it does not independently establish that the mapping is anonymous.
Here, r, w, and x mean readable, writable, and executable. The p denotes a private mapping. A writable executable region deserves attention, but it can exist for several reasons:
- Legacy compatibility. On Linux, the
READ_IMPLIES_EXECpersonality flag can cause a request for readable memory to imply executable memory. Its applicability depends on the architecture and execution environment; it should not be assumed to explain an arbitrary ARM64 Android mapping. - Just-in-time compilation. Runtimes generate machine code and must arrange for it to be written and executed. Permission handling varies by runtime and version. The historical ART JIT code cache implementation linked here, for example, uses RWX permissions during allocation and code updates. That source is an example of legitimate RWX use, not a description of every Android version.
- Injected code or an incomplete loader. A payload loader may leave a mapping writable and executable, as mine does. Those permissions can support injected code, but they do not reveal the mapping’s origin or purpose by themselves.
In this controlled experiment, I know which allocation the loader created, so I can associate the 72 KiB RWX region with the payload. The detector itself only reports the permission match. In a real application, that match would need further investigation before it could support a conclusion about exploitation.
This check therefore exposes a flaw in my implementation. A loader that does not leave the same permissions behind would not produce this particular signal, and the defender would need other evidence.
Results
The comparison covers three loading methods: ordinary dlopen as a baseline, dlopen followed by remapping, and manual mapping. The two experimental techniques progressively remove identifying information available to the pathname and linker checks.
Remapping removes the pathname from the replaced mappings but leaves the payload visible through linker enumeration. Manual mapping removes both of those direct identifiers for the payload. Its memory remains visible, and my implementation additionally leaves an RWX region that the permission check reports.
Table 1. Visibility of the payload under each loading method in this test setup.
| Loading method | Payload pathname in /proc/self/maps | Payload reported by linker enumeration | Memory mappings remain visible |
|---|---|---|---|
Ordinary dlopen | Yes | Yes | Yes |
dlopen followed by remapping | No, for replaced mappings | Yes | Yes |
| Manual mapping | No file pathname for its anonymous image | No | Yes |
These observations concern the payload’s loaded image. They do not establish that the injector, the library file on disk, or other traces of the experiment have disappeared.
Conclusions
This experiment helped separate three questions: which memory ranges exist, which shared objects the linker knows about, and which observations are sufficient to raise suspicion.
Remapping replaces file-backed mappings with anonymous ones. Pathname checks lose their identifying information, while linker enumeration can still report a library loaded through dlopen. The manual loader avoids registering its payload with the linker, but the memory it allocates remains visible in /proc/self/maps.
The RWX finding is an additional signal tied to this implementation. It helps identify an allocation worth examining in the test, but permissions alone do not establish its origin or intent. Likewise, the absence of an RWX finding would not establish that the process is free of injected code.
The conclusion I can support is that pathname checks, linker enumeration, and permission checks each provide a partial view. Combining them gives the defender more evidence, but this experiment does not establish a general detection guarantee. It also depends on the scanner and its information sources remaining trustworthy.
The next step is to compare these checks against clean baselines and isolated loading experiments across Android versions and page sizes. That would clarify which observations remain useful beyond this test application and how often legitimate runtime behavior produces the same signals.
Thanks for reading.
Back to all posts