blob: f2b484d7d06d540c88e72901d9baa45bbc27c8a3 [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
15using namespace llvm::object;
16
17using namespace lld;
18using namespace lld::elf2;
19
Michael J. Spencer84487f12015-07-24 21:03:0720// Returns 1, 0 or -1 if this symbol should take precedence
21// over the Other, tie or lose, respectively.
Rafael Espindoladaa92a62015-08-31 01:16:1922template <class ELFT> int SymbolBody::compare(SymbolBody *Other) {
Rafael Espindola30e17972015-08-30 23:17:3023 std::pair<bool, bool> L(isDefined(), !isWeak());
24 std::pair<bool, bool> R(Other->isDefined(), !Other->isWeak());
Rui Ueyamaa7ccb292015-07-27 20:39:0125
Rafael Espindola3a63f3f2015-08-28 20:19:3426 // Normalize
27 if (L > R)
Rafael Espindoladaa92a62015-08-31 01:16:1928 return -Other->compare<ELFT>(this);
Rui Ueyamaa7ccb292015-07-27 20:39:0129
Rafael Espindola3a63f3f2015-08-28 20:19:3430 if (L != R)
31 return -1;
Michael J. Spencer84487f12015-07-24 21:03:0732
Rafael Espindola30e17972015-08-30 23:17:3033 if (L.first && L.second) {
Rafael Espindoladaa92a62015-08-31 01:16:1934 if (isCommon()) {
35 if (Other->isCommon()) {
Rafael Espindolaf31f9612015-09-01 01:19:1236 auto *ThisC = cast<DefinedCommon<ELFT>>(this);
37 auto *OtherC = cast<DefinedCommon<ELFT>>(Other);
38 typename DefinedCommon<ELFT>::uintX_t MaxAlign =
39 std::max(ThisC->MaxAlignment, OtherC->MaxAlignment);
40 if (ThisC->Sym.st_size >= OtherC->Sym.st_size) {
41 ThisC->MaxAlignment = MaxAlign;
Rafael Espindoladaa92a62015-08-31 01:16:1942 return 1;
Rafael Espindolaf31f9612015-09-01 01:19:1243 }
44 OtherC->MaxAlignment = MaxAlign;
Rafael Espindoladaa92a62015-08-31 01:16:1945 return -1;
46 }
Rafael Espindola30e17972015-08-30 23:17:3047 return -1;
Rafael Espindoladaa92a62015-08-31 01:16:1948 }
Rafael Espindola30e17972015-08-30 23:17:3049 if (Other->isCommon())
50 return 1;
Michael J. Spencer84487f12015-07-24 21:03:0751 return 0;
Rafael Espindola30e17972015-08-30 23:17:3052 }
Rafael Espindola3a63f3f2015-08-28 20:19:3453 return 1;
Michael J. Spencer84487f12015-07-24 21:03:0754}
Rafael Espindoladaa92a62015-08-31 01:16:1955
56template int SymbolBody::compare<ELF32LE>(SymbolBody *Other);
57template int SymbolBody::compare<ELF32BE>(SymbolBody *Other);
58template int SymbolBody::compare<ELF64LE>(SymbolBody *Other);
59template int SymbolBody::compare<ELF64BE>(SymbolBody *Other);