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