From b25ab4f313f38d3dee9674789cf56b9143515034 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Alfonso=20S=C3=A1nchez-Beato?=
 <alfonso.sanchez-beato@canonical.com>
Date: Fri, 10 Sep 2021 09:55:26 +0100
Subject: [PATCH] [llvm-objcopy][COFF] Fix test for debug dir presence

If the number of directories was 6 (equal to the DEBUG_DIRECTORY
index), patchDebugDirectory() was run even though the debug directory
is actually the 7th entry. Use <= in the comparison to fix that.

This fixes https://llvm.org/PR51243

Differential Revision: https://reviews.llvm.org/D106940

Reviewed by: jhenderson
---
 .../COFF/check-debug-dir-present.test         | 81 +++++++++++++++++++
 llvm/tools/llvm-objcopy/COFF/Writer.cpp       |  2 +-
 2 files changed, 82 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/tools/llvm-objcopy/COFF/check-debug-dir-present.test

diff --git a/llvm/test/tools/llvm-objcopy/COFF/check-debug-dir-present.test b/llvm/test/tools/llvm-objcopy/COFF/check-debug-dir-present.test
new file mode 100644
index 000000000000..7fd10486b585
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/COFF/check-debug-dir-present.test
@@ -0,0 +1,81 @@
+## We check that we copy sections to an image in the case when there are
+## exactly 6 directory entries.
+# RUN: yaml2obj --docnum=1 %s -o %t
+# RUN: llvm-objcopy --add-section new_sect=%t %t %t1
+# RUN: obj2yaml %t1 | FileCheck %s
+
+# CHECK:         NumberOfRvaAndSize: 6
+# CHECK-NEXT:    ExportTable:
+# CHECK-NEXT:      RelativeVirtualAddress: 0
+# CHECK-NEXT:      Size:            0
+# CHECK-NEXT:    ImportTable:
+# CHECK-NEXT:      RelativeVirtualAddress: 0
+# CHECK-NEXT:      Size:            0
+# CHECK-NEXT:    ResourceTable:
+# CHECK-NEXT:      RelativeVirtualAddress: 0
+# CHECK-NEXT:      Size:            0
+# CHECK-NEXT:    ExceptionTable:
+# CHECK-NEXT:      RelativeVirtualAddress: 0
+# CHECK-NEXT:      Size:            0
+# CHECK-NEXT:    CertificateTable:
+# CHECK-NEXT:      RelativeVirtualAddress: 0
+# CHECK-NEXT:      Size:            0
+# CHECK-NEXT:    BaseRelocationTable:
+# CHECK-NEXT:      RelativeVirtualAddress: 0
+# CHECK-NEXT:      Size:            0
+# CHECK:       sections:
+# CHECK-NEXT:    - Name:            foo
+# CHECK-NEXT:      Characteristics: [  ]
+# CHECK-NEXT:      Alignment:       4
+# CHECK-NEXT:      SectionData:     ''
+# CHECK-NEXT:    - Name:            new_sect
+# CHECK-NEXT:      Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA ]
+# CHECK-NEXT:      Alignment:       1
+# CHECK-NEXT:      SectionData:     ''
+
+--- !COFF
+OptionalHeader:
+  AddressOfEntryPoint: 4096
+  ImageBase:       0
+  SectionAlignment: 4096
+  FileAlignment:   512
+  MajorOperatingSystemVersion: 0
+  MinorOperatingSystemVersion: 0
+  MajorImageVersion: 0
+  MinorImageVersion: 0
+  MajorSubsystemVersion: 0
+  MinorSubsystemVersion: 0
+  Subsystem:       IMAGE_SUBSYSTEM_EFI_APPLICATION
+  DLLCharacteristics: [  ]
+  SizeOfStackReserve: 0
+  SizeOfStackCommit: 0
+  SizeOfHeapReserve: 0
+  SizeOfHeapCommit: 0
+  NumberOfRvaAndSize: 6
+  ExportTable:
+    RelativeVirtualAddress: 0
+    Size:            0
+  ImportTable:
+    RelativeVirtualAddress: 0
+    Size:            0
+  ResourceTable:
+    RelativeVirtualAddress: 0
+    Size:            0
+  ExceptionTable:
+    RelativeVirtualAddress: 0
+    Size:            0
+  CertificateTable:
+    RelativeVirtualAddress: 0
+    Size:            0
+  BaseRelocationTable:
+    RelativeVirtualAddress: 0
+    Size:            0
+header:
+  Machine:         IMAGE_FILE_MACHINE_AMD64
+  Characteristics: [ ]
+sections:
+  - Name:            foo
+    Characteristics: [ ]
+    Alignment:       4
+symbols:
+...
diff --git a/llvm/tools/llvm-objcopy/COFF/Writer.cpp b/llvm/tools/llvm-objcopy/COFF/Writer.cpp
index 8cc2312d137c..cbd0e4261238 100644
--- a/llvm/tools/llvm-objcopy/COFF/Writer.cpp
+++ b/llvm/tools/llvm-objcopy/COFF/Writer.cpp
@@ -406,7 +406,7 @@ Expected<uint32_t> COFFWriter::virtualAddressToFileAddress(uint32_t RVA) {
 // the debug_directory structs in there, and set the PointerToRawData field
 // in all of them, according to their new physical location in the file.
 Error COFFWriter::patchDebugDirectory() {
-  if (Obj.DataDirectories.size() < DEBUG_DIRECTORY)
+  if (Obj.DataDirectories.size() <= DEBUG_DIRECTORY)
     return Error::success();
   const data_directory *Dir = &Obj.DataDirectories[DEBUG_DIRECTORY];
   if (Dir->Size <= 0)
-- 
2.25.1

