Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit d3b89a1

Browse files
committed
[llvm-objcopy] Consistently use createStringError instead of make_error<StringError>
This was requested in the review of D57006. Also add missing quotes around symbol names in error messages. Differential Revision: https://reviews.llvm.org/D57014 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@351799 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 1da2ee9 commit d3b89a1

File tree

6 files changed

+40
-42
lines changed

6 files changed

+40
-42
lines changed

test/tools/llvm-objcopy/COFF/remove-section.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
# Removing the .comdat section fails, since the .text section has relocations
9797
# against it.
9898
#
99-
# ERROR-RELOC: Relocation target foo ({{.*}}) not found
99+
# ERROR-RELOC: Relocation target 'foo' ({{.*}}) not found
100100
#
101101
#
102102
# Removing the .comdat section and .text (with a relocation against .comdat)

tools/llvm-objcopy/COFF/COFFObjcopy.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj) {
8484
// Explicitly removing a referenced symbol is an error.
8585
if (Sym.Referenced)
8686
reportError(Config.OutputFilename,
87-
make_error<StringError>(
88-
"not stripping symbol '" + Sym.Name +
89-
"' because it is named in a relocation.",
90-
llvm::errc::invalid_argument));
87+
createStringError(llvm::errc::invalid_argument,
88+
"not stripping symbol '%s' because it is "
89+
"named in a relocation.",
90+
Sym.Name.str().c_str()));
9191
return true;
9292
}
9393

tools/llvm-objcopy/COFF/Object.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,8 @@ Error Object::markSymbols() {
5656
for (const Relocation &R : Sec.Relocs) {
5757
auto It = SymbolMap.find(R.Target);
5858
if (It == SymbolMap.end())
59-
return make_error<StringError>("Relocation target " + Twine(R.Target) +
60-
" not found",
61-
object_error::invalid_symbol_index);
59+
return createStringError(object_error::invalid_symbol_index,
60+
"Relocation target %zu not found", R.Target);
6261
It->second->Referenced = true;
6362
}
6463
}

tools/llvm-objcopy/COFF/Reader.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ Error COFFReader::readSections(Object &Obj) const {
7777
if (auto EC = COFFObj.getSectionName(Sec, S.Name))
7878
return errorCodeToError(EC);
7979
if (Sec->hasExtendedRelocations())
80-
return make_error<StringError>("Extended relocations not supported yet",
81-
object_error::parse_failed);
80+
return createStringError(object_error::parse_failed,
81+
"Extended relocations not supported yet");
8282
}
8383
Obj.addSections(Sections);
8484
return Error::success();
@@ -116,16 +116,16 @@ Error COFFReader::readSymbols(Object &Obj, bool IsBigObj) const {
116116
Sections.size())
117117
Sym.TargetSectionId = Sections[SymRef.getSectionNumber() - 1].UniqueId;
118118
else
119-
return make_error<StringError>("Section number out of range",
120-
object_error::parse_failed);
119+
return createStringError(object_error::parse_failed,
120+
"Section number out of range");
121121
// For section definitions, check if it is comdat associative, and if
122122
// it is, find the target section unique id.
123123
const coff_aux_section_definition *SD = SymRef.getSectionDefinition();
124124
if (SD && SD->Selection == IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
125125
int32_t Index = SD->getNumber(IsBigObj);
126126
if (Index <= 0 || static_cast<uint32_t>(Index - 1) >= Sections.size())
127-
return make_error<StringError>("Unexpected associative section index",
128-
object_error::parse_failed);
127+
return createStringError(object_error::parse_failed,
128+
"Unexpected associative section index");
129129
Sym.AssociativeComdatTargetSectionId = Sections[Index - 1].UniqueId;
130130
}
131131
I += 1 + SymRef.getNumberOfAuxSymbols();
@@ -144,12 +144,12 @@ Error COFFReader::setRelocTargets(Object &Obj) const {
144144
for (Section &Sec : Obj.getMutableSections()) {
145145
for (Relocation &R : Sec.Relocs) {
146146
if (R.Reloc.SymbolTableIndex >= RawSymbolTable.size())
147-
return make_error<StringError>("SymbolTableIndex out of range",
148-
object_error::parse_failed);
147+
return createStringError(object_error::parse_failed,
148+
"SymbolTableIndex out of range");
149149
const Symbol *Sym = RawSymbolTable[R.Reloc.SymbolTableIndex];
150150
if (Sym == nullptr)
151-
return make_error<StringError>("Invalid SymbolTableIndex",
152-
object_error::parse_failed);
151+
return createStringError(object_error::parse_failed,
152+
"Invalid SymbolTableIndex");
153153
R.Target = Sym->UniqueId;
154154
R.TargetName = Sym->Name;
155155
}
@@ -169,8 +169,8 @@ Expected<std::unique_ptr<Object>> COFFReader::create() const {
169169
Obj->CoffFileHeader = *CFH;
170170
} else {
171171
if (!CBFH)
172-
return make_error<StringError>("No COFF file header returned",
173-
object_error::parse_failed);
172+
return createStringError(object_error::parse_failed,
173+
"No COFF file header returned");
174174
// Only copying the few fields from the bigobj header that we need
175175
// and won't recreate in the end.
176176
Obj->CoffFileHeader.Machine = CBFH->Machine;

tools/llvm-objcopy/COFF/Writer.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ Error COFFWriter::finalizeRelocTargets() {
2929
for (Relocation &R : Sec.Relocs) {
3030
const Symbol *Sym = Obj.findSymbol(R.Target);
3131
if (Sym == nullptr)
32-
return make_error<StringError>("Relocation target " + R.TargetName +
33-
" (" + Twine(R.Target) +
34-
") not found",
35-
object_error::invalid_symbol_index);
32+
return createStringError(object_error::invalid_symbol_index,
33+
"Relocation target '%s' (%zu) not found",
34+
R.TargetName.str().c_str(), R.Target);
3635
R.Reloc.SymbolTableIndex = Sym->RawIndex;
3736
}
3837
}
@@ -48,9 +47,9 @@ Error COFFWriter::finalizeSectionNumbers() {
4847
} else {
4948
const Section *Sec = Obj.findSection(Sym.TargetSectionId);
5049
if (Sec == nullptr)
51-
return make_error<StringError>("Symbol " + Sym.Name +
52-
" points to a removed section",
53-
object_error::invalid_symbol_index);
50+
return createStringError(object_error::invalid_symbol_index,
51+
"Symbol '%s' points to a removed section",
52+
Sym.Name.str().c_str());
5453
Sym.Sym.SectionNumber = Sec->Index;
5554

5655
if (Sym.Sym.NumberOfAuxSymbols == 1 &&
@@ -65,9 +64,10 @@ Error COFFWriter::finalizeSectionNumbers() {
6564
} else {
6665
Sec = Obj.findSection(Sym.AssociativeComdatTargetSectionId);
6766
if (Sec == nullptr)
68-
return make_error<StringError>(
69-
"Symbol " + Sym.Name + " is associative to a removed section",
70-
object_error::invalid_symbol_index);
67+
return createStringError(
68+
object_error::invalid_symbol_index,
69+
"Symbol '%s' is associative to a removed section",
70+
Sym.Name.str().c_str());
7171
SDSectionNumber = Sec->Index;
7272
}
7373
// Update the section definition with the new section number.
@@ -343,9 +343,8 @@ Error COFFWriter::patchDebugDirectory() {
343343
S.Header.VirtualAddress + S.Header.SizeOfRawData) {
344344
if (Dir->RelativeVirtualAddress + Dir->Size >
345345
S.Header.VirtualAddress + S.Header.SizeOfRawData)
346-
return make_error<StringError>(
347-
"Debug directory extends past end of section",
348-
object_error::parse_failed);
346+
return createStringError(object_error::parse_failed,
347+
"Debug directory extends past end of section");
349348

350349
size_t Offset = Dir->RelativeVirtualAddress - S.Header.VirtualAddress;
351350
uint8_t *Ptr = Buf.getBufferStart() + S.Header.PointerToRawData + Offset;
@@ -361,15 +360,15 @@ Error COFFWriter::patchDebugDirectory() {
361360
return Error::success();
362361
}
363362
}
364-
return make_error<StringError>("Debug directory not found",
365-
object_error::parse_failed);
363+
return createStringError(object_error::parse_failed,
364+
"Debug directory not found");
366365
}
367366

368367
Error COFFWriter::write() {
369368
bool IsBigObj = Obj.getSections().size() > MaxNumberOfSections16;
370369
if (IsBigObj && Obj.IsPE)
371-
return make_error<StringError>("Too many sections for executable",
372-
object_error::parse_failed);
370+
return createStringError(object_error::parse_failed,
371+
"Too many sections for executable");
373372
return write(IsBigObj);
374373
}
375374

tools/llvm-objcopy/ELF/ELFObjcopy.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,10 @@ static Error dumpSectionToFile(StringRef SecName, StringRef Filename,
185185
for (auto &Sec : Obj.sections()) {
186186
if (Sec.Name == SecName) {
187187
if (Sec.OriginalData.empty())
188-
return make_error<StringError>("Can't dump section \"" + SecName +
189-
"\": it has no contents",
190-
object_error::parse_failed);
188+
return createStringError(
189+
object_error::parse_failed,
190+
"Can't dump section \"%s\": it has no contents",
191+
SecName.str().c_str());
191192
Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
192193
FileOutputBuffer::create(Filename, Sec.OriginalData.size());
193194
if (!BufferOrErr)
@@ -200,8 +201,7 @@ static Error dumpSectionToFile(StringRef SecName, StringRef Filename,
200201
return Error::success();
201202
}
202203
}
203-
return make_error<StringError>("Section not found",
204-
object_error::parse_failed);
204+
return createStringError(object_error::parse_failed, "Section not found");
205205
}
206206

207207
static bool isCompressed(const SectionBase &Section) {

0 commit comments

Comments
 (0)