ClickHouse/base/common/getResource.cpp

27 lines
999 B
C++
Raw Normal View History

2020-08-08 03:42:42 +00:00
#include "getResource.h"
#include "unaligned.h"
#include <dlfcn.h>
#include <string>
2020-08-13 21:28:56 +00:00
#include <boost/algorithm/string/replace.hpp>
2020-08-08 03:42:42 +00:00
std::string_view getResource(std::string_view name)
{
std::string name_replaced(name);
std::replace(name_replaced.begin(), name_replaced.end(), '/', '_');
std::replace(name_replaced.begin(), name_replaced.end(), '-', '_');
std::replace(name_replaced.begin(), name_replaced.end(), '.', '_');
2020-08-13 21:28:56 +00:00
boost::replace_all(name_replaced, "+", "_PLUS_");
2020-08-08 03:42:42 +00:00
/// These are the names that are generated by "ld -r -b binary"
std::string symbol_name_data = "_binary_" + name_replaced + "_start";
std::string symbol_name_size = "_binary_" + name_replaced + "_size";
const void * sym_data = dlsym(RTLD_DEFAULT, symbol_name_data.c_str());
const void * sym_size = dlsym(RTLD_DEFAULT, symbol_name_size.c_str());
if (sym_data && sym_size)
return { static_cast<const char *>(sym_data), unalignedLoad<size_t>(&sym_size) };
return {};
}