Skip to content

1 - Program

  1. Create file memory_layout.c

    memory_layout.c
    #include <stdio.h>
    int variable_data = 100;
    int variable_bss;
    int main(){
    int variable_local_x = 200;
    int variable_local_y;
    return 0;
    }
  2. Create object file by gcc memory_layout.c -c memory_layout.o

    Terminal window
    c@c:~/cos3105$ gcc memory_layout.c -c memory_layout.o
    gcc: warning: memory_layout.o: linker input file unused because linking not done
  3. List all of sections in Object file readelf -S memory_layout.o

    Terminal window
    c@c:~/cos3105$ readelf -S memory_layout.o
    There are 12 section headers, starting at offset 0x228:
    Section Headers:
    [Nr] Name Type Address Offset
    Size EntSize Flags Link Info Align
    [ 0] NULL 0000000000000000 00000000
    0000000000000000 0000000000000000 0 0 0
    [ 1] .text PROGBITS 0000000000000000 00000040
    0000000000000016 0000000000000000 AX 0 0 1
    [ 2] .data PROGBITS 0000000000000000 00000058
    0000000000000004 0000000000000000 WA 0 0 4
    [ 3] .bss NOBITS 0000000000000000 0000005c
    0000000000000004 0000000000000000 WA 0 0 4
    [ 4] .comment PROGBITS 0000000000000000 0000005c
    000000000000002c 0000000000000001 MS 0 0 1
    [ 5] .note.GNU-stack PROGBITS 0000000000000000 00000088
    0000000000000000 0000000000000000 0 0 1
    [ 6] .note.gnu.pr[...] NOTE 0000000000000000 00000088
    0000000000000020 0000000000000000 A 0 0 8
    [ 7] .eh_frame PROGBITS 0000000000000000 000000a8
    0000000000000038 0000000000000000 A 0 0 8
    [ 8] .rela.eh_frame RELA 0000000000000000 000001a8
    0000000000000018 0000000000000018 I 9 7 8
    [ 9] .symtab SYMTAB 0000000000000000 000000e0
    0000000000000090 0000000000000018 10 3 8
    [10] .strtab STRTAB 0000000000000000 00000170
    0000000000000031 0000000000000000 0 0 1
    [11] .shstrtab STRTAB 0000000000000000 000001c0
    0000000000000067 0000000000000000 0 0 1
    Key to Flags:
    W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
    L (link order), O (extra OS processing required), G (group), T (TLS),
    C (compressed), x (unknown), o (OS specific), E (exclude),
    D (mbind), l (large), p (processor specific)
  4. Specific to .text by readelf -x .text memory_layout.o

    Terminal window
    c@c:~/cos3105$ readelf -x .text memory_layout.o
    Hex dump of section '.text':
    NOTE: This section has relocations against it, but these have NOT been applied to this dump.
    0x00000000 f30f1efa 554889e5 4883ec10 c745fcc8 ....UH..H....E..
    0x00000010 00000048 8d050000 00004889 c6488d05 ...H......H..H..
    0x00000020 00000000 4889c7b8 00000000 e8000000 ....H...........
    0x00000030 00b80000 0000c9c3 ........
  5. Specific to .data by readelf -x .data memory_layout.o

    Terminal window
    c@c:~/cos3105$ readelf -x .data memory_layout.o
    Hex dump of section '.data':
    0x00000000 64000000 d...
  6. Specific to .bss by readelf -x .bss memory_layout.o

    Terminal window
    c@c:~/cos3105$ readelf -x .bss memory_layout.o
    Section '.bss' has no data to dump.

Observe symbol/variable of each sections in Object file

Section titled “Observe symbol/variable of each sections in Object file”
  1. Observe symbol in .text by nm memory_layout.o | grep " T "

    Terminal window
    c@c:~/cos3105$ nm memory_layout.o | grep " T "
    0000000000000000 T main
  2. Observe symbol in .data by nm memory_layout.o | grep " D "

    Terminal window
    c@c:~/cos3105$ nm memory_layout.o | grep " D "
    0000000000000000 D variable_data
  3. Observe symbol in .bss by nm memory_layout.o | grep " B "

    Terminal window
    c@c:~/cos3105$ nm memory_layout.o | grep " B "
    0000000000000000 B variable_bss

Observe symbol/variable size of data section in Object file

Section titled “Observe symbol/variable size of data section in Object file”
  1. Create file memory_layout.c

    memory_layout.c
    #include <stdio.h>
    int variable_data = 100; // Goes to DATA (initialized global) - 4 bytes
    int variable_bss; // Goes to BSS (uninitialized global) - not in DATA
    int main(){
    int variable_local_x = 200; // Goes to STACK (local variable) - not in DATA
    int variable_local_y; // Goes to STACK (local variable) - not in DATA
    return 0;
    }
  2. Create object file by gcc memory_layout.c -c memory_layout.o

    Terminal window
    c@c:~/cos3105$ gcc memory_layout.c -c memory_layout.o
    gcc: warning: memory_layout.o: linker input file unused because linking not done
  3. Run size for observe size of object file

    Terminal window
    c@c:~/cos3105$ size memory_layout.o
    text data bss dec hex filename
    147 4 4 155 9b memory_layout.o