1b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines/*
2b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines * Copyright 2011, The Android Open Source Project
3b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines *
4b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines * Licensed under the Apache License, Version 2.0 (the "License");
5b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines * you may not use this file except in compliance with the License.
6b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines * You may obtain a copy of the License at
7b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines *
8b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines *     http://www.apache.org/licenses/LICENSE-2.0
9b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines *
10b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines * Unless required by applicable law or agreed to in writing, software
11b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines * distributed under the License is distributed on an "AS IS" BASIS,
12b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines * See the License for the specific language governing permissions and
14b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines * limitations under the License.
15b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines */
16b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines
17b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines#ifndef ELF_SECTION_BITS_HXX
18b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines#define ELF_SECTION_BITS_HXX
19b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines
20b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines#include "utils/flush_cpu_cache.h"
21b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines#include "utils/helper.h"
22b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines
23b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines#include <llvm/Support/raw_ostream.h>
24b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines
251033b5974b02238c2413119963408a1ad65d5c93Raphael Moll#ifndef USE_MINGW       /* TODO create a proper HAVE_MMAN_H */
26b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines#include <sys/mman.h>
271033b5974b02238c2413119963408a1ad65d5c93Raphael Moll#else
281033b5974b02238c2413119963408a1ad65d5c93Raphael Moll#include "mmanWindows.h"
291033b5974b02238c2413119963408a1ad65d5c93Raphael Moll#endif
30b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines
31b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hinestemplate <unsigned Bitwidth>
32b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hinesinline void ELFSectionBits<Bitwidth>::print() const {
33b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines  using namespace llvm;
34b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines
35b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines  char const *section_type_str =
36b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines    (sh->getType() == SHT_NOBITS) ? "NOBITS" : "PROGBITS";
37b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines
38b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines  out() << '\n' << fillformat('=', 79) << '\n';
39b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines  out().changeColor(raw_ostream::WHITE, true);
40b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines  out() << "ELF " << section_type_str << ": " << sh->getName() << '\n';
41b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines  out().resetColor();
42b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines  out() << fillformat('-', 79) << '\n';
43b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines
44b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines  out() << "  Size         : " << sh->getSize() << '\n';
45b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines  out() << "  Start Address: " << (void *)chunk.getBuffer() << '\n';
46b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines  out() << fillformat('-', 79) << '\n';
47b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines
48b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines  chunk.print();
49b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines
50b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines  out() << fillformat('=', 79) << '\n';
51b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines}
52b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines
53b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hinestemplate <unsigned Bitwidth>
54b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hinesinline bool ELFSectionBits<Bitwidth>::protect() {
55b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines  int prot = PROT_READ;
56b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines
57b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines  if (sh->getFlags() & SHF_WRITE) {
58b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines    prot |= PROT_WRITE;
59b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines  }
60b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines
61b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines  if (sh->getFlags() & SHF_EXECINSTR) {
62b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines    prot |= PROT_EXEC;
63b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines  }
64b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines
65b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines  return chunk.protect(prot);
66b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines}
67b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines
68b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines#endif // ELF_SECTION_BITS_HXX
69