CS 202 - Notes 2018-09-12
Source to app
write the program
- high level programming language
- just a text file
- need something structured because English is too ambiguous
intro.c
#include <stdio.h>
int main(int argc, char* argv[]){
printf("Don't Panic!\n");
}
compile the program (gcc -S intro.c
)
- transform the source code to assembly
- still a text file, still human readable (nominally) still can't be read by the computer
- but now at the level the computer understands (low level language)
intro.s
.file "intro.c"
.text
.section .rodata
.LC0:
.string "Don't Panic!"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $16, %rsp
movl %edi, -4(%rbp)
movq %rsi, -16(%rbp)
movl $.LC0, %edi
call puts
movl $0, %eax
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (GNU) 7.3.1 20180712 (Red Hat 7.3.1-6)"
.section .note.GNU-stack,"",@progbits
assemble the program (gcc -c intro.s
)
- translate the assembly into machine code
- produces object file
- no longer text, now binary
Examining the object file
We can use hexdump
to examine the actual contents of any file. In canonical mode (hexdump -C
), it displays each byte in hex, and shows any bytes that can be interpreted as ASCII characters on the right (I showed you a slightly different format in class).
Note the leading 7f 45 4c 46, which tells the system this is an ELF file (i.e., Linux executable).
$ hexdump -C intro.o
00000000 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 |.ELF............|
00000010 01 00 3e 00 01 00 00 00 00 00 00 00 00 00 00 00 |..>.............|
00000020 00 00 00 00 00 00 00 00 a8 02 00 00 00 00 00 00 |................|
00000030 00 00 00 00 40 00 00 00 00 00 40 00 0d 00 0c 00 |....@.....@.....|
00000040 55 48 89 e5 48 83 ec 10 89 7d fc 48 89 75 f0 bf |UH..H....}.H.u..|
00000050 00 00 00 00 e8 00 00 00 00 b8 00 00 00 00 c9 c3 |................|
00000060 44 6f 6e 27 74 20 50 61 6e 69 63 21 00 00 47 43 |Don't Panic!..GC|
00000070 43 3a 20 28 47 4e 55 29 20 37 2e 33 2e 31 20 32 |C: (GNU) 7.3.1 2|
00000080 30 31 38 30 37 31 32 20 28 52 65 64 20 48 61 74 |0180712 (Red Hat|
00000090 20 37 2e 33 2e 31 2d 36 29 00 00 00 00 00 00 00 | 7.3.1-6).......|
000000a0 14 00 00 00 00 00 00 00 01 7a 52 00 01 78 10 01 |.........zR..x..|
000000b0 1b 0c 07 08 90 01 00 00 1c 00 00 00 1c 00 00 00 |................|
000000c0 00 00 00 00 20 00 00 00 00 41 0e 10 86 02 43 0d |.... ....A....C.|
000000d0 06 5b 0c 07 08 00 00 00 00 00 00 00 00 00 00 00 |.[..............|
000000e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
000000f0 01 00 00 00 04 00 f1 ff 00 00 00 00 00 00 00 00 |................|
00000100 00 00 00 00 00 00 00 00 00 00 00 00 03 00 01 00 |................|
00000110 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000120 00 00 00 00 03 00 03 00 00 00 00 00 00 00 00 00 |................|
00000130 00 00 00 00 00 00 00 00 00 00 00 00 03 00 04 00 |................|
00000140 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000150 00 00 00 00 03 00 05 00 00 00 00 00 00 00 00 00 |................|
00000160 00 00 00 00 00 00 00 00 00 00 00 00 03 00 07 00 |................|
00000170 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000180 00 00 00 00 03 00 08 00 00 00 00 00 00 00 00 00 |................|
00000190 00 00 00 00 00 00 00 00 00 00 00 00 03 00 06 00 |................|
000001a0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
000001b0 09 00 00 00 12 00 01 00 00 00 00 00 00 00 00 00 |................|
000001c0 20 00 00 00 00 00 00 00 0e 00 00 00 10 00 00 00 | ...............|
000001d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
000001e0 00 69 6e 74 72 6f 2e 63 00 6d 61 69 6e 00 70 75 |.intro.c.main.pu|
000001f0 74 73 00 00 00 00 00 00 10 00 00 00 00 00 00 00 |ts..............|
00000200 0a 00 00 00 05 00 00 00 00 00 00 00 00 00 00 00 |................|
00000210 15 00 00 00 00 00 00 00 02 00 00 00 0a 00 00 00 |................|
00000220 fc ff ff ff ff ff ff ff 20 00 00 00 00 00 00 00 |........ .......|
00000230 02 00 00 00 02 00 00 00 00 00 00 00 00 00 00 00 |................|
00000240 00 2e 73 79 6d 74 61 62 00 2e 73 74 72 74 61 62 |..symtab..strtab|
00000250 00 2e 73 68 73 74 72 74 61 62 00 2e 72 65 6c 61 |..shstrtab..rela|
00000260 2e 74 65 78 74 00 2e 64 61 74 61 00 2e 62 73 73 |.text..data..bss|
00000270 00 2e 72 6f 64 61 74 61 00 2e 63 6f 6d 6d 65 6e |..rodata..commen|
00000280 74 00 2e 6e 6f 74 65 2e 47 4e 55 2d 73 74 61 63 |t..note.GNU-stac|
00000290 6b 00 2e 72 65 6c 61 2e 65 68 5f 66 72 61 6d 65 |k..rela.eh_frame|
000002a0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
000002e0 00 00 00 00 00 00 00 00 20 00 00 00 01 00 00 00 |........ .......|
000002f0 06 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000300 40 00 00 00 00 00 00 00 20 00 00 00 00 00 00 00 |@....... .......|
00000310 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 |................|
00000320 00 00 00 00 00 00 00 00 1b 00 00 00 04 00 00 00 |................|
00000330 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |@...............|
00000340 f8 01 00 00 00 00 00 00 30 00 00 00 00 00 00 00 |........0.......|
00000350 0a 00 00 00 01 00 00 00 08 00 00 00 00 00 00 00 |................|
00000360 18 00 00 00 00 00 00 00 26 00 00 00 01 00 00 00 |........&.......|
00000370 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000380 60 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |`...............|
00000390 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 |................|
000003a0 00 00 00 00 00 00 00 00 2c 00 00 00 08 00 00 00 |........,.......|
000003b0 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
000003c0 60 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |`...............|
000003d0 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 |................|
000003e0 00 00 00 00 00 00 00 00 31 00 00 00 01 00 00 00 |........1.......|
000003f0 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000400 60 00 00 00 00 00 00 00 0d 00 00 00 00 00 00 00 |`...............|
00000410 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 |................|
00000420 00 00 00 00 00 00 00 00 39 00 00 00 01 00 00 00 |........9.......|
00000430 30 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |0...............|
00000440 6d 00 00 00 00 00 00 00 2d 00 00 00 00 00 00 00 |m.......-.......|
00000450 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 |................|
00000460 01 00 00 00 00 00 00 00 42 00 00 00 01 00 00 00 |........B.......|
00000470 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000480 9a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000490 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 |................|
000004a0 00 00 00 00 00 00 00 00 57 00 00 00 01 00 00 00 |........W.......|
000004b0 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
000004c0 a0 00 00 00 00 00 00 00 38 00 00 00 00 00 00 00 |........8.......|
000004d0 00 00 00 00 00 00 00 00 08 00 00 00 00 00 00 00 |................|
000004e0 00 00 00 00 00 00 00 00 52 00 00 00 04 00 00 00 |........R.......|
000004f0 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |@...............|
00000500 28 02 00 00 00 00 00 00 18 00 00 00 00 00 00 00 |(...............|
00000510 0a 00 00 00 08 00 00 00 08 00 00 00 00 00 00 00 |................|
00000520 18 00 00 00 00 00 00 00 01 00 00 00 02 00 00 00 |................|
00000530 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000540 d8 00 00 00 00 00 00 00 08 01 00 00 00 00 00 00 |................|
00000550 0b 00 00 00 09 00 00 00 08 00 00 00 00 00 00 00 |................|
00000560 18 00 00 00 00 00 00 00 09 00 00 00 03 00 00 00 |................|
00000570 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000580 e0 01 00 00 00 00 00 00 13 00 00 00 00 00 00 00 |................|
00000590 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 |................|
000005a0 00 00 00 00 00 00 00 00 11 00 00 00 03 00 00 00 |................|
000005b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
000005c0 40 02 00 00 00 00 00 00 61 00 00 00 00 00 00 00 |@.......a.......|
000005d0 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 |................|
000005e0 00 00 00 00 00 00 00 00 |........|
000005e8
We can also use objdump
to examine the contents of object files. In particular, we can disassemble the file.
As we observed in class, this is an easy exercise because there is a pretty direct translation between assembly and machine code.
$ objdump -d intro.o
intro.o: file format elf64-x86-64
Disassembly of section .text:
0000000000000000 <main>:
0: 55 push %rbp
1: 48 89 e5 mov %rsp,%rbp
4: 48 83 ec 10 sub $0x10,%rsp
8: 89 7d fc mov %edi,-0x4(%rbp)
b: 48 89 75 f0 mov %rsi,-0x10(%rbp)
f: bf 00 00 00 00 mov $0x0,%edi
14: e8 00 00 00 00 callq 19 <main+0x19>
19: b8 00 00 00 00 mov $0x0,%eax
1e: c9 leaveq
1f: c3 retq
Compiled vs interpreted
When you write in a language like Python or Java, there are some slight differences
Python is interpreted. The executable that is actually running is a program called python
. Your code is just input to that process. The python
program reads your file and carries out your instructions for you.
Java is a slightly different in that there is a compilation stage. Java compiles to "Java Bytecode" rather than machine language, however. You then run the bytecode on a "Java virtual machine", which is, in essence, an emulation of the hardware. This makes the compiled form of Java portable to any platform with a virtual machine implementation written for it.
Note that with Python and Java, there is still a compiled program running on the processor -- it just isn't your code, it is the interpreter or the virtual machine.