Split MCEELFStreamer and ELFObjectWriter into .h and .cpp files, so that other components can use them.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@126942 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Jan Sjödin 2011-03-03 14:52:12 +00:00
parent ea2578c63f
commit 24b17c6730
4 changed files with 662 additions and 480 deletions

View File

@ -11,21 +11,14 @@
//
//===----------------------------------------------------------------------===//
#include "MCELF.h"
#include "llvm/ADT/OwningPtr.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "ELFObjectWriter.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/Twine.h"
#include "llvm/MC/MCAssembler.h"
#include "llvm/MC/MCAsmLayout.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCELFSymbolFlags.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCELFObjectWriter.h"
#include "llvm/MC/MCObjectWriter.h"
#include "llvm/MC/MCSectionELF.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/MC/MCValue.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
@ -39,361 +32,6 @@
#include <vector>
using namespace llvm;
namespace {
class ELFObjectWriter : public MCObjectWriter {
protected:
static bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind);
static bool RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant);
static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout);
static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
bool Used, bool Renamed);
static bool isLocal(const MCSymbolData &Data, bool isSignature,
bool isUsedInReloc);
static bool IsELFMetaDataSection(const MCSectionData &SD);
static uint64_t DataSectionSize(const MCSectionData &SD);
static uint64_t GetSectionFileSize(const MCAsmLayout &Layout,
const MCSectionData &SD);
static uint64_t GetSectionAddressSize(const MCAsmLayout &Layout,
const MCSectionData &SD);
static void WriteDataSectionData(ELFObjectWriter *W,
const MCSectionData &SD);
/*static bool isFixupKindX86RIPRel(unsigned Kind) {
return Kind == X86::reloc_riprel_4byte ||
Kind == X86::reloc_riprel_4byte_movq_load;
}*/
/// ELFSymbolData - Helper struct for containing some precomputed
/// information on symbols.
struct ELFSymbolData {
MCSymbolData *SymbolData;
uint64_t StringIndex;
uint32_t SectionIndex;
// Support lexicographic sorting.
bool operator<(const ELFSymbolData &RHS) const {
if (MCELF::GetType(*SymbolData) == ELF::STT_FILE)
return true;
if (MCELF::GetType(*RHS.SymbolData) == ELF::STT_FILE)
return false;
return SymbolData->getSymbol().getName() <
RHS.SymbolData->getSymbol().getName();
}
};
/// @name Relocation Data
/// @{
struct ELFRelocationEntry {
// Make these big enough for both 32-bit and 64-bit
uint64_t r_offset;
int Index;
unsigned Type;
const MCSymbol *Symbol;
uint64_t r_addend;
ELFRelocationEntry()
: r_offset(0), Index(0), Type(0), Symbol(0), r_addend(0) {}
ELFRelocationEntry(uint64_t RelocOffset, int Idx,
unsigned RelType, const MCSymbol *Sym,
uint64_t Addend)
: r_offset(RelocOffset), Index(Idx), Type(RelType),
Symbol(Sym), r_addend(Addend) {}
// Support lexicographic sorting.
bool operator<(const ELFRelocationEntry &RE) const {
return RE.r_offset < r_offset;
}
};
/// The target specific ELF writer instance.
llvm::OwningPtr<MCELFObjectTargetWriter> TargetObjectWriter;
SmallPtrSet<const MCSymbol *, 16> UsedInReloc;
SmallPtrSet<const MCSymbol *, 16> WeakrefUsedInReloc;
DenseMap<const MCSymbol *, const MCSymbol *> Renames;
llvm::DenseMap<const MCSectionData*,
std::vector<ELFRelocationEntry> > Relocations;
DenseMap<const MCSection*, uint64_t> SectionStringTableIndex;
/// @}
/// @name Symbol Table Data
/// @{
SmallString<256> StringTable;
std::vector<ELFSymbolData> LocalSymbolData;
std::vector<ELFSymbolData> ExternalSymbolData;
std::vector<ELFSymbolData> UndefinedSymbolData;
/// @}
bool NeedsGOT;
bool NeedsSymtabShndx;
// This holds the symbol table index of the last local symbol.
unsigned LastLocalSymbolIndex;
// This holds the .strtab section index.
unsigned StringTableIndex;
// This holds the .symtab section index.
unsigned SymbolTableIndex;
unsigned ShstrtabIndex;
const MCSymbol *SymbolToReloc(const MCAssembler &Asm,
const MCValue &Target,
const MCFragment &F) const;
// For arch-specific emission of explicit reloc symbol
virtual const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
const MCValue &Target,
const MCFragment &F,
bool IsBSS) const {
return NULL;
}
bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
bool hasRelocationAddend() const {
return TargetObjectWriter->hasRelocationAddend();
}
public:
ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
raw_ostream &_OS, bool IsLittleEndian)
: MCObjectWriter(_OS, IsLittleEndian),
TargetObjectWriter(MOTW),
NeedsGOT(false), NeedsSymtabShndx(false){
}
virtual ~ELFObjectWriter();
void WriteWord(uint64_t W) {
if (is64Bit())
Write64(W);
else
Write32(W);
}
void StringLE16(char *buf, uint16_t Value) {
buf[0] = char(Value >> 0);
buf[1] = char(Value >> 8);
}
void StringLE32(char *buf, uint32_t Value) {
StringLE16(buf, uint16_t(Value >> 0));
StringLE16(buf + 2, uint16_t(Value >> 16));
}
void StringLE64(char *buf, uint64_t Value) {
StringLE32(buf, uint32_t(Value >> 0));
StringLE32(buf + 4, uint32_t(Value >> 32));
}
void StringBE16(char *buf ,uint16_t Value) {
buf[0] = char(Value >> 8);
buf[1] = char(Value >> 0);
}
void StringBE32(char *buf, uint32_t Value) {
StringBE16(buf, uint16_t(Value >> 16));
StringBE16(buf + 2, uint16_t(Value >> 0));
}
void StringBE64(char *buf, uint64_t Value) {
StringBE32(buf, uint32_t(Value >> 32));
StringBE32(buf + 4, uint32_t(Value >> 0));
}
void String8(MCDataFragment &F, uint8_t Value) {
char buf[1];
buf[0] = Value;
F.getContents() += StringRef(buf, 1);
}
void String16(MCDataFragment &F, uint16_t Value) {
char buf[2];
if (isLittleEndian())
StringLE16(buf, Value);
else
StringBE16(buf, Value);
F.getContents() += StringRef(buf, 2);
}
void String32(MCDataFragment &F, uint32_t Value) {
char buf[4];
if (isLittleEndian())
StringLE32(buf, Value);
else
StringBE32(buf, Value);
F.getContents() += StringRef(buf, 4);
}
void String64(MCDataFragment &F, uint64_t Value) {
char buf[8];
if (isLittleEndian())
StringLE64(buf, Value);
else
StringBE64(buf, Value);
F.getContents() += StringRef(buf, 8);
}
virtual void WriteHeader(uint64_t SectionDataSize, unsigned NumberOfSections);
/// Default e_flags = 0
virtual void WriteEFlags() { Write32(0); }
virtual void WriteSymbolEntry(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
uint64_t name, uint8_t info,
uint64_t value, uint64_t size,
uint8_t other, uint32_t shndx,
bool Reserved);
virtual void WriteSymbol(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
ELFSymbolData &MSD,
const MCAsmLayout &Layout);
typedef DenseMap<const MCSectionELF*, uint32_t> SectionIndexMapTy;
virtual void WriteSymbolTable(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
const MCAssembler &Asm,
const MCAsmLayout &Layout,
const SectionIndexMapTy &SectionIndexMap);
virtual void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
const MCFragment *Fragment, const MCFixup &Fixup,
MCValue Target, uint64_t &FixedValue);
virtual uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
const MCSymbol *S);
// Map from a group section to the signature symbol
typedef DenseMap<const MCSectionELF*, const MCSymbol*> GroupMapTy;
// Map from a signature symbol to the group section
typedef DenseMap<const MCSymbol*, const MCSectionELF*> RevGroupMapTy;
/// ComputeSymbolTable - Compute the symbol table data
///
/// \param StringTable [out] - The string table data.
/// \param StringIndexMap [out] - Map from symbol names to offsets in the
/// string table.
virtual void ComputeSymbolTable(MCAssembler &Asm,
const SectionIndexMapTy &SectionIndexMap,
RevGroupMapTy RevGroupMap);
virtual void ComputeIndexMap(MCAssembler &Asm,
SectionIndexMapTy &SectionIndexMap);
virtual void WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
const MCSectionData &SD);
virtual void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout) {
for (MCAssembler::const_iterator it = Asm.begin(),
ie = Asm.end(); it != ie; ++it) {
WriteRelocation(Asm, Layout, *it);
}
}
virtual void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout,
const SectionIndexMapTy &SectionIndexMap);
// Create the sections that show up in the symbol table. Currently
// those are the .note.GNU-stack section and the group sections.
virtual void CreateIndexedSections(MCAssembler &Asm, MCAsmLayout &Layout,
GroupMapTy &GroupMap,
RevGroupMapTy &RevGroupMap);
virtual void ExecutePostLayoutBinding(MCAssembler &Asm,
const MCAsmLayout &Layout);
virtual void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
uint64_t Address, uint64_t Offset,
uint64_t Size, uint32_t Link, uint32_t Info,
uint64_t Alignment, uint64_t EntrySize);
virtual void WriteRelocationsFragment(const MCAssembler &Asm,
MCDataFragment *F,
const MCSectionData *SD);
virtual bool
IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
const MCSymbolData &DataA,
const MCFragment &FB,
bool InSet,
bool IsPCRel) const;
virtual void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
virtual void WriteSection(MCAssembler &Asm,
const SectionIndexMapTy &SectionIndexMap,
uint32_t GroupSymbolIndex,
uint64_t Offset, uint64_t Size, uint64_t Alignment,
const MCSectionELF &Section);
protected:
virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
bool IsPCRel, bool IsRelocWithSymbol,
int64_t Addend) = 0;
};
//===- X86ELFObjectWriter -------------------------------------------===//
class X86ELFObjectWriter : public ELFObjectWriter {
public:
X86ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
raw_ostream &_OS,
bool IsLittleEndian);
virtual ~X86ELFObjectWriter();
protected:
virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
bool IsPCRel, bool IsRelocWithSymbol,
int64_t Addend);
};
//===- ARMELFObjectWriter -------------------------------------------===//
class ARMELFObjectWriter : public ELFObjectWriter {
public:
// FIXME: MCAssembler can't yet return the Subtarget,
enum { DefaultEABIVersion = 0x05000000U };
ARMELFObjectWriter(MCELFObjectTargetWriter *MOTW,
raw_ostream &_OS,
bool IsLittleEndian);
virtual ~ARMELFObjectWriter();
virtual void WriteEFlags();
protected:
virtual const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
const MCValue &Target,
const MCFragment &F,
bool IsBSS) const;
virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
bool IsPCRel, bool IsRelocWithSymbol,
int64_t Addend);
};
//===- MBlazeELFObjectWriter -------------------------------------------===//
class MBlazeELFObjectWriter : public ELFObjectWriter {
public:
MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
raw_ostream &_OS,
bool IsLittleEndian);
virtual ~MBlazeELFObjectWriter();
protected:
virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
bool IsPCRel, bool IsRelocWithSymbol,
int64_t Addend);
};
}
bool ELFObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) {
const MCFixupKindInfo &FKI =
Asm.getBackend().getFixupKindInfo((MCFixupKind) Kind);

391
lib/MC/ELFObjectWriter.h Normal file
View File

@ -0,0 +1,391 @@
//===- lib/MC/ELFObjectWriter.h - ELF File Writer -------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements ELF object file writer information.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_MC_ELFOBJECTWRITER_H
#define LLVM_MC_ELFOBJECTWRITER_H
#include "MCELF.h"
#include "llvm/ADT/OwningPtr.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/MC/MCAssembler.h"
#include "llvm/MC/MCELFObjectWriter.h"
#include "llvm/MC/MCELFSymbolFlags.h"
#include "llvm/MC/MCObjectWriter.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCSymbol.h"
#include <vector>
namespace llvm {
class MCSection;
class MCDataFragment;
class MCSectionELF;
class ELFObjectWriter : public MCObjectWriter {
protected:
static bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind);
static bool RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant);
static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout);
static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
bool Used, bool Renamed);
static bool isLocal(const MCSymbolData &Data, bool isSignature,
bool isUsedInReloc);
static bool IsELFMetaDataSection(const MCSectionData &SD);
static uint64_t DataSectionSize(const MCSectionData &SD);
static uint64_t GetSectionFileSize(const MCAsmLayout &Layout,
const MCSectionData &SD);
static uint64_t GetSectionAddressSize(const MCAsmLayout &Layout,
const MCSectionData &SD);
static void WriteDataSectionData(ELFObjectWriter *W,
const MCSectionData &SD);
/*static bool isFixupKindX86RIPRel(unsigned Kind) {
return Kind == X86::reloc_riprel_4byte ||
Kind == X86::reloc_riprel_4byte_movq_load;
}*/
/// ELFSymbolData - Helper struct for containing some precomputed
/// information on symbols.
struct ELFSymbolData {
MCSymbolData *SymbolData;
uint64_t StringIndex;
uint32_t SectionIndex;
// Support lexicographic sorting.
bool operator<(const ELFSymbolData &RHS) const {
if (MCELF::GetType(*SymbolData) == ELF::STT_FILE)
return true;
if (MCELF::GetType(*RHS.SymbolData) == ELF::STT_FILE)
return false;
return SymbolData->getSymbol().getName() <
RHS.SymbolData->getSymbol().getName();
}
};
/// @name Relocation Data
/// @{
struct ELFRelocationEntry {
// Make these big enough for both 32-bit and 64-bit
uint64_t r_offset;
int Index;
unsigned Type;
const MCSymbol *Symbol;
uint64_t r_addend;
ELFRelocationEntry()
: r_offset(0), Index(0), Type(0), Symbol(0), r_addend(0) {}
ELFRelocationEntry(uint64_t RelocOffset, int Idx,
unsigned RelType, const MCSymbol *Sym,
uint64_t Addend)
: r_offset(RelocOffset), Index(Idx), Type(RelType),
Symbol(Sym), r_addend(Addend) {}
// Support lexicographic sorting.
bool operator<(const ELFRelocationEntry &RE) const {
return RE.r_offset < r_offset;
}
};
/// The target specific ELF writer instance.
llvm::OwningPtr<MCELFObjectTargetWriter> TargetObjectWriter;
SmallPtrSet<const MCSymbol *, 16> UsedInReloc;
SmallPtrSet<const MCSymbol *, 16> WeakrefUsedInReloc;
DenseMap<const MCSymbol *, const MCSymbol *> Renames;
llvm::DenseMap<const MCSectionData*,
std::vector<ELFRelocationEntry> > Relocations;
DenseMap<const MCSection*, uint64_t> SectionStringTableIndex;
/// @}
/// @name Symbol Table Data
/// @{
SmallString<256> StringTable;
std::vector<ELFSymbolData> LocalSymbolData;
std::vector<ELFSymbolData> ExternalSymbolData;
std::vector<ELFSymbolData> UndefinedSymbolData;
/// @}
bool NeedsGOT;
bool NeedsSymtabShndx;
// This holds the symbol table index of the last local symbol.
unsigned LastLocalSymbolIndex;
// This holds the .strtab section index.
unsigned StringTableIndex;
// This holds the .symtab section index.
unsigned SymbolTableIndex;
unsigned ShstrtabIndex;
const MCSymbol *SymbolToReloc(const MCAssembler &Asm,
const MCValue &Target,
const MCFragment &F) const;
// For arch-specific emission of explicit reloc symbol
virtual const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
const MCValue &Target,
const MCFragment &F,
bool IsBSS) const {
return NULL;
}
bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
bool hasRelocationAddend() const {
return TargetObjectWriter->hasRelocationAddend();
}
public:
ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
raw_ostream &_OS, bool IsLittleEndian)
: MCObjectWriter(_OS, IsLittleEndian),
TargetObjectWriter(MOTW),
NeedsGOT(false), NeedsSymtabShndx(false){
}
virtual ~ELFObjectWriter();
void WriteWord(uint64_t W) {
if (is64Bit())
Write64(W);
else
Write32(W);
}
void StringLE16(char *buf, uint16_t Value) {
buf[0] = char(Value >> 0);
buf[1] = char(Value >> 8);
}
void StringLE32(char *buf, uint32_t Value) {
StringLE16(buf, uint16_t(Value >> 0));
StringLE16(buf + 2, uint16_t(Value >> 16));
}
void StringLE64(char *buf, uint64_t Value) {
StringLE32(buf, uint32_t(Value >> 0));
StringLE32(buf + 4, uint32_t(Value >> 32));
}
void StringBE16(char *buf ,uint16_t Value) {
buf[0] = char(Value >> 8);
buf[1] = char(Value >> 0);
}
void StringBE32(char *buf, uint32_t Value) {
StringBE16(buf, uint16_t(Value >> 16));
StringBE16(buf + 2, uint16_t(Value >> 0));
}
void StringBE64(char *buf, uint64_t Value) {
StringBE32(buf, uint32_t(Value >> 32));
StringBE32(buf + 4, uint32_t(Value >> 0));
}
void String8(MCDataFragment &F, uint8_t Value) {
char buf[1];
buf[0] = Value;
F.getContents() += StringRef(buf, 1);
}
void String16(MCDataFragment &F, uint16_t Value) {
char buf[2];
if (isLittleEndian())
StringLE16(buf, Value);
else
StringBE16(buf, Value);
F.getContents() += StringRef(buf, 2);
}
void String32(MCDataFragment &F, uint32_t Value) {
char buf[4];
if (isLittleEndian())
StringLE32(buf, Value);
else
StringBE32(buf, Value);
F.getContents() += StringRef(buf, 4);
}
void String64(MCDataFragment &F, uint64_t Value) {
char buf[8];
if (isLittleEndian())
StringLE64(buf, Value);
else
StringBE64(buf, Value);
F.getContents() += StringRef(buf, 8);
}
virtual void WriteHeader(uint64_t SectionDataSize, unsigned NumberOfSections);
/// Default e_flags = 0
virtual void WriteEFlags() { Write32(0); }
virtual void WriteSymbolEntry(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
uint64_t name, uint8_t info,
uint64_t value, uint64_t size,
uint8_t other, uint32_t shndx,
bool Reserved);
virtual void WriteSymbol(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
ELFSymbolData &MSD,
const MCAsmLayout &Layout);
typedef DenseMap<const MCSectionELF*, uint32_t> SectionIndexMapTy;
virtual void WriteSymbolTable(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
const MCAssembler &Asm,
const MCAsmLayout &Layout,
const SectionIndexMapTy &SectionIndexMap);
virtual void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
const MCFragment *Fragment, const MCFixup &Fixup,
MCValue Target, uint64_t &FixedValue);
virtual uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
const MCSymbol *S);
// Map from a group section to the signature symbol
typedef DenseMap<const MCSectionELF*, const MCSymbol*> GroupMapTy;
// Map from a signature symbol to the group section
typedef DenseMap<const MCSymbol*, const MCSectionELF*> RevGroupMapTy;
/// ComputeSymbolTable - Compute the symbol table data
///
/// \param StringTable [out] - The string table data.
/// \param StringIndexMap [out] - Map from symbol names to offsets in the
/// string table.
virtual void ComputeSymbolTable(MCAssembler &Asm,
const SectionIndexMapTy &SectionIndexMap,
RevGroupMapTy RevGroupMap);
virtual void ComputeIndexMap(MCAssembler &Asm,
SectionIndexMapTy &SectionIndexMap);
virtual void WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
const MCSectionData &SD);
virtual void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout) {
for (MCAssembler::const_iterator it = Asm.begin(),
ie = Asm.end(); it != ie; ++it) {
WriteRelocation(Asm, Layout, *it);
}
}
virtual void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout,
const SectionIndexMapTy &SectionIndexMap);
// Create the sections that show up in the symbol table. Currently
// those are the .note.GNU-stack section and the group sections.
virtual void CreateIndexedSections(MCAssembler &Asm, MCAsmLayout &Layout,
GroupMapTy &GroupMap,
RevGroupMapTy &RevGroupMap);
virtual void ExecutePostLayoutBinding(MCAssembler &Asm,
const MCAsmLayout &Layout);
virtual void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
uint64_t Address, uint64_t Offset,
uint64_t Size, uint32_t Link, uint32_t Info,
uint64_t Alignment, uint64_t EntrySize);
virtual void WriteRelocationsFragment(const MCAssembler &Asm,
MCDataFragment *F,
const MCSectionData *SD);
virtual bool
IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
const MCSymbolData &DataA,
const MCFragment &FB,
bool InSet,
bool IsPCRel) const;
virtual void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
virtual void WriteSection(MCAssembler &Asm,
const SectionIndexMapTy &SectionIndexMap,
uint32_t GroupSymbolIndex,
uint64_t Offset, uint64_t Size, uint64_t Alignment,
const MCSectionELF &Section);
protected:
virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
bool IsPCRel, bool IsRelocWithSymbol,
int64_t Addend) = 0;
};
//===- X86ELFObjectWriter -------------------------------------------===//
class X86ELFObjectWriter : public ELFObjectWriter {
public:
X86ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
raw_ostream &_OS,
bool IsLittleEndian);
virtual ~X86ELFObjectWriter();
protected:
virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
bool IsPCRel, bool IsRelocWithSymbol,
int64_t Addend);
};
//===- ARMELFObjectWriter -------------------------------------------===//
class ARMELFObjectWriter : public ELFObjectWriter {
public:
// FIXME: MCAssembler can't yet return the Subtarget,
enum { DefaultEABIVersion = 0x05000000U };
ARMELFObjectWriter(MCELFObjectTargetWriter *MOTW,
raw_ostream &_OS,
bool IsLittleEndian);
virtual ~ARMELFObjectWriter();
virtual void WriteEFlags();
protected:
virtual const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
const MCValue &Target,
const MCFragment &F,
bool IsBSS) const;
virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
bool IsPCRel, bool IsRelocWithSymbol,
int64_t Addend);
};
//===- MBlazeELFObjectWriter -------------------------------------------===//
class MBlazeELFObjectWriter : public ELFObjectWriter {
public:
MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
raw_ostream &_OS,
bool IsLittleEndian);
virtual ~MBlazeELFObjectWriter();
protected:
virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
bool IsPCRel, bool IsRelocWithSymbol,
int64_t Addend);
};
}
#endif

View File

@ -11,19 +11,14 @@
//
//===----------------------------------------------------------------------===//
#include "llvm/MC/MCStreamer.h"
#include "MCELFStreamer.h"
#include "MCELF.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/MC/MCAssembler.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/MC/MCCodeEmitter.h"
#include "llvm/MC/MCELFSymbolFlags.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCInst.h"
#include "llvm/MC/MCObjectStreamer.h"
#include "llvm/MC/MCSection.h"
#include "llvm/MC/MCSectionELF.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/MC/MCValue.h"
#include "llvm/Support/Debug.h"
@ -35,116 +30,6 @@
using namespace llvm;
namespace {
class MCELFStreamer : public MCObjectStreamer {
public:
MCELFStreamer(MCContext &Context, TargetAsmBackend &TAB,
raw_ostream &OS, MCCodeEmitter *Emitter)
: MCObjectStreamer(Context, TAB, OS, Emitter) {}
~MCELFStreamer() {}
/// @name MCStreamer Interface
/// @{
virtual void InitSections();
virtual void ChangeSection(const MCSection *Section);
virtual void EmitLabel(MCSymbol *Symbol);
virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
virtual void EmitThumbFunc(MCSymbol *Func);
virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
assert(0 && "ELF doesn't support this directive");
}
virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
unsigned ByteAlignment);
virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
assert(0 && "ELF doesn't support this directive");
}
virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
assert(0 && "ELF doesn't support this directive");
}
virtual void EmitCOFFSymbolType(int Type) {
assert(0 && "ELF doesn't support this directive");
}
virtual void EndCOFFSymbolDef() {
assert(0 && "ELF doesn't support this directive");
}
virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
SD.setSize(Value);
}
virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
unsigned Size = 0, unsigned ByteAlignment = 0) {
assert(0 && "ELF doesn't support this directive");
}
virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
uint64_t Size, unsigned ByteAlignment = 0) {
assert(0 && "ELF doesn't support this directive");
}
virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
unsigned ValueSize = 1,
unsigned MaxBytesToEmit = 0);
virtual void EmitCodeAlignment(unsigned ByteAlignment,
unsigned MaxBytesToEmit = 0);
virtual void EmitFileDirective(StringRef Filename);
virtual void Finish();
private:
virtual void EmitInstToFragment(const MCInst &Inst);
virtual void EmitInstToData(const MCInst &Inst);
void fixSymbolsInTLSFixups(const MCExpr *expr);
struct LocalCommon {
MCSymbolData *SD;
uint64_t Size;
unsigned ByteAlignment;
};
std::vector<LocalCommon> LocalCommons;
SmallPtrSet<MCSymbol *, 16> BindingExplicitlySet;
/// @}
void SetSection(StringRef Section, unsigned Type, unsigned Flags,
SectionKind Kind) {
SwitchSection(getContext().getELFSection(Section, Type, Flags, Kind));
}
void SetSectionData() {
SetSection(".data", ELF::SHT_PROGBITS,
ELF::SHF_WRITE |ELF::SHF_ALLOC,
SectionKind::getDataRel());
EmitCodeAlignment(4, 0);
}
void SetSectionText() {
SetSection(".text", ELF::SHT_PROGBITS,
ELF::SHF_EXECINSTR |
ELF::SHF_ALLOC, SectionKind::getText());
EmitCodeAlignment(4, 0);
}
void SetSectionBss() {
SetSection(".bss", ELF::SHT_NOBITS,
ELF::SHF_WRITE |
ELF::SHF_ALLOC, SectionKind::getBSS());
EmitCodeAlignment(4, 0);
}
};
} // end anonymous namespace.
void MCELFStreamer::InitSections() {
// This emulates the same behavior of GNU as. This makes it easier
// to compare the output as the major sections are in the same order.

268
lib/MC/MCELFStreamer.h Normal file
View File

@ -0,0 +1,268 @@
//===- lib/MC/MCELFStreamer.h - ELF Object Output -------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file assembles .s files and emits ELF .o object files.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_MC_MCELFSTREAMER_H
#define LLVM_MC_MCELFSTREAMER_H
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/MC/MCAssembler.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCObjectStreamer.h"
#include "llvm/MC/MCSectionELF.h"
namespace llvm {
class MCELFStreamer : public MCObjectStreamer {
public:
MCELFStreamer(MCContext &Context, TargetAsmBackend &TAB,
raw_ostream &OS, MCCodeEmitter *Emitter)
: MCObjectStreamer(Context, TAB, OS, Emitter) {}
~MCELFStreamer() {}
/// @name MCStreamer Interface
/// @{
virtual void InitSections();
virtual void ChangeSection(const MCSection *Section);
virtual void EmitLabel(MCSymbol *Symbol);
virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
virtual void EmitThumbFunc(MCSymbol *Func);
virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
assert(0 && "ELF doesn't support this directive");
}
virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
unsigned ByteAlignment);
virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
assert(0 && "ELF doesn't support this directive");
}
virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
assert(0 && "ELF doesn't support this directive");
}
virtual void EmitCOFFSymbolType(int Type) {
assert(0 && "ELF doesn't support this directive");
}
virtual void EndCOFFSymbolDef() {
assert(0 && "ELF doesn't support this directive");
}
virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
SD.setSize(Value);
}
virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
unsigned Size = 0, unsigned ByteAlignment = 0) {
assert(0 && "ELF doesn't support this directive");
}
virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
uint64_t Size, unsigned ByteAlignment = 0) {
assert(0 && "ELF doesn't support this directive");
}
virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
unsigned ValueSize = 1,
unsigned MaxBytesToEmit = 0);
virtual void EmitCodeAlignment(unsigned ByteAlignment,
unsigned MaxBytesToEmit = 0);
virtual void EmitFileDirective(StringRef Filename);
virtual void Finish();
private:
virtual void EmitInstToFragment(const MCInst &Inst);
virtual void EmitInstToData(const MCInst &Inst);
void fixSymbolsInTLSFixups(const MCExpr *expr);
struct LocalCommon {
MCSymbolData *SD;
uint64_t Size;
unsigned ByteAlignment;
};
std::vector<LocalCommon> LocalCommons;
SmallPtrSet<MCSymbol *, 16> BindingExplicitlySet;
/// @}
void SetSection(StringRef Section, unsigned Type, unsigned Flags,
SectionKind Kind) {
SwitchSection(getContext().getELFSection(Section, Type, Flags, Kind));
}
void SetSectionData() {
SetSection(".data", ELF::SHT_PROGBITS,
ELF::SHF_WRITE |ELF::SHF_ALLOC,
SectionKind::getDataRel());
EmitCodeAlignment(4, 0);
}
void SetSectionText() {
SetSection(".text", ELF::SHT_PROGBITS,
ELF::SHF_EXECINSTR |
ELF::SHF_ALLOC, SectionKind::getText());
EmitCodeAlignment(4, 0);
}
void SetSectionBss() {
SetSection(".bss", ELF::SHT_NOBITS,
ELF::SHF_WRITE |
ELF::SHF_ALLOC, SectionKind::getBSS());
EmitCodeAlignment(4, 0);
}
};
} // end llvm namespace
#endif
//===- lib/MC/MCELFStreamer.h - ELF Object Output -------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file assembles .s files and emits ELF .o object files.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_MC_MCELFSTREAMER_H
#define LLVM_MC_MCELFSTREAMER_H
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/MC/MCAssembler.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCObjectStreamer.h"
#include "llvm/MC/MCSectionELF.h"
namespace llvm {
class MCELFStreamer : public MCObjectStreamer {
public:
MCELFStreamer(MCContext &Context, TargetAsmBackend &TAB,
raw_ostream &OS, MCCodeEmitter *Emitter)
: MCObjectStreamer(Context, TAB, OS, Emitter) {}
~MCELFStreamer() {}
/// @name MCStreamer Interface
/// @{
virtual void InitSections();
virtual void ChangeSection(const MCSection *Section);
virtual void EmitLabel(MCSymbol *Symbol);
virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
virtual void EmitThumbFunc(MCSymbol *Func);
virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
assert(0 && "ELF doesn't support this directive");
}
virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
unsigned ByteAlignment);
virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
assert(0 && "ELF doesn't support this directive");
}
virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
assert(0 && "ELF doesn't support this directive");
}
virtual void EmitCOFFSymbolType(int Type) {
assert(0 && "ELF doesn't support this directive");
}
virtual void EndCOFFSymbolDef() {
assert(0 && "ELF doesn't support this directive");
}
virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
SD.setSize(Value);
}
virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
unsigned Size = 0, unsigned ByteAlignment = 0) {
assert(0 && "ELF doesn't support this directive");
}
virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
uint64_t Size, unsigned ByteAlignment = 0) {
assert(0 && "ELF doesn't support this directive");
}
virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
unsigned ValueSize = 1,
unsigned MaxBytesToEmit = 0);
virtual void EmitCodeAlignment(unsigned ByteAlignment,
unsigned MaxBytesToEmit = 0);
virtual void EmitFileDirective(StringRef Filename);
virtual void Finish();
private:
virtual void EmitInstToFragment(const MCInst &Inst);
virtual void EmitInstToData(const MCInst &Inst);
void fixSymbolsInTLSFixups(const MCExpr *expr);
struct LocalCommon {
MCSymbolData *SD;
uint64_t Size;
unsigned ByteAlignment;
};
std::vector<LocalCommon> LocalCommons;
SmallPtrSet<MCSymbol *, 16> BindingExplicitlySet;
/// @}
void SetSection(StringRef Section, unsigned Type, unsigned Flags,
SectionKind Kind) {
SwitchSection(getContext().getELFSection(Section, Type, Flags, Kind));
}
void SetSectionData() {
SetSection(".data", ELF::SHT_PROGBITS,
ELF::SHF_WRITE |ELF::SHF_ALLOC,
SectionKind::getDataRel());
EmitCodeAlignment(4, 0);
}
void SetSectionText() {
SetSection(".text", ELF::SHT_PROGBITS,
ELF::SHF_EXECINSTR |
ELF::SHF_ALLOC, SectionKind::getText());
EmitCodeAlignment(4, 0);
}
void SetSectionBss() {
SetSection(".bss", ELF::SHT_NOBITS,
ELF::SHF_WRITE |
ELF::SHF_ALLOC, SectionKind::getBSS());
EmitCodeAlignment(4, 0);
}
};
} // end llvm namespace
#endif