Error compiling MbedTLS

I’m trying to compile the mbedTLS library to use with RP2040 microcontroler (in a .a library file) but I’m getting the linker error:

uite_psa_crypto_storage_format.current.data suites/test_suite_psa_crypto_storage_format.v0.data ...
/usr/lib/gcc/arm-none-eabi/10.3.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/10.3.1/../../../arm-none-eabi/lib/libc.a(lib_a-exit.o): in function `exit':
/build/newlib-pB30de/newlib-3.3.0/build/arm-none-eabi/newlib/libc/stdlib/../../../../../newlib/libc/stdlib/exit.c:64: undefined reference to `_exit'
/usr/lib/gcc/arm-none-eabi/10.3.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/10.3.1/../../../arm-none-eabi/lib/libc.a(lib_a-sbrkr.o): in function `_sbrk_r':
/build/newlib-pB30de/newlib-3.3.0/build/arm-none-eabi/newlib/libc/reent/../../../../../newlib/libc/reent/sbrkr.c:51: undefined reference to `_sbrk'
/usr/lib/gcc/arm-none-eabi/10.3.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/10.3.1/../../../arm-none-eabi/lib/libc.a(lib_a-writer.o): in function `_write_r':
/build/newlib-pB30de/newlib-3.3.0/build/arm-none-eabi/newlib/libc/reent/../../../../../newlib/libc/reent/writer.c:49: undefined reference to `_write'
/usr/lib/gcc/arm-none-eabi/10.3.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/10.3.1/../../../arm-none-eabi/lib/libc.a(lib_a-closer.o): in function `_close_r':
/build/newlib-pB30de/newlib-3.3.0/build/arm-none-eabi/newlib/libc/reent/../../../../../newlib/libc/reent/closer.c:47: undefined reference to `_close'
/usr/lib/gcc/arm-none-eabi/10.3.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/10.3.1/../../../arm-none-eabi/lib/libc.a(lib_a-fstatr.o): in function `_fstat_r':
/build/newlib-pB30de/newlib-3.3.0/build/arm-none-eabi/newlib/libc/reent/../../../../../newlib/libc/reent/fstatr.c:55: undefined reference to `_fstat'
/usr/lib/gcc/arm-none-eabi/10.3.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/10.3.1/../../../arm-none-eabi/lib/libc.a(lib_a-isattyr.o): in function `_isatty_r':
/build/newlib-pB30de/newlib-3.3.0/build/arm-none-eabi/newlib/libc/reent/../../../../../newlib/libc/reent/isattyr.c:52: undefined reference to `_isatty'
/usr/lib/gcc/arm-none-eabi/10.3.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/10.3.1/../../../arm-none-eabi/lib/libc.a(lib_a-lseekr.o): in function `_lseek_r':
/build/newlib-pB30de/newlib-3.3.0/build/arm-none-eabi/newlib/libc/reent/../../../../../newlib/libc/reent/lseekr.c:49: undefined reference to `_lseek'
/usr/lib/gcc/arm-none-eabi/10.3.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/10.3.1/../../../arm-none-eabi/lib/libc.a(lib_a-readr.o): in function `_read_r':
/build/newlib-pB30de/newlib-3.3.0/build/arm-none-eabi/newlib/libc/reent/../../../../../newlib/libc/reent/readr.c:49: undefined reference to `_read'
/usr/lib/gcc/arm-none-eabi/10.3.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/10.3.1/../../../arm-none-eabi/lib/libc.a(lib_a-abort.o): in function `abort':
/build/newlib-pB30de/newlib-3.3.0/build/arm-none-eabi/newlib/libc/stdlib/../../../../../newlib/libc/stdlib/abort.c:59: undefined reference to `_exit'
/usr/lib/gcc/arm-none-eabi/10.3.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/10.3.1/../../../arm-none-eabi/lib/libc.a(lib_a-signalr.o): in function `_kill_r':
/build/newlib-pB30de/newlib-3.3.0/build/arm-none-eabi/newlib/libc/reent/../../../../../newlib/libc/reent/signalr.c:53: undefined reference to `_kill'
/usr/lib/gcc/arm-none-eabi/10.3.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/10.3.1/../../../arm-none-eabi/lib/libc.a(lib_a-signalr.o): in function `_getpid_r':
/build/newlib-pB30de/newlib-3.3.0/build/arm-none-eabi/newlib/libc/reent/../../../../../newlib/libc/reent/signalr.c:83: undefined reference to `_getpid'
collect2: error: ld returned 1 exit status

Steps to reproduce:

  • I’m in a Linux Environment (but you can use WSL)
sudo apt update
sudo apt install -y  gcc-arm-none-eabi

Exporting my environment variables:

export CC="/usr/bin/arm-none-eabi-gcc"
export CXX="/usr/bin/arm-none-eabi-g++"
export AS="/usr/bin/arm-none-eabi-as"
export STRIP="/usr/bin/arm-none-eabi-strip"
export AR="/usr/bin/arm-none-eabi-ar"
export CFLAGS="-mcpu=cortex-m0plus -mthumb"
export CXXFLAGS="-mcpu=cortex-m0plus -mthumb"

Clone github MbedTLS repo:

https://github.com/Mbed-TLS/mbedtls.git
cd mbedtls

Configuring my compilation for baremetal_crypto

python3 scripts/config.py -w include/mbedtls/mbedtls_config.h crypto_baremetal

Running make command:

make generated_files

I followed the instructions of README.md did I miss something?

Hello,
Seems that some standard library is missing…

Not sure but maybe you can solve adding -lc in your CFLAGS list.

Lorenzo

1 Like

It’s basic like that! I just realize that I was missing the flag: --specs=nosys.specs

I basically solve my problem changing my compilation method to CMake and doing these steps:

  • First I create a toolchain-arm-none-eabi.cmake toolchain file with the content:
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(CMAKE_SYSTEM_VERSION 1)
# this info holds the location of installation
set(CMAKE_STAGING_PREFIX /tmp/mbedTLS)
set(CMAKE_C_COMPILER   /usr/bin/arm-none-eabi-gcc)
set(CMAKE_CXX_COMPILER /usr/bin/arm-none-eabi-g++)
set(CMAKE_MAKE_PROGRAM=/usr/bin/make)
set(CMAKE_C_FLAGS="-mcpu=cortex-m0plus -mthumb -O2")
set(CMAKE_CXX_FLAGS="-mcpu=cortex-m0plus -mthumb -O2")
set(CMAKE_EXE_LINKER_FLAGS " --specs=nosys.specs")
set(CMAKE_STRIP /usr/bin/arm-none-eabi-strip)
set(CMAKE_FIND_ROOT_PATH  /usr/arm-none-eabi)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
  • I put my toolchain-arm-none-eabi.cmake file in a folder and from that same folder I ran:
# Download the `.tar.gz` of MbedTLS
wget https://github.com/Mbed-TLS/mbedtls/archive/refs/tags/v3.2.1.tar.gz

# unzip
tar zxf v3.2.1.tar.gz

# Configure to crypto_baremetal
mbedtls-3.2.1/scripts/config.py -w mbedtls-3.2.1/include/mbedtls/mbedtls_config.h crypto_baremetal

# Folder to build
mkdir mbedtls

# Go inside build folder
pushd mbedtls

# Configuring (necessary force that compiler works to CMake)
cmake -DCMAKE_C_COMPILER_WORKS=1 \
-DCMAKE_BUILD_TYPE=Release -DUSE_SHARED_MBEDTLS_LIBRARY=OFF \
-DUSE_STATIC_MBEDTLS_LIBRARY=ON  \
-DENABLE_PROGRAMS=OFF -DENABLE_TESTING=OFF \
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
-DCMAKE_TOOLCHAIN_FILE=../toolchain-arm-none-eabi.cmake ../mbedtls-3.2.1

# Compiling and installing
make all install

# Free diretory stack
popd

Then inside the folder /tmp/mbedTLS I have:

cd /tmp/mbedTLS
tree
.
├── cmake
│   ├── MbedTLSConfig.cmake
│   ├── MbedTLSConfigVersion.cmake
│   ├── MbedTLSTargets.cmake
│   └── MbedTLSTargets-release.cmake
├── include
│   ├── mbedtls
│   │   ├── aes.h
│   │   ├── aria.h
│   │   ├── asn1.h
│   │   ├── asn1write.h
│   │   ├── base64.h
│   │   ├── bignum.h
│   │   ├── build_info.h
│   │   ├── camellia.h
│   │   ├── ccm.h
│   │   ├── chacha20.h
│   │   ├── chachapoly.h
│   │   ├── check_config.h
│   │   ├── cipher.h
│   │   ├── cmac.h
│   │   ├── compat-2.x.h
│   │   ├── config_psa.h
│   │   ├── constant_time.h
│   │   ├── ctr_drbg.h
│   │   ├── debug.h
│   │   ├── des.h
│   │   ├── dhm.h
│   │   ├── ecdh.h
│   │   ├── ecdsa.h
│   │   ├── ecjpake.h
│   │   ├── ecp.h
│   │   ├── entropy.h
│   │   ├── error.h
│   │   ├── gcm.h
│   │   ├── hkdf.h
│   │   ├── hmac_drbg.h
│   │   ├── mbedtls_config.h
│   │   ├── md5.h
│   │   ├── md.h
│   │   ├── memory_buffer_alloc.h
│   │   ├── net_sockets.h
│   │   ├── nist_kw.h
│   │   ├── oid.h
│   │   ├── pem.h
│   │   ├── pkcs12.h
│   │   ├── pkcs5.h
│   │   ├── pk.h
│   │   ├── platform.h
│   │   ├── platform_time.h
│   │   ├── platform_util.h
│   │   ├── poly1305.h
│   │   ├── private_access.h
│   │   ├── psa_util.h
│   │   ├── ripemd160.h
│   │   ├── rsa.h
│   │   ├── sha1.h
│   │   ├── sha256.h
│   │   ├── sha512.h
│   │   ├── ssl_cache.h
│   │   ├── ssl_ciphersuites.h
│   │   ├── ssl_cookie.h
│   │   ├── ssl.h
│   │   ├── ssl_ticket.h
│   │   ├── threading.h
│   │   ├── timing.h
│   │   ├── version.h
│   │   ├── x509_crl.h
│   │   ├── x509_crt.h
│   │   ├── x509_csr.h
│   │   └── x509.h
│   └── psa
│       ├── crypto_builtin_composites.h
│       ├── crypto_builtin_primitives.h
│       ├── crypto_compat.h
│       ├── crypto_config.h
│       ├── crypto_driver_common.h
│       ├── crypto_driver_contexts_composites.h
│       ├── crypto_driver_contexts_primitives.h
│       ├── crypto_extra.h
│       ├── crypto.h
│       ├── crypto_platform.h
│       ├── crypto_se_driver.h
│       ├── crypto_sizes.h
│       ├── crypto_struct.h
│       ├── crypto_types.h
│       └── crypto_values.h
└── lib
    ├── libmbedcrypto.a
    ├── libmbedtls.a
    └── libmbedx509.a
1 Like