Skip to content

Commit cf63b7e

Browse files
committed
[llvm-objcopy] Fix prints wrong path when section output path doesn't exist
1 parent 7612dcc commit cf63b7e

File tree

6 files changed

+191
-46
lines changed

6 files changed

+191
-46
lines changed

llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -186,27 +186,32 @@ 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) {
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

212217
Error 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));

llvm/lib/ObjCopy/MachO/MachOObjcopy.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -306,25 +306,26 @@ 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+
Error E = createStringError(object_error::parse_failed,
327+
"section '%s' not found", SecName.str().c_str());
328+
return createFileError(InputFilename, std::move(E));
328329
}
329330

330331
static Error addSection(const NewSectionInfo &NewSection, Object &Obj) {
@@ -426,12 +427,13 @@ static Error handleArgs(const CommonConfig &Config,
426427
StringRef SectionName;
427428
StringRef FileName;
428429
std::tie(SectionName, FileName) = Flag.split('=');
429-
if (Error E = dumpSectionToFile(SectionName, FileName, Obj))
430+
if (Error E =
431+
dumpSectionToFile(SectionName, FileName, Config.InputFilename, Obj))
430432
return E;
431433
}
432434

433435
if (Error E = removeSections(Config, Obj))
434-
return E;
436+
return createFileError(Config.InputFilename, std::move(E));
435437

436438
// Mark symbols to determine which symbols are still needed.
437439
if (Config.StripAll)
@@ -446,20 +448,20 @@ static Error handleArgs(const CommonConfig &Config,
446448

447449
for (const NewSectionInfo &NewSection : Config.AddSection) {
448450
if (Error E = isValidMachOCannonicalName(NewSection.SectionName))
449-
return E;
451+
return createFileError(Config.InputFilename, std::move(E));
450452
if (Error E = addSection(NewSection, Obj))
451-
return E;
453+
return createFileError(Config.InputFilename, std::move(E));
452454
}
453455

454456
for (const NewSectionInfo &NewSection : Config.UpdateSection) {
455457
if (Error E = isValidMachOCannonicalName(NewSection.SectionName))
456-
return E;
458+
return createFileError(Config.InputFilename, std::move(E));
457459
if (Error E = updateSection(NewSection, Obj))
458-
return E;
460+
return createFileError(Config.InputFilename, std::move(E));
459461
}
460462

461463
if (Error E = processLoadCommands(MachOConfig, Obj))
462-
return E;
464+
return createFileError(Config.InputFilename, std::move(E));
463465

464466
return Error::success();
465467
}
@@ -479,7 +481,7 @@ Error objcopy::macho::executeObjcopyOnBinary(const CommonConfig &Config,
479481
Config.InputFilename.str().c_str());
480482

481483
if (Error E = handleArgs(Config, MachOConfig, **O))
482-
return createFileError(Config.InputFilename, std::move(E));
484+
return E;
483485

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

llvm/lib/ObjCopy/wasm/WasmObjcopy.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,24 @@ 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+
Error E = createStringError(errc::invalid_argument, "section '%s' not found",
57+
SecName.str().c_str());
58+
return createFileError(Filename, std::move(E));
5859
}
5960

6061
static void removeSections(const CommonConfig &Config, Object &Obj) {
@@ -115,8 +116,9 @@ static Error handleArgs(const CommonConfig &Config, Object &Obj) {
115116
StringRef SecName;
116117
StringRef FileName;
117118
std::tie(SecName, FileName) = Flag.split("=");
118-
if (Error E = dumpSectionToFile(SecName, FileName, Obj))
119-
return createFileError(FileName, std::move(E));
119+
if (Error E =
120+
dumpSectionToFile(SecName, FileName, Config.InputFilename, Obj))
121+
return E;
120122
}
121123

122124
removeSections(Config, Obj);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
## Show that llvm-objcopy report that section file path is not exists.
2+
3+
# RUN: yaml2obj %s -o %t
4+
5+
# RUN: not llvm-objcopy --dump-section .text=not_exists/text-section %t 2>&1 \
6+
# RUN: | FileCheck %s -DINPUT=%t --check-prefix=NO-SUCH-PATH
7+
# NO-SUCH-PATH: error: 'not_exists/text-section': No such file or directory
8+
9+
!ELF
10+
FileHeader:
11+
Class: ELFCLASS64
12+
Data: ELFDATA2LSB
13+
Type: ET_EXEC
14+
Machine: EM_X86_64
15+
Sections:
16+
- Name: .text
17+
Type: SHT_PROGBITS
18+
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
19+
AddressAlign: 0x0000000000001000
20+
Content: "DEADBEEF"
21+
- Name: .foo
22+
Type: SHT_PROGBITS
23+
Flags: [ SHF_WRITE ]
24+
Content: "CAFE"
25+
- Name: .empty
26+
Type: SHT_PROGBITS
27+
Flags: [ SHF_ALLOC ]
28+
- Name: .bar
29+
Type: SHT_NOBITS
30+
Flags: [ SHF_WRITE ]
31+
ProgramHeaders:
32+
- Type: PT_LOAD
33+
Flags: [ PF_X, PF_R ]
34+
FirstSec: .text
35+
LastSec: .text
36+

0 commit comments

Comments
 (0)