[LLD][ELF] Cortex-M Security Extensions (CMSE) Support
authorAmilendra Kodithuwakku <Amilendra.Kodithuwakku@arm.com>
Thu, 6 Jul 2023 09:45:10 +0000 (10:45 +0100)
committerAmilendra Kodithuwakku <Amilendra.Kodithuwakku@arm.com>
Thu, 6 Jul 2023 10:34:07 +0000 (11:34 +0100)
commit9acbab60e59183e04741060984cb684163a73460
treef7841bffcd9e4ffb049064c1ff5b585ef4435052
parent7aafea001282bbe4c7165945c75bd5cc4a82446b
[LLD][ELF] Cortex-M Security Extensions (CMSE) Support

This commit provides linker support for Cortex-M Security Extensions (CMSE).
The specification for this feature can be found in ARM v8-M Security Extensions:
Requirements on Development Tools.

The linker synthesizes a security gateway veneer in a special section;
`.gnu.sgstubs`, when it finds non-local symbols `__acle_se_<entry>` and `<entry>`,
defined relative to the same text section and having the same address. The
address of `<entry>` is retargeted to the starting address of the
linker-synthesized security gateway veneer in section `.gnu.sgstubs`.

In summary, the linker translates input:

```
    .text
  entry:
  __acle_se_entry:
    [entry_code]

```
into:

```
    .section .gnu.sgstubs
  entry:
    SG
    B.W __acle_se_entry

    .text
  __acle_se_entry:
    [entry_code]
```

If addresses of `__acle_se_<entry>` and `<entry>` are not equal, the linker
considers that `<entry>` already defines a secure gateway veneer so does not
synthesize one.

If `--out-implib=<out.lib>` is specified, the linker writes the list of secure
gateway veneers into a CMSE import library `<out.lib>`. The CMSE import library
will have 3 sections: `.symtab`, `.strtab`, `.shstrtab`. For every secure gateway
veneer <entry> at address `<addr>`, `.symtab` contains a `SHN_ABS` symbol `<entry>` with
value `<addr>`.

If `--in-implib=<in.lib>` is specified, the linker reads the existing CMSE import
library `<in.lib>` and preserves the entry function addresses in the resulting
executable and new import library.

Reviewed By: MaskRay, peter.smith

Differential Revision: https://reviews.llvm.org/D139092
22 files changed:
lld/ELF/Arch/ARM.cpp
lld/ELF/Config.h
lld/ELF/Driver.cpp
lld/ELF/InputFiles.cpp
lld/ELF/InputFiles.h
lld/ELF/LinkerScript.cpp
lld/ELF/LinkerScript.h
lld/ELF/MarkLive.cpp
lld/ELF/Options.td
lld/ELF/SymbolTable.h
lld/ELF/SyntheticSections.cpp
lld/ELF/SyntheticSections.h
lld/ELF/Target.h
lld/ELF/Writer.cpp
lld/test/ELF/Inputs/arm-cmse-macros.s [new file with mode: 0644]
lld/test/ELF/aarch64-cmse.s [new file with mode: 0644]
lld/test/ELF/arm-cmse-diagnostics.s [new file with mode: 0644]
lld/test/ELF/arm-cmse-implib.s [new file with mode: 0644]
lld/test/ELF/arm-cmse-keep-sections.s [new file with mode: 0644]
lld/test/ELF/arm-cmse-noveneers.s [new file with mode: 0644]
lld/test/ELF/arm-cmse-secure.s [new file with mode: 0644]
lld/test/ELF/arm-cmse-veneers.s [new file with mode: 0644]