3

I have compiled the following C function (in sum.c) into object code:

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

using

gcc -c sum.c

When I check the sizes of different sections using size:

size sum.o
   text    data     bss     dec     hex filename
    112       0       0     112      70 sum.o

it tells me that the text section is allocated 112 bytes. But when I disassemble it using objdump:

objdump -d sum.o

sum.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <sum>:
   0:   f3 0f 1e fa             endbr64 
   4:   55                      push   %rbp
   5:   48 89 e5                mov    %rsp,%rbp
   8:   89 7d fc                mov    %edi,-0x4(%rbp)
   b:   89 75 f8                mov    %esi,-0x8(%rbp)
   e:   8b 55 fc                mov    -0x4(%rbp),%edx
  11:   8b 45 f8                mov    -0x8(%rbp),%eax
  14:   01 d0                   add    %edx,%eax
  16:   5d                      pop    %rbp
  17:   c3                      retq   

There are only 17 bytes worth of instructions in the .text section. Is there something that I am missing?

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
First User
  • 704
  • 5
  • 12
  • 1
    Note: That 17 is hex, so in decimal that is 23. But that is 0x17(23) is the start of the last instruction. There are actually 24 bytes worth of instructions. – Michael Petch Feb 11 '23 at 11:20

1 Answers1

2

Use size -A and objdump -D to get all the information you want.

On my system size without -A adds the size of the .eh_frame section onto text (probably because it is read-only and will be loaded together with the text).

Tom V
  • 4,827
  • 2
  • 5
  • 22
  • 2
    `size` without options seems to return all the ALLOC/LOAD/READONLY as `.text`. It also will include things like `.note.gnu.property` (if GCC is emitting it) and as you noticed `.eh_frame` sections. Same for `.rodata` if it existed. In the OP's case it appears that the 112 is the size of `.eh_frame` (56) + `.text` (24) + `.note.gnu.property` (32) = 112 . With `size` the sections have also been padded out to multiples of 8 (which happens to be the alignment for each of the sections) – Michael Petch Feb 11 '23 at 10:10