2018-10-29 21:22:58 +00:00
|
|
|
//===- ELFObjcopy.cpp -----------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 08:50:56 +00: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
|
2018-10-29 21:22:58 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ELFObjcopy.h"
|
|
|
|
#include "Buffer.h"
|
|
|
|
#include "CopyConfig.h"
|
|
|
|
#include "Object.h"
|
2018-12-03 19:49:23 +00:00
|
|
|
#include "llvm-objcopy.h"
|
2018-10-29 21:22:58 +00:00
|
|
|
|
|
|
|
#include "llvm/ADT/BitmaskEnum.h"
|
|
|
|
#include "llvm/ADT/Optional.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
|
|
|
#include "llvm/BinaryFormat/ELF.h"
|
|
|
|
#include "llvm/MC/MCTargetOptions.h"
|
|
|
|
#include "llvm/Object/Binary.h"
|
|
|
|
#include "llvm/Object/ELFObjectFile.h"
|
|
|
|
#include "llvm/Object/ELFTypes.h"
|
|
|
|
#include "llvm/Object/Error.h"
|
|
|
|
#include "llvm/Option/Option.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/Compression.h"
|
2018-12-03 19:49:23 +00:00
|
|
|
#include "llvm/Support/Errc.h"
|
2018-10-29 21:22:58 +00:00
|
|
|
#include "llvm/Support/Error.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/ErrorOr.h"
|
|
|
|
#include "llvm/Support/Memory.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <functional>
|
|
|
|
#include <iterator>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <system_error>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace objcopy {
|
|
|
|
namespace elf {
|
|
|
|
|
|
|
|
using namespace object;
|
|
|
|
using namespace ELF;
|
|
|
|
using SectionPred = std::function<bool(const SectionBase &Sec)>;
|
|
|
|
|
|
|
|
static bool isDebugSection(const SectionBase &Sec) {
|
|
|
|
return StringRef(Sec.Name).startswith(".debug") ||
|
|
|
|
StringRef(Sec.Name).startswith(".zdebug") || Sec.Name == ".gdb_index";
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isDWOSection(const SectionBase &Sec) {
|
|
|
|
return StringRef(Sec.Name).endswith(".dwo");
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool onlyKeepDWOPred(const Object &Obj, const SectionBase &Sec) {
|
|
|
|
// We can't remove the section header string table.
|
|
|
|
if (&Sec == Obj.SectionNames)
|
|
|
|
return false;
|
|
|
|
// Short of keeping the string table we want to keep everything that is a DWO
|
|
|
|
// section and remove everything else.
|
|
|
|
return !isDWOSection(Sec);
|
|
|
|
}
|
|
|
|
|
[llvm-objcopy] Implement --set-section-flags.
Summary:
--set-section-flags is used to change the section flags (e.g. SHF_ALLOC) for given sections. The flags allowed are the same from the existing --rename-section=.old=.new[,flags] feature.
Additionally, make sure that --set-section-flag cannot be used with --rename-section (either the source or destination), since --rename-section accepts flags. This avoids ambiguity for something like "--rename-section=.foo=.bar,alloc --set-section-flag=.bar,code".
Reviewers: jhenderson, jakehehrlich, alexshap, espindola
Reviewed By: jhenderson, jakehehrlich
Subscribers: llvm-commits, emaste, arichardson
Differential Revision: https://reviews.llvm.org/D57198
llvm-svn: 352505
2019-01-29 15:05:38 +00:00
|
|
|
static uint64_t setSectionFlagsPreserveMask(uint64_t OldFlags,
|
|
|
|
uint64_t NewFlags) {
|
|
|
|
// Preserve some flags which should not be dropped when setting flags.
|
|
|
|
// Also, preserve anything OS/processor dependant.
|
|
|
|
const uint64_t PreserveMask = ELF::SHF_COMPRESSED | ELF::SHF_EXCLUDE |
|
|
|
|
ELF::SHF_GROUP | ELF::SHF_LINK_ORDER |
|
|
|
|
ELF::SHF_MASKOS | ELF::SHF_MASKPROC |
|
|
|
|
ELF::SHF_TLS | ELF::SHF_INFO_LINK;
|
|
|
|
return (OldFlags & PreserveMask) | (NewFlags & ~PreserveMask);
|
|
|
|
}
|
|
|
|
|
2018-10-29 21:22:58 +00:00
|
|
|
static ElfType getOutputElfType(const Binary &Bin) {
|
|
|
|
// Infer output ELF type from the input ELF object
|
|
|
|
if (isa<ELFObjectFile<ELF32LE>>(Bin))
|
|
|
|
return ELFT_ELF32LE;
|
|
|
|
if (isa<ELFObjectFile<ELF64LE>>(Bin))
|
|
|
|
return ELFT_ELF64LE;
|
|
|
|
if (isa<ELFObjectFile<ELF32BE>>(Bin))
|
|
|
|
return ELFT_ELF32BE;
|
|
|
|
if (isa<ELFObjectFile<ELF64BE>>(Bin))
|
|
|
|
return ELFT_ELF64BE;
|
|
|
|
llvm_unreachable("Invalid ELFType");
|
|
|
|
}
|
|
|
|
|
|
|
|
static ElfType getOutputElfType(const MachineInfo &MI) {
|
|
|
|
// Infer output ELF type from the binary arch specified
|
|
|
|
if (MI.Is64Bit)
|
|
|
|
return MI.IsLittleEndian ? ELFT_ELF64LE : ELFT_ELF64BE;
|
|
|
|
else
|
|
|
|
return MI.IsLittleEndian ? ELFT_ELF32LE : ELFT_ELF32BE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::unique_ptr<Writer> createWriter(const CopyConfig &Config,
|
|
|
|
Object &Obj, Buffer &Buf,
|
|
|
|
ElfType OutputElfType) {
|
|
|
|
if (Config.OutputFormat == "binary") {
|
|
|
|
return llvm::make_unique<BinaryWriter>(Obj, Buf);
|
|
|
|
}
|
|
|
|
// Depending on the initial ELFT and OutputFormat we need a different Writer.
|
|
|
|
switch (OutputElfType) {
|
|
|
|
case ELFT_ELF32LE:
|
|
|
|
return llvm::make_unique<ELFWriter<ELF32LE>>(Obj, Buf,
|
|
|
|
!Config.StripSections);
|
|
|
|
case ELFT_ELF64LE:
|
|
|
|
return llvm::make_unique<ELFWriter<ELF64LE>>(Obj, Buf,
|
|
|
|
!Config.StripSections);
|
|
|
|
case ELFT_ELF32BE:
|
|
|
|
return llvm::make_unique<ELFWriter<ELF32BE>>(Obj, Buf,
|
|
|
|
!Config.StripSections);
|
|
|
|
case ELFT_ELF64BE:
|
|
|
|
return llvm::make_unique<ELFWriter<ELF64BE>>(Obj, Buf,
|
|
|
|
!Config.StripSections);
|
|
|
|
}
|
|
|
|
llvm_unreachable("Invalid output format");
|
|
|
|
}
|
|
|
|
|
2018-12-03 19:49:23 +00:00
|
|
|
template <class ELFT>
|
|
|
|
static Expected<ArrayRef<uint8_t>>
|
|
|
|
findBuildID(const object::ELFFile<ELFT> &In) {
|
|
|
|
for (const auto &Phdr : unwrapOrError(In.program_headers())) {
|
|
|
|
if (Phdr.p_type != PT_NOTE)
|
|
|
|
continue;
|
|
|
|
Error Err = Error::success();
|
2018-12-11 00:09:06 +00:00
|
|
|
for (const auto &Note : In.notes(Phdr, Err))
|
2018-12-03 19:49:23 +00:00
|
|
|
if (Note.getType() == NT_GNU_BUILD_ID && Note.getName() == ELF_NOTE_GNU)
|
|
|
|
return Note.getDesc();
|
|
|
|
if (Err)
|
|
|
|
return std::move(Err);
|
|
|
|
}
|
|
|
|
return createStringError(llvm::errc::invalid_argument,
|
|
|
|
"Could not find build ID.");
|
|
|
|
}
|
|
|
|
|
|
|
|
static Expected<ArrayRef<uint8_t>>
|
|
|
|
findBuildID(const object::ELFObjectFileBase &In) {
|
|
|
|
if (auto *O = dyn_cast<ELFObjectFile<ELF32LE>>(&In))
|
|
|
|
return findBuildID(*O->getELFFile());
|
|
|
|
else if (auto *O = dyn_cast<ELFObjectFile<ELF64LE>>(&In))
|
|
|
|
return findBuildID(*O->getELFFile());
|
|
|
|
else if (auto *O = dyn_cast<ELFObjectFile<ELF32BE>>(&In))
|
|
|
|
return findBuildID(*O->getELFFile());
|
|
|
|
else if (auto *O = dyn_cast<ELFObjectFile<ELF64BE>>(&In))
|
|
|
|
return findBuildID(*O->getELFFile());
|
|
|
|
|
|
|
|
llvm_unreachable("Bad file format");
|
|
|
|
}
|
|
|
|
|
2019-01-30 18:13:30 +00:00
|
|
|
static Error linkToBuildIdDir(const CopyConfig &Config, StringRef ToLink,
|
|
|
|
StringRef Suffix,
|
|
|
|
ArrayRef<uint8_t> BuildIdBytes) {
|
2018-12-03 19:49:23 +00:00
|
|
|
SmallString<128> Path = Config.BuildIdLinkDir;
|
|
|
|
sys::path::append(Path, llvm::toHex(BuildIdBytes[0], /*LowerCase*/ true));
|
|
|
|
if (auto EC = sys::fs::create_directories(Path))
|
2019-01-30 18:13:30 +00:00
|
|
|
return createFileError(
|
|
|
|
Path.str(),
|
|
|
|
createStringError(EC, "cannot create build ID link directory"));
|
2018-12-03 19:49:23 +00:00
|
|
|
|
|
|
|
sys::path::append(Path,
|
|
|
|
llvm::toHex(BuildIdBytes.slice(1), /*LowerCase*/ true));
|
|
|
|
Path += Suffix;
|
|
|
|
if (auto EC = sys::fs::create_hard_link(ToLink, Path)) {
|
|
|
|
// Hard linking failed, try to remove the file first if it exists.
|
|
|
|
if (sys::fs::exists(Path))
|
|
|
|
sys::fs::remove(Path);
|
|
|
|
EC = sys::fs::create_hard_link(ToLink, Path);
|
|
|
|
if (EC)
|
2019-01-30 18:13:30 +00:00
|
|
|
return createStringError(EC, "cannot link %s to %s", ToLink.data(),
|
|
|
|
Path.data());
|
2018-12-03 19:49:23 +00:00
|
|
|
}
|
2019-01-30 18:13:30 +00:00
|
|
|
return Error::success();
|
2018-12-03 19:49:23 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 14:36:53 +00:00
|
|
|
static Error splitDWOToFile(const CopyConfig &Config, const Reader &Reader,
|
|
|
|
StringRef File, ElfType OutputElfType) {
|
2018-10-29 21:22:58 +00:00
|
|
|
auto DWOFile = Reader.create();
|
2019-02-01 15:20:36 +00:00
|
|
|
auto OnlyKeepDWOPred = [&DWOFile](const SectionBase &Sec) {
|
|
|
|
return onlyKeepDWOPred(*DWOFile, Sec);
|
|
|
|
};
|
|
|
|
if (Error E = DWOFile->removeSections(OnlyKeepDWOPred))
|
|
|
|
return E;
|
2019-01-07 16:59:12 +00:00
|
|
|
if (Config.OutputArch)
|
|
|
|
DWOFile->Machine = Config.OutputArch.getValue().EMachine;
|
2018-10-29 21:22:58 +00:00
|
|
|
FileBuffer FB(File);
|
|
|
|
auto Writer = createWriter(Config, *DWOFile, FB, OutputElfType);
|
[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-22 23:49:16 +00:00
|
|
|
if (Error E = Writer->finalize())
|
2019-01-30 14:36:53 +00:00
|
|
|
return E;
|
|
|
|
return Writer->write();
|
2018-10-29 21:22:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static Error dumpSectionToFile(StringRef SecName, StringRef Filename,
|
|
|
|
Object &Obj) {
|
|
|
|
for (auto &Sec : Obj.sections()) {
|
|
|
|
if (Sec.Name == SecName) {
|
2018-12-20 00:57:06 +00:00
|
|
|
if (Sec.OriginalData.empty())
|
2019-01-22 10:57:59 +00:00
|
|
|
return createStringError(
|
|
|
|
object_error::parse_failed,
|
|
|
|
"Can't dump section \"%s\": it has no contents",
|
|
|
|
SecName.str().c_str());
|
2018-10-29 21:22:58 +00:00
|
|
|
Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
|
|
|
|
FileOutputBuffer::create(Filename, Sec.OriginalData.size());
|
|
|
|
if (!BufferOrErr)
|
|
|
|
return BufferOrErr.takeError();
|
|
|
|
std::unique_ptr<FileOutputBuffer> Buf = std::move(*BufferOrErr);
|
|
|
|
std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(),
|
|
|
|
Buf->getBufferStart());
|
|
|
|
if (Error E = Buf->commit())
|
|
|
|
return E;
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
}
|
2019-01-22 10:57:59 +00:00
|
|
|
return createStringError(object_error::parse_failed, "Section not found");
|
2018-10-29 21:22:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool isCompressable(const SectionBase &Section) {
|
2019-03-05 13:07:43 +00:00
|
|
|
return !(Section.Flags & ELF::SHF_COMPRESSED) &&
|
|
|
|
StringRef(Section.Name).startswith(".debug");
|
2018-10-29 21:22:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void replaceDebugSections(
|
|
|
|
const CopyConfig &Config, Object &Obj, SectionPred &RemovePred,
|
|
|
|
function_ref<bool(const SectionBase &)> shouldReplace,
|
|
|
|
function_ref<SectionBase *(const SectionBase *)> addSection) {
|
2019-03-11 11:01:24 +00:00
|
|
|
// Build a list of the debug sections we are going to replace.
|
|
|
|
// We can't call `addSection` while iterating over sections,
|
|
|
|
// because it would mutate the sections array.
|
2018-10-29 21:22:58 +00:00
|
|
|
SmallVector<SectionBase *, 13> ToReplace;
|
2019-03-11 11:01:24 +00:00
|
|
|
for (auto &Sec : Obj.sections())
|
2018-10-29 21:22:58 +00:00
|
|
|
if (shouldReplace(Sec))
|
|
|
|
ToReplace.push_back(&Sec);
|
|
|
|
|
2019-03-11 11:01:24 +00:00
|
|
|
// Build a mapping from original section to a new one.
|
|
|
|
DenseMap<SectionBase *, SectionBase *> FromTo;
|
|
|
|
for (SectionBase *S : ToReplace)
|
|
|
|
FromTo[S] = addSection(S);
|
2018-10-29 21:22:58 +00:00
|
|
|
|
2019-03-11 11:01:24 +00:00
|
|
|
// Now we want to update the target sections of relocation
|
|
|
|
// sections. Also we will update the relocations themselves
|
|
|
|
// to update the symbol references.
|
|
|
|
for (auto &Sec : Obj.sections())
|
|
|
|
Sec.replaceSectionReferences(FromTo);
|
2018-10-29 21:22:58 +00:00
|
|
|
|
|
|
|
RemovePred = [shouldReplace, RemovePred](const SectionBase &Sec) {
|
|
|
|
return shouldReplace(Sec) || RemovePred(Sec);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-02-13 07:34:54 +00:00
|
|
|
static bool isUnneededSymbol(const Symbol &Sym) {
|
|
|
|
return !Sym.Referenced &&
|
|
|
|
(Sym.Binding == STB_LOCAL || Sym.getShndx() == SHN_UNDEF) &&
|
|
|
|
Sym.Type != STT_FILE && Sym.Type != STT_SECTION;
|
|
|
|
}
|
|
|
|
|
2018-10-29 21:22:58 +00:00
|
|
|
// This function handles the high level operations of GNU objcopy including
|
|
|
|
// handling command line options. It's important to outline certain properties
|
|
|
|
// we expect to hold of the command line operations. Any operation that "keeps"
|
|
|
|
// should keep regardless of a remove. Additionally any removal should respect
|
|
|
|
// any previous removals. Lastly whether or not something is removed shouldn't
|
|
|
|
// depend a) on the order the options occur in or b) on some opaque priority
|
|
|
|
// system. The only priority is that keeps/copies overrule removes.
|
2019-01-30 14:36:53 +00:00
|
|
|
static Error handleArgs(const CopyConfig &Config, Object &Obj,
|
|
|
|
const Reader &Reader, ElfType OutputElfType) {
|
|
|
|
|
|
|
|
if (!Config.SplitDWO.empty())
|
|
|
|
if (Error E =
|
|
|
|
splitDWOToFile(Config, Reader, Config.SplitDWO, OutputElfType))
|
|
|
|
return E;
|
2018-10-29 21:22:58 +00:00
|
|
|
|
2019-01-07 16:59:12 +00:00
|
|
|
if (Config.OutputArch)
|
|
|
|
Obj.Machine = Config.OutputArch.getValue().EMachine;
|
2018-10-29 21:22:58 +00:00
|
|
|
|
|
|
|
// TODO: update or remove symbols only if there is an option that affects
|
|
|
|
// them.
|
|
|
|
if (Obj.SymbolTable) {
|
|
|
|
Obj.SymbolTable->updateSymbols([&](Symbol &Sym) {
|
[llvm-objcopy] Skip --localize-symbol for undefined symbols
Summary:
Include the symbol being defined in the list of requirements for using --localize-symbol.
This is used, for example, when someone is depending on two different projects that have the same (or close enough) method defined in each library, and using "-L sym" for a conflicting symbol in one of the libraries so that the definition from the other one is used. However, the library may have internal references to the symbol, which cause program crashes when those are used, i.e.:
```
$ cat foo.c
int foo() { return 5; }
$ cat bar.c
int foo();
int bar() { return 2 * foo(); }
$ cat foo2.c
int foo() { /* Safer implementation */ return 42; }
$ cat main.c
int bar();
int main() {
__builtin_printf("bar = %d\n", bar());
return 0;
}
$ ar rcs libfoo.a foo.o bar.o
$ ar rcs libfoo2.a foo2.o
# Picks the wrong foo() impl
$ clang main.o -lfoo -lfoo2 -L. -o main
# Picks the right foo() impl
$ objcopy -L foo libfoo.a && clang main.o -lfoo -lfoo2 -L. -o main
# Links somehow, but crashes at runtime
$ llvm-objcopy -L foo libfoo.a && clang main.o -lfoo -lfoo2 -L. -o main
```
Reviewers: jhenderson, alexshap, jakehehrlich, espindola
Subscribers: emaste, arichardson
Differential Revision: https://reviews.llvm.org/D57417
llvm-svn: 352767
2019-01-31 16:45:16 +00:00
|
|
|
// Common and undefined symbols don't make sense as local symbols, and can
|
|
|
|
// even cause crashes if we localize those, so skip them.
|
|
|
|
if (!Sym.isCommon() && Sym.getShndx() != SHN_UNDEF &&
|
[llvm-objcopy] Don't apply --localize flags to common symbols
Summary:
--localize-symbol and --localize-hidden will currently localize common symbols. GNU objcopy will not localize these symbols even when explicitly requested, which seems reasonable; common symbols should always be global so they can be merged during linking.
See PR39461
Reviewers: jakehehrlich, jhenderson, alexshap, MaskRay, espindola
Reviewed By: jakehehrlich, jhenderson, alexshap, MaskRay
Subscribers: emaste, arichardson, alexshap, MaskRay, llvm-commits
Differential Revision: https://reviews.llvm.org/D53782
llvm-svn: 345856
2018-11-01 17:26:36 +00:00
|
|
|
((Config.LocalizeHidden &&
|
|
|
|
(Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL)) ||
|
2018-11-29 17:32:51 +00:00
|
|
|
is_contained(Config.SymbolsToLocalize, Sym.Name)))
|
2018-10-29 21:22:58 +00:00
|
|
|
Sym.Binding = STB_LOCAL;
|
|
|
|
|
|
|
|
// Note: these two globalize flags have very similar names but different
|
|
|
|
// meanings:
|
|
|
|
//
|
|
|
|
// --globalize-symbol: promote a symbol to global
|
|
|
|
// --keep-global-symbol: all symbols except for these should be made local
|
|
|
|
//
|
|
|
|
// If --globalize-symbol is specified for a given symbol, it will be
|
|
|
|
// global in the output file even if it is not included via
|
|
|
|
// --keep-global-symbol. Because of that, make sure to check
|
|
|
|
// --globalize-symbol second.
|
|
|
|
if (!Config.SymbolsToKeepGlobal.empty() &&
|
2018-10-30 16:23:38 +00:00
|
|
|
!is_contained(Config.SymbolsToKeepGlobal, Sym.Name) &&
|
|
|
|
Sym.getShndx() != SHN_UNDEF)
|
2018-10-29 21:22:58 +00:00
|
|
|
Sym.Binding = STB_LOCAL;
|
|
|
|
|
2018-11-29 17:32:51 +00:00
|
|
|
if (is_contained(Config.SymbolsToGlobalize, Sym.Name) &&
|
2018-10-30 16:23:38 +00:00
|
|
|
Sym.getShndx() != SHN_UNDEF)
|
2018-10-29 21:22:58 +00:00
|
|
|
Sym.Binding = STB_GLOBAL;
|
|
|
|
|
2018-11-29 17:32:51 +00:00
|
|
|
if (is_contained(Config.SymbolsToWeaken, Sym.Name) &&
|
2018-10-29 21:22:58 +00:00
|
|
|
Sym.Binding == STB_GLOBAL)
|
|
|
|
Sym.Binding = STB_WEAK;
|
|
|
|
|
|
|
|
if (Config.Weaken && Sym.Binding == STB_GLOBAL &&
|
|
|
|
Sym.getShndx() != SHN_UNDEF)
|
|
|
|
Sym.Binding = STB_WEAK;
|
|
|
|
|
|
|
|
const auto I = Config.SymbolsToRename.find(Sym.Name);
|
|
|
|
if (I != Config.SymbolsToRename.end())
|
|
|
|
Sym.Name = I->getValue();
|
|
|
|
|
|
|
|
if (!Config.SymbolsPrefix.empty() && Sym.Type != STT_SECTION)
|
|
|
|
Sym.Name = (Config.SymbolsPrefix + Sym.Name).str();
|
|
|
|
});
|
|
|
|
|
|
|
|
// The purpose of this loop is to mark symbols referenced by sections
|
|
|
|
// (like GroupSection or RelocationSection). This way, we know which
|
|
|
|
// symbols are still 'needed' and which are not.
|
2019-02-13 07:34:54 +00:00
|
|
|
if (Config.StripUnneeded || !Config.UnneededSymbolsToRemove.empty()) {
|
2018-10-29 21:22:58 +00:00
|
|
|
for (auto &Section : Obj.sections())
|
|
|
|
Section.markSymbols();
|
|
|
|
}
|
|
|
|
|
2019-02-01 15:20:36 +00:00
|
|
|
auto RemoveSymbolsPred = [&](const Symbol &Sym) {
|
2018-11-29 17:32:51 +00:00
|
|
|
if (is_contained(Config.SymbolsToKeep, Sym.Name) ||
|
2018-10-29 21:22:58 +00:00
|
|
|
(Config.KeepFileSymbols && Sym.Type == STT_FILE))
|
|
|
|
return false;
|
|
|
|
|
[llvm-objcopy] Support -X|--discard-locals.
Summary:
This adds support for the --discard-locals flag, which acts similarly to --discard-all, except it only applies to compiler-generated symbols (i.e. symbols starting with `.L` in ELF).
I am not sure about COFF local symbols: those appear to also use `.L` in most cases, but also use just `L` in other cases, so for now I am just leaving it unimplemented there.
Fixes PR36160
Reviewers: jhenderson, alexshap, jakehehrlich, mstorsjo, espindola
Reviewed By: jhenderson
Subscribers: llvm-commits, emaste, arichardson
Differential Revision: https://reviews.llvm.org/D57248
llvm-svn: 352626
2019-01-30 14:58:13 +00:00
|
|
|
if ((Config.DiscardMode == DiscardType::All ||
|
|
|
|
(Config.DiscardMode == DiscardType::Locals &&
|
|
|
|
StringRef(Sym.Name).startswith(".L"))) &&
|
|
|
|
Sym.Binding == STB_LOCAL && Sym.getShndx() != SHN_UNDEF &&
|
|
|
|
Sym.Type != STT_FILE && Sym.Type != STT_SECTION)
|
2018-10-29 21:22:58 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
if (Config.StripAll || Config.StripAllGNU)
|
|
|
|
return true;
|
|
|
|
|
2018-11-29 17:32:51 +00:00
|
|
|
if (is_contained(Config.SymbolsToRemove, Sym.Name))
|
2018-10-29 21:22:58 +00:00
|
|
|
return true;
|
|
|
|
|
2019-02-13 07:34:54 +00:00
|
|
|
if ((Config.StripUnneeded ||
|
|
|
|
is_contained(Config.UnneededSymbolsToRemove, Sym.Name)) &&
|
|
|
|
isUnneededSymbol(Sym))
|
2018-10-29 21:22:58 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
2019-02-01 15:20:36 +00:00
|
|
|
};
|
|
|
|
if (Error E = Obj.removeSymbols(RemoveSymbolsPred))
|
|
|
|
return E;
|
2018-10-29 21:22:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SectionPred RemovePred = [](const SectionBase &) { return false; };
|
|
|
|
|
|
|
|
// Removes:
|
|
|
|
if (!Config.ToRemove.empty()) {
|
|
|
|
RemovePred = [&Config](const SectionBase &Sec) {
|
|
|
|
return is_contained(Config.ToRemove, Sec.Name);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Config.StripDWO || !Config.SplitDWO.empty())
|
|
|
|
RemovePred = [RemovePred](const SectionBase &Sec) {
|
|
|
|
return isDWOSection(Sec) || RemovePred(Sec);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (Config.ExtractDWO)
|
|
|
|
RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
|
|
|
|
return onlyKeepDWOPred(Obj, Sec) || RemovePred(Sec);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (Config.StripAllGNU)
|
|
|
|
RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
|
|
|
|
if (RemovePred(Sec))
|
|
|
|
return true;
|
|
|
|
if ((Sec.Flags & SHF_ALLOC) != 0)
|
|
|
|
return false;
|
|
|
|
if (&Sec == Obj.SectionNames)
|
|
|
|
return false;
|
|
|
|
switch (Sec.Type) {
|
|
|
|
case SHT_SYMTAB:
|
|
|
|
case SHT_REL:
|
|
|
|
case SHT_RELA:
|
|
|
|
case SHT_STRTAB:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return isDebugSection(Sec);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (Config.StripSections) {
|
|
|
|
RemovePred = [RemovePred](const SectionBase &Sec) {
|
2019-03-14 11:23:04 +00:00
|
|
|
return RemovePred(Sec) || (Sec.Flags & SHF_ALLOC) == 0;
|
2018-10-29 21:22:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Config.StripDebug) {
|
|
|
|
RemovePred = [RemovePred](const SectionBase &Sec) {
|
|
|
|
return RemovePred(Sec) || isDebugSection(Sec);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Config.StripNonAlloc)
|
|
|
|
RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
|
|
|
|
if (RemovePred(Sec))
|
|
|
|
return true;
|
|
|
|
if (&Sec == Obj.SectionNames)
|
|
|
|
return false;
|
2019-03-14 11:23:04 +00:00
|
|
|
return (Sec.Flags & SHF_ALLOC) == 0;
|
2018-10-29 21:22:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (Config.StripAll)
|
|
|
|
RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
|
|
|
|
if (RemovePred(Sec))
|
|
|
|
return true;
|
|
|
|
if (&Sec == Obj.SectionNames)
|
|
|
|
return false;
|
|
|
|
if (StringRef(Sec.Name).startswith(".gnu.warning"))
|
|
|
|
return false;
|
|
|
|
return (Sec.Flags & SHF_ALLOC) == 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Explicit copies:
|
2018-12-06 02:03:53 +00:00
|
|
|
if (!Config.OnlySection.empty()) {
|
2018-10-29 21:22:58 +00:00
|
|
|
RemovePred = [&Config, RemovePred, &Obj](const SectionBase &Sec) {
|
|
|
|
// Explicitly keep these sections regardless of previous removes.
|
2018-12-06 02:03:53 +00:00
|
|
|
if (is_contained(Config.OnlySection, Sec.Name))
|
2018-10-29 21:22:58 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Allow all implicit removes.
|
|
|
|
if (RemovePred(Sec))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Keep special sections.
|
|
|
|
if (Obj.SectionNames == &Sec)
|
|
|
|
return false;
|
|
|
|
if (Obj.SymbolTable == &Sec ||
|
|
|
|
(Obj.SymbolTable && Obj.SymbolTable->getStrTab() == &Sec))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Remove everything else.
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
[llvm-objcopy] Rename --keep to --keep-section.
Summary:
llvm-objcopy/strip support `--keep` (for sections) and `--keep-symbols` (for symbols). For consistency and clarity, rename `--keep` to `--keep-section`.
In fact, for GNU compatability, -K is --keep-symbol, so it's weird that the alias `-K` is not the same as the short-ish `--keep`.
Reviewers: jakehehrlich, jhenderson, alexshap, MaskRay, espindola
Reviewed By: jakehehrlich, MaskRay
Subscribers: emaste, arichardson, llvm-commits
Differential Revision: https://reviews.llvm.org/D54477
llvm-svn: 346782
2018-11-13 19:32:27 +00:00
|
|
|
if (!Config.KeepSection.empty()) {
|
2018-11-12 23:46:22 +00:00
|
|
|
RemovePred = [&Config, RemovePred](const SectionBase &Sec) {
|
2018-10-29 21:22:58 +00:00
|
|
|
// Explicitly keep these sections regardless of previous removes.
|
[llvm-objcopy] Rename --keep to --keep-section.
Summary:
llvm-objcopy/strip support `--keep` (for sections) and `--keep-symbols` (for symbols). For consistency and clarity, rename `--keep` to `--keep-section`.
In fact, for GNU compatability, -K is --keep-symbol, so it's weird that the alias `-K` is not the same as the short-ish `--keep`.
Reviewers: jakehehrlich, jhenderson, alexshap, MaskRay, espindola
Reviewed By: jakehehrlich, MaskRay
Subscribers: emaste, arichardson, llvm-commits
Differential Revision: https://reviews.llvm.org/D54477
llvm-svn: 346782
2018-11-13 19:32:27 +00:00
|
|
|
if (is_contained(Config.KeepSection, Sec.Name))
|
2018-10-29 21:22:58 +00:00
|
|
|
return false;
|
|
|
|
// Otherwise defer to RemovePred.
|
|
|
|
return RemovePred(Sec);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// This has to be the last predicate assignment.
|
|
|
|
// If the option --keep-symbol has been specified
|
|
|
|
// and at least one of those symbols is present
|
|
|
|
// (equivalently, the updated symbol table is not empty)
|
|
|
|
// the symbol table and the string table should not be removed.
|
|
|
|
if ((!Config.SymbolsToKeep.empty() || Config.KeepFileSymbols) &&
|
|
|
|
Obj.SymbolTable && !Obj.SymbolTable->empty()) {
|
|
|
|
RemovePred = [&Obj, RemovePred](const SectionBase &Sec) {
|
|
|
|
if (&Sec == Obj.SymbolTable || &Sec == Obj.SymbolTable->getStrTab())
|
|
|
|
return false;
|
|
|
|
return RemovePred(Sec);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Config.CompressionType != DebugCompressionType::None)
|
|
|
|
replaceDebugSections(Config, Obj, RemovePred, isCompressable,
|
|
|
|
[&Config, &Obj](const SectionBase *S) {
|
|
|
|
return &Obj.addSection<CompressedSection>(
|
|
|
|
*S, Config.CompressionType);
|
|
|
|
});
|
|
|
|
else if (Config.DecompressDebugSections)
|
|
|
|
replaceDebugSections(
|
|
|
|
Config, Obj, RemovePred,
|
|
|
|
[](const SectionBase &S) { return isa<CompressedSection>(&S); },
|
|
|
|
[&Obj](const SectionBase *S) {
|
|
|
|
auto CS = cast<CompressedSection>(S);
|
|
|
|
return &Obj.addSection<DecompressedSection>(*CS);
|
|
|
|
});
|
|
|
|
|
2019-02-01 15:20:36 +00:00
|
|
|
if (Error E = Obj.removeSections(RemovePred))
|
|
|
|
return E;
|
2018-10-29 21:22:58 +00:00
|
|
|
|
|
|
|
if (!Config.SectionsToRename.empty()) {
|
|
|
|
for (auto &Sec : Obj.sections()) {
|
|
|
|
const auto Iter = Config.SectionsToRename.find(Sec.Name);
|
|
|
|
if (Iter != Config.SectionsToRename.end()) {
|
|
|
|
const SectionRename &SR = Iter->second;
|
|
|
|
Sec.Name = SR.NewName;
|
[llvm-objcopy] Implement --set-section-flags.
Summary:
--set-section-flags is used to change the section flags (e.g. SHF_ALLOC) for given sections. The flags allowed are the same from the existing --rename-section=.old=.new[,flags] feature.
Additionally, make sure that --set-section-flag cannot be used with --rename-section (either the source or destination), since --rename-section accepts flags. This avoids ambiguity for something like "--rename-section=.foo=.bar,alloc --set-section-flag=.bar,code".
Reviewers: jhenderson, jakehehrlich, alexshap, espindola
Reviewed By: jhenderson, jakehehrlich
Subscribers: llvm-commits, emaste, arichardson
Differential Revision: https://reviews.llvm.org/D57198
llvm-svn: 352505
2019-01-29 15:05:38 +00:00
|
|
|
if (SR.NewFlags.hasValue())
|
|
|
|
Sec.Flags =
|
|
|
|
setSectionFlagsPreserveMask(Sec.Flags, SR.NewFlags.getValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Config.SetSectionFlags.empty()) {
|
|
|
|
for (auto &Sec : Obj.sections()) {
|
|
|
|
const auto Iter = Config.SetSectionFlags.find(Sec.Name);
|
|
|
|
if (Iter != Config.SetSectionFlags.end()) {
|
|
|
|
const SectionFlagsUpdate &SFU = Iter->second;
|
|
|
|
Sec.Flags = setSectionFlagsPreserveMask(Sec.Flags, SFU.NewFlags);
|
2018-10-29 21:22:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-12 12:41:06 +00:00
|
|
|
|
|
|
|
for (const auto &Flag : Config.AddSection) {
|
|
|
|
std::pair<StringRef, StringRef> SecPair = Flag.split("=");
|
|
|
|
StringRef SecName = SecPair.first;
|
|
|
|
StringRef File = SecPair.second;
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
|
|
|
|
MemoryBuffer::getFile(File);
|
|
|
|
if (!BufOrErr)
|
|
|
|
return createFileError(File, errorCodeToError(BufOrErr.getError()));
|
|
|
|
std::unique_ptr<MemoryBuffer> Buf = std::move(*BufOrErr);
|
|
|
|
ArrayRef<uint8_t> Data(
|
|
|
|
reinterpret_cast<const uint8_t *>(Buf->getBufferStart()),
|
|
|
|
Buf->getBufferSize());
|
|
|
|
OwnedDataSection &NewSection =
|
|
|
|
Obj.addSection<OwnedDataSection>(SecName, Data);
|
|
|
|
if (SecName.startswith(".note") && SecName != ".note.GNU-stack")
|
|
|
|
NewSection.Type = SHT_NOTE;
|
2018-10-29 21:22:58 +00:00
|
|
|
}
|
|
|
|
|
2019-03-12 12:41:06 +00:00
|
|
|
for (const auto &Flag : Config.DumpSection) {
|
|
|
|
std::pair<StringRef, StringRef> SecPair = Flag.split("=");
|
|
|
|
StringRef SecName = SecPair.first;
|
|
|
|
StringRef File = SecPair.second;
|
|
|
|
if (Error E = dumpSectionToFile(SecName, File, Obj))
|
|
|
|
return createFileError(Config.InputFilename, std::move(E));
|
2018-10-29 21:22:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!Config.AddGnuDebugLink.empty())
|
|
|
|
Obj.addSection<GnuDebugLinkSection>(Config.AddGnuDebugLink);
|
2019-01-30 14:36:53 +00:00
|
|
|
|
2019-02-25 14:12:41 +00:00
|
|
|
for (const NewSymbolInfo &SI : Config.SymbolsToAdd) {
|
|
|
|
SectionBase *Sec = Obj.findSection(SI.SectionName);
|
|
|
|
uint64_t Value = Sec ? Sec->Addr + SI.Value : SI.Value;
|
2019-02-27 10:19:53 +00:00
|
|
|
Obj.SymbolTable->addSymbol(
|
|
|
|
SI.SymbolName, SI.Bind, SI.Type, Sec, Value, SI.Visibility,
|
|
|
|
Sec ? (uint16_t)SYMBOL_SIMPLE_INDEX : (uint16_t)SHN_ABS, 0);
|
2019-02-25 14:12:41 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 09:24:22 +00:00
|
|
|
if (Config.EntryExpr)
|
|
|
|
Obj.Entry = Config.EntryExpr(Obj.Entry);
|
2019-01-30 14:36:53 +00:00
|
|
|
return Error::success();
|
2018-10-29 21:22:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 14:36:53 +00:00
|
|
|
Error executeObjcopyOnRawBinary(const CopyConfig &Config, MemoryBuffer &In,
|
|
|
|
Buffer &Out) {
|
2018-10-29 21:22:58 +00:00
|
|
|
BinaryReader Reader(Config.BinaryArch, &In);
|
|
|
|
std::unique_ptr<Object> Obj = Reader.create();
|
|
|
|
|
2019-01-07 16:59:12 +00:00
|
|
|
// Prefer OutputArch (-O<format>) if set, otherwise fallback to BinaryArch
|
|
|
|
// (-B<arch>).
|
|
|
|
const ElfType OutputElfType = getOutputElfType(
|
|
|
|
Config.OutputArch ? Config.OutputArch.getValue() : Config.BinaryArch);
|
2019-01-30 14:36:53 +00:00
|
|
|
if (Error E = handleArgs(Config, *Obj, Reader, OutputElfType))
|
|
|
|
return E;
|
2018-10-29 21:22:58 +00:00
|
|
|
std::unique_ptr<Writer> Writer =
|
|
|
|
createWriter(Config, *Obj, Out, OutputElfType);
|
[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-22 23:49:16 +00:00
|
|
|
if (Error E = Writer->finalize())
|
2019-01-30 14:36:53 +00:00
|
|
|
return E;
|
|
|
|
return Writer->write();
|
2018-10-29 21:22:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 14:36:53 +00:00
|
|
|
Error executeObjcopyOnBinary(const CopyConfig &Config,
|
|
|
|
object::ELFObjectFileBase &In, Buffer &Out) {
|
2018-10-29 21:22:58 +00:00
|
|
|
ELFReader Reader(&In);
|
|
|
|
std::unique_ptr<Object> Obj = Reader.create();
|
2019-01-07 16:59:12 +00:00
|
|
|
// Prefer OutputArch (-O<format>) if set, otherwise infer it from the input.
|
|
|
|
const ElfType OutputElfType =
|
|
|
|
Config.OutputArch ? getOutputElfType(Config.OutputArch.getValue())
|
|
|
|
: getOutputElfType(In);
|
2018-12-03 19:49:23 +00:00
|
|
|
ArrayRef<uint8_t> BuildIdBytes;
|
|
|
|
|
|
|
|
if (!Config.BuildIdLinkDir.empty()) {
|
|
|
|
BuildIdBytes = unwrapOrError(findBuildID(In));
|
|
|
|
if (BuildIdBytes.size() < 2)
|
2019-01-30 14:36:53 +00:00
|
|
|
return createFileError(
|
|
|
|
Config.InputFilename,
|
|
|
|
createStringError(object_error::parse_failed,
|
|
|
|
"build ID is smaller than two bytes."));
|
2018-12-03 19:49:23 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 18:13:30 +00:00
|
|
|
if (!Config.BuildIdLinkDir.empty() && Config.BuildIdLinkInput)
|
|
|
|
if (Error E =
|
|
|
|
linkToBuildIdDir(Config, Config.InputFilename,
|
|
|
|
Config.BuildIdLinkInput.getValue(), BuildIdBytes))
|
|
|
|
return E;
|
|
|
|
|
2019-01-30 14:36:53 +00:00
|
|
|
if (Error E = handleArgs(Config, *Obj, Reader, OutputElfType))
|
|
|
|
return E;
|
2018-10-29 21:22:58 +00:00
|
|
|
std::unique_ptr<Writer> Writer =
|
|
|
|
createWriter(Config, *Obj, Out, OutputElfType);
|
[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-22 23:49:16 +00:00
|
|
|
if (Error E = Writer->finalize())
|
2019-01-30 14:36:53 +00:00
|
|
|
return E;
|
[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-22 23:49:16 +00:00
|
|
|
if (Error E = Writer->write())
|
2019-01-30 14:36:53 +00:00
|
|
|
return E;
|
2019-01-30 18:13:30 +00:00
|
|
|
if (!Config.BuildIdLinkDir.empty() && Config.BuildIdLinkOutput)
|
|
|
|
if (Error E =
|
|
|
|
linkToBuildIdDir(Config, Config.OutputFilename,
|
|
|
|
Config.BuildIdLinkOutput.getValue(), BuildIdBytes))
|
|
|
|
return E;
|
|
|
|
|
2019-01-30 14:36:53 +00:00
|
|
|
return Error::success();
|
2018-10-29 21:22:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace elf
|
|
|
|
} // end namespace objcopy
|
|
|
|
} // end namespace llvm
|