summaryrefslogtreecommitdiff
path: root/win/create_def_file.js
blob: 461f4766a1d113293dc3866b7ead5f4fb019b360 (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
// create_def_file.js
//
// Copyright (c) 2009 Sun Microsystems, Inc.
// Use is subject to license terms.
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

/*
  This script extracts names and types of globally defined symbols from
  COFF object files, and writes this information to stdout using .DEF 
  file format (module definition file used by Microsoft linker)
  
  In MySQL this script is used to export symbols from mysqld.exe for use by
  storage engine DLLs.
  
  Usage:
  cscript create_def_file.js [x86|x64] [object|static_lib|directory ...]
  
  If directory is passed as a parameter, script will process all object files
  and static libraries in this directory and recursively in all subdrectories.

  Note :The script does not work properly if  /GL (global optimization) 
  compiler option was used to produce object files or libraries. This is a
  limitation of the dumpbin tool that is used by the script.
*/

ForReading = 1;
ForWriting = 2;
ForAppending = 8;


var args = WScript.Arguments;
  
// check that we got proper arguments
if (args.length < 2)
{
    echo("Usage: create_def_file <X86|X64> [static_library|objectfile|objectdirectory ...] ");
    WScript.Quit(1);
}


var is64 = args.Item(0).toLowerCase() == "x64";
var shell = new ActiveXObject("WScript.Shell");
var fso = new ActiveXObject("Scripting.FileSystemObject");

OutputSymbols(CollectSymbols());


// takes the array that has been built up and writes out mysqld.def
function OutputSymbols(symbols)
{
    var out = WScript.StdOut;
    out.WriteLine("EXPORTS");
    for (var sym in symbols)
       out.WriteLine(sym);
}

function echo(message)
{
    WScript.StdErr.WriteLine(message);
}

// Extract global symbol names and type from objects
function CollectSymbols()
{
    var uniqueSymbols = new Array();

    try
    {
        /*
         Compiler tools use VS_UNICODE_OUTPUT env. variable as indicator 
         that they run within IDE, so they can communicate with IDE via 
         pipes instead of usual stdout/stderr. Refer to 
         http://blogs.msdn.com/freik/archive/2006/04/05/569025.aspx 
         for more info.
         Unset this environment variable.
        */
        shell.Environment("PROCESS").Remove("VS_UNICODE_OUTPUT");
    }
    catch(e){}
 
    var rspfilename = "dumpsymbols.rsp";
    CreateResponseFile(rspfilename);
    var commandline="dumpbin @"+rspfilename;
    
    echo("Executing "+commandline);
    var oExec = shell.Exec(commandline);

    while(!oExec.StdOut.AtEndOfStream)
    {
        var line = oExec.StdOut.ReadLine();
        if (line.indexOf("External") == -1) continue;
        var columns = line.split(" ");
        var index = 0;
        if (columns.length < 3)
          continue;

        /*
          If the third column of dumpbin output contains SECTx, 
          the symbol is defined in that section of the object file. 
          If UNDEF appears, it is not defined in that object and must
          be resolved elsewhere. BSS symbols (like uninitialized arrays)
          appear to have non-zero second column.
        */
        if (columns[2].substring(0,4)!="SECT")
        {
          if (columns[2] == "UNDEF"  && parseInt(columns[1])==0 )
            continue;
        }

        /*
          Extract undecorated symbol names 
          between "|" and next whitespace after it.
        */
        for (; index < columns.length; index++)
          if (columns[index] == "|")
              break;

        var symbol = columns[index + 1];
        var firstSpace = symbol.indexOf(" ");
        if (firstSpace != -1)
            symbol = symbol.substring(0, firstSpace-1);

        // Don't export compiler defined stuff
        if (IsCompilerDefinedSymbol(symbol))
            continue;
        
        // Correct symbol name for cdecl calling convention on x86
        symbol = ScrubSymbol(symbol);

        // Check if we have function or data
        if (line.indexOf("notype () ") == -1)
            symbol = symbol + " DATA";

        uniqueSymbols[symbol] = 1;
    }
    fso.DeleteFile(rspfilename);
    return uniqueSymbols;
}

// performs necessary cleanup on the symbol name
function ScrubSymbol(symbol)
{
    if (is64) return symbol;
    if (symbol.charAt(0) != "_") 
        return symbol;

    var atSign = symbol.indexOf("@");
    if (atSign != -1)
    {
        var paramSize = symbol.substring(atSign+1, symbol.Length);
        if (paramSize.match("[0-9]+$")) return symbol;
    }
    return symbol.substring(1, symbol.length);
}

// returns true if the symbol is compiler defined
function IsCompilerDefinedSymbol(symbol)
{
    return ((symbol.indexOf("__real@") != -1) ||
    (symbol.indexOf("_xmm@") != -1) ||
    (symbol.indexOf("_RTC_") != -1) || 
    (symbol.indexOf("??_C@_") != -1) ||
    (symbol.indexOf("??_R") != -1) ||
    (symbol.indexOf("??_7") != -1)  ||
    (symbol.indexOf("?_G") != -1) ||           // scalar deleting destructor
    (symbol.indexOf("_VInfreq_?") != -1) ||    // special label (exception handler?) for Intel compiler
    (symbol.indexOf("?_E") != -1));            // vector deleting destructor
}

// Creates response file for dumpbin
function CreateResponseFile(filename)
{
  var responseFile = fso.CreateTextFile(filename,true);
  responseFile.WriteLine("/SYMBOLS");
  
  var index = 1;
  for (; index < args.length; index++)
  {
    addToResponseFile(args.Item(index),responseFile);
  }
  responseFile.Close();
}

// Add object file/library to the dumpbin response file.
// If filename parameter is directory, all objects and libs under 
// this directory or subdirectories are added.
function addToResponseFile(filename, responseFile)
{
   if (fso.FolderExists(filename))
   {
        var folder = fso.getFolder(filename);
        var enumerator = new Enumerator(folder.files);
        for (; !enumerator.atEnd(); enumerator.moveNext())
        {
            addToResponseFile(enumerator.item().Path, responseFile);
        }
        enumerator = new Enumerator(folder.subFolders);
        for (; !enumerator.atEnd(); enumerator.moveNext())
        {
            addToResponseFile(enumerator.item().Path, responseFile);
        }
    }
    else if (fso.FileExists(filename))
    {
       var extension = filename.substr(filename.length -3).toLowerCase();
       if(extension == "lib" || extension == "obj")
       {
           responseFile.WriteLine("\""+fso.GetFile(filename).Path+"\"");
       }
    }
}