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 """Build relocation packer unit test data.
9 Uses a built relocation packer to generate 'golden' reference test data
10 files for elf_file_unittests.cc.
20 def PackArmLibraryRelocations(android_pack_relocations
,
25 # Copy and add a 'NULL' .android.rel.dyn section for the packing tool.
26 with tempfile
.NamedTemporaryFile() as stream
:
29 objcopy_command
= [android_objcopy
,
30 '--add-section', '%s=%s' % (added_section
, stream
.name
),
31 input_path
, output_path
]
32 subprocess
.check_call(objcopy_command
)
35 pack_command
= [android_pack_relocations
, output_path
]
36 subprocess
.check_call(pack_command
)
39 def UnpackArmLibraryRelocations(android_pack_relocations
,
42 shutil
.copy(input_path
, output_path
)
44 # Unpack relocations. We leave the .android.rel.dyn or .android.rela.dyn
46 unpack_command
= [android_pack_relocations
, '-u', output_path
]
47 subprocess
.check_call(unpack_command
)
51 parser
= optparse
.OptionParser()
53 parser
.add_option('--android-pack-relocations',
54 help='Path to the ARM relocations packer binary')
55 parser
.add_option('--android-objcopy',
56 help='Path to the toolchain\'s objcopy binary')
57 parser
.add_option('--added-section',
58 choices
=['.android.rel.dyn', '.android.rela.dyn'],
59 help='Section to add, one of ".android.rel.dyn" or ".android.rela.dyn"')
60 parser
.add_option('--test-file',
61 help='Path to the input test file, an unpacked ARM .so')
62 parser
.add_option('--unpacked-output',
63 help='Path to the output file for reference unpacked data')
64 parser
.add_option('--packed-output',
65 help='Path to the output file for reference packed data')
67 options
, _
= parser
.parse_args()
69 for output
in [options
.unpacked_output
, options
.packed_output
]:
70 directory
= os
.path
.dirname(output
)
71 if not os
.path
.exists(directory
):
72 os
.makedirs(directory
)
74 PackArmLibraryRelocations(options
.android_pack_relocations
,
75 options
.android_objcopy
,
76 options
.added_section
,
78 options
.packed_output
)
80 UnpackArmLibraryRelocations(options
.android_pack_relocations
,
81 options
.packed_output
,
82 options
.unpacked_output
)
87 if __name__
== '__main__':