From 08fafd44b1841e79bea7d38d4b201cb9bf8c3139 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Tue, 28 May 2019 14:53:55 -0700 Subject: [PATCH] ircd::mods: Add platform-dependent ldso interface. --- configure.ac | 1 + include/ircd/mods/ldso.h | 32 ++++++++++++++++++++++++++++++++ include/ircd/mods/mods.h | 1 + ircd/mods_ldso.cc | 37 +++++++++++++++++++++++++++++++++++++ modules/console.cc | 17 +++++++++++++++++ 5 files changed, 88 insertions(+) create mode 100644 include/ircd/mods/ldso.h diff --git a/configure.ac b/configure.ac index d084925ce..260838a51 100644 --- a/configure.ac +++ b/configure.ac @@ -763,6 +763,7 @@ RB_CHK_SYSHEADER(signal.h, [SIGNAL_H]) RB_CHK_SYSHEADER(ifaddrs.h, [IFADDRS_H]) RB_CHK_SYSHEADER(fcntl.h, [FCNTL_H]) RB_CHK_SYSHEADER(link.h, [LINK_H]) +RB_CHK_SYSHEADER(dlfcn.h, [DLFCN_H]) RB_CHK_SYSHEADER(sys/types.h, [SYS_TYPES_H]) RB_CHK_SYSHEADER(sys/time.h, [SYS_TIME_H]) RB_CHK_SYSHEADER(sys/stat.h, [SYS_STAT_H]) diff --git a/include/ircd/mods/ldso.h b/include/ircd/mods/ldso.h new file mode 100644 index 000000000..8410d3fec --- /dev/null +++ b/include/ircd/mods/ldso.h @@ -0,0 +1,32 @@ +// Matrix Construct +// +// Copyright (C) Matrix Construct Developers, Authors & Contributors +// Copyright (C) 2016-2019 Jason Volk +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice is present in all copies. The +// full license for this software is available in the LICENSE file. + +#pragma once +#define HAVE_IRCD_MODS_LDSO_H + +// This is a platform dependent interface. While this header and these decls +// are unconditionally included in the standard include stack, the defs +// behind them may not be compiled and linked on all platforms. + +// Forward declarations for because it is not included here. +extern "C" +{ + struct link_map; +} + +/// Platform-dependent interface on ELF+ld.so supporting compilations. +namespace ircd::mods::ldso +{ + using link_map_closure = std::function; + using link_name_closure = std::function; + + bool for_each(const link_map_closure &); + bool for_each(const link_name_closure &); +} diff --git a/include/ircd/mods/mods.h b/include/ircd/mods/mods.h index 8dd09aef0..92ccce857 100644 --- a/include/ircd/mods/mods.h +++ b/include/ircd/mods/mods.h @@ -67,6 +67,7 @@ namespace ircd::mods #include "sym_ptr.h" #include "import.h" #include "import_shared.h" +#include "ldso.h" // Exports down into ircd:: namespace ircd diff --git a/ircd/mods_ldso.cc b/ircd/mods_ldso.cc index 8298710b0..9e242d903 100644 --- a/ircd/mods_ldso.cc +++ b/ircd/mods_ldso.cc @@ -12,8 +12,45 @@ // in libircd glibc+ELF supporting environments. Do not rely on these // definitions being available on all platforms. +#include (::dlopen(NULL, RTLD_NOLOAD|RTLD_LAZY)) + }; + + if(unlikely(!map)) + throw error + { + ::dlerror() + }; + + for(; map; map = map->l_next) + if(!closure(*map)) + return false; + + return true; +} + /////////////////////////////////////////////////////////////////////////////// // // Symbolic dl-error redefinition to throw our C++ exception for the symbol diff --git a/modules/console.cc b/modules/console.cc index e17060a6f..334a74cf2 100644 --- a/modules/console.cc +++ b/modules/console.cc @@ -1602,6 +1602,23 @@ console_cmd__mod__unload(opt &out, const string_view &line) return true; } +bool +console_cmd__mod__link_map(opt &out, const string_view &line) +{ + size_t i(0); + mods::ldso::for_each([&out, &i] + (const string_view &name) + { + out << std::setw(2) << (i++) + << " " << name + << std::endl; + + return true; + }); + + return true; +} + // // ctx //