Index: llvm/include/llvm/ObjectYAML/ELFYAML.h =================================================================== --- llvm/include/llvm/ObjectYAML/ELFYAML.h +++ llvm/include/llvm/ObjectYAML/ELFYAML.h @@ -26,6 +26,7 @@ namespace ELFYAML { 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 // directly give them their exact sizes. We don't need to worry about Index: llvm/lib/ObjectYAML/ELFEmitter.cpp =================================================================== --- llvm/lib/ObjectYAML/ELFEmitter.cpp +++ llvm/lib/ObjectYAML/ELFEmitter.cpp @@ -401,8 +401,19 @@ 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) { - size_t SuffixPos = S.rfind(" ["); + if (S.empty() || S.back() != SuffixEnd) + return S; + + size_t SuffixPos = S.rfind(SuffixStart); if (SuffixPos == StringRef::npos) return S; return S.substr(0, SuffixPos); Index: llvm/test/Object/X86/obj2yaml-dup-section-name.s =================================================================== --- llvm/test/Object/X86/obj2yaml-dup-section-name.s +++ llvm/test/Object/X86/obj2yaml-dup-section-name.s @@ -9,16 +9,16 @@ # CHECK: - Name: .text.foo{{$}} # CHECK: - Name: .rela.text.foo{{$}} # CHECK: Info: .text.foo{{$}} -# CHECK: - Name: '.group [1]' +# CHECK: - Name: '.group (1)' # CHECK: Members: -# CHECK: - SectionOrType: '.text.foo [1]' -# CHECK: - SectionOrType: '.rela.text.foo [1]' -# CHECK: - Name: '.text.foo [1]' -# CHECK: - Name: '.rela.text.foo [1]' -# CHECK: Info: '.text.foo [1]' +# CHECK: - SectionOrType: '.text.foo (1)' +# CHECK: - SectionOrType: '.rela.text.foo (1)' +# CHECK: - Name: '.text.foo (1)' +# CHECK: - Name: '.rela.text.foo (1)' +# CHECK: Info: '.text.foo (1)' # CHECK: Symbols: # CHECK: Section: .group{{$}} -# CHECK: Section: '.group [1]' +# CHECK: Section: '.group (1)' .section .text.foo,"axG",@progbits,sym1,comdat Index: llvm/test/tools/llvm-dwarfdump/X86/section_sizes_elf.test =================================================================== --- llvm/test/tools/llvm-dwarfdump/X86/section_sizes_elf.test +++ llvm/test/tools/llvm-dwarfdump/X86/section_sizes_elf.test @@ -39,7 +39,7 @@ - Name: .debug_type Type: SHT_PROGBITS Size: 13 - - Name: .debug_type [1] + - Name: .debug_type (1) Type: SHT_PROGBITS Size: 13 - Name: .debug_foo Index: llvm/test/tools/llvm-gsymutil/X86/elf-dwarf.yaml =================================================================== --- llvm/test/tools/llvm-gsymutil/X86/elf-dwarf.yaml +++ llvm/test/tools/llvm-gsymutil/X86/elf-dwarf.yaml @@ -571,7 +571,7 @@ - Name: main.cpp Type: STT_FILE Index: SHN_ABS - - Name: 'crtstuff.c [1]' + - Name: 'crtstuff.c (1)' Type: STT_FILE Index: SHN_ABS - Name: __FRAME_END__ Index: llvm/test/tools/llvm-objcopy/ELF/strip-dwo-groups.test =================================================================== --- llvm/test/tools/llvm-objcopy/ELF/strip-dwo-groups.test +++ llvm/test/tools/llvm-objcopy/ELF/strip-dwo-groups.test @@ -55,7 +55,7 @@ - Name: .text.group1 Type: SHT_PROGBITS Flags: [ SHF_ALLOC, SHF_EXECINSTR, SHF_GROUP ] - - Name: '.group [1]' + - Name: '.group (1)' Type: SHT_GROUP Link: .symtab Info: group2 Index: llvm/test/tools/llvm-readobj/ELF/stack-sizes.test =================================================================== --- llvm/test/tools/llvm-readobj/ELF/stack-sizes.test +++ llvm/test/tools/llvm-readobj/ELF/stack-sizes.test @@ -48,7 +48,7 @@ - Size: 0x10 - Size: 0x20 Link: .text - - Name: '.stack_sizes [1]' + - Name: '.stack_sizes (1)' Type: SHT_PROGBITS Entries: - Address: 0x20 @@ -67,9 +67,9 @@ Addend: 16 Symbol: .text Type: R_X86_64_64 - - Name: '.rela.stack_sizes [1]' + - Name: '.rela.stack_sizes (1)' Type: SHT_RELA - Info: '.stack_sizes [1]' + Info: '.stack_sizes (1)' Relocations: - Offset: 0 Symbol: separate_text_section_baz Index: llvm/test/tools/obj2yaml/ELF/duplicate-symbol-and-section-names.yaml =================================================================== --- llvm/test/tools/obj2yaml/ELF/duplicate-symbol-and-section-names.yaml +++ llvm/test/tools/obj2yaml/ELF/duplicate-symbol-and-section-names.yaml @@ -31,23 +31,23 @@ # CASE1-NEXT: Sections: # CASE1-NEXT: - Name: .foo # CASE1-NEXT: Type: SHT_PROGBITS -# CASE1-NEXT: - Name: '.foo [1]' +# CASE1-NEXT: - Name: '.foo (1)' # CASE1-NEXT: Type: SHT_PROGBITS -# CASE1-NEXT: - Name: '.foo [2]' +# CASE1-NEXT: - Name: '.foo (2)' # CASE1-NEXT: Type: SHT_PROGBITS # CASE1-NEXT: - Name: .bar # CASE1-NEXT: Type: SHT_PROGBITS -# CASE1-NEXT: - Name: '.bar [1]' +# CASE1-NEXT: - Name: '.bar (1)' # CASE1-NEXT: Type: SHT_PROGBITS -# CASE1-NEXT: - Name: '.bar [2]' +# CASE1-NEXT: - Name: '.bar (2)' # CASE1-NEXT: Type: SHT_PROGBITS # CASE1-NEXT: Symbols: # CASE1-NEXT: - Name: localfoo -# CASE1-NEXT: - Name: 'localfoo [1]' -# CASE1-NEXT: - Name: 'localfoo [2]' +# CASE1-NEXT: - Name: 'localfoo (1)' +# CASE1-NEXT: - Name: 'localfoo (2)' # CASE1-NEXT: - Name: localbar -# CASE1-NEXT: - Name: 'localbar [1]' -# CASE1-NEXT: - Name: 'localbar [2]' +# CASE1-NEXT: - Name: 'localbar (1)' +# CASE1-NEXT: - Name: 'localbar (2)' # CASE1-NEXT: ... --- !ELF @@ -59,23 +59,23 @@ Sections: - Name: .foo Type: SHT_PROGBITS - - Name: '.foo [555]' + - Name: '.foo (555)' Type: SHT_PROGBITS - - Name: '.foo [random_tag]' + - Name: '.foo (random_tag)' Type: SHT_PROGBITS - Name: .bar Type: SHT_PROGBITS - - Name: '.bar [666]' + - Name: '.bar (666)' Type: SHT_PROGBITS - - Name: '.bar [random_tag]' + - Name: '.bar (random_tag)' Type: SHT_PROGBITS Symbols: - - Name: 'localfoo [111]' - - Name: 'localfoo [222]' - - Name: 'localfoo [random_tag]' - - Name: 'localbar [333]' - - Name: 'localbar [444]' - - Name: 'localbar [random_tag]' + - Name: 'localfoo (111)' + - Name: 'localfoo (222)' + - Name: 'localfoo (random_tag)' + - Name: 'localbar (333)' + - Name: 'localbar (444)' + - Name: 'localbar (random_tag)' ## Check we can refer to symbols with the same ## name from relocations. @@ -84,14 +84,14 @@ # RUN: obj2yaml %t2 | FileCheck %s --check-prefix=CASE2 # CASE2: Relocations: -# CASE2-NEXT: - Symbol: 'foo [1]' +# CASE2-NEXT: - Symbol: 'foo (1)' # CASE2-NEXT: Type: R_X86_64_PC32 # CASE2-NEXT: - Offset: 0x0000000000000004 # CASE2-NEXT: Symbol: foo # CASE2-NEXT: Type: R_X86_64_PC32 # CASE2-NEXT: Symbols: # CASE2-NEXT: - Name: foo -# CASE2-NEXT: - Name: 'foo [1]' +# CASE2-NEXT: - Name: 'foo (1)' --- !ELF FileHeader: @@ -109,13 +109,13 @@ Link: .symtab Relocations: - Type: R_X86_64_PC32 - Symbol: 'foo [1]' + Symbol: 'foo (1)' - Type: R_X86_64_PC32 Offset: 4 Symbol: foo Symbols: - Name: foo - - Name: 'foo [1]' + - Name: 'foo (1)' ## Check obj2yaml does not add a suffix to a name if the ## symbol is in .symtab and .dynsym at the same time. Index: llvm/test/tools/obj2yaml/ELF/program-headers.yaml =================================================================== --- llvm/test/tools/obj2yaml/ELF/program-headers.yaml +++ llvm/test/tools/obj2yaml/ELF/program-headers.yaml @@ -55,7 +55,7 @@ # YAML-NEXT: - Type: PT_LOAD # YAML-NEXT: Flags: [ PF_R ] # YAML-NEXT: Sections: -# YAML-NEXT: - Section: '.foo [1]' +# YAML-NEXT: - Section: '.foo (1)' # YAML-NEXT: - Section: .baz # YAML-NEXT: VAddr: 0x0000000000002000 # YAML-NEXT: Align: 0x0000000000001000 @@ -123,7 +123,7 @@ - Type: PT_LOAD Flags: [ PF_R ] Sections: - - Section: '.foo [1]' + - Section: '.foo (1)' - Section: .baz VAddr: 0x2000 Align: 0x1000 @@ -209,7 +209,7 @@ Flags: [ SHF_ALLOC, SHF_EXECINSTR ] Address: 0x1008 Size: 0x8 - - Name: '.foo [1]' + - Name: '.foo (1)' Type: SHT_PROGBITS Flags: [ SHF_ALLOC ] Address: 0x2000 Index: llvm/test/tools/yaml2obj/ELF/call-graph-profile-section.yaml =================================================================== --- llvm/test/tools/yaml2obj/ELF/call-graph-profile-section.yaml +++ llvm/test/tools/yaml2obj/ELF/call-graph-profile-section.yaml @@ -142,12 +142,12 @@ Weight: 10 ## Case 2: Test we can refer to symbols with suffixes. - From: foo - To: 'foo [1]' + To: 'foo (1)' Weight: 30 Symbols: - Name: foo - Name: bar - - Name: 'foo [1]' + - Name: 'foo (1)' ## Check we can describe SHT_LLVM_CALL_GRAPH_PROFILE sections using the "Content" tag. # RUN: yaml2obj --docnum=5 %s -o %t.content Index: llvm/test/tools/yaml2obj/ELF/duplicate-section-names.yaml =================================================================== --- llvm/test/tools/yaml2obj/ELF/duplicate-section-names.yaml +++ llvm/test/tools/yaml2obj/ELF/duplicate-section-names.yaml @@ -2,12 +2,16 @@ ## containing sections with duplicate names (but different name suffixes). # 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: Name: .foo ( -# CASE1: Name: .foo ( -# CASE1: Name: .foo2 ( +# CASE1: [Nr] Name Type +# CASE1: [ 1] .foo1 PROGBITS +# CASE1-NEXT: [ 2] .foo PROGBITS +# 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 FileHeader: @@ -20,17 +24,23 @@ Type: SHT_PROGBITS - Name: .foo Type: SHT_PROGBITS - - Name: '.foo [1]' + - Name: '.foo (1)' Type: SHT_PROGBITS - Name: .foo2 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 ## sections with equal names and suffixes. # 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 3 +# 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 --- !ELF FileHeader: @@ -39,11 +49,11 @@ Type: ET_REL Machine: EM_X86_64 Sections: - - Name: '.foo [1]' + - Name: '.foo (1)' Type: SHT_PROGBITS - - Name: '.foo [1]' + - Name: '.foo (1)' Type: SHT_PROGBITS - - Name: '.foo [1]' + - Name: '.foo (1)' Type: SHT_PROGBITS ## Check that yaml2obj reports an error in case we have @@ -102,13 +112,13 @@ Sections: - Name: .foo Type: SHT_PROGBITS - - Name: '.foo [1]' + - Name: '.foo (1)' Type: SHT_PROGBITS Symbols: - Name: foo Section: .foo - Name: bar - Section: '.foo [1]' + Section: '.foo (1)' ## Check that yaml2obj can produce SHT_GROUP sections that ## reference sections and symbols with name suffixes. @@ -156,16 +166,16 @@ - SectionOrType: .text.foo - Name: .text.foo Type: SHT_PROGBITS - - Name: '.group [1]' + - Name: '.group (1)' Type: SHT_GROUP - Info: 'foo [1]' + Info: 'foo (1)' Members: - SectionOrType: GRP_COMDAT - - SectionOrType: '.text.foo [1]' - - Name: '.text.foo [1]' + - SectionOrType: '.text.foo (1)' + - Name: '.text.foo (1)' Type: SHT_PROGBITS Symbols: - Name: foo Section: .text.foo - - Name: 'foo [1]' - Section: '.text.foo [1]' + - Name: 'foo (1)' + Section: '.text.foo (1)' Index: llvm/test/tools/yaml2obj/ELF/duplicate-symbol-names.yaml =================================================================== --- llvm/test/tools/yaml2obj/ELF/duplicate-symbol-names.yaml +++ llvm/test/tools/yaml2obj/ELF/duplicate-symbol-names.yaml @@ -15,13 +15,13 @@ Machine: EM_X86_64 Symbols: - Name: localfoo - - Name: 'localfoo [1]' + - Name: 'localfoo (1)' ## Check that yaml2obj reports an error when we have ## symbols with equal names and suffixes. # 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 FileHeader: @@ -30,9 +30,9 @@ Type: ET_REL Machine: EM_X86_64 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 ## symbols without suffixes in the names and their @@ -94,7 +94,7 @@ Symbol: foo - Offset: 0x1 Type: R_X86_64_NONE - Symbol: 'foo [1]' + Symbol: 'foo (1)' Symbols: - Name: foo - - Name: 'foo [1]' + - Name: 'foo (1)' Index: llvm/test/tools/yaml2obj/ELF/stack-sizes.yaml =================================================================== --- llvm/test/tools/yaml2obj/ELF/stack-sizes.yaml +++ llvm/test/tools/yaml2obj/ELF/stack-sizes.yaml @@ -50,15 +50,15 @@ Machine: EM_X86_64 Sections: ## Valid. - - Name: '.stack_sizes [1]' + - Name: '.stack_sizes (1)' Type: SHT_PROGBITS Content: "100000000000000020" ## Truncated. - - Name: '.stack_sizes [2]' + - Name: '.stack_sizes (2)' Type: SHT_PROGBITS Content: "1000000000000000" ## Empty. - - Name: '.stack_sizes [3]' + - Name: '.stack_sizes (3)' Type: SHT_PROGBITS Content: "" @@ -287,12 +287,12 @@ Type: ET_EXEC Machine: EM_X86_64 Sections: - - Name: '.stack_sizes [1]' + - Name: '.stack_sizes (1)' Type: SHT_PROGBITS Entries: - Address: 0x10 Size: 0x20 - - Name: '.stack_sizes [2]' + - Name: '.stack_sizes (2)' Type: SHT_PROGBITS Entries: - Address: 0x30 Index: llvm/tools/obj2yaml/elf2yaml.cpp =================================================================== --- llvm/tools/obj2yaml/elf2yaml.cpp +++ llvm/tools/obj2yaml/elf2yaml.cpp @@ -129,7 +129,7 @@ auto It = UsedSectionNames.insert({Name, 0}); if (!It.second) - Ret = (Name + " [" + Twine(++It.first->second) + "]").str(); + Ret = ELFYAML::appendUniqueSuffix(Name, Twine(++It.first->second)); else Ret = std::string(Name); return Ret; @@ -161,7 +161,7 @@ auto It = UsedSymbolNames.insert({Name, 0}); if (!It.second) SymbolNames[Index] = - (Name + " [" + Twine(++It.first->second) + "]").str(); + ELFYAML::appendUniqueSuffix(Name, Twine(++It.first->second)); else SymbolNames[Index] = std::string(Name); return SymbolNames[Index];