Object.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. //===- Object.cpp ---------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "Object.h"
  10. #include "llvm-objcopy.h"
  11. #include "llvm/ADT/ArrayRef.h"
  12. #include "llvm/ADT/STLExtras.h"
  13. #include "llvm/ADT/StringRef.h"
  14. #include "llvm/ADT/Twine.h"
  15. #include "llvm/ADT/iterator_range.h"
  16. #include "llvm/BinaryFormat/ELF.h"
  17. #include "llvm/Object/ELFObjectFile.h"
  18. #include "llvm/Support/ErrorHandling.h"
  19. #include "llvm/Support/FileOutputBuffer.h"
  20. #include <algorithm>
  21. #include <cstddef>
  22. #include <cstdint>
  23. #include <iterator>
  24. #include <utility>
  25. #include <vector>
  26. using namespace llvm;
  27. using namespace object;
  28. using namespace ELF;
  29. template <class ELFT> void Segment::writeHeader(FileOutputBuffer &Out) const {
  30. using Elf_Ehdr = typename ELFT::Ehdr;
  31. using Elf_Phdr = typename ELFT::Phdr;
  32. uint8_t *Buf = Out.getBufferStart();
  33. Buf += sizeof(Elf_Ehdr) + Index * sizeof(Elf_Phdr);
  34. Elf_Phdr &Phdr = *reinterpret_cast<Elf_Phdr *>(Buf);
  35. Phdr.p_type = Type;
  36. Phdr.p_flags = Flags;
  37. Phdr.p_offset = Offset;
  38. Phdr.p_vaddr = VAddr;
  39. Phdr.p_paddr = PAddr;
  40. Phdr.p_filesz = FileSize;
  41. Phdr.p_memsz = MemSize;
  42. Phdr.p_align = Align;
  43. }
  44. void Segment::writeSegment(FileOutputBuffer &Out) const {
  45. uint8_t *Buf = Out.getBufferStart() + Offset;
  46. // We want to maintain segments' interstitial data and contents exactly.
  47. // This lets us just copy segments directly.
  48. std::copy(std::begin(Contents), std::end(Contents), Buf);
  49. }
  50. void SectionBase::removeSectionReferences(const SectionBase *Sec) {}
  51. void SectionBase::initialize(SectionTableRef SecTable) {}
  52. void SectionBase::finalize() {}
  53. template <class ELFT>
  54. void SectionBase::writeHeader(FileOutputBuffer &Out) const {
  55. uint8_t *Buf = Out.getBufferStart();
  56. Buf += HeaderOffset;
  57. typename ELFT::Shdr &Shdr = *reinterpret_cast<typename ELFT::Shdr *>(Buf);
  58. Shdr.sh_name = NameIndex;
  59. Shdr.sh_type = Type;
  60. Shdr.sh_flags = Flags;
  61. Shdr.sh_addr = Addr;
  62. Shdr.sh_offset = Offset;
  63. Shdr.sh_size = Size;
  64. Shdr.sh_link = Link;
  65. Shdr.sh_info = Info;
  66. Shdr.sh_addralign = Align;
  67. Shdr.sh_entsize = EntrySize;
  68. }
  69. void Section::writeSection(FileOutputBuffer &Out) const {
  70. if (Type == SHT_NOBITS)
  71. return;
  72. uint8_t *Buf = Out.getBufferStart() + Offset;
  73. std::copy(std::begin(Contents), std::end(Contents), Buf);
  74. }
  75. void OwnedDataSection::writeSection(FileOutputBuffer &Out) const {
  76. uint8_t *Buf = Out.getBufferStart() + Offset;
  77. std::copy(std::begin(Data), std::end(Data), Buf);
  78. }
  79. void StringTableSection::addString(StringRef Name) {
  80. StrTabBuilder.add(Name);
  81. Size = StrTabBuilder.getSize();
  82. }
  83. uint32_t StringTableSection::findIndex(StringRef Name) const {
  84. return StrTabBuilder.getOffset(Name);
  85. }
  86. void StringTableSection::finalize() { StrTabBuilder.finalize(); }
  87. void StringTableSection::writeSection(FileOutputBuffer &Out) const {
  88. StrTabBuilder.write(Out.getBufferStart() + Offset);
  89. }
  90. static bool isValidReservedSectionIndex(uint16_t Index, uint16_t Machine) {
  91. switch (Index) {
  92. case SHN_ABS:
  93. case SHN_COMMON:
  94. return true;
  95. }
  96. if (Machine == EM_HEXAGON) {
  97. switch (Index) {
  98. case SHN_HEXAGON_SCOMMON:
  99. case SHN_HEXAGON_SCOMMON_2:
  100. case SHN_HEXAGON_SCOMMON_4:
  101. case SHN_HEXAGON_SCOMMON_8:
  102. return true;
  103. }
  104. }
  105. return false;
  106. }
  107. uint16_t Symbol::getShndx() const {
  108. if (DefinedIn != nullptr) {
  109. return DefinedIn->Index;
  110. }
  111. switch (ShndxType) {
  112. // This means that we don't have a defined section but we do need to
  113. // output a legitimate section index.
  114. case SYMBOL_SIMPLE_INDEX:
  115. return SHN_UNDEF;
  116. case SYMBOL_ABS:
  117. case SYMBOL_COMMON:
  118. case SYMBOL_HEXAGON_SCOMMON:
  119. case SYMBOL_HEXAGON_SCOMMON_2:
  120. case SYMBOL_HEXAGON_SCOMMON_4:
  121. case SYMBOL_HEXAGON_SCOMMON_8:
  122. return static_cast<uint16_t>(ShndxType);
  123. }
  124. llvm_unreachable("Symbol with invalid ShndxType encountered");
  125. }
  126. void SymbolTableSection::addSymbol(StringRef Name, uint8_t Bind, uint8_t Type,
  127. SectionBase *DefinedIn, uint64_t Value,
  128. uint8_t Visibility, uint16_t Shndx,
  129. uint64_t Sz) {
  130. Symbol Sym;
  131. Sym.Name = Name;
  132. Sym.Binding = Bind;
  133. Sym.Type = Type;
  134. Sym.DefinedIn = DefinedIn;
  135. if (DefinedIn == nullptr) {
  136. if (Shndx >= SHN_LORESERVE)
  137. Sym.ShndxType = static_cast<SymbolShndxType>(Shndx);
  138. else
  139. Sym.ShndxType = SYMBOL_SIMPLE_INDEX;
  140. }
  141. Sym.Value = Value;
  142. Sym.Visibility = Visibility;
  143. Sym.Size = Sz;
  144. Sym.Index = Symbols.size();
  145. Symbols.emplace_back(llvm::make_unique<Symbol>(Sym));
  146. Size += this->EntrySize;
  147. }
  148. void SymbolTableSection::removeSectionReferences(const SectionBase *Sec) {
  149. if (SymbolNames == Sec) {
  150. error("String table " + SymbolNames->Name +
  151. " cannot be removed because it is referenced by the symbol table " +
  152. this->Name);
  153. }
  154. auto Iter =
  155. std::remove_if(std::begin(Symbols), std::end(Symbols),
  156. [=](const SymPtr &Sym) { return Sym->DefinedIn == Sec; });
  157. Size -= (std::end(Symbols) - Iter) * this->EntrySize;
  158. Symbols.erase(Iter, std::end(Symbols));
  159. }
  160. void SymbolTableSection::initialize(SectionTableRef SecTable) {
  161. Size = 0;
  162. setStrTab(SecTable.getSectionOfType<StringTableSection>(
  163. Link,
  164. "Symbol table has link index of " + Twine(Link) +
  165. " which is not a valid index",
  166. "Symbol table has link index of " + Twine(Link) +
  167. " which is not a string table"));
  168. }
  169. void SymbolTableSection::finalize() {
  170. // Make sure SymbolNames is finalized before getting name indexes.
  171. SymbolNames->finalize();
  172. uint32_t MaxLocalIndex = 0;
  173. for (auto &Sym : Symbols) {
  174. Sym->NameIndex = SymbolNames->findIndex(Sym->Name);
  175. if (Sym->Binding == STB_LOCAL)
  176. MaxLocalIndex = std::max(MaxLocalIndex, Sym->Index);
  177. }
  178. // Now we need to set the Link and Info fields.
  179. Link = SymbolNames->Index;
  180. Info = MaxLocalIndex + 1;
  181. }
  182. void SymbolTableSection::addSymbolNames() {
  183. // Add all of our strings to SymbolNames so that SymbolNames has the right
  184. // size before layout is decided.
  185. for (auto &Sym : Symbols)
  186. SymbolNames->addString(Sym->Name);
  187. }
  188. const Symbol *SymbolTableSection::getSymbolByIndex(uint32_t Index) const {
  189. if (Symbols.size() <= Index)
  190. error("Invalid symbol index: " + Twine(Index));
  191. return Symbols[Index].get();
  192. }
  193. template <class ELFT>
  194. void SymbolTableSectionImpl<ELFT>::writeSection(FileOutputBuffer &Out) const {
  195. uint8_t *Buf = Out.getBufferStart();
  196. Buf += Offset;
  197. typename ELFT::Sym *Sym = reinterpret_cast<typename ELFT::Sym *>(Buf);
  198. // Loop though symbols setting each entry of the symbol table.
  199. for (auto &Symbol : Symbols) {
  200. Sym->st_name = Symbol->NameIndex;
  201. Sym->st_value = Symbol->Value;
  202. Sym->st_size = Symbol->Size;
  203. Sym->st_other = Symbol->Visibility;
  204. Sym->setBinding(Symbol->Binding);
  205. Sym->setType(Symbol->Type);
  206. Sym->st_shndx = Symbol->getShndx();
  207. ++Sym;
  208. }
  209. }
  210. template <class SymTabType>
  211. void RelocSectionWithSymtabBase<SymTabType>::removeSectionReferences(
  212. const SectionBase *Sec) {
  213. if (Symbols == Sec) {
  214. error("Symbol table " + Symbols->Name + " cannot be removed because it is "
  215. "referenced by the relocation "
  216. "section " +
  217. this->Name);
  218. }
  219. }
  220. template <class SymTabType>
  221. void RelocSectionWithSymtabBase<SymTabType>::initialize(
  222. SectionTableRef SecTable) {
  223. setSymTab(SecTable.getSectionOfType<SymTabType>(
  224. Link,
  225. "Link field value " + Twine(Link) + " in section " + Name + " is invalid",
  226. "Link field value " + Twine(Link) + " in section " + Name +
  227. " is not a symbol table"));
  228. if (Info != SHN_UNDEF)
  229. setSection(SecTable.getSection(Info,
  230. "Info field value " + Twine(Info) +
  231. " in section " + Name + " is invalid"));
  232. else
  233. setSection(nullptr);
  234. }
  235. template <class SymTabType>
  236. void RelocSectionWithSymtabBase<SymTabType>::finalize() {
  237. this->Link = Symbols->Index;
  238. if (SecToApplyRel != nullptr)
  239. this->Info = SecToApplyRel->Index;
  240. }
  241. template <class ELFT>
  242. void setAddend(Elf_Rel_Impl<ELFT, false> &Rel, uint64_t Addend) {}
  243. template <class ELFT>
  244. void setAddend(Elf_Rel_Impl<ELFT, true> &Rela, uint64_t Addend) {
  245. Rela.r_addend = Addend;
  246. }
  247. template <class ELFT>
  248. template <class T>
  249. void RelocationSection<ELFT>::writeRel(T *Buf) const {
  250. for (const auto &Reloc : Relocations) {
  251. Buf->r_offset = Reloc.Offset;
  252. setAddend(*Buf, Reloc.Addend);
  253. Buf->setSymbolAndType(Reloc.RelocSymbol->Index, Reloc.Type, false);
  254. ++Buf;
  255. }
  256. }
  257. template <class ELFT>
  258. void RelocationSection<ELFT>::writeSection(FileOutputBuffer &Out) const {
  259. uint8_t *Buf = Out.getBufferStart() + Offset;
  260. if (Type == SHT_REL)
  261. writeRel(reinterpret_cast<Elf_Rel *>(Buf));
  262. else
  263. writeRel(reinterpret_cast<Elf_Rela *>(Buf));
  264. }
  265. void DynamicRelocationSection::writeSection(FileOutputBuffer &Out) const {
  266. std::copy(std::begin(Contents), std::end(Contents),
  267. Out.getBufferStart() + Offset);
  268. }
  269. void SectionWithStrTab::removeSectionReferences(const SectionBase *Sec) {
  270. if (StrTab == Sec) {
  271. error("String table " + StrTab->Name + " cannot be removed because it is "
  272. "referenced by the section " +
  273. this->Name);
  274. }
  275. }
  276. bool SectionWithStrTab::classof(const SectionBase *S) {
  277. return isa<DynamicSymbolTableSection>(S) || isa<DynamicSection>(S);
  278. }
  279. void SectionWithStrTab::initialize(SectionTableRef SecTable) {
  280. auto StrTab = SecTable.getSection(Link,
  281. "Link field value " + Twine(Link) +
  282. " in section " + Name + " is invalid");
  283. if (StrTab->Type != SHT_STRTAB) {
  284. error("Link field value " + Twine(Link) + " in section " + Name +
  285. " is not a string table");
  286. }
  287. setStrTab(StrTab);
  288. }
  289. void SectionWithStrTab::finalize() { this->Link = StrTab->Index; }
  290. // Returns true IFF a section is wholly inside the range of a segment
  291. static bool sectionWithinSegment(const SectionBase &Section,
  292. const Segment &Segment) {
  293. // If a section is empty it should be treated like it has a size of 1. This is
  294. // to clarify the case when an empty section lies on a boundary between two
  295. // segments and ensures that the section "belongs" to the second segment and
  296. // not the first.
  297. uint64_t SecSize = Section.Size ? Section.Size : 1;
  298. return Segment.Offset <= Section.OriginalOffset &&
  299. Segment.Offset + Segment.FileSize >= Section.OriginalOffset + SecSize;
  300. }
  301. // Returns true IFF a segment's original offset is inside of another segment's
  302. // range.
  303. static bool segmentOverlapsSegment(const Segment &Child,
  304. const Segment &Parent) {
  305. return Parent.OriginalOffset <= Child.OriginalOffset &&
  306. Parent.OriginalOffset + Parent.FileSize > Child.OriginalOffset;
  307. }
  308. static bool compareSegments(const Segment *A, const Segment *B) {
  309. // Any segment without a parent segment should come before a segment
  310. // that has a parent segment.
  311. if (A->OriginalOffset < B->OriginalOffset)
  312. return true;
  313. if (A->OriginalOffset > B->OriginalOffset)
  314. return false;
  315. return A->Index < B->Index;
  316. }
  317. template <class ELFT>
  318. void Object<ELFT>::readProgramHeaders(const ELFFile<ELFT> &ElfFile) {
  319. uint32_t Index = 0;
  320. for (const auto &Phdr : unwrapOrError(ElfFile.program_headers())) {
  321. ArrayRef<uint8_t> Data{ElfFile.base() + Phdr.p_offset,
  322. (size_t)Phdr.p_filesz};
  323. Segments.emplace_back(llvm::make_unique<Segment>(Data));
  324. Segment &Seg = *Segments.back();
  325. Seg.Type = Phdr.p_type;
  326. Seg.Flags = Phdr.p_flags;
  327. Seg.OriginalOffset = Phdr.p_offset;
  328. Seg.Offset = Phdr.p_offset;
  329. Seg.VAddr = Phdr.p_vaddr;
  330. Seg.PAddr = Phdr.p_paddr;
  331. Seg.FileSize = Phdr.p_filesz;
  332. Seg.MemSize = Phdr.p_memsz;
  333. Seg.Align = Phdr.p_align;
  334. Seg.Index = Index++;
  335. for (auto &Section : Sections) {
  336. if (sectionWithinSegment(*Section, Seg)) {
  337. Seg.addSection(&*Section);
  338. if (!Section->ParentSegment ||
  339. Section->ParentSegment->Offset > Seg.Offset) {
  340. Section->ParentSegment = &Seg;
  341. }
  342. }
  343. }
  344. }
  345. // Now we do an O(n^2) loop through the segments in order to match up
  346. // segments.
  347. for (auto &Child : Segments) {
  348. for (auto &Parent : Segments) {
  349. // Every segment will overlap with itself but we don't want a segment to
  350. // be it's own parent so we avoid that situation.
  351. if (&Child != &Parent && segmentOverlapsSegment(*Child, *Parent)) {
  352. // We want a canonical "most parental" segment but this requires
  353. // inspecting the ParentSegment.
  354. if (compareSegments(Parent.get(), Child.get()))
  355. if (Child->ParentSegment == nullptr ||
  356. compareSegments(Parent.get(), Child->ParentSegment)) {
  357. Child->ParentSegment = Parent.get();
  358. }
  359. }
  360. }
  361. }
  362. }
  363. template <class ELFT>
  364. void Object<ELFT>::initSymbolTable(const object::ELFFile<ELFT> &ElfFile,
  365. SymbolTableSection *SymTab,
  366. SectionTableRef SecTable) {
  367. const Elf_Shdr &Shdr = *unwrapOrError(ElfFile.getSection(SymTab->Index));
  368. StringRef StrTabData = unwrapOrError(ElfFile.getStringTableForSymtab(Shdr));
  369. for (const auto &Sym : unwrapOrError(ElfFile.symbols(&Shdr))) {
  370. SectionBase *DefSection = nullptr;
  371. StringRef Name = unwrapOrError(Sym.getName(StrTabData));
  372. if (Sym.st_shndx >= SHN_LORESERVE) {
  373. if (!isValidReservedSectionIndex(Sym.st_shndx, Machine)) {
  374. error(
  375. "Symbol '" + Name +
  376. "' has unsupported value greater than or equal to SHN_LORESERVE: " +
  377. Twine(Sym.st_shndx));
  378. }
  379. } else if (Sym.st_shndx != SHN_UNDEF) {
  380. DefSection = SecTable.getSection(
  381. Sym.st_shndx,
  382. "Symbol '" + Name + "' is defined in invalid section with index " +
  383. Twine(Sym.st_shndx));
  384. }
  385. SymTab->addSymbol(Name, Sym.getBinding(), Sym.getType(), DefSection,
  386. Sym.getValue(), Sym.st_other, Sym.st_shndx, Sym.st_size);
  387. }
  388. }
  389. template <class ELFT>
  390. static void getAddend(uint64_t &ToSet, const Elf_Rel_Impl<ELFT, false> &Rel) {}
  391. template <class ELFT>
  392. static void getAddend(uint64_t &ToSet, const Elf_Rel_Impl<ELFT, true> &Rela) {
  393. ToSet = Rela.r_addend;
  394. }
  395. template <class ELFT, class T>
  396. void initRelocations(RelocationSection<ELFT> *Relocs,
  397. SymbolTableSection *SymbolTable, T RelRange) {
  398. for (const auto &Rel : RelRange) {
  399. Relocation ToAdd;
  400. ToAdd.Offset = Rel.r_offset;
  401. getAddend(ToAdd.Addend, Rel);
  402. ToAdd.Type = Rel.getType(false);
  403. ToAdd.RelocSymbol = SymbolTable->getSymbolByIndex(Rel.getSymbol(false));
  404. Relocs->addRelocation(ToAdd);
  405. }
  406. }
  407. SectionBase *SectionTableRef::getSection(uint16_t Index, Twine ErrMsg) {
  408. if (Index == SHN_UNDEF || Index > Sections.size())
  409. error(ErrMsg);
  410. return Sections[Index - 1].get();
  411. }
  412. template <class T>
  413. T *SectionTableRef::getSectionOfType(uint16_t Index, Twine IndexErrMsg,
  414. Twine TypeErrMsg) {
  415. if (T *Sec = dyn_cast<T>(getSection(Index, IndexErrMsg)))
  416. return Sec;
  417. error(TypeErrMsg);
  418. }
  419. template <class ELFT>
  420. std::unique_ptr<SectionBase>
  421. Object<ELFT>::makeSection(const object::ELFFile<ELFT> &ElfFile,
  422. const Elf_Shdr &Shdr) {
  423. ArrayRef<uint8_t> Data;
  424. switch (Shdr.sh_type) {
  425. case SHT_REL:
  426. case SHT_RELA:
  427. if (Shdr.sh_flags & SHF_ALLOC) {
  428. Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
  429. return llvm::make_unique<DynamicRelocationSection>(Data);
  430. }
  431. return llvm::make_unique<RelocationSection<ELFT>>();
  432. case SHT_STRTAB:
  433. // If a string table is allocated we don't want to mess with it. That would
  434. // mean altering the memory image. There are no special link types or
  435. // anything so we can just use a Section.
  436. if (Shdr.sh_flags & SHF_ALLOC) {
  437. Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
  438. return llvm::make_unique<Section>(Data);
  439. }
  440. return llvm::make_unique<StringTableSection>();
  441. case SHT_HASH:
  442. case SHT_GNU_HASH:
  443. // Hash tables should refer to SHT_DYNSYM which we're not going to change.
  444. // Because of this we don't need to mess with the hash tables either.
  445. Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
  446. return llvm::make_unique<Section>(Data);
  447. case SHT_DYNSYM:
  448. Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
  449. return llvm::make_unique<DynamicSymbolTableSection>(Data);
  450. case SHT_DYNAMIC:
  451. Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
  452. return llvm::make_unique<DynamicSection>(Data);
  453. case SHT_SYMTAB: {
  454. auto SymTab = llvm::make_unique<SymbolTableSectionImpl<ELFT>>();
  455. SymbolTable = SymTab.get();
  456. return std::move(SymTab);
  457. }
  458. case SHT_NOBITS:
  459. return llvm::make_unique<Section>(Data);
  460. default:
  461. Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
  462. return llvm::make_unique<Section>(Data);
  463. }
  464. }
  465. template <class ELFT>
  466. SectionTableRef Object<ELFT>::readSectionHeaders(const ELFFile<ELFT> &ElfFile) {
  467. uint32_t Index = 0;
  468. for (const auto &Shdr : unwrapOrError(ElfFile.sections())) {
  469. if (Index == 0) {
  470. ++Index;
  471. continue;
  472. }
  473. SecPtr Sec = makeSection(ElfFile, Shdr);
  474. Sec->Name = unwrapOrError(ElfFile.getSectionName(&Shdr));
  475. Sec->Type = Shdr.sh_type;
  476. Sec->Flags = Shdr.sh_flags;
  477. Sec->Addr = Shdr.sh_addr;
  478. Sec->Offset = Shdr.sh_offset;
  479. Sec->OriginalOffset = Shdr.sh_offset;
  480. Sec->Size = Shdr.sh_size;
  481. Sec->Link = Shdr.sh_link;
  482. Sec->Info = Shdr.sh_info;
  483. Sec->Align = Shdr.sh_addralign;
  484. Sec->EntrySize = Shdr.sh_entsize;
  485. Sec->Index = Index++;
  486. Sections.push_back(std::move(Sec));
  487. }
  488. SectionTableRef SecTable(Sections);
  489. // Now that all of the sections have been added we can fill out some extra
  490. // details about symbol tables. We need the symbol table filled out before
  491. // any relocations.
  492. if (SymbolTable) {
  493. SymbolTable->initialize(SecTable);
  494. initSymbolTable(ElfFile, SymbolTable, SecTable);
  495. }
  496. // Now that all sections and symbols have been added we can add
  497. // relocations that reference symbols and set the link and info fields for
  498. // relocation sections.
  499. for (auto &Section : Sections) {
  500. if (Section.get() == SymbolTable)
  501. continue;
  502. Section->initialize(SecTable);
  503. if (auto RelSec = dyn_cast<RelocationSection<ELFT>>(Section.get())) {
  504. auto Shdr = unwrapOrError(ElfFile.sections()).begin() + RelSec->Index;
  505. if (RelSec->Type == SHT_REL)
  506. initRelocations(RelSec, SymbolTable, unwrapOrError(ElfFile.rels(Shdr)));
  507. else
  508. initRelocations(RelSec, SymbolTable,
  509. unwrapOrError(ElfFile.relas(Shdr)));
  510. }
  511. }
  512. return SecTable;
  513. }
  514. template <class ELFT> Object<ELFT>::Object(const ELFObjectFile<ELFT> &Obj) {
  515. const auto &ElfFile = *Obj.getELFFile();
  516. const auto &Ehdr = *ElfFile.getHeader();
  517. std::copy(Ehdr.e_ident, Ehdr.e_ident + 16, Ident);
  518. Type = Ehdr.e_type;
  519. Machine = Ehdr.e_machine;
  520. Version = Ehdr.e_version;
  521. Entry = Ehdr.e_entry;
  522. Flags = Ehdr.e_flags;
  523. SectionTableRef SecTable = readSectionHeaders(ElfFile);
  524. readProgramHeaders(ElfFile);
  525. SectionNames = SecTable.getSectionOfType<StringTableSection>(
  526. Ehdr.e_shstrndx,
  527. "e_shstrndx field value " + Twine(Ehdr.e_shstrndx) + " in elf header " +
  528. " is invalid",
  529. "e_shstrndx field value " + Twine(Ehdr.e_shstrndx) + " in elf header " +
  530. " is not a string table");
  531. }
  532. template <class ELFT>
  533. void Object<ELFT>::writeHeader(FileOutputBuffer &Out) const {
  534. uint8_t *Buf = Out.getBufferStart();
  535. Elf_Ehdr &Ehdr = *reinterpret_cast<Elf_Ehdr *>(Buf);
  536. std::copy(Ident, Ident + 16, Ehdr.e_ident);
  537. Ehdr.e_type = Type;
  538. Ehdr.e_machine = Machine;
  539. Ehdr.e_version = Version;
  540. Ehdr.e_entry = Entry;
  541. Ehdr.e_phoff = sizeof(Elf_Ehdr);
  542. Ehdr.e_flags = Flags;
  543. Ehdr.e_ehsize = sizeof(Elf_Ehdr);
  544. Ehdr.e_phentsize = sizeof(Elf_Phdr);
  545. Ehdr.e_phnum = Segments.size();
  546. Ehdr.e_shentsize = sizeof(Elf_Shdr);
  547. if (WriteSectionHeaders) {
  548. Ehdr.e_shoff = SHOffset;
  549. Ehdr.e_shnum = Sections.size() + 1;
  550. Ehdr.e_shstrndx = SectionNames->Index;
  551. } else {
  552. Ehdr.e_shoff = 0;
  553. Ehdr.e_shnum = 0;
  554. Ehdr.e_shstrndx = 0;
  555. }
  556. }
  557. template <class ELFT>
  558. void Object<ELFT>::writeProgramHeaders(FileOutputBuffer &Out) const {
  559. for (auto &Phdr : Segments)
  560. Phdr->template writeHeader<ELFT>(Out);
  561. }
  562. template <class ELFT>
  563. void Object<ELFT>::writeSectionHeaders(FileOutputBuffer &Out) const {
  564. uint8_t *Buf = Out.getBufferStart() + SHOffset;
  565. // This reference serves to write the dummy section header at the begining
  566. // of the file. It is not used for anything else
  567. Elf_Shdr &Shdr = *reinterpret_cast<Elf_Shdr *>(Buf);
  568. Shdr.sh_name = 0;
  569. Shdr.sh_type = SHT_NULL;
  570. Shdr.sh_flags = 0;
  571. Shdr.sh_addr = 0;
  572. Shdr.sh_offset = 0;
  573. Shdr.sh_size = 0;
  574. Shdr.sh_link = 0;
  575. Shdr.sh_info = 0;
  576. Shdr.sh_addralign = 0;
  577. Shdr.sh_entsize = 0;
  578. for (auto &Section : Sections)
  579. Section->template writeHeader<ELFT>(Out);
  580. }
  581. template <class ELFT>
  582. void Object<ELFT>::writeSectionData(FileOutputBuffer &Out) const {
  583. for (auto &Section : Sections)
  584. Section->writeSection(Out);
  585. }
  586. template <class ELFT>
  587. void Object<ELFT>::removeSections(
  588. std::function<bool(const SectionBase &)> ToRemove) {
  589. auto Iter = std::stable_partition(
  590. std::begin(Sections), std::end(Sections), [=](const SecPtr &Sec) {
  591. if (ToRemove(*Sec))
  592. return false;
  593. if (auto RelSec = dyn_cast<RelocationSectionBase>(Sec.get())) {
  594. if (auto ToRelSec = RelSec->getSection())
  595. return !ToRemove(*ToRelSec);
  596. }
  597. return true;
  598. });
  599. if (SymbolTable != nullptr && ToRemove(*SymbolTable))
  600. SymbolTable = nullptr;
  601. if (ToRemove(*SectionNames)) {
  602. if (WriteSectionHeaders)
  603. error("Cannot remove " + SectionNames->Name +
  604. " because it is the section header string table.");
  605. SectionNames = nullptr;
  606. }
  607. // Now make sure there are no remaining references to the sections that will
  608. // be removed. Sometimes it is impossible to remove a reference so we emit
  609. // an error here instead.
  610. for (auto &RemoveSec : make_range(Iter, std::end(Sections))) {
  611. for (auto &Segment : Segments)
  612. Segment->removeSection(RemoveSec.get());
  613. for (auto &KeepSec : make_range(std::begin(Sections), Iter))
  614. KeepSec->removeSectionReferences(RemoveSec.get());
  615. }
  616. // Now finally get rid of them all togethor.
  617. Sections.erase(Iter, std::end(Sections));
  618. }
  619. template <class ELFT>
  620. void Object<ELFT>::addSection(StringRef SecName, ArrayRef<uint8_t> Data) {
  621. auto Sec = llvm::make_unique<OwnedDataSection>(SecName, Data);
  622. Sec->OriginalOffset = ~0ULL;
  623. Sections.push_back(std::move(Sec));
  624. }
  625. template <class ELFT> void ELFObject<ELFT>::sortSections() {
  626. // Put all sections in offset order. Maintain the ordering as closely as
  627. // possible while meeting that demand however.
  628. auto CompareSections = [](const SecPtr &A, const SecPtr &B) {
  629. return A->OriginalOffset < B->OriginalOffset;
  630. };
  631. std::stable_sort(std::begin(this->Sections), std::end(this->Sections),
  632. CompareSections);
  633. }
  634. static uint64_t alignToAddr(uint64_t Offset, uint64_t Addr, uint64_t Align) {
  635. // Calculate Diff such that (Offset + Diff) & -Align == Addr & -Align.
  636. if (Align == 0)
  637. Align = 1;
  638. auto Diff =
  639. static_cast<int64_t>(Addr % Align) - static_cast<int64_t>(Offset % Align);
  640. // We only want to add to Offset, however, so if Diff < 0 we can add Align and
  641. // (Offset + Diff) & -Align == Addr & -Align will still hold.
  642. if (Diff < 0)
  643. Diff += Align;
  644. return Offset + Diff;
  645. }
  646. // Orders segments such that if x = y->ParentSegment then y comes before x.
  647. static void OrderSegments(std::vector<Segment *> &Segments) {
  648. std::stable_sort(std::begin(Segments), std::end(Segments), compareSegments);
  649. }
  650. // This function finds a consistent layout for a list of segments starting from
  651. // an Offset. It assumes that Segments have been sorted by OrderSegments and
  652. // returns an Offset one past the end of the last segment.
  653. static uint64_t LayoutSegments(std::vector<Segment *> &Segments,
  654. uint64_t Offset) {
  655. assert(std::is_sorted(std::begin(Segments), std::end(Segments),
  656. compareSegments));
  657. // The only way a segment should move is if a section was between two
  658. // segments and that section was removed. If that section isn't in a segment
  659. // then it's acceptable, but not ideal, to simply move it to after the
  660. // segments. So we can simply layout segments one after the other accounting
  661. // for alignment.
  662. for (auto &Segment : Segments) {
  663. // We assume that segments have been ordered by OriginalOffset and Index
  664. // such that a parent segment will always come before a child segment in
  665. // OrderedSegments. This means that the Offset of the ParentSegment should
  666. // already be set and we can set our offset relative to it.
  667. if (Segment->ParentSegment != nullptr) {
  668. auto Parent = Segment->ParentSegment;
  669. Segment->Offset =
  670. Parent->Offset + Segment->OriginalOffset - Parent->OriginalOffset;
  671. } else {
  672. Offset = alignToAddr(Offset, Segment->VAddr, Segment->Align);
  673. Segment->Offset = Offset;
  674. }
  675. Offset = std::max(Offset, Segment->Offset + Segment->FileSize);
  676. }
  677. return Offset;
  678. }
  679. // This function finds a consistent layout for a list of sections. It assumes
  680. // that the ->ParentSegment of each section has already been laid out. The
  681. // supplied starting Offset is used for the starting offset of any section that
  682. // does not have a ParentSegment. It returns either the offset given if all
  683. // sections had a ParentSegment or an offset one past the last section if there
  684. // was a section that didn't have a ParentSegment.
  685. template <class SecPtr>
  686. static uint64_t LayoutSections(std::vector<SecPtr> &Sections, uint64_t Offset) {
  687. // Now the offset of every segment has been set we can assign the offsets
  688. // of each section. For sections that are covered by a segment we should use
  689. // the segment's original offset and the section's original offset to compute
  690. // the offset from the start of the segment. Using the offset from the start
  691. // of the segment we can assign a new offset to the section. For sections not
  692. // covered by segments we can just bump Offset to the next valid location.
  693. uint32_t Index = 1;
  694. for (auto &Section : Sections) {
  695. Section->Index = Index++;
  696. if (Section->ParentSegment != nullptr) {
  697. auto Segment = Section->ParentSegment;
  698. Section->Offset =
  699. Segment->Offset + (Section->OriginalOffset - Segment->OriginalOffset);
  700. } else {
  701. Offset = alignTo(Offset, Section->Align == 0 ? 1 : Section->Align);
  702. Section->Offset = Offset;
  703. if (Section->Type != SHT_NOBITS)
  704. Offset += Section->Size;
  705. }
  706. }
  707. return Offset;
  708. }
  709. template <class ELFT> void ELFObject<ELFT>::assignOffsets() {
  710. // We need a temporary list of segments that has a special order to it
  711. // so that we know that anytime ->ParentSegment is set that segment has
  712. // already had its offset properly set.
  713. std::vector<Segment *> OrderedSegments;
  714. for (auto &Segment : this->Segments)
  715. OrderedSegments.push_back(Segment.get());
  716. OrderSegments(OrderedSegments);
  717. // The size of ELF + program headers will not change so it is ok to assume
  718. // that the first offset of the first segment is a good place to start
  719. // outputting sections. This covers both the standard case and the PT_PHDR
  720. // case.
  721. uint64_t Offset;
  722. if (!OrderedSegments.empty()) {
  723. Offset = OrderedSegments[0]->Offset;
  724. } else {
  725. Offset = sizeof(Elf_Ehdr);
  726. }
  727. Offset = LayoutSegments(OrderedSegments, Offset);
  728. Offset = LayoutSections(this->Sections, Offset);
  729. // If we need to write the section header table out then we need to align the
  730. // Offset so that SHOffset is valid.
  731. if (this->WriteSectionHeaders)
  732. Offset = alignTo(Offset, sizeof(typename ELFT::Addr));
  733. this->SHOffset = Offset;
  734. }
  735. template <class ELFT> size_t ELFObject<ELFT>::totalSize() const {
  736. // We already have the section header offset so we can calculate the total
  737. // size by just adding up the size of each section header.
  738. auto NullSectionSize = this->WriteSectionHeaders ? sizeof(Elf_Shdr) : 0;
  739. return this->SHOffset + this->Sections.size() * sizeof(Elf_Shdr) +
  740. NullSectionSize;
  741. }
  742. template <class ELFT> void ELFObject<ELFT>::write(FileOutputBuffer &Out) const {
  743. this->writeHeader(Out);
  744. this->writeProgramHeaders(Out);
  745. this->writeSectionData(Out);
  746. if (this->WriteSectionHeaders)
  747. this->writeSectionHeaders(Out);
  748. }
  749. template <class ELFT> void ELFObject<ELFT>::finalize() {
  750. // Make sure we add the names of all the sections.
  751. if (this->SectionNames != nullptr)
  752. for (const auto &Section : this->Sections) {
  753. this->SectionNames->addString(Section->Name);
  754. }
  755. // Make sure we add the names of all the symbols.
  756. if (this->SymbolTable != nullptr)
  757. this->SymbolTable->addSymbolNames();
  758. sortSections();
  759. assignOffsets();
  760. // Finalize SectionNames first so that we can assign name indexes.
  761. if (this->SectionNames != nullptr)
  762. this->SectionNames->finalize();
  763. // Finally now that all offsets and indexes have been set we can finalize any
  764. // remaining issues.
  765. uint64_t Offset = this->SHOffset + sizeof(Elf_Shdr);
  766. for (auto &Section : this->Sections) {
  767. Section->HeaderOffset = Offset;
  768. Offset += sizeof(Elf_Shdr);
  769. if (this->WriteSectionHeaders)
  770. Section->NameIndex = this->SectionNames->findIndex(Section->Name);
  771. Section->finalize();
  772. }
  773. }
  774. template <class ELFT> size_t BinaryObject<ELFT>::totalSize() const {
  775. return TotalSize;
  776. }
  777. template <class ELFT>
  778. void BinaryObject<ELFT>::write(FileOutputBuffer &Out) const {
  779. for (auto &Section : this->Sections) {
  780. if ((Section->Flags & SHF_ALLOC) == 0)
  781. continue;
  782. Section->writeSection(Out);
  783. }
  784. }
  785. template <class ELFT> void BinaryObject<ELFT>::finalize() {
  786. // TODO: Create a filter range to construct OrderedSegments from so that this
  787. // code can be deduped with assignOffsets above. This should also solve the
  788. // todo below for LayoutSections.
  789. // We need a temporary list of segments that has a special order to it
  790. // so that we know that anytime ->ParentSegment is set that segment has
  791. // already had it's offset properly set. We only want to consider the segments
  792. // that will affect layout of allocated sections so we only add those.
  793. std::vector<Segment *> OrderedSegments;
  794. for (auto &Section : this->Sections) {
  795. if ((Section->Flags & SHF_ALLOC) != 0 &&
  796. Section->ParentSegment != nullptr) {
  797. OrderedSegments.push_back(Section->ParentSegment);
  798. }
  799. }
  800. OrderSegments(OrderedSegments);
  801. // Because we add a ParentSegment for each section we might have duplicate
  802. // segments in OrderedSegments. If there were duplicates then LayoutSegments
  803. // would do very strange things.
  804. auto End =
  805. std::unique(std::begin(OrderedSegments), std::end(OrderedSegments));
  806. OrderedSegments.erase(End, std::end(OrderedSegments));
  807. // Modify the first segment so that there is no gap at the start. This allows
  808. // our layout algorithm to proceed as expected while not out writing out the
  809. // gap at the start.
  810. if (!OrderedSegments.empty()) {
  811. auto Seg = OrderedSegments[0];
  812. auto Sec = Seg->firstSection();
  813. auto Diff = Sec->OriginalOffset - Seg->OriginalOffset;
  814. Seg->OriginalOffset += Diff;
  815. // The size needs to be shrunk as well
  816. Seg->FileSize -= Diff;
  817. Seg->MemSize -= Diff;
  818. // The VAddr needs to be adjusted so that the alignment is correct as well
  819. Seg->VAddr += Diff;
  820. Seg->PAddr = Seg->VAddr;
  821. // We don't want this to be shifted by alignment so we need to set the
  822. // alignment to zero.
  823. Seg->Align = 0;
  824. }
  825. uint64_t Offset = LayoutSegments(OrderedSegments, 0);
  826. // TODO: generalize LayoutSections to take a range. Pass a special range
  827. // constructed from an iterator that skips values for which a predicate does
  828. // not hold. Then pass such a range to LayoutSections instead of constructing
  829. // AllocatedSections here.
  830. std::vector<SectionBase *> AllocatedSections;
  831. for (auto &Section : this->Sections) {
  832. if ((Section->Flags & SHF_ALLOC) == 0)
  833. continue;
  834. AllocatedSections.push_back(Section.get());
  835. }
  836. LayoutSections(AllocatedSections, Offset);
  837. // Now that every section has been laid out we just need to compute the total
  838. // file size. This might not be the same as the offset returned by
  839. // LayoutSections, because we want to truncate the last segment to the end of
  840. // its last section, to match GNU objcopy's behaviour.
  841. TotalSize = 0;
  842. for (const auto &Section : AllocatedSections) {
  843. if (Section->Type != SHT_NOBITS)
  844. TotalSize = std::max(TotalSize, Section->Offset + Section->Size);
  845. }
  846. }
  847. namespace llvm {
  848. template class Object<ELF64LE>;
  849. template class Object<ELF64BE>;
  850. template class Object<ELF32LE>;
  851. template class Object<ELF32BE>;
  852. template class ELFObject<ELF64LE>;
  853. template class ELFObject<ELF64BE>;
  854. template class ELFObject<ELF32LE>;
  855. template class ELFObject<ELF32BE>;
  856. template class BinaryObject<ELF64LE>;
  857. template class BinaryObject<ELF64BE>;
  858. template class BinaryObject<ELF32LE>;
  859. template class BinaryObject<ELF32BE>;
  860. } // end namespace llvm