-->
Page 1 of 2

GCC compiler and linker settings thread

PostPosted: Wed Mar 27, 2019 7:39 pm
by davydnorris
I'm starting this thread to capture the conversation thread that started on the NonOS Git repo where compiler settings were discussed.

Re: GCC compiler and linker settings thread

PostPosted: Wed Mar 27, 2019 7:41 pm
by davydnorris
These are my current settings, which I modified based on the discussion. I know others have slightly different settings too so post them up!


Code: Select allDEFINES = -D__ets__  -DICACHE_FLASH -DMEM_DEFAULT_USE_DRAM -DSPI_FLASH_SIZE_MAP=${SPI_SIZE_MAP} -DBUILD_ID="\"I${BUILD_DATE}\"" -DDEBUG_ON -DMQTT_DEBUG_ON

# compiler flags using during compilation of source files
CFLAGS = -std=gnu99 -flto -Wpointer-arith -Wundef -Werror -Wmissing-prototypes -Wstrict-prototypes -fno-builtin -fno-builtin-printf -fno-guess-branch-probability -freorder-blocks-and-partition -fno-cse-follow-jumps -ffunction-sections -fdata-sections -fno-inline-functions -nostdlib -mlongcalls -mtext-section-literals -mno-serialize-volatile ${DEFINES}
CXXFLAGS = $(CFLAGS) -fno-rtti -fno-exceptions

# linker flags used to generate the main object file
LDFLAGS = $(CFLAGS) -Wl,-EL -Wl,-flto -Wl,-gc-sections -Wl,--no-check-sections -u call_user_start -Wl,-static -Wl,--print-memory-usage -Wl,--cref -Wl,-Map=link.map #-Wl,-print-map

# linker script used for the above linker step
LD_SCRIPT = eagle.app.v6.ld

ifeq ($(ENABLE_GDB), 1)
   CFLAGS += -Og -ggdb -DENABLE_GDB=1
   MODULES       += gdbstub
   EXTRA_INCDIR += gdbstub
else
   CFLAGS += -Os
endif

Re: GCC compiler and linker settings thread

PostPosted: Mon Apr 01, 2019 2:03 pm
by eriksl
Good initiative!

Do you need this one?
Code: Select all-Wl,--no-check-sections
I think it's dangerous and not required.

Regarding LTO: I've experienced most of the time IRAM usage decreases quite a bit, but:
- not always, sometime the usage even goes up
- it will mangle all symbols, if you're debugging or scanning memory usage, turn it off
- I'd recommend these flags using fto:
Code: Select all-flto=8 -flto-compression-level=0 -fuse-linker-plugin -ffat-lto-objects -flto-partition=max
. -flto=x uses x threads, which may speed up linking; also you must use fat-lto-object or otherwise esptool(2) can't handle the resulting object file. Mostly -flto-partition=max gives best results (other than the default), but not always.
- no need to use
Code: Select all-Wl,-flto
, the linker recognises -flto. Even more, it's required that the linker gets exactly the same flags as the compiler (in this case the linker will do part of the actual compilation, the compiler will only generate intermediate code).

Re: GCC compiler and linker settings thread

PostPosted: Mon Apr 01, 2019 2:05 pm
by eriksl
The relevant parts of my makefile:

Code: Select allWARNINGS      :=   -Wall -Wextra -Werror \
                  -Wformat-overflow=2 -Wshift-overflow=2 -Wimplicit-fallthrough=5 \
                  -Wformat-signedness -Wformat-truncation=2 \
                  -Wstringop-overflow=4 -Wunused-const-variable=2 -Walloca \
                  -Warray-bounds=2 -Wswitch-bool -Wsizeof-array-argument \
                  -Wduplicated-branches -Wduplicated-cond -Wlto-type-mismatch -Wnull-dereference \
                  -Wdangling-else -Wno-incompatible-pointer-types \
                  -Wpacked -Wfloat-equal -Winit-self -Wmissing-include-dirs -Wstrict-overflow=2 \
                  -Wno-format -Wmissing-noreturn -Wbool-compare \
                  -Wsuggest-attribute=noreturn -Wsuggest-attribute=format -Wmissing-format-attribute \
                  -Wuninitialized -Wtrampolines -Wframe-larger-than=1024 \
                  -Wunsafe-loop-optimizations -Wshadow -Wpointer-arith -Wbad-function-cast \
                  -Wcast-qual -Wwrite-strings -Wsequence-point -Wlogical-op -Wlogical-not-parentheses \
                  -Wredundant-decls -Wvla -Wdisabled-optimization \
                  -Wunreachable-code -Wparentheses -Wdiscarded-array-qualifiers \
                  -Wmissing-prototypes -Wold-style-definition -Wold-style-declaration -Wmissing-declarations \
                  -Wcast-align -Winline \
                  -Wno-pointer-sign -Wno-unused-parameter -Wno-attributes \
                  -Wno-error=cast-qual -Wno-error=packed -Wno-error=unsafe-loop-optimizations

CFLAGS         :=   -pipe -O3 -g -std=gnu11 -fdiagnostics-color=always \
                  -ffreestanding -fno-inline -mlongcalls -mno-serialize-volatile -mno-target-align \
                  -fno-math-errno -fno-printf-return-value \
                  -fno-tree-forwprop \
                  -ffunction-sections -fdata-sections

ifeq ($(USE_LTO),1)
CFLAGS          +=   -flto=8 -flto-compression-level=0 -fuse-linker-plugin -ffat-lto-objects -flto-partition=max
endif

CFLAGS         +=   -D__ets__ -DICACHE_FLASH -DLWIP_OPEN_SRC -DPBUF_RSV_FOR_WLAN -DEBUF_LWIP \
                  -DBOOT_BIG_FLASH=1 -DBOOT_RTC_ENABLED=1 \
                  -DIMAGE_TYPE=$(IMAGE) -DIMAGE_OTA=$(IMAGE_OTA) \
                  -DUSER_CONFIG_SECTOR=$(USER_CONFIG_SECTOR) -DUSER_CONFIG_OFFSET=$(USER_CONFIG_OFFSET) -DUSER_CONFIG_SIZE=$(USER_CONFIG_SIZE) \
                  -DRFCAL_OFFSET=$(RFCAL_OFFSET) -DRFCAL_SIZE=$(RFCAL_SIZE) \
                  -DPHYDATA_OFFSET=$(PHYDATA_OFFSET) -DPHYDATA_SIZE=$(PHYDATA_SIZE) \
                  -DSYSTEM_CONFIG_OFFSET=$(SYSTEM_CONFIG_OFFSET) -DSYSTEM_CONFIG_SIZE=$(SYSTEM_CONFIG_SIZE) \
                  -DOFFSET_OTA_IMG_0=$(OFFSET_OTA_IMG_0) -DOFFSET_OTA_IMG_1=$(OFFSET_OTA_IMG_1) -DSIZE_OTA_IMG=$(SIZE_OTA_IMG) \
                  -DOFFSET_IRAM_PLAIN=$(OFFSET_IRAM_PLAIN) -DSIZE_IRAM_PLAIN=$(SIZE_IRAM_PLAIN) \
                  -DOFFSET_IROM_PLAIN=$(OFFSET_IROM_PLAIN) -DSIZE_IROM_PLAIN=$(SIZE_IROM_PLAIN) \
                  -DSEQUENCER_FLASH_OFFSET=$(SEQUENCER_FLASH_OFFSET_0) -DSEQUENCER_FLASH_SIZE=$(SEQUENCER_FLASH_SIZE) \
                  -DSEQUENCER_FLASH_OFFSET_0=$(SEQUENCER_FLASH_OFFSET_0) -DSEQUENCER_FLASH_OFFSET_1=$(SEQUENCER_FLASH_OFFSET_1) \
                  -DOFFSET_OTA_BOOT=$(OFFSET_OTA_BOOT) -DSIZE_OTA_BOOT=$(SIZE_OTA_BOOT) \
                  -DOFFSET_OTA_RBOOT_CFG=$(OFFSET_OTA_RBOOT_CFG) -DSIZE_OTA_RBOOT_CFG=$(SIZE_OTA_RBOOT_CFG) \
                  -DFLASH_SIZE_SDK=$(FLASH_SIZE_SDK)


Code: Select allCINC         := -I$(HAL)/include \
               -I$(ESPOPENSDK)/xtensa-lx106-elf/xtensa-lx106-elf/include \
               -I$(LWIP)/include -I$(LWIP)/include/lwip -I .

LDFLAGS         := -L. -L$(ESPSDK)/lib -Wl,--size-opt -Wl,--print-memory-usage -Wl,--gc-sections -Wl,--cref -Wl,-Map=$(LINKMAP) -nostdlib -u call_user_start -Wl,-static
SDKLIBS         := -lhal -lpp -lphy -lnet80211 -lwpa
LWIPLIBS      := -l$(LIBLWIPAPP) -l$(LIBLWIPCORE) -l$(LIBLWIPNETIF)
STDLIBS         := -lm -lgcc -lcrypto