Skip to content

Instantly share code, notes, and snippets.

@klueska
Created July 4, 2016 19:13
Show Gist options
  • Save klueska/5ecfa06d7c55be11a02cf716d43518b1 to your computer and use it in GitHub Desktop.
Save klueska/5ecfa06d7c55be11a02cf716d43518b1 to your computer and use it in GitHub Desktop.
#include <elfio/elfio.hpp>
int main(int argc, char **argv)
{
ELFIO::elfio elf;
if (!elf.load("/usr/bin/ls")) {
std::cerr << "Error loading binary" << std::endl;
exit(1);
}
auto section = elf.sections[".note.ABI-tag"];
if (section == NULL) {
std::cerr << "Error finding .note.ABI-tag section" << std::endl;
exit(1);
}
auto accessor = ELFIO::note_section_accessor(elf, section);
ELFIO::Elf_Word type;
std::string name;
void* descriptor;
ELFIO::Elf_Word descriptor_size;
if (!accessor.get_note(0, type, name, descriptor, descriptor_size)) {
std::cerr << "Error getting note" << std::endl;
exit(1);
}
// Linux mandates `name == GNU`.
if (name != "GNU") {
std::cerr << "The name field != GNU" << std::endl;
}
// The version array consists of 4 32-bit numbers, with the
// first number fixed at 0 (meaning it is a Linux ELF file), and
// the rest specifying the ABI version. For example, if the array
// contains {0, 2, 3, 99}, this signifies a Linux ELF file
// with an ABI version of 2.3.99.
std::vector<uint32_t> version(
(uint32_t*)descriptor,
(uint32_t*)((char*)descriptor + descriptor_size));
std::cout << "Version: " << version[1] << "."
<< version[2] << "."
<< version[3] << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment