Changed to use the OutputBuffer instead of the methods in MachO and ELF

writers.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33311 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Bill Wendling 2007-01-17 22:22:31 +00:00
parent f341ccbff9
commit 203d3e4386
5 changed files with 199 additions and 347 deletions

View File

@ -214,102 +214,6 @@ namespace llvm {
unsigned ELFHeader_e_shoff_Offset; // e_shoff in ELF header. unsigned ELFHeader_e_shoff_Offset; // e_shoff in ELF header.
unsigned ELFHeader_e_shstrndx_Offset; // e_shstrndx in ELF header. unsigned ELFHeader_e_shstrndx_Offset; // e_shstrndx in ELF header.
unsigned ELFHeader_e_shnum_Offset; // e_shnum in ELF header. unsigned ELFHeader_e_shnum_Offset; // e_shnum in ELF header.
// align - Emit padding into the file until the current output position is
// aligned to the specified power of two boundary.
static void align(DataBuffer &Output, unsigned Boundary) {
assert(Boundary && (Boundary & (Boundary-1)) == 0 &&
"Must align to 2^k boundary");
size_t Size = Output.size();
if (Size & (Boundary-1)) {
// Add padding to get alignment to the correct place.
size_t Pad = Boundary-(Size & (Boundary-1));
Output.resize(Size+Pad);
}
}
static void outbyte(DataBuffer &Output, unsigned char X) {
Output.push_back(X);
}
void outhalf(DataBuffer &Output, unsigned short X) {
if (isLittleEndian) {
Output.push_back(X&255);
Output.push_back(X >> 8);
} else {
Output.push_back(X >> 8);
Output.push_back(X&255);
}
}
void outword(DataBuffer &Output, unsigned X) {
if (isLittleEndian) {
Output.push_back((X >> 0) & 255);
Output.push_back((X >> 8) & 255);
Output.push_back((X >> 16) & 255);
Output.push_back((X >> 24) & 255);
} else {
Output.push_back((X >> 24) & 255);
Output.push_back((X >> 16) & 255);
Output.push_back((X >> 8) & 255);
Output.push_back((X >> 0) & 255);
}
}
void outxword(DataBuffer &Output, uint64_t X) {
if (isLittleEndian) {
Output.push_back(unsigned(X >> 0) & 255);
Output.push_back(unsigned(X >> 8) & 255);
Output.push_back(unsigned(X >> 16) & 255);
Output.push_back(unsigned(X >> 24) & 255);
Output.push_back(unsigned(X >> 32) & 255);
Output.push_back(unsigned(X >> 40) & 255);
Output.push_back(unsigned(X >> 48) & 255);
Output.push_back(unsigned(X >> 56) & 255);
} else {
Output.push_back(unsigned(X >> 56) & 255);
Output.push_back(unsigned(X >> 48) & 255);
Output.push_back(unsigned(X >> 40) & 255);
Output.push_back(unsigned(X >> 32) & 255);
Output.push_back(unsigned(X >> 24) & 255);
Output.push_back(unsigned(X >> 16) & 255);
Output.push_back(unsigned(X >> 8) & 255);
Output.push_back(unsigned(X >> 0) & 255);
}
}
void outaddr32(DataBuffer &Output, unsigned X) {
outword(Output, X);
}
void outaddr64(DataBuffer &Output, uint64_t X) {
outxword(Output, X);
}
void outaddr(DataBuffer &Output, uint64_t X) {
if (!is64Bit)
outword(Output, (unsigned)X);
else
outxword(Output, X);
}
// fix functions - Replace an existing entry at an offset.
void fixhalf(DataBuffer &Output, unsigned short X, unsigned Offset) {
unsigned char *P = &Output[Offset];
P[0] = (X >> (isLittleEndian ? 0 : 8)) & 255;
P[1] = (X >> (isLittleEndian ? 8 : 0)) & 255;
}
void fixword(DataBuffer &Output, unsigned X, unsigned Offset) {
unsigned char *P = &Output[Offset];
P[0] = (X >> (isLittleEndian ? 0 : 24)) & 255;
P[1] = (X >> (isLittleEndian ? 8 : 16)) & 255;
P[2] = (X >> (isLittleEndian ? 16 : 8)) & 255;
P[3] = (X >> (isLittleEndian ? 24 : 0)) & 255;
}
void fixaddr(DataBuffer &Output, uint64_t X, unsigned Offset) {
if (!is64Bit)
fixword(Output, (unsigned)X, Offset);
else
assert(0 && "Emission of 64-bit data not implemented yet!");
}
private: private:
void EmitGlobal(GlobalVariable *GV); void EmitGlobal(GlobalVariable *GV);

View File

@ -659,101 +659,6 @@ namespace llvm {
/// SymbolTable to aid in emitting the DYSYMTAB load command. /// SymbolTable to aid in emitting the DYSYMTAB load command.
std::vector<unsigned> DynamicSymbolTable; std::vector<unsigned> DynamicSymbolTable;
// align - Emit padding into the file until the current output position is
// aligned to the specified power of two boundary.
static void align(DataBuffer &Output, unsigned Boundary) {
assert(Boundary && (Boundary & (Boundary-1)) == 0 &&
"Must align to 2^k boundary");
size_t Size = Output.size();
if (Size & (Boundary-1)) {
// Add padding to get alignment to the correct place.
size_t Pad = Boundary-(Size & (Boundary-1));
Output.resize(Size+Pad);
}
}
void outbyte(DataBuffer &Output, unsigned char X) {
Output.push_back(X);
}
void outhalf(DataBuffer &Output, unsigned short X) {
if (isLittleEndian) {
Output.push_back(X&255);
Output.push_back(X >> 8);
} else {
Output.push_back(X >> 8);
Output.push_back(X&255);
}
}
void outword(DataBuffer &Output, unsigned X) {
if (isLittleEndian) {
Output.push_back((X >> 0) & 255);
Output.push_back((X >> 8) & 255);
Output.push_back((X >> 16) & 255);
Output.push_back((X >> 24) & 255);
} else {
Output.push_back((X >> 24) & 255);
Output.push_back((X >> 16) & 255);
Output.push_back((X >> 8) & 255);
Output.push_back((X >> 0) & 255);
}
}
void outxword(DataBuffer &Output, uint64_t X) {
if (isLittleEndian) {
Output.push_back(unsigned(X >> 0) & 255);
Output.push_back(unsigned(X >> 8) & 255);
Output.push_back(unsigned(X >> 16) & 255);
Output.push_back(unsigned(X >> 24) & 255);
Output.push_back(unsigned(X >> 32) & 255);
Output.push_back(unsigned(X >> 40) & 255);
Output.push_back(unsigned(X >> 48) & 255);
Output.push_back(unsigned(X >> 56) & 255);
} else {
Output.push_back(unsigned(X >> 56) & 255);
Output.push_back(unsigned(X >> 48) & 255);
Output.push_back(unsigned(X >> 40) & 255);
Output.push_back(unsigned(X >> 32) & 255);
Output.push_back(unsigned(X >> 24) & 255);
Output.push_back(unsigned(X >> 16) & 255);
Output.push_back(unsigned(X >> 8) & 255);
Output.push_back(unsigned(X >> 0) & 255);
}
}
void outaddr32(DataBuffer &Output, unsigned X) {
outword(Output, X);
}
void outaddr64(DataBuffer &Output, uint64_t X) {
outxword(Output, X);
}
void outaddr(DataBuffer &Output, uint64_t X) {
if (!is64Bit)
outword(Output, (unsigned)X);
else
outxword(Output, X);
}
void outstring(DataBuffer &Output, std::string &S, unsigned Length) {
unsigned len_to_copy = S.length() < Length ? S.length() : Length;
unsigned len_to_fill = S.length() < Length ? Length-S.length() : 0;
for (unsigned i = 0; i < len_to_copy; ++i)
outbyte(Output, S[i]);
for (unsigned i = 0; i < len_to_fill; ++i)
outbyte(Output, 0);
}
void fixhalf(DataBuffer &Output, unsigned short X, unsigned Offset) {
unsigned char *P = &Output[Offset];
P[0] = (X >> (isLittleEndian ? 0 : 8)) & 255;
P[1] = (X >> (isLittleEndian ? 8 : 0)) & 255;
}
void fixword(DataBuffer &Output, unsigned X, unsigned Offset) {
unsigned char *P = &Output[Offset];
P[0] = (X >> (isLittleEndian ? 0 : 24)) & 255;
P[1] = (X >> (isLittleEndian ? 8 : 16)) & 255;
P[2] = (X >> (isLittleEndian ? 16 : 8)) & 255;
P[3] = (X >> (isLittleEndian ? 24 : 0)) & 255;
}
static void InitMem(const Constant *C, void *Addr, intptr_t Offset, static void InitMem(const Constant *C, void *Addr, intptr_t Offset,
const TargetData *TD, const TargetData *TD,
std::vector<MachineRelocation> &MRs); std::vector<MachineRelocation> &MRs);

View File

@ -38,6 +38,7 @@
#include "llvm/Target/TargetData.h" #include "llvm/Target/TargetData.h"
#include "llvm/Target/TargetMachine.h" #include "llvm/Target/TargetMachine.h"
#include "llvm/Support/Mangler.h" #include "llvm/Support/Mangler.h"
#include "llvm/Support/OutputBuffer.h"
#include "llvm/Support/Streams.h" #include "llvm/Support/Streams.h"
using namespace llvm; using namespace llvm;
@ -50,11 +51,12 @@ namespace llvm {
/// functions to the ELF file. /// functions to the ELF file.
class ELFCodeEmitter : public MachineCodeEmitter { class ELFCodeEmitter : public MachineCodeEmitter {
ELFWriter &EW; ELFWriter &EW;
TargetMachine &TM;
ELFWriter::ELFSection *ES; // Section to write to. ELFWriter::ELFSection *ES; // Section to write to.
std::vector<unsigned char> *OutBuffer; std::vector<unsigned char> *OutBuffer;
size_t FnStart; size_t FnStart;
public: public:
ELFCodeEmitter(ELFWriter &ew) : EW(ew), OutBuffer(0) {} ELFCodeEmitter(ELFWriter &ew) : EW(ew), TM(EW.TM), OutBuffer(0) {}
void startFunction(MachineFunction &F); void startFunction(MachineFunction &F);
bool finishFunction(MachineFunction &F); bool finishFunction(MachineFunction &F);
@ -103,8 +105,8 @@ void ELFCodeEmitter::startFunction(MachineFunction &F) {
ELFWriter::ELFSection::SHF_EXECINSTR | ELFWriter::ELFSection::SHF_EXECINSTR |
ELFWriter::ELFSection::SHF_ALLOC); ELFWriter::ELFSection::SHF_ALLOC);
OutBuffer = &ES->SectionData; OutBuffer = &ES->SectionData;
cerr << "FIXME: This code needs to be updated for changes in the" cerr << "FIXME: This code needs to be updated for changes in the "
<< " CodeEmitter interfaces. In particular, this should set " << "CodeEmitter interfaces. In particular, this should set "
<< "BufferBegin/BufferEnd/CurBufferPtr, not deal with OutBuffer!"; << "BufferBegin/BufferEnd/CurBufferPtr, not deal with OutBuffer!";
abort(); abort();
@ -113,8 +115,8 @@ void ELFCodeEmitter::startFunction(MachineFunction &F) {
// Add padding zeros to the end of the buffer to make sure that the // 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. // function will start on the correct byte alignment within the section.
ELFWriter::align(*OutBuffer, Align); OutputBuffer OB(TM, *OutBuffer);
OB.align(Align);
FnStart = OutBuffer->size(); FnStart = OutBuffer->size();
} }
@ -145,7 +147,7 @@ bool ELFCodeEmitter::finishFunction(MachineFunction &F) {
FnSym.SetType(ELFWriter::ELFSym::STT_FUNC); FnSym.SetType(ELFWriter::ELFSym::STT_FUNC);
FnSym.SectionIdx = ES->SectionIdx; FnSym.SectionIdx = ES->SectionIdx;
FnSym.Value = FnStart; // Value = Offset from start of Section. FnSym.Value = FnStart; // Value = Offset from start of Section.
FnSym.Size = OutBuffer->size()-FnStart; FnSym.Size = OutBuffer->size()-FnStart;
// Finally, add it to the symtab. // Finally, add it to the symtab.
@ -180,37 +182,38 @@ bool ELFWriter::doInitialization(Module &M) {
// Local alias to shortenify coming code. // Local alias to shortenify coming code.
std::vector<unsigned char> &FH = FileHeader; std::vector<unsigned char> &FH = FileHeader;
OutputBuffer FHOut(TM, FH);
outbyte(FH, 0x7F); // EI_MAG0 FHOut.outbyte(0x7F); // EI_MAG0
outbyte(FH, 'E'); // EI_MAG1 FHOut.outbyte('E'); // EI_MAG1
outbyte(FH, 'L'); // EI_MAG2 FHOut.outbyte('L'); // EI_MAG2
outbyte(FH, 'F'); // EI_MAG3 FHOut.outbyte('F'); // EI_MAG3
outbyte(FH, is64Bit ? 2 : 1); // EI_CLASS FHOut.outbyte(is64Bit ? 2 : 1); // EI_CLASS
outbyte(FH, isLittleEndian ? 1 : 2); // EI_DATA FHOut.outbyte(isLittleEndian ? 1 : 2); // EI_DATA
outbyte(FH, 1); // EI_VERSION FHOut.outbyte(1); // EI_VERSION
FH.resize(16); // EI_PAD up to 16 bytes. FH.resize(16); // EI_PAD up to 16 bytes.
// This should change for shared objects. // This should change for shared objects.
outhalf(FH, 1); // e_type = ET_REL FHOut.outhalf(1); // e_type = ET_REL
outhalf(FH, e_machine); // e_machine = whatever the target wants FHOut.outhalf(e_machine); // e_machine = whatever the target wants
outword(FH, 1); // e_version = 1 FHOut.outword(1); // e_version = 1
outaddr(FH, 0); // e_entry = 0 -> no entry point in .o file FHOut.outaddr(0); // e_entry = 0 -> no entry point in .o file
outaddr(FH, 0); // e_phoff = 0 -> no program header for .o FHOut.outaddr(0); // e_phoff = 0 -> no program header for .o
ELFHeader_e_shoff_Offset = FH.size(); ELFHeader_e_shoff_Offset = FH.size();
outaddr(FH, 0); // e_shoff FHOut.outaddr(0); // e_shoff
outword(FH, e_flags); // e_flags = whatever the target wants FHOut.outword(e_flags); // e_flags = whatever the target wants
outhalf(FH, is64Bit ? 64 : 52); // e_ehsize = ELF header size FHOut.outhalf(is64Bit ? 64 : 52); // e_ehsize = ELF header size
outhalf(FH, 0); // e_phentsize = prog header entry size FHOut.outhalf(0); // e_phentsize = prog header entry size
outhalf(FH, 0); // e_phnum = # prog header entries = 0 FHOut.outhalf(0); // e_phnum = # prog header entries = 0
outhalf(FH, is64Bit ? 64 : 40); // e_shentsize = sect hdr entry size FHOut.outhalf(is64Bit ? 64 : 40); // e_shentsize = sect hdr entry size
ELFHeader_e_shnum_Offset = FH.size(); ELFHeader_e_shnum_Offset = FH.size();
outhalf(FH, 0); // e_shnum = # of section header ents FHOut.outhalf(0); // e_shnum = # of section header ents
ELFHeader_e_shstrndx_Offset = FH.size(); ELFHeader_e_shstrndx_Offset = FH.size();
outhalf(FH, 0); // e_shstrndx = Section # of '.shstrtab' FHOut.outhalf(0); // e_shstrndx = Section # of '.shstrtab'
// Add the null section, which is required to be first in the file. // Add the null section, which is required to be first in the file.
getSection("", 0, 0); getSection("", 0, 0);
@ -350,9 +353,10 @@ void ELFWriter::EmitSymbolTable() {
StrTab.Align = 1; StrTab.Align = 1;
DataBuffer &StrTabBuf = StrTab.SectionData; DataBuffer &StrTabBuf = StrTab.SectionData;
OutputBuffer StrTabOut(TM, StrTabBuf);
// Set the zero'th symbol to a null byte, as required. // Set the zero'th symbol to a null byte, as required.
outbyte(StrTabBuf, 0); StrTabOut.outbyte(0);
SymbolTable[0].NameIdx = 0; SymbolTable[0].NameIdx = 0;
unsigned Index = 1; unsigned Index = 1;
for (unsigned i = 1, e = SymbolTable.size(); i != e; ++i) { for (unsigned i = 1, e = SymbolTable.size(); i != e; ++i) {
@ -385,26 +389,27 @@ void ELFWriter::EmitSymbolTable() {
SymTab.Info = FirstNonLocalSymbol; // First non-STB_LOCAL symbol. SymTab.Info = FirstNonLocalSymbol; // First non-STB_LOCAL symbol.
SymTab.EntSize = 16; // Size of each symtab entry. FIXME: wrong for ELF64 SymTab.EntSize = 16; // Size of each symtab entry. FIXME: wrong for ELF64
DataBuffer &SymTabBuf = SymTab.SectionData; DataBuffer &SymTabBuf = SymTab.SectionData;
OutputBuffer SymTabOut(TM, SymTabBuf);
if (!is64Bit) { // 32-bit and 64-bit formats are shuffled a bit. if (!is64Bit) { // 32-bit and 64-bit formats are shuffled a bit.
for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) { for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) {
ELFSym &Sym = SymbolTable[i]; ELFSym &Sym = SymbolTable[i];
outword(SymTabBuf, Sym.NameIdx); SymTabOut.outword(Sym.NameIdx);
outaddr32(SymTabBuf, Sym.Value); SymTabOut.outaddr32(Sym.Value);
outword(SymTabBuf, Sym.Size); SymTabOut.outword(Sym.Size);
outbyte(SymTabBuf, Sym.Info); SymTabOut.outbyte(Sym.Info);
outbyte(SymTabBuf, Sym.Other); SymTabOut.outbyte(Sym.Other);
outhalf(SymTabBuf, Sym.SectionIdx); SymTabOut.outhalf(Sym.SectionIdx);
} }
} else { } else {
for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) { for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) {
ELFSym &Sym = SymbolTable[i]; ELFSym &Sym = SymbolTable[i];
outword(SymTabBuf, Sym.NameIdx); SymTabOut.outword(Sym.NameIdx);
outbyte(SymTabBuf, Sym.Info); SymTabOut.outbyte(Sym.Info);
outbyte(SymTabBuf, Sym.Other); SymTabOut.outbyte(Sym.Other);
outhalf(SymTabBuf, Sym.SectionIdx); SymTabOut.outhalf(Sym.SectionIdx);
outaddr64(SymTabBuf, Sym.Value); SymTabOut.outaddr64(Sym.Value);
outxword(SymTabBuf, Sym.Size); SymTabOut.outxword(Sym.Size);
} }
} }
@ -420,7 +425,8 @@ void ELFWriter::EmitSectionTableStringTable() {
// Now that we know which section number is the .shstrtab section, update the // Now that we know which section number is the .shstrtab section, update the
// e_shstrndx entry in the ELF header. // e_shstrndx entry in the ELF header.
fixhalf(FileHeader, SHStrTab.SectionIdx, ELFHeader_e_shstrndx_Offset); OutputBuffer FHOut(TM, FileHeader);
FHOut.fixhalf(SHStrTab.SectionIdx, ELFHeader_e_shstrndx_Offset);
// Set the NameIdx of each section in the string table and emit the bytes for // Set the NameIdx of each section in the string table and emit the bytes for
// the string table. // the string table.
@ -471,11 +477,12 @@ void ELFWriter::OutputSectionsAndSectionTable() {
// Now that we know where all of the sections will be emitted, set the e_shnum // Now that we know where all of the sections will be emitted, set the e_shnum
// entry in the ELF header. // entry in the ELF header.
fixhalf(FileHeader, NumSections, ELFHeader_e_shnum_Offset); OutputBuffer FHOut(TM, FileHeader);
FHOut.fixhalf(NumSections, ELFHeader_e_shnum_Offset);
// Now that we know the offset in the file of the section table, update the // Now that we know the offset in the file of the section table, update the
// e_shoff address in the ELF header. // e_shoff address in the ELF header.
fixaddr(FileHeader, FileOff, ELFHeader_e_shoff_Offset); FHOut.fixaddr(FileOff, ELFHeader_e_shoff_Offset);
// Now that we know all of the data in the file header, emit it and all of the // Now that we know all of the data in the file header, emit it and all of the
// sections! // sections!
@ -484,6 +491,7 @@ void ELFWriter::OutputSectionsAndSectionTable() {
DataBuffer().swap(FileHeader); DataBuffer().swap(FileHeader);
DataBuffer Table; DataBuffer Table;
OutputBuffer TableOut(TM, Table);
// Emit all of the section data and build the section table itself. // Emit all of the section data and build the section table itself.
while (!SectionList.empty()) { while (!SectionList.empty()) {
@ -497,16 +505,16 @@ void ELFWriter::OutputSectionsAndSectionTable() {
O.write((char*)&S.SectionData[0], S.SectionData.size()); O.write((char*)&S.SectionData[0], S.SectionData.size());
FileOff += S.SectionData.size(); FileOff += S.SectionData.size();
outword(Table, S.NameIdx); // sh_name - Symbol table name idx TableOut.outword(S.NameIdx); // sh_name - Symbol table name idx
outword(Table, S.Type); // sh_type - Section contents & semantics TableOut.outword(S.Type); // sh_type - Section contents & semantics
outword(Table, S.Flags); // sh_flags - Section flags. TableOut.outword(S.Flags); // sh_flags - Section flags.
outaddr(Table, S.Addr); // sh_addr - The mem addr this section is in. TableOut.outaddr(S.Addr); // sh_addr - The mem addr this section is in.
outaddr(Table, S.Offset); // sh_offset - Offset from the file start. TableOut.outaddr(S.Offset); // sh_offset - Offset from the file start.
outword(Table, S.Size); // sh_size - The section size. TableOut.outword(S.Size); // sh_size - The section size.
outword(Table, S.Link); // sh_link - Section header table index link. TableOut.outword(S.Link); // sh_link - Section header table index link.
outword(Table, S.Info); // sh_info - Auxillary information. TableOut.outword(S.Info); // sh_info - Auxillary information.
outword(Table, S.Align); // sh_addralign - Alignment of section. TableOut.outword(S.Align); // sh_addralign - Alignment of section.
outword(Table, S.EntSize); // sh_entsize - Size of entries in the section. TableOut.outword(S.EntSize); // sh_entsize - Size of entries in the section
SectionList.pop_front(); SectionList.pop_front();
} }

View File

@ -34,6 +34,7 @@
#include "llvm/Target/TargetJITInfo.h" #include "llvm/Target/TargetJITInfo.h"
#include "llvm/Support/Mangler.h" #include "llvm/Support/Mangler.h"
#include "llvm/Support/MathExtras.h" #include "llvm/Support/MathExtras.h"
#include "llvm/Support/OutputBuffer.h"
#include "llvm/Support/Streams.h" #include "llvm/Support/Streams.h"
#include <algorithm> #include <algorithm>
@ -49,6 +50,9 @@ namespace llvm {
class MachOCodeEmitter : public MachineCodeEmitter { class MachOCodeEmitter : public MachineCodeEmitter {
MachOWriter &MOW; MachOWriter &MOW;
/// Target machine description.
TargetMachine &TM;
/// Relocations - These are the relocations that the function needs, as /// Relocations - These are the relocations that the function needs, as
/// emitted. /// emitted.
std::vector<MachineRelocation> Relocations; std::vector<MachineRelocation> Relocations;
@ -71,7 +75,7 @@ namespace llvm {
std::vector<intptr_t> MBBLocations; std::vector<intptr_t> MBBLocations;
public: public:
MachOCodeEmitter(MachOWriter &mow) : MOW(mow) {} MachOCodeEmitter(MachOWriter &mow) : MOW(mow), TM(MOW.TM) {}
virtual void startFunction(MachineFunction &F); virtual void startFunction(MachineFunction &F);
virtual bool finishFunction(MachineFunction &F); virtual bool finishFunction(MachineFunction &F);
@ -163,7 +167,7 @@ bool MachOCodeEmitter::finishFunction(MachineFunction &F) {
// Get a symbol for the function to add to the symbol table // Get a symbol for the function to add to the symbol table
const GlobalValue *FuncV = F.getFunction(); 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) // Emit constant pool to appropriate section(s)
emitConstantPool(F.getConstantPool()); emitConstantPool(F.getConstantPool());
@ -211,7 +215,7 @@ void MachOCodeEmitter::emitConstantPool(MachineConstantPool *MCP) {
if (CP.empty()) return; if (CP.empty()) return;
// FIXME: handle PIC codegen // 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!"); 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 // Although there is no strict necessity that I am aware of, we will do what
@ -223,9 +227,11 @@ void MachOCodeEmitter::emitConstantPool(MachineConstantPool *MCP) {
// "giant object for PIC" optimization. // "giant object for PIC" optimization.
for (unsigned i = 0, e = CP.size(); i != e; ++i) { for (unsigned i = 0, e = CP.size(); i != e; ++i) {
const Type *Ty = CP[i].getType(); 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); MachOWriter::MachOSection *Sec = MOW.getConstSection(Ty);
OutputBuffer SecDataOut(TM, Sec->SectionData);
CPLocations.push_back(Sec->SectionData.size()); CPLocations.push_back(Sec->SectionData.size());
CPSections.push_back(Sec->Index); CPSections.push_back(Sec->Index);
@ -236,10 +242,10 @@ void MachOCodeEmitter::emitConstantPool(MachineConstantPool *MCP) {
// FIXME: need alignment? // FIXME: need alignment?
// FIXME: share between here and AddSymbolToSection? // FIXME: share between here and AddSymbolToSection?
for (unsigned j = 0; j < Size; ++j) for (unsigned j = 0; j < Size; ++j)
MOW.outbyte(Sec->SectionData, 0); SecDataOut.outbyte(0);
MOW.InitMem(CP[i].Val.ConstVal, &Sec->SectionData[0], CPLocations[i], MOW.InitMem(CP[i].Val.ConstVal, &Sec->SectionData[0], CPLocations[i],
MOW.TM.getTargetData(), Sec->Relocations); TM.getTargetData(), Sec->Relocations);
} }
} }
@ -250,11 +256,12 @@ void MachOCodeEmitter::emitJumpTables(MachineJumpTableInfo *MJTI) {
if (JT.empty()) return; if (JT.empty()) return;
// FIXME: handle PIC codegen // 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!"); assert(!isPIC && "PIC codegen not yet handled for mach-o jump tables!");
MachOWriter::MachOSection *Sec = MOW.getJumpTableSection(); MachOWriter::MachOSection *Sec = MOW.getJumpTableSection();
unsigned TextSecIndex = MOW.getTextSection()->Index; unsigned TextSecIndex = MOW.getTextSection()->Index;
OutputBuffer SecDataOut(TM, Sec->SectionData);
for (unsigned i = 0, e = JT.size(); i != e; ++i) { for (unsigned i = 0, e = JT.size(); i != e; ++i) {
// For each jump table, record its offset from the start of the section, // For each jump table, record its offset from the start of the section,
@ -267,7 +274,7 @@ void MachOCodeEmitter::emitJumpTables(MachineJumpTableInfo *MJTI) {
MR.setResultPointer((void *)JTLocations[i]); MR.setResultPointer((void *)JTLocations[i]);
MR.setConstantVal(TextSecIndex); MR.setConstantVal(TextSecIndex);
Sec->Relocations.push_back(MR); Sec->Relocations.push_back(MR);
MOW.outaddr(Sec->SectionData, 0); SecDataOut.outaddr(0);
} }
} }
// FIXME: remove when we have unified size + output buffer // FIXME: remove when we have unified size + output buffer
@ -302,6 +309,8 @@ void MachOWriter::AddSymbolToSection(MachOSection *Sec, GlobalVariable *GV) {
// Reserve space in the .bss section for this symbol while maintaining the // Reserve space in the .bss section for this symbol while maintaining the
// desired section alignment, which must be at least as much as required by // desired section alignment, which must be at least as much as required by
// this symbol. // this symbol.
OutputBuffer SecDataOut(TM, Sec->SectionData);
if (Align) { if (Align) {
uint64_t OrigSize = Sec->size; uint64_t OrigSize = Sec->size;
Align = Log2_32(Align); Align = Log2_32(Align);
@ -312,7 +321,7 @@ void MachOWriter::AddSymbolToSection(MachOSection *Sec, GlobalVariable *GV) {
// FIXME: remove when we have unified size + output buffer // FIXME: remove when we have unified size + output buffer
unsigned AlignedSize = Sec->size - OrigSize; unsigned AlignedSize = Sec->size - OrigSize;
for (unsigned i = 0; i < AlignedSize; ++i) for (unsigned i = 0; i < AlignedSize; ++i)
outbyte(Sec->SectionData, 0); SecDataOut.outbyte(0);
} }
// Record the offset of the symbol, and then allocate space for it. // Record the offset of the symbol, and then allocate space for it.
// FIXME: remove when we have unified size + output buffer // FIXME: remove when we have unified size + output buffer
@ -329,7 +338,7 @@ void MachOWriter::AddSymbolToSection(MachOSection *Sec, GlobalVariable *GV) {
// Allocate space in the section for the global. // Allocate space in the section for the global.
for (unsigned i = 0; i < Size; ++i) for (unsigned i = 0; i < Size; ++i)
outbyte(Sec->SectionData, 0); SecDataOut.outbyte(0);
} }
void MachOWriter::EmitGlobal(GlobalVariable *GV) { void MachOWriter::EmitGlobal(GlobalVariable *GV) {
@ -442,15 +451,17 @@ void MachOWriter::EmitHeaderAndLoadCommands() {
// Step #3: write the header to the file // Step #3: write the header to the file
// Local alias to shortenify coming code. // Local alias to shortenify coming code.
DataBuffer &FH = Header.HeaderData; DataBuffer &FH = Header.HeaderData;
outword(FH, Header.magic); OutputBuffer FHOut(TM, FH);
outword(FH, Header.cputype);
outword(FH, Header.cpusubtype); FHOut.outword(Header.magic);
outword(FH, Header.filetype); FHOut.outword(Header.cputype);
outword(FH, Header.ncmds); FHOut.outword(Header.cpusubtype);
outword(FH, Header.sizeofcmds); FHOut.outword(Header.filetype);
outword(FH, Header.flags); FHOut.outword(Header.ncmds);
FHOut.outword(Header.sizeofcmds);
FHOut.outword(Header.flags);
if (is64Bit) if (is64Bit)
outword(FH, Header.reserved); FHOut.outword(Header.reserved);
// Step #4: Finish filling in the segment load command and write it out // Step #4: Finish filling in the segment load command and write it out
for (std::vector<MachOSection*>::iterator I = SectionList.begin(), for (std::vector<MachOSection*>::iterator I = SectionList.begin(),
@ -460,17 +471,17 @@ void MachOWriter::EmitHeaderAndLoadCommands() {
SEG.vmsize = SEG.filesize; SEG.vmsize = SEG.filesize;
SEG.fileoff = Header.cmdSize(is64Bit) + Header.sizeofcmds; SEG.fileoff = Header.cmdSize(is64Bit) + Header.sizeofcmds;
outword(FH, SEG.cmd); FHOut.outword(SEG.cmd);
outword(FH, SEG.cmdsize); FHOut.outword(SEG.cmdsize);
outstring(FH, SEG.segname, 16); FHOut.outstring(SEG.segname, 16);
outaddr(FH, SEG.vmaddr); FHOut.outaddr(SEG.vmaddr);
outaddr(FH, SEG.vmsize); FHOut.outaddr(SEG.vmsize);
outaddr(FH, SEG.fileoff); FHOut.outaddr(SEG.fileoff);
outaddr(FH, SEG.filesize); FHOut.outaddr(SEG.filesize);
outword(FH, SEG.maxprot); FHOut.outword(SEG.maxprot);
outword(FH, SEG.initprot); FHOut.outword(SEG.initprot);
outword(FH, SEG.nsects); FHOut.outword(SEG.nsects);
outword(FH, SEG.flags); FHOut.outword(SEG.flags);
// Step #5: Finish filling in the fields of the MachOSections // Step #5: Finish filling in the fields of the MachOSections
uint64_t currentAddr = 0; uint64_t currentAddr = 0;
@ -497,19 +508,19 @@ void MachOWriter::EmitHeaderAndLoadCommands() {
currentAddr += MOS->nreloc * 8; currentAddr += MOS->nreloc * 8;
// write the finalized section command to the output buffer // write the finalized section command to the output buffer
outstring(FH, MOS->sectname, 16); FHOut.outstring(MOS->sectname, 16);
outstring(FH, MOS->segname, 16); FHOut.outstring(MOS->segname, 16);
outaddr(FH, MOS->addr); FHOut.outaddr(MOS->addr);
outaddr(FH, MOS->size); FHOut.outaddr(MOS->size);
outword(FH, MOS->offset); FHOut.outword(MOS->offset);
outword(FH, MOS->align); FHOut.outword(MOS->align);
outword(FH, MOS->reloff); FHOut.outword(MOS->reloff);
outword(FH, MOS->nreloc); FHOut.outword(MOS->nreloc);
outword(FH, MOS->flags); FHOut.outword(MOS->flags);
outword(FH, MOS->reserved1); FHOut.outword(MOS->reserved1);
outword(FH, MOS->reserved2); FHOut.outword(MOS->reserved2);
if (is64Bit) if (is64Bit)
outword(FH, MOS->reserved3); FHOut.outword(MOS->reserved3);
} }
// Step #7: Emit the symbol table to temporary buffers, so that we know the // Step #7: Emit the symbol table to temporary buffers, so that we know the
@ -521,36 +532,36 @@ void MachOWriter::EmitHeaderAndLoadCommands() {
SymTab.nsyms = SymbolTable.size(); SymTab.nsyms = SymbolTable.size();
SymTab.stroff = SymTab.symoff + SymT.size(); SymTab.stroff = SymTab.symoff + SymT.size();
SymTab.strsize = StrT.size(); SymTab.strsize = StrT.size();
outword(FH, SymTab.cmd); FHOut.outword(SymTab.cmd);
outword(FH, SymTab.cmdsize); FHOut.outword(SymTab.cmdsize);
outword(FH, SymTab.symoff); FHOut.outword(SymTab.symoff);
outword(FH, SymTab.nsyms); FHOut.outword(SymTab.nsyms);
outword(FH, SymTab.stroff); FHOut.outword(SymTab.stroff);
outword(FH, SymTab.strsize); FHOut.outword(SymTab.strsize);
// FIXME: set DySymTab fields appropriately // FIXME: set DySymTab fields appropriately
// We should probably just update these in BufferSymbolAndStringTable since // We should probably just update these in BufferSymbolAndStringTable since
// thats where we're partitioning up the different kinds of symbols. // thats where we're partitioning up the different kinds of symbols.
outword(FH, DySymTab.cmd); FHOut.outword(DySymTab.cmd);
outword(FH, DySymTab.cmdsize); FHOut.outword(DySymTab.cmdsize);
outword(FH, DySymTab.ilocalsym); FHOut.outword(DySymTab.ilocalsym);
outword(FH, DySymTab.nlocalsym); FHOut.outword(DySymTab.nlocalsym);
outword(FH, DySymTab.iextdefsym); FHOut.outword(DySymTab.iextdefsym);
outword(FH, DySymTab.nextdefsym); FHOut.outword(DySymTab.nextdefsym);
outword(FH, DySymTab.iundefsym); FHOut.outword(DySymTab.iundefsym);
outword(FH, DySymTab.nundefsym); FHOut.outword(DySymTab.nundefsym);
outword(FH, DySymTab.tocoff); FHOut.outword(DySymTab.tocoff);
outword(FH, DySymTab.ntoc); FHOut.outword(DySymTab.ntoc);
outword(FH, DySymTab.modtaboff); FHOut.outword(DySymTab.modtaboff);
outword(FH, DySymTab.nmodtab); FHOut.outword(DySymTab.nmodtab);
outword(FH, DySymTab.extrefsymoff); FHOut.outword(DySymTab.extrefsymoff);
outword(FH, DySymTab.nextrefsyms); FHOut.outword(DySymTab.nextrefsyms);
outword(FH, DySymTab.indirectsymoff); FHOut.outword(DySymTab.indirectsymoff);
outword(FH, DySymTab.nindirectsyms); FHOut.outword(DySymTab.nindirectsyms);
outword(FH, DySymTab.extreloff); FHOut.outword(DySymTab.extreloff);
outword(FH, DySymTab.nextrel); FHOut.outword(DySymTab.nextrel);
outword(FH, DySymTab.locreloff); FHOut.outword(DySymTab.locreloff);
outword(FH, DySymTab.nlocrel); FHOut.outword(DySymTab.nlocrel);
O.write((char*)&FH[0], FH.size()); O.write((char*)&FH[0], FH.size());
} }
@ -627,7 +638,8 @@ void MachOWriter::BufferSymbolAndStringTable() {
// Write out a leading zero byte when emitting string table, for n_strx == 0 // Write out a leading zero byte when emitting string table, for n_strx == 0
// which means an empty string. // which means an empty string.
outbyte(StrT, 0); OutputBuffer StrTOut(TM, StrT);
StrTOut.outbyte(0);
// The order of the string table is: // The order of the string table is:
// 1. strings for external symbols // 1. strings for external symbols
@ -640,10 +652,12 @@ void MachOWriter::BufferSymbolAndStringTable() {
I->n_strx = 0; I->n_strx = 0;
} else { } else {
I->n_strx = StrT.size(); I->n_strx = StrT.size();
outstring(StrT, I->GVName, I->GVName.length()+1); StrTOut.outstring(I->GVName, I->GVName.length()+1);
} }
} }
OutputBuffer SymTOut(TM, SymT);
for (std::vector<MachOSym>::iterator I = SymbolTable.begin(), for (std::vector<MachOSym>::iterator I = SymbolTable.begin(),
E = SymbolTable.end(); I != E; ++I) { E = SymbolTable.end(); I != E; ++I) {
// Add the section base address to the section offset in the n_value field // Add the section base address to the section offset in the n_value field
@ -654,11 +668,11 @@ void MachOWriter::BufferSymbolAndStringTable() {
I->n_value += GVSection[GV]->addr; I->n_value += GVSection[GV]->addr;
// Emit nlist to buffer // Emit nlist to buffer
outword(SymT, I->n_strx); SymTOut.outword(I->n_strx);
outbyte(SymT, I->n_type); SymTOut.outbyte(I->n_type);
outbyte(SymT, I->n_sect); SymTOut.outbyte(I->n_sect);
outhalf(SymT, I->n_desc); SymTOut.outhalf(I->n_desc);
outaddr(SymT, I->n_value); SymTOut.outaddr(I->n_value);
} }
} }

View File

@ -17,6 +17,7 @@
#include "llvm/PassManager.h" #include "llvm/PassManager.h"
#include "llvm/CodeGen/MachOWriter.h" #include "llvm/CodeGen/MachOWriter.h"
#include "llvm/Support/Compiler.h" #include "llvm/Support/Compiler.h"
#include "llvm/Support/OutputBuffer.h"
using namespace llvm; using namespace llvm;
namespace { namespace {
@ -91,24 +92,36 @@ void PPCMachOWriter::GetTargetRelocation(MachineRelocation &MR,
MachORelocation VANILLA(MR.getMachineCodeOffset(), To.Index, false, 2, MachORelocation VANILLA(MR.getMachineCodeOffset(), To.Index, false, 2,
isExtern, PPC_RELOC_VANILLA); isExtern, PPC_RELOC_VANILLA);
++From.nreloc; ++From.nreloc;
outword(From.RelocBuffer, VANILLA.r_address);
outword(From.RelocBuffer, VANILLA.getPackedFields()); OutputBuffer RelocOut(TM, From.RelocBuffer);
RelocOut.outword(VANILLA.r_address);
RelocOut.outword(VANILLA.getPackedFields());
OutputBuffer SecOut(TM, From.SectionData);
SecOut.fixword(Addr, MR.getMachineCodeOffset());
break;
} }
fixword(From.SectionData, Addr, MR.getMachineCodeOffset());
break;
case PPC::reloc_pcrel_bx: case PPC::reloc_pcrel_bx:
Addr -= MR.getMachineCodeOffset(); {
Addr >>= 2; Addr -= MR.getMachineCodeOffset();
Addr &= 0xFFFFFF; Addr >>= 2;
Addr <<= 2; Addr &= 0xFFFFFF;
Addr |= (From.SectionData[MR.getMachineCodeOffset()] << 24); Addr <<= 2;
fixword(From.SectionData, Addr, MR.getMachineCodeOffset()); Addr |= (From.SectionData[MR.getMachineCodeOffset()] << 24);
break;
OutputBuffer SecOut(TM, From.SectionData);
SecOut.fixword(Addr, MR.getMachineCodeOffset());
break;
}
case PPC::reloc_pcrel_bcx: case PPC::reloc_pcrel_bcx:
Addr -= MR.getMachineCodeOffset(); {
Addr &= 0xFFFC; Addr -= MR.getMachineCodeOffset();
fixhalf(From.SectionData, Addr, MR.getMachineCodeOffset() + 2); Addr &= 0xFFFC;
break;
OutputBuffer SecOut(TM, From.SectionData);
SecOut.fixhalf(Addr, MR.getMachineCodeOffset() + 2);
break;
}
case PPC::reloc_absolute_high: case PPC::reloc_absolute_high:
{ {
MachORelocation HA16(MR.getMachineCodeOffset(), To.Index, false, 2, MachORelocation HA16(MR.getMachineCodeOffset(), To.Index, false, 2,
@ -117,15 +130,19 @@ void PPCMachOWriter::GetTargetRelocation(MachineRelocation &MR,
PPC_RELOC_PAIR); PPC_RELOC_PAIR);
++From.nreloc; ++From.nreloc;
++From.nreloc; ++From.nreloc;
outword(From.RelocBuffer, HA16.r_address);
outword(From.RelocBuffer, HA16.getPackedFields()); OutputBuffer RelocOut(TM, From.RelocBuffer);
outword(From.RelocBuffer, PAIR.r_address); RelocOut.outword(HA16.r_address);
outword(From.RelocBuffer, PAIR.getPackedFields()); RelocOut.outword(HA16.getPackedFields());
RelocOut.outword(PAIR.r_address);
RelocOut.outword(PAIR.getPackedFields());
printf("ha16: %x\n", (unsigned)Addr);
Addr += 0x8000;
OutputBuffer SecOut(TM, From.SectionData);
SecOut.fixhalf(Addr >> 16, MR.getMachineCodeOffset() + 2);
break;
} }
printf("ha16: %x\n", (unsigned)Addr);
Addr += 0x8000;
fixhalf(From.SectionData, Addr >> 16, MR.getMachineCodeOffset() + 2);
break;
case PPC::reloc_absolute_low: case PPC::reloc_absolute_low:
{ {
MachORelocation LO16(MR.getMachineCodeOffset(), To.Index, false, 2, MachORelocation LO16(MR.getMachineCodeOffset(), To.Index, false, 2,
@ -134,14 +151,18 @@ void PPCMachOWriter::GetTargetRelocation(MachineRelocation &MR,
PPC_RELOC_PAIR); PPC_RELOC_PAIR);
++From.nreloc; ++From.nreloc;
++From.nreloc; ++From.nreloc;
outword(From.RelocBuffer, LO16.r_address);
outword(From.RelocBuffer, LO16.getPackedFields()); OutputBuffer RelocOut(TM, From.RelocBuffer);
outword(From.RelocBuffer, PAIR.r_address); RelocOut.outword(LO16.r_address);
outword(From.RelocBuffer, PAIR.getPackedFields()); RelocOut.outword(LO16.getPackedFields());
RelocOut.outword(PAIR.r_address);
RelocOut.outword(PAIR.getPackedFields());
printf("lo16: %x\n", (unsigned)Addr);
OutputBuffer SecOut(TM, From.SectionData);
SecOut.fixhalf(Addr, MR.getMachineCodeOffset() + 2);
break;
} }
printf("lo16: %x\n", (unsigned)Addr);
fixhalf(From.SectionData, Addr, MR.getMachineCodeOffset() + 2);
break;
} }
} }