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_shstrndx_Offset; // e_shstrndx 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:
void EmitGlobal(GlobalVariable *GV);

View File

@ -659,101 +659,6 @@ namespace llvm {
/// SymbolTable to aid in emitting the DYSYMTAB load command.
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,
const TargetData *TD,
std::vector<MachineRelocation> &MRs);

View File

@ -38,6 +38,7 @@
#include "llvm/Target/TargetData.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Support/Mangler.h"
#include "llvm/Support/OutputBuffer.h"
#include "llvm/Support/Streams.h"
using namespace llvm;
@ -50,11 +51,12 @@ namespace llvm {
/// functions to the ELF file.
class ELFCodeEmitter : public MachineCodeEmitter {
ELFWriter &EW;
TargetMachine &TM;
ELFWriter::ELFSection *ES; // Section to write to.
std::vector<unsigned char> *OutBuffer;
size_t FnStart;
public:
ELFCodeEmitter(ELFWriter &ew) : EW(ew), OutBuffer(0) {}
ELFCodeEmitter(ELFWriter &ew) : EW(ew), TM(EW.TM), OutBuffer(0) {}
void startFunction(MachineFunction &F);
bool finishFunction(MachineFunction &F);
@ -103,8 +105,8 @@ void ELFCodeEmitter::startFunction(MachineFunction &F) {
ELFWriter::ELFSection::SHF_EXECINSTR |
ELFWriter::ELFSection::SHF_ALLOC);
OutBuffer = &ES->SectionData;
cerr << "FIXME: This code needs to be updated for changes in the"
<< " CodeEmitter interfaces. In particular, this should set "
cerr << "FIXME: This code needs to be updated for changes in the "
<< "CodeEmitter interfaces. In particular, this should set "
<< "BufferBegin/BufferEnd/CurBufferPtr, not deal with OutBuffer!";
abort();
@ -113,8 +115,8 @@ 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);
OutputBuffer OB(TM, *OutBuffer);
OB.align(Align);
FnStart = OutBuffer->size();
}
@ -145,7 +147,7 @@ bool ELFCodeEmitter::finishFunction(MachineFunction &F) {
FnSym.SetType(ELFWriter::ELFSym::STT_FUNC);
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;
// Finally, add it to the symtab.
@ -180,37 +182,38 @@ bool ELFWriter::doInitialization(Module &M) {
// Local alias to shortenify coming code.
std::vector<unsigned char> &FH = FileHeader;
OutputBuffer FHOut(TM, FH);
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
FHOut.outbyte(0x7F); // EI_MAG0
FHOut.outbyte('E'); // EI_MAG1
FHOut.outbyte('L'); // EI_MAG2
FHOut.outbyte('F'); // EI_MAG3
FHOut.outbyte(is64Bit ? 2 : 1); // EI_CLASS
FHOut.outbyte(isLittleEndian ? 1 : 2); // EI_DATA
FHOut.outbyte(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
FHOut.outhalf(1); // e_type = ET_REL
FHOut.outhalf(e_machine); // e_machine = whatever the target wants
FHOut.outword(1); // e_version = 1
FHOut.outaddr(0); // e_entry = 0 -> no entry point in .o file
FHOut.outaddr(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
FHOut.outaddr(0); // e_shoff
FHOut.outword(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
FHOut.outhalf(is64Bit ? 64 : 52); // e_ehsize = ELF header size
FHOut.outhalf(0); // e_phentsize = prog header entry size
FHOut.outhalf(0); // e_phnum = # prog header entries = 0
FHOut.outhalf(is64Bit ? 64 : 40); // e_shentsize = sect hdr entry 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();
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.
getSection("", 0, 0);
@ -350,9 +353,10 @@ void ELFWriter::EmitSymbolTable() {
StrTab.Align = 1;
DataBuffer &StrTabBuf = StrTab.SectionData;
OutputBuffer StrTabOut(TM, StrTabBuf);
// Set the zero'th symbol to a null byte, as required.
outbyte(StrTabBuf, 0);
StrTabOut.outbyte(0);
SymbolTable[0].NameIdx = 0;
unsigned Index = 1;
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.EntSize = 16; // Size of each symtab entry. FIXME: wrong for ELF64
DataBuffer &SymTabBuf = SymTab.SectionData;
OutputBuffer SymTabOut(TM, SymTabBuf);
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);
SymTabOut.outword(Sym.NameIdx);
SymTabOut.outaddr32(Sym.Value);
SymTabOut.outword(Sym.Size);
SymTabOut.outbyte(Sym.Info);
SymTabOut.outbyte(Sym.Other);
SymTabOut.outhalf(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);
SymTabOut.outword(Sym.NameIdx);
SymTabOut.outbyte(Sym.Info);
SymTabOut.outbyte(Sym.Other);
SymTabOut.outhalf(Sym.SectionIdx);
SymTabOut.outaddr64(Sym.Value);
SymTabOut.outxword(Sym.Size);
}
}
@ -420,7 +425,8 @@ 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);
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
// 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
// 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
// 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
// sections!
@ -484,6 +491,7 @@ void ELFWriter::OutputSectionsAndSectionTable() {
DataBuffer().swap(FileHeader);
DataBuffer Table;
OutputBuffer TableOut(TM, Table);
// Emit all of the section data and build the section table itself.
while (!SectionList.empty()) {
@ -497,16 +505,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.
TableOut.outword(S.NameIdx); // sh_name - Symbol table name idx
TableOut.outword(S.Type); // sh_type - Section contents & semantics
TableOut.outword(S.Flags); // sh_flags - Section flags.
TableOut.outaddr(S.Addr); // sh_addr - The mem addr this section is in.
TableOut.outaddr(S.Offset); // sh_offset - Offset from the file start.
TableOut.outword(S.Size); // sh_size - The section size.
TableOut.outword(S.Link); // sh_link - Section header table index link.
TableOut.outword(S.Info); // sh_info - Auxillary information.
TableOut.outword(S.Align); // sh_addralign - Alignment of section.
TableOut.outword(S.EntSize); // sh_entsize - Size of entries in the section
SectionList.pop_front();
}

View File

@ -34,6 +34,7 @@
#include "llvm/Target/TargetJITInfo.h"
#include "llvm/Support/Mangler.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/OutputBuffer.h"
#include "llvm/Support/Streams.h"
#include <algorithm>
@ -49,6 +50,9 @@ namespace llvm {
class MachOCodeEmitter : public MachineCodeEmitter {
MachOWriter &MOW;
/// Target machine description.
TargetMachine &TM;
/// Relocations - These are the relocations that the function needs, as
/// emitted.
std::vector<MachineRelocation> Relocations;
@ -71,7 +75,7 @@ namespace llvm {
std::vector<intptr_t> MBBLocations;
public:
MachOCodeEmitter(MachOWriter &mow) : MOW(mow) {}
MachOCodeEmitter(MachOWriter &mow) : MOW(mow), TM(MOW.TM) {}
virtual void startFunction(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
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 +215,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,9 +227,11 @@ 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);
OutputBuffer SecDataOut(TM, Sec->SectionData);
CPLocations.push_back(Sec->SectionData.size());
CPSections.push_back(Sec->Index);
@ -236,10 +242,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);
SecDataOut.outbyte(0);
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;
// 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();
unsigned TextSecIndex = MOW.getTextSection()->Index;
OutputBuffer SecDataOut(TM, Sec->SectionData);
for (unsigned i = 0, e = JT.size(); i != e; ++i) {
// 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.setConstantVal(TextSecIndex);
Sec->Relocations.push_back(MR);
MOW.outaddr(Sec->SectionData, 0);
SecDataOut.outaddr(0);
}
}
// 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
// desired section alignment, which must be at least as much as required by
// this symbol.
OutputBuffer SecDataOut(TM, Sec->SectionData);
if (Align) {
uint64_t OrigSize = Sec->size;
Align = Log2_32(Align);
@ -312,7 +321,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);
SecDataOut.outbyte(0);
}
// Record the offset of the symbol, and then allocate space for it.
// 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.
for (unsigned i = 0; i < Size; ++i)
outbyte(Sec->SectionData, 0);
SecDataOut.outbyte(0);
}
void MachOWriter::EmitGlobal(GlobalVariable *GV) {
@ -442,15 +451,17 @@ 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);
OutputBuffer FHOut(TM, FH);
FHOut.outword(Header.magic);
FHOut.outword(Header.cputype);
FHOut.outword(Header.cpusubtype);
FHOut.outword(Header.filetype);
FHOut.outword(Header.ncmds);
FHOut.outword(Header.sizeofcmds);
FHOut.outword(Header.flags);
if (is64Bit)
outword(FH, Header.reserved);
FHOut.outword(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 +471,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);
FHOut.outword(SEG.cmd);
FHOut.outword(SEG.cmdsize);
FHOut.outstring(SEG.segname, 16);
FHOut.outaddr(SEG.vmaddr);
FHOut.outaddr(SEG.vmsize);
FHOut.outaddr(SEG.fileoff);
FHOut.outaddr(SEG.filesize);
FHOut.outword(SEG.maxprot);
FHOut.outword(SEG.initprot);
FHOut.outword(SEG.nsects);
FHOut.outword(SEG.flags);
// Step #5: Finish filling in the fields of the MachOSections
uint64_t currentAddr = 0;
@ -497,19 +508,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);
FHOut.outstring(MOS->sectname, 16);
FHOut.outstring(MOS->segname, 16);
FHOut.outaddr(MOS->addr);
FHOut.outaddr(MOS->size);
FHOut.outword(MOS->offset);
FHOut.outword(MOS->align);
FHOut.outword(MOS->reloff);
FHOut.outword(MOS->nreloc);
FHOut.outword(MOS->flags);
FHOut.outword(MOS->reserved1);
FHOut.outword(MOS->reserved2);
if (is64Bit)
outword(FH, MOS->reserved3);
FHOut.outword(MOS->reserved3);
}
// 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.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);
FHOut.outword(SymTab.cmd);
FHOut.outword(SymTab.cmdsize);
FHOut.outword(SymTab.symoff);
FHOut.outword(SymTab.nsyms);
FHOut.outword(SymTab.stroff);
FHOut.outword(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);
FHOut.outword(DySymTab.cmd);
FHOut.outword(DySymTab.cmdsize);
FHOut.outword(DySymTab.ilocalsym);
FHOut.outword(DySymTab.nlocalsym);
FHOut.outword(DySymTab.iextdefsym);
FHOut.outword(DySymTab.nextdefsym);
FHOut.outword(DySymTab.iundefsym);
FHOut.outword(DySymTab.nundefsym);
FHOut.outword(DySymTab.tocoff);
FHOut.outword(DySymTab.ntoc);
FHOut.outword(DySymTab.modtaboff);
FHOut.outword(DySymTab.nmodtab);
FHOut.outword(DySymTab.extrefsymoff);
FHOut.outword(DySymTab.nextrefsyms);
FHOut.outword(DySymTab.indirectsymoff);
FHOut.outword(DySymTab.nindirectsyms);
FHOut.outword(DySymTab.extreloff);
FHOut.outword(DySymTab.nextrel);
FHOut.outword(DySymTab.locreloff);
FHOut.outword(DySymTab.nlocrel);
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
// which means an empty string.
outbyte(StrT, 0);
OutputBuffer StrTOut(TM, StrT);
StrTOut.outbyte(0);
// The order of the string table is:
// 1. strings for external symbols
@ -640,10 +652,12 @@ void MachOWriter::BufferSymbolAndStringTable() {
I->n_strx = 0;
} else {
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(),
E = SymbolTable.end(); I != E; ++I) {
// 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;
// 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);
SymTOut.outword(I->n_strx);
SymTOut.outbyte(I->n_type);
SymTOut.outbyte(I->n_sect);
SymTOut.outhalf(I->n_desc);
SymTOut.outaddr(I->n_value);
}
}

View File

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