Loadable libs Position Indepent code vs Unfixed base address
Loadable libs Position Indepent code vs Unfixed base address
What are pros and cons of them?
Some other shared libraries approaches?
Some other shared libraries approaches?
- thepowersgang
- Member
- Posts: 734
- Joined: Tue Dec 25, 2007 6:03 am
- Libera.chat IRC: thePowersGang
- Location: Perth, Western Australia
- Contact:
Re: Loadable libs Position Indepent code vs Unfixed base add
I presume you're talking about the two philosophies used by ELF and PE. In short, Position independent code can be slightly slower to execute than statically linked code, but is faster to load and can always be shared between processes. On the flip side, runtime relocations (PE-style) mean that if the address chosen at compile time is unavailable, very expensive relocations need to be done (taking up time and memory), these also require far more relocation entries than PIC.
Nowdays, PIC is probably the best, as modern CPUs have pretty good PC-relative addressing and the like, but back in protected mode, static linking was quite a bit faster.
Nowdays, PIC is probably the best, as modern CPUs have pretty good PC-relative addressing and the like, but back in protected mode, static linking was quite a bit faster.
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
- Love4Boobies
- Member
- Posts: 2111
- Joined: Fri Mar 07, 2008 5:36 pm
- Location: Bucharest, Romania
Re: Loadable libs Position Indepent code vs Unfixed base add
While x86-64, ARM, etc. have improved the PIC situation with the addition of RIP-relative addressing, PC-relative instructions, etc., respectively, the most efficient solution in terms of performance is to forget about shared objects and only use position-dependent code. The trade-off is pretty good considering the fact that data typically takes up a lot more memory.
However, if you do want shared objects, PIC is the recommended way to go as otherwise the text segment would require runtime modifications (leading to memory and performance overheads, since we're talking about a read-only segment) that would ironically result in it becoming non-sharable. On the other hand, PIC uses indirections via tables like the GOT and the PLT, which reside in the data segment.
However, if you do want shared objects, PIC is the recommended way to go as otherwise the text segment would require runtime modifications (leading to memory and performance overheads, since we're talking about a read-only segment) that would ironically result in it becoming non-sharable. On the other hand, PIC uses indirections via tables like the GOT and the PLT, which reside in the data segment.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
[ Project UDI ]
Re: Loadable libs Position Indepent code vs Unfixed base add
Patching relocations in absence of single-address-space memory has a drawback that no library code could be shared between two processes; unless they are loaded at fixed virtual addresses.
Learn to read.
Re: Loadable libs Position Indepent code vs Unfixed base add
I'm going to also bring up that position independent code might be a little more practical to use in lower level environments (ie. kernel modules, etc.).
At least, that's the case in my experience.
For example, it's quite easy to relocate ELF files in memory to whatever address modules are loaded to.
(Potentially that being wherever it is that the boot loader put them, although that might cause issues later.)
They don't need much more than a simple move in memory, symbol address adjustment, bit of linking and perhaps a couple of page mappings.
At least, that's the case in my experience.
For example, it's quite easy to relocate ELF files in memory to whatever address modules are loaded to.
(Potentially that being wherever it is that the boot loader put them, although that might cause issues later.)
They don't need much more than a simple move in memory, symbol address adjustment, bit of linking and perhaps a couple of page mappings.
Re: Loadable libs Position Indepent code vs Unfixed base add
Thanks all for info. Are there some Cons of PE approach?
- Owen
- Member
- Posts: 1700
- Joined: Fri Jun 13, 2008 3:21 pm
- Location: Cambridge, United Kingdom
- Contact:
Re: Loadable libs Position Indepent code vs Unfixed base add
Theres a good security argument (defense in depth) that code at fixed addresses should be deprecated, as it makes stack smashing attacks (and similar) significantly easier and more effective (particularly in combination with e.g. return-to-libc attacks). Not only does it mean that all functions are at fixed addresses, it means that the locations of the R/W data pages are fixed also (which may make attacks easier or harder, depending upon how difficult it is for the attacker to invoke e.g. mprotect)Love4Boobies wrote:While x86-64, ARM, etc. have improved the PIC situation with the addition of RIP-relative addressing, PC-relative instructions, etc., respectively, the most efficient solution in terms of performance is to forget about shared objects and only use position-dependent code. The trade-off is pretty good considering the fact that data typically takes up a lot more memory.
(Another good argument for shared libraries is, of course, in ensuring that all library code is kept up to date)
Re: Loadable libs Position Indepent code vs Unfixed base add
I said it bad I wanted good things about windows approach?
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
Re: Loadable libs Position Indepent code vs Unfixed base add
In that formulation, a religious "no", perhaps?lopidas wrote:good things about windows approach?
Relocatable or fixed position code is significantly faster (~10%) on 32-bit x86 at runtime. On 64-bit, that difference is neglegible. Security can still be fixed using ASLR, but memory overhead and load times are potentially an order of magnitude worse.
- Love4Boobies
- Member
- Posts: 2111
- Joined: Fri Mar 07, 2008 5:36 pm
- Location: Bucharest, Romania
Re: Loadable libs Position Indepent code vs Unfixed base add
Personally, I feel the fault lies with programming languages or implementations thereof. The OS shouldn't try to protect vulnerable code from itself. With safe languages and/or language implementations, the code would avoid vulnerabilities like buffer overflows altogether instead of obfuscating them.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
[ Project UDI ]
- Owen
- Member
- Posts: 1700
- Joined: Fri Jun 13, 2008 3:21 pm
- Location: Cambridge, United Kingdom
- Contact:
Re: Loadable libs Position Indepent code vs Unfixed base add
There is a lot to be said for defense in depth. ASLR is a mitigation factor for buffer overflows. Obviously this doesn't obviate the issue of the buffer overflow, just reduces the exploitability of it.Love4Boobies wrote:Personally, I feel the fault lies with programming languages or implementations thereof. The OS shouldn't try to protect vulnerable code from itself. With safe languages and/or language implementations, the code would avoid vulnerabilities like buffer overflows altogether instead of obfuscating them.
Every line of defense you use is one additional hurdle your attacker has to jump over... and maybe, just maybe, the difference between a DOS and a privilege escalation. Or, if nothing else, the difference between a zero-day exploit and getting the patch out before the exploit is in the wild.
- Love4Boobies
- Member
- Posts: 2111
- Joined: Fri Mar 07, 2008 5:36 pm
- Location: Bucharest, Romania
Re: Loadable libs Position Indepent code vs Unfixed base add
I thought the advantages and disadvantages of both formats were summarized fairly well in this thread. Here is the same summary in simpler terms:lopidas wrote:I said it bad I wanted good things about windows approach?
In a nutshell, PE DLL's may have larger loading overhead and sharing them is not guaranteed; all due to rebasing. However, you may precalculate the base so as to drastically decrease the probability of rebasing being required in the first place. There is a catch, though: For this to work, you must give up ASLR, whose point is to reduce the likelihood of certain vulnerabilities (i.e., some buffer overflows) being exploited.
On the other hand, PIC with ELF shared objects is somewhat slower to run, albeit libraries are sometimes noticeably faster to load. Furthermore, sharing between different processes is always possible, even with ASLR.
Whether or not you design your loader to do ASLR is entirely up to you; as you can see, different people have different opinions on this.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
[ Project UDI ]
Re: Loadable libs Position Indepent code vs Unfixed base add
Thanks, nice for nice summary.