Skip to content

Commit 66bea0d

Browse files
authored
[llvm-objcopy] Fix prints wrong path when dump-section output path doesn't exist (llvm#125345)
Fix printing the correct file path in the error message when the output file specified by `--dump-section` cannot be opened Fixes: llvm#125113 on ELF, MachO, Wasm
1 parent ee80664 commit 66bea0d

File tree

6 files changed

+64
-49
lines changed

6 files changed

+64
-49
lines changed

llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -186,27 +186,28 @@ static std::unique_ptr<Writer> createWriter(const CommonConfig &Config,
186186
}
187187

188188
static 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) {
192192
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());
193+
return createFileError(InputFilename, object_error::parse_failed,
194+
"cannot dump section '%s': it has no contents",
195+
SecName.str().c_str());
196196
Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
197197
FileOutputBuffer::create(Filename, Sec.OriginalData.size());
198198
if (!BufferOrErr)
199-
return BufferOrErr.takeError();
199+
return createFileError(Filename, BufferOrErr.takeError());
200200
std::unique_ptr<FileOutputBuffer> Buf = std::move(*BufferOrErr);
201201
std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(),
202202
Buf->getBufferStart());
203203
if (Error E = Buf->commit())
204-
return E;
204+
return createFileError(Filename, std::move(E));
205205
return Error::success();
206206
}
207207
}
208-
return createStringError(object_error::parse_failed, "section '%s' not found",
209-
SecName.str().c_str());
208+
209+
return createFileError(InputFilename, object_error::parse_failed,
210+
"section '%s' not found", SecName.str().c_str());
210211
}
211212

212213
Error Object::compressOrDecompressSections(const CommonConfig &Config) {
@@ -798,7 +799,8 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
798799
StringRef SectionName;
799800
StringRef FileName;
800801
std::tie(SectionName, FileName) = Flag.split('=');
801-
if (Error E = dumpSectionToFile(SectionName, FileName, Obj))
802+
if (Error E =
803+
dumpSectionToFile(SectionName, FileName, Config.InputFilename, Obj))
802804
return E;
803805
}
804806

@@ -807,10 +809,10 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
807809
// us to avoid reporting the inappropriate errors about removing symbols
808810
// named in relocations.
809811
if (Error E = replaceAndRemoveSections(Config, ELFConfig, Obj))
810-
return E;
812+
return createFileError(Config.InputFilename, std::move(E));
811813

812814
if (Error E = updateAndRemoveSymbols(Config, ELFConfig, Obj))
813-
return E;
815+
return createFileError(Config.InputFilename, std::move(E));
814816

815817
if (!Config.SetSectionAlignment.empty()) {
816818
for (SectionBase &Sec : Obj.sections()) {
@@ -826,17 +828,17 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
826828
if (Config.ChangeSectionLMAValAll > 0 &&
827829
Seg.PAddr > std::numeric_limits<uint64_t>::max() -
828830
Config.ChangeSectionLMAValAll) {
829-
return createStringError(
830-
errc::invalid_argument,
831+
return createFileError(
832+
Config.InputFilename, errc::invalid_argument,
831833
"address 0x" + Twine::utohexstr(Seg.PAddr) +
832834
" cannot be increased by 0x" +
833835
Twine::utohexstr(Config.ChangeSectionLMAValAll) +
834836
". The result would overflow");
835837
} else if (Config.ChangeSectionLMAValAll < 0 &&
836838
Seg.PAddr < std::numeric_limits<uint64_t>::min() -
837839
Config.ChangeSectionLMAValAll) {
838-
return createStringError(
839-
errc::invalid_argument,
840+
return createFileError(
841+
Config.InputFilename, errc::invalid_argument,
840842
"address 0x" + Twine::utohexstr(Seg.PAddr) +
841843
" cannot be decreased by 0x" +
842844
Twine::utohexstr(std::abs(Config.ChangeSectionLMAValAll)) +
@@ -849,10 +851,9 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
849851

850852
if (!Config.ChangeSectionAddress.empty()) {
851853
if (Obj.Type != ELF::ET_REL)
852-
return createStringError(
853-
object_error::invalid_file_type,
854+
return createFileError(
855+
Config.InputFilename, object_error::invalid_file_type,
854856
"cannot change section address in a non-relocatable file");
855-
856857
StringMap<AddressUpdate> SectionsToUpdateAddress;
857858
for (const SectionPatternAddressUpdate &PatternUpdate :
858859
make_range(Config.ChangeSectionAddress.rbegin(),
@@ -863,8 +864,8 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
863864
.second) {
864865
if (PatternUpdate.Update.Kind == AdjustKind::Subtract &&
865866
Sec.Addr < PatternUpdate.Update.Value) {
866-
return createStringError(
867-
errc::invalid_argument,
867+
return createFileError(
868+
Config.InputFilename, errc::invalid_argument,
868869
"address 0x" + Twine::utohexstr(Sec.Addr) +
869870
" cannot be decreased by 0x" +
870871
Twine::utohexstr(PatternUpdate.Update.Value) +
@@ -873,8 +874,8 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
873874
if (PatternUpdate.Update.Kind == AdjustKind::Add &&
874875
Sec.Addr > std::numeric_limits<uint64_t>::max() -
875876
PatternUpdate.Update.Value) {
876-
return createStringError(
877-
errc::invalid_argument,
877+
return createFileError(
878+
Config.InputFilename, errc::invalid_argument,
878879
"address 0x" + Twine::utohexstr(Sec.Addr) +
879880
" cannot be increased by 0x" +
880881
Twine::utohexstr(PatternUpdate.Update.Value) +
@@ -909,7 +910,7 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
909910
if (!ELFConfig.NotesToRemove.empty()) {
910911
if (Error Err =
911912
removeNotes(Obj, E, ELFConfig.NotesToRemove, Config.ErrorCallback))
912-
return Err;
913+
return createFileError(Config.InputFilename, std::move(Err));
913914
}
914915

915916
for (const NewSectionInfo &AddedSection : Config.AddSection) {
@@ -924,15 +925,15 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
924925
return Error::success();
925926
};
926927
if (Error E = handleUserSection(AddedSection, AddSection))
927-
return E;
928+
return createFileError(Config.InputFilename, std::move(E));
928929
}
929930

930931
for (const NewSectionInfo &NewSection : Config.UpdateSection) {
931932
auto UpdateSection = [&](StringRef Name, ArrayRef<uint8_t> Data) {
932933
return Obj.updateSection(Name, Data);
933934
};
934935
if (Error E = handleUserSection(NewSection, UpdateSection))
935-
return E;
936+
return createFileError(Config.InputFilename, std::move(E));
936937
}
937938

938939
if (!Config.AddGnuDebugLink.empty())
@@ -943,7 +944,7 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
943944
// before adding new symbols.
944945
if (!Obj.SymbolTable && !Config.SymbolsToAdd.empty())
945946
if (Error E = Obj.addNewSymbolTable())
946-
return E;
947+
return createFileError(Config.InputFilename, std::move(E));
947948

948949
for (const NewSymbolInfo &SI : Config.SymbolsToAdd)
949950
addSymbol(Obj, SI, ELFConfig.NewSymbolVisibility);
@@ -955,7 +956,7 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
955956
if (Iter != Config.SetSectionFlags.end()) {
956957
const SectionFlagsUpdate &SFU = Iter->second;
957958
if (Error E = setSectionFlagsAndType(Sec, SFU.NewFlags, Obj.Machine))
958-
return E;
959+
return createFileError(Config.InputFilename, std::move(E));
959960
}
960961
auto It2 = Config.SetSectionType.find(Sec.Name);
961962
if (It2 != Config.SetSectionType.end())
@@ -974,7 +975,7 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
974975
Sec.Name = std::string(SR.NewName);
975976
if (SR.NewFlags) {
976977
if (Error E = setSectionFlagsAndType(Sec, *SR.NewFlags, Obj.Machine))
977-
return E;
978+
return createFileError(Config.InputFilename, std::move(E));
978979
}
979980
RenamedSections.insert(&Sec);
980981
} else if (RelocSec && !(Sec.Flags & SHF_ALLOC))
@@ -1091,7 +1092,7 @@ Error objcopy::elf::executeObjcopyOnBinary(const CommonConfig &Config,
10911092
: getOutputElfType(In);
10921093

10931094
if (Error E = handleArgs(Config, ELFConfig, OutputElfType, **Obj))
1094-
return createFileError(Config.InputFilename, std::move(E));
1095+
return E;
10951096

10961097
if (Error E = writeOutput(Config, **Obj, Out, OutputElfType))
10971098
return createFileError(Config.InputFilename, std::move(E));

llvm/lib/ObjCopy/MachO/MachOObjcopy.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -306,25 +306,25 @@ static Error processLoadCommands(const MachOConfig &MachOConfig, Object &Obj) {
306306
}
307307

308308
static Error dumpSectionToFile(StringRef SecName, StringRef Filename,
309-
Object &Obj) {
309+
StringRef InputFilename, Object &Obj) {
310310
for (LoadCommand &LC : Obj.LoadCommands)
311311
for (const std::unique_ptr<Section> &Sec : LC.Sections) {
312312
if (Sec->CanonicalName == SecName) {
313313
Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
314314
FileOutputBuffer::create(Filename, Sec->Content.size());
315315
if (!BufferOrErr)
316-
return BufferOrErr.takeError();
316+
return createFileError(Filename, BufferOrErr.takeError());
317317
std::unique_ptr<FileOutputBuffer> Buf = std::move(*BufferOrErr);
318318
llvm::copy(Sec->Content, Buf->getBufferStart());
319319

320320
if (Error E = Buf->commit())
321-
return E;
321+
return createFileError(Filename, std::move(E));
322322
return Error::success();
323323
}
324324
}
325325

326-
return createStringError(object_error::parse_failed, "section '%s' not found",
327-
SecName.str().c_str());
326+
return createFileError(InputFilename, object_error::parse_failed,
327+
"section '%s' not found", SecName.str().c_str());
328328
}
329329

330330
static Error addSection(const NewSectionInfo &NewSection, Object &Obj) {
@@ -426,12 +426,13 @@ static Error handleArgs(const CommonConfig &Config,
426426
StringRef SectionName;
427427
StringRef FileName;
428428
std::tie(SectionName, FileName) = Flag.split('=');
429-
if (Error E = dumpSectionToFile(SectionName, FileName, Obj))
429+
if (Error E =
430+
dumpSectionToFile(SectionName, FileName, Config.InputFilename, Obj))
430431
return E;
431432
}
432433

433434
if (Error E = removeSections(Config, Obj))
434-
return E;
435+
return createFileError(Config.InputFilename, std::move(E));
435436

436437
// Mark symbols to determine which symbols are still needed.
437438
if (Config.StripAll)
@@ -446,20 +447,20 @@ static Error handleArgs(const CommonConfig &Config,
446447

447448
for (const NewSectionInfo &NewSection : Config.AddSection) {
448449
if (Error E = isValidMachOCannonicalName(NewSection.SectionName))
449-
return E;
450+
return createFileError(Config.InputFilename, std::move(E));
450451
if (Error E = addSection(NewSection, Obj))
451-
return E;
452+
return createFileError(Config.InputFilename, std::move(E));
452453
}
453454

454455
for (const NewSectionInfo &NewSection : Config.UpdateSection) {
455456
if (Error E = isValidMachOCannonicalName(NewSection.SectionName))
456-
return E;
457+
return createFileError(Config.InputFilename, std::move(E));
457458
if (Error E = updateSection(NewSection, Obj))
458-
return E;
459+
return createFileError(Config.InputFilename, std::move(E));
459460
}
460461

461462
if (Error E = processLoadCommands(MachOConfig, Obj))
462-
return E;
463+
return createFileError(Config.InputFilename, std::move(E));
463464

464465
return Error::success();
465466
}
@@ -479,7 +480,7 @@ Error objcopy::macho::executeObjcopyOnBinary(const CommonConfig &Config,
479480
Config.InputFilename.str().c_str());
480481

481482
if (Error E = handleArgs(Config, MachOConfig, **O))
482-
return createFileError(Config.InputFilename, std::move(E));
483+
return E;
483484

484485
// Page size used for alignment of segment sizes in Mach-O executables and
485486
// dynamic libraries.

llvm/lib/ObjCopy/wasm/WasmObjcopy.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,23 @@ static bool isCommentSection(const Section &Sec) {
3838
}
3939

4040
static Error dumpSectionToFile(StringRef SecName, StringRef Filename,
41-
Object &Obj) {
41+
StringRef InputFilename, Object &Obj) {
4242
for (const Section &Sec : Obj.Sections) {
4343
if (Sec.Name == SecName) {
4444
ArrayRef<uint8_t> Contents = Sec.Contents;
4545
Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
4646
FileOutputBuffer::create(Filename, Contents.size());
4747
if (!BufferOrErr)
48-
return BufferOrErr.takeError();
48+
return createFileError(Filename, BufferOrErr.takeError());
4949
std::unique_ptr<FileOutputBuffer> Buf = std::move(*BufferOrErr);
5050
std::copy(Contents.begin(), Contents.end(), Buf->getBufferStart());
5151
if (Error E = Buf->commit())
52-
return E;
52+
return createFileError(Filename, std::move(E));
5353
return Error::success();
5454
}
5555
}
56-
return createStringError(errc::invalid_argument, "section '%s' not found",
57-
SecName.str().c_str());
56+
return createFileError(Filename, errc::invalid_argument,
57+
"section '%s' not found", SecName.str().c_str());
5858
}
5959

6060
static void removeSections(const CommonConfig &Config, Object &Obj) {
@@ -115,8 +115,9 @@ static Error handleArgs(const CommonConfig &Config, Object &Obj) {
115115
StringRef SecName;
116116
StringRef FileName;
117117
std::tie(SecName, FileName) = Flag.split("=");
118-
if (Error E = dumpSectionToFile(SecName, FileName, Obj))
119-
return createFileError(FileName, std::move(E));
118+
if (Error E =
119+
dumpSectionToFile(SecName, FileName, Config.InputFilename, Obj))
120+
return E;
120121
}
121122

122123
removeSections(Config, Obj);

llvm/test/tools/llvm-objcopy/ELF/dump-section.test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,7 @@ ProgramHeaders:
6464
# RUN: not llvm-objcopy --dump-section .text= %t /dev/null 2>&1 | FileCheck %s --check-prefix=ERR2
6565

6666
# ERR2: error: bad format for --dump-section, expected section=file
67+
68+
# RUN: not llvm-objcopy --dump-section .text=not_exists/text-section %t 2>&1 \
69+
# RUN: | FileCheck -DMSG=%errc_ENOENT %s -DINPUT=%t --check-prefix=NO-SUCH-PATH
70+
# NO-SUCH-PATH: error: 'not_exists/text-section': [[MSG]]

llvm/test/tools/llvm-objcopy/MachO/dump-section.test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
# RUN: | FileCheck %s -DINPUT=%t --check-prefix=NO-SUCH-SECTION
2222
# NO-SUCH-SECTION: error: '[[INPUT]]': section '__TEXT,__foo' not found
2323

24+
# RUN: not llvm-objcopy --dump-section __TEXT,__text=not_exists/text-section %t 2>&1 \
25+
# RUN: | FileCheck -DMSG=%errc_ENOENT %s -DINPUT=%t --check-prefix=NO-SUCH-PATH
26+
# NO-SUCH-PATH: error: 'not_exists/text-section': [[MSG]]
27+
2428
--- !mach-o
2529
FileHeader:
2630
magic: 0xFEEDFACF

llvm/test/tools/llvm-objcopy/wasm/dump-section.test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828

2929
# REMOVED-NOT: producers
3030

31+
# RUN: not llvm-objcopy --dump-section producers=not_exists/text-section %t 2>&1 \
32+
# RUN: | FileCheck -DMSG=%errc_ENOENT %s -DINPUT=%t --check-prefix=NO-SUCH-PATH
33+
# NO-SUCH-PATH: error: 'not_exists/text-section': [[MSG]]
34+
3135
--- !WASM
3236
FileHeader:
3337
Version: 0x00000001

0 commit comments

Comments
 (0)