|
From: Joseph K. <jk...@us...> - 2020-06-21 20:42:49
|
> I'm trying to modify the data of a global > variable in a C file. I was able to write > data to the .data section but I had two > problems. The first one is that when I > open the edited hex file I notice that my > .data section has shifted from 0x1030(where > my .data section starts in the elf file) > to ~0x0930. And the second problem is that > instead of overwriting in the address location > of my global variable, it writes the data in > the next memory address. I.E let's say the > global variable is defined at 0x1030 the new > data get written at 0x1031 instead of 0x1030. ... > if ((data = elf_newdata(scn)) == NULL) > errx(EXIT_FAILURE, "elf_newdata() failed: %s.", > elf_errmsg(-1)); This call to elf_newdata() would add new data to the section 'scn', changing its size and possibly causing the layout of the ELF object to change. If you would like to modify the existing content of the section without changing its size or layout, you could try the following: - read in the section's contents using elf_getdata(), - change the contents of the section (pointed to by the d_buf member of the Elf_Data descriptor returned by elf_getdata()), - set the ELF_F_DIRTY flag on the Elf_Data descriptor using elf_flagdata(), - call elf_update() as before. You might also want to set the ELF_F_LAYOUT flag on the Elf object, to turn off libelf's default layout rules. Regards, Joseph Koshy |