What is the correct way to declare assembler global variable ?
Yuri
yuri at rawbw.com
Fri May 3 22:26:38 UTC 2013
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;
}
More information about the freebsd-hackers
mailing list