Skip to content

Commit 7a6bf0c

Browse files
committed
Adding GOFFWriter (sans relocations).
* Got rid of "alignment" field on MCSymbolGOFF * No more checking for 'main'
1 parent a7f4576 commit 7a6bf0c

18 files changed

+1458
-60
lines changed

Diff for: llvm/include/llvm/BinaryFormat/GOFF.h

+30-6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ constexpr uint8_t RecordLength = 80;
2929
constexpr uint8_t RecordPrefixLength = 3;
3030
constexpr uint8_t PayloadLength = 77;
3131
constexpr uint8_t RecordContentLength = RecordLength - RecordPrefixLength;
32+
constexpr uint8_t ESDMetadataLength = 69;
33+
constexpr uint8_t TXTMetadataLength = 21;
3234

3335
/// \brief Maximum data length before starting a new card for RLD and TXT data.
3436
///
@@ -65,12 +67,7 @@ enum ESDNameSpaceId : uint8_t {
6567
ESD_NS_Parts = 3
6668
};
6769

68-
enum ESDReserveQwords : uint8_t {
69-
ESD_RQ_0 = 0,
70-
ESD_RQ_1 = 1,
71-
ESD_RQ_2 = 2,
72-
ESD_RQ_3 = 3
73-
};
70+
enum ESDReserveQwords : uint8_t { ESD_RQ_0 = 0, ESD_RQ_1 = 1 };
7471

7572
enum ESDAmode : uint8_t {
7673
ESD_AMODE_None = 0,
@@ -157,6 +154,12 @@ enum ESDAlignment : uint8_t {
157154
ESD_ALIGN_4Kpage = 12,
158155
};
159156

157+
enum TXTRecordStyle : uint8_t {
158+
TXT_RS_Byte = 0,
159+
TXT_RS_Structured = 1,
160+
TXT_RS_Unstructured = 2,
161+
};
162+
160163
enum ENDEntryPointRequest : uint8_t {
161164
END_EPR_None = 0,
162165
END_EPR_EsdidOffset = 1,
@@ -166,9 +169,30 @@ enum ENDEntryPointRequest : uint8_t {
166169

167170
// \brief Subsections of the primary C_CODE section in the object file.
168171
enum SubsectionKind : uint8_t {
172+
SK_ReadOnly = 1,
169173
SK_PPA1 = 2,
170174
SK_PPA2 = 4,
171175
};
176+
177+
// \brief Type of sections (properly - classes or modules) in the object file.
178+
enum GOFFSectionType : uint8_t {
179+
/// Code - This section belongs to the the Code CSECT.
180+
Code,
181+
182+
/// Static - This section belongs to the Static CSECT.
183+
Static,
184+
185+
/// PPA2Offset - This section contains the offset to the PPA2.
186+
/// Note: This is NOT the PPA2 section itself, which should
187+
/// reside within the Code CSECT.
188+
PPA2Offset,
189+
190+
/// B_IDRL -
191+
B_IDRL,
192+
193+
/// Other - All other sections.
194+
Other,
195+
};
172196
} // end namespace GOFF
173197

174198
} // end namespace llvm

Diff for: llvm/include/llvm/CodeGen/AsmPrinter.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -912,4 +912,4 @@ class AsmPrinter : public MachineFunctionPass {
912912

913913
} // end namespace llvm
914914

915-
#endif // LLVM_CODEGEN_ASMPRINTER_H
915+
#endif // LLVM_CODEGEN_ASMPRINTER_H

Diff for: llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h

+2
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,8 @@ class TargetLoweringObjectFileGOFF : public TargetLoweringObjectFile {
314314
const TargetMachine &TM) const override;
315315
MCSection *getSectionForLSDA(const Function &F, const MCSymbol &FnSym,
316316
const TargetMachine &TM) const override;
317+
MCSymbol *getTargetSymbol(const GlobalValue *GV,
318+
const TargetMachine &TM) const override;
317319
};
318320

319321
} // end namespace llvm

Diff for: llvm/include/llvm/MC/MCContext.h

+30-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "llvm/ADT/StringRef.h"
1717
#include "llvm/ADT/Twine.h"
1818
#include "llvm/BinaryFormat/Dwarf.h"
19+
#include "llvm/BinaryFormat/GOFF.h"
1920
#include "llvm/BinaryFormat/XCOFF.h"
2021
#include "llvm/MC/MCAsmMacro.h"
2122
#include "llvm/MC/MCDwarf.h"
@@ -299,6 +300,24 @@ class MCContext {
299300
}
300301
};
301302

303+
struct GOFFSectionKey {
304+
std::string SectionName;
305+
GOFF::GOFFSectionType SectionType;
306+
bool HasParentSection;
307+
308+
GOFFSectionKey(StringRef SectionName, GOFF::GOFFSectionType SectionType,
309+
bool HasParentSection)
310+
: SectionName(SectionName), SectionType(SectionType),
311+
HasParentSection(HasParentSection) {}
312+
313+
bool operator<(const GOFFSectionKey &Other) const {
314+
if (!HasParentSection && SectionType != GOFF::GOFFSectionType::Other) {
315+
return SectionType < Other.SectionType;
316+
}
317+
return SectionName < Other.SectionName;
318+
}
319+
};
320+
302321
struct WasmSectionKey {
303322
std::string SectionName;
304323
StringRef GroupName;
@@ -352,7 +371,7 @@ class MCContext {
352371
StringMap<MCSectionMachO *> MachOUniquingMap;
353372
std::map<ELFSectionKey, MCSectionELF *> ELFUniquingMap;
354373
std::map<COFFSectionKey, MCSectionCOFF *> COFFUniquingMap;
355-
std::map<std::string, MCSectionGOFF *> GOFFUniquingMap;
374+
std::map<GOFFSectionKey, MCSectionGOFF *> GOFFUniquingMap;
356375
std::map<WasmSectionKey, MCSectionWasm *> WasmUniquingMap;
357376
std::map<XCOFFSectionKey, MCSectionXCOFF *> XCOFFUniquingMap;
358377
StringMap<MCSectionDXContainer *> DXCUniquingMap;
@@ -633,8 +652,16 @@ class MCContext {
633652
unsigned Flags,
634653
unsigned EntrySize);
635654

636-
MCSectionGOFF *getGOFFSection(StringRef Section, SectionKind Kind,
637-
MCSection *Parent, const MCExpr *SubsectionId);
655+
MCSectionGOFF *getGOFFLSDASection(StringRef Section, SectionKind Kind);
656+
657+
MCSectionGOFF *
658+
getGOFFSection(StringRef Section, SectionKind Kind,
659+
MCSection *Parent = nullptr,
660+
const MCExpr *SubsectionId = nullptr,
661+
GOFF::GOFFSectionType SectionType = GOFF::Other,
662+
GOFF::ESDTextStyle TextStyle = GOFF::ESD_TS_ByteOriented,
663+
GOFF::ESDLoadingBehavior LoadBehavior = GOFF::ESD_LB_Initial,
664+
bool isRooted = false);
638665

639666
MCSectionCOFF *getCOFFSection(StringRef Section, unsigned Characteristics,
640667
SectionKind Kind, StringRef COMDATSymName,

Diff for: llvm/include/llvm/MC/MCGOFFStreamer.h

+7-4
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@ class MCGOFFStreamer : public MCObjectStreamer {
2424

2525
~MCGOFFStreamer() override;
2626

27-
bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override {
28-
return false;
29-
}
27+
// state management
28+
void initSections(bool NoExecStack, const MCSubtargetInfo &STI) override;
29+
30+
void switchSection(MCSection *Section,
31+
const MCExpr *Subsection = nullptr) override;
32+
bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override;
3033
void emitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
3134
Align ByteAlignment) override {}
32-
void emitInstToData(const MCInst &Inst, const MCSubtargetInfo &) override {}
35+
void emitInstToData(const MCInst &Inst, const MCSubtargetInfo &) override;
3336
void emitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,
3437
uint64_t Size = 0, Align ByteAlignment = Align(1),
3538
SMLoc Loc = SMLoc()) override {}

Diff for: llvm/include/llvm/MC/MCSectionGOFF.h

+41-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
/// This file declares the MCSectionGOFF class, which contains all of the
1111
/// necessary machine code sections for the GOFF file format.
1212
///
13+
/// GOFF doesn't truly have sections in the way object file formats on Unix
14+
/// such as ELF does, so MCSectionGOFF (more or less) represents a Class in
15+
/// GOFF. A GOFF Class is defined by a tuple of ESD symbols; specifically a SD
16+
/// symbol, an ED symbol, and PR or LD symbols. One of these symbols (PR or ED)
17+
/// must be the owner of a TXT record, which contains the actual contents of
18+
/// this Class.
19+
///
1320
//===----------------------------------------------------------------------===//
1421

1522
#ifndef LLVM_MC_MCSECTIONGOFF_H
@@ -24,14 +31,38 @@ namespace llvm {
2431
class MCExpr;
2532

2633
class MCSectionGOFF final : public MCSection {
27-
private:
2834
MCSection *Parent;
2935
const MCExpr *SubsectionId;
36+
GOFF::GOFFSectionType Type = GOFF::Other;
37+
GOFF::ESDTextStyle TextStyle = GOFF::ESD_TS_ByteOriented;
38+
GOFF::ESDLoadingBehavior LoadBehavior = GOFF::ESD_LB_Initial;
39+
40+
/// IsRooted - true iff the SD symbol used to define the GOFF Class this
41+
/// MCSectionGOFF represents is the "root" SD symbol.
42+
bool IsRooted = false;
3043

3144
friend class MCContext;
3245
MCSectionGOFF(StringRef Name, SectionKind K, MCSection *P, const MCExpr *Sub)
3346
: MCSection(SV_GOFF, Name, K, nullptr), Parent(P), SubsectionId(Sub) {}
3447

48+
MCSectionGOFF(StringRef Name, SectionKind K, MCSection *P, const MCExpr *Sub,
49+
GOFF::ESDTextStyle TextStyle,
50+
GOFF::ESDLoadingBehavior LoadBehavior, bool IsRooted)
51+
: MCSection(SV_GOFF, Name, K, nullptr), Parent(P), SubsectionId(Sub),
52+
TextStyle(TextStyle), LoadBehavior(LoadBehavior), IsRooted(IsRooted) {}
53+
54+
MCSectionGOFF(StringRef Name, SectionKind K, MCSection *P, const MCExpr *Sub,
55+
GOFF::GOFFSectionType Type)
56+
: MCSection(SV_GOFF, Name, K, nullptr), Parent(P), SubsectionId(Sub),
57+
Type(Type) {
58+
if (Type == GOFF::GOFFSectionType::PPA2Offset) {
59+
TextStyle = GOFF::ESD_TS_ByteOriented;
60+
} else if (Type == GOFF::GOFFSectionType::B_IDRL) {
61+
TextStyle = GOFF::ESD_TS_Structured;
62+
LoadBehavior = GOFF::ESD_LB_NoLoad;
63+
}
64+
}
65+
3566
public:
3667
void printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
3768
raw_ostream &OS,
@@ -43,6 +74,15 @@ class MCSectionGOFF final : public MCSection {
4374

4475
bool isVirtualSection() const override { return false; }
4576

77+
bool isCode() const { return Type == GOFF::Code; }
78+
bool isStatic() const { return Type == GOFF::Static; }
79+
bool isPPA2Offset() const { return Type == GOFF::PPA2Offset; }
80+
bool isB_IDRL() const { return Type == GOFF::B_IDRL; }
81+
82+
GOFF::ESDTextStyle getTextStyle() const { return TextStyle; }
83+
GOFF::ESDLoadingBehavior getLoadBehavior() const { return LoadBehavior; }
84+
bool getRooted() const { return IsRooted; }
85+
4686
MCSection *getParent() const { return Parent; }
4787
const MCExpr *getSubsectionId() const { return SubsectionId; }
4888

Diff for: llvm/include/llvm/MC/MCSymbolGOFF.h

+40
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,50 @@
1818
namespace llvm {
1919

2020
class MCSymbolGOFF : public MCSymbol {
21+
enum SymbolFlags : uint16_t {
22+
SF_NoRent = 0x01, // Symbol is no-reentrant.
23+
SF_Alias = 0x02, // Symbol is alias.
24+
SF_Hidden = 0x04, // Symbol is hidden, aka not exported.
25+
SF_Weak = 0x08, // Symbol is weak.
26+
SF_OSLinkage = 0x10, // Symbol uses OS linkage.
27+
};
28+
29+
// Shift value for GOFF::ESDExecutable. 3 possible values. 2 bits.
30+
static constexpr uint8_t GOFF_Executable_Shift = 6;
31+
static constexpr uint8_t GOFF_Executable_Bitmask = 0x03;
32+
2133
public:
2234
MCSymbolGOFF(const StringMapEntry<bool> *Name, bool IsTemporary)
2335
: MCSymbol(SymbolKindGOFF, Name, IsTemporary) {}
2436
static bool classof(const MCSymbol *S) { return S->isGOFF(); }
37+
38+
void setOSLinkage(bool Value = true) const {
39+
modifyFlags(Value ? SF_OSLinkage : 0, SF_OSLinkage);
40+
}
41+
bool isOSLinkage() const { return getFlags() & SF_OSLinkage; }
42+
43+
void setExecutable(GOFF::ESDExecutable Value) const {
44+
modifyFlags(Value << GOFF_Executable_Shift,
45+
GOFF_Executable_Bitmask << GOFF_Executable_Shift);
46+
}
47+
GOFF::ESDExecutable getExecutable() const {
48+
return static_cast<GOFF::ESDExecutable>(
49+
(getFlags() >> GOFF_Executable_Shift) & GOFF_Executable_Bitmask);
50+
}
51+
52+
void setHidden(bool Value = true) {
53+
modifyFlags(Value ? SF_Hidden : 0, SF_Hidden);
54+
}
55+
bool isHidden() const { return getFlags() & SF_Hidden; }
56+
bool isExported() const { return !isHidden(); }
57+
58+
void setWeak(bool Value = true) { modifyFlags(Value ? SF_Weak : 0, SF_Weak); }
59+
bool isWeak() const { return getFlags() & SF_Weak; }
60+
61+
void setAlias(bool Value = true) {
62+
modifyFlags(Value ? SF_Alias : 0, SF_Alias);
63+
}
64+
bool isAlias() const { return getFlags() & SF_Alias; }
2565
};
2666
} // end namespace llvm
2767

Diff for: llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp

+26-4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#include "llvm/MC/MCStreamer.h"
5555
#include "llvm/MC/MCSymbol.h"
5656
#include "llvm/MC/MCSymbolELF.h"
57+
#include "llvm/MC/MCSymbolGOFF.h"
5758
#include "llvm/MC/MCValue.h"
5859
#include "llvm/MC/SectionKind.h"
5960
#include "llvm/ProfileData/InstrProf.h"
@@ -2737,16 +2738,37 @@ MCSection *TargetLoweringObjectFileGOFF::getExplicitSectionGlobal(
27372738
MCSection *TargetLoweringObjectFileGOFF::getSectionForLSDA(
27382739
const Function &F, const MCSymbol &FnSym, const TargetMachine &TM) const {
27392740
std::string Name = ".gcc_exception_table." + F.getName().str();
2740-
return getContext().getGOFFSection(Name, SectionKind::getData(), nullptr,
2741-
nullptr);
2741+
return getContext().getGOFFLSDASection(Name, SectionKind::getData());
27422742
}
27432743

27442744
MCSection *TargetLoweringObjectFileGOFF::SelectSectionForGlobal(
27452745
const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
27462746
auto *Symbol = TM.getSymbol(GO);
2747+
if (Kind.isData())
2748+
return getContext().getGOFFSection(Symbol->getName(),
2749+
SectionKind::getData());
2750+
27472751
if (Kind.isBSS())
2748-
return getContext().getGOFFSection(Symbol->getName(), SectionKind::getBSS(),
2749-
nullptr, nullptr);
2752+
return getContext().getGOFFSection(Symbol->getName(),
2753+
SectionKind::getBSS());
27502754

27512755
return getContext().getObjectFileInfo()->getTextSection();
27522756
}
2757+
2758+
MCSymbol *
2759+
TargetLoweringObjectFileGOFF::getTargetSymbol(const GlobalValue *GV,
2760+
const TargetMachine &TM) const {
2761+
// Set alignment.
2762+
if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
2763+
if (!GVar->hasInitializer() && GVar->getValueType()->isSized()) {
2764+
SmallString<128> NameStr;
2765+
getNameWithPrefix(NameStr, GV, TM);
2766+
MCSymbolGOFF *Sym =
2767+
static_cast<MCSymbolGOFF *>(getContext().getOrCreateSymbol(NameStr));
2768+
auto Alignment = GVar->getParent()->getDataLayout().getPreferredAlign(GVar);
2769+
auto Size = GVar->getParent()->getDataLayout().getTypeSizeInBits(GVar->getValueType());
2770+
Sym->declareCommon(Size, Alignment, true);
2771+
}
2772+
}
2773+
return nullptr;
2774+
}

0 commit comments

Comments
 (0)