Skip to content

Commit 009082a

Browse files
committed
[MC] Move MCAssembler::DataRegions to MachObjectWriter
and make some cleanup.
1 parent 093aaca commit 009082a

File tree

5 files changed

+38
-70
lines changed

5 files changed

+38
-70
lines changed

llvm/include/llvm/MC/MCAssembler.h

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,6 @@ class MCObjectWriter;
5353
class MCSection;
5454
class MCValue;
5555

56-
// FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk
57-
// to one another.
58-
struct DataRegionData {
59-
// This enum should be kept in sync w/ the mach-o definition in
60-
// llvm/Object/MachOFormat.h.
61-
enum KindTy { Data = 1, JumpTable8, JumpTable16, JumpTable32 } Kind;
62-
MCSymbol *Start;
63-
MCSymbol *End;
64-
};
65-
6656
class MCAssembler {
6757
public:
6858
using SectionListType = std::vector<MCSection *>;
@@ -78,10 +68,6 @@ class MCAssembler {
7868
using symbol_range = iterator_range<symbol_iterator>;
7969
using const_symbol_range = iterator_range<const_symbol_iterator>;
8070

81-
using const_data_region_iterator =
82-
std::vector<DataRegionData>::const_iterator;
83-
using data_region_iterator = std::vector<DataRegionData>::iterator;
84-
8571
/// MachO specific deployment target version info.
8672
// A Major version of 0 indicates that no version information was supplied
8773
// and so the corresponding load command should not be emitted.
@@ -114,8 +100,6 @@ class MCAssembler {
114100

115101
SymbolDataListType Symbols;
116102

117-
std::vector<DataRegionData> DataRegions;
118-
119103
/// The list of linker options to propagate into the object file.
120104
std::vector<std::vector<std::string>> LinkerOptions;
121105

@@ -384,31 +368,6 @@ class MCAssembler {
384368
return LinkerOptions;
385369
}
386370

387-
/// @}
388-
/// \name Data Region List Access
389-
/// @{
390-
391-
// FIXME: This is a total hack, this should not be here. Once things are
392-
// factored so that the streamer has direct access to the .o writer, it can
393-
// disappear.
394-
std::vector<DataRegionData> &getDataRegions() { return DataRegions; }
395-
396-
data_region_iterator data_region_begin() { return DataRegions.begin(); }
397-
const_data_region_iterator data_region_begin() const {
398-
return DataRegions.begin();
399-
}
400-
401-
data_region_iterator data_region_end() { return DataRegions.end(); }
402-
const_data_region_iterator data_region_end() const {
403-
return DataRegions.end();
404-
}
405-
406-
size_t data_region_size() const { return DataRegions.size(); }
407-
408-
/// @}
409-
/// \name Data Region List Access
410-
/// @{
411-
412371
// FIXME: This is a total hack, this should not be here. Once things are
413372
// factored so that the streamer has direct access to the .o writer, it can
414373
// disappear.

llvm/include/llvm/MC/MCMachObjectWriter.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ class MCMachObjectTargetWriter : public MCObjectTargetWriter {
8181
};
8282

8383
class MachObjectWriter : public MCObjectWriter {
84+
public:
85+
struct DataRegionData {
86+
MachO::DataRegionType Kind;
87+
MCSymbol *Start;
88+
MCSymbol *End;
89+
};
90+
91+
private:
8492
/// Helper struct for containing some precomputed information on symbols.
8593
struct MachSymbolData {
8694
const MCSymbol *Symbol;
@@ -113,6 +121,8 @@ class MachObjectWriter : public MCObjectWriter {
113121
std::vector<IndirectSymbolData> IndirectSymbols;
114122
DenseMap<const MCSection *, unsigned> IndirectSymBase;
115123

124+
std::vector<DataRegionData> DataRegions;
125+
116126
SectionAddrMap SectionAddress;
117127

118128
// List of sections in layout order. Virtual sections are after non-virtual
@@ -162,6 +172,7 @@ class MachObjectWriter : public MCObjectWriter {
162172
std::vector<IndirectSymbolData> &getIndirectSymbols() {
163173
return IndirectSymbols;
164174
}
175+
std::vector<DataRegionData> &getDataRegions() { return DataRegions; }
165176
const llvm::SmallVectorImpl<MCSection *> &getSectionOrder() const {
166177
return SectionOrder;
167178
}

llvm/lib/MC/MCAssembler.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ void MCAssembler::reset() {
9797
IncrementalLinkerCompatible = false;
9898
Sections.clear();
9999
Symbols.clear();
100-
DataRegions.clear();
101100
LinkerOptions.clear();
102101
FileNames.clear();
103102
ThumbFuncs.clear();

llvm/lib/MC/MCMachOStreamer.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "llvm/ADT/SmallString.h"
1111
#include "llvm/ADT/SmallVector.h"
1212
#include "llvm/ADT/StringRef.h"
13+
#include "llvm/BinaryFormat/MachO.h"
1314
#include "llvm/MC/MCAsmBackend.h"
1415
#include "llvm/MC/MCAssembler.h"
1516
#include "llvm/MC/MCCodeEmitter.h"
@@ -63,7 +64,7 @@ class MCMachOStreamer : public MCObjectStreamer {
6364

6465
void emitInstToData(const MCInst &Inst, const MCSubtargetInfo &STI) override;
6566

66-
void emitDataRegion(DataRegionData::KindTy Kind);
67+
void emitDataRegion(MachO::DataRegionType Kind);
6768
void emitDataRegionEnd();
6869

6970
public:
@@ -83,6 +84,10 @@ class MCMachOStreamer : public MCObjectStreamer {
8384
MCObjectStreamer::reset();
8485
}
8586

87+
MachObjectWriter &getWriter() {
88+
return static_cast<MachObjectWriter &>(getAssembler().getWriter());
89+
}
90+
8691
/// @name MCStreamer Interface
8792
/// @{
8893

@@ -227,20 +232,18 @@ void MCMachOStreamer::emitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
227232
MCObjectStreamer::emitAssignment(Symbol, Value);
228233
}
229234

230-
void MCMachOStreamer::emitDataRegion(DataRegionData::KindTy Kind) {
235+
void MCMachOStreamer::emitDataRegion(MachO::DataRegionType Kind) {
231236
// Create a temporary label to mark the start of the data region.
232237
MCSymbol *Start = getContext().createTempSymbol();
233238
emitLabel(Start);
234239
// Record the region for the object writer to use.
235-
DataRegionData Data = { Kind, Start, nullptr };
236-
std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
237-
Regions.push_back(Data);
240+
getWriter().getDataRegions().push_back({Kind, Start, nullptr});
238241
}
239242

240243
void MCMachOStreamer::emitDataRegionEnd() {
241-
std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
244+
auto &Regions = getWriter().getDataRegions();
242245
assert(!Regions.empty() && "Mismatched .end_data_region!");
243-
DataRegionData &Data = Regions.back();
246+
auto &Data = Regions.back();
244247
assert(!Data.End && "Mismatched .end_data_region!");
245248
// Create a temporary label to mark the end of the data region.
246249
Data.End = getContext().createTempSymbol();
@@ -269,16 +272,16 @@ void MCMachOStreamer::emitLinkerOptions(ArrayRef<std::string> Options) {
269272
void MCMachOStreamer::emitDataRegion(MCDataRegionType Kind) {
270273
switch (Kind) {
271274
case MCDR_DataRegion:
272-
emitDataRegion(DataRegionData::Data);
275+
emitDataRegion(MachO::DataRegionType::DICE_KIND_DATA);
273276
return;
274277
case MCDR_DataRegionJT8:
275-
emitDataRegion(DataRegionData::JumpTable8);
278+
emitDataRegion(MachO::DataRegionType::DICE_KIND_JUMP_TABLE8);
276279
return;
277280
case MCDR_DataRegionJT16:
278-
emitDataRegion(DataRegionData::JumpTable16);
281+
emitDataRegion(MachO::DataRegionType::DICE_KIND_JUMP_TABLE16);
279282
return;
280283
case MCDR_DataRegionJT32:
281-
emitDataRegion(DataRegionData::JumpTable32);
284+
emitDataRegion(MachO::DataRegionType::DICE_KIND_JUMP_TABLE32);
282285
return;
283286
case MCDR_DataRegionEnd:
284287
emitDataRegionEnd();
@@ -322,9 +325,8 @@ bool MCMachOStreamer::emitSymbolAttribute(MCSymbol *Sym,
322325
if (Attribute == MCSA_IndirectSymbol) {
323326
// Note that we intentionally cannot use the symbol data here; this is
324327
// important for matching the string table that 'as' generates.
325-
static_cast<MachObjectWriter &>(getAssembler().getWriter())
326-
.getIndirectSymbols()
327-
.push_back({Symbol, getCurrentSectionOnly()});
328+
getWriter().getIndirectSymbols().push_back(
329+
{Symbol, getCurrentSectionOnly()});
328330
return true;
329331
}
330332

llvm/lib/MC/MachObjectWriter.cpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ void MachObjectWriter::reset() {
4848
Relocations.clear();
4949
IndirectSymBase.clear();
5050
IndirectSymbols.clear();
51+
DataRegions.clear();
5152
SectionAddress.clear();
5253
SectionOrder.clear();
5354
StringTable.clear();
@@ -828,7 +829,7 @@ uint64_t MachObjectWriter::writeObject(MCAssembler &Asm) {
828829
}
829830

830831
// Add the data-in-code load command size, if used.
831-
unsigned NumDataRegions = Asm.getDataRegions().size();
832+
unsigned NumDataRegions = DataRegions.size();
832833
if (NumDataRegions) {
833834
++NumLoadCommands;
834835
LoadCommandsSize += sizeof(MachO::linkedit_data_command);
@@ -1025,25 +1026,21 @@ uint64_t MachObjectWriter::writeObject(MCAssembler &Asm) {
10251026
}
10261027

10271028
// Write out the data-in-code region payload, if there is one.
1028-
for (MCAssembler::const_data_region_iterator
1029-
it = Asm.data_region_begin(), ie = Asm.data_region_end();
1030-
it != ie; ++it) {
1031-
const DataRegionData *Data = &(*it);
1032-
uint64_t Start = getSymbolAddress(*Data->Start, Asm);
1029+
for (DataRegionData Data : DataRegions) {
1030+
uint64_t Start = getSymbolAddress(*Data.Start, Asm);
10331031
uint64_t End;
1034-
if (Data->End)
1035-
End = getSymbolAddress(*Data->End, Asm);
1032+
if (Data.End)
1033+
End = getSymbolAddress(*Data.End, Asm);
10361034
else
10371035
report_fatal_error("Data region not terminated");
10381036

1039-
LLVM_DEBUG(dbgs() << "data in code region-- kind: " << Data->Kind
1040-
<< " start: " << Start << "(" << Data->Start->getName()
1041-
<< ")"
1042-
<< " end: " << End << "(" << Data->End->getName() << ")"
1043-
<< " size: " << End - Start << "\n");
1037+
LLVM_DEBUG(dbgs() << "data in code region-- kind: " << Data.Kind
1038+
<< " start: " << Start << "(" << Data.Start->getName()
1039+
<< ")" << " end: " << End << "(" << Data.End->getName()
1040+
<< ")" << " size: " << End - Start << "\n");
10441041
W.write<uint32_t>(Start);
10451042
W.write<uint16_t>(End - Start);
1046-
W.write<uint16_t>(Data->Kind);
1043+
W.write<uint16_t>(Data.Kind);
10471044
}
10481045

10491046
// Write out the loh commands, if there is one.

0 commit comments

Comments
 (0)