llvm in a bare metal environment

39
Hafiz Abid Qadeer LLVM in a Bare Metal Environment October 6, 2020

Upload: others

Post on 29-Oct-2021

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: LLVM in a Bare Metal Environment

Hafiz Abid Qadeer

LLVM in a Bare Metal Environment

October 6, 2020

Page 2: LLVM in a Bare Metal Environment

Restricted © 2020 Mentor Graphics Corporation

Overview

2

BackgroundBuilding a Bare Metal Toolchain

Result

Page 3: LLVM in a Bare Metal Environment

Restricted © 2020 Mentor Graphics Corporation

Why is Bare Metal Different

3

Hardware

User Program

Loading

Dynamic linking

Process &Thread Control

IO

Page 4: LLVM in a Bare Metal Environment

Restricted © 2020 Mentor Graphics Corporation

Why is Bare Metal Different

Static Libraries

Customized memory layout

No thread or process control

No Filesystem or console IO

4

Hardware

User Program

Page 5: LLVM in a Bare Metal Environment

Restricted © 2020 Mentor Graphics Corporation

Bare Metal in LLVM

Not exercised enough

5

Baremetal.cpp (added in 2017)("-lclang_rt.builtins-" + getArchName() + ".a"));

Baremetal.cpp (Fixed in 2020)("-lclang_rt.builtins-" + getArchName()));

Page 6: LLVM in a Bare Metal Environment

Restricted © 2020 Mentor Graphics Corporation

Components

6

Compiler Driver Compiler Proper Runtime Linker

Page 7: LLVM in a Bare Metal Environment

Restricted © 2020 Mentor Graphics Corporation

Clang Driver

Responsibilities

Baremetal.cpp

Only support ARM at the moment

Lacks Multilib support

7

Page 8: LLVM in a Bare Metal Environment

Restricted © 2020 Mentor Graphics Corporation

Runtime

8

Runtime

libc++

libc++abi

libunwind

C library compiler-rt

Page 9: LLVM in a Bare Metal Environment

Restricted © 2020 Mentor Graphics Corporation

Build Order

9

Host Tools

C

Library

compiler-rt

libc++

headerslibunwind libc++abi libc++

Page 10: LLVM in a Bare Metal Environment

Restricted © 2020 Mentor Graphics Corporation

Standalone Build

$ cd /path/to/build/

$ cmake …. $src/$library

$ make

$ make install

$ make check-xyz

10

Page 11: LLVM in a Bare Metal Environment

Restricted © 2020 Mentor Graphics Corporation

Building Runtime

CMAKE_C_COMPILER

CMAKE_ASM_COMPILER

CMAKE_CXX_COMPILER

CMAKE_C_FLAGS=“--target=… -march=…"

CMAKE_CXX_FLAGS

CMAKE_ASM_FLAGS

CMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY

11

Page 12: LLVM in a Bare Metal Environment

Restricted © 2020 Mentor Graphics Corporation

Building compiler-rt

cmake

-DCOMPILER_RT_BAREMETAL_BUILD=ON

-DCOMPILER_RT_BUILD_CRT=…

-DCOMPILER_RT_DEFAULT_TARGET_ONLY=On

-DCOMPILER_RT_BUILD_BUILTINS=ON

-DCOMPILER_RT_BUILD_SANITIZERS=OFF

-DCOMPILER_RT_BUILD_XRAY=OFF

-DCOMPILER_RT_BUILD_LIBFUZZER=OFF

-DCOMPILER_RT_BUILD_PROFILE=OFF

$SRC/compiler-rt

12

Page 13: LLVM in a Bare Metal Environment

Restricted © 2020 Mentor Graphics Corporation

Building libunwind

cmake

-DLIBUNWIND_ENABLE_SHARED=OFF

-DLIBCXX_ENABLE_SHARED=OFF

-DLIBUNWIND_IS_BAREMETAL=ON

-DLIBUNWIND_ENABLE_THREADS=OFF

-DLIBUNWIND_USE_COMPILER_RT=ON

….

$SRC/libunwind

13

Page 14: LLVM in a Bare Metal Environment

Restricted © 2020 Mentor Graphics Corporation

Building libc++abi

cmake

-DLIBCXXABI_BAREMETAL=ON

-DLIBCXXABI_ENABLE_THREADS=OFF

-DLIBCXXABI_ENABLE_SHARED=OFF

-DLIBCXX_ENABLE_SHARED=OFF

-DLIBCXXABI_USE_COMPILER_RT=ON

-DLIBCXXABI_ENABLE_EXCEPTIONS=ON

-DLIBCXXABI_LIBCXX_INCLUDES=...

-DLIBCXXABI_USE_LLVM_UNWINDER=ON

$SRC/libcxxabi

14

Page 15: LLVM in a Bare Metal Environment

Restricted © 2020 Mentor Graphics Corporation

Building libcxx

cmake

-DLIBCXX_BAREMETAL=ON

-DLIBCXX_USE_COMPILER_RT=ON

-DLIBCXX_ENABLE_SHARED=OFF

-DLIBCXX_ENABLE_EXCEPTIONS=ON

-DLIBCXX_ENABLE_THREADS=OFF

-DLIBCXX_ENABLE_MONOTONIC_CLOCK=OFF

-DLIBCXXABI_USE_LLVM_UNWINDER=ON

-DLIBCXX_CXX_ABI=libcxxabi

-DLIBCXX_ENABLE_FILESYSTEM=OFF

$SRC=libcxx

15

Page 16: LLVM in a Bare Metal Environment

Restricted © 2020 Mentor Graphics Corporation

Layout

COMPILER_RT_OS_DIR

LLVM_ENABLE_PER_TARGET_RUNTIME_DIR

16

$top

bin

lib clang $version lib $OS_DIR

$triple

Page 17: LLVM in a Bare Metal Environment

Restricted © 2020 Mentor Graphics Corporation

Layout

17

$top

bin

lib

clang $version lib $triple

clang-runtimes

$triple

include

lib

Page 18: LLVM in a Bare Metal Environment

Restricted © 2020 Mentor Graphics Corporation

Testing Runtimes

■ The host tools are tested somewhat like this:

RUN: Use a command to produce something

RUN: Dump using a tool | FileCheck …

CHECK: some pattern to match ..

18

Page 19: LLVM in a Bare Metal Environment

Restricted © 2020 Mentor Graphics Corporation

Testing Runtime

Low Level Library

Initialization

exit

Linker Scripts

__eh_frame_start

__eh_frame_end

__eh_frame_hdr_start

__eh_frame_hdr_end

Execution Support

Executor

Emulator

19

Page 20: LLVM in a Bare Metal Environment

Restricted © 2020 Mentor Graphics Corporation

Emulator/Executor

■ Emulator is invoked like this:

$emulator the_test_executable

■ Executor is invoked like this:

$executor --execdir %T --codesign_identity "" --env --the_test_executable

20

Testing

InfrastructureEmulator/Executor

Arguments

Return Code

Page 21: LLVM in a Bare Metal Environment

Restricted © 2020 Mentor Graphics Corporation

Testing compiler-rt

■ It looks like

// RUN: %clang_builtins %s %librt -lm -o %t && %run %t

■ COMPILER_RT_EMULATOR=...

■ COMPILER_RT_TEST_COMPILER_CFLAGS=…

21

Page 22: LLVM in a Bare Metal Environment

Restricted © 2020 Mentor Graphics Corporation

Testing libc++

22

Build: '%{cxx} %s %{flags} %{compile_flags} %{link_flags} -o %t.exe'Run: '%{exec} %t.exe'

Page 23: LLVM in a Bare Metal Environment

Restricted © 2020 Mentor Graphics Corporation

Testing libc++

23

Build: '%{cxx} %s %{flags} %{compile_flags} %{link_flags} -o %t.exe'Run: '%{exec} %t.exe'

FOO.pass.cppFOO.compile.fail.cpp

Page 24: LLVM in a Bare Metal Environment

Restricted © 2020 Mentor Graphics Corporation

Testing libc++

24

Build: '%{cxx} %s %{flags} %{compile_flags} %{link_flags} -o %t.exe'Run: '%{exec} %t.exe'

FOO.pass.cppFOO.compile.fail.cpp CMake Variables

Page 25: LLVM in a Bare Metal Environment

Restricted © 2020 Mentor Graphics Corporation

Testing libc++

25

Build: '%{cxx} %s %{flags} %{compile_flags} %{link_flags} -o %t.exe'Run: '%{exec} %t.exe'

FOO.pass.cppFOO.compile.fail.cpp CMake Variables

ADDITIONAL_COMPILE_FLAGS FILE_DEPENDENCIES.

Page 26: LLVM in a Bare Metal Environment

Restricted © 2020 Mentor Graphics Corporation

Testing libc++

26

*.cfg.in

config.test_linker_flags =

"@LIBCXX_TEST_LINKER_FLAGS@"

lit.site.cfg

config.test_linker_flags = “T script.ld"cmake -DLIBCXX_TEST_LINKER_FLAGS=“-T script.ld”

Page 27: LLVM in a Bare Metal Environment

Restricted © 2020 Mentor Graphics Corporation

Testing libc++

27

my.cfg.in lit.site.cfg

cmake -DLIBCXX_TEST_CONFIG=my.cfg.in

Page 28: LLVM in a Bare Metal Environment

Restricted © 2020 Mentor Graphics Corporation

Testing libc++

■ Overriding values at test time

$llvm-lit --param=test_linker_flags=“script2.ld" …

28

Page 29: LLVM in a Bare Metal Environment

Restricted © 2020 Mentor Graphics Corporation

Testing libc++

LIBCXXABI_TEST_LINKER_FLAGS

LIBCXXABI_TEST_COMPILER_FLAGS

LIBCXXABI_EXECUTOR

LIBCXX_TEST_LINKER_FLAGS

LIBCXX_TEST_COMPILER_FLAGS

LIBCXX_EXECUTOR

LIBUNWIND_TEST_LINKER_FLAGS

LIBUNWIND_TEST_COMPILER_FLAGS

LIBUNWIND_EXECUTOR

29

Page 30: LLVM in a Bare Metal Environment

Restricted © 2020 Mentor Graphics Corporation

Unsupported Tests

File streams

Process Control

C library limitations

locales

wide characters

strtold

30

Page 31: LLVM in a Bare Metal Environment

Restricted © 2020 Mentor Graphics Corporation

Multilibs

What are multilibs

31

$ riscv64-unknown-elf-gcc -print-multi-lib

.;

rv32i/ilp32;@march=rv32i@mabi=ilp32

rv32im/ilp32;@march=rv32im@mabi=ilp32

rv32iac/ilp32;@march=rv32iac@mabi=ilp32

rv32imac/ilp32;@march=rv32imac@mabi=ilp32

rv32imafc/ilp32f;@march=rv32imafc@mabi=ilp32f

rv64imac/lp64;@march=rv64imac@mabi=lp64

rv64imafdc/lp64d;@march=rv64imafdc@mabi=lp64d

Page 32: LLVM in a Bare Metal Environment

Restricted © 2020 Mentor Graphics Corporation

Multilibs

What are multilibs

32

$arm-none-eabi-gcc -print-multi-lib

.;

thumb;@mthumb

armv5te;@march=armv5te

be;@mbig-endian

armv8-a-hard;@march=armv8-a@mfloat-abi=hard@mfpu=neon-fp-armv8

armv8-a-hard-PIC;@march=armv8-a@mfloat-abi=hard@mfpu=neon-fp-

armv8@fPIC

armv7-a-neon;@march=armv7-a@mfloat-abi=softfp@mfpu=neon

armv7-a-hard;@march=armv7-a@mfloat-abi=hard@mfpu=neon

armv7-a-vfpv3-d16-hard;@march=armv7-a@mfloat-abi=hard@mfpu=vfpv3-d16

armv7-a-hard-PIC;@march=armv7-a@mfloat-abi=hard@mfpu=neon@fPIC

vfp-hard;@march=armv5te@mfloat-abi=hard

armv5te-PIC;@march=armv5te@fPIC

vfp-hard-PIC;@march=armv5te@mfloat-abi=hard@fPIC

armv7-m;@mthumb@march=armv7-m

armv7e-m;@mthumb@march=armv7e-m

armv6-m;@mthumb@march=armv6-m

thumb2;@mthumb@march=armv7@mfix-cortex-m3-ldrd

Page 33: LLVM in a Bare Metal Environment

Restricted © 2020 Mentor Graphics Corporation

Multilibs

What are multilibs

Multilibs in clang

33

$bin/clang --target=riscv64-unknown-elf -print-multi-lib

rv64imac/lp64;@march=rv64imac@mabi=lp64

rv64imafdc/lp64;@march=rv64imafdc@mabi=lp64

rv64imafdc/lp64d;@march=rv64imafdc@mabi=lp64d

Page 34: LLVM in a Bare Metal Environment

Restricted © 2020 Mentor Graphics Corporation

Standalone Build

$ for multilib in all_multilibs; do

$ cd /path/to/build/$multilib

$ cmake …. $src/$library

$ make

$ make install

$ make check-xyz

done

34

Page 35: LLVM in a Bare Metal Environment

Restricted © 2020 Mentor Graphics Corporation

Build Order Revisited

35

Host Tools

C

Library

compiler-rt

libc++

headerslibunwind libc++abi libc++

Page 36: LLVM in a Bare Metal Environment

Restricted © 2020 Mentor Graphics Corporation

Layout

36

$top

bin

lib

clang $version lib $triple

Multilib 1 Prefix

Multilib 2 Prefix

clang-runtimes

$triple

Multilib 1 prefix

include

lib

Multilib 2 prefix

include

lib

Page 37: LLVM in a Bare Metal Environment

Restricted © 2020 Mentor Graphics Corporation

Linker

LLD

Linker Script Issues

37

Page 38: LLVM in a Bare Metal Environment

Restricted © 2020 Mentor Graphics Corporation

Looking Ahead …

Freely available LLVM toolchain for RISCV.

https://www.mentor.com/embedded-software/toolchain-services/codebench-lite-downloads

Review upcoming patches.

38

Page 39: LLVM in a Bare Metal Environment

© Mentor Graphics Corp. Company Confidential

www.mentor.com/embedded

www.mentor.com/embedded

w w w . m e n t o r . c o m

Thank you!