What is the correct way to declare assembler global variable ?
Konstantin Belousov
kostikbel at gmail.com
Sat May 4 20:08:38 UTC 2013
On Fri, May 03, 2013 at 03:26:36PM -0700, Yuri wrote:
> On 05/03/2013 13:30, Konstantin Belousov wrote:
> > Formal answer is for you to read about the .type directive in the GNU
> > as manual. Also, you need to read about either common symbols, or about
> > the .size directive.
> >
> > But, note that you cannot access hidden libc symbols from the code which
> > links to libc (dynamically). You probably need to re-consider higher-level
> > approach to your issue.
> >
>
> I didn't write this code. This is google-perftools-2.0 I am trying to port.
> They added the procedure do_sbrk there (see below), that attempts to
> emulate what libc is doing for sbrk. It fails, they probably didn't test it.
> The idea is that they need to have hooks before and after sbrk call.
> I am not sure what the best approach would be here?
> How can the memory allocation library override sbrk in libc and still be
> able to attach to the original libc version of sbrk?
>
> On Linux there is another symbol __sbrk, and they just use it to conenct
> to the original sbrk. But there is no such thing of FreeBSD.
>
> Yuri
>
>
> static inline void* do_sbrk(intptr_t increment) {
> void* curbrk = 0;
>
> #if defined(__x86_64__) || defined(__amd64__)
> # ifdef PIC
> __asm__ __volatile__(
> "movq .curbrk at GOTPCREL(%%rip), %%rdx;"
> "movq (%%rdx), %%rax;"
> "movq %%rax, %0;"
> : "=r" (curbrk)
> :: "%rdx", "%rax");
> # else
> __asm__ __volatile__(
> "movq .curbrk(%%rip), %%rax;"
> "movq %%rax, %0;"
> : "=r" (curbrk)
> :: "%rax");
> # endif
> #else
> __asm__ __volatile__(
> "movl .curbrk, %%eax;"
> "movl %%eax, %0;"
> : "=r" (curbrk)
> :: "%eax");
> #endif
>
> if (increment == 0) {
> return curbrk;
> }
>
> char* prevbrk = static_cast<char*>(curbrk);
> void* newbrk = prevbrk + increment;
>
> if (brk(newbrk) == -1) {
> return reinterpret_cast<void*>(static_cast<intptr_t>(-1));
> }
>
> return prevbrk;
> }
>
> extern "C" void* sbrk(intptr_t increment) __THROW {
> MallocHook::InvokePreSbrkHook(increment);
> void *result = do_sbrk(increment);
> MallocHook::InvokeSbrkHook(result, increment);
> return result;
> }
So all the code needs is to call hooks before and after the sbrk call,
right ?
For the !PIC case, what is cited above would probably work, but it requires
that libc is also linked static.
For the dynamic case, it should be enough to use the dlsym(RTLD_NEXT,
"sbrk") to get the pointer to interposed symbol in the wrapper. It
assumes that the library which interposes sbrk is loaded before libc,
but it is relatively non-trivial to fail this. Something along the
lines, obviously not even compiled:
#ifdef PIC
void *
sbrk(intptr_t incr)
{
static void *(*libc_sbrk)(intptr_t);
void *ret;
if (libc_sbrk == NULL)
libc_sbrk = dlsym(RTLD_NEXT, "sbrk");
sbrk_pre_hook(incr);
ret = libc_sbrk(incr);
sbrk_post_hook(ret, incr);
return (ret);
}
#endif
The sample is in fact thread-safe, or rather, not any more unsafe than
an sbrk() use itself.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 834 bytes
Desc: not available
URL: <http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20130504/dd0c7ad0/attachment.sig>
More information about the freebsd-hackers
mailing list