2 * Copyright (c) 2002-2004 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #ifndef __BASE_LOADER_MEMORY_IMAGE_HH__
30 #define __BASE_LOADER_MEMORY_IMAGE_HH__
34 #include <initializer_list>
39 #include "base/loader/image_file_data.hh"
40 #include "base/logging.hh"
41 #include "base/types.hh"
53 Segment(const std::string &_name, Addr _base,
54 const uint8_t *_data, size_t _size) :
55 name(_name), base(_base), data(_data), size(_size)
58 Segment(const std::string &_name, Addr _base, size_t _size) :
59 name(_name), base(_base), size(_size)
62 Segment(const std::string &_name, Addr _base,
63 const ImageFileDataPtr &_ifd, Addr offset, size_t _size) :
64 ifd(_ifd), name(_name), base(_base), size(_size)
66 panic_if(offset + size > ifd->len(),
67 "Segment outside the bounds of the image data");
68 data = ifd->data() + offset;
71 Segment(const std::string &_name, const ImageFileDataPtr &_ifd) :
72 Segment(_name, 0, _ifd, 0, _ifd->len())
78 const uint8_t *data = nullptr;
84 MemoryImage(const Segment &seg)
89 MemoryImage(std::initializer_list<Segment> segs)
95 std::vector<Segment> _segments;
96 bool writeSegment(const Segment &seg, const PortProxy &proxy) const;
99 const std::vector<Segment> &
106 addSegment(const Segment &seg)
108 _segments.emplace_back(seg);
112 addSegments(std::initializer_list<Segment> segs)
114 for (auto &seg: segs)
118 bool write(const PortProxy &proxy) const;
119 MemoryImage &move(std::function<Addr(Addr)> mapper);
123 return move([by](Addr a){ return by + a; });
127 return move([m](Addr a) { return a & m; });
134 for (auto &seg: _segments)
135 max = std::max(max, seg.base + seg.size);
143 for (auto &seg: _segments)
144 min = std::min(min, seg.base);
149 contains(Addr addr) const
151 for (auto &seg: _segments) {
152 Addr start = seg.base;
153 Addr end = seg.base + seg.size;
154 if (addr >= start && addr < end)
161 static inline std::ostream &
162 operator << (std::ostream &os, const MemoryImage::Segment &seg)
164 ccprintf(os, "%s: %#x %d", seg.name, seg.base, seg.size);
168 } // namespace Loader
170 #endif // __BASE_LOADER_MEMORY_IMAGE_HH__