main.c 861 Bytes
#include "util/header/elf.h"

#include <asm-generic/errno-base.h>
#include <fcntl.h>

int main(int argc, char **argv) {
  if (argc != 3) return EINVAL;
  int32_t fd = open(argv[1], O_RDONLY);
  if (fd == -1) {
    close(fd);
    return ENOENT;
  }
  Elf64_Ehdr header;
  int8_t result;
  if ((result = process_header(fd, &header)) != 0) {
    close(fd);
    return result;
  }
  if ((result = process_segments(fd, &header)) != 0) {
    close(fd);
    return result;
  }
  Elf64_Shdr string_table_section;
  if ((result = read_string_table(fd, &header, &string_table_section)) != 0) {
    close(fd);
    return result;
  }

  Elf64_Shdr section;
  result = find_section(fd, &header, &string_table_section, argv[2], &section);
  if (result != 0) {
    close(fd);
    return result;
  }

  ((void(*)(void)) section.sh_addr)(); // NOLINT
  close(fd);
  return 0;
}