Mbed-TLS / mbedtls

An open source, portable, easy to use, readable and flexible TLS library, and reference implementation of the PSA Cryptography API. Releases are on a varying cadence, typically around 3 - 6 months between releases.
https://www.trustedfirmware.org/projects/mbed-tls/
Other
5.21k stars 2.55k forks source link

Linker Error when compiling with arm-none-eabi-gcc for RP2040 #6763

Closed locnnil closed 1 year ago

locnnil commented 1 year ago

Summary

I’m trying to compile the mbedTLS library to use with RP2040 microcontroler (in a .a library file), to use in with CMake pico-sdk of Raspberry Pi Pico board, but I’m getting an linker error.

System information

Mbed TLS version (number or commit id): 1a100b69a4132 (branch development) Operating system and version: Pop!_OS 22.04 LTS x86_64 Configuration (if not default, please attach mbedtls_config.h): crypto_baremetal Compiler and options (if you used a pre-built binary, please indicate how you obtained it): arm-none-eabi-gcc (15:10.3-2021.07-4) 10.3.1 20210621 Additional environment information: Compilling for RP2040 MCU, using compiler flags and env 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="-O2 -mcpu=cortex-m0plus -mthumb"
export CXXFLAGS="-O2 -mcpu=cortex-m0plus -mthumb"

Expected behavior

The compilation of MbedTLS library for baremetal_crypto configuration. (A .a library file)

Actual behavior

The error:

make[1]: Leaving directory '/home/lincoln/mbedtls/tests'
make[1]: Entering directory '/home/lincoln/mbedtls/programs'
  CC    aes/crypt_and_hash.c
/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/thumb/v6-m/nofp/libc.a(lib_a-exit.o): in function `exit':
/build/newlib-pB30de/newlib-3.3.0/build/arm-none-eabi/thumb/v6-m/nofp/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/thumb/v6-m/nofp/libc.a(lib_a-sbrkr.o): in function `_sbrk_r':
/build/newlib-pB30de/newlib-3.3.0/build/arm-none-eabi/thumb/v6-m/nofp/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/thumb/v6-m/nofp/libc.a(lib_a-writer.o): in function `_write_r':
/build/newlib-pB30de/newlib-3.3.0/build/arm-none-eabi/thumb/v6-m/nofp/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/thumb/v6-m/nofp/libc.a(lib_a-closer.o): in function `_close_r':
/build/newlib-pB30de/newlib-3.3.0/build/arm-none-eabi/thumb/v6-m/nofp/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/thumb/v6-m/nofp/libc.a(lib_a-fstatr.o): in function `_fstat_r':
/build/newlib-pB30de/newlib-3.3.0/build/arm-none-eabi/thumb/v6-m/nofp/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/thumb/v6-m/nofp/libc.a(lib_a-isattyr.o): in function `_isatty_r':
/build/newlib-pB30de/newlib-3.3.0/build/arm-none-eabi/thumb/v6-m/nofp/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/thumb/v6-m/nofp/libc.a(lib_a-lseekr.o): in function `_lseek_r':
/build/newlib-pB30de/newlib-3.3.0/build/arm-none-eabi/thumb/v6-m/nofp/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/thumb/v6-m/nofp/libc.a(lib_a-readr.o): in function `_read_r':
/build/newlib-pB30de/newlib-3.3.0/build/arm-none-eabi/thumb/v6-m/nofp/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/thumb/v6-m/nofp/libc.a(lib_a-abort.o): in function `abort':
/build/newlib-pB30de/newlib-3.3.0/build/arm-none-eabi/thumb/v6-m/nofp/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/thumb/v6-m/nofp/libc.a(lib_a-signalr.o): in function `_kill_r':
/build/newlib-pB30de/newlib-3.3.0/build/arm-none-eabi/thumb/v6-m/nofp/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/thumb/v6-m/nofp/libc.a(lib_a-signalr.o): in function `_getpid_r':
/build/newlib-pB30de/newlib-3.3.0/build/arm-none-eabi/thumb/v6-m/nofp/newlib/libc/reent/../../../../../../../../newlib/libc/reent/signalr.c:83: undefined reference to `_getpid'
collect2: error: ld returned 1 exit status

Steps to reproduce

In a linux-based environment (more specificaly in a Ubuntu based Environment):

# Install the compiler used "arm-none-eabi" the compiler of pico-sdk
sudo apt update
sudo apt install -y gcc-arm-none-eabi

# Environment variables (maybe some of them are unnecessary)
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="-O2 -mcpu=cortex-m0plus -mthumb"
export CXXFLAGS="-mcpu=cortex-m0plus -mthumb"

# Clone the mbedTLS repo
git clone git@github.com:Mbed-TLS/mbedtls.git
cd mbedtls

# Install requiriments
python3 -m pip install -r scripts/basic.requirements.txt

# Running the configuration script
python3 scripts/config.py -w include/mbedtls/mbedtls_config.h crypto_baremetal

# Running the makefile without tests (I also tried run just make but the result is the same)
make no_test

Additional information

I followed the instructions of README.md did I miss something? Already try to compile in my Raspberry Pi Zero W (using the OS: Raspbian GNU/Linux 11 (bullseye) armv6l) and I get the same result (The same errors).

locnnil commented 1 year ago

Figure out that the problem was in my environment variables. I Solve my problem using *CMake instead of Make for me works with these steps:

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)
# 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

I hope this comment could help others with the same problem as I did! =)