Bug Summary

File:tools/lld/COFF/Writer.cpp
Warning:line 657, column 23
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name Writer.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-eagerly-assume -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-7/lib/clang/7.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/tools/lld/COFF -I /build/llvm-toolchain-snapshot-7~svn329677/tools/lld/COFF -I /build/llvm-toolchain-snapshot-7~svn329677/tools/lld/include -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/tools/lld/include -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/include -I /build/llvm-toolchain-snapshot-7~svn329677/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0/backward -internal-isystem /usr/include/clang/7.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-7/lib/clang/7.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/tools/lld/COFF -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-04-11-031539-24776-1 -x c++ /build/llvm-toolchain-snapshot-7~svn329677/tools/lld/COFF/Writer.cpp
1//===- Writer.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 "Writer.h"
11#include "Config.h"
12#include "DLL.h"
13#include "InputFiles.h"
14#include "MapFile.h"
15#include "PDB.h"
16#include "SymbolTable.h"
17#include "Symbols.h"
18#include "lld/Common/ErrorHandler.h"
19#include "lld/Common/Memory.h"
20#include "lld/Common/Timer.h"
21#include "llvm/ADT/DenseMap.h"
22#include "llvm/ADT/STLExtras.h"
23#include "llvm/ADT/StringSwitch.h"
24#include "llvm/Support/BinaryStreamReader.h"
25#include "llvm/Support/Debug.h"
26#include "llvm/Support/Endian.h"
27#include "llvm/Support/FileOutputBuffer.h"
28#include "llvm/Support/Parallel.h"
29#include "llvm/Support/Path.h"
30#include "llvm/Support/RandomNumberGenerator.h"
31#include "llvm/Support/xxhash.h"
32#include <algorithm>
33#include <cstdio>
34#include <map>
35#include <memory>
36#include <utility>
37
38using namespace llvm;
39using namespace llvm::COFF;
40using namespace llvm::object;
41using namespace llvm::support;
42using namespace llvm::support::endian;
43using namespace lld;
44using namespace lld::coff;
45
46/* To re-generate DOSProgram:
47$ cat > /tmp/DOSProgram.asm
48org 0
49 ; Copy cs to ds.
50 push cs
51 pop ds
52 ; Point ds:dx at the $-terminated string.
53 mov dx, str
54 ; Int 21/AH=09h: Write string to standard output.
55 mov ah, 0x9
56 int 0x21
57 ; Int 21/AH=4Ch: Exit with return code (in AL).
58 mov ax, 0x4C01
59 int 0x21
60str:
61 db 'This program cannot be run in DOS mode.$'
62align 8, db 0
63$ nasm -fbin /tmp/DOSProgram.asm -o /tmp/DOSProgram.bin
64$ xxd -i /tmp/DOSProgram.bin
65*/
66static unsigned char DOSProgram[] = {
67 0x0e, 0x1f, 0xba, 0x0e, 0x00, 0xb4, 0x09, 0xcd, 0x21, 0xb8, 0x01, 0x4c,
68 0xcd, 0x21, 0x54, 0x68, 0x69, 0x73, 0x20, 0x70, 0x72, 0x6f, 0x67, 0x72,
69 0x61, 0x6d, 0x20, 0x63, 0x61, 0x6e, 0x6e, 0x6f, 0x74, 0x20, 0x62, 0x65,
70 0x20, 0x72, 0x75, 0x6e, 0x20, 0x69, 0x6e, 0x20, 0x44, 0x4f, 0x53, 0x20,
71 0x6d, 0x6f, 0x64, 0x65, 0x2e, 0x24, 0x00, 0x00
72};
73static_assert(sizeof(DOSProgram) % 8 == 0,
74 "DOSProgram size must be multiple of 8");
75
76static const int SectorSize = 512;
77static const int DOSStubSize = sizeof(dos_header) + sizeof(DOSProgram);
78static_assert(DOSStubSize % 8 == 0, "DOSStub size must be multiple of 8");
79
80static const int NumberfOfDataDirectory = 16;
81
82namespace {
83
84class DebugDirectoryChunk : public Chunk {
85public:
86 DebugDirectoryChunk(const std::vector<Chunk *> &R) : Records(R) {}
87
88 size_t getSize() const override {
89 return Records.size() * sizeof(debug_directory);
90 }
91
92 void writeTo(uint8_t *B) const override {
93 auto *D = reinterpret_cast<debug_directory *>(B + OutputSectionOff);
94
95 for (const Chunk *Record : Records) {
96 D->Characteristics = 0;
97 D->TimeDateStamp = 0;
98 D->MajorVersion = 0;
99 D->MinorVersion = 0;
100 D->Type = COFF::IMAGE_DEBUG_TYPE_CODEVIEW;
101 D->SizeOfData = Record->getSize();
102 D->AddressOfRawData = Record->getRVA();
103 OutputSection *OS = Record->getOutputSection();
104 uint64_t Offs = OS->getFileOff() + (Record->getRVA() - OS->getRVA());
105 D->PointerToRawData = Offs;
106
107 TimeDateStamps.push_back(&D->TimeDateStamp);
108 ++D;
109 }
110 }
111
112 void setTimeDateStamp(uint32_t TimeDateStamp) {
113 for (support::ulittle32_t *TDS : TimeDateStamps)
114 *TDS = TimeDateStamp;
115 }
116
117private:
118 mutable std::vector<support::ulittle32_t *> TimeDateStamps;
119 const std::vector<Chunk *> &Records;
120};
121
122class CVDebugRecordChunk : public Chunk {
123public:
124 CVDebugRecordChunk() {
125 PDBAbsPath = Config->PDBPath;
126 if (!PDBAbsPath.empty())
127 llvm::sys::fs::make_absolute(PDBAbsPath);
128 }
129
130 size_t getSize() const override {
131 return sizeof(codeview::DebugInfo) + PDBAbsPath.size() + 1;
132 }
133
134 void writeTo(uint8_t *B) const override {
135 // Save off the DebugInfo entry to backfill the file signature (build id)
136 // in Writer::writeBuildId
137 BuildId = reinterpret_cast<codeview::DebugInfo *>(B + OutputSectionOff);
138
139 // variable sized field (PDB Path)
140 char *P = reinterpret_cast<char *>(B + OutputSectionOff + sizeof(*BuildId));
141 if (!PDBAbsPath.empty())
142 memcpy(P, PDBAbsPath.data(), PDBAbsPath.size());
143 P[PDBAbsPath.size()] = '\0';
144 }
145
146 SmallString<128> PDBAbsPath;
147 mutable codeview::DebugInfo *BuildId = nullptr;
148};
149
150// The writer writes a SymbolTable result to a file.
151class Writer {
152public:
153 Writer() : Buffer(errorHandler().OutputBuffer) {}
154 void run();
155
156private:
157 void createSections();
158 void createMiscChunks();
159 void createImportTables();
160 void createExportTable();
161 void assignAddresses();
162 void removeEmptySections();
163 void createSymbolAndStringTable();
164 void openFile(StringRef OutputPath);
165 template <typename PEHeaderTy> void writeHeader();
166 void createSEHTable();
167 void createGuardCFTables();
168 void markSymbolsForRVATable(ObjFile *File,
169 ArrayRef<SectionChunk *> SymIdxChunks,
170 SymbolRVASet &TableSymbols);
171 void maybeAddRVATable(SymbolRVASet TableSymbols, StringRef TableSym,
172 StringRef CountSym);
173 void setSectionPermissions();
174 void writeSections();
175 void writeBuildId();
176 void sortExceptionTable();
177
178 llvm::Optional<coff_symbol16> createSymbol(Defined *D);
179 size_t addEntryToStringTable(StringRef Str);
180
181 OutputSection *findSection(StringRef Name);
182 void addBaserels();
183 void addBaserelBlocks(std::vector<Baserel> &V);
184
185 uint32_t getSizeOfInitializedData();
186 std::map<StringRef, std::vector<DefinedImportData *>> binImports();
187
188 std::unique_ptr<FileOutputBuffer> &Buffer;
189 std::vector<OutputSection *> OutputSections;
190 std::vector<char> Strtab;
191 std::vector<llvm::object::coff_symbol16> OutputSymtab;
192 IdataContents Idata;
193 DelayLoadContents DelayIdata;
194 EdataContents Edata;
195 RVATableChunk *GuardFidsTable = nullptr;
196 RVATableChunk *SEHTable = nullptr;
197
198 DebugDirectoryChunk *DebugDirectory = nullptr;
199 std::vector<Chunk *> DebugRecords;
200 CVDebugRecordChunk *BuildId = nullptr;
201 Optional<codeview::DebugInfo> PreviousBuildId;
202 ArrayRef<uint8_t> SectionTable;
203
204 uint64_t FileSize;
205 uint32_t PointerToSymbolTable = 0;
206 uint64_t SizeOfImage;
207 uint64_t SizeOfHeaders;
208
209 OutputSection *TextSec;
210 OutputSection *RdataSec;
211 OutputSection *DataSec;
212 OutputSection *IdataSec;
213 OutputSection *EdataSec;
214 OutputSection *DidatSec;
215 OutputSection *RsrcSec;
216 OutputSection *RelocSec;
217
218 // The first and last .pdata sections in the output file.
219 //
220 // We need to keep track of the location of .pdata in whichever section it
221 // gets merged into so that we can sort its contents and emit a correct data
222 // directory entry for the exception table. This is also the case for some
223 // other sections (such as .edata) but because the contents of those sections
224 // are entirely linker-generated we can keep track of their locations using
225 // the chunks that the linker creates. All .pdata chunks come from input
226 // files, so we need to keep track of them separately.
227 Chunk *FirstPdata = nullptr;
228 Chunk *LastPdata;
229};
230} // anonymous namespace
231
232namespace lld {
233namespace coff {
234
235static Timer CodeLayoutTimer("Code Layout", Timer::root());
236static Timer DiskCommitTimer("Commit Output File", Timer::root());
237
238void writeResult() { Writer().run(); }
1
Calling 'Writer::run'
239
240void OutputSection::addChunk(Chunk *C) {
241 Chunks.push_back(C);
242 C->setOutputSection(this);
243}
244
245void OutputSection::addPermissions(uint32_t C) {
246 Header.Characteristics |= C & PermMask;
247}
248
249void OutputSection::setPermissions(uint32_t C) {
250 Header.Characteristics = C & PermMask;
251}
252
253// Write the section header to a given buffer.
254void OutputSection::writeHeaderTo(uint8_t *Buf) {
255 auto *Hdr = reinterpret_cast<coff_section *>(Buf);
256 *Hdr = Header;
257 if (StringTableOff) {
258 // If name is too long, write offset into the string table as a name.
259 sprintf(Hdr->Name, "/%d", StringTableOff);
260 } else {
261 assert(!Config->Debug || Name.size() <= COFF::NameSize ||(static_cast <bool> (!Config->Debug || Name.size() <=
COFF::NameSize || (Hdr->Characteristics & IMAGE_SCN_MEM_DISCARDABLE
) == 0) ? void (0) : __assert_fail ("!Config->Debug || Name.size() <= COFF::NameSize || (Hdr->Characteristics & IMAGE_SCN_MEM_DISCARDABLE) == 0"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/lld/COFF/Writer.cpp"
, 262, __extension__ __PRETTY_FUNCTION__))
262 (Hdr->Characteristics & IMAGE_SCN_MEM_DISCARDABLE) == 0)(static_cast <bool> (!Config->Debug || Name.size() <=
COFF::NameSize || (Hdr->Characteristics & IMAGE_SCN_MEM_DISCARDABLE
) == 0) ? void (0) : __assert_fail ("!Config->Debug || Name.size() <= COFF::NameSize || (Hdr->Characteristics & IMAGE_SCN_MEM_DISCARDABLE) == 0"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/lld/COFF/Writer.cpp"
, 262, __extension__ __PRETTY_FUNCTION__))
;
263 strncpy(Hdr->Name, Name.data(),
264 std::min(Name.size(), (size_t)COFF::NameSize));
265 }
266}
267
268} // namespace coff
269} // namespace lld
270
271// PDBs are matched against executables using a build id which consists of three
272// components:
273// 1. A 16-bit GUID
274// 2. An age
275// 3. A time stamp.
276//
277// Debuggers and symbol servers match executables against debug info by checking
278// each of these components of the EXE/DLL against the corresponding value in
279// the PDB and failing a match if any of the components differ. In the case of
280// symbol servers, symbols are cached in a folder that is a function of the
281// GUID. As a result, in order to avoid symbol cache pollution where every
282// incremental build copies a new PDB to the symbol cache, we must try to re-use
283// the existing GUID if one exists, but bump the age. This way the match will
284// fail, so the symbol cache knows to use the new PDB, but the GUID matches, so
285// it overwrites the existing item in the symbol cache rather than making a new
286// one.
287static Optional<codeview::DebugInfo> loadExistingBuildId(StringRef Path) {
288 // We don't need to incrementally update a previous build id if we're not
289 // writing codeview debug info.
290 if (!Config->Debug)
291 return None;
292
293 auto ExpectedBinary = llvm::object::createBinary(Path);
294 if (!ExpectedBinary) {
295 consumeError(ExpectedBinary.takeError());
296 return None;
297 }
298
299 auto Binary = std::move(*ExpectedBinary);
300 if (!Binary.getBinary()->isCOFF())
301 return None;
302
303 std::error_code EC;
304 COFFObjectFile File(Binary.getBinary()->getMemoryBufferRef(), EC);
305 if (EC)
306 return None;
307
308 // If the machine of the binary we're outputting doesn't match the machine
309 // of the existing binary, don't try to re-use the build id.
310 if (File.is64() != Config->is64() || File.getMachine() != Config->Machine)
311 return None;
312
313 for (const auto &DebugDir : File.debug_directories()) {
314 if (DebugDir.Type != IMAGE_DEBUG_TYPE_CODEVIEW)
315 continue;
316
317 const codeview::DebugInfo *ExistingDI = nullptr;
318 StringRef PDBFileName;
319 if (auto EC = File.getDebugPDBInfo(ExistingDI, PDBFileName)) {
320 (void)EC;
321 return None;
322 }
323 // We only support writing PDBs in v70 format. So if this is not a build
324 // id that we recognize / support, ignore it.
325 if (ExistingDI->Signature.CVSignature != OMF::Signature::PDB70)
326 return None;
327 return *ExistingDI;
328 }
329 return None;
330}
331
332// The main function of the writer.
333void Writer::run() {
334 ScopedTimer T1(CodeLayoutTimer);
335
336 createSections();
337 createMiscChunks();
338 createImportTables();
339 createExportTable();
340 assignAddresses();
341 removeEmptySections();
342 setSectionPermissions();
343 createSymbolAndStringTable();
2
Calling 'Writer::createSymbolAndStringTable'
344
345 if (FileSize > UINT32_MAX(4294967295U))
346 fatal("image size (" + Twine(FileSize) + ") " +
347 "exceeds maximum allowable size (" + Twine(UINT32_MAX(4294967295U)) + ")");
348
349 // We must do this before opening the output file, as it depends on being able
350 // to read the contents of the existing output file.
351 PreviousBuildId = loadExistingBuildId(Config->OutputFile);
352 openFile(Config->OutputFile);
353 if (Config->is64()) {
354 writeHeader<pe32plus_header>();
355 } else {
356 writeHeader<pe32_header>();
357 }
358 writeSections();
359 sortExceptionTable();
360 writeBuildId();
361
362 T1.stop();
363
364 if (!Config->PDBPath.empty() && Config->Debug) {
365 assert(BuildId)(static_cast <bool> (BuildId) ? void (0) : __assert_fail
("BuildId", "/build/llvm-toolchain-snapshot-7~svn329677/tools/lld/COFF/Writer.cpp"
, 365, __extension__ __PRETTY_FUNCTION__))
;
366 createPDB(Symtab, OutputSections, SectionTable, *BuildId->BuildId);
367 }
368
369 writeMapFile(OutputSections);
370
371 ScopedTimer T2(DiskCommitTimer);
372 if (auto E = Buffer->commit())
373 fatal("failed to write the output file: " + toString(std::move(E)));
374}
375
376static StringRef getOutputSectionName(StringRef Name) {
377 StringRef S = Name.split('$').first;
378
379 // Treat a later period as a separator for MinGW, for sections like
380 // ".ctors.01234".
381 return S.substr(0, S.find('.', 1));
382}
383
384// For /order.
385static void sortBySectionOrder(std::vector<Chunk *> &Chunks) {
386 auto GetPriority = [](const Chunk *C) {
387 if (auto *Sec = dyn_cast<SectionChunk>(C))
388 if (Sec->Sym)
389 return Config->Order.lookup(Sec->Sym->getName());
390 return 0;
391 };
392
393 std::stable_sort(Chunks.begin(), Chunks.end(),
394 [=](const Chunk *A, const Chunk *B) {
395 return GetPriority(A) < GetPriority(B);
396 });
397}
398
399// Create output section objects and add them to OutputSections.
400void Writer::createSections() {
401 // First, create the builtin sections.
402 const uint32_t DATA = IMAGE_SCN_CNT_INITIALIZED_DATA;
403 const uint32_t BSS = IMAGE_SCN_CNT_UNINITIALIZED_DATA;
404 const uint32_t CODE = IMAGE_SCN_CNT_CODE;
405 const uint32_t DISCARDABLE = IMAGE_SCN_MEM_DISCARDABLE;
406 const uint32_t R = IMAGE_SCN_MEM_READ;
407 const uint32_t W = IMAGE_SCN_MEM_WRITE;
408 const uint32_t X = IMAGE_SCN_MEM_EXECUTE;
409
410 SmallDenseMap<StringRef, OutputSection *> Sections;
411 auto CreateSection = [&](StringRef Name, uint32_t Perms) {
412 auto I = Config->Merge.find(Name);
413 if (I != Config->Merge.end())
414 Name = I->second;
415 OutputSection *&Sec = Sections[Name];
416 if (!Sec) {
417 Sec = make<OutputSection>(Name);
418 OutputSections.push_back(Sec);
419 }
420 Sec->addPermissions(Perms);
421 return Sec;
422 };
423
424 // Try to match the section order used by link.exe.
425 TextSec = CreateSection(".text", CODE | R | X);
426 CreateSection(".bss", BSS | R | W);
427 RdataSec = CreateSection(".rdata", DATA | R);
428 DataSec = CreateSection(".data", DATA | R | W);
429 CreateSection(".pdata", DATA | R);
430 IdataSec = CreateSection(".idata", DATA | R);
431 EdataSec = CreateSection(".edata", DATA | R);
432 DidatSec = CreateSection(".didat", DATA | R);
433 RsrcSec = CreateSection(".rsrc", DATA | R);
434 RelocSec = CreateSection(".reloc", DATA | DISCARDABLE | R);
435
436 // Then bin chunks by name.
437 std::map<StringRef, std::vector<Chunk *>> Map;
438 for (Chunk *C : Symtab->getChunks()) {
439 auto *SC = dyn_cast<SectionChunk>(C);
440 if (SC && !SC->isLive()) {
441 if (Config->Verbose)
442 SC->printDiscardedMessage();
443 continue;
444 }
445 Map[C->getSectionName()].push_back(C);
446 }
447
448 // Process an /order option.
449 if (!Config->Order.empty())
450 for (auto &Pair : Map)
451 sortBySectionOrder(Pair.second);
452
453 // Then create an OutputSection for each section.
454 // '$' and all following characters in input section names are
455 // discarded when determining output section. So, .text$foo
456 // contributes to .text, for example. See PE/COFF spec 3.2.
457 for (auto Pair : Map) {
458 StringRef Name = getOutputSectionName(Pair.first);
459 if (Name == ".pdata") {
460 if (!FirstPdata)
461 FirstPdata = Pair.second.front();
462 LastPdata = Pair.second.back();
463 }
464 OutputSection *Sec = CreateSection(Name, 0);
465 std::vector<Chunk *> &Chunks = Pair.second;
466 for (Chunk *C : Chunks) {
467 Sec->addChunk(C);
468 Sec->addPermissions(C->getPermissions());
469 }
470 }
471
472 // Finally, move some output sections to the end.
473 auto SectionOrder = [&](OutputSection *S) {
474 // .reloc should come last of all since it refers to RVAs of data in the
475 // previous sections.
476 if (S == RelocSec)
477 return 3;
478 // Move DISCARDABLE (or non-memory-mapped) sections to the end of file because
479 // the loader cannot handle holes.
480 if (S->getPermissions() & IMAGE_SCN_MEM_DISCARDABLE)
481 return 2;
482 // .rsrc should come at the end of the non-discardable sections because its
483 // size may change by the Win32 UpdateResources() function, causing
484 // subsequent sections to move (see https://crbug.com/827082).
485 if (S == RsrcSec)
486 return 1;
487 return 0;
488 };
489 std::stable_sort(OutputSections.begin(), OutputSections.end(),
490 [&](OutputSection *S, OutputSection *T) {
491 return SectionOrder(S) < SectionOrder(T);
492 });
493}
494
495void Writer::createMiscChunks() {
496 for (auto &P : MergeChunk::Instances)
497 RdataSec->addChunk(P.second);
498
499 // Create thunks for locally-dllimported symbols.
500 if (!Symtab->LocalImportChunks.empty()) {
501 for (Chunk *C : Symtab->LocalImportChunks)
502 RdataSec->addChunk(C);
503 }
504
505 // Create Debug Information Chunks
506 if (Config->Debug) {
507 DebugDirectory = make<DebugDirectoryChunk>(DebugRecords);
508
509 // Make a CVDebugRecordChunk even when /DEBUG:CV is not specified. We
510 // output a PDB no matter what, and this chunk provides the only means of
511 // allowing a debugger to match a PDB and an executable. So we need it even
512 // if we're ultimately not going to write CodeView data to the PDB.
513 auto *CVChunk = make<CVDebugRecordChunk>();
514 BuildId = CVChunk;
515 DebugRecords.push_back(CVChunk);
516
517 RdataSec->addChunk(DebugDirectory);
518 for (Chunk *C : DebugRecords)
519 RdataSec->addChunk(C);
520 }
521
522 // Create SEH table. x86-only.
523 if (Config->Machine == I386)
524 createSEHTable();
525
526 // Create /guard:cf tables if requested.
527 if (Config->GuardCF != GuardCFLevel::Off)
528 createGuardCFTables();
529}
530
531// Create .idata section for the DLL-imported symbol table.
532// The format of this section is inherently Windows-specific.
533// IdataContents class abstracted away the details for us,
534// so we just let it create chunks and add them to the section.
535void Writer::createImportTables() {
536 if (ImportFile::Instances.empty())
537 return;
538
539 // Initialize DLLOrder so that import entries are ordered in
540 // the same order as in the command line. (That affects DLL
541 // initialization order, and this ordering is MSVC-compatible.)
542 for (ImportFile *File : ImportFile::Instances) {
543 if (!File->Live)
544 continue;
545
546 std::string DLL = StringRef(File->DLLName).lower();
547 if (Config->DLLOrder.count(DLL) == 0)
548 Config->DLLOrder[DLL] = Config->DLLOrder.size();
549 }
550
551 for (ImportFile *File : ImportFile::Instances) {
552 if (!File->Live)
553 continue;
554
555 if (DefinedImportThunk *Thunk = File->ThunkSym)
556 TextSec->addChunk(Thunk->getChunk());
557
558 if (Config->DelayLoads.count(StringRef(File->DLLName).lower())) {
559 if (!File->ThunkSym)
560 fatal("cannot delay-load " + toString(File) +
561 " due to import of data: " + toString(*File->ImpSym));
562 DelayIdata.add(File->ImpSym);
563 } else {
564 Idata.add(File->ImpSym);
565 }
566 }
567
568 if (!Idata.empty())
569 for (Chunk *C : Idata.getChunks())
570 IdataSec->addChunk(C);
571
572 if (!DelayIdata.empty()) {
573 Defined *Helper = cast<Defined>(Config->DelayLoadHelper);
574 DelayIdata.create(Helper);
575 for (Chunk *C : DelayIdata.getChunks())
576 DidatSec->addChunk(C);
577 for (Chunk *C : DelayIdata.getDataChunks())
578 DataSec->addChunk(C);
579 for (Chunk *C : DelayIdata.getCodeChunks())
580 TextSec->addChunk(C);
581 }
582}
583
584void Writer::createExportTable() {
585 if (Config->Exports.empty())
586 return;
587 for (Chunk *C : Edata.Chunks)
588 EdataSec->addChunk(C);
589}
590
591// The Windows loader doesn't seem to like empty sections,
592// so we remove them if any.
593void Writer::removeEmptySections() {
594 auto IsEmpty = [](OutputSection *S) { return S->getVirtualSize() == 0; };
595 OutputSections.erase(
596 std::remove_if(OutputSections.begin(), OutputSections.end(), IsEmpty),
597 OutputSections.end());
598 uint32_t Idx = 1;
599 for (OutputSection *Sec : OutputSections)
600 Sec->SectionIndex = Idx++;
601}
602
603size_t Writer::addEntryToStringTable(StringRef Str) {
604 assert(Str.size() > COFF::NameSize)(static_cast <bool> (Str.size() > COFF::NameSize) ? void
(0) : __assert_fail ("Str.size() > COFF::NameSize", "/build/llvm-toolchain-snapshot-7~svn329677/tools/lld/COFF/Writer.cpp"
, 604, __extension__ __PRETTY_FUNCTION__))
;
605 size_t OffsetOfEntry = Strtab.size() + 4; // +4 for the size field
606 Strtab.insert(Strtab.end(), Str.begin(), Str.end());
607 Strtab.push_back('\0');
608 return OffsetOfEntry;
609}
610
611Optional<coff_symbol16> Writer::createSymbol(Defined *Def) {
612 // Relative symbols are unrepresentable in a COFF symbol table.
613 if (isa<DefinedSynthetic>(Def))
10
Taking false branch
614 return None;
615
616 // Don't write dead symbols or symbols in codeview sections to the symbol
617 // table.
618 if (!Def->isLive())
11
Assuming the condition is false
12
Taking false branch
619 return None;
620 if (auto *D = dyn_cast<DefinedRegular>(Def))
13
Taking false branch
621 if (D->getChunk()->isCodeView())
622 return None;
623
624 coff_symbol16 Sym;
625 StringRef Name = Def->getName();
626 if (Name.size() > COFF::NameSize) {
14
Assuming the condition is false
15
Taking false branch
627 Sym.Name.Offset.Zeroes = 0;
628 Sym.Name.Offset.Offset = addEntryToStringTable(Name);
629 } else {
630 memset(Sym.Name.ShortName, 0, COFF::NameSize);
631 memcpy(Sym.Name.ShortName, Name.data(), Name.size());
632 }
633
634 if (auto *D = dyn_cast<DefinedCOFF>(Def)) {
16
Taking false branch
635 COFFSymbolRef Ref = D->getCOFFSymbol();
636 Sym.Type = Ref.getType();
637 Sym.StorageClass = Ref.getStorageClass();
638 } else {
639 Sym.Type = IMAGE_SYM_TYPE_NULL;
640 Sym.StorageClass = IMAGE_SYM_CLASS_EXTERNAL;
641 }
642 Sym.NumberOfAuxSymbols = 0;
643
644 switch (Def->kind()) {
17
Control jumps to the 'default' case at line 649
645 case Symbol::DefinedAbsoluteKind:
646 Sym.Value = Def->getRVA();
647 Sym.SectionNumber = IMAGE_SYM_ABSOLUTE;
648 break;
649 default: {
650 uint64_t RVA = Def->getRVA();
651 OutputSection *Sec = nullptr;
18
'Sec' initialized to a null pointer value
652 for (OutputSection *S : OutputSections) {
653 if (S->getRVA() > RVA)
654 break;
655 Sec = S;
656 }
657 Sym.Value = RVA - Sec->getRVA();
19
Called C++ object pointer is null
658 Sym.SectionNumber = Sec->SectionIndex;
659 break;
660 }
661 }
662 return Sym;
663}
664
665void Writer::createSymbolAndStringTable() {
666 // PE/COFF images are limited to 8 byte section names. Longer names can be
667 // supported by writing a non-standard string table, but this string table is
668 // not mapped at runtime and the long names will therefore be inaccessible.
669 // link.exe always truncates section names to 8 bytes, whereas binutils always
670 // preserves long section names via the string table. LLD adopts a hybrid
671 // solution where discardable sections have long names preserved and
672 // non-discardable sections have their names truncated, to ensure that any
673 // section which is mapped at runtime also has its name mapped at runtime.
674 for (OutputSection *Sec : OutputSections) {
675 if (Sec->Name.size() <= COFF::NameSize)
676 continue;
677 if ((Sec->getPermissions() & IMAGE_SCN_MEM_DISCARDABLE) == 0)
678 continue;
679 Sec->setStringTableOff(addEntryToStringTable(Sec->Name));
680 }
681
682 if (Config->DebugDwarf) {
3
Assuming the condition is true
4
Taking true branch
683 for (ObjFile *File : ObjFile::Instances) {
684 for (Symbol *B : File->getSymbols()) {
5
Assuming '__begin3' is not equal to '__end3'
685 auto *D = dyn_cast_or_null<Defined>(B);
686 if (!D || D->WrittenToSymtab)
6
Assuming 'D' is non-null
7
Assuming the condition is false
8
Taking false branch
687 continue;
688 D->WrittenToSymtab = true;
689
690 if (Optional<coff_symbol16> Sym = createSymbol(D))
9
Calling 'Writer::createSymbol'
691 OutputSymtab.push_back(*Sym);
692 }
693 }
694 }
695
696 if (OutputSymtab.empty() && Strtab.empty())
697 return;
698
699 // We position the symbol table to be adjacent to the end of the last section.
700 uint64_t FileOff = FileSize;
701 PointerToSymbolTable = FileOff;
702 FileOff += OutputSymtab.size() * sizeof(coff_symbol16);
703 FileOff += 4 + Strtab.size();
704 FileSize = alignTo(FileOff, SectorSize);
705}
706
707// Visits all sections to assign incremental, non-overlapping RVAs and
708// file offsets.
709void Writer::assignAddresses() {
710 SizeOfHeaders = DOSStubSize + sizeof(PEMagic) + sizeof(coff_file_header) +
711 sizeof(data_directory) * NumberfOfDataDirectory +
712 sizeof(coff_section) * OutputSections.size();
713 SizeOfHeaders +=
714 Config->is64() ? sizeof(pe32plus_header) : sizeof(pe32_header);
715 SizeOfHeaders = alignTo(SizeOfHeaders, SectorSize);
716 uint64_t RVA = PageSize; // The first page is kept unmapped.
717 FileSize = SizeOfHeaders;
718
719 for (OutputSection *Sec : OutputSections) {
720 if (Sec == RelocSec)
721 addBaserels();
722 uint64_t RawSize = 0, VirtualSize = 0;
723 Sec->Header.VirtualAddress = RVA;
724 for (Chunk *C : Sec->getChunks()) {
725 VirtualSize = alignTo(VirtualSize, C->Alignment);
726 C->setRVA(RVA + VirtualSize);
727 C->OutputSectionOff = VirtualSize;
728 C->finalizeContents();
729 VirtualSize += C->getSize();
730 if (C->hasData())
731 RawSize = alignTo(VirtualSize, SectorSize);
732 }
733 if (VirtualSize > UINT32_MAX(4294967295U))
734 error("section larger than 4 GiB: " + Sec->Name);
735 Sec->Header.VirtualSize = VirtualSize;
736 Sec->Header.SizeOfRawData = RawSize;
737 if (RawSize != 0)
738 Sec->Header.PointerToRawData = FileSize;
739 RVA += alignTo(VirtualSize, PageSize);
740 FileSize += alignTo(RawSize, SectorSize);
741 }
742 SizeOfImage = alignTo(RVA, PageSize);
743}
744
745template <typename PEHeaderTy> void Writer::writeHeader() {
746 // Write DOS header. For backwards compatibility, the first part of a PE/COFF
747 // executable consists of an MS-DOS MZ executable. If the executable is run
748 // under DOS, that program gets run (usually to just print an error message).
749 // When run under Windows, the loader looks at AddressOfNewExeHeader and uses
750 // the PE header instead.
751 uint8_t *Buf = Buffer->getBufferStart();
752 auto *DOS = reinterpret_cast<dos_header *>(Buf);
753 Buf += sizeof(dos_header);
754 DOS->Magic[0] = 'M';
755 DOS->Magic[1] = 'Z';
756 DOS->UsedBytesInTheLastPage = DOSStubSize % 512;
757 DOS->FileSizeInPages = divideCeil(DOSStubSize, 512);
758 DOS->HeaderSizeInParagraphs = sizeof(dos_header) / 16;
759
760 DOS->AddressOfRelocationTable = sizeof(dos_header);
761 DOS->AddressOfNewExeHeader = DOSStubSize;
762
763 // Write DOS program.
764 memcpy(Buf, DOSProgram, sizeof(DOSProgram));
765 Buf += sizeof(DOSProgram);
766
767 // Write PE magic
768 memcpy(Buf, PEMagic, sizeof(PEMagic));
769 Buf += sizeof(PEMagic);
770
771 // Write COFF header
772 auto *COFF = reinterpret_cast<coff_file_header *>(Buf);
773 Buf += sizeof(*COFF);
774 COFF->Machine = Config->Machine;
775 COFF->NumberOfSections = OutputSections.size();
776 COFF->Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE;
777 if (Config->LargeAddressAware)
778 COFF->Characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
779 if (!Config->is64())
780 COFF->Characteristics |= IMAGE_FILE_32BIT_MACHINE;
781 if (Config->DLL)
782 COFF->Characteristics |= IMAGE_FILE_DLL;
783 if (!Config->Relocatable)
784 COFF->Characteristics |= IMAGE_FILE_RELOCS_STRIPPED;
785 COFF->SizeOfOptionalHeader =
786 sizeof(PEHeaderTy) + sizeof(data_directory) * NumberfOfDataDirectory;
787
788 // Write PE header
789 auto *PE = reinterpret_cast<PEHeaderTy *>(Buf);
790 Buf += sizeof(*PE);
791 PE->Magic = Config->is64() ? PE32Header::PE32_PLUS : PE32Header::PE32;
792
793 // If {Major,Minor}LinkerVersion is left at 0.0, then for some
794 // reason signing the resulting PE file with Authenticode produces a
795 // signature that fails to validate on Windows 7 (but is OK on 10).
796 // Set it to 14.0, which is what VS2015 outputs, and which avoids
797 // that problem.
798 PE->MajorLinkerVersion = 14;
799 PE->MinorLinkerVersion = 0;
800
801 PE->ImageBase = Config->ImageBase;
802 PE->SectionAlignment = PageSize;
803 PE->FileAlignment = SectorSize;
804 PE->MajorImageVersion = Config->MajorImageVersion;
805 PE->MinorImageVersion = Config->MinorImageVersion;
806 PE->MajorOperatingSystemVersion = Config->MajorOSVersion;
807 PE->MinorOperatingSystemVersion = Config->MinorOSVersion;
808 PE->MajorSubsystemVersion = Config->MajorOSVersion;
809 PE->MinorSubsystemVersion = Config->MinorOSVersion;
810 PE->Subsystem = Config->Subsystem;
811 PE->SizeOfImage = SizeOfImage;
812 PE->SizeOfHeaders = SizeOfHeaders;
813 if (!Config->NoEntry) {
814 Defined *Entry = cast<Defined>(Config->Entry);
815 PE->AddressOfEntryPoint = Entry->getRVA();
816 // Pointer to thumb code must have the LSB set, so adjust it.
817 if (Config->Machine == ARMNT)
818 PE->AddressOfEntryPoint |= 1;
819 }
820 PE->SizeOfStackReserve = Config->StackReserve;
821 PE->SizeOfStackCommit = Config->StackCommit;
822 PE->SizeOfHeapReserve = Config->HeapReserve;
823 PE->SizeOfHeapCommit = Config->HeapCommit;
824 if (Config->AppContainer)
825 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_APPCONTAINER;
826 if (Config->DynamicBase)
827 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE;
828 if (Config->HighEntropyVA)
829 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA;
830 if (!Config->AllowBind)
831 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_BIND;
832 if (Config->NxCompat)
833 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NX_COMPAT;
834 if (!Config->AllowIsolation)
835 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION;
836 if (Config->GuardCF != GuardCFLevel::Off)
837 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_GUARD_CF;
838 if (Config->Machine == I386 && !SEHTable &&
839 !Symtab->findUnderscore("_load_config_used"))
840 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_SEH;
841 if (Config->TerminalServerAware)
842 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE;
843 PE->NumberOfRvaAndSize = NumberfOfDataDirectory;
844 if (TextSec->getVirtualSize()) {
845 PE->BaseOfCode = TextSec->getRVA();
846 PE->SizeOfCode = TextSec->getRawSize();
847 }
848 PE->SizeOfInitializedData = getSizeOfInitializedData();
849
850 // Write data directory
851 auto *Dir = reinterpret_cast<data_directory *>(Buf);
852 Buf += sizeof(*Dir) * NumberfOfDataDirectory;
853 if (!Config->Exports.empty()) {
854 Dir[EXPORT_TABLE].RelativeVirtualAddress = Edata.getRVA();
855 Dir[EXPORT_TABLE].Size = Edata.getSize();
856 }
857 if (!Idata.empty()) {
858 Dir[IMPORT_TABLE].RelativeVirtualAddress = Idata.getDirRVA();
859 Dir[IMPORT_TABLE].Size = Idata.getDirSize();
860 Dir[IAT].RelativeVirtualAddress = Idata.getIATRVA();
861 Dir[IAT].Size = Idata.getIATSize();
862 }
863 if (RsrcSec->getVirtualSize()) {
864 Dir[RESOURCE_TABLE].RelativeVirtualAddress = RsrcSec->getRVA();
865 Dir[RESOURCE_TABLE].Size = RsrcSec->getVirtualSize();
866 }
867 if (FirstPdata) {
868 Dir[EXCEPTION_TABLE].RelativeVirtualAddress = FirstPdata->getRVA();
869 Dir[EXCEPTION_TABLE].Size =
870 LastPdata->getRVA() + LastPdata->getSize() - FirstPdata->getRVA();
871 }
872 if (RelocSec->getVirtualSize()) {
873 Dir[BASE_RELOCATION_TABLE].RelativeVirtualAddress = RelocSec->getRVA();
874 Dir[BASE_RELOCATION_TABLE].Size = RelocSec->getVirtualSize();
875 }
876 if (Symbol *Sym = Symtab->findUnderscore("_tls_used")) {
877 if (Defined *B = dyn_cast<Defined>(Sym)) {
878 Dir[TLS_TABLE].RelativeVirtualAddress = B->getRVA();
879 Dir[TLS_TABLE].Size = Config->is64()
880 ? sizeof(object::coff_tls_directory64)
881 : sizeof(object::coff_tls_directory32);
882 }
883 }
884 if (Config->Debug) {
885 Dir[DEBUG_DIRECTORY].RelativeVirtualAddress = DebugDirectory->getRVA();
886 Dir[DEBUG_DIRECTORY].Size = DebugDirectory->getSize();
887 }
888 if (Symbol *Sym = Symtab->findUnderscore("_load_config_used")) {
889 if (auto *B = dyn_cast<DefinedRegular>(Sym)) {
890 SectionChunk *SC = B->getChunk();
891 assert(B->getRVA() >= SC->getRVA())(static_cast <bool> (B->getRVA() >= SC->getRVA
()) ? void (0) : __assert_fail ("B->getRVA() >= SC->getRVA()"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/lld/COFF/Writer.cpp"
, 891, __extension__ __PRETTY_FUNCTION__))
;
892 uint64_t OffsetInChunk = B->getRVA() - SC->getRVA();
893 if (!SC->hasData() || OffsetInChunk + 4 > SC->getSize())
894 fatal("_load_config_used is malformed");
895
896 ArrayRef<uint8_t> SecContents = SC->getContents();
897 uint32_t LoadConfigSize =
898 *reinterpret_cast<const ulittle32_t *>(&SecContents[OffsetInChunk]);
899 if (OffsetInChunk + LoadConfigSize > SC->getSize())
900 fatal("_load_config_used is too large");
901 Dir[LOAD_CONFIG_TABLE].RelativeVirtualAddress = B->getRVA();
902 Dir[LOAD_CONFIG_TABLE].Size = LoadConfigSize;
903 }
904 }
905 if (!DelayIdata.empty()) {
906 Dir[DELAY_IMPORT_DESCRIPTOR].RelativeVirtualAddress =
907 DelayIdata.getDirRVA();
908 Dir[DELAY_IMPORT_DESCRIPTOR].Size = DelayIdata.getDirSize();
909 }
910
911 // Write section table
912 for (OutputSection *Sec : OutputSections) {
913 Sec->writeHeaderTo(Buf);
914 Buf += sizeof(coff_section);
915 }
916 SectionTable = ArrayRef<uint8_t>(
917 Buf - OutputSections.size() * sizeof(coff_section), Buf);
918
919 if (OutputSymtab.empty() && Strtab.empty())
920 return;
921
922 COFF->PointerToSymbolTable = PointerToSymbolTable;
923 uint32_t NumberOfSymbols = OutputSymtab.size();
924 COFF->NumberOfSymbols = NumberOfSymbols;
925 auto *SymbolTable = reinterpret_cast<coff_symbol16 *>(
926 Buffer->getBufferStart() + COFF->PointerToSymbolTable);
927 for (size_t I = 0; I != NumberOfSymbols; ++I)
928 SymbolTable[I] = OutputSymtab[I];
929 // Create the string table, it follows immediately after the symbol table.
930 // The first 4 bytes is length including itself.
931 Buf = reinterpret_cast<uint8_t *>(&SymbolTable[NumberOfSymbols]);
932 write32le(Buf, Strtab.size() + 4);
933 if (!Strtab.empty())
934 memcpy(Buf + 4, Strtab.data(), Strtab.size());
935}
936
937void Writer::openFile(StringRef Path) {
938 Buffer = CHECK(check2(FileOutputBuffer::create(Path, FileSize, FileOutputBuffer
::F_executable), [&] { return toString("failed to open " +
Path); })
939 FileOutputBuffer::create(Path, FileSize, FileOutputBuffer::F_executable),check2(FileOutputBuffer::create(Path, FileSize, FileOutputBuffer
::F_executable), [&] { return toString("failed to open " +
Path); })
940 "failed to open " + Path)check2(FileOutputBuffer::create(Path, FileSize, FileOutputBuffer
::F_executable), [&] { return toString("failed to open " +
Path); })
;
941}
942
943void Writer::createSEHTable() {
944 SymbolRVASet Handlers;
945 for (ObjFile *File : ObjFile::Instances) {
946 // FIXME: We should error here instead of earlier unless /safeseh:no was
947 // passed.
948 if (!File->hasSafeSEH())
949 return;
950
951 markSymbolsForRVATable(File, File->getSXDataChunks(), Handlers);
952 }
953
954 maybeAddRVATable(std::move(Handlers), "__safe_se_handler_table",
955 "__safe_se_handler_count");
956}
957
958// Add a symbol to an RVA set. Two symbols may have the same RVA, but an RVA set
959// cannot contain duplicates. Therefore, the set is uniqued by Chunk and the
960// symbol's offset into that Chunk.
961static void addSymbolToRVASet(SymbolRVASet &RVASet, Defined *S) {
962 Chunk *C = S->getChunk();
963 if (auto *SC = dyn_cast<SectionChunk>(C))
964 C = SC->Repl; // Look through ICF replacement.
965 uint32_t Off = S->getRVA() - (C ? C->getRVA() : 0);
966 RVASet.insert({C, Off});
967}
968
969// Visit all relocations from all section contributions of this object file and
970// mark the relocation target as address-taken.
971static void markSymbolsWithRelocations(ObjFile *File,
972 SymbolRVASet &UsedSymbols) {
973 for (Chunk *C : File->getChunks()) {
974 // We only care about live section chunks. Common chunks and other chunks
975 // don't generally contain relocations.
976 SectionChunk *SC = dyn_cast<SectionChunk>(C);
977 if (!SC || !SC->isLive())
978 continue;
979
980 // Look for relocations in this section against symbols in executable output
981 // sections.
982 for (Symbol *Ref : SC->symbols()) {
983 // FIXME: Do further testing to see if the relocation type matters,
984 // especially for 32-bit where taking the address of something usually
985 // uses an absolute relocation instead of a relative one.
986 if (auto *D = dyn_cast_or_null<Defined>(Ref)) {
987 Chunk *RefChunk = D->getChunk();
988 OutputSection *OS = RefChunk ? RefChunk->getOutputSection() : nullptr;
989 if (OS && OS->getPermissions() & IMAGE_SCN_MEM_EXECUTE)
990 addSymbolToRVASet(UsedSymbols, D);
991 }
992 }
993 }
994}
995
996// Create the guard function id table. This is a table of RVAs of all
997// address-taken functions. It is sorted and uniqued, just like the safe SEH
998// table.
999void Writer::createGuardCFTables() {
1000 SymbolRVASet AddressTakenSyms;
1001 SymbolRVASet LongJmpTargets;
1002 for (ObjFile *File : ObjFile::Instances) {
1003 // If the object was compiled with /guard:cf, the address taken symbols
1004 // are in .gfids$y sections, and the longjmp targets are in .gljmp$y
1005 // sections. If the object was not compiled with /guard:cf, we assume there
1006 // were no setjmp targets, and that all code symbols with relocations are
1007 // possibly address-taken.
1008 if (File->hasGuardCF()) {
1009 markSymbolsForRVATable(File, File->getGuardFidChunks(), AddressTakenSyms);
1010 markSymbolsForRVATable(File, File->getGuardLJmpChunks(), LongJmpTargets);
1011 } else {
1012 markSymbolsWithRelocations(File, AddressTakenSyms);
1013 }
1014 }
1015
1016 // Mark the image entry as address-taken.
1017 if (Config->Entry)
1018 addSymbolToRVASet(AddressTakenSyms, cast<Defined>(Config->Entry));
1019
1020 maybeAddRVATable(std::move(AddressTakenSyms), "__guard_fids_table",
1021 "__guard_fids_count");
1022
1023 // Add the longjmp target table unless the user told us not to.
1024 if (Config->GuardCF == GuardCFLevel::Full)
1025 maybeAddRVATable(std::move(LongJmpTargets), "__guard_longjmp_table",
1026 "__guard_longjmp_count");
1027
1028 // Set __guard_flags, which will be used in the load config to indicate that
1029 // /guard:cf was enabled.
1030 uint32_t GuardFlags = uint32_t(coff_guard_flags::CFInstrumented) |
1031 uint32_t(coff_guard_flags::HasFidTable);
1032 if (Config->GuardCF == GuardCFLevel::Full)
1033 GuardFlags |= uint32_t(coff_guard_flags::HasLongJmpTable);
1034 Symbol *FlagSym = Symtab->findUnderscore("__guard_flags");
1035 cast<DefinedAbsolute>(FlagSym)->setVA(GuardFlags);
1036}
1037
1038// Take a list of input sections containing symbol table indices and add those
1039// symbols to an RVA table. The challenge is that symbol RVAs are not known and
1040// depend on the table size, so we can't directly build a set of integers.
1041void Writer::markSymbolsForRVATable(ObjFile *File,
1042 ArrayRef<SectionChunk *> SymIdxChunks,
1043 SymbolRVASet &TableSymbols) {
1044 for (SectionChunk *C : SymIdxChunks) {
1045 // Skip sections discarded by linker GC. This comes up when a .gfids section
1046 // is associated with something like a vtable and the vtable is discarded.
1047 // In this case, the associated gfids section is discarded, and we don't
1048 // mark the virtual member functions as address-taken by the vtable.
1049 if (!C->isLive())
1050 continue;
1051
1052 // Validate that the contents look like symbol table indices.
1053 ArrayRef<uint8_t> Data = C->getContents();
1054 if (Data.size() % 4 != 0) {
1055 warn("ignoring " + C->getSectionName() +
1056 " symbol table index section in object " + toString(File));
1057 continue;
1058 }
1059
1060 // Read each symbol table index and check if that symbol was included in the
1061 // final link. If so, add it to the table symbol set.
1062 ArrayRef<ulittle32_t> SymIndices(
1063 reinterpret_cast<const ulittle32_t *>(Data.data()), Data.size() / 4);
1064 ArrayRef<Symbol *> ObjSymbols = File->getSymbols();
1065 for (uint32_t SymIndex : SymIndices) {
1066 if (SymIndex >= ObjSymbols.size()) {
1067 warn("ignoring invalid symbol table index in section " +
1068 C->getSectionName() + " in object " + toString(File));
1069 continue;
1070 }
1071 if (Symbol *S = ObjSymbols[SymIndex]) {
1072 if (S->isLive())
1073 addSymbolToRVASet(TableSymbols, cast<Defined>(S));
1074 }
1075 }
1076 }
1077}
1078
1079// Replace the absolute table symbol with a synthetic symbol pointing to
1080// TableChunk so that we can emit base relocations for it and resolve section
1081// relative relocations.
1082void Writer::maybeAddRVATable(SymbolRVASet TableSymbols, StringRef TableSym,
1083 StringRef CountSym) {
1084 if (TableSymbols.empty())
1085 return;
1086
1087 RVATableChunk *TableChunk = make<RVATableChunk>(std::move(TableSymbols));
1088 RdataSec->addChunk(TableChunk);
1089
1090 Symbol *T = Symtab->findUnderscore(TableSym);
1091 Symbol *C = Symtab->findUnderscore(CountSym);
1092 replaceSymbol<DefinedSynthetic>(T, T->getName(), TableChunk);
1093 cast<DefinedAbsolute>(C)->setVA(TableChunk->getSize() / 4);
1094}
1095
1096// Handles /section options to allow users to overwrite
1097// section attributes.
1098void Writer::setSectionPermissions() {
1099 for (auto &P : Config->Section) {
1100 StringRef Name = P.first;
1101 uint32_t Perm = P.second;
1102 if (auto *Sec = findSection(Name))
1103 Sec->setPermissions(Perm);
1104 }
1105}
1106
1107// Write section contents to a mmap'ed file.
1108void Writer::writeSections() {
1109 // Record the number of sections to apply section index relocations
1110 // against absolute symbols. See applySecIdx in Chunks.cpp..
1111 DefinedAbsolute::NumOutputSections = OutputSections.size();
1112
1113 uint8_t *Buf = Buffer->getBufferStart();
1114 for (OutputSection *Sec : OutputSections) {
1115 uint8_t *SecBuf = Buf + Sec->getFileOff();
1116 // Fill gaps between functions in .text with INT3 instructions
1117 // instead of leaving as NUL bytes (which can be interpreted as
1118 // ADD instructions).
1119 if (Sec->getPermissions() & IMAGE_SCN_CNT_CODE)
1120 memset(SecBuf, 0xCC, Sec->getRawSize());
1121 for_each(parallel::par, Sec->getChunks().begin(), Sec->getChunks().end(),
1122 [&](Chunk *C) { C->writeTo(SecBuf); });
1123 }
1124}
1125
1126void Writer::writeBuildId() {
1127 // There are two important parts to the build ID.
1128 // 1) If building with debug info, the COFF debug directory contains a
1129 // timestamp as well as a Guid and Age of the PDB.
1130 // 2) In all cases, the PE COFF file header also contains a timestamp.
1131 // For reproducibility, instead of a timestamp we want to use a hash of the
1132 // binary, however when building with debug info the hash needs to take into
1133 // account the debug info, since it's possible to add blank lines to a file
1134 // which causes the debug info to change but not the generated code.
1135 //
1136 // To handle this, we first set the Guid and Age in the debug directory (but
1137 // only if we're doing a debug build). Then, we hash the binary (thus causing
1138 // the hash to change if only the debug info changes, since the Age will be
1139 // different). Finally, we write that hash into the debug directory (if
1140 // present) as well as the COFF file header (always).
1141 if (Config->Debug) {
1142 assert(BuildId && "BuildId is not set!")(static_cast <bool> (BuildId && "BuildId is not set!"
) ? void (0) : __assert_fail ("BuildId && \"BuildId is not set!\""
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/lld/COFF/Writer.cpp"
, 1142, __extension__ __PRETTY_FUNCTION__))
;
1143 if (PreviousBuildId.hasValue()) {
1144 *BuildId->BuildId = *PreviousBuildId;
1145 BuildId->BuildId->PDB70.Age = BuildId->BuildId->PDB70.Age + 1;
1146 } else {
1147 BuildId->BuildId->Signature.CVSignature = OMF::Signature::PDB70;
1148 BuildId->BuildId->PDB70.Age = 1;
1149 llvm::getRandomBytes(BuildId->BuildId->PDB70.Signature, 16);
1150 }
1151 }
1152
1153 // At this point the only fields in the COFF file which remain unset are the
1154 // "timestamp" in the COFF file header, and the ones in the coff debug
1155 // directory. Now we can hash the file and write that hash to the various
1156 // timestamp fields in the file.
1157 StringRef OutputFileData(
1158 reinterpret_cast<const char *>(Buffer->getBufferStart()),
1159 Buffer->getBufferSize());
1160
1161 uint32_t Hash = static_cast<uint32_t>(xxHash64(OutputFileData));
1162
1163 if (DebugDirectory)
1164 DebugDirectory->setTimeDateStamp(Hash);
1165
1166 uint8_t *Buf = Buffer->getBufferStart();
1167 Buf += DOSStubSize + sizeof(PEMagic);
1168 object::coff_file_header *CoffHeader =
1169 reinterpret_cast<coff_file_header *>(Buf);
1170 CoffHeader->TimeDateStamp = Hash;
1171}
1172
1173// Sort .pdata section contents according to PE/COFF spec 5.5.
1174void Writer::sortExceptionTable() {
1175 if (!FirstPdata)
1176 return;
1177 // We assume .pdata contains function table entries only.
1178 auto BufAddr = [&](Chunk *C) {
1179 return Buffer->getBufferStart() + C->getOutputSection()->getFileOff() +
1180 C->getRVA() - C->getOutputSection()->getRVA();
1181 };
1182 uint8_t *Begin = BufAddr(FirstPdata);
1183 uint8_t *End = BufAddr(LastPdata) + LastPdata->getSize();
1184 if (Config->Machine == AMD64) {
1185 struct Entry { ulittle32_t Begin, End, Unwind; };
1186 sort(parallel::par, (Entry *)Begin, (Entry *)End,
1187 [](const Entry &A, const Entry &B) { return A.Begin < B.Begin; });
1188 return;
1189 }
1190 if (Config->Machine == ARMNT || Config->Machine == ARM64) {
1191 struct Entry { ulittle32_t Begin, Unwind; };
1192 sort(parallel::par, (Entry *)Begin, (Entry *)End,
1193 [](const Entry &A, const Entry &B) { return A.Begin < B.Begin; });
1194 return;
1195 }
1196 errs() << "warning: don't know how to handle .pdata.\n";
1197}
1198
1199OutputSection *Writer::findSection(StringRef Name) {
1200 for (OutputSection *Sec : OutputSections)
1201 if (Sec->Name == Name)
1202 return Sec;
1203 return nullptr;
1204}
1205
1206uint32_t Writer::getSizeOfInitializedData() {
1207 uint32_t Res = 0;
1208 for (OutputSection *S : OutputSections)
1209 if (S->getPermissions() & IMAGE_SCN_CNT_INITIALIZED_DATA)
1210 Res += S->getRawSize();
1211 return Res;
1212}
1213
1214// Add base relocations to .reloc section.
1215void Writer::addBaserels() {
1216 if (!Config->Relocatable)
1217 return;
1218 std::vector<Baserel> V;
1219 for (OutputSection *Sec : OutputSections) {
1220 if (Sec == RelocSec)
1221 continue;
1222 // Collect all locations for base relocations.
1223 for (Chunk *C : Sec->getChunks())
1224 C->getBaserels(&V);
1225 // Add the addresses to .reloc section.
1226 if (!V.empty())
1227 addBaserelBlocks(V);
1228 V.clear();
1229 }
1230}
1231
1232// Add addresses to .reloc section. Note that addresses are grouped by page.
1233void Writer::addBaserelBlocks(std::vector<Baserel> &V) {
1234 const uint32_t Mask = ~uint32_t(PageSize - 1);
1235 uint32_t Page = V[0].RVA & Mask;
1236 size_t I = 0, J = 1;
1237 for (size_t E = V.size(); J < E; ++J) {
1238 uint32_t P = V[J].RVA & Mask;
1239 if (P == Page)
1240 continue;
1241 RelocSec->addChunk(make<BaserelChunk>(Page, &V[I], &V[0] + J));
1242 I = J;
1243 Page = P;
1244 }
1245 if (I == J)
1246 return;
1247 RelocSec->addChunk(make<BaserelChunk>(Page, &V[I], &V[0] + J));
1248}