This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: function addresses and ld.so
- From: "Zack Weinberg" <zack at codesourcery dot com>
- To: Andrew Pinski <pinskia at physics dot uc dot edu>
- Cc: Richard Henderson <rth at redhat dot com>, davidm at hpl dot hp dot com, Camm Maguire <camm at enhanced dot com>, gcl-devel at gnu dot org, debian-ia64 at lists dot debian dot org, gcc at gcc dot gnu dot org, 204789 at bugs dot debian dot org
- Date: Wed, 13 Aug 2003 17:32:53 -0700
- Subject: Re: function addresses and ld.so
- References: <41E13001-CDED-11D7-AEF7-000393A6D2F2@physics.uc.edu>
Andrew Pinski <pinskia@physics.uc.edu> writes:
> On Wednesday, Aug 13, 2003, at 20:11 US/Eastern, Zack Weinberg wrote:
>
>> Richard Henderson <rth@redhat.com> writes:
>>
>>> On Wed, Aug 13, 2003 at 04:35:36PM -0700, David Mosberger wrote:
>>>> Wouldn't, e.g., LD_PRELOADing something break this assumption?
>>>
>>> Yes. Or, indeed, just recompiling the library could result
>>> in different PLT offsets within the DSO, even on x86.
>>>
>>> This behaviour is completely broken. It'll never work reliably.
>>
>> A tactic that _will_ work is to mimic ld.so's PLT stubs. I'll use
>> sqrt() as an example:
>>
>> extern double sqrt(double);
>> static double stub_sqrt(double);
>>
>> // this is the value that gets written to the unexec file
>> double (*ptr_sqrt)(double) = stub_sqrt;
>>
>> double stub_sqrt(double x)
>> {
>> ptr_sqrt = sqrt;
>> return ptr_sqrt(x);
>> }
>>
>> Generate one of these stubs for every function you care about, and
>> bob's your uncle. Just make sure that none of the stubs get called
>> before the unexec file is written out.
>>
>> Unfortunately this won't work for variadic functions.
>>
>> There is no portable way that I know of to make the stub _be_ the PLT
>> stub, which is kind of a shame, as it would be (marginally) more
>> efficient and would work for variadics.
>
> Apple uses this exact technic in mach-o (and works with prebinding)
> but with an asm function to "bootstrap" the c function.
> The stub is only called once after setting the lazy-pointer which
> points to the real function.
Right, and ELF dynamic linkers do essentially the same thing under the
hood, too. That's what the "PLT stub" is.
Using assembly language lets you make this work for varargs functions,
which is handy.
zw