Skip to content

Commit 0373c76

Browse files
committed
[llvm-objcopy][NFC] refactor error handling. part 3.
Remove usages of special error reporting functions(error(), reportError()). Errors are reported as Expected<>/Error returning values. This part is for ELF subfolder of llvm-objcopy. Testing: check-all. Differential Revision: https://reviews.llvm.org/D87987
1 parent 045a620 commit 0373c76

File tree

9 files changed

+756
-511
lines changed

9 files changed

+756
-511
lines changed

llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include "Object.h"
1313
#include "Reader.h"
1414
#include "Writer.h"
15-
#include "llvm-objcopy.h"
1615

1716
#include "llvm/Object/Binary.h"
1817
#include "llvm/Object/COFF.h"

llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp

Lines changed: 65 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include "Buffer.h"
1111
#include "CopyConfig.h"
1212
#include "Object.h"
13-
#include "llvm-objcopy.h"
1413
#include "llvm/ADT/BitmaskEnum.h"
1514
#include "llvm/ADT/DenseSet.h"
1615
#include "llvm/ADT/Optional.h"
@@ -266,19 +265,23 @@ static Error linkToBuildIdDir(const CopyConfig &Config, StringRef ToLink,
266265

267266
static Error splitDWOToFile(const CopyConfig &Config, const Reader &Reader,
268267
StringRef File, ElfType OutputElfType) {
269-
auto DWOFile = Reader.create(false);
268+
Expected<std::unique_ptr<Object>> DWOFile = Reader.create(false);
269+
if (!DWOFile)
270+
return DWOFile.takeError();
271+
270272
auto OnlyKeepDWOPred = [&DWOFile](const SectionBase &Sec) {
271-
return onlyKeepDWOPred(*DWOFile, Sec);
273+
return onlyKeepDWOPred(**DWOFile, Sec);
272274
};
273-
if (Error E = DWOFile->removeSections(Config.AllowBrokenLinks,
274-
OnlyKeepDWOPred))
275+
if (Error E =
276+
(*DWOFile)->removeSections(Config.AllowBrokenLinks, OnlyKeepDWOPred))
275277
return E;
276278
if (Config.OutputArch) {
277-
DWOFile->Machine = Config.OutputArch.getValue().EMachine;
278-
DWOFile->OSABI = Config.OutputArch.getValue().OSABI;
279+
(*DWOFile)->Machine = Config.OutputArch.getValue().EMachine;
280+
(*DWOFile)->OSABI = Config.OutputArch.getValue().OSABI;
279281
}
280282
FileBuffer FB(File);
281-
auto Writer = createWriter(Config, *DWOFile, FB, OutputElfType);
283+
std::unique_ptr<Writer> Writer =
284+
createWriter(Config, **DWOFile, FB, OutputElfType);
282285
if (Error E = Writer->finalize())
283286
return E;
284287
return Writer->write();
@@ -313,12 +316,12 @@ static bool isCompressable(const SectionBase &Sec) {
313316
StringRef(Sec.Name).startswith(".debug");
314317
}
315318

316-
static void replaceDebugSections(
319+
static Error replaceDebugSections(
317320
Object &Obj, SectionPred &RemovePred,
318321
function_ref<bool(const SectionBase &)> shouldReplace,
319-
function_ref<SectionBase *(const SectionBase *)> addSection) {
322+
function_ref<Expected<SectionBase *>(const SectionBase *)> addSection) {
320323
// Build a list of the debug sections we are going to replace.
321-
// We can't call `addSection` while iterating over sections,
324+
// We can't call `AddSection` while iterating over sections,
322325
// because it would mutate the sections array.
323326
SmallVector<SectionBase *, 13> ToReplace;
324327
for (auto &Sec : Obj.sections())
@@ -327,8 +330,13 @@ static void replaceDebugSections(
327330

328331
// Build a mapping from original section to a new one.
329332
DenseMap<SectionBase *, SectionBase *> FromTo;
330-
for (SectionBase *S : ToReplace)
331-
FromTo[S] = addSection(S);
333+
for (SectionBase *S : ToReplace) {
334+
Expected<SectionBase *> NewSection = addSection(S);
335+
if (!NewSection)
336+
return NewSection.takeError();
337+
338+
FromTo[S] = *NewSection;
339+
}
332340

333341
// Now we want to update the target sections of relocation
334342
// sections. Also we will update the relocations themselves
@@ -339,6 +347,8 @@ static void replaceDebugSections(
339347
RemovePred = [shouldReplace, RemovePred](const SectionBase &Sec) {
340348
return shouldReplace(Sec) || RemovePred(Sec);
341349
};
350+
351+
return Error::success();
342352
}
343353

344354
static bool isUnneededSymbol(const Symbol &Sym) {
@@ -577,20 +587,28 @@ static Error replaceAndRemoveSections(const CopyConfig &Config, Object &Obj) {
577587
};
578588
}
579589

580-
if (Config.CompressionType != DebugCompressionType::None)
581-
replaceDebugSections(Obj, RemovePred, isCompressable,
582-
[&Config, &Obj](const SectionBase *S) {
583-
return &Obj.addSection<CompressedSection>(
584-
*S, Config.CompressionType);
585-
});
586-
else if (Config.DecompressDebugSections)
587-
replaceDebugSections(
588-
Obj, RemovePred,
589-
[](const SectionBase &S) { return isa<CompressedSection>(&S); },
590-
[&Obj](const SectionBase *S) {
591-
auto CS = cast<CompressedSection>(S);
592-
return &Obj.addSection<DecompressedSection>(*CS);
593-
});
590+
if (Config.CompressionType != DebugCompressionType::None) {
591+
if (Error Err = replaceDebugSections(
592+
Obj, RemovePred, isCompressable,
593+
[&Config, &Obj](const SectionBase *S) -> Expected<SectionBase *> {
594+
Expected<CompressedSection> NewSection =
595+
CompressedSection::create(*S, Config.CompressionType);
596+
if (!NewSection)
597+
return NewSection.takeError();
598+
599+
return &Obj.addSection<CompressedSection>(std::move(*NewSection));
600+
}))
601+
return Err;
602+
} else if (Config.DecompressDebugSections) {
603+
if (Error Err = replaceDebugSections(
604+
Obj, RemovePred,
605+
[](const SectionBase &S) { return isa<CompressedSection>(&S); },
606+
[&Obj](const SectionBase *S) {
607+
const CompressedSection *CS = cast<CompressedSection>(S);
608+
return &Obj.addSection<DecompressedSection>(*CS);
609+
}))
610+
return Err;
611+
}
594612

595613
return Obj.removeSections(Config.AllowBrokenLinks, RemovePred);
596614
}
@@ -740,9 +758,9 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj,
740758

741759
// If the symbol table was previously removed, we need to create a new one
742760
// before adding new symbols.
743-
if (!Obj.SymbolTable && !Config.ELF->SymbolsToAdd.empty()) {
744-
Obj.addNewSymbolTable();
745-
}
761+
if (!Obj.SymbolTable && !Config.ELF->SymbolsToAdd.empty())
762+
if (Error E = Obj.addNewSymbolTable())
763+
return E;
746764

747765
for (const NewSymbolInfo &SI : Config.ELF->SymbolsToAdd) {
748766
SectionBase *Sec = Obj.findSection(SI.SectionName);
@@ -769,34 +787,42 @@ static Error writeOutput(const CopyConfig &Config, Object &Obj, Buffer &Out,
769787
Error executeObjcopyOnIHex(const CopyConfig &Config, MemoryBuffer &In,
770788
Buffer &Out) {
771789
IHexReader Reader(&In);
772-
std::unique_ptr<Object> Obj = Reader.create(true);
790+
Expected<std::unique_ptr<Object>> Obj = Reader.create(true);
791+
if (!Obj)
792+
return Obj.takeError();
793+
773794
const ElfType OutputElfType =
774795
getOutputElfType(Config.OutputArch.getValueOr(MachineInfo()));
775-
if (Error E = handleArgs(Config, *Obj, Reader, OutputElfType))
796+
if (Error E = handleArgs(Config, **Obj, Reader, OutputElfType))
776797
return E;
777-
return writeOutput(Config, *Obj, Out, OutputElfType);
798+
return writeOutput(Config, **Obj, Out, OutputElfType);
778799
}
779800

780801
Error executeObjcopyOnRawBinary(const CopyConfig &Config, MemoryBuffer &In,
781802
Buffer &Out) {
782803
uint8_t NewSymbolVisibility =
783804
Config.ELF->NewSymbolVisibility.getValueOr((uint8_t)ELF::STV_DEFAULT);
784805
BinaryReader Reader(&In, NewSymbolVisibility);
785-
std::unique_ptr<Object> Obj = Reader.create(true);
806+
Expected<std::unique_ptr<Object>> Obj = Reader.create(true);
807+
if (!Obj)
808+
return Obj.takeError();
786809

787810
// Prefer OutputArch (-O<format>) if set, otherwise fallback to BinaryArch
788811
// (-B<arch>).
789812
const ElfType OutputElfType =
790813
getOutputElfType(Config.OutputArch.getValueOr(MachineInfo()));
791-
if (Error E = handleArgs(Config, *Obj, Reader, OutputElfType))
814+
if (Error E = handleArgs(Config, **Obj, Reader, OutputElfType))
792815
return E;
793-
return writeOutput(Config, *Obj, Out, OutputElfType);
816+
return writeOutput(Config, **Obj, Out, OutputElfType);
794817
}
795818

796819
Error executeObjcopyOnBinary(const CopyConfig &Config,
797820
object::ELFObjectFileBase &In, Buffer &Out) {
798821
ELFReader Reader(&In, Config.ExtractPartition);
799-
std::unique_ptr<Object> Obj = Reader.create(!Config.SymbolsToAdd.empty());
822+
Expected<std::unique_ptr<Object>> Obj =
823+
Reader.create(!Config.SymbolsToAdd.empty());
824+
if (!Obj)
825+
return Obj.takeError();
800826
// Prefer OutputArch (-O<format>) if set, otherwise infer it from the input.
801827
const ElfType OutputElfType =
802828
Config.OutputArch ? getOutputElfType(Config.OutputArch.getValue())
@@ -822,10 +848,10 @@ Error executeObjcopyOnBinary(const CopyConfig &Config,
822848
Config.BuildIdLinkInput.getValue(), BuildIdBytes))
823849
return E;
824850

825-
if (Error E = handleArgs(Config, *Obj, Reader, OutputElfType))
851+
if (Error E = handleArgs(Config, **Obj, Reader, OutputElfType))
826852
return createFileError(Config.InputFilename, std::move(E));
827853

828-
if (Error E = writeOutput(Config, *Obj, Out, OutputElfType))
854+
if (Error E = writeOutput(Config, **Obj, Out, OutputElfType))
829855
return createFileError(Config.InputFilename, std::move(E));
830856
if (!Config.BuildIdLinkDir.empty() && Config.BuildIdLinkOutput)
831857
if (Error E =

0 commit comments

Comments
 (0)