From: Nikhil M. <mit...@gm...> - 2010-08-07 11:08:06
|
Hi, My program does not give any errors and the program is executed till the end. But the ELF file given as an argument to my program is not modified. In other words, the ELF file should have been modified after my program adds a new section into the ELF file. The argument file I created is a simple "Hello World" c program. I have verified the necessary file permissions too. Here is the complete program: ------------------------------------------------- int main (int argc , char ** argv) { int i, fd; Elf* e; char* k, id, name; Elf_Kind ek; Elf32_Ehdr ehdr; Elf_Scn* scn; Elf_Data* data; Elf32_Shdr* shdr; size_t sh_size; int sh_num; uint32_t hash_words[] = { 0x01234567 }; if (argc != 2) { errx(EX_USAGE , "usage: %s filename", argv[0]); } if (elf_version(EV_CURRENT) == EV_NONE) { errx(EX_SOFTWARE, "ELF library initialization failed : %s", elf_errmsg(-1)); } printf("\n ELF Version : %d\n", EV_CURRENT); if ((fd = open (argv[1] , O_RDWR, 0)) < 0) { err(EX_NOINPUT, "open %s \" failed", argv [1]); } if ((e = elf_begin(fd, ELF_C_RDWR, NULL)) == NULL) { errx(EX_SOFTWARE , "elf_begin() failed : %s .", elf_errmsg(-1)); } ek = elf_kind(e); switch (ek) { case ELF_K_AR : k = "ar (1) archive" ; break ; case ELF_K_ELF : k = "elf object" ; break ; case ELF_K_NONE : k = "data" ; break ; default : k = "unrecognized" ; } printf("%s : %s \n", argv [1], k); if (elf32_getehdr(e) == NULL ) { errx(EX_SOFTWARE, " getehdr () failed : %s . ", elf_errmsg(-1)); } scn = NULL; if((scn = elf_newscn(e)) == NULL) { errx(EX_SOFTWARE , " newscn () failed : %s . ", elf_errmsg(-1)); } if((data = elf_newdata(scn)) == NULL) { errx(EX_SOFTWARE , " newdata () failed : %s . ", elf_errmsg(-1)); } data->d_align = 4; data->d_off = 0LL; data->d_buf = hash_words; data->d_type = ELF_T_WORD; data->d_size = sizeof(hash_words); data->d_version = EV_CURRENT; if((shdr = elf32_getshdr(scn)) == NULL) { errx(EX_SOFTWARE , " elf32_getshdr () failed : %s . ", elf_errmsg(-1)); } shdr->sh_name = 9999; shdr->sh_type = SHT_HASH; shdr->sh_flags = SHF_ALLOC; shdr->sh_entsize = 0; if (elf_update(e, ELF_C_NULL) < 0) { errx(EX_SOFTWARE , " elf_update () failed : %s . ", elf_errmsg(-1)); } (void) elf_flagshdr(scn, ELF_C_SET, ELF_F_DIRTY); (void) elf_flagscn(scn, ELF_C_SET, ELF_F_DIRTY); (void) elf_flagdata(data, ELF_C_SET, ELF_F_DIRTY); (void) elf_flagehdr(e, ELF_C_SET, ELF_F_DIRTY); (void) elf_flagelf(e, ELF_C_SET, ELF_F_DIRTY); if (elf_update(e, ELF_C_WRITE) < 0) { errx(EX_SOFTWARE , " elf_update () failed : %s . ", elf_errmsg(-1)); } elf_end(e); close(fd); exit(EX_OK); } -------------------------------------------------------------------------------------------------- - Nikhil On Sat, Aug 7, 2010 at 1:02 PM, Joseph Koshy <jk...@us...>wrote: > > Based on tutorial at "http://sourceforge.net/apps/trac/elftoolchain/" > > I wrote a program to create a new section into an existing ELF file. > > > My program is not writing out the changes into the ELF file. > > > Here is the snippet starting from the creation of new section till end of > > file: > > [snip] > > > if (elf_update(e, ELF_C_WRITE) < 0) > > { > > errx(EX_SOFTWARE , " elf_update () failed : %s . ", elf_errmsg(-1)); > > } > > There isn't enough information in your post to formulate a useful > reply. For example, your post does not indicate where precisely your > program fails, and what the error message at that point is. > > I personally have found the following article to be a good guide to > framing questions: > > http://catb.org/esr/faqs/smart-questions.html > > Koshy > |