Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 03 May 2013 15:26:36 -0700
From:      Yuri <yuri@rawbw.com>
To:        Konstantin Belousov <kostikbel@gmail.com>
Cc:        FreeBSD Hackers <hackers@freebsd.org>
Subject:   Re: What is the correct way to declare assembler global variable ?
Message-ID:  <5184399C.9000805@rawbw.com>
In-Reply-To: <20130503203020.GR3047@kib.kiev.ua>
References:  <518419B5.4000602@rawbw.com> <20130503203020.GR3047@kib.kiev.ua>

next in thread | previous in thread | raw e-mail | index | archive | help
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@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;
}




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?5184399C.9000805>