Simpify startup of dynamically linked binaries

This patch removes the component_entry_point library, which used to
proved a hook for the libc to intercept the call of the
'Component::construct' function. The mechansim has several shortcomings
(see the discussion in the associated issue) and was complex. So we
eventually discarded the approach in favor of the explicit handling of
the startup.

A regular Genode component provides a 'Component::construct' function,
which is determined by the dynamic linker via a symbol lookup.
For the time being, the dynamic linker falls back to looking up a 'main'
function if no 'Component::construct' function could be found.

The libc provides an implementation of 'Component::construct', which
sets up the libc's task handling and finally call the function
'Libc::Component::construct' from the context of the appllication task.
This function is expected to be provided by the libc-using application.
Consequently, Genode components that use the libc have to implement the
'Libc::Component::construct' function.

The new 'posix' library provides an implementation of
'Libc::Component::construct' that calls a main function. Hence, POSIX
programs that merely use the POSIX API merely have to add 'posix' to the
'LIBS' declaration in their 'target.mk' file. Their execution starts at
'main'.

Issue #2199
This commit is contained in:
Norman Feske
2016-12-22 15:01:19 +01:00
parent 8d521036fb
commit 4da52517c1
112 changed files with 366 additions and 369 deletions

View File

@ -152,6 +152,10 @@ namespace Genode {
}
Genode::size_t Component::stack_size() __attribute__((weak));
Genode::size_t Component::stack_size() { return 64*1024; }
/*
* We need to execute the constructor of the main entrypoint from a
* class called 'Startup' as 'Startup' is a friend of 'Entrypoint'.

View File

@ -31,8 +31,6 @@ namespace Genode {
extern bool inhibit_tracing;
void call_global_static_constructors();
void destroy_signal_thread();
extern void (*call_component_construct)(Genode::Env &);
}
@ -152,7 +150,7 @@ namespace {
Genode::call_global_static_constructors();
Genode::call_component_construct(env);
Component::construct(env);
}
};
}

View File

@ -317,6 +317,20 @@ Linker::Ld &Linker::Ld::linker()
}
/*
* Defined in the startup library, passed to legacy main functions.
*/
extern char **genode_argv;
extern int genode_argc;
extern char **genode_envp;
void genode_exit(int status);
static int exit_status;
static void exit_on_suspended() { genode_exit(exit_status); }
/**
* The dynamic binary to load
*/
@ -348,16 +362,26 @@ struct Linker::Binary : Root_object, Elf_object
Elf::Addr lookup_symbol(char const *name)
{
Elf::Sym const *symbol = 0;
if ((symbol = Elf_object::lookup_symbol(name, Hash_table::hash(name))))
return reloc_base() + symbol->st_value;
return 0;
try {
Elf::Addr base = 0;
Elf::Sym const *sym = Linker::lookup_symbol(name, dynamic().dep(), &base);
return base + sym->st_value;
}
catch (Linker::Not_found) { return 0; }
}
void call_entry_point(Env &env)
{
/* apply the component-provided stack size */
if (Elf::Addr addr = lookup_symbol("_ZN9Component10stack_sizeEv")) {
/* call 'Component::stack_size()' */
size_t const stack_size = ((size_t(*)())addr)();
/* expand stack according to the component's needs */
Thread::myself()->stack_size(stack_size);
}
/* call static construtors and register destructors */
Func * const ctors_start = (Func *)lookup_symbol("_ctors_start");
Func * const ctors_end = (Func *)lookup_symbol("_ctors_end");
@ -367,13 +391,33 @@ struct Linker::Binary : Root_object, Elf_object
Func * const dtors_end = (Func *)lookup_symbol("_dtors_end");
for (Func * dtor = dtors_start; dtor != dtors_end; genode_atexit(*dtor++));
/* call component entry point */
/* XXX the function type for call_component_construct() is a candidate
* for a base-internal header */
typedef void (*Entry)(Env &);
Entry const entry = reinterpret_cast<Entry>(_file->entry);
/* call 'Component::construct' function if present */
if (Elf::Addr addr = lookup_symbol("_ZN9Component9constructERN6Genode3EnvE")) {
((void(*)(Env &))addr)(env);
return;
}
entry(env);
/*
* The 'Component::construct' function is missing. This may be the
* case for legacy components that still implement a 'main' function.
*
* \deprecated the handling of legacy 'main' functions will be removed
*/
if (Elf::Addr addr = lookup_symbol("main")) {
warning("using legacy main function, please convert to 'Component::construct'");
exit_status = ((int (*)(int, char **, char **))addr)(genode_argc,
genode_argv,
genode_envp);
/* trigger suspend in the entry point */
env.ep().schedule_suspend(exit_on_suspended, nullptr);
/* return to entrypoint and exit via exit_on_suspended() */
return;
}
error("dynamic linker: component-entrypoint lookup failed");
}
void relocate(Bind bind) override
@ -486,7 +530,6 @@ Elf::Sym const *Linker::lookup_symbol(char const *name, Dependency const &dep,
if (binary_ptr && &dep != binary_ptr->first_dep()) {
return lookup_symbol(name, *binary_ptr->first_dep(), base, undef, other);
} else {
error("LD: could not lookup symbol \"", name, "\"");
throw Not_found();
}
}

View File

@ -250,3 +250,24 @@ extern "C" int _main()
/* never reached */
return 0;
}
static int exit_status;
static void exit_on_suspended() { genode_exit(exit_status); }
extern int main(int argc, char **argv, char **envp);
void Component::construct(Genode::Env &env) __attribute__((weak));
void Component::construct(Genode::Env &env)
{
/* call real main function */
exit_status = main(genode_argc, genode_argv, genode_envp);
/* trigger suspend in the entry point */
env.ep().schedule_suspend(exit_on_suspended, nullptr);
/* return to entrypoint and exit via exit_on_suspended() */
}

View File

@ -1,88 +0,0 @@
/*
* \brief Startup code for component construction
* \author Christian Helmuth
* \date 2016-01-21
*
* The component construction code is used by the startup library, which is
* linked to static binaries and ld.lib.so. The code is also used by the
* component_entry_point static library, which is linked to all dynamic
* executables to make the fallback implementation and the
* call_component_construct-hook function pointer available to these binaries.
*
* Note, for dynamic binaries we can't refer to the default implementation in
* ld.lib.so as it is a component itself implementing the Component functions.
*/
/*
* Copyright (C) 2016 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU General Public License version 2.
*/
/* Genode includes */
#include <base/component.h>
namespace Genode {
/*
* Hook for intercepting the call of the 'Component::construct' method. By
* hooking this function pointer in a library constructor, the libc is able
* to create a task context for the component code. This context is
* scheduled by the libc in a cooperative fashion, i.e. when the
* component's entrypoint is activated.
*/
extern void (*call_component_construct)(Genode::Env &) __attribute__((weak));
}
static void default_component_construct(Genode::Env &env)
{
Component::construct(env);
}
void (*Genode::call_component_construct)(Genode::Env &) = &default_component_construct;
/****************************************************
** Fallback implementation of Component interface **
****************************************************/
extern void genode_exit(int status);
static int exit_status;
static void exit_on_suspended() { genode_exit(exit_status); }
/*
* Regular components provide the 'Component' interface as defined in
* base/component.h. This fallback accommodates legacy components that lack the
* implementation of this interface but come with a main function.
*/
/*
* XXX these symbols reside in the startup library - candidate for
* base-internal header?
*/
extern char **genode_argv;
extern int genode_argc;
extern char **genode_envp;
extern int main(int argc, char **argv, char **envp);
void Component::construct(Genode::Env &env) __attribute__((weak));
void Component::construct(Genode::Env &env)
{
/* call real main function */
exit_status = main(genode_argc, genode_argv, genode_envp);
/* trigger suspend in the entry point */
env.ep().schedule_suspend(exit_on_suspended, nullptr);
/* return to entrypoint and exit via exit_on_suspended() */
}
Genode::size_t Component::stack_size() __attribute__((weak));
Genode::size_t Component::stack_size() { return 64*1024; }

View File

@ -1,32 +0,0 @@
/*
* \brief Component entry point for dynamic executables
* \author Christian Helmuth
* \date 2016-01-21
*
* The ELF entry point of dynamic binaries is set to component_entry_point(),
* which calls the call_component_construct-hook function pointer.
*/
/*
* Copyright (C) 2016 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU General Public License version 2.
*/
/* Genode includes */
#include <base/component.h>
/* FIXME move to base-internal header */
namespace Genode {
extern void (*call_component_construct)(Env &);
}
namespace Genode {
void component_entry_point(Genode::Env &env)
{
call_component_construct(env);
}
}