CS520
Spring 2018
Laboratory 6
Friday March 2
The lab must be submitted prior to 12noon on Sunday March 4.


This lab continues from where Lab 5 ended. In fact it would be wise not to wait a week to do this lab. Try to do this lab as soon as possible after Lab 5 in order to leave more time to complete the disassembler itself.

The goal of the lab is to locate the RISC-V instructions and to dump them in hex to stderr, one per line. Print all the hex digits, even if they are zero. Do not print anything else on the line. And do not print any headers or anything else to the output.

This should be done in a general way, demonstrating an understanding of the ELF file structure. In particular, you need to locate the ".text" section, which is where the instructions are stored. The sections are described by a section header table. The e_shoff field of the ELF header contains the byte offset in the file where the section header table begins. The e_shnum field of the ELF header contains the number of entries in the section header table. Here is a C struct that describes a section header table entry:

typedef struct {
  uint32_t sh_name;
  uint32_t sh_type;
  uint64_t sh_flags;
  uint64_t sh_addr;
  uint64_t sh_offset;
  uint64_t sh_size;
  uint32_t sh_link;
  uint32_t sh_info;
  uint64_t sh_addralign;
  uint64_t sh_entsize;
} Elf64_Shdr;
You need to search through the entries to find the one with a name of ".text".

The problem is that the characters of the names are stored in another section, known as the section name string table. But you don't need to do a search for this section, because the e_shstrndx field of the ELF header contains the index of the section header table entry that describes the section name string table. Once you locate that entry, its sh_offset field will give you the byte offset in the file where the section name string table begins. The sh_name field of a section header table entry is the byte offset into the section name string table where the characters of the section's name are stored. The names are NULL-terminated in the string table.

Now you can search through the section header table to find the entry whose sh_name field points to the string ".text". The sh_offset field in this entry will contain the byte offset in the file where the instructions begin. The sh_size field will describe the length of the section in units of bytes.

All that is left to do is to print the instructions in hex to stderr.

You should keep all of your code from Lab 5, including the printing of the file length and the three ELF header fields to stderr. In particular your program should be robust if you are given a file that is not a 64-bit, little-endian ELF file for the RISC-V.

Before starting I strongly suggest using od to examine some ELF files. See if you can locate the ".text" section by hand, by examining the od output.

Put all your source code in a single file, "disRISCV.c".

Turn in this laboratory by typing:

% ~cs520/bin/submit lab6 disRISCV.c

Submissions can be checked by typing:

% ~cs520/bin/scheck lab6

Remember that there are no late submissions of labs. Submit what you have prior to 12noon on Sunday March 4. Be careful: one minute late is late.


Last modified on January 19, 2018.

Comments and questions should be directed to pjh@cs.unh.edu