Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 1 | //===- Writer.cpp ---------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "Writer.h" |
| 11 | #include "Object.h" |
| 12 | #include "llvm-objcopy.h" |
| 13 | #include "llvm/ADT/ArrayRef.h" |
| 14 | #include "llvm/ADT/StringRef.h" |
| 15 | #include "llvm/BinaryFormat/COFF.h" |
| 16 | #include "llvm/Object/COFF.h" |
| 17 | #include "llvm/Support/ErrorHandling.h" |
| 18 | #include <cstddef> |
| 19 | #include <cstdint> |
| 20 | |
| 21 | namespace llvm { |
| 22 | namespace objcopy { |
| 23 | namespace coff { |
| 24 | |
| 25 | using namespace object; |
| 26 | using namespace COFF; |
| 27 | |
| 28 | Writer::~Writer() {} |
| 29 | |
Martin Storsjo | 10b7296 | 2019-01-10 21:28:24 +0000 | [diff] [blame^] | 30 | Error COFFWriter::finalizeRelocTargets() { |
| 31 | for (Section &Sec : Obj.Sections) { |
| 32 | for (Relocation &R : Sec.Relocs) { |
| 33 | const Symbol *Sym = Obj.findSymbol(R.Target); |
| 34 | if (Sym == nullptr) |
| 35 | return make_error<StringError>("Relocation target " + R.TargetName + |
| 36 | " (" + Twine(R.Target) + |
| 37 | ") not found", |
| 38 | object_error::invalid_symbol_index); |
| 39 | R.Reloc.SymbolTableIndex = Sym->RawIndex; |
| 40 | } |
| 41 | } |
| 42 | return Error::success(); |
| 43 | } |
| 44 | |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 45 | void COFFWriter::layoutSections() { |
| 46 | for (auto &S : Obj.Sections) { |
| 47 | if (S.Header.SizeOfRawData > 0) |
| 48 | S.Header.PointerToRawData = FileSize; |
| 49 | FileSize += S.Header.SizeOfRawData; // For executables, this is already |
| 50 | // aligned to FileAlignment. |
| 51 | if (S.Header.NumberOfRelocations > 0) |
| 52 | S.Header.PointerToRelocations = FileSize; |
| 53 | FileSize += S.Relocs.size() * sizeof(coff_relocation); |
| 54 | FileSize = alignTo(FileSize, FileAlignment); |
| 55 | |
| 56 | if (S.Header.Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA) |
| 57 | SizeOfInitializedData += S.Header.SizeOfRawData; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | size_t COFFWriter::finalizeStringTable() { |
| 62 | for (auto &S : Obj.Sections) |
| 63 | if (S.Name.size() > COFF::NameSize) |
| 64 | StrTabBuilder.add(S.Name); |
| 65 | |
Martin Storsjo | 10b7296 | 2019-01-10 21:28:24 +0000 | [diff] [blame^] | 66 | for (const auto &S : Obj.getSymbols()) |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 67 | if (S.Name.size() > COFF::NameSize) |
| 68 | StrTabBuilder.add(S.Name); |
| 69 | |
| 70 | StrTabBuilder.finalize(); |
| 71 | |
| 72 | for (auto &S : Obj.Sections) { |
| 73 | if (S.Name.size() > COFF::NameSize) { |
| 74 | snprintf(S.Header.Name, sizeof(S.Header.Name), "/%d", |
| 75 | (int)StrTabBuilder.getOffset(S.Name)); |
| 76 | } else { |
| 77 | strncpy(S.Header.Name, S.Name.data(), COFF::NameSize); |
| 78 | } |
| 79 | } |
Martin Storsjo | 10b7296 | 2019-01-10 21:28:24 +0000 | [diff] [blame^] | 80 | for (auto &S : Obj.getMutableSymbols()) { |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 81 | if (S.Name.size() > COFF::NameSize) { |
| 82 | S.Sym.Name.Offset.Zeroes = 0; |
| 83 | S.Sym.Name.Offset.Offset = StrTabBuilder.getOffset(S.Name); |
| 84 | } else { |
| 85 | strncpy(S.Sym.Name.ShortName, S.Name.data(), COFF::NameSize); |
| 86 | } |
| 87 | } |
| 88 | return StrTabBuilder.getSize(); |
| 89 | } |
| 90 | |
| 91 | template <class SymbolTy> |
| 92 | std::pair<size_t, size_t> COFFWriter::finalizeSymbolTable() { |
Martin Storsjo | 10b7296 | 2019-01-10 21:28:24 +0000 | [diff] [blame^] | 93 | size_t SymTabSize = Obj.getSymbols().size() * sizeof(SymbolTy); |
| 94 | for (const auto &S : Obj.getSymbols()) |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 95 | SymTabSize += S.AuxData.size(); |
| 96 | return std::make_pair(SymTabSize, sizeof(SymbolTy)); |
| 97 | } |
| 98 | |
Martin Storsjo | 10b7296 | 2019-01-10 21:28:24 +0000 | [diff] [blame^] | 99 | Error COFFWriter::finalize(bool IsBigObj) { |
| 100 | if (Error E = finalizeRelocTargets()) |
| 101 | return E; |
| 102 | |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 103 | size_t SizeOfHeaders = 0; |
| 104 | FileAlignment = 1; |
| 105 | size_t PeHeaderSize = 0; |
| 106 | if (Obj.IsPE) { |
| 107 | Obj.DosHeader.AddressOfNewExeHeader = |
| 108 | sizeof(Obj.DosHeader) + Obj.DosStub.size(); |
| 109 | SizeOfHeaders += Obj.DosHeader.AddressOfNewExeHeader + sizeof(PEMagic); |
| 110 | |
| 111 | FileAlignment = Obj.PeHeader.FileAlignment; |
| 112 | Obj.PeHeader.NumberOfRvaAndSize = Obj.DataDirectories.size(); |
| 113 | |
| 114 | PeHeaderSize = Obj.Is64 ? sizeof(pe32plus_header) : sizeof(pe32_header); |
| 115 | SizeOfHeaders += |
| 116 | PeHeaderSize + sizeof(data_directory) * Obj.DataDirectories.size(); |
| 117 | } |
| 118 | Obj.CoffFileHeader.NumberOfSections = Obj.Sections.size(); |
| 119 | SizeOfHeaders += |
| 120 | IsBigObj ? sizeof(coff_bigobj_file_header) : sizeof(coff_file_header); |
| 121 | SizeOfHeaders += sizeof(coff_section) * Obj.Sections.size(); |
| 122 | SizeOfHeaders = alignTo(SizeOfHeaders, FileAlignment); |
| 123 | |
| 124 | Obj.CoffFileHeader.SizeOfOptionalHeader = |
| 125 | PeHeaderSize + sizeof(data_directory) * Obj.DataDirectories.size(); |
| 126 | |
| 127 | FileSize = SizeOfHeaders; |
| 128 | SizeOfInitializedData = 0; |
| 129 | |
| 130 | layoutSections(); |
| 131 | |
| 132 | if (Obj.IsPE) { |
| 133 | Obj.PeHeader.SizeOfHeaders = SizeOfHeaders; |
| 134 | Obj.PeHeader.SizeOfInitializedData = SizeOfInitializedData; |
| 135 | |
| 136 | if (!Obj.Sections.empty()) { |
| 137 | const Section &S = Obj.Sections.back(); |
| 138 | Obj.PeHeader.SizeOfImage = |
| 139 | alignTo(S.Header.VirtualAddress + S.Header.VirtualSize, |
| 140 | Obj.PeHeader.SectionAlignment); |
| 141 | } |
| 142 | |
| 143 | // If the PE header had a checksum, clear it, since it isn't valid |
| 144 | // any longer. (We don't calculate a new one.) |
| 145 | Obj.PeHeader.CheckSum = 0; |
| 146 | } |
| 147 | |
| 148 | size_t StrTabSize = finalizeStringTable(); |
| 149 | size_t SymTabSize, SymbolSize; |
| 150 | std::tie(SymTabSize, SymbolSize) = IsBigObj |
| 151 | ? finalizeSymbolTable<coff_symbol32>() |
| 152 | : finalizeSymbolTable<coff_symbol16>(); |
| 153 | |
| 154 | size_t PointerToSymbolTable = FileSize; |
| 155 | // StrTabSize <= 4 is the size of an empty string table, only consisting |
| 156 | // of the length field. |
| 157 | if (SymTabSize == 0 && StrTabSize <= 4) { |
| 158 | // Don't point to the symbol table if empty. |
| 159 | PointerToSymbolTable = 0; |
| 160 | // For executables, skip the length field of an empty string table. |
| 161 | if (Obj.IsPE) |
| 162 | StrTabSize = 0; |
| 163 | } |
| 164 | |
| 165 | size_t NumRawSymbols = SymTabSize / SymbolSize; |
| 166 | Obj.CoffFileHeader.PointerToSymbolTable = PointerToSymbolTable; |
| 167 | Obj.CoffFileHeader.NumberOfSymbols = NumRawSymbols; |
| 168 | FileSize += SymTabSize + StrTabSize; |
| 169 | FileSize = alignTo(FileSize, FileAlignment); |
Martin Storsjo | 10b7296 | 2019-01-10 21:28:24 +0000 | [diff] [blame^] | 170 | |
| 171 | return Error::success(); |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | void COFFWriter::writeHeaders(bool IsBigObj) { |
| 175 | uint8_t *Ptr = Buf.getBufferStart(); |
| 176 | if (Obj.IsPE) { |
| 177 | memcpy(Ptr, &Obj.DosHeader, sizeof(Obj.DosHeader)); |
| 178 | Ptr += sizeof(Obj.DosHeader); |
| 179 | memcpy(Ptr, Obj.DosStub.data(), Obj.DosStub.size()); |
| 180 | Ptr += Obj.DosStub.size(); |
| 181 | memcpy(Ptr, PEMagic, sizeof(PEMagic)); |
| 182 | Ptr += sizeof(PEMagic); |
| 183 | } |
| 184 | if (!IsBigObj) { |
| 185 | memcpy(Ptr, &Obj.CoffFileHeader, sizeof(Obj.CoffFileHeader)); |
| 186 | Ptr += sizeof(Obj.CoffFileHeader); |
| 187 | } else { |
| 188 | // Generate a coff_bigobj_file_header, filling it in with the values |
| 189 | // from Obj.CoffFileHeader. All extra fields that don't exist in |
| 190 | // coff_file_header can be set to hardcoded values. |
| 191 | coff_bigobj_file_header BigObjHeader; |
| 192 | BigObjHeader.Sig1 = IMAGE_FILE_MACHINE_UNKNOWN; |
| 193 | BigObjHeader.Sig2 = 0xffff; |
| 194 | BigObjHeader.Version = BigObjHeader::MinBigObjectVersion; |
| 195 | BigObjHeader.Machine = Obj.CoffFileHeader.Machine; |
| 196 | BigObjHeader.TimeDateStamp = Obj.CoffFileHeader.TimeDateStamp; |
| 197 | memcpy(BigObjHeader.UUID, BigObjMagic, sizeof(BigObjMagic)); |
| 198 | BigObjHeader.unused1 = 0; |
| 199 | BigObjHeader.unused2 = 0; |
| 200 | BigObjHeader.unused3 = 0; |
| 201 | BigObjHeader.unused4 = 0; |
| 202 | // The value in Obj.CoffFileHeader.NumberOfSections is truncated, thus |
| 203 | // get the original one instead. |
| 204 | BigObjHeader.NumberOfSections = Obj.Sections.size(); |
| 205 | BigObjHeader.PointerToSymbolTable = Obj.CoffFileHeader.PointerToSymbolTable; |
| 206 | BigObjHeader.NumberOfSymbols = Obj.CoffFileHeader.NumberOfSymbols; |
| 207 | |
| 208 | memcpy(Ptr, &BigObjHeader, sizeof(BigObjHeader)); |
| 209 | Ptr += sizeof(BigObjHeader); |
| 210 | } |
| 211 | if (Obj.IsPE) { |
| 212 | if (Obj.Is64) { |
| 213 | memcpy(Ptr, &Obj.PeHeader, sizeof(Obj.PeHeader)); |
| 214 | Ptr += sizeof(Obj.PeHeader); |
| 215 | } else { |
| 216 | pe32_header PeHeader; |
| 217 | copyPeHeader(PeHeader, Obj.PeHeader); |
| 218 | // The pe32plus_header (stored in Object) lacks the BaseOfData field. |
| 219 | PeHeader.BaseOfData = Obj.BaseOfData; |
| 220 | |
| 221 | memcpy(Ptr, &PeHeader, sizeof(PeHeader)); |
| 222 | Ptr += sizeof(PeHeader); |
| 223 | } |
| 224 | for (const auto &DD : Obj.DataDirectories) { |
| 225 | memcpy(Ptr, &DD, sizeof(DD)); |
| 226 | Ptr += sizeof(DD); |
| 227 | } |
| 228 | } |
| 229 | for (const auto &S : Obj.Sections) { |
| 230 | memcpy(Ptr, &S.Header, sizeof(S.Header)); |
| 231 | Ptr += sizeof(S.Header); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | void COFFWriter::writeSections() { |
| 236 | for (const auto &S : Obj.Sections) { |
| 237 | uint8_t *Ptr = Buf.getBufferStart() + S.Header.PointerToRawData; |
Martin Storsjo | 14cfa9a | 2018-12-20 21:35:59 +0000 | [diff] [blame] | 238 | std::copy(S.Contents.begin(), S.Contents.end(), Ptr); |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 239 | |
| 240 | // For executable sections, pad the remainder of the raw data size with |
| 241 | // 0xcc, which is int3 on x86. |
| 242 | if ((S.Header.Characteristics & IMAGE_SCN_CNT_CODE) && |
| 243 | S.Header.SizeOfRawData > S.Contents.size()) |
| 244 | memset(Ptr + S.Contents.size(), 0xcc, |
| 245 | S.Header.SizeOfRawData - S.Contents.size()); |
| 246 | |
| 247 | Ptr += S.Header.SizeOfRawData; |
Martin Storsjo | 10b7296 | 2019-01-10 21:28:24 +0000 | [diff] [blame^] | 248 | for (const auto &R : S.Relocs) { |
| 249 | memcpy(Ptr, &R.Reloc, sizeof(R.Reloc)); |
| 250 | Ptr += sizeof(R.Reloc); |
| 251 | } |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
| 255 | template <class SymbolTy> void COFFWriter::writeSymbolStringTables() { |
| 256 | uint8_t *Ptr = Buf.getBufferStart() + Obj.CoffFileHeader.PointerToSymbolTable; |
Martin Storsjo | 10b7296 | 2019-01-10 21:28:24 +0000 | [diff] [blame^] | 257 | for (const auto &S : Obj.getSymbols()) { |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 258 | // Convert symbols back to the right size, from coff_symbol32. |
| 259 | copySymbol<SymbolTy, coff_symbol32>(*reinterpret_cast<SymbolTy *>(Ptr), |
| 260 | S.Sym); |
| 261 | Ptr += sizeof(SymbolTy); |
Martin Storsjo | 14cfa9a | 2018-12-20 21:35:59 +0000 | [diff] [blame] | 262 | std::copy(S.AuxData.begin(), S.AuxData.end(), Ptr); |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 263 | Ptr += S.AuxData.size(); |
| 264 | } |
| 265 | if (StrTabBuilder.getSize() > 4 || !Obj.IsPE) { |
| 266 | // Always write a string table in object files, even an empty one. |
| 267 | StrTabBuilder.write(Ptr); |
| 268 | Ptr += StrTabBuilder.getSize(); |
| 269 | } |
| 270 | } |
| 271 | |
Martin Storsjo | 0a5d5b1 | 2018-12-30 20:35:43 +0000 | [diff] [blame] | 272 | Error COFFWriter::write(bool IsBigObj) { |
Martin Storsjo | 10b7296 | 2019-01-10 21:28:24 +0000 | [diff] [blame^] | 273 | if (Error E = finalize(IsBigObj)) |
| 274 | return E; |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 275 | |
| 276 | Buf.allocate(FileSize); |
| 277 | |
| 278 | writeHeaders(IsBigObj); |
| 279 | writeSections(); |
| 280 | if (IsBigObj) |
| 281 | writeSymbolStringTables<coff_symbol32>(); |
| 282 | else |
| 283 | writeSymbolStringTables<coff_symbol16>(); |
| 284 | |
| 285 | if (Obj.IsPE) |
Martin Storsjo | 0a5d5b1 | 2018-12-30 20:35:43 +0000 | [diff] [blame] | 286 | if (Error E = patchDebugDirectory()) |
| 287 | return E; |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 288 | |
Martin Storsjo | 0a5d5b1 | 2018-12-30 20:35:43 +0000 | [diff] [blame] | 289 | return Buf.commit(); |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | // Locate which sections contain the debug directories, iterate over all |
| 293 | // the debug_directory structs in there, and set the PointerToRawData field |
| 294 | // in all of them, according to their new physical location in the file. |
Martin Storsjo | 0a5d5b1 | 2018-12-30 20:35:43 +0000 | [diff] [blame] | 295 | Error COFFWriter::patchDebugDirectory() { |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 296 | if (Obj.DataDirectories.size() < DEBUG_DIRECTORY) |
Martin Storsjo | 0a5d5b1 | 2018-12-30 20:35:43 +0000 | [diff] [blame] | 297 | return Error::success(); |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 298 | const data_directory *Dir = &Obj.DataDirectories[DEBUG_DIRECTORY]; |
| 299 | if (Dir->Size <= 0) |
Martin Storsjo | 0a5d5b1 | 2018-12-30 20:35:43 +0000 | [diff] [blame] | 300 | return Error::success(); |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 301 | for (const auto &S : Obj.Sections) { |
| 302 | if (Dir->RelativeVirtualAddress >= S.Header.VirtualAddress && |
| 303 | Dir->RelativeVirtualAddress < |
| 304 | S.Header.VirtualAddress + S.Header.SizeOfRawData) { |
| 305 | if (Dir->RelativeVirtualAddress + Dir->Size > |
| 306 | S.Header.VirtualAddress + S.Header.SizeOfRawData) |
Martin Storsjo | 0a5d5b1 | 2018-12-30 20:35:43 +0000 | [diff] [blame] | 307 | return make_error<StringError>( |
| 308 | "Debug directory extends past end of section", |
| 309 | object_error::parse_failed); |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 310 | |
| 311 | size_t Offset = Dir->RelativeVirtualAddress - S.Header.VirtualAddress; |
| 312 | uint8_t *Ptr = Buf.getBufferStart() + S.Header.PointerToRawData + Offset; |
| 313 | uint8_t *End = Ptr + Dir->Size; |
| 314 | while (Ptr < End) { |
| 315 | debug_directory *Debug = reinterpret_cast<debug_directory *>(Ptr); |
| 316 | Debug->PointerToRawData = |
| 317 | S.Header.PointerToRawData + Offset + sizeof(debug_directory); |
| 318 | Ptr += sizeof(debug_directory) + Debug->SizeOfData; |
| 319 | Offset += sizeof(debug_directory) + Debug->SizeOfData; |
| 320 | } |
| 321 | // Debug directory found and patched, all done. |
Martin Storsjo | 0a5d5b1 | 2018-12-30 20:35:43 +0000 | [diff] [blame] | 322 | return Error::success(); |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 323 | } |
| 324 | } |
Martin Storsjo | 0a5d5b1 | 2018-12-30 20:35:43 +0000 | [diff] [blame] | 325 | return make_error<StringError>("Debug directory not found", |
| 326 | object_error::parse_failed); |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Martin Storsjo | 0a5d5b1 | 2018-12-30 20:35:43 +0000 | [diff] [blame] | 329 | Error COFFWriter::write() { |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 330 | bool IsBigObj = Obj.Sections.size() > MaxNumberOfSections16; |
| 331 | if (IsBigObj && Obj.IsPE) |
Martin Storsjo | 0a5d5b1 | 2018-12-30 20:35:43 +0000 | [diff] [blame] | 332 | return make_error<StringError>("Too many sections for executable", |
| 333 | object_error::parse_failed); |
| 334 | return write(IsBigObj); |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | } // end namespace coff |
| 338 | } // end namespace objcopy |
| 339 | } // end namespace llvm |