This is the mail archive of the 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]

Re: libelf RDWR and elf_newscn do not work

On 10/09/2013 12:04 PM, Mark Wielaard wrote:
> On Tue, 2013-10-08 at 13:49 +0200, Jiri Slaby wrote:
>> On 10/08/2013 11:24 AM, Mark Wielaard wrote:
>>>> The attached patch fixed it for me. Any ideas?
>>> Not immediately. Would you mind posting the full code?
>>> In particular where does the Elf_Scn that you are adding come from?
>>> The code you posted seems incomplete since it only sets sh_type but
>>> nothing else.
>> It does not matter (the other members are set implicitly). With the
>> patch, it works.
>> Anyway, see the "#if 0" section in the attached file, even if I
>> uncomment it, it does not work.
> It is hard to see what you are actually trying to do since as given the
> program just crashes and it has several different sections #if 0 and/or
> commented out. I am not exactly sure which parts you do and do not
> intent to have enabled/uncommented or why.

It crashes maybe because you haven't passed an argument to the program
which is an arbitrary ELF?

It does not matter whether you uncomment something or not. The issue is
when I add a section to an existing ELF, the section header of that
section is not written to the ELF properly. Some random memory is
written to the section header to the file instead. The section content
(data) are written fine.

The fix is to always build all the section headers, because they might
have changed. Hence copying them all to a newly alloca'ed memory.

>>  See also the patch I attached earlier
>> what it does...
> The patch doesn't come with any explanation making it somewhat hard to
> know what you intended.
> So your patch changes the section write loop in "updatefile".
> It ignores the check to see whether the byte order changed or the shdr
> comes from reading a file. Then always allocas a new shdr_data instead
> of reusing the data from the file. Then for each section memcpy the data
> into the new shdr_data ignoring the existing the existing elf state shdr
> from the file.

Yes, because the state might have changed as can be demostrated by the
attached program (I removed the "#if 0" so yo ucan see it explicitly now).

> I assume in your case the number of sections changed and the current
> test doesn't take that into account? So you also want to reallocate the
> shdr in that case?


suse labs
#include <ctype.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <libelf.h>
#include <gelf.h>

#include <sys/types.h>
#include <sys/stat.h>

int main(int argc, char **argv)
	Elf *elf;
	Elf_Kind ek;
	Elf_Scn *scn;
	Elf_Data *data;
	int fd;

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

	ek = elf_kind(elf);
	if (ek != ELF_K_ELF)
		errx(EXIT_FAILURE, "not an ELF");

	scn = elf_newscn(elf);
	if (!scn)
		errx(EXIT_FAILURE, "elf_newscn: %s", elf_errmsg(-1));

	printf("new scn=%zd\n", elf_ndxscn(scn));

	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_PROGBITS;

	data->d_buf = "ahoj";
	data->d_type = ELF_T_BYTE;
	data->d_size = 5;
	data->d_align = 1;

	if (!gelf_update_shdr(scn, shdr))
		errx(1, "!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));

	return 0;

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