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/MC/MCAsmBackend.h" |
11 | #include "llvm/MC/MCAsmInfo.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/MCObjectFileInfo.h" |
19 | #include "llvm/MC/MCObjectWriter.h" |
20 | #include "llvm/MC/MCSection.h" |
21 | #include "llvm/MC/MCSymbol.h" |
22 | #include "llvm/MC/MCValue.h" |
23 | #include "llvm/Support/ErrorHandling.h" |
24 | #include "llvm/Support/SourceMgr.h" |
25 | using namespace llvm; |
26 | |
27 | MCObjectStreamer::MCObjectStreamer(MCContext &Context, |
28 | std::unique_ptr<MCAsmBackend> TAB, |
29 | std::unique_ptr<MCObjectWriter> OW, |
30 | std::unique_ptr<MCCodeEmitter> Emitter) |
31 | : MCStreamer(Context), |
32 | Assembler(std::make_unique<MCAssembler>( |
33 | args&: Context, args: std::move(TAB), args: std::move(Emitter), args: std::move(OW))), |
34 | EmitEHFrame(true), EmitDebugFrame(false) { |
35 | if (Assembler->getBackendPtr()) |
36 | setAllowAutoPadding(Assembler->getBackend().allowAutoPadding()); |
37 | } |
38 | |
39 | MCObjectStreamer::~MCObjectStreamer() = default; |
40 | |
41 | // AssemblerPtr is used for evaluation of expressions and causes |
42 | // difference between asm and object outputs. Return nullptr to in |
43 | // inline asm mode to limit divergence to assembly inputs. |
44 | MCAssembler *MCObjectStreamer::getAssemblerPtr() { |
45 | if (getUseAssemblerInfoForParsing()) |
46 | return Assembler.get(); |
47 | return nullptr; |
48 | } |
49 | |
50 | void MCObjectStreamer::addPendingLabel(MCSymbol* S) { |
51 | MCSection *CurSection = getCurrentSectionOnly(); |
52 | if (CurSection) { |
53 | // Register labels that have not yet been assigned to a Section. |
54 | if (!PendingLabels.empty()) { |
55 | for (MCSymbol* Sym : PendingLabels) |
56 | CurSection->addPendingLabel(label: Sym); |
57 | PendingLabels.clear(); |
58 | } |
59 | |
60 | // Add this label to the current Section / Subsection. |
61 | CurSection->addPendingLabel(label: S, Subsection: CurSubsectionIdx); |
62 | |
63 | // Add this Section to the list of PendingLabelSections. |
64 | PendingLabelSections.insert(X: CurSection); |
65 | } else |
66 | // There is no Section / Subsection for this label yet. |
67 | PendingLabels.push_back(Elt: S); |
68 | } |
69 | |
70 | void MCObjectStreamer::flushPendingLabels(MCFragment *F, uint64_t FOffset) { |
71 | assert(F); |
72 | MCSection *CurSection = getCurrentSectionOnly(); |
73 | if (!CurSection) { |
74 | assert(PendingLabels.empty()); |
75 | return; |
76 | } |
77 | // Register labels that have not yet been assigned to a Section. |
78 | if (!PendingLabels.empty()) { |
79 | for (MCSymbol* Sym : PendingLabels) |
80 | CurSection->addPendingLabel(label: Sym, Subsection: CurSubsectionIdx); |
81 | PendingLabels.clear(); |
82 | } |
83 | |
84 | // Associate the labels with F. |
85 | CurSection->flushPendingLabels(F, FOffset, Subsection: CurSubsectionIdx); |
86 | } |
87 | |
88 | void MCObjectStreamer::flushPendingLabels() { |
89 | // Register labels that have not yet been assigned to a Section. |
90 | if (!PendingLabels.empty()) { |
91 | MCSection *CurSection = getCurrentSectionOnly(); |
92 | assert(CurSection); |
93 | for (MCSymbol* Sym : PendingLabels) |
94 | CurSection->addPendingLabel(label: Sym, Subsection: CurSubsectionIdx); |
95 | PendingLabels.clear(); |
96 | } |
97 | |
98 | // Assign an empty data fragment to all remaining pending labels. |
99 | for (MCSection* Section : PendingLabelSections) |
100 | Section->flushPendingLabels(); |
101 | } |
102 | |
103 | // When fixup's offset is a forward declared label, e.g.: |
104 | // |
105 | // .reloc 1f, R_MIPS_JALR, foo |
106 | // 1: nop |
107 | // |
108 | // postpone adding it to Fixups vector until the label is defined and its offset |
109 | // is known. |
110 | void MCObjectStreamer::resolvePendingFixups() { |
111 | for (PendingMCFixup &PendingFixup : PendingFixups) { |
112 | if (!PendingFixup.Sym || PendingFixup.Sym->isUndefined ()) { |
113 | getContext().reportError(L: PendingFixup.Fixup.getLoc(), |
114 | Msg: "unresolved relocation offset" ); |
115 | continue; |
116 | } |
117 | flushPendingLabels(F: PendingFixup.DF, FOffset: PendingFixup.DF->getContents().size()); |
118 | PendingFixup.Fixup.setOffset(PendingFixup.Sym->getOffset() + |
119 | PendingFixup.Fixup.getOffset()); |
120 | |
121 | // If the location symbol to relocate is in MCEncodedFragmentWithFixups, |
122 | // put the Fixup into location symbol's fragment. Otherwise |
123 | // put into PendingFixup.DF |
124 | MCFragment *SymFragment = PendingFixup.Sym->getFragment(); |
125 | switch (SymFragment->getKind()) { |
126 | case MCFragment::FT_Relaxable: |
127 | case MCFragment::FT_Dwarf: |
128 | case MCFragment::FT_PseudoProbe: |
129 | cast<MCEncodedFragmentWithFixups<8, 1>>(Val: SymFragment) |
130 | ->getFixups() |
131 | .push_back(Elt: PendingFixup.Fixup); |
132 | break; |
133 | case MCFragment::FT_Data: |
134 | case MCFragment::FT_CVDefRange: |
135 | cast<MCEncodedFragmentWithFixups<32, 4>>(Val: SymFragment) |
136 | ->getFixups() |
137 | .push_back(Elt: PendingFixup.Fixup); |
138 | break; |
139 | default: |
140 | PendingFixup.DF->getFixups().push_back(Elt: PendingFixup.Fixup); |
141 | break; |
142 | } |
143 | } |
144 | PendingFixups.clear(); |
145 | } |
146 | |
147 | // As a compile-time optimization, avoid allocating and evaluating an MCExpr |
148 | // tree for (Hi - Lo) when Hi and Lo are offsets into the same fragment. |
149 | static std::optional<uint64_t> absoluteSymbolDiff(const MCSymbol *Hi, |
150 | const MCSymbol *Lo) { |
151 | assert(Hi && Lo); |
152 | if (!Hi->getFragment() || Hi->getFragment() != Lo->getFragment() || |
153 | Hi->isVariable() || Lo->isVariable()) |
154 | return std::nullopt; |
155 | |
156 | return Hi->getOffset() - Lo->getOffset(); |
157 | } |
158 | |
159 | void MCObjectStreamer::emitAbsoluteSymbolDiff(const MCSymbol *Hi, |
160 | const MCSymbol *Lo, |
161 | unsigned Size) { |
162 | if (!getAssembler().getContext().getTargetTriple().isRISCV()) |
163 | if (std::optional<uint64_t> Diff = absoluteSymbolDiff(Hi, Lo)) |
164 | return emitIntValue(Value: *Diff, Size); |
165 | MCStreamer::emitAbsoluteSymbolDiff(Hi, Lo, Size); |
166 | } |
167 | |
168 | void MCObjectStreamer::emitAbsoluteSymbolDiffAsULEB128(const MCSymbol *Hi, |
169 | const MCSymbol *Lo) { |
170 | if (!getAssembler().getContext().getTargetTriple().isRISCV()) |
171 | if (std::optional<uint64_t> Diff = absoluteSymbolDiff(Hi, Lo)) { |
172 | emitULEB128IntValue(Value: *Diff); |
173 | return; |
174 | } |
175 | MCStreamer::emitAbsoluteSymbolDiffAsULEB128(Hi, Lo); |
176 | } |
177 | |
178 | void MCObjectStreamer::reset() { |
179 | if (Assembler) |
180 | Assembler->reset(); |
181 | CurInsertionPoint = MCSection::iterator(); |
182 | EmitEHFrame = true; |
183 | EmitDebugFrame = false; |
184 | PendingLabels.clear(); |
185 | PendingLabelSections.clear(); |
186 | MCStreamer::reset(); |
187 | } |
188 | |
189 | void MCObjectStreamer::emitFrames(MCAsmBackend *MAB) { |
190 | if (!getNumFrameInfos()) |
191 | return; |
192 | |
193 | if (EmitEHFrame) |
194 | MCDwarfFrameEmitter::Emit(streamer&: *this, MAB, isEH: true); |
195 | |
196 | if (EmitDebugFrame) |
197 | MCDwarfFrameEmitter::Emit(streamer&: *this, MAB, isEH: false); |
198 | } |
199 | |
200 | MCFragment *MCObjectStreamer::getCurrentFragment() const { |
201 | assert(getCurrentSectionOnly() && "No current section!" ); |
202 | |
203 | if (CurInsertionPoint != getCurrentSectionOnly()->getFragmentList().begin()) |
204 | return &*std::prev(x: CurInsertionPoint); |
205 | |
206 | return nullptr; |
207 | } |
208 | |
209 | static bool canReuseDataFragment(const MCDataFragment &F, |
210 | const MCAssembler &Assembler, |
211 | const MCSubtargetInfo *STI) { |
212 | if (!F.hasInstructions()) |
213 | return true; |
214 | // Do not add data after a linker-relaxable instruction. The difference |
215 | // between a new label and a label at or before the linker-relaxable |
216 | // instruction cannot be resolved at assemble-time. |
217 | if (F.isLinkerRelaxable()) |
218 | return false; |
219 | // When bundling is enabled, we don't want to add data to a fragment that |
220 | // already has instructions (see MCELFStreamer::emitInstToData for details) |
221 | if (Assembler.isBundlingEnabled()) |
222 | return Assembler.getRelaxAll(); |
223 | // If the subtarget is changed mid fragment we start a new fragment to record |
224 | // the new STI. |
225 | return !STI || F.getSubtargetInfo() == STI; |
226 | } |
227 | |
228 | MCDataFragment * |
229 | MCObjectStreamer::getOrCreateDataFragment(const MCSubtargetInfo *STI) { |
230 | MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(Val: getCurrentFragment()); |
231 | if (!F || !canReuseDataFragment(F: *F, Assembler: *Assembler, STI)) { |
232 | F = new MCDataFragment(); |
233 | insert(F); |
234 | } |
235 | return F; |
236 | } |
237 | |
238 | void MCObjectStreamer::visitUsedSymbol(const MCSymbol &Sym) { |
239 | Assembler->registerSymbol(Symbol: Sym); |
240 | } |
241 | |
242 | void MCObjectStreamer::emitCFISections(bool EH, bool Debug) { |
243 | MCStreamer::emitCFISections(EH, Debug); |
244 | EmitEHFrame = EH; |
245 | EmitDebugFrame = Debug; |
246 | } |
247 | |
248 | void MCObjectStreamer::emitValueImpl(const MCExpr *Value, unsigned Size, |
249 | SMLoc Loc) { |
250 | MCStreamer::emitValueImpl(Value, Size, Loc); |
251 | MCDataFragment *DF = getOrCreateDataFragment(); |
252 | flushPendingLabels(F: DF, FOffset: DF->getContents().size()); |
253 | |
254 | MCDwarfLineEntry::make(MCOS: this, Section: getCurrentSectionOnly()); |
255 | |
256 | // Avoid fixups when possible. |
257 | int64_t AbsValue; |
258 | if (Value->evaluateAsAbsolute(Res&: AbsValue, Asm: getAssemblerPtr())) { |
259 | if (!isUIntN(N: 8 * Size, x: AbsValue) && !isIntN(N: 8 * Size, x: AbsValue)) { |
260 | getContext().reportError( |
261 | L: Loc, Msg: "value evaluated as " + Twine(AbsValue) + " is out of range." ); |
262 | return; |
263 | } |
264 | emitIntValue(Value: AbsValue, Size); |
265 | return; |
266 | } |
267 | DF->getFixups().push_back( |
268 | Elt: MCFixup::create(Offset: DF->getContents().size(), Value, |
269 | Kind: MCFixup::getKindForSize(Size, IsPCRel: false), Loc)); |
270 | DF->getContents().resize(N: DF->getContents().size() + Size, NV: 0); |
271 | } |
272 | |
273 | MCSymbol *MCObjectStreamer::emitCFILabel() { |
274 | MCSymbol *Label = getContext().createTempSymbol(Name: "cfi" ); |
275 | emitLabel(Symbol: Label); |
276 | return Label; |
277 | } |
278 | |
279 | void MCObjectStreamer::emitCFIStartProcImpl(MCDwarfFrameInfo &Frame) { |
280 | // We need to create a local symbol to avoid relocations. |
281 | Frame.Begin = getContext().createTempSymbol(); |
282 | emitLabel(Symbol: Frame.Begin); |
283 | } |
284 | |
285 | void MCObjectStreamer::emitCFIEndProcImpl(MCDwarfFrameInfo &Frame) { |
286 | Frame.End = getContext().createTempSymbol(); |
287 | emitLabel(Symbol: Frame.End); |
288 | } |
289 | |
290 | void MCObjectStreamer::emitLabel(MCSymbol *Symbol, SMLoc Loc) { |
291 | MCStreamer::emitLabel(Symbol, Loc); |
292 | |
293 | getAssembler().registerSymbol(Symbol: *Symbol); |
294 | |
295 | // If there is a current fragment, mark the symbol as pointing into it. |
296 | // Otherwise queue the label and set its fragment pointer when we emit the |
297 | // next fragment. |
298 | auto *F = dyn_cast_or_null<MCDataFragment>(Val: getCurrentFragment()); |
299 | if (F && !(getAssembler().isBundlingEnabled() && |
300 | getAssembler().getRelaxAll())) { |
301 | Symbol->setFragment(F); |
302 | Symbol->setOffset(F->getContents().size()); |
303 | } else { |
304 | // Assign all pending labels to offset 0 within the dummy "pending" |
305 | // fragment. (They will all be reassigned to a real fragment in |
306 | // flushPendingLabels()) |
307 | Symbol->setOffset(0); |
308 | addPendingLabel(S: Symbol); |
309 | } |
310 | |
311 | emitPendingAssignments(Symbol); |
312 | } |
313 | |
314 | void MCObjectStreamer::emitPendingAssignments(MCSymbol *Symbol) { |
315 | auto Assignments = pendingAssignments.find(Val: Symbol); |
316 | if (Assignments != pendingAssignments.end()) { |
317 | for (const PendingAssignment &A : Assignments->second) |
318 | emitAssignment(Symbol: A.Symbol, Value: A.Value); |
319 | |
320 | pendingAssignments.erase(I: Assignments); |
321 | } |
322 | } |
323 | |
324 | // Emit a label at a previously emitted fragment/offset position. This must be |
325 | // within the currently-active section. |
326 | void MCObjectStreamer::emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc, |
327 | MCFragment *F, uint64_t Offset) { |
328 | assert(F->getParent() == getCurrentSectionOnly()); |
329 | |
330 | MCStreamer::emitLabel(Symbol, Loc); |
331 | getAssembler().registerSymbol(Symbol: *Symbol); |
332 | auto *DF = dyn_cast_or_null<MCDataFragment>(Val: F); |
333 | Symbol->setOffset(Offset); |
334 | if (DF) { |
335 | Symbol->setFragment(F); |
336 | } else { |
337 | assert(isa<MCDummyFragment>(F) && |
338 | "F must either be an MCDataFragment or the pending MCDummyFragment" ); |
339 | assert(Offset == 0); |
340 | addPendingLabel(S: Symbol); |
341 | } |
342 | } |
343 | |
344 | void MCObjectStreamer::emitULEB128Value(const MCExpr *Value) { |
345 | int64_t IntValue; |
346 | if (Value->evaluateAsAbsolute(Res&: IntValue, Asm: getAssemblerPtr())) { |
347 | emitULEB128IntValue(Value: IntValue); |
348 | return; |
349 | } |
350 | insert(F: new MCLEBFragment(*Value, false)); |
351 | } |
352 | |
353 | void MCObjectStreamer::emitSLEB128Value(const MCExpr *Value) { |
354 | int64_t IntValue; |
355 | if (Value->evaluateAsAbsolute(Res&: IntValue, Asm: getAssemblerPtr())) { |
356 | emitSLEB128IntValue(Value: IntValue); |
357 | return; |
358 | } |
359 | insert(F: new MCLEBFragment(*Value, true)); |
360 | } |
361 | |
362 | void MCObjectStreamer::emitWeakReference(MCSymbol *Alias, |
363 | const MCSymbol *Symbol) { |
364 | report_fatal_error(reason: "This file format doesn't support weak aliases." ); |
365 | } |
366 | |
367 | void MCObjectStreamer::changeSection(MCSection *Section, |
368 | const MCExpr *Subsection) { |
369 | changeSectionImpl(Section, Subsection); |
370 | } |
371 | |
372 | bool MCObjectStreamer::changeSectionImpl(MCSection *Section, |
373 | const MCExpr *Subsection) { |
374 | assert(Section && "Cannot switch to a null section!" ); |
375 | getContext().clearDwarfLocSeen(); |
376 | |
377 | bool Created = getAssembler().registerSection(Section&: *Section); |
378 | |
379 | int64_t IntSubsection = 0; |
380 | if (Subsection && |
381 | !Subsection->evaluateAsAbsolute(Res&: IntSubsection, Asm: getAssemblerPtr())) { |
382 | getContext().reportError(L: Subsection->getLoc(), |
383 | Msg: "cannot evaluate subsection number" ); |
384 | } |
385 | if (!isUInt<31>(x: IntSubsection)) { |
386 | getContext().reportError(L: Subsection->getLoc(), |
387 | Msg: "subsection number " + Twine(IntSubsection) + |
388 | " is not within [0,2147483647]" ); |
389 | } |
390 | |
391 | CurSubsectionIdx = unsigned(IntSubsection); |
392 | CurInsertionPoint = |
393 | Section->getSubsectionInsertionPoint(Subsection: CurSubsectionIdx); |
394 | return Created; |
395 | } |
396 | |
397 | void MCObjectStreamer::emitAssignment(MCSymbol *Symbol, const MCExpr *Value) { |
398 | getAssembler().registerSymbol(Symbol: *Symbol); |
399 | MCStreamer::emitAssignment(Symbol, Value); |
400 | emitPendingAssignments(Symbol); |
401 | } |
402 | |
403 | void MCObjectStreamer::emitConditionalAssignment(MCSymbol *Symbol, |
404 | const MCExpr *Value) { |
405 | const MCSymbol *Target = &cast<MCSymbolRefExpr>(Val: *Value).getSymbol(); |
406 | |
407 | // If the symbol already exists, emit the assignment. Otherwise, emit it |
408 | // later only if the symbol is also emitted. |
409 | if (Target->isRegistered()) |
410 | emitAssignment(Symbol, Value); |
411 | else |
412 | pendingAssignments[Target].push_back(Elt: {.Symbol: Symbol, .Value: Value}); |
413 | } |
414 | |
415 | bool MCObjectStreamer::mayHaveInstructions(MCSection &Sec) const { |
416 | return Sec.hasInstructions(); |
417 | } |
418 | |
419 | void MCObjectStreamer::emitInstruction(const MCInst &Inst, |
420 | const MCSubtargetInfo &STI) { |
421 | const MCSection &Sec = *getCurrentSectionOnly(); |
422 | if (Sec.isVirtualSection()) { |
423 | getContext().reportError(L: Inst.getLoc(), Msg: Twine(Sec.getVirtualSectionKind()) + |
424 | " section '" + Sec.getName() + |
425 | "' cannot have instructions" ); |
426 | return; |
427 | } |
428 | getAssembler().getBackend().emitInstructionBegin(OS&: *this, Inst, STI); |
429 | emitInstructionImpl(Inst, STI); |
430 | getAssembler().getBackend().emitInstructionEnd(OS&: *this, Inst); |
431 | } |
432 | |
433 | void MCObjectStreamer::emitInstructionImpl(const MCInst &Inst, |
434 | const MCSubtargetInfo &STI) { |
435 | MCStreamer::emitInstruction(Inst, STI); |
436 | |
437 | MCSection *Sec = getCurrentSectionOnly(); |
438 | Sec->setHasInstructions(true); |
439 | |
440 | // Now that a machine instruction has been assembled into this section, make |
441 | // a line entry for any .loc directive that has been seen. |
442 | MCDwarfLineEntry::make(MCOS: this, Section: getCurrentSectionOnly()); |
443 | |
444 | // If this instruction doesn't need relaxation, just emit it as data. |
445 | MCAssembler &Assembler = getAssembler(); |
446 | MCAsmBackend &Backend = Assembler.getBackend(); |
447 | if (!(Backend.mayNeedRelaxation(Inst, STI) || |
448 | Backend.allowEnhancedRelaxation())) { |
449 | emitInstToData(Inst, STI); |
450 | return; |
451 | } |
452 | |
453 | // Otherwise, relax and emit it as data if either: |
454 | // - The RelaxAll flag was passed |
455 | // - Bundling is enabled and this instruction is inside a bundle-locked |
456 | // group. We want to emit all such instructions into the same data |
457 | // fragment. |
458 | if (Assembler.getRelaxAll() || |
459 | (Assembler.isBundlingEnabled() && Sec->isBundleLocked())) { |
460 | MCInst Relaxed = Inst; |
461 | while (Backend.mayNeedRelaxation(Inst: Relaxed, STI)) |
462 | Backend.relaxInstruction(Inst&: Relaxed, STI); |
463 | emitInstToData(Inst: Relaxed, STI); |
464 | return; |
465 | } |
466 | |
467 | // Otherwise emit to a separate fragment. |
468 | emitInstToFragment(Inst, STI); |
469 | } |
470 | |
471 | void MCObjectStreamer::emitInstToFragment(const MCInst &Inst, |
472 | const MCSubtargetInfo &STI) { |
473 | if (getAssembler().getRelaxAll() && getAssembler().isBundlingEnabled()) |
474 | llvm_unreachable("All instructions should have already been relaxed" ); |
475 | |
476 | // Always create a new, separate fragment here, because its size can change |
477 | // during relaxation. |
478 | MCRelaxableFragment *IF = new MCRelaxableFragment(Inst, STI); |
479 | insert(F: IF); |
480 | |
481 | SmallString<128> Code; |
482 | getAssembler().getEmitter().encodeInstruction(Inst, CB&: Code, Fixups&: IF->getFixups(), |
483 | STI); |
484 | IF->getContents().append(in_start: Code.begin(), in_end: Code.end()); |
485 | } |
486 | |
487 | #ifndef NDEBUG |
488 | static const char *const BundlingNotImplementedMsg = |
489 | "Aligned bundling is not implemented for this object format" ; |
490 | #endif |
491 | |
492 | void MCObjectStreamer::emitBundleAlignMode(Align Alignment) { |
493 | llvm_unreachable(BundlingNotImplementedMsg); |
494 | } |
495 | |
496 | void MCObjectStreamer::emitBundleLock(bool AlignToEnd) { |
497 | llvm_unreachable(BundlingNotImplementedMsg); |
498 | } |
499 | |
500 | void MCObjectStreamer::emitBundleUnlock() { |
501 | llvm_unreachable(BundlingNotImplementedMsg); |
502 | } |
503 | |
504 | void MCObjectStreamer::emitDwarfLocDirective(unsigned FileNo, unsigned Line, |
505 | unsigned Column, unsigned Flags, |
506 | unsigned Isa, |
507 | unsigned Discriminator, |
508 | StringRef FileName) { |
509 | // In case we see two .loc directives in a row, make sure the |
510 | // first one gets a line entry. |
511 | MCDwarfLineEntry::make(MCOS: this, Section: getCurrentSectionOnly()); |
512 | |
513 | this->MCStreamer::emitDwarfLocDirective(FileNo, Line, Column, Flags, Isa, |
514 | Discriminator, FileName); |
515 | } |
516 | |
517 | static const MCExpr *buildSymbolDiff(MCObjectStreamer &OS, const MCSymbol *A, |
518 | const MCSymbol *B, SMLoc Loc) { |
519 | MCContext &Context = OS.getContext(); |
520 | MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
521 | const MCExpr *ARef = MCSymbolRefExpr::create(Symbol: A, Kind: Variant, Ctx&: Context); |
522 | const MCExpr *BRef = MCSymbolRefExpr::create(Symbol: B, Kind: Variant, Ctx&: Context); |
523 | const MCExpr *AddrDelta = |
524 | MCBinaryExpr::create(Op: MCBinaryExpr::Sub, LHS: ARef, RHS: BRef, Ctx&: Context, Loc); |
525 | return AddrDelta; |
526 | } |
527 | |
528 | static void emitDwarfSetLineAddr(MCObjectStreamer &OS, |
529 | MCDwarfLineTableParams Params, |
530 | int64_t LineDelta, const MCSymbol *Label, |
531 | int PointerSize) { |
532 | // emit the sequence to set the address |
533 | OS.emitIntValue(Value: dwarf::DW_LNS_extended_op, Size: 1); |
534 | OS.emitULEB128IntValue(Value: PointerSize + 1); |
535 | OS.emitIntValue(Value: dwarf::DW_LNE_set_address, Size: 1); |
536 | OS.emitSymbolValue(Sym: Label, Size: PointerSize); |
537 | |
538 | // emit the sequence for the LineDelta (from 1) and a zero address delta. |
539 | MCDwarfLineAddr::Emit(MCOS: &OS, Params, LineDelta, AddrDelta: 0); |
540 | } |
541 | |
542 | void MCObjectStreamer::emitDwarfAdvanceLineAddr(int64_t LineDelta, |
543 | const MCSymbol *LastLabel, |
544 | const MCSymbol *Label, |
545 | unsigned PointerSize) { |
546 | if (!LastLabel) { |
547 | emitDwarfSetLineAddr(OS&: *this, Params: Assembler->getDWARFLinetableParams(), LineDelta, |
548 | Label, PointerSize); |
549 | return; |
550 | } |
551 | const MCExpr *AddrDelta = buildSymbolDiff(OS&: *this, A: Label, B: LastLabel, Loc: SMLoc()); |
552 | insert(F: new MCDwarfLineAddrFragment(LineDelta, *AddrDelta)); |
553 | } |
554 | |
555 | void MCObjectStreamer::emitDwarfLineEndEntry(MCSection *Section, |
556 | MCSymbol *LastLabel) { |
557 | // Emit a DW_LNE_end_sequence for the end of the section. |
558 | // Use the section end label to compute the address delta and use INT64_MAX |
559 | // as the line delta which is the signal that this is actually a |
560 | // DW_LNE_end_sequence. |
561 | MCSymbol *SectionEnd = endSection(Section); |
562 | |
563 | // Switch back the dwarf line section, in case endSection had to switch the |
564 | // section. |
565 | MCContext &Ctx = getContext(); |
566 | switchSection(Section: Ctx.getObjectFileInfo()->getDwarfLineSection()); |
567 | |
568 | const MCAsmInfo *AsmInfo = Ctx.getAsmInfo(); |
569 | emitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, Label: SectionEnd, |
570 | PointerSize: AsmInfo->getCodePointerSize()); |
571 | } |
572 | |
573 | void MCObjectStreamer::emitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel, |
574 | const MCSymbol *Label, |
575 | SMLoc Loc) { |
576 | const MCExpr *AddrDelta = buildSymbolDiff(OS&: *this, A: Label, B: LastLabel, Loc); |
577 | insert(F: new MCDwarfCallFrameFragment(*AddrDelta, nullptr)); |
578 | } |
579 | |
580 | void MCObjectStreamer::emitCVLocDirective(unsigned FunctionId, unsigned FileNo, |
581 | unsigned Line, unsigned Column, |
582 | bool PrologueEnd, bool IsStmt, |
583 | StringRef FileName, SMLoc Loc) { |
584 | // Validate the directive. |
585 | if (!checkCVLocSection(FuncId: FunctionId, FileNo, Loc)) |
586 | return; |
587 | |
588 | // Emit a label at the current position and record it in the CodeViewContext. |
589 | MCSymbol *LineSym = getContext().createTempSymbol(); |
590 | emitLabel(Symbol: LineSym); |
591 | getContext().getCVContext().recordCVLoc(Ctx&: getContext(), Label: LineSym, FunctionId, |
592 | FileNo, Line, Column, PrologueEnd, |
593 | IsStmt); |
594 | } |
595 | |
596 | void MCObjectStreamer::emitCVLinetableDirective(unsigned FunctionId, |
597 | const MCSymbol *Begin, |
598 | const MCSymbol *End) { |
599 | getContext().getCVContext().emitLineTableForFunction(OS&: *this, FuncId: FunctionId, FuncBegin: Begin, |
600 | FuncEnd: End); |
601 | this->MCStreamer::emitCVLinetableDirective(FunctionId, FnStart: Begin, FnEnd: End); |
602 | } |
603 | |
604 | void MCObjectStreamer::emitCVInlineLinetableDirective( |
605 | unsigned PrimaryFunctionId, unsigned SourceFileId, unsigned SourceLineNum, |
606 | const MCSymbol *FnStartSym, const MCSymbol *FnEndSym) { |
607 | getContext().getCVContext().emitInlineLineTableForFunction( |
608 | OS&: *this, PrimaryFunctionId, SourceFileId, SourceLineNum, FnStartSym, |
609 | FnEndSym); |
610 | this->MCStreamer::emitCVInlineLinetableDirective( |
611 | PrimaryFunctionId, SourceFileId, SourceLineNum, FnStartSym, FnEndSym); |
612 | } |
613 | |
614 | void MCObjectStreamer::emitCVDefRangeDirective( |
615 | ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges, |
616 | StringRef FixedSizePortion) { |
617 | MCFragment *Frag = |
618 | getContext().getCVContext().emitDefRange(OS&: *this, Ranges, FixedSizePortion); |
619 | // Attach labels that were pending before we created the defrange fragment to |
620 | // the beginning of the new fragment. |
621 | flushPendingLabels(F: Frag, FOffset: 0); |
622 | this->MCStreamer::emitCVDefRangeDirective(Ranges, FixedSizePortion); |
623 | } |
624 | |
625 | void MCObjectStreamer::emitCVStringTableDirective() { |
626 | getContext().getCVContext().emitStringTable(OS&: *this); |
627 | } |
628 | void MCObjectStreamer::emitCVFileChecksumsDirective() { |
629 | getContext().getCVContext().emitFileChecksums(OS&: *this); |
630 | } |
631 | |
632 | void MCObjectStreamer::emitCVFileChecksumOffsetDirective(unsigned FileNo) { |
633 | getContext().getCVContext().emitFileChecksumOffset(OS&: *this, FileNo); |
634 | } |
635 | |
636 | void MCObjectStreamer::emitBytes(StringRef Data) { |
637 | MCDwarfLineEntry::make(MCOS: this, Section: getCurrentSectionOnly()); |
638 | MCDataFragment *DF = getOrCreateDataFragment(); |
639 | flushPendingLabels(F: DF, FOffset: DF->getContents().size()); |
640 | DF->getContents().append(in_start: Data.begin(), in_end: Data.end()); |
641 | } |
642 | |
643 | void MCObjectStreamer::emitValueToAlignment(Align Alignment, int64_t Value, |
644 | unsigned ValueSize, |
645 | unsigned MaxBytesToEmit) { |
646 | if (MaxBytesToEmit == 0) |
647 | MaxBytesToEmit = Alignment.value(); |
648 | insert(F: new MCAlignFragment(Alignment, Value, ValueSize, MaxBytesToEmit)); |
649 | |
650 | // Update the maximum alignment on the current section if necessary. |
651 | MCSection *CurSec = getCurrentSectionOnly(); |
652 | CurSec->ensureMinAlignment(MinAlignment: Alignment); |
653 | } |
654 | |
655 | void MCObjectStreamer::emitCodeAlignment(Align Alignment, |
656 | const MCSubtargetInfo *STI, |
657 | unsigned MaxBytesToEmit) { |
658 | emitValueToAlignment(Alignment, Value: 0, ValueSize: 1, MaxBytesToEmit); |
659 | cast<MCAlignFragment>(Val: getCurrentFragment())->setEmitNops(Value: true, STI); |
660 | } |
661 | |
662 | void MCObjectStreamer::emitValueToOffset(const MCExpr *Offset, |
663 | unsigned char Value, |
664 | SMLoc Loc) { |
665 | insert(F: new MCOrgFragment(*Offset, Value, Loc)); |
666 | } |
667 | |
668 | // Associate DTPRel32 fixup with data and resize data area |
669 | void MCObjectStreamer::emitDTPRel32Value(const MCExpr *Value) { |
670 | MCDataFragment *DF = getOrCreateDataFragment(); |
671 | flushPendingLabels(F: DF, FOffset: DF->getContents().size()); |
672 | |
673 | DF->getFixups().push_back(Elt: MCFixup::create(Offset: DF->getContents().size(), |
674 | Value, Kind: FK_DTPRel_4)); |
675 | DF->getContents().resize(N: DF->getContents().size() + 4, NV: 0); |
676 | } |
677 | |
678 | // Associate DTPRel64 fixup with data and resize data area |
679 | void MCObjectStreamer::emitDTPRel64Value(const MCExpr *Value) { |
680 | MCDataFragment *DF = getOrCreateDataFragment(); |
681 | flushPendingLabels(F: DF, FOffset: DF->getContents().size()); |
682 | |
683 | DF->getFixups().push_back(Elt: MCFixup::create(Offset: DF->getContents().size(), |
684 | Value, Kind: FK_DTPRel_8)); |
685 | DF->getContents().resize(N: DF->getContents().size() + 8, NV: 0); |
686 | } |
687 | |
688 | // Associate TPRel32 fixup with data and resize data area |
689 | void MCObjectStreamer::emitTPRel32Value(const MCExpr *Value) { |
690 | MCDataFragment *DF = getOrCreateDataFragment(); |
691 | flushPendingLabels(F: DF, FOffset: DF->getContents().size()); |
692 | |
693 | DF->getFixups().push_back(Elt: MCFixup::create(Offset: DF->getContents().size(), |
694 | Value, Kind: FK_TPRel_4)); |
695 | DF->getContents().resize(N: DF->getContents().size() + 4, NV: 0); |
696 | } |
697 | |
698 | // Associate TPRel64 fixup with data and resize data area |
699 | void MCObjectStreamer::emitTPRel64Value(const MCExpr *Value) { |
700 | MCDataFragment *DF = getOrCreateDataFragment(); |
701 | flushPendingLabels(F: DF, FOffset: DF->getContents().size()); |
702 | |
703 | DF->getFixups().push_back(Elt: MCFixup::create(Offset: DF->getContents().size(), |
704 | Value, Kind: FK_TPRel_8)); |
705 | DF->getContents().resize(N: DF->getContents().size() + 8, NV: 0); |
706 | } |
707 | |
708 | // Associate GPRel32 fixup with data and resize data area |
709 | void MCObjectStreamer::emitGPRel32Value(const MCExpr *Value) { |
710 | MCDataFragment *DF = getOrCreateDataFragment(); |
711 | flushPendingLabels(F: DF, FOffset: DF->getContents().size()); |
712 | |
713 | DF->getFixups().push_back( |
714 | Elt: MCFixup::create(Offset: DF->getContents().size(), Value, Kind: FK_GPRel_4)); |
715 | DF->getContents().resize(N: DF->getContents().size() + 4, NV: 0); |
716 | } |
717 | |
718 | // Associate GPRel64 fixup with data and resize data area |
719 | void MCObjectStreamer::emitGPRel64Value(const MCExpr *Value) { |
720 | MCDataFragment *DF = getOrCreateDataFragment(); |
721 | flushPendingLabels(F: DF, FOffset: DF->getContents().size()); |
722 | |
723 | DF->getFixups().push_back( |
724 | Elt: MCFixup::create(Offset: DF->getContents().size(), Value, Kind: FK_GPRel_4)); |
725 | DF->getContents().resize(N: DF->getContents().size() + 8, NV: 0); |
726 | } |
727 | |
728 | static std::optional<std::pair<bool, std::string>> |
729 | getOffsetAndDataFragment(const MCSymbol &Symbol, uint32_t &RelocOffset, |
730 | MCDataFragment *&DF) { |
731 | if (Symbol.isVariable()) { |
732 | const MCExpr *SymbolExpr = Symbol.getVariableValue(); |
733 | MCValue OffsetVal; |
734 | if(!SymbolExpr->evaluateAsRelocatable(Res&: OffsetVal, Layout: nullptr, Fixup: nullptr)) |
735 | return std::make_pair(x: false, |
736 | y: std::string("symbol in .reloc offset is not " |
737 | "relocatable" )); |
738 | if (OffsetVal.isAbsolute()) { |
739 | RelocOffset = OffsetVal.getConstant(); |
740 | MCFragment *Fragment = Symbol.getFragment(); |
741 | // FIXME Support symbols with no DF. For example: |
742 | // .reloc .data, ENUM_VALUE, <some expr> |
743 | if (!Fragment || Fragment->getKind() != MCFragment::FT_Data) |
744 | return std::make_pair(x: false, |
745 | y: std::string("symbol in offset has no data " |
746 | "fragment" )); |
747 | DF = cast<MCDataFragment>(Val: Fragment); |
748 | return std::nullopt; |
749 | } |
750 | |
751 | if (OffsetVal.getSymB()) |
752 | return std::make_pair(x: false, |
753 | y: std::string(".reloc symbol offset is not " |
754 | "representable" )); |
755 | |
756 | const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(Val: *OffsetVal.getSymA()); |
757 | if (!SRE.getSymbol().isDefined()) |
758 | return std::make_pair(x: false, |
759 | y: std::string("symbol used in the .reloc offset is " |
760 | "not defined" )); |
761 | |
762 | if (SRE.getSymbol().isVariable()) |
763 | return std::make_pair(x: false, |
764 | y: std::string("symbol used in the .reloc offset is " |
765 | "variable" )); |
766 | |
767 | MCFragment *Fragment = SRE.getSymbol().getFragment(); |
768 | // FIXME Support symbols with no DF. For example: |
769 | // .reloc .data, ENUM_VALUE, <some expr> |
770 | if (!Fragment || Fragment->getKind() != MCFragment::FT_Data) |
771 | return std::make_pair(x: false, |
772 | y: std::string("symbol in offset has no data " |
773 | "fragment" )); |
774 | RelocOffset = SRE.getSymbol().getOffset() + OffsetVal.getConstant(); |
775 | DF = cast<MCDataFragment>(Val: Fragment); |
776 | } else { |
777 | RelocOffset = Symbol.getOffset(); |
778 | MCFragment *Fragment = Symbol.getFragment(); |
779 | // FIXME Support symbols with no DF. For example: |
780 | // .reloc .data, ENUM_VALUE, <some expr> |
781 | if (!Fragment || Fragment->getKind() != MCFragment::FT_Data) |
782 | return std::make_pair(x: false, |
783 | y: std::string("symbol in offset has no data " |
784 | "fragment" )); |
785 | DF = cast<MCDataFragment>(Val: Fragment); |
786 | } |
787 | return std::nullopt; |
788 | } |
789 | |
790 | std::optional<std::pair<bool, std::string>> |
791 | MCObjectStreamer::emitRelocDirective(const MCExpr &Offset, StringRef Name, |
792 | const MCExpr *Expr, SMLoc Loc, |
793 | const MCSubtargetInfo &STI) { |
794 | std::optional<MCFixupKind> MaybeKind = |
795 | Assembler->getBackend().getFixupKind(Name); |
796 | if (!MaybeKind) |
797 | return std::make_pair(x: true, y: std::string("unknown relocation name" )); |
798 | |
799 | MCFixupKind Kind = *MaybeKind; |
800 | if (Expr) |
801 | visitUsedExpr(Expr: *Expr); |
802 | else |
803 | Expr = |
804 | MCSymbolRefExpr::create(Symbol: getContext().createTempSymbol(), Ctx&: getContext()); |
805 | |
806 | MCDataFragment *DF = getOrCreateDataFragment(STI: &STI); |
807 | flushPendingLabels(F: DF, FOffset: DF->getContents().size()); |
808 | |
809 | MCValue OffsetVal; |
810 | if (!Offset.evaluateAsRelocatable(Res&: OffsetVal, Layout: nullptr, Fixup: nullptr)) |
811 | return std::make_pair(x: false, |
812 | y: std::string(".reloc offset is not relocatable" )); |
813 | if (OffsetVal.isAbsolute()) { |
814 | if (OffsetVal.getConstant() < 0) |
815 | return std::make_pair(x: false, y: std::string(".reloc offset is negative" )); |
816 | DF->getFixups().push_back( |
817 | Elt: MCFixup::create(Offset: OffsetVal.getConstant(), Value: Expr, Kind, Loc)); |
818 | return std::nullopt; |
819 | } |
820 | if (OffsetVal.getSymB()) |
821 | return std::make_pair(x: false, |
822 | y: std::string(".reloc offset is not representable" )); |
823 | |
824 | const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(Val: *OffsetVal.getSymA()); |
825 | const MCSymbol &Symbol = SRE.getSymbol(); |
826 | if (Symbol.isDefined()) { |
827 | uint32_t SymbolOffset = 0; |
828 | std::optional<std::pair<bool, std::string>> Error = |
829 | getOffsetAndDataFragment(Symbol, RelocOffset&: SymbolOffset, DF); |
830 | |
831 | if (Error != std::nullopt) |
832 | return Error; |
833 | |
834 | DF->getFixups().push_back( |
835 | Elt: MCFixup::create(Offset: SymbolOffset + OffsetVal.getConstant(), |
836 | Value: Expr, Kind, Loc)); |
837 | return std::nullopt; |
838 | } |
839 | |
840 | PendingFixups.emplace_back( |
841 | Args: &SRE.getSymbol(), Args&: DF, |
842 | Args: MCFixup::create(Offset: OffsetVal.getConstant(), Value: Expr, Kind, Loc)); |
843 | return std::nullopt; |
844 | } |
845 | |
846 | void MCObjectStreamer::emitFill(const MCExpr &NumBytes, uint64_t FillValue, |
847 | SMLoc Loc) { |
848 | MCDataFragment *DF = getOrCreateDataFragment(); |
849 | flushPendingLabels(F: DF, FOffset: DF->getContents().size()); |
850 | |
851 | assert(getCurrentSectionOnly() && "need a section" ); |
852 | insert(F: new MCFillFragment(FillValue, 1, NumBytes, Loc)); |
853 | } |
854 | |
855 | void MCObjectStreamer::emitFill(const MCExpr &NumValues, int64_t Size, |
856 | int64_t Expr, SMLoc Loc) { |
857 | int64_t IntNumValues; |
858 | // Do additional checking now if we can resolve the value. |
859 | if (NumValues.evaluateAsAbsolute(Res&: IntNumValues, Asm: getAssemblerPtr())) { |
860 | if (IntNumValues < 0) { |
861 | getContext().getSourceManager()->PrintMessage( |
862 | Loc, Kind: SourceMgr::DK_Warning, |
863 | Msg: "'.fill' directive with negative repeat count has no effect" ); |
864 | return; |
865 | } |
866 | // Emit now if we can for better errors. |
867 | int64_t NonZeroSize = Size > 4 ? 4 : Size; |
868 | Expr &= ~0ULL >> (64 - NonZeroSize * 8); |
869 | for (uint64_t i = 0, e = IntNumValues; i != e; ++i) { |
870 | emitIntValue(Value: Expr, Size: NonZeroSize); |
871 | if (NonZeroSize < Size) |
872 | emitIntValue(Value: 0, Size: Size - NonZeroSize); |
873 | } |
874 | return; |
875 | } |
876 | |
877 | // Otherwise emit as fragment. |
878 | MCDataFragment *DF = getOrCreateDataFragment(); |
879 | flushPendingLabels(F: DF, FOffset: DF->getContents().size()); |
880 | |
881 | assert(getCurrentSectionOnly() && "need a section" ); |
882 | insert(F: new MCFillFragment(Expr, Size, NumValues, Loc)); |
883 | } |
884 | |
885 | void MCObjectStreamer::emitNops(int64_t NumBytes, int64_t ControlledNopLength, |
886 | SMLoc Loc, const MCSubtargetInfo &STI) { |
887 | // Emit an NOP fragment. |
888 | MCDataFragment *DF = getOrCreateDataFragment(); |
889 | flushPendingLabels(F: DF, FOffset: DF->getContents().size()); |
890 | |
891 | assert(getCurrentSectionOnly() && "need a section" ); |
892 | |
893 | insert(F: new MCNopsFragment(NumBytes, ControlledNopLength, Loc, STI)); |
894 | } |
895 | |
896 | void MCObjectStreamer::emitFileDirective(StringRef Filename) { |
897 | getAssembler().addFileName(FileName: Filename); |
898 | } |
899 | |
900 | void MCObjectStreamer::emitFileDirective(StringRef Filename, |
901 | StringRef CompilerVersion, |
902 | StringRef TimeStamp, |
903 | StringRef Description) { |
904 | getAssembler().addFileName(FileName: Filename); |
905 | getAssembler().setCompilerVersion(CompilerVersion.str()); |
906 | // TODO: add TimeStamp and Description to .file symbol table entry |
907 | // with the integrated assembler. |
908 | } |
909 | |
910 | void MCObjectStreamer::emitAddrsig() { |
911 | getAssembler().getWriter().emitAddrsigSection(); |
912 | } |
913 | |
914 | void MCObjectStreamer::emitAddrsigSym(const MCSymbol *Sym) { |
915 | getAssembler().getWriter().addAddrsigSymbol(Sym); |
916 | } |
917 | |
918 | void MCObjectStreamer::finishImpl() { |
919 | getContext().RemapDebugPaths(); |
920 | |
921 | // If we are generating dwarf for assembly source files dump out the sections. |
922 | if (getContext().getGenDwarfForAssembly()) |
923 | MCGenDwarfInfo::Emit(MCOS: this); |
924 | |
925 | // Dump out the dwarf file & directory tables and line tables. |
926 | MCDwarfLineTable::emit(MCOS: this, Params: getAssembler().getDWARFLinetableParams()); |
927 | |
928 | // Emit pseudo probes for the current module. |
929 | MCPseudoProbeTable::emit(MCOS: this); |
930 | |
931 | // Update any remaining pending labels with empty data fragments. |
932 | flushPendingLabels(); |
933 | |
934 | resolvePendingFixups(); |
935 | getAssembler().Finish(); |
936 | } |
937 | |