From owner-freebsd-hackers@FreeBSD.ORG Fri May 3 22:26:38 2013 Return-Path: Delivered-To: hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by hub.freebsd.org (Postfix) with ESMTP id 641404B1 for ; Fri, 3 May 2013 22:26:38 +0000 (UTC) (envelope-from yuri@rawbw.com) Received: from shell0.rawbw.com (shell0.rawbw.com [198.144.192.45]) by mx1.freebsd.org (Postfix) with ESMTP id 3B2541865 for ; Fri, 3 May 2013 22:26:37 +0000 (UTC) Received: from eagle.yuri.org (stunnel@localhost [127.0.0.1]) (authenticated bits=0) by shell0.rawbw.com (8.14.4/8.14.4) with ESMTP id r43MQapB082409; Fri, 3 May 2013 15:26:36 -0700 (PDT) (envelope-from yuri@rawbw.com) Message-ID: <5184399C.9000805@rawbw.com> Date: Fri, 03 May 2013 15:26:36 -0700 From: Yuri User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:17.0) Gecko/20130327 Thunderbird/17.0.4 MIME-Version: 1.0 To: Konstantin Belousov Subject: Re: What is the correct way to declare assembler global variable ? References: <518419B5.4000602@rawbw.com> <20130503203020.GR3047@kib.kiev.ua> In-Reply-To: <20130503203020.GR3047@kib.kiev.ua> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: FreeBSD Hackers X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 03 May 2013 22:26:38 -0000 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(curbrk); void* newbrk = prevbrk + increment; if (brk(newbrk) == -1) { return reinterpret_cast(static_cast(-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; }