From: silver k. <he...@ho...> - 2020-06-21 05:45:14
|
Hello all, 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. I am using ubuntu 14.04 I was having problems with the vis.h file so I had to use -lbsd and #include <bsd/vis.h> in my file. The elf file I'm using it's from my hello.c file. I compiled with gcc using gcc -o hello hello.c and my libelf program is prog4.c and I compiled using cc -o prog4 prog4.c -lelf -lbsd then I did ./prog4 hello I opened the new hello elf file with a hex editor called Bless that can be installed with sudo apt-get Bless this is my libelf program ********************************************************************************** #include <err.h> #include <fcntl.h> #include <gelf.h> #include <stdio.h> #include <stdint.h> #include <stdlib.h> #include <unistd.h> #include <bsd/vis.h> int main(int argc, char **argv) { int fd; Elf *e; char *name, *p, pc[4*sizeof(char)]; Elf_Scn *scn; Elf_Data *data; GElf_Shdr shdr; GElf_Sym sym; size_t n, shstrndx, sz; uint32_t some_string[] = {0xaf}; if (argc != 2) errx(EXIT_FAILURE, "usage: %s file-name", argv[0]); if (elf_version(EV_CURRENT) == EV_NONE) errx(EXIT_FAILURE, "ELF library initialization " "failed: %s", elf_errmsg(-1)); if ((fd = open(argv[1], O_RDWR, 0)) < 0) err(EXIT_FAILURE, "open \%s\" failed", argv[1]); if ((e = elf_begin(fd, ELF_C_RDWR, NULL)) == NULL) errx(EXIT_FAILURE, "elf_begin() failed: %s.", elf_errmsg(-1)); if (elf_kind(e) != ELF_K_ELF) errx(EXIT_FAILURE, "%s is not an ELF object.", argv[1]); if ((scn = elf_getscn(e, 24)) == NULL) errx(EXIT_FAILURE, "elf_scn() failed: %s.", elf_errmsg(-1)); if (gelf_getshdr(scn, &shdr) != &shdr) errx(EXIT_FAILURE, "getshdr(shstrndx) failed: %s.", elf_errmsg(-1)); if ((data = elf_newdata(scn)) == NULL) errx(EXIT_FAILURE, "elf_newdata() failed: %s.", elf_errmsg(-1)); data ->d_align = 1; data ->d_off = 0LL; data ->d_buf = some_string; data ->d_type = ELF_T_WORD; data ->d_size = sizeof(some_string); data ->d_version = EV_CURRENT; (void) printf(".data: size=%jd\n", (uintmax_t)shdr.sh_size); if(elf_update(e,ELF_C_NULL) < 0 ) errx(EXIT_FAILURE, "elf_update(NULL) failed: %s.", elf_errmsg(-1)); if(elf_update(e,ELF_C_WRITE) < 0 ) errx(EXIT_FAILURE, "elf_update(NULL) failed: %s.", elf_errmsg(-1)); (void) putchar('\n'); (void) elf_end(e); (void) close(fd); exit(EXIT_SUCCESS); } ******************************************************************************** and this is my hello world program ******************************************************************************** #include <stdio.h> #include <stdint.h> uint8_t test = 0xce; uint8_t tuna = 0xab; int main(){ printf("hello world\n"); return 0; } They are both very simple since I'm just testing. Any hints or suggestions are appreciated thanks. |