5 &GetOptions(32=>\$m32, 64=>\$m64, "output=s"=>\$mod);
7 $#ARGV >= 0 or die "usage: compile-for-static-user-level-injection.pl [-32|-64] [--output=module_name] source.c+ [lib.a]*\n";
10 print "Assuming 32 bit. Use -64 to override\n";
15 print "No module name given, assuming a.tooth\n";
21 $lopt = "-melf_i386 --oformat elf32-i386";
26 $lopt = "-melf_x86_64 --oformat elf64-x86-64";
30 $linkerscript = <<END;
33 /* Must be on a page boundary */
34 /* Should link like ld -z max-page-size=4096 -T ld.script ... */
35 /* If object file is -fPIC, then it shouldn't matter where we load it */
37 /* Text, data, and bss squished together */
41 /* Result will be one load group marked RWX */
47 @stems=grep(/.*\.c$/,@ARGV);
48 @libs=grep(/.*\.a$/,@ARGV);
50 map { $_ =~ s/\.c$//g} @stems;
52 print "Compiling...\n";
54 system("gcc $gopt -fPIE -Wa,-R -c $s.c -nostartfiles -nodefaultlibs -nostdlib -static -o $s.o") == 0
55 or die "Compilation of $s.c failed\n";
56 system("gcc $gopt -fPIE -Wa,-R -S $s.c -nostartfiles -nodefaultlibs -nostdlib -static -o $s.s") == 0
57 or die "Compilation of $s.c failed\n";
59 print "Compilation done.\n";
61 open(W,">.linker_script");
62 print W $linkerscript;
67 $rc=system("ld $lopt -z max-page-size=4096 -T .linker_script ".join(" ",map { "$_.o" } @stems)." ".join(" ",@libs)." -o $mod\n");
69 unlink ".linker_script";
71 $rc==0 or die "Linking of $mod failed\n";
73 print "Linking of $mod completed. Done.\n";
75 open(E,"readelf -h $mod |");
77 if (/^\s*Entry point address:\s+(\S+)$/) {
78 print "Entry point relative to beginning of file: $1\n";