Skip to content

Commit

Permalink
arch: x86: exception: Weak symbol on default exception handlers.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
tejlmand committed Dec 6, 2018
1 parent 8b8adf8 commit c66dffa
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
4 changes: 2 additions & 2 deletions arch/x86/core/fatal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 35 additions & 4 deletions arch/x86/include/exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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
Expand All @@ -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 */

Expand Down

0 comments on commit c66dffa

Please sign in to comment.