1 //===- InputFiles.cpp -----------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "InputFiles.h"
16 #include "SymbolTable.h"
18 #include "llvm-c/lto.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/Triple.h"
21 #include "llvm/ADT/Twine.h"
22 #include "llvm/Object/Binary.h"
23 #include "llvm/Object/COFF.h"
24 #include "llvm/Support/COFF.h"
25 #include "llvm/Support/Casting.h"
26 #include "llvm/Support/Endian.h"
27 #include "llvm/Support/Error.h"
28 #include "llvm/Support/ErrorOr.h"
29 #include "llvm/Support/FileSystem.h"
30 #include "llvm/Target/TargetOptions.h"
32 #include <system_error>
36 using namespace llvm::COFF;
37 using namespace llvm::object;
38 using namespace llvm::support::endian;
41 using llvm::support::ulittle32_t;
46 /// Checks that Source is compatible with being a weak alias to Target.
47 /// If Source is Undefined and has no weak alias set, makes it a weak
49 static void checkAndSetWeakAlias(SymbolTable *Symtab, InputFile *F,
50 SymbolBody *Source, SymbolBody *Target) {
51 auto *U = dyn_cast<Undefined>(Source);
54 else if (!U->WeakAlias)
55 U->WeakAlias = Target;
56 else if (U->WeakAlias != Target)
57 Symtab->reportDuplicate(Source->symbol(), F);
60 ArchiveFile::ArchiveFile(MemoryBufferRef M) : InputFile(ArchiveKind, M) {}
62 void ArchiveFile::parse() {
63 // Parse a MemoryBufferRef as an archive file.
64 File = check(Archive::create(MB), toString(this));
66 // Read the symbol table to construct Lazy objects.
67 for (const Archive::Symbol &Sym : File->symbols())
68 Symtab->addLazy(this, Sym);
71 // Returns a buffer pointing to a member file containing a given symbol.
72 void ArchiveFile::addMember(const Archive::Symbol *Sym) {
73 const Archive::Child &C =
74 check(Sym->getMember(),
75 "could not get the member for symbol " + Sym->getName());
77 // Return an empty buffer if we have already returned the same buffer.
78 if (!Seen.insert(C.getChildOffset()).second)
81 Driver->enqueueArchiveMember(C, Sym->getName(), getName());
84 void ObjectFile::parse() {
85 // Parse a memory buffer as a COFF file.
86 std::unique_ptr<Binary> Bin = check(createBinary(MB), toString(this));
88 if (auto *Obj = dyn_cast<COFFObjectFile>(Bin.get())) {
92 fatal(toString(this) + " is not a COFF file");
95 // Read section and symbol tables.
101 void ObjectFile::initializeChunks() {
102 uint32_t NumSections = COFFObj->getNumberOfSections();
103 Chunks.reserve(NumSections);
104 SparseChunks.resize(NumSections + 1);
105 for (uint32_t I = 1; I < NumSections + 1; ++I) {
106 const coff_section *Sec;
108 if (auto EC = COFFObj->getSection(I, Sec))
109 fatal(EC, "getSection failed: #" + Twine(I));
110 if (auto EC = COFFObj->getSectionName(Sec, Name))
111 fatal(EC, "getSectionName failed: #" + Twine(I));
112 if (Name == ".sxdata") {
116 if (Name == ".drectve") {
117 ArrayRef<uint8_t> Data;
118 COFFObj->getSectionContents(Sec, Data);
119 Directives = std::string((const char *)Data.data(), Data.size());
123 // Object files may have DWARF debug info or MS CodeView debug info
126 // DWARF sections don't need any special handling from the perspective
127 // of the linker; they are just a data section containing relocations.
128 // We can just link them to complete debug info.
130 // CodeView needs a linker support. We need to interpret and debug
131 // info, and then write it to a separate .pdb file.
133 // Ignore debug info unless /debug is given.
134 if (!Config->Debug && Name.startswith(".debug"))
137 // CodeView sections are stored to a different vector because they are
138 // not linked in the regular manner.
139 if (Name == ".debug" || Name.startswith(".debug$")) {
140 DebugChunks.push_back(make<SectionChunk>(this, Sec));
144 if (Sec->Characteristics & llvm::COFF::IMAGE_SCN_LNK_REMOVE)
146 auto *C = make<SectionChunk>(this, Sec);
152 void ObjectFile::initializeSymbols() {
153 uint32_t NumSymbols = COFFObj->getNumberOfSymbols();
154 SymbolBodies.reserve(NumSymbols);
155 SparseSymbolBodies.resize(NumSymbols);
156 SmallVector<std::pair<SymbolBody *, uint32_t>, 8> WeakAliases;
157 int32_t LastSectionNumber = 0;
158 for (uint32_t I = 0; I < NumSymbols; ++I) {
159 // Get a COFFSymbolRef object.
160 ErrorOr<COFFSymbolRef> SymOrErr = COFFObj->getSymbol(I);
162 fatal(SymOrErr.getError(), "broken object file: " + toString(this));
163 COFFSymbolRef Sym = *SymOrErr;
165 const void *AuxP = nullptr;
166 if (Sym.getNumberOfAuxSymbols())
167 AuxP = COFFObj->getSymbol(I + 1)->getRawPtr();
168 bool IsFirst = (LastSectionNumber != Sym.getSectionNumber());
170 SymbolBody *Body = nullptr;
171 if (Sym.isUndefined()) {
172 Body = createUndefined(Sym);
173 } else if (Sym.isWeakExternal()) {
174 Body = createUndefined(Sym);
176 static_cast<const coff_aux_weak_external *>(AuxP)->TagIndex;
177 WeakAliases.emplace_back(Body, TagIndex);
179 Body = createDefined(Sym, AuxP, IsFirst);
182 SymbolBodies.push_back(Body);
183 SparseSymbolBodies[I] = Body;
185 I += Sym.getNumberOfAuxSymbols();
186 LastSectionNumber = Sym.getSectionNumber();
188 for (auto WeakAlias : WeakAliases)
189 checkAndSetWeakAlias(Symtab, this, WeakAlias.first,
190 SparseSymbolBodies[WeakAlias.second]);
193 SymbolBody *ObjectFile::createUndefined(COFFSymbolRef Sym) {
195 COFFObj->getSymbolName(Sym, Name);
196 return Symtab->addUndefined(Name, this, Sym.isWeakExternal())->body();
199 SymbolBody *ObjectFile::createDefined(COFFSymbolRef Sym, const void *AuxP,
202 if (Sym.isCommon()) {
203 auto *C = make<CommonChunk>(Sym);
205 COFFObj->getSymbolName(Sym, Name);
207 Symtab->addCommon(this, Name, Sym.getValue(), Sym.getGeneric(), C);
210 if (Sym.isAbsolute()) {
211 COFFObj->getSymbolName(Sym, Name);
212 // Skip special symbols.
213 if (Name == "@comp.id")
215 // COFF spec 5.10.1. The .sxdata section.
216 if (Name == "@feat.00") {
217 if (Sym.getValue() & 1)
221 if (Sym.isExternal())
222 return Symtab->addAbsolute(Name, Sym)->body();
224 return make<DefinedAbsolute>(Name, Sym);
226 int32_t SectionNumber = Sym.getSectionNumber();
227 if (SectionNumber == llvm::COFF::IMAGE_SYM_DEBUG)
230 // Reserved sections numbers don't have contents.
231 if (llvm::COFF::isReservedSectionNumber(SectionNumber))
232 fatal("broken object file: " + toString(this));
234 // This symbol references a section which is not present in the section
236 if ((uint32_t)SectionNumber >= SparseChunks.size())
237 fatal("broken object file: " + toString(this));
239 // Nothing else to do without a section chunk.
240 auto *SC = cast_or_null<SectionChunk>(SparseChunks[SectionNumber]);
244 // Handle section definitions
245 if (IsFirst && AuxP) {
246 auto *Aux = reinterpret_cast<const coff_aux_section_definition *>(AuxP);
247 if (Aux->Selection == IMAGE_COMDAT_SELECT_ASSOCIATIVE)
248 if (auto *ParentSC = cast_or_null<SectionChunk>(
249 SparseChunks[Aux->getNumber(Sym.isBigObj())]))
250 ParentSC->addAssociative(SC);
251 SC->Checksum = Aux->CheckSum;
255 if (Sym.isExternal()) {
256 COFFObj->getSymbolName(Sym, Name);
258 Symtab->addRegular(this, Name, SC->isCOMDAT(), Sym.getGeneric(), SC);
259 B = cast<DefinedRegular>(S->body());
261 B = make<DefinedRegular>(this, /*Name*/ "", SC->isCOMDAT(),
262 /*IsExternal*/ false, Sym.getGeneric(), SC);
263 if (SC->isCOMDAT() && Sym.getValue() == 0 && !AuxP)
269 void ObjectFile::initializeSEH() {
270 if (!SEHCompat || !SXData)
273 COFFObj->getSectionContents(SXData, A);
274 if (A.size() % 4 != 0)
275 fatal(".sxdata must be an array of symbol table indices");
276 auto *I = reinterpret_cast<const ulittle32_t *>(A.data());
277 auto *E = reinterpret_cast<const ulittle32_t *>(A.data() + A.size());
279 SEHandlers.insert(SparseSymbolBodies[*I]);
282 MachineTypes ObjectFile::getMachineType() {
284 return static_cast<MachineTypes>(COFFObj->getMachine());
285 return IMAGE_FILE_MACHINE_UNKNOWN;
288 StringRef ltrim1(StringRef S, const char *Chars) {
289 if (!S.empty() && strchr(Chars, S[0]))
294 void ImportFile::parse() {
295 const char *Buf = MB.getBufferStart();
296 const char *End = MB.getBufferEnd();
297 const auto *Hdr = reinterpret_cast<const coff_import_header *>(Buf);
299 // Check if the total size is valid.
300 if ((size_t)(End - Buf) != (sizeof(*Hdr) + Hdr->SizeOfData))
301 fatal("broken import library");
303 // Read names and create an __imp_ symbol.
304 StringRef Name = Saver.save(StringRef(Buf + sizeof(*Hdr)));
305 StringRef ImpName = Saver.save("__imp_" + Name);
306 const char *NameStart = Buf + sizeof(coff_import_header) + Name.size() + 1;
307 DLLName = StringRef(NameStart);
309 switch (Hdr->getNameType()) {
316 case IMPORT_NAME_NOPREFIX:
317 ExtName = ltrim1(Name, "?@_");
319 case IMPORT_NAME_UNDECORATE:
320 ExtName = ltrim1(Name, "?@_");
321 ExtName = ExtName.substr(0, ExtName.find('@'));
326 ExternalName = ExtName;
328 ImpSym = cast<DefinedImportData>(
329 Symtab->addImportData(ImpName, this)->body());
330 if (Hdr->getType() == llvm::COFF::IMPORT_CONST)
332 cast<DefinedImportData>(Symtab->addImportData(Name, this)->body());
334 // If type is function, we need to create a thunk which jump to an
335 // address pointed by the __imp_ symbol. (This allows you to call
336 // DLL functions just like regular non-DLL functions.)
337 if (Hdr->getType() != llvm::COFF::IMPORT_CODE)
339 ThunkSym = cast<DefinedImportThunk>(
340 Symtab->addImportThunk(Name, ImpSym, Hdr->Machine)->body());
343 void BitcodeFile::parse() {
344 Obj = check(lto::InputFile::create(MemoryBufferRef(
345 MB.getBuffer(), Saver.save(ParentName + MB.getBufferIdentifier()))));
346 for (const lto::InputFile::Symbol &ObjSym : Obj->symbols()) {
347 StringRef SymName = Saver.save(ObjSym.getName());
349 if (ObjSym.isUndefined()) {
350 Sym = Symtab->addUndefined(SymName, this, false);
351 } else if (ObjSym.isCommon()) {
352 Sym = Symtab->addCommon(this, SymName, ObjSym.getCommonSize());
353 } else if (ObjSym.isWeak() && ObjSym.isIndirect()) {
355 Sym = Symtab->addUndefined(SymName, this, true);
356 std::string Fallback = ObjSym.getCOFFWeakExternalFallback();
357 SymbolBody *Alias = Symtab->addUndefined(Saver.save(Fallback));
358 checkAndSetWeakAlias(Symtab, this, Sym->body(), Alias);
360 bool IsCOMDAT = ObjSym.getComdatIndex() != -1;
361 Sym = Symtab->addRegular(this, SymName, IsCOMDAT);
363 SymbolBodies.push_back(Sym->body());
365 Directives = Obj->getCOFFLinkerOpts();
368 MachineTypes BitcodeFile::getMachineType() {
369 switch (Triple(Obj->getTargetTriple()).getArch()) {
377 return IMAGE_FILE_MACHINE_UNKNOWN;
383 // Returns the last element of a path, which is supposed to be a filename.
384 static StringRef getBasename(StringRef Path) {
385 size_t Pos = Path.find_last_of("\\/");
386 if (Pos == StringRef::npos)
388 return Path.substr(Pos + 1);
391 // Returns a string in the format of "foo.obj" or "foo.obj(bar.lib)".
392 std::string lld::toString(coff::InputFile *File) {
395 if (File->ParentName.empty())
396 return File->getName().lower();
399 (getBasename(File->ParentName) + "(" + getBasename(File->getName()) + ")")
401 return StringRef(Res).lower();