How can I modify a PE import table so that instead of linking to foo.dll it links to bar.dll?
<p>I have a file <code>plugin.dll</code> whose import table links to several symbols in <code>foo.dll</code>. I want to make a new file <code>plugin-modified.dll</code>, such that <code>plugin-modified.dll</code> is identical to <code>plugin.dll</code>, except that instead of linking to these symbols in <code>foo.dll</code>, its import table imports those same symbols from <code>bar.dll</code>. Can anyone suggest a tool that will rewrite PE files in this way? Ideally command-line/scriptable and open-source, but I'm willing to consider alternatives...</p>

<p>(The real use case is that I want to replace the reference to <code>foo.dll</code> with <code>foo-unique-id-934019384.dll</code>, so as to be able to ship <code>plugin.dll</code> and <code>foo.dll</code> together while avoiding name collisions, just in case someone else also distributes some other plugin that links to its own, different version of <code>foo.dll</code>.</p>

<p>On Linux this can be done with the <code>patchelf</code> tool, and on OS X it can be done with <code>install_name_tool</code>; I'm looking for a Windows equivalent.</p>

<p>I'm aware that SxS assemblies may be able to solve this problem in a different way, but they have their own limitations so I want to explore both approaches before deciding.)</p>