llvm-objcopy.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. //===- llvm-objcopy.cpp ---------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "llvm-objcopy.h"
  10. #include "Object.h"
  11. #include "llvm/ADT/STLExtras.h"
  12. #include "llvm/ADT/StringRef.h"
  13. #include "llvm/ADT/Twine.h"
  14. #include "llvm/BinaryFormat/ELF.h"
  15. #include "llvm/Object/Binary.h"
  16. #include "llvm/Object/ELFObjectFile.h"
  17. #include "llvm/Object/ELFTypes.h"
  18. #include "llvm/Object/Error.h"
  19. #include "llvm/Support/Casting.h"
  20. #include "llvm/Support/CommandLine.h"
  21. #include "llvm/Support/Compiler.h"
  22. #include "llvm/Support/Error.h"
  23. #include "llvm/Support/ErrorHandling.h"
  24. #include "llvm/Support/ErrorOr.h"
  25. #include "llvm/Support/FileOutputBuffer.h"
  26. #include "llvm/Support/InitLLVM.h"
  27. #include "llvm/Support/raw_ostream.h"
  28. #include <algorithm>
  29. #include <cassert>
  30. #include <cstdlib>
  31. #include <functional>
  32. #include <iterator>
  33. #include <memory>
  34. #include <string>
  35. #include <system_error>
  36. #include <utility>
  37. using namespace llvm;
  38. using namespace object;
  39. using namespace ELF;
  40. // The name this program was invoked as.
  41. static StringRef ToolName;
  42. namespace llvm {
  43. LLVM_ATTRIBUTE_NORETURN void error(Twine Message) {
  44. errs() << ToolName << ": " << Message << ".\n";
  45. errs().flush();
  46. exit(1);
  47. }
  48. LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, std::error_code EC) {
  49. assert(EC);
  50. errs() << ToolName << ": '" << File << "': " << EC.message() << ".\n";
  51. exit(1);
  52. }
  53. LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, Error E) {
  54. assert(E);
  55. std::string Buf;
  56. raw_string_ostream OS(Buf);
  57. logAllUnhandledErrors(std::move(E), OS, "");
  58. OS.flush();
  59. errs() << ToolName << ": '" << File << "': " << Buf;
  60. exit(1);
  61. }
  62. } // end namespace llvm
  63. static cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input>"));
  64. static cl::opt<std::string> OutputFilename(cl::Positional, cl::desc("[ <output> ]"));
  65. static cl::opt<std::string>
  66. OutputFormat("O", cl::desc("Set output format to one of the following:"
  67. "\n\tbinary"));
  68. static cl::list<std::string> ToRemove("remove-section",
  69. cl::desc("Remove <section>"),
  70. cl::value_desc("section"));
  71. static cl::alias ToRemoveA("R", cl::desc("Alias for remove-section"),
  72. cl::aliasopt(ToRemove));
  73. static cl::opt<bool> StripAll(
  74. "strip-all",
  75. cl::desc(
  76. "Removes non-allocated sections other than .gnu.warning* sections"));
  77. static cl::opt<bool>
  78. StripAllGNU("strip-all-gnu",
  79. cl::desc("Removes symbol, relocation, and debug information"));
  80. static cl::list<std::string> Keep("keep", cl::desc("Keep <section>"),
  81. cl::value_desc("section"));
  82. static cl::list<std::string> OnlyKeep("only-keep",
  83. cl::desc("Remove all but <section>"),
  84. cl::value_desc("section"));
  85. static cl::alias OnlyKeepA("j", cl::desc("Alias for only-keep"),
  86. cl::aliasopt(OnlyKeep));
  87. static cl::opt<bool> StripDebug("strip-debug",
  88. cl::desc("Removes all debug information"));
  89. static cl::opt<bool> StripSections("strip-sections",
  90. cl::desc("Remove all section headers"));
  91. static cl::opt<bool>
  92. StripNonAlloc("strip-non-alloc",
  93. cl::desc("Remove all non-allocated sections"));
  94. static cl::opt<bool>
  95. StripDWO("strip-dwo", cl::desc("Remove all DWARF .dwo sections from file"));
  96. static cl::opt<bool> ExtractDWO(
  97. "extract-dwo",
  98. cl::desc("Remove all sections that are not DWARF .dwo sections from file"));
  99. static cl::opt<std::string>
  100. SplitDWO("split-dwo",
  101. cl::desc("Equivalent to extract-dwo on the input file to "
  102. "<dwo-file>, then strip-dwo on the input file"),
  103. cl::value_desc("dwo-file"));
  104. static cl::list<std::string> AddSection(
  105. "add-section",
  106. cl::desc("Make a section named <section> with the contents of <file>."),
  107. cl::value_desc("section=file"));
  108. static cl::opt<bool> LocalizeHidden(
  109. "localize-hidden",
  110. cl::desc(
  111. "Mark all symbols that have hidden or internal visibility as local"));
  112. static cl::opt<std::string>
  113. AddGnuDebugLink("add-gnu-debuglink",
  114. cl::desc("adds a .gnu_debuglink for <debug-file>"),
  115. cl::value_desc("debug-file"));
  116. using SectionPred = std::function<bool(const SectionBase &Sec)>;
  117. bool IsDWOSection(const SectionBase &Sec) { return Sec.Name.endswith(".dwo"); }
  118. bool OnlyKeepDWOPred(const Object &Obj, const SectionBase &Sec) {
  119. // We can't remove the section header string table.
  120. if (&Sec == Obj.SectionNames)
  121. return false;
  122. // Short of keeping the string table we want to keep everything that is a DWO
  123. // section and remove everything else.
  124. return !IsDWOSection(Sec);
  125. }
  126. static ElfType OutputElfType;
  127. std::unique_ptr<Writer> CreateWriter(Object &Obj, StringRef File) {
  128. if (OutputFormat == "binary") {
  129. return llvm::make_unique<BinaryWriter>(OutputFilename, Obj);
  130. }
  131. // Depending on the initial ELFT and OutputFormat we need a different Writer.
  132. switch (OutputElfType) {
  133. case ELFT_ELF32LE:
  134. return llvm::make_unique<ELFWriter<ELF32LE>>(File, Obj, !StripSections);
  135. case ELFT_ELF64LE:
  136. return llvm::make_unique<ELFWriter<ELF64LE>>(File, Obj, !StripSections);
  137. case ELFT_ELF32BE:
  138. return llvm::make_unique<ELFWriter<ELF32BE>>(File, Obj, !StripSections);
  139. case ELFT_ELF64BE:
  140. return llvm::make_unique<ELFWriter<ELF64BE>>(File, Obj, !StripSections);
  141. }
  142. llvm_unreachable("Invalid output format");
  143. }
  144. void SplitDWOToFile(const Reader &Reader, StringRef File) {
  145. auto DWOFile = Reader.create();
  146. DWOFile->removeSections(
  147. [&](const SectionBase &Sec) { return OnlyKeepDWOPred(*DWOFile, Sec); });
  148. auto Writer = CreateWriter(*DWOFile, File);
  149. Writer->finalize();
  150. Writer->write();
  151. }
  152. // This function handles the high level operations of GNU objcopy including
  153. // handling command line options. It's important to outline certain properties
  154. // we expect to hold of the command line operations. Any operation that "keeps"
  155. // should keep regardless of a remove. Additionally any removal should respect
  156. // any previous removals. Lastly whether or not something is removed shouldn't
  157. // depend a) on the order the options occur in or b) on some opaque priority
  158. // system. The only priority is that keeps/copies overrule removes.
  159. void HandleArgs(Object &Obj, const Reader &Reader) {
  160. if (!SplitDWO.empty()) {
  161. SplitDWOToFile(Reader, SplitDWO);
  162. }
  163. // Localize:
  164. if (LocalizeHidden) {
  165. Obj.SymbolTable->localize([](const Symbol &Sym) {
  166. return Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL;
  167. });
  168. }
  169. SectionPred RemovePred = [](const SectionBase &) { return false; };
  170. // Removes:
  171. if (!ToRemove.empty()) {
  172. RemovePred = [&](const SectionBase &Sec) {
  173. return std::find(std::begin(ToRemove), std::end(ToRemove), Sec.Name) !=
  174. std::end(ToRemove);
  175. };
  176. }
  177. if (StripDWO || !SplitDWO.empty())
  178. RemovePred = [RemovePred](const SectionBase &Sec) {
  179. return IsDWOSection(Sec) || RemovePred(Sec);
  180. };
  181. if (ExtractDWO)
  182. RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
  183. return OnlyKeepDWOPred(Obj, Sec) || RemovePred(Sec);
  184. };
  185. if (StripAllGNU)
  186. RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
  187. if (RemovePred(Sec))
  188. return true;
  189. if ((Sec.Flags & SHF_ALLOC) != 0)
  190. return false;
  191. if (&Sec == Obj.SectionNames)
  192. return false;
  193. switch (Sec.Type) {
  194. case SHT_SYMTAB:
  195. case SHT_REL:
  196. case SHT_RELA:
  197. case SHT_STRTAB:
  198. return true;
  199. }
  200. return Sec.Name.startswith(".debug");
  201. };
  202. if (StripSections) {
  203. RemovePred = [RemovePred](const SectionBase &Sec) {
  204. return RemovePred(Sec) || (Sec.Flags & SHF_ALLOC) == 0;
  205. };
  206. }
  207. if (StripDebug) {
  208. RemovePred = [RemovePred](const SectionBase &Sec) {
  209. return RemovePred(Sec) || Sec.Name.startswith(".debug");
  210. };
  211. }
  212. if (StripNonAlloc)
  213. RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
  214. if (RemovePred(Sec))
  215. return true;
  216. if (&Sec == Obj.SectionNames)
  217. return false;
  218. return (Sec.Flags & SHF_ALLOC) == 0;
  219. };
  220. if (StripAll)
  221. RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
  222. if (RemovePred(Sec))
  223. return true;
  224. if (&Sec == Obj.SectionNames)
  225. return false;
  226. if (Sec.Name.startswith(".gnu.warning"))
  227. return false;
  228. return (Sec.Flags & SHF_ALLOC) == 0;
  229. };
  230. // Explicit copies:
  231. if (!OnlyKeep.empty()) {
  232. RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
  233. // Explicitly keep these sections regardless of previous removes.
  234. if (std::find(std::begin(OnlyKeep), std::end(OnlyKeep), Sec.Name) !=
  235. std::end(OnlyKeep))
  236. return false;
  237. // Allow all implicit removes.
  238. if (RemovePred(Sec))
  239. return true;
  240. // Keep special sections.
  241. if (Obj.SectionNames == &Sec)
  242. return false;
  243. if (Obj.SymbolTable == &Sec || Obj.SymbolTable->getStrTab() == &Sec)
  244. return false;
  245. // Remove everything else.
  246. return true;
  247. };
  248. }
  249. if (!Keep.empty()) {
  250. RemovePred = [RemovePred](const SectionBase &Sec) {
  251. // Explicitly keep these sections regardless of previous removes.
  252. if (std::find(std::begin(Keep), std::end(Keep), Sec.Name) !=
  253. std::end(Keep))
  254. return false;
  255. // Otherwise defer to RemovePred.
  256. return RemovePred(Sec);
  257. };
  258. }
  259. Obj.removeSections(RemovePred);
  260. if (!AddSection.empty()) {
  261. for (const auto &Flag : AddSection) {
  262. auto SecPair = StringRef(Flag).split("=");
  263. auto SecName = SecPair.first;
  264. auto File = SecPair.second;
  265. auto BufOrErr = MemoryBuffer::getFile(File);
  266. if (!BufOrErr)
  267. reportError(File, BufOrErr.getError());
  268. auto Buf = std::move(*BufOrErr);
  269. auto BufPtr = reinterpret_cast<const uint8_t *>(Buf->getBufferStart());
  270. auto BufSize = Buf->getBufferSize();
  271. Obj.addSection<OwnedDataSection>(SecName,
  272. ArrayRef<uint8_t>(BufPtr, BufSize));
  273. }
  274. }
  275. if (!AddGnuDebugLink.empty()) {
  276. Obj.addSection<GnuDebugLinkSection>(StringRef(AddGnuDebugLink));
  277. }
  278. }
  279. std::unique_ptr<Reader> CreateReader() {
  280. // Right now we can only read ELF files so there's only one reader;
  281. auto Out = llvm::make_unique<ELFReader>(StringRef(InputFilename));
  282. // We need to set the default ElfType for output.
  283. OutputElfType = Out->getElfType();
  284. return std::move(Out);
  285. }
  286. int main(int argc, char **argv) {
  287. InitLLVM X(argc, argv);
  288. cl::ParseCommandLineOptions(argc, argv, "llvm objcopy utility\n");
  289. ToolName = argv[0];
  290. if (InputFilename.empty()) {
  291. cl::PrintHelpMessage();
  292. return 2;
  293. }
  294. auto Reader = CreateReader();
  295. auto Obj = Reader->create();
  296. StringRef Output =
  297. OutputFilename.getNumOccurrences() ? OutputFilename : InputFilename;
  298. auto Writer = CreateWriter(*Obj, Output);
  299. HandleArgs(*Obj, *Reader);
  300. Writer->finalize();
  301. Writer->write();
  302. }