diff --git a/llvm/lib/Object/COFFObjectFile.cpp b/llvm/lib/Object/COFFObjectFile.cpp --- a/llvm/lib/Object/COFFObjectFile.cpp +++ b/llvm/lib/Object/COFFObjectFile.cpp @@ -1168,7 +1168,7 @@ return createStringError(object_error::parse_failed, "invalid section name"); } else { - if (Name.substr(1).getAsInteger(10, Offset)) + if (Name.substr(1).consumeInteger(10, Offset)) return createStringError(object_error::parse_failed, "invalid section name"); } diff --git a/llvm/test/tools/llvm-objdump/COFF/Inputs/long-section-name.yaml b/llvm/test/tools/llvm-objdump/COFF/Inputs/long-section-name.yaml new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-objdump/COFF/Inputs/long-section-name.yaml @@ -0,0 +1,15 @@ +--- !COFF +header: + Machine: IMAGE_FILE_MACHINE_ARM64 + Characteristics: [ ] +sections: + - Name: LongSectionName + Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ] +symbols: + - Name: LongSectionName + Value: 0 + SectionNumber: 1 + SimpleType: IMAGE_SYM_TYPE_NULL + ComplexType: IMAGE_SYM_DTYPE_NULL + StorageClass: IMAGE_SYM_CLASS_STATIC +... diff --git a/llvm/test/tools/llvm-objdump/COFF/long-section-name.test b/llvm/test/tools/llvm-objdump/COFF/long-section-name.test new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-objdump/COFF/long-section-name.test @@ -0,0 +1,30 @@ +# RUN: yaml2obj %S/Inputs/long-section-name.yaml -o %t.obj + +## Replace the section name field of the object file with /4\0abcde emulating +## a section name field not fully null-padded at the end. +# RUN: %python %s %t.obj + +## This should print the LongSectionName section. +# RUN: llvm-objdump --headers %t.obj | FileCheck %s + +# CHECK: LongSectionName + +import sys + +if len(sys.argv) < 2: + print("Use: python3 long-section-name.test ") + exit(1) + +pattern = b'/4' +replacement = b'/4\0abcde' + +data = None +with open(sys.argv[1], "rb") as inp: + data = inp.read() +with open(sys.argv[1], "wb") as outp: + pos = data.find(pattern) + if pos == -1: + sys.exit("Error: Pattern /4 not found in " + sys.argv[1]) + outp.write(data[:pos]) + outp.write(replacement) + outp.write(data[pos + len(replacement):])