Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESP32-S2: Linker error undefined reference to `__sync_sub_and_fetch_4' (ESP32 is working) #5948

Closed
klausj1 opened this issue Nov 30, 2021 · 5 comments · Fixed by #6075
Closed
Assignees
Labels
Area: ESP-IDF related ESP-IDF related issues Type: Bug 🐛 All bugs

Comments

@klausj1
Copy link

klausj1 commented Nov 30, 2021

Hardware:

Board: ESP32-S2 (WROVER / ESP32-S2-Saola-1)
Core Installation version: 2.0.1
IDE name: Arduino IDE 1.8.16
Flash Frequency: 80Mhz
PSRAM enabled: No
Upload Speed: 921600
Computer OS: Windows 10

Description:

I am trying to connect my ESP32-S2 to the Azure IoT Hub using ESP32_AzureIoT - Azure IoT Hub library for esp32 devices in Arduino.

BTW: Is there a better way to connect the ESP32-S2 to Azure IoT than using this library?

When using the device ESP32SDev Module, compile and linking works.

When using the device ESP32S2 Dev Module, I get a linker error:

c:/users/klaus/appdata/local/arduino15/packages/esp32/tools/xtensa-esp32s2-elf-gcc/gcc8_4_0-esp-2021r1/bin/../lib/gcc/xtensa-esp32s2-elf/8.4.0/../../../../xtensa-esp32s2-elf/bin/ld.exe: c:\Users\Klaus\Documents\IoTWorkbenchProjects\examples\esp32_get_started\Device\.build\libraries\ESP32_AzureIoT_Arduino\az_iot\c-utility\src\constbuffer.c.o:(.literal.CONSTBUFFER_Destroy+0x0): undefined reference to `__sync_sub_and_fetch_4'
c:/users/klaus/appdata/local/arduino15/packages/esp32/tools/xtensa-esp32s2-elf-gcc/gcc8_4_0-esp-2021r1/bin/../lib/gcc/xtensa-esp32s2-elf/8.4.0/../../../../xtensa-esp32s2-elf/bin/ld.exe: c:\Users\Klaus\Documents\IoTWorkbenchProjects\examples\esp32_get_started\Device\.build\libraries\ESP32_AzureIoT_Arduino\az_iot\c-utility\src\constbuffer.c.o: in function `CONSTBUFFER_Destroy':
C:\Users\Klaus\Documents\Arduino\libraries\ESP32_AzureIoT_Arduino\src\az_iot\c-utility\src/constbuffer.c:143: undefined reference to `__sync_sub_and_fetch_4'
collect2.exe: error: ld returned 1 exit status

I tracked this down to this file, which defines this:

#define DEC_RETURN_ZERO (0)
#define INC_REF(type, var) __sync_add_and_fetch((&((REFCOUNT_TYPE(type)*)var)->count), 1)
#define DEC_REF(type, var) __sync_sub_and_fetch((&((REFCOUNT_TYPE(type)*)var)->count), 1)

If I change the contents of this file in a way, that DEC_REF is defined differently:

#define DEC_RETURN_ZERO (0)
#define INC_REF(type, var) ++((((REFCOUNT_TYPE(type)*)var)->count))
#define DEC_REF(type, var) --((((REFCOUNT_TYPE(type)*)var)->count))

Then compiling and linking works.

This is what I assume happens with ESP32 (not ESP32S2), but to be honest I do not really know what I am doing here.

Which definition is chosen depends on some defines like __STDC_VERSION__, __STDC_VERSION__, __STDC_NO_ATOMICS__, ARDUINO_ARCH_SAMD, FREERTOS_ARCH_ESP8266, but I could not figure out where these definitions are set and whats the difference between ESP32 and ESP32-S2.

(Still, the sketch is not working and delivers this runtime error:

Error: Time:Tue Nov 30 05:22:39 2021 File:C:\Users\Klaus\Documents\Arduino\libraries\ESP32_AzureIoT_Arduino\src\az_iot\c-utility\pal\socket_async.c Func:socket_async_create Line:134 Socket connect failed, not EINPROGRESS: 0
Info: Could not open the socket
Info: >>>Connection status: disconnected
Info: >>>Connection status: disconnected
Info: tlsio_openssl_close has been called when in neither TLSIO_STATE_OPEN nor TLSIO_STATE_ERROR.

But I would like to follow this up when I the linker error is clarifed.)

Sketch:

/**
 * A simple Azure IoT example for sending telemetry to Iot Hub.
 */

#include <WiFi.h>
#include "Esp32MQTTClient.h"

#define INTERVAL 10000
#define MESSAGE_MAX_LEN 256
// Please input the SSID and password of WiFi
const char* ssid     = "***";
const char* password = "***";

/*String containing Hostname, Device Id & Device Key in the format:                         */
/*  "HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>"                */
/*  "HostName=<host_name>;DeviceId=<device_id>;SharedAccessSignature=<device_sas_token>"    */
static const char* connectionString = "***";
static bool hasIoTHub = false;
static bool hasWifi = false;

void setup() {
  Serial.begin(115200);
  Serial.println("ESP32 Device");
  Serial.println("Initializing...");
  Serial.println(" > WiFi");
  Serial.println("Starting connecting WiFi.");

  delay(10);
  WiFi.mode(WIFI_AP);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    hasWifi = false;
  }
  hasWifi = true;
  
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println(" > IoT Hub");
  if (!Esp32MQTTClient_Init((const uint8_t*)connectionString, true))
  {
    hasIoTHub = false;
    Serial.println("Initializing IoT hub failed.");
    return;
  }
}

void loop() {
  delay(10);
}
@igrr
Copy link
Member

igrr commented Nov 30, 2021

Thank you for reporting this. We have added a few missing atomic builtins in IDF release/v4.4 branch, but looks like some are still missing. Will fix this in IDF and then arduino-esp32 will pull in the fix in the next release.

(Internal reference: IDF-4397)

@igrr igrr added Area: ESP-IDF related ESP-IDF related issues Type: Bug 🐛 All bugs labels Nov 30, 2021
@igrr igrr self-assigned this Nov 30, 2021
@klausj1
Copy link
Author

klausj1 commented Nov 30, 2021

You are welcome ;)

Could you give me a hint for a quick workaround which I could use? Or is this just not possible?

@igrr
Copy link
Member

igrr commented Dec 1, 2021

You could try to implement the missing functions in a way similar to https://github.com/espressif/esp-idf/blob/master/components/newlib/stdatomic.c, and add them somewhere in your sketch. I think linker might be able to find them. If you defined these functions in a .cpp or .ino file, make sure you declare them as extern "C".

espressif-bot pushed a commit to espressif/esp-idf that referenced this issue Dec 21, 2021
* Adds implementations of __{atomic,sync}_nand_fetch_n. These builtins
  were implemented for other operations but were not defined for NAND.
* Adds implementation of __atomic_OP_fetch_n for all OPs.
* Adds implementation of __sync_OP_and_fetch_n for all OPs.

Reported in espressif/arduino-esp32#5948
@igrr
Copy link
Member

igrr commented Dec 21, 2021

The fix has been added to IDF master branch. We'll try to get it merged into release/v4.4 before the v4.4 release is tagged.

espressif-bot pushed a commit to espressif/esp-idf that referenced this issue Dec 24, 2021
* Adds implementations of __{atomic,sync}_nand_fetch_n. These builtins
  were implemented for other operations but were not defined for NAND.
* Adds implementation of __atomic_OP_fetch_n for all OPs.
* Adds implementation of __sync_OP_and_fetch_n for all OPs.

Reported in espressif/arduino-esp32#5948
espressif-bot pushed a commit to espressif/esp-idf that referenced this issue Dec 27, 2021
* Adds implementations of __{atomic,sync}_nand_fetch_n. These builtins
  were implemented for other operations but were not defined for NAND.
* Adds implementation of __atomic_OP_fetch_n for all OPs.
* Adds implementation of __sync_OP_and_fetch_n for all OPs.

Reported in espressif/arduino-esp32#5948
@igrr
Copy link
Member

igrr commented Jan 4, 2022

Fix backported to IDF release/v4.4 in espressif/esp-idf@c8c65a2, it will be part of the next Arduino-esp32 release.

@igrr igrr mentioned this issue Jan 4, 2022
@VojtechBartoska VojtechBartoska linked a pull request Jan 5, 2022 that will close this issue
me-no-dev added a commit that referenced this issue Jan 18, 2022
esp-dsp: master 6b25cbb
esp-face: master 925c72e
esp-rainmaker: f1b82c7
esp32-camera: master 221d24d
esp_littlefs: master 5a13cd6

fixes: #5948
dskulina pushed a commit to playable-tech/esp-idf that referenced this issue Feb 4, 2022
* Adds implementations of __{atomic,sync}_nand_fetch_n. These builtins
  were implemented for other operations but were not defined for NAND.
* Adds implementation of __atomic_OP_fetch_n for all OPs.
* Adds implementation of __sync_OP_and_fetch_n for all OPs.

Reported in espressif/arduino-esp32#5948
dskulina pushed a commit to playable-tech/esp-idf that referenced this issue Feb 5, 2022
* Adds implementations of __{atomic,sync}_nand_fetch_n. These builtins
  were implemented for other operations but were not defined for NAND.
* Adds implementation of __atomic_OP_fetch_n for all OPs.
* Adds implementation of __sync_OP_and_fetch_n for all OPs.

Reported in espressif/arduino-esp32#5948
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: ESP-IDF related ESP-IDF related issues Type: Bug 🐛 All bugs
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants