2005-06-27 14:29:00 +08:00
|
|
|
//===-- ELFWriter.cpp - Target-independent ELF Writer code ----------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2005-07-17 01:36:04 +08:00
|
|
|
// This file was developed by Chris Lattner and is distributed under the
|
|
|
|
// University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-06-27 14:29:00 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the target-independent ELF writer. This file writes out
|
|
|
|
// the ELF file in the following order:
|
|
|
|
//
|
|
|
|
// #1. ELF Header
|
2005-07-07 15:02:20 +08:00
|
|
|
// #2. '.text' section
|
|
|
|
// #3. '.data' section
|
|
|
|
// #4. '.bss' section (conceptual position in file)
|
2005-06-27 14:29:00 +08:00
|
|
|
// ...
|
|
|
|
// #X. '.shstrtab' section
|
|
|
|
// #Y. Section Table
|
|
|
|
//
|
|
|
|
// The entries in the section table are laid out as:
|
|
|
|
// #0. Null entry [required]
|
2005-07-07 15:02:20 +08:00
|
|
|
// #1. ".text" entry - the program code
|
|
|
|
// #2. ".data" entry - global variables with initializers. [ if needed ]
|
|
|
|
// #3. ".bss" entry - global variables without initializers. [ if needed ]
|
2005-06-27 14:29:00 +08:00
|
|
|
// ...
|
|
|
|
// #N. ".shstrtab" entry - String table for the section names.
|
|
|
|
//
|
|
|
|
// NOTE: This code should eventually be extended to support 64-bit ELF (this
|
|
|
|
// won't be hard), but we haven't done so yet!
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-02-08 09:35:27 +08:00
|
|
|
#include "ELFWriter.h"
|
2005-06-27 14:29:00 +08:00
|
|
|
#include "llvm/Module.h"
|
2007-02-08 09:35:27 +08:00
|
|
|
#include "llvm/PassManager.h"
|
|
|
|
#include "llvm/CodeGen/FileWriters.h"
|
2005-07-11 13:17:18 +08:00
|
|
|
#include "llvm/CodeGen/MachineCodeEmitter.h"
|
|
|
|
#include "llvm/CodeGen/MachineConstantPool.h"
|
2007-02-08 09:35:27 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2006-05-12 14:33:49 +08:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2007-01-27 10:55:44 +08:00
|
|
|
#include "llvm/Target/TargetELFWriterInfo.h"
|
2005-06-27 14:29:00 +08:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2005-07-11 11:11:47 +08:00
|
|
|
#include "llvm/Support/Mangler.h"
|
2007-01-18 06:22:31 +08:00
|
|
|
#include "llvm/Support/OutputBuffer.h"
|
2006-11-29 08:39:47 +08:00
|
|
|
#include "llvm/Support/Streams.h"
|
2007-02-08 09:35:27 +08:00
|
|
|
#include <list>
|
2005-06-27 14:29:00 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2007-02-08 09:35:27 +08:00
|
|
|
/// AddELFWriter - Concrete function to add the ELF writer to the function pass
|
|
|
|
/// manager.
|
|
|
|
MachineCodeEmitter *llvm::AddELFWriter(FunctionPassManager &FPM,
|
|
|
|
std::ostream &O,
|
|
|
|
TargetMachine &TM) {
|
|
|
|
ELFWriter *EW = new ELFWriter(O, TM);
|
|
|
|
FPM.add(EW);
|
|
|
|
return &EW->getMachineCodeEmitter();
|
|
|
|
}
|
|
|
|
|
2005-07-11 14:34:30 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ELFCodeEmitter Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2005-07-11 13:17:18 +08:00
|
|
|
namespace llvm {
|
2005-07-11 14:34:30 +08:00
|
|
|
/// ELFCodeEmitter - This class is used by the ELFWriter to emit the code for
|
|
|
|
/// functions to the ELF file.
|
2005-07-11 13:17:18 +08:00
|
|
|
class ELFCodeEmitter : public MachineCodeEmitter {
|
|
|
|
ELFWriter &EW;
|
2007-01-18 06:22:31 +08:00
|
|
|
TargetMachine &TM;
|
2005-07-16 16:01:13 +08:00
|
|
|
ELFWriter::ELFSection *ES; // Section to write to.
|
|
|
|
std::vector<unsigned char> *OutBuffer;
|
2005-07-11 13:17:18 +08:00
|
|
|
size_t FnStart;
|
|
|
|
public:
|
2007-01-18 06:22:31 +08:00
|
|
|
ELFCodeEmitter(ELFWriter &ew) : EW(ew), TM(EW.TM), OutBuffer(0) {}
|
2005-07-11 13:17:18 +08:00
|
|
|
|
2005-07-11 14:34:30 +08:00
|
|
|
void startFunction(MachineFunction &F);
|
2006-05-03 02:27:26 +08:00
|
|
|
bool finishFunction(MachineFunction &F);
|
2005-07-11 14:17:35 +08:00
|
|
|
|
2005-07-11 13:17:18 +08:00
|
|
|
void addRelocation(const MachineRelocation &MR) {
|
|
|
|
assert(0 && "relo not handled yet!");
|
|
|
|
}
|
2006-05-04 01:10:41 +08:00
|
|
|
|
|
|
|
virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) {
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual intptr_t getConstantPoolEntryAddress(unsigned Index) const {
|
2005-07-11 13:17:18 +08:00
|
|
|
assert(0 && "CP not implementated yet!");
|
2005-07-12 10:53:33 +08:00
|
|
|
return 0;
|
2005-07-11 13:17:18 +08:00
|
|
|
}
|
2006-05-04 01:10:41 +08:00
|
|
|
virtual intptr_t getJumpTableEntryAddress(unsigned Index) const {
|
2006-04-23 02:53:45 +08:00
|
|
|
assert(0 && "JT not implementated yet!");
|
|
|
|
return 0;
|
|
|
|
}
|
2006-05-04 01:10:41 +08:00
|
|
|
|
|
|
|
virtual intptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const {
|
2006-05-03 07:22:24 +08:00
|
|
|
assert(0 && "JT not implementated yet!");
|
2006-05-04 01:10:41 +08:00
|
|
|
return 0;
|
2006-05-03 07:22:24 +08:00
|
|
|
}
|
|
|
|
|
2005-07-11 14:34:30 +08:00
|
|
|
/// JIT SPECIFIC FUNCTIONS - DO NOT IMPLEMENT THESE HERE!
|
2006-11-17 04:04:04 +08:00
|
|
|
void startFunctionStub(unsigned StubSize, unsigned Alignment = 1) {
|
2005-07-11 13:17:18 +08:00
|
|
|
assert(0 && "JIT specific function called!");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
void *finishFunctionStub(const Function *F) {
|
|
|
|
assert(0 && "JIT specific function called!");
|
|
|
|
abort();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2005-07-11 14:34:30 +08:00
|
|
|
/// startFunction - This callback is invoked when a new machine function is
|
|
|
|
/// about to be emitted.
|
|
|
|
void ELFCodeEmitter::startFunction(MachineFunction &F) {
|
|
|
|
// Align the output buffer to the appropriate alignment.
|
|
|
|
unsigned Align = 16; // FIXME: GENERICIZE!!
|
2005-07-16 16:01:13 +08:00
|
|
|
// Get the ELF Section that this function belongs in.
|
2005-07-17 01:36:04 +08:00
|
|
|
ES = &EW.getSection(".text", ELFWriter::ELFSection::SHT_PROGBITS,
|
|
|
|
ELFWriter::ELFSection::SHF_EXECINSTR |
|
|
|
|
ELFWriter::ELFSection::SHF_ALLOC);
|
2005-07-16 16:01:13 +08:00
|
|
|
OutBuffer = &ES->SectionData;
|
2007-01-18 06:22:31 +08:00
|
|
|
cerr << "FIXME: This code needs to be updated for changes in the "
|
|
|
|
<< "CodeEmitter interfaces. In particular, this should set "
|
2006-12-07 09:30:32 +08:00
|
|
|
<< "BufferBegin/BufferEnd/CurBufferPtr, not deal with OutBuffer!";
|
2006-05-03 02:27:26 +08:00
|
|
|
abort();
|
2005-07-27 14:12:32 +08:00
|
|
|
|
2005-07-11 14:34:30 +08:00
|
|
|
// Upgrade the section alignment if required.
|
2005-07-16 16:01:13 +08:00
|
|
|
if (ES->Align < Align) ES->Align = Align;
|
2005-07-27 14:12:32 +08:00
|
|
|
|
2005-07-11 14:34:30 +08:00
|
|
|
// 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.
|
2007-01-18 09:23:11 +08:00
|
|
|
OutputBuffer OB(*OutBuffer,
|
|
|
|
TM.getTargetData()->getPointerSizeInBits() == 64,
|
|
|
|
TM.getTargetData()->isLittleEndian());
|
2007-01-18 06:22:31 +08:00
|
|
|
OB.align(Align);
|
2005-07-16 16:01:13 +08:00
|
|
|
FnStart = OutBuffer->size();
|
2005-07-11 14:34:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// finishFunction - This callback is invoked after the function is completely
|
|
|
|
/// finished.
|
2006-05-03 02:27:26 +08:00
|
|
|
bool ELFCodeEmitter::finishFunction(MachineFunction &F) {
|
2005-07-11 14:34:30 +08:00
|
|
|
// We now know the size of the function, add a symbol to represent it.
|
|
|
|
ELFWriter::ELFSym FnSym(F.getFunction());
|
2005-07-27 14:12:32 +08:00
|
|
|
|
2005-07-11 14:34:30 +08:00
|
|
|
// Figure out the binding (linkage) of the symbol.
|
|
|
|
switch (F.getFunction()->getLinkage()) {
|
|
|
|
default:
|
|
|
|
// appending linkage is illegal for functions.
|
|
|
|
assert(0 && "Unknown linkage type!");
|
|
|
|
case GlobalValue::ExternalLinkage:
|
|
|
|
FnSym.SetBind(ELFWriter::ELFSym::STB_GLOBAL);
|
|
|
|
break;
|
|
|
|
case GlobalValue::LinkOnceLinkage:
|
|
|
|
case GlobalValue::WeakLinkage:
|
|
|
|
FnSym.SetBind(ELFWriter::ELFSym::STB_WEAK);
|
|
|
|
break;
|
|
|
|
case GlobalValue::InternalLinkage:
|
|
|
|
FnSym.SetBind(ELFWriter::ELFSym::STB_LOCAL);
|
|
|
|
break;
|
|
|
|
}
|
2005-07-16 16:01:13 +08:00
|
|
|
|
|
|
|
ES->Size = OutBuffer->size();
|
|
|
|
|
2005-07-11 14:34:30 +08:00
|
|
|
FnSym.SetType(ELFWriter::ELFSym::STT_FUNC);
|
2005-07-16 16:01:13 +08:00
|
|
|
FnSym.SectionIdx = ES->SectionIdx;
|
2007-01-18 06:22:31 +08:00
|
|
|
FnSym.Value = FnStart; // Value = Offset from start of Section.
|
2005-07-16 16:01:13 +08:00
|
|
|
FnSym.Size = OutBuffer->size()-FnStart;
|
2005-07-27 14:12:32 +08:00
|
|
|
|
2005-07-11 14:34:30 +08:00
|
|
|
// Finally, add it to the symtab.
|
|
|
|
EW.SymbolTable.push_back(FnSym);
|
2006-05-03 02:27:26 +08:00
|
|
|
return false;
|
2005-07-11 14:34:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ELFWriter Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
2005-07-11 13:17:18 +08:00
|
|
|
|
2005-06-27 14:29:00 +08:00
|
|
|
ELFWriter::ELFWriter(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) {
|
|
|
|
e_flags = 0; // e_flags defaults to 0, no flags.
|
|
|
|
|
2006-05-03 09:29:57 +08:00
|
|
|
is64Bit = TM.getTargetData()->getPointerSizeInBits() == 64;
|
|
|
|
isLittleEndian = TM.getTargetData()->isLittleEndian();
|
2005-07-11 13:17:18 +08:00
|
|
|
|
|
|
|
// Create the machine code emitter object for this target.
|
2007-01-17 17:06:13 +08:00
|
|
|
MCE = new ELFCodeEmitter(*this);
|
2005-07-16 16:01:13 +08:00
|
|
|
NumSections = 0;
|
2005-07-11 13:17:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ELFWriter::~ELFWriter() {
|
|
|
|
delete MCE;
|
2005-06-27 14:29:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// doInitialization - Emit the file header and all of the global variables for
|
|
|
|
// the module to the ELF file.
|
|
|
|
bool ELFWriter::doInitialization(Module &M) {
|
2005-07-11 11:11:47 +08:00
|
|
|
Mang = new Mangler(M);
|
|
|
|
|
2005-07-16 16:01:13 +08:00
|
|
|
// Local alias to shortenify coming code.
|
|
|
|
std::vector<unsigned char> &FH = FileHeader;
|
2007-01-18 09:23:11 +08:00
|
|
|
OutputBuffer FHOut(FH, is64Bit, isLittleEndian);
|
2007-01-18 06:22:31 +08:00
|
|
|
|
|
|
|
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
|
2007-01-17 17:06:13 +08:00
|
|
|
FH.resize(16); // EI_PAD up to 16 bytes.
|
2005-07-27 14:12:32 +08:00
|
|
|
|
2005-06-27 14:29:00 +08:00
|
|
|
// This should change for shared objects.
|
2007-01-18 06:22:31 +08:00
|
|
|
FHOut.outhalf(1); // e_type = ET_REL
|
2007-01-27 10:55:44 +08:00
|
|
|
FHOut.outword(TM.getELFWriterInfo()->getEMachine()); // target-defined
|
2007-01-18 06:22:31 +08:00
|
|
|
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
|
2005-06-27 14:29:00 +08:00
|
|
|
|
2005-07-16 16:01:13 +08:00
|
|
|
ELFHeader_e_shoff_Offset = FH.size();
|
2007-01-18 06:22:31 +08:00
|
|
|
FHOut.outaddr(0); // e_shoff
|
|
|
|
FHOut.outword(e_flags); // e_flags = whatever the target wants
|
2005-06-27 14:29:00 +08:00
|
|
|
|
2007-01-18 06:22:31 +08:00
|
|
|
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
|
2005-06-27 14:29:00 +08:00
|
|
|
|
2005-07-27 14:12:32 +08:00
|
|
|
|
2005-07-16 16:01:13 +08:00
|
|
|
ELFHeader_e_shnum_Offset = FH.size();
|
2007-01-18 06:22:31 +08:00
|
|
|
FHOut.outhalf(0); // e_shnum = # of section header ents
|
2005-07-16 16:01:13 +08:00
|
|
|
ELFHeader_e_shstrndx_Offset = FH.size();
|
2007-01-18 06:22:31 +08:00
|
|
|
FHOut.outhalf(0); // e_shstrndx = Section # of '.shstrtab'
|
2005-06-27 14:29:00 +08:00
|
|
|
|
2005-07-16 16:01:13 +08:00
|
|
|
// Add the null section, which is required to be first in the file.
|
2005-07-17 01:36:04 +08:00
|
|
|
getSection("", 0, 0);
|
2005-06-27 14:29:00 +08:00
|
|
|
|
2005-07-07 15:02:20 +08:00
|
|
|
// Start up the symbol table. The first entry in the symtab is the null
|
|
|
|
// entry.
|
|
|
|
SymbolTable.push_back(ELFSym(0));
|
2005-06-27 14:29:00 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-07-17 01:41:06 +08:00
|
|
|
void ELFWriter::EmitGlobal(GlobalVariable *GV) {
|
2005-07-08 13:47:00 +08:00
|
|
|
// If this is an external global, emit it now. TODO: Note that it would be
|
|
|
|
// better to ignore the symbol here and only add it to the symbol table if
|
|
|
|
// referenced.
|
|
|
|
if (!GV->hasInitializer()) {
|
|
|
|
ELFSym ExternalSym(GV);
|
|
|
|
ExternalSym.SetBind(ELFSym::STB_GLOBAL);
|
|
|
|
ExternalSym.SetType(ELFSym::STT_NOTYPE);
|
|
|
|
ExternalSym.SectionIdx = ELFSection::SHN_UNDEF;
|
|
|
|
SymbolTable.push_back(ExternalSym);
|
|
|
|
return;
|
|
|
|
}
|
2005-07-27 14:12:32 +08:00
|
|
|
|
2005-07-08 13:47:00 +08:00
|
|
|
const Type *GVType = (const Type*)GV->getType();
|
2007-02-14 13:52:17 +08:00
|
|
|
unsigned Align = TM.getTargetData()->getPrefTypeAlignment(GVType);
|
2006-05-03 09:29:57 +08:00
|
|
|
unsigned Size = TM.getTargetData()->getTypeSize(GVType);
|
2005-07-08 13:47:00 +08:00
|
|
|
|
2005-07-07 15:02:20 +08:00
|
|
|
// If this global has a zero initializer, it is part of the .bss or common
|
|
|
|
// section.
|
|
|
|
if (GV->getInitializer()->isNullValue()) {
|
|
|
|
// If this global is part of the common block, add it now. Variables are
|
|
|
|
// part of the common block if they are zero initialized and allowed to be
|
|
|
|
// merged with other symbols.
|
|
|
|
if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage()) {
|
|
|
|
ELFSym CommonSym(GV);
|
|
|
|
// Value for common symbols is the alignment required.
|
2005-07-08 13:47:00 +08:00
|
|
|
CommonSym.Value = Align;
|
|
|
|
CommonSym.Size = Size;
|
2005-07-07 15:02:20 +08:00
|
|
|
CommonSym.SetBind(ELFSym::STB_GLOBAL);
|
|
|
|
CommonSym.SetType(ELFSym::STT_OBJECT);
|
|
|
|
// TODO SOMEDAY: add ELF visibility.
|
|
|
|
CommonSym.SectionIdx = ELFSection::SHN_COMMON;
|
|
|
|
SymbolTable.push_back(CommonSym);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-07-08 13:47:00 +08:00
|
|
|
// Otherwise, this symbol is part of the .bss section. Emit it now.
|
|
|
|
|
|
|
|
// Handle alignment. Ensure section is aligned at least as much as required
|
|
|
|
// by this symbol.
|
2005-07-17 01:41:06 +08:00
|
|
|
ELFSection &BSSSection = getBSSSection();
|
2005-07-08 13:47:00 +08:00
|
|
|
BSSSection.Align = std::max(BSSSection.Align, Align);
|
|
|
|
|
|
|
|
// Within the section, emit enough virtual padding to get us to an alignment
|
|
|
|
// boundary.
|
|
|
|
if (Align)
|
|
|
|
BSSSection.Size = (BSSSection.Size + Align - 1) & ~(Align-1);
|
|
|
|
|
|
|
|
ELFSym BSSSym(GV);
|
|
|
|
BSSSym.Value = BSSSection.Size;
|
|
|
|
BSSSym.Size = Size;
|
|
|
|
BSSSym.SetType(ELFSym::STT_OBJECT);
|
|
|
|
|
|
|
|
switch (GV->getLinkage()) {
|
|
|
|
default: // weak/linkonce handled above
|
|
|
|
assert(0 && "Unexpected linkage type!");
|
|
|
|
case GlobalValue::AppendingLinkage: // FIXME: This should be improved!
|
|
|
|
case GlobalValue::ExternalLinkage:
|
|
|
|
BSSSym.SetBind(ELFSym::STB_GLOBAL);
|
|
|
|
break;
|
|
|
|
case GlobalValue::InternalLinkage:
|
|
|
|
BSSSym.SetBind(ELFSym::STB_LOCAL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the idx of the .bss section
|
2005-07-16 16:01:13 +08:00
|
|
|
BSSSym.SectionIdx = BSSSection.SectionIdx;
|
2005-07-08 13:47:00 +08:00
|
|
|
SymbolTable.push_back(BSSSym);
|
|
|
|
|
|
|
|
// Reserve space in the .bss section for this symbol.
|
|
|
|
BSSSection.Size += Size;
|
2005-07-07 15:02:20 +08:00
|
|
|
return;
|
|
|
|
}
|
2005-06-27 14:29:00 +08:00
|
|
|
|
2005-07-07 15:02:20 +08:00
|
|
|
// FIXME: handle .rodata
|
|
|
|
//assert(!GV->isConstant() && "unimp");
|
2005-06-27 14:29:00 +08:00
|
|
|
|
2005-07-07 15:02:20 +08:00
|
|
|
// FIXME: handle .data
|
|
|
|
//assert(0 && "unimp");
|
2005-06-27 14:29:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ELFWriter::runOnMachineFunction(MachineFunction &MF) {
|
2005-07-11 13:17:18 +08:00
|
|
|
// Nothing to do here, this is all done through the MCE object above.
|
2005-06-27 14:29:00 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// doFinalization - Now that the module has been completely processed, emit
|
|
|
|
/// the ELF file to 'O'.
|
|
|
|
bool ELFWriter::doFinalization(Module &M) {
|
2005-07-07 15:02:20 +08:00
|
|
|
// Okay, the ELF header and .text sections have been completed, build the
|
|
|
|
// .data, .bss, and "common" sections next.
|
2005-07-16 16:01:13 +08:00
|
|
|
for (Module::global_iterator I = M.global_begin(), E = M.global_end();
|
|
|
|
I != E; ++I)
|
2005-07-17 01:41:06 +08:00
|
|
|
EmitGlobal(I);
|
2005-07-07 15:02:20 +08:00
|
|
|
|
|
|
|
// Emit the symbol table now, if non-empty.
|
|
|
|
EmitSymbolTable();
|
|
|
|
|
|
|
|
// FIXME: Emit the relocations now.
|
|
|
|
|
2005-06-27 14:29:00 +08:00
|
|
|
// Emit the string table for the sections in the ELF file we have.
|
|
|
|
EmitSectionTableStringTable();
|
|
|
|
|
2005-07-16 16:01:13 +08:00
|
|
|
// Emit the sections to the .o file, and emit the section table for the file.
|
|
|
|
OutputSectionsAndSectionTable();
|
2005-06-27 14:29:00 +08:00
|
|
|
|
2005-07-16 16:01:13 +08:00
|
|
|
// We are done with the abstract symbols.
|
|
|
|
SectionList.clear();
|
|
|
|
NumSections = 0;
|
2005-07-11 11:11:47 +08:00
|
|
|
|
|
|
|
// Release the name mangler object.
|
|
|
|
delete Mang; Mang = 0;
|
2005-06-27 14:29:00 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-07-07 15:02:20 +08:00
|
|
|
/// EmitSymbolTable - If the current symbol table is non-empty, emit the string
|
|
|
|
/// table for it and then the symbol table itself.
|
|
|
|
void ELFWriter::EmitSymbolTable() {
|
|
|
|
if (SymbolTable.size() == 1) return; // Only the null entry.
|
|
|
|
|
|
|
|
// FIXME: compact all local symbols to the start of the symtab.
|
|
|
|
unsigned FirstNonLocalSymbol = 1;
|
|
|
|
|
2005-07-17 01:36:04 +08:00
|
|
|
ELFSection &StrTab = getSection(".strtab", ELFSection::SHT_STRTAB, 0);
|
2005-07-07 15:02:20 +08:00
|
|
|
StrTab.Align = 1;
|
|
|
|
|
2005-07-16 16:01:13 +08:00
|
|
|
DataBuffer &StrTabBuf = StrTab.SectionData;
|
2007-01-18 09:23:11 +08:00
|
|
|
OutputBuffer StrTabOut(StrTabBuf, is64Bit, isLittleEndian);
|
2005-07-16 16:01:13 +08:00
|
|
|
|
2005-07-07 15:02:20 +08:00
|
|
|
// Set the zero'th symbol to a null byte, as required.
|
2007-01-18 06:22:31 +08:00
|
|
|
StrTabOut.outbyte(0);
|
2005-07-07 15:02:20 +08:00
|
|
|
SymbolTable[0].NameIdx = 0;
|
|
|
|
unsigned Index = 1;
|
|
|
|
for (unsigned i = 1, e = SymbolTable.size(); i != e; ++i) {
|
2005-07-11 11:11:47 +08:00
|
|
|
// Use the name mangler to uniquify the LLVM symbol.
|
|
|
|
std::string Name = Mang->getValueName(SymbolTable[i].GV);
|
2005-07-07 15:02:20 +08:00
|
|
|
|
|
|
|
if (Name.empty()) {
|
|
|
|
SymbolTable[i].NameIdx = 0;
|
|
|
|
} else {
|
|
|
|
SymbolTable[i].NameIdx = Index;
|
|
|
|
|
|
|
|
// Add the name to the output buffer, including the null terminator.
|
2005-07-16 16:01:13 +08:00
|
|
|
StrTabBuf.insert(StrTabBuf.end(), Name.begin(), Name.end());
|
2005-07-07 15:02:20 +08:00
|
|
|
|
|
|
|
// Add a null terminator.
|
2005-07-16 16:01:13 +08:00
|
|
|
StrTabBuf.push_back(0);
|
2005-07-07 15:02:20 +08:00
|
|
|
|
|
|
|
// Keep track of the number of bytes emitted to this section.
|
|
|
|
Index += Name.size()+1;
|
|
|
|
}
|
|
|
|
}
|
2005-07-16 16:01:13 +08:00
|
|
|
assert(Index == StrTabBuf.size());
|
|
|
|
StrTab.Size = Index;
|
2005-07-07 15:02:20 +08:00
|
|
|
|
|
|
|
// Now that we have emitted the string table and know the offset into the
|
|
|
|
// string table of each symbol, emit the symbol table itself.
|
2005-07-17 01:36:04 +08:00
|
|
|
ELFSection &SymTab = getSection(".symtab", ELFSection::SHT_SYMTAB, 0);
|
2005-07-12 14:57:52 +08:00
|
|
|
SymTab.Align = is64Bit ? 8 : 4;
|
2005-07-16 16:01:13 +08:00
|
|
|
SymTab.Link = SymTab.SectionIdx; // Section Index of .strtab.
|
2005-07-07 15:02:20 +08:00
|
|
|
SymTab.Info = FirstNonLocalSymbol; // First non-STB_LOCAL symbol.
|
|
|
|
SymTab.EntSize = 16; // Size of each symtab entry. FIXME: wrong for ELF64
|
2005-07-16 16:01:13 +08:00
|
|
|
DataBuffer &SymTabBuf = SymTab.SectionData;
|
2007-01-18 09:23:11 +08:00
|
|
|
OutputBuffer SymTabOut(SymTabBuf, is64Bit, isLittleEndian);
|
2005-07-07 15:02:20 +08:00
|
|
|
|
2005-07-12 14:57:52 +08:00
|
|
|
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];
|
2007-01-18 06:22:31 +08:00
|
|
|
SymTabOut.outword(Sym.NameIdx);
|
|
|
|
SymTabOut.outaddr32(Sym.Value);
|
|
|
|
SymTabOut.outword(Sym.Size);
|
|
|
|
SymTabOut.outbyte(Sym.Info);
|
|
|
|
SymTabOut.outbyte(Sym.Other);
|
|
|
|
SymTabOut.outhalf(Sym.SectionIdx);
|
2005-07-12 14:57:52 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) {
|
|
|
|
ELFSym &Sym = SymbolTable[i];
|
2007-01-18 06:22:31 +08:00
|
|
|
SymTabOut.outword(Sym.NameIdx);
|
|
|
|
SymTabOut.outbyte(Sym.Info);
|
|
|
|
SymTabOut.outbyte(Sym.Other);
|
|
|
|
SymTabOut.outhalf(Sym.SectionIdx);
|
|
|
|
SymTabOut.outaddr64(Sym.Value);
|
|
|
|
SymTabOut.outxword(Sym.Size);
|
2005-07-12 14:57:52 +08:00
|
|
|
}
|
2005-07-07 15:02:20 +08:00
|
|
|
}
|
|
|
|
|
2005-07-16 16:01:13 +08:00
|
|
|
SymTab.Size = SymTabBuf.size();
|
2005-07-07 15:02:20 +08:00
|
|
|
}
|
|
|
|
|
2005-06-27 14:29:00 +08:00
|
|
|
/// EmitSectionTableStringTable - This method adds and emits a section for the
|
|
|
|
/// ELF Section Table string table: the string table that holds all of the
|
|
|
|
/// section names.
|
|
|
|
void ELFWriter::EmitSectionTableStringTable() {
|
|
|
|
// First step: add the section for the string table to the list of sections:
|
2005-07-17 01:36:04 +08:00
|
|
|
ELFSection &SHStrTab = getSection(".shstrtab", ELFSection::SHT_STRTAB, 0);
|
2005-06-27 14:29:00 +08:00
|
|
|
|
|
|
|
// Now that we know which section number is the .shstrtab section, update the
|
|
|
|
// e_shstrndx entry in the ELF header.
|
2007-01-18 09:23:11 +08:00
|
|
|
OutputBuffer FHOut(FileHeader, is64Bit, isLittleEndian);
|
2007-01-18 06:22:31 +08:00
|
|
|
FHOut.fixhalf(SHStrTab.SectionIdx, ELFHeader_e_shstrndx_Offset);
|
2005-06-27 14:29:00 +08:00
|
|
|
|
|
|
|
// Set the NameIdx of each section in the string table and emit the bytes for
|
|
|
|
// the string table.
|
|
|
|
unsigned Index = 0;
|
2005-07-16 16:01:13 +08:00
|
|
|
DataBuffer &Buf = SHStrTab.SectionData;
|
2005-06-27 14:29:00 +08:00
|
|
|
|
2005-07-16 16:01:13 +08:00
|
|
|
for (std::list<ELFSection>::iterator I = SectionList.begin(),
|
|
|
|
E = SectionList.end(); I != E; ++I) {
|
2005-06-27 14:29:00 +08:00
|
|
|
// Set the index into the table. Note if we have lots of entries with
|
|
|
|
// common suffixes, we could memoize them here if we cared.
|
2005-07-16 16:01:13 +08:00
|
|
|
I->NameIdx = Index;
|
2005-06-27 14:29:00 +08:00
|
|
|
|
|
|
|
// Add the name to the output buffer, including the null terminator.
|
2005-07-16 16:01:13 +08:00
|
|
|
Buf.insert(Buf.end(), I->Name.begin(), I->Name.end());
|
|
|
|
|
2005-06-27 14:29:00 +08:00
|
|
|
// Add a null terminator.
|
2005-07-16 16:01:13 +08:00
|
|
|
Buf.push_back(0);
|
2005-06-27 14:29:00 +08:00
|
|
|
|
|
|
|
// Keep track of the number of bytes emitted to this section.
|
2005-07-16 16:01:13 +08:00
|
|
|
Index += I->Name.size()+1;
|
2005-06-27 14:29:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the size of .shstrtab now that we know what it is.
|
2005-07-16 16:01:13 +08:00
|
|
|
assert(Index == Buf.size());
|
|
|
|
SHStrTab.Size = Index;
|
2005-06-27 14:29:00 +08:00
|
|
|
}
|
|
|
|
|
2005-07-16 16:01:13 +08:00
|
|
|
/// OutputSectionsAndSectionTable - Now that we have constructed the file header
|
|
|
|
/// and all of the sections, emit these to the ostream destination and emit the
|
|
|
|
/// SectionTable.
|
|
|
|
void ELFWriter::OutputSectionsAndSectionTable() {
|
|
|
|
// Pass #1: Compute the file offset for each section.
|
|
|
|
size_t FileOff = FileHeader.size(); // File header first.
|
|
|
|
|
|
|
|
// Emit all of the section data in order.
|
|
|
|
for (std::list<ELFSection>::iterator I = SectionList.begin(),
|
|
|
|
E = SectionList.end(); I != E; ++I) {
|
|
|
|
// Align FileOff to whatever the alignment restrictions of the section are.
|
|
|
|
if (I->Align)
|
|
|
|
FileOff = (FileOff+I->Align-1) & ~(I->Align-1);
|
|
|
|
I->Offset = FileOff;
|
|
|
|
FileOff += I->SectionData.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Align Section Header.
|
|
|
|
unsigned TableAlign = is64Bit ? 8 : 4;
|
|
|
|
FileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
|
|
|
|
|
|
|
|
// Now that we know where all of the sections will be emitted, set the e_shnum
|
|
|
|
// entry in the ELF header.
|
2007-01-18 09:23:11 +08:00
|
|
|
OutputBuffer FHOut(FileHeader, is64Bit, isLittleEndian);
|
2007-01-18 06:22:31 +08:00
|
|
|
FHOut.fixhalf(NumSections, ELFHeader_e_shnum_Offset);
|
2005-07-27 14:12:32 +08:00
|
|
|
|
2005-07-16 16:01:13 +08:00
|
|
|
// Now that we know the offset in the file of the section table, update the
|
|
|
|
// e_shoff address in the ELF header.
|
2007-01-18 06:22:31 +08:00
|
|
|
FHOut.fixaddr(FileOff, ELFHeader_e_shoff_Offset);
|
2005-07-27 14:12:32 +08:00
|
|
|
|
2005-07-16 16:01:13 +08:00
|
|
|
// Now that we know all of the data in the file header, emit it and all of the
|
|
|
|
// sections!
|
|
|
|
O.write((char*)&FileHeader[0], FileHeader.size());
|
|
|
|
FileOff = FileHeader.size();
|
|
|
|
DataBuffer().swap(FileHeader);
|
|
|
|
|
|
|
|
DataBuffer Table;
|
2007-01-18 09:23:11 +08:00
|
|
|
OutputBuffer TableOut(Table, is64Bit, isLittleEndian);
|
2005-07-16 16:01:13 +08:00
|
|
|
|
|
|
|
// Emit all of the section data and build the section table itself.
|
|
|
|
while (!SectionList.empty()) {
|
|
|
|
const ELFSection &S = *SectionList.begin();
|
|
|
|
|
|
|
|
// Align FileOff to whatever the alignment restrictions of the section are.
|
|
|
|
if (S.Align)
|
|
|
|
for (size_t NewFileOff = (FileOff+S.Align-1) & ~(S.Align-1);
|
|
|
|
FileOff != NewFileOff; ++FileOff)
|
2005-08-20 00:19:21 +08:00
|
|
|
O.put((char)0xAB);
|
2005-07-16 16:01:13 +08:00
|
|
|
O.write((char*)&S.SectionData[0], S.SectionData.size());
|
|
|
|
FileOff += S.SectionData.size();
|
|
|
|
|
2007-01-18 06:22:31 +08:00
|
|
|
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
|
2005-07-16 16:01:13 +08:00
|
|
|
|
|
|
|
SectionList.pop_front();
|
2005-06-27 14:29:00 +08:00
|
|
|
}
|
|
|
|
|
2005-07-16 16:01:13 +08:00
|
|
|
// Align output for the section table.
|
|
|
|
for (size_t NewFileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
|
|
|
|
FileOff != NewFileOff; ++FileOff)
|
2005-08-20 00:19:21 +08:00
|
|
|
O.put((char)0xAB);
|
2005-07-27 14:12:32 +08:00
|
|
|
|
2005-07-16 16:01:13 +08:00
|
|
|
// Emit the section table itself.
|
|
|
|
O.write((char*)&Table[0], Table.size());
|
2005-06-27 14:29:00 +08:00
|
|
|
}
|