@@ -186,27 +186,32 @@ static std::unique_ptr<Writer> createWriter(const CommonConfig &Config,
186186}
187187
188188static Error dumpSectionToFile (StringRef SecName, StringRef Filename,
189- Object &Obj) {
189+ StringRef InputFilename, Object &Obj) {
190190 for (auto &Sec : Obj.sections ()) {
191191 if (Sec.Name == SecName) {
192- if (Sec.Type == SHT_NOBITS)
193- return createStringError (object_error::parse_failed,
194- " cannot dump section '%s': it has no contents" ,
195- SecName.str ().c_str ());
192+ if (Sec.Type == SHT_NOBITS) {
193+ Error E =
194+ createStringError (object_error::parse_failed,
195+ " cannot dump section '%s': it has no contents" ,
196+ SecName.str ().c_str ());
197+ return createFileError (InputFilename, std::move (E));
198+ }
196199 Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
197200 FileOutputBuffer::create (Filename, Sec.OriginalData .size ());
198201 if (!BufferOrErr)
199- return BufferOrErr.takeError ();
202+ return createFileError (Filename, BufferOrErr.takeError () );
200203 std::unique_ptr<FileOutputBuffer> Buf = std::move (*BufferOrErr);
201204 std::copy (Sec.OriginalData .begin (), Sec.OriginalData .end (),
202205 Buf->getBufferStart ());
203206 if (Error E = Buf->commit ())
204- return E ;
207+ return createFileError (Filename, std::move (E)) ;
205208 return Error::success ();
206209 }
207210 }
208- return createStringError (object_error::parse_failed, " section '%s' not found" ,
209- SecName.str ().c_str ());
211+ Error E = createStringError (object_error::parse_failed,
212+ " section '%s' not found" , SecName.str ().c_str ());
213+
214+ return createFileError (InputFilename, std::move (E));
210215}
211216
212217Error Object::compressOrDecompressSections (const CommonConfig &Config) {
@@ -798,7 +803,8 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
798803 StringRef SectionName;
799804 StringRef FileName;
800805 std::tie (SectionName, FileName) = Flag.split (' =' );
801- if (Error E = dumpSectionToFile (SectionName, FileName, Obj))
806+ if (Error E =
807+ dumpSectionToFile (SectionName, FileName, Config.InputFilename , Obj))
802808 return E;
803809 }
804810
@@ -807,10 +813,10 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
807813 // us to avoid reporting the inappropriate errors about removing symbols
808814 // named in relocations.
809815 if (Error E = replaceAndRemoveSections (Config, ELFConfig, Obj))
810- return E ;
816+ return createFileError (Config. InputFilename , std::move (E)) ;
811817
812818 if (Error E = updateAndRemoveSymbols (Config, ELFConfig, Obj))
813- return E ;
819+ return createFileError (Config. InputFilename , std::move (E)) ;
814820
815821 if (!Config.SetSectionAlignment .empty ()) {
816822 for (SectionBase &Sec : Obj.sections ()) {
@@ -826,33 +832,37 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
826832 if (Config.ChangeSectionLMAValAll > 0 &&
827833 Seg.PAddr > std::numeric_limits<uint64_t >::max () -
828834 Config.ChangeSectionLMAValAll ) {
829- return createStringError (
835+ Error E = createStringError (
830836 errc::invalid_argument,
831837 " address 0x" + Twine::utohexstr (Seg.PAddr ) +
832838 " cannot be increased by 0x" +
833839 Twine::utohexstr (Config.ChangeSectionLMAValAll ) +
834840 " . The result would overflow" );
841+ return createFileError (Config.InputFilename , std::move (E));
835842 } else if (Config.ChangeSectionLMAValAll < 0 &&
836843 Seg.PAddr < std::numeric_limits<uint64_t >::min () -
837844 Config.ChangeSectionLMAValAll ) {
838- return createStringError (
845+ Error E = createStringError (
839846 errc::invalid_argument,
840847 " address 0x" + Twine::utohexstr (Seg.PAddr ) +
841848 " cannot be decreased by 0x" +
842849 Twine::utohexstr (std::abs (Config.ChangeSectionLMAValAll )) +
843850 " . The result would underflow" );
851+
852+ return createFileError (Config.InputFilename , std::move (E));
844853 }
845854 Seg.PAddr += Config.ChangeSectionLMAValAll ;
846855 }
847856 }
848857 }
849858
850859 if (!Config.ChangeSectionAddress .empty ()) {
851- if (Obj.Type != ELF::ET_REL)
852- return createStringError (
860+ if (Obj.Type != ELF::ET_REL) {
861+ Error E = createStringError (
853862 object_error::invalid_file_type,
854863 " cannot change section address in a non-relocatable file" );
855-
864+ return createFileError (Config.InputFilename , std::move (E));
865+ }
856866 StringMap<AddressUpdate> SectionsToUpdateAddress;
857867 for (const SectionPatternAddressUpdate &PatternUpdate :
858868 make_range (Config.ChangeSectionAddress .rbegin (),
@@ -863,22 +873,26 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
863873 .second ) {
864874 if (PatternUpdate.Update .Kind == AdjustKind::Subtract &&
865875 Sec.Addr < PatternUpdate.Update .Value ) {
866- return createStringError (
876+ Error E = createStringError (
867877 errc::invalid_argument,
868878 " address 0x" + Twine::utohexstr (Sec.Addr ) +
869879 " cannot be decreased by 0x" +
870880 Twine::utohexstr (PatternUpdate.Update .Value ) +
871881 " . The result would underflow" );
882+
883+ return createFileError (Config.InputFilename , std::move (E));
872884 }
873885 if (PatternUpdate.Update .Kind == AdjustKind::Add &&
874886 Sec.Addr > std::numeric_limits<uint64_t >::max () -
875887 PatternUpdate.Update .Value ) {
876- return createStringError (
888+ Error E = createStringError (
877889 errc::invalid_argument,
878890 " address 0x" + Twine::utohexstr (Sec.Addr ) +
879891 " cannot be increased by 0x" +
880892 Twine::utohexstr (PatternUpdate.Update .Value ) +
881893 " . The result would overflow" );
894+
895+ return createFileError (Config.InputFilename , std::move (E));
882896 }
883897
884898 switch (PatternUpdate.Update .Kind ) {
@@ -909,7 +923,7 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
909923 if (!ELFConfig.NotesToRemove .empty ()) {
910924 if (Error Err =
911925 removeNotes (Obj, E, ELFConfig.NotesToRemove , Config.ErrorCallback ))
912- return Err;
926+ return createFileError (Config. InputFilename , std::move ( Err)) ;
913927 }
914928
915929 for (const NewSectionInfo &AddedSection : Config.AddSection ) {
@@ -924,15 +938,15 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
924938 return Error::success ();
925939 };
926940 if (Error E = handleUserSection (AddedSection, AddSection))
927- return E ;
941+ return createFileError (Config. InputFilename , std::move (E)) ;
928942 }
929943
930944 for (const NewSectionInfo &NewSection : Config.UpdateSection ) {
931945 auto UpdateSection = [&](StringRef Name, ArrayRef<uint8_t > Data) {
932946 return Obj.updateSection (Name, Data);
933947 };
934948 if (Error E = handleUserSection (NewSection, UpdateSection))
935- return E ;
949+ return createFileError (Config. InputFilename , std::move (E)) ;
936950 }
937951
938952 if (!Config.AddGnuDebugLink .empty ())
@@ -943,7 +957,7 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
943957 // before adding new symbols.
944958 if (!Obj.SymbolTable && !Config.SymbolsToAdd .empty ())
945959 if (Error E = Obj.addNewSymbolTable ())
946- return E ;
960+ return createFileError (Config. InputFilename , std::move (E)) ;
947961
948962 for (const NewSymbolInfo &SI : Config.SymbolsToAdd )
949963 addSymbol (Obj, SI, ELFConfig.NewSymbolVisibility );
@@ -955,7 +969,7 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
955969 if (Iter != Config.SetSectionFlags .end ()) {
956970 const SectionFlagsUpdate &SFU = Iter->second ;
957971 if (Error E = setSectionFlagsAndType (Sec, SFU.NewFlags , Obj.Machine ))
958- return E ;
972+ return createFileError (Config. InputFilename , std::move (E)) ;
959973 }
960974 auto It2 = Config.SetSectionType .find (Sec.Name );
961975 if (It2 != Config.SetSectionType .end ())
@@ -974,7 +988,7 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
974988 Sec.Name = std::string (SR.NewName );
975989 if (SR.NewFlags ) {
976990 if (Error E = setSectionFlagsAndType (Sec, *SR.NewFlags , Obj.Machine ))
977- return E ;
991+ return createFileError (Config. InputFilename , std::move (E)) ;
978992 }
979993 RenamedSections.insert (&Sec);
980994 } else if (RelocSec && !(Sec.Flags & SHF_ALLOC))
@@ -1091,7 +1105,7 @@ Error objcopy::elf::executeObjcopyOnBinary(const CommonConfig &Config,
10911105 : getOutputElfType (In);
10921106
10931107 if (Error E = handleArgs (Config, ELFConfig, OutputElfType, **Obj))
1094- return createFileError (Config. InputFilename , std::move (E)) ;
1108+ return E ;
10951109
10961110 if (Error E = writeOutput (Config, **Obj, Out, OutputElfType))
10971111 return createFileError (Config.InputFilename , std::move (E));
0 commit comments