llvm.org GIT mirror llvm / 50bea40
Simplify getSymbolType. This is still a really odd function. Most calls are in object format specific contexts and should probably be replaced with a more direct query, but at least now this is not too obnoxious to use. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@240777 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 5 years ago
14 changed file(s) with 57 addition(s) and 106 deletion(s). Raw diff Collapse all Expand all
638638 uint64_t getSymbolValue(DataRefImpl Symb) const override;
639639 uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
640640 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
641 std::error_code getSymbolType(DataRefImpl Symb,
642 SymbolRef::Type &Res) const override;
641 SymbolRef::Type getSymbolType(DataRefImpl Symb) const override;
643642 std::error_code getSymbolSection(DataRefImpl Symb,
644643 section_iterator &Res) const override;
645644 void moveSectionNext(DataRefImpl &Sec) const override;
135135 uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
136136 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
137137 uint8_t getSymbolOther(DataRefImpl Symb) const override;
138 std::error_code getSymbolType(DataRefImpl Symb,
139 SymbolRef::Type &Res) const override;
138 SymbolRef::Type getSymbolType(DataRefImpl Symb) const override;
140139 section_iterator getSymbolSection(const Elf_Sym *Symb) const;
141140 std::error_code getSymbolSection(DataRefImpl Symb,
142141 section_iterator &Res) const override;
389388 }
390389
391390 template
392 std::error_code
393 ELFObjectFile::getSymbolType(DataRefImpl Symb,
394 SymbolRef::Type &Result) const {
391 SymbolRef::Type ELFObjectFile::getSymbolType(DataRefImpl Symb) const {
395392 const Elf_Sym *ESym = getSymbol(Symb);
396393
397394 switch (ESym->getType()) {
398395 case ELF::STT_NOTYPE:
399 Result = SymbolRef::ST_Unknown;
400 break;
396 return SymbolRef::ST_Unknown;
401397 case ELF::STT_SECTION:
402 Result = SymbolRef::ST_Debug;
403 break;
398 return SymbolRef::ST_Debug;
404399 case ELF::STT_FILE:
405 Result = SymbolRef::ST_File;
406 break;
400 return SymbolRef::ST_File;
407401 case ELF::STT_FUNC:
408 Result = SymbolRef::ST_Function;
409 break;
402 return SymbolRef::ST_Function;
410403 case ELF::STT_OBJECT:
411404 case ELF::STT_COMMON:
412405 case ELF::STT_TLS:
413 Result = SymbolRef::ST_Data;
414 break;
406 return SymbolRef::ST_Data;
415407 default:
416 Result = SymbolRef::ST_Other;
417 break;
418 }
419 return std::error_code();
408 return SymbolRef::ST_Other;
409 }
420410 }
421411
422412 template
210210 uint64_t getSymbolValue(DataRefImpl Symb) const override;
211211 uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
212212 uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
213 std::error_code getSymbolType(DataRefImpl Symb,
214 SymbolRef::Type &Res) const override;
213 SymbolRef::Type getSymbolType(DataRefImpl Symb) const override;
215214 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
216215 std::error_code getSymbolSection(DataRefImpl Symb,
217216 section_iterator &Res) const override;
149149 /// @brief Get the alignment of this symbol as the actual value (not log 2).
150150 uint32_t getAlignment() const;
151151 uint64_t getCommonSize() const;
152 std::error_code getType(SymbolRef::Type &Result) const;
152 SymbolRef::Type getType() const;
153153
154154 /// @brief Get section this symbol is defined in reference to. Result is
155155 /// end_sections() if it is undefined or is an absolute symbol.
209209 virtual uint64_t getSymbolValue(DataRefImpl Symb) const = 0;
210210 virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const;
211211 virtual uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const = 0;
212 virtual std::error_code getSymbolType(DataRefImpl Symb,
213 SymbolRef::Type &Res) const = 0;
212 virtual SymbolRef::Type getSymbolType(DataRefImpl Symb) const = 0;
214213 virtual std::error_code getSymbolSection(DataRefImpl Symb,
215214 section_iterator &Res) const = 0;
216215
346345 return getObject()->getSymbolSection(getRawDataRefImpl(), Result);
347346 }
348347
349 inline std::error_code SymbolRef::getType(SymbolRef::Type &Result) const {
350 return getObject()->getSymbolType(getRawDataRefImpl(), Result);
348 inline SymbolRef::Type SymbolRef::getType() const {
349 return getObject()->getSymbolType(getRawDataRefImpl());
351350 }
352351
353352 inline const ObjectFile *SymbolRef::getObject() const {
174174 if (IsCommon)
175175 CommonSymbols.push_back(*I);
176176 else {
177 object::SymbolRef::Type SymType;
178 Check(I->getType(SymType));
177 object::SymbolRef::Type SymType = I->getType();
179178
180179 if (SymType == object::SymbolRef::ST_Function ||
181180 SymType == object::SymbolRef::ST_Data ||
10811081 RTDyldSymbolTable::const_iterator gsi = GlobalSymbolTable.end();
10821082 if (Symbol != Obj.symbol_end()) {
10831083 gsi = GlobalSymbolTable.find(TargetName.data());
1084 Symbol->getType(SymType);
1084 SymType = Symbol->getType();
10851085 }
10861086 if (gsi != GlobalSymbolTable.end()) {
10871087 const auto &SymInfo = gsi->second;
176176 return std::error_code();
177177 }
178178
179 std::error_code COFFObjectFile::getSymbolType(DataRefImpl Ref,
180 SymbolRef::Type &Result) const {
179 SymbolRef::Type COFFObjectFile::getSymbolType(DataRefImpl Ref) const {
181180 COFFSymbolRef Symb = getCOFFSymbol(Ref);
182181 int32_t SectionNumber = Symb.getSectionNumber();
183 Result = SymbolRef::ST_Other;
184
185 if (Symb.isAnyUndefined()) {
186 Result = SymbolRef::ST_Unknown;
187 } else if (Symb.isFunctionDefinition()) {
188 Result = SymbolRef::ST_Function;
189 } else if (Symb.isCommon()) {
190 Result = SymbolRef::ST_Data;
191 } else if (Symb.isFileRecord()) {
192 Result = SymbolRef::ST_File;
193 } else if (SectionNumber == COFF::IMAGE_SYM_DEBUG ||
194 Symb.isSectionDefinition()) {
195 // TODO: perhaps we need a new symbol type ST_Section.
196 Result = SymbolRef::ST_Debug;
197 } else if (!COFF::isReservedSectionNumber(SectionNumber)) {
198 const coff_section *Section = nullptr;
199 if (std::error_code EC = getSection(SectionNumber, Section))
200 return EC;
201 uint32_t Characteristics = Section->Characteristics;
202 if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
203 Result = SymbolRef::ST_Function;
204 else if (Characteristics & (COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
205 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA))
206 Result = SymbolRef::ST_Data;
207 }
208 return std::error_code();
182
183 if (Symb.isAnyUndefined())
184 return SymbolRef::ST_Unknown;
185 if (Symb.isFunctionDefinition())
186 return SymbolRef::ST_Function;
187 if (Symb.isCommon())
188 return SymbolRef::ST_Data;
189 if (Symb.isFileRecord())
190 return SymbolRef::ST_File;
191
192 // TODO: perhaps we need a new symbol type ST_Section.
193 if (SectionNumber == COFF::IMAGE_SYM_DEBUG || Symb.isSectionDefinition())
194 return SymbolRef::ST_Debug;
195
196 if (!COFF::isReservedSectionNumber(SectionNumber))
197 return SymbolRef::ST_Data;
198
199 return SymbolRef::ST_Other;
209200 }
210201
211202 uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const {
398398 return Value;
399399 }
400400
401 std::error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
402 SymbolRef::Type &Res) const {
401 SymbolRef::Type MachOObjectFile::getSymbolType(DataRefImpl Symb) const {
403402 MachO::nlist_base Entry = getSymbolTableEntryBase(this, Symb);
404403 uint8_t n_type = Entry.n_type;
405404
406 Res = SymbolRef::ST_Other;
407
408405 // If this is a STAB debugging symbol, we can do nothing more.
409 if (n_type & MachO::N_STAB) {
410 Res = SymbolRef::ST_Debug;
411 return std::error_code();
412 }
406 if (n_type & MachO::N_STAB)
407 return SymbolRef::ST_Debug;
413408
414409 switch (n_type & MachO::N_TYPE) {
415410 case MachO::N_UNDF :
416 Res = SymbolRef::ST_Unknown;
417 break;
411 return SymbolRef::ST_Unknown;
418412 case MachO::N_SECT :
419 Res = SymbolRef::ST_Function;
420 break;
421 }
422 return std::error_code();
413 return SymbolRef::ST_Function;
414 }
415 return SymbolRef::ST_Other;
423416 }
424417
425418 uint32_t MachOObjectFile::getSymbolFlags(DataRefImpl DRI) const {
575568
576569 bool MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
577570 DataRefImpl Symb) const {
578 SymbolRef::Type ST;
579 this->getSymbolType(Symb, ST);
571 SymbolRef::Type ST = getSymbolType(Symb);
580572 if (ST == SymbolRef::ST_Unknown)
581573 return false;
582574
220220 const MachOObjectFile &MainBinary = MainBinaryHolder.GetAs();
221221 section_iterator Section = MainBinary.section_end();
222222 for (const auto &Sym : MainBinary.symbols()) {
223 SymbolRef::Type Type;
223 SymbolRef::Type Type = Sym.getType();
224224 // Skip undefined and STAB entries.
225 if (Sym.getType(Type) || (Type & SymbolRef::ST_Debug) ||
226 (Type & SymbolRef::ST_Unknown))
225 if ((Type & SymbolRef::ST_Debug) || (Type & SymbolRef::ST_Unknown))
227226 continue;
228227 StringRef Name;
229228 uint64_t Addr;
177177
178178 struct SymbolSorter {
179179 bool operator()(const SymbolRef &A, const SymbolRef &B) {
180 SymbolRef::Type AType, BType;
181 A.getType(AType);
182 B.getType(BType);
180 SymbolRef::Type AType = A.getType();
181 SymbolRef::Type BType = B.getType();
183182
184183 uint64_t AAddr, BAddr;
185184 if (AType != SymbolRef::ST_Function)
587586 SymbolAddressMap *AddrMap) {
588587 // Create a map of symbol addresses to symbol names.
589588 for (const SymbolRef &Symbol : O->symbols()) {
590 SymbolRef::Type ST;
591 Symbol.getType(ST);
589 SymbolRef::Type ST = Symbol.getType();
592590 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
593591 ST == SymbolRef::ST_Other) {
594592 uint64_t Address;
61236121 SymbolAddressMap AddrMap;
61246122 bool DisSymNameFound = false;
61256123 for (const SymbolRef &Symbol : MachOOF->symbols()) {
6126 SymbolRef::Type ST;
6127 Symbol.getType(ST);
6124 SymbolRef::Type ST = Symbol.getType();
61286125 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
61296126 ST == SymbolRef::ST_Other) {
61306127 uint64_t Address;
61726169 StringRef SymName;
61736170 Symbols[SymIdx].getName(SymName);
61746171
6175 SymbolRef::Type ST;
6176 Symbols[SymIdx].getType(ST);
6172 SymbolRef::Type ST = Symbols[SymIdx].getType();
61776173 if (ST != SymbolRef::ST_Function)
61786174 continue;
61796175
61986194 uint64_t NextSym = 0;
61996195 uint64_t NextSymIdx = SymIdx + 1;
62006196 while (Symbols.size() > NextSymIdx) {
6201 SymbolRef::Type NextSymType;
6202 Symbols[NextSymIdx].getType(NextSymType);
6197 SymbolRef::Type NextSymType = Symbols[NextSymIdx].getType();
62036198 if (NextSymType == SymbolRef::ST_Function) {
62046199 containsNextSym =
62056200 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]);
10771077 }
10781078 for (const SymbolRef &Symbol : o->symbols()) {
10791079 uint64_t Address;
1080 SymbolRef::Type Type;
1080 SymbolRef::Type Type = Symbol.getType();
10811081 uint32_t Flags = Symbol.getFlags();
10821082 section_iterator Section = o->section_end();
10831083 if (error(Symbol.getAddress(Address)))
1084 continue;
1085 if (error(Symbol.getType(Type)))
10861084 continue;
10871085 if (error(Symbol.getSection(Section)))
10881086 continue;
197197 ErrorOr Decoder::getSymbol(const COFFObjectFile &COFF,
198198 uint64_t VA, bool FunctionOnly) {
199199 for (const auto &Symbol : COFF.symbols()) {
200 if (FunctionOnly) {
201 SymbolRef::Type Type;
202 if (std::error_code EC = Symbol.getType(Type))
203 return EC;
204 if (Type != SymbolRef::ST_Function)
205 continue;
206 }
200 if (FunctionOnly && Symbol.getType() != SymbolRef::ST_Function)
201 continue;
207202
208203 uint64_t Address;
209204 if (std::error_code EC = Symbol.getAddress(Address))
264264 // Use symbol info to iterate functions in the object.
265265 for (const auto &P : SymAddr) {
266266 object::SymbolRef Sym = P.first;
267 object::SymbolRef::Type SymType;
268 if (Sym.getType(SymType))
269 continue;
270 if (SymType == object::SymbolRef::ST_Function) {
267 if (Sym.getType() == object::SymbolRef::ST_Function) {
271268 StringRef Name;
272269 uint64_t Addr;
273270 if (Sym.getName(Name))
7979
8080 void ModuleInfo::addSymbol(const SymbolRef &Symbol, uint64_t SymbolSize,
8181 DataExtractor *OpdExtractor, uint64_t OpdAddress) {
82 SymbolRef::Type SymbolType;
83 if (error(Symbol.getType(SymbolType)))
84 return;
82 SymbolRef::Type SymbolType = Symbol.getType();
8583 if (SymbolType != SymbolRef::ST_Function && SymbolType != SymbolRef::ST_Data)
8684 return;
8785 uint64_t SymbolAddress;