blob: b81ed0a83a17ae99542657a0f703e41d0d7b709e [file] [log] [blame]
Michael J. Spencer84487f12015-07-24 21:03:071//===- 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 Espindola9d06ab62015-09-22 00:01:3911#include "InputSection.h"
Rafael Espindola49a2ca62015-08-06 15:33:1912#include "Error.h"
Michael J. Spencercdae0a42015-07-28 22:58:2513#include "InputFiles.h"
Michael J. Spencer84487f12015-07-24 21:03:0714
Michael J. Spencer1b348a62015-09-04 22:28:1015#include "llvm/ADT/STLExtras.h"
16
17using namespace llvm;
Michael J. Spencer84487f12015-07-24 21:03:0718using namespace llvm::object;
Rafael Espindola78471f02015-09-01 23:12:5219using namespace llvm::ELF;
Michael J. Spencer84487f12015-07-24 21:03:0720
21using namespace lld;
22using namespace lld::elf2;
23
Rafael Espindola78471f02015-09-01 23:12:5224static 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. Spencer84487f12015-07-24 21:03:0732// Returns 1, 0 or -1 if this symbol should take precedence
33// over the Other, tie or lose, respectively.
Rafael Espindoladaa92a62015-08-31 01:16:1934template <class ELFT> int SymbolBody::compare(SymbolBody *Other) {
Michael J. Spencer1b348a62015-09-04 22:28:1035 assert(!isLazy() && !Other->isLazy());
Rafael Espindola30e17972015-08-30 23:17:3036 std::pair<bool, bool> L(isDefined(), !isWeak());
37 std::pair<bool, bool> R(Other->isDefined(), !Other->isWeak());
Rui Ueyamaa7ccb292015-07-27 20:39:0138
Rafael Espindola3a63f3f2015-08-28 20:19:3439 // Normalize
40 if (L > R)
Rafael Espindoladaa92a62015-08-31 01:16:1941 return -Other->compare<ELFT>(this);
Rui Ueyamaa7ccb292015-07-27 20:39:0142
Rafael Espindola78471f02015-09-01 23:12:5243 uint8_t LV = getMostConstrainingVisibility();
44 uint8_t RV = Other->getMostConstrainingVisibility();
45 MostConstrainingVisibility = getMinVisibility(LV, RV);
46 Other->MostConstrainingVisibility = MostConstrainingVisibility;
47
Rafael Espindola18173d42015-09-08 15:50:0548 IsUsedInRegularObj |= Other->IsUsedInRegularObj;
49 Other->IsUsedInRegularObj |= IsUsedInRegularObj;
50
Rafael Espindola3a63f3f2015-08-28 20:19:3451 if (L != R)
52 return -1;
Rui Ueyama7da94a52015-09-09 17:40:5153 if (!L.first || !L.second)
54 return 1;
Rafael Espindola8e5560d2015-09-23 14:23:5955 if (isShared())
56 return -1;
57 if (Other->isShared())
58 return 1;
Rui Ueyama7da94a52015-09-09 17:40:5159 if (isCommon()) {
Rui Ueyama6666f6a2015-09-09 17:55:0960 if (!Other->isCommon())
Rafael Espindola30e17972015-08-30 23:17:3061 return -1;
Rui Ueyama6666f6a2015-09-09 17:55:0962 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 Espindoladaa92a62015-08-31 01:16:1969 }
Rui Ueyama6666f6a2015-09-09 17:55:0970 OtherC->MaxAlignment = MaxAlign;
Rui Ueyama7da94a52015-09-09 17:40:5171 return -1;
Rafael Espindola30e17972015-08-30 23:17:3072 }
Rui Ueyama7da94a52015-09-09 17:40:5173 if (Other->isCommon())
74 return 1;
75 return 0;
Michael J. Spencer84487f12015-07-24 21:03:0776}
Rafael Espindoladaa92a62015-08-31 01:16:1977
Michael J. Spencer1b348a62015-09-04 22:28:1078std::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 Espindoladaa92a62015-08-31 01:16:1989template int SymbolBody::compare<ELF32LE>(SymbolBody *Other);
90template int SymbolBody::compare<ELF32BE>(SymbolBody *Other);
91template int SymbolBody::compare<ELF64LE>(SymbolBody *Other);
92template int SymbolBody::compare<ELF64BE>(SymbolBody *Other);