| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # | 2 # |
| 3 # Copyright 2014 The Chromium Authors. All rights reserved. | 3 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
| 6 | 6 |
| 7 """Pack ARM relative relocations in a library (or copy unchanged). | 7 """Pack ARM relative relocations in a library (or copy unchanged). |
| 8 | 8 |
| 9 If --enable-packing and --configuration-name=='Release', invoke the | 9 If --enable-packing and --configuration-name=='Release', invoke the |
| 10 relocation_packer tool to pack the .rel.dyn section in the given library | 10 relocation_packer tool to pack the .rel.dyn or .rela.dyn section in the given |
| 11 files. This step is inserted after the libraries are stripped. Packing | 11 library files. This step is inserted after the libraries are stripped. |
| 12 adds a new .android.rel.dyn section to the file and reduces the size of | 12 Packing adds a new .android.rel.dyn or .android.rela.dyn section to the file |
| 13 .rel.dyn accordingly. | 13 and reduces the size of .rel.dyn or .rela.dyn accordingly. |
| 14 | 14 |
| 15 Currently packing only understands ARM32 shared libraries. For all other | 15 Currently packing only understands ARM32 shared libraries. For all other |
| 16 architectures --enable-packing should be set to zero. In this case the | 16 architectures --enable-packing should be set to zero. In this case the |
| 17 script copies files verbatim, with no attempt to pack relative relocations. | 17 script copies files verbatim, with no attempt to pack relative relocations. |
| 18 | 18 |
| 19 Any library listed in --exclude-packing-list is also copied verbatim, | 19 Any library listed in --exclude-packing-list is also copied verbatim, |
| 20 irrespective of any --enable-packing setting. Typically this would be | 20 irrespective of any --enable-packing setting. Typically this would be |
| 21 'libchromium_android_linker.so'. | 21 'libchromium_android_linker.so'. |
| 22 """ | 22 """ |
| 23 | 23 |
| 24 import optparse | 24 import optparse |
| 25 import os | 25 import os |
| 26 import shlex | 26 import shlex |
| 27 import shutil | 27 import shutil |
| 28 import sys | 28 import sys |
| 29 import tempfile | 29 import tempfile |
| 30 | 30 |
| 31 from util import build_utils | 31 from util import build_utils |
| 32 | 32 |
| 33 def PackArmLibraryRelocations(android_pack_relocations, | 33 def PackArmLibraryRelocations(android_pack_relocations, |
| 34 android_objcopy, | 34 android_objcopy, |
| 35 has_relocations_with_addends, |
| 35 library_path, | 36 library_path, |
| 36 output_path): | 37 output_path): |
| 37 if not build_utils.IsTimeStale(output_path, [library_path]): | 38 if not build_utils.IsTimeStale(output_path, [library_path]): |
| 38 return | 39 return |
| 39 | 40 |
| 40 # Copy and add a 'NULL' .android.rel.dyn section for the packing tool. | 41 # Select an appropriate name for the section we add. |
| 42 if has_relocations_with_addends: |
| 43 new_section = '.android.rela.dyn' |
| 44 else: |
| 45 new_section = '.android.rel.dyn' |
| 46 |
| 47 # Copy and add a 'NULL' packed relocations section for the packing tool. |
| 41 with tempfile.NamedTemporaryFile() as stream: | 48 with tempfile.NamedTemporaryFile() as stream: |
| 42 stream.write('NULL') | 49 stream.write('NULL') |
| 43 stream.flush() | 50 stream.flush() |
| 44 objcopy_command = [android_objcopy, | 51 objcopy_command = [android_objcopy, |
| 45 '--add-section', '.android.rel.dyn=%s' % stream.name, | 52 '--add-section', '%s=%s' % (new_section, stream.name), |
| 46 library_path, output_path] | 53 library_path, output_path] |
| 47 build_utils.CheckOutput(objcopy_command) | 54 build_utils.CheckOutput(objcopy_command) |
| 48 | 55 |
| 49 # Pack R_ARM_RELATIVE relocations. | 56 # Pack R_ARM_RELATIVE relocations. |
| 50 pack_command = [android_pack_relocations, output_path] | 57 pack_command = [android_pack_relocations, output_path] |
| 51 build_utils.CheckOutput(pack_command) | 58 build_utils.CheckOutput(pack_command) |
| 52 | 59 |
| 53 | 60 |
| 54 def CopyArmLibraryUnchanged(library_path, output_path): | 61 def CopyArmLibraryUnchanged(library_path, output_path): |
| 55 if not build_utils.IsTimeStale(output_path, [library_path]): | 62 if not build_utils.IsTimeStale(output_path, [library_path]): |
| 56 return | 63 return |
| 57 | 64 |
| 58 shutil.copy(library_path, output_path) | 65 shutil.copy(library_path, output_path) |
| 59 | 66 |
| 60 | 67 |
| 61 def main(args): | 68 def main(args): |
| 62 args = build_utils.ExpandFileArgs(args) | 69 args = build_utils.ExpandFileArgs(args) |
| 63 parser = optparse.OptionParser() | 70 parser = optparse.OptionParser() |
| 64 | 71 |
| 65 parser.add_option('--configuration-name', | 72 parser.add_option('--configuration-name', |
| 66 default='Release', | 73 default='Release', |
| 67 help='Gyp configuration name (i.e. Debug, Release)') | 74 help='Gyp configuration name (i.e. Debug, Release)') |
| 68 parser.add_option('--enable-packing', | 75 parser.add_option('--enable-packing', |
| 69 choices=['0', '1'], | 76 choices=['0', '1'], |
| 70 help=('Pack relocations if 1 and configuration name is \'Release\',' | 77 help=('Pack relocations if 1 and configuration name is \'Release\',' |
| 71 ' otherwise plain file copy')) | 78 ' otherwise plain file copy')) |
| 79 parser.add_option('--has-relocations-with-addends', |
| 80 choices=['0', '1'], |
| 81 help=('Pack into \'.android.rela.dyn\' if 1, else \'.android.rel.dyn\'')) |
| 72 parser.add_option('--exclude-packing-list', | 82 parser.add_option('--exclude-packing-list', |
| 73 default='', | 83 default='', |
| 74 help='Names of any libraries explicitly not packed') | 84 help='Names of any libraries explicitly not packed') |
| 75 parser.add_option('--android-pack-relocations', | 85 parser.add_option('--android-pack-relocations', |
| 76 help='Path to the ARM relocations packer binary') | 86 help='Path to the ARM relocations packer binary') |
| 77 parser.add_option('--android-objcopy', | 87 parser.add_option('--android-objcopy', |
| 78 help='Path to the toolchain\'s objcopy binary') | 88 help='Path to the toolchain\'s objcopy binary') |
| 79 parser.add_option('--stripped-libraries-dir', | 89 parser.add_option('--stripped-libraries-dir', |
| 80 help='Directory for stripped libraries') | 90 help='Directory for stripped libraries') |
| 81 parser.add_option('--packed-libraries-dir', | 91 parser.add_option('--packed-libraries-dir', |
| 82 help='Directory for packed libraries') | 92 help='Directory for packed libraries') |
| 83 parser.add_option('--libraries', | 93 parser.add_option('--libraries', |
| 84 help='List of libraries') | 94 help='List of libraries') |
| 85 parser.add_option('--stamp', help='Path to touch on success') | 95 parser.add_option('--stamp', help='Path to touch on success') |
| 86 | 96 |
| 87 options, _ = parser.parse_args(args) | 97 options, _ = parser.parse_args(args) |
| 88 enable_packing = (options.enable_packing == '1' and | 98 enable_packing = (options.enable_packing == '1' and |
| 89 options.configuration_name == 'Release') | 99 options.configuration_name == 'Release') |
| 100 has_relocations_with_addends = (options.has_relocations_with_addends == '1') |
| 90 exclude_packing_set = set(shlex.split(options.exclude_packing_list)) | 101 exclude_packing_set = set(shlex.split(options.exclude_packing_list)) |
| 91 | 102 |
| 92 libraries = build_utils.ParseGypList(options.libraries) | 103 libraries = build_utils.ParseGypList(options.libraries) |
| 93 | 104 |
| 94 build_utils.MakeDirectory(options.packed_libraries_dir) | 105 build_utils.MakeDirectory(options.packed_libraries_dir) |
| 95 | 106 |
| 96 for library in libraries: | 107 for library in libraries: |
| 97 library_path = os.path.join(options.stripped_libraries_dir, library) | 108 library_path = os.path.join(options.stripped_libraries_dir, library) |
| 98 output_path = os.path.join(options.packed_libraries_dir, library) | 109 output_path = os.path.join(options.packed_libraries_dir, library) |
| 99 | 110 |
| 100 if enable_packing and library not in exclude_packing_set: | 111 if enable_packing and library not in exclude_packing_set: |
| 101 PackArmLibraryRelocations(options.android_pack_relocations, | 112 PackArmLibraryRelocations(options.android_pack_relocations, |
| 102 options.android_objcopy, | 113 options.android_objcopy, |
| 114 has_relocations_with_addends, |
| 103 library_path, | 115 library_path, |
| 104 output_path) | 116 output_path) |
| 105 else: | 117 else: |
| 106 CopyArmLibraryUnchanged(library_path, output_path) | 118 CopyArmLibraryUnchanged(library_path, output_path) |
| 107 | 119 |
| 108 if options.stamp: | 120 if options.stamp: |
| 109 build_utils.Touch(options.stamp) | 121 build_utils.Touch(options.stamp) |
| 110 | 122 |
| 111 return 0 | 123 return 0 |
| 112 | 124 |
| 113 | 125 |
| 114 if __name__ == '__main__': | 126 if __name__ == '__main__': |
| 115 sys.exit(main(sys.argv[1:])) | 127 sys.exit(main(sys.argv[1:])) |
| OLD | NEW |