Advertisement
Guest User

Untitled

a guest
Dec 14th, 2008
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.24 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. void _start() {
  75.   struct link_map* map=GetLinkMap();
  76.  
  77.   //step through the libraries
  78.   while(map!=0) {
  79.     printkeyandvalue_((long)map->l_name, map->l_addr); //l_addr is the value you'd get back from 'dlopen' with this name
  80.     map=map->l_next;
  81.   }
  82.  
  83.   asm("int\t$0x80\n\t"::"a"(SYS_exit), "b"(0));
  84. }
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement