From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 22420 invoked by alias); 9 Dec 2007 06:51:21 -0000 Received: (qmail 22412 invoked by uid 22791); 9 Dec 2007 06:51:20 -0000 X-Spam-Check-By: sourceware.org Received: from topnetmail3.outgw.tn (HELO topnetmail3.outgw.tn) (193.95.28.103) by sourceware.org (qpsmtpd/0.31) with ESMTP; Sun, 09 Dec 2007 06:51:13 +0000 Received: from server-d.ght (tounes114.tn [193.95.28.114]) by topnetmail3.outgw.tn (Postfix) with ESMTP id 83DE599A9 for ; Sun, 9 Dec 2007 07:51:10 +0100 (CET) Received: from tounes-27.ati.tn (unverified) by server-d.ght (Content Technologies SMTPRS 4.3.14) with ESMTP id for ; Sun, 9 Dec 2007 07:56:32 +0100 Received: from mail1.topnet.tn (smtp.topnet.tn [213.150.176.204]) by tounes-27.ati.tn (Postfix) with ESMTP id C2AC3520046 for ; Sun, 9 Dec 2007 07:51:09 +0100 (CET) Received: (qmail 1526 invoked by uid 89); 9 Dec 2007 06:44:29 -0000 Received: by simscan 1.1.0 ppid: 1523, pid: 1524, t: 0.2854s scanners: clamav: 0.91.2/m: Received: from unknown (HELO ?192.168.1.3?) (196.203.109.145) by mail1 with ESMTP; 9 Dec 2007 06:44:29 -0000 Message-ID: <475B9003.1050409@gmail.com> Date: Sun, 09 Dec 2007 06:51:00 -0000 From: Ilyes Gouta User-Agent: Thunderbird 1.5.0.13 (Windows/20070809) MIME-Version: 1.0 To: gcc-help@gcc.gnu.org Subject: Re: __declspec(naked) and function body size References: <475AB169.1000905@gmail.com> <475AF47C.F78F600@dessent.net> <475B1196.9000306@gmail.com> <475B282B.34CB1AD1@dessent.net> In-Reply-To: <475B282B.34CB1AD1@dessent.net> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-IsSubscribed: yes Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org X-SW-Source: 2007-12/txt/msg00188.txt.bz2 Hi, Thank you so much, Brian, for your help. I'm going to try this out and see if it fits my requirements. BR, Ilyes Gouta. Brian Dessent wrote: > Ilyes Gouta wrote: > >> Is it possible to get the size of a function, i.e the number of bytes generated by the compiler between the prologue and >> the final routine's ret instruction, so that it would be available through a 'sizeof' like operator for the other code? > > You can do this at the assembler level: > > int foo() > { > int a = 0, b = 1; > int c = a + b; > > return (c); > } > > asm(".set foo_size, .-foo"); > extern unsigned foo_size; > > int main() > { > unsigned char buf[512]; > > memcpy (buf, foo, foo_size); > } > > There are two problems with this: > > 1. In unit-at-a-time mode (which is enabled by default when optimizing) > the compiler does not guarantee that functions and top-level asm > statements are emitted in the same order as specified in the input > source. Since this technique requires that the asm() come immediately > after the function it refers to, this will break. So you have to use > -fno-unit-at-a-time (for gcc < 4.2.0) or preferably > -fno-toplevel-reorder (for gcc >= 4.2.0) unless you use -O0. > > 2. The asm has to be adjusted if you want it to work on > leading-underscore targets. For example on win32 you'd need to use > something like asm(".set _foo_size, .-_foo") instead. I suppose you > could abstract this away with some macro junk: > > #define ASMNAME(cname) ASMNAME2 (__USER_LABEL_PREFIX__, cname) > #define ASMNAME2(prefix, cname) STRING (prefix) cname > #define STRING(x) #x > > // ... > > asm(".set " ASMNAME("foo_size") ", .-" ASMNAME("foo")); > extern unsigned foo_size; > > Brian >