Ok, I must admit I'm a Johnny-come-lately to the whole rebasing thing, it's been covered over and over, with the need for it being stressed by all. And yet, here I am after 10+ years of maintaining the Win32 dlls we use without having done it. I'm back to beating my head on my DLL hell issue (more updates on that later), and one of the things I finally decided to take care of was getting the DLL's rebased.
It's one of those topics that seems like it'd be difficult. I did a bunch of research trying to figure out the best way to decide on starting addresses (seemed hard to find), and then determine how much spare memory I should reserve for future growth, calculating starting addresses backwards, fighting with the Delphi 2007 compile options because the base address didn't seem to be working, and then I read how simple this really is. No need for all the manual gyrations & calculations I was trying to do - I was making it far tougher than it needs to be.
Because this topic has been covered so many times by so many that are better writers than I am, I'm not going to bother with the whys of it. But here's the short version of it how to do it. Get your hands on a copy of Rebase.exe from Microsoft, it comes with VS so you probably already have it. List your dll's, give an output file for the logs and a starting address, and you're done.
>rebase.exe -b 0x68000000 -d -C coffbase.log -l rebase.log -v mydll1.dll mydll2.dll mydll3.dll
Where:
-b = Initial base address (MS recommends starting at 0x68000000)
-d = Top down rebase (MS recommended practice)
-C = Output coff_base.txt file
-l = write image bases to log file
-v = verbose output
It's that simple, there was no calculations needed, Rebase.exe handles them all. I created a simple .bat file to run the rebase command. I just execute the .bat after I recompile the exe's and bingo, all done. Yes, it really is that simple. Gonna add this step to our FinalBuilder build process now, it's so trivial I'm embarrased to admit I put off doing this for years.