From c66dffa987d5fdada12e2c67c5e635c366875af9 Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Tue, 7 Aug 2018 11:14:12 +0200 Subject: [PATCH] arch: x86: exception: Weak symbol on default exception handlers. Exceptions can now be connected in x86 with weak symbols. This ensures the linker will prefer the global defined exception over the weakly defined version during linking. The weakly defined handler is used in x86/core/fatal.c to ensure correct selection of handler by the linker. Signed-off-by: Torsten Rasmussen --- arch/x86/core/fatal.c | 4 ++-- arch/x86/include/exception.h | 39 ++++++++++++++++++++++++++++++++---- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/arch/x86/core/fatal.c b/arch/x86/core/fatal.c index af95bc318ecb19..046e560f7c3d3a 100644 --- a/arch/x86/core/fatal.c +++ b/arch/x86/core/fatal.c @@ -239,11 +239,11 @@ FUNC_NORETURN void handle_exc_##vector(const NANO_ESF *pEsf) \ #define _EXC_FUNC_CODE(vector) \ _EXC_FUNC(vector) \ - _EXCEPTION_CONNECT_CODE(handle_exc_##vector, vector) + _EXCEPTION_CONNECT_WEAK_CODE(handle_exc_##vector, vector) #define _EXC_FUNC_NOCODE(vector) \ _EXC_FUNC(vector) \ - _EXCEPTION_CONNECT_NOCODE(handle_exc_##vector, vector) + _EXCEPTION_CONNECT_WEAK_NOCODE(handle_exc_##vector, vector) /* Necessary indirection to ensure 'vector' is expanded before we expand * the handle_exc_##vector diff --git a/arch/x86/include/exception.h b/arch/x86/include/exception.h index 6aca1b372ce389..db91a6f351d5ce 100644 --- a/arch/x86/include/exception.h +++ b/arch/x86/include/exception.h @@ -41,12 +41,12 @@ * handlers without having to #ifdef out previous instances such as in * arch/x86/core/fatal.c */ -#define __EXCEPTION_CONNECT(handler, vector, codepush) \ +#define __EXCEPTION_CONNECT(handler, vector, codepush, flag) \ __asm__ ( \ _EXCEPTION_INTLIST(vector) \ ".pushsection .gnu.linkonce.t.exc_" STRINGIFY(vector) \ "_stub, \"ax\"\n\t" \ - ".global " STRINGIFY(_EXCEPTION_STUB_NAME(handler, vector)) "\n\t" \ + flag STRINGIFY(_EXCEPTION_STUB_NAME(handler, vector)) "\n\t" \ STRINGIFY(_EXCEPTION_STUB_NAME(handler, vector)) ":\n\t" \ "1:\n\t" \ codepush \ @@ -66,7 +66,7 @@ * @param vector Vector index in the IDT */ #define _EXCEPTION_CONNECT_NOCODE(handler, vector) \ - __EXCEPTION_CONNECT(handler, vector, "push $0\n\t") + __EXCEPTION_CONNECT(handler, vector, "push $0\n\t", ".global ") /** * @brief Connect an exception handler that does expect error code @@ -79,7 +79,38 @@ * @param vector Vector index in the IDT */ #define _EXCEPTION_CONNECT_CODE(handler, vector) \ - __EXCEPTION_CONNECT(handler, vector, "") + __EXCEPTION_CONNECT(handler, vector, "", ".global ") + +/** + * @brief Connect an exception handler that doesn't expect error code + * + * Assign an exception handler to a particular vector in the IDT. + * + * Note: The symbol will be weakly defined to allow stronger symbols to + * preferred by the linker. + * + * @param handler A handler function of the prototype + * void handler(const NANO_ESF *esf) + * @param vector Vector index in the IDT + */ +#define _EXCEPTION_CONNECT_WEAK_NOCODE(handler, vector) \ + __EXCEPTION_CONNECT(handler, vector, "push $0\n\t", ".weak ") + +/** + * @brief Connect an exception handler that does expect error code + * + * Assign an exception handler to a particular vector in the IDT. + * The error code will be accessible in esf->errorCode + * + * Note: The symbol will be weakly defined to allow stronger symbols to + * preferred by the linker. + * + * @param handler A handler function of the prototype + * void handler(const NANO_ESF *esf) + * @param vector Vector index in the IDT + */ +#define _EXCEPTION_CONNECT_WEAK_CODE(handler, vector) \ + __EXCEPTION_CONNECT(handler, vector, "", ".weak ") #endif /* _ASMLANGUAGE */