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(); } }
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; } }
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()); }
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(); }
/// 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; }
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"); } }
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); }