Object.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //===- Object.cpp ---------------------------------------------------------===//
  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. #include "Object.h"
  9. #include "llvm/ADT/DenseSet.h"
  10. #include <algorithm>
  11. namespace llvm {
  12. namespace objcopy {
  13. namespace coff {
  14. using namespace object;
  15. void Object::addSymbols(ArrayRef<Symbol> NewSymbols) {
  16. for (Symbol S : NewSymbols) {
  17. S.UniqueId = NextSymbolUniqueId++;
  18. Symbols.emplace_back(S);
  19. }
  20. updateSymbols();
  21. }
  22. void Object::updateSymbols() {
  23. SymbolMap = DenseMap<size_t, Symbol *>(Symbols.size());
  24. for (Symbol &Sym : Symbols)
  25. SymbolMap[Sym.UniqueId] = &Sym;
  26. }
  27. const Symbol *Object::findSymbol(size_t UniqueId) const {
  28. return SymbolMap.lookup(UniqueId);
  29. }
  30. Error Object::removeSymbols(
  31. function_ref<Expected<bool>(const Symbol &)> ToRemove) {
  32. Error Errs = Error::success();
  33. llvm::erase_if(Symbols, [ToRemove, &Errs](const Symbol &Sym) {
  34. Expected<bool> ShouldRemove = ToRemove(Sym);
  35. if (!ShouldRemove) {
  36. Errs = joinErrors(std::move(Errs), ShouldRemove.takeError());
  37. return false;
  38. }
  39. return *ShouldRemove;
  40. });
  41. updateSymbols();
  42. return Errs;
  43. }
  44. Error Object::markSymbols() {
  45. for (Symbol &Sym : Symbols)
  46. Sym.Referenced = false;
  47. for (const Section &Sec : Sections) {
  48. for (const Relocation &R : Sec.Relocs) {
  49. auto It = SymbolMap.find(R.Target);
  50. if (It == SymbolMap.end())
  51. return createStringError(object_error::invalid_symbol_index,
  52. "relocation target %zu not found", R.Target);
  53. It->second->Referenced = true;
  54. }
  55. }
  56. return Error::success();
  57. }
  58. void Object::addSections(ArrayRef<Section> NewSections) {
  59. for (Section S : NewSections) {
  60. S.UniqueId = NextSectionUniqueId++;
  61. Sections.emplace_back(S);
  62. }
  63. updateSections();
  64. }
  65. void Object::updateSections() {
  66. SectionMap = DenseMap<ssize_t, Section *>(Sections.size());
  67. size_t Index = 1;
  68. for (Section &S : Sections) {
  69. SectionMap[S.UniqueId] = &S;
  70. S.Index = Index++;
  71. }
  72. }
  73. const Section *Object::findSection(ssize_t UniqueId) const {
  74. return SectionMap.lookup(UniqueId);
  75. }
  76. void Object::removeSections(function_ref<bool(const Section &)> ToRemove) {
  77. DenseSet<ssize_t> AssociatedSections;
  78. auto RemoveAssociated = [&AssociatedSections](const Section &Sec) {
  79. return AssociatedSections.contains(Sec.UniqueId);
  80. };
  81. do {
  82. DenseSet<ssize_t> RemovedSections;
  83. llvm::erase_if(Sections, [ToRemove, &RemovedSections](const Section &Sec) {
  84. bool Remove = ToRemove(Sec);
  85. if (Remove)
  86. RemovedSections.insert(Sec.UniqueId);
  87. return Remove;
  88. });
  89. // Remove all symbols referring to the removed sections.
  90. AssociatedSections.clear();
  91. llvm::erase_if(
  92. Symbols, [&RemovedSections, &AssociatedSections](const Symbol &Sym) {
  93. // If there are sections that are associative to a removed
  94. // section,
  95. // remove those as well as nothing will include them (and we can't
  96. // leave them dangling).
  97. if (RemovedSections.contains(Sym.AssociativeComdatTargetSectionId))
  98. AssociatedSections.insert(Sym.TargetSectionId);
  99. return RemovedSections.contains(Sym.TargetSectionId);
  100. });
  101. ToRemove = RemoveAssociated;
  102. } while (!AssociatedSections.empty());
  103. updateSections();
  104. updateSymbols();
  105. }
  106. void Object::truncateSections(function_ref<bool(const Section &)> ToTruncate) {
  107. for (Section &Sec : Sections) {
  108. if (ToTruncate(Sec)) {
  109. Sec.clearContents();
  110. Sec.Relocs.clear();
  111. Sec.Header.SizeOfRawData = 0;
  112. }
  113. }
  114. }
  115. } // end namespace coff
  116. } // end namespace objcopy
  117. } // end namespace llvm