Wednesday, December 28, 2011

Code Share: Patch link.exe to ignore LNK4099

By default, Visual Studio's link.exe does not let you ignore the linker warning LNK4099 (PDB file was not found).

This can be a real nuisance when you have to link with third party libraries that reference (but do not come with) PDBs. You can get hundreds of linker warnings that you have no way of getting rid of.

The only way I've found of fixing the problem is to patch link.exe so that it allows warning 4099 to be ignored. Luckily, it is not as scary as it sounds. You only need to patch a single location to remove 4099 from a list of warnings that cannot be ignored. An outline of the procedure can be found here.

Following my general philosophy to write-a-script-for-it I wrote a short ruby script that does the patching. I'm sharing it here for everybody that want to do voodoo on their link.exe and get rid of the warning.

(Click here for pastebin version.)

# This ruby program will patch the linker executable (link.exe)
# so that linker warning LNK4099 is ignorable.
#
# Reference: http://www.bottledlight.com/docs/lnk4099.html

require "fileutils"

def link_exes()
 res = []
 res << File.join(ENV["VS90COMNTOOLS"], "../../VC/bin/link.exe") if ENV["VS90COMNTOOLS"]
 res << File.join(ENV["VS100COMNTOOLS"], "../../VC/bin/link.exe") if ENV["VS100COMNTOOLS"]
 res << File.join(ENV["XEDK"], "bin/win32/link.exe") if ENV["XEDK"]
 return res
end

def patch_link_exe(exe)
 data = nil
 File.open(exe, "rb") {|f| data = f.read}
 unpatched = [4088, 4099, 4105].pack("III")
 patched = [4088, 65535, 4105].pack("III")

 if data.scan(patched).size > 0
  puts "* Already patched #{exe}"
  return
 end

 num_unpatched = data.scan(unpatched).size
 raise "Multiple patch locations in #{exe}" if num_unpatched > 1
 raise "Patch location not found in #{exe}" if num_unpatched == 0

 offset = data.index(unpatched)
 puts "* Found patch location #{exe}:#{offset}"
 bak = exe + "-" + Time.now.strftime("%y%m%d-%H%M%S") + ".bak"
 puts "  Creating backup #{bak}"
 FileUtils.cp(exe, bak)
 puts "  Patching exe"
 data[offset,unpatched.size] = patched
 File.open(exe, "wb") {|f| f.write(data)}
 return true
end

link_exes.each do |exe|
 patch_link_exe(exe)
end

10 comments:

  1. We at Epson Printer Support have a breathtaking notoriety of being the acceptable Epson fix focus inside the market for providing our customers with the fine in superbness contributions. Our especially gifted experts can deal with any printer related issue and investigate it with snappy arrangements. We have a toll loosened helpline go +1-877-760-6111 at which you can name to explain your printer stresses and profit smooth and brisk answers from our specialists.
    epson printer setup

    ReplyDelete
  2. Welcome to the world of my blog. Click here.
    enadco.tv/"
    kingsvillerecord.net"

    ReplyDelete
  3. We are comprised of efficient and skilled writers, all of whom are experienced and are certified writers. We have a diversity of writers; they have backgrounds in different subject areas, and they hold degrees from outstanding universities of the world. We make sure that each writer is efficient and well researched in the respective field. As a result, our writers are able to write on almost every topic of your choice, the content we provide is unique and original. We make sure that our customers get what they require, that is why our customers never regret their choice of selecting us. Our hard work has made us one of the best writing agencies in the country.
    Essay Help Uk

    ReplyDelete