From: Eric S. <sch...@gm...> - 2011-10-25 18:33:01
|
Joseph Koshy <jk...@us...> writes: >>  $ ./extend-a-section hello >>  fstat() failed: "Bad address". >>  Size error: expected=118, elf_update()=6536 > >> I imagine that I broke something while replacing the TC_* macros >> with C code.  I apologize for asking such a simple question, but >> would you mind taking a look to see if the error is obvious to you. > > 1) The stat() call at line 46 appears to be failing because argv[2] > was not supplied. > > File "extend-a-section.c": > > 21 struct stat sb; > ... > 25 const char *srcfile = argv[1]; > 26 const char *reffile = argv[2]; > ... > 46 if (stat(reffile, &sb) < 0) { > 47 printf("fstat() failed: \"%s\".\n", strerror(errno)); > 48 } > > The value of the "sb.st_size" field would be undefined, and this > then leads to the failure further down. > > ... > 64 if (fsz1 != sb.st_size) { > 65 printf("Size error: expected=%ld, elf_update()=%ld\n", > sb.st_size, fsz1); > 66 } > > > You could remove these file size checks---they are only relevant for test code. > > 2) If your code is changing an ELF executable, it would also need to > adjust the ELF program header to match the new layout of the file's > content---in the ELF(3) API, the contents of the PHDR table are > managed by the application, not libelf. > > Regards, > Koshy Thank you for the advice. I am now able to read, modify and write an elf file without corruption. Specifically, using the attached script I can change the last byte the .text section (a padding byte which is of no consequence) of a hello world file. I can then write the modified file out to disk resulting in a new valid elf file which is different from the original but still executes successfully. Thanks for the help -- Eric |