gc.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. // gc.h -- 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. #ifndef GOLD_GC_H
  18. #define GOLD_GC_H
  19. #include <vector>
  20. #include "elfcpp.h"
  21. #include "symtab.h"
  22. #include "object.h"
  23. #include "icf.h"
  24. namespace gold
  25. {
  26. class Object;
  27. template<int size, bool big_endian>
  28. class Sized_relobj_file;
  29. class Output_section;
  30. class General_options;
  31. class Layout;
  32. class Garbage_collection
  33. {
  34. public:
  35. typedef Unordered_set<Section_id, Section_id_hash> Sections_reachable;
  36. typedef std::map<Section_id, Sections_reachable> Section_ref;
  37. typedef std::vector<Section_id> Worklist_type;
  38. // This maps the name of the section which can be represented as a C
  39. // identifier (cident) to the list of sections that have that name.
  40. // Different object files can have cident sections with the same name.
  41. typedef std::map<std::string, Sections_reachable> Cident_section_map;
  42. Garbage_collection()
  43. : is_worklist_ready_(false)
  44. { }
  45. // Accessor methods for the private members.
  46. Sections_reachable&
  47. referenced_list()
  48. { return referenced_list_; }
  49. Section_ref&
  50. section_reloc_map()
  51. { return this->section_reloc_map_; }
  52. Worklist_type&
  53. worklist()
  54. { return this->work_list_; }
  55. bool
  56. is_worklist_ready()
  57. { return this->is_worklist_ready_; }
  58. void
  59. worklist_ready()
  60. { this->is_worklist_ready_ = true; }
  61. void
  62. do_transitive_closure();
  63. bool
  64. is_section_garbage(Relobj* obj, unsigned int shndx)
  65. { return (this->referenced_list().find(Section_id(obj, shndx))
  66. == this->referenced_list().end()); }
  67. Cident_section_map*
  68. cident_sections()
  69. { return &cident_sections_; }
  70. void
  71. add_cident_section(std::string section_name,
  72. Section_id secn)
  73. { this->cident_sections_[section_name].insert(secn); }
  74. // Add a reference from the SRC_SHNDX-th section of SRC_OBJECT to
  75. // DST_SHNDX-th section of DST_OBJECT.
  76. void
  77. add_reference(Relobj* src_object, unsigned int src_shndx,
  78. Relobj* dst_object, unsigned int dst_shndx)
  79. {
  80. Section_id src_id(src_object, src_shndx);
  81. Section_id dst_id(dst_object, dst_shndx);
  82. Sections_reachable& reachable = this->section_reloc_map_[src_id];
  83. reachable.insert(dst_id);
  84. }
  85. private:
  86. Worklist_type work_list_;
  87. bool is_worklist_ready_;
  88. Section_ref section_reloc_map_;
  89. Sections_reachable referenced_list_;
  90. Cident_section_map cident_sections_;
  91. };
  92. // Data to pass between successive invocations of do_layout
  93. // in object.cc while garbage collecting. This data structure
  94. // is filled by using the data from Read_symbols_data.
  95. struct Symbols_data
  96. {
  97. // Section headers.
  98. unsigned char* section_headers_data;
  99. // Section names.
  100. unsigned char* section_names_data;
  101. // Size of section name data in bytes.
  102. section_size_type section_names_size;
  103. // Symbol data.
  104. unsigned char* symbols_data;
  105. // Size of symbol data in bytes.
  106. section_size_type symbols_size;
  107. // Offset of external symbols within symbol data. This structure
  108. // sometimes contains only external symbols, in which case this will
  109. // be zero. Sometimes it contains all symbols.
  110. section_offset_type external_symbols_offset;
  111. // Symbol names.
  112. unsigned char* symbol_names_data;
  113. // Size of symbol name data in bytes.
  114. section_size_type symbol_names_size;
  115. };
  116. // Relocations of type SHT_REL store the addend value in their bytes.
  117. // This function returns the size of the embedded addend which is
  118. // nothing but the size of the relocation.
  119. template<typename Classify_reloc>
  120. inline unsigned int
  121. get_embedded_addend_size(int r_type, Relobj* obj)
  122. {
  123. if (Classify_reloc::sh_type == elfcpp::SHT_REL)
  124. return Classify_reloc::get_size_for_reloc(r_type, obj);
  125. return 0;
  126. }
  127. // This function implements the generic part of reloc
  128. // processing to map a section to all the sections it
  129. // references through relocs. It is called only during
  130. // garbage collection (--gc-sections) and identical code
  131. // folding (--icf).
  132. template<int size, bool big_endian, typename Target_type,
  133. typename Scan, typename Classify_reloc>
  134. inline void
  135. gc_process_relocs(
  136. Symbol_table* symtab,
  137. Layout*,
  138. Target_type* target,
  139. Sized_relobj_file<size, big_endian>* src_obj,
  140. unsigned int src_indx,
  141. const unsigned char* prelocs,
  142. size_t reloc_count,
  143. Output_section*,
  144. bool,
  145. size_t local_count,
  146. const unsigned char* plocal_syms)
  147. {
  148. Scan scan;
  149. typedef typename Classify_reloc::Reltype Reltype;
  150. const int reloc_size = Classify_reloc::reloc_size;
  151. const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
  152. Icf::Sections_reachable_info* secvec = NULL;
  153. Icf::Symbol_info* symvec = NULL;
  154. Icf::Addend_info* addendvec = NULL;
  155. Icf::Offset_info* offsetvec = NULL;
  156. Icf::Reloc_addend_size_info* reloc_addend_size_vec = NULL;
  157. bool is_icf_tracked = false;
  158. const char* cident_section_name = NULL;
  159. std::string src_section_name = (parameters->options().icf_enabled()
  160. ? src_obj->section_name(src_indx)
  161. : "");
  162. bool check_section_for_function_pointers = false;
  163. if (parameters->options().icf_enabled()
  164. && (is_section_foldable_candidate(src_section_name)
  165. || is_prefix_of(".eh_frame", src_section_name.c_str())))
  166. {
  167. is_icf_tracked = true;
  168. Section_id src_id(src_obj, src_indx);
  169. Icf::Reloc_info* reloc_info =
  170. &symtab->icf()->reloc_info_list()[src_id];
  171. secvec = &reloc_info->section_info;
  172. symvec = &reloc_info->symbol_info;
  173. addendvec = &reloc_info->addend_info;
  174. offsetvec = &reloc_info->offset_info;
  175. reloc_addend_size_vec = &reloc_info->reloc_addend_size_info;
  176. }
  177. check_section_for_function_pointers =
  178. symtab->icf()->check_section_for_function_pointers(src_section_name,
  179. target);
  180. for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
  181. {
  182. Reltype reloc(prelocs);
  183. unsigned int r_sym = Classify_reloc::get_r_sym(&reloc);
  184. unsigned int r_type = Classify_reloc::get_r_type(&reloc);
  185. typename elfcpp::Elf_types<size>::Elf_Swxword addend =
  186. Classify_reloc::get_r_addend(&reloc);
  187. Relobj* dst_obj;
  188. unsigned int dst_indx;
  189. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  190. Address dst_off;
  191. if (r_sym < local_count)
  192. {
  193. gold_assert(plocal_syms != NULL);
  194. typename elfcpp::Sym<size, big_endian> lsym(plocal_syms
  195. + r_sym * sym_size);
  196. dst_indx = lsym.get_st_shndx();
  197. bool is_ordinary;
  198. dst_indx = src_obj->adjust_sym_shndx(r_sym, dst_indx, &is_ordinary);
  199. dst_obj = src_obj;
  200. dst_off = lsym.get_st_value() + addend;
  201. if (is_icf_tracked)
  202. {
  203. Address symvalue = dst_off - addend;
  204. if (is_ordinary)
  205. (*secvec).push_back(Section_id(src_obj, dst_indx));
  206. else
  207. (*secvec).push_back(Section_id(static_cast<Relobj*>(NULL), 0));
  208. // If the target of the relocation is an STT_SECTION symbol,
  209. // make a note of that by storing -1 in the symbol vector.
  210. if (lsym.get_st_type() == elfcpp::STT_SECTION)
  211. (*symvec).push_back(reinterpret_cast<Symbol*>(-1));
  212. else
  213. (*symvec).push_back(NULL);
  214. (*addendvec).push_back(std::make_pair(
  215. static_cast<long long>(symvalue),
  216. static_cast<long long>(addend)));
  217. uint64_t reloc_offset =
  218. convert_to_section_size_type(reloc.get_r_offset());
  219. (*offsetvec).push_back(reloc_offset);
  220. (*reloc_addend_size_vec).push_back(
  221. get_embedded_addend_size<Classify_reloc>(r_type, src_obj));
  222. }
  223. // When doing safe folding, check to see if this relocation is that
  224. // of a function pointer being taken.
  225. if (is_ordinary
  226. && check_section_for_function_pointers
  227. && lsym.get_st_type() != elfcpp::STT_OBJECT
  228. && scan.local_reloc_may_be_function_pointer(symtab, NULL, target,
  229. src_obj, src_indx,
  230. NULL, reloc, r_type,
  231. lsym))
  232. symtab->icf()->set_section_has_function_pointers(
  233. src_obj, lsym.get_st_shndx());
  234. if (!is_ordinary || dst_indx == src_indx)
  235. continue;
  236. }
  237. else
  238. {
  239. Symbol* gsym = src_obj->global_symbol(r_sym);
  240. gold_assert(gsym != NULL);
  241. if (gsym->is_forwarder())
  242. gsym = symtab->resolve_forwards(gsym);
  243. dst_obj = NULL;
  244. dst_indx = 0;
  245. bool is_ordinary = false;
  246. if (gsym->source() == Symbol::FROM_OBJECT
  247. && !gsym->object()->is_dynamic())
  248. {
  249. dst_obj = static_cast<Relobj*>(gsym->object());
  250. dst_indx = gsym->shndx(&is_ordinary);
  251. }
  252. dst_off = static_cast<const Sized_symbol<size>*>(gsym)->value();
  253. dst_off += addend;
  254. // When doing safe folding, check to see if this relocation is that
  255. // of a function pointer being taken.
  256. if (gsym->source() == Symbol::FROM_OBJECT
  257. && gsym->type() == elfcpp::STT_FUNC
  258. && check_section_for_function_pointers
  259. && dst_obj != NULL
  260. && (!is_ordinary
  261. || scan.global_reloc_may_be_function_pointer(
  262. symtab, NULL, target, src_obj, src_indx, NULL, reloc,
  263. r_type, gsym)))
  264. symtab->icf()->set_section_has_function_pointers(dst_obj, dst_indx);
  265. // If the symbol name matches '__start_XXX' then the section with
  266. // the C identifier like name 'XXX' should not be garbage collected.
  267. // A similar treatment to symbols with the name '__stop_XXX'.
  268. if (is_prefix_of(cident_section_start_prefix, gsym->name()))
  269. {
  270. cident_section_name = (gsym->name()
  271. + strlen(cident_section_start_prefix));
  272. }
  273. else if (is_prefix_of(cident_section_stop_prefix, gsym->name()))
  274. {
  275. cident_section_name = (gsym->name()
  276. + strlen(cident_section_stop_prefix));
  277. }
  278. if (is_icf_tracked)
  279. {
  280. Address symvalue = dst_off - addend;
  281. if (is_ordinary && dst_obj != NULL)
  282. (*secvec).push_back(Section_id(dst_obj, dst_indx));
  283. else
  284. (*secvec).push_back(Section_id(static_cast<Relobj*>(NULL), 0));
  285. (*symvec).push_back(gsym);
  286. (*addendvec).push_back(std::make_pair(
  287. static_cast<long long>(symvalue),
  288. static_cast<long long>(addend)));
  289. uint64_t reloc_offset =
  290. convert_to_section_size_type(reloc.get_r_offset());
  291. (*offsetvec).push_back(reloc_offset);
  292. (*reloc_addend_size_vec).push_back(
  293. get_embedded_addend_size<Classify_reloc>(r_type, src_obj));
  294. }
  295. if (dst_obj == NULL)
  296. continue;
  297. if (!is_ordinary)
  298. continue;
  299. }
  300. if (parameters->options().gc_sections())
  301. {
  302. symtab->gc()->add_reference(src_obj, src_indx, dst_obj, dst_indx);
  303. parameters->sized_target<size, big_endian>()
  304. ->gc_add_reference(symtab, src_obj, src_indx, dst_obj, dst_indx,
  305. dst_off);
  306. if (cident_section_name != NULL)
  307. {
  308. Garbage_collection::Cident_section_map::iterator ele =
  309. symtab->gc()->cident_sections()->find(std::string(cident_section_name));
  310. if (ele == symtab->gc()->cident_sections()->end())
  311. continue;
  312. Section_id src_id(src_obj, src_indx);
  313. Garbage_collection::Sections_reachable&
  314. v(symtab->gc()->section_reloc_map()[src_id]);
  315. Garbage_collection::Sections_reachable& cident_secn(ele->second);
  316. for (Garbage_collection::Sections_reachable::iterator it_v
  317. = cident_secn.begin();
  318. it_v != cident_secn.end();
  319. ++it_v)
  320. {
  321. v.insert(*it_v);
  322. }
  323. }
  324. }
  325. }
  326. return;
  327. }
  328. } // End of namespace gold.
  329. #endif