summaryrefslogtreecommitdiffstats
path: root/solenv/bin/remangle32to64.pl
blob: 68ac7abb1a61d8f74effcf243e2e45fb073237c0 (plain)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/usr/bin/perl -w /* -*- indent-tabs-mode: nil -*- */

#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

# Change MSVC mangled C++ names from 32-bit form to the corresponding
# 64-bit form.  Each line of input can contain at most one mangled
# name.

# Based on experimentation with MSVC2008 and the following web pages:

# http://www.geoffchappell.com/viewer.htm?doc=studies/msvc/language/decoration/index.htm
# Thorough but incomplete. Still, describes details the below sources
# don't mention.

# http://cvs.winehq.com/cvsweb/wine/dlls/msvcrt/undname.c
# Wine's __unDname function, presumably the most complete, although
# not really written to act as "documentation"

# http://mearie.org/documents/mscmangle/
# Relatively complete but a bit badly structured and terse.

# http://en.wikipedia.org/wiki/Microsoft_Visual_C%2B%2B_Name_Mangling
# seems to be mostly a ripoff on the mearie.org page

# Example transformation:
# ??0ORealDynamicLoader@salhelper@@IAE@PAPAV01@ABVOUString@rtl@@1PAX2@Z =>
# ??0ORealDynamicLoader@salhelper@@IEAA@PEAPEAV01@AEBVOUString@rtl@@1PEAX2@Z

# It should be relatively easy to remove the modification parts of the
# below code and use the regex for some other task on MSVC mangled
# names.

# The regular expression below accepts also nonsensical mangled names,
# so it should not be used to verify correctness of mangled names.

use strict;

my @opstack = ();

sub parse_number($)
{
  my ($num) = @_;

  return $num + 1 if ($num eq '0' || ($num ge '1' && $num le '9'));

  $num =~ tr/ABCDEFGHIJKLMNOP@/0123456789ABCDEF /;
  hex($num);
}

sub format_number($)
{
  my ($num) = @_;

  return $num - 1 if ($num <= 10);

  $num = sprintf("%X", $num);
  $num =~ tr/0123456789ABCDEF/ABCDEFGHIJKLMNOP/;
  $num.'@';
}

sub double_thunk($$)
{
  my ($number, $position) = @_;

  my $bytes = parse_number($number);
  $bytes *= 2;
  push(@opstack, 'r '.($position - length($number)).' '.length($number).' '.format_number($bytes));
}

while (<>)
  {
    m/
      # Named subpattern definitions. I use names of the form
      # __CamelCase__ for the named subpatters so that they are easier
      # to see.
      (?(DEFINE)
        (?<__Number__>
          \?? ([0-9] | [A-P]+@)
        )
        (?<__32BitChecksum__>
          [A-P]{8}@
        )
        (?<__CallingConvention__>
          (?:
            [AB]
            |
            [C-L]
              (?{ push(@opstack, 'r '.(pos()-1).' 1 A cdecl'); })
          )
        )
        (?<__StringLiteralText__>
          (?:
            [_a-zA-Z0-9]
            |
            \?\$[A-P][A-P]
            |
            \?[0-9A-Za-z]
          ){1,20}
        )
        (?<__Identifier__>
          [_a-zA-Z\$][_a-zA-Z0-9\$]*@
        )
        (?<__ArgsZTerminated__>
          (?&__DataTypeInArgs__)+ @? Z
        )
        (?<__ArgsNonZTerminated__>
          (?&__DataTypeInArgs__)+ @?
        )
        (?<__TemplateName__>
          (?&__Identifier__) (?&__ArgsNonZTerminated__)
        )
        (?<__Class__>
          (?:
            [0-9]
            |
            \?\$ (?&__TemplateName__)
            |
            (?&__Identifier__)
          )+@
        )
        (?<__DataTypeCommon__>
          (?:
            # extended types like _int64, bool and wchar_t
            _[D-NW]
            |
            # simple types
            [C-KMNOXZ]
            |
            # class, struct, union, cointerface
            [TUVY] (?&__Class__)
            |
            # references
            [AB]
              (?{ push(@opstack, 'i '.pos().' E reference'); })
              (?&__ModifiedType__)
            |
            # pointers
            [QRS]
              (?{ push(@opstack, 'i '.pos().' E pointer'); })
              (?&__ModifiedType__)
            |
            P
              (?:
                # function pointer
                6 (?&__CallingConvention__) (?&__DataTypeNotInArgs__) (?&__ArgsZTerminated__)
                |
                # other pointer
                (?{ push(@opstack, 'i '.pos().' E pointer'); })
                (?&__ModifiedType__)
              )
            |
            W 4 (?&__Class__)
            |
            [0-9]
            |
            \$ (?:
                 [0DQ] (?&__Number__)
                 |
                 F (?&__Number__){2}
                 |
                 G (?&__Number__){3}
                 |
                 \$ [ABCD] (?&__DataTypeNotInArgs__)
               )
          )
        )
        (?<__ModifiedType__>
          [ABCD]
          (?:
            # multidimensional array
            Y (?&__Number__)+
          )?
          (?&__DataTypeNotInArgs__)
        )
        (?<__DataTypeInArgs__>
          (?:
            (?&__DataTypeCommon__)
            |
            # template parameter
            \? (?&__Number__)
          )
        )
        (?<__DataTypeNotInArgs__>
          (?:
            (?&__DataTypeCommon__)
            |
            \? (?&__ModifiedType__)
          )
        )
      )

      # All mangled names start with a question mark
      \?
      (?:
        # Ctors, dtors, operators etc have separate a priori defined
        # special mangled names like the very simple ?0 for constructor
        # and ?_R16789 for "RTTI Base Class Descriptor at (6,7,8,9)"
        # whatever that might mean.
        (
          \?
          ([0-9A-Z]
           |
           _(?:
              # C is for string literals, see below
              # R is RTTI, see immediately below
              [0-9ABD-QS-Z]
              |
              R0(?&__DataTypeNotInArgs__)
              |
              R1(?&__Number__){4}
              |
              R[234]
              |
              _(?:
                 E
               )
           )
          )
        )?
        (?&__Class__)

        (?:
          # Static members and normal variables
          [0-5]
            (?&__DataTypeNotInArgs__)
            [ABCD]
          |
          # Compiler-generated static
          [67]
            [ABCD]
            (?:
              @
              |
              (?&__Class__)
            )
          |
          # Non-static Methods, implicit 'this'
          [ABEFIJMNQRUV]
            [AB]
            (?{ push(@opstack, 'i '.(pos()-1).' E this'); })
            (?&__CallingConvention__)
            (?:
              @
              |
              (?&__DataTypeNotInArgs__)
            )
            (?&__ArgsZTerminated__)
          |
          # Static methods
          [CDKLST]
            (?&__CallingConvention__)
            (?:
              @
              |
              (?&__DataTypeNotInArgs__)
            )
            (?&__ArgsZTerminated__)
          |
          # Thunks
          [GHOPWX]
            ((?&__Number__))
            (?{ double_thunk($^N, pos()); })
            [AB]
            (?{ push(@opstack, 'i '.(pos()-1).' E this'); })
            (?&__CallingConvention__)
            (?:
              @
              |
              (?&__DataTypeNotInArgs__)
            )
            (?&__ArgsZTerminated__)
          |
          # Functions
          [YZ]
            (?&__CallingConvention__)
            (?:
              @
              |
              (?&__DataTypeNotInArgs__)
            )
            (?&__ArgsZTerminated__)
          |
          # Template
          \$ (?&__Identifier__) (?&__ArgsNonZTerminated__)
        )
        |
        # pooled string literals
        \?_C\@_[01](?&__Number__)(?&__32BitChecksum__)(?&__StringLiteralText__)@
      )
      /x;

    while (my $op = pop(@opstack))
      {
        # print STDERR "op=$op\n";
        my @a = split (' ', $op);
        if ($a[0] eq 'i') {
          substr($_,$a[1],0) = $a[2];
        } elsif ($a[0] eq 'r') {
          substr($_,$a[1],$a[2]) = $a[3];
        }
      }

    print;
  }