llvm.org GIT mirror llvm / b24bbb7
Add an ELFSymbolRef type. This allows user code to say Sym.getSize() instead of having to manually fetch the object. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@240708 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 5 years ago
5 changed file(s) with 63 addition(s) and 20 deletion(s). Raw diff Collapse all Expand all
3434 namespace llvm {
3535 namespace object {
3636
37 class elf_symbol_iterator;
38 class ELFSymbolRef;
39
3740 class ELFObjectFileBase : public ObjectFile {
41 friend class ELFSymbolRef;
42
3843 protected:
3944 ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source);
45
46 virtual uint64_t getSymbolSize(DataRefImpl Symb) const = 0;
4047
4148 public:
4249 virtual ErrorOr getRelocationAddend(DataRefImpl Rel) const = 0;
4552 // and addend or not.
4653 virtual bool hasRelocationAddend(DataRefImpl Rel) const = 0;
4754
48 virtual symbol_iterator_range getDynamicSymbolIterators() const = 0;
55 typedef iterator_range elf_symbol_iterator_range;
56 virtual elf_symbol_iterator_range getDynamicSymbolIterators() const = 0;
4957
5058 virtual uint64_t getSectionFlags(SectionRef Sec) const = 0;
5159 virtual uint32_t getSectionType(SectionRef Sec) const = 0;
5260
53 virtual uint64_t getSymbolSize(SymbolRef Symb) const = 0;
61 elf_symbol_iterator_range symbols() const;
5462
5563 static inline bool classof(const Binary *v) { return v->isELF(); }
5664 };
5765
66 class ELFSymbolRef : public SymbolRef {
67 public:
68 ELFSymbolRef(const SymbolRef &B) : SymbolRef(B) {
69 assert(isa(SymbolRef::getObject()));
70 }
71
72 const ELFObjectFileBase *getObject() const {
73 return cast(BasicSymbolRef::getObject());
74 }
75
76 uint64_t getSize() const {
77 return getObject()->getSymbolSize(getRawDataRefImpl());
78 }
79 };
80
81 class elf_symbol_iterator : public symbol_iterator {
82 public:
83 elf_symbol_iterator(const basic_symbol_iterator &B)
84 : symbol_iterator(SymbolRef(B->getRawDataRefImpl(),
85 cast(B->getObject()))) {}
86
87 const ELFSymbolRef *operator->() const {
88 return static_cast(symbol_iterator::operator->());
89 }
90
91 const ELFSymbolRef &operator*() const {
92 return static_cast(symbol_iterator::operator*());
93 }
94 };
95
96 inline ELFObjectFileBase::elf_symbol_iterator_range
97 ELFObjectFileBase::symbols() const {
98 return elf_symbol_iterator_range(symbol_begin(), symbol_end());
99 }
100
58101 template class ELFObjectFile : public ELFObjectFileBase {
102 uint64_t getSymbolSize(DataRefImpl Sym) const override;
103
59104 public:
60105 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
61106
71116 typedef typename ELFFile::Elf_Sym_Iter Elf_Sym_Iter;
72117 typedef typename ELFFile::Elf_Shdr_Iter Elf_Shdr_Iter;
73118 typedef typename ELFFile::Elf_Dyn_Iter Elf_Dyn_Iter;
74
75 uint64_t getSymbolSize(SymbolRef Symb) const override;
76119
77120 protected:
78121 ELFFile EF;
205248 basic_symbol_iterator symbol_begin_impl() const override;
206249 basic_symbol_iterator symbol_end_impl() const override;
207250
208 symbol_iterator dynamic_symbol_begin() const;
209 symbol_iterator dynamic_symbol_end() const;
251 elf_symbol_iterator dynamic_symbol_begin() const;
252 elf_symbol_iterator dynamic_symbol_end() const;
210253
211254 section_iterator section_begin() const override;
212255 section_iterator section_end() const override;
235278 ELFT::Is64Bits);
236279 }
237280
238 symbol_iterator_range getDynamicSymbolIterators() const override;
281 elf_symbol_iterator_range getDynamicSymbolIterators() const override;
239282
240283 bool isRelocatableObject() const override;
241284 };
326369 }
327370
328371 template
329 uint64_t ELFObjectFile::getSymbolSize(SymbolRef Symb) const {
330 return toELFSymIter(Symb.getRawDataRefImpl())->st_size;
372 uint64_t ELFObjectFile::getSymbolSize(DataRefImpl Sym) const {
373 return toELFSymIter(Sym)->st_size;
331374 }
332375
333376 template
733776 }
734777
735778 template
736 symbol_iterator ELFObjectFile::dynamic_symbol_begin() const {
779 elf_symbol_iterator ELFObjectFile::dynamic_symbol_begin() const {
737780 return symbol_iterator(SymbolRef(toDRI(EF.begin_dynamic_symbols()), this));
738781 }
739782
740783 template
741 symbol_iterator ELFObjectFile::dynamic_symbol_end() const {
784 elf_symbol_iterator ELFObjectFile::dynamic_symbol_end() const {
742785 return symbol_iterator(SymbolRef(toDRI(EF.end_dynamic_symbols()), this));
743786 }
744787
861904 }
862905
863906 template
864 ObjectFile::symbol_iterator_range
907 ELFObjectFileBase::elf_symbol_iterator_range
865908 ELFObjectFile::getDynamicSymbolIterators() const {
866909 return make_range(dynamic_symbol_begin(), dynamic_symbol_end());
867910 }
5454 auto Syms = E->symbols();
5555 if (Syms.begin() == Syms.end())
5656 Syms = E->getDynamicSymbolIterators();
57 for (SymbolRef Sym : Syms)
58 Ret.push_back({Sym, E->getSymbolSize(Sym)});
57 for (ELFSymbolRef Sym : Syms)
58 Ret.push_back({Sym, Sym.getSize()});
5959 return Ret;
6060 }
6161
2626
2727 const MCExpr *createExprForRelocation(RelocationRef Rel) override {
2828 uint64_t RelType; Rel.getType(RelType);
29 symbol_iterator SymI = Rel.getSymbol();
29 elf_symbol_iterator SymI = Rel.getSymbol();
3030
3131 StringRef SymName; SymI->getName(SymName);
3232 uint64_t SymAddr; SymI->getAddress(SymAddr);
3333 auto *Obj = cast(Rel.getObjectFile());
34 uint64_t SymSize = Obj->getSymbolSize(*SymI);
34 uint64_t SymSize = SymI->getSize();
3535 int64_t Addend = *Obj->getRelocationAddend(Rel.getRawDataRefImpl());
3636
3737 MCSymbol *Sym = Ctx.getOrCreateSymbol(SymName);
929929 S.Size = 0;
930930 S.Address = UnknownAddress;
931931 if (PrintSize) {
932 if (auto *E = dyn_cast(&Obj))
933 S.Size = E->getSymbolSize(Sym);
932 if (isa(&Obj))
933 S.Size = ELFSymbolRef(Sym).getSize();
934934 }
935935 if (PrintAddress && isa(Obj)) {
936936 if (error(SymbolRef(Sym).getAddress(S.Address)))
11481148
11491149 outs() << '\t';
11501150 if (Common || isa(o)) {
1151 uint64_t Val = Common ? Symbol.getAlignment()
1152 : cast(o)->getSymbolSize(Symbol);
1151 uint64_t Val =
1152 Common ? Symbol.getAlignment() : ELFSymbolRef(Symbol).getSize();
11531153 outs() << format("\t %08" PRIx64 " ", Val);
11541154 }
11551155