llvmcortex-mcmsislld

LLD link failure ( expected, but got ) at KEEP(*.init_array)


I am trying to build firmware for Cortex-M0 with clang. The gcc build works of course fine. The build fails during linking

ld.lld: error: baremetal.ld:72: ( expected, but got )
>>>     KEEP(*.init_array)
>>>                      ^
clang-12: error: ld.lld command failed with exit code 1 (use -v to see invocation)

The linker script is pretty standard (and the offending line is also in scripts provided by CMSIS). Relevant part:

.rodata : ALIGN(4)
  {
    *(.rodata .rodata.* .gnu.linkonce.r.*)

    . = ALIGN(4);
    KEEP(*(.init))

    . = ALIGN(4);
    __preinit_array_start = . ;
    KEEP(*(.preinit_array))
    __preinit_array_end = . ;

    . = ALIGN(4);
    __init_array_start = . ;
    KEEP(*(SORT(.init_array.*)))
    KEEP(*.init_array)                <<======== error here
    __init_array_end = . ;

    . = ALIGN(4);
    KEEP(*(.fini))

    . = ALIGN(4);
    __fini_array_start = . ;
    KEEP(*(.fini_array))
    KEEP(*(SORT(.fini_array.*)))
    __fini_array_end = . ;

    . = ALIGN(4);
    KEEP (*crtbegin.o(.ctors))
    KEEP (*(EXCLUDE_FILE (*crtend.o) .ctors))
    KEEP (*(SORT(.ctors.*)))
    KEEP (*crtend.o(.ctors))

    . = ALIGN(0x4);
    KEEP (*crtbegin.o(.dtors))
    KEEP (*(EXCLUDE_FILE (*crtend.o) .dtors))
    KEEP (*(SORT(.dtors.*)))
    KEEP (*crtend.o(.dtors))

    *(.init .init.*)
    *(.fini .fini.*)

    PROVIDE_HIDDEN (__preinit_array_start = .);
    KEEP (*(.preinit_array))
    PROVIDE_HIDDEN (__preinit_array_end = .);
    PROVIDE_HIDDEN (__init_array_start = .);
    KEEP (*(SORT(.init_array.*)))
    KEEP (*(.init_array))
    PROVIDE_HIDDEN (__init_array_end = .);
    PROVIDE_HIDDEN (__fini_array_start = .);
    KEEP (*(.fini_array))
    KEEP (*(SORT(.fini_array.*)))
    PROVIDE_HIDDEN (__fini_array_end = .);

    . = ALIGN (8);
    *(.rom)
    *(.rom.b)

    _etext = . ;
    _sidata = _etext;    /* first address of initialized data in FLASH */

  } >FLASH

My LLD version

$ ld.lld -V    
LLD 13.0.0 (compatible with GNU linkers)

How can I fix the linking?


Solution

  • This linker script is not valid. The error message tells you exactly what you have got wrong: "expected (".

    The specification is filename(section) or KEEP(filename(section)).

    You can't write: KEEP(*.init_array) because there isn't a "(" between the filename and section name.

    If what you mean is "keep the section .init_array from any file", then you need:

    KEEP(*(.init_array))

    If this script was really provided with a released version of CMSIS (and you haven't just made a typo yourself) then you should report this as a bug.