1 | /* |
2 | * Copyright (c) 1997, 2018, 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 | #if !defined(_WINDOWS) && !defined(__APPLE__) |
28 | |
29 | #include "jvm.h" |
30 | #include "memory/allocation.inline.hpp" |
31 | #include "runtime/os.hpp" |
32 | #include "utilities/elfStringTable.hpp" |
33 | |
34 | // We will try to load whole string table into memory if we can. |
35 | // Otherwise, fallback to more expensive file operation. |
36 | ElfStringTable::ElfStringTable(FILE* const file, Elf_Shdr& shdr, int index) : |
37 | _next(NULL), _index(index), _section(file, shdr), _fd(file) { |
38 | _status = _section.status(); |
39 | } |
40 | |
41 | ElfStringTable::~ElfStringTable() { |
42 | if (_next != NULL) { |
43 | delete _next; |
44 | } |
45 | } |
46 | |
47 | bool ElfStringTable::string_at(size_t pos, char* buf, int buflen) { |
48 | if (NullDecoder::is_error(get_status())) { |
49 | return false; |
50 | } |
51 | |
52 | assert(buflen > 0, "no buffer" ); |
53 | if (pos >= _section.section_header()->sh_size) { |
54 | return false; |
55 | } |
56 | |
57 | const char* data = (const char*)_section.section_data(); |
58 | if (data != NULL) { |
59 | jio_snprintf(buf, buflen, "%s" , data + pos); |
60 | return true; |
61 | } else { // no cache data, read from file instead |
62 | const Elf_Shdr* const shdr = _section.section_header(); |
63 | MarkedFileReader mfd(_fd); |
64 | if (mfd.has_mark() && |
65 | mfd.set_position(shdr->sh_offset + pos) && |
66 | mfd.read((void*)buf, size_t(buflen))) { |
67 | buf[buflen - 1] = '\0'; |
68 | return true; |
69 | } else { |
70 | // put it in error state to avoid retry |
71 | _status = NullDecoder::file_invalid; |
72 | return false; |
73 | } |
74 | } |
75 | } |
76 | |
77 | #endif // !_WINDOWS && !__APPLE__ |
78 | |