Error: invalid ‘static_cast’ from type ‘llvm::MCSection’ to type ‘llvm::MCSectionELF&’

Here’s my code:

// Deal with MFs and MBBs in a ELF code section (.text) only
  for (MCSection &Sec : Layout.getAssembler()) {
    MCSectionELF &ELFSec = static_cast<MCSectionELF &>(Sec);
    std::string tmpSN, sectionName = ELFSec.getSectionName();
    if (sectionName.find(".text") == 0) {
      unsigned totalOffset = 0, totalFixups = 0, totalAlignSize = 0;
      int MFID, MBBID, prevMFID = -1;
      std::string prevID, canFallThrough;
      unsigned MBBSize, MBBOffset, numFixups, alignSize, MBBType;
      std::set<std::string> countedMBBs;
      
      // Per each fragment in a .text section
      for (MCFragment &MCF : Sec) {
        // Here MCDataFragment has combined with the following MCRelaxableFragment or MCAlignFragment
        // Corner case: MCDataFragment with no instruction - just skip it
        if (isa<MCDataFragment>(MCF) && MCF.hasInstructions()) {

        // Update the MBB offset and MF Size for all collected MBBs in the MF
          for (std::string ID : MCF.getAllMBBs()) {
            if (ID.length() == 0 && std::get<0>(MAI->MachineBasicBlocks[ID]) > 0)
              llvm_unreachable("[CCR-Error] MCAssembler(updateReorderInfoValues) - MCSomething went wrong in MCRelaxableFragment: MBB size > 0 with no parentID?");
          
            if (countedMBBs.find(ID) == countedMBBs.end() && ID.length() > 0) {
              bool isStartMF = false; // check if the new MF begins
              std::tie(MFID, MBBID) = separateID(ID);
              std::tie(MBBSize, MBBOffset, numFixups, alignSize, MBBType, tmpSN) = MAI->MachineBasicBlocks[ID];
              
              if (tmpSN.length() > 0) continue;
              MAI->MBBLayoutOrder.push_back(ID);
              
              // Handle a corner case: see handleDirectEmitDirectives() in AsmParser.cpp
              if (MAI->specialCntPriorToFunc > 0) {
                MAI->updateByteCounter(ID, MAI->specialCntPriorToFunc, /*numFixups=*/ 0, /*isAlign=*/ false, /*isInline=*/ false);
                MBBSize += MAI->specialCntPriorToFunc;
                MAI->specialCntPriorToFunc = 0;
              }

Here’s the error I’m getting

/home/pegasus/Documents/LLVM_Rust/CCR-master/llvm/lib/MC/MCAssembler.cpp:880:28: error: invalid ‘static_cast’ from type ‘llvm::MCSection’ to type ‘llvm::MCSectionELF&’
  880 |     MCSectionELF &ELFSec = static_cast<MCSectionELF &>(Sec);

I don’t know how to fix this.

Did you include MCSectionELF.h?

1 Like

Yes I’ve included that.

MCSectionELF is defined as class MCSectionELF final : public MCSection {. The only reason I can think of that a cast from MCSection to MCSectionELF could be illegal is that you don’t have the definition in scope. (That cast exactly as you’ve written it exists in the LLVM source code; see ELFWriter::writeSectionData.)

The error is in casting from non-reference to reference.

I take it back. Gcc doesn’t show & in the error message even if the type is a reference…

Thanks. I had checked MCSectionELF.h but it somehow got erased I hadn’t noticed.