blob: 0ae53d3ac9b10f1d986358c8e2116c3be72f6d59 [file] [log] [blame]
Tamas Banf8b0b2d2020-10-26 13:03:13 +00001#-------------------------------------------------------------------------------
2# Copyright (c) 2020, Arm Limited. All rights reserved.
3#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6#-------------------------------------------------------------------------------
7
8# A CMake script to weaken identical symbols in the target linked libraries to avoid
9# symbol collision at linking time between shared code and other libraries.
10# i.e.: Shared cryptographic code between MCUBoot and secure runtime firmware.
11#
12# INPUT parameters:
13# LIB_LIST - List of all libraries which are linked to the target, and are using
14# the shared code.
15# SHARED_CODE_PATH - The location of the shared code. It could be outside of TF-M repository.
16#
17# OUTPUTS produced by this script:
18# The libraries might be modified by this script, if they contain the same symbols
19# as the shared code.
20
21# TODO: Library search path is modified manually to include path for platform
22# related libraries.
23
24find_program(OBJCOPY arm-none-eabi-objcopy)
25if (OBJCOPY STREQUAL "OBJCOPY-NOTFOUND")
26 message(FATAL_ERROR "WeakenSymbols.cmake: mandatory tool 'arm-none-eabi-objcopy' is missing.")
27endif()
28
29# Macro to collect all libraries where an *.a file is found
30macro(LIBRARY_DIRECTORIES return_list)
31 file(GLOB_RECURSE new_list *.a)
32 set(dir_list "")
33 foreach(file_path ${new_list})
34 get_filename_component(dir_path ${file_path} PATH)
35 set(dir_list ${dir_list} ${dir_path})
36 endforeach()
37 list(REMOVE_DUPLICATES dir_list)
38 set(${return_list} ${dir_list})
39endmacro()
40
41# Create a library search path for static libraries
42LIBRARY_DIRECTORIES(LIBRARY_PATH)
43
44# Workaround to include directories outside of 'secure_fw' folder for platform
45list(APPEND LIBRARY_PATH ${CMAKE_CURRENT_BINARY_DIR}/../platform/ext/accelerator/cc312/crypto_service_cc312 # Musca-B1: libcrypto_service_cc312.a
46 ${CMAKE_CURRENT_BINARY_DIR}/../platform/ext/accelerator
47 ${CMAKE_CURRENT_BINARY_DIR}/../platform
48)
49
50# When invoking the CMake scripts the original list separator(;) is replaced with space.
51# Need to convert back to be able to handle as a list.
52string(REPLACE " " ";" _LIB_LIST ${LIB_LIST})
53
54# Want to weaken all shared symbols in one go, so first concatenate them.
55# There are libraries which might not contain any of these, but it does
56# not cause any issue, the command does not return with error code.
57file(STRINGS ${SHARED_CODE_PATH}/shared_symbols_name.txt SHARED_SYMBOL_NAME)
58foreach(_SYMBOL IN LISTS SHARED_SYMBOL_NAME)
59 list(APPEND ARGUMENT "-W${_SYMBOL}")
60endforeach()
61
62# Iterate over each library and set potentially colliding symbols to be weak
63foreach(LIB IN LISTS _LIB_LIST)
64 find_file(LIB_FULL_PATH "lib${LIB}.a" PATHS ${LIBRARY_PATH} PATH_SUFFIXES Common NO_DEFAULT_PATH)
65 if (NOT ${LIB_FULL_PATH} STREQUAL "LIB_FULL_PATH-NOTFOUND")
66 execute_process(COMMAND ${OBJCOPY} ${ARGUMENT} ${LIB_FULL_PATH}
67 TIMEOUT 120
68 OUTPUT_VARIABLE _RES
69 ERROR_VARIABLE _RES
70 RESULT_VARIABLE _STATUS_CODE
71 OUTPUT_STRIP_TRAILING_WHITESPACE)
72 if (_STATUS_CODE GREATER 0)
73 message(FATAL_ERROR "ERROR: Failed to execute ${OBJCOPY} ${_RES}")
74 endif()
75 endif()
76 unset(LIB_FULL_PATH CACHE)
77endforeach()