1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2025-01-31 20:51:52 +01:00

[yaml2obj] - Stop using square brackets for unique suffixes.

For describing section/symbol names we can use unique suffixes,
e.g:

```
- Name: '.foo [1]`
- Name: '.foo [2]`
```

It can be a problem (see https://reviews.llvm.org/D79984#inline-734829),
because `[]` are sometimes used to describe a macros:

```
- Name: "[[a0]]"
```

Seems the better approach is to use something else, like "()".
This patch does it and refactors the code related.

Differential revision: https://reviews.llvm.org/D80123
This commit is contained in:
Georgii Rymar 2020-05-18 16:43:13 +03:00
parent 4cbdb6203c
commit 11f7be54a0
14 changed files with 96 additions and 74 deletions

View File

@ -26,6 +26,7 @@ namespace llvm {
namespace ELFYAML { namespace ELFYAML {
StringRef dropUniqueSuffix(StringRef S); StringRef dropUniqueSuffix(StringRef S);
std::string appendUniqueSuffix(StringRef Name, const Twine& Msg);
// These types are invariant across 32/64-bit ELF, so for simplicity just // These types are invariant across 32/64-bit ELF, so for simplicity just
// directly give them their exact sizes. We don't need to worry about // directly give them their exact sizes. We don't need to worry about

View File

@ -401,8 +401,19 @@ bool ELFState<ELFT>::initImplicitHeader(ContiguousBlobAccumulator &CBA,
return true; return true;
} }
constexpr StringRef SuffixStart = " (";
constexpr char SuffixEnd = ')';
std::string llvm::ELFYAML::appendUniqueSuffix(StringRef Name,
const Twine &Msg) {
return (Name + SuffixStart + Msg + Twine(SuffixEnd)).str();
}
StringRef llvm::ELFYAML::dropUniqueSuffix(StringRef S) { StringRef llvm::ELFYAML::dropUniqueSuffix(StringRef S) {
size_t SuffixPos = S.rfind(" ["); if (S.empty() || S.back() != SuffixEnd)
return S;
size_t SuffixPos = S.rfind(SuffixStart);
if (SuffixPos == StringRef::npos) if (SuffixPos == StringRef::npos)
return S; return S;
return S.substr(0, SuffixPos); return S.substr(0, SuffixPos);

View File

@ -9,16 +9,16 @@
# CHECK: - Name: .text.foo{{$}} # CHECK: - Name: .text.foo{{$}}
# CHECK: - Name: .rela.text.foo{{$}} # CHECK: - Name: .rela.text.foo{{$}}
# CHECK: Info: .text.foo{{$}} # CHECK: Info: .text.foo{{$}}
# CHECK: - Name: '.group [1]' # CHECK: - Name: '.group (1)'
# CHECK: Members: # CHECK: Members:
# CHECK: - SectionOrType: '.text.foo [1]' # CHECK: - SectionOrType: '.text.foo (1)'
# CHECK: - SectionOrType: '.rela.text.foo [1]' # CHECK: - SectionOrType: '.rela.text.foo (1)'
# CHECK: - Name: '.text.foo [1]' # CHECK: - Name: '.text.foo (1)'
# CHECK: - Name: '.rela.text.foo [1]' # CHECK: - Name: '.rela.text.foo (1)'
# CHECK: Info: '.text.foo [1]' # CHECK: Info: '.text.foo (1)'
# CHECK: Symbols: # CHECK: Symbols:
# CHECK: Section: .group{{$}} # CHECK: Section: .group{{$}}
# CHECK: Section: '.group [1]' # CHECK: Section: '.group (1)'
.section .text.foo,"axG",@progbits,sym1,comdat .section .text.foo,"axG",@progbits,sym1,comdat

View File

@ -39,7 +39,7 @@ Sections:
- Name: .debug_type - Name: .debug_type
Type: SHT_PROGBITS Type: SHT_PROGBITS
Size: 13 Size: 13
- Name: .debug_type [1] - Name: .debug_type (1)
Type: SHT_PROGBITS Type: SHT_PROGBITS
Size: 13 Size: 13
- Name: .debug_foo - Name: .debug_foo

View File

@ -571,7 +571,7 @@ Symbols:
- Name: main.cpp - Name: main.cpp
Type: STT_FILE Type: STT_FILE
Index: SHN_ABS Index: SHN_ABS
- Name: 'crtstuff.c [1]' - Name: 'crtstuff.c (1)'
Type: STT_FILE Type: STT_FILE
Index: SHN_ABS Index: SHN_ABS
- Name: __FRAME_END__ - Name: __FRAME_END__

View File

@ -55,7 +55,7 @@ Sections:
- Name: .text.group1 - Name: .text.group1
Type: SHT_PROGBITS Type: SHT_PROGBITS
Flags: [ SHF_ALLOC, SHF_EXECINSTR, SHF_GROUP ] Flags: [ SHF_ALLOC, SHF_EXECINSTR, SHF_GROUP ]
- Name: '.group [1]' - Name: '.group (1)'
Type: SHT_GROUP Type: SHT_GROUP
Link: .symtab Link: .symtab
Info: group2 Info: group2

View File

@ -48,7 +48,7 @@ Sections:
- Size: 0x10 - Size: 0x10
- Size: 0x20 - Size: 0x20
Link: .text Link: .text
- Name: '.stack_sizes [1]' - Name: '.stack_sizes (1)'
Type: SHT_PROGBITS Type: SHT_PROGBITS
Entries: Entries:
- Address: 0x20 - Address: 0x20
@ -67,9 +67,9 @@ Sections:
Addend: 16 Addend: 16
Symbol: .text Symbol: .text
Type: R_X86_64_64 Type: R_X86_64_64
- Name: '.rela.stack_sizes [1]' - Name: '.rela.stack_sizes (1)'
Type: SHT_RELA Type: SHT_RELA
Info: '.stack_sizes [1]' Info: '.stack_sizes (1)'
Relocations: Relocations:
- Offset: 0 - Offset: 0
Symbol: separate_text_section_baz Symbol: separate_text_section_baz

View File

@ -31,23 +31,23 @@
# CASE1-NEXT: Sections: # CASE1-NEXT: Sections:
# CASE1-NEXT: - Name: .foo # CASE1-NEXT: - Name: .foo
# CASE1-NEXT: Type: SHT_PROGBITS # CASE1-NEXT: Type: SHT_PROGBITS
# CASE1-NEXT: - Name: '.foo [1]' # CASE1-NEXT: - Name: '.foo (1)'
# CASE1-NEXT: Type: SHT_PROGBITS # CASE1-NEXT: Type: SHT_PROGBITS
# CASE1-NEXT: - Name: '.foo [2]' # CASE1-NEXT: - Name: '.foo (2)'
# CASE1-NEXT: Type: SHT_PROGBITS # CASE1-NEXT: Type: SHT_PROGBITS
# CASE1-NEXT: - Name: .bar # CASE1-NEXT: - Name: .bar
# CASE1-NEXT: Type: SHT_PROGBITS # CASE1-NEXT: Type: SHT_PROGBITS
# CASE1-NEXT: - Name: '.bar [1]' # CASE1-NEXT: - Name: '.bar (1)'
# CASE1-NEXT: Type: SHT_PROGBITS # CASE1-NEXT: Type: SHT_PROGBITS
# CASE1-NEXT: - Name: '.bar [2]' # CASE1-NEXT: - Name: '.bar (2)'
# CASE1-NEXT: Type: SHT_PROGBITS # CASE1-NEXT: Type: SHT_PROGBITS
# CASE1-NEXT: Symbols: # CASE1-NEXT: Symbols:
# CASE1-NEXT: - Name: localfoo # CASE1-NEXT: - Name: localfoo
# CASE1-NEXT: - Name: 'localfoo [1]' # CASE1-NEXT: - Name: 'localfoo (1)'
# CASE1-NEXT: - Name: 'localfoo [2]' # CASE1-NEXT: - Name: 'localfoo (2)'
# CASE1-NEXT: - Name: localbar # CASE1-NEXT: - Name: localbar
# CASE1-NEXT: - Name: 'localbar [1]' # CASE1-NEXT: - Name: 'localbar (1)'
# CASE1-NEXT: - Name: 'localbar [2]' # CASE1-NEXT: - Name: 'localbar (2)'
# CASE1-NEXT: ... # CASE1-NEXT: ...
--- !ELF --- !ELF
@ -59,23 +59,23 @@ FileHeader:
Sections: Sections:
- Name: .foo - Name: .foo
Type: SHT_PROGBITS Type: SHT_PROGBITS
- Name: '.foo [555]' - Name: '.foo (555)'
Type: SHT_PROGBITS Type: SHT_PROGBITS
- Name: '.foo [random_tag]' - Name: '.foo (random_tag)'
Type: SHT_PROGBITS Type: SHT_PROGBITS
- Name: .bar - Name: .bar
Type: SHT_PROGBITS Type: SHT_PROGBITS
- Name: '.bar [666]' - Name: '.bar (666)'
Type: SHT_PROGBITS Type: SHT_PROGBITS
- Name: '.bar [random_tag]' - Name: '.bar (random_tag)'
Type: SHT_PROGBITS Type: SHT_PROGBITS
Symbols: Symbols:
- Name: 'localfoo [111]' - Name: 'localfoo (111)'
- Name: 'localfoo [222]' - Name: 'localfoo (222)'
- Name: 'localfoo [random_tag]' - Name: 'localfoo (random_tag)'
- Name: 'localbar [333]' - Name: 'localbar (333)'
- Name: 'localbar [444]' - Name: 'localbar (444)'
- Name: 'localbar [random_tag]' - Name: 'localbar (random_tag)'
## Check we can refer to symbols with the same ## Check we can refer to symbols with the same
## name from relocations. ## name from relocations.
@ -84,14 +84,14 @@ Symbols:
# RUN: obj2yaml %t2 | FileCheck %s --check-prefix=CASE2 # RUN: obj2yaml %t2 | FileCheck %s --check-prefix=CASE2
# CASE2: Relocations: # CASE2: Relocations:
# CASE2-NEXT: - Symbol: 'foo [1]' # CASE2-NEXT: - Symbol: 'foo (1)'
# CASE2-NEXT: Type: R_X86_64_PC32 # CASE2-NEXT: Type: R_X86_64_PC32
# CASE2-NEXT: - Offset: 0x0000000000000004 # CASE2-NEXT: - Offset: 0x0000000000000004
# CASE2-NEXT: Symbol: foo # CASE2-NEXT: Symbol: foo
# CASE2-NEXT: Type: R_X86_64_PC32 # CASE2-NEXT: Type: R_X86_64_PC32
# CASE2-NEXT: Symbols: # CASE2-NEXT: Symbols:
# CASE2-NEXT: - Name: foo # CASE2-NEXT: - Name: foo
# CASE2-NEXT: - Name: 'foo [1]' # CASE2-NEXT: - Name: 'foo (1)'
--- !ELF --- !ELF
FileHeader: FileHeader:
@ -109,13 +109,13 @@ Sections:
Link: .symtab Link: .symtab
Relocations: Relocations:
- Type: R_X86_64_PC32 - Type: R_X86_64_PC32
Symbol: 'foo [1]' Symbol: 'foo (1)'
- Type: R_X86_64_PC32 - Type: R_X86_64_PC32
Offset: 4 Offset: 4
Symbol: foo Symbol: foo
Symbols: Symbols:
- Name: foo - Name: foo
- Name: 'foo [1]' - Name: 'foo (1)'
## Check obj2yaml does not add a suffix to a name if the ## Check obj2yaml does not add a suffix to a name if the
## symbol is in .symtab and .dynsym at the same time. ## symbol is in .symtab and .dynsym at the same time.

View File

@ -55,7 +55,7 @@
# YAML-NEXT: - Type: PT_LOAD # YAML-NEXT: - Type: PT_LOAD
# YAML-NEXT: Flags: [ PF_R ] # YAML-NEXT: Flags: [ PF_R ]
# YAML-NEXT: Sections: # YAML-NEXT: Sections:
# YAML-NEXT: - Section: '.foo [1]' # YAML-NEXT: - Section: '.foo (1)'
# YAML-NEXT: - Section: .baz # YAML-NEXT: - Section: .baz
# YAML-NEXT: VAddr: 0x0000000000002000 # YAML-NEXT: VAddr: 0x0000000000002000
# YAML-NEXT: Align: 0x0000000000001000 # YAML-NEXT: Align: 0x0000000000001000
@ -123,7 +123,7 @@ ProgramHeaders:
- Type: PT_LOAD - Type: PT_LOAD
Flags: [ PF_R ] Flags: [ PF_R ]
Sections: Sections:
- Section: '.foo [1]' - Section: '.foo (1)'
- Section: .baz - Section: .baz
VAddr: 0x2000 VAddr: 0x2000
Align: 0x1000 Align: 0x1000
@ -209,7 +209,7 @@ Sections:
Flags: [ SHF_ALLOC, SHF_EXECINSTR ] Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
Address: 0x1008 Address: 0x1008
Size: 0x8 Size: 0x8
- Name: '.foo [1]' - Name: '.foo (1)'
Type: SHT_PROGBITS Type: SHT_PROGBITS
Flags: [ SHF_ALLOC ] Flags: [ SHF_ALLOC ]
Address: 0x2000 Address: 0x2000

View File

@ -142,12 +142,12 @@ Sections:
Weight: 10 Weight: 10
## Case 2: Test we can refer to symbols with suffixes. ## Case 2: Test we can refer to symbols with suffixes.
- From: foo - From: foo
To: 'foo [1]' To: 'foo (1)'
Weight: 30 Weight: 30
Symbols: Symbols:
- Name: foo - Name: foo
- Name: bar - Name: bar
- Name: 'foo [1]' - Name: 'foo (1)'
## Check we can describe SHT_LLVM_CALL_GRAPH_PROFILE sections using the "Content" tag. ## Check we can describe SHT_LLVM_CALL_GRAPH_PROFILE sections using the "Content" tag.
# RUN: yaml2obj --docnum=5 %s -o %t.content # RUN: yaml2obj --docnum=5 %s -o %t.content

View File

@ -2,12 +2,16 @@
## containing sections with duplicate names (but different name suffixes). ## containing sections with duplicate names (but different name suffixes).
# RUN: yaml2obj --docnum=1 %s -o %t1 # RUN: yaml2obj --docnum=1 %s -o %t1
# RUN: llvm-readobj -s %t1 | FileCheck %s --check-prefix=CASE1 # RUN: llvm-readelf -sections %t1 | FileCheck %s --check-prefix=CASE1
# CASE1: Name: .foo1 ( # CASE1: [Nr] Name Type
# CASE1: Name: .foo ( # CASE1: [ 1] .foo1 PROGBITS
# CASE1: Name: .foo ( # CASE1-NEXT: [ 2] .foo PROGBITS
# CASE1: Name: .foo2 ( # CASE1-NEXT: [ 3] .foo PROGBITS
# CASE1-NEXT: [ 4] .foo2 PROGBITS
# CASE1-NEXT: [ 5] .foo2 ( PROGBITS
# CASE1-NEXT: [ 6] .foo2 ) PROGBITS
# CASE1-NEXT: [ 7] .foo2 PROGBITS
--- !ELF --- !ELF
FileHeader: FileHeader:
@ -20,17 +24,23 @@ Sections:
Type: SHT_PROGBITS Type: SHT_PROGBITS
- Name: .foo - Name: .foo
Type: SHT_PROGBITS Type: SHT_PROGBITS
- Name: '.foo [1]' - Name: '.foo (1)'
Type: SHT_PROGBITS Type: SHT_PROGBITS
- Name: .foo2 - Name: .foo2
Type: SHT_PROGBITS Type: SHT_PROGBITS
- Name: '.foo2 ('
Type: SHT_PROGBITS
- Name: '.foo2 )'
Type: SHT_PROGBITS
- Name: '.foo2 ()'
Type: SHT_PROGBITS
## Check that yaml2obj reports an error in case we have ## Check that yaml2obj reports an error in case we have
## sections with equal names and suffixes. ## sections with equal names and suffixes.
# RUN: not yaml2obj --docnum=2 %s 2>&1 | FileCheck %s --check-prefix=CASE2 # RUN: not yaml2obj --docnum=2 %s 2>&1 | FileCheck %s --check-prefix=CASE2
# CASE2: error: repeated section/fill name: '.foo [1]' at YAML section/fill number 2 # CASE2: error: repeated section/fill name: '.foo (1)' at YAML section/fill number 2
# CASE2: error: repeated section/fill name: '.foo [1]' at YAML section/fill number 3 # CASE2: error: repeated section/fill name: '.foo (1)' at YAML section/fill number 3
--- !ELF --- !ELF
FileHeader: FileHeader:
@ -39,11 +49,11 @@ FileHeader:
Type: ET_REL Type: ET_REL
Machine: EM_X86_64 Machine: EM_X86_64
Sections: Sections:
- Name: '.foo [1]' - Name: '.foo (1)'
Type: SHT_PROGBITS Type: SHT_PROGBITS
- Name: '.foo [1]' - Name: '.foo (1)'
Type: SHT_PROGBITS Type: SHT_PROGBITS
- Name: '.foo [1]' - Name: '.foo (1)'
Type: SHT_PROGBITS Type: SHT_PROGBITS
## Check that yaml2obj reports an error in case we have ## Check that yaml2obj reports an error in case we have
@ -102,13 +112,13 @@ FileHeader:
Sections: Sections:
- Name: .foo - Name: .foo
Type: SHT_PROGBITS Type: SHT_PROGBITS
- Name: '.foo [1]' - Name: '.foo (1)'
Type: SHT_PROGBITS Type: SHT_PROGBITS
Symbols: Symbols:
- Name: foo - Name: foo
Section: .foo Section: .foo
- Name: bar - Name: bar
Section: '.foo [1]' Section: '.foo (1)'
## Check that yaml2obj can produce SHT_GROUP sections that ## Check that yaml2obj can produce SHT_GROUP sections that
## reference sections and symbols with name suffixes. ## reference sections and symbols with name suffixes.
@ -156,16 +166,16 @@ Sections:
- SectionOrType: .text.foo - SectionOrType: .text.foo
- Name: .text.foo - Name: .text.foo
Type: SHT_PROGBITS Type: SHT_PROGBITS
- Name: '.group [1]' - Name: '.group (1)'
Type: SHT_GROUP Type: SHT_GROUP
Info: 'foo [1]' Info: 'foo (1)'
Members: Members:
- SectionOrType: GRP_COMDAT - SectionOrType: GRP_COMDAT
- SectionOrType: '.text.foo [1]' - SectionOrType: '.text.foo (1)'
- Name: '.text.foo [1]' - Name: '.text.foo (1)'
Type: SHT_PROGBITS Type: SHT_PROGBITS
Symbols: Symbols:
- Name: foo - Name: foo
Section: .text.foo Section: .text.foo
- Name: 'foo [1]' - Name: 'foo (1)'
Section: '.text.foo [1]' Section: '.text.foo (1)'

View File

@ -15,13 +15,13 @@ FileHeader:
Machine: EM_X86_64 Machine: EM_X86_64
Symbols: Symbols:
- Name: localfoo - Name: localfoo
- Name: 'localfoo [1]' - Name: 'localfoo (1)'
## Check that yaml2obj reports an error when we have ## Check that yaml2obj reports an error when we have
## symbols with equal names and suffixes. ## symbols with equal names and suffixes.
# RUN: not yaml2obj --docnum=2 %s 2>&1| FileCheck %s --check-prefix=CASE2 # RUN: not yaml2obj --docnum=2 %s 2>&1| FileCheck %s --check-prefix=CASE2
# CASE2-COUNT-2: error: repeated symbol name: 'localfoo [1]' # CASE2-COUNT-2: error: repeated symbol name: 'localfoo (1)'
--- !ELF --- !ELF
FileHeader: FileHeader:
@ -30,9 +30,9 @@ FileHeader:
Type: ET_REL Type: ET_REL
Machine: EM_X86_64 Machine: EM_X86_64
Symbols: Symbols:
- Name: 'localfoo [1]' - Name: 'localfoo (1)'
- Name: 'localfoo [1]' - Name: 'localfoo (1)'
- Name: 'localfoo [1]' - Name: 'localfoo (1)'
## Check that yaml2obj reports an error when we have ## Check that yaml2obj reports an error when we have
## symbols without suffixes in the names and their ## symbols without suffixes in the names and their
@ -94,7 +94,7 @@ Sections:
Symbol: foo Symbol: foo
- Offset: 0x1 - Offset: 0x1
Type: R_X86_64_NONE Type: R_X86_64_NONE
Symbol: 'foo [1]' Symbol: 'foo (1)'
Symbols: Symbols:
- Name: foo - Name: foo
- Name: 'foo [1]' - Name: 'foo (1)'

View File

@ -50,15 +50,15 @@ FileHeader:
Machine: EM_X86_64 Machine: EM_X86_64
Sections: Sections:
## Valid. ## Valid.
- Name: '.stack_sizes [1]' - Name: '.stack_sizes (1)'
Type: SHT_PROGBITS Type: SHT_PROGBITS
Content: "100000000000000020" Content: "100000000000000020"
## Truncated. ## Truncated.
- Name: '.stack_sizes [2]' - Name: '.stack_sizes (2)'
Type: SHT_PROGBITS Type: SHT_PROGBITS
Content: "1000000000000000" Content: "1000000000000000"
## Empty. ## Empty.
- Name: '.stack_sizes [3]' - Name: '.stack_sizes (3)'
Type: SHT_PROGBITS Type: SHT_PROGBITS
Content: "" Content: ""
@ -287,12 +287,12 @@ FileHeader:
Type: ET_EXEC Type: ET_EXEC
Machine: EM_X86_64 Machine: EM_X86_64
Sections: Sections:
- Name: '.stack_sizes [1]' - Name: '.stack_sizes (1)'
Type: SHT_PROGBITS Type: SHT_PROGBITS
Entries: Entries:
- Address: 0x10 - Address: 0x10
Size: 0x20 Size: 0x20
- Name: '.stack_sizes [2]' - Name: '.stack_sizes (2)'
Type: SHT_PROGBITS Type: SHT_PROGBITS
Entries: Entries:
- Address: 0x30 - Address: 0x30

View File

@ -129,7 +129,7 @@ ELFDumper<ELFT>::getUniquedSectionName(const Elf_Shdr *Sec) {
auto It = UsedSectionNames.insert({Name, 0}); auto It = UsedSectionNames.insert({Name, 0});
if (!It.second) if (!It.second)
Ret = (Name + " [" + Twine(++It.first->second) + "]").str(); Ret = ELFYAML::appendUniqueSuffix(Name, Twine(++It.first->second));
else else
Ret = std::string(Name); Ret = std::string(Name);
return Ret; return Ret;
@ -161,7 +161,7 @@ ELFDumper<ELFT>::getUniquedSymbolName(const Elf_Sym *Sym, StringRef StrTable,
auto It = UsedSymbolNames.insert({Name, 0}); auto It = UsedSymbolNames.insert({Name, 0});
if (!It.second) if (!It.second)
SymbolNames[Index] = SymbolNames[Index] =
(Name + " [" + Twine(++It.first->second) + "]").str(); ELFYAML::appendUniqueSuffix(Name, Twine(++It.first->second));
else else
SymbolNames[Index] = std::string(Name); SymbolNames[Index] = std::string(Name);
return SymbolNames[Index]; return SymbolNames[Index];