Format:ELF (Executable and Linkable Format)/Android packed relocations and RELR
The compact relocation encodings of Android and modern Linux ELF files: Android's APS2 SLEB128 packed relocations (DT_ANDROID_REL/RELA), SHT_RELR/DT_RELR bitmaps and their Android-specific tag numbers.
Why they exist
PIE executables and shared libraries carry one R_*_RELATIVE relocation (16 or 24 bytes) per pointer in data, which is megabytes for big programs. Two encodings shrink that.
Tags and sections
| Name | Value | Meaning |
|---|---|---|
| DT_ANDROID_REL / DT_ANDROID_RELSZ | 0x6000000f / 0x60000010 | packed Rel table (ELF32) |
| DT_ANDROID_RELA / DT_ANDROID_RELASZ | 0x60000011 / 0x60000012 | packed Rela table (ELF64) |
| SHT_ANDROID_REL / SHT_ANDROID_RELA | 0x60000001 / 0x60000002 | the sections (usually .rel.dyn / .rela.dyn with APS2 content)
|
| SHT_RELR / DT_RELR, DT_RELRSZ, DT_RELRENT | 19 / 36, 35, 37 | standard RELR |
| SHT_ANDROID_RELR | 0x6fffff00 | old experimental Android number for RELR |
| DT_ANDROID_RELR, RELRSZ, RELRENT, RELRCOUNT | 0x6fffe000, 0x6fffe001, 0x6fffe003, 0x6fffe005 | old experimental tags; bionic still accepts them |
Bionic's linker only accepts the packed form matching the file class: it rejects DT_ANDROID_REL in a Rela (ELF64) file and DT_ANDROID_RELA in an ELF32 one.
APS2 packed relocations
The data starts with the four bytes 41 50 53 32 ("APS2"), checked by the linker before decoding. After that comes a stream of SLEB128 numbers (7 bits per byte, low bits first, bit 7 = continue, sign-extended from bit 6 of the last byte):
- total number of relocations
- initial r_offset
- groups until the count is exhausted. Each group: group size, group flags, then if flag 2: the r_offset delta shared by all; if flag 1: the r_info shared by all; (RELA) if flags have 8 but not 4: every relocation has its own addend; 8 and 4: a single addend delta added once per group; neither: addend 0. Then per relocation: offset delta (unless flag 2), r_info (unless flag 1), addend delta (flag 8 without flag 4).
Flag bits: 1 RELOCATION_GROUPED_BY_INFO, 2 RELOCATION_GROUPED_BY_OFFSET_DELTA, 4 RELOCATION_GROUPED_BY_ADDEND, 8 RELOCATION_GROUP_HAS_ADDEND. Offsets, infos and addends are running values: each is a delta from the previous relocation. lld writes this with --pack-dyn-relocs=android; the Chromium case measured in its source comment went from 2,911,184 to 174,693 bytes.
RELR
DT_RELR points to an array of pointer-sized words (8 bytes on 64-bit). All entries are R_*_RELATIVE for words that already contain the link-time address; the loader adds the load bias. Decoding:
- even word: an address; relocate that word and set
base = address + wordsize. - odd word: a bitmap; bit k (k from 1) set means the word at
base + (k-1)*wordsizeis relocated; afterwardsbase += (8*wordsize - 1) * wordsize(63 words on 64-bit, 31 on 32-bit).
Bionic's linker accepts DT_RELR and the older DT_ANDROID_RELR numbers (linker.cpp); glibc added DT_RELR in 2.36 (unverified here: version from memory); linkers need -z pack-relative-relocs (GNU ld, LLD) and glibc programs using it import GLIBC_ABI_DT_RELR in .gnu.version_r, a marker version in libc.so.6 (unverified here: confirm with readelf -V).
Both encodings are invisible to naive tools that only read .rela.dyn as Elf_Rela records: a sample parsed that way gives garbage or an empty list. Look for the APS2 magic or a DT_RELR tag first.