1 //===- unittest/IceELFSectionTest.cpp - ELF Section unit tests ------------===//
2 //
3 // The Subzero Code Generator
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include <algorithm>
11
12 #include "gtest/gtest.h"
13
14 #include "IceDefs.h"
15 #include "IceELFSection.h"
16
17 #include "llvm/Support/raw_os_ostream.h"
18
19 namespace Ice {
20 namespace {
21
22 // Test string table layout for various permutations. Test that pop,
23 // lollipop, and lipop are able to share data, while the other strings do not.
CheckStringTablePermLayout(const ELFStringTableSection & Strtab)24 void CheckStringTablePermLayout(const ELFStringTableSection &Strtab) {
25 size_t pop_index = Strtab.getIndex("pop");
26 size_t pop_size = std::string("pop").size();
27 size_t lollipop_index = Strtab.getIndex("lollipop");
28 size_t lollipop_size = std::string("lollipop").size();
29 size_t lipop_index = Strtab.getIndex("lipop");
30 size_t lipop_size = std::string("lipop").size();
31 size_t pops_index = Strtab.getIndex("pops");
32 size_t pops_size = std::string("pops").size();
33 size_t unpop_index = Strtab.getIndex("unpop");
34 size_t unpop_size = std::string("unpop").size();
35 size_t popular_index = Strtab.getIndex("popular");
36 size_t popular_size = std::string("popular").size();
37 size_t strtab_index = Strtab.getIndex(".strtab");
38 size_t strtab_size = std::string(".strtab").size();
39 size_t shstrtab_index = Strtab.getIndex(".shstrtab");
40 size_t shstrtab_size = std::string(".shstrtab").size();
41 size_t symtab_index = Strtab.getIndex(".symtab");
42 size_t symtab_size = std::string(".symtab").size();
43
44 // Check that some sharing exists.
45 EXPECT_EQ(pop_index, lollipop_index + (lollipop_size - pop_size));
46 EXPECT_EQ(lipop_index, lollipop_index + (lollipop_size - lipop_size));
47
48 // Check that .strtab does not share with .shstrtab (the dot throws it off).
49 EXPECT_NE(strtab_index, shstrtab_index + (shstrtab_size - strtab_size));
50
51 // Check contents make sense.
52 EXPECT_EQ(Strtab.getSectionData().slice(pop_index, pop_index + pop_size),
53 llvm::StringRef("pop"));
54 EXPECT_EQ(Strtab.getSectionData().slice(lollipop_index,
55 lollipop_index + lollipop_size),
56 llvm::StringRef("lollipop"));
57 EXPECT_EQ(Strtab.getSectionData().slice(pops_index, pops_index + pops_size),
58 llvm::StringRef("pops"));
59 EXPECT_EQ(
60 Strtab.getSectionData().slice(unpop_index, unpop_index + unpop_size),
61 llvm::StringRef("unpop"));
62 EXPECT_EQ(Strtab.getSectionData().slice(popular_index,
63 popular_index + popular_size),
64 llvm::StringRef("popular"));
65 EXPECT_EQ(
66 Strtab.getSectionData().slice(strtab_index, strtab_index + strtab_size),
67 llvm::StringRef(".strtab"));
68 EXPECT_EQ(Strtab.getSectionData().slice(shstrtab_index,
69 shstrtab_index + shstrtab_size),
70 llvm::StringRef(".shstrtab"));
71 EXPECT_EQ(
72 Strtab.getSectionData().slice(symtab_index, symtab_index + symtab_size),
73 llvm::StringRef(".symtab"));
74 }
75
76 // Test that the order in which strings are added doesn't matter.
TEST(IceELFSectionTest,StringTableBuilderPermSeveral)77 TEST(IceELFSectionTest, StringTableBuilderPermSeveral) {
78 std::vector<std::string> Strings;
79 Strings.push_back("pop");
80 Strings.push_back("lollipop");
81 Strings.push_back("lipop");
82 Strings.push_back("pops");
83 Strings.push_back("unpop");
84 Strings.push_back("popular");
85 Strings.push_back("a");
86 Strings.push_back("z");
87 Strings.push_back("foo");
88 Strings.push_back("bar");
89 Strings.push_back(".text");
90 Strings.push_back(".symtab");
91 Strings.push_back(".strtab");
92 Strings.push_back(".shstrtab");
93 Strings.push_back("_start");
94 const SizeT NumTests = 128;
95 const uint64_t RandomSeed = 12345; // arbitrary value for now
96 RandomNumberGenerator R(RandomSeed);
97 RandomNumberGeneratorWrapper RNG(R);
98 for (SizeT i = 0; i < NumTests; ++i) {
99 auto Str = std::unique_ptr<Ostream>(new llvm::raw_os_ostream(std::cout));
100 RandomShuffle(Strings.begin(), Strings.end(), RNG);
101 ELFStringTableSection Strtab(".strtab", SHT_STRTAB, 0, 1, 0);
102 for (auto &S : Strings) {
103 Strtab.add(S);
104 }
105 Strtab.doLayout();
106 CheckStringTablePermLayout(Strtab);
107 }
108 }
109
110 // Test that adding duplicate strings is fine.
TEST(IceELFSectionTest,StringTableBuilderDuplicates)111 TEST(IceELFSectionTest, StringTableBuilderDuplicates) {
112 auto Str = std::unique_ptr<Ostream>(new llvm::raw_os_ostream(std::cout));
113 ELFStringTableSection Strtab(".strtab", SHT_STRTAB, 0, 1, 0);
114 Strtab.add("unpop");
115 Strtab.add("pop");
116 Strtab.add("lollipop");
117 Strtab.add("a");
118 Strtab.add("popular");
119 Strtab.add("pops");
120 Strtab.add("lipop");
121 Strtab.add(".strtab");
122 Strtab.add(".shstrtab");
123 Strtab.add(".symtab");
124
125 Strtab.add(".symtab");
126 Strtab.add(".shstrtab");
127 Strtab.add(".strtab");
128 Strtab.add("lipop");
129 Strtab.add("pops");
130 Strtab.add("popular");
131 Strtab.add("a");
132 Strtab.add("lollipop");
133 Strtab.add("pop");
134 Strtab.add("unpop");
135
136 Strtab.doLayout();
137 CheckStringTablePermLayout(Strtab);
138 }
139
140 } // end of anonymous namespace
141 } // end of namespace Ice
142