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/BinaryFormat/COFF.h"
23 #include "llvm/Object/Binary.h"
24 #include "llvm/Object/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 std::vector
<ObjFile
*> ObjFile::Instances
;
47 std::vector
<ImportFile
*> ImportFile::Instances
;
48 std::vector
<BitcodeFile
*> BitcodeFile::Instances
;
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
53 static void checkAndSetWeakAlias(SymbolTable
*Symtab
, InputFile
*F
,
54 SymbolBody
*Source
, SymbolBody
*Target
) {
55 if (auto *U
= dyn_cast
<Undefined
>(Source
)) {
56 if (U
->WeakAlias
&& U
->WeakAlias
!= Target
)
57 Symtab
->reportDuplicate(Source
->symbol(), F
);
58 U
->WeakAlias
= Target
;
62 ArchiveFile::ArchiveFile(MemoryBufferRef M
) : InputFile(ArchiveKind
, M
) {}
64 void ArchiveFile::parse() {
65 // Parse a MemoryBufferRef as an archive file.
66 File
= check(Archive::create(MB
), toString(this));
68 // Read the symbol table to construct Lazy objects.
69 for (const Archive::Symbol
&Sym
: File
->symbols())
70 Symtab
->addLazy(this, Sym
);
73 // Returns a buffer pointing to a member file containing a given symbol.
74 void ArchiveFile::addMember(const Archive::Symbol
*Sym
) {
75 const Archive::Child
&C
=
76 check(Sym
->getMember(),
77 "could not get the member for symbol " + Sym
->getName());
79 // Return an empty buffer if we have already returned the same buffer.
80 if (!Seen
.insert(C
.getChildOffset()).second
)
83 Driver
->enqueueArchiveMember(C
, Sym
->getName(), getName());
86 void ObjFile::parse() {
87 // Parse a memory buffer as a COFF file.
88 std::unique_ptr
<Binary
> Bin
= check(createBinary(MB
), toString(this));
90 if (auto *Obj
= dyn_cast
<COFFObjectFile
>(Bin
.get())) {
94 fatal(toString(this) + " is not a COFF file");
97 // Read section and symbol tables.
103 void ObjFile::initializeChunks() {
104 uint32_t NumSections
= COFFObj
->getNumberOfSections();
105 Chunks
.reserve(NumSections
);
106 SparseChunks
.resize(NumSections
+ 1);
107 for (uint32_t I
= 1; I
< NumSections
+ 1; ++I
) {
108 const coff_section
*Sec
;
110 if (auto EC
= COFFObj
->getSection(I
, Sec
))
111 fatal(EC
, "getSection failed: #" + Twine(I
));
112 if (auto EC
= COFFObj
->getSectionName(Sec
, Name
))
113 fatal(EC
, "getSectionName failed: #" + Twine(I
));
114 if (Name
== ".sxdata") {
118 if (Name
== ".drectve") {
119 ArrayRef
<uint8_t> Data
;
120 COFFObj
->getSectionContents(Sec
, Data
);
121 Directives
= std::string((const char *)Data
.data(), Data
.size());
125 // Object files may have DWARF debug info or MS CodeView debug info
128 // DWARF sections don't need any special handling from the perspective
129 // of the linker; they are just a data section containing relocations.
130 // We can just link them to complete debug info.
132 // CodeView needs a linker support. We need to interpret and debug
133 // info, and then write it to a separate .pdb file.
135 // Ignore debug info unless /debug is given.
136 if (!Config
->Debug
&& Name
.startswith(".debug"))
139 if (Sec
->Characteristics
& llvm::COFF::IMAGE_SCN_LNK_REMOVE
)
141 auto *C
= make
<SectionChunk
>(this, Sec
);
143 // CodeView sections are stored to a different vector because they are not
144 // linked in the regular manner.
146 DebugChunks
.push_back(C
);
154 void ObjFile::initializeSymbols() {
155 uint32_t NumSymbols
= COFFObj
->getNumberOfSymbols();
156 SymbolBodies
.reserve(NumSymbols
);
157 SparseSymbolBodies
.resize(NumSymbols
);
159 SmallVector
<std::pair
<SymbolBody
*, uint32_t>, 8> WeakAliases
;
160 int32_t LastSectionNumber
= 0;
162 for (uint32_t I
= 0; I
< NumSymbols
; ++I
) {
163 // Get a COFFSymbolRef object.
164 ErrorOr
<COFFSymbolRef
> SymOrErr
= COFFObj
->getSymbol(I
);
166 fatal(SymOrErr
.getError(), "broken object file: " + toString(this));
167 COFFSymbolRef Sym
= *SymOrErr
;
169 const void *AuxP
= nullptr;
170 if (Sym
.getNumberOfAuxSymbols())
171 AuxP
= COFFObj
->getSymbol(I
+ 1)->getRawPtr();
172 bool IsFirst
= (LastSectionNumber
!= Sym
.getSectionNumber());
174 SymbolBody
*Body
= nullptr;
175 if (Sym
.isUndefined()) {
176 Body
= createUndefined(Sym
);
177 } else if (Sym
.isWeakExternal()) {
178 Body
= createUndefined(Sym
);
180 static_cast<const coff_aux_weak_external
*>(AuxP
)->TagIndex
;
181 WeakAliases
.emplace_back(Body
, TagIndex
);
183 Body
= createDefined(Sym
, AuxP
, IsFirst
);
186 SymbolBodies
.push_back(Body
);
187 SparseSymbolBodies
[I
] = Body
;
189 I
+= Sym
.getNumberOfAuxSymbols();
190 LastSectionNumber
= Sym
.getSectionNumber();
193 for (auto &KV
: WeakAliases
) {
194 SymbolBody
*Sym
= KV
.first
;
195 uint32_t Idx
= KV
.second
;
196 checkAndSetWeakAlias(Symtab
, this, Sym
, SparseSymbolBodies
[Idx
]);
200 SymbolBody
*ObjFile::createUndefined(COFFSymbolRef Sym
) {
202 COFFObj
->getSymbolName(Sym
, Name
);
203 return Symtab
->addUndefined(Name
, this, Sym
.isWeakExternal())->body();
206 SymbolBody
*ObjFile::createDefined(COFFSymbolRef Sym
, const void *AuxP
,
209 if (Sym
.isCommon()) {
210 auto *C
= make
<CommonChunk
>(Sym
);
212 COFFObj
->getSymbolName(Sym
, Name
);
214 Symtab
->addCommon(this, Name
, Sym
.getValue(), Sym
.getGeneric(), C
);
217 if (Sym
.isAbsolute()) {
218 COFFObj
->getSymbolName(Sym
, Name
);
219 // Skip special symbols.
220 if (Name
== "@comp.id")
222 // COFF spec 5.10.1. The .sxdata section.
223 if (Name
== "@feat.00") {
224 if (Sym
.getValue() & 1)
228 if (Sym
.isExternal())
229 return Symtab
->addAbsolute(Name
, Sym
)->body();
231 return make
<DefinedAbsolute
>(Name
, Sym
);
233 int32_t SectionNumber
= Sym
.getSectionNumber();
234 if (SectionNumber
== llvm::COFF::IMAGE_SYM_DEBUG
)
237 // Reserved sections numbers don't have contents.
238 if (llvm::COFF::isReservedSectionNumber(SectionNumber
))
239 fatal("broken object file: " + toString(this));
241 // This symbol references a section which is not present in the section
243 if ((uint32_t)SectionNumber
>= SparseChunks
.size())
244 fatal("broken object file: " + toString(this));
246 // Nothing else to do without a section chunk.
247 auto *SC
= cast_or_null
<SectionChunk
>(SparseChunks
[SectionNumber
]);
251 // Handle section definitions
252 if (IsFirst
&& AuxP
) {
253 auto *Aux
= reinterpret_cast<const coff_aux_section_definition
*>(AuxP
);
254 if (Aux
->Selection
== IMAGE_COMDAT_SELECT_ASSOCIATIVE
)
255 if (auto *ParentSC
= cast_or_null
<SectionChunk
>(
256 SparseChunks
[Aux
->getNumber(Sym
.isBigObj())])) {
257 ParentSC
->addAssociative(SC
);
258 // If we already discarded the parent, discard the child.
259 if (ParentSC
->isDiscarded())
262 SC
->Checksum
= Aux
->CheckSum
;
266 if (Sym
.isExternal()) {
267 COFFObj
->getSymbolName(Sym
, Name
);
269 Symtab
->addRegular(this, Name
, SC
->isCOMDAT(), Sym
.getGeneric(), SC
);
270 B
= cast
<DefinedRegular
>(S
->body());
272 B
= make
<DefinedRegular
>(this, /*Name*/ "", SC
->isCOMDAT(),
273 /*IsExternal*/ false, Sym
.getGeneric(), SC
);
274 if (SC
->isCOMDAT() && Sym
.getValue() == 0 && !AuxP
)
280 void ObjFile::initializeSEH() {
281 if (!SEHCompat
|| !SXData
)
284 COFFObj
->getSectionContents(SXData
, A
);
285 if (A
.size() % 4 != 0)
286 fatal(".sxdata must be an array of symbol table indices");
287 auto *I
= reinterpret_cast<const ulittle32_t
*>(A
.data());
288 auto *E
= reinterpret_cast<const ulittle32_t
*>(A
.data() + A
.size());
290 SEHandlers
.insert(SparseSymbolBodies
[*I
]);
293 MachineTypes
ObjFile::getMachineType() {
295 return static_cast<MachineTypes
>(COFFObj
->getMachine());
296 return IMAGE_FILE_MACHINE_UNKNOWN
;
299 StringRef
ltrim1(StringRef S
, const char *Chars
) {
300 if (!S
.empty() && strchr(Chars
, S
[0]))
305 void ImportFile::parse() {
306 const char *Buf
= MB
.getBufferStart();
307 const char *End
= MB
.getBufferEnd();
308 const auto *Hdr
= reinterpret_cast<const coff_import_header
*>(Buf
);
310 // Check if the total size is valid.
311 if ((size_t)(End
- Buf
) != (sizeof(*Hdr
) + Hdr
->SizeOfData
))
312 fatal("broken import library");
314 // Read names and create an __imp_ symbol.
315 StringRef Name
= Saver
.save(StringRef(Buf
+ sizeof(*Hdr
)));
316 StringRef ImpName
= Saver
.save("__imp_" + Name
);
317 const char *NameStart
= Buf
+ sizeof(coff_import_header
) + Name
.size() + 1;
318 DLLName
= StringRef(NameStart
);
320 switch (Hdr
->getNameType()) {
327 case IMPORT_NAME_NOPREFIX
:
328 ExtName
= ltrim1(Name
, "?@_");
330 case IMPORT_NAME_UNDECORATE
:
331 ExtName
= ltrim1(Name
, "?@_");
332 ExtName
= ExtName
.substr(0, ExtName
.find('@'));
337 ExternalName
= ExtName
;
339 ImpSym
= cast
<DefinedImportData
>(
340 Symtab
->addImportData(ImpName
, this)->body());
341 if (Hdr
->getType() == llvm::COFF::IMPORT_CONST
)
343 cast
<DefinedImportData
>(Symtab
->addImportData(Name
, this)->body());
345 // If type is function, we need to create a thunk which jump to an
346 // address pointed by the __imp_ symbol. (This allows you to call
347 // DLL functions just like regular non-DLL functions.)
348 if (Hdr
->getType() != llvm::COFF::IMPORT_CODE
)
350 ThunkSym
= cast
<DefinedImportThunk
>(
351 Symtab
->addImportThunk(Name
, ImpSym
, Hdr
->Machine
)->body());
354 void BitcodeFile::parse() {
355 Obj
= check(lto::InputFile::create(MemoryBufferRef(
356 MB
.getBuffer(), Saver
.save(ParentName
+ MB
.getBufferIdentifier()))));
357 for (const lto::InputFile::Symbol
&ObjSym
: Obj
->symbols()) {
358 StringRef SymName
= Saver
.save(ObjSym
.getName());
360 if (ObjSym
.isUndefined()) {
361 Sym
= Symtab
->addUndefined(SymName
, this, false);
362 } else if (ObjSym
.isCommon()) {
363 Sym
= Symtab
->addCommon(this, SymName
, ObjSym
.getCommonSize());
364 } else if (ObjSym
.isWeak() && ObjSym
.isIndirect()) {
366 Sym
= Symtab
->addUndefined(SymName
, this, true);
367 std::string Fallback
= ObjSym
.getCOFFWeakExternalFallback();
368 SymbolBody
*Alias
= Symtab
->addUndefined(Saver
.save(Fallback
));
369 checkAndSetWeakAlias(Symtab
, this, Sym
->body(), Alias
);
371 bool IsCOMDAT
= ObjSym
.getComdatIndex() != -1;
372 Sym
= Symtab
->addRegular(this, SymName
, IsCOMDAT
);
374 SymbolBodies
.push_back(Sym
->body());
376 Directives
= Obj
->getCOFFLinkerOpts();
379 MachineTypes
BitcodeFile::getMachineType() {
380 switch (Triple(Obj
->getTargetTriple()).getArch()) {
387 case Triple::aarch64
:
390 return IMAGE_FILE_MACHINE_UNKNOWN
;
396 // Returns the last element of a path, which is supposed to be a filename.
397 static StringRef
getBasename(StringRef Path
) {
398 size_t Pos
= Path
.find_last_of("\\/");
399 if (Pos
== StringRef::npos
)
401 return Path
.substr(Pos
+ 1);
404 // Returns a string in the format of "foo.obj" or "foo.obj(bar.lib)".
405 std::string
lld::toString(coff::InputFile
*File
) {
408 if (File
->ParentName
.empty())
409 return File
->getName().lower();
412 (getBasename(File
->ParentName
) + "(" + getBasename(File
->getName()) + ")")
414 return StringRef(Res
).lower();