| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 1 | //===- InputFiles.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 "InputFiles.h" |
| Rafael Espindola | 156f4ee | 2016-04-28 19:30:41 +0000 | [diff] [blame] | 11 | #include "Driver.h" |
| Rafael Espindola | 192e1fa | 2015-08-06 15:08:23 +0000 | [diff] [blame] | 12 | #include "Error.h" |
| Rafael Espindola | 9d13d04 | 2016-02-11 15:24:48 +0000 | [diff] [blame] | 13 | #include "InputSection.h" |
| Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 14 | #include "SymbolTable.h" |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 15 | #include "Symbols.h" |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
| Rafael Espindola | 4d480ed | 2016-04-21 21:44:25 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/Analysis.h" |
| Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 18 | #include "llvm/IR/LLVMContext.h" |
| Rafael Espindola | 4de44b7 | 2016-03-02 15:43:50 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Module.h" |
| Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 21 | |
| Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 23 | using namespace llvm::ELF; |
| Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 24 | using namespace llvm::object; |
| Rui Ueyama | f5c4aca | 2015-09-30 17:06:09 +0000 | [diff] [blame] | 25 | using namespace llvm::sys::fs; |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace lld; |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 28 | using namespace lld::elf; |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 29 | |
| Rafael Espindola | 78db5a9 | 2016-05-09 21:40:06 +0000 | [diff] [blame^] | 30 | // Returns "(internal)", "foo.a(bar.o)" or "baz.o". |
| 31 | std::string elf::getFilename(InputFile *F) { |
| 32 | if (!F) |
| 33 | return "(internal)"; |
| 34 | if (!F->ArchiveName.empty()) |
| 35 | return (F->ArchiveName + "(" + F->getName() + ")").str(); |
| 36 | return F->getName(); |
| 37 | } |
| 38 | |
| Rui Ueyama | eb3413e | 2016-03-03 06:22:29 +0000 | [diff] [blame] | 39 | template <class ELFT> |
| 40 | static ELFFile<ELFT> createELFObj(MemoryBufferRef MB) { |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 41 | std::error_code EC; |
| Rui Ueyama | eb3413e | 2016-03-03 06:22:29 +0000 | [diff] [blame] | 42 | ELFFile<ELFT> F(MB.getBuffer(), EC); |
| Rafael Espindola | 75714f6 | 2016-03-03 22:24:39 +0000 | [diff] [blame] | 43 | check(EC); |
| Rui Ueyama | eb3413e | 2016-03-03 06:22:29 +0000 | [diff] [blame] | 44 | return F; |
| Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 47 | template <class ELFT> |
| Rui Ueyama | eb3413e | 2016-03-03 06:22:29 +0000 | [diff] [blame] | 48 | ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB) |
| 49 | : InputFile(K, MB), ELFObj(createELFObj<ELFT>(MB)) {} |
| Rafael Espindola | e1901cc | 2015-09-24 15:11:50 +0000 | [diff] [blame] | 50 | |
| 51 | template <class ELFT> |
| Rui Ueyama | 2022e81 | 2015-11-20 02:10:52 +0000 | [diff] [blame] | 52 | ELFKind ELFFileBase<ELFT>::getELFKind() { |
| Rui Ueyama | f588ac4 | 2016-01-06 00:09:41 +0000 | [diff] [blame] | 53 | if (ELFT::TargetEndianness == support::little) |
| 54 | return ELFT::Is64Bits ? ELF64LEKind : ELF32LEKind; |
| 55 | return ELFT::Is64Bits ? ELF64BEKind : ELF32BEKind; |
| Rui Ueyama | 2022e81 | 2015-11-20 02:10:52 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | template <class ELFT> |
| Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 59 | typename ELFT::SymRange ELFFileBase<ELFT>::getElfSymbols(bool OnlyGlobals) { |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 60 | if (!Symtab) |
| 61 | return Elf_Sym_Range(nullptr, nullptr); |
| Rafael Espindola | e1901cc | 2015-09-24 15:11:50 +0000 | [diff] [blame] | 62 | Elf_Sym_Range Syms = ELFObj.symbols(Symtab); |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 63 | uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end()); |
| 64 | uint32_t FirstNonLocal = Symtab->sh_info; |
| 65 | if (FirstNonLocal > NumSymbols) |
| George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 66 | fatal("invalid sh_info in symbol table"); |
| Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 67 | |
| 68 | if (OnlyGlobals) |
| Rafael Espindola | 0f7ccc3 | 2016-04-05 14:47:28 +0000 | [diff] [blame] | 69 | return makeArrayRef(Syms.begin() + FirstNonLocal, Syms.end()); |
| 70 | return makeArrayRef(Syms.begin(), Syms.end()); |
| Davide Italiano | 6d328d3 | 2015-09-16 20:45:57 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 73 | template <class ELFT> |
| 74 | uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const { |
| Rui Ueyama | dc8d3a2 | 2015-12-24 08:36:56 +0000 | [diff] [blame] | 75 | uint32_t I = Sym.st_shndx; |
| 76 | if (I == ELF::SHN_XINDEX) |
| Rui Ueyama | e69ab10 | 2016-01-06 01:14:11 +0000 | [diff] [blame] | 77 | return ELFObj.getExtendedSymbolTableIndex(&Sym, Symtab, SymtabSHNDX); |
| Rafael Espindola | 972b236 | 2016-03-09 14:31:18 +0000 | [diff] [blame] | 78 | if (I >= ELF::SHN_LORESERVE) |
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 79 | return 0; |
| Rui Ueyama | dc8d3a2 | 2015-12-24 08:36:56 +0000 | [diff] [blame] | 80 | return I; |
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| Rafael Espindola | af70764 | 2015-10-12 01:55:32 +0000 | [diff] [blame] | 83 | template <class ELFT> void ELFFileBase<ELFT>::initStringTable() { |
| Rafael Espindola | 3e60379 | 2015-10-01 20:26:37 +0000 | [diff] [blame] | 84 | if (!Symtab) |
| 85 | return; |
| Rafael Espindola | 75714f6 | 2016-03-03 22:24:39 +0000 | [diff] [blame] | 86 | StringTable = check(ELFObj.getStringTableForSymtab(*Symtab)); |
| Rafael Espindola | 6a3b5de | 2015-10-01 19:52:48 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | template <class ELFT> |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 90 | elf::ObjectFile<ELFT>::ObjectFile(MemoryBufferRef M) |
| Rafael Espindola | 2a4b271 | 2015-10-13 01:17:02 +0000 | [diff] [blame] | 91 | : ELFFileBase<ELFT>(Base::ObjectKind, M) {} |
| Rafael Espindola | e1901cc | 2015-09-24 15:11:50 +0000 | [diff] [blame] | 92 | |
| 93 | template <class ELFT> |
| Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 94 | ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getNonLocalSymbols() { |
| 95 | if (!this->Symtab) |
| 96 | return this->SymbolBodies; |
| 97 | uint32_t FirstNonLocal = this->Symtab->sh_info; |
| 98 | return makeArrayRef(this->SymbolBodies).slice(FirstNonLocal); |
| 99 | } |
| 100 | |
| 101 | template <class ELFT> |
| 102 | ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getLocalSymbols() { |
| 103 | if (!this->Symtab) |
| 104 | return this->SymbolBodies; |
| 105 | uint32_t FirstNonLocal = this->Symtab->sh_info; |
| 106 | return makeArrayRef(this->SymbolBodies).slice(1, FirstNonLocal - 1); |
| 107 | } |
| 108 | |
| 109 | template <class ELFT> |
| 110 | ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getSymbols() { |
| 111 | if (!this->Symtab) |
| 112 | return this->SymbolBodies; |
| 113 | return makeArrayRef(this->SymbolBodies).slice(1); |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 116 | template <class ELFT> uint32_t elf::ObjectFile<ELFT>::getMipsGp0() const { |
| Simon Atanasyan | add74f3 | 2016-05-04 10:07:38 +0000 | [diff] [blame] | 117 | if (ELFT::Is64Bits && MipsOptions && MipsOptions->Reginfo) |
| 118 | return MipsOptions->Reginfo->ri_gp_value; |
| 119 | if (!ELFT::Is64Bits && MipsReginfo && MipsReginfo->Reginfo) |
| Rui Ueyama | 70eed36 | 2016-01-06 22:42:43 +0000 | [diff] [blame] | 120 | return MipsReginfo->Reginfo->ri_gp_value; |
| 121 | return 0; |
| Simon Atanasyan | 57830b6 | 2015-12-25 13:02:13 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 124 | template <class ELFT> |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 125 | void elf::ObjectFile<ELFT>::parse(DenseSet<StringRef> &ComdatGroups) { |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 126 | // Read section and symbol tables. |
| Rui Ueyama | 52d3b67 | 2016-01-06 02:06:33 +0000 | [diff] [blame] | 127 | initializeSections(ComdatGroups); |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 128 | initializeSymbols(); |
| 129 | } |
| 130 | |
| Rui Ueyama | 3f11c8c | 2015-12-24 08:41:12 +0000 | [diff] [blame] | 131 | // Sections with SHT_GROUP and comdat bits define comdat section groups. |
| 132 | // They are identified and deduplicated by group name. This function |
| 133 | // returns a group name. |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 134 | template <class ELFT> |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 135 | StringRef elf::ObjectFile<ELFT>::getShtGroupSignature(const Elf_Shdr &Sec) { |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 136 | const ELFFile<ELFT> &Obj = this->ELFObj; |
| 137 | uint32_t SymtabdSectionIndex = Sec.sh_link; |
| Rafael Espindola | 75714f6 | 2016-03-03 22:24:39 +0000 | [diff] [blame] | 138 | const Elf_Shdr *SymtabSec = check(Obj.getSection(SymtabdSectionIndex)); |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 139 | uint32_t SymIndex = Sec.sh_info; |
| 140 | const Elf_Sym *Sym = Obj.getSymbol(SymtabSec, SymIndex); |
| Rafael Espindola | 75714f6 | 2016-03-03 22:24:39 +0000 | [diff] [blame] | 141 | StringRef StringTable = check(Obj.getStringTableForSymtab(*SymtabSec)); |
| 142 | return check(Sym->getName(StringTable)); |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | template <class ELFT> |
| Rui Ueyama | 368e1ea | 2016-03-13 22:02:04 +0000 | [diff] [blame] | 146 | ArrayRef<typename elf::ObjectFile<ELFT>::Elf_Word> |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 147 | elf::ObjectFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) { |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 148 | const ELFFile<ELFT> &Obj = this->ELFObj; |
| Rui Ueyama | 368e1ea | 2016-03-13 22:02:04 +0000 | [diff] [blame] | 149 | ArrayRef<Elf_Word> Entries = |
| 150 | check(Obj.template getSectionContentsAsArray<Elf_Word>(&Sec)); |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 151 | if (Entries.empty() || Entries[0] != GRP_COMDAT) |
| George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 152 | fatal("unsupported SHT_GROUP format"); |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 153 | return Entries.slice(1); |
| 154 | } |
| 155 | |
| Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 156 | template <class ELFT> static bool shouldMerge(const typename ELFT::Shdr &Sec) { |
| 157 | typedef typename ELFT::uint uintX_t; |
| Rui Ueyama | fb6d499 | 2016-04-29 16:12:29 +0000 | [diff] [blame] | 158 | |
| 159 | // We don't merge sections if -O0 (default is -O1). This makes sometimes |
| 160 | // the linker significantly faster, although the output will be bigger. |
| 161 | if (Config->Optimize == 0) |
| 162 | return false; |
| 163 | |
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 164 | uintX_t Flags = Sec.sh_flags; |
| 165 | if (!(Flags & SHF_MERGE)) |
| 166 | return false; |
| 167 | if (Flags & SHF_WRITE) |
| George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 168 | fatal("writable SHF_MERGE sections are not supported"); |
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 169 | uintX_t EntSize = Sec.sh_entsize; |
| Rafael Espindola | 0d2ad42 | 2016-03-21 14:57:20 +0000 | [diff] [blame] | 170 | if (!EntSize || Sec.sh_size % EntSize) |
| Rui Ueyama | 64cfffd | 2016-01-28 18:40:06 +0000 | [diff] [blame] | 171 | fatal("SHF_MERGE section size must be a multiple of sh_entsize"); |
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 172 | |
| Rafael Espindola | 7efa5be | 2016-02-19 14:17:40 +0000 | [diff] [blame] | 173 | // Don't try to merge if the aligment is larger than the sh_entsize and this |
| 174 | // is not SHF_STRINGS. |
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 175 | // |
| Rafael Espindola | 7efa5be | 2016-02-19 14:17:40 +0000 | [diff] [blame] | 176 | // Since this is not a SHF_STRINGS, we would need to pad after every entity. |
| 177 | // It would be equivalent for the producer of the .o to just set a larger |
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 178 | // sh_entsize. |
| Rafael Espindola | 7efa5be | 2016-02-19 14:17:40 +0000 | [diff] [blame] | 179 | if (Flags & SHF_STRINGS) |
| 180 | return true; |
| 181 | |
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 182 | if (Sec.sh_addralign > EntSize) |
| 183 | return false; |
| 184 | |
| 185 | return true; |
| 186 | } |
| 187 | |
| 188 | template <class ELFT> |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 189 | void elf::ObjectFile<ELFT>::initializeSections( |
| Rafael Espindola | f1d598c | 2016-02-12 21:17:10 +0000 | [diff] [blame] | 190 | DenseSet<StringRef> &ComdatGroups) { |
| Rafael Espindola | e1901cc | 2015-09-24 15:11:50 +0000 | [diff] [blame] | 191 | uint64_t Size = this->ELFObj.getNumSections(); |
| Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 192 | Sections.resize(Size); |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 193 | unsigned I = -1; |
| Rafael Espindola | d42f4e5 | 2015-10-08 12:02:38 +0000 | [diff] [blame] | 194 | const ELFFile<ELFT> &Obj = this->ELFObj; |
| 195 | for (const Elf_Shdr &Sec : Obj.sections()) { |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 196 | ++I; |
| Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 197 | if (Sections[I] == &InputSection<ELFT>::Discarded) |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 198 | continue; |
| 199 | |
| Rafael Espindola | cde2513 | 2015-08-13 14:45:44 +0000 | [diff] [blame] | 200 | switch (Sec.sh_type) { |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 201 | case SHT_GROUP: |
| Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 202 | Sections[I] = &InputSection<ELFT>::Discarded; |
| Rui Ueyama | 52d3b67 | 2016-01-06 02:06:33 +0000 | [diff] [blame] | 203 | if (ComdatGroups.insert(getShtGroupSignature(Sec)).second) |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 204 | continue; |
| Rui Ueyama | 33b3f21 | 2016-01-06 20:30:02 +0000 | [diff] [blame] | 205 | for (uint32_t SecIndex : getShtGroupEntries(Sec)) { |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 206 | if (SecIndex >= Size) |
| George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 207 | fatal("invalid section index in group"); |
| Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 208 | Sections[SecIndex] = &InputSection<ELFT>::Discarded; |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 209 | } |
| 210 | break; |
| Rafael Espindola | cde2513 | 2015-08-13 14:45:44 +0000 | [diff] [blame] | 211 | case SHT_SYMTAB: |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 212 | this->Symtab = &Sec; |
| Rafael Espindola | cde2513 | 2015-08-13 14:45:44 +0000 | [diff] [blame] | 213 | break; |
| Rafael Espindola | 1130935c | 2016-03-03 16:21:44 +0000 | [diff] [blame] | 214 | case SHT_SYMTAB_SHNDX: |
| Rafael Espindola | 75714f6 | 2016-03-03 22:24:39 +0000 | [diff] [blame] | 215 | this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec)); |
| Rafael Espindola | 2034822 | 2015-08-24 21:43:25 +0000 | [diff] [blame] | 216 | break; |
| Rafael Espindola | cde2513 | 2015-08-13 14:45:44 +0000 | [diff] [blame] | 217 | case SHT_STRTAB: |
| 218 | case SHT_NULL: |
| Rafael Espindola | cde2513 | 2015-08-13 14:45:44 +0000 | [diff] [blame] | 219 | break; |
| Michael J. Spencer | 67bc8d6 | 2015-08-27 23:15:56 +0000 | [diff] [blame] | 220 | case SHT_RELA: |
| 221 | case SHT_REL: { |
| Rui Ueyama | e270c0a | 2016-03-13 21:52:57 +0000 | [diff] [blame] | 222 | // This section contains relocation information. |
| 223 | // If -r is given, we do not interpret or apply relocation |
| 224 | // but just copy relocation sections to output. |
| George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 225 | if (Config->Relocatable) { |
| Ivan Krasin | bfc9131 | 2016-04-06 01:11:10 +0000 | [diff] [blame] | 226 | Sections[I] = new (IAlloc.Allocate()) InputSection<ELFT>(this, &Sec); |
| Rui Ueyama | e270c0a | 2016-03-13 21:52:57 +0000 | [diff] [blame] | 227 | break; |
| 228 | } |
| 229 | |
| 230 | // Find the relocation target section and associate this |
| 231 | // section with it. |
| 232 | InputSectionBase<ELFT> *Target = getRelocTarget(Sec); |
| 233 | if (!Target) |
| 234 | break; |
| 235 | if (auto *S = dyn_cast<InputSection<ELFT>>(Target)) { |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 236 | S->RelocSections.push_back(&Sec); |
| Rui Ueyama | e270c0a | 2016-03-13 21:52:57 +0000 | [diff] [blame] | 237 | break; |
| 238 | } |
| 239 | if (auto *S = dyn_cast<EHInputSection<ELFT>>(Target)) { |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 240 | if (S->RelocSection) |
| George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 241 | fatal("multiple relocation sections to .eh_frame are not supported"); |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 242 | S->RelocSection = &Sec; |
| Rui Ueyama | e270c0a | 2016-03-13 21:52:57 +0000 | [diff] [blame] | 243 | break; |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 244 | } |
| Rui Ueyama | e270c0a | 2016-03-13 21:52:57 +0000 | [diff] [blame] | 245 | fatal("relocations pointing to SHF_MERGE are not supported"); |
| Michael J. Spencer | 67bc8d6 | 2015-08-27 23:15:56 +0000 | [diff] [blame] | 246 | } |
| Rui Ueyama | e79b09a | 2015-11-21 22:19:32 +0000 | [diff] [blame] | 247 | default: |
| Rui Ueyama | 3f11c8c | 2015-12-24 08:41:12 +0000 | [diff] [blame] | 248 | Sections[I] = createInputSection(Sec); |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| Rafael Espindola | f1d598c | 2016-02-12 21:17:10 +0000 | [diff] [blame] | 253 | template <class ELFT> |
| 254 | InputSectionBase<ELFT> * |
| Rui Ueyama | e270c0a | 2016-03-13 21:52:57 +0000 | [diff] [blame] | 255 | elf::ObjectFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) { |
| 256 | uint32_t Idx = Sec.sh_info; |
| 257 | if (Idx >= Sections.size()) |
| 258 | fatal("invalid relocated section index"); |
| 259 | InputSectionBase<ELFT> *Target = Sections[Idx]; |
| 260 | |
| 261 | // Strictly speaking, a relocation section must be included in the |
| 262 | // group of the section it relocates. However, LLVM 3.3 and earlier |
| 263 | // would fail to do so, so we gracefully handle that case. |
| Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 264 | if (Target == &InputSection<ELFT>::Discarded) |
| Rui Ueyama | e270c0a | 2016-03-13 21:52:57 +0000 | [diff] [blame] | 265 | return nullptr; |
| 266 | |
| 267 | if (!Target) |
| 268 | fatal("unsupported relocation reference"); |
| 269 | return Target; |
| 270 | } |
| 271 | |
| 272 | template <class ELFT> |
| 273 | InputSectionBase<ELFT> * |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 274 | elf::ObjectFile<ELFT>::createInputSection(const Elf_Shdr &Sec) { |
| Rafael Espindola | 75714f6 | 2016-03-03 22:24:39 +0000 | [diff] [blame] | 275 | StringRef Name = check(this->ELFObj.getSectionName(&Sec)); |
| Rui Ueyama | 3f11c8c | 2015-12-24 08:41:12 +0000 | [diff] [blame] | 276 | |
| 277 | // .note.GNU-stack is a marker section to control the presence of |
| 278 | // PT_GNU_STACK segment in outputs. Since the presence of the segment |
| 279 | // is controlled only by the command line option (-z execstack) in LLD, |
| 280 | // .note.GNU-stack is ignored. |
| 281 | if (Name == ".note.GNU-stack") |
| Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 282 | return &InputSection<ELFT>::Discarded; |
| Rui Ueyama | 3f11c8c | 2015-12-24 08:41:12 +0000 | [diff] [blame] | 283 | |
| Rui Ueyama | fc6a4b0 | 2016-04-07 21:04:51 +0000 | [diff] [blame] | 284 | if (Name == ".note.GNU-split-stack") { |
| George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 285 | error("objects using splitstacks are not supported"); |
| Rui Ueyama | fc6a4b0 | 2016-04-07 21:04:51 +0000 | [diff] [blame] | 286 | return &InputSection<ELFT>::Discarded; |
| 287 | } |
| 288 | |
| 289 | if (Config->StripDebug && Name.startswith(".debug")) |
| 290 | return &InputSection<ELFT>::Discarded; |
| George Rimar | 3c45ed2 | 2016-03-09 18:01:45 +0000 | [diff] [blame] | 291 | |
| Simon Atanasyan | add74f3 | 2016-05-04 10:07:38 +0000 | [diff] [blame] | 292 | // A MIPS object file has a special sections that contain register |
| 293 | // usage info, which need to be handled by the linker specially. |
| 294 | if (Config->EMachine == EM_MIPS) { |
| 295 | if (Name == ".reginfo") { |
| 296 | MipsReginfo.reset(new MipsReginfoInputSection<ELFT>(this, &Sec)); |
| 297 | return MipsReginfo.get(); |
| 298 | } |
| 299 | if (Name == ".MIPS.options") { |
| 300 | MipsOptions.reset(new MipsOptionsInputSection<ELFT>(this, &Sec)); |
| 301 | return MipsOptions.get(); |
| 302 | } |
| Simon Atanasyan | 57830b6 | 2015-12-25 13:02:13 +0000 | [diff] [blame] | 303 | } |
| Rui Ueyama | 3f11c8c | 2015-12-24 08:41:12 +0000 | [diff] [blame] | 304 | |
| George Rimar | 4cfe572 | 2016-03-03 07:49:35 +0000 | [diff] [blame] | 305 | // We dont need special handling of .eh_frame sections if relocatable |
| 306 | // output was choosen. Proccess them as usual input sections. |
| 307 | if (!Config->Relocatable && Name == ".eh_frame") |
| Rui Ueyama | e69ab10 | 2016-01-06 01:14:11 +0000 | [diff] [blame] | 308 | return new (EHAlloc.Allocate()) EHInputSection<ELFT>(this, &Sec); |
| Rui Ueyama | 3f11c8c | 2015-12-24 08:41:12 +0000 | [diff] [blame] | 309 | if (shouldMerge<ELFT>(Sec)) |
| Rui Ueyama | e69ab10 | 2016-01-06 01:14:11 +0000 | [diff] [blame] | 310 | return new (MAlloc.Allocate()) MergeInputSection<ELFT>(this, &Sec); |
| Ivan Krasin | bfc9131 | 2016-04-06 01:11:10 +0000 | [diff] [blame] | 311 | return new (IAlloc.Allocate()) InputSection<ELFT>(this, &Sec); |
| Rui Ueyama | 3f11c8c | 2015-12-24 08:41:12 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 314 | template <class ELFT> void elf::ObjectFile<ELFT>::initializeSymbols() { |
| Rafael Espindola | 6a3b5de | 2015-10-01 19:52:48 +0000 | [diff] [blame] | 315 | this->initStringTable(); |
| Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 316 | Elf_Sym_Range Syms = this->getElfSymbols(false); |
| Reid Kleckner | f7b85e0 | 2015-08-11 20:06:51 +0000 | [diff] [blame] | 317 | uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end()); |
| Rui Ueyama | e69ab10 | 2016-01-06 01:14:11 +0000 | [diff] [blame] | 318 | SymbolBodies.reserve(NumSymbols); |
| Rafael Espindola | 3031851 | 2015-08-04 14:00:56 +0000 | [diff] [blame] | 319 | for (const Elf_Sym &Sym : Syms) |
| Rui Ueyama | c5e372d | 2016-01-21 02:10:12 +0000 | [diff] [blame] | 320 | SymbolBodies.push_back(createSymbolBody(&Sym)); |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | template <class ELFT> |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 324 | InputSectionBase<ELFT> * |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 325 | elf::ObjectFile<ELFT>::getSection(const Elf_Sym &Sym) const { |
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 326 | uint32_t Index = this->getSectionIndex(Sym); |
| 327 | if (Index == 0) |
| Rafael Espindola | 4cda581 | 2015-10-16 15:29:48 +0000 | [diff] [blame] | 328 | return nullptr; |
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 329 | if (Index >= Sections.size() || !Sections[Index]) |
| George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 330 | fatal("invalid section index"); |
| Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 331 | InputSectionBase<ELFT> *S = Sections[Index]; |
| Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 332 | if (S == &InputSectionBase<ELFT>::Discarded) |
| Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 333 | return S; |
| 334 | return S->Repl; |
| Rafael Espindola | 4cda581 | 2015-10-16 15:29:48 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | template <class ELFT> |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 338 | SymbolBody *elf::ObjectFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) { |
| Rui Ueyama | 376ab7c | 2016-04-05 17:47:29 +0000 | [diff] [blame] | 339 | unsigned char Binding = Sym->getBinding(); |
| Rafael Espindola | 1f5b70f | 2016-03-11 14:21:37 +0000 | [diff] [blame] | 340 | InputSectionBase<ELFT> *Sec = getSection(*Sym); |
| 341 | if (Binding == STB_LOCAL) { |
| Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 342 | if (Sym->st_shndx == SHN_UNDEF) |
| Rui Ueyama | 62ee16f | 2016-04-28 00:26:54 +0000 | [diff] [blame] | 343 | return new (Alloc) Undefined(Sym->st_name, Sym->st_other, Sym->getType()); |
| Rafael Espindola | d9a1717 | 2016-04-05 11:47:46 +0000 | [diff] [blame] | 344 | return new (Alloc) DefinedRegular<ELFT>(*Sym, Sec); |
| Rafael Espindola | 1f5b70f | 2016-03-11 14:21:37 +0000 | [diff] [blame] | 345 | } |
| Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 346 | |
| Rafael Espindola | 75714f6 | 2016-03-03 22:24:39 +0000 | [diff] [blame] | 347 | StringRef Name = check(Sym->getName(this->StringTable)); |
| Rafael Espindola | 2034822 | 2015-08-24 21:43:25 +0000 | [diff] [blame] | 348 | |
| Rafael Espindola | 4cda581 | 2015-10-16 15:29:48 +0000 | [diff] [blame] | 349 | switch (Sym->st_shndx) { |
| Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 350 | case SHN_UNDEF: |
| Peter Collingbourne | 3db410e | 2016-05-01 06:00:09 +0000 | [diff] [blame] | 351 | return elf::Symtab<ELFT>::X |
| Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 352 | ->addUndefined(Name, Binding, Sym->st_other, Sym->getType(), this) |
| 353 | ->body(); |
| Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 354 | case SHN_COMMON: |
| Peter Collingbourne | 3db410e | 2016-05-01 06:00:09 +0000 | [diff] [blame] | 355 | return elf::Symtab<ELFT>::X |
| Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 356 | ->addCommon(Name, Sym->st_size, Sym->st_value, Binding, Sym->st_other, |
| 357 | Sym->getType(), this) |
| 358 | ->body(); |
| Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 359 | } |
| Rafael Espindola | 2034822 | 2015-08-24 21:43:25 +0000 | [diff] [blame] | 360 | |
| Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 361 | switch (Binding) { |
| Rafael Espindola | b13df65 | 2015-08-11 17:33:02 +0000 | [diff] [blame] | 362 | default: |
| George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 363 | fatal("unexpected binding"); |
| Rafael Espindola | b13df65 | 2015-08-11 17:33:02 +0000 | [diff] [blame] | 364 | case STB_GLOBAL: |
| Rafael Espindola | 3a63f3f | 2015-08-28 20:19:34 +0000 | [diff] [blame] | 365 | case STB_WEAK: |
| Rafael Espindola | 1f5b70f | 2016-03-11 14:21:37 +0000 | [diff] [blame] | 366 | case STB_GNU_UNIQUE: |
| Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 367 | if (Sec == &InputSection<ELFT>::Discarded) |
| Peter Collingbourne | 3db410e | 2016-05-01 06:00:09 +0000 | [diff] [blame] | 368 | return elf::Symtab<ELFT>::X |
| Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 369 | ->addUndefined(Name, Binding, Sym->st_other, Sym->getType(), this) |
| 370 | ->body(); |
| Peter Collingbourne | 3db410e | 2016-05-01 06:00:09 +0000 | [diff] [blame] | 371 | return elf::Symtab<ELFT>::X->addRegular(Name, *Sym, Sec)->body(); |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 372 | } |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 375 | template <class ELFT> void ArchiveFile::parse() { |
| Rui Ueyama | 64bd8df | 2016-03-14 21:31:07 +0000 | [diff] [blame] | 376 | File = check(Archive::create(MB), "failed to parse archive"); |
| Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 377 | |
| Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 378 | // Read the symbol table to construct Lazy objects. |
| 379 | for (const Archive::Symbol &Sym : File->symbols()) |
| Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 380 | Symtab<ELFT>::X->addLazyArchive(this, Sym); |
| Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | // Returns a buffer pointing to a member file containing a given symbol. |
| 384 | MemoryBufferRef ArchiveFile::getMember(const Archive::Symbol *Sym) { |
| Rafael Espindola | 1130935c | 2016-03-03 16:21:44 +0000 | [diff] [blame] | 385 | Archive::Child C = |
| Rafael Espindola | 75714f6 | 2016-03-03 22:24:39 +0000 | [diff] [blame] | 386 | check(Sym->getMember(), |
| Rui Ueyama | 64bd8df | 2016-03-14 21:31:07 +0000 | [diff] [blame] | 387 | "could not get the member for symbol " + Sym->getName()); |
| Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 388 | |
| Rafael Espindola | 8f3a6ae | 2015-11-05 14:40:28 +0000 | [diff] [blame] | 389 | if (!Seen.insert(C.getChildOffset()).second) |
| Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 390 | return MemoryBufferRef(); |
| Michael J. Spencer | 88f0d63 | 2015-09-08 20:36:20 +0000 | [diff] [blame] | 391 | |
| Rafael Espindola | 1dd2b3d | 2016-05-03 17:30:44 +0000 | [diff] [blame] | 392 | MemoryBufferRef Ret = |
| 393 | check(C.getMemoryBufferRef(), |
| 394 | "could not get the buffer for the member defining symbol " + |
| 395 | Sym->getName()); |
| Rafael Espindola | d1cbe4d | 2016-05-02 13:54:10 +0000 | [diff] [blame] | 396 | |
| Rafael Espindola | 1dd2b3d | 2016-05-03 17:30:44 +0000 | [diff] [blame] | 397 | if (C.getParent()->isThin()) |
| 398 | maybeCopyInputFile(check(C.getFullName()), Ret.getBuffer()); |
| 399 | |
| 400 | return Ret; |
| Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| Rafael Espindola | e1901cc | 2015-09-24 15:11:50 +0000 | [diff] [blame] | 403 | template <class ELFT> |
| 404 | SharedFile<ELFT>::SharedFile(MemoryBufferRef M) |
| Rui Ueyama | f588ac4 | 2016-01-06 00:09:41 +0000 | [diff] [blame] | 405 | : ELFFileBase<ELFT>(Base::SharedKind, M), AsNeeded(Config->AsNeeded) {} |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 406 | |
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 407 | template <class ELFT> |
| Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 408 | const typename ELFT::Shdr * |
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 409 | SharedFile<ELFT>::getSection(const Elf_Sym &Sym) const { |
| 410 | uint32_t Index = this->getSectionIndex(Sym); |
| 411 | if (Index == 0) |
| 412 | return nullptr; |
| Rafael Espindola | 75714f6 | 2016-03-03 22:24:39 +0000 | [diff] [blame] | 413 | return check(this->ELFObj.getSection(Index)); |
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| Rui Ueyama | 7c71331 | 2016-01-06 01:56:36 +0000 | [diff] [blame] | 416 | // Partially parse the shared object file so that we can call |
| 417 | // getSoName on this object. |
| Rafael Espindola | 6a3b5de | 2015-10-01 19:52:48 +0000 | [diff] [blame] | 418 | template <class ELFT> void SharedFile<ELFT>::parseSoName() { |
| Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 419 | typedef typename ELFT::Dyn Elf_Dyn; |
| 420 | typedef typename ELFT::uint uintX_t; |
| Rafael Espindola | c8b1581 | 2015-10-01 15:47:50 +0000 | [diff] [blame] | 421 | const Elf_Shdr *DynamicSec = nullptr; |
| 422 | |
| 423 | const ELFFile<ELFT> Obj = this->ELFObj; |
| 424 | for (const Elf_Shdr &Sec : Obj.sections()) { |
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 425 | switch (Sec.sh_type) { |
| 426 | default: |
| 427 | continue; |
| 428 | case SHT_DYNSYM: |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 429 | this->Symtab = &Sec; |
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 430 | break; |
| 431 | case SHT_DYNAMIC: |
| Rafael Espindola | c8b1581 | 2015-10-01 15:47:50 +0000 | [diff] [blame] | 432 | DynamicSec = &Sec; |
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 433 | break; |
| Rafael Espindola | 1130935c | 2016-03-03 16:21:44 +0000 | [diff] [blame] | 434 | case SHT_SYMTAB_SHNDX: |
| Rafael Espindola | 75714f6 | 2016-03-03 22:24:39 +0000 | [diff] [blame] | 435 | this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec)); |
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 436 | break; |
| Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 437 | case SHT_GNU_versym: |
| 438 | this->VersymSec = &Sec; |
| 439 | break; |
| 440 | case SHT_GNU_verdef: |
| 441 | this->VerdefSec = &Sec; |
| 442 | break; |
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 443 | } |
| Rafael Espindola | c8b1581 | 2015-10-01 15:47:50 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| Rafael Espindola | 6a3b5de | 2015-10-01 19:52:48 +0000 | [diff] [blame] | 446 | this->initStringTable(); |
| Rui Ueyama | e69ab10 | 2016-01-06 01:14:11 +0000 | [diff] [blame] | 447 | SoName = this->getName(); |
| Rafael Espindola | c8b1581 | 2015-10-01 15:47:50 +0000 | [diff] [blame] | 448 | |
| Rui Ueyama | 361d8b9 | 2015-10-12 15:49:02 +0000 | [diff] [blame] | 449 | if (!DynamicSec) |
| 450 | return; |
| 451 | auto *Begin = |
| 452 | reinterpret_cast<const Elf_Dyn *>(Obj.base() + DynamicSec->sh_offset); |
| 453 | const Elf_Dyn *End = Begin + DynamicSec->sh_size / sizeof(Elf_Dyn); |
| Rafael Espindola | c8b1581 | 2015-10-01 15:47:50 +0000 | [diff] [blame] | 454 | |
| Rui Ueyama | 361d8b9 | 2015-10-12 15:49:02 +0000 | [diff] [blame] | 455 | for (const Elf_Dyn &Dyn : make_range(Begin, End)) { |
| 456 | if (Dyn.d_tag == DT_SONAME) { |
| 457 | uintX_t Val = Dyn.getVal(); |
| 458 | if (Val >= this->StringTable.size()) |
| George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 459 | fatal("invalid DT_SONAME entry"); |
| Rui Ueyama | e69ab10 | 2016-01-06 01:14:11 +0000 | [diff] [blame] | 460 | SoName = StringRef(this->StringTable.data() + Val); |
| Rui Ueyama | 361d8b9 | 2015-10-12 15:49:02 +0000 | [diff] [blame] | 461 | return; |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 462 | } |
| 463 | } |
| Rafael Espindola | 6a3b5de | 2015-10-01 19:52:48 +0000 | [diff] [blame] | 464 | } |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 465 | |
| Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 466 | // Parse the version definitions in the object file if present. Returns a vector |
| 467 | // whose nth element contains a pointer to the Elf_Verdef for version identifier |
| 468 | // n. Version identifiers that are not definitions map to nullptr. The array |
| 469 | // always has at least length 1. |
| 470 | template <class ELFT> |
| 471 | std::vector<const typename ELFT::Verdef *> |
| 472 | SharedFile<ELFT>::parseVerdefs(const Elf_Versym *&Versym) { |
| 473 | std::vector<const Elf_Verdef *> Verdefs(1); |
| 474 | // We only need to process symbol versions for this DSO if it has both a |
| 475 | // versym and a verdef section, which indicates that the DSO contains symbol |
| 476 | // version definitions. |
| 477 | if (!VersymSec || !VerdefSec) |
| 478 | return Verdefs; |
| 479 | |
| 480 | // The location of the first global versym entry. |
| 481 | Versym = reinterpret_cast<const Elf_Versym *>(this->ELFObj.base() + |
| 482 | VersymSec->sh_offset) + |
| 483 | this->Symtab->sh_info; |
| 484 | |
| 485 | // We cannot determine the largest verdef identifier without inspecting |
| 486 | // every Elf_Verdef, but both bfd and gold assign verdef identifiers |
| 487 | // sequentially starting from 1, so we predict that the largest identifier |
| 488 | // will be VerdefCount. |
| 489 | unsigned VerdefCount = VerdefSec->sh_info; |
| 490 | Verdefs.resize(VerdefCount + 1); |
| 491 | |
| 492 | // Build the Verdefs array by following the chain of Elf_Verdef objects |
| 493 | // from the start of the .gnu.version_d section. |
| 494 | const uint8_t *Verdef = this->ELFObj.base() + VerdefSec->sh_offset; |
| 495 | for (unsigned I = 0; I != VerdefCount; ++I) { |
| 496 | auto *CurVerdef = reinterpret_cast<const Elf_Verdef *>(Verdef); |
| 497 | Verdef += CurVerdef->vd_next; |
| 498 | unsigned VerdefIndex = CurVerdef->vd_ndx; |
| 499 | if (Verdefs.size() <= VerdefIndex) |
| 500 | Verdefs.resize(VerdefIndex + 1); |
| 501 | Verdefs[VerdefIndex] = CurVerdef; |
| 502 | } |
| 503 | |
| 504 | return Verdefs; |
| 505 | } |
| 506 | |
| Rui Ueyama | 7c71331 | 2016-01-06 01:56:36 +0000 | [diff] [blame] | 507 | // Fully parse the shared object file. This must be called after parseSoName(). |
| 508 | template <class ELFT> void SharedFile<ELFT>::parseRest() { |
| Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 509 | // Create mapping from version identifiers to Elf_Verdef entries. |
| 510 | const Elf_Versym *Versym = nullptr; |
| 511 | std::vector<const Elf_Verdef *> Verdefs = parseVerdefs(Versym); |
| 512 | |
| Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 513 | Elf_Sym_Range Syms = this->getElfSymbols(true); |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 514 | for (const Elf_Sym &Sym : Syms) { |
| Rafael Espindola | fb4f2fe | 2016-04-29 17:46:07 +0000 | [diff] [blame] | 515 | unsigned VersymIndex = 0; |
| 516 | if (Versym) { |
| 517 | VersymIndex = Versym->vs_index; |
| 518 | ++Versym; |
| 519 | } |
| 520 | |
| Rafael Espindola | 18da0e5 | 2016-04-29 16:23:31 +0000 | [diff] [blame] | 521 | StringRef Name = check(Sym.getName(this->StringTable)); |
| 522 | if (Sym.isUndefined()) { |
| 523 | Undefs.push_back(Name); |
| 524 | continue; |
| 525 | } |
| 526 | |
| Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 527 | if (Versym) { |
| Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 528 | // Ignore local symbols and non-default versions. |
| 529 | if (VersymIndex == 0 || (VersymIndex & VERSYM_HIDDEN)) |
| 530 | continue; |
| 531 | } |
| Peter Collingbourne | 3db410e | 2016-05-01 06:00:09 +0000 | [diff] [blame] | 532 | elf::Symtab<ELFT>::X->addShared(this, Name, Sym, Verdefs[VersymIndex]); |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 533 | } |
| 534 | } |
| Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 535 | |
| Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 536 | BitcodeFile::BitcodeFile(MemoryBufferRef M) : InputFile(BitcodeKind, M) {} |
| 537 | |
| Rui Ueyama | fd4fee5 | 2016-03-07 00:54:17 +0000 | [diff] [blame] | 538 | static uint8_t getGvVisibility(const GlobalValue *GV) { |
| 539 | switch (GV->getVisibility()) { |
| Rui Ueyama | 68fae23 | 2016-03-07 19:06:14 +0000 | [diff] [blame] | 540 | case GlobalValue::DefaultVisibility: |
| 541 | return STV_DEFAULT; |
| Rui Ueyama | fd4fee5 | 2016-03-07 00:54:17 +0000 | [diff] [blame] | 542 | case GlobalValue::HiddenVisibility: |
| 543 | return STV_HIDDEN; |
| 544 | case GlobalValue::ProtectedVisibility: |
| 545 | return STV_PROTECTED; |
| Rui Ueyama | fd4fee5 | 2016-03-07 00:54:17 +0000 | [diff] [blame] | 546 | } |
| George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 547 | llvm_unreachable("unknown visibility"); |
| Rui Ueyama | f714955 | 2016-03-11 18:46:51 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 550 | template <class ELFT> |
| 551 | Symbol *BitcodeFile::createSymbol(const DenseSet<const Comdat *> &KeptComdats, |
| 552 | const IRObjectFile &Obj, |
| 553 | const BasicSymbolRef &Sym) { |
| 554 | const GlobalValue *GV = Obj.getSymbolGV(Sym.getRawDataRefImpl()); |
| 555 | |
| Davide Italiano | 9f8efff | 2016-04-22 18:26:33 +0000 | [diff] [blame] | 556 | SmallString<64> Name; |
| 557 | raw_svector_ostream OS(Name); |
| 558 | Sym.printName(OS); |
| 559 | StringRef NameRef = Saver.save(StringRef(Name)); |
| Rui Ueyama | f714955 | 2016-03-11 18:46:51 +0000 | [diff] [blame] | 560 | |
| Peter Collingbourne | 7cf73ec | 2016-04-11 16:39:43 +0000 | [diff] [blame] | 561 | uint32_t Flags = Sym.getFlags(); |
| Davide Italiano | 9f8efff | 2016-04-22 18:26:33 +0000 | [diff] [blame] | 562 | bool IsWeak = Flags & BasicSymbolRef::SF_Weak; |
| Peter Collingbourne | 60976ed | 2016-04-27 00:05:06 +0000 | [diff] [blame] | 563 | uint32_t Binding = IsWeak ? STB_WEAK : STB_GLOBAL; |
| Davide Italiano | 9f8efff | 2016-04-22 18:26:33 +0000 | [diff] [blame] | 564 | |
| Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 565 | uint8_t Type = STT_NOTYPE; |
| 566 | bool CanOmitFromDynSym = false; |
| 567 | // FIXME: Expose a thread-local flag for module asm symbols. |
| 568 | if (GV) { |
| 569 | if (GV->isThreadLocal()) |
| 570 | Type = STT_TLS; |
| 571 | CanOmitFromDynSym = canBeOmittedFromSymbolTable(GV); |
| 572 | } |
| 573 | |
| Peter Collingbourne | 7cf73ec | 2016-04-11 16:39:43 +0000 | [diff] [blame] | 574 | uint8_t Visibility; |
| 575 | if (GV) |
| 576 | Visibility = getGvVisibility(GV); |
| 577 | else |
| 578 | // FIXME: Set SF_Hidden flag correctly for module asm symbols, and expose |
| 579 | // protected visibility. |
| 580 | Visibility = STV_DEFAULT; |
| Rui Ueyama | f714955 | 2016-03-11 18:46:51 +0000 | [diff] [blame] | 581 | |
| Davide Italiano | 9f8efff | 2016-04-22 18:26:33 +0000 | [diff] [blame] | 582 | if (GV) |
| 583 | if (const Comdat *C = GV->getComdat()) |
| Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 584 | if (!KeptComdats.count(C)) |
| 585 | return Symtab<ELFT>::X->addUndefined(NameRef, Binding, Visibility, Type, |
| 586 | this); |
| Rui Ueyama | f714955 | 2016-03-11 18:46:51 +0000 | [diff] [blame] | 587 | |
| 588 | const Module &M = Obj.getModule(); |
| Davide Italiano | 9f8efff | 2016-04-22 18:26:33 +0000 | [diff] [blame] | 589 | if (Flags & BasicSymbolRef::SF_Undefined) |
| Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 590 | return Symtab<ELFT>::X->addUndefined(NameRef, Binding, Visibility, Type, |
| 591 | this); |
| Davide Italiano | 9f8efff | 2016-04-22 18:26:33 +0000 | [diff] [blame] | 592 | if (Flags & BasicSymbolRef::SF_Common) { |
| Peter Collingbourne | 7cf73ec | 2016-04-11 16:39:43 +0000 | [diff] [blame] | 593 | // FIXME: Set SF_Common flag correctly for module asm symbols, and expose |
| 594 | // size and alignment. |
| 595 | assert(GV); |
| Rui Ueyama | f714955 | 2016-03-11 18:46:51 +0000 | [diff] [blame] | 596 | const DataLayout &DL = M.getDataLayout(); |
| 597 | uint64_t Size = DL.getTypeAllocSize(GV->getValueType()); |
| Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 598 | return Symtab<ELFT>::X->addCommon(NameRef, Size, GV->getAlignment(), |
| 599 | Binding, Visibility, STT_OBJECT, this); |
| Rui Ueyama | f714955 | 2016-03-11 18:46:51 +0000 | [diff] [blame] | 600 | } |
| Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 601 | return Symtab<ELFT>::X->addBitcode(NameRef, IsWeak, Visibility, Type, |
| 602 | CanOmitFromDynSym, this); |
| Davide Italiano | 9f8efff | 2016-04-22 18:26:33 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 605 | bool BitcodeFile::shouldSkip(uint32_t Flags) { |
| Rui Ueyama | f714955 | 2016-03-11 18:46:51 +0000 | [diff] [blame] | 606 | if (!(Flags & BasicSymbolRef::SF_Global)) |
| 607 | return true; |
| 608 | if (Flags & BasicSymbolRef::SF_FormatSpecific) |
| 609 | return true; |
| 610 | return false; |
| Rafael Espindola | 9b3acf9 | 2016-03-11 16:11:47 +0000 | [diff] [blame] | 611 | } |
| 612 | |
| Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 613 | template <class ELFT> |
| Rafael Espindola | 4de44b7 | 2016-03-02 15:43:50 +0000 | [diff] [blame] | 614 | void BitcodeFile::parse(DenseSet<StringRef> &ComdatGroups) { |
| Rafael Espindola | 156f4ee | 2016-04-28 19:30:41 +0000 | [diff] [blame] | 615 | Obj = check(IRObjectFile::create(MB, Driver->Context)); |
| Rafael Espindola | 1130935c | 2016-03-03 16:21:44 +0000 | [diff] [blame] | 616 | const Module &M = Obj->getModule(); |
| Rafael Espindola | 4de44b7 | 2016-03-02 15:43:50 +0000 | [diff] [blame] | 617 | |
| 618 | DenseSet<const Comdat *> KeptComdats; |
| 619 | for (const auto &P : M.getComdatSymbolTable()) { |
| 620 | StringRef N = Saver.save(P.first()); |
| 621 | if (ComdatGroups.insert(N).second) |
| 622 | KeptComdats.insert(&P.second); |
| 623 | } |
| 624 | |
| Rui Ueyama | f714955 | 2016-03-11 18:46:51 +0000 | [diff] [blame] | 625 | for (const BasicSymbolRef &Sym : Obj->symbols()) |
| Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 626 | if (!shouldSkip(Sym.getFlags())) |
| 627 | Symbols.push_back(createSymbol<ELFT>(KeptComdats, *Obj, Sym)); |
| Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 628 | } |
| 629 | |
| Rui Ueyama | c4b6506 | 2015-10-12 15:31:09 +0000 | [diff] [blame] | 630 | template <typename T> |
| 631 | static std::unique_ptr<InputFile> createELFFileAux(MemoryBufferRef MB) { |
| 632 | std::unique_ptr<T> Ret = llvm::make_unique<T>(MB); |
| 633 | |
| 634 | if (!Config->FirstElf) |
| 635 | Config->FirstElf = Ret.get(); |
| 636 | |
| Rui Ueyama | e717a71 | 2015-10-13 16:20:50 +0000 | [diff] [blame] | 637 | if (Config->EKind == ELFNoneKind) { |
| 638 | Config->EKind = Ret->getELFKind(); |
| Rui Ueyama | c4b6506 | 2015-10-12 15:31:09 +0000 | [diff] [blame] | 639 | Config->EMachine = Ret->getEMachine(); |
| Simon Atanasyan | ae77ab7 | 2016-04-29 10:39:17 +0000 | [diff] [blame] | 640 | if (Config->EMachine == EM_MIPS && Config->EKind == ELF64LEKind) |
| 641 | Config->Mips64EL = true; |
| Rui Ueyama | c4b6506 | 2015-10-12 15:31:09 +0000 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | return std::move(Ret); |
| 645 | } |
| 646 | |
| 647 | template <template <class> class T> |
| Rui Ueyama | 533c030 | 2016-01-06 00:09:43 +0000 | [diff] [blame] | 648 | static std::unique_ptr<InputFile> createELFFile(MemoryBufferRef MB) { |
| Rui Ueyama | 57bbdaf | 2016-04-08 00:18:25 +0000 | [diff] [blame] | 649 | unsigned char Size; |
| 650 | unsigned char Endian; |
| 651 | std::tie(Size, Endian) = getElfArchType(MB.getBuffer()); |
| 652 | if (Endian != ELFDATA2LSB && Endian != ELFDATA2MSB) |
| George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 653 | fatal("invalid data encoding: " + MB.getBufferIdentifier()); |
| Rui Ueyama | c4b6506 | 2015-10-12 15:31:09 +0000 | [diff] [blame] | 654 | |
| Rui Ueyama | 57bbdaf | 2016-04-08 00:18:25 +0000 | [diff] [blame] | 655 | if (Size == ELFCLASS32) { |
| 656 | if (Endian == ELFDATA2LSB) |
| Rui Ueyama | d94478b | 2015-11-20 02:19:36 +0000 | [diff] [blame] | 657 | return createELFFileAux<T<ELF32LE>>(MB); |
| 658 | return createELFFileAux<T<ELF32BE>>(MB); |
| Rui Ueyama | c4b6506 | 2015-10-12 15:31:09 +0000 | [diff] [blame] | 659 | } |
| Rui Ueyama | 57bbdaf | 2016-04-08 00:18:25 +0000 | [diff] [blame] | 660 | if (Size == ELFCLASS64) { |
| 661 | if (Endian == ELFDATA2LSB) |
| Rui Ueyama | d94478b | 2015-11-20 02:19:36 +0000 | [diff] [blame] | 662 | return createELFFileAux<T<ELF64LE>>(MB); |
| 663 | return createELFFileAux<T<ELF64BE>>(MB); |
| Rui Ueyama | c4b6506 | 2015-10-12 15:31:09 +0000 | [diff] [blame] | 664 | } |
| George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 665 | fatal("invalid file class: " + MB.getBufferIdentifier()); |
| Rui Ueyama | c4b6506 | 2015-10-12 15:31:09 +0000 | [diff] [blame] | 666 | } |
| 667 | |
| Rui Ueyama | 4655ea3 | 2016-04-08 00:14:55 +0000 | [diff] [blame] | 668 | static bool isBitcode(MemoryBufferRef MB) { |
| 669 | using namespace sys::fs; |
| 670 | return identify_magic(MB.getBuffer()) == file_magic::bitcode; |
| 671 | } |
| 672 | |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 673 | std::unique_ptr<InputFile> elf::createObjectFile(MemoryBufferRef MB, |
| 674 | StringRef ArchiveName) { |
| Rui Ueyama | c89bff2 | 2016-02-23 18:17:11 +0000 | [diff] [blame] | 675 | std::unique_ptr<InputFile> F; |
| Rui Ueyama | 4655ea3 | 2016-04-08 00:14:55 +0000 | [diff] [blame] | 676 | if (isBitcode(MB)) |
| Rui Ueyama | c89bff2 | 2016-02-23 18:17:11 +0000 | [diff] [blame] | 677 | F.reset(new BitcodeFile(MB)); |
| 678 | else |
| 679 | F = createELFFile<ObjectFile>(MB); |
| Rui Ueyama | 71c066d | 2016-02-02 08:22:41 +0000 | [diff] [blame] | 680 | F->ArchiveName = ArchiveName; |
| 681 | return F; |
| Rui Ueyama | 533c030 | 2016-01-06 00:09:43 +0000 | [diff] [blame] | 682 | } |
| 683 | |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 684 | std::unique_ptr<InputFile> elf::createSharedFile(MemoryBufferRef MB) { |
| Rui Ueyama | 533c030 | 2016-01-06 00:09:43 +0000 | [diff] [blame] | 685 | return createELFFile<SharedFile>(MB); |
| 686 | } |
| 687 | |
| Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 688 | template <class ELFT> |
| Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 689 | void LazyObjectFile::parse() { |
| 690 | for (StringRef Sym : getSymbols()) |
| Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 691 | Symtab<ELFT>::X->addLazyObject(Sym, this->MB); |
| Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | template <class ELFT> std::vector<StringRef> LazyObjectFile::getElfSymbols() { |
| 695 | typedef typename ELFT::Shdr Elf_Shdr; |
| 696 | typedef typename ELFT::Sym Elf_Sym; |
| 697 | typedef typename ELFT::SymRange Elf_Sym_Range; |
| 698 | |
| 699 | const ELFFile<ELFT> Obj = createELFObj<ELFT>(this->MB); |
| 700 | for (const Elf_Shdr &Sec : Obj.sections()) { |
| 701 | if (Sec.sh_type != SHT_SYMTAB) |
| 702 | continue; |
| 703 | Elf_Sym_Range Syms = Obj.symbols(&Sec); |
| 704 | uint32_t FirstNonLocal = Sec.sh_info; |
| 705 | StringRef StringTable = check(Obj.getStringTableForSymtab(Sec)); |
| 706 | std::vector<StringRef> V; |
| 707 | for (const Elf_Sym &Sym : Syms.slice(FirstNonLocal)) |
| Rui Ueyama | 1f49289 | 2016-04-08 20:49:31 +0000 | [diff] [blame] | 708 | if (Sym.st_shndx != SHN_UNDEF) |
| 709 | V.push_back(check(Sym.getName(StringTable))); |
| Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 710 | return V; |
| 711 | } |
| 712 | return {}; |
| 713 | } |
| 714 | |
| 715 | std::vector<StringRef> LazyObjectFile::getBitcodeSymbols() { |
| 716 | LLVMContext Context; |
| 717 | std::unique_ptr<IRObjectFile> Obj = |
| 718 | check(IRObjectFile::create(this->MB, Context)); |
| 719 | std::vector<StringRef> V; |
| 720 | for (const BasicSymbolRef &Sym : Obj->symbols()) { |
| Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 721 | uint32_t Flags = Sym.getFlags(); |
| 722 | if (BitcodeFile::shouldSkip(Flags)) |
| Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 723 | continue; |
| Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 724 | if (Flags & BasicSymbolRef::SF_Undefined) |
| Rui Ueyama | 1f49289 | 2016-04-08 20:49:31 +0000 | [diff] [blame] | 725 | continue; |
| Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 726 | SmallString<64> Name; |
| 727 | raw_svector_ostream OS(Name); |
| 728 | Sym.printName(OS); |
| 729 | V.push_back(Saver.save(StringRef(Name))); |
| 730 | } |
| 731 | return V; |
| 732 | } |
| 733 | |
| Rui Ueyama | 1f49289 | 2016-04-08 20:49:31 +0000 | [diff] [blame] | 734 | // Returns a vector of globally-visible defined symbol names. |
| Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 735 | std::vector<StringRef> LazyObjectFile::getSymbols() { |
| Rui Ueyama | 4655ea3 | 2016-04-08 00:14:55 +0000 | [diff] [blame] | 736 | if (isBitcode(this->MB)) |
| Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 737 | return getBitcodeSymbols(); |
| 738 | |
| Rui Ueyama | 4655ea3 | 2016-04-08 00:14:55 +0000 | [diff] [blame] | 739 | unsigned char Size; |
| 740 | unsigned char Endian; |
| 741 | std::tie(Size, Endian) = getElfArchType(this->MB.getBuffer()); |
| 742 | if (Size == ELFCLASS32) { |
| 743 | if (Endian == ELFDATA2LSB) |
| Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 744 | return getElfSymbols<ELF32LE>(); |
| 745 | return getElfSymbols<ELF32BE>(); |
| 746 | } |
| Rui Ueyama | 4655ea3 | 2016-04-08 00:14:55 +0000 | [diff] [blame] | 747 | if (Endian == ELFDATA2LSB) |
| Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 748 | return getElfSymbols<ELF64LE>(); |
| 749 | return getElfSymbols<ELF64BE>(); |
| 750 | } |
| 751 | |
| Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 752 | template void ArchiveFile::parse<ELF32LE>(); |
| 753 | template void ArchiveFile::parse<ELF32BE>(); |
| 754 | template void ArchiveFile::parse<ELF64LE>(); |
| 755 | template void ArchiveFile::parse<ELF64BE>(); |
| 756 | |
| 757 | template void |
| 758 | BitcodeFile::parse<ELF32LE>(llvm::DenseSet<StringRef> &ComdatGroups); |
| 759 | template void |
| 760 | BitcodeFile::parse<ELF32BE>(llvm::DenseSet<StringRef> &ComdatGroups); |
| 761 | template void |
| 762 | BitcodeFile::parse<ELF64LE>(llvm::DenseSet<StringRef> &ComdatGroups); |
| 763 | template void |
| 764 | BitcodeFile::parse<ELF64BE>(llvm::DenseSet<StringRef> &ComdatGroups); |
| 765 | |
| 766 | template void LazyObjectFile::parse<ELF32LE>(); |
| 767 | template void LazyObjectFile::parse<ELF32BE>(); |
| 768 | template void LazyObjectFile::parse<ELF64LE>(); |
| 769 | template void LazyObjectFile::parse<ELF64BE>(); |
| 770 | |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 771 | template class elf::ELFFileBase<ELF32LE>; |
| 772 | template class elf::ELFFileBase<ELF32BE>; |
| 773 | template class elf::ELFFileBase<ELF64LE>; |
| 774 | template class elf::ELFFileBase<ELF64BE>; |
| Davide Italiano | 6d328d3 | 2015-09-16 20:45:57 +0000 | [diff] [blame] | 775 | |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 776 | template class elf::ObjectFile<ELF32LE>; |
| 777 | template class elf::ObjectFile<ELF32BE>; |
| 778 | template class elf::ObjectFile<ELF64LE>; |
| 779 | template class elf::ObjectFile<ELF64BE>; |
| Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 780 | |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 781 | template class elf::SharedFile<ELF32LE>; |
| 782 | template class elf::SharedFile<ELF32BE>; |
| 783 | template class elf::SharedFile<ELF64LE>; |
| 784 | template class elf::SharedFile<ELF64BE>; |