Skip to content

Commit 804b264

Browse files
authored
[obj2yaml] Support CREL
This decoder feature complements the yaml2obj encoder added by #91280. Pull Request: #98116
1 parent dfc8004 commit 804b264

File tree

2 files changed

+132
-20
lines changed

2 files changed

+132
-20
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
## Test how we dump SHT_CREL sections.
2+
# RUN: yaml2obj %s -o %t1
3+
# RUN: obj2yaml %t1 | FileCheck %s --check-prefix=YAML
4+
5+
# YAML: - Name: .crel.text
6+
# YAML-NEXT: Type: SHT_CREL
7+
# YAML-NEXT: Link: .symtab
8+
# YAML-NEXT: Info: .text
9+
# YAML-NEXT: Relocations:
10+
# YAML-NEXT: - Offset: 0x1
11+
# YAML-NEXT: Symbol: g1
12+
# YAML-NEXT: Type: R_X86_64_32
13+
# YAML-NEXT: Addend: 1
14+
# YAML-NEXT: - Offset: 0x2
15+
# YAML-NEXT: Symbol: l1
16+
# YAML-NEXT: Type: R_X86_64_64
17+
# YAML-NEXT: Addend: 2
18+
# YAML-NEXT: - Symbol: g1
19+
# YAML-NEXT: Type: R_X86_64_32S
20+
# YAML-NEXT: Addend: -1
21+
# YAML-NEXT: - Offset: 0x4
22+
# YAML-NEXT: Symbol: .text
23+
# YAML-NEXT: Type: R_X86_64_32S
24+
# YAML-NEXT: Addend: -9223372036854775808
25+
# YAML-NEXT: - Name: .crel.dyn
26+
# YAML-NEXT: Type: SHT_CREL
27+
# YAML-NEXT: Relocations: []
28+
29+
--- !ELF
30+
FileHeader:
31+
Class: ELFCLASS64
32+
Data: ELFDATA2LSB
33+
Type: ET_REL
34+
Machine: EM_X86_64
35+
36+
Sections:
37+
- Name: .foo
38+
Type: SHT_PROGBITS
39+
Flags: [SHF_ALLOC]
40+
- Name: .text
41+
Type: SHT_PROGBITS
42+
Content: "0000000000000000"
43+
Flags: [SHF_ALLOC]
44+
- Name: .crel.text
45+
Type: SHT_CREL
46+
Info: .text
47+
Link: .symtab
48+
Relocations:
49+
- Offset: 0x1
50+
Symbol: g1
51+
Type: R_X86_64_32
52+
Addend: 1
53+
- Offset: 0x2
54+
Symbol: l1
55+
Type: R_X86_64_64
56+
Addend: 2
57+
- Offset: 0x0
58+
Symbol: g1
59+
Type: R_X86_64_32S
60+
Addend: 0xffffffffffffffff
61+
- Offset: 0x4
62+
Symbol: .text
63+
Type: R_X86_64_32S
64+
Addend: 0x8000000000000000
65+
- Name: .crel.dyn
66+
Type: SHT_CREL
67+
Content: 00
68+
## Trigger the .dynsym emission.
69+
DynamicSymbols: []
70+
Symbols:
71+
- Name: unused
72+
Section: .text
73+
- Name: .text
74+
Type: STT_SECTION
75+
Section: .text
76+
- Name: l1
77+
- Name: g1
78+
Section: .text
79+
Value: 0x0
80+
Size: 4
81+
Binding: STB_GLOBAL
82+
83+
## Test the behavior when the sh_entsize field is broken.
84+
85+
# RUN: yaml2obj -DTYPE=SHT_RELA --docnum=2 %s -o %t2
86+
# RUN: not obj2yaml %t2 2>&1 | FileCheck %s --check-prefix=ERR1
87+
88+
# ERR1: unable to decode LEB128 at offset 0x00000000: malformed uleb128, extends past end
89+
90+
--- !ELF
91+
FileHeader:
92+
Class: ELFCLASS64
93+
Data: ELFDATA2LSB
94+
Type: ET_DYN
95+
Sections:
96+
- Name: .foo
97+
Type: SHT_CREL

llvm/tools/obj2yaml/elf2yaml.cpp

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,7 @@ ELFDumper<ELFT>::dumpSections() {
596596
return [this](const Elf_Shdr *S) { return dumpSymtabShndxSection(S); };
597597
case ELF::SHT_REL:
598598
case ELF::SHT_RELA:
599+
case ELF::SHT_CREL:
599600
return [this](const Elf_Shdr *S) { return dumpRelocSection(S); };
600601
case ELF::SHT_RELR:
601602
return [this](const Elf_Shdr *S) { return dumpRelrSection(S); };
@@ -1165,27 +1166,41 @@ ELFDumper<ELFT>::dumpRelocSection(const Elf_Shdr *Shdr) {
11651166
if (Shdr->sh_size != 0)
11661167
S->Relocations.emplace();
11671168

1168-
if (Shdr->sh_type == ELF::SHT_REL) {
1169-
auto Rels = Obj.rels(*Shdr);
1170-
if (!Rels)
1171-
return Rels.takeError();
1172-
for (const Elf_Rel &Rel : *Rels) {
1173-
ELFYAML::Relocation R;
1174-
if (Error E = dumpRelocation(&Rel, *SymTabOrErr, R))
1175-
return std::move(E);
1176-
S->Relocations->push_back(R);
1177-
}
1169+
std::vector<Elf_Rel> Rels;
1170+
std::vector<Elf_Rela> Relas;
1171+
if (Shdr->sh_type == ELF::SHT_CREL) {
1172+
Expected<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(*Shdr);
1173+
if (!ContentOrErr)
1174+
return ContentOrErr.takeError();
1175+
auto Crel = Obj.decodeCrel(*ContentOrErr);
1176+
if (!Crel)
1177+
return Crel.takeError();
1178+
Rels = std::move(Crel->first);
1179+
Relas = std::move(Crel->second);
1180+
} else if (Shdr->sh_type == ELF::SHT_REL) {
1181+
auto R = Obj.rels(*Shdr);
1182+
if (!R)
1183+
return R.takeError();
1184+
Rels = std::move(*R);
11781185
} else {
1179-
auto Rels = Obj.relas(*Shdr);
1180-
if (!Rels)
1181-
return Rels.takeError();
1182-
for (const Elf_Rela &Rel : *Rels) {
1183-
ELFYAML::Relocation R;
1184-
if (Error E = dumpRelocation(&Rel, *SymTabOrErr, R))
1185-
return std::move(E);
1186-
R.Addend = Rel.r_addend;
1187-
S->Relocations->push_back(R);
1188-
}
1186+
auto R = Obj.relas(*Shdr);
1187+
if (!R)
1188+
return R.takeError();
1189+
Relas = std::move(*R);
1190+
}
1191+
1192+
for (const Elf_Rel &Rel : Rels) {
1193+
ELFYAML::Relocation R;
1194+
if (Error E = dumpRelocation(&Rel, *SymTabOrErr, R))
1195+
return std::move(E);
1196+
S->Relocations->push_back(R);
1197+
}
1198+
for (const Elf_Rela &Rel : Relas) {
1199+
ELFYAML::Relocation R;
1200+
if (Error E = dumpRelocation(&Rel, *SymTabOrErr, R))
1201+
return std::move(E);
1202+
R.Addend = Rel.r_addend;
1203+
S->Relocations->push_back(R);
11891204
}
11901205

11911206
return S.release();

0 commit comments

Comments
 (0)