Object.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  1. //===- Object.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_TOOLS_OBJCOPY_OBJECT_H
  9. #define LLVM_TOOLS_OBJCOPY_OBJECT_H
  10. #include "CommonConfig.h"
  11. #include "llvm/ADT/ArrayRef.h"
  12. #include "llvm/ADT/StringRef.h"
  13. #include "llvm/ADT/Twine.h"
  14. #include "llvm/BinaryFormat/ELF.h"
  15. #include "llvm/MC/StringTableBuilder.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. virtual Error visit(const SymbolTableSection &Sec) override = 0;
  101. virtual Error visit(const RelocationSection &Sec) override = 0;
  102. virtual Error visit(const GnuDebugLinkSection &Sec) override = 0;
  103. virtual Error visit(const GroupSection &Sec) override = 0;
  104. virtual Error visit(const SectionIndexSection &Sec) override = 0;
  105. virtual Error visit(const CompressedSection &Sec) override = 0;
  106. virtual 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. DebugCompressionType CompressionType;
  451. uint64_t DecompressedSize;
  452. uint64_t DecompressedAlign;
  453. SmallVector<char, 128> CompressedData;
  454. public:
  455. static Expected<CompressedSection>
  456. create(const SectionBase &Sec, DebugCompressionType CompressionType);
  457. static Expected<CompressedSection> create(ArrayRef<uint8_t> CompressedData,
  458. uint64_t DecompressedSize,
  459. uint64_t DecompressedAlign);
  460. uint64_t getDecompressedSize() const { return DecompressedSize; }
  461. uint64_t getDecompressedAlign() const { return DecompressedAlign; }
  462. Error accept(SectionVisitor &Visitor) const override;
  463. Error accept(MutableSectionVisitor &Visitor) override;
  464. static bool classof(const SectionBase *S) {
  465. return (S->OriginalFlags & ELF::SHF_COMPRESSED) ||
  466. (StringRef(S->Name).startswith(".zdebug"));
  467. }
  468. private:
  469. CompressedSection(const SectionBase &Sec,
  470. DebugCompressionType CompressionType, Error &Err);
  471. CompressedSection(ArrayRef<uint8_t> CompressedData, uint64_t DecompressedSize,
  472. uint64_t DecompressedAlign);
  473. };
  474. class DecompressedSection : public SectionBase {
  475. MAKE_SEC_WRITER_FRIEND
  476. public:
  477. explicit DecompressedSection(const CompressedSection &Sec)
  478. : SectionBase(Sec) {
  479. Size = Sec.getDecompressedSize();
  480. Align = Sec.getDecompressedAlign();
  481. Flags = OriginalFlags = (Flags & ~ELF::SHF_COMPRESSED);
  482. if (StringRef(Name).startswith(".zdebug"))
  483. Name = "." + Name.substr(2);
  484. }
  485. Error accept(SectionVisitor &Visitor) const override;
  486. Error accept(MutableSectionVisitor &Visitor) override;
  487. };
  488. // There are two types of string tables that can exist, dynamic and not dynamic.
  489. // In the dynamic case the string table is allocated. Changing a dynamic string
  490. // table would mean altering virtual addresses and thus the memory image. So
  491. // dynamic string tables should not have an interface to modify them or
  492. // reconstruct them. This type lets us reconstruct a string table. To avoid
  493. // this class being used for dynamic string tables (which has happened) the
  494. // classof method checks that the particular instance is not allocated. This
  495. // then agrees with the makeSection method used to construct most sections.
  496. class StringTableSection : public SectionBase {
  497. MAKE_SEC_WRITER_FRIEND
  498. StringTableBuilder StrTabBuilder;
  499. public:
  500. StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) {
  501. Type = OriginalType = ELF::SHT_STRTAB;
  502. }
  503. void addString(StringRef Name);
  504. uint32_t findIndex(StringRef Name) const;
  505. void prepareForLayout();
  506. Error accept(SectionVisitor &Visitor) const override;
  507. Error accept(MutableSectionVisitor &Visitor) override;
  508. static bool classof(const SectionBase *S) {
  509. if (S->OriginalFlags & ELF::SHF_ALLOC)
  510. return false;
  511. return S->OriginalType == ELF::SHT_STRTAB;
  512. }
  513. };
  514. // Symbols have a st_shndx field that normally stores an index but occasionally
  515. // stores a different special value. This enum keeps track of what the st_shndx
  516. // field means. Most of the values are just copies of the special SHN_* values.
  517. // SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section.
  518. enum SymbolShndxType {
  519. SYMBOL_SIMPLE_INDEX = 0,
  520. SYMBOL_ABS = ELF::SHN_ABS,
  521. SYMBOL_COMMON = ELF::SHN_COMMON,
  522. SYMBOL_LOPROC = ELF::SHN_LOPROC,
  523. SYMBOL_AMDGPU_LDS = ELF::SHN_AMDGPU_LDS,
  524. SYMBOL_HEXAGON_SCOMMON = ELF::SHN_HEXAGON_SCOMMON,
  525. SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2,
  526. SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4,
  527. SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8,
  528. SYMBOL_HIPROC = ELF::SHN_HIPROC,
  529. SYMBOL_LOOS = ELF::SHN_LOOS,
  530. SYMBOL_HIOS = ELF::SHN_HIOS,
  531. SYMBOL_XINDEX = ELF::SHN_XINDEX,
  532. };
  533. struct Symbol {
  534. uint8_t Binding;
  535. SectionBase *DefinedIn = nullptr;
  536. SymbolShndxType ShndxType;
  537. uint32_t Index;
  538. std::string Name;
  539. uint32_t NameIndex;
  540. uint64_t Size;
  541. uint8_t Type;
  542. uint64_t Value;
  543. uint8_t Visibility;
  544. bool Referenced = false;
  545. uint16_t getShndx() const;
  546. bool isCommon() const;
  547. };
  548. class SectionIndexSection : public SectionBase {
  549. MAKE_SEC_WRITER_FRIEND
  550. private:
  551. std::vector<uint32_t> Indexes;
  552. SymbolTableSection *Symbols = nullptr;
  553. public:
  554. virtual ~SectionIndexSection() {}
  555. void addIndex(uint32_t Index) {
  556. assert(Size > 0);
  557. Indexes.push_back(Index);
  558. }
  559. void reserve(size_t NumSymbols) {
  560. Indexes.reserve(NumSymbols);
  561. Size = NumSymbols * 4;
  562. }
  563. void setSymTab(SymbolTableSection *SymTab) { Symbols = SymTab; }
  564. Error initialize(SectionTableRef SecTable) override;
  565. void finalize() override;
  566. Error accept(SectionVisitor &Visitor) const override;
  567. Error accept(MutableSectionVisitor &Visitor) override;
  568. SectionIndexSection() {
  569. Name = ".symtab_shndx";
  570. Align = 4;
  571. EntrySize = 4;
  572. Type = OriginalType = ELF::SHT_SYMTAB_SHNDX;
  573. }
  574. };
  575. class SymbolTableSection : public SectionBase {
  576. MAKE_SEC_WRITER_FRIEND
  577. void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
  578. void assignIndices();
  579. protected:
  580. std::vector<std::unique_ptr<Symbol>> Symbols;
  581. StringTableSection *SymbolNames = nullptr;
  582. SectionIndexSection *SectionIndexTable = nullptr;
  583. using SymPtr = std::unique_ptr<Symbol>;
  584. public:
  585. SymbolTableSection() { Type = OriginalType = ELF::SHT_SYMTAB; }
  586. void addSymbol(Twine Name, uint8_t Bind, uint8_t Type, SectionBase *DefinedIn,
  587. uint64_t Value, uint8_t Visibility, uint16_t Shndx,
  588. uint64_t SymbolSize);
  589. void prepareForLayout();
  590. // An 'empty' symbol table still contains a null symbol.
  591. bool empty() const { return Symbols.size() == 1; }
  592. void setShndxTable(SectionIndexSection *ShndxTable) {
  593. SectionIndexTable = ShndxTable;
  594. }
  595. const SectionIndexSection *getShndxTable() const { return SectionIndexTable; }
  596. void fillShndxTable();
  597. const SectionBase *getStrTab() const { return SymbolNames; }
  598. Expected<const Symbol *> getSymbolByIndex(uint32_t Index) const;
  599. Expected<Symbol *> getSymbolByIndex(uint32_t Index);
  600. void updateSymbols(function_ref<void(Symbol &)> Callable);
  601. Error removeSectionReferences(
  602. bool AllowBrokenLinks,
  603. function_ref<bool(const SectionBase *)> ToRemove) override;
  604. Error initialize(SectionTableRef SecTable) override;
  605. void finalize() override;
  606. Error accept(SectionVisitor &Visitor) const override;
  607. Error accept(MutableSectionVisitor &Visitor) override;
  608. Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
  609. void replaceSectionReferences(
  610. const DenseMap<SectionBase *, SectionBase *> &FromTo) override;
  611. static bool classof(const SectionBase *S) {
  612. return S->OriginalType == ELF::SHT_SYMTAB;
  613. }
  614. };
  615. struct Relocation {
  616. Symbol *RelocSymbol = nullptr;
  617. uint64_t Offset;
  618. uint64_t Addend;
  619. uint32_t Type;
  620. };
  621. // All relocation sections denote relocations to apply to another section.
  622. // However, some relocation sections use a dynamic symbol table and others use
  623. // a regular symbol table. Because the types of the two symbol tables differ in
  624. // our system (because they should behave differently) we can't uniformly
  625. // represent all relocations with the same base class if we expose an interface
  626. // that mentions the symbol table type. So we split the two base types into two
  627. // different classes, one which handles the section the relocation is applied to
  628. // and another which handles the symbol table type. The symbol table type is
  629. // taken as a type parameter to the class (see RelocSectionWithSymtabBase).
  630. class RelocationSectionBase : public SectionBase {
  631. protected:
  632. SectionBase *SecToApplyRel = nullptr;
  633. public:
  634. const SectionBase *getSection() const { return SecToApplyRel; }
  635. void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
  636. StringRef getNamePrefix() const;
  637. static bool classof(const SectionBase *S) {
  638. return S->OriginalType == ELF::SHT_REL || S->OriginalType == ELF::SHT_RELA;
  639. }
  640. };
  641. // Takes the symbol table type to use as a parameter so that we can deduplicate
  642. // that code between the two symbol table types.
  643. template <class SymTabType>
  644. class RelocSectionWithSymtabBase : public RelocationSectionBase {
  645. void setSymTab(SymTabType *SymTab) { Symbols = SymTab; }
  646. protected:
  647. RelocSectionWithSymtabBase() = default;
  648. SymTabType *Symbols = nullptr;
  649. public:
  650. Error initialize(SectionTableRef SecTable) override;
  651. void finalize() override;
  652. };
  653. class RelocationSection
  654. : public RelocSectionWithSymtabBase<SymbolTableSection> {
  655. MAKE_SEC_WRITER_FRIEND
  656. std::vector<Relocation> Relocations;
  657. const Object &Obj;
  658. public:
  659. RelocationSection(const Object &O) : Obj(O) {}
  660. void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
  661. Error accept(SectionVisitor &Visitor) const override;
  662. Error accept(MutableSectionVisitor &Visitor) override;
  663. Error removeSectionReferences(
  664. bool AllowBrokenLinks,
  665. function_ref<bool(const SectionBase *)> ToRemove) override;
  666. Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
  667. void markSymbols() override;
  668. void replaceSectionReferences(
  669. const DenseMap<SectionBase *, SectionBase *> &FromTo) override;
  670. const Object &getObject() const { return Obj; }
  671. static bool classof(const SectionBase *S) {
  672. if (S->OriginalFlags & ELF::SHF_ALLOC)
  673. return false;
  674. return S->OriginalType == ELF::SHT_REL || S->OriginalType == ELF::SHT_RELA;
  675. }
  676. };
  677. // TODO: The way stripping and groups interact is complicated
  678. // and still needs to be worked on.
  679. class GroupSection : public SectionBase {
  680. MAKE_SEC_WRITER_FRIEND
  681. const SymbolTableSection *SymTab = nullptr;
  682. Symbol *Sym = nullptr;
  683. ELF::Elf32_Word FlagWord;
  684. SmallVector<SectionBase *, 3> GroupMembers;
  685. public:
  686. // TODO: Contents is present in several classes of the hierarchy.
  687. // This needs to be refactored to avoid duplication.
  688. ArrayRef<uint8_t> Contents;
  689. explicit GroupSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
  690. void setSymTab(const SymbolTableSection *SymTabSec) { SymTab = SymTabSec; }
  691. void setSymbol(Symbol *S) { Sym = S; }
  692. void setFlagWord(ELF::Elf32_Word W) { FlagWord = W; }
  693. void addMember(SectionBase *Sec) { GroupMembers.push_back(Sec); }
  694. Error accept(SectionVisitor &) const override;
  695. Error accept(MutableSectionVisitor &Visitor) override;
  696. void finalize() override;
  697. Error removeSectionReferences(
  698. bool AllowBrokenLinks,
  699. function_ref<bool(const SectionBase *)> ToRemove) override;
  700. Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
  701. void markSymbols() override;
  702. void replaceSectionReferences(
  703. const DenseMap<SectionBase *, SectionBase *> &FromTo) override;
  704. void onRemove() override;
  705. static bool classof(const SectionBase *S) {
  706. return S->OriginalType == ELF::SHT_GROUP;
  707. }
  708. };
  709. class DynamicSymbolTableSection : public Section {
  710. public:
  711. explicit DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : Section(Data) {}
  712. static bool classof(const SectionBase *S) {
  713. return S->OriginalType == ELF::SHT_DYNSYM;
  714. }
  715. };
  716. class DynamicSection : public Section {
  717. public:
  718. explicit DynamicSection(ArrayRef<uint8_t> Data) : Section(Data) {}
  719. static bool classof(const SectionBase *S) {
  720. return S->OriginalType == ELF::SHT_DYNAMIC;
  721. }
  722. };
  723. class DynamicRelocationSection
  724. : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
  725. MAKE_SEC_WRITER_FRIEND
  726. private:
  727. ArrayRef<uint8_t> Contents;
  728. public:
  729. explicit DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
  730. Error accept(SectionVisitor &) const override;
  731. Error accept(MutableSectionVisitor &Visitor) override;
  732. Error removeSectionReferences(
  733. bool AllowBrokenLinks,
  734. function_ref<bool(const SectionBase *)> ToRemove) override;
  735. static bool classof(const SectionBase *S) {
  736. if (!(S->OriginalFlags & ELF::SHF_ALLOC))
  737. return false;
  738. return S->OriginalType == ELF::SHT_REL || S->OriginalType == ELF::SHT_RELA;
  739. }
  740. };
  741. class GnuDebugLinkSection : public SectionBase {
  742. MAKE_SEC_WRITER_FRIEND
  743. private:
  744. StringRef FileName;
  745. uint32_t CRC32;
  746. void init(StringRef File);
  747. public:
  748. // If we add this section from an external source we can use this ctor.
  749. explicit GnuDebugLinkSection(StringRef File, uint32_t PrecomputedCRC);
  750. Error accept(SectionVisitor &Visitor) const override;
  751. Error accept(MutableSectionVisitor &Visitor) override;
  752. };
  753. class Reader {
  754. public:
  755. virtual ~Reader();
  756. virtual Expected<std::unique_ptr<Object>> create(bool EnsureSymtab) const = 0;
  757. };
  758. using object::Binary;
  759. using object::ELFFile;
  760. using object::ELFObjectFile;
  761. using object::OwningBinary;
  762. class BasicELFBuilder {
  763. protected:
  764. std::unique_ptr<Object> Obj;
  765. void initFileHeader();
  766. void initHeaderSegment();
  767. StringTableSection *addStrTab();
  768. SymbolTableSection *addSymTab(StringTableSection *StrTab);
  769. Error initSections();
  770. public:
  771. BasicELFBuilder() : Obj(std::make_unique<Object>()) {}
  772. };
  773. class BinaryELFBuilder : public BasicELFBuilder {
  774. MemoryBuffer *MemBuf;
  775. uint8_t NewSymbolVisibility;
  776. void addData(SymbolTableSection *SymTab);
  777. public:
  778. BinaryELFBuilder(MemoryBuffer *MB, uint8_t NewSymbolVisibility)
  779. : MemBuf(MB), NewSymbolVisibility(NewSymbolVisibility) {}
  780. Expected<std::unique_ptr<Object>> build();
  781. };
  782. class IHexELFBuilder : public BasicELFBuilder {
  783. const std::vector<IHexRecord> &Records;
  784. void addDataSections();
  785. public:
  786. IHexELFBuilder(const std::vector<IHexRecord> &Records) : Records(Records) {}
  787. Expected<std::unique_ptr<Object>> build();
  788. };
  789. template <class ELFT> class ELFBuilder {
  790. private:
  791. using Elf_Addr = typename ELFT::Addr;
  792. using Elf_Shdr = typename ELFT::Shdr;
  793. using Elf_Word = typename ELFT::Word;
  794. const ELFFile<ELFT> &ElfFile;
  795. Object &Obj;
  796. size_t EhdrOffset = 0;
  797. Optional<StringRef> ExtractPartition;
  798. void setParentSegment(Segment &Child);
  799. Error readProgramHeaders(const ELFFile<ELFT> &HeadersFile);
  800. Error initGroupSection(GroupSection *GroupSec);
  801. Error initSymbolTable(SymbolTableSection *SymTab);
  802. Error readSectionHeaders();
  803. Error readSections(bool EnsureSymtab);
  804. Error findEhdrOffset();
  805. Expected<SectionBase &> makeSection(const Elf_Shdr &Shdr);
  806. public:
  807. ELFBuilder(const ELFObjectFile<ELFT> &ElfObj, Object &Obj,
  808. Optional<StringRef> ExtractPartition);
  809. Error build(bool EnsureSymtab);
  810. };
  811. class BinaryReader : public Reader {
  812. MemoryBuffer *MemBuf;
  813. uint8_t NewSymbolVisibility;
  814. public:
  815. BinaryReader(MemoryBuffer *MB, const uint8_t NewSymbolVisibility)
  816. : MemBuf(MB), NewSymbolVisibility(NewSymbolVisibility) {}
  817. Expected<std::unique_ptr<Object>> create(bool EnsureSymtab) const override;
  818. };
  819. class IHexReader : public Reader {
  820. MemoryBuffer *MemBuf;
  821. Expected<std::vector<IHexRecord>> parse() const;
  822. Error parseError(size_t LineNo, Error E) const {
  823. return LineNo == -1U
  824. ? createFileError(MemBuf->getBufferIdentifier(), std::move(E))
  825. : createFileError(MemBuf->getBufferIdentifier(), LineNo,
  826. std::move(E));
  827. }
  828. template <typename... Ts>
  829. Error parseError(size_t LineNo, char const *Fmt, const Ts &... Vals) const {
  830. Error E = createStringError(errc::invalid_argument, Fmt, Vals...);
  831. return parseError(LineNo, std::move(E));
  832. }
  833. public:
  834. IHexReader(MemoryBuffer *MB) : MemBuf(MB) {}
  835. Expected<std::unique_ptr<Object>> create(bool EnsureSymtab) const override;
  836. };
  837. class ELFReader : public Reader {
  838. Binary *Bin;
  839. Optional<StringRef> ExtractPartition;
  840. public:
  841. Expected<std::unique_ptr<Object>> create(bool EnsureSymtab) const override;
  842. explicit ELFReader(Binary *B, Optional<StringRef> ExtractPartition)
  843. : Bin(B), ExtractPartition(ExtractPartition) {}
  844. };
  845. class Object {
  846. private:
  847. using SecPtr = std::unique_ptr<SectionBase>;
  848. using SegPtr = std::unique_ptr<Segment>;
  849. std::vector<SecPtr> Sections;
  850. std::vector<SegPtr> Segments;
  851. std::vector<SecPtr> RemovedSections;
  852. DenseMap<SectionBase *, std::vector<uint8_t>> UpdatedSections;
  853. static bool sectionIsAlloc(const SectionBase &Sec) {
  854. return Sec.Flags & ELF::SHF_ALLOC;
  855. };
  856. public:
  857. template <class T>
  858. using ConstRange = iterator_range<pointee_iterator<
  859. typename std::vector<std::unique_ptr<T>>::const_iterator>>;
  860. // It is often the case that the ELF header and the program header table are
  861. // not present in any segment. This could be a problem during file layout,
  862. // because other segments may get assigned an offset where either of the
  863. // two should reside, which will effectively corrupt the resulting binary.
  864. // Other than that we use these segments to track program header offsets
  865. // when they may not follow the ELF header.
  866. Segment ElfHdrSegment;
  867. Segment ProgramHdrSegment;
  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_TOOLS_OBJCOPY_OBJECT_H