Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8203331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:30:54+00:00 2026-06-07T07:30:54+00:00

Suppose I have a function like below: # cat 003.c int foo(int a, int

  • 0

Suppose I have a function like below:

# cat 003.c

int foo(int a, int b)
{
    return a+b;
}

And compile it like this:

gcc -S 003.c

The gets the following assembly result:

     .file   "003.c"
     .text
 .globl foo
     .type   foo, @function
 foo:
 .LFB2:
     pushq   %rbp
 .LCFI0:
     movq    %rsp, %rbp
 .LCFI1:
     movl    %edi, -4(%rbp)
     movl    %esi, -8(%rbp)
     movl    -8(%rbp), %edx
     movl    -4(%rbp), %eax
     addl    %edx, %eax
     leave
     ret
 .LFE2:
     .size   foo, .-foo /* size of the function foo, how to get it?*/

The last line above do get the size of the function. Where does the compiler store the size? Can I get the function’s size in some way in my origin C program using C or inline asm?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T07:30:56+00:00Added an answer on June 7, 2026 at 7:30 am

    The information about a function size is stored in the ELF Attributes for the corresponding symbol (name). C example code how to parse this programmatically is at the bottom of the Solaris manpage forgelf_getsym(3ELF) (libelf does exist in Linux, *BSD and MacOS as well, you need to look for the st_size field of the GElf_Sym structure), but you also can use objdump / elfdump (Solaris) / readelf (Linux) for the task:

    $ objdump -h -d --section=.text foo3.o
    
    foo3.o:     file format elf64-x86-64
    
    Sections:
    Idx Name          Size      VMA               LMA               File off  Algn
      0 .text         00000012  0000000000000000  0000000000000000  00000040  2**2
                      CONTENTS, ALLOC, LOAD, READONLY, CODE
    [ ... ]
    Disassembly of section .text:
    
    0000000000000000 <foo>:
       0:   55                      push   %rbp
       1:   48 89 e5                mov    %rsp,%rbp
       4:   89 7d fc                mov    %edi,0xfffffffffffffffc(%rbp)
       7:   89 75 f8                mov    %esi,0xfffffffffffffff8(%rbp)
       a:   8b 45 f8                mov    0xfffffffffffffff8(%rbp),%eax
       d:   03 45 fc                add    0xfffffffffffffffc(%rbp),%eax
      10:   c9                      leaveq
      11:   c3                      retq

    This is for an unoptimized compile of your code, while the optimized version is:

    $ objdump -h -d --section=.text foo3.o
    
    foo3.o:     file format elf64-x86-64
    
    Sections:
    Idx Name          Size      VMA               LMA               File off  Algn
      0 .text         00000004  0000000000000000  0000000000000000  00000040  2**4
                      CONTENTS, ALLOC, LOAD, READONLY, CODE
    [ ... ]
    Disassembly of section .text:
    
    0000000000000000 <foo>:
       0:   8d 04 37                lea    (%rdi,%rsi,1),%eax
       3:   c3                      retq

    Note the “Size” change from 0x12 to 4 ? That’s what comes from the .size assembler directive.

    The “trick” of trying to use inline assembly to give you function sizes / code locations isn’t accounting for compiler-generated glue code (function entry prologues / exit epilogues, inline code generation, …), nor for the compiler re-ordering inline assembly (gcc is notorious to do so), hence it’s not generally a great idea to trust this. In the end, it depends on what exactly you’re trying to do …

    Edit: A few more references, external as well as on stackoverflow:

    1. From the gcc mailing list, thread on sizeof(function)
    2. what does sizeof (function name) return?
    3. Find size of a function in C
    4. LibELF by example sourceforge project (this is documentation / a tutorial)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a function like this (I use akka 2.0.2): def foo(message: String):
Suppose I have a function defined like this: class Foo() { public: void bar(MyClass*
Suppose I have a utility function like this - public static boolean isABlankSpace(char c)
Suppose I have a function which looks like this: template <class In, class In2>
Suppose I have a free function called InitFoo . I'd like to protect this
Suppose I have two functions which look like this: public static void myFunction1(int a,
Suppose I have a function void myFun(int*) In C++ what exactly does the following
suppose we have following function: void someFunction(int * araye){ for (int i=0;i<5;i++) cout <<araye[i]<<'
Suppose I have this function: void my_test() { A a1 = A_factory_func(); A a2(A_factory_func());
Suppose I have a function that creates data frames. I'd like to run that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.