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