aboutsummaryrefslogtreecommitdiff
path: root/llvm/tools/llvm-objcopy/llvm-objcopy.cpp
diff options
context:
space:
mode:
authorAlexey Lapshin <a.v.lapshin@mail.ru>2021-04-23 14:19:11 +0300
committerAlexey Lapshin <a.v.lapshin@mail.ru>2021-05-20 13:14:51 +0300
commit081c62501e4f0aad4ab31de52f871f98403073ad (patch)
treeb7e40533b7ef782654be5d254390dc2be3c2ad78 /llvm/tools/llvm-objcopy/llvm-objcopy.cpp
parenteeeeff0d7bbb0b881213be24c0e33de6b755fe64 (diff)
downloadllvm-081c62501e4f0aad4ab31de52f871f98403073ad.zip
llvm-081c62501e4f0aad4ab31de52f871f98403073ad.tar.gz
llvm-081c62501e4f0aad4ab31de52f871f98403073ad.tar.bz2
[llvm-objcopy] Refactor CopyConfig structure.
This patch prepares llvm-objcopy to move its implementation into a separate library. To make it possible it is necessary to minimize internal dependencies. Differential Revision: https://reviews.llvm.org/D99055
Diffstat (limited to 'llvm/tools/llvm-objcopy/llvm-objcopy.cpp')
-rw-r--r--llvm/tools/llvm-objcopy/llvm-objcopy.cpp115
1 files changed, 75 insertions, 40 deletions
diff --git a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp
index d3cc947..af1e903 100644
--- a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp
+++ b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp
@@ -7,10 +7,15 @@
//===----------------------------------------------------------------------===//
#include "llvm-objcopy.h"
+#include "COFF/COFFConfig.h"
#include "COFF/COFFObjcopy.h"
-#include "CopyConfig.h"
+#include "CommonConfig.h"
+#include "ConfigManager.h"
+#include "ELF/ELFConfig.h"
#include "ELF/ELFObjcopy.h"
+#include "MachO/MachOConfig.h"
#include "MachO/MachOObjcopy.h"
+#include "wasm/WasmConfig.h"
#include "wasm/WasmObjcopy.h"
#include "llvm/ADT/STLExtras.h"
@@ -133,18 +138,22 @@ static Error deepWriteArchive(StringRef ArcName,
/// The function executeObjcopyOnIHex does the dispatch based on the format
/// of the output specified by the command line options.
-static Error executeObjcopyOnIHex(CopyConfig &Config, MemoryBuffer &In,
+static Error executeObjcopyOnIHex(ConfigManager &ConfigMgr, MemoryBuffer &In,
raw_ostream &Out) {
// TODO: support output formats other than ELF.
- if (Error E = Config.parseELFConfig())
- return E;
- return elf::executeObjcopyOnIHex(Config, In, Out);
+ Expected<const ELFConfig &> ELFConfig = ConfigMgr.getELFConfig();
+ if (!ELFConfig)
+ return ELFConfig.takeError();
+
+ return elf::executeObjcopyOnIHex(ConfigMgr.getCommonConfig(), *ELFConfig, In,
+ Out);
}
/// The function executeObjcopyOnRawBinary does the dispatch based on the format
/// of the output specified by the command line options.
-static Error executeObjcopyOnRawBinary(CopyConfig &Config, MemoryBuffer &In,
- raw_ostream &Out) {
+static Error executeObjcopyOnRawBinary(ConfigManager &ConfigMgr,
+ MemoryBuffer &In, raw_ostream &Out) {
+ const CommonConfig &Config = ConfigMgr.getCommonConfig();
switch (Config.OutputFormat) {
case FileFormat::ELF:
// FIXME: Currently, we call elf::executeObjcopyOnRawBinary even if the
@@ -153,9 +162,11 @@ static Error executeObjcopyOnRawBinary(CopyConfig &Config, MemoryBuffer &In,
case FileFormat::Binary:
case FileFormat::IHex:
case FileFormat::Unspecified:
- if (Error E = Config.parseELFConfig())
- return E;
- return elf::executeObjcopyOnRawBinary(Config, In, Out);
+ Expected<const ELFConfig &> ELFConfig = ConfigMgr.getELFConfig();
+ if (!ELFConfig)
+ return ELFConfig.takeError();
+
+ return elf::executeObjcopyOnRawBinary(Config, *ELFConfig, In, Out);
}
llvm_unreachable("unsupported output format");
@@ -163,23 +174,41 @@ static Error executeObjcopyOnRawBinary(CopyConfig &Config, MemoryBuffer &In,
/// The function executeObjcopyOnBinary does the dispatch based on the format
/// of the input binary (ELF, MachO or COFF).
-static Error executeObjcopyOnBinary(CopyConfig &Config, object::Binary &In,
- raw_ostream &Out) {
+static Error executeObjcopyOnBinary(const MultiFormatConfig &Config,
+ object::Binary &In, raw_ostream &Out) {
if (auto *ELFBinary = dyn_cast<object::ELFObjectFileBase>(&In)) {
- if (Error E = Config.parseELFConfig())
- return E;
- return elf::executeObjcopyOnBinary(Config, *ELFBinary, Out);
- } else if (auto *COFFBinary = dyn_cast<object::COFFObjectFile>(&In))
- return coff::executeObjcopyOnBinary(Config, *COFFBinary, Out);
- else if (auto *MachOBinary = dyn_cast<object::MachOObjectFile>(&In))
- return macho::executeObjcopyOnBinary(Config, *MachOBinary, Out);
- else if (auto *MachOUniversalBinary =
- dyn_cast<object::MachOUniversalBinary>(&In))
+ Expected<const ELFConfig &> ELFConfig = Config.getELFConfig();
+ if (!ELFConfig)
+ return ELFConfig.takeError();
+
+ return elf::executeObjcopyOnBinary(Config.getCommonConfig(), *ELFConfig,
+ *ELFBinary, Out);
+ } else if (auto *COFFBinary = dyn_cast<object::COFFObjectFile>(&In)) {
+ Expected<const COFFConfig &> COFFConfig = Config.getCOFFConfig();
+ if (!COFFConfig)
+ return COFFConfig.takeError();
+
+ return coff::executeObjcopyOnBinary(Config.getCommonConfig(), *COFFConfig,
+ *COFFBinary, Out);
+ } else if (auto *MachOBinary = dyn_cast<object::MachOObjectFile>(&In)) {
+ Expected<const MachOConfig &> MachOConfig = Config.getMachOConfig();
+ if (!MachOConfig)
+ return MachOConfig.takeError();
+
+ return macho::executeObjcopyOnBinary(Config.getCommonConfig(), *MachOConfig,
+ *MachOBinary, Out);
+ } else if (auto *MachOUniversalBinary =
+ dyn_cast<object::MachOUniversalBinary>(&In)) {
return macho::executeObjcopyOnMachOUniversalBinary(
Config, *MachOUniversalBinary, Out);
- else if (auto *WasmBinary = dyn_cast<object::WasmObjectFile>(&In))
- return objcopy::wasm::executeObjcopyOnBinary(Config, *WasmBinary, Out);
- else
+ } else if (auto *WasmBinary = dyn_cast<object::WasmObjectFile>(&In)) {
+ Expected<const WasmConfig &> WasmConfig = Config.getWasmConfig();
+ if (!WasmConfig)
+ return WasmConfig.takeError();
+
+ return objcopy::wasm::executeObjcopyOnBinary(Config.getCommonConfig(),
+ *WasmConfig, *WasmBinary, Out);
+ } else
return createStringError(object_error::invalid_file_type,
"unsupported object file format");
}
@@ -188,7 +217,7 @@ namespace llvm {
namespace objcopy {
Expected<std::vector<NewArchiveMember>>
-createNewArchiveMembers(CopyConfig &Config, const Archive &Ar) {
+createNewArchiveMembers(const MultiFormatConfig &Config, const Archive &Ar) {
std::vector<NewArchiveMember> NewArchiveMembers;
Error Err = Error::success();
for (const Archive::Child &Child : Ar.children(Err)) {
@@ -207,8 +236,8 @@ createNewArchiveMembers(CopyConfig &Config, const Archive &Ar) {
if (Error E = executeObjcopyOnBinary(Config, *ChildOrErr->get(), MemStream))
return std::move(E);
- Expected<NewArchiveMember> Member =
- NewArchiveMember::getOldMember(Child, Config.DeterministicArchives);
+ Expected<NewArchiveMember> Member = NewArchiveMember::getOldMember(
+ Child, Config.getCommonConfig().DeterministicArchives);
if (!Member)
return createFileError(Ar.getFileName(), Member.takeError());
@@ -218,19 +247,21 @@ createNewArchiveMembers(CopyConfig &Config, const Archive &Ar) {
NewArchiveMembers.push_back(std::move(*Member));
}
if (Err)
- return createFileError(Config.InputFilename, std::move(Err));
+ return createFileError(Config.getCommonConfig().InputFilename,
+ std::move(Err));
return std::move(NewArchiveMembers);
}
} // end namespace objcopy
} // end namespace llvm
-static Error executeObjcopyOnArchive(CopyConfig &Config,
+static Error executeObjcopyOnArchive(const ConfigManager &ConfigMgr,
const object::Archive &Ar) {
Expected<std::vector<NewArchiveMember>> NewArchiveMembersOrErr =
- createNewArchiveMembers(Config, Ar);
+ createNewArchiveMembers(ConfigMgr, Ar);
if (!NewArchiveMembersOrErr)
return NewArchiveMembersOrErr.takeError();
+ const CommonConfig &Config = ConfigMgr.getCommonConfig();
return deepWriteArchive(Config.OutputFilename, *NewArchiveMembersOrErr,
Ar.hasSymbolTable(), Ar.kind(),
Config.DeterministicArchives, Ar.isThin());
@@ -238,8 +269,9 @@ static Error executeObjcopyOnArchive(CopyConfig &Config,
static Error restoreStatOnFile(StringRef Filename,
const sys::fs::file_status &Stat,
- const CopyConfig &Config) {
+ const ConfigManager &ConfigMgr) {
int FD;
+ const CommonConfig &Config = ConfigMgr.getCommonConfig();
// Writing to stdout should not be treated as an error here, just
// do not set access/modification times or permissions.
@@ -285,7 +317,9 @@ static Error restoreStatOnFile(StringRef Filename,
/// The function executeObjcopy does the higher level dispatch based on the type
/// of input (raw binary, archive or single object file) and takes care of the
/// format-agnostic modifications, i.e. preserving dates.
-static Error executeObjcopy(CopyConfig &Config) {
+static Error executeObjcopy(ConfigManager &ConfigMgr) {
+ CommonConfig &Config = ConfigMgr.Common;
+
sys::fs::file_status Stat;
if (Config.InputFilename != "-") {
if (auto EC = sys::fs::status(Config.InputFilename, Stat))
@@ -310,12 +344,13 @@ static Error executeObjcopy(CopyConfig &Config) {
if (Config.InputFormat == FileFormat::Binary)
ObjcopyFunc = [&](raw_ostream &OutFile) -> Error {
// Handle FileFormat::Binary.
- return executeObjcopyOnRawBinary(Config, *MemoryBufferHolder, OutFile);
+ return executeObjcopyOnRawBinary(ConfigMgr, *MemoryBufferHolder,
+ OutFile);
};
else
ObjcopyFunc = [&](raw_ostream &OutFile) -> Error {
// Handle FileFormat::IHex.
- return executeObjcopyOnIHex(Config, *MemoryBufferHolder, OutFile);
+ return executeObjcopyOnIHex(ConfigMgr, *MemoryBufferHolder, OutFile);
};
} else {
Expected<OwningBinary<llvm::object::Binary>> BinaryOrErr =
@@ -326,12 +361,12 @@ static Error executeObjcopy(CopyConfig &Config) {
if (Archive *Ar = dyn_cast<Archive>(BinaryHolder.getBinary())) {
// Handle Archive.
- if (Error E = executeObjcopyOnArchive(Config, *Ar))
+ if (Error E = executeObjcopyOnArchive(ConfigMgr, *Ar))
return E;
} else {
// Handle llvm::object::Binary.
ObjcopyFunc = [&](raw_ostream &OutFile) -> Error {
- return executeObjcopyOnBinary(Config, *BinaryHolder.getBinary(),
+ return executeObjcopyOnBinary(ConfigMgr, *BinaryHolder.getBinary(),
OutFile);
};
}
@@ -360,12 +395,12 @@ static Error executeObjcopy(CopyConfig &Config) {
}
}
- if (Error E = restoreStatOnFile(Config.OutputFilename, Stat, Config))
+ if (Error E = restoreStatOnFile(Config.OutputFilename, Stat, ConfigMgr))
return E;
if (!Config.SplitDWO.empty()) {
Stat.permissions(static_cast<sys::fs::perms>(0666));
- if (Error E = restoreStatOnFile(Config.SplitDWO, Stat, Config))
+ if (Error E = restoreStatOnFile(Config.SplitDWO, Stat, ConfigMgr))
return E;
}
@@ -401,8 +436,8 @@ int main(int argc, char **argv) {
WithColor::error(errs(), ToolName));
return 1;
}
- for (CopyConfig &CopyConfig : DriverConfig->CopyConfigs) {
- if (Error E = executeObjcopy(CopyConfig)) {
+ for (ConfigManager &ConfigMgr : DriverConfig->CopyConfigs) {
+ if (Error E = executeObjcopy(ConfigMgr)) {
logAllUnhandledErrors(std::move(E), WithColor::error(errs(), ToolName));
return 1;
}