1 : // Copyright 2014 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #ifndef SYZYGY_CORE_SECTION_OFFSET_ADDRESS_H_
16 : #define SYZYGY_CORE_SECTION_OFFSET_ADDRESS_H_
17 :
18 : #include <iosfwd>
19 : #include "base/basictypes.h"
20 : #include "syzygy/core/serialization.h"
21 :
22 : namespace core {
23 :
24 : // This class implements an address in a PE image file represented as a section
25 : // index and an offset in the section. It has the same interface as AddressImpl,
26 : // except for the operator- that accepts another address of the same type.
27 : // The class is a lightweight wrapper for 2 integers, which can be freely
28 : // copied.
29 : class SectionOffsetAddress {
30 : public:
31 : static const SectionOffsetAddress kInvalidAddress;
32 :
33 : // A struct that contains all data from a SectionOffsetAddress and that is
34 : // returned by value().
35 : struct SectionOffset {
36 : SectionOffset(uint32 section_id, uint32 offset);
37 :
38 : bool operator<(const SectionOffset& other) const;
39 : bool operator<=(const SectionOffset& other) const;
40 : bool operator>(const SectionOffset& other) const;
41 : bool operator>=(const SectionOffset& other) const;
42 : bool operator==(const SectionOffset& other) const;
43 : bool operator!=(const SectionOffset& other) const;
44 :
45 : uint32 section_id;
46 : uint32 offset;
47 : };
48 :
49 : SectionOffsetAddress();
50 : SectionOffsetAddress(uint32 section_id, uint32 offset);
51 :
52 : // Non-explicit copy constructor, for STL container compatibility.
53 : SectionOffsetAddress(const SectionOffsetAddress& other); // NOLINT
54 :
55 : bool operator<(const SectionOffsetAddress& other) const;
56 : bool operator<=(const SectionOffsetAddress& other) const;
57 : bool operator>(const SectionOffsetAddress& other) const;
58 : bool operator>=(const SectionOffsetAddress& other) const;
59 : bool operator==(const SectionOffsetAddress& other) const;
60 : bool operator!=(const SectionOffsetAddress& other) const;
61 :
62 : void operator=(const SectionOffsetAddress& other);
63 : void operator+=(int32 offset);
64 : void operator-=(int32 offset);
65 :
66 : SectionOffsetAddress operator+(size_t offset) const;
67 : SectionOffsetAddress operator-(size_t offset) const;
68 :
69 E : const SectionOffset& value() const { return value_; }
70 E : void set_value(const SectionOffset& value) { value_ = value; }
71 :
72 E : uint32 section_id() const { return value_.section_id; }
73 E : void set_section_id(uint32 section_id) { value_.section_id = section_id; }
74 :
75 E : uint32 offset() const { return value_.offset; }
76 E : void set_offset(uint32 offset) { value_.offset = offset; }
77 :
78 : // Aligns the address on a multiple of |alignment|.
79 : // @param alignment the alignment boundary to round the address up to.
80 : // @pre alignment != 0 && alignment <= 512.
81 : // @returns the aligned address.
82 : SectionOffsetAddress AlignUp(size_t alignment) const;
83 :
84 : // @param alignment The alignment boundary to test.
85 : // @pre alignment != 0 && alignment <= 512.
86 : // @returns true iff value is an even multiple of alignment.
87 : bool IsAligned(size_t alignment) const;
88 :
89 : // Determines the address alignment by counting the trailing zeros.
90 : // The returned value will be at most 512 because it is impossible to
91 : // guarantee an alignment on a greater power of 2 without knowing the
92 : // exact alignment of the section.
93 : // @returns the alignment of the address.
94 : uint32 GetAlignment() const;
95 :
96 : // For serialization.
97 : bool Save(OutArchive *out_archive) const;
98 : bool Load(InArchive *in_archive);
99 :
100 : private:
101 : SectionOffset value_;
102 : };
103 :
104 : std::ostream& operator<<(std::ostream& str, const SectionOffsetAddress& addr);
105 :
106 : } // namespace core
107 :
108 : #endif // SYZYGY_CORE_SECTION_OFFSET_ADDRESS_H_
|