2010-06-16 22:04:22 +02:00
|
|
|
//===- MCObjectStreamer.h - MCStreamer Object File Interface ----*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-06-16 22:04:22 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_MC_MCOBJECTSTREAMER_H
|
|
|
|
#define LLVM_MC_MCOBJECTSTREAMER_H
|
|
|
|
|
2020-04-26 16:39:30 +02:00
|
|
|
#include "llvm/ADT/SetVector.h"
|
2014-10-23 00:38:06 +02:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2013-04-17 23:18:16 +02:00
|
|
|
#include "llvm/MC/MCAssembler.h"
|
2015-05-26 00:57:48 +02:00
|
|
|
#include "llvm/MC/MCSection.h"
|
2010-06-16 22:04:22 +02:00
|
|
|
#include "llvm/MC/MCStreamer.h"
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
class MCAssembler;
|
|
|
|
class MCCodeEmitter;
|
2014-01-29 00:12:49 +01:00
|
|
|
class MCSubtargetInfo;
|
2010-07-19 08:13:10 +02:00
|
|
|
class MCExpr;
|
|
|
|
class MCFragment;
|
|
|
|
class MCDataFragment;
|
2011-07-26 01:24:55 +02:00
|
|
|
class MCAsmBackend;
|
2010-06-16 22:04:22 +02:00
|
|
|
class raw_ostream;
|
2015-04-15 00:14:34 +02:00
|
|
|
class raw_pwrite_stream;
|
2010-06-16 22:04:22 +02:00
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Streaming object file generation interface.
|
2010-06-16 22:04:22 +02:00
|
|
|
///
|
|
|
|
/// This class provides an implementation of the MCStreamer interface which is
|
|
|
|
/// suitable for use with the assembler backend. Specific object file formats
|
|
|
|
/// are expected to subclass this interface to implement directives specific
|
|
|
|
/// to that file format or custom semantics expected by the object writer
|
|
|
|
/// implementation.
|
|
|
|
class MCObjectStreamer : public MCStreamer {
|
2017-10-09 20:11:04 +02:00
|
|
|
std::unique_ptr<MCAssembler> Assembler;
|
2015-05-27 17:14:11 +02:00
|
|
|
MCSection::iterator CurInsertionPoint;
|
2014-05-12 16:02:44 +02:00
|
|
|
bool EmitEHFrame;
|
|
|
|
bool EmitDebugFrame;
|
2015-05-29 19:41:59 +02:00
|
|
|
SmallVector<MCSymbol *, 2> PendingLabels;
|
2020-04-26 16:39:30 +02:00
|
|
|
SmallSetVector<MCSection *, 4> PendingLabelSections;
|
[ MC ] Match labels to existing fragments even when switching sections.
(This commit restores the original branch (4272372c571) and applies an
additional change dropped from the original in a bad merge. This change
should address the previous bot failures. Both changes reviewed by pete.)
Summary:
This commit builds upon Derek Schuff's 2014 commit for attaching labels to
existing fragments ( Diff Revision: http://reviews.llvm.org/D5915 )
When temporary labels appear ahead of a fragment, MCObjectStreamer will
track the temporary label symbol in a "Pending Labels" list. Labels are
associated with fragments when a real fragment arrives; otherwise, an empty
data fragment will be created if the streamer's section changes or if the
stream finishes.
This commit moves the "Pending Labels" list into each MCStream, so that
this label-fragment matching process is resilient to section changes. If
the streamer emits a label in a new section, switches to another section to
do other work, then switches back to the first section and emits a
fragment, that initial label will be associated with this new fragment.
Labels will only receive empty data fragments in the case where no other
fragment exists for that section.
The downstream effects of this can be seen in Mach-O relocations. The
previous approach could produce local section relocations and external
symbol relocations for the same data in an object file, and this mix of
relocation types resulted in problems in the ld64 Mach-O linker. This
commit ensures relocations triggered by temporary labels are consistent.
Reviewers: pete, ab, dschuff
Reviewed By: pete, dschuff
Subscribers: hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D71368
2019-12-11 19:42:37 +01:00
|
|
|
unsigned CurSubsectionIdx;
|
2018-11-21 17:28:39 +01:00
|
|
|
struct PendingMCFixup {
|
|
|
|
const MCSymbol *Sym;
|
|
|
|
MCFixup Fixup;
|
|
|
|
MCDataFragment *DF;
|
|
|
|
PendingMCFixup(const MCSymbol *McSym, MCDataFragment *F, MCFixup McFixup)
|
|
|
|
: Sym(McSym), Fixup(McFixup), DF(F) {}
|
|
|
|
};
|
|
|
|
SmallVector<PendingMCFixup, 2> PendingFixups;
|
2010-06-16 22:04:22 +02:00
|
|
|
|
2020-04-20 11:53:00 +02:00
|
|
|
virtual void emitInstToData(const MCInst &Inst, const MCSubtargetInfo&) = 0;
|
2020-02-14 06:58:16 +01:00
|
|
|
void emitCFIStartProcImpl(MCDwarfFrameInfo &Frame) override;
|
|
|
|
void emitCFIEndProcImpl(MCDwarfFrameInfo &Frame) override;
|
|
|
|
MCSymbol *emitCFILabel() override;
|
|
|
|
void emitInstructionImpl(const MCInst &Inst, const MCSubtargetInfo &STI);
|
2018-11-21 17:28:39 +01:00
|
|
|
void resolvePendingFixups();
|
2010-11-01 17:27:31 +01:00
|
|
|
|
2010-06-16 22:04:22 +02:00
|
|
|
protected:
|
2017-10-11 03:57:21 +02:00
|
|
|
MCObjectStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
|
2018-05-18 20:26:45 +02:00
|
|
|
std::unique_ptr<MCObjectWriter> OW,
|
2017-10-12 01:34:47 +02:00
|
|
|
std::unique_ptr<MCCodeEmitter> Emitter);
|
|
|
|
~MCObjectStreamer();
|
2010-06-16 22:04:22 +02:00
|
|
|
|
2012-12-12 23:59:46 +01:00
|
|
|
public:
|
|
|
|
/// state management
|
2014-03-08 08:02:02 +01:00
|
|
|
void reset() override;
|
2012-12-12 23:59:46 +01:00
|
|
|
|
2014-02-13 15:44:26 +01:00
|
|
|
/// Object streamers require the integrated assembler.
|
2014-03-08 08:02:02 +01:00
|
|
|
bool isIntegratedAssemblerRequired() const override { return true; }
|
2014-02-13 15:44:26 +01:00
|
|
|
|
2020-04-21 04:28:13 +02:00
|
|
|
void emitFrames(MCAsmBackend *MAB);
|
2020-02-14 06:58:16 +01:00
|
|
|
void emitCFISections(bool EH, bool Debug) override;
|
2014-04-27 19:23:37 +02:00
|
|
|
|
2010-07-19 08:13:10 +02:00
|
|
|
MCFragment *getCurrentFragment() const;
|
|
|
|
|
2014-10-23 00:38:06 +02:00
|
|
|
void insert(MCFragment *F) {
|
|
|
|
flushPendingLabels(F);
|
2015-05-27 23:04:14 +02:00
|
|
|
MCSection *CurSection = getCurrentSectionOnly();
|
2015-05-27 22:52:32 +02:00
|
|
|
CurSection->getFragmentList().insert(CurInsertionPoint, F);
|
|
|
|
F->setParent(CurSection);
|
2013-04-17 23:18:16 +02:00
|
|
|
}
|
|
|
|
|
2010-07-19 08:13:10 +02:00
|
|
|
/// Get a data fragment to write into, creating a new one if the current
|
|
|
|
/// fragment is not a data fragment.
|
2018-06-06 11:40:06 +02:00
|
|
|
/// Optionally a \p STI can be passed in so that a new fragment is created
|
|
|
|
/// if the Subtarget differs from the current fragment.
|
|
|
|
MCDataFragment *getOrCreateDataFragment(const MCSubtargetInfo* STI = nullptr);
|
2010-07-19 08:13:10 +02:00
|
|
|
|
2016-01-29 01:49:42 +01:00
|
|
|
protected:
|
2015-05-21 21:20:38 +02:00
|
|
|
bool changeSectionImpl(MCSection *Section, const MCExpr *Subsection);
|
2015-03-20 21:00:01 +01:00
|
|
|
|
[ MC ] Match labels to existing fragments even when switching sections.
(This commit restores the original branch (4272372c571) and applies an
additional change dropped from the original in a bad merge. This change
should address the previous bot failures. Both changes reviewed by pete.)
Summary:
This commit builds upon Derek Schuff's 2014 commit for attaching labels to
existing fragments ( Diff Revision: http://reviews.llvm.org/D5915 )
When temporary labels appear ahead of a fragment, MCObjectStreamer will
track the temporary label symbol in a "Pending Labels" list. Labels are
associated with fragments when a real fragment arrives; otherwise, an empty
data fragment will be created if the streamer's section changes or if the
stream finishes.
This commit moves the "Pending Labels" list into each MCStream, so that
this label-fragment matching process is resilient to section changes. If
the streamer emits a label in a new section, switches to another section to
do other work, then switches back to the first section and emits a
fragment, that initial label will be associated with this new fragment.
Labels will only receive empty data fragments in the case where no other
fragment exists for that section.
The downstream effects of this can be seen in Mach-O relocations. The
previous approach could produce local section relocations and external
symbol relocations for the same data in an object file, and this mix of
relocation types resulted in problems in the ld64 Mach-O linker. This
commit ensures relocations triggered by temporary labels are consistent.
Reviewers: pete, ab, dschuff
Reviewed By: pete, dschuff
Subscribers: hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D71368
2019-12-11 19:42:37 +01:00
|
|
|
/// Assign a label to the current Section and Subsection even though a
|
|
|
|
/// fragment is not yet present. Use flushPendingLabels(F) to associate
|
|
|
|
/// a fragment with this label.
|
|
|
|
void addPendingLabel(MCSymbol* label);
|
|
|
|
|
|
|
|
/// If any labels have been emitted but not assigned fragments in the current
|
|
|
|
/// Section and Subsection, ensure that they get assigned, either to fragment
|
|
|
|
/// F if possible or to a new data fragment. Optionally, one can provide an
|
|
|
|
/// offset \p FOffset as a symbol offset within the fragment.
|
2015-04-13 01:42:25 +02:00
|
|
|
void flushPendingLabels(MCFragment *F, uint64_t FOffset = 0);
|
|
|
|
|
2010-06-16 22:04:22 +02:00
|
|
|
public:
|
2014-06-25 17:45:33 +02:00
|
|
|
void visitUsedSymbol(const MCSymbol &Sym) override;
|
2014-06-25 17:29:54 +02:00
|
|
|
|
[ MC ] Match labels to existing fragments even when switching sections.
(This commit restores the original branch (4272372c571) and applies an
additional change dropped from the original in a bad merge. This change
should address the previous bot failures. Both changes reviewed by pete.)
Summary:
This commit builds upon Derek Schuff's 2014 commit for attaching labels to
existing fragments ( Diff Revision: http://reviews.llvm.org/D5915 )
When temporary labels appear ahead of a fragment, MCObjectStreamer will
track the temporary label symbol in a "Pending Labels" list. Labels are
associated with fragments when a real fragment arrives; otherwise, an empty
data fragment will be created if the streamer's section changes or if the
stream finishes.
This commit moves the "Pending Labels" list into each MCStream, so that
this label-fragment matching process is resilient to section changes. If
the streamer emits a label in a new section, switches to another section to
do other work, then switches back to the first section and emits a
fragment, that initial label will be associated with this new fragment.
Labels will only receive empty data fragments in the case where no other
fragment exists for that section.
The downstream effects of this can be seen in Mach-O relocations. The
previous approach could produce local section relocations and external
symbol relocations for the same data in an object file, and this mix of
relocation types resulted in problems in the ld64 Mach-O linker. This
commit ensures relocations triggered by temporary labels are consistent.
Reviewers: pete, ab, dschuff
Reviewed By: pete, dschuff
Subscribers: hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D71368
2019-12-11 19:42:37 +01:00
|
|
|
/// Create a data fragment for any pending labels across all Sections
|
|
|
|
/// and Subsections.
|
|
|
|
void flushPendingLabels();
|
2018-07-10 17:32:17 +02:00
|
|
|
|
2010-06-16 22:04:22 +02:00
|
|
|
MCAssembler &getAssembler() { return *Assembler; }
|
[MC] Change AsmParser to leverage Assembler during evaluation
Teach AsmParser to check with Assembler for when evaluating constant
expressions. This improves the handing of preprocessor expressions
that must be resolved at parse time. This idiom can be found as
assembling-time assertion checks in source-level assemblers. Note that
this relies on the MCStreamer to keep sufficient tabs on Section /
Fragment information which the MCAsmStreamer does not. As a result the
textual output may fail where the equivalent object generation would
pass. This can most easily be resolved by folding the MCAsmStreamer
and MCObjectStreamer together which is planned for in a separate
patch.
Currently, this feature is only enabled for assembly input, keeping IR
compilation consistent between assembly and object generation.
Reviewers: echristo, rnk, probinson, espindola, peter.smith
Reviewed By: peter.smith
Subscribers: eraman, peter.smith, arichardson, jyknight, hiraditya, llvm-commits
Differential Revision: https://reviews.llvm.org/D45164
llvm-svn: 331218
2018-04-30 21:22:40 +02:00
|
|
|
MCAssembler *getAssemblerPtr() override;
|
2015-05-02 02:44:14 +02:00
|
|
|
/// \name MCStreamer Interface
|
2010-06-16 22:04:25 +02:00
|
|
|
/// @{
|
|
|
|
|
2020-02-15 04:21:58 +01:00
|
|
|
void emitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
|
2020-02-13 22:26:21 +01:00
|
|
|
virtual void emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc, MCFragment *F,
|
2019-11-10 22:12:29 +01:00
|
|
|
uint64_t Offset);
|
2020-02-15 03:16:24 +01:00
|
|
|
void emitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
|
2020-02-15 04:21:58 +01:00
|
|
|
void emitValueImpl(const MCExpr *Value, unsigned Size,
|
2015-09-21 01:35:59 +02:00
|
|
|
SMLoc Loc = SMLoc()) override;
|
2020-02-13 22:26:21 +01:00
|
|
|
void emitULEB128Value(const MCExpr *Value) override;
|
|
|
|
void emitSLEB128Value(const MCExpr *Value) override;
|
2020-02-15 03:16:24 +01:00
|
|
|
void emitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) override;
|
2020-04-21 04:28:13 +02:00
|
|
|
void changeSection(MCSection *Section, const MCExpr *Subsection) override;
|
2020-02-14 06:58:16 +01:00
|
|
|
void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override;
|
2012-12-20 20:05:53 +01:00
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Emit an instruction to a special fragment, because this instruction
|
2012-12-20 20:05:53 +01:00
|
|
|
/// can change its size during relaxation.
|
2020-04-21 04:28:13 +02:00
|
|
|
virtual void emitInstToFragment(const MCInst &Inst, const MCSubtargetInfo &);
|
2012-12-20 20:05:53 +01:00
|
|
|
|
2020-02-15 17:52:56 +01:00
|
|
|
void emitBundleAlignMode(unsigned AlignPow2) override;
|
|
|
|
void emitBundleLock(bool AlignToEnd) override;
|
|
|
|
void emitBundleUnlock() override;
|
2020-02-15 03:16:24 +01:00
|
|
|
void emitBytes(StringRef Data) override;
|
2020-02-15 04:21:58 +01:00
|
|
|
void emitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
|
2014-03-08 08:02:02 +01:00
|
|
|
unsigned ValueSize = 1,
|
|
|
|
unsigned MaxBytesToEmit = 0) override;
|
2020-02-15 04:21:58 +01:00
|
|
|
void emitCodeAlignment(unsigned ByteAlignment,
|
2014-03-08 08:02:02 +01:00
|
|
|
unsigned MaxBytesToEmit = 0) override;
|
2016-12-14 11:43:58 +01:00
|
|
|
void emitValueToOffset(const MCExpr *Offset, unsigned char Value,
|
|
|
|
SMLoc Loc) override;
|
2020-02-13 22:26:21 +01:00
|
|
|
void emitDwarfLocDirective(unsigned FileNo, unsigned Line, unsigned Column,
|
|
|
|
unsigned Flags, unsigned Isa,
|
|
|
|
unsigned Discriminator,
|
2014-03-08 08:02:02 +01:00
|
|
|
StringRef FileName) override;
|
2020-02-13 22:26:21 +01:00
|
|
|
void emitDwarfAdvanceLineAddr(int64_t LineDelta, const MCSymbol *LastLabel,
|
|
|
|
const MCSymbol *Label, unsigned PointerSize);
|
|
|
|
void emitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
|
2014-05-12 16:43:25 +02:00
|
|
|
const MCSymbol *Label);
|
2020-04-21 04:28:13 +02:00
|
|
|
void emitCVLocDirective(unsigned FunctionId, unsigned FileNo, unsigned Line,
|
2016-01-29 01:49:42 +01:00
|
|
|
unsigned Column, bool PrologueEnd, bool IsStmt,
|
2016-09-07 18:15:31 +02:00
|
|
|
StringRef FileName, SMLoc Loc) override;
|
2020-04-21 04:28:13 +02:00
|
|
|
void emitCVLinetableDirective(unsigned FunctionId, const MCSymbol *Begin,
|
2016-01-29 01:49:42 +01:00
|
|
|
const MCSymbol *End) override;
|
2020-04-21 04:28:13 +02:00
|
|
|
void emitCVInlineLinetableDirective(unsigned PrimaryFunctionId,
|
2016-09-07 18:15:31 +02:00
|
|
|
unsigned SourceFileId,
|
|
|
|
unsigned SourceLineNum,
|
|
|
|
const MCSymbol *FnStartSym,
|
|
|
|
const MCSymbol *FnEndSym) override;
|
2020-04-21 04:28:13 +02:00
|
|
|
void emitCVDefRangeDirective(
|
2016-02-05 02:55:49 +01:00
|
|
|
ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
|
|
|
|
StringRef FixedSizePortion) override;
|
2020-04-21 04:28:13 +02:00
|
|
|
void emitCVStringTableDirective() override;
|
|
|
|
void emitCVFileChecksumsDirective() override;
|
|
|
|
void emitCVFileChecksumOffsetDirective(unsigned FileNo) override;
|
2020-02-15 04:21:58 +01:00
|
|
|
void emitDTPRel32Value(const MCExpr *Value) override;
|
|
|
|
void emitDTPRel64Value(const MCExpr *Value) override;
|
|
|
|
void emitTPRel32Value(const MCExpr *Value) override;
|
|
|
|
void emitTPRel64Value(const MCExpr *Value) override;
|
|
|
|
void emitGPRel32Value(const MCExpr *Value) override;
|
|
|
|
void emitGPRel64Value(const MCExpr *Value) override;
|
2020-07-14 22:44:00 +02:00
|
|
|
Optional<std::pair<bool, std::string>>
|
|
|
|
emitRelocDirective(const MCExpr &Offset, StringRef Name, const MCExpr *Expr,
|
|
|
|
SMLoc Loc, const MCSubtargetInfo &STI) override;
|
2016-06-11 10:12:17 +02:00
|
|
|
using MCStreamer::emitFill;
|
2016-05-28 07:57:48 +02:00
|
|
|
void emitFill(const MCExpr &NumBytes, uint64_t FillValue,
|
|
|
|
SMLoc Loc = SMLoc()) override;
|
|
|
|
void emitFill(const MCExpr &NumValues, int64_t Size, int64_t Expr,
|
|
|
|
SMLoc Loc = SMLoc()) override;
|
2020-07-31 03:33:33 +02:00
|
|
|
void emitNops(int64_t NumBytes, int64_t ControlledNopLength,
|
|
|
|
SMLoc Loc) override;
|
2020-02-15 17:52:56 +01:00
|
|
|
void emitFileDirective(StringRef Filename) override;
|
2016-05-28 07:57:48 +02:00
|
|
|
|
2020-02-15 17:52:56 +01:00
|
|
|
void emitAddrsig() override;
|
|
|
|
void emitAddrsigSym(const MCSymbol *Sym) override;
|
2018-07-18 00:17:18 +02:00
|
|
|
|
2020-04-21 04:28:13 +02:00
|
|
|
void finishImpl() override;
|
2014-06-19 17:52:37 +02:00
|
|
|
|
2015-05-21 04:41:23 +02:00
|
|
|
/// Emit the absolute difference between two symbols if possible.
|
|
|
|
///
|
|
|
|
/// Emit the absolute difference between \c Hi and \c Lo, as long as we can
|
|
|
|
/// compute it. Currently, that requires that both symbols are in the same
|
2018-08-16 13:26:37 +02:00
|
|
|
/// data fragment and that the target has not specified that diff expressions
|
|
|
|
/// require relocations to be emitted. Otherwise, do nothing and return
|
|
|
|
/// \c false.
|
2015-05-21 04:41:23 +02:00
|
|
|
///
|
|
|
|
/// \pre Offset of \c Hi is greater than the offset \c Lo.
|
2015-06-11 20:58:08 +02:00
|
|
|
void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo,
|
2015-05-21 04:41:23 +02:00
|
|
|
unsigned Size) override;
|
|
|
|
|
2018-02-09 18:00:25 +01:00
|
|
|
void emitAbsoluteSymbolDiffAsULEB128(const MCSymbol *Hi,
|
|
|
|
const MCSymbol *Lo) override;
|
|
|
|
|
2015-05-25 20:34:26 +02:00
|
|
|
bool mayHaveInstructions(MCSection &Sec) const override;
|
2010-06-16 22:04:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif
|