Page MenuHomePhabricator

No OneTemporary

File Metadata

Created
Jan 24 2020, 4:30 PM
This file is larger than 256 KB, so syntax highlighting was skipped.
Index: llvm/trunk/test/MC/MachO/ios-version-min-load-command.s
===================================================================
--- llvm/trunk/test/MC/MachO/ios-version-min-load-command.s (revision 0)
+++ llvm/trunk/test/MC/MachO/ios-version-min-load-command.s (revision 204190)
@@ -0,0 +1,10 @@
+// RUN: llvm-mc -triple armv7-apple-ios %s -filetype=obj -o - | macho-dump | FileCheck %s
+
+// Test the formation of the version-min load command in the MachO.
+// use a nonsense but well formed version.
+.ios_version_min 99,8,7
+// CHECK: (('command', 37)
+// CHECK: ('size', 16)
+// CHECK: ('version, 6490119)
+// CHECK: ('reserved, 0)
+// CHECK: ),
Index: llvm/trunk/test/MC/MachO/osx-version-min-load-command.s
===================================================================
--- llvm/trunk/test/MC/MachO/osx-version-min-load-command.s (revision 0)
+++ llvm/trunk/test/MC/MachO/osx-version-min-load-command.s (revision 204190)
@@ -0,0 +1,10 @@
+// RUN: llvm-mc -triple x86_64-apple-darwin %s -filetype=obj -o - | macho-dump | FileCheck %s
+
+// Test the formation of the version-min load command in the MachO.
+// use a nonsense but well formed version.
+.macosx_version_min 25,3,1
+// CHECK: (('command', 36)
+// CHECK: ('size', 16)
+// CHECK: ('version, 1639169)
+// CHECK: ('reserved, 0)
+// CHECK: ),
Index: llvm/trunk/test/MC/AsmParser/version-min-diagnostics.s
===================================================================
--- llvm/trunk/test/MC/AsmParser/version-min-diagnostics.s (revision 0)
+++ llvm/trunk/test/MC/AsmParser/version-min-diagnostics.s (revision 204190)
@@ -0,0 +1,49 @@
+// RUN: not llvm-mc -triple i386-apple-darwin %s 2> %t
+// RUN: FileCheck %s < %t
+// RUN: not llvm-mc -triple x86_64-apple-darwin %s 2> %t
+// RUN: FileCheck %s < %t
+// RUN: not llvm-mc -triple armv7-apple-ios %s 2> %t
+// RUN: FileCheck %s < %t
+
+.ios_version_min 5,2,257
+.ios_version_min 5,256,1
+.ios_version_min 5,-1,1
+.ios_version_min 0,1,1
+.ios_version_min 70000,1
+.macosx_version_min 99,2,257
+.macosx_version_min 50,256,1
+.macosx_version_min 10,-1,1
+.macosx_version_min 0,1,1
+.macosx_version_min 70000,1
+
+
+// CHECK: error: invalid OS update number
+// CHECK: .ios_version_min 5,2,257
+// CHECK: ^
+// CHECK: error: invalid OS minor version number
+// CHECK: .ios_version_min 5,256,1
+// CHECK: ^
+// CHECK: error: invalid OS minor version number
+// CHECK: .ios_version_min 5,-1,1
+// CHECK: ^
+// CHECK: error: invalid OS major version number
+// CHECK: .ios_version_min 0,1,1
+// CHECK: ^
+// CHECK: error: invalid OS major version number
+// CHECK: .ios_version_min 70000,1
+// CHECK: ^
+// CHECK: error: invalid OS update number
+// CHECK: .macosx_version_min 99,2,257
+// CHECK: ^
+// CHECK: error: invalid OS minor version number
+// CHECK: .macosx_version_min 50,256,1
+// CHECK: ^
+// CHECK: error: invalid OS minor version number
+// CHECK: .macosx_version_min 10,-1,1
+// CHECK: ^
+// CHECK: error: invalid OS major version number
+// CHECK: .macosx_version_min 0,1,1
+// CHECK: ^
+// CHECK: error: invalid OS major version number
+// CHECK: .macosx_version_min 70000,1
+// CHECK: ^
Index: llvm/trunk/test/MC/AsmParser/version-min.s
===================================================================
--- llvm/trunk/test/MC/AsmParser/version-min.s (revision 0)
+++ llvm/trunk/test/MC/AsmParser/version-min.s (revision 204190)
@@ -0,0 +1,21 @@
+// RUN: llvm-mc -triple i386-apple-darwin %s | FileCheck %s
+// RUN: llvm-mc -triple x86_64-apple-darwin %s | FileCheck %s
+// RUN: llvm-mc -triple armv7s-apple-ios %s | FileCheck %s
+
+// Test the parsing of well-formed version-min directives.
+
+.ios_version_min 5,2,0
+.ios_version_min 3,2,1
+.ios_version_min 5,0
+
+// CHECK: .ios_version_min 5, 2
+// CHECK: .ios_version_min 3, 2, 1
+// CHECK: .ios_version_min 5, 0
+
+.macosx_version_min 10,2,0
+.macosx_version_min 10,8,1
+.macosx_version_min 2,0
+
+// CHECK: .macosx_version_min 10, 2
+// CHECK: .macosx_version_min 10, 8, 1
+// CHECK: .macosx_version_min 2, 0
Index: llvm/trunk/include/llvm/MC/MCStreamer.h
===================================================================
--- llvm/trunk/include/llvm/MC/MCStreamer.h (revision 204189)
+++ llvm/trunk/include/llvm/MC/MCStreamer.h (revision 204190)
@@ -1,773 +1,777 @@
//===- MCStreamer.h - High-level Streaming Machine Code Output --*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file declares the MCStreamer class.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_MC_MCSTREAMER_H
#define LLVM_MC_MCSTREAMER_H
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/MC/MCAssembler.h"
#include "llvm/MC/MCDirectives.h"
#include "llvm/MC/MCDwarf.h"
#include "llvm/MC/MCWin64EH.h"
#include "llvm/Support/DataTypes.h"
#include <string>
namespace llvm {
class MCAsmBackend;
class MCCodeEmitter;
class MCContext;
class MCExpr;
class MCInst;
class MCInstPrinter;
class MCSection;
class MCStreamer;
class MCSymbol;
class MCSymbolRefExpr;
class MCSubtargetInfo;
class StringRef;
class Twine;
class raw_ostream;
class formatted_raw_ostream;
class AssemblerConstantPools;
typedef std::pair<const MCSection *, const MCExpr *> MCSectionSubPair;
/// Target specific streamer interface. This is used so that targets can
/// implement support for target specific assembly directives.
///
/// If target foo wants to use this, it should implement 3 classes:
/// * FooTargetStreamer : public MCTargetStreamer
/// * FooTargetAsmSreamer : public FooTargetStreamer
/// * FooTargetELFStreamer : public FooTargetStreamer
///
/// FooTargetStreamer should have a pure virtual method for each directive. For
/// example, for a ".bar symbol_name" directive, it should have
/// virtual emitBar(const MCSymbol &Symbol) = 0;
///
/// The FooTargetAsmSreamer and FooTargetELFStreamer classes implement the
/// method. The assembly streamer just prints ".bar symbol_name". The object
/// streamer does whatever is needed to implement .bar in the object file.
///
/// In the assembly printer and parser the target streamer can be used by
/// calling getTargetStreamer and casting it to FooTargetStreamer:
///
/// MCTargetStreamer &TS = OutStreamer.getTargetStreamer();
/// FooTargetStreamer &ATS = static_cast<FooTargetStreamer &>(TS);
///
/// The base classes FooTargetAsmSreamer and FooTargetELFStreamer should *never*
/// be treated differently. Callers should always talk to a FooTargetStreamer.
class MCTargetStreamer {
protected:
MCStreamer &Streamer;
public:
MCTargetStreamer(MCStreamer &S);
virtual ~MCTargetStreamer();
const MCStreamer &getStreamer() { return Streamer; }
// Allow a target to add behavior to the EmitLabel of MCStreamer.
virtual void emitLabel(MCSymbol *Symbol);
virtual void finish();
};
// FIXME: declared here because it is used from
// lib/CodeGen/AsmPrinter/ARMException.cpp.
class ARMTargetStreamer : public MCTargetStreamer {
public:
ARMTargetStreamer(MCStreamer &S);
~ARMTargetStreamer();
virtual void emitFnStart();
virtual void emitFnEnd();
virtual void emitCantUnwind();
virtual void emitPersonality(const MCSymbol *Personality);
virtual void emitPersonalityIndex(unsigned Index);
virtual void emitHandlerData();
virtual void emitSetFP(unsigned FpReg, unsigned SpReg,
int64_t Offset = 0);
virtual void emitMovSP(unsigned Reg, int64_t Offset = 0);
virtual void emitPad(int64_t Offset);
virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
bool isVector);
virtual void emitUnwindRaw(int64_t StackOffset,
const SmallVectorImpl<uint8_t> &Opcodes);
virtual void switchVendor(StringRef Vendor);
virtual void emitAttribute(unsigned Attribute, unsigned Value);
virtual void emitTextAttribute(unsigned Attribute, StringRef String);
virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
StringRef StringValue = "");
virtual void emitFPU(unsigned FPU);
virtual void emitArch(unsigned Arch);
virtual void emitObjectArch(unsigned Arch);
virtual void finishAttributeSection();
virtual void emitInst(uint32_t Inst, char Suffix = '\0');
virtual void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE);
void finish() override;
/// Callback used to implement the ldr= pseudo.
/// Add a new entry to the constant pool for the current section and return an
/// MCExpr that can be used to refer to the constant pool location.
const MCExpr *addConstantPoolEntry(const MCExpr *);
/// Callback used to implemnt the .ltorg directive.
/// Emit contents of constant pool for the current section.
void emitCurrentConstantPool();
private:
std::unique_ptr<AssemblerConstantPools> ConstantPools;
};
/// MCStreamer - Streaming machine code generation interface. This interface
/// is intended to provide a programatic interface that is very similar to the
/// level that an assembler .s file provides. It has callbacks to emit bytes,
/// handle directives, etc. The implementation of this interface retains
/// state to know what the current section is etc.
///
/// There are multiple implementations of this interface: one for writing out
/// a .s file, and implementations that write out .o files of various formats.
///
class MCStreamer {
MCContext &Context;
std::unique_ptr<MCTargetStreamer> TargetStreamer;
MCStreamer(const MCStreamer &) LLVM_DELETED_FUNCTION;
MCStreamer &operator=(const MCStreamer &) LLVM_DELETED_FUNCTION;
bool EmitEHFrame;
bool EmitDebugFrame;
std::vector<MCDwarfFrameInfo> FrameInfos;
MCDwarfFrameInfo *getCurrentFrameInfo();
MCSymbol *EmitCFICommon();
void EnsureValidFrame();
std::vector<MCWin64EHUnwindInfo *> W64UnwindInfos;
MCWin64EHUnwindInfo *CurrentW64UnwindInfo;
void setCurrentW64UnwindInfo(MCWin64EHUnwindInfo *Frame);
void EnsureValidW64UnwindInfo();
MCSymbol *LastSymbol;
// SymbolOrdering - Tracks an index to represent the order
// a symbol was emitted in. Zero means we did not emit that symbol.
DenseMap<const MCSymbol *, unsigned> SymbolOrdering;
/// SectionStack - This is stack of current and previous section
/// values saved by PushSection.
SmallVector<std::pair<MCSectionSubPair, MCSectionSubPair>, 4> SectionStack;
protected:
MCStreamer(MCContext &Ctx);
const MCExpr *BuildSymbolDiff(MCContext &Context, const MCSymbol *A,
const MCSymbol *B);
const MCExpr *ForceExpAbs(const MCExpr *Expr);
void RecordProcStart(MCDwarfFrameInfo &Frame);
virtual void EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame);
void RecordProcEnd(MCDwarfFrameInfo &Frame);
virtual void EmitCFIEndProcImpl(MCDwarfFrameInfo &CurFrame);
void EmitFrames(MCAsmBackend *MAB, bool usingCFI);
MCWin64EHUnwindInfo *getCurrentW64UnwindInfo() {
return CurrentW64UnwindInfo;
}
void EmitW64Tables();
virtual void EmitRawTextImpl(StringRef String);
public:
virtual ~MCStreamer();
void setTargetStreamer(MCTargetStreamer *TS) {
TargetStreamer.reset(TS);
}
/// State management
///
virtual void reset();
MCContext &getContext() const { return Context; }
MCTargetStreamer *getTargetStreamer() {
return TargetStreamer.get();
}
unsigned getNumFrameInfos() { return FrameInfos.size(); }
const MCDwarfFrameInfo &getFrameInfo(unsigned i) { return FrameInfos[i]; }
ArrayRef<MCDwarfFrameInfo> getFrameInfos() const { return FrameInfos; }
unsigned getNumW64UnwindInfos() { return W64UnwindInfos.size(); }
MCWin64EHUnwindInfo &getW64UnwindInfo(unsigned i) {
return *W64UnwindInfos[i];
}
void generateCompactUnwindEncodings(MCAsmBackend *MAB);
/// @name Assembly File Formatting.
/// @{
/// isVerboseAsm - Return true if this streamer supports verbose assembly
/// and if it is enabled.
virtual bool isVerboseAsm() const { return false; }
/// hasRawTextSupport - Return true if this asm streamer supports emitting
/// unformatted text to the .s file with EmitRawText.
virtual bool hasRawTextSupport() const { return false; }
/// Is the integrated assembler required for this streamer to function
/// correctly?
virtual bool isIntegratedAssemblerRequired() const { return false; }
/// AddComment - Add a comment that can be emitted to the generated .s
/// file if applicable as a QoI issue to make the output of the compiler
/// more readable. This only affects the MCAsmStreamer, and only when
/// verbose assembly output is enabled.
///
/// If the comment includes embedded \n's, they will each get the comment
/// prefix as appropriate. The added comment should not end with a \n.
virtual void AddComment(const Twine &T) {}
/// GetCommentOS - Return a raw_ostream that comments can be written to.
/// Unlike AddComment, you are required to terminate comments with \n if you
/// use this method.
virtual raw_ostream &GetCommentOS();
/// Print T and prefix it with the comment string (normally #) and optionally
/// a tab. This prints the comment immediately, not at the end of the
/// current line. It is basically a safe version of EmitRawText: since it
/// only prints comments, the object streamer ignores it instead of asserting.
virtual void emitRawComment(const Twine &T, bool TabPrefix = true);
/// AddBlankLine - Emit a blank line to a .s file to pretty it up.
virtual void AddBlankLine() {}
/// @}
/// @name Symbol & Section Management
/// @{
/// getCurrentSection - Return the current section that the streamer is
/// emitting code to.
MCSectionSubPair getCurrentSection() const {
if (!SectionStack.empty())
return SectionStack.back().first;
return MCSectionSubPair();
}
/// getPreviousSection - Return the previous section that the streamer is
/// emitting code to.
MCSectionSubPair getPreviousSection() const {
if (!SectionStack.empty())
return SectionStack.back().second;
return MCSectionSubPair();
}
/// GetSymbolOrder - Returns an index to represent the order
/// a symbol was emitted in. (zero if we did not emit that symbol)
unsigned GetSymbolOrder(const MCSymbol *Sym) const {
return SymbolOrdering.lookup(Sym);
}
/// ChangeSection - Update streamer for a new active section.
///
/// This is called by PopSection and SwitchSection, if the current
/// section changes.
virtual void ChangeSection(const MCSection *, const MCExpr *) = 0;
/// pushSection - Save the current and previous section on the
/// section stack.
void PushSection() {
SectionStack.push_back(
std::make_pair(getCurrentSection(), getPreviousSection()));
}
/// popSection - Restore the current and previous section from
/// the section stack. Calls ChangeSection as needed.
///
/// Returns false if the stack was empty.
bool PopSection() {
if (SectionStack.size() <= 1)
return false;
MCSectionSubPair oldSection = SectionStack.pop_back_val().first;
MCSectionSubPair curSection = SectionStack.back().first;
if (oldSection != curSection)
ChangeSection(curSection.first, curSection.second);
return true;
}
bool SubSection(const MCExpr *Subsection) {
if (SectionStack.empty())
return false;
SwitchSection(SectionStack.back().first.first, Subsection);
return true;
}
/// SwitchSection - Set the current section where code is being emitted to
/// @p Section. This is required to update CurSection.
///
/// This corresponds to assembler directives like .section, .text, etc.
void SwitchSection(const MCSection *Section, const MCExpr *Subsection = 0) {
assert(Section && "Cannot switch to a null section!");
MCSectionSubPair curSection = SectionStack.back().first;
SectionStack.back().second = curSection;
if (MCSectionSubPair(Section, Subsection) != curSection) {
SectionStack.back().first = MCSectionSubPair(Section, Subsection);
ChangeSection(Section, Subsection);
}
}
/// SwitchSectionNoChange - Set the current section where code is being
/// emitted to @p Section. This is required to update CurSection. This
/// version does not call ChangeSection.
void SwitchSectionNoChange(const MCSection *Section,
const MCExpr *Subsection = 0) {
assert(Section && "Cannot switch to a null section!");
MCSectionSubPair curSection = SectionStack.back().first;
SectionStack.back().second = curSection;
if (MCSectionSubPair(Section, Subsection) != curSection)
SectionStack.back().first = MCSectionSubPair(Section, Subsection);
}
/// Create the default sections and set the initial one.
virtual void InitSections();
/// AssignSection - Sets the symbol's section.
///
/// Each emitted symbol will be tracked in the ordering table,
/// so we can sort on them later.
void AssignSection(MCSymbol *Symbol, const MCSection *Section);
/// EmitLabel - Emit a label for @p Symbol into the current section.
///
/// This corresponds to an assembler statement such as:
/// foo:
///
/// @param Symbol - The symbol to emit. A given symbol should only be
/// emitted as a label once, and symbols emitted as a label should never be
/// used in an assignment.
// FIXME: These emission are non-const because we mutate the symbol to
// add the section we're emitting it to later.
virtual void EmitLabel(MCSymbol *Symbol);
virtual void EmitDebugLabel(MCSymbol *Symbol);
virtual void EmitEHSymAttributes(const MCSymbol *Symbol, MCSymbol *EHSymbol);
/// EmitAssemblerFlag - Note in the output the specified @p Flag.
virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) = 0;
/// EmitLinkerOptions - Emit the given list @p Options of strings as linker
/// options into the output.
virtual void EmitLinkerOptions(ArrayRef<std::string> Kind) {}
/// EmitDataRegion - Note in the output the specified region @p Kind.
virtual void EmitDataRegion(MCDataRegionType Kind) {}
+ /// EmitVersionMin - Specify the MachO minimum deployment target version.
+ virtual void EmitVersionMin(MCVersionMinType, unsigned Major, unsigned Minor,
+ unsigned Update) {}
+
/// EmitThumbFunc - Note in the output that the specified @p Func is
/// a Thumb mode function (ARM target only).
virtual void EmitThumbFunc(MCSymbol *Func) = 0;
/// getOrCreateSymbolData - Get symbol data for given symbol.
virtual MCSymbolData &getOrCreateSymbolData(MCSymbol *Symbol);
/// EmitAssignment - Emit an assignment of @p Value to @p Symbol.
///
/// This corresponds to an assembler statement such as:
/// symbol = value
///
/// The assignment generates no code, but has the side effect of binding the
/// value in the current context. For the assembly streamer, this prints the
/// binding into the .s file.
///
/// @param Symbol - The symbol being assigned to.
/// @param Value - The value for the symbol.
virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) = 0;
/// EmitWeakReference - Emit an weak reference from @p Alias to @p Symbol.
///
/// This corresponds to an assembler statement such as:
/// .weakref alias, symbol
///
/// @param Alias - The alias that is being created.
/// @param Symbol - The symbol being aliased.
virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) = 0;
/// EmitSymbolAttribute - Add the given @p Attribute to @p Symbol.
virtual bool EmitSymbolAttribute(MCSymbol *Symbol,
MCSymbolAttr Attribute) = 0;
/// EmitSymbolDesc - Set the @p DescValue for the @p Symbol.
///
/// @param Symbol - The symbol to have its n_desc field set.
/// @param DescValue - The value to set into the n_desc field.
virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) = 0;
/// BeginCOFFSymbolDef - Start emitting COFF symbol definition
///
/// @param Symbol - The symbol to have its External & Type fields set.
virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) = 0;
/// EmitCOFFSymbolStorageClass - Emit the storage class of the symbol.
///
/// @param StorageClass - The storage class the symbol should have.
virtual void EmitCOFFSymbolStorageClass(int StorageClass) = 0;
/// EmitCOFFSymbolType - Emit the type of the symbol.
///
/// @param Type - A COFF type identifier (see COFF::SymbolType in X86COFF.h)
virtual void EmitCOFFSymbolType(int Type) = 0;
/// EndCOFFSymbolDef - Marks the end of the symbol definition.
virtual void EndCOFFSymbolDef() = 0;
/// EmitCOFFSectionIndex - Emits a COFF section index.
///
/// @param Symbol - Symbol the section number relocation should point to.
virtual void EmitCOFFSectionIndex(MCSymbol const *Symbol);
/// EmitCOFFSecRel32 - Emits a COFF section relative relocation.
///
/// @param Symbol - Symbol the section relative relocation should point to.
virtual void EmitCOFFSecRel32(MCSymbol const *Symbol);
/// EmitELFSize - Emit an ELF .size directive.
///
/// This corresponds to an assembler statement such as:
/// .size symbol, expression
///
virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) = 0;
/// EmitCommonSymbol - Emit a common symbol.
///
/// @param Symbol - The common symbol to emit.
/// @param Size - The size of the common symbol.
/// @param ByteAlignment - The alignment of the symbol if
/// non-zero. This must be a power of 2.
virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
unsigned ByteAlignment) = 0;
/// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
///
/// @param Symbol - The common symbol to emit.
/// @param Size - The size of the common symbol.
/// @param ByteAlignment - The alignment of the common symbol in bytes.
virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
unsigned ByteAlignment) = 0;
/// EmitZerofill - Emit the zerofill section and an optional symbol.
///
/// @param Section - The zerofill section to create and or to put the symbol
/// @param Symbol - The zerofill symbol to emit, if non-NULL.
/// @param Size - The size of the zerofill symbol.
/// @param ByteAlignment - The alignment of the zerofill symbol if
/// non-zero. This must be a power of 2 on some targets.
virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
uint64_t Size = 0, unsigned ByteAlignment = 0) = 0;
/// EmitTBSSSymbol - Emit a thread local bss (.tbss) symbol.
///
/// @param Section - The thread local common section.
/// @param Symbol - The thread local common symbol to emit.
/// @param Size - The size of the symbol.
/// @param ByteAlignment - The alignment of the thread local common symbol
/// if non-zero. This must be a power of 2 on some targets.
virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
uint64_t Size, unsigned ByteAlignment = 0) = 0;
/// @}
/// @name Generating Data
/// @{
/// EmitBytes - Emit the bytes in \p Data into the output.
///
/// This is used to implement assembler directives such as .byte, .ascii,
/// etc.
virtual void EmitBytes(StringRef Data) = 0;
/// EmitValue - Emit the expression @p Value into the output as a native
/// integer of the given @p Size bytes.
///
/// This is used to implement assembler directives such as .word, .quad,
/// etc.
///
/// @param Value - The value to emit.
/// @param Size - The size of the integer (in bytes) to emit. This must
/// match a native machine width.
virtual void EmitValueImpl(const MCExpr *Value, unsigned Size) = 0;
void EmitValue(const MCExpr *Value, unsigned Size);
/// EmitIntValue - Special case of EmitValue that avoids the client having
/// to pass in a MCExpr for constant integers.
virtual void EmitIntValue(uint64_t Value, unsigned Size);
/// EmitAbsValue - Emit the Value, but try to avoid relocations. On MachO
/// this is done by producing
/// foo = value
/// .long foo
void EmitAbsValue(const MCExpr *Value, unsigned Size);
virtual void EmitULEB128Value(const MCExpr *Value) = 0;
virtual void EmitSLEB128Value(const MCExpr *Value) = 0;
/// EmitULEB128Value - Special case of EmitULEB128Value that avoids the
/// client having to pass in a MCExpr for constant integers.
void EmitULEB128IntValue(uint64_t Value, unsigned Padding = 0);
/// EmitSLEB128Value - Special case of EmitSLEB128Value that avoids the
/// client having to pass in a MCExpr for constant integers.
void EmitSLEB128IntValue(int64_t Value);
/// EmitSymbolValue - Special case of EmitValue that avoids the client
/// having to pass in a MCExpr for MCSymbols.
void EmitSymbolValue(const MCSymbol *Sym, unsigned Size);
/// EmitGPRel64Value - Emit the expression @p Value into the output as a
/// gprel64 (64-bit GP relative) value.
///
/// This is used to implement assembler directives such as .gpdword on
/// targets that support them.
virtual void EmitGPRel64Value(const MCExpr *Value);
/// EmitGPRel32Value - Emit the expression @p Value into the output as a
/// gprel32 (32-bit GP relative) value.
///
/// This is used to implement assembler directives such as .gprel32 on
/// targets that support them.
virtual void EmitGPRel32Value(const MCExpr *Value);
/// EmitFill - Emit NumBytes bytes worth of the value specified by
/// FillValue. This implements directives such as '.space'.
virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue);
/// \brief Emit NumBytes worth of zeros.
/// This function properly handles data in virtual sections.
virtual void EmitZeros(uint64_t NumBytes);
/// EmitValueToAlignment - Emit some number of copies of @p Value until
/// the byte alignment @p ByteAlignment is reached.
///
/// If the number of bytes need to emit for the alignment is not a multiple
/// of @p ValueSize, then the contents of the emitted fill bytes is
/// undefined.
///
/// This used to implement the .align assembler directive.
///
/// @param ByteAlignment - The alignment to reach. This must be a power of
/// two on some targets.
/// @param Value - The value to use when filling bytes.
/// @param ValueSize - The size of the integer (in bytes) to emit for
/// @p Value. This must match a native machine width.
/// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
/// the alignment cannot be reached in this many bytes, no bytes are
/// emitted.
virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
unsigned ValueSize = 1,
unsigned MaxBytesToEmit = 0) = 0;
/// EmitCodeAlignment - Emit nops until the byte alignment @p ByteAlignment
/// is reached.
///
/// This used to align code where the alignment bytes may be executed. This
/// can emit different bytes for different sizes to optimize execution.
///
/// @param ByteAlignment - The alignment to reach. This must be a power of
/// two on some targets.
/// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
/// the alignment cannot be reached in this many bytes, no bytes are
/// emitted.
virtual void EmitCodeAlignment(unsigned ByteAlignment,
unsigned MaxBytesToEmit = 0) = 0;
/// EmitValueToOffset - Emit some number of copies of @p Value until the
/// byte offset @p Offset is reached.
///
/// This is used to implement assembler directives such as .org.
///
/// @param Offset - The offset to reach. This may be an expression, but the
/// expression must be associated with the current section.
/// @param Value - The value to use when filling bytes.
/// @return false on success, true if the offset was invalid.
virtual bool EmitValueToOffset(const MCExpr *Offset,
unsigned char Value = 0) = 0;
/// @}
/// EmitFileDirective - Switch to a new logical file. This is used to
/// implement the '.file "foo.c"' assembler directive.
virtual void EmitFileDirective(StringRef Filename) = 0;
/// Emit the "identifiers" directive. This implements the
/// '.ident "version foo"' assembler directive.
virtual void EmitIdent(StringRef IdentString) {}
/// EmitDwarfFileDirective - Associate a filename with a specified logical
/// file number. This implements the DWARF2 '.file 4 "foo.c"' assembler
/// directive.
virtual unsigned EmitDwarfFileDirective(unsigned FileNo, StringRef Directory,
StringRef Filename,
unsigned CUID = 0);
/// EmitDwarfLocDirective - This implements the DWARF2
// '.loc fileno lineno ...' assembler directive.
virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
unsigned Column, unsigned Flags,
unsigned Isa, unsigned Discriminator,
StringRef FileName);
virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
const MCSymbol *LastLabel,
const MCSymbol *Label,
unsigned PointerSize) = 0;
virtual void EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
const MCSymbol *Label) {}
void EmitDwarfSetLineAddr(int64_t LineDelta, const MCSymbol *Label,
int PointerSize);
virtual void EmitCompactUnwindEncoding(uint32_t CompactUnwindEncoding);
virtual void EmitCFISections(bool EH, bool Debug);
void EmitCFIStartProc(bool IsSimple);
void EmitCFIEndProc();
virtual void EmitCFIDefCfa(int64_t Register, int64_t Offset);
virtual void EmitCFIDefCfaOffset(int64_t Offset);
virtual void EmitCFIDefCfaRegister(int64_t Register);
virtual void EmitCFIOffset(int64_t Register, int64_t Offset);
virtual void EmitCFIPersonality(const MCSymbol *Sym, unsigned Encoding);
virtual void EmitCFILsda(const MCSymbol *Sym, unsigned Encoding);
virtual void EmitCFIRememberState();
virtual void EmitCFIRestoreState();
virtual void EmitCFISameValue(int64_t Register);
virtual void EmitCFIRestore(int64_t Register);
virtual void EmitCFIRelOffset(int64_t Register, int64_t Offset);
virtual void EmitCFIAdjustCfaOffset(int64_t Adjustment);
virtual void EmitCFIEscape(StringRef Values);
virtual void EmitCFISignalFrame();
virtual void EmitCFIUndefined(int64_t Register);
virtual void EmitCFIRegister(int64_t Register1, int64_t Register2);
virtual void EmitCFIWindowSave();
virtual void EmitWin64EHStartProc(const MCSymbol *Symbol);
virtual void EmitWin64EHEndProc();
virtual void EmitWin64EHStartChained();
virtual void EmitWin64EHEndChained();
virtual void EmitWin64EHHandler(const MCSymbol *Sym, bool Unwind,
bool Except);
virtual void EmitWin64EHHandlerData();
virtual void EmitWin64EHPushReg(unsigned Register);
virtual void EmitWin64EHSetFrame(unsigned Register, unsigned Offset);
virtual void EmitWin64EHAllocStack(unsigned Size);
virtual void EmitWin64EHSaveReg(unsigned Register, unsigned Offset);
virtual void EmitWin64EHSaveXMM(unsigned Register, unsigned Offset);
virtual void EmitWin64EHPushFrame(bool Code);
virtual void EmitWin64EHEndProlog();
/// EmitInstruction - Emit the given @p Instruction into the current
/// section.
virtual void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) = 0;
/// \brief Set the bundle alignment mode from now on in the section.
/// The argument is the power of 2 to which the alignment is set. The
/// value 0 means turn the bundle alignment off.
virtual void EmitBundleAlignMode(unsigned AlignPow2) = 0;
/// \brief The following instructions are a bundle-locked group.
///
/// \param AlignToEnd - If true, the bundle-locked group will be aligned to
/// the end of a bundle.
virtual void EmitBundleLock(bool AlignToEnd) = 0;
/// \brief Ends a bundle-locked group.
virtual void EmitBundleUnlock() = 0;
/// EmitRawText - If this file is backed by a assembly streamer, this dumps
/// the specified string in the output .s file. This capability is
/// indicated by the hasRawTextSupport() predicate. By default this aborts.
void EmitRawText(const Twine &String);
/// Flush - Causes any cached state to be written out.
virtual void Flush() {}
/// FinishImpl - Streamer specific finalization.
virtual void FinishImpl() = 0;
/// Finish - Finish emission of machine code.
void Finish();
};
/// createNullStreamer - Create a dummy machine code streamer, which does
/// nothing. This is useful for timing the assembler front end.
MCStreamer *createNullStreamer(MCContext &Ctx);
/// createAsmStreamer - Create a machine code streamer which will print out
/// assembly for the native target, suitable for compiling with a native
/// assembler.
///
/// \param InstPrint - If given, the instruction printer to use. If not given
/// the MCInst representation will be printed. This method takes ownership of
/// InstPrint.
///
/// \param CE - If given, a code emitter to use to show the instruction
/// encoding inline with the assembly. This method takes ownership of \p CE.
///
/// \param TAB - If given, a target asm backend to use to show the fixup
/// information in conjunction with encoding information. This method takes
/// ownership of \p TAB.
///
/// \param ShowInst - Whether to show the MCInst representation inline with
/// the assembly.
MCStreamer *createAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
bool isVerboseAsm, bool useCFI,
bool useDwarfDirectory, MCInstPrinter *InstPrint,
MCCodeEmitter *CE, MCAsmBackend *TAB,
bool ShowInst);
/// createMachOStreamer - Create a machine code streamer which will generate
/// Mach-O format object files.
///
/// Takes ownership of \p TAB and \p CE.
MCStreamer *createMachOStreamer(MCContext &Ctx, MCAsmBackend &TAB,
raw_ostream &OS, MCCodeEmitter *CE,
bool RelaxAll = false);
/// createWinCOFFStreamer - Create a machine code streamer which will
/// generate Microsoft COFF format object files.
///
/// Takes ownership of \p TAB and \p CE.
MCStreamer *createWinCOFFStreamer(MCContext &Ctx, MCAsmBackend &TAB,
MCCodeEmitter &CE, raw_ostream &OS,
bool RelaxAll = false);
/// createELFStreamer - Create a machine code streamer which will generate
/// ELF format object files.
MCStreamer *createELFStreamer(MCContext &Ctx, MCAsmBackend &TAB,
raw_ostream &OS, MCCodeEmitter *CE, bool RelaxAll,
bool NoExecStack);
} // end namespace llvm
#endif
Index: llvm/trunk/include/llvm/MC/MCDirectives.h
===================================================================
--- llvm/trunk/include/llvm/MC/MCDirectives.h (revision 204189)
+++ llvm/trunk/include/llvm/MC/MCDirectives.h (revision 204190)
@@ -1,65 +1,70 @@
//===- MCDirectives.h - Enums for directives on various targets -*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines various enums that represent target-specific directives.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_MC_MCDIRECTIVES_H
#define LLVM_MC_MCDIRECTIVES_H
namespace llvm {
enum MCSymbolAttr {
MCSA_Invalid = 0, ///< Not a valid directive.
// Various directives in alphabetical order.
MCSA_ELF_TypeFunction, ///< .type _foo, STT_FUNC # aka @function
MCSA_ELF_TypeIndFunction, ///< .type _foo, STT_GNU_IFUNC
MCSA_ELF_TypeObject, ///< .type _foo, STT_OBJECT # aka @object
MCSA_ELF_TypeTLS, ///< .type _foo, STT_TLS # aka @tls_object
MCSA_ELF_TypeCommon, ///< .type _foo, STT_COMMON # aka @common
MCSA_ELF_TypeNoType, ///< .type _foo, STT_NOTYPE # aka @notype
MCSA_ELF_TypeGnuUniqueObject, /// .type _foo, @gnu_unique_object
MCSA_Global, ///< .globl
MCSA_Hidden, ///< .hidden (ELF)
MCSA_IndirectSymbol, ///< .indirect_symbol (MachO)
MCSA_Internal, ///< .internal (ELF)
MCSA_LazyReference, ///< .lazy_reference (MachO)
MCSA_Local, ///< .local (ELF)
MCSA_NoDeadStrip, ///< .no_dead_strip (MachO)
MCSA_SymbolResolver, ///< .symbol_resolver (MachO)
MCSA_PrivateExtern, ///< .private_extern (MachO)
MCSA_Protected, ///< .protected (ELF)
MCSA_Reference, ///< .reference (MachO)
MCSA_Weak, ///< .weak
MCSA_WeakDefinition, ///< .weak_definition (MachO)
MCSA_WeakReference, ///< .weak_reference (MachO)
MCSA_WeakDefAutoPrivate ///< .weak_def_can_be_hidden (MachO)
};
enum MCAssemblerFlag {
MCAF_SyntaxUnified, ///< .syntax (ARM/ELF)
MCAF_SubsectionsViaSymbols, ///< .subsections_via_symbols (MachO)
MCAF_Code16, ///< .code16 (X86) / .code 16 (ARM)
MCAF_Code32, ///< .code32 (X86) / .code 32 (ARM)
MCAF_Code64 ///< .code64 (X86)
};
enum MCDataRegionType {
MCDR_DataRegion, ///< .data_region
MCDR_DataRegionJT8, ///< .data_region jt8
MCDR_DataRegionJT16, ///< .data_region jt16
MCDR_DataRegionJT32, ///< .data_region jt32
MCDR_DataRegionEnd ///< .end_data_region
};
+enum MCVersionMinType {
+ MCVM_IOSVersionMin, ///< .ios_version_min
+ MCVM_OSXVersionMin ///< .macosx_version_min
+};
+
} // end namespace llvm
#endif
Index: llvm/trunk/include/llvm/MC/MCAssembler.h
===================================================================
--- llvm/trunk/include/llvm/MC/MCAssembler.h (revision 204189)
+++ llvm/trunk/include/llvm/MC/MCAssembler.h (revision 204190)
@@ -1,1199 +1,1221 @@
//===- MCAssembler.h - Object File Generation -------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_MC_MCASSEMBLER_H
#define LLVM_MC_MCASSEMBLER_H
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/ilist.h"
#include "llvm/ADT/ilist_node.h"
+#include "llvm/MC/MCDirectives.h"
#include "llvm/MC/MCFixup.h"
#include "llvm/MC/MCInst.h"
#include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/DataTypes.h"
#include <algorithm>
#include <vector> // FIXME: Shouldn't be needed.
namespace llvm {
class raw_ostream;
class MCAsmLayout;
class MCAssembler;
class MCContext;
class MCCodeEmitter;
class MCExpr;
class MCFragment;
class MCObjectWriter;
class MCSection;
class MCSectionData;
class MCSubtargetInfo;
class MCSymbol;
class MCSymbolData;
class MCValue;
class MCAsmBackend;
class MCFragment : public ilist_node<MCFragment> {
friend class MCAsmLayout;
MCFragment(const MCFragment&) LLVM_DELETED_FUNCTION;
void operator=(const MCFragment&) LLVM_DELETED_FUNCTION;
public:
enum FragmentType {
FT_Align,
FT_Data,
FT_CompactEncodedInst,
FT_Fill,
FT_Relaxable,
FT_Org,
FT_Dwarf,
FT_DwarfFrame,
FT_LEB
};
private:
FragmentType Kind;
/// Parent - The data for the section this fragment is in.
MCSectionData *Parent;
/// Atom - The atom this fragment is in, as represented by it's defining
/// symbol.
MCSymbolData *Atom;
/// @name Assembler Backend Data
/// @{
//
// FIXME: This could all be kept private to the assembler implementation.
/// Offset - The offset of this fragment in its section. This is ~0 until
/// initialized.
uint64_t Offset;
/// LayoutOrder - The layout order of this fragment.
unsigned LayoutOrder;
/// @}
protected:
MCFragment(FragmentType _Kind, MCSectionData *_Parent = 0);
public:
// Only for sentinel.
MCFragment();
virtual ~MCFragment();
FragmentType getKind() const { return Kind; }
MCSectionData *getParent() const { return Parent; }
void setParent(MCSectionData *Value) { Parent = Value; }
MCSymbolData *getAtom() const { return Atom; }
void setAtom(MCSymbolData *Value) { Atom = Value; }
unsigned getLayoutOrder() const { return LayoutOrder; }
void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
/// \brief Does this fragment have instructions emitted into it? By default
/// this is false, but specific fragment types may set it to true.
virtual bool hasInstructions() const { return false; }
/// \brief Should this fragment be placed at the end of an aligned bundle?
virtual bool alignToBundleEnd() const { return false; }
virtual void setAlignToBundleEnd(bool V) { }
/// \brief Get the padding size that must be inserted before this fragment.
/// Used for bundling. By default, no padding is inserted.
/// Note that padding size is restricted to 8 bits. This is an optimization
/// to reduce the amount of space used for each fragment. In practice, larger
/// padding should never be required.
virtual uint8_t getBundlePadding() const {
return 0;
}
/// \brief Set the padding size for this fragment. By default it's a no-op,
/// and only some fragments have a meaningful implementation.
virtual void setBundlePadding(uint8_t N) {
}
void dump();
};
/// Interface implemented by fragments that contain encoded instructions and/or
/// data.
///
class MCEncodedFragment : public MCFragment {
virtual void anchor();
uint8_t BundlePadding;
public:
MCEncodedFragment(MCFragment::FragmentType FType, MCSectionData *SD = 0)
: MCFragment(FType, SD), BundlePadding(0)
{
}
virtual ~MCEncodedFragment();
virtual SmallVectorImpl<char> &getContents() = 0;
virtual const SmallVectorImpl<char> &getContents() const = 0;
uint8_t getBundlePadding() const override {
return BundlePadding;
}
void setBundlePadding(uint8_t N) override {
BundlePadding = N;
}
static bool classof(const MCFragment *F) {
MCFragment::FragmentType Kind = F->getKind();
switch (Kind) {
default:
return false;
case MCFragment::FT_Relaxable:
case MCFragment::FT_CompactEncodedInst:
case MCFragment::FT_Data:
return true;
}
}
};
/// Interface implemented by fragments that contain encoded instructions and/or
/// data and also have fixups registered.
///
class MCEncodedFragmentWithFixups : public MCEncodedFragment {
void anchor() override;
public:
MCEncodedFragmentWithFixups(MCFragment::FragmentType FType,
MCSectionData *SD = 0)
: MCEncodedFragment(FType, SD)
{
}
virtual ~MCEncodedFragmentWithFixups();
typedef SmallVectorImpl<MCFixup>::const_iterator const_fixup_iterator;
typedef SmallVectorImpl<MCFixup>::iterator fixup_iterator;
virtual SmallVectorImpl<MCFixup> &getFixups() = 0;
virtual const SmallVectorImpl<MCFixup> &getFixups() const = 0;
virtual fixup_iterator fixup_begin() = 0;
virtual const_fixup_iterator fixup_begin() const = 0;
virtual fixup_iterator fixup_end() = 0;
virtual const_fixup_iterator fixup_end() const = 0;
static bool classof(const MCFragment *F) {
MCFragment::FragmentType Kind = F->getKind();
return Kind == MCFragment::FT_Relaxable || Kind == MCFragment::FT_Data;
}
};
/// Fragment for data and encoded instructions.
///
class MCDataFragment : public MCEncodedFragmentWithFixups {
void anchor() override;
/// \brief Does this fragment contain encoded instructions anywhere in it?
bool HasInstructions;
/// \brief Should this fragment be aligned to the end of a bundle?
bool AlignToBundleEnd;
SmallVector<char, 32> Contents;
/// Fixups - The list of fixups in this fragment.
SmallVector<MCFixup, 4> Fixups;
public:
MCDataFragment(MCSectionData *SD = 0)
: MCEncodedFragmentWithFixups(FT_Data, SD),
HasInstructions(false), AlignToBundleEnd(false)
{
}
SmallVectorImpl<char> &getContents() override { return Contents; }
const SmallVectorImpl<char> &getContents() const override {
return Contents;
}
SmallVectorImpl<MCFixup> &getFixups() override {
return Fixups;
}
const SmallVectorImpl<MCFixup> &getFixups() const override {
return Fixups;
}
bool hasInstructions() const override { return HasInstructions; }
virtual void setHasInstructions(bool V) { HasInstructions = V; }
bool alignToBundleEnd() const override { return AlignToBundleEnd; }
void setAlignToBundleEnd(bool V) override { AlignToBundleEnd = V; }
fixup_iterator fixup_begin() override { return Fixups.begin(); }
const_fixup_iterator fixup_begin() const override { return Fixups.begin(); }
fixup_iterator fixup_end() override {return Fixups.end();}
const_fixup_iterator fixup_end() const override {return Fixups.end();}
static bool classof(const MCFragment *F) {
return F->getKind() == MCFragment::FT_Data;
}
};
/// This is a compact (memory-size-wise) fragment for holding an encoded
/// instruction (non-relaxable) that has no fixups registered. When applicable,
/// it can be used instead of MCDataFragment and lead to lower memory
/// consumption.
///
class MCCompactEncodedInstFragment : public MCEncodedFragment {
void anchor() override;
/// \brief Should this fragment be aligned to the end of a bundle?
bool AlignToBundleEnd;
SmallVector<char, 4> Contents;
public:
MCCompactEncodedInstFragment(MCSectionData *SD = 0)
: MCEncodedFragment(FT_CompactEncodedInst, SD), AlignToBundleEnd(false)
{
}
bool hasInstructions() const override {
return true;
}
SmallVectorImpl<char> &getContents() override { return Contents; }
const SmallVectorImpl<char> &getContents() const override { return Contents; }
bool alignToBundleEnd() const override { return AlignToBundleEnd; }
void setAlignToBundleEnd(bool V) override { AlignToBundleEnd = V; }
static bool classof(const MCFragment *F) {
return F->getKind() == MCFragment::FT_CompactEncodedInst;
}
};
/// A relaxable fragment holds on to its MCInst, since it may need to be
/// relaxed during the assembler layout and relaxation stage.
///
class MCRelaxableFragment : public MCEncodedFragmentWithFixups {
void anchor() override;
/// Inst - The instruction this is a fragment for.
MCInst Inst;
/// STI - The MCSubtargetInfo in effect when the instruction was encoded.
/// Keep a copy instead of a reference to make sure that updates to STI
/// in the assembler are not seen here.
const MCSubtargetInfo STI;
/// Contents - Binary data for the currently encoded instruction.
SmallVector<char, 8> Contents;
/// Fixups - The list of fixups in this fragment.
SmallVector<MCFixup, 1> Fixups;
public:
MCRelaxableFragment(const MCInst &_Inst,
const MCSubtargetInfo &_STI,
MCSectionData *SD = 0)
: MCEncodedFragmentWithFixups(FT_Relaxable, SD), Inst(_Inst), STI(_STI) {
}
SmallVectorImpl<char> &getContents() override { return Contents; }
const SmallVectorImpl<char> &getContents() const override { return Contents; }
const MCInst &getInst() const { return Inst; }
void setInst(const MCInst& Value) { Inst = Value; }
const MCSubtargetInfo &getSubtargetInfo() { return STI; }
SmallVectorImpl<MCFixup> &getFixups() override {
return Fixups;
}
const SmallVectorImpl<MCFixup> &getFixups() const override {
return Fixups;
}
bool hasInstructions() const override { return true; }
fixup_iterator fixup_begin() override { return Fixups.begin(); }
const_fixup_iterator fixup_begin() const override { return Fixups.begin(); }
fixup_iterator fixup_end() override {return Fixups.end();}
const_fixup_iterator fixup_end() const override {return Fixups.end();}
static bool classof(const MCFragment *F) {
return F->getKind() == MCFragment::FT_Relaxable;
}
};
class MCAlignFragment : public MCFragment {
virtual void anchor();
/// Alignment - The alignment to ensure, in bytes.
unsigned Alignment;
/// Value - Value to use for filling padding bytes.
int64_t Value;
/// ValueSize - The size of the integer (in bytes) of \p Value.
unsigned ValueSize;
/// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
/// cannot be satisfied in this width then this fragment is ignored.
unsigned MaxBytesToEmit;
/// EmitNops - Flag to indicate that (optimal) NOPs should be emitted instead
/// of using the provided value. The exact interpretation of this flag is
/// target dependent.
bool EmitNops : 1;
public:
MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize,
unsigned _MaxBytesToEmit, MCSectionData *SD = 0)
: MCFragment(FT_Align, SD), Alignment(_Alignment),
Value(_Value),ValueSize(_ValueSize),
MaxBytesToEmit(_MaxBytesToEmit), EmitNops(false) {}
/// @name Accessors
/// @{
unsigned getAlignment() const { return Alignment; }
int64_t getValue() const { return Value; }
unsigned getValueSize() const { return ValueSize; }
unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
bool hasEmitNops() const { return EmitNops; }
void setEmitNops(bool Value) { EmitNops = Value; }
/// @}
static bool classof(const MCFragment *F) {
return F->getKind() == MCFragment::FT_Align;
}
};
class MCFillFragment : public MCFragment {
virtual void anchor();
/// Value - Value to use for filling bytes.
int64_t Value;
/// ValueSize - The size (in bytes) of \p Value to use when filling, or 0 if
/// this is a virtual fill fragment.
unsigned ValueSize;
/// Size - The number of bytes to insert.
uint64_t Size;
public:
MCFillFragment(int64_t _Value, unsigned _ValueSize, uint64_t _Size,
MCSectionData *SD = 0)
: MCFragment(FT_Fill, SD),
Value(_Value), ValueSize(_ValueSize), Size(_Size) {
assert((!ValueSize || (Size % ValueSize) == 0) &&
"Fill size must be a multiple of the value size!");
}
/// @name Accessors
/// @{
int64_t getValue() const { return Value; }
unsigned getValueSize() const { return ValueSize; }
uint64_t getSize() const { return Size; }
/// @}
static bool classof(const MCFragment *F) {
return F->getKind() == MCFragment::FT_Fill;
}
};
class MCOrgFragment : public MCFragment {
virtual void anchor();
/// Offset - The offset this fragment should start at.
const MCExpr *Offset;
/// Value - Value to use for filling bytes.
int8_t Value;
public:
MCOrgFragment(const MCExpr &_Offset, int8_t _Value, MCSectionData *SD = 0)
: MCFragment(FT_Org, SD),
Offset(&_Offset), Value(_Value) {}
/// @name Accessors
/// @{
const MCExpr &getOffset() const { return *Offset; }
uint8_t getValue() const { return Value; }
/// @}
static bool classof(const MCFragment *F) {
return F->getKind() == MCFragment::FT_Org;
}
};
class MCLEBFragment : public MCFragment {
virtual void anchor();
/// Value - The value this fragment should contain.
const MCExpr *Value;
/// IsSigned - True if this is a sleb128, false if uleb128.
bool IsSigned;
SmallString<8> Contents;
public:
MCLEBFragment(const MCExpr &Value_, bool IsSigned_, MCSectionData *SD = 0)
: MCFragment(FT_LEB, SD),
Value(&Value_), IsSigned(IsSigned_) { Contents.push_back(0); }
/// @name Accessors
/// @{
const MCExpr &getValue() const { return *Value; }
bool isSigned() const { return IsSigned; }
SmallString<8> &getContents() { return Contents; }
const SmallString<8> &getContents() const { return Contents; }
/// @}
static bool classof(const MCFragment *F) {
return F->getKind() == MCFragment::FT_LEB;
}
};
class MCDwarfLineAddrFragment : public MCFragment {
virtual void anchor();
/// LineDelta - the value of the difference between the two line numbers
/// between two .loc dwarf directives.
int64_t LineDelta;
/// AddrDelta - The expression for the difference of the two symbols that
/// make up the address delta between two .loc dwarf directives.
const MCExpr *AddrDelta;
SmallString<8> Contents;
public:
MCDwarfLineAddrFragment(int64_t _LineDelta, const MCExpr &_AddrDelta,
MCSectionData *SD = 0)
: MCFragment(FT_Dwarf, SD),
LineDelta(_LineDelta), AddrDelta(&_AddrDelta) { Contents.push_back(0); }
/// @name Accessors
/// @{
int64_t getLineDelta() const { return LineDelta; }
const MCExpr &getAddrDelta() const { return *AddrDelta; }
SmallString<8> &getContents() { return Contents; }
const SmallString<8> &getContents() const { return Contents; }
/// @}
static bool classof(const MCFragment *F) {
return F->getKind() == MCFragment::FT_Dwarf;
}
};
class MCDwarfCallFrameFragment : public MCFragment {
virtual void anchor();
/// AddrDelta - The expression for the difference of the two symbols that
/// make up the address delta between two .cfi_* dwarf directives.
const MCExpr *AddrDelta;
SmallString<8> Contents;
public:
MCDwarfCallFrameFragment(const MCExpr &_AddrDelta, MCSectionData *SD = 0)
: MCFragment(FT_DwarfFrame, SD),
AddrDelta(&_AddrDelta) { Contents.push_back(0); }
/// @name Accessors
/// @{
const MCExpr &getAddrDelta() const { return *AddrDelta; }
SmallString<8> &getContents() { return Contents; }
const SmallString<8> &getContents() const { return Contents; }
/// @}
static bool classof(const MCFragment *F) {
return F->getKind() == MCFragment::FT_DwarfFrame;
}
};
// FIXME: Should this be a separate class, or just merged into MCSection? Since
// we anticipate the fast path being through an MCAssembler, the only reason to
// keep it out is for API abstraction.
class MCSectionData : public ilist_node<MCSectionData> {
friend class MCAsmLayout;
MCSectionData(const MCSectionData&) LLVM_DELETED_FUNCTION;
void operator=(const MCSectionData&) LLVM_DELETED_FUNCTION;
public:
typedef iplist<MCFragment> FragmentListType;
typedef FragmentListType::const_iterator const_iterator;
typedef FragmentListType::iterator iterator;
typedef FragmentListType::const_reverse_iterator const_reverse_iterator;
typedef FragmentListType::reverse_iterator reverse_iterator;
/// \brief Express the state of bundle locked groups while emitting code.
enum BundleLockStateType {
NotBundleLocked,
BundleLocked,
BundleLockedAlignToEnd
};
private:
FragmentListType Fragments;
const MCSection *Section;
/// Ordinal - The section index in the assemblers section list.
unsigned Ordinal;
/// LayoutOrder - The index of this section in the layout order.
unsigned LayoutOrder;
/// Alignment - The maximum alignment seen in this section.
unsigned Alignment;
/// \brief Keeping track of bundle-locked state.
BundleLockStateType BundleLockState;
/// \brief We've seen a bundle_lock directive but not its first instruction
/// yet.
bool BundleGroupBeforeFirstInst;
/// @name Assembler Backend Data
/// @{
//
// FIXME: This could all be kept private to the assembler implementation.
/// HasInstructions - Whether this section has had instructions emitted into
/// it.
unsigned HasInstructions : 1;
/// Mapping from subsection number to insertion point for subsection numbers
/// below that number.
SmallVector<std::pair<unsigned, MCFragment *>, 1> SubsectionFragmentMap;
/// @}
public:
// Only for use as sentinel.
MCSectionData();
MCSectionData(const MCSection &Section, MCAssembler *A = 0);
const MCSection &getSection() const { return *Section; }
unsigned getAlignment() const { return Alignment; }
void setAlignment(unsigned Value) { Alignment = Value; }
bool hasInstructions() const { return HasInstructions; }
void setHasInstructions(bool Value) { HasInstructions = Value; }
unsigned getOrdinal() const { return Ordinal; }
void setOrdinal(unsigned Value) { Ordinal = Value; }
unsigned getLayoutOrder() const { return LayoutOrder; }
void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
/// @name Fragment Access
/// @{
const FragmentListType &getFragmentList() const { return Fragments; }
FragmentListType &getFragmentList() { return Fragments; }
iterator begin() { return Fragments.begin(); }
const_iterator begin() const { return Fragments.begin(); }
iterator end() { return Fragments.end(); }
const_iterator end() const { return Fragments.end(); }
reverse_iterator rbegin() { return Fragments.rbegin(); }
const_reverse_iterator rbegin() const { return Fragments.rbegin(); }
reverse_iterator rend() { return Fragments.rend(); }
const_reverse_iterator rend() const { return Fragments.rend(); }
size_t size() const { return Fragments.size(); }
bool empty() const { return Fragments.empty(); }
iterator getSubsectionInsertionPoint(unsigned Subsection);
bool isBundleLocked() const {
return BundleLockState != NotBundleLocked;
}
BundleLockStateType getBundleLockState() const {
return BundleLockState;
}
void setBundleLockState(BundleLockStateType NewState) {
BundleLockState = NewState;
}
bool isBundleGroupBeforeFirstInst() const {
return BundleGroupBeforeFirstInst;
}
void setBundleGroupBeforeFirstInst(bool IsFirst) {
BundleGroupBeforeFirstInst = IsFirst;
}
void dump();
/// @}
};
// FIXME: Same concerns as with SectionData.
class MCSymbolData : public ilist_node<MCSymbolData> {
public:
const MCSymbol *Symbol;
/// Fragment - The fragment this symbol's value is relative to, if any.
MCFragment *Fragment;
/// Offset - The offset to apply to the fragment address to form this symbol's
/// value.
uint64_t Offset;
/// IsExternal - True if this symbol is visible outside this translation
/// unit.
unsigned IsExternal : 1;
/// IsPrivateExtern - True if this symbol is private extern.
unsigned IsPrivateExtern : 1;
/// CommonSize - The size of the symbol, if it is 'common', or 0.
//
// FIXME: Pack this in with other fields? We could put it in offset, since a
// common symbol can never get a definition.
uint64_t CommonSize;
/// SymbolSize - An expression describing how to calculate the size of
/// a symbol. If a symbol has no size this field will be NULL.
const MCExpr *SymbolSize;
/// CommonAlign - The alignment of the symbol, if it is 'common'.
//
// FIXME: Pack this in with other fields?
unsigned CommonAlign;
/// Flags - The Flags field is used by object file implementations to store
/// additional per symbol information which is not easily classified.
uint32_t Flags;
/// Index - Index field, for use by the object file implementation.
uint64_t Index;
public:
// Only for use as sentinel.
MCSymbolData();
MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset,
MCAssembler *A = 0);
/// @name Accessors
/// @{
const MCSymbol &getSymbol() const { return *Symbol; }
MCFragment *getFragment() const { return Fragment; }
void setFragment(MCFragment *Value) { Fragment = Value; }
uint64_t getOffset() const { return Offset; }
void setOffset(uint64_t Value) { Offset = Value; }
/// @}
/// @name Symbol Attributes
/// @{
bool isExternal() const { return IsExternal; }
void setExternal(bool Value) { IsExternal = Value; }
bool isPrivateExtern() const { return IsPrivateExtern; }
void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
/// isCommon - Is this a 'common' symbol.
bool isCommon() const { return CommonSize != 0; }
/// setCommon - Mark this symbol as being 'common'.
///
/// \param Size - The size of the symbol.
/// \param Align - The alignment of the symbol.
void setCommon(uint64_t Size, unsigned Align) {
CommonSize = Size;
CommonAlign = Align;
}
/// getCommonSize - Return the size of a 'common' symbol.
uint64_t getCommonSize() const {
assert(isCommon() && "Not a 'common' symbol!");
return CommonSize;
}
void setSize(const MCExpr *SS) {
SymbolSize = SS;
}
const MCExpr *getSize() const {
return SymbolSize;
}
/// getCommonAlignment - Return the alignment of a 'common' symbol.
unsigned getCommonAlignment() const {
assert(isCommon() && "Not a 'common' symbol!");
return CommonAlign;
}
/// getFlags - Get the (implementation defined) symbol flags.
uint32_t getFlags() const { return Flags; }
/// setFlags - Set the (implementation defined) symbol flags.
void setFlags(uint32_t Value) { Flags = Value; }
/// modifyFlags - Modify the flags via a mask
void modifyFlags(uint32_t Value, uint32_t Mask) {
Flags = (Flags & ~Mask) | Value;
}
/// getIndex - Get the (implementation defined) index.
uint64_t getIndex() const { return Index; }
/// setIndex - Set the (implementation defined) index.
void setIndex(uint64_t Value) { Index = Value; }
/// @}
void dump();
};
// FIXME: This really doesn't belong here. See comments below.
struct IndirectSymbolData {
MCSymbol *Symbol;
MCSectionData *SectionData;
};
// FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk
// to one another.
struct DataRegionData {
// This enum should be kept in sync w/ the mach-o definition in
// llvm/Object/MachOFormat.h.
enum KindTy { Data = 1, JumpTable8, JumpTable16, JumpTable32 } Kind;
MCSymbol *Start;
MCSymbol *End;
};
class MCAssembler {
friend class MCAsmLayout;
public:
typedef iplist<MCSectionData> SectionDataListType;
typedef iplist<MCSymbolData> SymbolDataListType;
typedef SectionDataListType::const_iterator const_iterator;
typedef SectionDataListType::iterator iterator;
typedef SymbolDataListType::const_iterator const_symbol_iterator;
typedef SymbolDataListType::iterator symbol_iterator;
typedef std::vector<std::string> FileNameVectorType;
typedef FileNameVectorType::const_iterator const_file_name_iterator;
typedef std::vector<IndirectSymbolData>::const_iterator
const_indirect_symbol_iterator;
typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
typedef std::vector<DataRegionData>::const_iterator
const_data_region_iterator;
typedef std::vector<DataRegionData>::iterator data_region_iterator;
+ /// MachO specific deployment target version info.
+ // A Major version of 0 indicates that no version information was supplied
+ // and so the corresponding load command should not be emitted.
+ typedef struct {
+ MCVersionMinType Kind;
+ unsigned Major;
+ unsigned Minor;
+ unsigned Update;
+ } VersionMinInfoType;
private:
MCAssembler(const MCAssembler&) LLVM_DELETED_FUNCTION;
void operator=(const MCAssembler&) LLVM_DELETED_FUNCTION;
MCContext &Context;
MCAsmBackend &Backend;
MCCodeEmitter &Emitter;
MCObjectWriter &Writer;
raw_ostream &OS;
iplist<MCSectionData> Sections;
iplist<MCSymbolData> Symbols;
/// The map of sections to their associated assembler backend data.
//
// FIXME: Avoid this indirection?
DenseMap<const MCSection*, MCSectionData*> SectionMap;
/// The map of symbols to their associated assembler backend data.
//
// FIXME: Avoid this indirection?
DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
std::vector<IndirectSymbolData> IndirectSymbols;
std::vector<DataRegionData> DataRegions;
/// The list of linker options to propagate into the object file.
std::vector<std::vector<std::string> > LinkerOptions;
/// List of declared file names
FileNameVectorType FileNames;
/// The set of function symbols for which a .thumb_func directive has
/// been seen.
//
// FIXME: We really would like this in target specific code rather than
// here. Maybe when the relocation stuff moves to target specific,
// this can go with it? The streamer would need some target specific
// refactoring too.
SmallPtrSet<const MCSymbol*, 64> ThumbFuncs;
/// \brief The bundle alignment size currently set in the assembler.
///
/// By default it's 0, which means bundling is disabled.
unsigned BundleAlignSize;
unsigned RelaxAll : 1;
unsigned NoExecStack : 1;
unsigned SubsectionsViaSymbols : 1;
/// ELF specific e_header flags
// It would be good if there were an MCELFAssembler class to hold this.
// ELF header flags are used both by the integrated and standalone assemblers.
// Access to the flags is necessary in cases where assembler directives affect
// which flags to be set.
unsigned ELFHeaderEFlags;
+
+ VersionMinInfoType VersionMinInfo;
private:
/// Evaluate a fixup to a relocatable expression and the value which should be
/// placed into the fixup.
///
/// \param Layout The layout to use for evaluation.
/// \param Fixup The fixup to evaluate.
/// \param DF The fragment the fixup is inside.
/// \param Target [out] On return, the relocatable expression the fixup
/// evaluates to.
/// \param Value [out] On return, the value of the fixup as currently laid
/// out.
/// \return Whether the fixup value was fully resolved. This is true if the
/// \p Value result is fixed, otherwise the value may change due to
/// relocation.
bool evaluateFixup(const MCAsmLayout &Layout,
const MCFixup &Fixup, const MCFragment *DF,
MCValue &Target, uint64_t &Value) const;
/// Check whether a fixup can be satisfied, or whether it needs to be relaxed
/// (increased in size, in order to hold its value correctly).
bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCRelaxableFragment *DF,
const MCAsmLayout &Layout) const;
/// Check whether the given fragment needs relaxation.
bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF,
const MCAsmLayout &Layout) const;
/// \brief Perform one layout iteration and return true if any offsets
/// were adjusted.
bool layoutOnce(MCAsmLayout &Layout);
/// \brief Perform one layout iteration of the given section and return true
/// if any offsets were adjusted.
bool layoutSectionOnce(MCAsmLayout &Layout, MCSectionData &SD);
bool relaxInstruction(MCAsmLayout &Layout, MCRelaxableFragment &IF);
bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
MCDwarfCallFrameFragment &DF);
/// finishLayout - Finalize a layout, including fragment lowering.
void finishLayout(MCAsmLayout &Layout);
uint64_t handleFixup(const MCAsmLayout &Layout,
MCFragment &F, const MCFixup &Fixup);
public:
/// Compute the effective fragment size assuming it is laid out at the given
/// \p SectionAddress and \p FragmentOffset.
uint64_t computeFragmentSize(const MCAsmLayout &Layout,
const MCFragment &F) const;
/// Find the symbol which defines the atom containing the given symbol, or
/// null if there is no such symbol.
const MCSymbolData *getAtom(const MCSymbolData *Symbol) const;
/// Check whether a particular symbol is visible to the linker and is required
/// in the symbol table, or whether it can be discarded by the assembler. This
/// also effects whether the assembler treats the label as potentially
/// defining a separate atom.
bool isSymbolLinkerVisible(const MCSymbol &SD) const;
/// Emit the section contents using the given object writer.
void writeSectionData(const MCSectionData *Section,
const MCAsmLayout &Layout) const;
/// Check whether a given symbol has been flagged with .thumb_func.
bool isThumbFunc(const MCSymbol *Func) const {
return ThumbFuncs.count(Func);
}
/// Flag a function symbol as the target of a .thumb_func directive.
void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
/// ELF e_header flags
unsigned getELFHeaderEFlags() const {return ELFHeaderEFlags;}
void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags;}
+ /// MachO deployment target version information.
+ const VersionMinInfoType &getVersionMinInfo() const { return VersionMinInfo; }
+ void setVersionMinInfo(MCVersionMinType Kind, unsigned Major, unsigned Minor,
+ unsigned Update) {
+ VersionMinInfo.Kind = Kind;
+ VersionMinInfo.Major = Major;
+ VersionMinInfo.Minor = Minor;
+ VersionMinInfo.Update = Update;
+ }
+
public:
/// Construct a new assembler instance.
///
/// \param OS The stream to output to.
//
// FIXME: How are we going to parameterize this? Two obvious options are stay
// concrete and require clients to pass in a target like object. The other
// option is to make this abstract, and have targets provide concrete
// implementations as we do with AsmParser.
MCAssembler(MCContext &Context_, MCAsmBackend &Backend_,
MCCodeEmitter &Emitter_, MCObjectWriter &Writer_,
raw_ostream &OS);
~MCAssembler();
/// Reuse an assembler instance
///
void reset();
MCContext &getContext() const { return Context; }
MCAsmBackend &getBackend() const { return Backend; }
MCCodeEmitter &getEmitter() const { return Emitter; }
MCObjectWriter &getWriter() const { return Writer; }
/// Finish - Do final processing and write the object to the output stream.
/// \p Writer is used for custom object writer (as the MCJIT does),
/// if not specified it is automatically created from backend.
void Finish();
// FIXME: This does not belong here.
bool getSubsectionsViaSymbols() const {
return SubsectionsViaSymbols;
}
void setSubsectionsViaSymbols(bool Value) {
SubsectionsViaSymbols = Value;
}
bool getRelaxAll() const { return RelaxAll; }
void setRelaxAll(bool Value) { RelaxAll = Value; }
bool getNoExecStack() const { return NoExecStack; }
void setNoExecStack(bool Value) { NoExecStack = Value; }
bool isBundlingEnabled() const {
return BundleAlignSize != 0;
}
unsigned getBundleAlignSize() const {
return BundleAlignSize;
}
void setBundleAlignSize(unsigned Size) {
assert((Size == 0 || !(Size & (Size - 1))) &&
"Expect a power-of-two bundle align size");
BundleAlignSize = Size;
}
/// @name Section List Access
/// @{
const SectionDataListType &getSectionList() const { return Sections; }
SectionDataListType &getSectionList() { return Sections; }
iterator begin() { return Sections.begin(); }
const_iterator begin() const { return Sections.begin(); }
iterator end() { return Sections.end(); }
const_iterator end() const { return Sections.end(); }
size_t size() const { return Sections.size(); }
/// @}
/// @name Symbol List Access
/// @{
const SymbolDataListType &getSymbolList() const { return Symbols; }
SymbolDataListType &getSymbolList() { return Symbols; }
symbol_iterator symbol_begin() { return Symbols.begin(); }
const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
symbol_iterator symbol_end() { return Symbols.end(); }
const_symbol_iterator symbol_end() const { return Symbols.end(); }
size_t symbol_size() const { return Symbols.size(); }
/// @}
/// @name Indirect Symbol List Access
/// @{
// FIXME: This is a total hack, this should not be here. Once things are
// factored so that the streamer has direct access to the .o writer, it can
// disappear.
std::vector<IndirectSymbolData> &getIndirectSymbols() {
return IndirectSymbols;
}
indirect_symbol_iterator indirect_symbol_begin() {
return IndirectSymbols.begin();
}
const_indirect_symbol_iterator indirect_symbol_begin() const {
return IndirectSymbols.begin();
}
indirect_symbol_iterator indirect_symbol_end() {
return IndirectSymbols.end();
}
const_indirect_symbol_iterator indirect_symbol_end() const {
return IndirectSymbols.end();
}
size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
/// @}
/// @name Linker Option List Access
/// @{
std::vector<std::vector<std::string> > &getLinkerOptions() {
return LinkerOptions;
}
/// @}
/// @name Data Region List Access
/// @{
// FIXME: This is a total hack, this should not be here. Once things are
// factored so that the streamer has direct access to the .o writer, it can
// disappear.
std::vector<DataRegionData> &getDataRegions() {
return DataRegions;
}
data_region_iterator data_region_begin() {
return DataRegions.begin();
}
const_data_region_iterator data_region_begin() const {
return DataRegions.begin();
}
data_region_iterator data_region_end() {
return DataRegions.end();
}
const_data_region_iterator data_region_end() const {
return DataRegions.end();
}
size_t data_region_size() const { return DataRegions.size(); }
/// @}
/// @name Backend Data Access
/// @{
MCSectionData &getSectionData(const MCSection &Section) const {
MCSectionData *Entry = SectionMap.lookup(&Section);
assert(Entry && "Missing section data!");
return *Entry;
}
MCSectionData &getOrCreateSectionData(const MCSection &Section,
bool *Created = 0) {
MCSectionData *&Entry = SectionMap[&Section];
if (Created) *Created = !Entry;
if (!Entry)
Entry = new MCSectionData(Section, this);
return *Entry;
}
bool hasSymbolData(const MCSymbol &Symbol) const {
return SymbolMap.lookup(&Symbol) != 0;
}
MCSymbolData &getSymbolData(const MCSymbol &Symbol) const {
MCSymbolData *Entry = SymbolMap.lookup(&Symbol);
assert(Entry && "Missing symbol data!");
return *Entry;
}
MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
bool *Created = 0) {
MCSymbolData *&Entry = SymbolMap[&Symbol];
if (Created) *Created = !Entry;
if (!Entry)
Entry = new MCSymbolData(Symbol, 0, 0, this);
return *Entry;
}
const_file_name_iterator file_names_begin() const {
return FileNames.begin();
}
const_file_name_iterator file_names_end() const {
return FileNames.end();
}
void addFileName(StringRef FileName) {
if (std::find(file_names_begin(), file_names_end(), FileName) ==
file_names_end())
FileNames.push_back(FileName);
}
/// @}
void dump();
};
} // end namespace llvm
#endif
Index: llvm/trunk/include/llvm/Object/MachO.h
===================================================================
--- llvm/trunk/include/llvm/Object/MachO.h (revision 204189)
+++ llvm/trunk/include/llvm/Object/MachO.h (revision 204190)
@@ -1,271 +1,273 @@
//===- MachO.h - MachO object file implementation ---------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file declares the MachOObjectFile class, which implement the ObjectFile
// interface for MachO files.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_OBJECT_MACHO_H
#define LLVM_OBJECT_MACHO_H
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Triple.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/Support/MachO.h"
namespace llvm {
namespace object {
/// DiceRef - This is a value type class that represents a single
/// data in code entry in the table in a Mach-O object file.
class DiceRef {
DataRefImpl DicePimpl;
const ObjectFile *OwningObject;
public:
DiceRef() : OwningObject(NULL) { }
DiceRef(DataRefImpl DiceP, const ObjectFile *Owner);
bool operator==(const DiceRef &Other) const;
bool operator<(const DiceRef &Other) const;
void moveNext();
error_code getOffset(uint32_t &Result) const;
error_code getLength(uint16_t &Result) const;
error_code getKind(uint16_t &Result) const;
DataRefImpl getRawDataRefImpl() const;
const ObjectFile *getObjectFile() const;
};
typedef content_iterator<DiceRef> dice_iterator;
class MachOObjectFile : public ObjectFile {
public:
struct LoadCommandInfo {
const char *Ptr; // Where in memory the load command is.
MachO::load_command C; // The command itself.
};
MachOObjectFile(MemoryBuffer *Object, bool IsLittleEndian, bool Is64Bits,
error_code &EC, bool BufferOwned = true);
void moveSymbolNext(DataRefImpl &Symb) const override;
error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const override;
error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const override;
error_code getSymbolFileOffset(DataRefImpl Symb,
uint64_t &Res) const override;
error_code getSymbolAlignment(DataRefImpl Symb, uint32_t &Res) const override;
error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const override;
error_code getSymbolType(DataRefImpl Symb,
SymbolRef::Type &Res) const override;
uint32_t getSymbolFlags(DataRefImpl Symb) const override;
error_code getSymbolSection(DataRefImpl Symb,
section_iterator &Res) const override;
error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const override;
void moveSectionNext(DataRefImpl &Sec) const override;
error_code getSectionName(DataRefImpl Sec, StringRef &Res) const override;
error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const override;
error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const override;
error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const override;
error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const override;
error_code isSectionText(DataRefImpl Sec, bool &Res) const override;
error_code isSectionData(DataRefImpl Sec, bool &Res) const override;
error_code isSectionBSS(DataRefImpl Sec, bool &Res) const override;
error_code isSectionRequiredForExecution(DataRefImpl Sec,
bool &Res) const override;
error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const override;
error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const override;
error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const override;
error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
bool &Result) const override;
relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
relocation_iterator section_rel_end(DataRefImpl Sec) const override;
void moveRelocationNext(DataRefImpl &Rel) const override;
error_code getRelocationAddress(DataRefImpl Rel,
uint64_t &Res) const override;
error_code getRelocationOffset(DataRefImpl Rel, uint64_t &Res) const override;
symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
error_code getRelocationType(DataRefImpl Rel, uint64_t &Res) const override;
error_code getRelocationTypeName(DataRefImpl Rel,
SmallVectorImpl<char> &Result) const override;
error_code getRelocationValueString(DataRefImpl Rel,
SmallVectorImpl<char> &Result) const override;
error_code getRelocationHidden(DataRefImpl Rel, bool &Result) const override;
error_code getLibraryNext(DataRefImpl LibData,
LibraryRef &Res) const override;
error_code getLibraryPath(DataRefImpl LibData, StringRef &Res) const override;
// TODO: Would be useful to have an iterator based version
// of the load command interface too.
basic_symbol_iterator symbol_begin_impl() const override;
basic_symbol_iterator symbol_end_impl() const override;
section_iterator section_begin() const override;
section_iterator section_end() const override;
library_iterator needed_library_begin() const override;
library_iterator needed_library_end() const override;
uint8_t getBytesInAddress() const override;
StringRef getFileFormatName() const override;
unsigned getArch() const override;
StringRef getLoadName() const override;
relocation_iterator section_rel_begin(unsigned Index) const;
relocation_iterator section_rel_end(unsigned Index) const;
dice_iterator begin_dices() const;
dice_iterator end_dices() const;
// In a MachO file, sections have a segment name. This is used in the .o
// files. They have a single segment, but this field specifies which segment
// a section should be put in in the final object.
StringRef getSectionFinalSegmentName(DataRefImpl Sec) const;
// Names are stored as 16 bytes. These returns the raw 16 bytes without
// interpreting them as a C string.
ArrayRef<char> getSectionRawName(DataRefImpl Sec) const;
ArrayRef<char> getSectionRawFinalSegmentName(DataRefImpl Sec) const;
// MachO specific Info about relocations.
bool isRelocationScattered(const MachO::any_relocation_info &RE) const;
unsigned getPlainRelocationSymbolNum(
const MachO::any_relocation_info &RE) const;
bool getPlainRelocationExternal(const MachO::any_relocation_info &RE) const;
bool getScatteredRelocationScattered(
const MachO::any_relocation_info &RE) const;
uint32_t getScatteredRelocationValue(
const MachO::any_relocation_info &RE) const;
unsigned getAnyRelocationAddress(const MachO::any_relocation_info &RE) const;
unsigned getAnyRelocationPCRel(const MachO::any_relocation_info &RE) const;
unsigned getAnyRelocationLength(const MachO::any_relocation_info &RE) const;
unsigned getAnyRelocationType(const MachO::any_relocation_info &RE) const;
SectionRef getRelocationSection(const MachO::any_relocation_info &RE) const;
// Walk load commands.
LoadCommandInfo getFirstLoadCommandInfo() const;
LoadCommandInfo getNextLoadCommandInfo(const LoadCommandInfo &L) const;
// MachO specific structures.
MachO::section getSection(DataRefImpl DRI) const;
MachO::section_64 getSection64(DataRefImpl DRI) const;
MachO::section getSection(const LoadCommandInfo &L, unsigned Index) const;
MachO::section_64 getSection64(const LoadCommandInfo &L,unsigned Index) const;
MachO::nlist getSymbolTableEntry(DataRefImpl DRI) const;
MachO::nlist_64 getSymbol64TableEntry(DataRefImpl DRI) const;
MachO::linkedit_data_command
getLinkeditDataLoadCommand(const LoadCommandInfo &L) const;
MachO::segment_command
getSegmentLoadCommand(const LoadCommandInfo &L) const;
MachO::segment_command_64
getSegment64LoadCommand(const LoadCommandInfo &L) const;
MachO::linker_options_command
getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const;
+ MachO::version_min_command
+ getVersionMinLoadCommand(const LoadCommandInfo &L) const;
MachO::any_relocation_info getRelocation(DataRefImpl Rel) const;
MachO::data_in_code_entry getDice(DataRefImpl Rel) const;
MachO::mach_header getHeader() const;
MachO::mach_header_64 getHeader64() const;
uint32_t
getIndirectSymbolTableEntry(const MachO::dysymtab_command &DLC,
unsigned Index) const;
MachO::data_in_code_entry getDataInCodeTableEntry(uint32_t DataOffset,
unsigned Index) const;
MachO::symtab_command getSymtabLoadCommand() const;
MachO::dysymtab_command getDysymtabLoadCommand() const;
MachO::linkedit_data_command getDataInCodeLoadCommand() const;
StringRef getStringTableData() const;
bool is64Bit() const;
void ReadULEB128s(uint64_t Index, SmallVectorImpl<uint64_t> &Out) const;
static Triple::ArchType getArch(uint32_t CPUType);
static bool classof(const Binary *v) {
return v->isMachO();
}
private:
typedef SmallVector<const char*, 1> SectionList;
SectionList Sections;
const char *SymtabLoadCmd;
const char *DysymtabLoadCmd;
const char *DataInCodeLoadCmd;
};
/// DiceRef
inline DiceRef::DiceRef(DataRefImpl DiceP, const ObjectFile *Owner)
: DicePimpl(DiceP) , OwningObject(Owner) {}
inline bool DiceRef::operator==(const DiceRef &Other) const {
return DicePimpl == Other.DicePimpl;
}
inline bool DiceRef::operator<(const DiceRef &Other) const {
return DicePimpl < Other.DicePimpl;
}
inline void DiceRef::moveNext() {
const MachO::data_in_code_entry *P =
reinterpret_cast<const MachO::data_in_code_entry *>(DicePimpl.p);
DicePimpl.p = reinterpret_cast<uintptr_t>(P + 1);
}
// Since a Mach-O data in code reference, a DiceRef, can only be created when
// the OwningObject ObjectFile is a MachOObjectFile a static_cast<> is used for
// the methods that get the values of the fields of the reference.
inline error_code DiceRef::getOffset(uint32_t &Result) const {
const MachOObjectFile *MachOOF =
static_cast<const MachOObjectFile *>(OwningObject);
MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
Result = Dice.offset;
return object_error::success;
}
inline error_code DiceRef::getLength(uint16_t &Result) const {
const MachOObjectFile *MachOOF =
static_cast<const MachOObjectFile *>(OwningObject);
MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
Result = Dice.length;
return object_error::success;
}
inline error_code DiceRef::getKind(uint16_t &Result) const {
const MachOObjectFile *MachOOF =
static_cast<const MachOObjectFile *>(OwningObject);
MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
Result = Dice.kind;
return object_error::success;
}
inline DataRefImpl DiceRef::getRawDataRefImpl() const {
return DicePimpl;
}
inline const ObjectFile *DiceRef::getObjectFile() const {
return OwningObject;
}
}
}
#endif
Index: llvm/trunk/tools/macho-dump/macho-dump.cpp
===================================================================
--- llvm/trunk/tools/macho-dump/macho-dump.cpp (revision 204189)
+++ llvm/trunk/tools/macho-dump/macho-dump.cpp (revision 204190)
@@ -1,415 +1,427 @@
//===-- macho-dump.cpp - Mach Object Dumping Tool -------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This is a testing tool for use with the MC/Mach-O LLVM components.
//
//===----------------------------------------------------------------------===//
#include "llvm/Object/MachO.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/system_error.h"
using namespace llvm;
using namespace llvm::object;
static cl::opt<std::string>
InputFile(cl::Positional, cl::desc("<input file>"), cl::init("-"));
static cl::opt<bool>
ShowSectionData("dump-section-data", cl::desc("Dump the contents of sections"),
cl::init(false));
///
static const char *ProgramName;
static void Message(const char *Type, const Twine &Msg) {
errs() << ProgramName << ": " << Type << ": " << Msg << "\n";
}
static int Error(const Twine &Msg) {
Message("error", Msg);
return 1;
}
static void Warning(const Twine &Msg) {
Message("warning", Msg);
}
///
static void DumpSegmentCommandData(StringRef Name,
uint64_t VMAddr, uint64_t VMSize,
uint64_t FileOffset, uint64_t FileSize,
uint32_t MaxProt, uint32_t InitProt,
uint32_t NumSections, uint32_t Flags) {
outs() << " ('segment_name', '";
outs().write_escaped(Name, /*UseHexEscapes=*/true) << "')\n";
outs() << " ('vm_addr', " << VMAddr << ")\n";
outs() << " ('vm_size', " << VMSize << ")\n";
outs() << " ('file_offset', " << FileOffset << ")\n";
outs() << " ('file_size', " << FileSize << ")\n";
outs() << " ('maxprot', " << MaxProt << ")\n";
outs() << " ('initprot', " << InitProt << ")\n";
outs() << " ('num_sections', " << NumSections << ")\n";
outs() << " ('flags', " << Flags << ")\n";
}
static int DumpSectionData(const MachOObjectFile &Obj, unsigned Index,
StringRef Name,
StringRef SegmentName, uint64_t Address,
uint64_t Size, uint32_t Offset,
uint32_t Align, uint32_t RelocationTableOffset,
uint32_t NumRelocationTableEntries,
uint32_t Flags, uint32_t Reserved1,
uint32_t Reserved2, uint64_t Reserved3 = ~0ULL) {
outs() << " # Section " << Index << "\n";
outs() << " (('section_name', '";
outs().write_escaped(Name, /*UseHexEscapes=*/true) << "')\n";
outs() << " ('segment_name', '";
outs().write_escaped(SegmentName, /*UseHexEscapes=*/true) << "')\n";
outs() << " ('address', " << Address << ")\n";
outs() << " ('size', " << Size << ")\n";
outs() << " ('offset', " << Offset << ")\n";
outs() << " ('alignment', " << Align << ")\n";
outs() << " ('reloc_offset', " << RelocationTableOffset << ")\n";
outs() << " ('num_reloc', " << NumRelocationTableEntries << ")\n";
outs() << " ('flags', " << format("0x%x", Flags) << ")\n";
outs() << " ('reserved1', " << Reserved1 << ")\n";
outs() << " ('reserved2', " << Reserved2 << ")\n";
if (Reserved3 != ~0ULL)
outs() << " ('reserved3', " << Reserved3 << ")\n";
outs() << " ),\n";
// Dump the relocation entries.
outs() << " ('_relocations', [\n";
unsigned RelNum = 0;
for (relocation_iterator I = Obj.section_rel_begin(Index),
E = Obj.section_rel_end(Index);
I != E; ++I, ++RelNum) {
MachO::any_relocation_info RE = Obj.getRelocation(I->getRawDataRefImpl());
outs() << " # Relocation " << RelNum << "\n";
outs() << " (('word-0', " << format("0x%x", RE.r_word0) << "),\n";
outs() << " ('word-1', " << format("0x%x", RE.r_word1) << ")),\n";
}
outs() << " ])\n";
// Dump the section data, if requested.
if (ShowSectionData) {
outs() << " ('_section_data', '";
StringRef Data = Obj.getData().substr(Offset, Size);
for (unsigned i = 0; i != Data.size(); ++i) {
if (i && (i % 4) == 0)
outs() << ' ';
outs() << hexdigit((Data[i] >> 4) & 0xF, /*LowerCase=*/true);
outs() << hexdigit((Data[i] >> 0) & 0xF, /*LowerCase=*/true);
}
outs() << "')\n";
}
return 0;
}
static int DumpSegmentCommand(const MachOObjectFile &Obj,
const MachOObjectFile::LoadCommandInfo &LCI) {
MachO::segment_command SLC = Obj.getSegmentLoadCommand(LCI);
DumpSegmentCommandData(StringRef(SLC.segname, 16), SLC.vmaddr,
SLC.vmsize, SLC.fileoff, SLC.filesize,
SLC.maxprot, SLC.initprot, SLC.nsects, SLC.flags);
// Dump the sections.
outs() << " ('sections', [\n";
for (unsigned i = 0; i != SLC.nsects; ++i) {
MachO::section Sect = Obj.getSection(LCI, i);
DumpSectionData(Obj, i, StringRef(Sect.sectname, 16),
StringRef(Sect.segname, 16), Sect.addr,
Sect.size, Sect.offset, Sect.align,
Sect.reloff, Sect.nreloc, Sect.flags,
Sect.reserved1, Sect.reserved2);
}
outs() << " ])\n";
return 0;
}
static int DumpSegment64Command(const MachOObjectFile &Obj,
const MachOObjectFile::LoadCommandInfo &LCI) {
MachO::segment_command_64 SLC = Obj.getSegment64LoadCommand(LCI);
DumpSegmentCommandData(StringRef(SLC.segname, 16), SLC.vmaddr,
SLC.vmsize, SLC.fileoff, SLC.filesize,
SLC.maxprot, SLC.initprot, SLC.nsects, SLC.flags);
// Dump the sections.
outs() << " ('sections', [\n";
for (unsigned i = 0; i != SLC.nsects; ++i) {
MachO::section_64 Sect = Obj.getSection64(LCI, i);
DumpSectionData(Obj, i, StringRef(Sect.sectname, 16),
StringRef(Sect.segname, 16), Sect.addr,
Sect.size, Sect.offset, Sect.align,
Sect.reloff, Sect.nreloc, Sect.flags,
Sect.reserved1, Sect.reserved2,
Sect.reserved3);
}
outs() << " ])\n";
return 0;
}
static void DumpSymbolTableEntryData(const MachOObjectFile &Obj,
unsigned Index, uint32_t StringIndex,
uint8_t Type, uint8_t SectionIndex,
uint16_t Flags, uint64_t Value,
StringRef StringTable) {
const char *Name = &StringTable.data()[StringIndex];
outs() << " # Symbol " << Index << "\n";
outs() << " (('n_strx', " << StringIndex << ")\n";
outs() << " ('n_type', " << format("0x%x", Type) << ")\n";
outs() << " ('n_sect', " << uint32_t(SectionIndex) << ")\n";
outs() << " ('n_desc', " << Flags << ")\n";
outs() << " ('n_value', " << Value << ")\n";
outs() << " ('_string', '" << Name << "')\n";
outs() << " ),\n";
}
static int DumpSymtabCommand(const MachOObjectFile &Obj) {
MachO::symtab_command SLC = Obj.getSymtabLoadCommand();
outs() << " ('symoff', " << SLC.symoff << ")\n";
outs() << " ('nsyms', " << SLC.nsyms << ")\n";
outs() << " ('stroff', " << SLC.stroff << ")\n";
outs() << " ('strsize', " << SLC.strsize << ")\n";
// Dump the string data.
outs() << " ('_string_data', '";
StringRef StringTable = Obj.getStringTableData();
outs().write_escaped(StringTable,
/*UseHexEscapes=*/true) << "')\n";
// Dump the symbol table.
outs() << " ('_symbols', [\n";
unsigned SymNum = 0;
for (const SymbolRef &Symbol : Obj.symbols()) {
DataRefImpl DRI = Symbol.getRawDataRefImpl();
if (Obj.is64Bit()) {
MachO::nlist_64 STE = Obj.getSymbol64TableEntry(DRI);
DumpSymbolTableEntryData(Obj, SymNum, STE.n_strx, STE.n_type,
STE.n_sect, STE.n_desc, STE.n_value,
StringTable);
} else {
MachO::nlist STE = Obj.getSymbolTableEntry(DRI);
DumpSymbolTableEntryData(Obj, SymNum, STE.n_strx, STE.n_type,
STE.n_sect, STE.n_desc, STE.n_value,
StringTable);
}
SymNum++;
}
outs() << " ])\n";
return 0;
}
static int DumpDysymtabCommand(const MachOObjectFile &Obj) {
MachO::dysymtab_command DLC = Obj.getDysymtabLoadCommand();
outs() << " ('ilocalsym', " << DLC.ilocalsym << ")\n";
outs() << " ('nlocalsym', " << DLC.nlocalsym << ")\n";
outs() << " ('iextdefsym', " << DLC.iextdefsym << ")\n";
outs() << " ('nextdefsym', " << DLC.nextdefsym << ")\n";
outs() << " ('iundefsym', " << DLC.iundefsym << ")\n";
outs() << " ('nundefsym', " << DLC.nundefsym << ")\n";
outs() << " ('tocoff', " << DLC.tocoff << ")\n";
outs() << " ('ntoc', " << DLC.ntoc << ")\n";
outs() << " ('modtaboff', " << DLC.modtaboff << ")\n";
outs() << " ('nmodtab', " << DLC.nmodtab << ")\n";
outs() << " ('extrefsymoff', " << DLC.extrefsymoff << ")\n";
outs() << " ('nextrefsyms', " << DLC.nextrefsyms << ")\n";
outs() << " ('indirectsymoff', " << DLC.indirectsymoff << ")\n";
outs() << " ('nindirectsyms', " << DLC.nindirectsyms << ")\n";
outs() << " ('extreloff', " << DLC.extreloff << ")\n";
outs() << " ('nextrel', " << DLC.nextrel << ")\n";
outs() << " ('locreloff', " << DLC.locreloff << ")\n";
outs() << " ('nlocrel', " << DLC.nlocrel << ")\n";
// Dump the indirect symbol table.
outs() << " ('_indirect_symbols', [\n";
for (unsigned i = 0; i != DLC.nindirectsyms; ++i) {
uint32_t ISTE = Obj.getIndirectSymbolTableEntry(DLC, i);
outs() << " # Indirect Symbol " << i << "\n";
outs() << " (('symbol_index', " << format("0x%x", ISTE) << "),),\n";
}
outs() << " ])\n";
return 0;
}
static int
DumpLinkeditDataCommand(const MachOObjectFile &Obj,
const MachOObjectFile::LoadCommandInfo &LCI) {
MachO::linkedit_data_command LLC = Obj.getLinkeditDataLoadCommand(LCI);
outs() << " ('dataoff', " << LLC.dataoff << ")\n"
<< " ('datasize', " << LLC.datasize << ")\n"
<< " ('_addresses', [\n";
SmallVector<uint64_t, 8> Addresses;
Obj.ReadULEB128s(LLC.dataoff, Addresses);
for (unsigned i = 0, e = Addresses.size(); i != e; ++i)
outs() << " # Address " << i << '\n'
<< " ('address', " << format("0x%x", Addresses[i]) << "),\n";
outs() << " ])\n";
return 0;
}
static int
DumpDataInCodeDataCommand(const MachOObjectFile &Obj,
const MachOObjectFile::LoadCommandInfo &LCI) {
MachO::linkedit_data_command LLC = Obj.getLinkeditDataLoadCommand(LCI);
outs() << " ('dataoff', " << LLC.dataoff << ")\n"
<< " ('datasize', " << LLC.datasize << ")\n"
<< " ('_data_regions', [\n";
unsigned NumRegions = LLC.datasize / sizeof(MachO::data_in_code_entry);
for (unsigned i = 0; i < NumRegions; ++i) {
MachO::data_in_code_entry DICE= Obj.getDataInCodeTableEntry(LLC.dataoff, i);
outs() << " # DICE " << i << "\n"
<< " ('offset', " << DICE.offset << ")\n"
<< " ('length', " << DICE.length << ")\n"
<< " ('kind', " << DICE.kind << ")\n";
}
outs() <<" ])\n";
return 0;
}
static int
DumpLinkerOptionsCommand(const MachOObjectFile &Obj,
const MachOObjectFile::LoadCommandInfo &LCI) {
MachO::linker_options_command LOLC = Obj.getLinkerOptionsLoadCommand(LCI);
outs() << " ('count', " << LOLC.count << ")\n"
<< " ('_strings', [\n";
uint64_t DataSize = LOLC.cmdsize - sizeof(MachO::linker_options_command);
const char *P = LCI.Ptr + sizeof(MachO::linker_options_command);
StringRef Data(P, DataSize);
for (unsigned i = 0; i != LOLC.count; ++i) {
std::pair<StringRef,StringRef> Split = Data.split('\0');
outs() << "\t\"";
outs().write_escaped(Split.first);
outs() << "\",\n";
Data = Split.second;
}
outs() <<" ])\n";
return 0;
}
+static int
+DumpVersionMin(const MachOObjectFile &Obj,
+ const MachOObjectFile::LoadCommandInfo &LCI) {
+ MachO::version_min_command VMLC = Obj.getVersionMinLoadCommand(LCI);
+ outs() << " ('version, " << VMLC.version << ")\n"
+ << " ('reserved, " << VMLC.reserved << ")\n";
+ return 0;
+}
+
static int DumpLoadCommand(const MachOObjectFile &Obj,
MachOObjectFile::LoadCommandInfo &LCI) {
switch (LCI.C.cmd) {
case MachO::LC_SEGMENT:
return DumpSegmentCommand(Obj, LCI);
case MachO::LC_SEGMENT_64:
return DumpSegment64Command(Obj, LCI);
case MachO::LC_SYMTAB:
return DumpSymtabCommand(Obj);
case MachO::LC_DYSYMTAB:
return DumpDysymtabCommand(Obj);
case MachO::LC_CODE_SIGNATURE:
case MachO::LC_SEGMENT_SPLIT_INFO:
case MachO::LC_FUNCTION_STARTS:
return DumpLinkeditDataCommand(Obj, LCI);
case MachO::LC_DATA_IN_CODE:
return DumpDataInCodeDataCommand(Obj, LCI);
case MachO::LC_LINKER_OPTIONS:
return DumpLinkerOptionsCommand(Obj, LCI);
+ case MachO::LC_VERSION_MIN_IPHONEOS:
+ case MachO::LC_VERSION_MIN_MACOSX:
+ return DumpVersionMin(Obj, LCI);
default:
Warning("unknown load command: " + Twine(LCI.C.cmd));
return 0;
}
}
static int DumpLoadCommand(const MachOObjectFile &Obj, unsigned Index,
MachOObjectFile::LoadCommandInfo &LCI) {
outs() << " # Load Command " << Index << "\n"
<< " (('command', " << LCI.C.cmd << ")\n"
<< " ('size', " << LCI.C.cmdsize << ")\n";
int Res = DumpLoadCommand(Obj, LCI);
outs() << " ),\n";
return Res;
}
static void printHeader(const MachOObjectFile *Obj,
const MachO::mach_header &Header) {
outs() << "('cputype', " << Header.cputype << ")\n";
outs() << "('cpusubtype', " << Header.cpusubtype << ")\n";
outs() << "('filetype', " << Header.filetype << ")\n";
outs() << "('num_load_commands', " << Header.ncmds << ")\n";
outs() << "('load_commands_size', " << Header.sizeofcmds << ")\n";
outs() << "('flag', " << Header.flags << ")\n";
// Print extended header if 64-bit.
if (Obj->is64Bit()) {
const MachO::mach_header_64 *Header64 =
reinterpret_cast<const MachO::mach_header_64 *>(&Header);
outs() << "('reserved', " << Header64->reserved << ")\n";
}
}
int main(int argc, char **argv) {
ProgramName = argv[0];
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
cl::ParseCommandLineOptions(argc, argv, "llvm Mach-O dumping tool\n");
ErrorOr<Binary *> BinaryOrErr = createBinary(InputFile);
if (error_code EC = BinaryOrErr.getError())
return Error("unable to read input: '" + EC.message() + "'");
std::unique_ptr<Binary> Binary(BinaryOrErr.get());
const MachOObjectFile *InputObject = dyn_cast<MachOObjectFile>(Binary.get());
if (!InputObject)
return Error("Not a MachO object");
// Print the header
MachO::mach_header_64 Header64;
MachO::mach_header *Header = reinterpret_cast<MachO::mach_header*>(&Header64);
if (InputObject->is64Bit())
Header64 = InputObject->getHeader64();
else
*Header = InputObject->getHeader();
printHeader(InputObject, *Header);
// Print the load commands.
int Res = 0;
MachOObjectFile::LoadCommandInfo Command =
InputObject->getFirstLoadCommandInfo();
outs() << "('load_commands', [\n";
for (unsigned i = 0; ; ++i) {
if (DumpLoadCommand(*InputObject, i, Command))
break;
if (i == Header->ncmds - 1)
break;
Command = InputObject->getNextLoadCommandInfo(Command);
}
outs() << "])\n";
return Res;
}
Index: llvm/trunk/lib/MC/MCAsmStreamer.cpp
===================================================================
--- llvm/trunk/lib/MC/MCAsmStreamer.cpp (revision 204189)
+++ llvm/trunk/lib/MC/MCAsmStreamer.cpp (revision 204190)
@@ -1,1415 +1,1429 @@
//===- lib/MC/MCAsmStreamer.cpp - Text Assembly Output --------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/MC/MCStreamer.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/Twine.h"
#include "llvm/MC/MCAsmBackend.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCCodeEmitter.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCFixupKindInfo.h"
#include "llvm/MC/MCInst.h"
#include "llvm/MC/MCInstPrinter.h"
#include "llvm/MC/MCObjectFileInfo.h"
#include "llvm/MC/MCRegisterInfo.h"
#include "llvm/MC/MCSectionCOFF.h"
#include "llvm/MC/MCSectionMachO.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/FormattedStream.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/Path.h"
#include <cctype>
using namespace llvm;
namespace {
class MCAsmStreamer : public MCStreamer {
protected:
formatted_raw_ostream &OS;
const MCAsmInfo *MAI;
private:
std::unique_ptr<MCInstPrinter> InstPrinter;
std::unique_ptr<MCCodeEmitter> Emitter;
std::unique_ptr<MCAsmBackend> AsmBackend;
SmallString<128> CommentToEmit;
raw_svector_ostream CommentStream;
unsigned IsVerboseAsm : 1;
unsigned ShowInst : 1;
unsigned UseCFI : 1;
unsigned UseDwarfDirectory : 1;
enum EHSymbolFlags { EHGlobal = 1,
EHWeakDefinition = 1 << 1,
EHPrivateExtern = 1 << 2 };
DenseMap<const MCSymbol*, unsigned> FlagMap;
bool needsSet(const MCExpr *Value);
void EmitRegisterName(int64_t Register);
void EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame) override;
void EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) override;
public:
MCAsmStreamer(MCContext &Context, formatted_raw_ostream &os,
bool isVerboseAsm, bool useCFI, bool useDwarfDirectory,
MCInstPrinter *printer, MCCodeEmitter *emitter,
MCAsmBackend *asmbackend, bool showInst)
: MCStreamer(Context), OS(os), MAI(Context.getAsmInfo()),
InstPrinter(printer), Emitter(emitter), AsmBackend(asmbackend),
CommentStream(CommentToEmit), IsVerboseAsm(isVerboseAsm),
ShowInst(showInst), UseCFI(useCFI),
UseDwarfDirectory(useDwarfDirectory) {
if (InstPrinter && IsVerboseAsm)
InstPrinter->setCommentStream(CommentStream);
}
~MCAsmStreamer() {}
inline void EmitEOL() {
// If we don't have any comments, just emit a \n.
if (!IsVerboseAsm) {
OS << '\n';
return;
}
EmitCommentsAndEOL();
}
void EmitCommentsAndEOL();
/// isVerboseAsm - Return true if this streamer supports verbose assembly at
/// all.
bool isVerboseAsm() const override { return IsVerboseAsm; }
/// hasRawTextSupport - We support EmitRawText.
bool hasRawTextSupport() const override { return true; }
/// AddComment - Add a comment that can be emitted to the generated .s
/// file if applicable as a QoI issue to make the output of the compiler
/// more readable. This only affects the MCAsmStreamer, and only when
/// verbose assembly output is enabled.
void AddComment(const Twine &T) override;
/// AddEncodingComment - Add a comment showing the encoding of an instruction.
void AddEncodingComment(const MCInst &Inst, const MCSubtargetInfo &);
/// GetCommentOS - Return a raw_ostream that comments can be written to.
/// Unlike AddComment, you are required to terminate comments with \n if you
/// use this method.
raw_ostream &GetCommentOS() override {
if (!IsVerboseAsm)
return nulls(); // Discard comments unless in verbose asm mode.
return CommentStream;
}
void emitRawComment(const Twine &T, bool TabPrefix = true) override;
/// AddBlankLine - Emit a blank line to a .s file to pretty it up.
void AddBlankLine() override {
EmitEOL();
}
/// @name MCStreamer Interface
/// @{
void ChangeSection(const MCSection *Section,
const MCExpr *Subsection) override;
void EmitLabel(MCSymbol *Symbol) override;
void EmitDebugLabel(MCSymbol *Symbol) override;
void EmitEHSymAttributes(const MCSymbol *Symbol, MCSymbol *EHSymbol) override;
void EmitAssemblerFlag(MCAssemblerFlag Flag) override;
void EmitLinkerOptions(ArrayRef<std::string> Options) override;
void EmitDataRegion(MCDataRegionType Kind) override;
+ void EmitVersionMin(MCVersionMinType Kind, unsigned Major, unsigned Minor,
+ unsigned Update) override;
void EmitThumbFunc(MCSymbol *Func) override;
void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) override;
void EmitDwarfAdvanceLineAddr(int64_t LineDelta, const MCSymbol *LastLabel,
const MCSymbol *Label,
unsigned PointerSize) override;
void EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
const MCSymbol *Label) override;
bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override;
void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override;
void BeginCOFFSymbolDef(const MCSymbol *Symbol) override;
void EmitCOFFSymbolStorageClass(int StorageClass) override;
void EmitCOFFSymbolType(int Type) override;
void EndCOFFSymbolDef() override;
void EmitCOFFSectionIndex(MCSymbol const *Symbol) override;
void EmitCOFFSecRel32(MCSymbol const *Symbol) override;
void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) override;
void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
unsigned ByteAlignment) override;
/// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
///
/// @param Symbol - The common symbol to emit.
/// @param Size - The size of the common symbol.
/// @param ByteAlignment - The alignment of the common symbol in bytes.
void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
unsigned ByteAlignment) override;
void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
uint64_t Size = 0, unsigned ByteAlignment = 0) override;
void EmitTBSSSymbol (const MCSection *Section, MCSymbol *Symbol,
uint64_t Size, unsigned ByteAlignment = 0) override;
void EmitBytes(StringRef Data) override;
void EmitValueImpl(const MCExpr *Value, unsigned Size) override;
void EmitIntValue(uint64_t Value, unsigned Size) override;
void EmitULEB128Value(const MCExpr *Value) override;
void EmitSLEB128Value(const MCExpr *Value) override;
void EmitGPRel64Value(const MCExpr *Value) override;
void EmitGPRel32Value(const MCExpr *Value) override;
void EmitFill(uint64_t NumBytes, uint8_t FillValue) override;
void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
unsigned ValueSize = 1,
unsigned MaxBytesToEmit = 0) override;
void EmitCodeAlignment(unsigned ByteAlignment,
unsigned MaxBytesToEmit = 0) override;
bool EmitValueToOffset(const MCExpr *Offset,
unsigned char Value = 0) override;
void EmitFileDirective(StringRef Filename) override;
unsigned EmitDwarfFileDirective(unsigned FileNo, StringRef Directory,
StringRef Filename,
unsigned CUID = 0) override;
void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
unsigned Column, unsigned Flags,
unsigned Isa, unsigned Discriminator,
StringRef FileName) override;
void EmitIdent(StringRef IdentString) override;
void EmitCFISections(bool EH, bool Debug) override;
void EmitCFIDefCfa(int64_t Register, int64_t Offset) override;
void EmitCFIDefCfaOffset(int64_t Offset) override;
void EmitCFIDefCfaRegister(int64_t Register) override;
void EmitCFIOffset(int64_t Register, int64_t Offset) override;
void EmitCFIPersonality(const MCSymbol *Sym, unsigned Encoding) override;
void EmitCFILsda(const MCSymbol *Sym, unsigned Encoding) override;
void EmitCFIRememberState() override;
void EmitCFIRestoreState() override;
void EmitCFISameValue(int64_t Register) override;
void EmitCFIRelOffset(int64_t Register, int64_t Offset) override;
void EmitCFIAdjustCfaOffset(int64_t Adjustment) override;
void EmitCFISignalFrame() override;
void EmitCFIUndefined(int64_t Register) override;
void EmitCFIRegister(int64_t Register1, int64_t Register2) override;
void EmitCFIWindowSave() override;
void EmitWin64EHStartProc(const MCSymbol *Symbol) override;
void EmitWin64EHEndProc() override;
void EmitWin64EHStartChained() override;
void EmitWin64EHEndChained() override;
void EmitWin64EHHandler(const MCSymbol *Sym, bool Unwind,
bool Except) override;
void EmitWin64EHHandlerData() override;
void EmitWin64EHPushReg(unsigned Register) override;
void EmitWin64EHSetFrame(unsigned Register, unsigned Offset) override;
void EmitWin64EHAllocStack(unsigned Size) override;
void EmitWin64EHSaveReg(unsigned Register, unsigned Offset) override;
void EmitWin64EHSaveXMM(unsigned Register, unsigned Offset) override;
void EmitWin64EHPushFrame(bool Code) override;
void EmitWin64EHEndProlog() override;
void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override;
void EmitBundleAlignMode(unsigned AlignPow2) override;
void EmitBundleLock(bool AlignToEnd) override;
void EmitBundleUnlock() override;
/// EmitRawText - If this file is backed by an assembly streamer, this dumps
/// the specified string in the output .s file. This capability is
/// indicated by the hasRawTextSupport() predicate.
void EmitRawTextImpl(StringRef String) override;
void FinishImpl() override;
};
} // end anonymous namespace.
/// AddComment - Add a comment that can be emitted to the generated .s
/// file if applicable as a QoI issue to make the output of the compiler
/// more readable. This only affects the MCAsmStreamer, and only when
/// verbose assembly output is enabled.
void MCAsmStreamer::AddComment(const Twine &T) {
if (!IsVerboseAsm) return;
// Make sure that CommentStream is flushed.
CommentStream.flush();
T.toVector(CommentToEmit);
// Each comment goes on its own line.
CommentToEmit.push_back('\n');
// Tell the comment stream that the vector changed underneath it.
CommentStream.resync();
}
void MCAsmStreamer::EmitCommentsAndEOL() {
if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
OS << '\n';
return;
}
CommentStream.flush();
StringRef Comments = CommentToEmit.str();
assert(Comments.back() == '\n' &&
"Comment array not newline terminated");
do {
// Emit a line of comments.
OS.PadToColumn(MAI->getCommentColumn());
size_t Position = Comments.find('\n');
OS << MAI->getCommentString() << ' ' << Comments.substr(0, Position) <<'\n';
Comments = Comments.substr(Position+1);
} while (!Comments.empty());
CommentToEmit.clear();
// Tell the comment stream that the vector changed underneath it.
CommentStream.resync();
}
static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
assert(Bytes && "Invalid size!");
return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
}
void MCAsmStreamer::emitRawComment(const Twine &T, bool TabPrefix) {
if (TabPrefix)
OS << '\t';
OS << MAI->getCommentString() << T;
EmitEOL();
}
void MCAsmStreamer::ChangeSection(const MCSection *Section,
const MCExpr *Subsection) {
assert(Section && "Cannot switch to a null section!");
Section->PrintSwitchToSection(*MAI, OS, Subsection);
}
void MCAsmStreamer::EmitEHSymAttributes(const MCSymbol *Symbol,
MCSymbol *EHSymbol) {
if (UseCFI)
return;
unsigned Flags = FlagMap.lookup(Symbol);
if (Flags & EHGlobal)
EmitSymbolAttribute(EHSymbol, MCSA_Global);
if (Flags & EHWeakDefinition)
EmitSymbolAttribute(EHSymbol, MCSA_WeakDefinition);
if (Flags & EHPrivateExtern)
EmitSymbolAttribute(EHSymbol, MCSA_PrivateExtern);
}
void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
MCStreamer::EmitLabel(Symbol);
OS << *Symbol << MAI->getLabelSuffix();
EmitEOL();
}
void MCAsmStreamer::EmitDebugLabel(MCSymbol *Symbol) {
assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
MCStreamer::EmitDebugLabel(Symbol);
OS << *Symbol << MAI->getDebugLabelSuffix();
EmitEOL();
}
void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
switch (Flag) {
case MCAF_SyntaxUnified: OS << "\t.syntax unified"; break;
case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
case MCAF_Code16: OS << '\t'<< MAI->getCode16Directive();break;
case MCAF_Code32: OS << '\t'<< MAI->getCode32Directive();break;
case MCAF_Code64: OS << '\t'<< MAI->getCode64Directive();break;
}
EmitEOL();
}
void MCAsmStreamer::EmitLinkerOptions(ArrayRef<std::string> Options) {
assert(!Options.empty() && "At least one option is required!");
OS << "\t.linker_option \"" << Options[0] << '"';
for (ArrayRef<std::string>::iterator it = Options.begin() + 1,
ie = Options.end(); it != ie; ++it) {
OS << ", " << '"' << *it << '"';
}
OS << "\n";
}
void MCAsmStreamer::EmitDataRegion(MCDataRegionType Kind) {
if (!MAI->doesSupportDataRegionDirectives())
return;
switch (Kind) {
case MCDR_DataRegion: OS << "\t.data_region"; break;
case MCDR_DataRegionJT8: OS << "\t.data_region jt8"; break;
case MCDR_DataRegionJT16: OS << "\t.data_region jt16"; break;
case MCDR_DataRegionJT32: OS << "\t.data_region jt32"; break;
case MCDR_DataRegionEnd: OS << "\t.end_data_region"; break;
}
EmitEOL();
}
+void MCAsmStreamer::EmitVersionMin(MCVersionMinType Kind, unsigned Major,
+ unsigned Minor, unsigned Update) {
+ switch (Kind) {
+ case MCVM_IOSVersionMin: OS << "\t.ios_version_min"; break;
+ case MCVM_OSXVersionMin: OS << "\t.macosx_version_min"; break;
+ }
+ OS << " " << Major << ", " << Minor;
+ if (Update)
+ OS << ", " << Update;
+ EmitEOL();
+}
+
void MCAsmStreamer::EmitThumbFunc(MCSymbol *Func) {
// This needs to emit to a temporary string to get properly quoted
// MCSymbols when they have spaces in them.
OS << "\t.thumb_func";
// Only Mach-O hasSubsectionsViaSymbols()
if (MAI->hasSubsectionsViaSymbols())
OS << '\t' << *Func;
EmitEOL();
}
void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
OS << *Symbol << " = " << *Value;
EmitEOL();
// FIXME: Lift context changes into super class.
Symbol->setVariableValue(Value);
}
void MCAsmStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
OS << ".weakref " << *Alias << ", " << *Symbol;
EmitEOL();
}
void MCAsmStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta,
const MCSymbol *LastLabel,
const MCSymbol *Label,
unsigned PointerSize) {
EmitDwarfSetLineAddr(LineDelta, Label, PointerSize);
}
void MCAsmStreamer::EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
const MCSymbol *Label) {
EmitIntValue(dwarf::DW_CFA_advance_loc4, 1);
const MCExpr *AddrDelta = BuildSymbolDiff(getContext(), Label, LastLabel);
AddrDelta = ForceExpAbs(AddrDelta);
EmitValue(AddrDelta, 4);
}
bool MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
MCSymbolAttr Attribute) {
switch (Attribute) {
case MCSA_Invalid: llvm_unreachable("Invalid symbol attribute");
case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
case MCSA_ELF_TypeGnuUniqueObject: /// .type _foo, @gnu_unique_object
if (!MAI->hasDotTypeDotSizeDirective())
return false; // Symbol attribute not supported
OS << "\t.type\t" << *Symbol << ','
<< ((MAI->getCommentString()[0] != '@') ? '@' : '%');
switch (Attribute) {
default: return false;
case MCSA_ELF_TypeFunction: OS << "function"; break;
case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
case MCSA_ELF_TypeObject: OS << "object"; break;
case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
case MCSA_ELF_TypeCommon: OS << "common"; break;
case MCSA_ELF_TypeNoType: OS << "no_type"; break;
case MCSA_ELF_TypeGnuUniqueObject: OS << "gnu_unique_object"; break;
}
EmitEOL();
return true;
case MCSA_Global: // .globl/.global
OS << MAI->getGlobalDirective();
FlagMap[Symbol] |= EHGlobal;
break;
case MCSA_Hidden: OS << "\t.hidden\t"; break;
case MCSA_IndirectSymbol: OS << "\t.indirect_symbol\t"; break;
case MCSA_Internal: OS << "\t.internal\t"; break;
case MCSA_LazyReference: OS << "\t.lazy_reference\t"; break;
case MCSA_Local: OS << "\t.local\t"; break;
case MCSA_NoDeadStrip: OS << "\t.no_dead_strip\t"; break;
case MCSA_SymbolResolver: OS << "\t.symbol_resolver\t"; break;
case MCSA_PrivateExtern:
OS << "\t.private_extern\t";
FlagMap[Symbol] |= EHPrivateExtern;
break;
case MCSA_Protected: OS << "\t.protected\t"; break;
case MCSA_Reference: OS << "\t.reference\t"; break;
case MCSA_Weak: OS << "\t.weak\t"; break;
case MCSA_WeakDefinition:
OS << "\t.weak_definition\t";
FlagMap[Symbol] |= EHWeakDefinition;
break;
// .weak_reference
case MCSA_WeakReference: OS << MAI->getWeakRefDirective(); break;
case MCSA_WeakDefAutoPrivate: OS << "\t.weak_def_can_be_hidden\t"; break;
}
OS << *Symbol;
EmitEOL();
return true;
}
void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
EmitEOL();
}
void MCAsmStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
OS << "\t.def\t " << *Symbol << ';';
EmitEOL();
}
void MCAsmStreamer::EmitCOFFSymbolStorageClass (int StorageClass) {
OS << "\t.scl\t" << StorageClass << ';';
EmitEOL();
}
void MCAsmStreamer::EmitCOFFSymbolType (int Type) {
OS << "\t.type\t" << Type << ';';
EmitEOL();
}
void MCAsmStreamer::EndCOFFSymbolDef() {
OS << "\t.endef";
EmitEOL();
}
void MCAsmStreamer::EmitCOFFSectionIndex(MCSymbol const *Symbol) {
OS << "\t.secidx\t" << *Symbol;
EmitEOL();
}
void MCAsmStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol) {
OS << "\t.secrel32\t" << *Symbol;
EmitEOL();
}
void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
assert(MAI->hasDotTypeDotSizeDirective());
OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
}
void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
unsigned ByteAlignment) {
// Common symbols do not belong to any actual section.
AssignSection(Symbol, NULL);
OS << "\t.comm\t" << *Symbol << ',' << Size;
if (ByteAlignment != 0) {
if (MAI->getCOMMDirectiveAlignmentIsInBytes())
OS << ',' << ByteAlignment;
else
OS << ',' << Log2_32(ByteAlignment);
}
EmitEOL();
}
/// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
///
/// @param Symbol - The common symbol to emit.
/// @param Size - The size of the common symbol.
void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
unsigned ByteAlign) {
// Common symbols do not belong to any actual section.
AssignSection(Symbol, NULL);
OS << "\t.lcomm\t" << *Symbol << ',' << Size;
if (ByteAlign > 1) {
switch (MAI->getLCOMMDirectiveAlignmentType()) {
case LCOMM::NoAlignment:
llvm_unreachable("alignment not supported on .lcomm!");
case LCOMM::ByteAlignment:
OS << ',' << ByteAlign;
break;
case LCOMM::Log2Alignment:
assert(isPowerOf2_32(ByteAlign) && "alignment must be a power of 2");
OS << ',' << Log2_32(ByteAlign);
break;
}
}
EmitEOL();
}
void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
uint64_t Size, unsigned ByteAlignment) {
if (Symbol)
AssignSection(Symbol, Section);
// Note: a .zerofill directive does not switch sections.
OS << ".zerofill ";
// This is a mach-o specific directive.
const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
if (Symbol != NULL) {
OS << ',' << *Symbol << ',' << Size;
if (ByteAlignment != 0)
OS << ',' << Log2_32(ByteAlignment);
}
EmitEOL();
}
// .tbss sym, size, align
// This depends that the symbol has already been mangled from the original,
// e.g. _a.
void MCAsmStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
uint64_t Size, unsigned ByteAlignment) {
AssignSection(Symbol, Section);
assert(Symbol != NULL && "Symbol shouldn't be NULL!");
// Instead of using the Section we'll just use the shortcut.
// This is a mach-o specific directive and section.
OS << ".tbss " << *Symbol << ", " << Size;
// Output align if we have it. We default to 1 so don't bother printing
// that.
if (ByteAlignment > 1) OS << ", " << Log2_32(ByteAlignment);
EmitEOL();
}
static inline char toOctal(int X) { return (X&7)+'0'; }
static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
OS << '"';
for (unsigned i = 0, e = Data.size(); i != e; ++i) {
unsigned char C = Data[i];
if (C == '"' || C == '\\') {
OS << '\\' << (char)C;
continue;
}
if (isprint((unsigned char)C)) {
OS << (char)C;
continue;
}
switch (C) {
case '\b': OS << "\\b"; break;
case '\f': OS << "\\f"; break;
case '\n': OS << "\\n"; break;
case '\r': OS << "\\r"; break;
case '\t': OS << "\\t"; break;
default:
OS << '\\';
OS << toOctal(C >> 6);
OS << toOctal(C >> 3);
OS << toOctal(C >> 0);
break;
}
}
OS << '"';
}
void MCAsmStreamer::EmitBytes(StringRef Data) {
assert(getCurrentSection().first &&
"Cannot emit contents before setting section!");
if (Data.empty()) return;
if (Data.size() == 1) {
OS << MAI->getData8bitsDirective();
OS << (unsigned)(unsigned char)Data[0];
EmitEOL();
return;
}
// If the data ends with 0 and the target supports .asciz, use it, otherwise
// use .ascii
if (MAI->getAscizDirective() && Data.back() == 0) {
OS << MAI->getAscizDirective();
Data = Data.substr(0, Data.size()-1);
} else {
OS << MAI->getAsciiDirective();
}
PrintQuotedString(Data, OS);
EmitEOL();
}
void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size) {
EmitValue(MCConstantExpr::Create(Value, getContext()), Size);
}
void MCAsmStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size) {
assert(Size <= 8 && "Invalid size");
assert(getCurrentSection().first &&
"Cannot emit contents before setting section!");
const char *Directive = 0;
switch (Size) {
default: break;
case 1: Directive = MAI->getData8bitsDirective(); break;
case 2: Directive = MAI->getData16bitsDirective(); break;
case 4: Directive = MAI->getData32bitsDirective(); break;
case 8: Directive = MAI->getData64bitsDirective(); break;
}
if (!Directive) {
int64_t IntValue;
if (!Value->EvaluateAsAbsolute(IntValue))
report_fatal_error("Don't know how to emit this value.");
// We couldn't handle the requested integer size so we fallback by breaking
// the request down into several, smaller, integers. Since sizes greater
// than eight are invalid and size equivalent to eight should have been
// handled earlier, we use four bytes as our largest piece of granularity.
bool IsLittleEndian = MAI->isLittleEndian();
for (unsigned Emitted = 0; Emitted != Size;) {
unsigned Remaining = Size - Emitted;
// The size of our partial emission must be a power of two less than
// eight.
unsigned EmissionSize = PowerOf2Floor(Remaining);
if (EmissionSize > 4)
EmissionSize = 4;
// Calculate the byte offset of our partial emission taking into account
// the endianness of the target.
unsigned ByteOffset =
IsLittleEndian ? Emitted : (Remaining - EmissionSize);
uint64_t ValueToEmit = IntValue >> (ByteOffset * 8);
// We truncate our partial emission to fit within the bounds of the
// emission domain. This produces nicer output and silences potential
// truncation warnings when round tripping through another assembler.
ValueToEmit &= ~0ULL >> (64 - EmissionSize * 8);
EmitIntValue(ValueToEmit, EmissionSize);
Emitted += EmissionSize;
}
return;
}
assert(Directive && "Invalid size for machine code value!");
OS << Directive << *Value;
EmitEOL();
}
void MCAsmStreamer::EmitULEB128Value(const MCExpr *Value) {
int64_t IntValue;
if (Value->EvaluateAsAbsolute(IntValue)) {
EmitULEB128IntValue(IntValue);
return;
}
assert(MAI->hasLEB128() && "Cannot print a .uleb");
OS << ".uleb128 " << *Value;
EmitEOL();
}
void MCAsmStreamer::EmitSLEB128Value(const MCExpr *Value) {
int64_t IntValue;
if (Value->EvaluateAsAbsolute(IntValue)) {
EmitSLEB128IntValue(IntValue);
return;
}
assert(MAI->hasLEB128() && "Cannot print a .sleb");
OS << ".sleb128 " << *Value;
EmitEOL();
}
void MCAsmStreamer::EmitGPRel64Value(const MCExpr *Value) {
assert(MAI->getGPRel64Directive() != 0);
OS << MAI->getGPRel64Directive() << *Value;
EmitEOL();
}
void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
assert(MAI->getGPRel32Directive() != 0);
OS << MAI->getGPRel32Directive() << *Value;
EmitEOL();
}
/// EmitFill - Emit NumBytes bytes worth of the value specified by
/// FillValue. This implements directives such as '.space'.
void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue) {
if (NumBytes == 0) return;
if (const char *ZeroDirective = MAI->getZeroDirective()) {
OS << ZeroDirective << NumBytes;
if (FillValue != 0)
OS << ',' << (int)FillValue;
EmitEOL();
return;
}
// Emit a byte at a time.
MCStreamer::EmitFill(NumBytes, FillValue);
}
void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
unsigned ValueSize,
unsigned MaxBytesToEmit) {
// Some assemblers don't support non-power of two alignments, so we always
// emit alignments as a power of two if possible.
if (isPowerOf2_32(ByteAlignment)) {
switch (ValueSize) {
default:
llvm_unreachable("Invalid size for machine code value!");
case 1:
OS << "\t.align\t";
break;
case 2:
OS << ".p2alignw ";
break;
case 4:
OS << ".p2alignl ";
break;
case 8:
llvm_unreachable("Unsupported alignment size!");
}
if (MAI->getAlignmentIsInBytes())
OS << ByteAlignment;
else
OS << Log2_32(ByteAlignment);
if (Value || MaxBytesToEmit) {
OS << ", 0x";
OS.write_hex(truncateToSize(Value, ValueSize));
if (MaxBytesToEmit)
OS << ", " << MaxBytesToEmit;
}
EmitEOL();
return;
}
// Non-power of two alignment. This is not widely supported by assemblers.
// FIXME: Parameterize this based on MAI.
switch (ValueSize) {
default: llvm_unreachable("Invalid size for machine code value!");
case 1: OS << ".balign"; break;
case 2: OS << ".balignw"; break;
case 4: OS << ".balignl"; break;
case 8: llvm_unreachable("Unsupported alignment size!");
}
OS << ' ' << ByteAlignment;
OS << ", " << truncateToSize(Value, ValueSize);
if (MaxBytesToEmit)
OS << ", " << MaxBytesToEmit;
EmitEOL();
}
void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
unsigned MaxBytesToEmit) {
// Emit with a text fill value.
EmitValueToAlignment(ByteAlignment, MAI->getTextAlignFillValue(),
1, MaxBytesToEmit);
}
bool MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
unsigned char Value) {
// FIXME: Verify that Offset is associated with the current section.
OS << ".org " << *Offset << ", " << (unsigned) Value;
EmitEOL();
return false;
}
void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
assert(MAI->hasSingleParameterDotFile());
OS << "\t.file\t";
PrintQuotedString(Filename, OS);
EmitEOL();
}
unsigned MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo,
StringRef Directory,
StringRef Filename,
unsigned CUID) {
assert(CUID == 0);
MCDwarfLineTable &Table = getContext().getMCDwarfLineTable(CUID);
unsigned NumFiles = Table.getMCDwarfFiles().size();
FileNo = Table.getFile(Directory, Filename, FileNo);
if (FileNo == 0)
return 0;
if (NumFiles == Table.getMCDwarfFiles().size())
return FileNo;
SmallString<128> FullPathName;
if (!UseDwarfDirectory && !Directory.empty()) {
if (sys::path::is_absolute(Filename))
Directory = "";
else {
FullPathName = Directory;
sys::path::append(FullPathName, Filename);
Directory = "";
Filename = FullPathName;
}
}
OS << "\t.file\t" << FileNo << ' ';
if (!Directory.empty()) {
PrintQuotedString(Directory, OS);
OS << ' ';
}
PrintQuotedString(Filename, OS);
EmitEOL();
return FileNo;
}
void MCAsmStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
unsigned Column, unsigned Flags,
unsigned Isa,
unsigned Discriminator,
StringRef FileName) {
this->MCStreamer::EmitDwarfLocDirective(FileNo, Line, Column, Flags,
Isa, Discriminator, FileName);
OS << "\t.loc\t" << FileNo << " " << Line << " " << Column;
if (Flags & DWARF2_FLAG_BASIC_BLOCK)
OS << " basic_block";
if (Flags & DWARF2_FLAG_PROLOGUE_END)
OS << " prologue_end";
if (Flags & DWARF2_FLAG_EPILOGUE_BEGIN)
OS << " epilogue_begin";
unsigned OldFlags = getContext().getCurrentDwarfLoc().getFlags();
if ((Flags & DWARF2_FLAG_IS_STMT) != (OldFlags & DWARF2_FLAG_IS_STMT)) {
OS << " is_stmt ";
if (Flags & DWARF2_FLAG_IS_STMT)
OS << "1";
else
OS << "0";
}
if (Isa)
OS << " isa " << Isa;
if (Discriminator)
OS << " discriminator " << Discriminator;
if (IsVerboseAsm) {
OS.PadToColumn(MAI->getCommentColumn());
OS << MAI->getCommentString() << ' ' << FileName << ':'
<< Line << ':' << Column;
}
EmitEOL();
}
void MCAsmStreamer::EmitIdent(StringRef IdentString) {
assert(MAI->hasIdentDirective() && ".ident directive not supported");
OS << "\t.ident\t";
PrintQuotedString(IdentString, OS);
EmitEOL();
}
void MCAsmStreamer::EmitCFISections(bool EH, bool Debug) {
MCStreamer::EmitCFISections(EH, Debug);
if (!UseCFI)
return;
OS << "\t.cfi_sections ";
if (EH) {
OS << ".eh_frame";
if (Debug)
OS << ", .debug_frame";
} else if (Debug) {
OS << ".debug_frame";
}
EmitEOL();
}
void MCAsmStreamer::EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame) {
if (!UseCFI) {
RecordProcStart(Frame);
return;
}
OS << "\t.cfi_startproc";
if (Frame.IsSimple)
OS << " simple";
EmitEOL();
}
void MCAsmStreamer::EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) {
if (!UseCFI) {
RecordProcEnd(Frame);
return;
}
// Put a dummy non-null value in Frame.End to mark that this frame has been
// closed.
Frame.End = (MCSymbol *) 1;
OS << "\t.cfi_endproc";
EmitEOL();
}
void MCAsmStreamer::EmitRegisterName(int64_t Register) {
if (InstPrinter && !MAI->useDwarfRegNumForCFI()) {
const MCRegisterInfo *MRI = getContext().getRegisterInfo();
unsigned LLVMRegister = MRI->getLLVMRegNum(Register, true);
InstPrinter->printRegName(OS, LLVMRegister);
} else {
OS << Register;
}
}
void MCAsmStreamer::EmitCFIDefCfa(int64_t Register, int64_t Offset) {
MCStreamer::EmitCFIDefCfa(Register, Offset);
if (!UseCFI)
return;
OS << "\t.cfi_def_cfa ";
EmitRegisterName(Register);
OS << ", " << Offset;
EmitEOL();
}
void MCAsmStreamer::EmitCFIDefCfaOffset(int64_t Offset) {
MCStreamer::EmitCFIDefCfaOffset(Offset);
if (!UseCFI)
return;
OS << "\t.cfi_def_cfa_offset " << Offset;
EmitEOL();
}
void MCAsmStreamer::EmitCFIDefCfaRegister(int64_t Register) {
MCStreamer::EmitCFIDefCfaRegister(Register);
if (!UseCFI)
return;
OS << "\t.cfi_def_cfa_register ";
EmitRegisterName(Register);
EmitEOL();
}
void MCAsmStreamer::EmitCFIOffset(int64_t Register, int64_t Offset) {
this->MCStreamer::EmitCFIOffset(Register, Offset);
if (!UseCFI)
return;
OS << "\t.cfi_offset ";
EmitRegisterName(Register);
OS << ", " << Offset;
EmitEOL();
}
void MCAsmStreamer::EmitCFIPersonality(const MCSymbol *Sym,
unsigned Encoding) {
MCStreamer::EmitCFIPersonality(Sym, Encoding);
if (!UseCFI)
return;
OS << "\t.cfi_personality " << Encoding << ", " << *Sym;
EmitEOL();
}
void MCAsmStreamer::EmitCFILsda(const MCSymbol *Sym, unsigned Encoding) {
MCStreamer::EmitCFILsda(Sym, Encoding);
if (!UseCFI)
return;
OS << "\t.cfi_lsda " << Encoding << ", " << *Sym;
EmitEOL();
}
void MCAsmStreamer::EmitCFIRememberState() {
MCStreamer::EmitCFIRememberState();
if (!UseCFI)
return;
OS << "\t.cfi_remember_state";
EmitEOL();
}
void MCAsmStreamer::EmitCFIRestoreState() {
MCStreamer::EmitCFIRestoreState();
if (!UseCFI)
return;
OS << "\t.cfi_restore_state";
EmitEOL();
}
void MCAsmStreamer::EmitCFISameValue(int64_t Register) {
MCStreamer::EmitCFISameValue(Register);
if (!UseCFI)
return;
OS << "\t.cfi_same_value ";
EmitRegisterName(Register);
EmitEOL();
}
void MCAsmStreamer::EmitCFIRelOffset(int64_t Register, int64_t Offset) {
MCStreamer::EmitCFIRelOffset(Register, Offset);
if (!UseCFI)
return;
OS << "\t.cfi_rel_offset ";
EmitRegisterName(Register);
OS << ", " << Offset;
EmitEOL();
}
void MCAsmStreamer::EmitCFIAdjustCfaOffset(int64_t Adjustment) {
MCStreamer::EmitCFIAdjustCfaOffset(Adjustment);
if (!UseCFI)
return;
OS << "\t.cfi_adjust_cfa_offset " << Adjustment;
EmitEOL();
}
void MCAsmStreamer::EmitCFISignalFrame() {
MCStreamer::EmitCFISignalFrame();
if (!UseCFI)
return;
OS << "\t.cfi_signal_frame";
EmitEOL();
}
void MCAsmStreamer::EmitCFIUndefined(int64_t Register) {
MCStreamer::EmitCFIUndefined(Register);
if (!UseCFI)
return;
OS << "\t.cfi_undefined " << Register;
EmitEOL();
}
void MCAsmStreamer::EmitCFIRegister(int64_t Register1, int64_t Register2) {
MCStreamer::EmitCFIRegister(Register1, Register2);
if (!UseCFI)
return;
OS << "\t.cfi_register " << Register1 << ", " << Register2;
EmitEOL();
}
void MCAsmStreamer::EmitCFIWindowSave() {
MCStreamer::EmitCFIWindowSave();
if (!UseCFI)
return;
OS << "\t.cfi_window_save";
EmitEOL();
}
void MCAsmStreamer::EmitWin64EHStartProc(const MCSymbol *Symbol) {
MCStreamer::EmitWin64EHStartProc(Symbol);
OS << ".seh_proc " << *Symbol;
EmitEOL();
}
void MCAsmStreamer::EmitWin64EHEndProc() {
MCStreamer::EmitWin64EHEndProc();
OS << "\t.seh_endproc";
EmitEOL();
}
void MCAsmStreamer::EmitWin64EHStartChained() {
MCStreamer::EmitWin64EHStartChained();
OS << "\t.seh_startchained";
EmitEOL();
}
void MCAsmStreamer::EmitWin64EHEndChained() {
MCStreamer::EmitWin64EHEndChained();
OS << "\t.seh_endchained";
EmitEOL();
}
void MCAsmStreamer::EmitWin64EHHandler(const MCSymbol *Sym, bool Unwind,
bool Except) {
MCStreamer::EmitWin64EHHandler(Sym, Unwind, Except);
OS << "\t.seh_handler " << *Sym;
if (Unwind)
OS << ", @unwind";
if (Except)
OS << ", @except";
EmitEOL();
}
static const MCSection *getWin64EHTableSection(StringRef suffix,
MCContext &context) {
// FIXME: This doesn't belong in MCObjectFileInfo. However,
/// this duplicate code in MCWin64EH.cpp.
if (suffix == "")
return context.getObjectFileInfo()->getXDataSection();
return context.getCOFFSection((".xdata"+suffix).str(),
COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
COFF::IMAGE_SCN_MEM_READ |
COFF::IMAGE_SCN_MEM_WRITE,
SectionKind::getDataRel());
}
void MCAsmStreamer::EmitWin64EHHandlerData() {
MCStreamer::EmitWin64EHHandlerData();
// Switch sections. Don't call SwitchSection directly, because that will
// cause the section switch to be visible in the emitted assembly.
// We only do this so the section switch that terminates the handler
// data block is visible.
MCWin64EHUnwindInfo *CurFrame = getCurrentW64UnwindInfo();
StringRef suffix=MCWin64EHUnwindEmitter::GetSectionSuffix(CurFrame->Function);
const MCSection *xdataSect = getWin64EHTableSection(suffix, getContext());
if (xdataSect)
SwitchSectionNoChange(xdataSect);
OS << "\t.seh_handlerdata";
EmitEOL();
}
void MCAsmStreamer::EmitWin64EHPushReg(unsigned Register) {
MCStreamer::EmitWin64EHPushReg(Register);
OS << "\t.seh_pushreg " << Register;
EmitEOL();
}
void MCAsmStreamer::EmitWin64EHSetFrame(unsigned Register, unsigned Offset) {
MCStreamer::EmitWin64EHSetFrame(Register, Offset);
OS << "\t.seh_setframe " << Register << ", " << Offset;
EmitEOL();
}
void MCAsmStreamer::EmitWin64EHAllocStack(unsigned Size) {
MCStreamer::EmitWin64EHAllocStack(Size);
OS << "\t.seh_stackalloc " << Size;
EmitEOL();
}
void MCAsmStreamer::EmitWin64EHSaveReg(unsigned Register, unsigned Offset) {
MCStreamer::EmitWin64EHSaveReg(Register, Offset);
OS << "\t.seh_savereg " << Register << ", " << Offset;
EmitEOL();
}
void MCAsmStreamer::EmitWin64EHSaveXMM(unsigned Register, unsigned Offset) {
MCStreamer::EmitWin64EHSaveXMM(Register, Offset);
OS << "\t.seh_savexmm " << Register << ", " << Offset;
EmitEOL();
}
void MCAsmStreamer::EmitWin64EHPushFrame(bool Code) {
MCStreamer::EmitWin64EHPushFrame(Code);
OS << "\t.seh_pushframe";
if (Code)
OS << " @code";
EmitEOL();
}
void MCAsmStreamer::EmitWin64EHEndProlog(void) {
MCStreamer::EmitWin64EHEndProlog();
OS << "\t.seh_endprologue";
EmitEOL();
}
void MCAsmStreamer::AddEncodingComment(const MCInst &Inst,
const MCSubtargetInfo &STI) {
raw_ostream &OS = GetCommentOS();
SmallString<256> Code;
SmallVector<MCFixup, 4> Fixups;
raw_svector_ostream VecOS(Code);
Emitter->EncodeInstruction(Inst, VecOS, Fixups, STI);
VecOS.flush();
// If we are showing fixups, create symbolic markers in the encoded
// representation. We do this by making a per-bit map to the fixup item index,
// then trying to display it as nicely as possible.
SmallVector<uint8_t, 64> FixupMap;
FixupMap.resize(Code.size() * 8);
for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
FixupMap[i] = 0;
for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
MCFixup &F = Fixups[i];
const MCFixupKindInfo &Info = AsmBackend->getFixupKindInfo(F.getKind());
for (unsigned j = 0; j != Info.TargetSize; ++j) {
unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
FixupMap[Index] = 1 + i;
}
}
// FIXME: Note the fixup comments for Thumb2 are completely bogus since the
// high order halfword of a 32-bit Thumb2 instruction is emitted first.
OS << "encoding: [";
for (unsigned i = 0, e = Code.size(); i != e; ++i) {
if (i)
OS << ',';
// See if all bits are the same map entry.
uint8_t MapEntry = FixupMap[i * 8 + 0];
for (unsigned j = 1; j != 8; ++j) {
if (FixupMap[i * 8 + j] == MapEntry)
continue;
MapEntry = uint8_t(~0U);
break;
}
if (MapEntry != uint8_t(~0U)) {
if (MapEntry == 0) {
OS << format("0x%02x", uint8_t(Code[i]));
} else {
if (Code[i]) {
// FIXME: Some of the 8 bits require fix up.
OS << format("0x%02x", uint8_t(Code[i])) << '\''
<< char('A' + MapEntry - 1) << '\'';
} else
OS << char('A' + MapEntry - 1);
}
} else {
// Otherwise, write out in binary.
OS << "0b";
for (unsigned j = 8; j--;) {
unsigned Bit = (Code[i] >> j) & 1;
unsigned FixupBit;
if (MAI->isLittleEndian())
FixupBit = i * 8 + j;
else
FixupBit = i * 8 + (7-j);
if (uint8_t MapEntry = FixupMap[FixupBit]) {
assert(Bit == 0 && "Encoder wrote into fixed up bit!");
OS << char('A' + MapEntry - 1);
} else
OS << Bit;
}
}
}
OS << "]\n";
for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
MCFixup &F = Fixups[i];
const MCFixupKindInfo &Info = AsmBackend->getFixupKindInfo(F.getKind());
OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
<< ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
}
}
void MCAsmStreamer::EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) {
assert(getCurrentSection().first &&
"Cannot emit contents before setting section!");
// Show the encoding in a comment if we have a code emitter.
if (Emitter)
AddEncodingComment(Inst, STI);
// Show the MCInst if enabled.
if (ShowInst) {
Inst.dump_pretty(GetCommentOS(), MAI, InstPrinter.get(), "\n ");
GetCommentOS() << "\n";
}
// If we have an AsmPrinter, use that to print, otherwise print the MCInst.
if (InstPrinter)
InstPrinter->printInst(&Inst, OS, "");
else
Inst.print(OS, MAI);
EmitEOL();
}
void MCAsmStreamer::EmitBundleAlignMode(unsigned AlignPow2) {
OS << "\t.bundle_align_mode " << AlignPow2;
EmitEOL();
}
void MCAsmStreamer::EmitBundleLock(bool AlignToEnd) {
OS << "\t.bundle_lock";
if (AlignToEnd)
OS << " align_to_end";
EmitEOL();
}
void MCAsmStreamer::EmitBundleUnlock() {
OS << "\t.bundle_unlock";
EmitEOL();
}
/// EmitRawText - If this file is backed by an assembly streamer, this dumps
/// the specified string in the output .s file. This capability is
/// indicated by the hasRawTextSupport() predicate.
void MCAsmStreamer::EmitRawTextImpl(StringRef String) {
if (!String.empty() && String.back() == '\n')
String = String.substr(0, String.size()-1);
OS << String;
EmitEOL();
}
void MCAsmStreamer::FinishImpl() {
// If we are generating dwarf for assembly source files dump out the sections.
if (getContext().getGenDwarfForAssembly())
MCGenDwarfInfo::Emit(this, NULL);
if (!UseCFI)
EmitFrames(AsmBackend.get(), false);
}
MCStreamer *llvm::createAsmStreamer(MCContext &Context,
formatted_raw_ostream &OS,
bool isVerboseAsm, bool useCFI,
bool useDwarfDirectory, MCInstPrinter *IP,
MCCodeEmitter *CE, MCAsmBackend *MAB,
bool ShowInst) {
return new MCAsmStreamer(Context, OS, isVerboseAsm, useCFI, useDwarfDirectory,
IP, CE, MAB, ShowInst);
}
Index: llvm/trunk/lib/MC/MCParser/DarwinAsmParser.cpp
===================================================================
--- llvm/trunk/lib/MC/MCParser/DarwinAsmParser.cpp (revision 204189)
+++ llvm/trunk/lib/MC/MCParser/DarwinAsmParser.cpp (revision 204190)
@@ -1,868 +1,916 @@
//===- DarwinAsmParser.cpp - Darwin (Mach-O) Assembly Parser --------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/MC/MCParser/MCAsmParserExtension.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/ADT/Twine.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCParser/MCAsmLexer.h"
#include "llvm/MC/MCParser/MCAsmParser.h"
#include "llvm/MC/MCSectionMachO.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/SourceMgr.h"
using namespace llvm;
namespace {
/// \brief Implementation of directive handling which is shared across all
/// Darwin targets.
class DarwinAsmParser : public MCAsmParserExtension {
template<bool (DarwinAsmParser::*HandlerMethod)(StringRef, SMLoc)>
void addDirectiveHandler(StringRef Directive) {
MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
this, HandleDirective<DarwinAsmParser, HandlerMethod>);
getParser().addDirectiveHandler(Directive, Handler);
}
bool ParseSectionSwitch(const char *Segment, const char *Section,
unsigned TAA = 0, unsigned ImplicitAlign = 0,
unsigned StubSize = 0);
public:
DarwinAsmParser() {}
void Initialize(MCAsmParser &Parser) override {
// Call the base implementation.
this->MCAsmParserExtension::Initialize(Parser);
addDirectiveHandler<&DarwinAsmParser::ParseDirectiveDesc>(".desc");
addDirectiveHandler<&DarwinAsmParser::ParseDirectiveIndirectSymbol>(
".indirect_symbol");
addDirectiveHandler<&DarwinAsmParser::ParseDirectiveLsym>(".lsym");
addDirectiveHandler<&DarwinAsmParser::ParseDirectiveSubsectionsViaSymbols>(
".subsections_via_symbols");
addDirectiveHandler<&DarwinAsmParser::ParseDirectiveDumpOrLoad>(".dump");
addDirectiveHandler<&DarwinAsmParser::ParseDirectiveDumpOrLoad>(".load");
addDirectiveHandler<&DarwinAsmParser::ParseDirectiveSection>(".section");
addDirectiveHandler<&DarwinAsmParser::ParseDirectivePushSection>(
".pushsection");
addDirectiveHandler<&DarwinAsmParser::ParseDirectivePopSection>(
".popsection");
addDirectiveHandler<&DarwinAsmParser::ParseDirectivePrevious>(".previous");
addDirectiveHandler<&DarwinAsmParser::ParseDirectiveSecureLogUnique>(
".secure_log_unique");
addDirectiveHandler<&DarwinAsmParser::ParseDirectiveSecureLogReset>(
".secure_log_reset");
addDirectiveHandler<&DarwinAsmParser::ParseDirectiveTBSS>(".tbss");
addDirectiveHandler<&DarwinAsmParser::ParseDirectiveZerofill>(".zerofill");
addDirectiveHandler<&DarwinAsmParser::ParseDirectiveDataRegion>(
".data_region");
addDirectiveHandler<&DarwinAsmParser::ParseDirectiveDataRegionEnd>(
".end_data_region");
// Special section directives.
addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveBss>(".bss");
addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveConst>(".const");
addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveConstData>(
".const_data");
addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveConstructor>(
".constructor");
addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveCString>(
".cstring");
addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveData>(".data");
addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveDestructor>(
".destructor");
addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveDyld>(".dyld");
addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveFVMLibInit0>(
".fvmlib_init0");
addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveFVMLibInit1>(
".fvmlib_init1");
addDirectiveHandler<
&DarwinAsmParser::ParseSectionDirectiveLazySymbolPointers>(
".lazy_symbol_pointer");
addDirectiveHandler<&DarwinAsmParser::ParseDirectiveLinkerOption>(
".linker_option");
addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveLiteral16>(
".literal16");
addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveLiteral4>(
".literal4");
addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveLiteral8>(
".literal8");
addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveModInitFunc>(
".mod_init_func");
addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveModTermFunc>(
".mod_term_func");
addDirectiveHandler<
&DarwinAsmParser::ParseSectionDirectiveNonLazySymbolPointers>(
".non_lazy_symbol_pointer");
addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCCatClsMeth>(
".objc_cat_cls_meth");
addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCCatInstMeth>(
".objc_cat_inst_meth");
addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCCategory>(
".objc_category");
addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCClass>(
".objc_class");
addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCClassNames>(
".objc_class_names");
addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCClassVars>(
".objc_class_vars");
addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCClsMeth>(
".objc_cls_meth");
addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCClsRefs>(
".objc_cls_refs");
addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCInstMeth>(
".objc_inst_meth");
addDirectiveHandler<
&DarwinAsmParser::ParseSectionDirectiveObjCInstanceVars>(
".objc_instance_vars");
addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCMessageRefs>(
".objc_message_refs");
addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCMetaClass>(
".objc_meta_class");
addDirectiveHandler<
&DarwinAsmParser::ParseSectionDirectiveObjCMethVarNames>(
".objc_meth_var_names");
addDirectiveHandler<
&DarwinAsmParser::ParseSectionDirectiveObjCMethVarTypes>(
".objc_meth_var_types");
addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCModuleInfo>(
".objc_module_info");
addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCProtocol>(
".objc_protocol");
addDirectiveHandler<
&DarwinAsmParser::ParseSectionDirectiveObjCSelectorStrs>(
".objc_selector_strs");
addDirectiveHandler<
&DarwinAsmParser::ParseSectionDirectiveObjCStringObject>(
".objc_string_object");
addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCSymbols>(
".objc_symbols");
addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectivePICSymbolStub>(
".picsymbol_stub");
addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveStaticConst>(
".static_const");
addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveStaticData>(
".static_data");
addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveSymbolStub>(
".symbol_stub");
addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveTData>(".tdata");
addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveText>(".text");
addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveThreadInitFunc>(
".thread_init_func");
addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveTLV>(".tlv");
addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveIdent>(".ident");
+ addDirectiveHandler<&DarwinAsmParser::ParseVersionMin>(".ios_version_min");
+ addDirectiveHandler<&DarwinAsmParser::ParseVersionMin>(
+ ".macosx_version_min");
}
bool ParseDirectiveDesc(StringRef, SMLoc);
bool ParseDirectiveIndirectSymbol(StringRef, SMLoc);
bool ParseDirectiveDumpOrLoad(StringRef, SMLoc);
bool ParseDirectiveLsym(StringRef, SMLoc);
bool ParseDirectiveLinkerOption(StringRef, SMLoc);
bool ParseDirectiveSection(StringRef, SMLoc);
bool ParseDirectivePushSection(StringRef, SMLoc);
bool ParseDirectivePopSection(StringRef, SMLoc);
bool ParseDirectivePrevious(StringRef, SMLoc);
bool ParseDirectiveSecureLogReset(StringRef, SMLoc);
bool ParseDirectiveSecureLogUnique(StringRef, SMLoc);
bool ParseDirectiveSubsectionsViaSymbols(StringRef, SMLoc);
bool ParseDirectiveTBSS(StringRef, SMLoc);
bool ParseDirectiveZerofill(StringRef, SMLoc);
bool ParseDirectiveDataRegion(StringRef, SMLoc);
bool ParseDirectiveDataRegionEnd(StringRef, SMLoc);
// Named Section Directive
bool ParseSectionDirectiveBss(StringRef, SMLoc) {
return ParseSectionSwitch("__DATA", "__bss");
}
bool ParseSectionDirectiveConst(StringRef, SMLoc) {
return ParseSectionSwitch("__TEXT", "__const");
}
bool ParseSectionDirectiveStaticConst(StringRef, SMLoc) {
return ParseSectionSwitch("__TEXT", "__static_const");
}
bool ParseSectionDirectiveCString(StringRef, SMLoc) {
return ParseSectionSwitch("__TEXT","__cstring",
MachO::S_CSTRING_LITERALS);
}
bool ParseSectionDirectiveLiteral4(StringRef, SMLoc) {
return ParseSectionSwitch("__TEXT", "__literal4",
MachO::S_4BYTE_LITERALS, 4);
}
bool ParseSectionDirectiveLiteral8(StringRef, SMLoc) {
return ParseSectionSwitch("__TEXT", "__literal8",
MachO::S_8BYTE_LITERALS, 8);
}
bool ParseSectionDirectiveLiteral16(StringRef, SMLoc) {
return ParseSectionSwitch("__TEXT","__literal16",
MachO::S_16BYTE_LITERALS, 16);
}
bool ParseSectionDirectiveConstructor(StringRef, SMLoc) {
return ParseSectionSwitch("__TEXT","__constructor");
}
bool ParseSectionDirectiveDestructor(StringRef, SMLoc) {
return ParseSectionSwitch("__TEXT","__destructor");
}
bool ParseSectionDirectiveFVMLibInit0(StringRef, SMLoc) {
return ParseSectionSwitch("__TEXT","__fvmlib_init0");
}
bool ParseSectionDirectiveFVMLibInit1(StringRef, SMLoc) {
return ParseSectionSwitch("__TEXT","__fvmlib_init1");
}
bool ParseSectionDirectiveSymbolStub(StringRef, SMLoc) {
return ParseSectionSwitch("__TEXT","__symbol_stub",
MachO::S_SYMBOL_STUBS |
MachO::S_ATTR_PURE_INSTRUCTIONS,
// FIXME: Different on PPC and ARM.
0, 16);
}
bool ParseSectionDirectivePICSymbolStub(StringRef, SMLoc) {
return ParseSectionSwitch("__TEXT","__picsymbol_stub",
MachO::S_SYMBOL_STUBS |
MachO::S_ATTR_PURE_INSTRUCTIONS, 0, 26);
}
bool ParseSectionDirectiveData(StringRef, SMLoc) {
return ParseSectionSwitch("__DATA", "__data");
}
bool ParseSectionDirectiveStaticData(StringRef, SMLoc) {
return ParseSectionSwitch("__DATA", "__static_data");
}
bool ParseSectionDirectiveNonLazySymbolPointers(StringRef, SMLoc) {
return ParseSectionSwitch("__DATA", "__nl_symbol_ptr",
MachO::S_NON_LAZY_SYMBOL_POINTERS, 4);
}
bool ParseSectionDirectiveLazySymbolPointers(StringRef, SMLoc) {
return ParseSectionSwitch("__DATA", "__la_symbol_ptr",
MachO::S_LAZY_SYMBOL_POINTERS, 4);
}
bool ParseSectionDirectiveDyld(StringRef, SMLoc) {
return ParseSectionSwitch("__DATA", "__dyld");
}
bool ParseSectionDirectiveModInitFunc(StringRef, SMLoc) {
return ParseSectionSwitch("__DATA", "__mod_init_func",
MachO::S_MOD_INIT_FUNC_POINTERS, 4);
}
bool ParseSectionDirectiveModTermFunc(StringRef, SMLoc) {
return ParseSectionSwitch("__DATA", "__mod_term_func",
MachO::S_MOD_TERM_FUNC_POINTERS, 4);
}
bool ParseSectionDirectiveConstData(StringRef, SMLoc) {
return ParseSectionSwitch("__DATA", "__const");
}
bool ParseSectionDirectiveObjCClass(StringRef, SMLoc) {
return ParseSectionSwitch("__OBJC", "__class",
MachO::S_ATTR_NO_DEAD_STRIP);
}
bool ParseSectionDirectiveObjCMetaClass(StringRef, SMLoc) {
return ParseSectionSwitch("__OBJC", "__meta_class",
MachO::S_ATTR_NO_DEAD_STRIP);
}
bool ParseSectionDirectiveObjCCatClsMeth(StringRef, SMLoc) {
return ParseSectionSwitch("__OBJC", "__cat_cls_meth",
MachO::S_ATTR_NO_DEAD_STRIP);
}
bool ParseSectionDirectiveObjCCatInstMeth(StringRef, SMLoc) {
return ParseSectionSwitch("__OBJC", "__cat_inst_meth",
MachO::S_ATTR_NO_DEAD_STRIP);
}
bool ParseSectionDirectiveObjCProtocol(StringRef, SMLoc) {
return ParseSectionSwitch("__OBJC", "__protocol",
MachO::S_ATTR_NO_DEAD_STRIP);
}
bool ParseSectionDirectiveObjCStringObject(StringRef, SMLoc) {
return ParseSectionSwitch("__OBJC", "__string_object",
MachO::S_ATTR_NO_DEAD_STRIP);
}
bool ParseSectionDirectiveObjCClsMeth(StringRef, SMLoc) {
return ParseSectionSwitch("__OBJC", "__cls_meth",
MachO::S_ATTR_NO_DEAD_STRIP);
}
bool ParseSectionDirectiveObjCInstMeth(StringRef, SMLoc) {
return ParseSectionSwitch("__OBJC", "__inst_meth",
MachO::S_ATTR_NO_DEAD_STRIP);
}
bool ParseSectionDirectiveObjCClsRefs(StringRef, SMLoc) {
return ParseSectionSwitch("__OBJC", "__cls_refs",
MachO::S_ATTR_NO_DEAD_STRIP |
MachO::S_LITERAL_POINTERS, 4);
}
bool ParseSectionDirectiveObjCMessageRefs(StringRef, SMLoc) {
return ParseSectionSwitch("__OBJC", "__message_refs",
MachO::S_ATTR_NO_DEAD_STRIP |
MachO::S_LITERAL_POINTERS, 4);
}
bool ParseSectionDirectiveObjCSymbols(StringRef, SMLoc) {
return ParseSectionSwitch("__OBJC", "__symbols",
MachO::S_ATTR_NO_DEAD_STRIP);
}
bool ParseSectionDirectiveObjCCategory(StringRef, SMLoc) {
return ParseSectionSwitch("__OBJC", "__category",
MachO::S_ATTR_NO_DEAD_STRIP);
}
bool ParseSectionDirectiveObjCClassVars(StringRef, SMLoc) {
return ParseSectionSwitch("__OBJC", "__class_vars",
MachO::S_ATTR_NO_DEAD_STRIP);
}
bool ParseSectionDirectiveObjCInstanceVars(StringRef, SMLoc) {
return ParseSectionSwitch("__OBJC", "__instance_vars",
MachO::S_ATTR_NO_DEAD_STRIP);
}
bool ParseSectionDirectiveObjCModuleInfo(StringRef, SMLoc) {
return ParseSectionSwitch("__OBJC", "__module_info",
MachO::S_ATTR_NO_DEAD_STRIP);
}
bool ParseSectionDirectiveObjCClassNames(StringRef, SMLoc) {
return ParseSectionSwitch("__TEXT", "__cstring",
MachO::S_CSTRING_LITERALS);
}
bool ParseSectionDirectiveObjCMethVarTypes(StringRef, SMLoc) {
return ParseSectionSwitch("__TEXT", "__cstring",
MachO::S_CSTRING_LITERALS);
}
bool ParseSectionDirectiveObjCMethVarNames(StringRef, SMLoc) {
return ParseSectionSwitch("__TEXT", "__cstring",
MachO::S_CSTRING_LITERALS);
}
bool ParseSectionDirectiveObjCSelectorStrs(StringRef, SMLoc) {
return ParseSectionSwitch("__OBJC", "__selector_strs",
MachO::S_CSTRING_LITERALS);
}
bool ParseSectionDirectiveTData(StringRef, SMLoc) {
return ParseSectionSwitch("__DATA", "__thread_data",
MachO::S_THREAD_LOCAL_REGULAR);
}
bool ParseSectionDirectiveText(StringRef, SMLoc) {
return ParseSectionSwitch("__TEXT", "__text",
MachO::S_ATTR_PURE_INSTRUCTIONS);
}
bool ParseSectionDirectiveTLV(StringRef, SMLoc) {
return ParseSectionSwitch("__DATA", "__thread_vars",
MachO::S_THREAD_LOCAL_VARIABLES);
}
bool ParseSectionDirectiveIdent(StringRef, SMLoc) {
// Darwin silently ignores the .ident directive.
getParser().eatToEndOfStatement();
return false;
}
bool ParseSectionDirectiveThreadInitFunc(StringRef, SMLoc) {
return ParseSectionSwitch("__DATA", "__thread_init",
MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
}
+ bool ParseVersionMin(StringRef, SMLoc);
};
} // end anonymous namespace
bool DarwinAsmParser::ParseSectionSwitch(const char *Segment,
const char *Section,
unsigned TAA, unsigned Align,
unsigned StubSize) {
if (getLexer().isNot(AsmToken::EndOfStatement))
return TokError("unexpected token in section switching directive");
Lex();
// FIXME: Arch specific.
bool isText = TAA & MachO::S_ATTR_PURE_INSTRUCTIONS;
getStreamer().SwitchSection(getContext().getMachOSection(
Segment, Section, TAA, StubSize,
isText ? SectionKind::getText()
: SectionKind::getDataRel()));
// Set the implicit alignment, if any.
//
// FIXME: This isn't really what 'as' does; I think it just uses the implicit
// alignment on the section (e.g., if one manually inserts bytes into the
// section, then just issuing the section switch directive will not realign
// the section. However, this is arguably more reasonable behavior, and there
// is no good reason for someone to intentionally emit incorrectly sized
// values into the implicitly aligned sections.
if (Align)
getStreamer().EmitValueToAlignment(Align);
return false;
}
/// ParseDirectiveDesc
/// ::= .desc identifier , expression
bool DarwinAsmParser::ParseDirectiveDesc(StringRef, SMLoc) {
StringRef Name;
if (getParser().parseIdentifier(Name))
return TokError("expected identifier in directive");
// Handle the identifier as the key symbol.
MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
if (getLexer().isNot(AsmToken::Comma))
return TokError("unexpected token in '.desc' directive");
Lex();
int64_t DescValue;
if (getParser().parseAbsoluteExpression(DescValue))
return true;
if (getLexer().isNot(AsmToken::EndOfStatement))
return TokError("unexpected token in '.desc' directive");
Lex();
// Set the n_desc field of this Symbol to this DescValue
getStreamer().EmitSymbolDesc(Sym, DescValue);
return false;
}
/// ParseDirectiveIndirectSymbol
/// ::= .indirect_symbol identifier
bool DarwinAsmParser::ParseDirectiveIndirectSymbol(StringRef, SMLoc Loc) {
const MCSectionMachO *Current = static_cast<const MCSectionMachO*>(
getStreamer().getCurrentSection().first);
MachO::SectionType SectionType = Current->getType();
if (SectionType != MachO::S_NON_LAZY_SYMBOL_POINTERS &&
SectionType != MachO::S_LAZY_SYMBOL_POINTERS &&
SectionType != MachO::S_SYMBOL_STUBS)
return Error(Loc, "indirect symbol not in a symbol pointer or stub "
"section");
StringRef Name;
if (getParser().parseIdentifier(Name))
return TokError("expected identifier in .indirect_symbol directive");
MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
// Assembler local symbols don't make any sense here. Complain loudly.
if (Sym->isTemporary())
return TokError("non-local symbol required in directive");
if (!getStreamer().EmitSymbolAttribute(Sym, MCSA_IndirectSymbol))
return TokError("unable to emit indirect symbol attribute for: " + Name);
if (getLexer().isNot(AsmToken::EndOfStatement))
return TokError("unexpected token in '.indirect_symbol' directive");
Lex();
return false;
}
/// ParseDirectiveDumpOrLoad
/// ::= ( .dump | .load ) "filename"
bool DarwinAsmParser::ParseDirectiveDumpOrLoad(StringRef Directive,
SMLoc IDLoc) {
bool IsDump = Directive == ".dump";
if (getLexer().isNot(AsmToken::String))
return TokError("expected string in '.dump' or '.load' directive");
Lex();
if (getLexer().isNot(AsmToken::EndOfStatement))
return TokError("unexpected token in '.dump' or '.load' directive");
Lex();
// FIXME: If/when .dump and .load are implemented they will be done in the
// the assembly parser and not have any need for an MCStreamer API.
if (IsDump)
return Warning(IDLoc, "ignoring directive .dump for now");
else
return Warning(IDLoc, "ignoring directive .load for now");
}
/// ParseDirectiveLinkerOption
/// ::= .linker_option "string" ( , "string" )*
bool DarwinAsmParser::ParseDirectiveLinkerOption(StringRef IDVal, SMLoc) {
SmallVector<std::string, 4> Args;
for (;;) {
if (getLexer().isNot(AsmToken::String))
return TokError("expected string in '" + Twine(IDVal) + "' directive");
std::string Data;
if (getParser().parseEscapedString(Data))
return true;
Args.push_back(Data);
Lex();
if (getLexer().is(AsmToken::EndOfStatement))
break;
if (getLexer().isNot(AsmToken::Comma))
return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Lex();
}
getStreamer().EmitLinkerOptions(Args);
return false;
}
/// ParseDirectiveLsym
/// ::= .lsym identifier , expression
bool DarwinAsmParser::ParseDirectiveLsym(StringRef, SMLoc) {
StringRef Name;
if (getParser().parseIdentifier(Name))
return TokError("expected identifier in directive");
// Handle the identifier as the key symbol.
MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
if (getLexer().isNot(AsmToken::Comma))
return TokError("unexpected token in '.lsym' directive");
Lex();
const MCExpr *Value;
if (getParser().parseExpression(Value))
return true;
if (getLexer().isNot(AsmToken::EndOfStatement))
return TokError("unexpected token in '.lsym' directive");
Lex();
// We don't currently support this directive.
//
// FIXME: Diagnostic location!
(void) Sym;
return TokError("directive '.lsym' is unsupported");
}
/// ParseDirectiveSection:
/// ::= .section identifier (',' identifier)*
bool DarwinAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
SMLoc Loc = getLexer().getLoc();
StringRef SectionName;
if (getParser().parseIdentifier(SectionName))
return Error(Loc, "expected identifier after '.section' directive");
// Verify there is a following comma.
if (!getLexer().is(AsmToken::Comma))
return TokError("unexpected token in '.section' directive");
std::string SectionSpec = SectionName;
SectionSpec += ",";
// Add all the tokens until the end of the line, ParseSectionSpecifier will
// handle this.
StringRef EOL = getLexer().LexUntilEndOfStatement();
SectionSpec.append(EOL.begin(), EOL.end());
Lex();
if (getLexer().isNot(AsmToken::EndOfStatement))
return TokError("unexpected token in '.section' directive");
Lex();
StringRef Segment, Section;
unsigned StubSize;
unsigned TAA;
bool TAAParsed;
std::string ErrorStr =
MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
TAA, TAAParsed, StubSize);
if (!ErrorStr.empty())
return Error(Loc, ErrorStr.c_str());
// FIXME: Arch specific.
bool isText = Segment == "__TEXT"; // FIXME: Hack.
getStreamer().SwitchSection(getContext().getMachOSection(
Segment, Section, TAA, StubSize,
isText ? SectionKind::getText()
: SectionKind::getDataRel()));
return false;
}
/// ParseDirectivePushSection:
/// ::= .pushsection identifier (',' identifier)*
bool DarwinAsmParser::ParseDirectivePushSection(StringRef S, SMLoc Loc) {
getStreamer().PushSection();
if (ParseDirectiveSection(S, Loc)) {
getStreamer().PopSection();
return true;
}
return false;
}
/// ParseDirectivePopSection:
/// ::= .popsection
bool DarwinAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
if (!getStreamer().PopSection())
return TokError(".popsection without corresponding .pushsection");
return false;
}
/// ParseDirectivePrevious:
/// ::= .previous
bool DarwinAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
if (PreviousSection.first == NULL)
return TokError(".previous without corresponding .section");
getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
return false;
}
/// ParseDirectiveSecureLogUnique
/// ::= .secure_log_unique ... message ...
bool DarwinAsmParser::ParseDirectiveSecureLogUnique(StringRef, SMLoc IDLoc) {
StringRef LogMessage = getParser().parseStringToEndOfStatement();
if (getLexer().isNot(AsmToken::EndOfStatement))
return TokError("unexpected token in '.secure_log_unique' directive");
if (getContext().getSecureLogUsed() != false)
return Error(IDLoc, ".secure_log_unique specified multiple times");
// Get the secure log path.
const char *SecureLogFile = getContext().getSecureLogFile();
if (SecureLogFile == NULL)
return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE "
"environment variable unset.");
// Open the secure log file if we haven't already.
raw_ostream *OS = getContext().getSecureLog();
if (OS == NULL) {
std::string Err;
OS = new raw_fd_ostream(SecureLogFile, Err,
sys::fs::F_Append | sys::fs::F_Text);
if (!Err.empty()) {
delete OS;
return Error(IDLoc, Twine("can't open secure log file: ") +
SecureLogFile + " (" + Err + ")");
}
getContext().setSecureLog(OS);
}
// Write the message.
int CurBuf = getSourceManager().FindBufferContainingLoc(IDLoc);
*OS << getSourceManager().getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
<< ":" << getSourceManager().FindLineNumber(IDLoc, CurBuf) << ":"
<< LogMessage + "\n";
getContext().setSecureLogUsed(true);
return false;
}
/// ParseDirectiveSecureLogReset
/// ::= .secure_log_reset
bool DarwinAsmParser::ParseDirectiveSecureLogReset(StringRef, SMLoc IDLoc) {
if (getLexer().isNot(AsmToken::EndOfStatement))
return TokError("unexpected token in '.secure_log_reset' directive");
Lex();
getContext().setSecureLogUsed(false);
return false;
}
/// ParseDirectiveSubsectionsViaSymbols
/// ::= .subsections_via_symbols
bool DarwinAsmParser::ParseDirectiveSubsectionsViaSymbols(StringRef, SMLoc) {
if (getLexer().isNot(AsmToken::EndOfStatement))
return TokError("unexpected token in '.subsections_via_symbols' directive");
Lex();
getStreamer().EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
return false;
}
/// ParseDirectiveTBSS
/// ::= .tbss identifier, size, align
bool DarwinAsmParser::ParseDirectiveTBSS(StringRef, SMLoc) {
SMLoc IDLoc = getLexer().getLoc();
StringRef Name;
if (getParser().parseIdentifier(Name))
return TokError("expected identifier in directive");
// Handle the identifier as the key symbol.
MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
if (getLexer().isNot(AsmToken::Comma))
return TokError("unexpected token in directive");
Lex();
int64_t Size;
SMLoc SizeLoc = getLexer().getLoc();
if (getParser().parseAbsoluteExpression(Size))
return true;
int64_t Pow2Alignment = 0;
SMLoc Pow2AlignmentLoc;
if (getLexer().is(AsmToken::Comma)) {
Lex();
Pow2AlignmentLoc = getLexer().getLoc();
if (getParser().parseAbsoluteExpression(Pow2Alignment))
return true;
}
if (getLexer().isNot(AsmToken::EndOfStatement))
return TokError("unexpected token in '.tbss' directive");
Lex();
if (Size < 0)
return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
"zero");
// FIXME: Diagnose overflow.
if (Pow2Alignment < 0)
return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
"than zero");
if (!Sym->isUndefined())
return Error(IDLoc, "invalid symbol redefinition");
getStreamer().EmitTBSSSymbol(getContext().getMachOSection(
"__DATA", "__thread_bss",
MachO::S_THREAD_LOCAL_ZEROFILL,
0, SectionKind::getThreadBSS()),
Sym, Size, 1 << Pow2Alignment);
return false;
}
/// ParseDirectiveZerofill
/// ::= .zerofill segname , sectname [, identifier , size_expression [
/// , align_expression ]]
bool DarwinAsmParser::ParseDirectiveZerofill(StringRef, SMLoc) {
StringRef Segment;
if (getParser().parseIdentifier(Segment))
return TokError("expected segment name after '.zerofill' directive");
if (getLexer().isNot(AsmToken::Comma))
return TokError("unexpected token in directive");
Lex();
StringRef Section;
if (getParser().parseIdentifier(Section))
return TokError("expected section name after comma in '.zerofill' "
"directive");
// If this is the end of the line all that was wanted was to create the
// the section but with no symbol.
if (getLexer().is(AsmToken::EndOfStatement)) {
// Create the zerofill section but no symbol
getStreamer().EmitZerofill(getContext().getMachOSection(
Segment, Section, MachO::S_ZEROFILL,
0, SectionKind::getBSS()));
return false;
}
if (getLexer().isNot(AsmToken::Comma))
return TokError("unexpected token in directive");
Lex();
SMLoc IDLoc = getLexer().getLoc();
StringRef IDStr;
if (getParser().parseIdentifier(IDStr))
return TokError("expected identifier in directive");
// handle the identifier as the key symbol.
MCSymbol *Sym = getContext().GetOrCreateSymbol(IDStr);
if (getLexer().isNot(AsmToken::Comma))
return TokError("unexpected token in directive");
Lex();
int64_t Size;
SMLoc SizeLoc = getLexer().getLoc();
if (getParser().parseAbsoluteExpression(Size))
return true;
int64_t Pow2Alignment = 0;
SMLoc Pow2AlignmentLoc;
if (getLexer().is(AsmToken::Comma)) {
Lex();
Pow2AlignmentLoc = getLexer().getLoc();
if (getParser().parseAbsoluteExpression(Pow2Alignment))
return true;
}
if (getLexer().isNot(AsmToken::EndOfStatement))
return TokError("unexpected token in '.zerofill' directive");
Lex();
if (Size < 0)
return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
"than zero");
// NOTE: The alignment in the directive is a power of 2 value, the assembler
// may internally end up wanting an alignment in bytes.
// FIXME: Diagnose overflow.
if (Pow2Alignment < 0)
return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
"can't be less than zero");
if (!Sym->isUndefined())
return Error(IDLoc, "invalid symbol redefinition");
// Create the zerofill Symbol with Size and Pow2Alignment
//
// FIXME: Arch specific.
getStreamer().EmitZerofill(getContext().getMachOSection(
Segment, Section, MachO::S_ZEROFILL,
0, SectionKind::getBSS()),
Sym, Size, 1 << Pow2Alignment);
return false;
}
/// ParseDirectiveDataRegion
/// ::= .data_region [ ( jt8 | jt16 | jt32 ) ]
bool DarwinAsmParser::ParseDirectiveDataRegion(StringRef, SMLoc) {
if (getLexer().is(AsmToken::EndOfStatement)) {
Lex();
getStreamer().EmitDataRegion(MCDR_DataRegion);
return false;
}
StringRef RegionType;
SMLoc Loc = getParser().getTok().getLoc();
if (getParser().parseIdentifier(RegionType))
return TokError("expected region type after '.data_region' directive");
int Kind = StringSwitch<int>(RegionType)
.Case("jt8", MCDR_DataRegionJT8)
.Case("jt16", MCDR_DataRegionJT16)
.Case("jt32", MCDR_DataRegionJT32)
.Default(-1);
if (Kind == -1)
return Error(Loc, "unknown region type in '.data_region' directive");
Lex();
getStreamer().EmitDataRegion((MCDataRegionType)Kind);
return false;
}
/// ParseDirectiveDataRegionEnd
/// ::= .end_data_region
bool DarwinAsmParser::ParseDirectiveDataRegionEnd(StringRef, SMLoc) {
if (getLexer().isNot(AsmToken::EndOfStatement))
return TokError("unexpected token in '.end_data_region' directive");
Lex();
getStreamer().EmitDataRegion(MCDR_DataRegionEnd);
return false;
}
+/// ParseVersionMin
+/// ::= .ios_version_min major,minor[,update]
+/// ::= .macosx_version_min major,minor[,update]
+bool DarwinAsmParser::ParseVersionMin(StringRef Directive, SMLoc) {
+ int64_t Major = 0, Minor = 0, Update = 0;
+ int Kind = StringSwitch<int>(Directive)
+ .Case(".ios_version_min", MCVM_IOSVersionMin)
+ .Case(".macosx_version_min", MCVM_OSXVersionMin);
+ // Get the major version number.
+ if (getLexer().isNot(AsmToken::Integer))
+ return TokError("invalid OS major version number");
+ Major = getLexer().getTok().getIntVal();
+ if (Major > 65535 || Major <= 0)
+ return TokError("invalid OS major version number");
+ Lex();
+ if (getLexer().isNot(AsmToken::Comma))
+ return TokError("minor OS version number required, comma expected");
+ Lex();
+ // Get the minor version number.
+ if (getLexer().isNot(AsmToken::Integer))
+ return TokError("invalid OS minor version number");
+ Minor = getLexer().getTok().getIntVal();
+ if (Minor > 255 || Minor < 0)
+ return TokError("invalid OS minor version number");
+ Lex();
+ // Get the update level, if specified
+ if (getLexer().isNot(AsmToken::EndOfStatement)) {
+ if (getLexer().isNot(AsmToken::Comma))
+ return TokError("invalid update specifier, comma expected");
+ Lex();
+ if (getLexer().isNot(AsmToken::Integer))
+ return TokError("invalid OS update number");
+ Update = getLexer().getTok().getIntVal();
+ if (Update > 255 || Update < 0)
+ return TokError("invalid OS update number");
+ Lex();
+ }
+
+ // We've parsed a correct version specifier, so send it to the streamer.
+ getStreamer().EmitVersionMin((MCVersionMinType)Kind, Major, Minor, Update);
+
+ return false;
+}
+
namespace llvm {
MCAsmParserExtension *createDarwinAsmParser() {
return new DarwinAsmParser;
}
} // end llvm namespace
Index: llvm/trunk/lib/MC/MCAssembler.cpp
===================================================================
--- llvm/trunk/lib/MC/MCAssembler.cpp (revision 204189)
+++ llvm/trunk/lib/MC/MCAssembler.cpp (revision 204190)
@@ -1,1187 +1,1188 @@
//===- lib/MC/MCAssembler.cpp - Assembler Backend Implementation ----------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "assembler"
#include "llvm/MC/MCAssembler.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/Twine.h"
#include "llvm/MC/MCAsmBackend.h"
#include "llvm/MC/MCAsmLayout.h"
#include "llvm/MC/MCCodeEmitter.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCDwarf.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCFixupKindInfo.h"
#include "llvm/MC/MCObjectWriter.h"
#include "llvm/MC/MCSection.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/MC/MCValue.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/LEB128.h"
#include "llvm/Support/TargetRegistry.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
namespace {
namespace stats {
STATISTIC(EmittedFragments, "Number of emitted assembler fragments - total");
STATISTIC(EmittedRelaxableFragments,
"Number of emitted assembler fragments - relaxable");
STATISTIC(EmittedDataFragments,
"Number of emitted assembler fragments - data");
STATISTIC(EmittedCompactEncodedInstFragments,
"Number of emitted assembler fragments - compact encoded inst");
STATISTIC(EmittedAlignFragments,
"Number of emitted assembler fragments - align");
STATISTIC(EmittedFillFragments,
"Number of emitted assembler fragments - fill");
STATISTIC(EmittedOrgFragments,
"Number of emitted assembler fragments - org");
STATISTIC(evaluateFixup, "Number of evaluated fixups");
STATISTIC(FragmentLayouts, "Number of fragment layouts");
STATISTIC(ObjectBytes, "Number of emitted object file bytes");
STATISTIC(RelaxationSteps, "Number of assembler layout and relaxation steps");
STATISTIC(RelaxedInstructions, "Number of relaxed instructions");
}
}
// FIXME FIXME FIXME: There are number of places in this file where we convert
// what is a 64-bit assembler value used for computation into a value in the
// object file, which may truncate it. We should detect that truncation where
// invalid and report errors back.
/* *** */
MCAsmLayout::MCAsmLayout(MCAssembler &Asm)
: Assembler(Asm), LastValidFragment()
{
// Compute the section layout order. Virtual sections must go last.
for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
if (!it->getSection().isVirtualSection())
SectionOrder.push_back(&*it);
for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
if (it->getSection().isVirtualSection())
SectionOrder.push_back(&*it);
}
bool MCAsmLayout::isFragmentValid(const MCFragment *F) const {
const MCSectionData &SD = *F->getParent();
const MCFragment *LastValid = LastValidFragment.lookup(&SD);
if (!LastValid)
return false;
assert(LastValid->getParent() == F->getParent());
return F->getLayoutOrder() <= LastValid->getLayoutOrder();
}
void MCAsmLayout::invalidateFragmentsFrom(MCFragment *F) {
// If this fragment wasn't already valid, we don't need to do anything.
if (!isFragmentValid(F))
return;
// Otherwise, reset the last valid fragment to the previous fragment
// (if this is the first fragment, it will be NULL).
const MCSectionData &SD = *F->getParent();
LastValidFragment[&SD] = F->getPrevNode();
}
void MCAsmLayout::ensureValid(const MCFragment *F) const {
MCSectionData &SD = *F->getParent();
MCFragment *Cur = LastValidFragment[&SD];
if (!Cur)
Cur = &*SD.begin();
else
Cur = Cur->getNextNode();
// Advance the layout position until the fragment is valid.
while (!isFragmentValid(F)) {
assert(Cur && "Layout bookkeeping error");
const_cast<MCAsmLayout*>(this)->layoutFragment(Cur);
Cur = Cur->getNextNode();
}
}
uint64_t MCAsmLayout::getFragmentOffset(const MCFragment *F) const {
ensureValid(F);
assert(F->Offset != ~UINT64_C(0) && "Address not set!");
return F->Offset;
}
uint64_t MCAsmLayout::getSymbolOffset(const MCSymbolData *SD) const {
const MCSymbol &S = SD->getSymbol();
// If this is a variable, then recursively evaluate now.
if (S.isVariable()) {
MCValue Target;
if (!S.getVariableValue()->EvaluateAsRelocatable(Target, this))
report_fatal_error("unable to evaluate offset for variable '" +
S.getName() + "'");
// Verify that any used symbols are defined.
if (Target.getSymA() && Target.getSymA()->getSymbol().isUndefined())
report_fatal_error("unable to evaluate offset to undefined symbol '" +
Target.getSymA()->getSymbol().getName() + "'");
if (Target.getSymB() && Target.getSymB()->getSymbol().isUndefined())
report_fatal_error("unable to evaluate offset to undefined symbol '" +
Target.getSymB()->getSymbol().getName() + "'");
uint64_t Offset = Target.getConstant();
if (Target.getSymA())
Offset += getSymbolOffset(&Assembler.getSymbolData(
Target.getSymA()->getSymbol()));
if (Target.getSymB())
Offset -= getSymbolOffset(&Assembler.getSymbolData(
Target.getSymB()->getSymbol()));
return Offset;
}
assert(SD->getFragment() && "Invalid getOffset() on undefined symbol!");
return getFragmentOffset(SD->getFragment()) + SD->getOffset();
}
uint64_t MCAsmLayout::getSectionAddressSize(const MCSectionData *SD) const {
// The size is the last fragment's end offset.
const MCFragment &F = SD->getFragmentList().back();
return getFragmentOffset(&F) + getAssembler().computeFragmentSize(*this, F);
}
uint64_t MCAsmLayout::getSectionFileSize(const MCSectionData *SD) const {
// Virtual sections have no file size.
if (SD->getSection().isVirtualSection())
return 0;
// Otherwise, the file size is the same as the address space size.
return getSectionAddressSize(SD);
}
uint64_t MCAsmLayout::computeBundlePadding(const MCFragment *F,
uint64_t FOffset, uint64_t FSize) {
uint64_t BundleSize = Assembler.getBundleAlignSize();
assert(BundleSize > 0 &&
"computeBundlePadding should only be called if bundling is enabled");
uint64_t BundleMask = BundleSize - 1;
uint64_t OffsetInBundle = FOffset & BundleMask;
uint64_t EndOfFragment = OffsetInBundle + FSize;
// There are two kinds of bundling restrictions:
//
// 1) For alignToBundleEnd(), add padding to ensure that the fragment will
// *end* on a bundle boundary.
// 2) Otherwise, check if the fragment would cross a bundle boundary. If it
// would, add padding until the end of the bundle so that the fragment
// will start in a new one.
if (F->alignToBundleEnd()) {
// Three possibilities here:
//
// A) The fragment just happens to end at a bundle boundary, so we're good.
// B) The fragment ends before the current bundle boundary: pad it just
// enough to reach the boundary.
// C) The fragment ends after the current bundle boundary: pad it until it
// reaches the end of the next bundle boundary.
//
// Note: this code could be made shorter with some modulo trickery, but it's
// intentionally kept in its more explicit form for simplicity.
if (EndOfFragment == BundleSize)
return 0;
else if (EndOfFragment < BundleSize)
return BundleSize - EndOfFragment;
else { // EndOfFragment > BundleSize
return 2 * BundleSize - EndOfFragment;
}
} else if (EndOfFragment > BundleSize)
return BundleSize - OffsetInBundle;
else
return 0;
}
/* *** */
MCFragment::MCFragment() : Kind(FragmentType(~0)) {
}
MCFragment::~MCFragment() {
}
MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent)
: Kind(_Kind), Parent(_Parent), Atom(0), Offset(~UINT64_C(0))
{
if (Parent)
Parent->getFragmentList().push_back(this);
}
/* *** */
MCEncodedFragment::~MCEncodedFragment() {
}
/* *** */
MCEncodedFragmentWithFixups::~MCEncodedFragmentWithFixups() {
}
/* *** */
MCSectionData::MCSectionData() : Section(0) {}
MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A)
: Section(&_Section),
Ordinal(~UINT32_C(0)),
Alignment(1),
BundleLockState(NotBundleLocked), BundleGroupBeforeFirstInst(false),
HasInstructions(false)
{
if (A)
A->getSectionList().push_back(this);
}
MCSectionData::iterator
MCSectionData::getSubsectionInsertionPoint(unsigned Subsection) {
if (Subsection == 0 && SubsectionFragmentMap.empty())
return end();
SmallVectorImpl<std::pair<unsigned, MCFragment *> >::iterator MI =
std::lower_bound(SubsectionFragmentMap.begin(), SubsectionFragmentMap.end(),
std::make_pair(Subsection, (MCFragment *)0));
bool ExactMatch = false;
if (MI != SubsectionFragmentMap.end()) {
ExactMatch = MI->first == Subsection;
if (ExactMatch)
++MI;
}
iterator IP;
if (MI == SubsectionFragmentMap.end())
IP = end();
else
IP = MI->second;
if (!ExactMatch && Subsection != 0) {
// The GNU as documentation claims that subsections have an alignment of 4,
// although this appears not to be the case.
MCFragment *F = new MCDataFragment();
SubsectionFragmentMap.insert(MI, std::make_pair(Subsection, F));
getFragmentList().insert(IP, F);
F->setParent(this);
}
return IP;
}
/* *** */
MCSymbolData::MCSymbolData() : Symbol(0) {}
MCSymbolData::MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment,
uint64_t _Offset, MCAssembler *A)
: Symbol(&_Symbol), Fragment(_Fragment), Offset(_Offset),
IsExternal(false), IsPrivateExtern(false),
CommonSize(0), SymbolSize(0), CommonAlign(0),
Flags(0), Index(0)
{
if (A)
A->getSymbolList().push_back(this);
}
/* *** */
MCAssembler::MCAssembler(MCContext &Context_, MCAsmBackend &Backend_,
MCCodeEmitter &Emitter_, MCObjectWriter &Writer_,
raw_ostream &OS_)
: Context(Context_), Backend(Backend_), Emitter(Emitter_), Writer(Writer_),
OS(OS_), BundleAlignSize(0), RelaxAll(false), NoExecStack(false),
SubsectionsViaSymbols(false), ELFHeaderEFlags(0) {
+ VersionMinInfo.Major = 0; // Major version == 0 for "none specified"
}
MCAssembler::~MCAssembler() {
}
void MCAssembler::reset() {
Sections.clear();
Symbols.clear();
SectionMap.clear();
SymbolMap.clear();
IndirectSymbols.clear();
DataRegions.clear();
ThumbFuncs.clear();
RelaxAll = false;
NoExecStack = false;
SubsectionsViaSymbols = false;
ELFHeaderEFlags = 0;
// reset objects owned by us
getBackend().reset();
getEmitter().reset();
getWriter().reset();
}
bool MCAssembler::isSymbolLinkerVisible(const MCSymbol &Symbol) const {
// Non-temporary labels should always be visible to the linker.
if (!Symbol.isTemporary())
return true;
// Absolute temporary labels are never visible.
if (!Symbol.isInSection())
return false;
// Otherwise, check if the section requires symbols even for temporary labels.
return getBackend().doesSectionRequireSymbols(Symbol.getSection());
}
const MCSymbolData *MCAssembler::getAtom(const MCSymbolData *SD) const {
// Linker visible symbols define atoms.
if (isSymbolLinkerVisible(SD->getSymbol()))
return SD;
// Absolute and undefined symbols have no defining atom.
if (!SD->getFragment())
return 0;
// Non-linker visible symbols in sections which can't be atomized have no
// defining atom.
if (!getBackend().isSectionAtomizable(
SD->getFragment()->getParent()->getSection()))
return 0;
// Otherwise, return the atom for the containing fragment.
return SD->getFragment()->getAtom();
}
bool MCAssembler::evaluateFixup(const MCAsmLayout &Layout,
const MCFixup &Fixup, const MCFragment *DF,
MCValue &Target, uint64_t &Value) const {
++stats::evaluateFixup;
if (!Fixup.getValue()->EvaluateAsRelocatable(Target, &Layout))
getContext().FatalError(Fixup.getLoc(), "expected relocatable expression");
bool IsPCRel = Backend.getFixupKindInfo(
Fixup.getKind()).Flags & MCFixupKindInfo::FKF_IsPCRel;
bool IsResolved;
if (IsPCRel) {
if (Target.getSymB()) {
IsResolved = false;
} else if (!Target.getSymA()) {
IsResolved = false;
} else {
const MCSymbolRefExpr *A = Target.getSymA();
const MCSymbol &SA = A->getSymbol();
if (A->getKind() != MCSymbolRefExpr::VK_None ||
SA.AliasedSymbol().isUndefined()) {
IsResolved = false;
} else {
const MCSymbolData &DataA = getSymbolData(SA);
IsResolved =
getWriter().IsSymbolRefDifferenceFullyResolvedImpl(*this, DataA,
*DF, false, true);
}
}
} else {
IsResolved = Target.isAbsolute();
}
Value = Target.getConstant();
if (const MCSymbolRefExpr *A = Target.getSymA()) {
const MCSymbol &Sym = A->getSymbol().AliasedSymbol();
if (Sym.isDefined())
Value += Layout.getSymbolOffset(&getSymbolData(Sym));
}
if (const MCSymbolRefExpr *B = Target.getSymB()) {
const MCSymbol &Sym = B->getSymbol().AliasedSymbol();
if (Sym.isDefined())
Value -= Layout.getSymbolOffset(&getSymbolData(Sym));
}
bool ShouldAlignPC = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
MCFixupKindInfo::FKF_IsAlignedDownTo32Bits;
assert((ShouldAlignPC ? IsPCRel : true) &&
"FKF_IsAlignedDownTo32Bits is only allowed on PC-relative fixups!");
if (IsPCRel) {
uint32_t Offset = Layout.getFragmentOffset(DF) + Fixup.getOffset();
// A number of ARM fixups in Thumb mode require that the effective PC
// address be determined as the 32-bit aligned version of the actual offset.
if (ShouldAlignPC) Offset &= ~0x3;
Value -= Offset;
}
// Let the backend adjust the fixup value if necessary, including whether
// we need a relocation.
Backend.processFixupValue(*this, Layout, Fixup, DF, Target, Value,
IsResolved);
return IsResolved;
}
uint64_t MCAssembler::computeFragmentSize(const MCAsmLayout &Layout,
const MCFragment &F) const {
switch (F.getKind()) {
case MCFragment::FT_Data:
case MCFragment::FT_Relaxable:
case MCFragment::FT_CompactEncodedInst:
return cast<MCEncodedFragment>(F).getContents().size();
case MCFragment::FT_Fill:
return cast<MCFillFragment>(F).getSize();
case MCFragment::FT_LEB:
return cast<MCLEBFragment>(F).getContents().size();
case MCFragment::FT_Align: {
const MCAlignFragment &AF = cast<MCAlignFragment>(F);
unsigned Offset = Layout.getFragmentOffset(&AF);
unsigned Size = OffsetToAlignment(Offset, AF.getAlignment());
// If we are padding with nops, force the padding to be larger than the
// minimum nop size.
if (Size > 0 && AF.hasEmitNops()) {
while (Size % getBackend().getMinimumNopSize())
Size += AF.getAlignment();
}
if (Size > AF.getMaxBytesToEmit())
return 0;
return Size;
}
case MCFragment::FT_Org: {
const MCOrgFragment &OF = cast<MCOrgFragment>(F);
int64_t TargetLocation;
if (!OF.getOffset().EvaluateAsAbsolute(TargetLocation, Layout))
report_fatal_error("expected assembly-time absolute expression");
// FIXME: We need a way to communicate this error.
uint64_t FragmentOffset = Layout.getFragmentOffset(&OF);
int64_t Size = TargetLocation - FragmentOffset;
if (Size < 0 || Size >= 0x40000000)
report_fatal_error("invalid .org offset '" + Twine(TargetLocation) +
"' (at offset '" + Twine(FragmentOffset) + "')");
return Size;
}
case MCFragment::FT_Dwarf:
return cast<MCDwarfLineAddrFragment>(F).getContents().size();
case MCFragment::FT_DwarfFrame:
return cast<MCDwarfCallFrameFragment>(F).getContents().size();
}
llvm_unreachable("invalid fragment kind");
}
void MCAsmLayout::layoutFragment(MCFragment *F) {
MCFragment *Prev = F->getPrevNode();
// We should never try to recompute something which is valid.
assert(!isFragmentValid(F) && "Attempt to recompute a valid fragment!");
// We should never try to compute the fragment layout if its predecessor
// isn't valid.
assert((!Prev || isFragmentValid(Prev)) &&
"Attempt to compute fragment before its predecessor!");
++stats::FragmentLayouts;
// Compute fragment offset and size.
if (Prev)
F->Offset = Prev->Offset + getAssembler().computeFragmentSize(*this, *Prev);
else
F->Offset = 0;
LastValidFragment[F->getParent()] = F;
// If bundling is enabled and this fragment has instructions in it, it has to
// obey the bundling restrictions. With padding, we'll have:
//
//
// BundlePadding
// |||
// -------------------------------------
// Prev |##########| F |
// -------------------------------------
// ^
// |
// F->Offset
//
// The fragment's offset will point to after the padding, and its computed
// size won't include the padding.
//
if (Assembler.isBundlingEnabled() && F->hasInstructions()) {
assert(isa<MCEncodedFragment>(F) &&
"Only MCEncodedFragment implementations have instructions");
uint64_t FSize = Assembler.computeFragmentSize(*this, *F);
if (FSize > Assembler.getBundleAlignSize())
report_fatal_error("Fragment can't be larger than a bundle size");
uint64_t RequiredBundlePadding = computeBundlePadding(F, F->Offset, FSize);
if (RequiredBundlePadding > UINT8_MAX)
report_fatal_error("Padding cannot exceed 255 bytes");
F->setBundlePadding(static_cast<uint8_t>(RequiredBundlePadding));
F->Offset += RequiredBundlePadding;
}
}
/// \brief Write the contents of a fragment to the given object writer. Expects
/// a MCEncodedFragment.
static void writeFragmentContents(const MCFragment &F, MCObjectWriter *OW) {
const MCEncodedFragment &EF = cast<MCEncodedFragment>(F);
OW->WriteBytes(EF.getContents());
}
/// \brief Write the fragment \p F to the output file.
static void writeFragment(const MCAssembler &Asm, const MCAsmLayout &Layout,
const MCFragment &F) {
MCObjectWriter *OW = &Asm.getWriter();
// FIXME: Embed in fragments instead?
uint64_t FragmentSize = Asm.computeFragmentSize(Layout, F);
// Should NOP padding be written out before this fragment?
unsigned BundlePadding = F.getBundlePadding();
if (BundlePadding > 0) {
assert(Asm.isBundlingEnabled() &&
"Writing bundle padding with disabled bundling");
assert(F.hasInstructions() &&
"Writing bundle padding for a fragment without instructions");
unsigned TotalLength = BundlePadding + static_cast<unsigned>(FragmentSize);
if (F.alignToBundleEnd() && TotalLength > Asm.getBundleAlignSize()) {
// If the padding itself crosses a bundle boundary, it must be emitted
// in 2 pieces, since even nop instructions must not cross boundaries.
// v--------------v <- BundleAlignSize
// v---------v <- BundlePadding
// ----------------------------
// | Prev |####|####| F |
// ----------------------------
// ^-------------------^ <- TotalLength
unsigned DistanceToBoundary = TotalLength - Asm.getBundleAlignSize();
if (!Asm.getBackend().writeNopData(DistanceToBoundary, OW))
report_fatal_error("unable to write NOP sequence of " +
Twine(DistanceToBoundary) + " bytes");
BundlePadding -= DistanceToBoundary;
}
if (!Asm.getBackend().writeNopData(BundlePadding, OW))
report_fatal_error("unable to write NOP sequence of " +
Twine(BundlePadding) + " bytes");
}
// This variable (and its dummy usage) is to participate in the assert at
// the end of the function.
uint64_t Start = OW->getStream().tell();
(void) Start;
++stats::EmittedFragments;
switch (F.getKind()) {
case MCFragment::FT_Align: {
++stats::EmittedAlignFragments;
const MCAlignFragment &AF = cast<MCAlignFragment>(F);
assert(AF.getValueSize() && "Invalid virtual align in concrete fragment!");
uint64_t Count = FragmentSize / AF.getValueSize();
// FIXME: This error shouldn't actually occur (the front end should emit
// multiple .align directives to enforce the semantics it wants), but is
// severe enough that we want to report it. How to handle this?
if (Count * AF.getValueSize() != FragmentSize)
report_fatal_error("undefined .align directive, value size '" +
Twine(AF.getValueSize()) +
"' is not a divisor of padding size '" +
Twine(FragmentSize) + "'");
// See if we are aligning with nops, and if so do that first to try to fill
// the Count bytes. Then if that did not fill any bytes or there are any
// bytes left to fill use the Value and ValueSize to fill the rest.
// If we are aligning with nops, ask that target to emit the right data.
if (AF.hasEmitNops()) {
if (!Asm.getBackend().writeNopData(Count, OW))
report_fatal_error("unable to write nop sequence of " +
Twine(Count) + " bytes");
break;
}
// Otherwise, write out in multiples of the value size.
for (uint64_t i = 0; i != Count; ++i) {
switch (AF.getValueSize()) {
default: llvm_unreachable("Invalid size!");
case 1: OW->Write8 (uint8_t (AF.getValue())); break;
case 2: OW->Write16(uint16_t(AF.getValue())); break;
case 4: OW->Write32(uint32_t(AF.getValue())); break;
case 8: OW->Write64(uint64_t(AF.getValue())); break;
}
}
break;
}
case MCFragment::FT_Data:
++stats::EmittedDataFragments;
writeFragmentContents(F, OW);
break;
case MCFragment::FT_Relaxable:
++stats::EmittedRelaxableFragments;
writeFragmentContents(F, OW);
break;
case MCFragment::FT_CompactEncodedInst:
++stats::EmittedCompactEncodedInstFragments;
writeFragmentContents(F, OW);
break;
case MCFragment::FT_Fill: {
++stats::EmittedFillFragments;
const MCFillFragment &FF = cast<MCFillFragment>(F);
assert(FF.getValueSize() && "Invalid virtual align in concrete fragment!");
for (uint64_t i = 0, e = FF.getSize() / FF.getValueSize(); i != e; ++i) {
switch (FF.getValueSize()) {
default: llvm_unreachable("Invalid size!");
case 1: OW->Write8 (uint8_t (FF.getValue())); break;
case 2: OW->Write16(uint16_t(FF.getValue())); break;
case 4: OW->Write32(uint32_t(FF.getValue())); break;
case 8: OW->Write64(uint64_t(FF.getValue())); break;
}
}
break;
}
case MCFragment::FT_LEB: {
const MCLEBFragment &LF = cast<MCLEBFragment>(F);
OW->WriteBytes(LF.getContents().str());
break;
}
case MCFragment::FT_Org: {
++stats::EmittedOrgFragments;
const MCOrgFragment &OF = cast<MCOrgFragment>(F);
for (uint64_t i = 0, e = FragmentSize; i != e; ++i)
OW->Write8(uint8_t(OF.getValue()));
break;
}
case MCFragment::FT_Dwarf: {
const MCDwarfLineAddrFragment &OF = cast<MCDwarfLineAddrFragment>(F);
OW->WriteBytes(OF.getContents().str());
break;
}
case MCFragment::FT_DwarfFrame: {
const MCDwarfCallFrameFragment &CF = cast<MCDwarfCallFrameFragment>(F);
OW->WriteBytes(CF.getContents().str());
break;
}
}
assert(OW->getStream().tell() - Start == FragmentSize &&
"The stream should advance by fragment size");
}
void MCAssembler::writeSectionData(const MCSectionData *SD,
const MCAsmLayout &Layout) const {
// Ignore virtual sections.
if (SD->getSection().isVirtualSection()) {
assert(Layout.getSectionFileSize(SD) == 0 && "Invalid size for section!");
// Check that contents are only things legal inside a virtual section.
for (MCSectionData::const_iterator it = SD->begin(),
ie = SD->end(); it != ie; ++it) {
switch (it->getKind()) {
default: llvm_unreachable("Invalid fragment in virtual section!");
case MCFragment::FT_Data: {
// Check that we aren't trying to write a non-zero contents (or fixups)
// into a virtual section. This is to support clients which use standard
// directives to fill the contents of virtual sections.
const MCDataFragment &DF = cast<MCDataFragment>(*it);
assert(DF.fixup_begin() == DF.fixup_end() &&
"Cannot have fixups in virtual section!");
for (unsigned i = 0, e = DF.getContents().size(); i != e; ++i)
assert(DF.getContents()[i] == 0 &&
"Invalid data value for virtual section!");
break;
}
case MCFragment::FT_Align:
// Check that we aren't trying to write a non-zero value into a virtual
// section.
assert((cast<MCAlignFragment>(it)->getValueSize() == 0 ||
cast<MCAlignFragment>(it)->getValue() == 0) &&
"Invalid align in virtual section!");
break;
case MCFragment::FT_Fill:
assert((cast<MCFillFragment>(it)->getValueSize() == 0 ||
cast<MCFillFragment>(it)->getValue() == 0) &&
"Invalid fill in virtual section!");
break;
}
}
return;
}
uint64_t Start = getWriter().getStream().tell();
(void)Start;
for (MCSectionData::const_iterator it = SD->begin(), ie = SD->end();
it != ie; ++it)
writeFragment(*this, Layout, *it);
assert(getWriter().getStream().tell() - Start ==
Layout.getSectionAddressSize(SD));
}
uint64_t MCAssembler::handleFixup(const MCAsmLayout &Layout,
MCFragment &F,
const MCFixup &Fixup) {
// Evaluate the fixup.
MCValue Target;
uint64_t FixedValue;
if (!evaluateFixup(Layout, Fixup, &F, Target, FixedValue)) {
// The fixup was unresolved, we need a relocation. Inform the object
// writer of the relocation, and give it an opportunity to adjust the
// fixup value if need be.
getWriter().RecordRelocation(*this, Layout, &F, Fixup, Target, FixedValue);
}
return FixedValue;
}
void MCAssembler::Finish() {
DEBUG_WITH_TYPE("mc-dump", {
llvm::errs() << "assembler backend - pre-layout\n--\n";
dump(); });
// Create the layout object.
MCAsmLayout Layout(*this);
// Create dummy fragments and assign section ordinals.
unsigned SectionIndex = 0;
for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
// Create dummy fragments to eliminate any empty sections, this simplifies
// layout.
if (it->getFragmentList().empty())
new MCDataFragment(it);
it->setOrdinal(SectionIndex++);
}
// Assign layout order indices to sections and fragments.
for (unsigned i = 0, e = Layout.getSectionOrder().size(); i != e; ++i) {
MCSectionData *SD = Layout.getSectionOrder()[i];
SD->setLayoutOrder(i);
unsigned FragmentIndex = 0;
for (MCSectionData::iterator iFrag = SD->begin(), iFragEnd = SD->end();
iFrag != iFragEnd; ++iFrag)
iFrag->setLayoutOrder(FragmentIndex++);
}
// Layout until everything fits.
while (layoutOnce(Layout))
continue;
DEBUG_WITH_TYPE("mc-dump", {
llvm::errs() << "assembler backend - post-relaxation\n--\n";
dump(); });
// Finalize the layout, including fragment lowering.
finishLayout(Layout);
DEBUG_WITH_TYPE("mc-dump", {
llvm::errs() << "assembler backend - final-layout\n--\n";
dump(); });
uint64_t StartOffset = OS.tell();
// Allow the object writer a chance to perform post-layout binding (for
// example, to set the index fields in the symbol data).
getWriter().ExecutePostLayoutBinding(*this, Layout);
// Evaluate and apply the fixups, generating relocation entries as necessary.
for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
for (MCSectionData::iterator it2 = it->begin(),
ie2 = it->end(); it2 != ie2; ++it2) {
MCEncodedFragmentWithFixups *F =
dyn_cast<MCEncodedFragmentWithFixups>(it2);
if (F) {
for (MCEncodedFragmentWithFixups::fixup_iterator it3 = F->fixup_begin(),
ie3 = F->fixup_end(); it3 != ie3; ++it3) {
MCFixup &Fixup = *it3;
uint64_t FixedValue = handleFixup(Layout, *F, Fixup);
getBackend().applyFixup(Fixup, F->getContents().data(),
F->getContents().size(), FixedValue);
}
}
}
}
// Write the object file.
getWriter().WriteObject(*this, Layout);
stats::ObjectBytes += OS.tell() - StartOffset;
}
bool MCAssembler::fixupNeedsRelaxation(const MCFixup &Fixup,
const MCRelaxableFragment *DF,
const MCAsmLayout &Layout) const {
// If we cannot resolve the fixup value, it requires relaxation.
MCValue Target;
uint64_t Value;
if (!evaluateFixup(Layout, Fixup, DF, Target, Value))
return true;
return getBackend().fixupNeedsRelaxation(Fixup, Value, DF, Layout);
}
bool MCAssembler::fragmentNeedsRelaxation(const MCRelaxableFragment *F,
const MCAsmLayout &Layout) const {
// If this inst doesn't ever need relaxation, ignore it. This occurs when we
// are intentionally pushing out inst fragments, or because we relaxed a
// previous instruction to one that doesn't need relaxation.
if (!getBackend().mayNeedRelaxation(F->getInst()))
return false;
for (MCRelaxableFragment::const_fixup_iterator it = F->fixup_begin(),
ie = F->fixup_end(); it != ie; ++it)
if (fixupNeedsRelaxation(*it, F, Layout))
return true;
return false;
}
bool MCAssembler::relaxInstruction(MCAsmLayout &Layout,
MCRelaxableFragment &F) {
if (!fragmentNeedsRelaxation(&F, Layout))
return false;
++stats::RelaxedInstructions;
// FIXME-PERF: We could immediately lower out instructions if we can tell
// they are fully resolved, to avoid retesting on later passes.
// Relax the fragment.
MCInst Relaxed;
getBackend().relaxInstruction(F.getInst(), Relaxed);
// Encode the new instruction.
//
// FIXME-PERF: If it matters, we could let the target do this. It can
// probably do so more efficiently in many cases.
SmallVector<MCFixup, 4> Fixups;
SmallString<256> Code;
raw_svector_ostream VecOS(Code);
getEmitter().EncodeInstruction(Relaxed, VecOS, Fixups, F.getSubtargetInfo());
VecOS.flush();
// Update the fragment.
F.setInst(Relaxed);
F.getContents() = Code;
F.getFixups() = Fixups;
return true;
}
bool MCAssembler::relaxLEB(MCAsmLayout &Layout, MCLEBFragment &LF) {
int64_t Value = 0;
uint64_t OldSize = LF.getContents().size();
bool IsAbs = LF.getValue().EvaluateAsAbsolute(Value, Layout);
(void)IsAbs;
assert(IsAbs);
SmallString<8> &Data = LF.getContents();
Data.clear();
raw_svector_ostream OSE(Data);
if (LF.isSigned())
encodeSLEB128(Value, OSE);
else
encodeULEB128(Value, OSE);
OSE.flush();
return OldSize != LF.getContents().size();
}
bool MCAssembler::relaxDwarfLineAddr(MCAsmLayout &Layout,
MCDwarfLineAddrFragment &DF) {
MCContext &Context = Layout.getAssembler().getContext();
int64_t AddrDelta = 0;
uint64_t OldSize = DF.getContents().size();
bool IsAbs = DF.getAddrDelta().EvaluateAsAbsolute(AddrDelta, Layout);
(void)IsAbs;
assert(IsAbs);
int64_t LineDelta;
LineDelta = DF.getLineDelta();
SmallString<8> &Data = DF.getContents();
Data.clear();
raw_svector_ostream OSE(Data);
MCDwarfLineAddr::Encode(Context, LineDelta, AddrDelta, OSE);
OSE.flush();
return OldSize != Data.size();
}
bool MCAssembler::relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
MCDwarfCallFrameFragment &DF) {
MCContext &Context = Layout.getAssembler().getContext();
int64_t AddrDelta = 0;
uint64_t OldSize = DF.getContents().size();
bool IsAbs = DF.getAddrDelta().EvaluateAsAbsolute(AddrDelta, Layout);
(void)IsAbs;
assert(IsAbs);
SmallString<8> &Data = DF.getContents();
Data.clear();
raw_svector_ostream OSE(Data);
MCDwarfFrameEmitter::EncodeAdvanceLoc(Context, AddrDelta, OSE);
OSE.flush();
return OldSize != Data.size();
}
bool MCAssembler::layoutSectionOnce(MCAsmLayout &Layout, MCSectionData &SD) {
// Holds the first fragment which needed relaxing during this layout. It will
// remain NULL if none were relaxed.
// When a fragment is relaxed, all the fragments following it should get
// invalidated because their offset is going to change.
MCFragment *FirstRelaxedFragment = NULL;
// Attempt to relax all the fragments in the section.
for (MCSectionData::iterator I = SD.begin(), IE = SD.end(); I != IE; ++I) {
// Check if this is a fragment that needs relaxation.
bool RelaxedFrag = false;
switch(I->getKind()) {
default:
break;
case MCFragment::FT_Relaxable:
assert(!getRelaxAll() &&
"Did not expect a MCRelaxableFragment in RelaxAll mode");
RelaxedFrag = relaxInstruction(Layout, *cast<MCRelaxableFragment>(I));
break;
case MCFragment::FT_Dwarf:
RelaxedFrag = relaxDwarfLineAddr(Layout,
*cast<MCDwarfLineAddrFragment>(I));
break;
case MCFragment::FT_DwarfFrame:
RelaxedFrag =
relaxDwarfCallFrameFragment(Layout,
*cast<MCDwarfCallFrameFragment>(I));
break;
case MCFragment::FT_LEB:
RelaxedFrag = relaxLEB(Layout, *cast<MCLEBFragment>(I));
break;
}
if (RelaxedFrag && !FirstRelaxedFragment)
FirstRelaxedFragment = I;
}
if (FirstRelaxedFragment) {
Layout.invalidateFragmentsFrom(FirstRelaxedFragment);
return true;
}
return false;
}
bool MCAssembler::layoutOnce(MCAsmLayout &Layout) {
++stats::RelaxationSteps;
bool WasRelaxed = false;
for (iterator it = begin(), ie = end(); it != ie; ++it) {
MCSectionData &SD = *it;
while (layoutSectionOnce(Layout, SD))
WasRelaxed = true;
}
return WasRelaxed;
}
void MCAssembler::finishLayout(MCAsmLayout &Layout) {
// The layout is done. Mark every fragment as valid.
for (unsigned int i = 0, n = Layout.getSectionOrder().size(); i != n; ++i) {
Layout.getFragmentOffset(&*Layout.getSectionOrder()[i]->rbegin());
}
}
// Debugging methods
namespace llvm {
raw_ostream &operator<<(raw_ostream &OS, const MCFixup &AF) {
OS << "<MCFixup" << " Offset:" << AF.getOffset()
<< " Value:" << *AF.getValue()
<< " Kind:" << AF.getKind() << ">";
return OS;
}
}
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
void MCFragment::dump() {
raw_ostream &OS = llvm::errs();
OS << "<";
switch (getKind()) {
case MCFragment::FT_Align: OS << "MCAlignFragment"; break;
case MCFragment::FT_Data: OS << "MCDataFragment"; break;
case MCFragment::FT_CompactEncodedInst:
OS << "MCCompactEncodedInstFragment"; break;
case MCFragment::FT_Fill: OS << "MCFillFragment"; break;
case MCFragment::FT_Relaxable: OS << "MCRelaxableFragment"; break;
case MCFragment::FT_Org: OS << "MCOrgFragment"; break;
case MCFragment::FT_Dwarf: OS << "MCDwarfFragment"; break;
case MCFragment::FT_DwarfFrame: OS << "MCDwarfCallFrameFragment"; break;
case MCFragment::FT_LEB: OS << "MCLEBFragment"; break;
}
OS << "<MCFragment " << (void*) this << " LayoutOrder:" << LayoutOrder
<< " Offset:" << Offset
<< " HasInstructions:" << hasInstructions()
<< " BundlePadding:" << static_cast<unsigned>(getBundlePadding()) << ">";
switch (getKind()) {
case MCFragment::FT_Align: {
const MCAlignFragment *AF = cast<MCAlignFragment>(this);
if (AF->hasEmitNops())
OS << " (emit nops)";
OS << "\n ";
OS << " Alignment:" << AF->getAlignment()
<< " Value:" << AF->getValue() << " ValueSize:" << AF->getValueSize()
<< " MaxBytesToEmit:" << AF->getMaxBytesToEmit() << ">";
break;
}
case MCFragment::FT_Data: {
const MCDataFragment *DF = cast<MCDataFragment>(this);
OS << "\n ";
OS << " Contents:[";
const SmallVectorImpl<char> &Contents = DF->getContents();
for (unsigned i = 0, e = Contents.size(); i != e; ++i) {
if (i) OS << ",";
OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
}
OS << "] (" << Contents.size() << " bytes)";
if (DF->fixup_begin() != DF->fixup_end()) {
OS << ",\n ";
OS << " Fixups:[";
for (MCDataFragment::const_fixup_iterator it = DF->fixup_begin(),
ie = DF->fixup_end(); it != ie; ++it) {
if (it != DF->fixup_begin()) OS << ",\n ";
OS << *it;
}
OS << "]";
}
break;
}
case MCFragment::FT_CompactEncodedInst: {
const MCCompactEncodedInstFragment *CEIF =
cast<MCCompactEncodedInstFragment>(this);
OS << "\n ";
OS << " Contents:[";
const SmallVectorImpl<char> &Contents = CEIF->getContents();
for (unsigned i = 0, e = Contents.size(); i != e; ++i) {
if (i) OS << ",";
OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
}
OS << "] (" << Contents.size() << " bytes)";
break;
}
case MCFragment::FT_Fill: {
const MCFillFragment *FF = cast<MCFillFragment>(this);
OS << " Value:" << FF->getValue() << " ValueSize:" << FF->getValueSize()
<< " Size:" << FF->getSize();
break;
}
case MCFragment::FT_Relaxable: {
const MCRelaxableFragment *F = cast<MCRelaxableFragment>(this);
OS << "\n ";
OS << " Inst:";
F->getInst().dump_pretty(OS);
break;
}
case MCFragment::FT_Org: {
const MCOrgFragment *OF = cast<MCOrgFragment>(this);
OS << "\n ";
OS << " Offset:" << OF->getOffset() << " Value:" << OF->getValue();
break;
}
case MCFragment::FT_Dwarf: {
const MCDwarfLineAddrFragment *OF = cast<MCDwarfLineAddrFragment>(this);
OS << "\n ";
OS << " AddrDelta:" << OF->getAddrDelta()
<< " LineDelta:" << OF->getLineDelta();
break;
}
case MCFragment::FT_DwarfFrame: {
const MCDwarfCallFrameFragment *CF = cast<MCDwarfCallFrameFragment>(this);
OS << "\n ";
OS << " AddrDelta:" << CF->getAddrDelta();
break;
}
case MCFragment::FT_LEB: {
const MCLEBFragment *LF = cast<MCLEBFragment>(this);
OS << "\n ";
OS << " Value:" << LF->getValue() << " Signed:" << LF->isSigned();
break;
}
}
OS << ">";
}
void MCSectionData::dump() {
raw_ostream &OS = llvm::errs();
OS << "<MCSectionData";
OS << " Alignment:" << getAlignment()
<< " Fragments:[\n ";
for (iterator it = begin(), ie = end(); it != ie; ++it) {
if (it != begin()) OS << ",\n ";
it->dump();
}
OS << "]>";
}
void MCSymbolData::dump() {
raw_ostream &OS = llvm::errs();
OS << "<MCSymbolData Symbol:" << getSymbol()
<< " Fragment:" << getFragment() << " Offset:" << getOffset()
<< " Flags:" << getFlags() << " Index:" << getIndex();
if (isCommon())
OS << " (common, size:" << getCommonSize()
<< " align: " << getCommonAlignment() << ")";
if (isExternal())
OS << " (external)";
if (isPrivateExtern())
OS << " (private extern)";
OS << ">";
}
void MCAssembler::dump() {
raw_ostream &OS = llvm::errs();
OS << "<MCAssembler\n";
OS << " Sections:[\n ";
for (iterator it = begin(), ie = end(); it != ie; ++it) {
if (it != begin()) OS << ",\n ";
it->dump();
}
OS << "],\n";
OS << " Symbols:[";
for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) {
if (it != symbol_begin()) OS << ",\n ";
it->dump();
}
OS << "]>\n";
}
#endif
// anchors for MC*Fragment vtables
void MCEncodedFragment::anchor() { }
void MCEncodedFragmentWithFixups::anchor() { }
void MCDataFragment::anchor() { }
void MCCompactEncodedInstFragment::anchor() { }
void MCRelaxableFragment::anchor() { }
void MCAlignFragment::anchor() { }
void MCFillFragment::anchor() { }
void MCOrgFragment::anchor() { }
void MCLEBFragment::anchor() { }
void MCDwarfLineAddrFragment::anchor() { }
void MCDwarfCallFrameFragment::anchor() { }
Index: llvm/trunk/lib/MC/MCMachOStreamer.cpp
===================================================================
--- llvm/trunk/lib/MC/MCMachOStreamer.cpp (revision 204189)
+++ llvm/trunk/lib/MC/MCMachOStreamer.cpp (revision 204190)
@@ -1,426 +1,433 @@
//===-- MCMachOStreamer.cpp - MachO Streamer ------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/MC/MCStreamer.h"
#include "llvm/MC/MCAsmBackend.h"
#include "llvm/MC/MCAssembler.h"
#include "llvm/MC/MCCodeEmitter.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCDwarf.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCInst.h"
#include "llvm/MC/MCMachOSymbolFlags.h"
#include "llvm/MC/MCObjectFileInfo.h"
#include "llvm/MC/MCObjectStreamer.h"
#include "llvm/MC/MCSection.h"
#include "llvm/MC/MCSectionMachO.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/Support/Dwarf.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
namespace {
class MCMachOStreamer : public MCObjectStreamer {
private:
void EmitInstToData(const MCInst &Inst, const MCSubtargetInfo &STI) override;
void EmitDataRegion(DataRegionData::KindTy Kind);
void EmitDataRegionEnd();
public:
MCMachOStreamer(MCContext &Context, MCAsmBackend &MAB, raw_ostream &OS,
MCCodeEmitter *Emitter)
: MCObjectStreamer(Context, MAB, OS, Emitter) {}
/// @name MCStreamer Interface
/// @{
void EmitLabel(MCSymbol *Symbol) override;
void EmitDebugLabel(MCSymbol *Symbol) override;
void EmitEHSymAttributes(const MCSymbol *Symbol, MCSymbol *EHSymbol) override;
void EmitAssemblerFlag(MCAssemblerFlag Flag) override;
void EmitLinkerOptions(ArrayRef<std::string> Options) override;
void EmitDataRegion(MCDataRegionType Kind) override;
+ void EmitVersionMin(MCVersionMinType Kind, unsigned Major,
+ unsigned Minor, unsigned Update) override;
void EmitThumbFunc(MCSymbol *Func) override;
bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override;
void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override;
void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
unsigned ByteAlignment) override;
void BeginCOFFSymbolDef(const MCSymbol *Symbol) override {
llvm_unreachable("macho doesn't support this directive");
}
void EmitCOFFSymbolStorageClass(int StorageClass) override {
llvm_unreachable("macho doesn't support this directive");
}
void EmitCOFFSymbolType(int Type) override {
llvm_unreachable("macho doesn't support this directive");
}
void EndCOFFSymbolDef() override {
llvm_unreachable("macho doesn't support this directive");
}
void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) override {
llvm_unreachable("macho doesn't support this directive");
}
void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
unsigned ByteAlignment) override;
void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
uint64_t Size = 0, unsigned ByteAlignment = 0) override;
virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
uint64_t Size, unsigned ByteAlignment = 0) override;
void EmitFileDirective(StringRef Filename) override {
// FIXME: Just ignore the .file; it isn't important enough to fail the
// entire assembly.
// report_fatal_error("unsupported directive: '.file'");
}
void EmitIdent(StringRef IdentString) override {
llvm_unreachable("macho doesn't support this directive");
}
void FinishImpl() override;
};
} // end anonymous namespace.
void MCMachOStreamer::EmitEHSymAttributes(const MCSymbol *Symbol,
MCSymbol *EHSymbol) {
MCSymbolData &SD =
getAssembler().getOrCreateSymbolData(*Symbol);
if (SD.isExternal())
EmitSymbolAttribute(EHSymbol, MCSA_Global);
if (SD.getFlags() & SF_WeakDefinition)
EmitSymbolAttribute(EHSymbol, MCSA_WeakDefinition);
if (SD.isPrivateExtern())
EmitSymbolAttribute(EHSymbol, MCSA_PrivateExtern);
}
void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
// isSymbolLinkerVisible uses the section.
AssignSection(Symbol, getCurrentSection().first);
// We have to create a new fragment if this is an atom defining symbol,
// fragments cannot span atoms.
if (getAssembler().isSymbolLinkerVisible(*Symbol))
insert(new MCDataFragment());
MCObjectStreamer::EmitLabel(Symbol);
MCSymbolData &SD = getAssembler().getSymbolData(*Symbol);
// This causes the reference type flag to be cleared. Darwin 'as' was "trying"
// to clear the weak reference and weak definition bits too, but the
// implementation was buggy. For now we just try to match 'as', for
// diffability.
//
// FIXME: Cleanup this code, these bits should be emitted based on semantic
// properties, not on the order of definition, etc.
SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeMask);
}
void MCMachOStreamer::EmitDebugLabel(MCSymbol *Symbol) {
EmitLabel(Symbol);
}
void MCMachOStreamer::EmitDataRegion(DataRegionData::KindTy Kind) {
if (!getAssembler().getBackend().hasDataInCodeSupport())
return;
// Create a temporary label to mark the start of the data region.
MCSymbol *Start = getContext().CreateTempSymbol();
EmitLabel(Start);
// Record the region for the object writer to use.
DataRegionData Data = { Kind, Start, NULL };
std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
Regions.push_back(Data);
}
void MCMachOStreamer::EmitDataRegionEnd() {
if (!getAssembler().getBackend().hasDataInCodeSupport())
return;
std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
assert(Regions.size() && "Mismatched .end_data_region!");
DataRegionData &Data = Regions.back();
assert(Data.End == NULL && "Mismatched .end_data_region!");
// Create a temporary label to mark the end of the data region.
Data.End = getContext().CreateTempSymbol();
EmitLabel(Data.End);
}
void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
// Let the target do whatever target specific stuff it needs to do.
getAssembler().getBackend().handleAssemblerFlag(Flag);
// Do any generic stuff we need to do.
switch (Flag) {
case MCAF_SyntaxUnified: return; // no-op here.
case MCAF_Code16: return; // Change parsing mode; no-op here.
case MCAF_Code32: return; // Change parsing mode; no-op here.
case MCAF_Code64: return; // Change parsing mode; no-op here.
case MCAF_SubsectionsViaSymbols:
getAssembler().setSubsectionsViaSymbols(true);
return;
}
}
void MCMachOStreamer::EmitLinkerOptions(ArrayRef<std::string> Options) {
getAssembler().getLinkerOptions().push_back(Options);
}
void MCMachOStreamer::EmitDataRegion(MCDataRegionType Kind) {
switch (Kind) {
case MCDR_DataRegion:
EmitDataRegion(DataRegionData::Data);
return;
case MCDR_DataRegionJT8:
EmitDataRegion(DataRegionData::JumpTable8);
return;
case MCDR_DataRegionJT16:
EmitDataRegion(DataRegionData::JumpTable16);
return;
case MCDR_DataRegionJT32:
EmitDataRegion(DataRegionData::JumpTable32);
return;
case MCDR_DataRegionEnd:
EmitDataRegionEnd();
return;
}
}
+void MCMachOStreamer::EmitVersionMin(MCVersionMinType Kind, unsigned Major,
+ unsigned Minor, unsigned Update) {
+ getAssembler().setVersionMinInfo(Kind, Major, Minor, Update);
+}
+
void MCMachOStreamer::EmitThumbFunc(MCSymbol *Symbol) {
// Remember that the function is a thumb function. Fixup and relocation
// values will need adjusted.
getAssembler().setIsThumbFunc(Symbol);
// Mark the thumb bit on the symbol.
MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
SD.setFlags(SD.getFlags() | SF_ThumbFunc);
}
bool MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
MCSymbolAttr Attribute) {
// Indirect symbols are handled differently, to match how 'as' handles
// them. This makes writing matching .o files easier.
if (Attribute == MCSA_IndirectSymbol) {
// Note that we intentionally cannot use the symbol data here; this is
// important for matching the string table that 'as' generates.
IndirectSymbolData ISD;
ISD.Symbol = Symbol;
ISD.SectionData = getCurrentSectionData();
getAssembler().getIndirectSymbols().push_back(ISD);
return true;
}
// Adding a symbol attribute always introduces the symbol, note that an
// important side effect of calling getOrCreateSymbolData here is to register
// the symbol with the assembler.
MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
// The implementation of symbol attributes is designed to match 'as', but it
// leaves much to desired. It doesn't really make sense to arbitrarily add and
// remove flags, but 'as' allows this (in particular, see .desc).
//
// In the future it might be worth trying to make these operations more well
// defined.
switch (Attribute) {
case MCSA_Invalid:
case MCSA_ELF_TypeFunction:
case MCSA_ELF_TypeIndFunction:
case MCSA_ELF_TypeObject:
case MCSA_ELF_TypeTLS:
case MCSA_ELF_TypeCommon:
case MCSA_ELF_TypeNoType:
case MCSA_ELF_TypeGnuUniqueObject:
case MCSA_Hidden:
case MCSA_IndirectSymbol:
case MCSA_Internal:
case MCSA_Protected:
case MCSA_Weak:
case MCSA_Local:
return false;
case MCSA_Global:
SD.setExternal(true);
// This effectively clears the undefined lazy bit, in Darwin 'as', although
// it isn't very consistent because it implements this as part of symbol
// lookup.
//
// FIXME: Cleanup this code, these bits should be emitted based on semantic
// properties, not on the order of definition, etc.
SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeUndefinedLazy);
break;
case MCSA_LazyReference:
// FIXME: This requires -dynamic.
SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
if (Symbol->isUndefined())
SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
break;
// Since .reference sets the no dead strip bit, it is equivalent to
// .no_dead_strip in practice.
case MCSA_Reference:
case MCSA_NoDeadStrip:
SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
break;
case MCSA_SymbolResolver:
SD.setFlags(SD.getFlags() | SF_SymbolResolver);
break;
case MCSA_PrivateExtern:
SD.setExternal(true);
SD.setPrivateExtern(true);
break;
case MCSA_WeakReference:
// FIXME: This requires -dynamic.
if (Symbol->isUndefined())
SD.setFlags(SD.getFlags() | SF_WeakReference);
break;
case MCSA_WeakDefinition:
// FIXME: 'as' enforces that this is defined and global. The manual claims
// it has to be in a coalesced section, but this isn't enforced.
SD.setFlags(SD.getFlags() | SF_WeakDefinition);
break;
case MCSA_WeakDefAutoPrivate:
SD.setFlags(SD.getFlags() | SF_WeakDefinition | SF_WeakReference);
break;
}
return true;
}
void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
// Encode the 'desc' value into the lowest implementation defined bits.
assert(DescValue == (DescValue & SF_DescFlagsMask) &&
"Invalid .desc value!");
getAssembler().getOrCreateSymbolData(*Symbol).setFlags(
DescValue & SF_DescFlagsMask);
}
void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
unsigned ByteAlignment) {
// FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
AssignSection(Symbol, NULL);
MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
SD.setExternal(true);
SD.setCommon(Size, ByteAlignment);
}
void MCMachOStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
unsigned ByteAlignment) {
// '.lcomm' is equivalent to '.zerofill'.
return EmitZerofill(getContext().getObjectFileInfo()->getDataBSSSection(),
Symbol, Size, ByteAlignment);
}
void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
uint64_t Size, unsigned ByteAlignment) {
MCSectionData &SectData = getAssembler().getOrCreateSectionData(*Section);
// The symbol may not be present, which only creates the section.
if (!Symbol)
return;
// On darwin all virtual sections have zerofill type.
assert(Section->isVirtualSection() && "Section does not have zerofill type!");
assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
// Emit an align fragment if necessary.
if (ByteAlignment != 1)
new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectData);
MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
SD.setFragment(F);
AssignSection(Symbol, Section);
// Update the maximum alignment on the zero fill section if necessary.
if (ByteAlignment > SectData.getAlignment())
SectData.setAlignment(ByteAlignment);
}
// This should always be called with the thread local bss section. Like the
// .zerofill directive this doesn't actually switch sections on us.
void MCMachOStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
uint64_t Size, unsigned ByteAlignment) {
EmitZerofill(Section, Symbol, Size, ByteAlignment);
return;
}
void MCMachOStreamer::EmitInstToData(const MCInst &Inst,
const MCSubtargetInfo &STI) {
MCDataFragment *DF = getOrCreateDataFragment();
SmallVector<MCFixup, 4> Fixups;
SmallString<256> Code;
raw_svector_ostream VecOS(Code);
getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups, STI);
VecOS.flush();
// Add the fixups and data.
for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
DF->getFixups().push_back(Fixups[i]);
}
DF->getContents().append(Code.begin(), Code.end());
}
void MCMachOStreamer::FinishImpl() {
EmitFrames(&getAssembler().getBackend(), true);
// We have to set the fragment atom associations so we can relax properly for
// Mach-O.
// First, scan the symbol table to build a lookup table from fragments to
// defining symbols.
DenseMap<const MCFragment*, MCSymbolData*> DefiningSymbolMap;
for (MCAssembler::symbol_iterator it = getAssembler().symbol_begin(),
ie = getAssembler().symbol_end(); it != ie; ++it) {
if (getAssembler().isSymbolLinkerVisible(it->getSymbol()) &&
it->getFragment()) {
// An atom defining symbol should never be internal to a fragment.
assert(it->getOffset() == 0 && "Invalid offset in atom defining symbol!");
DefiningSymbolMap[it->getFragment()] = it;