Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | // -*- mode: C++ -*- |
michael@0 | 2 | |
michael@0 | 3 | // Copyright (c) 2010, Google Inc. |
michael@0 | 4 | // All rights reserved. |
michael@0 | 5 | // |
michael@0 | 6 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 7 | // modification, are permitted provided that the following conditions are |
michael@0 | 8 | // met: |
michael@0 | 9 | // |
michael@0 | 10 | // * Redistributions of source code must retain the above copyright |
michael@0 | 11 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 12 | // * Redistributions in binary form must reproduce the above |
michael@0 | 13 | // copyright notice, this list of conditions and the following disclaimer |
michael@0 | 14 | // in the documentation and/or other materials provided with the |
michael@0 | 15 | // distribution. |
michael@0 | 16 | // * Neither the name of Google Inc. nor the names of its |
michael@0 | 17 | // contributors may be used to endorse or promote products derived from |
michael@0 | 18 | // this software without specific prior written permission. |
michael@0 | 19 | // |
michael@0 | 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 31 | |
michael@0 | 32 | // Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com> |
michael@0 | 33 | |
michael@0 | 34 | // test-assembler.h: interface to class for building complex binary streams. |
michael@0 | 35 | |
michael@0 | 36 | // To test the Breakpad symbol dumper and processor thoroughly, for |
michael@0 | 37 | // all combinations of host system and minidump processor |
michael@0 | 38 | // architecture, we need to be able to easily generate complex test |
michael@0 | 39 | // data like debugging information and minidump files. |
michael@0 | 40 | // |
michael@0 | 41 | // For example, if we want our unit tests to provide full code |
michael@0 | 42 | // coverage for stack walking, it may be difficult to persuade the |
michael@0 | 43 | // compiler to generate every possible sort of stack walking |
michael@0 | 44 | // information that we want to support; there are probably DWARF CFI |
michael@0 | 45 | // opcodes that GCC never emits. Similarly, if we want to test our |
michael@0 | 46 | // error handling, we will need to generate damaged minidumps or |
michael@0 | 47 | // debugging information that (we hope) the client or compiler will |
michael@0 | 48 | // never produce on its own. |
michael@0 | 49 | // |
michael@0 | 50 | // google_breakpad::TestAssembler provides a predictable and |
michael@0 | 51 | // (relatively) simple way to generate complex formatted data streams |
michael@0 | 52 | // like minidumps and CFI. Furthermore, because TestAssembler is |
michael@0 | 53 | // portable, developers without access to (say) Visual Studio or a |
michael@0 | 54 | // SPARC assembler can still work on test data for those targets. |
michael@0 | 55 | |
michael@0 | 56 | #ifndef PROCESSOR_TEST_ASSEMBLER_H_ |
michael@0 | 57 | #define PROCESSOR_TEST_ASSEMBLER_H_ |
michael@0 | 58 | |
michael@0 | 59 | #include <list> |
michael@0 | 60 | #include <vector> |
michael@0 | 61 | #include <string> |
michael@0 | 62 | |
michael@0 | 63 | #include "common/using_std_string.h" |
michael@0 | 64 | #include "google_breakpad/common/breakpad_types.h" |
michael@0 | 65 | |
michael@0 | 66 | namespace google_breakpad { |
michael@0 | 67 | |
michael@0 | 68 | using std::list; |
michael@0 | 69 | using std::vector; |
michael@0 | 70 | |
michael@0 | 71 | namespace test_assembler { |
michael@0 | 72 | |
michael@0 | 73 | // A Label represents a value not yet known that we need to store in a |
michael@0 | 74 | // section. As long as all the labels a section refers to are defined |
michael@0 | 75 | // by the time we retrieve its contents as bytes, we can use undefined |
michael@0 | 76 | // labels freely in that section's construction. |
michael@0 | 77 | // |
michael@0 | 78 | // A label can be in one of three states: |
michael@0 | 79 | // - undefined, |
michael@0 | 80 | // - defined as the sum of some other label and a constant, or |
michael@0 | 81 | // - a constant. |
michael@0 | 82 | // |
michael@0 | 83 | // A label's value never changes, but it can accumulate constraints. |
michael@0 | 84 | // Adding labels and integers is permitted, and yields a label. |
michael@0 | 85 | // Subtracting a constant from a label is permitted, and also yields a |
michael@0 | 86 | // label. Subtracting two labels that have some relationship to each |
michael@0 | 87 | // other is permitted, and yields a constant. |
michael@0 | 88 | // |
michael@0 | 89 | // For example: |
michael@0 | 90 | // |
michael@0 | 91 | // Label a; // a's value is undefined |
michael@0 | 92 | // Label b; // b's value is undefined |
michael@0 | 93 | // { |
michael@0 | 94 | // Label c = a + 4; // okay, even though a's value is unknown |
michael@0 | 95 | // b = c + 4; // also okay; b is now a+8 |
michael@0 | 96 | // } |
michael@0 | 97 | // Label d = b - 2; // okay; d == a+6, even though c is gone |
michael@0 | 98 | // d.Value(); // error: d's value is not yet known |
michael@0 | 99 | // d - a; // is 6, even though their values are not known |
michael@0 | 100 | // a = 12; // now b == 20, and d == 18 |
michael@0 | 101 | // d.Value(); // 18: no longer an error |
michael@0 | 102 | // b.Value(); // 20 |
michael@0 | 103 | // d = 10; // error: d is already defined. |
michael@0 | 104 | // |
michael@0 | 105 | // Label objects' lifetimes are unconstrained: notice that, in the |
michael@0 | 106 | // above example, even though a and b are only related through c, and |
michael@0 | 107 | // c goes out of scope, the assignment to a sets b's value as well. In |
michael@0 | 108 | // particular, it's not necessary to ensure that a Label lives beyond |
michael@0 | 109 | // Sections that refer to it. |
michael@0 | 110 | class Label { |
michael@0 | 111 | public: |
michael@0 | 112 | Label(); // An undefined label. |
michael@0 | 113 | Label(uint64_t value); // A label with a fixed value |
michael@0 | 114 | Label(const Label &value); // A label equal to another. |
michael@0 | 115 | ~Label(); |
michael@0 | 116 | |
michael@0 | 117 | // Return this label's value; it must be known. |
michael@0 | 118 | // |
michael@0 | 119 | // Providing this as a cast operator is nifty, but the conversions |
michael@0 | 120 | // happen in unexpected places. In particular, ISO C++ says that |
michael@0 | 121 | // Label + size_t becomes ambigious, because it can't decide whether |
michael@0 | 122 | // to convert the Label to a uint64_t and then to a size_t, or use |
michael@0 | 123 | // the overloaded operator that returns a new label, even though the |
michael@0 | 124 | // former could fail if the label is not yet defined and the latter won't. |
michael@0 | 125 | uint64_t Value() const; |
michael@0 | 126 | |
michael@0 | 127 | Label &operator=(uint64_t value); |
michael@0 | 128 | Label &operator=(const Label &value); |
michael@0 | 129 | Label operator+(uint64_t addend) const; |
michael@0 | 130 | Label operator-(uint64_t subtrahend) const; |
michael@0 | 131 | uint64_t operator-(const Label &subtrahend) const; |
michael@0 | 132 | |
michael@0 | 133 | // We could also provide == and != that work on undefined, but |
michael@0 | 134 | // related, labels. |
michael@0 | 135 | |
michael@0 | 136 | // Return true if this label's value is known. If VALUE_P is given, |
michael@0 | 137 | // set *VALUE_P to the known value if returning true. |
michael@0 | 138 | bool IsKnownConstant(uint64_t *value_p = NULL) const; |
michael@0 | 139 | |
michael@0 | 140 | // Return true if the offset from LABEL to this label is known. If |
michael@0 | 141 | // OFFSET_P is given, set *OFFSET_P to the offset when returning true. |
michael@0 | 142 | // |
michael@0 | 143 | // You can think of l.KnownOffsetFrom(m, &d) as being like 'd = l-m', |
michael@0 | 144 | // except that it also returns a value indicating whether the |
michael@0 | 145 | // subtraction is possible given what we currently know of l and m. |
michael@0 | 146 | // It can be possible even if we don't know l and m's values. For |
michael@0 | 147 | // example: |
michael@0 | 148 | // |
michael@0 | 149 | // Label l, m; |
michael@0 | 150 | // m = l + 10; |
michael@0 | 151 | // l.IsKnownConstant(); // false |
michael@0 | 152 | // m.IsKnownConstant(); // false |
michael@0 | 153 | // uint64_t d; |
michael@0 | 154 | // l.IsKnownOffsetFrom(m, &d); // true, and sets d to -10. |
michael@0 | 155 | // l-m // -10 |
michael@0 | 156 | // m-l // 10 |
michael@0 | 157 | // m.Value() // error: m's value is not known |
michael@0 | 158 | bool IsKnownOffsetFrom(const Label &label, uint64_t *offset_p = NULL) const; |
michael@0 | 159 | |
michael@0 | 160 | private: |
michael@0 | 161 | // A label's value, or if that is not yet known, how the value is |
michael@0 | 162 | // related to other labels' values. A binding may be: |
michael@0 | 163 | // - a known constant, |
michael@0 | 164 | // - constrained to be equal to some other binding plus a constant, or |
michael@0 | 165 | // - unconstrained, and free to take on any value. |
michael@0 | 166 | // |
michael@0 | 167 | // Many labels may point to a single binding, and each binding may |
michael@0 | 168 | // refer to another, so bindings and labels form trees whose leaves |
michael@0 | 169 | // are labels, whose interior nodes (and roots) are bindings, and |
michael@0 | 170 | // where links point from children to parents. Bindings are |
michael@0 | 171 | // reference counted, allowing labels to be lightweight, copyable, |
michael@0 | 172 | // assignable, placed in containers, and so on. |
michael@0 | 173 | class Binding { |
michael@0 | 174 | public: |
michael@0 | 175 | Binding(); |
michael@0 | 176 | Binding(uint64_t addend); |
michael@0 | 177 | ~Binding(); |
michael@0 | 178 | |
michael@0 | 179 | // Increment our reference count. |
michael@0 | 180 | void Acquire() { reference_count_++; }; |
michael@0 | 181 | // Decrement our reference count, and return true if it is zero. |
michael@0 | 182 | bool Release() { return --reference_count_ == 0; } |
michael@0 | 183 | |
michael@0 | 184 | // Set this binding to be equal to BINDING + ADDEND. If BINDING is |
michael@0 | 185 | // NULL, then set this binding to the known constant ADDEND. |
michael@0 | 186 | // Update every binding on this binding's chain to point directly |
michael@0 | 187 | // to BINDING, or to be a constant, with addends adjusted |
michael@0 | 188 | // appropriately. |
michael@0 | 189 | void Set(Binding *binding, uint64_t value); |
michael@0 | 190 | |
michael@0 | 191 | // Return what we know about the value of this binding. |
michael@0 | 192 | // - If this binding's value is a known constant, set BASE to |
michael@0 | 193 | // NULL, and set ADDEND to its value. |
michael@0 | 194 | // - If this binding is not a known constant but related to other |
michael@0 | 195 | // bindings, set BASE to the binding at the end of the relation |
michael@0 | 196 | // chain (which will always be unconstrained), and set ADDEND to the |
michael@0 | 197 | // value to add to that binding's value to get this binding's |
michael@0 | 198 | // value. |
michael@0 | 199 | // - If this binding is unconstrained, set BASE to this, and leave |
michael@0 | 200 | // ADDEND unchanged. |
michael@0 | 201 | void Get(Binding **base, uint64_t *addend); |
michael@0 | 202 | |
michael@0 | 203 | private: |
michael@0 | 204 | // There are three cases: |
michael@0 | 205 | // |
michael@0 | 206 | // - A binding representing a known constant value has base_ NULL, |
michael@0 | 207 | // and addend_ equal to the value. |
michael@0 | 208 | // |
michael@0 | 209 | // - A binding representing a completely unconstrained value has |
michael@0 | 210 | // base_ pointing to this; addend_ is unused. |
michael@0 | 211 | // |
michael@0 | 212 | // - A binding whose value is related to some other binding's |
michael@0 | 213 | // value has base_ pointing to that other binding, and addend_ |
michael@0 | 214 | // set to the amount to add to that binding's value to get this |
michael@0 | 215 | // binding's value. We only represent relationships of the form |
michael@0 | 216 | // x = y+c. |
michael@0 | 217 | // |
michael@0 | 218 | // Thus, the bind_ links form a chain terminating in either a |
michael@0 | 219 | // known constant value or a completely unconstrained value. Most |
michael@0 | 220 | // operations on bindings do path compression: they change every |
michael@0 | 221 | // binding on the chain to point directly to the final value, |
michael@0 | 222 | // adjusting addends as appropriate. |
michael@0 | 223 | Binding *base_; |
michael@0 | 224 | uint64_t addend_; |
michael@0 | 225 | |
michael@0 | 226 | // The number of Labels and Bindings pointing to this binding. |
michael@0 | 227 | // (When a binding points to itself, indicating a completely |
michael@0 | 228 | // unconstrained binding, that doesn't count as a reference.) |
michael@0 | 229 | int reference_count_; |
michael@0 | 230 | }; |
michael@0 | 231 | |
michael@0 | 232 | // This label's value. |
michael@0 | 233 | Binding *value_; |
michael@0 | 234 | }; |
michael@0 | 235 | |
michael@0 | 236 | inline Label operator+(uint64_t a, const Label &l) { return l + a; } |
michael@0 | 237 | // Note that int-Label isn't defined, as negating a Label is not an |
michael@0 | 238 | // operation we support. |
michael@0 | 239 | |
michael@0 | 240 | // Conventions for representing larger numbers as sequences of bytes. |
michael@0 | 241 | enum Endianness { |
michael@0 | 242 | kBigEndian, // Big-endian: the most significant byte comes first. |
michael@0 | 243 | kLittleEndian, // Little-endian: the least significant byte comes first. |
michael@0 | 244 | kUnsetEndian, // used internally |
michael@0 | 245 | }; |
michael@0 | 246 | |
michael@0 | 247 | // A section is a sequence of bytes, constructed by appending bytes |
michael@0 | 248 | // to the end. Sections have a convenient and flexible set of member |
michael@0 | 249 | // functions for appending data in various formats: big-endian and |
michael@0 | 250 | // little-endian signed and unsigned values of different sizes; |
michael@0 | 251 | // LEB128 and ULEB128 values (see below), and raw blocks of bytes. |
michael@0 | 252 | // |
michael@0 | 253 | // If you need to append a value to a section that is not convenient |
michael@0 | 254 | // to compute immediately, you can create a label, append the |
michael@0 | 255 | // label's value to the section, and then set the label's value |
michael@0 | 256 | // later, when it's convenient to do so. Once a label's value is |
michael@0 | 257 | // known, the section class takes care of updating all previously |
michael@0 | 258 | // appended references to it. |
michael@0 | 259 | // |
michael@0 | 260 | // Once all the labels to which a section refers have had their |
michael@0 | 261 | // values determined, you can get a copy of the section's contents |
michael@0 | 262 | // as a string. |
michael@0 | 263 | // |
michael@0 | 264 | // Note that there is no specified "start of section" label. This is |
michael@0 | 265 | // because there are typically several different meanings for "the |
michael@0 | 266 | // start of a section": the offset of the section within an object |
michael@0 | 267 | // file, the address in memory at which the section's content appear, |
michael@0 | 268 | // and so on. It's up to the code that uses the Section class to |
michael@0 | 269 | // keep track of these explicitly, as they depend on the application. |
michael@0 | 270 | class Section { |
michael@0 | 271 | public: |
michael@0 | 272 | Section(Endianness endianness = kUnsetEndian) |
michael@0 | 273 | : endianness_(endianness) { }; |
michael@0 | 274 | |
michael@0 | 275 | // A base class destructor should be either public and virtual, |
michael@0 | 276 | // or protected and nonvirtual. |
michael@0 | 277 | virtual ~Section() { }; |
michael@0 | 278 | |
michael@0 | 279 | // Set the default endianness of this section to ENDIANNESS. This |
michael@0 | 280 | // sets the behavior of the D<N> appending functions. If the |
michael@0 | 281 | // assembler's default endianness was set, this is the |
michael@0 | 282 | void set_endianness(Endianness endianness) { |
michael@0 | 283 | endianness_ = endianness; |
michael@0 | 284 | } |
michael@0 | 285 | |
michael@0 | 286 | // Return the default endianness of this section. |
michael@0 | 287 | Endianness endianness() const { return endianness_; } |
michael@0 | 288 | |
michael@0 | 289 | // Append the SIZE bytes at DATA or the contents of STRING to the |
michael@0 | 290 | // end of this section. Return a reference to this section. |
michael@0 | 291 | Section &Append(const uint8_t *data, size_t size) { |
michael@0 | 292 | contents_.append(reinterpret_cast<const char *>(data), size); |
michael@0 | 293 | return *this; |
michael@0 | 294 | }; |
michael@0 | 295 | Section &Append(const string &data) { |
michael@0 | 296 | contents_.append(data); |
michael@0 | 297 | return *this; |
michael@0 | 298 | }; |
michael@0 | 299 | |
michael@0 | 300 | // Append SIZE copies of BYTE to the end of this section. Return a |
michael@0 | 301 | // reference to this section. |
michael@0 | 302 | Section &Append(size_t size, uint8_t byte) { |
michael@0 | 303 | contents_.append(size, (char) byte); |
michael@0 | 304 | return *this; |
michael@0 | 305 | } |
michael@0 | 306 | |
michael@0 | 307 | // Append NUMBER to this section. ENDIANNESS is the endianness to |
michael@0 | 308 | // use to write the number. SIZE is the length of the number in |
michael@0 | 309 | // bytes. Return a reference to this section. |
michael@0 | 310 | Section &Append(Endianness endianness, size_t size, uint64_t number); |
michael@0 | 311 | Section &Append(Endianness endianness, size_t size, const Label &label); |
michael@0 | 312 | |
michael@0 | 313 | // Append SECTION to the end of this section. The labels SECTION |
michael@0 | 314 | // refers to need not be defined yet. |
michael@0 | 315 | // |
michael@0 | 316 | // Note that this has no effect on any Labels' values, or on |
michael@0 | 317 | // SECTION. If placing SECTION within 'this' provides new |
michael@0 | 318 | // constraints on existing labels' values, then it's up to the |
michael@0 | 319 | // caller to fiddle with those labels as needed. |
michael@0 | 320 | Section &Append(const Section §ion); |
michael@0 | 321 | |
michael@0 | 322 | // Append the contents of DATA as a series of bytes terminated by |
michael@0 | 323 | // a NULL character. |
michael@0 | 324 | Section &AppendCString(const string &data) { |
michael@0 | 325 | Append(data); |
michael@0 | 326 | contents_ += '\0'; |
michael@0 | 327 | return *this; |
michael@0 | 328 | } |
michael@0 | 329 | |
michael@0 | 330 | // Append at most SIZE bytes from DATA; if DATA is less than SIZE bytes |
michael@0 | 331 | // long, pad with '\0' characters. |
michael@0 | 332 | Section &AppendCString(const string &data, size_t size) { |
michael@0 | 333 | contents_.append(data, 0, size); |
michael@0 | 334 | if (data.size() < size) |
michael@0 | 335 | Append(size - data.size(), 0); |
michael@0 | 336 | return *this; |
michael@0 | 337 | } |
michael@0 | 338 | |
michael@0 | 339 | // Append VALUE or LABEL to this section, with the given bit width and |
michael@0 | 340 | // endianness. Return a reference to this section. |
michael@0 | 341 | // |
michael@0 | 342 | // The names of these functions have the form <ENDIANNESS><BITWIDTH>: |
michael@0 | 343 | // <ENDIANNESS> is either 'L' (little-endian, least significant byte first), |
michael@0 | 344 | // 'B' (big-endian, most significant byte first), or |
michael@0 | 345 | // 'D' (default, the section's default endianness) |
michael@0 | 346 | // <BITWIDTH> is 8, 16, 32, or 64. |
michael@0 | 347 | // |
michael@0 | 348 | // Since endianness doesn't matter for a single byte, all the |
michael@0 | 349 | // <BITWIDTH>=8 functions are equivalent. |
michael@0 | 350 | // |
michael@0 | 351 | // These can be used to write both signed and unsigned values, as |
michael@0 | 352 | // the compiler will properly sign-extend a signed value before |
michael@0 | 353 | // passing it to the function, at which point the function's |
michael@0 | 354 | // behavior is the same either way. |
michael@0 | 355 | Section &L8(uint8_t value) { contents_ += value; return *this; } |
michael@0 | 356 | Section &B8(uint8_t value) { contents_ += value; return *this; } |
michael@0 | 357 | Section &D8(uint8_t value) { contents_ += value; return *this; } |
michael@0 | 358 | Section &L16(uint16_t), &L32(uint32_t), &L64(uint64_t), |
michael@0 | 359 | &B16(uint16_t), &B32(uint32_t), &B64(uint64_t), |
michael@0 | 360 | &D16(uint16_t), &D32(uint32_t), &D64(uint64_t); |
michael@0 | 361 | Section &L8(const Label &label), &L16(const Label &label), |
michael@0 | 362 | &L32(const Label &label), &L64(const Label &label), |
michael@0 | 363 | &B8(const Label &label), &B16(const Label &label), |
michael@0 | 364 | &B32(const Label &label), &B64(const Label &label), |
michael@0 | 365 | &D8(const Label &label), &D16(const Label &label), |
michael@0 | 366 | &D32(const Label &label), &D64(const Label &label); |
michael@0 | 367 | |
michael@0 | 368 | // Append VALUE in a signed LEB128 (Little-Endian Base 128) form. |
michael@0 | 369 | // |
michael@0 | 370 | // The signed LEB128 representation of an integer N is a variable |
michael@0 | 371 | // number of bytes: |
michael@0 | 372 | // |
michael@0 | 373 | // - If N is between -0x40 and 0x3f, then its signed LEB128 |
michael@0 | 374 | // representation is a single byte whose value is N. |
michael@0 | 375 | // |
michael@0 | 376 | // - Otherwise, its signed LEB128 representation is (N & 0x7f) | |
michael@0 | 377 | // 0x80, followed by the signed LEB128 representation of N / 128, |
michael@0 | 378 | // rounded towards negative infinity. |
michael@0 | 379 | // |
michael@0 | 380 | // In other words, we break VALUE into groups of seven bits, put |
michael@0 | 381 | // them in little-endian order, and then write them as eight-bit |
michael@0 | 382 | // bytes with the high bit on all but the last. |
michael@0 | 383 | // |
michael@0 | 384 | // Note that VALUE cannot be a Label (we would have to implement |
michael@0 | 385 | // relaxation). |
michael@0 | 386 | Section &LEB128(long long value); |
michael@0 | 387 | |
michael@0 | 388 | // Append VALUE in unsigned LEB128 (Little-Endian Base 128) form. |
michael@0 | 389 | // |
michael@0 | 390 | // The unsigned LEB128 representation of an integer N is a variable |
michael@0 | 391 | // number of bytes: |
michael@0 | 392 | // |
michael@0 | 393 | // - If N is between 0 and 0x7f, then its unsigned LEB128 |
michael@0 | 394 | // representation is a single byte whose value is N. |
michael@0 | 395 | // |
michael@0 | 396 | // - Otherwise, its unsigned LEB128 representation is (N & 0x7f) | |
michael@0 | 397 | // 0x80, followed by the unsigned LEB128 representation of N / |
michael@0 | 398 | // 128, rounded towards negative infinity. |
michael@0 | 399 | // |
michael@0 | 400 | // Note that VALUE cannot be a Label (we would have to implement |
michael@0 | 401 | // relaxation). |
michael@0 | 402 | Section &ULEB128(uint64_t value); |
michael@0 | 403 | |
michael@0 | 404 | // Jump to the next location aligned on an ALIGNMENT-byte boundary, |
michael@0 | 405 | // relative to the start of the section. Fill the gap with PAD_BYTE. |
michael@0 | 406 | // ALIGNMENT must be a power of two. Return a reference to this |
michael@0 | 407 | // section. |
michael@0 | 408 | Section &Align(size_t alignment, uint8_t pad_byte = 0); |
michael@0 | 409 | |
michael@0 | 410 | // Clear the contents of this section. |
michael@0 | 411 | void Clear(); |
michael@0 | 412 | |
michael@0 | 413 | // Return the current size of the section. |
michael@0 | 414 | size_t Size() const { return contents_.size(); } |
michael@0 | 415 | |
michael@0 | 416 | // Return a label representing the start of the section. |
michael@0 | 417 | // |
michael@0 | 418 | // It is up to the user whether this label represents the section's |
michael@0 | 419 | // position in an object file, the section's address in memory, or |
michael@0 | 420 | // what have you; some applications may need both, in which case |
michael@0 | 421 | // this simple-minded interface won't be enough. This class only |
michael@0 | 422 | // provides a single start label, for use with the Here and Mark |
michael@0 | 423 | // member functions. |
michael@0 | 424 | // |
michael@0 | 425 | // Ideally, we'd provide this in a subclass that actually knows more |
michael@0 | 426 | // about the application at hand and can provide an appropriate |
michael@0 | 427 | // collection of start labels. But then the appending member |
michael@0 | 428 | // functions like Append and D32 would return a reference to the |
michael@0 | 429 | // base class, not the derived class, and the chaining won't work. |
michael@0 | 430 | // Since the only value here is in pretty notation, that's a fatal |
michael@0 | 431 | // flaw. |
michael@0 | 432 | Label start() const { return start_; } |
michael@0 | 433 | |
michael@0 | 434 | // Return a label representing the point at which the next Appended |
michael@0 | 435 | // item will appear in the section, relative to start(). |
michael@0 | 436 | Label Here() const { return start_ + Size(); } |
michael@0 | 437 | |
michael@0 | 438 | // Set *LABEL to Here, and return a reference to this section. |
michael@0 | 439 | Section &Mark(Label *label) { *label = Here(); return *this; } |
michael@0 | 440 | |
michael@0 | 441 | // If there are no undefined label references left in this |
michael@0 | 442 | // section, set CONTENTS to the contents of this section, as a |
michael@0 | 443 | // string, and clear this section. Return true on success, or false |
michael@0 | 444 | // if there were still undefined labels. |
michael@0 | 445 | bool GetContents(string *contents); |
michael@0 | 446 | |
michael@0 | 447 | private: |
michael@0 | 448 | // Used internally. A reference to a label's value. |
michael@0 | 449 | struct Reference { |
michael@0 | 450 | Reference(size_t set_offset, Endianness set_endianness, size_t set_size, |
michael@0 | 451 | const Label &set_label) |
michael@0 | 452 | : offset(set_offset), endianness(set_endianness), size(set_size), |
michael@0 | 453 | label(set_label) { } |
michael@0 | 454 | |
michael@0 | 455 | // The offset of the reference within the section. |
michael@0 | 456 | size_t offset; |
michael@0 | 457 | |
michael@0 | 458 | // The endianness of the reference. |
michael@0 | 459 | Endianness endianness; |
michael@0 | 460 | |
michael@0 | 461 | // The size of the reference. |
michael@0 | 462 | size_t size; |
michael@0 | 463 | |
michael@0 | 464 | // The label to which this is a reference. |
michael@0 | 465 | Label label; |
michael@0 | 466 | }; |
michael@0 | 467 | |
michael@0 | 468 | // The default endianness of this section. |
michael@0 | 469 | Endianness endianness_; |
michael@0 | 470 | |
michael@0 | 471 | // The contents of the section. |
michael@0 | 472 | string contents_; |
michael@0 | 473 | |
michael@0 | 474 | // References to labels within those contents. |
michael@0 | 475 | vector<Reference> references_; |
michael@0 | 476 | |
michael@0 | 477 | // A label referring to the beginning of the section. |
michael@0 | 478 | Label start_; |
michael@0 | 479 | }; |
michael@0 | 480 | |
michael@0 | 481 | } // namespace test_assembler |
michael@0 | 482 | } // namespace google_breakpad |
michael@0 | 483 | |
michael@0 | 484 | #endif // PROCESSOR_TEST_ASSEMBLER_H_ |