46 constexpr
unsigned DefaultSectionAlign = 4;
47 constexpr int16_t MaxSectionIndex = INT16_MAX;
65 struct ControlSection {
74 : MCCsect(MCSec), SymbolTableIndex(-1), Address(-1), Size(0) {}
81 using CsectGroup = std::deque<ControlSection>;
83 using CsectGroups = std::deque<CsectGroup *>;
104 const bool IsVirtual;
113 static constexpr int16_t UninitializedIndex =
121 FileOffsetToData = 0;
122 FileOffsetToRelocations = 0;
124 Index = UninitializedIndex;
126 for (
auto *Group : Groups)
132 : Address(0), Size(0), FileOffsetToData(0), FileOffsetToRelocations(0),
133 RelocationCount(0), Flags(Flags), Index(UninitializedIndex),
134 IsVirtual(IsVirtual), Groups(Groups) {
146 std::unique_ptr<MCXCOFFObjectTargetWriter> TargetObjectWriter;
152 CsectGroup UndefinedCsects;
153 CsectGroup ProgramCodeCsects;
154 CsectGroup ReadOnlyCsects;
155 CsectGroup DataCsects;
156 CsectGroup FuncDSCsects;
157 CsectGroup TOCCsects;
158 CsectGroup BSSCsects;
167 std::array<Section *const, 3> Sections{{&Text, &
Data, &BSS}};
171 virtual void reset()
override;
180 static bool nameShouldBeInStringTable(
const StringRef &);
182 void writeSymbolTableEntryForCsectMemberLabel(
const Symbol &,
183 const ControlSection &, int16_t,
185 void writeSymbolTableEntryForControlSection(
const ControlSection &, int16_t,
187 void writeFileHeader();
188 void writeSectionHeaderTable();
200 void assignAddressesAndIndices(
const MCAsmLayout &);
203 needsAuxiliaryHeader()
const {
208 size_t auxiliaryHeaderSize()
const {
209 assert(!needsAuxiliaryHeader() &&
210 "Auxiliary header support not implemented.");
215 XCOFFObjectWriter(std::unique_ptr<MCXCOFFObjectTargetWriter> MOTW,
219 XCOFFObjectWriter::XCOFFObjectWriter(
221 : W(OS, support::
big), TargetObjectWriter(
std::move(MOTW)),
224 CsectGroups{&ProgramCodeCsects, &ReadOnlyCsects}),
226 CsectGroups{&DataCsects, &FuncDSCsects, &TOCCsects}),
228 CsectGroups{&BSSCsects}) {}
230 void XCOFFObjectWriter::reset() {
231 UndefinedCsects.clear();
234 for (
auto *Sec : Sections)
238 SymbolTableEntryCount = 0;
239 SymbolTableOffset = 0;
246 CsectGroup &XCOFFObjectWriter::getCsectGroup(
const MCSectionXCOFF *MCSec) {
250 "Only an initialized csect can contain program code.");
251 return ProgramCodeCsects;
254 "Only an initialized csect can contain read only data.");
255 return ReadOnlyCsects;
268 "Mapping invalid csect. CSECT with bss storage class must be " 273 "Only an initialized csect can contain TOC-base.");
274 assert(TOCCsects.empty() &&
275 "We should have only one TOC-base, and it should be the first csect " 276 "in this CsectGroup.");
280 "Only an initialized csect can contain TC entry.");
281 assert(!TOCCsects.empty() &&
282 "We should at least have a TOC-base in this CsectGroup.");
289 void XCOFFObjectWriter::executePostLayoutBinding(
MCAssembler &
Asm,
291 if (TargetObjectWriter->is64Bit())
299 for (
const auto &S : Asm) {
300 const auto *MCSec = cast<const MCSectionXCOFF>(&S);
302 "Cannot add a csect twice.");
304 "An undefined csect should not get registered.");
311 CsectGroup &Group = getCsectGroup(MCSec);
312 Group.emplace_back(MCSec);
313 WrapperMap[MCSec] = &Group.back();
316 for (
const MCSymbol &S : Asm.symbols()) {
326 UndefinedCsects.emplace_back(ContainingCsect);
332 if (XSym == ContainingCsect->getQualNameSymbol())
335 assert(WrapperMap.
find(ContainingCsect) != WrapperMap.
end() &&
336 "Expected containing csect to exist in map");
339 WrapperMap[ContainingCsect]->Syms.emplace_back(XSym);
343 if (nameShouldBeInStringTable(XSym->
getName()))
348 assignAddressesAndIndices(Layout);
357 void XCOFFObjectWriter::writeSections(
const MCAssembler &Asm,
359 uint32_t CurrentAddressLocation = 0;
360 for (
const auto *
Section : Sections) {
362 if (
Section->Index == Section::UninitializedIndex ||
Section->IsVirtual)
366 "Sections should be written consecutively.");
367 for (
const auto *Group :
Section->Groups) {
368 for (
const auto &Csect : *Group) {
369 if (
uint32_t PaddingSize = Csect.Address - CurrentAddressLocation)
373 CurrentAddressLocation = Csect.Address + Csect.Size;
383 CurrentAddressLocation += PaddingSize;
388 uint64_t XCOFFObjectWriter::writeObject(
MCAssembler &Asm,
396 if (TargetObjectWriter->is64Bit())
399 uint64_t StartOffset = W.
OS.
tell();
402 writeSectionHeaderTable();
403 writeSections(Asm, Layout);
410 return W.
OS.
tell() - StartOffset;
417 void XCOFFObjectWriter::writeSymbolName(
const StringRef &SymbolName) {
418 if (nameShouldBeInStringTable(SymbolName)) {
429 void XCOFFObjectWriter::writeSymbolTableEntryForCsectMemberLabel(
430 const Symbol &SymbolRef,
const ControlSection &CSectionRef,
431 int16_t SectionIndex, uint64_t SymbolOffset) {
433 writeSymbolName(SymbolRef.getName());
434 assert(SymbolOffset <= UINT32_MAX - CSectionRef.Address &&
435 "Symbol address overflows.");
437 W.
write<int16_t>(SectionIndex);
446 W.
write<uint8_t>(SymbolRef.getStorageClass());
459 W.
write<uint8_t>(CSectionRef.MCCsect->getMappingClass());
466 void XCOFFObjectWriter::writeSymbolTableEntryForControlSection(
467 const ControlSection &CSectionRef, int16_t SectionIndex,
470 writeSymbolName(CSectionRef.getName());
474 W.
write<int16_t>(SectionIndex);
495 W.
write<uint8_t>(getEncodedType(CSectionRef.MCCsect));
497 W.
write<uint8_t>(CSectionRef.MCCsect->getMappingClass());
504 void XCOFFObjectWriter::writeFileHeader() {
515 W.
write<int32_t>(SymbolTableEntryCount);
522 void XCOFFObjectWriter::writeSectionHeaderTable() {
523 for (
const auto *Sec : Sections) {
525 if (Sec->Index == Section::UninitializedIndex)
548 W.
write<int32_t>(Sec->Flags);
553 for (
const auto &Csect : UndefinedCsects) {
554 writeSymbolTableEntryForControlSection(
558 for (
const auto *
Section : Sections) {
560 if (
Section->Index == Section::UninitializedIndex)
563 for (
const auto *Group :
Section->Groups) {
567 const int16_t SectionIndex =
Section->Index;
568 for (
const auto &Csect : *Group) {
570 writeSymbolTableEntryForControlSection(
571 Csect, SectionIndex, Csect.MCCsect->getStorageClass());
573 for (
const auto &Sym : Csect.Syms)
574 writeSymbolTableEntryForCsectMemberLabel(
581 void XCOFFObjectWriter::assignAddressesAndIndices(
const MCAsmLayout &Layout) {
587 for (
auto &Csect : UndefinedCsects) {
590 Csect.SymbolTableIndex = SymbolTableIndex;
592 SymbolTableIndex += 2;
600 int32_t SectionIndex = 1;
602 for (
auto *
Section : Sections) {
605 [](
const CsectGroup *Group) { return Group->empty(); });
609 if (SectionIndex > MaxSectionIndex)
611 Section->Index = SectionIndex++;
614 bool SectionAddressSet =
false;
615 for (
auto *Group :
Section->Groups) {
619 for (
auto &Csect : *Group) {
623 Address = Csect.Address + Csect.Size;
624 Csect.SymbolTableIndex = SymbolTableIndex;
626 SymbolTableIndex += 2;
628 for (
auto &Sym : Csect.Syms) {
629 Sym.SymbolTableIndex = SymbolTableIndex;
632 SymbolTableIndex += 2;
636 if (!SectionAddressSet) {
637 Section->Address = Group->front().Address;
638 SectionAddressSet =
true;
644 Address =
alignTo(Address, DefaultSectionAlign);
648 SymbolTableEntryCount = SymbolTableIndex;
653 for (
auto *Sec : Sections) {
654 if (Sec->Index == Section::UninitializedIndex || Sec->IsVirtual)
657 Sec->FileOffsetToData = RawPointer;
658 RawPointer += Sec->Size;
665 if (SymbolTableEntryCount)
666 SymbolTableOffset = RawPointer;
675 unsigned Log2Align =
Log2_32(Align);
679 uint8_t EncodedAlign = Log2Align << 3;
685 std::unique_ptr<MCObjectWriter>
688 return std::make_unique<XCOFFObjectWriter>(std::move(MOTW), OS);
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
This class represents lattice values for constants.
This represents an "assembler immediate".
uint64_t getSectionAddressSize(const MCSection *Sec) const
Get the address space size of the given section, as it effects layout.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
TOC Anchor for TOC Addressability.
raw_ostream & write_zeros(unsigned NumZeros)
write_zeros - Insert 'NumZeros' nulls.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly...
COFF::SymbolStorageClass StorageClass
Defines the object file and target independent interfaces used by the assembler backend to write nati...
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
unsigned getAlignment() const
constexpr char SymbolName[]
Key for Kernel::Metadata::mSymbolName.
Encapsulates the layout of an assembly file at a particular point in time.
static StringRef getName(Value *V)
void write(raw_ostream &OS) const
Utility for building string tables with deduplicated suffixes.
size_t add(CachedHashStringRef S)
Add a string to the builder.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
LLVM_NODISCARD size_t size() const
size - Get the string size.
Common csect definition. For uninitialized storage.
iterator find(const_arg_type_t< KeyT > Val)
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
virtual void reset()
lifetime management
bool getSymbolOffset(const MCSymbol &S, uint64_t &Val) const
Get the offset of the given symbol, as computed in the current layout.
MCSectionXCOFF * getContainingCsect() const
void writeSectionData(raw_ostream &OS, const MCSection *Section, const MCAsmLayout &Layout) const
Emit the section contents to OS.
void finalize()
Analyze the strings and build the final table.
This struct is a compact representation of a valid (non-zero power of two) alignment.
std::unique_ptr< MCObjectWriter > createXCOFFObjectWriter(std::unique_ptr< MCXCOFFObjectTargetWriter > MOTW, raw_pwrite_stream &OS)
XCOFF::SymbolType getCSectType() const
static const X86InstrFMA3Group Groups[]
BSS class (uninitialized static internal)
size_t getOffset(CachedHashStringRef S) const
Get the offest of a string in the string table.
void write(ArrayRef< value_type > Val)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Csect definition for initialized storage.
Adapter to write values to a stream in a particular byte order.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
static void writeSymbolTable(raw_ostream &Out, object::Archive::Kind Kind, bool Deterministic, ArrayRef< MemberData > Members, StringRef StringTable)
XCOFF::StorageMappingClass getMappingClass() const
StringRef getSectionName() const
StringRef getName() const
getName - Get the symbol name.
An abstract base class for streams implementations that also support a pwrite operation.
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool isIncrementalLinkerCompatible() const
XCOFF::StorageClass getStorageClass() const
uint64_t tell() const
tell - Return the current offset with the file.
StringRef - Represent a constant reference to a string, i.e.