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;
- };
- void DumpSymbols(struct link_map* linkMap) {
- 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];
- printkeyandvalue_((long)&strtab[symbol->st_name], symbol->st_value); //st value is what you'd get back from 'dlsym' with this name
- }
- }
- 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
- printstring_((long)"*******************************************************************\n");
- if(*map->l_name!=0) {
- DumpSymbols(map);
- }
- 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