summaryrefslogtreecommitdiffstats
path: root/lib/romlib
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 09:13:47 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 09:13:47 +0000
commit102b0d2daa97dae68d3eed54d8fe37a9cc38a892 (patch)
treebcf648efac40ca6139842707f0eba5a4496a6dd2 /lib/romlib
parentInitial commit. (diff)
downloadarm-trusted-firmware-102b0d2daa97dae68d3eed54d8fe37a9cc38a892.tar.xz
arm-trusted-firmware-102b0d2daa97dae68d3eed54d8fe37a9cc38a892.zip
Adding upstream version 2.8.0+dfsg.upstream/2.8.0+dfsgupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'lib/romlib')
-rw-r--r--lib/romlib/Makefile98
-rwxr-xr-xlib/romlib/gen_combined_bl1_romlib.sh53
-rw-r--r--lib/romlib/init.s36
-rw-r--r--lib/romlib/jmptbl.i44
-rw-r--r--lib/romlib/romlib.ld.S44
-rwxr-xr-xlib/romlib/romlib_generator.py277
-rw-r--r--lib/romlib/templates/jmptbl_entry_function.S6
-rw-r--r--lib/romlib/templates/jmptbl_entry_function_bti.S7
-rw-r--r--lib/romlib/templates/jmptbl_entry_reserved.S6
-rw-r--r--lib/romlib/templates/jmptbl_entry_reserved_bti.S7
-rw-r--r--lib/romlib/templates/jmptbl_glob_var.S9
-rw-r--r--lib/romlib/templates/jmptbl_header.S8
-rw-r--r--lib/romlib/templates/wrapper.S12
-rw-r--r--lib/romlib/templates/wrapper_bti.S13
14 files changed, 620 insertions, 0 deletions
diff --git a/lib/romlib/Makefile b/lib/romlib/Makefile
new file mode 100644
index 0000000..2ff480b
--- /dev/null
+++ b/lib/romlib/Makefile
@@ -0,0 +1,98 @@
+#
+# Copyright (c) 2018-2019, ARM Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+
+AS = $(CROSS_COMPILE)as
+AR = $(CROSS_COMPILE)ar
+LD = $(CROSS_COMPILE)ld
+OC = $(CROSS_COMPILE)objcopy
+CPP = $(CROSS_COMPILE)cpp
+ROMLIB_GEN = ./romlib_generator.py
+BUILD_DIR = $(BUILD_PLAT)/romlib
+LIB_DIR = $(BUILD_PLAT)/lib
+WRAPPER_DIR = $(BUILD_PLAT)/libwrapper
+LIBS = -lmbedtls -lfdt -lc
+INC = $(INCLUDES:-I%=-I../../%)
+PPFLAGS = $(INC) $(DEFINES) -P -x assembler-with-cpp -D__LINKER__ -MD -MP -MT $(BUILD_DIR)/romlib.ld
+OBJS = $(BUILD_DIR)/jmptbl.o $(BUILD_DIR)/init.o
+MAPFILE = $(BUILD_PLAT)/romlib/romlib.map
+
+ifneq ($(PLAT_DIR),)
+ WRAPPER_SOURCES = $(shell $(ROMLIB_GEN) genwrappers -b $(WRAPPER_DIR) --list ../../$(PLAT_DIR)/jmptbl.i)
+ WRAPPER_OBJS = $(WRAPPER_SOURCES:.s=.o)
+endif
+
+V ?= 0
+ifeq ($(V),0)
+ Q := @
+else
+ Q :=
+endif
+
+LDFLAGS := --gc-sections -O1
+ifeq ($(DEBUG),1)
+ LDFLAGS += -Map=$(MAPFILE)
+endif
+
+ifeq (${ARM_ARCH_MINOR},0)
+ ASFLAGS = -march=armv8-a
+else
+ ASFLAGS = -march=armv8.${ARM_ARCH_MINOR}-a
+endif
+
+.PHONY: all clean distclean
+
+all: $(BUILD_DIR)/romlib.bin $(LIB_DIR)/libwrappers.a
+
+%.o: %.s
+ @echo " AS $@"
+ $(Q)$(AS) $(ASFLAGS) -o $@ $<
+
+$(BUILD_DIR)/%.o: %.s
+ @echo " AS $@"
+ $(Q)$(AS) $(ASFLAGS) -o $@ $<
+
+$(BUILD_DIR)/romlib.ld: romlib.ld.S
+ @echo " PP $@"
+ $(Q)$(CPP) $(PPFLAGS) -o $@ romlib.ld.S
+
+$(BUILD_DIR)/romlib.elf: $(OBJS) $(BUILD_DIR)/romlib.ld
+ @echo " LD $@"
+ $(Q)$(LD) -T $(BUILD_DIR)/romlib.ld -L$(LIB_DIR) $(LDFLAGS) -o $@ $(OBJS) $(LIBS)
+
+$(BUILD_DIR)/romlib.bin: $(BUILD_DIR)/romlib.elf
+ @echo " BIN $@"
+ $(Q)$(OC) -O binary $(BUILD_DIR)/romlib.elf $@
+
+$(WRAPPER_DIR)/jmpvar.s: $(BUILD_DIR)/romlib.elf
+ @echo " VAR $@"
+ $(Q)$(ROMLIB_GEN) genvar --output $@ $<
+
+$(LIB_DIR)/libwrappers.a: $(WRAPPER_DIR)/jmpvar.o $(WRAPPER_OBJS)
+ @echo " AR $@"
+ $(Q)$(AR) -rc $@ $(WRAPPER_DIR)/jmpvar.o $(WRAPPER_OBJS)
+
+$(BUILD_DIR)/jmptbl.i: ../../$(PLAT_DIR)/jmptbl.i
+ @echo " PRE $@"
+ $(Q)$(ROMLIB_GEN) pre --output $@ --deps $(BUILD_DIR)/jmptbl.d $<
+
+$(BUILD_DIR)/wrappers.stamp: $(BUILD_DIR)/jmptbl.i
+ @echo " WRP $<"
+ $(Q)$(ROMLIB_GEN) genwrappers --bti=$(ENABLE_BTI) -b $(WRAPPER_DIR) $<
+ @touch $@
+
+$(WRAPPER_SOURCES): $(BUILD_DIR)/wrappers.stamp
+
+$(WRAPPER_OBJS): $(WRAPPER_SOURCES) $(BUILD_DIR)/wrappers.stamp
+
+$(BUILD_DIR)/jmptbl.s: $(BUILD_DIR)/jmptbl.i
+ @echo " TBL $@"
+ $(Q)$(ROMLIB_GEN) gentbl --output $@ --bti=$(ENABLE_BTI) $<
+
+clean:
+ @rm -f $(BUILD_DIR)/*
+
+-include $(BUILD_DIR)/romlib.d
+-include $(BUILD_DIR)/jmptbl.d
diff --git a/lib/romlib/gen_combined_bl1_romlib.sh b/lib/romlib/gen_combined_bl1_romlib.sh
new file mode 100755
index 0000000..1e3f73a
--- /dev/null
+++ b/lib/romlib/gen_combined_bl1_romlib.sh
@@ -0,0 +1,53 @@
+#!/bin/sh
+# Copyright (c) 2018, ARM Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+
+set -e
+
+output="bl1_romlib.bin"
+
+# Set trap for removing temporary file
+trap 'r=$?;rm -f $bin_path/$$.tmp;exit $r' EXIT HUP QUIT INT TERM
+
+# Read input parameters
+for i
+do
+ case $i in
+ -o)
+ output=$2
+ shift 2
+ ;;
+ --)
+ shift
+ break
+ ;;
+ -*)
+ echo usage: gen_combined_bl1_romlib.sh [-o output] path_to_build_directory >&2
+ ;;
+ esac
+done
+
+
+bin_path=$1
+romlib_path=$1/romlib
+bl1_file="$1/bl1/bl1.elf"
+romlib_file="$1/romlib/romlib.elf"
+bl1_end=""
+romlib_begin=""
+
+# Get address of __BL1_ROM_END__
+bl1_end=`nm -a "$bl1_file" |
+awk '$3 == "__BL1_ROM_END__" {print "0x"$1}'`
+
+# Get start address of romlib "text" section
+romlib_begin=`nm -a "$romlib_file" |
+awk '$3 == ".text" {print "0x"$1}'`
+
+# Character "U" will be read as "55" in hex when it is
+# concatenated with bl1.bin. Generate combined BL1 and ROMLIB
+# binary with filler bytes for juno
+(cat $bin_path/bl1.bin
+ yes U | sed $(($romlib_begin - $bl1_end))q | tr -d '\n'
+ cat $bin_path/romlib/romlib.bin) > $bin_path/$$.tmp &&
+mv $bin_path/$$.tmp $bin_path/$output
diff --git a/lib/romlib/init.s b/lib/romlib/init.s
new file mode 100644
index 0000000..7d97e4d
--- /dev/null
+++ b/lib/romlib/init.s
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+ .globl rom_lib_init
+ .extern __DATA_RAM_START__, __DATA_ROM_START__, __DATA_RAM_END__
+ .extern memset, memcpy
+
+rom_lib_init:
+ cmp w0, #1
+ mov w0, #0
+ b.le 1f
+ ret
+
+1: stp x29, x30, [sp, #-16]!
+ adrp x0, __DATA_RAM_START__
+ adrp x1, __DATA_ROM_START__
+ add x1, x1, :lo12:__DATA_ROM_START__
+ adrp x2, __DATA_RAM_END__
+ add x2, x2, :lo12:__DATA_RAM_END__
+ sub x2, x2, x0
+ bl memcpy
+
+ adrp x0,__BSS_START__
+ add x0, x0, :lo12:__BSS_START__
+ mov x1, #0
+ adrp x2, __BSS_END__
+ add x2, x2, :lo12:__BSS_END__
+ sub x2, x2, x0
+ bl memset
+ ldp x29, x30, [sp], #16
+
+ mov w0, #1
+ ret
diff --git a/lib/romlib/jmptbl.i b/lib/romlib/jmptbl.i
new file mode 100644
index 0000000..33710f5
--- /dev/null
+++ b/lib/romlib/jmptbl.i
@@ -0,0 +1,44 @@
+#
+# Copyright (c) 2018-2020, ARM Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+# Format:
+# lib function [patch]
+# Add "patch" at the end of the line to patch a function. For example:
+# mbedtls mbedtls_memory_buffer_alloc_init patch
+# Holes can be introduced in the table by using a special keyword "reserved".
+# Example:
+# reserved reserved
+# The jump table will contain an invalid instruction instead of branch
+
+rom rom_lib_init
+fdt fdt_getprop_namelen
+fdt fdt_setprop_inplace
+fdt fdt_check_header
+fdt fdt_node_offset_by_compatible
+fdt fdt_setprop_inplace_namelen_partial
+mbedtls mbedtls_asn1_get_alg
+mbedtls mbedtls_asn1_get_alg_null
+mbedtls mbedtls_asn1_get_bitstring_null
+mbedtls mbedtls_asn1_get_bool
+mbedtls mbedtls_asn1_get_int
+mbedtls mbedtls_asn1_get_tag
+mbedtls mbedtls_free
+mbedtls mbedtls_md
+mbedtls mbedtls_md_get_size
+mbedtls mbedtls_memory_buffer_alloc_init
+mbedtls mbedtls_oid_get_md_alg
+mbedtls mbedtls_oid_get_numeric_string
+mbedtls mbedtls_oid_get_pk_alg
+mbedtls mbedtls_oid_get_sig_alg
+mbedtls mbedtls_pk_free
+mbedtls mbedtls_pk_init
+mbedtls mbedtls_pk_parse_subpubkey
+mbedtls mbedtls_pk_verify_ext
+mbedtls mbedtls_platform_set_snprintf
+mbedtls mbedtls_x509_get_rsassa_pss_params
+mbedtls mbedtls_x509_get_sig_alg
+mbedtls mbedtls_md_info_from_type
+c exit
+c atexit
diff --git a/lib/romlib/romlib.ld.S b/lib/romlib/romlib.ld.S
new file mode 100644
index 0000000..2aac4ad
--- /dev/null
+++ b/lib/romlib/romlib.ld.S
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <lib/xlat_tables/xlat_tables_defs.h>
+#include <platform_def.h>
+
+MEMORY {
+ ROM (rx): ORIGIN = ROMLIB_RO_BASE, LENGTH = ROMLIB_RO_LIMIT - ROMLIB_RO_BASE
+ RAM (rwx): ORIGIN = ROMLIB_RW_BASE, LENGTH = ROMLIB_RW_END - ROMLIB_RW_BASE
+}
+
+OUTPUT_FORMAT(PLATFORM_LINKER_FORMAT)
+OUTPUT_ARCH(PLATFORM_LINKER_ARCH)
+ENTRY(jmptbl)
+
+SECTIONS
+{
+ . = ROMLIB_RO_BASE;
+ .text : {
+ *jmptbl.o(.text)
+ *(.text*)
+ *(.rodata*)
+ } >ROM
+
+ __DATA_ROM_START__ = LOADADDR(.data);
+
+ .data : {
+ __DATA_RAM_START__ = .;
+ *(.data*)
+ __DATA_RAM_END__ = .;
+ } >RAM AT>ROM
+
+ __DATA_SIZE__ = SIZEOF(.data);
+
+ .bss : {
+ __BSS_START__ = .;
+ *(.bss*)
+ __BSS_END__ = .;
+ } >RAM
+ __BSS_SIZE__ = SIZEOF(.bss);
+}
diff --git a/lib/romlib/romlib_generator.py b/lib/romlib/romlib_generator.py
new file mode 100755
index 0000000..0682dd4
--- /dev/null
+++ b/lib/romlib/romlib_generator.py
@@ -0,0 +1,277 @@
+#!/usr/bin/env python3
+# Copyright (c) 2019, Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+
+"""
+This module contains a set of classes and a runner that can generate code for the romlib module
+based on the templates in the 'templates' directory.
+"""
+
+import argparse
+import os
+import re
+import subprocess
+import string
+import sys
+
+class IndexFileParser:
+ """
+ Parses the contents of the index file into the items and dependencies variables. It
+ also resolves included files in the index files recursively with circular inclusion detection.
+ """
+
+ def __init__(self):
+ self.items = []
+ self.dependencies = {}
+ self.include_chain = []
+
+ def add_dependency(self, parent, dependency):
+ """ Adds a dependency into the dependencies variable. """
+ if parent in self.dependencies:
+ self.dependencies[parent].append(dependency)
+ else:
+ self.dependencies[parent] = [dependency]
+
+ def get_dependencies(self, parent):
+ """ Gets all the recursive dependencies of a parent file. """
+ parent = os.path.normpath(parent)
+ if parent in self.dependencies:
+ direct_deps = self.dependencies[parent]
+ deps = direct_deps
+ for direct_dep in direct_deps:
+ deps += self.get_dependencies(direct_dep)
+ return deps
+
+ return []
+
+ def parse(self, file_name):
+ """ Opens and parses index file. """
+ file_name = os.path.normpath(file_name)
+
+ if file_name not in self.include_chain:
+ self.include_chain.append(file_name)
+ self.dependencies[file_name] = []
+ else:
+ raise Exception("Circular dependency detected: " + file_name)
+
+ with open(file_name, "r") as index_file:
+ for line in index_file.readlines():
+ line_elements = line.split()
+
+ if line.startswith("#") or not line_elements:
+ # Comment or empty line
+ continue
+
+ if line_elements[0] == "reserved":
+ # Reserved slot in the jump table
+ self.items.append({"type": "reserved"})
+ elif line_elements[0] == "include" and len(line_elements) > 1:
+ # Include other index file
+ included_file = os.path.normpath(line_elements[1])
+ self.add_dependency(file_name, included_file)
+ self.parse(included_file)
+ elif len(line_elements) > 1:
+ # Library function
+ library_name = line_elements[0]
+ function_name = line_elements[1]
+ patch = bool(len(line_elements) > 2 and line_elements[2] == "patch")
+
+ self.items.append({"type": "function", "library_name": library_name,
+ "function_name": function_name, "patch": patch})
+ else:
+ raise Exception("Invalid line: '" + line + "'")
+
+ self.include_chain.pop()
+
+class RomlibApplication:
+ """ Base class of romlib applications. """
+ TEMPLATE_DIR = os.path.dirname(os.path.realpath(__file__)) + "/templates/"
+
+ def __init__(self, prog):
+ self.args = argparse.ArgumentParser(prog=prog, description=self.__doc__)
+ self.config = None
+
+ def parse_arguments(self, argv):
+ """ Parses the arguments that should come from the command line arguments. """
+ self.config = self.args.parse_args(argv)
+
+ def build_template(self, name, mapping=None, remove_comment=False):
+ """
+ Loads a template and builds it with the defined mapping. Template paths are always relative
+ to this script.
+ """
+
+ with open(self.TEMPLATE_DIR + name, "r") as template_file:
+ if remove_comment:
+ # Removing copyright comment to make the generated code more readable when the
+ # template is inserted multiple times into the output.
+ template_lines = template_file.readlines()
+ end_of_comment_line = 0
+ for index, line in enumerate(template_lines):
+ if line.find("*/") != -1:
+ end_of_comment_line = index
+ break
+ template_data = "".join(template_lines[end_of_comment_line + 1:])
+ else:
+ template_data = template_file.read()
+
+ template = string.Template(template_data)
+ return template.substitute(mapping)
+
+class IndexPreprocessor(RomlibApplication):
+ """ Removes empty and comment lines from the index file and resolves includes. """
+
+ def __init__(self, prog):
+ RomlibApplication.__init__(self, prog)
+
+ self.args.add_argument("-o", "--output", help="Output file", metavar="output",
+ default="jmpvar.s")
+ self.args.add_argument("--deps", help="Dependency file")
+ self.args.add_argument("file", help="Input file")
+
+ def main(self):
+ """
+ After parsing the input index file it generates a clean output with all includes resolved.
+ Using --deps option it also outputs the dependencies in makefile format like gcc's with -M.
+ """
+
+ index_file_parser = IndexFileParser()
+ index_file_parser.parse(self.config.file)
+
+ with open(self.config.output, "w") as output_file:
+ for item in index_file_parser.items:
+ if item["type"] == "function":
+ patch = "\tpatch" if item["patch"] else ""
+ output_file.write(
+ item["library_name"] + "\t" + item["function_name"] + patch + "\n")
+ else:
+ output_file.write("reserved\n")
+
+ if self.config.deps:
+ with open(self.config.deps, "w") as deps_file:
+ deps = [self.config.file] + index_file_parser.get_dependencies(self.config.file)
+ deps_file.write(self.config.output + ": " + " \\\n".join(deps) + "\n")
+
+class TableGenerator(RomlibApplication):
+ """ Generates the jump table by parsing the index file. """
+
+ def __init__(self, prog):
+ RomlibApplication.__init__(self, prog)
+
+ self.args.add_argument("-o", "--output", help="Output file", metavar="output",
+ default="jmpvar.s")
+ self.args.add_argument("--bti", help="Branch Target Identification", type=int)
+ self.args.add_argument("file", help="Input file")
+
+ def main(self):
+ """
+ Inserts the jmptbl definition and the jump entries into the output file. Also can insert
+ BTI related code before entries if --bti option set. It can output a dependency file of the
+ included index files. This can be directly included in makefiles.
+ """
+
+ index_file_parser = IndexFileParser()
+ index_file_parser.parse(self.config.file)
+
+ with open(self.config.output, "w") as output_file:
+ output_file.write(self.build_template("jmptbl_header.S"))
+ bti = "_bti" if self.config.bti == 1 else ""
+
+ for item in index_file_parser.items:
+ template_name = "jmptbl_entry_" + item["type"] + bti + ".S"
+ output_file.write(self.build_template(template_name, item, True))
+
+class WrapperGenerator(RomlibApplication):
+ """
+ Generates a wrapper function for each entry in the index file except for the ones that contain
+ the keyword patch. The generated wrapper file is called <lib>_<fn_name>.s.
+ """
+
+ def __init__(self, prog):
+ RomlibApplication.__init__(self, prog)
+
+ self.args.add_argument("-b", help="Build directory", default=".", metavar="build")
+ self.args.add_argument("--bti", help="Branch Target Identification", type=int)
+ self.args.add_argument("--list", help="Only list assembly files", action="store_true")
+ self.args.add_argument("file", help="Input file")
+
+ def main(self):
+ """
+ Iterates through the items in the parsed index file and builds the template for each entry.
+ """
+
+ index_file_parser = IndexFileParser()
+ index_file_parser.parse(self.config.file)
+
+ bti = "_bti" if self.config.bti == 1 else ""
+ function_offset = 0
+ files = []
+
+ for item_index in range(0, len(index_file_parser.items)):
+ item = index_file_parser.items[item_index]
+
+ if item["type"] == "reserved" or item["patch"]:
+ continue
+
+ asm = self.config.b + "/" + item["function_name"] + ".s"
+ if self.config.list:
+ # Only listing files
+ files.append(asm)
+ else:
+ with open(asm, "w") as asm_file:
+ # The jump instruction is 4 bytes but BTI requires and extra instruction so
+ # this makes it 8 bytes per entry.
+ function_offset = item_index * (8 if self.config.bti else 4)
+
+ item["function_offset"] = function_offset
+ asm_file.write(self.build_template("wrapper" + bti + ".S", item))
+
+ if self.config.list:
+ print(" ".join(files))
+
+class VariableGenerator(RomlibApplication):
+ """ Generates the jump table global variable with the absolute address in ROM. """
+
+ def __init__(self, prog):
+ RomlibApplication.__init__(self, prog)
+
+ self.args.add_argument("-o", "--output", help="Output file", metavar="output",
+ default="jmpvar.s")
+ self.args.add_argument("file", help="Input file")
+
+ def main(self):
+ """
+ Runs nm -a command on the input file and inserts the address of the .text section into the
+ template as the ROM address of the jmp_table.
+ """
+ symbols = subprocess.check_output(["nm", "-a", self.config.file])
+
+ matching_symbol = re.search("([0-9A-Fa-f]+) . \\.text", str(symbols))
+ if not matching_symbol:
+ raise Exception("No '.text' section was found in %s" % self.config.file)
+
+ mapping = {"jmptbl_address": matching_symbol.group(1)}
+
+ with open(self.config.output, "w") as output_file:
+ output_file.write(self.build_template("jmptbl_glob_var.S", mapping))
+
+if __name__ == "__main__":
+ APPS = {"genvar": VariableGenerator, "pre": IndexPreprocessor,
+ "gentbl": TableGenerator, "genwrappers": WrapperGenerator}
+
+ if len(sys.argv) < 2 or sys.argv[1] not in APPS:
+ print("usage: romlib_generator.py [%s] [args]" % "|".join(APPS.keys()), file=sys.stderr)
+ sys.exit(1)
+
+ APP = APPS[sys.argv[1]]("romlib_generator.py " + sys.argv[1])
+ APP.parse_arguments(sys.argv[2:])
+ try:
+ APP.main()
+ sys.exit(0)
+ except FileNotFoundError as file_not_found_error:
+ print(file_not_found_error, file=sys.stderr)
+ except subprocess.CalledProcessError as called_process_error:
+ print(called_process_error.output, file=sys.stderr)
+
+ sys.exit(1)
diff --git a/lib/romlib/templates/jmptbl_entry_function.S b/lib/romlib/templates/jmptbl_entry_function.S
new file mode 100644
index 0000000..a0f8456
--- /dev/null
+++ b/lib/romlib/templates/jmptbl_entry_function.S
@@ -0,0 +1,6 @@
+/*
+ * Copyright (c) 2019, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+ b ${function_name}
diff --git a/lib/romlib/templates/jmptbl_entry_function_bti.S b/lib/romlib/templates/jmptbl_entry_function_bti.S
new file mode 100644
index 0000000..d96ee94
--- /dev/null
+++ b/lib/romlib/templates/jmptbl_entry_function_bti.S
@@ -0,0 +1,7 @@
+/*
+ * Copyright (c) 2019, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+ bti j
+ b ${function_name}
diff --git a/lib/romlib/templates/jmptbl_entry_reserved.S b/lib/romlib/templates/jmptbl_entry_reserved.S
new file mode 100644
index 0000000..a9b5f18
--- /dev/null
+++ b/lib/romlib/templates/jmptbl_entry_reserved.S
@@ -0,0 +1,6 @@
+/*
+ * Copyright (c) 2019, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+ b .
diff --git a/lib/romlib/templates/jmptbl_entry_reserved_bti.S b/lib/romlib/templates/jmptbl_entry_reserved_bti.S
new file mode 100644
index 0000000..a9f0375
--- /dev/null
+++ b/lib/romlib/templates/jmptbl_entry_reserved_bti.S
@@ -0,0 +1,7 @@
+/*
+ * Copyright (c) 2019, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+ bti j
+ b .
diff --git a/lib/romlib/templates/jmptbl_glob_var.S b/lib/romlib/templates/jmptbl_glob_var.S
new file mode 100644
index 0000000..d306512
--- /dev/null
+++ b/lib/romlib/templates/jmptbl_glob_var.S
@@ -0,0 +1,9 @@
+/*
+ * Copyright (c) 2019, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+ .data
+ .globl jmptbl
+ .align 4
+jmptbl: .quad 0x${jmptbl_address}
diff --git a/lib/romlib/templates/jmptbl_header.S b/lib/romlib/templates/jmptbl_header.S
new file mode 100644
index 0000000..72b8ce5
--- /dev/null
+++ b/lib/romlib/templates/jmptbl_header.S
@@ -0,0 +1,8 @@
+/*
+ * Copyright (c) 2019, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+ .text
+ .globl jmptbl
+jmptbl:
diff --git a/lib/romlib/templates/wrapper.S b/lib/romlib/templates/wrapper.S
new file mode 100644
index 0000000..734a68a
--- /dev/null
+++ b/lib/romlib/templates/wrapper.S
@@ -0,0 +1,12 @@
+/*
+ * Copyright (c) 2019, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+ .globl ${function_name}
+${function_name}:
+ ldr x17, =jmptbl
+ mov x16, #${function_offset}
+ ldr x17, [x17]
+ add x16, x16, x17
+ br x16
diff --git a/lib/romlib/templates/wrapper_bti.S b/lib/romlib/templates/wrapper_bti.S
new file mode 100644
index 0000000..ba9b11c
--- /dev/null
+++ b/lib/romlib/templates/wrapper_bti.S
@@ -0,0 +1,13 @@
+/*
+ * Copyright (c) 2019, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+ .globl ${function_name}
+${function_name}:
+ bti jc
+ ldr x17, =jmptbl
+ mov x16, #${function_offset}
+ ldr x17, [x17]
+ add x16, x16, x17
+ br x16