ObjCopy.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //===- 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/ObjCopy.h"
  9. #include "llvm/ObjCopy/COFF/COFFConfig.h"
  10. #include "llvm/ObjCopy/COFF/COFFObjcopy.h"
  11. #include "llvm/ObjCopy/CommonConfig.h"
  12. #include "llvm/ObjCopy/ELF/ELFConfig.h"
  13. #include "llvm/ObjCopy/ELF/ELFObjcopy.h"
  14. #include "llvm/ObjCopy/MachO/MachOConfig.h"
  15. #include "llvm/ObjCopy/MachO/MachOObjcopy.h"
  16. #include "llvm/ObjCopy/MultiFormatConfig.h"
  17. #include "llvm/ObjCopy/wasm/WasmConfig.h"
  18. #include "llvm/ObjCopy/wasm/WasmObjcopy.h"
  19. #include "llvm/ObjCopy/XCOFF/XCOFFConfig.h"
  20. #include "llvm/ObjCopy/XCOFF/XCOFFObjcopy.h"
  21. #include "llvm/Object/COFF.h"
  22. #include "llvm/Object/ELFObjectFile.h"
  23. #include "llvm/Object/Error.h"
  24. #include "llvm/Object/MachO.h"
  25. #include "llvm/Object/MachOUniversal.h"
  26. #include "llvm/Object/Wasm.h"
  27. #include "llvm/Object/XCOFFObjectFile.h"
  28. #include "llvm/Support/SmallVectorMemoryBuffer.h"
  29. namespace llvm {
  30. namespace objcopy {
  31. using namespace llvm::object;
  32. /// The function executeObjcopyOnBinary does the dispatch based on the format
  33. /// of the input binary (ELF, MachO or COFF).
  34. Error executeObjcopyOnBinary(const MultiFormatConfig &Config,
  35. object::Binary &In, raw_ostream &Out) {
  36. if (auto *ELFBinary = dyn_cast<object::ELFObjectFileBase>(&In)) {
  37. Expected<const ELFConfig &> ELFConfig = Config.getELFConfig();
  38. if (!ELFConfig)
  39. return ELFConfig.takeError();
  40. return elf::executeObjcopyOnBinary(Config.getCommonConfig(), *ELFConfig,
  41. *ELFBinary, Out);
  42. }
  43. if (auto *COFFBinary = dyn_cast<object::COFFObjectFile>(&In)) {
  44. Expected<const COFFConfig &> COFFConfig = Config.getCOFFConfig();
  45. if (!COFFConfig)
  46. return COFFConfig.takeError();
  47. return coff::executeObjcopyOnBinary(Config.getCommonConfig(), *COFFConfig,
  48. *COFFBinary, Out);
  49. }
  50. if (auto *MachOBinary = dyn_cast<object::MachOObjectFile>(&In)) {
  51. Expected<const MachOConfig &> MachOConfig = Config.getMachOConfig();
  52. if (!MachOConfig)
  53. return MachOConfig.takeError();
  54. return macho::executeObjcopyOnBinary(Config.getCommonConfig(), *MachOConfig,
  55. *MachOBinary, Out);
  56. }
  57. if (auto *MachOUniversalBinary =
  58. dyn_cast<object::MachOUniversalBinary>(&In)) {
  59. return macho::executeObjcopyOnMachOUniversalBinary(
  60. Config, *MachOUniversalBinary, Out);
  61. }
  62. if (auto *WasmBinary = dyn_cast<object::WasmObjectFile>(&In)) {
  63. Expected<const WasmConfig &> WasmConfig = Config.getWasmConfig();
  64. if (!WasmConfig)
  65. return WasmConfig.takeError();
  66. return objcopy::wasm::executeObjcopyOnBinary(Config.getCommonConfig(),
  67. *WasmConfig, *WasmBinary, Out);
  68. }
  69. if (auto *XCOFFBinary = dyn_cast<object::XCOFFObjectFile>(&In)) {
  70. Expected<const XCOFFConfig &> XCOFFConfig = Config.getXCOFFConfig();
  71. if (!XCOFFConfig)
  72. return XCOFFConfig.takeError();
  73. return xcoff::executeObjcopyOnBinary(Config.getCommonConfig(), *XCOFFConfig,
  74. *XCOFFBinary, Out);
  75. }
  76. return createStringError(object_error::invalid_file_type,
  77. "unsupported object file format");
  78. }
  79. } // end namespace objcopy
  80. } // end namespace llvm