Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libelfin: reading ELF binaries and DWARFv4 debug information #3283

Merged
merged 5 commits into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions recipes/libelfin/all/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
cmake_minimum_required(VERSION 3.1)
project(libelfin CXX)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()

if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 11)
endif()
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(PythonInterp 3 REQUIRED)

file(GLOB_RECURSE elf_sources source_subfolder/elf/*.cc)
set(elf_headers
source_subfolder/elf/common.hh
source_subfolder/elf/data.hh
source_subfolder/elf/elf++.hh)
file(GLOB_RECURSE dwarf_sources source_subfolder/dwarf/*.cc)
set(dwarf_headers
source_subfolder/dwarf/data.hh
source_subfolder/dwarf/dwarf++.hh
source_subfolder/dwarf/small_vector.hh)

add_custom_command(
OUTPUT source_subfolder/elf/to_string.cc
COMMAND ${CMAKE_COMMAND} -E echo '// Automatically generated' > to_string.cc
COMMAND ${CMAKE_COMMAND} -E echo '// DO NOT EDIT' >> to_string.cc
COMMAND ${CMAKE_COMMAND} -E echo >> to_string.cc
COMMAND ${CMAKE_COMMAND} -E echo '\#include \"data.hh\"' >> to_string.cc
COMMAND ${CMAKE_COMMAND} -E echo '\#include \"to_hex.hh\"' >> to_string.cc
COMMAND ${CMAKE_COMMAND} -E echo >> to_string.cc
COMMAND ${CMAKE_COMMAND} -E echo 'ELFPP_BEGIN_NAMESPACE' >> to_string.cc
COMMAND ${CMAKE_COMMAND} -E echo >> to_string.cc
COMMAND ${PYTHON_EXECUTABLE} enum-print.py -u --hex --no-type --mask shf --mask pf -x loos -x hios -x loproc -x hiproc < data.hh >> to_string.cc
COMMAND ${CMAKE_COMMAND} -E echo 'ELFPP_END_NAMESPACE' >> to_string.cc
DEPENDS source_subfolder/elf/enum-print.py source_subfolder/elf/data.hh
WORKING_DIRECTORY source_subfolder/elf)

add_custom_command(
OUTPUT source_subfolder/dwarf/to_string.cc
COMMAND ${CMAKE_COMMAND} -E echo '// Automatically generated' > to_string.cc
COMMAND ${CMAKE_COMMAND} -E echo '// DO NOT EDIT' >> to_string.cc
COMMAND ${CMAKE_COMMAND} -E echo >> to_string.cc
COMMAND ${CMAKE_COMMAND} -E echo '\#include \"internal.hh\"' >> to_string.cc
COMMAND ${CMAKE_COMMAND} -E echo >> to_string.cc
COMMAND ${CMAKE_COMMAND} -E echo 'DWARFPP_BEGIN_NAMESPACE' >> to_string.cc
COMMAND ${CMAKE_COMMAND} -E echo >> to_string.cc
COMMAND ${PYTHON_EXECUTABLE} ../elf/enum-print.py < dwarf++.hh >> to_string.cc
COMMAND ${PYTHON_EXECUTABLE} ../elf/enum-print.py -s _ -u --hex -x hi_user -x lo_user < data.hh >> to_string.cc
COMMAND ${CMAKE_COMMAND} -E echo 'DWARFPP_END_NAMESPACE' >> to_string.cc
DEPENDS source_subfolder/elf/enum-print.py source_subfolder/dwarf/data.hh
WORKING_DIRECTORY source_subfolder/dwarf)

klimkin marked this conversation as resolved.
Show resolved Hide resolved
add_library(elf++ ${elf_sources} source_subfolder/elf/to_string.cc)
set_target_properties(elf++ PROPERTIES
PUBLIC_HEADER "${elf_headers}"
VERSION ${CONAN_PACKAGE_VERSION})

add_library(dwarf++ ${dwarf_sources} source_subfolder/dwarf/to_string.cc)
set_target_properties(dwarf++ PROPERTIES
PUBLIC_HEADER "${dwarf_headers}"
VERSION ${CONAN_PACKAGE_VERSION})

include(GNUInstallDirs)
install(TARGETS elf++
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/libelfin/elf)
install(TARGETS dwarf++
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/libelfin/dwarf)

install(FILES source_subfolder/LICENSE DESTINATION licenses)
8 changes: 8 additions & 0 deletions recipes/libelfin/all/conandata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
sources:
"0.3":
url: "https://github.com/aclements/libelfin/archive/v0.3.tar.gz"
sha256: "c338942b967582922b3514b54b93175ca9051a9668db92dd8ef619824d443ac7"
patches:
"0.3":
- patch_file: "patches/commit-9d0db16d0a0b3c4f8aaa60a3e4dab295df34b6b2.patch"
base_path: "source_subfolder"
64 changes: 64 additions & 0 deletions recipes/libelfin/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import os
from conans import ConanFile, CMake, tools
from conans.errors import ConanInvalidConfiguration


class LibelfinConan(ConanFile):
name = "libelfin"
description = "C++11 library for reading ELF binaries and DWARFv4 debug information"
url = "https://github.com/conan-io/conan-center-index"
homepage = "https://github.com/aclements/libelfin"
license = "MIT"
topics = ("conan", "elf", "dwarf", "libelfin")

settings = "os", "arch", "compiler", "build_type"
options = {"shared": [True, False], "fPIC": [True, False]}
default_options = {"shared": False, "fPIC": True}

exports_sources = "CMakeLists.txt", "patches/*"
generators = "cmake"

_cmake = None
_source_subfolder = "source_subfolder"

def config_options(self):
if self.settings.os == "Windows":
del self.options.fPIC

def configure(self):
if self.settings.compiler == "Visual Studio":
raise ConanInvalidConfiguration("libelfin doesn't support compiler: {} on OS: {}.".
format(self.settings.compiler, self.settings.os))
klimkin marked this conversation as resolved.
Show resolved Hide resolved
if self.options.shared:
del self.options.fPIC
if self.settings.compiler.cppstd:
tools.check_min_cppstd(self, "11")

def source(self):
tools.get(**self.conan_data["sources"][self.version])
extracted_dir = self.name + "-" + self.version
os.rename(extracted_dir, self._source_subfolder)

klimkin marked this conversation as resolved.
Show resolved Hide resolved
def _configure_cmake(self):
if self._cmake:
return self._cmake
self._cmake = CMake(self)
self._cmake.configure()
return self._cmake

def build(self):
for patch in self.conan_data.get("patches", {}).get(self.version, []):
tools.patch(**patch)
cmake = self._configure_cmake()
cmake.build()

def package(self):
cmake = self._configure_cmake()
cmake.install()

def package_info(self):
self.cpp_info.components["libelf++"].names["pkg_config"] = "libelf++"
self.cpp_info.components["libelf++"].libs = ["elf++"]
self.cpp_info.components["libdwarf++"].names["pkg_config"] = "libdwarf++"
self.cpp_info.components["libdwarf++"].libs = ["dwarf++"]
self.cpp_info.components["libdwarf++"].requires = ["libelf++"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
#
# Updates v0.3 to the commit 9d0db16d0a0b3c4f8aaa60a3e4dab295df34b6b2:
# 9d0db16 fix data initialization of elf segment
# 74f1116 ignore dwarf extensions of gcc in the line table
# ac45a09 Added #include <errno.h> to files that use errno.
# 4a678c7 Don't mandate that compilation units need a DW_AT_comp_dir for line table parsing
# 85812f6 Fix typos
#
diff --git a/dwarf/abbrev.cc b/dwarf/abbrev.cc
index c780b8e..f77dc0c 100644
--- a/dwarf/abbrev.cc
+++ b/dwarf/abbrev.cc
@@ -133,6 +133,10 @@ resolve_type(DW_AT name, DW_FORM form)
case DW_AT::ranges:
return value::type::rangelist;

+ case DW_AT::lo_user...DW_AT::hi_user:
+ //HACK: ignore vendor extensions
+ return value::type::invalid;
+
default:
throw format_error("DW_FORM_sec_offset not expected for attribute " +
to_string(name));
diff --git a/dwarf/dwarf.cc b/dwarf/dwarf.cc
index fe9b947..2465eef 100644
--- a/dwarf/dwarf.cc
+++ b/dwarf/dwarf.cc
@@ -289,8 +289,7 @@ compilation_unit::get_line_table() const
{
if (!m->lt.valid()) {
const die &d = root();
- if (!d.has(DW_AT::stmt_list) || !d.has(DW_AT::name) ||
- !d.has(DW_AT::comp_dir))
+ if (!d.has(DW_AT::stmt_list) || !d.has(DW_AT::name))
goto done;

shared_ptr<section> sec;
@@ -300,8 +299,10 @@ compilation_unit::get_line_table() const
goto done;
}

+ auto comp_dir = d.has(DW_AT::comp_dir) ? at_comp_dir(d) : "";
+
m->lt = line_table(sec, d[DW_AT::stmt_list].as_sec_offset(),
- m->subsec->addr_size, at_comp_dir(d),
+ m->subsec->addr_size, comp_dir,
at_name(d));
}
done:
diff --git a/elf/elf++.hh b/elf/elf++.hh
index 562d1de..ee59ed0 100644
--- a/elf/elf++.hh
+++ b/elf/elf++.hh
@@ -48,7 +48,7 @@ public:
* section data. Hence, callers must ensure that the loader passed to
* this file remains live as long as any such pointer is in use.
* Keeping any object that can return such a pointer live is
- * sufficieint to keep the loader live.
+ * sufficient to keep the loader live.
*/
class elf
{
@@ -204,7 +204,7 @@ public:

/**
* Return the in-memory size of this segment in bytes.
- * Bytes between file_size() and mem_size() are implicity zeroes.
+ * Bytes between file_size() and mem_size() are implicitly zeroes.
*/
size_t mem_size() const;

diff --git a/elf/elf.cc b/elf/elf.cc
index 587329f..61172ac 100644
--- a/elf/elf.cc
+++ b/elf/elf.cc
@@ -167,12 +167,10 @@ elf::get_segment(unsigned index) const

struct segment::impl {
impl(const elf &f)
- : f(f) { }
+ : f(f), data(nullptr) { }

const elf f;
Phdr<> hdr;
- // const char *name;
- // size_t name_len;
const void *data;
};

diff --git a/elf/mmap_loader.cc b/elf/mmap_loader.cc
index 69d8acb..875d7bd 100644
--- a/elf/mmap_loader.cc
+++ b/elf/mmap_loader.cc
@@ -9,6 +9,7 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
+#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

diff --git a/examples/dump-lines.cc b/examples/dump-lines.cc
index ec58be1..9fed1fa 100644
--- a/examples/dump-lines.cc
+++ b/examples/dump-lines.cc
@@ -1,6 +1,7 @@
#include "elf++.hh"
#include "dwarf++.hh"

+#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>

diff --git a/examples/dump-sections.cc b/examples/dump-sections.cc
index 12e7569..22526ec 100644
--- a/examples/dump-sections.cc
+++ b/examples/dump-sections.cc
@@ -2,6 +2,7 @@

#include <sys/types.h>
#include <sys/stat.h>
+#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>

diff --git a/examples/dump-segments.cc b/examples/dump-segments.cc
index f9e07d3..4c6319f 100644
--- a/examples/dump-segments.cc
+++ b/examples/dump-segments.cc
@@ -2,6 +2,7 @@

#include <sys/types.h>
#include <sys/stat.h>
+#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>

diff --git a/examples/dump-syms.cc b/examples/dump-syms.cc
index e7c2c3b..af7f484 100644
--- a/examples/dump-syms.cc
+++ b/examples/dump-syms.cc
@@ -2,6 +2,7 @@

#include <sys/types.h>
#include <sys/stat.h>
+#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>

diff --git a/examples/dump-tree.cc b/examples/dump-tree.cc
index 57d2b70..8bf2a63 100644
--- a/examples/dump-tree.cc
+++ b/examples/dump-tree.cc
@@ -1,6 +1,7 @@
#include "elf++.hh"
#include "dwarf++.hh"

+#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>

diff --git a/examples/find-pc.cc b/examples/find-pc.cc
index dc40b85..2e324fc 100644
--- a/examples/find-pc.cc
+++ b/examples/find-pc.cc
@@ -1,6 +1,7 @@
#include "elf++.hh"
#include "dwarf++.hh"

+#include <errno.h>
#include <fcntl.h>
#include <string>
#include <inttypes.h>
11 changes: 11 additions & 0 deletions recipes/libelfin/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.1)
project(test_package CXX)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()

add_executable(${PROJECT_NAME} test_package.cpp)
target_link_libraries(${PROJECT_NAME} ${CONAN_LIBS})
klimkin marked this conversation as resolved.
Show resolved Hide resolved
set_target_properties(${PROJECT_NAME} PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED ON)
5 changes: 5 additions & 0 deletions recipes/libelfin/all/test_package/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# From http://timelessname.com/elfbin/

hello: hello.asm
nasm -f elf hello.asm
gcc -m32 -s -o hello hello.o -nostartfiles -nostdlib -nodefaultlibs
19 changes: 19 additions & 0 deletions recipes/libelfin/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from conans import ConanFile, CMake, tools
import os


class TestPackageConan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "cmake"

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()

def test(self):
if tools.cross_building(self.settings):
return
bin_path = os.path.join("bin", "test_package")
elf_path = os.path.join(self.source_folder, "hello")
self.run("{} {}".format(bin_path, elf_path), run_environment=True)
Binary file added recipes/libelfin/all/test_package/hello
Binary file not shown.
16 changes: 16 additions & 0 deletions recipes/libelfin/all/test_package/hello.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
SECTION .data
msg: db "Hi World",10
len: equ $-msg

SECTION .text
global main
main:
mov edx,len
mov ecx,msg
mov ebx,1
mov eax,4
int 0x80
mov ebx,0
mov eax,1
int 0x80

Loading