Cheat Sheet :: GCC Compiling Cheat Sheet

1. Compilação estática

1
gcc -static cve_2016_0728.c -o cve_2016_0728 -lkeyutils -Wall

2. Compilação cruzada de C do Linux para Windows

2.1. Instalar o cross-compiler mingw-w64:

1
apt-get install mingw-w64

2.2. Para compilar um executável Windows PE 32-bit:

1
2
i686-w64-mingw32-gcc 271.c -o 271
x86_64-w64-mingw32-gcc-win32 271.c -o 271

2.3. Rodar o binário com wine:

1
wine file.exe

3. Compilando com símbolos de debug

Tipos de arquivo de símbolo de debug:

  1. DWARF 2
  2. COFF
  3. XCOFF
  4. STABS

3.1. O GCC usa a opção -g para definir o tipo:

1
gcc -ggdb source.c -o prog_with_symbols

3.2. Removendo símbolos do binário

1
objcopy --strip-debug --strip-unneeded prog_not_stripped prog_stripped

3.3. Extraindo símbolos do binário

1
objcopy --only-keep-debug rip_from_binary debug_file

4. Brincando com ponteiros em C

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>

int main(void){
    int var = 5;
    int *p;

    printf("Address of var is: %p\n", &var);
    printf("Address of p is: %p\n", &p);

    p = &var;
    printf("Content of p is: %p\n", p);
    printf("Content wich p is pointing is: %i\n", *p);
    printf("Content of var is: %i\n", var);
    printf("Address of var is: %p\n", &var);
    printf("Address of p is: %p\n", &p);

    printf("*********************\n");
    *p = 100;
    printf("%i\n", var);
    printf("%i\n", *p);
    printf("Content of p is: %p\n", p);
    printf("Address of var is: %p\n", &var);
    printf("Address of p is: %p\n", &p);

    printf("*********************\n");
    p = 300;
    printf("%i\n", var);
    printf("%i\n", p);
    printf("Content of p is: %p\n", p);
    printf("Address of var is: %p\n", &var);
    printf("Address of p is: %p\n", &p);

    printf("*********************\n");
    printf("Final content of var: %i\n", var);
    printf("Content of p is: %p\n", p);
    return 0;
}