src/share/vm/utilities/elfFile.cpp

Wed, 02 Feb 2011 11:35:26 -0500

author
bobv
date
Wed, 02 Feb 2011 11:35:26 -0500
changeset 2508
b92c45f2bc75
parent 2364
2d4762ec74af
child 2834
2a3da7eaf4a6
permissions
-rw-r--r--

7016023: Enable building ARM and PPC from src/closed repository
Reviewed-by: dholmes, bdelsart

     1 /*
     2  * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    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  */
    25 #include "precompiled.hpp"
    27 #ifndef _WINDOWS
    29 #include <string.h>
    30 #include <stdio.h>
    31 #include <limits.h>
    33 #include "memory/allocation.inline.hpp"
    34 #include "utilities/decoder.hpp"
    35 #include "utilities/elfFile.hpp"
    36 #include "utilities/elfStringTable.hpp"
    37 #include "utilities/elfSymbolTable.hpp"
    40 ElfFile::ElfFile(const char* filepath) {
    41   assert(filepath, "null file path");
    42   memset(&m_elfHdr, 0, sizeof(m_elfHdr));
    43   m_string_tables = NULL;
    44   m_symbol_tables = NULL;
    45   m_next = NULL;
    46   m_status = Decoder::no_error;
    48   int len = strlen(filepath) + 1;
    49   m_filepath = NEW_C_HEAP_ARRAY(char, len);
    50   if (m_filepath != NULL) {
    51     strcpy((char*)m_filepath, filepath);
    52     m_file = fopen(filepath, "r");
    53     if (m_file != NULL) {
    54       load_tables();
    55     } else {
    56       m_status = Decoder::file_not_found;
    57     }
    58   } else {
    59     m_status = Decoder::out_of_memory;
    60   }
    61 }
    63 ElfFile::~ElfFile() {
    64   if (m_string_tables != NULL) {
    65     delete m_string_tables;
    66   }
    68   if (m_symbol_tables != NULL) {
    69     delete m_symbol_tables;
    70   }
    72   if (m_file != NULL) {
    73     fclose(m_file);
    74   }
    76   if (m_filepath != NULL) {
    77     FREE_C_HEAP_ARRAY(char, m_filepath);
    78   }
    80   if (m_next != NULL) {
    81     delete m_next;
    82   }
    83 };
    86 //Check elf header to ensure the file is valid.
    87 bool ElfFile::is_elf_file(Elf_Ehdr& hdr) {
    88   return (ELFMAG0 == hdr.e_ident[EI_MAG0] &&
    89       ELFMAG1 == hdr.e_ident[EI_MAG1] &&
    90       ELFMAG2 == hdr.e_ident[EI_MAG2] &&
    91       ELFMAG3 == hdr.e_ident[EI_MAG3] &&
    92       ELFCLASSNONE != hdr.e_ident[EI_CLASS] &&
    93       ELFDATANONE != hdr.e_ident[EI_DATA]);
    94 }
    96 bool ElfFile::load_tables() {
    97   assert(m_file, "file not open");
    98   assert(m_status == Decoder::no_error, "already in error");
   100   // read elf file header
   101   if (fread(&m_elfHdr, sizeof(m_elfHdr), 1, m_file) != 1) {
   102     m_status = Decoder::file_invalid;
   103     return false;
   104   }
   106   if (!is_elf_file(m_elfHdr)) {
   107     m_status = Decoder::file_invalid;
   108     return false;
   109   }
   111   // walk elf file's section headers, and load string tables
   112   Elf_Shdr shdr;
   113   if (!fseek(m_file, m_elfHdr.e_shoff, SEEK_SET)) {
   114     if (m_status != Decoder::no_error) return false;
   116     for (int index = 0; index < m_elfHdr.e_shnum; index ++) {
   117       if (fread((void*)&shdr, sizeof(Elf_Shdr), 1, m_file) != 1) {
   118         m_status = Decoder::file_invalid;
   119         return false;
   120       }
   121       // string table
   122       if (shdr.sh_type == SHT_STRTAB) {
   123         ElfStringTable* table = new ElfStringTable(m_file, shdr, index);
   124         if (table == NULL) {
   125           m_status = Decoder::out_of_memory;
   126           return false;
   127         }
   128         add_string_table(table);
   129       } else if (shdr.sh_type == SHT_SYMTAB || shdr.sh_type == SHT_DYNSYM) {
   130         ElfSymbolTable* table = new ElfSymbolTable(m_file, shdr);
   131         if (table == NULL) {
   132           m_status = Decoder::out_of_memory;
   133           return false;
   134         }
   135         add_symbol_table(table);
   136       }
   137     }
   138   }
   139   return true;
   140 }
   142 const char* ElfFile::decode(address addr, int* offset) {
   143   // something already went wrong, just give up
   144   if (m_status != Decoder::no_error) {
   145     return NULL;
   146   }
   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 (Decoder::no_error == 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 NULL;
   161   ElfStringTable* string_table = get_string_table(string_table_index);
   162   if (string_table == NULL) {
   163     m_status = Decoder::file_invalid;
   164     return NULL;
   165   }
   166   if (offset) *offset = off;
   167   return string_table->string_at(pos_in_string_table);
   168 }
   171 void ElfFile::add_symbol_table(ElfSymbolTable* table) {
   172   if (m_symbol_tables == NULL) {
   173     m_symbol_tables = table;
   174   } else {
   175     table->m_next = m_symbol_tables;
   176     m_symbol_tables = table;
   177   }
   178 }
   180 void ElfFile::add_string_table(ElfStringTable* table) {
   181   if (m_string_tables == NULL) {
   182     m_string_tables = table;
   183   } else {
   184     table->m_next = m_string_tables;
   185     m_string_tables = table;
   186   }
   187 }
   189 ElfStringTable* ElfFile::get_string_table(int index) {
   190   ElfStringTable* p = m_string_tables;
   191   while (p != NULL) {
   192     if (p->index() == index) return p;
   193     p = p->m_next;
   194   }
   195   return NULL;
   196 }
   198 #endif // _WINDOWS

mercurial