ELFObject.h 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  1. //===- ELFObject.h ----------------------------------------------*- C++ -*-===//
  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. #ifndef LLVM_LIB_OBJCOPY_ELF_ELFOBJECT_H
  9. #define LLVM_LIB_OBJCOPY_ELF_ELFOBJECT_H
  10. #include "llvm/ADT/ArrayRef.h"
  11. #include "llvm/ADT/StringRef.h"
  12. #include "llvm/ADT/Twine.h"
  13. #include "llvm/BinaryFormat/ELF.h"
  14. #include "llvm/MC/StringTableBuilder.h"
  15. #include "llvm/ObjCopy/CommonConfig.h"
  16. #include "llvm/Object/ELFObjectFile.h"
  17. #include "llvm/Support/Errc.h"
  18. #include "llvm/Support/FileOutputBuffer.h"
  19. #include "llvm/Support/MemoryBuffer.h"
  20. #include <cstddef>
  21. #include <cstdint>
  22. #include <functional>
  23. #include <memory>
  24. #include <set>
  25. #include <vector>
  26. namespace llvm {
  27. enum class DebugCompressionType;
  28. namespace objcopy {
  29. namespace elf {
  30. class SectionBase;
  31. class Section;
  32. class OwnedDataSection;
  33. class StringTableSection;
  34. class SymbolTableSection;
  35. class RelocationSection;
  36. class DynamicRelocationSection;
  37. class GnuDebugLinkSection;
  38. class GroupSection;
  39. class SectionIndexSection;
  40. class CompressedSection;
  41. class DecompressedSection;
  42. class Segment;
  43. class Object;
  44. struct Symbol;
  45. class SectionTableRef {
  46. ArrayRef<std::unique_ptr<SectionBase>> Sections;
  47. public:
  48. using iterator = pointee_iterator<const std::unique_ptr<SectionBase> *>;
  49. explicit SectionTableRef(ArrayRef<std::unique_ptr<SectionBase>> Secs)
  50. : Sections(Secs) {}
  51. SectionTableRef(const SectionTableRef &) = default;
  52. iterator begin() const { return iterator(Sections.data()); }
  53. iterator end() const { return iterator(Sections.data() + Sections.size()); }
  54. size_t size() const { return Sections.size(); }
  55. Expected<SectionBase *> getSection(uint32_t Index, Twine ErrMsg);
  56. template <class T>
  57. Expected<T *> getSectionOfType(uint32_t Index, Twine IndexErrMsg,
  58. Twine TypeErrMsg);
  59. };
  60. enum ElfType { ELFT_ELF32LE, ELFT_ELF64LE, ELFT_ELF32BE, ELFT_ELF64BE };
  61. class SectionVisitor {
  62. public:
  63. virtual ~SectionVisitor() = default;
  64. virtual Error visit(const Section &Sec) = 0;
  65. virtual Error visit(const OwnedDataSection &Sec) = 0;
  66. virtual Error visit(const StringTableSection &Sec) = 0;
  67. virtual Error visit(const SymbolTableSection &Sec) = 0;
  68. virtual Error visit(const RelocationSection &Sec) = 0;
  69. virtual Error visit(const DynamicRelocationSection &Sec) = 0;
  70. virtual Error visit(const GnuDebugLinkSection &Sec) = 0;
  71. virtual Error visit(const GroupSection &Sec) = 0;
  72. virtual Error visit(const SectionIndexSection &Sec) = 0;
  73. virtual Error visit(const CompressedSection &Sec) = 0;
  74. virtual Error visit(const DecompressedSection &Sec) = 0;
  75. };
  76. class MutableSectionVisitor {
  77. public:
  78. virtual ~MutableSectionVisitor() = default;
  79. virtual Error visit(Section &Sec) = 0;
  80. virtual Error visit(OwnedDataSection &Sec) = 0;
  81. virtual Error visit(StringTableSection &Sec) = 0;
  82. virtual Error visit(SymbolTableSection &Sec) = 0;
  83. virtual Error visit(RelocationSection &Sec) = 0;
  84. virtual Error visit(DynamicRelocationSection &Sec) = 0;
  85. virtual Error visit(GnuDebugLinkSection &Sec) = 0;
  86. virtual Error visit(GroupSection &Sec) = 0;
  87. virtual Error visit(SectionIndexSection &Sec) = 0;
  88. virtual Error visit(CompressedSection &Sec) = 0;
  89. virtual Error visit(DecompressedSection &Sec) = 0;
  90. };
  91. class SectionWriter : public SectionVisitor {
  92. protected:
  93. WritableMemoryBuffer &Out;
  94. public:
  95. virtual ~SectionWriter() = default;
  96. Error visit(const Section &Sec) override;
  97. Error visit(const OwnedDataSection &Sec) override;
  98. Error visit(const StringTableSection &Sec) override;
  99. Error visit(const DynamicRelocationSection &Sec) override;
  100. Error visit(const SymbolTableSection &Sec) override = 0;
  101. Error visit(const RelocationSection &Sec) override = 0;
  102. Error visit(const GnuDebugLinkSection &Sec) override = 0;
  103. Error visit(const GroupSection &Sec) override = 0;
  104. Error visit(const SectionIndexSection &Sec) override = 0;
  105. Error visit(const CompressedSection &Sec) override = 0;
  106. Error visit(const DecompressedSection &Sec) override = 0;
  107. explicit SectionWriter(WritableMemoryBuffer &Buf) : Out(Buf) {}
  108. };
  109. template <class ELFT> class ELFSectionWriter : public SectionWriter {
  110. private:
  111. using Elf_Word = typename ELFT::Word;
  112. using Elf_Rel = typename ELFT::Rel;
  113. using Elf_Rela = typename ELFT::Rela;
  114. using Elf_Sym = typename ELFT::Sym;
  115. public:
  116. virtual ~ELFSectionWriter() {}
  117. Error visit(const SymbolTableSection &Sec) override;
  118. Error visit(const RelocationSection &Sec) override;
  119. Error visit(const GnuDebugLinkSection &Sec) override;
  120. Error visit(const GroupSection &Sec) override;
  121. Error visit(const SectionIndexSection &Sec) override;
  122. Error visit(const CompressedSection &Sec) override;
  123. Error visit(const DecompressedSection &Sec) override;
  124. explicit ELFSectionWriter(WritableMemoryBuffer &Buf) : SectionWriter(Buf) {}
  125. };
  126. template <class ELFT> class ELFSectionSizer : public MutableSectionVisitor {
  127. private:
  128. using Elf_Rel = typename ELFT::Rel;
  129. using Elf_Rela = typename ELFT::Rela;
  130. using Elf_Sym = typename ELFT::Sym;
  131. using Elf_Word = typename ELFT::Word;
  132. using Elf_Xword = typename ELFT::Xword;
  133. public:
  134. Error visit(Section &Sec) override;
  135. Error visit(OwnedDataSection &Sec) override;
  136. Error visit(StringTableSection &Sec) override;
  137. Error visit(DynamicRelocationSection &Sec) override;
  138. Error visit(SymbolTableSection &Sec) override;
  139. Error visit(RelocationSection &Sec) override;
  140. Error visit(GnuDebugLinkSection &Sec) override;
  141. Error visit(GroupSection &Sec) override;
  142. Error visit(SectionIndexSection &Sec) override;
  143. Error visit(CompressedSection &Sec) override;
  144. Error visit(DecompressedSection &Sec) override;
  145. };
  146. #define MAKE_SEC_WRITER_FRIEND \
  147. friend class SectionWriter; \
  148. friend class IHexSectionWriterBase; \
  149. friend class IHexSectionWriter; \
  150. template <class ELFT> friend class ELFSectionWriter; \
  151. template <class ELFT> friend class ELFSectionSizer;
  152. class BinarySectionWriter : public SectionWriter {
  153. public:
  154. virtual ~BinarySectionWriter() {}
  155. Error visit(const SymbolTableSection &Sec) override;
  156. Error visit(const RelocationSection &Sec) override;
  157. Error visit(const GnuDebugLinkSection &Sec) override;
  158. Error visit(const GroupSection &Sec) override;
  159. Error visit(const SectionIndexSection &Sec) override;
  160. Error visit(const CompressedSection &Sec) override;
  161. Error visit(const DecompressedSection &Sec) override;
  162. explicit BinarySectionWriter(WritableMemoryBuffer &Buf)
  163. : SectionWriter(Buf) {}
  164. };
  165. using IHexLineData = SmallVector<char, 64>;
  166. struct IHexRecord {
  167. // Memory address of the record.
  168. uint16_t Addr;
  169. // Record type (see below).
  170. uint16_t Type;
  171. // Record data in hexadecimal form.
  172. StringRef HexData;
  173. // Helper method to get file length of the record
  174. // including newline character
  175. static size_t getLength(size_t DataSize) {
  176. // :LLAAAATT[DD...DD]CC'
  177. return DataSize * 2 + 11;
  178. }
  179. // Gets length of line in a file (getLength + CRLF).
  180. static size_t getLineLength(size_t DataSize) {
  181. return getLength(DataSize) + 2;
  182. }
  183. // Given type, address and data returns line which can
  184. // be written to output file.
  185. static IHexLineData getLine(uint8_t Type, uint16_t Addr,
  186. ArrayRef<uint8_t> Data);
  187. // Parses the line and returns record if possible.
  188. // Line should be trimmed from whitespace characters.
  189. static Expected<IHexRecord> parse(StringRef Line);
  190. // Calculates checksum of stringified record representation
  191. // S must NOT contain leading ':' and trailing whitespace
  192. // characters
  193. static uint8_t getChecksum(StringRef S);
  194. enum Type {
  195. // Contains data and a 16-bit starting address for the data.
  196. // The byte count specifies number of data bytes in the record.
  197. Data = 0,
  198. // Must occur exactly once per file in the last line of the file.
  199. // The data field is empty (thus byte count is 00) and the address
  200. // field is typically 0000.
  201. EndOfFile = 1,
  202. // The data field contains a 16-bit segment base address (thus byte
  203. // count is always 02) compatible with 80x86 real mode addressing.
  204. // The address field (typically 0000) is ignored. The segment address
  205. // from the most recent 02 record is multiplied by 16 and added to each
  206. // subsequent data record address to form the physical starting address
  207. // for the data. This allows addressing up to one megabyte of address
  208. // space.
  209. SegmentAddr = 2,
  210. // or 80x86 processors, specifies the initial content of the CS:IP
  211. // registers. The address field is 0000, the byte count is always 04,
  212. // the first two data bytes are the CS value, the latter two are the
  213. // IP value.
  214. StartAddr80x86 = 3,
  215. // Allows for 32 bit addressing (up to 4GiB). The record's address field
  216. // is ignored (typically 0000) and its byte count is always 02. The two
  217. // data bytes (big endian) specify the upper 16 bits of the 32 bit
  218. // absolute address for all subsequent type 00 records
  219. ExtendedAddr = 4,
  220. // The address field is 0000 (not used) and the byte count is always 04.
  221. // The four data bytes represent a 32-bit address value. In the case of
  222. // 80386 and higher CPUs, this address is loaded into the EIP register.
  223. StartAddr = 5,
  224. // We have no other valid types
  225. InvalidType = 6
  226. };
  227. };
  228. // Base class for IHexSectionWriter. This class implements writing algorithm,
  229. // but doesn't actually write records. It is used for output buffer size
  230. // calculation in IHexWriter::finalize.
  231. class IHexSectionWriterBase : public BinarySectionWriter {
  232. // 20-bit segment address
  233. uint32_t SegmentAddr = 0;
  234. // Extended linear address
  235. uint32_t BaseAddr = 0;
  236. // Write segment address corresponding to 'Addr'
  237. uint64_t writeSegmentAddr(uint64_t Addr);
  238. // Write extended linear (base) address corresponding to 'Addr'
  239. uint64_t writeBaseAddr(uint64_t Addr);
  240. protected:
  241. // Offset in the output buffer
  242. uint64_t Offset = 0;
  243. void writeSection(const SectionBase *Sec, ArrayRef<uint8_t> Data);
  244. virtual void writeData(uint8_t Type, uint16_t Addr, ArrayRef<uint8_t> Data);
  245. public:
  246. explicit IHexSectionWriterBase(WritableMemoryBuffer &Buf)
  247. : BinarySectionWriter(Buf) {}
  248. uint64_t getBufferOffset() const { return Offset; }
  249. Error visit(const Section &Sec) final;
  250. Error visit(const OwnedDataSection &Sec) final;
  251. Error visit(const StringTableSection &Sec) override;
  252. Error visit(const DynamicRelocationSection &Sec) final;
  253. using BinarySectionWriter::visit;
  254. };
  255. // Real IHEX section writer
  256. class IHexSectionWriter : public IHexSectionWriterBase {
  257. public:
  258. IHexSectionWriter(WritableMemoryBuffer &Buf) : IHexSectionWriterBase(Buf) {}
  259. void writeData(uint8_t Type, uint16_t Addr, ArrayRef<uint8_t> Data) override;
  260. Error visit(const StringTableSection &Sec) override;
  261. };
  262. class Writer {
  263. protected:
  264. Object &Obj;
  265. std::unique_ptr<WritableMemoryBuffer> Buf;
  266. raw_ostream &Out;
  267. public:
  268. virtual ~Writer();
  269. virtual Error finalize() = 0;
  270. virtual Error write() = 0;
  271. Writer(Object &O, raw_ostream &Out) : Obj(O), Out(Out) {}
  272. };
  273. template <class ELFT> class ELFWriter : public Writer {
  274. private:
  275. using Elf_Addr = typename ELFT::Addr;
  276. using Elf_Shdr = typename ELFT::Shdr;
  277. using Elf_Phdr = typename ELFT::Phdr;
  278. using Elf_Ehdr = typename ELFT::Ehdr;
  279. void initEhdrSegment();
  280. void writeEhdr();
  281. void writePhdr(const Segment &Seg);
  282. void writeShdr(const SectionBase &Sec);
  283. void writePhdrs();
  284. void writeShdrs();
  285. Error writeSectionData();
  286. void writeSegmentData();
  287. void assignOffsets();
  288. std::unique_ptr<ELFSectionWriter<ELFT>> SecWriter;
  289. size_t totalSize() const;
  290. public:
  291. virtual ~ELFWriter() {}
  292. bool WriteSectionHeaders;
  293. // For --only-keep-debug, select an alternative section/segment layout
  294. // algorithm.
  295. bool OnlyKeepDebug;
  296. Error finalize() override;
  297. Error write() override;
  298. ELFWriter(Object &Obj, raw_ostream &Out, bool WSH, bool OnlyKeepDebug);
  299. };
  300. class BinaryWriter : public Writer {
  301. private:
  302. std::unique_ptr<BinarySectionWriter> SecWriter;
  303. uint64_t TotalSize = 0;
  304. public:
  305. ~BinaryWriter() {}
  306. Error finalize() override;
  307. Error write() override;
  308. BinaryWriter(Object &Obj, raw_ostream &Out) : Writer(Obj, Out) {}
  309. };
  310. class IHexWriter : public Writer {
  311. struct SectionCompare {
  312. bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const;
  313. };
  314. std::set<const SectionBase *, SectionCompare> Sections;
  315. size_t TotalSize = 0;
  316. Error checkSection(const SectionBase &Sec);
  317. uint64_t writeEntryPointRecord(uint8_t *Buf);
  318. uint64_t writeEndOfFileRecord(uint8_t *Buf);
  319. public:
  320. ~IHexWriter() {}
  321. Error finalize() override;
  322. Error write() override;
  323. IHexWriter(Object &Obj, raw_ostream &Out) : Writer(Obj, Out) {}
  324. };
  325. class SectionBase {
  326. public:
  327. std::string Name;
  328. Segment *ParentSegment = nullptr;
  329. uint64_t HeaderOffset = 0;
  330. uint32_t Index = 0;
  331. uint32_t OriginalIndex = 0;
  332. uint64_t OriginalFlags = 0;
  333. uint64_t OriginalType = ELF::SHT_NULL;
  334. uint64_t OriginalOffset = std::numeric_limits<uint64_t>::max();
  335. uint64_t Addr = 0;
  336. uint64_t Align = 1;
  337. uint32_t EntrySize = 0;
  338. uint64_t Flags = 0;
  339. uint64_t Info = 0;
  340. uint64_t Link = ELF::SHN_UNDEF;
  341. uint64_t NameIndex = 0;
  342. uint64_t Offset = 0;
  343. uint64_t Size = 0;
  344. uint64_t Type = ELF::SHT_NULL;
  345. ArrayRef<uint8_t> OriginalData;
  346. bool HasSymbol = false;
  347. SectionBase() = default;
  348. SectionBase(const SectionBase &) = default;
  349. virtual ~SectionBase() = default;
  350. virtual Error initialize(SectionTableRef SecTable);
  351. virtual void finalize();
  352. // Remove references to these sections. The list of sections must be sorted.
  353. virtual Error
  354. removeSectionReferences(bool AllowBrokenLinks,
  355. function_ref<bool(const SectionBase *)> ToRemove);
  356. virtual Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
  357. virtual Error accept(SectionVisitor &Visitor) const = 0;
  358. virtual Error accept(MutableSectionVisitor &Visitor) = 0;
  359. virtual void markSymbols();
  360. virtual void
  361. replaceSectionReferences(const DenseMap<SectionBase *, SectionBase *> &);
  362. virtual bool hasContents() const { return false; }
  363. // Notify the section that it is subject to removal.
  364. virtual void onRemove();
  365. };
  366. class Segment {
  367. private:
  368. struct SectionCompare {
  369. bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const {
  370. // Some sections might have the same address if one of them is empty. To
  371. // fix this we can use the lexicographic ordering on ->Addr and the
  372. // original index.
  373. if (Lhs->OriginalOffset == Rhs->OriginalOffset)
  374. return Lhs->OriginalIndex < Rhs->OriginalIndex;
  375. return Lhs->OriginalOffset < Rhs->OriginalOffset;
  376. }
  377. };
  378. public:
  379. uint32_t Type = 0;
  380. uint32_t Flags = 0;
  381. uint64_t Offset = 0;
  382. uint64_t VAddr = 0;
  383. uint64_t PAddr = 0;
  384. uint64_t FileSize = 0;
  385. uint64_t MemSize = 0;
  386. uint64_t Align = 0;
  387. uint32_t Index = 0;
  388. uint64_t OriginalOffset = 0;
  389. Segment *ParentSegment = nullptr;
  390. ArrayRef<uint8_t> Contents;
  391. std::set<const SectionBase *, SectionCompare> Sections;
  392. explicit Segment(ArrayRef<uint8_t> Data) : Contents(Data) {}
  393. Segment() = default;
  394. const SectionBase *firstSection() const {
  395. if (!Sections.empty())
  396. return *Sections.begin();
  397. return nullptr;
  398. }
  399. void removeSection(const SectionBase *Sec) { Sections.erase(Sec); }
  400. void addSection(const SectionBase *Sec) { Sections.insert(Sec); }
  401. ArrayRef<uint8_t> getContents() const { return Contents; }
  402. };
  403. class Section : public SectionBase {
  404. MAKE_SEC_WRITER_FRIEND
  405. ArrayRef<uint8_t> Contents;
  406. SectionBase *LinkSection = nullptr;
  407. public:
  408. explicit Section(ArrayRef<uint8_t> Data) : Contents(Data) {}
  409. Error accept(SectionVisitor &Visitor) const override;
  410. Error accept(MutableSectionVisitor &Visitor) override;
  411. Error removeSectionReferences(
  412. bool AllowBrokenLinks,
  413. function_ref<bool(const SectionBase *)> ToRemove) override;
  414. Error initialize(SectionTableRef SecTable) override;
  415. void finalize() override;
  416. bool hasContents() const override {
  417. return Type != ELF::SHT_NOBITS && Type != ELF::SHT_NULL;
  418. }
  419. };
  420. class OwnedDataSection : public SectionBase {
  421. MAKE_SEC_WRITER_FRIEND
  422. std::vector<uint8_t> Data;
  423. public:
  424. OwnedDataSection(StringRef SecName, ArrayRef<uint8_t> Data)
  425. : Data(std::begin(Data), std::end(Data)) {
  426. Name = SecName.str();
  427. Type = OriginalType = ELF::SHT_PROGBITS;
  428. Size = Data.size();
  429. OriginalOffset = std::numeric_limits<uint64_t>::max();
  430. }
  431. OwnedDataSection(const Twine &SecName, uint64_t SecAddr, uint64_t SecFlags,
  432. uint64_t SecOff) {
  433. Name = SecName.str();
  434. Type = OriginalType = ELF::SHT_PROGBITS;
  435. Addr = SecAddr;
  436. Flags = OriginalFlags = SecFlags;
  437. OriginalOffset = SecOff;
  438. }
  439. OwnedDataSection(SectionBase &S, ArrayRef<uint8_t> Data)
  440. : SectionBase(S), Data(std::begin(Data), std::end(Data)) {
  441. Size = Data.size();
  442. }
  443. void appendHexData(StringRef HexData);
  444. Error accept(SectionVisitor &Sec) const override;
  445. Error accept(MutableSectionVisitor &Visitor) override;
  446. bool hasContents() const override { return true; }
  447. };
  448. class CompressedSection : public SectionBase {
  449. MAKE_SEC_WRITER_FRIEND
  450. uint32_t ChType = 0;
  451. DebugCompressionType CompressionType;
  452. uint64_t DecompressedSize;
  453. uint64_t DecompressedAlign;
  454. SmallVector<uint8_t, 128> CompressedData;
  455. public:
  456. CompressedSection(const SectionBase &Sec,
  457. DebugCompressionType CompressionType, bool Is64Bits);
  458. CompressedSection(ArrayRef<uint8_t> CompressedData, uint32_t ChType,
  459. uint64_t DecompressedSize, uint64_t DecompressedAlign);
  460. uint64_t getDecompressedSize() const { return DecompressedSize; }
  461. uint64_t getDecompressedAlign() const { return DecompressedAlign; }
  462. uint64_t getChType() const { return ChType; }
  463. Error accept(SectionVisitor &Visitor) const override;
  464. Error accept(MutableSectionVisitor &Visitor) override;
  465. static bool classof(const SectionBase *S) {
  466. return S->OriginalFlags & ELF::SHF_COMPRESSED;
  467. }
  468. };
  469. class DecompressedSection : public SectionBase {
  470. MAKE_SEC_WRITER_FRIEND
  471. public:
  472. uint32_t ChType;
  473. explicit DecompressedSection(const CompressedSection &Sec)
  474. : SectionBase(Sec), ChType(Sec.getChType()) {
  475. Size = Sec.getDecompressedSize();
  476. Align = Sec.getDecompressedAlign();
  477. Flags = OriginalFlags = (Flags & ~ELF::SHF_COMPRESSED);
  478. }
  479. Error accept(SectionVisitor &Visitor) const override;
  480. Error accept(MutableSectionVisitor &Visitor) override;
  481. };
  482. // There are two types of string tables that can exist, dynamic and not dynamic.
  483. // In the dynamic case the string table is allocated. Changing a dynamic string
  484. // table would mean altering virtual addresses and thus the memory image. So
  485. // dynamic string tables should not have an interface to modify them or
  486. // reconstruct them. This type lets us reconstruct a string table. To avoid
  487. // this class being used for dynamic string tables (which has happened) the
  488. // classof method checks that the particular instance is not allocated. This
  489. // then agrees with the makeSection method used to construct most sections.
  490. class StringTableSection : public SectionBase {
  491. MAKE_SEC_WRITER_FRIEND
  492. StringTableBuilder StrTabBuilder;
  493. public:
  494. StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) {
  495. Type = OriginalType = ELF::SHT_STRTAB;
  496. }
  497. void addString(StringRef Name);
  498. uint32_t findIndex(StringRef Name) const;
  499. void prepareForLayout();
  500. Error accept(SectionVisitor &Visitor) const override;
  501. Error accept(MutableSectionVisitor &Visitor) override;
  502. static bool classof(const SectionBase *S) {
  503. if (S->OriginalFlags & ELF::SHF_ALLOC)
  504. return false;
  505. return S->OriginalType == ELF::SHT_STRTAB;
  506. }
  507. };
  508. // Symbols have a st_shndx field that normally stores an index but occasionally
  509. // stores a different special value. This enum keeps track of what the st_shndx
  510. // field means. Most of the values are just copies of the special SHN_* values.
  511. // SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section.
  512. enum SymbolShndxType {
  513. SYMBOL_SIMPLE_INDEX = 0,
  514. SYMBOL_ABS = ELF::SHN_ABS,
  515. SYMBOL_COMMON = ELF::SHN_COMMON,
  516. SYMBOL_LOPROC = ELF::SHN_LOPROC,
  517. SYMBOL_AMDGPU_LDS = ELF::SHN_AMDGPU_LDS,
  518. SYMBOL_HEXAGON_SCOMMON = ELF::SHN_HEXAGON_SCOMMON,
  519. SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2,
  520. SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4,
  521. SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8,
  522. SYMBOL_MIPS_ACOMMON = ELF::SHN_MIPS_ACOMMON,
  523. SYMBOL_MIPS_TEXT = ELF::SHN_MIPS_TEXT,
  524. SYMBOL_MIPS_DATA = ELF::SHN_MIPS_DATA,
  525. SYMBOL_MIPS_SCOMMON = ELF::SHN_MIPS_SCOMMON,
  526. SYMBOL_MIPS_SUNDEFINED = ELF::SHN_MIPS_SUNDEFINED,
  527. SYMBOL_HIPROC = ELF::SHN_HIPROC,
  528. SYMBOL_LOOS = ELF::SHN_LOOS,
  529. SYMBOL_HIOS = ELF::SHN_HIOS,
  530. SYMBOL_XINDEX = ELF::SHN_XINDEX,
  531. };
  532. struct Symbol {
  533. uint8_t Binding;
  534. SectionBase *DefinedIn = nullptr;
  535. SymbolShndxType ShndxType;
  536. uint32_t Index;
  537. std::string Name;
  538. uint32_t NameIndex;
  539. uint64_t Size;
  540. uint8_t Type;
  541. uint64_t Value;
  542. uint8_t Visibility;
  543. bool Referenced = false;
  544. uint16_t getShndx() const;
  545. bool isCommon() const;
  546. };
  547. class SectionIndexSection : public SectionBase {
  548. MAKE_SEC_WRITER_FRIEND
  549. private:
  550. std::vector<uint32_t> Indexes;
  551. SymbolTableSection *Symbols = nullptr;
  552. public:
  553. virtual ~SectionIndexSection() {}
  554. void addIndex(uint32_t Index) {
  555. assert(Size > 0);
  556. Indexes.push_back(Index);
  557. }
  558. void reserve(size_t NumSymbols) {
  559. Indexes.reserve(NumSymbols);
  560. Size = NumSymbols * 4;
  561. }
  562. void setSymTab(SymbolTableSection *SymTab) { Symbols = SymTab; }
  563. Error initialize(SectionTableRef SecTable) override;
  564. void finalize() override;
  565. Error accept(SectionVisitor &Visitor) const override;
  566. Error accept(MutableSectionVisitor &Visitor) override;
  567. SectionIndexSection() {
  568. Name = ".symtab_shndx";
  569. Align = 4;
  570. EntrySize = 4;
  571. Type = OriginalType = ELF::SHT_SYMTAB_SHNDX;
  572. }
  573. };
  574. class SymbolTableSection : public SectionBase {
  575. MAKE_SEC_WRITER_FRIEND
  576. void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
  577. void assignIndices();
  578. protected:
  579. std::vector<std::unique_ptr<Symbol>> Symbols;
  580. StringTableSection *SymbolNames = nullptr;
  581. SectionIndexSection *SectionIndexTable = nullptr;
  582. using SymPtr = std::unique_ptr<Symbol>;
  583. public:
  584. SymbolTableSection() { Type = OriginalType = ELF::SHT_SYMTAB; }
  585. void addSymbol(Twine Name, uint8_t Bind, uint8_t Type, SectionBase *DefinedIn,
  586. uint64_t Value, uint8_t Visibility, uint16_t Shndx,
  587. uint64_t SymbolSize);
  588. void prepareForLayout();
  589. // An 'empty' symbol table still contains a null symbol.
  590. bool empty() const { return Symbols.size() == 1; }
  591. void setShndxTable(SectionIndexSection *ShndxTable) {
  592. SectionIndexTable = ShndxTable;
  593. }
  594. const SectionIndexSection *getShndxTable() const { return SectionIndexTable; }
  595. void fillShndxTable();
  596. const SectionBase *getStrTab() const { return SymbolNames; }
  597. Expected<const Symbol *> getSymbolByIndex(uint32_t Index) const;
  598. Expected<Symbol *> getSymbolByIndex(uint32_t Index);
  599. void updateSymbols(function_ref<void(Symbol &)> Callable);
  600. Error removeSectionReferences(
  601. bool AllowBrokenLinks,
  602. function_ref<bool(const SectionBase *)> ToRemove) override;
  603. Error initialize(SectionTableRef SecTable) override;
  604. void finalize() override;
  605. Error accept(SectionVisitor &Visitor) const override;
  606. Error accept(MutableSectionVisitor &Visitor) override;
  607. Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
  608. void replaceSectionReferences(
  609. const DenseMap<SectionBase *, SectionBase *> &FromTo) override;
  610. static bool classof(const SectionBase *S) {
  611. return S->OriginalType == ELF::SHT_SYMTAB;
  612. }
  613. };
  614. struct Relocation {
  615. Symbol *RelocSymbol = nullptr;
  616. uint64_t Offset;
  617. uint64_t Addend;
  618. uint32_t Type;
  619. };
  620. // All relocation sections denote relocations to apply to another section.
  621. // However, some relocation sections use a dynamic symbol table and others use
  622. // a regular symbol table. Because the types of the two symbol tables differ in
  623. // our system (because they should behave differently) we can't uniformly
  624. // represent all relocations with the same base class if we expose an interface
  625. // that mentions the symbol table type. So we split the two base types into two
  626. // different classes, one which handles the section the relocation is applied to
  627. // and another which handles the symbol table type. The symbol table type is
  628. // taken as a type parameter to the class (see RelocSectionWithSymtabBase).
  629. class RelocationSectionBase : public SectionBase {
  630. protected:
  631. SectionBase *SecToApplyRel = nullptr;
  632. public:
  633. const SectionBase *getSection() const { return SecToApplyRel; }
  634. void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
  635. StringRef getNamePrefix() const;
  636. static bool classof(const SectionBase *S) {
  637. return S->OriginalType == ELF::SHT_REL || S->OriginalType == ELF::SHT_RELA;
  638. }
  639. };
  640. // Takes the symbol table type to use as a parameter so that we can deduplicate
  641. // that code between the two symbol table types.
  642. template <class SymTabType>
  643. class RelocSectionWithSymtabBase : public RelocationSectionBase {
  644. void setSymTab(SymTabType *SymTab) { Symbols = SymTab; }
  645. protected:
  646. RelocSectionWithSymtabBase() = default;
  647. SymTabType *Symbols = nullptr;
  648. public:
  649. Error initialize(SectionTableRef SecTable) override;
  650. void finalize() override;
  651. };
  652. class RelocationSection
  653. : public RelocSectionWithSymtabBase<SymbolTableSection> {
  654. MAKE_SEC_WRITER_FRIEND
  655. std::vector<Relocation> Relocations;
  656. const Object &Obj;
  657. public:
  658. RelocationSection(const Object &O) : Obj(O) {}
  659. void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
  660. Error accept(SectionVisitor &Visitor) const override;
  661. Error accept(MutableSectionVisitor &Visitor) override;
  662. Error removeSectionReferences(
  663. bool AllowBrokenLinks,
  664. function_ref<bool(const SectionBase *)> ToRemove) override;
  665. Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
  666. void markSymbols() override;
  667. void replaceSectionReferences(
  668. const DenseMap<SectionBase *, SectionBase *> &FromTo) override;
  669. const Object &getObject() const { return Obj; }
  670. static bool classof(const SectionBase *S) {
  671. if (S->OriginalFlags & ELF::SHF_ALLOC)
  672. return false;
  673. return S->OriginalType == ELF::SHT_REL || S->OriginalType == ELF::SHT_RELA;
  674. }
  675. };
  676. // TODO: The way stripping and groups interact is complicated
  677. // and still needs to be worked on.
  678. class GroupSection : public SectionBase {
  679. MAKE_SEC_WRITER_FRIEND
  680. const SymbolTableSection *SymTab = nullptr;
  681. Symbol *Sym = nullptr;
  682. ELF::Elf32_Word FlagWord;
  683. SmallVector<SectionBase *, 3> GroupMembers;
  684. public:
  685. // TODO: Contents is present in several classes of the hierarchy.
  686. // This needs to be refactored to avoid duplication.
  687. ArrayRef<uint8_t> Contents;
  688. explicit GroupSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
  689. void setSymTab(const SymbolTableSection *SymTabSec) { SymTab = SymTabSec; }
  690. void setSymbol(Symbol *S) { Sym = S; }
  691. void setFlagWord(ELF::Elf32_Word W) { FlagWord = W; }
  692. void addMember(SectionBase *Sec) { GroupMembers.push_back(Sec); }
  693. Error accept(SectionVisitor &) const override;
  694. Error accept(MutableSectionVisitor &Visitor) override;
  695. void finalize() override;
  696. Error removeSectionReferences(
  697. bool AllowBrokenLinks,
  698. function_ref<bool(const SectionBase *)> ToRemove) override;
  699. Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
  700. void markSymbols() override;
  701. void replaceSectionReferences(
  702. const DenseMap<SectionBase *, SectionBase *> &FromTo) override;
  703. void onRemove() override;
  704. static bool classof(const SectionBase *S) {
  705. return S->OriginalType == ELF::SHT_GROUP;
  706. }
  707. };
  708. class DynamicSymbolTableSection : public Section {
  709. public:
  710. explicit DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : Section(Data) {}
  711. static bool classof(const SectionBase *S) {
  712. return S->OriginalType == ELF::SHT_DYNSYM;
  713. }
  714. };
  715. class DynamicSection : public Section {
  716. public:
  717. explicit DynamicSection(ArrayRef<uint8_t> Data) : Section(Data) {}
  718. static bool classof(const SectionBase *S) {
  719. return S->OriginalType == ELF::SHT_DYNAMIC;
  720. }
  721. };
  722. class DynamicRelocationSection
  723. : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
  724. MAKE_SEC_WRITER_FRIEND
  725. private:
  726. ArrayRef<uint8_t> Contents;
  727. public:
  728. explicit DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
  729. Error accept(SectionVisitor &) const override;
  730. Error accept(MutableSectionVisitor &Visitor) override;
  731. Error removeSectionReferences(
  732. bool AllowBrokenLinks,
  733. function_ref<bool(const SectionBase *)> ToRemove) override;
  734. static bool classof(const SectionBase *S) {
  735. if (!(S->OriginalFlags & ELF::SHF_ALLOC))
  736. return false;
  737. return S->OriginalType == ELF::SHT_REL || S->OriginalType == ELF::SHT_RELA;
  738. }
  739. };
  740. class GnuDebugLinkSection : public SectionBase {
  741. MAKE_SEC_WRITER_FRIEND
  742. private:
  743. StringRef FileName;
  744. uint32_t CRC32;
  745. void init(StringRef File);
  746. public:
  747. // If we add this section from an external source we can use this ctor.
  748. explicit GnuDebugLinkSection(StringRef File, uint32_t PrecomputedCRC);
  749. Error accept(SectionVisitor &Visitor) const override;
  750. Error accept(MutableSectionVisitor &Visitor) override;
  751. };
  752. class Reader {
  753. public:
  754. virtual ~Reader();
  755. virtual Expected<std::unique_ptr<Object>> create(bool EnsureSymtab) const = 0;
  756. };
  757. using object::Binary;
  758. using object::ELFFile;
  759. using object::ELFObjectFile;
  760. using object::OwningBinary;
  761. class BasicELFBuilder {
  762. protected:
  763. std::unique_ptr<Object> Obj;
  764. void initFileHeader();
  765. void initHeaderSegment();
  766. StringTableSection *addStrTab();
  767. SymbolTableSection *addSymTab(StringTableSection *StrTab);
  768. Error initSections();
  769. public:
  770. BasicELFBuilder() : Obj(std::make_unique<Object>()) {}
  771. };
  772. class BinaryELFBuilder : public BasicELFBuilder {
  773. MemoryBuffer *MemBuf;
  774. uint8_t NewSymbolVisibility;
  775. void addData(SymbolTableSection *SymTab);
  776. public:
  777. BinaryELFBuilder(MemoryBuffer *MB, uint8_t NewSymbolVisibility)
  778. : MemBuf(MB), NewSymbolVisibility(NewSymbolVisibility) {}
  779. Expected<std::unique_ptr<Object>> build();
  780. };
  781. class IHexELFBuilder : public BasicELFBuilder {
  782. const std::vector<IHexRecord> &Records;
  783. void addDataSections();
  784. public:
  785. IHexELFBuilder(const std::vector<IHexRecord> &Records) : Records(Records) {}
  786. Expected<std::unique_ptr<Object>> build();
  787. };
  788. template <class ELFT> class ELFBuilder {
  789. private:
  790. using Elf_Addr = typename ELFT::Addr;
  791. using Elf_Shdr = typename ELFT::Shdr;
  792. using Elf_Word = typename ELFT::Word;
  793. const ELFFile<ELFT> &ElfFile;
  794. Object &Obj;
  795. size_t EhdrOffset = 0;
  796. std::optional<StringRef> ExtractPartition;
  797. void setParentSegment(Segment &Child);
  798. Error readProgramHeaders(const ELFFile<ELFT> &HeadersFile);
  799. Error initGroupSection(GroupSection *GroupSec);
  800. Error initSymbolTable(SymbolTableSection *SymTab);
  801. Error readSectionHeaders();
  802. Error readSections(bool EnsureSymtab);
  803. Error findEhdrOffset();
  804. Expected<SectionBase &> makeSection(const Elf_Shdr &Shdr);
  805. public:
  806. ELFBuilder(const ELFObjectFile<ELFT> &ElfObj, Object &Obj,
  807. std::optional<StringRef> ExtractPartition);
  808. Error build(bool EnsureSymtab);
  809. };
  810. class BinaryReader : public Reader {
  811. MemoryBuffer *MemBuf;
  812. uint8_t NewSymbolVisibility;
  813. public:
  814. BinaryReader(MemoryBuffer *MB, const uint8_t NewSymbolVisibility)
  815. : MemBuf(MB), NewSymbolVisibility(NewSymbolVisibility) {}
  816. Expected<std::unique_ptr<Object>> create(bool EnsureSymtab) const override;
  817. };
  818. class IHexReader : public Reader {
  819. MemoryBuffer *MemBuf;
  820. Expected<std::vector<IHexRecord>> parse() const;
  821. Error parseError(size_t LineNo, Error E) const {
  822. return LineNo == -1U
  823. ? createFileError(MemBuf->getBufferIdentifier(), std::move(E))
  824. : createFileError(MemBuf->getBufferIdentifier(), LineNo,
  825. std::move(E));
  826. }
  827. template <typename... Ts>
  828. Error parseError(size_t LineNo, char const *Fmt, const Ts &...Vals) const {
  829. Error E = createStringError(errc::invalid_argument, Fmt, Vals...);
  830. return parseError(LineNo, std::move(E));
  831. }
  832. public:
  833. IHexReader(MemoryBuffer *MB) : MemBuf(MB) {}
  834. Expected<std::unique_ptr<Object>> create(bool EnsureSymtab) const override;
  835. };
  836. class ELFReader : public Reader {
  837. Binary *Bin;
  838. std::optional<StringRef> ExtractPartition;
  839. public:
  840. Expected<std::unique_ptr<Object>> create(bool EnsureSymtab) const override;
  841. explicit ELFReader(Binary *B, std::optional<StringRef> ExtractPartition)
  842. : Bin(B), ExtractPartition(ExtractPartition) {}
  843. };
  844. class Object {
  845. private:
  846. using SecPtr = std::unique_ptr<SectionBase>;
  847. using SegPtr = std::unique_ptr<Segment>;
  848. std::vector<SecPtr> Sections;
  849. std::vector<SegPtr> Segments;
  850. std::vector<SecPtr> RemovedSections;
  851. DenseMap<SectionBase *, std::vector<uint8_t>> UpdatedSections;
  852. static bool sectionIsAlloc(const SectionBase &Sec) {
  853. return Sec.Flags & ELF::SHF_ALLOC;
  854. };
  855. public:
  856. template <class T>
  857. using ConstRange = iterator_range<pointee_iterator<
  858. typename std::vector<std::unique_ptr<T>>::const_iterator>>;
  859. // It is often the case that the ELF header and the program header table are
  860. // not present in any segment. This could be a problem during file layout,
  861. // because other segments may get assigned an offset where either of the
  862. // two should reside, which will effectively corrupt the resulting binary.
  863. // Other than that we use these segments to track program header offsets
  864. // when they may not follow the ELF header.
  865. Segment ElfHdrSegment;
  866. Segment ProgramHdrSegment;
  867. bool Is64Bits;
  868. uint8_t OSABI;
  869. uint8_t ABIVersion;
  870. uint64_t Entry;
  871. uint64_t SHOff;
  872. uint32_t Type;
  873. uint32_t Machine;
  874. uint32_t Version;
  875. uint32_t Flags;
  876. bool HadShdrs = true;
  877. bool MustBeRelocatable = false;
  878. StringTableSection *SectionNames = nullptr;
  879. SymbolTableSection *SymbolTable = nullptr;
  880. SectionIndexSection *SectionIndexTable = nullptr;
  881. bool IsMips64EL = false;
  882. SectionTableRef sections() const { return SectionTableRef(Sections); }
  883. iterator_range<
  884. filter_iterator<pointee_iterator<std::vector<SecPtr>::const_iterator>,
  885. decltype(&sectionIsAlloc)>>
  886. allocSections() const {
  887. return make_filter_range(make_pointee_range(Sections), sectionIsAlloc);
  888. }
  889. const auto &getUpdatedSections() const { return UpdatedSections; }
  890. Error updateSection(StringRef Name, ArrayRef<uint8_t> Data);
  891. SectionBase *findSection(StringRef Name) {
  892. auto SecIt =
  893. find_if(Sections, [&](const SecPtr &Sec) { return Sec->Name == Name; });
  894. return SecIt == Sections.end() ? nullptr : SecIt->get();
  895. }
  896. SectionTableRef removedSections() { return SectionTableRef(RemovedSections); }
  897. ConstRange<Segment> segments() const { return make_pointee_range(Segments); }
  898. Error removeSections(bool AllowBrokenLinks,
  899. std::function<bool(const SectionBase &)> ToRemove);
  900. Error replaceSections(const DenseMap<SectionBase *, SectionBase *> &FromTo);
  901. Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
  902. template <class T, class... Ts> T &addSection(Ts &&...Args) {
  903. auto Sec = std::make_unique<T>(std::forward<Ts>(Args)...);
  904. auto Ptr = Sec.get();
  905. MustBeRelocatable |= isa<RelocationSection>(*Ptr);
  906. Sections.emplace_back(std::move(Sec));
  907. Ptr->Index = Sections.size();
  908. return *Ptr;
  909. }
  910. Error addNewSymbolTable();
  911. Segment &addSegment(ArrayRef<uint8_t> Data) {
  912. Segments.emplace_back(std::make_unique<Segment>(Data));
  913. return *Segments.back();
  914. }
  915. bool isRelocatable() const {
  916. return (Type != ELF::ET_DYN && Type != ELF::ET_EXEC) || MustBeRelocatable;
  917. }
  918. };
  919. } // end namespace elf
  920. } // end namespace objcopy
  921. } // end namespace llvm
  922. #endif // LLVM_LIB_OBJCOPY_ELF_ELFOBJECT_H