mirror of
https://github.com/genodelabs/genode.git
synced 2025-06-18 07:08:18 +00:00
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:
@ -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();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user