1 //===- ELFDynObjReader.cpp ------------------------------------------------===// 2 // 3 // The MCLinker Project 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 #include <mcld/LD/ELFDynObjReader.h> 10 11 #include <mcld/LinkerConfig.h> 12 #include <mcld/IRBuilder.h> 13 #include <mcld/LD/ELFReader.h> 14 #include <mcld/MC/MCLDInput.h> 15 #include <mcld/Support/MemoryRegion.h> 16 #include <mcld/Target/GNULDBackend.h> 17 18 #include <llvm/ADT/Twine.h> 19 #include <llvm/ADT/OwningPtr.h> 20 #include <llvm/Support/ErrorHandling.h> 21 22 #include <string> 23 24 using namespace mcld; 25 26 //===----------------------------------------------------------------------===// 27 // ELFDynObjReader 28 //===----------------------------------------------------------------------===// 29 ELFDynObjReader::ELFDynObjReader(GNULDBackend& pBackend, 30 IRBuilder& pBuilder, 31 const LinkerConfig& pConfig) 32 : DynObjReader(), 33 m_pELFReader(0), 34 m_Builder(pBuilder) { 35 if (pConfig.targets().is32Bits() && pConfig.targets().isLittleEndian()) 36 m_pELFReader = new ELFReader<32, true>(pBackend); 37 else if (pConfig.targets().is64Bits() && pConfig.targets().isLittleEndian()) 38 m_pELFReader = new ELFReader<64, true>(pBackend); 39 } 40 41 ELFDynObjReader::~ELFDynObjReader() 42 { 43 delete m_pELFReader; 44 } 45 46 /// isMyFormat 47 bool ELFDynObjReader::isMyFormat(Input &pInput) const 48 { 49 assert(pInput.hasMemArea()); 50 51 // Don't warning about the frequently requests. 52 // MemoryArea has a list of cache to handle this. 53 size_t hdr_size = m_pELFReader->getELFHeaderSize(); 54 MemoryRegion* region = pInput.memArea()->request(pInput.fileOffset(), 55 hdr_size); 56 57 uint8_t* ELF_hdr = region->start(); 58 bool result = true; 59 if (!m_pELFReader->isELF(ELF_hdr)) 60 result = false; 61 else if (!m_pELFReader->isMyEndian(ELF_hdr)) 62 result = false; 63 else if (!m_pELFReader->isMyMachine(ELF_hdr)) 64 result = false; 65 else if (Input::DynObj != m_pELFReader->fileType(ELF_hdr)) 66 result = false; 67 pInput.memArea()->release(region); 68 return result; 69 } 70 71 /// readHeader 72 bool ELFDynObjReader::readHeader(Input& pInput) 73 { 74 assert(pInput.hasMemArea()); 75 76 size_t hdr_size = m_pELFReader->getELFHeaderSize(); 77 MemoryRegion* region = pInput.memArea()->request(pInput.fileOffset(), 78 hdr_size); 79 uint8_t* ELF_hdr = region->start(); 80 81 bool shdr_result = m_pELFReader->readSectionHeaders(pInput, ELF_hdr); 82 pInput.memArea()->release(region); 83 84 // read .dynamic to get the correct SONAME 85 bool dyn_result = m_pELFReader->readDynamic(pInput); 86 87 return (shdr_result && dyn_result); 88 } 89 90 /// readSymbols 91 bool ELFDynObjReader::readSymbols(Input& pInput) 92 { 93 assert(pInput.hasMemArea()); 94 95 LDSection* symtab_shdr = pInput.context()->getSection(".dynsym"); 96 if (NULL == symtab_shdr) { 97 note(diag::note_has_no_symtab) << pInput.name() 98 << pInput.path() 99 << ".dynsym"; 100 return true; 101 } 102 103 LDSection* strtab_shdr = symtab_shdr->getLink(); 104 if (NULL == strtab_shdr) { 105 fatal(diag::fatal_cannot_read_strtab) << pInput.name() 106 << pInput.path() 107 << ".dynsym"; 108 return false; 109 } 110 111 MemoryRegion* symtab_region = pInput.memArea()->request( 112 pInput.fileOffset() + symtab_shdr->offset(), symtab_shdr->size()); 113 114 MemoryRegion* strtab_region = pInput.memArea()->request( 115 pInput.fileOffset() + strtab_shdr->offset(), strtab_shdr->size()); 116 char* strtab = reinterpret_cast<char*>(strtab_region->start()); 117 bool result = m_pELFReader->readSymbols(pInput, m_Builder, 118 *symtab_region, strtab); 119 pInput.memArea()->release(symtab_region); 120 pInput.memArea()->release(strtab_region); 121 122 return result; 123 } 124 125