Skip to content

Commit

Permalink
Modernize codebase (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
apolukhin authored Dec 28, 2024
1 parent f71d8e7 commit 816955c
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 207 deletions.
8 changes: 2 additions & 6 deletions example/getting_started.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

#include <boost/core/lightweight_test.hpp>
#include <boost/dll.hpp>
#include <boost/function.hpp>
#include <string>
#include "b2_workarounds.hpp"

Expand All @@ -20,7 +19,7 @@ int main(int argc, char* argv[]) {

//[getting_started_imports_c_function
// Importing pure C function
function<int(int)> c_func = dll::import_symbol<int(int)>(
auto c_func = dll::import_symbol<int(int)>(
path_to_shared_library, "c_func_name"
);
//]
Expand All @@ -43,7 +42,7 @@ int main(int argc, char* argv[]) {

//[getting_started_imports_alias
// Importing function by alias name
/*<-*/ function<std::string(const std::string&)> /*->*/ /*=auto*/ cpp_func = dll::import_alias<std::string(const std::string&)>(
auto cpp_func = dll::import_alias<std::string(const std::string&)>(
path_to_shared_library, "pretty_name"
);
//]
Expand All @@ -52,7 +51,6 @@ int main(int argc, char* argv[]) {
std::string cpp_func_res = cpp_func(std::string("In importer."));
BOOST_TEST(cpp_func_res == "In importer. Hello from lib!");

#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_AUTO_DECLARATIONS)
//[getting_started_imports_cpp11_function
// Importing function.
auto cpp11_func = dll::import_symbol<int(std::string&&)>(
Expand All @@ -63,8 +61,6 @@ int main(int argc, char* argv[]) {
// calling the function
int cpp11_func_res = cpp11_func(std::string("In importer."));
BOOST_TEST(cpp11_func_res == sizeof("In importer.") - 1);
#endif


//[getting_started_imports_cpp_variable
// Importing variable.
Expand Down
8 changes: 0 additions & 8 deletions example/getting_started_library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,14 @@
#include <boost/dll.hpp>
#include <string>

#ifdef BOOST_NO_CXX11_NOEXCEPT
#define noexcept
#endif

#define API extern "C" BOOST_SYMBOL_EXPORT

#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
//[getting_started_exports_cpp11_function
namespace some_namespace {
API int i_am_a_cpp11_function(std::string&& param) noexcept;
// ^-------------------- function name to use in dll::import_symbol<>
}
//]
#endif


//[getting_started_exports_cpp_variable
Expand Down Expand Up @@ -61,10 +55,8 @@ namespace some_namespace {
return param + " Hello from lib!";
}

#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
int i_am_a_cpp11_function(std::string&& param) noexcept {
return static_cast<int>(param.size());
}
#endif
}

4 changes: 0 additions & 4 deletions example/tutorial9/tutorial9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ int main() {
typedef HANDLE(__stdcall GetStdHandle_t)(DWORD ); // function signature with calling convention

// OPTION #0, requires C++11 compatible compiler that understands GetStdHandle_t signature.
/*<-*/
#if defined(_MSC_VER) && !defined(BOOST_NO_CXX11_TRAILING_RESULT_TYPES) && !defined(BOOST_NO_CXX11_DECLTYPE) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) /*->*/
auto get_std_handle = dll::import_symbol<GetStdHandle_t>(
"Kernel32.dll",
"GetStdHandle",
Expand All @@ -33,8 +31,6 @@ int main() {
// Signature template parameter that contains calling conventions, so you'll have to remove the calling convention.
std::function<HANDLE(DWORD)> get_std_handle2 = get_std_handle;
std::cout << "0.1 GetStdHandle() returned " << get_std_handle2(STD_OUTPUT_HANDLE) << std::endl;
/*<-*/
#endif /*->*/

// OPTION #1, hand write the import.
dll::shared_library lib("Kernel32.dll", dll::load_mode::search_system_folders);
Expand Down
130 changes: 65 additions & 65 deletions include/boost/dll/detail/elf_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,109 +14,109 @@
# pragma once
#endif

#include <cstdint>
#include <cstring>
#include <fstream>
#include <limits>
#include <vector>

#include <boost/cstdint.hpp>
#include <boost/throw_exception.hpp>

namespace boost { namespace dll { namespace detail {

template <class AddressOffsetT>
struct Elf_Ehdr_template {
unsigned char e_ident[16]; /* Magic number and other info */
boost::uint16_t e_type; /* Object file type */
boost::uint16_t e_machine; /* Architecture */
boost::uint32_t e_version; /* Object file version */
AddressOffsetT e_entry; /* Entry point virtual address */
AddressOffsetT e_phoff; /* Program header table file offset */
AddressOffsetT e_shoff; /* Section header table file offset */
boost::uint32_t e_flags; /* Processor-specific flags */
boost::uint16_t e_ehsize; /* ELF header size in bytes */
boost::uint16_t e_phentsize; /* Program header table entry size */
boost::uint16_t e_phnum; /* Program header table entry count */
boost::uint16_t e_shentsize; /* Section header table entry size */
boost::uint16_t e_shnum; /* Section header table entry count */
boost::uint16_t e_shstrndx; /* Section header string table index */
unsigned char e_ident[16]; /* Magic number and other info */
std::uint16_t e_type; /* Object file type */
std::uint16_t e_machine; /* Architecture */
std::uint32_t e_version; /* Object file version */
AddressOffsetT e_entry; /* Entry point virtual address */
AddressOffsetT e_phoff; /* Program header table file offset */
AddressOffsetT e_shoff; /* Section header table file offset */
std::uint32_t e_flags; /* Processor-specific flags */
std::uint16_t e_ehsize; /* ELF header size in bytes */
std::uint16_t e_phentsize; /* Program header table entry size */
std::uint16_t e_phnum; /* Program header table entry count */
std::uint16_t e_shentsize; /* Section header table entry size */
std::uint16_t e_shnum; /* Section header table entry count */
std::uint16_t e_shstrndx; /* Section header string table index */
};

typedef Elf_Ehdr_template<boost::uint32_t> Elf32_Ehdr_;
typedef Elf_Ehdr_template<boost::uint64_t> Elf64_Ehdr_;
using Elf32_Ehdr_ = Elf_Ehdr_template<std::uint32_t>;
using Elf64_Ehdr_ = Elf_Ehdr_template<std::uint64_t>;

template <class AddressOffsetT>
struct Elf_Shdr_template {
boost::uint32_t sh_name; /* Section name (string tbl index) */
boost::uint32_t sh_type; /* Section type */
AddressOffsetT sh_flags; /* Section flags */
AddressOffsetT sh_addr; /* Section virtual addr at execution */
AddressOffsetT sh_offset; /* Section file offset */
AddressOffsetT sh_size; /* Section size in bytes */
boost::uint32_t sh_link; /* Link to another section */
boost::uint32_t sh_info; /* Additional section information */
AddressOffsetT sh_addralign; /* Section alignment */
AddressOffsetT sh_entsize; /* Entry size if section holds table */
std::uint32_t sh_name; /* Section name (string tbl index) */
std::uint32_t sh_type; /* Section type */
AddressOffsetT sh_flags; /* Section flags */
AddressOffsetT sh_addr; /* Section virtual addr at execution */
AddressOffsetT sh_offset; /* Section file offset */
AddressOffsetT sh_size; /* Section size in bytes */
std::uint32_t sh_link; /* Link to another section */
std::uint32_t sh_info; /* Additional section information */
AddressOffsetT sh_addralign; /* Section alignment */
AddressOffsetT sh_entsize; /* Entry size if section holds table */
};

typedef Elf_Shdr_template<boost::uint32_t> Elf32_Shdr_;
typedef Elf_Shdr_template<boost::uint64_t> Elf64_Shdr_;
using Elf32_Shdr_ = Elf_Shdr_template<std::uint32_t>;
using Elf64_Shdr_ = Elf_Shdr_template<std::uint64_t>;

template <class AddressOffsetT>
struct Elf_Sym_template;

template <>
struct Elf_Sym_template<boost::uint32_t> {
typedef boost::uint32_t AddressOffsetT;

boost::uint32_t st_name; /* Symbol name (string tbl index) */
AddressOffsetT st_value; /* Symbol value */
AddressOffsetT st_size; /* Symbol size */
unsigned char st_info; /* Symbol type and binding */
unsigned char st_other; /* Symbol visibility */
boost::uint16_t st_shndx; /* Section index */
struct Elf_Sym_template<std::uint32_t> {
using AddressOffsetT = std::uint32_t;

std::uint32_t st_name; /* Symbol name (string tbl index) */
AddressOffsetT st_value; /* Symbol value */
AddressOffsetT st_size; /* Symbol size */
unsigned char st_info; /* Symbol type and binding */
unsigned char st_other; /* Symbol visibility */
std::uint16_t st_shndx; /* Section index */
};

template <>
struct Elf_Sym_template<boost::uint64_t> {
typedef boost::uint64_t AddressOffsetT;

boost::uint32_t st_name; /* Symbol name (string tbl index) */
unsigned char st_info; /* Symbol type and binding */
unsigned char st_other; /* Symbol visibility */
boost::uint16_t st_shndx; /* Section index */
AddressOffsetT st_value; /* Symbol value */
AddressOffsetT st_size; /* Symbol size */
struct Elf_Sym_template<std::uint64_t> {
using AddressOffsetT = std::uint64_t;

std::uint32_t st_name; /* Symbol name (string tbl index) */
unsigned char st_info; /* Symbol type and binding */
unsigned char st_other; /* Symbol visibility */
std::uint16_t st_shndx; /* Section index */
AddressOffsetT st_value; /* Symbol value */
AddressOffsetT st_size; /* Symbol size */
};


typedef Elf_Sym_template<boost::uint32_t> Elf32_Sym_;
typedef Elf_Sym_template<boost::uint64_t> Elf64_Sym_;
using Elf32_Sym_ = Elf_Sym_template<std::uint32_t>;
using Elf64_Sym_ = Elf_Sym_template<std::uint64_t>;

template <class AddressOffsetT>
class elf_info {
typedef boost::dll::detail::Elf_Ehdr_template<AddressOffsetT> header_t;
typedef boost::dll::detail::Elf_Shdr_template<AddressOffsetT> section_t;
typedef boost::dll::detail::Elf_Sym_template<AddressOffsetT> symbol_t;
using header_t = boost::dll::detail::Elf_Ehdr_template<AddressOffsetT>;
using section_t= boost::dll::detail::Elf_Shdr_template<AddressOffsetT>;
using symbol_t = boost::dll::detail::Elf_Sym_template<AddressOffsetT>;

BOOST_STATIC_CONSTANT(boost::uint32_t, SHT_SYMTAB_ = 2);
BOOST_STATIC_CONSTANT(boost::uint32_t, SHT_STRTAB_ = 3);
BOOST_STATIC_CONSTANT(boost::uint32_t, SHT_DYNSYM_ = 11);
static constexpr std::uint32_t SHT_SYMTAB_ = 2;
static constexpr std::uint32_t SHT_STRTAB_ = 3;
static constexpr std::uint32_t SHT_DYNSYM_ = 11;

BOOST_STATIC_CONSTANT(unsigned char, STB_LOCAL_ = 0); /* Local symbol */
BOOST_STATIC_CONSTANT(unsigned char, STB_GLOBAL_ = 1); /* Global symbol */
BOOST_STATIC_CONSTANT(unsigned char, STB_WEAK_ = 2); /* Weak symbol */
static constexpr unsigned char STB_LOCAL_ = 0; /* Local symbol */
static constexpr unsigned char STB_GLOBAL_ = 1; /* Global symbol */
static constexpr unsigned char STB_WEAK_ = 2; /* Weak symbol */

/* Symbol visibility specification encoded in the st_other field. */
BOOST_STATIC_CONSTANT(unsigned char, STV_DEFAULT_ = 0); /* Default symbol visibility rules */
BOOST_STATIC_CONSTANT(unsigned char, STV_INTERNAL_ = 1); /* Processor specific hidden class */
BOOST_STATIC_CONSTANT(unsigned char, STV_HIDDEN_ = 2); /* Sym unavailable in other modules */
BOOST_STATIC_CONSTANT(unsigned char, STV_PROTECTED_ = 3); /* Not preemptible, not exported */
static constexpr unsigned char STV_DEFAULT_ = 0; /* Default symbol visibility rules */
static constexpr unsigned char STV_INTERNAL_ = 1; /* Processor specific hidden class */
static constexpr unsigned char STV_HIDDEN_ = 2; /* Sym unavailable in other modules */
static constexpr unsigned char STV_PROTECTED_ = 3; /* Not preemptible, not exported */

public:
static bool parsing_supported(std::ifstream& fs) {
const unsigned char magic_bytes[5] = {
0x7f, 'E', 'L', 'F', sizeof(boost::uint32_t) == sizeof(AddressOffsetT) ? 1 : 2
0x7f, 'E', 'L', 'F', sizeof(std::uint32_t) == sizeof(AddressOffsetT) ? 1 : 2
};

unsigned char ch;
Expand Down Expand Up @@ -336,8 +336,8 @@ class elf_info {
}
};

typedef elf_info<boost::uint32_t> elf_info32;
typedef elf_info<boost::uint64_t> elf_info64;
using elf_info32 = elf_info<std::uint32_t> ;
using elf_info64 = elf_info<std::uint64_t>;

}}} // namespace boost::dll::detail

Expand Down
Loading

0 comments on commit 816955c

Please sign in to comment.