#include <vector>
using namespace llvm;
+static unsigned GetType(const MCSymbolData &SD) {
+ uint32_t Type = (SD.getFlags() & (0xf << ELF_STT_Shift)) >> ELF_STT_Shift;
+ assert(Type == ELF::STT_NOTYPE || Type == ELF::STT_OBJECT ||
+ Type == ELF::STT_FUNC || Type == ELF::STT_SECTION ||
+ Type == ELF::STT_FILE || Type == ELF::STT_COMMON ||
+ Type == ELF::STT_TLS);
+ return Type;
+}
+
+static unsigned GetBinding(const MCSymbolData &SD) {
+ uint32_t Binding = (SD.getFlags() & (0xf << ELF_STB_Shift)) >> ELF_STB_Shift;
+ assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
+ Binding == ELF::STB_WEAK);
+ return Binding;
+}
+
+static void SetBinding(MCSymbolData &SD, unsigned Binding) {
+ assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
+ Binding == ELF::STB_WEAK);
+ uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STB_Shift);
+ SD.setFlags(OtherFlags | (Binding << ELF_STB_Shift));
+}
+
namespace {
class ELFObjectWriterImpl {
// Support lexicographic sorting.
bool operator<(const ELFSymbolData &RHS) const {
+ if (GetType(*SymbolData) == ELF::STT_FILE)
+ return true;
+ if (GetType(*RHS.SymbolData) == ELF::STT_FILE)
+ return false;
return SymbolData->getSymbol().getName() <
RHS.SymbolData->getSymbol().getName();
}
bool HasRelocationAddend;
+ Triple::OSType OSType;
+
// This holds the symbol table index of the last local symbol.
unsigned LastLocalSymbolIndex;
// This holds the .strtab section index.
public:
ELFObjectWriterImpl(ELFObjectWriter *_Writer, bool _Is64Bit,
- bool _HasRelAddend)
+ bool _HasRelAddend, Triple::OSType _OSType)
: Writer(_Writer), OS(Writer->getStream()),
- Is64Bit(_Is64Bit), HasRelocationAddend(_HasRelAddend) {
+ Is64Bit(_Is64Bit), HasRelocationAddend(_HasRelAddend),
+ OSType(_OSType) {
}
void Write8(uint8_t Value) { Writer->Write8(Value); }
const MCAsmLayout &Layout);
void WriteSymbolTable(MCDataFragment *F, const MCAssembler &Asm,
- const MCAsmLayout &Layout);
+ const MCAsmLayout &Layout,
+ unsigned NumRegularSections);
void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
const MCFragment *Fragment, const MCFixup &Fixup,
void WriteRelocationsFragment(const MCAssembler &Asm, MCDataFragment *F,
const MCSectionData *SD);
+ bool IsFixupFullyResolved(const MCAssembler &Asm,
+ const MCValue Target,
+ bool IsPCRel,
+ const MCFragment *DF) const;
+
void WriteObject(const MCAssembler &Asm, const MCAsmLayout &Layout);
};
Write8(Writer->isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
- Write8(ELF::ELFOSABI_LINUX); // e_ident[EI_OSABI]
+ // e_ident[EI_OSABI]
+ switch (OSType) {
+ case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
+ case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
+ default: Write8(ELF::ELFOSABI_NONE); break;
+ }
Write8(0); // e_ident[EI_ABIVERSION]
WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
}
}
+static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout) {
+ if (Data.isCommon() && Data.isExternal())
+ return Data.getCommonAlignment();
+
+ const MCSymbol &Symbol = Data.getSymbol();
+ if (!Symbol.isInSection())
+ return 0;
+
+ if (!Data.isCommon() && !(Data.getFlags() & ELF_STB_Weak))
+ if (MCFragment *FF = Data.getFragment())
+ return Layout.getSymbolAddress(&Data) -
+ Layout.getSectionAddress(FF->getParent());
+
+ return 0;
+}
+
void ELFObjectWriterImpl::WriteSymbol(MCDataFragment *F, ELFSymbolData &MSD,
const MCAsmLayout &Layout) {
MCSymbolData &Data = *MSD.SymbolData;
uint8_t Info = (Data.getFlags() & 0xff);
uint8_t Other = ((Data.getFlags() & 0xf00) >> ELF_STV_Shift);
- uint64_t Value = 0;
+ uint64_t Value = SymbolValue(Data, Layout);
uint64_t Size = 0;
const MCExpr *ESize;
- if (Data.isCommon() && Data.isExternal())
- Value = Data.getCommonAlignment();
-
- if (!Data.isCommon() && !(Data.getFlags() & ELF_STB_Weak))
- if (MCFragment *FF = Data.getFragment())
- Value = Layout.getSymbolAddress(&Data) -
- Layout.getSectionAddress(FF->getParent());
+ assert(!(Data.isCommon() && !Data.isExternal()));
ESize = Data.getSize();
if (Data.getSize()) {
void ELFObjectWriterImpl::WriteSymbolTable(MCDataFragment *F,
const MCAssembler &Asm,
- const MCAsmLayout &Layout) {
+ const MCAsmLayout &Layout,
+ unsigned NumRegularSections) {
// The string table must be emitted first because we need the index
// into the string table for all the symbol names.
assert(StringTable.size() && "Missing string table");
WriteSymbol(F, MSD, Layout);
}
- // Write out a symbol table entry for each section.
- // leaving out the just added .symtab which is at
- // the very end
+ // Write out a symbol table entry for each regular section.
unsigned Index = 1;
- for (MCAssembler::const_iterator it = Asm.begin(),
- ie = Asm.end(); it != ie; ++it, ++Index) {
+ for (MCAssembler::const_iterator it = Asm.begin();
+ Index <= NumRegularSections; ++it, ++Index) {
const MCSectionELF &Section =
static_cast<const MCSectionELF&>(it->getSection());
// Leave out relocations so we don't have indexes within
// the relocations messed up
if (Section.getType() == ELF::SHT_RELA || Section.getType() == ELF::SHT_REL)
continue;
- if (Index == Asm.size())
- continue;
WriteSymbolEntry(F, 0, ELF::STT_SECTION, 0, 0, ELF::STV_DEFAULT, Index);
LastLocalSymbolIndex++;
}
assert((Data.getFlags() & ELF_STB_Global) &&
"External symbol requires STB_GLOBAL flag");
WriteSymbol(F, MSD, Layout);
- if (Data.getFlags() & ELF_STB_Local)
+ if (GetBinding(Data) == ELF::STB_LOCAL)
LastLocalSymbolIndex++;
}
for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
ELFSymbolData &MSD = UndefinedSymbolData[i];
MCSymbolData &Data = *MSD.SymbolData;
- Data.setFlags(Data.getFlags() | ELF_STB_Global);
WriteSymbol(F, MSD, Layout);
- if (Data.getFlags() & ELF_STB_Local)
+ if (GetBinding(Data) == ELF::STB_LOCAL)
LastLocalSymbolIndex++;
}
}
+static const MCSymbolData *getAtom(const MCSymbolData &SD) {
+ if (!SD.getFragment())
+ return 0;
+
+ return SD.getFragment()->getAtom();
+}
+
// FIXME: this is currently X86/X86_64 only
void ELFObjectWriterImpl::RecordRelocation(const MCAssembler &Asm,
const MCAsmLayout &Layout,
unsigned Index = 0;
int64_t Value = Target.getConstant();
+ bool IsPCRel = isFixupKindX86PCRel(Fixup.getKind());
if (!Target.isAbsolute()) {
const MCSymbol *Symbol = &Target.getSymA()->getSymbol();
MCSymbolData &SD = Asm.getSymbolData(*Symbol);
- const MCSymbolData *Base = Asm.getAtom(Layout, &SD);
+ const MCSymbolData *Base = getAtom(SD);
MCFragment *F = SD.getFragment();
+ // Avoid relocations for cases like jumps and calls in the same file.
+ if (Symbol->isDefined() && !SD.isExternal() &&
+ IsPCRel &&
+ &Fragment->getParent()->getSection() == &Symbol->getSection()) {
+ uint64_t FixupAddr = Layout.getFragmentAddress(Fragment) + Fixup.getOffset();
+ FixedValue = Layout.getSymbolAddress(&SD) + Target.getConstant() - FixupAddr;
+ return;
+ }
+
if (Base) {
- if (F && (!Symbol->isInSection() || SD.isCommon()) && !SD.isExternal()) {
+ if (Base != &SD) {
Index = F->getParent()->getOrdinal() + LocalSymbolData.size() + 1;
- Value += Layout.getSymbolAddress(&SD);
+
+ MCSectionData *FSD = F->getParent();
+ // Offset of the symbol in the section
+ Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
} else
Index = getSymbolIndexInSymbolTable(Asm, Symbol);
- if (Base != &SD)
- Value += Layout.getSymbolAddress(&SD) - Layout.getSymbolAddress(Base);
Addend = Value;
// Compensate for the addend on i386.
if (Is64Bit)
MCSectionData *FSD = F->getParent();
// Offset of the symbol in the section
- Addend = Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
+ Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
} else {
- FixedValue = Value;
- return;
+ Index = getSymbolIndexInSymbolTable(Asm, Symbol);
}
+ Addend = Value;
+ // Compensate for the addend on i386.
+ if (Is64Bit)
+ Value = 0;
}
}
FixedValue = Value;
// determine the type of the relocation
- bool IsPCRel = isFixupKindX86PCRel(Fixup.getKind());
unsigned Type;
if (Is64Bit) {
if (IsPCRel) {
case X86::reloc_pcrel_4byte:
case FK_Data_4:
// check that the offset fits within a signed long
- if (isInt<32>(Target.getConstant()))
+ if (Target.getConstant() < 0) {
+ assert(isInt<32>(Target.getConstant()));
Type = ELF::R_X86_64_32S;
- else
+ } else {
+ assert(isUInt<32>(Target.getConstant()));
Type = ELF::R_X86_64_32;
+ }
break;
case FK_Data_2: Type = ELF::R_X86_64_16; break;
case X86::reloc_pcrel_1byte:
const MCSymbol &Symbol = it->getSymbol();
// Ignore non-linker visible symbols.
- if (!Asm.isSymbolLinkerVisible(Symbol))
+ if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
continue;
if (!it->isExternal() && !Symbol.isUndefined())
continue;
+ if (Symbol.isVariable())
+ continue;
+
uint64_t &Entry = StringIndexMap[Symbol.getName()];
if (!Entry) {
Entry = StringTable.size();
MSD.SymbolData = it;
MSD.StringIndex = Entry;
- if (Symbol.isUndefined()) {
+ if (it->isCommon()) {
+ MSD.SectionIndex = ELF::SHN_COMMON;
+ ExternalSymbolData.push_back(MSD);
+ } else if (Symbol.isUndefined()) {
MSD.SectionIndex = ELF::SHN_UNDEF;
- // XXX: for some reason we dont Emit* this
- it->setFlags(it->getFlags() | ELF_STB_Global);
+ // FIXME: Undefined symbols are global, but this is the first place we
+ // are able to set it.
+ if (GetBinding(*it) == ELF::STB_LOCAL)
+ SetBinding(*it, ELF::STB_GLOBAL);
UndefinedSymbolData.push_back(MSD);
} else if (Symbol.isAbsolute()) {
MSD.SectionIndex = ELF::SHN_ABS;
ExternalSymbolData.push_back(MSD);
- } else if (it->isCommon()) {
- MSD.SectionIndex = ELF::SHN_COMMON;
- ExternalSymbolData.push_back(MSD);
} else {
MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
assert(MSD.SectionIndex && "Invalid section index!");
WriteRelocationsFragment(Asm, F, &SD);
- Asm.AddSectionToTheEnd(RelaSD, Layout);
+ Asm.AddSectionToTheEnd(*Writer, RelaSD, Layout);
}
}
for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
ELFRelocationEntry entry = Relocs[e - i - 1];
- unsigned WordSize = Is64Bit ? 8 : 4;
- F->getContents() += StringRef((const char *)&entry.r_offset, WordSize);
- F->getContents() += StringRef((const char *)&entry.r_info, WordSize);
+ if (Is64Bit) {
+ char buf[8];
- if (HasRelocationAddend)
- F->getContents() += StringRef((const char *)&entry.r_addend, WordSize);
+ String64(buf, entry.r_offset);
+ F->getContents() += StringRef(buf, 8);
+
+ String64(buf, entry.r_info);
+ F->getContents() += StringRef(buf, 8);
+
+ if (HasRelocationAddend) {
+ String64(buf, entry.r_addend);
+ F->getContents() += StringRef(buf, 8);
+ }
+ } else {
+ char buf[4];
+
+ String32(buf, entry.r_offset);
+ F->getContents() += StringRef(buf, 4);
+
+ String32(buf, entry.r_info);
+ F->getContents() += StringRef(buf, 4);
+
+ if (HasRelocationAddend) {
+ String32(buf, entry.r_addend);
+ F->getContents() += StringRef(buf, 4);
+ }
+ }
}
}
MCContext &Ctx = Asm.getContext();
MCDataFragment *F;
- WriteRelocations(Asm, Layout);
-
const MCSection *SymtabSection;
unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
+ unsigned NumRegularSections = Asm.size();
+
+ // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
+ const MCSection *ShstrtabSection;
+ ShstrtabSection = Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
+ SectionKind::getReadOnly(), false);
+ MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
+ ShstrtabSD.setAlignment(1);
+ ShstrtabIndex = Asm.size();
+
SymtabSection = Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
SectionKind::getReadOnly(),
false, EntrySize);
-
MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
-
SymtabSD.setAlignment(Is64Bit ? 8 : 4);
- F = new MCDataFragment(&SymtabSD);
-
- // Symbol table
- WriteSymbolTable(F, Asm, Layout);
- Asm.AddSectionToTheEnd(SymtabSD, Layout);
-
const MCSection *StrtabSection;
StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
SectionKind::getReadOnly(), false);
-
MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
StrtabSD.setAlignment(1);
-
- // FIXME: This isn't right. If the sections get rearranged this will
- // be wrong. We need a proper lookup.
StringTableIndex = Asm.size();
- F = new MCDataFragment(&StrtabSD);
- F->getContents().append(StringTable.begin(), StringTable.end());
- Asm.AddSectionToTheEnd(StrtabSD, Layout);
+ WriteRelocations(Asm, Layout);
- const MCSection *ShstrtabSection;
- ShstrtabSection = Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
- SectionKind::getReadOnly(), false);
+ // Symbol table
+ F = new MCDataFragment(&SymtabSD);
+ WriteSymbolTable(F, Asm, Layout, NumRegularSections);
+ Asm.AddSectionToTheEnd(*Writer, SymtabSD, Layout);
- MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
- ShstrtabSD.setAlignment(1);
+ F = new MCDataFragment(&StrtabSD);
+ F->getContents().append(StringTable.begin(), StringTable.end());
+ Asm.AddSectionToTheEnd(*Writer, StrtabSD, Layout);
F = new MCDataFragment(&ShstrtabSD);
- // FIXME: This isn't right. If the sections get rearranged this will
- // be wrong. We need a proper lookup.
- ShstrtabIndex = Asm.size();
-
// Section header string table.
//
// The first entry of a string table holds a null character so skip
ie = Asm.end(); it != ie; ++it) {
const MCSectionELF &Section =
static_cast<const MCSectionELF&>(it->getSection());
+ // FIXME: We could merge suffixes like in .text and .rela.text.
// Remember the index into the string table so we can write it
// into the sh_name field of the section header table.
F->getContents() += '\x00';
}
- Asm.AddSectionToTheEnd(ShstrtabSD, Layout);
+ Asm.AddSectionToTheEnd(*Writer, ShstrtabSD, Layout);
+}
+
+bool ELFObjectWriterImpl::IsFixupFullyResolved(const MCAssembler &Asm,
+ const MCValue Target,
+ bool IsPCRel,
+ const MCFragment *DF) const {
+ // If this is a PCrel relocation, find the section this fixup value is
+ // relative to.
+ const MCSection *BaseSection = 0;
+ if (IsPCRel) {
+ BaseSection = &DF->getParent()->getSection();
+ assert(BaseSection);
+ }
+
+ const MCSection *SectionA = 0;
+ const MCSymbol *SymbolA = 0;
+ if (const MCSymbolRefExpr *A = Target.getSymA()) {
+ SymbolA = &A->getSymbol();
+ SectionA = &SymbolA->getSection();
+ }
+
+ const MCSection *SectionB = 0;
+ if (const MCSymbolRefExpr *B = Target.getSymB()) {
+ SectionB = &B->getSymbol().getSection();
+ }
+
+ if (!BaseSection)
+ return SectionA == SectionB;
+
+ const MCSymbolData &DataA = Asm.getSymbolData(*SymbolA);
+ if (DataA.isExternal())
+ return false;
+
+ return !SectionB && BaseSection == SectionA;
}
void ELFObjectWriterImpl::WriteObject(const MCAssembler &Asm,
WriteSecHdrEntry(SectionStringTableIndex[&it->getSection()],
Section.getType(), Section.getFlags(),
- Layout.getSectionAddress(&SD),
+ 0,
SectionOffsetMap.lookup(&SD.getSection()),
Layout.getSectionSize(&SD), sh_link,
sh_info, SD.getAlignment(),
ELFObjectWriter::ELFObjectWriter(raw_ostream &OS,
bool Is64Bit,
+ Triple::OSType OSType,
bool IsLittleEndian,
bool HasRelocationAddend)
: MCObjectWriter(OS, IsLittleEndian)
{
- Impl = new ELFObjectWriterImpl(this, Is64Bit, HasRelocationAddend);
+ Impl = new ELFObjectWriterImpl(this, Is64Bit, HasRelocationAddend, OSType);
}
ELFObjectWriter::~ELFObjectWriter() {
Target, FixedValue);
}
+bool ELFObjectWriter::IsFixupFullyResolved(const MCAssembler &Asm,
+ const MCValue Target,
+ bool IsPCRel,
+ const MCFragment *DF) const {
+ return ((ELFObjectWriterImpl*) Impl)->IsFixupFullyResolved(Asm, Target,
+ IsPCRel, DF);
+}
+
void ELFObjectWriter::WriteObject(const MCAssembler &Asm,
const MCAsmLayout &Layout) {
((ELFObjectWriterImpl*) Impl)->WriteObject(Asm, Layout);