1 From d3b89a1637cddee1c61e59257cfe92227ead29e5 Mon Sep 17 00:00:00 2001
2 From: Martin Storsjo <martin@martin.st>
3 Date: Tue, 22 Jan 2019 10:57:59 +0000
4 Subject: [PATCH] [llvm-objcopy] Consistently use createStringError instead of
5 make_error<StringError>
7 This was requested in the review of D57006.
9 Also add missing quotes around symbol names in error messages.
11 Differential Revision: https://reviews.llvm.org/D57014
13 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@351799 91177308-0d34-0410-b5e6-96231b3b80d8
15 .../llvm-objcopy/COFF/remove-section.test | 2 +-
16 tools/llvm-objcopy/COFF/COFFObjcopy.cpp | 8 ++---
17 tools/llvm-objcopy/COFF/Object.cpp | 5 ++-
18 tools/llvm-objcopy/COFF/Reader.cpp | 24 +++++++-------
19 tools/llvm-objcopy/COFF/Writer.cpp | 33 +++++++++----------
20 tools/llvm-objcopy/ELF/ELFObjcopy.cpp | 10 +++---
21 6 files changed, 40 insertions(+), 42 deletions(-)
23 diff --git a/llvm/test/tools/llvm-objcopy/COFF/remove-section.test b/llvm/test/tools/llvm-objcopy/COFF/remove-section.test
24 index b3dfb0b98cb..6dc8f6a6c2e 100644
25 --- a/test/tools/llvm-objcopy/COFF/remove-section.test
26 +++ b/llvm/test/tools/llvm-objcopy/COFF/remove-section.test
28 # Removing the .comdat section fails, since the .text section has relocations
31 -# ERROR-RELOC: Relocation target foo ({{.*}}) not found
32 +# ERROR-RELOC: Relocation target 'foo' ({{.*}}) not found
35 # Removing the .comdat section and .text (with a relocation against .comdat)
36 diff --git a/llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp b/llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp
37 index 99929d10a1f..8d8f53d13d8 100644
38 --- a/tools/llvm-objcopy/COFF/COFFObjcopy.cpp
39 +++ b/llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp
40 @@ -84,10 +84,10 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj) {
41 // Explicitly removing a referenced symbol is an error.
43 reportError(Config.OutputFilename,
44 - make_error<StringError>(
45 - "not stripping symbol '" + Sym.Name +
46 - "' because it is named in a relocation.",
47 - llvm::errc::invalid_argument));
48 + createStringError(llvm::errc::invalid_argument,
49 + "not stripping symbol '%s' because it is "
50 + "named in a relocation.",
51 + Sym.Name.str().c_str()));
55 diff --git a/llvm/tools/llvm-objcopy/COFF/Object.cpp b/llvm/tools/llvm-objcopy/COFF/Object.cpp
56 index fc87d9e574d..83435dffa98 100644
57 --- a/tools/llvm-objcopy/COFF/Object.cpp
58 +++ b/llvm/tools/llvm-objcopy/COFF/Object.cpp
59 @@ -56,9 +56,8 @@ Error Object::markSymbols() {
60 for (const Relocation &R : Sec.Relocs) {
61 auto It = SymbolMap.find(R.Target);
62 if (It == SymbolMap.end())
63 - return make_error<StringError>("Relocation target " + Twine(R.Target) +
65 - object_error::invalid_symbol_index);
66 + return createStringError(object_error::invalid_symbol_index,
67 + "Relocation target %zu not found", R.Target);
68 It->second->Referenced = true;
71 diff --git a/llvm/tools/llvm-objcopy/COFF/Reader.cpp b/llvm/tools/llvm-objcopy/COFF/Reader.cpp
72 index c8abe2913a2..20ff32a59dc 100644
73 --- a/tools/llvm-objcopy/COFF/Reader.cpp
74 +++ b/llvm/tools/llvm-objcopy/COFF/Reader.cpp
75 @@ -77,8 +77,8 @@ Error COFFReader::readSections(Object &Obj) const {
76 if (auto EC = COFFObj.getSectionName(Sec, S.Name))
77 return errorCodeToError(EC);
78 if (Sec->hasExtendedRelocations())
79 - return make_error<StringError>("Extended relocations not supported yet",
80 - object_error::parse_failed);
81 + return createStringError(object_error::parse_failed,
82 + "Extended relocations not supported yet");
84 Obj.addSections(Sections);
85 return Error::success();
86 @@ -116,16 +116,16 @@ Error COFFReader::readSymbols(Object &Obj, bool IsBigObj) const {
88 Sym.TargetSectionId = Sections[SymRef.getSectionNumber() - 1].UniqueId;
90 - return make_error<StringError>("Section number out of range",
91 - object_error::parse_failed);
92 + return createStringError(object_error::parse_failed,
93 + "Section number out of range");
94 // For section definitions, check if it is comdat associative, and if
95 // it is, find the target section unique id.
96 const coff_aux_section_definition *SD = SymRef.getSectionDefinition();
97 if (SD && SD->Selection == IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
98 int32_t Index = SD->getNumber(IsBigObj);
99 if (Index <= 0 || static_cast<uint32_t>(Index - 1) >= Sections.size())
100 - return make_error<StringError>("Unexpected associative section index",
101 - object_error::parse_failed);
102 + return createStringError(object_error::parse_failed,
103 + "Unexpected associative section index");
104 Sym.AssociativeComdatTargetSectionId = Sections[Index - 1].UniqueId;
106 I += 1 + SymRef.getNumberOfAuxSymbols();
107 @@ -144,12 +144,12 @@ Error COFFReader::setRelocTargets(Object &Obj) const {
108 for (Section &Sec : Obj.getMutableSections()) {
109 for (Relocation &R : Sec.Relocs) {
110 if (R.Reloc.SymbolTableIndex >= RawSymbolTable.size())
111 - return make_error<StringError>("SymbolTableIndex out of range",
112 - object_error::parse_failed);
113 + return createStringError(object_error::parse_failed,
114 + "SymbolTableIndex out of range");
115 const Symbol *Sym = RawSymbolTable[R.Reloc.SymbolTableIndex];
117 - return make_error<StringError>("Invalid SymbolTableIndex",
118 - object_error::parse_failed);
119 + return createStringError(object_error::parse_failed,
120 + "Invalid SymbolTableIndex");
121 R.Target = Sym->UniqueId;
122 R.TargetName = Sym->Name;
124 @@ -169,8 +169,8 @@ Expected<std::unique_ptr<Object>> COFFReader::create() const {
125 Obj->CoffFileHeader = *CFH;
128 - return make_error<StringError>("No COFF file header returned",
129 - object_error::parse_failed);
130 + return createStringError(object_error::parse_failed,
131 + "No COFF file header returned");
132 // Only copying the few fields from the bigobj header that we need
133 // and won't recreate in the end.
134 Obj->CoffFileHeader.Machine = CBFH->Machine;
135 diff --git a/llvm/tools/llvm-objcopy/COFF/Writer.cpp b/llvm/tools/llvm-objcopy/COFF/Writer.cpp
136 index 9fb7812672b..0321f94a896 100644
137 --- a/tools/llvm-objcopy/COFF/Writer.cpp
138 +++ b/llvm/tools/llvm-objcopy/COFF/Writer.cpp
139 @@ -29,10 +29,9 @@ Error COFFWriter::finalizeRelocTargets() {
140 for (Relocation &R : Sec.Relocs) {
141 const Symbol *Sym = Obj.findSymbol(R.Target);
143 - return make_error<StringError>("Relocation target " + R.TargetName +
144 - " (" + Twine(R.Target) +
146 - object_error::invalid_symbol_index);
147 + return createStringError(object_error::invalid_symbol_index,
148 + "Relocation target '%s' (%zu) not found",
149 + R.TargetName.str().c_str(), R.Target);
150 R.Reloc.SymbolTableIndex = Sym->RawIndex;
153 @@ -48,9 +47,9 @@ Error COFFWriter::finalizeSectionNumbers() {
155 const Section *Sec = Obj.findSection(Sym.TargetSectionId);
157 - return make_error<StringError>("Symbol " + Sym.Name +
158 - " points to a removed section",
159 - object_error::invalid_symbol_index);
160 + return createStringError(object_error::invalid_symbol_index,
161 + "Symbol '%s' points to a removed section",
162 + Sym.Name.str().c_str());
163 Sym.Sym.SectionNumber = Sec->Index;
165 if (Sym.Sym.NumberOfAuxSymbols == 1 &&
166 @@ -65,9 +64,10 @@ Error COFFWriter::finalizeSectionNumbers() {
168 Sec = Obj.findSection(Sym.AssociativeComdatTargetSectionId);
170 - return make_error<StringError>(
171 - "Symbol " + Sym.Name + " is associative to a removed section",
172 - object_error::invalid_symbol_index);
173 + return createStringError(
174 + object_error::invalid_symbol_index,
175 + "Symbol '%s' is associative to a removed section",
176 + Sym.Name.str().c_str());
177 SDSectionNumber = Sec->Index;
179 // Update the section definition with the new section number.
180 @@ -343,9 +343,8 @@ Error COFFWriter::patchDebugDirectory() {
181 S.Header.VirtualAddress + S.Header.SizeOfRawData) {
182 if (Dir->RelativeVirtualAddress + Dir->Size >
183 S.Header.VirtualAddress + S.Header.SizeOfRawData)
184 - return make_error<StringError>(
185 - "Debug directory extends past end of section",
186 - object_error::parse_failed);
187 + return createStringError(object_error::parse_failed,
188 + "Debug directory extends past end of section");
190 size_t Offset = Dir->RelativeVirtualAddress - S.Header.VirtualAddress;
191 uint8_t *Ptr = Buf.getBufferStart() + S.Header.PointerToRawData + Offset;
192 @@ -361,15 +360,15 @@ Error COFFWriter::patchDebugDirectory() {
193 return Error::success();
196 - return make_error<StringError>("Debug directory not found",
197 - object_error::parse_failed);
198 + return createStringError(object_error::parse_failed,
199 + "Debug directory not found");
202 Error COFFWriter::write() {
203 bool IsBigObj = Obj.getSections().size() > MaxNumberOfSections16;
204 if (IsBigObj && Obj.IsPE)
205 - return make_error<StringError>("Too many sections for executable",
206 - object_error::parse_failed);
207 + return createStringError(object_error::parse_failed,
208 + "Too many sections for executable");
209 return write(IsBigObj);
212 diff --git a/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp b/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp
213 index db0cd76ced4..a2996395c1f 100644
214 --- a/tools/llvm-objcopy/ELF/ELFObjcopy.cpp
215 +++ b/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp
216 @@ -185,9 +185,10 @@ static Error dumpSectionToFile(StringRef SecName, StringRef Filename,
217 for (auto &Sec : Obj.sections()) {
218 if (Sec.Name == SecName) {
219 if (Sec.OriginalData.empty())
220 - return make_error<StringError>("Can't dump section \"" + SecName +
221 - "\": it has no contents",
222 - object_error::parse_failed);
223 + return createStringError(
224 + object_error::parse_failed,
225 + "Can't dump section \"%s\": it has no contents",
226 + SecName.str().c_str());
227 Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
228 FileOutputBuffer::create(Filename, Sec.OriginalData.size());
230 @@ -200,8 +201,7 @@ static Error dumpSectionToFile(StringRef SecName, StringRef Filename,
231 return Error::success();
234 - return make_error<StringError>("Section not found",
235 - object_error::parse_failed);
236 + return createStringError(object_error::parse_failed, "Section not found");
239 static bool isCompressed(const SectionBase &Section) {