Skip to content

Instantly share code, notes, and snippets.

@awreece
Created April 29, 2014 18:35
Show Gist options
  • Save awreece/11408390 to your computer and use it in GitHub Desktop.
Save awreece/11408390 to your computer and use it in GitHub Desktop.
$ gcc -fPIC -shared -c myctor.c -o myctor.o
$ gcc -fPIC -shared myctor.o -o libmyctor.so
$ LD_PRELOAD=$(pwd)/libmyctor.so ls
myctor
libmyctor.so myctor.c myctor.o
$ % dis -t .init libmyctor.so
disassembly for libmyctor.so
section .init
_init()
_init: 55 pushl %ebp
_init+0x1: 89 e5 movl %esp,%ebp
_init+0x3: 83 e4 f0 andl $0xfffffff0,%esp
_init+0x6: 83 ec 0c subl $0xc,%esp
_init+0x9: 53 pushl %ebx
_init+0xa: e8 00 00 00 00 call +0x0 <_init+0xf>
_init+0xf: 5b popl %ebx
_init+0x10: 81 c3 49 00 01 00 addl $0x10049,%ebx
_init+0x16: e8 35 ff ff ff call -0xcb <frame_dummy>
_init+0x1b: e8 a0 ff ff ff call -0x60 <__do_global_ctors_aux>
_init+0x20: 5b popl %ebx
_init+0x21: c9 leave
_init+0x22: c3 ret
$ libtool --mode=compile gcc -g -O -c myctor.c
libtool: compile: gcc -g -O -c myctor.c -fPIC -DPIC -o .libs/myctor.o
$ libtool --mode=link gcc -g -O -o libmyctor.la myctor.lo -rpath /usr/lib
libtool: link: rm -fr .libs/libmyctor.a .libs/libmyctor.la
libtool: link: gcc -shared -fPIC -DPIC -Wl,-z -Wl,text -Wl,-h -Wl,libmyctor.so.0 -o .libs/libmyctor.so.0.0.0 .libs/myctor.o -O -nostdlib -lc
libtool: link: (cd ".libs" && rm -f "libmyctor.so.0" && ln -s "libmyctor.so.0.0.0" "libmyctor.so.0")
libtool: link: (cd ".libs" && rm -f "libmyctor.so" && ln -s "libmyctor.so.0.0.0" "libmyctor.so")
libtool: link: ( cd ".libs" && rm -f "libmyctor.la" && ln -s "../libmyctor.la" "libmyctor.la" )
$ LD_PRELOAD=$(pwd)/.libs/libmyctor.so ls
libmyctor.la myctor.c myctor.lo myctor.o
$ dis -t .init .libs/libmyctor.so
disassembly for .libs/libmyctor.so
dis: warning: failed to find section '.init' in '.libs/libmyctor.so'
#include <stdio.h>
void __attribute__((constructor)) myctor() {
printf("myctor\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment