bool MainMemoryAccessBase::doRead(uint32_t address, uint32_t* buffer, size_t count)
{
	MemoryArea* cpu = mm->getMemoryArea("CPU");
	if (!cpu)
		return false;

	uint32_t pc = 0;
	cpu->read(0, &pc, 1);

	bool omitFirst = (address & 0x1);
	if (omitFirst) {
		--address;
		++count;
	}
	bool omitLast = (count & 1);
	if (omitLast) {
		++count;
	}

	const hal_id readMacro = devHandle->supportsQuickMemRead() ?
								ID_ReadMemQuick : ID_ReadMemWords;

	HalExecElement* el = new HalExecElement(this->devHandle->checkHalId(readMacro));
	el->appendInputData32(this->getStart() + address);
	el->appendInputData32(static_cast<uint32_t>(count/2));
	el->appendInputData32(pc);

	el->setOutputSize(count);

	ReadElement r(buffer, count, omitFirst, omitLast, 0);
	this->readMap[this->elements.size()] = r;
	this->elements.push_back(el);
	return true;
}
Esempio n. 2
0
TEST_F( ELFReaderTest, read_symbol_and_rela )
{
  ASSERT_TRUE(m_pInput->hasMemArea());
  ASSERT_TRUE(m_pInput->hasContext());
  m_pInput->setType(Input::Object);

  // -- read symbols
  LDSection* symtab_shdr = m_pInput->context()->getSection(".symtab");
  ASSERT_TRUE(NULL!=symtab_shdr);

  LDSection* strtab_shdr = symtab_shdr->getLink();
  ASSERT_TRUE(NULL!=strtab_shdr);

  MemoryRegion* symtab_region = m_pInput->memArea()->request(
                         m_pInput->fileOffset() + symtab_shdr->offset(),
                         symtab_shdr->size());

  MemoryRegion* strtab_region = m_pInput->memArea()->request(
                         m_pInput->fileOffset() + strtab_shdr->offset(),
                         strtab_shdr->size());
  char* strtab = reinterpret_cast<char*>(strtab_region->start());
  bool result = m_pELFReader->readSymbols(*m_pInput, *m_pIRBuilder,
                                          *symtab_region, strtab);
  ASSERT_TRUE(result);
  ASSERT_EQ("hello.c", std::string(m_pInput->context()->getSymbol(1)->name()));
  ASSERT_EQ("puts", std::string(m_pInput->context()->getSymbol(10)->name()));
  ASSERT_TRUE(NULL==m_pInput->context()->getSymbol(11));
  m_pInput->memArea()->release(symtab_region);
  m_pInput->memArea()->release(strtab_region);

  // -- read relocations
  MemoryArea* mem = m_pInput->memArea();
  LDContext::sect_iterator rs = m_pInput->context()->relocSectBegin();
  ASSERT_TRUE(rs!=m_pInput->context()->relocSectEnd());
  ASSERT_EQ(".rela.text", (*rs)->name());

  uint64_t offset = m_pInput->fileOffset() + (*rs)->offset();
  uint64_t size = (*rs)->size();
  MemoryRegion* region = mem->request(offset, size);
  IRBuilder::CreateRelocData(**rs); /// create relocation data for the header

  ASSERT_EQ(llvm::ELF::SHT_RELA, (*rs)->type());
  ASSERT_TRUE(m_pELFReader->readRela(*m_pInput, **rs, *region));
  mem->release(region);

  const RelocData::RelocationListType &rRelocs =
                          (*rs)->getRelocData()->getRelocationList();
  RelocData::const_iterator rReloc = rRelocs.begin();
  ASSERT_EQ(2, rRelocs.size());
  ASSERT_TRUE(rRelocs.end()!=rReloc);
  ++rReloc; /// test rRelocs[1]
  ASSERT_EQ("puts", std::string(rReloc->symInfo()->name()));
  ASSERT_EQ(llvm::ELF::R_X86_64_PC32, rReloc->type());
  ASSERT_EQ(0x0, rReloc->symValue());
  ASSERT_EQ(-0x4, rReloc->addend());
}
Esempio n. 3
0
void TraceThreadListener::recordStreamWriteFromMemory(FILE *Stream,
                                                      MemoryArea Area)
{
  ProcessTime = getCIProcessTime();

  EventsOut.write<EventType::FileWriteFromMemory>
                 (ProcessTime,
                  reinterpret_cast<uintptr_t>(Stream),
                  Area.start(),
                  Area.length());
}
Esempio n. 4
0
bool MemoryManagerV3::uploadFunclet(FuncletCode::Type type)
{
	const FuncletCode& funclet = parent->getFunclet(type);
	const uint8_t* code = (uint8_t*)funclet.code();
	const size_t count = funclet.codeSize();

	vector<uint32_t> tmp(code, code + count); // copy funclet into vector
	MemoryArea* ram = this->getMemoryArea("system", 0);

	return ram && ram->write(0, &tmp[0], count) && ram->sync();
}
VOID FreeEnter(ADDRINT startAddress, THREADID threadid)
{
	GetLock(&memorySetLock, threadid);
	const MemoryArea area = findMemoryArea(startAddress);
	ADDRINT freeSize = area.size();
	memorySet.erase(area);
	ReleaseLock(&memorySetLock);

	if (freeSize == 0)
		return;

	freeMemoryAddress(startAddress, area.to, threadid);
}
Esempio n. 6
0
Module Process::inject(const Library& lib)
{
	if (isInjected(lib))
		BOOST_THROW_EXCEPTION(ex_injection() << e_text("library already in process") << e_library(lib.path()) << e_process(*this));

	// copy the pathname to the remote process
	SIZE_T libPathLen = (lib.path().wstring().size() + 1) * sizeof(wchar_t);
	MemoryArea libFileRemote = alloc(libPathLen, true, MEM_COMMIT, PAGE_READWRITE);
	libFileRemote.write((void*)(lib.path().c_str()));

	PTHREAD_START_ROUTINE loadLibraryW = (PTHREAD_START_ROUTINE)Module::kernel32().getProcAddress("LoadLibraryW");
	/*DWORD exitCode =*/ runInHiddenThread(loadLibraryW, libFileRemote.address());

	return isInjected(lib);
}
Esempio n. 7
0
void ELFObjectWriter::emitSectionHeader(const Module& pModule,
                                        const LinkerConfig& pConfig,
                                        MemoryArea& pOutput) const
{
  typedef typename ELFSizeTraits<SIZE>::Shdr ElfXX_Shdr;

  // emit section header
  unsigned int sectNum = pModule.size();
  unsigned int header_size = sizeof(ElfXX_Shdr) * sectNum;
  MemoryRegion* region = pOutput.request(getLastStartOffset<SIZE>(pModule),
                                         header_size);
  ElfXX_Shdr* shdr = (ElfXX_Shdr*)region->start();

  // Iterate the SectionTable in LDContext
  unsigned int sectIdx = 0;
  unsigned int shstridx = 0; // NULL section has empty name
  for (; sectIdx < sectNum; ++sectIdx) {
    const LDSection *ld_sect   = pModule.getSectionTable().at(sectIdx);
    shdr[sectIdx].sh_name      = shstridx;
    shdr[sectIdx].sh_type      = ld_sect->type();
    shdr[sectIdx].sh_flags     = ld_sect->flag();
    shdr[sectIdx].sh_addr      = ld_sect->addr();
    shdr[sectIdx].sh_offset    = ld_sect->offset();
    shdr[sectIdx].sh_size      = ld_sect->size();
    shdr[sectIdx].sh_addralign = ld_sect->align();
    shdr[sectIdx].sh_entsize   = getSectEntrySize<SIZE>(*ld_sect);
    shdr[sectIdx].sh_link      = getSectLink(*ld_sect, pConfig);
    shdr[sectIdx].sh_info      = getSectInfo(*ld_sect);

    // adjust strshidx
    shstridx += ld_sect->name().size() + 1;
  }
}
Esempio n. 8
0
bool MemoryManagerV3::flushAll()
{
	for (unsigned int i = 0; i < this->count(); ++i) 
	{
		MemoryArea* area = this->getMemoryArea(i);
		MemoryCacheCtrl* ctrl = area->getCacheCtrl();
		if (ctrl) 
		{
			if (!ctrl->flush(0, area->getSize()))
				return false;

			ctrl->clear(0, area->getSize());
		}
	}
	return true;
}
Esempio n. 9
0
void ELFObjectWriter::emitProgramHeader(MemoryArea& pOutput) const
{
  typedef typename ELFSizeTraits<SIZE>::Ehdr ElfXX_Ehdr;
  typedef typename ELFSizeTraits<SIZE>::Phdr ElfXX_Phdr;

  uint64_t start_offset, phdr_size;

  start_offset = sizeof(ElfXX_Ehdr);
  phdr_size = sizeof(ElfXX_Phdr);
  // Program header must start directly after ELF header
  MemoryRegion *region =
    pOutput.request(start_offset,
                    target().elfSegmentTable().size() * phdr_size);

  ElfXX_Phdr* phdr = (ElfXX_Phdr*)region->start();

  // Iterate the elf segment table in GNULDBackend
  size_t index = 0;
  ELFSegmentFactory::const_iterator seg = target().elfSegmentTable().begin(),
                                 segEnd = target().elfSegmentTable().end();
  for (; seg != segEnd; ++seg, ++index) {
    phdr[index].p_type   = (*seg)->type();
    phdr[index].p_flags  = (*seg)->flag();
    phdr[index].p_offset = (*seg)->offset();
    phdr[index].p_vaddr  = (*seg)->vaddr();
    phdr[index].p_paddr  = (*seg)->paddr();
    phdr[index].p_filesz = (*seg)->filesz();
    phdr[index].p_memsz  = (*seg)->memsz();
    phdr[index].p_align  = (*seg)->align();
  }
}
Esempio n. 10
0
void ELFObjectWriter::writeELFHeader(const LinkerConfig& pConfig,
                                     const Module& pModule,
                                     MemoryArea& pOutput) const
{
  typedef typename ELFSizeTraits<SIZE>::Ehdr ElfXX_Ehdr;
  typedef typename ELFSizeTraits<SIZE>::Shdr ElfXX_Shdr;
  typedef typename ELFSizeTraits<SIZE>::Phdr ElfXX_Phdr;

  // ELF header must start from 0x0
  MemoryRegion *region = pOutput.request(0, sizeof(ElfXX_Ehdr));
  ElfXX_Ehdr* header = (ElfXX_Ehdr*)region->start();

  memcpy(header->e_ident, ElfMagic, EI_MAG3+1);

  header->e_ident[EI_CLASS]      = (SIZE == 32) ? ELFCLASS32 : ELFCLASS64;
  header->e_ident[EI_DATA]       = pConfig.targets().isLittleEndian()?
                                       ELFDATA2LSB : ELFDATA2MSB;
  header->e_ident[EI_VERSION]    = target().getInfo().ELFVersion();
  header->e_ident[EI_OSABI]      = target().getInfo().OSABI();
  header->e_ident[EI_ABIVERSION] = target().getInfo().ABIVersion();

  // FIXME: add processor-specific and core file types.
  switch(pConfig.codeGenType()) {
    case LinkerConfig::Object:
      header->e_type = ET_REL;
      break;
    case LinkerConfig::DynObj:
      header->e_type = ET_DYN;
      break;
    case LinkerConfig::Exec:
      header->e_type = ET_EXEC;
      break;
    default:
      llvm::errs() << "unspported output file type: " << pConfig.codeGenType() << ".\n";
      header->e_type = ET_NONE;
  }
  header->e_machine   = target().getInfo().machine();
  header->e_version   = header->e_ident[EI_VERSION];
  header->e_entry     = getEntryPoint(pConfig, pModule);

  if (LinkerConfig::Object != pConfig.codeGenType())
    header->e_phoff   = sizeof(ElfXX_Ehdr);
  else
    header->e_phoff   = 0x0;

  header->e_shoff     = getLastStartOffset<SIZE>(pModule);
  header->e_flags     = target().getInfo().flags();
  header->e_ehsize    = sizeof(ElfXX_Ehdr);
  header->e_phentsize = sizeof(ElfXX_Phdr);
  header->e_phnum     = target().elfSegmentTable().size();
  header->e_shentsize = sizeof(ElfXX_Shdr);
  header->e_shnum     = pModule.size();
  header->e_shstrndx  = pModule.getSection(".shstrtab")->index();
}
Esempio n. 11
0
/// emitShStrTab - emit section string table
void
ELFObjectWriter::emitShStrTab(const LDSection& pShStrTab,
                              const Module& pModule,
                              MemoryArea& pOutput)
{
  // write out data
  MemoryRegion* region = pOutput.request(pShStrTab.offset(), pShStrTab.size());
  unsigned char* data = region->start();
  size_t shstrsize = 0;
  Module::const_iterator section, sectEnd = pModule.end();
  for (section = pModule.begin(); section != sectEnd; ++section) {
    strcpy((char*)(data + shstrsize), (*section)->name().data());
    shstrsize += (*section)->name().size() + 1;
  }
}
bool ELFObjectReader::readRelocations(Input& pInput)
{
  assert(pInput.hasMemArea());

  MemoryArea* mem = pInput.memArea();
  LDContext::sect_iterator rs, rsEnd = pInput.context()->relocSectEnd();
  for (rs = pInput.context()->relocSectBegin(); rs != rsEnd; ++rs) {
    if (LDFileFormat::Ignore == (*rs)->kind())
      continue;

    uint32_t offset = pInput.fileOffset() + (*rs)->offset();
    uint32_t size = (*rs)->size();
    llvm::StringRef region = mem->request(offset, size);
    IRBuilder::CreateRelocData(**rs); ///< create relocation data for the header
    switch ((*rs)->type()) {
      case llvm::ELF::SHT_RELA: {
        if (!m_pELFReader->readRela(pInput, **rs, region)) {
          return false;
        }
        break;
      }
      case llvm::ELF::SHT_REL: {
        if (!m_pELFReader->readRel(pInput, **rs, region)) {
          return false;
        }
        break;
      }
      default: { ///< should not enter
        return false;
      }
    } // end of switch

  } // end of for all relocation data

  return true;
}
Esempio n. 13
0
void Process::remoteDllMainCall(LPVOID lpModuleEntry, HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
	struct DLLMAINCALL dllMainCall = { (DLLMAIN)lpModuleEntry, hModule, ul_reason_for_call, lpReserved };
	SIZE_T DllMainWrapperSize = (SIZE_T)DllMainWrapper_end - (SIZE_T)DllMainWrapper; 

	MemoryArea param          = alloc(sizeof(struct DLLMAINCALL));
	MemoryArea dllCallWrapper = alloc((SIZE_T)((DWORD_PTR)DllMainWrapper_end - (DWORD_PTR)DllMainWrapper));

	param.write((LPCVOID)&dllMainCall, sizeof(struct DLLMAINCALL));
	dllCallWrapper.write((LPCVOID)DllMainWrapper, DllMainWrapperSize);

	runInHiddenThread((LPTHREAD_START_ROUTINE)dllCallWrapper.address(), param.address());
}
Esempio n. 14
0
bool MemoryManagerV3::erase ()
{
	MemoryArea* main = this->getMemoryArea("main");
	MemoryArea* info = this->getMemoryArea("information");
	MemoryArea* bsl = this->getMemoryArea("boot");

	if (main && !main->erase())
		return false;

	if (info && !info->erase())
		return false;
	
	if (bsl && !bsl->erase())
		return false;

	return true;
}
Esempio n. 15
0
void EhFrameHdr::emitOutput<32>(MemoryArea& pOutput)
{
    MemoryRegion* ehframehdr_region =
        pOutput.request(m_EhFrameHdr.offset(), m_EhFrameHdr.size());

    MemoryRegion* ehframe_region =
        pOutput.request(m_EhFrame.offset(),
                        m_EhFrame.size());

    uint8_t* data = (uint8_t*)ehframehdr_region->start();
    // version
    data[0] = 1;
    // eh_frame_ptr_enc
    data[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4;

    // eh_frame_ptr
    uint32_t* eh_frame_ptr = (uint32_t*)(data + 4);
    *eh_frame_ptr = m_EhFrame.addr() - (m_EhFrameHdr.addr() + 4);

    // fde_count
    uint32_t* fde_count = (uint32_t*)(data + 8);
    if (m_EhFrame.hasEhFrame())
        *fde_count = 0;
    else
        *fde_count = m_EhFrame.getEhFrame()->numOfFDEs();

    if (0 != *fde_count) {
        // fde_count_enc
        data[2] = DW_EH_PE_udata4;
        // table_enc
        data[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4;

    }
    else {
        // fde_count_enc
        data[2] = DW_EH_PE_omit;
        // table_enc
        data[3] = DW_EH_PE_omit;
    }

    if (0 != *fde_count) {

        // prepare the binary search table
        typedef std::vector<bit32::Entry> SearchTableType;
        SearchTableType search_table;
        MemoryRegion* ehframe_region =
            pOutput.request(m_EhFrame.offset(), m_EhFrame.size());
        EhFrame::const_fde_iterator fde, fde_end = m_EhFrame.getEhFrame()->fde_end();
        for(fde = m_EhFrame.getEhFrame()->fde_begin(); fde != fde_end; ++fde) {
            assert(*fde != NULL);
            SizeTraits<32>::Offset offset;
            SizeTraits<32>::Address fde_pc;
            SizeTraits<32>::Address fde_addr;
            offset = (*fde)->getOffset();
            fde_pc = computePCBegin(**fde, *ehframe_region);
            fde_addr = m_EhFrame.addr() + offset;
            search_table.push_back(std::make_pair(fde_pc, fde_addr));
        }
        pOutput.release(ehframe_region);

        std::sort(search_table.begin(), search_table.end(), bit32::EntryCompare);

        // write out the binary search table
        uint32_t* bst = (uint32_t*)(data + 12);
        SearchTableType::const_iterator entry, entry_end = search_table.end();
        size_t id = 0;
        for (entry = search_table.begin(); entry != entry_end; ++entry) {
            bst[id++] = (*entry).first - m_EhFrameHdr.addr();
            bst[id++] = (*entry).second - m_EhFrameHdr.addr();
        }
    }
    pOutput.release(ehframehdr_region);
    pOutput.release(ehframe_region);
}
Esempio n. 16
0
void ELFObjectWriter::writeSection(MemoryArea& pOutput, LDSection *section)
{
  MemoryRegion* region;
  // Request output region
  switch (section->kind()) {
  case LDFileFormat::Note:
    if (section->getSectionData() == NULL)
      return;
    // Fall through
  case LDFileFormat::Regular:
  case LDFileFormat::Relocation:
  case LDFileFormat::Target:
  case LDFileFormat::Debug:
  case LDFileFormat::GCCExceptTable:
  case LDFileFormat::EhFrame: {
    region = pOutput.request(section->offset(), section->size());
    if (NULL == region) {
      llvm::report_fatal_error(llvm::Twine("cannot get enough memory region for output section `") +
                               llvm::Twine(section->name()) +
                               llvm::Twine("'.\n"));
    }
    break;
  }
  case LDFileFormat::Null:
  case LDFileFormat::NamePool:
  case LDFileFormat::BSS:
  case LDFileFormat::MetaData:
  case LDFileFormat::Version:
  case LDFileFormat::EhFrameHdr:
  case LDFileFormat::StackNote:
    // Ignore these sections
    return;
  default:
    llvm::errs() << "WARNING: unsupported section kind: "
                 << section->kind()
                 << " of section "
                 << section->name()
                 << ".\n";
    return;
  }

  // Write out sections with data
  switch(section->kind()) {
  case LDFileFormat::GCCExceptTable:
  case LDFileFormat::EhFrame:
  case LDFileFormat::Regular:
  case LDFileFormat::Debug:
  case LDFileFormat::Note:
    // FIXME: if optimization of exception handling sections is enabled,
    // then we should emit these sections by the other way.
    emitSectionData(*section, *region);
    break;
  case LDFileFormat::Relocation:
    // sort relocation for the benefit of the dynamic linker.
    target().sortRelocation(*section);

    emitRelocation(m_Config, *section, *region);
    break;
  case LDFileFormat::Target:
    target().emitSectionData(*section, *region);
    break;
  default:
    llvm_unreachable("invalid section kind");
  }
}
bool DebugManagerV3::run (uint16_t controlMask, DebugEventTarget * cb, bool releaseJtag)
{
	MemoryManager* mm = this->parent->getMemoryManager();
	MemoryArea* cpu = mm->getMemoryArea("CPU"); 
	if (!cpu)
    {
		return false;
    }

	lpm5WakeupDetected = false;

	if(cb!=0)
	{
		cbx=cb;	
	}

	uint32_t pc, sr;
	cpu->read(0, &pc, 1);
	cpu->read(2, &sr, 1);

	if(mm->flushAll()==false)
	{
		return false;
	}
	
	cycleCounter_.reset();
	ConfigManager *cm = parent->getFetHandle()->getConfigManager();

	const uint16_t mdb = parent->getEmulationManager()->getSoftwareBreakpoints()->getSwbpManager()->getInstructionAt(pc);
	if (mdb != 0)
	{
		mdbPatchValue = mdb;
	}

	HalExecElement* el = new HalExecElement(this->parent->checkHalId(ID_RestoreContext_ReleaseJtag));
	this->parent->getWatchdogControl()->addParamsTo(el);
	el->appendInputData32(pc);
	el->appendInputData16(sr);
	el->appendInputData16(controlMask!=0? 0x0007: 0x0006);	// eem control bits 
	el->appendInputData16(mdbPatchValue);		// mdb
	el->appendInputData16(releaseJtag ? 1 : 0);
	el->appendInputData16(cm->ulpDebugEnabled() ? 1 : 0);

	mdbPatchValue = 0;

	HalExecCommand cmd;
	cmd.elements.push_back(el);

	if (!this->parent->send(cmd))
	{
		return false;
	}
	
	// handle lpmx5 polling
	if (releaseJtag)
	{
		pausePolling();
	}
	else
	{
		this->resumePolling();
	}	

	if (controlMask!=0 && !releaseJtag)
	{
		if (!activatePolling(controlMask))
		{
			return false;
		}
	}

	resetCycleCounterBeforeNextStep = true;

	return true;
}
Esempio n. 18
0
void TraceData::parseReport(const std::string& filename)
{
	release();

	std::ifstream file;
	std::istream& in = filename.empty() ? std::cin : file;
	if (!filename.empty()) {
		file.open(filename.c_str(), std::ios_base::in);
		if (file.fail()) {
			throw std::ios_base::failure(Formatter() << "Failed to open file: " << filename);
		}
	}
	char buffer[4096];

	in.getline(buffer, sizeof(buffer));
	if (in.eof()) throw std::runtime_error(Formatter() << "Empty input file: " << filename);

	if (buffer[0] == (char)SP_RTRACE_PROTO_HS_ID) {
		throw std::runtime_error("Can't process sp-rtrace binary files. "
				                 "Convert to text format with sp-rtrace-postproc and try again.");
	}

	sp_rtrace_parser_parse_header(buffer, &header);

	while (true && (filename_maps.empty() || filename_pageflags.empty()) ) {
		in.getline(buffer, sizeof(buffer));
		if (in.eof()) break;

		sp_rtrace_record_t rec;
		int rec_type = sp_rtrace_parser_parse_record(buffer, &rec);
		switch (rec_type) {
			case SP_RTRACE_RECORD_ATTACHMENT:
				if (!strcmp(rec.attachment.name, ATTACHMENT_MAPS)) filename_maps = rec.attachment.path;
				else if (!strcmp(rec.attachment.name, ATTACHMENT_PAGEFLAGS)) filename_pageflags = rec.attachment.path;
				break;
		}
		sp_rtrace_parser_free_record(rec_type, &rec);
	}

	if (access(filename_maps.c_str(), F_OK) == -1) throw std::runtime_error(Formatter() << "Failed to access maps file '" << filename_maps << "': " << strerror(errno));
	if (access(filename_pageflags.c_str(), F_OK) == -1) throw std::runtime_error(Formatter() << "Failed to access pageflags file '" << filename_pageflags << "': " << strerror(errno));

	// map the pageflags file to memory
	pageflags_fd = open(filename_pageflags.c_str(), O_RDONLY);
	if (pageflags_fd == -1) throw std::runtime_error(Formatter() << "Failed to open pageflags file '" << filename_pageflags << "': " << strerror(errno));

	struct stat ps;
	if (fstat(pageflags_fd, &ps) ==  -1) throw std::runtime_error(Formatter() << "Failed to stat pageflags file '" << filename_pageflags << "': " << strerror(errno));
	pageflags_size = ps.st_size;

	pageflags_addr = mmap(NULL, pageflags_size, PROT_READ, MAP_SHARED, pageflags_fd, 0);
	if (pageflags_addr == MAP_FAILED) throw std::runtime_error(Formatter() << "Failed to mmap pageflags file '" << filename_pageflags << "': " << strerror(errno));

	// scan the mapped areas from maps/pageflags files
	scanMemoryAreas();

	// scan the allocation events
	in.seekg(0);
	std::list<CallEvent*> last_events;

	sp_rtrace_btframe_t frames[512];
	sp_rtrace_btframe_t* pframe = frames;

	while (true) {
		in.getline(buffer, sizeof(buffer));
		if (in.eof()) break;

		sp_rtrace_record_t rec;
		int rec_type = sp_rtrace_parser_parse_record(buffer, &rec);
		if (rec_type == SP_RTRACE_RECORD_TRACE) {
			/* don't collect backtrace records if there is no preceeding allocation
			 * function event */
			if (last_events.empty()) {
				sp_rtrace_parser_free_record(rec_type, &rec);
			}
			else {
				*pframe++ = rec.frame;
			}
			continue;
		}
		// Store backtrace record for last cached events
		if ((pframe > frames || !*buffer) && !last_events.empty()) {
			storeTrace(last_events, frames, pframe - frames);
			last_events.clear();
		}

		if (rec_type == SP_RTRACE_RECORD_CALL) {
			if (rec.call.type == SP_RTRACE_FTYPE_ALLOC) {
				MemoryArea* area = findMemoryArea(rec.call.res_id);
				if (area) {
					last_events.push_back(area->addEvent(rec.call));
					continue;
				}
			}
		}
		pframe = frames;
		sp_rtrace_parser_free_record(rec_type, &rec);
	}
	if ((pframe > frames || !*buffer) && !last_events.empty()) {
		fprintf(stderr, "last parse\n");
		storeTrace(last_events, frames, pframe - frames);
		last_events.clear();
	}

	// sort the allocation events inside areas
	for (MemoryArea::vector_t::iterator iter = memory_areas.begin(); iter != memory_areas.end(); iter++) {
		iter->get()->sortEvents();
	}
}
bool FramMemoryAccessBaseFR57<MPU>::erase(uint32_t start, uint32_t end, uint32_t block_size, int type)
{
	using boost::shared_ptr;
	using boost::bind;

	// check if valid erase type is used
	if ((type != ERASE_SEGMENT) && (type != ERASE_MAIN))
	{
		return false;
	}
	if (block_size < 1)
	{
		return false;
	}
	// if the  MPU is enabled, disable it to enable memory erase
	if(!mpu.readMpuSettings() || !mpu.disableMpu())
	{
		return false;
	}

	// get Device RAM parameters for funclet upload
	MemoryArea* ram = mm->getMemoryArea("system", 0);
	if (!ram)
	{
		return false;
	}

	if ( !uploadFunclet(FuncletCode::ERASE) )
	{
		return false;
	}
	shared_ptr<void> restoreRamOnExit(static_cast<void*>(0), 
									  bind(&FramMemoryAccessBaseFR57<MPU>::restoreRam, this));

	//Note the erase on an FRAM device is just a dummy write with 0xFFFF to the device FRAm 
	int32_t erase_address = start;

	const FuncletCode& funclet = devHandle->getFunclet(FuncletCode::ERASE);
	
	const uint32_t eraseType = 0;
	const uint32_t eraseLength = end - start + 1;
	const uint16_t flags = 0x0;
	const uint16_t programStartAddress = ram->getStart() + funclet.programStartOffset();

	HalExecCommand cmd;
	cmd.setTimeout(10000);	// overwrite 3 sec default with 10 sec
	HalExecElement* el = new HalExecElement(ID_SetDeviceChainInfo);
	el->appendInputData16(static_cast<uint16_t>(this->devHandle->getDevChainInfo()->getBusId()));
	cmd.elements.push_back(el);

	el = new HalExecElement(this->devHandle->checkHalId(ID_ExecuteFunclet));
	el->appendInputData16(static_cast<uint16_t>(ram->getStart() & 0xFFFF));
	el->appendInputData16(static_cast<uint16_t>(ram->getSize() & 0xFFFF));
	el->appendInputData16(programStartAddress);
	el->appendInputData32(static_cast<uint32_t>(erase_address));
	el->appendInputData32(eraseLength);
	el->appendInputData16(eraseType);
	el->appendInputData16(flags);
	el->appendInputData16(devHandle->getClockCalibration()->getCal0());
	el->appendInputData16(devHandle->getClockCalibration()->getCal1());

	//Dummy data to trigger execution of erase funclet
	el->appendInputData32(0xDEADBEEF);

	// set value for return length
	el->setOutputSize(2);
	cmd.elements.push_back(el);

	if (!this->devHandle->send(cmd))
	{
		return false;
	}
	return true;
}
Esempio n. 20
0
llvm::error_code ELFObjectWriter::writeObject(Module& pModule,
                                              MemoryArea& pOutput)
{
  bool is_dynobj = m_Config.codeGenType() == LinkerConfig::DynObj;
  bool is_exec = m_Config.codeGenType() == LinkerConfig::Exec;
  bool is_binary = m_Config.codeGenType() == LinkerConfig::Binary;
  bool is_object = m_Config.codeGenType() == LinkerConfig::Object;

  assert(is_dynobj || is_exec || is_binary || is_object);

  if (is_dynobj || is_exec) {
    // Allow backend to sort symbols before emitting
    target().orderSymbolTable(pModule);

    // Write out the interpreter section: .interp
    target().emitInterp(pOutput);

    // Write out name pool sections: .dynsym, .dynstr, .hash
    target().emitDynNamePools(pModule, pOutput);
  }

  if (is_object || is_dynobj || is_exec) {
    // Write out name pool sections: .symtab, .strtab
    target().emitRegNamePools(pModule, pOutput);
  }

  if (is_binary) {
    // Iterate over the loadable segments and write the corresponding sections
    ELFSegmentFactory::iterator seg, segEnd = target().elfSegmentTable().end();

    for (seg = target().elfSegmentTable().begin(); seg != segEnd; ++seg) {
      if (llvm::ELF::PT_LOAD == (*seg)->type()) {
        ELFSegment::iterator sect, sectEnd = (*seg)->end();
        for (sect = (*seg)->begin(); sect != sectEnd; ++sect)
          writeSection(pOutput, *sect);
      }
    }
  } else {
    // Write out regular ELF sections
    Module::iterator sect, sectEnd = pModule.end();
    for (sect = pModule.begin(); sect != sectEnd; ++sect)
      writeSection(pOutput, *sect);

    emitShStrTab(target().getOutputFormat()->getShStrTab(), pModule, pOutput);

    if (m_Config.targets().is32Bits()) {
      // Write out ELF header
      // Write out section header table
      writeELFHeader<32>(m_Config, pModule, pOutput);
      if (is_dynobj || is_exec)
        emitProgramHeader<32>(pOutput);

      emitSectionHeader<32>(pModule, m_Config, pOutput);
    }
    else if (m_Config.targets().is64Bits()) {
      // Write out ELF header
      // Write out section header table
      writeELFHeader<64>(m_Config, pModule, pOutput);
      if (is_dynobj || is_exec)
        emitProgramHeader<64>(pOutput);

      emitSectionHeader<64>(pModule, m_Config, pOutput);
    }
    else
      return make_error_code(errc::not_supported);
  }

  pOutput.clear();
  return llvm::make_error_code(llvm::errc::success);
}