使用Clang构建Cortex-M0固件时LLD链接报错:KEEP(*.init_array)语法错误修复咨询
KEEP(*.init_array) for Cortex-M0 Let's break down what's happening here and how to resolve this quickly:
The Root Cause
GNU ld (the linker bundled with GCC) allows a convenient shorthand for selecting sections in linker scripts—you can write *.init_array instead of the explicit *(.init_array). However, LLD enforces stricter adherence to the official linker script grammar, which requires that section matching patterns are wrapped in parentheses immediately after the wildcard *.
Your error line KEEP(*.init_array) uses the GNU ld shorthand that LLD doesn't recognize. It expects the fully compliant KEEP(*(.init_array)) syntax instead.
The Fix
Update the problematic line (and any other matching instances in your script) to include the required parentheses:
KEEP(*(.init_array)) // Replaces the original KEEP(*.init_array)
While you're editing, scan the rest of your linker script for other shorthand section references. For example, if you see KEEP(*.fini_array) later in the script, adjust that to KEEP(*(.fini_array)) as well to avoid similar errors.
Why This Works for GCC but Not Clang/LLD
GNU ld has supported these shorthand notations as a legacy convenience, but LLD was built to follow the linker script specification more strictly. This strictness eliminates ambiguity and ensures better consistency with standard script syntax across different toolchains.
Verify the Fix
After making the syntax adjustments, re-run your Clang build command. The linker error should disappear, and your firmware should complete linking successfully just like it does with GCC.
内容的提问来源于stack exchange,提问作者filo

