1

We recieved a homework assignment in which we need to take an ELF file and print its sections' names.

We are supposed to do all that using only the data we receive directly from the ELF header, meaning we can't use any "high level" procedures - we need to go directly to the data we need.

So, im trying to print the first section's name. I know the names are supposed to be in the string table. This is what I have so far:

I'm getting the start of the ELF file using mmap...

elfhead =(Elf32_Ehdr *) mmap...

I'm getting the section offset using the members in the ELF header

sectionoffset = elfhead->e_shoff

then

section = (Elf32_Shdr*)(elfhead + sectionoffset)
nameoffset = section->sh_name    
stringoffset = elfhead->e_shstrndx;

To be clear -

  • in elfhead i have the elf header
  • in section i have the section header
  • in stringoffset i have the index inside the section table where the string table is supposed to be
  • in nameoffset i have the index in the string table where the first section name is suppose to be.

How do I go to the first name and print it, given the code above?

Mike
  • 47,263
  • 29
  • 113
  • 177
iddqd
  • 1,225
  • 2
  • 16
  • 34
  • possible duplicate of [getting the sh_name member in a section header elf file](http://stackoverflow.com/questions/10863510/getting-the-sh-name-member-in-a-section-header-elf-file) – Employed Russian Jun 03 '12 at 19:22

1 Answers1

1

Well first off you'd have to have access to the section's String Table, and since the header is the first thing in the ELF file:

char* stringTable = elfhead + (section + header->stringoffset)->sh_offset;

Once you have that, all you really have to do is print the first one using the nameoffset you already obtained, like so.

char* name = stringTable + nameoffset;
printf("%s\n",name);

FYI, printing the rest of the names would be a simple loop:

for(i=0;i<header->e_shnum;i++){
        char* name = stringTable + nameoffset;
        printf("%s\n",name);
        section++;
    }
RELnemtzov
  • 81
  • 2
  • 7