Kinetis Bootloader Host  2.0.0
Host Tools for Kinetis devices
apps/elftosb/common/StExecutableImage.h
1 /*
2  * File: StExecutableImage.h
3  *
4  * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
5  * See included license file for license details.
6  */
7 #if !defined(_StExecutableImage_h_)
8 #define _StExecutableImage_h_
9 
10 #include "stdafx.h"
11 #include <list>
12 
28 {
29 public:
31  typedef enum
32  {
36 
38  enum
39  {
43 
45 
48  };
49 
55  struct MemoryRegion
56  {
57  MemoryRegionType m_type;
58  uint32_t m_address;
59  uint32_t m_length;
60  uint8_t *m_data;
61  unsigned m_flags;
62 
64  inline uint32_t endAddress() const { return m_address + m_length - 1; }
66  bool operator==(const MemoryRegion &other);
67  };
68 
70  typedef std::list<MemoryRegion> MemoryRegionList;
71 
74  typedef MemoryRegionList::const_iterator const_iterator;
75 
77  typedef enum
78  {
84 
91  {
92  AddressFilterAction m_action;
93  uint32_t m_fromAddress;
94  uint32_t m_toAddress;
95  unsigned m_priority;
96 
98  AddressFilter(AddressFilterAction action, uint32_t from, uint32_t to, unsigned priority = 0)
99  : m_action(action)
100  , m_fromAddress(from)
101  , m_toAddress(to)
102  , m_priority(priority)
103  {
104  }
105 
107  bool matchesMemoryRegion(const MemoryRegion &region) const;
108 
110  int compare(const AddressFilter &other) const;
111 
113 
114  inline bool operator<(const AddressFilter &other) const { return compare(other) == -1; }
115  inline bool operator>(const AddressFilter &other) const { return compare(other) == 1; }
116  inline bool operator==(const AddressFilter &other) const { return compare(other) == 0; }
117  inline bool operator<=(const AddressFilter &other) const { return compare(other) != 1; }
118  inline bool operator>=(const AddressFilter &other) const { return compare(other) != -1; }
120  };
121 
123  typedef std::list<AddressFilter> AddressFilterList;
124 
128  {
129  public:
134  address_filter_exception(bool isError, std::string &imageName, const AddressFilter &matchingFilter)
135  : m_isError(isError)
136  , m_imageName(imageName)
137  , m_filter(matchingFilter)
138  {
139  }
140 
143  inline bool isError() const { return m_isError; }
145  inline std::string getImageName() const { return m_imageName; }
147  inline const AddressFilter &getMatchingFilter() const { return m_filter; }
148  protected:
149  bool m_isError;
150  std::string m_imageName;
151  AddressFilter m_filter;
152  };
153 
154 public:
156  StExecutableImage(int inAlignment = 256);
157 
159  StExecutableImage(const StExecutableImage &inOther);
160 
162  virtual ~StExecutableImage();
163 
166 
167  virtual void setName(const std::string &inName);
169 
171  virtual std::string getName() const;
173 
176 
177  virtual void addFillRegion(uint32_t inAddress, unsigned inLength);
179 
181  virtual void addTextRegion(uint32_t inAddress, const uint8_t *inData, unsigned inLength);
182 
187  inline unsigned getRegionCount() const { return static_cast<unsigned>(m_image.size()); }
189  const MemoryRegion &getRegionAtIndex(unsigned inIndex) const;
190 
192  inline const_iterator getRegionBegin() const { return m_image.begin(); }
194  inline const_iterator getRegionEnd() const { return m_image.end(); }
196 
198 
199  inline void setEntryPoint(uint32_t inEntryAddress)
201  {
202  m_entry = inEntryAddress;
203  m_hasEntry = true;
204  }
205 
207  inline bool hasEntryPoint() const { return m_hasEntry; }
209  inline uint32_t getEntryPoint() const { return hasEntryPoint() ? m_entry : 0; }
211 
213 
214  virtual void addAddressFilter(const AddressFilter &filter);
216 
222  template <typename _T>
223  void addAddressFilters(_T from, _T to)
224  {
225  _T it = from;
226  for (; it != to; ++it)
227  {
228  addAddressFilter(*it);
229  }
230  }
231 
233  virtual void clearAddressFilters();
234 
236  virtual void applyAddressFilters();
238 
239 protected:
240  std::string m_name;
242  bool m_hasEntry;
243  uint32_t m_entry;
244  MemoryRegionList m_image;
245  AddressFilterList m_filters;
246 
248  void cropRegionToFilter(MemoryRegion &region, const AddressFilter &filter);
249 
251  void insertOrMergeRegion(MemoryRegion &inRegion);
252 
254  void mergeRegions(MemoryRegion &inOldRegion, MemoryRegion &inNewRegion);
255 };
256 
257 #endif // _StExecutableImage_h_
MemoryRegionType m_type
Memory region type.
Definition: apps/elftosb/common/StExecutableImage.h:57
const_iterator getRegionBegin() const
Return an iterator to the first region.
Definition: apps/elftosb/common/StExecutableImage.h:192
virtual void addAddressFilter(const AddressFilter &filter)
Add a new address filter.
Definition: apps/elftosb/common/StExecutableImage.cpp:129
AddressFilterAction m_action
Action to be performed when the filter is matched.
Definition: apps/elftosb/common/StExecutableImage.h:92
virtual void addFillRegion(uint32_t inAddress, unsigned inLength)
Add a region to be filled with zeroes.
Definition: apps/elftosb/common/StExecutableImage.cpp:74
uint32_t m_length
Number of bytes in this region.
Definition: apps/elftosb/common/StExecutableImage.h:59
const_iterator getRegionEnd() const
Return an iterator to the next-after-last region.
Definition: apps/elftosb/common/StExecutableImage.h:194
StExecutableImage(int inAlignment=256)
Constructor.
Definition: apps/elftosb/common/StExecutableImage.cpp:14
unsigned m_flags
Flags for the region.
Definition: apps/elftosb/common/StExecutableImage.h:61
Region is readable.
Definition: apps/elftosb/common/StExecutableImage.h:40
A region containing data or instructions.
Definition: apps/elftosb/common/StExecutableImage.h:33
virtual ~StExecutableImage()
Destructor.
Definition: apps/elftosb/common/StExecutableImage.cpp:46
virtual void clearAddressFilters()
Remove all active filters.
Definition: apps/elftosb/common/StExecutableImage.cpp:136
Definition: apps/elftosb/common/StExecutableImage.h:127
Region to be initialized with zero bytes.
Definition: apps/elftosb/common/StExecutableImage.h:34
virtual void applyAddressFilters()
Process all active filters and perform associated actions.
Definition: apps/elftosb/common/StExecutableImage.cpp:148
int m_alignment
The required address alignment for each memory region.
Definition: apps/elftosb/common/StExecutableImage.h:241
unsigned m_priority
Priority for this filter. Zero is the lowest priority.
Definition: apps/elftosb/common/StExecutableImage.h:95
Used to build a representation of memory regions.
Definition: apps/elftosb/common/StExecutableImage.h:27
uint32_t m_fromAddress
Start address of the filter. Should be lower than or equal to m_toAddress.
Definition: apps/elftosb/common/StExecutableImage.h:93
uint32_t getEntryPoint() const
Returns the entry point address.
Definition: apps/elftosb/common/StExecutableImage.h:209
bool hasEntryPoint() const
Returns true if an entry point has been set.
Definition: apps/elftosb/common/StExecutableImage.h:207
virtual void addTextRegion(uint32_t inAddress, const uint8_t *inData, unsigned inLength)
Add a region containing data to be loaded.
Definition: apps/elftosb/common/StExecutableImage.cpp:93
std::string m_name
The name of the image (can be a file name, for instance).
Definition: apps/elftosb/common/StExecutableImage.h:240
Definition: apps/elftosb/common/StExecutableImage.h:90
const MemoryRegion & getRegionAtIndex(unsigned inIndex) const
Returns a reference to the region specified by inIndex.
Definition: apps/elftosb/common/StExecutableImage.cpp:110
virtual void setName(const std::string &inName)
Sets the image&#39;s name to inName.
Definition: apps/elftosb/common/StExecutableImage.cpp:61
Do nothing.
Definition: apps/elftosb/common/StExecutableImage.h:79
bool m_hasEntry
True if an entry point has been set.
Definition: apps/elftosb/common/StExecutableImage.h:242
void mergeRegions(MemoryRegion &inOldRegion, MemoryRegion &inNewRegion)
Merges two memory regions into one.
Definition: apps/elftosb/common/StExecutableImage.cpp:333
AddressFilter(AddressFilterAction action, uint32_t from, uint32_t to, unsigned priority=0)
Constructor.
Definition: apps/elftosb/common/StExecutableImage.h:98
void addAddressFilters(_T from, _T to)
Add multiple address filters at once.
Definition: apps/elftosb/common/StExecutableImage.h:223
MemoryRegionList m_image
The memory regions.
Definition: apps/elftosb/common/StExecutableImage.h:244
void setEntryPoint(uint32_t inEntryAddress)
Sets the entry point address.
Definition: apps/elftosb/common/StExecutableImage.h:200
AddressFilterList m_filters
List of active address filters.
Definition: apps/elftosb/common/StExecutableImage.h:245
bool operator==(const MemoryRegion &other)
Equality operator.
Definition: apps/elftosb/common/StExecutableImage.cpp:402
uint32_t endAddress() const
Calculates the address of the last byte occupied by this region.
Definition: apps/elftosb/common/StExecutableImage.h:64
uint32_t m_address
The 32-bit start address of this region.
Definition: apps/elftosb/common/StExecutableImage.h:58
Mask to access only permissions flags for a region.
Definition: apps/elftosb/common/StExecutableImage.h:47
void insertOrMergeRegion(MemoryRegion &inRegion)
Inserts the region in sorted order or merges with one already in the image.
Definition: apps/elftosb/common/StExecutableImage.cpp:286
address_filter_exception(bool isError, std::string &imageName, const AddressFilter &matchingFilter)
Constructor.
Definition: apps/elftosb/common/StExecutableImage.h:134
unsigned getRegionCount() const
Returns the total number of regions.
Definition: apps/elftosb/common/StExecutableImage.h:187
MemoryRegionType
Possible types of memory regions.
Definition: apps/elftosb/common/StExecutableImage.h:31
AddressFilterAction
The possible actions for regions matching an address filter range.
Definition: apps/elftosb/common/StExecutableImage.h:77
bool isError() const
Returns true if the exception is an error. Otherwise the exception is for a warning.
Definition: apps/elftosb/common/StExecutableImage.h:143
uint8_t * m_data
Pointer to data. Will be NULL for FILL_REGION type.
Definition: apps/elftosb/common/StExecutableImage.h:60
void cropRegionToFilter(MemoryRegion &region, const AddressFilter &filter)
Deletes the portion region that overlaps filter.
Definition: apps/elftosb/common/StExecutableImage.cpp:205
std::list< MemoryRegion > MemoryRegionList
A list of StExecutableImage::MemoryRegion objects.
Definition: apps/elftosb/common/StExecutableImage.h:70
virtual std::string getName() const
Returns a copy of the image&#39;s name.
Definition: apps/elftosb/common/StExecutableImage.cpp:66
Raise a warning exception.
Definition: apps/elftosb/common/StExecutableImage.h:81
MemoryRegionList::const_iterator const_iterator
Definition: apps/elftosb/common/StExecutableImage.h:74
Don&#39;t include the matching address range in the executable image.
Definition: apps/elftosb/common/StExecutableImage.h:82
Region is writable.
Definition: apps/elftosb/common/StExecutableImage.h:41
Definition: apps/elftosb/common/StExecutableImage.h:55
Region is read-write.
Definition: apps/elftosb/common/StExecutableImage.h:44
Region may contain executable code.
Definition: apps/elftosb/common/StExecutableImage.h:42
std::list< AddressFilter > AddressFilterList
List of StExecutableImage::AddressFilter objects.
Definition: apps/elftosb/common/StExecutableImage.h:123
uint32_t m_entry
Entry point address.
Definition: apps/elftosb/common/StExecutableImage.h:243
Raise an error exception.
Definition: apps/elftosb/common/StExecutableImage.h:80
uint32_t m_toAddress
End address of the filter. Should be higher than or equal to m_fromAddress.
Definition: apps/elftosb/common/StExecutableImage.h:94