Chunks.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. //===- Chunks.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 "Chunks.h"
  9. #include "InputFiles.h"
  10. #include "Symbols.h"
  11. #include "Writer.h"
  12. #include "SymbolTable.h"
  13. #include "lld/Common/ErrorHandler.h"
  14. #include "llvm/ADT/Twine.h"
  15. #include "llvm/BinaryFormat/COFF.h"
  16. #include "llvm/Object/COFF.h"
  17. #include "llvm/Support/Debug.h"
  18. #include "llvm/Support/Endian.h"
  19. #include "llvm/Support/raw_ostream.h"
  20. #include <algorithm>
  21. using namespace llvm;
  22. using namespace llvm::object;
  23. using namespace llvm::support::endian;
  24. using namespace llvm::COFF;
  25. using llvm::support::ulittle32_t;
  26. namespace lld {
  27. namespace coff {
  28. SectionChunk::SectionChunk(ObjFile *f, const coff_section *h)
  29. : Chunk(SectionKind), file(f), header(h), repl(this) {
  30. // Initialize relocs.
  31. setRelocs(file->getCOFFObj()->getRelocations(header));
  32. // Initialize sectionName.
  33. StringRef sectionName;
  34. if (Expected<StringRef> e = file->getCOFFObj()->getSectionName(header))
  35. sectionName = *e;
  36. sectionNameData = sectionName.data();
  37. sectionNameSize = sectionName.size();
  38. setAlignment(header->getAlignment());
  39. hasData = !(header->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA);
  40. // If linker GC is disabled, every chunk starts out alive. If linker GC is
  41. // enabled, treat non-comdat sections as roots. Generally optimized object
  42. // files will be built with -ffunction-sections or /Gy, so most things worth
  43. // stripping will be in a comdat.
  44. live = !config->doGC || !isCOMDAT();
  45. }
  46. // SectionChunk is one of the most frequently allocated classes, so it is
  47. // important to keep it as compact as possible. As of this writing, the number
  48. // below is the size of this class on x64 platforms.
  49. static_assert(sizeof(SectionChunk) <= 88, "SectionChunk grew unexpectedly");
  50. static void add16(uint8_t *p, int16_t v) { write16le(p, read16le(p) + v); }
  51. static void add32(uint8_t *p, int32_t v) { write32le(p, read32le(p) + v); }
  52. static void add64(uint8_t *p, int64_t v) { write64le(p, read64le(p) + v); }
  53. static void or16(uint8_t *p, uint16_t v) { write16le(p, read16le(p) | v); }
  54. static void or32(uint8_t *p, uint32_t v) { write32le(p, read32le(p) | v); }
  55. // Verify that given sections are appropriate targets for SECREL
  56. // relocations. This check is relaxed because unfortunately debug
  57. // sections have section-relative relocations against absolute symbols.
  58. static bool checkSecRel(const SectionChunk *sec, OutputSection *os) {
  59. if (os)
  60. return true;
  61. if (sec->isCodeView())
  62. return false;
  63. error("SECREL relocation cannot be applied to absolute symbols");
  64. return false;
  65. }
  66. static void applySecRel(const SectionChunk *sec, uint8_t *off,
  67. OutputSection *os, uint64_t s) {
  68. if (!checkSecRel(sec, os))
  69. return;
  70. uint64_t secRel = s - os->getRVA();
  71. if (secRel > UINT32_MAX) {
  72. error("overflow in SECREL relocation in section: " + sec->getSectionName());
  73. return;
  74. }
  75. add32(off, secRel);
  76. }
  77. static void applySecIdx(uint8_t *off, OutputSection *os) {
  78. // Absolute symbol doesn't have section index, but section index relocation
  79. // against absolute symbol should be resolved to one plus the last output
  80. // section index. This is required for compatibility with MSVC.
  81. if (os)
  82. add16(off, os->sectionIndex);
  83. else
  84. add16(off, DefinedAbsolute::numOutputSections + 1);
  85. }
  86. void SectionChunk::applyRelX64(uint8_t *off, uint16_t type, OutputSection *os,
  87. uint64_t s, uint64_t p) const {
  88. switch (type) {
  89. case IMAGE_REL_AMD64_ADDR32: add32(off, s + config->imageBase); break;
  90. case IMAGE_REL_AMD64_ADDR64: add64(off, s + config->imageBase); break;
  91. case IMAGE_REL_AMD64_ADDR32NB: add32(off, s); break;
  92. case IMAGE_REL_AMD64_REL32: add32(off, s - p - 4); break;
  93. case IMAGE_REL_AMD64_REL32_1: add32(off, s - p - 5); break;
  94. case IMAGE_REL_AMD64_REL32_2: add32(off, s - p - 6); break;
  95. case IMAGE_REL_AMD64_REL32_3: add32(off, s - p - 7); break;
  96. case IMAGE_REL_AMD64_REL32_4: add32(off, s - p - 8); break;
  97. case IMAGE_REL_AMD64_REL32_5: add32(off, s - p - 9); break;
  98. case IMAGE_REL_AMD64_SECTION: applySecIdx(off, os); break;
  99. case IMAGE_REL_AMD64_SECREL: applySecRel(this, off, os, s); break;
  100. default:
  101. error("unsupported relocation type 0x" + Twine::utohexstr(type) + " in " +
  102. toString(file));
  103. }
  104. }
  105. void SectionChunk::applyRelX86(uint8_t *off, uint16_t type, OutputSection *os,
  106. uint64_t s, uint64_t p) const {
  107. switch (type) {
  108. case IMAGE_REL_I386_ABSOLUTE: break;
  109. case IMAGE_REL_I386_DIR32: add32(off, s + config->imageBase); break;
  110. case IMAGE_REL_I386_DIR32NB: add32(off, s); break;
  111. case IMAGE_REL_I386_REL32: add32(off, s - p - 4); break;
  112. case IMAGE_REL_I386_SECTION: applySecIdx(off, os); break;
  113. case IMAGE_REL_I386_SECREL: applySecRel(this, off, os, s); break;
  114. default:
  115. error("unsupported relocation type 0x" + Twine::utohexstr(type) + " in " +
  116. toString(file));
  117. }
  118. }
  119. static void applyMOV(uint8_t *off, uint16_t v) {
  120. write16le(off, (read16le(off) & 0xfbf0) | ((v & 0x800) >> 1) | ((v >> 12) & 0xf));
  121. write16le(off + 2, (read16le(off + 2) & 0x8f00) | ((v & 0x700) << 4) | (v & 0xff));
  122. }
  123. static uint16_t readMOV(uint8_t *off, bool movt) {
  124. uint16_t op1 = read16le(off);
  125. if ((op1 & 0xfbf0) != (movt ? 0xf2c0 : 0xf240))
  126. error("unexpected instruction in " + Twine(movt ? "MOVT" : "MOVW") +
  127. " instruction in MOV32T relocation");
  128. uint16_t op2 = read16le(off + 2);
  129. if ((op2 & 0x8000) != 0)
  130. error("unexpected instruction in " + Twine(movt ? "MOVT" : "MOVW") +
  131. " instruction in MOV32T relocation");
  132. return (op2 & 0x00ff) | ((op2 >> 4) & 0x0700) | ((op1 << 1) & 0x0800) |
  133. ((op1 & 0x000f) << 12);
  134. }
  135. void applyMOV32T(uint8_t *off, uint32_t v) {
  136. uint16_t immW = readMOV(off, false); // read MOVW operand
  137. uint16_t immT = readMOV(off + 4, true); // read MOVT operand
  138. uint32_t imm = immW | (immT << 16);
  139. v += imm; // add the immediate offset
  140. applyMOV(off, v); // set MOVW operand
  141. applyMOV(off + 4, v >> 16); // set MOVT operand
  142. }
  143. static void applyBranch20T(uint8_t *off, int32_t v) {
  144. if (!isInt<21>(v))
  145. error("relocation out of range");
  146. uint32_t s = v < 0 ? 1 : 0;
  147. uint32_t j1 = (v >> 19) & 1;
  148. uint32_t j2 = (v >> 18) & 1;
  149. or16(off, (s << 10) | ((v >> 12) & 0x3f));
  150. or16(off + 2, (j1 << 13) | (j2 << 11) | ((v >> 1) & 0x7ff));
  151. }
  152. void applyBranch24T(uint8_t *off, int32_t v) {
  153. if (!isInt<25>(v))
  154. error("relocation out of range");
  155. uint32_t s = v < 0 ? 1 : 0;
  156. uint32_t j1 = ((~v >> 23) & 1) ^ s;
  157. uint32_t j2 = ((~v >> 22) & 1) ^ s;
  158. or16(off, (s << 10) | ((v >> 12) & 0x3ff));
  159. // Clear out the J1 and J2 bits which may be set.
  160. write16le(off + 2, (read16le(off + 2) & 0xd000) | (j1 << 13) | (j2 << 11) | ((v >> 1) & 0x7ff));
  161. }
  162. void SectionChunk::applyRelARM(uint8_t *off, uint16_t type, OutputSection *os,
  163. uint64_t s, uint64_t p) const {
  164. // Pointer to thumb code must have the LSB set.
  165. uint64_t sx = s;
  166. if (os && (os->header.Characteristics & IMAGE_SCN_MEM_EXECUTE))
  167. sx |= 1;
  168. switch (type) {
  169. case IMAGE_REL_ARM_ADDR32: add32(off, sx + config->imageBase); break;
  170. case IMAGE_REL_ARM_ADDR32NB: add32(off, sx); break;
  171. case IMAGE_REL_ARM_MOV32T: applyMOV32T(off, sx + config->imageBase); break;
  172. case IMAGE_REL_ARM_BRANCH20T: applyBranch20T(off, sx - p - 4); break;
  173. case IMAGE_REL_ARM_BRANCH24T: applyBranch24T(off, sx - p - 4); break;
  174. case IMAGE_REL_ARM_BLX23T: applyBranch24T(off, sx - p - 4); break;
  175. case IMAGE_REL_ARM_SECTION: applySecIdx(off, os); break;
  176. case IMAGE_REL_ARM_SECREL: applySecRel(this, off, os, s); break;
  177. case IMAGE_REL_ARM_REL32: add32(off, sx - p - 4); break;
  178. default:
  179. error("unsupported relocation type 0x" + Twine::utohexstr(type) + " in " +
  180. toString(file));
  181. }
  182. }
  183. // Interpret the existing immediate value as a byte offset to the
  184. // target symbol, then update the instruction with the immediate as
  185. // the page offset from the current instruction to the target.
  186. void applyArm64Addr(uint8_t *off, uint64_t s, uint64_t p, int shift) {
  187. uint32_t orig = read32le(off);
  188. uint64_t imm = ((orig >> 29) & 0x3) | ((orig >> 3) & 0x1FFFFC);
  189. s += imm;
  190. imm = (s >> shift) - (p >> shift);
  191. uint32_t immLo = (imm & 0x3) << 29;
  192. uint32_t immHi = (imm & 0x1FFFFC) << 3;
  193. uint64_t mask = (0x3 << 29) | (0x1FFFFC << 3);
  194. write32le(off, (orig & ~mask) | immLo | immHi);
  195. }
  196. // Update the immediate field in a AARCH64 ldr, str, and add instruction.
  197. // Optionally limit the range of the written immediate by one or more bits
  198. // (rangeLimit).
  199. void applyArm64Imm(uint8_t *off, uint64_t imm, uint32_t rangeLimit) {
  200. uint32_t orig = read32le(off);
  201. imm += (orig >> 10) & 0xFFF;
  202. orig &= ~(0xFFF << 10);
  203. write32le(off, orig | ((imm & (0xFFF >> rangeLimit)) << 10));
  204. }
  205. // Add the 12 bit page offset to the existing immediate.
  206. // Ldr/str instructions store the opcode immediate scaled
  207. // by the load/store size (giving a larger range for larger
  208. // loads/stores). The immediate is always (both before and after
  209. // fixing up the relocation) stored scaled similarly.
  210. // Even if larger loads/stores have a larger range, limit the
  211. // effective offset to 12 bit, since it is intended to be a
  212. // page offset.
  213. static void applyArm64Ldr(uint8_t *off, uint64_t imm) {
  214. uint32_t orig = read32le(off);
  215. uint32_t size = orig >> 30;
  216. // 0x04000000 indicates SIMD/FP registers
  217. // 0x00800000 indicates 128 bit
  218. if ((orig & 0x4800000) == 0x4800000)
  219. size += 4;
  220. if ((imm & ((1 << size) - 1)) != 0)
  221. error("misaligned ldr/str offset");
  222. applyArm64Imm(off, imm >> size, size);
  223. }
  224. static void applySecRelLow12A(const SectionChunk *sec, uint8_t *off,
  225. OutputSection *os, uint64_t s) {
  226. if (checkSecRel(sec, os))
  227. applyArm64Imm(off, (s - os->getRVA()) & 0xfff, 0);
  228. }
  229. static void applySecRelHigh12A(const SectionChunk *sec, uint8_t *off,
  230. OutputSection *os, uint64_t s) {
  231. if (!checkSecRel(sec, os))
  232. return;
  233. uint64_t secRel = (s - os->getRVA()) >> 12;
  234. if (0xfff < secRel) {
  235. error("overflow in SECREL_HIGH12A relocation in section: " +
  236. sec->getSectionName());
  237. return;
  238. }
  239. applyArm64Imm(off, secRel & 0xfff, 0);
  240. }
  241. static void applySecRelLdr(const SectionChunk *sec, uint8_t *off,
  242. OutputSection *os, uint64_t s) {
  243. if (checkSecRel(sec, os))
  244. applyArm64Ldr(off, (s - os->getRVA()) & 0xfff);
  245. }
  246. void applyArm64Branch26(uint8_t *off, int64_t v) {
  247. if (!isInt<28>(v))
  248. error("relocation out of range");
  249. or32(off, (v & 0x0FFFFFFC) >> 2);
  250. }
  251. static void applyArm64Branch19(uint8_t *off, int64_t v) {
  252. if (!isInt<21>(v))
  253. error("relocation out of range");
  254. or32(off, (v & 0x001FFFFC) << 3);
  255. }
  256. static void applyArm64Branch14(uint8_t *off, int64_t v) {
  257. if (!isInt<16>(v))
  258. error("relocation out of range");
  259. or32(off, (v & 0x0000FFFC) << 3);
  260. }
  261. void SectionChunk::applyRelARM64(uint8_t *off, uint16_t type, OutputSection *os,
  262. uint64_t s, uint64_t p) const {
  263. switch (type) {
  264. case IMAGE_REL_ARM64_PAGEBASE_REL21: applyArm64Addr(off, s, p, 12); break;
  265. case IMAGE_REL_ARM64_REL21: applyArm64Addr(off, s, p, 0); break;
  266. case IMAGE_REL_ARM64_PAGEOFFSET_12A: applyArm64Imm(off, s & 0xfff, 0); break;
  267. case IMAGE_REL_ARM64_PAGEOFFSET_12L: applyArm64Ldr(off, s & 0xfff); break;
  268. case IMAGE_REL_ARM64_BRANCH26: applyArm64Branch26(off, s - p); break;
  269. case IMAGE_REL_ARM64_BRANCH19: applyArm64Branch19(off, s - p); break;
  270. case IMAGE_REL_ARM64_BRANCH14: applyArm64Branch14(off, s - p); break;
  271. case IMAGE_REL_ARM64_ADDR32: add32(off, s + config->imageBase); break;
  272. case IMAGE_REL_ARM64_ADDR32NB: add32(off, s); break;
  273. case IMAGE_REL_ARM64_ADDR64: add64(off, s + config->imageBase); break;
  274. case IMAGE_REL_ARM64_SECREL: applySecRel(this, off, os, s); break;
  275. case IMAGE_REL_ARM64_SECREL_LOW12A: applySecRelLow12A(this, off, os, s); break;
  276. case IMAGE_REL_ARM64_SECREL_HIGH12A: applySecRelHigh12A(this, off, os, s); break;
  277. case IMAGE_REL_ARM64_SECREL_LOW12L: applySecRelLdr(this, off, os, s); break;
  278. case IMAGE_REL_ARM64_SECTION: applySecIdx(off, os); break;
  279. case IMAGE_REL_ARM64_REL32: add32(off, s - p - 4); break;
  280. default:
  281. error("unsupported relocation type 0x" + Twine::utohexstr(type) + " in " +
  282. toString(file));
  283. }
  284. }
  285. static void maybeReportRelocationToDiscarded(const SectionChunk *fromChunk,
  286. Defined *sym,
  287. const coff_relocation &rel) {
  288. // Don't report these errors when the relocation comes from a debug info
  289. // section or in mingw mode. MinGW mode object files (built by GCC) can
  290. // have leftover sections with relocations against discarded comdat
  291. // sections. Such sections are left as is, with relocations untouched.
  292. if (fromChunk->isCodeView() || fromChunk->isDWARF() || config->mingw)
  293. return;
  294. // Get the name of the symbol. If it's null, it was discarded early, so we
  295. // have to go back to the object file.
  296. ObjFile *file = fromChunk->file;
  297. StringRef name;
  298. if (sym) {
  299. name = sym->getName();
  300. } else {
  301. COFFSymbolRef coffSym =
  302. check(file->getCOFFObj()->getSymbol(rel.SymbolTableIndex));
  303. file->getCOFFObj()->getSymbolName(coffSym, name);
  304. }
  305. std::vector<std::string> symbolLocations =
  306. getSymbolLocations(file, rel.SymbolTableIndex);
  307. std::string out;
  308. llvm::raw_string_ostream os(out);
  309. os << "relocation against symbol in discarded section: " + name;
  310. for (const std::string &s : symbolLocations)
  311. os << s;
  312. error(os.str());
  313. }
  314. void SectionChunk::writeTo(uint8_t *buf) const {
  315. if (!hasData)
  316. return;
  317. // Copy section contents from source object file to output file.
  318. ArrayRef<uint8_t> a = getContents();
  319. if (!a.empty())
  320. memcpy(buf, a.data(), a.size());
  321. // Apply relocations.
  322. size_t inputSize = getSize();
  323. for (size_t i = 0, e = relocsSize; i < e; i++) {
  324. const coff_relocation &rel = relocsData[i];
  325. // Check for an invalid relocation offset. This check isn't perfect, because
  326. // we don't have the relocation size, which is only known after checking the
  327. // machine and relocation type. As a result, a relocation may overwrite the
  328. // beginning of the following input section.
  329. if (rel.VirtualAddress >= inputSize) {
  330. error("relocation points beyond the end of its parent section");
  331. continue;
  332. }
  333. uint8_t *off = buf + rel.VirtualAddress;
  334. auto *sym =
  335. dyn_cast_or_null<Defined>(file->getSymbol(rel.SymbolTableIndex));
  336. // Get the output section of the symbol for this relocation. The output
  337. // section is needed to compute SECREL and SECTION relocations used in debug
  338. // info.
  339. Chunk *c = sym ? sym->getChunk() : nullptr;
  340. OutputSection *os = c ? c->getOutputSection() : nullptr;
  341. // Skip the relocation if it refers to a discarded section, and diagnose it
  342. // as an error if appropriate. If a symbol was discarded early, it may be
  343. // null. If it was discarded late, the output section will be null, unless
  344. // it was an absolute or synthetic symbol.
  345. if (!sym ||
  346. (!os && !isa<DefinedAbsolute>(sym) && !isa<DefinedSynthetic>(sym))) {
  347. maybeReportRelocationToDiscarded(this, sym, rel);
  348. continue;
  349. }
  350. uint64_t s = sym->getRVA();
  351. // Compute the RVA of the relocation for relative relocations.
  352. uint64_t p = rva + rel.VirtualAddress;
  353. switch (config->machine) {
  354. case AMD64:
  355. applyRelX64(off, rel.Type, os, s, p);
  356. break;
  357. case I386:
  358. applyRelX86(off, rel.Type, os, s, p);
  359. break;
  360. case ARMNT:
  361. applyRelARM(off, rel.Type, os, s, p);
  362. break;
  363. case ARM64:
  364. applyRelARM64(off, rel.Type, os, s, p);
  365. break;
  366. default:
  367. llvm_unreachable("unknown machine type");
  368. }
  369. }
  370. }
  371. void SectionChunk::addAssociative(SectionChunk *child) {
  372. // Insert this child at the head of the list.
  373. assert(child->assocChildren == nullptr &&
  374. "associated sections cannot have their own associated children");
  375. child->assocChildren = assocChildren;
  376. assocChildren = child;
  377. }
  378. static uint8_t getBaserelType(const coff_relocation &rel) {
  379. switch (config->machine) {
  380. case AMD64:
  381. if (rel.Type == IMAGE_REL_AMD64_ADDR64)
  382. return IMAGE_REL_BASED_DIR64;
  383. return IMAGE_REL_BASED_ABSOLUTE;
  384. case I386:
  385. if (rel.Type == IMAGE_REL_I386_DIR32)
  386. return IMAGE_REL_BASED_HIGHLOW;
  387. return IMAGE_REL_BASED_ABSOLUTE;
  388. case ARMNT:
  389. if (rel.Type == IMAGE_REL_ARM_ADDR32)
  390. return IMAGE_REL_BASED_HIGHLOW;
  391. if (rel.Type == IMAGE_REL_ARM_MOV32T)
  392. return IMAGE_REL_BASED_ARM_MOV32T;
  393. return IMAGE_REL_BASED_ABSOLUTE;
  394. case ARM64:
  395. if (rel.Type == IMAGE_REL_ARM64_ADDR64)
  396. return IMAGE_REL_BASED_DIR64;
  397. return IMAGE_REL_BASED_ABSOLUTE;
  398. default:
  399. llvm_unreachable("unknown machine type");
  400. }
  401. }
  402. // Windows-specific.
  403. // Collect all locations that contain absolute addresses, which need to be
  404. // fixed by the loader if load-time relocation is needed.
  405. // Only called when base relocation is enabled.
  406. void SectionChunk::getBaserels(std::vector<Baserel> *res) {
  407. for (size_t i = 0, e = relocsSize; i < e; i++) {
  408. const coff_relocation &rel = relocsData[i];
  409. uint8_t ty = getBaserelType(rel);
  410. if (ty == IMAGE_REL_BASED_ABSOLUTE)
  411. continue;
  412. Symbol *target = file->getSymbol(rel.SymbolTableIndex);
  413. if (!target || isa<DefinedAbsolute>(target))
  414. continue;
  415. res->emplace_back(rva + rel.VirtualAddress, ty);
  416. }
  417. }
  418. // MinGW specific.
  419. // Check whether a static relocation of type Type can be deferred and
  420. // handled at runtime as a pseudo relocation (for references to a module
  421. // local variable, which turned out to actually need to be imported from
  422. // another DLL) This returns the size the relocation is supposed to update,
  423. // in bits, or 0 if the relocation cannot be handled as a runtime pseudo
  424. // relocation.
  425. static int getRuntimePseudoRelocSize(uint16_t type) {
  426. // Relocations that either contain an absolute address, or a plain
  427. // relative offset, since the runtime pseudo reloc implementation
  428. // adds 8/16/32/64 bit values to a memory address.
  429. //
  430. // Given a pseudo relocation entry,
  431. //
  432. // typedef struct {
  433. // DWORD sym;
  434. // DWORD target;
  435. // DWORD flags;
  436. // } runtime_pseudo_reloc_item_v2;
  437. //
  438. // the runtime relocation performs this adjustment:
  439. // *(base + .target) += *(base + .sym) - (base + .sym)
  440. //
  441. // This works for both absolute addresses (IMAGE_REL_*_ADDR32/64,
  442. // IMAGE_REL_I386_DIR32, where the memory location initially contains
  443. // the address of the IAT slot, and for relative addresses (IMAGE_REL*_REL32),
  444. // where the memory location originally contains the relative offset to the
  445. // IAT slot.
  446. //
  447. // This requires the target address to be writable, either directly out of
  448. // the image, or temporarily changed at runtime with VirtualProtect.
  449. // Since this only operates on direct address values, it doesn't work for
  450. // ARM/ARM64 relocations, other than the plain ADDR32/ADDR64 relocations.
  451. switch (config->machine) {
  452. case AMD64:
  453. switch (type) {
  454. case IMAGE_REL_AMD64_ADDR64:
  455. return 64;
  456. case IMAGE_REL_AMD64_ADDR32:
  457. case IMAGE_REL_AMD64_REL32:
  458. case IMAGE_REL_AMD64_REL32_1:
  459. case IMAGE_REL_AMD64_REL32_2:
  460. case IMAGE_REL_AMD64_REL32_3:
  461. case IMAGE_REL_AMD64_REL32_4:
  462. case IMAGE_REL_AMD64_REL32_5:
  463. return 32;
  464. default:
  465. return 0;
  466. }
  467. case I386:
  468. switch (type) {
  469. case IMAGE_REL_I386_DIR32:
  470. case IMAGE_REL_I386_REL32:
  471. return 32;
  472. default:
  473. return 0;
  474. }
  475. case ARMNT:
  476. switch (type) {
  477. case IMAGE_REL_ARM_ADDR32:
  478. return 32;
  479. default:
  480. return 0;
  481. }
  482. case ARM64:
  483. switch (type) {
  484. case IMAGE_REL_ARM64_ADDR64:
  485. return 64;
  486. case IMAGE_REL_ARM64_ADDR32:
  487. return 32;
  488. default:
  489. return 0;
  490. }
  491. default:
  492. llvm_unreachable("unknown machine type");
  493. }
  494. }
  495. // MinGW specific.
  496. // Append information to the provided vector about all relocations that
  497. // need to be handled at runtime as runtime pseudo relocations (references
  498. // to a module local variable, which turned out to actually need to be
  499. // imported from another DLL).
  500. void SectionChunk::getRuntimePseudoRelocs(
  501. std::vector<RuntimePseudoReloc> &res) {
  502. for (const coff_relocation &rel : getRelocs()) {
  503. auto *target =
  504. dyn_cast_or_null<Defined>(file->getSymbol(rel.SymbolTableIndex));
  505. if (!target || !target->isRuntimePseudoReloc)
  506. continue;
  507. int sizeInBits = getRuntimePseudoRelocSize(rel.Type);
  508. if (sizeInBits == 0) {
  509. error("unable to automatically import from " + target->getName() +
  510. " with relocation type " +
  511. file->getCOFFObj()->getRelocationTypeName(rel.Type) + " in " +
  512. toString(file));
  513. continue;
  514. }
  515. // sizeInBits is used to initialize the Flags field; currently no
  516. // other flags are defined.
  517. res.emplace_back(
  518. RuntimePseudoReloc(target, this, rel.VirtualAddress, sizeInBits));
  519. }
  520. }
  521. bool SectionChunk::isCOMDAT() const {
  522. return header->Characteristics & IMAGE_SCN_LNK_COMDAT;
  523. }
  524. void SectionChunk::printDiscardedMessage() const {
  525. // Removed by dead-stripping. If it's removed by ICF, ICF already
  526. // printed out the name, so don't repeat that here.
  527. if (sym && this == repl)
  528. message("Discarded " + sym->getName());
  529. }
  530. StringRef SectionChunk::getDebugName() const {
  531. if (sym)
  532. return sym->getName();
  533. return "";
  534. }
  535. ArrayRef<uint8_t> SectionChunk::getContents() const {
  536. ArrayRef<uint8_t> a;
  537. cantFail(file->getCOFFObj()->getSectionContents(header, a));
  538. return a;
  539. }
  540. ArrayRef<uint8_t> SectionChunk::consumeDebugMagic() {
  541. assert(isCodeView());
  542. return consumeDebugMagic(getContents(), getSectionName());
  543. }
  544. ArrayRef<uint8_t> SectionChunk::consumeDebugMagic(ArrayRef<uint8_t> data,
  545. StringRef sectionName) {
  546. if (data.empty())
  547. return {};
  548. // First 4 bytes are section magic.
  549. if (data.size() < 4)
  550. fatal("the section is too short: " + sectionName);
  551. if (!sectionName.startswith(".debug$"))
  552. fatal("invalid section: " + sectionName);
  553. uint32_t magic = support::endian::read32le(data.data());
  554. uint32_t expectedMagic = sectionName == ".debug$H"
  555. ? DEBUG_HASHES_SECTION_MAGIC
  556. : DEBUG_SECTION_MAGIC;
  557. if (magic != expectedMagic) {
  558. warn("ignoring section " + sectionName + " with unrecognized magic 0x" +
  559. utohexstr(magic));
  560. return {};
  561. }
  562. return data.slice(4);
  563. }
  564. SectionChunk *SectionChunk::findByName(ArrayRef<SectionChunk *> sections,
  565. StringRef name) {
  566. for (SectionChunk *c : sections)
  567. if (c->getSectionName() == name)
  568. return c;
  569. return nullptr;
  570. }
  571. void SectionChunk::replace(SectionChunk *other) {
  572. p2Align = std::max(p2Align, other->p2Align);
  573. other->repl = repl;
  574. other->live = false;
  575. }
  576. uint32_t SectionChunk::getSectionNumber() const {
  577. DataRefImpl r;
  578. r.p = reinterpret_cast<uintptr_t>(header);
  579. SectionRef s(r, file->getCOFFObj());
  580. return s.getIndex() + 1;
  581. }
  582. CommonChunk::CommonChunk(const COFFSymbolRef s) : sym(s) {
  583. // The value of a common symbol is its size. Align all common symbols smaller
  584. // than 32 bytes naturally, i.e. round the size up to the next power of two.
  585. // This is what MSVC link.exe does.
  586. setAlignment(std::min(32U, uint32_t(PowerOf2Ceil(sym.getValue()))));
  587. hasData = false;
  588. }
  589. uint32_t CommonChunk::getOutputCharacteristics() const {
  590. return IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ |
  591. IMAGE_SCN_MEM_WRITE;
  592. }
  593. void StringChunk::writeTo(uint8_t *buf) const {
  594. memcpy(buf, str.data(), str.size());
  595. buf[str.size()] = '\0';
  596. }
  597. ImportThunkChunkX64::ImportThunkChunkX64(Defined *s) : ImportThunkChunk(s) {
  598. // Intel Optimization Manual says that all branch targets
  599. // should be 16-byte aligned. MSVC linker does this too.
  600. setAlignment(16);
  601. }
  602. void ImportThunkChunkX64::writeTo(uint8_t *buf) const {
  603. memcpy(buf, importThunkX86, sizeof(importThunkX86));
  604. // The first two bytes is a JMP instruction. Fill its operand.
  605. write32le(buf + 2, impSymbol->getRVA() - rva - getSize());
  606. }
  607. void ImportThunkChunkX86::getBaserels(std::vector<Baserel> *res) {
  608. res->emplace_back(getRVA() + 2);
  609. }
  610. void ImportThunkChunkX86::writeTo(uint8_t *buf) const {
  611. memcpy(buf, importThunkX86, sizeof(importThunkX86));
  612. // The first two bytes is a JMP instruction. Fill its operand.
  613. write32le(buf + 2,
  614. impSymbol->getRVA() + config->imageBase);
  615. }
  616. void ImportThunkChunkARM::getBaserels(std::vector<Baserel> *res) {
  617. res->emplace_back(getRVA(), IMAGE_REL_BASED_ARM_MOV32T);
  618. }
  619. void ImportThunkChunkARM::writeTo(uint8_t *buf) const {
  620. memcpy(buf, importThunkARM, sizeof(importThunkARM));
  621. // Fix mov.w and mov.t operands.
  622. applyMOV32T(buf, impSymbol->getRVA() + config->imageBase);
  623. }
  624. void ImportThunkChunkARM64::writeTo(uint8_t *buf) const {
  625. int64_t off = impSymbol->getRVA() & 0xfff;
  626. memcpy(buf, importThunkARM64, sizeof(importThunkARM64));
  627. applyArm64Addr(buf, impSymbol->getRVA(), rva, 12);
  628. applyArm64Ldr(buf + 4, off);
  629. }
  630. // A Thumb2, PIC, non-interworking range extension thunk.
  631. const uint8_t armThunk[] = {
  632. 0x40, 0xf2, 0x00, 0x0c, // P: movw ip,:lower16:S - (P + (L1-P) + 4)
  633. 0xc0, 0xf2, 0x00, 0x0c, // movt ip,:upper16:S - (P + (L1-P) + 4)
  634. 0xe7, 0x44, // L1: add pc, ip
  635. };
  636. size_t RangeExtensionThunkARM::getSize() const {
  637. assert(config->machine == ARMNT);
  638. return sizeof(armThunk);
  639. }
  640. void RangeExtensionThunkARM::writeTo(uint8_t *buf) const {
  641. assert(config->machine == ARMNT);
  642. uint64_t offset = target->getRVA() - rva - 12;
  643. memcpy(buf, armThunk, sizeof(armThunk));
  644. applyMOV32T(buf, uint32_t(offset));
  645. }
  646. // A position independent ARM64 adrp+add thunk, with a maximum range of
  647. // +/- 4 GB, which is enough for any PE-COFF.
  648. const uint8_t arm64Thunk[] = {
  649. 0x10, 0x00, 0x00, 0x90, // adrp x16, Dest
  650. 0x10, 0x02, 0x00, 0x91, // add x16, x16, :lo12:Dest
  651. 0x00, 0x02, 0x1f, 0xd6, // br x16
  652. };
  653. size_t RangeExtensionThunkARM64::getSize() const {
  654. assert(config->machine == ARM64);
  655. return sizeof(arm64Thunk);
  656. }
  657. void RangeExtensionThunkARM64::writeTo(uint8_t *buf) const {
  658. assert(config->machine == ARM64);
  659. memcpy(buf, arm64Thunk, sizeof(arm64Thunk));
  660. applyArm64Addr(buf + 0, target->getRVA(), rva, 12);
  661. applyArm64Imm(buf + 4, target->getRVA() & 0xfff, 0);
  662. }
  663. void LocalImportChunk::getBaserels(std::vector<Baserel> *res) {
  664. res->emplace_back(getRVA());
  665. }
  666. size_t LocalImportChunk::getSize() const { return config->wordsize; }
  667. void LocalImportChunk::writeTo(uint8_t *buf) const {
  668. if (config->is64()) {
  669. write64le(buf, sym->getRVA() + config->imageBase);
  670. } else {
  671. write32le(buf, sym->getRVA() + config->imageBase);
  672. }
  673. }
  674. void RVATableChunk::writeTo(uint8_t *buf) const {
  675. ulittle32_t *begin = reinterpret_cast<ulittle32_t *>(buf);
  676. size_t cnt = 0;
  677. for (const ChunkAndOffset &co : syms)
  678. begin[cnt++] = co.inputChunk->getRVA() + co.offset;
  679. std::sort(begin, begin + cnt);
  680. assert(std::unique(begin, begin + cnt) == begin + cnt &&
  681. "RVA tables should be de-duplicated");
  682. }
  683. // MinGW specific, for the "automatic import of variables from DLLs" feature.
  684. size_t PseudoRelocTableChunk::getSize() const {
  685. if (relocs.empty())
  686. return 0;
  687. return 12 + 12 * relocs.size();
  688. }
  689. // MinGW specific.
  690. void PseudoRelocTableChunk::writeTo(uint8_t *buf) const {
  691. if (relocs.empty())
  692. return;
  693. ulittle32_t *table = reinterpret_cast<ulittle32_t *>(buf);
  694. // This is the list header, to signal the runtime pseudo relocation v2
  695. // format.
  696. table[0] = 0;
  697. table[1] = 0;
  698. table[2] = 1;
  699. size_t idx = 3;
  700. for (const RuntimePseudoReloc &rpr : relocs) {
  701. table[idx + 0] = rpr.sym->getRVA();
  702. table[idx + 1] = rpr.target->getRVA() + rpr.targetOffset;
  703. table[idx + 2] = rpr.flags;
  704. idx += 3;
  705. }
  706. }
  707. // Windows-specific. This class represents a block in .reloc section.
  708. // The format is described here.
  709. //
  710. // On Windows, each DLL is linked against a fixed base address and
  711. // usually loaded to that address. However, if there's already another
  712. // DLL that overlaps, the loader has to relocate it. To do that, DLLs
  713. // contain .reloc sections which contain offsets that need to be fixed
  714. // up at runtime. If the loader finds that a DLL cannot be loaded to its
  715. // desired base address, it loads it to somewhere else, and add <actual
  716. // base address> - <desired base address> to each offset that is
  717. // specified by the .reloc section. In ELF terms, .reloc sections
  718. // contain relative relocations in REL format (as opposed to RELA.)
  719. //
  720. // This already significantly reduces the size of relocations compared
  721. // to ELF .rel.dyn, but Windows does more to reduce it (probably because
  722. // it was invented for PCs in the late '80s or early '90s.) Offsets in
  723. // .reloc are grouped by page where the page size is 12 bits, and
  724. // offsets sharing the same page address are stored consecutively to
  725. // represent them with less space. This is very similar to the page
  726. // table which is grouped by (multiple stages of) pages.
  727. //
  728. // For example, let's say we have 0x00030, 0x00500, 0x00700, 0x00A00,
  729. // 0x20004, and 0x20008 in a .reloc section for x64. The uppermost 4
  730. // bits have a type IMAGE_REL_BASED_DIR64 or 0xA. In the section, they
  731. // are represented like this:
  732. //
  733. // 0x00000 -- page address (4 bytes)
  734. // 16 -- size of this block (4 bytes)
  735. // 0xA030 -- entries (2 bytes each)
  736. // 0xA500
  737. // 0xA700
  738. // 0xAA00
  739. // 0x20000 -- page address (4 bytes)
  740. // 12 -- size of this block (4 bytes)
  741. // 0xA004 -- entries (2 bytes each)
  742. // 0xA008
  743. //
  744. // Usually we have a lot of relocations for each page, so the number of
  745. // bytes for one .reloc entry is close to 2 bytes on average.
  746. BaserelChunk::BaserelChunk(uint32_t page, Baserel *begin, Baserel *end) {
  747. // Block header consists of 4 byte page RVA and 4 byte block size.
  748. // Each entry is 2 byte. Last entry may be padding.
  749. data.resize(alignTo((end - begin) * 2 + 8, 4));
  750. uint8_t *p = data.data();
  751. write32le(p, page);
  752. write32le(p + 4, data.size());
  753. p += 8;
  754. for (Baserel *i = begin; i != end; ++i) {
  755. write16le(p, (i->type << 12) | (i->rva - page));
  756. p += 2;
  757. }
  758. }
  759. void BaserelChunk::writeTo(uint8_t *buf) const {
  760. memcpy(buf, data.data(), data.size());
  761. }
  762. uint8_t Baserel::getDefaultType() {
  763. switch (config->machine) {
  764. case AMD64:
  765. case ARM64:
  766. return IMAGE_REL_BASED_DIR64;
  767. case I386:
  768. case ARMNT:
  769. return IMAGE_REL_BASED_HIGHLOW;
  770. default:
  771. llvm_unreachable("unknown machine type");
  772. }
  773. }
  774. MergeChunk *MergeChunk::instances[Log2MaxSectionAlignment + 1] = {};
  775. MergeChunk::MergeChunk(uint32_t alignment)
  776. : builder(StringTableBuilder::RAW, alignment) {
  777. setAlignment(alignment);
  778. }
  779. void MergeChunk::addSection(SectionChunk *c) {
  780. assert(isPowerOf2_32(c->getAlignment()));
  781. uint8_t p2Align = llvm::Log2_32(c->getAlignment());
  782. assert(p2Align < array_lengthof(instances));
  783. auto *&mc = instances[p2Align];
  784. if (!mc)
  785. mc = make<MergeChunk>(c->getAlignment());
  786. mc->sections.push_back(c);
  787. }
  788. void MergeChunk::finalizeContents() {
  789. assert(!finalized && "should only finalize once");
  790. for (SectionChunk *c : sections)
  791. if (c->live)
  792. builder.add(toStringRef(c->getContents()));
  793. builder.finalize();
  794. finalized = true;
  795. }
  796. void MergeChunk::assignSubsectionRVAs() {
  797. for (SectionChunk *c : sections) {
  798. if (!c->live)
  799. continue;
  800. size_t off = builder.getOffset(toStringRef(c->getContents()));
  801. c->setRVA(rva + off);
  802. }
  803. }
  804. uint32_t MergeChunk::getOutputCharacteristics() const {
  805. return IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA;
  806. }
  807. size_t MergeChunk::getSize() const {
  808. return builder.getSize();
  809. }
  810. void MergeChunk::writeTo(uint8_t *buf) const {
  811. builder.write(buf);
  812. }
  813. // MinGW specific.
  814. size_t AbsolutePointerChunk::getSize() const { return config->wordsize; }
  815. void AbsolutePointerChunk::writeTo(uint8_t *buf) const {
  816. if (config->is64()) {
  817. write64le(buf, value);
  818. } else {
  819. write32le(buf, value);
  820. }
  821. }
  822. } // namespace coff
  823. } // namespace lld