void MCObjectDisassembler::buildSectionAtoms(MCModule *Module) {
  error_code ec;
  for (section_iterator SI = Obj.begin_sections(),
                        SE = Obj.end_sections();
                        SI != SE;
                        SI.increment(ec)) {
    if (ec) break;

    bool isText; SI->isText(isText);
    bool isData; SI->isData(isData);
    if (!isData && !isText)
      continue;

    uint64_t StartAddr; SI->getAddress(StartAddr);
    uint64_t SecSize; SI->getSize(SecSize);
    if (StartAddr == UnknownAddressOrSize || SecSize == UnknownAddressOrSize)
      continue;

    StringRef Contents; SI->getContents(Contents);
    StringRefMemoryObject memoryObject(Contents);

    // We don't care about things like non-file-backed sections yet.
    if (Contents.size() != SecSize || !SecSize)
      continue;
    uint64_t EndAddr = StartAddr + SecSize - 1;

    StringRef SecName; SI->getName(SecName);

    if (isText) {
      MCTextAtom *Text = Module->createTextAtom(StartAddr, EndAddr);
      Text->setName(SecName);
      uint64_t InstSize;
      for (uint64_t Index = 0; Index < SecSize; Index += InstSize) {
        MCInst Inst;
        if (Dis.getInstruction(Inst, InstSize, memoryObject, Index,
                               nulls(), nulls()))
          Text->addInst(Inst, InstSize);
        else
          // We don't care about splitting mixed atoms either.
          llvm_unreachable("Couldn't disassemble instruction in atom.");
      }

    } else {
      MCDataAtom *Data = Module->createDataAtom(StartAddr, EndAddr);
      Data->setName(SecName);
      for (uint64_t Index = 0; Index < SecSize; ++Index)
        Data->addData(Contents[Index]);
    }
  }
}
MCDataAtom *MCDataAtom::split(uint64_t SplitPt) {
  uint64_t LBegin, LEnd, RBegin, REnd;
  remapForSplit(SplitPt, LBegin, LEnd, RBegin, REnd);

  MCDataAtom *RightAtom = Parent->createDataAtom(RBegin, REnd);
  RightAtom->setName(getName());

  std::vector<MCData>::iterator I = Data.begin() + (RBegin - LBegin);
  assert(I != Data.end() && "Split point not found in range!");

  std::copy(I, Data.end(), std::back_inserter(RightAtom->Data));
  Data.erase(I, Data.end());
  return RightAtom;
}
Esempio n. 3
0
void MCObjectDisassembler::buildSectionAtoms(MCModule *Module) {
  error_code ec;
  for (section_iterator SI = Obj.begin_sections(),
                        SE = Obj.end_sections();
                        SI != SE;
                        SI.increment(ec)) {
    if (ec) break;

    bool isText; SI->isText(isText);
    bool isData; SI->isData(isData);
    if (!isData && !isText)
      continue;

    uint64_t StartAddr; SI->getAddress(StartAddr);
    uint64_t SecSize; SI->getSize(SecSize);
    if (StartAddr == UnknownAddressOrSize || SecSize == UnknownAddressOrSize)
      continue;
    StartAddr = getEffectiveLoadAddr(StartAddr);

    StringRef Contents; SI->getContents(Contents);
    StringRefMemoryObject memoryObject(Contents, StartAddr);

    // We don't care about things like non-file-backed sections yet.
    if (Contents.size() != SecSize || !SecSize)
      continue;
    uint64_t EndAddr = StartAddr + SecSize - 1;

    StringRef SecName; SI->getName(SecName);

    if (isText) {
      MCTextAtom *Text = 0;
      MCDataAtom *InvalidData = 0;

      uint64_t InstSize;
      for (uint64_t Index = 0; Index < SecSize; Index += InstSize) {
        const uint64_t CurAddr = StartAddr + Index;
        MCInst Inst;
        if (Dis.getInstruction(Inst, InstSize, memoryObject, CurAddr, nulls(),
                               nulls())) {
          if (!Text) {
            Text = Module->createTextAtom(CurAddr, CurAddr);
            Text->setName(SecName);
          }
          Text->addInst(Inst, InstSize);
          InvalidData = 0;
        } else {
          if (!InvalidData) {
            Text = 0;
            InvalidData = Module->createDataAtom(CurAddr, EndAddr);
          }
          InvalidData->addData(Contents[Index]);
        }
      }
    } else {
      MCDataAtom *Data = Module->createDataAtom(StartAddr, EndAddr);
      Data->setName(SecName);
      for (uint64_t Index = 0; Index < SecSize; ++Index)
        Data->addData(Contents[Index]);
    }
  }
}
void MCObjectDisassembler::buildSectionAtoms(MCModule *Module) {
    for (const SectionRef &Section : Obj.sections()) {
        bool isText;
        Section.isText(isText);
        bool isData;
        Section.isData(isData);
        if (!isData && !isText)
            continue;

        uint64_t StartAddr;
        Section.getAddress(StartAddr);
        uint64_t SecSize;
        Section.getSize(SecSize);
        if (StartAddr == UnknownAddressOrSize || SecSize == UnknownAddressOrSize)
            continue;
        StartAddr = getEffectiveLoadAddr(StartAddr);

        StringRef Contents;
        Section.getContents(Contents);
        StringRefMemoryObject memoryObject(Contents, StartAddr);

        // We don't care about things like non-file-backed sections yet.
        if (Contents.size() != SecSize || !SecSize)
            continue;
        uint64_t EndAddr = StartAddr + SecSize - 1;

        StringRef SecName;
        Section.getName(SecName);

        if (isText) {
            MCTextAtom *Text = nullptr;
            MCDataAtom *InvalidData = nullptr;

            uint64_t InstSize;
            for (uint64_t Index = 0; Index < SecSize; Index += InstSize) {
                const uint64_t CurAddr = StartAddr + Index;
                MCInst Inst;
                if (Dis.getInstruction(Inst, InstSize, memoryObject, CurAddr, nulls(),
                                       nulls())) {
                    if (!Text) {
                        Text = Module->createTextAtom(CurAddr, CurAddr);
                        Text->setName(SecName);
                    }
                    Text->addInst(Inst, InstSize);
                    InvalidData = nullptr;
                } else {
                    assert(InstSize && "getInstruction() consumed no bytes");
                    if (!InvalidData) {
                        Text = nullptr;
                        InvalidData = Module->createDataAtom(CurAddr, CurAddr+InstSize - 1);
                    }
                    for (uint64_t I = 0; I < InstSize; ++I)
                        InvalidData->addData(Contents[Index+I]);
                }
            }
        } else {
            MCDataAtom *Data = Module->createDataAtom(StartAddr, EndAddr);
            Data->setName(SecName);
            for (uint64_t Index = 0; Index < SecSize; ++Index)
                Data->addData(Contents[Index]);
        }
    }
}