2017-11-01 22:16:06 +01:00
|
|
|
//===- llvm-objcopy.cpp ---------------------------------------------------===//
|
2017-08-01 02:33:58 +02:00
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-08-01 02:33:58 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2017-11-01 22:16:06 +01:00
|
|
|
|
2017-08-01 02:33:58 +02:00
|
|
|
#include "llvm-objcopy.h"
|
2018-10-16 07:40:18 +02:00
|
|
|
#include "Buffer.h"
|
2018-12-19 08:24:38 +01:00
|
|
|
#include "COFF/COFFObjcopy.h"
|
2018-10-12 00:33:50 +02:00
|
|
|
#include "CopyConfig.h"
|
2018-10-29 22:22:58 +01:00
|
|
|
#include "ELF/ELFObjcopy.h"
|
2018-10-12 00:33:50 +02:00
|
|
|
|
2017-11-01 22:16:06 +01:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2018-08-01 18:23:22 +02:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2017-11-01 22:16:06 +01:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
2018-07-06 19:51:03 +02:00
|
|
|
#include "llvm/Object/Archive.h"
|
|
|
|
#include "llvm/Object/ArchiveWriter.h"
|
2017-11-01 22:16:06 +01:00
|
|
|
#include "llvm/Object/Binary.h"
|
2018-12-19 08:24:38 +01:00
|
|
|
#include "llvm/Object/COFF.h"
|
2017-11-01 22:16:06 +01:00
|
|
|
#include "llvm/Object/ELFObjectFile.h"
|
|
|
|
#include "llvm/Object/ELFTypes.h"
|
|
|
|
#include "llvm/Object/Error.h"
|
2018-04-24 07:43:32 +02:00
|
|
|
#include "llvm/Option/Arg.h"
|
|
|
|
#include "llvm/Option/ArgList.h"
|
|
|
|
#include "llvm/Option/Option.h"
|
2017-11-01 22:16:06 +01:00
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/Error.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/ErrorOr.h"
|
2018-04-13 20:26:06 +02:00
|
|
|
#include "llvm/Support/InitLLVM.h"
|
[llvm-objcopy] Add support for -I binary -B <arch>.
Summary:
The -I (--input-target) and -B (--binary-architecture) flags exist but are currently silently ignored. This adds support for -I binary for architectures i386, x86-64 (and alias i386:x86-64), arm, aarch64, sparc, and ppc (powerpc:common64). This is largely based on D41687.
This is done by implementing an additional subclass of Reader, BinaryReader, which works by interpreting the input file as contents for .data field, sets up a synthetic header, and adds additional sections/symbols (e.g. _binary__tmp_data_txt_start).
Reviewers: jakehehrlich, alexshap, jhenderson, javed.absar
Reviewed By: jhenderson
Subscribers: jyknight, nemanjai, kbarton, fedor.sergeev, jrtc27, kristof.beyls, paulsemel, llvm-commits
Differential Revision: https://reviews.llvm.org/D50343
llvm-svn: 340070
2018-08-17 20:51:11 +02:00
|
|
|
#include "llvm/Support/Memory.h"
|
2018-05-07 21:32:09 +02:00
|
|
|
#include "llvm/Support/Path.h"
|
2018-08-16 20:29:40 +02:00
|
|
|
#include "llvm/Support/Process.h"
|
2018-08-10 00:52:03 +02:00
|
|
|
#include "llvm/Support/WithColor.h"
|
2017-11-01 22:16:06 +01:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdlib>
|
2017-08-01 02:33:58 +02:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <system_error>
|
2017-11-01 22:16:06 +01:00
|
|
|
#include <utility>
|
2017-08-01 02:33:58 +02:00
|
|
|
|
2018-07-18 02:10:51 +02:00
|
|
|
namespace llvm {
|
|
|
|
namespace objcopy {
|
|
|
|
|
|
|
|
// The name this program was invoked as.
|
|
|
|
StringRef ToolName;
|
|
|
|
|
|
|
|
LLVM_ATTRIBUTE_NORETURN void error(Twine Message) {
|
2018-08-10 00:52:03 +02:00
|
|
|
WithColor::error(errs(), ToolName) << Message << ".\n";
|
2018-07-18 02:10:51 +02:00
|
|
|
errs().flush();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, std::error_code EC) {
|
|
|
|
assert(EC);
|
2018-08-10 00:52:03 +02:00
|
|
|
WithColor::error(errs(), ToolName)
|
|
|
|
<< "'" << File << "': " << EC.message() << ".\n";
|
2018-07-18 02:10:51 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, Error E) {
|
|
|
|
assert(E);
|
|
|
|
std::string Buf;
|
|
|
|
raw_string_ostream OS(Buf);
|
2018-11-11 02:46:03 +01:00
|
|
|
logAllUnhandledErrors(std::move(E), OS);
|
2018-07-18 02:10:51 +02:00
|
|
|
OS.flush();
|
2018-08-10 00:52:03 +02:00
|
|
|
WithColor::error(errs(), ToolName) << "'" << File << "': " << Buf;
|
2018-07-18 02:10:51 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace objcopy
|
2018-10-25 00:49:06 +02:00
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::object;
|
|
|
|
using namespace llvm::objcopy;
|
|
|
|
|
2018-07-06 19:51:03 +02:00
|
|
|
// For regular archives this function simply calls llvm::writeArchive,
|
|
|
|
// For thin archives it writes the archive file itself as well as its members.
|
2018-07-17 00:17:05 +02:00
|
|
|
static Error deepWriteArchive(StringRef ArcName,
|
|
|
|
ArrayRef<NewArchiveMember> NewMembers,
|
|
|
|
bool WriteSymtab, object::Archive::Kind Kind,
|
|
|
|
bool Deterministic, bool Thin) {
|
2018-07-06 19:51:03 +02:00
|
|
|
Error E =
|
|
|
|
writeArchive(ArcName, NewMembers, WriteSymtab, Kind, Deterministic, Thin);
|
|
|
|
if (!Thin || E)
|
|
|
|
return E;
|
|
|
|
for (const NewArchiveMember &Member : NewMembers) {
|
|
|
|
// Internally, FileBuffer will use the buffer created by
|
|
|
|
// FileOutputBuffer::create, for regular files (that is the case for
|
|
|
|
// deepWriteArchive) FileOutputBuffer::create will return OnDiskBuffer.
|
|
|
|
// OnDiskBuffer uses a temporary file and then renames it. So in reality
|
|
|
|
// there is no inefficiency / duplicated in-memory buffers in this case. For
|
|
|
|
// now in-memory buffers can not be completely avoided since
|
|
|
|
// NewArchiveMember still requires them even though writeArchive does not
|
|
|
|
// write them on disk.
|
|
|
|
FileBuffer FB(Member.MemberName);
|
|
|
|
FB.allocate(Member.Buf->getBufferSize());
|
|
|
|
std::copy(Member.Buf->getBufferStart(), Member.Buf->getBufferEnd(),
|
|
|
|
FB.getBufferStart());
|
|
|
|
if (auto E = FB.commit())
|
|
|
|
return E;
|
|
|
|
}
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2018-10-25 00:49:06 +02:00
|
|
|
/// The function executeObjcopyOnRawBinary does the dispatch based on the format
|
|
|
|
/// of the output specified by the command line options.
|
|
|
|
static void executeObjcopyOnRawBinary(const CopyConfig &Config,
|
|
|
|
MemoryBuffer &In, Buffer &Out) {
|
|
|
|
// TODO: llvm-objcopy should parse CopyConfig.OutputFormat to recognize
|
|
|
|
// formats other than ELF / "binary" and invoke
|
|
|
|
// elf::executeObjcopyOnRawBinary, macho::executeObjcopyOnRawBinary or
|
|
|
|
// coff::executeObjcopyOnRawBinary accordingly.
|
|
|
|
return elf::executeObjcopyOnRawBinary(Config, In, Out);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The function executeObjcopyOnBinary does the dispatch based on the format
|
|
|
|
/// of the input binary (ELF, MachO or COFF).
|
|
|
|
static void executeObjcopyOnBinary(const CopyConfig &Config, object::Binary &In,
|
|
|
|
Buffer &Out) {
|
|
|
|
if (auto *ELFBinary = dyn_cast<object::ELFObjectFileBase>(&In))
|
|
|
|
return elf::executeObjcopyOnBinary(Config, *ELFBinary, Out);
|
2018-12-19 08:24:38 +01:00
|
|
|
else if (auto *COFFBinary = dyn_cast<object::COFFObjectFile>(&In))
|
|
|
|
return coff::executeObjcopyOnBinary(Config, *COFFBinary, Out);
|
2018-10-25 00:49:06 +02:00
|
|
|
else
|
|
|
|
error("Unsupported object file format");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void executeObjcopyOnArchive(const CopyConfig &Config,
|
|
|
|
const Archive &Ar) {
|
2018-07-06 19:51:03 +02:00
|
|
|
std::vector<NewArchiveMember> NewArchiveMembers;
|
|
|
|
Error Err = Error::success();
|
|
|
|
for (const Archive::Child &Child : Ar.children(Err)) {
|
|
|
|
Expected<std::unique_ptr<Binary>> ChildOrErr = Child.getAsBinary();
|
|
|
|
if (!ChildOrErr)
|
|
|
|
reportError(Ar.getFileName(), ChildOrErr.takeError());
|
[llvm-objcopy] Add support for -I binary -B <arch>.
Summary:
The -I (--input-target) and -B (--binary-architecture) flags exist but are currently silently ignored. This adds support for -I binary for architectures i386, x86-64 (and alias i386:x86-64), arm, aarch64, sparc, and ppc (powerpc:common64). This is largely based on D41687.
This is done by implementing an additional subclass of Reader, BinaryReader, which works by interpreting the input file as contents for .data field, sets up a synthetic header, and adds additional sections/symbols (e.g. _binary__tmp_data_txt_start).
Reviewers: jakehehrlich, alexshap, jhenderson, javed.absar
Reviewed By: jhenderson
Subscribers: jyknight, nemanjai, kbarton, fedor.sergeev, jrtc27, kristof.beyls, paulsemel, llvm-commits
Differential Revision: https://reviews.llvm.org/D50343
llvm-svn: 340070
2018-08-17 20:51:11 +02:00
|
|
|
Binary *Bin = ChildOrErr->get();
|
|
|
|
|
2018-07-06 19:51:03 +02:00
|
|
|
Expected<StringRef> ChildNameOrErr = Child.getName();
|
|
|
|
if (!ChildNameOrErr)
|
|
|
|
reportError(Ar.getFileName(), ChildNameOrErr.takeError());
|
|
|
|
|
|
|
|
MemBuffer MB(ChildNameOrErr.get());
|
2018-10-25 00:49:06 +02:00
|
|
|
executeObjcopyOnBinary(Config, *Bin, MB);
|
2018-07-06 19:51:03 +02:00
|
|
|
|
|
|
|
Expected<NewArchiveMember> Member =
|
2018-11-01 18:36:37 +01:00
|
|
|
NewArchiveMember::getOldMember(Child, Config.DeterministicArchives);
|
2018-07-06 19:51:03 +02:00
|
|
|
if (!Member)
|
|
|
|
reportError(Ar.getFileName(), Member.takeError());
|
|
|
|
Member->Buf = MB.releaseMemoryBuffer();
|
|
|
|
Member->MemberName = Member->Buf->getBufferIdentifier();
|
|
|
|
NewArchiveMembers.push_back(std::move(*Member));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Err)
|
|
|
|
reportError(Config.InputFilename, std::move(Err));
|
2018-11-01 18:36:37 +01:00
|
|
|
if (Error E = deepWriteArchive(Config.OutputFilename, NewArchiveMembers,
|
|
|
|
Ar.hasSymbolTable(), Ar.kind(),
|
|
|
|
Config.DeterministicArchives, Ar.isThin()))
|
2018-07-06 19:51:03 +02:00
|
|
|
reportError(Config.OutputFilename, std::move(E));
|
|
|
|
}
|
|
|
|
|
2018-08-16 20:29:40 +02:00
|
|
|
static void restoreDateOnFile(StringRef Filename,
|
|
|
|
const sys::fs::file_status &Stat) {
|
|
|
|
int FD;
|
|
|
|
|
2018-08-30 01:21:56 +02:00
|
|
|
if (auto EC =
|
|
|
|
sys::fs::openFileForWrite(Filename, FD, sys::fs::CD_OpenExisting))
|
2018-08-16 20:29:40 +02:00
|
|
|
reportError(Filename, EC);
|
|
|
|
|
|
|
|
if (auto EC = sys::fs::setLastAccessAndModificationTime(
|
|
|
|
FD, Stat.getLastAccessedTime(), Stat.getLastModificationTime()))
|
|
|
|
reportError(Filename, EC);
|
|
|
|
|
|
|
|
if (auto EC = sys::Process::SafelyCloseFileDescriptor(FD))
|
|
|
|
reportError(Filename, EC);
|
|
|
|
}
|
|
|
|
|
2018-10-25 00:49:06 +02:00
|
|
|
/// 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 void executeObjcopy(const CopyConfig &Config) {
|
2018-08-16 20:29:40 +02:00
|
|
|
sys::fs::file_status Stat;
|
|
|
|
if (Config.PreserveDates)
|
|
|
|
if (auto EC = sys::fs::status(Config.InputFilename, Stat))
|
|
|
|
reportError(Config.InputFilename, EC);
|
|
|
|
|
[llvm-objcopy] Add support for -I binary -B <arch>.
Summary:
The -I (--input-target) and -B (--binary-architecture) flags exist but are currently silently ignored. This adds support for -I binary for architectures i386, x86-64 (and alias i386:x86-64), arm, aarch64, sparc, and ppc (powerpc:common64). This is largely based on D41687.
This is done by implementing an additional subclass of Reader, BinaryReader, which works by interpreting the input file as contents for .data field, sets up a synthetic header, and adds additional sections/symbols (e.g. _binary__tmp_data_txt_start).
Reviewers: jakehehrlich, alexshap, jhenderson, javed.absar
Reviewed By: jhenderson
Subscribers: jyknight, nemanjai, kbarton, fedor.sergeev, jrtc27, kristof.beyls, paulsemel, llvm-commits
Differential Revision: https://reviews.llvm.org/D50343
llvm-svn: 340070
2018-08-17 20:51:11 +02:00
|
|
|
if (Config.InputFormat == "binary") {
|
|
|
|
auto BufOrErr = MemoryBuffer::getFile(Config.InputFilename);
|
|
|
|
if (!BufOrErr)
|
|
|
|
reportError(Config.InputFilename, BufOrErr.getError());
|
2018-08-16 20:29:40 +02:00
|
|
|
FileBuffer FB(Config.OutputFilename);
|
2018-10-25 00:49:06 +02:00
|
|
|
executeObjcopyOnRawBinary(Config, *BufOrErr->get(), FB);
|
[llvm-objcopy] Add support for -I binary -B <arch>.
Summary:
The -I (--input-target) and -B (--binary-architecture) flags exist but are currently silently ignored. This adds support for -I binary for architectures i386, x86-64 (and alias i386:x86-64), arm, aarch64, sparc, and ppc (powerpc:common64). This is largely based on D41687.
This is done by implementing an additional subclass of Reader, BinaryReader, which works by interpreting the input file as contents for .data field, sets up a synthetic header, and adds additional sections/symbols (e.g. _binary__tmp_data_txt_start).
Reviewers: jakehehrlich, alexshap, jhenderson, javed.absar
Reviewed By: jhenderson
Subscribers: jyknight, nemanjai, kbarton, fedor.sergeev, jrtc27, kristof.beyls, paulsemel, llvm-commits
Differential Revision: https://reviews.llvm.org/D50343
llvm-svn: 340070
2018-08-17 20:51:11 +02:00
|
|
|
} else {
|
|
|
|
Expected<OwningBinary<llvm::object::Binary>> BinaryOrErr =
|
|
|
|
createBinary(Config.InputFilename);
|
|
|
|
if (!BinaryOrErr)
|
|
|
|
reportError(Config.InputFilename, BinaryOrErr.takeError());
|
|
|
|
|
|
|
|
if (Archive *Ar = dyn_cast<Archive>(BinaryOrErr.get().getBinary())) {
|
2018-10-25 00:49:06 +02:00
|
|
|
executeObjcopyOnArchive(Config, *Ar);
|
[llvm-objcopy] Add support for -I binary -B <arch>.
Summary:
The -I (--input-target) and -B (--binary-architecture) flags exist but are currently silently ignored. This adds support for -I binary for architectures i386, x86-64 (and alias i386:x86-64), arm, aarch64, sparc, and ppc (powerpc:common64). This is largely based on D41687.
This is done by implementing an additional subclass of Reader, BinaryReader, which works by interpreting the input file as contents for .data field, sets up a synthetic header, and adds additional sections/symbols (e.g. _binary__tmp_data_txt_start).
Reviewers: jakehehrlich, alexshap, jhenderson, javed.absar
Reviewed By: jhenderson
Subscribers: jyknight, nemanjai, kbarton, fedor.sergeev, jrtc27, kristof.beyls, paulsemel, llvm-commits
Differential Revision: https://reviews.llvm.org/D50343
llvm-svn: 340070
2018-08-17 20:51:11 +02:00
|
|
|
} else {
|
|
|
|
FileBuffer FB(Config.OutputFilename);
|
2018-10-25 00:49:06 +02:00
|
|
|
executeObjcopyOnBinary(Config, *BinaryOrErr.get().getBinary(), FB);
|
[llvm-objcopy] Add support for -I binary -B <arch>.
Summary:
The -I (--input-target) and -B (--binary-architecture) flags exist but are currently silently ignored. This adds support for -I binary for architectures i386, x86-64 (and alias i386:x86-64), arm, aarch64, sparc, and ppc (powerpc:common64). This is largely based on D41687.
This is done by implementing an additional subclass of Reader, BinaryReader, which works by interpreting the input file as contents for .data field, sets up a synthetic header, and adds additional sections/symbols (e.g. _binary__tmp_data_txt_start).
Reviewers: jakehehrlich, alexshap, jhenderson, javed.absar
Reviewed By: jhenderson
Subscribers: jyknight, nemanjai, kbarton, fedor.sergeev, jrtc27, kristof.beyls, paulsemel, llvm-commits
Differential Revision: https://reviews.llvm.org/D50343
llvm-svn: 340070
2018-08-17 20:51:11 +02:00
|
|
|
}
|
2018-08-16 20:29:40 +02:00
|
|
|
}
|
2018-07-06 19:51:03 +02:00
|
|
|
|
2018-08-16 20:29:40 +02:00
|
|
|
if (Config.PreserveDates) {
|
|
|
|
restoreDateOnFile(Config.OutputFilename, Stat);
|
|
|
|
if (!Config.SplitDWO.empty())
|
|
|
|
restoreDateOnFile(Config.SplitDWO, Stat);
|
|
|
|
}
|
2018-07-06 19:51:03 +02:00
|
|
|
}
|
|
|
|
|
2017-08-01 02:33:58 +02:00
|
|
|
int main(int argc, char **argv) {
|
2018-04-13 20:26:06 +02:00
|
|
|
InitLLVM X(argc, argv);
|
2017-08-01 02:33:58 +02:00
|
|
|
ToolName = argv[0];
|
2018-09-05 15:10:03 +02:00
|
|
|
DriverConfig DriverConfig;
|
2018-11-07 04:02:11 +01:00
|
|
|
if (sys::path::stem(ToolName).contains("strip"))
|
2018-09-05 15:10:03 +02:00
|
|
|
DriverConfig = parseStripOptions(makeArrayRef(argv + 1, argc));
|
2018-05-07 21:32:09 +02:00
|
|
|
else
|
2018-09-05 15:10:03 +02:00
|
|
|
DriverConfig = parseObjcopyOptions(makeArrayRef(argv + 1, argc));
|
|
|
|
for (const CopyConfig &CopyConfig : DriverConfig.CopyConfigs)
|
2018-10-25 00:49:06 +02:00
|
|
|
executeObjcopy(CopyConfig);
|
2017-08-01 02:33:58 +02:00
|
|
|
}
|