1# Copyright 2022 The Pigweed Authors 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); you may not 4# use this file except in compliance with the License. You may obtain a copy of 5# the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12# License for the specific language governing permissions and limitations under 13# the License. 14 15load("@rules_cc//cc:cc_binary.bzl", "cc_binary") 16load("@rules_cc//cc:cc_library.bzl", "cc_library") 17load("//pw_build:binary_tools.bzl", "pw_elf_to_bin", "pw_elf_to_dump") 18load("//pw_build:pw_linker_script.bzl", "pw_linker_script") 19 20cc_library( 21 name = "header_test", 22 hdrs = ["header_test.h"], 23 includes = ["."], 24) 25 26pw_linker_script( 27 name = "linker_script_test", 28 defines = [ 29 "PW_BOOT_FLASH_BEGIN=0x08000200", 30 "PW_BOOT_FLASH_SIZE=1024K", 31 "PW_BOOT_HEAP_SIZE=112K", 32 "PW_BOOT_MIN_STACK_SIZE=1K", 33 "PW_BOOT_RAM_BEGIN=0x20000000", 34 "PW_BOOT_RAM_SIZE=192K", 35 "PW_BOOT_VECTOR_TABLE_BEGIN=0x08000000", 36 "PW_BOOT_VECTOR_TABLE_SIZE=1M", 37 ], 38 linker_script = "linker_script.ld", 39 deps = [ 40 ":header_test", 41 "//pw_build:must_place", 42 ], 43) 44 45# Use cc_binary to build the test to avoid duplicating the linker script in the 46# command line via implicit deps in pw_cc_binary. 47cc_binary( 48 name = "test_linker_script", 49 srcs = ["test.cc"], 50 copts = ["-Wno-unused-variable"], 51 # Only compatible with platforms that support linker scripts. 52 # This test and its siblings will not link with asan: 53 # ld.lld: error: section '.text' will not fit in region 'VECTOR_TABLE': overflowed by 319296 bytes 54 # ld.lld: error: section '.init' will not fit in region 'VECTOR_TABLE': overflowed by 319319 bytes 55 # ld.lld: error: section '.fini' will not fit in region 'VECTOR_TABLE': overflowed by 319329 bytes 56 # ld.lld: error: section '.plt' will not fit in region 'VECTOR_TABLE': overflowed by 320096 bytes 57 # ld.lld: error: section '.bss' will not fit in region 'RAM': overflowed by 10135216 bytes 58 features = ["-pic"], 59 target_compatible_with = select({ 60 "//pw_toolchain/host_clang:asan_enabled": ["@platforms//:incompatible"], 61 "//pw_toolchain/host_clang:tsan_enabled": ["@platforms//:incompatible"], 62 "//pw_toolchain/host_clang:ubsan_enabled": ["@platforms//:incompatible"], 63 "@platforms//os:linux": [], 64 "//conditions:default": ["@platforms//:incompatible"], 65 }), 66 deps = [":linker_script_test"], 67) 68 69# Use cc_library to depend on the linker script, and then use cc_binary to build 70# the test, verifying that linker scripts can be included via transitive deps. 71cc_library( 72 name = "lib_linker_script", 73 deps = [":linker_script_test"], 74) 75 76cc_binary( 77 name = "test_transitive_linker_script", 78 srcs = ["test.cc"], 79 copts = ["-Wno-unused-variable"], 80 features = ["-pic"], 81 # Only compatible with platforms that support linker scripts. 82 target_compatible_with = select({ 83 "//pw_toolchain/host_clang:asan_enabled": ["@platforms//:incompatible"], 84 "//pw_toolchain/host_clang:tsan_enabled": ["@platforms//:incompatible"], 85 "//pw_toolchain/host_clang:ubsan_enabled": ["@platforms//:incompatible"], 86 "@platforms//os:linux": [], 87 "//conditions:default": ["@platforms//:incompatible"], 88 }), 89 deps = [":lib_linker_script"], 90) 91 92# Verify that the linker script can also be specified directly. 93cc_binary( 94 name = "test_direct_linker_script", 95 srcs = ["test.cc"], 96 additional_linker_inputs = [":linker_script_test"], 97 copts = ["-Wno-unused-variable"], 98 features = ["-pic"], 99 linkopts = ["-T $(execpath :linker_script_test)"], 100 # Only compatible with platforms that support linker scripts. 101 target_compatible_with = select({ 102 "//pw_toolchain/host_clang:asan_enabled": ["@platforms//:incompatible"], 103 "//pw_toolchain/host_clang:tsan_enabled": ["@platforms//:incompatible"], 104 "//pw_toolchain/host_clang:ubsan_enabled": ["@platforms//:incompatible"], 105 "@platforms//os:linux": [], 106 "//conditions:default": ["@platforms//:incompatible"], 107 }), 108) 109 110pw_elf_to_bin( 111 name = "test_bin", 112 bin_out = "test.bin", 113 elf_input = ":test_linker_script", 114) 115 116pw_elf_to_dump( 117 name = "test_dump", 118 dump_out = "test.dump", 119 elf_input = ":test_linker_script", 120) 121