This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

LP-AM261: Migrating AM261x SBL from TI Clang to IAR: Unexpected binary size increase

Part Number: LP-AM261

Tool/software:

Hi Team,

I am working on AM261x-LP, with IAR toolchain using CMake Build system. Since AM261x was not supported by IAR(9.5), I upgraded my IAR to 9.7 which has the support now.

Initially I had used TIClang 4.0.1 with CMakebuild system. I was able to build & flash Secondary Bootloader and a sample blinky application by enabling the XIP mode.

With TIClang I didnt have any issues, but I want to use IAR toolchain. For that I started with Secondary Bootloader(SBL_OSPI), I am able to build the SBL but the generated binary size is too large. Below is the size differences of both toolchains:
a) TI Compiler: out image size -> 983KB and Post build bin size is 218KB, and able to sign using mcu_rom_image_gen.py to generate sbl_ospi.tiimage.
b) IAR Compiler: out image size -> 1.6MB and Post build bin size is 1.75Gb.

Since it is bootloader mcu_rom_image_gen.py expects bin file size to be less than 985KB.


Changes done for IAR CMake build:

1) Changed Baremetal kernel files to be compatible with IAR , Since existing Assembly files support TIClang only.
Files from mcu_plus_sdk_am261x_10_02_00_15 :
boot_armv7r_asm.S
CacheP_armv7r_asm.S
Cpuid_armv7r_asm.S
HwiP_armv7r_asm.S
HwiP_armv7r_handlers_nortos_asm.S
HwiP_armv7r_vectors_nortos_asm.S
MpuP_armv7r_asm.S
PmuP_armv7r_asm.S

Sample Snip of CacheP_armv7r_asm Assembly:
Before:
/* FUNCTION DEF: void CacheP_enableL1d(void) */
.global CacheP_enableL1d
.type CacheP_enableL1d,%function
.section ".text.cache","ax",%progbits
.arm
.align 2
CacheP_enableL1d:
mrc p15, #0, r0, c1, c0, #0 // read SCR register
orr r0, r0, #0x0004 // set C bit (bit 2) to 1
dsb
mcr p15, #0, r1, c15, c5, #0 // Invalidate entire data cache
mcr p15, #0, r0, c1, c0, #0 // L1D cache enabled
bx lr

After:
/* FUNCTION DEF: void CacheP_enableL1d(void) */
PUBLIC CacheP_enableL1d
SECTION .text_cache:CODE:NOROOT(2) ; keep function in .text, align 2 bytes
ARM
CacheP_enableL1d:
mrc p15, #0, r0, c1, c0, #0 // read SCR register
orr r0, r0, #0x0004 // set C bit (bit 2) to 1
dsb
mcr p15, #0, r1, c15, c5, #0 // Invalidate entire data cache
mcr p15, #0, r0, c1, c0, #0 // L1D cache enabled
bx lr

2) IAR compiler Flags in CMake:
# Compilers
set(CMAKE_C_COMPILER "${TOOLCHAIN_BIN}/iccarm.exe")
set(CMAKE_CXX_COMPILER "${TOOLCHAIN_BIN}/iccarm.exe")
set(CMAKE_ASM_COMPILER "${TOOLCHAIN_BIN}/iasmarm.exe")
set(CMAKE_AR "${TOOLCHAIN_BIN}/iarchive.exe")
set(CMAKE_LINKER "${TOOLCHAIN_BIN}/ilinkarm.exe")
set(CMAKE_OBJCOPY "${TOOLCHAIN_BIN}/ielftool.exe")

a)C- CPP Flags: set(TARGET_MCU_FLAGS
"--cpu=cortex-r5"
"--fpu=vfpv3"
"--endian=little"
"--thumb"
)

b)Common-Flags: set(COMPILER_FLAGS
"-e"
"--diag_suppress=Pa050"
"--no_path_in_file_macros"
"--no_cse"
"--no_unroll"
"--no_inline"
"--no_code_motion"
"--no_tbaa"
"--no_clustering"
"--no_scheduling"
"--mfc"
)
c)CMAKE_ASM_FLAGS: set(CMAKE_ASM_FLAGS
${TARGET_MCU_FLAGS}
"-s+"
"-w+"
# "-r"
"-M<>"
)
d)Debug & Release Flags:
set(CMAKE_C_FLAGS_DEBUG "-On --debug")
set(CMAKE_C_FLAGS_RELEASE "-Os -guard_calls")

e) Linker Flags:
set(CMAKE_EXE_LINKER_FLAGS
"--map +"
"--inline"
"--no_exceptions"
"--vfe"
"--merge_duplicate_sections"
"--redirect _Printf=_PrintfLargeNoMb"
"--redirect _Scanf=_ScanfFullNoMb"
"--no_remove"
)

3)Changes to linker script(.icf) to align with sections defined:

/* Stack and heap sizes */
define symbol __ICFEDIT_size_cstack__ = 0x2000; /* 8kb */
define symbol __ICFEDIT_size_heap__ = 0x8000; /* 32768 */

/* R5 mode-specific stacks */
define symbol __IRQ_STACK_SIZE = 0x1000; /* 4096 */
define symbol __FIQ_STACK_SIZE = 0x0100; /* 256 */
define symbol __SVC_STACK_SIZE = 0x1000; /* 4096 */
define symbol __ABORT_STACK_SIZE = 0x0100; /* 256 */
define symbol __UNDEFINED_STACK_SIZE = 0x0100; /* 256 */

/*------------------------------------------------------------------*/
/* MEMORY regions */
/*------------------------------------------------------------------*/
define memory mem with size = 4G;

define region R5F_VECS = mem:[from 0x00000000 to 0x000000FF];
define region R5F_TCMA = mem:[from 0x00000100 to 0x00007FFF];
define region R5F_TCMB0 = mem:[from 0x00080000 to 0x00087FFF];
define region MSRAM_VECS = mem:[from 0x70002000 to 0x700020FF];
define region MSRAM_0 = mem:[from 0x70002100 to 0x70031FFF];
define region MSRAM_HSMRT = mem:[from 0x70032000 to 0x70071FFF];
define region MAILBOX_HSM = mem:[from 0x44000000 to 0x440003CD];
define region MAILBOX_R5F = mem:[from 0x44000400 to 0x440007CD];

/*------------------------------------------------------------------*/
/* BLOCKS for stacks + heap */
/*------------------------------------------------------------------*/
define block CSTACK with alignment = 8, size = __ICFEDIT_size_cstack__ { rw section CSTACK };
define block HEAP with alignment = 8, size = __ICFEDIT_size_heap__ { };
define block IRQ_STACK with alignment = 8, size = __IRQ_STACK_SIZE { rw section IRQ_STACK };
define block FIQ_STACK with alignment = 8, size = __FIQ_STACK_SIZE { rw section FIQ_STACK };
define block SVC_STACK with alignment = 8, size = __SVC_STACK_SIZE { rw section SVC_STACK };
define block ABT_STACK with alignment = 8, size = __ABORT_STACK_SIZE { rw section ABT_STACK };
define block UND_STACK with alignment = 8, size = __UNDEFINED_STACK_SIZE { rw section UND_STACK};

place in MSRAM_0 {
readwrite section .bss,
readwrite section .sysmem,
readwrite section .stack,

block CSTACK,
block HEAP,
block IRQ_STACK,
block FIQ_STACK,
block SVC_STACK,
block ABT_STACK,
block UND_STACK
};

/*------------------------------------------------------------------*/
/* PLACEMENT */
/*------------------------------------------------------------------*/
place at address mem:0x00000000 { readonly section .intvec };

place in MSRAM_VECS {
readonly section .sbl_init_code,
readonly section .vectors
};

place in MSRAM_0
{
/* text/ro */
readonly section .text,
readonly section .text_hwi,
readonly section .text_pmu,
readonly section .text_cache,
readonly section .text_boot,
readonly section .rodata,
readonly section .rodata_cfg,
readonly section .iar.init_table,
readonly section .data.pmu_init,

/* data/rw */
readwrite section .data,
readwrite section .data.pmu,
};

place in MSRAM_HSMRT {
readonly section .rodata.hsmrt
};

place in MAILBOX_HSM {
readwrite section .bss.sipc_hsm_queue_mem
};

place in MAILBOX_R5F {
readwrite section .bss.sipc_secure_host_queue_mem
};

/* Mark as NOLOAD */
do not initialize { section .bss.sipc_hsm_queue_mem };
do not initialize { section .bss.sipc_secure_host_queue_mem };




There is no change in Application code, I am using same for both toolchains.
But in my application code I am using "__attribute " as below, do I need to to use #pragma instead of __attribute ? or anything else could make it work .

#define RODATA_CFG_SECTION __attribute__((section(".rodata_cfg")))
/* ----------- CacheP ----------- */
const CacheP_Config gCacheConfig RODATA_CFG_SECTION = {
.enable = 1,
.enableForceWrThru = 0,
};


I need to make AM261x compatible with IAR toolchain.
Any help is greatly appreciated. Thanks!

  • Hi Dileep

    b) IAR Compiler: out image size -> 1.6MB and Post build bin size is 1.75Gb.

    This seems like a configuration issue, have you looked at the compilation flags.

    Also post build size is abnormal.

  • Would it be possible for you to share the IAR SBL project, so that I can understand the configurations better.

  • #---------------------------------------------------------------------
    # General configuration
    #---------------------------------------------------------------------
    cmake_minimum_required(VERSION 3.21)
    
    # IAR Toolchain file path
    set(CMAKE_TOOLCHAIN_FILE "${CMAKE_SOURCE_DIR}/Cmake/iar.cmake" CACHE STRING "Toolchain file")
    
    
    # Set the project name
    project(AM261X_Blinky C CXX ASM)
    
    # Define the path to the SDK 
    set(SDK_ROOT "C:/ti/mcu_plus_sdk_am261x_10_02_00_15")
    
    set(CMAKE_VERBOSE_MAKEFILE ON)
    
    # === Source files ===
    add_executable(AM261X_Blinky
        ${CMAKE_SOURCE_DIR}/Core/main.c
        ${CMAKE_SOURCE_DIR}/Core/board.c
    
        # Generated source files
        ${CMAKE_SOURCE_DIR}/Generated_source/ti_board_open_close.c
        ${CMAKE_SOURCE_DIR}/Generated_source/ti_dpl_config.c
        ${CMAKE_SOURCE_DIR}/Generated_source/ti_drivers_config.c
        ${CMAKE_SOURCE_DIR}/Generated_source/ti_drivers_open_close.c
        ${CMAKE_SOURCE_DIR}/Generated_source/ti_pinmux_config.c
        ${CMAKE_SOURCE_DIR}/Generated_source/ti_power_clock_config.c
        ${CMAKE_SOURCE_DIR}/startup_am261x.s
    
        #SDK Dependency
        ${CMAKE_SOURCE_DIR}/SDK_dependency/source/soc.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/source/soc_rcm.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/source/pinmux.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/source/ospi_v0.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/source/ospi_v0_lld.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/source/i2c_v1.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/source/i2c_v1_lld.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/source/ClockP_nortos.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/source/ClockP_nortos_r5.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/source/bootloader.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/source/bootloader_soc.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/source/bootloader_hsmRt_load.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/source/bootloader_flash.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/source/bootloader_profile.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/source/pmic.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/source/pmic_lld.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/source/pmic_tps65036xx.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/source/pmic_io.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/source/pmic_irq.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/source/pmic_core.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/source/ioexp_tca6408.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/source/flash.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/source/flash_nor_ospi.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/source/ospi_edma_lld.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/source/ospi_lld_dma.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/source/ospi_phy_am26x_lld.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/source/edma.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/source/hsmclient.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/source/sipc_notify_src.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/source/TimerP_rti_nortos.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/source/TimerP_rti_priv.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/source/hsmclient_loadhsmrt.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/source/hsmclient_wait_for_boot_notify.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/source/pmic_wdg.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/source/ospi_phy_am26x.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/source/sipc_notify_cfg.c
    
        ${CMAKE_SOURCE_DIR}/SDK_dependency/nortos_dependency/DebugP_log.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/nortos_dependency/DebugP_nortos.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/nortos_dependency/printf.c
    
    
        #nortos_lib
        ${CMAKE_SOURCE_DIR}/SDK_dependency/nortos_dependency/HwiP_armv7r_asm.S
        ${CMAKE_SOURCE_DIR}/SDK_dependency/nortos_dependency/HwiP_armv7r_handlers_nortos.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/nortos_dependency/Cpuid_armv7r_asm.S
        ${CMAKE_SOURCE_DIR}/SDK_dependency/nortos_dependency/HwiP_armv7r_vim.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/nortos_dependency/CacheP_armv7r_asm.S
        ${CMAKE_SOURCE_DIR}/SDK_dependency/nortos_dependency/CacheP_armv7r.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/nortos_dependency/SemaphoreP_nortos.c
        ${CMAKE_SOURCE_DIR}/SDK_dependency/nortos_dependency/PmuP_armv7r_asm.S
        ${CMAKE_SOURCE_DIR}/SDK_dependency/nortos_dependency/PmuP_armv7r.c
        # ${CMAKE_SOURCE_DIR}/SDK_dependency/nortos_dependency/boot_armv7r.c
        # ${CMAKE_SOURCE_DIR}/SDK_dependency/nortos_dependency/CacheP_armv7r.c
        # ${CMAKE_SOURCE_DIR}/SDK_dependency/nortos_dependency/CacheP_armv7r.c
        # ${CMAKE_SOURCE_DIR}/SDK_dependency/nortos_dependency/HwiP_armv7r_vectors_nortos.c
        # ${CMAKE_SOURCE_DIR}/SDK_dependency/nortos_dependency/HwiP_armv7r_vectors_nortos_asm.c
        # ${CMAKE_SOURCE_DIR}/SDK_dependency/nortos_dependency/HwiP_armv7r_vectors_nortos_sbl_asm.c
    
    
    
    
        
    )
      
    # Define the build type
    if(NOT CMAKE_BUILD_TYPE)
        set(CMAKE_BUILD_TYPE "Debug")
    endif()
    
    
     
    set_target_properties(AM261X_Blinky PROPERTIES 
        OUTPUT_NAME "${CMAKE_PROJECT_NAME}"
        SUFFIX      ".out"
        )
    
    # Debug message to check toolchain file usage
    message(STATUS "Using toolchain file: ${CMAKE_TOOLCHAIN_FILE}")
    
    # Include directories
    target_include_directories(AM261X_Blinky PRIVATE
        ${SDK_ROOT}
        ${SDK_ROOT}/source
        ${SDK_ROOT}/source/kernel/nortos/dpl/common
        ${SDK_ROOT}/source/security/
        ${SDK_ROOT}/source/security/security_common/drivers/hsmclient
        ${SDK_ROOT}/source/kernel/nortos/dpl/r5
        ${SDK_ROOT}/source/kernel
        ${CMAKE_SOURCE_DIR}/Generated_source
        ${CMAKE_SOURCE_DIR}/SDK_dependency/include
        ${TI_TOOLCHAIN_PATH}/include/c
        ${TI_TOOLCHAIN_PATH}/include/c++/v1
        ${TI_TOOLCHAIN_PATH}/include/armv7reb-ti-none-eabihf/c++/v1
        ${SDK_ROOT}/source/board/pmic/pmic_lld/derby/include
    )
    
    # Preprocessor macros
    target_compile_definitions(AM261X_Blinky PRIVATE
        SOC_AM261X
        DERBY
    )
    
    # Compiler options
    target_compile_options(AM261X_Blinky PRIVATE ${IAR_COMMON_FLAGS})
    
    
    set(LINKER_SCRIPT "${CMAKE_SOURCE_DIR}/am26xx_r5.icf")
    
    # Linker options for target
    target_link_options(AM261X_Blinky PRIVATE
        "--config=${LINKER_SCRIPT}"
        "--cpu=cortex-r5"                             
        "--fpu=vfpv3"  
        # "--endian=little"                               
    )
    
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --config ${LINKER_SCRIPT}")
    
    string(JOIN " " C_FLAGS_STRING ${CMAKE_EXE_LINKER_FLAGS})
    
    
    # Link libraries
    target_link_libraries(AM261X_Blinky PRIVATE
        # "${SDK_ROOT}/source/board/lib/board.am261x.r5f.ti-arm-clang.release.lib"
        # "${SDK_ROOT}/source/drivers/lib/drivers.am261x.r5f.ti-arm-clang.release.lib"
        # "${SDK_ROOT}/source/kernel/nortos/lib/nortos.am261x.r5f.ti-arm-clang.release.lib"
        # "${SDK_ROOT}/source/security/lib/security.am261x.r5f.ti-arm-clang.release.lib"
        # "${SDK_ROOT}/source/board/pmic/lib/pmic_derby.am261x.r5f.ti-arm-clang.release.lib"
    )
    
    add_custom_command(TARGET AM261X_Blinky POST_BUILD
        COMMAND ${CMAKE_OBJCOPY}
                --bin
                ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}.out
                ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}.bin
    )
    
    
    
    
    
    
    --------Here is IAR toolchain.cmake------------------------------
    
    # Toolchain file for IAR Arm 
    
    # Target system and architecture
    set(CMAKE_SYSTEM_NAME        Generic)
    set(CMAKE_SYSTEM_PROCESSOR   arm)
    
    # Force use of specified compilers
    set(CMAKE_C_COMPILER_FORCED   TRUE)
    set(CMAKE_CXX_COMPILER_FORCED TRUE)
    set(CMAKE_ASM_COMPILER_FORCED TRUE)
    
    # IAR toolchain path
    set(IAR_TOOLCHAIN_PATH "C:/Program Files/IAR Systems/IAR_9.7/arm")
    set(TOOLCHAIN_BIN "${IAR_TOOLCHAIN_PATH}/bin")
    
    # Compilers
    set(CMAKE_C_COMPILER   "${TOOLCHAIN_BIN}/iccarm.exe")
    set(CMAKE_CXX_COMPILER "${TOOLCHAIN_BIN}/iccarm.exe")
    set(CMAKE_ASM_COMPILER "${TOOLCHAIN_BIN}/iasmarm.exe")
    set(CMAKE_AR           "${TOOLCHAIN_BIN}/iarchive.exe") 
    set(CMAKE_LINKER       "${TOOLCHAIN_BIN}/ilinkarm.exe") 
    set(CMAKE_OBJCOPY      "${TOOLCHAIN_BIN}/ielftool.exe")
    
    # Output suffix
    set(CMAKE_EXECUTABLE_SUFFIX_ASM  ".out")
    set(CMAKE_EXECUTABLE_SUFFIX_C    ".out")
    set(CMAKE_EXECUTABLE_SUFFIX_CXX  ".out")
    
    # Don't attempt full program builds when testing compilers
    set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
    
    set(TARGET_MCU_FLAGS
        "--cpu=cortex-r5"
        "--fpu=vfpv3"
        "--endian=little"
        "--thumb"
    )
    
    set(COMPILER_FLAGS
        "-e"                 
        "--diag_suppress=Pa050" # Warning[Pa050]: non-native end of line sequence detected
        "--no_path_in_file_macros"
        "--no_cse"
        "--no_unroll"
        "--no_inline"
        "--no_code_motion"
        "--no_tbaa"
        "--no_clustering"
        "--no_scheduling"
        "--mfc"
    )
    
    # Common flags
    set(CMAKE_C_FLAGS
        ${TARGET_MCU_FLAGS}
        ${COMPILER_FLAGS}
    )
    
    # Join flags into a space-separated string for compiler
    string(JOIN " " CMAKE_C_FLAGS ${CMAKE_C_FLAGS})
    
    
    set(CMAKE_ASM_FLAGS
        ${TARGET_MCU_FLAGS}
        "-s+"                          # Enable case sensitivity
        "-w+"                          # Enable warnings
        "-M<>"                         # Set macro quote characters
    )
    
    list(JOIN CMAKE_ASM_FLAGS " " CMAKE_ASM_FLAGS)
    
    
    set(CMAKE_C_FLAGS_DEBUG   "-On --debug")
    set(CMAKE_C_FLAGS_RELEASE "-Ohs -guard_calls")
    
    
    set(IAR_LINKER_ENTRY_ROUTINE "__iar_program_start" CACHE STRING "IAR Linker entry point routine." FORCE)
    
    set(CMAKE_EXE_LINKER_FLAGS
        "--map +"
        "--inline"
        "--no_exceptions"
        "--vfe"
        "--merge_duplicate_sections"
        "--redirect _Printf=_PrintfLargeNoMb"
        "--redirect _Scanf=_ScanfFullNoMb"
        "--no_remove"
        )
    list(JOIN CMAKE_EXE_LINKER_FLAGS " " CMAKE_EXE_LINKER_FLAGS)
    
    message(STATUS "Linker Flags: ${CMAKE_EXE_LINKER_FLAGS}")
    
    

    Hi Nilabh Anand,

    Thank you for your response.

    Due to external security constraints, I'm unable to share the complete project. However, I’ve attached the relevant CMakeLists.txt and iar.cmake toolchain files for your review.

    To clarify, I’m using the standard SBL example project from the MCU+SDK with the above configuration

  • Hi DIleep,

    The script that is use to convert the SBL.out to sbl.tiiimage is located here:

    mcu_plus_sdk/source/security/security_common/tools/boot/signing/rom_image_gen.py

    Please the post build script arguments are correctly given I donot see the post build script command in the above snippet.