ELFObjectWriter.cpp 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549
  1. //===- lib/MC/ELFObjectWriter.cpp - ELF File Writer -----------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements ELF object file writer information.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/ADT/ArrayRef.h"
  13. #include "llvm/ADT/DenseMap.h"
  14. #include "llvm/ADT/STLExtras.h"
  15. #include "llvm/ADT/SmallVector.h"
  16. #include "llvm/ADT/StringRef.h"
  17. #include "llvm/ADT/Twine.h"
  18. #include "llvm/ADT/iterator.h"
  19. #include "llvm/BinaryFormat/ELF.h"
  20. #include "llvm/MC/MCAsmBackend.h"
  21. #include "llvm/MC/MCAsmInfo.h"
  22. #include "llvm/MC/MCAsmLayout.h"
  23. #include "llvm/MC/MCAssembler.h"
  24. #include "llvm/MC/MCContext.h"
  25. #include "llvm/MC/MCELFObjectWriter.h"
  26. #include "llvm/MC/MCExpr.h"
  27. #include "llvm/MC/MCFixup.h"
  28. #include "llvm/MC/MCFixupKindInfo.h"
  29. #include "llvm/MC/MCFragment.h"
  30. #include "llvm/MC/MCObjectWriter.h"
  31. #include "llvm/MC/MCSection.h"
  32. #include "llvm/MC/MCSectionELF.h"
  33. #include "llvm/MC/MCSymbol.h"
  34. #include "llvm/MC/MCSymbolELF.h"
  35. #include "llvm/MC/MCTargetOptions.h"
  36. #include "llvm/MC/MCValue.h"
  37. #include "llvm/MC/StringTableBuilder.h"
  38. #include "llvm/Support/Alignment.h"
  39. #include "llvm/Support/Casting.h"
  40. #include "llvm/Support/Compression.h"
  41. #include "llvm/Support/Endian.h"
  42. #include "llvm/Support/EndianStream.h"
  43. #include "llvm/Support/Error.h"
  44. #include "llvm/Support/ErrorHandling.h"
  45. #include "llvm/Support/Host.h"
  46. #include "llvm/Support/LEB128.h"
  47. #include "llvm/Support/MathExtras.h"
  48. #include "llvm/Support/SMLoc.h"
  49. #include "llvm/Support/raw_ostream.h"
  50. #include <algorithm>
  51. #include <cassert>
  52. #include <cstddef>
  53. #include <cstdint>
  54. #include <map>
  55. #include <memory>
  56. #include <string>
  57. #include <utility>
  58. #include <vector>
  59. using namespace llvm;
  60. #undef DEBUG_TYPE
  61. #define DEBUG_TYPE "reloc-info"
  62. namespace {
  63. using SectionIndexMapTy = DenseMap<const MCSectionELF *, uint32_t>;
  64. class ELFObjectWriter;
  65. struct ELFWriter;
  66. bool isDwoSection(const MCSectionELF &Sec) {
  67. return Sec.getName().endswith(".dwo");
  68. }
  69. class SymbolTableWriter {
  70. ELFWriter &EWriter;
  71. bool Is64Bit;
  72. // indexes we are going to write to .symtab_shndx.
  73. std::vector<uint32_t> ShndxIndexes;
  74. // The numbel of symbols written so far.
  75. unsigned NumWritten;
  76. void createSymtabShndx();
  77. template <typename T> void write(T Value);
  78. public:
  79. SymbolTableWriter(ELFWriter &EWriter, bool Is64Bit);
  80. void writeSymbol(uint32_t name, uint8_t info, uint64_t value, uint64_t size,
  81. uint8_t other, uint32_t shndx, bool Reserved);
  82. ArrayRef<uint32_t> getShndxIndexes() const { return ShndxIndexes; }
  83. };
  84. struct ELFWriter {
  85. ELFObjectWriter &OWriter;
  86. support::endian::Writer W;
  87. enum DwoMode {
  88. AllSections,
  89. NonDwoOnly,
  90. DwoOnly,
  91. } Mode;
  92. static uint64_t SymbolValue(const MCSymbol &Sym, const MCAsmLayout &Layout);
  93. static bool isInSymtab(const MCAsmLayout &Layout, const MCSymbolELF &Symbol,
  94. bool Used, bool Renamed);
  95. /// Helper struct for containing some precomputed information on symbols.
  96. struct ELFSymbolData {
  97. const MCSymbolELF *Symbol;
  98. StringRef Name;
  99. uint32_t SectionIndex;
  100. uint32_t Order;
  101. };
  102. /// @}
  103. /// @name Symbol Table Data
  104. /// @{
  105. StringTableBuilder StrTabBuilder{StringTableBuilder::ELF};
  106. /// @}
  107. // This holds the symbol table index of the last local symbol.
  108. unsigned LastLocalSymbolIndex;
  109. // This holds the .strtab section index.
  110. unsigned StringTableIndex;
  111. // This holds the .symtab section index.
  112. unsigned SymbolTableIndex;
  113. // Sections in the order they are to be output in the section table.
  114. std::vector<const MCSectionELF *> SectionTable;
  115. unsigned addToSectionTable(const MCSectionELF *Sec);
  116. // TargetObjectWriter wrappers.
  117. bool is64Bit() const;
  118. bool usesRela(const MCSectionELF &Sec) const;
  119. uint64_t align(Align Alignment);
  120. bool maybeWriteCompression(uint32_t ChType, uint64_t Size,
  121. SmallVectorImpl<uint8_t> &CompressedContents,
  122. Align Alignment);
  123. public:
  124. ELFWriter(ELFObjectWriter &OWriter, raw_pwrite_stream &OS,
  125. bool IsLittleEndian, DwoMode Mode)
  126. : OWriter(OWriter),
  127. W(OS, IsLittleEndian ? support::little : support::big), Mode(Mode) {}
  128. void WriteWord(uint64_t Word) {
  129. if (is64Bit())
  130. W.write<uint64_t>(Word);
  131. else
  132. W.write<uint32_t>(Word);
  133. }
  134. template <typename T> void write(T Val) {
  135. W.write(Val);
  136. }
  137. void writeHeader(const MCAssembler &Asm);
  138. void writeSymbol(SymbolTableWriter &Writer, uint32_t StringIndex,
  139. ELFSymbolData &MSD, const MCAsmLayout &Layout);
  140. // Start and end offset of each section
  141. using SectionOffsetsTy =
  142. std::map<const MCSectionELF *, std::pair<uint64_t, uint64_t>>;
  143. // Map from a signature symbol to the group section index
  144. using RevGroupMapTy = DenseMap<const MCSymbol *, unsigned>;
  145. /// Compute the symbol table data
  146. ///
  147. /// \param Asm - The assembler.
  148. /// \param SectionIndexMap - Maps a section to its index.
  149. /// \param RevGroupMap - Maps a signature symbol to the group section.
  150. void computeSymbolTable(MCAssembler &Asm, const MCAsmLayout &Layout,
  151. const SectionIndexMapTy &SectionIndexMap,
  152. const RevGroupMapTy &RevGroupMap,
  153. SectionOffsetsTy &SectionOffsets);
  154. void writeAddrsigSection();
  155. MCSectionELF *createRelocationSection(MCContext &Ctx,
  156. const MCSectionELF &Sec);
  157. void createMemtagRelocs(MCAssembler &Asm);
  158. void writeSectionHeader(const MCAsmLayout &Layout,
  159. const SectionIndexMapTy &SectionIndexMap,
  160. const SectionOffsetsTy &SectionOffsets);
  161. void writeSectionData(const MCAssembler &Asm, MCSection &Sec,
  162. const MCAsmLayout &Layout);
  163. void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
  164. uint64_t Address, uint64_t Offset, uint64_t Size,
  165. uint32_t Link, uint32_t Info, MaybeAlign Alignment,
  166. uint64_t EntrySize);
  167. void writeRelocations(const MCAssembler &Asm, const MCSectionELF &Sec);
  168. uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout);
  169. void writeSection(const SectionIndexMapTy &SectionIndexMap,
  170. uint32_t GroupSymbolIndex, uint64_t Offset, uint64_t Size,
  171. const MCSectionELF &Section);
  172. };
  173. class ELFObjectWriter : public MCObjectWriter {
  174. /// The target specific ELF writer instance.
  175. std::unique_ptr<MCELFObjectTargetWriter> TargetObjectWriter;
  176. DenseMap<const MCSectionELF *, std::vector<ELFRelocationEntry>> Relocations;
  177. DenseMap<const MCSymbolELF *, const MCSymbolELF *> Renames;
  178. bool SeenGnuAbi = false;
  179. bool hasRelocationAddend() const;
  180. bool shouldRelocateWithSymbol(const MCAssembler &Asm,
  181. const MCSymbolRefExpr *RefA,
  182. const MCSymbolELF *Sym, uint64_t C,
  183. unsigned Type) const;
  184. public:
  185. ELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW)
  186. : TargetObjectWriter(std::move(MOTW)) {}
  187. void reset() override {
  188. SeenGnuAbi = false;
  189. Relocations.clear();
  190. Renames.clear();
  191. MCObjectWriter::reset();
  192. }
  193. bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
  194. const MCSymbol &SymA,
  195. const MCFragment &FB, bool InSet,
  196. bool IsPCRel) const override;
  197. virtual bool checkRelocation(MCContext &Ctx, SMLoc Loc,
  198. const MCSectionELF *From,
  199. const MCSectionELF *To) {
  200. return true;
  201. }
  202. void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
  203. const MCFragment *Fragment, const MCFixup &Fixup,
  204. MCValue Target, uint64_t &FixedValue) override;
  205. void executePostLayoutBinding(MCAssembler &Asm,
  206. const MCAsmLayout &Layout) override;
  207. void markGnuAbi() override { SeenGnuAbi = true; }
  208. bool seenGnuAbi() const { return SeenGnuAbi; }
  209. friend struct ELFWriter;
  210. };
  211. class ELFSingleObjectWriter : public ELFObjectWriter {
  212. raw_pwrite_stream &OS;
  213. bool IsLittleEndian;
  214. public:
  215. ELFSingleObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
  216. raw_pwrite_stream &OS, bool IsLittleEndian)
  217. : ELFObjectWriter(std::move(MOTW)), OS(OS),
  218. IsLittleEndian(IsLittleEndian) {}
  219. uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override {
  220. return ELFWriter(*this, OS, IsLittleEndian, ELFWriter::AllSections)
  221. .writeObject(Asm, Layout);
  222. }
  223. friend struct ELFWriter;
  224. };
  225. class ELFDwoObjectWriter : public ELFObjectWriter {
  226. raw_pwrite_stream &OS, &DwoOS;
  227. bool IsLittleEndian;
  228. public:
  229. ELFDwoObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
  230. raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS,
  231. bool IsLittleEndian)
  232. : ELFObjectWriter(std::move(MOTW)), OS(OS), DwoOS(DwoOS),
  233. IsLittleEndian(IsLittleEndian) {}
  234. bool checkRelocation(MCContext &Ctx, SMLoc Loc, const MCSectionELF *From,
  235. const MCSectionELF *To) override {
  236. if (isDwoSection(*From)) {
  237. Ctx.reportError(Loc, "A dwo section may not contain relocations");
  238. return false;
  239. }
  240. if (To && isDwoSection(*To)) {
  241. Ctx.reportError(Loc, "A relocation may not refer to a dwo section");
  242. return false;
  243. }
  244. return true;
  245. }
  246. uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override {
  247. uint64_t Size = ELFWriter(*this, OS, IsLittleEndian, ELFWriter::NonDwoOnly)
  248. .writeObject(Asm, Layout);
  249. Size += ELFWriter(*this, DwoOS, IsLittleEndian, ELFWriter::DwoOnly)
  250. .writeObject(Asm, Layout);
  251. return Size;
  252. }
  253. };
  254. } // end anonymous namespace
  255. uint64_t ELFWriter::align(Align Alignment) {
  256. uint64_t Offset = W.OS.tell();
  257. uint64_t NewOffset = alignTo(Offset, Alignment);
  258. W.OS.write_zeros(NewOffset - Offset);
  259. return NewOffset;
  260. }
  261. unsigned ELFWriter::addToSectionTable(const MCSectionELF *Sec) {
  262. SectionTable.push_back(Sec);
  263. StrTabBuilder.add(Sec->getName());
  264. return SectionTable.size();
  265. }
  266. void SymbolTableWriter::createSymtabShndx() {
  267. if (!ShndxIndexes.empty())
  268. return;
  269. ShndxIndexes.resize(NumWritten);
  270. }
  271. template <typename T> void SymbolTableWriter::write(T Value) {
  272. EWriter.write(Value);
  273. }
  274. SymbolTableWriter::SymbolTableWriter(ELFWriter &EWriter, bool Is64Bit)
  275. : EWriter(EWriter), Is64Bit(Is64Bit), NumWritten(0) {}
  276. void SymbolTableWriter::writeSymbol(uint32_t name, uint8_t info, uint64_t value,
  277. uint64_t size, uint8_t other,
  278. uint32_t shndx, bool Reserved) {
  279. bool LargeIndex = shndx >= ELF::SHN_LORESERVE && !Reserved;
  280. if (LargeIndex)
  281. createSymtabShndx();
  282. if (!ShndxIndexes.empty()) {
  283. if (LargeIndex)
  284. ShndxIndexes.push_back(shndx);
  285. else
  286. ShndxIndexes.push_back(0);
  287. }
  288. uint16_t Index = LargeIndex ? uint16_t(ELF::SHN_XINDEX) : shndx;
  289. if (Is64Bit) {
  290. write(name); // st_name
  291. write(info); // st_info
  292. write(other); // st_other
  293. write(Index); // st_shndx
  294. write(value); // st_value
  295. write(size); // st_size
  296. } else {
  297. write(name); // st_name
  298. write(uint32_t(value)); // st_value
  299. write(uint32_t(size)); // st_size
  300. write(info); // st_info
  301. write(other); // st_other
  302. write(Index); // st_shndx
  303. }
  304. ++NumWritten;
  305. }
  306. bool ELFWriter::is64Bit() const {
  307. return OWriter.TargetObjectWriter->is64Bit();
  308. }
  309. bool ELFWriter::usesRela(const MCSectionELF &Sec) const {
  310. return OWriter.hasRelocationAddend() &&
  311. Sec.getType() != ELF::SHT_LLVM_CALL_GRAPH_PROFILE;
  312. }
  313. // Emit the ELF header.
  314. void ELFWriter::writeHeader(const MCAssembler &Asm) {
  315. // ELF Header
  316. // ----------
  317. //
  318. // Note
  319. // ----
  320. // emitWord method behaves differently for ELF32 and ELF64, writing
  321. // 4 bytes in the former and 8 in the latter.
  322. W.OS << ELF::ElfMagic; // e_ident[EI_MAG0] to e_ident[EI_MAG3]
  323. W.OS << char(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
  324. // e_ident[EI_DATA]
  325. W.OS << char(W.Endian == support::little ? ELF::ELFDATA2LSB
  326. : ELF::ELFDATA2MSB);
  327. W.OS << char(ELF::EV_CURRENT); // e_ident[EI_VERSION]
  328. // e_ident[EI_OSABI]
  329. uint8_t OSABI = OWriter.TargetObjectWriter->getOSABI();
  330. W.OS << char(OSABI == ELF::ELFOSABI_NONE && OWriter.seenGnuAbi()
  331. ? int(ELF::ELFOSABI_GNU)
  332. : OSABI);
  333. // e_ident[EI_ABIVERSION]
  334. W.OS << char(OWriter.TargetObjectWriter->getABIVersion());
  335. W.OS.write_zeros(ELF::EI_NIDENT - ELF::EI_PAD);
  336. W.write<uint16_t>(ELF::ET_REL); // e_type
  337. W.write<uint16_t>(OWriter.TargetObjectWriter->getEMachine()); // e_machine = target
  338. W.write<uint32_t>(ELF::EV_CURRENT); // e_version
  339. WriteWord(0); // e_entry, no entry point in .o file
  340. WriteWord(0); // e_phoff, no program header for .o
  341. WriteWord(0); // e_shoff = sec hdr table off in bytes
  342. // e_flags = whatever the target wants
  343. W.write<uint32_t>(Asm.getELFHeaderEFlags());
  344. // e_ehsize = ELF header size
  345. W.write<uint16_t>(is64Bit() ? sizeof(ELF::Elf64_Ehdr)
  346. : sizeof(ELF::Elf32_Ehdr));
  347. W.write<uint16_t>(0); // e_phentsize = prog header entry size
  348. W.write<uint16_t>(0); // e_phnum = # prog header entries = 0
  349. // e_shentsize = Section header entry size
  350. W.write<uint16_t>(is64Bit() ? sizeof(ELF::Elf64_Shdr)
  351. : sizeof(ELF::Elf32_Shdr));
  352. // e_shnum = # of section header ents
  353. W.write<uint16_t>(0);
  354. // e_shstrndx = Section # of '.strtab'
  355. assert(StringTableIndex < ELF::SHN_LORESERVE);
  356. W.write<uint16_t>(StringTableIndex);
  357. }
  358. uint64_t ELFWriter::SymbolValue(const MCSymbol &Sym,
  359. const MCAsmLayout &Layout) {
  360. if (Sym.isCommon())
  361. return Sym.getCommonAlignment()->value();
  362. uint64_t Res;
  363. if (!Layout.getSymbolOffset(Sym, Res))
  364. return 0;
  365. if (Layout.getAssembler().isThumbFunc(&Sym))
  366. Res |= 1;
  367. return Res;
  368. }
  369. static uint8_t mergeTypeForSet(uint8_t origType, uint8_t newType) {
  370. uint8_t Type = newType;
  371. // Propagation rules:
  372. // IFUNC > FUNC > OBJECT > NOTYPE
  373. // TLS_OBJECT > OBJECT > NOTYPE
  374. //
  375. // dont let the new type degrade the old type
  376. switch (origType) {
  377. default:
  378. break;
  379. case ELF::STT_GNU_IFUNC:
  380. if (Type == ELF::STT_FUNC || Type == ELF::STT_OBJECT ||
  381. Type == ELF::STT_NOTYPE || Type == ELF::STT_TLS)
  382. Type = ELF::STT_GNU_IFUNC;
  383. break;
  384. case ELF::STT_FUNC:
  385. if (Type == ELF::STT_OBJECT || Type == ELF::STT_NOTYPE ||
  386. Type == ELF::STT_TLS)
  387. Type = ELF::STT_FUNC;
  388. break;
  389. case ELF::STT_OBJECT:
  390. if (Type == ELF::STT_NOTYPE)
  391. Type = ELF::STT_OBJECT;
  392. break;
  393. case ELF::STT_TLS:
  394. if (Type == ELF::STT_OBJECT || Type == ELF::STT_NOTYPE ||
  395. Type == ELF::STT_GNU_IFUNC || Type == ELF::STT_FUNC)
  396. Type = ELF::STT_TLS;
  397. break;
  398. }
  399. return Type;
  400. }
  401. static bool isIFunc(const MCSymbolELF *Symbol) {
  402. while (Symbol->getType() != ELF::STT_GNU_IFUNC) {
  403. const MCSymbolRefExpr *Value;
  404. if (!Symbol->isVariable() ||
  405. !(Value = dyn_cast<MCSymbolRefExpr>(Symbol->getVariableValue())) ||
  406. Value->getKind() != MCSymbolRefExpr::VK_None ||
  407. mergeTypeForSet(Symbol->getType(), ELF::STT_GNU_IFUNC) != ELF::STT_GNU_IFUNC)
  408. return false;
  409. Symbol = &cast<MCSymbolELF>(Value->getSymbol());
  410. }
  411. return true;
  412. }
  413. void ELFWriter::writeSymbol(SymbolTableWriter &Writer, uint32_t StringIndex,
  414. ELFSymbolData &MSD, const MCAsmLayout &Layout) {
  415. const auto &Symbol = cast<MCSymbolELF>(*MSD.Symbol);
  416. const MCSymbolELF *Base =
  417. cast_or_null<MCSymbolELF>(Layout.getBaseSymbol(Symbol));
  418. // This has to be in sync with when computeSymbolTable uses SHN_ABS or
  419. // SHN_COMMON.
  420. bool IsReserved = !Base || Symbol.isCommon();
  421. // Binding and Type share the same byte as upper and lower nibbles
  422. uint8_t Binding = Symbol.getBinding();
  423. uint8_t Type = Symbol.getType();
  424. if (isIFunc(&Symbol))
  425. Type = ELF::STT_GNU_IFUNC;
  426. if (Base) {
  427. Type = mergeTypeForSet(Type, Base->getType());
  428. }
  429. uint8_t Info = (Binding << 4) | Type;
  430. // Other and Visibility share the same byte with Visibility using the lower
  431. // 2 bits
  432. uint8_t Visibility = Symbol.getVisibility();
  433. uint8_t Other = Symbol.getOther() | Visibility;
  434. uint64_t Value = SymbolValue(*MSD.Symbol, Layout);
  435. uint64_t Size = 0;
  436. const MCExpr *ESize = MSD.Symbol->getSize();
  437. if (!ESize && Base) {
  438. // For expressions like .set y, x+1, if y's size is unset, inherit from x.
  439. ESize = Base->getSize();
  440. // For `.size x, 2; y = x; .size y, 1; z = y; z1 = z; .symver y, y@v1`, z,
  441. // z1, and y@v1's st_size equals y's. However, `Base` is `x` which will give
  442. // us 2. Follow the MCSymbolRefExpr assignment chain, which covers most
  443. // needs. MCBinaryExpr is not handled.
  444. const MCSymbolELF *Sym = &Symbol;
  445. while (Sym->isVariable()) {
  446. if (auto *Expr =
  447. dyn_cast<MCSymbolRefExpr>(Sym->getVariableValue(false))) {
  448. Sym = cast<MCSymbolELF>(&Expr->getSymbol());
  449. if (!Sym->getSize())
  450. continue;
  451. ESize = Sym->getSize();
  452. }
  453. break;
  454. }
  455. }
  456. if (ESize) {
  457. int64_t Res;
  458. if (!ESize->evaluateKnownAbsolute(Res, Layout))
  459. report_fatal_error("Size expression must be absolute.");
  460. Size = Res;
  461. }
  462. // Write out the symbol table entry
  463. Writer.writeSymbol(StringIndex, Info, Value, Size, Other, MSD.SectionIndex,
  464. IsReserved);
  465. }
  466. bool ELFWriter::isInSymtab(const MCAsmLayout &Layout, const MCSymbolELF &Symbol,
  467. bool Used, bool Renamed) {
  468. if (Symbol.isVariable()) {
  469. const MCExpr *Expr = Symbol.getVariableValue();
  470. // Target Expressions that are always inlined do not appear in the symtab
  471. if (const auto *T = dyn_cast<MCTargetExpr>(Expr))
  472. if (T->inlineAssignedExpr())
  473. return false;
  474. if (const MCSymbolRefExpr *Ref = dyn_cast<MCSymbolRefExpr>(Expr)) {
  475. if (Ref->getKind() == MCSymbolRefExpr::VK_WEAKREF)
  476. return false;
  477. }
  478. }
  479. if (Used)
  480. return true;
  481. if (Renamed)
  482. return false;
  483. if (Symbol.isVariable() && Symbol.isUndefined()) {
  484. // FIXME: this is here just to diagnose the case of a var = commmon_sym.
  485. Layout.getBaseSymbol(Symbol);
  486. return false;
  487. }
  488. if (Symbol.isTemporary())
  489. return false;
  490. if (Symbol.getType() == ELF::STT_SECTION)
  491. return false;
  492. return true;
  493. }
  494. void ELFWriter::createMemtagRelocs(MCAssembler &Asm) {
  495. MCSectionELF *MemtagRelocs = nullptr;
  496. for (const MCSymbol &Sym : Asm.symbols()) {
  497. const auto &SymE = cast<MCSymbolELF>(Sym);
  498. if (!SymE.isMemtag())
  499. continue;
  500. if (MemtagRelocs == nullptr) {
  501. MemtagRelocs = OWriter.TargetObjectWriter->getMemtagRelocsSection(Asm.getContext());
  502. if (MemtagRelocs == nullptr)
  503. report_fatal_error("Tagged globals are not available on this architecture.");
  504. Asm.registerSection(*MemtagRelocs);
  505. }
  506. ELFRelocationEntry Rec(0, &SymE, ELF::R_AARCH64_NONE, 0, nullptr, 0);
  507. OWriter.Relocations[MemtagRelocs].push_back(Rec);
  508. }
  509. }
  510. void ELFWriter::computeSymbolTable(
  511. MCAssembler &Asm, const MCAsmLayout &Layout,
  512. const SectionIndexMapTy &SectionIndexMap, const RevGroupMapTy &RevGroupMap,
  513. SectionOffsetsTy &SectionOffsets) {
  514. MCContext &Ctx = Asm.getContext();
  515. SymbolTableWriter Writer(*this, is64Bit());
  516. // Symbol table
  517. unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
  518. MCSectionELF *SymtabSection =
  519. Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0, EntrySize);
  520. SymtabSection->setAlignment(is64Bit() ? Align(8) : Align(4));
  521. SymbolTableIndex = addToSectionTable(SymtabSection);
  522. uint64_t SecStart = align(SymtabSection->getAlign());
  523. // The first entry is the undefined symbol entry.
  524. Writer.writeSymbol(0, 0, 0, 0, 0, 0, false);
  525. std::vector<ELFSymbolData> LocalSymbolData;
  526. std::vector<ELFSymbolData> ExternalSymbolData;
  527. MutableArrayRef<std::pair<std::string, size_t>> FileNames =
  528. Asm.getFileNames();
  529. for (const std::pair<std::string, size_t> &F : FileNames)
  530. StrTabBuilder.add(F.first);
  531. // Add the data for the symbols.
  532. bool HasLargeSectionIndex = false;
  533. for (auto It : llvm::enumerate(Asm.symbols())) {
  534. const auto &Symbol = cast<MCSymbolELF>(It.value());
  535. bool Used = Symbol.isUsedInReloc();
  536. bool WeakrefUsed = Symbol.isWeakrefUsedInReloc();
  537. bool isSignature = Symbol.isSignature();
  538. if (!isInSymtab(Layout, Symbol, Used || WeakrefUsed || isSignature,
  539. OWriter.Renames.count(&Symbol)))
  540. continue;
  541. if (Symbol.isTemporary() && Symbol.isUndefined()) {
  542. Ctx.reportError(SMLoc(), "Undefined temporary symbol " + Symbol.getName());
  543. continue;
  544. }
  545. ELFSymbolData MSD;
  546. MSD.Symbol = cast<MCSymbolELF>(&Symbol);
  547. MSD.Order = It.index();
  548. bool Local = Symbol.getBinding() == ELF::STB_LOCAL;
  549. assert(Local || !Symbol.isTemporary());
  550. if (Symbol.isAbsolute()) {
  551. MSD.SectionIndex = ELF::SHN_ABS;
  552. } else if (Symbol.isCommon()) {
  553. if (Symbol.isTargetCommon()) {
  554. MSD.SectionIndex = Symbol.getIndex();
  555. } else {
  556. assert(!Local);
  557. MSD.SectionIndex = ELF::SHN_COMMON;
  558. }
  559. } else if (Symbol.isUndefined()) {
  560. if (isSignature && !Used) {
  561. MSD.SectionIndex = RevGroupMap.lookup(&Symbol);
  562. if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
  563. HasLargeSectionIndex = true;
  564. } else {
  565. MSD.SectionIndex = ELF::SHN_UNDEF;
  566. }
  567. } else {
  568. const MCSectionELF &Section =
  569. static_cast<const MCSectionELF &>(Symbol.getSection());
  570. // We may end up with a situation when section symbol is technically
  571. // defined, but should not be. That happens because we explicitly
  572. // pre-create few .debug_* sections to have accessors.
  573. // And if these sections were not really defined in the code, but were
  574. // referenced, we simply error out.
  575. if (!Section.isRegistered()) {
  576. assert(static_cast<const MCSymbolELF &>(Symbol).getType() ==
  577. ELF::STT_SECTION);
  578. Ctx.reportError(SMLoc(),
  579. "Undefined section reference: " + Symbol.getName());
  580. continue;
  581. }
  582. if (Mode == NonDwoOnly && isDwoSection(Section))
  583. continue;
  584. MSD.SectionIndex = SectionIndexMap.lookup(&Section);
  585. assert(MSD.SectionIndex && "Invalid section index!");
  586. if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
  587. HasLargeSectionIndex = true;
  588. }
  589. StringRef Name = Symbol.getName();
  590. // Sections have their own string table
  591. if (Symbol.getType() != ELF::STT_SECTION) {
  592. MSD.Name = Name;
  593. StrTabBuilder.add(Name);
  594. }
  595. if (Local)
  596. LocalSymbolData.push_back(MSD);
  597. else
  598. ExternalSymbolData.push_back(MSD);
  599. }
  600. // This holds the .symtab_shndx section index.
  601. unsigned SymtabShndxSectionIndex = 0;
  602. if (HasLargeSectionIndex) {
  603. MCSectionELF *SymtabShndxSection =
  604. Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0, 4);
  605. SymtabShndxSectionIndex = addToSectionTable(SymtabShndxSection);
  606. SymtabShndxSection->setAlignment(Align(4));
  607. }
  608. StrTabBuilder.finalize();
  609. // Make the first STT_FILE precede previous local symbols.
  610. unsigned Index = 1;
  611. auto FileNameIt = FileNames.begin();
  612. if (!FileNames.empty())
  613. FileNames[0].second = 0;
  614. for (ELFSymbolData &MSD : LocalSymbolData) {
  615. // Emit STT_FILE symbols before their associated local symbols.
  616. for (; FileNameIt != FileNames.end() && FileNameIt->second <= MSD.Order;
  617. ++FileNameIt) {
  618. Writer.writeSymbol(StrTabBuilder.getOffset(FileNameIt->first),
  619. ELF::STT_FILE | ELF::STB_LOCAL, 0, 0, ELF::STV_DEFAULT,
  620. ELF::SHN_ABS, true);
  621. ++Index;
  622. }
  623. unsigned StringIndex = MSD.Symbol->getType() == ELF::STT_SECTION
  624. ? 0
  625. : StrTabBuilder.getOffset(MSD.Name);
  626. MSD.Symbol->setIndex(Index++);
  627. writeSymbol(Writer, StringIndex, MSD, Layout);
  628. }
  629. for (; FileNameIt != FileNames.end(); ++FileNameIt) {
  630. Writer.writeSymbol(StrTabBuilder.getOffset(FileNameIt->first),
  631. ELF::STT_FILE | ELF::STB_LOCAL, 0, 0, ELF::STV_DEFAULT,
  632. ELF::SHN_ABS, true);
  633. ++Index;
  634. }
  635. // Write the symbol table entries.
  636. LastLocalSymbolIndex = Index;
  637. for (ELFSymbolData &MSD : ExternalSymbolData) {
  638. unsigned StringIndex = StrTabBuilder.getOffset(MSD.Name);
  639. MSD.Symbol->setIndex(Index++);
  640. writeSymbol(Writer, StringIndex, MSD, Layout);
  641. assert(MSD.Symbol->getBinding() != ELF::STB_LOCAL);
  642. }
  643. uint64_t SecEnd = W.OS.tell();
  644. SectionOffsets[SymtabSection] = std::make_pair(SecStart, SecEnd);
  645. ArrayRef<uint32_t> ShndxIndexes = Writer.getShndxIndexes();
  646. if (ShndxIndexes.empty()) {
  647. assert(SymtabShndxSectionIndex == 0);
  648. return;
  649. }
  650. assert(SymtabShndxSectionIndex != 0);
  651. SecStart = W.OS.tell();
  652. const MCSectionELF *SymtabShndxSection =
  653. SectionTable[SymtabShndxSectionIndex - 1];
  654. for (uint32_t Index : ShndxIndexes)
  655. write(Index);
  656. SecEnd = W.OS.tell();
  657. SectionOffsets[SymtabShndxSection] = std::make_pair(SecStart, SecEnd);
  658. }
  659. void ELFWriter::writeAddrsigSection() {
  660. for (const MCSymbol *Sym : OWriter.AddrsigSyms)
  661. if (Sym->getIndex() != 0)
  662. encodeULEB128(Sym->getIndex(), W.OS);
  663. }
  664. MCSectionELF *ELFWriter::createRelocationSection(MCContext &Ctx,
  665. const MCSectionELF &Sec) {
  666. if (OWriter.Relocations[&Sec].empty())
  667. return nullptr;
  668. const StringRef SectionName = Sec.getName();
  669. bool Rela = usesRela(Sec);
  670. std::string RelaSectionName = Rela ? ".rela" : ".rel";
  671. RelaSectionName += SectionName;
  672. unsigned EntrySize;
  673. if (Rela)
  674. EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
  675. else
  676. EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
  677. unsigned Flags = ELF::SHF_INFO_LINK;
  678. if (Sec.getFlags() & ELF::SHF_GROUP)
  679. Flags = ELF::SHF_GROUP;
  680. MCSectionELF *RelaSection = Ctx.createELFRelSection(
  681. RelaSectionName, Rela ? ELF::SHT_RELA : ELF::SHT_REL, Flags, EntrySize,
  682. Sec.getGroup(), &Sec);
  683. RelaSection->setAlignment(is64Bit() ? Align(8) : Align(4));
  684. return RelaSection;
  685. }
  686. // Include the debug info compression header.
  687. bool ELFWriter::maybeWriteCompression(
  688. uint32_t ChType, uint64_t Size,
  689. SmallVectorImpl<uint8_t> &CompressedContents, Align Alignment) {
  690. uint64_t HdrSize =
  691. is64Bit() ? sizeof(ELF::Elf32_Chdr) : sizeof(ELF::Elf64_Chdr);
  692. if (Size <= HdrSize + CompressedContents.size())
  693. return false;
  694. // Platform specific header is followed by compressed data.
  695. if (is64Bit()) {
  696. // Write Elf64_Chdr header.
  697. write(static_cast<ELF::Elf64_Word>(ChType));
  698. write(static_cast<ELF::Elf64_Word>(0)); // ch_reserved field.
  699. write(static_cast<ELF::Elf64_Xword>(Size));
  700. write(static_cast<ELF::Elf64_Xword>(Alignment.value()));
  701. } else {
  702. // Write Elf32_Chdr header otherwise.
  703. write(static_cast<ELF::Elf32_Word>(ChType));
  704. write(static_cast<ELF::Elf32_Word>(Size));
  705. write(static_cast<ELF::Elf32_Word>(Alignment.value()));
  706. }
  707. return true;
  708. }
  709. void ELFWriter::writeSectionData(const MCAssembler &Asm, MCSection &Sec,
  710. const MCAsmLayout &Layout) {
  711. MCSectionELF &Section = static_cast<MCSectionELF &>(Sec);
  712. StringRef SectionName = Section.getName();
  713. auto &MC = Asm.getContext();
  714. const auto &MAI = MC.getAsmInfo();
  715. const DebugCompressionType CompressionType = MAI->compressDebugSections();
  716. if (CompressionType == DebugCompressionType::None ||
  717. !SectionName.startswith(".debug_")) {
  718. Asm.writeSectionData(W.OS, &Section, Layout);
  719. return;
  720. }
  721. SmallVector<char, 128> UncompressedData;
  722. raw_svector_ostream VecOS(UncompressedData);
  723. Asm.writeSectionData(VecOS, &Section, Layout);
  724. ArrayRef<uint8_t> Uncompressed =
  725. ArrayRef(reinterpret_cast<uint8_t *>(UncompressedData.data()),
  726. UncompressedData.size());
  727. SmallVector<uint8_t, 128> Compressed;
  728. uint32_t ChType;
  729. switch (CompressionType) {
  730. case DebugCompressionType::None:
  731. llvm_unreachable("has been handled");
  732. case DebugCompressionType::Zlib:
  733. ChType = ELF::ELFCOMPRESS_ZLIB;
  734. break;
  735. case DebugCompressionType::Zstd:
  736. ChType = ELF::ELFCOMPRESS_ZSTD;
  737. break;
  738. }
  739. compression::compress(compression::Params(CompressionType), Uncompressed,
  740. Compressed);
  741. if (!maybeWriteCompression(ChType, UncompressedData.size(), Compressed,
  742. Sec.getAlign())) {
  743. W.OS << UncompressedData;
  744. return;
  745. }
  746. Section.setFlags(Section.getFlags() | ELF::SHF_COMPRESSED);
  747. // Alignment field should reflect the requirements of
  748. // the compressed section header.
  749. Section.setAlignment(is64Bit() ? Align(8) : Align(4));
  750. W.OS << toStringRef(Compressed);
  751. }
  752. void ELFWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
  753. uint64_t Address, uint64_t Offset,
  754. uint64_t Size, uint32_t Link, uint32_t Info,
  755. MaybeAlign Alignment, uint64_t EntrySize) {
  756. W.write<uint32_t>(Name); // sh_name: index into string table
  757. W.write<uint32_t>(Type); // sh_type
  758. WriteWord(Flags); // sh_flags
  759. WriteWord(Address); // sh_addr
  760. WriteWord(Offset); // sh_offset
  761. WriteWord(Size); // sh_size
  762. W.write<uint32_t>(Link); // sh_link
  763. W.write<uint32_t>(Info); // sh_info
  764. WriteWord(Alignment ? Alignment->value() : 0); // sh_addralign
  765. WriteWord(EntrySize); // sh_entsize
  766. }
  767. void ELFWriter::writeRelocations(const MCAssembler &Asm,
  768. const MCSectionELF &Sec) {
  769. std::vector<ELFRelocationEntry> &Relocs = OWriter.Relocations[&Sec];
  770. // We record relocations by pushing to the end of a vector. Reverse the vector
  771. // to get the relocations in the order they were created.
  772. // In most cases that is not important, but it can be for special sections
  773. // (.eh_frame) or specific relocations (TLS optimizations on SystemZ).
  774. std::reverse(Relocs.begin(), Relocs.end());
  775. // Sort the relocation entries. MIPS needs this.
  776. OWriter.TargetObjectWriter->sortRelocs(Asm, Relocs);
  777. const bool Rela = usesRela(Sec);
  778. for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
  779. const ELFRelocationEntry &Entry = Relocs[e - i - 1];
  780. unsigned Index = Entry.Symbol ? Entry.Symbol->getIndex() : 0;
  781. if (is64Bit()) {
  782. write(Entry.Offset);
  783. if (OWriter.TargetObjectWriter->getEMachine() == ELF::EM_MIPS) {
  784. write(uint32_t(Index));
  785. write(OWriter.TargetObjectWriter->getRSsym(Entry.Type));
  786. write(OWriter.TargetObjectWriter->getRType3(Entry.Type));
  787. write(OWriter.TargetObjectWriter->getRType2(Entry.Type));
  788. write(OWriter.TargetObjectWriter->getRType(Entry.Type));
  789. } else {
  790. struct ELF::Elf64_Rela ERE64;
  791. ERE64.setSymbolAndType(Index, Entry.Type);
  792. write(ERE64.r_info);
  793. }
  794. if (Rela)
  795. write(Entry.Addend);
  796. } else {
  797. write(uint32_t(Entry.Offset));
  798. struct ELF::Elf32_Rela ERE32;
  799. ERE32.setSymbolAndType(Index, Entry.Type);
  800. write(ERE32.r_info);
  801. if (Rela)
  802. write(uint32_t(Entry.Addend));
  803. if (OWriter.TargetObjectWriter->getEMachine() == ELF::EM_MIPS) {
  804. if (uint32_t RType =
  805. OWriter.TargetObjectWriter->getRType2(Entry.Type)) {
  806. write(uint32_t(Entry.Offset));
  807. ERE32.setSymbolAndType(0, RType);
  808. write(ERE32.r_info);
  809. write(uint32_t(0));
  810. }
  811. if (uint32_t RType =
  812. OWriter.TargetObjectWriter->getRType3(Entry.Type)) {
  813. write(uint32_t(Entry.Offset));
  814. ERE32.setSymbolAndType(0, RType);
  815. write(ERE32.r_info);
  816. write(uint32_t(0));
  817. }
  818. }
  819. }
  820. }
  821. }
  822. void ELFWriter::writeSection(const SectionIndexMapTy &SectionIndexMap,
  823. uint32_t GroupSymbolIndex, uint64_t Offset,
  824. uint64_t Size, const MCSectionELF &Section) {
  825. uint64_t sh_link = 0;
  826. uint64_t sh_info = 0;
  827. switch(Section.getType()) {
  828. default:
  829. // Nothing to do.
  830. break;
  831. case ELF::SHT_DYNAMIC:
  832. llvm_unreachable("SHT_DYNAMIC in a relocatable object");
  833. case ELF::SHT_REL:
  834. case ELF::SHT_RELA: {
  835. sh_link = SymbolTableIndex;
  836. assert(sh_link && ".symtab not found");
  837. const MCSection *InfoSection = Section.getLinkedToSection();
  838. sh_info = SectionIndexMap.lookup(cast<MCSectionELF>(InfoSection));
  839. break;
  840. }
  841. case ELF::SHT_SYMTAB:
  842. sh_link = StringTableIndex;
  843. sh_info = LastLocalSymbolIndex;
  844. break;
  845. case ELF::SHT_SYMTAB_SHNDX:
  846. case ELF::SHT_LLVM_CALL_GRAPH_PROFILE:
  847. case ELF::SHT_LLVM_ADDRSIG:
  848. sh_link = SymbolTableIndex;
  849. break;
  850. case ELF::SHT_GROUP:
  851. sh_link = SymbolTableIndex;
  852. sh_info = GroupSymbolIndex;
  853. break;
  854. }
  855. if (Section.getFlags() & ELF::SHF_LINK_ORDER) {
  856. // If the value in the associated metadata is not a definition, Sym will be
  857. // undefined. Represent this with sh_link=0.
  858. const MCSymbol *Sym = Section.getLinkedToSymbol();
  859. if (Sym && Sym->isInSection()) {
  860. const MCSectionELF *Sec = cast<MCSectionELF>(&Sym->getSection());
  861. sh_link = SectionIndexMap.lookup(Sec);
  862. }
  863. }
  864. WriteSecHdrEntry(StrTabBuilder.getOffset(Section.getName()),
  865. Section.getType(), Section.getFlags(), 0, Offset, Size,
  866. sh_link, sh_info, Section.getAlign(),
  867. Section.getEntrySize());
  868. }
  869. void ELFWriter::writeSectionHeader(
  870. const MCAsmLayout &Layout, const SectionIndexMapTy &SectionIndexMap,
  871. const SectionOffsetsTy &SectionOffsets) {
  872. const unsigned NumSections = SectionTable.size();
  873. // Null section first.
  874. uint64_t FirstSectionSize =
  875. (NumSections + 1) >= ELF::SHN_LORESERVE ? NumSections + 1 : 0;
  876. WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, 0, 0, std::nullopt, 0);
  877. for (const MCSectionELF *Section : SectionTable) {
  878. uint32_t GroupSymbolIndex;
  879. unsigned Type = Section->getType();
  880. if (Type != ELF::SHT_GROUP)
  881. GroupSymbolIndex = 0;
  882. else
  883. GroupSymbolIndex = Section->getGroup()->getIndex();
  884. const std::pair<uint64_t, uint64_t> &Offsets =
  885. SectionOffsets.find(Section)->second;
  886. uint64_t Size;
  887. if (Type == ELF::SHT_NOBITS)
  888. Size = Layout.getSectionAddressSize(Section);
  889. else
  890. Size = Offsets.second - Offsets.first;
  891. writeSection(SectionIndexMap, GroupSymbolIndex, Offsets.first, Size,
  892. *Section);
  893. }
  894. }
  895. uint64_t ELFWriter::writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) {
  896. uint64_t StartOffset = W.OS.tell();
  897. MCContext &Ctx = Asm.getContext();
  898. MCSectionELF *StrtabSection =
  899. Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0);
  900. StringTableIndex = addToSectionTable(StrtabSection);
  901. createMemtagRelocs(Asm);
  902. RevGroupMapTy RevGroupMap;
  903. SectionIndexMapTy SectionIndexMap;
  904. std::map<const MCSymbol *, std::vector<const MCSectionELF *>> GroupMembers;
  905. // Write out the ELF header ...
  906. writeHeader(Asm);
  907. // ... then the sections ...
  908. SectionOffsetsTy SectionOffsets;
  909. std::vector<MCSectionELF *> Groups;
  910. std::vector<MCSectionELF *> Relocations;
  911. for (MCSection &Sec : Asm) {
  912. MCSectionELF &Section = static_cast<MCSectionELF &>(Sec);
  913. if (Mode == NonDwoOnly && isDwoSection(Section))
  914. continue;
  915. if (Mode == DwoOnly && !isDwoSection(Section))
  916. continue;
  917. // Remember the offset into the file for this section.
  918. const uint64_t SecStart = align(Section.getAlign());
  919. const MCSymbolELF *SignatureSymbol = Section.getGroup();
  920. writeSectionData(Asm, Section, Layout);
  921. uint64_t SecEnd = W.OS.tell();
  922. SectionOffsets[&Section] = std::make_pair(SecStart, SecEnd);
  923. MCSectionELF *RelSection = createRelocationSection(Ctx, Section);
  924. if (SignatureSymbol) {
  925. unsigned &GroupIdx = RevGroupMap[SignatureSymbol];
  926. if (!GroupIdx) {
  927. MCSectionELF *Group =
  928. Ctx.createELFGroupSection(SignatureSymbol, Section.isComdat());
  929. GroupIdx = addToSectionTable(Group);
  930. Group->setAlignment(Align(4));
  931. Groups.push_back(Group);
  932. }
  933. std::vector<const MCSectionELF *> &Members =
  934. GroupMembers[SignatureSymbol];
  935. Members.push_back(&Section);
  936. if (RelSection)
  937. Members.push_back(RelSection);
  938. }
  939. SectionIndexMap[&Section] = addToSectionTable(&Section);
  940. if (RelSection) {
  941. SectionIndexMap[RelSection] = addToSectionTable(RelSection);
  942. Relocations.push_back(RelSection);
  943. }
  944. OWriter.TargetObjectWriter->addTargetSectionFlags(Ctx, Section);
  945. }
  946. for (MCSectionELF *Group : Groups) {
  947. // Remember the offset into the file for this section.
  948. const uint64_t SecStart = align(Group->getAlign());
  949. const MCSymbol *SignatureSymbol = Group->getGroup();
  950. assert(SignatureSymbol);
  951. write(uint32_t(Group->isComdat() ? unsigned(ELF::GRP_COMDAT) : 0));
  952. for (const MCSectionELF *Member : GroupMembers[SignatureSymbol]) {
  953. uint32_t SecIndex = SectionIndexMap.lookup(Member);
  954. write(SecIndex);
  955. }
  956. uint64_t SecEnd = W.OS.tell();
  957. SectionOffsets[Group] = std::make_pair(SecStart, SecEnd);
  958. }
  959. if (Mode == DwoOnly) {
  960. // dwo files don't have symbol tables or relocations, but they do have
  961. // string tables.
  962. StrTabBuilder.finalize();
  963. } else {
  964. MCSectionELF *AddrsigSection;
  965. if (OWriter.EmitAddrsigSection) {
  966. AddrsigSection = Ctx.getELFSection(".llvm_addrsig", ELF::SHT_LLVM_ADDRSIG,
  967. ELF::SHF_EXCLUDE);
  968. addToSectionTable(AddrsigSection);
  969. }
  970. // Compute symbol table information.
  971. computeSymbolTable(Asm, Layout, SectionIndexMap, RevGroupMap,
  972. SectionOffsets);
  973. for (MCSectionELF *RelSection : Relocations) {
  974. // Remember the offset into the file for this section.
  975. const uint64_t SecStart = align(RelSection->getAlign());
  976. writeRelocations(Asm,
  977. cast<MCSectionELF>(*RelSection->getLinkedToSection()));
  978. uint64_t SecEnd = W.OS.tell();
  979. SectionOffsets[RelSection] = std::make_pair(SecStart, SecEnd);
  980. }
  981. if (OWriter.EmitAddrsigSection) {
  982. uint64_t SecStart = W.OS.tell();
  983. writeAddrsigSection();
  984. uint64_t SecEnd = W.OS.tell();
  985. SectionOffsets[AddrsigSection] = std::make_pair(SecStart, SecEnd);
  986. }
  987. }
  988. {
  989. uint64_t SecStart = W.OS.tell();
  990. StrTabBuilder.write(W.OS);
  991. SectionOffsets[StrtabSection] = std::make_pair(SecStart, W.OS.tell());
  992. }
  993. const uint64_t SectionHeaderOffset = align(is64Bit() ? Align(8) : Align(4));
  994. // ... then the section header table ...
  995. writeSectionHeader(Layout, SectionIndexMap, SectionOffsets);
  996. uint16_t NumSections = support::endian::byte_swap<uint16_t>(
  997. (SectionTable.size() + 1 >= ELF::SHN_LORESERVE) ? (uint16_t)ELF::SHN_UNDEF
  998. : SectionTable.size() + 1,
  999. W.Endian);
  1000. unsigned NumSectionsOffset;
  1001. auto &Stream = static_cast<raw_pwrite_stream &>(W.OS);
  1002. if (is64Bit()) {
  1003. uint64_t Val =
  1004. support::endian::byte_swap<uint64_t>(SectionHeaderOffset, W.Endian);
  1005. Stream.pwrite(reinterpret_cast<char *>(&Val), sizeof(Val),
  1006. offsetof(ELF::Elf64_Ehdr, e_shoff));
  1007. NumSectionsOffset = offsetof(ELF::Elf64_Ehdr, e_shnum);
  1008. } else {
  1009. uint32_t Val =
  1010. support::endian::byte_swap<uint32_t>(SectionHeaderOffset, W.Endian);
  1011. Stream.pwrite(reinterpret_cast<char *>(&Val), sizeof(Val),
  1012. offsetof(ELF::Elf32_Ehdr, e_shoff));
  1013. NumSectionsOffset = offsetof(ELF::Elf32_Ehdr, e_shnum);
  1014. }
  1015. Stream.pwrite(reinterpret_cast<char *>(&NumSections), sizeof(NumSections),
  1016. NumSectionsOffset);
  1017. return W.OS.tell() - StartOffset;
  1018. }
  1019. bool ELFObjectWriter::hasRelocationAddend() const {
  1020. return TargetObjectWriter->hasRelocationAddend();
  1021. }
  1022. void ELFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
  1023. const MCAsmLayout &Layout) {
  1024. // The presence of symbol versions causes undefined symbols and
  1025. // versions declared with @@@ to be renamed.
  1026. for (const MCAssembler::Symver &S : Asm.Symvers) {
  1027. StringRef AliasName = S.Name;
  1028. const auto &Symbol = cast<MCSymbolELF>(*S.Sym);
  1029. size_t Pos = AliasName.find('@');
  1030. assert(Pos != StringRef::npos);
  1031. StringRef Prefix = AliasName.substr(0, Pos);
  1032. StringRef Rest = AliasName.substr(Pos);
  1033. StringRef Tail = Rest;
  1034. if (Rest.startswith("@@@"))
  1035. Tail = Rest.substr(Symbol.isUndefined() ? 2 : 1);
  1036. auto *Alias =
  1037. cast<MCSymbolELF>(Asm.getContext().getOrCreateSymbol(Prefix + Tail));
  1038. Asm.registerSymbol(*Alias);
  1039. const MCExpr *Value = MCSymbolRefExpr::create(&Symbol, Asm.getContext());
  1040. Alias->setVariableValue(Value);
  1041. // Aliases defined with .symvar copy the binding from the symbol they alias.
  1042. // This is the first place we are able to copy this information.
  1043. Alias->setBinding(Symbol.getBinding());
  1044. Alias->setVisibility(Symbol.getVisibility());
  1045. Alias->setOther(Symbol.getOther());
  1046. if (!Symbol.isUndefined() && S.KeepOriginalSym)
  1047. continue;
  1048. if (Symbol.isUndefined() && Rest.startswith("@@") &&
  1049. !Rest.startswith("@@@")) {
  1050. Asm.getContext().reportError(S.Loc, "default version symbol " +
  1051. AliasName + " must be defined");
  1052. continue;
  1053. }
  1054. if (Renames.count(&Symbol) && Renames[&Symbol] != Alias) {
  1055. Asm.getContext().reportError(S.Loc, Twine("multiple versions for ") +
  1056. Symbol.getName());
  1057. continue;
  1058. }
  1059. Renames.insert(std::make_pair(&Symbol, Alias));
  1060. }
  1061. for (const MCSymbol *&Sym : AddrsigSyms) {
  1062. if (const MCSymbol *R = Renames.lookup(cast<MCSymbolELF>(Sym)))
  1063. Sym = R;
  1064. if (Sym->isInSection() && Sym->getName().startswith(".L"))
  1065. Sym = Sym->getSection().getBeginSymbol();
  1066. Sym->setUsedInReloc();
  1067. }
  1068. }
  1069. // It is always valid to create a relocation with a symbol. It is preferable
  1070. // to use a relocation with a section if that is possible. Using the section
  1071. // allows us to omit some local symbols from the symbol table.
  1072. bool ELFObjectWriter::shouldRelocateWithSymbol(const MCAssembler &Asm,
  1073. const MCSymbolRefExpr *RefA,
  1074. const MCSymbolELF *Sym,
  1075. uint64_t C,
  1076. unsigned Type) const {
  1077. // A PCRel relocation to an absolute value has no symbol (or section). We
  1078. // represent that with a relocation to a null section.
  1079. if (!RefA)
  1080. return false;
  1081. MCSymbolRefExpr::VariantKind Kind = RefA->getKind();
  1082. switch (Kind) {
  1083. default:
  1084. break;
  1085. // The .odp creation emits a relocation against the symbol ".TOC." which
  1086. // create a R_PPC64_TOC relocation. However the relocation symbol name
  1087. // in final object creation should be NULL, since the symbol does not
  1088. // really exist, it is just the reference to TOC base for the current
  1089. // object file. Since the symbol is undefined, returning false results
  1090. // in a relocation with a null section which is the desired result.
  1091. case MCSymbolRefExpr::VK_PPC_TOCBASE:
  1092. return false;
  1093. // These VariantKind cause the relocation to refer to something other than
  1094. // the symbol itself, like a linker generated table. Since the address of
  1095. // symbol is not relevant, we cannot replace the symbol with the
  1096. // section and patch the difference in the addend.
  1097. case MCSymbolRefExpr::VK_GOT:
  1098. case MCSymbolRefExpr::VK_PLT:
  1099. case MCSymbolRefExpr::VK_GOTPCREL:
  1100. case MCSymbolRefExpr::VK_GOTPCREL_NORELAX:
  1101. case MCSymbolRefExpr::VK_PPC_GOT_LO:
  1102. case MCSymbolRefExpr::VK_PPC_GOT_HI:
  1103. case MCSymbolRefExpr::VK_PPC_GOT_HA:
  1104. return true;
  1105. }
  1106. // An undefined symbol is not in any section, so the relocation has to point
  1107. // to the symbol itself.
  1108. assert(Sym && "Expected a symbol");
  1109. if (Sym->isUndefined())
  1110. return true;
  1111. // For memory-tagged symbols, ensure that the relocation uses the symbol. For
  1112. // tagged symbols, we emit an empty relocation (R_AARCH64_NONE) in a special
  1113. // section (SHT_AARCH64_MEMTAG_GLOBALS_STATIC) to indicate to the linker that
  1114. // this global needs to be tagged. In addition, the linker needs to know
  1115. // whether to emit a special addend when relocating `end` symbols, and this
  1116. // can only be determined by the attributes of the symbol itself.
  1117. if (Sym->isMemtag())
  1118. return true;
  1119. unsigned Binding = Sym->getBinding();
  1120. switch(Binding) {
  1121. default:
  1122. llvm_unreachable("Invalid Binding");
  1123. case ELF::STB_LOCAL:
  1124. break;
  1125. case ELF::STB_WEAK:
  1126. // If the symbol is weak, it might be overridden by a symbol in another
  1127. // file. The relocation has to point to the symbol so that the linker
  1128. // can update it.
  1129. return true;
  1130. case ELF::STB_GLOBAL:
  1131. case ELF::STB_GNU_UNIQUE:
  1132. // Global ELF symbols can be preempted by the dynamic linker. The relocation
  1133. // has to point to the symbol for a reason analogous to the STB_WEAK case.
  1134. return true;
  1135. }
  1136. // Keep symbol type for a local ifunc because it may result in an IRELATIVE
  1137. // reloc that the dynamic loader will use to resolve the address at startup
  1138. // time.
  1139. if (Sym->getType() == ELF::STT_GNU_IFUNC)
  1140. return true;
  1141. // If a relocation points to a mergeable section, we have to be careful.
  1142. // If the offset is zero, a relocation with the section will encode the
  1143. // same information. With a non-zero offset, the situation is different.
  1144. // For example, a relocation can point 42 bytes past the end of a string.
  1145. // If we change such a relocation to use the section, the linker would think
  1146. // that it pointed to another string and subtracting 42 at runtime will
  1147. // produce the wrong value.
  1148. if (Sym->isInSection()) {
  1149. auto &Sec = cast<MCSectionELF>(Sym->getSection());
  1150. unsigned Flags = Sec.getFlags();
  1151. if (Flags & ELF::SHF_MERGE) {
  1152. if (C != 0)
  1153. return true;
  1154. // gold<2.34 incorrectly ignored the addend for R_386_GOTOFF (9)
  1155. // (http://sourceware.org/PR16794).
  1156. if (TargetObjectWriter->getEMachine() == ELF::EM_386 &&
  1157. Type == ELF::R_386_GOTOFF)
  1158. return true;
  1159. // ld.lld handles R_MIPS_HI16/R_MIPS_LO16 separately, not as a whole, so
  1160. // it doesn't know that an R_MIPS_HI16 with implicit addend 1 and an
  1161. // R_MIPS_LO16 with implicit addend -32768 represents 32768, which is in
  1162. // range of a MergeInputSection. We could introduce a new RelExpr member
  1163. // (like R_RISCV_PC_INDIRECT for R_RISCV_PCREL_HI20 / R_RISCV_PCREL_LO12)
  1164. // but the complexity is unnecessary given that GNU as keeps the original
  1165. // symbol for this case as well.
  1166. if (TargetObjectWriter->getEMachine() == ELF::EM_MIPS &&
  1167. !hasRelocationAddend())
  1168. return true;
  1169. }
  1170. // Most TLS relocations use a got, so they need the symbol. Even those that
  1171. // are just an offset (@tpoff), require a symbol in gold versions before
  1172. // 5efeedf61e4fe720fd3e9a08e6c91c10abb66d42 (2014-09-26) which fixed
  1173. // http://sourceware.org/PR16773.
  1174. if (Flags & ELF::SHF_TLS)
  1175. return true;
  1176. }
  1177. // If the symbol is a thumb function the final relocation must set the lowest
  1178. // bit. With a symbol that is done by just having the symbol have that bit
  1179. // set, so we would lose the bit if we relocated with the section.
  1180. // FIXME: We could use the section but add the bit to the relocation value.
  1181. if (Asm.isThumbFunc(Sym))
  1182. return true;
  1183. if (TargetObjectWriter->needsRelocateWithSymbol(*Sym, Type))
  1184. return true;
  1185. return false;
  1186. }
  1187. void ELFObjectWriter::recordRelocation(MCAssembler &Asm,
  1188. const MCAsmLayout &Layout,
  1189. const MCFragment *Fragment,
  1190. const MCFixup &Fixup, MCValue Target,
  1191. uint64_t &FixedValue) {
  1192. MCAsmBackend &Backend = Asm.getBackend();
  1193. bool IsPCRel = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
  1194. MCFixupKindInfo::FKF_IsPCRel;
  1195. const MCSectionELF &FixupSection = cast<MCSectionELF>(*Fragment->getParent());
  1196. uint64_t C = Target.getConstant();
  1197. uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
  1198. MCContext &Ctx = Asm.getContext();
  1199. if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
  1200. const auto &SymB = cast<MCSymbolELF>(RefB->getSymbol());
  1201. if (SymB.isUndefined()) {
  1202. Ctx.reportError(Fixup.getLoc(),
  1203. Twine("symbol '") + SymB.getName() +
  1204. "' can not be undefined in a subtraction expression");
  1205. return;
  1206. }
  1207. assert(!SymB.isAbsolute() && "Should have been folded");
  1208. const MCSection &SecB = SymB.getSection();
  1209. if (&SecB != &FixupSection) {
  1210. Ctx.reportError(Fixup.getLoc(),
  1211. "Cannot represent a difference across sections");
  1212. return;
  1213. }
  1214. assert(!IsPCRel && "should have been folded");
  1215. IsPCRel = true;
  1216. C += FixupOffset - Layout.getSymbolOffset(SymB);
  1217. }
  1218. // We either rejected the fixup or folded B into C at this point.
  1219. const MCSymbolRefExpr *RefA = Target.getSymA();
  1220. const auto *SymA = RefA ? cast<MCSymbolELF>(&RefA->getSymbol()) : nullptr;
  1221. bool ViaWeakRef = false;
  1222. if (SymA && SymA->isVariable()) {
  1223. const MCExpr *Expr = SymA->getVariableValue();
  1224. if (const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr)) {
  1225. if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF) {
  1226. SymA = cast<MCSymbolELF>(&Inner->getSymbol());
  1227. ViaWeakRef = true;
  1228. }
  1229. }
  1230. }
  1231. const MCSectionELF *SecA = (SymA && SymA->isInSection())
  1232. ? cast<MCSectionELF>(&SymA->getSection())
  1233. : nullptr;
  1234. if (!checkRelocation(Ctx, Fixup.getLoc(), &FixupSection, SecA))
  1235. return;
  1236. unsigned Type = TargetObjectWriter->getRelocType(Ctx, Target, Fixup, IsPCRel);
  1237. const auto *Parent = cast<MCSectionELF>(Fragment->getParent());
  1238. // Emiting relocation with sybmol for CG Profile to help with --cg-profile.
  1239. bool RelocateWithSymbol =
  1240. shouldRelocateWithSymbol(Asm, RefA, SymA, C, Type) ||
  1241. (Parent->getType() == ELF::SHT_LLVM_CALL_GRAPH_PROFILE);
  1242. uint64_t Addend = 0;
  1243. FixedValue = !RelocateWithSymbol && SymA && !SymA->isUndefined()
  1244. ? C + Layout.getSymbolOffset(*SymA)
  1245. : C;
  1246. if (hasRelocationAddend()) {
  1247. Addend = FixedValue;
  1248. FixedValue = 0;
  1249. }
  1250. if (!RelocateWithSymbol) {
  1251. const auto *SectionSymbol =
  1252. SecA ? cast<MCSymbolELF>(SecA->getBeginSymbol()) : nullptr;
  1253. if (SectionSymbol)
  1254. SectionSymbol->setUsedInReloc();
  1255. ELFRelocationEntry Rec(FixupOffset, SectionSymbol, Type, Addend, SymA, C);
  1256. Relocations[&FixupSection].push_back(Rec);
  1257. return;
  1258. }
  1259. const MCSymbolELF *RenamedSymA = SymA;
  1260. if (SymA) {
  1261. if (const MCSymbolELF *R = Renames.lookup(SymA))
  1262. RenamedSymA = R;
  1263. if (ViaWeakRef)
  1264. RenamedSymA->setIsWeakrefUsedInReloc();
  1265. else
  1266. RenamedSymA->setUsedInReloc();
  1267. }
  1268. ELFRelocationEntry Rec(FixupOffset, RenamedSymA, Type, Addend, SymA, C);
  1269. Relocations[&FixupSection].push_back(Rec);
  1270. }
  1271. bool ELFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
  1272. const MCAssembler &Asm, const MCSymbol &SA, const MCFragment &FB,
  1273. bool InSet, bool IsPCRel) const {
  1274. const auto &SymA = cast<MCSymbolELF>(SA);
  1275. if (IsPCRel) {
  1276. assert(!InSet);
  1277. if (SymA.getBinding() != ELF::STB_LOCAL ||
  1278. SymA.getType() == ELF::STT_GNU_IFUNC)
  1279. return false;
  1280. }
  1281. return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
  1282. InSet, IsPCRel);
  1283. }
  1284. std::unique_ptr<MCObjectWriter>
  1285. llvm::createELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
  1286. raw_pwrite_stream &OS, bool IsLittleEndian) {
  1287. return std::make_unique<ELFSingleObjectWriter>(std::move(MOTW), OS,
  1288. IsLittleEndian);
  1289. }
  1290. std::unique_ptr<MCObjectWriter>
  1291. llvm::createELFDwoObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
  1292. raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS,
  1293. bool IsLittleEndian) {
  1294. return std::make_unique<ELFDwoObjectWriter>(std::move(MOTW), OS, DwoOS,
  1295. IsLittleEndian);
  1296. }