File size: 6,884 Bytes
cf36d98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/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')
HeaderVersion9 = namedtuple(
    'HeaderVersion9',
    'BinaryIdsSize DataSize PaddingBytesBeforeCounters CountersSize \
    PaddingBytesAfterCounters NumBitmapBytes PaddingBytesAfterBitmapBytes NamesSize CountersDelta BitmapDelta NamesDelta ValueKindLast'
)

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]
    # address was made relative
    return True
  # no changes done
  return False


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.')
  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.')

  if generic_header.version == 5:
    generic_header = generic_header._replace(version=7)
    # Upgrade from version 5 to 7 by adding binaryids field.
    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:]
  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]))

  if base_version <= 8 and v9_header.BinaryIdsSize % 8 != 0:
    # Adds padding for binary ids.
    # cf commit b9f547e8e51182d32f1912f97a3e53f4899ea6be
    # cf https://reviews.llvm.org/D110365
    padlen = 8 - (v9_header.BinaryIdsSize % 8)
    v7_header = v9_header._replace(BinaryIdsSize=v9_header.BinaryIdsSize +
                                   padlen)
    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.
  dataref = sect_prf_data
  # 80 is offset of CountersDelta.
  if not relativize_address(data, 80, dataref, sect_prf_cnts, sect_prf_data):
    return data

  offset = 112 + v9_header.BinaryIdsSize
  # This also works for C+Rust binaries compiled with
  # clang-14/rust-nightly-clang-13.
  for _ in range(v9_header.DataSize):
    # 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.
    dataref += 44 + 2 * (v9_header.ValueKindLast + 1)
    if was8:
      #profraw9 added RelativeBitmapPtr and NumBitmapBytes (8+4 rounded up to 16)
      dataref -= 16
    # This is the size of one ProfrawData structure.
    offset += 44 + 2 * (v9_header.ValueKindLast + 1)

  return data


def main():
  """Helper script for upgrading a profraw file to latest version."""
  if len(sys.argv) < 3:
    sys.stderr.write('Usage: %s <binary> options? <profraw>...\n' % sys.argv[0])
    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:
      sect_prf_cnts = int(line.split()[3], 16)
    elif b'__llvm_prf_data' in line:
      sect_prf_data = int(line.split()[3], 16)

  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)

  return 0


if __name__ == '__main__':
  sys.exit(main())