macsim/src/memory.c
// Load little endian ELF binary into memory
Load little endian ELF binary into memory
Load little endian ELF binary into memory
void memory_load_elf(node_t *node, unsigned et, uint8_t *buf) 
{
    uint_fast64_t i;
    node->pc            = ELF_GET_EH(et, buf, e_entry);
    uint_fast64_t phnum = ELF_GET_EH(et, buf, e_phnum);
    for (i=0; i<phnum; i++) {
        uint_fast64_t phoff     = ELF_GET_EH(et, buf, e_phoff);
        uint_fast64_t phentsize = ELF_GET_EH(et, buf, e_phentsize);
        uint8_t *ph = buf + phoff + i*phentsize;
        uint_fast64_t type   = ELF_GET_PH(et, ph, p_type);
        uint_fast64_t filesz = ELF_GET_PH(et, ph, p_filesz);
        uint_fast64_t paddr  = ELF_GET_PH(et, ph, p_paddr);
        uint_fast64_t offset = ELF_GET_PH(et, ph, p_offset);
        uint_fast64_t memsz  = ELF_GET_PH(et, ph, p_memsz);
        if (type == PT_LOAD) { 
            if (filesz > 0)
                memory_write(node, paddr, buf+offset, filesz);
            if (filesz < memsz) {
                uint8_t *zeros = (uint8_t *)fatal_malloc(memsz-filesz);
                memset(zeros, 0, memsz-filesz);
                memory_write(node, paddr+filesz, zeros, memsz-filesz);
                free(zeros);
            }
        }
    }
}
void memory_load_elf ( node_t * node , unsigned et , uint8_t * buf ) { uint_fast64_t i ; node -> pc = ELF_GET_EH ( et , buf , e_entry ) ; uint_fast64_t phnum = ELF_GET_EH ( et , buf , e_phnum ) ; for ( i = 0 ; i < phnum ; i ++ ) { uint_fast64_t phoff = ELF_GET_EH ( et , buf , e_phoff ) ; uint_fast64_t phentsize = ELF_GET_EH ( et , buf , e_phentsize ) ; uint8_t * ph = buf + phoff + i * phentsize ; uint_fast64_t type = ELF_GET_PH ( et , ph , p_type ) ; uint_fast64_t filesz = ELF_GET_PH ( et , ph , p_filesz ) ; uint_fast64_t paddr = ELF_GET_PH ( et , ph , p_paddr ) ; uint_fast64_t offset = ELF_GET_PH ( et , ph , p_offset ) ; uint_fast64_t memsz = ELF_GET_PH ( et , ph , p_memsz ) ; if ( type == PT_LOAD ) { if ( filesz > 0 ) memory_write ( node , paddr , buf + offset , filesz ) ; if ( filesz < memsz ) { uint8_t * zeros = ( uint8_t * ) fatal_malloc ( memsz - filesz ) ; memset ( zeros , 0 , memsz - filesz ) ; memory_write ( node , paddr + filesz , zeros , memsz - filesz ) ; free ( zeros ) ; } } } }
Load little endian ELF binary into memory
Load little endian ELF binary into memory
// go through every progam header entry, each one describes one segment // only one type of segment must be loaded // load code to physical address // fill rest of segment with 0