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;
- }
- void _start() {
- struct link_map* map=GetLinkMap();
- //step through the libraries
- while(map!=0) {
- printkeyandvalue_((long)map->l_name, map->l_addr); //l_addr is the value you'd get back from 'dlopen' with this name
- map=map->l_next;
- }
- asm("int\t$0x80\n\t"::"a"(SYS_exit), "b"(0));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement