#ifndef LLVM_SUPPORT_OUTPUTBUFFER_H
#define LLVM_SUPPORT_OUTPUTBUFFER_H
+#include <string>
#include <vector>
namespace llvm {
/// machine directly, indicating what header values and flags to set.
bool is64Bit, isLittleEndian;
public:
- OutputBuffer(const TargetMachine& TM,
- std::vector<unsigned char> &Out) : Output(Out) {
- is64Bit = TM.getTargetData()->getPointerSizeInBits() == 64;
- isLittleEndian = TM.getTargetData()->isLittleEndian();
- }
+ OutputBuffer(std::vector<unsigned char> &Out,
+ bool is64bit, bool le)
+ : Output(Out), is64Bit(is64bit), isLittleEndian(le) {}
// align - Emit padding into the file until the current output position is
// aligned to the specified power of two boundary.
else
outxword(X);
}
- void outstring(std::string &S, unsigned Length) {
+ void outstring(const 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;
// 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.
- OutputBuffer OB(TM, *OutBuffer);
+ OutputBuffer OB(*OutBuffer,
+ TM.getTargetData()->getPointerSizeInBits() == 64,
+ TM.getTargetData()->isLittleEndian());
OB.align(Align);
FnStart = OutBuffer->size();
}
// Local alias to shortenify coming code.
std::vector<unsigned char> &FH = FileHeader;
- OutputBuffer FHOut(TM, FH);
+ OutputBuffer FHOut(FH, is64Bit, isLittleEndian);
FHOut.outbyte(0x7F); // EI_MAG0
FHOut.outbyte('E'); // EI_MAG1
StrTab.Align = 1;
DataBuffer &StrTabBuf = StrTab.SectionData;
- OutputBuffer StrTabOut(TM, StrTabBuf);
+ OutputBuffer StrTabOut(StrTabBuf, is64Bit, isLittleEndian);
// Set the zero'th symbol to a null byte, as required.
StrTabOut.outbyte(0);
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);
+ OutputBuffer SymTabOut(SymTabBuf, is64Bit, isLittleEndian);
if (!is64Bit) { // 32-bit and 64-bit formats are shuffled a bit.
for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) {
// Now that we know which section number is the .shstrtab section, update the
// e_shstrndx entry in the ELF header.
- OutputBuffer FHOut(TM, FileHeader);
+ OutputBuffer FHOut(FileHeader, is64Bit, isLittleEndian);
FHOut.fixhalf(SHStrTab.SectionIdx, ELFHeader_e_shstrndx_Offset);
// Set the NameIdx of each section in the string table and emit the bytes for
// Now that we know where all of the sections will be emitted, set the e_shnum
// entry in the ELF header.
- OutputBuffer FHOut(TM, FileHeader);
+ OutputBuffer FHOut(FileHeader, is64Bit, isLittleEndian);
FHOut.fixhalf(NumSections, ELFHeader_e_shnum_Offset);
// Now that we know the offset in the file of the section table, update the
DataBuffer().swap(FileHeader);
DataBuffer Table;
- OutputBuffer TableOut(TM, Table);
+ OutputBuffer TableOut(Table, is64Bit, isLittleEndian);
// Emit all of the section data and build the section table itself.
while (!SectionList.empty()) {
/// Target machine description.
TargetMachine &TM;
+ /// is64Bit/isLittleEndian - This information is inferred from the target
+ /// machine directly, indicating what header values and flags to set.
+ bool is64Bit, isLittleEndian;
+
/// Relocations - These are the relocations that the function needs, as
/// emitted.
std::vector<MachineRelocation> Relocations;
std::vector<intptr_t> MBBLocations;
public:
- MachOCodeEmitter(MachOWriter &mow) : MOW(mow), TM(MOW.TM) {}
+ MachOCodeEmitter(MachOWriter &mow) : MOW(mow), TM(MOW.TM) {
+ is64Bit = TM.getTargetData()->getPointerSizeInBits() == 64;
+ isLittleEndian = TM.getTargetData()->isLittleEndian();
+ }
virtual void startFunction(MachineFunction &F);
virtual bool finishFunction(MachineFunction &F);
unsigned Size = TM.getTargetData()->getTypeSize(Ty);
MachOWriter::MachOSection *Sec = MOW.getConstSection(Ty);
- OutputBuffer SecDataOut(TM, Sec->SectionData);
+ OutputBuffer SecDataOut(Sec->SectionData, is64Bit, isLittleEndian);
CPLocations.push_back(Sec->SectionData.size());
CPSections.push_back(Sec->Index);
MachOWriter::MachOSection *Sec = MOW.getJumpTableSection();
unsigned TextSecIndex = MOW.getTextSection()->Index;
- OutputBuffer SecDataOut(TM, Sec->SectionData);
+ OutputBuffer SecDataOut(Sec->SectionData, is64Bit, isLittleEndian);
for (unsigned i = 0, e = JT.size(); i != e; ++i) {
// For each jump table, record its offset from the start of the section,
// 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);
+ OutputBuffer SecDataOut(Sec->SectionData, is64Bit, isLittleEndian);
if (Align) {
uint64_t OrigSize = Sec->size;
// Step #3: write the header to the file
// Local alias to shortenify coming code.
DataBuffer &FH = Header.HeaderData;
- OutputBuffer FHOut(TM, FH);
+ OutputBuffer FHOut(FH, is64Bit, isLittleEndian);
FHOut.outword(Header.magic);
FHOut.outword(Header.cputype);
// Write out a leading zero byte when emitting string table, for n_strx == 0
// which means an empty string.
- OutputBuffer StrTOut(TM, StrT);
+ OutputBuffer StrTOut(StrT, is64Bit, isLittleEndian);
StrTOut.outbyte(0);
// The order of the string table is:
}
}
- OutputBuffer SymTOut(TM, SymT);
+ OutputBuffer SymTOut(SymT, is64Bit, isLittleEndian);
for (std::vector<MachOSym>::iterator I = SymbolTable.begin(),
E = SymbolTable.end(); I != E; ++I) {
isExtern, PPC_RELOC_VANILLA);
++From.nreloc;
- OutputBuffer RelocOut(TM, From.RelocBuffer);
+ OutputBuffer RelocOut(From.RelocBuffer, is64Bit, isLittleEndian);
RelocOut.outword(VANILLA.r_address);
RelocOut.outword(VANILLA.getPackedFields());
- OutputBuffer SecOut(TM, From.SectionData);
+ OutputBuffer SecOut(From.SectionData, is64Bit, isLittleEndian);
SecOut.fixword(Addr, MR.getMachineCodeOffset());
break;
}
Addr <<= 2;
Addr |= (From.SectionData[MR.getMachineCodeOffset()] << 24);
- OutputBuffer SecOut(TM, From.SectionData);
+ OutputBuffer SecOut(From.SectionData, is64Bit, isLittleEndian);
SecOut.fixword(Addr, MR.getMachineCodeOffset());
break;
}
Addr -= MR.getMachineCodeOffset();
Addr &= 0xFFFC;
- OutputBuffer SecOut(TM, From.SectionData);
+ OutputBuffer SecOut(From.SectionData, is64Bit, isLittleEndian);
SecOut.fixhalf(Addr, MR.getMachineCodeOffset() + 2);
break;
}
++From.nreloc;
++From.nreloc;
- OutputBuffer RelocOut(TM, From.RelocBuffer);
+ OutputBuffer RelocOut(From.RelocBuffer, is64Bit, isLittleEndian);
RelocOut.outword(HA16.r_address);
RelocOut.outword(HA16.getPackedFields());
RelocOut.outword(PAIR.r_address);
printf("ha16: %x\n", (unsigned)Addr);
Addr += 0x8000;
- OutputBuffer SecOut(TM, From.SectionData);
+ OutputBuffer SecOut(From.SectionData, is64Bit, isLittleEndian);
SecOut.fixhalf(Addr >> 16, MR.getMachineCodeOffset() + 2);
break;
}
++From.nreloc;
++From.nreloc;
- OutputBuffer RelocOut(TM, From.RelocBuffer);
+ OutputBuffer RelocOut(From.RelocBuffer, is64Bit, isLittleEndian);
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);
+ OutputBuffer SecOut(From.SectionData, is64Bit, isLittleEndian);
SecOut.fixhalf(Addr, MR.getMachineCodeOffset() + 2);
break;
}