src/share/vm/utilities/elfFile.cpp

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


 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;




 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 tables
 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       }
 131       // symbol tables
 132       else if (shdr.sh_type == SHT_SYMTAB || shdr.sh_type == SHT_DYNSYM) {
 133         ElfSymbolTable* table = new (std::nothrow) ElfSymbolTable(m_file, shdr);
 134         if (table == NULL) {
 135           m_status = NullDecoder::out_of_memory;
 136           return false;
 137         }
 138         add_symbol_table(table);
 139       }
 140     }
 141 
 142 #if defined(PPC64)
 143     // Now read the .opd section wich contains the PPC64 function descriptor table.
 144     // This is only possible after we have successfully read in the string tables in te previous loop.
 145     m_funcDesc_table = NULL;
 146     // Reset the file pointer
 147     if (fseek(m_file, m_elfHdr.e_shoff, SEEK_SET)) {
 148       m_status = NullDecoder::file_invalid;
 149       return false;
 150     }
 151     for (int index = 0; index < m_elfHdr.e_shnum; index ++) {
 152       if (fread((void*)&shdr, sizeof(Elf_Shdr), 1, m_file) != 1) {
 153         m_status = NullDecoder::file_invalid;
 154         return false;
 155       }
 156       if (m_elfHdr.e_shstrndx != SHN_UNDEF && shdr.sh_type == SHT_PROGBITS) {
 157         ElfStringTable* string_table = get_string_table(m_elfHdr.e_shstrndx);
 158         if (string_table == NULL) {
 159           m_status = NullDecoder::file_invalid;
 160           return false;
 161         }
 162         char buf[8]; // We only want to read ".opd"
 163         if (string_table->string_at(shdr.sh_name, buf, sizeof(buf)) && !strncmp(".opd", buf, 4)) {
 164           m_funcDesc_table = new (std::nothrow) ElfFuncDescTable(m_file, shdr);
 165         }
 166       }
 167     }
 168 #endif
 169 
 170   }
 171   return true;
 172 }
 173 
 174 bool ElfFile::decode(address addr, char* buf, int buflen, int* offset) {
 175   // something already went wrong, just give up
 176   if (NullDecoder::is_error(m_status)) {
 177     return false;
 178   }
 179   ElfSymbolTable* symbol_table = m_symbol_tables;
 180   int string_table_index;
 181   int pos_in_string_table;
 182   int off = INT_MAX;
 183   bool found_symbol = false;
 184   while (symbol_table != NULL) {
 185 #if defined(PPC64)
 186     if (symbol_table->lookup(addr, &string_table_index, &pos_in_string_table, &off, m_funcDesc_table)) {
 187 #else
 188     if (symbol_table->lookup(addr, &string_table_index, &pos_in_string_table, &off)) {
 189 #endif
 190       found_symbol = true;
 191       break;
 192     }
 193     symbol_table = symbol_table->m_next;
 194   }
 195   if (!found_symbol) return false;
 196 
 197   ElfStringTable* string_table = get_string_table(string_table_index);
 198 
 199   if (string_table == NULL) {
 200     m_status = NullDecoder::file_invalid;
 201     return false;
 202   }
 203   if (offset) *offset = off;
 204 
 205   return string_table->string_at(pos_in_string_table, buf, buflen);
 206 }
 207 
 208 
 209 void ElfFile::add_symbol_table(ElfSymbolTable* table) {
 210   if (m_symbol_tables == NULL) {
 211     m_symbol_tables = table;