Save memory allocation in the elf loader
PATCH 1/1 Save memory allocation in the elf loader
The current elf loader uses too much memory. For example, I have a executable with a bss section of 400 MB and I set the ram size to 512 MB. Qemu uses about 780MB of RAM (which is fine), but there's a peak at 1.6 GB during initialization (this is not fine).
This patch fixes two things:
1) do not allocate each elf program twice.
2) do not allocate memory for areas that are only zeros.
For this we need a new field in Rom: "datasize" which is the size of the allocated data. If datasize is less than romsize, it means that the area from datasize to romsize is filled with zeros.
Signed-off-by Fabien Chouteau <[email protected]>
--- hw/elf_ops.h | 18 ++++++++------ hw/loader.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++----------- hw/loader.h | 2 + 3 files changed, 72 insertions(+), 22 deletions(-)
hw/elf_ops.h +10 -8
@@ -199,7 +199,7 @@ static int glue(load_elf, SZ)(const char *name, int fd,struct elfhdr ehdr;struct elf_phdr *phdr =NULL , *ph;int size, i, total_size; -elf_word mem_size; +elf_word mem_size, file_size;uint64_t addr, low = (uint64_t )-1 , high =0 ;uint8_t *data =NULL ;char label[128 ];
@@ -254,14 +254,16 @@ static int glue(load_elf, SZ)(const char *name, int fd,for (i =0 ; i < ehdr.e_phnum; i++) { ph = &phdr[i];if (ph->p_type ==PT_LOAD ) { - mem_size = ph->p_memsz; -/* XXX: avoid allocating */ - data =g_malloc0 (mem_size); + mem_size = ph->p_memsz;/* Size of the ROM */ + file_size = ph->p_filesz;/* Size of the allocated data */ + data =g_malloc0 (file_size);if (ph->p_filesz >0 ) { -if (lseek (fd, ph->p_offset,SEEK_SET ) <0 ) +if (lseek (fd, ph->p_offset,SEEK_SET ) <0 ) { goto fail; -if (read (fd, data, ph->p_filesz) != ph->p_filesz) + } +if (read (fd, data, file_size) != file_size) { goto fail; + } }/* address_offset is hack for kernel images that are linked at the wrong physical address. */
@@ -272,7 +274,8 @@ static int glue(load_elf, SZ)(const char *name, int fd, }snprintf (label,sizeof (label),"phdr #%d: %s" , i, name); -rom_add_blob_fixed (label, data, mem_size, addr); + +rom_add_elf_program (label, data, file_size, mem_size, addr); total_size += mem_size;if (addr < low)
@@ -280,7 +283,6 @@ static int glue(load_elf, SZ)(const char *name, int fd,if ((addr + mem_size) > high) high = addr + mem_size; -g_free (data); data =NULL ; } }
hw/loader.c +60 -14
124 lines not on this page · raw message
hw/loader.h +2 -0
@@ -25,6 +25,8 @@ int rom_add_file(const char *file, const char *fw_dir, target_phys_addr_t addr, int32_t bootindex);int rom_add_blob (const char *name,const void *blob,size_t len,target_phys_addr_t addr); +int rom_add_elf_program (const char *name,void *data,size_t datasize, +size_t romsize,target_phys_addr_t addr);int rom_load_all (void );void rom_set_fw (void *f);int rom_copy (uint8_t *dest,target_phys_addr_t addr,size_t size);
Signature · 1 line
1.7.4.1