blob: 35bc4998e56619cbb7a5b89122d8305a4cdde6ab [file] [log] [blame]
Michael J. Spencer84487f12015-07-24 21:03:07 +00001//===- 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"
Michael J. Spencercdae0a42015-07-28 22:58:25 +000012#include "InputFiles.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000013
14using namespace llvm::object;
15
16using namespace lld;
17using namespace lld::elf2;
18
Michael J. Spencercdae0a42015-07-28 22:58:25 +000019template <class ELFT>
20StringRef
21getSymbolName(const llvm::object::ELFFile<ELFT> *F,
22 const typename llvm::object::ELFFile<ELFT>::Elf_Sym *S) {
23 ErrorOr<StringRef> StrTab = F->getStringTableForSymtab(*F->getDotSymtabSec());
24 if (!StrTab || S->st_name >= StrTab->size())
25 llvm::report_fatal_error("Invalid string table.");
26 return StrTab->data() + S->st_name;
27}
28
29template <class ELFT>
30DefinedRegular<ELFT>::DefinedRegular(ObjectFile<ELFT> *F, const Elf_Sym *S)
31 : Defined(DefinedRegularKind, getSymbolName<ELFT>(F->getObj(), S)), File(F),
32 Sym(S) {
33 IsExternal = S->isExternal();
34}
35
Michael J. Spencer84487f12015-07-24 21:03:07 +000036// Returns 1, 0 or -1 if this symbol should take precedence
37// over the Other, tie or lose, respectively.
Rui Ueyamaa7ccb292015-07-27 20:39:01 +000038int SymbolBody::compare(SymbolBody *Other) {
39 Kind LK = kind();
40 Kind RK = Other->kind();
41
42 // Normalize so that the smaller kind is on the left.
43 if (LK > RK)
Michael J. Spencer84487f12015-07-24 21:03:07 +000044 return -Other->compare(this);
Rui Ueyamaa7ccb292015-07-27 20:39:01 +000045
46 // First handle comparisons between two different kinds.
47 if (LK != RK) {
48 assert(LK == DefinedRegularKind);
49 assert(RK == UndefinedKind);
Michael J. Spencer84487f12015-07-24 21:03:07 +000050 return 1;
Rui Ueyamaa7ccb292015-07-27 20:39:01 +000051 }
Michael J. Spencer84487f12015-07-24 21:03:07 +000052
Rui Ueyamaa7ccb292015-07-27 20:39:01 +000053 // Now handle the case where the kinds are the same.
54 switch (LK) {
55 case DefinedRegularKind:
Michael J. Spencer84487f12015-07-24 21:03:07 +000056 return 0;
Rui Ueyamaa7ccb292015-07-27 20:39:01 +000057 case UndefinedKind:
58 return 1;
59 default:
60 llvm_unreachable("unknown symbol kind");
61 }
Michael J. Spencer84487f12015-07-24 21:03:07 +000062}
63
Michael J. Spencer84487f12015-07-24 21:03:07 +000064namespace lld {
65namespace elf2 {
66template class DefinedRegular<llvm::object::ELF32LE>;
67template class DefinedRegular<llvm::object::ELF32BE>;
68template class DefinedRegular<llvm::object::ELF64LE>;
69template class DefinedRegular<llvm::object::ELF64BE>;
70}
71}