llvm-objcopy.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. //===- llvm-objcopy.cpp ---------------------------------------------------===//
  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. #include "llvm-objcopy.h"
  9. #include "COFF/COFFConfig.h"
  10. #include "COFF/COFFObjcopy.h"
  11. #include "CommonConfig.h"
  12. #include "ConfigManager.h"
  13. #include "ELF/ELFConfig.h"
  14. #include "ELF/ELFObjcopy.h"
  15. #include "MachO/MachOConfig.h"
  16. #include "MachO/MachOObjcopy.h"
  17. #include "wasm/WasmConfig.h"
  18. #include "wasm/WasmObjcopy.h"
  19. #include "llvm/ADT/STLExtras.h"
  20. #include "llvm/ADT/SmallVector.h"
  21. #include "llvm/ADT/StringRef.h"
  22. #include "llvm/ADT/Twine.h"
  23. #include "llvm/BinaryFormat/ELF.h"
  24. #include "llvm/Object/Archive.h"
  25. #include "llvm/Object/ArchiveWriter.h"
  26. #include "llvm/Object/Binary.h"
  27. #include "llvm/Object/COFF.h"
  28. #include "llvm/Object/ELFObjectFile.h"
  29. #include "llvm/Object/ELFTypes.h"
  30. #include "llvm/Object/Error.h"
  31. #include "llvm/Object/MachO.h"
  32. #include "llvm/Object/MachOUniversal.h"
  33. #include "llvm/Object/Wasm.h"
  34. #include "llvm/Option/Arg.h"
  35. #include "llvm/Option/ArgList.h"
  36. #include "llvm/Option/Option.h"
  37. #include "llvm/Support/Casting.h"
  38. #include "llvm/Support/CommandLine.h"
  39. #include "llvm/Support/Errc.h"
  40. #include "llvm/Support/Error.h"
  41. #include "llvm/Support/ErrorHandling.h"
  42. #include "llvm/Support/ErrorOr.h"
  43. #include "llvm/Support/Host.h"
  44. #include "llvm/Support/InitLLVM.h"
  45. #include "llvm/Support/Memory.h"
  46. #include "llvm/Support/Path.h"
  47. #include "llvm/Support/Process.h"
  48. #include "llvm/Support/SmallVectorMemoryBuffer.h"
  49. #include "llvm/Support/StringSaver.h"
  50. #include "llvm/Support/WithColor.h"
  51. #include "llvm/Support/raw_ostream.h"
  52. #include <algorithm>
  53. #include <cassert>
  54. #include <cstdlib>
  55. #include <memory>
  56. #include <string>
  57. #include <system_error>
  58. #include <utility>
  59. using namespace llvm;
  60. using namespace llvm::objcopy;
  61. using namespace llvm::object;
  62. // The name this program was invoked as.
  63. static StringRef ToolName;
  64. static ErrorSuccess reportWarning(Error E) {
  65. assert(E);
  66. WithColor::warning(errs(), ToolName) << toString(std::move(E)) << '\n';
  67. return Error::success();
  68. }
  69. static Expected<DriverConfig> getDriverConfig(ArrayRef<const char *> Args) {
  70. StringRef Stem = sys::path::stem(ToolName);
  71. auto Is = [=](StringRef Tool) {
  72. // We need to recognize the following filenames:
  73. //
  74. // llvm-objcopy -> objcopy
  75. // strip-10.exe -> strip
  76. // powerpc64-unknown-freebsd13-objcopy -> objcopy
  77. // llvm-install-name-tool -> install-name-tool
  78. auto I = Stem.rfind_insensitive(Tool);
  79. return I != StringRef::npos &&
  80. (I + Tool.size() == Stem.size() || !isAlnum(Stem[I + Tool.size()]));
  81. };
  82. if (Is("bitcode-strip") || Is("bitcode_strip"))
  83. return parseBitcodeStripOptions(Args);
  84. else if (Is("strip"))
  85. return parseStripOptions(Args, reportWarning);
  86. else if (Is("install-name-tool") || Is("install_name_tool"))
  87. return parseInstallNameToolOptions(Args);
  88. else
  89. return parseObjcopyOptions(Args, reportWarning);
  90. }
  91. // For regular archives this function simply calls llvm::writeArchive,
  92. // For thin archives it writes the archive file itself as well as its members.
  93. static Error deepWriteArchive(StringRef ArcName,
  94. ArrayRef<NewArchiveMember> NewMembers,
  95. bool WriteSymtab, object::Archive::Kind Kind,
  96. bool Deterministic, bool Thin) {
  97. if (Error E = writeArchive(ArcName, NewMembers, WriteSymtab, Kind,
  98. Deterministic, Thin))
  99. return createFileError(ArcName, std::move(E));
  100. if (!Thin)
  101. return Error::success();
  102. for (const NewArchiveMember &Member : NewMembers) {
  103. // For regular files (as is the case for deepWriteArchive),
  104. // FileOutputBuffer::create will return OnDiskBuffer.
  105. // OnDiskBuffer uses a temporary file and then renames it. So in reality
  106. // there is no inefficiency / duplicated in-memory buffers in this case. For
  107. // now in-memory buffers can not be completely avoided since
  108. // NewArchiveMember still requires them even though writeArchive does not
  109. // write them on disk.
  110. Expected<std::unique_ptr<FileOutputBuffer>> FB =
  111. FileOutputBuffer::create(Member.MemberName, Member.Buf->getBufferSize(),
  112. FileOutputBuffer::F_executable);
  113. if (!FB)
  114. return FB.takeError();
  115. std::copy(Member.Buf->getBufferStart(), Member.Buf->getBufferEnd(),
  116. (*FB)->getBufferStart());
  117. if (Error E = (*FB)->commit())
  118. return E;
  119. }
  120. return Error::success();
  121. }
  122. /// The function executeObjcopyOnIHex does the dispatch based on the format
  123. /// of the output specified by the command line options.
  124. static Error executeObjcopyOnIHex(ConfigManager &ConfigMgr, MemoryBuffer &In,
  125. raw_ostream &Out) {
  126. // TODO: support output formats other than ELF.
  127. Expected<const ELFConfig &> ELFConfig = ConfigMgr.getELFConfig();
  128. if (!ELFConfig)
  129. return ELFConfig.takeError();
  130. return elf::executeObjcopyOnIHex(ConfigMgr.getCommonConfig(), *ELFConfig, In,
  131. Out);
  132. }
  133. /// The function executeObjcopyOnRawBinary does the dispatch based on the format
  134. /// of the output specified by the command line options.
  135. static Error executeObjcopyOnRawBinary(ConfigManager &ConfigMgr,
  136. MemoryBuffer &In, raw_ostream &Out) {
  137. const CommonConfig &Config = ConfigMgr.getCommonConfig();
  138. switch (Config.OutputFormat) {
  139. case FileFormat::ELF:
  140. // FIXME: Currently, we call elf::executeObjcopyOnRawBinary even if the
  141. // output format is binary/ihex or it's not given. This behavior differs from
  142. // GNU objcopy. See https://bugs.llvm.org/show_bug.cgi?id=42171 for details.
  143. case FileFormat::Binary:
  144. case FileFormat::IHex:
  145. case FileFormat::Unspecified:
  146. Expected<const ELFConfig &> ELFConfig = ConfigMgr.getELFConfig();
  147. if (!ELFConfig)
  148. return ELFConfig.takeError();
  149. return elf::executeObjcopyOnRawBinary(Config, *ELFConfig, In, Out);
  150. }
  151. llvm_unreachable("unsupported output format");
  152. }
  153. /// The function executeObjcopyOnBinary does the dispatch based on the format
  154. /// of the input binary (ELF, MachO or COFF).
  155. static Error executeObjcopyOnBinary(const MultiFormatConfig &Config,
  156. object::Binary &In, raw_ostream &Out) {
  157. if (auto *ELFBinary = dyn_cast<object::ELFObjectFileBase>(&In)) {
  158. Expected<const ELFConfig &> ELFConfig = Config.getELFConfig();
  159. if (!ELFConfig)
  160. return ELFConfig.takeError();
  161. return elf::executeObjcopyOnBinary(Config.getCommonConfig(), *ELFConfig,
  162. *ELFBinary, Out);
  163. } else if (auto *COFFBinary = dyn_cast<object::COFFObjectFile>(&In)) {
  164. Expected<const COFFConfig &> COFFConfig = Config.getCOFFConfig();
  165. if (!COFFConfig)
  166. return COFFConfig.takeError();
  167. return coff::executeObjcopyOnBinary(Config.getCommonConfig(), *COFFConfig,
  168. *COFFBinary, Out);
  169. } else if (auto *MachOBinary = dyn_cast<object::MachOObjectFile>(&In)) {
  170. Expected<const MachOConfig &> MachOConfig = Config.getMachOConfig();
  171. if (!MachOConfig)
  172. return MachOConfig.takeError();
  173. return macho::executeObjcopyOnBinary(Config.getCommonConfig(), *MachOConfig,
  174. *MachOBinary, Out);
  175. } else if (auto *MachOUniversalBinary =
  176. dyn_cast<object::MachOUniversalBinary>(&In)) {
  177. return macho::executeObjcopyOnMachOUniversalBinary(
  178. Config, *MachOUniversalBinary, Out);
  179. } else if (auto *WasmBinary = dyn_cast<object::WasmObjectFile>(&In)) {
  180. Expected<const WasmConfig &> WasmConfig = Config.getWasmConfig();
  181. if (!WasmConfig)
  182. return WasmConfig.takeError();
  183. return objcopy::wasm::executeObjcopyOnBinary(Config.getCommonConfig(),
  184. *WasmConfig, *WasmBinary, Out);
  185. } else
  186. return createStringError(object_error::invalid_file_type,
  187. "unsupported object file format");
  188. }
  189. namespace llvm {
  190. namespace objcopy {
  191. Expected<std::vector<NewArchiveMember>>
  192. createNewArchiveMembers(const MultiFormatConfig &Config, const Archive &Ar) {
  193. std::vector<NewArchiveMember> NewArchiveMembers;
  194. Error Err = Error::success();
  195. for (const Archive::Child &Child : Ar.children(Err)) {
  196. Expected<StringRef> ChildNameOrErr = Child.getName();
  197. if (!ChildNameOrErr)
  198. return createFileError(Ar.getFileName(), ChildNameOrErr.takeError());
  199. Expected<std::unique_ptr<Binary>> ChildOrErr = Child.getAsBinary();
  200. if (!ChildOrErr)
  201. return createFileError(Ar.getFileName() + "(" + *ChildNameOrErr + ")",
  202. ChildOrErr.takeError());
  203. SmallVector<char, 0> Buffer;
  204. raw_svector_ostream MemStream(Buffer);
  205. if (Error E = executeObjcopyOnBinary(Config, *ChildOrErr->get(), MemStream))
  206. return std::move(E);
  207. Expected<NewArchiveMember> Member = NewArchiveMember::getOldMember(
  208. Child, Config.getCommonConfig().DeterministicArchives);
  209. if (!Member)
  210. return createFileError(Ar.getFileName(), Member.takeError());
  211. Member->Buf = std::make_unique<SmallVectorMemoryBuffer>(
  212. std::move(Buffer), ChildNameOrErr.get(),
  213. /*RequiresNullTerminator=*/false);
  214. Member->MemberName = Member->Buf->getBufferIdentifier();
  215. NewArchiveMembers.push_back(std::move(*Member));
  216. }
  217. if (Err)
  218. return createFileError(Config.getCommonConfig().InputFilename,
  219. std::move(Err));
  220. return std::move(NewArchiveMembers);
  221. }
  222. } // end namespace objcopy
  223. } // end namespace llvm
  224. static Error executeObjcopyOnArchive(const ConfigManager &ConfigMgr,
  225. const object::Archive &Ar) {
  226. Expected<std::vector<NewArchiveMember>> NewArchiveMembersOrErr =
  227. createNewArchiveMembers(ConfigMgr, Ar);
  228. if (!NewArchiveMembersOrErr)
  229. return NewArchiveMembersOrErr.takeError();
  230. const CommonConfig &Config = ConfigMgr.getCommonConfig();
  231. return deepWriteArchive(Config.OutputFilename, *NewArchiveMembersOrErr,
  232. Ar.hasSymbolTable(), Ar.kind(),
  233. Config.DeterministicArchives, Ar.isThin());
  234. }
  235. static Error restoreStatOnFile(StringRef Filename,
  236. const sys::fs::file_status &Stat,
  237. const ConfigManager &ConfigMgr) {
  238. int FD;
  239. const CommonConfig &Config = ConfigMgr.getCommonConfig();
  240. // Writing to stdout should not be treated as an error here, just
  241. // do not set access/modification times or permissions.
  242. if (Filename == "-")
  243. return Error::success();
  244. if (auto EC =
  245. sys::fs::openFileForWrite(Filename, FD, sys::fs::CD_OpenExisting))
  246. return createFileError(Filename, EC);
  247. if (Config.PreserveDates)
  248. if (auto EC = sys::fs::setLastAccessAndModificationTime(
  249. FD, Stat.getLastAccessedTime(), Stat.getLastModificationTime()))
  250. return createFileError(Filename, EC);
  251. sys::fs::file_status OStat;
  252. if (std::error_code EC = sys::fs::status(FD, OStat))
  253. return createFileError(Filename, EC);
  254. if (OStat.type() == sys::fs::file_type::regular_file) {
  255. #ifndef _WIN32
  256. // Keep ownership if llvm-objcopy is called under root.
  257. if (Config.InputFilename == Config.OutputFilename && OStat.getUser() == 0)
  258. sys::fs::changeFileOwnership(FD, Stat.getUser(), Stat.getGroup());
  259. #endif
  260. sys::fs::perms Perm = Stat.permissions();
  261. if (Config.InputFilename != Config.OutputFilename)
  262. Perm = static_cast<sys::fs::perms>(Perm & ~sys::fs::getUmask() & ~06000);
  263. #ifdef _WIN32
  264. if (auto EC = sys::fs::setPermissions(Filename, Perm))
  265. #else
  266. if (auto EC = sys::fs::setPermissions(FD, Perm))
  267. #endif
  268. return createFileError(Filename, EC);
  269. }
  270. if (auto EC = sys::Process::SafelyCloseFileDescriptor(FD))
  271. return createFileError(Filename, EC);
  272. return Error::success();
  273. }
  274. /// The function executeObjcopy does the higher level dispatch based on the type
  275. /// of input (raw binary, archive or single object file) and takes care of the
  276. /// format-agnostic modifications, i.e. preserving dates.
  277. static Error executeObjcopy(ConfigManager &ConfigMgr) {
  278. CommonConfig &Config = ConfigMgr.Common;
  279. sys::fs::file_status Stat;
  280. if (Config.InputFilename != "-") {
  281. if (auto EC = sys::fs::status(Config.InputFilename, Stat))
  282. return createFileError(Config.InputFilename, EC);
  283. } else {
  284. Stat.permissions(static_cast<sys::fs::perms>(0777));
  285. }
  286. std::function<Error(raw_ostream & OutFile)> ObjcopyFunc;
  287. OwningBinary<llvm::object::Binary> BinaryHolder;
  288. std::unique_ptr<MemoryBuffer> MemoryBufferHolder;
  289. if (Config.InputFormat == FileFormat::Binary ||
  290. Config.InputFormat == FileFormat::IHex) {
  291. ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
  292. MemoryBuffer::getFileOrSTDIN(Config.InputFilename);
  293. if (!BufOrErr)
  294. return createFileError(Config.InputFilename, BufOrErr.getError());
  295. MemoryBufferHolder = std::move(*BufOrErr);
  296. if (Config.InputFormat == FileFormat::Binary)
  297. ObjcopyFunc = [&](raw_ostream &OutFile) -> Error {
  298. // Handle FileFormat::Binary.
  299. return executeObjcopyOnRawBinary(ConfigMgr, *MemoryBufferHolder,
  300. OutFile);
  301. };
  302. else
  303. ObjcopyFunc = [&](raw_ostream &OutFile) -> Error {
  304. // Handle FileFormat::IHex.
  305. return executeObjcopyOnIHex(ConfigMgr, *MemoryBufferHolder, OutFile);
  306. };
  307. } else {
  308. Expected<OwningBinary<llvm::object::Binary>> BinaryOrErr =
  309. createBinary(Config.InputFilename);
  310. if (!BinaryOrErr)
  311. return createFileError(Config.InputFilename, BinaryOrErr.takeError());
  312. BinaryHolder = std::move(*BinaryOrErr);
  313. if (Archive *Ar = dyn_cast<Archive>(BinaryHolder.getBinary())) {
  314. // Handle Archive.
  315. if (Error E = executeObjcopyOnArchive(ConfigMgr, *Ar))
  316. return E;
  317. } else {
  318. // Handle llvm::object::Binary.
  319. ObjcopyFunc = [&](raw_ostream &OutFile) -> Error {
  320. return executeObjcopyOnBinary(ConfigMgr, *BinaryHolder.getBinary(),
  321. OutFile);
  322. };
  323. }
  324. }
  325. if (ObjcopyFunc) {
  326. if (Config.SplitDWO.empty()) {
  327. // Apply transformations described by Config and store result into
  328. // Config.OutputFilename using specified ObjcopyFunc function.
  329. if (Error E = writeToOutput(Config.OutputFilename, ObjcopyFunc))
  330. return E;
  331. } else {
  332. Config.ExtractDWO = true;
  333. Config.StripDWO = false;
  334. // Copy .dwo tables from the Config.InputFilename into Config.SplitDWO
  335. // file using specified ObjcopyFunc function.
  336. if (Error E = writeToOutput(Config.SplitDWO, ObjcopyFunc))
  337. return E;
  338. Config.ExtractDWO = false;
  339. Config.StripDWO = true;
  340. // Apply transformations described by Config, remove .dwo tables and
  341. // store result into Config.OutputFilename using specified ObjcopyFunc
  342. // function.
  343. if (Error E = writeToOutput(Config.OutputFilename, ObjcopyFunc))
  344. return E;
  345. }
  346. }
  347. if (Error E = restoreStatOnFile(Config.OutputFilename, Stat, ConfigMgr))
  348. return E;
  349. if (!Config.SplitDWO.empty()) {
  350. Stat.permissions(static_cast<sys::fs::perms>(0666));
  351. if (Error E = restoreStatOnFile(Config.SplitDWO, Stat, ConfigMgr))
  352. return E;
  353. }
  354. return Error::success();
  355. }
  356. int main(int argc, char **argv) {
  357. InitLLVM X(argc, argv);
  358. ToolName = argv[0];
  359. // Expand response files.
  360. // TODO: Move these lines, which are copied from lib/Support/CommandLine.cpp,
  361. // into a separate function in the CommandLine library and call that function
  362. // here. This is duplicated code.
  363. SmallVector<const char *, 20> NewArgv(argv, argv + argc);
  364. BumpPtrAllocator A;
  365. StringSaver Saver(A);
  366. cl::ExpandResponseFiles(Saver,
  367. Triple(sys::getProcessTriple()).isOSWindows()
  368. ? cl::TokenizeWindowsCommandLine
  369. : cl::TokenizeGNUCommandLine,
  370. NewArgv);
  371. auto Args = makeArrayRef(NewArgv).drop_front();
  372. Expected<DriverConfig> DriverConfig = getDriverConfig(Args);
  373. if (!DriverConfig) {
  374. logAllUnhandledErrors(DriverConfig.takeError(),
  375. WithColor::error(errs(), ToolName));
  376. return 1;
  377. }
  378. for (ConfigManager &ConfigMgr : DriverConfig->CopyConfigs) {
  379. if (Error E = executeObjcopy(ConfigMgr)) {
  380. logAllUnhandledErrors(std::move(E), WithColor::error(errs(), ToolName));
  381. return 1;
  382. }
  383. }
  384. return 0;
  385. }