1 - Program
Object file
Section titled “Object file”Observe sections in Object file
Section titled “Observe sections in Object file”-
Create file
memory_layout.cmemory_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;} -
Create object file by
gcc memory_layout.c -c memory_layout.oTerminal window c@c:~/cos3105$ gcc memory_layout.c -c memory_layout.ogcc: warning: memory_layout.o: linker input file unused because linking not done -
List all of sections in Object file
readelf -S memory_layout.oTerminal window c@c:~/cos3105$ readelf -S memory_layout.oThere are 12 section headers, starting at offset 0x228:Section Headers:[Nr] Name Type Address OffsetSize EntSize Flags Link Info Align[ 0] NULL 0000000000000000 000000000000000000000000 0000000000000000 0 0 0[ 1] .text PROGBITS 0000000000000000 000000400000000000000016 0000000000000000 AX 0 0 1[ 2] .data PROGBITS 0000000000000000 000000580000000000000004 0000000000000000 WA 0 0 4[ 3] .bss NOBITS 0000000000000000 0000005c0000000000000004 0000000000000000 WA 0 0 4[ 4] .comment PROGBITS 0000000000000000 0000005c000000000000002c 0000000000000001 MS 0 0 1[ 5] .note.GNU-stack PROGBITS 0000000000000000 000000880000000000000000 0000000000000000 0 0 1[ 6] .note.gnu.pr[...] NOTE 0000000000000000 000000880000000000000020 0000000000000000 A 0 0 8[ 7] .eh_frame PROGBITS 0000000000000000 000000a80000000000000038 0000000000000000 A 0 0 8[ 8] .rela.eh_frame RELA 0000000000000000 000001a80000000000000018 0000000000000018 I 9 7 8[ 9] .symtab SYMTAB 0000000000000000 000000e00000000000000090 0000000000000018 10 3 8[10] .strtab STRTAB 0000000000000000 000001700000000000000031 0000000000000000 0 0 1[11] .shstrtab STRTAB 0000000000000000 000001c00000000000000067 0000000000000000 0 0 1Key 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) -
Specific to .text by
readelf -x .text memory_layout.oTerminal window c@c:~/cos3105$ readelf -x .text memory_layout.oHex 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 ........ -
Specific to .data by
readelf -x .data memory_layout.oTerminal window c@c:~/cos3105$ readelf -x .data memory_layout.oHex dump of section '.data':0x00000000 64000000 d... -
Specific to .bss by
readelf -x .bss memory_layout.oTerminal window c@c:~/cos3105$ readelf -x .bss memory_layout.oSection '.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”-
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 -
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 -
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”-
Create file
memory_layout.cmemory_layout.c #include <stdio.h>int variable_data = 100; // Goes to DATA (initialized global) - 4 bytesint variable_bss; // Goes to BSS (uninitialized global) - not in DATAint main(){int variable_local_x = 200; // Goes to STACK (local variable) - not in DATAint variable_local_y; // Goes to STACK (local variable) - not in DATAreturn 0;} -
Create object file by
gcc memory_layout.c -c memory_layout.oTerminal window c@c:~/cos3105$ gcc memory_layout.c -c memory_layout.ogcc: warning: memory_layout.o: linker input file unused because linking not done -
Run
sizefor observe size of object fileTerminal window c@c:~/cos3105$ size memory_layout.otext data bss dec hex filename147 4 4 155 9b memory_layout.o