From 476d1f5deaf8a2982256d0d978a3ce99ae68723e Mon Sep 17 00:00:00 2001 From: FreeArtMan Date: Fri, 12 Aug 2022 11:33:09 +0100 Subject: Add static --- md/notes/undefined_c/titles.md | 85 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) (limited to 'md') diff --git a/md/notes/undefined_c/titles.md b/md/notes/undefined_c/titles.md index 014035b..158ed20 100644 --- a/md/notes/undefined_c/titles.md +++ b/md/notes/undefined_c/titles.md @@ -454,6 +454,7 @@ int main() #include #include #include +#include int main() { int arr[4] = {0x00112233,0x44556677,0x8899AABB, 0xCCDDEEFF}; @@ -509,12 +510,96 @@ $ hexdump int.bin2 Compiler have whole list of command line arguments that you can enable for different purposes, lets look into some of them https://gcc.gnu.org/onlinedocs/gcc/Option-Summary.html +Lets try to apply some of the flags to examples above. + +Best starte options is, those will give you more warnings. +``` +-Wall -Wextra +``` + +Most of the examples here was written in sloppy style, so adding extra checks like will find more issues with code, probably +all of provided examples will show issues with this extra compiler flags + +``` +Wformat-security -Wduplicated-cond -Wfloat-equal -Wshadow -Wconversion -Wjump-misses-init -Wlogical-not-parentheses -Wnull-dereference +``` + +To get all macroses expanded in C code add compiler flag. Output will be C source with all macro expansion +``` +-E +``` + +Output resulting file not to binary but to generated assembly add +``` +-S +``` + +More readable output can be obtained with + +``` +gcc FILE.c -Wa,-adhln=FILE.S -g -fverbose-asm -masm=intel +``` + +Basic compiler optimisation flags that can speedup program or make it smaller + +``` +-O -O0 -O1 -O2 -O3 -Os -Ofast -Og -Oz +``` + +https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#Optimize-Options + +https://panthema.net/2013/0124-GCC-Output-Assembler-Code/ +https://blogs.oracle.com/linux/post/making-code-more-secure-with-gcc-part-1 ### Static binary + +Static binary don't use any shared libraries, and its possible to built it once and distribute on other platforms +without need to install dependencies. + + +```c +#include +#include + +int main(int argc, char **argv) { + return 0; +} +``` + +First step to compile file and see that is dynamically lined +``` +$ gcc static_elf.c -o static_elf +$ file static_elf +static_elf: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=bc6ac706075874858e1c4a8accf77e704f4ea25a, for GNU/Linux 4.4.0, with debug_info, not stripped +$ ldd ./static_elf + linux-vdso.so.1 (0x00007ffccef49000) + libc.so.6 => /usr/lib/libc.so.6 (0x00007fcbb8800000) + /lib64/ld-linux-x86-64.so.2 => /usr/lib64/ld-linux-x86-64.so.2 (0x00007fcbb8b63000) + +``` + +After adding static option we can verify that tools now report it as statically linked. Size of binary increased as all functions +that require to run executable are now contained in binary. + +``` +$ gcc static_elf.c -static -o static_elf +$ file static_elf +static_elf: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), statically linked, BuildID[sha1]=c54d2e4d2a3d11fe920bee9a44af045c6f67ab56, for GNU/Linux 4.4.0, with debug_info, not stripped +$ ldd static_elf + not a dynamic executable +``` + +Statically compiled file should work on most platforms. + ### Dynamic binary + + + ### stdin,stdout,stderr + + ### Styles -- cgit v1.2.3