Writer.cpp 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945
  1. //===- Writer.cpp ---------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "Writer.h"
  9. #include "Config.h"
  10. #include "DLL.h"
  11. #include "InputFiles.h"
  12. #include "MapFile.h"
  13. #include "PDB.h"
  14. #include "SymbolTable.h"
  15. #include "Symbols.h"
  16. #include "lld/Common/ErrorHandler.h"
  17. #include "lld/Common/Memory.h"
  18. #include "lld/Common/Threads.h"
  19. #include "lld/Common/Timer.h"
  20. #include "llvm/ADT/DenseMap.h"
  21. #include "llvm/ADT/STLExtras.h"
  22. #include "llvm/ADT/StringSwitch.h"
  23. #include "llvm/Support/BinaryStreamReader.h"
  24. #include "llvm/Support/Debug.h"
  25. #include "llvm/Support/Endian.h"
  26. #include "llvm/Support/FileOutputBuffer.h"
  27. #include "llvm/Support/Parallel.h"
  28. #include "llvm/Support/Path.h"
  29. #include "llvm/Support/RandomNumberGenerator.h"
  30. #include "llvm/Support/xxhash.h"
  31. #include <algorithm>
  32. #include <cstdio>
  33. #include <map>
  34. #include <memory>
  35. #include <utility>
  36. using namespace llvm;
  37. using namespace llvm::COFF;
  38. using namespace llvm::object;
  39. using namespace llvm::support;
  40. using namespace llvm::support::endian;
  41. namespace lld {
  42. namespace coff {
  43. /* To re-generate DOSProgram:
  44. $ cat > /tmp/DOSProgram.asm
  45. org 0
  46. ; Copy cs to ds.
  47. push cs
  48. pop ds
  49. ; Point ds:dx at the $-terminated string.
  50. mov dx, str
  51. ; Int 21/AH=09h: Write string to standard output.
  52. mov ah, 0x9
  53. int 0x21
  54. ; Int 21/AH=4Ch: Exit with return code (in AL).
  55. mov ax, 0x4C01
  56. int 0x21
  57. str:
  58. db 'This program cannot be run in DOS mode.$'
  59. align 8, db 0
  60. $ nasm -fbin /tmp/DOSProgram.asm -o /tmp/DOSProgram.bin
  61. $ xxd -i /tmp/DOSProgram.bin
  62. */
  63. static unsigned char dosProgram[] = {
  64. 0x0e, 0x1f, 0xba, 0x0e, 0x00, 0xb4, 0x09, 0xcd, 0x21, 0xb8, 0x01, 0x4c,
  65. 0xcd, 0x21, 0x54, 0x68, 0x69, 0x73, 0x20, 0x70, 0x72, 0x6f, 0x67, 0x72,
  66. 0x61, 0x6d, 0x20, 0x63, 0x61, 0x6e, 0x6e, 0x6f, 0x74, 0x20, 0x62, 0x65,
  67. 0x20, 0x72, 0x75, 0x6e, 0x20, 0x69, 0x6e, 0x20, 0x44, 0x4f, 0x53, 0x20,
  68. 0x6d, 0x6f, 0x64, 0x65, 0x2e, 0x24, 0x00, 0x00
  69. };
  70. static_assert(sizeof(dosProgram) % 8 == 0,
  71. "DOSProgram size must be multiple of 8");
  72. static const int dosStubSize = sizeof(dos_header) + sizeof(dosProgram);
  73. static_assert(dosStubSize % 8 == 0, "DOSStub size must be multiple of 8");
  74. static const int numberOfDataDirectory = 16;
  75. // Global vector of all output sections. After output sections are finalized,
  76. // this can be indexed by Chunk::getOutputSection.
  77. static std::vector<OutputSection *> outputSections;
  78. OutputSection *Chunk::getOutputSection() const {
  79. return osidx == 0 ? nullptr : outputSections[osidx - 1];
  80. }
  81. namespace {
  82. class DebugDirectoryChunk : public NonSectionChunk {
  83. public:
  84. DebugDirectoryChunk(const std::vector<Chunk *> &r, bool writeRepro)
  85. : records(r), writeRepro(writeRepro) {}
  86. size_t getSize() const override {
  87. return (records.size() + int(writeRepro)) * sizeof(debug_directory);
  88. }
  89. void writeTo(uint8_t *b) const override {
  90. auto *d = reinterpret_cast<debug_directory *>(b);
  91. for (const Chunk *record : records) {
  92. OutputSection *os = record->getOutputSection();
  93. uint64_t offs = os->getFileOff() + (record->getRVA() - os->getRVA());
  94. fillEntry(d, COFF::IMAGE_DEBUG_TYPE_CODEVIEW, record->getSize(),
  95. record->getRVA(), offs);
  96. ++d;
  97. }
  98. if (writeRepro) {
  99. // FIXME: The COFF spec allows either a 0-sized entry to just say
  100. // "the timestamp field is really a hash", or a 4-byte size field
  101. // followed by that many bytes containing a longer hash (with the
  102. // lowest 4 bytes usually being the timestamp in little-endian order).
  103. // Consider storing the full 8 bytes computed by xxHash64 here.
  104. fillEntry(d, COFF::IMAGE_DEBUG_TYPE_REPRO, 0, 0, 0);
  105. }
  106. }
  107. void setTimeDateStamp(uint32_t timeDateStamp) {
  108. for (support::ulittle32_t *tds : timeDateStamps)
  109. *tds = timeDateStamp;
  110. }
  111. private:
  112. void fillEntry(debug_directory *d, COFF::DebugType debugType, size_t size,
  113. uint64_t rva, uint64_t offs) const {
  114. d->Characteristics = 0;
  115. d->TimeDateStamp = 0;
  116. d->MajorVersion = 0;
  117. d->MinorVersion = 0;
  118. d->Type = debugType;
  119. d->SizeOfData = size;
  120. d->AddressOfRawData = rva;
  121. d->PointerToRawData = offs;
  122. timeDateStamps.push_back(&d->TimeDateStamp);
  123. }
  124. mutable std::vector<support::ulittle32_t *> timeDateStamps;
  125. const std::vector<Chunk *> &records;
  126. bool writeRepro;
  127. };
  128. class CVDebugRecordChunk : public NonSectionChunk {
  129. public:
  130. size_t getSize() const override {
  131. return sizeof(codeview::DebugInfo) + config->pdbAltPath.size() + 1;
  132. }
  133. void writeTo(uint8_t *b) const override {
  134. // Save off the DebugInfo entry to backfill the file signature (build id)
  135. // in Writer::writeBuildId
  136. buildId = reinterpret_cast<codeview::DebugInfo *>(b);
  137. // variable sized field (PDB Path)
  138. char *p = reinterpret_cast<char *>(b + sizeof(*buildId));
  139. if (!config->pdbAltPath.empty())
  140. memcpy(p, config->pdbAltPath.data(), config->pdbAltPath.size());
  141. p[config->pdbAltPath.size()] = '\0';
  142. }
  143. mutable codeview::DebugInfo *buildId = nullptr;
  144. };
  145. // PartialSection represents a group of chunks that contribute to an
  146. // OutputSection. Collating a collection of PartialSections of same name and
  147. // characteristics constitutes the OutputSection.
  148. class PartialSectionKey {
  149. public:
  150. StringRef name;
  151. unsigned characteristics;
  152. bool operator<(const PartialSectionKey &other) const {
  153. int c = name.compare(other.name);
  154. if (c == 1)
  155. return false;
  156. if (c == 0)
  157. return characteristics < other.characteristics;
  158. return true;
  159. }
  160. };
  161. // The writer writes a SymbolTable result to a file.
  162. class Writer {
  163. public:
  164. Writer() : buffer(errorHandler().outputBuffer) {}
  165. void run();
  166. private:
  167. void createSections();
  168. void createMiscChunks();
  169. void createImportTables();
  170. void appendImportThunks();
  171. void locateImportTables();
  172. void createExportTable();
  173. void mergeSections();
  174. void removeUnusedSections();
  175. void assignAddresses();
  176. void finalizeAddresses();
  177. void removeEmptySections();
  178. void assignOutputSectionIndices();
  179. void createSymbolAndStringTable();
  180. void openFile(StringRef outputPath);
  181. template <typename PEHeaderTy> void writeHeader();
  182. void createSEHTable();
  183. void createRuntimePseudoRelocs();
  184. void insertCtorDtorSymbols();
  185. void createGuardCFTables();
  186. void markSymbolsForRVATable(ObjFile *file,
  187. ArrayRef<SectionChunk *> symIdxChunks,
  188. SymbolRVASet &tableSymbols);
  189. void maybeAddRVATable(SymbolRVASet tableSymbols, StringRef tableSym,
  190. StringRef countSym);
  191. void setSectionPermissions();
  192. void writeSections();
  193. void writeBuildId();
  194. void sortExceptionTable();
  195. void sortCRTSectionChunks(std::vector<Chunk *> &chunks);
  196. void addSyntheticIdata();
  197. void fixPartialSectionChars(StringRef name, uint32_t chars);
  198. bool fixGnuImportChunks();
  199. PartialSection *createPartialSection(StringRef name, uint32_t outChars);
  200. PartialSection *findPartialSection(StringRef name, uint32_t outChars);
  201. llvm::Optional<coff_symbol16> createSymbol(Defined *d);
  202. size_t addEntryToStringTable(StringRef str);
  203. OutputSection *findSection(StringRef name);
  204. void addBaserels();
  205. void addBaserelBlocks(std::vector<Baserel> &v);
  206. uint32_t getSizeOfInitializedData();
  207. std::unique_ptr<FileOutputBuffer> &buffer;
  208. std::map<PartialSectionKey, PartialSection *> partialSections;
  209. std::vector<char> strtab;
  210. std::vector<llvm::object::coff_symbol16> outputSymtab;
  211. IdataContents idata;
  212. Chunk *importTableStart = nullptr;
  213. uint64_t importTableSize = 0;
  214. Chunk *edataStart = nullptr;
  215. Chunk *edataEnd = nullptr;
  216. Chunk *iatStart = nullptr;
  217. uint64_t iatSize = 0;
  218. DelayLoadContents delayIdata;
  219. EdataContents edata;
  220. bool setNoSEHCharacteristic = false;
  221. DebugDirectoryChunk *debugDirectory = nullptr;
  222. std::vector<Chunk *> debugRecords;
  223. CVDebugRecordChunk *buildId = nullptr;
  224. ArrayRef<uint8_t> sectionTable;
  225. uint64_t fileSize;
  226. uint32_t pointerToSymbolTable = 0;
  227. uint64_t sizeOfImage;
  228. uint64_t sizeOfHeaders;
  229. OutputSection *textSec;
  230. OutputSection *rdataSec;
  231. OutputSection *buildidSec;
  232. OutputSection *dataSec;
  233. OutputSection *pdataSec;
  234. OutputSection *idataSec;
  235. OutputSection *edataSec;
  236. OutputSection *didatSec;
  237. OutputSection *rsrcSec;
  238. OutputSection *relocSec;
  239. OutputSection *ctorsSec;
  240. OutputSection *dtorsSec;
  241. // The first and last .pdata sections in the output file.
  242. //
  243. // We need to keep track of the location of .pdata in whichever section it
  244. // gets merged into so that we can sort its contents and emit a correct data
  245. // directory entry for the exception table. This is also the case for some
  246. // other sections (such as .edata) but because the contents of those sections
  247. // are entirely linker-generated we can keep track of their locations using
  248. // the chunks that the linker creates. All .pdata chunks come from input
  249. // files, so we need to keep track of them separately.
  250. Chunk *firstPdata = nullptr;
  251. Chunk *lastPdata;
  252. };
  253. } // anonymous namespace
  254. static Timer codeLayoutTimer("Code Layout", Timer::root());
  255. static Timer diskCommitTimer("Commit Output File", Timer::root());
  256. void writeResult() { Writer().run(); }
  257. void OutputSection::addChunk(Chunk *c) {
  258. chunks.push_back(c);
  259. }
  260. void OutputSection::insertChunkAtStart(Chunk *c) {
  261. chunks.insert(chunks.begin(), c);
  262. }
  263. void OutputSection::setPermissions(uint32_t c) {
  264. header.Characteristics &= ~permMask;
  265. header.Characteristics |= c;
  266. }
  267. void OutputSection::merge(OutputSection *other) {
  268. chunks.insert(chunks.end(), other->chunks.begin(), other->chunks.end());
  269. other->chunks.clear();
  270. contribSections.insert(contribSections.end(), other->contribSections.begin(),
  271. other->contribSections.end());
  272. other->contribSections.clear();
  273. }
  274. // Write the section header to a given buffer.
  275. void OutputSection::writeHeaderTo(uint8_t *buf) {
  276. auto *hdr = reinterpret_cast<coff_section *>(buf);
  277. *hdr = header;
  278. if (stringTableOff) {
  279. // If name is too long, write offset into the string table as a name.
  280. sprintf(hdr->Name, "/%d", stringTableOff);
  281. } else {
  282. assert(!config->debug || name.size() <= COFF::NameSize ||
  283. (hdr->Characteristics & IMAGE_SCN_MEM_DISCARDABLE) == 0);
  284. strncpy(hdr->Name, name.data(),
  285. std::min(name.size(), (size_t)COFF::NameSize));
  286. }
  287. }
  288. void OutputSection::addContributingPartialSection(PartialSection *sec) {
  289. contribSections.push_back(sec);
  290. }
  291. // Check whether the target address S is in range from a relocation
  292. // of type relType at address P.
  293. static bool isInRange(uint16_t relType, uint64_t s, uint64_t p, int margin) {
  294. if (config->machine == ARMNT) {
  295. int64_t diff = AbsoluteDifference(s, p + 4) + margin;
  296. switch (relType) {
  297. case IMAGE_REL_ARM_BRANCH20T:
  298. return isInt<21>(diff);
  299. case IMAGE_REL_ARM_BRANCH24T:
  300. case IMAGE_REL_ARM_BLX23T:
  301. return isInt<25>(diff);
  302. default:
  303. return true;
  304. }
  305. } else if (config->machine == ARM64) {
  306. int64_t diff = AbsoluteDifference(s, p) + margin;
  307. switch (relType) {
  308. case IMAGE_REL_ARM64_BRANCH26:
  309. return isInt<28>(diff);
  310. case IMAGE_REL_ARM64_BRANCH19:
  311. return isInt<21>(diff);
  312. case IMAGE_REL_ARM64_BRANCH14:
  313. return isInt<16>(diff);
  314. default:
  315. return true;
  316. }
  317. } else {
  318. llvm_unreachable("Unexpected architecture");
  319. }
  320. }
  321. // Return the last thunk for the given target if it is in range,
  322. // or create a new one.
  323. static std::pair<Defined *, bool>
  324. getThunk(DenseMap<uint64_t, Defined *> &lastThunks, Defined *target, uint64_t p,
  325. uint16_t type, int margin) {
  326. Defined *&lastThunk = lastThunks[target->getRVA()];
  327. if (lastThunk && isInRange(type, lastThunk->getRVA(), p, margin))
  328. return {lastThunk, false};
  329. Chunk *c;
  330. switch (config->machine) {
  331. case ARMNT:
  332. c = make<RangeExtensionThunkARM>(target);
  333. break;
  334. case ARM64:
  335. c = make<RangeExtensionThunkARM64>(target);
  336. break;
  337. default:
  338. llvm_unreachable("Unexpected architecture");
  339. }
  340. Defined *d = make<DefinedSynthetic>("", c);
  341. lastThunk = d;
  342. return {d, true};
  343. }
  344. // This checks all relocations, and for any relocation which isn't in range
  345. // it adds a thunk after the section chunk that contains the relocation.
  346. // If the latest thunk for the specific target is in range, that is used
  347. // instead of creating a new thunk. All range checks are done with the
  348. // specified margin, to make sure that relocations that originally are in
  349. // range, but only barely, also get thunks - in case other added thunks makes
  350. // the target go out of range.
  351. //
  352. // After adding thunks, we verify that all relocations are in range (with
  353. // no extra margin requirements). If this failed, we restart (throwing away
  354. // the previously created thunks) and retry with a wider margin.
  355. static bool createThunks(OutputSection *os, int margin) {
  356. bool addressesChanged = false;
  357. DenseMap<uint64_t, Defined *> lastThunks;
  358. DenseMap<std::pair<ObjFile *, Defined *>, uint32_t> thunkSymtabIndices;
  359. size_t thunksSize = 0;
  360. // Recheck Chunks.size() each iteration, since we can insert more
  361. // elements into it.
  362. for (size_t i = 0; i != os->chunks.size(); ++i) {
  363. SectionChunk *sc = dyn_cast_or_null<SectionChunk>(os->chunks[i]);
  364. if (!sc)
  365. continue;
  366. size_t thunkInsertionSpot = i + 1;
  367. // Try to get a good enough estimate of where new thunks will be placed.
  368. // Offset this by the size of the new thunks added so far, to make the
  369. // estimate slightly better.
  370. size_t thunkInsertionRVA = sc->getRVA() + sc->getSize() + thunksSize;
  371. ObjFile *file = sc->file;
  372. std::vector<std::pair<uint32_t, uint32_t>> relocReplacements;
  373. ArrayRef<coff_relocation> originalRelocs =
  374. file->getCOFFObj()->getRelocations(sc->header);
  375. for (size_t j = 0, e = originalRelocs.size(); j < e; ++j) {
  376. const coff_relocation &rel = originalRelocs[j];
  377. Symbol *relocTarget = file->getSymbol(rel.SymbolTableIndex);
  378. // The estimate of the source address P should be pretty accurate,
  379. // but we don't know whether the target Symbol address should be
  380. // offset by thunksSize or not (or by some of thunksSize but not all of
  381. // it), giving us some uncertainty once we have added one thunk.
  382. uint64_t p = sc->getRVA() + rel.VirtualAddress + thunksSize;
  383. Defined *sym = dyn_cast_or_null<Defined>(relocTarget);
  384. if (!sym)
  385. continue;
  386. uint64_t s = sym->getRVA();
  387. if (isInRange(rel.Type, s, p, margin))
  388. continue;
  389. // If the target isn't in range, hook it up to an existing or new
  390. // thunk.
  391. Defined *thunk;
  392. bool wasNew;
  393. std::tie(thunk, wasNew) = getThunk(lastThunks, sym, p, rel.Type, margin);
  394. if (wasNew) {
  395. Chunk *thunkChunk = thunk->getChunk();
  396. thunkChunk->setRVA(
  397. thunkInsertionRVA); // Estimate of where it will be located.
  398. os->chunks.insert(os->chunks.begin() + thunkInsertionSpot, thunkChunk);
  399. thunkInsertionSpot++;
  400. thunksSize += thunkChunk->getSize();
  401. thunkInsertionRVA += thunkChunk->getSize();
  402. addressesChanged = true;
  403. }
  404. // To redirect the relocation, add a symbol to the parent object file's
  405. // symbol table, and replace the relocation symbol table index with the
  406. // new index.
  407. auto insertion = thunkSymtabIndices.insert({{file, thunk}, ~0U});
  408. uint32_t &thunkSymbolIndex = insertion.first->second;
  409. if (insertion.second)
  410. thunkSymbolIndex = file->addRangeThunkSymbol(thunk);
  411. relocReplacements.push_back({j, thunkSymbolIndex});
  412. }
  413. // Get a writable copy of this section's relocations so they can be
  414. // modified. If the relocations point into the object file, allocate new
  415. // memory. Otherwise, this must be previously allocated memory that can be
  416. // modified in place.
  417. ArrayRef<coff_relocation> curRelocs = sc->getRelocs();
  418. MutableArrayRef<coff_relocation> newRelocs;
  419. if (originalRelocs.data() == curRelocs.data()) {
  420. newRelocs = makeMutableArrayRef(
  421. bAlloc.Allocate<coff_relocation>(originalRelocs.size()),
  422. originalRelocs.size());
  423. } else {
  424. newRelocs = makeMutableArrayRef(
  425. const_cast<coff_relocation *>(curRelocs.data()), curRelocs.size());
  426. }
  427. // Copy each relocation, but replace the symbol table indices which need
  428. // thunks.
  429. auto nextReplacement = relocReplacements.begin();
  430. auto endReplacement = relocReplacements.end();
  431. for (size_t i = 0, e = originalRelocs.size(); i != e; ++i) {
  432. newRelocs[i] = originalRelocs[i];
  433. if (nextReplacement != endReplacement && nextReplacement->first == i) {
  434. newRelocs[i].SymbolTableIndex = nextReplacement->second;
  435. ++nextReplacement;
  436. }
  437. }
  438. sc->setRelocs(newRelocs);
  439. }
  440. return addressesChanged;
  441. }
  442. // Verify that all relocations are in range, with no extra margin requirements.
  443. static bool verifyRanges(const std::vector<Chunk *> chunks) {
  444. for (Chunk *c : chunks) {
  445. SectionChunk *sc = dyn_cast_or_null<SectionChunk>(c);
  446. if (!sc)
  447. continue;
  448. ArrayRef<coff_relocation> relocs = sc->getRelocs();
  449. for (size_t j = 0, e = relocs.size(); j < e; ++j) {
  450. const coff_relocation &rel = relocs[j];
  451. Symbol *relocTarget = sc->file->getSymbol(rel.SymbolTableIndex);
  452. Defined *sym = dyn_cast_or_null<Defined>(relocTarget);
  453. if (!sym)
  454. continue;
  455. uint64_t p = sc->getRVA() + rel.VirtualAddress;
  456. uint64_t s = sym->getRVA();
  457. if (!isInRange(rel.Type, s, p, 0))
  458. return false;
  459. }
  460. }
  461. return true;
  462. }
  463. // Assign addresses and add thunks if necessary.
  464. void Writer::finalizeAddresses() {
  465. assignAddresses();
  466. if (config->machine != ARMNT && config->machine != ARM64)
  467. return;
  468. size_t origNumChunks = 0;
  469. for (OutputSection *sec : outputSections) {
  470. sec->origChunks = sec->chunks;
  471. origNumChunks += sec->chunks.size();
  472. }
  473. int pass = 0;
  474. int margin = 1024 * 100;
  475. while (true) {
  476. // First check whether we need thunks at all, or if the previous pass of
  477. // adding them turned out ok.
  478. bool rangesOk = true;
  479. size_t numChunks = 0;
  480. for (OutputSection *sec : outputSections) {
  481. if (!verifyRanges(sec->chunks)) {
  482. rangesOk = false;
  483. break;
  484. }
  485. numChunks += sec->chunks.size();
  486. }
  487. if (rangesOk) {
  488. if (pass > 0)
  489. log("Added " + Twine(numChunks - origNumChunks) + " thunks with " +
  490. "margin " + Twine(margin) + " in " + Twine(pass) + " passes");
  491. return;
  492. }
  493. if (pass >= 10)
  494. fatal("adding thunks hasn't converged after " + Twine(pass) + " passes");
  495. if (pass > 0) {
  496. // If the previous pass didn't work out, reset everything back to the
  497. // original conditions before retrying with a wider margin. This should
  498. // ideally never happen under real circumstances.
  499. for (OutputSection *sec : outputSections)
  500. sec->chunks = sec->origChunks;
  501. margin *= 2;
  502. }
  503. // Try adding thunks everywhere where it is needed, with a margin
  504. // to avoid things going out of range due to the added thunks.
  505. bool addressesChanged = false;
  506. for (OutputSection *sec : outputSections)
  507. addressesChanged |= createThunks(sec, margin);
  508. // If the verification above thought we needed thunks, we should have
  509. // added some.
  510. assert(addressesChanged);
  511. // Recalculate the layout for the whole image (and verify the ranges at
  512. // the start of the next round).
  513. assignAddresses();
  514. pass++;
  515. }
  516. }
  517. // The main function of the writer.
  518. void Writer::run() {
  519. ScopedTimer t1(codeLayoutTimer);
  520. createImportTables();
  521. createSections();
  522. createMiscChunks();
  523. appendImportThunks();
  524. createExportTable();
  525. mergeSections();
  526. removeUnusedSections();
  527. finalizeAddresses();
  528. removeEmptySections();
  529. assignOutputSectionIndices();
  530. setSectionPermissions();
  531. createSymbolAndStringTable();
  532. if (fileSize > UINT32_MAX)
  533. fatal("image size (" + Twine(fileSize) + ") " +
  534. "exceeds maximum allowable size (" + Twine(UINT32_MAX) + ")");
  535. openFile(config->outputFile);
  536. if (config->is64()) {
  537. writeHeader<pe32plus_header>();
  538. } else {
  539. writeHeader<pe32_header>();
  540. }
  541. writeSections();
  542. sortExceptionTable();
  543. t1.stop();
  544. if (!config->pdbPath.empty() && config->debug) {
  545. assert(buildId);
  546. createPDB(symtab, outputSections, sectionTable, buildId->buildId);
  547. }
  548. writeBuildId();
  549. writeMapFile(outputSections);
  550. if (errorCount())
  551. return;
  552. ScopedTimer t2(diskCommitTimer);
  553. if (auto e = buffer->commit())
  554. fatal("failed to write the output file: " + toString(std::move(e)));
  555. }
  556. static StringRef getOutputSectionName(StringRef name) {
  557. StringRef s = name.split('$').first;
  558. // Treat a later period as a separator for MinGW, for sections like
  559. // ".ctors.01234".
  560. return s.substr(0, s.find('.', 1));
  561. }
  562. // For /order.
  563. static void sortBySectionOrder(std::vector<Chunk *> &chunks) {
  564. auto getPriority = [](const Chunk *c) {
  565. if (auto *sec = dyn_cast<SectionChunk>(c))
  566. if (sec->sym)
  567. return config->order.lookup(sec->sym->getName());
  568. return 0;
  569. };
  570. llvm::stable_sort(chunks, [=](const Chunk *a, const Chunk *b) {
  571. return getPriority(a) < getPriority(b);
  572. });
  573. }
  574. // Change the characteristics of existing PartialSections that belong to the
  575. // section Name to Chars.
  576. void Writer::fixPartialSectionChars(StringRef name, uint32_t chars) {
  577. for (auto it : partialSections) {
  578. PartialSection *pSec = it.second;
  579. StringRef curName = pSec->name;
  580. if (!curName.consume_front(name) ||
  581. (!curName.empty() && !curName.startswith("$")))
  582. continue;
  583. if (pSec->characteristics == chars)
  584. continue;
  585. PartialSection *destSec = createPartialSection(pSec->name, chars);
  586. destSec->chunks.insert(destSec->chunks.end(), pSec->chunks.begin(),
  587. pSec->chunks.end());
  588. pSec->chunks.clear();
  589. }
  590. }
  591. // Sort concrete section chunks from GNU import libraries.
  592. //
  593. // GNU binutils doesn't use short import files, but instead produces import
  594. // libraries that consist of object files, with section chunks for the .idata$*
  595. // sections. These are linked just as regular static libraries. Each import
  596. // library consists of one header object, one object file for every imported
  597. // symbol, and one trailer object. In order for the .idata tables/lists to
  598. // be formed correctly, the section chunks within each .idata$* section need
  599. // to be grouped by library, and sorted alphabetically within each library
  600. // (which makes sure the header comes first and the trailer last).
  601. bool Writer::fixGnuImportChunks() {
  602. uint32_t rdata = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ;
  603. // Make sure all .idata$* section chunks are mapped as RDATA in order to
  604. // be sorted into the same sections as our own synthesized .idata chunks.
  605. fixPartialSectionChars(".idata", rdata);
  606. bool hasIdata = false;
  607. // Sort all .idata$* chunks, grouping chunks from the same library,
  608. // with alphabetical ordering of the object fils within a library.
  609. for (auto it : partialSections) {
  610. PartialSection *pSec = it.second;
  611. if (!pSec->name.startswith(".idata"))
  612. continue;
  613. if (!pSec->chunks.empty())
  614. hasIdata = true;
  615. llvm::stable_sort(pSec->chunks, [&](Chunk *s, Chunk *t) {
  616. SectionChunk *sc1 = dyn_cast_or_null<SectionChunk>(s);
  617. SectionChunk *sc2 = dyn_cast_or_null<SectionChunk>(t);
  618. if (!sc1 || !sc2) {
  619. // if SC1, order them ascending. If SC2 or both null,
  620. // S is not less than T.
  621. return sc1 != nullptr;
  622. }
  623. // Make a string with "libraryname/objectfile" for sorting, achieving
  624. // both grouping by library and sorting of objects within a library,
  625. // at once.
  626. std::string key1 =
  627. (sc1->file->parentName + "/" + sc1->file->getName()).str();
  628. std::string key2 =
  629. (sc2->file->parentName + "/" + sc2->file->getName()).str();
  630. return key1 < key2;
  631. });
  632. }
  633. return hasIdata;
  634. }
  635. // Add generated idata chunks, for imported symbols and DLLs, and a
  636. // terminator in .idata$2.
  637. void Writer::addSyntheticIdata() {
  638. uint32_t rdata = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ;
  639. idata.create();
  640. // Add the .idata content in the right section groups, to allow
  641. // chunks from other linked in object files to be grouped together.
  642. // See Microsoft PE/COFF spec 5.4 for details.
  643. auto add = [&](StringRef n, std::vector<Chunk *> &v) {
  644. PartialSection *pSec = createPartialSection(n, rdata);
  645. pSec->chunks.insert(pSec->chunks.end(), v.begin(), v.end());
  646. };
  647. // The loader assumes a specific order of data.
  648. // Add each type in the correct order.
  649. add(".idata$2", idata.dirs);
  650. add(".idata$4", idata.lookups);
  651. add(".idata$5", idata.addresses);
  652. if (!idata.hints.empty())
  653. add(".idata$6", idata.hints);
  654. add(".idata$7", idata.dllNames);
  655. }
  656. // Locate the first Chunk and size of the import directory list and the
  657. // IAT.
  658. void Writer::locateImportTables() {
  659. uint32_t rdata = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ;
  660. if (PartialSection *importDirs = findPartialSection(".idata$2", rdata)) {
  661. if (!importDirs->chunks.empty())
  662. importTableStart = importDirs->chunks.front();
  663. for (Chunk *c : importDirs->chunks)
  664. importTableSize += c->getSize();
  665. }
  666. if (PartialSection *importAddresses = findPartialSection(".idata$5", rdata)) {
  667. if (!importAddresses->chunks.empty())
  668. iatStart = importAddresses->chunks.front();
  669. for (Chunk *c : importAddresses->chunks)
  670. iatSize += c->getSize();
  671. }
  672. }
  673. // Return whether a SectionChunk's suffix (the dollar and any trailing
  674. // suffix) should be removed and sorted into the main suffixless
  675. // PartialSection.
  676. static bool shouldStripSectionSuffix(SectionChunk *sc, StringRef name) {
  677. // On MinGW, comdat groups are formed by putting the comdat group name
  678. // after the '$' in the section name. For .eh_frame$<symbol>, that must
  679. // still be sorted before the .eh_frame trailer from crtend.o, thus just
  680. // strip the section name trailer. For other sections, such as
  681. // .tls$$<symbol> (where non-comdat .tls symbols are otherwise stored in
  682. // ".tls$"), they must be strictly sorted after .tls. And for the
  683. // hypothetical case of comdat .CRT$XCU, we definitely need to keep the
  684. // suffix for sorting. Thus, to play it safe, only strip the suffix for
  685. // the standard sections.
  686. if (!config->mingw)
  687. return false;
  688. if (!sc || !sc->isCOMDAT())
  689. return false;
  690. return name.startswith(".text$") || name.startswith(".data$") ||
  691. name.startswith(".rdata$") || name.startswith(".pdata$") ||
  692. name.startswith(".xdata$") || name.startswith(".eh_frame$");
  693. }
  694. // Create output section objects and add them to OutputSections.
  695. void Writer::createSections() {
  696. // First, create the builtin sections.
  697. const uint32_t data = IMAGE_SCN_CNT_INITIALIZED_DATA;
  698. const uint32_t bss = IMAGE_SCN_CNT_UNINITIALIZED_DATA;
  699. const uint32_t code = IMAGE_SCN_CNT_CODE;
  700. const uint32_t discardable = IMAGE_SCN_MEM_DISCARDABLE;
  701. const uint32_t r = IMAGE_SCN_MEM_READ;
  702. const uint32_t w = IMAGE_SCN_MEM_WRITE;
  703. const uint32_t x = IMAGE_SCN_MEM_EXECUTE;
  704. SmallDenseMap<std::pair<StringRef, uint32_t>, OutputSection *> sections;
  705. auto createSection = [&](StringRef name, uint32_t outChars) {
  706. OutputSection *&sec = sections[{name, outChars}];
  707. if (!sec) {
  708. sec = make<OutputSection>(name, outChars);
  709. outputSections.push_back(sec);
  710. }
  711. return sec;
  712. };
  713. // Try to match the section order used by link.exe.
  714. textSec = createSection(".text", code | r | x);
  715. createSection(".bss", bss | r | w);
  716. rdataSec = createSection(".rdata", data | r);
  717. buildidSec = createSection(".buildid", data | r);
  718. dataSec = createSection(".data", data | r | w);
  719. pdataSec = createSection(".pdata", data | r);
  720. idataSec = createSection(".idata", data | r);
  721. edataSec = createSection(".edata", data | r);
  722. didatSec = createSection(".didat", data | r);
  723. rsrcSec = createSection(".rsrc", data | r);
  724. relocSec = createSection(".reloc", data | discardable | r);
  725. ctorsSec = createSection(".ctors", data | r | w);
  726. dtorsSec = createSection(".dtors", data | r | w);
  727. // Then bin chunks by name and output characteristics.
  728. for (Chunk *c : symtab->getChunks()) {
  729. auto *sc = dyn_cast<SectionChunk>(c);
  730. if (sc && !sc->live) {
  731. if (config->verbose)
  732. sc->printDiscardedMessage();
  733. continue;
  734. }
  735. StringRef name = c->getSectionName();
  736. if (shouldStripSectionSuffix(sc, name))
  737. name = name.split('$').first;
  738. PartialSection *pSec = createPartialSection(name,
  739. c->getOutputCharacteristics());
  740. pSec->chunks.push_back(c);
  741. }
  742. fixPartialSectionChars(".rsrc", data | r);
  743. fixPartialSectionChars(".edata", data | r);
  744. // Even in non MinGW cases, we might need to link against GNU import
  745. // libraries.
  746. bool hasIdata = fixGnuImportChunks();
  747. if (!idata.empty())
  748. hasIdata = true;
  749. if (hasIdata)
  750. addSyntheticIdata();
  751. // Process an /order option.
  752. if (!config->order.empty())
  753. for (auto it : partialSections)
  754. sortBySectionOrder(it.second->chunks);
  755. if (hasIdata)
  756. locateImportTables();
  757. // Then create an OutputSection for each section.
  758. // '$' and all following characters in input section names are
  759. // discarded when determining output section. So, .text$foo
  760. // contributes to .text, for example. See PE/COFF spec 3.2.
  761. for (auto it : partialSections) {
  762. PartialSection *pSec = it.second;
  763. StringRef name = getOutputSectionName(pSec->name);
  764. uint32_t outChars = pSec->characteristics;
  765. if (name == ".CRT") {
  766. // In link.exe, there is a special case for the I386 target where .CRT
  767. // sections are treated as if they have output characteristics DATA | R if
  768. // their characteristics are DATA | R | W. This implements the same
  769. // special case for all architectures.
  770. outChars = data | r;
  771. log("Processing section " + pSec->name + " -> " + name);
  772. sortCRTSectionChunks(pSec->chunks);
  773. }
  774. OutputSection *sec = createSection(name, outChars);
  775. for (Chunk *c : pSec->chunks)
  776. sec->addChunk(c);
  777. sec->addContributingPartialSection(pSec);
  778. }
  779. // Finally, move some output sections to the end.
  780. auto sectionOrder = [&](const OutputSection *s) {
  781. // Move DISCARDABLE (or non-memory-mapped) sections to the end of file
  782. // because the loader cannot handle holes. Stripping can remove other
  783. // discardable ones than .reloc, which is first of them (created early).
  784. if (s->header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE)
  785. return 2;
  786. // .rsrc should come at the end of the non-discardable sections because its
  787. // size may change by the Win32 UpdateResources() function, causing
  788. // subsequent sections to move (see https://crbug.com/827082).
  789. if (s == rsrcSec)
  790. return 1;
  791. return 0;
  792. };
  793. llvm::stable_sort(outputSections,
  794. [&](const OutputSection *s, const OutputSection *t) {
  795. return sectionOrder(s) < sectionOrder(t);
  796. });
  797. }
  798. void Writer::createMiscChunks() {
  799. for (MergeChunk *p : MergeChunk::instances) {
  800. if (p) {
  801. p->finalizeContents();
  802. rdataSec->addChunk(p);
  803. }
  804. }
  805. // Create thunks for locally-dllimported symbols.
  806. if (!symtab->localImportChunks.empty()) {
  807. for (Chunk *c : symtab->localImportChunks)
  808. rdataSec->addChunk(c);
  809. }
  810. // Create Debug Information Chunks
  811. OutputSection *debugInfoSec = config->mingw ? buildidSec : rdataSec;
  812. if (config->debug || config->repro) {
  813. debugDirectory = make<DebugDirectoryChunk>(debugRecords, config->repro);
  814. debugInfoSec->addChunk(debugDirectory);
  815. }
  816. if (config->debug) {
  817. // Make a CVDebugRecordChunk even when /DEBUG:CV is not specified. We
  818. // output a PDB no matter what, and this chunk provides the only means of
  819. // allowing a debugger to match a PDB and an executable. So we need it even
  820. // if we're ultimately not going to write CodeView data to the PDB.
  821. buildId = make<CVDebugRecordChunk>();
  822. debugRecords.push_back(buildId);
  823. for (Chunk *c : debugRecords)
  824. debugInfoSec->addChunk(c);
  825. }
  826. // Create SEH table. x86-only.
  827. if (config->safeSEH)
  828. createSEHTable();
  829. // Create /guard:cf tables if requested.
  830. if (config->guardCF != GuardCFLevel::Off)
  831. createGuardCFTables();
  832. if (config->mingw) {
  833. createRuntimePseudoRelocs();
  834. insertCtorDtorSymbols();
  835. }
  836. }
  837. // Create .idata section for the DLL-imported symbol table.
  838. // The format of this section is inherently Windows-specific.
  839. // IdataContents class abstracted away the details for us,
  840. // so we just let it create chunks and add them to the section.
  841. void Writer::createImportTables() {
  842. // Initialize DLLOrder so that import entries are ordered in
  843. // the same order as in the command line. (That affects DLL
  844. // initialization order, and this ordering is MSVC-compatible.)
  845. for (ImportFile *file : ImportFile::instances) {
  846. if (!file->live)
  847. continue;
  848. std::string dll = StringRef(file->dllName).lower();
  849. if (config->dllOrder.count(dll) == 0)
  850. config->dllOrder[dll] = config->dllOrder.size();
  851. if (file->impSym && !isa<DefinedImportData>(file->impSym))
  852. fatal(toString(*file->impSym) + " was replaced");
  853. DefinedImportData *impSym = cast_or_null<DefinedImportData>(file->impSym);
  854. if (config->delayLoads.count(StringRef(file->dllName).lower())) {
  855. if (!file->thunkSym)
  856. fatal("cannot delay-load " + toString(file) +
  857. " due to import of data: " + toString(*impSym));
  858. delayIdata.add(impSym);
  859. } else {
  860. idata.add(impSym);
  861. }
  862. }
  863. }
  864. void Writer::appendImportThunks() {
  865. if (ImportFile::instances.empty())
  866. return;
  867. for (ImportFile *file : ImportFile::instances) {
  868. if (!file->live)
  869. continue;
  870. if (!file->thunkSym)
  871. continue;
  872. if (!isa<DefinedImportThunk>(file->thunkSym))
  873. fatal(toString(*file->thunkSym) + " was replaced");
  874. DefinedImportThunk *thunk = cast<DefinedImportThunk>(file->thunkSym);
  875. if (file->thunkLive)
  876. textSec->addChunk(thunk->getChunk());
  877. }
  878. if (!delayIdata.empty()) {
  879. Defined *helper = cast<Defined>(config->delayLoadHelper);
  880. delayIdata.create(helper);
  881. for (Chunk *c : delayIdata.getChunks())
  882. didatSec->addChunk(c);
  883. for (Chunk *c : delayIdata.getDataChunks())
  884. dataSec->addChunk(c);
  885. for (Chunk *c : delayIdata.getCodeChunks())
  886. textSec->addChunk(c);
  887. }
  888. }
  889. void Writer::createExportTable() {
  890. if (!edataSec->chunks.empty()) {
  891. // Allow using a custom built export table from input object files, instead
  892. // of having the linker synthesize the tables.
  893. if (config->hadExplicitExports)
  894. warn("literal .edata sections override exports");
  895. } else if (!config->exports.empty()) {
  896. for (Chunk *c : edata.chunks)
  897. edataSec->addChunk(c);
  898. }
  899. if (!edataSec->chunks.empty()) {
  900. edataStart = edataSec->chunks.front();
  901. edataEnd = edataSec->chunks.back();
  902. }
  903. }
  904. void Writer::removeUnusedSections() {
  905. // Remove sections that we can be sure won't get content, to avoid
  906. // allocating space for their section headers.
  907. auto isUnused = [this](OutputSection *s) {
  908. if (s == relocSec)
  909. return false; // This section is populated later.
  910. // MergeChunks have zero size at this point, as their size is finalized
  911. // later. Only remove sections that have no Chunks at all.
  912. return s->chunks.empty();
  913. };
  914. outputSections.erase(
  915. std::remove_if(outputSections.begin(), outputSections.end(), isUnused),
  916. outputSections.end());
  917. }
  918. // The Windows loader doesn't seem to like empty sections,
  919. // so we remove them if any.
  920. void Writer::removeEmptySections() {
  921. auto isEmpty = [](OutputSection *s) { return s->getVirtualSize() == 0; };
  922. outputSections.erase(
  923. std::remove_if(outputSections.begin(), outputSections.end(), isEmpty),
  924. outputSections.end());
  925. }
  926. void Writer::assignOutputSectionIndices() {
  927. // Assign final output section indices, and assign each chunk to its output
  928. // section.
  929. uint32_t idx = 1;
  930. for (OutputSection *os : outputSections) {
  931. os->sectionIndex = idx;
  932. for (Chunk *c : os->chunks)
  933. c->setOutputSectionIdx(idx);
  934. ++idx;
  935. }
  936. // Merge chunks are containers of chunks, so assign those an output section
  937. // too.
  938. for (MergeChunk *mc : MergeChunk::instances)
  939. if (mc)
  940. for (SectionChunk *sc : mc->sections)
  941. if (sc && sc->live)
  942. sc->setOutputSectionIdx(mc->getOutputSectionIdx());
  943. }
  944. size_t Writer::addEntryToStringTable(StringRef str) {
  945. assert(str.size() > COFF::NameSize);
  946. size_t offsetOfEntry = strtab.size() + 4; // +4 for the size field
  947. strtab.insert(strtab.end(), str.begin(), str.end());
  948. strtab.push_back('\0');
  949. return offsetOfEntry;
  950. }
  951. Optional<coff_symbol16> Writer::createSymbol(Defined *def) {
  952. coff_symbol16 sym;
  953. switch (def->kind()) {
  954. case Symbol::DefinedAbsoluteKind:
  955. sym.Value = def->getRVA();
  956. sym.SectionNumber = IMAGE_SYM_ABSOLUTE;
  957. break;
  958. case Symbol::DefinedSyntheticKind:
  959. // Relative symbols are unrepresentable in a COFF symbol table.
  960. return None;
  961. default: {
  962. // Don't write symbols that won't be written to the output to the symbol
  963. // table.
  964. Chunk *c = def->getChunk();
  965. if (!c)
  966. return None;
  967. OutputSection *os = c->getOutputSection();
  968. if (!os)
  969. return None;
  970. sym.Value = def->getRVA() - os->getRVA();
  971. sym.SectionNumber = os->sectionIndex;
  972. break;
  973. }
  974. }
  975. // Symbols that are runtime pseudo relocations don't point to the actual
  976. // symbol data itself (as they are imported), but points to the IAT entry
  977. // instead. Avoid emitting them to the symbol table, as they can confuse
  978. // debuggers.
  979. if (def->isRuntimePseudoReloc)
  980. return None;
  981. StringRef name = def->getName();
  982. if (name.size() > COFF::NameSize) {
  983. sym.Name.Offset.Zeroes = 0;
  984. sym.Name.Offset.Offset = addEntryToStringTable(name);
  985. } else {
  986. memset(sym.Name.ShortName, 0, COFF::NameSize);
  987. memcpy(sym.Name.ShortName, name.data(), name.size());
  988. }
  989. if (auto *d = dyn_cast<DefinedCOFF>(def)) {
  990. COFFSymbolRef ref = d->getCOFFSymbol();
  991. sym.Type = ref.getType();
  992. sym.StorageClass = ref.getStorageClass();
  993. } else {
  994. sym.Type = IMAGE_SYM_TYPE_NULL;
  995. sym.StorageClass = IMAGE_SYM_CLASS_EXTERNAL;
  996. }
  997. sym.NumberOfAuxSymbols = 0;
  998. return sym;
  999. }
  1000. void Writer::createSymbolAndStringTable() {
  1001. // PE/COFF images are limited to 8 byte section names. Longer names can be
  1002. // supported by writing a non-standard string table, but this string table is
  1003. // not mapped at runtime and the long names will therefore be inaccessible.
  1004. // link.exe always truncates section names to 8 bytes, whereas binutils always
  1005. // preserves long section names via the string table. LLD adopts a hybrid
  1006. // solution where discardable sections have long names preserved and
  1007. // non-discardable sections have their names truncated, to ensure that any
  1008. // section which is mapped at runtime also has its name mapped at runtime.
  1009. for (OutputSection *sec : outputSections) {
  1010. if (sec->name.size() <= COFF::NameSize)
  1011. continue;
  1012. if ((sec->header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE) == 0)
  1013. continue;
  1014. sec->setStringTableOff(addEntryToStringTable(sec->name));
  1015. }
  1016. if (config->debugDwarf || config->debugSymtab) {
  1017. for (ObjFile *file : ObjFile::instances) {
  1018. for (Symbol *b : file->getSymbols()) {
  1019. auto *d = dyn_cast_or_null<Defined>(b);
  1020. if (!d || d->writtenToSymtab)
  1021. continue;
  1022. d->writtenToSymtab = true;
  1023. if (Optional<coff_symbol16> sym = createSymbol(d))
  1024. outputSymtab.push_back(*sym);
  1025. }
  1026. }
  1027. }
  1028. if (outputSymtab.empty() && strtab.empty())
  1029. return;
  1030. // We position the symbol table to be adjacent to the end of the last section.
  1031. uint64_t fileOff = fileSize;
  1032. pointerToSymbolTable = fileOff;
  1033. fileOff += outputSymtab.size() * sizeof(coff_symbol16);
  1034. fileOff += 4 + strtab.size();
  1035. fileSize = alignTo(fileOff, config->fileAlign);
  1036. }
  1037. void Writer::mergeSections() {
  1038. if (!pdataSec->chunks.empty()) {
  1039. firstPdata = pdataSec->chunks.front();
  1040. lastPdata = pdataSec->chunks.back();
  1041. }
  1042. for (auto &p : config->merge) {
  1043. StringRef toName = p.second;
  1044. if (p.first == toName)
  1045. continue;
  1046. StringSet<> names;
  1047. while (1) {
  1048. if (!names.insert(toName).second)
  1049. fatal("/merge: cycle found for section '" + p.first + "'");
  1050. auto i = config->merge.find(toName);
  1051. if (i == config->merge.end())
  1052. break;
  1053. toName = i->second;
  1054. }
  1055. OutputSection *from = findSection(p.first);
  1056. OutputSection *to = findSection(toName);
  1057. if (!from)
  1058. continue;
  1059. if (!to) {
  1060. from->name = toName;
  1061. continue;
  1062. }
  1063. to->merge(from);
  1064. }
  1065. }
  1066. // Visits all sections to assign incremental, non-overlapping RVAs and
  1067. // file offsets.
  1068. void Writer::assignAddresses() {
  1069. sizeOfHeaders = dosStubSize + sizeof(PEMagic) + sizeof(coff_file_header) +
  1070. sizeof(data_directory) * numberOfDataDirectory +
  1071. sizeof(coff_section) * outputSections.size();
  1072. sizeOfHeaders +=
  1073. config->is64() ? sizeof(pe32plus_header) : sizeof(pe32_header);
  1074. sizeOfHeaders = alignTo(sizeOfHeaders, config->fileAlign);
  1075. fileSize = sizeOfHeaders;
  1076. // The first page is kept unmapped.
  1077. uint64_t rva = alignTo(sizeOfHeaders, config->align);
  1078. for (OutputSection *sec : outputSections) {
  1079. if (sec == relocSec)
  1080. addBaserels();
  1081. uint64_t rawSize = 0, virtualSize = 0;
  1082. sec->header.VirtualAddress = rva;
  1083. // If /FUNCTIONPADMIN is used, functions are padded in order to create a
  1084. // hotpatchable image.
  1085. const bool isCodeSection =
  1086. (sec->header.Characteristics & IMAGE_SCN_CNT_CODE) &&
  1087. (sec->header.Characteristics & IMAGE_SCN_MEM_READ) &&
  1088. (sec->header.Characteristics & IMAGE_SCN_MEM_EXECUTE);
  1089. uint32_t padding = isCodeSection ? config->functionPadMin : 0;
  1090. for (Chunk *c : sec->chunks) {
  1091. if (padding && c->isHotPatchable())
  1092. virtualSize += padding;
  1093. virtualSize = alignTo(virtualSize, c->getAlignment());
  1094. c->setRVA(rva + virtualSize);
  1095. virtualSize += c->getSize();
  1096. if (c->hasData)
  1097. rawSize = alignTo(virtualSize, config->fileAlign);
  1098. }
  1099. if (virtualSize > UINT32_MAX)
  1100. error("section larger than 4 GiB: " + sec->name);
  1101. sec->header.VirtualSize = virtualSize;
  1102. sec->header.SizeOfRawData = rawSize;
  1103. if (rawSize != 0)
  1104. sec->header.PointerToRawData = fileSize;
  1105. rva += alignTo(virtualSize, config->align);
  1106. fileSize += alignTo(rawSize, config->fileAlign);
  1107. }
  1108. sizeOfImage = alignTo(rva, config->align);
  1109. // Assign addresses to sections in MergeChunks.
  1110. for (MergeChunk *mc : MergeChunk::instances)
  1111. if (mc)
  1112. mc->assignSubsectionRVAs();
  1113. }
  1114. template <typename PEHeaderTy> void Writer::writeHeader() {
  1115. // Write DOS header. For backwards compatibility, the first part of a PE/COFF
  1116. // executable consists of an MS-DOS MZ executable. If the executable is run
  1117. // under DOS, that program gets run (usually to just print an error message).
  1118. // When run under Windows, the loader looks at AddressOfNewExeHeader and uses
  1119. // the PE header instead.
  1120. uint8_t *buf = buffer->getBufferStart();
  1121. auto *dos = reinterpret_cast<dos_header *>(buf);
  1122. buf += sizeof(dos_header);
  1123. dos->Magic[0] = 'M';
  1124. dos->Magic[1] = 'Z';
  1125. dos->UsedBytesInTheLastPage = dosStubSize % 512;
  1126. dos->FileSizeInPages = divideCeil(dosStubSize, 512);
  1127. dos->HeaderSizeInParagraphs = sizeof(dos_header) / 16;
  1128. dos->AddressOfRelocationTable = sizeof(dos_header);
  1129. dos->AddressOfNewExeHeader = dosStubSize;
  1130. // Write DOS program.
  1131. memcpy(buf, dosProgram, sizeof(dosProgram));
  1132. buf += sizeof(dosProgram);
  1133. // Write PE magic
  1134. memcpy(buf, PEMagic, sizeof(PEMagic));
  1135. buf += sizeof(PEMagic);
  1136. // Write COFF header
  1137. auto *coff = reinterpret_cast<coff_file_header *>(buf);
  1138. buf += sizeof(*coff);
  1139. coff->Machine = config->machine;
  1140. coff->NumberOfSections = outputSections.size();
  1141. coff->Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE;
  1142. if (config->largeAddressAware)
  1143. coff->Characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
  1144. if (!config->is64())
  1145. coff->Characteristics |= IMAGE_FILE_32BIT_MACHINE;
  1146. if (config->dll)
  1147. coff->Characteristics |= IMAGE_FILE_DLL;
  1148. if (!config->relocatable)
  1149. coff->Characteristics |= IMAGE_FILE_RELOCS_STRIPPED;
  1150. if (config->swaprunCD)
  1151. coff->Characteristics |= IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP;
  1152. if (config->swaprunNet)
  1153. coff->Characteristics |= IMAGE_FILE_NET_RUN_FROM_SWAP;
  1154. coff->SizeOfOptionalHeader =
  1155. sizeof(PEHeaderTy) + sizeof(data_directory) * numberOfDataDirectory;
  1156. // Write PE header
  1157. auto *pe = reinterpret_cast<PEHeaderTy *>(buf);
  1158. buf += sizeof(*pe);
  1159. pe->Magic = config->is64() ? PE32Header::PE32_PLUS : PE32Header::PE32;
  1160. // If {Major,Minor}LinkerVersion is left at 0.0, then for some
  1161. // reason signing the resulting PE file with Authenticode produces a
  1162. // signature that fails to validate on Windows 7 (but is OK on 10).
  1163. // Set it to 14.0, which is what VS2015 outputs, and which avoids
  1164. // that problem.
  1165. pe->MajorLinkerVersion = 14;
  1166. pe->MinorLinkerVersion = 0;
  1167. pe->ImageBase = config->imageBase;
  1168. pe->SectionAlignment = config->align;
  1169. pe->FileAlignment = config->fileAlign;
  1170. pe->MajorImageVersion = config->majorImageVersion;
  1171. pe->MinorImageVersion = config->minorImageVersion;
  1172. pe->MajorOperatingSystemVersion = config->majorOSVersion;
  1173. pe->MinorOperatingSystemVersion = config->minorOSVersion;
  1174. pe->MajorSubsystemVersion = config->majorOSVersion;
  1175. pe->MinorSubsystemVersion = config->minorOSVersion;
  1176. pe->Subsystem = config->subsystem;
  1177. pe->SizeOfImage = sizeOfImage;
  1178. pe->SizeOfHeaders = sizeOfHeaders;
  1179. if (!config->noEntry) {
  1180. Defined *entry = cast<Defined>(config->entry);
  1181. pe->AddressOfEntryPoint = entry->getRVA();
  1182. // Pointer to thumb code must have the LSB set, so adjust it.
  1183. if (config->machine == ARMNT)
  1184. pe->AddressOfEntryPoint |= 1;
  1185. }
  1186. pe->SizeOfStackReserve = config->stackReserve;
  1187. pe->SizeOfStackCommit = config->stackCommit;
  1188. pe->SizeOfHeapReserve = config->heapReserve;
  1189. pe->SizeOfHeapCommit = config->heapCommit;
  1190. if (config->appContainer)
  1191. pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_APPCONTAINER;
  1192. if (config->dynamicBase)
  1193. pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE;
  1194. if (config->highEntropyVA)
  1195. pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA;
  1196. if (!config->allowBind)
  1197. pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_BIND;
  1198. if (config->nxCompat)
  1199. pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NX_COMPAT;
  1200. if (!config->allowIsolation)
  1201. pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION;
  1202. if (config->guardCF != GuardCFLevel::Off)
  1203. pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_GUARD_CF;
  1204. if (config->integrityCheck)
  1205. pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY;
  1206. if (setNoSEHCharacteristic)
  1207. pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_SEH;
  1208. if (config->terminalServerAware)
  1209. pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE;
  1210. pe->NumberOfRvaAndSize = numberOfDataDirectory;
  1211. if (textSec->getVirtualSize()) {
  1212. pe->BaseOfCode = textSec->getRVA();
  1213. pe->SizeOfCode = textSec->getRawSize();
  1214. }
  1215. pe->SizeOfInitializedData = getSizeOfInitializedData();
  1216. // Write data directory
  1217. auto *dir = reinterpret_cast<data_directory *>(buf);
  1218. buf += sizeof(*dir) * numberOfDataDirectory;
  1219. if (edataStart) {
  1220. dir[EXPORT_TABLE].RelativeVirtualAddress = edataStart->getRVA();
  1221. dir[EXPORT_TABLE].Size =
  1222. edataEnd->getRVA() + edataEnd->getSize() - edataStart->getRVA();
  1223. }
  1224. if (importTableStart) {
  1225. dir[IMPORT_TABLE].RelativeVirtualAddress = importTableStart->getRVA();
  1226. dir[IMPORT_TABLE].Size = importTableSize;
  1227. }
  1228. if (iatStart) {
  1229. dir[IAT].RelativeVirtualAddress = iatStart->getRVA();
  1230. dir[IAT].Size = iatSize;
  1231. }
  1232. if (rsrcSec->getVirtualSize()) {
  1233. dir[RESOURCE_TABLE].RelativeVirtualAddress = rsrcSec->getRVA();
  1234. dir[RESOURCE_TABLE].Size = rsrcSec->getVirtualSize();
  1235. }
  1236. if (firstPdata) {
  1237. dir[EXCEPTION_TABLE].RelativeVirtualAddress = firstPdata->getRVA();
  1238. dir[EXCEPTION_TABLE].Size =
  1239. lastPdata->getRVA() + lastPdata->getSize() - firstPdata->getRVA();
  1240. }
  1241. if (relocSec->getVirtualSize()) {
  1242. dir[BASE_RELOCATION_TABLE].RelativeVirtualAddress = relocSec->getRVA();
  1243. dir[BASE_RELOCATION_TABLE].Size = relocSec->getVirtualSize();
  1244. }
  1245. if (Symbol *sym = symtab->findUnderscore("_tls_used")) {
  1246. if (Defined *b = dyn_cast<Defined>(sym)) {
  1247. dir[TLS_TABLE].RelativeVirtualAddress = b->getRVA();
  1248. dir[TLS_TABLE].Size = config->is64()
  1249. ? sizeof(object::coff_tls_directory64)
  1250. : sizeof(object::coff_tls_directory32);
  1251. }
  1252. }
  1253. if (debugDirectory) {
  1254. dir[DEBUG_DIRECTORY].RelativeVirtualAddress = debugDirectory->getRVA();
  1255. dir[DEBUG_DIRECTORY].Size = debugDirectory->getSize();
  1256. }
  1257. if (Symbol *sym = symtab->findUnderscore("_load_config_used")) {
  1258. if (auto *b = dyn_cast<DefinedRegular>(sym)) {
  1259. SectionChunk *sc = b->getChunk();
  1260. assert(b->getRVA() >= sc->getRVA());
  1261. uint64_t offsetInChunk = b->getRVA() - sc->getRVA();
  1262. if (!sc->hasData || offsetInChunk + 4 > sc->getSize())
  1263. fatal("_load_config_used is malformed");
  1264. ArrayRef<uint8_t> secContents = sc->getContents();
  1265. uint32_t loadConfigSize =
  1266. *reinterpret_cast<const ulittle32_t *>(&secContents[offsetInChunk]);
  1267. if (offsetInChunk + loadConfigSize > sc->getSize())
  1268. fatal("_load_config_used is too large");
  1269. dir[LOAD_CONFIG_TABLE].RelativeVirtualAddress = b->getRVA();
  1270. dir[LOAD_CONFIG_TABLE].Size = loadConfigSize;
  1271. }
  1272. }
  1273. if (!delayIdata.empty()) {
  1274. dir[DELAY_IMPORT_DESCRIPTOR].RelativeVirtualAddress =
  1275. delayIdata.getDirRVA();
  1276. dir[DELAY_IMPORT_DESCRIPTOR].Size = delayIdata.getDirSize();
  1277. }
  1278. // Write section table
  1279. for (OutputSection *sec : outputSections) {
  1280. sec->writeHeaderTo(buf);
  1281. buf += sizeof(coff_section);
  1282. }
  1283. sectionTable = ArrayRef<uint8_t>(
  1284. buf - outputSections.size() * sizeof(coff_section), buf);
  1285. if (outputSymtab.empty() && strtab.empty())
  1286. return;
  1287. coff->PointerToSymbolTable = pointerToSymbolTable;
  1288. uint32_t numberOfSymbols = outputSymtab.size();
  1289. coff->NumberOfSymbols = numberOfSymbols;
  1290. auto *symbolTable = reinterpret_cast<coff_symbol16 *>(
  1291. buffer->getBufferStart() + coff->PointerToSymbolTable);
  1292. for (size_t i = 0; i != numberOfSymbols; ++i)
  1293. symbolTable[i] = outputSymtab[i];
  1294. // Create the string table, it follows immediately after the symbol table.
  1295. // The first 4 bytes is length including itself.
  1296. buf = reinterpret_cast<uint8_t *>(&symbolTable[numberOfSymbols]);
  1297. write32le(buf, strtab.size() + 4);
  1298. if (!strtab.empty())
  1299. memcpy(buf + 4, strtab.data(), strtab.size());
  1300. }
  1301. void Writer::openFile(StringRef path) {
  1302. buffer = CHECK(
  1303. FileOutputBuffer::create(path, fileSize, FileOutputBuffer::F_executable),
  1304. "failed to open " + path);
  1305. }
  1306. void Writer::createSEHTable() {
  1307. SymbolRVASet handlers;
  1308. for (ObjFile *file : ObjFile::instances) {
  1309. if (!file->hasSafeSEH())
  1310. error("/safeseh: " + file->getName() + " is not compatible with SEH");
  1311. markSymbolsForRVATable(file, file->getSXDataChunks(), handlers);
  1312. }
  1313. // Set the "no SEH" characteristic if there really were no handlers, or if
  1314. // there is no load config object to point to the table of handlers.
  1315. setNoSEHCharacteristic =
  1316. handlers.empty() || !symtab->findUnderscore("_load_config_used");
  1317. maybeAddRVATable(std::move(handlers), "__safe_se_handler_table",
  1318. "__safe_se_handler_count");
  1319. }
  1320. // Add a symbol to an RVA set. Two symbols may have the same RVA, but an RVA set
  1321. // cannot contain duplicates. Therefore, the set is uniqued by Chunk and the
  1322. // symbol's offset into that Chunk.
  1323. static void addSymbolToRVASet(SymbolRVASet &rvaSet, Defined *s) {
  1324. Chunk *c = s->getChunk();
  1325. if (auto *sc = dyn_cast<SectionChunk>(c))
  1326. c = sc->repl; // Look through ICF replacement.
  1327. uint32_t off = s->getRVA() - (c ? c->getRVA() : 0);
  1328. rvaSet.insert({c, off});
  1329. }
  1330. // Given a symbol, add it to the GFIDs table if it is a live, defined, function
  1331. // symbol in an executable section.
  1332. static void maybeAddAddressTakenFunction(SymbolRVASet &addressTakenSyms,
  1333. Symbol *s) {
  1334. if (!s)
  1335. return;
  1336. switch (s->kind()) {
  1337. case Symbol::DefinedLocalImportKind:
  1338. case Symbol::DefinedImportDataKind:
  1339. // Defines an __imp_ pointer, so it is data, so it is ignored.
  1340. break;
  1341. case Symbol::DefinedCommonKind:
  1342. // Common is always data, so it is ignored.
  1343. break;
  1344. case Symbol::DefinedAbsoluteKind:
  1345. case Symbol::DefinedSyntheticKind:
  1346. // Absolute is never code, synthetic generally isn't and usually isn't
  1347. // determinable.
  1348. break;
  1349. case Symbol::LazyArchiveKind:
  1350. case Symbol::LazyObjectKind:
  1351. case Symbol::UndefinedKind:
  1352. // Undefined symbols resolve to zero, so they don't have an RVA. Lazy
  1353. // symbols shouldn't have relocations.
  1354. break;
  1355. case Symbol::DefinedImportThunkKind:
  1356. // Thunks are always code, include them.
  1357. addSymbolToRVASet(addressTakenSyms, cast<Defined>(s));
  1358. break;
  1359. case Symbol::DefinedRegularKind: {
  1360. // This is a regular, defined, symbol from a COFF file. Mark the symbol as
  1361. // address taken if the symbol type is function and it's in an executable
  1362. // section.
  1363. auto *d = cast<DefinedRegular>(s);
  1364. if (d->getCOFFSymbol().getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION) {
  1365. SectionChunk *sc = dyn_cast<SectionChunk>(d->getChunk());
  1366. if (sc && sc->live &&
  1367. sc->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE)
  1368. addSymbolToRVASet(addressTakenSyms, d);
  1369. }
  1370. break;
  1371. }
  1372. }
  1373. }
  1374. // Visit all relocations from all section contributions of this object file and
  1375. // mark the relocation target as address-taken.
  1376. static void markSymbolsWithRelocations(ObjFile *file,
  1377. SymbolRVASet &usedSymbols) {
  1378. for (Chunk *c : file->getChunks()) {
  1379. // We only care about live section chunks. Common chunks and other chunks
  1380. // don't generally contain relocations.
  1381. SectionChunk *sc = dyn_cast<SectionChunk>(c);
  1382. if (!sc || !sc->live)
  1383. continue;
  1384. for (const coff_relocation &reloc : sc->getRelocs()) {
  1385. if (config->machine == I386 && reloc.Type == COFF::IMAGE_REL_I386_REL32)
  1386. // Ignore relative relocations on x86. On x86_64 they can't be ignored
  1387. // since they're also used to compute absolute addresses.
  1388. continue;
  1389. Symbol *ref = sc->file->getSymbol(reloc.SymbolTableIndex);
  1390. maybeAddAddressTakenFunction(usedSymbols, ref);
  1391. }
  1392. }
  1393. }
  1394. // Create the guard function id table. This is a table of RVAs of all
  1395. // address-taken functions. It is sorted and uniqued, just like the safe SEH
  1396. // table.
  1397. void Writer::createGuardCFTables() {
  1398. SymbolRVASet addressTakenSyms;
  1399. SymbolRVASet longJmpTargets;
  1400. for (ObjFile *file : ObjFile::instances) {
  1401. // If the object was compiled with /guard:cf, the address taken symbols
  1402. // are in .gfids$y sections, and the longjmp targets are in .gljmp$y
  1403. // sections. If the object was not compiled with /guard:cf, we assume there
  1404. // were no setjmp targets, and that all code symbols with relocations are
  1405. // possibly address-taken.
  1406. if (file->hasGuardCF()) {
  1407. markSymbolsForRVATable(file, file->getGuardFidChunks(), addressTakenSyms);
  1408. markSymbolsForRVATable(file, file->getGuardLJmpChunks(), longJmpTargets);
  1409. } else {
  1410. markSymbolsWithRelocations(file, addressTakenSyms);
  1411. }
  1412. }
  1413. // Mark the image entry as address-taken.
  1414. if (config->entry)
  1415. maybeAddAddressTakenFunction(addressTakenSyms, config->entry);
  1416. // Mark exported symbols in executable sections as address-taken.
  1417. for (Export &e : config->exports)
  1418. maybeAddAddressTakenFunction(addressTakenSyms, e.sym);
  1419. // Ensure sections referenced in the gfid table are 16-byte aligned.
  1420. for (const ChunkAndOffset &c : addressTakenSyms)
  1421. if (c.inputChunk->getAlignment() < 16)
  1422. c.inputChunk->setAlignment(16);
  1423. maybeAddRVATable(std::move(addressTakenSyms), "__guard_fids_table",
  1424. "__guard_fids_count");
  1425. // Add the longjmp target table unless the user told us not to.
  1426. if (config->guardCF == GuardCFLevel::Full)
  1427. maybeAddRVATable(std::move(longJmpTargets), "__guard_longjmp_table",
  1428. "__guard_longjmp_count");
  1429. // Set __guard_flags, which will be used in the load config to indicate that
  1430. // /guard:cf was enabled.
  1431. uint32_t guardFlags = uint32_t(coff_guard_flags::CFInstrumented) |
  1432. uint32_t(coff_guard_flags::HasFidTable);
  1433. if (config->guardCF == GuardCFLevel::Full)
  1434. guardFlags |= uint32_t(coff_guard_flags::HasLongJmpTable);
  1435. Symbol *flagSym = symtab->findUnderscore("__guard_flags");
  1436. cast<DefinedAbsolute>(flagSym)->setVA(guardFlags);
  1437. }
  1438. // Take a list of input sections containing symbol table indices and add those
  1439. // symbols to an RVA table. The challenge is that symbol RVAs are not known and
  1440. // depend on the table size, so we can't directly build a set of integers.
  1441. void Writer::markSymbolsForRVATable(ObjFile *file,
  1442. ArrayRef<SectionChunk *> symIdxChunks,
  1443. SymbolRVASet &tableSymbols) {
  1444. for (SectionChunk *c : symIdxChunks) {
  1445. // Skip sections discarded by linker GC. This comes up when a .gfids section
  1446. // is associated with something like a vtable and the vtable is discarded.
  1447. // In this case, the associated gfids section is discarded, and we don't
  1448. // mark the virtual member functions as address-taken by the vtable.
  1449. if (!c->live)
  1450. continue;
  1451. // Validate that the contents look like symbol table indices.
  1452. ArrayRef<uint8_t> data = c->getContents();
  1453. if (data.size() % 4 != 0) {
  1454. warn("ignoring " + c->getSectionName() +
  1455. " symbol table index section in object " + toString(file));
  1456. continue;
  1457. }
  1458. // Read each symbol table index and check if that symbol was included in the
  1459. // final link. If so, add it to the table symbol set.
  1460. ArrayRef<ulittle32_t> symIndices(
  1461. reinterpret_cast<const ulittle32_t *>(data.data()), data.size() / 4);
  1462. ArrayRef<Symbol *> objSymbols = file->getSymbols();
  1463. for (uint32_t symIndex : symIndices) {
  1464. if (symIndex >= objSymbols.size()) {
  1465. warn("ignoring invalid symbol table index in section " +
  1466. c->getSectionName() + " in object " + toString(file));
  1467. continue;
  1468. }
  1469. if (Symbol *s = objSymbols[symIndex]) {
  1470. if (s->isLive())
  1471. addSymbolToRVASet(tableSymbols, cast<Defined>(s));
  1472. }
  1473. }
  1474. }
  1475. }
  1476. // Replace the absolute table symbol with a synthetic symbol pointing to
  1477. // tableChunk so that we can emit base relocations for it and resolve section
  1478. // relative relocations.
  1479. void Writer::maybeAddRVATable(SymbolRVASet tableSymbols, StringRef tableSym,
  1480. StringRef countSym) {
  1481. if (tableSymbols.empty())
  1482. return;
  1483. RVATableChunk *tableChunk = make<RVATableChunk>(std::move(tableSymbols));
  1484. rdataSec->addChunk(tableChunk);
  1485. Symbol *t = symtab->findUnderscore(tableSym);
  1486. Symbol *c = symtab->findUnderscore(countSym);
  1487. replaceSymbol<DefinedSynthetic>(t, t->getName(), tableChunk);
  1488. cast<DefinedAbsolute>(c)->setVA(tableChunk->getSize() / 4);
  1489. }
  1490. // MinGW specific. Gather all relocations that are imported from a DLL even
  1491. // though the code didn't expect it to, produce the table that the runtime
  1492. // uses for fixing them up, and provide the synthetic symbols that the
  1493. // runtime uses for finding the table.
  1494. void Writer::createRuntimePseudoRelocs() {
  1495. std::vector<RuntimePseudoReloc> rels;
  1496. for (Chunk *c : symtab->getChunks()) {
  1497. auto *sc = dyn_cast<SectionChunk>(c);
  1498. if (!sc || !sc->live)
  1499. continue;
  1500. sc->getRuntimePseudoRelocs(rels);
  1501. }
  1502. if (!rels.empty())
  1503. log("Writing " + Twine(rels.size()) + " runtime pseudo relocations");
  1504. PseudoRelocTableChunk *table = make<PseudoRelocTableChunk>(rels);
  1505. rdataSec->addChunk(table);
  1506. EmptyChunk *endOfList = make<EmptyChunk>();
  1507. rdataSec->addChunk(endOfList);
  1508. Symbol *headSym = symtab->findUnderscore("__RUNTIME_PSEUDO_RELOC_LIST__");
  1509. Symbol *endSym = symtab->findUnderscore("__RUNTIME_PSEUDO_RELOC_LIST_END__");
  1510. replaceSymbol<DefinedSynthetic>(headSym, headSym->getName(), table);
  1511. replaceSymbol<DefinedSynthetic>(endSym, endSym->getName(), endOfList);
  1512. }
  1513. // MinGW specific.
  1514. // The MinGW .ctors and .dtors lists have sentinels at each end;
  1515. // a (uintptr_t)-1 at the start and a (uintptr_t)0 at the end.
  1516. // There's a symbol pointing to the start sentinel pointer, __CTOR_LIST__
  1517. // and __DTOR_LIST__ respectively.
  1518. void Writer::insertCtorDtorSymbols() {
  1519. AbsolutePointerChunk *ctorListHead = make<AbsolutePointerChunk>(-1);
  1520. AbsolutePointerChunk *ctorListEnd = make<AbsolutePointerChunk>(0);
  1521. AbsolutePointerChunk *dtorListHead = make<AbsolutePointerChunk>(-1);
  1522. AbsolutePointerChunk *dtorListEnd = make<AbsolutePointerChunk>(0);
  1523. ctorsSec->insertChunkAtStart(ctorListHead);
  1524. ctorsSec->addChunk(ctorListEnd);
  1525. dtorsSec->insertChunkAtStart(dtorListHead);
  1526. dtorsSec->addChunk(dtorListEnd);
  1527. Symbol *ctorListSym = symtab->findUnderscore("__CTOR_LIST__");
  1528. Symbol *dtorListSym = symtab->findUnderscore("__DTOR_LIST__");
  1529. replaceSymbol<DefinedSynthetic>(ctorListSym, ctorListSym->getName(),
  1530. ctorListHead);
  1531. replaceSymbol<DefinedSynthetic>(dtorListSym, dtorListSym->getName(),
  1532. dtorListHead);
  1533. }
  1534. // Handles /section options to allow users to overwrite
  1535. // section attributes.
  1536. void Writer::setSectionPermissions() {
  1537. for (auto &p : config->section) {
  1538. StringRef name = p.first;
  1539. uint32_t perm = p.second;
  1540. for (OutputSection *sec : outputSections)
  1541. if (sec->name == name)
  1542. sec->setPermissions(perm);
  1543. }
  1544. }
  1545. // Write section contents to a mmap'ed file.
  1546. void Writer::writeSections() {
  1547. // Record the number of sections to apply section index relocations
  1548. // against absolute symbols. See applySecIdx in Chunks.cpp..
  1549. DefinedAbsolute::numOutputSections = outputSections.size();
  1550. uint8_t *buf = buffer->getBufferStart();
  1551. for (OutputSection *sec : outputSections) {
  1552. uint8_t *secBuf = buf + sec->getFileOff();
  1553. // Fill gaps between functions in .text with INT3 instructions
  1554. // instead of leaving as NUL bytes (which can be interpreted as
  1555. // ADD instructions).
  1556. if (sec->header.Characteristics & IMAGE_SCN_CNT_CODE)
  1557. memset(secBuf, 0xCC, sec->getRawSize());
  1558. parallelForEach(sec->chunks, [&](Chunk *c) {
  1559. c->writeTo(secBuf + c->getRVA() - sec->getRVA());
  1560. });
  1561. }
  1562. }
  1563. void Writer::writeBuildId() {
  1564. // There are two important parts to the build ID.
  1565. // 1) If building with debug info, the COFF debug directory contains a
  1566. // timestamp as well as a Guid and Age of the PDB.
  1567. // 2) In all cases, the PE COFF file header also contains a timestamp.
  1568. // For reproducibility, instead of a timestamp we want to use a hash of the
  1569. // PE contents.
  1570. if (config->debug) {
  1571. assert(buildId && "BuildId is not set!");
  1572. // BuildId->BuildId was filled in when the PDB was written.
  1573. }
  1574. // At this point the only fields in the COFF file which remain unset are the
  1575. // "timestamp" in the COFF file header, and the ones in the coff debug
  1576. // directory. Now we can hash the file and write that hash to the various
  1577. // timestamp fields in the file.
  1578. StringRef outputFileData(
  1579. reinterpret_cast<const char *>(buffer->getBufferStart()),
  1580. buffer->getBufferSize());
  1581. uint32_t timestamp = config->timestamp;
  1582. uint64_t hash = 0;
  1583. bool generateSyntheticBuildId =
  1584. config->mingw && config->debug && config->pdbPath.empty();
  1585. if (config->repro || generateSyntheticBuildId)
  1586. hash = xxHash64(outputFileData);
  1587. if (config->repro)
  1588. timestamp = static_cast<uint32_t>(hash);
  1589. if (generateSyntheticBuildId) {
  1590. // For MinGW builds without a PDB file, we still generate a build id
  1591. // to allow associating a crash dump to the executable.
  1592. buildId->buildId->PDB70.CVSignature = OMF::Signature::PDB70;
  1593. buildId->buildId->PDB70.Age = 1;
  1594. memcpy(buildId->buildId->PDB70.Signature, &hash, 8);
  1595. // xxhash only gives us 8 bytes, so put some fixed data in the other half.
  1596. memcpy(&buildId->buildId->PDB70.Signature[8], "LLD PDB.", 8);
  1597. }
  1598. if (debugDirectory)
  1599. debugDirectory->setTimeDateStamp(timestamp);
  1600. uint8_t *buf = buffer->getBufferStart();
  1601. buf += dosStubSize + sizeof(PEMagic);
  1602. object::coff_file_header *coffHeader =
  1603. reinterpret_cast<coff_file_header *>(buf);
  1604. coffHeader->TimeDateStamp = timestamp;
  1605. }
  1606. // Sort .pdata section contents according to PE/COFF spec 5.5.
  1607. void Writer::sortExceptionTable() {
  1608. if (!firstPdata)
  1609. return;
  1610. // We assume .pdata contains function table entries only.
  1611. auto bufAddr = [&](Chunk *c) {
  1612. OutputSection *os = c->getOutputSection();
  1613. return buffer->getBufferStart() + os->getFileOff() + c->getRVA() -
  1614. os->getRVA();
  1615. };
  1616. uint8_t *begin = bufAddr(firstPdata);
  1617. uint8_t *end = bufAddr(lastPdata) + lastPdata->getSize();
  1618. if (config->machine == AMD64) {
  1619. struct Entry { ulittle32_t begin, end, unwind; };
  1620. parallelSort(
  1621. MutableArrayRef<Entry>((Entry *)begin, (Entry *)end),
  1622. [](const Entry &a, const Entry &b) { return a.begin < b.begin; });
  1623. return;
  1624. }
  1625. if (config->machine == ARMNT || config->machine == ARM64) {
  1626. struct Entry { ulittle32_t begin, unwind; };
  1627. parallelSort(
  1628. MutableArrayRef<Entry>((Entry *)begin, (Entry *)end),
  1629. [](const Entry &a, const Entry &b) { return a.begin < b.begin; });
  1630. return;
  1631. }
  1632. errs() << "warning: don't know how to handle .pdata.\n";
  1633. }
  1634. // The CRT section contains, among other things, the array of function
  1635. // pointers that initialize every global variable that is not trivially
  1636. // constructed. The CRT calls them one after the other prior to invoking
  1637. // main().
  1638. //
  1639. // As per C++ spec, 3.6.2/2.3,
  1640. // "Variables with ordered initialization defined within a single
  1641. // translation unit shall be initialized in the order of their definitions
  1642. // in the translation unit"
  1643. //
  1644. // It is therefore critical to sort the chunks containing the function
  1645. // pointers in the order that they are listed in the object file (top to
  1646. // bottom), otherwise global objects might not be initialized in the
  1647. // correct order.
  1648. void Writer::sortCRTSectionChunks(std::vector<Chunk *> &chunks) {
  1649. auto sectionChunkOrder = [](const Chunk *a, const Chunk *b) {
  1650. auto sa = dyn_cast<SectionChunk>(a);
  1651. auto sb = dyn_cast<SectionChunk>(b);
  1652. assert(sa && sb && "Non-section chunks in CRT section!");
  1653. StringRef sAObj = sa->file->mb.getBufferIdentifier();
  1654. StringRef sBObj = sb->file->mb.getBufferIdentifier();
  1655. return sAObj == sBObj && sa->getSectionNumber() < sb->getSectionNumber();
  1656. };
  1657. llvm::stable_sort(chunks, sectionChunkOrder);
  1658. if (config->verbose) {
  1659. for (auto &c : chunks) {
  1660. auto sc = dyn_cast<SectionChunk>(c);
  1661. log(" " + sc->file->mb.getBufferIdentifier().str() +
  1662. ", SectionID: " + Twine(sc->getSectionNumber()));
  1663. }
  1664. }
  1665. }
  1666. OutputSection *Writer::findSection(StringRef name) {
  1667. for (OutputSection *sec : outputSections)
  1668. if (sec->name == name)
  1669. return sec;
  1670. return nullptr;
  1671. }
  1672. uint32_t Writer::getSizeOfInitializedData() {
  1673. uint32_t res = 0;
  1674. for (OutputSection *s : outputSections)
  1675. if (s->header.Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA)
  1676. res += s->getRawSize();
  1677. return res;
  1678. }
  1679. // Add base relocations to .reloc section.
  1680. void Writer::addBaserels() {
  1681. if (!config->relocatable)
  1682. return;
  1683. relocSec->chunks.clear();
  1684. std::vector<Baserel> v;
  1685. for (OutputSection *sec : outputSections) {
  1686. if (sec->header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE)
  1687. continue;
  1688. // Collect all locations for base relocations.
  1689. for (Chunk *c : sec->chunks)
  1690. c->getBaserels(&v);
  1691. // Add the addresses to .reloc section.
  1692. if (!v.empty())
  1693. addBaserelBlocks(v);
  1694. v.clear();
  1695. }
  1696. }
  1697. // Add addresses to .reloc section. Note that addresses are grouped by page.
  1698. void Writer::addBaserelBlocks(std::vector<Baserel> &v) {
  1699. const uint32_t mask = ~uint32_t(pageSize - 1);
  1700. uint32_t page = v[0].rva & mask;
  1701. size_t i = 0, j = 1;
  1702. for (size_t e = v.size(); j < e; ++j) {
  1703. uint32_t p = v[j].rva & mask;
  1704. if (p == page)
  1705. continue;
  1706. relocSec->addChunk(make<BaserelChunk>(page, &v[i], &v[0] + j));
  1707. i = j;
  1708. page = p;
  1709. }
  1710. if (i == j)
  1711. return;
  1712. relocSec->addChunk(make<BaserelChunk>(page, &v[i], &v[0] + j));
  1713. }
  1714. PartialSection *Writer::createPartialSection(StringRef name,
  1715. uint32_t outChars) {
  1716. PartialSection *&pSec = partialSections[{name, outChars}];
  1717. if (pSec)
  1718. return pSec;
  1719. pSec = make<PartialSection>(name, outChars);
  1720. return pSec;
  1721. }
  1722. PartialSection *Writer::findPartialSection(StringRef name, uint32_t outChars) {
  1723. auto it = partialSections.find({name, outChars});
  1724. if (it != partialSections.end())
  1725. return it->second;
  1726. return nullptr;
  1727. }
  1728. } // namespace coff
  1729. } // namespace lld