CommonConfig.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. //===- CommonConfig.h -------------------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef LLVM_TOOLS_LLVM_OBJCOPY_COMMONCONFIG_H
  9. #define LLVM_TOOLS_LLVM_OBJCOPY_COMMONCONFIG_H
  10. #include "llvm/ADT/ArrayRef.h"
  11. #include "llvm/ADT/CachedHashString.h"
  12. #include "llvm/ADT/DenseSet.h"
  13. #include "llvm/ADT/Optional.h"
  14. #include "llvm/ADT/SmallVector.h"
  15. #include "llvm/ADT/StringMap.h"
  16. #include "llvm/ADT/StringRef.h"
  17. #include "llvm/Object/ELFTypes.h"
  18. #include "llvm/Support/GlobPattern.h"
  19. #include "llvm/Support/Regex.h"
  20. // Necessary for llvm::DebugCompressionType::None
  21. #include "llvm/Target/TargetOptions.h"
  22. #include <vector>
  23. namespace llvm {
  24. namespace objcopy {
  25. enum class FileFormat {
  26. Unspecified,
  27. ELF,
  28. Binary,
  29. IHex,
  30. };
  31. // This type keeps track of the machine info for various architectures. This
  32. // lets us map architecture names to ELF types and the e_machine value of the
  33. // ELF file.
  34. struct MachineInfo {
  35. MachineInfo(uint16_t EM, uint8_t ABI, bool Is64, bool IsLittle)
  36. : EMachine(EM), OSABI(ABI), Is64Bit(Is64), IsLittleEndian(IsLittle) {}
  37. // Alternative constructor that defaults to NONE for OSABI.
  38. MachineInfo(uint16_t EM, bool Is64, bool IsLittle)
  39. : MachineInfo(EM, ELF::ELFOSABI_NONE, Is64, IsLittle) {}
  40. // Default constructor for unset fields.
  41. MachineInfo() : MachineInfo(0, 0, false, false) {}
  42. uint16_t EMachine;
  43. uint8_t OSABI;
  44. bool Is64Bit;
  45. bool IsLittleEndian;
  46. };
  47. // Flags set by --set-section-flags or --rename-section. Interpretation of these
  48. // is format-specific and not all flags are meaningful for all object file
  49. // formats. This is a bitmask; many section flags may be set.
  50. enum SectionFlag {
  51. SecNone = 0,
  52. SecAlloc = 1 << 0,
  53. SecLoad = 1 << 1,
  54. SecNoload = 1 << 2,
  55. SecReadonly = 1 << 3,
  56. SecDebug = 1 << 4,
  57. SecCode = 1 << 5,
  58. SecData = 1 << 6,
  59. SecRom = 1 << 7,
  60. SecMerge = 1 << 8,
  61. SecStrings = 1 << 9,
  62. SecContents = 1 << 10,
  63. SecShare = 1 << 11,
  64. SecExclude = 1 << 12,
  65. LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/SecExclude)
  66. };
  67. struct SectionRename {
  68. StringRef OriginalName;
  69. StringRef NewName;
  70. Optional<SectionFlag> NewFlags;
  71. };
  72. struct SectionFlagsUpdate {
  73. StringRef Name;
  74. SectionFlag NewFlags;
  75. };
  76. enum class DiscardType {
  77. None, // Default
  78. All, // --discard-all (-x)
  79. Locals, // --discard-locals (-X)
  80. };
  81. enum class MatchStyle {
  82. Literal, // Default for symbols.
  83. Wildcard, // Default for sections, or enabled with --wildcard (-w).
  84. Regex, // Enabled with --regex.
  85. };
  86. class NameOrPattern {
  87. StringRef Name;
  88. // Regex is shared between multiple CommonConfig instances.
  89. std::shared_ptr<Regex> R;
  90. std::shared_ptr<GlobPattern> G;
  91. bool IsPositiveMatch = true;
  92. NameOrPattern(StringRef N) : Name(N) {}
  93. NameOrPattern(std::shared_ptr<Regex> R) : R(R) {}
  94. NameOrPattern(std::shared_ptr<GlobPattern> G, bool IsPositiveMatch)
  95. : G(G), IsPositiveMatch(IsPositiveMatch) {}
  96. public:
  97. // ErrorCallback is used to handle recoverable errors. An Error returned
  98. // by the callback aborts the parsing and is then returned by this function.
  99. static Expected<NameOrPattern>
  100. create(StringRef Pattern, MatchStyle MS,
  101. llvm::function_ref<Error(Error)> ErrorCallback);
  102. bool isPositiveMatch() const { return IsPositiveMatch; }
  103. Optional<StringRef> getName() const {
  104. if (!R && !G)
  105. return Name;
  106. return None;
  107. }
  108. bool operator==(StringRef S) const {
  109. return R ? R->match(S) : G ? G->match(S) : Name == S;
  110. }
  111. bool operator!=(StringRef S) const { return !operator==(S); }
  112. };
  113. // Matcher that checks symbol or section names against the command line flags
  114. // provided for that option.
  115. class NameMatcher {
  116. DenseSet<CachedHashStringRef> PosNames;
  117. std::vector<NameOrPattern> PosPatterns;
  118. std::vector<NameOrPattern> NegMatchers;
  119. public:
  120. Error addMatcher(Expected<NameOrPattern> Matcher) {
  121. if (!Matcher)
  122. return Matcher.takeError();
  123. if (Matcher->isPositiveMatch()) {
  124. if (Optional<StringRef> MaybeName = Matcher->getName())
  125. PosNames.insert(CachedHashStringRef(*MaybeName));
  126. else
  127. PosPatterns.push_back(std::move(*Matcher));
  128. } else {
  129. NegMatchers.push_back(std::move(*Matcher));
  130. }
  131. return Error::success();
  132. }
  133. bool matches(StringRef S) const {
  134. return (PosNames.contains(CachedHashStringRef(S)) ||
  135. is_contained(PosPatterns, S)) &&
  136. !is_contained(NegMatchers, S);
  137. }
  138. bool empty() const {
  139. return PosNames.empty() && PosPatterns.empty() && NegMatchers.empty();
  140. }
  141. };
  142. enum class SymbolFlag {
  143. Global,
  144. Local,
  145. Weak,
  146. Default,
  147. Hidden,
  148. Protected,
  149. File,
  150. Section,
  151. Object,
  152. Function,
  153. IndirectFunction,
  154. Debug,
  155. Constructor,
  156. Warning,
  157. Indirect,
  158. Synthetic,
  159. UniqueObject,
  160. };
  161. // Symbol info specified by --add-symbol option. Symbol flags not supported
  162. // by a concrete format should be ignored.
  163. struct NewSymbolInfo {
  164. StringRef SymbolName;
  165. StringRef SectionName;
  166. uint64_t Value = 0;
  167. std::vector<SymbolFlag> Flags;
  168. std::vector<StringRef> BeforeSyms;
  169. };
  170. // Configuration for copying/stripping a single file.
  171. struct CommonConfig {
  172. // Main input/output options
  173. StringRef InputFilename;
  174. FileFormat InputFormat = FileFormat::Unspecified;
  175. StringRef OutputFilename;
  176. FileFormat OutputFormat = FileFormat::Unspecified;
  177. // Only applicable when --output-format!=binary (e.g. elf64-x86-64).
  178. Optional<MachineInfo> OutputArch;
  179. // Advanced options
  180. StringRef AddGnuDebugLink;
  181. // Cached gnu_debuglink's target CRC
  182. uint32_t GnuDebugLinkCRC32;
  183. Optional<StringRef> ExtractPartition;
  184. StringRef SplitDWO;
  185. StringRef SymbolsPrefix;
  186. StringRef AllocSectionsPrefix;
  187. DiscardType DiscardMode = DiscardType::None;
  188. // Repeated options
  189. std::vector<StringRef> AddSection;
  190. std::vector<StringRef> DumpSection;
  191. std::vector<StringRef> UpdateSection;
  192. // Section matchers
  193. NameMatcher KeepSection;
  194. NameMatcher OnlySection;
  195. NameMatcher ToRemove;
  196. // Symbol matchers
  197. NameMatcher SymbolsToGlobalize;
  198. NameMatcher SymbolsToKeep;
  199. NameMatcher SymbolsToLocalize;
  200. NameMatcher SymbolsToRemove;
  201. NameMatcher UnneededSymbolsToRemove;
  202. NameMatcher SymbolsToWeaken;
  203. NameMatcher SymbolsToKeepGlobal;
  204. // Map options
  205. StringMap<SectionRename> SectionsToRename;
  206. StringMap<uint64_t> SetSectionAlignment;
  207. StringMap<SectionFlagsUpdate> SetSectionFlags;
  208. StringMap<StringRef> SymbolsToRename;
  209. // Symbol info specified by --add-symbol option.
  210. std::vector<NewSymbolInfo> SymbolsToAdd;
  211. // Boolean options
  212. bool DeterministicArchives = true;
  213. bool ExtractDWO = false;
  214. bool ExtractMainPartition = false;
  215. bool OnlyKeepDebug = false;
  216. bool PreserveDates = false;
  217. bool StripAll = false;
  218. bool StripAllGNU = false;
  219. bool StripDWO = false;
  220. bool StripDebug = false;
  221. bool StripNonAlloc = false;
  222. bool StripSections = false;
  223. bool StripUnneeded = false;
  224. bool Weaken = false;
  225. bool DecompressDebugSections = false;
  226. DebugCompressionType CompressionType = DebugCompressionType::None;
  227. };
  228. } // namespace objcopy
  229. } // namespace llvm
  230. #endif // LLVM_TOOLS_LLVM_OBJCOPY_COMMONCONFIG_H