1f75da0c8SAlexey Lapshin //===- Objcopy.cpp --------------------------------------------------------===//
2f75da0c8SAlexey Lapshin //
3f75da0c8SAlexey Lapshin // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4f75da0c8SAlexey Lapshin // See https://llvm.org/LICENSE.txt for license information.
5f75da0c8SAlexey Lapshin // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f75da0c8SAlexey Lapshin //
7f75da0c8SAlexey Lapshin //===----------------------------------------------------------------------===//
8f75da0c8SAlexey Lapshin
9f75da0c8SAlexey Lapshin #include "llvm/ObjCopy/ObjCopy.h"
10f75da0c8SAlexey Lapshin #include "llvm/ObjCopy/COFF/COFFConfig.h"
11f75da0c8SAlexey Lapshin #include "llvm/ObjCopy/COFF/COFFObjcopy.h"
12f75da0c8SAlexey Lapshin #include "llvm/ObjCopy/CommonConfig.h"
13f75da0c8SAlexey Lapshin #include "llvm/ObjCopy/ELF/ELFConfig.h"
14f75da0c8SAlexey Lapshin #include "llvm/ObjCopy/ELF/ELFObjcopy.h"
15f75da0c8SAlexey Lapshin #include "llvm/ObjCopy/MachO/MachOConfig.h"
16f75da0c8SAlexey Lapshin #include "llvm/ObjCopy/MachO/MachOObjcopy.h"
17f75da0c8SAlexey Lapshin #include "llvm/ObjCopy/MultiFormatConfig.h"
18f75da0c8SAlexey Lapshin #include "llvm/ObjCopy/wasm/WasmConfig.h"
19f75da0c8SAlexey Lapshin #include "llvm/ObjCopy/wasm/WasmObjcopy.h"
20*61835d19Sesmeyi #include "llvm/ObjCopy/XCOFF/XCOFFConfig.h"
21*61835d19Sesmeyi #include "llvm/ObjCopy/XCOFF/XCOFFObjcopy.h"
22f75da0c8SAlexey Lapshin #include "llvm/Object/COFF.h"
23f75da0c8SAlexey Lapshin #include "llvm/Object/ELFObjectFile.h"
24f75da0c8SAlexey Lapshin #include "llvm/Object/Error.h"
25f75da0c8SAlexey Lapshin #include "llvm/Object/MachO.h"
26f75da0c8SAlexey Lapshin #include "llvm/Object/MachOUniversal.h"
27f75da0c8SAlexey Lapshin #include "llvm/Object/Wasm.h"
28*61835d19Sesmeyi #include "llvm/Object/XCOFFObjectFile.h"
29f75da0c8SAlexey Lapshin #include "llvm/Support/SmallVectorMemoryBuffer.h"
30f75da0c8SAlexey Lapshin
31f75da0c8SAlexey Lapshin namespace llvm {
32f75da0c8SAlexey Lapshin namespace objcopy {
33f75da0c8SAlexey Lapshin
34f75da0c8SAlexey Lapshin using namespace llvm::object;
35f75da0c8SAlexey Lapshin
36f75da0c8SAlexey Lapshin /// The function executeObjcopyOnBinary does the dispatch based on the format
37f75da0c8SAlexey Lapshin /// of the input binary (ELF, MachO or COFF).
executeObjcopyOnBinary(const MultiFormatConfig & Config,object::Binary & In,raw_ostream & Out)38f75da0c8SAlexey Lapshin Error executeObjcopyOnBinary(const MultiFormatConfig &Config,
39f75da0c8SAlexey Lapshin object::Binary &In, raw_ostream &Out) {
40f75da0c8SAlexey Lapshin if (auto *ELFBinary = dyn_cast<object::ELFObjectFileBase>(&In)) {
41f75da0c8SAlexey Lapshin Expected<const ELFConfig &> ELFConfig = Config.getELFConfig();
42f75da0c8SAlexey Lapshin if (!ELFConfig)
43f75da0c8SAlexey Lapshin return ELFConfig.takeError();
44f75da0c8SAlexey Lapshin
45f75da0c8SAlexey Lapshin return elf::executeObjcopyOnBinary(Config.getCommonConfig(), *ELFConfig,
46f75da0c8SAlexey Lapshin *ELFBinary, Out);
47f75da0c8SAlexey Lapshin }
48f75da0c8SAlexey Lapshin if (auto *COFFBinary = dyn_cast<object::COFFObjectFile>(&In)) {
49f75da0c8SAlexey Lapshin Expected<const COFFConfig &> COFFConfig = Config.getCOFFConfig();
50f75da0c8SAlexey Lapshin if (!COFFConfig)
51f75da0c8SAlexey Lapshin return COFFConfig.takeError();
52f75da0c8SAlexey Lapshin
53f75da0c8SAlexey Lapshin return coff::executeObjcopyOnBinary(Config.getCommonConfig(), *COFFConfig,
54f75da0c8SAlexey Lapshin *COFFBinary, Out);
55f75da0c8SAlexey Lapshin }
56f75da0c8SAlexey Lapshin if (auto *MachOBinary = dyn_cast<object::MachOObjectFile>(&In)) {
57f75da0c8SAlexey Lapshin Expected<const MachOConfig &> MachOConfig = Config.getMachOConfig();
58f75da0c8SAlexey Lapshin if (!MachOConfig)
59f75da0c8SAlexey Lapshin return MachOConfig.takeError();
60f75da0c8SAlexey Lapshin
61f75da0c8SAlexey Lapshin return macho::executeObjcopyOnBinary(Config.getCommonConfig(), *MachOConfig,
62f75da0c8SAlexey Lapshin *MachOBinary, Out);
63f75da0c8SAlexey Lapshin }
64f75da0c8SAlexey Lapshin if (auto *MachOUniversalBinary =
65f75da0c8SAlexey Lapshin dyn_cast<object::MachOUniversalBinary>(&In)) {
66f75da0c8SAlexey Lapshin return macho::executeObjcopyOnMachOUniversalBinary(
67f75da0c8SAlexey Lapshin Config, *MachOUniversalBinary, Out);
68f75da0c8SAlexey Lapshin }
69f75da0c8SAlexey Lapshin if (auto *WasmBinary = dyn_cast<object::WasmObjectFile>(&In)) {
70f75da0c8SAlexey Lapshin Expected<const WasmConfig &> WasmConfig = Config.getWasmConfig();
71f75da0c8SAlexey Lapshin if (!WasmConfig)
72f75da0c8SAlexey Lapshin return WasmConfig.takeError();
73f75da0c8SAlexey Lapshin
74f75da0c8SAlexey Lapshin return objcopy::wasm::executeObjcopyOnBinary(Config.getCommonConfig(),
75f75da0c8SAlexey Lapshin *WasmConfig, *WasmBinary, Out);
76f75da0c8SAlexey Lapshin }
77*61835d19Sesmeyi if (auto *XCOFFBinary = dyn_cast<object::XCOFFObjectFile>(&In)) {
78*61835d19Sesmeyi Expected<const XCOFFConfig &> XCOFFConfig = Config.getXCOFFConfig();
79*61835d19Sesmeyi if (!XCOFFConfig)
80*61835d19Sesmeyi return XCOFFConfig.takeError();
81*61835d19Sesmeyi
82*61835d19Sesmeyi return xcoff::executeObjcopyOnBinary(Config.getCommonConfig(), *XCOFFConfig,
83*61835d19Sesmeyi *XCOFFBinary, Out);
84*61835d19Sesmeyi }
85f75da0c8SAlexey Lapshin return createStringError(object_error::invalid_file_type,
86f75da0c8SAlexey Lapshin "unsupported object file format");
87f75da0c8SAlexey Lapshin }
88f75da0c8SAlexey Lapshin
89f75da0c8SAlexey Lapshin } // end namespace objcopy
90f75da0c8SAlexey Lapshin } // end namespace llvm
91