@@ -583,42 +583,23 @@ void MCAssembler::writeSectionData(raw_ostream &OS,
583583 const MCSection *Sec) const {
584584 assert (getBackendPtr () && " Expected assembler backend" );
585585
586- // Ignore virtual sections.
587586 if (Sec->isVirtualSection ()) {
588587 assert (getSectionFileSize (*Sec) == 0 && " Invalid size for section!" );
589588
590- // Check that contents are only things legal inside a virtual section.
589+ // Ensure no fixups or non-zero bytes are written to BSS sections, catching
590+ // errors in both input assembly code and MCStreamer API usage. Location is
591+ // not tracked for efficiency.
592+ auto Fn = [](char c) { return c != 0 ; };
591593 for (const MCFragment &F : *Sec) {
592- switch (F.getKind ()) {
593- default : llvm_unreachable (" Invalid fragment in virtual section!" );
594- case MCFragment::FT_Data: {
595- // Check that we aren't trying to write a non-zero contents (or fixups)
596- // into a virtual section. This is to support clients which use standard
597- // directives to fill the contents of virtual sections.
598- if (F.getFixups ().size () || F.getVarFixups ().size ())
599- reportError (SMLoc (), Sec->getVirtualSectionKind () + " section '" +
600- Sec->getName () + " ' cannot have fixups" );
601- for (char C : F.getContents ())
602- if (C) {
603- reportError (SMLoc (), Sec->getVirtualSectionKind () + " section '" +
604- Sec->getName () +
605- " ' cannot have non-zero initializers" );
606- break ;
607- }
594+ if (any_of (F.getContents (), Fn) || any_of (F.getVarContents (), Fn)) {
595+ reportError (SMLoc (), Sec->getVirtualSectionKind () + " section '" +
596+ Sec->getName () +
597+ " ' cannot have non-zero bytes" );
608598 break ;
609599 }
610- case MCFragment::FT_Align:
611- // Check that we aren't trying to write a non-zero value into a virtual
612- // section.
613- assert ((cast<MCAlignFragment>(F).getFillLen () == 0 ||
614- cast<MCAlignFragment>(F).getFill () == 0 ) &&
615- " Invalid align in virtual section!" );
616- break ;
617- case MCFragment::FT_Fill:
618- assert ((cast<MCFillFragment>(F).getValue () == 0 ) &&
619- " Invalid fill in virtual section!" );
620- break ;
621- case MCFragment::FT_Org:
600+ if (F.getFixups ().size () || F.getVarFixups ().size ()) {
601+ reportError (SMLoc (), Sec->getVirtualSectionKind () + " section '" +
602+ Sec->getName () + " ' cannot have fixups" );
622603 break ;
623604 }
624605 }
0 commit comments