28#define DEBUG_TYPE "goff-writer"
64 char *BufferPtr = Buffer;
67 char Buffer[BufferSize];
79 uint32_t getNumLogicalRecords() {
return LogicalRecords; }
85 void write_zeros(
unsigned NumZeros);
88 template <
typename value_type>
void writebe(value_type
Value) {
91 write((
const char *)&
Value,
sizeof(value_type));
98 void finalizeRecord();
103 void updateFlagsAndWritePrefix(
bool IsContinued);
106 size_t getRemainingSize();
112GOFFOstream::~GOFFOstream() { finalizeRecord(); }
114void GOFFOstream::updateFlagsAndWritePrefix(
bool IsContinued) {
116 if (TypeAndFlags & RecContinued)
117 TypeAndFlags |= RecContinuation;
119 TypeAndFlags |= RecContinued;
121 TypeAndFlags &= ~RecContinued;
124 <<
static_cast<unsigned char>(TypeAndFlags)
125 <<
static_cast<unsigned char>(0);
130size_t GOFFOstream::getRemainingSize() {
131 return size_t(&Buffer[BufferSize] - BufferPtr);
134void GOFFOstream::write(
const char *Ptr,
size_t Size) {
135 size_t RemainingSize = getRemainingSize();
139 memcpy(BufferPtr, Ptr,
Size);
146 updateFlagsAndWritePrefix(
true);
147 OS.
write(Buffer,
size_t(BufferPtr - Buffer));
148 if (RemainingSize > 0) {
149 OS.
write(Ptr, RemainingSize);
150 Ptr += RemainingSize;
151 Size -= RemainingSize;
154 while (
Size > BufferSize) {
155 updateFlagsAndWritePrefix(
true);
156 OS.
write(Ptr, BufferSize);
162 memcpy(Buffer, Ptr,
Size);
163 BufferPtr = &Buffer[
Size];
166void GOFFOstream::write_zeros(
unsigned NumZeros) {
167 assert(NumZeros <= 16 &&
"Range for zeros too large");
170 size_t RemainingSize = getRemainingSize();
172 memset(BufferPtr, 0, NumZeros);
173 BufferPtr += NumZeros;
178 static char Zeros[16] = {
181 write(Zeros, NumZeros);
186 TypeAndFlags =
Type << 4;
190void GOFFOstream::finalizeRecord() {
191 if (Buffer == BufferPtr)
193 updateFlagsAndWritePrefix(
false);
194 OS.
write(Buffer,
size_t(BufferPtr - Buffer));
205 uint32_t ParentEsdId;
212 GOFF::BehavioralAttributes BehavAttrs;
213 GOFF::SymbolFlags SymbolFlags;
214 uint32_t SortKey = 0;
215 uint32_t SectionLength = 0;
216 uint32_t ADAEsdId = 0;
217 uint32_t EASectionEDEsdId = 0;
218 uint32_t EASectionOffset = 0;
219 uint8_t FillByteValue = 0;
221 GOFFSymbol() : EsdId(0), ParentEsdId(0) {}
223 GOFFSymbol(StringRef Name, uint32_t EsdID,
const GOFF::SDAttr &Attr)
230 GOFFSymbol(StringRef Name, uint32_t EsdID, uint32_t ParentEsdID,
231 const GOFF::EDAttr &Attr)
241 BehavAttrs.setRmode(Attr.
Rmode);
248 GOFFSymbol(StringRef Name, uint32_t EsdID, uint32_t ParentEsdID,
255 BehavAttrs.setLinkageType(Attr.
Linkage);
256 BehavAttrs.setAmode(Attr.
Amode);
260 GOFFSymbol(StringRef Name, uint32_t EsdID, uint32_t ParentEsdID,
261 const GOFF::EDAttr &EDAttr,
const GOFF::PRAttr &Attr)
266 BehavAttrs.setLinkageType(Attr.
Linkage);
268 BehavAttrs.setAlignment(EDAttr.
Alignment);
271 GOFFSymbol(StringRef Name, uint32_t EsdID, uint32_t ParentEsdID,
272 const GOFF::ERAttr &Attr)
278 BehavAttrs.setLinkageType(Attr.
Linkage);
279 BehavAttrs.setAmode(Attr.
Amode);
288 MCSectionGOFF *RootSD;
291 std::vector<GOFFRelocationEntry> &Relocations;
294 void writeSymbol(
const GOFFSymbol &Symbol);
295 void writeText(
const MCSectionGOFF *MC);
296 void writeRelocations();
299 void defineSectionSymbols(
const MCSectionGOFF &Section);
300 void defineLabel(
const MCSymbolGOFF &Symbol);
301 void defineExtern(
const MCSymbolGOFF &Symbol);
302 void defineSymbols();
305 GOFFWriter(raw_pwrite_stream &OS, MCAssembler &Asm, MCSectionGOFF *RootSD,
306 std::vector<GOFFRelocationEntry> &Relocations);
307 uint64_t writeObject();
311GOFFWriter::GOFFWriter(raw_pwrite_stream &OS, MCAssembler &Asm,
312 MCSectionGOFF *RootSD,
313 std::vector<GOFFRelocationEntry> &Relocations)
314 : OS(OS),
Asm(
Asm), RootSD(RootSD), Relocations(Relocations) {}
316void GOFFWriter::defineSectionSymbols(
const MCSectionGOFF &Section) {
331 MCSectionGOFF *Parent =
Section.getParent();
336 if (
Section.requiresNonZeroLength()) {
342 if (!PR.SectionLength)
343 PR.SectionLength = 2;
349void GOFFWriter::defineLabel(
const MCSymbolGOFF &Symbol) {
350 MCSectionGOFF &
Section =
static_cast<MCSectionGOFF &
>(
Symbol.getSection());
353 GOFF::LDAttr{false, Symbol.getCodeData(),
354 Symbol.getBindingStrength(), Symbol.getLinkage(),
355 GOFF::ESD_AMODE_64, Symbol.getBindingScope()});
357 LD.ADAEsdId =
Symbol.getADA()->getOrdinal();
362void GOFFWriter::defineExtern(
const MCSymbolGOFF &Symbol) {
363 GOFFSymbol ER(
Symbol.getExternalName(),
Symbol.getIndex(),
365 GOFF::ERAttr{Symbol.isIndirect(), Symbol.getCodeData(),
366 Symbol.getBindingStrength(), Symbol.getLinkage(),
367 GOFF::ESD_AMODE_64, Symbol.getBindingScope()});
371void GOFFWriter::defineSymbols() {
372 unsigned Ordinal = 0;
374 for (MCSection &S : Asm) {
375 auto &
Section =
static_cast<MCSectionGOFF &
>(S);
377 defineSectionSymbols(Section);
381 for (
const MCSymbol &Sym :
Asm.symbols()) {
382 if (Sym.isTemporary())
384 auto &
Symbol =
static_cast<const MCSymbolGOFF &
>(Sym);
385 if (!
Symbol.isDefined()) {
386 Symbol.setIndex(++Ordinal);
387 defineExtern(Symbol);
388 }
else if (
Symbol.isInEDSection()) {
389 Symbol.setIndex(++Ordinal);
398void GOFFWriter::writeHeader() {
401 OS.writebe<uint32_t>(0);
402 OS.writebe<uint32_t>(0);
404 OS.writebe<uint16_t>(0);
407 OS.writebe<uint32_t>(1);
408 OS.writebe<uint16_t>(0);
412void GOFFWriter::writeSymbol(
const GOFFSymbol &Symbol) {
413 if (
Symbol.Offset >= (((uint64_t)1) << 31))
417 SmallString<256>
Name;
424 uint16_t NameLength =
Name.size();
427 OS.writebe<uint8_t>(
Symbol.SymbolType);
428 OS.writebe<uint32_t>(
Symbol.EsdId);
429 OS.writebe<uint32_t>(
Symbol.ParentEsdId);
430 OS.writebe<uint32_t>(0);
431 OS.writebe<uint32_t>(
432 static_cast<uint32_t
>(
Symbol.Offset));
433 OS.writebe<uint32_t>(0);
434 OS.writebe<uint32_t>(
Symbol.SectionLength);
435 OS.writebe<uint32_t>(
Symbol.EASectionEDEsdId);
436 OS.writebe<uint32_t>(
Symbol.EASectionOffset);
437 OS.writebe<uint32_t>(0);
438 OS.writebe<uint8_t>(
Symbol.NameSpace);
439 OS.writebe<uint8_t>(
Symbol.SymbolFlags);
440 OS.writebe<uint8_t>(
Symbol.FillByteValue);
441 OS.writebe<uint8_t>(0);
442 OS.writebe<uint32_t>(
Symbol.ADAEsdId);
443 OS.writebe<uint32_t>(
Symbol.SortKey);
444 OS.writebe<uint64_t>(0);
445 for (
auto F :
Symbol.BehavAttrs.Attr)
446 OS.writebe<uint8_t>(
F);
447 OS.writebe<uint16_t>(NameLength);
448 OS.write(
Name.data(), NameLength);
453class TextStream :
public raw_ostream {
462 char Buffer[BufferSize];
469 const uint32_t EsdId;
475 void write_impl(
const char *Ptr,
size_t Size)
override;
477 uint64_t current_pos()
const override {
return Offset; }
480 explicit TextStream(GOFFOstream &OS, uint32_t EsdId,
482 : OS(OS),
Offset(0), EsdId(EsdId), RecordStyle(RecordStyle) {
483 SetBuffer(Buffer,
sizeof(Buffer));
486 ~TextStream()
override { flush(); }
490void TextStream::write_impl(
const char *Ptr,
size_t Size) {
491 size_t WrittenLength = 0;
494 if (
Offset +
Size > std::numeric_limits<int32_t>::max())
497 while (WrittenLength <
Size) {
498 size_t ToWriteLength =
502 OS.writebe<uint8_t>(GOFF::Flags(4, 4, RecordStyle));
503 OS.writebe<uint32_t>(EsdId);
504 OS.writebe<uint32_t>(0);
505 OS.writebe<uint32_t>(
static_cast<uint32_t
>(
Offset));
506 OS.writebe<uint32_t>(0);
507 OS.writebe<uint16_t>(0);
508 OS.writebe<uint16_t>(ToWriteLength);
509 OS.write(Ptr + WrittenLength, ToWriteLength);
511 WrittenLength += ToWriteLength;
516void GOFFWriter::writeText(
const MCSectionGOFF *Section) {
522 Asm.writeSectionData(S, Section);
527class RelocDataItemBuffer {
532 RelocDataItemBuffer() : Ptr(Buffer) {}
533 const char *
data() {
return Buffer; }
534 size_t size() {
return Ptr - Buffer; }
535 void reset() { Ptr = Buffer; }
537 template <
typename T>
void writebe(
T Val) {
538 assert(fits(
sizeof(
T)) &&
"Out-of-bounds write");
545void GOFFWriter::writeRelocations() {
547 for (
auto &RelocEntry : Relocations) {
548 auto GetRptr = [](
const MCSymbolGOFF *Sym) -> uint32_t {
549 if (Sym->isTemporary())
550 return static_cast<MCSectionGOFF &
>(Sym->getSection())
553 return Sym->getIndex();
556 RelocEntry.PEsdId = RelocEntry.Pptr->getOrdinal();
557 RelocEntry.REsdId = GetRptr(RelocEntry.Rptr);
562 Relocations.begin(), Relocations.end(),
563 [](
const GOFFRelocationEntry &
Left,
const GOFFRelocationEntry &
Right) {
564 return std::tie(Left.PEsdId, Left.REsdId, Left.POffset) <
565 std::tie(Right.PEsdId, Right.REsdId, Right.POffset);
569 RelocDataItemBuffer Buffer;
570 for (
auto I = Relocations.begin(),
E = Relocations.end();
I !=
E;) {
573 uint32_t PrevResdId = -1;
574 uint32_t PrevPesdId = -1;
575 uint64_t PrevPOffset = -1;
576 for (;
I !=
E; ++
I) {
577 const GOFFRelocationEntry &Rel = *
I;
579 bool SameREsdId = (Rel.
REsdId == PrevResdId);
580 bool SamePEsdId = (Rel.
PEsdId == PrevPesdId);
581 bool SamePOffset = (Rel.
POffset == PrevPOffset);
582 bool EightByteOffset = ((Rel.
POffset >> 32) & 0xffffffff);
592 ItemSize += (EightByteOffset ? 8 : 4);
593 if (!Buffer.fits(ItemSize))
596 GOFF::Flags RelocFlags[6];
597 RelocFlags[0].
set(0, 1, SameREsdId);
598 RelocFlags[0].
set(1, 1, SamePEsdId);
599 RelocFlags[0].
set(2, 1, SamePOffset);
600 RelocFlags[0].
set(6, 1, EightByteOffset);
610 for (
auto F : RelocFlags)
611 Buffer.writebe<uint8_t>(
F);
612 Buffer.writebe<uint16_t>(0);
614 Buffer.writebe<uint32_t>(Rel.
REsdId);
616 Buffer.writebe<uint32_t>(Rel.
PEsdId);
619 Buffer.writebe<uint64_t>(Rel.
POffset);
621 Buffer.writebe<uint32_t>(Rel.
POffset);
630 OS.writebe<uint8_t>(0);
631 OS.writebe<uint16_t>(Buffer.size());
632 OS.write(Buffer.data(), Buffer.size());
636void GOFFWriter::writeEnd() {
644 OS.writebe<uint8_t>(GOFF::Flags(6, 2,
F));
645 OS.writebe<uint8_t>(AMODE);
650 OS.writebe<uint32_t>(0);
651 OS.writebe<uint32_t>(ESDID);
654uint64_t GOFFWriter::writeObject() {
659 for (
const MCSection &Section : Asm)
660 writeText(
static_cast<const MCSectionGOFF *
>(&Section));
670 <<
" logical records.");
672 return OS.getWrittenSize();
677 : TargetObjectWriter(
std::
move(MOTW)), OS(OS) {}
685 Asm->getBackend().getFixupKindInfo(
Fixup.getKind());
690 unsigned RelocType = TargetObjectWriter->getRelocType(
Target,
Fixup);
696 if (
A.isUndefined()) {
701 .
concat(
" must be defined for a relative immediate relocation"));
704 if (&
A.getSection() != PSection) {
707 Twine(
"relative immediate relocation section mismatch: ")
718 Twine(
"subtractive symbol ")
720 .
concat(
" not supported for a relative immediate relocation"));
723 FixedValue =
Asm->getSymbolOffset(
A) - FixupOffset +
Target.getConstant();
726 FixedValue =
Target.getConstant();
729 FixedValue +=
A.isTemporary() ?
Asm->getSymbolOffset(
A) : 0;
732 FixedValue -=
B->isTemporary() ?
Asm->getSymbolOffset(*
B) : 0;
746 "No dependent relocations expected");
773 dbgs() <<
"Reloc " <<
N <<
": " << Con
774 <<
" Rptr: " << Sym->getExternalName()
776 <<
" Offset: " << FixupOffset <<
" Fixed Imm: " << FixedValue
783 Relocations.emplace_back(PSection, &
A,
ReferenceType, ReferentType,
787 Relocations.emplace_back(
794 uint64_t Size = GOFFWriter(OS, *
Asm, RootSD, Relocations).writeObject();
798std::unique_ptr<MCObjectWriter>
801 return std::make_unique<GOFFObjectWriter>(std::move(MOTW), OS);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_LIKELY(EXPR)
This file provides utility functions for converting between EBCDIC-1047 and UTF-8.
This file declares the MCSectionGOFF class, which contains all of the necessary machine code sections...
This file contains the MCSymbolGOFF class.
PowerPC TLS Dynamic Call Fixup
GOFFObjectWriter(std::unique_ptr< MCGOFFObjectTargetWriter > MOTW, raw_pwrite_stream &OS)
uint64_t writeObject() override
Write the object file and returns the number of bytes written.
~GOFFObjectWriter() override
void recordRelocation(const MCFragment &F, const MCFixup &Fixup, MCValue Target, uint64_t &FixedValue) override
Record a relocation entry.
constexpr void set(uint8_t BitIndex, uint8_t Length, T NewValue)
LLVM_ABI bool getSymbolOffset(const MCSymbol &S, uint64_t &Val) const
LLVM_ABI uint64_t getSectionAddressSize(const MCSection &Sec) const
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
GOFF::EDAttr getEDAttributes() const
StringRef getExternalName() const
unsigned getOrdinal() const
Target - Wrapper for Target specific information.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Twine concat(const Twine &Suffix) const
The instances of the Type class are immutable: once they are created, they are never changed.
LLVM Value Representation.
raw_ostream & write_zeros(unsigned NumZeros)
write_zeros - Insert 'NumZeros' nulls.
raw_ostream & write(unsigned char C)
An abstract base class for streams implementations that also support a pwrite operation.
LLVM_ABI std::error_code convertToEBCDIC(StringRef Source, SmallVectorImpl< char > &Result)
constexpr uint8_t PayloadLength
constexpr uint16_t MaxDataLength
Maximum data length before starting a new card for RLD and TXT data.
constexpr uint8_t PTVPrefix
Prefix byte on every record. This indicates GOFF format.
constexpr uint8_t RecordLength
Length of the parts of a physical GOFF record.
@ ESD_NS_ProgramManagementBinder
@ ESD_ST_ElementDefinition
@ ESD_ST_SectionDefinition
@ ESD_ST_ExternalReference
value_type byte_swap(value_type value, endianness endian)
void write(void *memory, value_type value, endianness endian)
Write a value to memory with a particular endianness.
This is an optimization pass for GlobalISel generic memory operations.
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
std::unique_ptr< MCObjectWriter > createGOFFObjectWriter(std::unique_ptr< MCGOFFObjectTargetWriter > MOTW, raw_pwrite_stream &OS)
Construct a new GOFF writer instance.
detail::concat_range< ValueT, RangeTs... > concat(RangeTs &&...Ranges)
Returns a concatenated range across two or more ranges.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
LLVM_ABI Error write(MCStreamer &Out, ArrayRef< std::string > Inputs, OnCuIndexOverflow OverflowOptValue, Dwarf64StrOffsetsPromotion StrOffsetsOptValue)
Implement std::hash so that hash_code can be used in STL containers.
GOFF::RLDReferenceType ReferenceType
GOFF::RLDFetchStore FetchStore
GOFF::RLDReferentType ReferentType
GOFF::ESDReserveQwords ReservedQwords
GOFF::ESDAlignment Alignment
GOFF::ESDTextStyle TextStyle
GOFF::ESDLoadingBehavior LoadBehavior
GOFF::ESDNameSpaceId NameSpace
GOFF::ESDBindingAlgorithm BindAlgorithm
GOFF::ESDBindingScope BindingScope
GOFF::ESDBindingStrength BindingStrength
GOFF::ESDLinkageType Linkage
GOFF::ESDExecutable Executable
GOFF::ESDBindingStrength BindingStrength
GOFF::ESDExecutable Executable
GOFF::ESDBindingScope BindingScope
GOFF::ESDLinkageType Linkage
GOFF::ESDLinkageType Linkage
GOFF::ESDBindingScope BindingScope
GOFF::ESDExecutable Executable
GOFF::ESDTaskingBehavior TaskingBehavior
GOFF::ESDBindingScope BindingScope
Target independent information on a fixup kind.
uint8_t TargetSize
The number of bits written by this fixup.