46 #include <sys/types.h>
57 #include "debug/Loader.hh"
71 if (elf_version(EV_CURRENT) == EV_NONE)
72 panic(
"wrong elf version number!");
79 elf_memory((
char *)
const_cast<uint8_t *
>(ifd->data()), ifd->len());
83 if (gelf_getehdr(elf, &ehdr) == 0)
97 std::string interpDir;
105 "Error: setInterpDir has already been called once\n");
112 elf = elf_memory((
char *)
const_cast<uint8_t *
>(
imageData->data()),
126 for (
int i = 0;
i <
ehdr.e_phnum; ++
i) {
128 if (gelf_getphdr(
elf,
i, &phdr) == 0) {
129 panic(
"gelf_getphdr failed for segment %d.",
i);
132 if (phdr.p_type == PT_LOAD)
134 if (phdr.p_type == PT_INTERP) {
146 "No loadable segments in '%s'. ELF file corrupted?\n",
155 if (elf_version(EV_CURRENT) == EV_NONE)
156 panic(
"wrong elf version number!");
160 Elf_Scn *section = elf_getscn(
elf, sec_idx);
165 gelf_getshdr(section, &shdr);
167 if (shdr.sh_type == SHT_SYMTAB) {
168 Elf_Data *
data = elf_getdata(section,
nullptr);
169 int count = shdr.sh_size / shdr.sh_entsize;
170 DPRINTF(Loader,
"Found Symbol Table, %d symbols present.",
count);
175 gelf_getsym(
data,
i, &sym);
177 char *sym_name = elf_strptr(
elf, shdr.sh_link, sym.st_name);
178 if (!sym_name || sym_name[0] ==
'$')
183 symbol.
name = sym_name;
185 switch (GELF_ST_BIND(sym.st_info)) {
200 DPRINTF(Loader,
"Symbol: %-40s value %#x.\n",
206 section = elf_getscn(
elf, sec_idx);
214 const std::string elf_path = (
char *)
imageData->data() + phdr.p_offset;
215 if (!interpDir.empty())
216 return interpDir + elf_path;
224 auto &emach =
ehdr.e_machine;
225 auto &eclass =
ehdr.e_ident[EI_CLASS];
226 auto &edata =
ehdr.e_ident[EI_DATA];
229 if (emach == EM_SPARC64 || (emach == EM_SPARC && eclass == ELFCLASS64) ||
230 emach == EM_SPARCV9) {
232 }
else if (emach == EM_SPARC32PLUS ||
233 (emach == EM_SPARC && eclass == ELFCLASS32)) {
235 }
else if (emach == EM_MIPS && eclass == ELFCLASS32) {
237 if (edata != ELFDATA2LSB) {
238 fatal(
"The binary you're trying to load is compiled for big "
239 "endian MIPS. gem5\nonly supports little endian MIPS. "
240 "Please recompile your binary.\n");
242 }
else if (emach == EM_X86_64 && eclass == ELFCLASS64) {
244 }
else if (emach == EM_386 && eclass == ELFCLASS32) {
246 }
else if (emach == EM_ARM && eclass == ELFCLASS32) {
248 }
else if (emach == EM_AARCH64 && eclass == ELFCLASS64) {
250 }
else if (emach == EM_RISCV) {
252 }
else if (emach == EM_PPC && eclass == ELFCLASS32) {
254 }
else if (emach == EM_PPC64 && eclass == ELFCLASS64) {
257 warn(
"Unknown architecture: %d\n", emach);
266 if (
ehdr.e_machine == EM_PPC64) {
267 switch (
ehdr.e_flags & 0x3) {
271 if (
ehdr.e_ident[EI_DATA] == ELFDATA2MSB)
273 if (
ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
280 switch (
ehdr.e_ident[EI_OSABI]) {
284 case ELFOSABI_SOLARIS:
293 case ELFOSABI_FREEBSD:
300 Elf_Scn *section = elf_getscn(
elf, 1);
301 for (
int sec_idx = 1; section; section = elf_getscn(
elf, ++sec_idx)) {
303 gelf_getshdr(section, &shdr);
305 char *e_str = elf_strptr(
elf,
ehdr.e_shstrndx, shdr.sh_name);
306 if (shdr.sh_type == SHT_NOTE && !strcmp(
".note.ABI-tag", e_str)) {
310 Elf_Data *raw_data = elf_rawdata(section,
nullptr);
311 assert(raw_data && raw_data->d_buf);
313 uint32_t raw_abi = ((uint32_t *)raw_data->d_buf)[4];
314 bool is_le =
ehdr.e_ident[EI_DATA] == ELFDATA2LSB;
315 uint32_t os_abi = is_le ?
htole(raw_abi) :
htobe(raw_abi);
322 fatal(
"gem5 does not support the HURD ABI.\n");
332 if (!strcmp(
".SUNW_version", e_str) || !strcmp(
".stab.index", e_str)) {
342 auto edata =
ehdr.e_ident[EI_DATA];
343 if (edata == ELFDATANONE)
344 panic(
"invalid ELF data encoding");
345 byteOrder = (edata == ELFDATA2MSB) ? ByteOrder::big : ByteOrder::little;
353 if (phdr.p_memsz == 0) {
354 warn(
"Ignoring empty loadable segment %s",
name);
359 phdr.p_offset, phdr.p_filesz });
360 Addr uninitialized = phdr.p_memsz - phdr.p_filesz;
367 phdr.p_paddr + phdr.p_filesz, uninitialized });
370 const Addr file_start = phdr.p_offset;
371 const Addr file_end = file_start + phdr.p_filesz;
377 if (file_start <= ehdr.e_phoff && file_end >
ehdr.e_phoff)
392 if (elf_version(EV_CURRENT) == EV_NONE)
393 panic(
"wrong elf version number!");
397 elf_memory((
char *)
const_cast<uint8_t *
>(
imageData->data()),
403 if (gelf_getehdr(
elf, &
ehdr) ==0) {
404 panic(
"Not ELF, shouldn't be here");
409 Elf_Scn *section = elf_getscn(
elf, sec_idx);
414 gelf_getshdr(section, &shdr);
416 section = elf_getscn(
elf, ++sec_idx);