Commit 88d1eb64 authored by Léo Grange's avatar Léo Grange

memory areas now handle partially 0-filled areas from file mapping

parent eec955c1
......@@ -205,11 +205,11 @@ int elfloader_load_segment(struct file *filep, void *offset,
filep->count ++;
area->flags = MEM_AREA_TYPE_FILE;
area->file.filep = filep;
area->file.origin = ph->offset;
area->file.base_offset = ph->offset;
area->max_size = ph->memsz;
// allow to fill with 0 any 0-initialized sections (like .bss)
area->file.infile_size = ph->filesz;
area->address = offset + ph->vaddr;
// FIXME area between filesz and memsz is not 0-filled as it should be!
mem_area_insert(dest, area);
......
......@@ -3,6 +3,7 @@
#include <utils/pool_alloc.h>
#include <fs/vfs_file.h>
#include <utils/log.h>
#include <utils/strutils.h>
// pool allocation data
......@@ -102,18 +103,41 @@ int mem_area_copy_raw(struct mem_area *area, size_t offset, void *dest, size_t s
// file mapped to memory, for now use a generic way to handle them
// TODO improve this with map_area_ops struct to allow "override"
size_t nbread;
size_t readsize = size;
// fill with 0 if needed
if(area->file.infile_size < offset + size) {
size_t zeroed_offset;
size_t zeroed_size;
// partially
if(area->file.infile_size > offset) {
readsize = area->file.infile_size - offset;
zeroed_offset = readsize;
zeroed_size = (offset + size) - area->file.infile_size;
}
else {
zeroed_offset = 0;
zeroed_size = size;
readsize = 0;
}
vfs_lseek(area->file.filep, area->file.origin + offset, SEEK_SET);
nbread = vfs_read(area->file.filep, dest, size);
ret = nbread == size ? 0 : -1;
if(ret) {
printk(LOG_ERR, "mem_area: failed loading %d bytes from offset 0x%x"
" [absolute 0x%x] (read returns %d)\n",
size, offset, area->file.origin + offset, nbread);
memset(dest + zeroed_offset, 0, zeroed_size);
}
else {
printk(LOG_DEBUG, "mem_area: loaded %d bytes @%p from file\n", size, dest);
if(readsize > 0) {
vfs_lseek(area->file.filep, area->file.base_offset + offset, SEEK_SET);
nbread = vfs_read(area->file.filep, dest, readsize);
ret = nbread == readsize ? 0 : -1;
if(ret) {
printk(LOG_ERR, "mem_area: failed loading %d bytes from offset 0x%x"
" [absolute 0x%x] (read returns %d)\n",
readsize, offset, area->file.base_offset + offset, nbread);
}
else {
printk(LOG_DEBUG, "mem_area: loaded %d bytes @%p from file\n", readsize, dest);
}
}
//print_memory(LOG_DEBUG, dest, nbread);
......
......@@ -34,7 +34,11 @@ struct mem_area {
struct file *filep;
// origin's position in the file
size_t origin;
size_t base_offset;
// size of the data effectively found in file, if not equals to
// max_size, bytes between them *should* be initialized as 0!
size_t infile_size;
} file;
struct {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment