blob: 78bf346cc0e54a517681e9c8473390340a6d76f5 [file] [log] [blame]
Rui Ueyama411c63602015-05-28 19:09:30 +00001//===- 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
Rui Ueyama9381eb12016-12-18 14:06:06 +000010#include "InputFiles.h"
Rui Ueyama411c63602015-05-28 19:09:30 +000011#include "Chunks.h"
Eugene Zelenko1a299ea2016-04-21 17:14:10 +000012#include "Config.h"
Peter Collingbournefeee2102016-07-26 02:00:42 +000013#include "Driver.h"
Rui Ueyama8fd9fb92015-06-01 02:58:15 +000014#include "Error.h"
Rui Ueyama9381eb12016-12-18 14:06:06 +000015#include "Memory.h"
Peter Collingbourne79a5e6b2016-12-09 21:55:24 +000016#include "SymbolTable.h"
Rui Ueyama67fcd1a02015-08-05 19:51:28 +000017#include "Symbols.h"
Rui Ueyama9381eb12016-12-18 14:06:06 +000018#include "llvm-c/lto.h"
Eugene Zelenko1a299ea2016-04-21 17:14:10 +000019#include "llvm/ADT/SmallVector.h"
20#include "llvm/ADT/Triple.h"
21#include "llvm/ADT/Twine.h"
Bob Haarmancde5e5b2017-02-02 23:58:14 +000022#include "llvm/Bitcode/BitcodeReader.h"
Eugene Zelenko1a299ea2016-04-21 17:14:10 +000023#include "llvm/Object/Binary.h"
Rui Ueyama411c63602015-05-28 19:09:30 +000024#include "llvm/Object/COFF.h"
25#include "llvm/Support/COFF.h"
Eugene Zelenko1a299ea2016-04-21 17:14:10 +000026#include "llvm/Support/Casting.h"
Rui Ueyama411c63602015-05-28 19:09:30 +000027#include "llvm/Support/Endian.h"
Eugene Zelenko1a299ea2016-04-21 17:14:10 +000028#include "llvm/Support/Error.h"
29#include "llvm/Support/ErrorOr.h"
30#include "llvm/Support/FileSystem.h"
31#include "llvm/Target/TargetOptions.h"
Eugene Zelenko1a299ea2016-04-21 17:14:10 +000032#include <cstring>
33#include <system_error>
34#include <utility>
Rui Ueyama411c63602015-05-28 19:09:30 +000035
Rui Ueyama1d99ab32016-09-15 22:24:51 +000036using namespace llvm;
Rui Ueyama1c79ce92015-07-08 20:22:50 +000037using namespace llvm::COFF;
Rui Ueyama411c63602015-05-28 19:09:30 +000038using namespace llvm::object;
39using namespace llvm::support::endian;
Eugene Zelenko1a299ea2016-04-21 17:14:10 +000040
Rui Ueyamaea533cd2015-07-09 19:54:13 +000041using llvm::Triple;
Rui Ueyamacd3f99b2015-07-24 23:51:14 +000042using llvm::support::ulittle32_t;
Rui Ueyama411c63602015-05-28 19:09:30 +000043
44namespace lld {
45namespace coff {
46
Bob Haarmancde5e5b2017-02-02 23:58:14 +000047/// Checks that Source is compatible with being a weak alias to Target.
48/// If Source is Undefined and has no weak alias set, makes it a weak
49/// alias to Target.
50static void checkAndSetWeakAlias(SymbolTable *Symtab, InputFile *F,
51 SymbolBody *Source, SymbolBody *Target) {
52 auto *U = dyn_cast<Undefined>(Source);
53 if (!U)
54 return;
55 else if (!U->WeakAlias)
56 U->WeakAlias = Target;
57 else if (U->WeakAlias != Target)
58 Symtab->reportDuplicate(Source->symbol(), F);
59}
Rui Ueyama411c63602015-05-28 19:09:30 +000060
Rui Ueyama1d99ab32016-09-15 22:24:51 +000061ArchiveFile::ArchiveFile(MemoryBufferRef M) : InputFile(ArchiveKind, M) {}
62
Rafael Espindolab835ae82015-08-06 14:58:50 +000063void ArchiveFile::parse() {
Rui Ueyamad7c2f582015-05-31 21:04:56 +000064 // Parse a MemoryBufferRef as an archive file.
Rui Ueyamaa45d45e2016-12-07 23:17:02 +000065 File = check(Archive::create(MB), toString(this));
Rui Ueyama411c63602015-05-28 19:09:30 +000066
Peter Collingbourne79a5e6b2016-12-09 21:55:24 +000067 // Read the symbol table to construct Lazy objects.
68 for (const Archive::Symbol &Sym : File->symbols())
69 Symtab->addLazy(this, Sym);
Rui Ueyama411c63602015-05-28 19:09:30 +000070}
71
72// Returns a buffer pointing to a member file containing a given symbol.
Peter Collingbourne6ee0b4e2016-12-15 04:02:23 +000073void ArchiveFile::addMember(const Archive::Symbol *Sym) {
Rui Ueyama659a4f22016-07-15 01:06:38 +000074 const Archive::Child &C =
75 check(Sym->getMember(),
Rui Ueyamabb579542016-07-15 01:12:24 +000076 "could not get the member for symbol " + Sym->getName());
Rui Ueyama411c63602015-05-28 19:09:30 +000077
78 // Return an empty buffer if we have already returned the same buffer.
Peter Collingbourne8155dfa2016-12-12 03:16:14 +000079 if (!Seen.insert(C.getChildOffset()).second)
Peter Collingbourne6ee0b4e2016-12-15 04:02:23 +000080 return;
Peter Collingbournefeee2102016-07-26 02:00:42 +000081
Peter Collingbourne6ee0b4e2016-12-15 04:02:23 +000082 Driver->enqueueArchiveMember(C, Sym->getName(), getName());
Peter Collingbourne79a5e6b2016-12-09 21:55:24 +000083}
Rui Ueyama1d99ab32016-09-15 22:24:51 +000084
Rafael Espindolab835ae82015-08-06 14:58:50 +000085void ObjectFile::parse() {
Rui Ueyama411c63602015-05-28 19:09:30 +000086 // Parse a memory buffer as a COFF file.
Rui Ueyamaaa4f4452016-12-08 18:49:04 +000087 std::unique_ptr<Binary> Bin = check(createBinary(MB), toString(this));
88
89 if (auto *Obj = dyn_cast<COFFObjectFile>(Bin.get())) {
90 Bin.release();
91 COFFObj.reset(Obj);
92 } else {
93 fatal(toString(this) + " is not a COFF file");
94 }
Rui Ueyama411c63602015-05-28 19:09:30 +000095
96 // Read section and symbol tables.
Rafael Espindolab835ae82015-08-06 14:58:50 +000097 initializeChunks();
98 initializeSymbols();
99 initializeSEH();
Rui Ueyama411c63602015-05-28 19:09:30 +0000100}
101
Rafael Espindolab835ae82015-08-06 14:58:50 +0000102void ObjectFile::initializeChunks() {
Rui Ueyama411c63602015-05-28 19:09:30 +0000103 uint32_t NumSections = COFFObj->getNumberOfSections();
104 Chunks.reserve(NumSections);
105 SparseChunks.resize(NumSections + 1);
106 for (uint32_t I = 1; I < NumSections + 1; ++I) {
107 const coff_section *Sec;
108 StringRef Name;
Rui Ueyama0d09a862016-07-15 00:40:46 +0000109 if (auto EC = COFFObj->getSection(I, Sec))
110 fatal(EC, "getSection failed: #" + Twine(I));
111 if (auto EC = COFFObj->getSectionName(Sec, Name))
112 fatal(EC, "getSectionName failed: #" + Twine(I));
Rui Ueyamacd3f99b2015-07-24 23:51:14 +0000113 if (Name == ".sxdata") {
114 SXData = Sec;
115 continue;
116 }
Rui Ueyama411c63602015-05-28 19:09:30 +0000117 if (Name == ".drectve") {
118 ArrayRef<uint8_t> Data;
119 COFFObj->getSectionContents(Sec, Data);
Rui Ueyama0569ecf2015-07-04 03:27:46 +0000120 Directives = std::string((const char *)Data.data(), Data.size());
Rui Ueyama411c63602015-05-28 19:09:30 +0000121 continue;
122 }
Rui Ueyamabe939b32016-11-21 17:22:35 +0000123
124 // Object files may have DWARF debug info or MS CodeView debug info
125 // (or both).
126 //
127 // DWARF sections don't need any special handling from the perspective
128 // of the linker; they are just a data section containing relocations.
129 // We can just link them to complete debug info.
130 //
131 // CodeView needs a linker support. We need to interpret and debug
132 // info, and then write it to a separate .pdb file.
133
134 // Ignore debug info unless /debug is given.
David Majnemer2c345a32015-07-08 16:37:50 +0000135 if (!Config->Debug && Name.startswith(".debug"))
Rui Ueyama411c63602015-05-28 19:09:30 +0000136 continue;
Rui Ueyamabe939b32016-11-21 17:22:35 +0000137
138 // CodeView sections are stored to a different vector because they are
139 // not linked in the regular manner.
140 if (Name == ".debug" || Name.startswith(".debug$")) {
141 DebugChunks.push_back(new (Alloc) SectionChunk(this, Sec));
142 continue;
143 }
144
Rui Ueyama411c63602015-05-28 19:09:30 +0000145 if (Sec->Characteristics & llvm::COFF::IMAGE_SCN_LNK_REMOVE)
146 continue;
Peter Collingbournebd3a29d2015-06-24 00:12:36 +0000147 auto *C = new (Alloc) SectionChunk(this, Sec);
Rui Ueyama411c63602015-05-28 19:09:30 +0000148 Chunks.push_back(C);
149 SparseChunks[I] = C;
150 }
Rui Ueyama411c63602015-05-28 19:09:30 +0000151}
152
Rafael Espindolab835ae82015-08-06 14:58:50 +0000153void ObjectFile::initializeSymbols() {
Rui Ueyama411c63602015-05-28 19:09:30 +0000154 uint32_t NumSymbols = COFFObj->getNumberOfSymbols();
155 SymbolBodies.reserve(NumSymbols);
156 SparseSymbolBodies.resize(NumSymbols);
Peter Collingbourne79a5e6b2016-12-09 21:55:24 +0000157 SmallVector<std::pair<SymbolBody *, uint32_t>, 8> WeakAliases;
Rui Ueyama411c63602015-05-28 19:09:30 +0000158 int32_t LastSectionNumber = 0;
159 for (uint32_t I = 0; I < NumSymbols; ++I) {
160 // Get a COFFSymbolRef object.
Peter Collingbournefa7f3932016-12-09 20:51:33 +0000161 ErrorOr<COFFSymbolRef> SymOrErr = COFFObj->getSymbol(I);
162 if (!SymOrErr)
163 fatal(SymOrErr.getError(), "broken object file: " + toString(this));
164 COFFSymbolRef Sym = *SymOrErr;
Rui Ueyama411c63602015-05-28 19:09:30 +0000165
Rui Ueyama411c63602015-05-28 19:09:30 +0000166 const void *AuxP = nullptr;
167 if (Sym.getNumberOfAuxSymbols())
168 AuxP = COFFObj->getSymbol(I + 1)->getRawPtr();
169 bool IsFirst = (LastSectionNumber != Sym.getSectionNumber());
170
Rui Ueyamadae16612015-06-29 22:16:21 +0000171 SymbolBody *Body = nullptr;
172 if (Sym.isUndefined()) {
173 Body = createUndefined(Sym);
174 } else if (Sym.isWeakExternal()) {
David Majnemer2aa29ee2016-03-20 22:56:31 +0000175 Body = createUndefined(Sym);
176 uint32_t TagIndex =
177 static_cast<const coff_aux_weak_external *>(AuxP)->TagIndex;
Peter Collingbourne79a5e6b2016-12-09 21:55:24 +0000178 WeakAliases.emplace_back(Body, TagIndex);
Rui Ueyamadae16612015-06-29 22:16:21 +0000179 } else {
180 Body = createDefined(Sym, AuxP, IsFirst);
181 }
Rui Ueyama411c63602015-05-28 19:09:30 +0000182 if (Body) {
183 SymbolBodies.push_back(Body);
184 SparseSymbolBodies[I] = Body;
185 }
186 I += Sym.getNumberOfAuxSymbols();
187 LastSectionNumber = Sym.getSectionNumber();
188 }
Bob Haarmancde5e5b2017-02-02 23:58:14 +0000189 for (auto WeakAlias : WeakAliases)
190 checkAndSetWeakAlias(Symtab, this, WeakAlias.first,
191 SparseSymbolBodies[WeakAlias.second]);
Rui Ueyama411c63602015-05-28 19:09:30 +0000192}
193
Peter Collingbourne79a5e6b2016-12-09 21:55:24 +0000194SymbolBody *ObjectFile::createUndefined(COFFSymbolRef Sym) {
Rui Ueyama57fe78d2015-06-08 19:43:59 +0000195 StringRef Name;
Rui Ueyamadae16612015-06-29 22:16:21 +0000196 COFFObj->getSymbolName(Sym, Name);
Peter Collingbourne79a5e6b2016-12-09 21:55:24 +0000197 return Symtab->addUndefined(Name, this, Sym.isWeakExternal())->body();
Rui Ueyamadae16612015-06-29 22:16:21 +0000198}
199
Peter Collingbourne79a5e6b2016-12-09 21:55:24 +0000200SymbolBody *ObjectFile::createDefined(COFFSymbolRef Sym, const void *AuxP,
201 bool IsFirst) {
Rui Ueyamadae16612015-06-29 22:16:21 +0000202 StringRef Name;
Rui Ueyama411c63602015-05-28 19:09:30 +0000203 if (Sym.isCommon()) {
Rui Ueyamafc510f42015-06-25 19:10:58 +0000204 auto *C = new (Alloc) CommonChunk(Sym);
Rui Ueyama411c63602015-05-28 19:09:30 +0000205 Chunks.push_back(C);
Bob Haarmancde5e5b2017-02-02 23:58:14 +0000206 COFFObj->getSymbolName(Sym, Name);
207 Symbol *S =
208 Symtab->addCommon(this, Name, Sym.getValue(), Sym.getGeneric(), C);
209 return S->body();
Rui Ueyama411c63602015-05-28 19:09:30 +0000210 }
Rui Ueyama57fe78d2015-06-08 19:43:59 +0000211 if (Sym.isAbsolute()) {
212 COFFObj->getSymbolName(Sym, Name);
213 // Skip special symbols.
Rui Ueyamacd3f99b2015-07-24 23:51:14 +0000214 if (Name == "@comp.id")
Rui Ueyama57fe78d2015-06-08 19:43:59 +0000215 return nullptr;
Rui Ueyamacd3f99b2015-07-24 23:51:14 +0000216 // COFF spec 5.10.1. The .sxdata section.
217 if (Name == "@feat.00") {
218 if (Sym.getValue() & 1)
219 SEHCompat = true;
220 return nullptr;
221 }
Peter Collingbourne79a5e6b2016-12-09 21:55:24 +0000222 if (Sym.isExternal())
223 return Symtab->addAbsolute(Name, Sym)->body();
224 else
225 return new (Alloc) DefinedAbsolute(Name, Sym);
Rui Ueyama57fe78d2015-06-08 19:43:59 +0000226 }
David Majnemer272de1e2016-03-15 16:47:28 +0000227 int32_t SectionNumber = Sym.getSectionNumber();
228 if (SectionNumber == llvm::COFF::IMAGE_SYM_DEBUG)
Peter Collingbournec7b685d2015-06-24 00:05:50 +0000229 return nullptr;
Chandler Carruthee5bf522015-06-29 21:32:37 +0000230
David Majnemer272de1e2016-03-15 16:47:28 +0000231 // Reserved sections numbers don't have contents.
232 if (llvm::COFF::isReservedSectionNumber(SectionNumber))
Rui Ueyamaa45d45e2016-12-07 23:17:02 +0000233 fatal("broken object file: " + toString(this));
David Majnemer272de1e2016-03-15 16:47:28 +0000234
235 // This symbol references a section which is not present in the section
236 // header.
237 if ((uint32_t)SectionNumber >= SparseChunks.size())
Rui Ueyamaa45d45e2016-12-07 23:17:02 +0000238 fatal("broken object file: " + toString(this));
David Majnemer272de1e2016-03-15 16:47:28 +0000239
Chandler Carruthee5bf522015-06-29 21:32:37 +0000240 // Nothing else to do without a section chunk.
David Majnemer272de1e2016-03-15 16:47:28 +0000241 auto *SC = cast_or_null<SectionChunk>(SparseChunks[SectionNumber]);
Chandler Carruthee5bf522015-06-29 21:32:37 +0000242 if (!SC)
243 return nullptr;
244
Rui Ueyama2dcc2352015-09-04 20:45:50 +0000245 // Handle section definitions
Rui Ueyama411c63602015-05-28 19:09:30 +0000246 if (IsFirst && AuxP) {
Chandler Carruthee5bf522015-06-29 21:32:37 +0000247 auto *Aux = reinterpret_cast<const coff_aux_section_definition *>(AuxP);
248 if (Aux->Selection == IMAGE_COMDAT_SELECT_ASSOCIATIVE)
249 if (auto *ParentSC = cast_or_null<SectionChunk>(
250 SparseChunks[Aux->getNumber(Sym.isBigObj())]))
251 ParentSC->addAssociative(SC);
Rui Ueyama2dcc2352015-09-04 20:45:50 +0000252 SC->Checksum = Aux->CheckSum;
Rui Ueyama411c63602015-05-28 19:09:30 +0000253 }
Chandler Carruthee5bf522015-06-29 21:32:37 +0000254
Peter Collingbourne79a5e6b2016-12-09 21:55:24 +0000255 DefinedRegular *B;
Bob Haarmancde5e5b2017-02-02 23:58:14 +0000256 if (Sym.isExternal()) {
257 COFFObj->getSymbolName(Sym, Name);
258 Symbol *S =
259 Symtab->addRegular(this, Name, SC->isCOMDAT(), Sym.getGeneric(), SC);
260 B = cast<DefinedRegular>(S->body());
261 } else
262 B = new (Alloc) DefinedRegular(this, /*Name*/ "", SC->isCOMDAT(),
263 /*IsExternal*/ false, Sym.getGeneric(), SC);
Chandler Carruthee5bf522015-06-29 21:32:37 +0000264 if (SC->isCOMDAT() && Sym.getValue() == 0 && !AuxP)
265 SC->setSymbol(B);
266
267 return B;
Rui Ueyama411c63602015-05-28 19:09:30 +0000268}
269
Rafael Espindolab835ae82015-08-06 14:58:50 +0000270void ObjectFile::initializeSEH() {
Rui Ueyamacd3f99b2015-07-24 23:51:14 +0000271 if (!SEHCompat || !SXData)
Rafael Espindolab835ae82015-08-06 14:58:50 +0000272 return;
Rui Ueyamacd3f99b2015-07-24 23:51:14 +0000273 ArrayRef<uint8_t> A;
274 COFFObj->getSectionContents(SXData, A);
Rafael Espindolab835ae82015-08-06 14:58:50 +0000275 if (A.size() % 4 != 0)
Rui Ueyama60604792016-07-14 23:37:14 +0000276 fatal(".sxdata must be an array of symbol table indices");
Rui Ueyamacd3f99b2015-07-24 23:51:14 +0000277 auto *I = reinterpret_cast<const ulittle32_t *>(A.data());
278 auto *E = reinterpret_cast<const ulittle32_t *>(A.data() + A.size());
279 for (; I != E; ++I)
280 SEHandlers.insert(SparseSymbolBodies[*I]);
Rui Ueyamacd3f99b2015-07-24 23:51:14 +0000281}
282
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000283MachineTypes ObjectFile::getMachineType() {
284 if (COFFObj)
285 return static_cast<MachineTypes>(COFFObj->getMachine());
286 return IMAGE_FILE_MACHINE_UNKNOWN;
287}
288
Rui Ueyamaa841bb02015-07-09 20:22:41 +0000289StringRef ltrim1(StringRef S, const char *Chars) {
290 if (!S.empty() && strchr(Chars, S[0]))
291 return S.substr(1);
292 return S;
293}
294
Rafael Espindolab835ae82015-08-06 14:58:50 +0000295void ImportFile::parse() {
Rui Ueyamad7c2f582015-05-31 21:04:56 +0000296 const char *Buf = MB.getBufferStart();
297 const char *End = MB.getBufferEnd();
Rui Ueyama411c63602015-05-28 19:09:30 +0000298 const auto *Hdr = reinterpret_cast<const coff_import_header *>(Buf);
299
300 // Check if the total size is valid.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000301 if ((size_t)(End - Buf) != (sizeof(*Hdr) + Hdr->SizeOfData))
Rui Ueyama60604792016-07-14 23:37:14 +0000302 fatal("broken import library");
Rui Ueyama411c63602015-05-28 19:09:30 +0000303
304 // Read names and create an __imp_ symbol.
305 StringRef Name = StringAlloc.save(StringRef(Buf + sizeof(*Hdr)));
Rui Ueyama1a3fd132016-07-14 23:43:36 +0000306 StringRef ImpName = StringAlloc.save("__imp_" + Name);
Rui Ueyamad1570882015-08-17 08:30:31 +0000307 const char *NameStart = Buf + sizeof(coff_import_header) + Name.size() + 1;
Rui Ueyamabfbd2772015-09-02 07:27:31 +0000308 DLLName = StringRef(NameStart);
Rui Ueyama1c79ce92015-07-08 20:22:50 +0000309 StringRef ExtName;
310 switch (Hdr->getNameType()) {
311 case IMPORT_ORDINAL:
312 ExtName = "";
313 break;
314 case IMPORT_NAME:
315 ExtName = Name;
316 break;
317 case IMPORT_NAME_NOPREFIX:
Rui Ueyamaa841bb02015-07-09 20:22:41 +0000318 ExtName = ltrim1(Name, "?@_");
Rui Ueyama1c79ce92015-07-08 20:22:50 +0000319 break;
320 case IMPORT_NAME_UNDECORATE:
Rui Ueyamaa841bb02015-07-09 20:22:41 +0000321 ExtName = ltrim1(Name, "?@_");
Rui Ueyama1c79ce92015-07-08 20:22:50 +0000322 ExtName = ExtName.substr(0, ExtName.find('@'));
323 break;
324 }
Peter Collingbourne79a5e6b2016-12-09 21:55:24 +0000325
326 this->Hdr = Hdr;
327 ExternalName = ExtName;
328
329 ImpSym = cast<DefinedImportData>(
330 Symtab->addImportData(ImpName, this)->body());
Rui Ueyama411c63602015-05-28 19:09:30 +0000331
332 // If type is function, we need to create a thunk which jump to an
333 // address pointed by the __imp_ symbol. (This allows you to call
334 // DLL functions just like regular non-DLL functions.)
Rui Ueyamae73e4182015-08-17 07:27:45 +0000335 if (Hdr->getType() != llvm::COFF::IMPORT_CODE)
336 return;
Peter Collingbourne79a5e6b2016-12-09 21:55:24 +0000337 ThunkSym = cast<DefinedImportThunk>(
338 Symtab->addImportThunk(Name, ImpSym, Hdr->Machine)->body());
Rui Ueyama411c63602015-05-28 19:09:30 +0000339}
340
Rafael Espindolab835ae82015-08-06 14:58:50 +0000341void BitcodeFile::parse() {
Bob Haarmancde5e5b2017-02-02 23:58:14 +0000342 Obj = check(lto::InputFile::create(
343 MemoryBufferRef(MB.getBuffer(), Saver.save(MB.getBufferIdentifier()))));
344 for (const lto::InputFile::Symbol &ObjSym : Obj->symbols()) {
345 StringRef SymName = Saver.save(ObjSym.getName());
346 auto Flags = ObjSym.getFlags();
347 Symbol *Sym;
348 if (Flags & object::BasicSymbolRef::SF_Undefined) {
349 Sym = Symtab->addUndefined(SymName, this, false);
350 } else if (Flags & object::BasicSymbolRef::SF_Common) {
351 Sym = Symtab->addCommon(this, SymName, ObjSym.getCommonSize());
352 } else if ((Flags & object::BasicSymbolRef::SF_Weak) &&
353 (Flags & object::BasicSymbolRef::SF_Indirect)) {
354 // Weak external.
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);
Peter Collingbourne60c16162015-06-01 20:10:10 +0000359 } else {
Bob Haarmancde5e5b2017-02-02 23:58:14 +0000360 Expected<int> ComdatIndex = ObjSym.getComdatIndex();
361 bool IsCOMDAT = ComdatIndex && *ComdatIndex != -1;
362 Sym = Symtab->addRegular(this, SymName, IsCOMDAT);
Peter Collingbourne60c16162015-06-01 20:10:10 +0000363 }
Bob Haarmancde5e5b2017-02-02 23:58:14 +0000364 SymbolBodies.push_back(Sym->body());
Peter Collingbourne60c16162015-06-01 20:10:10 +0000365 }
Bob Haarmancde5e5b2017-02-02 23:58:14 +0000366 Directives = check(Obj->getLinkerOpts());
Peter Collingbourne60c16162015-06-01 20:10:10 +0000367}
Rui Ueyama81b030c2015-06-01 21:19:43 +0000368
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000369MachineTypes BitcodeFile::getMachineType() {
Bob Haarmancde5e5b2017-02-02 23:58:14 +0000370 Expected<std::string> ET = getBitcodeTargetTriple(MB);
371 if (!ET)
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000372 return IMAGE_FILE_MACHINE_UNKNOWN;
Bob Haarmancde5e5b2017-02-02 23:58:14 +0000373 switch (Triple(*ET).getArch()) {
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000374 case Triple::x86_64:
Rui Ueyama5e706b32015-07-25 21:54:50 +0000375 return AMD64;
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000376 case Triple::x86:
Rui Ueyama5e706b32015-07-25 21:54:50 +0000377 return I386;
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000378 case Triple::arm:
Rui Ueyama5e706b32015-07-25 21:54:50 +0000379 return ARMNT;
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000380 default:
381 return IMAGE_FILE_MACHINE_UNKNOWN;
382 }
383}
Rui Ueyamace039262017-01-06 10:04:08 +0000384} // namespace coff
385} // namespace lld
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000386
Rui Ueyamaa45d45e2016-12-07 23:17:02 +0000387// Returns the last element of a path, which is supposed to be a filename.
388static StringRef getBasename(StringRef Path) {
389 size_t Pos = Path.find_last_of("\\/");
390 if (Pos == StringRef::npos)
391 return Path;
392 return Path.substr(Pos + 1);
393}
394
395// Returns a string in the format of "foo.obj" or "foo.obj(bar.lib)".
Rui Ueyamace039262017-01-06 10:04:08 +0000396std::string lld::toString(coff::InputFile *File) {
Rui Ueyamaa45d45e2016-12-07 23:17:02 +0000397 if (!File)
398 return "(internal)";
399 if (File->ParentName.empty())
400 return File->getName().lower();
401
402 std::string Res =
403 (getBasename(File->ParentName) + "(" + getBasename(File->getName()) + ")")
404 .str();
405 return StringRef(Res).lower();
406}