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