#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <elf.h>
#include <sys/auxv.h>
#include <time.h>

time_t (*vdso_time)(time_t *);

int main(int argc, char *argv[])
{
  void *vdso = (void *)getauxval(AT_SYSINFO_EHDR);

  Elf64_Ehdr *hdr = vdso;
  Elf64_Shdr *shdr = (Elf64_Shdr *)(vdso + hdr->e_shoff);

  char *shstrtab = vdso + shdr[hdr->e_shstrndx].sh_offset;
  char *dynstr;

  Elf64_Sym *dynsym = NULL;
  int dynents = 0;

  for(int i=0; i < hdr->e_shnum; i++) {
    char *text = shstrtab + shdr[i].sh_name;
    if (strcmp(text, ".dynsym") == 0) {
       dynsym = (vdso + shdr[i].sh_offset);
       dynents = shdr[i].sh_size / sizeof(Elf64_Sym);
    }
    if (strcmp(text, ".dynstr") == 0) {
      dynstr = (vdso + shdr[i].sh_offset);
    }
  }

  for(int i=0; i < dynents; i++) {
    char *sym = dynstr + dynsym[i].st_name;
    printf("%s\n", sym);
    if (strcmp(sym, "time") == 0) vdso_time = vdso + dynsym[i].st_value;
  }

  if (vdso_time) {
    printf("glibc time(0): %ld\n", time(0));
    printf(" vdso_time(0): %ld\n", vdso_time(0));
  }

  return 0;
}
