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 "Chunks.h"
11 #include "Error.h"
12 #include "InputFiles.h"
13 #include "Symbols.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/IR/LLVMContext.h"
16 #include "llvm/LTO/LTOModule.h"
17 #include "llvm/Object/COFF.h"
18 #include "llvm/Support/COFF.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/Endian.h"
21 #include "llvm/Support/raw_ostream.h"
22 
23 using namespace llvm::COFF;
24 using namespace llvm::object;
25 using namespace llvm::support::endian;
26 using llvm::RoundUpToAlignment;
27 using llvm::Triple;
28 using llvm::support::ulittle32_t;
29 using llvm::sys::fs::file_magic;
30 using llvm::sys::fs::identify_magic;
31 
32 namespace lld {
33 namespace coff {
34 
35 int InputFile::NextIndex = 0;
36 
37 // Returns the last element of a path, which is supposed to be a filename.
38 static StringRef getBasename(StringRef Path) {
39   size_t Pos = Path.find_last_of("\\/");
40   if (Pos == StringRef::npos)
41     return Path;
42   return Path.substr(Pos + 1);
43 }
44 
45 // Returns a string in the format of "foo.obj" or "foo.obj(bar.lib)".
46 std::string InputFile::getShortName() {
47   if (ParentName == "")
48     return getName().lower();
49   std::string Res = (getBasename(ParentName) + "(" +
50                      getBasename(getName()) + ")").str();
51   return StringRef(Res).lower();
52 }
53 
54 void ArchiveFile::parse() {
55   // Parse a MemoryBufferRef as an archive file.
56   auto ArchiveOrErr = Archive::create(MB);
57   error(ArchiveOrErr, "Failed to parse static library");
58   File = std::move(*ArchiveOrErr);
59 
60   // Allocate a buffer for Lazy objects.
61   size_t NumSyms = File->getNumberOfSymbols();
62   LazySymbols.reserve(NumSyms);
63 
64   // Read the symbol table to construct Lazy objects.
65   for (const Archive::Symbol &Sym : File->symbols())
66     LazySymbols.emplace_back(this, Sym);
67 
68   // Seen is a map from member files to boolean values. Initially
69   // all members are mapped to false, which indicates all these files
70   // are not read yet.
71   for (auto &ChildOrErr : File->children()) {
72     error(ChildOrErr, "Failed to parse static library");
73     const Archive::Child &Child = *ChildOrErr;
74     Seen[Child.getChildOffset()].clear();
75   }
76 }
77 
78 // Returns a buffer pointing to a member file containing a given symbol.
79 // This function is thread-safe.
80 MemoryBufferRef ArchiveFile::getMember(const Archive::Symbol *Sym) {
81   auto COrErr = Sym->getMember();
82   error(COrErr, Twine("Could not get the member for symbol ") + Sym->getName());
83   const Archive::Child &C = *COrErr;
84 
85   // Return an empty buffer if we have already returned the same buffer.
86   if (Seen[C.getChildOffset()].test_and_set())
87     return MemoryBufferRef();
88   ErrorOr<MemoryBufferRef> Ret = C.getMemoryBufferRef();
89   error(Ret, Twine("Could not get the buffer for the member defining symbol ") +
90                  Sym->getName());
91   return *Ret;
92 }
93 
94 void ObjectFile::parse() {
95   // Parse a memory buffer as a COFF file.
96   auto BinOrErr = createBinary(MB);
97   error(BinOrErr, "Failed to parse object file");
98   std::unique_ptr<Binary> Bin = std::move(*BinOrErr);
99 
100   if (auto *Obj = dyn_cast<COFFObjectFile>(Bin.get())) {
101     Bin.release();
102     COFFObj.reset(Obj);
103   } else {
104     error(Twine(getName()) + " is not a COFF file.");
105   }
106 
107   // Read section and symbol tables.
108   initializeChunks();
109   initializeSymbols();
110   initializeSEH();
111 }
112 
113 void ObjectFile::initializeChunks() {
114   uint32_t NumSections = COFFObj->getNumberOfSections();
115   Chunks.reserve(NumSections);
116   SparseChunks.resize(NumSections + 1);
117   for (uint32_t I = 1; I < NumSections + 1; ++I) {
118     const coff_section *Sec;
119     StringRef Name;
120     std::error_code EC = COFFObj->getSection(I, Sec);
121     error(EC, Twine("getSection failed: #") + Twine(I));
122     EC = COFFObj->getSectionName(Sec, Name);
123     error(EC, Twine("getSectionName failed: #") + Twine(I));
124     if (Name == ".sxdata") {
125       SXData = Sec;
126       continue;
127     }
128     if (Name == ".drectve") {
129       ArrayRef<uint8_t> Data;
130       COFFObj->getSectionContents(Sec, Data);
131       Directives = std::string((const char *)Data.data(), Data.size());
132       continue;
133     }
134     // Skip non-DWARF debug info. MSVC linker converts the sections into
135     // a PDB file, but we don't support that.
136     if (Name == ".debug" || Name.startswith(".debug$"))
137       continue;
138     // We want to preserve DWARF debug sections only when /debug is on.
139     if (!Config->Debug && Name.startswith(".debug"))
140       continue;
141     if (Sec->Characteristics & llvm::COFF::IMAGE_SCN_LNK_REMOVE)
142       continue;
143     auto *C = new (Alloc) SectionChunk(this, Sec);
144     Chunks.push_back(C);
145     SparseChunks[I] = C;
146   }
147 }
148 
149 void ObjectFile::initializeSymbols() {
150   uint32_t NumSymbols = COFFObj->getNumberOfSymbols();
151   SymbolBodies.reserve(NumSymbols);
152   SparseSymbolBodies.resize(NumSymbols);
153   llvm::SmallVector<Undefined *, 8> WeakAliases;
154   int32_t LastSectionNumber = 0;
155   for (uint32_t I = 0; I < NumSymbols; ++I) {
156     // Get a COFFSymbolRef object.
157     auto SymOrErr = COFFObj->getSymbol(I);
158     error(SymOrErr, Twine("broken object file: ") + getName());
159 
160     COFFSymbolRef Sym = *SymOrErr;
161 
162     const void *AuxP = nullptr;
163     if (Sym.getNumberOfAuxSymbols())
164       AuxP = COFFObj->getSymbol(I + 1)->getRawPtr();
165     bool IsFirst = (LastSectionNumber != Sym.getSectionNumber());
166 
167     SymbolBody *Body = nullptr;
168     if (Sym.isUndefined()) {
169       Body = createUndefined(Sym);
170     } else if (Sym.isWeakExternal()) {
171       Body = createWeakExternal(Sym, AuxP);
172       WeakAliases.push_back((Undefined *)Body);
173     } else {
174       Body = createDefined(Sym, AuxP, IsFirst);
175     }
176     if (Body) {
177       SymbolBodies.push_back(Body);
178       SparseSymbolBodies[I] = Body;
179     }
180     I += Sym.getNumberOfAuxSymbols();
181     LastSectionNumber = Sym.getSectionNumber();
182   }
183   for (Undefined *U : WeakAliases)
184     U->WeakAlias = SparseSymbolBodies[(uintptr_t)U->WeakAlias];
185 }
186 
187 Undefined *ObjectFile::createUndefined(COFFSymbolRef Sym) {
188   StringRef Name;
189   COFFObj->getSymbolName(Sym, Name);
190   return new (Alloc) Undefined(Name);
191 }
192 
193 Undefined *ObjectFile::createWeakExternal(COFFSymbolRef Sym, const void *AuxP) {
194   StringRef Name;
195   COFFObj->getSymbolName(Sym, Name);
196   auto *U = new (Alloc) Undefined(Name);
197   auto *Aux = (const coff_aux_weak_external *)AuxP;
198   U->WeakAlias = (Undefined *)(uintptr_t)Aux->TagIndex;
199   return U;
200 }
201 
202 Defined *ObjectFile::createDefined(COFFSymbolRef Sym, const void *AuxP,
203                                    bool IsFirst) {
204   StringRef Name;
205   if (Sym.isCommon()) {
206     auto *C = new (Alloc) CommonChunk(Sym);
207     Chunks.push_back(C);
208     return new (Alloc) DefinedCommon(this, Sym, C);
209   }
210   if (Sym.isAbsolute()) {
211     COFFObj->getSymbolName(Sym, Name);
212     // Skip special symbols.
213     if (Name == "@comp.id")
214       return nullptr;
215     // COFF spec 5.10.1. The .sxdata section.
216     if (Name == "@feat.00") {
217       if (Sym.getValue() & 1)
218         SEHCompat = true;
219       return nullptr;
220     }
221     return new (Alloc) DefinedAbsolute(Name, Sym);
222   }
223   if (Sym.getSectionNumber() == llvm::COFF::IMAGE_SYM_DEBUG)
224     return nullptr;
225 
226   // Nothing else to do without a section chunk.
227   auto *SC = cast_or_null<SectionChunk>(SparseChunks[Sym.getSectionNumber()]);
228   if (!SC)
229     return nullptr;
230 
231   // Handle section definitions
232   if (IsFirst && AuxP) {
233     auto *Aux = reinterpret_cast<const coff_aux_section_definition *>(AuxP);
234     if (Aux->Selection == IMAGE_COMDAT_SELECT_ASSOCIATIVE)
235       if (auto *ParentSC = cast_or_null<SectionChunk>(
236               SparseChunks[Aux->getNumber(Sym.isBigObj())]))
237         ParentSC->addAssociative(SC);
238     SC->Checksum = Aux->CheckSum;
239   }
240 
241   auto *B = new (Alloc) DefinedRegular(this, Sym, SC);
242   if (SC->isCOMDAT() && Sym.getValue() == 0 && !AuxP)
243     SC->setSymbol(B);
244 
245   return B;
246 }
247 
248 void ObjectFile::initializeSEH() {
249   if (!SEHCompat || !SXData)
250     return;
251   ArrayRef<uint8_t> A;
252   COFFObj->getSectionContents(SXData, A);
253   if (A.size() % 4 != 0)
254     error(".sxdata must be an array of symbol table indices");
255   auto *I = reinterpret_cast<const ulittle32_t *>(A.data());
256   auto *E = reinterpret_cast<const ulittle32_t *>(A.data() + A.size());
257   for (; I != E; ++I)
258     SEHandlers.insert(SparseSymbolBodies[*I]);
259 }
260 
261 MachineTypes ObjectFile::getMachineType() {
262   if (COFFObj)
263     return static_cast<MachineTypes>(COFFObj->getMachine());
264   return IMAGE_FILE_MACHINE_UNKNOWN;
265 }
266 
267 StringRef ltrim1(StringRef S, const char *Chars) {
268   if (!S.empty() && strchr(Chars, S[0]))
269     return S.substr(1);
270   return S;
271 }
272 
273 void ImportFile::parse() {
274   const char *Buf = MB.getBufferStart();
275   const char *End = MB.getBufferEnd();
276   const auto *Hdr = reinterpret_cast<const coff_import_header *>(Buf);
277 
278   // Check if the total size is valid.
279   if ((size_t)(End - Buf) != (sizeof(*Hdr) + Hdr->SizeOfData))
280     error("broken import library");
281 
282   // Read names and create an __imp_ symbol.
283   StringRef Name = StringAlloc.save(StringRef(Buf + sizeof(*Hdr)));
284   StringRef ImpName = StringAlloc.save(Twine("__imp_") + Name);
285   const char *NameStart = Buf + sizeof(coff_import_header) + Name.size() + 1;
286   DLLName = StringRef(NameStart);
287   StringRef ExtName;
288   switch (Hdr->getNameType()) {
289   case IMPORT_ORDINAL:
290     ExtName = "";
291     break;
292   case IMPORT_NAME:
293     ExtName = Name;
294     break;
295   case IMPORT_NAME_NOPREFIX:
296     ExtName = ltrim1(Name, "?@_");
297     break;
298   case IMPORT_NAME_UNDECORATE:
299     ExtName = ltrim1(Name, "?@_");
300     ExtName = ExtName.substr(0, ExtName.find('@'));
301     break;
302   }
303   ImpSym = new (Alloc) DefinedImportData(DLLName, ImpName, ExtName, Hdr);
304   SymbolBodies.push_back(ImpSym);
305 
306   // If type is function, we need to create a thunk which jump to an
307   // address pointed by the __imp_ symbol. (This allows you to call
308   // DLL functions just like regular non-DLL functions.)
309   if (Hdr->getType() != llvm::COFF::IMPORT_CODE)
310     return;
311   ThunkSym = new (Alloc) DefinedImportThunk(Name, ImpSym, Hdr->Machine);
312   SymbolBodies.push_back(ThunkSym);
313 }
314 
315 void BitcodeFile::parse() {
316   // Usually parse() is thread-safe, but bitcode file is an exception.
317   std::lock_guard<std::mutex> Lock(Mu);
318 
319   ErrorOr<std::unique_ptr<LTOModule>> ModOrErr =
320       LTOModule::createFromBuffer(llvm::getGlobalContext(), MB.getBufferStart(),
321                                   MB.getBufferSize(), llvm::TargetOptions());
322   error(ModOrErr, "Could not create lto module");
323   M = std::move(*ModOrErr);
324 
325   llvm::StringSaver Saver(Alloc);
326   for (unsigned I = 0, E = M->getSymbolCount(); I != E; ++I) {
327     lto_symbol_attributes Attrs = M->getSymbolAttributes(I);
328     if ((Attrs & LTO_SYMBOL_SCOPE_MASK) == LTO_SYMBOL_SCOPE_INTERNAL)
329       continue;
330 
331     StringRef SymName = Saver.save(M->getSymbolName(I));
332     int SymbolDef = Attrs & LTO_SYMBOL_DEFINITION_MASK;
333     if (SymbolDef == LTO_SYMBOL_DEFINITION_UNDEFINED) {
334       SymbolBodies.push_back(new (Alloc) Undefined(SymName));
335     } else {
336       bool Replaceable =
337           (SymbolDef == LTO_SYMBOL_DEFINITION_TENTATIVE || // common
338            (Attrs & LTO_SYMBOL_COMDAT) ||                  // comdat
339            (SymbolDef == LTO_SYMBOL_DEFINITION_WEAK &&     // weak external
340             (Attrs & LTO_SYMBOL_ALIAS)));
341       SymbolBodies.push_back(new (Alloc) DefinedBitcode(this, SymName,
342                                                         Replaceable));
343     }
344   }
345 
346   Directives = M->getLinkerOpts();
347 }
348 
349 MachineTypes BitcodeFile::getMachineType() {
350   if (!M)
351     return IMAGE_FILE_MACHINE_UNKNOWN;
352   switch (Triple(M->getTargetTriple()).getArch()) {
353   case Triple::x86_64:
354     return AMD64;
355   case Triple::x86:
356     return I386;
357   case Triple::arm:
358     return ARMNT;
359   default:
360     return IMAGE_FILE_MACHINE_UNKNOWN;
361   }
362 }
363 
364 std::mutex BitcodeFile::Mu;
365 
366 } // namespace coff
367 } // namespace lld
368