/* Load an ELF file */ int load_elf(const char *elf) { int ret = 0; do { g_elfdata = load_file(elf); if(g_elfdata == NULL) { break; } if(!validate_header(g_elfdata)) { break; } if(!load_sections(g_elfdata)) { break; } if(!process_relocs()) { break; } reindex_sections(); ret = 1; } while(0); return ret; }
bool elfio::load( std::istream &stream ) { clean(); unsigned char e_ident[EI_NIDENT]; // Read ELF file signature stream.seekg( 0 ); stream.read( reinterpret_cast<char*>( &e_ident ), sizeof( e_ident ) ); // Is it ELF file? if ( stream.gcount() != sizeof( e_ident ) || e_ident[EI_MAG0] != ELFMAG0 || e_ident[EI_MAG1] != ELFMAG1 || e_ident[EI_MAG2] != ELFMAG2 || e_ident[EI_MAG3] != ELFMAG3 ) { return false; } if ( ( e_ident[EI_CLASS] != ELFCLASS64 ) && ( e_ident[EI_CLASS] != ELFCLASS32 )) { return false; } convertor.setup( e_ident[EI_DATA] ); header = create_header( e_ident[EI_CLASS], e_ident[EI_DATA] ); if ( 0 == header ) { return false; } if ( !header->load( stream ) ) { return false; } load_sections( stream ); load_segments( stream ); return true; }
/* Load an ELF file */ int load_elf(const char *elf) { int ret = 0; do { g_elfdata = load_file(elf, &g_elfsize); if(g_elfdata == NULL) { break; } if(!validate_header(g_elfdata)) { break; } if(!load_sections(g_elfdata)) { break; } ret = 1; } while(0); return ret; }
//------------------------------------------------------------------------------ bool load( const std::string& file_name ) { clean(); std::ifstream stream; stream.open( file_name.c_str(), std::ios::in | std::ios::binary ); if ( !stream ) { return false; } unsigned char e_ident[EI_NIDENT]; // Read ELF file signature stream.seekg( 0 ); stream.read( reinterpret_cast<char*>( &e_ident ), sizeof( e_ident ) ); // Is it ELF file? if ( stream.gcount() != sizeof( e_ident ) || e_ident[EI_MAG0] != ELFMAG0 || e_ident[EI_MAG1] != ELFMAG1 || e_ident[EI_MAG2] != ELFMAG2 || e_ident[EI_MAG3] != ELFMAG3 ) { return false; } if ( ( e_ident[EI_CLASS] != ELFCLASS64 ) && ( e_ident[EI_CLASS] != ELFCLASS32 )) { return false; } convertor.setup( e_ident[EI_DATA] ); header = create_header( e_ident[EI_CLASS], e_ident[EI_DATA] ); if ( 0 == header ) { return false; } if ( !header->load( stream ) ) { return false; } load_sections( stream ); load_segments( stream ); return true; }
int load_elf32(struct proc_info * proc, file_t * file, uintptr_t * vaddr_base) { struct elf32_header elfhdr; struct elf32_phdr * phdr = NULL; uintptr_t rbase; int retval; if (!vaddr_base) return -EINVAL; if (read_elf32_header(&elfhdr, file)) return -ENOEXEC; switch (elfhdr.e_type) { case ET_DYN: rbase = *vaddr_base; break; case ET_EXEC: rbase = 0; break; default: return -ENOEXEC; } phdr = read_program_headers(file, &elfhdr); if (!phdr) { return -ENOEXEC; } if ((retval = verify_loadable_sections(&elfhdr, phdr, rbase)) || (retval = load_sections(proc, file, &elfhdr, phdr, rbase, vaddr_base))) goto out; retval = 0; out: kfree(phdr); return retval; }