This is the mail archive of the
elfutils-devel@sourceware.org
mailing list for the elfutils project.
elf_rawdata failing when using ELF_C_READ
- From: Martin Storsjö <martin at martin.st>
- To: elfutils-devel at lists dot fedorahosted dot org
- Date: Tue, 10 Nov 2009 15:05:23 +0200
- Subject: elf_rawdata failing when using ELF_C_READ
Hi,
When using libelf from elfutils 0.143, the elf_rawdata function fails (and
elf_errmsg says "out of memory") when trying to get raw data using these
function calls:
int fd = open(elfinput, O_RDONLY);
Elf* elf = elf_begin(fd, ELF_C_READ, NULL);
size_t filesize = 0;
const char* rawdata = elf_rawfile(elf, &filesize);
(The full uncut example reproducing this bug is attached.)
As far as I can tell, this is due to elf->maximum_size being ~0 when this
call is made. If I change the flags given to elf_begin to ELF_C_READ_MMAP,
this works fine.
The same example above works just fine when using libelf by Michael
Riepe.
Is this a bug, or are there any prerequisites for calling elf_rawfile?
Kind regards,
// Martin Storsjö
#define _LARGEFILE64_SOURCE
#include <sys/types.h>
#include <libelf.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
const char* elfinput;
int fd;
Elf* elf;
size_t filesize;
const char* rawdata;
if (elf_version(EV_CURRENT) == EV_NONE) {
fprintf(stderr, "Elf library out of date\n");
return 1;
}
if (argc < 2) {
fprintf(stderr, "%s elffile\n", argv[0]);
return 1;
}
elfinput = argv[1];
fd = open(elfinput, O_RDONLY);
if (fd < 0) {
perror(elfinput);
return 1;
}
elf = elf_begin(fd, ELF_C_READ, NULL);
if (!elf) {
fprintf(stderr, "%s\n", elf_errmsg(elf_errno()));
close(fd);
return 1;
}
filesize = 0;
rawdata = elf_rawfile(elf, &filesize);
if (!rawdata) {
fprintf(stderr, "elf_rawfile failed: %s\n", elf_errmsg(elf_errno()));
return 1;
}
printf("rawdata: %p size: %d\n", rawdata, filesize);
elf_end(elf);
close(fd);
return 0;
}