Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 Introduction: | 1 Introduction: |
| 2 ------------- | 2 ------------- |
| 3 | 3 |
| 4 R_ARM_RELATIVE relocations are the bulk of dynamic relocations (the .rel.dyn | 4 Relative relocations are the bulk of dynamic relocations (the .rel.dyn |
| 5 section) in libchrome.<version>.so. The ELF standard representation of them | 5 or .rela.dyn sections) in libchrome.<version>.so. The ELF standard |
| 6 is wasteful. | 6 representation of them is wasteful. |
| 7 | 7 |
| 8 Packing uses run length encoding to store them more efficiently. Packed | 8 Packing uses combinations of run length encoding, delta encoding, and LEB128 |
|
rmcilroy
2014/07/28 10:08:44
nit - s/combinations/a combination
simonb (inactive)
2014/07/28 12:20:56
Done.
| |
| 9 relocations are placed in a new .android.rel.dyn section. Packing reduces | 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 | |
| 10 the footprint of libchrome.<version>.so in the filesystem, in APK downloads, | 11 the footprint of libchrome.<version>.so in the filesystem, in APK downloads, |
| 11 and in memory when loaded on the device. | 12 and in memory when loaded on the device. |
| 12 | 13 |
| 13 A packed libchrome.<version>.so is designed so that it can be loaded directly | 14 A packed libchrome.<version>.so is designed so that it can be loaded directly |
| 14 on Android, but requires the explicit support of a crazy linker that has been | 15 on Android, but requires the explicit support of a crazy linker that has been |
| 15 extended to understand packed relocations. Packed relocations are currently | 16 extended to understand packed relocations. Packed relocations are currently |
| 16 only supported on ARM. | 17 only supported on ARM. |
| 17 | 18 |
| 18 A packed libchrome.<version>.so cannot currently be used with the standard | 19 A packed libchrome.<version>.so cannot currently be used with the standard |
| 19 Android runtime linker. | 20 Android runtime linker. |
| 20 | 21 |
| 21 See src/*.h for design and implementation notes. | 22 See src/*.h for design and implementation notes. |
| 22 | 23 |
| 23 | 24 |
| 24 Notes: | 25 Notes: |
| 25 ------ | 26 ------ |
| 26 | 27 |
| 27 Packing does not adjust debug data. An unstripped libchrome.<version>.so | 28 Packing does not adjust debug data. An unstripped libchrome.<version>.so |
| 28 can be packed and will run, but may no longer be useful for debugging. | 29 can be packed and will run, but may no longer be useful for debugging. |
| 29 | 30 |
| 30 Unpacking on the device requires the explicit support of an extended crazy | 31 Unpacking on the device requires the explicit support of an extended crazy |
| 31 linker. Adds the following new .dynamic tags, used by the crazy linker to | 32 linker. Adds the following new .dynamic tags, used by the crazy linker to |
| 32 find the packed .android.rel.dyn section data: | 33 find the packed .android.rel.dyn or .android.rela.dyn section data: |
| 33 | 34 |
| 34 DT_ANDROID_ARM_REL_OFFSET = DT_LOPROC (Processor specific: 0x70000000) | 35 DT_ANDROID_ARM_REL_OFFSET = DT_LOPROC (Processor specific: 0x70000000) |
|
rmcilroy
2014/07/28 10:08:44
/s/DT_ANDROID_ARM_REL_OFFSET/DT_ANDROID_REL_OFFSET
simonb (inactive)
2014/07/28 12:20:55
Updated in patch set 4 (you're commenting here on
| |
| 35 - The offset of .android.rel.dyn data in libchrome.<version>.so | 36 - The offset of packed relocation data in libchrome.<version>.so |
| 36 DT_ANDROID_ARM_REL_SIZE = DT_LOPROC + 1 (Processor Specific: 0x70000001) | 37 DT_ANDROID_ARM_REL_SIZE = DT_LOPROC + 1 (Processor Specific: 0x70000001) |
| 37 - The size of .android.rel.dyn data in bytes | 38 - The size of packed relocation data in bytes |
| 38 | 39 |
| 39 The format of .android.rel.dyn data is: | 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. | |
| 43 | |
| 44 Where libchrome.<version>.so contains relocations without addends, the format | |
| 45 of .android.rel.dyn data is: | |
| 40 | 46 |
| 41 "APR1" identifier | 47 "APR1" identifier |
| 42 N: the number of count-delta pairs in the encoding | 48 N: the number of count-delta pairs in the encoding |
| 43 A: the initial offset | 49 A: the initial offset |
| 44 N * C,D: N count-delta pairs | 50 N * C,D: N count-delta pairs |
| 45 | 51 |
| 52 Where libchrome.<version>.so contains relocations with addends, the format | |
| 53 of .android.rela.dyn data is: | |
| 54 | |
| 55 "APA1" identifier | |
| 56 N: the number of addr-addend delta pairs in the encoding | |
| 57 N * A,V: N addr-addend delta pairs | |
| 58 | |
| 46 All numbers in the encoding stream are stored as LEB128 values. For details | 59 All numbers in the encoding stream are stored as LEB128 values. For details |
| 47 see http://en.wikipedia.org/wiki/LEB128. | 60 see http://en.wikipedia.org/wiki/LEB128. |
| 48 | 61 |
| 49 The streaming unpacking algorithm is: | 62 The streaming unpacking algorithm for 32 bit ARM is: |
| 50 | 63 |
| 51 skip over "APR1" | 64 skip over "APR1" |
| 52 pairs, addr = next leb128 value, next leb128 value | 65 pairs, addr = next leb128 value, next leb128 value |
| 53 emit R_ARM_RELATIVE relocation with r_offset = addr | 66 emit R_ARM_RELATIVE relocation with r_offset = addr |
| 54 while pairs: | 67 while pairs: |
| 55 count, delta = next leb128 value, next leb128 value | 68 count, delta = next leb128 value, next leb128 value |
| 56 while count: | 69 while count: |
| 57 addr += delta | 70 addr += delta |
| 58 emit R_ARM_RELATIVE relocation with r_offset = addr | 71 emit R_ARM_RELATIVE relocation with r_offset = addr |
| 59 count-- | 72 count-- |
| 60 pairs--; | 73 pairs--; |
| 61 | 74 |
| 75 The streaming unpacking algorithm for 64 bit ARM is: | |
| 76 | |
| 77 skip over "APA1" | |
| 78 pairs = next signed leb128 value | |
| 79 addr, addend = 0, 0 | |
| 80 while pairs: | |
| 81 addr += next signed leb128 value | |
| 82 addend += next signed leb128 value | |
| 83 emit R_ARM_RELATIVE relocation with r_offset = addr, r_addend = addend | |
|
rmcilroy
2014/07/28 10:08:44
R_AARCH64_RELATIVE
simonb (inactive)
2014/07/28 12:20:55
Done. Good catch, thanks.
| |
| 84 pairs--; | |
| 85 | |
| 62 | 86 |
| 63 Usage instructions: | 87 Usage instructions: |
| 64 ------------------- | 88 ------------------- |
| 65 | 89 |
| 66 To pack relocations, add an empty .android.rel.dyn and then run the tool: | 90 To pack relocations, add an empty .android.rel.dyn or .android.rela.dyn and |
| 91 then run the tool: | |
| 67 | 92 |
| 68 echo -n 'NULL' >/tmp/small | 93 echo -n 'NULL' >/tmp/small |
| 69 arm-linux-gnueabi-objcopy \ | 94 if file libchrome.<version>.so | grep -q 'ELF 32'; then |
| 70 --add-section .android.rel.dyn=/tmp/small \ | 95 arm-linux-androideabi-objcopy |
| 71 libchrome.<version>.so libchrome.<version>.so.packed | 96 --add-section .android.rel.dyn=/tmp/small |
| 97 libchrome.<version>.so libchrome.<version>.so.packed | |
| 98 else | |
| 99 aarch64-linux-android-objcopy | |
| 100 --add-section .android.rela.dyn=/tmp/small | |
| 101 libchrome.<version>.so libchrome.<version>.so.packed | |
| 102 fi | |
| 72 rm /tmp/small | 103 rm /tmp/small |
| 73 relocation_packer libchrome.<version>.so.packed | 104 relocation_packer libchrome.<version>.so.packed |
| 74 | 105 |
| 75 To unpack and restore the shared library to its original state: | 106 To unpack and restore the shared library to its original state: |
| 76 | 107 |
| 77 cp libchrome.<version>.so.packed unpackable | 108 cp libchrome.<version>.so.packed unpackable |
| 78 relocation_packer -u unpackable | 109 relocation_packer -u unpackable |
| 79 arm-linux-gnueabi-objcopy \ | 110 if file libchrome.<version>.so | grep -q 'ELF 32'; then |
| 80 --remove-section=.android.rel.dyn unpackable libchrome.<version>.so | 111 arm-linux-androideabi-objcopy \ |
| 112 --remove-section=.android.rel.dyn unpackable libchrome.<version>.so | |
| 113 else | |
| 114 aarch64-linux-android-objcopy \ | |
| 115 --remove-section=.android.rela.dyn unpackable libchrome.<version>.so | |
| 116 endif | |
| 81 rm unpackable | 117 rm unpackable |
| 82 | 118 |
| 83 | 119 |
| 84 Bugs & TODOs: | 120 Bugs & TODOs: |
| 85 ------------- | 121 ------------- |
| 86 | 122 |
| 87 Currently only supports arm32. Support for arm64 requires some extension | |
| 88 and modification. | |
| 89 | |
| 90 Requires two free slots in the .dynamic section. Uses these to add data that | 123 Requires two free slots in the .dynamic section. Uses these to add data that |
| 91 tells the crazy linker where to find the packed .android.rel.dyn data. Fails | 124 tells the crazy linker where to find the packed relocation data. Fails |
| 92 if insufficient free slots exist (use gold --spare-dynamic-slots to increase | 125 if insufficient free slots exist (use gold --spare-dynamic-slots to increase |
| 93 the allocation). | 126 the allocation). |
| 94 | 127 |
| 95 Requires libelf 0.158 or later. Earlier libelf releases may be buggy in | 128 Requires libelf 0.158 or later. Earlier libelf releases may be buggy in |
| 96 ways that prevent the packer from working correctly. | 129 ways that prevent the packer from working correctly. |
| 97 | 130 |
| 98 | 131 |
| 99 Testing: | 132 Testing: |
| 100 -------- | 133 -------- |
| 101 | 134 |
| 102 Unittests run under gtest, on the host system. | 135 Unittests run under gtest, on the host system. |
| OLD | NEW |