diff options
Diffstat (limited to 'src/common/dwarf/elf_reader.cc')
-rw-r--r-- | src/common/dwarf/elf_reader.cc | 150 |
1 files changed, 75 insertions, 75 deletions
diff --git a/src/common/dwarf/elf_reader.cc b/src/common/dwarf/elf_reader.cc index 1b665213..bbfdba68 100644 --- a/src/common/dwarf/elf_reader.cc +++ b/src/common/dwarf/elf_reader.cc @@ -130,11 +130,11 @@ class Elf32 { static const int kElfClass = ELFCLASS32; // Given a symbol pointer, return the binding type (eg STB_WEAK). - static char Bind(const Elf32_Sym *sym) { + static char Bind(const Elf32_Sym* sym) { return ELF32_ST_BIND(sym->st_info); } // Given a symbol pointer, return the symbol type (eg STT_FUNC). - static char Type(const Elf32_Sym *sym) { + static char Type(const Elf32_Sym* sym) { return ELF32_ST_TYPE(sym->st_info); } @@ -158,10 +158,10 @@ class Elf64 { // What should be in the EI_CLASS header. static const int kElfClass = ELFCLASS64; - static char Bind(const Elf64_Sym *sym) { + static char Bind(const Elf64_Sym* sym) { return ELF64_ST_BIND(sym->st_info); } - static char Type(const Elf64_Sym *sym) { + static char Type(const Elf64_Sym* sym) { return ELF64_ST_TYPE(sym->st_info); } static int r_sym(const Elf64_Xword r_info) { @@ -182,8 +182,8 @@ class Elf64 { template<class ElfArch> class ElfSectionReader { public: - ElfSectionReader(const char *name, const string &path, int fd, - const typename ElfArch::Shdr §ion_header) + ElfSectionReader(const char* name, const string& path, int fd, + const typename ElfArch::Shdr& section_header) : contents_aligned_(NULL), contents_(NULL), header_(section_header) { @@ -199,7 +199,7 @@ class ElfSectionReader { contents_aligned_ = mmap(NULL, size_aligned_, PROT_READ, MAP_SHARED, fd, offset_aligned); // Set where the offset really should begin. - contents_ = reinterpret_cast<char *>(contents_aligned_) + + contents_ = reinterpret_cast<char*>(contents_aligned_) + (header_.sh_offset - offset_aligned); // Check for and handle any compressed contents. @@ -217,24 +217,24 @@ class ElfSectionReader { } // Return the section header for this section. - typename ElfArch::Shdr const &header() const { return header_; } + typename ElfArch::Shdr const& header() const { return header_; } // Return memory at the given offset within this section. - const char *GetOffset(typename ElfArch::Word bytes) const { + const char* GetOffset(typename ElfArch::Word bytes) const { return contents_ + bytes; } - const char *contents() const { return contents_; } + const char* contents() const { return contents_; } size_t section_size() const { return section_size_; } private: // page-aligned file contents - void *contents_aligned_; + void* contents_aligned_; // contents as usable by the client. For non-compressed sections, // pointer within contents_aligned_ to where the section data // begins; for compressed sections, pointer to the decompressed // data. - char *contents_; + char* contents_; // size of contents_aligned_ size_t size_aligned_; // size of contents. @@ -249,7 +249,7 @@ class ElfSectionReader { template<class ElfArch> class SymbolIterator { public: - SymbolIterator(ElfReaderImpl<ElfArch> *reader, + SymbolIterator(ElfReaderImpl<ElfArch>* reader, typename ElfArch::Word section_type) : symbol_section_(reader->GetSectionByType(section_type)), string_section_(NULL), @@ -280,7 +280,7 @@ class SymbolIterator { // Return a pointer to the current symbol. // REQUIRES: !done() - const typename ElfArch::Sym *GetSymbol() const { + const typename ElfArch::Sym* GetSymbol() const { return reinterpret_cast<const typename ElfArch::Sym*>( symbol_section_->GetOffset(symbol_within_section_ * symbol_section_->header().sh_entsize)); @@ -288,7 +288,7 @@ class SymbolIterator { // Return the name of the current symbol, NULL if it has none. // REQUIRES: !done() - const char *GetSymbolName() const { + const char* GetSymbolName() const { int name_offset = GetSymbol()->st_name; if (name_offset == 0) return NULL; @@ -300,8 +300,8 @@ class SymbolIterator { } private: - const ElfSectionReader<ElfArch> *const symbol_section_; - const ElfSectionReader<ElfArch> *string_section_; + const ElfSectionReader<ElfArch>* const symbol_section_; + const ElfSectionReader<ElfArch>* string_section_; int num_symbols_in_section_; int symbol_within_section_; }; @@ -326,7 +326,7 @@ static inline bool MyHasSuffixString(const string& str, const string& suffix) { template<class ElfArch> class ElfReaderImpl { public: - explicit ElfReaderImpl(const string &path, int fd) + explicit ElfReaderImpl(const string& path, int fd) : path_(path), fd_(fd), section_headers_(NULL), @@ -347,7 +347,7 @@ class ElfReaderImpl { // "opd_section_" must always be checked for NULL before use. opd_section_ = GetSectionInfoByName(".opd", &opd_info_); for (unsigned int k = 0u; k < GetNumSections(); ++k) { - const char *name = GetSectionName(section_headers_[k].sh_name); + const char* name = GetSectionName(section_headers_[k].sh_name); if (strncmp(name, ".text", strlen(".text")) == 0) { base_for_text_ = section_headers_[k].sh_addr - section_headers_[k].sh_offset; @@ -384,7 +384,7 @@ class ElfReaderImpl { // to see if the ELF file appears to match the current // architecture. If error is non-NULL, it will be set with a reason // in case of failure. - static bool IsArchElfFile(int fd, string *error) { + static bool IsArchElfFile(int fd, string* error) { unsigned char header[EI_NIDENT]; if (pread(fd, header, sizeof(header), 0) != sizeof(header)) { if (error != NULL) *error = "Could not read header"; @@ -415,7 +415,7 @@ class ElfReaderImpl { } // Return true if we can use this symbol in Address-to-Symbol map. - bool CanUseSymbol(const char *name, const typename ElfArch::Sym *sym) { + bool CanUseSymbol(const char* name, const typename ElfArch::Sym* sym) { // For now we only save FUNC and NOTYPE symbols. For now we just // care about functions, but some functions written in assembler // don't have a proper ELF type attached to them, so we store @@ -444,7 +444,7 @@ class ElfReaderImpl { // Iterate over the symbols in a section, either SHT_DYNSYM or // SHT_SYMTAB. Add all symbols to the given SymbolMap. /* - void GetSymbolPositions(SymbolMap *symbols, + void GetSymbolPositions(SymbolMap* symbols, typename ElfArch::Word section_type, uint64_t mem_offset, uint64_t file_offset) { @@ -453,10 +453,10 @@ class ElfReaderImpl { AddrToSymMap addr_to_sym_map; for (SymbolIterator<ElfArch> it(this, section_type); !it.done(); it.Next()) { - const char *name = it.GetSymbolName(); + const char* name = it.GetSymbolName(); if (name == NULL) continue; - const typename ElfArch::Sym *sym = it.GetSymbol(); + const typename ElfArch::Sym* sym = it.GetSymbol(); if (CanUseSymbol(name, sym)) { const int sec = sym->st_shndx; @@ -519,9 +519,9 @@ class ElfReaderImpl { if (addr_to_sym_map.empty()) { return; } - const ElfSectionReader<ElfArch> *const symbol_section = + const ElfSectionReader<ElfArch>* const symbol_section = this->GetSectionByType(section_type); - const ElfSectionReader<ElfArch> *const string_section = + const ElfSectionReader<ElfArch>* const string_section = this->GetSection(symbol_section->header().sh_link); typename AddrToSymMap::iterator curr = addr_to_sym_map.begin(); @@ -532,8 +532,8 @@ class ElfReaderImpl { for (; curr != addr_to_sym_map.end(); ++curr) { const uint64_t prev_addr = prev->first; const uint64_t curr_addr = curr->first; - const typename ElfArch::Sym *const prev_sym = prev->second; - const typename ElfArch::Sym *const curr_sym = curr->second; + const typename ElfArch::Sym* const prev_sym = prev->second; + const typename ElfArch::Sym* const curr_sym = curr->second; if (prev_addr + prev_sym->st_size <= curr_addr || // The next condition is true if two symbols overlap like this: // @@ -552,7 +552,7 @@ class ElfReaderImpl { // (e.g. 0619e071) will produce the current symbol, // which is the desired outcome. prev_addr + prev_sym->st_size < curr_addr + curr_sym->st_size) { - const char *name = string_section->GetOffset(curr_sym->st_name); + const char* name = string_section->GetOffset(curr_sym->st_name); symbols->AddSymbol(name, curr_addr, curr_sym->st_size); prev = curr; } else { @@ -572,20 +572,20 @@ class ElfReaderImpl { */ void VisitSymbols(typename ElfArch::Word section_type, - ElfReader::SymbolSink *sink) { + ElfReader::SymbolSink* sink) { VisitSymbols(section_type, sink, -1, -1, false); } void VisitSymbols(typename ElfArch::Word section_type, - ElfReader::SymbolSink *sink, + ElfReader::SymbolSink* sink, int symbol_binding, int symbol_type, bool get_raw_symbol_values) { for (SymbolIterator<ElfArch> it(this, section_type); !it.done(); it.Next()) { - const char *name = it.GetSymbolName(); + const char* name = it.GetSymbolName(); if (!name) continue; - const typename ElfArch::Sym *sym = it.GetSymbol(); + const typename ElfArch::Sym* sym = it.GetSymbol(); if ((symbol_binding < 0 || ElfArch::Bind(sym) == symbol_binding) && (symbol_type < 0 || ElfArch::Type(sym) == symbol_type)) { typename ElfArch::Sym symbol = *sym; @@ -691,7 +691,7 @@ class ElfReaderImpl { // Return an ElfSectionReader for the first section of the given // type by iterating through all section headers. Returns NULL if // the section type is not found. - const ElfSectionReader<ElfArch> *GetSectionByType( + const ElfSectionReader<ElfArch>* GetSectionByType( typename ElfArch::Word section_type) { for (unsigned int k = 0u; k < GetNumSections(); ++k) { if (section_headers_[k].sh_type == section_type) { @@ -703,14 +703,14 @@ class ElfReaderImpl { // Return the name of section "shndx". Returns NULL if the section // is not found. - const char *GetSectionNameByIndex(int shndx) { + const char* GetSectionNameByIndex(int shndx) { return GetSectionName(section_headers_[shndx].sh_name); } // Return a pointer to section "shndx", and store the size in // "size". Returns NULL if the section is not found. - const char *GetSectionContentsByIndex(int shndx, size_t *size) { - const ElfSectionReader<ElfArch> *section = GetSection(shndx); + const char* GetSectionContentsByIndex(int shndx, size_t* size) { + const ElfSectionReader<ElfArch>* section = GetSection(shndx); if (section != NULL) { *size = section->section_size(); return section->contents(); @@ -721,16 +721,16 @@ class ElfReaderImpl { // Return a pointer to the first section of the given name by // iterating through all section headers, and store the size in // "size". Returns NULL if the section name is not found. - const char *GetSectionContentsByName(const string §ion_name, - size_t *size) { + const char* GetSectionContentsByName(const string& section_name, + size_t* size) { for (unsigned int k = 0u; k < GetNumSections(); ++k) { // When searching for sections in a .dwp file, the sections // we're looking for will always be at the end of the section // table, so reverse the direction of iteration. int shndx = is_dwp_ ? GetNumSections() - k - 1 : k; - const char *name = GetSectionName(section_headers_[shndx].sh_name); + const char* name = GetSectionName(section_headers_[shndx].sh_name); if (name != NULL && ElfReader::SectionNamesMatch(section_name, name)) { - const ElfSectionReader<ElfArch> *section = GetSection(shndx); + const ElfSectionReader<ElfArch>* section = GetSection(shndx); if (section == NULL) { return NULL; } else { @@ -744,16 +744,16 @@ class ElfReaderImpl { // This is like GetSectionContentsByName() but it returns a lot of extra // information about the section. - const char *GetSectionInfoByName(const string §ion_name, - ElfReader::SectionInfo *info) { + const char* GetSectionInfoByName(const string& section_name, + ElfReader::SectionInfo* info) { for (unsigned int k = 0u; k < GetNumSections(); ++k) { // When searching for sections in a .dwp file, the sections // we're looking for will always be at the end of the section // table, so reverse the direction of iteration. int shndx = is_dwp_ ? GetNumSections() - k - 1 : k; - const char *name = GetSectionName(section_headers_[shndx].sh_name); + const char* name = GetSectionName(section_headers_[shndx].sh_name); if (name != NULL && ElfReader::SectionNamesMatch(section_name, name)) { - const ElfSectionReader<ElfArch> *section = GetSection(shndx); + const ElfSectionReader<ElfArch>* section = GetSection(shndx); if (section == NULL) { return NULL; } else { @@ -797,7 +797,7 @@ class ElfReaderImpl { // Debug sections are likely to be near the end, so reverse the // direction of iteration. for (int k = GetNumSections() - 1; k >= 0; --k) { - const char *name = GetSectionName(section_headers_[k].sh_name); + const char* name = GetSectionName(section_headers_[k].sh_name); if (strncmp(name, ".debug", strlen(".debug")) == 0) return true; if (strncmp(name, ".zdebug", strlen(".zdebug")) == 0) return true; } @@ -816,7 +816,7 @@ class ElfReaderImpl { } private: - typedef vector<pair<uint64_t, const typename ElfArch::Sym *> > AddrToSymMap; + typedef vector<pair<uint64_t, const typename ElfArch::Sym*> > AddrToSymMap; static bool AddrToSymSorter(const typename AddrToSymMap::value_type& lhs, const typename AddrToSymMap::value_type& rhs) { @@ -854,8 +854,8 @@ class ElfReaderImpl { // Given an offset into the section header string table, return the // section name. - const char *GetSectionName(typename ElfArch::Word sh_name) { - const ElfSectionReader<ElfArch> *shstrtab = + const char* GetSectionName(typename ElfArch::Word sh_name) { + const ElfSectionReader<ElfArch>* shstrtab = GetSection(GetStringTableIndex()); if (shstrtab != NULL) { return shstrtab->GetOffset(sh_name); @@ -865,15 +865,15 @@ class ElfReaderImpl { // Return an ElfSectionReader for the given section. The reader will // be freed when this object is destroyed. - const ElfSectionReader<ElfArch> *GetSection(int num) { - const char *name; + const ElfSectionReader<ElfArch>* GetSection(int num) { + const char* name; // Hard-coding the name for the section-name string table prevents // infinite recursion. if (num == GetStringTableIndex()) name = ".shstrtab"; else name = GetSectionNameByIndex(num); - ElfSectionReader<ElfArch> *& reader = sections_[num]; + ElfSectionReader<ElfArch>*& reader = sections_[num]; if (reader == NULL) reader = new ElfSectionReader<ElfArch>(name, path_, fd_, section_headers_[num]); @@ -883,7 +883,7 @@ class ElfReaderImpl { // Parse out the overall header information from the file and assert // that it looks sane. This contains information like the magic // number and target architecture. - bool ParseHeaders(int fd, const string &path) { + bool ParseHeaders(int fd, const string& path) { // Read in the global ELF header. if (pread(fd, &header_, sizeof(header_), 0) != sizeof(header_)) { return false; @@ -985,11 +985,11 @@ class ElfReaderImpl { // Array of GetNumSections() section headers, allocated when we read // in the global header. - typename ElfArch::Shdr *section_headers_; + typename ElfArch::Shdr* section_headers_; // Array of GetNumProgramHeaders() program headers, allocated when we read // in the global header. - typename ElfArch::Phdr *program_headers_; + typename ElfArch::Phdr* program_headers_; // An array of pointers to ElfSectionReaders. Sections are // mmaped as they're needed and not released until this object is @@ -1000,7 +1000,7 @@ class ElfReaderImpl { // values for funtion symbols values. Function descriptors are kept in the // .opd section and are dereferenced to find the function address. ElfReader::SectionInfo opd_info_; - const char *opd_section_; // Must be checked for NULL before use. + const char* opd_section_; // Must be checked for NULL before use. int64_t base_for_text_; // Read PLT-related sections for the current architecture. @@ -1026,7 +1026,7 @@ class ElfReaderImpl { bool is_dwp_; }; -ElfReader::ElfReader(const string &path) +ElfReader::ElfReader(const string& path) : path_(path), fd_(-1), impl32_(NULL), impl64_(NULL) { // linux 2.6.XX kernel can show deleted files like this: // /var/run/nscd/dbYLJYaE (deleted) @@ -1063,7 +1063,7 @@ ElfReader::~ElfReader() { #endif template <typename ElfArch> -static bool IsElfFile(const int fd, const string &path) { +static bool IsElfFile(const int fd, const string& path) { if (fd < 0) return false; if (!ElfReaderImpl<ElfArch>::IsArchElfFile(fd, NULL)) { @@ -1086,7 +1086,7 @@ bool ElfReader::IsElf64File() const { } /* -void ElfReader::AddSymbols(SymbolMap *symbols, +void ElfReader::AddSymbols(SymbolMap* symbols, uint64_t mem_offset, uint64_t file_offset, uint64_t length) { if (fd_ < 0) @@ -1109,17 +1109,17 @@ void ElfReader::AddSymbols(SymbolMap *symbols, } */ -void ElfReader::VisitSymbols(ElfReader::SymbolSink *sink) { +void ElfReader::VisitSymbols(ElfReader::SymbolSink* sink) { VisitSymbols(sink, -1, -1); } -void ElfReader::VisitSymbols(ElfReader::SymbolSink *sink, +void ElfReader::VisitSymbols(ElfReader::SymbolSink* sink, int symbol_binding, int symbol_type) { VisitSymbols(sink, symbol_binding, symbol_type, false); } -void ElfReader::VisitSymbols(ElfReader::SymbolSink *sink, +void ElfReader::VisitSymbols(ElfReader::SymbolSink* sink, int symbol_binding, int symbol_type, bool get_raw_symbol_values) { @@ -1148,7 +1148,7 @@ uint64_t ElfReader::VaddrOfFirstLoadSegment() { } } -const char *ElfReader::GetSectionName(int shndx) { +const char* ElfReader::GetSectionName(int shndx) { if (shndx < 0 || static_cast<unsigned int>(shndx) >= GetNumSections()) return NULL; if (IsElf32File()) { return GetImpl32()->GetSectionNameByIndex(shndx); @@ -1169,7 +1169,7 @@ uint64_t ElfReader::GetNumSections() { } } -const char *ElfReader::GetSectionByIndex(int shndx, size_t *size) { +const char* ElfReader::GetSectionByIndex(int shndx, size_t* size) { if (IsElf32File()) { return GetImpl32()->GetSectionContentsByIndex(shndx, size); } else if (IsElf64File()) { @@ -1179,8 +1179,8 @@ const char *ElfReader::GetSectionByIndex(int shndx, size_t *size) { } } -const char *ElfReader::GetSectionByName(const string §ion_name, - size_t *size) { +const char* ElfReader::GetSectionByName(const string& section_name, + size_t* size) { if (IsElf32File()) { return GetImpl32()->GetSectionContentsByName(section_name, size); } else if (IsElf64File()) { @@ -1190,8 +1190,8 @@ const char *ElfReader::GetSectionByName(const string §ion_name, } } -const char *ElfReader::GetSectionInfoByName(const string §ion_name, - SectionInfo *info) { +const char* ElfReader::GetSectionInfoByName(const string& section_name, + SectionInfo* info) { if (IsElf32File()) { return GetImpl32()->GetSectionInfoByName(section_name, info); } else if (IsElf64File()) { @@ -1201,7 +1201,7 @@ const char *ElfReader::GetSectionInfoByName(const string §ion_name, } } -bool ElfReader::SectionNamesMatch(const string &name, const string &sh_name) { +bool ElfReader::SectionNamesMatch(const string& name, const string& sh_name) { if ((name.find(".debug_", 0) == 0) && (sh_name.find(".zdebug_", 0) == 0)) { const string name_suffix(name, strlen(".debug_")); const string sh_name_suffix(sh_name, strlen(".zdebug_")); @@ -1220,14 +1220,14 @@ bool ElfReader::IsDynamicSharedObject() { } } -ElfReaderImpl<Elf32> *ElfReader::GetImpl32() { +ElfReaderImpl<Elf32>* ElfReader::GetImpl32() { if (impl32_ == NULL) { impl32_ = new ElfReaderImpl<Elf32>(path_, fd_); } return impl32_; } -ElfReaderImpl<Elf64> *ElfReader::GetImpl64() { +ElfReaderImpl<Elf64>* ElfReader::GetImpl64() { if (impl64_ == NULL) { impl64_ = new ElfReaderImpl<Elf64>(path_, fd_); } @@ -1238,7 +1238,7 @@ ElfReaderImpl<Elf64> *ElfReader::GetImpl64() { // debug info (debug_only=true) or symbol table (debug_only=false). // Otherwise, return false. template <typename ElfArch> -static bool IsNonStrippedELFBinaryImpl(const string &path, const int fd, +static bool IsNonStrippedELFBinaryImpl(const string& path, const int fd, bool debug_only) { if (!ElfReaderImpl<ElfArch>::IsArchElfFile(fd, NULL)) return false; ElfReaderImpl<ElfArch> elf_reader(path, fd); @@ -1248,7 +1248,7 @@ static bool IsNonStrippedELFBinaryImpl(const string &path, const int fd, } // Helper for the IsNon[Debug]StrippedELFBinary functions. -static bool IsNonStrippedELFBinaryHelper(const string &path, +static bool IsNonStrippedELFBinaryHelper(const string& path, bool debug_only) { const int fd = open(path.c_str(), O_RDONLY); if (fd == -1) { @@ -1264,11 +1264,11 @@ static bool IsNonStrippedELFBinaryHelper(const string &path, return false; } -bool ElfReader::IsNonStrippedELFBinary(const string &path) { +bool ElfReader::IsNonStrippedELFBinary(const string& path) { return IsNonStrippedELFBinaryHelper(path, false); } -bool ElfReader::IsNonDebugStrippedELFBinary(const string &path) { +bool ElfReader::IsNonDebugStrippedELFBinary(const string& path) { return IsNonStrippedELFBinaryHelper(path, true); } } // namespace dwarf2reader |