Daniel Dunbar | 8a3c9d9 | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 1 | //===- lib/MC/MCObjectStreamer.cpp - Object File MCStreamer Interface -----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm/MC/MCObjectStreamer.h" |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/STLExtras.h" |
Craig Topper | 6e80c28 | 2012-03-26 06:58:25 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCAsmBackend.h" |
| 13 | #include "llvm/MC/MCAsmInfo.h" |
Daniel Dunbar | 8a3c9d9 | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCAssembler.h" |
Benjamin Kramer | a3e0ddb | 2010-07-29 17:48:06 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCCodeEmitter.h" |
Rafael Espindola | 0a017a6 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCContext.h" |
Rafael Espindola | 72b5488 | 2010-11-01 16:27:31 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCDwarf.h" |
Michael J. Spencer | e2da0a4 | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCExpr.h" |
Craig Topper | 6e80c28 | 2012-03-26 06:58:25 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCObjectWriter.h" |
Serge Pavlov | 24a3ebb | 2013-06-27 14:35:03 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCSection.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCSymbol.h" |
Craig Topper | 6e80c28 | 2012-03-26 06:58:25 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ErrorHandling.h" |
Petr Hosek | 67a94a7 | 2016-05-28 05:57:48 +0000 | [diff] [blame^] | 23 | #include "llvm/Support/SourceMgr.h" |
Rafael Espindola | 105270f | 2015-03-18 19:08:20 +0000 | [diff] [blame] | 24 | #include "llvm/Support/TargetRegistry.h" |
Daniel Dunbar | 8a3c9d9 | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
Rafael Espindola | 24ea09e | 2014-01-26 06:06:37 +0000 | [diff] [blame] | 27 | MCObjectStreamer::MCObjectStreamer(MCContext &Context, MCAsmBackend &TAB, |
Rafael Espindola | 5560a4c | 2015-04-14 22:14:34 +0000 | [diff] [blame] | 28 | raw_pwrite_stream &OS, |
| 29 | MCCodeEmitter *Emitter_) |
Rafael Espindola | 24ea09e | 2014-01-26 06:06:37 +0000 | [diff] [blame] | 30 | : MCStreamer(Context), |
Chandler Carruth | de093ef | 2013-01-31 23:29:57 +0000 | [diff] [blame] | 31 | Assembler(new MCAssembler(Context, TAB, *Emitter_, |
David Majnemer | 83c862a | 2015-09-01 23:19:38 +0000 | [diff] [blame] | 32 | *TAB.createObjectWriter(OS))), |
Rafael Espindola | 2f9bdd8 | 2015-05-27 20:52:32 +0000 | [diff] [blame] | 33 | EmitEHFrame(true), EmitDebugFrame(false) {} |
Daniel Dunbar | 8a3c9d9 | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 34 | |
| 35 | MCObjectStreamer::~MCObjectStreamer() { |
Benjamin Kramer | a3e0ddb | 2010-07-29 17:48:06 +0000 | [diff] [blame] | 36 | delete &Assembler->getBackend(); |
| 37 | delete &Assembler->getEmitter(); |
Daniel Dunbar | 42a037a | 2010-12-17 02:45:41 +0000 | [diff] [blame] | 38 | delete &Assembler->getWriter(); |
Daniel Dunbar | 8a3c9d9 | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 39 | delete Assembler; |
| 40 | } |
Daniel Dunbar | b2347fe | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 41 | |
Petr Hosek | 9e0c890 | 2015-04-12 23:42:25 +0000 | [diff] [blame] | 42 | void MCObjectStreamer::flushPendingLabels(MCFragment *F, uint64_t FOffset) { |
Rafael Espindola | 81413c0 | 2015-10-03 00:57:12 +0000 | [diff] [blame] | 43 | if (PendingLabels.empty()) |
| 44 | return; |
| 45 | if (!F) { |
| 46 | F = new MCDataFragment(); |
| 47 | MCSection *CurSection = getCurrentSectionOnly(); |
| 48 | CurSection->getFragmentList().insert(CurInsertionPoint, F); |
| 49 | F->setParent(CurSection); |
Derek Schuff | 5f708e5 | 2014-10-22 22:38:06 +0000 | [diff] [blame] | 50 | } |
Rafael Espindola | 81413c0 | 2015-10-03 00:57:12 +0000 | [diff] [blame] | 51 | for (MCSymbol *Sym : PendingLabels) { |
| 52 | Sym->setFragment(F); |
| 53 | Sym->setOffset(FOffset); |
| 54 | } |
| 55 | PendingLabels.clear(); |
Derek Schuff | 5f708e5 | 2014-10-22 22:38:06 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Rafael Espindola | 7c6e6e4 | 2015-06-11 18:58:08 +0000 | [diff] [blame] | 58 | void MCObjectStreamer::emitAbsoluteSymbolDiff(const MCSymbol *Hi, |
Duncan P. N. Exon Smith | 0b73d71 | 2015-05-21 02:41:23 +0000 | [diff] [blame] | 59 | const MCSymbol *Lo, |
| 60 | unsigned Size) { |
Rafael Espindola | 7c6e6e4 | 2015-06-11 18:58:08 +0000 | [diff] [blame] | 61 | // If not assigned to the same (valid) fragment, fallback. |
Rafael Espindola | e3a20f5 | 2015-10-05 12:07:05 +0000 | [diff] [blame] | 62 | if (!Hi->getFragment() || Hi->getFragment() != Lo->getFragment() || |
| 63 | Hi->isVariable() || Lo->isVariable()) { |
Rafael Espindola | 7c6e6e4 | 2015-06-11 18:58:08 +0000 | [diff] [blame] | 64 | MCStreamer::emitAbsoluteSymbolDiff(Hi, Lo, Size); |
| 65 | return; |
| 66 | } |
Duncan P. N. Exon Smith | 0b73d71 | 2015-05-21 02:41:23 +0000 | [diff] [blame] | 67 | |
Rafael Espindola | 1467250 | 2015-05-29 17:48:04 +0000 | [diff] [blame] | 68 | EmitIntValue(Hi->getOffset() - Lo->getOffset(), Size); |
Duncan P. N. Exon Smith | 0b73d71 | 2015-05-21 02:41:23 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Pedro Artigas | 7212ee4 | 2012-12-12 22:59:46 +0000 | [diff] [blame] | 71 | void MCObjectStreamer::reset() { |
Pedro Artigas | b95c53e | 2012-12-14 18:52:11 +0000 | [diff] [blame] | 72 | if (Assembler) |
| 73 | Assembler->reset(); |
Rafael Espindola | a32d0e9 | 2015-05-27 15:14:11 +0000 | [diff] [blame] | 74 | CurInsertionPoint = MCSection::iterator(); |
Rafael Espindola | 3dd8ef6 | 2014-05-12 14:02:44 +0000 | [diff] [blame] | 75 | EmitEHFrame = true; |
| 76 | EmitDebugFrame = false; |
Derek Schuff | 5f708e5 | 2014-10-22 22:38:06 +0000 | [diff] [blame] | 77 | PendingLabels.clear(); |
Pedro Artigas | 7212ee4 | 2012-12-12 22:59:46 +0000 | [diff] [blame] | 78 | MCStreamer::reset(); |
| 79 | } |
| 80 | |
Rafael Espindola | 3dd8ef6 | 2014-05-12 14:02:44 +0000 | [diff] [blame] | 81 | void MCObjectStreamer::EmitFrames(MCAsmBackend *MAB) { |
| 82 | if (!getNumFrameInfos()) |
| 83 | return; |
| 84 | |
| 85 | if (EmitEHFrame) |
| 86 | MCDwarfFrameEmitter::Emit(*this, MAB, true); |
| 87 | |
| 88 | if (EmitDebugFrame) |
| 89 | MCDwarfFrameEmitter::Emit(*this, MAB, false); |
| 90 | } |
| 91 | |
Michael J. Spencer | e2da0a4 | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 92 | MCFragment *MCObjectStreamer::getCurrentFragment() const { |
Rafael Espindola | 983bec6 | 2015-05-27 21:04:14 +0000 | [diff] [blame] | 93 | assert(getCurrentSectionOnly() && "No current section!"); |
Michael J. Spencer | e2da0a4 | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 94 | |
Rafael Espindola | 983bec6 | 2015-05-27 21:04:14 +0000 | [diff] [blame] | 95 | if (CurInsertionPoint != getCurrentSectionOnly()->getFragmentList().begin()) |
Duncan P. N. Exon Smith | a5f45da | 2015-10-10 00:13:11 +0000 | [diff] [blame] | 96 | return &*std::prev(CurInsertionPoint); |
Michael J. Spencer | e2da0a4 | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 97 | |
Craig Topper | bb694de | 2014-04-13 04:57:38 +0000 | [diff] [blame] | 98 | return nullptr; |
Michael J. Spencer | e2da0a4 | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Derek Schuff | 5f708e5 | 2014-10-22 22:38:06 +0000 | [diff] [blame] | 101 | MCDataFragment *MCObjectStreamer::getOrCreateDataFragment() { |
Michael J. Spencer | e2da0a4 | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 102 | MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment()); |
Derek Schuff | 8878bcc9 | 2013-02-15 22:50:52 +0000 | [diff] [blame] | 103 | // When bundling is enabled, we don't want to add data to a fragment that |
| 104 | // already has instructions (see MCELFStreamer::EmitInstToData for details) |
Petr Hosek | 9e0c890 | 2015-04-12 23:42:25 +0000 | [diff] [blame] | 105 | if (!F || (Assembler->isBundlingEnabled() && !Assembler->getRelaxAll() && |
| 106 | F->hasInstructions())) { |
David Blaikie | 4d3b043 | 2014-04-10 21:53:47 +0000 | [diff] [blame] | 107 | F = new MCDataFragment(); |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 108 | insert(F); |
| 109 | } |
Michael J. Spencer | e2da0a4 | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 110 | return F; |
| 111 | } |
| 112 | |
Rafael Espindola | 2be1281 | 2014-06-25 15:29:54 +0000 | [diff] [blame] | 113 | void MCObjectStreamer::visitUsedSymbol(const MCSymbol &Sym) { |
Rafael Espindola | b5d316b | 2015-05-29 20:21:02 +0000 | [diff] [blame] | 114 | Assembler->registerSymbol(Sym); |
Rafael Espindola | 2be1281 | 2014-06-25 15:29:54 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Rafael Espindola | 3dd8ef6 | 2014-05-12 14:02:44 +0000 | [diff] [blame] | 117 | void MCObjectStreamer::EmitCFISections(bool EH, bool Debug) { |
| 118 | MCStreamer::EmitCFISections(EH, Debug); |
| 119 | EmitEHFrame = EH; |
| 120 | EmitDebugFrame = Debug; |
| 121 | } |
| 122 | |
Kevin Enderby | 96918bc | 2014-04-22 17:27:29 +0000 | [diff] [blame] | 123 | void MCObjectStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size, |
Craig Topper | 3c76c52 | 2015-09-20 23:35:59 +0000 | [diff] [blame] | 124 | SMLoc Loc) { |
Rafael Espindola | 591c641 | 2014-06-25 18:37:33 +0000 | [diff] [blame] | 125 | MCStreamer::EmitValueImpl(Value, Size, Loc); |
Rafael Espindola | a084fd6 | 2010-11-28 23:08:47 +0000 | [diff] [blame] | 126 | MCDataFragment *DF = getOrCreateDataFragment(); |
Petr Hosek | 3294670 | 2015-06-27 01:54:17 +0000 | [diff] [blame] | 127 | flushPendingLabels(DF, DF->getContents().size()); |
Rafael Espindola | a084fd6 | 2010-11-28 23:08:47 +0000 | [diff] [blame] | 128 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 129 | MCCVLineEntry::Make(this); |
David Majnemer | 8cc3078 | 2016-01-21 01:59:03 +0000 | [diff] [blame] | 130 | MCDwarfLineEntry::Make(this, getCurrentSection().first); |
Cameron Zwarich | 80cbcd2 | 2013-05-25 21:56:53 +0000 | [diff] [blame] | 131 | |
Rafael Espindola | a084fd6 | 2010-11-28 23:08:47 +0000 | [diff] [blame] | 132 | // Avoid fixups when possible. |
| 133 | int64_t AbsValue; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 134 | if (Value->evaluateAsAbsolute(AbsValue, getAssembler())) { |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 135 | EmitIntValue(AbsValue, Size); |
Rafael Espindola | 4c70eea | 2010-12-03 02:54:21 +0000 | [diff] [blame] | 136 | return; |
Rafael Espindola | a084fd6 | 2010-11-28 23:08:47 +0000 | [diff] [blame] | 137 | } |
Eli Bendersky | a31a894 | 2012-12-07 19:13:57 +0000 | [diff] [blame] | 138 | DF->getFixups().push_back( |
Jim Grosbach | 63661f8 | 2015-05-15 19:13:05 +0000 | [diff] [blame] | 139 | MCFixup::create(DF->getContents().size(), Value, |
Kevin Enderby | 96918bc | 2014-04-22 17:27:29 +0000 | [diff] [blame] | 140 | MCFixup::getKindForSize(Size, false), Loc)); |
Rafael Espindola | 4c70eea | 2010-12-03 02:54:21 +0000 | [diff] [blame] | 141 | DF->getContents().resize(DF->getContents().size() + Size, 0); |
Rafael Espindola | a084fd6 | 2010-11-28 23:08:47 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Oliver Stannard | cf6bfb1 | 2014-11-03 12:19:03 +0000 | [diff] [blame] | 144 | void MCObjectStreamer::EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame) { |
| 145 | // We need to create a local symbol to avoid relocations. |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 146 | Frame.Begin = getContext().createTempSymbol(); |
Oliver Stannard | cf6bfb1 | 2014-11-03 12:19:03 +0000 | [diff] [blame] | 147 | EmitLabel(Frame.Begin); |
Rafael Espindola | 3824120 | 2012-01-07 22:42:19 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Rafael Espindola | f28213c | 2012-01-09 00:17:29 +0000 | [diff] [blame] | 150 | void MCObjectStreamer::EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) { |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 151 | Frame.End = getContext().createTempSymbol(); |
Rafael Espindola | 49bbfd0 | 2014-06-25 00:13:59 +0000 | [diff] [blame] | 152 | EmitLabel(Frame.End); |
Rafael Espindola | f28213c | 2012-01-09 00:17:29 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Rafael Espindola | e5b7415 | 2010-11-28 17:18:55 +0000 | [diff] [blame] | 155 | void MCObjectStreamer::EmitLabel(MCSymbol *Symbol) { |
Rafael Espindola | 27c0c9b | 2011-04-27 15:21:19 +0000 | [diff] [blame] | 156 | MCStreamer::EmitLabel(Symbol); |
Rafael Espindola | e5b7415 | 2010-11-28 17:18:55 +0000 | [diff] [blame] | 157 | |
Rafael Espindola | b5d316b | 2015-05-29 20:21:02 +0000 | [diff] [blame] | 158 | getAssembler().registerSymbol(*Symbol); |
Derek Schuff | 5f708e5 | 2014-10-22 22:38:06 +0000 | [diff] [blame] | 159 | |
| 160 | // If there is a current fragment, mark the symbol as pointing into it. |
| 161 | // Otherwise queue the label and set its fragment pointer when we emit the |
| 162 | // next fragment. |
Petr Hosek | 9e0c890 | 2015-04-12 23:42:25 +0000 | [diff] [blame] | 163 | auto *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment()); |
| 164 | if (F && !(getAssembler().isBundlingEnabled() && |
| 165 | getAssembler().getRelaxAll())) { |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 166 | Symbol->setFragment(F); |
Rafael Espindola | 1467250 | 2015-05-29 17:48:04 +0000 | [diff] [blame] | 167 | Symbol->setOffset(F->getContents().size()); |
Derek Schuff | 5f708e5 | 2014-10-22 22:38:06 +0000 | [diff] [blame] | 168 | } else { |
Rafael Espindola | 66ccf49 | 2015-05-29 17:41:59 +0000 | [diff] [blame] | 169 | PendingLabels.push_back(Symbol); |
Derek Schuff | 5f708e5 | 2014-10-22 22:38:06 +0000 | [diff] [blame] | 170 | } |
Rafael Espindola | e5b7415 | 2010-11-28 17:18:55 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Rafael Espindola | 6aea592 | 2011-04-21 23:39:26 +0000 | [diff] [blame] | 173 | void MCObjectStreamer::EmitULEB128Value(const MCExpr *Value) { |
Rafael Espindola | 675fbb2 | 2010-12-03 01:19:49 +0000 | [diff] [blame] | 174 | int64_t IntValue; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 175 | if (Value->evaluateAsAbsolute(IntValue, getAssembler())) { |
Rafael Espindola | 6aea592 | 2011-04-21 23:39:26 +0000 | [diff] [blame] | 176 | EmitULEB128IntValue(IntValue); |
Rafael Espindola | 675fbb2 | 2010-12-03 01:19:49 +0000 | [diff] [blame] | 177 | return; |
| 178 | } |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 179 | insert(new MCLEBFragment(*Value, false)); |
Rafael Espindola | 5e87498 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Rafael Espindola | 6aea592 | 2011-04-21 23:39:26 +0000 | [diff] [blame] | 182 | void MCObjectStreamer::EmitSLEB128Value(const MCExpr *Value) { |
Rafael Espindola | 675fbb2 | 2010-12-03 01:19:49 +0000 | [diff] [blame] | 183 | int64_t IntValue; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 184 | if (Value->evaluateAsAbsolute(IntValue, getAssembler())) { |
Rafael Espindola | 6aea592 | 2011-04-21 23:39:26 +0000 | [diff] [blame] | 185 | EmitSLEB128IntValue(IntValue); |
Rafael Espindola | 675fbb2 | 2010-12-03 01:19:49 +0000 | [diff] [blame] | 186 | return; |
| 187 | } |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 188 | insert(new MCLEBFragment(*Value, true)); |
Rafael Espindola | 5e87498 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Rafael Espindola | 1614597 | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 191 | void MCObjectStreamer::EmitWeakReference(MCSymbol *Alias, |
| 192 | const MCSymbol *Symbol) { |
| 193 | report_fatal_error("This file format doesn't support weak aliases."); |
| 194 | } |
| 195 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 196 | void MCObjectStreamer::ChangeSection(MCSection *Section, |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 197 | const MCExpr *Subsection) { |
Rafael Espindola | 36a15cb | 2015-03-20 20:00:01 +0000 | [diff] [blame] | 198 | changeSectionImpl(Section, Subsection); |
| 199 | } |
| 200 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 201 | bool MCObjectStreamer::changeSectionImpl(MCSection *Section, |
Rafael Espindola | 36a15cb | 2015-03-20 20:00:01 +0000 | [diff] [blame] | 202 | const MCExpr *Subsection) { |
Daniel Dunbar | b2347fe | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 203 | assert(Section && "Cannot switch to a null section!"); |
Derek Schuff | 5f708e5 | 2014-10-22 22:38:06 +0000 | [diff] [blame] | 204 | flushPendingLabels(nullptr); |
Daniel Dunbar | b2347fe | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 205 | |
Rafael Espindola | bb9a71c | 2015-05-26 15:07:25 +0000 | [diff] [blame] | 206 | bool Created = getAssembler().registerSection(*Section); |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 207 | |
| 208 | int64_t IntSubsection = 0; |
| 209 | if (Subsection && |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 210 | !Subsection->evaluateAsAbsolute(IntSubsection, getAssembler())) |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 211 | report_fatal_error("Cannot evaluate subsection number"); |
| 212 | if (IntSubsection < 0 || IntSubsection > 8192) |
| 213 | report_fatal_error("Subsection number out of range"); |
| 214 | CurInsertionPoint = |
Rafael Espindola | 2f9bdd8 | 2015-05-27 20:52:32 +0000 | [diff] [blame] | 215 | Section->getSubsectionInsertionPoint(unsigned(IntSubsection)); |
Rafael Espindola | 36a15cb | 2015-03-20 20:00:01 +0000 | [diff] [blame] | 216 | return Created; |
Daniel Dunbar | b2347fe | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Eli Bendersky | ea2824d | 2012-12-07 17:42:41 +0000 | [diff] [blame] | 219 | void MCObjectStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) { |
Rafael Espindola | b5d316b | 2015-05-29 20:21:02 +0000 | [diff] [blame] | 220 | getAssembler().registerSymbol(*Symbol); |
Zoran Jovanovic | 28221d8 | 2014-03-20 09:44:49 +0000 | [diff] [blame] | 221 | MCStreamer::EmitAssignment(Symbol, Value); |
Eli Bendersky | ea2824d | 2012-12-07 17:42:41 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Rafael Espindola | cd62518 | 2015-05-25 18:34:26 +0000 | [diff] [blame] | 224 | bool MCObjectStreamer::mayHaveInstructions(MCSection &Sec) const { |
| 225 | return Sec.hasInstructions(); |
| 226 | } |
| 227 | |
Rafael Espindola | 591c641 | 2014-06-25 18:37:33 +0000 | [diff] [blame] | 228 | void MCObjectStreamer::EmitInstruction(const MCInst &Inst, |
| 229 | const MCSubtargetInfo &STI) { |
| 230 | MCStreamer::EmitInstruction(Inst, STI); |
Rafael Espindola | 72b5488 | 2010-11-01 16:27:31 +0000 | [diff] [blame] | 231 | |
Rafael Espindola | 983bec6 | 2015-05-27 21:04:14 +0000 | [diff] [blame] | 232 | MCSection *Sec = getCurrentSectionOnly(); |
Rafael Espindola | 3d2aeb2 | 2015-05-26 14:48:11 +0000 | [diff] [blame] | 233 | Sec->setHasInstructions(true); |
Rafael Espindola | 72b5488 | 2010-11-01 16:27:31 +0000 | [diff] [blame] | 234 | |
| 235 | // Now that a machine instruction has been assembled into this section, make |
| 236 | // a line entry for any .loc directive that has been seen. |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 237 | MCCVLineEntry::Make(this); |
David Majnemer | 8cc3078 | 2016-01-21 01:59:03 +0000 | [diff] [blame] | 238 | MCDwarfLineEntry::Make(this, getCurrentSection().first); |
Rafael Espindola | 72b5488 | 2010-11-01 16:27:31 +0000 | [diff] [blame] | 239 | |
| 240 | // If this instruction doesn't need relaxation, just emit it as data. |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 241 | MCAssembler &Assembler = getAssembler(); |
| 242 | if (!Assembler.getBackend().mayNeedRelaxation(Inst)) { |
David Woodhouse | 6f3c73f | 2014-01-28 23:12:49 +0000 | [diff] [blame] | 243 | EmitInstToData(Inst, STI); |
Rafael Espindola | 72b5488 | 2010-11-01 16:27:31 +0000 | [diff] [blame] | 244 | return; |
| 245 | } |
| 246 | |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 247 | // Otherwise, relax and emit it as data if either: |
| 248 | // - The RelaxAll flag was passed |
| 249 | // - Bundling is enabled and this instruction is inside a bundle-locked |
| 250 | // group. We want to emit all such instructions into the same data |
| 251 | // fragment. |
| 252 | if (Assembler.getRelaxAll() || |
Rafael Espindola | 3d2aeb2 | 2015-05-26 14:48:11 +0000 | [diff] [blame] | 253 | (Assembler.isBundlingEnabled() && Sec->isBundleLocked())) { |
Rafael Espindola | 72b5488 | 2010-11-01 16:27:31 +0000 | [diff] [blame] | 254 | MCInst Relaxed; |
Jim Grosbach | aba3de9 | 2012-01-18 18:52:16 +0000 | [diff] [blame] | 255 | getAssembler().getBackend().relaxInstruction(Inst, Relaxed); |
| 256 | while (getAssembler().getBackend().mayNeedRelaxation(Relaxed)) |
| 257 | getAssembler().getBackend().relaxInstruction(Relaxed, Relaxed); |
David Woodhouse | 6f3c73f | 2014-01-28 23:12:49 +0000 | [diff] [blame] | 258 | EmitInstToData(Relaxed, STI); |
Rafael Espindola | 72b5488 | 2010-11-01 16:27:31 +0000 | [diff] [blame] | 259 | return; |
| 260 | } |
| 261 | |
| 262 | // Otherwise emit to a separate fragment. |
David Woodhouse | 6f3c73f | 2014-01-28 23:12:49 +0000 | [diff] [blame] | 263 | EmitInstToFragment(Inst, STI); |
Rafael Espindola | 72b5488 | 2010-11-01 16:27:31 +0000 | [diff] [blame] | 264 | } |
| 265 | |
David Woodhouse | 6f3c73f | 2014-01-28 23:12:49 +0000 | [diff] [blame] | 266 | void MCObjectStreamer::EmitInstToFragment(const MCInst &Inst, |
| 267 | const MCSubtargetInfo &STI) { |
Petr Hosek | 9e0c890 | 2015-04-12 23:42:25 +0000 | [diff] [blame] | 268 | if (getAssembler().getRelaxAll() && getAssembler().isBundlingEnabled()) |
| 269 | llvm_unreachable("All instructions should have already been relaxed"); |
| 270 | |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 271 | // Always create a new, separate fragment here, because its size can change |
| 272 | // during relaxation. |
David Woodhouse | f5199f6 | 2014-01-28 23:12:53 +0000 | [diff] [blame] | 273 | MCRelaxableFragment *IF = new MCRelaxableFragment(Inst, STI); |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 274 | insert(IF); |
Rafael Espindola | 3fa6fc1 | 2010-12-02 05:44:06 +0000 | [diff] [blame] | 275 | |
Eli Friedman | b2545fb | 2011-04-18 20:54:46 +0000 | [diff] [blame] | 276 | SmallString<128> Code; |
| 277 | raw_svector_ostream VecOS(Code); |
Jim Grosbach | 91df21f | 2015-05-15 19:13:16 +0000 | [diff] [blame] | 278 | getAssembler().getEmitter().encodeInstruction(Inst, VecOS, IF->getFixups(), |
David Woodhouse | 9784cef | 2014-01-28 23:13:07 +0000 | [diff] [blame] | 279 | STI); |
Eli Bendersky | a31a894 | 2012-12-07 19:13:57 +0000 | [diff] [blame] | 280 | IF->getContents().append(Code.begin(), Code.end()); |
Rafael Espindola | 3fa6fc1 | 2010-12-02 05:44:06 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Matt Beaumont-Gay | 5ae7208 | 2013-02-15 23:12:33 +0000 | [diff] [blame] | 283 | #ifndef NDEBUG |
Craig Topper | d3a34f8 | 2013-07-16 01:17:10 +0000 | [diff] [blame] | 284 | static const char *const BundlingNotImplementedMsg = |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 285 | "Aligned bundling is not implemented for this object format"; |
Matt Beaumont-Gay | 5ae7208 | 2013-02-15 23:12:33 +0000 | [diff] [blame] | 286 | #endif |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 287 | |
| 288 | void MCObjectStreamer::EmitBundleAlignMode(unsigned AlignPow2) { |
| 289 | llvm_unreachable(BundlingNotImplementedMsg); |
| 290 | } |
| 291 | |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 292 | void MCObjectStreamer::EmitBundleLock(bool AlignToEnd) { |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 293 | llvm_unreachable(BundlingNotImplementedMsg); |
| 294 | } |
| 295 | |
| 296 | void MCObjectStreamer::EmitBundleUnlock() { |
| 297 | llvm_unreachable(BundlingNotImplementedMsg); |
| 298 | } |
| 299 | |
Ulrich Weigand | 64f4405 | 2013-06-19 21:27:27 +0000 | [diff] [blame] | 300 | void MCObjectStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line, |
| 301 | unsigned Column, unsigned Flags, |
| 302 | unsigned Isa, |
| 303 | unsigned Discriminator, |
| 304 | StringRef FileName) { |
| 305 | // In case we see two .loc directives in a row, make sure the |
| 306 | // first one gets a line entry. |
David Majnemer | 8cc3078 | 2016-01-21 01:59:03 +0000 | [diff] [blame] | 307 | MCDwarfLineEntry::Make(this, getCurrentSection().first); |
Ulrich Weigand | 64f4405 | 2013-06-19 21:27:27 +0000 | [diff] [blame] | 308 | |
| 309 | this->MCStreamer::EmitDwarfLocDirective(FileNo, Line, Column, Flags, |
| 310 | Isa, Discriminator, FileName); |
| 311 | } |
| 312 | |
Rafael Espindola | 3851808 | 2014-08-15 14:31:47 +0000 | [diff] [blame] | 313 | static const MCExpr *buildSymbolDiff(MCObjectStreamer &OS, const MCSymbol *A, |
| 314 | const MCSymbol *B) { |
| 315 | MCContext &Context = OS.getContext(); |
| 316 | MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 317 | const MCExpr *ARef = MCSymbolRefExpr::create(A, Variant, Context); |
| 318 | const MCExpr *BRef = MCSymbolRefExpr::create(B, Variant, Context); |
Rafael Espindola | 3851808 | 2014-08-15 14:31:47 +0000 | [diff] [blame] | 319 | const MCExpr *AddrDelta = |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 320 | MCBinaryExpr::create(MCBinaryExpr::Sub, ARef, BRef, Context); |
Rafael Espindola | 3851808 | 2014-08-15 14:31:47 +0000 | [diff] [blame] | 321 | return AddrDelta; |
| 322 | } |
| 323 | |
Frederic Riss | a5ab844 | 2015-08-07 15:14:08 +0000 | [diff] [blame] | 324 | static void emitDwarfSetLineAddr(MCObjectStreamer &OS, |
| 325 | MCDwarfLineTableParams Params, |
| 326 | int64_t LineDelta, const MCSymbol *Label, |
| 327 | int PointerSize) { |
Rafael Espindola | 5e955fb | 2014-08-15 14:43:02 +0000 | [diff] [blame] | 328 | // emit the sequence to set the address |
| 329 | OS.EmitIntValue(dwarf::DW_LNS_extended_op, 1); |
| 330 | OS.EmitULEB128IntValue(PointerSize + 1); |
| 331 | OS.EmitIntValue(dwarf::DW_LNE_set_address, 1); |
| 332 | OS.EmitSymbolValue(Label, PointerSize); |
| 333 | |
| 334 | // emit the sequence for the LineDelta (from 1) and a zero address delta. |
Frederic Riss | a5ab844 | 2015-08-07 15:14:08 +0000 | [diff] [blame] | 335 | MCDwarfLineAddr::Emit(&OS, Params, LineDelta, 0); |
Rafael Espindola | 5e955fb | 2014-08-15 14:43:02 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Rafael Espindola | 57ab708 | 2010-12-03 00:55:40 +0000 | [diff] [blame] | 338 | void MCObjectStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta, |
| 339 | const MCSymbol *LastLabel, |
Evan Cheng | c7ac690 | 2011-07-14 05:43:07 +0000 | [diff] [blame] | 340 | const MCSymbol *Label, |
| 341 | unsigned PointerSize) { |
Rafael Espindola | 57ab708 | 2010-12-03 00:55:40 +0000 | [diff] [blame] | 342 | if (!LastLabel) { |
Frederic Riss | a5ab844 | 2015-08-07 15:14:08 +0000 | [diff] [blame] | 343 | emitDwarfSetLineAddr(*this, Assembler->getDWARFLinetableParams(), LineDelta, |
| 344 | Label, PointerSize); |
Rafael Espindola | 57ab708 | 2010-12-03 00:55:40 +0000 | [diff] [blame] | 345 | return; |
| 346 | } |
Rafael Espindola | 3851808 | 2014-08-15 14:31:47 +0000 | [diff] [blame] | 347 | const MCExpr *AddrDelta = buildSymbolDiff(*this, Label, LastLabel); |
Rafael Espindola | 57ab708 | 2010-12-03 00:55:40 +0000 | [diff] [blame] | 348 | int64_t Res; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 349 | if (AddrDelta->evaluateAsAbsolute(Res, getAssembler())) { |
Frederic Riss | a5ab844 | 2015-08-07 15:14:08 +0000 | [diff] [blame] | 350 | MCDwarfLineAddr::Emit(this, Assembler->getDWARFLinetableParams(), LineDelta, |
| 351 | Res); |
Rafael Espindola | 57ab708 | 2010-12-03 00:55:40 +0000 | [diff] [blame] | 352 | return; |
| 353 | } |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 354 | insert(new MCDwarfLineAddrFragment(LineDelta, *AddrDelta)); |
Rafael Espindola | 57ab708 | 2010-12-03 00:55:40 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Rafael Espindola | 736a35d | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 357 | void MCObjectStreamer::EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel, |
| 358 | const MCSymbol *Label) { |
Rafael Espindola | 3851808 | 2014-08-15 14:31:47 +0000 | [diff] [blame] | 359 | const MCExpr *AddrDelta = buildSymbolDiff(*this, Label, LastLabel); |
Rafael Espindola | 736a35d | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 360 | int64_t Res; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 361 | if (AddrDelta->evaluateAsAbsolute(Res, getAssembler())) { |
Rafael Espindola | 736a35d | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 362 | MCDwarfFrameEmitter::EmitAdvanceLoc(*this, Res); |
| 363 | return; |
| 364 | } |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 365 | insert(new MCDwarfCallFrameFragment(*AddrDelta)); |
Rafael Espindola | 736a35d | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 368 | void MCObjectStreamer::EmitCVLocDirective(unsigned FunctionId, unsigned FileNo, |
| 369 | unsigned Line, unsigned Column, |
| 370 | bool PrologueEnd, bool IsStmt, |
| 371 | StringRef FileName) { |
| 372 | // In case we see two .cv_loc directives in a row, make sure the |
| 373 | // first one gets a line entry. |
| 374 | MCCVLineEntry::Make(this); |
| 375 | |
| 376 | this->MCStreamer::EmitCVLocDirective(FunctionId, FileNo, Line, Column, |
| 377 | PrologueEnd, IsStmt, FileName); |
| 378 | } |
| 379 | |
| 380 | void MCObjectStreamer::EmitCVLinetableDirective(unsigned FunctionId, |
| 381 | const MCSymbol *Begin, |
| 382 | const MCSymbol *End) { |
| 383 | getContext().getCVContext().emitLineTableForFunction(*this, FunctionId, Begin, |
| 384 | End); |
| 385 | this->MCStreamer::EmitCVLinetableDirective(FunctionId, Begin, End); |
| 386 | } |
| 387 | |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 388 | void MCObjectStreamer::EmitCVInlineLinetableDirective( |
| 389 | unsigned PrimaryFunctionId, unsigned SourceFileId, unsigned SourceLineNum, |
David Majnemer | c9911f2 | 2016-02-02 19:22:34 +0000 | [diff] [blame] | 390 | const MCSymbol *FnStartSym, const MCSymbol *FnEndSym, |
| 391 | ArrayRef<unsigned> SecondaryFunctionIds) { |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 392 | getContext().getCVContext().emitInlineLineTableForFunction( |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 393 | *this, PrimaryFunctionId, SourceFileId, SourceLineNum, FnStartSym, |
David Majnemer | c9911f2 | 2016-02-02 19:22:34 +0000 | [diff] [blame] | 394 | FnEndSym, SecondaryFunctionIds); |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 395 | this->MCStreamer::EmitCVInlineLinetableDirective( |
David Majnemer | c9911f2 | 2016-02-02 19:22:34 +0000 | [diff] [blame] | 396 | PrimaryFunctionId, SourceFileId, SourceLineNum, FnStartSym, FnEndSym, |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 397 | SecondaryFunctionIds); |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 398 | } |
| 399 | |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 400 | void MCObjectStreamer::EmitCVDefRangeDirective( |
| 401 | ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges, |
| 402 | StringRef FixedSizePortion) { |
| 403 | getContext().getCVContext().emitDefRange(*this, Ranges, FixedSizePortion); |
| 404 | this->MCStreamer::EmitCVDefRangeDirective(Ranges, FixedSizePortion); |
| 405 | } |
| 406 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 407 | void MCObjectStreamer::EmitCVStringTableDirective() { |
| 408 | getContext().getCVContext().emitStringTable(*this); |
| 409 | } |
| 410 | void MCObjectStreamer::EmitCVFileChecksumsDirective() { |
| 411 | getContext().getCVContext().emitFileChecksums(*this); |
| 412 | } |
| 413 | |
| 414 | |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 415 | void MCObjectStreamer::EmitBytes(StringRef Data) { |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 416 | MCCVLineEntry::Make(this); |
David Majnemer | 8cc3078 | 2016-01-21 01:59:03 +0000 | [diff] [blame] | 417 | MCDwarfLineEntry::Make(this, getCurrentSection().first); |
Petr Hosek | 3294670 | 2015-06-27 01:54:17 +0000 | [diff] [blame] | 418 | MCDataFragment *DF = getOrCreateDataFragment(); |
| 419 | flushPendingLabels(DF, DF->getContents().size()); |
| 420 | DF->getContents().append(Data.begin(), Data.end()); |
Benjamin Kramer | 64ddcb0 | 2012-10-04 13:12:43 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | void MCObjectStreamer::EmitValueToAlignment(unsigned ByteAlignment, |
| 424 | int64_t Value, |
| 425 | unsigned ValueSize, |
| 426 | unsigned MaxBytesToEmit) { |
| 427 | if (MaxBytesToEmit == 0) |
| 428 | MaxBytesToEmit = ByteAlignment; |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 429 | insert(new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit)); |
Benjamin Kramer | 64ddcb0 | 2012-10-04 13:12:43 +0000 | [diff] [blame] | 430 | |
| 431 | // Update the maximum alignment on the current section if necessary. |
Rafael Espindola | 967d6a6 | 2015-05-21 21:02:35 +0000 | [diff] [blame] | 432 | MCSection *CurSec = getCurrentSection().first; |
| 433 | if (ByteAlignment > CurSec->getAlignment()) |
| 434 | CurSec->setAlignment(ByteAlignment); |
Benjamin Kramer | 64ddcb0 | 2012-10-04 13:12:43 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | void MCObjectStreamer::EmitCodeAlignment(unsigned ByteAlignment, |
| 438 | unsigned MaxBytesToEmit) { |
| 439 | EmitValueToAlignment(ByteAlignment, 0, 1, MaxBytesToEmit); |
| 440 | cast<MCAlignFragment>(getCurrentFragment())->setEmitNops(true); |
| 441 | } |
| 442 | |
Rafael Espindola | 7ae65d8 | 2015-11-04 23:59:18 +0000 | [diff] [blame] | 443 | void MCObjectStreamer::emitValueToOffset(const MCExpr *Offset, |
Jim Grosbach | 5aac954 | 2012-01-26 23:47:45 +0000 | [diff] [blame] | 444 | unsigned char Value) { |
Rafael Espindola | 04d3926 | 2015-11-04 23:50:29 +0000 | [diff] [blame] | 445 | insert(new MCOrgFragment(*Offset, Value)); |
Rafael Espindola | 9065bfc6 | 2010-12-02 05:59:38 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Akira Hatanaka | f5ddf13 | 2011-11-23 22:18:04 +0000 | [diff] [blame] | 448 | // Associate GPRel32 fixup with data and resize data area |
| 449 | void MCObjectStreamer::EmitGPRel32Value(const MCExpr *Value) { |
| 450 | MCDataFragment *DF = getOrCreateDataFragment(); |
Petr Hosek | 3294670 | 2015-06-27 01:54:17 +0000 | [diff] [blame] | 451 | flushPendingLabels(DF, DF->getContents().size()); |
Akira Hatanaka | f5ddf13 | 2011-11-23 22:18:04 +0000 | [diff] [blame] | 452 | |
Jim Grosbach | 63661f8 | 2015-05-15 19:13:05 +0000 | [diff] [blame] | 453 | DF->getFixups().push_back(MCFixup::create(DF->getContents().size(), |
Eli Bendersky | a31a894 | 2012-12-07 19:13:57 +0000 | [diff] [blame] | 454 | Value, FK_GPRel_4)); |
Akira Hatanaka | f5ddf13 | 2011-11-23 22:18:04 +0000 | [diff] [blame] | 455 | DF->getContents().resize(DF->getContents().size() + 4, 0); |
| 456 | } |
| 457 | |
Jack Carter | 77064c0 | 2012-08-22 00:49:30 +0000 | [diff] [blame] | 458 | // Associate GPRel32 fixup with data and resize data area |
| 459 | void MCObjectStreamer::EmitGPRel64Value(const MCExpr *Value) { |
| 460 | MCDataFragment *DF = getOrCreateDataFragment(); |
Petr Hosek | 3294670 | 2015-06-27 01:54:17 +0000 | [diff] [blame] | 461 | flushPendingLabels(DF, DF->getContents().size()); |
Jack Carter | 77064c0 | 2012-08-22 00:49:30 +0000 | [diff] [blame] | 462 | |
Jim Grosbach | 63661f8 | 2015-05-15 19:13:05 +0000 | [diff] [blame] | 463 | DF->getFixups().push_back(MCFixup::create(DF->getContents().size(), |
Eli Bendersky | a31a894 | 2012-12-07 19:13:57 +0000 | [diff] [blame] | 464 | Value, FK_GPRel_4)); |
Jack Carter | 77064c0 | 2012-08-22 00:49:30 +0000 | [diff] [blame] | 465 | DF->getContents().resize(DF->getContents().size() + 8, 0); |
| 466 | } |
| 467 | |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 468 | bool MCObjectStreamer::EmitRelocDirective(const MCExpr &Offset, StringRef Name, |
| 469 | const MCExpr *Expr, SMLoc Loc) { |
| 470 | int64_t OffsetValue; |
| 471 | if (!Offset.evaluateAsAbsolute(OffsetValue)) |
| 472 | llvm_unreachable("Offset is not absolute"); |
| 473 | |
David Majnemer | ce10842 | 2016-01-19 23:05:27 +0000 | [diff] [blame] | 474 | if (OffsetValue < 0) |
| 475 | llvm_unreachable("Offset is negative"); |
| 476 | |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 477 | MCDataFragment *DF = getOrCreateDataFragment(); |
| 478 | flushPendingLabels(DF, DF->getContents().size()); |
| 479 | |
David Majnemer | ce10842 | 2016-01-19 23:05:27 +0000 | [diff] [blame] | 480 | Optional<MCFixupKind> MaybeKind = Assembler->getBackend().getFixupKind(Name); |
| 481 | if (!MaybeKind.hasValue()) |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 482 | return true; |
| 483 | |
David Majnemer | ce10842 | 2016-01-19 23:05:27 +0000 | [diff] [blame] | 484 | MCFixupKind Kind = *MaybeKind; |
| 485 | |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 486 | if (Expr == nullptr) |
| 487 | Expr = |
| 488 | MCSymbolRefExpr::create(getContext().createTempSymbol(), getContext()); |
| 489 | DF->getFixups().push_back(MCFixup::create(OffsetValue, Expr, Kind, Loc)); |
| 490 | return false; |
| 491 | } |
| 492 | |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 493 | void MCObjectStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue) { |
Michael Ilseman | 5be22a1 | 2014-12-12 21:48:03 +0000 | [diff] [blame] | 494 | const MCSection *Sec = getCurrentSection().first; |
Rafael Espindola | 1a7e8b4 | 2016-01-19 16:57:08 +0000 | [diff] [blame] | 495 | (void)Sec; |
Michael Ilseman | 5be22a1 | 2014-12-12 21:48:03 +0000 | [diff] [blame] | 496 | assert(Sec && "need a section"); |
Rafael Espindola | 1a7e8b4 | 2016-01-19 16:57:08 +0000 | [diff] [blame] | 497 | insert(new MCFillFragment(FillValue, NumBytes)); |
Serge Pavlov | 24a3ebb | 2013-06-27 14:35:03 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Petr Hosek | 67a94a7 | 2016-05-28 05:57:48 +0000 | [diff] [blame^] | 500 | void MCObjectStreamer::emitFill(const MCExpr &NumBytes, uint64_t FillValue, |
| 501 | SMLoc Loc) { |
| 502 | MCDataFragment *DF = getOrCreateDataFragment(); |
| 503 | flushPendingLabels(DF, DF->getContents().size()); |
| 504 | |
| 505 | int64_t IntNumBytes; |
| 506 | if (!NumBytes.evaluateAsAbsolute(IntNumBytes, getAssembler())) { |
| 507 | getContext().reportError(Loc, "expected absolute expression"); |
| 508 | return; |
| 509 | } |
| 510 | |
| 511 | if (IntNumBytes <= 0) { |
| 512 | getContext().reportError(Loc, "invalid number of bytes"); |
| 513 | return; |
| 514 | } |
| 515 | |
| 516 | EmitFill(IntNumBytes, FillValue); |
| 517 | } |
| 518 | |
| 519 | void MCObjectStreamer::emitFill(const MCExpr &NumValues, int64_t Size, |
| 520 | int64_t Expr, SMLoc Loc) { |
| 521 | int64_t IntNumValues; |
| 522 | if (!NumValues.evaluateAsAbsolute(IntNumValues, getAssembler())) { |
| 523 | getContext().reportError(Loc, "expected absolute expression"); |
| 524 | return; |
| 525 | } |
| 526 | |
| 527 | if (IntNumValues < 0) { |
| 528 | getContext().getSourceManager()->PrintMessage( |
| 529 | Loc, SourceMgr::DK_Warning, |
| 530 | "'.fill' directive with negative repeat count has no effect"); |
| 531 | return; |
| 532 | } |
| 533 | |
| 534 | MCStreamer::emitFill(IntNumValues, Size, Expr); |
| 535 | } |
| 536 | |
Rafael Espindola | 0708209 | 2012-01-07 03:13:18 +0000 | [diff] [blame] | 537 | void MCObjectStreamer::FinishImpl() { |
Kevin Enderby | e7739d4 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 538 | // If we are generating dwarf for assembly source files dump out the sections. |
| 539 | if (getContext().getGenDwarfForAssembly()) |
David Blaikie | 8bf66c4 | 2014-04-01 07:35:52 +0000 | [diff] [blame] | 540 | MCGenDwarfInfo::Emit(this); |
| 541 | |
| 542 | // Dump out the dwarf file & directory tables and line tables. |
Frederic Riss | a5ab844 | 2015-08-07 15:14:08 +0000 | [diff] [blame] | 543 | MCDwarfLineTable::Emit(this, getAssembler().getDWARFLinetableParams()); |
Kevin Enderby | e7739d4 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 544 | |
Derek Schuff | 5f708e5 | 2014-10-22 22:38:06 +0000 | [diff] [blame] | 545 | flushPendingLabels(nullptr); |
Daniel Dunbar | b2347fe | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 546 | getAssembler().Finish(); |
| 547 | } |