Open an3l opened 4 years ago
Cppcoreguidlenes [l1].
cppref [l2]
godbolt [l3]
quick-bench [l4]
Talk [l5]
empty program 8KB gcc -0s
(optimize size)
objdump --no-show-raw-insn -dC c/empyt
(-d: dissassembly, C: demangling (decode low level symbol names into user names man objdump
), --no-show: no bytecode (instruction bytes) when dissassemblyng the instruction (--prefix-addresses
) addresses about 200hex -> 512B
Dump everything readelf -a empty
(elf -> executable and link format)
.text
(code).rodata
(read-only data - can be loaded somewhere else and attempt can to write can cause error).data
(rw data).bss
(block started by symbol -> zero-initialised data, global scope and set to zero, will be there)Create a struct example with global object with behavior to run at construction time -> bss section global variable, without declared object it will be 0, and with 1.
Breakpoint on constructor to see when it gets incremented.
init_array
section different from .text
Linker collect .o files, resolves references between .o files, determining layout of exe, writes metadata. gcc -c -0s hello.c -o hello.o && gcc -c -Os message.c -message.o && gcc -Os hello.o message.o -o hello
. To see the files file hello hello.o message.o
$ file hello hello.o message.o
hello: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically
linked, interpreter /lib64/l, for GNU/Linux 3.2.0,
BuildID[sha1]=f5c45fe1311787809821b531a0e0de9701f998ea, not stripped
hello.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
message.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
objdump -d hello.o
:
Disassembly of section .text:
0000000000000000
0000000000000000
- **Relocation**: Linker needs to find the symbol and when find it where to put it.
- Zeros in opcode needs to be converted to something by linker -> `objdump --reloc -d hello.o`, show content of relocation section, separate section in obj. files that describes all things needed to be done to link exe.
Disassembly of section .text:
0000000000000000
Disassembly of section .text.startup:
0000000000000000
- Symbols: section in a file providing all needed symbols `objdump --syms -C hello.o` or `objdump -t -C hello.o`, `l`-> local, `g` -> global, `F`- >defined in this file,`getMessage` undef.
SYMBOL TABLE: 0000000000000000 l df ABS 0000000000000000 hello.c 0000000000000000 l d .text 0000000000000000 .text 0000000000000000 l d .data 0000000000000000 .data 0000000000000000 l d .bss 0000000000000000 .bss 0000000000000000 l d .text.startup 0000000000000000 .text.startup 0000000000000000 l d .note.GNU-stack 0000000000000000 .note.GNU-stack 0000000000000000 l d .eh_frame 0000000000000000 .eh_frame 0000000000000000 l d .comment 0000000000000000 .comment 0000000000000000 g F .text 0000000000000014 greet 0000000000000000 UND 0000000000000000 _GLOBAL_OFFSETTABLE 0000000000000000 UND 0000000000000000 getMessage 0000000000000000 UND 0000000000000000 puts 0000000000000000 g F .text.startup 000000000000000f main
$ objdump -t message.o
message.o: file format elf64-x86-64
SYMBOL TABLE: 0000000000000000 l df ABS 0000000000000000 message.c 0000000000000000 l d .text 0000000000000000 .text 0000000000000000 l d .data 0000000000000000 .data 0000000000000000 l d .bss 0000000000000000 .bss 0000000000000000 l d .rodata.str1.1 0000000000000000 .rodata.str1.1 0000000000000000 l d .note.GNU-stack 0000000000000000 .note.GNU-stack 0000000000000000 l d .eh_frame 0000000000000000 .eh_frame 0000000000000000 l .rodata.str1.1 0000000000000000 .LC0 0000000000000000 l d .comment 0000000000000000 .comment 0000000000000000 g F .text 0000000000000008 getMessage
[l1]: http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines
[l2]: https://en.cppreference.com/w/
[l3]: https://godbolt.org/
[l4]: http://quick-bench.com/
[l5]: https://www.youtube.com/watch?v=dOfucXtyEsU
In C you preprocess, then compile and lastly link your object files.
-D
passes a preprocessor definition which should be passed even before compilation.
gcc -DMY_FUN='XXX' -c main.c
gcc -DMY_FUN='XXX' -c foo.c
gcc main.o foo.o
./a.out
First and second lines preprocess and compile. Third line links object files and produce actual executable with default name a.out
$ cat main.c
#include "stdio.h"
extern const char *MY_FUN();
int main()
{
printf("Hi !\n");
printf("%s\n", MY_FUN());
return 0;
}
$ cat foo.c
const char *MY_FUN()
{
const char *r= __func__;
return r;
}
See preprocesor defaults declaration header search paths 1:
$ cpp -v /dev/null -o /dev/null
Using built-in specs.
COLLECT_GCC=cpp
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 7.5.0-3ubuntu1~18.04' --with-bugurl=file:///usr/share/doc/gcc-7/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-7 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)
COLLECT_GCC_OPTIONS='-E' '-v' '-o' '/dev/null' '-mtune=generic' '-march=x86-64'
/usr/lib/gcc/x86_64-linux-gnu/7/cc1 -E -quiet -v -imultiarch x86_64-linux-gnu /dev/null -o /dev/null -mtune=generic -march=x86-64 -fstack-protector-strong -Wformat -Wformat-security
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/7/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
**#include <...> search starts here:**
/usr/lib/gcc/x86_64-linux-gnu/7/include
/usr/local/include
/usr/lib/gcc/x86_64-linux-gnu/7/include-fixed
/usr/include/x86_64-linux-gnu
**/usr/include**
End of search list.
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/7/:/usr/lib/gcc/x86_64-linux-gnu/7/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/7/:/usr/lib/gcc/x86_64-linux-gnu/
**LIBRARY_PATH**=/usr/lib/gcc/x86_64-linux-gnu/7/:/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/7/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/7/../../../:/lib/:**/usr/lib/**
COLLECT_GCC_OPTIONS='-E' '-v' '-o' '/dev/null' '-mtune=generic' '-march=x86-64'
Definitions in /usr/lib/
. find . -name 'libm.a -> /lib/x86_64-linux-gnu/libm.a'
gcc /abspath/libm.a
CPATH
same as gcc -Icustomdir
2. Change #include "some.h" from current file to something else iqoute header1
-> test cpp -iqoute header1 -v
, use Makefile.
thread apply all bt
or coredump gdb <binary> <coredump> -ex "thread apply all bt" -ex "quit" > output.log
Set breakpoint to all function in class
rbreak slave_connection_state::.
Can be used for all functions in file also
rbreak file:.
also if you want a lot of breakpoints, you can mistype thr a a br
instead of thr a a bt
does rbreak
create one breakpoint or many? I mean, in i b
output
PS4='$LINENO: ' && bash -x ./mysqld_safe --defaults-file=~/.my.cnf
cmake ../../10.5 && sudo cmake --build . --target install -- DESTDIR=../anel -j8
andcpack
l4cmake ../../10.2 -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_COMPILER=g++-9 -DCMAKE_C_COMPILER=gcc-9 -DWITH_EMBEDDED_SERVER=OFF -DWITH_UNIT_TESTS=OFF -DCMAKE_BUILD_TYPE=Debug -DPLUGIN_{ARCHIVE,TOKUDB,MROONGA,OQGRAPH,ROCKSDB,PERFSCHEMA,SPIDER,SPHINX}=NO