Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 | [diff] [blame] | 1 | //===- Symbols.cpp --------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Linker |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "Symbols.h" |
Rafael Espindola | 9d06ab6 | 2015-09-22 00:01:39 | [diff] [blame] | 11 | #include "InputSection.h" |
Rafael Espindola | 49a2ca6 | 2015-08-06 15:33:19 | [diff] [blame] | 12 | #include "Error.h" |
Michael J. Spencer | cdae0a4 | 2015-07-28 22:58:25 | [diff] [blame] | 13 | #include "InputFiles.h" |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 | [diff] [blame] | 14 | |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 | [diff] [blame] | 15 | #include "llvm/ADT/STLExtras.h" |
| 16 | |
| 17 | using namespace llvm; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 | [diff] [blame] | 18 | using namespace llvm::object; |
Rafael Espindola | 78471f0 | 2015-09-01 23:12:52 | [diff] [blame] | 19 | using namespace llvm::ELF; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 | [diff] [blame] | 20 | |
| 21 | using namespace lld; |
| 22 | using namespace lld::elf2; |
| 23 | |
Rafael Espindola | 78471f0 | 2015-09-01 23:12:52 | [diff] [blame] | 24 | static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) { |
| 25 | if (VA == STV_DEFAULT) |
| 26 | return VB; |
| 27 | if (VB == STV_DEFAULT) |
| 28 | return VA; |
| 29 | return std::min(VA, VB); |
| 30 | } |
| 31 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 | [diff] [blame] | 32 | // Returns 1, 0 or -1 if this symbol should take precedence |
| 33 | // over the Other, tie or lose, respectively. |
Rafael Espindola | daa92a6 | 2015-08-31 01:16:19 | [diff] [blame] | 34 | template <class ELFT> int SymbolBody::compare(SymbolBody *Other) { |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 | [diff] [blame] | 35 | assert(!isLazy() && !Other->isLazy()); |
Rafael Espindola | 30e1797 | 2015-08-30 23:17:30 | [diff] [blame] | 36 | std::pair<bool, bool> L(isDefined(), !isWeak()); |
| 37 | std::pair<bool, bool> R(Other->isDefined(), !Other->isWeak()); |
Rui Ueyama | a7ccb29 | 2015-07-27 20:39:01 | [diff] [blame] | 38 | |
Rafael Espindola | 3a63f3f | 2015-08-28 20:19:34 | [diff] [blame] | 39 | // Normalize |
| 40 | if (L > R) |
Rafael Espindola | daa92a6 | 2015-08-31 01:16:19 | [diff] [blame] | 41 | return -Other->compare<ELFT>(this); |
Rui Ueyama | a7ccb29 | 2015-07-27 20:39:01 | [diff] [blame] | 42 | |
Rafael Espindola | 78471f0 | 2015-09-01 23:12:52 | [diff] [blame] | 43 | uint8_t LV = getMostConstrainingVisibility(); |
| 44 | uint8_t RV = Other->getMostConstrainingVisibility(); |
| 45 | MostConstrainingVisibility = getMinVisibility(LV, RV); |
| 46 | Other->MostConstrainingVisibility = MostConstrainingVisibility; |
| 47 | |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 | [diff] [blame] | 48 | IsUsedInRegularObj |= Other->IsUsedInRegularObj; |
| 49 | Other->IsUsedInRegularObj |= IsUsedInRegularObj; |
| 50 | |
Rafael Espindola | 3a63f3f | 2015-08-28 20:19:34 | [diff] [blame] | 51 | if (L != R) |
| 52 | return -1; |
Rui Ueyama | 7da94a5 | 2015-09-09 17:40:51 | [diff] [blame] | 53 | if (!L.first || !L.second) |
| 54 | return 1; |
Rafael Espindola | 8e5560d | 2015-09-23 14:23:59 | [diff] [blame^] | 55 | if (isShared()) |
| 56 | return -1; |
| 57 | if (Other->isShared()) |
| 58 | return 1; |
Rui Ueyama | 7da94a5 | 2015-09-09 17:40:51 | [diff] [blame] | 59 | if (isCommon()) { |
Rui Ueyama | 6666f6a | 2015-09-09 17:55:09 | [diff] [blame] | 60 | if (!Other->isCommon()) |
Rafael Espindola | 30e1797 | 2015-08-30 23:17:30 | [diff] [blame] | 61 | return -1; |
Rui Ueyama | 6666f6a | 2015-09-09 17:55:09 | [diff] [blame] | 62 | auto *ThisC = cast<DefinedCommon<ELFT>>(this); |
| 63 | auto *OtherC = cast<DefinedCommon<ELFT>>(Other); |
| 64 | typename DefinedCommon<ELFT>::uintX_t MaxAlign = |
| 65 | std::max(ThisC->MaxAlignment, OtherC->MaxAlignment); |
| 66 | if (ThisC->Sym.st_size >= OtherC->Sym.st_size) { |
| 67 | ThisC->MaxAlignment = MaxAlign; |
| 68 | return 1; |
Rafael Espindola | daa92a6 | 2015-08-31 01:16:19 | [diff] [blame] | 69 | } |
Rui Ueyama | 6666f6a | 2015-09-09 17:55:09 | [diff] [blame] | 70 | OtherC->MaxAlignment = MaxAlign; |
Rui Ueyama | 7da94a5 | 2015-09-09 17:40:51 | [diff] [blame] | 71 | return -1; |
Rafael Espindola | 30e1797 | 2015-08-30 23:17:30 | [diff] [blame] | 72 | } |
Rui Ueyama | 7da94a5 | 2015-09-09 17:40:51 | [diff] [blame] | 73 | if (Other->isCommon()) |
| 74 | return 1; |
| 75 | return 0; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 | [diff] [blame] | 76 | } |
Rafael Espindola | daa92a6 | 2015-08-31 01:16:19 | [diff] [blame] | 77 | |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 | [diff] [blame] | 78 | std::unique_ptr<InputFile> Lazy::getMember() { |
| 79 | MemoryBufferRef MBRef = File->getMember(&Sym); |
| 80 | |
| 81 | // getMember returns an empty buffer if the member was already |
| 82 | // read from the library. |
| 83 | if (MBRef.getBuffer().empty()) |
| 84 | return std::unique_ptr<InputFile>(nullptr); |
| 85 | |
| 86 | return createELFFile<ObjectFile>(MBRef); |
| 87 | } |
| 88 | |
Rafael Espindola | daa92a6 | 2015-08-31 01:16:19 | [diff] [blame] | 89 | template int SymbolBody::compare<ELF32LE>(SymbolBody *Other); |
| 90 | template int SymbolBody::compare<ELF32BE>(SymbolBody *Other); |
| 91 | template int SymbolBody::compare<ELF64LE>(SymbolBody *Other); |
| 92 | template int SymbolBody::compare<ELF64BE>(SymbolBody *Other); |