return false;
}
+// Get jump table section on the section name returned by TAI
+ELFSection &ELFWriter::getJumpTableSection() {
+ unsigned Align = TM.getTargetData()->getPointerABIAlignment();
+ return getSection(TAI->getJumpTableDataSection(),
+ ELFSection::SHT_PROGBITS,
+ ELFSection::SHF_ALLOC, Align);
+}
+
+// Get a constant pool section based on the section name returned by TAI
+ELFSection &ELFWriter::getConstantPoolSection(MachineConstantPoolEntry &CPE) {
+ uint64_t Size = TM.getTargetData()->getTypeAllocSize(CPE.getType());
+
+ std::string CstPoolName =
+ TAI->getSectionForMergableConstant(Size,CPE.getRelocationInfo())->getName();
+ return getSection(CstPoolName,
+ ELFSection::SHT_PROGBITS,
+ ELFSection::SHF_MERGE | ELFSection::SHF_ALLOC,
+ CPE.getAlignment());
+}
+
+// Return the relocation section of section 'S'. 'RelA' is true
+// if the relocation section contains entries with addends.
+ELFSection &ELFWriter::getRelocSection(ELFSection &S) {
+ unsigned SectionHeaderTy = TEW->hasRelocationAddend() ?
+ ELFSection::SHT_RELA : ELFSection::SHT_REL;
+ std::string RelSName(".rel");
+ if (TEW->hasRelocationAddend())
+ RelSName.append("a");
+ RelSName.append(S.getName());
+
+ return getSection(RelSName, SectionHeaderTy, 0, TEW->getPrefELFAlignment());
+}
+
// getGlobalELFVisibility - Returns the ELF specific visibility type
unsigned ELFWriter::getGlobalELFVisibility(const GlobalValue *GV) {
switch (GV->getVisibility()) {
// isELFBssSym - for an undef or null value, the symbol must go to a bss
// section if it's not weak for linker, otherwise it's a common sym.
-static bool isELFBssSym(const GlobalValue *GV) {
- return (!GV->isDeclaration() &&
- (GV->isNullValue() || isa<UndefValue>(GV)) &&
- !GV->isWeakForLinker());
+static bool isELFBssSym(const GlobalVariable *GV) {
+ const Constant *CV = GV->getInitializer();
+ return ((CV->isNullValue() || isa<UndefValue>(CV)) && !GV->isWeakForLinker());
}
// isELFCommonSym - for an undef or null value, the symbol must go to a
// common section if it's weak for linker, otherwise bss.
-static bool isELFCommonSym(const GlobalValue *GV) {
- return (!GV->isDeclaration() &&
- (GV->isNullValue() || isa<UndefValue>(GV))
- && GV->isWeakForLinker());
+static bool isELFCommonSym(const GlobalVariable *GV) {
+ const Constant *CV = GV->getInitializer();
+ return ((CV->isNullValue() || isa<UndefValue>(CV)) && GV->isWeakForLinker());
}
// isELFDataSym - if the symbol is an initialized but no null constant
// it must go to some kind of data section gathered from TAI
-static bool isELFDataSym(const GlobalValue *GV) {
- return (!GV->isDeclaration() &&
- !(GV->isNullValue() || isa<UndefValue>(GV)));
+static bool isELFDataSym(const Constant *CV) {
+ return (!(CV->isNullValue() || isa<UndefValue>(CV)));
}
// EmitGlobal - Choose the right section for global and emit it
void ELFWriter::EmitGlobal(const GlobalValue *GV) {
+ // Check if the referenced symbol is already emitted
+ if (GblSymLookup.find(GV) != GblSymLookup.end())
+ return;
+
// Handle ELF Bind, Visibility and Type for the current symbol
unsigned SymBind = getGlobalELFBinding(GV);
ELFSym *GblSym = new ELFSym(GV);
unsigned Size = TD->getTypeAllocSize(GVar->getInitializer()->getType());
GblSym->Size = Size;
- if (isELFCommonSym(GV)) {
+ if (isELFCommonSym(GVar)) {
GblSym->SectionIdx = ELFSection::SHN_COMMON;
getSection(S->getName(), ELFSection::SHT_NOBITS, SectionFlags, 1);
// value contains its alignment.
GblSym->Value = Align;
- } else if (isELFBssSym(GV)) {
+ } else if (isELFBssSym(GVar)) {
ELFSection &ES =
getSection(S->getName(), ELFSection::SHT_NOBITS, SectionFlags);
GblSym->SectionIdx = ES.SectionIdx;
}
}
- if (!GV->hasPrivateLinkage())
+ // Private symbols must never go to the symbol table.
+ unsigned SymIdx = 0;
+ if (GV->hasPrivateLinkage()) {
+ PrivateSyms.push_back(GblSym);
+ SymIdx = PrivateSyms.size()-1;
+ } else {
SymbolList.push_back(GblSym);
+ }
+
+ setGlobalSymLookup(GV, SymIdx);
}
void ELFWriter::EmitGlobalConstantStruct(const ConstantStruct *CVS,
for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
EmitGlobalConstant(CP->getOperand(I), GblS);
return;
+ } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
+ // This is a constant address for a global variable or function and
+ // therefore must be referenced using a relocation entry.
+
+ // Check if the referenced symbol is already emitted
+ if (GblSymLookup.find(GV) == GblSymLookup.end())
+ EmitGlobal(GV);
+
+ // Create the relocation entry for the global value
+ MachineRelocation MR =
+ MachineRelocation::getGV(GblS.getCurrentPCOffset(),
+ TEW->getAbsoluteLabelMachineRelTy(),
+ const_cast<GlobalValue*>(GV));
+
+ // Fill the data entry with zeros
+ for (unsigned i=0; i < Size; ++i)
+ GblS.emitByte(0);
+
+ // Add the relocation entry for the current data section
+ GblS.addRelocation(MR);
+ return;
+ } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
+ if (CE->getOpcode() == Instruction::BitCast) {
+ EmitGlobalConstant(CE->getOperand(0), GblS);
+ return;
+ }
+ // See AsmPrinter::EmitConstantValueOnly for other ConstantExpr types
+ llvm_unreachable("Unsupported ConstantExpr type");
}
- llvm_unreachable("unknown global constant");
+
+ llvm_unreachable("Unknown global constant type");
}
// Build and emit data, bss and "common" sections.
for (Module::global_iterator I = M.global_begin(), E = M.global_end();
- I != E; ++I) {
+ I != E; ++I)
EmitGlobal(I);
- GblSymLookup[I] = 0;
- }
// Emit all pending globals
- // TODO: this should be done only for referenced symbols
for (SetVector<GlobalValue*>::const_iterator I = PendingGlobals.begin(),
- E = PendingGlobals.end(); I != E; ++I) {
-
- // No need to emit the symbol again
- if (GblSymLookup.find(*I) != GblSymLookup.end())
- continue;
-
+ E = PendingGlobals.end(); I != E; ++I)
EmitGlobal(*I);
- GblSymLookup[*I] = 0;
- }
// Emit non-executable stack note
if (TAI->getNonexecutableStackDirective())
return false;
}
+// RelocateField - Patch relocatable field with 'Offset' in 'BO'
+// using a 'Value' of known 'Size'
+void ELFWriter::RelocateField(BinaryObject &BO, uint32_t Offset,
+ int64_t Value, unsigned Size) {
+ if (Size == 32)
+ BO.fixWord32(Value, Offset);
+ else if (Size == 64)
+ BO.fixWord64(Value, Offset);
+ else
+ llvm_unreachable("don't know howto patch relocatable field");
+}
+
/// EmitRelocations - Emit relocations
void ELFWriter::EmitRelocations() {
+ // True if the target uses the relocation entry to hold the addend,
+ // otherwise the addend is written directly to the relocatable field.
+ bool HasRelA = TEW->hasRelocationAddend();
+
// Create Relocation sections for each section which needs it.
- for (ELFSectionIter I=SectionList.begin(), E=SectionList.end(); I != E; ++I) {
- ELFSection &S = *(*I);
+ for (unsigned i=0, e=SectionList.size(); i != e; ++i) {
+ ELFSection &S = *SectionList[i];
// This section does not have relocations
if (!S.hasRelocations()) continue;
-
- // Get the relocation section for section 'S'
- bool HasRelA = TEW->hasRelocationAddend();
- ELFSection &RelSec = getRelocSection(S.getName(), HasRelA,
- TEW->getPrefELFAlignment());
+ ELFSection &RelSec = getRelocSection(S);
// 'Link' - Section hdr idx of the associated symbol table
// 'Info' - Section hdr idx of the section to which the relocation applies
MRE = Relos.end(); MRI != MRE; ++MRI) {
MachineRelocation &MR = *MRI;
- // Offset from the start of the section containing the symbol
- unsigned Offset = MR.getMachineCodeOffset();
+ // Relocatable field offset from the section start
+ unsigned RelOffset = MR.getMachineCodeOffset();
// Symbol index in the symbol table
unsigned SymIdx = 0;
- // Target specific ELF relocation type
+ // Target specific relocation field type and size
unsigned RelType = TEW->getRelocationType(MR.getRelocationType());
-
- // Constant addend used to compute the value to be stored
- // into the relocatable field
+ unsigned RelTySize = TEW->getRelocationTySize(RelType);
int64_t Addend = 0;
// There are several machine relocations types, and each one of
if (MR.isGlobalValue()) {
const GlobalValue *G = MR.getGlobalValue();
SymIdx = GblSymLookup[G];
- Addend = TEW->getAddendForRelTy(RelType);
+ if (G->hasPrivateLinkage()) {
+ // If the target uses a section offset in the relocation:
+ // SymIdx + Addend = section sym for global + section offset
+ unsigned SectionIdx = PrivateSyms[SymIdx]->SectionIdx;
+ Addend = PrivateSyms[SymIdx]->Value;
+ SymIdx = SectionList[SectionIdx]->getSymbolTableIndex();
+ } else {
+ Addend = TEW->getDefaultAddendForRelTy(RelType);
+ }
} else {
- // Get the symbol index for the section symbol referenced
- // by the relocation
+ // Get the symbol index for the section symbol
unsigned SectionIdx = MR.getConstantVal();
- SymIdx = SectionList[SectionIdx]->Sym->SymTabIdx;
+ SymIdx = SectionList[SectionIdx]->getSymbolTableIndex();
Addend = (uint64_t)MR.getResultPointer();
+
+ // For pc relative relocations where symbols are defined in the same
+ // section they are referenced, ignore the relocation entry and patch
+ // the relocatable field with the symbol offset directly.
+ if (S.SectionIdx == SectionIdx && TEW->isPCRelativeRel(RelType)) {
+ int64_t Value = TEW->computeRelocation(Addend, RelOffset, RelType);
+ RelocateField(S, RelOffset, Value, RelTySize);
+ continue;
+ }
+
+ // Handle Jump Table Index relocation
+ if ((SectionIdx == getJumpTableSection().SectionIdx) &&
+ TEW->hasCustomJumpTableIndexRelTy()) {
+ RelType = TEW->getJumpTableIndexRelTy();
+ RelTySize = TEW->getRelocationTySize(RelType);
+ }
}
+ // The target without addend on the relocation symbol must be
+ // patched in the relocation place itself to contain the addend
+ if (!HasRelA)
+ RelocateField(S, RelOffset, Addend, RelTySize);
+
// Get the relocation entry and emit to the relocation section
- ELFRelocation Rel(Offset, SymIdx, RelType, HasRelA, Addend);
+ ELFRelocation Rel(RelOffset, SymIdx, RelType, HasRelA, Addend);
EmitRelocation(RelSec, Rel, HasRelA);
}
}
// Set the zero'th symbol to a null byte, as required.
StrTab.emitByte(0);
- // Walk on the symbol list and write symbol names into the
- // string table.
+ // Walk on the symbol list and write symbol names into the string table.
unsigned Index = 1;
for (ELFSymIter I=SymbolList.begin(), E=SymbolList.end(); I != E; ++I) {
ELFSym &Sym = *(*I);
EmitSymbol(SymTab, Sym);
// Record the symbol table index for each global value
- if (Sym.GV) GblSymLookup[Sym.GV] = i;
+ if (Sym.GV) setGlobalSymLookup(Sym.GV, i);
// Keep track on the symbol index into the symbol table
Sym.SymTabIdx = i;