Link to home
Start Free TrialLog in
Avatar of rosemthomas
rosemthomas

asked on

How to find the entry point of an executable file

Hi,

   I have an executable file and have to find it entry point from my C code.

Thanks,
Ros
Avatar of sunnycoder
sunnycoder
Flag of India image

You have to know the format of the executable before hand. All executable types have a well defined format which helps enumerating the point from where execution has to begin. What is the format of your executable file?
Avatar of rosemthomas
rosemthomas

ASKER


The format of the executable file that I have is elf32-littlemips

TIA,
Ros
download the ELF format from www.wotsit.org and look for E_ENTRY

  The down load contains one postscript file .How can we use that post script file from a C code to find entry point?
The postscript file is a document which has format specification for the ELF file. Open it using psview on linux or ghostview on windows. Or convert it to pdf using a tool like ps2pdf and then view it using acrobat reader etc.
Hi rosemthomas,
See if you have the "elfdump" utility installed. It will tell you all about your file structure and the symbol table.

Cheers!

Stefan
Is your code running in the SAME program, or is the program some external FILE, or is the program running in it's own separate process space?    Answers are going to vary a lot depending on that info.

rosemthomas,
Here's a nice and simple example of calling another binary from within Solaris (>= 2.7):

client.c:
#include <stdio.h>

int main(){
        printf("Client says: Hello, World!\n");

        return 0;
}

server.c:
#include <stdio.h>
#include <dlfcn.h>
#include <link.h>
#include <stdlib.h>

typedef int (*main_ptr)();

int main(){
    void *client;
    main_ptr client_main;
    printf("Server: Calling dlopen()\n");
    client=dlopen("./client", RTLD_NOW | RTLD_LOCAL);
    if(!client){
        fprintf(stderr,"dlopen() failure: ");
        perror(dlerror());
        exit(2);
    }
   
    printf("Server: Calling dlsym()\n");
    client_main=(main_ptr)dlsym(client,"main");
    if(!client_main){
        fprintf(stderr,"dlsym() failure: ");
        perror(dlerror());
        exit(2);
    }
   
    printf("Server: Calling client's main()\n");
    client_main();
   
    printf("Server: Unloading client\n");
    dlclose(client);
   
    return 0;
}

Compile both:
cc -xarch=native64 -xcode=abs32 server.c -o server -ldl
cc -xarch=native64 client.c -o client

and, voilà - it works:

> ./server
Server: Calling dlopen()
Server: Calling dlsym()
Server: Calling client's main()
Client says: Hello, World!
Server: Unloading client
ASKER CERTIFIED SOLUTION
Avatar of stefan73
stefan73
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Rosemthomas,

What do you need to do once you have the entry point?

- If you want to run the exe, then fork()/exec() is the straightforward approach.
I suppose that is not what you want.