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);
287 MCSectionGOFF *RootSD;
290 std::vector<GOFFRelocationEntry> &Relocations;
293 void writeSymbol(
const GOFFSymbol &Symbol);
294 void writeText(
const MCSectionGOFF *MC);
295 void writeRelocations();
298 void defineSectionSymbols(
const MCSectionGOFF &Section);
299 void defineLabel(
const MCSymbolGOFF &Symbol);
300 void defineExtern(
const MCSymbolGOFF &Symbol);
301 void defineSymbols();
304 GOFFWriter(raw_pwrite_stream &OS, MCAssembler &Asm, MCSectionGOFF *RootSD,
305 std::vector<GOFFRelocationEntry> &Relocations);
306 uint64_t writeObject();
310GOFFWriter::GOFFWriter(raw_pwrite_stream &OS, MCAssembler &Asm,
311 MCSectionGOFF *RootSD,
312 std::vector<GOFFRelocationEntry> &Relocations)
313 : OS(OS),
Asm(
Asm), RootSD(RootSD), Relocations(Relocations) {}
315void GOFFWriter::defineSectionSymbols(
const MCSectionGOFF &Section) {
330 MCSectionGOFF *Parent =
Section.getParent();
334 if (
Section.requiresNonZeroLength()) {
340 if (!PR.SectionLength)
341 PR.SectionLength = 2;
347void GOFFWriter::defineLabel(
const MCSymbolGOFF &Symbol) {
348 MCSectionGOFF &
Section =
static_cast<MCSectionGOFF &
>(
Symbol.getSection());
350 Section.getEDAttributes().NameSpace,
351 GOFF::LDAttr{false, Symbol.getCodeData(),
352 Symbol.getBindingStrength(), Symbol.getLinkage(),
353 GOFF::ESD_AMODE_64, Symbol.getBindingScope()});
355 LD.ADAEsdId =
Symbol.getADA()->getOrdinal();
360void GOFFWriter::defineExtern(
const MCSymbolGOFF &Symbol) {
362 GOFF::ERAttr{Symbol.getCodeData(), Symbol.getBindingStrength(),
363 Symbol.getLinkage(), GOFF::ESD_AMODE_64,
364 Symbol.getBindingScope()});
368void GOFFWriter::defineSymbols() {
369 unsigned Ordinal = 0;
371 for (MCSection &S : Asm) {
372 auto &
Section =
static_cast<MCSectionGOFF &
>(S);
374 defineSectionSymbols(Section);
378 for (
const MCSymbol &Sym :
Asm.symbols()) {
379 if (Sym.isTemporary())
381 auto &
Symbol =
static_cast<const MCSymbolGOFF &
>(Sym);
382 if (!
Symbol.isDefined()) {
383 Symbol.setIndex(++Ordinal);
384 defineExtern(Symbol);
385 }
else if (
Symbol.isInEDSection()) {
386 Symbol.setIndex(++Ordinal);
392void GOFFWriter::writeHeader() {
395 OS.writebe<uint32_t>(0);
396 OS.writebe<uint32_t>(0);
398 OS.writebe<uint16_t>(0);
401 OS.writebe<uint32_t>(1);
402 OS.writebe<uint16_t>(0);
406void GOFFWriter::writeSymbol(
const GOFFSymbol &Symbol) {
407 if (
Symbol.Offset >= (((uint64_t)1) << 31))
411 SmallString<256>
Name;
418 uint16_t NameLength =
Name.size();
421 OS.writebe<uint8_t>(
Symbol.SymbolType);
422 OS.writebe<uint32_t>(
Symbol.EsdId);
423 OS.writebe<uint32_t>(
Symbol.ParentEsdId);
424 OS.writebe<uint32_t>(0);
425 OS.writebe<uint32_t>(
426 static_cast<uint32_t
>(
Symbol.Offset));
427 OS.writebe<uint32_t>(0);
428 OS.writebe<uint32_t>(
Symbol.SectionLength);
429 OS.writebe<uint32_t>(
Symbol.EASectionEDEsdId);
430 OS.writebe<uint32_t>(
Symbol.EASectionOffset);
431 OS.writebe<uint32_t>(0);
432 OS.writebe<uint8_t>(
Symbol.NameSpace);
433 OS.writebe<uint8_t>(
Symbol.SymbolFlags);
434 OS.writebe<uint8_t>(
Symbol.FillByteValue);
435 OS.writebe<uint8_t>(0);
436 OS.writebe<uint32_t>(
Symbol.ADAEsdId);
437 OS.writebe<uint32_t>(
Symbol.SortKey);
438 OS.writebe<uint64_t>(0);
439 for (
auto F :
Symbol.BehavAttrs.Attr)
440 OS.writebe<uint8_t>(
F);
441 OS.writebe<uint16_t>(NameLength);
442 OS.write(
Name.data(), NameLength);
447class TextStream :
public raw_ostream {
456 char Buffer[BufferSize];
463 const uint32_t EsdId;
469 void write_impl(
const char *Ptr,
size_t Size)
override;
471 uint64_t current_pos()
const override {
return Offset; }
474 explicit TextStream(GOFFOstream &OS, uint32_t EsdId,
476 : OS(OS),
Offset(0), EsdId(EsdId), RecordStyle(RecordStyle) {
477 SetBuffer(Buffer,
sizeof(Buffer));
480 ~TextStream()
override { flush(); }
484void TextStream::write_impl(
const char *Ptr,
size_t Size) {
485 size_t WrittenLength = 0;
488 if (
Offset +
Size > std::numeric_limits<int32_t>::max())
491 while (WrittenLength <
Size) {
492 size_t ToWriteLength =
496 OS.writebe<uint8_t>(GOFF::Flags(4, 4, RecordStyle));
497 OS.writebe<uint32_t>(EsdId);
498 OS.writebe<uint32_t>(0);
499 OS.writebe<uint32_t>(
static_cast<uint32_t
>(
Offset));
500 OS.writebe<uint32_t>(0);
501 OS.writebe<uint16_t>(0);
502 OS.writebe<uint16_t>(ToWriteLength);
503 OS.write(Ptr + WrittenLength, ToWriteLength);
505 WrittenLength += ToWriteLength;
510void GOFFWriter::writeText(
const MCSectionGOFF *Section) {
516 Asm.writeSectionData(S, Section);
521class RelocDataItemBuffer {
526 RelocDataItemBuffer() : Ptr(Buffer) {}
527 const char *
data() {
return Buffer; }
528 size_t size() {
return Ptr - Buffer; }
529 void reset() { Ptr = Buffer; }
531 template <
typename T>
void writebe(
T Val) {
532 assert(fits(
sizeof(
T)) &&
"Out-of-bounds write");
539void GOFFWriter::writeRelocations() {
541 for (
auto &RelocEntry : Relocations) {
542 auto GetRptr = [](
const MCSymbolGOFF *Sym) -> uint32_t {
543 if (Sym->isTemporary())
544 return static_cast<MCSectionGOFF &
>(Sym->getSection())
547 return Sym->getIndex();
550 RelocEntry.PEsdId = RelocEntry.Pptr->getOrdinal();
551 RelocEntry.REsdId = GetRptr(RelocEntry.Rptr);
556 Relocations.begin(), Relocations.end(),
557 [](
const GOFFRelocationEntry &
Left,
const GOFFRelocationEntry &
Right) {
558 return std::tie(Left.PEsdId, Left.REsdId, Left.POffset) <
559 std::tie(Right.PEsdId, Right.REsdId, Right.POffset);
563 RelocDataItemBuffer Buffer;
564 for (
auto I = Relocations.begin(),
E = Relocations.end();
I !=
E;) {
567 uint32_t PrevResdId = -1;
568 uint32_t PrevPesdId = -1;
569 uint64_t PrevPOffset = -1;
570 for (;
I !=
E; ++
I) {
571 const GOFFRelocationEntry &Rel = *
I;
573 bool SameREsdId = (Rel.
REsdId == PrevResdId);
574 bool SamePEsdId = (Rel.
PEsdId == PrevPesdId);
575 bool SamePOffset = (Rel.
POffset == PrevPOffset);
576 bool EightByteOffset = ((Rel.
POffset >> 32) & 0xffffffff);
586 ItemSize += (EightByteOffset ? 8 : 4);
587 if (!Buffer.fits(ItemSize))
590 GOFF::Flags RelocFlags[6];
591 RelocFlags[0].
set(0, 1, SameREsdId);
592 RelocFlags[0].
set(1, 1, SamePEsdId);
593 RelocFlags[0].
set(2, 1, SamePOffset);
594 RelocFlags[0].
set(6, 1, EightByteOffset);
604 for (
auto F : RelocFlags)
605 Buffer.writebe<uint8_t>(
F);
606 Buffer.writebe<uint16_t>(0);
608 Buffer.writebe<uint32_t>(Rel.
REsdId);
610 Buffer.writebe<uint32_t>(Rel.
PEsdId);
613 Buffer.writebe<uint64_t>(Rel.
POffset);
615 Buffer.writebe<uint32_t>(Rel.
POffset);
624 OS.writebe<uint8_t>(0);
625 OS.writebe<uint16_t>(Buffer.size());
626 OS.write(Buffer.data(), Buffer.size());
630void GOFFWriter::writeEnd() {
638 OS.writebe<uint8_t>(GOFF::Flags(6, 2,
F));
639 OS.writebe<uint8_t>(AMODE);
644 OS.writebe<uint32_t>(0);
645 OS.writebe<uint32_t>(ESDID);
648uint64_t GOFFWriter::writeObject() {
653 for (
const MCSection &Section : Asm)
654 writeText(
static_cast<const MCSectionGOFF *
>(&Section));
664 <<
" logical records.");
666 return OS.getWrittenSize();
671 : TargetObjectWriter(
std::
move(MOTW)), OS(OS) {}
679 Asm->getBackend().getFixupKindInfo(
Fixup.getKind());
684 unsigned RelocType = TargetObjectWriter->getRelocType(
Target,
Fixup);
690 if (
A.isUndefined()) {
695 .
concat(
" must be defined for a relative immediate relocation"));
698 if (&
A.getSection() != PSection) {
700 Twine(
"relative immediate relocation section mismatch: ")
701 .
concat(
A.getSection().getName())
711 Twine(
"subtractive symbol ")
713 .
concat(
" not supported for a relative immediate relocation"));
716 FixedValue =
Asm->getSymbolOffset(
A) - FixupOffset +
Target.getConstant();
719 FixedValue =
Target.getConstant();
722 FixedValue +=
A.isTemporary() ?
Asm->getSymbolOffset(
A) : 0;
725 FixedValue -=
B->isTemporary() ?
Asm->getSymbolOffset(*
B) : 0;
739 B ==
nullptr &&
"No dependent relocations expected");
766 dbgs() <<
"Reloc " <<
N <<
": " << Con <<
" Rptr: " << Sym->getName()
767 <<
" Pptr: " << PSection->
getName() <<
" Offset: " << FixupOffset
768 <<
" Fixed Imm: " << FixedValue <<
"\n";
774 Relocations.emplace_back(PSection, &
A,
ReferenceType, ReferentType,
778 Relocations.emplace_back(
785 uint64_t Size = GOFFWriter(OS, *
Asm, RootSD, Relocations).writeObject();
789std::unique_ptr<MCObjectWriter>
792 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
unsigned getOrdinal() const
StringRef getName() 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.