llvm-objcopy-4.patch 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. From 526aa2e94355b7feb3bf7774a6e1899f68e94ad8 Mon Sep 17 00:00:00 2001
  2. From: Martin Storsjo <martin@martin.st>
  3. Date: Sat, 19 Jan 2019 19:42:48 +0000
  4. Subject: [PATCH] [llvm-objcopy] [COFF] Implement --only-keep-debug
  5. Differential Revision: https://reviews.llvm.org/D56840
  6. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@351662 91177308-0d34-0410-b5e6-96231b3b80d8
  7. ---
  8. .../COFF/Inputs/only-keep-sections.yaml | 77 +++++++++++++++++++
  9. .../llvm-objcopy/COFF/only-keep-debug.test | 58 ++++++++++++++
  10. tools/llvm-objcopy/COFF/COFFObjcopy.cpp | 10 +++
  11. tools/llvm-objcopy/COFF/Object.cpp | 10 +++
  12. tools/llvm-objcopy/COFF/Object.h | 1 +
  13. 5 files changed, 156 insertions(+)
  14. create mode 100644 test/tools/llvm-objcopy/COFF/Inputs/only-keep-sections.yaml
  15. create mode 100644 test/tools/llvm-objcopy/COFF/only-keep-debug.test
  16. diff --git a/llvm/test/tools/llvm-objcopy/COFF/Inputs/only-keep-sections.yaml b/llvm/test/tools/llvm-objcopy/COFF/Inputs/only-keep-sections.yaml
  17. new file mode 100644
  18. index 00000000000..b5437e10763
  19. --- /dev/null
  20. +++ b/llvm/test/tools/llvm-objcopy/COFF/Inputs/only-keep-sections.yaml
  21. @@ -0,0 +1,77 @@
  22. +--- !COFF
  23. +OptionalHeader:
  24. + AddressOfEntryPoint: 4144
  25. + ImageBase: 1073741824
  26. + SectionAlignment: 4096
  27. + FileAlignment: 512
  28. + MajorOperatingSystemVersion: 6
  29. + MinorOperatingSystemVersion: 0
  30. + MajorImageVersion: 0
  31. + MinorImageVersion: 0
  32. + MajorSubsystemVersion: 6
  33. + MinorSubsystemVersion: 0
  34. + Subsystem: IMAGE_SUBSYSTEM_WINDOWS_CUI
  35. + DLLCharacteristics: [ ]
  36. + SizeOfStackReserve: 1048576
  37. + SizeOfStackCommit: 4096
  38. + SizeOfHeapReserve: 1048576
  39. + SizeOfHeapCommit: 4096
  40. +header:
  41. + Machine: IMAGE_FILE_MACHINE_AMD64
  42. + Characteristics: [ ]
  43. +sections:
  44. + - Name: .text
  45. + Characteristics: [ IMAGE_SCN_CNT_CODE ]
  46. + VirtualAddress: 4096
  47. + VirtualSize: 4
  48. + SectionData: C3C3C3C3
  49. + - Name: .rdata
  50. + Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA ]
  51. + VirtualAddress: 8192
  52. + VirtualSize: 4
  53. + SectionData: 2A000000
  54. + - Name: .buildid
  55. + Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA ]
  56. + VirtualAddress: 12288
  57. + VirtualSize: 4
  58. + SectionData: 2B000000
  59. + - Name: .reloc
  60. + Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE ]
  61. + VirtualAddress: 16384
  62. + VirtualSize: 4
  63. + SectionData: 2C000000
  64. + - Name: .debug_discardable
  65. + Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE ]
  66. + VirtualAddress: 20480
  67. + VirtualSize: 4
  68. + SectionData: 2D000000
  69. + - Name: .debug_undiscardable
  70. + Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA ]
  71. + VirtualAddress: 24576
  72. + VirtualSize: 4
  73. + SectionData: 2E000000
  74. + - Name: .unflagged
  75. + Characteristics: [ ]
  76. + VirtualAddress: 28672
  77. + VirtualSize: 4
  78. + SectionData: 2F000000
  79. +symbols:
  80. + - Name: main
  81. + Value: 2
  82. + SectionNumber: 1
  83. + SimpleType: IMAGE_SYM_TYPE_NULL
  84. + ComplexType: IMAGE_SYM_DTYPE_FUNCTION
  85. + StorageClass: IMAGE_SYM_CLASS_EXTERNAL
  86. + - Name: debug_discardable_sym
  87. + Value: 0
  88. + SectionNumber: 5
  89. + SimpleType: IMAGE_SYM_TYPE_NULL
  90. + ComplexType: IMAGE_SYM_DTYPE_FUNCTION
  91. + StorageClass: IMAGE_SYM_CLASS_EXTERNAL
  92. + - Name: debug_undiscardable_sym
  93. + Value: 0
  94. + SectionNumber: 6
  95. + SimpleType: IMAGE_SYM_TYPE_NULL
  96. + ComplexType: IMAGE_SYM_DTYPE_FUNCTION
  97. + StorageClass: IMAGE_SYM_CLASS_EXTERNAL
  98. +...
  99. diff --git a/llvm/test/tools/llvm-objcopy/COFF/only-keep-debug.test b/llvm/test/tools/llvm-objcopy/COFF/only-keep-debug.test
  100. new file mode 100644
  101. index 00000000000..5518d4000fc
  102. --- /dev/null
  103. +++ b/llvm/test/tools/llvm-objcopy/COFF/only-keep-debug.test
  104. @@ -0,0 +1,58 @@
  105. +RUN: yaml2obj %p/Inputs/only-keep-sections.yaml > %t.in.exe
  106. +
  107. +RUN: llvm-objcopy --only-keep-debug %t.in.exe %t.out.exe
  108. +RUN: llvm-readobj --sections %t.out.exe | FileCheck %s --check-prefix=SECTIONS
  109. +RUN: llvm-objdump -t %t.out.exe | FileCheck %s --check-prefix=SYMBOLS
  110. +
  111. +Check that all non-debug/buildid sections with IMAGE_SCN_CNT_CODE
  112. +or IMAGE_SCN_CNT_INITIALIZED_DATA are truncated, and no others.
  113. +
  114. +SECTIONS: Sections [
  115. +SECTIONS-NEXT: Section {
  116. +SECTIONS-NEXT: Number: 1
  117. +SECTIONS-NEXT: Name: .text
  118. +SECTIONS-NEXT: VirtualSize: 0x4
  119. +SECTIONS-NEXT: VirtualAddress:
  120. +SECTIONS-NEXT: RawDataSize: 0
  121. +SECTIONS: Section {
  122. +SECTIONS-NEXT: Number: 2
  123. +SECTIONS-NEXT: Name: .rdata
  124. +SECTIONS-NEXT: VirtualSize: 0x4
  125. +SECTIONS-NEXT: VirtualAddress:
  126. +SECTIONS-NEXT: RawDataSize: 0
  127. +SECTIONS: Section {
  128. +SECTIONS-NEXT: Number: 3
  129. +SECTIONS-NEXT: Name: .buildid
  130. +SECTIONS-NEXT: VirtualSize: 0x4
  131. +SECTIONS-NEXT: VirtualAddress:
  132. +SECTIONS-NEXT: RawDataSize: 512
  133. +SECTIONS: Section {
  134. +SECTIONS-NEXT: Number: 4
  135. +SECTIONS-NEXT: Name: .reloc
  136. +SECTIONS-NEXT: VirtualSize: 0x4
  137. +SECTIONS-NEXT: VirtualAddress:
  138. +SECTIONS-NEXT: RawDataSize: 0
  139. +SECTIONS: Section {
  140. +SECTIONS-NEXT: Number: 5
  141. +SECTIONS-NEXT: Name: .debug_discardable
  142. +SECTIONS-NEXT: VirtualSize: 0x4
  143. +SECTIONS-NEXT: VirtualAddress:
  144. +SECTIONS-NEXT: RawDataSize: 512
  145. +SECTIONS: Section {
  146. +SECTIONS-NEXT: Number: 6
  147. +SECTIONS-NEXT: Name: .debug_undiscardable
  148. +SECTIONS-NEXT: VirtualSize: 0x4
  149. +SECTIONS-NEXT: VirtualAddress:
  150. +SECTIONS-NEXT: RawDataSize: 512
  151. +SECTIONS: Section {
  152. +SECTIONS-NEXT: Number: 7
  153. +SECTIONS-NEXT: Name: .unflagged
  154. +SECTIONS-NEXT: VirtualSize: 0x4
  155. +SECTIONS-NEXT: VirtualAddress:
  156. +SECTIONS-NEXT: RawDataSize: 512
  157. +
  158. +SYMBOLS: SYMBOL TABLE:
  159. +SYMBOLS-NEXT: main
  160. +SYMBOLS-NEXT: debug_discardable_sym
  161. +SYMBOLS-NEXT: debug_undiscardable_sym
  162. +SYMBOLS-EMPTY:
  163. diff --git a/llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp b/llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp
  164. index 13d8efde37c..60afbf7bb54 100644
  165. --- a/llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp
  166. +++ b/llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp
  167. @@ -46,6 +46,16 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj) {
  168. return false;
  169. });
  170. + if (Config.OnlyKeepDebug) {
  171. + // For --only-keep-debug, we keep all other sections, but remove their
  172. + // content. The VirtualSize field in the section header is kept intact.
  173. + Obj.truncateSections([](const Section &Sec) {
  174. + return !isDebugSection(Sec) && Sec.Name != ".buildid" &&
  175. + ((Sec.Header.Characteristics &
  176. + (IMAGE_SCN_CNT_CODE | IMAGE_SCN_CNT_INITIALIZED_DATA)) != 0);
  177. + });
  178. + }
  179. +
  180. // StripAll removes all symbols and thus also removes all relocations.
  181. if (Config.StripAll || Config.StripAllGNU)
  182. for (Section &Sec : Obj.getMutableSections())
  183. diff --git a/llvm/tools/llvm-objcopy/COFF/Object.cpp b/llvm/tools/llvm-objcopy/COFF/Object.cpp
  184. index e19cea6aa9d..fc87d9e574d 100644
  185. --- a/llvm/tools/llvm-objcopy/COFF/Object.cpp
  186. +++ b/llvm/tools/llvm-objcopy/COFF/Object.cpp
  187. @@ -127,6 +127,16 @@ void Object::removeSections(function_ref<bool(const Section &)> ToRemove) {
  188. updateSymbols();
  189. }
  190. +void Object::truncateSections(function_ref<bool(const Section &)> ToTruncate) {
  191. + for (Section &Sec : Sections) {
  192. + if (ToTruncate(Sec)) {
  193. + Sec.Contents = ArrayRef<uint8_t>();
  194. + Sec.Relocs.clear();
  195. + Sec.Header.SizeOfRawData = 0;
  196. + }
  197. + }
  198. +}
  199. +
  200. } // end namespace coff
  201. } // end namespace objcopy
  202. } // end namespace llvm
  203. diff --git a/llvm/tools/llvm-objcopy/COFF/Object.h b/llvm/tools/llvm-objcopy/COFF/Object.h
  204. index a73e93620d3..8e200369f0b 100644
  205. --- a/llvm/tools/llvm-objcopy/COFF/Object.h
  206. +++ b/llvm/tools/llvm-objcopy/COFF/Object.h
  207. @@ -93,6 +93,7 @@ struct Object {
  208. void addSections(ArrayRef<Section> NewSections);
  209. void removeSections(function_ref<bool(const Section &)> ToRemove);
  210. + void truncateSections(function_ref<bool(const Section &)> ToTruncate);
  211. private:
  212. std::vector<Symbol> Symbols;
  213. --
  214. 2.17.1