llvm.org GIT mirror llvm / 90ce9f7
Simplify handling of --noexecstack by using getNonexecutableStackSection. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@219799 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 6 years ago
31 changed file(s) with 81 addition(s) and 127 deletion(s). Raw diff Collapse all Expand all
914914 unsigned BundleAlignSize;
915915
916916 unsigned RelaxAll : 1;
917 unsigned NoExecStack : 1;
918917 unsigned SubsectionsViaSymbols : 1;
919918
920919 /// ELF specific e_header flags
10601059 bool getRelaxAll() const { return RelaxAll; }
10611060 void setRelaxAll(bool Value) { RelaxAll = Value; }
10621061
1063 bool getNoExecStack() const { return NoExecStack; }
1064 void setNoExecStack(bool Value) { NoExecStack = Value; }
1065
10661062 bool isBundlingEnabled() const {
10671063 return BundleAlignSize != 0;
10681064 }
5151 /// @name MCStreamer Interface
5252 /// @{
5353
54 void InitSections() override;
54 void InitSections(bool NoExecStack) override;
5555 void ChangeSection(const MCSection *Section,
5656 const MCExpr *Subsection) override;
5757 void EmitLabel(MCSymbol *Symbol) override;
114114
115115 MCELFStreamer *createARMELFStreamer(MCContext &Context, MCAsmBackend &TAB,
116116 raw_ostream &OS, MCCodeEmitter *Emitter,
117 bool RelaxAll, bool NoExecStack,
118 bool IsThumb);
117 bool RelaxAll, bool IsThumb);
119118
120119 } // end namespace llvm
121120
367367 }
368368
369369 /// Create the default sections and set the initial one.
370 virtual void InitSections();
370 virtual void InitSections(bool NoExecStack);
371371
372372 /// AssignSection - Sets the symbol's section.
373373 ///
765765 /// createELFStreamer - Create a machine code streamer which will generate
766766 /// ELF format object files.
767767 MCStreamer *createELFStreamer(MCContext &Ctx, MCAsmBackend &TAB,
768 raw_ostream &OS, MCCodeEmitter *CE, bool RelaxAll,
769 bool NoExecStack);
768 raw_ostream &OS, MCCodeEmitter *CE,
769 bool RelaxAll);
770770
771771 } // end namespace llvm
772772
3838 /// \name MCStreamer interface
3939 /// \{
4040
41 void InitSections() override;
41 void InitSections(bool NoExecStack) override;
4242 void EmitLabel(MCSymbol *Symbol) override;
4343 void EmitAssemblerFlag(MCAssemblerFlag Flag) override;
4444 void EmitThumbFunc(MCSymbol *Func) override;
122122 const MCRegisterInfo &MRI,
123123 const MCSubtargetInfo &STI,
124124 MCContext &Ctx);
125 typedef MCStreamer *(*MCObjectStreamerCtorTy)(const Target &T,
126 StringRef TT,
127 MCContext &Ctx,
128 MCAsmBackend &TAB,
129 raw_ostream &_OS,
130 MCCodeEmitter *_Emitter,
131 const MCSubtargetInfo &STI,
132 bool RelaxAll,
133 bool NoExecStack);
125 typedef MCStreamer *(*MCObjectStreamerCtorTy)(
126 const Target &T, StringRef TT, MCContext &Ctx, MCAsmBackend &TAB,
127 raw_ostream &_OS, MCCodeEmitter *_Emitter, const MCSubtargetInfo &STI,
128 bool RelaxAll);
134129 typedef MCStreamer *(*AsmStreamerCtorTy)(MCContext &Ctx,
135130 formatted_raw_ostream &OS,
136131 bool isVerboseAsm,
422417 /// \param _OS The stream object.
423418 /// \param _Emitter The target independent assembler object.Takes ownership.
424419 /// \param RelaxAll Relax all fixups?
425 /// \param NoExecStack Mark file as not needing a executable stack.
426420 MCStreamer *createMCObjectStreamer(StringRef TT, MCContext &Ctx,
427 MCAsmBackend &TAB,
428 raw_ostream &_OS,
421 MCAsmBackend &TAB, raw_ostream &_OS,
429422 MCCodeEmitter *_Emitter,
430423 const MCSubtargetInfo &STI,
431 bool RelaxAll,
432 bool NoExecStack) const {
424 bool RelaxAll) const {
433425 if (!MCObjectStreamerCtorFn)
434426 return nullptr;
435427 return MCObjectStreamerCtorFn(*this, TT, Ctx, TAB, _OS, _Emitter, STI,
436 RelaxAll, NoExecStack);
428 RelaxAll);
437429 }
438430
439431 /// createAsmStreamer - Create a target specific MCStreamer.
172172 const_cast(getObjFileLowering())
173173 .Initialize(OutContext, TM);
174174
175 OutStreamer.InitSections();
175 OutStreamer.InitSections(false);
176176
177177 Mang = new Mangler(TM.getSubtargetImpl()->getDataLayout());
178178
200200 if (!MCE || !MAB)
201201 return true;
202202
203 AsmStreamer.reset(getTarget().createMCObjectStreamer(
204 getTargetTriple(), *Context, *MAB, Out, MCE, STI,
205 Options.MCOptions.MCRelaxAll, Options.MCOptions.MCNoExecStack));
203 AsmStreamer.reset(
204 getTarget()
205 .createMCObjectStreamer(getTargetTriple(), *Context, *MAB, Out, MCE,
206 STI, Options.MCOptions.MCRelaxAll));
206207 break;
207208 }
208209 case CGFT_Null:
254255 return true;
255256
256257 std::unique_ptr AsmStreamer;
257 AsmStreamer.reset(getTarget().createMCObjectStreamer(
258 getTargetTriple(), *Ctx, *MAB, Out, MCE, STI,
259 Options.MCOptions.MCRelaxAll, Options.MCOptions.MCNoExecStack));
258 AsmStreamer.reset(getTarget()
259 .createMCObjectStreamer(getTargetTriple(), *Ctx, *MAB,
260 Out, MCE, STI,
261 Options.MCOptions.MCRelaxAll));
260262
261263 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
262264 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
14571457 RevGroupMapTy &RevGroupMap,
14581458 SectionIndexMapTy &SectionIndexMap,
14591459 const RelMapTy &RelMap) {
1460 // Create the .note.GNU-stack section if needed.
14611460 MCContext &Ctx = Asm.getContext();
1462 if (Asm.getNoExecStack()) {
1463 const MCSectionELF *GnuStackSection =
1464 Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0,
1465 SectionKind::getReadOnly());
1466 Asm.getOrCreateSectionData(*GnuStackSection);
1467 }
14681461
14691462 // Build the groups
14701463 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
344344 MCAssembler::MCAssembler(MCContext &Context_, MCAsmBackend &Backend_,
345345 MCCodeEmitter &Emitter_, MCObjectWriter &Writer_,
346346 raw_ostream &OS_)
347 : Context(Context_), Backend(Backend_), Emitter(Emitter_), Writer(Writer_),
348 OS(OS_), BundleAlignSize(0), RelaxAll(false), NoExecStack(false),
349 SubsectionsViaSymbols(false), ELFHeaderEFlags(0) {
347 : Context(Context_), Backend(Backend_), Emitter(Emitter_), Writer(Writer_),
348 OS(OS_), BundleAlignSize(0), RelaxAll(false),
349 SubsectionsViaSymbols(false), ELFHeaderEFlags(0) {
350350 VersionMinInfo.Major = 0; // Major version == 0 for "none specified"
351351 }
352352
365365 ThumbFuncs.clear();
366366 BundleAlignSize = 0;
367367 RelaxAll = false;
368 NoExecStack = false;
369368 SubsectionsViaSymbols = false;
370369 ELFHeaderEFlags = 0;
371370 LOHContainer.reset();
1414 #include "llvm/ADT/STLExtras.h"
1515 #include "llvm/ADT/SmallPtrSet.h"
1616 #include "llvm/MC/MCAsmBackend.h"
17 #include "llvm/MC/MCAsmInfo.h"
1718 #include "llvm/MC/MCAssembler.h"
1819 #include "llvm/MC/MCCodeEmitter.h"
1920 #include "llvm/MC/MCContext.h"
3738 MCELFStreamer::~MCELFStreamer() {
3839 }
3940
40 void MCELFStreamer::InitSections() {
41 void MCELFStreamer::InitSections(bool NoExecStack) {
4142 // This emulates the same behavior of GNU as. This makes it easier
4243 // to compare the output as the major sections are in the same order.
43 SwitchSection(getContext().getObjectFileInfo()->getTextSection());
44 MCContext &Ctx = getContext();
45 SwitchSection(Ctx.getObjectFileInfo()->getTextSection());
4446 EmitCodeAlignment(4);
4547
46 SwitchSection(getContext().getObjectFileInfo()->getDataSection());
48 SwitchSection(Ctx.getObjectFileInfo()->getDataSection());
4749 EmitCodeAlignment(4);
4850
49 SwitchSection(getContext().getObjectFileInfo()->getBSSSection());
51 SwitchSection(Ctx.getObjectFileInfo()->getBSSSection());
5052 EmitCodeAlignment(4);
5153
52 SwitchSection(getContext().getObjectFileInfo()->getTextSection());
54 SwitchSection(Ctx.getObjectFileInfo()->getTextSection());
55
56 if (NoExecStack)
57 SwitchSection(Ctx.getAsmInfo()->getNonexecutableStackSection(Ctx));
5358 }
5459
5560 void MCELFStreamer::EmitLabel(MCSymbol *Symbol) {
542547
543548 MCStreamer *llvm::createELFStreamer(MCContext &Context, MCAsmBackend &MAB,
544549 raw_ostream &OS, MCCodeEmitter *CE,
545 bool RelaxAll, bool NoExecStack) {
550 bool RelaxAll) {
546551 MCELFStreamer *S = new MCELFStreamer(Context, MAB, OS, CE);
547552 if (RelaxAll)
548553 S->getAssembler().setRelaxAll(true);
549 if (NoExecStack)
550 S->getAssembler().setNoExecStack(true);
551554 return S;
552555 }
553556
606606 bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
607607 // Create the initial section, if requested.
608608 if (!NoInitialTextSection)
609 Out.InitSections();
609 Out.InitSections(false);
610610
611611 // Prime the lexer.
612612 Lex();
689689 void AsmParser::checkForValidSection() {
690690 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
691691 TokError("expected section directive before assembly directive");
692 Out.InitSections();
692 Out.InitSections(false);
693693 }
694694 }
695695
181181 MCSymbol *EHSymbol) {
182182 }
183183
184 void MCStreamer::InitSections() {
184 void MCStreamer::InitSections(bool NoExecStack) {
185185 SwitchSection(getContext().getObjectFileInfo()->getTextSection());
186186 }
187187
6060 DF->getContents().append(Code.begin(), Code.end());
6161 }
6262
63 void MCWinCOFFStreamer::InitSections() {
63 void MCWinCOFFStreamer::InitSections(bool NoExecStack) {
6464 // FIXME: this is identical to the ELF one.
6565 // This emulates the same behavior of GNU as. This makes it easier
6666 // to compare the output as the major sections are in the same order.
148148 namespace llvm {
149149 MCELFStreamer *createAArch64ELFStreamer(MCContext &Context, MCAsmBackend &TAB,
150150 raw_ostream &OS, MCCodeEmitter *Emitter,
151 bool RelaxAll, bool NoExecStack) {
151 bool RelaxAll) {
152152 AArch64ELFStreamer *S = new AArch64ELFStreamer(Context, TAB, OS, Emitter);
153153 if (RelaxAll)
154154 S->getAssembler().setRelaxAll(true);
155 if (NoExecStack)
156 S->getAssembler().setNoExecStack(true);
157155 return S;
158156 }
159157 }
1919
2020 MCELFStreamer *createAArch64ELFStreamer(MCContext &Context, MCAsmBackend &TAB,
2121 raw_ostream &OS, MCCodeEmitter *Emitter,
22 bool RelaxAll, bool NoExecStack);
22 bool RelaxAll);
2323 }
2424
2525 #endif
125125 static MCStreamer *createMCStreamer(const Target &T, StringRef TT,
126126 MCContext &Ctx, MCAsmBackend &TAB,
127127 raw_ostream &OS, MCCodeEmitter *Emitter,
128 const MCSubtargetInfo &STI, bool RelaxAll,
129 bool NoExecStack) {
128 const MCSubtargetInfo &STI, bool RelaxAll) {
130129 Triple TheTriple(TT);
131130
132131 if (TheTriple.isOSDarwin())
133132 return createMachOStreamer(Ctx, TAB, OS, Emitter, RelaxAll,
134133 /*LabelSections*/ true);
135134
136 return createAArch64ELFStreamer(Ctx, TAB, OS, Emitter, RelaxAll, NoExecStack);
135 return createAArch64ELFStreamer(Ctx, TAB, OS, Emitter, RelaxAll);
137136 }
138137
139138 // Force static initialization.
92599259 }
92609260
92619261 if (!Section) {
9262 getStreamer().InitSections();
9262 getStreamer().InitSections(false);
92639263 Section = getStreamer().getCurrentSection().first;
92649264 }
92659265
13461346 return S;
13471347 }
13481348
1349 MCELFStreamer* createARMELFStreamer(MCContext &Context, MCAsmBackend &TAB,
1350 raw_ostream &OS, MCCodeEmitter *Emitter,
1351 bool RelaxAll, bool NoExecStack,
1352 bool IsThumb) {
1349 MCELFStreamer *createARMELFStreamer(MCContext &Context, MCAsmBackend &TAB,
1350 raw_ostream &OS, MCCodeEmitter *Emitter,
1351 bool RelaxAll, bool IsThumb) {
13531352 ARMELFStreamer *S = new ARMELFStreamer(Context, TAB, OS, Emitter, IsThumb);
13541353 new ARMTargetELFStreamer(*S);
13551354 // FIXME: This should eventually end up somewhere else where more
13591358
13601359 if (RelaxAll)
13611360 S->getAssembler().setRelaxAll(true);
1362 if (NoExecStack)
1363 S->getAssembler().setNoExecStack(true);
13641361 return S;
13651362 }
13661363
264264 // This is duplicated code. Refactor this.
265265 static MCStreamer *createMCStreamer(const Target &T, StringRef TT,
266266 MCContext &Ctx, MCAsmBackend &MAB,
267 raw_ostream &OS,
268 MCCodeEmitter *Emitter,
269 const MCSubtargetInfo &STI,
270 bool RelaxAll,
271 bool NoExecStack) {
267 raw_ostream &OS, MCCodeEmitter *Emitter,
268 const MCSubtargetInfo &STI, bool RelaxAll) {
272269 Triple TheTriple(TT);
273270
274271 switch (TheTriple.getObjectFormat()) {
282279 assert(TheTriple.isOSWindows() && "non-Windows ARM COFF is not supported");
283280 return createARMWinCOFFStreamer(Ctx, MAB, *Emitter, OS);
284281 case Triple::ELF:
285 return createARMELFStreamer(Ctx, MAB, OS, Emitter, false, NoExecStack,
282 return createARMELFStreamer(Ctx, MAB, OS, Emitter, false,
286283 TheTriple.getArch() == Triple::thumb);
287284 }
288285 }
3636 namespace llvm {
3737 MCELFStreamer *createMipsELFStreamer(MCContext &Context, MCAsmBackend &MAB,
3838 raw_ostream &OS, MCCodeEmitter *Emitter,
39 const MCSubtargetInfo &STI, bool RelaxAll,
40 bool NoExecStack) {
39 const MCSubtargetInfo &STI,
40 bool RelaxAll) {
4141 return new MipsELFStreamer(Context, MAB, OS, Emitter, STI);
4242 }
4343 }
5151
5252 MCELFStreamer *createMipsELFStreamer(MCContext &Context, MCAsmBackend &MAB,
5353 raw_ostream &OS, MCCodeEmitter *Emitter,
54 const MCSubtargetInfo &STI, bool RelaxAll,
55 bool NoExecStack);
54 const MCSubtargetInfo &STI, bool RelaxAll);
5655 } // namespace llvm.
5756 #endif
2525 raw_ostream &OS,
2626 MCCodeEmitter *Emitter,
2727 const MCSubtargetInfo &STI,
28 bool RelaxAll, bool NoExecStack);
29
28 bool RelaxAll);
3029 }
3130
3231 #endif
108108 static MCStreamer *createMCStreamer(const Target &T, StringRef TT,
109109 MCContext &Context, MCAsmBackend &MAB,
110110 raw_ostream &OS, MCCodeEmitter *Emitter,
111 const MCSubtargetInfo &STI,
112 bool RelaxAll, bool NoExecStack) {
111 const MCSubtargetInfo &STI, bool RelaxAll) {
113112 MCStreamer *S;
114113 if (!Triple(TT).isOSNaCl())
115 S = createMipsELFStreamer(Context, MAB, OS, Emitter, STI, RelaxAll,
116 NoExecStack);
114 S = createMipsELFStreamer(Context, MAB, OS, Emitter, STI, RelaxAll);
117115 else
118 S = createMipsNaClELFStreamer(Context, MAB, OS, Emitter, STI, RelaxAll,
119 NoExecStack);
116 S = createMipsNaClELFStreamer(Context, MAB, OS, Emitter, STI, RelaxAll);
120117 new MipsTargetELFStreamer(*S, STI);
121118 return S;
122119 }
254254 raw_ostream &OS,
255255 MCCodeEmitter *Emitter,
256256 const MCSubtargetInfo &STI,
257 bool RelaxAll, bool NoExecStack) {
257 bool RelaxAll) {
258258 MipsNaClELFStreamer *S = new MipsNaClELFStreamer(Context, TAB, OS, Emitter,
259259 STI);
260260 if (RelaxAll)
261261 S->getAssembler().setRelaxAll(true);
262 if (NoExecStack)
263 S->getAssembler().setNoExecStack(true);
264262
265263 // Set bundle-alignment as required by the NaCl ABI for the target.
266264 S->EmitBundleAlignMode(MIPS_NACL_BUNDLE_ALIGN);
207207 // This is duplicated code. Refactor this.
208208 static MCStreamer *createMCStreamer(const Target &T, StringRef TT,
209209 MCContext &Ctx, MCAsmBackend &MAB,
210 raw_ostream &OS,
211 MCCodeEmitter *Emitter,
212 const MCSubtargetInfo &STI,
213 bool RelaxAll,
214 bool NoExecStack) {
210 raw_ostream &OS, MCCodeEmitter *Emitter,
211 const MCSubtargetInfo &STI, bool RelaxAll) {
215212 if (Triple(TT).isOSDarwin()) {
216213 MCStreamer *S = createMachOStreamer(Ctx, MAB, OS, Emitter, RelaxAll);
217214 new PPCTargetMachOStreamer(*S);
218215 return S;
219216 }
220217
221 MCStreamer *S =
222 createELFStreamer(Ctx, MAB, OS, Emitter, RelaxAll, NoExecStack);
218 MCStreamer *S = createELFStreamer(Ctx, MAB, OS, Emitter, RelaxAll);
223219 new PPCTargetELFStreamer(*S);
224220 return S;
225221 }
8383
8484 static MCStreamer *createMCStreamer(const Target &T, StringRef TT,
8585 MCContext &Ctx, MCAsmBackend &MAB,
86 raw_ostream &_OS,
87 MCCodeEmitter *_Emitter,
88 const MCSubtargetInfo &STI,
89 bool RelaxAll,
90 bool NoExecStack) {
91 return createELFStreamer(Ctx, MAB, _OS, _Emitter, false, false);
86 raw_ostream &_OS, MCCodeEmitter *_Emitter,
87 const MCSubtargetInfo &STI, bool RelaxAll) {
88 return createELFStreamer(Ctx, MAB, _OS, _Emitter, false);
9289 }
9390
9491 extern "C" void LLVMInitializeR600TargetMC() {
124124 static MCStreamer *createMCStreamer(const Target &T, StringRef TT,
125125 MCContext &Context, MCAsmBackend &MAB,
126126 raw_ostream &OS, MCCodeEmitter *Emitter,
127 const MCSubtargetInfo &STI, bool RelaxAll,
128 bool NoExecStack) {
129 MCStreamer *S =
130 createELFStreamer(Context, MAB, OS, Emitter, RelaxAll, NoExecStack);
127 const MCSubtargetInfo &STI, bool RelaxAll) {
128 MCStreamer *S = createELFStreamer(Context, MAB, OS, Emitter, RelaxAll);
131129 new SparcTargetELFStreamer(*S);
132130 return S;
133131 }
180180 return new SystemZInstPrinter(MAI, MII, MRI);
181181 }
182182
183 static MCStreamer *createSystemZMCObjectStreamer(const Target &T, StringRef TT,
184 MCContext &Ctx,
185 MCAsmBackend &MAB,
186 raw_ostream &OS,
187 MCCodeEmitter *Emitter,
188 const MCSubtargetInfo &STI,
189 bool RelaxAll,
190 bool NoExecStack) {
191 return createELFStreamer(Ctx, MAB, OS, Emitter, RelaxAll, NoExecStack);
183 static MCStreamer *
184 createSystemZMCObjectStreamer(const Target &T, StringRef TT, MCContext &Ctx,
185 MCAsmBackend &MAB, raw_ostream &OS,
186 MCCodeEmitter *Emitter,
187 const MCSubtargetInfo &STI, bool RelaxAll) {
188 return createELFStreamer(Ctx, MAB, OS, Emitter, RelaxAll);
192189 }
193190
194191 extern "C" void LLVMInitializeSystemZTargetMC() {
350350
351351 static MCStreamer *createMCStreamer(const Target &T, StringRef TT,
352352 MCContext &Ctx, MCAsmBackend &MAB,
353 raw_ostream &_OS,
354 MCCodeEmitter *_Emitter,
355 const MCSubtargetInfo &STI,
356 bool RelaxAll,
357 bool NoExecStack) {
353 raw_ostream &_OS, MCCodeEmitter *_Emitter,
354 const MCSubtargetInfo &STI, bool RelaxAll) {
358355 Triple TheTriple(TT);
359356
360357 switch (TheTriple.getObjectFormat()) {
365362 assert(TheTriple.isOSWindows() && "only Windows COFF is supported");
366363 return createX86WinCOFFStreamer(Ctx, MAB, _Emitter, _OS, RelaxAll);
367364 case Triple::ELF:
368 return createELFStreamer(Ctx, MAB, _OS, _Emitter, RelaxAll, NoExecStack);
365 return createELFStreamer(Ctx, MAB, _OS, _Emitter, RelaxAll);
369366 }
370367 }
371368
184184 }
185185
186186 // Set up initial section manually here
187 Streamer.InitSections();
187 Streamer.InitSections(false);
188188
189189 bool ErrorOccurred = false;
190190
470470 assert(FileType == OFT_ObjectFile && "Invalid file type!");
471471 MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, *STI, Ctx);
472472 MCAsmBackend *MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, MCPU);
473 Str.reset(TheTarget->createMCObjectStreamer(TripleName, Ctx, *MAB,
474 FOS, CE, *STI, RelaxAll,
475 NoExecStack));
473 Str.reset(TheTarget->createMCObjectStreamer(TripleName, Ctx, *MAB, FOS, CE,
474 *STI, RelaxAll));
475 if (NoExecStack)
476 Str->InitSections(true);
476477 }
477478
478479 int Res = 1;