diff --git a/src/system/runtime_loader/Jamfile b/src/system/runtime_loader/Jamfile index 4cb8d3624a..9837993920 100644 --- a/src/system/runtime_loader/Jamfile +++ b/src/system/runtime_loader/Jamfile @@ -60,7 +60,7 @@ Objects elf.c export.c heap.c - utility.c + utility.cpp arch_relocate.c arch_call_init_term.c ; diff --git a/src/system/runtime_loader/elf.c b/src/system/runtime_loader/elf.c index 56cc87e3e4..25558e36e5 100644 --- a/src/system/runtime_loader/elf.c +++ b/src/system/runtime_loader/elf.c @@ -1199,6 +1199,14 @@ load_program(char const *path, void **_entry) remap_images(); // ToDo: once setup_system_time() is fixed, move this one line higher! + // Since the images are initialized now, we no longer should use our + // getenv(), but use the one from libroot.so + { + struct Elf32_Sym *symbol = find_symbol_in_loaded_images(&image, "getenv"); + if (symbol != NULL) + gGetEnv = (void *)(symbol->st_value + image->regions[0].delta); + } + if (sProgramImage->entry_point == NULL) { status = B_NOT_AN_EXECUTABLE; goto err; diff --git a/src/system/runtime_loader/runtime_loader_private.h b/src/system/runtime_loader/runtime_loader_private.h index 1e2ceb7171..af9e4d0b1d 100644 --- a/src/system/runtime_loader/runtime_loader_private.h +++ b/src/system/runtime_loader/runtime_loader_private.h @@ -13,6 +13,15 @@ #include +extern struct uspace_program_args *gProgramArgs; +extern struct rld_export gRuntimeLoader; +extern char *(*gGetEnv)(const char *name); + + +#ifdef __cplusplus +extern "C" { +#endif + int runtime_loader(void *arg); int open_executable(char *name, image_type type, const char *rpath); status_t test_executable(const char *path, uid_t user, gid_t group, char *starter); @@ -42,7 +51,8 @@ status_t arch_relocate_image(image_t *image); void arch_call_init(image_t *image); void arch_call_term(image_t *image); -extern struct uspace_program_args *gProgramArgs; -extern struct rld_export gRuntimeLoader; +#ifdef __cplusplus +} +#endif #endif /* RUNTIME_LOADER_H */ diff --git a/src/system/runtime_loader/utility.c b/src/system/runtime_loader/utility.cpp similarity index 59% rename from src/system/runtime_loader/utility.c rename to src/system/runtime_loader/utility.cpp index fcb81861cb..98c3387bda 100644 --- a/src/system/runtime_loader/utility.c +++ b/src/system/runtime_loader/utility.cpp @@ -9,17 +9,26 @@ #include "runtime_loader_private.h" +#include + #include #include #include -#include +#include -char * +char *(*gGetEnv)(const char *name) = NULL; + + +extern "C" char * getenv(const char *name) { - // ToDo: this should use the real environ pointer once available! - // (or else, any updates to the search paths while the app is running are ignored) + if (gGetEnv != NULL) { + // Use libroot's getenv() as soon as it is available to us - the environment + // in gProgramArgs is static. + return gGetEnv(name); + } + char **environ = gProgramArgs->envp; int32 length = strlen(name); int32 i; @@ -33,18 +42,17 @@ getenv(const char *name) } -int -printf(const char *fmt, ...) +extern "C" int +printf(const char *format, ...) { + char buffer[1024]; va_list args; - char buf[1024]; - int i; - va_start(args, fmt); - i = vsprintf(buf, fmt, args); + va_start(args, format); + int length = vsprintf(buffer, format, args); va_end(args); - _kern_write(2, 0, buf, strlen(buf)); + _kern_write(STDERR_FILENO, 0, buffer, length); - return i; + return length; }