| 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 | 192e1fa | 2015-08-06 15:08:23 +0000 | [diff] [blame] | 11 | #include "Error.h" |
| Rafael Espindola | 9d13d04 | 2016-02-11 15:24:48 +0000 | [diff] [blame] | 12 | #include "InputSection.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" |
| Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 15 | #include "llvm/IR/LLVMContext.h" |
| Rafael Espindola | 4de44b7 | 2016-03-02 15:43:50 +0000 | [diff] [blame] | 16 | #include "llvm/IR/Module.h" |
| Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 17 | #include "llvm/Object/IRObjectFile.h" |
| 18 | #include "llvm/Support/raw_ostream.h" |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 19 | |
| Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 21 | using namespace llvm::ELF; |
| Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 22 | using namespace llvm::object; |
| Rui Ueyama | f5c4aca | 2015-09-30 17:06:09 +0000 | [diff] [blame] | 23 | using namespace llvm::sys::fs; |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace lld; |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 26 | using namespace lld::elf; |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 27 | |
| Rui Ueyama | eb3413e | 2016-03-03 06:22:29 +0000 | [diff] [blame] | 28 | template <class ELFT> |
| 29 | static ELFFile<ELFT> createELFObj(MemoryBufferRef MB) { |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 30 | std::error_code EC; |
| Rui Ueyama | eb3413e | 2016-03-03 06:22:29 +0000 | [diff] [blame] | 31 | ELFFile<ELFT> F(MB.getBuffer(), EC); |
| 32 | fatal(EC); |
| 33 | return F; |
| Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 36 | template <class ELFT> |
| Rui Ueyama | eb3413e | 2016-03-03 06:22:29 +0000 | [diff] [blame] | 37 | ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB) |
| 38 | : InputFile(K, MB), ELFObj(createELFObj<ELFT>(MB)) {} |
| Rafael Espindola | e1901cc | 2015-09-24 15:11:50 +0000 | [diff] [blame] | 39 | |
| 40 | template <class ELFT> |
| Rui Ueyama | 2022e81 | 2015-11-20 02:10:52 +0000 | [diff] [blame] | 41 | ELFKind ELFFileBase<ELFT>::getELFKind() { |
| Rui Ueyama | f588ac4 | 2016-01-06 00:09:41 +0000 | [diff] [blame] | 42 | if (ELFT::TargetEndianness == support::little) |
| 43 | return ELFT::Is64Bits ? ELF64LEKind : ELF32LEKind; |
| 44 | return ELFT::Is64Bits ? ELF64BEKind : ELF32BEKind; |
| Rui Ueyama | 2022e81 | 2015-11-20 02:10:52 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | template <class ELFT> |
| Rafael Espindola | af70764 | 2015-10-12 01:55:32 +0000 | [diff] [blame] | 48 | typename ELFFileBase<ELFT>::Elf_Sym_Range |
| 49 | ELFFileBase<ELFT>::getSymbolsHelper(bool Local) { |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 50 | if (!Symtab) |
| 51 | return Elf_Sym_Range(nullptr, nullptr); |
| Rafael Espindola | e1901cc | 2015-09-24 15:11:50 +0000 | [diff] [blame] | 52 | Elf_Sym_Range Syms = ELFObj.symbols(Symtab); |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 53 | uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end()); |
| 54 | uint32_t FirstNonLocal = Symtab->sh_info; |
| 55 | if (FirstNonLocal > NumSymbols) |
| Rui Ueyama | 64cfffd | 2016-01-28 18:40:06 +0000 | [diff] [blame] | 56 | fatal("Invalid sh_info in symbol table"); |
| Davide Italiano | 6d328d3 | 2015-09-16 20:45:57 +0000 | [diff] [blame] | 57 | if (!Local) |
| Rui Ueyama | 90b3daa | 2015-09-30 02:37:51 +0000 | [diff] [blame] | 58 | return make_range(Syms.begin() + FirstNonLocal, Syms.end()); |
| 59 | // +1 to skip over dummy symbol. |
| 60 | return make_range(Syms.begin() + 1, Syms.begin() + FirstNonLocal); |
| Davide Italiano | 6d328d3 | 2015-09-16 20:45:57 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 63 | template <class ELFT> |
| 64 | uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const { |
| Rui Ueyama | dc8d3a2 | 2015-12-24 08:36:56 +0000 | [diff] [blame] | 65 | uint32_t I = Sym.st_shndx; |
| 66 | if (I == ELF::SHN_XINDEX) |
| Rui Ueyama | e69ab10 | 2016-01-06 01:14:11 +0000 | [diff] [blame] | 67 | return ELFObj.getExtendedSymbolTableIndex(&Sym, Symtab, SymtabSHNDX); |
| Rafael Espindola | 02ce26a | 2015-12-24 14:22:24 +0000 | [diff] [blame] | 68 | if (I >= ELF::SHN_LORESERVE || I == ELF::SHN_ABS) |
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 69 | return 0; |
| Rui Ueyama | dc8d3a2 | 2015-12-24 08:36:56 +0000 | [diff] [blame] | 70 | return I; |
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| Rafael Espindola | af70764 | 2015-10-12 01:55:32 +0000 | [diff] [blame] | 73 | template <class ELFT> void ELFFileBase<ELFT>::initStringTable() { |
| Rafael Espindola | 3e60379 | 2015-10-01 20:26:37 +0000 | [diff] [blame] | 74 | if (!Symtab) |
| 75 | return; |
| Rafael Espindola | 1130935c | 2016-03-03 16:21:44 +0000 | [diff] [blame^] | 76 | StringTable = fatal(ELFObj.getStringTableForSymtab(*Symtab)); |
| Rafael Espindola | 6a3b5de | 2015-10-01 19:52:48 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | template <class ELFT> |
| Rafael Espindola | af70764 | 2015-10-12 01:55:32 +0000 | [diff] [blame] | 80 | typename ELFFileBase<ELFT>::Elf_Sym_Range |
| 81 | ELFFileBase<ELFT>::getNonLocalSymbols() { |
| Davide Italiano | 6d328d3 | 2015-09-16 20:45:57 +0000 | [diff] [blame] | 82 | return getSymbolsHelper(false); |
| 83 | } |
| 84 | |
| 85 | template <class ELFT> |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 86 | elf::ObjectFile<ELFT>::ObjectFile(MemoryBufferRef M) |
| Rafael Espindola | 2a4b271 | 2015-10-13 01:17:02 +0000 | [diff] [blame] | 87 | : ELFFileBase<ELFT>(Base::ObjectKind, M) {} |
| Rafael Espindola | e1901cc | 2015-09-24 15:11:50 +0000 | [diff] [blame] | 88 | |
| 89 | template <class ELFT> |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 90 | typename elf::ObjectFile<ELFT>::Elf_Sym_Range |
| 91 | elf::ObjectFile<ELFT>::getLocalSymbols() { |
| Davide Italiano | 6d328d3 | 2015-09-16 20:45:57 +0000 | [diff] [blame] | 92 | return this->getSymbolsHelper(true); |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 95 | template <class ELFT> uint32_t elf::ObjectFile<ELFT>::getMipsGp0() const { |
| Rui Ueyama | 70eed36 | 2016-01-06 22:42:43 +0000 | [diff] [blame] | 96 | if (MipsReginfo) |
| 97 | return MipsReginfo->Reginfo->ri_gp_value; |
| 98 | return 0; |
| Simon Atanasyan | 57830b6 | 2015-12-25 13:02:13 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 101 | template <class ELFT> |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 102 | const typename elf::ObjectFile<ELFT>::Elf_Sym * |
| 103 | elf::ObjectFile<ELFT>::getLocalSymbol(uintX_t SymIndex) { |
| Rui Ueyama | c4aaed9 | 2015-10-22 18:49:53 +0000 | [diff] [blame] | 104 | uint32_t FirstNonLocal = this->Symtab->sh_info; |
| 105 | if (SymIndex >= FirstNonLocal) |
| 106 | return nullptr; |
| 107 | Elf_Sym_Range Syms = this->ELFObj.symbols(this->Symtab); |
| 108 | return Syms.begin() + SymIndex; |
| 109 | } |
| 110 | |
| 111 | template <class ELFT> |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 112 | void elf::ObjectFile<ELFT>::parse(DenseSet<StringRef> &ComdatGroups) { |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 113 | // Read section and symbol tables. |
| Rui Ueyama | 52d3b67 | 2016-01-06 02:06:33 +0000 | [diff] [blame] | 114 | initializeSections(ComdatGroups); |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 115 | initializeSymbols(); |
| 116 | } |
| 117 | |
| Rui Ueyama | 3f11c8c | 2015-12-24 08:41:12 +0000 | [diff] [blame] | 118 | // Sections with SHT_GROUP and comdat bits define comdat section groups. |
| 119 | // They are identified and deduplicated by group name. This function |
| 120 | // returns a group name. |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 121 | template <class ELFT> |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 122 | StringRef elf::ObjectFile<ELFT>::getShtGroupSignature(const Elf_Shdr &Sec) { |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 123 | const ELFFile<ELFT> &Obj = this->ELFObj; |
| 124 | uint32_t SymtabdSectionIndex = Sec.sh_link; |
| Rafael Espindola | 1130935c | 2016-03-03 16:21:44 +0000 | [diff] [blame^] | 125 | const Elf_Shdr *SymtabSec = fatal(Obj.getSection(SymtabdSectionIndex)); |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 126 | uint32_t SymIndex = Sec.sh_info; |
| 127 | const Elf_Sym *Sym = Obj.getSymbol(SymtabSec, SymIndex); |
| Rafael Espindola | 1130935c | 2016-03-03 16:21:44 +0000 | [diff] [blame^] | 128 | StringRef StringTable = fatal(Obj.getStringTableForSymtab(*SymtabSec)); |
| 129 | return fatal(Sym->getName(StringTable)); |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | template <class ELFT> |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 133 | ArrayRef<typename elf::ObjectFile<ELFT>::uint32_X> |
| 134 | elf::ObjectFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) { |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 135 | const ELFFile<ELFT> &Obj = this->ELFObj; |
| Rafael Espindola | 1130935c | 2016-03-03 16:21:44 +0000 | [diff] [blame^] | 136 | ArrayRef<uint32_X> Entries = |
| 137 | fatal(Obj.template getSectionContentsAsArray<uint32_X>(&Sec)); |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 138 | if (Entries.empty() || Entries[0] != GRP_COMDAT) |
| Rui Ueyama | 64cfffd | 2016-01-28 18:40:06 +0000 | [diff] [blame] | 139 | fatal("Unsupported SHT_GROUP format"); |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 140 | return Entries.slice(1); |
| 141 | } |
| 142 | |
| 143 | template <class ELFT> |
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 144 | static bool shouldMerge(const typename ELFFile<ELFT>::Elf_Shdr &Sec) { |
| 145 | typedef typename ELFFile<ELFT>::uintX_t uintX_t; |
| 146 | uintX_t Flags = Sec.sh_flags; |
| 147 | if (!(Flags & SHF_MERGE)) |
| 148 | return false; |
| 149 | if (Flags & SHF_WRITE) |
| Rui Ueyama | 64cfffd | 2016-01-28 18:40:06 +0000 | [diff] [blame] | 150 | fatal("Writable SHF_MERGE sections are not supported"); |
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 151 | uintX_t EntSize = Sec.sh_entsize; |
| George Rimar | 564da7e | 2015-11-09 08:40:44 +0000 | [diff] [blame] | 152 | if (!EntSize || Sec.sh_size % EntSize) |
| Rui Ueyama | 64cfffd | 2016-01-28 18:40:06 +0000 | [diff] [blame] | 153 | fatal("SHF_MERGE section size must be a multiple of sh_entsize"); |
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 154 | |
| Rafael Espindola | 7efa5be | 2016-02-19 14:17:40 +0000 | [diff] [blame] | 155 | // Don't try to merge if the aligment is larger than the sh_entsize and this |
| 156 | // is not SHF_STRINGS. |
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 157 | // |
| Rafael Espindola | 7efa5be | 2016-02-19 14:17:40 +0000 | [diff] [blame] | 158 | // Since this is not a SHF_STRINGS, we would need to pad after every entity. |
| 159 | // 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] | 160 | // sh_entsize. |
| Rafael Espindola | 7efa5be | 2016-02-19 14:17:40 +0000 | [diff] [blame] | 161 | if (Flags & SHF_STRINGS) |
| 162 | return true; |
| 163 | |
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 164 | if (Sec.sh_addralign > EntSize) |
| 165 | return false; |
| 166 | |
| 167 | return true; |
| 168 | } |
| 169 | |
| 170 | template <class ELFT> |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 171 | void elf::ObjectFile<ELFT>::initializeSections( |
| Rafael Espindola | f1d598c | 2016-02-12 21:17:10 +0000 | [diff] [blame] | 172 | DenseSet<StringRef> &ComdatGroups) { |
| Rafael Espindola | e1901cc | 2015-09-24 15:11:50 +0000 | [diff] [blame] | 173 | uint64_t Size = this->ELFObj.getNumSections(); |
| Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 174 | Sections.resize(Size); |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 175 | unsigned I = -1; |
| Rafael Espindola | d42f4e5 | 2015-10-08 12:02:38 +0000 | [diff] [blame] | 176 | const ELFFile<ELFT> &Obj = this->ELFObj; |
| 177 | for (const Elf_Shdr &Sec : Obj.sections()) { |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 178 | ++I; |
| Rui Ueyama | 733153d | 2016-02-24 18:33:35 +0000 | [diff] [blame] | 179 | if (Sections[I] == InputSection<ELFT>::Discarded) |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 180 | continue; |
| 181 | |
| Rafael Espindola | cde2513 | 2015-08-13 14:45:44 +0000 | [diff] [blame] | 182 | switch (Sec.sh_type) { |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 183 | case SHT_GROUP: |
| Rui Ueyama | 733153d | 2016-02-24 18:33:35 +0000 | [diff] [blame] | 184 | Sections[I] = InputSection<ELFT>::Discarded; |
| Rui Ueyama | 52d3b67 | 2016-01-06 02:06:33 +0000 | [diff] [blame] | 185 | if (ComdatGroups.insert(getShtGroupSignature(Sec)).second) |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 186 | continue; |
| Rui Ueyama | 33b3f21 | 2016-01-06 20:30:02 +0000 | [diff] [blame] | 187 | for (uint32_t SecIndex : getShtGroupEntries(Sec)) { |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 188 | if (SecIndex >= Size) |
| Rui Ueyama | 64cfffd | 2016-01-28 18:40:06 +0000 | [diff] [blame] | 189 | fatal("Invalid section index in group"); |
| Rui Ueyama | 733153d | 2016-02-24 18:33:35 +0000 | [diff] [blame] | 190 | Sections[SecIndex] = InputSection<ELFT>::Discarded; |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 191 | } |
| 192 | break; |
| Rafael Espindola | cde2513 | 2015-08-13 14:45:44 +0000 | [diff] [blame] | 193 | case SHT_SYMTAB: |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 194 | this->Symtab = &Sec; |
| Rafael Espindola | cde2513 | 2015-08-13 14:45:44 +0000 | [diff] [blame] | 195 | break; |
| Rafael Espindola | 1130935c | 2016-03-03 16:21:44 +0000 | [diff] [blame^] | 196 | case SHT_SYMTAB_SHNDX: |
| 197 | this->SymtabSHNDX = fatal(Obj.getSHNDXTable(Sec)); |
| Rafael Espindola | 2034822 | 2015-08-24 21:43:25 +0000 | [diff] [blame] | 198 | break; |
| Rafael Espindola | cde2513 | 2015-08-13 14:45:44 +0000 | [diff] [blame] | 199 | case SHT_STRTAB: |
| 200 | case SHT_NULL: |
| Rafael Espindola | cde2513 | 2015-08-13 14:45:44 +0000 | [diff] [blame] | 201 | break; |
| Michael J. Spencer | 67bc8d6 | 2015-08-27 23:15:56 +0000 | [diff] [blame] | 202 | case SHT_RELA: |
| 203 | case SHT_REL: { |
| 204 | uint32_t RelocatedSectionIndex = Sec.sh_info; |
| 205 | if (RelocatedSectionIndex >= Size) |
| Rui Ueyama | 64cfffd | 2016-01-28 18:40:06 +0000 | [diff] [blame] | 206 | fatal("Invalid relocated section index"); |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 207 | InputSectionBase<ELFT> *RelocatedSection = |
| 208 | Sections[RelocatedSectionIndex]; |
| Sean Silva | 09247f8 | 2016-02-04 21:41:07 +0000 | [diff] [blame] | 209 | // Strictly speaking, a relocation section must be included in the |
| 210 | // group of the section it relocates. However, LLVM 3.3 and earlier |
| 211 | // would fail to do so, so we gracefully handle that case. |
| Rui Ueyama | 733153d | 2016-02-24 18:33:35 +0000 | [diff] [blame] | 212 | if (RelocatedSection == InputSection<ELFT>::Discarded) |
| Sean Silva | 09247f8 | 2016-02-04 21:41:07 +0000 | [diff] [blame] | 213 | continue; |
| Michael J. Spencer | 67bc8d6 | 2015-08-27 23:15:56 +0000 | [diff] [blame] | 214 | if (!RelocatedSection) |
| Rui Ueyama | 64cfffd | 2016-01-28 18:40:06 +0000 | [diff] [blame] | 215 | fatal("Unsupported relocation reference"); |
| George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 216 | if (Config->Relocatable) { |
| 217 | // For -r, relocation sections are handled as regular input sections. |
| 218 | Sections[I] = new (Alloc) InputSection<ELFT>(this, &Sec); |
| 219 | } else if (auto *S = dyn_cast<InputSection<ELFT>>(RelocatedSection)) { |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 220 | S->RelocSections.push_back(&Sec); |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 221 | } else if (auto *S = dyn_cast<EHInputSection<ELFT>>(RelocatedSection)) { |
| 222 | if (S->RelocSection) |
| Rui Ueyama | 64cfffd | 2016-01-28 18:40:06 +0000 | [diff] [blame] | 223 | fatal("Multiple relocation sections to .eh_frame are not supported"); |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 224 | S->RelocSection = &Sec; |
| 225 | } else { |
| Rui Ueyama | 64cfffd | 2016-01-28 18:40:06 +0000 | [diff] [blame] | 226 | fatal("Relocations pointing to SHF_MERGE are not supported"); |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 227 | } |
| Michael J. Spencer | 67bc8d6 | 2015-08-27 23:15:56 +0000 | [diff] [blame] | 228 | break; |
| 229 | } |
| Rui Ueyama | e79b09a | 2015-11-21 22:19:32 +0000 | [diff] [blame] | 230 | default: |
| Rui Ueyama | 3f11c8c | 2015-12-24 08:41:12 +0000 | [diff] [blame] | 231 | Sections[I] = createInputSection(Sec); |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| Rafael Espindola | f1d598c | 2016-02-12 21:17:10 +0000 | [diff] [blame] | 236 | template <class ELFT> |
| 237 | InputSectionBase<ELFT> * |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 238 | elf::ObjectFile<ELFT>::createInputSection(const Elf_Shdr &Sec) { |
| Rafael Espindola | 1130935c | 2016-03-03 16:21:44 +0000 | [diff] [blame^] | 239 | StringRef Name = fatal(this->ELFObj.getSectionName(&Sec)); |
| Rui Ueyama | 3f11c8c | 2015-12-24 08:41:12 +0000 | [diff] [blame] | 240 | |
| 241 | // .note.GNU-stack is a marker section to control the presence of |
| 242 | // PT_GNU_STACK segment in outputs. Since the presence of the segment |
| 243 | // is controlled only by the command line option (-z execstack) in LLD, |
| 244 | // .note.GNU-stack is ignored. |
| 245 | if (Name == ".note.GNU-stack") |
| Rui Ueyama | 733153d | 2016-02-24 18:33:35 +0000 | [diff] [blame] | 246 | return InputSection<ELFT>::Discarded; |
| Rui Ueyama | 3f11c8c | 2015-12-24 08:41:12 +0000 | [diff] [blame] | 247 | |
| 248 | // A MIPS object file has a special section that contains register |
| 249 | // usage info, which needs to be handled by the linker specially. |
| Simon Atanasyan | 57830b6 | 2015-12-25 13:02:13 +0000 | [diff] [blame] | 250 | if (Config->EMachine == EM_MIPS && Name == ".reginfo") { |
| Rui Ueyama | e69ab10 | 2016-01-06 01:14:11 +0000 | [diff] [blame] | 251 | MipsReginfo = new (Alloc) MipsReginfoInputSection<ELFT>(this, &Sec); |
| Simon Atanasyan | 57830b6 | 2015-12-25 13:02:13 +0000 | [diff] [blame] | 252 | return MipsReginfo; |
| 253 | } |
| Rui Ueyama | 3f11c8c | 2015-12-24 08:41:12 +0000 | [diff] [blame] | 254 | |
| George Rimar | 4cfe572 | 2016-03-03 07:49:35 +0000 | [diff] [blame] | 255 | // We dont need special handling of .eh_frame sections if relocatable |
| 256 | // output was choosen. Proccess them as usual input sections. |
| 257 | if (!Config->Relocatable && Name == ".eh_frame") |
| Rui Ueyama | e69ab10 | 2016-01-06 01:14:11 +0000 | [diff] [blame] | 258 | return new (EHAlloc.Allocate()) EHInputSection<ELFT>(this, &Sec); |
| Rui Ueyama | 3f11c8c | 2015-12-24 08:41:12 +0000 | [diff] [blame] | 259 | if (shouldMerge<ELFT>(Sec)) |
| Rui Ueyama | e69ab10 | 2016-01-06 01:14:11 +0000 | [diff] [blame] | 260 | return new (MAlloc.Allocate()) MergeInputSection<ELFT>(this, &Sec); |
| 261 | return new (Alloc) InputSection<ELFT>(this, &Sec); |
| Rui Ueyama | 3f11c8c | 2015-12-24 08:41:12 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 264 | template <class ELFT> void elf::ObjectFile<ELFT>::initializeSymbols() { |
| Rafael Espindola | 6a3b5de | 2015-10-01 19:52:48 +0000 | [diff] [blame] | 265 | this->initStringTable(); |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 266 | Elf_Sym_Range Syms = this->getNonLocalSymbols(); |
| Reid Kleckner | f7b85e0 | 2015-08-11 20:06:51 +0000 | [diff] [blame] | 267 | uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end()); |
| Rui Ueyama | e69ab10 | 2016-01-06 01:14:11 +0000 | [diff] [blame] | 268 | SymbolBodies.reserve(NumSymbols); |
| Rafael Espindola | 3031851 | 2015-08-04 14:00:56 +0000 | [diff] [blame] | 269 | for (const Elf_Sym &Sym : Syms) |
| Rui Ueyama | c5e372d | 2016-01-21 02:10:12 +0000 | [diff] [blame] | 270 | SymbolBodies.push_back(createSymbolBody(&Sym)); |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | template <class ELFT> |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 274 | InputSectionBase<ELFT> * |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 275 | elf::ObjectFile<ELFT>::getSection(const Elf_Sym &Sym) const { |
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 276 | uint32_t Index = this->getSectionIndex(Sym); |
| 277 | if (Index == 0) |
| Rafael Espindola | 4cda581 | 2015-10-16 15:29:48 +0000 | [diff] [blame] | 278 | return nullptr; |
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 279 | if (Index >= Sections.size() || !Sections[Index]) |
| Rui Ueyama | 64cfffd | 2016-01-28 18:40:06 +0000 | [diff] [blame] | 280 | fatal("Invalid section index"); |
| Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 281 | InputSectionBase<ELFT> *S = Sections[Index]; |
| 282 | if (S == InputSectionBase<ELFT>::Discarded) |
| 283 | return S; |
| 284 | return S->Repl; |
| Rafael Espindola | 4cda581 | 2015-10-16 15:29:48 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | template <class ELFT> |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 288 | SymbolBody *elf::ObjectFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) { |
| Rafael Espindola | 1130935c | 2016-03-03 16:21:44 +0000 | [diff] [blame^] | 289 | StringRef Name = fatal(Sym->getName(this->StringTable)); |
| Rafael Espindola | 2034822 | 2015-08-24 21:43:25 +0000 | [diff] [blame] | 290 | |
| Rafael Espindola | 4cda581 | 2015-10-16 15:29:48 +0000 | [diff] [blame] | 291 | switch (Sym->st_shndx) { |
| Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 292 | case SHN_UNDEF: |
| Rui Ueyama | e69ab10 | 2016-01-06 01:14:11 +0000 | [diff] [blame] | 293 | return new (Alloc) UndefinedElf<ELFT>(Name, *Sym); |
| Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 294 | case SHN_COMMON: |
| Rui Ueyama | e69ab10 | 2016-01-06 01:14:11 +0000 | [diff] [blame] | 295 | return new (Alloc) DefinedCommon(Name, Sym->st_size, Sym->st_value, |
| 296 | Sym->getBinding() == llvm::ELF::STB_WEAK, |
| 297 | Sym->getVisibility()); |
| Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 298 | } |
| Rafael Espindola | 2034822 | 2015-08-24 21:43:25 +0000 | [diff] [blame] | 299 | |
| Rafael Espindola | b13df65 | 2015-08-11 17:33:02 +0000 | [diff] [blame] | 300 | switch (Sym->getBinding()) { |
| 301 | default: |
| Rui Ueyama | 64cfffd | 2016-01-28 18:40:06 +0000 | [diff] [blame] | 302 | fatal("unexpected binding"); |
| Rafael Espindola | b13df65 | 2015-08-11 17:33:02 +0000 | [diff] [blame] | 303 | case STB_GLOBAL: |
| Rafael Espindola | 3a63f3f | 2015-08-28 20:19:34 +0000 | [diff] [blame] | 304 | case STB_WEAK: |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 305 | case STB_GNU_UNIQUE: { |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 306 | InputSectionBase<ELFT> *Sec = getSection(*Sym); |
| Rui Ueyama | 733153d | 2016-02-24 18:33:35 +0000 | [diff] [blame] | 307 | if (Sec == InputSection<ELFT>::Discarded) |
| Rui Ueyama | e69ab10 | 2016-01-06 01:14:11 +0000 | [diff] [blame] | 308 | return new (Alloc) UndefinedElf<ELFT>(Name, *Sym); |
| 309 | return new (Alloc) DefinedRegular<ELFT>(Name, *Sym, Sec); |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 310 | } |
| Rafael Espindola | b13df65 | 2015-08-11 17:33:02 +0000 | [diff] [blame] | 311 | } |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| Igor Kudrin | 2696bbe | 2015-10-01 18:02:21 +0000 | [diff] [blame] | 314 | void ArchiveFile::parse() { |
| Rafael Espindola | 1130935c | 2016-03-03 16:21:44 +0000 | [diff] [blame^] | 315 | File = fatal(Archive::create(MB), "Failed to parse archive"); |
| Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 316 | |
| 317 | // Allocate a buffer for Lazy objects. |
| 318 | size_t NumSyms = File->getNumberOfSymbols(); |
| 319 | LazySymbols.reserve(NumSyms); |
| 320 | |
| 321 | // Read the symbol table to construct Lazy objects. |
| 322 | for (const Archive::Symbol &Sym : File->symbols()) |
| 323 | LazySymbols.emplace_back(this, Sym); |
| 324 | } |
| 325 | |
| 326 | // Returns a buffer pointing to a member file containing a given symbol. |
| 327 | MemoryBufferRef ArchiveFile::getMember(const Archive::Symbol *Sym) { |
| Rafael Espindola | 1130935c | 2016-03-03 16:21:44 +0000 | [diff] [blame^] | 328 | Archive::Child C = |
| 329 | fatal(Sym->getMember(), |
| 330 | "Could not get the member for symbol " + Sym->getName()); |
| Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 331 | |
| Rafael Espindola | 8f3a6ae | 2015-11-05 14:40:28 +0000 | [diff] [blame] | 332 | if (!Seen.insert(C.getChildOffset()).second) |
| Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 333 | return MemoryBufferRef(); |
| Michael J. Spencer | 88f0d63 | 2015-09-08 20:36:20 +0000 | [diff] [blame] | 334 | |
| Rafael Espindola | 1130935c | 2016-03-03 16:21:44 +0000 | [diff] [blame^] | 335 | return fatal(C.getMemoryBufferRef(), |
| 336 | "Could not get the buffer for the member defining symbol " + |
| 337 | Sym->getName()); |
| Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| Rafael Espindola | e1901cc | 2015-09-24 15:11:50 +0000 | [diff] [blame] | 340 | template <class ELFT> |
| 341 | SharedFile<ELFT>::SharedFile(MemoryBufferRef M) |
| Rui Ueyama | f588ac4 | 2016-01-06 00:09:41 +0000 | [diff] [blame] | 342 | : ELFFileBase<ELFT>(Base::SharedKind, M), AsNeeded(Config->AsNeeded) {} |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 343 | |
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 344 | template <class ELFT> |
| 345 | const typename ELFFile<ELFT>::Elf_Shdr * |
| 346 | SharedFile<ELFT>::getSection(const Elf_Sym &Sym) const { |
| 347 | uint32_t Index = this->getSectionIndex(Sym); |
| 348 | if (Index == 0) |
| 349 | return nullptr; |
| Rafael Espindola | 1130935c | 2016-03-03 16:21:44 +0000 | [diff] [blame^] | 350 | return fatal(this->ELFObj.getSection(Index)); |
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| Rui Ueyama | 7c71331 | 2016-01-06 01:56:36 +0000 | [diff] [blame] | 353 | // Partially parse the shared object file so that we can call |
| 354 | // getSoName on this object. |
| Rafael Espindola | 6a3b5de | 2015-10-01 19:52:48 +0000 | [diff] [blame] | 355 | template <class ELFT> void SharedFile<ELFT>::parseSoName() { |
| Rafael Espindola | c8b1581 | 2015-10-01 15:47:50 +0000 | [diff] [blame] | 356 | typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn; |
| 357 | typedef typename ELFFile<ELFT>::uintX_t uintX_t; |
| 358 | const Elf_Shdr *DynamicSec = nullptr; |
| 359 | |
| 360 | const ELFFile<ELFT> Obj = this->ELFObj; |
| 361 | for (const Elf_Shdr &Sec : Obj.sections()) { |
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 362 | switch (Sec.sh_type) { |
| 363 | default: |
| 364 | continue; |
| 365 | case SHT_DYNSYM: |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 366 | this->Symtab = &Sec; |
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 367 | break; |
| 368 | case SHT_DYNAMIC: |
| Rafael Espindola | c8b1581 | 2015-10-01 15:47:50 +0000 | [diff] [blame] | 369 | DynamicSec = &Sec; |
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 370 | break; |
| Rafael Espindola | 1130935c | 2016-03-03 16:21:44 +0000 | [diff] [blame^] | 371 | case SHT_SYMTAB_SHNDX: |
| 372 | this->SymtabSHNDX = fatal(Obj.getSHNDXTable(Sec)); |
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 373 | break; |
| 374 | } |
| Rafael Espindola | c8b1581 | 2015-10-01 15:47:50 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| Rafael Espindola | 6a3b5de | 2015-10-01 19:52:48 +0000 | [diff] [blame] | 377 | this->initStringTable(); |
| Rui Ueyama | e69ab10 | 2016-01-06 01:14:11 +0000 | [diff] [blame] | 378 | SoName = this->getName(); |
| Rafael Espindola | c8b1581 | 2015-10-01 15:47:50 +0000 | [diff] [blame] | 379 | |
| Rui Ueyama | 361d8b9 | 2015-10-12 15:49:02 +0000 | [diff] [blame] | 380 | if (!DynamicSec) |
| 381 | return; |
| 382 | auto *Begin = |
| 383 | reinterpret_cast<const Elf_Dyn *>(Obj.base() + DynamicSec->sh_offset); |
| 384 | const Elf_Dyn *End = Begin + DynamicSec->sh_size / sizeof(Elf_Dyn); |
| Rafael Espindola | c8b1581 | 2015-10-01 15:47:50 +0000 | [diff] [blame] | 385 | |
| Rui Ueyama | 361d8b9 | 2015-10-12 15:49:02 +0000 | [diff] [blame] | 386 | for (const Elf_Dyn &Dyn : make_range(Begin, End)) { |
| 387 | if (Dyn.d_tag == DT_SONAME) { |
| 388 | uintX_t Val = Dyn.getVal(); |
| 389 | if (Val >= this->StringTable.size()) |
| Rui Ueyama | 64cfffd | 2016-01-28 18:40:06 +0000 | [diff] [blame] | 390 | fatal("Invalid DT_SONAME entry"); |
| Rui Ueyama | e69ab10 | 2016-01-06 01:14:11 +0000 | [diff] [blame] | 391 | SoName = StringRef(this->StringTable.data() + Val); |
| Rui Ueyama | 361d8b9 | 2015-10-12 15:49:02 +0000 | [diff] [blame] | 392 | return; |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 393 | } |
| 394 | } |
| Rafael Espindola | 6a3b5de | 2015-10-01 19:52:48 +0000 | [diff] [blame] | 395 | } |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 396 | |
| Rui Ueyama | 7c71331 | 2016-01-06 01:56:36 +0000 | [diff] [blame] | 397 | // Fully parse the shared object file. This must be called after parseSoName(). |
| 398 | template <class ELFT> void SharedFile<ELFT>::parseRest() { |
| Rafael Espindola | 6a3b5de | 2015-10-01 19:52:48 +0000 | [diff] [blame] | 399 | Elf_Sym_Range Syms = this->getNonLocalSymbols(); |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 400 | uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end()); |
| 401 | SymbolBodies.reserve(NumSymbols); |
| 402 | for (const Elf_Sym &Sym : Syms) { |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 403 | ErrorOr<StringRef> NameOrErr = Sym.getName(this->StringTable); |
| Rui Ueyama | 64cfffd | 2016-01-28 18:40:06 +0000 | [diff] [blame] | 404 | fatal(NameOrErr.getError()); |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 405 | StringRef Name = *NameOrErr; |
| 406 | |
| Rui Ueyama | f8432d9 | 2015-10-13 16:34:14 +0000 | [diff] [blame] | 407 | if (Sym.isUndefined()) |
| 408 | Undefs.push_back(Name); |
| 409 | else |
| 410 | SymbolBodies.emplace_back(this, Name, Sym); |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 411 | } |
| 412 | } |
| Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 413 | |
| Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 414 | BitcodeFile::BitcodeFile(MemoryBufferRef M) : InputFile(BitcodeKind, M) {} |
| 415 | |
| 416 | bool BitcodeFile::classof(const InputFile *F) { |
| 417 | return F->kind() == BitcodeKind; |
| 418 | } |
| 419 | |
| Rafael Espindola | 4de44b7 | 2016-03-02 15:43:50 +0000 | [diff] [blame] | 420 | void BitcodeFile::parse(DenseSet<StringRef> &ComdatGroups) { |
| Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 421 | LLVMContext Context; |
| Rafael Espindola | 1130935c | 2016-03-03 16:21:44 +0000 | [diff] [blame^] | 422 | std::unique_ptr<IRObjectFile> Obj = fatal(IRObjectFile::create(MB, Context)); |
| 423 | const Module &M = Obj->getModule(); |
| Rafael Espindola | 4de44b7 | 2016-03-02 15:43:50 +0000 | [diff] [blame] | 424 | |
| 425 | DenseSet<const Comdat *> KeptComdats; |
| 426 | for (const auto &P : M.getComdatSymbolTable()) { |
| 427 | StringRef N = Saver.save(P.first()); |
| 428 | if (ComdatGroups.insert(N).second) |
| 429 | KeptComdats.insert(&P.second); |
| 430 | } |
| 431 | |
| Rafael Espindola | 1130935c | 2016-03-03 16:21:44 +0000 | [diff] [blame^] | 432 | for (const BasicSymbolRef &Sym : Obj->symbols()) { |
| 433 | if (const GlobalValue *GV = Obj->getSymbolGV(Sym.getRawDataRefImpl())) |
| Rafael Espindola | 4de44b7 | 2016-03-02 15:43:50 +0000 | [diff] [blame] | 434 | if (const Comdat *C = GV->getComdat()) |
| 435 | if (!KeptComdats.count(C)) |
| 436 | continue; |
| 437 | |
| Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 438 | SmallString<64> Name; |
| 439 | raw_svector_ostream OS(Name); |
| 440 | Sym.printName(OS); |
| 441 | StringRef NameRef = Saver.save(StringRef(Name)); |
| Rafael Espindola | 6feecec | 2016-02-19 22:50:16 +0000 | [diff] [blame] | 442 | SymbolBody *Body; |
| Rafael Espindola | 148445e | 2016-02-25 16:25:41 +0000 | [diff] [blame] | 443 | uint32_t Flags = Sym.getFlags(); |
| Rafael Espindola | 1b62553 | 2016-02-29 14:29:48 +0000 | [diff] [blame] | 444 | bool IsWeak = Flags & BasicSymbolRef::SF_Weak; |
| Rafael Espindola | 148445e | 2016-02-25 16:25:41 +0000 | [diff] [blame] | 445 | if (Flags & BasicSymbolRef::SF_Undefined) |
| Rafael Espindola | 1b62553 | 2016-02-29 14:29:48 +0000 | [diff] [blame] | 446 | Body = new (Alloc) Undefined(NameRef, IsWeak, STV_DEFAULT, false); |
| Rafael Espindola | 6feecec | 2016-02-19 22:50:16 +0000 | [diff] [blame] | 447 | else |
| Rafael Espindola | 1b62553 | 2016-02-29 14:29:48 +0000 | [diff] [blame] | 448 | Body = new (Alloc) DefinedBitcode(NameRef, IsWeak); |
| Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 449 | SymbolBodies.push_back(Body); |
| 450 | } |
| 451 | } |
| 452 | |
| Rui Ueyama | c4b6506 | 2015-10-12 15:31:09 +0000 | [diff] [blame] | 453 | template <typename T> |
| 454 | static std::unique_ptr<InputFile> createELFFileAux(MemoryBufferRef MB) { |
| 455 | std::unique_ptr<T> Ret = llvm::make_unique<T>(MB); |
| 456 | |
| 457 | if (!Config->FirstElf) |
| 458 | Config->FirstElf = Ret.get(); |
| 459 | |
| Rui Ueyama | e717a71 | 2015-10-13 16:20:50 +0000 | [diff] [blame] | 460 | if (Config->EKind == ELFNoneKind) { |
| 461 | Config->EKind = Ret->getELFKind(); |
| Rui Ueyama | c4b6506 | 2015-10-12 15:31:09 +0000 | [diff] [blame] | 462 | Config->EMachine = Ret->getEMachine(); |
| 463 | } |
| 464 | |
| 465 | return std::move(Ret); |
| 466 | } |
| 467 | |
| 468 | template <template <class> class T> |
| Rui Ueyama | 533c030 | 2016-01-06 00:09:43 +0000 | [diff] [blame] | 469 | static std::unique_ptr<InputFile> createELFFile(MemoryBufferRef MB) { |
| Rui Ueyama | d94478b | 2015-11-20 02:19:36 +0000 | [diff] [blame] | 470 | std::pair<unsigned char, unsigned char> Type = getElfArchType(MB.getBuffer()); |
| Rui Ueyama | c4b6506 | 2015-10-12 15:31:09 +0000 | [diff] [blame] | 471 | if (Type.second != ELF::ELFDATA2LSB && Type.second != ELF::ELFDATA2MSB) |
| Rui Ueyama | 64cfffd | 2016-01-28 18:40:06 +0000 | [diff] [blame] | 472 | fatal("Invalid data encoding: " + MB.getBufferIdentifier()); |
| Rui Ueyama | c4b6506 | 2015-10-12 15:31:09 +0000 | [diff] [blame] | 473 | |
| 474 | if (Type.first == ELF::ELFCLASS32) { |
| 475 | if (Type.second == ELF::ELFDATA2LSB) |
| Rui Ueyama | d94478b | 2015-11-20 02:19:36 +0000 | [diff] [blame] | 476 | return createELFFileAux<T<ELF32LE>>(MB); |
| 477 | return createELFFileAux<T<ELF32BE>>(MB); |
| Rui Ueyama | c4b6506 | 2015-10-12 15:31:09 +0000 | [diff] [blame] | 478 | } |
| 479 | if (Type.first == ELF::ELFCLASS64) { |
| 480 | if (Type.second == ELF::ELFDATA2LSB) |
| Rui Ueyama | d94478b | 2015-11-20 02:19:36 +0000 | [diff] [blame] | 481 | return createELFFileAux<T<ELF64LE>>(MB); |
| 482 | return createELFFileAux<T<ELF64BE>>(MB); |
| Rui Ueyama | c4b6506 | 2015-10-12 15:31:09 +0000 | [diff] [blame] | 483 | } |
| Rui Ueyama | 64cfffd | 2016-01-28 18:40:06 +0000 | [diff] [blame] | 484 | fatal("Invalid file class: " + MB.getBufferIdentifier()); |
| Rui Ueyama | c4b6506 | 2015-10-12 15:31:09 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 487 | std::unique_ptr<InputFile> elf::createObjectFile(MemoryBufferRef MB, |
| 488 | StringRef ArchiveName) { |
| Rui Ueyama | c89bff2 | 2016-02-23 18:17:11 +0000 | [diff] [blame] | 489 | using namespace sys::fs; |
| 490 | std::unique_ptr<InputFile> F; |
| 491 | if (identify_magic(MB.getBuffer()) == file_magic::bitcode) |
| 492 | F.reset(new BitcodeFile(MB)); |
| 493 | else |
| 494 | F = createELFFile<ObjectFile>(MB); |
| Rui Ueyama | 71c066d | 2016-02-02 08:22:41 +0000 | [diff] [blame] | 495 | F->ArchiveName = ArchiveName; |
| 496 | return F; |
| Rui Ueyama | 533c030 | 2016-01-06 00:09:43 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 499 | std::unique_ptr<InputFile> elf::createSharedFile(MemoryBufferRef MB) { |
| Rui Ueyama | 533c030 | 2016-01-06 00:09:43 +0000 | [diff] [blame] | 500 | return createELFFile<SharedFile>(MB); |
| 501 | } |
| 502 | |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 503 | template class elf::ELFFileBase<ELF32LE>; |
| 504 | template class elf::ELFFileBase<ELF32BE>; |
| 505 | template class elf::ELFFileBase<ELF64LE>; |
| 506 | template class elf::ELFFileBase<ELF64BE>; |
| Davide Italiano | 6d328d3 | 2015-09-16 20:45:57 +0000 | [diff] [blame] | 507 | |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 508 | template class elf::ObjectFile<ELF32LE>; |
| 509 | template class elf::ObjectFile<ELF32BE>; |
| 510 | template class elf::ObjectFile<ELF64LE>; |
| 511 | template class elf::ObjectFile<ELF64BE>; |
| Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 512 | |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 513 | template class elf::SharedFile<ELF32LE>; |
| 514 | template class elf::SharedFile<ELF32BE>; |
| 515 | template class elf::SharedFile<ELF64LE>; |
| 516 | template class elf::SharedFile<ELF64BE>; |