![]() |
| |||||||
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #16 |
| Registered User Join Date: Sep 2009
Posts: 10
| |
| redone is offline | |
| | #17 |
| mastering the obvious Join Date: Jul 2008 Location: SE Queens
Posts: 5,131
| I think we all agree on this point. |
| MK27 is offline | |
| | #18 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,657
| You open the file, you read some data, you compare. If you're dickering about with modifying an executable and you don't know how to do this basic stuff, then chances are you're going to screw it up. Post some effort, and stop trying to get others to do the basics as well. |
| Salem is offline | |
| | #19 | |
| Guest Join Date: Aug 2001
Posts: 5,020
| Quote:
| |
| Sebastiani is offline | |
| | #20 | |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,776
| Quote:
| |
| tabstop is offline | |
| | #21 |
| Registered User Join Date: Sep 2009
Posts: 10
| here the my code source : Code: /*
* Print the names of ELF sections.
*/
#include <err.h>
#include <fcntl.h>
#include <gelf.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <sysexits.h>
#include <unistd.h>
#include <inttypes.h>
//#include <vis.h>
char *a="fggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg";
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;
size_t n, shstrndx, sz;
char c[1000];
/* if (argc != 2)
errx(EX_USAGE, "usage: %s file-name", getprogname());
*/
if (elf_version(EV_CURRENT) == EV_NONE)
errx(EX_SOFTWARE, "ELF library initialization failed: %s",
elf_errmsg(-1));
if ((fd = open(argv[1], O_RDONLY, 0)) < 0)
err(EX_NOINPUT, "open \%s\" failed", argv[1]);
if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL)
errx(EX_SOFTWARE, "elf_begin() failed: %s.",
elf_errmsg(-1));
if (elf_kind(e) != ELF_K_ELF)
errx(EX_DATAERR, "%s is not an ELF object.", argv[1]);
if (elf_getshstrndx(e, &shstrndx) == 0)
errx(EX_SOFTWARE, "getshstrndx() failed: %s.",
elf_errmsg(-1));
scn = NULL;
while ((scn = elf_nextscn(e, scn)) != NULL) {
if (gelf_getshdr(scn, &shdr) != &shdr)
errx(EX_SOFTWARE, "getshdr() failed: %s.",
elf_errmsg(-1));
if ((name = elf_strptr(e, shstrndx, shdr.sh_name)) == NULL)
errx(EX_SOFTWARE, "elf_strptr() failed: %s.",
elf_errmsg(-1));
if(strcmp(name,".data")==0)
{
data=elf_getdata(scn,NULL);
printf("%zd ",data->d_size);
//printf("%d ",(void *)data->d_off);
p = (char *) data->d_buf;
printf("%s",c);
printf("%p \n",p);
}
(void) printf("Section %-4.4jd %s\n", (uintmax_t) elf_ndxscn(scn),
name);
}
if ((scn = elf_getscn(e, shstrndx)) == NULL)
errx(EX_SOFTWARE, "getscn() failed: %s.",
elf_errmsg(-1));
if (gelf_getshdr(scn, &shdr) != &shdr)
errx(EX_SOFTWARE, "getshdr(shstrndx) failed: %s.",
elf_errmsg(-1));
(void) printf(".shstrab: size=%jd\n", (uintmax_t) shdr.sh_size);
/* data = NULL; n = 0;
while (n < shdr.sh_size && (data = elf_getdata(scn, data)) != NULL) {
p = (char *) data->d_buf;
while (p < (char *) data->d_buf + data->d_size) {
if (vis(pc, *p, VIS_WHITE, 0))
printf("%s", pc);
n++; p++;
(void) putchar((n % 16) ? ' ' : '\n');
}
}
(void) putchar('\n');*/
(void) elf_end(e);
(void) close(fd);
exit(EX_OK);
}
|
| redone is offline | |
| | #22 | |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,657
| > data=elf_getdata(scn,NULL); Quote:
FWIW, your "fggg" thing isn't in the data segment in all likelihood, it is a string constant. | |
| Salem is offline | |
| | #23 |
| Registered User Join Date: Sep 2009
Posts: 10
| the while loop before the bold text loops through all the sections and then i check for the section whose name is ".data"when i call data=elf_getdata(scn,NULL); and i print the data->d_buf i get a address in hexadecimal I think this the begining address of the global variable .If I'm right Iwant to read from his memory location and this is what I m unable to do .plz help me |
| redone is offline | |
| | #24 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,657
| a) there is potentially more than one global variable (so a loop within the section). You're not the only one with global data you know, parts of the standard libraries have them too. b) like I already mentioned, string constants MAY not be in the data section, but in .rodata. Try char a[] = "ffffgggg"; and see if you can find that. |
| Salem is offline | |
| | #25 |
| Registered User Join Date: Sep 2009
Posts: 10
| Code: #include <err.h>
#include <fcntl.h>
#include <gelf.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <sysexits.h>
#include <unistd.h>
#include <inttypes.h>
//#include <vis.h>
char *a="fggg";
int b=4;
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;
size_t n, shstrndx, sz;
char c[1000];
/* if (argc != 2)
errx(EX_USAGE, "usage: %s file-name", getprogname());
*/
if (elf_version(EV_CURRENT) == EV_NONE)
errx(EX_SOFTWARE, "ELF library initialization failed: %s",
elf_errmsg(-1));
if ((fd = open(argv[1], O_RDONLY, 0)) < 0)
err(EX_NOINPUT, "open \%s\" failed", argv[1]);
if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL)
errx(EX_SOFTWARE, "elf_begin() failed: %s.",
elf_errmsg(-1));
if (elf_kind(e) != ELF_K_ELF)
errx(EX_DATAERR, "%s is not an ELF object.", argv[1]);
if (elf_getshstrndx(e, &shstrndx) == 0)
errx(EX_SOFTWARE, "getshstrndx() failed: %s.",
elf_errmsg(-1));
scn = NULL;
while ((scn = elf_nextscn(e, scn)) != NULL) {
if (gelf_getshdr(scn, &shdr) != &shdr)
errx(EX_SOFTWARE, "getshdr() failed: %s.",
elf_errmsg(-1));
if ((name = elf_strptr(e, shstrndx, shdr.sh_name)) == NULL)
errx(EX_SOFTWARE, "elf_strptr() failed: %s.",
elf_errmsg(-1));
if(strcmp(name,".data")==0)
{
data=elf_getdata(scn,NULL);
printf("%zd ",data->d_size);
//printf("%d ",(void *)data->d_off);
p = (char *) data->d_buf;
printf("%p \n",p);
}
if(strcmp(name,".rodata")==0)
{
data=elf_getdata(scn,NULL);
printf("%zd ",data->d_size);
p = (char *) data->d_buf;
printf("%p \n",p);
}
}
(void) elf_end(e);
(void) close(fd);
exit(EX_OK);
}
redone@ubuntu:~/Bureau$ ./sec sec 224 0xb7f29c68 16 0xb7f2a04c I think this is the size and the offset of respectivly the .data and the .rodata Still I m unable to read the content of this memory location and its size If anyone knows how to do that plz help me |
| redone is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Writing and modifying data in a file | Micko | C Programming | 2 | 02-17-2005 03:42 AM |