enable-reloc-section-ld.patch 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. From fba503a78c50d6134943245d55e820f53e8f19cd Mon Sep 17 00:00:00 2001
  2. From: Erinn Clark <erinn@torproject.org>
  3. Date: Fri, 8 Aug 2014 14:23:44 -0400
  4. Subject: [PATCH] add relocation section so Windows bundles can have ASLR
  5. Patch by skruffy.
  6. ---
  7. ld/emultempl/pe.em | 7 ++++++
  8. ld/emultempl/pep.em | 11 ++++++++--
  9. ld/pe-dll.c | 63 ++++++++++++++++++++++++++++++-----------------------
  10. ld/pe-dll.h | 1 +
  11. 4 files changed, 53 insertions(+), 29 deletions(-)
  12. diff --git a/ld/emultempl/pe.em b/ld/emultempl/pe.em
  13. index 339b7c5..3958b81 100644
  14. --- a/ld/emultempl/pe.em
  15. +++ b/ld/emultempl/pe.em
  16. @@ -272,6 +272,7 @@ fragment <<EOF
  17. #define OPTION_INSERT_TIMESTAMP (OPTION_TERMINAL_SERVER_AWARE + 1)
  18. #define OPTION_NO_INSERT_TIMESTAMP (OPTION_INSERT_TIMESTAMP + 1)
  19. #define OPTION_BUILD_ID (OPTION_NO_INSERT_TIMESTAMP + 1)
  20. +#define OPTION_ENABLE_RELOC_SECTION (OPTION_BUILD_ID + 1)
  21. static void
  22. gld${EMULATION_NAME}_add_options
  23. @@ -315,6 +316,7 @@ gld${EMULATION_NAME}_add_options
  24. {"export-all-symbols", no_argument, NULL, OPTION_EXPORT_ALL},
  25. {"exclude-symbols", required_argument, NULL, OPTION_EXCLUDE_SYMBOLS},
  26. {"exclude-all-symbols", no_argument, NULL, OPTION_EXCLUDE_ALL_SYMBOLS},
  27. + {"enable-reloc-section", no_argument, NULL, OPTION_ENABLE_RELOC_SECTION},
  28. {"exclude-libs", required_argument, NULL, OPTION_EXCLUDE_LIBS},
  29. {"exclude-modules-for-implib", required_argument, NULL, OPTION_EXCLUDE_MODULES_FOR_IMPLIB},
  30. {"kill-at", no_argument, NULL, OPTION_KILL_ATS},
  31. @@ -782,6 +784,9 @@ gld${EMULATION_NAME}_handle_option (int optc)
  32. case OPTION_EXCLUDE_ALL_SYMBOLS:
  33. pe_dll_exclude_all_symbols = 1;
  34. break;
  35. + case OPTION_ENABLE_RELOC_SECTION:
  36. + pe_dll_enable_reloc_section = 1;
  37. + break;
  38. case OPTION_EXCLUDE_LIBS:
  39. pe_dll_add_excludes (optarg, EXCLUDELIBS);
  40. break;
  41. @@ -2076,6 +2081,8 @@ gld_${EMULATION_NAME}_finish (void)
  42. #if !defined(TARGET_IS_shpe)
  43. || (!bfd_link_relocatable (&link_info)
  44. && pe_def_file->num_exports != 0)
  45. + || (!bfd_link_relocatable (&link_info)
  46. + && pe_dll_enable_reloc_section)
  47. #endif
  48. )
  49. {
  50. diff --git a/ld/emultempl/pep.em b/ld/emultempl/pep.em
  51. index c253d2f..fccbd63 100644
  52. --- a/ld/emultempl/pep.em
  53. +++ b/ld/emultempl/pep.em
  54. @@ -246,7 +246,8 @@ enum options
  55. OPTION_INSERT_TIMESTAMP,
  56. OPTION_NO_INSERT_TIMESTAMP,
  57. OPTION_TERMINAL_SERVER_AWARE,
  58. - OPTION_BUILD_ID
  59. + OPTION_BUILD_ID,
  60. + OPTION_ENABLE_RELOC_SECTION
  61. };
  62. static void
  63. @@ -288,6 +289,7 @@ gld${EMULATION_NAME}_add_options
  64. {"export-all-symbols", no_argument, NULL, OPTION_EXPORT_ALL},
  65. {"exclude-symbols", required_argument, NULL, OPTION_EXCLUDE_SYMBOLS},
  66. {"exclude-all-symbols", no_argument, NULL, OPTION_EXCLUDE_ALL_SYMBOLS},
  67. + {"enable-reloc-section", no_argument, NULL, OPTION_ENABLE_RELOC_SECTION},
  68. {"exclude-libs", required_argument, NULL, OPTION_EXCLUDE_LIBS},
  69. {"exclude-modules-for-implib", required_argument, NULL, OPTION_EXCLUDE_MODULES_FOR_IMPLIB},
  70. {"kill-at", no_argument, NULL, OPTION_KILL_ATS},
  71. @@ -739,6 +741,9 @@ gld${EMULATION_NAME}_handle_option (int optc)
  72. case OPTION_EXCLUDE_ALL_SYMBOLS:
  73. pep_dll_exclude_all_symbols = 1;
  74. break;
  75. + case OPTION_ENABLE_RELOC_SECTION:
  76. + pe_dll_enable_reloc_section = 1;
  77. + break;
  78. case OPTION_EXCLUDE_LIBS:
  79. pep_dll_add_excludes (optarg, EXCLUDELIBS);
  80. break;
  81. @@ -1857,7 +1862,9 @@ gld_${EMULATION_NAME}_finish (void)
  82. #ifdef DLL_SUPPORT
  83. if (bfd_link_pic (&link_info)
  84. || (!bfd_link_relocatable (&link_info)
  85. - && pep_def_file->num_exports != 0))
  86. + && pe_dll_enable_reloc_section)
  87. + || (!bfd_link_relocatable (&link_info)
  88. + && pep_def_file->num_exports != 0))
  89. {
  90. pep_dll_fill_sections (link_info.output_bfd, &link_info);
  91. if (command_line.out_implib_filename)
  92. diff --git a/ld/pe-dll.c b/ld/pe-dll.c
  93. index c398f23..3797f1a 100644
  94. --- a/ld/pe-dll.c
  95. +++ b/ld/pe-dll.c
  96. @@ -151,6 +151,7 @@ def_file * pe_def_file = 0;
  97. int pe_dll_export_everything = 0;
  98. int pe_dll_exclude_all_symbols = 0;
  99. int pe_dll_do_default_excludes = 1;
  100. +int pe_dll_enable_reloc_section = 0;
  101. int pe_dll_kill_ats = 0;
  102. int pe_dll_stdcall_aliases = 0;
  103. int pe_dll_warn_dup_exports = 0;
  104. @@ -3430,8 +3431,15 @@ pe_dll_build_sections (bfd *abfd, struct bfd_link_info *info)
  105. process_def_file_and_drectve (abfd, info);
  106. if (pe_def_file->num_exports == 0 && !bfd_link_pic (info))
  107. - return;
  108. -
  109. + {
  110. + if (pe_dll_enable_reloc_section)
  111. + {
  112. + build_filler_bfd (0);
  113. + pe_output_file_set_long_section_names (filler_bfd);
  114. + }
  115. + return;
  116. + }
  117. +
  118. generate_edata (abfd, info);
  119. build_filler_bfd (1);
  120. pe_output_file_set_long_section_names (filler_bfd);
  121. @@ -3446,13 +3454,9 @@ pe_exe_build_sections (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
  122. pe_output_file_set_long_section_names (filler_bfd);
  123. }
  124. -void
  125. -pe_dll_fill_sections (bfd *abfd, struct bfd_link_info *info)
  126. +static void
  127. +pe_dll_create_reloc (bfd *abfd, struct bfd_link_info *info)
  128. {
  129. - pe_dll_id_target (bfd_get_target (abfd));
  130. - pe_output_file_set_long_section_names (abfd);
  131. - image_base = pe_data (abfd)->pe_opthdr.ImageBase;
  132. -
  133. generate_reloc (abfd, info);
  134. if (reloc_sz > 0)
  135. {
  136. @@ -3469,38 +3473,43 @@ pe_dll_fill_sections (bfd *abfd, struct bfd_link_info *info)
  137. lang_do_assignments (lang_final_phase_enum);
  138. }
  139. - fill_edata (abfd, info);
  140. -
  141. - if (bfd_link_dll (info))
  142. - pe_data (abfd)->dll = 1;
  143. -
  144. - edata_s->contents = edata_d;
  145. reloc_s->contents = reloc_d;
  146. }
  147. void
  148. -pe_exe_fill_sections (bfd *abfd, struct bfd_link_info *info)
  149. +pe_dll_fill_sections (bfd *abfd, struct bfd_link_info *info)
  150. {
  151. + if (!reloc_s && !edata_s)
  152. + return;
  153. pe_dll_id_target (bfd_get_target (abfd));
  154. pe_output_file_set_long_section_names (abfd);
  155. image_base = pe_data (abfd)->pe_opthdr.ImageBase;
  156. - generate_reloc (abfd, info);
  157. - if (reloc_sz > 0)
  158. + if (reloc_s)
  159. + pe_dll_create_reloc (abfd, info);
  160. +
  161. + if (edata_s)
  162. {
  163. - bfd_set_section_size (filler_bfd, reloc_s, reloc_sz);
  164. + fill_edata (abfd, info);
  165. + edata_s->contents = edata_d;
  166. + }
  167. - /* Resize the sections. */
  168. - lang_reset_memory_regions ();
  169. - lang_size_sections (NULL, TRUE);
  170. + if (bfd_link_pic (info) && !bfd_link_pie (info))
  171. + pe_data (abfd)->dll = 1;
  172. - /* Redo special stuff. */
  173. - ldemul_after_allocation ();
  174. - /* Do the assignments again. */
  175. - lang_do_assignments (lang_final_phase_enum);
  176. - }
  177. - reloc_s->contents = reloc_d;
  178. +}
  179. +
  180. +void
  181. +pe_exe_fill_sections (bfd *abfd, struct bfd_link_info *info)
  182. +{
  183. + if (!reloc_s)
  184. + return;
  185. + pe_dll_id_target (bfd_get_target (abfd));
  186. + pe_output_file_set_long_section_names (abfd);
  187. + image_base = pe_data (abfd)->pe_opthdr.ImageBase;
  188. +
  189. + pe_dll_create_reloc (abfd, info);
  190. }
  191. bfd_boolean
  192. diff --git a/ld/pe-dll.h b/ld/pe-dll.h
  193. index 48d169b..05ff72b 100644
  194. --- a/ld/pe-dll.h
  195. +++ b/ld/pe-dll.h
  196. @@ -30,6 +30,7 @@ extern def_file *pe_def_file;
  197. extern int pe_dll_export_everything;
  198. extern int pe_dll_exclude_all_symbols;
  199. extern int pe_dll_do_default_excludes;
  200. +extern int pe_dll_enable_reloc_section;
  201. extern int pe_dll_kill_ats;
  202. extern int pe_dll_stdcall_aliases;
  203. extern int pe_dll_warn_dup_exports;
  204. --
  205. 2.1.4