| 1 | /* |
|---|
| 2 | This file is part of Libelfpp. |
|---|
| 3 | |
|---|
| 4 | Libelfpp is free software: you can redistribute it and/or modify |
|---|
| 5 | it under the terms of the GNU Lesser General Public License as |
|---|
| 6 | published by the Free Software Foundation, either version 3 of the |
|---|
| 7 | License, or (at your option) any later version. |
|---|
| 8 | |
|---|
| 9 | Libelfpp is distributed in the hope that it will be useful, but |
|---|
| 10 | WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 12 | General Public License for more details. |
|---|
| 13 | |
|---|
| 14 | You should have received a copy of the GNU Lesser General Public |
|---|
| 15 | License along with Libelfpp. If not, see |
|---|
| 16 | <http://www.gnu.org/licenses/>. |
|---|
| 17 | |
|---|
| 18 | Copyright (c) Alexandre Becoulet <alexandre.becoulet@free.fr> |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | #include <iostream> |
|---|
| 23 | #include <stdlib.h> |
|---|
| 24 | #include <cstring> |
|---|
| 25 | #include <stdlib.h> |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | #include <dpp/foreach> |
|---|
| 29 | |
|---|
| 30 | #include <elfpp/section> |
|---|
| 31 | #include <elfpp/object> |
|---|
| 32 | #include <elfpp/segment> |
|---|
| 33 | #include <elfpp/symbol> |
|---|
| 34 | #include <elfpp/reloc> |
|---|
| 35 | |
|---|
| 36 | namespace elfpp |
|---|
| 37 | { |
|---|
| 38 | |
|---|
| 39 | section::section(object &obj, sh_type_e type) |
|---|
| 40 | : name_(), |
|---|
| 41 | type_(type), |
|---|
| 42 | flags_(SHF_NONE), |
|---|
| 43 | vaddr_(0), |
|---|
| 44 | size_(0), |
|---|
| 45 | link_(0), |
|---|
| 46 | info_(0), |
|---|
| 47 | align_(0), |
|---|
| 48 | entsize_(0), |
|---|
| 49 | index_(0), |
|---|
| 50 | back_info_(0), |
|---|
| 51 | back_link_(0), |
|---|
| 52 | object_(&obj), |
|---|
| 53 | content_(0), |
|---|
| 54 | sym_tab_() |
|---|
| 55 | { |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | section::~section() |
|---|
| 59 | { |
|---|
| 60 | if (!orphan()) |
|---|
| 61 | remove(); |
|---|
| 62 | |
|---|
| 63 | if (content_) |
|---|
| 64 | free(content_); |
|---|
| 65 | |
|---|
| 66 | object_->secidx_[index_] = 0; |
|---|
| 67 | |
|---|
| 68 | FOREACH(s, sym_tab_) |
|---|
| 69 | delete s->second; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | void section::set_size(size_t size) |
|---|
| 73 | { |
|---|
| 74 | if (type_ != SHT_NOBITS && size_ != size) |
|---|
| 75 | { |
|---|
| 76 | content_ = (uint8_t*)realloc(content_, size); |
|---|
| 77 | |
|---|
| 78 | assert(content_ || size == 0); |
|---|
| 79 | |
|---|
| 80 | if (size > size_) |
|---|
| 81 | std::memset(content_ + size_, 0, size - size_); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | size_ = size; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | void section::set_content(void* a) |
|---|
| 88 | { |
|---|
| 89 | if(content_) |
|---|
| 90 | free(content_); |
|---|
| 91 | content_ = (uint8_t*) a; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | void section::add_symbol(symbol &sym) |
|---|
| 95 | { |
|---|
| 96 | sym_tab_.insert(sym_tab_map_t::value_type(sym.get_name(), &sym)); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | symbol & section::get_symbol(uint64_t offset) |
|---|
| 100 | { |
|---|
| 101 | symbol *res = 0; |
|---|
| 102 | |
|---|
| 103 | FOREACH(i, get_symbol_table()) |
|---|
| 104 | { |
|---|
| 105 | symbol *j = i->second; |
|---|
| 106 | |
|---|
| 107 | if (!j->get_size()) |
|---|
| 108 | continue; |
|---|
| 109 | |
|---|
| 110 | if (j->get_value() <= offset && (!res || j->get_value() > res->get_value())) |
|---|
| 111 | res = j; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | if (!res) |
|---|
| 115 | throw std::runtime_error("no symbol found"); |
|---|
| 116 | |
|---|
| 117 | if (offset - res->get_value() >= res->get_size()) |
|---|
| 118 | throw std::runtime_error("offset above symbol size"); |
|---|
| 119 | |
|---|
| 120 | return *res; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | segment * section::get_segment() const |
|---|
| 124 | { |
|---|
| 125 | if (!object_) |
|---|
| 126 | throw std::runtime_error("section is not part of an object"); |
|---|
| 127 | |
|---|
| 128 | // find associated segment based on section content in elf file |
|---|
| 129 | FOREACH(s, object_->get_segment_table()) |
|---|
| 130 | { |
|---|
| 131 | if (get_file_offset() >= s->get_file_offset() && |
|---|
| 132 | get_file_offset() < s->get_file_offset() + (off_t)s->get_file_size()) |
|---|
| 133 | return &*s; |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | return 0; |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | uint64_t section::get_load_address() const |
|---|
| 140 | { |
|---|
| 141 | segment *s = get_segment(); |
|---|
| 142 | |
|---|
| 143 | if (!s) |
|---|
| 144 | throw std::runtime_error("section is not located in a segment"); |
|---|
| 145 | |
|---|
| 146 | return vaddr_ - s->get_vaddr() + s->get_paddr(); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | std::ostream & operator<<(std::ostream &o, const section &s) |
|---|
| 150 | { |
|---|
| 151 | o << "[" << s.name_ << " addr:0x" << std::hex << s.vaddr_ << " size:" << std::dec << s.size_ << "]"; |
|---|
| 152 | return o; |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | } |
|---|
| 156 | |
|---|