1/*
2 * Copyright 2011, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ELF_SECTION_STR_TAB_HXX
18#define ELF_SECTION_STR_TAB_HXX
19
20#include "utils/helper.h"
21#include "utils/raw_ostream.h"
22
23#include <llvm/Support/Format.h>
24#include <llvm/Support/raw_ostream.h>
25
26template <unsigned Bitwidth>
27template <typename Archiver>
28ELFSectionStrTab<Bitwidth> *
29ELFSectionStrTab<Bitwidth>::read(Archiver &AR,
30                                 ELFSectionHeaderTy const *sh) {
31
32  std::unique_ptr<ELFSectionStrTab> st(new ELFSectionStrTab());
33  st->buf.resize(sh->getSize());
34
35  // Save section_header
36  st->section_header = sh;
37
38  AR.seek(sh->getOffset(), true);
39  AR.prologue(sh->getSize());
40  AR.readBytes(&*st->buf.begin(), sh->getSize());
41  AR.epilogue(sh->getSize());
42
43  if (!AR) {
44    // Unable to read the string table.
45    return 0;
46  }
47
48  return st.release();
49}
50
51template <unsigned Bitwidth>
52void ELFSectionStrTab<Bitwidth>::print() const {
53  using namespace llvm;
54
55  out() << '\n' << fillformat('=', 79) << '\n';
56  out().changeColor(raw_ostream::WHITE, true);
57  out() << "ELF String Table: " << this->section_header->getName() << '\n';
58  out().resetColor();
59  out() << fillformat('-', 79) << '\n';
60
61  dump_hex((unsigned char const *)&*buf.begin(), buf.size(), 0, buf.size());
62
63  out() << fillformat('=', 79) << '\n';
64}
65
66#endif // ELF_SECTION_STR_TAB_HXX
67