file mapping works

This commit is contained in:
mykola2312 2024-08-25 08:30:21 +03:00
parent bc2f89b039
commit 9e80bee9aa
3 changed files with 31 additions and 11 deletions

View file

@ -4,7 +4,9 @@
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/mman.h>
#include <string.h> #include <string.h>
#include <errno.h>
relf_value_t relf_open(relf_t* relf, const char* path) relf_value_t relf_open(relf_t* relf, const char* path)
{ {
@ -13,12 +15,13 @@ relf_value_t relf_open(relf_t* relf, const char* path)
// try stat file // try stat file
struct stat st = {0}; struct stat st = {0};
if (stat(path, &stat)) if (stat(path, &st))
return RELF_ERROR(RELF_FAILED_OPEN); return RELF_ERROR(RELF_FAILED_OPEN);
TRACE("st_size %lu\n", st.st_size);
// open file and read ELF header // open file and read ELF header
relf->fd = open(path, O_RDONLY); int fd = open(path, O_RDONLY);
if (relf->fd < 0) if (fd < 0)
return RELF_ERROR(RELF_FAILED_OPEN); return RELF_ERROR(RELF_FAILED_OPEN);
union { union {
@ -27,9 +30,9 @@ relf_value_t relf_open(relf_t* relf, const char* path)
} e; } e;
// read biggest value by default // read biggest value by default
if (read(relf->fd, &e.hdr64, sizeof(e.hdr64)) < sizeof(e.hdr64)) if (read(fd, &e.hdr64, sizeof(e.hdr64)) < sizeof(e.hdr64))
{ {
close(relf->fd); close(fd);
return RELF_ERROR(RELF_FAILED_OPEN); return RELF_ERROR(RELF_FAILED_OPEN);
} }
@ -38,7 +41,7 @@ relf_value_t relf_open(relf_t* relf, const char* path)
if (!memcmp(e.hdr64.e_ident, ELFMAG, sizeof(ELFMAG))) if (!memcmp(e.hdr64.e_ident, ELFMAG, sizeof(ELFMAG)))
{ {
// not an ELF file at all // not an ELF file at all
close(relf->fd); close(fd);
return RELF_ERROR(RELF_NOT_AN_ELF); return RELF_ERROR(RELF_NOT_AN_ELF);
} }
@ -48,14 +51,28 @@ relf_value_t relf_open(relf_t* relf, const char* path)
case ELFCLASS32: relf->type = RELF_32BIT; break; case ELFCLASS32: relf->type = RELF_32BIT; break;
case ELFCLASS64: relf->type = RELF_64BIT; break; case ELFCLASS64: relf->type = RELF_64BIT; break;
default: default:
close(relf->fd); close(fd);
return RELF_ERROR(RELF_UNSUPPORTED); return RELF_ERROR(RELF_UNSUPPORTED);
} }
if (e.hdr64.e_ident[EI_DATA] != ELFDATA2LSB) if (e.hdr64.e_ident[EI_DATA] != ELFDATA2LSB)
{ {
// not little endian, we can't work with that // not little endian, we can't work with that
close(relf->fd); close(fd);
return RELF_ERROR(RELF_UNSUPPORTED); return RELF_ERROR(RELF_UNSUPPORTED);
} }
// we don't care about ABI, OS, machine type or ELF type,
// as long as we got little endian we're good to go, so
// let's map file to memory
relf->image = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
close(fd); // we can close file after mmap
// but still check for errors
if (relf->image == MAP_FAILED)
{
TRACE("mmap failed errno %d %s\n", errno, strerror(errno));
return RELF_ERROR(RELF_MMAP_FAILED);
}
return RELF_ERROR(RELF_OK);
} }

View file

@ -29,7 +29,6 @@ typedef enum {
// relf instance // relf instance
typedef struct { typedef struct {
int fd;
void* image; void* image;
// is it 64 or 32 bit mode // is it 64 or 32 bit mode

View file

@ -1,8 +1,12 @@
#include "relf/relf.h" #include "relf/relf.h"
#include <stdio.h> #include <stdio.h>
int main() int main(int argc, char** argv)
{ {
printf("relf_test\n"); relf_t dummy;
relf_open(&dummy, argv[1]);
printf("image %p\n", dummy.image);
printf("type %u\n", dummy.type);
return 0; return 0;
} }