21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 27 #if !defined(_WINDOWS) && !defined(__APPLE__) 28 29 #include "memory/allocation.inline.hpp" 30 #include "runtime/os.hpp" 31 #include "utilities/elfStringTable.hpp" 32 33 // We will try to load whole string table into memory if we can. 34 // Otherwise, fallback to more expensive file operation. 35 ElfStringTable::ElfStringTable(FILE* file, Elf_Shdr shdr, int index) { 36 assert(file, "null file handle"); 37 m_table = NULL; 38 m_index = index; 39 m_next = NULL; 40 m_file = file; 41 m_status = Decoder::no_error; 42 43 // try to load the string table 44 long cur_offset = ftell(file); 45 m_table = (char*)os::malloc(sizeof(char) * shdr.sh_size); 46 if (m_table != NULL) { 47 // if there is an error, mark the error 48 if (fseek(file, shdr.sh_offset, SEEK_SET) || 49 fread((void*)m_table, shdr.sh_size, 1, file) != 1 || 50 fseek(file, cur_offset, SEEK_SET)) { 51 m_status = Decoder::file_invalid; 52 os::free((void*)m_table); 53 m_table = NULL; 54 } 55 } else { 56 memcpy(&m_shdr, &shdr, sizeof(Elf_Shdr)); 57 } 58 } 59 60 ElfStringTable::~ElfStringTable() { 61 if (m_table != NULL) { 62 os::free((void*)m_table); 63 } 64 65 if (m_next != NULL) { 66 delete m_next; 67 } 68 } 69 70 const char* ElfStringTable::string_at(int pos) { 71 if (m_status != Decoder::no_error) { 72 return NULL; 73 } 74 if (m_table != NULL) { 75 return (const char*)(m_table + pos); 76 } else { 77 long cur_pos = ftell(m_file); 78 if (cur_pos == -1 || 79 fseek(m_file, m_shdr.sh_offset + pos, SEEK_SET) || 80 fread(m_symbol, 1, MAX_SYMBOL_LEN, m_file) <= 0 || 81 fseek(m_file, cur_pos, SEEK_SET)) { 82 m_status = Decoder::file_invalid; 83 return NULL; 84 } 85 return (const char*)m_symbol; 86 } 87 } 88 89 #endif // _WINDOWS | 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 27 #if !defined(_WINDOWS) && !defined(__APPLE__) 28 29 #include "memory/allocation.inline.hpp" 30 #include "runtime/os.hpp" 31 #include "utilities/elfStringTable.hpp" 32 33 // We will try to load whole string table into memory if we can. 34 // Otherwise, fallback to more expensive file operation. 35 ElfStringTable::ElfStringTable(FILE* file, Elf_Shdr shdr, int index) { 36 assert(file, "null file handle"); 37 m_table = NULL; 38 m_index = index; 39 m_next = NULL; 40 m_file = file; 41 m_status = NullDecoder::no_error; 42 43 // try to load the string table 44 long cur_offset = ftell(file); 45 m_table = (char*)os::malloc(sizeof(char) * shdr.sh_size); 46 if (m_table != NULL) { 47 // if there is an error, mark the error 48 if (fseek(file, shdr.sh_offset, SEEK_SET) || 49 fread((void*)m_table, shdr.sh_size, 1, file) != 1 || 50 fseek(file, cur_offset, SEEK_SET)) { 51 m_status = NullDecoder::file_invalid; 52 os::free((void*)m_table); 53 m_table = NULL; 54 } 55 } else { 56 memcpy(&m_shdr, &shdr, sizeof(Elf_Shdr)); 57 } 58 } 59 60 ElfStringTable::~ElfStringTable() { 61 if (m_table != NULL) { 62 os::free((void*)m_table); 63 } 64 65 if (m_next != NULL) { 66 delete m_next; 67 } 68 } 69 70 bool ElfStringTable::string_at(int pos, char* buf, int buflen) { 71 if (NullDecoder::is_error(m_status)) { 72 return false; 73 } 74 if (m_table != NULL) { 75 jio_snprintf(buf, buflen, "%s", (const char*)(m_table + pos)); 76 return true; 77 } else { 78 long cur_pos = ftell(m_file); 79 if (cur_pos == -1 || 80 fseek(m_file, m_shdr.sh_offset + pos, SEEK_SET) || 81 fread(buf, 1, buflen, m_file) <= 0 || 82 fseek(m_file, cur_pos, SEEK_SET)) { 83 m_status = NullDecoder::file_invalid; 84 return false; 85 } 86 return true; 87 } 88 } 89 90 #endif // _WINDOWS |