38 #define DEBUG_TYPE "reloc-info"
48 bool Used,
bool Renamed);
49 static bool isLocal(
const MCSymbolData &Data,
bool isSignature,
53 static uint64_t GetSectionFileSize(
const MCAsmLayout &Layout,
55 static uint64_t GetSectionAddressSize(
const MCAsmLayout &Layout,
69 struct ELFSymbolData {
72 uint32_t SectionIndex;
75 bool operator<(
const ELFSymbolData &RHS)
const {
76 return SymbolData->getSymbol().getName() <
77 RHS.SymbolData->getSymbol().getName();
89 std::vector<ELFRelocationEntry> > Relocations;
97 std::vector<uint64_t> FileSymbolData;
98 std::vector<ELFSymbolData> LocalSymbolData;
99 std::vector<ELFSymbolData> ExternalSymbolData;
100 std::vector<ELFSymbolData> UndefinedSymbolData;
106 bool NeedsSymtabShndx;
109 unsigned LastLocalSymbolIndex;
111 unsigned StringTableIndex;
113 unsigned SymbolTableIndex;
115 unsigned ShstrtabIndex;
129 bool IsPCRel)
const {
130 return TargetObjectWriter->ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
134 bool IsPCRel)
const {
135 return TargetObjectWriter->undefinedExplicitRelSym(Target, Fixup,
139 bool is64Bit()
const {
return TargetObjectWriter->is64Bit(); }
140 bool hasRelocationAddend()
const {
141 return TargetObjectWriter->hasRelocationAddend();
143 unsigned GetRelocType(
const MCValue &Target,
const MCFixup &Fixup,
144 bool IsPCRel,
bool IsRelocWithSymbol,
145 int64_t Addend)
const {
146 return TargetObjectWriter->GetRelocType(Target, Fixup, IsPCRel,
147 IsRelocWithSymbol, Addend);
154 TargetObjectWriter(MOTW),
155 NeedsGOT(
false), NeedsSymtabShndx(
false) {
158 virtual ~ELFObjectWriter();
160 void WriteWord(uint64_t W) {
167 void StringLE16(
char *buf, uint16_t
Value) {
168 buf[0] = char(Value >> 0);
169 buf[1] = char(Value >> 8);
172 void StringLE32(
char *buf, uint32_t Value) {
173 StringLE16(buf, uint16_t(Value >> 0));
174 StringLE16(buf + 2, uint16_t(Value >> 16));
177 void StringLE64(
char *buf, uint64_t Value) {
178 StringLE32(buf, uint32_t(Value >> 0));
179 StringLE32(buf + 4, uint32_t(Value >> 32));
182 void StringBE16(
char *buf ,uint16_t Value) {
183 buf[0] = char(Value >> 8);
184 buf[1] = char(Value >> 0);
187 void StringBE32(
char *buf, uint32_t Value) {
188 StringBE16(buf, uint16_t(Value >> 16));
189 StringBE16(buf + 2, uint16_t(Value >> 0));
192 void StringBE64(
char *buf, uint64_t Value) {
193 StringBE32(buf, uint32_t(Value >> 32));
194 StringBE32(buf + 4, uint32_t(Value >> 0));
205 if (isLittleEndian())
206 StringLE16(buf, Value);
208 StringBE16(buf, Value);
214 if (isLittleEndian())
215 StringLE32(buf, Value);
217 StringBE32(buf, Value);
223 if (isLittleEndian())
224 StringLE64(buf, Value);
226 StringBE64(buf, Value);
231 uint64_t SectionDataSize,
232 unsigned NumberOfSections);
236 uint64_t name, uint8_t
info,
237 uint64_t value, uint64_t size,
238 uint8_t other, uint32_t shndx,
250 const SectionIndexMapTy &SectionIndexMap);
252 virtual void RecordRelocation(
const MCAssembler &Asm,
256 MCValue Target, uint64_t &FixedValue);
258 uint64_t getSymbolIndexInSymbolTable(
const MCAssembler &Asm,
277 const SectionIndexMapTy &SectionIndexMap,
278 RevGroupMapTy RevGroupMap,
279 unsigned NumRegularSections);
282 SectionIndexMapTy &SectionIndexMap,
283 const RelMapTy &RelMap);
289 const RelMapTy &RelMap);
292 SectionIndexMapTy &SectionIndexMap,
293 const RelMapTy &RelMap);
298 GroupMapTy &GroupMap,
299 RevGroupMapTy &RevGroupMap,
300 SectionIndexMapTy &SectionIndexMap,
301 const RelMapTy &RelMap);
303 virtual void ExecutePostLayoutBinding(
MCAssembler &Asm,
306 void WriteSectionHeader(
MCAssembler &Asm,
const GroupMapTy &GroupMap,
308 const SectionIndexMapTy &SectionIndexMap,
309 const SectionOffsetMapTy &SectionOffsetMap);
312 std::vector<const MCSectionELF*> &Sections);
314 void WriteSecHdrEntry(uint32_t
Name, uint32_t
Type, uint64_t
Flags,
315 uint64_t Address, uint64_t Offset,
316 uint64_t Size, uint32_t Link, uint32_t Info,
317 uint64_t Alignment, uint64_t EntrySize);
319 void WriteRelocationsFragment(
const MCAssembler &Asm,
324 IsSymbolRefDifferenceFullyResolvedImpl(
const MCAssembler &Asm,
332 const SectionIndexMapTy &SectionIndexMap,
333 uint32_t GroupSymbolIndex,
334 uint64_t Offset, uint64_t Size, uint64_t Alignment,
367 ELFObjectWriter::~ELFObjectWriter()
371 void ELFObjectWriter::WriteHeader(
const MCAssembler &Asm,
372 uint64_t SectionDataSize,
373 unsigned NumberOfSections) {
394 Write8(TargetObjectWriter->getOSABI());
401 Write16(TargetObjectWriter->getEMachine());
425 Write16(NumberOfSections);
431 Write16(ShstrtabIndex);
437 uint8_t
info, uint64_t value,
438 uint64_t size, uint8_t other,
443 String32(*ShndxF, shndx);
445 String32(*ShndxF, 0);
452 String32(*SymtabF, name);
453 String8(*SymtabF, info);
454 String8(*SymtabF, other);
455 String16(*SymtabF, Index);
456 String64(*SymtabF, value);
457 String64(*SymtabF, size);
459 String32(*SymtabF, name);
460 String32(*SymtabF, value);
461 String32(*SymtabF, size);
462 String8(*SymtabF, info);
463 String8(*SymtabF, other);
464 String16(*SymtabF, Index);
468 uint64_t ELFObjectWriter::SymbolValue(
MCSymbolData &Data,
478 if (
Value->EvaluateAsAbsolute(IntValue, Layout))
479 return (uint64_t)IntValue;
497 void ELFObjectWriter::ExecutePostLayoutBinding(
MCAssembler &Asm,
504 const MCSymbol &Alias = it->getSymbol();
509 if (&Symbol == &Alias)
513 size_t Pos = AliasName.
find(
'@');
531 Renames.insert(std::make_pair(&Symbol, &Alias));
558 uint64_t
Value = SymbolValue(Data, Layout);
566 if (!ESize->EvaluateAsAbsolute(Res, Layout))
572 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
573 Size, Other, MSD.SectionIndex, IsReserved);
580 const SectionIndexMapTy &SectionIndexMap) {
583 assert(StringTable.size() &&
"Missing string table");
588 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0,
false);
590 for (
unsigned i = 0, e = FileSymbolData.size(); i != e; ++i) {
591 WriteSymbolEntry(SymtabF, ShndxF, FileSymbolData[i],
597 LastLocalSymbolIndex = FileSymbolData.size() + LocalSymbolData.size() + 1;
599 for (
unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
600 ELFSymbolData &MSD = LocalSymbolData[i];
601 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
609 if (Section.
getType() == ELF::SHT_RELA ||
610 Section.
getType() == ELF::SHT_REL ||
611 Section.
getType() == ELF::SHT_STRTAB ||
612 Section.
getType() == ELF::SHT_SYMTAB ||
613 Section.
getType() == ELF::SHT_SYMTAB_SHNDX)
618 LastLocalSymbolIndex++;
621 for (
unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
622 ELFSymbolData &MSD = ExternalSymbolData[i];
626 "External symbol requires STB_GLOBAL or STB_WEAK flag");
627 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
629 LastLocalSymbolIndex++;
632 for (
unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
633 ELFSymbolData &MSD = UndefinedSymbolData[i];
635 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
637 LastLocalSymbolIndex++;
645 bool IsPCRel)
const {
648 const MCSymbol *Renamed = Renames.lookup(&Symbol);
654 return undefinedExplicitRelSym(Target, Fixup, IsPCRel);
668 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
680 if (&Sec2 != &Section &&
689 if (Section.
getFlags() & ELF::SHF_MERGE) {
691 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
697 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
702 void ELFObjectWriter::RecordRelocation(
const MCAssembler &Asm,
707 uint64_t &FixedValue) {
713 bool IsPCRel = isFixupKindPCRel(Asm, Fixup.
getKind());
717 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment, Fixup, IsPCRel);
720 const MCSymbol &SymbolB = RefB->getSymbol();
745 WeakrefUsedInReloc.insert(RelocSymbol);
747 UsedInReloc.insert(RelocSymbol);
751 if (hasRelocationAddend())
756 unsigned Type = GetRelocType(Target, Fixup, IsPCRel,
757 (RelocSymbol != 0), Addend);
760 if (RelocNeedsGOT(Modifier))
766 if (!hasRelocationAddend())
770 assert(isInt<64>(Addend));
775 Relocations[Fragment->
getParent()].push_back(ERE);
780 ELFObjectWriter::getSymbolIndexInSymbolTable(
const MCAssembler &Asm,
786 bool ELFObjectWriter::isInSymtab(
const MCAssembler &Asm,
788 bool Used,
bool Renamed) {
800 if (Symbol.
getName() ==
"_GLOBAL_OFFSET_TABLE_")
820 bool ELFObjectWriter::isLocal(
const MCSymbolData &Data,
bool isSignature,
821 bool isUsedInReloc) {
829 if (isSignature && !isUsedInReloc)
838 void ELFObjectWriter::ComputeIndexMap(
MCAssembler &Asm,
839 SectionIndexMapTy &SectionIndexMap,
840 const RelMapTy &RelMap) {
843 ie = Asm.
end(); it != ie; ++it) {
846 if (Section.
getType() != ELF::SHT_GROUP)
848 SectionIndexMap[&
Section] = Index++;
852 ie = Asm.
end(); it != ie; ++it) {
855 if (Section.
getType() == ELF::SHT_GROUP ||
856 Section.
getType() == ELF::SHT_REL ||
857 Section.
getType() == ELF::SHT_RELA)
859 SectionIndexMap[&
Section] = Index++;
860 const MCSectionELF *RelSection = RelMap.lookup(&Section);
862 SectionIndexMap[RelSection] = Index++;
866 void ELFObjectWriter::ComputeSymbolTable(
MCAssembler &Asm,
867 const SectionIndexMapTy &SectionIndexMap,
868 RevGroupMapTy RevGroupMap,
869 unsigned NumRegularSections) {
882 StringTable +=
'\x00';
892 uint64_t &Entry = StringIndexMap[
Name];
894 Entry = StringTable.
size();
896 StringTable +=
'\x00';
898 FileSymbolData.push_back(Entry);
904 const MCSymbol &Symbol = it->getSymbol();
906 bool Used = UsedInReloc.count(&Symbol);
907 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
908 bool isSignature = RevGroupMap.count(&Symbol);
910 if (!isInSymtab(Asm, *it,
911 Used || WeakrefUsed || isSignature,
912 Renames.count(&Symbol)))
921 bool Local = isLocal(*it, isSignature, Used);
928 if (RefSymbol.
isUndefined() && !Used && WeakrefUsed)
931 if (it->isCommon()) {
937 if (isSignature && !Used)
938 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
944 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
946 NeedsSymtabShndx =
true;
947 assert(MSD.SectionIndex &&
"Invalid section index!");
955 size_t Pos = Name.
find(
"@@@");
957 Buf += Name.
substr(0, Pos);
959 Buf += Name.
substr(Pos + Skip);
963 uint64_t &Entry = StringIndexMap[
Name];
965 Entry = StringTable.
size();
967 StringTable +=
'\x00';
969 MSD.StringIndex = Entry;
971 UndefinedSymbolData.push_back(MSD);
973 LocalSymbolData.push_back(MSD);
975 ExternalSymbolData.push_back(MSD);
980 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
981 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
985 unsigned Index = FileSymbolData.size() + 1;
986 for (
unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
987 LocalSymbolData[i].SymbolData->setIndex(Index++);
989 Index += NumRegularSections;
991 for (
unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
992 ExternalSymbolData[i].SymbolData->setIndex(Index++);
993 for (
unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
994 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
997 NeedsSymtabShndx =
true;
1000 void ELFObjectWriter::CreateRelocationSections(
MCAssembler &Asm,
1004 ie = Asm.
end(); it != ie; ++it) {
1006 if (Relocations[&SD].empty())
1014 std::string RelaSectionName = hasRelocationAddend() ?
".rela" :
".rel";
1015 RelaSectionName += SectionName;
1018 if (hasRelocationAddend())
1025 if (Section.
getFlags() & ELF::SHF_GROUP) {
1026 Flags = ELF::SHF_GROUP;
1032 ELF::SHT_RELA : ELF::SHT_REL, Flags,
1035 RelMap[&
Section] = RelaSection;
1041 const RelMapTy &RelMap) {
1043 ie = Asm.
end(); it != ie; ++it) {
1048 const MCSectionELF *RelaSection = RelMap.lookup(&Section);
1055 WriteRelocationsFragment(Asm, F, &*it);
1059 void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1060 uint64_t Flags, uint64_t Address,
1061 uint64_t Offset, uint64_t Size,
1062 uint32_t Link, uint32_t Info,
1064 uint64_t EntrySize) {
1073 WriteWord(Alignment);
1074 WriteWord(EntrySize);
1077 void ELFObjectWriter::WriteRelocationsFragment(
const MCAssembler &Asm,
1080 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
1084 TargetObjectWriter->sortRelocs(Asm, Relocs);
1086 for (
unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1091 else if (entry.
Index < 0)
1092 entry.
Index = getSymbolIndexInSymbolTable(Asm, entry.
Symbol);
1094 entry.
Index += FileSymbolData.size() + LocalSymbolData.size();
1097 if (TargetObjectWriter->isN64()) {
1098 String32(*F, entry.
Index);
1100 String8(*F, TargetObjectWriter->getRSsym(entry.
Type));
1101 String8(*F, TargetObjectWriter->getRType3(entry.
Type));
1102 String8(*F, TargetObjectWriter->getRType2(entry.
Type));
1103 String8(*F, TargetObjectWriter->getRType(entry.
Type));
1108 String64(*F, ERE64.r_info);
1110 if (hasRelocationAddend())
1117 String32(*F, ERE32.r_info);
1119 if (hasRelocationAddend())
1127 const StringRef &NameA = (*a)->getSectionName();
1128 const StringRef &NameB = (*b)->getSectionName();
1129 const unsigned sizeA = NameA.
size();
1130 const unsigned sizeB = NameB.
size();
1131 const unsigned len = std::min(sizeA, sizeB);
1132 for (
unsigned int i = 0; i < len; ++i) {
1133 char ca = NameA[sizeA - i - 1];
1134 char cb = NameB[sizeB - i - 1];
1139 return sizeB - sizeA;
1142 void ELFObjectWriter::CreateMetadataSections(
MCAssembler &Asm,
1144 SectionIndexMapTy &SectionIndexMap,
1145 const RelMapTy &RelMap) {
1167 if (NeedsSymtabShndx) {
1169 Ctx.
getELFSection(
".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
1176 StrtabSection = Ctx.
getELFSection(
".strtab", ELF::SHT_STRTAB, 0,
1181 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
1183 ShstrtabIndex = SectionIndexMap.lookup(ShstrtabSection);
1184 SymbolTableIndex = SectionIndexMap.lookup(SymtabSection);
1185 StringTableIndex = SectionIndexMap.lookup(StrtabSection);
1190 if (NeedsSymtabShndx) {
1193 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
1200 std::vector<const MCSectionELF*> Sections;
1202 ie = Asm.
end(); it != ie; ++it) {
1205 Sections.push_back(&Section);
1216 for (
unsigned int I = 0, E = Sections.size();
I != E; ++
I) {
1221 StringRef PreviousName = Sections[
I - 1]->getSectionName();
1223 SectionStringTableIndex[&
Section] = Index - Name.
size() - 1;
1229 SectionStringTableIndex[&
Section] = Index;
1231 Index += Name.
size() + 1;
1237 void ELFObjectWriter::CreateIndexedSections(
MCAssembler &Asm,
1239 GroupMapTy &GroupMap,
1240 RevGroupMapTy &RevGroupMap,
1241 SectionIndexMapTy &SectionIndexMap,
1242 const RelMapTy &RelMap) {
1257 if (!(Section.
getFlags() & ELF::SHF_GROUP))
1262 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
1268 String32(*F, ELF::GRP_COMDAT);
1270 GroupMap[Group] = SignatureSymbol;
1273 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
1280 if (!(Section.
getFlags() & ELF::SHF_GROUP))
1286 unsigned Index = SectionIndexMap.lookup(&Section);
1287 String32(*F, Index);
1291 void ELFObjectWriter::WriteSection(
MCAssembler &Asm,
1292 const SectionIndexMapTy &SectionIndexMap,
1293 uint32_t GroupSymbolIndex,
1294 uint64_t Offset, uint64_t Size,
1297 uint64_t sh_link = 0;
1298 uint64_t sh_info = 0;
1301 case ELF::SHT_DYNAMIC:
1302 sh_link = SectionStringTableIndex[&
Section];
1307 case ELF::SHT_RELA: {
1313 sh_link = SectionIndexMap.lookup(SymtabSection);
1314 assert(sh_link &&
".symtab not found");
1317 unsigned SecNameLen = (Section.
getType() == ELF::SHT_REL) ? 4 : 5;
1325 sh_info = SectionIndexMap.lookup(InfoSection);
1329 case ELF::SHT_SYMTAB:
1330 case ELF::SHT_DYNSYM:
1331 sh_link = StringTableIndex;
1332 sh_info = LastLocalSymbolIndex;
1335 case ELF::SHT_SYMTAB_SHNDX:
1336 sh_link = SymbolTableIndex;
1339 case ELF::SHT_PROGBITS:
1340 case ELF::SHT_STRTAB:
1341 case ELF::SHT_NOBITS:
1344 case ELF::SHT_ARM_ATTRIBUTES:
1345 case ELF::SHT_INIT_ARRAY:
1346 case ELF::SHT_FINI_ARRAY:
1347 case ELF::SHT_PREINIT_ARRAY:
1348 case ELF::SHT_X86_64_UNWIND:
1349 case ELF::SHT_MIPS_REGINFO:
1350 case ELF::SHT_MIPS_OPTIONS:
1354 case ELF::SHT_GROUP:
1355 sh_link = SymbolTableIndex;
1356 sh_info = GroupSymbolIndex;
1360 assert(0 &&
"FIXME: sh_type value not supported!");
1364 if (TargetObjectWriter->getEMachine() ==
ELF::EM_ARM &&
1365 Section.
getType() == ELF::SHT_ARM_EXIDX) {
1367 if (SecName ==
".ARM.exidx") {
1368 sh_link = SectionIndexMap.lookup(
1371 ELF::SHF_EXECINSTR | ELF::SHF_ALLOC,
1373 }
else if (SecName.startswith(
".ARM.exidx")) {
1377 SecName.substr(
sizeof(
".ARM.exidx") - 1), ELF::SHT_PROGBITS,
1383 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1384 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1385 Alignment, Section.getEntrySize());
1388 bool ELFObjectWriter::IsELFMetaDataSection(
const MCSectionData &SD) {
1393 uint64_t ELFObjectWriter::DataSectionSize(
const MCSectionData &SD) {
1399 Ret += cast<MCDataFragment>(
F).getContents().size();
1404 uint64_t ELFObjectWriter::GetSectionFileSize(
const MCAsmLayout &Layout,
1406 if (IsELFMetaDataSection(SD))
1407 return DataSectionSize(SD);
1411 uint64_t ELFObjectWriter::GetSectionAddressSize(
const MCAsmLayout &Layout,
1413 if (IsELFMetaDataSection(SD))
1414 return DataSectionSize(SD);
1418 void ELFObjectWriter::WriteDataSectionData(
MCAssembler &Asm,
1424 WriteZeros(Padding);
1426 if (IsELFMetaDataSection(SD)) {
1431 WriteBytes(cast<MCDataFragment>(F).getContents());
1438 void ELFObjectWriter::WriteSectionHeader(
MCAssembler &Asm,
1439 const GroupMapTy &GroupMap,
1441 const SectionIndexMapTy &SectionIndexMap,
1442 const SectionOffsetMapTy &SectionOffsetMap) {
1443 const unsigned NumSections = Asm.
size() + 1;
1445 std::vector<const MCSectionELF*> Sections;
1446 Sections.resize(NumSections - 1);
1448 for (SectionIndexMapTy::const_iterator i=
1449 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1450 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
1451 Sections[p.second - 1] = p.first;
1455 uint64_t FirstSectionSize =
1457 uint32_t FirstSectionLink =
1459 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
1461 for (
unsigned i = 0; i < NumSections - 1; ++i) {
1464 uint32_t GroupSymbolIndex;
1465 if (Section.
getType() != ELF::SHT_GROUP)
1466 GroupSymbolIndex = 0;
1468 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm,
1469 GroupMap.lookup(&Section));
1471 uint64_t Size = GetSectionAddressSize(Layout, SD);
1473 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
1474 SectionOffsetMap.lookup(&Section), Size,
1479 void ELFObjectWriter::ComputeSectionOrder(
MCAssembler &Asm,
1480 std::vector<const MCSectionELF*> &Sections) {
1482 ie = Asm.
end(); it != ie; ++it) {
1485 if (Section.
getType() == ELF::SHT_GROUP)
1486 Sections.push_back(&Section);
1490 ie = Asm.
end(); it != ie; ++it) {
1493 if (Section.
getType() != ELF::SHT_GROUP &&
1494 Section.
getType() != ELF::SHT_REL &&
1495 Section.
getType() != ELF::SHT_RELA)
1496 Sections.push_back(&Section);
1500 ie = Asm.
end(); it != ie; ++it) {
1503 if (Section.
getType() == ELF::SHT_REL ||
1504 Section.
getType() == ELF::SHT_RELA)
1505 Sections.push_back(&Section);
1509 void ELFObjectWriter::WriteObject(
MCAssembler &Asm,
1511 GroupMapTy GroupMap;
1512 RevGroupMapTy RevGroupMap;
1513 SectionIndexMapTy SectionIndexMap;
1515 unsigned NumUserSections = Asm.
size();
1518 CreateRelocationSections(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1520 const unsigned NumUserAndRelocSections = Asm.
size();
1521 CreateIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1522 RevGroupMap, SectionIndexMap, RelMap);
1523 const unsigned AllSections = Asm.
size();
1524 const unsigned NumIndexedSections = AllSections - NumUserAndRelocSections;
1526 unsigned NumRegularSections = NumUserSections + NumIndexedSections;
1529 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap, NumRegularSections);
1532 WriteRelocations(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1534 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1535 const_cast<MCAsmLayout&>(Layout),
1539 uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1544 std::vector<const MCSectionELF*> Sections;
1545 ComputeSectionOrder(Asm, Sections);
1546 unsigned NumSections = Sections.size();
1547 SectionOffsetMapTy SectionOffsetMap;
1548 for (
unsigned i = 0; i < NumRegularSections + 1; ++i) {
1555 SectionOffsetMap[&
Section] = FileOff;
1558 FileOff += GetSectionFileSize(Layout, SD);
1563 const unsigned SectionHeaderOffset = FileOff -
HeaderSize;
1565 uint64_t SectionHeaderEntrySize = is64Bit() ?
1567 FileOff += (NumSections + 1) * SectionHeaderEntrySize;
1569 for (
unsigned i = NumRegularSections + 1; i < NumSections; ++i) {
1576 SectionOffsetMap[&
Section] = FileOff;
1579 FileOff += GetSectionFileSize(Layout, SD);
1583 WriteHeader(Asm, SectionHeaderOffset, NumSections + 1);
1587 for (
unsigned i = 0; i < NumRegularSections + 1; ++i)
1588 WriteDataSectionData(Asm, Layout, *Sections[i]);
1591 WriteZeros(Padding);
1594 WriteSectionHeader(Asm, GroupMap, Layout, SectionIndexMap,
1598 for (
unsigned i = NumRegularSections + 1; i < NumSections; ++i)
1599 WriteDataSectionData(Asm, Layout, *Sections[i]);
1603 ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(
const MCAssembler &Asm,
1607 bool IsPCRel)
const {
1611 Asm, DataA, FB,InSet, IsPCRel);
1616 bool IsLittleEndian) {
1617 return new ELFObjectWriter(MOTW, OS, IsLittleEndian);
bool isInt< 32 >(int64_t x)
const MCSectionELF * CreateELFGroupSection()
void push_back(const T &Elt)
void setExternal(bool Value)
StringRef getSectionName() const
void setSymbolAndType(Elf32_Word s, unsigned char t)
const MCSymbol & getSymbol() const
size_t size() const
size - Get the string size.
const MCExpr * getSize() const
static unsigned getOther(MCSymbolData &SD)
const MCSymbol * getGroup() const
size_t find(char C, size_t From=0) const
const MCSectionELF * getELFSection(StringRef Section, unsigned Type, unsigned Flags, SectionKind Kind)
bool endswith(StringRef Suffix) const
Check if this string ends with the given Suffix.
bool getNoExecStack() const
unsigned getFlags() const
StringRef substr(size_t Start, size_t N=npos) const
void operator<(const Optional< T > &X, const Optional< U > &Y)
Poison comparison between two Optional objects. Clients needs to explicitly compare the underlying va...
virtual SmallVectorImpl< char > & getContents()
void setSymbolAndType(Elf64_Word s, Elf64_Word t)
unsigned getELFHeaderEFlags() const
ELF e_header flags.
MCContext & getContext() const
const MCSymbol & getSymbol() const
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
symbol_iterator symbol_begin()
static unsigned GetVisibility(MCSymbolData &SD)
MCSymbol * GetOrCreateSymbol(StringRef Name)
uint64_t getIndex() const
getIndex - Get the (implementation defined) index.
const MCSection & getSection() const
const MCExpr * getVariableValue() const
getVariableValue() - Get the value for variable symbols.
static unsigned GetType(const MCSymbolData &SD)
virtual bool IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm, const MCSymbolData &DataA, const MCFragment &FB, bool InSet, bool IsPCRel) const
bool isAbsolute() const
isAbsolute - Is this an absolute (as opposed to relocatable) value.
MCFragment * getFragment() const
const MCSection & getSection() const
uint32_t getOffset() const
unsigned getOrdinal() const
static void SetBinding(MCSymbolData &SD, unsigned Binding)
void array_pod_sort(IteratorTy Start, IteratorTy End)
MCSectionData * getParent() const
uint32_t getFlags() const
getFlags - Get the (implementation defined) symbol flags.
FragmentType getKind() const
MCFixupKind
MCFixupKind - Extensible enumeration to represent the type of a fixup.
FileNameVectorType::const_iterator const_file_name_iterator
uint64_t getSectionFileSize(const MCSectionData *SD) const
Get the data size of the given section, as emitted to the object file. This may include additional pa...
void append(in_iter in_start, in_iter in_end)
MCFixupKind getKind() const
const MCSymbolRefExpr * getSymB() const
MCObjectWriter * createELFObjectWriter(MCELFObjectTargetWriter *MOTW, raw_ostream &OS, bool IsLittleEndian)
Construct a new ELF writer instance.
bool isAbsolute() const
isAbsolute - Check if this is an absolute symbol.
bool isThreadLocal() const
MCSymbolData & getOrCreateSymbolData(const MCSymbol &Symbol, bool *Created=0)
bool isSymbolLinkerVisible(const MCSymbol &SD) const
MCSymbolData & getSymbolData(const MCSymbol &Symbol) const
static int compareBySuffix(const MCSectionELF *const *a, const MCSectionELF *const *b)
bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
SectionKind getKind() const
const MCSymbolRefExpr * getSymA() const
unsigned getCommonAlignment() const
getCommonAlignment - Return the alignment of a 'common' symbol.
virtual bool isVirtualSection() const =0
unsigned getAlignment() const
bool isCommon() const
isCommon - Is this a 'common' symbol.
const_file_name_iterator file_names_begin() const
uint64_t getSymbolOffset(const MCSymbolData *SD) const
Get the offset of the given symbol, as computed in the current layout.
uint64_t getSectionAddressSize(const MCSectionData *SD) const
Get the address space size of the given section, as it effects layout. This may differ from the size ...
bool isTemporary() const
isTemporary - Check if this is an assembler temporary symbol.
const_file_name_iterator file_names_end() const
uint64_t RoundUpToAlignment(uint64_t Value, uint64_t Align)
MCSectionData & getOrCreateSectionData(const MCSection &Section, bool *Created=0)
static unsigned GetBinding(const MCSymbolData &SD)
StringRef getName() const
getName - Get the symbol name.
const MCSymbol & AliasedSymbol() const
MCAsmBackend & getBackend() const
uint64_t getFragmentOffset(const MCFragment *F) const
Get the offset of the given fragment inside its containing section.
void setAlignment(unsigned Value)
MCFixupKindInfo - Target independent information on a fixup kind.
bool isVariable() const
isVariable - Check if this is a variable symbol.
VariantKind getKind() const
int64_t getConstant() const
LLVM Value Representation.
MCAssembler & getAssembler() const
Get the assembler object this is a layout for.
uint64_t OffsetToAlignment(uint64_t Value, uint64_t Align)
void writeSectionData(const MCSectionData *Section, const MCAsmLayout &Layout) const
Emit the section contents using the given object writer.
bool isUndefined() const
isUndefined - Check if this symbol undefined (i.e., implicitly defined).
virtual const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const
getFixupKindInfo - Get information on a fixup kind.
static SectionKind getReadOnly()
unsigned Flags
Flags describing additional information on this fixup kind.
static SectionKind getText()
symbol_iterator symbol_end()