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
5 # found in the LICENSE file.
7 """Pack ARM relative relocations in a library (or copy unchanged).
9 If --enable-packing and --configuration-name=='Release', invoke the
10 relocation_packer tool to pack the .rel.dyn or .rela.dyn section in the given
11 library files. This step is inserted after the libraries are stripped.
12 Packing adds a new .android.rel.dyn or .android.rela.dyn section to the file
13 and reduces the size of .rel.dyn or .rela.dyn accordingly.
15 Currently packing only understands ARM32 shared libraries. For all other
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.
19 Any library listed in --exclude-packing-list is also copied verbatim,
20 irrespective of any --enable-packing setting. Typically this would be
21 'libchromium_android_linker.so'.
31 from util
import build_utils
33 def PackArmLibraryRelocations(android_pack_relocations
,
35 has_relocations_with_addends
,
38 # Select an appropriate name for the section we add.
39 if has_relocations_with_addends
:
40 new_section
= '.android.rela.dyn'
42 new_section
= '.android.rel.dyn'
44 # Copy and add a 'NULL' packed relocations section for the packing tool.
45 with tempfile
.NamedTemporaryFile() as stream
:
48 objcopy_command
= [android_objcopy
,
49 '--add-section', '%s=%s' % (new_section
, stream
.name
),
50 library_path
, output_path
]
51 build_utils
.CheckOutput(objcopy_command
)
53 # Pack R_ARM_RELATIVE relocations.
54 pack_command
= [android_pack_relocations
, output_path
]
55 build_utils
.CheckOutput(pack_command
)
58 def CopyArmLibraryUnchanged(library_path
, output_path
):
59 shutil
.copy(library_path
, output_path
)
63 args
= build_utils
.ExpandFileArgs(args
)
64 parser
= optparse
.OptionParser()
65 build_utils
.AddDepfileOption(parser
)
66 parser
.add_option('--clear-dir', action
='store_true',
67 help='If set, the destination directory will be deleted '
68 'before copying files to it. This is highly recommended to '
69 'ensure that no stale files are left in the directory.')
71 parser
.add_option('--configuration-name',
73 help='Gyp configuration name (i.e. Debug, Release)')
74 parser
.add_option('--enable-packing',
76 help=('Pack relocations if 1 and configuration name is \'Release\','
77 ' otherwise plain file copy'))
78 parser
.add_option('--has-relocations-with-addends',
80 help=('Pack into \'.android.rela.dyn\' if 1, else \'.android.rel.dyn\''))
81 parser
.add_option('--exclude-packing-list',
83 help='Names of any libraries explicitly not packed')
84 parser
.add_option('--android-pack-relocations',
85 help='Path to the ARM relocations packer binary')
86 parser
.add_option('--android-objcopy',
87 help='Path to the toolchain\'s objcopy binary')
88 parser
.add_option('--stripped-libraries-dir',
89 help='Directory for stripped libraries')
90 parser
.add_option('--packed-libraries-dir',
91 help='Directory for packed libraries')
92 parser
.add_option('--libraries', action
='append',
93 help='List of libraries')
94 parser
.add_option('--stamp', help='Path to touch on success')
96 options
, _
= parser
.parse_args(args
)
97 enable_packing
= (options
.enable_packing
== '1' and
98 options
.configuration_name
== 'Release')
99 has_relocations_with_addends
= (options
.has_relocations_with_addends
== '1')
100 exclude_packing_set
= set(shlex
.split(options
.exclude_packing_list
))
103 for libs_arg
in options
.libraries
:
104 libraries
+= build_utils
.ParseGypList(libs_arg
)
106 if options
.clear_dir
:
107 build_utils
.DeleteDirectory(options
.packed_libraries_dir
)
109 build_utils
.MakeDirectory(options
.packed_libraries_dir
)
111 for library
in libraries
:
112 library_path
= os
.path
.join(options
.stripped_libraries_dir
, library
)
113 output_path
= os
.path
.join(
114 options
.packed_libraries_dir
, os
.path
.basename(library
))
116 if enable_packing
and library
not in exclude_packing_set
:
117 PackArmLibraryRelocations(options
.android_pack_relocations
,
118 options
.android_objcopy
,
119 has_relocations_with_addends
,
123 CopyArmLibraryUnchanged(library_path
, output_path
)
126 build_utils
.WriteDepfile(
128 libraries
+ build_utils
.GetPythonDependencies())
131 build_utils
.Touch(options
.stamp
)
136 if __name__
== '__main__':
137 sys
.exit(main(sys
.argv
[1:]))