Advertisement
niklasfrykholm

Patch link.exe to ignore LNK4099

Dec 28th, 2011
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.20 KB | None | 0 0
  1. # This ruby program will patch the linker executable (link.exe)
  2. # so that linker warning LNK4099 is ignorable.
  3. #
  4. # Reference: http://www.bottledlight.com/docs/lnk4099.html
  5.  
  6. require "fileutils"
  7.  
  8. def link_exes()
  9.     res = []
  10.     res << File.join(ENV["VS90COMNTOOLS"], "../../VC/bin/link.exe") if ENV["VS90COMNTOOLS"]
  11.     res << File.join(ENV["VS100COMNTOOLS"], "../../VC/bin/link.exe") if ENV["VS100COMNTOOLS"]
  12.     res << File.join(ENV["XEDK"], "bin/win32/link.exe") if ENV["XEDK"]
  13.     return res
  14. end
  15.  
  16. def patch_link_exe(exe)
  17.     data = nil
  18.     File.open(exe, "rb") {|f| data = f.read}
  19.     pattern = [4099].pack("I")
  20.     offset = 0
  21.     i = 0
  22.     while true
  23.         i = data.index(pattern, offset)
  24.         return false unless i
  25.         offset = i+1
  26.         next unless i%4 == 0
  27.         int_before = data[i-4,4].unpack("I")[0]
  28.         int_after = data[i+4,4].unpack("I")[0]
  29.         break if int_before == 4088 && int_after == 4105
  30.     end
  31.     puts "* Found patch location #{exe}:#{i}"
  32.     bak = exe + "-" + Time.now.strftime("%y%m%d-%H%M%S") + ".bak"
  33.     puts "  Creating backup #{bak}"
  34.     FileUtils.cp(exe, bak)
  35.     puts "  Patching exe"
  36.     data[i,4] = [65535].pack("I")
  37.     File.open(exe, "wb") {|f| f.write(data)}
  38.     return true
  39. end
  40.  
  41. link_exes.each do |exe|
  42.     patch_link_exe(exe)
  43. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement