Beispiel #1
0
int	parse_file(char const *const file, t_elf *const elf)
{
  char	err[300];
  int	fd;

  if ((fd = open(file, O_RDONLY)) == -1)
    {
      sprintf(err, "open \"%s\"", file);
      perror(err);
      return (0);
    }
  if ((elf->len = lseek(fd, 0, SEEK_END)) == (unsigned)-1)
    {
      perror("lseek");
      return (0);
    }
  if (!(elf->ehdr = mmap(NULL, elf->len, PROT_READ, MAP_SHARED, fd, 0)))
    {
      perror("mmap");
      return (0);
    }
  elf->end = (void *)elf->ehdr + elf->len;
  return (check_magic_number(elf));
}
Beispiel #2
0
static inline int
read_elf_ident(struct elf_file *file) {
  struct ehdr *ehdr = &file->ehdr;
  int retval = 0;

  // It is straight-forward to read the ELF Identification. It is just an array
  // of 16 bytes. So there is no trouble with endianness; that comes later.

  if (fread(&ehdr->e_ident, sizeof(ehdr->e_ident), 1, file->stream) != 1) {
    error(0, errno, "couldn't read ELF Identification");
    return ELF_ERROR_IO_ERROR;
  }

  retval = check_magic_number(ehdr);
  if (retval != 0) return retval;

  retval = check_elf_class_32(ehdr);
  if (retval != 0) return retval;

  retval = check_data_encoding(ehdr);
  if (retval != 0) return retval;

  return 0;
}