Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/MC/MCObjectStreamer.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- lib/MC/MCObjectStreamer.cpp - Object File MCStreamer Interface -----===//
2
//
3
// 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
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "llvm/MC/MCObjectStreamer.h"
10
#include "llvm/ADT/STLExtras.h"
11
#include "llvm/MC/MCAsmBackend.h"
12
#include "llvm/MC/MCAssembler.h"
13
#include "llvm/MC/MCCodeEmitter.h"
14
#include "llvm/MC/MCCodeView.h"
15
#include "llvm/MC/MCContext.h"
16
#include "llvm/MC/MCDwarf.h"
17
#include "llvm/MC/MCExpr.h"
18
#include "llvm/MC/MCObjectWriter.h"
19
#include "llvm/MC/MCSection.h"
20
#include "llvm/MC/MCSymbol.h"
21
#include "llvm/Support/ErrorHandling.h"
22
#include "llvm/Support/SourceMgr.h"
23
using namespace llvm;
24
25
MCObjectStreamer::MCObjectStreamer(MCContext &Context,
26
                                   std::unique_ptr<MCAsmBackend> TAB,
27
                                   std::unique_ptr<MCObjectWriter> OW,
28
                                   std::unique_ptr<MCCodeEmitter> Emitter)
29
    : MCStreamer(Context),
30
      Assembler(llvm::make_unique<MCAssembler>(
31
          Context, std::move(TAB), std::move(Emitter), std::move(OW))),
32
20.6k
      EmitEHFrame(true), EmitDebugFrame(false) {}
33
34
20.5k
MCObjectStreamer::~MCObjectStreamer() {}
35
36
// 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.
39
2.48M
MCAssembler *MCObjectStreamer::getAssemblerPtr() {
40
2.48M
  if (getUseAssemblerInfoForParsing())
41
26.6k
    return Assembler.get();
42
2.46M
  return nullptr;
43
2.46M
}
44
45
18.7M
void MCObjectStreamer::flushPendingLabels(MCFragment *F, uint64_t FOffset) {
46
18.7M
  if (PendingLabels.empty())
47
18.4M
    return;
48
260k
  if (!F) {
49
6.10k
    F = new MCDataFragment();
50
6.10k
    MCSection *CurSection = getCurrentSectionOnly();
51
6.10k
    CurSection->getFragmentList().insert(CurInsertionPoint, F);
52
6.10k
    F->setParent(CurSection);
53
6.10k
  }
54
406k
  for (MCSymbol *Sym : PendingLabels) {
55
406k
    Sym->setFragment(F);
56
406k
    Sym->setOffset(FOffset);
57
406k
  }
58
260k
  PendingLabels.clear();
59
260k
}
60
61
// When fixup's offset is a forward declared label, e.g.:
62
//
63
//   .reloc 1f, R_MIPS_JALR, foo
64
// 1: nop
65
//
66
// postpone adding it to Fixups vector until the label is defined and its offset
67
// is known.
68
20.4k
void MCObjectStreamer::resolvePendingFixups() {
69
20.4k
  for (PendingMCFixup &PendingFixup : PendingFixups) {
70
51
    if (!PendingFixup.Sym || PendingFixup.Sym->isUndefined ()) {
71
2
      getContext().reportError(PendingFixup.Fixup.getLoc(),
72
2
                               "unresolved relocation offset");
73
2
      continue;
74
2
    }
75
49
    flushPendingLabels(PendingFixup.DF, PendingFixup.DF->getContents().size());
76
49
    PendingFixup.Fixup.setOffset(PendingFixup.Sym->getOffset());
77
49
    PendingFixup.DF->getFixups().push_back(PendingFixup.Fixup);
78
49
  }
79
20.4k
  PendingFixups.clear();
80
20.4k
}
81
82
// As a compile-time optimization, avoid allocating and evaluating an MCExpr
83
// tree for (Hi - Lo) when Hi and Lo are offsets into the same fragment.
84
static Optional<uint64_t>
85
454k
absoluteSymbolDiff(MCAssembler &Asm, const MCSymbol *Hi, const MCSymbol *Lo) {
86
454k
  assert(Hi && Lo);
87
454k
  if (Asm.getBackendPtr()->requiresDiffExpressionRelocations())
88
16
    return None;
89
454k
90
454k
  if (!Hi->getFragment() || 
Hi->getFragment() != Lo->getFragment()311k
||
91
454k
      
Hi->isVariable()79.7k
||
Lo->isVariable()79.6k
)
92
374k
    return None;
93
79.6k
94
79.6k
  return Hi->getOffset() - Lo->getOffset();
95
79.6k
}
96
97
void MCObjectStreamer::emitAbsoluteSymbolDiff(const MCSymbol *Hi,
98
                                              const MCSymbol *Lo,
99
378k
                                              unsigned Size) {
100
378k
  if (Optional<uint64_t> Diff = absoluteSymbolDiff(getAssembler(), Hi, Lo)) {
101
8.80k
    EmitIntValue(*Diff, Size);
102
8.80k
    return;
103
8.80k
  }
104
369k
  MCStreamer::emitAbsoluteSymbolDiff(Hi, Lo, Size);
105
369k
}
106
107
void MCObjectStreamer::emitAbsoluteSymbolDiffAsULEB128(const MCSymbol *Hi,
108
75.6k
                                                       const MCSymbol *Lo) {
109
75.6k
  if (Optional<uint64_t> Diff = absoluteSymbolDiff(getAssembler(), Hi, Lo)) {
110
70.8k
    EmitULEB128IntValue(*Diff);
111
70.8k
    return;
112
70.8k
  }
113
4.80k
  MCStreamer::emitAbsoluteSymbolDiffAsULEB128(Hi, Lo);
114
4.80k
}
115
116
14.4k
void MCObjectStreamer::reset() {
117
14.4k
  if (Assembler)
118
14.4k
    Assembler->reset();
119
14.4k
  CurInsertionPoint = MCSection::iterator();
120
14.4k
  EmitEHFrame = true;
121
14.4k
  EmitDebugFrame = false;
122
14.4k
  PendingLabels.clear();
123
14.4k
  MCStreamer::reset();
124
14.4k
}
125
126
20.4k
void MCObjectStreamer::EmitFrames(MCAsmBackend *MAB) {
127
20.4k
  if (!getNumFrameInfos())
128
14.8k
    return;
129
5.62k
130
5.62k
  if (EmitEHFrame)
131
5.50k
    MCDwarfFrameEmitter::Emit(*this, MAB, true);
132
5.62k
133
5.62k
  if (EmitDebugFrame)
134
115
    MCDwarfFrameEmitter::Emit(*this, MAB, false);
135
5.62k
}
136
137
52.3M
MCFragment *MCObjectStreamer::getCurrentFragment() const {
138
52.3M
  assert(getCurrentSectionOnly() && "No current section!");
139
52.3M
140
52.3M
  if (CurInsertionPoint != getCurrentSectionOnly()->getFragmentList().begin())
141
52.2M
    return &*std::prev(CurInsertionPoint);
142
60.0k
143
60.0k
  return nullptr;
144
60.0k
}
145
146
static bool CanReuseDataFragment(const MCDataFragment &F,
147
                                 const MCAssembler &Assembler,
148
27.6M
                                 const MCSubtargetInfo *STI) {
149
27.6M
  if (!F.hasInstructions())
150
10.4M
    return true;
151
17.2M
  // When bundling is enabled, we don't want to add data to a fragment that
152
17.2M
  // already has instructions (see MCELFStreamer::EmitInstToData for details)
153
17.2M
  if (Assembler.isBundlingEnabled())
154
126
    return Assembler.getRelaxAll();
155
17.2M
  // If the subtarget is changed mid fragment we start a new fragment to record
156
17.2M
  // the new STI.
157
17.2M
  return !STI || 
F.getSubtargetInfo() == STI62.8k
;
158
17.2M
}
159
160
MCDataFragment *
161
29.5M
MCObjectStreamer::getOrCreateDataFragment(const MCSubtargetInfo *STI) {
162
29.5M
  MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
163
29.5M
  if (!F || 
!CanReuseDataFragment(*F, *Assembler, STI)27.6M
) {
164
1.88M
    F = new MCDataFragment();
165
1.88M
    insert(F);
166
1.88M
  }
167
29.5M
  return F;
168
29.5M
}
169
170
0
MCPaddingFragment *MCObjectStreamer::getOrCreatePaddingFragment() {
171
0
  MCPaddingFragment *F =
172
0
      dyn_cast_or_null<MCPaddingFragment>(getCurrentFragment());
173
0
  if (!F) {
174
0
    F = new MCPaddingFragment();
175
0
    insert(F);
176
0
  }
177
0
  return F;
178
0
}
179
180
6.98M
void MCObjectStreamer::visitUsedSymbol(const MCSymbol &Sym) {
181
6.98M
  Assembler->registerSymbol(Sym);
182
6.98M
}
183
184
115
void MCObjectStreamer::EmitCFISections(bool EH, bool Debug) {
185
115
  MCStreamer::EmitCFISections(EH, Debug);
186
115
  EmitEHFrame = EH;
187
115
  EmitDebugFrame = Debug;
188
115
}
189
190
void MCObjectStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
191
1.37M
                                     SMLoc Loc) {
192
1.37M
  MCStreamer::EmitValueImpl(Value, Size, Loc);
193
1.37M
  MCDataFragment *DF = getOrCreateDataFragment();
194
1.37M
  flushPendingLabels(DF, DF->getContents().size());
195
1.37M
196
1.37M
  MCDwarfLineEntry::Make(this, getCurrentSectionOnly());
197
1.37M
198
1.37M
  // Avoid fixups when possible.
199
1.37M
  int64_t AbsValue;
200
1.37M
  if (Value->evaluateAsAbsolute(AbsValue, getAssemblerPtr())) {
201
4.62k
    if (!isUIntN(8 * Size, AbsValue) && 
!isIntN(8 * Size, AbsValue)4
) {
202
1
      getContext().reportError(
203
1
          Loc, "value evaluated as " + Twine(AbsValue) + " is out of range.");
204
1
      return;
205
1
    }
206
4.62k
    EmitIntValue(AbsValue, Size);
207
4.62k
    return;
208
4.62k
  }
209
1.36M
  DF->getFixups().push_back(
210
1.36M
      MCFixup::create(DF->getContents().size(), Value,
211
1.36M
                      MCFixup::getKindForSize(Size, false), Loc));
212
1.36M
  DF->getContents().resize(DF->getContents().size() + Size, 0);
213
1.36M
}
214
215
384k
MCSymbol *MCObjectStreamer::EmitCFILabel() {
216
384k
  MCSymbol *Label = getContext().createTempSymbol("cfi", true);
217
384k
  EmitLabel(Label);
218
384k
  return Label;
219
384k
}
220
221
65.4k
void MCObjectStreamer::EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame) {
222
65.4k
  // We need to create a local symbol to avoid relocations.
223
65.4k
  Frame.Begin = getContext().createTempSymbol();
224
65.4k
  EmitLabel(Frame.Begin);
225
65.4k
}
226
227
65.4k
void MCObjectStreamer::EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) {
228
65.4k
  Frame.End = getContext().createTempSymbol();
229
65.4k
  EmitLabel(Frame.End);
230
65.4k
}
231
232
5.68M
void MCObjectStreamer::EmitLabel(MCSymbol *Symbol, SMLoc Loc) {
233
5.68M
  MCStreamer::EmitLabel(Symbol, Loc);
234
5.68M
235
5.68M
  getAssembler().registerSymbol(*Symbol);
236
5.68M
237
5.68M
  // If there is a current fragment, mark the symbol as pointing into it.
238
5.68M
  // Otherwise queue the label and set its fragment pointer when we emit the
239
5.68M
  // next fragment.
240
5.68M
  auto *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
241
5.68M
  if (F && 
!(5.27M
getAssembler().isBundlingEnabled()5.27M
&&
242
5.27M
             
getAssembler().getRelaxAll()26
)) {
243
5.27M
    Symbol->setFragment(F);
244
5.27M
    Symbol->setOffset(F->getContents().size());
245
5.27M
  } else {
246
406k
    PendingLabels.push_back(Symbol);
247
406k
  }
248
5.68M
}
249
250
12
void MCObjectStreamer::EmitLabel(MCSymbol *Symbol, SMLoc Loc, MCFragment *F) {
251
12
  MCStreamer::EmitLabel(Symbol, Loc);
252
12
  getAssembler().registerSymbol(*Symbol);
253
12
  auto *DF = dyn_cast_or_null<MCDataFragment>(F);
254
12
  if (DF)
255
12
    Symbol->setFragment(F);
256
0
  else
257
0
    PendingLabels.push_back(Symbol);
258
12
}
259
260
5.17k
void MCObjectStreamer::EmitULEB128Value(const MCExpr *Value) {
261
5.17k
  int64_t IntValue;
262
5.17k
  if (Value->evaluateAsAbsolute(IntValue, getAssemblerPtr())) {
263
359
    EmitULEB128IntValue(IntValue);
264
359
    return;
265
359
  }
266
4.81k
  insert(new MCLEBFragment(*Value, false));
267
4.81k
}
268
269
60
void MCObjectStreamer::EmitSLEB128Value(const MCExpr *Value) {
270
60
  int64_t IntValue;
271
60
  if (Value->evaluateAsAbsolute(IntValue, getAssemblerPtr())) {
272
58
    EmitSLEB128IntValue(IntValue);
273
58
    return;
274
58
  }
275
2
  insert(new MCLEBFragment(*Value, true));
276
2
}
277
278
void MCObjectStreamer::EmitWeakReference(MCSymbol *Alias,
279
0
                                         const MCSymbol *Symbol) {
280
0
  report_fatal_error("This file format doesn't support weak aliases.");
281
0
}
282
283
void MCObjectStreamer::ChangeSection(MCSection *Section,
284
5.44k
                                     const MCExpr *Subsection) {
285
5.44k
  changeSectionImpl(Section, Subsection);
286
5.44k
}
287
288
bool MCObjectStreamer::changeSectionImpl(MCSection *Section,
289
638k
                                         const MCExpr *Subsection) {
290
638k
  assert(Section && "Cannot switch to a null section!");
291
638k
  flushPendingLabels(nullptr);
292
638k
  getContext().clearDwarfLocSeen();
293
638k
294
638k
  bool Created = getAssembler().registerSection(*Section);
295
638k
296
638k
  int64_t IntSubsection = 0;
297
638k
  if (Subsection &&
298
638k
      
!Subsection->evaluateAsAbsolute(IntSubsection, getAssemblerPtr())6
)
299
0
    report_fatal_error("Cannot evaluate subsection number");
300
638k
  
if (638k
IntSubsection < 0638k
|| IntSubsection > 8192)
301
0
    report_fatal_error("Subsection number out of range");
302
638k
  CurInsertionPoint =
303
638k
      Section->getSubsectionInsertionPoint(unsigned(IntSubsection));
304
638k
  return Created;
305
638k
}
306
307
535k
void MCObjectStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
308
535k
  getAssembler().registerSymbol(*Symbol);
309
535k
  MCStreamer::EmitAssignment(Symbol, Value);
310
535k
}
311
312
70
bool MCObjectStreamer::mayHaveInstructions(MCSection &Sec) const {
313
70
  return Sec.hasInstructions();
314
70
}
315
316
void MCObjectStreamer::EmitInstruction(const MCInst &Inst,
317
16.7M
                                       const MCSubtargetInfo &STI) {
318
16.7M
  getAssembler().getBackend().handleCodePaddingInstructionBegin(Inst);
319
16.7M
  EmitInstructionImpl(Inst, STI);
320
16.7M
  getAssembler().getBackend().handleCodePaddingInstructionEnd(Inst);
321
16.7M
}
322
323
void MCObjectStreamer::EmitInstructionImpl(const MCInst &Inst,
324
16.7M
                                           const MCSubtargetInfo &STI) {
325
16.7M
  MCStreamer::EmitInstruction(Inst, STI);
326
16.7M
327
16.7M
  MCSection *Sec = getCurrentSectionOnly();
328
16.7M
  Sec->setHasInstructions(true);
329
16.7M
330
16.7M
  // Now that a machine instruction has been assembled into this section, make
331
16.7M
  // a line entry for any .loc directive that has been seen.
332
16.7M
  MCDwarfLineEntry::Make(this, getCurrentSectionOnly());
333
16.7M
334
16.7M
  // If this instruction doesn't need relaxation, just emit it as data.
335
16.7M
  MCAssembler &Assembler = getAssembler();
336
16.7M
  if (!Assembler.getBackend().mayNeedRelaxation(Inst, STI)) {
337
16.5M
    EmitInstToData(Inst, STI);
338
16.5M
    return;
339
16.5M
  }
340
237k
341
237k
  // Otherwise, relax and emit it as data if either:
342
237k
  // - The RelaxAll flag was passed
343
237k
  // - Bundling is enabled and this instruction is inside a bundle-locked
344
237k
  //   group. We want to emit all such instructions into the same data
345
237k
  //   fragment.
346
237k
  if (Assembler.getRelaxAll() ||
347
237k
      
(237k
Assembler.isBundlingEnabled()237k
&&
Sec->isBundleLocked()5
)) {
348
50
    MCInst Relaxed;
349
50
    getAssembler().getBackend().relaxInstruction(Inst, STI, Relaxed);
350
50
    while (getAssembler().getBackend().mayNeedRelaxation(Relaxed, STI))
351
0
      getAssembler().getBackend().relaxInstruction(Relaxed, STI, Relaxed);
352
50
    EmitInstToData(Relaxed, STI);
353
50
    return;
354
50
  }
355
237k
356
237k
  // Otherwise emit to a separate fragment.
357
237k
  EmitInstToFragment(Inst, STI);
358
237k
}
359
360
void MCObjectStreamer::EmitInstToFragment(const MCInst &Inst,
361
237k
                                          const MCSubtargetInfo &STI) {
362
237k
  if (getAssembler().getRelaxAll() && 
getAssembler().isBundlingEnabled()0
)
363
237k
    
llvm_unreachable0
("All instructions should have already been relaxed");
364
237k
365
237k
  // Always create a new, separate fragment here, because its size can change
366
237k
  // during relaxation.
367
237k
  MCRelaxableFragment *IF = new MCRelaxableFragment(Inst, STI);
368
237k
  insert(IF);
369
237k
370
237k
  SmallString<128> Code;
371
237k
  raw_svector_ostream VecOS(Code);
372
237k
  getAssembler().getEmitter().encodeInstruction(Inst, VecOS, IF->getFixups(),
373
237k
                                                STI);
374
237k
  IF->getContents().append(Code.begin(), Code.end());
375
237k
}
376
377
#ifndef NDEBUG
378
static const char *const BundlingNotImplementedMsg =
379
  "Aligned bundling is not implemented for this object format";
380
#endif
381
382
0
void MCObjectStreamer::EmitBundleAlignMode(unsigned AlignPow2) {
383
0
  llvm_unreachable(BundlingNotImplementedMsg);
384
0
}
385
386
0
void MCObjectStreamer::EmitBundleLock(bool AlignToEnd) {
387
0
  llvm_unreachable(BundlingNotImplementedMsg);
388
0
}
389
390
0
void MCObjectStreamer::EmitBundleUnlock() {
391
0
  llvm_unreachable(BundlingNotImplementedMsg);
392
0
}
393
394
void MCObjectStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
395
                                             unsigned Column, unsigned Flags,
396
                                             unsigned Isa,
397
                                             unsigned Discriminator,
398
967k
                                             StringRef FileName) {
399
967k
  // In case we see two .loc directives in a row, make sure the
400
967k
  // first one gets a line entry.
401
967k
  MCDwarfLineEntry::Make(this, getCurrentSectionOnly());
402
967k
403
967k
  this->MCStreamer::EmitDwarfLocDirective(FileNo, Line, Column, Flags,
404
967k
                                          Isa, Discriminator, FileName);
405
967k
}
406
407
static const MCExpr *buildSymbolDiff(MCObjectStreamer &OS, const MCSymbol *A,
408
1.09M
                                     const MCSymbol *B) {
409
1.09M
  MCContext &Context = OS.getContext();
410
1.09M
  MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
411
1.09M
  const MCExpr *ARef = MCSymbolRefExpr::create(A, Variant, Context);
412
1.09M
  const MCExpr *BRef = MCSymbolRefExpr::create(B, Variant, Context);
413
1.09M
  const MCExpr *AddrDelta =
414
1.09M
      MCBinaryExpr::create(MCBinaryExpr::Sub, ARef, BRef, Context);
415
1.09M
  return AddrDelta;
416
1.09M
}
417
418
static void emitDwarfSetLineAddr(MCObjectStreamer &OS,
419
                                 MCDwarfLineTableParams Params,
420
                                 int64_t LineDelta, const MCSymbol *Label,
421
2.02k
                                 int PointerSize) {
422
2.02k
  // emit the sequence to set the address
423
2.02k
  OS.EmitIntValue(dwarf::DW_LNS_extended_op, 1);
424
2.02k
  OS.EmitULEB128IntValue(PointerSize + 1);
425
2.02k
  OS.EmitIntValue(dwarf::DW_LNE_set_address, 1);
426
2.02k
  OS.EmitSymbolValue(Label, PointerSize);
427
2.02k
428
2.02k
  // emit the sequence for the LineDelta (from 1) and a zero address delta.
429
2.02k
  MCDwarfLineAddr::Emit(&OS, Params, LineDelta, 0);
430
2.02k
}
431
432
void MCObjectStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta,
433
                                                const MCSymbol *LastLabel,
434
                                                const MCSymbol *Label,
435
969k
                                                unsigned PointerSize) {
436
969k
  if (!LastLabel) {
437
2.02k
    emitDwarfSetLineAddr(*this, Assembler->getDWARFLinetableParams(), LineDelta,
438
2.02k
                         Label, PointerSize);
439
2.02k
    return;
440
2.02k
  }
441
967k
  const MCExpr *AddrDelta = buildSymbolDiff(*this, Label, LastLabel);
442
967k
  int64_t Res;
443
967k
  if (AddrDelta->evaluateAsAbsolute(Res, getAssemblerPtr())) {
444
1.61k
    MCDwarfLineAddr::Emit(this, Assembler->getDWARFLinetableParams(), LineDelta,
445
1.61k
                          Res);
446
1.61k
    return;
447
1.61k
  }
448
965k
  insert(new MCDwarfLineAddrFragment(LineDelta, *AddrDelta));
449
965k
}
450
451
void MCObjectStreamer::EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
452
132k
                                                 const MCSymbol *Label) {
453
132k
  const MCExpr *AddrDelta = buildSymbolDiff(*this, Label, LastLabel);
454
132k
  int64_t Res;
455
132k
  if (AddrDelta->evaluateAsAbsolute(Res, getAssemblerPtr())) {
456
1.12k
    MCDwarfFrameEmitter::EmitAdvanceLoc(*this, Res);
457
1.12k
    return;
458
1.12k
  }
459
131k
  insert(new MCDwarfCallFrameFragment(*AddrDelta));
460
131k
}
461
462
void MCObjectStreamer::EmitCVLocDirective(unsigned FunctionId, unsigned FileNo,
463
                                          unsigned Line, unsigned Column,
464
                                          bool PrologueEnd, bool IsStmt,
465
818
                                          StringRef FileName, SMLoc Loc) {
466
818
  // Validate the directive.
467
818
  if (!checkCVLocSection(FunctionId, FileNo, Loc))
468
0
    return;
469
818
470
818
  // Emit a label at the current position and record it in the CodeViewContext.
471
818
  MCSymbol *LineSym = getContext().createTempSymbol();
472
818
  EmitLabel(LineSym);
473
818
  getContext().getCVContext().recordCVLoc(getContext(), LineSym, FunctionId,
474
818
                                          FileNo, Line, Column, PrologueEnd,
475
818
                                          IsStmt);
476
818
}
477
478
void MCObjectStreamer::EmitCVLinetableDirective(unsigned FunctionId,
479
                                                const MCSymbol *Begin,
480
277
                                                const MCSymbol *End) {
481
277
  getContext().getCVContext().emitLineTableForFunction(*this, FunctionId, Begin,
482
277
                                                       End);
483
277
  this->MCStreamer::EmitCVLinetableDirective(FunctionId, Begin, End);
484
277
}
485
486
void MCObjectStreamer::EmitCVInlineLinetableDirective(
487
    unsigned PrimaryFunctionId, unsigned SourceFileId, unsigned SourceLineNum,
488
33
    const MCSymbol *FnStartSym, const MCSymbol *FnEndSym) {
489
33
  getContext().getCVContext().emitInlineLineTableForFunction(
490
33
      *this, PrimaryFunctionId, SourceFileId, SourceLineNum, FnStartSym,
491
33
      FnEndSym);
492
33
  this->MCStreamer::EmitCVInlineLinetableDirective(
493
33
      PrimaryFunctionId, SourceFileId, SourceLineNum, FnStartSym, FnEndSym);
494
33
}
495
496
void MCObjectStreamer::EmitCVDefRangeDirective(
497
    ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
498
358
    StringRef FixedSizePortion) {
499
358
  MCFragment *Frag =
500
358
      getContext().getCVContext().emitDefRange(*this, Ranges, FixedSizePortion);
501
358
  // Attach labels that were pending before we created the defrange fragment to
502
358
  // the beginning of the new fragment.
503
358
  flushPendingLabels(Frag, 0);
504
358
  this->MCStreamer::EmitCVDefRangeDirective(Ranges, FixedSizePortion);
505
358
}
506
507
149
void MCObjectStreamer::EmitCVStringTableDirective() {
508
149
  getContext().getCVContext().emitStringTable(*this);
509
149
}
510
146
void MCObjectStreamer::EmitCVFileChecksumsDirective() {
511
146
  getContext().getCVContext().emitFileChecksums(*this);
512
146
}
513
514
312
void MCObjectStreamer::EmitCVFileChecksumOffsetDirective(unsigned FileNo) {
515
312
  getContext().getCVContext().emitFileChecksumOffset(*this, FileNo);
516
312
}
517
518
11.0M
void MCObjectStreamer::EmitBytes(StringRef Data) {
519
11.0M
  MCDwarfLineEntry::Make(this, getCurrentSectionOnly());
520
11.0M
  MCDataFragment *DF = getOrCreateDataFragment();
521
11.0M
  flushPendingLabels(DF, DF->getContents().size());
522
11.0M
  DF->getContents().append(Data.begin(), Data.end());
523
11.0M
524
11.0M
  // EmitBytes might not cover all possible ways we emit data (or could be used
525
11.0M
  // to emit executable code in some cases), but is the best method we have
526
11.0M
  // right now for checking this.
527
11.0M
  MCSection *Sec = getCurrentSectionOnly();
528
11.0M
  Sec->setHasData(true);
529
11.0M
}
530
531
void MCObjectStreamer::EmitValueToAlignment(unsigned ByteAlignment,
532
                                            int64_t Value,
533
                                            unsigned ValueSize,
534
488k
                                            unsigned MaxBytesToEmit) {
535
488k
  if (MaxBytesToEmit == 0)
536
488k
    MaxBytesToEmit = ByteAlignment;
537
488k
  insert(new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit));
538
488k
539
488k
  // Update the maximum alignment on the current section if necessary.
540
488k
  MCSection *CurSec = getCurrentSectionOnly();
541
488k
  if (ByteAlignment > CurSec->getAlignment())
542
53.9k
    CurSec->setAlignment(ByteAlignment);
543
488k
}
544
545
void MCObjectStreamer::EmitCodeAlignment(unsigned ByteAlignment,
546
300k
                                         unsigned MaxBytesToEmit) {
547
300k
  EmitValueToAlignment(ByteAlignment, 0, 1, MaxBytesToEmit);
548
300k
  cast<MCAlignFragment>(getCurrentFragment())->setEmitNops(true);
549
300k
}
550
551
void MCObjectStreamer::emitValueToOffset(const MCExpr *Offset,
552
                                         unsigned char Value,
553
19
                                         SMLoc Loc) {
554
19
  insert(new MCOrgFragment(*Offset, Value, Loc));
555
19
}
556
557
void MCObjectStreamer::EmitCodePaddingBasicBlockStart(
558
2.37M
    const MCCodePaddingContext &Context) {
559
2.37M
  getAssembler().getBackend().handleCodePaddingBasicBlockStart(this, Context);
560
2.37M
}
561
562
void MCObjectStreamer::EmitCodePaddingBasicBlockEnd(
563
2.37M
    const MCCodePaddingContext &Context) {
564
2.37M
  getAssembler().getBackend().handleCodePaddingBasicBlockEnd(Context);
565
2.37M
}
566
567
// Associate DTPRel32 fixup with data and resize data area
568
3
void MCObjectStreamer::EmitDTPRel32Value(const MCExpr *Value) {
569
3
  MCDataFragment *DF = getOrCreateDataFragment();
570
3
  flushPendingLabels(DF, DF->getContents().size());
571
3
572
3
  DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
573
3
                                            Value, FK_DTPRel_4));
574
3
  DF->getContents().resize(DF->getContents().size() + 4, 0);
575
3
}
576
577
// Associate DTPRel64 fixup with data and resize data area
578
3
void MCObjectStreamer::EmitDTPRel64Value(const MCExpr *Value) {
579
3
  MCDataFragment *DF = getOrCreateDataFragment();
580
3
  flushPendingLabels(DF, DF->getContents().size());
581
3
582
3
  DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
583
3
                                            Value, FK_DTPRel_8));
584
3
  DF->getContents().resize(DF->getContents().size() + 8, 0);
585
3
}
586
587
// Associate TPRel32 fixup with data and resize data area
588
3
void MCObjectStreamer::EmitTPRel32Value(const MCExpr *Value) {
589
3
  MCDataFragment *DF = getOrCreateDataFragment();
590
3
  flushPendingLabels(DF, DF->getContents().size());
591
3
592
3
  DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
593
3
                                            Value, FK_TPRel_4));
594
3
  DF->getContents().resize(DF->getContents().size() + 4, 0);
595
3
}
596
597
// Associate TPRel64 fixup with data and resize data area
598
3
void MCObjectStreamer::EmitTPRel64Value(const MCExpr *Value) {
599
3
  MCDataFragment *DF = getOrCreateDataFragment();
600
3
  flushPendingLabels(DF, DF->getContents().size());
601
3
602
3
  DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
603
3
                                            Value, FK_TPRel_8));
604
3
  DF->getContents().resize(DF->getContents().size() + 8, 0);
605
3
}
606
607
// Associate GPRel32 fixup with data and resize data area
608
14
void MCObjectStreamer::EmitGPRel32Value(const MCExpr *Value) {
609
14
  MCDataFragment *DF = getOrCreateDataFragment();
610
14
  flushPendingLabels(DF, DF->getContents().size());
611
14
612
14
  DF->getFixups().push_back(
613
14
      MCFixup::create(DF->getContents().size(), Value, FK_GPRel_4));
614
14
  DF->getContents().resize(DF->getContents().size() + 4, 0);
615
14
}
616
617
// Associate GPRel64 fixup with data and resize data area
618
13
void MCObjectStreamer::EmitGPRel64Value(const MCExpr *Value) {
619
13
  MCDataFragment *DF = getOrCreateDataFragment();
620
13
  flushPendingLabels(DF, DF->getContents().size());
621
13
622
13
  DF->getFixups().push_back(
623
13
      MCFixup::create(DF->getContents().size(), Value, FK_GPRel_4));
624
13
  DF->getContents().resize(DF->getContents().size() + 8, 0);
625
13
}
626
627
bool MCObjectStreamer::EmitRelocDirective(const MCExpr &Offset, StringRef Name,
628
                                          const MCExpr *Expr, SMLoc Loc,
629
204
                                          const MCSubtargetInfo &STI) {
630
204
  Optional<MCFixupKind> MaybeKind = Assembler->getBackend().getFixupKind(Name);
631
204
  if (!MaybeKind.hasValue())
632
0
    return true;
633
204
634
204
  MCFixupKind Kind = *MaybeKind;
635
204
636
204
  if (Expr == nullptr)
637
3
    Expr =
638
3
        MCSymbolRefExpr::create(getContext().createTempSymbol(), getContext());
639
204
640
204
  MCDataFragment *DF = getOrCreateDataFragment(&STI);
641
204
  flushPendingLabels(DF, DF->getContents().size());
642
204
643
204
  int64_t OffsetValue;
644
204
  if (Offset.evaluateAsAbsolute(OffsetValue)) {
645
142
    if (OffsetValue < 0)
646
142
      
llvm_unreachable0
(".reloc offset is negative");
647
142
    DF->getFixups().push_back(MCFixup::create(OffsetValue, Expr, Kind, Loc));
648
142
    return false;
649
62
  }
650
62
651
62
  if (Offset.getKind() != llvm::MCExpr::SymbolRef)
652
62
    
llvm_unreachable0
(".reloc offset is not absolute nor a label");
653
62
654
62
  const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(Offset);
655
62
  if (SRE.getSymbol().isDefined()) {
656
9
    DF->getFixups().push_back(MCFixup::create(SRE.getSymbol().getOffset(),
657
9
                                              Expr, Kind, Loc));
658
9
    return false;
659
9
  }
660
53
661
53
  PendingFixups.emplace_back(&SRE.getSymbol(), DF,
662
53
                                         MCFixup::create(-1, Expr, Kind, Loc));
663
53
  return false;
664
53
}
665
666
void MCObjectStreamer::emitFill(const MCExpr &NumBytes, uint64_t FillValue,
667
609k
                                SMLoc Loc) {
668
609k
  MCDataFragment *DF = getOrCreateDataFragment();
669
609k
  flushPendingLabels(DF, DF->getContents().size());
670
609k
671
609k
  assert(getCurrentSectionOnly() && "need a section");
672
609k
  insert(new MCFillFragment(FillValue, 1, NumBytes, Loc));
673
609k
}
674
675
void MCObjectStreamer::emitFill(const MCExpr &NumValues, int64_t Size,
676
568
                                int64_t Expr, SMLoc Loc) {
677
568
  int64_t IntNumValues;
678
568
  // Do additional checking now if we can resolve the value.
679
568
  if (NumValues.evaluateAsAbsolute(IntNumValues, getAssemblerPtr())) {
680
563
    if (IntNumValues < 0) {
681
2
      getContext().getSourceManager()->PrintMessage(
682
2
          Loc, SourceMgr::DK_Warning,
683
2
          "'.fill' directive with negative repeat count has no effect");
684
2
      return;
685
2
    }
686
561
    // Emit now if we can for better errors.
687
561
    int64_t NonZeroSize = Size > 4 ? 
45
:
Size556
;
688
561
    Expr &= ~0ULL >> (64 - NonZeroSize * 8);
689
3.06M
    for (uint64_t i = 0, e = IntNumValues; i != e; 
++i3.06M
) {
690
3.06M
      EmitIntValue(Expr, NonZeroSize);
691
3.06M
      if (NonZeroSize < Size)
692
7
        EmitIntValue(0, Size - NonZeroSize);
693
3.06M
    }
694
561
    return;
695
561
  }
696
5
697
5
  // Otherwise emit as fragment.
698
5
  MCDataFragment *DF = getOrCreateDataFragment();
699
5
  flushPendingLabels(DF, DF->getContents().size());
700
5
701
5
  assert(getCurrentSectionOnly() && "need a section");
702
5
  insert(new MCFillFragment(Expr, Size, NumValues, Loc));
703
5
}
704
705
1.72k
void MCObjectStreamer::EmitFileDirective(StringRef Filename) {
706
1.72k
  getAssembler().addFileName(Filename);
707
1.72k
}
708
709
286
void MCObjectStreamer::EmitAddrsig() {
710
286
  getAssembler().getWriter().emitAddrsigSection();
711
286
}
712
713
102
void MCObjectStreamer::EmitAddrsigSym(const MCSymbol *Sym) {
714
102
  getAssembler().registerSymbol(*Sym);
715
102
  getAssembler().getWriter().addAddrsigSymbol(Sym);
716
102
}
717
718
20.4k
void MCObjectStreamer::FinishImpl() {
719
20.4k
  getContext().RemapDebugPaths();
720
20.4k
721
20.4k
  // If we are generating dwarf for assembly source files dump out the sections.
722
20.4k
  if (getContext().getGenDwarfForAssembly())
723
63
    MCGenDwarfInfo::Emit(this);
724
20.4k
725
20.4k
  // Dump out the dwarf file & directory tables and line tables.
726
20.4k
  MCDwarfLineTable::Emit(this, getAssembler().getDWARFLinetableParams());
727
20.4k
728
20.4k
  flushPendingLabels();
729
20.4k
  resolvePendingFixups();
730
20.4k
  getAssembler().Finish();
731
20.4k
}