| 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 | 9d06ab6 | 2015-09-22 00:01:39 +0000 | [diff] [blame] | 11 | #include "InputSection.h" |
| Rafael Espindola | 192e1fa | 2015-08-06 15:08:23 +0000 | [diff] [blame] | 12 | #include "Error.h" |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 13 | #include "Symbols.h" |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/STLExtras.h" |
| 15 | |
| Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 16 | using namespace llvm; |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 17 | using namespace llvm::ELF; |
| Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 18 | using namespace llvm::object; |
| Rui Ueyama | f5c4aca | 2015-09-30 17:06:09 +0000 | [diff] [blame] | 19 | using namespace llvm::sys::fs; |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace lld; |
| 22 | using namespace lld::elf2; |
| 23 | |
| Rafael Espindola | e1901cc | 2015-09-24 15:11:50 +0000 | [diff] [blame] | 24 | namespace { |
| 25 | class ECRAII { |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 26 | std::error_code EC; |
| Rafael Espindola | e1901cc | 2015-09-24 15:11:50 +0000 | [diff] [blame] | 27 | |
| 28 | public: |
| 29 | std::error_code &getEC() { return EC; } |
| 30 | ~ECRAII() { error(EC); } |
| 31 | }; |
| Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 34 | template <class ELFT> |
| Rafael Espindola | 2a4b271 | 2015-10-13 01:17:02 +0000 | [diff] [blame] | 35 | ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef M) |
| 36 | : InputFile(K, M), ELFObj(MB.getBuffer(), ECRAII().getEC()) {} |
| Rafael Espindola | e1901cc | 2015-09-24 15:11:50 +0000 | [diff] [blame] | 37 | |
| 38 | template <class ELFT> |
| Rafael Espindola | af70764 | 2015-10-12 01:55:32 +0000 | [diff] [blame] | 39 | typename ELFFileBase<ELFT>::Elf_Sym_Range |
| 40 | ELFFileBase<ELFT>::getSymbolsHelper(bool Local) { |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 41 | if (!Symtab) |
| 42 | return Elf_Sym_Range(nullptr, nullptr); |
| Rafael Espindola | e1901cc | 2015-09-24 15:11:50 +0000 | [diff] [blame] | 43 | Elf_Sym_Range Syms = ELFObj.symbols(Symtab); |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 44 | uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end()); |
| 45 | uint32_t FirstNonLocal = Symtab->sh_info; |
| 46 | if (FirstNonLocal > NumSymbols) |
| 47 | error("Invalid sh_info in symbol table"); |
| Davide Italiano | 6d328d3 | 2015-09-16 20:45:57 +0000 | [diff] [blame] | 48 | if (!Local) |
| Rui Ueyama | 90b3daa | 2015-09-30 02:37:51 +0000 | [diff] [blame] | 49 | return make_range(Syms.begin() + FirstNonLocal, Syms.end()); |
| 50 | // +1 to skip over dummy symbol. |
| 51 | return make_range(Syms.begin() + 1, Syms.begin() + FirstNonLocal); |
| Davide Italiano | 6d328d3 | 2015-09-16 20:45:57 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 54 | template <class ELFT> |
| 55 | uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const { |
| 56 | uint32_t Index = Sym.st_shndx; |
| 57 | if (Index == ELF::SHN_XINDEX) |
| 58 | Index = this->ELFObj.getExtendedSymbolTableIndex(&Sym, this->Symtab, |
| 59 | SymtabSHNDX); |
| 60 | else if (Index == ELF::SHN_UNDEF || Index >= ELF::SHN_LORESERVE) |
| 61 | return 0; |
| 62 | |
| 63 | if (!Index) |
| 64 | error("Invalid section index"); |
| 65 | return Index; |
| 66 | } |
| 67 | |
| Rafael Espindola | af70764 | 2015-10-12 01:55:32 +0000 | [diff] [blame] | 68 | template <class ELFT> void ELFFileBase<ELFT>::initStringTable() { |
| Rafael Espindola | 3e60379 | 2015-10-01 20:26:37 +0000 | [diff] [blame] | 69 | if (!Symtab) |
| 70 | return; |
| Rafael Espindola | e1901cc | 2015-09-24 15:11:50 +0000 | [diff] [blame] | 71 | ErrorOr<StringRef> StringTableOrErr = ELFObj.getStringTableForSymtab(*Symtab); |
| Davide Italiano | 6d328d3 | 2015-09-16 20:45:57 +0000 | [diff] [blame] | 72 | error(StringTableOrErr.getError()); |
| 73 | StringTable = *StringTableOrErr; |
| Rafael Espindola | 6a3b5de | 2015-10-01 19:52:48 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | template <class ELFT> |
| Rafael Espindola | af70764 | 2015-10-12 01:55:32 +0000 | [diff] [blame] | 77 | typename ELFFileBase<ELFT>::Elf_Sym_Range |
| 78 | ELFFileBase<ELFT>::getNonLocalSymbols() { |
| Davide Italiano | 6d328d3 | 2015-09-16 20:45:57 +0000 | [diff] [blame] | 79 | return getSymbolsHelper(false); |
| 80 | } |
| 81 | |
| 82 | template <class ELFT> |
| Rafael Espindola | e1901cc | 2015-09-24 15:11:50 +0000 | [diff] [blame] | 83 | ObjectFile<ELFT>::ObjectFile(MemoryBufferRef M) |
| Rafael Espindola | 2a4b271 | 2015-10-13 01:17:02 +0000 | [diff] [blame] | 84 | : ELFFileBase<ELFT>(Base::ObjectKind, M) {} |
| Rafael Espindola | e1901cc | 2015-09-24 15:11:50 +0000 | [diff] [blame] | 85 | |
| 86 | template <class ELFT> |
| Davide Italiano | 6d328d3 | 2015-09-16 20:45:57 +0000 | [diff] [blame] | 87 | typename ObjectFile<ELFT>::Elf_Sym_Range ObjectFile<ELFT>::getLocalSymbols() { |
| 88 | return this->getSymbolsHelper(true); |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 91 | template <class ELFT> |
| Rui Ueyama | c4aaed9 | 2015-10-22 18:49:53 +0000 | [diff] [blame] | 92 | const typename ObjectFile<ELFT>::Elf_Sym * |
| 93 | ObjectFile<ELFT>::getLocalSymbol(uintX_t SymIndex) { |
| 94 | uint32_t FirstNonLocal = this->Symtab->sh_info; |
| 95 | if (SymIndex >= FirstNonLocal) |
| 96 | return nullptr; |
| 97 | Elf_Sym_Range Syms = this->ELFObj.symbols(this->Symtab); |
| 98 | return Syms.begin() + SymIndex; |
| 99 | } |
| 100 | |
| 101 | template <class ELFT> |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 102 | void elf2::ObjectFile<ELFT>::parse(DenseSet<StringRef> &Comdats) { |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 103 | // Read section and symbol tables. |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 104 | initializeSections(Comdats); |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 105 | initializeSymbols(); |
| 106 | } |
| 107 | |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 108 | template <class ELFT> |
| 109 | StringRef ObjectFile<ELFT>::getShtGroupSignature(const Elf_Shdr &Sec) { |
| 110 | const ELFFile<ELFT> &Obj = this->ELFObj; |
| 111 | uint32_t SymtabdSectionIndex = Sec.sh_link; |
| 112 | ErrorOr<const Elf_Shdr *> SecOrErr = Obj.getSection(SymtabdSectionIndex); |
| 113 | error(SecOrErr); |
| 114 | const Elf_Shdr *SymtabSec = *SecOrErr; |
| 115 | uint32_t SymIndex = Sec.sh_info; |
| 116 | const Elf_Sym *Sym = Obj.getSymbol(SymtabSec, SymIndex); |
| 117 | ErrorOr<StringRef> StringTableOrErr = Obj.getStringTableForSymtab(*SymtabSec); |
| 118 | error(StringTableOrErr); |
| 119 | ErrorOr<StringRef> SignatureOrErr = Sym->getName(*StringTableOrErr); |
| 120 | error(SignatureOrErr); |
| 121 | return *SignatureOrErr; |
| 122 | } |
| 123 | |
| 124 | template <class ELFT> |
| 125 | ArrayRef<typename ObjectFile<ELFT>::GroupEntryType> |
| 126 | ObjectFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) { |
| 127 | const ELFFile<ELFT> &Obj = this->ELFObj; |
| 128 | ErrorOr<ArrayRef<GroupEntryType>> EntriesOrErr = |
| 129 | Obj.template getSectionContentsAsArray<GroupEntryType>(&Sec); |
| 130 | error(EntriesOrErr.getError()); |
| 131 | ArrayRef<GroupEntryType> Entries = *EntriesOrErr; |
| 132 | if (Entries.empty() || Entries[0] != GRP_COMDAT) |
| 133 | error("Unsupported SHT_GROUP format"); |
| 134 | return Entries.slice(1); |
| 135 | } |
| 136 | |
| 137 | template <class ELFT> |
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 138 | static bool shouldMerge(const typename ELFFile<ELFT>::Elf_Shdr &Sec) { |
| 139 | typedef typename ELFFile<ELFT>::uintX_t uintX_t; |
| 140 | uintX_t Flags = Sec.sh_flags; |
| 141 | if (!(Flags & SHF_MERGE)) |
| 142 | return false; |
| 143 | if (Flags & SHF_WRITE) |
| 144 | error("Writable SHF_MERGE sections are not supported"); |
| 145 | uintX_t EntSize = Sec.sh_entsize; |
| 146 | if (Sec.sh_size % EntSize) |
| 147 | error("SHF_MERGE section size must be a multiple of sh_entsize"); |
| 148 | |
| 149 | // Don't try to merge if the aligment is larger than the sh_entsize. |
| 150 | // |
| 151 | // If this is not a SHF_STRINGS, we would need to pad after every entity. It |
| 152 | // would be equivalent for the producer of the .o to just set a larger |
| 153 | // sh_entsize. |
| 154 | // |
| 155 | // If this is a SHF_STRINGS, the larger alignment makes sense. Unfortunately |
| 156 | // it would complicate tail merging. This doesn't seem that common to |
| 157 | // justify the effort. |
| 158 | if (Sec.sh_addralign > EntSize) |
| 159 | return false; |
| 160 | |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | template <class ELFT> |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 165 | void elf2::ObjectFile<ELFT>::initializeSections(DenseSet<StringRef> &Comdats) { |
| Rafael Espindola | e1901cc | 2015-09-24 15:11:50 +0000 | [diff] [blame] | 166 | uint64_t Size = this->ELFObj.getNumSections(); |
| Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 167 | Sections.resize(Size); |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 168 | unsigned I = -1; |
| Rafael Espindola | d42f4e5 | 2015-10-08 12:02:38 +0000 | [diff] [blame] | 169 | const ELFFile<ELFT> &Obj = this->ELFObj; |
| 170 | for (const Elf_Shdr &Sec : Obj.sections()) { |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 171 | ++I; |
| 172 | if (Sections[I] == &InputSection<ELFT>::Discarded) |
| 173 | continue; |
| 174 | |
| Rafael Espindola | cde2513 | 2015-08-13 14:45:44 +0000 | [diff] [blame] | 175 | switch (Sec.sh_type) { |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 176 | case SHT_GROUP: |
| 177 | Sections[I] = &InputSection<ELFT>::Discarded; |
| 178 | if (Comdats.insert(getShtGroupSignature(Sec)).second) |
| 179 | continue; |
| 180 | for (GroupEntryType E : getShtGroupEntries(Sec)) { |
| 181 | uint32_t SecIndex = E; |
| 182 | if (SecIndex >= Size) |
| 183 | error("Invalid section index in group"); |
| 184 | Sections[SecIndex] = &InputSection<ELFT>::Discarded; |
| 185 | } |
| 186 | break; |
| Rafael Espindola | cde2513 | 2015-08-13 14:45:44 +0000 | [diff] [blame] | 187 | case SHT_SYMTAB: |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 188 | this->Symtab = &Sec; |
| Rafael Espindola | cde2513 | 2015-08-13 14:45:44 +0000 | [diff] [blame] | 189 | break; |
| Rafael Espindola | 2034822 | 2015-08-24 21:43:25 +0000 | [diff] [blame] | 190 | case SHT_SYMTAB_SHNDX: { |
| Rafael Espindola | d42f4e5 | 2015-10-08 12:02:38 +0000 | [diff] [blame] | 191 | ErrorOr<ArrayRef<Elf_Word>> ErrorOrTable = Obj.getSHNDXTable(Sec); |
| Rafael Espindola | 2034822 | 2015-08-24 21:43:25 +0000 | [diff] [blame] | 192 | error(ErrorOrTable); |
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 193 | this->SymtabSHNDX = *ErrorOrTable; |
| Rafael Espindola | 2034822 | 2015-08-24 21:43:25 +0000 | [diff] [blame] | 194 | break; |
| 195 | } |
| Rafael Espindola | cde2513 | 2015-08-13 14:45:44 +0000 | [diff] [blame] | 196 | case SHT_STRTAB: |
| 197 | case SHT_NULL: |
| Rafael Espindola | cde2513 | 2015-08-13 14:45:44 +0000 | [diff] [blame] | 198 | break; |
| Michael J. Spencer | 67bc8d6 | 2015-08-27 23:15:56 +0000 | [diff] [blame] | 199 | case SHT_RELA: |
| 200 | case SHT_REL: { |
| 201 | uint32_t RelocatedSectionIndex = Sec.sh_info; |
| 202 | if (RelocatedSectionIndex >= Size) |
| 203 | error("Invalid relocated section index"); |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 204 | InputSectionBase<ELFT> *RelocatedSection = |
| 205 | Sections[RelocatedSectionIndex]; |
| Michael J. Spencer | 67bc8d6 | 2015-08-27 23:15:56 +0000 | [diff] [blame] | 206 | if (!RelocatedSection) |
| 207 | error("Unsupported relocation reference"); |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 208 | if (auto *S = dyn_cast<InputSection<ELFT>>(RelocatedSection)) |
| 209 | S->RelocSections.push_back(&Sec); |
| 210 | else |
| 211 | error("Relocations pointing to SHF_MERGE are not supported"); |
| Michael J. Spencer | 67bc8d6 | 2015-08-27 23:15:56 +0000 | [diff] [blame] | 212 | break; |
| 213 | } |
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 214 | default: |
| 215 | if (shouldMerge<ELFT>(Sec)) |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 216 | Sections[I] = new (this->Alloc) MergeInputSection<ELFT>(this, &Sec); |
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 217 | else |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 218 | Sections[I] = new (this->Alloc) InputSection<ELFT>(this, &Sec); |
| Rafael Espindola | cde2513 | 2015-08-13 14:45:44 +0000 | [diff] [blame] | 219 | break; |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | template <class ELFT> void elf2::ObjectFile<ELFT>::initializeSymbols() { |
| Rafael Espindola | 6a3b5de | 2015-10-01 19:52:48 +0000 | [diff] [blame] | 225 | this->initStringTable(); |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 226 | Elf_Sym_Range Syms = this->getNonLocalSymbols(); |
| Reid Kleckner | f7b85e0 | 2015-08-11 20:06:51 +0000 | [diff] [blame] | 227 | uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end()); |
| Rafael Espindola | af70764 | 2015-10-12 01:55:32 +0000 | [diff] [blame] | 228 | this->SymbolBodies.reserve(NumSymbols); |
| Rafael Espindola | 3031851 | 2015-08-04 14:00:56 +0000 | [diff] [blame] | 229 | for (const Elf_Sym &Sym : Syms) |
| Rafael Espindola | af70764 | 2015-10-12 01:55:32 +0000 | [diff] [blame] | 230 | this->SymbolBodies.push_back(createSymbolBody(this->StringTable, &Sym)); |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | template <class ELFT> |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 234 | InputSectionBase<ELFT> * |
| Rafael Espindola | 4cda581 | 2015-10-16 15:29:48 +0000 | [diff] [blame] | 235 | elf2::ObjectFile<ELFT>::getSection(const Elf_Sym &Sym) const { |
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 236 | uint32_t Index = this->getSectionIndex(Sym); |
| 237 | if (Index == 0) |
| Rafael Espindola | 4cda581 | 2015-10-16 15:29:48 +0000 | [diff] [blame] | 238 | return nullptr; |
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 239 | if (Index >= Sections.size() || !Sections[Index]) |
| Rafael Espindola | 4cda581 | 2015-10-16 15:29:48 +0000 | [diff] [blame] | 240 | error("Invalid section index"); |
| 241 | return Sections[Index]; |
| 242 | } |
| 243 | |
| 244 | template <class ELFT> |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 245 | SymbolBody *elf2::ObjectFile<ELFT>::createSymbolBody(StringRef StringTable, |
| 246 | const Elf_Sym *Sym) { |
| 247 | ErrorOr<StringRef> NameOrErr = Sym->getName(StringTable); |
| 248 | error(NameOrErr.getError()); |
| 249 | StringRef Name = *NameOrErr; |
| Rafael Espindola | 2034822 | 2015-08-24 21:43:25 +0000 | [diff] [blame] | 250 | |
| Rafael Espindola | 4cda581 | 2015-10-16 15:29:48 +0000 | [diff] [blame] | 251 | switch (Sym->st_shndx) { |
| Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 252 | case SHN_ABS: |
| Rafael Espindola | af70764 | 2015-10-12 01:55:32 +0000 | [diff] [blame] | 253 | return new (this->Alloc) DefinedAbsolute<ELFT>(Name, *Sym); |
| Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 254 | case SHN_UNDEF: |
| Rafael Espindola | af70764 | 2015-10-12 01:55:32 +0000 | [diff] [blame] | 255 | return new (this->Alloc) Undefined<ELFT>(Name, *Sym); |
| Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 256 | case SHN_COMMON: |
| Rafael Espindola | af70764 | 2015-10-12 01:55:32 +0000 | [diff] [blame] | 257 | return new (this->Alloc) DefinedCommon<ELFT>(Name, *Sym); |
| Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 258 | } |
| Rafael Espindola | 2034822 | 2015-08-24 21:43:25 +0000 | [diff] [blame] | 259 | |
| Rafael Espindola | b13df65 | 2015-08-11 17:33:02 +0000 | [diff] [blame] | 260 | switch (Sym->getBinding()) { |
| 261 | default: |
| 262 | error("unexpected binding"); |
| 263 | case STB_GLOBAL: |
| Rafael Espindola | 3a63f3f | 2015-08-28 20:19:34 +0000 | [diff] [blame] | 264 | case STB_WEAK: |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 265 | case STB_GNU_UNIQUE: { |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 266 | InputSectionBase<ELFT> *Sec = getSection(*Sym); |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 267 | if (Sec == &InputSection<ELFT>::Discarded) |
| Rafael Espindola | af70764 | 2015-10-12 01:55:32 +0000 | [diff] [blame] | 268 | return new (this->Alloc) Undefined<ELFT>(Name, *Sym); |
| 269 | return new (this->Alloc) DefinedRegular<ELFT>(Name, *Sym, *Sec); |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 270 | } |
| Rafael Espindola | b13df65 | 2015-08-11 17:33:02 +0000 | [diff] [blame] | 271 | } |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| Igor Kudrin | 2696bbe | 2015-10-01 18:02:21 +0000 | [diff] [blame] | 274 | static std::unique_ptr<Archive> openArchive(MemoryBufferRef MB) { |
| Rui Ueyama | d0e5aeb | 2015-09-30 02:42:27 +0000 | [diff] [blame] | 275 | ErrorOr<std::unique_ptr<Archive>> ArchiveOrErr = Archive::create(MB); |
| Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 276 | error(ArchiveOrErr, "Failed to parse archive"); |
| Igor Kudrin | 2696bbe | 2015-10-01 18:02:21 +0000 | [diff] [blame] | 277 | return std::move(*ArchiveOrErr); |
| 278 | } |
| 279 | |
| 280 | void ArchiveFile::parse() { |
| 281 | File = openArchive(MB); |
| Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 282 | |
| 283 | // Allocate a buffer for Lazy objects. |
| 284 | size_t NumSyms = File->getNumberOfSymbols(); |
| 285 | LazySymbols.reserve(NumSyms); |
| 286 | |
| 287 | // Read the symbol table to construct Lazy objects. |
| 288 | for (const Archive::Symbol &Sym : File->symbols()) |
| 289 | LazySymbols.emplace_back(this, Sym); |
| 290 | } |
| 291 | |
| 292 | // Returns a buffer pointing to a member file containing a given symbol. |
| 293 | MemoryBufferRef ArchiveFile::getMember(const Archive::Symbol *Sym) { |
| Rafael Espindola | 8f3a6ae | 2015-11-05 14:40:28 +0000 | [diff] [blame^] | 294 | ErrorOr<Archive::Child> COrErr = Sym->getMember(); |
| 295 | error(COrErr, "Could not get the member for symbol " + Sym->getName()); |
| 296 | const Archive::Child &C = *COrErr; |
| Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 297 | |
| Rafael Espindola | 8f3a6ae | 2015-11-05 14:40:28 +0000 | [diff] [blame^] | 298 | if (!Seen.insert(C.getChildOffset()).second) |
| Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 299 | return MemoryBufferRef(); |
| Michael J. Spencer | 88f0d63 | 2015-09-08 20:36:20 +0000 | [diff] [blame] | 300 | |
| Rafael Espindola | 8f3a6ae | 2015-11-05 14:40:28 +0000 | [diff] [blame^] | 301 | ErrorOr<MemoryBufferRef> Ret = C.getMemoryBufferRef(); |
| Rui Ueyama | 1c42afc | 2015-10-12 15:49:06 +0000 | [diff] [blame] | 302 | error(Ret, "Could not get the buffer for the member defining symbol " + |
| Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 303 | Sym->getName()); |
| 304 | return *Ret; |
| 305 | } |
| 306 | |
| Igor Kudrin | 2696bbe | 2015-10-01 18:02:21 +0000 | [diff] [blame] | 307 | std::vector<MemoryBufferRef> ArchiveFile::getMembers() { |
| 308 | File = openArchive(MB); |
| 309 | |
| 310 | std::vector<MemoryBufferRef> Result; |
| 311 | for (const Archive::Child &Child : File->children()) { |
| 312 | ErrorOr<MemoryBufferRef> MbOrErr = Child.getMemoryBufferRef(); |
| Rui Ueyama | 1c42afc | 2015-10-12 15:49:06 +0000 | [diff] [blame] | 313 | error(MbOrErr, "Could not get the buffer for a child of the archive " + |
| 314 | File->getFileName()); |
| Igor Kudrin | 2696bbe | 2015-10-01 18:02:21 +0000 | [diff] [blame] | 315 | Result.push_back(MbOrErr.get()); |
| 316 | } |
| 317 | return Result; |
| 318 | } |
| 319 | |
| Rafael Espindola | e1901cc | 2015-09-24 15:11:50 +0000 | [diff] [blame] | 320 | template <class ELFT> |
| 321 | SharedFile<ELFT>::SharedFile(MemoryBufferRef M) |
| Rafael Espindola | 2a4b271 | 2015-10-13 01:17:02 +0000 | [diff] [blame] | 322 | : ELFFileBase<ELFT>(Base::SharedKind, M) { |
| Rafael Espindola | dfce5a2 | 2015-10-12 02:22:58 +0000 | [diff] [blame] | 323 | AsNeeded = Config->AsNeeded; |
| 324 | } |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 325 | |
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 326 | template <class ELFT> |
| 327 | const typename ELFFile<ELFT>::Elf_Shdr * |
| 328 | SharedFile<ELFT>::getSection(const Elf_Sym &Sym) const { |
| 329 | uint32_t Index = this->getSectionIndex(Sym); |
| 330 | if (Index == 0) |
| 331 | return nullptr; |
| 332 | ErrorOr<const Elf_Shdr *> Ret = this->ELFObj.getSection(Index); |
| 333 | error(Ret); |
| 334 | return *Ret; |
| 335 | } |
| 336 | |
| Rafael Espindola | 6a3b5de | 2015-10-01 19:52:48 +0000 | [diff] [blame] | 337 | template <class ELFT> void SharedFile<ELFT>::parseSoName() { |
| Rafael Espindola | c8b1581 | 2015-10-01 15:47:50 +0000 | [diff] [blame] | 338 | typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn; |
| 339 | typedef typename ELFFile<ELFT>::uintX_t uintX_t; |
| 340 | const Elf_Shdr *DynamicSec = nullptr; |
| 341 | |
| 342 | const ELFFile<ELFT> Obj = this->ELFObj; |
| 343 | for (const Elf_Shdr &Sec : Obj.sections()) { |
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 344 | switch (Sec.sh_type) { |
| 345 | default: |
| 346 | continue; |
| 347 | case SHT_DYNSYM: |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 348 | this->Symtab = &Sec; |
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 349 | break; |
| 350 | case SHT_DYNAMIC: |
| Rafael Espindola | c8b1581 | 2015-10-01 15:47:50 +0000 | [diff] [blame] | 351 | DynamicSec = &Sec; |
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 352 | break; |
| 353 | case SHT_SYMTAB_SHNDX: { |
| 354 | ErrorOr<ArrayRef<Elf_Word>> ErrorOrTable = Obj.getSHNDXTable(Sec); |
| 355 | error(ErrorOrTable); |
| 356 | this->SymtabSHNDX = *ErrorOrTable; |
| 357 | break; |
| 358 | } |
| 359 | } |
| Rafael Espindola | c8b1581 | 2015-10-01 15:47:50 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| Rafael Espindola | 6a3b5de | 2015-10-01 19:52:48 +0000 | [diff] [blame] | 362 | this->initStringTable(); |
| Rafael Espindola | af70764 | 2015-10-12 01:55:32 +0000 | [diff] [blame] | 363 | this->SoName = this->getName(); |
| Rafael Espindola | c8b1581 | 2015-10-01 15:47:50 +0000 | [diff] [blame] | 364 | |
| Rui Ueyama | 361d8b9 | 2015-10-12 15:49:02 +0000 | [diff] [blame] | 365 | if (!DynamicSec) |
| 366 | return; |
| 367 | auto *Begin = |
| 368 | reinterpret_cast<const Elf_Dyn *>(Obj.base() + DynamicSec->sh_offset); |
| 369 | const Elf_Dyn *End = Begin + DynamicSec->sh_size / sizeof(Elf_Dyn); |
| Rafael Espindola | c8b1581 | 2015-10-01 15:47:50 +0000 | [diff] [blame] | 370 | |
| Rui Ueyama | 361d8b9 | 2015-10-12 15:49:02 +0000 | [diff] [blame] | 371 | for (const Elf_Dyn &Dyn : make_range(Begin, End)) { |
| 372 | if (Dyn.d_tag == DT_SONAME) { |
| 373 | uintX_t Val = Dyn.getVal(); |
| 374 | if (Val >= this->StringTable.size()) |
| 375 | error("Invalid DT_SONAME entry"); |
| 376 | this->SoName = StringRef(this->StringTable.data() + Val); |
| 377 | return; |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 378 | } |
| 379 | } |
| Rafael Espindola | 6a3b5de | 2015-10-01 19:52:48 +0000 | [diff] [blame] | 380 | } |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 381 | |
| Rafael Espindola | 6a3b5de | 2015-10-01 19:52:48 +0000 | [diff] [blame] | 382 | template <class ELFT> void SharedFile<ELFT>::parse() { |
| 383 | Elf_Sym_Range Syms = this->getNonLocalSymbols(); |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 384 | uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end()); |
| 385 | SymbolBodies.reserve(NumSymbols); |
| 386 | for (const Elf_Sym &Sym : Syms) { |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 387 | ErrorOr<StringRef> NameOrErr = Sym.getName(this->StringTable); |
| 388 | error(NameOrErr.getError()); |
| 389 | StringRef Name = *NameOrErr; |
| 390 | |
| Rui Ueyama | f8432d9 | 2015-10-13 16:34:14 +0000 | [diff] [blame] | 391 | if (Sym.isUndefined()) |
| 392 | Undefs.push_back(Name); |
| 393 | else |
| 394 | SymbolBodies.emplace_back(this, Name, Sym); |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 395 | } |
| 396 | } |
| Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 397 | |
| Rui Ueyama | c4b6506 | 2015-10-12 15:31:09 +0000 | [diff] [blame] | 398 | template <typename T> |
| 399 | static std::unique_ptr<InputFile> createELFFileAux(MemoryBufferRef MB) { |
| 400 | std::unique_ptr<T> Ret = llvm::make_unique<T>(MB); |
| 401 | |
| 402 | if (!Config->FirstElf) |
| 403 | Config->FirstElf = Ret.get(); |
| 404 | |
| Rui Ueyama | e717a71 | 2015-10-13 16:20:50 +0000 | [diff] [blame] | 405 | if (Config->EKind == ELFNoneKind) { |
| 406 | Config->EKind = Ret->getELFKind(); |
| Rui Ueyama | c4b6506 | 2015-10-12 15:31:09 +0000 | [diff] [blame] | 407 | Config->EMachine = Ret->getEMachine(); |
| 408 | } |
| 409 | |
| 410 | return std::move(Ret); |
| 411 | } |
| 412 | |
| 413 | template <template <class> class T> |
| 414 | std::unique_ptr<InputFile> lld::elf2::createELFFile(MemoryBufferRef MB) { |
| 415 | using namespace llvm; |
| 416 | |
| 417 | std::pair<unsigned char, unsigned char> Type = |
| 418 | object::getElfArchType(MB.getBuffer()); |
| 419 | if (Type.second != ELF::ELFDATA2LSB && Type.second != ELF::ELFDATA2MSB) |
| 420 | error("Invalid data encoding: " + MB.getBufferIdentifier()); |
| 421 | |
| 422 | if (Type.first == ELF::ELFCLASS32) { |
| 423 | if (Type.second == ELF::ELFDATA2LSB) |
| 424 | return createELFFileAux<T<object::ELF32LE>>(MB); |
| 425 | return createELFFileAux<T<object::ELF32BE>>(MB); |
| 426 | } |
| 427 | if (Type.first == ELF::ELFCLASS64) { |
| 428 | if (Type.second == ELF::ELFDATA2LSB) |
| 429 | return createELFFileAux<T<object::ELF64LE>>(MB); |
| 430 | return createELFFileAux<T<object::ELF64BE>>(MB); |
| 431 | } |
| 432 | error("Invalid file class: " + MB.getBufferIdentifier()); |
| 433 | } |
| 434 | |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 435 | namespace lld { |
| 436 | namespace elf2 { |
| Rafael Espindola | af70764 | 2015-10-12 01:55:32 +0000 | [diff] [blame] | 437 | template class ELFFileBase<llvm::object::ELF32LE>; |
| 438 | template class ELFFileBase<llvm::object::ELF32BE>; |
| 439 | template class ELFFileBase<llvm::object::ELF64LE>; |
| 440 | template class ELFFileBase<llvm::object::ELF64BE>; |
| Davide Italiano | 6d328d3 | 2015-09-16 20:45:57 +0000 | [diff] [blame] | 441 | |
| Rui Ueyama | 2ee8e11 | 2015-10-12 15:27:09 +0000 | [diff] [blame] | 442 | template class ObjectFile<llvm::object::ELF32LE>; |
| 443 | template class ObjectFile<llvm::object::ELF32BE>; |
| 444 | template class ObjectFile<llvm::object::ELF64LE>; |
| 445 | template class ObjectFile<llvm::object::ELF64BE>; |
| Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 446 | |
| Rui Ueyama | 2ee8e11 | 2015-10-12 15:27:09 +0000 | [diff] [blame] | 447 | template class SharedFile<llvm::object::ELF32LE>; |
| 448 | template class SharedFile<llvm::object::ELF32BE>; |
| 449 | template class SharedFile<llvm::object::ELF64LE>; |
| 450 | template class SharedFile<llvm::object::ELF64BE>; |
| Rui Ueyama | c4b6506 | 2015-10-12 15:31:09 +0000 | [diff] [blame] | 451 | |
| 452 | template std::unique_ptr<InputFile> createELFFile<ObjectFile>(MemoryBufferRef); |
| 453 | template std::unique_ptr<InputFile> createELFFile<SharedFile>(MemoryBufferRef); |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 454 | } |
| 455 | } |