COFFWriter.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. //===- COFFWriter.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 "COFFWriter.h"
  9. #include "COFFObject.h"
  10. #include "llvm/ADT/ArrayRef.h"
  11. #include "llvm/ADT/StringRef.h"
  12. #include "llvm/BinaryFormat/COFF.h"
  13. #include "llvm/Object/COFF.h"
  14. #include "llvm/Support/Errc.h"
  15. #include "llvm/Support/ErrorHandling.h"
  16. #include <cstddef>
  17. #include <cstdint>
  18. namespace llvm {
  19. namespace objcopy {
  20. namespace coff {
  21. using namespace object;
  22. using namespace COFF;
  23. Error COFFWriter::finalizeRelocTargets() {
  24. for (Section &Sec : Obj.getMutableSections()) {
  25. for (Relocation &R : Sec.Relocs) {
  26. const Symbol *Sym = Obj.findSymbol(R.Target);
  27. if (Sym == nullptr)
  28. return createStringError(object_error::invalid_symbol_index,
  29. "relocation target '%s' (%zu) not found",
  30. R.TargetName.str().c_str(), R.Target);
  31. R.Reloc.SymbolTableIndex = Sym->RawIndex;
  32. }
  33. }
  34. return Error::success();
  35. }
  36. Error COFFWriter::finalizeSymbolContents() {
  37. for (Symbol &Sym : Obj.getMutableSymbols()) {
  38. if (Sym.TargetSectionId <= 0) {
  39. // Undefined, or a special kind of symbol. These negative values
  40. // are stored in the SectionNumber field which is unsigned.
  41. Sym.Sym.SectionNumber = static_cast<uint32_t>(Sym.TargetSectionId);
  42. } else {
  43. const Section *Sec = Obj.findSection(Sym.TargetSectionId);
  44. if (Sec == nullptr)
  45. return createStringError(object_error::invalid_symbol_index,
  46. "symbol '%s' points to a removed section",
  47. Sym.Name.str().c_str());
  48. Sym.Sym.SectionNumber = Sec->Index;
  49. if (Sym.Sym.NumberOfAuxSymbols == 1 &&
  50. Sym.Sym.StorageClass == IMAGE_SYM_CLASS_STATIC) {
  51. coff_aux_section_definition *SD =
  52. reinterpret_cast<coff_aux_section_definition *>(
  53. Sym.AuxData[0].Opaque);
  54. uint32_t SDSectionNumber;
  55. if (Sym.AssociativeComdatTargetSectionId == 0) {
  56. // Not a comdat associative section; just set the Number field to
  57. // the number of the section itself.
  58. SDSectionNumber = Sec->Index;
  59. } else {
  60. Sec = Obj.findSection(Sym.AssociativeComdatTargetSectionId);
  61. if (Sec == nullptr)
  62. return createStringError(
  63. object_error::invalid_symbol_index,
  64. "symbol '%s' is associative to a removed section",
  65. Sym.Name.str().c_str());
  66. SDSectionNumber = Sec->Index;
  67. }
  68. // Update the section definition with the new section number.
  69. SD->NumberLowPart = static_cast<uint16_t>(SDSectionNumber);
  70. SD->NumberHighPart = static_cast<uint16_t>(SDSectionNumber >> 16);
  71. }
  72. }
  73. // Check that we actually have got AuxData to match the weak symbol target
  74. // we want to set. Only >= 1 would be required, but only == 1 makes sense.
  75. if (Sym.WeakTargetSymbolId && Sym.Sym.NumberOfAuxSymbols == 1) {
  76. coff_aux_weak_external *WE =
  77. reinterpret_cast<coff_aux_weak_external *>(Sym.AuxData[0].Opaque);
  78. const Symbol *Target = Obj.findSymbol(*Sym.WeakTargetSymbolId);
  79. if (Target == nullptr)
  80. return createStringError(object_error::invalid_symbol_index,
  81. "symbol '%s' is missing its weak target",
  82. Sym.Name.str().c_str());
  83. WE->TagIndex = Target->RawIndex;
  84. }
  85. }
  86. return Error::success();
  87. }
  88. void COFFWriter::layoutSections() {
  89. for (auto &S : Obj.getMutableSections()) {
  90. if (S.Header.SizeOfRawData > 0)
  91. S.Header.PointerToRawData = FileSize;
  92. else
  93. S.Header.PointerToRawData = 0;
  94. FileSize += S.Header.SizeOfRawData; // For executables, this is already
  95. // aligned to FileAlignment.
  96. if (S.Relocs.size() >= 0xffff) {
  97. S.Header.Characteristics |= COFF::IMAGE_SCN_LNK_NRELOC_OVFL;
  98. S.Header.NumberOfRelocations = 0xffff;
  99. S.Header.PointerToRelocations = FileSize;
  100. FileSize += sizeof(coff_relocation);
  101. } else {
  102. S.Header.NumberOfRelocations = S.Relocs.size();
  103. S.Header.PointerToRelocations = S.Relocs.size() ? FileSize : 0;
  104. }
  105. FileSize += S.Relocs.size() * sizeof(coff_relocation);
  106. FileSize = alignTo(FileSize, FileAlignment);
  107. if (S.Header.Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA)
  108. SizeOfInitializedData += S.Header.SizeOfRawData;
  109. }
  110. }
  111. Expected<size_t> COFFWriter::finalizeStringTable() {
  112. for (const auto &S : Obj.getSections())
  113. if (S.Name.size() > COFF::NameSize)
  114. StrTabBuilder.add(S.Name);
  115. for (const auto &S : Obj.getSymbols())
  116. if (S.Name.size() > COFF::NameSize)
  117. StrTabBuilder.add(S.Name);
  118. StrTabBuilder.finalize();
  119. for (auto &S : Obj.getMutableSections()) {
  120. memset(S.Header.Name, 0, sizeof(S.Header.Name));
  121. if (S.Name.size() <= COFF::NameSize) {
  122. // Short names can go in the field directly.
  123. memcpy(S.Header.Name, S.Name.data(), S.Name.size());
  124. } else {
  125. // Offset of the section name in the string table.
  126. size_t Offset = StrTabBuilder.getOffset(S.Name);
  127. if (!COFF::encodeSectionName(S.Header.Name, Offset))
  128. return createStringError(object_error::invalid_section_index,
  129. "COFF string table is greater than 64GB, "
  130. "unable to encode section name offset");
  131. }
  132. }
  133. for (auto &S : Obj.getMutableSymbols()) {
  134. if (S.Name.size() > COFF::NameSize) {
  135. S.Sym.Name.Offset.Zeroes = 0;
  136. S.Sym.Name.Offset.Offset = StrTabBuilder.getOffset(S.Name);
  137. } else {
  138. strncpy(S.Sym.Name.ShortName, S.Name.data(), COFF::NameSize);
  139. }
  140. }
  141. return StrTabBuilder.getSize();
  142. }
  143. template <class SymbolTy>
  144. std::pair<size_t, size_t> COFFWriter::finalizeSymbolTable() {
  145. size_t RawSymIndex = 0;
  146. for (auto &S : Obj.getMutableSymbols()) {
  147. // Symbols normally have NumberOfAuxSymbols set correctly all the time.
  148. // For file symbols, we need to know the output file's symbol size to be
  149. // able to calculate the number of slots it occupies.
  150. if (!S.AuxFile.empty())
  151. S.Sym.NumberOfAuxSymbols =
  152. alignTo(S.AuxFile.size(), sizeof(SymbolTy)) / sizeof(SymbolTy);
  153. S.RawIndex = RawSymIndex;
  154. RawSymIndex += 1 + S.Sym.NumberOfAuxSymbols;
  155. }
  156. return std::make_pair(RawSymIndex * sizeof(SymbolTy), sizeof(SymbolTy));
  157. }
  158. Error COFFWriter::finalize(bool IsBigObj) {
  159. size_t SymTabSize, SymbolSize;
  160. std::tie(SymTabSize, SymbolSize) = IsBigObj
  161. ? finalizeSymbolTable<coff_symbol32>()
  162. : finalizeSymbolTable<coff_symbol16>();
  163. if (Error E = finalizeRelocTargets())
  164. return E;
  165. if (Error E = finalizeSymbolContents())
  166. return E;
  167. size_t SizeOfHeaders = 0;
  168. FileAlignment = 1;
  169. size_t PeHeaderSize = 0;
  170. if (Obj.IsPE) {
  171. Obj.DosHeader.AddressOfNewExeHeader =
  172. sizeof(Obj.DosHeader) + Obj.DosStub.size();
  173. SizeOfHeaders += Obj.DosHeader.AddressOfNewExeHeader + sizeof(PEMagic);
  174. FileAlignment = Obj.PeHeader.FileAlignment;
  175. Obj.PeHeader.NumberOfRvaAndSize = Obj.DataDirectories.size();
  176. PeHeaderSize = Obj.Is64 ? sizeof(pe32plus_header) : sizeof(pe32_header);
  177. SizeOfHeaders +=
  178. PeHeaderSize + sizeof(data_directory) * Obj.DataDirectories.size();
  179. }
  180. Obj.CoffFileHeader.NumberOfSections = Obj.getSections().size();
  181. SizeOfHeaders +=
  182. IsBigObj ? sizeof(coff_bigobj_file_header) : sizeof(coff_file_header);
  183. SizeOfHeaders += sizeof(coff_section) * Obj.getSections().size();
  184. SizeOfHeaders = alignTo(SizeOfHeaders, FileAlignment);
  185. Obj.CoffFileHeader.SizeOfOptionalHeader =
  186. PeHeaderSize + sizeof(data_directory) * Obj.DataDirectories.size();
  187. FileSize = SizeOfHeaders;
  188. SizeOfInitializedData = 0;
  189. layoutSections();
  190. if (Obj.IsPE) {
  191. Obj.PeHeader.SizeOfHeaders = SizeOfHeaders;
  192. Obj.PeHeader.SizeOfInitializedData = SizeOfInitializedData;
  193. if (!Obj.getSections().empty()) {
  194. const Section &S = Obj.getSections().back();
  195. Obj.PeHeader.SizeOfImage =
  196. alignTo(S.Header.VirtualAddress + S.Header.VirtualSize,
  197. Obj.PeHeader.SectionAlignment);
  198. }
  199. // If the PE header had a checksum, clear it, since it isn't valid
  200. // any longer. (We don't calculate a new one.)
  201. Obj.PeHeader.CheckSum = 0;
  202. }
  203. Expected<size_t> StrTabSizeOrErr = finalizeStringTable();
  204. if (!StrTabSizeOrErr)
  205. return StrTabSizeOrErr.takeError();
  206. size_t StrTabSize = *StrTabSizeOrErr;
  207. size_t PointerToSymbolTable = FileSize;
  208. // StrTabSize <= 4 is the size of an empty string table, only consisting
  209. // of the length field.
  210. if (SymTabSize == 0 && StrTabSize <= 4 && Obj.IsPE) {
  211. // For executables, don't point to the symbol table and skip writing
  212. // the length field, if both the symbol and string tables are empty.
  213. PointerToSymbolTable = 0;
  214. StrTabSize = 0;
  215. }
  216. size_t NumRawSymbols = SymTabSize / SymbolSize;
  217. Obj.CoffFileHeader.PointerToSymbolTable = PointerToSymbolTable;
  218. Obj.CoffFileHeader.NumberOfSymbols = NumRawSymbols;
  219. FileSize += SymTabSize + StrTabSize;
  220. FileSize = alignTo(FileSize, FileAlignment);
  221. return Error::success();
  222. }
  223. void COFFWriter::writeHeaders(bool IsBigObj) {
  224. uint8_t *Ptr = reinterpret_cast<uint8_t *>(Buf->getBufferStart());
  225. if (Obj.IsPE) {
  226. memcpy(Ptr, &Obj.DosHeader, sizeof(Obj.DosHeader));
  227. Ptr += sizeof(Obj.DosHeader);
  228. memcpy(Ptr, Obj.DosStub.data(), Obj.DosStub.size());
  229. Ptr += Obj.DosStub.size();
  230. memcpy(Ptr, PEMagic, sizeof(PEMagic));
  231. Ptr += sizeof(PEMagic);
  232. }
  233. if (!IsBigObj) {
  234. memcpy(Ptr, &Obj.CoffFileHeader, sizeof(Obj.CoffFileHeader));
  235. Ptr += sizeof(Obj.CoffFileHeader);
  236. } else {
  237. // Generate a coff_bigobj_file_header, filling it in with the values
  238. // from Obj.CoffFileHeader. All extra fields that don't exist in
  239. // coff_file_header can be set to hardcoded values.
  240. coff_bigobj_file_header BigObjHeader;
  241. BigObjHeader.Sig1 = IMAGE_FILE_MACHINE_UNKNOWN;
  242. BigObjHeader.Sig2 = 0xffff;
  243. BigObjHeader.Version = BigObjHeader::MinBigObjectVersion;
  244. BigObjHeader.Machine = Obj.CoffFileHeader.Machine;
  245. BigObjHeader.TimeDateStamp = Obj.CoffFileHeader.TimeDateStamp;
  246. memcpy(BigObjHeader.UUID, BigObjMagic, sizeof(BigObjMagic));
  247. BigObjHeader.unused1 = 0;
  248. BigObjHeader.unused2 = 0;
  249. BigObjHeader.unused3 = 0;
  250. BigObjHeader.unused4 = 0;
  251. // The value in Obj.CoffFileHeader.NumberOfSections is truncated, thus
  252. // get the original one instead.
  253. BigObjHeader.NumberOfSections = Obj.getSections().size();
  254. BigObjHeader.PointerToSymbolTable = Obj.CoffFileHeader.PointerToSymbolTable;
  255. BigObjHeader.NumberOfSymbols = Obj.CoffFileHeader.NumberOfSymbols;
  256. memcpy(Ptr, &BigObjHeader, sizeof(BigObjHeader));
  257. Ptr += sizeof(BigObjHeader);
  258. }
  259. if (Obj.IsPE) {
  260. if (Obj.Is64) {
  261. memcpy(Ptr, &Obj.PeHeader, sizeof(Obj.PeHeader));
  262. Ptr += sizeof(Obj.PeHeader);
  263. } else {
  264. pe32_header PeHeader;
  265. copyPeHeader(PeHeader, Obj.PeHeader);
  266. // The pe32plus_header (stored in Object) lacks the BaseOfData field.
  267. PeHeader.BaseOfData = Obj.BaseOfData;
  268. memcpy(Ptr, &PeHeader, sizeof(PeHeader));
  269. Ptr += sizeof(PeHeader);
  270. }
  271. for (const auto &DD : Obj.DataDirectories) {
  272. memcpy(Ptr, &DD, sizeof(DD));
  273. Ptr += sizeof(DD);
  274. }
  275. }
  276. for (const auto &S : Obj.getSections()) {
  277. memcpy(Ptr, &S.Header, sizeof(S.Header));
  278. Ptr += sizeof(S.Header);
  279. }
  280. }
  281. void COFFWriter::writeSections() {
  282. for (const auto &S : Obj.getSections()) {
  283. uint8_t *Ptr = reinterpret_cast<uint8_t *>(Buf->getBufferStart()) +
  284. S.Header.PointerToRawData;
  285. ArrayRef<uint8_t> Contents = S.getContents();
  286. std::copy(Contents.begin(), Contents.end(), Ptr);
  287. // For executable sections, pad the remainder of the raw data size with
  288. // 0xcc, which is int3 on x86.
  289. if ((S.Header.Characteristics & IMAGE_SCN_CNT_CODE) &&
  290. S.Header.SizeOfRawData > Contents.size())
  291. memset(Ptr + Contents.size(), 0xcc,
  292. S.Header.SizeOfRawData - Contents.size());
  293. Ptr += S.Header.SizeOfRawData;
  294. if (S.Relocs.size() >= 0xffff) {
  295. object::coff_relocation R;
  296. R.VirtualAddress = S.Relocs.size() + 1;
  297. R.SymbolTableIndex = 0;
  298. R.Type = 0;
  299. memcpy(Ptr, &R, sizeof(R));
  300. Ptr += sizeof(R);
  301. }
  302. for (const auto &R : S.Relocs) {
  303. memcpy(Ptr, &R.Reloc, sizeof(R.Reloc));
  304. Ptr += sizeof(R.Reloc);
  305. }
  306. }
  307. }
  308. template <class SymbolTy> void COFFWriter::writeSymbolStringTables() {
  309. uint8_t *Ptr = reinterpret_cast<uint8_t *>(Buf->getBufferStart()) +
  310. Obj.CoffFileHeader.PointerToSymbolTable;
  311. for (const auto &S : Obj.getSymbols()) {
  312. // Convert symbols back to the right size, from coff_symbol32.
  313. copySymbol<SymbolTy, coff_symbol32>(*reinterpret_cast<SymbolTy *>(Ptr),
  314. S.Sym);
  315. Ptr += sizeof(SymbolTy);
  316. if (!S.AuxFile.empty()) {
  317. // For file symbols, just write the string into the aux symbol slots,
  318. // assuming that the unwritten parts are initialized to zero in the memory
  319. // mapped file.
  320. std::copy(S.AuxFile.begin(), S.AuxFile.end(), Ptr);
  321. Ptr += S.Sym.NumberOfAuxSymbols * sizeof(SymbolTy);
  322. } else {
  323. // For other auxillary symbols, write their opaque payload into one symbol
  324. // table slot each. For big object files, the symbols are larger than the
  325. // opaque auxillary symbol struct and we leave padding at the end of each
  326. // entry.
  327. for (const AuxSymbol &AuxSym : S.AuxData) {
  328. ArrayRef<uint8_t> Ref = AuxSym.getRef();
  329. std::copy(Ref.begin(), Ref.end(), Ptr);
  330. Ptr += sizeof(SymbolTy);
  331. }
  332. }
  333. }
  334. if (StrTabBuilder.getSize() > 4 || !Obj.IsPE) {
  335. // Always write a string table in object files, even an empty one.
  336. StrTabBuilder.write(Ptr);
  337. Ptr += StrTabBuilder.getSize();
  338. }
  339. }
  340. Error COFFWriter::write(bool IsBigObj) {
  341. if (Error E = finalize(IsBigObj))
  342. return E;
  343. Buf = WritableMemoryBuffer::getNewMemBuffer(FileSize);
  344. if (!Buf)
  345. return createStringError(llvm::errc::not_enough_memory,
  346. "failed to allocate memory buffer of " +
  347. Twine::utohexstr(FileSize) + " bytes.");
  348. writeHeaders(IsBigObj);
  349. writeSections();
  350. if (IsBigObj)
  351. writeSymbolStringTables<coff_symbol32>();
  352. else
  353. writeSymbolStringTables<coff_symbol16>();
  354. if (Obj.IsPE)
  355. if (Error E = patchDebugDirectory())
  356. return E;
  357. // TODO: Implement direct writing to the output stream (without intermediate
  358. // memory buffer Buf).
  359. Out.write(Buf->getBufferStart(), Buf->getBufferSize());
  360. return Error::success();
  361. }
  362. Expected<uint32_t> COFFWriter::virtualAddressToFileAddress(uint32_t RVA) {
  363. for (const auto &S : Obj.getSections()) {
  364. if (RVA >= S.Header.VirtualAddress &&
  365. RVA < S.Header.VirtualAddress + S.Header.SizeOfRawData)
  366. return S.Header.PointerToRawData + RVA - S.Header.VirtualAddress;
  367. }
  368. return createStringError(object_error::parse_failed,
  369. "debug directory payload not found");
  370. }
  371. // Locate which sections contain the debug directories, iterate over all
  372. // the debug_directory structs in there, and set the PointerToRawData field
  373. // in all of them, according to their new physical location in the file.
  374. Error COFFWriter::patchDebugDirectory() {
  375. if (Obj.DataDirectories.size() <= DEBUG_DIRECTORY)
  376. return Error::success();
  377. const data_directory *Dir = &Obj.DataDirectories[DEBUG_DIRECTORY];
  378. if (Dir->Size <= 0)
  379. return Error::success();
  380. for (const auto &S : Obj.getSections()) {
  381. if (Dir->RelativeVirtualAddress >= S.Header.VirtualAddress &&
  382. Dir->RelativeVirtualAddress <
  383. S.Header.VirtualAddress + S.Header.SizeOfRawData) {
  384. if (Dir->RelativeVirtualAddress + Dir->Size >
  385. S.Header.VirtualAddress + S.Header.SizeOfRawData)
  386. return createStringError(object_error::parse_failed,
  387. "debug directory extends past end of section");
  388. size_t Offset = Dir->RelativeVirtualAddress - S.Header.VirtualAddress;
  389. uint8_t *Ptr = reinterpret_cast<uint8_t *>(Buf->getBufferStart()) +
  390. S.Header.PointerToRawData + Offset;
  391. uint8_t *End = Ptr + Dir->Size;
  392. while (Ptr < End) {
  393. debug_directory *Debug = reinterpret_cast<debug_directory *>(Ptr);
  394. if (Debug->PointerToRawData) {
  395. if (Expected<uint32_t> FilePosOrErr =
  396. virtualAddressToFileAddress(Debug->AddressOfRawData))
  397. Debug->PointerToRawData = *FilePosOrErr;
  398. else
  399. return FilePosOrErr.takeError();
  400. }
  401. Ptr += sizeof(debug_directory);
  402. Offset += sizeof(debug_directory);
  403. }
  404. // Debug directory found and patched, all done.
  405. return Error::success();
  406. }
  407. }
  408. return createStringError(object_error::parse_failed,
  409. "debug directory not found");
  410. }
  411. Error COFFWriter::write() {
  412. bool IsBigObj = Obj.getSections().size() > MaxNumberOfSections16;
  413. if (IsBigObj && Obj.IsPE)
  414. return createStringError(object_error::parse_failed,
  415. "too many sections for executable");
  416. return write(IsBigObj);
  417. }
  418. } // end namespace coff
  419. } // end namespace objcopy
  420. } // end namespace llvm