1 //===- ELFObjectReader.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/ELFObjectReader.h> 10 11 #include <string> 12 #include <cassert> 13 14 #include <llvm/Support/ELF.h> 15 #include <llvm/ADT/Twine.h> 16 17 #include <mcld/IRBuilder.h> 18 #include <mcld/MC/MCLDInput.h> 19 #include <mcld/LD/ELFReader.h> 20 #include <mcld/LD/EhFrameReader.h> 21 #include <mcld/LD/EhFrame.h> 22 #include <mcld/Target/GNULDBackend.h> 23 #include <mcld/Support/MsgHandling.h> 24 #include <mcld/Object/ObjectBuilder.h> 25 26 using namespace mcld; 27 28 //===----------------------------------------------------------------------===// 29 // ELFObjectReader 30 //===----------------------------------------------------------------------===// 31 /// constructor 32 ELFObjectReader::ELFObjectReader(GNULDBackend& pBackend, 33 IRBuilder& pBuilder, 34 const LinkerConfig& pConfig) 35 : ObjectReader(), 36 m_pELFReader(NULL), 37 m_pEhFrameReader(NULL), 38 m_Builder(pBuilder), 39 m_ReadFlag(ParseEhFrame), 40 m_Backend(pBackend), 41 m_Config(pConfig) { 42 if (pConfig.targets().is32Bits() && pConfig.targets().isLittleEndian()) { 43 m_pELFReader = new ELFReader<32, true>(pBackend); 44 } 45 else if (pConfig.targets().is64Bits() && pConfig.targets().isLittleEndian()) { 46 m_pELFReader = new ELFReader<64, true>(pBackend); 47 } 48 49 m_pEhFrameReader = new EhFrameReader(); 50 } 51 52 /// destructor 53 ELFObjectReader::~ELFObjectReader() 54 { 55 delete m_pELFReader; 56 delete m_pEhFrameReader; 57 } 58 59 /// isMyFormat 60 bool ELFObjectReader::isMyFormat(Input &pInput) const 61 { 62 assert(pInput.hasMemArea()); 63 64 // Don't warning about the frequently requests. 65 // MemoryArea has a list of cache to handle this. 66 size_t hdr_size = m_pELFReader->getELFHeaderSize(); 67 MemoryRegion* region = pInput.memArea()->request(pInput.fileOffset(), 68 hdr_size); 69 70 uint8_t* ELF_hdr = region->start(); 71 bool result = true; 72 if (!m_pELFReader->isELF(ELF_hdr)) 73 result = false; 74 else if (!m_pELFReader->isMyEndian(ELF_hdr)) 75 result = false; 76 else if (!m_pELFReader->isMyMachine(ELF_hdr)) 77 result = false; 78 else if (Input::Object != m_pELFReader->fileType(ELF_hdr)) 79 result = false; 80 pInput.memArea()->release(region); 81 return result; 82 } 83 84 /// readHeader - read section header and create LDSections. 85 bool ELFObjectReader::readHeader(Input& pInput) 86 { 87 assert(pInput.hasMemArea()); 88 89 size_t hdr_size = m_pELFReader->getELFHeaderSize(); 90 MemoryRegion* region = pInput.memArea()->request(pInput.fileOffset(), 91 hdr_size); 92 uint8_t* ELF_hdr = region->start(); 93 bool result = m_pELFReader->readSectionHeaders(pInput, ELF_hdr); 94 pInput.memArea()->release(region); 95 return result; 96 } 97 98 /// readSections - read all regular sections. 99 bool ELFObjectReader::readSections(Input& pInput) 100 { 101 // handle sections 102 LDContext::sect_iterator section, sectEnd = pInput.context()->sectEnd(); 103 for (section = pInput.context()->sectBegin(); section != sectEnd; ++section) { 104 // ignore the section if the LDSection* in input context is NULL 105 if (NULL == *section) 106 continue; 107 108 switch((*section)->kind()) { 109 /** group sections **/ 110 case LDFileFormat::Group: { 111 assert(NULL != (*section)->getLink()); 112 ResolveInfo* signature = 113 m_pELFReader->readSignature(pInput, 114 *(*section)->getLink(), 115 (*section)->getInfo()); 116 117 bool exist = false; 118 if (0 == signature->nameSize() && 119 ResolveInfo::Section == signature->type()) { 120 // if the signature is a section symbol in input object, we use the 121 // section name as group signature. 122 signatures().insert((*section)->name(), exist); 123 } else { 124 signatures().insert(signature->name(), exist); 125 } 126 127 if (exist) { 128 // if this is not the first time we see this group signature, then 129 // ignore all the members in this group (set Ignore) 130 MemoryRegion* region = pInput.memArea()->request( 131 pInput.fileOffset() + (*section)->offset(), (*section)->size()); 132 llvm::ELF::Elf32_Word* value = 133 reinterpret_cast<llvm::ELF::Elf32_Word*>(region->start()); 134 135 size_t size = region->size() / sizeof(llvm::ELF::Elf32_Word); 136 if (llvm::ELF::GRP_COMDAT == *value) { 137 for (size_t index = 1; index < size; ++index) { 138 pInput.context()->getSection(value[index])->setKind(LDFileFormat::Ignore); 139 } 140 } 141 pInput.memArea()->release(region); 142 } 143 ResolveInfo::Destroy(signature); 144 break; 145 } 146 /** relocation sections **/ 147 case LDFileFormat::Relocation: { 148 assert(NULL != (*section)->getLink()); 149 size_t link_index = (*section)->getLink()->index(); 150 LDSection* link_sect = pInput.context()->getSection(link_index); 151 if (NULL == link_sect || LDFileFormat::Ignore == link_sect->kind()) { 152 // Relocation sections of group members should also be part of the 153 // group. Thus, if the associated member sections are ignored, the 154 // related relocations should be also ignored. 155 (*section)->setKind(LDFileFormat::Ignore); 156 } 157 break; 158 } 159 /** normal sections **/ 160 // FIXME: support Version Kind 161 case LDFileFormat::Version: 162 // FIXME: support GCCExceptTable Kind 163 case LDFileFormat::GCCExceptTable: 164 /** Fall through **/ 165 case LDFileFormat::Regular: 166 case LDFileFormat::Note: 167 case LDFileFormat::MetaData: { 168 SectionData* sd = IRBuilder::CreateSectionData(**section); 169 if (!m_pELFReader->readRegularSection(pInput, *sd)) 170 fatal(diag::err_cannot_read_section) << (*section)->name(); 171 break; 172 } 173 case LDFileFormat::Debug: { 174 if (m_Config.options().stripDebug()) { 175 (*section)->setKind(LDFileFormat::Ignore); 176 } 177 else { 178 SectionData* sd = IRBuilder::CreateSectionData(**section); 179 if (!m_pELFReader->readRegularSection(pInput, *sd)) { 180 fatal(diag::err_cannot_read_section) << (*section)->name(); 181 } 182 } 183 break; 184 } 185 case LDFileFormat::EhFrame: { 186 EhFrame* eh_frame = IRBuilder::CreateEhFrame(**section); 187 188 if (m_Config.options().hasEhFrameHdr() && 189 (m_ReadFlag & ParseEhFrame)) { 190 191 // if --eh-frame-hdr option is given, parse .eh_frame. 192 if (!m_pEhFrameReader->read<32, true>(pInput, *eh_frame)) { 193 // if we failed to parse a .eh_frame, we should not parse the rest 194 // .eh_frame. 195 m_ReadFlag ^= ParseEhFrame; 196 } 197 } 198 else { 199 if (!m_pELFReader->readRegularSection(pInput, 200 eh_frame->getSectionData())) { 201 fatal(diag::err_cannot_read_section) << (*section)->name(); 202 } 203 } 204 break; 205 } 206 /** target dependent sections **/ 207 case LDFileFormat::Target: { 208 SectionData* sd = IRBuilder::CreateSectionData(**section); 209 if (!m_Backend.readSection(pInput, *sd)) { 210 fatal(diag::err_cannot_read_target_section) << (*section)->name(); 211 } 212 break; 213 } 214 /** BSS sections **/ 215 case LDFileFormat::BSS: { 216 IRBuilder::CreateBSS(**section); 217 break; 218 } 219 // ignore 220 case LDFileFormat::Null: 221 case LDFileFormat::NamePool: 222 case LDFileFormat::Ignore: 223 case LDFileFormat::StackNote: 224 continue; 225 // warning 226 case LDFileFormat::EhFrameHdr: 227 default: { 228 warning(diag::warn_illegal_input_section) << (*section)->name() 229 << pInput.name() 230 << pInput.path(); 231 break; 232 } 233 } 234 } // end of for all sections 235 236 return true; 237 } 238 239 /// readSymbols - read symbols from the input relocatable object. 240 bool ELFObjectReader::readSymbols(Input& pInput) 241 { 242 assert(pInput.hasMemArea()); 243 244 LDSection* symtab_shdr = pInput.context()->getSection(".symtab"); 245 if (NULL == symtab_shdr) { 246 note(diag::note_has_no_symtab) << pInput.name() 247 << pInput.path() 248 << ".symtab"; 249 return true; 250 } 251 252 LDSection* strtab_shdr = symtab_shdr->getLink(); 253 if (NULL == strtab_shdr) { 254 fatal(diag::fatal_cannot_read_strtab) << pInput.name() 255 << pInput.path() 256 << ".symtab"; 257 return false; 258 } 259 260 MemoryRegion* symtab_region = pInput.memArea()->request( 261 pInput.fileOffset() + symtab_shdr->offset(), symtab_shdr->size()); 262 MemoryRegion* strtab_region = pInput.memArea()->request( 263 pInput.fileOffset() + strtab_shdr->offset(), strtab_shdr->size()); 264 char* strtab = reinterpret_cast<char*>(strtab_region->start()); 265 bool result = m_pELFReader->readSymbols(pInput, 266 m_Builder, 267 *symtab_region, 268 strtab); 269 pInput.memArea()->release(symtab_region); 270 pInput.memArea()->release(strtab_region); 271 return result; 272 } 273 274 bool ELFObjectReader::readRelocations(Input& pInput) 275 { 276 assert(pInput.hasMemArea()); 277 278 MemoryArea* mem = pInput.memArea(); 279 LDContext::sect_iterator rs, rsEnd = pInput.context()->relocSectEnd(); 280 for (rs = pInput.context()->relocSectBegin(); rs != rsEnd; ++rs) { 281 if (LDFileFormat::Ignore == (*rs)->kind()) 282 continue; 283 284 uint32_t offset = pInput.fileOffset() + (*rs)->offset(); 285 uint32_t size = (*rs)->size(); 286 MemoryRegion* region = mem->request(offset, size); 287 IRBuilder::CreateRelocData(**rs); ///< create relocation data for the header 288 switch ((*rs)->type()) { 289 case llvm::ELF::SHT_RELA: { 290 if (!m_pELFReader->readRela(pInput, **rs, *region)) { 291 mem->release(region); 292 return false; 293 } 294 break; 295 } 296 case llvm::ELF::SHT_REL: { 297 if (!m_pELFReader->readRel(pInput, **rs, *region)) { 298 mem->release(region); 299 return false; 300 } 301 break; 302 } 303 default: { ///< should not enter 304 mem->release(region); 305 return false; 306 } 307 } // end of switch 308 309 mem->release(region); 310 } // end of for all relocation data 311 312 return true; 313 } 314 315