StringRef &Res) const override;
std::error_code getSymbolAddress(DataRefImpl Symb,
uint64_t &Res) const override;
- uint64_t getSymbolSize(DataRefImpl Symb) const override;
+ uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
uint32_t getSymbolFlags(DataRefImpl Symb) const override;
std::error_code getSymbolType(DataRefImpl Symb,
SymbolRef::Type &Res) const override;
virtual uint64_t getSectionFlags(SectionRef Sec) const = 0;
virtual uint32_t getSectionType(SectionRef Sec) const = 0;
+ virtual uint64_t getSymbolSize(SymbolRef Symb) const = 0;
+
static inline bool classof(const Binary *v) { return v->isELF(); }
};
typedef typename ELFFile<ELFT>::Elf_Shdr_Iter Elf_Shdr_Iter;
typedef typename ELFFile<ELFT>::Elf_Dyn_Iter Elf_Dyn_Iter;
+ uint64_t getSymbolSize(SymbolRef Symb) const override;
+
protected:
ELFFile<ELFT> EF;
std::error_code getSymbolAddress(DataRefImpl Symb,
uint64_t &Res) const override;
uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
- uint64_t getSymbolSize(DataRefImpl Symb) const override;
+ uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
uint32_t getSymbolFlags(DataRefImpl Symb) const override;
std::error_code getSymbolOther(DataRefImpl Symb, uint8_t &Res) const override;
std::error_code getSymbolType(DataRefImpl Symb,
switch (EF.getSymbolTableIndex(ESym)) {
case ELF::SHN_COMMON:
case ELF::SHN_UNDEF:
- Result = UnknownAddressOrSize;
+ Result = UnknownAddress;
return std::error_code();
case ELF::SHN_ABS:
Result = ESym->st_value;
}
template <class ELFT>
-uint64_t ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Symb) const {
+uint64_t ELFObjectFile<ELFT>::getSymbolSize(SymbolRef Symb) const {
+ return toELFSymIter(Symb.getRawDataRefImpl())->st_size;
+}
+
+template <class ELFT>
+uint64_t ELFObjectFile<ELFT>::getCommonSymbolSizeImpl(DataRefImpl Symb) const {
return toELFSymIter(Symb)->st_size;
}
std::error_code getSymbolAddress(DataRefImpl Symb,
uint64_t &Res) const override;
uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
- uint64_t getSymbolSize(DataRefImpl Symb) const override;
+ uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
std::error_code getSymbolType(DataRefImpl Symb,
SymbolRef::Type &Res) const override;
uint32_t getSymbolFlags(DataRefImpl Symb) const override;
std::error_code getAddress(uint64_t &Result) const;
/// @brief Get the alignment of this symbol as the actual value (not log 2).
uint32_t getAlignment() const;
- uint64_t getSize() const;
+ uint64_t getCommonSize() const;
std::error_code getType(SymbolRef::Type &Result) const;
std::error_code getOther(uint8_t &Result) const;
virtual std::error_code getSymbolAddress(DataRefImpl Symb,
uint64_t &Res) const = 0;
virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const;
- virtual uint64_t getSymbolSize(DataRefImpl Symb) const = 0;
+ virtual uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const = 0;
virtual std::error_code getSymbolType(DataRefImpl Symb,
SymbolRef::Type &Res) const = 0;
virtual std::error_code getSymbolSection(DataRefImpl Symb,
}
public:
+ uint64_t getCommonSymbolSize(DataRefImpl Symb) const {
+ assert(getSymbolFlags(Symb) & SymbolRef::SF_Common);
+ return getCommonSymbolSizeImpl(Symb);
+ }
+
typedef iterator_range<symbol_iterator> symbol_iterator_range;
symbol_iterator_range symbols() const {
return symbol_iterator_range(symbol_begin(), symbol_end());
return getObject()->getSymbolAlignment(getRawDataRefImpl());
}
-inline uint64_t SymbolRef::getSize() const {
- return getObject()->getSymbolSize(getRawDataRefImpl());
+inline uint64_t SymbolRef::getCommonSize() const {
+ return getObject()->getCommonSymbolSize(getRawDataRefImpl());
}
inline std::error_code SymbolRef::getSection(section_iterator &Result) const {
typedef content_iterator<BasicSymbolRef> basic_symbol_iterator;
-const uint64_t UnknownAddressOrSize = ~0ULL;
+const uint64_t UnknownAddress = ~0ULL;
class SymbolicFile : public Binary {
public:
if (std::error_code EC = Sym.getAddress(Address))
return EC;
- if (Address == UnknownAddressOrSize) {
- Result = UnknownAddressOrSize;
+ if (Address == UnknownAddress) {
+ Result = UnknownAddress;
return std::error_code();
}
return EC;
if (SecI == Obj->section_end()) {
- Result = UnknownAddressOrSize;
+ Result = UnknownAddress;
return std::error_code();
}
uint32_t Flags = I->getFlags();
if (Flags & SymbolRef::SF_Common) {
// Add the common symbols to a list. We'll allocate them all below.
- uint64_t Size = I->getSize();
+ uint64_t Size = I->getCommonSize();
CommonSize += Size;
}
}
}
uint32_t Align = Sym.getAlignment();
- uint64_t Size = Sym.getSize();
+ uint64_t Size = Sym.getCommonSize();
CommonSize += Align + Size;
SymbolsToAllocate.push_back(Sym);
for (auto &Sym : SymbolsToAllocate) {
uint32_t Align = Sym.getAlignment();
StringRef Name;
- uint64_t Size = Sym.getSize();
+ uint64_t Size = Sym.getCommonSize();
Check(Sym.getName(Name));
if (Align) {
// This symbol has an alignment requirement.
uint64_t RuntimeDyldCOFF::getSymbolOffset(const SymbolRef &Sym) {
uint64_t Address;
if (Sym.getAddress(Address))
- return UnknownAddressOrSize;
+ return UnknownAddress;
- if (Address == UnknownAddressOrSize)
- return UnknownAddressOrSize;
+ if (Address == UnknownAddress)
+ return UnknownAddress;
const ObjectFile *Obj = Sym.getObject();
section_iterator SecI(Obj->section_end());
if (Sym.getSection(SecI))
- return UnknownAddressOrSize;
+ return UnknownAddress;
if (SecI == Obj->section_end())
- return UnknownAddressOrSize;
+ return UnknownAddress;
uint64_t SectionAddress = SecI->getAddress();
return Address - SectionAddress;
COFFSymbolRef Symb = getCOFFSymbol(Ref);
if (Symb.isAnyUndefined()) {
- Result = UnknownAddressOrSize;
+ Result = UnknownAddress;
return std::error_code();
}
if (Symb.isCommon()) {
- Result = UnknownAddressOrSize;
+ Result = UnknownAddress;
return std::error_code();
}
int32_t SectionNumber = Symb.getSectionNumber();
return Result;
}
-uint64_t COFFObjectFile::getSymbolSize(DataRefImpl Ref) const {
+uint64_t COFFObjectFile::getCommonSymbolSizeImpl(DataRefImpl Ref) const {
COFFSymbolRef Symb = getCOFFSymbol(Ref);
-
- if (Symb.isCommon())
- return Symb.getValue();
- return UnknownAddressOrSize;
+ return Symb.getValue();
}
std::error_code
MachO::nlist_64 Entry = getSymbol64TableEntry(Symb);
if ((Entry.n_type & MachO::N_TYPE) == MachO::N_UNDF &&
Entry.n_value == 0)
- Res = UnknownAddressOrSize;
+ Res = UnknownAddress;
else
Res = Entry.n_value;
} else {
MachO::nlist Entry = getSymbolTableEntry(Symb);
if ((Entry.n_type & MachO::N_TYPE) == MachO::N_UNDF &&
Entry.n_value == 0)
- Res = UnknownAddressOrSize;
+ Res = UnknownAddress;
else
Res = Entry.n_value;
}
return 0;
}
-uint64_t MachOObjectFile::getSymbolSize(DataRefImpl DRI) const {
+uint64_t MachOObjectFile::getCommonSymbolSizeImpl(DataRefImpl DRI) const {
uint64_t Value;
getSymbolAddress(DRI, Value);
- uint32_t flags = getSymbolFlags(DRI);
- if (flags & SymbolRef::SF_Common)
- return Value;
- return UnknownAddressOrSize;
+ return Value;
}
std::error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF) {
uint64_t Value;
getSymbolAddress(DRI, Value);
- if (Value && Value != UnknownAddressOrSize)
+ if (Value && Value != UnknownAddress)
Result |= SymbolRef::SF_Common;
}
}
uint64_t LLVMGetSymbolSize(LLVMSymbolIteratorRef SI) {
- return (*unwrap(SI))->getSize();
+ return (*unwrap(SI))->getCommonSize();
}
// RelocationRef accessors
llvm::object::computeSymbolSizes(const ObjectFile &O) {
std::vector<std::pair<SymbolRef, uint64_t>> Ret;
- if (isa<ELFObjectFileBase>(&O)) {
- for (SymbolRef Sym : O.symbols()) {
- Ret.push_back({Sym, Sym.getSize()});
- }
+ if (const auto *E = dyn_cast<ELFObjectFileBase>(&O)) {
+ for (SymbolRef Sym : E->symbols())
+ Ret.push_back({Sym, E->getSymbolSize(Sym)});
return Ret;
}
StringRef SymName; SymI->getName(SymName);
uint64_t SymAddr; SymI->getAddress(SymAddr);
- uint64_t SymSize = SymI->getSize();
auto *Obj = cast<ELFObjectFileBase>(Rel.getObjectFile());
+ uint64_t SymSize = Obj->getSymbolSize(*SymI);
int64_t Addend = *Obj->getRelocationAddend(Rel.getRawDataRefImpl());
MCSymbol *Sym = Ctx.getOrCreateSymbol(SymName);
--- /dev/null
+// RUN: llvm-mc %s -o %t -filetype=obj -triple=x86_64-pc-linux
+// RUN: llvm-nm --print-size %t | FileCheck %s
+
+// CHECK: 0000000000000000 ffffffffffffffff t a
+
+a:
+ .size a, 0xffffffffffffffff
-config.suffixes = ['.test', '.ll', '.yaml']
+config.suffixes = ['.test', '.ll', '.s', '.yaml']
// symbol table to find its address as it might not be in the
// debug map (for common symbols).
Value = getMainBinarySymbolAddress(Name);
- if (Value == UnknownAddressOrSize)
+ if (Value == UnknownAddress)
return;
break;
case MachO::N_FUN:
for (auto Sym : CurrentObjectHolder.Get().symbols()) {
StringRef Name;
uint64_t Addr;
- if (Sym.getAddress(Addr) || Addr == UnknownAddressOrSize ||
- Sym.getName(Name))
+ if (Sym.getAddress(Addr) || Addr == UnknownAddress || Sym.getName(Name))
continue;
CurrentObjectAddresses[Name] = Addr;
}
uint64_t MachODebugMapParser::getMainBinarySymbolAddress(StringRef Name) {
auto Sym = MainBinarySymbolAddresses.find(Name);
if (Sym == MainBinarySymbolAddresses.end())
- return UnknownAddressOrSize;
+ return UnknownAddress;
return Sym->second;
}
// 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 (Sym.getAddress(Addr) || Addr == UnknownAddressOrSize ||
+ if (Sym.getAddress(Addr) || Addr == UnknownAddress ||
!(Sym.getFlags() & SymbolRef::SF_Global) || Sym.getSection(Section) ||
Section->isText() || Sym.getName(Name) || Name.size() == 0 ||
Name[0] == '\0')
continue;
if ((I->TypeChar == 'U') && DefinedOnly)
continue;
- if (SizeSort && !PrintAddress && I->Size == UnknownAddressOrSize)
+ if (SizeSort && !PrintAddress)
continue;
if (PrintFileName) {
if (!ArchitectureName.empty())
char SymbolAddrStr[18] = "";
char SymbolSizeStr[18] = "";
- if (OutputFormat == sysv || I->Address == UnknownAddressOrSize)
+ if (OutputFormat == sysv || I->Address == UnknownAddress)
strcpy(SymbolAddrStr, printBlanks);
if (OutputFormat == sysv)
strcpy(SymbolSizeStr, printBlanks);
- if (I->Address != UnknownAddressOrSize)
+ if (I->Address != UnknownAddress)
format(printFormat, I->Address)
.print(SymbolAddrStr, sizeof(SymbolAddrStr));
- if (I->Size != UnknownAddressOrSize)
- format(printFormat, I->Size).print(SymbolSizeStr, sizeof(SymbolSizeStr));
+ format(printFormat, I->Size).print(SymbolSizeStr, sizeof(SymbolSizeStr));
// If OutputFormat is darwin or we are printing Mach-O symbols in hex and
// we have a MachOObjectFile, call darwinPrintSymbol to print as darwin's
outs() << SymbolAddrStr << ' ';
if (PrintSize) {
outs() << SymbolSizeStr;
- if (I->Size != UnknownAddressOrSize)
- outs() << ' ';
+ outs() << ' ';
}
outs() << I->TypeChar;
if (I->TypeChar == '-' && MachO)
if (Nsect && Nsect != getNsectInMachO(*MachO, I))
continue;
NMSymbol S;
- S.Size = UnknownAddressOrSize;
- S.Address = UnknownAddressOrSize;
- if (PrintSize && isa<ELFObjectFileBase>(Obj)) {
- symbol_iterator SymI = I;
- S.Size = SymI->getSize();
+ S.Size = 0;
+ S.Address = UnknownAddress;
+ if (PrintSize) {
+ if (auto *E = dyn_cast<ELFObjectFileBase>(&Obj)) {
+ symbol_iterator SymI = I;
+ S.Size = E->getSymbolSize(*SymI);
+ }
}
if (PrintAddress && isa<ObjectFile>(Obj))
if (error(symbol_iterator(I)->getAddress(S.Address)))
// for the specified section offset in the specified section reference.
// If no relocation information is found and a non-zero ReferenceValue for the
// 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 = UnknownAddressOrSize) {
+static const char *get_symbol_64(uint32_t sect_offset, SectionRef S,
+ DisassembleInfo *info, uint64_t &n_value,
+ uint64_t ReferenceValue = UnknownAddress) {
n_value = 0;
if (!info->verbose)
return nullptr;
const char *SymbolName = nullptr;
if (reloc_found && isExtern) {
Symbol.getAddress(n_value);
- if (n_value == UnknownAddressOrSize)
+ if (n_value == UnknownAddress)
n_value = 0;
StringRef name;
Symbol.getName(name);
// 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 != UnknownAddressOrSize)
+ if (ReferenceValue != UnknownAddress)
SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
return SymbolName;
uint64_t Address;
if (error(Symbol.getAddress(Address)))
break;
- if (Address == UnknownAddressOrSize)
+ if (Address == UnknownAddress)
continue;
Address -= SectionAddr;
if (Address >= SectSize)
bool Hidden = Flags & SymbolRef::SF_Hidden;
if (Common)
- Address = Symbol.getSize();
+ Address = Symbol.getCommonSize();
- if (Address == UnknownAddressOrSize)
+ if (Address == UnknownAddress)
Address = 0;
char GlobLoc = ' ';
if (Type != SymbolRef::ST_Unknown)
outs() << '\t';
if (Common || isa<ELFObjectFileBase>(o)) {
- uint64_t Val = Common ? Symbol.getAlignment() : Symbol.getSize();
+ uint64_t Val = Common ? Symbol.getAlignment()
+ : cast<ELFObjectFileBase>(o)->getSymbolSize(Symbol);
outs() << format("\t %08" PRIx64 " ", Val);
}
return;
uint64_t SymbolAddress;
if (error(Symbol.getAddress(SymbolAddress)) ||
- SymbolAddress == UnknownAddressOrSize)
+ SymbolAddress == UnknownAddress)
return;
if (OpdExtractor) {
// For big-endian PowerPC64 ELF, symbols in the .opd section refer to
SymbolAddress = OpdExtractor->getAddress(&OpdOffset32);
}
uint64_t SymbolSize;
- // Getting symbol size is linear for Mach-O files, so assume that symbol
- // occupies the memory range up to the following symbol.
- if (isa<MachOObjectFile>(Module))
+ // Onyl ELF has a size for every symbol so assume that symbol occupies the
+ // memory range up to the following symbol.
+ if (auto *E = dyn_cast<ELFObjectFileBase>(Module))
+ SymbolSize = E->getSymbolSize(Symbol);
+ else
SymbolSize = 0;
- else {
- SymbolSize = Symbol.getSize();
- if (SymbolSize == UnknownAddressOrSize)
- return;
- }
StringRef SymbolName;
if (error(Symbol.getName(SymbolName)))
return;