diff --git a/src/system/glue/arch/arm/crti.S b/src/system/glue/arch/arm/crti.S index 337c772ec2..680b4f24a5 100644 --- a/src/system/glue/arch/arm/crti.S +++ b/src/system/glue/arch/arm/crti.S @@ -21,10 +21,8 @@ .section .init FUNCTION(_init): - bl __haiku_init_before // crtbegin.o stuff comes here .section .fini FUNCTION(_fini): - bl __haiku_term_before // crtbegin.o stuff comes here diff --git a/src/system/glue/arch/arm/crtn.S b/src/system/glue/arch/arm/crtn.S index 125022e37d..48e3dc6170 100644 --- a/src/system/glue/arch/arm/crtn.S +++ b/src/system/glue/arch/arm/crtn.S @@ -10,10 +10,8 @@ .section .init // the image ID and program args are still on the stack - bl __haiku_init_after .section .fini // the image ID and program args are still on the stack - bl __haiku_term_after diff --git a/src/system/glue/arch/m68k/crti.S b/src/system/glue/arch/m68k/crti.S index 27e4ea9654..f415ef49f6 100644 --- a/src/system/glue/arch/m68k/crti.S +++ b/src/system/glue/arch/m68k/crti.S @@ -21,10 +21,8 @@ .section .init FUNCTION(_init): - jsr __haiku_init_before // crtbegin.o stuff comes here .section .fini FUNCTION(_fini): - jsr __haiku_term_before // crtbegin.o stuff comes here diff --git a/src/system/glue/arch/m68k/crtn.S b/src/system/glue/arch/m68k/crtn.S index 528eb14444..0a5519b7df 100644 --- a/src/system/glue/arch/m68k/crtn.S +++ b/src/system/glue/arch/m68k/crtn.S @@ -10,10 +10,8 @@ .section .init // the image ID and program args are still on the stack - jsr __haiku_init_after rts .section .fini // the image ID and program args are still on the stack - jsr __haiku_term_after rts diff --git a/src/system/glue/arch/ppc/crti.S b/src/system/glue/arch/ppc/crti.S index ce318b5788..f415ef49f6 100644 --- a/src/system/glue/arch/ppc/crti.S +++ b/src/system/glue/arch/ppc/crti.S @@ -21,10 +21,8 @@ .section .init FUNCTION(_init): - bl __haiku_init_before // crtbegin.o stuff comes here .section .fini FUNCTION(_fini): - bl __haiku_term_before // crtbegin.o stuff comes here diff --git a/src/system/glue/arch/ppc/crtn.S b/src/system/glue/arch/ppc/crtn.S index b9666c9b02..91aae4473a 100644 --- a/src/system/glue/arch/ppc/crtn.S +++ b/src/system/glue/arch/ppc/crtn.S @@ -10,10 +10,8 @@ .section .init // the image ID and program args are still on the stack - bl __haiku_init_after blr .section .fini // the image ID and program args are still on the stack - bl __haiku_term_after blr diff --git a/src/system/glue/arch/riscv32/crti.S b/src/system/glue/arch/riscv32/crti.S index 3f51db9ced..680b4f24a5 100644 --- a/src/system/glue/arch/riscv32/crti.S +++ b/src/system/glue/arch/riscv32/crti.S @@ -21,10 +21,8 @@ .section .init FUNCTION(_init): - jal __haiku_init_before // crtbegin.o stuff comes here .section .fini FUNCTION(_fini): - jal __haiku_term_before // crtbegin.o stuff comes here diff --git a/src/system/glue/arch/riscv32/crtn.S b/src/system/glue/arch/riscv32/crtn.S index ff63444a4d..48e3dc6170 100644 --- a/src/system/glue/arch/riscv32/crtn.S +++ b/src/system/glue/arch/riscv32/crtn.S @@ -10,10 +10,8 @@ .section .init // the image ID and program args are still on the stack - jal __haiku_init_after .section .fini // the image ID and program args are still on the stack - jal __haiku_term_after diff --git a/src/system/glue/arch/riscv64/crti.S b/src/system/glue/arch/riscv64/crti.S index 3f51db9ced..680b4f24a5 100644 --- a/src/system/glue/arch/riscv64/crti.S +++ b/src/system/glue/arch/riscv64/crti.S @@ -21,10 +21,8 @@ .section .init FUNCTION(_init): - jal __haiku_init_before // crtbegin.o stuff comes here .section .fini FUNCTION(_fini): - jal __haiku_term_before // crtbegin.o stuff comes here diff --git a/src/system/glue/arch/riscv64/crtn.S b/src/system/glue/arch/riscv64/crtn.S index ff63444a4d..48e3dc6170 100644 --- a/src/system/glue/arch/riscv64/crtn.S +++ b/src/system/glue/arch/riscv64/crtn.S @@ -10,10 +10,8 @@ .section .init // the image ID and program args are still on the stack - jal __haiku_init_after .section .fini // the image ID and program args are still on the stack - jal __haiku_term_after diff --git a/src/system/glue/arch/x86/crti.S b/src/system/glue/arch/x86/crti.S index 711213cdfe..406326221f 100644 --- a/src/system/glue/arch/x86/crti.S +++ b/src/system/glue/arch/x86/crti.S @@ -25,7 +25,6 @@ FUNCTION(_init): movl %esp, %ebp sub $4,%esp // Keep stack aligned pushl 8(%ebp) // put image ID on the stack again - call __haiku_init_before // crtbegin.o stuff comes here .section .fini @@ -34,5 +33,4 @@ FUNCTION(_fini): movl %esp, %ebp sub $4,%esp // Keep stack aligned pushl 8(%ebp) - call __haiku_term_before // crtend.o stuff comes here diff --git a/src/system/glue/arch/x86/crtn.S b/src/system/glue/arch/x86/crtn.S index f898161818..793d0286e1 100644 --- a/src/system/glue/arch/x86/crtn.S +++ b/src/system/glue/arch/x86/crtn.S @@ -10,14 +10,12 @@ .section .init // the image ID is still on the stack - call __haiku_init_after movl %ebp, %esp popl %ebp ret .section .fini // the image ID is still on the stack - call __haiku_term_after movl %ebp, %esp popl %ebp ret diff --git a/src/system/glue/arch/x86_64/crti.S b/src/system/glue/arch/x86_64/crti.S index ec26b6d5a3..dff0deeddb 100644 --- a/src/system/glue/arch/x86_64/crti.S +++ b/src/system/glue/arch/x86_64/crti.S @@ -15,7 +15,6 @@ * stacked together like this: * * crti.S entry point - * call to _init_before/_term_before * crtbegin.S GCC specific: constructors/destructors are called, ... * crtend.S * crtn.S call to _init_after/_term_after @@ -28,18 +27,18 @@ FUNCTION(_init): push %rbp movq %rsp, %rbp - // Preserve image ID for call to __haiku_init_after. + // Preserve image ID. push %rdi sub $0x8, %rsp - call __haiku_init_before // crtbegin.o stuff comes here .section .fini FUNCTION(_fini): push %rbp movq %rsp, %rbp + push %rdi sub $0x8, %rsp - call __haiku_term_before + // crtend.o stuff comes here diff --git a/src/system/glue/arch/x86_64/crtn.S b/src/system/glue/arch/x86_64/crtn.S index 6eb07e181a..5a4b4139a9 100644 --- a/src/system/glue/arch/x86_64/crtn.S +++ b/src/system/glue/arch/x86_64/crtn.S @@ -15,7 +15,7 @@ // The image ID is preserved on the stack. add $0x8, %rsp pop %rdi - call __haiku_init_after + movq %rbp, %rsp pop %rbp ret @@ -23,7 +23,7 @@ .section .fini add $0x8, %rsp pop %rdi - call __haiku_term_after + movq %rbp, %rsp pop %rbp ret diff --git a/src/system/glue/init_term_dyn.c b/src/system/glue/init_term_dyn.c index 707b2e41a3..fccfa7ed4d 100644 --- a/src/system/glue/init_term_dyn.c +++ b/src/system/glue/init_term_dyn.c @@ -1,81 +1,6 @@ /* - * Copyright 2003-2006, Axel Dörfler, axeld@pinc-software.de. All rights reserved. + * Copyright 2019, Haiku, Inc. All rights reserved. * Distributed under the terms of the MIT License. */ - -#include -#include - -#include "init_term_dyn.h" - - -// include the version glue -- it's separate for the kernel add-ons only #include "haiku_version_glue.c" - - -/*! These functions are called from _init()/_fini() (in crti.S, crtn.S) - __haiku_{init,term}_before() is called before crtbegin/end code is - executed, __haiku_{init,term}_after() after this. crtbegin contains - code to initialize all global constructors and other GCC related things - (like exception frames). - */ - - -#define HIDDEN_FUNCTION(function) asm volatile(".hidden " #function) - - -void -__haiku_init_before(image_id id) -{ - void (*before)(image_id); - - HIDDEN_FUNCTION(__haiku_init_before); - - if (get_image_symbol(id, B_INIT_BEFORE_FUNCTION_NAME, B_SYMBOL_TYPE_TEXT, - (void**)&before) == B_OK) { - before(id); - } -} - - -void -__haiku_init_after(image_id id) -{ - void (*after)(image_id); - - HIDDEN_FUNCTION(__haiku_init_after); - - if (get_image_symbol(id, B_INIT_AFTER_FUNCTION_NAME, B_SYMBOL_TYPE_TEXT, - (void**)&after) == B_OK) { - after(id); - } -} - - -void -__haiku_term_before(image_id id) -{ - void (*before)(image_id); - - HIDDEN_FUNCTION(__haiku_term_before); - - if (get_image_symbol(id, B_TERM_BEFORE_FUNCTION_NAME, B_SYMBOL_TYPE_TEXT, - (void**)&before) == B_OK) { - before(id); - } -} - - -void -__haiku_term_after(image_id id) -{ - void (*after)(image_id); - - HIDDEN_FUNCTION(__haiku_term_after); - - if (get_image_symbol(id, B_TERM_AFTER_FUNCTION_NAME, B_SYMBOL_TYPE_TEXT, - (void**)&after) == B_OK) { - after(id); - } -} diff --git a/src/system/glue/init_term_dyn.h b/src/system/glue/init_term_dyn.h deleted file mode 100644 index e35ae8c811..0000000000 --- a/src/system/glue/init_term_dyn.h +++ /dev/null @@ -1,18 +0,0 @@ -/* - * Copyright 2003-2006, Axel Dörfler, axeld@pinc-software.de. All rights reserved. - * Distributed under the terms of the MIT License. - */ -#ifndef INIT_TERM_DYN_H -#define INIT_TERM_DYN_H - - -#include - - -void __haiku_init_before(image_id id); -void __haiku_init_after(image_id id); -void __haiku_term_before(image_id id); -void __haiku_term_after(image_id id); - - -#endif /* INIT_TERM_DYN_H */ diff --git a/src/system/runtime_loader/elf.cpp b/src/system/runtime_loader/elf.cpp index 018315955d..fb617260ee 100644 --- a/src/system/runtime_loader/elf.cpp +++ b/src/system/runtime_loader/elf.cpp @@ -42,6 +42,7 @@ static const char* const kLockName = "runtime loader"; typedef void (*init_term_function)(image_id); +typedef void (*initfini_array_function)(); bool gProgramLoaded = false; image_t* gProgramImage; @@ -260,7 +261,14 @@ init_dependencies(image_t *image, bool initHead) if (image->preinit_array) { uint count_preinit = image->preinit_array_len / sizeof(addr_t); for (uint j = 0; j < count_preinit; j++) - ((init_term_function)image->preinit_array[j])(image->id); + ((initfini_array_function)image->preinit_array[j])(); + } + + init_term_function before; + if (find_symbol(image, + SymbolLookupInfo(B_INIT_BEFORE_FUNCTION_NAME, B_SYMBOL_TYPE_TEXT), + (void**)&before) == B_OK) { + before(image->id); } if (image->init_routine != 0) @@ -269,12 +277,19 @@ init_dependencies(image_t *image, bool initHead) if (image->init_array) { uint count_init = image->init_array_len / sizeof(addr_t); for (uint j = 0; j < count_init; j++) - ((init_term_function)image->init_array[j])(image->id); + ((initfini_array_function)image->init_array[j])(); + } + + init_term_function after; + if (find_symbol(image, + SymbolLookupInfo(B_INIT_AFTER_FUNCTION_NAME, B_SYMBOL_TYPE_TEXT), + (void**)&after) == B_OK) { + after(image->id); } image_event(image, IMAGE_EVENT_INITIALIZED); } - TRACE(("%ld: init done.\n", find_thread(NULL))); + TRACE(("%ld: init done.\n", find_thread(NULL))); free(initList); } @@ -645,15 +660,29 @@ unload_library(void* handle, image_id imageID, bool addOn) image_event(image, IMAGE_EVENT_UNINITIALIZING); + init_term_function before; + if (find_symbol(image, + SymbolLookupInfo(B_TERM_BEFORE_FUNCTION_NAME, B_SYMBOL_TYPE_TEXT), + (void**)&before) == B_OK) { + before(image->id); + } + if (image->term_array) { uint count_term = image->term_array_len / sizeof(addr_t); for (uint i = count_term; i-- > 0;) - ((init_term_function)image->term_array[i])(image->id); + ((initfini_array_function)image->term_array[i])(); } if (image->term_routine) ((init_term_function)image->term_routine)(image->id); + init_term_function after; + if (find_symbol(image, + SymbolLookupInfo(B_TERM_AFTER_FUNCTION_NAME, B_SYMBOL_TYPE_TEXT), + (void**)&after) == B_OK) { + after(image->id); + } + TLSBlockTemplates::Get().Unregister(image->dso_tls_id); dequeue_disposable_image(image); @@ -812,6 +841,15 @@ get_symbol(image_id imageID, char const *symbolName, int32 symbolType, if (symbolName == NULL) return B_BAD_VALUE; + // Previously, these functions were called in __haiku_init_before + // and __haiku_init_after. Now we call them inside runtime_loader, + // so we prevent applications from fetching them. + if (strcmp(symbolName, B_INIT_BEFORE_FUNCTION_NAME) == 0 + || strcmp(symbolName, B_INIT_AFTER_FUNCTION_NAME) == 0 + || strcmp(symbolName, B_TERM_BEFORE_FUNCTION_NAME) == 0 + || strcmp(symbolName, B_TERM_AFTER_FUNCTION_NAME) == 0) + return B_BAD_VALUE; + rld_lock(); // for now, just do stupid simple global locking