llvm.org GIT mirror llvm / 7b7c81c
Delete UnknownAddress. It is a perfectly valid symbol value. getSymbolValue now returns a value that in convenient for most callers: * 0 for undefined * symbol size for common symbols * offset/address for symbols the rest Code that needs something more specific can check getSymbolFlags. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@241605 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 5 years ago
15 changed file(s) with 42 addition(s) and 72 deletion(s). Raw diff Collapse all Expand all
648648 void moveSymbolNext(DataRefImpl &Symb) const override;
649649 ErrorOr getSymbolName(DataRefImpl Symb) const override;
650650 ErrorOr getSymbolAddress(DataRefImpl Symb) const override;
651 uint64_t getSymbolValue(DataRefImpl Symb) const override;
651 uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
652652 uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
653653 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
654654 SymbolRef::Type getSymbolType(DataRefImpl Symb) const override;
196196 void moveSymbolNext(DataRefImpl &Symb) const override;
197197 ErrorOr getSymbolName(DataRefImpl Symb) const override;
198198 ErrorOr getSymbolAddress(DataRefImpl Symb) const override;
199 uint64_t getSymbolValue(DataRefImpl Symb) const override;
199 uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
200200 uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
201201 uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
202202 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
369369 }
370370
371371 template
372 uint64_t ELFObjectFile::getSymbolValue(DataRefImpl Symb) const {
372 uint64_t ELFObjectFile::getSymbolValueImpl(DataRefImpl Symb) const {
373373 const Elf_Sym *ESym = getSymbol(Symb);
374 switch (ESym->st_shndx) {
375 case ELF::SHN_COMMON:
376 case ELF::SHN_UNDEF:
377 return UnknownAddress;
378 case ELF::SHN_ABS:
379 return ESym->st_value;
380 }
374 uint64_t Ret = ESym->st_value;
375 if (ESym->st_shndx == ELF::SHN_ABS)
376 return Ret;
381377
382378 const Elf_Ehdr *Header = EF.getHeader();
383 uint64_t Ret = ESym->st_value;
384
385379 // Clear the ARM/Thumb or microMIPS indicator flag.
386380 if ((Header->e_machine == ELF::EM_ARM || Header->e_machine == ELF::EM_MIPS) &&
387381 ESym->getType() == ELF::STT_FUNC)
205205 unsigned getSectionType(SectionRef Sec) const;
206206
207207 ErrorOr getSymbolAddress(DataRefImpl Symb) const override;
208 uint64_t getSymbolValue(DataRefImpl Symb) const override;
209208 uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
210209 uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
211210 SymbolRef::Type getSymbolType(DataRefImpl Symb) const override;
424423 }
425424
426425 private:
426 uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
427
427428 union {
428429 MachO::mach_header_64 Header64;
429430 MachO::mach_header Header;
197197 std::error_code printSymbolName(raw_ostream &OS,
198198 DataRefImpl Symb) const override;
199199 virtual ErrorOr getSymbolAddress(DataRefImpl Symb) const = 0;
200 virtual uint64_t getSymbolValue(DataRefImpl Symb) const = 0;
200 virtual uint64_t getSymbolValueImpl(DataRefImpl Symb) const = 0;
201201 virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const;
202202 virtual uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const = 0;
203203 virtual SymbolRef::Type getSymbolType(DataRefImpl Symb) const = 0;
232232 virtual void getRelocationTypeName(DataRefImpl Rel,
233233 SmallVectorImpl &Result) const = 0;
234234
235 uint64_t getSymbolValue(DataRefImpl Symb) const;
236
235237 public:
236238 uint64_t getCommonSymbolSize(DataRefImpl Symb) const {
237239 assert(getSymbolFlags(Symb) & SymbolRef::SF_Common);
114114
115115 typedef content_iterator basic_symbol_iterator;
116116
117 const uint64_t UnknownAddress = ~0ULL;
118
119117 class SymbolicFile : public Binary {
120118 public:
121119 ~SymbolicFile() override;
153153 return Result;
154154 }
155155
156 uint64_t COFFObjectFile::getSymbolValue(DataRefImpl Ref) const {
157 COFFSymbolRef Sym = getCOFFSymbol(Ref);
158
159 if (Sym.isAnyUndefined() || Sym.isCommon())
160 return UnknownAddress;
161
162 return Sym.getValue();
156 uint64_t COFFObjectFile::getSymbolValueImpl(DataRefImpl Ref) const {
157 return getCOFFSymbol(Ref).getValue();
163158 }
164159
165160 ErrorOr COFFObjectFile::getSymbolAddress(DataRefImpl Ref) const {
367367 return std::error_code();
368368 }
369369
370 uint64_t MachOObjectFile::getSymbolValue(DataRefImpl Sym) const {
371 MachO::nlist_base Entry = getSymbolTableEntryBase(this, Sym);
372 if ((Entry.n_type & MachO::N_TYPE) == MachO::N_UNDF)
373 return UnknownAddress;
370 uint64_t MachOObjectFile::getSymbolValueImpl(DataRefImpl Sym) const {
374371 return getNValue(Sym);
375372 }
376373
3232 if (S.getSection(SymSec))
3333 return false;
3434 return *this == *SymSec;
35 }
36
37 uint64_t ObjectFile::getSymbolValue(DataRefImpl Ref) const {
38 uint32_t Flags = getSymbolFlags(Ref);
39 if (Flags & SymbolRef::SF_Undefined)
40 return 0;
41 if (Flags & SymbolRef::SF_Common)
42 return getCommonSymbolSize(Ref);
43 return getSymbolValueImpl(Ref);
3544 }
3645
3746 std::error_code ObjectFile::printSymbolName(raw_ostream &OS,
33 // CHECK: 0000000000000000 ffffffffffffffff n a
44 // CHECK: 0000000000000000 0000000000000000 N b
55 // CHECK: 0000000000000004 0000000000000004 C c
6 // CHECK: ffffffffffffffff 0000000000000000 a d
67
78 .section foo
89 a:
1213 b:
1314
1415 .comm c,4,8
16
17 d = 0xffffffffffffffff
215215 // during the test, we can't hardcode the symbols addresses, so
216216 // look them up here and rewrite them.
217217 for (const auto &Sym : ErrOrObjectFile->symbols()) {
218 uint64_t Address;
219 if (Sym.getFlags() & SymbolRef::SF_Common)
220 Address = Sym.getCommonSize();
221 else
222 Address = Sym.getValue();
218 uint64_t Address = Sym.getValue();
223219 ErrorOr Name = Sym.getName();
224220 if (!Name)
225221 continue;
159159 // symbol table to find its address as it might not be in the
160160 // debug map (for common symbols).
161161 Value = getMainBinarySymbolAddress(Name);
162 if (Value == UnknownAddress)
163 return;
164162 break;
165163 case MachO::N_FUN:
166164 // Functions are scopes in STABS. They have an end marker that
196194 CurrentObjectAddresses.clear();
197195
198196 for (auto Sym : CurrentObjectHolder.Get().symbols()) {
199 uint64_t Addr;
200 if (Sym.getFlags() & SymbolRef::SF_Common) {
201 Addr = Sym.getCommonSize();
202 } else {
203 Addr = Sym.getValue();
204 if (Addr == UnknownAddress)
205 continue;
206 }
197 uint64_t Addr = Sym.getValue();
207198 ErrorOr Name = Sym.getName();
208199 if (!Name)
209200 continue;
217208 uint64_t MachODebugMapParser::getMainBinarySymbolAddress(StringRef Name) {
218209 auto Sym = MainBinarySymbolAddresses.find(Name);
219210 if (Sym == MainBinarySymbolAddresses.end())
220 return UnknownAddress;
211 return 0;
221212 return Sym->second;
222213 }
223214
231222 // Skip undefined and STAB entries.
232223 if ((Type & SymbolRef::ST_Debug) || (Type & SymbolRef::ST_Unknown))
233224 continue;
234 uint64_t Addr = Sym.getValue();
235225 // The only symbols of interest are the global variables. These
236226 // are the only ones that need to be queried because the address
237227 // of common data won't be described in the debug map. All other
238228 // addresses should be fetched for the debug map.
239 if (Addr == UnknownAddress || !(Sym.getFlags() & SymbolRef::SF_Global) ||
240 Sym.getSection(Section) || Section->isText())
241 continue;
229 if (!(Sym.getFlags() & SymbolRef::SF_Global) || Sym.getSection(Section) ||
230 Section == MainBinary.section_end() || Section->isText())
231 continue;
232 uint64_t Addr = Sym.getValue();
242233 ErrorOr NameOrErr = Sym.getName();
243234 if (!NameOrErr)
244235 continue;
563563 char SymbolAddrStr[18] = "";
564564 char SymbolSizeStr[18] = "";
565565
566 if (OutputFormat == sysv || I->Address == UnknownAddress)
566 if (OutputFormat == sysv || I->TypeChar == 'U')
567567 strcpy(SymbolAddrStr, printBlanks);
568568 if (OutputFormat == sysv)
569569 strcpy(SymbolSizeStr, printBlanks);
570570
571 if (I->Address != UnknownAddress)
571 if (I->TypeChar != 'U')
572572 format(printFormat, I->Address)
573573 .print(SymbolAddrStr, sizeof(SymbolAddrStr));
574574 format(printFormat, I->Size).print(SymbolSizeStr, sizeof(SymbolSizeStr));
880880 continue;
881881 NMSymbol S;
882882 S.Size = 0;
883 S.Address = UnknownAddress;
883 S.Address = 0;
884884 if (PrintSize) {
885885 if (isa(&Obj))
886886 S.Size = ELFSymbolRef(Sym).getSize();
887887 }
888888 if (PrintAddress && isa(Obj)) {
889889 SymbolRef SymRef(Sym);
890 if (SymFlags & SymbolRef::SF_Common) {
891 S.Address = SymRef.getCommonSize();
892 } else {
893 ErrorOr AddressOrErr = SymRef.getAddress();
894 if (error(AddressOrErr.getError()))
895 break;
896 S.Address = *AddressOrErr;
897 }
890 ErrorOr AddressOrErr = SymRef.getAddress();
891 if (error(AddressOrErr.getError()))
892 break;
893 S.Address = *AddressOrErr;
898894 }
899895 S.TypeChar = getNMTypeChar(Obj, Sym);
900896 if (error(Sym.printName(OS)))
24122412 // symbol is passed, look up that address in the info's AddrMap.
24132413 static const char *get_symbol_64(uint32_t sect_offset, SectionRef S,
24142414 DisassembleInfo *info, uint64_t &n_value,
2415 uint64_t ReferenceValue = UnknownAddress) {
2415 uint64_t ReferenceValue = 0) {
24162416 n_value = 0;
24172417 if (!info->verbose)
24182418 return nullptr;
24452445 const char *SymbolName = nullptr;
24462446 if (reloc_found && isExtern) {
24472447 n_value = Symbol.getValue();
2448 if (n_value == UnknownAddress)
2449 n_value = 0;
24502448 ErrorOr NameOrError = Symbol.getName();
24512449 if (std::error_code EC = NameOrError.getError())
24522450 report_fatal_error(EC.message());
24682466
24692467 // We did not find an external relocation entry so look up the ReferenceValue
24702468 // as an address of a symbol and if found return that symbol's name.
2471 if (ReferenceValue != UnknownAddress)
2472 SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
2469 SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
24732470
24742471 return SymbolName;
24752472 }
824824 if (error(AddressOrErr.getError()))
825825 break;
826826 uint64_t Address = *AddressOrErr;
827 if (Address == UnknownAddress)
828 continue;
829827 Address -= SectionAddr;
830828 if (Address >= SectSize)
831829 continue;
11351133 bool Common = Flags & SymbolRef::SF_Common;
11361134 bool Hidden = Flags & SymbolRef::SF_Hidden;
11371135
1138 if (Common)
1139 Address = Symbol.getCommonSize();
1140
1141 if (Address == UnknownAddress)
1142 Address = 0;
11431136 char GlobLoc = ' ';
11441137 if (Type != SymbolRef::ST_Unknown)
11451138 GlobLoc = Global ? 'g' : 'l';
8787 if (error(SymbolAddressOrErr.getError()))
8888 return;
8989 uint64_t SymbolAddress = *SymbolAddressOrErr;
90 if (SymbolAddress == UnknownAddress)
91 return;
9290 if (OpdExtractor) {
9391 // For big-endian PowerPC64 ELF, symbols in the .opd section refer to
9492 // function descriptors. The first word of the descriptor is a pointer to