Skip to content
Advertisement

size and objdump report different sizes for the text segment

I have tried this, but stuck up to the answer below questions where hello_world-1.c is

#include<stdio.h>
int main(void)
{
    printf("Hello worldn");
    return 0;
}

The executed commands:

[kishore@localhost-localdomain ~]$ gcc -Wall -Wextra -c hello_world-1.c
[kishore@localhost-localdomain ~]$ gcc -o hello_world-1 hello_world-1.o
[kishore@localhost-localdomain ~]$ size hello_world-1 hello_world-1.o

    text    data    bss     dec    hex    filename
    1222     280      4    1506    5e2    hello_world-1
     139       0      0     139     8b    hello_world-1.o

[kishore@localhost-localdomain ~]$ objdump -h hello_world-1.o

    hello_world-1.o:     file format elf32-i386

    Sections:
    Idx Name          Size      VMA       LMA       File off  Algn
      0 .text         0000003b  00000000  00000000  00000034  2**0
                      CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
      1 .data         00000000  00000000  00000000  0000006f  2**0
                      CONTENTS, ALLOC, LOAD, DATA
      2 .bss          00000000  00000000  00000000  0000006f  2**0
                      ALLOC
      3 .rodata       0000000c  00000000  00000000  0000006f  2**0
                      CONTENTS, ALLOC, LOAD, READONLY, DATA
      4 .comment      0000002d  00000000  00000000  0000007b  2**0
                      CONTENTS, READONLY
      5 .note.GNU-stack 00000000  00000000  00000000  000000a8  2**0
                      CONTENTS, READONLY
      6 .eh_frame     00000044  00000000  00000000  000000a8  2**2
                      CONTENTS, ALLOC, LOAD, RELOC, READONLY, DATA

Can anyone please help me to figure out what might be the reasons for the questions below?

  1. The size command didn’t list a stack or heap segment for hello_world or hello_world.o. What might be the reason behind this? (for above question, the answer is as I didn’t called any function and also not used any heap segment, that’s why they didn’t showed up here. Am I correct?)
  2. There are no global variables in hello_world-1.c. Why size reports that the data and bss segments have zero length for the object file but non-zero length for the executable?
  3. size and objdump report different sizes for the text segment. Can you please advise where the discrepancy comes from?

I have tried but failed to conclude on above 3 questions. Appreciate your help on this.

Advertisement

Answer

1) Heap and stack are created by the OS at run time; that is, after the executable has been loaded into virtual memory. Therefore, they are not part of the executable file.

2) Because the executable also contains data — and code, mind you — from the stdio library that has been linked to the object file by the linker.

3) Because size, invoked like this (without any parameters) displays sizes according to the Berkeley convention. Under this scenario, the text entry reports the combined sizes of three different segments:

.text

.rodata

.eh_frame

On the other hand, objdump reports the size of .text only. You can see the sizes of .text, .rodata, and .eh_frame separately with size if you invoke it according to the SysV convention, like this: size -A hello_world-1.c. Then you will see exactly the same info you see with objdump.

Hope this helps.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement