mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "vamsi krishna" <vamsi.krishnak@gmail.com>
To: linux-kernel@vger.kernel.org
Subject: Re: Program to convert core file to executable.
Date: Wed, 24 May 2006 22:55:00 +0530	[thread overview]
Message-ID: <3faf05680605241025g722bf73dma0c0d1b3e0554e8b@mail.gmail.com> (raw)
In-Reply-To: <3faf05680605241018q302d5c0em6844765f81669498@mail.gmail.com>

> Hello All,
>
> o I have written the following program to convert a core file to a
> executable, and tried to execute the converted executable but my
> system __HANGED__, The kernel did'nt give any messages the complete
> system was stuck.
>
> o Theoretically , the OS loader should jump into the virtual address
> specified at 'ELF_HDR.e_entry and start executing instructions from
> that point if the ELF_TYPE is ET_EXEC.
>
> o So I wrote a program which
> changes ELF_TYPE form ET_CORE to ET_EXEC and modifies e_entry to
> virtual address of the 'main' symbol, since the core file has valid offset
> to access the PHDRS, and for ET_EXEC the elf loader just need to map
> the PHDRS at the vaddr specified and start jump to e_entry.
>
> o Is there anything I'am missing, can some experts throw light on why
> kernel does not load this program, could it be a bug in the kernel code ?
>
> o The following is the program which converts core file to executable,
> its simple to use just compile it with 'gcc convertcore.c -o
> convertcore' , run with 'convertcore <core-file-name> <new-exec-name>
> <vaddr-of-main>'
>
> o I dump the core by CRTL+\
>
> really appreciate your inputs
>
========================<BEGIN>===============================
#include<elf.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>

#ifndef __64_BIT__
#define __32_BIT__
#endif

#ifdef __32_BIT__
#define ELF_EHDR Elf32_Ehdr
#else
#define ELF_EHDR Elf64_Ehdr
#endif

ELF_EHDR place_holder;

/*Chages the elf_header in the file with ptr */
int ChangeElfHeader(int CoreFd, int WriteFd, unsigned long vaddr){

      unsigned long got_len=0;

      if((got_len = read(CoreFd,&place_holder,sizeof(ELF_EHDR)))
              != sizeof(ELF_EHDR)){
              perror("Unable to read the ELF Header::");
              exit(1);
      }
      /*Change the ET_CORE tto ET_EXEC*/
      if(place_holder.e_type == ET_CORE) {
              place_holder.e_type = ET_EXEC;
      } else {
              fprintf(stderr,"The file is not of ELF core file");
              exit(1);
      }

      /*Change the entry */

      place_holder.e_entry = vaddr;

      /*Write back the header*/
      got_len = 0;
      if (( got_len = write(WriteFd,&place_holder,sizeof(ELF_EHDR)))
              != sizeof(ELF_EHDR)) {
              perror("Unable to write the header::");
              exit(1);
      }
      return 1;
}

static void finishWriting(int coreFd, int writeFd) {

      unsigned char write_buffer[4*1024];
      int got_len = -1;

      while( (got_len = read(coreFd,write_buffer,4096)) != 0) {
              if(write(writeFd,write_buffer,got_len) != got_len ){
                      perror("Unable to to write the length which was read:");
                      exit(1);
              }
      }
      close(writeFd);
      close(coreFd);

}

int main(int argc,char* argv[]){

      int coreFd;
      int writeFd;
      unsigned long vaddr;

      if( argc < 3 ) {
              fprintf(stderr,"Usage core2elf core.file exe.file.name");
              exit(1);
      }
      if( (coreFd = open(argv[1],O_RDONLY)) < 0) {
              perror("Unable to open the core file:");
              exit(1);
      }
      if ((writeFd = open(argv[2],O_WRONLY| O_CREAT)) < 0) {
              perror("Unable to open the write file::");
              exit(1);
      }
      sscanf(argv[3],"%lx",&vaddr);
      ChangeElfHeader(coreFd,writeFd,vaddr);
      finishWriting(coreFd,writeFd);


}
=========================<END>===========================

Best Regards,
Vamsi kundeti.

  reply	other threads:[~2006-05-24 17:25 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-05-24 17:18 vamsi krishna
2006-05-24 17:25 ` vamsi krishna [this message]
2006-05-24 17:38 ` Daniel Jacobowitz
2006-05-24 20:06   ` vamsi krishna
2006-05-24 20:08     ` Daniel Jacobowitz
2006-05-24 20:19       ` vamsi krishna
2006-05-24 20:25         ` Daniel Jacobowitz
2006-05-24 20:31     ` H. Peter Anvin
2006-05-24 20:52     ` Eric Piel
2006-05-24 21:33     ` Greg KH
2006-05-25 11:04 ` Marcel Holtmann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3faf05680605241025g722bf73dma0c0d1b3e0554e8b@mail.gmail.com \
    --to=vamsi.krishnak@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®