Advertisement
Guest User

Untitled

a guest
Dec 14th, 2008
515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.68 KB | None | 0 0
  1. //Use: gcc -s -lSDL -nostartfiles -nostdlib main.c
  2.  
  3. #include <dlfcn.h>
  4. #include <syscall.h>
  5. #include <fcntl.h>
  6. #include <elf.h>
  7. #include <stdio.h>
  8. #include <SDL/SDL.h>
  9. #include <link.h>
  10.  
  11. //****************************************************************************************
  12. //Some string functions for debug output.
  13. //****************************************************************************************
  14. long syscall3(long num, long arg1, long arg2, long arg3) {
  15.   long __res;
  16.   __asm__ volatile ("int $0x80":"=a"(__res):"0"(num), "b"(arg1), "c"(arg2), "d"(arg3));
  17.   return __res;
  18. }
  19.  
  20. void printhexstring_(unsigned int num) {
  21.   char string[9];
  22.   int i;
  23.   unsigned char digit;
  24.   for(i=0; i<8; i++) {
  25.     digit=num&0xf;
  26.     if(digit>9) digit+=('a'-10); else digit+='0';
  27.     string[7-i]=digit;
  28.     num>>=4;
  29.   }
  30.   string[8]='\n';
  31.  
  32.   long result=syscall3(SYS_write, 0, (long)string, 9);
  33. }
  34.  
  35. void printstring_(long string) {
  36.   long count=0;
  37.   char* p=(char*)string;
  38.   while(p[count]!='\0')
  39.     count++;
  40.  
  41.   long result=syscall3(SYS_write, 0, string, count+1);
  42. }
  43.  
  44. void printkeyandvalue_(long string, long value) {
  45.   printstring_((long)string);
  46.   printhexstring_((long)value);
  47. }
  48. //*****************************************************************************************
  49.  
  50.  
  51. const long baseAddress=0x08048000; //base address that the executable is loaded at
  52.  
  53. struct link_map* GetLinkMap() {
  54.   Elf32_Ehdr* ehdr=(Elf32_Ehdr*)baseAddress; //the elf header
  55.   Elf32_Phdr* phdr=(Elf32_Phdr*)((void*)((unsigned int)ehdr+(unsigned int)ehdr->e_phoff)); //the program header
  56.  
  57.   //find the dynamic section entry in the program header
  58.   while(phdr->p_type!=PT_DYNAMIC) {
  59.     phdr++;
  60.   }
  61.  
  62.   //get the dynamic section's address
  63.   Elf32_Dyn* dyn=(Elf32_Dyn*)phdr->p_vaddr;
  64.  
  65.   //find the debug entry
  66.   while(dyn->d_tag!=DT_DEBUG) {
  67.     dyn++;
  68.   }
  69.  
  70.   struct r_debug* debug=(struct r_debug*)dyn->d_un.d_ptr;
  71.   return debug->r_map;
  72. }
  73.  
  74. unsigned long GetDynamicSectionValue(struct link_map* map, long tag) {
  75.   Elf32_Dyn* dynamic=(Elf32_Dyn*)map->l_ld;
  76.  
  77.   while(dynamic->d_tag!=tag)
  78.     dynamic++;
  79.  
  80.   unsigned long address=(long)dynamic->d_un.d_ptr;
  81.  
  82.   if(address<map->l_addr)
  83.     address+=map->l_addr;
  84.  
  85.   return address;
  86. }
  87.  
  88. unsigned long Hash(unsigned char* str) {
  89.   unsigned long hash=0;
  90.   int c;
  91.  
  92.   while(c=*str++)
  93.     hash=((hash<<5)+hash)^c;
  94.  
  95.   return hash;
  96. }
  97.  
  98. void* FindSymbolInMap(struct link_map* linkMap, unsigned long hash) {
  99.   char* strtab=(char*)GetDynamicSectionValue(linkMap, DT_STRTAB);
  100.   Elf32_Sym* symtab=(Elf32_Sym*)GetDynamicSectionValue(linkMap, DT_SYMTAB);
  101.   unsigned long* hashtable=(unsigned long*)GetDynamicSectionValue(linkMap, DT_HASH);
  102.  
  103.   unsigned long i;
  104.   for(i=0; i<hashtable[1]; i++) { //hashtable[1] == numchains
  105.     Elf32_Sym* symbol=&symtab[i];
  106.     if(Hash(&strtab[symbol->st_name])==hash)
  107.       return (void*)symbol->st_value;
  108.   }
  109.  
  110.   return 0;
  111. }
  112.  
  113. void* GetSymbol(unsigned long hash) {
  114.   struct link_map* map=GetLinkMap();
  115.  
  116.   while(map!=0) {
  117.     if(*(map->l_name)!=0) {
  118.       void* ptr=FindSymbolInMap(map, hash);
  119.       if(ptr!=0)
  120.     return ptr+map->l_addr;
  121.     }
  122.     map=map->l_next;
  123.   }
  124.  
  125.   return 0;
  126. }
  127.  
  128. struct Imports {
  129.   int (*SDL_Init)(unsigned int);
  130.   SDL_Surface* (*SDL_SetVideoMode)(int, int, int, unsigned int);
  131.   void (*SDL_Quit)(void);
  132. } imports;
  133.  
  134. void _start() {
  135.   imports.SDL_Init=(void*)GetSymbol(Hash("SDL_Init"));
  136.   imports.SDL_SetVideoMode=(void*)GetSymbol(Hash("SDL_SetVideoMode"));
  137.   imports.SDL_Quit=(void*)GetSymbol(Hash("SDL_Quit"));
  138.  
  139.   imports.SDL_Init(SDL_INIT_VIDEO);
  140.   imports.SDL_SetVideoMode(1024, 768, 0, SDL_OPENGL);
  141.   imports.SDL_Quit();
  142.  
  143.   asm("int\t$0x80\n\t"::"a"(SYS_exit), "b"(0));
  144. }
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement