need help in this code

Discussion of programming on Linux, including shell scripting, perl, python, c/c++, mono, java. Whatever tickles your fancy.
Post Reply
khan001
Cadet
Posts: 3
Joined: Sat Feb 05, 2011 10:56 pm

need help in this code

Post by khan001 »

i want to write a code which determines whether a file is executable or relocatable.....i have try this code but its now working ...


#include<stdio.h>
#include<elf.h>
int main()
{
int fd;
elf32_ehdr myhdr;
fd=open("abc.c");
if (myhdr.e_type==ET_RELl)
{
printf(:relocatable");
if (myhdr.e_type==ET_EXEC)
printf(executable");


else
printf(:other type");

return(0);
}
soni
Naik
Posts: 70
Joined: Sat Oct 04, 2003 1:44 pm
Location: Karachi
Contact:

Post by soni »

Hey, You are trying to open a C source code file...

Code: Select all

fd=open("abc.c"); 
you need a compiled file. Compile this file and then open it and read its header... relocate able files are shared objects imported/loaded by the dynamic linker when needed by the executable files. Under GNU/Linux both are ELF. A while ago(when I was studying ELF files), I did an application, it sort of works like readelf(a member of binutility package).

A snippet of a code (which determines if file is exe or shared) from one of its files..

Code: Select all

/* Display the file type */
   printf("File-Type                    = ");
   switch( ((ELF32_HDR *)elfHeader)->e_type ){
      case ET_NONE:
         printf("`No file type is given'\n");        
      break;
      case ET_REL:
         printf("`Relocateable/Object file'");
      break;
      case ET_EXEC:
         printf("`Executable file'");
      break;
      case ET_DYN:
         printf("`Shared object file'");
      break;
      case ET_CORE:
         printf("ET_CORE\n");
      break;
      default:
         printf("0x%x", ((ELF32_HDR *)elfHeader)->e_type);
      break;
you can download the complete source code of this application, it is at following address...

http://cquery.sf.net/cquery/alf.tar.bz2

Thankyou,
Please do not open M$Windoze.
Post Reply