Use the methods in the TargetObjInfo object instead of internal methods.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33289 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Bill Wendling 2007-01-17 03:49:21 +00:00
parent 3007f7a30e
commit 157c4eeb4d
2 changed files with 156 additions and 126 deletions

View File

@ -37,6 +37,7 @@
#include "llvm/CodeGen/MachineConstantPool.h"
#include "llvm/Target/TargetData.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetObjInfo.h"
#include "llvm/Support/Mangler.h"
#include "llvm/Support/Streams.h"
using namespace llvm;
@ -53,8 +54,20 @@ namespace llvm {
ELFWriter::ELFSection *ES; // Section to write to.
std::vector<unsigned char> *OutBuffer;
size_t FnStart;
/// Target machine description.
///
TargetMachine &TM;
/// Target object writer info.
///
const TargetObjInfo *TOI;
public:
ELFCodeEmitter(ELFWriter &ew) : EW(ew), OutBuffer(0) {}
ELFCodeEmitter(ELFWriter &ew, TargetMachine &tm)
: EW(ew), OutBuffer(0), TM(tm) {
// Create the target object info object for this target.
TOI = TM.getTargetObjInfo();
}
void startFunction(MachineFunction &F);
bool finishFunction(MachineFunction &F);
@ -113,7 +126,7 @@ void ELFCodeEmitter::startFunction(MachineFunction &F) {
// Add padding zeros to the end of the buffer to make sure that the
// function will start on the correct byte alignment within the section.
ELFWriter::align(*OutBuffer, Align);
TOI->align(*OutBuffer, Align);
FnStart = OutBuffer->size();
}
@ -165,8 +178,11 @@ ELFWriter::ELFWriter(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) {
isLittleEndian = TM.getTargetData()->isLittleEndian();
// Create the machine code emitter object for this target.
MCE = new ELFCodeEmitter(*this);
MCE = new ELFCodeEmitter(*this, TM);
NumSections = 0;
// Create the target object info object for this target.
TOI = TM.getTargetObjInfo();
}
ELFWriter::~ELFWriter() {
@ -181,36 +197,36 @@ bool ELFWriter::doInitialization(Module &M) {
// Local alias to shortenify coming code.
std::vector<unsigned char> &FH = FileHeader;
outbyte(FH, 0x7F); // EI_MAG0
outbyte(FH, 'E'); // EI_MAG1
outbyte(FH, 'L'); // EI_MAG2
outbyte(FH, 'F'); // EI_MAG3
outbyte(FH, is64Bit ? 2 : 1); // EI_CLASS
outbyte(FH, isLittleEndian ? 1 : 2); // EI_DATA
outbyte(FH, 1); // EI_VERSION
FH.resize(16); // EI_PAD up to 16 bytes.
TOI->outbyte(FH, 0x7F); // EI_MAG0
TOI->outbyte(FH, 'E'); // EI_MAG1
TOI->outbyte(FH, 'L'); // EI_MAG2
TOI->outbyte(FH, 'F'); // EI_MAG3
TOI->outbyte(FH, is64Bit ? 2 : 1); // EI_CLASS
TOI->outbyte(FH, isLittleEndian ? 1 : 2); // EI_DATA
TOI->outbyte(FH, 1); // EI_VERSION
FH.resize(16); // EI_PAD up to 16 bytes.
// This should change for shared objects.
outhalf(FH, 1); // e_type = ET_REL
outhalf(FH, e_machine); // e_machine = whatever the target wants
outword(FH, 1); // e_version = 1
outaddr(FH, 0); // e_entry = 0 -> no entry point in .o file
outaddr(FH, 0); // e_phoff = 0 -> no program header for .o
TOI->outhalf(FH, 1); // e_type = ET_REL
TOI->outhalf(FH, e_machine); // e_machine = whatever the target wants
TOI->outword(FH, 1); // e_version = 1
TOI->outaddr(FH, 0); // e_entry = 0 -> no entry point in .o file
TOI->outaddr(FH, 0); // e_phoff = 0 -> no program header for .o
ELFHeader_e_shoff_Offset = FH.size();
outaddr(FH, 0); // e_shoff
outword(FH, e_flags); // e_flags = whatever the target wants
TOI->outaddr(FH, 0); // e_shoff
TOI->outword(FH, e_flags); // e_flags = whatever the target wants
outhalf(FH, is64Bit ? 64 : 52); // e_ehsize = ELF header size
outhalf(FH, 0); // e_phentsize = prog header entry size
outhalf(FH, 0); // e_phnum = # prog header entries = 0
outhalf(FH, is64Bit ? 64 : 40); // e_shentsize = sect hdr entry size
TOI->outhalf(FH, is64Bit ? 64 : 52); // e_ehsize = ELF header size
TOI->outhalf(FH, 0); // e_phentsize = prog header entry size
TOI->outhalf(FH, 0); // e_phnum = # prog header entries=0
TOI->outhalf(FH, is64Bit ? 64 : 40); // e_shentsize = sect hdr entry size
ELFHeader_e_shnum_Offset = FH.size();
outhalf(FH, 0); // e_shnum = # of section header ents
TOI->outhalf(FH, 0); // e_shnum = # of section header ents
ELFHeader_e_shstrndx_Offset = FH.size();
outhalf(FH, 0); // e_shstrndx = Section # of '.shstrtab'
TOI->outhalf(FH, 0); // e_shstrndx = Section # of '.shstrtab'
// Add the null section, which is required to be first in the file.
getSection("", 0, 0);
@ -352,7 +368,7 @@ void ELFWriter::EmitSymbolTable() {
DataBuffer &StrTabBuf = StrTab.SectionData;
// Set the zero'th symbol to a null byte, as required.
outbyte(StrTabBuf, 0);
TOI->outbyte(StrTabBuf, 0);
SymbolTable[0].NameIdx = 0;
unsigned Index = 1;
for (unsigned i = 1, e = SymbolTable.size(); i != e; ++i) {
@ -389,22 +405,22 @@ void ELFWriter::EmitSymbolTable() {
if (!is64Bit) { // 32-bit and 64-bit formats are shuffled a bit.
for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) {
ELFSym &Sym = SymbolTable[i];
outword(SymTabBuf, Sym.NameIdx);
outaddr32(SymTabBuf, Sym.Value);
outword(SymTabBuf, Sym.Size);
outbyte(SymTabBuf, Sym.Info);
outbyte(SymTabBuf, Sym.Other);
outhalf(SymTabBuf, Sym.SectionIdx);
TOI->outword(SymTabBuf, Sym.NameIdx);
TOI->outaddr32(SymTabBuf, Sym.Value);
TOI->outword(SymTabBuf, Sym.Size);
TOI->outbyte(SymTabBuf, Sym.Info);
TOI->outbyte(SymTabBuf, Sym.Other);
TOI->outhalf(SymTabBuf, Sym.SectionIdx);
}
} else {
for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) {
ELFSym &Sym = SymbolTable[i];
outword(SymTabBuf, Sym.NameIdx);
outbyte(SymTabBuf, Sym.Info);
outbyte(SymTabBuf, Sym.Other);
outhalf(SymTabBuf, Sym.SectionIdx);
outaddr64(SymTabBuf, Sym.Value);
outxword(SymTabBuf, Sym.Size);
TOI->outword(SymTabBuf, Sym.NameIdx);
TOI->outbyte(SymTabBuf, Sym.Info);
TOI->outbyte(SymTabBuf, Sym.Other);
TOI->outhalf(SymTabBuf, Sym.SectionIdx);
TOI->outaddr64(SymTabBuf, Sym.Value);
TOI->outxword(SymTabBuf, Sym.Size);
}
}
@ -420,7 +436,7 @@ void ELFWriter::EmitSectionTableStringTable() {
// Now that we know which section number is the .shstrtab section, update the
// e_shstrndx entry in the ELF header.
fixhalf(FileHeader, SHStrTab.SectionIdx, ELFHeader_e_shstrndx_Offset);
TOI->fixhalf(FileHeader, SHStrTab.SectionIdx, ELFHeader_e_shstrndx_Offset);
// Set the NameIdx of each section in the string table and emit the bytes for
// the string table.
@ -471,11 +487,11 @@ void ELFWriter::OutputSectionsAndSectionTable() {
// Now that we know where all of the sections will be emitted, set the e_shnum
// entry in the ELF header.
fixhalf(FileHeader, NumSections, ELFHeader_e_shnum_Offset);
TOI->fixhalf(FileHeader, NumSections, ELFHeader_e_shnum_Offset);
// Now that we know the offset in the file of the section table, update the
// e_shoff address in the ELF header.
fixaddr(FileHeader, FileOff, ELFHeader_e_shoff_Offset);
TOI->fixaddr(FileHeader, FileOff, ELFHeader_e_shoff_Offset);
// Now that we know all of the data in the file header, emit it and all of the
// sections!
@ -497,16 +513,16 @@ void ELFWriter::OutputSectionsAndSectionTable() {
O.write((char*)&S.SectionData[0], S.SectionData.size());
FileOff += S.SectionData.size();
outword(Table, S.NameIdx); // sh_name - Symbol table name idx
outword(Table, S.Type); // sh_type - Section contents & semantics
outword(Table, S.Flags); // sh_flags - Section flags.
outaddr(Table, S.Addr); // sh_addr - The mem addr this section is in.
outaddr(Table, S.Offset); // sh_offset - Offset from the file start.
outword(Table, S.Size); // sh_size - The section size.
outword(Table, S.Link); // sh_link - Section header table index link.
outword(Table, S.Info); // sh_info - Auxillary information.
outword(Table, S.Align); // sh_addralign - Alignment of section.
outword(Table, S.EntSize); // sh_entsize - Size of entries in the section.
TOI->outword(Table, S.NameIdx); // sh_name - Symbol table name idx
TOI->outword(Table, S.Type); // sh_type - Section contents & semantics
TOI->outword(Table, S.Flags); // sh_flags - Section flags
TOI->outaddr(Table, S.Addr); // sh_addr - The mem addr this section is in
TOI->outaddr(Table, S.Offset); // sh_offset - Offset from the file start
TOI->outword(Table, S.Size); // sh_size - The section size
TOI->outword(Table, S.Link); // sh_link - Section header table index link
TOI->outword(Table, S.Info); // sh_info - Auxillary information
TOI->outword(Table, S.Align); // sh_addralign - Alignment of section
TOI->outword(Table, S.EntSize); // sh_entsize - Size of entries in the sect
SectionList.pop_front();
}

View File

@ -32,11 +32,11 @@
#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/Target/TargetAsmInfo.h"
#include "llvm/Target/TargetJITInfo.h"
#include "llvm/Target/TargetObjInfo.h"
#include "llvm/Support/Mangler.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/Streams.h"
#include <algorithm>
using namespace llvm;
//===----------------------------------------------------------------------===//
@ -49,6 +49,14 @@ namespace llvm {
class MachOCodeEmitter : public MachineCodeEmitter {
MachOWriter &MOW;
/// Target machine description.
///
TargetMachine &TM;
/// Target object writer info.
///
const TargetObjInfo *TOI;
/// Relocations - These are the relocations that the function needs, as
/// emitted.
std::vector<MachineRelocation> Relocations;
@ -71,7 +79,10 @@ namespace llvm {
std::vector<intptr_t> MBBLocations;
public:
MachOCodeEmitter(MachOWriter &mow) : MOW(mow) {}
MachOCodeEmitter(MachOWriter &mow, TargetMachine &tm) : MOW(mow), TM(tm) {
// Create the target object info object for this target.
TOI = TM.getTargetObjInfo();
}
virtual void startFunction(MachineFunction &F);
virtual bool finishFunction(MachineFunction &F);
@ -163,7 +174,7 @@ bool MachOCodeEmitter::finishFunction(MachineFunction &F) {
// Get a symbol for the function to add to the symbol table
const GlobalValue *FuncV = F.getFunction();
MachOSym FnSym(FuncV, MOW.Mang->getValueName(FuncV), MOS->Index, MOW.TM);
MachOSym FnSym(FuncV, MOW.Mang->getValueName(FuncV), MOS->Index, TM);
// Emit constant pool to appropriate section(s)
emitConstantPool(F.getConstantPool());
@ -211,7 +222,7 @@ void MachOCodeEmitter::emitConstantPool(MachineConstantPool *MCP) {
if (CP.empty()) return;
// FIXME: handle PIC codegen
bool isPIC = MOW.TM.getRelocationModel() == Reloc::PIC_;
bool isPIC = TM.getRelocationModel() == Reloc::PIC_;
assert(!isPIC && "PIC codegen not yet handled for mach-o jump tables!");
// Although there is no strict necessity that I am aware of, we will do what
@ -223,7 +234,7 @@ void MachOCodeEmitter::emitConstantPool(MachineConstantPool *MCP) {
// "giant object for PIC" optimization.
for (unsigned i = 0, e = CP.size(); i != e; ++i) {
const Type *Ty = CP[i].getType();
unsigned Size = MOW.TM.getTargetData()->getTypeSize(Ty);
unsigned Size = TM.getTargetData()->getTypeSize(Ty);
MachOWriter::MachOSection *Sec = MOW.getConstSection(Ty);
CPLocations.push_back(Sec->SectionData.size());
@ -236,10 +247,10 @@ void MachOCodeEmitter::emitConstantPool(MachineConstantPool *MCP) {
// FIXME: need alignment?
// FIXME: share between here and AddSymbolToSection?
for (unsigned j = 0; j < Size; ++j)
MOW.outbyte(Sec->SectionData, 0);
TOI->outbyte(Sec->SectionData, 0);
MOW.InitMem(CP[i].Val.ConstVal, &Sec->SectionData[0], CPLocations[i],
MOW.TM.getTargetData(), Sec->Relocations);
TM.getTargetData(), Sec->Relocations);
}
}
@ -250,7 +261,7 @@ void MachOCodeEmitter::emitJumpTables(MachineJumpTableInfo *MJTI) {
if (JT.empty()) return;
// FIXME: handle PIC codegen
bool isPIC = MOW.TM.getRelocationModel() == Reloc::PIC_;
bool isPIC = TM.getRelocationModel() == Reloc::PIC_;
assert(!isPIC && "PIC codegen not yet handled for mach-o jump tables!");
MachOWriter::MachOSection *Sec = MOW.getJumpTableSection();
@ -267,7 +278,7 @@ void MachOCodeEmitter::emitJumpTables(MachineJumpTableInfo *MJTI) {
MR.setResultPointer((void *)JTLocations[i]);
MR.setConstantVal(TextSecIndex);
Sec->Relocations.push_back(MR);
MOW.outaddr(Sec->SectionData, 0);
TOI->outaddr(Sec->SectionData, 0);
}
}
// FIXME: remove when we have unified size + output buffer
@ -283,7 +294,10 @@ MachOWriter::MachOWriter(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) {
isLittleEndian = TM.getTargetData()->isLittleEndian();
// Create the machine code emitter object for this target.
MCE = new MachOCodeEmitter(*this);
MCE = new MachOCodeEmitter(*this, tm);
// Create the target object info object for this target.
TOI = TM.getTargetObjInfo();
}
MachOWriter::~MachOWriter() {
@ -312,7 +326,7 @@ void MachOWriter::AddSymbolToSection(MachOSection *Sec, GlobalVariable *GV) {
// FIXME: remove when we have unified size + output buffer
unsigned AlignedSize = Sec->size - OrigSize;
for (unsigned i = 0; i < AlignedSize; ++i)
outbyte(Sec->SectionData, 0);
TOI->outbyte(Sec->SectionData, 0);
}
// Record the offset of the symbol, and then allocate space for it.
// FIXME: remove when we have unified size + output buffer
@ -329,7 +343,7 @@ void MachOWriter::AddSymbolToSection(MachOSection *Sec, GlobalVariable *GV) {
// Allocate space in the section for the global.
for (unsigned i = 0; i < Size; ++i)
outbyte(Sec->SectionData, 0);
TOI->outbyte(Sec->SectionData, 0);
}
void MachOWriter::EmitGlobal(GlobalVariable *GV) {
@ -442,15 +456,15 @@ void MachOWriter::EmitHeaderAndLoadCommands() {
// Step #3: write the header to the file
// Local alias to shortenify coming code.
DataBuffer &FH = Header.HeaderData;
outword(FH, Header.magic);
outword(FH, Header.cputype);
outword(FH, Header.cpusubtype);
outword(FH, Header.filetype);
outword(FH, Header.ncmds);
outword(FH, Header.sizeofcmds);
outword(FH, Header.flags);
TOI->outword(FH, Header.magic);
TOI->outword(FH, Header.cputype);
TOI->outword(FH, Header.cpusubtype);
TOI->outword(FH, Header.filetype);
TOI->outword(FH, Header.ncmds);
TOI->outword(FH, Header.sizeofcmds);
TOI->outword(FH, Header.flags);
if (is64Bit)
outword(FH, Header.reserved);
TOI->outword(FH, Header.reserved);
// Step #4: Finish filling in the segment load command and write it out
for (std::vector<MachOSection*>::iterator I = SectionList.begin(),
@ -460,17 +474,17 @@ void MachOWriter::EmitHeaderAndLoadCommands() {
SEG.vmsize = SEG.filesize;
SEG.fileoff = Header.cmdSize(is64Bit) + Header.sizeofcmds;
outword(FH, SEG.cmd);
outword(FH, SEG.cmdsize);
outstring(FH, SEG.segname, 16);
outaddr(FH, SEG.vmaddr);
outaddr(FH, SEG.vmsize);
outaddr(FH, SEG.fileoff);
outaddr(FH, SEG.filesize);
outword(FH, SEG.maxprot);
outword(FH, SEG.initprot);
outword(FH, SEG.nsects);
outword(FH, SEG.flags);
TOI->outword(FH, SEG.cmd);
TOI->outword(FH, SEG.cmdsize);
TOI->outstring(FH, SEG.segname, 16);
TOI->outaddr(FH, SEG.vmaddr);
TOI->outaddr(FH, SEG.vmsize);
TOI->outaddr(FH, SEG.fileoff);
TOI->outaddr(FH, SEG.filesize);
TOI->outword(FH, SEG.maxprot);
TOI->outword(FH, SEG.initprot);
TOI->outword(FH, SEG.nsects);
TOI->outword(FH, SEG.flags);
// Step #5: Finish filling in the fields of the MachOSections
uint64_t currentAddr = 0;
@ -497,19 +511,19 @@ void MachOWriter::EmitHeaderAndLoadCommands() {
currentAddr += MOS->nreloc * 8;
// write the finalized section command to the output buffer
outstring(FH, MOS->sectname, 16);
outstring(FH, MOS->segname, 16);
outaddr(FH, MOS->addr);
outaddr(FH, MOS->size);
outword(FH, MOS->offset);
outword(FH, MOS->align);
outword(FH, MOS->reloff);
outword(FH, MOS->nreloc);
outword(FH, MOS->flags);
outword(FH, MOS->reserved1);
outword(FH, MOS->reserved2);
TOI->outstring(FH, MOS->sectname, 16);
TOI->outstring(FH, MOS->segname, 16);
TOI->outaddr(FH, MOS->addr);
TOI->outaddr(FH, MOS->size);
TOI->outword(FH, MOS->offset);
TOI->outword(FH, MOS->align);
TOI->outword(FH, MOS->reloff);
TOI->outword(FH, MOS->nreloc);
TOI->outword(FH, MOS->flags);
TOI->outword(FH, MOS->reserved1);
TOI->outword(FH, MOS->reserved2);
if (is64Bit)
outword(FH, MOS->reserved3);
TOI->outword(FH, MOS->reserved3);
}
// Step #7: Emit the symbol table to temporary buffers, so that we know the
@ -521,36 +535,36 @@ void MachOWriter::EmitHeaderAndLoadCommands() {
SymTab.nsyms = SymbolTable.size();
SymTab.stroff = SymTab.symoff + SymT.size();
SymTab.strsize = StrT.size();
outword(FH, SymTab.cmd);
outword(FH, SymTab.cmdsize);
outword(FH, SymTab.symoff);
outword(FH, SymTab.nsyms);
outword(FH, SymTab.stroff);
outword(FH, SymTab.strsize);
TOI->outword(FH, SymTab.cmd);
TOI->outword(FH, SymTab.cmdsize);
TOI->outword(FH, SymTab.symoff);
TOI->outword(FH, SymTab.nsyms);
TOI->outword(FH, SymTab.stroff);
TOI->outword(FH, SymTab.strsize);
// FIXME: set DySymTab fields appropriately
// We should probably just update these in BufferSymbolAndStringTable since
// thats where we're partitioning up the different kinds of symbols.
outword(FH, DySymTab.cmd);
outword(FH, DySymTab.cmdsize);
outword(FH, DySymTab.ilocalsym);
outword(FH, DySymTab.nlocalsym);
outword(FH, DySymTab.iextdefsym);
outword(FH, DySymTab.nextdefsym);
outword(FH, DySymTab.iundefsym);
outword(FH, DySymTab.nundefsym);
outword(FH, DySymTab.tocoff);
outword(FH, DySymTab.ntoc);
outword(FH, DySymTab.modtaboff);
outword(FH, DySymTab.nmodtab);
outword(FH, DySymTab.extrefsymoff);
outword(FH, DySymTab.nextrefsyms);
outword(FH, DySymTab.indirectsymoff);
outword(FH, DySymTab.nindirectsyms);
outword(FH, DySymTab.extreloff);
outword(FH, DySymTab.nextrel);
outword(FH, DySymTab.locreloff);
outword(FH, DySymTab.nlocrel);
TOI->outword(FH, DySymTab.cmd);
TOI->outword(FH, DySymTab.cmdsize);
TOI->outword(FH, DySymTab.ilocalsym);
TOI->outword(FH, DySymTab.nlocalsym);
TOI->outword(FH, DySymTab.iextdefsym);
TOI->outword(FH, DySymTab.nextdefsym);
TOI->outword(FH, DySymTab.iundefsym);
TOI->outword(FH, DySymTab.nundefsym);
TOI->outword(FH, DySymTab.tocoff);
TOI->outword(FH, DySymTab.ntoc);
TOI->outword(FH, DySymTab.modtaboff);
TOI->outword(FH, DySymTab.nmodtab);
TOI->outword(FH, DySymTab.extrefsymoff);
TOI->outword(FH, DySymTab.nextrefsyms);
TOI->outword(FH, DySymTab.indirectsymoff);
TOI->outword(FH, DySymTab.nindirectsyms);
TOI->outword(FH, DySymTab.extreloff);
TOI->outword(FH, DySymTab.nextrel);
TOI->outword(FH, DySymTab.locreloff);
TOI->outword(FH, DySymTab.nlocrel);
O.write((char*)&FH[0], FH.size());
}
@ -627,7 +641,7 @@ void MachOWriter::BufferSymbolAndStringTable() {
// Write out a leading zero byte when emitting string table, for n_strx == 0
// which means an empty string.
outbyte(StrT, 0);
TOI->outbyte(StrT, 0);
// The order of the string table is:
// 1. strings for external symbols
@ -640,7 +654,7 @@ void MachOWriter::BufferSymbolAndStringTable() {
I->n_strx = 0;
} else {
I->n_strx = StrT.size();
outstring(StrT, I->GVName, I->GVName.length()+1);
TOI->outstring(StrT, I->GVName, I->GVName.length()+1);
}
}
@ -654,11 +668,11 @@ void MachOWriter::BufferSymbolAndStringTable() {
I->n_value += GVSection[GV]->addr;
// Emit nlist to buffer
outword(SymT, I->n_strx);
outbyte(SymT, I->n_type);
outbyte(SymT, I->n_sect);
outhalf(SymT, I->n_desc);
outaddr(SymT, I->n_value);
TOI->outword(SymT, I->n_strx);
TOI->outbyte(SymT, I->n_type);
TOI->outbyte(SymT, I->n_sect);
TOI->outhalf(SymT, I->n_desc);
TOI->outaddr(SymT, I->n_value);
}
}