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