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