[PATCH] [Object/ELF] Provide symbol helpers

Davide Italiano dccitaliano at gmail.com
Thu Jun 4 17:32:44 PDT 2015


Hi ruiu, rafael, shankar.easwaran,

These were in lld. They can be reused for other tools, e.g. llvm-readobj.
If needed, we can introduce another class as COFF does (i.e. COFFSymboRef).
Note: Hexagon has a different notion of common symbol I'm not entirely sure I understand.
I wasn't able to find documentation either, so any help on this side is greatly appreciated.

REPOSITORY
  rL LLVM

http://reviews.llvm.org/D10264

Files:
  include/llvm/Object/ELF.h

Index: include/llvm/Object/ELF.h
===================================================================
--- include/llvm/Object/ELF.h
+++ include/llvm/Object/ELF.h
@@ -421,6 +421,11 @@
   uint64_t getSymbolIndex(const Elf_Sym *sym) const;
   ErrorOr<ArrayRef<uint8_t> > getSectionContents(const Elf_Shdr *Sec) const;
   StringRef getLoadName() const;
+
+  bool isUndefinedSymbol(const Elf_Sym *Sym) const;
+  bool isAbsoluteSymbol(const Elf_Sym *Sym) const;
+  bool isCommonSymbol(const Elf_Sym *Sym) const;
+  bool isDefinedSymbol(const Elf_Sym *Sym) const;
 };
 
 typedef ELFFile<ELFType<support::little, false>> ELF32LEFile;
@@ -1010,6 +1015,39 @@
   return StringRef(getDynamicString(name_offset));
 }
 
+template <class ELFT>
+bool ELFFile<ELFT>::isUndefinedSymbol(const Elf_Sym *Sym) const {
+  return Sym->st_shndx == llvm::ELF::SHN_UNDEF;
+}
+
+template <class ELFT>
+bool ELFFile<ELFT>::isAbsoluteSymbol(const Elf_Sym *Sym) const {
+  return Sym->st_shndx == llvm::ELF::SHN_ABS;
+}
+
+template <class ELFT>
+bool ELFFile<ELFT>::isCommonSymbol(const Elf_Sym *Sym) const {
+  return Sym->getType() == llvm::ELF::STT_COMMON ||
+         Sym->st_shndx == llvm::ELF::SHN_COMMON ||
+         /* Hexagon-specific */
+         Sym->st_shndx == llvm::ELF::SHN_HEXAGON_SCOMMON ||
+         Sym->st_shndx == llvm::ELF::SHN_HEXAGON_SCOMMON_1 ||
+         Sym->st_shndx == llvm::ELF::SHN_HEXAGON_SCOMMON_2 ||
+         Sym->st_shndx == llvm::ELF::SHN_HEXAGON_SCOMMON_4 ||
+         Sym->st_shndx == llvm::ELF::SHN_HEXAGON_SCOMMON_8;
+}
+
+template <class ELFT>
+bool ELFFile<ELFT>::isDefinedSymbol(const Elf_Sym *Sym) const {
+  return Sym->getType() == llvm::ELF::STT_NOTYPE ||
+         Sym->getType() == llvm::ELF::STT_OBJECT ||
+         Sym->getType() == llvm::ELF::STT_FUNC ||
+         Sym->getType() == llvm::ELF::STT_GNU_IFUNC ||
+         Sym->getType() == llvm::ELF::STT_SECTION ||
+         Sym->getType() == llvm::ELF::STT_FILE ||
+         Sym->getType() == llvm::ELF::STT_TLS;
+}
+
 /// This function returns the hash value for a symbol in the .dynsym section
 /// Name of the API remains consistent as specified in the libelf
 /// REF : http://www.sco.com/developers/gabi/latest/ch5.dynamic.html#hash

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D10264.27166.patch
Type: text/x-patch
Size: 2210 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150605/85bcc5ee/attachment.bin>


More information about the llvm-commits mailing list