Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Use: gcc -s -lSDL -nostartfiles -nostdlib main.c
- #include <dlfcn.h>
- #include <syscall.h>
- #include <fcntl.h>
- #include <elf.h>
- #include <stdio.h>
- #include <SDL/SDL.h>
- #include <link.h>
- //****************************************************************************************
- //Some string functions for debug output.
- //****************************************************************************************
- long syscall3(long num, long arg1, long arg2, long arg3) {
- long __res;
- __asm__ volatile ("int $0x80":"=a"(__res):"0"(num), "b"(arg1), "c"(arg2), "d"(arg3));
- return __res;
- }
- void printhexstring_(unsigned int num) {
- char string[9];
- int i;
- unsigned char digit;
- for(i=0; i<8; i++) {
- digit=num&0xf;
- if(digit>9) digit+=('a'-10); else digit+='0';
- string[7-i]=digit;
- num>>=4;
- }
- string[8]='\n';
- long result=syscall3(SYS_write, 0, (long)string, 9);
- }
- void printstring_(long string) {
- long count=0;
- char* p=(char*)string;
- while(p[count]!='\0')
- count++;
- long result=syscall3(SYS_write, 0, string, count+1);
- }
- void printkeyandvalue_(long string, long value) {
- printstring_((long)string);
- printhexstring_((long)value);
- }
- //*****************************************************************************************
- const long baseAddress=0x08048000; //base address that the executable is loaded at
- struct link_map* GetLinkMap() {
- Elf32_Ehdr* ehdr=(Elf32_Ehdr*)baseAddress; //the elf header
- Elf32_Phdr* phdr=(Elf32_Phdr*)((void*)((unsigned int)ehdr+(unsigned int)ehdr->e_phoff)); //the program header
- //find the dynamic section entry in the program header
- while(phdr->p_type!=PT_DYNAMIC) {
- phdr++;
- }
- //get the dynamic section's address
- Elf32_Dyn* dyn=(Elf32_Dyn*)phdr->p_vaddr;
- //find the debug entry
- while(dyn->d_tag!=DT_DEBUG) {
- dyn++;
- }
- struct r_debug* debug=(struct r_debug*)dyn->d_un.d_ptr;
- return debug->r_map;
- }
- unsigned long GetDynamicSectionValue(struct link_map* map, long tag) {
- Elf32_Dyn* dynamic=(Elf32_Dyn*)map->l_ld;
- while(dynamic->d_tag!=tag)
- dynamic++;
- unsigned long address=(long)dynamic->d_un.d_ptr;
- if(address<map->l_addr)
- address+=map->l_addr;
- return address;
- }
- unsigned long Hash(unsigned char* str) {
- unsigned long hash=0;
- int c;
- while(c=*str++)
- hash=((hash<<5)+hash)^c;
- return hash;
- }
- void* FindSymbolInMap(struct link_map* linkMap, unsigned long hash) {
- char* strtab=(char*)GetDynamicSectionValue(linkMap, DT_STRTAB);
- Elf32_Sym* symtab=(Elf32_Sym*)GetDynamicSectionValue(linkMap, DT_SYMTAB);
- unsigned long* hashtable=(unsigned long*)GetDynamicSectionValue(linkMap, DT_HASH);
- unsigned long i;
- for(i=0; i<hashtable[1]; i++) { //hashtable[1] == numchains
- Elf32_Sym* symbol=&symtab[i];
- if(Hash(&strtab[symbol->st_name])==hash)
- return (void*)symbol->st_value;
- }
- return 0;
- }
- void* GetSymbol(unsigned long hash) {
- struct link_map* map=GetLinkMap();
- while(map!=0) {
- if(*(map->l_name)!=0) {
- void* ptr=FindSymbolInMap(map, hash);
- if(ptr!=0)
- return ptr+map->l_addr;
- }
- map=map->l_next;
- }
- return 0;
- }
- struct Imports {
- int (*SDL_Init)(unsigned int);
- SDL_Surface* (*SDL_SetVideoMode)(int, int, int, unsigned int);
- void (*SDL_Quit)(void);
- } imports;
- void _start() {
- imports.SDL_Init=(void*)GetSymbol(Hash("SDL_Init"));
- imports.SDL_SetVideoMode=(void*)GetSymbol(Hash("SDL_SetVideoMode"));
- imports.SDL_Quit=(void*)GetSymbol(Hash("SDL_Quit"));
- imports.SDL_Init(SDL_INIT_VIDEO);
- imports.SDL_SetVideoMode(1024, 768, 0, SDL_OPENGL);
- imports.SDL_Quit();
- asm("int\t$0x80\n\t"::"a"(SYS_exit), "b"(0));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement