diff --git a/llvm/include/llvm/Object/Error.h b/llvm/include/llvm/Object/Error.h index d29c2dc25590..c0902dd25657 100644 --- a/llvm/include/llvm/Object/Error.h +++ b/llvm/include/llvm/Object/Error.h @@ -34,7 +34,6 @@ enum class object_error { string_table_non_null_end, invalid_section_index, bitcode_section_not_found, - macho_small_load_command, macho_load_segment_too_many_sections, macho_load_segment_too_small, }; diff --git a/llvm/lib/Object/Error.cpp b/llvm/lib/Object/Error.cpp index fae4394767dc..297418c95a97 100644 --- a/llvm/lib/Object/Error.cpp +++ b/llvm/lib/Object/Error.cpp @@ -47,8 +47,6 @@ std::string _object_error_category::message(int EV) const { return "Invalid section index"; case object_error::bitcode_section_not_found: return "Bitcode section not found in object file"; - case object_error::macho_small_load_command: - return "Mach-O load command with size < 8 bytes"; case object_error::macho_load_segment_too_many_sections: return "Mach-O segment load command contains too many sections"; case object_error::macho_load_segment_too_small: diff --git a/llvm/lib/Object/MachOObjectFile.cpp b/llvm/lib/Object/MachOObjectFile.cpp index 744603ae730c..ad29d0042e3c 100644 --- a/llvm/lib/Object/MachOObjectFile.cpp +++ b/llvm/lib/Object/MachOObjectFile.cpp @@ -177,11 +177,13 @@ static uint32_t getSectionFlags(const MachOObjectFile *O, } static Expected -getLoadCommandInfo(const MachOObjectFile *Obj, const char *Ptr) { +getLoadCommandInfo(const MachOObjectFile *Obj, const char *Ptr, + uint32_t LoadCommandIndex) { if (auto CmdOrErr = getStructOrErr(Obj, Ptr)) { if (CmdOrErr->cmdsize < 8) - return malformedError(*Obj, "Mach-O load command with size < 8 bytes", - object_error::macho_small_load_command); + return malformedError(*Obj, Twine("truncated or malformed object (load " + "command ") + Twine(LoadCommandIndex) + + Twine(" with size less than 8 bytes)")); return MachOObjectFile::LoadCommandInfo({Ptr, *CmdOrErr}); } else return CmdOrErr.takeError(); @@ -195,7 +197,7 @@ getFirstLoadCommandInfo(const MachOObjectFile *Obj) { return malformedError(*Obj, "truncated or malformed object (load command " "0 extends past the end all load commands in the " "file)"); - return getLoadCommandInfo(Obj, getPtr(Obj, HeaderSize)); + return getLoadCommandInfo(Obj, getPtr(Obj, HeaderSize), 0); } static Expected @@ -209,7 +211,7 @@ getNextLoadCommandInfo(const MachOObjectFile *Obj, uint32_t LoadCommandIndex, "(load command ") + Twine(LoadCommandIndex + 1) + Twine(" extends past the end all load commands in the " "file)")); - return getLoadCommandInfo(Obj, L.Ptr + L.C.cmdsize); + return getLoadCommandInfo(Obj, L.Ptr + L.C.cmdsize, LoadCommandIndex + 1); } template diff --git a/llvm/test/Object/Inputs/macho64-invalid-too-small-load-command.1 b/llvm/test/Object/Inputs/macho64-invalid-too-small-load-command.1 new file mode 100644 index 000000000000..cb8886ed3f7e Binary files /dev/null and b/llvm/test/Object/Inputs/macho64-invalid-too-small-load-command.1 differ diff --git a/llvm/test/Object/macho-invalid.test b/llvm/test/Object/macho-invalid.test index 92b18079b5de..4ddea18fedfb 100644 --- a/llvm/test/Object/macho-invalid.test +++ b/llvm/test/Object/macho-invalid.test @@ -19,6 +19,10 @@ RUN: not llvm-objdump -macho -private-headers %p/Inputs/macho64-invalid-too-smal RUN: | FileCheck -check-prefix SMALL-LOADC-SIZE %s SMALL-LOADC-SIZE: truncated or malformed object (load commands extend past the end of the file) +RUN: not llvm-objdump -macho -private-headers %p/Inputs/macho64-invalid-too-small-load-command.1 2>&1 \ +RUN: | FileCheck -check-prefix SMALL-LOADC-SIZE-1 %s +SMALL-LOADC-SIZE-1: truncated or malformed object (load command 1 with size less than 8 bytes) + RUN: not llvm-objdump -private-headers %p/Inputs/macho-invalid-too-small-segment-load-command 2>&1 \ RUN: | FileCheck -check-prefix SMALL-SEGLOADC-SIZE %s RUN: not llvm-objdump -private-headers %p/Inputs/macho64-invalid-too-small-segment-load-command 2>&1 \