llvm.org GIT mirror llvm / df39be6
Add support for subsections to the ELF assembler. Fixes PR8717. Differential Revision: http://llvm-reviews.chandlerc.com/D598 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@179725 91177308-0d34-0410-b5e6-96231b3b80d8 Peter Collingbourne 6 years ago
30 changed file(s) with 289 addition(s) and 119 deletion(s). Raw diff Collapse all Expand all
8181 /// @}
8282
8383 protected:
84 MCFragment(FragmentType _Kind, MCSectionData *_Parent);
84 MCFragment(FragmentType _Kind, MCSectionData *_Parent = 0);
8585
8686 public:
8787 // Only for sentinel.
9191 FragmentType getKind() const { return Kind; }
9292
9393 MCSectionData *getParent() const { return Parent; }
94 void setParent(MCSectionData *Value) { Parent = Value; }
9495
9596 MCSymbolData *getAtom() const { return Atom; }
9697 void setAtom(MCSymbolData *Value) { Atom = Value; }
131132
132133 uint8_t BundlePadding;
133134 public:
134 MCEncodedFragment(MCFragment::FragmentType FType, MCSectionData *SD)
135 MCEncodedFragment(MCFragment::FragmentType FType, MCSectionData *SD = 0)
135136 : MCFragment(FType, SD), BundlePadding(0)
136137 {
137138 }
346347
347348 public:
348349 MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize,
349 unsigned _MaxBytesToEmit, MCSectionData *SD)
350 unsigned _MaxBytesToEmit, MCSectionData *SD = 0)
350351 : MCFragment(FT_Align, SD), Alignment(_Alignment),
351352 Value(_Value),ValueSize(_ValueSize),
352353 MaxBytesToEmit(_MaxBytesToEmit), EmitNops(false) {}
387388
388389 public:
389390 MCFillFragment(int64_t _Value, unsigned _ValueSize, uint64_t _Size,
390 MCSectionData *SD)
391 MCSectionData *SD = 0)
391392 : MCFragment(FT_Fill, SD),
392393 Value(_Value), ValueSize(_ValueSize), Size(_Size) {
393394 assert((!ValueSize || (Size % ValueSize) == 0) &&
420421 int8_t Value;
421422
422423 public:
423 MCOrgFragment(const MCExpr &_Offset, int8_t _Value, MCSectionData *SD)
424 MCOrgFragment(const MCExpr &_Offset, int8_t _Value, MCSectionData *SD = 0)
424425 : MCFragment(FT_Org, SD),
425426 Offset(&_Offset), Value(_Value) {}
426427
449450
450451 SmallString<8> Contents;
451452 public:
452 MCLEBFragment(const MCExpr &Value_, bool IsSigned_, MCSectionData *SD)
453 MCLEBFragment(const MCExpr &Value_, bool IsSigned_, MCSectionData *SD = 0)
453454 : MCFragment(FT_LEB, SD),
454455 Value(&Value_), IsSigned(IsSigned_) { Contents.push_back(0); }
455456
485486
486487 public:
487488 MCDwarfLineAddrFragment(int64_t _LineDelta, const MCExpr &_AddrDelta,
488 MCSectionData *SD)
489 MCSectionData *SD = 0)
489490 : MCFragment(FT_Dwarf, SD),
490491 LineDelta(_LineDelta), AddrDelta(&_AddrDelta) { Contents.push_back(0); }
491492
516517 SmallString<8> Contents;
517518
518519 public:
519 MCDwarfCallFrameFragment(const MCExpr &_AddrDelta, MCSectionData *SD)
520 MCDwarfCallFrameFragment(const MCExpr &_AddrDelta, MCSectionData *SD = 0)
520521 : MCFragment(FT_DwarfFrame, SD),
521522 AddrDelta(&_AddrDelta) { Contents.push_back(0); }
522523
588589 /// it.
589590 unsigned HasInstructions : 1;
590591
592 /// Mapping from subsection number to insertion point for subsection numbers
593 /// below that number.
594 SmallVector, 1> SubsectionFragmentMap;
595
591596 /// @}
592597
593598 public:
630635 size_t size() const { return Fragments.size(); }
631636
632637 bool empty() const { return Fragments.empty(); }
638
639 iterator getSubsectionInsertionPoint(unsigned Subsection);
633640
634641 bool isBundleLocked() const {
635642 return BundleLockState != NotBundleLocked;
4949
5050 virtual void InitSections();
5151 virtual void InitToTextSection();
52 virtual void ChangeSection(const MCSection *Section);
52 virtual void ChangeSection(const MCSection *Section,
53 const MCExpr *Subsection);
5354 virtual void EmitLabel(MCSymbol *Symbol);
5455 virtual void EmitDebugLabel(MCSymbol *Symbol);
5556 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
99 #ifndef LLVM_MC_MCOBJECTSTREAMER_H
1010 #define LLVM_MC_MCOBJECTSTREAMER_H
1111
12 #include "llvm/MC/MCAssembler.h"
1213 #include "llvm/MC/MCStreamer.h"
1314
1415 namespace llvm {
3132 class MCObjectStreamer : public MCStreamer {
3233 MCAssembler *Assembler;
3334 MCSectionData *CurSectionData;
35 MCSectionData::iterator CurInsertionPoint;
3436
3537 virtual void EmitInstToData(const MCInst &Inst) = 0;
3638 virtual void EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame);
5557
5658 MCFragment *getCurrentFragment() const;
5759
60 void insert(MCFragment *F) const {
61 CurSectionData->getFragmentList().insert(CurInsertionPoint, F);
62 F->setParent(CurSectionData);
63 }
64
5865 /// Get a data fragment to write into, creating a new one if the current
5966 /// fragment is not a data fragment.
6067 MCDataFragment *getOrCreateDataFragment() const;
7582 virtual void EmitULEB128Value(const MCExpr *Value);
7683 virtual void EmitSLEB128Value(const MCExpr *Value);
7784 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
78 virtual void ChangeSection(const MCSection *Section);
85 virtual void ChangeSection(const MCSection *Section,
86 const MCExpr *Subsection);
7987 virtual void EmitInstruction(const MCInst &Inst);
8088
8189 /// \brief Emit an instruction to a special fragment, because this instruction
1919
2020 namespace llvm {
2121 class MCAsmInfo;
22 class MCExpr;
2223 class raw_ostream;
2324
2425 /// MCSection - Instances of this class represent a uniqued identifier for a
4748 SectionVariant getVariant() const { return Variant; }
4849
4950 virtual void PrintSwitchToSection(const MCAsmInfo &MAI,
50 raw_ostream &OS) const = 0;
51 raw_ostream &OS,
52 const MCExpr *Subsection) const = 0;
5153
5254 // Convenience routines to get label names for the beginning/end of a
5355 // section.
5959 int getSelection () const { return Selection; }
6060
6161 virtual void PrintSwitchToSection(const MCAsmInfo &MAI,
62 raw_ostream &OS) const;
62 raw_ostream &OS,
63 const MCExpr *Subsection) const;
6364 virtual bool UseCodeAlign() const;
6465 virtual bool isVirtualSection() const;
6566
6969 const MCSymbol *getGroup() const { return Group; }
7070
7171 void PrintSwitchToSection(const MCAsmInfo &MAI,
72 raw_ostream &OS) const;
72 raw_ostream &OS,
73 const MCExpr *Subsection) const;
7374 virtual bool UseCodeAlign() const;
7475 virtual bool isVirtualSection() const;
7576
174174 unsigned &StubSize); // Out.
175175
176176 virtual void PrintSwitchToSection(const MCAsmInfo &MAI,
177 raw_ostream &OS) const;
177 raw_ostream &OS,
178 const MCExpr *Subsection) const;
178179 virtual bool UseCodeAlign() const;
179180 virtual bool isVirtualSection() const;
180181
3636 class raw_ostream;
3737 class formatted_raw_ostream;
3838
39 typedef std::pair MCSectionSubPair;
40
3941 /// MCStreamer - Streaming machine code generation interface. This interface
4042 /// is intended to provide a programatic interface that is very similar to the
4143 /// level that an assembler .s file provides. It has callbacks to emit bytes,
8587
8688 /// SectionStack - This is stack of current and previous section
8789 /// values saved by PushSection.
88 SmallVector
89 const MCSection *>, 4> SectionStack;
90 SmallVector>, 4> SectionStack;
9091
9192 bool AutoInitSections;
9293
173174
174175 /// getCurrentSection - Return the current section that the streamer is
175176 /// emitting code to.
176 const MCSection *getCurrentSection() const {
177 MCSectionSubPair getCurrentSection() const {
177178 if (!SectionStack.empty())
178179 return SectionStack.back().first;
179 return NULL;
180 return MCSectionSubPair();
180181 }
181182
182183 /// getPreviousSection - Return the previous section that the streamer is
183184 /// emitting code to.
184 const MCSection *getPreviousSection() const {
185 MCSectionSubPair getPreviousSection() const {
185186 if (!SectionStack.empty())
186187 return SectionStack.back().second;
187 return NULL;
188 return MCSectionSubPair();
188189 }
189190
190191 /// ChangeSection - Update streamer for a new active section.
191192 ///
192193 /// This is called by PopSection and SwitchSection, if the current
193194 /// section changes.
194 virtual void ChangeSection(const MCSection *) = 0;
195 virtual void ChangeSection(const MCSection *, const MCExpr *) = 0;
195196
196197 /// pushSection - Save the current and previous section on the
197198 /// section stack.
207208 bool PopSection() {
208209 if (SectionStack.size() <= 1)
209210 return false;
210 const MCSection *oldSection = SectionStack.pop_back_val().first;
211 const MCSection *curSection = SectionStack.back().first;
211 MCSectionSubPair oldSection = SectionStack.pop_back_val().first;
212 MCSectionSubPair curSection = SectionStack.back().first;
212213
213214 if (oldSection != curSection)
214 ChangeSection(curSection);
215 ChangeSection(curSection.first, curSection.second);
216 return true;
217 }
218
219 bool SubSection(const MCExpr *Subsection) {
220 if (SectionStack.empty())
221 return false;
222
223 SwitchSection(SectionStack.back().first.first, Subsection);
215224 return true;
216225 }
217226
219228 /// @p Section. This is required to update CurSection.
220229 ///
221230 /// This corresponds to assembler directives like .section, .text, etc.
222 void SwitchSection(const MCSection *Section) {
231 void SwitchSection(const MCSection *Section, const MCExpr *Subsection = 0) {
223232 assert(Section && "Cannot switch to a null section!");
224 const MCSection *curSection = SectionStack.back().first;
233 MCSectionSubPair curSection = SectionStack.back().first;
225234 SectionStack.back().second = curSection;
226 if (Section != curSection) {
227 SectionStack.back().first = Section;
228 ChangeSection(Section);
235 if (MCSectionSubPair(Section, Subsection) != curSection) {
236 SectionStack.back().first = MCSectionSubPair(Section, Subsection);
237 ChangeSection(Section, Subsection);
229238 }
230239 }
231240
232241 /// SwitchSectionNoChange - Set the current section where code is being
233242 /// emitted to @p Section. This is required to update CurSection. This
234243 /// version does not call ChangeSection.
235 void SwitchSectionNoChange(const MCSection *Section) {
244 void SwitchSectionNoChange(const MCSection *Section,
245 const MCExpr *Subsection = 0) {
236246 assert(Section && "Cannot switch to a null section!");
237 const MCSection *curSection = SectionStack.back().first;
247 MCSectionSubPair curSection = SectionStack.back().first;
238248 SectionStack.back().second = curSection;
239 if (Section != curSection)
240 SectionStack.back().first = Section;
249 if (MCSectionSubPair(Section, Subsection) != curSection)
250 SectionStack.back().first = MCSectionSubPair(Section, Subsection);
241251 }
242252
243253 /// Initialize the streamer.
134134
135135 /// getCurrentSection() - Return the current section we are emitting to.
136136 const MCSection *AsmPrinter::getCurrentSection() const {
137 return OutStreamer.getCurrentSection();
137 return OutStreamer.getCurrentSection().first;
138138 }
139139
140140
123123 /// @name MCStreamer Interface
124124 /// @{
125125
126 virtual void ChangeSection(const MCSection *Section);
126 virtual void ChangeSection(const MCSection *Section,
127 const MCExpr *Subsection);
127128
128129 virtual void InitSections() {
129130 InitToTextSection();
327328 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
328329 }
329330
330 void MCAsmStreamer::ChangeSection(const MCSection *Section) {
331 void MCAsmStreamer::ChangeSection(const MCSection *Section,
332 const MCExpr *Subsection) {
331333 assert(Section && "Cannot switch to a null section!");
332 Section->PrintSwitchToSection(MAI, OS);
334 Section->PrintSwitchToSection(MAI, OS, Subsection);
333335 }
334336
335337 void MCAsmStreamer::EmitEHSymAttributes(const MCSymbol *Symbol,
636638
637639
638640 void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
639 assert(getCurrentSection() && "Cannot emit contents before setting section!");
641 assert(getCurrentSection().first &&
642 "Cannot emit contents before setting section!");
640643 if (Data.empty()) return;
641644
642645 if (Data.size() == 1) {
667670
668671 void MCAsmStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
669672 unsigned AddrSpace) {
670 assert(getCurrentSection() && "Cannot emit contents before setting section!");
673 assert(getCurrentSection().first &&
674 "Cannot emit contents before setting section!");
671675 const char *Directive = 0;
672676 switch (Size) {
673677 default: break;
13621366 }
13631367
13641368 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
1365 assert(getCurrentSection() && "Cannot emit contents before setting section!");
1369 assert(getCurrentSection().first &&
1370 "Cannot emit contents before setting section!");
13661371
13671372 // Show the encoding in a comment if we have a code emitter.
13681373 if (Emitter)
213213 MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent)
214214 : Kind(_Kind), Parent(_Parent), Atom(0), Offset(~UINT64_C(0))
215215 {
216 Parent->getFragmentList().push_back(this);
216 if (Parent)
217 Parent->getFragmentList().push_back(this);
217218 }
218219
219220 /* *** */
239240 {
240241 if (A)
241242 A->getSectionList().push_back(this);
243 }
244
245 MCSectionData::iterator
246 MCSectionData::getSubsectionInsertionPoint(unsigned Subsection) {
247 if (Subsection == 0 && SubsectionFragmentMap.empty())
248 return end();
249
250 SmallVectorImpl >::iterator MI =
251 std::lower_bound(SubsectionFragmentMap.begin(), SubsectionFragmentMap.end(),
252 std::make_pair(Subsection, (MCFragment *)0));
253 bool ExactMatch = false;
254 if (MI != SubsectionFragmentMap.end()) {
255 ExactMatch = MI->first == Subsection;
256 if (ExactMatch)
257 ++MI;
258 }
259 iterator IP;
260 if (MI == SubsectionFragmentMap.end())
261 IP = end();
262 else
263 IP = MI->second;
264 if (!ExactMatch && Subsection != 0) {
265 // The GNU as documentation claims that subsections have an alignment of 4,
266 // although this appears not to be the case.
267 MCFragment *F = new MCDataFragment();
268 SubsectionFragmentMap.insert(MI, std::make_pair(Subsection, F));
269 getFragmentList().insert(IP, F);
270 F->setParent(this);
271 }
272 return IP;
242273 }
243274
244275 /* *** */
196196 // actually a DW_LNE_end_sequence.
197197
198198 // Switch to the section to be able to create a symbol at its end.
199 // TODO: keep track of the last subsection so that this symbol appears in the
200 // correct place.
199201 MCOS->SwitchSection(Section);
200202
201203 MCContext &context = MCOS->getContext();
786788 if (Symbol->isTemporary())
787789 return;
788790 MCContext &context = MCOS->getContext();
789 if (context.getGenDwarfSection() != MCOS->getCurrentSection())
791 if (context.getGenDwarfSection() != MCOS->getCurrentSection().first)
790792 return;
791793
792794 // The dwarf label's name does not have the symbol name's leading
108108 llvm_unreachable("invalid assembler flag!");
109109 }
110110
111 void MCELFStreamer::ChangeSection(const MCSection *Section) {
111 void MCELFStreamer::ChangeSection(const MCSection *Section,
112 const MCExpr *Subsection) {
112113 MCSectionData *CurSection = getCurrentSectionData();
113114 if (CurSection && CurSection->isBundleLocked())
114115 report_fatal_error("Unterminated .bundle_lock when changing a section");
115116 const MCSymbol *Grp = static_cast(Section)->getGroup();
116117 if (Grp)
117118 getAssembler().getOrCreateSymbolData(*Grp);
118 this->MCObjectStreamer::ChangeSection(Section);
119 this->MCObjectStreamer::ChangeSection(Section, Subsection);
119120 }
120121
121122 void MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
317318 // entry in the module's symbol table (the first being the null symbol).
318319 void MCELFStreamer::EmitFileDirective(StringRef Filename) {
319320 MCSymbol *Symbol = getAssembler().getContext().GetOrCreateSymbol(Filename);
320 Symbol->setSection(*getCurrentSection());
321 Symbol->setSection(*getCurrentSection().first);
321322 Symbol->setAbsolute();
322323
323324 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
433434 // Optimize memory usage by emitting the instruction to a
434435 // MCCompactEncodedInstFragment when not in a bundle-locked group and
435436 // there are no fixups registered.
436 MCCompactEncodedInstFragment *CEIF = new MCCompactEncodedInstFragment(SD);
437 MCCompactEncodedInstFragment *CEIF = new MCCompactEncodedInstFragment();
438 insert(CEIF);
437439 CEIF->getContents().append(Code.begin(), Code.end());
438440 return;
439441 } else {
440 DF = new MCDataFragment(SD);
442 DF = new MCDataFragment();
443 insert(DF);
441444 if (SD->getBundleLockState() == MCSectionData::BundleLockedAlignToEnd) {
442445 // If this is a new fragment created for a bundle-locked group, and the
443446 // group was marked as "align_to_end", set a flag in the fragment.
121121 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
122122
123123 // isSymbolLinkerVisible uses the section.
124 Symbol->setSection(*getCurrentSection());
124 Symbol->setSection(*getCurrentSection().first);
125125 // We have to create a new fragment if this is an atom defining symbol,
126126 // fragments cannot span atoms.
127127 if (getAssembler().isSymbolLinkerVisible(*Symbol))
128 new MCDataFragment(getCurrentSectionData());
128 insert(new MCDataFragment());
129129
130130 MCObjectStreamer::EmitLabel(Symbol);
131131
2929 virtual void InitSections() {
3030 }
3131
32 virtual void ChangeSection(const MCSection *Section) {
32 virtual void ChangeSection(const MCSection *Section,
33 const MCExpr *Subsection) {
3334 }
3435
3536 virtual void EmitLabel(MCSymbol *Symbol) {
3637 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
37 assert(getCurrentSection() && "Cannot emit before setting section!");
38 Symbol->setSection(*getCurrentSection());
38 assert(getCurrentSection().first &&"Cannot emit before setting section!");
39 Symbol->setSection(*getCurrentSection().first);
3940 }
4041 virtual void EmitDebugLabel(MCSymbol *Symbol) {
4142 EmitLabel(Symbol);
77 //===----------------------------------------------------------------------===//
88
99 #include "llvm/MC/MCObjectStreamer.h"
10 #include "llvm/ADT/STLExtras.h"
1011 #include "llvm/MC/MCAsmBackend.h"
1112 #include "llvm/MC/MCAsmInfo.h"
1213 #include "llvm/MC/MCAssembler.h"
4445 if (Assembler)
4546 Assembler->reset();
4647 CurSectionData = 0;
48 CurInsertionPoint = MCSectionData::iterator();
4749 MCStreamer::reset();
4850 }
4951
5052 MCFragment *MCObjectStreamer::getCurrentFragment() const {
5153 assert(getCurrentSectionData() && "No current section!");
5254
53 if (!getCurrentSectionData()->empty())
54 return &getCurrentSectionData()->getFragmentList().back();
55 if (CurInsertionPoint != getCurrentSectionData()->getFragmentList().begin())
56 return prior(CurInsertionPoint);
5557
5658 return 0;
5759 }
6062 MCDataFragment *F = dyn_cast_or_null(getCurrentFragment());
6163 // When bundling is enabled, we don't want to add data to a fragment that
6264 // already has instructions (see MCELFStreamer::EmitInstToData for details)
63 if (!F || (Assembler->isBundlingEnabled() && F->hasInstructions()))
64 F = new MCDataFragment(getCurrentSectionData());
65 if (!F || (Assembler->isBundlingEnabled() && F->hasInstructions())) {
66 F = new MCDataFragment();
67 insert(F);
68 }
6569 return F;
6670 }
6771
144148 return;
145149 }
146150 Value = ForceExpAbs(Value);
147 new MCLEBFragment(*Value, false, getCurrentSectionData());
151 insert(new MCLEBFragment(*Value, false));
148152 }
149153
150154 void MCObjectStreamer::EmitSLEB128Value(const MCExpr *Value) {
154158 return;
155159 }
156160 Value = ForceExpAbs(Value);
157 new MCLEBFragment(*Value, true, getCurrentSectionData());
161 insert(new MCLEBFragment(*Value, true));
158162 }
159163
160164 void MCObjectStreamer::EmitWeakReference(MCSymbol *Alias,
162166 report_fatal_error("This file format doesn't support weak aliases.");
163167 }
164168
165 void MCObjectStreamer::ChangeSection(const MCSection *Section) {
169 void MCObjectStreamer::ChangeSection(const MCSection *Section,
170 const MCExpr *Subsection) {
166171 assert(Section && "Cannot switch to a null section!");
167172
168173 CurSectionData = &getAssembler().getOrCreateSectionData(*Section);
174
175 int64_t IntSubsection = 0;
176 if (Subsection &&
177 !Subsection->EvaluateAsAbsolute(IntSubsection, getAssembler()))
178 report_fatal_error("Cannot evaluate subsection number");
179 if (IntSubsection < 0 || IntSubsection > 8192)
180 report_fatal_error("Subsection number out of range");
181 CurInsertionPoint =
182 CurSectionData->getSubsectionInsertionPoint(unsigned(IntSubsection));
169183 }
170184
171185 void MCObjectStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
184198
185199 // Now that a machine instruction has been assembled into this section, make
186200 // a line entry for any .loc directive that has been seen.
187 MCLineEntry::Make(this, getCurrentSection());
201 MCLineEntry::Make(this, getCurrentSection().first);
188202
189203 // If this instruction doesn't need relaxation, just emit it as data.
190204 MCAssembler &Assembler = getAssembler();
215229 void MCObjectStreamer::EmitInstToFragment(const MCInst &Inst) {
216230 // Always create a new, separate fragment here, because its size can change
217231 // during relaxation.
218 MCRelaxableFragment *IF =
219 new MCRelaxableFragment(Inst, getCurrentSectionData());
232 MCRelaxableFragment *IF = new MCRelaxableFragment(Inst);
233 insert(IF);
220234
221235 SmallString<128> Code;
222236 raw_svector_ostream VecOS(Code);
257271 return;
258272 }
259273 AddrDelta = ForceExpAbs(AddrDelta);
260 new MCDwarfLineAddrFragment(LineDelta, *AddrDelta, getCurrentSectionData());
274 insert(new MCDwarfLineAddrFragment(LineDelta, *AddrDelta));
261275 }
262276
263277 void MCObjectStreamer::EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
269283 return;
270284 }
271285 AddrDelta = ForceExpAbs(AddrDelta);
272 new MCDwarfCallFrameFragment(*AddrDelta, getCurrentSectionData());
286 insert(new MCDwarfCallFrameFragment(*AddrDelta));
273287 }
274288
275289 void MCObjectStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
283297 unsigned MaxBytesToEmit) {
284298 if (MaxBytesToEmit == 0)
285299 MaxBytesToEmit = ByteAlignment;
286 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
287 getCurrentSectionData());
300 insert(new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit));
288301
289302 // Update the maximum alignment on the current section if necessary.
290303 if (ByteAlignment > getCurrentSectionData()->getAlignment())
301314 unsigned char Value) {
302315 int64_t Res;
303316 if (Offset->EvaluateAsAbsolute(Res, getAssembler())) {
304 new MCOrgFragment(*Offset, Value, getCurrentSectionData());
317 insert(new MCOrgFragment(*Offset, Value));
305318 return false;
306319 }
307320
601601 // If we are generating dwarf for assembly source files save the initial text
602602 // section and generate a .file directive.
603603 if (getContext().getGenDwarfForAssembly()) {
604 getContext().setGenDwarfSection(getStreamer().getCurrentSection());
604 getContext().setGenDwarfSection(getStreamer().getCurrentSection().first);
605605 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
606606 getStreamer().EmitLabel(SectionStartSym);
607607 getContext().setGenDwarfSectionStartSym(SectionStartSym);
666666 }
667667
668668 void AsmParser::checkForValidSection() {
669 if (!ParsingInlineAsm && !getStreamer().getCurrentSection()) {
669 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
670670 TokError("expected section directive before assembly directive");
671671 Out.InitToTextSection();
672672 }
14921492 // section is the initial text section then generate a .loc directive for
14931493 // the instruction.
14941494 if (!HadError && getContext().getGenDwarfForAssembly() &&
1495 getContext().getGenDwarfSection() == getStreamer().getCurrentSection()) {
1495 getContext().getGenDwarfSection() ==
1496 getStreamer().getCurrentSection().first) {
14961497
14971498 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
14981499
24832484
24842485 // Check whether we should use optimal code alignment for this .align
24852486 // directive.
2486 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
2487 bool UseCodeAlign = getStreamer().getCurrentSection().first->UseCodeAlign();
24872488 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
24882489 ValueSize == 1 && UseCodeAlign) {
24892490 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
565565 /// ParseDirectivePrevious:
566566 /// ::= .previous
567567 bool DarwinAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
568 const MCSection *PreviousSection = getStreamer().getPreviousSection();
569 if (PreviousSection == NULL)
568 MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
569 if (PreviousSection.first == NULL)
570570 return TokError(".previous without corresponding .section");
571 getStreamer().SwitchSection(PreviousSection);
571 getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
572572 return false;
573573 }
574574
7575 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".internal");
7676 addDirectiveHandler<
7777 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".hidden");
78 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSubsection>(".subsection");
7879 }
7980
8081 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
146147 bool ParseDirectiveVersion(StringRef, SMLoc);
147148 bool ParseDirectiveWeakref(StringRef, SMLoc);
148149 bool ParseDirectiveSymbolAttribute(StringRef, SMLoc);
150 bool ParseDirectiveSubsection(StringRef, SMLoc);
149151
150152 private:
151153 bool ParseSectionName(StringRef &SectionName);
154 bool ParseSectionArguments(bool IsPush);
152155 };
153156
154157 }
190193
191194 bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
192195 unsigned Flags, SectionKind Kind) {
193 if (getLexer().isNot(AsmToken::EndOfStatement))
194 return TokError("unexpected token in section switching directive");
195 Lex();
196 const MCExpr *Subsection = 0;
197 if (getLexer().isNot(AsmToken::EndOfStatement)) {
198 if (getParser().parseExpression(Subsection))
199 return true;
200 }
196201
197202 getStreamer().SwitchSection(getContext().getELFSection(
198 Section, Type, Flags, Kind));
203 Section, Type, Flags, Kind),
204 Subsection);
199205
200206 return false;
201207 }
315321 bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
316322 getStreamer().PushSection();
317323
318 if (ParseDirectiveSection(s, loc)) {
324 if (ParseSectionArguments(/*IsPush=*/true)) {
319325 getStreamer().PopSection();
320326 return true;
321327 }
331337
332338 // FIXME: This is a work in progress.
333339 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
340 return ParseSectionArguments(/*IsPush=*/false);
341 }
342
343 bool ELFAsmParser::ParseSectionArguments(bool IsPush) {
334344 StringRef SectionName;
335345
336346 if (ParseSectionName(SectionName))
340350 int64_t Size = 0;
341351 StringRef GroupName;
342352 unsigned Flags = 0;
353 const MCExpr *Subsection = 0;
343354
344355 // Set the defaults first.
345356 if (SectionName == ".fini" || SectionName == ".init" ||
351362 if (getLexer().is(AsmToken::Comma)) {
352363 Lex();
353364
365 if (IsPush && getLexer().isNot(AsmToken::String)) {
366 if (getParser().parseExpression(Subsection))
367 return true;
368 if (getLexer().isNot(AsmToken::Comma))
369 goto EndStmt;
370 Lex();
371 }
372
354373 if (getLexer().isNot(AsmToken::String))
355374 return TokError("expected string in directive");
356375
407426 }
408427 }
409428
429 EndStmt:
410430 if (getLexer().isNot(AsmToken::EndOfStatement))
411431 return TokError("unexpected token in directive");
412432
443463 SectionKind Kind = computeSectionKind(Flags);
444464 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
445465 Flags, Kind, Size,
446 GroupName));
466 GroupName),
467 Subsection);
447468 return false;
448469 }
449470
450471 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
451 const MCSection *PreviousSection = getStreamer().getPreviousSection();
452 if (PreviousSection == NULL)
472 MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
473 if (PreviousSection.first == NULL)
453474 return TokError(".previous without corresponding .section");
454 getStreamer().SwitchSection(PreviousSection);
475 getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
455476
456477 return false;
457478 }
612633 return false;
613634 }
614635
636 bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) {
637 const MCExpr *Subsection = 0;
638 if (getLexer().isNot(AsmToken::EndOfStatement)) {
639 if (getParser().parseExpression(Subsection))
640 return true;
641 }
642
643 if (getLexer().isNot(AsmToken::EndOfStatement))
644 return TokError("unexpected token in directive");
645
646 getStreamer().SubSection(Subsection);
647 return false;
648 }
649
615650 namespace llvm {
616651
617652 MCAsmParserExtension *createELFAsmParser() {
117117 void MCPureStreamer::EmitLabel(MCSymbol *Symbol) {
118118 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
119119 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
120 assert(getCurrentSection() && "Cannot emit before setting section!");
121
122 Symbol->setSection(*getCurrentSection());
120 assert(getCurrentSection().first && "Cannot emit before setting section!");
121
122 Symbol->setSection(*getCurrentSection().first);
123123
124124 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
125125
126126 // We have to create a new fragment if this is an atom defining symbol,
127127 // fragments cannot span atoms.
128128 if (getAssembler().isSymbolLinkerVisible(SD.getSymbol()))
129 new MCDataFragment(getCurrentSectionData());
129 insert(new MCDataFragment());
130130
131131 // FIXME: This is wasteful, we don't necessarily need to create a data
132132 // fragment. Instead, we should mark the symbol as pointing into the data
161161 // MCObjectStreamer.
162162 if (MaxBytesToEmit == 0)
163163 MaxBytesToEmit = ByteAlignment;
164 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
165 getCurrentSectionData());
164 insert(new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit));
166165
167166 // Update the maximum alignment on the current section if necessary.
168167 if (ByteAlignment > getCurrentSectionData()->getAlignment())
175174 // MCObjectStreamer.
176175 if (MaxBytesToEmit == 0)
177176 MaxBytesToEmit = ByteAlignment;
178 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
179 getCurrentSectionData());
177 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit);
178 insert(F);
180179 F->setEmitNops(true);
181180
182181 // Update the maximum alignment on the current section if necessary.
186185
187186 bool MCPureStreamer::EmitValueToOffset(const MCExpr *Offset,
188187 unsigned char Value) {
189 new MCOrgFragment(*Offset, Value, getCurrentSectionData());
188 insert(new MCOrgFragment(*Offset, Value));
190189 return false;
191190 }
192191
193192 void MCPureStreamer::EmitInstToFragment(const MCInst &Inst) {
194 MCRelaxableFragment *IF =
195 new MCRelaxableFragment(Inst, getCurrentSectionData());
193 MCRelaxableFragment *IF = new MCRelaxableFragment(Inst);
194 insert(IF);
196195
197196 // Add the fixups and data.
198197 //
2828 }
2929
3030 void MCSectionCOFF::PrintSwitchToSection(const MCAsmInfo &MAI,
31 raw_ostream &OS) const {
31 raw_ostream &OS,
32 const MCExpr *Subsection) const {
3233
3334 // standard sections don't require the '.section'
3435 if (ShouldOmitSectionDirective(SectionName, MAI)) {
99 #include "llvm/MC/MCSectionELF.h"
1010 #include "llvm/MC/MCAsmInfo.h"
1111 #include "llvm/MC/MCContext.h"
12 #include "llvm/MC/MCExpr.h"
1213 #include "llvm/MC/MCSymbol.h"
1314 #include "llvm/Support/ELF.h"
1415 #include "llvm/Support/raw_ostream.h"
3132 }
3233
3334 void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI,
34 raw_ostream &OS) const {
35 raw_ostream &OS,
36 const MCExpr *Subsection) const {
3537
3638 if (ShouldOmitSectionDirective(SectionName, MAI)) {
37 OS << '\t' << getSectionName() << '\n';
39 OS << '\t' << getSectionName();
40 if (Subsection)
41 OS << '\t' << *Subsection;
42 OS << '\n';
3843 return;
3944 }
4045
128133 if (Flags & ELF::SHF_GROUP)
129134 OS << "," << Group->getName() << ",comdat";
130135 OS << '\n';
136
137 if (Subsection)
138 OS << "\t.subsection\t" << *Subsection << '\n';
131139 }
132140
133141 bool MCSectionELF::UseCodeAlign() const {
9090 }
9191
9292 void MCSectionMachO::PrintSwitchToSection(const MCAsmInfo &MAI,
93 raw_ostream &OS) const {
93 raw_ostream &OS,
94 const MCExpr *Subsection) const {
9495 OS << "\t.section\t" << getSegmentName() << ',' << getSectionName();
9596
9697 // Get the section type and attributes.
2323 MCStreamer::MCStreamer(StreamerKind Kind, MCContext &Ctx)
2424 : Kind(Kind), Context(Ctx), EmitEHFrame(true), EmitDebugFrame(false),
2525 CurrentW64UnwindInfo(0), LastSymbol(0), AutoInitSections(false) {
26 const MCSection *section = 0;
27 SectionStack.push_back(std::make_pair(section, section));
26 SectionStack.push_back(std::pair());
2827 }
2928
3029 MCStreamer::~MCStreamer() {
3938 EmitDebugFrame = false;
4039 CurrentW64UnwindInfo = 0;
4140 LastSymbol = 0;
42 const MCSection *section = 0;
4341 SectionStack.clear();
44 SectionStack.push_back(std::make_pair(section, section));
42 SectionStack.push_back(std::pair());
4543 }
4644
4745 const MCExpr *MCStreamer::BuildSymbolDiff(MCContext &Context,
187185
188186 void MCStreamer::EmitLabel(MCSymbol *Symbol) {
189187 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
190 assert(getCurrentSection() && "Cannot emit before setting section!");
191 Symbol->setSection(*getCurrentSection());
188 assert(getCurrentSection().first && "Cannot emit before setting section!");
189 Symbol->setSection(*getCurrentSection().first);
192190 LastSymbol = Symbol;
193191 }
194192
195193 void MCStreamer::EmitDebugLabel(MCSymbol *Symbol) {
196194 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
197 assert(getCurrentSection() && "Cannot emit before setting section!");
198 Symbol->setSection(*getCurrentSection());
195 assert(getCurrentSection().first && "Cannot emit before setting section!");
196 Symbol->setSection(*getCurrentSection().first);
199197 LastSymbol = Symbol;
200198 }
201199
6262
6363 ~AArch64ELFStreamer() {}
6464
65 virtual void ChangeSection(const MCSection *Section) {
65 virtual void ChangeSection(const MCSection *Section,
66 const MCExpr *Subsection) {
6667 // We have to keep track of the mapping symbol state of any sections we
6768 // use. Each one should start off as EMS_None, which is provided as the
6869 // default constructor by DenseMap::lookup.
69 LastMappingSymbols[getPreviousSection()] = LastEMS;
70 LastMappingSymbols[getPreviousSection().first] = LastEMS;
7071 LastEMS = LastMappingSymbols.lookup(Section);
7172
72 MCELFStreamer::ChangeSection(Section);
73 MCELFStreamer::ChangeSection(Section, Subsection);
7374 }
7475
7576 /// This function is the one used to emit instruction data into the ELF
128129 MCELF::SetType(SD, ELF::STT_NOTYPE);
129130 MCELF::SetBinding(SD, ELF::STB_LOCAL);
130131 SD.setExternal(false);
131 Symbol->setSection(*getCurrentSection());
132 Symbol->setSection(*getCurrentSection().first);
132133
133134 const MCExpr *Value = MCSymbolRefExpr::Create(Start, getContext());
134135 Symbol->setVariableValue(Value);
8282 virtual void EmitRegSave(const SmallVectorImpl &RegList,
8383 bool isVector);
8484
85 virtual void ChangeSection(const MCSection *Section) {
85 virtual void ChangeSection(const MCSection *Section,
86 const MCExpr *Subsection) {
8687 // We have to keep track of the mapping symbol state of any sections we
8788 // use. Each one should start off as EMS_None, which is provided as the
8889 // default constructor by DenseMap::lookup.
89 LastMappingSymbols[getPreviousSection()] = LastEMS;
90 LastMappingSymbols[getPreviousSection().first] = LastEMS;
9091 LastEMS = LastMappingSymbols.lookup(Section);
9192
92 MCELFStreamer::ChangeSection(Section);
93 MCELFStreamer::ChangeSection(Section, Subsection);
9394 }
9495
9596 /// This function is the one used to emit instruction data into the ELF
182183 MCELF::SetType(SD, ELF::STT_NOTYPE);
183184 MCELF::SetBinding(SD, ELF::STB_LOCAL);
184185 SD.setExternal(false);
185 Symbol->setSection(*getCurrentSection());
186 Symbol->setSection(*getCurrentSection().first);
186187
187188 const MCExpr *Value = MCSymbolRefExpr::Create(Start, getContext());
188189 Symbol->setVariableValue(Value);
3131 /// Override this as NVPTX has its own way of printing switching
3232 /// to a section.
3333 virtual void PrintSwitchToSection(const MCAsmInfo &MAI,
34 raw_ostream &OS) const {}
34 raw_ostream &OS,
35 const MCExpr *Subsection) const {}
3536
3637 /// Base address of PTX sections is zero.
3738 virtual bool isBaseAddressKnownZero() const { return true; }
720720 return AsmPrinter::EmitFunctionEntryLabel();
721721
722722 // Emit an official procedure descriptor.
723 const MCSection *Current = OutStreamer.getCurrentSection();
723 MCSectionSubPair Current = OutStreamer.getCurrentSection();
724724 const MCSectionELF *Section = OutStreamer.getContext().getELFSection(".opd",
725725 ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC,
726726 SectionKind::getReadOnly());
740740 8/*size*/);
741741 // Emit a null environment pointer.
742742 OutStreamer.EmitIntValue(0, 8 /* size */);
743 OutStreamer.SwitchSection(Current);
743 OutStreamer.SwitchSection(Current.first, Current.second);
744744
745745 MCSymbol *RealFnSym = OutContext.GetOrCreateSymbol(
746746 ".L." + Twine(CurrentFnSym->getName()));
0 // RUN: llvm-mc -filetype=obj %s -o - -triple x86_64-pc-linux | llvm-objdump -s - | FileCheck %s
1
2 // CHECK: Contents of section .text:
3 // CHECK-NEXT: 0000 03042502 00000003 04250100 0000ebf7
4 .text 1
5 add 1, %eax
6 jmp label
7 .subsection
8 add 2, %eax
9 label:
10
11 // CHECK-NOT: Contents of section .rela.text:
12
13 // CHECK: Contents of section .data:
14 // CHECK-NEXT: 0000 01030402 74657374
15 .data
16 l0:
17 .byte 1
18 .subsection 1+1
19 l1:
20 .byte 2
21 l2:
22 .subsection l2-l1
23 .byte l1-l0
24 .subsection 3
25 .ascii "test"
26 .previous
27 .byte 4
28
29 // CHECK: Contents of section test:
30 // CHECK-NEXT: 0000 010302
31 .section test
32 .byte 1
33 .pushsection test, 1
34 .byte 2
35 .popsection
36 .byte 3
742742 AddValueSymbols(Inst.getOperand(i).getExpr());
743743 }
744744 virtual void EmitLabel(MCSymbol *Symbol) {
745 Symbol->setSection(*getCurrentSection());
745 Symbol->setSection(*getCurrentSection().first);
746746 markDefined(*Symbol);
747747 }
748748 virtual void EmitDebugLabel(MCSymbol *Symbol) {
770770 virtual void EmitBundleUnlock() {}
771771
772772 // Noop calls.
773 virtual void ChangeSection(const MCSection *Section) {}
773 virtual void ChangeSection(const MCSection *Section,
774 const MCExpr *Subsection) {}
774775 virtual void InitToTextSection() {}
775776 virtual void InitSections() {}
776777 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {}