Skip to content

Commit

Permalink
elf/app: Pass argc and argv to library initialization
Browse files Browse the repository at this point in the history
As explained nicely in golang/go#13492, since 1996
glibc's dlopen() passes argv and a bunch of other
stuff to the initialization functions of shared
objects, while we don't pass any argument
(see object::run_init_funcs()). Golang code
(ref cloudius-systems#522), in particular, assumes it gets argv,
auxv, etc., this way.

Fixes: cloudius-systems#795

Signed-off-by: Benoît Canet <benoit@scylladb.com>
  • Loading branch information
Benoît Canet committed Feb 9, 2017
1 parent e991402 commit fb6e551
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion core/app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ void application::main()
{
__libc_stack_end = __builtin_frame_address(0);

elf::get_program()->init_library();
elf::get_program()->init_library(_args.size(), _argv.get());
sched::thread::current()->set_name(_command);

if (_main) {
Expand Down
12 changes: 6 additions & 6 deletions core/elf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -932,19 +932,19 @@ std::string object::pathname()
}

// Run the object's static constructors or similar initialization
void object::run_init_funcs()
void object::run_init_funcs(int argc, char** argv)
{
if (dynamic_exists(DT_INIT)) {
auto func = dynamic_ptr<void>(DT_INIT);
if (func) {
reinterpret_cast<void(*)()>(func)();
reinterpret_cast<void(*)(int, char**)>(func)(argc, argv);
}
}
if (dynamic_exists(DT_INIT_ARRAY)) {
auto funcs = dynamic_ptr<void (*)()>(DT_INIT_ARRAY);
auto funcs = dynamic_ptr<void(*)(int, char**)>(DT_INIT_ARRAY);
auto nr = dynamic_val(DT_INIT_ARRAYSZ) / sizeof(*funcs);
for (auto i = 0u; i < nr; ++i) {
funcs[i]();
funcs[i](argc, argv);
}
}
}
Expand Down Expand Up @@ -1194,7 +1194,7 @@ program::get_library(std::string name, std::vector<std::string> extra_path, bool
return ret;
}

void program::init_library()
void program::init_library(int argc, char** argv)
{
// get the list of weak pointers before iterating on them
std::vector<std::shared_ptr<object>> loaded_objects =
Expand All @@ -1208,7 +1208,7 @@ void program::init_library()
loaded_objects[i]->setprivate(true);
}
for (int i = size - 1; i >= 0; i--) {
loaded_objects[i]->run_init_funcs();
loaded_objects[i]->run_init_funcs(argc, argv);
}
for (unsigned i = 0; i < size; i++) {
loaded_objects[i]->setprivate(false);
Expand Down
4 changes: 2 additions & 2 deletions include/osv/elf.hh
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ public:
const std::vector<Elf64_Phdr> *phdrs();
std::string soname();
std::string pathname();
void run_init_funcs();
void run_init_funcs(int argc, char** argv);
void run_fini_funcs();
template <typename T = void>
T* lookup(const char* name);
Expand Down Expand Up @@ -528,7 +528,7 @@ public:
std::shared_ptr<elf::object>
get_library(std::string lib, std::vector<std::string> extra_path = {}, bool no_init = false);

void init_library();
void init_library(int argc = 0, char **argv = nullptr);

/**
* Set the default search path for get_library().
Expand Down

0 comments on commit fb6e551

Please sign in to comment.