ELF Hacks

我喜欢基于代码片段的学习,之前学习过一些binutils和其他处理ELF的工具的奇技淫巧,了解到很多概念,比如weak symbol、common section、RPATH、把资源文件嵌入ELF等,昨天创建了一个项目https://github.com/MaskRay/ElfHacks整理了很多自包含的例子。

#ProgramDescription
backtracegccbacktrace(3)
bss-section-in-c-and-c++gcc/g++.bss
gcc-attribute-aliasgccalias attribute emits an alias for another symbol
gcc-nostdlibgccdo not use standard system startup files or libraries
gcc-piegccproduce a position independent executable (IMHO, like PIC+Bsymbolic)
gcc-staticgccstatically linked executable
g++-inline-means-weak-symbolg++inline functions translated to weak symbols
implicit-inline-member-functiong++member functions defined in classes are implicitly inline
ld-Bsymbolicld-Bsymbolic binds references to local symbols
ld-dy-dnld-dn makes ld link against static libraries
ld-execstackld-z execstack and NX bit
ld-export-dynamicld--export-dynamic makes executables export dynamic symbols
ld-nowld-z now tells ld.so to resolve symbols immediately
ld-rpathld-rpath sets DT_RUNPATH which adds a directory to runtime library search path
ld-whole-archiveld--whole-archive includes every object files (not only those which are required)
ld-wrapld--wrap makes undefined references to SYMBOL be resolved to __wrap_SYMBOL
LD_BIND_NOWld.soresolve all symbols at startup instead of deferring resolution to the first call
LD_PROFILEld.soprofile a shared library
LD_TRACE_LOADED_OBJECTSld.solist dynamic library dependencies rather than running
as-syscallasmake syscalls in x86-64 assembly
as-syscall-i386asmake syscalls in i386 assembly
shellcode-in-cgcc,objcopywrite shellcode in c using gcc & objcopy
objcopy-link-blobobjcopylink blob into executable
libSegFaultglibc

每个例子都存放在单独的目录,里面有Makefile。在单独的目录里,执行make show可以看到说明,和所描述特性相关的输出用终端的红色字标出了。有些例子提供了make gdb,是用Expect脚本写的gdb的交互式会话,用来观察所描述的特性如何影响程序的运行时状态。

项目根目录也提供了Makefile,执行make show或make gdb会递归地在各例子目录里执行make。