8000 ESP32: `.iram0.text` overflow with recent IDF versions · Issue #8260 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

ESP32: .iram0.text overflow with recent IDF versions #8260

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

Closed
jonathanhogg opened this issue Feb 4, 2022 · 17 comments
Closed

ESP32: .iram0.text overflow with recent IDF versions #8260

jonathanhogg opened this issue Feb 4, 2022 · 17 comments

Comments

@jonathanhogg
Copy link
Contributor

I'm seeing a bunch of problems building against different versions of the IDF. All of the following is building 102cc12 with 2 of my patches integrated on top (#6263 and #7582).

A GENERIC ESP32 build against the latest v4.4 tag results in a binary that explodes on boot:

ELF file SHA256: 2995987005bacb8b

Rebooting...
ets Jun  8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:4344
load:0x40078000,len:13816
load:0x40080400,len:3340
0x40080400: _init at ??:?

entry 0x40080618
Guru Meditation Error: Core  1 panic'ed (StoreProhibited). Exception was unhandled.

Core  1 register dump:
PC      : 0x4014899a  PS      : 0x00060733  A0      : 0x800d7048  A1      : 0x3ffcef20  
0x4014899a: uart_isr_register at /Users/jonathan/Projects/outputarts/esp-idf-4.4/components/driver/uart.c:594

A2      : 0x00000000  A3      : 0x00000000  A4      : 0x3ffb78e8  A5      : 0x0000040e  
A6      : 0x3ffcef50  A7      : 0x3ffbf24c  A8      : 0x00000000  A9      : 0x3ffceef0  
A10     : 0x00000000  A11     : 0x0000040e  A12     : 0x4008334c  A13     : 0x00000000  
0x4008334c: uart_irq_handler at /Users/jonathan/Projects/outputarts/micropython/ports/esp32/uart.c:46

A14     : 0x3ffcef50  A15     : 0x00000000  SAR     : 0x0000001d  EXCCAUSE: 0x0000001d  
EXCVADDR: 0x00000008  LBEG    : 0x00000000  LEND    : 0x00000000  LCOUNT  : 0x00000000  


Backtrace:0x40148997:0x3ffcef200x400d7045:0x3ffcef50 0x400d6eb9:0x3ffcef80 
0x40148997: uart_isr_register at /Users/jonathan/Projects/outputarts/esp-idf-4.4/components/driver/uart.c:594

0x400d7045: uart_init at /Users/jonathan/Projects/outputarts/micropython/ports/esp32/uart.c:41

0x400d6eb9: mp_task at /Users/jonathan/Projects/outputarts/micropython/ports/esp32/main.c:96

Switching to v4.3.2 works, but I am unable to do a matching GENERIC_SPIRAM build (for the other boards that I use) as it seems to be overflowing the iram0 memory segment:

[100%] Linking CXX executable micropython.elf
/Users/jonathan/.espressif/tools/xtensa-esp32-elf/esp-2021r2-8.4.0/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld: micropython.elf section `.iram0.text' will not fit in region `iram0_0_seg'
/Users/jonathan/.espressif/tools/xtensa-esp32-elf/esp-2021r2-8.4.0/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld: IRAM0 segment data does not fit.
/Users/jonathan/.espressif/tools/xtensa-esp32-elf/esp-2021r2-8.4.0/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld: region `iram0_0_seg' overflowed by 492 bytes
collect2: error: ld returned 1 exit status
make[3]: *** [micropython.elf] Error 1
make[2]: *** [CMakeFiles/micropython.elf.dir/all] Error 2
make[1]: *** [all] Error 2
make failed with exit code 2
make: *** [all] Error 2

I have verified that this is the same building against master without my patches.

This last error is a little outside my area of expertise: I'm not actually familiar with the ESP32/MicroPython memory model. I did a little hunting to see if I could figure out what's in that text area and why it's suddenly overflowing with v4.3.2, but nothing immediately jumped out at me. I'm also not sure how the size of the regions is configured.

Backing up one more version to build against v4.3.1 appears to work OK, but suffers from the missing memory problem documented in #7813

rst:0x1 (POWERON_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:4604
ho 0 tail 12 room 4
load:0x40078000,len:13396
load:0x40080400,len:3312
entry 0x4008061c
MicroPython v1.18-82-gd57e23b8d on 2022-02-04; ESP32 module with ESP32
Type "help()" for more information.
>>> import gc
>>> gc.mem_free()
63008

This is about 109000 free on a straight MicroPython 1.18 GENERIC download.

Going back to v4.0.2 I get seemingly good builds for both GENERIC and GENERIC_SPIRAM and my memory back:

rst:0x1 (POWERON_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:4252
load:0x40078000,len:11920
load:0x40080400,len:3344
entry 0x4008060c
MicroPython v1.18-82-gd57e23b8d on 2022-02-04; ESP32 module with ESP32
Type "help()" for more information.
>>> import gc
>>> gc.mem_free()
110176

Is the moral of this tale: do not attempt to build MicroPython against any version of the IDF more recent than v4.0.2?

I've been using v4.3.1 for ages, but always with a GENERIC_SPIRAM build so I hadn't noticed the missing memory on a non-SPIRAM device until yesterday.

@stephanelsmith
Copy link
Contributor
stephanelsmith commented Feb 4, 2022

I'm successfully building esp-idf v4.4rc1 with micropython master. I'm targeting ESP32 d4, ESP32 S2, and ESP32 S3.

@russhughes
Copy link
russhughes commented Feb 5, 2022

I don't know if this is useful but here is an idf.py size-components diff report between GENERIC_SPIRAM and GENERIC using esp-idf V4.4 showing the static IRAM overflow.

WARNING: Given section not found in any memory region.
Check whether the LD file is compatible with the definitions in get_mem_regions in idf_size.py

<CURRENT> MAP file: build-GENERIC_SPIRAM/micropython.map
<REFERENCE> MAP file: build-GENERIC/micropython.map

Difference is counted as <CURRENT> - <REFERENCE>,           i.e. a positive number means that <CURRENT> is larger.
Total sizes of <CURRENT>:                                       <REFERENCE>     Difference
Used static DRAM:   51492 bytes (  73088 remain, 41.3% used)          51320           +172 (   -172 remain,      +0 total)
      .data size:   22564 bytes                                       22448           +116
      .bss  size:   28928 bytes                                       28872            +56
Used static IRAM:  132830 bytes (  -1758 remain, 101.3% used) Overflow detected!         118855         +13975 ( -13975 remain,      +0 total)
      .text size:  131803 bytes                                      117828         +13975
   .vectors size:    1027 bytes                                        1027
Used stat D/IRAM:  184322 bytes (  71330 remain, 72.1% used)         170175         +14147 ( -14147 remain,      +0 total)
      .data size:   22564 bytes                                       22448           +116
      .bss  size:   28928 bytes                                       28872            +56
      .text size:  131803 bytes                                      117828         +13975
   .vectors size:    1027 bytes                                        1027
Used Flash size : 1556802 bytes                                     1547302          +9500
      .text     : 1139527 bytes                                     1087411         +52116
      .rodata   :  417019 bytes                                      459635         -42616
Total image size: 1867590 bytes (.bin may be padded larger)         1829908         +37682
Per-archive contributions to ELF file:
            Archive File              DRAM .data               .rtc.data               DRAM .bss               .rtc.text             IRAM0 .text             & 0.vectors            ram_st_total             Flash .text               & .rodata        & .rodata_noload              & .appdesc             flash_total
                        |    <C>|    <R>|<C>-<R>|    <C>|    <R>|<C>-<R>|    <C>|    <R>|<C>-<R>|    <C>|    <R>|<C>-<R>|    <C>|    <R>|<C>-<R>|    <C>|    <R>|<C>-<R>|    <C>|    <R>|<C>-<R>|    <C>|    <R>|<C>-<R>|    <C>|    <R>|<C>-<R>|    <C>|    <R>|<C>-<R>|    <C>|    <R>|<C>-<R>|    <C>|    <R>|<C>-<R>
                         -----------------------                         -----------------------                         -----------------------                         -----------------------                         -----------------------                         -----------------------                        
               libmain.a|    146|    146|       |   2054|   2054|       |   2406|   2406|       |       |       |       |   8524|   8177|   +347|       |       |       |  13130|  12783|   +347| 239554| 228858| +10696| 255044| 303226| -48182|       |       |       |       |       |       | 505322| 542461| -37139
               liblwip.a|     21|     21|       |       |       |       |   3888|   3888|       |       |       |       |       |       |       |       |       |       |   3909|   3909|       | 120345| 111234|  +9111|  17728|  17728|       |       |       |       |       |       |       | 138094| 128983|  +9111
           libnet80211.a|    759|    759|       |       |       |       |   6085|   6085|       |       |       |       |       |       |       |       |       |       |   6844|   6844|       | 103797| 103752|    +45|   6014|   6014|       |  12516|  12516|       |       |       |       | 123086| 123041|    +45
           libbtdm_app.a|    773|    773|       |       |       |       |   2617|   2617|       |       |       |       |  28344|  28416|    -72|       |       |       |  31734|  31806|    -72|  77146|  77138|     +8|   5207|   5207|       |       |       |       |       |       |       | 111470| 111534|    -64
             libdriver.a|    404|    404|       |       |       |       |    408|    408|       |       |       |       |   7159|   6882|   +277|       |       |       |   7971|   7694|   +277|  61940|  58380|  +3560|  21958|  21958|       |       |       |       |       |       |       |  91461|  87624|  +3837
         libmbedcrypto.a|     64|     64|       |       |       |       |     85|     85|       |       |       |       |     30|     30|       |       |       |       |    179|    179|       |  73675|  70420|  +3255|  10869|  10869|       |       |       |       |       |       |       |  84638|  81383|  +3255
                 libbt.a|   4923|   4923|       |       |       |       |   5226|   5226|       |       |       |       |   1782|   1702|    +80|       |       |       |  11931|  11851|    +80|  64151|  59031|  +5120|   5802|   5802|       |       |       |       |       |       |       |  76658|  71458|  +5200
                  libc.a|     41|      4|    +37|       |       |       |       |       |       |       |       |       |   6648|       |  +6648|       |       |       |   6689|      4|  +6685|  60546|  55745|  +4801|   4206|   4206|       |       |       |       |       |       |       |  71441|  59955| +11486
                 libpp.a|   2427|   2427|       |       |       |       |   1300|   1300|       |       |       |       |   1354|   1358|     -4|       |       |       |   5081|   5085|     -4|  49537|  49525|    +12|   2512|   2512|       |   2284|   2284|       |       |       |       |  58114|  58106|     +8
                libphy.a|   1436|   1436|       |       |       |       |    715|    715|       |       |       |       |   7758|   7758|       |       |       |       |   9909|   9909|       |  27783|  27703|    +80|       |       |       |       |       |       |       |       |       |  36977|  36897|    +80
     libwpa_supplicant.a|     12|     12|       |       |       |       |    948|    948|       |       |       |       |       |       |       |       |       |       |    960|    960|       |  33585|  31475|  +2110|    866|    866|       |       |       |       |       |       |       |  34463|  32353|  +2110
                   (exe)|    200|    200|       |       |       |       |      8|      8|       |       |       |       |       |       |       |      3|      3|       |    211|    211|       |  28495|  26065|  +2430|   2920|   2920|       |       |       |       |       |       |       |  31618|  29188|  +2430
            libmbedtls.a|     60|     60|       |       |       |       |    200|    200|       |       |       |       |       |       |       |       |       |       |    260|    260|       |  24316|  22878|  +1438|   4781|   4781|       |       |       |       |       |       |       |  29157|  27719|  +1438
            libesp_eth.a|      4|      4|       |       |       |       |      1|      1|       |       |       |       |     48|     46|     +2|       |       |       |     53|     51|     +2|  20830|  19784|  +1046|   8269|   8269|       |       |       |       |       |       |       |  29151|  28103|  +1048
     libesp_hw_support.a|    237|    220|    +17|     16|     16|       |     95|     46|    +49|    103|     99|     +4|  12549|   7235|  +5314|       |       |       |  13000|   7616|  +5384|   7897|   5562|  +2335|   6685|   1189|  +5496|       |       |       |       |       |       |  27487|  14321| +13166
           libfreertos.a|   3112|   3112|       |       |       |       |   1044|   1044|       |       |       |       |  16875|  16236|   +639|    425|    425|       |  21456|  20817|   +639|    195|    196|     -1|   4541|   4541|       |       |       |       |       |       |       |  25148|  24510|   +638
                libhal.a|    166|    166|       |       |       |       |       |       |       |       |       |       |   6647|   6444|   +203|       |       |       |   6813|   6610|   +203|  14327|  13918|   +409|   3864|   3864|       |       |       |       |       |       |       |  25004|  24392|   +612
               libmdns.a|       |       |       |       |       |       |   1898|   1898|       |       |       |       |       |       |       |       |       |       |   1898|   1898|       |  23707|  22260|  +1447|    310|    310|       |       |       |       |       |       |       |  24017|  22570|  +1447
                  libm.a|      4|      4|       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |      4|      4|       |  19800|  19158|   +642|   1704|   1704|       |       |       |       |       |       |       |  21508|  20866|   +642
          libnvs_flash.a|       |       |       |       |       |       |     24|     24|       |       |       |       |       |       |       |       |       |       |     24|     24|       |  12953|  12293|   +660|   2671|   2671|       |       |       |       |       |       |       |  15624|  14964|   +660
          libspi_flash.a|   1451|   1451|       |       |       |       |    294|    294|       |       |       |       |   8850|   8572|   +278|       |       |       |  10595|  10317|   +278|   2088|   2039|    +49|   1810|   1810|       |       |       |       |       |       |       |  14199|  13872|   +327
         libesp_system.a|    246|    189|    +57|       |       |       |    210|    210|       |       |       |       |   3930|   3209|   +721|       |       |       |   4386|   3608|   +778|   5592|   5352|   +240|   3085|   3029|    +56|       |       |       |       |       |       |  12853|  11779|  +1074
              libsdmmc.a|       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |   8035|   7426|   +609|   3259|   3259|       |       |       |       |       |       |       |  11294|  10685|   +609
          libesp_netif.a|     20|     20|       |       |       |       |     25|     25|       |       |       |       |       |       |       |       |       |       |     45|     45|       |   7197|   6865|   +332|   3343|   3343|       |       |       |       |       |       |       |  10560|  10228|   +332
               libheap.a|   1826|   1826|       |       |       |       |      8|      8|       |       |       |       |   4119|   4025|    +94|       |       |       |   5953|   5859|    +94|   1628|   1576|    +52|   2054|   2030|    +24|       |       |       |       |       |       |   9627|   9457|   +170
            libcoexist.a|   1816|   1816|       |       |       |       |     33|     33|       |       |       |       |   3173|   3181|     -8|       |       |       |   5022|   5030|     -8|   3884|   3884|       |    315|    315|       |       |       |       |       |       |       |   9188|   9196|     -8
                libvfs.a|    308|    308|       |       |       |       |     60|     60|       |       |       |       |       |       |       |       |       |       |    368|    368|       |   7096|   6830|   +266|    645|    645|       |       |       |       |       |       |       |   8049|   7783|   +266
         libesp_common.a|       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |     48|     48|       |   7001|   7001|       |       |       |       |       |       |       |   7049|   7049|       
           libmbedx509.a|       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |   6580|   6334|   +246|    101|    101|       |       |       |       |       |       |       |   6681|   6435|   +246
        libesp_ringbuf.a|       |       |       |       |       |       |       |       |       |       |       |       |   3738|   3629|   +109|       |       |       |   3738|   3629|   +109|       |       |       |   2294|   2294|       |       |       |       |       |       |       |   6032|   5923|   +109
           libesp_wifi.a|    564|    564|       |       |       |       |     39|     39|       |       |       |       |    513|    513|       |       |       |       |   1116|   1116|       |   3139|   3030|   +109|   1009|   1009|       |       |       |       |       |       |       |   5225|   5116|   +109
          libesp_event.a|      4|      4|       |       |       |       |     13|     13|       |       |       |       |       |       |       |       |       |       |     17|     17|       |   3613|   3456|   +157|   1199|   1199|       |       |       |       |       |       |       |   4816|   4659|   +157
 libbootloader_support.a|       |       |       |       |       |       |      4|      4|       |       |       |       |    613|   1533|   -920|       |       |       |    617|   1537|   -920|   2514|   2283|   +231|   1353|   1353|       |       |       |       |       |       |       |   4480|   5169|   -689
          libesp_timer.a|     32|     32|       |       |       |       |     24|     24|       |       |       |       |   1400|   1362|    +38|       |       |       |   1456|   1418|    +38|   1306|   1265|    +41|    678|    678|       |       |       |       |       |       |       |   3416|   3337|    +79
             libnewlib.a|    231|    231|       |       |       |       |    440|    440|       |       |       |       |   1409|   1400|     +9|       |       |       |   2080|   2071|     +9|    948|    915|    +33|    332|    332|       |       |       |       |       |       |       |   2920|   2878|    +42
         libapp_update.a|      1|      1|       |       |       |       |     12|     12|       |       |       |       |    183|    162|    +21|       |       |       |    196|    175|    +21|   1344|   1300|    +44|    665|    665|       |       |       |       |    256|    256|       |   2449|   2384|    +65
            libesp_phy.a|     16|     16|       |       |       |       |     22|     22|       |       |       |       |    144|    144|       |       |       |       |    182|    182|       |   1310|   1286|    +24|    956|    956|       |       |       |       |       |       |       |   2426|   2402|    +24
             libesp_pm.a|     12|     12|       |       |       |       |    119|    119|       |       |       |       |    910|    868|    +42|       |       |       |   1041|    999|    +42|   1031|    981|    +50|    421|    421|       |       |       |       |       |       |       |   2374|   2282|    +92
                librtc.a|       |       |       |       |       |       |      4|      4|       |       |       |       |   2219|   2219|       |       |       |       |   2223|   2223|       |       |       |       |       |       |       |       |       |       |       |       |       |   2219|   2219|       
                libsoc.a|       |       |       |       |       |       |       |       |       |       |       |       |    216|    205|    +11|       |       |       |    216|    205|    +11|       |       |       |   1964|   1964|       |       |       |       |       |       |       |   2180|   2169|    +11
             libstdc++.a|      4|      4|       |       |       |       |     20|     20|       |       |       |       |       |       |       |       |       |       |     24|     24|       |   1262|   1199|    +63|    704|    704|       |       |       |       |       |       |       |   1970|   1907|    +63
              libefuse.a|     64|     64|       |       |       |       |      4|      4|       |       |       |       |       |       |       |       |       |       |     68|     68|       |   1153|   1119|    +34|    752|    752|       |       |       |       |       |       |       |   1969|   1935|    +34
            libesp_ipc.a|     28|     28|       |       |       |       |     88|     88|       |       |       |       |    648|    633|    +15|       |       |       |    764|    749|    +15|    674|    651|    +23|    271|    271|       |       |       |       |       |       |       |   1621|   1583|    +38
        libesp_adc_cal.a|       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |    858|    840|    +18|    639|    639|       |       |       |       |       |       |       |   1497|   1479|    +18
             libxtensa.a|   1024|   1024|       |       |       |       |       |       |       |       |       |       |     77|     77|       |       |       |       |   1101|   1101|       |    119|    114|     +5|     35|     35|       |       |       |       |       |       |       |   1255|   1250|     +5
                liblog.a|      8|      8|       |       |       |       |    272|    272|       |       |       |       |    279|    267|    +12|       |       |       |    559|    547|    +12|    724|    697|    +27|    139|    139|       |       |       |       |       |       |       |   1150|   1111|    +39
      libtcpip_adapter.a|       |       |       |       |       |       |     17|     17|       |       |       |       |       |       |       |       |       |       |     17|     17|       |    663|    638|    +25|    136|    136|       |       |       |       |       |       |       |    799|    774|    +25
            libpthread.a|      8|      8|       |       |       |       |     12|     12|       |       |       |       |       |       |       |       |       |       |     20|     20|       |    591|    561|    +30|    106|    106|       |       |       |       |       |       |       |    705|    675|    +30
             libxt_hal.a|       |       |       |       |       |       |       |       |       |       |       |       |    443|    443|       |       |       |       |    443|    443|       |       |       |       |     32|     32|       |       |       |       |       |       |       |    475|    475|       
                libgcc.a|       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |    308|    308|       |    160|    160|       |       |       |       |       |       |       |    468|    468|       
                libulp.a|       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |    449|    416|    +33|       |       |       |       |       |       |       |       |       |    449|    416|    +33
               libcore.a|       |       |       |       |       |       |     33|     33|       |       |       |       |       |       |       |       |       |       |     33|     33|       |    247|    247|       |      5|      5|       |     43|     43|       |       |       |       |    295|    295|       
            libesp_rom.a|       |       |       |       |       |       |       |       |       |       |       |       |     93|     90|     +3|       |       |       |     93|     90|     +3|     44|     44|       |       |       |       |       |       |       |       |       |       |    137|    134|     +3
                libcxx.a|       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |     41|     41|       |       |       |       |       |       |       |       |       |       |     41|     41|       
               libmesh.a|       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       

@jonathanhogg
Copy link
Contributor Author
jonathanhogg commented Feb 5, 2022

Interesting! Thanks for this @russhughes.

Inspired by this I've used idf_size.py to do a comparison of 4.3.1 and 4.3.2 GENERIC_SPIRAM builds (the latter failing to link) and these are the main changes in .iram0.text:

libbootloader_support.a:bootloader_flash.c.o        710
libnewlib.a:assert.c.o                              362
libesp_hw_support.a:regi2c_ctrl.c.o                 171
libesp_system.a:sleep_modes.c.o                     163
libbtdm_app.a:coex_schm.o                           160
libbtdm_app.a:arch_main.o                           148
libmain.a:vm.c.o                                    123
libhal.a:spi_hal_iram.c.o                            80
libfreertos.a:tasks.c.o                              62
libdriver.a:gpio.c.o                                 40
...
libbtdm_app.a:lld_pdu.o                              -4
libbtdm_app.a:ld_acl.o                               -4
libbtdm_app.a:huart.o                                -4
libc.a:lib_a-strcmp.o                                -8
libesp_timer.a:esp_timer_impl_lac.c.o               -15
libesp_timer.a:esp_timer.c.o                        -15
libesp_system.a:cpu_start.c.o                       -28
libbtdm_app.a:lld_evt.o                             -52
libesp_hw_support.a:rtc_clk.c.o                     -76
libbtdm_app.a:ld_fm.o                              -244

The worst culprit, bootloader_flash.c.o, appears to have gained a load of new functions with IRAM_ATTR, which I assume is pulling the code for those into SRAM.

The GENERIC_SPIRAM build appears to just generally use more SRAM than GENERIC (I guess it can afford to be profligate) and that combined with these changes is busting the segment.

Gist of code for doing comparison for reference.

@dpgeorge
Copy link
Member
dpgeorge commented Feb 6, 2022

There are at least 2 factors here for running out of iRAM:

  • Building with SPIRAM enabled includes some additional compiler flags to work around bugs in the silicon/caching. These options add extra memory barrier instructions in many places, and this increases the size of the firmware, including functions that live in iRAM. Overall the firmware is just bigger with SPIRAM enabled (maybe this can be improved with variants like C3? not sure).
  • In commit 549448e some functions in the MP runtime were placed in iRAM to improve performance (also -O2 was used which increase build size cf -Os). You can try switching back to -Os (GENERIC_D2WD board does this to reduce its size), and/or disabling those MICROPY_WRAP_xxx macros so that iRAM has less in it.

@jonathanhogg
Copy link
Contributor Author

Thanks for the background @dpgeorge. I just attempted a new 4.3.2 GENERIC_SPIRAM_SMALL build – changing just the optimization to be for size rather than performance – and that results in a successful link.

For interest, changes in .iram0.text usage between this and the standard (failing) 4.3.2 GENERIC_SPIRAM build are:

## -- End pasted text --
libmain.a:scheduler.c.o                              17
libesp32.a:spiram.c.o                                12
libesp_timer.a:esp_timer_impl_lac.c.o                10
libheap.a:multi_heap.c.o                              8
libesp_timer.a:ets_timer_legacy.c.o                   6
libesp_hw_support.a:cpu_util.c.o                      4
libspi_flash.a:memspi_host_driver.c.o                 3
libnewlib.a:locks.c.o                                 3
libhal.a:soc_hal.c.o                                  3
libesp_system.a:system_api.c.o                        2
...
libmain.a:vm.c.o                                   -231
libhal.a:spi_flash_hal_iram.c.o                    -242
libesp_hw_support.a:rtc_clk.c.o                    -257
libesp_system.a:panic.c.o                          -259
libesp_system.a:sleep_modes.c.o                    -268
libdriver.a:rmt.c.o                                -292
libhal.a:spi_hal_iram.c.o                          -341
libspi_flash.a:spi_flash_chip_generic.c.o          -354
libesp32.a:spiram_psram.c.o                        -535
libfreertos.a:tasks.c.o                            -622

So quite a surprising number of large reductions. Off to dig out a microcontroller to test this build on…

< 8000 /div>
@UnexpectedMaker
Copy link
Contributor

I'm also seeing boot loop issues with latest master and IDF4.4 release on my TinyPICO (has SPIRAM) - working fine on my S2 and S3 boards.

So it seems we will need to remove some of the IRAM optimisations for PSRAM boards @dpgeorge ? Is that even possible to do on a per board level?

@dpgeorge
Copy link
Member
dpgeorge commented Feb 8, 2022

@UnexpectedMaker can you test the plain GENERIC build with IDF4.4 on TinyPICO? Then we can narrow down SPIRAM vs non-SPIRAM builds.

So it seems we will need to remove some of the IRAM optimisations for PSRAM boards @dpgeorge ? Is that even possible to do on a per board level?

Yes. Define relevant macros in mpconfigboard.h, eg #define MICROPY_WRAP_MP_BINARY_OP (0)

@UnexpectedMaker
Copy link
Contributor

GENERIC still boot loops my TinyPICO.

@jonathanhogg
Copy link
Contributor Author

Yes, at the moment I'm not convinced that the boot loop failure on v4.4 tag builds is the same IRAM problem as the other stuff I've been hitting. The backtrace I got suggested a bad pointer 8000 in the UART initialisation. I've not had time to dig deeper into it though – I've gone back to 4.3.2 and am building GENERIC as normal and a new -Os version of GENERIC_SPIRAM.

@jonathanhogg
Copy link
Contributor Author

Have opened #8277 about the v4.4 problem – it's not a build problem by the looks of things.

@jonathanhogg jonathanhogg changed the title ESP32: recent IDF versions build woes (crashing, missing memory, iram0 overflow…) ESP32: .iram0.text overflow with recent IDF versions Feb 8, 2022
@jonathanhogg
Copy link
Contributor Author

[Focusing on the main issue from all of this noise that I've generated:]

Given there's a clear constraint on iram0 size with later versions of the IDF, is there a way of having the build variables switch on the version and build with -Os? Or is it possible to build the IDF components with -Os and build MicroPython with -O2? I am insufficiently familiar with the CMake build process to play with this.

dpgeorge pushed a commit that referenced this issue Apr 14, 2022
These boards do not build with IDF v4.4 because the section .iram0.text
does not fit in region iram0_0_seg.  Enabling SPIRAM increases the code
size so use -Os instead of -O2 to build these boards.

Fixes issue #8260.
@dpgeorge
Copy link
Member

GENERIC_SPIRAM and UM_TINYPICO (which also uses SPIRAM) are now unconditionally built with -Os instead of -O2, as of ae3d528. I think that's a reasonable thing to do, otherwise things get too complicated.

This issue should be able to be closed now.

tannewt pushed a commit to tannewt/circuitpython that referenced this issue Aug 12, 2023
@harbaum
Copy link
harbaum commented Dec 5, 2023

I am seeting the same again at this very moment. A clean micropython pull and esp-idf 5.0.2 (which is imho the version currently supposed to use) results in:

region iram0_0_seg' overflowed by 432 bytes`

@robert-hh
Copy link
Contributor

region iram0_0_seg' overflowed by 432 bytes`

I do not get that with a GENERIC_ESP32 build. Which board variant did you try?

@harbaum
Copy link
harbaum commented Dec 5, 2023

Ah, sorry. It's with

make BOARD=ESP32_GENERIC BOARD_VARIANT=SPIRAM

Going back to tag v1.21.0 fixes this. But current git fails.

@robert-hh
Copy link
Contributor

Confirmed. The SPIRAM board variant fails to build. It came with commit:
30b0ee34d esp32: Enable mbedtls cert time validation.

@dpgeorge
Copy link
Member
dpgeorge commented Dec 5, 2023

See #13131.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
0