1 //===-- lib/MC/XCOFFObjectWriter.cpp - XCOFF file writer ------------------===// 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 // This file implements XCOFF object file writer information. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/BinaryFormat/XCOFF.h" 14 #include "llvm/MC/MCAsmLayout.h" 15 #include "llvm/MC/MCAssembler.h" 16 #include "llvm/MC/MCObjectWriter.h" 17 #include "llvm/MC/MCSectionXCOFF.h" 18 #include "llvm/MC/MCSymbolXCOFF.h" 19 #include "llvm/MC/MCValue.h" 20 #include "llvm/MC/MCXCOFFObjectWriter.h" 21 #include "llvm/MC/StringTableBuilder.h" 22 #include "llvm/Support/Error.h" 23 #include "llvm/Support/MathExtras.h" 24 25 #include <deque> 26 27 using namespace llvm; 28 29 // An XCOFF object file has a limited set of predefined sections. The most 30 // important ones for us (right now) are: 31 // .text --> contains program code and read-only data. 32 // .data --> contains initialized data, function descriptors, and the TOC. 33 // .bss --> contains uninitialized data. 34 // Each of these sections is composed of 'Control Sections'. A Control Section 35 // is more commonly referred to as a csect. A csect is an indivisible unit of 36 // code or data, and acts as a container for symbols. A csect is mapped 37 // into a section based on its storage-mapping class, with the exception of 38 // XMC_RW which gets mapped to either .data or .bss based on whether it's 39 // explicitly initialized or not. 40 // 41 // We don't represent the sections in the MC layer as there is nothing 42 // interesting about them at at that level: they carry information that is 43 // only relevant to the ObjectWriter, so we materialize them in this class. 44 namespace { 45 46 constexpr unsigned DefaultSectionAlign = 4; 47 48 // Packs the csect's alignment and type into a byte. 49 uint8_t getEncodedType(const MCSectionXCOFF *); 50 51 // Wrapper around an MCSymbolXCOFF. 52 struct Symbol { 53 const MCSymbolXCOFF *const MCSym; 54 uint32_t SymbolTableIndex; 55 56 XCOFF::StorageClass getStorageClass() const { 57 return MCSym->getStorageClass(); 58 } 59 StringRef getName() const { return MCSym->getName(); } 60 bool nameInStringTable() const { 61 return MCSym->getName().size() > XCOFF::NameSize; 62 } 63 64 Symbol(const MCSymbolXCOFF *MCSym) : MCSym(MCSym), SymbolTableIndex(-1) {} 65 }; 66 67 // Wrapper for an MCSectionXCOFF. 68 struct ControlSection { 69 const MCSectionXCOFF *const MCCsect; 70 uint32_t SymbolTableIndex; 71 uint32_t Address; 72 uint32_t Size; 73 74 SmallVector<Symbol, 1> Syms; 75 76 ControlSection(const MCSectionXCOFF *MCSec) 77 : MCCsect(MCSec), SymbolTableIndex(-1), Address(-1) {} 78 }; 79 80 // Represents the data related to a section excluding the csects that make up 81 // the raw data of the section. The csects are stored separately as not all 82 // sections contain csects, and some sections contain csects which are better 83 // stored separately, e.g. the .data section containing read-write, descriptor, 84 // TOCBase and TOC-entry csects. 85 struct Section { 86 char Name[XCOFF::NameSize]; 87 // The physical/virtual address of the section. For an object file 88 // these values are equivalent. 89 uint32_t Address; 90 uint32_t Size; 91 uint32_t FileOffsetToData; 92 uint32_t FileOffsetToRelocations; 93 uint32_t RelocationCount; 94 int32_t Flags; 95 96 uint16_t Index; 97 98 // Virtual sections do not need storage allocated in the object file. 99 const bool IsVirtual; 100 101 void reset() { 102 Address = 0; 103 Size = 0; 104 FileOffsetToData = 0; 105 FileOffsetToRelocations = 0; 106 RelocationCount = 0; 107 Index = -1; 108 } 109 110 Section(const char *N, XCOFF::SectionTypeFlags Flags, bool IsVirtual) 111 : Address(0), Size(0), FileOffsetToData(0), FileOffsetToRelocations(0), 112 RelocationCount(0), Flags(Flags), Index(-1), IsVirtual(IsVirtual) { 113 strncpy(Name, N, XCOFF::NameSize); 114 } 115 }; 116 117 class XCOFFObjectWriter : public MCObjectWriter { 118 // Type to be used for a container representing a set of csects with 119 // (approximately) the same storage mapping class. For example all the csects 120 // with a storage mapping class of `xmc_pr` will get placed into the same 121 // container. 122 using ControlSections = std::deque<ControlSection>; 123 124 support::endian::Writer W; 125 std::unique_ptr<MCXCOFFObjectTargetWriter> TargetObjectWriter; 126 StringTableBuilder Strings; 127 128 // The non-empty sections, in the order they will appear in the section header 129 // table. 130 std::vector<Section *> Sections; 131 132 // The Predefined sections. 133 Section Text; 134 Section BSS; 135 136 // ControlSections. These store the csects which make up different parts of 137 // the sections. Should have one for each set of csects that get mapped into 138 // the same section and get handled in a 'similar' way. 139 ControlSections ProgramCodeCsects; 140 ControlSections BSSCsects; 141 142 uint32_t SymbolTableEntryCount = 0; 143 uint32_t SymbolTableOffset = 0; 144 145 virtual void reset() override; 146 147 void executePostLayoutBinding(MCAssembler &, const MCAsmLayout &) override; 148 149 void recordRelocation(MCAssembler &, const MCAsmLayout &, const MCFragment *, 150 const MCFixup &, MCValue, uint64_t &) override; 151 152 uint64_t writeObject(MCAssembler &, const MCAsmLayout &) override; 153 154 void writeFileHeader(); 155 void writeSectionHeaderTable(); 156 void writeSymbolTable(); 157 158 // Called after all the csects and symbols have been processed by 159 // `executePostLayoutBinding`, this function handles building up the majority 160 // of the structures in the object file representation. Namely: 161 // *) Calculates physical/virtual addresses, raw-pointer offsets, and section 162 // sizes. 163 // *) Assigns symbol table indices. 164 // *) Builds up the section header table by adding any non-empty sections to 165 // `Sections`. 166 void assignAddressesAndIndices(const llvm::MCAsmLayout &); 167 168 bool 169 needsAuxiliaryHeader() const { /* TODO aux header support not implemented. */ 170 return false; 171 } 172 173 // Returns the size of the auxiliary header to be written to the object file. 174 size_t auxiliaryHeaderSize() const { 175 assert(!needsAuxiliaryHeader() && 176 "Auxiliary header support not implemented."); 177 return 0; 178 } 179 180 public: 181 XCOFFObjectWriter(std::unique_ptr<MCXCOFFObjectTargetWriter> MOTW, 182 raw_pwrite_stream &OS); 183 }; 184 185 XCOFFObjectWriter::XCOFFObjectWriter( 186 std::unique_ptr<MCXCOFFObjectTargetWriter> MOTW, raw_pwrite_stream &OS) 187 : W(OS, support::big), TargetObjectWriter(std::move(MOTW)), 188 Strings(StringTableBuilder::XCOFF), 189 Text(".text", XCOFF::STYP_TEXT, /* IsVirtual */ false), 190 BSS(".bss", XCOFF::STYP_BSS, /* IsVirtual */ true) {} 191 192 void XCOFFObjectWriter::reset() { 193 // Reset any sections we have written to, and empty the section header table. 194 for (auto *Sec : Sections) 195 Sec->reset(); 196 Sections.clear(); 197 198 // Clear any csects we have stored. 199 ProgramCodeCsects.clear(); 200 BSSCsects.clear(); 201 202 // Reset the symbol table and string table. 203 SymbolTableEntryCount = 0; 204 SymbolTableOffset = 0; 205 Strings.clear(); 206 207 MCObjectWriter::reset(); 208 } 209 210 void XCOFFObjectWriter::executePostLayoutBinding( 211 llvm::MCAssembler &Asm, const llvm::MCAsmLayout &Layout) { 212 if (TargetObjectWriter->is64Bit()) 213 report_fatal_error("64-bit XCOFF object files are not supported yet."); 214 215 // Maps the MC Section representation to its corresponding ControlSection 216 // wrapper. Needed for finding the ControlSection to insert an MCSymbol into 217 // from its containing MCSectionXCOFF. 218 DenseMap<const MCSectionXCOFF *, ControlSection *> WrapperMap; 219 220 for (const auto &S : Asm) { 221 const MCSectionXCOFF *MCSec = dyn_cast<const MCSectionXCOFF>(&S); 222 assert(WrapperMap.find(MCSec) == WrapperMap.end() && 223 "Cannot add a csect twice."); 224 225 switch (MCSec->getMappingClass()) { 226 case XCOFF::XMC_PR: 227 assert(XCOFF::XTY_SD == MCSec->getCSectType() && 228 "Only an initialized csect can contain program code."); 229 // TODO FIXME Handle .text section csects. 230 break; 231 case XCOFF::XMC_RW: 232 if (XCOFF::XTY_CM == MCSec->getCSectType()) { 233 BSSCsects.emplace_back(MCSec); 234 WrapperMap[MCSec] = &BSSCsects.back(); 235 break; 236 } 237 report_fatal_error("Unhandled mapping of read-write csect to section."); 238 default: 239 report_fatal_error("Unhandled mapping of csect to section."); 240 } 241 } 242 243 for (const MCSymbol &S : Asm.symbols()) { 244 // Nothing to do for temporary symbols. 245 if (S.isTemporary()) 246 continue; 247 const MCSymbolXCOFF *XSym = cast<MCSymbolXCOFF>(&S); 248 249 // Map the symbol into its containing csect. 250 MCSectionXCOFF *ContainingCsect = 251 dyn_cast<MCSectionXCOFF>(XSym->getFragment(false)->getParent()); 252 assert(WrapperMap.find(ContainingCsect) != WrapperMap.end() && 253 "Expected containing csect to exist in map"); 254 255 // Lookup the containing csect and add the symbol to it. 256 WrapperMap[ContainingCsect]->Syms.emplace_back(XSym); 257 258 // If the name does not fit in the storage provided in the symbol table 259 // entry, add it to the string table. 260 const Symbol &WrapperSym = WrapperMap[ContainingCsect]->Syms.back(); 261 if (WrapperSym.nameInStringTable()) { 262 Strings.add(WrapperSym.getName()); 263 } 264 } 265 266 Strings.finalize(); 267 assignAddressesAndIndices(Layout); 268 } 269 270 void XCOFFObjectWriter::recordRelocation(MCAssembler &, const MCAsmLayout &, 271 const MCFragment *, const MCFixup &, 272 MCValue, uint64_t &) { 273 report_fatal_error("XCOFF relocations not supported."); 274 } 275 276 uint64_t XCOFFObjectWriter::writeObject(MCAssembler &Asm, const MCAsmLayout &) { 277 // We always emit a timestamp of 0 for reproducibility, so ensure incremental 278 // linking is not enabled, in case, like with Windows COFF, such a timestamp 279 // is incompatible with incremental linking of XCOFF. 280 if (Asm.isIncrementalLinkerCompatible()) 281 report_fatal_error("Incremental linking not supported for XCOFF."); 282 283 if (TargetObjectWriter->is64Bit()) 284 report_fatal_error("64-bit XCOFF object files are not supported yet."); 285 286 uint64_t StartOffset = W.OS.tell(); 287 288 writeFileHeader(); 289 writeSectionHeaderTable(); 290 // TODO writeSections(); 291 // TODO writeRelocations(); 292 293 // TODO FIXME Finalize symbols. 294 writeSymbolTable(); 295 // Write the string table. 296 Strings.write(W.OS); 297 298 return W.OS.tell() - StartOffset; 299 } 300 301 void XCOFFObjectWriter::writeFileHeader() { 302 // Magic. 303 W.write<uint16_t>(0x01df); 304 // Number of sections. 305 W.write<uint16_t>(Sections.size()); 306 // Timestamp field. For reproducible output we write a 0, which represents no 307 // timestamp. 308 W.write<int32_t>(0); 309 // Byte Offset to the start of the symbol table. 310 W.write<uint32_t>(SymbolTableOffset); 311 // Number of entries in the symbol table. 312 W.write<int32_t>(SymbolTableEntryCount); 313 // Size of the optional header. 314 W.write<uint16_t>(0); 315 // Flags. 316 W.write<uint16_t>(0); 317 } 318 319 void XCOFFObjectWriter::writeSectionHeaderTable() { 320 for (const auto *Sec : Sections) { 321 // Write Name. 322 ArrayRef<char> NameRef(Sec->Name, XCOFF::NameSize); 323 W.write(NameRef); 324 325 // Write the Physical Address and Virtual Address. In an object file these 326 // are the same. 327 W.write<uint32_t>(Sec->Address); 328 W.write<uint32_t>(Sec->Address); 329 330 W.write<uint32_t>(Sec->Size); 331 W.write<uint32_t>(Sec->FileOffsetToData); 332 333 // Relocation pointer and Lineno pointer. Not supported yet. 334 W.write<uint32_t>(0); 335 W.write<uint32_t>(0); 336 337 // Relocation and line-number counts. Not supported yet. 338 W.write<uint16_t>(0); 339 W.write<uint16_t>(0); 340 341 W.write<int32_t>(Sec->Flags); 342 } 343 } 344 345 void XCOFFObjectWriter::writeSymbolTable() { 346 assert(ProgramCodeCsects.size() == 0 && ".text csects not handled yet."); 347 348 // The BSS Section is special in that the csects must contain a single symbol, 349 // and the contained symbol cannot be represented in the symbol table as a 350 // label definition. 351 for (auto &Sec : BSSCsects) { 352 assert(Sec.Syms.size() == 1 && 353 "Uninitialized csect cannot contain more then 1 symbol."); 354 Symbol &Sym = Sec.Syms.back(); 355 356 // Write the symbol's name. 357 if (Sym.nameInStringTable()) { 358 W.write<int32_t>(0); 359 W.write<uint32_t>(Strings.getOffset(Sym.getName())); 360 } else { 361 char Name[XCOFF::NameSize]; 362 std::strncpy(Name, Sym.getName().data(), XCOFF::NameSize); 363 ArrayRef<char> NameRef(Name, XCOFF::NameSize); 364 W.write(NameRef); 365 } 366 367 W.write<uint32_t>(Sec.Address); 368 W.write<int16_t>(BSS.Index); 369 // Basic/Derived type. See the description of the n_type field for symbol 370 // table entries for a detailed description. Since we don't yet support 371 // visibility, and all other bits are either optionally set or reserved, 372 // this is always zero. 373 // TODO FIXME How to assert a symbols visibility is default? 374 W.write<uint16_t>(0); 375 376 W.write<uint8_t>(Sym.getStorageClass()); 377 378 // Always 1 aux entry for now. 379 W.write<uint8_t>(1); 380 381 W.write<uint32_t>(Sec.Size); 382 383 // Parameter typecheck hash. Not supported. 384 W.write<uint32_t>(0); 385 // Typecheck section number. Not supported. 386 W.write<uint16_t>(0); 387 // Symbol type. 388 W.write<uint8_t>(getEncodedType(Sec.MCCsect)); 389 // Storage mapping class. 390 W.write<uint8_t>(Sec.MCCsect->getMappingClass()); 391 // Reserved (x_stab). 392 W.write<uint32_t>(0); 393 // Reserved (x_snstab). 394 W.write<uint16_t>(0); 395 } 396 } 397 398 void XCOFFObjectWriter::assignAddressesAndIndices( 399 const llvm::MCAsmLayout &Layout) { 400 // The address corrresponds to the address of sections and symbols in the 401 // object file. We place the shared address 0 immediately after the 402 // section header table. 403 uint32_t Address = 0; 404 // Section indices are 1-based in XCOFF. 405 uint16_t SectionIndex = 1; 406 // The first symbol table entry is for the file name. We are not emitting it 407 // yet, so start at index 0. 408 uint32_t SymbolTableIndex = 0; 409 410 // Text section comes first. TODO 411 // Data section Second. TODO 412 413 // BSS Section third. 414 if (!BSSCsects.empty()) { 415 Sections.push_back(&BSS); 416 BSS.Index = SectionIndex++; 417 assert(alignTo(Address, DefaultSectionAlign) == Address && 418 "Improperly aligned address for section."); 419 uint32_t StartAddress = Address; 420 for (auto &Csect : BSSCsects) { 421 const MCSectionXCOFF *MCSec = Csect.MCCsect; 422 Address = alignTo(Address, MCSec->getAlignment()); 423 Csect.Address = Address; 424 Address += Layout.getSectionAddressSize(MCSec); 425 Csect.SymbolTableIndex = SymbolTableIndex; 426 // 1 main and 1 auxiliary symbol table entry for the csect. 427 SymbolTableIndex += 2; 428 Csect.Size = Layout.getSectionAddressSize(MCSec); 429 430 assert(Csect.Syms.size() == 1 && 431 "csect in the BSS can only contain a single symbol."); 432 Csect.Syms[0].SymbolTableIndex = Csect.SymbolTableIndex; 433 } 434 // Pad out Address to the default alignment. This is to match how the system 435 // assembler handles the .bss section. Its size is always a multiple of 4. 436 Address = alignTo(Address, DefaultSectionAlign); 437 BSS.Size = Address - StartAddress; 438 } 439 440 SymbolTableEntryCount = SymbolTableIndex; 441 442 // Calculate the RawPointer value for each section. 443 uint64_t RawPointer = sizeof(XCOFF::FileHeader32) + auxiliaryHeaderSize() + 444 Sections.size() * sizeof(XCOFF::SectionHeader32); 445 for (auto *Sec : Sections) { 446 if (!Sec->IsVirtual) { 447 Sec->FileOffsetToData = RawPointer; 448 RawPointer += Sec->Size; 449 } 450 } 451 452 // TODO Add in Relocation storage to the RawPointer Calculation. 453 // TODO What to align the SymbolTable to? 454 // TODO Error check that the number of symbol table entries fits in 32-bits 455 // signed ... 456 if (SymbolTableEntryCount) 457 SymbolTableOffset = RawPointer; 458 } 459 460 // Takes the log base 2 of the alignment and shifts the result into the 5 most 461 // significant bits of a byte, then or's in the csect type into the least 462 // significant 3 bits. 463 uint8_t getEncodedType(const MCSectionXCOFF *Sec) { 464 unsigned Align = Sec->getAlignment(); 465 assert(isPowerOf2_32(Align) && "Alignment must be a power of 2."); 466 unsigned Log2Align = Log2_32(Align); 467 // Result is a number in the range [0, 31] which fits in the 5 least 468 // significant bits. Shift this value into the 5 most significant bits, and 469 // bitwise-or in the csect type. 470 uint8_t EncodedAlign = Log2Align << 3; 471 return EncodedAlign | Sec->getCSectType(); 472 } 473 474 } // end anonymous namespace 475 476 std::unique_ptr<MCObjectWriter> 477 llvm::createXCOFFObjectWriter(std::unique_ptr<MCXCOFFObjectTargetWriter> MOTW, 478 raw_pwrite_stream &OS) { 479 return std::make_unique<XCOFFObjectWriter>(std::move(MOTW), OS); 480 } 481