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