[bazel] Migrate all linker scripts to use ld_library()

Signed-off-by: Miguel Young de la Sota <mcyoung@google.com>
diff --git a/hw/top_earlgrey/sw/autogen/BUILD b/hw/top_earlgrey/sw/autogen/BUILD
index b731307..f53520a 100644
--- a/hw/top_earlgrey/sw/autogen/BUILD
+++ b/hw/top_earlgrey/sw/autogen/BUILD
@@ -2,6 +2,8 @@
 # Licensed under the Apache License, Version 2.0, see LICENSE for details.
 # SPDX-License-Identifier: Apache-2.0
 
+load("//rules:linker.bzl", "ld_library")
+
 package(default_visibility = ["//visibility:public"])
 
 cc_library(
@@ -15,9 +17,9 @@
     ],
 )
 
-cc_library(
-    name = "linker_script",
-    deps = ["top_earlgrey_memory.ld"],
+ld_library(
+    name = "top_earlgrey_memory",
+    fragments = ["top_earlgrey_memory.ld"],
 )
 
 filegroup(
diff --git a/sw/device/BUILD b/sw/device/BUILD
index 9d194a1..0e991cd 100644
--- a/sw/device/BUILD
+++ b/sw/device/BUILD
@@ -2,9 +2,11 @@
 # Licensed under the Apache License, Version 2.0, see LICENSE for details.
 # SPDX-License-Identifier: Apache-2.0
 
+load("//rules:linker.bzl", "ld_library")
+
 package(default_visibility = ["//visibility:public"])
 
-cc_library(
+ld_library(
     name = "info_sections",
-    deps = ["info_sections.ld"],
+    fragments = ["info_sections.ld"],
 )
diff --git a/sw/device/lib/testing/test_framework/BUILD b/sw/device/lib/testing/test_framework/BUILD
index c98e695..02bdcda 100644
--- a/sw/device/lib/testing/test_framework/BUILD
+++ b/sw/device/lib/testing/test_framework/BUILD
@@ -3,6 +3,7 @@
 # SPDX-License-Identifier: Apache-2.0
 
 load("//rules:opentitan.bzl", "OPENTITAN_CPU")
+load("//rules:linker.bzl", "ld_library")
 
 package(default_visibility = ["//visibility:public"])
 
@@ -75,6 +76,15 @@
     ],
 )
 
+ld_library(
+    name = "linker_script",
+    script = "ottf.ld",
+    deps = [
+        "//hw/top_earlgrey/sw/autogen:top_earlgrey_memory",
+        "//sw/device:info_sections",
+    ],
+)
+
 cc_library(
     name = "ottf_start",
     srcs = [
@@ -90,14 +100,9 @@
         "-nostdlib",
         "-ffreestanding",
     ],
-    linkopts = [
-        "-T $(location ottf.ld)",
-    ],
     target_compatible_with = [OPENTITAN_CPU],
     deps = [
-        "ottf.ld",
-        "//hw/top_earlgrey/sw/autogen:linker_script",
-        "//sw/device:info_sections",
+        ":linker_script",
         "//sw/device/lib/base:csr",
         "//sw/device/lib/base:macros",
         "//sw/device/lib/crt",
diff --git a/sw/device/lib/testing/test_rom/BUILD b/sw/device/lib/testing/test_rom/BUILD
index 70df9eb..04cf63a 100644
--- a/sw/device/lib/testing/test_rom/BUILD
+++ b/sw/device/lib/testing/test_rom/BUILD
@@ -5,6 +5,7 @@
 load("//rules:opentitan.bzl", "OPENTITAN_CPU", "opentitan_rom_binary")
 load("//rules:opentitan_test.bzl", "opentitan_functest")
 load("//rules:autogen.bzl", "autogen_chip_info")
+load("//rules:linker.bzl", "ld_library")
 
 package(default_visibility = ["//visibility:public"])
 
@@ -12,13 +13,19 @@
     name = "chip_info",
 )
 
+ld_library(
+    name = "linker_script",
+    script = "test_rom.ld",
+    deps = [
+        "//hw/top_earlgrey/sw/autogen:top_earlgrey_memory",
+        "//sw/device:info_sections",
+    ],
+)
+
 opentitan_rom_binary(
     name = "test_rom",
-    linkopts = [
-        "-T $(location test_rom.ld)",
-    ],
     deps = [
-        "test_rom.ld",
+        ":linker_script",
         ":test_rom_lib",
         "//sw/device/lib/base:mmio",
     ],
@@ -42,9 +49,7 @@
         "//hw/ip/sram_ctrl/data:sram_ctrl_regs",
         "//hw/top_earlgrey/ip/ast/data:ast_regs",
         "//hw/top_earlgrey/ip/sensor_ctrl/data:sensor_ctrl_regs",
-        "//hw/top_earlgrey/sw/autogen:linker_script",
         "//hw/top_earlgrey/sw/autogen:top_earlgrey",
-        "//sw/device:info_sections",
         "//sw/device/lib:ibex_peri",
         "//sw/device/lib:pinmux",
         "//sw/device/lib/base:mmio",
diff --git a/sw/device/silicon_creator/lib/base/BUILD b/sw/device/silicon_creator/lib/base/BUILD
index 7d8eeea..affeb56 100644
--- a/sw/device/silicon_creator/lib/base/BUILD
+++ b/sw/device/silicon_creator/lib/base/BUILD
@@ -4,11 +4,11 @@
 
 load("//rules:opentitan.bzl", "OPENTITAN_CPU")
 load("//rules:cross_platform.bzl", "dual_cc_device_library_of", "dual_cc_library", "dual_inputs")
+load("//rules:linker.bzl", "ld_library")
 
 package(default_visibility = ["//visibility:public"])
 
 exports_files([
-    "static_critical.ld",
     "mock_csr.h",
     "mock_csr.cc",
     "mock_csr_test.cc",
@@ -49,3 +49,8 @@
         "@googletest//:gtest_main",
     ],
 )
+
+ld_library(
+    name = "static_critical_sections",
+    fragments = ["static_critical.ld"],
+)
diff --git a/sw/device/silicon_creator/mask_rom/BUILD b/sw/device/silicon_creator/mask_rom/BUILD
index fd268cc..6669e3f 100644
--- a/sw/device/silicon_creator/mask_rom/BUILD
+++ b/sw/device/silicon_creator/mask_rom/BUILD
@@ -3,6 +3,7 @@
 # SPDX-License-Identifier: Apache-2.0
 
 load("//rules:opentitan.bzl", "OPENTITAN_CPU", "opentitan_rom_binary")
+load("//rules:linker.bzl", "ld_library")
 load(
     "//rules:opentitan_test.bzl",
     "cw310_params",
@@ -59,6 +60,16 @@
     ],
 )
 
+ld_library(
+    name = "linker_script",
+    script = "mask_rom.ld",
+    deps = [
+        "//hw/top_earlgrey/sw/autogen:top_earlgrey_memory",
+        "//sw/device:info_sections",
+        "//sw/device/silicon_creator/lib/base:static_critical_sections",
+    ],
+)
+
 opentitan_rom_binary(
     name = "mask_rom",
     srcs = [
@@ -69,12 +80,9 @@
     # Mask ROM does not permit the DV simulation backdoor logging mechanism that
     # test ROM does, for obvious security reasons.
     extract_sw_logs_db = False,
-    linkopts = [
-        "-T $(location mask_rom.ld)",
-    ],
     deps = [
-        "mask_rom.ld",
         ":boot_policy",
+        ":linker_script",
         ":mask_rom_epmp",
         ":primitive_bootstrap",
         ":sigverify_keys",
@@ -87,9 +95,7 @@
         "//hw/top_earlgrey/ip/ast/data:ast_regs",
         "//hw/top_earlgrey/ip/pwrmgr/data/autogen:pwrmgr_regs",
         "//hw/top_earlgrey/ip/sensor_ctrl/data:sensor_ctrl_regs",
-        "//hw/top_earlgrey/sw/autogen:linker_script",
         "//hw/top_earlgrey/sw/autogen:top_earlgrey",
-        "//sw/device:info_sections",
         "//sw/device/lib/arch:device",
         "//sw/device/lib/base:bitfield",
         "//sw/device/lib/base:csr",
@@ -105,7 +111,6 @@
         "//sw/device/silicon_creator/lib:manifest",
         "//sw/device/silicon_creator/lib:shutdown",
         "//sw/device/silicon_creator/lib/base:sec_mmio",
-        "//sw/device/silicon_creator/lib/base:static_critical.ld",
         "//sw/device/silicon_creator/lib/drivers:flash_ctrl",
         "//sw/device/silicon_creator/lib/drivers:keymgr",
         "//sw/device/silicon_creator/lib/drivers:lifecycle",
@@ -142,9 +147,6 @@
         "mask_rom_epmp_test.c",
         "mask_rom_start.S",
     ],
-    linkopts = [
-        "-T $(location mask_rom.ld)",
-    ],
     # This test doesn't use the OTTF.
     ottf = [],
     targets = ["verilator"],  # Can only run in verilator right now.
@@ -155,7 +157,7 @@
     # Verilator or CW310 params).
     test_in_rom = True,
     deps = [
-        "mask_rom.ld",
+        ":linker_script",
         ":mask_rom_epmp",
         "//hw/ip/aon_timer/data:aon_timer_regs",
         "//hw/ip/csrng/data:csrng_regs",
@@ -167,9 +169,7 @@
         "//hw/top_earlgrey/ip/ast/data:ast_regs",
         "//hw/top_earlgrey/ip/pwrmgr/data/autogen:pwrmgr_regs",
         "//hw/top_earlgrey/ip/sensor_ctrl/data:sensor_ctrl_regs",
-        "//hw/top_earlgrey/sw/autogen:linker_script",
         "//hw/top_earlgrey/sw/autogen:top_earlgrey",
-        "//sw/device:info_sections",
         "//sw/device/lib:pinmux",
         "//sw/device/lib/arch:device",
         "//sw/device/lib/base:abs_mmio",
@@ -186,7 +186,6 @@
         "//sw/device/silicon_creator/lib:epmp_test_unlock",
         "//sw/device/silicon_creator/lib:irq_asm",
         "//sw/device/silicon_creator/lib/base:sec_mmio",
-        "//sw/device/silicon_creator/lib/base:static_critical.ld",
         "//sw/device/silicon_creator/lib/drivers:flash_ctrl",
         "//sw/device/silicon_creator/lib/drivers:uart",
     ],