dshin
dshin

Reputation: 2398

Determining size of c++ method

I have a .cpp file with various methods defined:

// test.cpp

// foo() is not inlined
void foo() {
  ...
}

I compile it:

g++ test.cpp -o test.o -S

And now I want to determine, from examination of test.o, how many bytes of memory the instruction foo() takes up. How can I do this?

I ask because I have, through careful profiling and experimentation, determined that I am incurring instruction cache misses leading to significant slowdowns on certain critical paths. I'd therefore like to get a sense of how many bytes are occupied by various methods, to guide my efforts in shrinking my instruction set size.

Upvotes: 4

Views: 99

Answers (2)

Seva Alekseyev
Seva Alekseyev

Reputation: 61341

Build with map file generation, parse the map file. There might be some padding in the end, but it'll give you an idea.

In g++, the option goes:

-Xlinker -Map=MyProject.txt

Upvotes: 4

Carl Norum
Carl Norum

Reputation: 224864

I wouldn't recommend the -S flag for that, unless you're in love with your ISA's manual and hand-calculating instruction sizes. Instead, just build and disassemble, presto - out comes the answer:

$ cc -c example.c 
$ objdump -d example.o 

example.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <f>:
   0:   55                      push   %rbp
   1:   48 89 e5                mov    %rsp,%rbp
   4:   89 7d fc                mov    %edi,-0x4(%rbp)
   7:   8b 45 fc                mov    -0x4(%rbp),%eax
   a:   83 c0 03                add    $0x3,%eax
   d:   5d                      pop    %rbp
   e:   c3                      retq   

As you can see, this function is 15 bytes long.

Upvotes: 4

Related Questions