2019-05-28 12:47:02 +02:00
|
|
|
// 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.
|
|
|
|
|
2019-05-28 23:50:55 +02:00
|
|
|
// This file is platform specific. It is conditionally compiled and included
|
|
|
|
// in libircd glibc+ELF supporting environments. Do not rely on these
|
|
|
|
// definitions being available on all platforms.
|
|
|
|
|
2019-06-03 05:38:00 +02:00
|
|
|
#include <RB_INC_ELF_H
|
2019-05-28 23:53:55 +02:00
|
|
|
#include <RB_INC_DLFCN_H
|
2019-05-28 12:47:02 +02:00
|
|
|
#include <RB_INC_LINK_H
|
|
|
|
|
2019-05-28 23:53:55 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2020-03-09 02:55:33 +01:00
|
|
|
// dlfcn suite
|
|
|
|
//
|
|
|
|
#if defined(HAVE_DLFCN_H)
|
|
|
|
|
2020-05-02 20:35:08 +02:00
|
|
|
ircd::mods::ldso::info::info(const void *const &addr)
|
|
|
|
{
|
|
|
|
::Dl_info info;
|
|
|
|
syscall(::dladdr, addr, &info);
|
|
|
|
|
|
|
|
fname = info.dli_fname;
|
|
|
|
fbase = info.dli_fbase;
|
|
|
|
sname = info.dli_sname;
|
|
|
|
saddr = info.dli_saddr;
|
|
|
|
}
|
|
|
|
|
2020-03-09 02:55:33 +01:00
|
|
|
bool
|
|
|
|
ircd::mods::ldso::loaded(const string_view &name_)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
static const int flags
|
|
|
|
{
|
|
|
|
RTLD_NOLOAD | RTLD_LAZY
|
|
|
|
};
|
|
|
|
|
|
|
|
const char *const name
|
|
|
|
{
|
|
|
|
data(strlcpy(fs::name_scratch, name_))
|
|
|
|
};
|
|
|
|
|
|
|
|
const custom_ptr<void> ptr
|
|
|
|
{
|
|
|
|
::dlopen(name, flags), ::dlclose
|
|
|
|
};
|
|
|
|
|
|
|
|
return ptr.get() != nullptr;
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // HAVE_DLFCN_H
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// link_map suite
|
2019-05-28 23:53:55 +02:00
|
|
|
//
|
2019-07-14 03:42:01 +02:00
|
|
|
#if defined(HAVE_LINK_H)
|
2019-05-28 23:53:55 +02:00
|
|
|
|
2019-06-04 12:18:03 +02:00
|
|
|
bool
|
|
|
|
ircd::mods::ldso::for_each_needed(const struct link_map &map,
|
|
|
|
const string_closure &closure)
|
|
|
|
{
|
|
|
|
const char *strtab {nullptr};
|
|
|
|
for(auto d(map.l_ld); d->d_tag != DT_NULL; ++d)
|
|
|
|
if(d->d_tag == DT_STRTAB)
|
|
|
|
{
|
|
|
|
strtab = reinterpret_cast<const char *>(d->d_un.d_ptr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!strtab)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for(auto d(map.l_ld); d->d_tag != DT_NULL; ++d)
|
|
|
|
{
|
|
|
|
if(d->d_tag != DT_NEEDED)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(!closure(strtab + d->d_un.d_val))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-14 03:42:01 +02:00
|
|
|
#if defined(HAVE_ELF_H)
|
2019-06-03 05:38:00 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::mods::ldso::string(const struct link_map &map,
|
|
|
|
const size_t &idx)
|
|
|
|
{
|
2020-05-03 01:22:21 +02:00
|
|
|
const char *str
|
|
|
|
{
|
|
|
|
strtab(map)
|
|
|
|
};
|
2019-06-03 05:38:00 +02:00
|
|
|
|
|
|
|
size_t i(1);
|
|
|
|
for(++str; *str && i < idx; str += strlen(str) + 1)
|
|
|
|
++i;
|
|
|
|
|
|
|
|
return i == idx?
|
|
|
|
string_view{str}:
|
|
|
|
string_view{};
|
|
|
|
}
|
2019-07-14 03:42:01 +02:00
|
|
|
#endif
|
2019-06-03 05:38:00 +02:00
|
|
|
|
2020-05-03 01:22:21 +02:00
|
|
|
#if __has_include(<elf.h>)
|
|
|
|
const char *
|
|
|
|
ircd::mods::ldso::strtab(const struct link_map &map)
|
|
|
|
{
|
|
|
|
const char *str {nullptr};
|
|
|
|
for(auto d(map.l_ld); d->d_tag != DT_NULL; ++d)
|
|
|
|
if(d->d_tag == DT_STRTAB)
|
|
|
|
{
|
|
|
|
str = reinterpret_cast<const char *>(d->d_un.d_ptr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-06-03 02:05:36 +02:00
|
|
|
struct link_map &
|
|
|
|
ircd::mods::ldso::get(const string_view &name)
|
|
|
|
{
|
|
|
|
struct link_map *const ret
|
|
|
|
{
|
|
|
|
get(std::nothrow, name)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(unlikely(!ret))
|
|
|
|
throw not_found
|
|
|
|
{
|
|
|
|
"No library '%s' is currently mapped.", name
|
|
|
|
};
|
|
|
|
|
|
|
|
return *ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct link_map *
|
|
|
|
ircd::mods::ldso::get(std::nothrow_t,
|
|
|
|
const string_view &name)
|
|
|
|
{
|
|
|
|
struct link_map *ret{nullptr};
|
|
|
|
for_each([&name, &ret]
|
|
|
|
(struct link_map &link)
|
|
|
|
{
|
|
|
|
if(ldso::name(link) == name)
|
|
|
|
{
|
|
|
|
ret = &link;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-05-30 00:41:43 +02:00
|
|
|
size_t
|
|
|
|
ircd::mods::ldso::count()
|
|
|
|
{
|
|
|
|
size_t ret(0);
|
|
|
|
for_each([&ret](const struct link_map &)
|
|
|
|
{
|
|
|
|
++ret;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-05-28 23:53:55 +02:00
|
|
|
bool
|
2019-05-30 00:41:43 +02:00
|
|
|
ircd::mods::ldso::has(const string_view &name)
|
2019-05-28 23:53:55 +02:00
|
|
|
{
|
2019-05-30 00:41:43 +02:00
|
|
|
return !for_each([&name]
|
|
|
|
(const auto &link)
|
2019-05-28 23:53:55 +02:00
|
|
|
{
|
2019-05-30 00:41:43 +02:00
|
|
|
// false to break
|
|
|
|
return name == ldso::name(link)? false : true;
|
|
|
|
});
|
2019-05-28 23:53:55 +02:00
|
|
|
}
|
|
|
|
|
2019-06-03 00:12:32 +02:00
|
|
|
bool
|
|
|
|
ircd::mods::ldso::has_soname(const string_view &name)
|
|
|
|
{
|
|
|
|
return !for_each([&name]
|
|
|
|
(const auto &link)
|
|
|
|
{
|
|
|
|
// false to break
|
|
|
|
return name == soname(link)? false : true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::mods::ldso::has_fullname(const string_view &name)
|
|
|
|
{
|
|
|
|
return !for_each([&name]
|
|
|
|
(const auto &link)
|
|
|
|
{
|
|
|
|
// false to break
|
|
|
|
return name == fullname(link)? false : true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-28 23:53:55 +02:00
|
|
|
bool
|
2019-05-30 00:41:43 +02:00
|
|
|
ircd::mods::ldso::for_each(const link_closure &closure)
|
2019-05-28 23:53:55 +02:00
|
|
|
{
|
|
|
|
auto *map
|
|
|
|
{
|
2022-06-14 01:42:28 +02:00
|
|
|
reinterpret_cast<struct link_map *>(::dlopen(nullptr, RTLD_NOLOAD|RTLD_LAZY))
|
2019-05-28 23:53:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if(unlikely(!map))
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
::dlerror()
|
|
|
|
};
|
|
|
|
|
|
|
|
for(; map; map = map->l_next)
|
|
|
|
if(!closure(*map))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-02 22:28:26 +02:00
|
|
|
const void *
|
|
|
|
ircd::mods::ldso::addr(const struct link_map &map)
|
|
|
|
{
|
|
|
|
return reinterpret_cast<const void *>(map.l_addr);
|
|
|
|
}
|
|
|
|
|
2019-06-03 00:12:32 +02:00
|
|
|
ircd::mods::ldso::semantic_version
|
|
|
|
ircd::mods::ldso::version(const struct link_map &map)
|
|
|
|
{
|
|
|
|
return version(soname(map));
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::mods::ldso::semantic_version
|
|
|
|
ircd::mods::ldso::version(const string_view &soname)
|
|
|
|
{
|
|
|
|
const auto str
|
|
|
|
{
|
|
|
|
split(soname, ".so.").second
|
|
|
|
};
|
|
|
|
|
|
|
|
string_view val[3];
|
|
|
|
const size_t num
|
|
|
|
{
|
|
|
|
tokens(str, '.', val)
|
|
|
|
};
|
|
|
|
|
|
|
|
semantic_version ret {0};
|
|
|
|
for(size_t i(0); i < num && i < 3; ++i)
|
|
|
|
ret[i] = lex_cast<long>(val[i]);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-05-30 00:41:43 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::mods::ldso::name(const struct link_map &map)
|
2019-06-03 00:12:32 +02:00
|
|
|
{
|
|
|
|
return name(soname(map));
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::mods::ldso::name(const string_view &soname)
|
|
|
|
{
|
|
|
|
return lstrip(split(soname, '.').first, "lib");
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::mods::ldso::soname(const struct link_map &map)
|
|
|
|
{
|
|
|
|
return soname(fullname(map));
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::mods::ldso::soname(const string_view &fullname)
|
|
|
|
{
|
|
|
|
return token_last(fullname, '/');
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::mods::ldso::fullname(const struct link_map &map)
|
2019-05-30 00:41:43 +02:00
|
|
|
{
|
|
|
|
return map.l_name;
|
|
|
|
}
|
|
|
|
|
2019-07-14 03:42:01 +02:00
|
|
|
#endif // defined(HAVE_LINK_H)
|
|
|
|
|
2019-05-28 12:47:02 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Symbolic dl-error redefinition to throw our C++ exception for the symbol
|
|
|
|
// lookup failure, during the lazy binding, directly from the callsite. THIS IS
|
|
|
|
// BETTER than the default glibc/elf/dl behavior of terminating the program.
|
|
|
|
//
|
|
|
|
// We probably need asynchronous-unwind-tables for an exception to safely
|
|
|
|
// transit from here through libdl and out of a random PLT slot. non-call-
|
|
|
|
// exceptions does not appear to be necessary, at least for FUNC symbols.
|
|
|
|
//
|
2019-07-14 03:42:01 +02:00
|
|
|
#if defined(HAVE_DLFCN_H)
|
2019-05-28 12:47:02 +02:00
|
|
|
|
|
|
|
// glibc/sysdeps/generic/ldsodefs.h
|
|
|
|
struct dl_exception
|
|
|
|
{
|
|
|
|
const char *objname;
|
|
|
|
const char *errstring;
|
|
|
|
char *message_buffer;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
_dl_exception_free(struct dl_exception *)
|
|
|
|
__attribute__ ((nonnull(1)));
|
|
|
|
|
2022-06-14 01:42:28 +02:00
|
|
|
extern "C" void
|
2022-06-20 05:03:21 +02:00
|
|
|
__attribute__((noreturn))
|
2022-06-14 01:42:28 +02:00
|
|
|
_dl_signal_exception(int, struct dl_exception *, const char *);
|
|
|
|
|
2019-05-28 12:47:02 +02:00
|
|
|
extern "C" void
|
2022-06-20 05:03:21 +02:00
|
|
|
__attribute__((noreturn))
|
2019-05-28 12:47:02 +02:00
|
|
|
_dl_signal_exception(int errcode,
|
|
|
|
struct dl_exception *e,
|
|
|
|
const char *occasion)
|
|
|
|
{
|
2019-09-19 20:00:12 +02:00
|
|
|
using namespace ircd;
|
|
|
|
|
|
|
|
const unwind free
|
2019-05-28 12:47:02 +02:00
|
|
|
{
|
|
|
|
std::bind(_dl_exception_free, e)
|
|
|
|
};
|
|
|
|
|
2019-09-19 20:00:12 +02:00
|
|
|
assert(e);
|
|
|
|
log::derror
|
2019-05-28 12:47:02 +02:00
|
|
|
{
|
2019-09-19 20:00:12 +02:00
|
|
|
mods::log, "dynamic linker (%d) %s in `%s' :%s",
|
2019-05-28 12:47:02 +02:00
|
|
|
errcode,
|
|
|
|
occasion,
|
|
|
|
e->objname,
|
|
|
|
e->errstring
|
|
|
|
};
|
|
|
|
|
2019-09-19 20:00:12 +02:00
|
|
|
static const auto &undefined_symbol_prefix
|
|
|
|
{
|
|
|
|
"undefined symbol: "
|
|
|
|
};
|
|
|
|
|
|
|
|
if(startswith(e->errstring, undefined_symbol_prefix))
|
|
|
|
{
|
|
|
|
const auto &mangled
|
|
|
|
{
|
|
|
|
lstrip(e->errstring, undefined_symbol_prefix)
|
|
|
|
};
|
|
|
|
|
|
|
|
throw mods::undefined_symbol
|
|
|
|
{
|
|
|
|
"%s %s (%s)",
|
|
|
|
e->objname,
|
|
|
|
demangle(mangled),
|
|
|
|
mangled,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
throw mods::error
|
2019-05-28 12:47:02 +02:00
|
|
|
{
|
|
|
|
"%s in %s (%d) %s",
|
|
|
|
occasion,
|
|
|
|
e->objname,
|
|
|
|
errcode,
|
|
|
|
e->errstring,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-07-14 03:42:01 +02:00
|
|
|
#endif // defined(HAVE_DLFCN_H
|
|
|
|
|
2019-05-28 12:47:02 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// symbolic dlsym hook
|
|
|
|
//
|
2019-07-14 03:42:01 +02:00
|
|
|
#ifdef HAVE_DLFCN_H
|
2019-05-28 12:47:02 +02:00
|
|
|
#ifdef IRCD_MODS_HOOK_DLSYM
|
|
|
|
|
|
|
|
extern "C" void *
|
|
|
|
__libc_dlsym(void *, const char *);
|
|
|
|
|
|
|
|
extern "C" void *
|
|
|
|
dlsym(void *const handle,
|
|
|
|
const char *const symbol)
|
|
|
|
{
|
|
|
|
#ifdef RB_DEBUG_MODS_HOOK_DLSYM
|
|
|
|
ircd::log::debug
|
|
|
|
{
|
|
|
|
ircd::mods::log, "handle:%p symbol lookup '%s'",
|
|
|
|
handle,
|
|
|
|
symbol
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return __libc_dlsym(handle, symbol);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif IRCD_MODS_HOOK_DLSYM
|
2019-07-14 03:42:01 +02:00
|
|
|
#endif HAVE_DLFCN_H
|