Index: llvm/test/tools/llvm-readobj/elf-linker-options.test =================================================================== --- llvm/test/tools/llvm-readobj/elf-linker-options.test +++ llvm/test/tools/llvm-readobj/elf-linker-options.test @@ -1,7 +1,7 @@ ## Check that we can use the --elf-linker-options option ## to dump SHT_LLVM_LINKER_OPTIONS sections. -# RUN: yaml2obj %s -o %t1 +# RUN: yaml2obj --docnum=1 %s -o %t1 # RUN: llvm-readobj --elf-linker-options %t1 | FileCheck %s # CHECK: LinkerOptions [ @@ -28,3 +28,51 @@ # RUN: llvm-readelf --elf-linker-options %t1 2>&1 | FileCheck %s --check-prefix=READELF # READELF: printELFLinkerOptions not implemented! + +## Check we do not attemp to dump a data from outside of the SHT_LLVM_LINKER_OPTIONS section +## when it contains an incomplete key-value pair. + +# RUN: yaml2obj --docnum=2 %s -o %t2 +# RUN: llvm-readobj --elf-linker-options %t2 2>&1 | FileCheck %s --check-prefix=INCOMPLETE -DFILE=%t2 + +# INCOMPLETE: LinkerOptions [ +# INCOMPLETE: warning: '[[FILE]]': SHT_LLVM_LINKER_OPTIONS is broken: an incomplete key-value pair was found, the last possible key was: "foo" +# INCOMPLETE: ] + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .linker-options + Type: SHT_LLVM_LINKER_OPTIONS + Content: "666f6f00" ## 'f', 'o', 'o', '\0' + - Type: Fill + Pattern: "FF" + Size: "1" + +## Check we do not attemp to dump a data from outside of the SHT_LLVM_LINKER_OPTIONS section +## when it is not null-terminated. + +# RUN: yaml2obj --docnum=3 %s -o %t3 +# RUN: llvm-readobj --elf-linker-options %t3 2>&1 | FileCheck %s --check-prefix=NOT-NULL-TERMINATED -DFILE=%t3 + +# NOT-NULL-TERMINATED: LinkerOptions [ +# NOT-NULL-TERMINATED: warning: '[[FILE]]': SHT_LLVM_LINKER_OPTIONS is broken: the content is not null-terminated +# NOT-NULL-TERMINATED: ] + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .linker-options + Type: SHT_LLVM_LINKER_OPTIONS + Content: "61" + - Type: Fill + Pattern: "6200" + Size: "2" Index: llvm/tools/llvm-readobj/ELFDumper.cpp =================================================================== --- llvm/tools/llvm-readobj/ELFDumper.cpp +++ llvm/tools/llvm-readobj/ELFDumper.cpp @@ -6017,17 +6017,32 @@ if (Shdr.sh_type != ELF::SHT_LLVM_LINKER_OPTIONS) continue; - ArrayRef Contents = + ArrayRef Content = unwrapOrError(this->FileName, Obj->getSectionContents(&Shdr)); - for (const uint8_t *P = Contents.begin(), *E = Contents.end(); P < E; ) { - StringRef Key = StringRef(reinterpret_cast(P)); - StringRef Value = - StringRef(reinterpret_cast(P) + Key.size() + 1); + if (Content.empty()) + return; - W.printString(Key, Value); + if (Content.back() != 0) { + reportWarning(createError("SHT_LLVM_LINKER_OPTIONS is broken: the " + "content is not null-terminated"), + FileName); + return; + } - P = P + Key.size() + Value.size() + 2; + SmallVector Strings; + toStringRef(Content.drop_back()).split(Strings, '\0'); + if (Strings.size() % 2 != 0) { + reportWarning( + createError( + "SHT_LLVM_LINKER_OPTIONS is broken: an incomplete " + "key-value pair was found, the last possible key was: \"" + + Strings.back() + "\""), + FileName); + return; } + + for (size_t I = 0; I < Strings.size(); I += 2) + W.printString(Strings[I], Strings[I + 1]); } }