1 /*
2  * File:  DataTarget.cpp
3  *
4  * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
5  * See included license file for license details.
6  */
7 
8 #include "DataTarget.h"
9 #include "DataSource.h"
10 #include "ElftosbErrors.h"
11 
12 using namespace elftosb;
13 
14 //! \exception elftosb::semantic_error Thrown if the source has multiple segments.
getRangeForSegment(DataSource & source,DataSource::Segment & segment)15 DataTarget::AddressRange ConstantDataTarget::getRangeForSegment(DataSource & source, DataSource::Segment & segment)
16 {
17           // can't handle multi-segment data sources
18           if (source.getSegmentCount() > 1)
19           {
20                     throw semantic_error("constant targets only support single-segment sources");
21           }
22 
23           // always relocate the segment to our begin address
24           AddressRange range;
25           range.m_begin = m_begin;
26 
27           if (isBounded())
28           {
29                     // we have an end address. trim the result range to the segment size
30                     // or let the end address crop the segment.
31                     range.m_end = std::min<uint32_t>(m_end, m_begin + segment.getLength());
32           }
33           else
34           {
35                     // we have no end address, so the segment size determines it.
36                     range.m_end = m_begin + segment.getLength();
37           }
38 
39           return range;
40 }
41 
42 //! If the \a segment has a natural location, the returned address range extends
43 //! from the segment's base address to its base address plus its length.
44 //!
45 //! \exception elftosb::semantic_error This exception is thrown if the \a segment
46 //!                 does not have a natural location associated with it.
getRangeForSegment(DataSource & source,DataSource::Segment & segment)47 DataTarget::AddressRange NaturalDataTarget::getRangeForSegment(DataSource & source, DataSource::Segment & segment)
48 {
49           if (!segment.hasNaturalLocation())
50           {
51                     throw semantic_error("source has no natural location");
52           }
53 
54           AddressRange range;
55           range.m_begin = segment.getBaseAddress();
56           range.m_end = segment.getBaseAddress() + segment.getLength();
57           return range;
58 }
59 
60