/** * $Id$ * * Copyright (C) shinichiro.h * http://shinh.skr.jp/ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include #include static void* reserved_byte; int main(int argc, char* argv); extern char _binary_COPYING_start[], _binary_COPYING_end[]; int gpl_main(int argc, char* argv[]) { char* m = (char*)main; void** ma = (void**)(m+1); int i; *ma = reserved_byte; mprotect((void*)(((int)m+4095)&~4095-4096), 4096, PROT_READ | PROT_EXEC); for (i = 1; i < argc; i++) { if (strcmp("--copying", argv[i]) == 0) { fwrite(_binary_COPYING_start, 1, _binary_COPYING_end - _binary_COPYING_start, stdout); printf("\n"); for (i++; i < argc; i++) { argv[i-1] = argv[i]; } __asm__("subl $0x1, 0x8(%ebp)"); goto jmp; } } /* is this too verbose? */ /* printf("%s is free software, covered by the GNU General Public License,\nand you are welcome to change it and/or distribute copies of it under\ncertain conditions. There is absolutely no warranty for %s.\n\n", argv[0], argv[0]); */ jmp: __asm__("jmp *%0"::"g"(m+3)); } void __attribute__ ((constructor)) gpl_init() { char* m = (char*)main; void** ma = (void**)(m+1); mprotect((void*)(((int)m+4095)&~4095-4096), 4096, PROT_READ | PROT_WRITE | PROT_EXEC); m[0] = 0xe9; reserved_byte = *ma; *ma = (void*)((int)gpl_main-(int)main-5); }