config: Add new option to replace IDF_PATH and project path with placeholders in macros

Allows building with asserts on and still not finding any actual file paths in the
final binary file.

Alternative fix for https://github.com/espressif/esp-idf/issues/6306

Progress towards https://github.com/espressif/esp-idf/issues/5873
This commit is contained in:
Angus Gratton 2021-02-18 16:02:23 +11:00
parent 9ae01e40b5
commit 9b988ca097
10 changed files with 53 additions and 8 deletions

View File

@ -90,6 +90,11 @@ if(CONFIG_COMPILER_DUMP_RTL_FILES)
list(APPEND compile_options "-fdump-rtl-expand")
endif()
if(CONFIG_COMPILER_HIDE_PATHS_MACROS)
list(APPEND compile_options "-fmacro-prefix-map=${CMAKE_SOURCE_DIR}=.")
list(APPEND compile_options "-fmacro-prefix-map=${IDF_PATH}=IDF")
endif()
# GCC-specific options
if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
list(APPEND compile_options "-fstrict-volatile-bitfields"

16
Kconfig
View File

@ -279,6 +279,22 @@ mainmenu "Espressif IoT Development Framework Configuration"
endchoice # assertions
menuconfig COMPILER_HIDE_PATHS_MACROS
bool "Replace ESP-IDF and project paths in binaries"
default y
depends on IDF_CMAKE
help
When expanding the __FILE__ and __BASE_FILE__ macros, replace paths inside ESP-IDF
with paths relative to the placeholder string "IDF", and convert paths inside the
project directory to relative paths.
This allows building the project with assertions or other code that embeds file paths,
without the binary containing the exact path to the IDF or project directories.
This option passes -fmacro-prefix-map options to the GCC command line. To replace additional
paths in your binaries, modify the project CMakeLists.txt file to pass custom -fmacro-prefix-map or
-ffile-prefix-map arguments.
menuconfig COMPILER_CXX_EXCEPTIONS
bool "Enable C++ exceptions"
default n

View File

@ -9,9 +9,11 @@ idf_build_get_property(idf_path IDF_PATH)
idf_build_get_property(python PYTHON)
idf_build_get_property(elf EXECUTABLE)
# If the configuration is one that doesn't expect any paths to be found then run this build step
# If the configuration is one that doesn't expect the IDF_PATH to be found in binaries then run this build step
# after building the ELF, will fail if it finds any file paths in binary files
if(CONFIG_OPTIMIZATION_ASSERTIONS_SILENT OR CONFIG_OPTIMIZATION_ASSERTIONS_DISABLED)
if(CONFIG_OPTIMIZATION_ASSERTIONS_SILENT OR
CONFIG_OPTIMIZATION_ASSERTIONS_DISABLED OR
CONFIG_COMPILER_HIDE_PATHS_MACROS)
add_custom_command(
TARGET ${elf}
POST_BUILD

View File

@ -3,12 +3,12 @@
This test app exists to verify that paths (like __FILE__) are not compiled into
any object files in configurations where this should be avoided.
It doubles up as a build-time check that disabling assertions doesn't lead to
any warnings.
Configurations where this is relevant include:
(These configurations include: assertions disabled, 'silent' asserts, any reproducible
builds configuration.)
* Assertions disabled (doubles up as a build-time check that disabling assertions doesn't lead to any warnings)
* Silent assertions
* CONFIG_COMPILER_HIDE_PATHS_MACROS is set to replace IDF_PATH and project dir with placeholders when expanding `__FILE__`
Not embedding paths reduces the binary size, avoids leaking information about
the compilation environment, and is a necessary step to supporet reproducible
the compilation environment, and is a necessary step to support reproducible
builds across projects built in different directories.

View File

@ -1,4 +1,14 @@
/* This test app only exists for the build stage, so doesn't need to do anything at runtime */
/* This test app only exists for the build stage, so doesn't need to do anything at runtime
apart from embedding an assert to check asserts inside the project dir */
#include <assert.h>
// Declared non-static to avoid the assert being optimized out
int other_function(void)
{
return 3;
}
void app_main(void)
{
assert(other_function() == 3);
}

View File

@ -1,5 +1,6 @@
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE=y
CONFIG_FREERTOS_ASSERT_DISABLE=y
CONFIG_COMPILER_HIDE_PATHS_MACROS=n
# compiling as many files as possible here (we don't have 100% coverage of course, due to config options, but
# try to maximize what we can check

View File

@ -1,5 +1,6 @@
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE=y
CONFIG_FREERTOS_ASSERT_DISABLE=y
CONFIG_COMPILER_HIDE_PATHS_MACROS=n
# the other sdkconfig builds Bluedroid, build NimBLE here
#

View File

@ -0,0 +1,8 @@
# this is the default value, actually
CONFIG_COMPILER_HIDE_PATHS_MACROS=y
# compiling as many files as possible here (we don't have 100% coverage of course, due to config options, but
# try to maximize what we can check
CONFIG_BT_ENABLED=y
CONFIG_BT_BLUEDROID_ENABLED=y
CONFIG_BLE_MESH=y

View File

@ -1,4 +1,5 @@
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT=y
CONFIG_COMPILER_HIDE_PATHS_MACROS=n
# compiling as many files as possible here (we don't have 100% coverage of course, due to config options, but
# try to maximize what we can check

View File

@ -1,5 +1,6 @@
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT=y
CONFIG_FREERTOS_ASSERT_DISABLE=y
CONFIG_COMPILER_HIDE_PATHS_MACROS=n
# the other sdkconfig builds Bluedroid, build NimBLE here
#