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