I modified my code a bit, I can display the .data section header correctly and 
the contents of the .data section, but I'm still not able to use the get_data() 
function properly.
I thought in order to modify the global variable it would be calling get_data() 
and set data-> some_string; but I get the error from my previous post.

U U U U \^@ \^@ \^@ \^@ \^@ \^@ \^@ \^@  \^@ \^@ \^@ \^@
*** Error in './prog4' : double free or corruption (out)
\M^Y \M^Y \M^Y \M^Y \^@ \^@ \^@ \^@ Aborted (core dumped)

if I comment out the data ->some_string; line as I have I currently have below, 
I get the following output

\^@ \^@ \^@ \^@ \^@ \^@ \^@ \^@  \^@ \^@ \^@ \^@ \^@  \^@ \^@ \^@ \^@
 \^Q \^Q \^Q \^Q \M-+ \M-+ \M-+ \M-+

The "U" character represents some_string[] = {0x55555555}; this is what I'm 
passing to the data -> d_buf = some_string;
The "M^Y" character represents the other global variable I have defined in 
prog4.c other_string[] = {0x99999999};
The "\^Q" and "\M-+" are the contents of the global variables from hello.c
uint32_t test1 =0x11111111;
uint32_t test1 =0xabababab; respectively

ideally what I want to see for the output is this:

\^@ \^@ \^@ \^@ \^@ \^@ \^@ \^@  \^@ \^@ \^@ \^@ \^@  \^@ \^@ \^@ \^@
 U U U U  \M-+ \M-+ \M-+ \M-+

I'm assuming since other_string will be ignored since it's not being passed in 
the d_buf parameter. Again, any suggestion is appreciated thanks.

#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;
    char *name, *p, pc[4*sizeof(char)];
    Elf_Scn *scn;
    Elf_Data *data;
    GElf_Shdr shdr;
    size_t n, shstrndx, sz;
    Elf *e;

    uint32_t some_string[1] = {0x55555555};
    uint32_t other_string[1] = {0x99999999};

    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 (elf_getshdrstrndx(e, &shstrndx) != 0)
errx(EXIT_FAILURE, "elf_getshdrstrndx() failed: %s.",
   elf_errmsg(-1));

scn = NULL;

while((scn = elf_nextscn(e, scn)) != NULL) {

if (gelf_getshdr(scn, &shdr) != &shdr)
errx(EXIT_FAILURE, "gelf_getshdr() failed: %s.",
           elf_errmsg(-1));

if ((name = elf_strptr(e, shstrndx, shdr.sh_name)) == NULL)
errx(EXIT_FAILURE, "elf_strptr() failed: %s.",
           elf_errmsg(-1));

    if (strcmp(name, ".data") == 0)
{
break;
        }

}

data = NULL; n = 0;

    while (n < shdr.sh_size && (data = elf_getdata(scn, data))!= NULL){
(void) printf("Section %-4.4jd %s\n", (uintmax_t)
elf_ndxscn(scn), name);

//data ->d_buf = some_string;

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');
}
}


    if(elf_update(e,ELF_C_NULL) < 0 )
errx(EXIT_FAILURE, "elf_update(NULL) 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(EXIT_FAILURE, "elf_update(NULL) failed: %s.",
   elf_errmsg(-1));

    if(elf_flagelf(e,ELF_C_SET, ELF_F_LAYOUT) < 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);
}


_______________________________________________
Elftoolchain-developers mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/elftoolchain-developers

Reply via email to