Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

What's the purpose of a base address in an executable?

For example, in Microsoft Visual C++'s linker, you can set a base address, or use the default of 0x1000000. But with virtual memory, why would a base address be needed?

Why would you not just link it to 0x0 and put the initialization routine (mainCRTStartup on Windows) there which would make it so writing to NULL (0x0) would just overwrite the startup routine (a method that will never be called again). Plus, you could just set the code's pages to execute only on an x86 processor so writing will cause an exception.

share|improve this question
2  
For one, language implementations tend to use 0x0 to represent a NULL address because it makes comparisons to NULL easy. If the base address is 0x0, then this becomes a valid pointer and some other value has to be used as NULL. If you pick some other value to represent NULL, then you have to worry about addresses getting large enough to hit that value, which means that only 0x0 and 0xFFFFFFFF (for 32 bit) make sense to represent NULL. –  Steven Burnap Jan 25 '13 at 16:28
    
@StevenBurnap That's why OSX 64-bit uses 0x100000000 (32 bit max + 1) for base, but it doesn't answer my question really... –  Cole Johnson Jan 25 '13 at 17:43
1  
@DeadMG Then why didn't you just edit it to a neutral form like what @.whatsisname did? Using M$ isn't different from other forms of bad writing, leetspeak, etc. –  CodesInChaos Jan 25 '13 at 18:28
    
There's a big difference, because M$ exists only to express a childish prejudice against a specific company, rather than just silliness or abbreviation in general. –  DeadMG Jan 25 '13 at 19:02
    
Please try to keep votes relevant to the content of the question, and not the tools being used. Thank you. –  maple_shaft Jan 25 '13 at 19:04
show 1 more comment

2 Answers

up vote 3 down vote accepted

Traditionally executables get mapped at their desired base address, if that's not in use yet. You can't map at 0, since the lowest part of the address space is reserved (leads to crashes on null pointer dereferencing, instead of crashing).

For .exe files there is no need to use a unique base address. So most .exe files keep the default value 0x400000. They often lack a relocation table, and thus must be loaded at that address.

For dlls things are a bit more complicated, since their target address can be in use already. In that case they'll get mapped at another address, and a relocation table is used to patch parts of its code that now need to point to other addresses. This fixup costs post CPU time and memory(executables are mapped as copy-on-write, and the fixup means your application gets a unique copy of the dll). So base addresses for dlls are typically chosen to avoid overlaps and thus relocation.


Nowadays things are different. RAM is much cheaper, so the cost of relocation isn't that important anymore. On the other hand security is very important now. So we use a process called ASLR where executables are mapped at unguessable addresses. This makes the base address the executable specifies rather meaningless.

Thus executables being able to choose their own base address is a performance optimization that was useful on slow computers with little RAM but isn't useful on modern systems anymore since MS decided to trade a bit of performance for increased security.

share|improve this answer
add comment

Base addresses are needed to compute the virtual addresses of objects in the file after it has been loaded into virtual memory. Remember that the linker may have to link many PE files, and each one may request to be loaded at 0x0- this means that the PE file will have to cope with being located in different places in virtual memory.

The base address is used to make the other addresses relative- so if you have a vtable, you load the address 0x1000FF00, say. Then, when the dynamic linker rebases the executable because there was already something there, it knows how to modify this address to point to the new location.

share|improve this answer
    
But you could just make it relative to the virtual page's 0x0 –  Cole Johnson Jan 25 '13 at 18:26
    
But since every executable would not be able to achieve that address, you'd have to rebase every executable- expensive. –  DeadMG Jan 25 '13 at 19:01
    
why won't they be able to achieve the address? In referring to setting the linker to link to a base of 0x0 instead of the default. Why not use 0x0? –  Cole Johnson Jan 25 '13 at 22:54
    
Because every executable will attempt to map itself to the same address, and also, that would destroy every piece of code now compiled to use 0 as NULL. In fact, many operating systems now support ASLR, which effectively means the base address is random. –  DeadMG Jan 26 '13 at 13:15
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.