IRBuilder.cpp revision f33f6de54db174aa679a4b6d1e040d37e95541c0
1//===- IRBuilder.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/IRBuilder.h> 10#include <mcld/LinkerScript.h> 11#include <mcld/LD/ELFReader.h> 12#include <mcld/Object/ObjectBuilder.h> 13#include <mcld/LD/SectionData.h> 14#include <mcld/LD/EhFrame.h> 15#include <mcld/LD/RelocData.h> 16#include <mcld/Support/MsgHandling.h> 17#include <mcld/Support/MemoryArea.h> 18#include <mcld/Support/ELF.h> 19#include <mcld/Fragment/FragmentRef.h> 20#include <llvm/ADT/StringRef.h> 21 22using namespace mcld; 23 24//===----------------------------------------------------------------------===// 25// Helper Functions 26//===----------------------------------------------------------------------===// 27LDFileFormat::Kind GetELFSectionKind(uint32_t pType, const char* pName, 28 uint32_t pFlag) 29{ 30 if (pFlag & mcld::ELF::SHF_EXCLUDE) 31 return LDFileFormat::Exclude; 32 33 if (pFlag & llvm::ELF::SHF_MASKPROC) 34 return LDFileFormat::Target; 35 36 // name rules 37 llvm::StringRef name(pName); 38 if (name.startswith(".debug") || 39 name.startswith(".zdebug") || 40 name.startswith(".line") || 41 name.startswith(".stab")) 42 return LDFileFormat::Debug; 43 if (name.startswith(".comment")) 44 return LDFileFormat::MetaData; 45 if (name.startswith(".interp") || name.startswith(".dynamic")) 46 return LDFileFormat::Note; 47 if (name.startswith(".eh_frame")) 48 return LDFileFormat::EhFrame; 49 if (name.startswith(".eh_frame_hdr")) 50 return LDFileFormat::EhFrameHdr; 51 if (name.startswith(".gcc_except_table")) 52 return LDFileFormat::GCCExceptTable; 53 if (name.startswith(".note.GNU-stack")) 54 return LDFileFormat::StackNote; 55 if (name.startswith(".gnu.linkonce")) 56 return LDFileFormat::LinkOnce; 57 58 // type rules 59 switch(pType) { 60 case llvm::ELF::SHT_NULL: 61 return LDFileFormat::Null; 62 case llvm::ELF::SHT_INIT_ARRAY: 63 case llvm::ELF::SHT_FINI_ARRAY: 64 case llvm::ELF::SHT_PREINIT_ARRAY: 65 case llvm::ELF::SHT_PROGBITS: 66 return LDFileFormat::Regular; 67 case llvm::ELF::SHT_SYMTAB: 68 case llvm::ELF::SHT_DYNSYM: 69 case llvm::ELF::SHT_STRTAB: 70 case llvm::ELF::SHT_HASH: 71 case llvm::ELF::SHT_DYNAMIC: 72 case llvm::ELF::SHT_SYMTAB_SHNDX: 73 return LDFileFormat::NamePool; 74 case llvm::ELF::SHT_RELA: 75 case llvm::ELF::SHT_REL: 76 return LDFileFormat::Relocation; 77 case llvm::ELF::SHT_NOBITS: 78 return LDFileFormat::BSS; 79 case llvm::ELF::SHT_NOTE: 80 return LDFileFormat::Note; 81 case llvm::ELF::SHT_GROUP: 82 return LDFileFormat::Group; 83 case llvm::ELF::SHT_GNU_versym: 84 case llvm::ELF::SHT_GNU_verdef: 85 case llvm::ELF::SHT_GNU_verneed: 86 return LDFileFormat::Version; 87 case llvm::ELF::SHT_SHLIB: 88 return LDFileFormat::Target; 89 default: 90 if ((pType >= llvm::ELF::SHT_LOPROC && pType <= llvm::ELF::SHT_HIPROC) || 91 (pType >= llvm::ELF::SHT_LOOS && pType <= llvm::ELF::SHT_HIOS) || 92 (pType >= llvm::ELF::SHT_LOUSER && pType <= llvm::ELF::SHT_HIUSER)) 93 return LDFileFormat::Target; 94 fatal(diag::err_unsupported_section) << pName << pType; 95 } 96 return LDFileFormat::MetaData; 97} 98 99//===----------------------------------------------------------------------===// 100// IRBuilder 101//===----------------------------------------------------------------------===// 102IRBuilder::IRBuilder(Module& pModule, const LinkerConfig& pConfig) 103 : m_Module(pModule), m_Config(pConfig), m_InputBuilder(pConfig) { 104 m_InputBuilder.setCurrentTree(m_Module.getInputTree()); 105 106 // FIXME: where to set up Relocation? 107 Relocation::SetUp(m_Config); 108} 109 110IRBuilder::~IRBuilder() 111{ 112} 113 114/// CreateInput - To create an input file and append it to the input tree. 115Input* IRBuilder::CreateInput(const std::string& pName, 116 const sys::fs::Path& pPath, Input::Type pType) 117{ 118 if (Input::Unknown == pType) 119 return ReadInput(pName, pPath); 120 121 m_InputBuilder.createNode<InputTree::Positional>(pName, pPath, pType); 122 Input* input = *m_InputBuilder.getCurrentNode(); 123 124 if (!input->hasContext()) 125 m_InputBuilder.setContext(*input, false); 126 127 return input; 128} 129 130/// ReadInput - To read an input file and append it to the input tree. 131Input* 132IRBuilder::ReadInput(const std::string& pName, const sys::fs::Path& pPath) 133{ 134 m_InputBuilder.createNode<InputTree::Positional>(pName, pPath, Input::Unknown); 135 Input* input = *m_InputBuilder.getCurrentNode(); 136 137 if (!input->hasContext()) 138 m_InputBuilder.setContext(*input); 139 140 if (!input->hasMemArea()) 141 m_InputBuilder.setMemory(*input, FileHandle::ReadOnly, FileHandle::System); 142 143 return input; 144} 145 146/// ReadInput - To read an input file and append it to the input tree. 147Input* IRBuilder::ReadInput(const std::string& pNameSpec) 148{ 149 const sys::fs::Path* path = NULL; 150 // find out the real path of the namespec. 151 if (m_InputBuilder.getConstraint().isSharedSystem()) { 152 // In the system with shared object support, we can find both archive 153 // and shared object. 154 155 if (m_InputBuilder.getAttributes().isStatic()) { 156 // with --static, we must search an archive. 157 path = m_Module.getScript().directories().find(pNameSpec, Input::Archive); 158 } 159 else { 160 // otherwise, with --Bdynamic, we can find either an archive or a 161 // shared object. 162 path = m_Module.getScript().directories().find(pNameSpec, Input::DynObj); 163 } 164 } 165 else { 166 // In the system without shared object support, we only look for an archive 167 path = m_Module.getScript().directories().find(pNameSpec, Input::Archive); 168 } 169 170 if (NULL == path) { 171 fatal(diag::err_cannot_find_namespec) << pNameSpec; 172 return NULL; 173 } 174 175 m_InputBuilder.createNode<InputTree::Positional>(pNameSpec, *path); 176 Input* input = *m_InputBuilder.getCurrentNode(); 177 178 if (!input->hasContext()) 179 m_InputBuilder.setContext(*input); 180 181 if (!input->hasMemArea()) 182 m_InputBuilder.setMemory(*input, FileHandle::ReadOnly, FileHandle::System); 183 184 return input; 185} 186 187/// ReadInput - To read an input file and append it to the input tree. 188Input* IRBuilder::ReadInput(FileHandle& pFileHandle) 189{ 190 m_InputBuilder.createNode<InputTree::Positional>("file handler", 191 pFileHandle.path()); 192 193 Input* input = *m_InputBuilder.getCurrentNode(); 194 if (pFileHandle.path().empty()) { 195 m_InputBuilder.setContext(*input, false); 196 m_InputBuilder.setMemory(*input, pFileHandle.handler(), FileHandle::ReadOnly); 197 } 198 else { 199 m_InputBuilder.setContext(*input, true); 200 m_InputBuilder.setMemory(*input, FileHandle::ReadOnly, FileHandle::System); 201 } 202 203 return input; 204} 205 206/// ReadInput - To read an input file and append it to the input tree. 207Input* IRBuilder::ReadInput(const std::string& pName, void* pRawMemory, size_t pSize) 208{ 209 m_InputBuilder.createNode<InputTree::Positional>(pName, "NAN"); 210 Input* input = *m_InputBuilder.getCurrentNode(); 211 m_InputBuilder.setContext(*input, false); 212 m_InputBuilder.setMemory(*input, pRawMemory, pSize); 213 return input; 214} 215 216bool IRBuilder::StartGroup() 217{ 218 if (m_InputBuilder.isInGroup()) { 219 fatal(diag::fatal_forbid_nest_group); 220 return false; 221 } 222 m_InputBuilder.enterGroup(); 223 return true; 224} 225 226bool IRBuilder::EndGroup() 227{ 228 m_InputBuilder.exitGroup(); 229 return true; 230} 231 232void IRBuilder::WholeArchive() 233{ 234 m_InputBuilder.getAttributes().setWholeArchive(); 235} 236 237void IRBuilder::NoWholeArchive() 238{ 239 m_InputBuilder.getAttributes().unsetWholeArchive(); 240} 241 242void IRBuilder::AsNeeded() 243{ 244 m_InputBuilder.getAttributes().setAsNeeded(); 245} 246 247void IRBuilder::NoAsNeeded() 248{ 249 m_InputBuilder.getAttributes().unsetAsNeeded(); 250} 251 252void IRBuilder::CopyDTNeeded() 253{ 254 m_InputBuilder.getAttributes().setAddNeeded(); 255} 256 257void IRBuilder::NoCopyDTNeeded() 258{ 259 m_InputBuilder.getAttributes().unsetAddNeeded(); 260} 261 262void IRBuilder::AgainstShared() 263{ 264 m_InputBuilder.getAttributes().setDynamic(); 265} 266 267void IRBuilder::AgainstStatic() 268{ 269 m_InputBuilder.getAttributes().setStatic(); 270} 271 272LDSection* IRBuilder::CreateELFHeader(Input& pInput, 273 const std::string& pName, 274 uint32_t pType, 275 uint32_t pFlag, 276 uint32_t pAlign) 277{ 278 // Create section header 279 LDFileFormat::Kind kind = GetELFSectionKind(pType, pName.c_str(), pFlag); 280 LDSection* header = LDSection::Create(pName, kind, pType, pFlag); 281 header->setAlign(pAlign); 282 283 // Append section header in input 284 pInput.context()->appendSection(*header); 285 return header; 286} 287 288/// CreateSectionData - To create a section data for given pSection. 289SectionData* IRBuilder::CreateSectionData(LDSection& pSection) 290{ 291 assert(!pSection.hasSectionData() && "pSection already has section data."); 292 293 SectionData* sect_data = SectionData::Create(pSection); 294 pSection.setSectionData(sect_data); 295 return sect_data; 296} 297 298/// CreateRelocData - To create a relocation data for given pSection. 299RelocData* IRBuilder::CreateRelocData(LDSection &pSection) 300{ 301 assert(!pSection.hasRelocData() && "pSection already has relocation data."); 302 303 RelocData* reloc_data = RelocData::Create(pSection); 304 pSection.setRelocData(reloc_data); 305 return reloc_data; 306} 307 308/// CreateEhFrame - To create a eh_frame for given pSection 309EhFrame* IRBuilder::CreateEhFrame(LDSection& pSection) 310{ 311 assert(!pSection.hasEhFrame() && "pSection already has eh_frame."); 312 313 EhFrame* eh_frame = EhFrame::Create(pSection); 314 pSection.setEhFrame(eh_frame); 315 return eh_frame; 316} 317 318/// CreateBSS - To create a bss section for given pSection 319SectionData* IRBuilder::CreateBSS(LDSection& pSection) 320{ 321 assert(!pSection.hasSectionData() && "pSection already has section data."); 322 assert((pSection.kind() == LDFileFormat::BSS) && "pSection is not a BSS section."); 323 324 SectionData* sect_data = SectionData::Create(pSection); 325 pSection.setSectionData(sect_data); 326 327 /* value, valsize, size*/ 328 FillFragment* frag = new FillFragment(0x0, 1, pSection.size()); 329 330 ObjectBuilder::AppendFragment(*frag, *sect_data); 331 return sect_data; 332} 333 334/// CreateRegion - To create a region fragment in the input file. 335Fragment* IRBuilder::CreateRegion(Input& pInput, size_t pOffset, size_t pLength) 336{ 337 if (!pInput.hasMemArea()) { 338 fatal(diag::fatal_cannot_read_input) << pInput.path(); 339 return NULL; 340 } 341 342 if (0 == pLength) 343 return new FillFragment(0x0, 0, 0); 344 345 llvm::StringRef region = pInput.memArea()->request(pOffset, pLength); 346 return new RegionFragment(region); 347} 348 349/// CreateRegion - To create a region fragment wrapping the given memory 350Fragment* IRBuilder::CreateRegion(void* pMemory, size_t pLength) 351{ 352 if (0 == pLength) 353 return new FillFragment(0x0, 0, 0); 354 355 llvm::StringRef region(reinterpret_cast<const char*>(pMemory), pLength); 356 return new RegionFragment(region); 357} 358 359/// AppendFragment - To append pFrag to the given SectionData pSD 360uint64_t IRBuilder::AppendFragment(Fragment& pFrag, SectionData& pSD) 361{ 362 uint64_t size = ObjectBuilder::AppendFragment(pFrag, 363 pSD, 364 pSD.getSection().align()); 365 pSD.getSection().setSize(pSD.getSection().size() + size); 366 return size; 367} 368 369/// AppendRelocation - To append an relocation to the given RelocData pRD. 370void IRBuilder::AppendRelocation(Relocation& pRelocation, RelocData& pRD) 371{ 372 pRD.append(pRelocation); 373} 374 375/// AppendEhFrame - To append a fragment to EhFrame. 376uint64_t IRBuilder::AppendEhFrame(Fragment& pFrag, EhFrame& pEhFrame) 377{ 378 uint64_t size = ObjectBuilder::AppendFragment(pFrag, 379 *pEhFrame.getSectionData(), 380 pEhFrame.getSection().align()); 381 pEhFrame.getSection().setSize(pEhFrame.getSection().size() + size); 382 return size; 383} 384 385/// AppendEhFrame - To append a FDE to the given EhFrame pEhFram. 386uint64_t IRBuilder::AppendEhFrame(EhFrame::FDE& pFDE, EhFrame& pEhFrame) 387{ 388 pEhFrame.addFDE(pFDE); 389 pEhFrame.getSection().setSize(pEhFrame.getSection().size() + pFDE.size()); 390 return pFDE.size(); 391} 392 393/// AppendEhFrame - To append a CIE to the given EhFrame pEhFram. 394uint64_t IRBuilder::AppendEhFrame(EhFrame::CIE& pCIE, EhFrame& pEhFrame) 395{ 396 pEhFrame.addCIE(pCIE); 397 pEhFrame.getSection().setSize(pEhFrame.getSection().size() + pCIE.size()); 398 return pCIE.size(); 399} 400 401/// AddSymbol - To add a symbol in the input file and resolve the symbol 402/// immediately 403LDSymbol* IRBuilder::AddSymbol(Input& pInput, 404 const std::string& pName, 405 ResolveInfo::Type pType, 406 ResolveInfo::Desc pDesc, 407 ResolveInfo::Binding pBind, 408 ResolveInfo::SizeType pSize, 409 LDSymbol::ValueType pValue, 410 LDSection* pSection, 411 ResolveInfo::Visibility pVis) 412{ 413 // rename symbols 414 std::string name = pName; 415 if (!m_Module.getScript().renameMap().empty() && 416 ResolveInfo::Undefined == pDesc) { 417 // If the renameMap is not empty, some symbols should be renamed. 418 // --wrap and --portable defines the symbol rename map. 419 const LinkerScript& script = m_Module.getScript(); 420 LinkerScript::SymbolRenameMap::const_iterator renameSym = 421 script.renameMap().find(pName); 422 if (script.renameMap().end() != renameSym) 423 name = renameSym.getEntry()->value(); 424 } 425 426 switch (pInput.type()) { 427 case Input::Object: { 428 429 FragmentRef* frag = NULL; 430 if (NULL == pSection || 431 ResolveInfo::Undefined == pDesc || 432 ResolveInfo::Common == pDesc || 433 ResolveInfo::Absolute == pBind || 434 LDFileFormat::Ignore == pSection->kind() || 435 LDFileFormat::Group == pSection->kind()) 436 frag = FragmentRef::Null(); 437 else 438 frag = FragmentRef::Create(*pSection, pValue); 439 440 LDSymbol* input_sym = addSymbolFromObject(name, pType, pDesc, pBind, pSize, pValue, frag, pVis); 441 pInput.context()->addSymbol(input_sym); 442 return input_sym; 443 } 444 case Input::DynObj: { 445 return addSymbolFromDynObj(pInput, name, pType, pDesc, pBind, pSize, pValue, pVis); 446 } 447 default: { 448 return NULL; 449 break; 450 } 451 } 452 return NULL; 453} 454 455LDSymbol* IRBuilder::addSymbolFromObject(const std::string& pName, 456 ResolveInfo::Type pType, 457 ResolveInfo::Desc pDesc, 458 ResolveInfo::Binding pBinding, 459 ResolveInfo::SizeType pSize, 460 LDSymbol::ValueType pValue, 461 FragmentRef* pFragmentRef, 462 ResolveInfo::Visibility pVisibility) 463{ 464 // Step 1. calculate a Resolver::Result 465 // resolved_result is a triple <resolved_info, existent, override> 466 Resolver::Result resolved_result; 467 ResolveInfo old_info; // used for arrange output symbols 468 469 if (pBinding == ResolveInfo::Local) { 470 // if the symbol is a local symbol, create a LDSymbol for input, but do not 471 // resolve them. 472 resolved_result.info = m_Module.getNamePool().createSymbol(pName, 473 false, 474 pType, 475 pDesc, 476 pBinding, 477 pSize, 478 pVisibility); 479 480 // No matter if there is a symbol with the same name, insert the symbol 481 // into output symbol table. So, we let the existent false. 482 resolved_result.existent = false; 483 resolved_result.overriden = true; 484 } 485 else { 486 // if the symbol is not local, insert and resolve it immediately 487 m_Module.getNamePool().insertSymbol(pName, false, pType, pDesc, pBinding, 488 pSize, pValue, pVisibility, 489 &old_info, resolved_result); 490 } 491 492 // the return ResolveInfo should not NULL 493 assert(NULL != resolved_result.info); 494 495 /// Step 2. create an input LDSymbol. 496 // create a LDSymbol for the input file. 497 LDSymbol* input_sym = LDSymbol::Create(*resolved_result.info); 498 input_sym->setFragmentRef(pFragmentRef); 499 input_sym->setValue(pValue); 500 501 // Step 3. Set up corresponding output LDSymbol 502 LDSymbol* output_sym = resolved_result.info->outSymbol(); 503 bool has_output_sym = (NULL != output_sym); 504 if (!resolved_result.existent || !has_output_sym) { 505 // it is a new symbol, the output_sym should be NULL. 506 assert(NULL == output_sym); 507 508 if (pType == ResolveInfo::Section) { 509 // if it is a section symbol, its output LDSymbol is the input LDSymbol. 510 output_sym = input_sym; 511 } 512 else { 513 // if it is a new symbol, create a LDSymbol for the output 514 output_sym = LDSymbol::Create(*resolved_result.info); 515 } 516 resolved_result.info->setSymPtr(output_sym); 517 } 518 519 if (resolved_result.overriden || !has_output_sym) { 520 // symbol can be overriden only if it exists. 521 assert(output_sym != NULL); 522 523 // should override output LDSymbol 524 output_sym->setFragmentRef(pFragmentRef); 525 output_sym->setValue(pValue); 526 } 527 return input_sym; 528} 529 530LDSymbol* IRBuilder::addSymbolFromDynObj(Input& pInput, 531 const std::string& pName, 532 ResolveInfo::Type pType, 533 ResolveInfo::Desc pDesc, 534 ResolveInfo::Binding pBinding, 535 ResolveInfo::SizeType pSize, 536 LDSymbol::ValueType pValue, 537 ResolveInfo::Visibility pVisibility) 538{ 539 // We don't need sections of dynamic objects. So we ignore section symbols. 540 if (pType == ResolveInfo::Section) 541 return NULL; 542 543 // ignore symbols with local binding or that have internal or hidden 544 // visibility 545 if (pBinding == ResolveInfo::Local || 546 pVisibility == ResolveInfo::Internal || 547 pVisibility == ResolveInfo::Hidden) 548 return NULL; 549 550 // A protected symbol in a shared library must be treated as a 551 // normal symbol when viewed from outside the shared library. 552 if (pVisibility == ResolveInfo::Protected) 553 pVisibility = ResolveInfo::Default; 554 555 // insert symbol and resolve it immediately 556 // resolved_result is a triple <resolved_info, existent, override> 557 Resolver::Result resolved_result; 558 m_Module.getNamePool().insertSymbol(pName, true, pType, pDesc, 559 pBinding, pSize, pValue, pVisibility, 560 NULL, resolved_result); 561 562 // the return ResolveInfo should not NULL 563 assert(NULL != resolved_result.info); 564 565 if (resolved_result.overriden || !resolved_result.existent) 566 pInput.setNeeded(); 567 568 // create a LDSymbol for the input file. 569 LDSymbol* input_sym = LDSymbol::Create(*resolved_result.info); 570 input_sym->setFragmentRef(FragmentRef::Null()); 571 input_sym->setValue(pValue); 572 573 // this symbol is seen in a dynamic object, set the InDyn flag 574 resolved_result.info->setInDyn(); 575 576 if (!resolved_result.existent) { 577 // we get a new symbol, leave it as NULL 578 resolved_result.info->setSymPtr(NULL); 579 } 580 return input_sym; 581} 582 583/// AddRelocation - add a relocation entry 584/// 585/// All symbols should be read and resolved before calling this function. 586Relocation* IRBuilder::AddRelocation(LDSection& pSection, 587 Relocation::Type pType, 588 LDSymbol& pSym, 589 uint32_t pOffset, 590 Relocation::Address pAddend) 591{ 592 FragmentRef* frag_ref = FragmentRef::Create(*pSection.getLink(), pOffset); 593 594 Relocation* relocation = Relocation::Create(pType, *frag_ref, pAddend); 595 596 relocation->setSymInfo(pSym.resolveInfo()); 597 pSection.getRelocData()->append(*relocation); 598 599 return relocation; 600} 601 602/// AddSymbol - define an output symbol and override it immediately 603template<> LDSymbol* 604IRBuilder::AddSymbol<IRBuilder::Force, IRBuilder::Unresolve>( 605 const llvm::StringRef& pName, 606 ResolveInfo::Type pType, 607 ResolveInfo::Desc pDesc, 608 ResolveInfo::Binding pBinding, 609 ResolveInfo::SizeType pSize, 610 LDSymbol::ValueType pValue, 611 FragmentRef* pFragmentRef, 612 ResolveInfo::Visibility pVisibility) 613{ 614 ResolveInfo* info = m_Module.getNamePool().findInfo(pName); 615 LDSymbol* output_sym = NULL; 616 if (NULL == info) { 617 // the symbol is not in the pool, create a new one. 618 // create a ResolveInfo 619 Resolver::Result result; 620 m_Module.getNamePool().insertSymbol(pName, false, pType, pDesc, 621 pBinding, pSize, pValue, pVisibility, 622 NULL, result); 623 assert(!result.existent); 624 625 // create a output LDSymbol 626 output_sym = LDSymbol::Create(*result.info); 627 result.info->setSymPtr(output_sym); 628 629 if (result.info->shouldForceLocal(m_Config)) 630 m_Module.getSymbolTable().forceLocal(*output_sym); 631 else 632 m_Module.getSymbolTable().add(*output_sym); 633 } 634 else { 635 // the symbol is already in the pool, override it 636 ResolveInfo old_info; 637 old_info.override(*info); 638 639 info->setRegular(); 640 info->setType(pType); 641 info->setDesc(pDesc); 642 info->setBinding(pBinding); 643 info->setVisibility(pVisibility); 644 info->setIsSymbol(true); 645 info->setSize(pSize); 646 647 output_sym = info->outSymbol(); 648 if (NULL != output_sym) 649 m_Module.getSymbolTable().arrange(*output_sym, old_info); 650 else { 651 // create a output LDSymbol 652 output_sym = LDSymbol::Create(*info); 653 info->setSymPtr(output_sym); 654 655 m_Module.getSymbolTable().add(*output_sym); 656 } 657 } 658 659 if (NULL != output_sym) { 660 output_sym->setFragmentRef(pFragmentRef); 661 output_sym->setValue(pValue); 662 } 663 664 return output_sym; 665} 666 667/// AddSymbol - define an output symbol and override it immediately 668template<> LDSymbol* 669IRBuilder::AddSymbol<IRBuilder::AsReferred, IRBuilder::Unresolve>( 670 const llvm::StringRef& pName, 671 ResolveInfo::Type pType, 672 ResolveInfo::Desc pDesc, 673 ResolveInfo::Binding pBinding, 674 ResolveInfo::SizeType pSize, 675 LDSymbol::ValueType pValue, 676 FragmentRef* pFragmentRef, 677 ResolveInfo::Visibility pVisibility) 678{ 679 ResolveInfo* info = m_Module.getNamePool().findInfo(pName); 680 681 if (NULL == info || !(info->isUndef() || info->isDyn())) { 682 // only undefined symbol and dynamic symbol can make a reference. 683 return NULL; 684 } 685 686 // the symbol is already in the pool, override it 687 ResolveInfo old_info; 688 old_info.override(*info); 689 690 info->setRegular(); 691 info->setType(pType); 692 info->setDesc(pDesc); 693 info->setBinding(pBinding); 694 info->setVisibility(pVisibility); 695 info->setIsSymbol(true); 696 info->setSize(pSize); 697 698 LDSymbol* output_sym = info->outSymbol(); 699 if (NULL != output_sym) { 700 output_sym->setFragmentRef(pFragmentRef); 701 output_sym->setValue(pValue); 702 m_Module.getSymbolTable().arrange(*output_sym, old_info); 703 } 704 else { 705 // create a output LDSymbol 706 output_sym = LDSymbol::Create(*info); 707 info->setSymPtr(output_sym); 708 709 m_Module.getSymbolTable().add(*output_sym); 710 } 711 712 return output_sym; 713} 714 715/// AddSymbol - define an output symbol and resolve it 716/// immediately 717template<> LDSymbol* 718IRBuilder::AddSymbol<IRBuilder::Force, IRBuilder::Resolve>( 719 const llvm::StringRef& pName, 720 ResolveInfo::Type pType, 721 ResolveInfo::Desc pDesc, 722 ResolveInfo::Binding pBinding, 723 ResolveInfo::SizeType pSize, 724 LDSymbol::ValueType pValue, 725 FragmentRef* pFragmentRef, 726 ResolveInfo::Visibility pVisibility) 727{ 728 // Result is <info, existent, override> 729 Resolver::Result result; 730 ResolveInfo old_info; 731 m_Module.getNamePool().insertSymbol(pName, false, pType, pDesc, pBinding, 732 pSize, pValue, pVisibility, 733 &old_info, result); 734 735 LDSymbol* output_sym = result.info->outSymbol(); 736 bool has_output_sym = (NULL != output_sym); 737 738 if (!result.existent || !has_output_sym) { 739 output_sym = LDSymbol::Create(*result.info); 740 result.info->setSymPtr(output_sym); 741 } 742 743 if (result.overriden || !has_output_sym) { 744 output_sym->setFragmentRef(pFragmentRef); 745 output_sym->setValue(pValue); 746 } 747 748 // After symbol resolution, the visibility is changed to the most restrict. 749 // arrange the output position 750 if (result.info->shouldForceLocal(m_Config)) 751 m_Module.getSymbolTable().forceLocal(*output_sym); 752 else if (has_output_sym) 753 m_Module.getSymbolTable().arrange(*output_sym, old_info); 754 else 755 m_Module.getSymbolTable().add(*output_sym); 756 757 return output_sym; 758} 759 760/// defineSymbol - define an output symbol and resolve it immediately. 761template<> LDSymbol* 762IRBuilder::AddSymbol<IRBuilder::AsReferred, IRBuilder::Resolve>( 763 const llvm::StringRef& pName, 764 ResolveInfo::Type pType, 765 ResolveInfo::Desc pDesc, 766 ResolveInfo::Binding pBinding, 767 ResolveInfo::SizeType pSize, 768 LDSymbol::ValueType pValue, 769 FragmentRef* pFragmentRef, 770 ResolveInfo::Visibility pVisibility) 771{ 772 ResolveInfo* info = m_Module.getNamePool().findInfo(pName); 773 774 if (NULL == info || !(info->isUndef() || info->isDyn())) { 775 // only undefined symbol and dynamic symbol can make a reference. 776 return NULL; 777 } 778 779 return AddSymbol<Force, Resolve>(pName, 780 pType, 781 pDesc, 782 pBinding, 783 pSize, 784 pValue, 785 pFragmentRef, 786 pVisibility); 787} 788 789