From fb6e5515f82db464072a8f70c1dbf0b1ae333ef0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Canet?= Date: Wed, 26 Oct 2016 14:09:13 +0200 Subject: [PATCH] elf/app: Pass argc and argv to library initialization MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 #522), in particular, assumes it gets argv, auxv, etc., this way. Fixes: #795 Signed-off-by: BenoƮt Canet --- core/app.cc | 2 +- core/elf.cc | 12 ++++++------ include/osv/elf.hh | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/core/app.cc b/core/app.cc index ee92c18d59..bf8ebdc2d8 100644 --- a/core/app.cc +++ b/core/app.cc @@ -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) { diff --git a/core/elf.cc b/core/elf.cc index f848bace58..ca948be3aa 100644 --- a/core/elf.cc +++ b/core/elf.cc @@ -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(DT_INIT); if (func) { - reinterpret_cast(func)(); + reinterpret_cast(func)(argc, argv); } } if (dynamic_exists(DT_INIT_ARRAY)) { - auto funcs = dynamic_ptr(DT_INIT_ARRAY); + auto funcs = dynamic_ptr(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); } } } @@ -1194,7 +1194,7 @@ program::get_library(std::string name, std::vector 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> loaded_objects = @@ -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); diff --git a/include/osv/elf.hh b/include/osv/elf.hh index 494ed4503f..280c9b0197 100644 --- a/include/osv/elf.hh +++ b/include/osv/elf.hh @@ -335,7 +335,7 @@ public: const std::vector *phdrs(); std::string soname(); std::string pathname(); - void run_init_funcs(); + void run_init_funcs(int argc, char** argv); void run_fini_funcs(); template T* lookup(const char* name); @@ -528,7 +528,7 @@ public: std::shared_ptr get_library(std::string lib, std::vector 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().