[llvm] fef3bfb - [yaml2obj] Fix bug when referencing items in SectionHeaderTable

James Henderson via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 16 02:02:45 PDT 2021


Author: James Henderson
Date: 2021-06-16T10:02:22+01:00
New Revision: fef3bfb1b23af09668d20b09f09c65cc14b5479c

URL: https://github.com/llvm/llvm-project/commit/fef3bfb1b23af09668d20b09f09c65cc14b5479c
DIFF: https://github.com/llvm/llvm-project/commit/fef3bfb1b23af09668d20b09f09c65cc14b5479c.diff

LOG: [yaml2obj] Fix bug when referencing items in SectionHeaderTable

There was an off-by-one error caused by an index (which included an
index for the null section header) being used to check against the size
of a list of sections (which didn't include the null section header).

This is a partial fix for https://bugs.llvm.org/show_bug.cgi?id=50506.

Reviewed by: MaskRay

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

Added: 
    

Modified: 
    llvm/lib/ObjectYAML/ELFEmitter.cpp
    llvm/test/tools/yaml2obj/ELF/section-headers.yaml

Removed: 
    


################################################################################
diff  --git a/llvm/lib/ObjectYAML/ELFEmitter.cpp b/llvm/lib/ObjectYAML/ELFEmitter.cpp
index b77c36dde9de9..a4e0fe004988a 100644
--- a/llvm/lib/ObjectYAML/ELFEmitter.cpp
+++ b/llvm/lib/ObjectYAML/ELFEmitter.cpp
@@ -563,7 +563,7 @@ unsigned ELFState<ELFT>::toSectionIndex(StringRef S, StringRef LocSec,
          !SectionHeaders.Sections);
   size_t FirstExcluded =
       SectionHeaders.Sections ? SectionHeaders.Sections->size() : 0;
-  if (Index >= FirstExcluded) {
+  if (Index > FirstExcluded) {
     if (LocSym.empty())
       reportError("unable to link '" + LocSec + "' to excluded section '" + S +
                   "'");

diff  --git a/llvm/test/tools/yaml2obj/ELF/section-headers.yaml b/llvm/test/tools/yaml2obj/ELF/section-headers.yaml
index 3d09290a6a5a1..5bc6ac5496f4d 100644
--- a/llvm/test/tools/yaml2obj/ELF/section-headers.yaml
+++ b/llvm/test/tools/yaml2obj/ELF/section-headers.yaml
@@ -418,3 +418,39 @@ Sections:
     Sections:
       - Name: .strtab
       - Name: .shstrtab
+
+## Show that we can reference both the first and last section in the section
+## header table, via sh_link.
+
+# RUN: yaml2obj %s --docnum=14 -o %t14
+# RUN: llvm-readelf --sections %t14 | FileCheck %s --check-prefix=LINK
+
+--- !ELF
+FileHeader:
+  Class: ELFCLASS64
+  Data:  ELFDATA2LSB
+  Type:  ET_REL
+Sections:
+  - Name: .strtab
+    Type: SHT_STRTAB
+  - Type: SectionHeaderTable
+    Sections:
+      - Name: .strtab
+      - Name: .ref.first
+      - Name: .ref.last
+      - Name: .shstrtab
+  - Name: .ref.first
+    Type: SHT_PROGBITS
+    Link: .strtab
+  - Name: .ref.last
+    Type: SHT_PROGBITS
+    Link: .shstrtab
+  - Name: .shstrtab
+    Type: SHT_STRTAB
+
+# LINK:      There are 5 section headers
+# LINK:        [Nr] Name       Type     Address  Off      Size     ES       Flg Lk
+# LINK:        [ 1] .strtab
+# LINK-NEXT:   [ 2] .ref.first PROGBITS [[#%x,]] [[#%x,]] [[#%x,]] [[#%x,]]      1
+# LINK-NEXT:   [ 3] .ref.last  PROGBITS [[#%x,]] [[#%x,]] [[#%x,]] [[#%x,]]      4
+# LINK-NEXT:   [ 4] .shstrtab


        


More information about the llvm-commits mailing list