macsim/src/memory.c
// Detect file format and load it into memory
// Supported: ELF, raw binary
Detect file format and load it into memory
Supported: ELF, raw binary
Detect file format and load it into memory Supported : ELF raw binary
void memory_load_file(node_t *node, const char *filename)
{
    unsigned long int size;
    uint8_t *buf;
    buf = read_whole_file(filename, &size);
    if (buf==0) fatal("Could not read file");
    Elf32_Ehdr *elf_header = (Elf32_Ehdr *)buf;
    if (elf_header->e_ident[0]==ELFMAG0 &&
        elf_header->e_ident[1]==ELFMAG1 &&
        elf_header->e_ident[2]==ELFMAG2 &&
        elf_header->e_ident[3]==ELFMAG3)
    {
        unsigned elf_type;
        switch (buf[EI_CLASS]) {
        case ELFCLASS32:
            switch (buf[EI_DATA]) {
            case ELFDATA2LSB: elf_type = ELF_32LE; break;
            case ELFDATA2MSB: elf_type = ELF_32BE; break;
            default: fatal("Unknown ELF byte order");
            }
            break;
        case ELFCLASS64:
            switch (buf[EI_DATA]) {
            case ELFDATA2LSB: elf_type = ELF_64LE; break;
            case ELFDATA2MSB: elf_type = ELF_64BE; break;
            default: fatal("Unknown ELF byte order");
            }
            break;
        default: fatal("Unknown ELF word size");
        }
        memory_load_elf(node, elf_type, buf);
    } else {
        memory_write((node_t *)node, 0, buf, size);
    }
    free(buf);
    if (node->core_type==CT_armv6m)
        node->pc = node->pc & ~1;
}
void memory_load_file ( node_t * node , const char * filename ) { unsigned long int size ; uint8_t * buf ; buf = read_whole_file ( filename , & size ) ; if ( buf == 0 ) fatal ( " " ) ; Elf32_Ehdr * elf_header = ( Elf32_Ehdr * ) buf ; if ( elf_header -> e_ident [ 0 ] == ELFMAG0 && elf_header -> e_ident [ 1 ] == ELFMAG1 && elf_header -> e_ident [ 2 ] == ELFMAG2 && elf_header -> e_ident [ 3 ] == ELFMAG3 ) { unsigned elf_type ; switch ( buf [ EI_CLASS ] ) { case ELFCLASS32 : switch ( buf [ EI_DATA ] ) { case ELFDATA2LSB : elf_type = ELF_32LE ; break ; case ELFDATA2MSB : elf_type = ELF_32BE ; break ; default : fatal ( " " ) ; } break ; case ELFCLASS64 : switch ( buf [ EI_DATA ] ) { case ELFDATA2LSB : elf_type = ELF_64LE ; break ; case ELFDATA2MSB : elf_type = ELF_64BE ; break ; default : fatal ( " " ) ; } break ; default : fatal ( " " ) ; } memory_load_elf ( node , elf_type , buf ) ; } else { memory_write ( ( node_t * ) node , 0 , buf , size ) ; } free ( buf ) ; if ( node -> core_type == CT_armv6m ) node -> pc = node -> pc & ~ 1 ; }
Detect file format and load it into memory
Supported: ELF, raw binary
Detect file format and load it into memory Supported : ELF raw binary
// ELF recognised // write raw file directly to start of memory // ugly workaround