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-10-12 00:33:50 +02:00
|
|
|
#include "CopyConfig.h"
|
2018-10-29 22:22:58 +01:00
|
|
|
#include "ELF/ELFObjcopy.h"
|
2019-06-01 09:36:57 +02:00
|
|
|
#include "COFF/COFFObjcopy.h"
|
2019-02-02 01:38:07 +01:00
|
|
|
#include "MachO/MachOObjcopy.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"
|
2019-02-02 01:38:07 +01:00
|
|
|
#include "llvm/Object/MachO.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) {
|
2019-06-14 04:04:02 +02:00
|
|
|
WithColor::error(errs(), ToolName) << Message << "\n";
|
2018-07-18 02:10:51 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
[llvm-objcopy] Return Error from Buffer::allocate(), [ELF]Writer::finalize(), and [ELF]Writer::commit()
Summary:
This patch changes a few methods to return Error instead of manually calling error/reportError to abort. This will make it easier to extract into a library.
Note that error() takes just a string (this patch also adds an overload that takes an Error), while reportError() takes string + [error/code]. To help unify things, use FileError to associate a given filename with an error. Note that this takes some special care (for now), e.g. calling reportError(FileName, <something that could be FileError>) will duplicate the filename. The goal is to eventually remove reportError() and have every error associated with a file to be a FileError, and just one error handling block at the tool level.
This change was suggested in D56806. I took it a little further than suggested, but completely fixing llvm-objcopy will take a couple more patches. If this approach looks good, I'll commit this and apply similar patche(s) for the rest.
This change is NFC in terms of non-error related code, although the error message changes in one context.
Reviewers: alexshap, jhenderson, jakehehrlich, mstorsjo, espindola
Reviewed By: alexshap, jhenderson
Subscribers: llvm-commits, emaste, arichardson
Differential Revision: https://reviews.llvm.org/D56930
llvm-svn: 351896
2019-01-23 00:49:16 +01:00
|
|
|
LLVM_ATTRIBUTE_NORETURN void error(Error E) {
|
|
|
|
assert(E);
|
|
|
|
std::string Buf;
|
|
|
|
raw_string_ostream OS(Buf);
|
|
|
|
logAllUnhandledErrors(std::move(E), OS);
|
|
|
|
OS.flush();
|
|
|
|
WithColor::error(errs(), ToolName) << Buf;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2018-07-18 02:10:51 +02:00
|
|
|
LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, std::error_code EC) {
|
2019-02-11 10:49:37 +01:00
|
|
|
assert(EC);
|
|
|
|
error(createFileError(File, EC));
|
2018-07-18 02:10:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-06-18 02:39:10 +02:00
|
|
|
ErrorSuccess reportWarning(Error E) {
|
|
|
|
assert(E);
|
2019-08-20 17:00:07 +02:00
|
|
|
WithColor::warning(errs(), ToolName) << toString(std::move(E)) << '\n';
|
2019-06-18 02:39:10 +02:00
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2018-07-18 02:10:51 +02:00
|
|
|
} // 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) {
|
2019-02-01 18:08:07 +01:00
|
|
|
if (Error E = writeArchive(ArcName, NewMembers, WriteSymtab, Kind,
|
|
|
|
Deterministic, Thin))
|
|
|
|
return createFileError(ArcName, std::move(E));
|
|
|
|
|
|
|
|
if (!Thin)
|
|
|
|
return Error::success();
|
|
|
|
|
2018-07-06 19:51:03 +02:00
|
|
|
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);
|
[llvm-objcopy] Return Error from Buffer::allocate(), [ELF]Writer::finalize(), and [ELF]Writer::commit()
Summary:
This patch changes a few methods to return Error instead of manually calling error/reportError to abort. This will make it easier to extract into a library.
Note that error() takes just a string (this patch also adds an overload that takes an Error), while reportError() takes string + [error/code]. To help unify things, use FileError to associate a given filename with an error. Note that this takes some special care (for now), e.g. calling reportError(FileName, <something that could be FileError>) will duplicate the filename. The goal is to eventually remove reportError() and have every error associated with a file to be a FileError, and just one error handling block at the tool level.
This change was suggested in D56806. I took it a little further than suggested, but completely fixing llvm-objcopy will take a couple more patches. If this approach looks good, I'll commit this and apply similar patche(s) for the rest.
This change is NFC in terms of non-error related code, although the error message changes in one context.
Reviewers: alexshap, jhenderson, jakehehrlich, mstorsjo, espindola
Reviewed By: alexshap, jhenderson
Subscribers: llvm-commits, emaste, arichardson
Differential Revision: https://reviews.llvm.org/D56930
llvm-svn: 351896
2019-01-23 00:49:16 +01:00
|
|
|
if (Error E = FB.allocate(Member.Buf->getBufferSize()))
|
|
|
|
return E;
|
2018-07-06 19:51:03 +02:00
|
|
|
std::copy(Member.Buf->getBufferStart(), Member.Buf->getBufferEnd(),
|
|
|
|
FB.getBufferStart());
|
[llvm-objcopy] Return Error from Buffer::allocate(), [ELF]Writer::finalize(), and [ELF]Writer::commit()
Summary:
This patch changes a few methods to return Error instead of manually calling error/reportError to abort. This will make it easier to extract into a library.
Note that error() takes just a string (this patch also adds an overload that takes an Error), while reportError() takes string + [error/code]. To help unify things, use FileError to associate a given filename with an error. Note that this takes some special care (for now), e.g. calling reportError(FileName, <something that could be FileError>) will duplicate the filename. The goal is to eventually remove reportError() and have every error associated with a file to be a FileError, and just one error handling block at the tool level.
This change was suggested in D56806. I took it a little further than suggested, but completely fixing llvm-objcopy will take a couple more patches. If this approach looks good, I'll commit this and apply similar patche(s) for the rest.
This change is NFC in terms of non-error related code, although the error message changes in one context.
Reviewers: alexshap, jhenderson, jakehehrlich, mstorsjo, espindola
Reviewed By: alexshap, jhenderson
Subscribers: llvm-commits, emaste, arichardson
Differential Revision: https://reviews.llvm.org/D56930
llvm-svn: 351896
2019-01-23 00:49:16 +01:00
|
|
|
if (Error E = FB.commit())
|
2018-07-06 19:51:03 +02:00
|
|
|
return E;
|
|
|
|
}
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2019-06-13 11:56:14 +02:00
|
|
|
/// The function executeObjcopyOnIHex does the dispatch based on the format
|
|
|
|
/// of the output specified by the command line options.
|
|
|
|
static Error executeObjcopyOnIHex(const CopyConfig &Config, MemoryBuffer &In,
|
|
|
|
Buffer &Out) {
|
|
|
|
// TODO: support output formats other than ELF.
|
|
|
|
return elf::executeObjcopyOnIHex(Config, In, Out);
|
|
|
|
}
|
|
|
|
|
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.
|
2019-01-30 15:36:53 +01:00
|
|
|
static Error executeObjcopyOnRawBinary(const CopyConfig &Config,
|
|
|
|
MemoryBuffer &In, Buffer &Out) {
|
2019-07-05 07:28:38 +02:00
|
|
|
switch (Config.OutputFormat) {
|
|
|
|
case FileFormat::ELF:
|
|
|
|
// FIXME: Currently, we call elf::executeObjcopyOnRawBinary even if the
|
|
|
|
// output format is binary/ihex or it's not given. This behavior differs from
|
|
|
|
// GNU objcopy. See https://bugs.llvm.org/show_bug.cgi?id=42171 for details.
|
|
|
|
case FileFormat::Binary:
|
|
|
|
case FileFormat::IHex:
|
|
|
|
case FileFormat::Unspecified:
|
|
|
|
return elf::executeObjcopyOnRawBinary(Config, In, Out);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("unsupported output format");
|
2018-10-25 00:49:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The function executeObjcopyOnBinary does the dispatch based on the format
|
|
|
|
/// of the input binary (ELF, MachO or COFF).
|
2019-01-30 15:36:53 +01:00
|
|
|
static Error executeObjcopyOnBinary(const CopyConfig &Config,
|
|
|
|
object::Binary &In, Buffer &Out) {
|
2018-10-25 00:49:06 +02:00
|
|
|
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);
|
2019-02-02 01:38:07 +01:00
|
|
|
else if (auto *MachOBinary = dyn_cast<object::MachOObjectFile>(&In))
|
|
|
|
return macho::executeObjcopyOnBinary(Config, *MachOBinary, Out);
|
2018-10-25 00:49:06 +02:00
|
|
|
else
|
2019-01-30 15:36:53 +01:00
|
|
|
return createStringError(object_error::invalid_file_type,
|
2019-06-14 04:04:02 +02:00
|
|
|
"unsupported object file format");
|
2018-10-25 00:49:06 +02:00
|
|
|
}
|
|
|
|
|
2019-01-30 15:36:53 +01:00
|
|
|
static Error 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<StringRef> ChildNameOrErr = Child.getName();
|
|
|
|
if (!ChildNameOrErr)
|
2019-02-01 18:08:07 +01:00
|
|
|
return createFileError(Ar.getFileName(), ChildNameOrErr.takeError());
|
2018-07-06 19:51:03 +02:00
|
|
|
|
2019-05-08 15:28:58 +02:00
|
|
|
Expected<std::unique_ptr<Binary>> ChildOrErr = Child.getAsBinary();
|
|
|
|
if (!ChildOrErr)
|
|
|
|
return createFileError(Ar.getFileName() + "(" + *ChildNameOrErr + ")",
|
|
|
|
ChildOrErr.takeError());
|
|
|
|
|
2018-07-06 19:51:03 +02:00
|
|
|
MemBuffer MB(ChildNameOrErr.get());
|
2019-05-08 15:28:58 +02:00
|
|
|
if (Error E = executeObjcopyOnBinary(Config, *ChildOrErr->get(), MB))
|
2019-01-30 15:36:53 +01:00
|
|
|
return E;
|
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)
|
2019-02-01 18:08:07 +01:00
|
|
|
return createFileError(Ar.getFileName(), Member.takeError());
|
2018-07-06 19:51:03 +02:00
|
|
|
Member->Buf = MB.releaseMemoryBuffer();
|
|
|
|
Member->MemberName = Member->Buf->getBufferIdentifier();
|
|
|
|
NewArchiveMembers.push_back(std::move(*Member));
|
|
|
|
}
|
|
|
|
if (Err)
|
2019-02-01 18:08:07 +01:00
|
|
|
return createFileError(Config.InputFilename, std::move(Err));
|
|
|
|
|
|
|
|
return deepWriteArchive(Config.OutputFilename, NewArchiveMembers,
|
|
|
|
Ar.hasSymbolTable(), Ar.kind(),
|
|
|
|
Config.DeterministicArchives, Ar.isThin());
|
2018-07-06 19:51:03 +02:00
|
|
|
}
|
|
|
|
|
[llvm-objcopy] Change handling of output file permissions
Summary: Address bug [[ https://bugs.llvm.org/show_bug.cgi?id=42082 | 42082 ]] where files were always outputted with 0775 permissions. Now, the output file is given either 0666 or 0777 if the object is executable.
Reviewers: espindola, alexshap, rupprecht, jhenderson, jakehehrlich, MaskRay
Reviewed By: rupprecht, jhenderson, jakehehrlich, MaskRay
Subscribers: emaste, arichardson, jakehehrlich, MaskRay, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D62718
llvm-svn: 365162
2019-07-05 00:45:27 +02:00
|
|
|
static Error restoreStatOnFile(StringRef Filename,
|
|
|
|
const sys::fs::file_status &Stat,
|
|
|
|
bool PreserveDates) {
|
2018-08-16 20:29:40 +02:00
|
|
|
int FD;
|
|
|
|
|
[llvm-objcopy] Change handling of output file permissions
Summary: Address bug [[ https://bugs.llvm.org/show_bug.cgi?id=42082 | 42082 ]] where files were always outputted with 0775 permissions. Now, the output file is given either 0666 or 0777 if the object is executable.
Reviewers: espindola, alexshap, rupprecht, jhenderson, jakehehrlich, MaskRay
Reviewed By: rupprecht, jhenderson, jakehehrlich, MaskRay
Subscribers: emaste, arichardson, jakehehrlich, MaskRay, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D62718
llvm-svn: 365162
2019-07-05 00:45:27 +02:00
|
|
|
// Writing to stdout should not be treated as an error here, just
|
|
|
|
// do not set access/modification times or permissions.
|
|
|
|
if (Filename == "-")
|
|
|
|
return Error::success();
|
|
|
|
|
2018-08-30 01:21:56 +02:00
|
|
|
if (auto EC =
|
|
|
|
sys::fs::openFileForWrite(Filename, FD, sys::fs::CD_OpenExisting))
|
2019-02-21 18:05:19 +01:00
|
|
|
return createFileError(Filename, EC);
|
2018-08-16 20:29:40 +02:00
|
|
|
|
[llvm-objcopy] Change handling of output file permissions
Summary: Address bug [[ https://bugs.llvm.org/show_bug.cgi?id=42082 | 42082 ]] where files were always outputted with 0775 permissions. Now, the output file is given either 0666 or 0777 if the object is executable.
Reviewers: espindola, alexshap, rupprecht, jhenderson, jakehehrlich, MaskRay
Reviewed By: rupprecht, jhenderson, jakehehrlich, MaskRay
Subscribers: emaste, arichardson, jakehehrlich, MaskRay, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D62718
llvm-svn: 365162
2019-07-05 00:45:27 +02:00
|
|
|
if (PreserveDates)
|
|
|
|
if (auto EC = sys::fs::setLastAccessAndModificationTime(
|
|
|
|
FD, Stat.getLastAccessedTime(), Stat.getLastModificationTime()))
|
|
|
|
return createFileError(Filename, EC);
|
|
|
|
|
2019-07-11 12:17:59 +02:00
|
|
|
sys::fs::file_status OStat;
|
|
|
|
if (std::error_code EC = sys::fs::status(FD, OStat))
|
2019-02-21 18:05:19 +01:00
|
|
|
return createFileError(Filename, EC);
|
2019-07-11 12:17:59 +02:00
|
|
|
if (OStat.type() == sys::fs::file_type::regular_file)
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (auto EC = sys::fs::setPermissions(
|
|
|
|
Filename, static_cast<sys::fs::perms>(Stat.permissions() &
|
|
|
|
~sys::fs::getUmask())))
|
|
|
|
#else
|
|
|
|
if (auto EC = sys::fs::setPermissions(
|
|
|
|
FD, static_cast<sys::fs::perms>(Stat.permissions() &
|
|
|
|
~sys::fs::getUmask())))
|
|
|
|
#endif
|
|
|
|
return createFileError(Filename, EC);
|
2018-08-16 20:29:40 +02:00
|
|
|
|
|
|
|
if (auto EC = sys::Process::SafelyCloseFileDescriptor(FD))
|
2019-02-21 18:05:19 +01:00
|
|
|
return createFileError(Filename, EC);
|
|
|
|
|
|
|
|
return Error::success();
|
2018-08-16 20:29:40 +02:00
|
|
|
}
|
|
|
|
|
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.
|
2019-02-21 18:05:19 +01:00
|
|
|
static Error executeObjcopy(const CopyConfig &Config) {
|
2018-08-16 20:29:40 +02:00
|
|
|
sys::fs::file_status Stat;
|
[llvm-objcopy] Change handling of output file permissions
Summary: Address bug [[ https://bugs.llvm.org/show_bug.cgi?id=42082 | 42082 ]] where files were always outputted with 0775 permissions. Now, the output file is given either 0666 or 0777 if the object is executable.
Reviewers: espindola, alexshap, rupprecht, jhenderson, jakehehrlich, MaskRay
Reviewed By: rupprecht, jhenderson, jakehehrlich, MaskRay
Subscribers: emaste, arichardson, jakehehrlich, MaskRay, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D62718
llvm-svn: 365162
2019-07-05 00:45:27 +02:00
|
|
|
if (Config.InputFilename != "-") {
|
2018-08-16 20:29:40 +02:00
|
|
|
if (auto EC = sys::fs::status(Config.InputFilename, Stat))
|
2019-02-21 18:05:19 +01:00
|
|
|
return createFileError(Config.InputFilename, EC);
|
[llvm-objcopy] Change handling of output file permissions
Summary: Address bug [[ https://bugs.llvm.org/show_bug.cgi?id=42082 | 42082 ]] where files were always outputted with 0775 permissions. Now, the output file is given either 0666 or 0777 if the object is executable.
Reviewers: espindola, alexshap, rupprecht, jhenderson, jakehehrlich, MaskRay
Reviewed By: rupprecht, jhenderson, jakehehrlich, MaskRay
Subscribers: emaste, arichardson, jakehehrlich, MaskRay, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D62718
llvm-svn: 365162
2019-07-05 00:45:27 +02:00
|
|
|
} else {
|
|
|
|
Stat.permissions(static_cast<sys::fs::perms>(0777));
|
|
|
|
}
|
2018-08-16 20:29:40 +02:00
|
|
|
|
2019-06-13 11:56:14 +02:00
|
|
|
typedef Error (*ProcessRawFn)(const CopyConfig &, MemoryBuffer &, Buffer &);
|
2019-07-05 07:28:38 +02:00
|
|
|
ProcessRawFn ProcessRaw;
|
|
|
|
switch (Config.InputFormat) {
|
|
|
|
case FileFormat::Binary:
|
|
|
|
ProcessRaw = executeObjcopyOnRawBinary;
|
|
|
|
break;
|
|
|
|
case FileFormat::IHex:
|
|
|
|
ProcessRaw = executeObjcopyOnIHex;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ProcessRaw = nullptr;
|
|
|
|
}
|
2019-06-13 11:56:14 +02:00
|
|
|
|
|
|
|
if (ProcessRaw) {
|
|
|
|
auto BufOrErr = MemoryBuffer::getFileOrSTDIN(Config.InputFilename);
|
[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 (!BufOrErr)
|
2019-02-21 18:05:19 +01:00
|
|
|
return createFileError(Config.InputFilename, BufOrErr.getError());
|
2018-08-16 20:29:40 +02:00
|
|
|
FileBuffer FB(Config.OutputFilename);
|
2019-06-13 11:56:14 +02:00
|
|
|
if (Error E = ProcessRaw(Config, *BufOrErr->get(), FB))
|
2019-02-21 18:05:19 +01:00
|
|
|
return E;
|
[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)
|
2019-02-21 18:05:19 +01:00
|
|
|
return createFileError(Config.InputFilename, BinaryOrErr.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
|
|
|
|
|
|
|
if (Archive *Ar = dyn_cast<Archive>(BinaryOrErr.get().getBinary())) {
|
2019-01-30 15:36:53 +01:00
|
|
|
if (Error E = executeObjcopyOnArchive(Config, *Ar))
|
2019-02-21 18:05:19 +01:00
|
|
|
return E;
|
[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);
|
2019-01-30 15:36:53 +01:00
|
|
|
if (Error E = executeObjcopyOnBinary(Config,
|
|
|
|
*BinaryOrErr.get().getBinary(), FB))
|
2019-02-21 18:05:19 +01:00
|
|
|
return E;
|
[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
|
|
|
|
[llvm-objcopy] Change handling of output file permissions
Summary: Address bug [[ https://bugs.llvm.org/show_bug.cgi?id=42082 | 42082 ]] where files were always outputted with 0775 permissions. Now, the output file is given either 0666 or 0777 if the object is executable.
Reviewers: espindola, alexshap, rupprecht, jhenderson, jakehehrlich, MaskRay
Reviewed By: rupprecht, jhenderson, jakehehrlich, MaskRay
Subscribers: emaste, arichardson, jakehehrlich, MaskRay, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D62718
llvm-svn: 365162
2019-07-05 00:45:27 +02:00
|
|
|
if (Error E =
|
|
|
|
restoreStatOnFile(Config.OutputFilename, Stat, Config.PreserveDates))
|
|
|
|
return E;
|
|
|
|
|
|
|
|
if (!Config.SplitDWO.empty()) {
|
|
|
|
Stat.permissions(static_cast<sys::fs::perms>(0666));
|
|
|
|
if (Error E =
|
|
|
|
restoreStatOnFile(Config.SplitDWO, Stat, Config.PreserveDates))
|
2019-02-21 18:05:19 +01:00
|
|
|
return E;
|
2018-08-16 20:29:40 +02:00
|
|
|
}
|
2019-02-21 18:05:19 +01:00
|
|
|
|
|
|
|
return Error::success();
|
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];
|
2019-02-21 18:05:19 +01:00
|
|
|
bool IsStrip = sys::path::stem(ToolName).contains("strip");
|
|
|
|
Expected<DriverConfig> DriverConfig =
|
2019-06-18 02:39:10 +02:00
|
|
|
IsStrip ? parseStripOptions(makeArrayRef(argv + 1, argc), reportWarning)
|
2019-02-21 18:05:19 +01:00
|
|
|
: parseObjcopyOptions(makeArrayRef(argv + 1, argc));
|
|
|
|
if (!DriverConfig) {
|
|
|
|
logAllUnhandledErrors(DriverConfig.takeError(),
|
|
|
|
WithColor::error(errs(), ToolName));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
for (const CopyConfig &CopyConfig : DriverConfig->CopyConfigs) {
|
|
|
|
if (Error E = executeObjcopy(CopyConfig)) {
|
|
|
|
logAllUnhandledErrors(std::move(E), WithColor::error(errs(), ToolName));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2017-08-01 02:33:58 +02:00
|
|
|
}
|