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 "jvm.h"
26#include "utilities/decoder_elf.hpp"
27#include "utilities/elfFile.hpp"
28
29#include <cxxabi.h>
30
31bool ElfDecoder::demangle(const char* symbol, char *buf, int buflen) {
32 int status;
33 char* result;
34 size_t size = (size_t)buflen;
35
36#ifdef PPC64
37 // On PPC64 ElfDecoder::decode() may return a dot (.) prefixed name
38 // (see elfFuncDescTable.hpp for details)
39 if (symbol && *symbol == '.') symbol += 1;
40#endif
41
42 // Don't pass buf to __cxa_demangle. In case of the 'buf' is too small,
43 // __cxa_demangle will call system "realloc" for additional memory, which
44 // may use different malloc/realloc mechanism that allocates 'buf'.
45 if ((result = abi::__cxa_demangle(symbol, NULL, NULL, &status)) != NULL) {
46 jio_snprintf(buf, buflen, "%s", result);
47 // call c library's free
48 ::free(result);
49 return true;
50 }
51 return false;
52}
53
54// Returns true if the elf file is marked NOT to require an executable stack,
55// or if the file could not be opened.
56// Returns false if the elf file requires an executable stack, the stack flag
57// is not set at all, or if the file can not be read.
58bool ElfFile::specifies_noexecstack(const char* filepath) {
59 if (filepath == NULL) return true;
60
61 FILE* file = fopen(filepath, "r");
62 if (file == NULL) return true;
63
64 // AARCH64 defaults to noexecstack. All others default to execstack.
65 bool result = AARCH64_ONLY(true) NOT_AARCH64(false);
66
67 // Read file header
68 Elf_Ehdr head;
69 if (fread(&head, sizeof(Elf_Ehdr), 1, file) == 1 &&
70 is_elf_file(head) &&
71 fseek(file, head.e_phoff, SEEK_SET) == 0) {
72
73 // Read program header table
74 Elf_Phdr phdr;
75 for (int index = 0; index < head.e_phnum; index ++) {
76 if (fread((void*)&phdr, sizeof(Elf_Phdr), 1, file) != 1) {
77 result = false;
78 break;
79 }
80 if (phdr.p_type == PT_GNU_STACK) {
81 result = (phdr.p_flags == (PF_R | PF_W));
82 break;
83 }
84 }
85 }
86 fclose(file);
87 return result;
88}
89