llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[MC] Assertion "Cannot set fragment of variable" when a section is named after a symbol equated to a register

Open skaeads12 opened this issue 2 months ago • 2 comments

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 .s file assert in llvm-mc -triple=x86_64-unknown-linux-gnu -filetype=obj (https://godbolt.org/z/6chYavTK3, next to GNU as, which accepts it).
  • -S asserts too, via MCStreamer::switchSection.
  • .section __stack without .popsection asserts the same way (https://godbolt.org/z/54d1jYhYv), but it also leaves the function in the wrong section.

Root cause

  1. .equ __stack, %esp makes __stack a variable symbol whose value is an X86MCExpr (a register).
  2. MCContext::getOrCreateSectionSymbol already refuses to reuse a conflicting symbol as a section symbol: #183001 fixed this same assertion for x=0; .section x. But the check only fires for Sym->isDefined().
  3. X86MCExpr::findAssociatedFragment() returns null, so a register-equated symbol counts as undefined and the check is skipped.
  4. The equated symbol becomes the section's begin symbol, and changeSection calls setFragment on 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 -c and 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, %esp itself. 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 redefinition error).
  • Found by a fuzzer (generated C program), reduced by hand.

skaeads12 avatar Sep 30 '26 05:09 skaeads12

@llvm/issue-subscribers-llvm-mc

Author: skaeads12

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 .s file assert in llvm-mc -triple=x86_64-unknown-linux-gnu -filetype=obj (https://godbolt.org/z/6chYavTK3, next to GNU as, which accepts it).
  • -S asserts too, via MCStreamer::switchSection.
  • .section __stack without .popsection asserts the same way (https://godbolt.org/z/54d1jYhYv), but it also leaves the function in the wrong section.

Root cause

  1. .equ __stack, %esp makes __stack a variable symbol whose value is an X86MCExpr (a register).
  2. MCContext::getOrCreateSectionSymbol already refuses to reuse a conflicting symbol as a section symbol: #183001 fixed this same assertion for x=0; .section x. But the check only fires for Sym-&gt;isDefined().
  3. X86MCExpr::findAssociatedFragment() returns null, so a register-equated symbol counts as undefined and the check is skipped.
  4. The equated symbol becomes the section's begin symbol, and changeSection calls setFragment on it.

Suggested fix (untested)

Either extend the check to variable symbols (e.g. Sym-&gt;isDefined() || Sym-&gt;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 -c and 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, %esp itself. 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 redefinition error).
  • 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.

schizophrenicmaniac avatar Sep 30 '26 15:09 schizophrenicmaniac