0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-05-18 10:53:48 +02:00

ircd::mods: Add platform-dependent ldso interface.

This commit is contained in:
Jason Volk 2019-05-28 14:53:55 -07:00
parent b8744b2a8b
commit 08fafd44b1
5 changed files with 88 additions and 0 deletions

View file

@ -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])

32
include/ircd/mods/ldso.h Normal file
View file

@ -0,0 +1,32 @@
// Matrix Construct
//
// Copyright (C) Matrix Construct Developers, Authors & Contributors
// Copyright (C) 2016-2019 Jason Volk <jason@zemos.net>
//
// 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 <link.h> 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<bool (struct link_map &)>;
using link_name_closure = std::function<bool (const string_view &)>;
bool for_each(const link_map_closure &);
bool for_each(const link_name_closure &);
}

View file

@ -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

View file

@ -12,8 +12,45 @@
// in libircd glibc+ELF supporting environments. Do not rely on these
// definitions being available on all platforms.
#include <RB_INC_DLFCN_H
#include <RB_INC_LINK_H
///////////////////////////////////////////////////////////////////////////////
//
// mods/ldso.h
//
bool
ircd::mods::ldso::for_each(const link_name_closure &closure)
{
return !for_each(link_map_closure{[&closure]
(struct link_map &map)
{
return closure(map.l_name);
}});
}
bool
ircd::mods::ldso::for_each(const link_map_closure &closure)
{
auto *map
{
reinterpret_cast<struct link_map *>(::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

View file

@ -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
//