CS471/571 - Operating Systems


Exercise #9

Read the manual pages for elf(5), vdso(7) and getauxval(3). Using getauxval() get the address of the Linux VDSO (Virtual Dynamic Shared Object) and scan it as a ELF object and find the address for the time(2) function and use it to get and print the current time in seconds since the Epoch.

Elf structures you will need to know about:

Elf64_Ehdr - The main ELF header Elf64_Shdr - The section header structure Elf64_Sym - The structure for a symbol table

Steps you may want to follow:

  1. Start by getting the header information, you will want to scan the section headers. The section header strings are in the section header defined by the e_shstrndx value in the header.

  2. In the section headers you want to find the ".dynstr" and ".dynsym" sections. You are looking for the "time" symbol in the .dynsym symbol table. The value of the symbol is the address of the function. Read time(2) for the function prototype.

Programming hints:

A string table is a array of characters, a name is the offset from the beginning of a given string table, i.e.:

char *strtab = <address>
char *name = strtab + offset;

Note when accessing an array of structures given by a base address, there are two ways to access each structure:

Pointer method:

  X_hdr *ptr = <address>
  for(int i=0; i < num_structs; i++) {

Array method:

  X_hdr *ptr = <address>
  for(int i=0; i < num_structs; i++) {

The array method is probably preferred.