This is the mail archive of the elfutils-devel@sourceware.org mailing list for the elfutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

libelf RDWR and elf_newscn do not work


Hi,

if I add a section using the code below, I obtain a section like this:
  [29]                   NULL            0000000000000000
fff1000400000001 000000 00      0   0 281487861612544

The attached patch fixed it for me. Any ideas?

Code:
        if (elf_version(EV_CURRENT) == EV_NONE)
                errx(1, "elf_version: %s" , elf_errmsg(-1));

        fd = open(argv[1], O_RDWR, 0);
        if (fd < 0)
                err(1, "open");

        elf = elf_begin(fd, ELF_C_RDWR, NULL);
        if (!elf)
                errx(EXIT_FAILURE, "elf_begin: %s", elf_errmsg(-1));

        data = elf_newdata(scn);
        if (!data)
                errx(1, "!elf_newdata: %s", elf_errmsg(-1));

        GElf_Shdr shdr_data, *shdr;
        shdr = gelf_getshdr(scn, &shdr_data);
        if (!shdr)
                errx(1, "gelf_getshdr: %s", elf_errmsg(-1));

        shdr->sh_type = SHT_NOBITS;

        if (!gelf_update_shdr(scn, shdr))
                errx(EXIT_FAILURE, "gelf_update_shdr: %s", elf_errmsg(-1));

        if (elf_update(elf, ELF_C_NULL) < 0)
                errx(EXIT_FAILURE, "elf_update1: %s", elf_errmsg(-1));

        if (elf_update(elf, ELF_C_WRITE) < 0)
                errx(EXIT_FAILURE, "elf_update2: %s", elf_errmsg(-1));

        if (elf_end(elf))
                errx(EXIT_FAILURE, "elf_end: %s", elf_errmsg(-1));
        close(fd);

thanks,
-- 
js
suse labs
---
 libelf/elf32_updatefile.c |    8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

--- a/libelf/elf32_updatefile.c
+++ b/libelf/elf32_updatefile.c
@@ -632,12 +632,8 @@ __elfw2(LIBELFBITS,updatefile) (Elf *elf
 # define shdr_fctp __elf_xfctstom[0][EV_CURRENT - 1][ELFW(ELFCLASS, LIBELFBITS) - 1][ELF_T_SHDR]
 #endif
 
-      ElfW2(LIBELFBITS,Shdr) *shdr_data;
-      if (change_bo || elf->state.ELFW(elf,LIBELFBITS).shdr == NULL)
-	shdr_data = (ElfW2(LIBELFBITS,Shdr) *)
+      ElfW2(LIBELFBITS,Shdr) *shdr_data = (ElfW2(LIBELFBITS,Shdr) *)
 	  alloca (shnum * sizeof (ElfW2(LIBELFBITS,Shdr)));
-      else
-	shdr_data = elf->state.ELFW(elf,LIBELFBITS).shdr;
       int shdr_flags = elf->flags;
 
       /* Get all sections into the array and sort them.  */
@@ -764,7 +760,7 @@ __elfw2(LIBELFBITS,updatefile) (Elf *elf
 	    (*shdr_fctp) (&shdr_data[scn->index],
 			  scn->shdr.ELFW(e,LIBELFBITS),
 			  sizeof (ElfW2(LIBELFBITS,Shdr)), 1);
-	  else if (elf->state.ELFW(elf,LIBELFBITS).shdr == NULL)
+	  else
 	    memcpy (&shdr_data[scn->index], scn->shdr.ELFW(e,LIBELFBITS),
 		    sizeof (ElfW2(LIBELFBITS,Shdr)));
 

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]