@@ -186,27 +186,32 @@ static std::unique_ptr<Writer> createWriter(const CommonConfig &Config,
186
186
}
187
187
188
188
static Error dumpSectionToFile (StringRef SecName, StringRef Filename,
189
- Object &Obj) {
189
+ StringRef InputFilename, Object &Obj) {
190
190
for (auto &Sec : Obj.sections ()) {
191
191
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
+ }
196
199
Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
197
200
FileOutputBuffer::create (Filename, Sec.OriginalData .size ());
198
201
if (!BufferOrErr)
199
- return BufferOrErr.takeError ();
202
+ return createFileError (Filename, BufferOrErr.takeError () );
200
203
std::unique_ptr<FileOutputBuffer> Buf = std::move (*BufferOrErr);
201
204
std::copy (Sec.OriginalData .begin (), Sec.OriginalData .end (),
202
205
Buf->getBufferStart ());
203
206
if (Error E = Buf->commit ())
204
- return E ;
207
+ return createFileError (Filename, std::move (E)) ;
205
208
return Error::success ();
206
209
}
207
210
}
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));
210
215
}
211
216
212
217
Error Object::compressOrDecompressSections (const CommonConfig &Config) {
@@ -798,7 +803,8 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
798
803
StringRef SectionName;
799
804
StringRef FileName;
800
805
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))
802
808
return E;
803
809
}
804
810
@@ -807,10 +813,10 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
807
813
// us to avoid reporting the inappropriate errors about removing symbols
808
814
// named in relocations.
809
815
if (Error E = replaceAndRemoveSections (Config, ELFConfig, Obj))
810
- return E ;
816
+ return createFileError (Config. InputFilename , std::move (E)) ;
811
817
812
818
if (Error E = updateAndRemoveSymbols (Config, ELFConfig, Obj))
813
- return E ;
819
+ return createFileError (Config. InputFilename , std::move (E)) ;
814
820
815
821
if (!Config.SetSectionAlignment .empty ()) {
816
822
for (SectionBase &Sec : Obj.sections ()) {
@@ -826,33 +832,37 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
826
832
if (Config.ChangeSectionLMAValAll > 0 &&
827
833
Seg.PAddr > std::numeric_limits<uint64_t >::max () -
828
834
Config.ChangeSectionLMAValAll ) {
829
- return createStringError (
835
+ Error E = createStringError (
830
836
errc::invalid_argument,
831
837
" address 0x" + Twine::utohexstr (Seg.PAddr ) +
832
838
" cannot be increased by 0x" +
833
839
Twine::utohexstr (Config.ChangeSectionLMAValAll ) +
834
840
" . The result would overflow" );
841
+ return createFileError (Config.InputFilename , std::move (E));
835
842
} else if (Config.ChangeSectionLMAValAll < 0 &&
836
843
Seg.PAddr < std::numeric_limits<uint64_t >::min () -
837
844
Config.ChangeSectionLMAValAll ) {
838
- return createStringError (
845
+ Error E = createStringError (
839
846
errc::invalid_argument,
840
847
" address 0x" + Twine::utohexstr (Seg.PAddr ) +
841
848
" cannot be decreased by 0x" +
842
849
Twine::utohexstr (std::abs (Config.ChangeSectionLMAValAll )) +
843
850
" . The result would underflow" );
851
+
852
+ return createFileError (Config.InputFilename , std::move (E));
844
853
}
845
854
Seg.PAddr += Config.ChangeSectionLMAValAll ;
846
855
}
847
856
}
848
857
}
849
858
850
859
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 (
853
862
object_error::invalid_file_type,
854
863
" cannot change section address in a non-relocatable file" );
855
-
864
+ return createFileError (Config.InputFilename , std::move (E));
865
+ }
856
866
StringMap<AddressUpdate> SectionsToUpdateAddress;
857
867
for (const SectionPatternAddressUpdate &PatternUpdate :
858
868
make_range (Config.ChangeSectionAddress .rbegin (),
@@ -863,22 +873,26 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
863
873
.second ) {
864
874
if (PatternUpdate.Update .Kind == AdjustKind::Subtract &&
865
875
Sec.Addr < PatternUpdate.Update .Value ) {
866
- return createStringError (
876
+ Error E = createStringError (
867
877
errc::invalid_argument,
868
878
" address 0x" + Twine::utohexstr (Sec.Addr ) +
869
879
" cannot be decreased by 0x" +
870
880
Twine::utohexstr (PatternUpdate.Update .Value ) +
871
881
" . The result would underflow" );
882
+
883
+ return createFileError (Config.InputFilename , std::move (E));
872
884
}
873
885
if (PatternUpdate.Update .Kind == AdjustKind::Add &&
874
886
Sec.Addr > std::numeric_limits<uint64_t >::max () -
875
887
PatternUpdate.Update .Value ) {
876
- return createStringError (
888
+ Error E = createStringError (
877
889
errc::invalid_argument,
878
890
" address 0x" + Twine::utohexstr (Sec.Addr ) +
879
891
" cannot be increased by 0x" +
880
892
Twine::utohexstr (PatternUpdate.Update .Value ) +
881
893
" . The result would overflow" );
894
+
895
+ return createFileError (Config.InputFilename , std::move (E));
882
896
}
883
897
884
898
switch (PatternUpdate.Update .Kind ) {
@@ -909,7 +923,7 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
909
923
if (!ELFConfig.NotesToRemove .empty ()) {
910
924
if (Error Err =
911
925
removeNotes (Obj, E, ELFConfig.NotesToRemove , Config.ErrorCallback ))
912
- return Err;
926
+ return createFileError (Config. InputFilename , std::move ( Err)) ;
913
927
}
914
928
915
929
for (const NewSectionInfo &AddedSection : Config.AddSection ) {
@@ -924,15 +938,15 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
924
938
return Error::success ();
925
939
};
926
940
if (Error E = handleUserSection (AddedSection, AddSection))
927
- return E ;
941
+ return createFileError (Config. InputFilename , std::move (E)) ;
928
942
}
929
943
930
944
for (const NewSectionInfo &NewSection : Config.UpdateSection ) {
931
945
auto UpdateSection = [&](StringRef Name, ArrayRef<uint8_t > Data) {
932
946
return Obj.updateSection (Name, Data);
933
947
};
934
948
if (Error E = handleUserSection (NewSection, UpdateSection))
935
- return E ;
949
+ return createFileError (Config. InputFilename , std::move (E)) ;
936
950
}
937
951
938
952
if (!Config.AddGnuDebugLink .empty ())
@@ -943,7 +957,7 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
943
957
// before adding new symbols.
944
958
if (!Obj.SymbolTable && !Config.SymbolsToAdd .empty ())
945
959
if (Error E = Obj.addNewSymbolTable ())
946
- return E ;
960
+ return createFileError (Config. InputFilename , std::move (E)) ;
947
961
948
962
for (const NewSymbolInfo &SI : Config.SymbolsToAdd )
949
963
addSymbol (Obj, SI, ELFConfig.NewSymbolVisibility );
@@ -955,7 +969,7 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
955
969
if (Iter != Config.SetSectionFlags .end ()) {
956
970
const SectionFlagsUpdate &SFU = Iter->second ;
957
971
if (Error E = setSectionFlagsAndType (Sec, SFU.NewFlags , Obj.Machine ))
958
- return E ;
972
+ return createFileError (Config. InputFilename , std::move (E)) ;
959
973
}
960
974
auto It2 = Config.SetSectionType .find (Sec.Name );
961
975
if (It2 != Config.SetSectionType .end ())
@@ -974,7 +988,7 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
974
988
Sec.Name = std::string (SR.NewName );
975
989
if (SR.NewFlags ) {
976
990
if (Error E = setSectionFlagsAndType (Sec, *SR.NewFlags , Obj.Machine ))
977
- return E ;
991
+ return createFileError (Config. InputFilename , std::move (E)) ;
978
992
}
979
993
RenamedSections.insert (&Sec);
980
994
} else if (RelocSec && !(Sec.Flags & SHF_ALLOC))
@@ -1091,7 +1105,7 @@ Error objcopy::elf::executeObjcopyOnBinary(const CommonConfig &Config,
1091
1105
: getOutputElfType (In);
1092
1106
1093
1107
if (Error E = handleArgs (Config, ELFConfig, OutputElfType, **Obj))
1094
- return createFileError (Config. InputFilename , std::move (E)) ;
1108
+ return E ;
1095
1109
1096
1110
if (Error E = writeOutput (Config, **Obj, Out, OutputElfType))
1097
1111
return createFileError (Config.InputFilename , std::move (E));
0 commit comments