llvm-objcopy-3.patch 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. From 2b6e1b7585d6bd997ea4e4233c904a6d2c11ad33 Mon Sep 17 00:00:00 2001
  2. From: Martin Storsjo <martin@martin.st>
  3. Date: Sat, 19 Jan 2019 19:42:41 +0000
  4. Subject: [PATCH] [llvm-objcopy] [COFF] Implement --strip-debug
  5. Also remove sections similarly for --strip-all, --discard-all,
  6. --strip-unneeded.
  7. Differential Revision: https://reviews.llvm.org/D56839
  8. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@351661 91177308-0d34-0410-b5e6-96231b3b80d8
  9. ---
  10. test/tools/llvm-objcopy/COFF/strip-debug.test | 109 ++++++++++++++++++
  11. tools/llvm-objcopy/COFF/COFFObjcopy.cpp | 11 ++
  12. 2 files changed, 120 insertions(+)
  13. create mode 100644 test/tools/llvm-objcopy/COFF/strip-debug.test
  14. diff --git a/llvm/test/tools/llvm-objcopy/COFF/strip-debug.test b/llvm/test/tools/llvm-objcopy/COFF/strip-debug.test
  15. new file mode 100644
  16. index 00000000000..97fa96aac70
  17. --- /dev/null
  18. +++ b/llvm/test/tools/llvm-objcopy/COFF/strip-debug.test
  19. @@ -0,0 +1,109 @@
  20. +# RUN: yaml2obj %s > %t.in.o
  21. +#
  22. +# RUN: llvm-objdump --section-headers %t.in.o | FileCheck %s --check-prefixes=SECTIONS,SECTIONS-PRE
  23. +# RUN: llvm-objdump -t %t.in.o | FileCheck %s --check-prefixes=SYMBOLS,SYMBOLS-PRE
  24. +#
  25. +# RUN: llvm-objcopy --strip-debug %t.in.o %t.out.o
  26. +# RUN: llvm-objdump --section-headers %t.out.o | FileCheck %s --check-prefixes=SECTIONS
  27. +# RUN: llvm-objdump -t %t.out.o | FileCheck %s --check-prefixes=SYMBOLS
  28. +#
  29. +# Test that --strip-all, --strip-all-gnu, --discard-all and --strip-unneeded,
  30. +# plus llvm-strip without arguments all produce a similiar set of sections
  31. +# (while they remove symbols differently).
  32. +#
  33. +# RUN: llvm-objcopy --strip-all %t.in.o %t.strip-all.o
  34. +# RUN: llvm-objdump --section-headers %t.strip-all.o | FileCheck %s --check-prefixes=SECTIONS
  35. +#
  36. +# RUN: llvm-objcopy --strip-all-gnu %t.in.o %t.strip-all-gnu.o
  37. +# RUN: llvm-objdump --section-headers %t.strip-all-gnu.o | FileCheck %s --check-prefixes=SECTIONS
  38. +#
  39. +# RUN: llvm-objcopy --discard-all %t.in.o %t.discard-all.o
  40. +# RUN: llvm-objdump --section-headers %t.discard-all.o | FileCheck %s --check-prefixes=SECTIONS
  41. +#
  42. +# RUN: llvm-objcopy --discard-all %t.in.o %t.strip-unneeded.o
  43. +# RUN: llvm-objdump --section-headers %t.strip-unneeded.o | FileCheck %s --check-prefixes=SECTIONS
  44. +#
  45. +# SECTIONS: Sections:
  46. +# SECTIONS-NEXT: Idx Name
  47. +# SECTIONS-NEXT: 0 .text
  48. +# SECTIONS-NEXT: 1 .data
  49. +# SECTIONS-NEXT: 2 .bss
  50. +# SECTIONS-NEXT: 3 .xdata
  51. +# SECTIONS-NEXT: 4 .reloc
  52. +# SECTIONS-PRE-NEXT: 5 .debug_discardable
  53. +# SECTIONS-NEXT: {{.*}} .debug_undiscardable
  54. +# SECTIONS-NEXT: {{.*}} .llvm_addrsig
  55. +# SECTIONS-EMPTY:
  56. +#
  57. +# Test that --strip-debug doesn't remove e.g. unreferenced local symbols.
  58. +#
  59. +# SYMBOLS: SYMBOL TABLE:
  60. +# SYMBOLS-NEXT: external
  61. +# SYMBOLS-NEXT: local_unreferenced
  62. +# SYMBOLS-PRE-NEXT: debug_discardable_sym
  63. +# SYMBOLS-NEXT: debug_undiscardable_sym
  64. +# SYMBOLS-EMPTY:
  65. +
  66. +--- !COFF
  67. +header:
  68. + Machine: IMAGE_FILE_MACHINE_AMD64
  69. + Characteristics: [ ]
  70. +sections:
  71. + - Name: .text
  72. + Characteristics: [ ]
  73. + Alignment: 4
  74. + SectionData: 00000000
  75. + - Name: .data
  76. + Characteristics: [ ]
  77. + Alignment: 4
  78. + SectionData: 00000000
  79. + - Name: .bss
  80. + Characteristics: [ ]
  81. + Alignment: 4
  82. + SectionData: 00000000
  83. + - Name: .xdata
  84. + Characteristics: [ ]
  85. + Alignment: 4
  86. + SectionData: 00000000
  87. + - Name: .reloc
  88. + Characteristics: [ IMAGE_SCN_MEM_DISCARDABLE ]
  89. + Alignment: 4
  90. + SectionData: 00000000
  91. + - Name: .debug_discardable
  92. + Characteristics: [ IMAGE_SCN_MEM_DISCARDABLE ]
  93. + Alignment: 4
  94. + SectionData: 00000000
  95. + - Name: .debug_undiscardable
  96. + Characteristics: [ ]
  97. + Alignment: 4
  98. + SectionData: 00000000
  99. + - Name: .llvm_addrsig
  100. + Characteristics: [ IMAGE_SCN_LNK_REMOVE ]
  101. + Alignment: 4
  102. + SectionData: 00000000
  103. +symbols:
  104. + - Name: external
  105. + Value: 0
  106. + SectionNumber: 1
  107. + SimpleType: IMAGE_SYM_TYPE_NULL
  108. + ComplexType: IMAGE_SYM_DTYPE_NULL
  109. + StorageClass: IMAGE_SYM_CLASS_EXTERNAL
  110. + - Name: local_unreferenced
  111. + Value: 0
  112. + SectionNumber: 1
  113. + SimpleType: IMAGE_SYM_TYPE_NULL
  114. + ComplexType: IMAGE_SYM_DTYPE_NULL
  115. + StorageClass: IMAGE_SYM_CLASS_STATIC
  116. + - Name: debug_discardable_sym
  117. + Value: 0
  118. + SectionNumber: 6
  119. + SimpleType: IMAGE_SYM_TYPE_NULL
  120. + ComplexType: IMAGE_SYM_DTYPE_NULL
  121. + StorageClass: IMAGE_SYM_CLASS_EXTERNAL
  122. + - Name: debug_undiscardable_sym
  123. + Value: 0
  124. + SectionNumber: 7
  125. + SimpleType: IMAGE_SYM_TYPE_NULL
  126. + ComplexType: IMAGE_SYM_DTYPE_NULL
  127. + StorageClass: IMAGE_SYM_CLASS_EXTERNAL
  128. +...
  129. diff --git a/llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp b/llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp
  130. index dd2e4829218..13d8efde37c 100644
  131. --- a/llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp
  132. +++ b/llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp
  133. @@ -26,9 +26,20 @@ namespace coff {
  134. using namespace object;
  135. using namespace COFF;
  136. +static bool isDebugSection(const Section &Sec) {
  137. + return Sec.Name.startswith(".debug");
  138. +}
  139. +
  140. static Error handleArgs(const CopyConfig &Config, Object &Obj) {
  141. // Perform the actual section removals.
  142. Obj.removeSections([&Config](const Section &Sec) {
  143. + if (Config.StripDebug || Config.StripAll || Config.StripAllGNU ||
  144. + Config.DiscardAll || Config.StripUnneeded) {
  145. + if (isDebugSection(Sec) &&
  146. + (Sec.Header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE) != 0)
  147. + return true;
  148. + }
  149. +
  150. if (is_contained(Config.ToRemove, Sec.Name))
  151. return true;
  152. --
  153. 2.17.1