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);
}
need help in this code
Hey, You are trying to open a C source code file...
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..
you can download the complete source code of this application, it is at following address...
http://cquery.sf.net/cquery/alf.tar.bz2
Thankyou,
Code: Select all
fd=open("abc.c");
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;
http://cquery.sf.net/cquery/alf.tar.bz2
Thankyou,
Please do not open M$Windoze.