29 #ifndef COFFI_SYMBOLS_HPP
30 #define COFFI_SYMBOLS_HPP
37 #if defined(__has_include) && __has_include(<gsl/narrow>)
39 using gsl::narrow_cast;
42 #define narrow_cast static_cast
56 std::fill_n(
reinterpret_cast<char*
>(&header),
sizeof(header),
'\0');
61 COFFI_GET_SET_ACCESS(uint32_t, value);
62 COFFI_GET_SET_ACCESS(uint16_t, section_number);
63 COFFI_GET_SET_ACCESS(uint16_t, type);
64 COFFI_GET_SET_ACCESS(uint8_t, storage_class);
65 COFFI_GET_SET_ACCESS(uint8_t, aux_symbols_number);
69 uint32_t get_index()
const {
return index_; }
72 void set_index(uint32_t index) { index_ = index; }
75 const std::string get_name()
const
81 void set_name(
const std::string& value)
87 const std::vector<auxiliary_symbol_record>& get_auxiliary_symbols()
const
93 std::vector<auxiliary_symbol_record>& get_auxiliary_symbols()
99 bool load(std::istream& stream)
101 stream.read(
reinterpret_cast<char*
>(&header),
sizeof(header));
102 if (stream.gcount() !=
sizeof(header)) {
106 for (uint8_t i = 0; i < get_aux_symbols_number(); ++i) {
108 stream.read(
reinterpret_cast<char*
>(&a),
sizeof(
symbol_record));
118 void save(std::ostream& stream)
120 set_aux_symbols_number(narrow_cast<uint8_t>(auxs.size()));
121 stream.write(
reinterpret_cast<char*
>(&header),
sizeof(header));
122 for (
auto const& aux : auxs) {
123 stream.write(
reinterpret_cast<char const*
>(&aux),
131 std::vector<auxiliary_symbol_record> auxs;
160 uint32_t R = narrow_cast<uint32_t>(symbols_.size()) - 1;
162 uint32_t m = (L + R) / 2;
163 if (symbols_[m].get_index() < index) {
166 else if (symbols_[m].get_index() > index) {
170 return &(symbols_[m]);
187 for (
auto s = symbols_.begin(); s != symbols_.end(); s++) {
188 if (s->get_name() == name) {
196 std::vector<symbol>* get_symbols() {
return &symbols_; }
199 const std::vector<symbol>* get_symbols()
const {
return &symbols_; }
206 if (symbols_.size() > 0) {
207 index = (symbols_.end() - 1)->get_index() + 1 +
208 narrow_cast<uint32_t>(
209 (symbols_.end() - 1)->get_auxiliary_symbols().size());
214 symbols_.push_back(s);
215 return &*(symbols_.end() - 1);
221 void clean_symbols() { symbols_.clear(); }
224 bool load_symbols(std::istream& stream,
const coff_header* header)
226 if (header->get_symbol_table_offset() == 0) {
230 stream.seekg(header->get_symbol_table_offset());
231 for (uint32_t i = 0; i < header->get_symbols_count(); ++i) {
233 if (!s.load(stream)) {
237 i += narrow_cast<uint32_t>(s.get_auxiliary_symbols().size());
238 symbols_.push_back(s);
245 void save_symbols(std::ostream& stream)
247 for (
auto s : symbols_) {
253 uint32_t get_symbols_filesize()
255 uint32_t filesize = 0;
256 for (
auto s : symbols_) {
258 sizeof(symbol_record) *
259 (1 + narrow_cast<uint32_t>(s.get_auxiliary_symbols().size()));
265 std::vector<symbol> symbols_;
Class for accessing the symbol table.
virtual symbol * get_symbol(uint32_t index)
Gets a symbol from its index.
virtual const symbol * get_symbol(const std::string &name) const
Gets a symbol from its name.
virtual symbol * get_symbol(const std::string &name)
Gets a symbol from its name.
symbol * add_symbol(const std::string &name)
Adds a symbol in the table.
virtual const symbol * get_symbol(uint32_t index) const
Gets a symbol from its index.
Interface for accessing to the string table.
virtual std::string string_to_name(const char *str) const =0
Converts an 8-bytes symbol short name into a full name, eventually by looking into the strings table.
virtual void name_to_string(const std::string &name, char *str)=0
Converts symbol full name into an 8-bytes short name, eventually creating an entry in the strings tab...
Interface for accessing to the symbol table.
Class for accessing a COFF symbol.
Generic auxiliary symbol record, covers any type of auxiliary symbol.