Skip to content

Commit 1f9bfbb

Browse files
committed
[GOFF] Write out relocations in the GOFF writer
Add support for writing relocations. Since the symbol numbering is only available after the symbols are written, the relocations are collected in a vector. At write time, the relocations are converted using the symbols ids, compressed and written out. A relocation data record is limited to 32K-1 bytes, which requires making sure that larger relocation data is written into multiple records.
1 parent ee1434b commit 1f9bfbb

File tree

7 files changed

+378
-15
lines changed

7 files changed

+378
-15
lines changed

llvm/include/llvm/BinaryFormat/GOFF.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,32 @@ enum ESDAlignment : uint8_t {
157157
ESD_ALIGN_4Kpage = 12,
158158
};
159159

160+
enum RLDReferenceType : uint8_t {
161+
RLD_RT_RAddress = 0,
162+
RLD_RT_ROffset = 1,
163+
RLD_RT_RLength = 2,
164+
RLD_RT_RRelativeImmediate = 6,
165+
RLD_RT_RTypeConstant = 7,
166+
RLD_RT_RLongDisplacement = 9,
167+
};
168+
169+
enum RLDReferentType : uint8_t {
170+
RLD_RO_Label = 0,
171+
RLD_RO_Element = 1,
172+
RLD_RO_Class = 2,
173+
RLD_RO_Part = 3,
174+
};
175+
176+
enum RLDAction : uint8_t {
177+
RLD_ACT_Add = 0,
178+
RLD_ACT_Subtract = 1,
179+
};
180+
181+
enum RLDFetchStore : uint8_t {
182+
RLD_FS_Fetch = 0,
183+
RLD_FS_Store = 1
184+
};
185+
160186
enum ENDEntryPointRequest : uint8_t {
161187
END_EPR_None = 0,
162188
END_EPR_EsdidOffset = 1,

llvm/include/llvm/MC/MCGOFFObjectWriter.h

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,75 @@
1111

1212
#include "llvm/MC/MCObjectWriter.h"
1313
#include "llvm/MC/MCValue.h"
14+
#include <memory>
15+
#include <vector>
1416

1517
namespace llvm {
1618
class MCObjectWriter;
19+
class MCSectionGOFF;
20+
class MCSymbolGOFF;
1721
class raw_pwrite_stream;
1822

1923
class MCGOFFObjectTargetWriter : public MCObjectTargetWriter {
2024
protected:
2125
MCGOFFObjectTargetWriter() = default;
2226

2327
public:
28+
enum RLDRelocationType {
29+
Reloc_Type_ACon = 0x1, // General address.
30+
Reloc_Type_RelImm = 0x2, // Relative-immediate address.
31+
Reloc_Type_QCon = 0x3, // Offset of symbol in class.
32+
Reloc_Type_VCon = 0x4, // Address of external symbol.
33+
Reloc_Type_RCon = 0x5, // PSECT of symbol.
34+
};
35+
2436
~MCGOFFObjectTargetWriter() override = default;
2537

38+
virtual unsigned getRelocType(const MCValue &Target, const MCFixup &Fixup,
39+
bool IsPCRel) const = 0;
40+
2641
Triple::ObjectFormatType getFormat() const override { return Triple::GOFF; }
2742

2843
static bool classof(const MCObjectTargetWriter *W) {
2944
return W->getFormat() == Triple::GOFF;
3045
}
3146
};
3247

48+
struct GOFFSavedRelocationEntry {
49+
const MCSectionGOFF *Section;
50+
const MCSymbolGOFF *SymA;
51+
const MCSymbolGOFF *SymB;
52+
unsigned RelocType;
53+
uint64_t FixupOffset;
54+
uint32_t Length;
55+
uint64_t FixedValue; // Info only.
56+
57+
GOFFSavedRelocationEntry(const MCSectionGOFF *Section,
58+
const MCSymbolGOFF *SymA, const MCSymbolGOFF *SymB,
59+
unsigned RelocType, uint64_t FixupOffset,
60+
uint32_t Length, uint64_t FixedValue)
61+
: Section(Section), SymA(SymA), SymB(SymB), RelocType(RelocType),
62+
FixupOffset(FixupOffset), Length(Length), FixedValue(FixedValue) {}
63+
};
64+
3365
class GOFFObjectWriter : public MCObjectWriter {
3466
// The target specific GOFF writer instance.
3567
std::unique_ptr<MCGOFFObjectTargetWriter> TargetObjectWriter;
3668

3769
// The stream used to write the GOFF records.
3870
raw_pwrite_stream &OS;
3971

72+
// Saved relocation data.
73+
std::vector<GOFFSavedRelocationEntry> SavedRelocs;
74+
4075
public:
4176
GOFFObjectWriter(std::unique_ptr<MCGOFFObjectTargetWriter> MOTW,
4277
raw_pwrite_stream &OS);
4378
~GOFFObjectWriter() override;
4479

4580
// Implementation of the MCObjectWriter interface.
4681
void recordRelocation(const MCFragment &F, const MCFixup &Fixup,
47-
MCValue Target, uint64_t &FixedValue) override {}
82+
MCValue Target, uint64_t &FixedValue) override;
4883

4984
uint64_t writeObject() override;
5085
};

0 commit comments

Comments
 (0)