[MC] Assertion "Cannot set fragment of variable" when a section is named after a symbol equated to a register
Inline asm that equates a symbol to a register and then uses the same name for a section asserts in MCSymbol::setFragment. GNU as accepts the input, and clang release builds accept it silently.
Reproducer
https://godbolt.org/z/sz1Po3eoW (assertions trunk, release trunk and GCC 16.2, all compiling to an object file)
void a(void) {
asm(".equ __stack, %esp\n"
".pushsection __stack,\"\"\n"
".popsection\n");
}
clang --target=x86_64-unknown-linux-gnu -O2 -c repro.c (the -O level does not matter)
clang: llvm/include/llvm/MC/MCSymbol.h:258: void llvm::MCSymbol::setFragment(llvm::MCFragment*) const: Assertion `!isVariable() && "Cannot set fragment of variable"' failed.
#10 llvm::MCObjectStreamer::changeSection(llvm::MCSection*, unsigned int)
#11 llvm::MCELFStreamer::changeSection(llvm::MCSection*, unsigned int)
#12 llvm::MCStreamer::switchSection(llvm::MCSection*, unsigned int)
#14 (anonymous namespace)::ELFAsmParser::parseSectionArguments(bool, llvm::SMLoc)
- The same three lines as a
.sfile assert inllvm-mc -triple=x86_64-unknown-linux-gnu -filetype=obj(https://godbolt.org/z/6chYavTK3, next to GNU as, which accepts it). -Sasserts too, viaMCStreamer::switchSection..section __stackwithout.popsectionasserts the same way (https://godbolt.org/z/54d1jYhYv), but it also leaves the function in the wrong section.
Root cause
.equ __stack, %espmakes__stacka variable symbol whose value is anX86MCExpr(a register).MCContext::getOrCreateSectionSymbolalready refuses to reuse a conflicting symbol as a section symbol: #183001 fixed this same assertion forx=0; .section x. But the check only fires forSym->isDefined().X86MCExpr::findAssociatedFragment()returns null, so a register-equated symbol counts as undefined and the check is skipped.- The equated symbol becomes the section's begin symbol, and
changeSectioncallssetFragmenton it.
Suggested fix (untested)
Either extend the check to variable symbols (e.g. Sym->isDefined() || Sym->isVariable()), which gives the existing invalid symbol redefinition error, or do not reuse the symbol, since GNU as accepts this input. LLVM already rejects the absolute case (.set foo, 1 / .pushsection foo, which gas also accepts), so an error here would be consistent with existing behaviour.
Details
- Classification: crash on valid input. GCC 16.2 with
-cand GNU as 2.44 both assemble it without a diagnostic. - Release builds (7.0, 22.1, 23.1, trunk): accept it silently.
- History: asserts in every assertions build tried from 7.0 to trunk (up to 21.1 under
MCContext::getELFSection); 4.0 to 6.0 reject.equ __stack, %espitself. Not bisected. - Tested: clang 24.0.0git ef45ca75ce72 (Compiler Explorer assertions trunk, 2026-09-29).
- Related: #183001 (merged; the same assertion for an equated constant). #42657 (open; unhelpful message for that
invalid symbol redefinitionerror). - Found by a fuzzer (generated C program), reduced by hand.
@llvm/issue-subscribers-llvm-mc
Author: skaeads12
Reproducer
https://godbolt.org/z/sz1Po3eoW (assertions trunk, release trunk and GCC 16.2, all compiling to an object file)
void a(void) {
asm(".equ __stack, %esp\n"
".pushsection __stack,\"\"\n"
".popsection\n");
}
clang --target=x86_64-unknown-linux-gnu -O2 -c repro.c (the -O level does not matter)
clang: llvm/include/llvm/MC/MCSymbol.h:258: void llvm::MCSymbol::setFragment(llvm::MCFragment*) const: Assertion `!isVariable() && "Cannot set fragment of variable"' failed.
#<!-- -->10 llvm::MCObjectStreamer::changeSection(llvm::MCSection*, unsigned int)
#<!-- -->11 llvm::MCELFStreamer::changeSection(llvm::MCSection*, unsigned int)
#<!-- -->12 llvm::MCStreamer::switchSection(llvm::MCSection*, unsigned int)
#<!-- -->14 (anonymous namespace)::ELFAsmParser::parseSectionArguments(bool, llvm::SMLoc)
- The same three lines as a
.sfile assert inllvm-mc -triple=x86_64-unknown-linux-gnu -filetype=obj(https://godbolt.org/z/6chYavTK3, next to GNU as, which accepts it). -Sasserts too, viaMCStreamer::switchSection..section __stackwithout.popsectionasserts the same way (https://godbolt.org/z/54d1jYhYv), but it also leaves the function in the wrong section.
Root cause
.equ __stack, %espmakes__stacka variable symbol whose value is anX86MCExpr(a register).MCContext::getOrCreateSectionSymbolalready refuses to reuse a conflicting symbol as a section symbol: #183001 fixed this same assertion forx=0; .section x. But the check only fires forSym->isDefined().X86MCExpr::findAssociatedFragment()returns null, so a register-equated symbol counts as undefined and the check is skipped.- The equated symbol becomes the section's begin symbol, and
changeSectioncallssetFragmenton it.
Suggested fix (untested)
Either extend the check to variable symbols (e.g. Sym->isDefined() || Sym->isVariable()), which gives the existing invalid symbol redefinition error, or do not reuse the symbol, since GNU as accepts this input. LLVM already rejects the absolute case (.set foo, 1 / .pushsection foo, which gas also accepts), so an error here would be consistent with existing behaviour.
Details
- Classification: crash on valid input. GCC 16.2 with
-cand GNU as 2.44 both assemble it without a diagnostic. - Release builds (7.0, 22.1, 23.1, trunk): accept it silently.
- History: asserts in every assertions build tried from 7.0 to trunk (up to 21.1 under
MCContext::getELFSection); 4.0 to 6.0 reject.equ __stack, %espitself. Not bisected. - Tested: clang 24.0.0git ef45ca75ce72 (Compiler Explorer assertions trunk, 2026-09-29).
- Related: #183001 (merged; the same assertion for an equated constant). #42657 (open; unhelpful message for that
invalid symbol redefinitionerror). - Found by a fuzzer (generated C program), reduced by hand.
Hi, I'd like to work on this. getOrCreateSectionSymbol in MCContext.cpp only rejects the reused symbol when isDefined(), so I'd also check isVariable() to get the existing "invalid symbol redefinition" error, and add the register-equated case to test/MC/ELF/section-sym-err.s.