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) {
364 MCSectionGOFF *ED =
Symbol.getADA()->getParent();
367 GOFF::PRAttr{ false, Symbol.getCodeData(),
368 Symbol.getLinkage(), Symbol.getBindingScope(),
372 GOFFSymbol ER(
Symbol.getExternalName(),
Symbol.getIndex(),
374 GOFF::ERAttr{Symbol.isIndirect(), Symbol.getCodeData(),
375 Symbol.getBindingStrength(), Symbol.getLinkage(),
376 GOFF::ESD_AMODE_64, Symbol.getBindingScope()});
381void GOFFWriter::defineSymbols() {
382 unsigned Ordinal = 0;
384 for (MCSection &S : Asm) {
385 auto &
Section =
static_cast<MCSectionGOFF &
>(S);
387 defineSectionSymbols(Section);
391 for (
const MCSymbol &Sym :
Asm.symbols()) {
392 if (Sym.isTemporary())
394 auto &
Symbol =
static_cast<const MCSymbolGOFF &
>(Sym);
395 if (!
Symbol.isDefined()) {
396 Symbol.setIndex(++Ordinal);
397 defineExtern(Symbol);
398 }
else if (
Symbol.isInEDSection()) {
399 Symbol.setIndex(++Ordinal);
408void GOFFWriter::writeHeader() {
411 OS.writebe<uint32_t>(0);
412 OS.writebe<uint32_t>(0);
414 OS.writebe<uint16_t>(0);
417 OS.writebe<uint32_t>(1);
418 OS.writebe<uint16_t>(0);
422void GOFFWriter::writeSymbol(
const GOFFSymbol &Symbol) {
423 if (
Symbol.Offset >= (((uint64_t)1) << 31))
427 SmallString<256>
Name;
434 uint16_t NameLength =
Name.size();
437 OS.writebe<uint8_t>(
Symbol.SymbolType);
438 OS.writebe<uint32_t>(
Symbol.EsdId);
439 OS.writebe<uint32_t>(
Symbol.ParentEsdId);
440 OS.writebe<uint32_t>(0);
441 OS.writebe<uint32_t>(
442 static_cast<uint32_t
>(
Symbol.Offset));
443 OS.writebe<uint32_t>(0);
444 OS.writebe<uint32_t>(
Symbol.SectionLength);
445 OS.writebe<uint32_t>(
Symbol.EASectionEDEsdId);
446 OS.writebe<uint32_t>(
Symbol.EASectionOffset);
447 OS.writebe<uint32_t>(0);
448 OS.writebe<uint8_t>(
Symbol.NameSpace);
449 OS.writebe<uint8_t>(
Symbol.SymbolFlags);
450 OS.writebe<uint8_t>(
Symbol.FillByteValue);
451 OS.writebe<uint8_t>(0);
452 OS.writebe<uint32_t>(
Symbol.ADAEsdId);
453 OS.writebe<uint32_t>(
Symbol.SortKey);
454 OS.writebe<uint64_t>(0);
455 for (
auto F :
Symbol.BehavAttrs.Attr)
456 OS.writebe<uint8_t>(
F);
457 OS.writebe<uint16_t>(NameLength);
458 OS.write(
Name.data(), NameLength);
463class TextStream :
public raw_ostream {
472 char Buffer[BufferSize];
479 const uint32_t EsdId;
485 void write_impl(
const char *Ptr,
size_t Size)
override;
487 uint64_t current_pos()
const override {
return Offset; }
490 explicit TextStream(GOFFOstream &OS, uint32_t EsdId,
492 : OS(OS),
Offset(0), EsdId(EsdId), RecordStyle(RecordStyle) {
493 SetBuffer(Buffer,
sizeof(Buffer));
496 ~TextStream()
override { flush(); }
500void TextStream::write_impl(
const char *Ptr,
size_t Size) {
501 size_t WrittenLength = 0;
504 if (
Offset +
Size > std::numeric_limits<int32_t>::max())
507 while (WrittenLength <
Size) {
508 size_t ToWriteLength =
512 OS.writebe<uint8_t>(GOFF::Flags(4, 4, RecordStyle));
513 OS.writebe<uint32_t>(EsdId);
514 OS.writebe<uint32_t>(0);
515 OS.writebe<uint32_t>(
static_cast<uint32_t
>(
Offset));
516 OS.writebe<uint32_t>(0);
517 OS.writebe<uint16_t>(0);
518 OS.writebe<uint16_t>(ToWriteLength);
519 OS.write(Ptr + WrittenLength, ToWriteLength);
521 WrittenLength += ToWriteLength;
526void GOFFWriter::writeText(
const MCSectionGOFF *Section) {
532 Asm.writeSectionData(S, Section);
537class RelocDataItemBuffer {
542 RelocDataItemBuffer() : Ptr(Buffer) {}
543 const char *
data() {
return Buffer; }
544 size_t size() {
return Ptr - Buffer; }
545 void reset() { Ptr = Buffer; }
547 template <
typename T>
void writebe(
T Val) {
548 assert(fits(
sizeof(
T)) &&
"Out-of-bounds write");
555void GOFFWriter::writeRelocations() {
557 for (
auto &RelocEntry : Relocations) {
558 auto GetRptr = [](
const MCSymbolGOFF *Sym) -> uint32_t {
559 if (Sym->isTemporary())
560 return static_cast<MCSectionGOFF &
>(Sym->getSection())
563 return Sym->getIndex();
566 RelocEntry.PEsdId = RelocEntry.Pptr->getOrdinal();
567 RelocEntry.REsdId = GetRptr(RelocEntry.Rptr);
572 Relocations.begin(), Relocations.end(),
573 [](
const GOFFRelocationEntry &
Left,
const GOFFRelocationEntry &
Right) {
574 return std::tie(Left.PEsdId, Left.REsdId, Left.POffset) <
575 std::tie(Right.PEsdId, Right.REsdId, Right.POffset);
579 RelocDataItemBuffer Buffer;
580 for (
auto I = Relocations.begin(),
E = Relocations.end();
I !=
E;) {
583 uint32_t PrevResdId = -1;
584 uint32_t PrevPesdId = -1;
585 uint64_t PrevPOffset = -1;
586 for (;
I !=
E; ++
I) {
587 const GOFFRelocationEntry &Rel = *
I;
589 bool SameREsdId = (Rel.
REsdId == PrevResdId);
590 bool SamePEsdId = (Rel.
PEsdId == PrevPesdId);
591 bool SamePOffset = (Rel.
POffset == PrevPOffset);
592 bool EightByteOffset = ((Rel.
POffset >> 32) & 0xffffffff);
602 ItemSize += (EightByteOffset ? 8 : 4);
603 if (!Buffer.fits(ItemSize))
606 GOFF::Flags RelocFlags[6];
607 RelocFlags[0].
set(0, 1, SameREsdId);
608 RelocFlags[0].
set(1, 1, SamePEsdId);
609 RelocFlags[0].
set(2, 1, SamePOffset);
610 RelocFlags[0].
set(6, 1, EightByteOffset);
620 for (
auto F : RelocFlags)
621 Buffer.writebe<uint8_t>(
F);
622 Buffer.writebe<uint16_t>(0);
624 Buffer.writebe<uint32_t>(Rel.
REsdId);
626 Buffer.writebe<uint32_t>(Rel.
PEsdId);
629 Buffer.writebe<uint64_t>(Rel.
POffset);
631 Buffer.writebe<uint32_t>(Rel.
POffset);
640 OS.writebe<uint8_t>(0);
641 OS.writebe<uint16_t>(Buffer.size());
642 OS.write(Buffer.data(), Buffer.size());
646void GOFFWriter::writeEnd() {
654 OS.writebe<uint8_t>(GOFF::Flags(6, 2,
F));
655 OS.writebe<uint8_t>(AMODE);
660 OS.writebe<uint32_t>(0);
661 OS.writebe<uint32_t>(ESDID);
664uint64_t GOFFWriter::writeObject() {
669 for (
const MCSection &Section : Asm)
670 writeText(
static_cast<const MCSectionGOFF *
>(&Section));
680 <<
" logical records.");
682 return OS.getWrittenSize();
687 : TargetObjectWriter(
std::
move(MOTW)), OS(OS) {}
695 Asm->getBackend().getFixupKindInfo(
Fixup.getKind());
700 unsigned RelocType = TargetObjectWriter->getRelocType(
Target,
Fixup);
706 if (
A.isUndefined()) {
711 .
concat(
" must be defined for a relative immediate relocation"));
714 if (&
A.getSection() != PSection) {
717 Twine(
"relative immediate relocation section mismatch: ")
728 Twine(
"subtractive symbol ")
730 .
concat(
" not supported for a relative immediate relocation"));
733 FixedValue =
Asm->getSymbolOffset(
A) - FixupOffset +
Target.getConstant();
736 FixedValue =
Target.getConstant();
739 FixedValue +=
A.isTemporary() ?
Asm->getSymbolOffset(
A) : 0;
742 FixedValue -=
B->isTemporary() ?
Asm->getSymbolOffset(*
B) : 0;
756 "No dependent relocations expected");
783 dbgs() <<
"Reloc " <<
N <<
": " << Con
784 <<
" Rptr: " << Sym->getExternalName()
786 <<
" Offset: " << FixupOffset <<
" Fixed Imm: " << FixedValue
793 Relocations.emplace_back(PSection, &
A,
ReferenceType, ReferentType,
797 Relocations.emplace_back(
804 uint64_t Size = GOFFWriter(OS, *
Asm, RootSD, Relocations).writeObject();
808std::unique_ptr<MCObjectWriter>
811 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.