Ticket #14: binary_file_loader.diff
File binary_file_loader.diff, 49.9 KB (added by , 15 years ago) |
---|
-
soclib/lib/iss_memchecker/metadata/iss_memchecker.sd
15 15 Uses('common:iss2'), 16 16 Uses('common:mapping_table'), 17 17 Uses('common:exception'), 18 Uses('common:elf_ loader'),18 Uses('common:elf_file_loader'), 19 19 ], 20 20 extensions = [ 21 21 'dsx:cpu=wrapper:iss_memchecker_t', -
soclib/lib/iss_memchecker/include/iss_memchecker.h
97 97 } 98 98 99 99 static void init( const soclib::common::MappingTable &mt, 100 const soclib::common:: ElfLoader &loader,100 const soclib::common::BinaryFileLoader &loader, 101 101 const std::string &exclusions = "" ); 102 102 103 103 private: -
soclib/lib/iss_memchecker/src/iss_memchecker.cpp
35 35 #include "exception.h" 36 36 37 37 #include "soclib_endian.h" 38 #include " elf_loader.h"38 #include "binary_file_loader.h" 39 39 40 40 namespace soclib { namespace common { 41 41 … … 433 433 434 434 class MemoryState 435 435 { 436 ElfLoader m_binary;436 BinaryFileLoader m_binary; 437 437 438 438 typedef std::map<uint32_t, ContextState *> context_map_t; 439 439 typedef std::map<uint32_t, std::vector<AddressInfo> *> region_map_t; … … 446 446 ContextState * const unknown_context; 447 447 448 448 MemoryState( const soclib::common::MappingTable &mt, 449 const soclib::common:: ElfLoader &loader,449 const soclib::common::BinaryFileLoader &loader, 450 450 const std::string &exclusions ) 451 451 : m_binary(loader), 452 452 m_contexts(), … … 458 458 unknown_context->ref(); 459 459 460 460 const std::list<Segment> &segments = mt.getAllSegmentList(); 461 ElfLoader::section_list_t sections = loader.sections();461 BinaryFileLoader::section_list_t sections = loader.sections(); 462 462 463 463 std::string exclusion_list = ","; 464 464 exclusion_list += exclusions + ","; … … 478 478 m_regions[i->baseAddress()] = rm; 479 479 } 480 480 481 for ( ElfLoader::section_list_t::const_iterator i = sections.begin();481 for ( BinaryFileLoader::section_list_t::const_iterator i = sections.begin(); 482 482 i != sections.end(); 483 483 ++i ) { 484 484 … … 511 511 info_for_address( i->baseAddress()+j )->do_write(); 512 512 } 513 513 514 uintptr_t comm_addr = loader.get_symbol_addr( "soclib_iss_memchecker_addr" ); 515 if ( comm_addr ) { 516 std::cout << "Binary file defined IssMemchecker communication address to " << comm_addr << std::endl; 517 m_comm_address = comm_addr; 514 const BinaryFileSymbol *sym = loader.get_symbol_by_name( "soclib_iss_memchecker_addr" ); 515 if ( sym ) { 516 m_comm_address = sym->address(); 517 std::cout << "Binary file defined IssMemchecker communication address to " 518 << m_comm_address << std::endl; 518 519 } 519 520 } 520 521 … … 585 586 std::vector<AddressInfo> &r = *(i->second); 586 587 if ( region_base <= address && word_no < r.size() ) 587 588 return &r[word_no]; 588 std::cout << "Warning: address " << std::hex << address589 << " " << std::dec << (r.size()-word_no) << " words beyond "590 << r[r.size()-1] << std::endl;589 // std::cout << "Warning: address " << std::hex << address 590 // << " " << std::dec << (r.size()-word_no) << " words beyond " 591 // << r[r.size()-1] << std::endl; 591 592 return &m_default_address; 592 593 } 593 594 … … 623 624 info_for_address(a)->region_set(nri); 624 625 } 625 626 626 std::stringget_symbol( uintptr_t addr ) const627 BinaryFileSymbolOffset get_symbol( uintptr_t addr ) const 627 628 { 628 return m_binary.get_symbol (addr);629 return m_binary.get_symbol_by_addr(addr); 629 630 } 630 631 }; 631 632 … … 655 656 656 657 template<typename iss_t> 657 658 void IssMemchecker<iss_t>::init( const soclib::common::MappingTable &mt, 658 const soclib::common:: ElfLoader &loader,659 const soclib::common::BinaryFileLoader &loader, 659 660 const std::string &exclusions ) 660 661 { 661 662 s_memory_state = new MemoryState( mt, loader, exclusions ); -
soclib/lib/elf_file_loader/metadata/elf_file_loader.sd
1 2 # -*- python -*- 3 4 Module('common:elf_file_loader', 5 implementation_files = ['../src/elf_file_loader.cpp', 6 ], 7 uses = [ 8 Uses('common:exception'), 9 Uses('common:binary_file_loader'), 10 ], 11 ) -
soclib/lib/elf_file_loader/src/elf_file_loader.cpp
1 /* -*- c++ -*- 2 * 3 * SOCLIB_LGPL_HEADER_BEGIN 4 * 5 * This file is part of SoCLib, GNU LGPLv2.1. 6 * 7 * SoCLib is free software; you can redistribute it and/or modify it 8 * under the terms of the GNU Lesser General Public License as published 9 * by the Free Software Foundation; version 2.1 of the License. 10 * 11 * SoCLib is distributed in the hope that it will be useful, but 12 * WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with SoCLib; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19 * 02110-1301 USA 20 * 21 * SOCLIB_LGPL_HEADER_END 22 * 23 * Copyright (c) UPMC, Lip6, SoC 24 * Nicolas Pouillon <nipo@ssji.net>, 2009 25 */ 26 27 extern "C" { 28 #include <bfd.h> 29 30 /* 31 To: "Robert Norton" <rnorton at broadcom dot com> 32 Cc: binutils at sourceware dot org 33 Subject: Re: BFD and Elf Symbol Size 34 From: Ian Lance Taylor <iant at google dot com> 35 Date: 19 Oct 2007 08:29:27 -0700 36 In-Reply-To: <B0D822BFECD50F4991F2516EA50F273C030480A5 at NT-IRVA-0752 dot brcm dot ad dot broadcom dot com> 37 Message-ID: <m33aw72kdk.fsf@localhost.localdomain> 38 39 "Robert Norton" <rnorton@broadcom.com> writes: 40 41 > Is there any way to get the size of an elf symbol via the BFD? 42 43 In general I would have to recommend against using BFD as a general 44 purpose library. 45 46 That said, sure, you can do anything. In this case, 47 48 #include "elf-bfd.h" 49 50 and then 51 52 ((elf_symbol_type *) symbol)->internal_elf_sym.st_size; 53 54 Ian 55 */ 56 57 // Unfortunately, elf-bfd.h is not a distributed header, therefore 58 // i'll copy here the only needed parts of the struct. 59 60 struct elf_internal_sym { 61 bfd_vma st_value; /* Value of the symbol */ 62 bfd_vma st_size; /* Associated symbol size */ 63 // Other fields I skipped 64 }; 65 66 typedef struct 67 { 68 asymbol symbol; 69 struct elf_internal_sym internal_elf_sym; 70 // Other fields I skipped 71 } elf_symbol_type; 72 73 } 74 75 #include <algorithm> 76 #include <string.h> 77 #include <sstream> 78 #include <cassert> 79 80 #include "exception.h" 81 #include "static_init_code.h" 82 #include "binary_file_loader.h" 83 84 namespace soclib { namespace common { namespace { 85 86 struct elf_state 87 { 88 uintptr_t mask; 89 BinaryFileLoader *loader; 90 }; 91 92 void add_section_cb(bfd *bfd, asection *sect, void *state_) 93 { 94 struct elf_state *state = (struct elf_state*)state_; 95 96 if ( !(sect->flags & SEC_ALLOC) ) 97 return; 98 99 bfd_byte *blob = 0; 100 if ( sect->flags & SEC_LOAD && sect->size ) { 101 int ret = bfd_malloc_and_get_section(bfd, sect, &blob); 102 if ( !ret || !blob ) 103 throw soclib::exception::RunTimeError( 104 std::string("BFD failed: ") + bfd_errmsg(bfd_get_error())); 105 } 106 107 uint32_t flags = 0; 108 if ( sect->flags & SEC_LOAD ) flags |= BinaryFileSection::FLAG_LOAD; 109 if ( sect->flags & SEC_READONLY ) flags |= BinaryFileSection::FLAG_READONLY; 110 if ( sect->flags & SEC_CODE ) flags |= BinaryFileSection::FLAG_CODE; 111 if ( sect->flags & SEC_DATA ) flags |= BinaryFileSection::FLAG_DATA; 112 113 state->loader->addSection( 114 BinaryFileSection( 115 sect->name, 116 sect->vma & state->mask, sect->lma & state->mask, 117 flags, 118 sect->size, blob )); 119 } 120 121 void read_symbols(struct bfd *abfd, struct elf_state *state) 122 { 123 long storage_needed; 124 asymbol **symbol_table; 125 long number_of_symbols; 126 long i; 127 128 storage_needed = bfd_get_symtab_upper_bound (abfd); 129 130 if (storage_needed <= 0) 131 return; 132 133 symbol_table = (asymbol**)malloc (storage_needed); 134 number_of_symbols = 135 bfd_canonicalize_symtab (abfd, symbol_table); 136 137 if (number_of_symbols < 0) 138 return; 139 140 for (i = 0; i < number_of_symbols; i++) { 141 asymbol *s = symbol_table[i]; 142 143 size_t symsize = ((elf_symbol_type *) s)->internal_elf_sym.st_size; 144 145 if ( ! (s->flags & (BSF_FUNCTION|BSF_LOCAL|BSF_GLOBAL)) ) 146 continue; 147 148 uintptr_t addr = ((s->section->lma & state->mask) + s->value); 149 state->loader->addSymbol(BinaryFileSymbol( s->name, addr, symsize )); 150 } 151 free(symbol_table); 152 } 153 154 bool elf_load( const std::string &filename, BinaryFileLoader &loader ) 155 { 156 static int s_refcount = 0; 157 if ( s_refcount == 0 ) 158 bfd_init(); 159 ++s_refcount; 160 161 struct elf_state state; 162 state.loader = &loader; 163 164 struct bfd *a_bfd = bfd_openr(filename.c_str(), NULL); 165 if ( !(bool)a_bfd ) 166 throw soclib::exception::RunTimeError( 167 std::string("Cant open binary image ")+filename); 168 169 if ( !bfd_check_format(a_bfd, bfd_object) 170 && !(a_bfd->flags & EXEC_P)) 171 throw soclib::exception::RunTimeError( 172 std::string("Invalid ELF format in image ")+filename); 173 174 state.mask = (uintptr_t)-1; 175 if ( (size_t)bfd_get_arch_size(a_bfd) < sizeof(uintptr_t)*8 ) 176 state.mask = ((uintptr_t)1<<bfd_get_arch_size(a_bfd))-1; 177 178 bfd_map_over_sections(a_bfd, 179 &add_section_cb, 180 &state); 181 182 read_symbols(a_bfd, &state); 183 184 if ( a_bfd ) 185 bfd_close(a_bfd); 186 187 return true; 188 } 189 190 STATIC_INIT_CODE( 191 BinaryFileLoader::register_loader("elf", elf_load); 192 ) 193 194 }}} 195 196 // Local Variables: 197 // tab-width: 4 198 // c-basic-offset: 4 199 // c-file-offsets:((innamespace . 0)(inline-open . 0)) 200 // indent-tabs-mode: nil 201 // End: 202 203 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 204 -
soclib/lib/elf_loader/metadata/elf_loader.sd
Property changes on: soclib/lib/elf_file_loader/src/elf_file_loader.cpp ___________________________________________________________________ Name: svn:eol-style + native
4 4 Module('common:elf_loader', 5 5 classname = 'soclib::common::ElfLoader', 6 6 header_files = ['../include/elf_loader.h',], 7 implementation_files = ['../src/elf_loader.cpp',8 '../src/elf_section.cpp',9 ],10 7 instance_parameters = [ 11 8 parameter.String('binary', auto = 'env:binary'), 12 9 ], 13 10 uses = [ 14 Uses('common:exception'), 11 Uses('common:binary_file_loader'), 12 Uses('common:elf_file_loader'), 15 13 ], 16 14 extensions = [ 17 15 'dsx:configurator', 18 16 'dsx:noname' 19 ] 17 ], 18 deprecated = "Please use binary_file_loader now.", 20 19 ) -
soclib/lib/elf_loader/include/elf_loader.h
28 28 #ifndef SOCLIB_ELF_LOADER_H_ 29 29 #define SOCLIB_ELF_LOADER_H_ 30 30 31 #include <string> 32 #include <vector> 33 #include <map> 34 #include "stdint.h" 31 #include "binary_file_loader.h" 35 32 36 33 namespace soclib { namespace common { 37 34 38 class ElfSectionData;39 classElfLoader;35 typedef BinaryFileSymbol ElfSymbol; 36 typedef BinaryFileLoader ElfLoader; 40 37 41 class ElfSection42 {43 friend class ElfLoader;44 45 ElfSectionData *m_data;46 uint32_t m_flags;47 std::string m_name;48 uintptr_t m_vma;49 uintptr_t m_lma;50 size_t m_size;51 52 public:53 bool load_overlap_in_buffer( void *buffer,54 uintptr_t buffer_base_address,55 uintptr_t buffer_size ) const;56 void get_data( void *buffer ) const;57 58 bool has_data() const;59 60 bool flag_load() const;61 bool flag_read_only() const;62 bool flag_code() const;63 bool flag_data() const;64 const std::string& name() const;65 uintptr_t vma() const;66 uintptr_t lma() const;67 size_t size() const;68 69 ElfSection( const ElfSection & );70 ~ElfSection();71 ElfSection &operator=( const ElfSection & );72 ElfSection();73 74 void print( std::ostream &o ) const;75 76 friend std::ostream &operator<<( std::ostream &o, const ElfSection &s )77 {78 s.print(o);79 return o;80 }81 82 private:83 ElfSection( const std::string &name,84 uintptr_t vma, uintptr_t lma,85 uint32_t flags,86 size_t data_size, void *given_data_ptr );87 };88 89 class ElfLoader90 {91 static int s_refcount;92 public:93 typedef std::vector<ElfSection> section_list_t;94 95 private:96 std::string m_filename;97 void *m_bfd_ptr;98 section_list_t m_sections;99 uintptr_t m_mask;100 std::map<uintptr_t, std::pair<size_t, std::string> > m_symbol_table;101 102 void read_symbols();103 104 static void add_section_cb( void *bfd, void *sect, void *thisptr );105 void add_section( void *bfd, void *sect );106 107 public:108 std::vector<ElfSection> sections() const;109 110 ElfLoader( const ElfLoader &ref );111 ElfLoader( const std::string &filename );112 void load( void *buffer, uintptr_t address, size_t length );113 ~ElfLoader();114 115 std::string arch() const;116 117 void print( std::ostream &o ) const;118 119 std::string get_symbol( uintptr_t addr ) const;120 uintptr_t get_symbol_addr( const std::string & ) const;121 122 inline const std::string & filename() const123 {124 return m_filename;125 }126 127 friend std::ostream &operator << (std::ostream &o, const ElfLoader &el)128 {129 el.print(o);130 return o;131 }132 };133 134 38 }} 135 39 136 40 #endif /* SOCLIB_ELF_LOADER_H_ */ -
soclib/lib/elf_loader/src/elf_loader.cpp
1 /* -*- c++ -*-2 *3 * SOCLIB_LGPL_HEADER_BEGIN4 *5 * This file is part of SoCLib, GNU LGPLv2.1.6 *7 * SoCLib is free software; you can redistribute it and/or modify it8 * under the terms of the GNU Lesser General Public License as published9 * by the Free Software Foundation; version 2.1 of the License.10 *11 * SoCLib is distributed in the hope that it will be useful, but12 * WITHOUT ANY WARRANTY; without even the implied warranty of13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU14 * Lesser General Public License for more details.15 *16 * You should have received a copy of the GNU Lesser General Public17 * License along with SoCLib; if not, write to the Free Software18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA19 * 02110-1301 USA20 *21 * SOCLIB_LGPL_HEADER_END22 *23 * Copyright (c) UPMC, Lip6, SoC24 * Nicolas Pouillon <nipo@ssji.net>, 200725 */26 27 extern "C" {28 #include <bfd.h>29 30 // To: "Robert Norton" <rnorton at broadcom dot com>31 // Cc: binutils at sourceware dot org32 // Subject: Re: BFD and Elf Symbol Size33 // From: Ian Lance Taylor <iant at google dot com>34 // Date: 19 Oct 2007 08:29:27 -070035 // In-Reply-To: <B0D822BFECD50F4991F2516EA50F273C030480A5 at NT-IRVA-0752 dot brcm dot ad dot broadcom dot com>36 // Message-ID: <m33aw72kdk.fsf@localhost.localdomain>37 //38 // "Robert Norton" <rnorton@broadcom.com> writes:39 //40 // > Is there any way to get the size of an elf symbol via the BFD?41 //42 // In general I would have to recommend against using BFD as a general43 // purpose library.44 //45 // That said, sure, you can do anything. In this case,46 //47 // #include "elf-bfd.h"48 //49 // and then50 //51 // ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;52 //53 // Ian54 55 // Unfortunately, elf-bfd.h is not a distributed header, therefore56 // i'll copy here the only needed parts of the struct.57 58 struct elf_internal_sym {59 bfd_vma st_value; /* Value of the symbol */60 bfd_vma st_size; /* Associated symbol size */61 // Other fields I skipped62 };63 64 typedef struct65 {66 asymbol symbol;67 struct elf_internal_sym internal_elf_sym;68 // Other fields I skipped69 } elf_symbol_type;70 71 }72 73 #include <algorithm>74 #include <string.h>75 #include <sstream>76 #include <cassert>77 78 #include "exception.h"79 #include "elf_loader.h"80 81 namespace soclib { namespace common {82 83 int ElfLoader::s_refcount = 0;84 85 void ElfLoader::add_section_cb(void *bfd, void *_sect, void *thisptr)86 {87 static_cast<ElfLoader*>(thisptr)->add_section(bfd, _sect);88 }89 90 void ElfLoader::add_section(void *_bfd, void *_sect)91 {92 bfd *a_bfd = (bfd*)_bfd;93 asection *sect = (asection*)_sect;94 95 if ( !(sect->flags & SEC_ALLOC) )96 return;97 98 bfd_byte *blob = 0;99 if ( sect->flags & SEC_LOAD && sect->size ) {100 int ret = bfd_malloc_and_get_section(a_bfd, sect, &blob);101 if ( !ret || !blob )102 throw soclib::exception::RunTimeError(103 std::string("BFD failed: ") + bfd_errmsg(bfd_get_error()));104 }105 106 m_sections.push_back(ElfSection(107 sect->name,108 sect->vma & m_mask, sect->lma & m_mask,109 sect->flags,110 sect->size, blob ));111 }112 113 void ElfLoader::read_symbols()114 {115 struct bfd *abfd = (struct bfd*)m_bfd_ptr;116 long storage_needed;117 asymbol **symbol_table;118 long number_of_symbols;119 long i;120 121 storage_needed = bfd_get_symtab_upper_bound (abfd);122 123 if (storage_needed <= 0)124 return;125 126 symbol_table = (asymbol**)malloc (storage_needed);127 number_of_symbols =128 bfd_canonicalize_symtab (abfd, symbol_table);129 130 if (number_of_symbols < 0)131 return;132 133 for (i = 0; i < number_of_symbols; i++) {134 asymbol *s = symbol_table[i];135 136 size_t symsize = ((elf_symbol_type *) s)->internal_elf_sym.st_size;137 138 if ( ! (s->flags & (BSF_FUNCTION|BSF_LOCAL|BSF_GLOBAL)) )139 continue;140 141 uintptr_t addr = ((s->section->lma & m_mask) + s->value);142 m_symbol_table[addr] = std::pair<size_t, std::string>(symsize, std::string(s->name));143 }144 free(symbol_table);145 }146 147 ElfLoader::ElfLoader( const std::string &filename )148 : m_filename(filename)149 {150 if ( s_refcount == 0 )151 bfd_init();152 ++s_refcount;153 154 struct bfd *a_bfd = bfd_openr(m_filename.c_str(), NULL);155 if ( !(bool)a_bfd )156 throw soclib::exception::RunTimeError(157 std::string("Cant open binary image ")+m_filename);158 159 if ( !bfd_check_format(a_bfd, bfd_object)160 && !(a_bfd->flags & EXEC_P))161 throw soclib::exception::RunTimeError(162 std::string("Invalid ELF format in image ")+m_filename);163 m_bfd_ptr = (void*)a_bfd;164 165 m_mask = (uintptr_t)-1;166 if ( (size_t)bfd_get_arch_size(a_bfd) < sizeof(uintptr_t)*8 )167 m_mask = ((uintptr_t)1<<bfd_get_arch_size(a_bfd))-1;168 169 m_sections.reserve(a_bfd->section_count);170 171 bfd_map_over_sections(a_bfd,172 (void(*)(bfd*, asection*, void*))173 &ElfLoader::add_section_cb,174 this);175 176 read_symbols();177 }178 179 ElfLoader::ElfLoader( const ElfLoader &ref )180 : m_filename(ref.m_filename),181 m_sections(ref.m_sections),182 m_mask(ref.m_mask),183 m_symbol_table(ref.m_symbol_table)184 {185 if ( s_refcount == 0 )186 bfd_init();187 ++s_refcount;188 189 struct bfd *a_bfd = bfd_openr(m_filename.c_str(), NULL);190 if ( !(bool)a_bfd )191 throw soclib::exception::RunTimeError(192 std::string("Cant open binary image ")+m_filename);193 194 if ( !bfd_check_format(a_bfd, bfd_object)195 && !(a_bfd->flags & EXEC_P))196 throw soclib::exception::RunTimeError(197 std::string("Invalid ELF format in image ")+m_filename);198 m_bfd_ptr = (void*)a_bfd;199 }200 201 void ElfLoader::load( void *buffer, uintptr_t address, size_t length )202 {203 std::cout204 << std::showbase << std::hex205 << "Loading at " << address206 << " size " << std::dec << length207 << ": ";208 bool one = false;209 for ( section_list_t::const_iterator i = m_sections.begin();210 i != m_sections.end();211 ++i ) {212 if ( i->load_overlap_in_buffer( buffer, address, length ) ) {213 one = true;214 std::cout << i->name() << " ";215 }216 }217 if ( !one )218 std::cout << "nothing";219 std::cout << std::endl;220 }221 222 void ElfLoader::print( std::ostream &o ) const223 {224 struct bfd* a_bfd = (struct bfd*)m_bfd_ptr;225 o << "<ElfLoader " << m_filename << std::endl226 << " target: " << arch() << std::endl227 << " endianness: " << a_bfd->xvec->byteorder << std::endl;228 for ( section_list_t::const_iterator i = m_sections.begin();229 i != m_sections.end();230 ++i )231 o << " " << *i << std::endl;232 o << ">" << std::endl;233 }234 235 std::string ElfLoader::arch() const236 {237 struct bfd *a_bfd = (struct bfd*)m_bfd_ptr;238 enum bfd_architecture arch = bfd_get_arch (a_bfd);239 std::cout << "Arch " << (int)arch << std::endl;240 switch(arch) {241 case bfd_arch_mips:242 return "mipsel";243 case bfd_arch_powerpc:244 return "powerpc";245 default:246 return "unknown";247 }248 }249 250 ElfLoader::~ElfLoader()251 {252 struct bfd* a_bfd = (struct bfd*)m_bfd_ptr;253 if ( a_bfd )254 bfd_close(a_bfd);255 256 // if ( --s_refcount == 0 )257 // bfd_fini();258 }259 260 std::vector<ElfSection> ElfLoader::sections() const261 {262 return m_sections;263 }264 265 std::string ElfLoader::get_symbol( uintptr_t addr ) const266 {267 addr &= m_mask;268 std::map<uintptr_t, std::pair<size_t, std::string> >::const_iterator i =269 m_symbol_table.lower_bound( addr );270 271 if ( i != m_symbol_table.end()272 && i != m_symbol_table.begin()273 && i->first > addr )274 --i;275 276 std::ostringstream o;277 o << "[@" << std::showbase << std::hex278 << addr << ": ";279 if ( i == m_symbol_table.end() || ( i->second.first != 0 && i->first+i->second.first <= addr ) ) {280 o << "<unknown>";281 } else {282 ptrdiff_t d = addr - i->first;283 o << '(' << i->second.second << " + " << d << ')';284 }285 o << "]";286 return o.str();287 }288 289 uintptr_t ElfLoader::get_symbol_addr( const std::string &sym ) const290 {291 std::map<uintptr_t, std::pair<size_t, std::string> >::const_iterator i;292 for ( i = m_symbol_table.begin();293 i != m_symbol_table.end();294 ++i ) {295 if ( i->second.second == sym )296 return i->first;297 }298 return 0;299 }300 301 }}302 303 // Local Variables:304 // tab-width: 4305 // c-basic-offset: 4306 // c-file-offsets:((innamespace . 0)(inline-open . 0))307 // indent-tabs-mode: nil308 // End:309 310 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4311 -
soclib/lib/binary_file_loader/metadata/binary_file_section.sd
1 2 # -*- python -*- 3 4 Module('common:binary_file_section', 5 classname = 'soclib::common::BinaryFileSection', 6 header_files = ['../include/binary_file_section.h',], 7 implementation_files = ['../src/binary_file_section.cpp', 8 ], 9 ) -
soclib/lib/binary_file_loader/metadata/binary_file_symbol.sd
1 2 # -*- python -*- 3 4 Module('common:binary_file_symbol', 5 classname = 'soclib::common::BinaryFileSymbol', 6 header_files = ['../include/binary_file_symbol.h',], 7 implementation_files = ['../src/binary_file_symbol.cpp', 8 ], 9 ) -
soclib/lib/binary_file_loader/metadata/binary_file_loader.sd
1 2 # -*- python -*- 3 4 Module('common:binary_file_loader', 5 classname = 'soclib::common::BinaryFileLoader', 6 header_files = ['../include/binary_file_loader.h',], 7 implementation_files = ['../src/binary_file_loader.cpp', 8 ], 9 instance_parameters = [ 10 parameter.String('binary', auto = 'env:binary'), 11 ], 12 uses = [ 13 Uses('common:exception'), 14 Uses('common:binary_file_section'), 15 Uses('common:binary_file_symbol'), 16 ], 17 extensions = [ 18 'dsx:configurator', 19 'dsx:noname' 20 ] 21 ) -
soclib/lib/binary_file_loader/include/binary_file_loader.h
1 /* -*- c++ -*- 2 * 3 * SOCLIB_LGPL_HEADER_BEGIN 4 * 5 * This file is part of SoCLib, GNU LGPLv2.1. 6 * 7 * SoCLib is free software; you can redistribute it and/or modify it 8 * under the terms of the GNU Lesser General Public License as published 9 * by the Free Software Foundation; version 2.1 of the License. 10 * 11 * SoCLib is distributed in the hope that it will be useful, but 12 * WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with SoCLib; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19 * 02110-1301 USA 20 * 21 * SOCLIB_LGPL_HEADER_END 22 * 23 * Copyright (c) UPMC, Lip6, SoC 24 * Nicolas Pouillon <nipo@ssji.net>, 2009 25 * 26 * Maintainers: nipo 27 */ 28 #ifndef SOCLIB_BINARY_LOADER_H_ 29 #define SOCLIB_BINARY_LOADER_H_ 30 31 #include <string> 32 #include <vector> 33 #include <map> 34 #include "stdint.h" 35 36 #include "binary_file_symbol.h" 37 #include "binary_file_section.h" 38 39 namespace soclib { namespace common { 40 41 class BinaryFileLoader 42 { 43 public: 44 typedef std::vector<BinaryFileSection> section_list_t; 45 typedef bool (*binary_loader_t)( const std::string &desc_str, BinaryFileLoader &loader ); 46 47 private: 48 typedef std::map<std::string, binary_loader_t> loader_registry_t; 49 50 section_list_t m_sections; 51 uintptr_t m_mask; 52 std::map<uintptr_t, BinaryFileSymbol> m_symbol_table; 53 54 void load_file(const std::string &desc_str); 55 56 static loader_registry_t ®istry(); 57 public: 58 static void register_loader( const std::string &name, 59 binary_loader_t loader ); 60 61 section_list_t sections() const; 62 63 void addSection( const BinaryFileSection §ion ); 64 void addSymbol( const BinaryFileSymbol &symbol ); 65 66 BinaryFileLoader( const BinaryFileLoader &ref ); 67 BinaryFileLoader( const std::string &filename ); 68 void load( void *buffer, uintptr_t address, size_t length ); 69 ~BinaryFileLoader(); 70 71 void print( std::ostream &o ) const; 72 73 BinaryFileSymbolOffset get_symbol_by_addr( uintptr_t addr ) const; 74 const BinaryFileSymbol *get_symbol_by_name( const std::string & ) const; 75 76 friend std::ostream &operator << (std::ostream &o, const BinaryFileLoader &el) 77 { 78 el.print(o); 79 return o; 80 } 81 }; 82 83 }} 84 85 #endif /* SOCLIB_BINARY_LOADER_H_ */ 86 87 // Local Variables: 88 // tab-width: 4 89 // c-basic-offset: 4 90 // c-file-offsets:((innamespace . 0)(inline-open . 0)) 91 // indent-tabs-mode: nil 92 // End: 93 94 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 95 -
soclib/lib/binary_file_loader/include/binary_file_section.h
Property changes on: soclib/lib/binary_file_loader/include/binary_file_loader.h ___________________________________________________________________ Name: svn:eol-style + native
1 /* -*- c++ -*- 2 * 3 * SOCLIB_LGPL_HEADER_BEGIN 4 * 5 * This file is part of SoCLib, GNU LGPLv2.1. 6 * 7 * SoCLib is free software; you can redistribute it and/or modify it 8 * under the terms of the GNU Lesser General Public License as published 9 * by the Free Software Foundation; version 2.1 of the License. 10 * 11 * SoCLib is distributed in the hope that it will be useful, but 12 * WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with SoCLib; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19 * 02110-1301 USA 20 * 21 * SOCLIB_LGPL_HEADER_END 22 * 23 * Copyright (c) UPMC, Lip6, SoC 24 * Nicolas Pouillon <nipo@ssji.net>, 2007 25 * 26 * Maintainers: nipo 27 */ 28 #ifndef SOCLIB_BINARY_FILE_SECTION_H_ 29 #define SOCLIB_BINARY_FILE_SECTION_H_ 30 31 #include <string> 32 #include <ios> 33 34 #include "stdint.h" 35 36 #include "binary_file_section.h" 37 38 namespace soclib { namespace common { 39 40 class BinarySectionData; 41 class BinaryFileLoader; 42 43 class BinaryFileSection 44 { 45 friend class BinaryFileLoader; 46 47 BinarySectionData *m_data; 48 uint32_t m_flags; 49 std::string m_name; 50 uintptr_t m_vma; 51 uintptr_t m_lma; 52 size_t m_size; 53 54 public: 55 enum flags { 56 FLAG_LOAD = 1, 57 FLAG_DATA = 2, 58 FLAG_READONLY = 4, 59 FLAG_CODE = 8, 60 }; 61 62 bool load_overlap_in_buffer( void *buffer, 63 uintptr_t buffer_base_address, 64 uintptr_t buffer_size ) const; 65 void get_data( void *buffer ) const; 66 67 bool has_data() const; 68 69 bool flag_load() const; 70 bool flag_read_only() const; 71 bool flag_code() const; 72 bool flag_data() const; 73 const std::string& name() const; 74 uintptr_t vma() const; 75 uintptr_t lma() const; 76 size_t size() const; 77 78 BinaryFileSection( const BinaryFileSection & ); 79 ~BinaryFileSection(); 80 BinaryFileSection &operator=( const BinaryFileSection & ); 81 BinaryFileSection(); 82 83 void print( std::ostream &o ) const; 84 85 friend std::ostream &operator<<( std::ostream &o, const BinaryFileSection &s ) 86 { 87 s.print(o); 88 return o; 89 } 90 91 BinaryFileSection( const std::string &name, 92 uintptr_t vma, uintptr_t lma, 93 uint32_t flags, 94 size_t data_size, void *given_data_ptr ); 95 }; 96 97 }} 98 99 #endif /* SOCLIB_BINARY_FILE_SECTION_H_ */ 100 101 // Local Variables: 102 // tab-width: 4 103 // c-basic-offset: 4 104 // c-file-offsets:((innamespace . 0)(inline-open . 0)) 105 // indent-tabs-mode: nil 106 // End: 107 108 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 109 -
soclib/lib/binary_file_loader/include/binary_file_symbol.h
Property changes on: soclib/lib/binary_file_loader/include/binary_file_section.h ___________________________________________________________________ Name: svn:eol-style + native
1 /* -*- c++ -*- 2 * 3 * SOCLIB_LGPL_HEADER_BEGIN 4 * 5 * This file is part of SoCLib, GNU LGPLv2.1. 6 * 7 * SoCLib is free software; you can redistribute it and/or modify it 8 * under the terms of the GNU Lesser General Public License as published 9 * by the Free Software Foundation; version 2.1 of the License. 10 * 11 * SoCLib is distributed in the hope that it will be useful, but 12 * WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with SoCLib; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19 * 02110-1301 USA 20 * 21 * SOCLIB_LGPL_HEADER_END 22 * 23 * Copyright (c) UPMC, Lip6, SoC 24 * Nicolas Pouillon <nipo@ssji.net>, 2009 25 * 26 * Maintainers: nipo 27 */ 28 #ifndef SOCLIB_BINARY_FILE_SYMBOL_H_ 29 #define SOCLIB_BINARY_FILE_SYMBOL_H_ 30 31 #include <string> 32 #include "stdint.h" 33 #include <ios> 34 35 namespace soclib { namespace common { 36 37 class BinaryFileSymbol 38 { 39 std::string m_name; 40 uintptr_t m_address; 41 size_t m_size; 42 public: 43 const std::string &name() const 44 { 45 return m_name; 46 } 47 48 const uintptr_t address() const 49 { 50 return m_address; 51 } 52 53 const size_t size() const 54 { 55 return m_size; 56 } 57 58 BinaryFileSymbol( const std::string &name, uintptr_t address, size_t size ); 59 60 BinaryFileSymbol( const BinaryFileSymbol &ref ); 61 62 BinaryFileSymbol(); 63 64 const BinaryFileSymbol& operator=( const BinaryFileSymbol &ref ); 65 66 bool contains( uintptr_t addr ) const; 67 }; 68 69 class BinaryFileSymbolOffset 70 { 71 const BinaryFileSymbol m_sym; 72 const ptrdiff_t m_addr; 73 74 public: 75 BinaryFileSymbolOffset( const BinaryFileSymbol sym, const ptrdiff_t addr ); 76 77 BinaryFileSymbolOffset( const BinaryFileSymbolOffset &ref ); 78 79 void print( std::ostream &o ) const; 80 81 friend std::ostream & operator<<( std::ostream &o, const BinaryFileSymbolOffset &so ) 82 { 83 so.print(o); 84 return o; 85 } 86 }; 87 88 }} 89 90 #endif /* SOCLIB_BINARY_LOADER_H_ */ 91 92 // Local Variables: 93 // tab-width: 4 94 // c-basic-offset: 4 95 // c-file-offsets:((innamespace . 0)(inline-open . 0)) 96 // indent-tabs-mode: nil 97 // End: 98 99 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 100 -
soclib/lib/binary_file_loader/src/binary_file_section.cpp
Property changes on: soclib/lib/binary_file_loader/include/binary_file_symbol.h ___________________________________________________________________ Name: svn:eol-style + native
1 /* -*- c++ -*- 2 * 3 * SOCLIB_LGPL_HEADER_BEGIN 4 * 5 * This file is part of SoCLib, GNU LGPLv2.1. 6 * 7 * SoCLib is free software; you can redistribute it and/or modify it 8 * under the terms of the GNU Lesser General Public License as published 9 * by the Free Software Foundation; version 2.1 of the License. 10 * 11 * SoCLib is distributed in the hope that it will be useful, but 12 * WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with SoCLib; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19 * 02110-1301 USA 20 * 21 * SOCLIB_LGPL_HEADER_END 22 * 23 * Copyright (c) UPMC, Lip6, SoC 24 * Nicolas Pouillon <nipo@ssji.net>, 2009 25 */ 26 27 #include <algorithm> 28 #include <string.h> 29 #include <cassert> 30 31 #include <iostream> 32 33 #include "binary_file_section.h" 34 35 namespace soclib { namespace common { 36 37 class BinarySectionData 38 { 39 uint32_t m_refcount; 40 size_t m_size; 41 void *m_data; 42 public: 43 BinarySectionData( size_t size, void *data ) 44 : m_refcount(0), 45 m_size(size), 46 m_data(data) 47 {} 48 49 ~BinarySectionData() 50 { 51 free(m_data); 52 } 53 54 void *data() const 55 { 56 return m_data; 57 } 58 59 size_t size() const 60 { 61 return m_size; 62 } 63 64 void ref() 65 { 66 m_refcount++; 67 } 68 69 void check() const 70 { 71 assert(m_refcount); 72 } 73 74 void unref() 75 { 76 if ( --m_refcount == 0 ) 77 delete this; 78 } 79 80 void print( std::ostream &o ) const 81 { 82 o << "<BinarySectionData " << std::dec 83 << m_size << " bytes" 84 << ", refcount=" << m_refcount 85 << ">"; 86 } 87 88 friend std::ostream &operator << (std::ostream &o, const BinarySectionData &d) 89 { 90 d.print(o); 91 return o; 92 } 93 94 }; 95 96 const std::string & BinaryFileSection::name() const 97 { 98 return m_name; 99 } 100 101 uintptr_t BinaryFileSection::vma() const 102 { 103 return m_vma; 104 } 105 106 uintptr_t BinaryFileSection::lma() const 107 { 108 return m_lma; 109 } 110 111 size_t BinaryFileSection::size() const 112 { 113 return m_size; 114 } 115 116 bool BinaryFileSection::has_data() const 117 { 118 return m_data; 119 } 120 121 bool BinaryFileSection::flag_load() const 122 { 123 return m_flags & FLAG_LOAD; 124 } 125 126 bool BinaryFileSection::flag_read_only() const 127 { 128 return m_flags & FLAG_READONLY; 129 } 130 131 bool BinaryFileSection::flag_code() const 132 { 133 return m_flags & FLAG_CODE; 134 } 135 136 bool BinaryFileSection::flag_data() const 137 { 138 return m_flags & FLAG_DATA; 139 } 140 141 BinaryFileSection::BinaryFileSection( const std::string &name, 142 uintptr_t vma, uintptr_t lma, 143 uint32_t flags, size_t size, 144 void *blob ) 145 : m_data( blob ? new BinarySectionData( size, blob ) : 0 ), 146 m_flags(flags), 147 m_name(name), 148 m_vma(vma), 149 m_lma(lma), 150 m_size(size) 151 { 152 if ( m_data ) 153 m_data->ref(); 154 if ( m_data ) 155 m_data->check(); 156 157 // std::cout << "New BinaryFileSection " << *this << std::endl; 158 } 159 160 bool BinaryFileSection::load_overlap_in_buffer( 161 void *buffer, 162 uintptr_t buffer_base_address, 163 size_t buffer_size ) const 164 { 165 if ( !m_data ) 166 return false; 167 168 void *src, *dst; 169 size_t copy_size; 170 if ( buffer_base_address < m_lma ) { 171 size_t dest_buffer_offset = m_lma - buffer_base_address; 172 173 dst = (void*)((uintptr_t)buffer + dest_buffer_offset); 174 src = m_data->data(); 175 if ( dest_buffer_offset > buffer_size ) 176 copy_size = 0; 177 else 178 copy_size = buffer_size - dest_buffer_offset; 179 if ( copy_size > m_data->size() ) 180 copy_size = m_data->size(); 181 } else { 182 size_t src_data_offset = buffer_base_address - m_lma; 183 184 dst = buffer; 185 src = (void*)((uintptr_t)m_data->data() + src_data_offset); 186 if ( src_data_offset > m_data->size() ) 187 copy_size = 0; 188 else 189 copy_size = m_data->size() - src_data_offset; 190 if ( copy_size > buffer_size ) 191 copy_size = buffer_size; 192 } 193 if ( copy_size == 0 ) 194 return false; 195 if ( m_data ) 196 m_data->check(); 197 memcpy( dst, src, copy_size ); 198 return true; 199 } 200 201 void BinaryFileSection::print( std::ostream &o ) const 202 { 203 o << "<Section " 204 << std::showbase << m_name 205 << ", vma: " << std::hex << m_vma 206 << ", lma: " << std::hex << m_lma 207 << ", size: " << std::dec << m_size << " bytes"; 208 if ( m_data ) { 209 m_data->check(); 210 o << ", " << *m_data; 211 } else { 212 o << ", no data"; 213 } 214 o << ", flags: "; 215 if ( flag_load() ) 216 o << "L,"; 217 if ( flag_read_only() ) 218 o << "RO,"; 219 if ( flag_code() ) 220 o << "C"; 221 if ( flag_data() ) 222 o << "D"; 223 o << ">"; 224 } 225 226 void BinaryFileSection::get_data( void *buffer ) const 227 { 228 if ( !m_data ) 229 return; 230 m_data->check(); 231 memcpy( buffer, m_data->data(), m_data->size() ); 232 } 233 234 BinaryFileSection::~BinaryFileSection() 235 { 236 // std::cout << "Deleted BinaryFileSection " << *this << std::endl; 237 if ( m_data ) 238 m_data->check(); 239 if ( m_data ) 240 m_data->unref(); 241 } 242 243 BinaryFileSection::BinaryFileSection( const BinaryFileSection &ref ) 244 : m_data(0), 245 m_flags(0), 246 m_name("To be copied"), 247 m_vma(0), 248 m_lma(0), 249 m_size(0) 250 { 251 // std::cout << "New BinaryFileSection " << *this << " copied from " << ref << std::endl; 252 (*this) = ref; 253 } 254 255 BinaryFileSection::BinaryFileSection() 256 : m_data(0), 257 m_flags(0), 258 m_name("Empty section"), 259 m_vma(0), 260 m_lma(0), 261 m_size(0) 262 { 263 // std::cout << "New empty BinaryFileSection " << *this << std::endl; 264 } 265 266 BinaryFileSection & BinaryFileSection::operator=( const BinaryFileSection &ref ) 267 { 268 if ( &ref == this ) 269 return *this; 270 271 // std::cout << "Copying " << ref << " to " << *this << std::endl; 272 273 if ( m_data ) { 274 m_data->check(); 275 m_data->unref(); 276 } 277 if ( ref.m_data ) { 278 m_data = ref.m_data; 279 m_data->ref(); 280 m_data->check(); 281 } else { 282 m_data = 0; 283 } 284 m_flags = ref.m_flags; 285 m_name = ref.m_name; 286 m_vma = ref.m_vma; 287 m_lma = ref.m_lma; 288 m_size = ref.m_size; 289 290 return *this; 291 } 292 293 294 }} 295 296 // Local Variables: 297 // tab-width: 4 298 // c-basic-offset: 4 299 // c-file-offsets:((innamespace . 0)(inline-open . 0)) 300 // indent-tabs-mode: nil 301 // End: 302 303 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 304 -
soclib/lib/binary_file_loader/src/binary_file_symbol.cpp
Property changes on: soclib/lib/binary_file_loader/src/binary_file_section.cpp ___________________________________________________________________ Name: svn:eol-style + native
1 /* -*- c++ -*- 2 * 3 * SOCLIB_LGPL_HEADER_BEGIN 4 * 5 * This file is part of SoCLib, GNU LGPLv2.1. 6 * 7 * SoCLib is free software; you can redistribute it and/or modify it 8 * under the terms of the GNU Lesser General Public License as published 9 * by the Free Software Foundation; version 2.1 of the License. 10 * 11 * SoCLib is distributed in the hope that it will be useful, but 12 * WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with SoCLib; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19 * 02110-1301 USA 20 * 21 * SOCLIB_LGPL_HEADER_END 22 * 23 * Copyright (c) UPMC, Lip6, SoC 24 * Nicolas Pouillon <nipo@ssji.net>, 2009 25 */ 26 27 #include <algorithm> 28 #include <string.h> 29 #include <cassert> 30 31 #include <iostream> 32 #include <iomanip> 33 34 #include "binary_file_symbol.h" 35 36 namespace soclib { namespace common { 37 38 BinaryFileSymbol::BinaryFileSymbol( const std::string &name, uintptr_t address, size_t size ) 39 : m_name(name), 40 m_address(address), 41 m_size(size) 42 {} 43 44 BinaryFileSymbol::BinaryFileSymbol( const BinaryFileSymbol &ref ) 45 : m_name(ref.m_name), 46 m_address(ref.m_address), 47 m_size(ref.m_size) 48 {} 49 50 BinaryFileSymbol::BinaryFileSymbol() 51 : m_name("Unknown"), 52 m_address(0), 53 m_size(0) 54 {} 55 56 const BinaryFileSymbol& BinaryFileSymbol::operator=( const BinaryFileSymbol &ref ) 57 { 58 m_name = ref.m_name; 59 m_address = ref.m_address; 60 m_size = ref.m_size; 61 return *this; 62 } 63 64 bool BinaryFileSymbol::contains( uintptr_t addr ) const 65 { 66 return addr >= m_address && addr < m_address+m_size; 67 } 68 69 BinaryFileSymbolOffset::BinaryFileSymbolOffset( const BinaryFileSymbol sym, const ptrdiff_t addr ) 70 : m_sym(sym), 71 m_addr(addr) 72 { 73 } 74 75 BinaryFileSymbolOffset::BinaryFileSymbolOffset( const BinaryFileSymbolOffset &ref ) 76 : m_sym(ref.m_sym), 77 m_addr(ref.m_addr) 78 { 79 } 80 81 void BinaryFileSymbolOffset::print( std::ostream &o ) const 82 { 83 o << "[@" << std::showbase << std::hex 84 << m_addr << ": "; 85 ptrdiff_t d = m_addr - m_sym.address(); 86 if ( d < m_sym.size() ) 87 o << '(' << m_sym.name() << " + " << d << ')'; 88 else 89 o << '(' << m_sym.name() << ", " << d-m_sym.size() << " bytes above)"; 90 o << "]"; 91 } 92 93 }} 94 95 // Local Variables: 96 // tab-width: 4 97 // c-basic-offset: 4 98 // c-file-offsets:((innamespace . 0)(inline-open . 0)) 99 // indent-tabs-mode: nil 100 // End: 101 102 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 103 -
soclib/lib/binary_file_loader/src/binary_file_loader.cpp
Property changes on: soclib/lib/binary_file_loader/src/binary_file_symbol.cpp ___________________________________________________________________ Name: svn:eol-style + native
1 /* -*- c++ -*- 2 * 3 * SOCLIB_LGPL_HEADER_BEGIN 4 * 5 * This file is part of SoCLib, GNU LGPLv2.1. 6 * 7 * SoCLib is free software; you can redistribute it and/or modify it 8 * under the terms of the GNU Lesser General Public License as published 9 * by the Free Software Foundation; version 2.1 of the License. 10 * 11 * SoCLib is distributed in the hope that it will be useful, but 12 * WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with SoCLib; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19 * 02110-1301 USA 20 * 21 * SOCLIB_LGPL_HEADER_END 22 * 23 * Copyright (c) UPMC, Lip6, SoC 24 * Nicolas Pouillon <nipo@ssji.net>, 2009 25 */ 26 27 #include <algorithm> 28 #include <string.h> 29 #include <sstream> 30 #include <cassert> 31 32 #include "exception.h" 33 #include "binary_file_loader.h" 34 35 namespace soclib { namespace common { 36 37 BinaryFileLoader::BinaryFileLoader( const std::string &filename ) 38 { 39 load_file(filename); 40 } 41 42 BinaryFileLoader::BinaryFileLoader( const BinaryFileLoader &ref ) 43 : m_sections(ref.m_sections), 44 m_symbol_table(ref.m_symbol_table) 45 { 46 } 47 48 void BinaryFileLoader::load( void *buffer, uintptr_t address, size_t length ) 49 { 50 std::cout 51 << std::showbase << std::hex 52 << "Loading at " << address 53 << " size " << std::dec << length 54 << ": "; 55 bool one = false; 56 for ( section_list_t::const_iterator i = m_sections.begin(); 57 i != m_sections.end(); 58 ++i ) { 59 if ( i->load_overlap_in_buffer( buffer, address, length ) ) { 60 one = true; 61 std::cout << i->name() << " "; 62 } 63 } 64 if ( !one ) 65 std::cout << "nothing"; 66 std::cout << std::endl; 67 } 68 69 void BinaryFileLoader::print( std::ostream &o ) const 70 { 71 o << "<BinaryFileLoader " << std::endl; 72 for ( section_list_t::const_iterator i = m_sections.begin(); 73 i != m_sections.end(); 74 ++i ) 75 o << " " << *i << std::endl; 76 o << ">" << std::endl; 77 } 78 79 BinaryFileLoader::~BinaryFileLoader() 80 { 81 } 82 83 std::vector<BinaryFileSection> BinaryFileLoader::sections() const 84 { 85 return m_sections; 86 } 87 88 BinaryFileSymbolOffset BinaryFileLoader::get_symbol_by_addr( uintptr_t addr ) const 89 { 90 addr &= m_mask; 91 std::map<uintptr_t, BinaryFileSymbol>::const_iterator i = 92 m_symbol_table.lower_bound( addr ); 93 94 while ( i != m_symbol_table.end() 95 && i != m_symbol_table.begin() 96 && i->first > addr ) 97 --i; 98 99 if ( i->second.contains(addr) || i->second.size() == 0 ) 100 return BinaryFileSymbolOffset(i->second, addr); 101 return BinaryFileSymbolOffset(BinaryFileSymbol("Unknown", addr, 1), addr); 102 } 103 104 const BinaryFileSymbol *BinaryFileLoader::get_symbol_by_name( const std::string &sym ) const 105 { 106 std::map<uintptr_t, BinaryFileSymbol>::const_iterator i; 107 for ( i = m_symbol_table.begin(); 108 i != m_symbol_table.end(); 109 ++i ) { 110 if ( i->second.name() == sym ) 111 return &(i->second); 112 } 113 return NULL; 114 } 115 116 BinaryFileLoader::loader_registry_t & 117 BinaryFileLoader::registry() 118 { 119 static loader_registry_t reg; 120 return reg; 121 } 122 123 void BinaryFileLoader::register_loader( const std::string &name, 124 binary_loader_t loader ) 125 { 126 loader_registry_t ® = registry(); 127 128 reg[name] = loader; 129 } 130 131 void BinaryFileLoader::load_file( const std::string &desc_str ) 132 { 133 loader_registry_t ® = registry(); 134 std::string tried; 135 for ( loader_registry_t::iterator i = reg.begin(); 136 i != reg.end(); 137 i++ ) { 138 if ( tried.size() ) 139 tried += ", "; 140 tried += i->first; 141 if ( (i->second)( desc_str, *this ) ) 142 return; 143 } 144 throw exception::RunTimeError( 145 std::string("Found no method able to load file ") + 146 desc_str + " even if tried: " + tried); 147 } 148 149 void BinaryFileLoader::addSection( const BinaryFileSection §ion ) 150 { 151 m_sections.push_back(section); 152 } 153 154 void BinaryFileLoader::addSymbol( const BinaryFileSymbol &symbol ) 155 { 156 m_symbol_table[symbol.address()] = symbol; 157 } 158 159 }} 160 161 // Local Variables: 162 // tab-width: 4 163 // c-basic-offset: 4 164 // c-file-offsets:((innamespace . 0)(inline-open . 0)) 165 // indent-tabs-mode: nil 166 // End: 167 168 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 169