Skip to content

Commit

Permalink
Upgrade to upstream 7e4bd56 (fix & add more weak symbols)
Browse files Browse the repository at this point in the history
  • Loading branch information
kinke committed Nov 8, 2018
1 parent a048ca9 commit 00f4f2a
Show file tree
Hide file tree
Showing 8 changed files with 1,088 additions and 263 deletions.
54 changes: 19 additions & 35 deletions buildsdk.d
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import std.string;
version = verbose;

bool x64;
string[string] weakSymbols; // weak name => real name


void runShell(string cmd)
Expand Down Expand Up @@ -132,17 +131,15 @@ void sanitizeDef(string defFile)
return `LIBRARY "vcruntime140.dll"`;

// The MinGW-w64 .def files specify weak external symbols as 'alias == realName'.
// Just ignore them; they are incomplete and sometimes wrong.
const i = line.indexOf("==");
if (i > 0)
{
const weakName = strip(line[0 .. i]);
const realName = strip(line[i+2 .. $]);

if (weakName.indexOf(' ') < 0 && realName.indexOf(' ') < 0)
{
weakSymbols[weakName] = realName;
return ";" ~ line;
}
}

// Un-hide functions overridden by the MinGW runtime.
Expand Down Expand Up @@ -393,47 +390,34 @@ void buildMsvcrt(string outDir)

void buildOldnames(string outDir)
{
static string prependUnderscore(string symbolName)
{
return symbolName.startsWith("__imp_")
? symbolName[0 .. 6] ~ "_" ~ symbolName[6 .. $] // __imp_name => __imp__name
: "_" ~ symbolName; // name => _name
}
static struct WeakSymbol { string name; string targetName; }
WeakSymbol[] weakSymbols;

const lines = std.file.readText("oldnames.list").splitLines;
foreach (line; lines)
void processAliasesFile(string path)
{
if (line.length == 0)
continue;
foreach (line; std.file.readText(path).splitLines)
{
if (line.length == 0 || line[0] == ';')
continue;

string weakName;
string realName;
const equalsIndex = line.indexOf('=');
const weakName = line[0 .. equalsIndex];
const realName = line[equalsIndex+1 .. $];

const equalsIndex = line.indexOf('=');
if (equalsIndex > 0)
{
weakName = line[0 .. equalsIndex];
realName = line[equalsIndex+1 .. $];
}
else // most real names just feature an additional leading underscore
{
weakName = line;
realName = prependUnderscore(weakName);
weakSymbols ~= WeakSymbol(weakName, realName);
}

weakSymbols[weakName] = realName;
}

static string getMangledName(string symbolName)
{
return x64 ? symbolName : prependUnderscore(symbolName);
}
const suffix = x64 ? "64" : "32";
processAliasesFile("oldnames.aliases" ~ suffix);
// include the weak symbols from msvcrt.lib too
processAliasesFile("msvcrt140.aliases" ~ suffix);

const oldnames_c =
// access this __ref_oldnames symbol to drag in the generated linker directives (msvcrt_stub.c)
// access this __ref_oldnames symbol (in msvcrt_stub.c) to drag in the generated linker directives
"int __ref_oldnames;\n" ~
weakSymbols.byKeyValue.map!(pair =>
`__pragma(comment(linker, "/alternatename:` ~ getMangledName(pair.key) ~ `=` ~ getMangledName(pair.value) ~ `"));`
weakSymbols.map!(sym =>
`__pragma(comment(linker, "/alternatename:` ~ sym.name ~ `=` ~ sym.targetName ~ `"));`
).join("\n");

version (verbose)
Expand Down
53 changes: 53 additions & 0 deletions extractAliases.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import std.file;
import std.process;
import std.stdio;
import std.string;

string untilFirstSpace(string str)
{
const spaceIndex = str.indexOf(' ');
return spaceIndex < 0 ? str : str[0 .. spaceIndex];
}

int main(string[] args)
{
if (args.length != 2)
{
writefln("Usage: %s <path to .lib file>", args[0]);
return 1;
}

const command = `dumpbin /symbols "` ~ args[1] ~ `"`;
const result = executeShell(command);
if (result.status)
{
writefln("Error: '%s' failed with status %d", command, result.status);
return 1;
}

writeln("; aliases extracted from ", args[1]);

const lines = splitLines(result.output);
foreach (i; 1 .. lines.length-1)
{
const line = lines[i];
const previousLine = lines[i-1];
const nextLine = lines[i+1];

const weakExternalIndex = line.indexOf(" WeakExternal | ");
if (weakExternalIndex < 0)
continue;
if (nextLine.indexOf(" 2 Alias record") < 0)
continue;
const externalIndex = previousLine.indexOf(" External | ");
if (externalIndex < 0)
continue;

const weakName = untilFirstSpace(line[weakExternalIndex+16 .. $]);
const realName = untilFirstSpace(previousLine[externalIndex+16 .. $]);

writeln(weakName, "=", realName);
}

return 0;
}
Loading

0 comments on commit 00f4f2a

Please sign in to comment.