xref: /llvm-project/llvm/lib/ObjCopy/ObjCopy.cpp (revision 54dad9e269f365d0eff2f63c5ee843564eecca7e)
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/ELF/ELFConfig.h"
13f75da0c8SAlexey Lapshin #include "llvm/ObjCopy/ELF/ELFObjcopy.h"
14f75da0c8SAlexey Lapshin #include "llvm/ObjCopy/MachO/MachOConfig.h"
15f75da0c8SAlexey Lapshin #include "llvm/ObjCopy/MachO/MachOObjcopy.h"
16f75da0c8SAlexey Lapshin #include "llvm/ObjCopy/MultiFormatConfig.h"
1761835d19Sesmeyi #include "llvm/ObjCopy/XCOFF/XCOFFConfig.h"
1861835d19Sesmeyi #include "llvm/ObjCopy/XCOFF/XCOFFObjcopy.h"
19*54dad9e2SKazu Hirata #include "llvm/ObjCopy/wasm/WasmConfig.h"
20*54dad9e2SKazu Hirata #include "llvm/ObjCopy/wasm/WasmObjcopy.h"
21f75da0c8SAlexey Lapshin #include "llvm/Object/COFF.h"
22f75da0c8SAlexey Lapshin #include "llvm/Object/ELFObjectFile.h"
23f75da0c8SAlexey Lapshin #include "llvm/Object/Error.h"
24f75da0c8SAlexey Lapshin #include "llvm/Object/MachO.h"
25f75da0c8SAlexey Lapshin #include "llvm/Object/MachOUniversal.h"
26f75da0c8SAlexey Lapshin #include "llvm/Object/Wasm.h"
2761835d19Sesmeyi #include "llvm/Object/XCOFFObjectFile.h"
28f75da0c8SAlexey Lapshin 
29f75da0c8SAlexey Lapshin namespace llvm {
30f75da0c8SAlexey Lapshin namespace objcopy {
31f75da0c8SAlexey Lapshin 
32f75da0c8SAlexey Lapshin using namespace llvm::object;
33f75da0c8SAlexey Lapshin 
34f75da0c8SAlexey Lapshin /// The function executeObjcopyOnBinary does the dispatch based on the format
35f75da0c8SAlexey Lapshin /// of the input binary (ELF, MachO or COFF).
36f75da0c8SAlexey Lapshin Error executeObjcopyOnBinary(const MultiFormatConfig &Config,
37f75da0c8SAlexey Lapshin                              object::Binary &In, raw_ostream &Out) {
38f75da0c8SAlexey Lapshin   if (auto *ELFBinary = dyn_cast<object::ELFObjectFileBase>(&In)) {
39f75da0c8SAlexey Lapshin     Expected<const ELFConfig &> ELFConfig = Config.getELFConfig();
40f75da0c8SAlexey Lapshin     if (!ELFConfig)
41f75da0c8SAlexey Lapshin       return ELFConfig.takeError();
42f75da0c8SAlexey Lapshin 
43f75da0c8SAlexey Lapshin     return elf::executeObjcopyOnBinary(Config.getCommonConfig(), *ELFConfig,
44f75da0c8SAlexey Lapshin                                        *ELFBinary, Out);
45f75da0c8SAlexey Lapshin   }
46f75da0c8SAlexey Lapshin   if (auto *COFFBinary = dyn_cast<object::COFFObjectFile>(&In)) {
47f75da0c8SAlexey Lapshin     Expected<const COFFConfig &> COFFConfig = Config.getCOFFConfig();
48f75da0c8SAlexey Lapshin     if (!COFFConfig)
49f75da0c8SAlexey Lapshin       return COFFConfig.takeError();
50f75da0c8SAlexey Lapshin 
51f75da0c8SAlexey Lapshin     return coff::executeObjcopyOnBinary(Config.getCommonConfig(), *COFFConfig,
52f75da0c8SAlexey Lapshin                                         *COFFBinary, Out);
53f75da0c8SAlexey Lapshin   }
54f75da0c8SAlexey Lapshin   if (auto *MachOBinary = dyn_cast<object::MachOObjectFile>(&In)) {
55f75da0c8SAlexey Lapshin     Expected<const MachOConfig &> MachOConfig = Config.getMachOConfig();
56f75da0c8SAlexey Lapshin     if (!MachOConfig)
57f75da0c8SAlexey Lapshin       return MachOConfig.takeError();
58f75da0c8SAlexey Lapshin 
59f75da0c8SAlexey Lapshin     return macho::executeObjcopyOnBinary(Config.getCommonConfig(), *MachOConfig,
60f75da0c8SAlexey Lapshin                                          *MachOBinary, Out);
61f75da0c8SAlexey Lapshin   }
62f75da0c8SAlexey Lapshin   if (auto *MachOUniversalBinary =
63f75da0c8SAlexey Lapshin           dyn_cast<object::MachOUniversalBinary>(&In)) {
64f75da0c8SAlexey Lapshin     return macho::executeObjcopyOnMachOUniversalBinary(
65f75da0c8SAlexey Lapshin         Config, *MachOUniversalBinary, Out);
66f75da0c8SAlexey Lapshin   }
67f75da0c8SAlexey Lapshin   if (auto *WasmBinary = dyn_cast<object::WasmObjectFile>(&In)) {
68f75da0c8SAlexey Lapshin     Expected<const WasmConfig &> WasmConfig = Config.getWasmConfig();
69f75da0c8SAlexey Lapshin     if (!WasmConfig)
70f75da0c8SAlexey Lapshin       return WasmConfig.takeError();
71f75da0c8SAlexey Lapshin 
72f75da0c8SAlexey Lapshin     return objcopy::wasm::executeObjcopyOnBinary(Config.getCommonConfig(),
73f75da0c8SAlexey Lapshin                                                  *WasmConfig, *WasmBinary, Out);
74f75da0c8SAlexey Lapshin   }
7561835d19Sesmeyi   if (auto *XCOFFBinary = dyn_cast<object::XCOFFObjectFile>(&In)) {
7661835d19Sesmeyi     Expected<const XCOFFConfig &> XCOFFConfig = Config.getXCOFFConfig();
7761835d19Sesmeyi     if (!XCOFFConfig)
7861835d19Sesmeyi       return XCOFFConfig.takeError();
7961835d19Sesmeyi 
8061835d19Sesmeyi     return xcoff::executeObjcopyOnBinary(Config.getCommonConfig(), *XCOFFConfig,
8161835d19Sesmeyi                                          *XCOFFBinary, Out);
8261835d19Sesmeyi   }
83f75da0c8SAlexey Lapshin   return createStringError(object_error::invalid_file_type,
84f75da0c8SAlexey Lapshin                            "unsupported object file format");
85f75da0c8SAlexey Lapshin }
86f75da0c8SAlexey Lapshin 
87f75da0c8SAlexey Lapshin } // end namespace objcopy
88f75da0c8SAlexey Lapshin } // end namespace llvm
89