blob: 5c42667f99108b1c5c0d0d8fada2253d74ffdcae [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))),
Rafael Espindola2f9bdd82015-05-27 20:52:32 +000032 EmitEHFrame(true), EmitDebugFrame(false) {}
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +000033
Lang Hames2241ffa2017-10-11 23:34:47 +000034MCObjectStreamer::~MCObjectStreamer() {}
Daniel Dunbarb2347fe2010-06-16 20:04:25 +000035
Nirav Dave6c0665e2018-04-30 19:22:40 +000036// AssemblerPtr is used for evaluation of expressions and causes
37// difference between asm and object outputs. Return nullptr to in
38// inline asm mode to limit divergence to assembly inputs.
39MCAssembler *MCObjectStreamer::getAssemblerPtr() {
40 if (getUseAssemblerInfoForParsing())
41 return Assembler.get();
42 return nullptr;
43}
44
Michael Trent6f95d332019-12-11 10:42:37 -080045void MCObjectStreamer::addPendingLabel(MCSymbol* S) {
46 MCSection *CurSection = getCurrentSectionOnly();
47 if (CurSection) {
48 // Register labels that have not yet been assigned to a Section.
49 if (!PendingLabels.empty()) {
50 for (MCSymbol* Sym : PendingLabels)
51 CurSection->addPendingLabel(Sym);
52 PendingLabels.clear();
53 }
54
55 // Add this label to the current Section / Subsection.
56 CurSection->addPendingLabel(S, CurSubsectionIdx);
57
58 // Add this Section to the list of PendingLabelSections.
59 auto SecIt = std::find(PendingLabelSections.begin(),
60 PendingLabelSections.end(), CurSection);
61 if (SecIt == PendingLabelSections.end())
62 PendingLabelSections.push_back(CurSection);
63 }
64 else
65 // There is no Section / Subsection for this label yet.
66 PendingLabels.push_back(S);
67}
68
Petr Hosek9e0c8902015-04-12 23:42:25 +000069void MCObjectStreamer::flushPendingLabels(MCFragment *F, uint64_t FOffset) {
Michael Trent6f95d332019-12-11 10:42:37 -080070 MCSection *CurSection = getCurrentSectionOnly();
71 if (!CurSection) {
72 assert(PendingLabels.empty());
Rafael Espindola81413c02015-10-03 00:57:12 +000073 return;
Michael Trent6f95d332019-12-11 10:42:37 -080074 }
75 // Register labels that have not yet been assigned to a Section.
76 if (!PendingLabels.empty()) {
77 for (MCSymbol* Sym : PendingLabels)
78 CurSection->addPendingLabel(Sym, CurSubsectionIdx);
79 PendingLabels.clear();
80 }
81
82 // Associate a fragment with this label, either the supplied fragment
83 // or an empty data fragment.
84 if (F)
85 CurSection->flushPendingLabels(F, FOffset, CurSubsectionIdx);
86 else
87 CurSection->flushPendingLabels(nullptr, 0, CurSubsectionIdx);
88}
89
90void MCObjectStreamer::flushPendingLabels() {
91 // Register labels that have not yet been assigned to a Section.
92 if (!PendingLabels.empty()) {
Rafael Espindola81413c02015-10-03 00:57:12 +000093 MCSection *CurSection = getCurrentSectionOnly();
Michael Trent6f95d332019-12-11 10:42:37 -080094 assert(CurSection);
95 for (MCSymbol* Sym : PendingLabels)
96 CurSection->addPendingLabel(Sym, CurSubsectionIdx);
97 PendingLabels.clear();
Derek Schuff5f708e52014-10-22 22:38:06 +000098 }
Michael Trent6f95d332019-12-11 10:42:37 -080099
100 // Assign an empty data fragment to all remaining pending labels.
101 for (MCSection* Section : PendingLabelSections)
102 Section->flushPendingLabels();
Derek Schuff5f708e52014-10-22 22:38:06 +0000103}
104
Vladimir Stefanovic1d2714b2018-11-21 16:28:39 +0000105// When fixup's offset is a forward declared label, e.g.:
106//
107// .reloc 1f, R_MIPS_JALR, foo
108// 1: nop
109//
110// postpone adding it to Fixups vector until the label is defined and its offset
111// is known.
112void MCObjectStreamer::resolvePendingFixups() {
113 for (PendingMCFixup &PendingFixup : PendingFixups) {
114 if (!PendingFixup.Sym || PendingFixup.Sym->isUndefined ()) {
115 getContext().reportError(PendingFixup.Fixup.getLoc(),
116 "unresolved relocation offset");
117 continue;
118 }
119 flushPendingLabels(PendingFixup.DF, PendingFixup.DF->getContents().size());
120 PendingFixup.Fixup.setOffset(PendingFixup.Sym->getOffset());
121 PendingFixup.DF->getFixups().push_back(PendingFixup.Fixup);
122 }
123 PendingFixups.clear();
124}
125
Rafael Espindolad09b4162018-02-09 17:00:25 +0000126// As a compile-time optimization, avoid allocating and evaluating an MCExpr
127// tree for (Hi - Lo) when Hi and Lo are offsets into the same fragment.
Alex Bradburyfdc46472018-08-16 11:26:37 +0000128static Optional<uint64_t>
129absoluteSymbolDiff(MCAssembler &Asm, const MCSymbol *Hi, const MCSymbol *Lo) {
Reid Kleckner5a791ee2018-03-15 21:24:04 +0000130 assert(Hi && Lo);
Alex Bradburyfdc46472018-08-16 11:26:37 +0000131 if (Asm.getBackendPtr()->requiresDiffExpressionRelocations())
132 return None;
133
Rafael Espindolad09b4162018-02-09 17:00:25 +0000134 if (!Hi->getFragment() || Hi->getFragment() != Lo->getFragment() ||
135 Hi->isVariable() || Lo->isVariable())
136 return None;
137
138 return Hi->getOffset() - Lo->getOffset();
139}
140
Rafael Espindola7c6e6e42015-06-11 18:58:08 +0000141void MCObjectStreamer::emitAbsoluteSymbolDiff(const MCSymbol *Hi,
Duncan P. N. Exon Smith0b73d712015-05-21 02:41:23 +0000142 const MCSymbol *Lo,
143 unsigned Size) {
Alex Bradburyfdc46472018-08-16 11:26:37 +0000144 if (Optional<uint64_t> Diff = absoluteSymbolDiff(getAssembler(), Hi, Lo)) {
Rafael Espindolad09b4162018-02-09 17:00:25 +0000145 EmitIntValue(*Diff, Size);
Rafael Espindola7c6e6e42015-06-11 18:58:08 +0000146 return;
147 }
Rafael Espindolad09b4162018-02-09 17:00:25 +0000148 MCStreamer::emitAbsoluteSymbolDiff(Hi, Lo, Size);
149}
Duncan P. N. Exon Smith0b73d712015-05-21 02:41:23 +0000150
Rafael Espindolad09b4162018-02-09 17:00:25 +0000151void MCObjectStreamer::emitAbsoluteSymbolDiffAsULEB128(const MCSymbol *Hi,
152 const MCSymbol *Lo) {
Alex Bradburyfdc46472018-08-16 11:26:37 +0000153 if (Optional<uint64_t> Diff = absoluteSymbolDiff(getAssembler(), Hi, Lo)) {
Rafael Espindolad09b4162018-02-09 17:00:25 +0000154 EmitULEB128IntValue(*Diff);
155 return;
156 }
157 MCStreamer::emitAbsoluteSymbolDiffAsULEB128(Hi, Lo);
Duncan P. N. Exon Smith0b73d712015-05-21 02:41:23 +0000158}
159
Pedro Artigas7212ee42012-12-12 22:59:46 +0000160void MCObjectStreamer::reset() {
Pedro Artigasb95c53e2012-12-14 18:52:11 +0000161 if (Assembler)
162 Assembler->reset();
Rafael Espindolaa32d0e92015-05-27 15:14:11 +0000163 CurInsertionPoint = MCSection::iterator();
Rafael Espindola3dd8ef62014-05-12 14:02:44 +0000164 EmitEHFrame = true;
165 EmitDebugFrame = false;
Derek Schuff5f708e52014-10-22 22:38:06 +0000166 PendingLabels.clear();
Michael Trent6f95d332019-12-11 10:42:37 -0800167 PendingLabelSections.clear();
Pedro Artigas7212ee42012-12-12 22:59:46 +0000168 MCStreamer::reset();
169}
170
Rafael Espindola3dd8ef62014-05-12 14:02:44 +0000171void MCObjectStreamer::EmitFrames(MCAsmBackend *MAB) {
172 if (!getNumFrameInfos())
173 return;
174
175 if (EmitEHFrame)
176 MCDwarfFrameEmitter::Emit(*this, MAB, true);
177
178 if (EmitDebugFrame)
179 MCDwarfFrameEmitter::Emit(*this, MAB, false);
180}
181
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000182MCFragment *MCObjectStreamer::getCurrentFragment() const {
Rafael Espindola983bec62015-05-27 21:04:14 +0000183 assert(getCurrentSectionOnly() && "No current section!");
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000184
Rafael Espindola983bec62015-05-27 21:04:14 +0000185 if (CurInsertionPoint != getCurrentSectionOnly()->getFragmentList().begin())
Duncan P. N. Exon Smitha5f45da2015-10-10 00:13:11 +0000186 return &*std::prev(CurInsertionPoint);
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000187
Craig Topperbb694de2014-04-13 04:57:38 +0000188 return nullptr;
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000189}
190
Peter Smith57f661b2018-06-06 09:40:06 +0000191static bool CanReuseDataFragment(const MCDataFragment &F,
192 const MCAssembler &Assembler,
193 const MCSubtargetInfo *STI) {
194 if (!F.hasInstructions())
195 return true;
Derek Schuff8878bcc92013-02-15 22:50:52 +0000196 // When bundling is enabled, we don't want to add data to a fragment that
197 // already has instructions (see MCELFStreamer::EmitInstToData for details)
Peter Smith57f661b2018-06-06 09:40:06 +0000198 if (Assembler.isBundlingEnabled())
199 return Assembler.getRelaxAll();
200 // If the subtarget is changed mid fragment we start a new fragment to record
201 // the new STI.
202 return !STI || F.getSubtargetInfo() == STI;
203}
204
205MCDataFragment *
206MCObjectStreamer::getOrCreateDataFragment(const MCSubtargetInfo *STI) {
207 MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
208 if (!F || !CanReuseDataFragment(*F, *Assembler, STI)) {
David Blaikie4d3b0432014-04-10 21:53:47 +0000209 F = new MCDataFragment();
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000210 insert(F);
211 }
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000212 return F;
213}
214
Rafael Espindola2be12812014-06-25 15:29:54 +0000215void MCObjectStreamer::visitUsedSymbol(const MCSymbol &Sym) {
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000216 Assembler->registerSymbol(Sym);
Rafael Espindola2be12812014-06-25 15:29:54 +0000217}
218
Rafael Espindola3dd8ef62014-05-12 14:02:44 +0000219void MCObjectStreamer::EmitCFISections(bool EH, bool Debug) {
220 MCStreamer::EmitCFISections(EH, Debug);
221 EmitEHFrame = EH;
222 EmitDebugFrame = Debug;
223}
224
Kevin Enderby96918bc2014-04-22 17:27:29 +0000225void MCObjectStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
Craig Topper3c76c522015-09-20 23:35:59 +0000226 SMLoc Loc) {
Rafael Espindola591c6412014-06-25 18:37:33 +0000227 MCStreamer::EmitValueImpl(Value, Size, Loc);
Rafael Espindolaa084fd62010-11-28 23:08:47 +0000228 MCDataFragment *DF = getOrCreateDataFragment();
Petr Hosek32946702015-06-27 01:54:17 +0000229 flushPendingLabels(DF, DF->getContents().size());
Rafael Espindolaa084fd62010-11-28 23:08:47 +0000230
Eric Christopher445c9522016-10-14 05:47:37 +0000231 MCDwarfLineEntry::Make(this, getCurrentSectionOnly());
Cameron Zwarich80cbcd22013-05-25 21:56:53 +0000232
Rafael Espindolaa084fd62010-11-28 23:08:47 +0000233 // Avoid fixups when possible.
234 int64_t AbsValue;
Nirav Dave6c0665e2018-04-30 19:22:40 +0000235 if (Value->evaluateAsAbsolute(AbsValue, getAssemblerPtr())) {
Arnaud A. de Grandmaison6d241792017-05-15 08:43:27 +0000236 if (!isUIntN(8 * Size, AbsValue) && !isIntN(8 * Size, AbsValue)) {
237 getContext().reportError(
238 Loc, "value evaluated as " + Twine(AbsValue) + " is out of range.");
239 return;
240 }
Rafael Espindola64e1af82013-07-02 15:49:13 +0000241 EmitIntValue(AbsValue, Size);
Rafael Espindola4c70eea2010-12-03 02:54:21 +0000242 return;
Rafael Espindolaa084fd62010-11-28 23:08:47 +0000243 }
Eli Benderskya31a8942012-12-07 19:13:57 +0000244 DF->getFixups().push_back(
Jim Grosbach63661f82015-05-15 19:13:05 +0000245 MCFixup::create(DF->getContents().size(), Value,
Kevin Enderby96918bc2014-04-22 17:27:29 +0000246 MCFixup::getKindForSize(Size, false), Loc));
Rafael Espindola4c70eea2010-12-03 02:54:21 +0000247 DF->getContents().resize(DF->getContents().size() + Size, 0);
Rafael Espindolaa084fd62010-11-28 23:08:47 +0000248}
249
Reid Klecknerab23dac2017-10-10 00:57:36 +0000250MCSymbol *MCObjectStreamer::EmitCFILabel() {
251 MCSymbol *Label = getContext().createTempSymbol("cfi", true);
252 EmitLabel(Label);
253 return Label;
254}
255
Oliver Stannardcf6bfb12014-11-03 12:19:03 +0000256void MCObjectStreamer::EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame) {
257 // We need to create a local symbol to avoid relocations.
Jim Grosbach6f482002015-05-18 18:43:14 +0000258 Frame.Begin = getContext().createTempSymbol();
Oliver Stannardcf6bfb12014-11-03 12:19:03 +0000259 EmitLabel(Frame.Begin);
Rafael Espindola38241202012-01-07 22:42:19 +0000260}
261
Rafael Espindolaf28213c2012-01-09 00:17:29 +0000262void MCObjectStreamer::EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000263 Frame.End = getContext().createTempSymbol();
Rafael Espindola49bbfd02014-06-25 00:13:59 +0000264 EmitLabel(Frame.End);
Rafael Espindolaf28213c2012-01-09 00:17:29 +0000265}
266
Rafael Espindolabe991572017-02-10 15:13:12 +0000267void MCObjectStreamer::EmitLabel(MCSymbol *Symbol, SMLoc Loc) {
268 MCStreamer::EmitLabel(Symbol, Loc);
Rafael Espindolae5b74152010-11-28 17:18:55 +0000269
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000270 getAssembler().registerSymbol(*Symbol);
Derek Schuff5f708e52014-10-22 22:38:06 +0000271
272 // If there is a current fragment, mark the symbol as pointing into it.
273 // Otherwise queue the label and set its fragment pointer when we emit the
274 // next fragment.
Petr Hosek9e0c8902015-04-12 23:42:25 +0000275 auto *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
276 if (F && !(getAssembler().isBundlingEnabled() &&
277 getAssembler().getRelaxAll())) {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000278 Symbol->setFragment(F);
Rafael Espindola14672502015-05-29 17:48:04 +0000279 Symbol->setOffset(F->getContents().size());
Derek Schuff5f708e52014-10-22 22:38:06 +0000280 } else {
James Y Knightbf142fc2019-11-10 16:12:29 -0500281 // Assign all pending labels to offset 0 within the dummy "pending"
282 // fragment. (They will all be reassigned to a real fragment in
283 // flushPendingLabels())
284 Symbol->setOffset(0);
Michael Trent6f95d332019-12-11 10:42:37 -0800285 addPendingLabel(Symbol);
Derek Schuff5f708e52014-10-22 22:38:06 +0000286 }
Rafael Espindolae5b74152010-11-28 17:18:55 +0000287}
288
James Y Knightbf142fc2019-11-10 16:12:29 -0500289// Emit a label at a previously emitted fragment/offset position. This must be
290// within the currently-active section.
291void MCObjectStreamer::EmitLabelAtPos(MCSymbol *Symbol, SMLoc Loc,
292 MCFragment *F, uint64_t Offset) {
293 assert(F->getParent() == getCurrentSectionOnly());
294
Weiming Zhao74a7fa02017-04-03 21:50:04 +0000295 MCStreamer::EmitLabel(Symbol, Loc);
296 getAssembler().registerSymbol(*Symbol);
297 auto *DF = dyn_cast_or_null<MCDataFragment>(F);
James Y Knightbf142fc2019-11-10 16:12:29 -0500298 Symbol->setOffset(Offset);
299 if (DF) {
Weiming Zhao74a7fa02017-04-03 21:50:04 +0000300 Symbol->setFragment(F);
James Y Knightbf142fc2019-11-10 16:12:29 -0500301 } else {
302 assert(isa<MCDummyFragment>(F) &&
303 "F must either be an MCDataFragment or the pending MCDummyFragment");
304 assert(Offset == 0);
Michael Trent6f95d332019-12-11 10:42:37 -0800305 addPendingLabel(Symbol);
James Y Knightbf142fc2019-11-10 16:12:29 -0500306 }
Weiming Zhao74a7fa02017-04-03 21:50:04 +0000307}
308
Rafael Espindola6aea5922011-04-21 23:39:26 +0000309void MCObjectStreamer::EmitULEB128Value(const MCExpr *Value) {
Rafael Espindola675fbb22010-12-03 01:19:49 +0000310 int64_t IntValue;
Nirav Dave6c0665e2018-04-30 19:22:40 +0000311 if (Value->evaluateAsAbsolute(IntValue, getAssemblerPtr())) {
Rafael Espindola6aea5922011-04-21 23:39:26 +0000312 EmitULEB128IntValue(IntValue);
Rafael Espindola675fbb22010-12-03 01:19:49 +0000313 return;
314 }
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000315 insert(new MCLEBFragment(*Value, false));
Rafael Espindola5e874982010-11-02 17:22:24 +0000316}
317
Rafael Espindola6aea5922011-04-21 23:39:26 +0000318void MCObjectStreamer::EmitSLEB128Value(const MCExpr *Value) {
Rafael Espindola675fbb22010-12-03 01:19:49 +0000319 int64_t IntValue;
Nirav Dave6c0665e2018-04-30 19:22:40 +0000320 if (Value->evaluateAsAbsolute(IntValue, getAssemblerPtr())) {
Rafael Espindola6aea5922011-04-21 23:39:26 +0000321 EmitSLEB128IntValue(IntValue);
Rafael Espindola675fbb22010-12-03 01:19:49 +0000322 return;
323 }
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000324 insert(new MCLEBFragment(*Value, true));
Rafael Espindola5e874982010-11-02 17:22:24 +0000325}
326
Rafael Espindola16145972010-11-01 14:28:48 +0000327void MCObjectStreamer::EmitWeakReference(MCSymbol *Alias,
328 const MCSymbol *Symbol) {
329 report_fatal_error("This file format doesn't support weak aliases.");
330}
331
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000332void MCObjectStreamer::ChangeSection(MCSection *Section,
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000333 const MCExpr *Subsection) {
Rafael Espindola36a15cb2015-03-20 20:00:01 +0000334 changeSectionImpl(Section, Subsection);
335}
336
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000337bool MCObjectStreamer::changeSectionImpl(MCSection *Section,
Rafael Espindola36a15cb2015-03-20 20:00:01 +0000338 const MCExpr *Subsection) {
Daniel Dunbarb2347fe2010-06-16 20:04:25 +0000339 assert(Section && "Cannot switch to a null section!");
David Blaikie6c5bbae2017-03-16 00:52:18 +0000340 getContext().clearDwarfLocSeen();
Daniel Dunbarb2347fe2010-06-16 20:04:25 +0000341
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000342 bool Created = getAssembler().registerSection(*Section);
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000343
344 int64_t IntSubsection = 0;
345 if (Subsection &&
Nirav Dave6c0665e2018-04-30 19:22:40 +0000346 !Subsection->evaluateAsAbsolute(IntSubsection, getAssemblerPtr()))
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000347 report_fatal_error("Cannot evaluate subsection number");
348 if (IntSubsection < 0 || IntSubsection > 8192)
349 report_fatal_error("Subsection number out of range");
Michael Trent6f95d332019-12-11 10:42:37 -0800350 CurSubsectionIdx = unsigned(IntSubsection);
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000351 CurInsertionPoint =
Michael Trent6f95d332019-12-11 10:42:37 -0800352 Section->getSubsectionInsertionPoint(CurSubsectionIdx);
Rafael Espindola36a15cb2015-03-20 20:00:01 +0000353 return Created;
Daniel Dunbarb2347fe2010-06-16 20:04:25 +0000354}
355
Eli Benderskyea2824d2012-12-07 17:42:41 +0000356void MCObjectStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000357 getAssembler().registerSymbol(*Symbol);
Zoran Jovanovic28221d82014-03-20 09:44:49 +0000358 MCStreamer::EmitAssignment(Symbol, Value);
Eli Benderskyea2824d2012-12-07 17:42:41 +0000359}
360
Rafael Espindolacd625182015-05-25 18:34:26 +0000361bool MCObjectStreamer::mayHaveInstructions(MCSection &Sec) const {
362 return Sec.hasInstructions();
363}
364
Rafael Espindola591c6412014-06-25 18:37:33 +0000365void MCObjectStreamer::EmitInstruction(const MCInst &Inst,
Andrea Di Biagioedbf06a2019-02-04 12:51:26 +0000366 const MCSubtargetInfo &STI) {
Philip Reames14fc20c2019-12-20 10:51:05 -0800367 getAssembler().getBackend().alignBranchesBegin(*this, Inst);
368 EmitInstructionImpl(Inst, STI);
369 getAssembler().getBackend().alignBranchesEnd(*this, Inst);
370}
371
372void MCObjectStreamer::EmitInstructionImpl(const MCInst &Inst,
373 const MCSubtargetInfo &STI) {
Rafael Espindola591c6412014-06-25 18:37:33 +0000374 MCStreamer::EmitInstruction(Inst, STI);
Rafael Espindola72b54882010-11-01 16:27:31 +0000375
Rafael Espindola983bec62015-05-27 21:04:14 +0000376 MCSection *Sec = getCurrentSectionOnly();
Rafael Espindola3d2aeb22015-05-26 14:48:11 +0000377 Sec->setHasInstructions(true);
Rafael Espindola72b54882010-11-01 16:27:31 +0000378
379 // Now that a machine instruction has been assembled into this section, make
380 // a line entry for any .loc directive that has been seen.
Eric Christopher445c9522016-10-14 05:47:37 +0000381 MCDwarfLineEntry::Make(this, getCurrentSectionOnly());
Rafael Espindola72b54882010-11-01 16:27:31 +0000382
383 // If this instruction doesn't need relaxation, just emit it as data.
Eli Benderskyf483ff92012-12-20 19:05:53 +0000384 MCAssembler &Assembler = getAssembler();
Peter Smith57f661b2018-06-06 09:40:06 +0000385 if (!Assembler.getBackend().mayNeedRelaxation(Inst, STI)) {
David Woodhouse6f3c73f2014-01-28 23:12:49 +0000386 EmitInstToData(Inst, STI);
Rafael Espindola72b54882010-11-01 16:27:31 +0000387 return;
388 }
389
Eli Benderskyf483ff92012-12-20 19:05:53 +0000390 // Otherwise, relax and emit it as data if either:
391 // - The RelaxAll flag was passed
392 // - Bundling is enabled and this instruction is inside a bundle-locked
393 // group. We want to emit all such instructions into the same data
394 // fragment.
395 if (Assembler.getRelaxAll() ||
Rafael Espindola3d2aeb22015-05-26 14:48:11 +0000396 (Assembler.isBundlingEnabled() && Sec->isBundleLocked())) {
Rafael Espindola72b54882010-11-01 16:27:31 +0000397 MCInst Relaxed;
Nirav Dave86030622016-07-11 14:23:53 +0000398 getAssembler().getBackend().relaxInstruction(Inst, STI, Relaxed);
Peter Smith57f661b2018-06-06 09:40:06 +0000399 while (getAssembler().getBackend().mayNeedRelaxation(Relaxed, STI))
Nirav Dave86030622016-07-11 14:23:53 +0000400 getAssembler().getBackend().relaxInstruction(Relaxed, STI, Relaxed);
David Woodhouse6f3c73f2014-01-28 23:12:49 +0000401 EmitInstToData(Relaxed, STI);
Rafael Espindola72b54882010-11-01 16:27:31 +0000402 return;
403 }
404
405 // Otherwise emit to a separate fragment.
David Woodhouse6f3c73f2014-01-28 23:12:49 +0000406 EmitInstToFragment(Inst, STI);
Rafael Espindola72b54882010-11-01 16:27:31 +0000407}
408
David Woodhouse6f3c73f2014-01-28 23:12:49 +0000409void MCObjectStreamer::EmitInstToFragment(const MCInst &Inst,
410 const MCSubtargetInfo &STI) {
Petr Hosek9e0c8902015-04-12 23:42:25 +0000411 if (getAssembler().getRelaxAll() && getAssembler().isBundlingEnabled())
412 llvm_unreachable("All instructions should have already been relaxed");
413
Eli Benderskyf483ff92012-12-20 19:05:53 +0000414 // Always create a new, separate fragment here, because its size can change
415 // during relaxation.
David Woodhousef5199f62014-01-28 23:12:53 +0000416 MCRelaxableFragment *IF = new MCRelaxableFragment(Inst, STI);
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000417 insert(IF);
Rafael Espindola3fa6fc12010-12-02 05:44:06 +0000418
Eli Friedmanb2545fb2011-04-18 20:54:46 +0000419 SmallString<128> Code;
420 raw_svector_ostream VecOS(Code);
Jim Grosbach91df21f2015-05-15 19:13:16 +0000421 getAssembler().getEmitter().encodeInstruction(Inst, VecOS, IF->getFixups(),
David Woodhouse9784cef2014-01-28 23:13:07 +0000422 STI);
Eli Benderskya31a8942012-12-07 19:13:57 +0000423 IF->getContents().append(Code.begin(), Code.end());
Rafael Espindola3fa6fc12010-12-02 05:44:06 +0000424}
425
Matt Beaumont-Gay5ae72082013-02-15 23:12:33 +0000426#ifndef NDEBUG
Craig Topperd3a34f82013-07-16 01:17:10 +0000427static const char *const BundlingNotImplementedMsg =
Eli Benderskyf483ff92012-12-20 19:05:53 +0000428 "Aligned bundling is not implemented for this object format";
Matt Beaumont-Gay5ae72082013-02-15 23:12:33 +0000429#endif
Eli Benderskyf483ff92012-12-20 19:05:53 +0000430
431void MCObjectStreamer::EmitBundleAlignMode(unsigned AlignPow2) {
432 llvm_unreachable(BundlingNotImplementedMsg);
433}
434
Eli Bendersky802b6282013-01-07 21:51:08 +0000435void MCObjectStreamer::EmitBundleLock(bool AlignToEnd) {
Eli Benderskyf483ff92012-12-20 19:05:53 +0000436 llvm_unreachable(BundlingNotImplementedMsg);
437}
438
439void MCObjectStreamer::EmitBundleUnlock() {
440 llvm_unreachable(BundlingNotImplementedMsg);
441}
442
Ulrich Weigand64f44052013-06-19 21:27:27 +0000443void MCObjectStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
444 unsigned Column, unsigned Flags,
445 unsigned Isa,
446 unsigned Discriminator,
447 StringRef FileName) {
448 // In case we see two .loc directives in a row, make sure the
449 // first one gets a line entry.
Eric Christopher445c9522016-10-14 05:47:37 +0000450 MCDwarfLineEntry::Make(this, getCurrentSectionOnly());
Ulrich Weigand64f44052013-06-19 21:27:27 +0000451
452 this->MCStreamer::EmitDwarfLocDirective(FileNo, Line, Column, Flags,
453 Isa, Discriminator, FileName);
454}
455
Rafael Espindola38518082014-08-15 14:31:47 +0000456static const MCExpr *buildSymbolDiff(MCObjectStreamer &OS, const MCSymbol *A,
457 const MCSymbol *B) {
458 MCContext &Context = OS.getContext();
459 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000460 const MCExpr *ARef = MCSymbolRefExpr::create(A, Variant, Context);
461 const MCExpr *BRef = MCSymbolRefExpr::create(B, Variant, Context);
Rafael Espindola38518082014-08-15 14:31:47 +0000462 const MCExpr *AddrDelta =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000463 MCBinaryExpr::create(MCBinaryExpr::Sub, ARef, BRef, Context);
Rafael Espindola38518082014-08-15 14:31:47 +0000464 return AddrDelta;
465}
466
Frederic Rissa5ab8442015-08-07 15:14:08 +0000467static void emitDwarfSetLineAddr(MCObjectStreamer &OS,
468 MCDwarfLineTableParams Params,
469 int64_t LineDelta, const MCSymbol *Label,
470 int PointerSize) {
Rafael Espindola5e955fb2014-08-15 14:43:02 +0000471 // emit the sequence to set the address
472 OS.EmitIntValue(dwarf::DW_LNS_extended_op, 1);
473 OS.EmitULEB128IntValue(PointerSize + 1);
474 OS.EmitIntValue(dwarf::DW_LNE_set_address, 1);
475 OS.EmitSymbolValue(Label, PointerSize);
476
477 // emit the sequence for the LineDelta (from 1) and a zero address delta.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000478 MCDwarfLineAddr::Emit(&OS, Params, LineDelta, 0);
Rafael Espindola5e955fb2014-08-15 14:43:02 +0000479}
480
Rafael Espindola57ab7082010-12-03 00:55:40 +0000481void MCObjectStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta,
482 const MCSymbol *LastLabel,
Evan Chengc7ac6902011-07-14 05:43:07 +0000483 const MCSymbol *Label,
484 unsigned PointerSize) {
Rafael Espindola57ab7082010-12-03 00:55:40 +0000485 if (!LastLabel) {
Frederic Rissa5ab8442015-08-07 15:14:08 +0000486 emitDwarfSetLineAddr(*this, Assembler->getDWARFLinetableParams(), LineDelta,
487 Label, PointerSize);
Rafael Espindola57ab7082010-12-03 00:55:40 +0000488 return;
489 }
Rafael Espindola38518082014-08-15 14:31:47 +0000490 const MCExpr *AddrDelta = buildSymbolDiff(*this, Label, LastLabel);
Rafael Espindola57ab7082010-12-03 00:55:40 +0000491 int64_t Res;
Nirav Dave6c0665e2018-04-30 19:22:40 +0000492 if (AddrDelta->evaluateAsAbsolute(Res, getAssemblerPtr())) {
Frederic Rissa5ab8442015-08-07 15:14:08 +0000493 MCDwarfLineAddr::Emit(this, Assembler->getDWARFLinetableParams(), LineDelta,
494 Res);
Rafael Espindola57ab7082010-12-03 00:55:40 +0000495 return;
496 }
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000497 insert(new MCDwarfLineAddrFragment(LineDelta, *AddrDelta));
Rafael Espindola57ab7082010-12-03 00:55:40 +0000498}
499
Rafael Espindola736a35d2010-12-28 05:39:27 +0000500void MCObjectStreamer::EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
501 const MCSymbol *Label) {
Rafael Espindola38518082014-08-15 14:31:47 +0000502 const MCExpr *AddrDelta = buildSymbolDiff(*this, Label, LastLabel);
Rafael Espindola736a35d2010-12-28 05:39:27 +0000503 int64_t Res;
Nirav Dave6c0665e2018-04-30 19:22:40 +0000504 if (AddrDelta->evaluateAsAbsolute(Res, getAssemblerPtr())) {
Rafael Espindola736a35d2010-12-28 05:39:27 +0000505 MCDwarfFrameEmitter::EmitAdvanceLoc(*this, Res);
506 return;
507 }
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000508 insert(new MCDwarfCallFrameFragment(*AddrDelta));
Rafael Espindola736a35d2010-12-28 05:39:27 +0000509}
510
Reid Kleckner2214ed82016-01-29 00:49:42 +0000511void MCObjectStreamer::EmitCVLocDirective(unsigned FunctionId, unsigned FileNo,
512 unsigned Line, unsigned Column,
513 bool PrologueEnd, bool IsStmt,
Reid Klecknera9f4cc92016-09-07 16:15:31 +0000514 StringRef FileName, SMLoc Loc) {
Reid Kleckner689f7732018-08-28 23:25:59 +0000515 // Validate the directive.
516 if (!checkCVLocSection(FunctionId, FileNo, Loc))
517 return;
518
519 // Emit a label at the current position and record it in the CodeViewContext.
520 MCSymbol *LineSym = getContext().createTempSymbol();
521 EmitLabel(LineSym);
522 getContext().getCVContext().recordCVLoc(getContext(), LineSym, FunctionId,
523 FileNo, Line, Column, PrologueEnd,
524 IsStmt);
Reid Kleckner2214ed82016-01-29 00:49:42 +0000525}
526
527void MCObjectStreamer::EmitCVLinetableDirective(unsigned FunctionId,
528 const MCSymbol *Begin,
529 const MCSymbol *End) {
530 getContext().getCVContext().emitLineTableForFunction(*this, FunctionId, Begin,
531 End);
532 this->MCStreamer::EmitCVLinetableDirective(FunctionId, Begin, End);
533}
534
David Majnemer6fcbd7e2016-01-29 19:24:12 +0000535void MCObjectStreamer::EmitCVInlineLinetableDirective(
536 unsigned PrimaryFunctionId, unsigned SourceFileId, unsigned SourceLineNum,
Reid Klecknera9f4cc92016-09-07 16:15:31 +0000537 const MCSymbol *FnStartSym, const MCSymbol *FnEndSym) {
David Majnemer6fcbd7e2016-01-29 19:24:12 +0000538 getContext().getCVContext().emitInlineLineTableForFunction(
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000539 *this, PrimaryFunctionId, SourceFileId, SourceLineNum, FnStartSym,
Reid Klecknera9f4cc92016-09-07 16:15:31 +0000540 FnEndSym);
David Majnemer6fcbd7e2016-01-29 19:24:12 +0000541 this->MCStreamer::EmitCVInlineLinetableDirective(
Reid Klecknera9f4cc92016-09-07 16:15:31 +0000542 PrimaryFunctionId, SourceFileId, SourceLineNum, FnStartSym, FnEndSym);
David Majnemer6fcbd7e2016-01-29 19:24:12 +0000543}
544
David Majnemer408b5e62016-02-05 01:55:49 +0000545void MCObjectStreamer::EmitCVDefRangeDirective(
546 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
547 StringRef FixedSizePortion) {
Reid Kleckner94ee0722018-12-17 21:49:35 +0000548 MCFragment *Frag =
549 getContext().getCVContext().emitDefRange(*this, Ranges, FixedSizePortion);
550 // Attach labels that were pending before we created the defrange fragment to
551 // the beginning of the new fragment.
552 flushPendingLabels(Frag, 0);
David Majnemer408b5e62016-02-05 01:55:49 +0000553 this->MCStreamer::EmitCVDefRangeDirective(Ranges, FixedSizePortion);
554}
555
Reid Kleckner2214ed82016-01-29 00:49:42 +0000556void MCObjectStreamer::EmitCVStringTableDirective() {
557 getContext().getCVContext().emitStringTable(*this);
558}
559void MCObjectStreamer::EmitCVFileChecksumsDirective() {
560 getContext().getCVContext().emitFileChecksums(*this);
561}
562
Reid Kleckner26fa1bf2017-09-19 18:14:45 +0000563void MCObjectStreamer::EmitCVFileChecksumOffsetDirective(unsigned FileNo) {
564 getContext().getCVContext().emitFileChecksumOffset(*this, FileNo);
565}
Reid Kleckner2214ed82016-01-29 00:49:42 +0000566
Rafael Espindola64e1af82013-07-02 15:49:13 +0000567void MCObjectStreamer::EmitBytes(StringRef Data) {
Eric Christopher445c9522016-10-14 05:47:37 +0000568 MCDwarfLineEntry::Make(this, getCurrentSectionOnly());
Petr Hosek32946702015-06-27 01:54:17 +0000569 MCDataFragment *DF = getOrCreateDataFragment();
570 flushPendingLabels(DF, DF->getContents().size());
571 DF->getContents().append(Data.begin(), Data.end());
Eric Christopherfe832702018-09-06 22:09:31 +0000572
573 // EmitBytes might not cover all possible ways we emit data (or could be used
574 // to emit executable code in some cases), but is the best method we have
575 // right now for checking this.
576 MCSection *Sec = getCurrentSectionOnly();
577 Sec->setHasData(true);
Benjamin Kramer64ddcb02012-10-04 13:12:43 +0000578}
579
580void MCObjectStreamer::EmitValueToAlignment(unsigned ByteAlignment,
581 int64_t Value,
582 unsigned ValueSize,
583 unsigned MaxBytesToEmit) {
584 if (MaxBytesToEmit == 0)
585 MaxBytesToEmit = ByteAlignment;
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000586 insert(new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit));
Benjamin Kramer64ddcb02012-10-04 13:12:43 +0000587
588 // Update the maximum alignment on the current section if necessary.
Eric Christopher445c9522016-10-14 05:47:37 +0000589 MCSection *CurSec = getCurrentSectionOnly();
Rafael Espindola967d6a62015-05-21 21:02:35 +0000590 if (ByteAlignment > CurSec->getAlignment())
Guillaume Chatelet18f805a2019-09-27 12:54:21 +0000591 CurSec->setAlignment(Align(ByteAlignment));
Benjamin Kramer64ddcb02012-10-04 13:12:43 +0000592}
593
594void MCObjectStreamer::EmitCodeAlignment(unsigned ByteAlignment,
595 unsigned MaxBytesToEmit) {
596 EmitValueToAlignment(ByteAlignment, 0, 1, MaxBytesToEmit);
597 cast<MCAlignFragment>(getCurrentFragment())->setEmitNops(true);
598}
599
Rafael Espindola7ae65d82015-11-04 23:59:18 +0000600void MCObjectStreamer::emitValueToOffset(const MCExpr *Offset,
Oliver Stannard268f42f2016-12-14 10:43:58 +0000601 unsigned char Value,
602 SMLoc Loc) {
603 insert(new MCOrgFragment(*Offset, Value, Loc));
Rafael Espindola9065bfc62010-12-02 05:59:38 +0000604}
605
Simon Atanasyaneb9ed612016-08-22 16:18:42 +0000606// Associate DTPRel32 fixup with data and resize data area
607void MCObjectStreamer::EmitDTPRel32Value(const MCExpr *Value) {
608 MCDataFragment *DF = getOrCreateDataFragment();
609 flushPendingLabels(DF, DF->getContents().size());
610
611 DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
612 Value, FK_DTPRel_4));
613 DF->getContents().resize(DF->getContents().size() + 4, 0);
614}
615
616// Associate DTPRel64 fixup with data and resize data area
617void MCObjectStreamer::EmitDTPRel64Value(const MCExpr *Value) {
618 MCDataFragment *DF = getOrCreateDataFragment();
619 flushPendingLabels(DF, DF->getContents().size());
620
621 DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
622 Value, FK_DTPRel_8));
623 DF->getContents().resize(DF->getContents().size() + 8, 0);
624}
625
626// Associate TPRel32 fixup with data and resize data area
627void MCObjectStreamer::EmitTPRel32Value(const MCExpr *Value) {
628 MCDataFragment *DF = getOrCreateDataFragment();
629 flushPendingLabels(DF, DF->getContents().size());
630
631 DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
632 Value, FK_TPRel_4));
633 DF->getContents().resize(DF->getContents().size() + 4, 0);
634}
635
636// Associate TPRel64 fixup with data and resize data area
637void MCObjectStreamer::EmitTPRel64Value(const MCExpr *Value) {
638 MCDataFragment *DF = getOrCreateDataFragment();
639 flushPendingLabels(DF, DF->getContents().size());
640
641 DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
642 Value, FK_TPRel_8));
643 DF->getContents().resize(DF->getContents().size() + 8, 0);
644}
645
Akira Hatanakaf5ddf132011-11-23 22:18:04 +0000646// Associate GPRel32 fixup with data and resize data area
647void MCObjectStreamer::EmitGPRel32Value(const MCExpr *Value) {
648 MCDataFragment *DF = getOrCreateDataFragment();
Petr Hosek32946702015-06-27 01:54:17 +0000649 flushPendingLabels(DF, DF->getContents().size());
Akira Hatanakaf5ddf132011-11-23 22:18:04 +0000650
Weiming Zhao74a7fa02017-04-03 21:50:04 +0000651 DF->getFixups().push_back(
652 MCFixup::create(DF->getContents().size(), Value, FK_GPRel_4));
Akira Hatanakaf5ddf132011-11-23 22:18:04 +0000653 DF->getContents().resize(DF->getContents().size() + 4, 0);
654}
655
Simon Atanasyaneb9ed612016-08-22 16:18:42 +0000656// Associate GPRel64 fixup with data and resize data area
Jack Carter77064c02012-08-22 00:49:30 +0000657void MCObjectStreamer::EmitGPRel64Value(const MCExpr *Value) {
658 MCDataFragment *DF = getOrCreateDataFragment();
Petr Hosek32946702015-06-27 01:54:17 +0000659 flushPendingLabels(DF, DF->getContents().size());
Jack Carter77064c02012-08-22 00:49:30 +0000660
Weiming Zhao74a7fa02017-04-03 21:50:04 +0000661 DF->getFixups().push_back(
662 MCFixup::create(DF->getContents().size(), Value, FK_GPRel_4));
Jack Carter77064c02012-08-22 00:49:30 +0000663 DF->getContents().resize(DF->getContents().size() + 8, 0);
664}
665
Daniel Sanders9f6ad492015-11-12 13:33:00 +0000666bool MCObjectStreamer::EmitRelocDirective(const MCExpr &Offset, StringRef Name,
Peter Smith57f661b2018-06-06 09:40:06 +0000667 const MCExpr *Expr, SMLoc Loc,
668 const MCSubtargetInfo &STI) {
David Majnemerce108422016-01-19 23:05:27 +0000669 Optional<MCFixupKind> MaybeKind = Assembler->getBackend().getFixupKind(Name);
670 if (!MaybeKind.hasValue())
Daniel Sanders9f6ad492015-11-12 13:33:00 +0000671 return true;
672
David Majnemerce108422016-01-19 23:05:27 +0000673 MCFixupKind Kind = *MaybeKind;
674
Daniel Sanders9f6ad492015-11-12 13:33:00 +0000675 if (Expr == nullptr)
676 Expr =
677 MCSymbolRefExpr::create(getContext().createTempSymbol(), getContext());
Vladimir Stefanovic1d2714b2018-11-21 16:28:39 +0000678
679 MCDataFragment *DF = getOrCreateDataFragment(&STI);
680 flushPendingLabels(DF, DF->getContents().size());
681
682 int64_t OffsetValue;
683 if (Offset.evaluateAsAbsolute(OffsetValue)) {
684 if (OffsetValue < 0)
685 llvm_unreachable(".reloc offset is negative");
686 DF->getFixups().push_back(MCFixup::create(OffsetValue, Expr, Kind, Loc));
687 return false;
688 }
689
690 if (Offset.getKind() != llvm::MCExpr::SymbolRef)
691 llvm_unreachable(".reloc offset is not absolute nor a label");
692
693 const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(Offset);
694 if (SRE.getSymbol().isDefined()) {
695 DF->getFixups().push_back(MCFixup::create(SRE.getSymbol().getOffset(),
696 Expr, Kind, Loc));
697 return false;
698 }
699
700 PendingFixups.emplace_back(&SRE.getSymbol(), DF,
701 MCFixup::create(-1, Expr, Kind, Loc));
Daniel Sanders9f6ad492015-11-12 13:33:00 +0000702 return false;
703}
704
Petr Hosek67a94a72016-05-28 05:57:48 +0000705void MCObjectStreamer::emitFill(const MCExpr &NumBytes, uint64_t FillValue,
706 SMLoc Loc) {
707 MCDataFragment *DF = getOrCreateDataFragment();
708 flushPendingLabels(DF, DF->getContents().size());
709
Rafael Espindola8fcd07d2018-01-09 19:29:33 +0000710 assert(getCurrentSectionOnly() && "need a section");
Nirav Dave588fad42018-05-18 17:45:48 +0000711 insert(new MCFillFragment(FillValue, 1, NumBytes, Loc));
Petr Hosek67a94a72016-05-28 05:57:48 +0000712}
713
714void MCObjectStreamer::emitFill(const MCExpr &NumValues, int64_t Size,
715 int64_t Expr, SMLoc Loc) {
716 int64_t IntNumValues;
Nirav Dave588fad42018-05-18 17:45:48 +0000717 // Do additional checking now if we can resolve the value.
718 if (NumValues.evaluateAsAbsolute(IntNumValues, getAssemblerPtr())) {
719 if (IntNumValues < 0) {
720 getContext().getSourceManager()->PrintMessage(
721 Loc, SourceMgr::DK_Warning,
722 "'.fill' directive with negative repeat count has no effect");
723 return;
724 }
725 // Emit now if we can for better errors.
726 int64_t NonZeroSize = Size > 4 ? 4 : Size;
727 Expr &= ~0ULL >> (64 - NonZeroSize * 8);
728 for (uint64_t i = 0, e = IntNumValues; i != e; ++i) {
729 EmitIntValue(Expr, NonZeroSize);
730 if (NonZeroSize < Size)
731 EmitIntValue(0, Size - NonZeroSize);
732 }
Petr Hosek67a94a72016-05-28 05:57:48 +0000733 return;
734 }
735
Nirav Dave588fad42018-05-18 17:45:48 +0000736 // Otherwise emit as fragment.
737 MCDataFragment *DF = getOrCreateDataFragment();
738 flushPendingLabels(DF, DF->getContents().size());
Petr Hosek67a94a72016-05-28 05:57:48 +0000739
Nirav Dave588fad42018-05-18 17:45:48 +0000740 assert(getCurrentSectionOnly() && "need a section");
741 insert(new MCFillFragment(Expr, Size, NumValues, Loc));
Petr Hosek67a94a72016-05-28 05:57:48 +0000742}
743
Peter Collingbourne5b75fd92017-03-03 21:22:06 +0000744void MCObjectStreamer::EmitFileDirective(StringRef Filename) {
745 getAssembler().addFileName(Filename);
746}
747
Peter Collingbourne3e227332018-07-17 22:17:18 +0000748void MCObjectStreamer::EmitAddrsig() {
749 getAssembler().getWriter().emitAddrsigSection();
750}
751
752void MCObjectStreamer::EmitAddrsigSym(const MCSymbol *Sym) {
753 getAssembler().registerSymbol(*Sym);
754 getAssembler().getWriter().addAddrsigSymbol(Sym);
755}
756
Rafael Espindola07082092012-01-07 03:13:18 +0000757void MCObjectStreamer::FinishImpl() {
Jonas Devlieghere26ddf272018-07-11 12:30:35 +0000758 getContext().RemapDebugPaths();
Paul Robinsonc17c8bf2018-07-10 14:41:54 +0000759
Kevin Enderbye7739d42011-12-09 18:09:40 +0000760 // If we are generating dwarf for assembly source files dump out the sections.
761 if (getContext().getGenDwarfForAssembly())
David Blaikie8bf66c42014-04-01 07:35:52 +0000762 MCGenDwarfInfo::Emit(this);
763
764 // Dump out the dwarf file & directory tables and line tables.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000765 MCDwarfLineTable::Emit(this, getAssembler().getDWARFLinetableParams());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000766
Michael Trent6f95d332019-12-11 10:42:37 -0800767 // Update any remaining pending labels with empty data fragments.
Jonas Devliegheree13e6db2018-07-10 15:32:17 +0000768 flushPendingLabels();
Michael Trent6f95d332019-12-11 10:42:37 -0800769
Vladimir Stefanovic1d2714b2018-11-21 16:28:39 +0000770 resolvePendingFixups();
Daniel Dunbarb2347fe2010-06-16 20:04:25 +0000771 getAssembler().Finish();
772}