llvm-objcopy-6.patch 12 KB

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