gc.cc 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // gc.cc -- garbage collection of unused sections
  2. // Copyright (C) 2009-2022 Free Software Foundation, Inc.
  3. // Written by Sriraman Tallam <tmsriram@google.com>.
  4. // This file is part of gold.
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. // MA 02110-1301, USA.
  17. #include "gold.h"
  18. #include "object.h"
  19. #include "gc.h"
  20. #include "symtab.h"
  21. namespace gold
  22. {
  23. // Garbage collection uses a worklist style algorithm to determine the
  24. // transitive closure of all referenced sections.
  25. void
  26. Garbage_collection::do_transitive_closure()
  27. {
  28. while (!this->worklist().empty())
  29. {
  30. // Add elements from the work list to the referenced list
  31. // one by one.
  32. Section_id entry = this->worklist().back();
  33. this->worklist().pop_back();
  34. if (!this->referenced_list().insert(entry).second)
  35. continue;
  36. Garbage_collection::Section_ref::iterator find_it =
  37. this->section_reloc_map().find(entry);
  38. if (find_it == this->section_reloc_map().end())
  39. continue;
  40. const Garbage_collection::Sections_reachable &v = find_it->second;
  41. // Scan the vector of references for each work_list entry.
  42. for (Garbage_collection::Sections_reachable::const_iterator it_v =
  43. v.begin();
  44. it_v != v.end();
  45. ++it_v)
  46. {
  47. // Do not add already processed sections to the work_list.
  48. if (this->referenced_list().find(*it_v)
  49. == this->referenced_list().end())
  50. {
  51. this->worklist().push_back(*it_v);
  52. }
  53. }
  54. }
  55. this->worklist_ready();
  56. }
  57. } // End namespace gold.