| OLD | NEW |
| 1 //===- subzero/src/IceELFSection.cpp - Representation of ELF sections -----===// | 1 //===- subzero/src/IceELFSection.cpp - Representation of ELF sections -----===// |
| 2 // | 2 // |
| 3 // The Subzero Code Generator | 3 // The Subzero Code Generator |
| 4 // | 4 // |
| 5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
| 7 // | 7 // |
| 8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
| 9 /// | 9 /// |
| 10 /// \file | 10 /// \file |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 } | 87 } |
| 88 } | 88 } |
| 89 } | 89 } |
| 90 | 90 |
| 91 size_t ELFRelocationSection::getSectionDataSize() const { | 91 size_t ELFRelocationSection::getSectionDataSize() const { |
| 92 return Fixups.size() * Header.sh_entsize; | 92 return Fixups.size() * Header.sh_entsize; |
| 93 } | 93 } |
| 94 | 94 |
| 95 // Symbol tables. | 95 // Symbol tables. |
| 96 | 96 |
| 97 void ELFSymbolTableSection::createNullSymbol(ELFSection *NullSection) { | 97 void ELFSymbolTableSection::createNullSymbol(ELFSection *NullSection, |
| 98 GlobalContext *Ctx) { |
| 98 // The first entry in the symbol table should be a NULL entry, so make sure | 99 // The first entry in the symbol table should be a NULL entry, so make sure |
| 99 // the map is still empty. | 100 // the map is still empty. |
| 100 assert(LocalSymbols.empty()); | 101 assert(LocalSymbols.empty()); |
| 101 const IceString NullSymName(""); | 102 // Explicitly set the null symbol name to the empty string, so that |
| 102 createDefinedSym(NullSymName, STT_NOTYPE, STB_LOCAL, NullSection, 0, 0); | 103 // GlobalString::operator<() orders the null string first. |
| 103 NullSymbol = findSymbol(NullSymName); | 104 NullSymbolName = GlobalString::createWithString(Ctx, ""); |
| 105 createDefinedSym(NullSymbolName, STT_NOTYPE, STB_LOCAL, NullSection, 0, 0); |
| 106 NullSymbol = findSymbol(NullSymbolName); |
| 104 } | 107 } |
| 105 | 108 |
| 106 void ELFSymbolTableSection::createDefinedSym(const IceString &Name, | 109 void ELFSymbolTableSection::createDefinedSym(GlobalString Name, uint8_t Type, |
| 107 uint8_t Type, uint8_t Binding, | 110 uint8_t Binding, |
| 108 ELFSection *Section, | 111 ELFSection *Section, |
| 109 RelocOffsetT Offset, SizeT Size) { | 112 RelocOffsetT Offset, SizeT Size) { |
| 110 ELFSym NewSymbol = ELFSym(); | 113 ELFSym NewSymbol = ELFSym(); |
| 111 NewSymbol.Sym.setBindingAndType(Binding, Type); | 114 NewSymbol.Sym.setBindingAndType(Binding, Type); |
| 112 NewSymbol.Sym.st_value = Offset; | 115 NewSymbol.Sym.st_value = Offset; |
| 113 NewSymbol.Sym.st_size = Size; | 116 NewSymbol.Sym.st_size = Size; |
| 114 NewSymbol.Section = Section; | 117 NewSymbol.Section = Section; |
| 115 NewSymbol.Number = ELFSym::UnknownNumber; | 118 NewSymbol.Number = ELFSym::UnknownNumber; |
| 116 bool Unique; | 119 bool Unique; |
| 117 if (Binding == STB_LOCAL) | 120 if (Binding == STB_LOCAL) |
| 118 Unique = LocalSymbols.insert(std::make_pair(Name, NewSymbol)).second; | 121 Unique = LocalSymbols.insert(std::make_pair(Name, NewSymbol)).second; |
| 119 else | 122 else |
| 120 Unique = GlobalSymbols.insert(std::make_pair(Name, NewSymbol)).second; | 123 Unique = GlobalSymbols.insert(std::make_pair(Name, NewSymbol)).second; |
| 121 assert(Unique); | 124 assert(Unique); |
| 122 (void)Unique; | 125 (void)Unique; |
| 123 } | 126 } |
| 124 | 127 |
| 125 void ELFSymbolTableSection::noteUndefinedSym(const IceString &Name, | 128 void ELFSymbolTableSection::noteUndefinedSym(GlobalString Name, |
| 126 ELFSection *NullSection) { | 129 ELFSection *NullSection) { |
| 127 ELFSym NewSymbol = ELFSym(); | 130 ELFSym NewSymbol = ELFSym(); |
| 128 NewSymbol.Sym.setBindingAndType(STB_GLOBAL, STT_NOTYPE); | 131 NewSymbol.Sym.setBindingAndType(STB_GLOBAL, STT_NOTYPE); |
| 129 NewSymbol.Section = NullSection; | 132 NewSymbol.Section = NullSection; |
| 130 NewSymbol.Number = ELFSym::UnknownNumber; | 133 NewSymbol.Number = ELFSym::UnknownNumber; |
| 131 bool Unique = GlobalSymbols.insert(std::make_pair(Name, NewSymbol)).second; | 134 bool Unique = GlobalSymbols.insert(std::make_pair(Name, NewSymbol)).second; |
| 132 if (!Unique) { | 135 if (!Unique) { |
| 133 std::string Buffer; | 136 std::string Buffer; |
| 134 llvm::raw_string_ostream StrBuf(Buffer); | 137 llvm::raw_string_ostream StrBuf(Buffer); |
| 135 StrBuf << "Symbol external and defined: " << Name; | 138 StrBuf << "Symbol external and defined: " << Name; |
| 136 llvm::report_fatal_error(StrBuf.str()); | 139 llvm::report_fatal_error(StrBuf.str()); |
| 137 } | 140 } |
| 138 (void)Unique; | 141 (void)Unique; |
| 139 } | 142 } |
| 140 | 143 |
| 141 const ELFSym *ELFSymbolTableSection::findSymbol(const IceString &Name) const { | 144 const ELFSym *ELFSymbolTableSection::findSymbol(GlobalString Name) const { |
| 142 auto I = LocalSymbols.find(Name); | 145 auto I = LocalSymbols.find(Name); |
| 143 if (I != LocalSymbols.end()) | 146 if (I != LocalSymbols.end()) |
| 144 return &I->second; | 147 return &I->second; |
| 145 I = GlobalSymbols.find(Name); | 148 I = GlobalSymbols.find(Name); |
| 146 if (I != GlobalSymbols.end()) | 149 if (I != GlobalSymbols.end()) |
| 147 return &I->second; | 150 return &I->second; |
| 148 return nullptr; | 151 return nullptr; |
| 149 } | 152 } |
| 150 | 153 |
| 151 void ELFSymbolTableSection::updateIndices(const ELFStringTableSection *StrTab) { | 154 void ELFSymbolTableSection::updateIndices(const ELFStringTableSection *StrTab) { |
| 152 SizeT SymNumber = 0; | 155 SizeT SymNumber = 0; |
| 153 for (auto &KeyValue : LocalSymbols) { | 156 for (auto &KeyValue : LocalSymbols) { |
| 154 const IceString &Name = KeyValue.first; | 157 GlobalString Name = KeyValue.first; |
| 155 ELFSection *Section = KeyValue.second.Section; | 158 ELFSection *Section = KeyValue.second.Section; |
| 156 Elf64_Sym &SymInfo = KeyValue.second.Sym; | 159 Elf64_Sym &SymInfo = KeyValue.second.Sym; |
| 157 if (!Name.empty()) | 160 if (Name != NullSymbolName && Name.hasStdString()) |
| 158 SymInfo.st_name = StrTab->getIndex(Name); | 161 SymInfo.st_name = StrTab->getIndex(Name.toString()); |
| 159 SymInfo.st_shndx = Section->getNumber(); | 162 SymInfo.st_shndx = Section->getNumber(); |
| 160 KeyValue.second.setNumber(SymNumber++); | 163 KeyValue.second.setNumber(SymNumber++); |
| 161 } | 164 } |
| 162 for (auto &KeyValue : GlobalSymbols) { | 165 for (auto &KeyValue : GlobalSymbols) { |
| 163 const IceString &Name = KeyValue.first; | 166 const std::string &Name = KeyValue.first.toString(); |
| 164 ELFSection *Section = KeyValue.second.Section; | 167 ELFSection *Section = KeyValue.second.Section; |
| 165 Elf64_Sym &SymInfo = KeyValue.second.Sym; | 168 Elf64_Sym &SymInfo = KeyValue.second.Sym; |
| 166 if (!Name.empty()) | 169 if (!Name.empty()) |
| 167 SymInfo.st_name = StrTab->getIndex(Name); | 170 SymInfo.st_name = StrTab->getIndex(Name); |
| 168 SymInfo.st_shndx = Section->getNumber(); | 171 SymInfo.st_shndx = Section->getNumber(); |
| 169 KeyValue.second.setNumber(SymNumber++); | 172 KeyValue.second.setNumber(SymNumber++); |
| 170 } | 173 } |
| 171 } | 174 } |
| 172 | 175 |
| 173 void ELFSymbolTableSection::writeData(ELFStreamer &Str, bool IsELF64) { | 176 void ELFSymbolTableSection::writeData(ELFStreamer &Str, bool IsELF64) { |
| 174 if (IsELF64) { | 177 if (IsELF64) { |
| 175 writeSymbolMap<true>(Str, LocalSymbols); | 178 writeSymbolMap<true>(Str, LocalSymbols); |
| 176 writeSymbolMap<true>(Str, GlobalSymbols); | 179 writeSymbolMap<true>(Str, GlobalSymbols); |
| 177 } else { | 180 } else { |
| 178 writeSymbolMap<false>(Str, LocalSymbols); | 181 writeSymbolMap<false>(Str, LocalSymbols); |
| 179 writeSymbolMap<false>(Str, GlobalSymbols); | 182 writeSymbolMap<false>(Str, GlobalSymbols); |
| 180 } | 183 } |
| 181 } | 184 } |
| 182 | 185 |
| 183 // String tables. | 186 // String tables. |
| 184 | 187 |
| 185 void ELFStringTableSection::add(const IceString &Str) { | 188 void ELFStringTableSection::add(const std::string &Str) { |
| 186 assert(!isLaidOut()); | 189 assert(!isLaidOut()); |
| 187 assert(!Str.empty()); | 190 assert(!Str.empty()); |
| 188 StringToIndexMap.insert(std::make_pair(Str, UnknownIndex)); | 191 StringToIndexMap.insert(std::make_pair(Str, UnknownIndex)); |
| 189 } | 192 } |
| 190 | 193 |
| 191 size_t ELFStringTableSection::getIndex(const IceString &Str) const { | 194 size_t ELFStringTableSection::getIndex(const std::string &Str) const { |
| 192 assert(isLaidOut()); | 195 assert(isLaidOut()); |
| 193 StringToIndexType::const_iterator It = StringToIndexMap.find(Str); | 196 StringToIndexType::const_iterator It = StringToIndexMap.find(Str); |
| 194 if (It == StringToIndexMap.end()) { | 197 if (It == StringToIndexMap.end()) { |
| 195 llvm_unreachable("String index not found"); | 198 llvm::report_fatal_error("String index not found: " + Str); |
| 196 return UnknownIndex; | 199 return UnknownIndex; |
| 197 } | 200 } |
| 198 return It->second; | 201 return It->second; |
| 199 } | 202 } |
| 200 | 203 |
| 201 bool ELFStringTableSection::SuffixComparator:: | 204 bool ELFStringTableSection::SuffixComparator:: |
| 202 operator()(const IceString &StrA, const IceString &StrB) const { | 205 operator()(const std::string &StrA, const std::string &StrB) const { |
| 203 size_t LenA = StrA.size(); | 206 size_t LenA = StrA.size(); |
| 204 size_t LenB = StrB.size(); | 207 size_t LenB = StrB.size(); |
| 205 size_t CommonLen = std::min(LenA, LenB); | 208 size_t CommonLen = std::min(LenA, LenB); |
| 206 // If there is a difference in the common suffix, use that diff to sort. | 209 // If there is a difference in the common suffix, use that diff to sort. |
| 207 for (size_t i = 0; i < CommonLen; ++i) { | 210 for (size_t i = 0; i < CommonLen; ++i) { |
| 208 char a = StrA[LenA - i - 1]; | 211 char a = StrA[LenA - i - 1]; |
| 209 char b = StrB[LenB - i - 1]; | 212 char b = StrB[LenB - i - 1]; |
| 210 if (a != b) | 213 if (a != b) |
| 211 return a > b; | 214 return a > b; |
| 212 } | 215 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 232 continue; | 235 continue; |
| 233 } | 236 } |
| 234 StringIndex.second = StringData.size(); | 237 StringIndex.second = StringData.size(); |
| 235 std::copy(Cur.begin(), Cur.end(), back_inserter(StringData)); | 238 std::copy(Cur.begin(), Cur.end(), back_inserter(StringData)); |
| 236 StringData.push_back(0); | 239 StringData.push_back(0); |
| 237 Prev = Cur; | 240 Prev = Cur; |
| 238 } | 241 } |
| 239 } | 242 } |
| 240 | 243 |
| 241 } // end of namespace Ice | 244 } // end of namespace Ice |
| OLD | NEW |