4 Relative relocations are the bulk of dynamic relocations (the .rel.dyn
5 or .rela.dyn sections) in libchrome.<version>.so. The ELF standard
6 representation of them is wasteful.
8 Packing uses a combination of run length encoding, delta encoding, and LEB128
9 encoding to store them more efficiently. Packed relocations are placed in
10 a new .android.rel.dyn or .android.rela.dyn section. Packing reduces
11 the footprint of libchrome.<version>.so in the filesystem, in APK downloads,
12 and in memory when loaded on the device.
14 A packed libchrome.<version>.so is designed so that it can be loaded directly
15 on Android, but requires the explicit support of a crazy linker that has been
16 extended to understand packed relocations. Packed relocations are currently
17 only supported on ARM.
19 A packed libchrome.<version>.so cannot currently be used with the standard
20 Android runtime linker.
22 See src/*.h for design and implementation notes.
28 Packing does not adjust debug data. An unstripped libchrome.<version>.so
29 can be packed and will run, but may no longer be useful for debugging.
31 Unpacking on the device requires the explicit support of an extended crazy
32 linker. Adds the following new .dynamic tags, used by the crazy linker to
33 find the packed .android.rel.dyn or .android.rela.dyn section data:
35 DT_ANDROID_REL_OFFSET = DT_LOOS (Operating System specific: 0x6000000d)
36 - The offset of packed relocation data in libchrome.<version>.so
37 DT_ANDROID_REL_SIZE = DT_LOOS + 1 (Operating System Specific: 0x6000000e)
38 - The size of packed relocation data in bytes
40 32 bit ARM libraries use relocations without addends. 64 bit ARM libraries
41 use relocations with addends. The packing strategy necessarily differs for
42 the two relocation types.
44 Where libchrome.<version>.so contains relocations without addends, the format
45 of .android.rel.dyn data is:
48 N: the number of count-delta pairs in the encoding
50 N * C,D: N count-delta pairs
52 Where libchrome.<version>.so contains relocations with addends, the format
53 of .android.rela.dyn data is:
56 N: the number of addr-addend delta pairs in the encoding
57 N * A,V: N addr-addend delta pairs
59 All numbers in the encoding stream are stored as LEB128 values. For details
60 see http://en.wikipedia.org/wiki/LEB128.
62 The streaming unpacking algorithm for 32 bit ARM is:
65 pairs, addr = next leb128 value, next leb128 value
66 emit R_ARM_RELATIVE relocation with r_offset = addr
68 count, delta = next leb128 value, next leb128 value
71 emit R_ARM_RELATIVE relocation with r_offset = addr
75 The streaming unpacking algorithm for 64 bit ARM is:
78 pairs = next signed leb128 value
81 addr += next signed leb128 value
82 addend += next signed leb128 value
83 emit R_AARCH64_RELATIVE relocation with r_offset = addr, r_addend = addend
90 To pack relocations, add an empty .android.rel.dyn or .android.rela.dyn and
93 echo -n 'NULL' >/tmp/small
94 if file libchrome.<version>.so | grep -q 'ELF 32'; then
95 arm-linux-androideabi-objcopy
96 --add-section .android.rel.dyn=/tmp/small
97 libchrome.<version>.so libchrome.<version>.so.packed
99 aarch64-linux-android-objcopy
100 --add-section .android.rela.dyn=/tmp/small
101 libchrome.<version>.so libchrome.<version>.so.packed
104 relocation_packer libchrome.<version>.so.packed
106 To unpack and restore the shared library to its original state:
108 cp libchrome.<version>.so.packed unpackable
109 relocation_packer -u unpackable
110 if file libchrome.<version>.so | grep -q 'ELF 32'; then
111 arm-linux-androideabi-objcopy \
112 --remove-section=.android.rel.dyn unpackable libchrome.<version>.so
114 aarch64-linux-android-objcopy \
115 --remove-section=.android.rela.dyn unpackable libchrome.<version>.so
123 Requires two free slots in the .dynamic section. Uses these to add data that
124 tells the crazy linker where to find the packed relocation data. Fails
125 if insufficient free slots exist (use gold --spare-dynamic-slots to increase
128 Requires libelf 0.158 or later. Earlier libelf releases may be buggy in
129 ways that prevent the packer from working correctly.
135 Unittests run under gtest, on the host system.