plugin_section_alignment.cc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // plugin_section_alignment.cc -- plugins to test ordering with {size,alignment}
  2. //
  3. // Copyright (C) 2016-2022 Free Software Foundation, Inc.
  4. // Written by Than McIntosh <thanm@google.com>.
  5. //
  6. // This file is part of gold.
  7. //
  8. // This program is free software; you can redistribute it and/or modify
  9. // it under the terms of the GNU General Public License as published by
  10. // the Free Software Foundation; either version 3 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU General Public License
  19. // along with this program; if not, write to the Free Software
  20. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  21. // MA 02110-1301, USA.
  22. //
  23. #ifdef HAVE_CONFIG_H
  24. #include "config.h"
  25. #endif
  26. #include <assert.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <string>
  31. #include <vector>
  32. #include <algorithm>
  33. #include "plugin-api.h"
  34. static ld_plugin_get_input_section_count get_input_section_count = NULL;
  35. static ld_plugin_get_input_section_alignment get_input_section_alignment = NULL;
  36. static ld_plugin_get_input_section_size get_input_section_size = NULL;
  37. static ld_plugin_get_input_section_name get_input_section_name = NULL;
  38. static ld_plugin_update_section_order update_section_order = NULL;
  39. static ld_plugin_allow_section_ordering allow_section_ordering = NULL;
  40. extern "C" {
  41. enum ld_plugin_status onload(struct ld_plugin_tv *tv);
  42. enum ld_plugin_status claim_file_hook(const struct ld_plugin_input_file *file,
  43. int *claimed);
  44. enum ld_plugin_status all_symbols_read_hook(void);
  45. }
  46. typedef enum { FL_BSS, FL_DATA, FL_RODATA, FL_UNKNOWN } sec_flavor;
  47. inline static int is_prefix_of(const char *prefix, const char *str)
  48. {
  49. return strncmp(prefix, str, strlen(prefix)) == 0;
  50. }
  51. inline static sec_flavor flavor_from_name(const char *secname)
  52. {
  53. if (is_prefix_of(".data.v", secname)) {
  54. return FL_DATA;
  55. } else if (is_prefix_of(".bss.v", secname)) {
  56. return FL_BSS;
  57. } else if (is_prefix_of(".rodata.v", secname)) {
  58. return FL_RODATA;
  59. } else {
  60. return FL_UNKNOWN;
  61. }
  62. }
  63. struct SectionInfo {
  64. ld_plugin_section plugin_section;
  65. std::string name;
  66. uint64_t size;
  67. sec_flavor flavor;
  68. unsigned align;
  69. static bool SectionInfoLt(const SectionInfo &i1,
  70. const SectionInfo &i2)
  71. {
  72. if (i1.flavor != i2.flavor) {
  73. return ((unsigned) i1.flavor) < ((unsigned) i2.flavor);
  74. }
  75. switch (i1.flavor) {
  76. case FL_DATA:
  77. // Sort data items by increasing alignment then increasing size
  78. if (i1.align != i2.align) {
  79. return ((unsigned) i1.align) < ((unsigned) i2.align);
  80. }
  81. if (i1.size != i2.size) {
  82. return ((unsigned) i1.size) < ((unsigned) i2.size);
  83. }
  84. break;
  85. case FL_BSS:
  86. // Sort bss items by decreasing alignment then decreasing size
  87. if (i1.align != i2.align) {
  88. return ((unsigned) i2.align) < ((unsigned) i1.align);
  89. }
  90. if (i1.size != i2.size) {
  91. return ((unsigned) i2.size) < ((unsigned) i1.size);
  92. }
  93. break;
  94. case FL_RODATA:
  95. // Sort rodata items by decreasing alignment then increasing size
  96. if (i1.align != i2.align) {
  97. return ((unsigned) i2.align) < ((unsigned) i1.align);
  98. }
  99. if (i1.size != i2.size) {
  100. return ((unsigned) i1.size) < ((unsigned) i2.size);
  101. }
  102. default:
  103. break;
  104. }
  105. // Break ties by name
  106. return i1.name.compare(i2.name) < 0;
  107. }
  108. };
  109. typedef std::vector<SectionInfo> section_info_vector;
  110. section_info_vector raw_sections;
  111. // Set to 1 for debugging output
  112. unsigned trace = 0;
  113. extern "C" {
  114. // Plugin entry point.
  115. enum ld_plugin_status onload(struct ld_plugin_tv *tv)
  116. {
  117. struct ld_plugin_tv *entry;
  118. for (entry = tv; entry->tv_tag != LDPT_NULL; ++entry)
  119. {
  120. switch (entry->tv_tag)
  121. {
  122. case LDPT_OPTION:
  123. if (!strcmp(entry->tv_u.tv_string,"-trace")) {
  124. fprintf(stderr, "enabling tracing\n");
  125. trace += 1;
  126. } else {
  127. fprintf(stderr, "unknown plugin option %s", entry->tv_u.tv_string);
  128. }
  129. break;
  130. case LDPT_REGISTER_CLAIM_FILE_HOOK:
  131. assert((*entry->tv_u.tv_register_claim_file)(claim_file_hook) ==
  132. LDPS_OK);
  133. break;
  134. case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK:
  135. assert((*entry->tv_u.tv_register_all_symbols_read)(
  136. all_symbols_read_hook) == LDPS_OK);
  137. break;
  138. case LDPT_GET_INPUT_SECTION_COUNT:
  139. get_input_section_count = *entry->tv_u.tv_get_input_section_count;
  140. break;
  141. case LDPT_GET_INPUT_SECTION_NAME:
  142. get_input_section_name = *entry->tv_u.tv_get_input_section_name;
  143. break;
  144. case LDPT_GET_INPUT_SECTION_ALIGNMENT:
  145. get_input_section_alignment =
  146. *entry->tv_u.tv_get_input_section_alignment;
  147. break;
  148. case LDPT_GET_INPUT_SECTION_SIZE:
  149. get_input_section_size = *entry->tv_u.tv_get_input_section_size;
  150. break;
  151. case LDPT_UPDATE_SECTION_ORDER:
  152. update_section_order = *entry->tv_u.tv_update_section_order;
  153. break;
  154. case LDPT_ALLOW_SECTION_ORDERING:
  155. allow_section_ordering = *entry->tv_u.tv_allow_section_ordering;
  156. break;
  157. default:
  158. break;
  159. }
  160. }
  161. if (get_input_section_count == NULL || get_input_section_alignment == NULL ||
  162. get_input_section_size == NULL || get_input_section_name == NULL ||
  163. update_section_order == NULL || allow_section_ordering == NULL) {
  164. fprintf(stderr, "Some interfaces are missing\n");
  165. return LDPS_ERR;
  166. }
  167. return LDPS_OK;
  168. }
  169. // This function is called by the linker for every new object it encounters.
  170. enum ld_plugin_status claim_file_hook(const struct ld_plugin_input_file *file,
  171. int *claimed)
  172. {
  173. static bool is_ordering_specified = false;
  174. struct ld_plugin_section section;
  175. unsigned count = 0;
  176. unsigned shndx;
  177. *claimed = 0;
  178. if (!is_ordering_specified) {
  179. // Inform the linker to prepare for section reordering.
  180. (*allow_section_ordering)();
  181. is_ordering_specified = true;
  182. }
  183. (*get_input_section_count)(file->handle, &count);
  184. for (shndx = 0; shndx < count; ++shndx) {
  185. char *name = NULL;
  186. section.handle = file->handle;
  187. section.shndx = shndx;
  188. (*get_input_section_name)(section, &name);
  189. if (is_prefix_of(".data.v", name) ||
  190. is_prefix_of(".bss.v", name) ||
  191. is_prefix_of(".rodata.v", name)) {
  192. SectionInfo si;
  193. si.plugin_section.handle = file->handle;
  194. si.plugin_section.shndx = shndx;
  195. (*get_input_section_size)(section, &si.size);
  196. (*get_input_section_alignment)(section, &si.align);
  197. si.name = name;
  198. si.flavor = flavor_from_name(name);
  199. raw_sections.push_back(si);
  200. }
  201. }
  202. return LDPS_OK;
  203. }
  204. // This function is called by the linker after all the symbols have been read.
  205. // At this stage, it is fine to tell the linker the desired function order.
  206. enum ld_plugin_status all_symbols_read_hook(void)
  207. {
  208. // We expect to see a total of twelve sections -- if this is not the case
  209. // then something went wrong somewhere along the way.
  210. if (raw_sections.size() != 12) {
  211. fprintf(stderr, "Expected 12 sections, found %u sections",
  212. (unsigned) raw_sections.size());
  213. return LDPS_ERR;
  214. }
  215. std::sort(raw_sections.begin(), raw_sections.end(),
  216. SectionInfo::SectionInfoLt);
  217. struct ld_plugin_section section_list[12];
  218. for (unsigned ii = 0; ii < 12; ++ii) {
  219. section_list[ii] = raw_sections[ii].plugin_section;
  220. }
  221. if (trace) {
  222. fprintf(stderr, "Specified section order is:\n");
  223. for (section_info_vector::iterator it = raw_sections.begin();
  224. it != raw_sections.end();
  225. ++it) {
  226. const SectionInfo &si = (*it);
  227. fprintf(stderr, "Idx=%u Name=%s Align=%u Size=%u\n",
  228. si.plugin_section.shndx, si.name.c_str(), si.align,
  229. (unsigned) si.size);
  230. }
  231. }
  232. update_section_order(section_list, 12);
  233. return LDPS_OK;
  234. }
  235. } // end extern "C"