Commit 10dd25ba authored by Stephanie Kim's avatar Stephanie Kim Committed by Commit Bot

check_static_initializers should use hermetic xcode paths

Both `otool` and `atos` commands needed to be run with the hermetic
xcode paths. The code will first check that the hermetic xcode paths
exists before using that path.

Bug: 1113466
Change-Id: I11304f1981faf55946d3aa6fde44af631c87dc3a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2340795
Commit-Queue: Stephanie Kim <kimstephanie@google.com>
Reviewed-by: default avatarNico Weber <thakis@chromium.org>
Cr-Commit-Position: refs/heads/master@{#799292}
parent 70d00041
......@@ -68,7 +68,19 @@ def main_mac(src_dir):
# Count the number of files with at least one static initializer.
si_count = 0
# Find the __DATA,__mod_init_func section.
stdout = run_process(['otool', '-l', chromium_framework_executable])
# If the checkout uses the hermetic xcode binaries, then otool must be
# directly invoked. The indirection via /usr/bin/otool won't work unless
# there's an actual system install of Xcode.
hermetic_xcode_path = os.path.join(src_dir, 'build', 'mac_files',
'xcode_binaries')
if os.path.exists(hermetic_xcode_path):
otool_path = os.path.join(hermetic_xcode_path, 'Contents', 'Developer',
'Toolchains', 'XcodeDefault.xctoolchain', 'usr', 'bin', 'otool')
else:
otool_path = 'otool'
stdout = run_process([otool_path, '-l', chromium_framework_executable])
section_index = stdout.find('sectname __mod_init_func')
if section_index != -1:
# If the section exists, the "size" line must follow it.
......@@ -106,6 +118,10 @@ def main_mac(src_dir):
else:
print '# Warning: Falling back to potentially stripped output.'
args.append(chromium_framework_executable)
if os.path.exists(hermetic_xcode_path):
args.extend(['--xcode-path', hermetic_xcode_path])
stdout = run_process(args)
print stdout
return ret
......
......@@ -17,31 +17,37 @@ dump-static-initializers.py instead.
from __future__ import print_function
import optparse
import os
import subprocess
import sys
def ShowModuleInitializers(binary):
def ShowModuleInitializers(binary, xcode_path):
"""Gathers the module initializers for |binary| and symbolizes the addresses.
"""
initializers = GetModuleInitializers(binary)
initializers = GetModuleInitializers(binary, xcode_path)
if not initializers:
# atos will do work even if there are no addresses, so bail early.
return
symbols = SymbolizeAddresses(binary, initializers)
symbols = SymbolizeAddresses(binary, initializers, xcode_path)
print(binary)
for initializer in zip(initializers, symbols):
print('%s @ %s' % initializer)
def GetModuleInitializers(binary):
def GetModuleInitializers(binary, xcode_path):
"""Parses the __DATA,__mod_init_func segment of |binary| and returns a list
of string hexadecimal addresses of the module initializers.
"""
if xcode_path:
otool_path = os.path.join(xcode_path, 'Contents', 'Developer',
'Toolchains', 'XcodeDefault.xctoolchain', 'usr', 'bin', 'otool')
else:
otool_path = 'otool'
# The -v flag will display the addresses in a usable form (as opposed to
# just its on-disk little-endian byte representation).
otool = ['otool', '-v', '-s', '__DATA', '__mod_init_func', binary]
otool = [otool_path, '-v', '-s', '__DATA', '__mod_init_func', binary]
lines = subprocess.check_output(otool).strip().split('\n')
# Skip the first two header lines and then get the address of the
......@@ -53,22 +59,34 @@ def GetModuleInitializers(binary):
return [line.split(' ')[1] for line in lines[2:]]
def SymbolizeAddresses(binary, addresses):
def SymbolizeAddresses(binary, addresses, xcode_path):
"""Given a |binary| and a list of |addresses|, symbolizes them using atos.
"""
atos = ['xcrun', 'atos', '-o', binary] + addresses
if xcode_path:
atos_path = os.path.join(xcode_path, 'Contents', 'Developer', 'usr',
'bin', 'atos')
else:
atos_path = 'atos'
atos = [atos_path, '-o', binary] + addresses
lines = subprocess.check_output(atos).strip().split('\n')
return lines
def Main():
parser = optparse.OptionParser(usage='%prog filename')
parser.add_option(
'--xcode-path',
default=None,
help='Optional custom path to xcode binaries. By default, commands such '
'as `otool` will be run as `/usr/bin/otool` which only works '
'if there is a system-wide install of Xcode.')
opts, args = parser.parse_args()
if len(args) != 1:
parser.error('missing binary filename')
return 1
ShowModuleInitializers(args[0])
ShowModuleInitializers(args[0], opts.xcode_path)
return 0
if __name__ == '__main__':
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment