| 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" | 
| George Rimar | 67e3ff8 | 2016-08-12 19:56:57 +0000 | [diff] [blame] | 13 | #include "LinkerScript.h" | 
| Rui Ueyama | 9381eb1 | 2016-12-18 14:06:06 +0000 | [diff] [blame] | 14 | #include "Memory.h" | 
| Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 15 | #include "SymbolTable.h" | 
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 16 | #include "Symbols.h" | 
| Peter Smith | 532bc98 | 2016-12-14 10:36:12 +0000 | [diff] [blame] | 17 | #include "SyntheticSections.h" | 
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/STLExtras.h" | 
| Rafael Espindola | 4d480ed | 2016-04-21 21:44:25 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/Analysis.h" | 
| Eugene Leviant | b380b24 | 2016-10-26 11:07:09 +0000 | [diff] [blame] | 20 | #include "llvm/DebugInfo/DWARF/DWARFContext.h" | 
| Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 21 | #include "llvm/IR/LLVMContext.h" | 
| Rafael Espindola | 4de44b7 | 2016-03-02 15:43:50 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Module.h" | 
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 23 | #include "llvm/LTO/LTO.h" | 
| Michael J. Spencer | a9424f3 | 2016-09-09 22:08:04 +0000 | [diff] [blame] | 24 | #include "llvm/MC/StringTableBuilder.h" | 
| Eugene Leviant | c468120 | 2016-11-01 09:17:50 +0000 | [diff] [blame] | 25 | #include "llvm/Object/ELFObjectFile.h" | 
| Davide Italiano | e02ba98 | 2016-09-08 21:18:38 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Path.h" | 
| Rui Ueyama | ec1c75e | 2017-01-09 01:42:02 +0000 | [diff] [blame] | 27 | #include "llvm/Support/TarWriter.h" | 
| Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" | 
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 29 |  | 
| Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 30 | using namespace llvm; | 
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 31 | using namespace llvm::ELF; | 
| Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 32 | using namespace llvm::object; | 
| Rui Ueyama | f5c4aca | 2015-09-30 17:06:09 +0000 | [diff] [blame] | 33 | using namespace llvm::sys::fs; | 
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 34 |  | 
|  | 35 | using namespace lld; | 
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 36 | using namespace lld::elf; | 
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 37 |  | 
| Rui Ueyama | ec1c75e | 2017-01-09 01:42:02 +0000 | [diff] [blame] | 38 | TarWriter *elf::Tar; | 
|  | 39 |  | 
| Rui Ueyama | 4e4e866 | 2017-04-03 19:11:23 +0000 | [diff] [blame] | 40 | InputFile::InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {} | 
| Rui Ueyama | 37e60a5 | 2017-03-30 21:13:00 +0000 | [diff] [blame] | 41 |  | 
| Eugene Leviant | c468120 | 2016-11-01 09:17:50 +0000 | [diff] [blame] | 42 | namespace { | 
|  | 43 | // In ELF object file all section addresses are zero. If we have multiple | 
|  | 44 | // .text sections (when using -ffunction-section or comdat group) then | 
|  | 45 | // LLVM DWARF parser will not be able to parse .debug_line correctly, unless | 
|  | 46 | // we assign each section some unique address. This callback method assigns | 
|  | 47 | // each section an address equal to its offset in ELF object file. | 
|  | 48 | class ObjectInfo : public LoadedObjectInfo { | 
|  | 49 | public: | 
|  | 50 | uint64_t getSectionLoadAddress(const object::SectionRef &Sec) const override { | 
|  | 51 | return static_cast<const ELFSectionRef &>(Sec).getOffset(); | 
|  | 52 | } | 
|  | 53 | std::unique_ptr<LoadedObjectInfo> clone() const override { | 
|  | 54 | return std::unique_ptr<LoadedObjectInfo>(); | 
|  | 55 | } | 
|  | 56 | }; | 
|  | 57 | } | 
|  | 58 |  | 
| Rui Ueyama | ec1c75e | 2017-01-09 01:42:02 +0000 | [diff] [blame] | 59 | Optional<MemoryBufferRef> elf::readFile(StringRef Path) { | 
| Rui Ueyama | e6e206d | 2017-02-21 23:22:56 +0000 | [diff] [blame] | 60 | log(Path); | 
| Rui Ueyama | ec1c75e | 2017-01-09 01:42:02 +0000 | [diff] [blame] | 61 | auto MBOrErr = MemoryBuffer::getFile(Path); | 
|  | 62 | if (auto EC = MBOrErr.getError()) { | 
| Rui Ueyama | c8d3a83 | 2017-01-12 22:18:04 +0000 | [diff] [blame] | 63 | error("cannot open " + Path + ": " + EC.message()); | 
| Rui Ueyama | ec1c75e | 2017-01-09 01:42:02 +0000 | [diff] [blame] | 64 | return None; | 
|  | 65 | } | 
| Rui Ueyama | e6e206d | 2017-02-21 23:22:56 +0000 | [diff] [blame] | 66 |  | 
| Rui Ueyama | ec1c75e | 2017-01-09 01:42:02 +0000 | [diff] [blame] | 67 | std::unique_ptr<MemoryBuffer> &MB = *MBOrErr; | 
|  | 68 | MemoryBufferRef MBRef = MB->getMemBufferRef(); | 
|  | 69 | make<std::unique_ptr<MemoryBuffer>>(std::move(MB)); // take MB ownership | 
|  | 70 |  | 
|  | 71 | if (Tar) | 
|  | 72 | Tar->append(relativeToRoot(Path), MBRef.getBuffer()); | 
|  | 73 | return MBRef; | 
|  | 74 | } | 
|  | 75 |  | 
| Rui Ueyama | 7463ada | 2016-11-02 19:51:41 +0000 | [diff] [blame] | 76 | template <class ELFT> void elf::ObjectFile<ELFT>::initializeDwarfLine() { | 
| Rui Ueyama | 7556f6b | 2016-11-02 18:42:13 +0000 | [diff] [blame] | 77 | std::unique_ptr<object::ObjectFile> Obj = | 
| Rui Ueyama | 4e4e866 | 2017-04-03 19:11:23 +0000 | [diff] [blame] | 78 | check(object::ObjectFile::createObjectFile(this->MB), toString(this)); | 
| Eugene Leviant | b380b24 | 2016-10-26 11:07:09 +0000 | [diff] [blame] | 79 |  | 
| Eugene Leviant | c468120 | 2016-11-01 09:17:50 +0000 | [diff] [blame] | 80 | ObjectInfo ObjInfo; | 
| Rui Ueyama | 9a97acc | 2016-12-07 23:26:39 +0000 | [diff] [blame] | 81 | DWARFContextInMemory Dwarf(*Obj, &ObjInfo); | 
| Eugene Leviant | b380b24 | 2016-10-26 11:07:09 +0000 | [diff] [blame] | 82 | DwarfLine.reset(new DWARFDebugLine(&Dwarf.getLineSection().Relocs)); | 
| Rui Ueyama | d57e74b7 | 2017-03-17 23:29:01 +0000 | [diff] [blame] | 83 | DataExtractor LineData(Dwarf.getLineSection().Data, Config->IsLE, | 
|  | 84 | Config->Wordsize); | 
| Rui Ueyama | 7556f6b | 2016-11-02 18:42:13 +0000 | [diff] [blame] | 85 |  | 
| Eugene Leviant | b380b24 | 2016-10-26 11:07:09 +0000 | [diff] [blame] | 86 | // The second parameter is offset in .debug_line section | 
|  | 87 | // for compilation unit (CU) of interest. We have only one | 
|  | 88 | // CU (object file), so offset is always 0. | 
|  | 89 | DwarfLine->getOrParseLineTable(LineData, 0); | 
|  | 90 | } | 
|  | 91 |  | 
| Rui Ueyama | 7556f6b | 2016-11-02 18:42:13 +0000 | [diff] [blame] | 92 | // Returns source line information for a given offset | 
|  | 93 | // using DWARF debug info. | 
| Eugene Leviant | c468120 | 2016-11-01 09:17:50 +0000 | [diff] [blame] | 94 | template <class ELFT> | 
| Rui Ueyama | b876020 | 2017-03-30 19:13:47 +0000 | [diff] [blame] | 95 | Optional<DILineInfo> elf::ObjectFile<ELFT>::getDILineInfo(InputSectionBase *S, | 
|  | 96 | uint64_t Offset) { | 
| Eugene Leviant | b380b24 | 2016-10-26 11:07:09 +0000 | [diff] [blame] | 97 | if (!DwarfLine) | 
| Rui Ueyama | 7556f6b | 2016-11-02 18:42:13 +0000 | [diff] [blame] | 98 | initializeDwarfLine(); | 
| Eugene Leviant | b380b24 | 2016-10-26 11:07:09 +0000 | [diff] [blame] | 99 |  | 
| Rui Ueyama | 7556f6b | 2016-11-02 18:42:13 +0000 | [diff] [blame] | 100 | // The offset to CU is 0. | 
|  | 101 | const DWARFDebugLine::LineTable *Tbl = DwarfLine->getLineTable(0); | 
|  | 102 | if (!Tbl) | 
| Rui Ueyama | b876020 | 2017-03-30 19:13:47 +0000 | [diff] [blame] | 103 | return None; | 
| Eugene Leviant | c468120 | 2016-11-01 09:17:50 +0000 | [diff] [blame] | 104 |  | 
|  | 105 | // Use fake address calcuated by adding section file offset and offset in | 
| Rui Ueyama | 7556f6b | 2016-11-02 18:42:13 +0000 | [diff] [blame] | 106 | // section. See comments for ObjectInfo class. | 
|  | 107 | DILineInfo Info; | 
| George Rimar | 92c88a4 | 2016-12-07 19:42:25 +0000 | [diff] [blame] | 108 | Tbl->getFileLineInfoForAddress( | 
| Rafael Espindola | 35ae65e | 2017-03-08 15:57:17 +0000 | [diff] [blame] | 109 | S->getOffsetInFile() + Offset, nullptr, | 
| George Rimar | 92c88a4 | 2016-12-07 19:42:25 +0000 | [diff] [blame] | 110 | DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, Info); | 
| Rui Ueyama | 7556f6b | 2016-11-02 18:42:13 +0000 | [diff] [blame] | 111 | if (Info.Line == 0) | 
| Rui Ueyama | b876020 | 2017-03-30 19:13:47 +0000 | [diff] [blame] | 112 | return None; | 
|  | 113 | return Info; | 
|  | 114 | } | 
|  | 115 |  | 
|  | 116 | // Returns source line information for a given offset | 
|  | 117 | // using DWARF debug info. | 
|  | 118 | template <class ELFT> | 
|  | 119 | std::string elf::ObjectFile<ELFT>::getLineInfo(InputSectionBase *S, | 
|  | 120 | uint64_t Offset) { | 
|  | 121 | if (Optional<DILineInfo> Info = getDILineInfo(S, Offset)) | 
|  | 122 | return Info->FileName + ":" + std::to_string(Info->Line); | 
|  | 123 | return ""; | 
| Eugene Leviant | b380b24 | 2016-10-26 11:07:09 +0000 | [diff] [blame] | 124 | } | 
|  | 125 |  | 
| Rui Ueyama | 8a3ef95 | 2017-04-28 20:00:09 +0000 | [diff] [blame] | 126 | // Returns "<internal>", "foo.a(bar.o)" or "baz.o". | 
| Rui Ueyama | ce03926 | 2017-01-06 10:04:08 +0000 | [diff] [blame] | 127 | std::string lld::toString(const InputFile *F) { | 
| Rui Ueyama | 4e4e866 | 2017-04-03 19:11:23 +0000 | [diff] [blame] | 128 | if (!F) | 
| Rui Ueyama | 8a3ef95 | 2017-04-28 20:00:09 +0000 | [diff] [blame] | 129 | return "<internal>"; | 
| Rui Ueyama | 4e4e866 | 2017-04-03 19:11:23 +0000 | [diff] [blame] | 130 |  | 
|  | 131 | if (F->ToStringCache.empty()) { | 
|  | 132 | if (F->ArchiveName.empty()) | 
|  | 133 | F->ToStringCache = F->getName(); | 
|  | 134 | else | 
|  | 135 | F->ToStringCache = (F->ArchiveName + "(" + F->getName() + ")").str(); | 
|  | 136 | } | 
|  | 137 | return F->ToStringCache; | 
| Rafael Espindola | 78db5a9 | 2016-05-09 21:40:06 +0000 | [diff] [blame] | 138 | } | 
|  | 139 |  | 
| Rui Ueyama | 2022e81 | 2015-11-20 02:10:52 +0000 | [diff] [blame] | 140 | template <class ELFT> | 
| Rafael Espindola | e19abab | 2016-11-03 20:44:50 +0000 | [diff] [blame] | 141 | ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB) : InputFile(K, MB) { | 
| Rui Ueyama | 330e52b | 2017-04-26 22:51:51 +0000 | [diff] [blame] | 142 | if (ELFT::TargetEndianness == support::little) | 
|  | 143 | EKind = ELFT::Is64Bits ? ELF64LEKind : ELF32LEKind; | 
|  | 144 | else | 
|  | 145 | EKind = ELFT::Is64Bits ? ELF64BEKind : ELF32BEKind; | 
|  | 146 |  | 
| Rafael Espindola | e19abab | 2016-11-03 20:44:50 +0000 | [diff] [blame] | 147 | EMachine = getObj().getHeader()->e_machine; | 
|  | 148 | OSABI = getObj().getHeader()->e_ident[llvm::ELF::EI_OSABI]; | 
| Rui Ueyama | 5e64d3f | 2016-06-29 01:30:50 +0000 | [diff] [blame] | 149 | } | 
|  | 150 |  | 
|  | 151 | template <class ELFT> | 
| Rafael Espindola | 8e23257 | 2016-11-03 20:48:57 +0000 | [diff] [blame] | 152 | typename ELFT::SymRange ELFFileBase<ELFT>::getGlobalSymbols() { | 
| George Rimar | 0b2d374 | 2016-11-14 10:05:53 +0000 | [diff] [blame] | 153 | return makeArrayRef(Symbols.begin() + FirstNonLocal, Symbols.end()); | 
| Davide Italiano | 6d328d3 | 2015-09-16 20:45:57 +0000 | [diff] [blame] | 154 | } | 
|  | 155 |  | 
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 156 | template <class ELFT> | 
|  | 157 | uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const { | 
| Rui Ueyama | 37e60a5 | 2017-03-30 21:13:00 +0000 | [diff] [blame] | 158 | return check(getObj().getSectionIndex(&Sym, Symbols, SymtabSHNDX), | 
| Rui Ueyama | 4e4e866 | 2017-04-03 19:11:23 +0000 | [diff] [blame] | 159 | toString(this)); | 
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 160 | } | 
|  | 161 |  | 
| Rafael Espindola | 6d18d38 | 2016-11-03 13:24:29 +0000 | [diff] [blame] | 162 | template <class ELFT> | 
| Rafael Espindola | 21d8be9 | 2016-11-03 15:43:47 +0000 | [diff] [blame] | 163 | void ELFFileBase<ELFT>::initSymtab(ArrayRef<Elf_Shdr> Sections, | 
|  | 164 | const Elf_Shdr *Symtab) { | 
| Rafael Espindola | 6dcf4c6 | 2016-11-03 16:55:44 +0000 | [diff] [blame] | 165 | FirstNonLocal = Symtab->sh_info; | 
| Rui Ueyama | 4e4e866 | 2017-04-03 19:11:23 +0000 | [diff] [blame] | 166 | Symbols = check(getObj().symbols(Symtab), toString(this)); | 
| Rafael Espindola | 6dcf4c6 | 2016-11-03 16:55:44 +0000 | [diff] [blame] | 167 | if (FirstNonLocal == 0 || FirstNonLocal > Symbols.size()) | 
| Rui Ueyama | 3fc0f7e | 2016-11-23 18:07:33 +0000 | [diff] [blame] | 168 | fatal(toString(this) + ": invalid sh_info in symbol table"); | 
| Rafael Espindola | 6dcf4c6 | 2016-11-03 16:55:44 +0000 | [diff] [blame] | 169 |  | 
| Rui Ueyama | 4e4e866 | 2017-04-03 19:11:23 +0000 | [diff] [blame] | 170 | StringTable = check(getObj().getStringTableForSymtab(*Symtab, Sections), | 
|  | 171 | toString(this)); | 
| Rafael Espindola | 6a3b5de | 2015-10-01 19:52:48 +0000 | [diff] [blame] | 172 | } | 
|  | 173 |  | 
|  | 174 | template <class ELFT> | 
| Rui Ueyama | 330e52b | 2017-04-26 22:51:51 +0000 | [diff] [blame] | 175 | elf::ObjectFile<ELFT>::ObjectFile(MemoryBufferRef M, StringRef ArchiveName) | 
|  | 176 | : ELFFileBase<ELFT>(Base::ObjectKind, M) { | 
|  | 177 | this->ArchiveName = ArchiveName; | 
|  | 178 | } | 
| Rafael Espindola | e1901cc | 2015-09-24 15:11:50 +0000 | [diff] [blame] | 179 |  | 
|  | 180 | template <class ELFT> | 
| Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 181 | ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getLocalSymbols() { | 
| Rafael Espindola | 6dcf4c6 | 2016-11-03 16:55:44 +0000 | [diff] [blame] | 182 | if (this->SymbolBodies.empty()) | 
| Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 183 | return this->SymbolBodies; | 
| Rafael Espindola | 6dcf4c6 | 2016-11-03 16:55:44 +0000 | [diff] [blame] | 184 | return makeArrayRef(this->SymbolBodies).slice(1, this->FirstNonLocal - 1); | 
| Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 185 | } | 
|  | 186 |  | 
|  | 187 | template <class ELFT> | 
|  | 188 | ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getSymbols() { | 
| Rafael Espindola | 6dcf4c6 | 2016-11-03 16:55:44 +0000 | [diff] [blame] | 189 | if (this->SymbolBodies.empty()) | 
| Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 190 | return this->SymbolBodies; | 
|  | 191 | return makeArrayRef(this->SymbolBodies).slice(1); | 
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 192 | } | 
|  | 193 |  | 
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 194 | template <class ELFT> | 
| Rafael Espindola | 1c2baad | 2017-05-25 21:53:02 +0000 | [diff] [blame] | 195 | void elf::ObjectFile<ELFT>::parse(DenseSet<CachedHashStringRef> &ComdatGroups) { | 
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 196 | // Read section and symbol tables. | 
| Rafael Espindola | 73c3a36 | 2016-11-08 15:51:00 +0000 | [diff] [blame] | 197 | initializeSections(ComdatGroups); | 
|  | 198 | initializeSymbols(); | 
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 199 | } | 
|  | 200 |  | 
| Rui Ueyama | 3f11c8c | 2015-12-24 08:41:12 +0000 | [diff] [blame] | 201 | // Sections with SHT_GROUP and comdat bits define comdat section groups. | 
|  | 202 | // They are identified and deduplicated by group name. This function | 
|  | 203 | // returns a group name. | 
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 204 | template <class ELFT> | 
| Rafael Espindola | 7c7abaf | 2016-11-03 02:28:13 +0000 | [diff] [blame] | 205 | StringRef | 
|  | 206 | elf::ObjectFile<ELFT>::getShtGroupSignature(ArrayRef<Elf_Shdr> Sections, | 
|  | 207 | const Elf_Shdr &Sec) { | 
| Rafael Espindola | 6dcf4c6 | 2016-11-03 16:55:44 +0000 | [diff] [blame] | 208 | if (this->Symbols.empty()) | 
| Rui Ueyama | 37e60a5 | 2017-03-30 21:13:00 +0000 | [diff] [blame] | 209 | this->initSymtab( | 
|  | 210 | Sections, | 
| Rui Ueyama | 4e4e866 | 2017-04-03 19:11:23 +0000 | [diff] [blame] | 211 | check(object::getSection<ELFT>(Sections, Sec.sh_link), toString(this))); | 
|  | 212 | const Elf_Sym *Sym = check( | 
|  | 213 | object::getSymbol<ELFT>(this->Symbols, Sec.sh_info), toString(this)); | 
|  | 214 | return check(Sym->getName(this->StringTable), toString(this)); | 
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 215 | } | 
|  | 216 |  | 
|  | 217 | template <class ELFT> | 
| Rui Ueyama | 368e1ea | 2016-03-13 22:02:04 +0000 | [diff] [blame] | 218 | ArrayRef<typename elf::ObjectFile<ELFT>::Elf_Word> | 
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 219 | elf::ObjectFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) { | 
| Rafael Espindola | e19abab | 2016-11-03 20:44:50 +0000 | [diff] [blame] | 220 | const ELFFile<ELFT> &Obj = this->getObj(); | 
| Rui Ueyama | 37e60a5 | 2017-03-30 21:13:00 +0000 | [diff] [blame] | 221 | ArrayRef<Elf_Word> Entries = check( | 
| Rui Ueyama | 4e4e866 | 2017-04-03 19:11:23 +0000 | [diff] [blame] | 222 | Obj.template getSectionContentsAsArray<Elf_Word>(&Sec), toString(this)); | 
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 223 | if (Entries.empty() || Entries[0] != GRP_COMDAT) | 
| Rui Ueyama | 3fc0f7e | 2016-11-23 18:07:33 +0000 | [diff] [blame] | 224 | fatal(toString(this) + ": unsupported SHT_GROUP format"); | 
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 225 | return Entries.slice(1); | 
|  | 226 | } | 
|  | 227 |  | 
| Rui Ueyama | 429ef2a | 2016-07-15 20:38:28 +0000 | [diff] [blame] | 228 | template <class ELFT> | 
|  | 229 | bool elf::ObjectFile<ELFT>::shouldMerge(const Elf_Shdr &Sec) { | 
| Rui Ueyama | fb6d499 | 2016-04-29 16:12:29 +0000 | [diff] [blame] | 230 | // We don't merge sections if -O0 (default is -O1). This makes sometimes | 
|  | 231 | // the linker significantly faster, although the output will be bigger. | 
|  | 232 | if (Config->Optimize == 0) | 
|  | 233 | return false; | 
|  | 234 |  | 
| Simon Atanasyan | 02b9c3f | 2016-10-05 07:49:18 +0000 | [diff] [blame] | 235 | // Do not merge sections if generating a relocatable object. It makes | 
|  | 236 | // the code simpler because we do not need to update relocation addends | 
|  | 237 | // to reflect changes introduced by merging. Instead of that we write | 
|  | 238 | // such "merge" sections into separate OutputSections and keep SHF_MERGE | 
|  | 239 | // / SHF_STRINGS flags and sh_entsize value to be able to perform merging | 
|  | 240 | // later during a final linking. | 
|  | 241 | if (Config->Relocatable) | 
|  | 242 | return false; | 
|  | 243 |  | 
| Rui Ueyama | 3ebc71e | 2016-08-03 05:28:02 +0000 | [diff] [blame] | 244 | // A mergeable section with size 0 is useless because they don't have | 
|  | 245 | // any data to merge. A mergeable string section with size 0 can be | 
|  | 246 | // argued as invalid because it doesn't end with a null character. | 
|  | 247 | // We'll avoid a mess by handling them as if they were non-mergeable. | 
|  | 248 | if (Sec.sh_size == 0) | 
|  | 249 | return false; | 
|  | 250 |  | 
| Rui Ueyama | c75ef85 | 2016-09-21 03:22:18 +0000 | [diff] [blame] | 251 | // Check for sh_entsize. The ELF spec is not clear about the zero | 
|  | 252 | // sh_entsize. It says that "the member [sh_entsize] contains 0 if | 
|  | 253 | // the section does not hold a table of fixed-size entries". We know | 
|  | 254 | // that Rust 1.13 produces a string mergeable section with a zero | 
|  | 255 | // sh_entsize. Here we just accept it rather than being picky about it. | 
| Rui Ueyama | 9cc8438 | 2017-02-24 19:52:52 +0000 | [diff] [blame] | 256 | uint64_t EntSize = Sec.sh_entsize; | 
| Rui Ueyama | c75ef85 | 2016-09-21 03:22:18 +0000 | [diff] [blame] | 257 | if (EntSize == 0) | 
|  | 258 | return false; | 
|  | 259 | if (Sec.sh_size % EntSize) | 
| Rui Ueyama | 3fc0f7e | 2016-11-23 18:07:33 +0000 | [diff] [blame] | 260 | fatal(toString(this) + | 
| Rui Ueyama | c75ef85 | 2016-09-21 03:22:18 +0000 | [diff] [blame] | 261 | ": SHF_MERGE section size must be a multiple of sh_entsize"); | 
|  | 262 |  | 
| Rui Ueyama | 9cc8438 | 2017-02-24 19:52:52 +0000 | [diff] [blame] | 263 | uint64_t Flags = Sec.sh_flags; | 
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 264 | if (!(Flags & SHF_MERGE)) | 
|  | 265 | return false; | 
|  | 266 | if (Flags & SHF_WRITE) | 
| Rui Ueyama | 3fc0f7e | 2016-11-23 18:07:33 +0000 | [diff] [blame] | 267 | fatal(toString(this) + ": writable SHF_MERGE section is not supported"); | 
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 268 |  | 
| Peter Smith | 4df2e14 | 2016-05-18 11:40:16 +0000 | [diff] [blame] | 269 | // Don't try to merge if the alignment is larger than the sh_entsize and this | 
| Rafael Espindola | 7efa5be | 2016-02-19 14:17:40 +0000 | [diff] [blame] | 270 | // is not SHF_STRINGS. | 
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 271 | // | 
| Rafael Espindola | 7efa5be | 2016-02-19 14:17:40 +0000 | [diff] [blame] | 272 | // Since this is not a SHF_STRINGS, we would need to pad after every entity. | 
|  | 273 | // 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] | 274 | // sh_entsize. | 
| Rafael Espindola | 7efa5be | 2016-02-19 14:17:40 +0000 | [diff] [blame] | 275 | if (Flags & SHF_STRINGS) | 
|  | 276 | return true; | 
|  | 277 |  | 
| George Rimar | dcddfb6 | 2016-06-08 12:04:59 +0000 | [diff] [blame] | 278 | return Sec.sh_addralign <= EntSize; | 
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 279 | } | 
|  | 280 |  | 
|  | 281 | template <class ELFT> | 
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 282 | void elf::ObjectFile<ELFT>::initializeSections( | 
| Rafael Espindola | 1c2baad | 2017-05-25 21:53:02 +0000 | [diff] [blame] | 283 | DenseSet<CachedHashStringRef> &ComdatGroups) { | 
| Rui Ueyama | 240b951 | 2017-05-26 02:17:30 +0000 | [diff] [blame] | 284 | const ELFFile<ELFT> &Obj = this->getObj(); | 
|  | 285 |  | 
| Rui Ueyama | 37e60a5 | 2017-03-30 21:13:00 +0000 | [diff] [blame] | 286 | ArrayRef<Elf_Shdr> ObjSections = | 
| Rui Ueyama | 4e4e866 | 2017-04-03 19:11:23 +0000 | [diff] [blame] | 287 | check(this->getObj().sections(), toString(this)); | 
| Rafael Espindola | 235d82c | 2016-11-02 14:42:20 +0000 | [diff] [blame] | 288 | uint64_t Size = ObjSections.size(); | 
| George Rimar | 3f7c3df | 2017-03-21 08:44:25 +0000 | [diff] [blame] | 289 | this->Sections.resize(Size); | 
| Rui Ueyama | 240b951 | 2017-05-26 02:17:30 +0000 | [diff] [blame] | 290 |  | 
| Rui Ueyama | 37e60a5 | 2017-03-30 21:13:00 +0000 | [diff] [blame] | 291 | StringRef SectionStringTable = | 
| Rui Ueyama | 4e4e866 | 2017-04-03 19:11:23 +0000 | [diff] [blame] | 292 | check(Obj.getSectionStringTable(ObjSections), toString(this)); | 
| Rui Ueyama | 240b951 | 2017-05-26 02:17:30 +0000 | [diff] [blame] | 293 |  | 
|  | 294 | for (size_t I = 0, E = ObjSections.size(); I < E; I++) { | 
| George Rimar | 3f7c3df | 2017-03-21 08:44:25 +0000 | [diff] [blame] | 295 | if (this->Sections[I] == &InputSection::Discarded) | 
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 296 | continue; | 
| Rui Ueyama | 240b951 | 2017-05-26 02:17:30 +0000 | [diff] [blame] | 297 | const Elf_Shdr &Sec = ObjSections[I]; | 
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 298 |  | 
| Rui Ueyama | af9793d | 2016-10-04 16:47:49 +0000 | [diff] [blame] | 299 | // SHF_EXCLUDE'ed sections are discarded by the linker. However, | 
|  | 300 | // if -r is given, we'll let the final link discard such sections. | 
|  | 301 | // This is compatible with GNU. | 
|  | 302 | if ((Sec.sh_flags & SHF_EXCLUDE) && !Config->Relocatable) { | 
| George Rimar | 3f7c3df | 2017-03-21 08:44:25 +0000 | [diff] [blame] | 303 | this->Sections[I] = &InputSection::Discarded; | 
| Eugene Leviant | 27be542 | 2016-09-28 08:42:02 +0000 | [diff] [blame] | 304 | continue; | 
|  | 305 | } | 
|  | 306 |  | 
| Rafael Espindola | cde2513 | 2015-08-13 14:45:44 +0000 | [diff] [blame] | 307 | switch (Sec.sh_type) { | 
| George Rimar | 3b189d1 | 2017-05-29 08:37:50 +0000 | [diff] [blame^] | 308 | case SHT_GROUP: { | 
|  | 309 | // We discard comdat sections usually. When -r we should not do that. We | 
|  | 310 | // still do deduplication in this case to simplify implementation, because | 
|  | 311 | // otherwise merging group sections together would requre additional | 
|  | 312 | // regeneration of its contents. | 
|  | 313 | bool New = ComdatGroups | 
|  | 314 | .insert(CachedHashStringRef( | 
|  | 315 | getShtGroupSignature(ObjSections, Sec))) | 
|  | 316 | .second; | 
|  | 317 | if (New && Config->Relocatable) | 
|  | 318 | this->Sections[I] = createInputSection(Sec, SectionStringTable); | 
|  | 319 | else | 
|  | 320 | this->Sections[I] = &InputSection::Discarded; | 
|  | 321 | if (New) | 
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 322 | continue; | 
| George Rimar | 3b189d1 | 2017-05-29 08:37:50 +0000 | [diff] [blame^] | 323 |  | 
| Rui Ueyama | 33b3f21 | 2016-01-06 20:30:02 +0000 | [diff] [blame] | 324 | for (uint32_t SecIndex : getShtGroupEntries(Sec)) { | 
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 325 | if (SecIndex >= Size) | 
| George Rimar | 3f7c3df | 2017-03-21 08:44:25 +0000 | [diff] [blame] | 326 | fatal(toString(this) + | 
|  | 327 | ": invalid section index in group: " + Twine(SecIndex)); | 
|  | 328 | this->Sections[SecIndex] = &InputSection::Discarded; | 
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 329 | } | 
|  | 330 | break; | 
| George Rimar | 3b189d1 | 2017-05-29 08:37:50 +0000 | [diff] [blame^] | 331 | } | 
| Rafael Espindola | cde2513 | 2015-08-13 14:45:44 +0000 | [diff] [blame] | 332 | case SHT_SYMTAB: | 
| Rafael Espindola | 21d8be9 | 2016-11-03 15:43:47 +0000 | [diff] [blame] | 333 | this->initSymtab(ObjSections, &Sec); | 
| Rafael Espindola | cde2513 | 2015-08-13 14:45:44 +0000 | [diff] [blame] | 334 | break; | 
| Rafael Espindola | 1130935c | 2016-03-03 16:21:44 +0000 | [diff] [blame] | 335 | case SHT_SYMTAB_SHNDX: | 
| Rui Ueyama | 37e60a5 | 2017-03-30 21:13:00 +0000 | [diff] [blame] | 336 | this->SymtabSHNDX = | 
| Rui Ueyama | 4e4e866 | 2017-04-03 19:11:23 +0000 | [diff] [blame] | 337 | check(Obj.getSHNDXTable(Sec, ObjSections), toString(this)); | 
| Rafael Espindola | 2034822 | 2015-08-24 21:43:25 +0000 | [diff] [blame] | 338 | break; | 
| Rafael Espindola | cde2513 | 2015-08-13 14:45:44 +0000 | [diff] [blame] | 339 | case SHT_STRTAB: | 
|  | 340 | case SHT_NULL: | 
| Rafael Espindola | cde2513 | 2015-08-13 14:45:44 +0000 | [diff] [blame] | 341 | break; | 
| Rui Ueyama | e79b09a | 2015-11-21 22:19:32 +0000 | [diff] [blame] | 342 | default: | 
| George Rimar | 3f7c3df | 2017-03-21 08:44:25 +0000 | [diff] [blame] | 343 | this->Sections[I] = createInputSection(Sec, SectionStringTable); | 
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 344 | } | 
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 345 |  | 
| Rafael Espindola | c17e0b6 | 2016-11-02 13:36:31 +0000 | [diff] [blame] | 346 | // .ARM.exidx sections have a reverse dependency on the InputSection they | 
|  | 347 | // have a SHF_LINK_ORDER dependency, this is identified by the sh_link. | 
|  | 348 | if (Sec.sh_flags & SHF_LINK_ORDER) { | 
| George Rimar | f56cadd | 2017-03-21 08:57:13 +0000 | [diff] [blame] | 349 | if (Sec.sh_link >= this->Sections.size()) | 
| Rui Ueyama | 3fc0f7e | 2016-11-23 18:07:33 +0000 | [diff] [blame] | 350 | fatal(toString(this) + ": invalid sh_link index: " + | 
| Rafael Espindola | c17e0b6 | 2016-11-02 13:36:31 +0000 | [diff] [blame] | 351 | Twine(Sec.sh_link)); | 
| George Rimar | 3f7c3df | 2017-03-21 08:44:25 +0000 | [diff] [blame] | 352 | this->Sections[Sec.sh_link]->DependentSections.push_back( | 
|  | 353 | this->Sections[I]); | 
| Rafael Espindola | c17e0b6 | 2016-11-02 13:36:31 +0000 | [diff] [blame] | 354 | } | 
| Peter Smith | 0760605 | 2016-10-10 10:10:27 +0000 | [diff] [blame] | 355 | } | 
|  | 356 | } | 
|  | 357 |  | 
| Rafael Espindola | f1d598c | 2016-02-12 21:17:10 +0000 | [diff] [blame] | 358 | template <class ELFT> | 
| Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 359 | InputSectionBase *elf::ObjectFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) { | 
| Rui Ueyama | e270c0a | 2016-03-13 21:52:57 +0000 | [diff] [blame] | 360 | uint32_t Idx = Sec.sh_info; | 
| George Rimar | 3f7c3df | 2017-03-21 08:44:25 +0000 | [diff] [blame] | 361 | if (Idx >= this->Sections.size()) | 
| Rui Ueyama | 3fc0f7e | 2016-11-23 18:07:33 +0000 | [diff] [blame] | 362 | fatal(toString(this) + ": invalid relocated section index: " + Twine(Idx)); | 
| George Rimar | 3f7c3df | 2017-03-21 08:44:25 +0000 | [diff] [blame] | 363 | InputSectionBase *Target = this->Sections[Idx]; | 
| Rui Ueyama | e270c0a | 2016-03-13 21:52:57 +0000 | [diff] [blame] | 364 |  | 
|  | 365 | // Strictly speaking, a relocation section must be included in the | 
|  | 366 | // group of the section it relocates. However, LLVM 3.3 and earlier | 
|  | 367 | // would fail to do so, so we gracefully handle that case. | 
| Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 368 | if (Target == &InputSection::Discarded) | 
| Rui Ueyama | e270c0a | 2016-03-13 21:52:57 +0000 | [diff] [blame] | 369 | return nullptr; | 
|  | 370 |  | 
|  | 371 | if (!Target) | 
| Rui Ueyama | 3fc0f7e | 2016-11-23 18:07:33 +0000 | [diff] [blame] | 372 | fatal(toString(this) + ": unsupported relocation reference"); | 
| Rui Ueyama | e270c0a | 2016-03-13 21:52:57 +0000 | [diff] [blame] | 373 | return Target; | 
|  | 374 | } | 
|  | 375 |  | 
| Rui Ueyama | 92a8d79 | 2017-05-01 20:49:09 +0000 | [diff] [blame] | 376 | // Create a regular InputSection class that has the same contents | 
|  | 377 | // as a given section. | 
|  | 378 | InputSectionBase *toRegularSection(MergeInputSection *Sec) { | 
|  | 379 | auto *Ret = make<InputSection>(Sec->Flags, Sec->Type, Sec->Alignment, | 
|  | 380 | Sec->Data, Sec->Name); | 
|  | 381 | Ret->File = Sec->File; | 
|  | 382 | return Ret; | 
|  | 383 | } | 
|  | 384 |  | 
| Rui Ueyama | e270c0a | 2016-03-13 21:52:57 +0000 | [diff] [blame] | 385 | template <class ELFT> | 
| Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 386 | InputSectionBase * | 
| Rafael Espindola | ec05a57 | 2016-11-01 21:48:00 +0000 | [diff] [blame] | 387 | elf::ObjectFile<ELFT>::createInputSection(const Elf_Shdr &Sec, | 
|  | 388 | StringRef SectionStringTable) { | 
| Rui Ueyama | 37e60a5 | 2017-03-30 21:13:00 +0000 | [diff] [blame] | 389 | StringRef Name = check( | 
| Rui Ueyama | 4e4e866 | 2017-04-03 19:11:23 +0000 | [diff] [blame] | 390 | this->getObj().getSectionName(&Sec, SectionStringTable), toString(this)); | 
| Rui Ueyama | 3f11c8c | 2015-12-24 08:41:12 +0000 | [diff] [blame] | 391 |  | 
| Rafael Espindola | 042a3f2 | 2016-09-08 14:06:08 +0000 | [diff] [blame] | 392 | switch (Sec.sh_type) { | 
|  | 393 | case SHT_ARM_ATTRIBUTES: | 
| Peter Smith | 532bc98 | 2016-12-14 10:36:12 +0000 | [diff] [blame] | 394 | // FIXME: ARM meta-data section. Retain the first attribute section | 
|  | 395 | // we see. The eglibc ARM dynamic loaders require the presence of an | 
|  | 396 | // attribute section for dlopen to work. | 
|  | 397 | // In a full implementation we would merge all attribute sections. | 
| Rafael Espindola | 895aea6 | 2017-05-11 22:02:41 +0000 | [diff] [blame] | 398 | if (InX::ARMAttributes == nullptr) { | 
|  | 399 | InX::ARMAttributes = make<InputSection>(this, &Sec, Name); | 
|  | 400 | return InX::ARMAttributes; | 
| Peter Smith | 532bc98 | 2016-12-14 10:36:12 +0000 | [diff] [blame] | 401 | } | 
| Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 402 | return &InputSection::Discarded; | 
| Rafael Espindola | 042a3f2 | 2016-09-08 14:06:08 +0000 | [diff] [blame] | 403 | case SHT_RELA: | 
|  | 404 | case SHT_REL: { | 
| George Rimar | ee6f22c | 2017-02-14 16:42:38 +0000 | [diff] [blame] | 405 | // Find the relocation target section and associate this | 
|  | 406 | // section with it. Target can be discarded, for example | 
|  | 407 | // if it is a duplicated member of SHT_GROUP section, we | 
|  | 408 | // do not create or proccess relocatable sections then. | 
| Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 409 | InputSectionBase *Target = getRelocTarget(Sec); | 
| George Rimar | ee6f22c | 2017-02-14 16:42:38 +0000 | [diff] [blame] | 410 | if (!Target) | 
|  | 411 | return nullptr; | 
|  | 412 |  | 
| Rafael Espindola | 042a3f2 | 2016-09-08 14:06:08 +0000 | [diff] [blame] | 413 | // This section contains relocation information. | 
|  | 414 | // If -r is given, we do not interpret or apply relocation | 
|  | 415 | // but just copy relocation sections to output. | 
|  | 416 | if (Config->Relocatable) | 
| Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 417 | return make<InputSection>(this, &Sec, Name); | 
| Rafael Espindola | 042a3f2 | 2016-09-08 14:06:08 +0000 | [diff] [blame] | 418 |  | 
| Rafael Espindola | 9f0c4bb | 2016-11-10 14:53:24 +0000 | [diff] [blame] | 419 | if (Target->FirstRelocation) | 
| Rui Ueyama | 3fc0f7e | 2016-11-23 18:07:33 +0000 | [diff] [blame] | 420 | fatal(toString(this) + | 
| Rafael Espindola | 9f0c4bb | 2016-11-10 14:53:24 +0000 | [diff] [blame] | 421 | ": multiple relocation sections to one section are not supported"); | 
| Rui Ueyama | 92a8d79 | 2017-05-01 20:49:09 +0000 | [diff] [blame] | 422 |  | 
|  | 423 | // Mergeable sections with relocations are tricky because relocations | 
|  | 424 | // need to be taken into account when comparing section contents for | 
| Rui Ueyama | c344313 | 2017-05-02 21:16:06 +0000 | [diff] [blame] | 425 | // merging. It's not worth supporting such mergeable sections because | 
| Rui Ueyama | dfb1e2a | 2017-05-02 02:58:04 +0000 | [diff] [blame] | 426 | // they are rare and it'd complicates the internal design (we usually | 
|  | 427 | // have to determine if two sections are mergeable early in the link | 
|  | 428 | // process much before applying relocations). We simply handle mergeable | 
|  | 429 | // sections with relocations as non-mergeable. | 
| Rui Ueyama | 92a8d79 | 2017-05-01 20:49:09 +0000 | [diff] [blame] | 430 | if (auto *MS = dyn_cast<MergeInputSection>(Target)) { | 
|  | 431 | Target = toRegularSection(MS); | 
|  | 432 | this->Sections[Sec.sh_info] = Target; | 
| Davide Italiano | d765638 | 2017-04-29 01:24:34 +0000 | [diff] [blame] | 433 | } | 
| Rafael Espindola | 9f0c4bb | 2016-11-10 14:53:24 +0000 | [diff] [blame] | 434 |  | 
|  | 435 | size_t NumRelocations; | 
|  | 436 | if (Sec.sh_type == SHT_RELA) { | 
| Rui Ueyama | 37e60a5 | 2017-03-30 21:13:00 +0000 | [diff] [blame] | 437 | ArrayRef<Elf_Rela> Rels = | 
| Rui Ueyama | 4e4e866 | 2017-04-03 19:11:23 +0000 | [diff] [blame] | 438 | check(this->getObj().relas(&Sec), toString(this)); | 
| Rafael Espindola | 9f0c4bb | 2016-11-10 14:53:24 +0000 | [diff] [blame] | 439 | Target->FirstRelocation = Rels.begin(); | 
|  | 440 | NumRelocations = Rels.size(); | 
|  | 441 | Target->AreRelocsRela = true; | 
|  | 442 | } else { | 
| Rui Ueyama | 4e4e866 | 2017-04-03 19:11:23 +0000 | [diff] [blame] | 443 | ArrayRef<Elf_Rel> Rels = check(this->getObj().rels(&Sec), toString(this)); | 
| Rafael Espindola | 9f0c4bb | 2016-11-10 14:53:24 +0000 | [diff] [blame] | 444 | Target->FirstRelocation = Rels.begin(); | 
|  | 445 | NumRelocations = Rels.size(); | 
|  | 446 | Target->AreRelocsRela = false; | 
| Rafael Espindola | 042a3f2 | 2016-09-08 14:06:08 +0000 | [diff] [blame] | 447 | } | 
| Rafael Espindola | 9f0c4bb | 2016-11-10 14:53:24 +0000 | [diff] [blame] | 448 | assert(isUInt<31>(NumRelocations)); | 
|  | 449 | Target->NumRelocations = NumRelocations; | 
| George Rimar | 82bd8be | 2017-02-08 16:18:10 +0000 | [diff] [blame] | 450 |  | 
|  | 451 | // Relocation sections processed by the linker are usually removed | 
|  | 452 | // from the output, so returning `nullptr` for the normal case. | 
|  | 453 | // However, if -emit-relocs is given, we need to leave them in the output. | 
|  | 454 | // (Some post link analysis tools need this information.) | 
| George Rimar | 858a659 | 2017-02-17 19:46:47 +0000 | [diff] [blame] | 455 | if (Config->EmitRelocs) { | 
| Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 456 | InputSection *RelocSec = make<InputSection>(this, &Sec, Name); | 
| George Rimar | 858a659 | 2017-02-17 19:46:47 +0000 | [diff] [blame] | 457 | // We will not emit relocation section if target was discarded. | 
|  | 458 | Target->DependentSections.push_back(RelocSec); | 
|  | 459 | return RelocSec; | 
|  | 460 | } | 
| Rafael Espindola | 9f0c4bb | 2016-11-10 14:53:24 +0000 | [diff] [blame] | 461 | return nullptr; | 
| Rafael Espindola | 042a3f2 | 2016-09-08 14:06:08 +0000 | [diff] [blame] | 462 | } | 
|  | 463 | } | 
|  | 464 |  | 
| Rui Ueyama | 0633273 | 2017-02-23 07:06:43 +0000 | [diff] [blame] | 465 | // The GNU linker uses .note.GNU-stack section as a marker indicating | 
|  | 466 | // that the code in the object file does not expect that the stack is | 
|  | 467 | // executable (in terms of NX bit). If all input files have the marker, | 
|  | 468 | // the GNU linker adds a PT_GNU_STACK segment to tells the loader to | 
| Rui Ueyama | 65efe35 | 2017-02-23 07:15:46 +0000 | [diff] [blame] | 469 | // make the stack non-executable. Most object files have this section as | 
|  | 470 | // of 2017. | 
| Rui Ueyama | 0633273 | 2017-02-23 07:06:43 +0000 | [diff] [blame] | 471 | // | 
|  | 472 | // But making the stack non-executable is a norm today for security | 
| Rui Ueyama | 65efe35 | 2017-02-23 07:15:46 +0000 | [diff] [blame] | 473 | // reasons. Failure to do so may result in a serious security issue. | 
|  | 474 | // Therefore, we make LLD always add PT_GNU_STACK unless it is | 
| Rui Ueyama | 0633273 | 2017-02-23 07:06:43 +0000 | [diff] [blame] | 475 | // explicitly told to do otherwise (by -z execstack). Because the stack | 
|  | 476 | // executable-ness is controlled solely by command line options, | 
|  | 477 | // .note.GNU-stack sections are simply ignored. | 
| Rui Ueyama | 3f11c8c | 2015-12-24 08:41:12 +0000 | [diff] [blame] | 478 | if (Name == ".note.GNU-stack") | 
| Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 479 | return &InputSection::Discarded; | 
| Rui Ueyama | 3f11c8c | 2015-12-24 08:41:12 +0000 | [diff] [blame] | 480 |  | 
| Rui Ueyama | c1a0ac2 | 2017-02-23 07:35:11 +0000 | [diff] [blame] | 481 | // Split stacks is a feature to support a discontiguous stack. At least | 
|  | 482 | // as of 2017, it seems that the feature is not being used widely. | 
|  | 483 | // Only GNU gold supports that. We don't. For the details about that, | 
|  | 484 | // see https://gcc.gnu.org/wiki/SplitStacks | 
| Rui Ueyama | fc6a4b0 | 2016-04-07 21:04:51 +0000 | [diff] [blame] | 485 | if (Name == ".note.GNU-split-stack") { | 
| Rui Ueyama | b7f39b0 | 2017-02-23 07:35:30 +0000 | [diff] [blame] | 486 | error(toString(this) + | 
|  | 487 | ": object file compiled with -fsplit-stack is not supported"); | 
| Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 488 | return &InputSection::Discarded; | 
| Rui Ueyama | fc6a4b0 | 2016-04-07 21:04:51 +0000 | [diff] [blame] | 489 | } | 
|  | 490 |  | 
| George Rimar | f21aade | 2016-08-31 08:38:11 +0000 | [diff] [blame] | 491 | if (Config->Strip != StripPolicy::None && Name.startswith(".debug")) | 
| Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 492 | return &InputSection::Discarded; | 
| George Rimar | 3c45ed2 | 2016-03-09 18:01:45 +0000 | [diff] [blame] | 493 |  | 
| Rui Ueyama | e2f1169 | 2017-04-28 22:46:55 +0000 | [diff] [blame] | 494 | // If -gdb-index is given, LLD creates .gdb_index section, and that | 
|  | 495 | // section serves the same purpose as .debug_gnu_pub{names,types} sections. | 
|  | 496 | // If that's the case, we want to eliminate .debug_gnu_pub{names,types} | 
|  | 497 | // because they are redundant and can waste large amount of disk space | 
|  | 498 | // (for example, they are about 400 MiB in total for a clang debug build.) | 
|  | 499 | if (Config->GdbIndex && | 
|  | 500 | (Name == ".debug_gnu_pubnames" || Name == ".debug_gnu_pubtypes")) | 
|  | 501 | return &InputSection::Discarded; | 
|  | 502 |  | 
| Peter Collingbourne | c39e5d6 | 2017-01-09 20:26:33 +0000 | [diff] [blame] | 503 | // The linkonce feature is a sort of proto-comdat. Some glibc i386 object | 
|  | 504 | // files contain definitions of symbol "__x86.get_pc_thunk.bx" in linkonce | 
|  | 505 | // sections. Drop those sections to avoid duplicate symbol errors. | 
|  | 506 | // FIXME: This is glibc PR20543, we should remove this hack once that has been | 
|  | 507 | // fixed for a while. | 
|  | 508 | if (Name.startswith(".gnu.linkonce.")) | 
| Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 509 | return &InputSection::Discarded; | 
| Peter Collingbourne | c39e5d6 | 2017-01-09 20:26:33 +0000 | [diff] [blame] | 510 |  | 
| Rui Ueyama | eba9b63 | 2016-07-15 04:57:44 +0000 | [diff] [blame] | 511 | // The linker merges EH (exception handling) frames and creates a | 
|  | 512 | // .eh_frame_hdr section for runtime. So we handle them with a special | 
|  | 513 | // class. For relocatable outputs, they are just passed through. | 
|  | 514 | if (Name == ".eh_frame" && !Config->Relocatable) | 
| Rafael Espindola | 5c02b74 | 2017-03-06 21:17:18 +0000 | [diff] [blame] | 515 | return make<EhInputSection>(this, &Sec, Name); | 
| Rui Ueyama | eba9b63 | 2016-07-15 04:57:44 +0000 | [diff] [blame] | 516 |  | 
| Rui Ueyama | 429ef2a | 2016-07-15 20:38:28 +0000 | [diff] [blame] | 517 | if (shouldMerge(Sec)) | 
| Rafael Espindola | 6119b86 | 2017-03-06 20:23:56 +0000 | [diff] [blame] | 518 | return make<MergeInputSection>(this, &Sec, Name); | 
| Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 519 | return make<InputSection>(this, &Sec, Name); | 
| Rui Ueyama | 3f11c8c | 2015-12-24 08:41:12 +0000 | [diff] [blame] | 520 | } | 
|  | 521 |  | 
| Rafael Espindola | 73c3a36 | 2016-11-08 15:51:00 +0000 | [diff] [blame] | 522 | template <class ELFT> void elf::ObjectFile<ELFT>::initializeSymbols() { | 
| Rui Ueyama | 2711940 | 2016-11-03 18:20:08 +0000 | [diff] [blame] | 523 | SymbolBodies.reserve(this->Symbols.size()); | 
|  | 524 | for (const Elf_Sym &Sym : this->Symbols) | 
| Rui Ueyama | c5e372d | 2016-01-21 02:10:12 +0000 | [diff] [blame] | 525 | SymbolBodies.push_back(createSymbolBody(&Sym)); | 
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 526 | } | 
|  | 527 |  | 
|  | 528 | template <class ELFT> | 
| Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 529 | InputSectionBase *elf::ObjectFile<ELFT>::getSection(const Elf_Sym &Sym) const { | 
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 530 | uint32_t Index = this->getSectionIndex(Sym); | 
| George Rimar | 3f7c3df | 2017-03-21 08:44:25 +0000 | [diff] [blame] | 531 | if (Index >= this->Sections.size()) | 
| Rui Ueyama | 3fc0f7e | 2016-11-23 18:07:33 +0000 | [diff] [blame] | 532 | fatal(toString(this) + ": invalid section index: " + Twine(Index)); | 
| George Rimar | 3f7c3df | 2017-03-21 08:44:25 +0000 | [diff] [blame] | 533 | InputSectionBase *S = this->Sections[Index]; | 
| George Rimar | 24adce9 | 2016-10-06 09:17:55 +0000 | [diff] [blame] | 534 |  | 
| George Rimar | 308752e | 2016-11-15 07:56:28 +0000 | [diff] [blame] | 535 | // We found that GNU assembler 2.17.50 [FreeBSD] 2007-07-03 could | 
|  | 536 | // generate broken objects. STT_SECTION/STT_NOTYPE symbols can be | 
| George Rimar | 683a35d | 2016-08-12 19:25:54 +0000 | [diff] [blame] | 537 | // associated with SHT_REL[A]/SHT_SYMTAB/SHT_STRTAB sections. | 
| George Rimar | 308752e | 2016-11-15 07:56:28 +0000 | [diff] [blame] | 538 | // In this case it is fine for section to be null here as we do not | 
|  | 539 | // allocate sections of these types. | 
| George Rimar | 24adce9 | 2016-10-06 09:17:55 +0000 | [diff] [blame] | 540 | if (!S) { | 
| George Rimar | 308752e | 2016-11-15 07:56:28 +0000 | [diff] [blame] | 541 | if (Index == 0 || Sym.getType() == STT_SECTION || | 
|  | 542 | Sym.getType() == STT_NOTYPE) | 
| George Rimar | 24adce9 | 2016-10-06 09:17:55 +0000 | [diff] [blame] | 543 | return nullptr; | 
| Rui Ueyama | 3fc0f7e | 2016-11-23 18:07:33 +0000 | [diff] [blame] | 544 | fatal(toString(this) + ": invalid section index: " + Twine(Index)); | 
| George Rimar | 24adce9 | 2016-10-06 09:17:55 +0000 | [diff] [blame] | 545 | } | 
|  | 546 |  | 
| Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 547 | if (S == &InputSection::Discarded) | 
| Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 548 | return S; | 
|  | 549 | return S->Repl; | 
| Rafael Espindola | 4cda581 | 2015-10-16 15:29:48 +0000 | [diff] [blame] | 550 | } | 
|  | 551 |  | 
|  | 552 | template <class ELFT> | 
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 553 | SymbolBody *elf::ObjectFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) { | 
| Rui Ueyama | 429ef2a | 2016-07-15 20:38:28 +0000 | [diff] [blame] | 554 | int Binding = Sym->getBinding(); | 
| Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 555 | InputSectionBase *Sec = getSection(*Sym); | 
| Rui Ueyama | 0cbf749 | 2016-11-23 06:59:47 +0000 | [diff] [blame] | 556 |  | 
| Rui Ueyama | a13efc2 | 2016-11-29 18:05:04 +0000 | [diff] [blame] | 557 | uint8_t StOther = Sym->st_other; | 
|  | 558 | uint8_t Type = Sym->getType(); | 
| Rui Ueyama | 9cc8438 | 2017-02-24 19:52:52 +0000 | [diff] [blame] | 559 | uint64_t Value = Sym->st_value; | 
|  | 560 | uint64_t Size = Sym->st_size; | 
| Rui Ueyama | a13efc2 | 2016-11-29 18:05:04 +0000 | [diff] [blame] | 561 |  | 
| Rafael Espindola | 1f5b70f | 2016-03-11 14:21:37 +0000 | [diff] [blame] | 562 | if (Binding == STB_LOCAL) { | 
| Eugene Leviant | b380b24 | 2016-10-26 11:07:09 +0000 | [diff] [blame] | 563 | if (Sym->getType() == STT_FILE) | 
| Rui Ueyama | 4e4e866 | 2017-04-03 19:11:23 +0000 | [diff] [blame] | 564 | SourceFile = check(Sym->getName(this->StringTable), toString(this)); | 
| Rui Ueyama | c72ba3a | 2016-11-23 04:57:25 +0000 | [diff] [blame] | 565 |  | 
|  | 566 | if (this->StringTable.size() <= Sym->st_name) | 
| Rui Ueyama | 3fc0f7e | 2016-11-23 18:07:33 +0000 | [diff] [blame] | 567 | fatal(toString(this) + ": invalid symbol name offset"); | 
| Rui Ueyama | c72ba3a | 2016-11-23 04:57:25 +0000 | [diff] [blame] | 568 |  | 
| Rui Ueyama | 84e65a7 | 2016-11-29 19:11:39 +0000 | [diff] [blame] | 569 | StringRefZ Name = this->StringTable.data() + Sym->st_name; | 
| Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 570 | if (Sym->st_shndx == SHN_UNDEF) | 
| Rui Ueyama | 175e81c | 2017-02-28 19:36:30 +0000 | [diff] [blame] | 571 | return make<Undefined>(Name, /*IsLocal=*/true, StOther, Type, this); | 
| Rui Ueyama | a13efc2 | 2016-11-29 18:05:04 +0000 | [diff] [blame] | 572 |  | 
| Rui Ueyama | 175e81c | 2017-02-28 19:36:30 +0000 | [diff] [blame] | 573 | return make<DefinedRegular>(Name, /*IsLocal=*/true, StOther, Type, Value, | 
|  | 574 | Size, Sec, this); | 
| Rafael Espindola | 1f5b70f | 2016-03-11 14:21:37 +0000 | [diff] [blame] | 575 | } | 
| Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 576 |  | 
| Rui Ueyama | 4e4e866 | 2017-04-03 19:11:23 +0000 | [diff] [blame] | 577 | StringRef Name = check(Sym->getName(this->StringTable), toString(this)); | 
| Rafael Espindola | 2034822 | 2015-08-24 21:43:25 +0000 | [diff] [blame] | 578 |  | 
| Rafael Espindola | 4cda581 | 2015-10-16 15:29:48 +0000 | [diff] [blame] | 579 | switch (Sym->st_shndx) { | 
| Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 580 | case SHN_UNDEF: | 
| Rui Ueyama | 0cbf749 | 2016-11-23 06:59:47 +0000 | [diff] [blame] | 581 | return elf::Symtab<ELFT>::X | 
| Rui Ueyama | a13efc2 | 2016-11-29 18:05:04 +0000 | [diff] [blame] | 582 | ->addUndefined(Name, /*IsLocal=*/false, Binding, StOther, Type, | 
| Rui Ueyama | 0cbf749 | 2016-11-23 06:59:47 +0000 | [diff] [blame] | 583 | /*CanOmitFromDynSym=*/false, this) | 
| Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 584 | ->body(); | 
| Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 585 | case SHN_COMMON: | 
| Rui Ueyama | 0cbf749 | 2016-11-23 06:59:47 +0000 | [diff] [blame] | 586 | if (Value == 0 || Value >= UINT32_MAX) | 
| Rui Ueyama | 3fc0f7e | 2016-11-23 18:07:33 +0000 | [diff] [blame] | 587 | fatal(toString(this) + ": common symbol '" + Name + | 
| Rui Ueyama | 0cbf749 | 2016-11-23 06:59:47 +0000 | [diff] [blame] | 588 | "' has invalid alignment: " + Twine(Value)); | 
|  | 589 | return elf::Symtab<ELFT>::X | 
|  | 590 | ->addCommon(Name, Size, Value, Binding, StOther, Type, this) | 
| Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 591 | ->body(); | 
| Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 592 | } | 
| Rafael Espindola | 2034822 | 2015-08-24 21:43:25 +0000 | [diff] [blame] | 593 |  | 
| Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 594 | switch (Binding) { | 
| Rafael Espindola | b13df65 | 2015-08-11 17:33:02 +0000 | [diff] [blame] | 595 | default: | 
| Rui Ueyama | 3fc0f7e | 2016-11-23 18:07:33 +0000 | [diff] [blame] | 596 | fatal(toString(this) + ": unexpected binding: " + Twine(Binding)); | 
| Rafael Espindola | b13df65 | 2015-08-11 17:33:02 +0000 | [diff] [blame] | 597 | case STB_GLOBAL: | 
| Rafael Espindola | 3a63f3f | 2015-08-28 20:19:34 +0000 | [diff] [blame] | 598 | case STB_WEAK: | 
| Rafael Espindola | 1f5b70f | 2016-03-11 14:21:37 +0000 | [diff] [blame] | 599 | case STB_GNU_UNIQUE: | 
| Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 600 | if (Sec == &InputSection::Discarded) | 
| Rui Ueyama | 0cbf749 | 2016-11-23 06:59:47 +0000 | [diff] [blame] | 601 | return elf::Symtab<ELFT>::X | 
| Rui Ueyama | a13efc2 | 2016-11-29 18:05:04 +0000 | [diff] [blame] | 602 | ->addUndefined(Name, /*IsLocal=*/false, Binding, StOther, Type, | 
| Rui Ueyama | 0cbf749 | 2016-11-23 06:59:47 +0000 | [diff] [blame] | 603 | /*CanOmitFromDynSym=*/false, this) | 
| Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 604 | ->body(); | 
| Rui Ueyama | 0cbf749 | 2016-11-23 06:59:47 +0000 | [diff] [blame] | 605 | return elf::Symtab<ELFT>::X | 
|  | 606 | ->addRegular(Name, StOther, Type, Value, Size, Binding, Sec, this) | 
|  | 607 | ->body(); | 
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 608 | } | 
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 609 | } | 
|  | 610 |  | 
| Rui Ueyama | fd7deda | 2017-05-03 21:03:08 +0000 | [diff] [blame] | 611 | ArchiveFile::ArchiveFile(std::unique_ptr<Archive> &&File) | 
|  | 612 | : InputFile(ArchiveKind, File->getMemoryBufferRef()), | 
|  | 613 | File(std::move(File)) {} | 
|  | 614 |  | 
| Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 615 | template <class ELFT> void ArchiveFile::parse() { | 
| Rui Ueyama | fd7deda | 2017-05-03 21:03:08 +0000 | [diff] [blame] | 616 | for (const Archive::Symbol &Sym : File->symbols()) | 
| Rui Ueyama | 3d0f77b | 2016-09-30 17:56:20 +0000 | [diff] [blame] | 617 | Symtab<ELFT>::X->addLazyArchive(this, Sym); | 
| Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 618 | } | 
|  | 619 |  | 
|  | 620 | // Returns a buffer pointing to a member file containing a given symbol. | 
| Davide Italiano | bcdd6c6 | 2016-10-12 19:35:54 +0000 | [diff] [blame] | 621 | std::pair<MemoryBufferRef, uint64_t> | 
|  | 622 | ArchiveFile::getMember(const Archive::Symbol *Sym) { | 
| Rafael Espindola | 1130935c | 2016-03-03 16:21:44 +0000 | [diff] [blame] | 623 | Archive::Child C = | 
| Rui Ueyama | 4e4e866 | 2017-04-03 19:11:23 +0000 | [diff] [blame] | 624 | check(Sym->getMember(), toString(this) + | 
| Rui Ueyama | 37e60a5 | 2017-03-30 21:13:00 +0000 | [diff] [blame] | 625 | ": could not get the member for symbol " + | 
|  | 626 | Sym->getName()); | 
| Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 627 |  | 
| Rafael Espindola | 8f3a6ae | 2015-11-05 14:40:28 +0000 | [diff] [blame] | 628 | if (!Seen.insert(C.getChildOffset()).second) | 
| Davide Italiano | bcdd6c6 | 2016-10-12 19:35:54 +0000 | [diff] [blame] | 629 | return {MemoryBufferRef(), 0}; | 
| Michael J. Spencer | 88f0d63 | 2015-09-08 20:36:20 +0000 | [diff] [blame] | 630 |  | 
| Rafael Espindola | 1dd2b3d | 2016-05-03 17:30:44 +0000 | [diff] [blame] | 631 | MemoryBufferRef Ret = | 
|  | 632 | check(C.getMemoryBufferRef(), | 
| Rui Ueyama | 4e4e866 | 2017-04-03 19:11:23 +0000 | [diff] [blame] | 633 | toString(this) + | 
| Rui Ueyama | 37e60a5 | 2017-03-30 21:13:00 +0000 | [diff] [blame] | 634 | ": could not get the buffer for the member defining symbol " + | 
| Rafael Espindola | 1dd2b3d | 2016-05-03 17:30:44 +0000 | [diff] [blame] | 635 | Sym->getName()); | 
| Rafael Espindola | d1cbe4d | 2016-05-02 13:54:10 +0000 | [diff] [blame] | 636 |  | 
| Rui Ueyama | ec1c75e | 2017-01-09 01:42:02 +0000 | [diff] [blame] | 637 | if (C.getParent()->isThin() && Tar) | 
| Rui Ueyama | 4e4e866 | 2017-04-03 19:11:23 +0000 | [diff] [blame] | 638 | Tar->append(relativeToRoot(check(C.getFullName(), toString(this))), | 
| Rui Ueyama | 37e60a5 | 2017-03-30 21:13:00 +0000 | [diff] [blame] | 639 | Ret.getBuffer()); | 
| Davide Italiano | bcdd6c6 | 2016-10-12 19:35:54 +0000 | [diff] [blame] | 640 | if (C.getParent()->isThin()) | 
|  | 641 | return {Ret, 0}; | 
|  | 642 | return {Ret, C.getChildOffset()}; | 
| Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 643 | } | 
|  | 644 |  | 
| Rafael Espindola | e1901cc | 2015-09-24 15:11:50 +0000 | [diff] [blame] | 645 | template <class ELFT> | 
| Rafael Espindola | 3460cdd | 2017-04-24 21:44:20 +0000 | [diff] [blame] | 646 | SharedFile<ELFT>::SharedFile(MemoryBufferRef M, StringRef DefaultSoName) | 
|  | 647 | : ELFFileBase<ELFT>(Base::SharedKind, M), SoName(DefaultSoName), | 
|  | 648 | AsNeeded(Config->AsNeeded) {} | 
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 649 |  | 
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 650 | template <class ELFT> | 
| Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 651 | const typename ELFT::Shdr * | 
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 652 | SharedFile<ELFT>::getSection(const Elf_Sym &Sym) const { | 
| Rafael Espindola | e19abab | 2016-11-03 20:44:50 +0000 | [diff] [blame] | 653 | return check( | 
| Rui Ueyama | 37e60a5 | 2017-03-30 21:13:00 +0000 | [diff] [blame] | 654 | this->getObj().getSection(&Sym, this->Symbols, this->SymtabSHNDX), | 
| Rui Ueyama | 4e4e866 | 2017-04-03 19:11:23 +0000 | [diff] [blame] | 655 | toString(this)); | 
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 656 | } | 
|  | 657 |  | 
| Rui Ueyama | 7c71331 | 2016-01-06 01:56:36 +0000 | [diff] [blame] | 658 | // Partially parse the shared object file so that we can call | 
|  | 659 | // getSoName on this object. | 
| Rafael Espindola | 6a3b5de | 2015-10-01 19:52:48 +0000 | [diff] [blame] | 660 | template <class ELFT> void SharedFile<ELFT>::parseSoName() { | 
| Rafael Espindola | c8b1581 | 2015-10-01 15:47:50 +0000 | [diff] [blame] | 661 | const Elf_Shdr *DynamicSec = nullptr; | 
| Rafael Espindola | e19abab | 2016-11-03 20:44:50 +0000 | [diff] [blame] | 662 | const ELFFile<ELFT> Obj = this->getObj(); | 
| Rui Ueyama | 4e4e866 | 2017-04-03 19:11:23 +0000 | [diff] [blame] | 663 | ArrayRef<Elf_Shdr> Sections = check(Obj.sections(), toString(this)); | 
| Rui Ueyama | 3233d3e | 2017-04-13 00:23:32 +0000 | [diff] [blame] | 664 |  | 
|  | 665 | // Search for .dynsym, .dynamic, .symtab, .gnu.version and .gnu.version_d. | 
| Rafael Espindola | 84d6a17 | 2016-11-03 12:21:00 +0000 | [diff] [blame] | 666 | for (const Elf_Shdr &Sec : Sections) { | 
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 667 | switch (Sec.sh_type) { | 
|  | 668 | default: | 
|  | 669 | continue; | 
|  | 670 | case SHT_DYNSYM: | 
| Rafael Espindola | 21d8be9 | 2016-11-03 15:43:47 +0000 | [diff] [blame] | 671 | this->initSymtab(Sections, &Sec); | 
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 672 | break; | 
|  | 673 | case SHT_DYNAMIC: | 
| Rafael Espindola | c8b1581 | 2015-10-01 15:47:50 +0000 | [diff] [blame] | 674 | DynamicSec = &Sec; | 
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 675 | break; | 
| Rafael Espindola | 1130935c | 2016-03-03 16:21:44 +0000 | [diff] [blame] | 676 | case SHT_SYMTAB_SHNDX: | 
| Rui Ueyama | 37e60a5 | 2017-03-30 21:13:00 +0000 | [diff] [blame] | 677 | this->SymtabSHNDX = | 
| Rui Ueyama | 4e4e866 | 2017-04-03 19:11:23 +0000 | [diff] [blame] | 678 | check(Obj.getSHNDXTable(Sec, Sections), toString(this)); | 
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 679 | break; | 
| Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 680 | case SHT_GNU_versym: | 
|  | 681 | this->VersymSec = &Sec; | 
|  | 682 | break; | 
|  | 683 | case SHT_GNU_verdef: | 
|  | 684 | this->VerdefSec = &Sec; | 
|  | 685 | break; | 
| Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 686 | } | 
| Rafael Espindola | c8b1581 | 2015-10-01 15:47:50 +0000 | [diff] [blame] | 687 | } | 
|  | 688 |  | 
| Rafael Espindola | 6dcf4c6 | 2016-11-03 16:55:44 +0000 | [diff] [blame] | 689 | if (this->VersymSec && this->Symbols.empty()) | 
| George Rimar | bcba39a | 2016-11-02 10:16:25 +0000 | [diff] [blame] | 690 | error("SHT_GNU_versym should be associated with symbol table"); | 
|  | 691 |  | 
| Rui Ueyama | 3233d3e | 2017-04-13 00:23:32 +0000 | [diff] [blame] | 692 | // Search for a DT_SONAME tag to initialize this->SoName. | 
| Rui Ueyama | 361d8b9 | 2015-10-12 15:49:02 +0000 | [diff] [blame] | 693 | if (!DynamicSec) | 
|  | 694 | return; | 
| George Rimar | 53cf2a8 | 2016-10-07 09:01:04 +0000 | [diff] [blame] | 695 | ArrayRef<Elf_Dyn> Arr = | 
|  | 696 | check(Obj.template getSectionContentsAsArray<Elf_Dyn>(DynamicSec), | 
| Rui Ueyama | 4e4e866 | 2017-04-03 19:11:23 +0000 | [diff] [blame] | 697 | toString(this)); | 
| George Rimar | 53cf2a8 | 2016-10-07 09:01:04 +0000 | [diff] [blame] | 698 | for (const Elf_Dyn &Dyn : Arr) { | 
| Rui Ueyama | 361d8b9 | 2015-10-12 15:49:02 +0000 | [diff] [blame] | 699 | if (Dyn.d_tag == DT_SONAME) { | 
| Rui Ueyama | 9cc8438 | 2017-02-24 19:52:52 +0000 | [diff] [blame] | 700 | uint64_t Val = Dyn.getVal(); | 
| Rui Ueyama | 361d8b9 | 2015-10-12 15:49:02 +0000 | [diff] [blame] | 701 | if (Val >= this->StringTable.size()) | 
| Rui Ueyama | 3fc0f7e | 2016-11-23 18:07:33 +0000 | [diff] [blame] | 702 | fatal(toString(this) + ": invalid DT_SONAME entry"); | 
| Rui Ueyama | e5ad298 | 2017-04-26 23:00:32 +0000 | [diff] [blame] | 703 | SoName = this->StringTable.data() + Val; | 
| Rui Ueyama | 361d8b9 | 2015-10-12 15:49:02 +0000 | [diff] [blame] | 704 | return; | 
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 705 | } | 
|  | 706 | } | 
| Rafael Espindola | 6a3b5de | 2015-10-01 19:52:48 +0000 | [diff] [blame] | 707 | } | 
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 708 |  | 
| Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 709 | // Parse the version definitions in the object file if present. Returns a vector | 
|  | 710 | // whose nth element contains a pointer to the Elf_Verdef for version identifier | 
|  | 711 | // n. Version identifiers that are not definitions map to nullptr. The array | 
|  | 712 | // always has at least length 1. | 
|  | 713 | template <class ELFT> | 
|  | 714 | std::vector<const typename ELFT::Verdef *> | 
|  | 715 | SharedFile<ELFT>::parseVerdefs(const Elf_Versym *&Versym) { | 
|  | 716 | std::vector<const Elf_Verdef *> Verdefs(1); | 
|  | 717 | // We only need to process symbol versions for this DSO if it has both a | 
|  | 718 | // versym and a verdef section, which indicates that the DSO contains symbol | 
|  | 719 | // version definitions. | 
|  | 720 | if (!VersymSec || !VerdefSec) | 
|  | 721 | return Verdefs; | 
|  | 722 |  | 
|  | 723 | // The location of the first global versym entry. | 
| Rafael Espindola | e19abab | 2016-11-03 20:44:50 +0000 | [diff] [blame] | 724 | const char *Base = this->MB.getBuffer().data(); | 
|  | 725 | Versym = reinterpret_cast<const Elf_Versym *>(Base + VersymSec->sh_offset) + | 
| Rafael Espindola | 6dcf4c6 | 2016-11-03 16:55:44 +0000 | [diff] [blame] | 726 | this->FirstNonLocal; | 
| Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 727 |  | 
|  | 728 | // We cannot determine the largest verdef identifier without inspecting | 
|  | 729 | // every Elf_Verdef, but both bfd and gold assign verdef identifiers | 
|  | 730 | // sequentially starting from 1, so we predict that the largest identifier | 
|  | 731 | // will be VerdefCount. | 
|  | 732 | unsigned VerdefCount = VerdefSec->sh_info; | 
|  | 733 | Verdefs.resize(VerdefCount + 1); | 
|  | 734 |  | 
|  | 735 | // Build the Verdefs array by following the chain of Elf_Verdef objects | 
|  | 736 | // from the start of the .gnu.version_d section. | 
| Rafael Espindola | e19abab | 2016-11-03 20:44:50 +0000 | [diff] [blame] | 737 | const char *Verdef = Base + VerdefSec->sh_offset; | 
| Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 738 | for (unsigned I = 0; I != VerdefCount; ++I) { | 
|  | 739 | auto *CurVerdef = reinterpret_cast<const Elf_Verdef *>(Verdef); | 
|  | 740 | Verdef += CurVerdef->vd_next; | 
|  | 741 | unsigned VerdefIndex = CurVerdef->vd_ndx; | 
|  | 742 | if (Verdefs.size() <= VerdefIndex) | 
|  | 743 | Verdefs.resize(VerdefIndex + 1); | 
|  | 744 | Verdefs[VerdefIndex] = CurVerdef; | 
|  | 745 | } | 
|  | 746 |  | 
|  | 747 | return Verdefs; | 
|  | 748 | } | 
|  | 749 |  | 
| Rui Ueyama | 7c71331 | 2016-01-06 01:56:36 +0000 | [diff] [blame] | 750 | // Fully parse the shared object file. This must be called after parseSoName(). | 
|  | 751 | template <class ELFT> void SharedFile<ELFT>::parseRest() { | 
| Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 752 | // Create mapping from version identifiers to Elf_Verdef entries. | 
|  | 753 | const Elf_Versym *Versym = nullptr; | 
|  | 754 | std::vector<const Elf_Verdef *> Verdefs = parseVerdefs(Versym); | 
|  | 755 |  | 
| Rafael Espindola | 8e23257 | 2016-11-03 20:48:57 +0000 | [diff] [blame] | 756 | Elf_Sym_Range Syms = this->getGlobalSymbols(); | 
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 757 | for (const Elf_Sym &Sym : Syms) { | 
| Rafael Espindola | fb4f2fe | 2016-04-29 17:46:07 +0000 | [diff] [blame] | 758 | unsigned VersymIndex = 0; | 
|  | 759 | if (Versym) { | 
|  | 760 | VersymIndex = Versym->vs_index; | 
|  | 761 | ++Versym; | 
|  | 762 | } | 
| Rafael Espindola | 2756e04 | 2017-01-06 22:30:35 +0000 | [diff] [blame] | 763 | bool Hidden = VersymIndex & VERSYM_HIDDEN; | 
|  | 764 | VersymIndex = VersymIndex & ~VERSYM_HIDDEN; | 
| Rafael Espindola | fb4f2fe | 2016-04-29 17:46:07 +0000 | [diff] [blame] | 765 |  | 
| Rui Ueyama | 4e4e866 | 2017-04-03 19:11:23 +0000 | [diff] [blame] | 766 | StringRef Name = check(Sym.getName(this->StringTable), toString(this)); | 
| Rafael Espindola | 18da0e5 | 2016-04-29 16:23:31 +0000 | [diff] [blame] | 767 | if (Sym.isUndefined()) { | 
|  | 768 | Undefs.push_back(Name); | 
|  | 769 | continue; | 
|  | 770 | } | 
|  | 771 |  | 
| Rafael Espindola | 2756e04 | 2017-01-06 22:30:35 +0000 | [diff] [blame] | 772 | // Ignore local symbols. | 
|  | 773 | if (Versym && VersymIndex == VER_NDX_LOCAL) | 
|  | 774 | continue; | 
| Rafael Espindola | d2454d6 | 2016-06-09 15:45:49 +0000 | [diff] [blame] | 775 |  | 
|  | 776 | const Elf_Verdef *V = | 
|  | 777 | VersymIndex == VER_NDX_GLOBAL ? nullptr : Verdefs[VersymIndex]; | 
| Rafael Espindola | 2756e04 | 2017-01-06 22:30:35 +0000 | [diff] [blame] | 778 |  | 
|  | 779 | if (!Hidden) | 
|  | 780 | elf::Symtab<ELFT>::X->addShared(this, Name, Sym, V); | 
|  | 781 |  | 
|  | 782 | // Also add the symbol with the versioned name to handle undefined symbols | 
|  | 783 | // with explicit versions. | 
|  | 784 | if (V) { | 
|  | 785 | StringRef VerName = this->StringTable.data() + V->getAux()->vda_name; | 
| Rui Ueyama | 63d48e5 | 2017-04-27 04:01:14 +0000 | [diff] [blame] | 786 | Name = Saver.save(Name + "@" + VerName); | 
| Rafael Espindola | 2756e04 | 2017-01-06 22:30:35 +0000 | [diff] [blame] | 787 | elf::Symtab<ELFT>::X->addShared(this, Name, Sym, V); | 
|  | 788 | } | 
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 789 | } | 
|  | 790 | } | 
| Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 791 |  | 
| Peter Collingbourne | 8446f1f | 2017-04-14 02:55:06 +0000 | [diff] [blame] | 792 | static ELFKind getBitcodeELFKind(const Triple &T) { | 
| Rui Ueyama | 7fdb438 | 2016-08-03 20:25:29 +0000 | [diff] [blame] | 793 | if (T.isLittleEndian()) | 
|  | 794 | return T.isArch64Bit() ? ELF64LEKind : ELF32LEKind; | 
|  | 795 | return T.isArch64Bit() ? ELF64BEKind : ELF32BEKind; | 
| Davide Italiano | 60976ba | 2016-06-29 06:12:39 +0000 | [diff] [blame] | 796 | } | 
|  | 797 |  | 
| Peter Collingbourne | 8446f1f | 2017-04-14 02:55:06 +0000 | [diff] [blame] | 798 | static uint8_t getBitcodeMachineKind(StringRef Path, const Triple &T) { | 
| Rui Ueyama | 7fdb438 | 2016-08-03 20:25:29 +0000 | [diff] [blame] | 799 | switch (T.getArch()) { | 
| Rui Ueyama | 523744d | 2016-07-07 02:46:30 +0000 | [diff] [blame] | 800 | case Triple::aarch64: | 
|  | 801 | return EM_AARCH64; | 
|  | 802 | case Triple::arm: | 
| Sean Silva | 1d96185 | 2017-02-28 03:00:48 +0000 | [diff] [blame] | 803 | case Triple::thumb: | 
| Rui Ueyama | 523744d | 2016-07-07 02:46:30 +0000 | [diff] [blame] | 804 | return EM_ARM; | 
|  | 805 | case Triple::mips: | 
|  | 806 | case Triple::mipsel: | 
|  | 807 | case Triple::mips64: | 
|  | 808 | case Triple::mips64el: | 
|  | 809 | return EM_MIPS; | 
|  | 810 | case Triple::ppc: | 
|  | 811 | return EM_PPC; | 
|  | 812 | case Triple::ppc64: | 
|  | 813 | return EM_PPC64; | 
|  | 814 | case Triple::x86: | 
| Rui Ueyama | 7fdb438 | 2016-08-03 20:25:29 +0000 | [diff] [blame] | 815 | return T.isOSIAMCU() ? EM_IAMCU : EM_386; | 
| Rui Ueyama | 523744d | 2016-07-07 02:46:30 +0000 | [diff] [blame] | 816 | case Triple::x86_64: | 
|  | 817 | return EM_X86_64; | 
|  | 818 | default: | 
| Peter Collingbourne | 8446f1f | 2017-04-14 02:55:06 +0000 | [diff] [blame] | 819 | fatal(Path + ": could not infer e_machine from bitcode target triple " + | 
|  | 820 | T.str()); | 
| Davide Italiano | 60976ba | 2016-06-29 06:12:39 +0000 | [diff] [blame] | 821 | } | 
|  | 822 | } | 
|  | 823 |  | 
| Peter Collingbourne | 8446f1f | 2017-04-14 02:55:06 +0000 | [diff] [blame] | 824 | BitcodeFile::BitcodeFile(MemoryBufferRef MB, StringRef ArchiveName, | 
|  | 825 | uint64_t OffsetInArchive) | 
|  | 826 | : InputFile(BitcodeKind, MB) { | 
|  | 827 | this->ArchiveName = ArchiveName; | 
|  | 828 |  | 
|  | 829 | // Here we pass a new MemoryBufferRef which is identified by ArchiveName | 
|  | 830 | // (the fully resolved path of the archive) + member name + offset of the | 
|  | 831 | // member in the archive. | 
|  | 832 | // ThinLTO uses the MemoryBufferRef identifier to access its internal | 
|  | 833 | // data structures and if two archives define two members with the same name, | 
|  | 834 | // this causes a collision which result in only one of the objects being | 
|  | 835 | // taken into consideration at LTO time (which very likely causes undefined | 
|  | 836 | // symbols later in the link stage). | 
|  | 837 | MemoryBufferRef MBRef(MB.getBuffer(), | 
|  | 838 | Saver.save(ArchiveName + MB.getBufferIdentifier() + | 
|  | 839 | utostr(OffsetInArchive))); | 
|  | 840 | Obj = check(lto::InputFile::create(MBRef), toString(this)); | 
|  | 841 |  | 
|  | 842 | Triple T(Obj->getTargetTriple()); | 
|  | 843 | EKind = getBitcodeELFKind(T); | 
|  | 844 | EMachine = getBitcodeMachineKind(MB.getBufferIdentifier(), T); | 
| Davide Italiano | 60976ba | 2016-06-29 06:12:39 +0000 | [diff] [blame] | 845 | } | 
| Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 846 |  | 
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 847 | static uint8_t mapVisibility(GlobalValue::VisibilityTypes GvVisibility) { | 
|  | 848 | switch (GvVisibility) { | 
| Rui Ueyama | 68fae23 | 2016-03-07 19:06:14 +0000 | [diff] [blame] | 849 | case GlobalValue::DefaultVisibility: | 
|  | 850 | return STV_DEFAULT; | 
| Rui Ueyama | fd4fee5 | 2016-03-07 00:54:17 +0000 | [diff] [blame] | 851 | case GlobalValue::HiddenVisibility: | 
|  | 852 | return STV_HIDDEN; | 
|  | 853 | case GlobalValue::ProtectedVisibility: | 
|  | 854 | return STV_PROTECTED; | 
| Rui Ueyama | fd4fee5 | 2016-03-07 00:54:17 +0000 | [diff] [blame] | 855 | } | 
| George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 856 | llvm_unreachable("unknown visibility"); | 
| Rui Ueyama | f714955 | 2016-03-11 18:46:51 +0000 | [diff] [blame] | 857 | } | 
|  | 858 |  | 
| Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 859 | template <class ELFT> | 
| Rafael Espindola | 3db7021 | 2016-10-25 12:02:31 +0000 | [diff] [blame] | 860 | static Symbol *createBitcodeSymbol(const std::vector<bool> &KeptComdats, | 
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 861 | const lto::InputFile::Symbol &ObjSym, | 
| Rui Ueyama | 55518e7 | 2016-10-28 20:57:25 +0000 | [diff] [blame] | 862 | BitcodeFile *F) { | 
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 863 | StringRef NameRef = Saver.save(ObjSym.getName()); | 
| Peter Collingbourne | 0d56b95 | 2017-03-28 22:31:35 +0000 | [diff] [blame] | 864 | uint32_t Binding = ObjSym.isWeak() ? STB_WEAK : STB_GLOBAL; | 
| Davide Italiano | 9f8efff | 2016-04-22 18:26:33 +0000 | [diff] [blame] | 865 |  | 
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 866 | uint8_t Type = ObjSym.isTLS() ? STT_TLS : STT_NOTYPE; | 
|  | 867 | uint8_t Visibility = mapVisibility(ObjSym.getVisibility()); | 
|  | 868 | bool CanOmitFromDynSym = ObjSym.canBeOmittedFromSymbolTable(); | 
| Davide Italiano | 29fa6ab | 2016-08-31 12:27:47 +0000 | [diff] [blame] | 869 |  | 
| Peter Collingbourne | 7b30f16 | 2017-03-31 04:47:07 +0000 | [diff] [blame] | 870 | int C = ObjSym.getComdatIndex(); | 
| Rafael Espindola | 3db7021 | 2016-10-25 12:02:31 +0000 | [diff] [blame] | 871 | if (C != -1 && !KeptComdats[C]) | 
| Rui Ueyama | a13efc2 | 2016-11-29 18:05:04 +0000 | [diff] [blame] | 872 | return Symtab<ELFT>::X->addUndefined(NameRef, /*IsLocal=*/false, Binding, | 
|  | 873 | Visibility, Type, CanOmitFromDynSym, | 
|  | 874 | F); | 
| Rui Ueyama | f714955 | 2016-03-11 18:46:51 +0000 | [diff] [blame] | 875 |  | 
| Peter Collingbourne | 0d56b95 | 2017-03-28 22:31:35 +0000 | [diff] [blame] | 876 | if (ObjSym.isUndefined()) | 
| Rui Ueyama | a13efc2 | 2016-11-29 18:05:04 +0000 | [diff] [blame] | 877 | return Symtab<ELFT>::X->addUndefined(NameRef, /*IsLocal=*/false, Binding, | 
|  | 878 | Visibility, Type, CanOmitFromDynSym, | 
|  | 879 | F); | 
| Davide Italiano | 9f8efff | 2016-04-22 18:26:33 +0000 | [diff] [blame] | 880 |  | 
| Peter Collingbourne | 0d56b95 | 2017-03-28 22:31:35 +0000 | [diff] [blame] | 881 | if (ObjSym.isCommon()) | 
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 882 | return Symtab<ELFT>::X->addCommon(NameRef, ObjSym.getCommonSize(), | 
|  | 883 | ObjSym.getCommonAlignment(), Binding, | 
|  | 884 | Visibility, STT_OBJECT, F); | 
|  | 885 |  | 
|  | 886 | return Symtab<ELFT>::X->addBitcode(NameRef, Binding, Visibility, Type, | 
|  | 887 | CanOmitFromDynSym, F); | 
| Rafael Espindola | 9b3acf9 | 2016-03-11 16:11:47 +0000 | [diff] [blame] | 888 | } | 
|  | 889 |  | 
| Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 890 | template <class ELFT> | 
| Rafael Espindola | 1c2baad | 2017-05-25 21:53:02 +0000 | [diff] [blame] | 891 | void BitcodeFile::parse(DenseSet<CachedHashStringRef> &ComdatGroups) { | 
| Rafael Espindola | 3db7021 | 2016-10-25 12:02:31 +0000 | [diff] [blame] | 892 | std::vector<bool> KeptComdats; | 
| Peter Collingbourne | 7b30f16 | 2017-03-31 04:47:07 +0000 | [diff] [blame] | 893 | for (StringRef S : Obj->getComdatTable()) | 
| Rafael Espindola | 1c2baad | 2017-05-25 21:53:02 +0000 | [diff] [blame] | 894 | KeptComdats.push_back(ComdatGroups.insert(CachedHashStringRef(S)).second); | 
| Rafael Espindola | 3db7021 | 2016-10-25 12:02:31 +0000 | [diff] [blame] | 895 |  | 
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 896 | for (const lto::InputFile::Symbol &ObjSym : Obj->symbols()) | 
| Rui Ueyama | 55518e7 | 2016-10-28 20:57:25 +0000 | [diff] [blame] | 897 | Symbols.push_back(createBitcodeSymbol<ELFT>(KeptComdats, ObjSym, this)); | 
| Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 898 | } | 
|  | 899 |  | 
| Rui Ueyama | 330e52b | 2017-04-26 22:51:51 +0000 | [diff] [blame] | 900 | static ELFKind getELFKind(MemoryBufferRef MB) { | 
| Rui Ueyama | 57bbdaf | 2016-04-08 00:18:25 +0000 | [diff] [blame] | 901 | unsigned char Size; | 
|  | 902 | unsigned char Endian; | 
|  | 903 | std::tie(Size, Endian) = getElfArchType(MB.getBuffer()); | 
| Rui Ueyama | 330e52b | 2017-04-26 22:51:51 +0000 | [diff] [blame] | 904 |  | 
| Rui Ueyama | 57bbdaf | 2016-04-08 00:18:25 +0000 | [diff] [blame] | 905 | if (Endian != ELFDATA2LSB && Endian != ELFDATA2MSB) | 
| Eugene Leviant | c3a44b2 | 2016-11-23 10:07:46 +0000 | [diff] [blame] | 906 | fatal(MB.getBufferIdentifier() + ": invalid data encoding"); | 
| Rui Ueyama | 330e52b | 2017-04-26 22:51:51 +0000 | [diff] [blame] | 907 | if (Size != ELFCLASS32 && Size != ELFCLASS64) | 
|  | 908 | fatal(MB.getBufferIdentifier() + ": invalid file class"); | 
| Rui Ueyama | c4b6506 | 2015-10-12 15:31:09 +0000 | [diff] [blame] | 909 |  | 
| Rafael Espindola | 22e9a8e | 2016-11-03 20:17:25 +0000 | [diff] [blame] | 910 | size_t BufSize = MB.getBuffer().size(); | 
|  | 911 | if ((Size == ELFCLASS32 && BufSize < sizeof(Elf32_Ehdr)) || | 
|  | 912 | (Size == ELFCLASS64 && BufSize < sizeof(Elf64_Ehdr))) | 
| Eugene Leviant | c3a44b2 | 2016-11-23 10:07:46 +0000 | [diff] [blame] | 913 | fatal(MB.getBufferIdentifier() + ": file is too short"); | 
| Rafael Espindola | 22e9a8e | 2016-11-03 20:17:25 +0000 | [diff] [blame] | 914 |  | 
| Rui Ueyama | 330e52b | 2017-04-26 22:51:51 +0000 | [diff] [blame] | 915 | if (Size == ELFCLASS32) | 
|  | 916 | return (Endian == ELFDATA2LSB) ? ELF32LEKind : ELF32BEKind; | 
|  | 917 | return (Endian == ELFDATA2LSB) ? ELF64LEKind : ELF64BEKind; | 
| Rui Ueyama | c4b6506 | 2015-10-12 15:31:09 +0000 | [diff] [blame] | 918 | } | 
|  | 919 |  | 
| Rafael Espindola | 093abab | 2016-10-27 17:45:40 +0000 | [diff] [blame] | 920 | template <class ELFT> void BinaryFile::parse() { | 
| Rui Ueyama | c9d82b9 | 2017-04-27 04:01:36 +0000 | [diff] [blame] | 921 | ArrayRef<uint8_t> Data = toArrayRef(MB.getBuffer()); | 
| Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 922 | auto *Section = | 
|  | 923 | make<InputSection>(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, 8, Data, ".data"); | 
| Rafael Espindola | 093abab | 2016-10-27 17:45:40 +0000 | [diff] [blame] | 924 | Sections.push_back(Section); | 
|  | 925 |  | 
| Rui Ueyama | c9d82b9 | 2017-04-27 04:01:36 +0000 | [diff] [blame] | 926 | // For each input file foo that is embedded to a result as a binary | 
|  | 927 | // blob, we define _binary_foo_{start,end,size} symbols, so that | 
|  | 928 | // user programs can access blobs by name. Non-alphanumeric | 
|  | 929 | // characters in a filename are replaced with underscore. | 
|  | 930 | std::string S = "_binary_" + MB.getBufferIdentifier().str(); | 
|  | 931 | for (size_t I = 0; I < S.size(); ++I) | 
|  | 932 | if (!isalnum(S[I])) | 
|  | 933 | S[I] = '_'; | 
|  | 934 |  | 
|  | 935 | elf::Symtab<ELFT>::X->addRegular(Saver.save(S + "_start"), STV_DEFAULT, | 
|  | 936 | STT_OBJECT, 0, 0, STB_GLOBAL, Section, | 
| George Rimar | 463984d | 2016-11-15 08:07:14 +0000 | [diff] [blame] | 937 | nullptr); | 
| Rui Ueyama | c9d82b9 | 2017-04-27 04:01:36 +0000 | [diff] [blame] | 938 | elf::Symtab<ELFT>::X->addRegular(Saver.save(S + "_end"), STV_DEFAULT, | 
|  | 939 | STT_OBJECT, Data.size(), 0, STB_GLOBAL, | 
|  | 940 | Section, nullptr); | 
|  | 941 | elf::Symtab<ELFT>::X->addRegular(Saver.save(S + "_size"), STV_DEFAULT, | 
|  | 942 | STT_OBJECT, Data.size(), 0, STB_GLOBAL, | 
|  | 943 | nullptr, nullptr); | 
| Michael J. Spencer | a9424f3 | 2016-09-09 22:08:04 +0000 | [diff] [blame] | 944 | } | 
|  | 945 |  | 
| Rui Ueyama | 4655ea3 | 2016-04-08 00:14:55 +0000 | [diff] [blame] | 946 | static bool isBitcode(MemoryBufferRef MB) { | 
|  | 947 | using namespace sys::fs; | 
|  | 948 | return identify_magic(MB.getBuffer()) == file_magic::bitcode; | 
|  | 949 | } | 
|  | 950 |  | 
| Rui Ueyama | 55518e7 | 2016-10-28 20:57:25 +0000 | [diff] [blame] | 951 | InputFile *elf::createObjectFile(MemoryBufferRef MB, StringRef ArchiveName, | 
| Davide Italiano | bcdd6c6 | 2016-10-12 19:35:54 +0000 | [diff] [blame] | 952 | uint64_t OffsetInArchive) { | 
| Rui Ueyama | 330e52b | 2017-04-26 22:51:51 +0000 | [diff] [blame] | 953 | if (isBitcode(MB)) | 
|  | 954 | return make<BitcodeFile>(MB, ArchiveName, OffsetInArchive); | 
|  | 955 |  | 
|  | 956 | switch (getELFKind(MB)) { | 
|  | 957 | case ELF32LEKind: | 
|  | 958 | return make<ObjectFile<ELF32LE>>(MB, ArchiveName); | 
|  | 959 | case ELF32BEKind: | 
|  | 960 | return make<ObjectFile<ELF32BE>>(MB, ArchiveName); | 
|  | 961 | case ELF64LEKind: | 
|  | 962 | return make<ObjectFile<ELF64LE>>(MB, ArchiveName); | 
|  | 963 | case ELF64BEKind: | 
|  | 964 | return make<ObjectFile<ELF64BE>>(MB, ArchiveName); | 
|  | 965 | default: | 
|  | 966 | llvm_unreachable("getELFKind"); | 
|  | 967 | } | 
| Rui Ueyama | 533c030 | 2016-01-06 00:09:43 +0000 | [diff] [blame] | 968 | } | 
|  | 969 |  | 
| Rafael Espindola | 3460cdd | 2017-04-24 21:44:20 +0000 | [diff] [blame] | 970 | InputFile *elf::createSharedFile(MemoryBufferRef MB, StringRef DefaultSoName) { | 
| Rui Ueyama | 330e52b | 2017-04-26 22:51:51 +0000 | [diff] [blame] | 971 | switch (getELFKind(MB)) { | 
|  | 972 | case ELF32LEKind: | 
|  | 973 | return make<SharedFile<ELF32LE>>(MB, DefaultSoName); | 
|  | 974 | case ELF32BEKind: | 
|  | 975 | return make<SharedFile<ELF32BE>>(MB, DefaultSoName); | 
|  | 976 | case ELF64LEKind: | 
|  | 977 | return make<SharedFile<ELF64LE>>(MB, DefaultSoName); | 
|  | 978 | case ELF64BEKind: | 
|  | 979 | return make<SharedFile<ELF64BE>>(MB, DefaultSoName); | 
|  | 980 | default: | 
|  | 981 | llvm_unreachable("getELFKind"); | 
|  | 982 | } | 
| Rui Ueyama | 533c030 | 2016-01-06 00:09:43 +0000 | [diff] [blame] | 983 | } | 
|  | 984 |  | 
| Rafael Espindola | 65c65ce | 2016-06-14 21:56:36 +0000 | [diff] [blame] | 985 | MemoryBufferRef LazyObjectFile::getBuffer() { | 
|  | 986 | if (Seen) | 
|  | 987 | return MemoryBufferRef(); | 
|  | 988 | Seen = true; | 
|  | 989 | return MB; | 
|  | 990 | } | 
|  | 991 |  | 
| Rafael Espindola | 808f2d3 | 2017-05-04 14:54:48 +0000 | [diff] [blame] | 992 | InputFile *LazyObjectFile::fetch() { | 
|  | 993 | MemoryBufferRef MBRef = getBuffer(); | 
|  | 994 | if (MBRef.getBuffer().empty()) | 
|  | 995 | return nullptr; | 
| Rafael Espindola | 0f6cc65 | 2017-05-05 15:17:07 +0000 | [diff] [blame] | 996 | return createObjectFile(MBRef, ArchiveName, OffsetInArchive); | 
| Rafael Espindola | 808f2d3 | 2017-05-04 14:54:48 +0000 | [diff] [blame] | 997 | } | 
|  | 998 |  | 
| George Rimar | 10874f7 | 2016-10-03 11:13:55 +0000 | [diff] [blame] | 999 | template <class ELFT> void LazyObjectFile::parse() { | 
| Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 1000 | for (StringRef Sym : getSymbols()) | 
| Rafael Espindola | 65c65ce | 2016-06-14 21:56:36 +0000 | [diff] [blame] | 1001 | Symtab<ELFT>::X->addLazyObject(Sym, *this); | 
| Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 1002 | } | 
|  | 1003 |  | 
|  | 1004 | template <class ELFT> std::vector<StringRef> LazyObjectFile::getElfSymbols() { | 
|  | 1005 | typedef typename ELFT::Shdr Elf_Shdr; | 
|  | 1006 | typedef typename ELFT::Sym Elf_Sym; | 
|  | 1007 | typedef typename ELFT::SymRange Elf_Sym_Range; | 
|  | 1008 |  | 
| Rafael Espindola | 22e9a8e | 2016-11-03 20:17:25 +0000 | [diff] [blame] | 1009 | const ELFFile<ELFT> Obj(this->MB.getBuffer()); | 
| Rui Ueyama | 4e4e866 | 2017-04-03 19:11:23 +0000 | [diff] [blame] | 1010 | ArrayRef<Elf_Shdr> Sections = check(Obj.sections(), toString(this)); | 
| Rafael Espindola | 6d18d38 | 2016-11-03 13:24:29 +0000 | [diff] [blame] | 1011 | for (const Elf_Shdr &Sec : Sections) { | 
| Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 1012 | if (Sec.sh_type != SHT_SYMTAB) | 
|  | 1013 | continue; | 
| Rui Ueyama | 37e60a5 | 2017-03-30 21:13:00 +0000 | [diff] [blame] | 1014 |  | 
| Rui Ueyama | 4e4e866 | 2017-04-03 19:11:23 +0000 | [diff] [blame] | 1015 | Elf_Sym_Range Syms = check(Obj.symbols(&Sec), toString(this)); | 
| Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 1016 | uint32_t FirstNonLocal = Sec.sh_info; | 
| Rui Ueyama | 37e60a5 | 2017-03-30 21:13:00 +0000 | [diff] [blame] | 1017 | StringRef StringTable = | 
| Rui Ueyama | 4e4e866 | 2017-04-03 19:11:23 +0000 | [diff] [blame] | 1018 | check(Obj.getStringTableForSymtab(Sec, Sections), toString(this)); | 
| Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 1019 | std::vector<StringRef> V; | 
| Rui Ueyama | 37e60a5 | 2017-03-30 21:13:00 +0000 | [diff] [blame] | 1020 |  | 
| Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 1021 | for (const Elf_Sym &Sym : Syms.slice(FirstNonLocal)) | 
| Rui Ueyama | 1f49289 | 2016-04-08 20:49:31 +0000 | [diff] [blame] | 1022 | if (Sym.st_shndx != SHN_UNDEF) | 
| Rui Ueyama | 4e4e866 | 2017-04-03 19:11:23 +0000 | [diff] [blame] | 1023 | V.push_back(check(Sym.getName(StringTable), toString(this))); | 
| Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 1024 | return V; | 
|  | 1025 | } | 
|  | 1026 | return {}; | 
|  | 1027 | } | 
|  | 1028 |  | 
|  | 1029 | std::vector<StringRef> LazyObjectFile::getBitcodeSymbols() { | 
| Rui Ueyama | 37e60a5 | 2017-03-30 21:13:00 +0000 | [diff] [blame] | 1030 | std::unique_ptr<lto::InputFile> Obj = | 
| Rui Ueyama | 4e4e866 | 2017-04-03 19:11:23 +0000 | [diff] [blame] | 1031 | check(lto::InputFile::create(this->MB), toString(this)); | 
| Rui Ueyama | d72dd1f | 2016-09-29 00:58:10 +0000 | [diff] [blame] | 1032 | std::vector<StringRef> V; | 
|  | 1033 | for (const lto::InputFile::Symbol &Sym : Obj->symbols()) | 
| Peter Collingbourne | 0d56b95 | 2017-03-28 22:31:35 +0000 | [diff] [blame] | 1034 | if (!Sym.isUndefined()) | 
| Rui Ueyama | d72dd1f | 2016-09-29 00:58:10 +0000 | [diff] [blame] | 1035 | V.push_back(Saver.save(Sym.getName())); | 
| Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 1036 | return V; | 
|  | 1037 | } | 
|  | 1038 |  | 
| Rui Ueyama | 1f49289 | 2016-04-08 20:49:31 +0000 | [diff] [blame] | 1039 | // Returns a vector of globally-visible defined symbol names. | 
| Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 1040 | std::vector<StringRef> LazyObjectFile::getSymbols() { | 
| Rui Ueyama | 4655ea3 | 2016-04-08 00:14:55 +0000 | [diff] [blame] | 1041 | if (isBitcode(this->MB)) | 
| Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 1042 | return getBitcodeSymbols(); | 
|  | 1043 |  | 
| Rui Ueyama | 330e52b | 2017-04-26 22:51:51 +0000 | [diff] [blame] | 1044 | switch (getELFKind(this->MB)) { | 
|  | 1045 | case ELF32LEKind: | 
|  | 1046 | return getElfSymbols<ELF32LE>(); | 
|  | 1047 | case ELF32BEKind: | 
| Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 1048 | return getElfSymbols<ELF32BE>(); | 
| Rui Ueyama | 330e52b | 2017-04-26 22:51:51 +0000 | [diff] [blame] | 1049 | case ELF64LEKind: | 
| Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 1050 | return getElfSymbols<ELF64LE>(); | 
| Rui Ueyama | 330e52b | 2017-04-26 22:51:51 +0000 | [diff] [blame] | 1051 | case ELF64BEKind: | 
|  | 1052 | return getElfSymbols<ELF64BE>(); | 
|  | 1053 | default: | 
|  | 1054 | llvm_unreachable("getELFKind"); | 
|  | 1055 | } | 
| Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 1056 | } | 
|  | 1057 |  | 
| Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 1058 | template void ArchiveFile::parse<ELF32LE>(); | 
|  | 1059 | template void ArchiveFile::parse<ELF32BE>(); | 
|  | 1060 | template void ArchiveFile::parse<ELF64LE>(); | 
|  | 1061 | template void ArchiveFile::parse<ELF64BE>(); | 
|  | 1062 |  | 
| Rafael Espindola | 1c2baad | 2017-05-25 21:53:02 +0000 | [diff] [blame] | 1063 | template void BitcodeFile::parse<ELF32LE>(DenseSet<CachedHashStringRef> &); | 
|  | 1064 | template void BitcodeFile::parse<ELF32BE>(DenseSet<CachedHashStringRef> &); | 
|  | 1065 | template void BitcodeFile::parse<ELF64LE>(DenseSet<CachedHashStringRef> &); | 
|  | 1066 | template void BitcodeFile::parse<ELF64BE>(DenseSet<CachedHashStringRef> &); | 
| Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 1067 |  | 
|  | 1068 | template void LazyObjectFile::parse<ELF32LE>(); | 
|  | 1069 | template void LazyObjectFile::parse<ELF32BE>(); | 
|  | 1070 | template void LazyObjectFile::parse<ELF64LE>(); | 
|  | 1071 | template void LazyObjectFile::parse<ELF64BE>(); | 
|  | 1072 |  | 
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 1073 | template class elf::ELFFileBase<ELF32LE>; | 
|  | 1074 | template class elf::ELFFileBase<ELF32BE>; | 
|  | 1075 | template class elf::ELFFileBase<ELF64LE>; | 
|  | 1076 | template class elf::ELFFileBase<ELF64BE>; | 
| Davide Italiano | 6d328d3 | 2015-09-16 20:45:57 +0000 | [diff] [blame] | 1077 |  | 
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 1078 | template class elf::ObjectFile<ELF32LE>; | 
|  | 1079 | template class elf::ObjectFile<ELF32BE>; | 
|  | 1080 | template class elf::ObjectFile<ELF64LE>; | 
|  | 1081 | template class elf::ObjectFile<ELF64BE>; | 
| Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 1082 |  | 
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 1083 | template class elf::SharedFile<ELF32LE>; | 
|  | 1084 | template class elf::SharedFile<ELF32BE>; | 
|  | 1085 | template class elf::SharedFile<ELF64LE>; | 
|  | 1086 | template class elf::SharedFile<ELF64BE>; | 
| Michael J. Spencer | a9424f3 | 2016-09-09 22:08:04 +0000 | [diff] [blame] | 1087 |  | 
| Rafael Espindola | 093abab | 2016-10-27 17:45:40 +0000 | [diff] [blame] | 1088 | template void BinaryFile::parse<ELF32LE>(); | 
|  | 1089 | template void BinaryFile::parse<ELF32BE>(); | 
|  | 1090 | template void BinaryFile::parse<ELF64LE>(); | 
|  | 1091 | template void BinaryFile::parse<ELF64BE>(); |