[llvm] 804b264 - [obj2yaml] Support CREL

via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 9 10:39:19 PDT 2024


Author: Fangrui Song
Date: 2024-07-09T10:39:15-07:00
New Revision: 804b264d02f24ffb1cb2415d5a8141a267f66279

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

LOG: [obj2yaml] Support CREL

This decoder feature complements the yaml2obj encoder added by #91280.

Pull Request: https://github.com/llvm/llvm-project/pull/98116

Added: 
    llvm/test/tools/obj2yaml/ELF/crel-section.yaml

Modified: 
    llvm/tools/obj2yaml/elf2yaml.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/test/tools/obj2yaml/ELF/crel-section.yaml b/llvm/test/tools/obj2yaml/ELF/crel-section.yaml
new file mode 100644
index 0000000000000..2e6c60b46de03
--- /dev/null
+++ b/llvm/test/tools/obj2yaml/ELF/crel-section.yaml
@@ -0,0 +1,97 @@
+## Test how we dump SHT_CREL sections.
+# RUN: yaml2obj %s -o %t1
+# RUN: obj2yaml %t1 | FileCheck %s --check-prefix=YAML
+
+# YAML:      - Name:            .crel.text
+# YAML-NEXT:   Type:            SHT_CREL
+# YAML-NEXT:   Link:            .symtab
+# YAML-NEXT:   Info:            .text
+# YAML-NEXT:   Relocations:
+# YAML-NEXT:     - Offset:          0x1
+# YAML-NEXT:       Symbol:          g1
+# YAML-NEXT:       Type:            R_X86_64_32
+# YAML-NEXT:       Addend:          1
+# YAML-NEXT:     - Offset:          0x2
+# YAML-NEXT:       Symbol:          l1
+# YAML-NEXT:       Type:            R_X86_64_64
+# YAML-NEXT:       Addend:          2
+# YAML-NEXT:     - Symbol:          g1
+# YAML-NEXT:       Type:            R_X86_64_32S
+# YAML-NEXT:       Addend:          -1
+# YAML-NEXT:     - Offset:          0x4
+# YAML-NEXT:       Symbol:          .text
+# YAML-NEXT:       Type:            R_X86_64_32S
+# YAML-NEXT:       Addend:          -9223372036854775808
+# YAML-NEXT: - Name:            .crel.dyn
+# YAML-NEXT:   Type:            SHT_CREL
+# YAML-NEXT:   Relocations:     []
+
+--- !ELF
+FileHeader:
+  Class: ELFCLASS64
+  Data: ELFDATA2LSB
+  Type: ET_REL
+  Machine: EM_X86_64
+
+Sections:
+- Name: .foo
+  Type: SHT_PROGBITS
+  Flags: [SHF_ALLOC]
+- Name: .text
+  Type: SHT_PROGBITS
+  Content: "0000000000000000"
+  Flags: [SHF_ALLOC]
+- Name: .crel.text
+  Type: SHT_CREL
+  Info: .text
+  Link: .symtab
+  Relocations:
+    - Offset: 0x1
+      Symbol: g1
+      Type:   R_X86_64_32
+      Addend: 1
+    - Offset: 0x2
+      Symbol: l1
+      Type:   R_X86_64_64
+      Addend: 2
+    - Offset: 0x0
+      Symbol: g1
+      Type:   R_X86_64_32S
+      Addend: 0xffffffffffffffff
+    - Offset: 0x4
+      Symbol: .text
+      Type:   R_X86_64_32S
+      Addend: 0x8000000000000000
+- Name:    .crel.dyn
+  Type:    SHT_CREL
+  Content: 00
+## Trigger the .dynsym emission.
+DynamicSymbols: []
+Symbols:
+  - Name: unused
+    Section: .text
+  - Name: .text
+    Type: STT_SECTION
+    Section: .text
+  - Name:    l1
+  - Name:    g1
+    Section: .text
+    Value:   0x0
+    Size:    4
+    Binding: STB_GLOBAL
+
+## Test the behavior when the sh_entsize field is broken.
+
+# RUN: yaml2obj -DTYPE=SHT_RELA --docnum=2 %s -o %t2
+# RUN: not obj2yaml %t2 2>&1 | FileCheck %s --check-prefix=ERR1
+
+# ERR1: unable to decode LEB128 at offset 0x00000000: malformed uleb128, extends past end
+
+--- !ELF
+FileHeader:
+  Class: ELFCLASS64
+  Data:  ELFDATA2LSB
+  Type:  ET_DYN
+Sections:
+  - Name:    .foo
+    Type:    SHT_CREL

diff  --git a/llvm/tools/obj2yaml/elf2yaml.cpp b/llvm/tools/obj2yaml/elf2yaml.cpp
index 6b9af906736c3..9b4644bde36c0 100644
--- a/llvm/tools/obj2yaml/elf2yaml.cpp
+++ b/llvm/tools/obj2yaml/elf2yaml.cpp
@@ -596,6 +596,7 @@ ELFDumper<ELFT>::dumpSections() {
       return [this](const Elf_Shdr *S) { return dumpSymtabShndxSection(S); };
     case ELF::SHT_REL:
     case ELF::SHT_RELA:
+    case ELF::SHT_CREL:
       return [this](const Elf_Shdr *S) { return dumpRelocSection(S); };
     case ELF::SHT_RELR:
       return [this](const Elf_Shdr *S) { return dumpRelrSection(S); };
@@ -1165,27 +1166,41 @@ ELFDumper<ELFT>::dumpRelocSection(const Elf_Shdr *Shdr) {
   if (Shdr->sh_size != 0)
     S->Relocations.emplace();
 
-  if (Shdr->sh_type == ELF::SHT_REL) {
-    auto Rels = Obj.rels(*Shdr);
-    if (!Rels)
-      return Rels.takeError();
-    for (const Elf_Rel &Rel : *Rels) {
-      ELFYAML::Relocation R;
-      if (Error E = dumpRelocation(&Rel, *SymTabOrErr, R))
-        return std::move(E);
-      S->Relocations->push_back(R);
-    }
+  std::vector<Elf_Rel> Rels;
+  std::vector<Elf_Rela> Relas;
+  if (Shdr->sh_type == ELF::SHT_CREL) {
+    Expected<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(*Shdr);
+    if (!ContentOrErr)
+      return ContentOrErr.takeError();
+    auto Crel = Obj.decodeCrel(*ContentOrErr);
+    if (!Crel)
+      return Crel.takeError();
+    Rels = std::move(Crel->first);
+    Relas = std::move(Crel->second);
+  } else if (Shdr->sh_type == ELF::SHT_REL) {
+    auto R = Obj.rels(*Shdr);
+    if (!R)
+      return R.takeError();
+    Rels = std::move(*R);
   } else {
-    auto Rels = Obj.relas(*Shdr);
-    if (!Rels)
-      return Rels.takeError();
-    for (const Elf_Rela &Rel : *Rels) {
-      ELFYAML::Relocation R;
-      if (Error E = dumpRelocation(&Rel, *SymTabOrErr, R))
-        return std::move(E);
-      R.Addend = Rel.r_addend;
-      S->Relocations->push_back(R);
-    }
+    auto R = Obj.relas(*Shdr);
+    if (!R)
+      return R.takeError();
+    Relas = std::move(*R);
+  }
+
+  for (const Elf_Rel &Rel : Rels) {
+    ELFYAML::Relocation R;
+    if (Error E = dumpRelocation(&Rel, *SymTabOrErr, R))
+      return std::move(E);
+    S->Relocations->push_back(R);
+  }
+  for (const Elf_Rela &Rel : Relas) {
+    ELFYAML::Relocation R;
+    if (Error E = dumpRelocation(&Rel, *SymTabOrErr, R))
+      return std::move(E);
+    R.Addend = Rel.r_addend;
+    S->Relocations->push_back(R);
   }
 
   return S.release();


        


More information about the llvm-commits mailing list