MC/Mach-O: Lift some MachObjectWriter arguments into the target specific

interface.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@121981 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Daniel Dunbar 2010-12-16 17:21:02 +00:00
parent fe068b3c36
commit 5d05d9769e
6 changed files with 82 additions and 56 deletions

View File

@ -11,15 +11,30 @@
#define LLVM_MC_MCMACHOBJECTWRITER_H #define LLVM_MC_MCMACHOBJECTWRITER_H
#include "llvm/MC/MCObjectWriter.h" #include "llvm/MC/MCObjectWriter.h"
#include "llvm/Support/DataTypes.h"
namespace llvm { namespace llvm {
class MCMachObjectTargetWriter { class MCMachObjectTargetWriter {
const unsigned Is64Bit : 1;
const uint32_t CPUType;
const uint32_t CPUSubtype;
protected: protected:
MCMachObjectTargetWriter(); MCMachObjectTargetWriter(bool Is64Bit_, uint32_t CPUType_,
uint32_t CPUSubtype_);
public: public:
virtual ~MCMachObjectTargetWriter(); virtual ~MCMachObjectTargetWriter();
/// @name Accessors
/// @{
bool is64Bit() const { return Is64Bit; }
uint32_t getCPUType() const { return CPUType; }
uint32_t getCPUSubtype() const { return CPUSubtype; }
/// @}
}; };
/// \brief Construct a new Mach-O writer instance. /// \brief Construct a new Mach-O writer instance.
@ -30,9 +45,7 @@ public:
/// \param OS - The stream to write to. /// \param OS - The stream to write to.
/// \returns The constructed object writer. /// \returns The constructed object writer.
MCObjectWriter *createMachObjectWriter(MCMachObjectTargetWriter *MOTW, MCObjectWriter *createMachObjectWriter(MCMachObjectTargetWriter *MOTW,
raw_ostream &OS, bool is64Bit, raw_ostream &OS, bool IsLittleEndian);
uint32_t CPUType, uint32_t CPUSubtype,
bool IsLittleEndian);
} // End llvm namespace } // End llvm namespace

View File

@ -11,7 +11,9 @@
using namespace llvm; using namespace llvm;
MCMachObjectTargetWriter::MCMachObjectTargetWriter() { MCMachObjectTargetWriter::MCMachObjectTargetWriter(
bool Is64Bit_, uint32_t CPUType_, uint32_t CPUSubtype_)
: Is64Bit(Is64Bit_), CPUType(CPUType_), CPUSubtype(CPUSubtype_) {
} }
MCMachObjectTargetWriter::~MCMachObjectTargetWriter() { MCMachObjectTargetWriter::~MCMachObjectTargetWriter() {

View File

@ -225,19 +225,19 @@ private:
return OffsetToAlignment(EndAddr, NextSD.getAlignment()); return OffsetToAlignment(EndAddr, NextSD.getAlignment());
} }
unsigned Is64Bit : 1;
uint32_t CPUType;
uint32_t CPUSubtype;
public: public:
MachObjectWriter(MCMachObjectTargetWriter *MOTW, raw_ostream &_OS, MachObjectWriter(MCMachObjectTargetWriter *MOTW, raw_ostream &_OS,
bool _Is64Bit, uint32_t _CPUType, uint32_t _CPUSubtype,
bool _IsLittleEndian) bool _IsLittleEndian)
: MCObjectWriter(_OS, _IsLittleEndian), TargetObjectWriter(MOTW), : MCObjectWriter(_OS, _IsLittleEndian), TargetObjectWriter(MOTW) {
Is64Bit(_Is64Bit), CPUType(_CPUType), CPUSubtype(_CPUSubtype) {
} }
/// @name Target Writer Proxy Accessors
/// @{
bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
/// @}
void WriteHeader(unsigned NumLoadCommands, unsigned LoadCommandsSize, void WriteHeader(unsigned NumLoadCommands, unsigned LoadCommandsSize,
bool SubsectionsViaSymbols) { bool SubsectionsViaSymbols) {
uint32_t Flags = 0; uint32_t Flags = 0;
@ -251,19 +251,19 @@ public:
uint64_t Start = OS.tell(); uint64_t Start = OS.tell();
(void) Start; (void) Start;
Write32(Is64Bit ? macho::HM_Object64 : macho::HM_Object32); Write32(is64Bit() ? macho::HM_Object64 : macho::HM_Object32);
Write32(CPUType); Write32(TargetObjectWriter->getCPUType());
Write32(CPUSubtype); Write32(TargetObjectWriter->getCPUSubtype());
Write32(macho::HFT_Object); Write32(macho::HFT_Object);
Write32(NumLoadCommands); Write32(NumLoadCommands);
Write32(LoadCommandsSize); Write32(LoadCommandsSize);
Write32(Flags); Write32(Flags);
if (Is64Bit) if (is64Bit())
Write32(0); // reserved Write32(0); // reserved
assert(OS.tell() - Start == Is64Bit ? assert(OS.tell() - Start == is64Bit() ?
macho::Header64Size : macho::Header32Size); macho::Header64Size : macho::Header32Size);
} }
@ -281,15 +281,16 @@ public:
uint64_t Start = OS.tell(); uint64_t Start = OS.tell();
(void) Start; (void) Start;
unsigned SegmentLoadCommandSize = Is64Bit ? macho::SegmentLoadCommand64Size: unsigned SegmentLoadCommandSize =
is64Bit() ? macho::SegmentLoadCommand64Size:
macho::SegmentLoadCommand32Size; macho::SegmentLoadCommand32Size;
Write32(Is64Bit ? macho::LCT_Segment64 : macho::LCT_Segment); Write32(is64Bit() ? macho::LCT_Segment64 : macho::LCT_Segment);
Write32(SegmentLoadCommandSize + Write32(SegmentLoadCommandSize +
NumSections * (Is64Bit ? macho::Section64Size : NumSections * (is64Bit() ? macho::Section64Size :
macho::Section32Size)); macho::Section32Size));
WriteBytes("", 16); WriteBytes("", 16);
if (Is64Bit) { if (is64Bit()) {
Write64(0); // vmaddr Write64(0); // vmaddr
Write64(VMSize); // vmsize Write64(VMSize); // vmsize
Write64(SectionDataStartOffset); // file offset Write64(SectionDataStartOffset); // file offset
@ -328,7 +329,7 @@ public:
const MCSectionMachO &Section = cast<MCSectionMachO>(SD.getSection()); const MCSectionMachO &Section = cast<MCSectionMachO>(SD.getSection());
WriteBytes(Section.getSectionName(), 16); WriteBytes(Section.getSectionName(), 16);
WriteBytes(Section.getSegmentName(), 16); WriteBytes(Section.getSegmentName(), 16);
if (Is64Bit) { if (is64Bit()) {
Write64(getSectionAddress(&SD)); // address Write64(getSectionAddress(&SD)); // address
Write64(SectionSize); // size Write64(SectionSize); // size
} else { } else {
@ -348,10 +349,10 @@ public:
Write32(Flags); Write32(Flags);
Write32(IndirectSymBase.lookup(&SD)); // reserved1 Write32(IndirectSymBase.lookup(&SD)); // reserved1
Write32(Section.getStubSize()); // reserved2 Write32(Section.getStubSize()); // reserved2
if (Is64Bit) if (is64Bit())
Write32(0); // reserved3 Write32(0); // reserved3
assert(OS.tell() - Start == Is64Bit ? macho::Section64Size : assert(OS.tell() - Start == is64Bit() ? macho::Section64Size :
macho::Section32Size); macho::Section32Size);
} }
@ -469,7 +470,7 @@ public:
// The Mach-O streamer uses the lowest 16-bits of the flags for the 'desc' // The Mach-O streamer uses the lowest 16-bits of the flags for the 'desc'
// value. // value.
Write16(Flags); Write16(Flags);
if (Is64Bit) if (is64Bit())
Write64(Address); Write64(Address);
else else
Write32(Address); Write32(Address);
@ -798,7 +799,7 @@ public:
const MCFixup &Fixup, MCValue Target, const MCFixup &Fixup, MCValue Target,
uint64_t &FixedValue) { uint64_t &FixedValue) {
assert(Target.getSymA()->getKind() == MCSymbolRefExpr::VK_TLVP && assert(Target.getSymA()->getKind() == MCSymbolRefExpr::VK_TLVP &&
!Is64Bit && !is64Bit() &&
"Should only be called with a 32-bit TLVP relocation!"); "Should only be called with a 32-bit TLVP relocation!");
unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind()); unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind());
@ -840,7 +841,7 @@ public:
void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout, void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
const MCFragment *Fragment, const MCFixup &Fixup, const MCFragment *Fragment, const MCFixup &Fixup,
MCValue Target, uint64_t &FixedValue) { MCValue Target, uint64_t &FixedValue) {
if (Is64Bit) { if (is64Bit()) {
RecordX86_64Relocation(Asm, Layout, Fragment, Fixup, Target, FixedValue); RecordX86_64Relocation(Asm, Layout, Fragment, Fixup, Target, FixedValue);
return; return;
} }
@ -1164,7 +1165,7 @@ public:
// The section data starts after the header, the segment load command (and // The section data starts after the header, the segment load command (and
// section headers) and the symbol table. // section headers) and the symbol table.
unsigned NumLoadCommands = 1; unsigned NumLoadCommands = 1;
uint64_t LoadCommandsSize = Is64Bit ? uint64_t LoadCommandsSize = is64Bit() ?
macho::SegmentLoadCommand64Size + NumSections * macho::Section64Size : macho::SegmentLoadCommand64Size + NumSections * macho::Section64Size :
macho::SegmentLoadCommand32Size + NumSections * macho::Section32Size; macho::SegmentLoadCommand32Size + NumSections * macho::Section32Size;
@ -1179,7 +1180,7 @@ public:
// Compute the total size of the section data, as well as its file size and // Compute the total size of the section data, as well as its file size and
// vm size. // vm size.
uint64_t SectionDataStart = (Is64Bit ? macho::Header64Size : uint64_t SectionDataStart = (is64Bit() ? macho::Header64Size :
macho::Header32Size) + LoadCommandsSize; macho::Header32Size) + LoadCommandsSize;
uint64_t SectionDataSize = 0; uint64_t SectionDataSize = 0;
uint64_t SectionDataFileSize = 0; uint64_t SectionDataFileSize = 0;
@ -1247,7 +1248,7 @@ public:
// The string table is written after symbol table. // The string table is written after symbol table.
uint64_t StringTableOffset = uint64_t StringTableOffset =
SymbolTableOffset + NumSymTabSymbols * (Is64Bit ? macho::Nlist64Size : SymbolTableOffset + NumSymTabSymbols * (is64Bit() ? macho::Nlist64Size :
macho::Nlist32Size); macho::Nlist32Size);
WriteSymtabLoadCommand(SymbolTableOffset, NumSymTabSymbols, WriteSymtabLoadCommand(SymbolTableOffset, NumSymTabSymbols,
StringTableOffset, StringTable.size()); StringTableOffset, StringTable.size());
@ -1327,10 +1328,7 @@ public:
} }
MCObjectWriter *llvm::createMachObjectWriter(MCMachObjectTargetWriter *MOTW, MCObjectWriter *llvm::createMachObjectWriter(MCMachObjectTargetWriter *MOTW,
raw_ostream &OS, bool is64Bit, raw_ostream &OS,
uint32_t CPUType,
uint32_t CPUSubtype,
bool IsLittleEndian) { bool IsLittleEndian) {
return new MachObjectWriter(MOTW, OS, is64Bit, CPUType, CPUSubtype, return new MachObjectWriter(MOTW, OS, IsLittleEndian);
IsLittleEndian);
} }

View File

@ -29,6 +29,10 @@ using namespace llvm;
namespace { namespace {
class ARMMachObjectWriter : public MCMachObjectTargetWriter { class ARMMachObjectWriter : public MCMachObjectTargetWriter {
public:
ARMMachObjectWriter(bool Is64Bit, uint32_t CPUType,
uint32_t CPUSubtype)
: MCMachObjectTargetWriter(Is64Bit, CPUType, CPUSubtype) {}
}; };
class ARMAsmBackend : public TargetAsmBackend { class ARMAsmBackend : public TargetAsmBackend {
@ -385,10 +389,11 @@ public:
MCObjectWriter *createObjectWriter(raw_ostream &OS) const { MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
// FIXME: Subtarget info should be derived. Force v7 for now. // FIXME: Subtarget info should be derived. Force v7 for now.
return createMachObjectWriter(new ARMMachObjectWriter, return createMachObjectWriter(new ARMMachObjectWriter(
OS, /*Is64Bit=*/false, /*Is64Bit=*/false,
object::mach::CTM_ARM, object::mach::CTM_ARM,
object::mach::CSARM_V7, object::mach::CSARM_V7),
OS,
/*IsLittleEndian=*/true); /*IsLittleEndian=*/true);
} }

View File

@ -20,6 +20,10 @@ using namespace llvm;
namespace { namespace {
class PPCMachObjectWriter : public MCMachObjectTargetWriter { class PPCMachObjectWriter : public MCMachObjectTargetWriter {
public:
PPCMachObjectWriter(bool Is64Bit, uint32_t CPUType,
uint32_t CPUSubtype)
: MCMachObjectTargetWriter(Is64Bit, CPUType, CPUSubtype) {}
}; };
class PPCAsmBackend : public TargetAsmBackend { class PPCAsmBackend : public TargetAsmBackend {
@ -95,12 +99,12 @@ namespace {
MCObjectWriter *createObjectWriter(raw_ostream &OS) const { MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
bool is64 = getPointerSize() == 8; bool is64 = getPointerSize() == 8;
return createMachObjectWriter(new PPCMachObjectWriter, return createMachObjectWriter(new PPCMachObjectWriter(
OS, /*Is64Bit=*/is64, /*Is64Bit=*/is64,
(is64 ? object::mach::CTM_PowerPC64 : (is64 ? object::mach::CTM_PowerPC64 :
object::mach::CTM_PowerPC), object::mach::CTM_PowerPC),
object::mach::CSPPC_ALL, object::mach::CSPPC_ALL),
/*IsLittleEndian=*/false); OS, /*IsLittleEndian=*/false);
} }
virtual bool doesSectionRequireSymbols(const MCSection &Section) const { virtual bool doesSectionRequireSymbols(const MCSection &Section) const {

View File

@ -47,6 +47,10 @@ static unsigned getFixupKindLog2Size(unsigned Kind) {
namespace { namespace {
class X86MachObjectWriter : public MCMachObjectTargetWriter { class X86MachObjectWriter : public MCMachObjectTargetWriter {
public:
X86MachObjectWriter(bool Is64Bit, uint32_t CPUType,
uint32_t CPUSubtype)
: MCMachObjectTargetWriter(Is64Bit, CPUType, CPUSubtype) {}
}; };
class X86AsmBackend : public TargetAsmBackend { class X86AsmBackend : public TargetAsmBackend {
@ -365,11 +369,11 @@ public:
: DarwinX86AsmBackend(T) {} : DarwinX86AsmBackend(T) {}
MCObjectWriter *createObjectWriter(raw_ostream &OS) const { MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
return createMachObjectWriter(new X86MachObjectWriter, return createMachObjectWriter(new X86MachObjectWriter(
OS, /*Is64Bit=*/false, /*Is64Bit=*/false,
object::mach::CTM_i386, object::mach::CTM_i386,
object::mach::CSX86_ALL, object::mach::CSX86_ALL),
/*IsLittleEndian=*/true); OS, /*IsLittleEndian=*/true);
} }
}; };
@ -381,11 +385,11 @@ public:
} }
MCObjectWriter *createObjectWriter(raw_ostream &OS) const { MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
return createMachObjectWriter(new X86MachObjectWriter, return createMachObjectWriter(new X86MachObjectWriter(
OS, /*Is64Bit=*/true, /*Is64Bit=*/true,
object::mach::CTM_x86_64, object::mach::CTM_x86_64,
object::mach::CSX86_ALL, object::mach::CSX86_ALL),
/*IsLittleEndian=*/true); OS, /*IsLittleEndian=*/true);
} }
virtual bool doesSectionRequireSymbols(const MCSection &Section) const { virtual bool doesSectionRequireSymbols(const MCSection &Section) const {