• C

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
rosemthomasAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sunnycoderCommented:
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?
0
rosemthomasAuthor Commented:

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

TIA,
Ros
0
sunnycoderCommented:
download the ELF format from www.wotsit.org and look for E_ENTRY
0
Top Threats of Q1 & How to Defend Against Them

WEBINAR: Join WatchGuard CTO and our Threat Research Team on Aug. 2nd to hear the findings from our Q1 Internet Security Report! Learn more about the top threats detected in the first quarter and how you can defend your business against them!

rosemthomasAuthor Commented:

  The down load contains one postscript file .How can we use that post script file from a C code to find entry point?
0
sunnycoderCommented:
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.
0
stefan73Commented:
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
0
grg99Commented:
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.

0
stefan73Commented:
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
0
stefan73Commented:
Another interesting one is a lookup with with dladdr() at the loader's base address:

#include <stdio.h>
#include <dlfcn.h>
#include <link.h>
#include <stdlib.h>

int main(){
     Dl_info dinfo;
     int res;
     void* solaris_v8_loader_base = (void*) 0x10000;
     res=dladdr(solaris_v8_loader_base,&dinfo);
     
    return 0;
}


Say in the debugger:


dinfo = {
    dli_fname = 0xffbeffb8 "/mnusers/mn/csm/csmdev4/user/stefans/other_stuff/dlopen/lookup_main"
    dli_fbase = 0x10000
    dli_sname = 0x103ef "__fsr_init_value"
    dli_saddr = (nil)
}

The loader base address can be modified with linker parameters.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ravs120499Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.