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_STR_TAB_HXX
18b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines#define ELF_SECTION_STR_TAB_HXX
19b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines
20b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines#include "utils/helper.h"
21b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines#include "utils/raw_ostream.h"
22b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines
23b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines#include <llvm/ADT/OwningPtr.h>
24b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines#include <llvm/Support/Format.h>
25b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines#include <llvm/Support/raw_ostream.h>
26b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines
27b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hinestemplate <unsigned Bitwidth>
28b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hinestemplate <typename Archiver>
29b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen HinesELFSectionStrTab<Bitwidth> *
30b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen HinesELFSectionStrTab<Bitwidth>::read(Archiver &AR,
31b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines                                 ELFSectionHeaderTy const *sh) {
32b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines
33b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines  llvm::OwningPtr<ELFSectionStrTab> st(new ELFSectionStrTab());
34b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines  st->buf.resize(sh->getSize());
35b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines
36b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines  // Save section_header
37b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines  st->section_header = sh;
38b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines
39b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines  AR.seek(sh->getOffset(), true);
40b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines  AR.prologue(sh->getSize());
41b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines  AR.readBytes(&*st->buf.begin(), sh->getSize());
42b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines  AR.epilogue(sh->getSize());
43b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines
44b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines  if (!AR) {
45b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines    // Unable to read the string table.
46b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines    return 0;
47b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines  }
48b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines
49b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines  return st.take();
50b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines}
51b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines
52b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hinestemplate <unsigned Bitwidth>
53b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hinesvoid ELFSectionStrTab<Bitwidth>::print() const {
54b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines  using namespace llvm;
55b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines
56b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines  out() << '\n' << fillformat('=', 79) << '\n';
57b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines  out().changeColor(raw_ostream::WHITE, true);
58b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines  out() << "ELF String Table: " << this->section_header->getName() << '\n';
59b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines  out().resetColor();
60b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines  out() << fillformat('-', 79) << '\n';
61b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines
62b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines  dump_hex((unsigned char const *)&*buf.begin(), buf.size(), 0, buf.size());
63b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines
64b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines  out() << fillformat('=', 79) << '\n';
65b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines}
66b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines
67b53c8a59e6f21ed36a0c3d9d4ce5834d4cc3c298Stephen Hines#endif // ELF_SECTION_STR_TAB_HXX
68