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