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 <stdint.h>
19 : #include <iosfwd>
20 :
21 : #include "syzygy/core/serialization.h"
22 :
23 : namespace core {
24 :
25 : // This class implements an address in a PE image file represented as a section
26 : // index and an offset in the section. It has the same interface as AddressImpl,
27 : // except for the operator- that accepts another address of the same type.
28 : // The class is a lightweight wrapper for 2 integers, which can be freely
29 : // copied.
30 : class SectionOffsetAddress {
31 : public:
32 : static const SectionOffsetAddress kInvalidAddress;
33 :
34 : // A struct that contains all data from a SectionOffsetAddress and that is
35 : // returned by value().
36 : struct SectionOffset {
37 : SectionOffset(uint32_t section_id, uint32_t offset);
38 :
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 : bool operator!=(const SectionOffset& other) const;
45 :
46 : uint32_t section_id;
47 : uint32_t offset;
48 : };
49 :
50 : SectionOffsetAddress();
51 : SectionOffsetAddress(uint32_t section_id, uint32_t offset);
52 :
53 : // Non-explicit copy constructor, for STL container compatibility.
54 : SectionOffsetAddress(const SectionOffsetAddress& other); // NOLINT
55 :
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 : bool operator!=(const SectionOffsetAddress& other) const;
62 :
63 : void operator=(const SectionOffsetAddress& other);
64 : void operator+=(int32_t offset);
65 : void operator-=(int32_t offset);
66 :
67 : SectionOffsetAddress operator+(size_t offset) const;
68 : SectionOffsetAddress operator-(size_t offset) const;
69 :
70 E : const SectionOffset& value() const { return value_; }
71 E : void set_value(const SectionOffset& value) { value_ = value; }
72 :
73 E : uint32_t section_id() const { return value_.section_id; }
74 E : void set_section_id(uint32_t section_id) { value_.section_id = section_id; }
75 :
76 E : uint32_t offset() const { return value_.offset; }
77 E : void set_offset(uint32_t offset) { value_.offset = offset; }
78 :
79 : // Aligns the address on a multiple of |alignment|.
80 : // @param alignment the alignment boundary to round the address up to.
81 : // @pre alignment != 0 && alignment <= 512.
82 : // @returns the aligned address.
83 : SectionOffsetAddress AlignUp(size_t alignment) const;
84 :
85 : // @param alignment The alignment boundary to test.
86 : // @pre alignment != 0 && alignment <= 512.
87 : // @returns true iff value is an even multiple of alignment.
88 : bool IsAligned(size_t alignment) const;
89 :
90 : // Determines the address alignment by counting the trailing zeros.
91 : // The returned value will be at most 512 because it is impossible to
92 : // guarantee an alignment on a greater power of 2 without knowing the
93 : // exact alignment of the section.
94 : // @returns the alignment of the address.
95 : uint32_t GetAlignment() const;
96 :
97 : // For serialization.
98 : bool Save(OutArchive *out_archive) const;
99 : bool Load(InArchive *in_archive);
100 :
101 : private:
102 : SectionOffset value_;
103 : };
104 :
105 : std::ostream& operator<<(std::ostream& str, const SectionOffsetAddress& addr);
106 :
107 : } // namespace core
108 :
109 : #endif // SYZYGY_CORE_SECTION_OFFSET_ADDRESS_H_
|