Example #1
0
Status ObjectFile::LoadInMemory(Target &target, bool set_pc) {
  Status error;
  ProcessSP process = target.CalculateProcess();
  if (!process)
    return Status("No Process");
  if (set_pc && !GetEntryPointAddress().IsValid())
    return Status("No entry address in object file");

  SectionList *section_list = GetSectionList();
  if (!section_list)
    return Status("No section in object file");
  size_t section_count = section_list->GetNumSections(0);
  for (size_t i = 0; i < section_count; ++i) {
    SectionSP section_sp = section_list->GetSectionAtIndex(i);
    addr_t addr = target.GetSectionLoadList().GetSectionLoadAddress(section_sp);
    if (addr != LLDB_INVALID_ADDRESS) {
      DataExtractor section_data;
      // We can skip sections like bss
      if (section_sp->GetFileSize() == 0)
        continue;
      section_sp->GetSectionData(section_data);
      lldb::offset_t written = process->WriteMemory(
          addr, section_data.GetDataStart(), section_data.GetByteSize(), error);
      if (written != section_data.GetByteSize())
        return error;
    }
  }
  if (set_pc) {
    ThreadList &thread_list = process->GetThreadList();
    ThreadSP curr_thread(thread_list.GetSelectedThread());
    RegisterContextSP reg_context(curr_thread->GetRegisterContext());
    Address file_entry = GetEntryPointAddress();
    reg_context->SetPC(file_entry.GetLoadAddress(&target));
  }
  return error;
}