| 1 | //===- lib/MC/GOFFObjectWriter.cpp - GOFF 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 GOFF object file writer information. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "llvm/BinaryFormat/GOFF.h" |
| 14 | #include "llvm/MC/MCAsmBackend.h" |
| 15 | #include "llvm/MC/MCAssembler.h" |
| 16 | #include "llvm/MC/MCGOFFAttributes.h" |
| 17 | #include "llvm/MC/MCGOFFObjectWriter.h" |
| 18 | #include "llvm/MC/MCObjectWriter.h" |
| 19 | #include "llvm/MC/MCSectionGOFF.h" |
| 20 | #include "llvm/MC/MCSymbolGOFF.h" |
| 21 | #include "llvm/MC/MCValue.h" |
| 22 | #include "llvm/Support/ConvertEBCDIC.h" |
| 23 | #include "llvm/Support/Debug.h" |
| 24 | #include "llvm/Support/Endian.h" |
| 25 | #include "llvm/Support/raw_ostream.h" |
| 26 | |
| 27 | using namespace llvm; |
| 28 | |
| 29 | #define DEBUG_TYPE "goff-writer" |
| 30 | |
| 31 | namespace { |
| 32 | // Common flag values on records. |
| 33 | |
| 34 | // Flag: This record is continued. |
| 35 | constexpr uint8_t RecContinued = GOFF::Flags(7, 1, 1); |
| 36 | |
| 37 | // Flag: This record is a continuation. |
| 38 | constexpr uint8_t RecContinuation = GOFF::Flags(6, 1, 1); |
| 39 | |
| 40 | // The GOFFOstream is responsible to write the data into the fixed physical |
| 41 | // records of the format. A user of this class announces the begin of a new |
| 42 | // logical record. While writing the payload, the physical records are created |
| 43 | // for the data. Possible fill bytes at the end of a physical record are written |
| 44 | // automatically. In principle, the GOFFOstream is agnostic of the endianness of |
| 45 | // the payload. However, it also supports writing data in big endian byte order. |
| 46 | // |
| 47 | // The physical records use the flag field to indicate if the there is a |
| 48 | // successor and predecessor record. To be able to set these flags while |
| 49 | // writing, the basic implementation idea is to always buffer the last seen |
| 50 | // physical record. |
| 51 | class GOFFOstream { |
| 52 | /// The underlying raw_pwrite_stream. |
| 53 | raw_pwrite_stream &OS; |
| 54 | |
| 55 | /// The number of logical records emitted so far. |
| 56 | uint32_t LogicalRecords = 0; |
| 57 | |
| 58 | /// The number of physical records emitted so far. |
| 59 | uint32_t PhysicalRecords = 0; |
| 60 | |
| 61 | /// The size of the buffer. Same as the payload size of a physical record. |
| 62 | static constexpr uint8_t BufferSize = GOFF::PayloadLength; |
| 63 | |
| 64 | /// Current position in buffer. |
| 65 | char *BufferPtr = Buffer; |
| 66 | |
| 67 | /// Static allocated buffer for the stream. |
| 68 | char Buffer[BufferSize]; |
| 69 | |
| 70 | /// The type of the current logical record, and the flags (aka continued and |
| 71 | /// continuation indicators) for the previous (physical) record. |
| 72 | uint8_t TypeAndFlags = 0; |
| 73 | |
| 74 | public: |
| 75 | GOFFOstream(raw_pwrite_stream &OS); |
| 76 | ~GOFFOstream(); |
| 77 | |
| 78 | raw_pwrite_stream &getOS() { return OS; } |
| 79 | size_t getWrittenSize() const { return PhysicalRecords * GOFF::RecordLength; } |
| 80 | uint32_t getNumLogicalRecords() { return LogicalRecords; } |
| 81 | |
| 82 | /// Write the specified bytes. |
| 83 | void write(const char *Ptr, size_t Size); |
| 84 | |
| 85 | /// Write zeroes, up to a maximum of 16 bytes. |
| 86 | void write_zeros(unsigned NumZeros); |
| 87 | |
| 88 | /// Support for endian-specific data. |
| 89 | template <typename value_type> void writebe(value_type Value) { |
| 90 | Value = |
| 91 | support::endian::byte_swap<value_type>(Value, llvm::endianness::big); |
| 92 | write(Ptr: (const char *)&Value, Size: sizeof(value_type)); |
| 93 | } |
| 94 | |
| 95 | /// Begin a new logical record. Implies finalizing the previous record. |
| 96 | void newRecord(GOFF::RecordType Type); |
| 97 | |
| 98 | /// Ends a logical record. |
| 99 | void finalizeRecord(); |
| 100 | |
| 101 | private: |
| 102 | /// Updates the continued/continuation flags, and writes the record prefix of |
| 103 | /// a physical record. |
| 104 | void updateFlagsAndWritePrefix(bool IsContinued); |
| 105 | |
| 106 | /// Returns the remaining size in the buffer. |
| 107 | size_t getRemainingSize(); |
| 108 | }; |
| 109 | } // namespace |
| 110 | |
| 111 | GOFFOstream::GOFFOstream(raw_pwrite_stream &OS) : OS(OS) {} |
| 112 | |
| 113 | GOFFOstream::~GOFFOstream() { finalizeRecord(); } |
| 114 | |
| 115 | void GOFFOstream::updateFlagsAndWritePrefix(bool IsContinued) { |
| 116 | // Update the flags based on the previous state and the flag IsContinued. |
| 117 | if (TypeAndFlags & RecContinued) |
| 118 | TypeAndFlags |= RecContinuation; |
| 119 | if (IsContinued) |
| 120 | TypeAndFlags |= RecContinued; |
| 121 | else |
| 122 | TypeAndFlags &= ~RecContinued; |
| 123 | |
| 124 | OS << static_cast<unsigned char>(GOFF::PTVPrefix) // Record Type |
| 125 | << static_cast<unsigned char>(TypeAndFlags) // Continuation |
| 126 | << static_cast<unsigned char>(0); // Version |
| 127 | |
| 128 | ++PhysicalRecords; |
| 129 | } |
| 130 | |
| 131 | size_t GOFFOstream::getRemainingSize() { |
| 132 | return size_t(&Buffer[BufferSize] - BufferPtr); |
| 133 | } |
| 134 | |
| 135 | void GOFFOstream::write(const char *Ptr, size_t Size) { |
| 136 | size_t RemainingSize = getRemainingSize(); |
| 137 | |
| 138 | // Data fits into the buffer. |
| 139 | if (LLVM_LIKELY(Size <= RemainingSize)) { |
| 140 | memcpy(dest: BufferPtr, src: Ptr, n: Size); |
| 141 | BufferPtr += Size; |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | // Otherwise the buffer is partially filled or full, and data does not fit |
| 146 | // into it. |
| 147 | updateFlagsAndWritePrefix(/*IsContinued=*/true); |
| 148 | OS.write(Ptr: Buffer, Size: size_t(BufferPtr - Buffer)); |
| 149 | if (RemainingSize > 0) { |
| 150 | OS.write(Ptr, Size: RemainingSize); |
| 151 | Ptr += RemainingSize; |
| 152 | Size -= RemainingSize; |
| 153 | } |
| 154 | |
| 155 | while (Size > BufferSize) { |
| 156 | updateFlagsAndWritePrefix(/*IsContinued=*/true); |
| 157 | OS.write(Ptr, Size: BufferSize); |
| 158 | Ptr += BufferSize; |
| 159 | Size -= BufferSize; |
| 160 | } |
| 161 | |
| 162 | // The remaining bytes fit into the buffer. |
| 163 | memcpy(dest: Buffer, src: Ptr, n: Size); |
| 164 | BufferPtr = &Buffer[Size]; |
| 165 | } |
| 166 | |
| 167 | void GOFFOstream::write_zeros(unsigned NumZeros) { |
| 168 | assert(NumZeros <= 16 && "Range for zeros too large" ); |
| 169 | |
| 170 | // Handle the common case first: all fits in the buffer. |
| 171 | size_t RemainingSize = getRemainingSize(); |
| 172 | if (LLVM_LIKELY(RemainingSize >= NumZeros)) { |
| 173 | memset(s: BufferPtr, c: 0, n: NumZeros); |
| 174 | BufferPtr += NumZeros; |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | // Otherwise some field value is cleared. |
| 179 | static char Zeros[16] = { |
| 180 | 0, |
| 181 | }; |
| 182 | write(Ptr: Zeros, Size: NumZeros); |
| 183 | } |
| 184 | |
| 185 | void GOFFOstream::newRecord(GOFF::RecordType Type) { |
| 186 | finalizeRecord(); |
| 187 | TypeAndFlags = Type << 4; |
| 188 | ++LogicalRecords; |
| 189 | } |
| 190 | |
| 191 | void GOFFOstream::finalizeRecord() { |
| 192 | if (Buffer == BufferPtr) |
| 193 | return; |
| 194 | updateFlagsAndWritePrefix(/*IsContinued=*/false); |
| 195 | OS.write(Ptr: Buffer, Size: size_t(BufferPtr - Buffer)); |
| 196 | OS.write_zeros(NumZeros: getRemainingSize()); |
| 197 | BufferPtr = Buffer; |
| 198 | } |
| 199 | |
| 200 | namespace { |
| 201 | // A GOFFSymbol holds all the data required for writing an ESD record. |
| 202 | class GOFFSymbol { |
| 203 | public: |
| 204 | std::string Name; |
| 205 | uint32_t EsdId; |
| 206 | uint32_t ParentEsdId; |
| 207 | uint64_t Offset = 0; // Offset of the symbol into the section. LD only. |
| 208 | // Offset is only 32 bit, the larger type is used to |
| 209 | // enable error checking. |
| 210 | GOFF::ESDSymbolType SymbolType; |
| 211 | GOFF::ESDNameSpaceId NameSpace = GOFF::ESD_NS_ProgramManagementBinder; |
| 212 | |
| 213 | GOFF::BehavioralAttributes BehavAttrs; |
| 214 | GOFF::SymbolFlags SymbolFlags; |
| 215 | uint32_t SortKey = 0; |
| 216 | uint32_t SectionLength = 0; |
| 217 | uint32_t ADAEsdId = 0; |
| 218 | uint32_t EASectionEDEsdId = 0; |
| 219 | uint32_t EASectionOffset = 0; |
| 220 | uint8_t FillByteValue = 0; |
| 221 | |
| 222 | GOFFSymbol() : EsdId(0), ParentEsdId(0) {} |
| 223 | |
| 224 | GOFFSymbol(StringRef Name, uint32_t EsdID, const GOFF::SDAttr &Attr) |
| 225 | : Name(Name.data(), Name.size()), EsdId(EsdID), ParentEsdId(0), |
| 226 | SymbolType(GOFF::ESD_ST_SectionDefinition) { |
| 227 | BehavAttrs.setTaskingBehavior(Attr.TaskingBehavior); |
| 228 | BehavAttrs.setBindingScope(Attr.BindingScope); |
| 229 | } |
| 230 | |
| 231 | GOFFSymbol(StringRef Name, uint32_t EsdID, uint32_t ParentEsdID, |
| 232 | const GOFF::EDAttr &Attr) |
| 233 | : Name(Name.data(), Name.size()), EsdId(EsdID), ParentEsdId(ParentEsdID), |
| 234 | SymbolType(GOFF::ESD_ST_ElementDefinition) { |
| 235 | this->NameSpace = Attr.NameSpace; |
| 236 | // We always set a fill byte value. |
| 237 | this->FillByteValue = Attr.FillByteValue; |
| 238 | SymbolFlags.setFillBytePresence(1); |
| 239 | SymbolFlags.setReservedQwords(Attr.ReservedQwords); |
| 240 | // TODO Do we need/should set the "mangled" flag? |
| 241 | BehavAttrs.setReadOnly(Attr.IsReadOnly); |
| 242 | BehavAttrs.setRmode(Attr.Rmode); |
| 243 | BehavAttrs.setTextStyle(Attr.TextStyle); |
| 244 | BehavAttrs.setBindingAlgorithm(Attr.BindAlgorithm); |
| 245 | BehavAttrs.setLoadingBehavior(Attr.LoadBehavior); |
| 246 | BehavAttrs.setAlignment(Attr.Alignment); |
| 247 | } |
| 248 | |
| 249 | GOFFSymbol(StringRef Name, uint32_t EsdID, uint32_t ParentEsdID, |
| 250 | GOFF::ESDNameSpaceId NameSpace, const GOFF::LDAttr &Attr) |
| 251 | : Name(Name.data(), Name.size()), EsdId(EsdID), ParentEsdId(ParentEsdID), |
| 252 | SymbolType(GOFF::ESD_ST_LabelDefinition), NameSpace(NameSpace) { |
| 253 | SymbolFlags.setRenameable(Attr.IsRenamable); |
| 254 | BehavAttrs.setExecutable(Attr.Executable); |
| 255 | BehavAttrs.setBindingStrength(Attr.BindingStrength); |
| 256 | BehavAttrs.setLinkageType(Attr.Linkage); |
| 257 | BehavAttrs.setAmode(Attr.Amode); |
| 258 | BehavAttrs.setBindingScope(Attr.BindingScope); |
| 259 | } |
| 260 | |
| 261 | GOFFSymbol(StringRef Name, uint32_t EsdID, uint32_t ParentEsdID, |
| 262 | const GOFF::EDAttr &EDAttr, const GOFF::PRAttr &Attr) |
| 263 | : Name(Name.data(), Name.size()), EsdId(EsdID), ParentEsdId(ParentEsdID), |
| 264 | SymbolType(GOFF::ESD_ST_PartReference), NameSpace(EDAttr.NameSpace) { |
| 265 | SymbolFlags.setRenameable(Attr.IsRenamable); |
| 266 | BehavAttrs.setExecutable(Attr.Executable); |
| 267 | BehavAttrs.setLinkageType(Attr.Linkage); |
| 268 | BehavAttrs.setBindingScope(Attr.BindingScope); |
| 269 | BehavAttrs.setAlignment(EDAttr.Alignment); |
| 270 | } |
| 271 | |
| 272 | GOFFSymbol(StringRef Name, uint32_t EsdID, uint32_t ParentEsdID, |
| 273 | const GOFF::ERAttr &Attr) |
| 274 | : Name(Name.data(), Name.size()), EsdId(EsdID), ParentEsdId(ParentEsdID), |
| 275 | SymbolType(GOFF::ESD_ST_ExternalReference), |
| 276 | NameSpace(GOFF::ESD_NS_NormalName) { |
| 277 | BehavAttrs.setExecutable(Attr.Executable); |
| 278 | BehavAttrs.setBindingStrength(Attr.BindingStrength); |
| 279 | BehavAttrs.setLinkageType(Attr.Linkage); |
| 280 | BehavAttrs.setAmode(Attr.Amode); |
| 281 | BehavAttrs.setBindingScope(Attr.BindingScope); |
| 282 | BehavAttrs.setIndirectReference(Attr.IsIndirectReference); |
| 283 | } |
| 284 | }; |
| 285 | |
| 286 | class GOFFWriter { |
| 287 | GOFFOstream OS; |
| 288 | MCAssembler &Asm; |
| 289 | MCSectionGOFF *RootSD; |
| 290 | |
| 291 | /// Saved relocation data collected in recordRelocations(). |
| 292 | std::vector<GOFFRelocationEntry> &Relocations; |
| 293 | |
| 294 | void writeHeader(); |
| 295 | void writeSymbol(const GOFFSymbol &Symbol); |
| 296 | void writeText(const MCSectionGOFF *MC); |
| 297 | void writeRelocations(); |
| 298 | void writeEnd(); |
| 299 | |
| 300 | void defineSectionSymbols(const MCSectionGOFF &Section); |
| 301 | void defineLabel(const MCSymbolGOFF &Symbol); |
| 302 | void defineExtern(const MCSymbolGOFF &Symbol); |
| 303 | void defineSymbols(); |
| 304 | |
| 305 | public: |
| 306 | GOFFWriter(raw_pwrite_stream &OS, MCAssembler &Asm, MCSectionGOFF *RootSD, |
| 307 | std::vector<GOFFRelocationEntry> &Relocations); |
| 308 | uint64_t writeObject(); |
| 309 | }; |
| 310 | } // namespace |
| 311 | |
| 312 | GOFFWriter::GOFFWriter(raw_pwrite_stream &OS, MCAssembler &Asm, |
| 313 | MCSectionGOFF *RootSD, |
| 314 | std::vector<GOFFRelocationEntry> &Relocations) |
| 315 | : OS(OS), Asm(Asm), RootSD(RootSD), Relocations(Relocations) {} |
| 316 | |
| 317 | void GOFFWriter::defineSectionSymbols(const MCSectionGOFF &Section) { |
| 318 | if (Section.isSD()) { |
| 319 | GOFFSymbol SD(Section.getExternalName(), Section.getOrdinal(), |
| 320 | Section.getSDAttributes()); |
| 321 | writeSymbol(Symbol: SD); |
| 322 | } |
| 323 | |
| 324 | if (Section.isED()) { |
| 325 | GOFFSymbol ED(Section.getExternalName(), Section.getOrdinal(), |
| 326 | Section.getParent()->getOrdinal(), Section.getEDAttributes()); |
| 327 | ED.SectionLength = Asm.getSectionAddressSize(Sec: Section); |
| 328 | writeSymbol(Symbol: ED); |
| 329 | } |
| 330 | |
| 331 | if (Section.isPR()) { |
| 332 | MCSectionGOFF *Parent = Section.getParent(); |
| 333 | GOFFSymbol PR(Section.getExternalName(), Section.getOrdinal(), |
| 334 | Parent->getOrdinal(), Parent->getEDAttributes(), |
| 335 | Section.getPRAttributes()); |
| 336 | PR.SectionLength = Asm.getSectionAddressSize(Sec: Section); |
| 337 | if (Section.requiresNonZeroLength()) { |
| 338 | // We cannot have a zero-length section for data. If we do, |
| 339 | // artificially inflate it. Use 2 bytes to avoid odd alignments. Note: |
| 340 | // if this is ever changed, you will need to update the code in |
| 341 | // SystemZAsmPrinter::emitCEEMAIN and SystemZAsmPrinter::emitCELQMAIN to |
| 342 | // generate -1 if there is no ADA |
| 343 | if (!PR.SectionLength) |
| 344 | PR.SectionLength = 2; |
| 345 | } |
| 346 | writeSymbol(Symbol: PR); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | void GOFFWriter::defineLabel(const MCSymbolGOFF &Symbol) { |
| 351 | MCSectionGOFF &Section = static_cast<MCSectionGOFF &>(Symbol.getSection()); |
| 352 | GOFFSymbol LD(Symbol.getExternalName(), Symbol.getIndex(), |
| 353 | Section.getOrdinal(), Section.getEDAttributes().NameSpace, |
| 354 | GOFF::LDAttr{.IsRenamable: false, .Executable: Symbol.getCodeData(), |
| 355 | .BindingStrength: Symbol.getBindingStrength(), .Linkage: Symbol.getLinkage(), |
| 356 | .Amode: GOFF::ESD_AMODE_64, .BindingScope: Symbol.getBindingScope()}); |
| 357 | if (Symbol.getADA()) |
| 358 | LD.ADAEsdId = Symbol.getADA()->getOrdinal(); |
| 359 | LD.Offset = Asm.getSymbolOffset(S: Symbol); |
| 360 | writeSymbol(Symbol: LD); |
| 361 | } |
| 362 | |
| 363 | void GOFFWriter::defineExtern(const MCSymbolGOFF &Symbol) { |
| 364 | if (Symbol.getCodeData() == GOFF::ESD_EXE_DATA) { |
| 365 | MCSectionGOFF *ED = Symbol.getADA()->getParent(); |
| 366 | GOFFSymbol PR(Symbol.getExternalName(), Symbol.getIndex(), ED->getOrdinal(), |
| 367 | ED->getEDAttributes(), |
| 368 | GOFF::PRAttr{/*IsRenamable*/ false, .Executable: Symbol.getCodeData(), |
| 369 | .Linkage: Symbol.getLinkage(), .BindingScope: Symbol.getBindingScope(), |
| 370 | .SortKey: 0}); |
| 371 | writeSymbol(Symbol: PR); |
| 372 | } else { |
| 373 | GOFFSymbol ER(Symbol.getExternalName(), Symbol.getIndex(), |
| 374 | RootSD->getOrdinal(), |
| 375 | GOFF::ERAttr{.IsIndirectReference: Symbol.isIndirect(), .Executable: Symbol.getCodeData(), |
| 376 | .BindingStrength: Symbol.getBindingStrength(), .Linkage: Symbol.getLinkage(), |
| 377 | .Amode: GOFF::ESD_AMODE_64, .BindingScope: Symbol.getBindingScope()}); |
| 378 | writeSymbol(Symbol: ER); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | void GOFFWriter::defineSymbols() { |
| 383 | unsigned Ordinal = 0; |
| 384 | // Process all sections. |
| 385 | for (MCSection &S : Asm) { |
| 386 | auto &Section = static_cast<MCSectionGOFF &>(S); |
| 387 | Section.setOrdinal(++Ordinal); |
| 388 | defineSectionSymbols(Section); |
| 389 | } |
| 390 | |
| 391 | // Process all symbols |
| 392 | for (const MCSymbol &Sym : Asm.symbols()) { |
| 393 | if (Sym.isTemporary()) |
| 394 | continue; |
| 395 | auto &Symbol = static_cast<const MCSymbolGOFF &>(Sym); |
| 396 | if (!Symbol.isDefined()) { |
| 397 | Symbol.setIndex(++Ordinal); |
| 398 | defineExtern(Symbol); |
| 399 | } else if (Symbol.isInEDSection()) { |
| 400 | Symbol.setIndex(++Ordinal); |
| 401 | defineLabel(Symbol); |
| 402 | } else { |
| 403 | // Symbol is in PR section, the symbol refers to the section. |
| 404 | Symbol.setIndex(Symbol.getSection().getOrdinal()); |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | void GOFFWriter::() { |
| 410 | OS.newRecord(Type: GOFF::RT_HDR); |
| 411 | OS.write_zeros(NumZeros: 1); // Reserved |
| 412 | OS.writebe<uint32_t>(Value: 0); // Target Hardware Environment |
| 413 | OS.writebe<uint32_t>(Value: 0); // Target Operating System Environment |
| 414 | OS.write_zeros(NumZeros: 2); // Reserved |
| 415 | OS.writebe<uint16_t>(Value: 0); // CCSID |
| 416 | OS.write_zeros(NumZeros: 16); // Character Set name |
| 417 | OS.write_zeros(NumZeros: 16); // Language Product Identifier |
| 418 | OS.writebe<uint32_t>(Value: 1); // Architecture Level |
| 419 | OS.writebe<uint16_t>(Value: 0); // Module Properties Length |
| 420 | OS.write_zeros(NumZeros: 6); // Reserved |
| 421 | } |
| 422 | |
| 423 | void GOFFWriter::writeSymbol(const GOFFSymbol &Symbol) { |
| 424 | if (Symbol.Offset >= (((uint64_t)1) << 31)) |
| 425 | report_fatal_error(reason: "ESD offset out of range" ); |
| 426 | |
| 427 | // All symbol names are in EBCDIC. |
| 428 | SmallString<256> Name; |
| 429 | ConverterEBCDIC::convertToEBCDIC(Source: Symbol.Name, Result&: Name); |
| 430 | |
| 431 | // Check length here since this number is technically signed but we need uint |
| 432 | // for writing to records. |
| 433 | if (Name.size() >= GOFF::MaxDataLength) |
| 434 | report_fatal_error(reason: "Symbol max name length exceeded" ); |
| 435 | uint16_t NameLength = Name.size(); |
| 436 | |
| 437 | OS.newRecord(Type: GOFF::RT_ESD); |
| 438 | OS.writebe<uint8_t>(Value: Symbol.SymbolType); // Symbol Type |
| 439 | OS.writebe<uint32_t>(Value: Symbol.EsdId); // ESDID |
| 440 | OS.writebe<uint32_t>(Value: Symbol.ParentEsdId); // Parent or Owning ESDID |
| 441 | OS.writebe<uint32_t>(Value: 0); // Reserved |
| 442 | OS.writebe<uint32_t>( |
| 443 | Value: static_cast<uint32_t>(Symbol.Offset)); // Offset or Address |
| 444 | OS.writebe<uint32_t>(Value: 0); // Reserved |
| 445 | OS.writebe<uint32_t>(Value: Symbol.SectionLength); // Length |
| 446 | OS.writebe<uint32_t>(Value: Symbol.EASectionEDEsdId); // Extended Attribute ESDID |
| 447 | OS.writebe<uint32_t>(Value: Symbol.EASectionOffset); // Extended Attribute Offset |
| 448 | OS.writebe<uint32_t>(Value: 0); // Reserved |
| 449 | OS.writebe<uint8_t>(Value: Symbol.NameSpace); // Name Space ID |
| 450 | OS.writebe<uint8_t>(Value: Symbol.SymbolFlags); // Flags |
| 451 | OS.writebe<uint8_t>(Value: Symbol.FillByteValue); // Fill-Byte Value |
| 452 | OS.writebe<uint8_t>(Value: 0); // Reserved |
| 453 | OS.writebe<uint32_t>(Value: Symbol.ADAEsdId); // ADA ESDID |
| 454 | OS.writebe<uint32_t>(Value: Symbol.SortKey); // Sort Priority |
| 455 | OS.writebe<uint64_t>(Value: 0); // Reserved |
| 456 | for (auto F : Symbol.BehavAttrs.Attr) |
| 457 | OS.writebe<uint8_t>(Value: F); // Behavioral Attributes |
| 458 | OS.writebe<uint16_t>(Value: NameLength); // Name Length |
| 459 | OS.write(Ptr: Name.data(), Size: NameLength); // Name |
| 460 | } |
| 461 | |
| 462 | namespace { |
| 463 | /// Adapter stream to write a text section. |
| 464 | class TextStream : public raw_ostream { |
| 465 | /// The underlying GOFFOstream. |
| 466 | GOFFOstream &OS; |
| 467 | |
| 468 | /// The buffer size is the maximum number of bytes in a TXT section. |
| 469 | static constexpr size_t BufferSize = GOFF::MaxDataLength; |
| 470 | |
| 471 | /// Static allocated buffer for the stream, used by the raw_ostream class. The |
| 472 | /// buffer is sized to hold the payload of a logical TXT record. |
| 473 | char Buffer[BufferSize]; |
| 474 | |
| 475 | /// The offset for the next TXT record. This is equal to the number of bytes |
| 476 | /// written. |
| 477 | size_t Offset; |
| 478 | |
| 479 | /// The Esdid of the GOFF section. |
| 480 | const uint32_t EsdId; |
| 481 | |
| 482 | /// The record style. |
| 483 | const GOFF::ESDTextStyle RecordStyle; |
| 484 | |
| 485 | /// See raw_ostream::write_impl. |
| 486 | void write_impl(const char *Ptr, size_t Size) override; |
| 487 | |
| 488 | uint64_t current_pos() const override { return Offset; } |
| 489 | |
| 490 | public: |
| 491 | explicit TextStream(GOFFOstream &OS, uint32_t EsdId, |
| 492 | GOFF::ESDTextStyle RecordStyle) |
| 493 | : OS(OS), Offset(0), EsdId(EsdId), RecordStyle(RecordStyle) { |
| 494 | SetBuffer(BufferStart: Buffer, Size: sizeof(Buffer)); |
| 495 | } |
| 496 | |
| 497 | ~TextStream() override { flush(); } |
| 498 | }; |
| 499 | } // namespace |
| 500 | |
| 501 | void TextStream::write_impl(const char *Ptr, size_t Size) { |
| 502 | size_t WrittenLength = 0; |
| 503 | |
| 504 | // We only have signed 32bits of offset. |
| 505 | if (Offset + Size > std::numeric_limits<int32_t>::max()) |
| 506 | report_fatal_error(reason: "TXT section too large" ); |
| 507 | |
| 508 | while (WrittenLength < Size) { |
| 509 | size_t ToWriteLength = |
| 510 | std::min(a: Size - WrittenLength, b: size_t(GOFF::MaxDataLength)); |
| 511 | |
| 512 | OS.newRecord(Type: GOFF::RT_TXT); |
| 513 | OS.writebe<uint8_t>(Value: GOFF::Flags(4, 4, RecordStyle)); // Text Record Style |
| 514 | OS.writebe<uint32_t>(Value: EsdId); // Element ESDID |
| 515 | OS.writebe<uint32_t>(Value: 0); // Reserved |
| 516 | OS.writebe<uint32_t>(Value: static_cast<uint32_t>(Offset)); // Offset |
| 517 | OS.writebe<uint32_t>(Value: 0); // Text Field True Length |
| 518 | OS.writebe<uint16_t>(Value: 0); // Text Encoding |
| 519 | OS.writebe<uint16_t>(Value: ToWriteLength); // Data Length |
| 520 | OS.write(Ptr: Ptr + WrittenLength, Size: ToWriteLength); // Data |
| 521 | |
| 522 | WrittenLength += ToWriteLength; |
| 523 | Offset += ToWriteLength; |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | void GOFFWriter::writeText(const MCSectionGOFF *Section) { |
| 528 | // A BSS section contains only zeros, no need to write this. |
| 529 | if (Section->isBSS()) |
| 530 | return; |
| 531 | |
| 532 | TextStream S(OS, Section->getOrdinal(), Section->getTextStyle()); |
| 533 | Asm.writeSectionData(OS&: S, Section); |
| 534 | } |
| 535 | |
| 536 | namespace { |
| 537 | // RelocDataItemBuffer provides a static buffer for relocation data items. |
| 538 | class RelocDataItemBuffer { |
| 539 | char Buffer[GOFF::MaxDataLength]; |
| 540 | char *Ptr; |
| 541 | |
| 542 | public: |
| 543 | RelocDataItemBuffer() : Ptr(Buffer) {} |
| 544 | const char *data() { return Buffer; } |
| 545 | size_t size() { return Ptr - Buffer; } |
| 546 | void reset() { Ptr = Buffer; } |
| 547 | bool fits(size_t S) { return size() + S < GOFF::MaxDataLength; } |
| 548 | template <typename T> void writebe(T Val) { |
| 549 | assert(fits(sizeof(T)) && "Out-of-bounds write" ); |
| 550 | support::endian::write<T, llvm::endianness::big>(Ptr, Val); |
| 551 | Ptr += sizeof(T); |
| 552 | } |
| 553 | }; |
| 554 | } // namespace |
| 555 | |
| 556 | void GOFFWriter::writeRelocations() { |
| 557 | // Set the IDs in the relocation entries. |
| 558 | for (auto &RelocEntry : Relocations) { |
| 559 | auto GetRptr = [](const MCSymbolGOFF *Sym) -> uint32_t { |
| 560 | if (Sym->isTemporary()) |
| 561 | return static_cast<MCSectionGOFF &>(Sym->getSection()) |
| 562 | .getBeginSymbol() |
| 563 | ->getIndex(); |
| 564 | return Sym->getIndex(); |
| 565 | }; |
| 566 | |
| 567 | RelocEntry.PEsdId = RelocEntry.Pptr->getOrdinal(); |
| 568 | RelocEntry.REsdId = GetRptr(RelocEntry.Rptr); |
| 569 | } |
| 570 | |
| 571 | // Sort relocation data items by the P pointer to save space. |
| 572 | std::sort( |
| 573 | first: Relocations.begin(), last: Relocations.end(), |
| 574 | comp: [](const GOFFRelocationEntry &Left, const GOFFRelocationEntry &Right) { |
| 575 | return std::tie(args: Left.PEsdId, args: Left.REsdId, args: Left.POffset) < |
| 576 | std::tie(args: Right.PEsdId, args: Right.REsdId, args: Right.POffset); |
| 577 | }); |
| 578 | |
| 579 | // Construct the compressed relocation data items, and write them out. |
| 580 | RelocDataItemBuffer Buffer; |
| 581 | for (auto I = Relocations.begin(), E = Relocations.end(); I != E;) { |
| 582 | Buffer.reset(); |
| 583 | |
| 584 | uint32_t PrevResdId = -1; |
| 585 | uint32_t PrevPesdId = -1; |
| 586 | uint64_t PrevPOffset = -1; |
| 587 | for (; I != E; ++I) { |
| 588 | const GOFFRelocationEntry &Rel = *I; |
| 589 | |
| 590 | bool SameREsdId = (Rel.REsdId == PrevResdId); |
| 591 | bool SamePEsdId = (Rel.PEsdId == PrevPesdId); |
| 592 | bool SamePOffset = (Rel.POffset == PrevPOffset); |
| 593 | bool EightByteOffset = ((Rel.POffset >> 32) & 0xffffffff); |
| 594 | |
| 595 | // Calculate size of relocation data item, and check if it still fits into |
| 596 | // the record. |
| 597 | size_t ItemSize = 8; // Smallest size of a relocation data item. |
| 598 | if (!SameREsdId) |
| 599 | ItemSize += 4; |
| 600 | if (!SamePEsdId) |
| 601 | ItemSize += 4; |
| 602 | if (!SamePOffset) |
| 603 | ItemSize += (EightByteOffset ? 8 : 4); |
| 604 | if (!Buffer.fits(S: ItemSize)) |
| 605 | break; |
| 606 | |
| 607 | GOFF::Flags RelocFlags[6]; |
| 608 | RelocFlags[0].set(BitIndex: 0, Length: 1, NewValue: SameREsdId); |
| 609 | RelocFlags[0].set(BitIndex: 1, Length: 1, NewValue: SamePEsdId); |
| 610 | RelocFlags[0].set(BitIndex: 2, Length: 1, NewValue: SamePOffset); |
| 611 | RelocFlags[0].set(BitIndex: 6, Length: 1, NewValue: EightByteOffset); |
| 612 | |
| 613 | RelocFlags[1].set(BitIndex: 0, Length: 4, NewValue: Rel.ReferenceType); |
| 614 | RelocFlags[1].set(BitIndex: 4, Length: 4, NewValue: Rel.ReferentType); |
| 615 | |
| 616 | RelocFlags[2].set(BitIndex: 0, Length: 7, NewValue: Rel.Action); |
| 617 | RelocFlags[2].set(BitIndex: 7, Length: 1, NewValue: Rel.FetchStore); |
| 618 | |
| 619 | RelocFlags[4].set(BitIndex: 0, Length: 8, NewValue: Rel.TargetLength); |
| 620 | |
| 621 | for (auto F : RelocFlags) |
| 622 | Buffer.writebe<uint8_t>(Val: F); |
| 623 | Buffer.writebe<uint16_t>(Val: 0); // Reserved. |
| 624 | if (!SameREsdId) |
| 625 | Buffer.writebe<uint32_t>(Val: Rel.REsdId); |
| 626 | if (!SamePEsdId) |
| 627 | Buffer.writebe<uint32_t>(Val: Rel.PEsdId); |
| 628 | if (!SamePOffset) { |
| 629 | if (EightByteOffset) |
| 630 | Buffer.writebe<uint64_t>(Val: Rel.POffset); |
| 631 | else |
| 632 | Buffer.writebe<uint32_t>(Val: Rel.POffset); |
| 633 | } |
| 634 | |
| 635 | PrevResdId = Rel.REsdId; |
| 636 | PrevPesdId = Rel.PEsdId; |
| 637 | PrevPOffset = Rel.POffset; |
| 638 | } |
| 639 | |
| 640 | OS.newRecord(Type: GOFF::RT_RLD); |
| 641 | OS.writebe<uint8_t>(Value: 0); // Reserved. |
| 642 | OS.writebe<uint16_t>(Value: Buffer.size()); // Length (of the relocation data). |
| 643 | OS.write(Ptr: Buffer.data(), Size: Buffer.size()); // Relocation Directory Data Items. |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | void GOFFWriter::writeEnd() { |
| 648 | uint8_t F = GOFF::END_EPR_None; |
| 649 | uint8_t AMODE = 0; |
| 650 | uint32_t ESDID = 0; |
| 651 | |
| 652 | // TODO Set Flags/AMODE/ESDID for entry point. |
| 653 | |
| 654 | OS.newRecord(Type: GOFF::RT_END); |
| 655 | OS.writebe<uint8_t>(Value: GOFF::Flags(6, 2, F)); // Indicator flags |
| 656 | OS.writebe<uint8_t>(Value: AMODE); // AMODE |
| 657 | OS.write_zeros(NumZeros: 3); // Reserved |
| 658 | // The record count is the number of logical records. In principle, this value |
| 659 | // is available as OS.logicalRecords(). However, some tools rely on this field |
| 660 | // being zero. |
| 661 | OS.writebe<uint32_t>(Value: 0); // Record Count |
| 662 | OS.writebe<uint32_t>(Value: ESDID); // ESDID (of entry point) |
| 663 | } |
| 664 | |
| 665 | uint64_t GOFFWriter::writeObject() { |
| 666 | writeHeader(); |
| 667 | |
| 668 | defineSymbols(); |
| 669 | |
| 670 | for (const MCSection &Section : Asm) |
| 671 | writeText(Section: static_cast<const MCSectionGOFF *>(&Section)); |
| 672 | |
| 673 | writeRelocations(); |
| 674 | |
| 675 | writeEnd(); |
| 676 | |
| 677 | // Make sure all records are written. |
| 678 | OS.finalizeRecord(); |
| 679 | |
| 680 | LLVM_DEBUG(dbgs() << "Wrote " << OS.getNumLogicalRecords() |
| 681 | << " logical records." ); |
| 682 | |
| 683 | return OS.getWrittenSize(); |
| 684 | } |
| 685 | |
| 686 | GOFFObjectWriter::GOFFObjectWriter( |
| 687 | std::unique_ptr<MCGOFFObjectTargetWriter> MOTW, raw_pwrite_stream &OS) |
| 688 | : TargetObjectWriter(std::move(MOTW)), OS(OS) {} |
| 689 | |
| 690 | GOFFObjectWriter::~GOFFObjectWriter() = default; |
| 691 | |
| 692 | void GOFFObjectWriter::reset() { |
| 693 | Relocations.clear(); |
| 694 | RootSD = nullptr; |
| 695 | MCObjectWriter::reset(); |
| 696 | } |
| 697 | |
| 698 | void GOFFObjectWriter::recordRelocation(const MCFragment &F, |
| 699 | const MCFixup &Fixup, MCValue Target, |
| 700 | uint64_t &FixedValue) { |
| 701 | const MCFixupKindInfo &FKI = |
| 702 | Asm->getBackend().getFixupKindInfo(Kind: Fixup.getKind()); |
| 703 | const uint32_t Length = FKI.TargetSize / 8; |
| 704 | assert(FKI.TargetSize % 8 == 0 && "Target Size not multiple of 8" ); |
| 705 | const uint64_t FixupOffset = Asm->getFragmentOffset(F) + Fixup.getOffset(); |
| 706 | |
| 707 | unsigned RelocType = TargetObjectWriter->getRelocType(Target, Fixup); |
| 708 | |
| 709 | const MCSectionGOFF *PSection = static_cast<MCSectionGOFF *>(F.getParent()); |
| 710 | const auto &A = *static_cast<const MCSymbolGOFF *>(Target.getAddSym()); |
| 711 | const MCSymbolGOFF *B = static_cast<const MCSymbolGOFF *>(Target.getSubSym()); |
| 712 | if (RelocType == MCGOFFObjectTargetWriter::Reloc_Type_RICon) { |
| 713 | if (A.isUndefined()) { |
| 714 | Asm->reportError( |
| 715 | L: Fixup.getLoc(), |
| 716 | Msg: Twine("symbol " ) |
| 717 | .concat(Suffix: A.getExternalName()) |
| 718 | .concat(Suffix: " must be defined for a relative immediate relocation" )); |
| 719 | return; |
| 720 | } |
| 721 | if (&A.getSection() != PSection) { |
| 722 | MCSectionGOFF &GOFFSection = static_cast<MCSectionGOFF &>(A.getSection()); |
| 723 | Asm->reportError(L: Fixup.getLoc(), |
| 724 | Msg: Twine("relative immediate relocation section mismatch: " ) |
| 725 | .concat(Suffix: GOFFSection.getExternalName()) |
| 726 | .concat(Suffix: " of symbol " ) |
| 727 | .concat(Suffix: A.getExternalName()) |
| 728 | .concat(Suffix: " <-> " ) |
| 729 | .concat(Suffix: PSection->getExternalName())); |
| 730 | return; |
| 731 | } |
| 732 | if (B) { |
| 733 | Asm->reportError( |
| 734 | L: Fixup.getLoc(), |
| 735 | Msg: Twine("subtractive symbol " ) |
| 736 | .concat(Suffix: B->getExternalName()) |
| 737 | .concat(Suffix: " not supported for a relative immediate relocation" )); |
| 738 | return; |
| 739 | } |
| 740 | FixedValue = Asm->getSymbolOffset(S: A) - FixupOffset + Target.getConstant(); |
| 741 | return; |
| 742 | } |
| 743 | FixedValue = Target.getConstant(); |
| 744 | |
| 745 | // The symbol only has a section-relative offset if it is a temporary symbol. |
| 746 | FixedValue += A.isTemporary() ? Asm->getSymbolOffset(S: A) : 0; |
| 747 | A.setUsedInReloc(); |
| 748 | if (B) { |
| 749 | FixedValue -= B->isTemporary() ? Asm->getSymbolOffset(S: *B) : 0; |
| 750 | B->setUsedInReloc(); |
| 751 | } |
| 752 | |
| 753 | // UseQCon causes class offsets versus absolute addresses to be used. This |
| 754 | // is analogous to using QCONs in older OBJ object file format. |
| 755 | bool UseQCon = RelocType == MCGOFFObjectTargetWriter::Reloc_Type_QCon; |
| 756 | |
| 757 | GOFF::RLDFetchStore FetchStore = |
| 758 | (RelocType == MCGOFFObjectTargetWriter::Reloc_Type_RCon || |
| 759 | RelocType == MCGOFFObjectTargetWriter::Reloc_Type_VCon) |
| 760 | ? GOFF::RLDFetchStore::RLD_FS_Store |
| 761 | : GOFF::RLDFetchStore::RLD_FS_Fetch; |
| 762 | assert((FetchStore == GOFF::RLDFetchStore::RLD_FS_Fetch || B == nullptr) && |
| 763 | "No dependent relocations expected" ); |
| 764 | |
| 765 | enum GOFF::RLDReferenceType ReferenceType = GOFF::RLD_RT_RAddress; |
| 766 | enum GOFF::RLDReferentType ReferentType = GOFF::RLD_RO_Label; |
| 767 | if (UseQCon) { |
| 768 | ReferenceType = GOFF::RLD_RT_ROffset; |
| 769 | ReferentType = GOFF::RLD_RO_Class; |
| 770 | } |
| 771 | if (RelocType == MCGOFFObjectTargetWriter::Reloc_Type_RCon) |
| 772 | ReferenceType = GOFF::RLD_RT_RTypeConstant; |
| 773 | |
| 774 | auto DumpReloc = [&PSection, &ReferenceType, &FixupOffset, |
| 775 | &FixedValue](const char *N, const MCSymbolGOFF *Sym) { |
| 776 | const char *Con; |
| 777 | switch (ReferenceType) { |
| 778 | case GOFF::RLDReferenceType::RLD_RT_RAddress: |
| 779 | Con = "ACon" ; |
| 780 | break; |
| 781 | case GOFF::RLDReferenceType::RLD_RT_ROffset: |
| 782 | Con = "QCon" ; |
| 783 | break; |
| 784 | case GOFF::RLDReferenceType::RLD_RT_RTypeConstant: |
| 785 | Con = "VCon" ; |
| 786 | break; |
| 787 | default: |
| 788 | Con = "(unknown)" ; |
| 789 | } |
| 790 | dbgs() << "Reloc " << N << ": " << Con |
| 791 | << " Rptr: " << Sym->getExternalName() |
| 792 | << " Pptr: " << PSection->getExternalName() |
| 793 | << " Offset: " << FixupOffset << " Fixed Imm: " << FixedValue |
| 794 | << "\n" ; |
| 795 | }; |
| 796 | (void)DumpReloc; |
| 797 | |
| 798 | // Save relocation data for later writing. |
| 799 | LLVM_DEBUG(DumpReloc("A" , &A)); |
| 800 | Relocations.emplace_back(args&: PSection, args: &A, args&: ReferenceType, args&: ReferentType, |
| 801 | args: GOFF::RLD_ACT_Add, args&: FetchStore, args: FixupOffset, args: Length); |
| 802 | if (B) { |
| 803 | LLVM_DEBUG(DumpReloc("B" , B)); |
| 804 | Relocations.emplace_back( |
| 805 | args&: PSection, args&: B, args&: ReferenceType, args&: ReferentType, args: GOFF::RLD_ACT_Subtract, |
| 806 | args: GOFF::RLDFetchStore::RLD_FS_Fetch, args: FixupOffset, args: Length); |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | uint64_t GOFFObjectWriter::writeObject() { |
| 811 | uint64_t Size = GOFFWriter(OS, *Asm, RootSD, Relocations).writeObject(); |
| 812 | return Size; |
| 813 | } |
| 814 | |
| 815 | std::unique_ptr<MCObjectWriter> |
| 816 | llvm::createGOFFObjectWriter(std::unique_ptr<MCGOFFObjectTargetWriter> MOTW, |
| 817 | raw_pwrite_stream &OS) { |
| 818 | return std::make_unique<GOFFObjectWriter>(args: std::move(MOTW), args&: OS); |
| 819 | } |
| 820 | |