ELFReader.cpp revision 6f75755c9204b1d8817ae5a65a2f7e5af0ec3f70
1//===- ELFReader.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/ELFReader.h> 10 11#include <mcld/IRBuilder.h> 12#include <mcld/Fragment/FillFragment.h> 13#include <mcld/LD/EhFrame.h> 14#include <mcld/LD/SectionData.h> 15#include <mcld/Target/GNULDBackend.h> 16#include <mcld/Support/MemoryRegion.h> 17#include <mcld/Support/MsgHandling.h> 18#include <mcld/Object/ObjectBuilder.h> 19 20#include <cstring> 21 22#include <llvm/ADT/StringRef.h> 23#include <llvm/ADT/Twine.h> 24#include <llvm/Support/ELF.h> 25#include <llvm/Support/Host.h> 26 27#include <iostream> 28 29using namespace mcld; 30 31//===----------------------------------------------------------------------===// 32// ELFReader<32, true> 33//===----------------------------------------------------------------------===// 34/// constructor 35ELFReader<32, true>::ELFReader(GNULDBackend& pBackend) 36 : ELFReaderIF(pBackend) { 37} 38 39/// destructor 40ELFReader<32, true>::~ELFReader() 41{ 42} 43 44/// isELF - is this a ELF file 45bool ELFReader<32, true>::isELF(void* pELFHeader) const 46{ 47 llvm::ELF::Elf32_Ehdr* hdr = 48 reinterpret_cast<llvm::ELF::Elf32_Ehdr*>(pELFHeader); 49 if (0 == memcmp(llvm::ELF::ElfMagic, hdr, 4)) 50 return true; 51 return false; 52} 53 54/// readRegularSection - read a regular section and create fragments. 55bool 56ELFReader<32, true>::readRegularSection(Input& pInput, SectionData& pSD) const 57{ 58 uint32_t offset = pInput.fileOffset() + pSD.getSection().offset(); 59 uint32_t size = pSD.getSection().size(); 60 61 Fragment* frag = IRBuilder::CreateRegion(pInput, offset, size); 62 ObjectBuilder::AppendFragment(*frag, pSD); 63 return true; 64} 65 66/// readSymbols - read ELF symbols and create LDSymbol 67bool ELFReader<32, true>::readSymbols(Input& pInput, 68 IRBuilder& pBuilder, 69 const MemoryRegion& pRegion, 70 const char* pStrTab) const 71{ 72 // get number of symbols 73 size_t entsize = pRegion.size()/sizeof(llvm::ELF::Elf32_Sym); 74 const llvm::ELF::Elf32_Sym* symtab = 75 reinterpret_cast<const llvm::ELF::Elf32_Sym*>(pRegion.start()); 76 77 uint32_t st_name = 0x0; 78 uint32_t st_value = 0x0; 79 uint32_t st_size = 0x0; 80 uint8_t st_info = 0x0; 81 uint8_t st_other = 0x0; 82 uint16_t st_shndx = 0x0; 83 84 // skip the first NULL symbol 85 pInput.context()->addSymbol(LDSymbol::Null()); 86 87 for (size_t idx = 1; idx < entsize; ++idx) { 88 st_info = symtab[idx].st_info; 89 st_other = symtab[idx].st_other; 90 91 if (llvm::sys::isLittleEndianHost()) { 92 st_name = symtab[idx].st_name; 93 st_value = symtab[idx].st_value; 94 st_size = symtab[idx].st_size; 95 st_shndx = symtab[idx].st_shndx; 96 } 97 else { 98 st_name = mcld::bswap32(symtab[idx].st_name); 99 st_value = mcld::bswap32(symtab[idx].st_value); 100 st_size = mcld::bswap32(symtab[idx].st_size); 101 st_shndx = mcld::bswap16(symtab[idx].st_shndx); 102 } 103 104 // If the section should not be included, set the st_shndx SHN_UNDEF 105 // - A section in interrelated groups are not included. 106 if (pInput.type() == Input::Object && 107 st_shndx < llvm::ELF::SHN_LORESERVE && 108 st_shndx != llvm::ELF::SHN_UNDEF) { 109 if (NULL == pInput.context()->getSection(st_shndx)) 110 st_shndx = llvm::ELF::SHN_UNDEF; 111 } 112 113 // get ld_type 114 ResolveInfo::Type ld_type = getSymType(st_info, st_shndx); 115 116 // get ld_desc 117 ResolveInfo::Desc ld_desc = getSymDesc(st_shndx, pInput); 118 119 // get ld_binding 120 ResolveInfo::Binding ld_binding = getSymBinding((st_info >> 4), st_shndx, st_other); 121 122 // get ld_value - ld_value must be section relative. 123 uint64_t ld_value = getSymValue(st_value, st_shndx, pInput); 124 125 // get ld_vis 126 ResolveInfo::Visibility ld_vis = getSymVisibility(st_other); 127 128 // get section 129 LDSection* section = NULL; 130 if (st_shndx < llvm::ELF::SHN_LORESERVE) // including ABS and COMMON 131 section = pInput.context()->getSection(st_shndx); 132 133 // get ld_name 134 std::string ld_name; 135 if (ResolveInfo::Section == ld_type) { 136 // Section symbol's st_name is the section index. 137 assert(NULL != section && "get a invalid section"); 138 ld_name = section->name(); 139 } 140 else { 141 ld_name = std::string(pStrTab + st_name); 142 } 143 144 pBuilder.AddSymbol(pInput, 145 ld_name, 146 ld_type, 147 ld_desc, 148 ld_binding, 149 st_size, 150 ld_value, 151 section, ld_vis); 152 } // end of for loop 153 return true; 154} 155 156//===----------------------------------------------------------------------===// 157// ELFReader::read relocations - read ELF rela and rel, and create Relocation 158//===----------------------------------------------------------------------===// 159/// ELFReader::readRela - read ELF rela and create Relocation 160bool ELFReader<32, true>::readRela(Input& pInput, 161 LDSection& pSection, 162 const MemoryRegion& pRegion) const 163{ 164 // get the number of rela 165 size_t entsize = pRegion.size() / sizeof(llvm::ELF::Elf32_Rela); 166 const llvm::ELF::Elf32_Rela* relaTab = 167 reinterpret_cast<const llvm::ELF::Elf32_Rela*>(pRegion.start()); 168 169 for (size_t idx=0; idx < entsize; ++idx) { 170 uint32_t r_offset = 0x0; 171 uint32_t r_info = 0x0; 172 int32_t r_addend = 0; 173 if (llvm::sys::isLittleEndianHost()) { 174 r_offset = relaTab[idx].r_offset; 175 r_info = relaTab[idx].r_info; 176 r_addend = relaTab[idx].r_addend; 177 } 178 else { 179 r_offset = mcld::bswap32(relaTab[idx].r_offset); 180 r_info = mcld::bswap32(relaTab[idx].r_info); 181 r_addend = mcld::bswap32(relaTab[idx].r_addend); 182 } 183 184 uint8_t r_type = static_cast<unsigned char>(r_info); 185 uint32_t r_sym = (r_info >> 8); 186 LDSymbol* symbol = pInput.context()->getSymbol(r_sym); 187 if (NULL == symbol) { 188 fatal(diag::err_cannot_read_symbol) << r_sym << pInput.path(); 189 } 190 191 IRBuilder::AddRelocation(pSection, r_type, *symbol, r_offset, r_addend); 192 } // end of for 193 return true; 194} 195 196/// readRel - read ELF rel and create Relocation 197bool ELFReader<32, true>::readRel(Input& pInput, 198 LDSection& pSection, 199 const MemoryRegion& pRegion) const 200{ 201 // get the number of rel 202 size_t entsize = pRegion.size() / sizeof(llvm::ELF::Elf32_Rel); 203 const llvm::ELF::Elf32_Rel* relTab = 204 reinterpret_cast<const llvm::ELF::Elf32_Rel*>(pRegion.start()); 205 206 for (size_t idx=0; idx < entsize; ++idx) { 207 uint32_t r_offset = 0x0; 208 uint32_t r_info = 0x0; 209 if (llvm::sys::isLittleEndianHost()) { 210 r_offset = relTab[idx].r_offset; 211 r_info = relTab[idx].r_info; 212 } 213 else { 214 r_offset = mcld::bswap32(relTab[idx].r_offset); 215 r_info = mcld::bswap32(relTab[idx].r_info); 216 } 217 218 uint8_t r_type = static_cast<unsigned char>(r_info); 219 uint32_t r_sym = (r_info >> 8); 220 221 LDSymbol* symbol = pInput.context()->getSymbol(r_sym); 222 if (NULL == symbol) { 223 fatal(diag::err_cannot_read_symbol) << r_sym << pInput.path(); 224 } 225 226 IRBuilder::AddRelocation(pSection, r_type, *symbol, r_offset); 227 } // end of for 228 return true; 229} 230 231/// isMyEndian - is this ELF file in the same endian to me? 232bool ELFReader<32, true>::isMyEndian(void* pELFHeader) const 233{ 234 llvm::ELF::Elf32_Ehdr* hdr = 235 reinterpret_cast<llvm::ELF::Elf32_Ehdr*>(pELFHeader); 236 237 return (hdr->e_ident[llvm::ELF::EI_DATA] == llvm::ELF::ELFDATA2LSB); 238} 239 240/// isMyMachine - is this ELF file generated for the same machine. 241bool ELFReader<32, true>::isMyMachine(void* pELFHeader) const 242{ 243 llvm::ELF::Elf32_Ehdr* hdr = 244 reinterpret_cast<llvm::ELF::Elf32_Ehdr*>(pELFHeader); 245 246 if (llvm::sys::isLittleEndianHost()) 247 return (hdr->e_machine == target().getInfo().machine()); 248 return (mcld::bswap16(hdr->e_machine) == target().getInfo().machine()); 249} 250 251/// fileType - return the file type 252Input::Type ELFReader<32, true>::fileType(void* pELFHeader) const 253{ 254 llvm::ELF::Elf32_Ehdr* hdr = 255 reinterpret_cast<llvm::ELF::Elf32_Ehdr*>(pELFHeader); 256 uint32_t type = 0x0; 257 if (llvm::sys::isLittleEndianHost()) 258 type = hdr->e_type; 259 else 260 type = mcld::bswap16(hdr->e_type); 261 262 switch(type) { 263 case llvm::ELF::ET_REL: 264 return Input::Object; 265 case llvm::ELF::ET_EXEC: 266 return Input::Exec; 267 case llvm::ELF::ET_DYN: 268 return Input::DynObj; 269 case llvm::ELF::ET_CORE: 270 return Input::CoreFile; 271 case llvm::ELF::ET_NONE: 272 default: 273 return Input::Unknown; 274 } 275} 276 277/// readSectionHeaders - read ELF section header table and create LDSections 278bool 279ELFReader<32, true>::readSectionHeaders(Input& pInput, void* pELFHeader) const 280{ 281 llvm::ELF::Elf32_Ehdr* ehdr = 282 reinterpret_cast<llvm::ELF::Elf32_Ehdr*>(pELFHeader); 283 284 uint32_t shoff = 0x0; 285 uint16_t shentsize = 0x0; 286 uint32_t shnum = 0x0; 287 uint32_t shstrtab = 0x0; 288 289 if (llvm::sys::isLittleEndianHost()) { 290 shoff = ehdr->e_shoff; 291 shentsize = ehdr->e_shentsize; 292 shnum = ehdr->e_shnum; 293 shstrtab = ehdr->e_shstrndx; 294 } 295 else { 296 shoff = mcld::bswap32(ehdr->e_shoff); 297 shentsize = mcld::bswap16(ehdr->e_shentsize); 298 shnum = mcld::bswap16(ehdr->e_shnum); 299 shstrtab = mcld::bswap16(ehdr->e_shstrndx); 300 } 301 302 // If the file has no section header table, e_shoff holds zero. 303 if (0x0 == shoff) 304 return true; 305 306 llvm::ELF::Elf32_Shdr *shdr = NULL; 307 MemoryRegion* shdr_region = NULL; 308 uint32_t sh_name = 0x0; 309 uint32_t sh_type = 0x0; 310 uint32_t sh_flags = 0x0; 311 uint32_t sh_offset = 0x0; 312 uint32_t sh_size = 0x0; 313 uint32_t sh_link = 0x0; 314 uint32_t sh_info = 0x0; 315 uint32_t sh_addralign = 0x0; 316 317 // if shnum and shstrtab overflow, the actual values are in the 1st shdr 318 if (shnum == llvm::ELF::SHN_UNDEF || shstrtab == llvm::ELF::SHN_XINDEX) { 319 shdr_region = pInput.memArea()->request(pInput.fileOffset() + shoff, 320 shentsize); 321 shdr = reinterpret_cast<llvm::ELF::Elf32_Shdr*>(shdr_region->start()); 322 323 if (llvm::sys::isLittleEndianHost()) { 324 sh_size = shdr->sh_size; 325 sh_link = shdr->sh_link; 326 } 327 else { 328 sh_size = mcld::bswap32(shdr->sh_size); 329 sh_link = mcld::bswap32(shdr->sh_link); 330 } 331 pInput.memArea()->release(shdr_region); 332 333 if (shnum == llvm::ELF::SHN_UNDEF) 334 shnum = sh_size; 335 if (shstrtab == llvm::ELF::SHN_XINDEX) 336 shstrtab = sh_link; 337 338 shoff += shentsize; 339 } 340 341 shdr_region = pInput.memArea()->request(pInput.fileOffset() + shoff, 342 shnum * shentsize); 343 llvm::ELF::Elf32_Shdr * shdrTab = 344 reinterpret_cast<llvm::ELF::Elf32_Shdr*>(shdr_region->start()); 345 346 // get .shstrtab first 347 shdr = &shdrTab[shstrtab]; 348 if (llvm::sys::isLittleEndianHost()) { 349 sh_offset = shdr->sh_offset; 350 sh_size = shdr->sh_size; 351 } 352 else { 353 sh_offset = mcld::bswap32(shdr->sh_offset); 354 sh_size = mcld::bswap32(shdr->sh_size); 355 } 356 357 MemoryRegion* sect_name_region = pInput.memArea()->request( 358 pInput.fileOffset() + sh_offset, sh_size); 359 const char* sect_name = 360 reinterpret_cast<const char*>(sect_name_region->start()); 361 362 LinkInfoList link_info_list; 363 364 // create all LDSections, including first NULL section. 365 for (size_t idx = 0; idx < shnum; ++idx) { 366 if (llvm::sys::isLittleEndianHost()) { 367 sh_name = shdrTab[idx].sh_name; 368 sh_type = shdrTab[idx].sh_type; 369 sh_flags = shdrTab[idx].sh_flags; 370 sh_offset = shdrTab[idx].sh_offset; 371 sh_size = shdrTab[idx].sh_size; 372 sh_link = shdrTab[idx].sh_link; 373 sh_info = shdrTab[idx].sh_info; 374 sh_addralign = shdrTab[idx].sh_addralign; 375 } 376 else { 377 sh_name = mcld::bswap32(shdrTab[idx].sh_name); 378 sh_type = mcld::bswap32(shdrTab[idx].sh_type); 379 sh_flags = mcld::bswap32(shdrTab[idx].sh_flags); 380 sh_offset = mcld::bswap32(shdrTab[idx].sh_offset); 381 sh_size = mcld::bswap32(shdrTab[idx].sh_size); 382 sh_link = mcld::bswap32(shdrTab[idx].sh_link); 383 sh_info = mcld::bswap32(shdrTab[idx].sh_info); 384 sh_addralign = mcld::bswap32(shdrTab[idx].sh_addralign); 385 } 386 387 LDSection* section = IRBuilder::CreateELFHeader(pInput, 388 sect_name+sh_name, 389 sh_type, 390 sh_flags, 391 sh_addralign); 392 section->setSize(sh_size); 393 section->setOffset(sh_offset); 394 section->setInfo(sh_info); 395 396 if (sh_link != 0x0 || sh_info != 0x0) { 397 LinkInfo link_info = { section, sh_link, sh_info }; 398 link_info_list.push_back(link_info); 399 } 400 } // end of for 401 402 // set up InfoLink 403 LinkInfoList::iterator info, infoEnd = link_info_list.end(); 404 for (info = link_info_list.begin(); info != infoEnd; ++info) { 405 if (LDFileFormat::NamePool == info->section->kind() || 406 LDFileFormat::Group == info->section->kind() || 407 LDFileFormat::Note == info->section->kind()) { 408 info->section->setLink(pInput.context()->getSection(info->sh_link)); 409 continue; 410 } 411 if (LDFileFormat::Relocation == info->section->kind()) { 412 info->section->setLink(pInput.context()->getSection(info->sh_info)); 413 continue; 414 } 415 } 416 417 pInput.memArea()->release(shdr_region); 418 pInput.memArea()->release(sect_name_region); 419 420 return true; 421} 422 423/// readSignature - read a symbol from the given Input and index in symtab 424/// This is used to get the signature of a group section. 425ResolveInfo* ELFReader<32, true>::readSignature(Input& pInput, 426 LDSection& pSymTab, 427 uint32_t pSymIdx) const 428{ 429 LDSection* symtab = &pSymTab; 430 LDSection* strtab = symtab->getLink(); 431 assert(NULL != symtab && NULL != strtab); 432 433 uint32_t offset = pInput.fileOffset() + symtab->offset() + 434 sizeof(llvm::ELF::Elf32_Sym) * pSymIdx; 435 MemoryRegion* symbol_region = 436 pInput.memArea()->request(offset, sizeof(llvm::ELF::Elf32_Sym)); 437 llvm::ELF::Elf32_Sym* entry = 438 reinterpret_cast<llvm::ELF::Elf32_Sym*>(symbol_region->start()); 439 440 uint32_t st_name = 0x0; 441 uint8_t st_info = 0x0; 442 uint8_t st_other = 0x0; 443 uint16_t st_shndx = 0x0; 444 st_info = entry->st_info; 445 st_other = entry->st_other; 446 if (llvm::sys::isLittleEndianHost()) { 447 st_name = entry->st_name; 448 st_shndx = entry->st_shndx; 449 } 450 else { 451 st_name = mcld::bswap32(entry->st_name); 452 st_shndx = mcld::bswap16(entry->st_shndx); 453 } 454 455 MemoryRegion* strtab_region = pInput.memArea()->request( 456 pInput.fileOffset() + strtab->offset(), strtab->size()); 457 458 // get ld_name 459 llvm::StringRef ld_name( 460 reinterpret_cast<char*>(strtab_region->start() + st_name)); 461 462 ResolveInfo* result = ResolveInfo::Create(ld_name); 463 result->setSource(pInput.type() == Input::DynObj); 464 result->setType(static_cast<ResolveInfo::Type>(st_info & 0xF)); 465 result->setDesc(getSymDesc(st_shndx, pInput)); 466 result->setBinding(getSymBinding((st_info >> 4), st_shndx, st_other)); 467 result->setVisibility(getSymVisibility(st_other)); 468 469 // release regions 470 pInput.memArea()->release(symbol_region); 471 pInput.memArea()->release(strtab_region); 472 473 return result; 474} 475 476/// readDynamic - read ELF .dynamic in input dynobj 477bool ELFReader<32, true>::readDynamic(Input& pInput) const 478{ 479 assert(pInput.type() == Input::DynObj); 480 const LDSection* dynamic_sect = pInput.context()->getSection(".dynamic"); 481 if (NULL == dynamic_sect) { 482 fatal(diag::err_cannot_read_section) << ".dynamic"; 483 } 484 const LDSection* dynstr_sect = dynamic_sect->getLink(); 485 if (NULL == dynstr_sect) { 486 fatal(diag::err_cannot_read_section) << ".dynstr"; 487 } 488 489 MemoryRegion* dynamic_region = pInput.memArea()->request( 490 pInput.fileOffset() + dynamic_sect->offset(), dynamic_sect->size()); 491 492 MemoryRegion* dynstr_region = pInput.memArea()->request( 493 pInput.fileOffset() + dynstr_sect->offset(), dynstr_sect->size()); 494 495 assert(NULL != dynamic_region && NULL != dynstr_region); 496 497 const llvm::ELF::Elf32_Dyn* dynamic = 498 (llvm::ELF::Elf32_Dyn*) dynamic_region->start(); 499 const char* dynstr = (const char*) dynstr_region->start(); 500 bool hasSOName = false; 501 size_t numOfEntries = dynamic_sect->size() / sizeof(llvm::ELF::Elf32_Dyn); 502 503 for (size_t idx = 0; idx < numOfEntries; ++idx) { 504 505 llvm::ELF::Elf32_Sword d_tag = 0x0; 506 llvm::ELF::Elf32_Word d_val = 0x0; 507 508 if (llvm::sys::isLittleEndianHost()) { 509 d_tag = dynamic[idx].d_tag; 510 d_val = dynamic[idx].d_un.d_val; 511 } else { 512 d_tag = mcld::bswap32(dynamic[idx].d_tag); 513 d_val = mcld::bswap32(dynamic[idx].d_un.d_val); 514 } 515 516 switch (d_tag) { 517 case llvm::ELF::DT_SONAME: 518 assert(d_val < dynstr_sect->size()); 519 pInput.setName(sys::fs::Path(dynstr + d_val).filename().native()); 520 hasSOName = true; 521 break; 522 case llvm::ELF::DT_NEEDED: 523 // TODO: 524 break; 525 case llvm::ELF::DT_NULL: 526 default: 527 break; 528 } 529 } 530 531 // if there is no SONAME in .dynamic, then set it from input path 532 if (!hasSOName) 533 pInput.setName(pInput.path().filename().native()); 534 535 pInput.memArea()->release(dynamic_region); 536 pInput.memArea()->release(dynstr_region); 537 return true; 538} 539 540//===----------------------------------------------------------------------===// 541// ELFReader<64, true> 542//===----------------------------------------------------------------------===// 543/// constructor 544ELFReader<64, true>::ELFReader(GNULDBackend& pBackend) 545 : ELFReaderIF(pBackend) { 546} 547 548/// destructor 549ELFReader<64, true>::~ELFReader() 550{ 551} 552 553/// isELF - is this a ELF file 554bool ELFReader<64, true>::isELF(void* pELFHeader) const 555{ 556 llvm::ELF::Elf64_Ehdr* hdr = 557 reinterpret_cast<llvm::ELF::Elf64_Ehdr*>(pELFHeader); 558 if (0 == memcmp(llvm::ELF::ElfMagic, hdr, 4)) 559 return true; 560 return false; 561} 562 563/// readRegularSection - read a regular section and create fragments. 564bool 565ELFReader<64, true>::readRegularSection(Input& pInput, SectionData& pSD) const 566{ 567 uint64_t offset = pInput.fileOffset() + pSD.getSection().offset(); 568 uint64_t size = pSD.getSection().size(); 569 570 Fragment* frag = IRBuilder::CreateRegion(pInput, offset, size); 571 ObjectBuilder::AppendFragment(*frag, pSD); 572 return true; 573} 574 575/// readSymbols - read ELF symbols and create LDSymbol 576bool ELFReader<64, true>::readSymbols(Input& pInput, 577 IRBuilder& pBuilder, 578 const MemoryRegion& pRegion, 579 const char* pStrTab) const 580{ 581 // get number of symbols 582 size_t entsize = pRegion.size()/sizeof(llvm::ELF::Elf64_Sym); 583 const llvm::ELF::Elf64_Sym* symtab = 584 reinterpret_cast<const llvm::ELF::Elf64_Sym*>(pRegion.start()); 585 586 uint32_t st_name = 0x0; 587 uint64_t st_value = 0x0; 588 uint64_t st_size = 0x0; 589 uint8_t st_info = 0x0; 590 uint8_t st_other = 0x0; 591 uint16_t st_shndx = 0x0; 592 593 // skip the first NULL symbol 594 pInput.context()->addSymbol(LDSymbol::Null()); 595 596 for (size_t idx = 1; idx < entsize; ++idx) { 597 st_info = symtab[idx].st_info; 598 st_other = symtab[idx].st_other; 599 600 if (llvm::sys::isLittleEndianHost()) { 601 st_name = symtab[idx].st_name; 602 st_value = symtab[idx].st_value; 603 st_size = symtab[idx].st_size; 604 st_shndx = symtab[idx].st_shndx; 605 } 606 else { 607 st_name = mcld::bswap32(symtab[idx].st_name); 608 st_value = mcld::bswap64(symtab[idx].st_value); 609 st_size = mcld::bswap64(symtab[idx].st_size); 610 st_shndx = mcld::bswap16(symtab[idx].st_shndx); 611 } 612 613 // If the section should not be included, set the st_shndx SHN_UNDEF 614 // - A section in interrelated groups are not included. 615 if (pInput.type() == Input::Object && 616 st_shndx < llvm::ELF::SHN_LORESERVE && 617 st_shndx != llvm::ELF::SHN_UNDEF) { 618 if (NULL == pInput.context()->getSection(st_shndx)) 619 st_shndx = llvm::ELF::SHN_UNDEF; 620 } 621 622 // get ld_type 623 ResolveInfo::Type ld_type = getSymType(st_info, st_shndx); 624 625 // get ld_desc 626 ResolveInfo::Desc ld_desc = getSymDesc(st_shndx, pInput); 627 628 // get ld_binding 629 ResolveInfo::Binding ld_binding = getSymBinding((st_info >> 4), st_shndx, st_other); 630 631 // get ld_value - ld_value must be section relative. 632 uint64_t ld_value = getSymValue(st_value, st_shndx, pInput); 633 634 // get ld_vis 635 ResolveInfo::Visibility ld_vis = getSymVisibility(st_other); 636 637 // get section 638 LDSection* section = NULL; 639 if (st_shndx < llvm::ELF::SHN_LORESERVE) // including ABS and COMMON 640 section = pInput.context()->getSection(st_shndx); 641 642 // get ld_name 643 std::string ld_name; 644 if (ResolveInfo::Section == ld_type) { 645 // Section symbol's st_name is the section index. 646 assert(NULL != section && "get a invalid section"); 647 ld_name = section->name(); 648 } 649 else { 650 ld_name = std::string(pStrTab + st_name); 651 } 652 653 pBuilder.AddSymbol(pInput, 654 ld_name, 655 ld_type, 656 ld_desc, 657 ld_binding, 658 st_size, 659 ld_value, 660 section, ld_vis); 661 } // end of for loop 662 return true; 663} 664 665//===----------------------------------------------------------------------===// 666// ELFReader::read relocations - read ELF rela and rel, and create Relocation 667//===----------------------------------------------------------------------===// 668/// ELFReader::readRela - read ELF rela and create Relocation 669bool ELFReader<64, true>::readRela(Input& pInput, 670 LDSection& pSection, 671 const MemoryRegion& pRegion) const 672{ 673 // get the number of rela 674 size_t entsize = pRegion.size() / sizeof(llvm::ELF::Elf64_Rela); 675 const llvm::ELF::Elf64_Rela* relaTab = 676 reinterpret_cast<const llvm::ELF::Elf64_Rela*>(pRegion.start()); 677 678 for (size_t idx=0; idx < entsize; ++idx) { 679 uint64_t r_offset = 0x0; 680 uint64_t r_info = 0x0; 681 int64_t r_addend = 0; 682 if (llvm::sys::isLittleEndianHost()) { 683 r_offset = relaTab[idx].r_offset; 684 r_info = relaTab[idx].r_info; 685 r_addend = relaTab[idx].r_addend; 686 } 687 else { 688 r_offset = mcld::bswap64(relaTab[idx].r_offset); 689 r_info = mcld::bswap64(relaTab[idx].r_info); 690 r_addend = mcld::bswap64(relaTab[idx].r_addend); 691 } 692 693 uint32_t r_type = static_cast<uint32_t>(r_info); 694 uint32_t r_sym = (r_info >> 32); 695 LDSymbol* symbol = pInput.context()->getSymbol(r_sym); 696 if (NULL == symbol) { 697 fatal(diag::err_cannot_read_symbol) << r_sym << pInput.path(); 698 } 699 700 IRBuilder::AddRelocation(pSection, r_type, *symbol, r_offset, r_addend); 701 } // end of for 702 return true; 703} 704 705/// readRel - read ELF rel and create Relocation 706bool ELFReader<64, true>::readRel(Input& pInput, 707 LDSection& pSection, 708 const MemoryRegion& pRegion) const 709{ 710 // get the number of rel 711 size_t entsize = pRegion.size() / sizeof(llvm::ELF::Elf64_Rel); 712 const llvm::ELF::Elf64_Rel* relTab = 713 reinterpret_cast<const llvm::ELF::Elf64_Rel*>(pRegion.start()); 714 715 for (size_t idx=0; idx < entsize; ++idx) { 716 uint64_t r_offset = 0x0; 717 uint64_t r_info = 0x0; 718 if (llvm::sys::isLittleEndianHost()) { 719 r_offset = relTab[idx].r_offset; 720 r_info = relTab[idx].r_info; 721 } 722 else { 723 r_offset = mcld::bswap64(relTab[idx].r_offset); 724 r_info = mcld::bswap64(relTab[idx].r_info); 725 } 726 727 uint32_t r_type = static_cast<uint32_t>(r_info); 728 uint32_t r_sym = (r_info >> 32); 729 730 LDSymbol* symbol = pInput.context()->getSymbol(r_sym); 731 if (NULL == symbol) { 732 fatal(diag::err_cannot_read_symbol) << r_sym << pInput.path(); 733 } 734 735 IRBuilder::AddRelocation(pSection, r_type, *symbol, r_offset); 736 } // end of for 737 return true; 738} 739 740/// isMyEndian - is this ELF file in the same endian to me? 741bool ELFReader<64, true>::isMyEndian(void* pELFHeader) const 742{ 743 llvm::ELF::Elf64_Ehdr* hdr = 744 reinterpret_cast<llvm::ELF::Elf64_Ehdr*>(pELFHeader); 745 746 return (hdr->e_ident[llvm::ELF::EI_DATA] == llvm::ELF::ELFDATA2LSB); 747} 748 749/// isMyMachine - is this ELF file generated for the same machine. 750bool ELFReader<64, true>::isMyMachine(void* pELFHeader) const 751{ 752 llvm::ELF::Elf64_Ehdr* hdr = 753 reinterpret_cast<llvm::ELF::Elf64_Ehdr*>(pELFHeader); 754 755 if (llvm::sys::isLittleEndianHost()) 756 return (hdr->e_machine == target().getInfo().machine()); 757 return (mcld::bswap16(hdr->e_machine) == target().getInfo().machine()); 758} 759 760/// fileType - return the file type 761Input::Type ELFReader<64, true>::fileType(void* pELFHeader) const 762{ 763 llvm::ELF::Elf64_Ehdr* hdr = 764 reinterpret_cast<llvm::ELF::Elf64_Ehdr*>(pELFHeader); 765 uint32_t type = 0x0; 766 if (llvm::sys::isLittleEndianHost()) 767 type = hdr->e_type; 768 else 769 type = mcld::bswap16(hdr->e_type); 770 771 switch(type) { 772 case llvm::ELF::ET_REL: 773 return Input::Object; 774 case llvm::ELF::ET_EXEC: 775 return Input::Exec; 776 case llvm::ELF::ET_DYN: 777 return Input::DynObj; 778 case llvm::ELF::ET_CORE: 779 return Input::CoreFile; 780 case llvm::ELF::ET_NONE: 781 default: 782 return Input::Unknown; 783 } 784} 785 786/// readSectionHeaders - read ELF section header table and create LDSections 787bool 788ELFReader<64, true>::readSectionHeaders(Input& pInput, void* pELFHeader) const 789{ 790 llvm::ELF::Elf64_Ehdr* ehdr = 791 reinterpret_cast<llvm::ELF::Elf64_Ehdr*>(pELFHeader); 792 793 uint64_t shoff = 0x0; 794 uint16_t shentsize = 0x0; 795 uint32_t shnum = 0x0; 796 uint32_t shstrtab = 0x0; 797 798 if (llvm::sys::isLittleEndianHost()) { 799 shoff = ehdr->e_shoff; 800 shentsize = ehdr->e_shentsize; 801 shnum = ehdr->e_shnum; 802 shstrtab = ehdr->e_shstrndx; 803 } 804 else { 805 shoff = mcld::bswap64(ehdr->e_shoff); 806 shentsize = mcld::bswap16(ehdr->e_shentsize); 807 shnum = mcld::bswap16(ehdr->e_shnum); 808 shstrtab = mcld::bswap16(ehdr->e_shstrndx); 809 } 810 811 // If the file has no section header table, e_shoff holds zero. 812 if (0x0 == shoff) 813 return true; 814 815 llvm::ELF::Elf64_Shdr *shdr = NULL; 816 MemoryRegion* shdr_region = NULL; 817 uint32_t sh_name = 0x0; 818 uint32_t sh_type = 0x0; 819 uint64_t sh_flags = 0x0; 820 uint64_t sh_offset = 0x0; 821 uint64_t sh_size = 0x0; 822 uint32_t sh_link = 0x0; 823 uint32_t sh_info = 0x0; 824 uint64_t sh_addralign = 0x0; 825 826 // if shnum and shstrtab overflow, the actual values are in the 1st shdr 827 if (shnum == llvm::ELF::SHN_UNDEF || shstrtab == llvm::ELF::SHN_XINDEX) { 828 shdr_region = pInput.memArea()->request(pInput.fileOffset() + shoff, 829 shentsize); 830 shdr = reinterpret_cast<llvm::ELF::Elf64_Shdr*>(shdr_region->start()); 831 832 if (llvm::sys::isLittleEndianHost()) { 833 sh_size = shdr->sh_size; 834 sh_link = shdr->sh_link; 835 } 836 else { 837 sh_size = mcld::bswap64(shdr->sh_size); 838 sh_link = mcld::bswap32(shdr->sh_link); 839 } 840 pInput.memArea()->release(shdr_region); 841 842 if (shnum == llvm::ELF::SHN_UNDEF) 843 shnum = sh_size; 844 if (shstrtab == llvm::ELF::SHN_XINDEX) 845 shstrtab = sh_link; 846 847 shoff += shentsize; 848 } 849 850 shdr_region = pInput.memArea()->request(pInput.fileOffset() + shoff, 851 shnum * shentsize); 852 llvm::ELF::Elf64_Shdr * shdrTab = 853 reinterpret_cast<llvm::ELF::Elf64_Shdr*>(shdr_region->start()); 854 855 // get .shstrtab first 856 shdr = &shdrTab[shstrtab]; 857 if (llvm::sys::isLittleEndianHost()) { 858 sh_offset = shdr->sh_offset; 859 sh_size = shdr->sh_size; 860 } 861 else { 862 sh_offset = mcld::bswap64(shdr->sh_offset); 863 sh_size = mcld::bswap64(shdr->sh_size); 864 } 865 866 MemoryRegion* sect_name_region = pInput.memArea()->request( 867 pInput.fileOffset() + sh_offset, sh_size); 868 const char* sect_name = 869 reinterpret_cast<const char*>(sect_name_region->start()); 870 871 LinkInfoList link_info_list; 872 873 // create all LDSections, including first NULL section. 874 for (size_t idx = 0; idx < shnum; ++idx) { 875 if (llvm::sys::isLittleEndianHost()) { 876 sh_name = shdrTab[idx].sh_name; 877 sh_type = shdrTab[idx].sh_type; 878 sh_flags = shdrTab[idx].sh_flags; 879 sh_offset = shdrTab[idx].sh_offset; 880 sh_size = shdrTab[idx].sh_size; 881 sh_link = shdrTab[idx].sh_link; 882 sh_info = shdrTab[idx].sh_info; 883 sh_addralign = shdrTab[idx].sh_addralign; 884 } 885 else { 886 sh_name = mcld::bswap32(shdrTab[idx].sh_name); 887 sh_type = mcld::bswap32(shdrTab[idx].sh_type); 888 sh_flags = mcld::bswap64(shdrTab[idx].sh_flags); 889 sh_offset = mcld::bswap64(shdrTab[idx].sh_offset); 890 sh_size = mcld::bswap64(shdrTab[idx].sh_size); 891 sh_link = mcld::bswap32(shdrTab[idx].sh_link); 892 sh_info = mcld::bswap32(shdrTab[idx].sh_info); 893 sh_addralign = mcld::bswap64(shdrTab[idx].sh_addralign); 894 } 895 896 LDSection* section = IRBuilder::CreateELFHeader(pInput, 897 sect_name+sh_name, 898 sh_type, 899 sh_flags, 900 sh_addralign); 901 section->setSize(sh_size); 902 section->setOffset(sh_offset); 903 section->setInfo(sh_info); 904 905 if (sh_link != 0x0 || sh_info != 0x0) { 906 LinkInfo link_info = { section, sh_link, sh_info }; 907 link_info_list.push_back(link_info); 908 } 909 } // end of for 910 911 // set up InfoLink 912 LinkInfoList::iterator info, infoEnd = link_info_list.end(); 913 for (info = link_info_list.begin(); info != infoEnd; ++info) { 914 if (LDFileFormat::NamePool == info->section->kind() || 915 LDFileFormat::Group == info->section->kind() || 916 LDFileFormat::Note == info->section->kind()) { 917 info->section->setLink(pInput.context()->getSection(info->sh_link)); 918 continue; 919 } 920 if (LDFileFormat::Relocation == info->section->kind()) { 921 info->section->setLink(pInput.context()->getSection(info->sh_info)); 922 continue; 923 } 924 } 925 926 pInput.memArea()->release(shdr_region); 927 pInput.memArea()->release(sect_name_region); 928 929 return true; 930} 931 932/// readSignature - read a symbol from the given Input and index in symtab 933/// This is used to get the signature of a group section. 934ResolveInfo* ELFReader<64, true>::readSignature(Input& pInput, 935 LDSection& pSymTab, 936 uint32_t pSymIdx) const 937{ 938 LDSection* symtab = &pSymTab; 939 LDSection* strtab = symtab->getLink(); 940 assert(NULL != symtab && NULL != strtab); 941 942 uint64_t offset = pInput.fileOffset() + symtab->offset() + 943 sizeof(llvm::ELF::Elf64_Sym) * pSymIdx; 944 MemoryRegion* symbol_region = 945 pInput.memArea()->request(offset, sizeof(llvm::ELF::Elf64_Sym)); 946 llvm::ELF::Elf64_Sym* entry = 947 reinterpret_cast<llvm::ELF::Elf64_Sym*>(symbol_region->start()); 948 949 uint32_t st_name = 0x0; 950 uint8_t st_info = 0x0; 951 uint8_t st_other = 0x0; 952 uint16_t st_shndx = 0x0; 953 st_info = entry->st_info; 954 st_other = entry->st_other; 955 if (llvm::sys::isLittleEndianHost()) { 956 st_name = entry->st_name; 957 st_shndx = entry->st_shndx; 958 } 959 else { 960 st_name = mcld::bswap32(entry->st_name); 961 st_shndx = mcld::bswap16(entry->st_shndx); 962 } 963 964 MemoryRegion* strtab_region = pInput.memArea()->request( 965 pInput.fileOffset() + strtab->offset(), strtab->size()); 966 967 // get ld_name 968 llvm::StringRef ld_name( 969 reinterpret_cast<char*>(strtab_region->start() + st_name)); 970 971 ResolveInfo* result = ResolveInfo::Create(ld_name); 972 result->setSource(pInput.type() == Input::DynObj); 973 result->setType(static_cast<ResolveInfo::Type>(st_info & 0xF)); 974 result->setDesc(getSymDesc(st_shndx, pInput)); 975 result->setBinding(getSymBinding((st_info >> 4), st_shndx, st_other)); 976 result->setVisibility(getSymVisibility(st_other)); 977 978 // release regions 979 pInput.memArea()->release(symbol_region); 980 pInput.memArea()->release(strtab_region); 981 982 return result; 983} 984 985/// readDynamic - read ELF .dynamic in input dynobj 986bool ELFReader<64, true>::readDynamic(Input& pInput) const 987{ 988 assert(pInput.type() == Input::DynObj); 989 const LDSection* dynamic_sect = pInput.context()->getSection(".dynamic"); 990 if (NULL == dynamic_sect) { 991 fatal(diag::err_cannot_read_section) << ".dynamic"; 992 } 993 const LDSection* dynstr_sect = dynamic_sect->getLink(); 994 if (NULL == dynstr_sect) { 995 fatal(diag::err_cannot_read_section) << ".dynstr"; 996 } 997 998 MemoryRegion* dynamic_region = pInput.memArea()->request( 999 pInput.fileOffset() + dynamic_sect->offset(), dynamic_sect->size()); 1000 1001 MemoryRegion* dynstr_region = pInput.memArea()->request( 1002 pInput.fileOffset() + dynstr_sect->offset(), dynstr_sect->size()); 1003 1004 assert(NULL != dynamic_region && NULL != dynstr_region); 1005 1006 const llvm::ELF::Elf64_Dyn* dynamic = 1007 (llvm::ELF::Elf64_Dyn*) dynamic_region->start(); 1008 const char* dynstr = (const char*) dynstr_region->start(); 1009 bool hasSOName = false; 1010 size_t numOfEntries = dynamic_sect->size() / sizeof(llvm::ELF::Elf64_Dyn); 1011 1012 for (size_t idx = 0; idx < numOfEntries; ++idx) { 1013 1014 llvm::ELF::Elf64_Sxword d_tag = 0x0; 1015 llvm::ELF::Elf64_Xword d_val = 0x0; 1016 1017 if (llvm::sys::isLittleEndianHost()) { 1018 d_tag = dynamic[idx].d_tag; 1019 d_val = dynamic[idx].d_un.d_val; 1020 } else { 1021 d_tag = mcld::bswap64(dynamic[idx].d_tag); 1022 d_val = mcld::bswap64(dynamic[idx].d_un.d_val); 1023 } 1024 1025 switch (d_tag) { 1026 case llvm::ELF::DT_SONAME: 1027 assert(d_val < dynstr_sect->size()); 1028 pInput.setName(sys::fs::Path(dynstr + d_val).filename().native()); 1029 hasSOName = true; 1030 break; 1031 case llvm::ELF::DT_NEEDED: 1032 // TODO: 1033 break; 1034 case llvm::ELF::DT_NULL: 1035 default: 1036 break; 1037 } 1038 } 1039 1040 // if there is no SONAME in .dynamic, then set it from input path 1041 if (!hasSOName) 1042 pInput.setName(pInput.path().filename().native()); 1043 1044 pInput.memArea()->release(dynamic_region); 1045 pInput.memArea()->release(dynstr_region); 1046 return true; 1047} 1048 1049