blob: f9b8733b0dde7422884d611a91b7d3ffdc02fbd9 [file] [log] [blame]
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +00001//===- lib/MC/MCObjectStreamer.cpp - Object File MCStreamer Interface -----===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/MC/MCObjectStreamer.h"
Peter Collingbourne2f495b92013-04-17 21:18:16 +000010#include "llvm/ADT/STLExtras.h"
Craig Topper6e80c282012-03-26 06:58:25 +000011#include "llvm/MC/MCAsmBackend.h"
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +000012#include "llvm/MC/MCAssembler.h"
Benjamin Kramera3e0ddb2010-07-29 17:48:06 +000013#include "llvm/MC/MCCodeEmitter.h"
Reid Klecknera5b1eef2016-08-26 17:58:37 +000014#include "llvm/MC/MCCodeView.h"
Rafael Espindola0a017a62010-12-10 07:39:47 +000015#include "llvm/MC/MCContext.h"
Rafael Espindola72b54882010-11-01 16:27:31 +000016#include "llvm/MC/MCDwarf.h"
Michael J. Spencere2da0a42010-07-19 06:13:10 +000017#include "llvm/MC/MCExpr.h"
Craig Topper6e80c282012-03-26 06:58:25 +000018#include "llvm/MC/MCObjectWriter.h"
Serge Pavlov24a3ebb2013-06-27 14:35:03 +000019#include "llvm/MC/MCSection.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000020#include "llvm/MC/MCSymbol.h"
Craig Topper6e80c282012-03-26 06:58:25 +000021#include "llvm/Support/ErrorHandling.h"
Petr Hosek67a94a72016-05-28 05:57:48 +000022#include "llvm/Support/SourceMgr.h"
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +000023using namespace llvm;
24
Lang Hames02d33052017-10-11 01:57:21 +000025MCObjectStreamer::MCObjectStreamer(MCContext &Context,
26 std::unique_ptr<MCAsmBackend> TAB,
Peter Collingbournef7b81db2018-05-18 18:26:45 +000027 std::unique_ptr<MCObjectWriter> OW,
Lang Hames2241ffa2017-10-11 23:34:47 +000028 std::unique_ptr<MCCodeEmitter> Emitter)
Nirav Dave8728e092018-04-27 15:45:27 +000029 : MCStreamer(Context),
Jonas Devlieghere0eaee542019-08-15 15:54:37 +000030 Assembler(std::make_unique<MCAssembler>(
Peter Collingbournef7b81db2018-05-18 18:26:45 +000031 Context, std::move(TAB), std::move(Emitter), std::move(OW))),
Philip Reames29ccb122020-01-08 09:58:42 -080032 EmitEHFrame(true), EmitDebugFrame(false) {
33 if (Assembler->getBackendPtr())
34 setAllowAutoPadding(Assembler->getBackend().allowAutoPadding());
35}
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +000036
Lang Hames2241ffa2017-10-11 23:34:47 +000037MCObjectStreamer::~MCObjectStreamer() {}
Daniel Dunbarb2347fe2010-06-16 20:04:25 +000038
Nirav Dave6c0665e2018-04-30 19:22:40 +000039// AssemblerPtr is used for evaluation of expressions and causes
40// difference between asm and object outputs. Return nullptr to in
41// inline asm mode to limit divergence to assembly inputs.
42MCAssembler *MCObjectStreamer::getAssemblerPtr() {
43 if (getUseAssemblerInfoForParsing())
44 return Assembler.get();
45 return nullptr;
46}
47
Michael Trent6f95d332019-12-11 10:42:37 -080048void MCObjectStreamer::addPendingLabel(MCSymbol* S) {
49 MCSection *CurSection = getCurrentSectionOnly();
50 if (CurSection) {
51 // Register labels that have not yet been assigned to a Section.
52 if (!PendingLabels.empty()) {
53 for (MCSymbol* Sym : PendingLabels)
54 CurSection->addPendingLabel(Sym);
55 PendingLabels.clear();
56 }
57
58 // Add this label to the current Section / Subsection.
59 CurSection->addPendingLabel(S, CurSubsectionIdx);
60
61 // Add this Section to the list of PendingLabelSections.
62 auto SecIt = std::find(PendingLabelSections.begin(),
63 PendingLabelSections.end(), CurSection);
64 if (SecIt == PendingLabelSections.end())
65 PendingLabelSections.push_back(CurSection);
66 }
67 else
68 // There is no Section / Subsection for this label yet.
69 PendingLabels.push_back(S);
70}
71
Petr Hosek9e0c8902015-04-12 23:42:25 +000072void MCObjectStreamer::flushPendingLabels(MCFragment *F, uint64_t FOffset) {
Michael Trent6f95d332019-12-11 10:42:37 -080073 MCSection *CurSection = getCurrentSectionOnly();
74 if (!CurSection) {
75 assert(PendingLabels.empty());
Rafael Espindola81413c02015-10-03 00:57:12 +000076 return;
Michael Trent6f95d332019-12-11 10:42:37 -080077 }
78 // Register labels that have not yet been assigned to a Section.
79 if (!PendingLabels.empty()) {
80 for (MCSymbol* Sym : PendingLabels)
81 CurSection->addPendingLabel(Sym, CurSubsectionIdx);
82 PendingLabels.clear();
83 }
84
85 // Associate a fragment with this label, either the supplied fragment
86 // or an empty data fragment.
87 if (F)
88 CurSection->flushPendingLabels(F, FOffset, CurSubsectionIdx);
89 else
90 CurSection->flushPendingLabels(nullptr, 0, CurSubsectionIdx);
91}
92
93void MCObjectStreamer::flushPendingLabels() {
94 // Register labels that have not yet been assigned to a Section.
95 if (!PendingLabels.empty()) {
Rafael Espindola81413c02015-10-03 00:57:12 +000096 MCSection *CurSection = getCurrentSectionOnly();
Michael Trent6f95d332019-12-11 10:42:37 -080097 assert(CurSection);
98 for (MCSymbol* Sym : PendingLabels)
99 CurSection->addPendingLabel(Sym, CurSubsectionIdx);
100 PendingLabels.clear();
Derek Schuff5f708e52014-10-22 22:38:06 +0000101 }
Michael Trent6f95d332019-12-11 10:42:37 -0800102
103 // Assign an empty data fragment to all remaining pending labels.
104 for (MCSection* Section : PendingLabelSections)
105 Section->flushPendingLabels();
Derek Schuff5f708e52014-10-22 22:38:06 +0000106}
107
Vladimir Stefanovic1d2714b2018-11-21 16:28:39 +0000108// When fixup's offset is a forward declared label, e.g.:
109//
110// .reloc 1f, R_MIPS_JALR, foo
111// 1: nop
112//
113// postpone adding it to Fixups vector until the label is defined and its offset
114// is known.
115void MCObjectStreamer::resolvePendingFixups() {
116 for (PendingMCFixup &PendingFixup : PendingFixups) {
117 if (!PendingFixup.Sym || PendingFixup.Sym->isUndefined ()) {
118 getContext().reportError(PendingFixup.Fixup.getLoc(),
119 "unresolved relocation offset");
120 continue;
121 }
122 flushPendingLabels(PendingFixup.DF, PendingFixup.DF->getContents().size());
123 PendingFixup.Fixup.setOffset(PendingFixup.Sym->getOffset());
124 PendingFixup.DF->getFixups().push_back(PendingFixup.Fixup);
125 }
126 PendingFixups.clear();
127}
128
Rafael Espindolad09b4162018-02-09 17:00:25 +0000129// As a compile-time optimization, avoid allocating and evaluating an MCExpr
130// tree for (Hi - Lo) when Hi and Lo are offsets into the same fragment.
Alex Bradburyfdc46472018-08-16 11:26:37 +0000131static Optional<uint64_t>
132absoluteSymbolDiff(MCAssembler &Asm, const MCSymbol *Hi, const MCSymbol *Lo) {
Reid Kleckner5a791ee2018-03-15 21:24:04 +0000133 assert(Hi && Lo);
Alex Bradburyfdc46472018-08-16 11:26:37 +0000134 if (Asm.getBackendPtr()->requiresDiffExpressionRelocations())
135 return None;
136
Rafael Espindolad09b4162018-02-09 17:00:25 +0000137 if (!Hi->getFragment() || Hi->getFragment() != Lo->getFragment() ||
138 Hi->isVariable() || Lo->isVariable())
139 return None;
140
141 return Hi->getOffset() - Lo->getOffset();
142}
143
Rafael Espindola7c6e6e42015-06-11 18:58:08 +0000144void MCObjectStreamer::emitAbsoluteSymbolDiff(const MCSymbol *Hi,
Duncan P. N. Exon Smith0b73d712015-05-21 02:41:23 +0000145 const MCSymbol *Lo,
146 unsigned Size) {
Alex Bradburyfdc46472018-08-16 11:26:37 +0000147 if (Optional<uint64_t> Diff = absoluteSymbolDiff(getAssembler(), Hi, Lo)) {
Rafael Espindolad09b4162018-02-09 17:00:25 +0000148 EmitIntValue(*Diff, Size);
Rafael Espindola7c6e6e42015-06-11 18:58:08 +0000149 return;
150 }
Rafael Espindolad09b4162018-02-09 17:00:25 +0000151 MCStreamer::emitAbsoluteSymbolDiff(Hi, Lo, Size);
152}
Duncan P. N. Exon Smith0b73d712015-05-21 02:41:23 +0000153
Rafael Espindolad09b4162018-02-09 17:00:25 +0000154void MCObjectStreamer::emitAbsoluteSymbolDiffAsULEB128(const MCSymbol *Hi,
155 const MCSymbol *Lo) {
Alex Bradburyfdc46472018-08-16 11:26:37 +0000156 if (Optional<uint64_t> Diff = absoluteSymbolDiff(getAssembler(), Hi, Lo)) {
Fangrui Song0bc77a02020-02-13 13:26:21 -0800157 emitULEB128IntValue(*Diff);
Rafael Espindolad09b4162018-02-09 17:00:25 +0000158 return;
159 }
160 MCStreamer::emitAbsoluteSymbolDiffAsULEB128(Hi, Lo);
Duncan P. N. Exon Smith0b73d712015-05-21 02:41:23 +0000161}
162
Pedro Artigas7212ee42012-12-12 22:59:46 +0000163void MCObjectStreamer::reset() {
Pedro Artigasb95c53e2012-12-14 18:52:11 +0000164 if (Assembler)
165 Assembler->reset();
Rafael Espindolaa32d0e92015-05-27 15:14:11 +0000166 CurInsertionPoint = MCSection::iterator();
Rafael Espindola3dd8ef62014-05-12 14:02:44 +0000167 EmitEHFrame = true;
168 EmitDebugFrame = false;
Derek Schuff5f708e52014-10-22 22:38:06 +0000169 PendingLabels.clear();
Michael Trent6f95d332019-12-11 10:42:37 -0800170 PendingLabelSections.clear();
Pedro Artigas7212ee42012-12-12 22:59:46 +0000171 MCStreamer::reset();
172}
173
Rafael Espindola3dd8ef62014-05-12 14:02:44 +0000174void MCObjectStreamer::EmitFrames(MCAsmBackend *MAB) {
175 if (!getNumFrameInfos())
176 return;
177
178 if (EmitEHFrame)
179 MCDwarfFrameEmitter::Emit(*this, MAB, true);
180
181 if (EmitDebugFrame)
182 MCDwarfFrameEmitter::Emit(*this, MAB, false);
183}
184
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000185MCFragment *MCObjectStreamer::getCurrentFragment() const {
Rafael Espindola983bec62015-05-27 21:04:14 +0000186 assert(getCurrentSectionOnly() && "No current section!");
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000187
Rafael Espindola983bec62015-05-27 21:04:14 +0000188 if (CurInsertionPoint != getCurrentSectionOnly()->getFragmentList().begin())
Duncan P. N. Exon Smitha5f45da2015-10-10 00:13:11 +0000189 return &*std::prev(CurInsertionPoint);
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000190
Craig Topperbb694de2014-04-13 04:57:38 +0000191 return nullptr;
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000192}
193
Peter Smith57f661b2018-06-06 09:40:06 +0000194static bool CanReuseDataFragment(const MCDataFragment &F,
195 const MCAssembler &Assembler,
196 const MCSubtargetInfo *STI) {
197 if (!F.hasInstructions())
198 return true;
Derek Schuff8878bcc92013-02-15 22:50:52 +0000199 // When bundling is enabled, we don't want to add data to a fragment that
200 // already has instructions (see MCELFStreamer::EmitInstToData for details)
Peter Smith57f661b2018-06-06 09:40:06 +0000201 if (Assembler.isBundlingEnabled())
202 return Assembler.getRelaxAll();
203 // If the subtarget is changed mid fragment we start a new fragment to record
204 // the new STI.
205 return !STI || F.getSubtargetInfo() == STI;
206}
207
208MCDataFragment *
209MCObjectStreamer::getOrCreateDataFragment(const MCSubtargetInfo *STI) {
210 MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
211 if (!F || !CanReuseDataFragment(*F, *Assembler, STI)) {
David Blaikie4d3b0432014-04-10 21:53:47 +0000212 F = new MCDataFragment();
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000213 insert(F);
214 }
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000215 return F;
216}
217
Rafael Espindola2be12812014-06-25 15:29:54 +0000218void MCObjectStreamer::visitUsedSymbol(const MCSymbol &Sym) {
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000219 Assembler->registerSymbol(Sym);
Rafael Espindola2be12812014-06-25 15:29:54 +0000220}
221
Fangrui Songbcd24b22020-02-13 21:58:16 -0800222void MCObjectStreamer::emitCFISections(bool EH, bool Debug) {
223 MCStreamer::emitCFISections(EH, Debug);
Rafael Espindola3dd8ef62014-05-12 14:02:44 +0000224 EmitEHFrame = EH;
225 EmitDebugFrame = Debug;
226}
227
Kevin Enderby96918bc2014-04-22 17:27:29 +0000228void MCObjectStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
Craig Topper3c76c522015-09-20 23:35:59 +0000229 SMLoc Loc) {
Rafael Espindola591c6412014-06-25 18:37:33 +0000230 MCStreamer::EmitValueImpl(Value, Size, Loc);
Rafael Espindolaa084fd62010-11-28 23:08:47 +0000231 MCDataFragment *DF = getOrCreateDataFragment();
Petr Hosek32946702015-06-27 01:54:17 +0000232 flushPendingLabels(DF, DF->getContents().size());
Rafael Espindolaa084fd62010-11-28 23:08:47 +0000233
Eric Christopher445c9522016-10-14 05:47:37 +0000234 MCDwarfLineEntry::Make(this, getCurrentSectionOnly());
Cameron Zwarich80cbcd22013-05-25 21:56:53 +0000235
Rafael Espindolaa084fd62010-11-28 23:08:47 +0000236 // Avoid fixups when possible.
237 int64_t AbsValue;
Nirav Dave6c0665e2018-04-30 19:22:40 +0000238 if (Value->evaluateAsAbsolute(AbsValue, getAssemblerPtr())) {
Arnaud A. de Grandmaison6d241792017-05-15 08:43:27 +0000239 if (!isUIntN(8 * Size, AbsValue) && !isIntN(8 * Size, AbsValue)) {
240 getContext().reportError(
241 Loc, "value evaluated as " + Twine(AbsValue) + " is out of range.");
242 return;
243 }
Rafael Espindola64e1af82013-07-02 15:49:13 +0000244 EmitIntValue(AbsValue, Size);
Rafael Espindola4c70eea2010-12-03 02:54:21 +0000245 return;
Rafael Espindolaa084fd62010-11-28 23:08:47 +0000246 }
Eli Benderskya31a8942012-12-07 19:13:57 +0000247 DF->getFixups().push_back(
Jim Grosbach63661f82015-05-15 19:13:05 +0000248 MCFixup::create(DF->getContents().size(), Value,
Kevin Enderby96918bc2014-04-22 17:27:29 +0000249 MCFixup::getKindForSize(Size, false), Loc));
Rafael Espindola4c70eea2010-12-03 02:54:21 +0000250 DF->getContents().resize(DF->getContents().size() + Size, 0);
Rafael Espindolaa084fd62010-11-28 23:08:47 +0000251}
252
Fangrui Songbcd24b22020-02-13 21:58:16 -0800253MCSymbol *MCObjectStreamer::emitCFILabel() {
Reid Klecknerab23dac2017-10-10 00:57:36 +0000254 MCSymbol *Label = getContext().createTempSymbol("cfi", true);
255 EmitLabel(Label);
256 return Label;
257}
258
Fangrui Songbcd24b22020-02-13 21:58:16 -0800259void MCObjectStreamer::emitCFIStartProcImpl(MCDwarfFrameInfo &Frame) {
Oliver Stannardcf6bfb12014-11-03 12:19:03 +0000260 // We need to create a local symbol to avoid relocations.
Jim Grosbach6f482002015-05-18 18:43:14 +0000261 Frame.Begin = getContext().createTempSymbol();
Oliver Stannardcf6bfb12014-11-03 12:19:03 +0000262 EmitLabel(Frame.Begin);
Rafael Espindola38241202012-01-07 22:42:19 +0000263}
264
Fangrui Songbcd24b22020-02-13 21:58:16 -0800265void MCObjectStreamer::emitCFIEndProcImpl(MCDwarfFrameInfo &Frame) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000266 Frame.End = getContext().createTempSymbol();
Rafael Espindola49bbfd02014-06-25 00:13:59 +0000267 EmitLabel(Frame.End);
Rafael Espindolaf28213c2012-01-09 00:17:29 +0000268}
269
Rafael Espindolabe991572017-02-10 15:13:12 +0000270void MCObjectStreamer::EmitLabel(MCSymbol *Symbol, SMLoc Loc) {
271 MCStreamer::EmitLabel(Symbol, Loc);
Rafael Espindolae5b74152010-11-28 17:18:55 +0000272
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000273 getAssembler().registerSymbol(*Symbol);
Derek Schuff5f708e52014-10-22 22:38:06 +0000274
275 // If there is a current fragment, mark the symbol as pointing into it.
276 // Otherwise queue the label and set its fragment pointer when we emit the
277 // next fragment.
Petr Hosek9e0c8902015-04-12 23:42:25 +0000278 auto *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
279 if (F && !(getAssembler().isBundlingEnabled() &&
280 getAssembler().getRelaxAll())) {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000281 Symbol->setFragment(F);
Rafael Espindola14672502015-05-29 17:48:04 +0000282 Symbol->setOffset(F->getContents().size());
Derek Schuff5f708e52014-10-22 22:38:06 +0000283 } else {
James Y Knightbf142fc2019-11-10 16:12:29 -0500284 // Assign all pending labels to offset 0 within the dummy "pending"
285 // fragment. (They will all be reassigned to a real fragment in
286 // flushPendingLabels())
287 Symbol->setOffset(0);
Michael Trent6f95d332019-12-11 10:42:37 -0800288 addPendingLabel(Symbol);
Derek Schuff5f708e52014-10-22 22:38:06 +0000289 }
Rafael Espindolae5b74152010-11-28 17:18:55 +0000290}
291
James Y Knightbf142fc2019-11-10 16:12:29 -0500292// Emit a label at a previously emitted fragment/offset position. This must be
293// within the currently-active section.
Fangrui Song0bc77a02020-02-13 13:26:21 -0800294void MCObjectStreamer::emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc,
James Y Knightbf142fc2019-11-10 16:12:29 -0500295 MCFragment *F, uint64_t Offset) {
296 assert(F->getParent() == getCurrentSectionOnly());
297
Weiming Zhao74a7fa02017-04-03 21:50:04 +0000298 MCStreamer::EmitLabel(Symbol, Loc);
299 getAssembler().registerSymbol(*Symbol);
300 auto *DF = dyn_cast_or_null<MCDataFragment>(F);
James Y Knightbf142fc2019-11-10 16:12:29 -0500301 Symbol->setOffset(Offset);
302 if (DF) {
Weiming Zhao74a7fa02017-04-03 21:50:04 +0000303 Symbol->setFragment(F);
James Y Knightbf142fc2019-11-10 16:12:29 -0500304 } else {
305 assert(isa<MCDummyFragment>(F) &&
306 "F must either be an MCDataFragment or the pending MCDummyFragment");
307 assert(Offset == 0);
Michael Trent6f95d332019-12-11 10:42:37 -0800308 addPendingLabel(Symbol);
James Y Knightbf142fc2019-11-10 16:12:29 -0500309 }
Weiming Zhao74a7fa02017-04-03 21:50:04 +0000310}
311
Fangrui Song0bc77a02020-02-13 13:26:21 -0800312void MCObjectStreamer::emitULEB128Value(const MCExpr *Value) {
Rafael Espindola675fbb22010-12-03 01:19:49 +0000313 int64_t IntValue;
Nirav Dave6c0665e2018-04-30 19:22:40 +0000314 if (Value->evaluateAsAbsolute(IntValue, getAssemblerPtr())) {
Fangrui Song0bc77a02020-02-13 13:26:21 -0800315 emitULEB128IntValue(IntValue);
Rafael Espindola675fbb22010-12-03 01:19:49 +0000316 return;
317 }
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000318 insert(new MCLEBFragment(*Value, false));
Rafael Espindola5e874982010-11-02 17:22:24 +0000319}
320
Fangrui Song0bc77a02020-02-13 13:26:21 -0800321void MCObjectStreamer::emitSLEB128Value(const MCExpr *Value) {
Rafael Espindola675fbb22010-12-03 01:19:49 +0000322 int64_t IntValue;
Nirav Dave6c0665e2018-04-30 19:22:40 +0000323 if (Value->evaluateAsAbsolute(IntValue, getAssemblerPtr())) {
Fangrui Song0bc77a02020-02-13 13:26:21 -0800324 emitSLEB128IntValue(IntValue);
Rafael Espindola675fbb22010-12-03 01:19:49 +0000325 return;
326 }
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000327 insert(new MCLEBFragment(*Value, true));
Rafael Espindola5e874982010-11-02 17:22:24 +0000328}
329
Rafael Espindola16145972010-11-01 14:28:48 +0000330void MCObjectStreamer::EmitWeakReference(MCSymbol *Alias,
331 const MCSymbol *Symbol) {
332 report_fatal_error("This file format doesn't support weak aliases.");
333}
334
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000335void MCObjectStreamer::ChangeSection(MCSection *Section,
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000336 const MCExpr *Subsection) {
Rafael Espindola36a15cb2015-03-20 20:00:01 +0000337 changeSectionImpl(Section, Subsection);
338}
339
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000340bool MCObjectStreamer::changeSectionImpl(MCSection *Section,
Rafael Espindola36a15cb2015-03-20 20:00:01 +0000341 const MCExpr *Subsection) {
Daniel Dunbarb2347fe2010-06-16 20:04:25 +0000342 assert(Section && "Cannot switch to a null section!");
David Blaikie6c5bbae2017-03-16 00:52:18 +0000343 getContext().clearDwarfLocSeen();
Daniel Dunbarb2347fe2010-06-16 20:04:25 +0000344
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000345 bool Created = getAssembler().registerSection(*Section);
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000346
347 int64_t IntSubsection = 0;
348 if (Subsection &&
Nirav Dave6c0665e2018-04-30 19:22:40 +0000349 !Subsection->evaluateAsAbsolute(IntSubsection, getAssemblerPtr()))
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000350 report_fatal_error("Cannot evaluate subsection number");
351 if (IntSubsection < 0 || IntSubsection > 8192)
352 report_fatal_error("Subsection number out of range");
Michael Trent6f95d332019-12-11 10:42:37 -0800353 CurSubsectionIdx = unsigned(IntSubsection);
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000354 CurInsertionPoint =
Michael Trent6f95d332019-12-11 10:42:37 -0800355 Section->getSubsectionInsertionPoint(CurSubsectionIdx);
Rafael Espindola36a15cb2015-03-20 20:00:01 +0000356 return Created;
Daniel Dunbarb2347fe2010-06-16 20:04:25 +0000357}
358
Eli Benderskyea2824d2012-12-07 17:42:41 +0000359void MCObjectStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000360 getAssembler().registerSymbol(*Symbol);
Zoran Jovanovic28221d82014-03-20 09:44:49 +0000361 MCStreamer::EmitAssignment(Symbol, Value);
Eli Benderskyea2824d2012-12-07 17:42:41 +0000362}
363
Rafael Espindolacd625182015-05-25 18:34:26 +0000364bool MCObjectStreamer::mayHaveInstructions(MCSection &Sec) const {
365 return Sec.hasInstructions();
366}
367
Fangrui Songbcd24b22020-02-13 21:58:16 -0800368void MCObjectStreamer::emitInstruction(const MCInst &Inst,
Andrea Di Biagioedbf06a2019-02-04 12:51:26 +0000369 const MCSubtargetInfo &STI) {
Philip Reames14fc20c2019-12-20 10:51:05 -0800370 getAssembler().getBackend().alignBranchesBegin(*this, Inst);
Fangrui Songbcd24b22020-02-13 21:58:16 -0800371 emitInstructionImpl(Inst, STI);
Philip Reames14fc20c2019-12-20 10:51:05 -0800372 getAssembler().getBackend().alignBranchesEnd(*this, Inst);
373}
374
Fangrui Songbcd24b22020-02-13 21:58:16 -0800375void MCObjectStreamer::emitInstructionImpl(const MCInst &Inst,
Philip Reames14fc20c2019-12-20 10:51:05 -0800376 const MCSubtargetInfo &STI) {
Fangrui Songbcd24b22020-02-13 21:58:16 -0800377 MCStreamer::emitInstruction(Inst, STI);
Rafael Espindola72b54882010-11-01 16:27:31 +0000378
Rafael Espindola983bec62015-05-27 21:04:14 +0000379 MCSection *Sec = getCurrentSectionOnly();
Rafael Espindola3d2aeb22015-05-26 14:48:11 +0000380 Sec->setHasInstructions(true);
Rafael Espindola72b54882010-11-01 16:27:31 +0000381
382 // Now that a machine instruction has been assembled into this section, make
383 // a line entry for any .loc directive that has been seen.
Eric Christopher445c9522016-10-14 05:47:37 +0000384 MCDwarfLineEntry::Make(this, getCurrentSectionOnly());
Rafael Espindola72b54882010-11-01 16:27:31 +0000385
386 // If this instruction doesn't need relaxation, just emit it as data.
Eli Benderskyf483ff92012-12-20 19:05:53 +0000387 MCAssembler &Assembler = getAssembler();
Peter Smith57f661b2018-06-06 09:40:06 +0000388 if (!Assembler.getBackend().mayNeedRelaxation(Inst, STI)) {
David Woodhouse6f3c73f2014-01-28 23:12:49 +0000389 EmitInstToData(Inst, STI);
Rafael Espindola72b54882010-11-01 16:27:31 +0000390 return;
391 }
392
Eli Benderskyf483ff92012-12-20 19:05:53 +0000393 // Otherwise, relax and emit it as data if either:
394 // - The RelaxAll flag was passed
395 // - Bundling is enabled and this instruction is inside a bundle-locked
396 // group. We want to emit all such instructions into the same data
397 // fragment.
398 if (Assembler.getRelaxAll() ||
Rafael Espindola3d2aeb22015-05-26 14:48:11 +0000399 (Assembler.isBundlingEnabled() && Sec->isBundleLocked())) {
Rafael Espindola72b54882010-11-01 16:27:31 +0000400 MCInst Relaxed;
Nirav Dave86030622016-07-11 14:23:53 +0000401 getAssembler().getBackend().relaxInstruction(Inst, STI, Relaxed);
Peter Smith57f661b2018-06-06 09:40:06 +0000402 while (getAssembler().getBackend().mayNeedRelaxation(Relaxed, STI))
Nirav Dave86030622016-07-11 14:23:53 +0000403 getAssembler().getBackend().relaxInstruction(Relaxed, STI, Relaxed);
David Woodhouse6f3c73f2014-01-28 23:12:49 +0000404 EmitInstToData(Relaxed, STI);
Rafael Espindola72b54882010-11-01 16:27:31 +0000405 return;
406 }
407
408 // Otherwise emit to a separate fragment.
David Woodhouse6f3c73f2014-01-28 23:12:49 +0000409 EmitInstToFragment(Inst, STI);
Rafael Espindola72b54882010-11-01 16:27:31 +0000410}
411
David Woodhouse6f3c73f2014-01-28 23:12:49 +0000412void MCObjectStreamer::EmitInstToFragment(const MCInst &Inst,
413 const MCSubtargetInfo &STI) {
Petr Hosek9e0c8902015-04-12 23:42:25 +0000414 if (getAssembler().getRelaxAll() && getAssembler().isBundlingEnabled())
415 llvm_unreachable("All instructions should have already been relaxed");
416
Eli Benderskyf483ff92012-12-20 19:05:53 +0000417 // Always create a new, separate fragment here, because its size can change
418 // during relaxation.
David Woodhousef5199f62014-01-28 23:12:53 +0000419 MCRelaxableFragment *IF = new MCRelaxableFragment(Inst, STI);
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000420 insert(IF);
Rafael Espindola3fa6fc12010-12-02 05:44:06 +0000421
Eli Friedmanb2545fb2011-04-18 20:54:46 +0000422 SmallString<128> Code;
423 raw_svector_ostream VecOS(Code);
Jim Grosbach91df21f2015-05-15 19:13:16 +0000424 getAssembler().getEmitter().encodeInstruction(Inst, VecOS, IF->getFixups(),
David Woodhouse9784cef2014-01-28 23:13:07 +0000425 STI);
Eli Benderskya31a8942012-12-07 19:13:57 +0000426 IF->getContents().append(Code.begin(), Code.end());
Rafael Espindola3fa6fc12010-12-02 05:44:06 +0000427}
428
Matt Beaumont-Gay5ae72082013-02-15 23:12:33 +0000429#ifndef NDEBUG
Craig Topperd3a34f82013-07-16 01:17:10 +0000430static const char *const BundlingNotImplementedMsg =
Eli Benderskyf483ff92012-12-20 19:05:53 +0000431 "Aligned bundling is not implemented for this object format";
Matt Beaumont-Gay5ae72082013-02-15 23:12:33 +0000432#endif
Eli Benderskyf483ff92012-12-20 19:05:53 +0000433
434void MCObjectStreamer::EmitBundleAlignMode(unsigned AlignPow2) {
435 llvm_unreachable(BundlingNotImplementedMsg);
436}
437
Eli Bendersky802b6282013-01-07 21:51:08 +0000438void MCObjectStreamer::EmitBundleLock(bool AlignToEnd) {
Eli Benderskyf483ff92012-12-20 19:05:53 +0000439 llvm_unreachable(BundlingNotImplementedMsg);
440}
441
442void MCObjectStreamer::EmitBundleUnlock() {
443 llvm_unreachable(BundlingNotImplementedMsg);
444}
445
Fangrui Song0bc77a02020-02-13 13:26:21 -0800446void MCObjectStreamer::emitDwarfLocDirective(unsigned FileNo, unsigned Line,
Ulrich Weigand64f44052013-06-19 21:27:27 +0000447 unsigned Column, unsigned Flags,
448 unsigned Isa,
449 unsigned Discriminator,
450 StringRef FileName) {
451 // In case we see two .loc directives in a row, make sure the
452 // first one gets a line entry.
Eric Christopher445c9522016-10-14 05:47:37 +0000453 MCDwarfLineEntry::Make(this, getCurrentSectionOnly());
Ulrich Weigand64f44052013-06-19 21:27:27 +0000454
Fangrui Song0bc77a02020-02-13 13:26:21 -0800455 this->MCStreamer::emitDwarfLocDirective(FileNo, Line, Column, Flags, Isa,
456 Discriminator, FileName);
Ulrich Weigand64f44052013-06-19 21:27:27 +0000457}
458
Rafael Espindola38518082014-08-15 14:31:47 +0000459static const MCExpr *buildSymbolDiff(MCObjectStreamer &OS, const MCSymbol *A,
460 const MCSymbol *B) {
461 MCContext &Context = OS.getContext();
462 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000463 const MCExpr *ARef = MCSymbolRefExpr::create(A, Variant, Context);
464 const MCExpr *BRef = MCSymbolRefExpr::create(B, Variant, Context);
Rafael Espindola38518082014-08-15 14:31:47 +0000465 const MCExpr *AddrDelta =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000466 MCBinaryExpr::create(MCBinaryExpr::Sub, ARef, BRef, Context);
Rafael Espindola38518082014-08-15 14:31:47 +0000467 return AddrDelta;
468}
469
Frederic Rissa5ab8442015-08-07 15:14:08 +0000470static void emitDwarfSetLineAddr(MCObjectStreamer &OS,
471 MCDwarfLineTableParams Params,
472 int64_t LineDelta, const MCSymbol *Label,
473 int PointerSize) {
Rafael Espindola5e955fb2014-08-15 14:43:02 +0000474 // emit the sequence to set the address
475 OS.EmitIntValue(dwarf::DW_LNS_extended_op, 1);
Fangrui Song0bc77a02020-02-13 13:26:21 -0800476 OS.emitULEB128IntValue(PointerSize + 1);
Rafael Espindola5e955fb2014-08-15 14:43:02 +0000477 OS.EmitIntValue(dwarf::DW_LNE_set_address, 1);
478 OS.EmitSymbolValue(Label, PointerSize);
479
480 // emit the sequence for the LineDelta (from 1) and a zero address delta.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000481 MCDwarfLineAddr::Emit(&OS, Params, LineDelta, 0);
Rafael Espindola5e955fb2014-08-15 14:43:02 +0000482}
483
Fangrui Song0bc77a02020-02-13 13:26:21 -0800484void MCObjectStreamer::emitDwarfAdvanceLineAddr(int64_t LineDelta,
Rafael Espindola57ab7082010-12-03 00:55:40 +0000485 const MCSymbol *LastLabel,
Evan Chengc7ac6902011-07-14 05:43:07 +0000486 const MCSymbol *Label,
487 unsigned PointerSize) {
Rafael Espindola57ab7082010-12-03 00:55:40 +0000488 if (!LastLabel) {
Frederic Rissa5ab8442015-08-07 15:14:08 +0000489 emitDwarfSetLineAddr(*this, Assembler->getDWARFLinetableParams(), LineDelta,
490 Label, PointerSize);
Rafael Espindola57ab7082010-12-03 00:55:40 +0000491 return;
492 }
Rafael Espindola38518082014-08-15 14:31:47 +0000493 const MCExpr *AddrDelta = buildSymbolDiff(*this, Label, LastLabel);
Rafael Espindola57ab7082010-12-03 00:55:40 +0000494 int64_t Res;
Nirav Dave6c0665e2018-04-30 19:22:40 +0000495 if (AddrDelta->evaluateAsAbsolute(Res, getAssemblerPtr())) {
Frederic Rissa5ab8442015-08-07 15:14:08 +0000496 MCDwarfLineAddr::Emit(this, Assembler->getDWARFLinetableParams(), LineDelta,
497 Res);
Rafael Espindola57ab7082010-12-03 00:55:40 +0000498 return;
499 }
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000500 insert(new MCDwarfLineAddrFragment(LineDelta, *AddrDelta));
Rafael Espindola57ab7082010-12-03 00:55:40 +0000501}
502
Fangrui Song0bc77a02020-02-13 13:26:21 -0800503void MCObjectStreamer::emitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
Rafael Espindola736a35d2010-12-28 05:39:27 +0000504 const MCSymbol *Label) {
Rafael Espindola38518082014-08-15 14:31:47 +0000505 const MCExpr *AddrDelta = buildSymbolDiff(*this, Label, LastLabel);
Rafael Espindola736a35d2010-12-28 05:39:27 +0000506 int64_t Res;
Nirav Dave6c0665e2018-04-30 19:22:40 +0000507 if (AddrDelta->evaluateAsAbsolute(Res, getAssemblerPtr())) {
Rafael Espindola736a35d2010-12-28 05:39:27 +0000508 MCDwarfFrameEmitter::EmitAdvanceLoc(*this, Res);
509 return;
510 }
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000511 insert(new MCDwarfCallFrameFragment(*AddrDelta));
Rafael Espindola736a35d2010-12-28 05:39:27 +0000512}
513
Reid Kleckner2214ed82016-01-29 00:49:42 +0000514void MCObjectStreamer::EmitCVLocDirective(unsigned FunctionId, unsigned FileNo,
515 unsigned Line, unsigned Column,
516 bool PrologueEnd, bool IsStmt,
Reid Klecknera9f4cc92016-09-07 16:15:31 +0000517 StringRef FileName, SMLoc Loc) {
Reid Kleckner689f7732018-08-28 23:25:59 +0000518 // Validate the directive.
519 if (!checkCVLocSection(FunctionId, FileNo, Loc))
520 return;
521
522 // Emit a label at the current position and record it in the CodeViewContext.
523 MCSymbol *LineSym = getContext().createTempSymbol();
524 EmitLabel(LineSym);
525 getContext().getCVContext().recordCVLoc(getContext(), LineSym, FunctionId,
526 FileNo, Line, Column, PrologueEnd,
527 IsStmt);
Reid Kleckner2214ed82016-01-29 00:49:42 +0000528}
529
530void MCObjectStreamer::EmitCVLinetableDirective(unsigned FunctionId,
531 const MCSymbol *Begin,
532 const MCSymbol *End) {
533 getContext().getCVContext().emitLineTableForFunction(*this, FunctionId, Begin,
534 End);
535 this->MCStreamer::EmitCVLinetableDirective(FunctionId, Begin, End);
536}
537
David Majnemer6fcbd7e2016-01-29 19:24:12 +0000538void MCObjectStreamer::EmitCVInlineLinetableDirective(
539 unsigned PrimaryFunctionId, unsigned SourceFileId, unsigned SourceLineNum,
Reid Klecknera9f4cc92016-09-07 16:15:31 +0000540 const MCSymbol *FnStartSym, const MCSymbol *FnEndSym) {
David Majnemer6fcbd7e2016-01-29 19:24:12 +0000541 getContext().getCVContext().emitInlineLineTableForFunction(
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000542 *this, PrimaryFunctionId, SourceFileId, SourceLineNum, FnStartSym,
Reid Klecknera9f4cc92016-09-07 16:15:31 +0000543 FnEndSym);
David Majnemer6fcbd7e2016-01-29 19:24:12 +0000544 this->MCStreamer::EmitCVInlineLinetableDirective(
Reid Klecknera9f4cc92016-09-07 16:15:31 +0000545 PrimaryFunctionId, SourceFileId, SourceLineNum, FnStartSym, FnEndSym);
David Majnemer6fcbd7e2016-01-29 19:24:12 +0000546}
547
David Majnemer408b5e62016-02-05 01:55:49 +0000548void MCObjectStreamer::EmitCVDefRangeDirective(
549 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
550 StringRef FixedSizePortion) {
Reid Kleckner94ee0722018-12-17 21:49:35 +0000551 MCFragment *Frag =
552 getContext().getCVContext().emitDefRange(*this, Ranges, FixedSizePortion);
553 // Attach labels that were pending before we created the defrange fragment to
554 // the beginning of the new fragment.
555 flushPendingLabels(Frag, 0);
David Majnemer408b5e62016-02-05 01:55:49 +0000556 this->MCStreamer::EmitCVDefRangeDirective(Ranges, FixedSizePortion);
557}
558
Reid Kleckner2214ed82016-01-29 00:49:42 +0000559void MCObjectStreamer::EmitCVStringTableDirective() {
560 getContext().getCVContext().emitStringTable(*this);
561}
562void MCObjectStreamer::EmitCVFileChecksumsDirective() {
563 getContext().getCVContext().emitFileChecksums(*this);
564}
565
Reid Kleckner26fa1bf2017-09-19 18:14:45 +0000566void MCObjectStreamer::EmitCVFileChecksumOffsetDirective(unsigned FileNo) {
567 getContext().getCVContext().emitFileChecksumOffset(*this, FileNo);
568}
Reid Kleckner2214ed82016-01-29 00:49:42 +0000569
Rafael Espindola64e1af82013-07-02 15:49:13 +0000570void MCObjectStreamer::EmitBytes(StringRef Data) {
Eric Christopher445c9522016-10-14 05:47:37 +0000571 MCDwarfLineEntry::Make(this, getCurrentSectionOnly());
Petr Hosek32946702015-06-27 01:54:17 +0000572 MCDataFragment *DF = getOrCreateDataFragment();
573 flushPendingLabels(DF, DF->getContents().size());
574 DF->getContents().append(Data.begin(), Data.end());
Benjamin Kramer64ddcb02012-10-04 13:12:43 +0000575}
576
577void MCObjectStreamer::EmitValueToAlignment(unsigned ByteAlignment,
578 int64_t Value,
579 unsigned ValueSize,
580 unsigned MaxBytesToEmit) {
581 if (MaxBytesToEmit == 0)
582 MaxBytesToEmit = ByteAlignment;
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000583 insert(new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit));
Benjamin Kramer64ddcb02012-10-04 13:12:43 +0000584
585 // Update the maximum alignment on the current section if necessary.
Eric Christopher445c9522016-10-14 05:47:37 +0000586 MCSection *CurSec = getCurrentSectionOnly();
Rafael Espindola967d6a62015-05-21 21:02:35 +0000587 if (ByteAlignment > CurSec->getAlignment())
Guillaume Chatelet18f805a2019-09-27 12:54:21 +0000588 CurSec->setAlignment(Align(ByteAlignment));
Benjamin Kramer64ddcb02012-10-04 13:12:43 +0000589}
590
591void MCObjectStreamer::EmitCodeAlignment(unsigned ByteAlignment,
592 unsigned MaxBytesToEmit) {
593 EmitValueToAlignment(ByteAlignment, 0, 1, MaxBytesToEmit);
594 cast<MCAlignFragment>(getCurrentFragment())->setEmitNops(true);
595}
596
Rafael Espindola7ae65d82015-11-04 23:59:18 +0000597void MCObjectStreamer::emitValueToOffset(const MCExpr *Offset,
Oliver Stannard268f42f2016-12-14 10:43:58 +0000598 unsigned char Value,
599 SMLoc Loc) {
600 insert(new MCOrgFragment(*Offset, Value, Loc));
Rafael Espindola9065bfc62010-12-02 05:59:38 +0000601}
602
Simon Atanasyaneb9ed612016-08-22 16:18:42 +0000603// Associate DTPRel32 fixup with data and resize data area
604void MCObjectStreamer::EmitDTPRel32Value(const MCExpr *Value) {
605 MCDataFragment *DF = getOrCreateDataFragment();
606 flushPendingLabels(DF, DF->getContents().size());
607
608 DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
609 Value, FK_DTPRel_4));
610 DF->getContents().resize(DF->getContents().size() + 4, 0);
611}
612
613// Associate DTPRel64 fixup with data and resize data area
614void MCObjectStreamer::EmitDTPRel64Value(const MCExpr *Value) {
615 MCDataFragment *DF = getOrCreateDataFragment();
616 flushPendingLabels(DF, DF->getContents().size());
617
618 DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
619 Value, FK_DTPRel_8));
620 DF->getContents().resize(DF->getContents().size() + 8, 0);
621}
622
623// Associate TPRel32 fixup with data and resize data area
624void MCObjectStreamer::EmitTPRel32Value(const MCExpr *Value) {
625 MCDataFragment *DF = getOrCreateDataFragment();
626 flushPendingLabels(DF, DF->getContents().size());
627
628 DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
629 Value, FK_TPRel_4));
630 DF->getContents().resize(DF->getContents().size() + 4, 0);
631}
632
633// Associate TPRel64 fixup with data and resize data area
634void MCObjectStreamer::EmitTPRel64Value(const MCExpr *Value) {
635 MCDataFragment *DF = getOrCreateDataFragment();
636 flushPendingLabels(DF, DF->getContents().size());
637
638 DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
639 Value, FK_TPRel_8));
640 DF->getContents().resize(DF->getContents().size() + 8, 0);
641}
642
Akira Hatanakaf5ddf132011-11-23 22:18:04 +0000643// Associate GPRel32 fixup with data and resize data area
644void MCObjectStreamer::EmitGPRel32Value(const MCExpr *Value) {
645 MCDataFragment *DF = getOrCreateDataFragment();
Petr Hosek32946702015-06-27 01:54:17 +0000646 flushPendingLabels(DF, DF->getContents().size());
Akira Hatanakaf5ddf132011-11-23 22:18:04 +0000647
Weiming Zhao74a7fa02017-04-03 21:50:04 +0000648 DF->getFixups().push_back(
649 MCFixup::create(DF->getContents().size(), Value, FK_GPRel_4));
Akira Hatanakaf5ddf132011-11-23 22:18:04 +0000650 DF->getContents().resize(DF->getContents().size() + 4, 0);
651}
652
Simon Atanasyaneb9ed612016-08-22 16:18:42 +0000653// Associate GPRel64 fixup with data and resize data area
Jack Carter77064c02012-08-22 00:49:30 +0000654void MCObjectStreamer::EmitGPRel64Value(const MCExpr *Value) {
655 MCDataFragment *DF = getOrCreateDataFragment();
Petr Hosek32946702015-06-27 01:54:17 +0000656 flushPendingLabels(DF, DF->getContents().size());
Jack Carter77064c02012-08-22 00:49:30 +0000657
Weiming Zhao74a7fa02017-04-03 21:50:04 +0000658 DF->getFixups().push_back(
659 MCFixup::create(DF->getContents().size(), Value, FK_GPRel_4));
Jack Carter77064c02012-08-22 00:49:30 +0000660 DF->getContents().resize(DF->getContents().size() + 8, 0);
661}
662
Daniel Sanders9f6ad492015-11-12 13:33:00 +0000663bool MCObjectStreamer::EmitRelocDirective(const MCExpr &Offset, StringRef Name,
Peter Smith57f661b2018-06-06 09:40:06 +0000664 const MCExpr *Expr, SMLoc Loc,
665 const MCSubtargetInfo &STI) {
David Majnemerce108422016-01-19 23:05:27 +0000666 Optional<MCFixupKind> MaybeKind = Assembler->getBackend().getFixupKind(Name);
667 if (!MaybeKind.hasValue())
Daniel Sanders9f6ad492015-11-12 13:33:00 +0000668 return true;
669
David Majnemerce108422016-01-19 23:05:27 +0000670 MCFixupKind Kind = *MaybeKind;
671
Daniel Sanders9f6ad492015-11-12 13:33:00 +0000672 if (Expr == nullptr)
673 Expr =
674 MCSymbolRefExpr::create(getContext().createTempSymbol(), getContext());
Vladimir Stefanovic1d2714b2018-11-21 16:28:39 +0000675
676 MCDataFragment *DF = getOrCreateDataFragment(&STI);
677 flushPendingLabels(DF, DF->getContents().size());
678
679 int64_t OffsetValue;
680 if (Offset.evaluateAsAbsolute(OffsetValue)) {
681 if (OffsetValue < 0)
682 llvm_unreachable(".reloc offset is negative");
683 DF->getFixups().push_back(MCFixup::create(OffsetValue, Expr, Kind, Loc));
684 return false;
685 }
686
687 if (Offset.getKind() != llvm::MCExpr::SymbolRef)
688 llvm_unreachable(".reloc offset is not absolute nor a label");
689
690 const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(Offset);
691 if (SRE.getSymbol().isDefined()) {
692 DF->getFixups().push_back(MCFixup::create(SRE.getSymbol().getOffset(),
693 Expr, Kind, Loc));
694 return false;
695 }
696
697 PendingFixups.emplace_back(&SRE.getSymbol(), DF,
698 MCFixup::create(-1, Expr, Kind, Loc));
Daniel Sanders9f6ad492015-11-12 13:33:00 +0000699 return false;
700}
701
Petr Hosek67a94a72016-05-28 05:57:48 +0000702void MCObjectStreamer::emitFill(const MCExpr &NumBytes, uint64_t FillValue,
703 SMLoc Loc) {
704 MCDataFragment *DF = getOrCreateDataFragment();
705 flushPendingLabels(DF, DF->getContents().size());
706
Rafael Espindola8fcd07d2018-01-09 19:29:33 +0000707 assert(getCurrentSectionOnly() && "need a section");
Nirav Dave588fad42018-05-18 17:45:48 +0000708 insert(new MCFillFragment(FillValue, 1, NumBytes, Loc));
Petr Hosek67a94a72016-05-28 05:57:48 +0000709}
710
711void MCObjectStreamer::emitFill(const MCExpr &NumValues, int64_t Size,
712 int64_t Expr, SMLoc Loc) {
713 int64_t IntNumValues;
Nirav Dave588fad42018-05-18 17:45:48 +0000714 // Do additional checking now if we can resolve the value.
715 if (NumValues.evaluateAsAbsolute(IntNumValues, getAssemblerPtr())) {
716 if (IntNumValues < 0) {
717 getContext().getSourceManager()->PrintMessage(
718 Loc, SourceMgr::DK_Warning,
719 "'.fill' directive with negative repeat count has no effect");
720 return;
721 }
722 // Emit now if we can for better errors.
723 int64_t NonZeroSize = Size > 4 ? 4 : Size;
724 Expr &= ~0ULL >> (64 - NonZeroSize * 8);
725 for (uint64_t i = 0, e = IntNumValues; i != e; ++i) {
726 EmitIntValue(Expr, NonZeroSize);
727 if (NonZeroSize < Size)
728 EmitIntValue(0, Size - NonZeroSize);
729 }
Petr Hosek67a94a72016-05-28 05:57:48 +0000730 return;
731 }
732
Nirav Dave588fad42018-05-18 17:45:48 +0000733 // Otherwise emit as fragment.
734 MCDataFragment *DF = getOrCreateDataFragment();
735 flushPendingLabels(DF, DF->getContents().size());
Petr Hosek67a94a72016-05-28 05:57:48 +0000736
Nirav Dave588fad42018-05-18 17:45:48 +0000737 assert(getCurrentSectionOnly() && "need a section");
738 insert(new MCFillFragment(Expr, Size, NumValues, Loc));
Petr Hosek67a94a72016-05-28 05:57:48 +0000739}
740
Peter Collingbourne5b75fd92017-03-03 21:22:06 +0000741void MCObjectStreamer::EmitFileDirective(StringRef Filename) {
742 getAssembler().addFileName(Filename);
743}
744
Peter Collingbourne3e227332018-07-17 22:17:18 +0000745void MCObjectStreamer::EmitAddrsig() {
746 getAssembler().getWriter().emitAddrsigSection();
747}
748
749void MCObjectStreamer::EmitAddrsigSym(const MCSymbol *Sym) {
750 getAssembler().registerSymbol(*Sym);
751 getAssembler().getWriter().addAddrsigSymbol(Sym);
752}
753
Rafael Espindola07082092012-01-07 03:13:18 +0000754void MCObjectStreamer::FinishImpl() {
Jonas Devlieghere26ddf272018-07-11 12:30:35 +0000755 getContext().RemapDebugPaths();
Paul Robinsonc17c8bf2018-07-10 14:41:54 +0000756
Kevin Enderbye7739d42011-12-09 18:09:40 +0000757 // If we are generating dwarf for assembly source files dump out the sections.
758 if (getContext().getGenDwarfForAssembly())
David Blaikie8bf66c42014-04-01 07:35:52 +0000759 MCGenDwarfInfo::Emit(this);
760
761 // Dump out the dwarf file & directory tables and line tables.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000762 MCDwarfLineTable::Emit(this, getAssembler().getDWARFLinetableParams());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000763
Michael Trent6f95d332019-12-11 10:42:37 -0800764 // Update any remaining pending labels with empty data fragments.
Jonas Devliegheree13e6db2018-07-10 15:32:17 +0000765 flushPendingLabels();
Michael Trent6f95d332019-12-11 10:42:37 -0800766
Vladimir Stefanovic1d2714b2018-11-21 16:28:39 +0000767 resolvePendingFixups();
Daniel Dunbarb2347fe2010-06-16 20:04:25 +0000768 getAssembler().Finish();
769}