2021-10-11 16:52:33 +00:00
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
# Copyright 2021 Google LLC
|
|
|
|
|
#
|
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License.
|
|
|
|
|
#
|
|
|
|
|
################################################################################
|
|
|
|
|
"""Helper script for upgrading a profraw file to latest version."""
|
|
|
|
|
|
|
|
|
|
from collections import namedtuple
|
|
|
|
|
import struct
|
|
|
|
|
import subprocess
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
HeaderGeneric = namedtuple('HeaderGeneric', 'magic version')
|
2024-10-01 14:03:06 +00:00
|
|
|
|
HeaderVersion9 = namedtuple(
|
|
|
|
|
'HeaderVersion9',
|
2021-10-11 16:52:33 +00:00
|
|
|
|
'BinaryIdsSize DataSize PaddingBytesBeforeCounters CountersSize \
|
2024-10-01 14:03:06 +00:00
|
|
|
|
PaddingBytesAfterCounters NumBitmapBytes PaddingBytesAfterBitmapBytes NamesSize CountersDelta BitmapDelta NamesDelta ValueKindLast'
|
|
|
|
|
)
|
2021-10-11 16:52:33 +00:00
|
|
|
|
|
|
|
|
|
PROFRAW_MAGIC = 0xff6c70726f667281
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def relativize_address(data, offset, databegin, sect_prf_cnts, sect_prf_data):
|
|
|
|
|
"""Turns an absolute offset into a relative one."""
|
|
|
|
|
value = struct.unpack('Q', data[offset:offset + 8])[0]
|
|
|
|
|
if sect_prf_cnts <= value < sect_prf_data:
|
|
|
|
|
# If the value is an address in the right section, make it relative.
|
|
|
|
|
value = (value - databegin) & 0xffffffffffffffff
|
|
|
|
|
value = struct.pack('Q', value)
|
|
|
|
|
for i in range(8):
|
|
|
|
|
data[offset + i] = value[i]
|
2024-10-01 14:03:06 +00:00
|
|
|
|
# address was made relative
|
|
|
|
|
return True
|
|
|
|
|
# no changes done
|
|
|
|
|
return False
|
2021-10-11 16:52:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def upgrade(data, sect_prf_cnts, sect_prf_data):
|
|
|
|
|
"""Upgrades profraw data, knowing the sections addresses."""
|
|
|
|
|
generic_header = HeaderGeneric._make(struct.unpack('QQ', data[:16]))
|
|
|
|
|
if generic_header.magic != PROFRAW_MAGIC:
|
|
|
|
|
raise Exception('Bad magic.')
|
2024-10-01 14:03:06 +00:00
|
|
|
|
base_version = generic_header.version
|
|
|
|
|
|
|
|
|
|
if base_version >= 9:
|
|
|
|
|
# Nothing to do.
|
|
|
|
|
return data
|
|
|
|
|
if base_version < 5 or base_version == 6:
|
|
|
|
|
raise Exception('Unhandled version.')
|
|
|
|
|
|
2021-10-11 16:52:33 +00:00
|
|
|
|
if generic_header.version == 5:
|
|
|
|
|
generic_header = generic_header._replace(version=7)
|
|
|
|
|
# Upgrade from version 5 to 7 by adding binaryids field.
|
2021-10-13 16:46:50 +00:00
|
|
|
|
data = data[:8] + struct.pack('Q', generic_header.version) + struct.pack(
|
|
|
|
|
'Q', 0) + data[16:]
|
|
|
|
|
if generic_header.version == 7:
|
|
|
|
|
# cf https://reviews.llvm.org/D111123
|
|
|
|
|
generic_header = generic_header._replace(version=8)
|
|
|
|
|
data = data[:8] + struct.pack('Q', generic_header.version) + data[16:]
|
2024-10-01 14:03:06 +00:00
|
|
|
|
if generic_header.version == 8:
|
|
|
|
|
# see https://reviews.llvm.org/D138846
|
|
|
|
|
generic_header = generic_header._replace(version=9)
|
|
|
|
|
# Upgrade from version 8 to 9 by adding NumBitmapBytes, PaddingBytesAfterBitmapBytes and BitmapDelta fields.
|
|
|
|
|
data = data[:8] + struct.pack(
|
|
|
|
|
'Q', generic_header.version) + data[16:56] + struct.pack(
|
|
|
|
|
'QQ', 0, 0) + data[56:72] + struct.pack('Q', 0) + data[72:]
|
|
|
|
|
|
|
|
|
|
v9_header = HeaderVersion9._make(struct.unpack('QQQQQQQQQQQQ', data[16:112]))
|
2021-10-11 16:52:33 +00:00
|
|
|
|
|
2024-10-01 14:03:06 +00:00
|
|
|
|
if base_version <= 8 and v9_header.BinaryIdsSize % 8 != 0:
|
2021-10-11 16:52:33 +00:00
|
|
|
|
# Adds padding for binary ids.
|
|
|
|
|
# cf commit b9f547e8e51182d32f1912f97a3e53f4899ea6be
|
|
|
|
|
# cf https://reviews.llvm.org/D110365
|
2024-10-01 14:03:06 +00:00
|
|
|
|
padlen = 8 - (v9_header.BinaryIdsSize % 8)
|
|
|
|
|
v7_header = v9_header._replace(BinaryIdsSize=v9_header.BinaryIdsSize +
|
2021-10-11 16:52:33 +00:00
|
|
|
|
padlen)
|
2024-10-01 14:03:06 +00:00
|
|
|
|
data = data[:16] + struct.pack('Q', v9_header.BinaryIdsSize) + data[24:]
|
|
|
|
|
data = data[:112 + v9_header.BinaryIdsSize] + bytes(
|
|
|
|
|
padlen) + data[112 + v9_header.BinaryIdsSize:]
|
|
|
|
|
|
|
|
|
|
if base_version <= 8:
|
|
|
|
|
offset = 112 + v9_header.BinaryIdsSize
|
|
|
|
|
for d in range(v9_header.DataSize):
|
|
|
|
|
# Add BitmapPtr and aligned u32(NumBitmapBytes)
|
|
|
|
|
data = data[:offset + 3 * 8] + struct.pack(
|
|
|
|
|
'Q', 0) + data[offset + 3 * 8:offset + 6 * 8] + struct.pack(
|
|
|
|
|
'Q', 0) + data[offset + 6 * 8:]
|
|
|
|
|
value = struct.unpack('Q',
|
|
|
|
|
data[offset + 2 * 8:offset + 3 * 8])[0] - 16 * d
|
|
|
|
|
data = data[:offset + 2 * 8] + struct.pack('Q',
|
|
|
|
|
value) + data[offset + 3 * 8:]
|
|
|
|
|
offset += 8 * 8
|
|
|
|
|
|
|
|
|
|
if base_version >= 8:
|
|
|
|
|
# Nothing more to do.
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
# Last changes are relaed to bump from 7 to version 8 making CountersPtr relative.
|
2021-10-11 16:52:33 +00:00
|
|
|
|
dataref = sect_prf_data
|
2024-10-01 14:03:06 +00:00
|
|
|
|
# 80 is offset of CountersDelta.
|
|
|
|
|
if not relativize_address(data, 80, dataref, sect_prf_cnts, sect_prf_data):
|
|
|
|
|
return data
|
2021-10-11 16:52:33 +00:00
|
|
|
|
|
2024-10-01 14:03:06 +00:00
|
|
|
|
offset = 112 + v9_header.BinaryIdsSize
|
2021-10-11 16:52:33 +00:00
|
|
|
|
# This also works for C+Rust binaries compiled with
|
|
|
|
|
# clang-14/rust-nightly-clang-13.
|
2024-10-01 14:03:06 +00:00
|
|
|
|
for _ in range(v9_header.DataSize):
|
2021-10-11 16:52:33 +00:00
|
|
|
|
# 16 is the offset of CounterPtr in ProfrawData structure.
|
|
|
|
|
relativize_address(data, offset + 16, dataref, sect_prf_cnts, sect_prf_data)
|
|
|
|
|
# We need this because of CountersDelta -= sizeof(*SrcData);
|
|
|
|
|
# seen in __llvm_profile_merge_from_buffer.
|
2024-10-01 14:03:06 +00:00
|
|
|
|
dataref += 44 + 2 * (v9_header.ValueKindLast + 1)
|
|
|
|
|
if was8:
|
|
|
|
|
#profraw9 added RelativeBitmapPtr and NumBitmapBytes (8+4 rounded up to 16)
|
|
|
|
|
dataref -= 16
|
2021-10-11 16:52:33 +00:00
|
|
|
|
# This is the size of one ProfrawData structure.
|
2024-10-01 14:03:06 +00:00
|
|
|
|
offset += 44 + 2 * (v9_header.ValueKindLast + 1)
|
2021-10-11 16:52:33 +00:00
|
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
"""Helper script for upgrading a profraw file to latest version."""
|
2024-10-01 14:03:06 +00:00
|
|
|
|
if len(sys.argv) < 3:
|
|
|
|
|
sys.stderr.write('Usage: %s <binary> options? <profraw>...\n' % sys.argv[0])
|
2021-10-11 16:52:33 +00:00
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
|
# First find llvm profile sections addresses in the elf, quick and dirty.
|
|
|
|
|
process = subprocess.Popen(['readelf', '-S', sys.argv[1]],
|
|
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
|
output, err = process.communicate()
|
|
|
|
|
if err:
|
|
|
|
|
print('readelf failed')
|
|
|
|
|
return 2
|
|
|
|
|
for line in iter(output.split(b'\n')):
|
|
|
|
|
if b'__llvm_prf_cnts' in line:
|
2021-10-14 19:03:16 +00:00
|
|
|
|
sect_prf_cnts = int(line.split()[3], 16)
|
2021-10-11 16:52:33 +00:00
|
|
|
|
elif b'__llvm_prf_data' in line:
|
2021-10-14 19:03:16 +00:00
|
|
|
|
sect_prf_data = int(line.split()[3], 16)
|
2021-10-11 16:52:33 +00:00
|
|
|
|
|
2024-10-01 14:03:06 +00:00
|
|
|
|
out_name = "default.profup"
|
|
|
|
|
in_place = False
|
|
|
|
|
start = 2
|
|
|
|
|
if sys.argv[2] == "-i":
|
|
|
|
|
in_place = True
|
|
|
|
|
start = start + 1
|
|
|
|
|
elif sys.argv[2] == "-o":
|
|
|
|
|
out_name = sys.argv[3]
|
|
|
|
|
start = 4
|
|
|
|
|
|
|
|
|
|
if len(sys.argv) < start:
|
|
|
|
|
sys.stderr.write('Usage: %s <binary> options <profraw>...\n' % sys.argv[0])
|
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
|
for i in range(start, len(sys.argv)):
|
|
|
|
|
# Then open and read the input profraw file.
|
|
|
|
|
with open(sys.argv[i], 'rb') as input_file:
|
|
|
|
|
profraw_base = bytearray(input_file.read())
|
|
|
|
|
# Do the upgrade, returning a bytes object.
|
|
|
|
|
profraw_latest = upgrade(profraw_base, sect_prf_cnts, sect_prf_data)
|
|
|
|
|
# Write the output to the file given to the command line.
|
|
|
|
|
if in_place:
|
|
|
|
|
out_name = sys.argv[i]
|
|
|
|
|
with open(out_name, 'wb') as output_file:
|
|
|
|
|
output_file.write(profraw_latest)
|
2021-10-11 16:52:33 +00:00
|
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
sys.exit(main())
|