src/share/vm/utilities/elfFile.cpp

Print this page
rev 5729 : 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables


  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 
  27 #if !defined(_WINDOWS) && !defined(__APPLE__)
  28 
  29 #include <string.h>
  30 #include <stdio.h>
  31 #include <limits.h>
  32 #include <new>
  33 
  34 #include "memory/allocation.inline.hpp"
  35 #include "utilities/decoder.hpp"
  36 #include "utilities/elfFile.hpp"

  37 #include "utilities/elfStringTable.hpp"
  38 #include "utilities/elfSymbolTable.hpp"
  39 
  40 
  41 ElfFile::ElfFile(const char* filepath) {
  42   assert(filepath, "null file path");
  43   memset(&m_elfHdr, 0, sizeof(m_elfHdr));
  44   m_string_tables = NULL;
  45   m_symbol_tables = NULL;

  46   m_next = NULL;
  47   m_status = NullDecoder::no_error;
  48 
  49   int len = strlen(filepath) + 1;
  50   m_filepath = (const char*)os::malloc(len * sizeof(char), mtInternal);
  51   if (m_filepath != NULL) {
  52     strcpy((char*)m_filepath, filepath);
  53     m_file = fopen(filepath, "r");
  54     if (m_file != NULL) {
  55       load_tables();
  56     } else {
  57       m_status = NullDecoder::file_not_found;
  58     }
  59   } else {
  60     m_status = NullDecoder::out_of_memory;
  61   }
  62 }
  63 
  64 ElfFile::~ElfFile() {
  65   if (m_string_tables != NULL) {


 102   if (fread(&m_elfHdr, sizeof(m_elfHdr), 1, m_file) != 1) {
 103     m_status = NullDecoder::file_invalid;
 104     return false;
 105   }
 106 
 107   if (!is_elf_file(m_elfHdr)) {
 108     m_status = NullDecoder::file_invalid;
 109     return false;
 110   }
 111 
 112   // walk elf file's section headers, and load string tables
 113   Elf_Shdr shdr;
 114   if (!fseek(m_file, m_elfHdr.e_shoff, SEEK_SET)) {
 115     if (NullDecoder::is_error(m_status)) return false;
 116 
 117     for (int index = 0; index < m_elfHdr.e_shnum; index ++) {
 118       if (fread((void*)&shdr, sizeof(Elf_Shdr), 1, m_file) != 1) {
 119         m_status = NullDecoder::file_invalid;
 120         return false;
 121       }
 122       // string table
 123       if (shdr.sh_type == SHT_STRTAB) {
 124         ElfStringTable* table = new (std::nothrow) ElfStringTable(m_file, shdr, index);
 125         if (table == NULL) {
 126           m_status = NullDecoder::out_of_memory;
 127           return false;
 128         }
 129         add_string_table(table);
 130       } else if (shdr.sh_type == SHT_SYMTAB || shdr.sh_type == SHT_DYNSYM) {


 131         ElfSymbolTable* table = new (std::nothrow) ElfSymbolTable(m_file, shdr);
 132         if (table == NULL) {
 133           m_status = NullDecoder::out_of_memory;
 134           return false;
 135         }
 136         add_symbol_table(table);
 137       }
 138     }




































 139   }
 140   return true;
 141 }
 142 
 143 bool ElfFile::decode(address addr, char* buf, int buflen, int* offset) {
 144   // something already went wrong, just give up
 145   if (NullDecoder::is_error(m_status)) {
 146     return false;
 147   }
 148   ElfSymbolTable* symbol_table = m_symbol_tables;
 149   int string_table_index;
 150   int pos_in_string_table;
 151   int off = INT_MAX;
 152   bool found_symbol = false;
 153   while (symbol_table != NULL) {
 154     if (symbol_table->lookup(addr, &string_table_index, &pos_in_string_table, &off)) {
 155       found_symbol = true;

 156     }
 157     symbol_table = symbol_table->m_next;
 158   }
 159   if (!found_symbol) return false;
 160 
 161   ElfStringTable* string_table = get_string_table(string_table_index);
 162 
 163   if (string_table == NULL) {
 164     m_status = NullDecoder::file_invalid;
 165     return false;
 166   }
 167   if (offset) *offset = off;
 168 
 169   return string_table->string_at(pos_in_string_table, buf, buflen);
 170 }
 171 
 172 
 173 void ElfFile::add_symbol_table(ElfSymbolTable* table) {
 174   if (m_symbol_tables == NULL) {
 175     m_symbol_tables = table;




  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 
  27 #if !defined(_WINDOWS) && !defined(__APPLE__)
  28 
  29 #include <string.h>
  30 #include <stdio.h>
  31 #include <limits.h>
  32 #include <new>
  33 
  34 #include "memory/allocation.inline.hpp"
  35 #include "utilities/decoder.hpp"
  36 #include "utilities/elfFile.hpp"
  37 #include "utilities/elfFuncDescTable.hpp"
  38 #include "utilities/elfStringTable.hpp"
  39 #include "utilities/elfSymbolTable.hpp"
  40 
  41 
  42 ElfFile::ElfFile(const char* filepath) {
  43   assert(filepath, "null file path");
  44   memset(&m_elfHdr, 0, sizeof(m_elfHdr));
  45   m_string_tables = NULL;
  46   m_symbol_tables = NULL;
  47   m_funcDesc_table = NULL;
  48   m_next = NULL;
  49   m_status = NullDecoder::no_error;
  50 
  51   int len = strlen(filepath) + 1;
  52   m_filepath = (const char*)os::malloc(len * sizeof(char), mtInternal);
  53   if (m_filepath != NULL) {
  54     strcpy((char*)m_filepath, filepath);
  55     m_file = fopen(filepath, "r");
  56     if (m_file != NULL) {
  57       load_tables();
  58     } else {
  59       m_status = NullDecoder::file_not_found;
  60     }
  61   } else {
  62     m_status = NullDecoder::out_of_memory;
  63   }
  64 }
  65 
  66 ElfFile::~ElfFile() {
  67   if (m_string_tables != NULL) {


 104   if (fread(&m_elfHdr, sizeof(m_elfHdr), 1, m_file) != 1) {
 105     m_status = NullDecoder::file_invalid;
 106     return false;
 107   }
 108 
 109   if (!is_elf_file(m_elfHdr)) {
 110     m_status = NullDecoder::file_invalid;
 111     return false;
 112   }
 113 
 114   // walk elf file's section headers, and load string tables
 115   Elf_Shdr shdr;
 116   if (!fseek(m_file, m_elfHdr.e_shoff, SEEK_SET)) {
 117     if (NullDecoder::is_error(m_status)) return false;
 118 
 119     for (int index = 0; index < m_elfHdr.e_shnum; index ++) {
 120       if (fread((void*)&shdr, sizeof(Elf_Shdr), 1, m_file) != 1) {
 121         m_status = NullDecoder::file_invalid;
 122         return false;
 123       }
 124       // string tables
 125       if (shdr.sh_type == SHT_STRTAB) {
 126         ElfStringTable* table = new (std::nothrow) ElfStringTable(m_file, shdr, index);
 127         if (table == NULL) {
 128           m_status = NullDecoder::out_of_memory;
 129           return false;
 130         }
 131         add_string_table(table);
 132       }
 133       // symbol tables
 134       else if (shdr.sh_type == SHT_SYMTAB || shdr.sh_type == SHT_DYNSYM) {
 135         ElfSymbolTable* table = new (std::nothrow) ElfSymbolTable(m_file, shdr);
 136         if (table == NULL) {
 137           m_status = NullDecoder::out_of_memory;
 138           return false;
 139         }
 140         add_symbol_table(table);
 141       }
 142     }
 143 
 144 #if defined(PPC64)
 145     // Now read the .opd section wich contains the PPC64 function descriptor table.
 146     // The .opd section is only available on PPC64 (see for example:
 147     // http://refspecs.linuxfoundation.org/LSB_3.1.1/LSB-Core-PPC64/LSB-Core-PPC64/specialsections.html)
 148     // so this code should do no harm on other platforms but because of performance reasons we only
 149     // execute it on PPC64 platforms.
 150     // Notice that we can only find the .opd section after we have successfully read in the string
 151     // tables in the previous loop, because we need to query the name of each section which is
 152     // contained in one of the string tables (i.e. the one with the index m_elfHdr.e_shstrndx).
 153 
 154     // Reset the file pointer
 155     if (fseek(m_file, m_elfHdr.e_shoff, SEEK_SET)) {
 156       m_status = NullDecoder::file_invalid;
 157       return false;
 158     }
 159     for (int index = 0; index < m_elfHdr.e_shnum; index ++) {
 160       if (fread((void*)&shdr, sizeof(Elf_Shdr), 1, m_file) != 1) {
 161         m_status = NullDecoder::file_invalid;
 162         return false;
 163       }
 164       if (m_elfHdr.e_shstrndx != SHN_UNDEF && shdr.sh_type == SHT_PROGBITS) {
 165         ElfStringTable* string_table = get_string_table(m_elfHdr.e_shstrndx);
 166         if (string_table == NULL) {
 167           m_status = NullDecoder::file_invalid;
 168           return false;
 169         }
 170         char buf[8]; // '8' is enough because we only want to read ".opd"
 171         if (string_table->string_at(shdr.sh_name, buf, sizeof(buf)) && !strncmp(".opd", buf, 4)) {
 172           m_funcDesc_table = new (std::nothrow) ElfFuncDescTable(m_file, shdr);
 173           break;
 174         }
 175       }
 176     }
 177 #endif
 178 
 179   }
 180   return true;
 181 }
 182 
 183 bool ElfFile::decode(address addr, char* buf, int buflen, int* offset) {
 184   // something already went wrong, just give up
 185   if (NullDecoder::is_error(m_status)) {
 186     return false;
 187   }
 188   ElfSymbolTable* symbol_table = m_symbol_tables;
 189   int string_table_index;
 190   int pos_in_string_table;
 191   int off = INT_MAX;
 192   bool found_symbol = false;
 193   while (symbol_table != NULL) {
 194     if (symbol_table->lookup(addr, &string_table_index, &pos_in_string_table, &off, m_funcDesc_table)) {
 195       found_symbol = true;
 196       break;
 197     }
 198     symbol_table = symbol_table->m_next;
 199   }
 200   if (!found_symbol) return false;
 201 
 202   ElfStringTable* string_table = get_string_table(string_table_index);
 203 
 204   if (string_table == NULL) {
 205     m_status = NullDecoder::file_invalid;
 206     return false;
 207   }
 208   if (offset) *offset = off;
 209 
 210   return string_table->string_at(pos_in_string_table, buf, buflen);
 211 }
 212 
 213 
 214 void ElfFile::add_symbol_table(ElfSymbolTable* table) {
 215   if (m_symbol_tables == NULL) {
 216     m_symbol_tables = table;