10
10
#include " Buffer.h"
11
11
#include " CopyConfig.h"
12
12
#include " Object.h"
13
- #include " llvm-objcopy.h"
14
13
#include " llvm/ADT/BitmaskEnum.h"
15
14
#include " llvm/ADT/DenseSet.h"
16
15
#include " llvm/ADT/Optional.h"
@@ -266,19 +265,23 @@ static Error linkToBuildIdDir(const CopyConfig &Config, StringRef ToLink,
266
265
267
266
static Error splitDWOToFile (const CopyConfig &Config, const Reader &Reader,
268
267
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
+
270
272
auto OnlyKeepDWOPred = [&DWOFile](const SectionBase &Sec) {
271
- return onlyKeepDWOPred (*DWOFile, Sec);
273
+ return onlyKeepDWOPred (** DWOFile, Sec);
272
274
};
273
- if (Error E = DWOFile-> removeSections (Config. AllowBrokenLinks ,
274
- OnlyKeepDWOPred))
275
+ if (Error E =
276
+ (*DWOFile)-> removeSections (Config. AllowBrokenLinks , OnlyKeepDWOPred))
275
277
return E;
276
278
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 ;
279
281
}
280
282
FileBuffer FB (File);
281
- auto Writer = createWriter (Config, *DWOFile, FB, OutputElfType);
283
+ std::unique_ptr<Writer> Writer =
284
+ createWriter (Config, **DWOFile, FB, OutputElfType);
282
285
if (Error E = Writer->finalize ())
283
286
return E;
284
287
return Writer->write ();
@@ -313,12 +316,12 @@ static bool isCompressable(const SectionBase &Sec) {
313
316
StringRef (Sec.Name ).startswith (" .debug" );
314
317
}
315
318
316
- static void replaceDebugSections (
319
+ static Error replaceDebugSections (
317
320
Object &Obj, SectionPred &RemovePred,
318
321
function_ref<bool (const SectionBase &)> shouldReplace,
319
- function_ref<SectionBase *(const SectionBase *)> addSection) {
322
+ function_ref<Expected< SectionBase *> (const SectionBase *)> addSection) {
320
323
// 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,
322
325
// because it would mutate the sections array.
323
326
SmallVector<SectionBase *, 13 > ToReplace;
324
327
for (auto &Sec : Obj.sections ())
@@ -327,8 +330,13 @@ static void replaceDebugSections(
327
330
328
331
// Build a mapping from original section to a new one.
329
332
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
+ }
332
340
333
341
// Now we want to update the target sections of relocation
334
342
// sections. Also we will update the relocations themselves
@@ -339,6 +347,8 @@ static void replaceDebugSections(
339
347
RemovePred = [shouldReplace, RemovePred](const SectionBase &Sec) {
340
348
return shouldReplace (Sec) || RemovePred (Sec);
341
349
};
350
+
351
+ return Error::success ();
342
352
}
343
353
344
354
static bool isUnneededSymbol (const Symbol &Sym) {
@@ -577,20 +587,28 @@ static Error replaceAndRemoveSections(const CopyConfig &Config, Object &Obj) {
577
587
};
578
588
}
579
589
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
+ }
594
612
595
613
return Obj.removeSections (Config.AllowBrokenLinks , RemovePred);
596
614
}
@@ -740,9 +758,9 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj,
740
758
741
759
// If the symbol table was previously removed, we need to create a new one
742
760
// 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;
746
764
747
765
for (const NewSymbolInfo &SI : Config.ELF ->SymbolsToAdd ) {
748
766
SectionBase *Sec = Obj.findSection (SI.SectionName );
@@ -769,34 +787,42 @@ static Error writeOutput(const CopyConfig &Config, Object &Obj, Buffer &Out,
769
787
Error executeObjcopyOnIHex (const CopyConfig &Config, MemoryBuffer &In,
770
788
Buffer &Out) {
771
789
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
+
773
794
const ElfType OutputElfType =
774
795
getOutputElfType (Config.OutputArch .getValueOr (MachineInfo ()));
775
- if (Error E = handleArgs (Config, *Obj, Reader, OutputElfType))
796
+ if (Error E = handleArgs (Config, ** Obj, Reader, OutputElfType))
776
797
return E;
777
- return writeOutput (Config, *Obj, Out, OutputElfType);
798
+ return writeOutput (Config, ** Obj, Out, OutputElfType);
778
799
}
779
800
780
801
Error executeObjcopyOnRawBinary (const CopyConfig &Config, MemoryBuffer &In,
781
802
Buffer &Out) {
782
803
uint8_t NewSymbolVisibility =
783
804
Config.ELF ->NewSymbolVisibility .getValueOr ((uint8_t )ELF::STV_DEFAULT);
784
805
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 ();
786
809
787
810
// Prefer OutputArch (-O<format>) if set, otherwise fallback to BinaryArch
788
811
// (-B<arch>).
789
812
const ElfType OutputElfType =
790
813
getOutputElfType (Config.OutputArch .getValueOr (MachineInfo ()));
791
- if (Error E = handleArgs (Config, *Obj, Reader, OutputElfType))
814
+ if (Error E = handleArgs (Config, ** Obj, Reader, OutputElfType))
792
815
return E;
793
- return writeOutput (Config, *Obj, Out, OutputElfType);
816
+ return writeOutput (Config, ** Obj, Out, OutputElfType);
794
817
}
795
818
796
819
Error executeObjcopyOnBinary (const CopyConfig &Config,
797
820
object::ELFObjectFileBase &In, Buffer &Out) {
798
821
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 ();
800
826
// Prefer OutputArch (-O<format>) if set, otherwise infer it from the input.
801
827
const ElfType OutputElfType =
802
828
Config.OutputArch ? getOutputElfType (Config.OutputArch .getValue ())
@@ -822,10 +848,10 @@ Error executeObjcopyOnBinary(const CopyConfig &Config,
822
848
Config.BuildIdLinkInput .getValue (), BuildIdBytes))
823
849
return E;
824
850
825
- if (Error E = handleArgs (Config, *Obj, Reader, OutputElfType))
851
+ if (Error E = handleArgs (Config, ** Obj, Reader, OutputElfType))
826
852
return createFileError (Config.InputFilename , std::move (E));
827
853
828
- if (Error E = writeOutput (Config, *Obj, Out, OutputElfType))
854
+ if (Error E = writeOutput (Config, ** Obj, Out, OutputElfType))
829
855
return createFileError (Config.InputFilename , std::move (E));
830
856
if (!Config.BuildIdLinkDir .empty () && Config.BuildIdLinkOutput )
831
857
if (Error E =
0 commit comments