blob: b58d9e91bc20af11590ef2c08c65bffe8f13d439 [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"
11#include "Chunks.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 Espindola3a63f3f2015-08-28 20:19:3448 if (L != R)
49 return -1;
Michael J. Spencer84487f12015-07-24 21:03:0750
Rafael Espindola30e17972015-08-30 23:17:3051 if (L.first && L.second) {
Rafael Espindoladaa92a62015-08-31 01:16:1952 if (isCommon()) {
53 if (Other->isCommon()) {
Rafael Espindolaf31f9612015-09-01 01:19:1254 auto *ThisC = cast<DefinedCommon<ELFT>>(this);
55 auto *OtherC = cast<DefinedCommon<ELFT>>(Other);
56 typename DefinedCommon<ELFT>::uintX_t MaxAlign =
57 std::max(ThisC->MaxAlignment, OtherC->MaxAlignment);
58 if (ThisC->Sym.st_size >= OtherC->Sym.st_size) {
59 ThisC->MaxAlignment = MaxAlign;
Rafael Espindoladaa92a62015-08-31 01:16:1960 return 1;
Rafael Espindolaf31f9612015-09-01 01:19:1261 }
62 OtherC->MaxAlignment = MaxAlign;
Rafael Espindoladaa92a62015-08-31 01:16:1963 return -1;
64 }
Rafael Espindola30e17972015-08-30 23:17:3065 return -1;
Rafael Espindoladaa92a62015-08-31 01:16:1966 }
Rafael Espindola30e17972015-08-30 23:17:3067 if (Other->isCommon())
68 return 1;
Michael J. Spencer84487f12015-07-24 21:03:0769 return 0;
Rafael Espindola30e17972015-08-30 23:17:3070 }
Rafael Espindola3a63f3f2015-08-28 20:19:3471 return 1;
Michael J. Spencer84487f12015-07-24 21:03:0772}
Rafael Espindoladaa92a62015-08-31 01:16:1973
Michael J. Spencer1b348a62015-09-04 22:28:1074std::unique_ptr<InputFile> Lazy::getMember() {
75 MemoryBufferRef MBRef = File->getMember(&Sym);
76
77 // getMember returns an empty buffer if the member was already
78 // read from the library.
79 if (MBRef.getBuffer().empty())
80 return std::unique_ptr<InputFile>(nullptr);
81
82 return createELFFile<ObjectFile>(MBRef);
83}
84
Rafael Espindoladaa92a62015-08-31 01:16:1985template int SymbolBody::compare<ELF32LE>(SymbolBody *Other);
86template int SymbolBody::compare<ELF32BE>(SymbolBody *Other);
87template int SymbolBody::compare<ELF64LE>(SymbolBody *Other);
88template int SymbolBody::compare<ELF64BE>(SymbolBody *Other);