Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 399
  • Last Modified:

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
0
rosemthomas
Asked:
rosemthomas
  • 3
  • 3
  • 2
  • +2
1 Solution
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now