Skip to content

Commit

Permalink
Delete UnknownAddress. It is a perfectly valid symbol value.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
espindola committed Jul 7, 2015
1 parent 9327084 commit 7b7c81c
Show file tree
Hide file tree
Showing 15 changed files with 41 additions and 71 deletions.
2 changes: 1 addition & 1 deletion include/llvm/Object/COFF.h
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ class COFFObjectFile : public ObjectFile {
void moveSymbolNext(DataRefImpl &Symb) const override;
ErrorOr<StringRef> getSymbolName(DataRefImpl Symb) const override;
ErrorOr<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
uint64_t getSymbolValue(DataRefImpl Symb) const override;
uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
uint32_t getSymbolFlags(DataRefImpl Symb) const override;
SymbolRef::Type getSymbolType(DataRefImpl Symb) const override;
Expand Down
16 changes: 5 additions & 11 deletions include/llvm/Object/ELFObjectFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
void moveSymbolNext(DataRefImpl &Symb) const override;
ErrorOr<StringRef> getSymbolName(DataRefImpl Symb) const override;
ErrorOr<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
uint64_t getSymbolValue(DataRefImpl Symb) const override;
uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
uint32_t getSymbolFlags(DataRefImpl Symb) const override;
Expand Down Expand Up @@ -370,19 +370,13 @@ uint32_t ELFObjectFile<ELFT>::getSectionType(DataRefImpl Sec) const {
}

template <class ELFT>
uint64_t ELFObjectFile<ELFT>::getSymbolValue(DataRefImpl Symb) const {
uint64_t ELFObjectFile<ELFT>::getSymbolValueImpl(DataRefImpl Symb) const {
const Elf_Sym *ESym = getSymbol(Symb);
switch (ESym->st_shndx) {
case ELF::SHN_COMMON:
case ELF::SHN_UNDEF:
return UnknownAddress;
case ELF::SHN_ABS:
return ESym->st_value;
}

const Elf_Ehdr *Header = EF.getHeader();
uint64_t Ret = ESym->st_value;
if (ESym->st_shndx == ELF::SHN_ABS)
return Ret;

const Elf_Ehdr *Header = EF.getHeader();
// Clear the ARM/Thumb or microMIPS indicator flag.
if ((Header->e_machine == ELF::EM_ARM || Header->e_machine == ELF::EM_MIPS) &&
ESym->getType() == ELF::STT_FUNC)
Expand Down
3 changes: 2 additions & 1 deletion include/llvm/Object/MachO.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ class MachOObjectFile : public ObjectFile {
unsigned getSectionType(SectionRef Sec) const;

ErrorOr<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
uint64_t getSymbolValue(DataRefImpl Symb) const override;
uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
SymbolRef::Type getSymbolType(DataRefImpl Symb) const override;
Expand Down Expand Up @@ -425,6 +424,8 @@ class MachOObjectFile : public ObjectFile {
}

private:
uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;

union {
MachO::mach_header_64 Header64;
MachO::mach_header Header;
Expand Down
4 changes: 3 additions & 1 deletion include/llvm/Object/ObjectFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class ObjectFile : public SymbolicFile {
std::error_code printSymbolName(raw_ostream &OS,
DataRefImpl Symb) const override;
virtual ErrorOr<uint64_t> getSymbolAddress(DataRefImpl Symb) const = 0;
virtual uint64_t getSymbolValue(DataRefImpl Symb) const = 0;
virtual uint64_t getSymbolValueImpl(DataRefImpl Symb) const = 0;
virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const;
virtual uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const = 0;
virtual SymbolRef::Type getSymbolType(DataRefImpl Symb) const = 0;
Expand Down Expand Up @@ -233,6 +233,8 @@ class ObjectFile : public SymbolicFile {
virtual void getRelocationTypeName(DataRefImpl Rel,
SmallVectorImpl<char> &Result) const = 0;

uint64_t getSymbolValue(DataRefImpl Symb) const;

public:
uint64_t getCommonSymbolSize(DataRefImpl Symb) const {
assert(getSymbolFlags(Symb) & SymbolRef::SF_Common);
Expand Down
2 changes: 0 additions & 2 deletions include/llvm/Object/SymbolicFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ class BasicSymbolRef {

typedef content_iterator<BasicSymbolRef> basic_symbol_iterator;

const uint64_t UnknownAddress = ~0ULL;

class SymbolicFile : public Binary {
public:
~SymbolicFile() override;
Expand Down
9 changes: 2 additions & 7 deletions lib/Object/COFFObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,8 @@ ErrorOr<StringRef> COFFObjectFile::getSymbolName(DataRefImpl Ref) const {
return Result;
}

uint64_t COFFObjectFile::getSymbolValue(DataRefImpl Ref) const {
COFFSymbolRef Sym = getCOFFSymbol(Ref);

if (Sym.isAnyUndefined() || Sym.isCommon())
return UnknownAddress;

return Sym.getValue();
uint64_t COFFObjectFile::getSymbolValueImpl(DataRefImpl Ref) const {
return getCOFFSymbol(Ref).getValue();
}

ErrorOr<uint64_t> COFFObjectFile::getSymbolAddress(DataRefImpl Ref) const {
Expand Down
5 changes: 1 addition & 4 deletions lib/Object/MachOObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,10 +368,7 @@ std::error_code MachOObjectFile::getIndirectName(DataRefImpl Symb,
return std::error_code();
}

uint64_t MachOObjectFile::getSymbolValue(DataRefImpl Sym) const {
MachO::nlist_base Entry = getSymbolTableEntryBase(this, Sym);
if ((Entry.n_type & MachO::N_TYPE) == MachO::N_UNDF)
return UnknownAddress;
uint64_t MachOObjectFile::getSymbolValueImpl(DataRefImpl Sym) const {
return getNValue(Sym);
}

Expand Down
9 changes: 9 additions & 0 deletions lib/Object/ObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ bool SectionRef::containsSymbol(SymbolRef S) const {
return *this == *SymSec;
}

uint64_t ObjectFile::getSymbolValue(DataRefImpl Ref) const {
uint32_t Flags = getSymbolFlags(Ref);
if (Flags & SymbolRef::SF_Undefined)
return 0;
if (Flags & SymbolRef::SF_Common)
return getCommonSymbolSize(Ref);
return getSymbolValueImpl(Ref);
}

std::error_code ObjectFile::printSymbolName(raw_ostream &OS,
DataRefImpl Symb) const {
ErrorOr<StringRef> Name = getSymbolName(Symb);
Expand Down
3 changes: 3 additions & 0 deletions test/Object/X86/nm-print-size.s
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// CHECK: 0000000000000000 ffffffffffffffff n a
// CHECK: 0000000000000000 0000000000000000 N b
// CHECK: 0000000000000004 0000000000000004 C c
// CHECK: ffffffffffffffff 0000000000000000 a d

.section foo
a:
Expand All @@ -13,3 +14,5 @@ a:
b:

.comm c,4,8

d = 0xffffffffffffffff
6 changes: 1 addition & 5 deletions tools/dsymutil/DebugMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,7 @@ MappingTraits<dsymutil::DebugMapObject>::YamlDMO::denormalize(IO &IO) {
// during the test, we can't hardcode the symbols addresses, so
// look them up here and rewrite them.
for (const auto &Sym : ErrOrObjectFile->symbols()) {
uint64_t Address;
if (Sym.getFlags() & SymbolRef::SF_Common)
Address = Sym.getCommonSize();
else
Address = Sym.getValue();
uint64_t Address = Sym.getValue();
ErrorOr<StringRef> Name = Sym.getName();
if (!Name)
continue;
Expand Down
19 changes: 5 additions & 14 deletions tools/dsymutil/MachODebugMapParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,6 @@ void MachODebugMapParser::handleStabSymbolTableEntry(uint32_t StringIndex,
// symbol table to find its address as it might not be in the
// debug map (for common symbols).
Value = getMainBinarySymbolAddress(Name);
if (Value == UnknownAddress)
return;
break;
case MachO::N_FUN:
// Functions are scopes in STABS. They have an end marker that
Expand Down Expand Up @@ -197,14 +195,7 @@ void MachODebugMapParser::loadCurrentObjectFileSymbols() {
CurrentObjectAddresses.clear();

for (auto Sym : CurrentObjectHolder.Get().symbols()) {
uint64_t Addr;
if (Sym.getFlags() & SymbolRef::SF_Common) {
Addr = Sym.getCommonSize();
} else {
Addr = Sym.getValue();
if (Addr == UnknownAddress)
continue;
}
uint64_t Addr = Sym.getValue();
ErrorOr<StringRef> Name = Sym.getName();
if (!Name)
continue;
Expand All @@ -218,7 +209,7 @@ void MachODebugMapParser::loadCurrentObjectFileSymbols() {
uint64_t MachODebugMapParser::getMainBinarySymbolAddress(StringRef Name) {
auto Sym = MainBinarySymbolAddresses.find(Name);
if (Sym == MainBinarySymbolAddresses.end())
return UnknownAddress;
return 0;
return Sym->second;
}

Expand All @@ -232,14 +223,14 @@ void MachODebugMapParser::loadMainBinarySymbols() {
// Skip undefined and STAB entries.
if ((Type & SymbolRef::ST_Debug) || (Type & SymbolRef::ST_Unknown))
continue;
uint64_t Addr = Sym.getValue();
// The only symbols of interest are the global variables. These
// are the only ones that need to be queried because the address
// of common data won't be described in the debug map. All other
// addresses should be fetched for the debug map.
if (Addr == UnknownAddress || !(Sym.getFlags() & SymbolRef::SF_Global) ||
Sym.getSection(Section) || Section->isText())
if (!(Sym.getFlags() & SymbolRef::SF_Global) || Sym.getSection(Section) ||
Section == MainBinary.section_end() || Section->isText())
continue;
uint64_t Addr = Sym.getValue();
ErrorOr<StringRef> NameOrErr = Sym.getName();
if (!NameOrErr)
continue;
Expand Down
18 changes: 7 additions & 11 deletions tools/llvm-nm/llvm-nm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -564,12 +564,12 @@ static void sortAndPrintSymbolList(SymbolicFile &Obj, bool printName,
char SymbolAddrStr[18] = "";
char SymbolSizeStr[18] = "";

if (OutputFormat == sysv || I->Address == UnknownAddress)
if (OutputFormat == sysv || I->TypeChar == 'U')
strcpy(SymbolAddrStr, printBlanks);
if (OutputFormat == sysv)
strcpy(SymbolSizeStr, printBlanks);

if (I->Address != UnknownAddress)
if (I->TypeChar != 'U')
format(printFormat, I->Address)
.print(SymbolAddrStr, sizeof(SymbolAddrStr));
format(printFormat, I->Size).print(SymbolSizeStr, sizeof(SymbolSizeStr));
Expand Down Expand Up @@ -881,21 +881,17 @@ static void dumpSymbolNamesFromObject(SymbolicFile &Obj, bool printName,
continue;
NMSymbol S;
S.Size = 0;
S.Address = UnknownAddress;
S.Address = 0;
if (PrintSize) {
if (isa<ELFObjectFileBase>(&Obj))
S.Size = ELFSymbolRef(Sym).getSize();
}
if (PrintAddress && isa<ObjectFile>(Obj)) {
SymbolRef SymRef(Sym);
if (SymFlags & SymbolRef::SF_Common) {
S.Address = SymRef.getCommonSize();
} else {
ErrorOr<uint64_t> AddressOrErr = SymRef.getAddress();
if (error(AddressOrErr.getError()))
break;
S.Address = *AddressOrErr;
}
ErrorOr<uint64_t> AddressOrErr = SymRef.getAddress();
if (error(AddressOrErr.getError()))
break;
S.Address = *AddressOrErr;
}
S.TypeChar = getNMTypeChar(Obj, Sym);
if (error(Sym.printName(OS)))
Expand Down
7 changes: 2 additions & 5 deletions tools/llvm-objdump/MachODump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2413,7 +2413,7 @@ static const char *get_pointer_32(uint32_t Address, uint32_t &offset,
// symbol is passed, look up that address in the info's AddrMap.
static const char *get_symbol_64(uint32_t sect_offset, SectionRef S,
DisassembleInfo *info, uint64_t &n_value,
uint64_t ReferenceValue = UnknownAddress) {
uint64_t ReferenceValue = 0) {
n_value = 0;
if (!info->verbose)
return nullptr;
Expand Down Expand Up @@ -2446,8 +2446,6 @@ static const char *get_symbol_64(uint32_t sect_offset, SectionRef S,
const char *SymbolName = nullptr;
if (reloc_found && isExtern) {
n_value = Symbol.getValue();
if (n_value == UnknownAddress)
n_value = 0;
ErrorOr<StringRef> NameOrError = Symbol.getName();
if (std::error_code EC = NameOrError.getError())
report_fatal_error(EC.message());
Expand All @@ -2469,8 +2467,7 @@ static const char *get_symbol_64(uint32_t sect_offset, SectionRef S,

// We did not find an external relocation entry so look up the ReferenceValue
// as an address of a symbol and if found return that symbol's name.
if (ReferenceValue != UnknownAddress)
SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);

return SymbolName;
}
Expand Down
7 changes: 0 additions & 7 deletions tools/llvm-objdump/llvm-objdump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -825,8 +825,6 @@ static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
if (error(AddressOrErr.getError()))
break;
uint64_t Address = *AddressOrErr;
if (Address == UnknownAddress)
continue;
Address -= SectionAddr;
if (Address >= SectSize)
continue;
Expand Down Expand Up @@ -1136,11 +1134,6 @@ void llvm::PrintSymbolTable(const ObjectFile *o) {
bool Common = Flags & SymbolRef::SF_Common;
bool Hidden = Flags & SymbolRef::SF_Hidden;

if (Common)
Address = Symbol.getCommonSize();

if (Address == UnknownAddress)
Address = 0;
char GlobLoc = ' ';
if (Type != SymbolRef::ST_Unknown)
GlobLoc = Global ? 'g' : 'l';
Expand Down
2 changes: 0 additions & 2 deletions tools/llvm-symbolizer/LLVMSymbolize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ void ModuleInfo::addSymbol(const SymbolRef &Symbol, uint64_t SymbolSize,
if (error(SymbolAddressOrErr.getError()))
return;
uint64_t SymbolAddress = *SymbolAddressOrErr;
if (SymbolAddress == UnknownAddress)
return;
if (OpdExtractor) {
// For big-endian PowerPC64 ELF, symbols in the .opd section refer to
// function descriptors. The first word of the descriptor is a pointer to
Expand Down

0 comments on commit 7b7c81c

Please sign in to comment.