Error on cstartup


I’m trying to use Mbed TLS on an Atmel SAME54 microcontroller on top of Microchip Harmony 3, with xc32 as compiler (4.10).

Although the library compiles properly, I’ve noticed that if certain portions of the code are linked, the board doesn’t start (I’m switching a led on after configuring the ports to confirm whether it’s working or not). My guess is that there’s a problem during the initialization of global variables (cstartup or whatever equivalent function used by xc32 (based on gcc) before calling main).

In order to try the library, I followed the following example:
TLS 1.3 sample

I packed all the code inside a function and if the function is compiled and linked (it’s called somewhere), the led remain off, which means, the flow of the code doesn’t get there.

In order to detect where the problem was, I commented all the calls to the library and started to discomment the calls one by one. This way I detected a problem in the call to mbedtls_x509_crt_parse:

ret = mbedtls_x509_crt_parse( &cacert, gtsr1_pem, sizeof(gtsr1_pem) );

As you can see, gtsr1_pem is a const unsigned char gtsr1_pem[] (based on a #define) and the sizeof operator is used on it to get its size. According to the expected behaviour of this operator, the result should be equivalent to strlen(gtsr1_pem) + 1. Using the sizeof format, the board doesn’t start but using the strlen format, it does, so there’s something wrong there. I mean, I’d find reasonable to detect a problem in real time once the flow got there depending on which version is being used but I don’t see the point of failing during the initialisation.

After having modified this, I could continue discommenting calls until I reached a similar situation. It was following this set of calls:

ret = mbedtls_ssl_handshake( &appData3.ssl ); (still in the example)

ret = mbedtls_ssl_handshake_step( ssl );

ret = mbedtls_ssl_tls13_handshake_server_step( ssl );

ret = ssl_tls13_write_server_finished( ssl );

mbedtls_ssl_set_inbound_transform( ssl, ssl->handshake->transform_handshake );

The moment I discomment mbedtls_ssl_set_inbound_transform, the problem shows up again. And that’s definitely surprising based on the content of the function:

    ssl->transform_in = transform;
    memset( ssl->in_ctr, 0, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );

So my guess is that something must be linked as a result of adding this call that generates the problem. In fact things get even weirder because it still fails even if I comment all the code of the function, which means the call itself seems to be causing the problem.

What do you think it could be happening? I mean, I’ve never seen anything like this and trying to modify every single point where sizeof is used is crazy.