"""
This script parses the /verbose output from the VC++ linker and uses it to
explain why a particular object file is being linked in. It parses records
like these:
Found "public: static void * __cdecl SkTLS::Get(void * (__cdecl*)(void)...
Referenced in chrome_crash_reporter_client_win.obj
Referenced in skia.lib(SkError.obj)
Loaded skia.lib(SkTLS.obj)
and then uses the information to answer questions such as "why is SkTLS.obj
being linked in. In this case it was requested by SkError.obj, and the process
is then repeated for SkError.obj. It traces the dependency tree back to a file
that was specified on the command line. Typically that file is part of a
source_set, and if that source_set is causing unnecessary code and data to be
pulled in then changing it to a static_library may reduce the binary size. See
crrev.com/2556603002 for an example of a ~900 KB savings from such a change.
In other cases the source_set to static_library fix does not work because some
of the symbols are required, while others are pulling in unwanted object files.
In these cases it can be necessary to see what symbol is causing one object file
to reference another. Removing or moving the problematic symbol can fix the
problem. See crrev.com/2559063002 for an example of such a change.
In some cases a target needs to be a source_set in component builds (so that all
of its functions will be exported) but should be a static_library in
non-component builds. The BUILD.gn pattern for that is:
if (is_component_build) {
link_target_type = "source_set"
} else {
link_target_type = "static_library"
}
target(link_target_type, "filters") {
One complication is that there are sometimes multiple source files with the
same name, such as mime_util.cc, all creating mime_util.obj. The script takes
whatever search criteria you pass and looks for all .obj files that were loaded
that contain that sub-string. It will print the search list that it will use
before reporting on why all of these .obj files were loaded. For instance, the
initial output if mime_util.obj is specified will be something like this:
>python linker_verbose_tracking.py verbose.txt mime_util.obj
Searching for [u'net.lib(mime_util.obj)', u'base.lib(mime_util.obj)']
If you want to restrict the search to just one of these .obj files then you can
give a fully specified name, like this:
>python linker_verbose_tracking.py verbose.txt base.lib(mime_util.obj)
Object file name matching is case sensitive.
Typical output when run on chrome_watcher.dll verbose link output is:
>python tools\win\linker_verbose_tracking.py verbose08.txt drop_data
Database loaded - 3844 xrefs found
Searching for common_sources.lib(drop_data.obj)
common_sources.lib(drop_data.obj).obj pulled in for symbol Metadata::Metadata...
common.lib(content_message_generator.obj)
common.lib(content_message_generator.obj).obj pulled in for symbol ...
Command-line obj file: url_loader.mojom.obj
"""
from __future__ import print_function
import io
import pdb
import re
import sys
def ParseVerbose(input_file):
obj_match = re.compile('.*(Referenced in |Loaded )(.*)')
found_prefix = ' Found'
cross_refs = {}
cross_refed_symbols = {}
references = None
file_encoding = 'utf-8'
with open(input_file) as file_handle:
header = file_handle.read(2)
if header == '\xff\xfe' or header == '\xfe\xff':
file_encoding = 'utf-16'
with io.open(input_file, encoding=file_encoding) as file_handle:
for line in file_handle:
if line.startswith(found_prefix):
references = []
symbol = line[len(found_prefix):].strip()
if symbol[0] == '"':
symbol = symbol[1:-1]
continue
if type(references) == type([]):
sub_line = line.strip()
match = obj_match.match(sub_line)
if match:
match_type, obj_name = match.groups()
if match_type == 'Referenced in ':
if '.lib' in obj_name:
reference = obj_name
else:
reference = ('Command-line obj file: ' + obj_name)
references.append(reference)
else:
assert (match_type == 'Loaded ')
if '.lib' in obj_name and '.obj' in obj_name:
cross_refs[obj_name] = references
cross_refed_symbols[obj_name] = symbol
references = None
if line.startswith('Finished pass 1'):
break
return cross_refs, cross_refed_symbols
def TrackObj(cross_refs, cross_refed_symbols, obj_name):
tracked = {}
targets = []
for key in cross_refs.keys():
if obj_name in key:
targets.append(key)
if len(targets) == 0:
targets.append(obj_name)
if len(targets) == 1:
print('Searching for %s' % targets[0])
else:
print('Searching for %s' % targets)
printed = False
for i in range(100):
new_targets = {}
for target in targets:
if not target in tracked:
tracked[target] = True
if target in cross_refs.keys():
symbol = cross_refed_symbols[target]
printed = True
print('%s.obj pulled in for symbol "%s" by' %
(target, symbol))
for ref in cross_refs[target]:
print('\t%s' % ref)
new_targets[ref] = True
if len(new_targets) == 0:
break
print()
targets = new_targets.keys()
if not printed:
print('No references to %s found. Directly specified in sources or a '
'source_set?' % obj_name)
def main():
if len(sys.argv) < 3:
print(r'Usage: %s <verbose_output_file> <objfile>' % sys.argv[0])
print(r'Sample: %s chrome_dll_verbose.txt SkTLS' % sys.argv[0])
return 0
cross_refs, cross_refed_symbols = ParseVerbose(sys.argv[1])
print('Database loaded - %d xrefs found' % len(cross_refs))
if not len(cross_refs):
print('No data found to analyze. Exiting')
return 0
TrackObj(cross_refs, cross_refed_symbols, sys.argv[2])
if __name__ == '__main__':
sys.exit(main())