[llvm] [ObjectYAML][ELF] Report incorrect offset to generate notes (PR #118741)

Igor Kudrin via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 13 20:54:21 PST 2024


https://github.com/igorkudrin updated https://github.com/llvm/llvm-project/pull/118741

>From 3df90c73f870564e120b161915e2a37110d33f05 Mon Sep 17 00:00:00 2001
From: Igor Kudrin <ikudrin at accesssoftek.com>
Date: Wed, 4 Dec 2024 18:58:06 -0800
Subject: [PATCH 1/2] [ObjectYAML][ELF] Report incorrect offset to generate
 notes

All notes in the note section must be correctly aligned, including the
first. The tool should refuse to generate notes if the section offset is
incorrect in this respect.
---
 llvm/lib/ObjectYAML/ELFEmitter.cpp            |  9 ++-
 .../test/tools/yaml2obj/ELF/note-section.yaml | 55 +++++++++++++++++++
 2 files changed, 63 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/ObjectYAML/ELFEmitter.cpp b/llvm/lib/ObjectYAML/ELFEmitter.cpp
index 5daf6c32ec936a..cc41bbe6bbde24 100644
--- a/llvm/lib/ObjectYAML/ELFEmitter.cpp
+++ b/llvm/lib/ObjectYAML/ELFEmitter.cpp
@@ -1796,7 +1796,7 @@ template <class ELFT>
 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
                                          const ELFYAML::NoteSection &Section,
                                          ContiguousBlobAccumulator &CBA) {
-  if (!Section.Notes)
+  if (!Section.Notes || Section.Notes->empty())
     return;
 
   unsigned Align;
@@ -1814,6 +1814,13 @@ void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
     return;
   }
 
+  if (CBA.getOffset() != alignTo(CBA.getOffset(), Align)) {
+    reportError(Section.Name + ": invalid offset of a note section: 0x" +
+                Twine::utohexstr(CBA.getOffset()) + ", should be aligned to " +
+                Twine(Align));
+    return;
+  }
+
   uint64_t Offset = CBA.tell();
   for (const ELFYAML::NoteEntry &NE : *Section.Notes) {
     // Write name size.
diff --git a/llvm/test/tools/yaml2obj/ELF/note-section.yaml b/llvm/test/tools/yaml2obj/ELF/note-section.yaml
index 10fd36fefeabfd..ef65f98409de26 100644
--- a/llvm/test/tools/yaml2obj/ELF/note-section.yaml
+++ b/llvm/test/tools/yaml2obj/ELF/note-section.yaml
@@ -514,3 +514,58 @@ Sections:
         Desc: 030405
       - Name: GNU
         Type: NT_GNU_BUILD_ID
+
+## Check that an incorrect offset for generating notes is reported.
+
+# RUN: not yaml2obj --docnum=19 %s 2>&1 | FileCheck %s --check-prefix=ERR_OFFSET
+# ERR_OFFSET: error: .note: invalid offset of a note section: 0x{{.*}}, should be aligned to 4
+
+--- !ELF
+FileHeader:
+  Class: ELFCLASS32
+  Data:  ELFDATA2LSB
+  Type:  ET_EXEC
+Sections:
+  - Name: .dummy
+    Type: SHT_PROGBITS
+    Size: 1
+  - Name: .note
+    Type: SHT_NOTE
+    Notes:
+      - Type: 0x1
+
+## Do not issue an error if the notes array is empty.
+
+# RUN: yaml2obj --docnum=20 %s -o - | \
+# RUN:   llvm-readobj --sections --section-data - | \
+# RUN:   FileCheck %s --check-prefix=TEST20
+
+# TEST20:      Section {
+# TEST20:      Name: .note
+# TEST20-NEXT:   Type: SHT_NOTE
+# TEST20-NEXT:   Flags [ (0x0)
+# TEST20-NEXT:   ]
+# TEST20-NEXT:   Address:
+# TEST20-NEXT:   Offset:
+# TEST20-NEXT:   Size: 0
+# TEST20-NEXT:   Link:
+# TEST20-NEXT:   Info:
+# TEST20-NEXT:   AddressAlignment: 5
+# TEST20-NEXT:   EntrySize:
+# TEST20-NEXT:   SectionData (
+# TEST20-NEXT:   )
+# TEST20-NEXT: }
+
+--- !ELF
+FileHeader:
+  Class: ELFCLASS32
+  Data:  ELFDATA2LSB
+  Type:  ET_EXEC
+Sections:
+  - Name: .dummy
+    Type: SHT_PROGBITS
+    Size: 1
+  - Name: .note
+    Type: SHT_NOTE
+    AddressAlign: 5
+    Notes: []

>From 67792d28ee539fa0526f8d5b2e5fda6c27e66603 Mon Sep 17 00:00:00 2001
From: Igor Kudrin <ikudrin at accesssoftek.com>
Date: Fri, 13 Dec 2024 20:53:57 -0800
Subject: [PATCH 2/2] fixup: use an exact offset value in the test

---
 .../test/tools/yaml2obj/ELF/note-section.yaml | 22 ++++++++-----------
 1 file changed, 9 insertions(+), 13 deletions(-)

diff --git a/llvm/test/tools/yaml2obj/ELF/note-section.yaml b/llvm/test/tools/yaml2obj/ELF/note-section.yaml
index ef65f98409de26..944e158f4ffa93 100644
--- a/llvm/test/tools/yaml2obj/ELF/note-section.yaml
+++ b/llvm/test/tools/yaml2obj/ELF/note-section.yaml
@@ -518,7 +518,7 @@ Sections:
 ## Check that an incorrect offset for generating notes is reported.
 
 # RUN: not yaml2obj --docnum=19 %s 2>&1 | FileCheck %s --check-prefix=ERR_OFFSET
-# ERR_OFFSET: error: .note: invalid offset of a note section: 0x{{.*}}, should be aligned to 4
+# ERR_OFFSET: error: .note: invalid offset of a note section: 0x501, should be aligned to 4
 
 --- !ELF
 FileHeader:
@@ -526,11 +526,9 @@ FileHeader:
   Data:  ELFDATA2LSB
   Type:  ET_EXEC
 Sections:
-  - Name: .dummy
-    Type: SHT_PROGBITS
-    Size: 1
-  - Name: .note
-    Type: SHT_NOTE
+  - Name:   .note
+    Type:   SHT_NOTE
+    Offset: 0x501
     Notes:
       - Type: 0x1
 
@@ -546,7 +544,7 @@ Sections:
 # TEST20-NEXT:   Flags [ (0x0)
 # TEST20-NEXT:   ]
 # TEST20-NEXT:   Address:
-# TEST20-NEXT:   Offset:
+# TEST20-NEXT:   Offset: 0x501
 # TEST20-NEXT:   Size: 0
 # TEST20-NEXT:   Link:
 # TEST20-NEXT:   Info:
@@ -562,10 +560,8 @@ FileHeader:
   Data:  ELFDATA2LSB
   Type:  ET_EXEC
 Sections:
-  - Name: .dummy
-    Type: SHT_PROGBITS
-    Size: 1
-  - Name: .note
-    Type: SHT_NOTE
+  - Name:         .note
+    Type:         SHT_NOTE
+    Offset:       0x501
     AddressAlign: 5
-    Notes: []
+    Notes:        []



More information about the llvm-commits mailing list