/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/COFF/InputFiles.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===- InputFiles.cpp -----------------------------------------------------===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | |
9 | | #include "InputFiles.h" |
10 | | #include "Chunks.h" |
11 | | #include "Config.h" |
12 | | #include "Driver.h" |
13 | | #include "SymbolTable.h" |
14 | | #include "Symbols.h" |
15 | | #include "lld/Common/ErrorHandler.h" |
16 | | #include "lld/Common/Memory.h" |
17 | | #include "llvm-c/lto.h" |
18 | | #include "llvm/ADT/SmallVector.h" |
19 | | #include "llvm/ADT/Triple.h" |
20 | | #include "llvm/ADT/Twine.h" |
21 | | #include "llvm/BinaryFormat/COFF.h" |
22 | | #include "llvm/Object/Binary.h" |
23 | | #include "llvm/Object/COFF.h" |
24 | | #include "llvm/Support/Casting.h" |
25 | | #include "llvm/Support/Endian.h" |
26 | | #include "llvm/Support/Error.h" |
27 | | #include "llvm/Support/ErrorOr.h" |
28 | | #include "llvm/Support/FileSystem.h" |
29 | | #include "llvm/Support/Path.h" |
30 | | #include "llvm/Target/TargetOptions.h" |
31 | | #include <cstring> |
32 | | #include <system_error> |
33 | | #include <utility> |
34 | | |
35 | | using namespace llvm; |
36 | | using namespace llvm::COFF; |
37 | | using namespace llvm::object; |
38 | | using namespace llvm::support::endian; |
39 | | |
40 | | using llvm::Triple; |
41 | | using llvm::support::ulittle32_t; |
42 | | |
43 | | namespace lld { |
44 | | namespace coff { |
45 | | |
46 | | std::vector<ObjFile *> ObjFile::Instances; |
47 | | std::vector<ImportFile *> ImportFile::Instances; |
48 | | std::vector<BitcodeFile *> BitcodeFile::Instances; |
49 | | |
50 | | /// Checks that Source is compatible with being a weak alias to Target. |
51 | | /// If Source is Undefined and has no weak alias set, makes it a weak |
52 | | /// alias to Target. |
53 | | static void checkAndSetWeakAlias(SymbolTable *Symtab, InputFile *F, |
54 | 11 | Symbol *Source, Symbol *Target) { |
55 | 11 | if (auto *U = dyn_cast<Undefined>(Source)) { |
56 | 10 | if (U->WeakAlias && U->WeakAlias != Target2 ) { |
57 | 2 | // Weak aliases as produced by GCC are named in the form |
58 | 2 | // .weak.<weaksymbol>.<othersymbol>, where <othersymbol> is the name |
59 | 2 | // of another symbol emitted near the weak symbol. |
60 | 2 | // Just use the definition from the first object file that defined |
61 | 2 | // this weak symbol. |
62 | 2 | if (Config->MinGW) |
63 | 2 | return; |
64 | 0 | Symtab->reportDuplicate(Source, F); |
65 | 0 | } |
66 | 10 | U->WeakAlias = Target; |
67 | 8 | } |
68 | 11 | } |
69 | | |
70 | 85 | ArchiveFile::ArchiveFile(MemoryBufferRef M) : InputFile(ArchiveKind, M) {} |
71 | | |
72 | 85 | void ArchiveFile::parse() { |
73 | 85 | // Parse a MemoryBufferRef as an archive file. |
74 | 85 | File = CHECK(Archive::create(MB), this); |
75 | 85 | |
76 | 85 | // Read the symbol table to construct Lazy objects. |
77 | 85 | for (const Archive::Symbol &Sym : File->symbols()) |
78 | 461 | Symtab->addLazy(this, Sym); |
79 | 85 | } |
80 | | |
81 | | // Returns a buffer pointing to a member file containing a given symbol. |
82 | 142 | void ArchiveFile::addMember(const Archive::Symbol *Sym) { |
83 | 142 | const Archive::Child &C = |
84 | 142 | CHECK(Sym->getMember(), |
85 | 142 | "could not get the member for symbol " + Sym->getName()); |
86 | 142 | |
87 | 142 | // Return an empty buffer if we have already returned the same buffer. |
88 | 142 | if (!Seen.insert(C.getChildOffset()).second) |
89 | 1 | return; |
90 | 141 | |
91 | 141 | Driver->enqueueArchiveMember(C, Sym->getName(), getName()); |
92 | 141 | } |
93 | | |
94 | 5 | std::vector<MemoryBufferRef> getArchiveMembers(Archive *File) { |
95 | 5 | std::vector<MemoryBufferRef> V; |
96 | 5 | Error Err = Error::success(); |
97 | 5 | for (const ErrorOr<Archive::Child> &COrErr : File->children(Err)) { |
98 | 5 | Archive::Child C = |
99 | 5 | CHECK(COrErr, |
100 | 5 | File->getFileName() + ": could not get the child of the archive"); |
101 | 5 | MemoryBufferRef MBRef = |
102 | 5 | CHECK(C.getMemoryBufferRef(), |
103 | 5 | File->getFileName() + |
104 | 5 | ": could not get the buffer for a child of the archive"); |
105 | 5 | V.push_back(MBRef); |
106 | 5 | } |
107 | 5 | if (Err) |
108 | 0 | fatal(File->getFileName() + |
109 | 0 | ": Archive::children failed: " + toString(std::move(Err))); |
110 | 5 | return V; |
111 | 5 | } |
112 | | |
113 | 728 | void ObjFile::parse() { |
114 | 728 | // Parse a memory buffer as a COFF file. |
115 | 728 | std::unique_ptr<Binary> Bin = CHECK(createBinary(MB), this); |
116 | 728 | |
117 | 728 | if (auto *Obj = dyn_cast<COFFObjectFile>(Bin.get())) { |
118 | 728 | Bin.release(); |
119 | 728 | COFFObj.reset(Obj); |
120 | 728 | } else { |
121 | 0 | fatal(toString(this) + " is not a COFF file"); |
122 | 0 | } |
123 | 728 | |
124 | 728 | // Read section and symbol tables. |
125 | 728 | initializeChunks(); |
126 | 728 | initializeSymbols(); |
127 | 728 | } |
128 | | |
129 | 5.22k | const coff_section* ObjFile::getSection(uint32_t I) { |
130 | 5.22k | const coff_section *Sec; |
131 | 5.22k | if (auto EC = COFFObj->getSection(I, Sec)) |
132 | 0 | fatal("getSection failed: #" + Twine(I) + ": " + EC.message()); |
133 | 5.22k | return Sec; |
134 | 5.22k | } |
135 | | |
136 | | // We set SectionChunk pointers in the SparseChunks vector to this value |
137 | | // temporarily to mark comdat sections as having an unknown resolution. As we |
138 | | // walk the object file's symbol table, once we visit either a leader symbol or |
139 | | // an associative section definition together with the parent comdat's leader, |
140 | | // we set the pointer to either nullptr (to mark the section as discarded) or a |
141 | | // valid SectionChunk for that section. |
142 | | static SectionChunk *const PendingComdat = reinterpret_cast<SectionChunk *>(1); |
143 | | |
144 | 728 | void ObjFile::initializeChunks() { |
145 | 728 | uint32_t NumSections = COFFObj->getNumberOfSections(); |
146 | 728 | Chunks.reserve(NumSections); |
147 | 728 | SparseChunks.resize(NumSections + 1); |
148 | 3.35k | for (uint32_t I = 1; I < NumSections + 1; ++I2.63k ) { |
149 | 2.63k | const coff_section *Sec = getSection(I); |
150 | 2.63k | if (Sec->Characteristics & IMAGE_SCN_LNK_COMDAT) |
151 | 490 | SparseChunks[I] = PendingComdat; |
152 | 2.14k | else |
153 | 2.14k | SparseChunks[I] = readSection(I, nullptr, ""); |
154 | 2.63k | } |
155 | 728 | } |
156 | | |
157 | | SectionChunk *ObjFile::readSection(uint32_t SectionNumber, |
158 | | const coff_aux_section_definition *Def, |
159 | 2.57k | StringRef LeaderName) { |
160 | 2.57k | const coff_section *Sec = getSection(SectionNumber); |
161 | 2.57k | |
162 | 2.57k | StringRef Name; |
163 | 2.57k | if (auto EC = COFFObj->getSectionName(Sec, Name)) |
164 | 0 | fatal("getSectionName failed: #" + Twine(SectionNumber) + ": " + |
165 | 0 | EC.message()); |
166 | 2.57k | |
167 | 2.57k | if (Name == ".drectve") { |
168 | 176 | ArrayRef<uint8_t> Data; |
169 | 176 | COFFObj->getSectionContents(Sec, Data); |
170 | 176 | Directives = std::string((const char *)Data.data(), Data.size()); |
171 | 176 | return nullptr; |
172 | 176 | } |
173 | 2.39k | |
174 | 2.39k | if (Name == ".llvm_addrsig") { |
175 | 4 | AddrsigSec = Sec; |
176 | 4 | return nullptr; |
177 | 4 | } |
178 | 2.39k | |
179 | 2.39k | // Object files may have DWARF debug info or MS CodeView debug info |
180 | 2.39k | // (or both). |
181 | 2.39k | // |
182 | 2.39k | // DWARF sections don't need any special handling from the perspective |
183 | 2.39k | // of the linker; they are just a data section containing relocations. |
184 | 2.39k | // We can just link them to complete debug info. |
185 | 2.39k | // |
186 | 2.39k | // CodeView needs linker support. We need to interpret debug info, |
187 | 2.39k | // and then write it to a separate .pdb file. |
188 | 2.39k | |
189 | 2.39k | // Ignore DWARF debug info unless /debug is given. |
190 | 2.39k | if (!Config->Debug && Name.startswith(".debug_")1.77k ) |
191 | 16 | return nullptr; |
192 | 2.37k | |
193 | 2.37k | if (Sec->Characteristics & llvm::COFF::IMAGE_SCN_LNK_REMOVE) |
194 | 9 | return nullptr; |
195 | 2.36k | auto *C = make<SectionChunk>(this, Sec); |
196 | 2.36k | if (Def) |
197 | 433 | C->Checksum = Def->CheckSum; |
198 | 2.36k | |
199 | 2.36k | // CodeView sections are stored to a different vector because they are not |
200 | 2.36k | // linked in the regular manner. |
201 | 2.36k | if (C->isCodeView()) |
202 | 226 | DebugChunks.push_back(C); |
203 | 2.14k | else if (Config->GuardCF != GuardCFLevel::Off && Name == ".gfids$y"109 ) |
204 | 10 | GuardFidChunks.push_back(C); |
205 | 2.13k | else if (Config->GuardCF != GuardCFLevel::Off && Name == ".gljmp$y"99 ) |
206 | 1 | GuardLJmpChunks.push_back(C); |
207 | 2.13k | else if (Name == ".sxdata") |
208 | 8 | SXDataChunks.push_back(C); |
209 | 2.12k | else if (Config->TailMerge && Sec->NumberOfRelocations == 01.61k && |
210 | 2.12k | Name == ".rdata"1.33k && LeaderName.startswith("??_C@")44 ) |
211 | 12 | // COFF sections that look like string literal sections (i.e. no |
212 | 12 | // relocations, in .rdata, leader symbol name matches the MSVC name mangling |
213 | 12 | // for string literals) are subject to string tail merging. |
214 | 12 | MergeChunk::addSection(C); |
215 | 2.11k | else |
216 | 2.11k | Chunks.push_back(C); |
217 | 2.36k | |
218 | 2.36k | return C; |
219 | 2.36k | } |
220 | | |
221 | | void ObjFile::readAssociativeDefinition( |
222 | 116 | COFFSymbolRef Sym, const coff_aux_section_definition *Def) { |
223 | 116 | readAssociativeDefinition(Sym, Def, Def->getNumber(Sym.isBigObj())); |
224 | 116 | } |
225 | | |
226 | | void ObjFile::readAssociativeDefinition(COFFSymbolRef Sym, |
227 | | const coff_aux_section_definition *Def, |
228 | 120 | uint32_t ParentIndex) { |
229 | 120 | SectionChunk *Parent = SparseChunks[ParentIndex]; |
230 | 120 | int32_t SectionNumber = Sym.getSectionNumber(); |
231 | 120 | |
232 | 120 | auto Diag = [&]() { |
233 | 2 | StringRef Name, ParentName; |
234 | 2 | COFFObj->getSymbolName(Sym, Name); |
235 | 2 | |
236 | 2 | const coff_section *ParentSec = getSection(ParentIndex); |
237 | 2 | COFFObj->getSectionName(ParentSec, ParentName); |
238 | 2 | error(toString(this) + ": associative comdat " + Name + " (sec " + |
239 | 2 | Twine(SectionNumber) + ") has invalid reference to section " + |
240 | 2 | ParentName + " (sec " + Twine(ParentIndex) + ")"); |
241 | 2 | }; |
242 | 120 | |
243 | 120 | if (Parent == PendingComdat) { |
244 | 2 | // This can happen if an associative comdat refers to another associative |
245 | 2 | // comdat that appears after it (invalid per COFF spec) or to a section |
246 | 2 | // without any symbols. |
247 | 2 | Diag(); |
248 | 2 | return; |
249 | 2 | } |
250 | 118 | |
251 | 118 | // Check whether the parent is prevailing. If it is, so are we, and we read |
252 | 118 | // the section; otherwise mark it as discarded. |
253 | 118 | if (Parent) { |
254 | 107 | SectionChunk *C = readSection(SectionNumber, Def, ""); |
255 | 107 | SparseChunks[SectionNumber] = C; |
256 | 107 | if (C) { |
257 | 107 | C->Selection = IMAGE_COMDAT_SELECT_ASSOCIATIVE; |
258 | 107 | Parent->addAssociative(C); |
259 | 107 | } |
260 | 107 | } else { |
261 | 11 | SparseChunks[SectionNumber] = nullptr; |
262 | 11 | } |
263 | 118 | } |
264 | | |
265 | | void ObjFile::recordPrevailingSymbolForMingw( |
266 | 6 | COFFSymbolRef Sym, DenseMap<StringRef, uint32_t> &PrevailingSectionMap) { |
267 | 6 | // For comdat symbols in executable sections, where this is the copy |
268 | 6 | // of the section chunk we actually include instead of discarding it, |
269 | 6 | // add the symbol to a map to allow using it for implicitly |
270 | 6 | // associating .[px]data$<func> sections to it. |
271 | 6 | int32_t SectionNumber = Sym.getSectionNumber(); |
272 | 6 | SectionChunk *SC = SparseChunks[SectionNumber]; |
273 | 6 | if (SC && SC->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE) { |
274 | 4 | StringRef Name; |
275 | 4 | COFFObj->getSymbolName(Sym, Name); |
276 | 4 | PrevailingSectionMap[Name] = SectionNumber; |
277 | 4 | } |
278 | 6 | } |
279 | | |
280 | | void ObjFile::maybeAssociateSEHForMingw( |
281 | | COFFSymbolRef Sym, const coff_aux_section_definition *Def, |
282 | 21 | const DenseMap<StringRef, uint32_t> &PrevailingSectionMap) { |
283 | 21 | StringRef Name; |
284 | 21 | COFFObj->getSymbolName(Sym, Name); |
285 | 21 | if (Name.consume_front(".pdata$") || Name.consume_front(".xdata$")19 ) { |
286 | 10 | // For MinGW, treat .[px]data$<func> as implicitly associative to |
287 | 10 | // the symbol <func>. |
288 | 10 | auto ParentSym = PrevailingSectionMap.find(Name); |
289 | 10 | if (ParentSym != PrevailingSectionMap.end()) |
290 | 4 | readAssociativeDefinition(Sym, Def, ParentSym->second); |
291 | 10 | } |
292 | 21 | } |
293 | | |
294 | 3.92k | Symbol *ObjFile::createRegular(COFFSymbolRef Sym) { |
295 | 3.92k | SectionChunk *SC = SparseChunks[Sym.getSectionNumber()]; |
296 | 3.92k | if (Sym.isExternal()) { |
297 | 975 | StringRef Name; |
298 | 975 | COFFObj->getSymbolName(Sym, Name); |
299 | 975 | if (SC) |
300 | 974 | return Symtab->addRegular(this, Name, Sym.getGeneric(), SC); |
301 | 1 | // For MinGW symbols named .weak.* that point to a discarded section, |
302 | 1 | // don't create an Undefined symbol. If nothing ever refers to the symbol, |
303 | 1 | // everything should be fine. If something actually refers to the symbol |
304 | 1 | // (e.g. the undefined weak alias), linking will fail due to undefined |
305 | 1 | // references at the end. |
306 | 1 | if (Config->MinGW && Name.startswith(".weak.")) |
307 | 1 | return nullptr; |
308 | 0 | return Symtab->addUndefined(Name, this, false); |
309 | 0 | } |
310 | 2.95k | if (SC) |
311 | 2.77k | return make<DefinedRegular>(this, /*Name*/ "", /*IsCOMDAT*/ false, |
312 | 2.77k | /*IsExternal*/ false, Sym.getGeneric(), SC); |
313 | 180 | return nullptr; |
314 | 180 | } |
315 | | |
316 | 728 | void ObjFile::initializeSymbols() { |
317 | 728 | uint32_t NumSymbols = COFFObj->getNumberOfSymbols(); |
318 | 728 | Symbols.resize(NumSymbols); |
319 | 728 | |
320 | 728 | SmallVector<std::pair<Symbol *, uint32_t>, 8> WeakAliases; |
321 | 728 | std::vector<uint32_t> PendingIndexes; |
322 | 728 | PendingIndexes.reserve(NumSymbols); |
323 | 728 | |
324 | 728 | DenseMap<StringRef, uint32_t> PrevailingSectionMap; |
325 | 728 | std::vector<const coff_aux_section_definition *> ComdatDefs( |
326 | 728 | COFFObj->getNumberOfSections() + 1); |
327 | 728 | |
328 | 5.69k | for (uint32_t I = 0; I < NumSymbols; ++I4.96k ) { |
329 | 4.96k | COFFSymbolRef COFFSym = check(COFFObj->getSymbol(I)); |
330 | 4.96k | bool PrevailingComdat; |
331 | 4.96k | if (COFFSym.isUndefined()) { |
332 | 293 | Symbols[I] = createUndefined(COFFSym); |
333 | 4.67k | } else if (COFFSym.isWeakExternal()) { |
334 | 9 | Symbols[I] = createUndefined(COFFSym); |
335 | 9 | uint32_t TagIndex = COFFSym.getAux<coff_aux_weak_external>()->TagIndex; |
336 | 9 | WeakAliases.emplace_back(Symbols[I], TagIndex); |
337 | 4.66k | } else if (Optional<Symbol *> OptSym = |
338 | 4.14k | createDefined(COFFSym, ComdatDefs, PrevailingComdat)) { |
339 | 4.14k | Symbols[I] = *OptSym; |
340 | 4.14k | if (Config->MinGW && PrevailingComdat398 ) |
341 | 6 | recordPrevailingSymbolForMingw(COFFSym, PrevailingSectionMap); |
342 | 4.14k | } else { |
343 | 518 | // createDefined() returns None if a symbol belongs to a section that |
344 | 518 | // was pending at the point when the symbol was read. This can happen in |
345 | 518 | // two cases: |
346 | 518 | // 1) section definition symbol for a comdat leader; |
347 | 518 | // 2) symbol belongs to a comdat section associated with another section. |
348 | 518 | // In both of these cases, we can expect the section to be resolved by |
349 | 518 | // the time we finish visiting the remaining symbols in the symbol |
350 | 518 | // table. So we postpone the handling of this symbol until that time. |
351 | 518 | PendingIndexes.push_back(I); |
352 | 518 | } |
353 | 4.96k | I += COFFSym.getNumberOfAuxSymbols(); |
354 | 4.96k | } |
355 | 728 | |
356 | 728 | for (uint32_t I : PendingIndexes) { |
357 | 518 | COFFSymbolRef Sym = check(COFFObj->getSymbol(I)); |
358 | 518 | if (const coff_aux_section_definition *Def = Sym.getSectionDefinition()) { |
359 | 490 | if (Def->Selection == IMAGE_COMDAT_SELECT_ASSOCIATIVE) |
360 | 116 | readAssociativeDefinition(Sym, Def); |
361 | 374 | else if (Config->MinGW) |
362 | 21 | maybeAssociateSEHForMingw(Sym, Def, PrevailingSectionMap); |
363 | 490 | } |
364 | 518 | if (SparseChunks[Sym.getSectionNumber()] == PendingComdat) { |
365 | 12 | StringRef Name; |
366 | 12 | COFFObj->getSymbolName(Sym, Name); |
367 | 12 | log("comdat section " + Name + |
368 | 12 | " without leader and unassociated, discarding"); |
369 | 12 | continue; |
370 | 12 | } |
371 | 506 | Symbols[I] = createRegular(Sym); |
372 | 506 | } |
373 | 728 | |
374 | 728 | for (auto &KV : WeakAliases) { |
375 | 9 | Symbol *Sym = KV.first; |
376 | 9 | uint32_t Idx = KV.second; |
377 | 9 | checkAndSetWeakAlias(Symtab, this, Sym, Symbols[Idx]); |
378 | 9 | } |
379 | 728 | } |
380 | | |
381 | 302 | Symbol *ObjFile::createUndefined(COFFSymbolRef Sym) { |
382 | 302 | StringRef Name; |
383 | 302 | COFFObj->getSymbolName(Sym, Name); |
384 | 302 | return Symtab->addUndefined(Name, this, Sym.isWeakExternal()); |
385 | 302 | } |
386 | | |
387 | | void ObjFile::handleComdatSelection(COFFSymbolRef Sym, COMDATType &Selection, |
388 | 340 | bool &Prevailing, DefinedRegular *Leader) { |
389 | 340 | if (Prevailing) |
390 | 299 | return; |
391 | 41 | // There's already an existing comdat for this symbol: `Leader`. |
392 | 41 | // Use the comdats's selection field to determine if the new |
393 | 41 | // symbol in `Sym` should be discarded, produce a duplicate symbol |
394 | 41 | // error, etc. |
395 | 41 | |
396 | 41 | SectionChunk *LeaderChunk = nullptr; |
397 | 41 | COMDATType LeaderSelection = IMAGE_COMDAT_SELECT_ANY; |
398 | 41 | |
399 | 41 | if (Leader->Data) { |
400 | 37 | LeaderChunk = Leader->getChunk(); |
401 | 37 | LeaderSelection = LeaderChunk->Selection; |
402 | 37 | } else { |
403 | 4 | // FIXME: comdats from LTO files don't know their selection; treat them |
404 | 4 | // as "any". |
405 | 4 | Selection = LeaderSelection; |
406 | 4 | } |
407 | 41 | |
408 | 41 | if ((Selection == IMAGE_COMDAT_SELECT_ANY && |
409 | 41 | LeaderSelection == IMAGE_COMDAT_SELECT_LARGEST20 ) || |
410 | 41 | (40 Selection == IMAGE_COMDAT_SELECT_LARGEST40 && |
411 | 40 | LeaderSelection == IMAGE_COMDAT_SELECT_ANY11 )) { |
412 | 2 | // cl.exe picks "any" for vftables when building with /GR- and |
413 | 2 | // "largest" when building with /GR. To be able to link object files |
414 | 2 | // compiled with each flag, "any" and "largest" are merged as "largest". |
415 | 2 | LeaderSelection = Selection = IMAGE_COMDAT_SELECT_LARGEST; |
416 | 2 | } |
417 | 41 | |
418 | 41 | // Other than that, comdat selections must match. This is a bit more |
419 | 41 | // strict than link.exe which allows merging "any" and "largest" if "any" |
420 | 41 | // is the first symbol the linker sees, and it allows merging "largest" |
421 | 41 | // with everything (!) if "largest" is the first symbol the linker sees. |
422 | 41 | // Making this symmetric independent of which selection is seen first |
423 | 41 | // seems better though. |
424 | 41 | // (This behavior matches ModuleLinker::getComdatResult().) |
425 | 41 | if (Selection != LeaderSelection) { |
426 | 5 | log(("conflicting comdat type for " + toString(*Leader) + ": " + |
427 | 5 | Twine((int)LeaderSelection) + " in " + toString(Leader->getFile()) + |
428 | 5 | " and " + Twine((int)Selection) + " in " + toString(this)) |
429 | 5 | .str()); |
430 | 5 | Symtab->reportDuplicate(Leader, this); |
431 | 5 | return; |
432 | 5 | } |
433 | 36 | |
434 | 36 | switch (Selection) { |
435 | 36 | case IMAGE_COMDAT_SELECT_NODUPLICATES: |
436 | 1 | Symtab->reportDuplicate(Leader, this); |
437 | 1 | break; |
438 | 36 | |
439 | 36 | case IMAGE_COMDAT_SELECT_ANY: |
440 | 18 | // Nothing to do. |
441 | 18 | break; |
442 | 36 | |
443 | 36 | case IMAGE_COMDAT_SELECT_SAME_SIZE: |
444 | 2 | if (LeaderChunk->getSize() != getSection(Sym)->SizeOfRawData) |
445 | 1 | Symtab->reportDuplicate(Leader, this); |
446 | 2 | break; |
447 | 36 | |
448 | 36 | case IMAGE_COMDAT_SELECT_EXACT_MATCH: { |
449 | 3 | SectionChunk NewChunk(this, getSection(Sym)); |
450 | 3 | // link.exe only compares section contents here and doesn't complain |
451 | 3 | // if the two comdat sections have e.g. different alignment. |
452 | 3 | // Match that. |
453 | 3 | if (LeaderChunk->getContents() != NewChunk.getContents()) |
454 | 2 | Symtab->reportDuplicate(Leader, this); |
455 | 3 | break; |
456 | 36 | } |
457 | 36 | |
458 | 36 | case IMAGE_COMDAT_SELECT_ASSOCIATIVE: |
459 | 0 | // createDefined() is never called for IMAGE_COMDAT_SELECT_ASSOCIATIVE. |
460 | 0 | // (This means lld-link doesn't produce duplicate symbol errors for |
461 | 0 | // associative comdats while link.exe does, but associate comdats |
462 | 0 | // are never extern in practice.) |
463 | 0 | llvm_unreachable("createDefined not called for associative comdats"); |
464 | 36 | |
465 | 36 | case IMAGE_COMDAT_SELECT_LARGEST: |
466 | 12 | if (LeaderChunk->getSize() < getSection(Sym)->SizeOfRawData) { |
467 | 5 | // Replace the existing comdat symbol with the new one. |
468 | 5 | StringRef Name; |
469 | 5 | COFFObj->getSymbolName(Sym, Name); |
470 | 5 | // FIXME: This is incorrect: With /opt:noref, the previous sections |
471 | 5 | // make it into the final executable as well. Correct handling would |
472 | 5 | // be to undo reading of the whole old section that's being replaced, |
473 | 5 | // or doing one pass that determines what the final largest comdat |
474 | 5 | // is for all IMAGE_COMDAT_SELECT_LARGEST comdats and then reading |
475 | 5 | // only the largest one. |
476 | 5 | replaceSymbol<DefinedRegular>(Leader, this, Name, /*IsCOMDAT*/ true, |
477 | 5 | /*IsExternal*/ true, Sym.getGeneric(), |
478 | 5 | nullptr); |
479 | 5 | Prevailing = true; |
480 | 5 | } |
481 | 12 | break; |
482 | 36 | |
483 | 36 | case IMAGE_COMDAT_SELECT_NEWEST: |
484 | 0 | llvm_unreachable("should have been rejected earlier"); |
485 | 36 | } |
486 | 36 | } |
487 | | |
488 | | Optional<Symbol *> ObjFile::createDefined( |
489 | | COFFSymbolRef Sym, |
490 | | std::vector<const coff_aux_section_definition *> &ComdatDefs, |
491 | 4.66k | bool &Prevailing) { |
492 | 4.66k | Prevailing = false; |
493 | 4.66k | auto GetName = [&]() { |
494 | 956 | StringRef S; |
495 | 956 | COFFObj->getSymbolName(Sym, S); |
496 | 956 | return S; |
497 | 956 | }; |
498 | 4.66k | |
499 | 4.66k | if (Sym.isCommon()) { |
500 | 12 | auto *C = make<CommonChunk>(Sym); |
501 | 12 | Chunks.push_back(C); |
502 | 12 | return Symtab->addCommon(this, GetName(), Sym.getValue(), Sym.getGeneric(), |
503 | 12 | C); |
504 | 12 | } |
505 | 4.65k | |
506 | 4.65k | if (Sym.isAbsolute()) { |
507 | 278 | StringRef Name = GetName(); |
508 | 278 | |
509 | 278 | // Skip special symbols. |
510 | 278 | if (Name == "@comp.id") |
511 | 80 | return nullptr; |
512 | 198 | if (Name == "@feat.00") { |
513 | 186 | Feat00Flags = Sym.getValue(); |
514 | 186 | return nullptr; |
515 | 186 | } |
516 | 12 | |
517 | 12 | if (Sym.isExternal()) |
518 | 8 | return Symtab->addAbsolute(Name, Sym); |
519 | 4 | return make<DefinedAbsolute>(Name, Sym); |
520 | 4 | } |
521 | 4.37k | |
522 | 4.37k | int32_t SectionNumber = Sym.getSectionNumber(); |
523 | 4.37k | if (SectionNumber == llvm::COFF::IMAGE_SYM_DEBUG) |
524 | 73 | return nullptr; |
525 | 4.30k | |
526 | 4.30k | if (llvm::COFF::isReservedSectionNumber(SectionNumber)) |
527 | 0 | fatal(toString(this) + ": " + GetName() + |
528 | 0 | " should not refer to special section " + Twine(SectionNumber)); |
529 | 4.30k | |
530 | 4.30k | if ((uint32_t)SectionNumber >= SparseChunks.size()) |
531 | 0 | fatal(toString(this) + ": " + GetName() + |
532 | 0 | " should not refer to non-existent section " + Twine(SectionNumber)); |
533 | 4.30k | |
534 | 4.30k | // Comdat handling. |
535 | 4.30k | // A comdat symbol consists of two symbol table entries. |
536 | 4.30k | // The first symbol entry has the name of the section (e.g. .text), fixed |
537 | 4.30k | // values for the other fields, and one auxilliary record. |
538 | 4.30k | // The second symbol entry has the name of the comdat symbol, called the |
539 | 4.30k | // "comdat leader". |
540 | 4.30k | // When this function is called for the first symbol entry of a comdat, |
541 | 4.30k | // it sets ComdatDefs and returns None, and when it's called for the second |
542 | 4.30k | // symbol entry it reads ComdatDefs and then sets it back to nullptr. |
543 | 4.30k | |
544 | 4.30k | // Handle comdat leader. |
545 | 4.30k | if (const coff_aux_section_definition *Def = ComdatDefs[SectionNumber]) { |
546 | 362 | ComdatDefs[SectionNumber] = nullptr; |
547 | 362 | DefinedRegular *Leader; |
548 | 362 | |
549 | 362 | if (Sym.isExternal()) { |
550 | 340 | std::tie(Leader, Prevailing) = |
551 | 340 | Symtab->addComdat(this, GetName(), Sym.getGeneric()); |
552 | 340 | } else { |
553 | 22 | Leader = make<DefinedRegular>(this, /*Name*/ "", /*IsCOMDAT*/ false, |
554 | 22 | /*IsExternal*/ false, Sym.getGeneric()); |
555 | 22 | Prevailing = true; |
556 | 22 | } |
557 | 362 | |
558 | 362 | if (Def->Selection < (int)IMAGE_COMDAT_SELECT_NODUPLICATES || |
559 | 362 | // Intentionally ends at IMAGE_COMDAT_SELECT_LARGEST: link.exe |
560 | 362 | // doesn't understand IMAGE_COMDAT_SELECT_NEWEST either. |
561 | 362 | Def->Selection > (int)IMAGE_COMDAT_SELECT_LARGEST) { |
562 | 0 | fatal("unknown comdat type " + std::to_string((int)Def->Selection) + |
563 | 0 | " for " + GetName() + " in " + toString(this)); |
564 | 0 | } |
565 | 362 | COMDATType Selection = (COMDATType)Def->Selection; |
566 | 362 | |
567 | 362 | if (Leader->isCOMDAT()) |
568 | 340 | handleComdatSelection(Sym, Selection, Prevailing, Leader); |
569 | 362 | |
570 | 362 | if (Prevailing) { |
571 | 326 | SectionChunk *C = readSection(SectionNumber, Def, GetName()); |
572 | 326 | SparseChunks[SectionNumber] = C; |
573 | 326 | C->Sym = cast<DefinedRegular>(Leader); |
574 | 326 | C->Selection = Selection; |
575 | 326 | cast<DefinedRegular>(Leader)->Data = &C->Repl; |
576 | 326 | } else { |
577 | 36 | SparseChunks[SectionNumber] = nullptr; |
578 | 36 | } |
579 | 362 | return Leader; |
580 | 362 | } |
581 | 3.93k | |
582 | 3.93k | // Prepare to handle the comdat leader symbol by setting the section's |
583 | 3.93k | // ComdatDefs pointer if we encounter a non-associative comdat. |
584 | 3.93k | if (SparseChunks[SectionNumber] == PendingComdat) { |
585 | 518 | if (const coff_aux_section_definition *Def = Sym.getSectionDefinition()) { |
586 | 490 | if (Def->Selection != IMAGE_COMDAT_SELECT_ASSOCIATIVE) |
587 | 374 | ComdatDefs[SectionNumber] = Def; |
588 | 490 | } |
589 | 518 | return None; |
590 | 518 | } |
591 | 3.42k | |
592 | 3.42k | return createRegular(Sym); |
593 | 3.42k | } |
594 | | |
595 | 676 | MachineTypes ObjFile::getMachineType() { |
596 | 676 | if (COFFObj) |
597 | 676 | return static_cast<MachineTypes>(COFFObj->getMachine()); |
598 | 0 | return IMAGE_FILE_MACHINE_UNKNOWN; |
599 | 0 | } |
600 | | |
601 | 21 | StringRef ltrim1(StringRef S, const char *Chars) { |
602 | 21 | if (!S.empty() && strchr(Chars, S[0])) |
603 | 21 | return S.substr(1); |
604 | 0 | return S; |
605 | 0 | } |
606 | | |
607 | 102 | void ImportFile::parse() { |
608 | 102 | const char *Buf = MB.getBufferStart(); |
609 | 102 | const char *End = MB.getBufferEnd(); |
610 | 102 | const auto *Hdr = reinterpret_cast<const coff_import_header *>(Buf); |
611 | 102 | |
612 | 102 | // Check if the total size is valid. |
613 | 102 | if ((size_t)(End - Buf) != (sizeof(*Hdr) + Hdr->SizeOfData)) |
614 | 0 | fatal("broken import library"); |
615 | 102 | |
616 | 102 | // Read names and create an __imp_ symbol. |
617 | 102 | StringRef Name = Saver.save(StringRef(Buf + sizeof(*Hdr))); |
618 | 102 | StringRef ImpName = Saver.save("__imp_" + Name); |
619 | 102 | const char *NameStart = Buf + sizeof(coff_import_header) + Name.size() + 1; |
620 | 102 | DLLName = StringRef(NameStart); |
621 | 102 | StringRef ExtName; |
622 | 102 | switch (Hdr->getNameType()) { |
623 | 102 | case IMPORT_ORDINAL: |
624 | 13 | ExtName = ""; |
625 | 13 | break; |
626 | 102 | case IMPORT_NAME: |
627 | 68 | ExtName = Name; |
628 | 68 | break; |
629 | 102 | case IMPORT_NAME_NOPREFIX: |
630 | 2 | ExtName = ltrim1(Name, "?@_"); |
631 | 2 | break; |
632 | 102 | case IMPORT_NAME_UNDECORATE: |
633 | 19 | ExtName = ltrim1(Name, "?@_"); |
634 | 19 | ExtName = ExtName.substr(0, ExtName.find('@')); |
635 | 19 | break; |
636 | 102 | } |
637 | 102 | |
638 | 102 | this->Hdr = Hdr; |
639 | 102 | ExternalName = ExtName; |
640 | 102 | |
641 | 102 | ImpSym = Symtab->addImportData(ImpName, this); |
642 | 102 | // If this was a duplicate, we logged an error but may continue; |
643 | 102 | // in this case, ImpSym is nullptr. |
644 | 102 | if (!ImpSym) |
645 | 2 | return; |
646 | 100 | |
647 | 100 | if (Hdr->getType() == llvm::COFF::IMPORT_CONST) |
648 | 1 | static_cast<void>(Symtab->addImportData(Name, this)); |
649 | 100 | |
650 | 100 | // If type is function, we need to create a thunk which jump to an |
651 | 100 | // address pointed by the __imp_ symbol. (This allows you to call |
652 | 100 | // DLL functions just like regular non-DLL functions.) |
653 | 100 | if (Hdr->getType() == llvm::COFF::IMPORT_CODE) |
654 | 92 | ThunkSym = Symtab->addImportThunk( |
655 | 92 | Name, cast_or_null<DefinedImportData>(ImpSym), Hdr->Machine); |
656 | 100 | } |
657 | | |
658 | 57 | void BitcodeFile::parse() { |
659 | 57 | Obj = check(lto::InputFile::create(MemoryBufferRef( |
660 | 57 | MB.getBuffer(), Saver.save(ParentName + MB.getBufferIdentifier())))); |
661 | 57 | std::vector<std::pair<Symbol *, bool>> Comdat(Obj->getComdatTable().size()); |
662 | 80 | for (size_t I = 0; I != Obj->getComdatTable().size(); ++I23 ) |
663 | 23 | // FIXME: lto::InputFile doesn't keep enough data to do correct comdat |
664 | 23 | // selection handling. |
665 | 23 | Comdat[I] = Symtab->addComdat(this, Saver.save(Obj->getComdatTable()[I])); |
666 | 112 | for (const lto::InputFile::Symbol &ObjSym : Obj->symbols()) { |
667 | 112 | StringRef SymName = Saver.save(ObjSym.getName()); |
668 | 112 | int ComdatIndex = ObjSym.getComdatIndex(); |
669 | 112 | Symbol *Sym; |
670 | 112 | if (ObjSym.isUndefined()) { |
671 | 19 | Sym = Symtab->addUndefined(SymName, this, false); |
672 | 93 | } else if (ObjSym.isCommon()) { |
673 | 0 | Sym = Symtab->addCommon(this, SymName, ObjSym.getCommonSize()); |
674 | 93 | } else if (ObjSym.isWeak() && ObjSym.isIndirect()17 ) { |
675 | 2 | // Weak external. |
676 | 2 | Sym = Symtab->addUndefined(SymName, this, true); |
677 | 2 | std::string Fallback = ObjSym.getCOFFWeakExternalFallback(); |
678 | 2 | Symbol *Alias = Symtab->addUndefined(Saver.save(Fallback)); |
679 | 2 | checkAndSetWeakAlias(Symtab, this, Sym, Alias); |
680 | 91 | } else if (ComdatIndex != -1) { |
681 | 23 | if (SymName == Obj->getComdatTable()[ComdatIndex]) |
682 | 23 | Sym = Comdat[ComdatIndex].first; |
683 | 0 | else if (Comdat[ComdatIndex].second) |
684 | 0 | Sym = Symtab->addRegular(this, SymName); |
685 | 0 | else |
686 | 0 | Sym = Symtab->addUndefined(SymName, this, false); |
687 | 68 | } else { |
688 | 68 | Sym = Symtab->addRegular(this, SymName); |
689 | 68 | } |
690 | 112 | Symbols.push_back(Sym); |
691 | 112 | if (ObjSym.isUsed()) |
692 | 1 | Config->GCRoot.push_back(Sym); |
693 | 112 | } |
694 | 57 | Directives = Obj->getCOFFLinkerOpts(); |
695 | 57 | } |
696 | | |
697 | 57 | MachineTypes BitcodeFile::getMachineType() { |
698 | 57 | switch (Triple(Obj->getTargetTriple()).getArch()) { |
699 | 57 | case Triple::x86_64: |
700 | 55 | return AMD64; |
701 | 57 | case Triple::x86: |
702 | 2 | return I386; |
703 | 57 | case Triple::arm: |
704 | 0 | return ARMNT; |
705 | 57 | case Triple::aarch64: |
706 | 0 | return ARM64; |
707 | 57 | default: |
708 | 0 | return IMAGE_FILE_MACHINE_UNKNOWN; |
709 | 57 | } |
710 | 57 | } |
711 | | } // namespace coff |
712 | | } // namespace lld |
713 | | |
714 | | // Returns the last element of a path, which is supposed to be a filename. |
715 | 198 | static StringRef getBasename(StringRef Path) { |
716 | 198 | return sys::path::filename(Path, sys::path::Style::windows); |
717 | 198 | } |
718 | | |
719 | | // Returns a string in the format of "foo.obj" or "foo.obj(bar.lib)". |
720 | 1.23k | std::string lld::toString(const coff::InputFile *File) { |
721 | 1.23k | if (!File) |
722 | 0 | return "<internal>"; |
723 | 1.23k | if (File->ParentName.empty()) |
724 | 1.13k | return File->getName(); |
725 | 99 | |
726 | 99 | return (getBasename(File->ParentName) + "(" + getBasename(File->getName()) + |
727 | 99 | ")") |
728 | 99 | .str(); |
729 | 99 | } |