struct ELFRelocationEntry {
uint64_t Offset; // Where is the relocation.
- bool UseSymbol; // Relocate with a symbol, not the section.
- union {
- const MCSymbol *Symbol; // The symbol to relocate with.
- const MCSectionData *Section; // The section to relocate with.
- };
+ const MCSymbol *Symbol; // The symbol to relocate with.
unsigned Type; // The type of the relocation.
uint64_t Addend; // The addend to use.
ELFRelocationEntry(uint64_t Offset, const MCSymbol *Symbol, unsigned Type,
uint64_t Addend)
- : Offset(Offset), UseSymbol(true), Symbol(Symbol), Type(Type),
- Addend(Addend) {}
-
- ELFRelocationEntry(uint64_t Offset, const MCSectionData *Section,
- unsigned Type, uint64_t Addend)
- : Offset(Offset), UseSymbol(false), Section(Section), Type(Type),
- Addend(Addend) {}
+ : Offset(Offset), Symbol(Symbol), Type(Type), Addend(Addend) {}
};
class ELFObjectWriter : public MCObjectWriter {
// Support lexicographic sorting.
bool operator<(const ELFSymbolData &RHS) const {
+ unsigned LHSType = MCELF::GetType(*SymbolData);
+ unsigned RHSType = MCELF::GetType(*RHS.SymbolData);
+ if (LHSType == ELF::STT_SECTION && RHSType != ELF::STT_SECTION)
+ return false;
+ if (LHSType != ELF::STT_SECTION && RHSType == ELF::STT_SECTION)
+ return true;
+ if (LHSType == ELF::STT_SECTION && RHSType == ELF::STT_SECTION)
+ return SectionIndex < RHS.SectionIndex;
return Name < RHS.Name;
}
};
WriteSymbol(Writer, MSD, Layout);
}
- // Write out a symbol table entry for each regular section.
- for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
- ++i) {
- const MCSectionELF &Section =
- static_cast<const MCSectionELF&>(i->getSection());
- if (Section.getType() == ELF::SHT_RELA ||
- Section.getType() == ELF::SHT_REL ||
- Section.getType() == ELF::SHT_STRTAB ||
- Section.getType() == ELF::SHT_SYMTAB ||
- Section.getType() == ELF::SHT_SYMTAB_SHNDX)
- continue;
- Writer.writeSymbol(0, ELF::STT_SECTION, 0, 0, ELF::STV_DEFAULT,
- SectionIndexMap.lookup(&Section), false);
- LastLocalSymbolIndex++;
- }
-
for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
ELFSymbolData &MSD = ExternalSymbolData[i];
MCSymbolData &Data = *MSD.SymbolData;
if (!RelocateWithSymbol) {
const MCSection *SecA =
(SymA && !SymA->isUndefined()) ? &SymA->getSection() : nullptr;
- const MCSectionData *SecAD = SecA ? &Asm.getSectionData(*SecA) : nullptr;
- ELFRelocationEntry Rec(FixupOffset, SecAD, Type, Addend);
+ auto *ELFSec = cast_or_null<MCSectionELF>(SecA);
+ MCSymbol *SectionSymbol =
+ ELFSec ? Asm.getContext().getOrCreateSectionSymbol(*ELFSec)
+ : nullptr;
+ ELFRelocationEntry Rec(FixupOffset, SectionSymbol, Type, Addend);
Relocations[FixupSection].push_back(Rec);
return;
}
Buf += Name.substr(Pos + Skip);
Name = Buf;
}
- MSD.Name = StrTabBuilder.add(Name);
+
+ // Sections have their own string table
+ if (MCELF::GetType(SD) != ELF::STT_SECTION)
+ MSD.Name = StrTabBuilder.add(Name);
if (MSD.SectionIndex == ELF::SHN_UNDEF)
UndefinedSymbolData.push_back(MSD);
for (auto i = Asm.file_names_begin(), e = Asm.file_names_end(); i != e; ++i)
FileSymbolData.push_back(StrTabBuilder.getOffset(*i));
- for (ELFSymbolData& MSD : LocalSymbolData)
- MSD.StringIndex = StrTabBuilder.getOffset(MSD.Name);
- for (ELFSymbolData& MSD : ExternalSymbolData)
+ for (ELFSymbolData &MSD : LocalSymbolData)
+ MSD.StringIndex = MCELF::GetType(*MSD.SymbolData) == ELF::STT_SECTION
+ ? 0
+ : StrTabBuilder.getOffset(MSD.Name);
+ for (ELFSymbolData &MSD : ExternalSymbolData)
MSD.StringIndex = StrTabBuilder.getOffset(MSD.Name);
for (ELFSymbolData& MSD : UndefinedSymbolData)
MSD.StringIndex = StrTabBuilder.getOffset(MSD.Name);
for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
LocalSymbolData[i].SymbolData->setIndex(Index++);
- Index += NumRegularSections;
-
for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
ExternalSymbolData[i].SymbolData->setIndex(Index++);
for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
const ELFRelocationEntry &Entry = Relocs[e - i - 1];
-
- unsigned Index;
- if (Entry.UseSymbol) {
- Index = getSymbolIndexInSymbolTable(Asm, Entry.Symbol);
- } else {
- const MCSectionData *Sec = Entry.Section;
- if (Sec)
- Index = Sec->getOrdinal() + FileSymbolData.size() +
- LocalSymbolData.size() + 1;
- else
- Index = 0;
- }
+ unsigned Index =
+ Entry.Symbol ? getSymbolIndexInSymbolTable(Asm, Entry.Symbol) : 0;
if (is64Bit()) {
write(*F, Entry.Offset);
--- /dev/null
+// RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o - | llvm-readobj -s -t -r --expand-relocs | FileCheck %s
+
+.section foo, "aG", @progbits, f1, comdat
+.section foo, "G", @progbits, f2, comdat
+.section bar
+.long foo
+
+// Test that the relocation points to the first section foo.
+
+// The first seciton foo has index 6
+// CHECK: Section {
+// CHECK: Index: 6
+// CHECK-NEXT: Name: foo (28)
+// CHECK-NEXT: Type: SHT_PROGBITS (0x1)
+// CHECK-NEXT: Flags [ (0x202)
+// CHECK-NEXT: SHF_ALLOC (0x2)
+// CHECK-NEXT: SHF_GROUP (0x200)
+// CHECK-NEXT: ]
+// CHECK-NEXT: Address: 0x0
+// CHECK-NEXT: Offset: 0x50
+// CHECK-NEXT: Size: 0
+// CHECK-NEXT: Link: 0
+// CHECK-NEXT: Info: 0
+// CHECK-NEXT: AddressAlignment: 1
+// CHECK-NEXT: EntrySize: 0
+// CHECK-NEXT: }
+// CHECK-NEXT: Section {
+// CHECK-NEXT: Index: 7
+// CHECK-NEXT: Name: foo (28)
+// CHECK-NEXT: Type: SHT_PROGBITS (0x1)
+// CHECK-NEXT: Flags [ (0x200)
+// CHECK-NEXT: SHF_GROUP (0x200)
+// CHECK-NEXT: ]
+// CHECK-NEXT: Address: 0x0
+// CHECK-NEXT: Offset: 0x50
+// CHECK-NEXT: Size: 0
+// CHECK-NEXT: Link: 0
+// CHECK-NEXT: Info: 0
+// CHECK-NEXT: AddressAlignment: 1
+// CHECK-NEXT: EntrySize: 0
+// CHECK-NEXT: }
+
+// The relocation points to symbol 6
+// CHECK: Relocations [
+// CHECK-NEXT: Section (9) .relabar {
+// CHECK-NEXT: Relocation {
+// CHECK-NEXT: Offset: 0x0
+// CHECK-NEXT: Type: R_X86_64_32 (10)
+// CHECK-NEXT: Symbol: foo (6)
+// CHECK-NEXT: Addend: 0x0
+// CHECK-NEXT: }
+// CHECK-NEXT: }
+// CHECK-NEXT: ]
+
+
+// The symbol 6 corresponds section 6
+// CHECK: Symbols [
+
+// symbol 0
+// CHECK-NOT: Name
+// CHECK: Name:
+
+// symbol 1
+// CHECK-NOT: Name
+// CHECK: Name: f1
+
+// symbol 2
+// CHECK-NOT: Name
+// CHECK: Name: f2
+
+// symbol 3
+// CHECK-NOT: Name
+// CHECK: Name: .text
+
+// symbol 4
+// CHECK-NOT: Name
+// CHECK: Name: .data
+
+// symbol 5
+// CHECK-NOT: Name
+// CHECK: Name: .bss
+
+// symbol 6
+// CHECK-NOT: Name
+// CHECK: Name: foo
+// CHECK: Section: foo (0x6)
+
+// symbol 7
+// CHECK-NOT: Name
+// CHECK: Name: foo
+// CHECK: Section: foo (0x7)