diff --git a/include/linker/priv_stacks-text.ld b/include/linker/priv_stacks-text.ld index ff10375fb97..2a999b7592a 100644 --- a/include/linker/priv_stacks-text.ld +++ b/include/linker/priv_stacks-text.ld @@ -7,16 +7,32 @@ /* We need to reserve room for the gperf generated hash functions. * Fortunately, unlike the data tables, the size of the code is * reasonably predictable. - * - * The linker will error out complaining that the location pointer - * is moving backwards if the reserved room isn't large enough. */ _priv_stacks_text_area_start = .; *(".priv_stacks.text*") _priv_stacks_text_area_end = .; + + _priv_stacks_text_area_used = _priv_stacks_text_area_end - _priv_stacks_text_area_start; + #ifndef LINKER_PASS2 PROVIDE(_k_priv_stack_find = .); #endif - . += CONFIG_PRIVILEGED_STACK_TEXT_AREA - (_priv_stacks_text_area_end - _priv_stacks_text_area_start); -#endif /* CONFIG_USERSPACE */ + /* In a valid build the MAX function will always evaluate to the + second argument below, but to give the user a good error message + when the area overflows we need to temporarily corrupt the + location counter, and then detect the overflow with an assertion + later on. */ + + . = MAX(., _priv_stacks_text_area_start + CONFIG_PRIVILEGED_STACK_TEXT_AREA); + + ASSERT( + CONFIG_PRIVILEGED_STACK_TEXT_AREA >= _priv_stacks_text_area_used, +"The configuration system has incorrectly set +'CONFIG_PRIVILEGED_STACK_TEXT_AREA' to +CONFIG_PRIVILEGED_STACK_TEXT_AREA, which is not big enough. You must +through Kconfig either disable 'CONFIG_USERSPACE', or set +'CONFIG_PRIVILEGED_STACK_TEXT_AREA' to a value larger than +CONFIG_PRIVILEGED_STACK_TEXT_AREA." + ); +#endif /* CONFIG_USERSPACE */