/src/mozilla-central/tools/profiler/tests/gtest/LulTestInfrastructure.h
Line | Count | Source (jump to first uncovered line) |
1 | | // -*- mode: C++ -*- |
2 | | |
3 | | // Copyright (c) 2010, Google Inc. |
4 | | // All rights reserved. |
5 | | // |
6 | | // Redistribution and use in source and binary forms, with or without |
7 | | // modification, are permitted provided that the following conditions are |
8 | | // met: |
9 | | // |
10 | | // * Redistributions of source code must retain the above copyright |
11 | | // notice, this list of conditions and the following disclaimer. |
12 | | // * Redistributions in binary form must reproduce the above |
13 | | // copyright notice, this list of conditions and the following disclaimer |
14 | | // in the documentation and/or other materials provided with the |
15 | | // distribution. |
16 | | // * Neither the name of Google Inc. nor the names of its |
17 | | // contributors may be used to endorse or promote products derived from |
18 | | // this software without specific prior written permission. |
19 | | // |
20 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
21 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
22 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
23 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
24 | | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
25 | | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
26 | | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
27 | | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
28 | | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
29 | | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
30 | | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
31 | | |
32 | | // Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com> |
33 | | |
34 | | // Derived from: |
35 | | // cfi_assembler.h: Define CFISection, a class for creating properly |
36 | | // (and improperly) formatted DWARF CFI data for unit tests. |
37 | | |
38 | | // Derived from: |
39 | | // test-assembler.h: interface to class for building complex binary streams. |
40 | | |
41 | | // To test the Breakpad symbol dumper and processor thoroughly, for |
42 | | // all combinations of host system and minidump processor |
43 | | // architecture, we need to be able to easily generate complex test |
44 | | // data like debugging information and minidump files. |
45 | | // |
46 | | // For example, if we want our unit tests to provide full code |
47 | | // coverage for stack walking, it may be difficult to persuade the |
48 | | // compiler to generate every possible sort of stack walking |
49 | | // information that we want to support; there are probably DWARF CFI |
50 | | // opcodes that GCC never emits. Similarly, if we want to test our |
51 | | // error handling, we will need to generate damaged minidumps or |
52 | | // debugging information that (we hope) the client or compiler will |
53 | | // never produce on its own. |
54 | | // |
55 | | // google_breakpad::TestAssembler provides a predictable and |
56 | | // (relatively) simple way to generate complex formatted data streams |
57 | | // like minidumps and CFI. Furthermore, because TestAssembler is |
58 | | // portable, developers without access to (say) Visual Studio or a |
59 | | // SPARC assembler can still work on test data for those targets. |
60 | | |
61 | | #ifndef LUL_TEST_INFRASTRUCTURE_H |
62 | | #define LUL_TEST_INFRASTRUCTURE_H |
63 | | |
64 | | #include <string> |
65 | | #include <vector> |
66 | | |
67 | | using std::string; |
68 | | using std::vector; |
69 | | |
70 | | namespace lul_test { |
71 | | namespace test_assembler { |
72 | | |
73 | | // A Label represents a value not yet known that we need to store in a |
74 | | // section. As long as all the labels a section refers to are defined |
75 | | // by the time we retrieve its contents as bytes, we can use undefined |
76 | | // labels freely in that section's construction. |
77 | | // |
78 | | // A label can be in one of three states: |
79 | | // - undefined, |
80 | | // - defined as the sum of some other label and a constant, or |
81 | | // - a constant. |
82 | | // |
83 | | // A label's value never changes, but it can accumulate constraints. |
84 | | // Adding labels and integers is permitted, and yields a label. |
85 | | // Subtracting a constant from a label is permitted, and also yields a |
86 | | // label. Subtracting two labels that have some relationship to each |
87 | | // other is permitted, and yields a constant. |
88 | | // |
89 | | // For example: |
90 | | // |
91 | | // Label a; // a's value is undefined |
92 | | // Label b; // b's value is undefined |
93 | | // { |
94 | | // Label c = a + 4; // okay, even though a's value is unknown |
95 | | // b = c + 4; // also okay; b is now a+8 |
96 | | // } |
97 | | // Label d = b - 2; // okay; d == a+6, even though c is gone |
98 | | // d.Value(); // error: d's value is not yet known |
99 | | // d - a; // is 6, even though their values are not known |
100 | | // a = 12; // now b == 20, and d == 18 |
101 | | // d.Value(); // 18: no longer an error |
102 | | // b.Value(); // 20 |
103 | | // d = 10; // error: d is already defined. |
104 | | // |
105 | | // Label objects' lifetimes are unconstrained: notice that, in the |
106 | | // above example, even though a and b are only related through c, and |
107 | | // c goes out of scope, the assignment to a sets b's value as well. In |
108 | | // particular, it's not necessary to ensure that a Label lives beyond |
109 | | // Sections that refer to it. |
110 | | class Label { |
111 | | public: |
112 | | Label(); // An undefined label. |
113 | | explicit Label(uint64_t value); // A label with a fixed value |
114 | | Label(const Label &value); // A label equal to another. |
115 | | ~Label(); |
116 | | |
117 | | Label &operator=(uint64_t value); |
118 | | Label &operator=(const Label &value); |
119 | | Label operator+(uint64_t addend) const; |
120 | | Label operator-(uint64_t subtrahend) const; |
121 | | uint64_t operator-(const Label &subtrahend) const; |
122 | | |
123 | | // We could also provide == and != that work on undefined, but |
124 | | // related, labels. |
125 | | |
126 | | // Return true if this label's value is known. If VALUE_P is given, |
127 | | // set *VALUE_P to the known value if returning true. |
128 | | bool IsKnownConstant(uint64_t *value_p = NULL) const; |
129 | | |
130 | | // Return true if the offset from LABEL to this label is known. If |
131 | | // OFFSET_P is given, set *OFFSET_P to the offset when returning true. |
132 | | // |
133 | | // You can think of l.KnownOffsetFrom(m, &d) as being like 'd = l-m', |
134 | | // except that it also returns a value indicating whether the |
135 | | // subtraction is possible given what we currently know of l and m. |
136 | | // It can be possible even if we don't know l and m's values. For |
137 | | // example: |
138 | | // |
139 | | // Label l, m; |
140 | | // m = l + 10; |
141 | | // l.IsKnownConstant(); // false |
142 | | // m.IsKnownConstant(); // false |
143 | | // uint64_t d; |
144 | | // l.IsKnownOffsetFrom(m, &d); // true, and sets d to -10. |
145 | | // l-m // -10 |
146 | | // m-l // 10 |
147 | | // m.Value() // error: m's value is not known |
148 | | bool IsKnownOffsetFrom(const Label &label, uint64_t *offset_p = NULL) const; |
149 | | |
150 | | private: |
151 | | // A label's value, or if that is not yet known, how the value is |
152 | | // related to other labels' values. A binding may be: |
153 | | // - a known constant, |
154 | | // - constrained to be equal to some other binding plus a constant, or |
155 | | // - unconstrained, and free to take on any value. |
156 | | // |
157 | | // Many labels may point to a single binding, and each binding may |
158 | | // refer to another, so bindings and labels form trees whose leaves |
159 | | // are labels, whose interior nodes (and roots) are bindings, and |
160 | | // where links point from children to parents. Bindings are |
161 | | // reference counted, allowing labels to be lightweight, copyable, |
162 | | // assignable, placed in containers, and so on. |
163 | | class Binding { |
164 | | public: |
165 | | Binding(); |
166 | | explicit Binding(uint64_t addend); |
167 | | ~Binding(); |
168 | | |
169 | | // Increment our reference count. |
170 | 0 | void Acquire() { reference_count_++; }; |
171 | | // Decrement our reference count, and return true if it is zero. |
172 | 0 | bool Release() { return --reference_count_ == 0; } |
173 | | |
174 | | // Set this binding to be equal to BINDING + ADDEND. If BINDING is |
175 | | // NULL, then set this binding to the known constant ADDEND. |
176 | | // Update every binding on this binding's chain to point directly |
177 | | // to BINDING, or to be a constant, with addends adjusted |
178 | | // appropriately. |
179 | | void Set(Binding *binding, uint64_t value); |
180 | | |
181 | | // Return what we know about the value of this binding. |
182 | | // - If this binding's value is a known constant, set BASE to |
183 | | // NULL, and set ADDEND to its value. |
184 | | // - If this binding is not a known constant but related to other |
185 | | // bindings, set BASE to the binding at the end of the relation |
186 | | // chain (which will always be unconstrained), and set ADDEND to the |
187 | | // value to add to that binding's value to get this binding's |
188 | | // value. |
189 | | // - If this binding is unconstrained, set BASE to this, and leave |
190 | | // ADDEND unchanged. |
191 | | void Get(Binding **base, uint64_t *addend); |
192 | | |
193 | | private: |
194 | | // There are three cases: |
195 | | // |
196 | | // - A binding representing a known constant value has base_ NULL, |
197 | | // and addend_ equal to the value. |
198 | | // |
199 | | // - A binding representing a completely unconstrained value has |
200 | | // base_ pointing to this; addend_ is unused. |
201 | | // |
202 | | // - A binding whose value is related to some other binding's |
203 | | // value has base_ pointing to that other binding, and addend_ |
204 | | // set to the amount to add to that binding's value to get this |
205 | | // binding's value. We only represent relationships of the form |
206 | | // x = y+c. |
207 | | // |
208 | | // Thus, the bind_ links form a chain terminating in either a |
209 | | // known constant value or a completely unconstrained value. Most |
210 | | // operations on bindings do path compression: they change every |
211 | | // binding on the chain to point directly to the final value, |
212 | | // adjusting addends as appropriate. |
213 | | Binding *base_; |
214 | | uint64_t addend_; |
215 | | |
216 | | // The number of Labels and Bindings pointing to this binding. |
217 | | // (When a binding points to itself, indicating a completely |
218 | | // unconstrained binding, that doesn't count as a reference.) |
219 | | int reference_count_; |
220 | | }; |
221 | | |
222 | | // This label's value. |
223 | | Binding *value_; |
224 | | }; |
225 | | |
226 | | // Conventions for representing larger numbers as sequences of bytes. |
227 | | enum Endianness { |
228 | | kBigEndian, // Big-endian: the most significant byte comes first. |
229 | | kLittleEndian, // Little-endian: the least significant byte comes first. |
230 | | kUnsetEndian, // used internally |
231 | | }; |
232 | | |
233 | | // A section is a sequence of bytes, constructed by appending bytes |
234 | | // to the end. Sections have a convenient and flexible set of member |
235 | | // functions for appending data in various formats: big-endian and |
236 | | // little-endian signed and unsigned values of different sizes; |
237 | | // LEB128 and ULEB128 values (see below), and raw blocks of bytes. |
238 | | // |
239 | | // If you need to append a value to a section that is not convenient |
240 | | // to compute immediately, you can create a label, append the |
241 | | // label's value to the section, and then set the label's value |
242 | | // later, when it's convenient to do so. Once a label's value is |
243 | | // known, the section class takes care of updating all previously |
244 | | // appended references to it. |
245 | | // |
246 | | // Once all the labels to which a section refers have had their |
247 | | // values determined, you can get a copy of the section's contents |
248 | | // as a string. |
249 | | // |
250 | | // Note that there is no specified "start of section" label. This is |
251 | | // because there are typically several different meanings for "the |
252 | | // start of a section": the offset of the section within an object |
253 | | // file, the address in memory at which the section's content appear, |
254 | | // and so on. It's up to the code that uses the Section class to |
255 | | // keep track of these explicitly, as they depend on the application. |
256 | | class Section { |
257 | | public: |
258 | | explicit Section(Endianness endianness = kUnsetEndian) |
259 | 0 | : endianness_(endianness) { }; |
260 | | |
261 | | // A base class destructor should be either public and virtual, |
262 | | // or protected and nonvirtual. |
263 | 0 | virtual ~Section() { }; |
264 | | |
265 | | // Return the default endianness of this section. |
266 | 0 | Endianness endianness() const { return endianness_; } |
267 | | |
268 | | // Append the SIZE bytes at DATA to the end of this section. Return |
269 | | // a reference to this section. |
270 | 0 | Section &Append(const string &data) { |
271 | 0 | contents_.append(data); |
272 | 0 | return *this; |
273 | 0 | }; |
274 | | |
275 | | // Append SIZE copies of BYTE to the end of this section. Return a |
276 | | // reference to this section. |
277 | 0 | Section &Append(size_t size, uint8_t byte) { |
278 | 0 | contents_.append(size, (char) byte); |
279 | 0 | return *this; |
280 | 0 | } |
281 | | |
282 | | // Append NUMBER to this section. ENDIANNESS is the endianness to |
283 | | // use to write the number. SIZE is the length of the number in |
284 | | // bytes. Return a reference to this section. |
285 | | Section &Append(Endianness endianness, size_t size, uint64_t number); |
286 | | Section &Append(Endianness endianness, size_t size, const Label &label); |
287 | | |
288 | | // Append SECTION to the end of this section. The labels SECTION |
289 | | // refers to need not be defined yet. |
290 | | // |
291 | | // Note that this has no effect on any Labels' values, or on |
292 | | // SECTION. If placing SECTION within 'this' provides new |
293 | | // constraints on existing labels' values, then it's up to the |
294 | | // caller to fiddle with those labels as needed. |
295 | | Section &Append(const Section §ion); |
296 | | |
297 | | // Append the contents of DATA as a series of bytes terminated by |
298 | | // a NULL character. |
299 | 0 | Section &AppendCString(const string &data) { |
300 | 0 | Append(data); |
301 | 0 | contents_ += '\0'; |
302 | 0 | return *this; |
303 | 0 | } |
304 | | |
305 | | // Append VALUE or LABEL to this section, with the given bit width and |
306 | | // endianness. Return a reference to this section. |
307 | | // |
308 | | // The names of these functions have the form <ENDIANNESS><BITWIDTH>: |
309 | | // <ENDIANNESS> is either 'L' (little-endian, least significant byte first), |
310 | | // 'B' (big-endian, most significant byte first), or |
311 | | // 'D' (default, the section's default endianness) |
312 | | // <BITWIDTH> is 8, 16, 32, or 64. |
313 | | // |
314 | | // Since endianness doesn't matter for a single byte, all the |
315 | | // <BITWIDTH>=8 functions are equivalent. |
316 | | // |
317 | | // These can be used to write both signed and unsigned values, as |
318 | | // the compiler will properly sign-extend a signed value before |
319 | | // passing it to the function, at which point the function's |
320 | | // behavior is the same either way. |
321 | 0 | Section &L8(uint8_t value) { contents_ += value; return *this; } |
322 | 0 | Section &B8(uint8_t value) { contents_ += value; return *this; } |
323 | 0 | Section &D8(uint8_t value) { contents_ += value; return *this; } |
324 | | Section &L16(uint16_t), &L32(uint32_t), &L64(uint64_t), |
325 | | &B16(uint16_t), &B32(uint32_t), &B64(uint64_t), |
326 | | &D16(uint16_t), &D32(uint32_t), &D64(uint64_t); |
327 | | Section &L8(const Label &label), &L16(const Label &label), |
328 | | &L32(const Label &label), &L64(const Label &label), |
329 | | &B8(const Label &label), &B16(const Label &label), |
330 | | &B32(const Label &label), &B64(const Label &label), |
331 | | &D8(const Label &label), &D16(const Label &label), |
332 | | &D32(const Label &label), &D64(const Label &label); |
333 | | |
334 | | // Append VALUE in a signed LEB128 (Little-Endian Base 128) form. |
335 | | // |
336 | | // The signed LEB128 representation of an integer N is a variable |
337 | | // number of bytes: |
338 | | // |
339 | | // - If N is between -0x40 and 0x3f, then its signed LEB128 |
340 | | // representation is a single byte whose value is N. |
341 | | // |
342 | | // - Otherwise, its signed LEB128 representation is (N & 0x7f) | |
343 | | // 0x80, followed by the signed LEB128 representation of N / 128, |
344 | | // rounded towards negative infinity. |
345 | | // |
346 | | // In other words, we break VALUE into groups of seven bits, put |
347 | | // them in little-endian order, and then write them as eight-bit |
348 | | // bytes with the high bit on all but the last. |
349 | | // |
350 | | // Note that VALUE cannot be a Label (we would have to implement |
351 | | // relaxation). |
352 | | Section &LEB128(long long value); |
353 | | |
354 | | // Append VALUE in unsigned LEB128 (Little-Endian Base 128) form. |
355 | | // |
356 | | // The unsigned LEB128 representation of an integer N is a variable |
357 | | // number of bytes: |
358 | | // |
359 | | // - If N is between 0 and 0x7f, then its unsigned LEB128 |
360 | | // representation is a single byte whose value is N. |
361 | | // |
362 | | // - Otherwise, its unsigned LEB128 representation is (N & 0x7f) | |
363 | | // 0x80, followed by the unsigned LEB128 representation of N / |
364 | | // 128, rounded towards negative infinity. |
365 | | // |
366 | | // Note that VALUE cannot be a Label (we would have to implement |
367 | | // relaxation). |
368 | | Section &ULEB128(uint64_t value); |
369 | | |
370 | | // Jump to the next location aligned on an ALIGNMENT-byte boundary, |
371 | | // relative to the start of the section. Fill the gap with PAD_BYTE. |
372 | | // ALIGNMENT must be a power of two. Return a reference to this |
373 | | // section. |
374 | | Section &Align(size_t alignment, uint8_t pad_byte = 0); |
375 | | |
376 | | // Return the current size of the section. |
377 | 0 | size_t Size() const { return contents_.size(); } |
378 | | |
379 | | // Return a label representing the start of the section. |
380 | | // |
381 | | // It is up to the user whether this label represents the section's |
382 | | // position in an object file, the section's address in memory, or |
383 | | // what have you; some applications may need both, in which case |
384 | | // this simple-minded interface won't be enough. This class only |
385 | | // provides a single start label, for use with the Here and Mark |
386 | | // member functions. |
387 | | // |
388 | | // Ideally, we'd provide this in a subclass that actually knows more |
389 | | // about the application at hand and can provide an appropriate |
390 | | // collection of start labels. But then the appending member |
391 | | // functions like Append and D32 would return a reference to the |
392 | | // base class, not the derived class, and the chaining won't work. |
393 | | // Since the only value here is in pretty notation, that's a fatal |
394 | | // flaw. |
395 | 0 | Label start() const { return start_; } |
396 | | |
397 | | // Return a label representing the point at which the next Appended |
398 | | // item will appear in the section, relative to start(). |
399 | 0 | Label Here() const { return start_ + Size(); } |
400 | | |
401 | | // Set *LABEL to Here, and return a reference to this section. |
402 | 0 | Section &Mark(Label *label) { *label = Here(); return *this; } |
403 | | |
404 | | // If there are no undefined label references left in this |
405 | | // section, set CONTENTS to the contents of this section, as a |
406 | | // string, and clear this section. Return true on success, or false |
407 | | // if there were still undefined labels. |
408 | | bool GetContents(string *contents); |
409 | | |
410 | | private: |
411 | | // Used internally. A reference to a label's value. |
412 | | struct Reference { |
413 | | Reference(size_t set_offset, Endianness set_endianness, size_t set_size, |
414 | | const Label &set_label) |
415 | | : offset(set_offset), endianness(set_endianness), size(set_size), |
416 | 0 | label(set_label) { } |
417 | | |
418 | | // The offset of the reference within the section. |
419 | | size_t offset; |
420 | | |
421 | | // The endianness of the reference. |
422 | | Endianness endianness; |
423 | | |
424 | | // The size of the reference. |
425 | | size_t size; |
426 | | |
427 | | // The label to which this is a reference. |
428 | | Label label; |
429 | | }; |
430 | | |
431 | | // The default endianness of this section. |
432 | | Endianness endianness_; |
433 | | |
434 | | // The contents of the section. |
435 | | string contents_; |
436 | | |
437 | | // References to labels within those contents. |
438 | | vector<Reference> references_; |
439 | | |
440 | | // A label referring to the beginning of the section. |
441 | | Label start_; |
442 | | }; |
443 | | |
444 | | } // namespace test_assembler |
445 | | } // namespace lul_test |
446 | | |
447 | | |
448 | | namespace lul_test { |
449 | | |
450 | | using lul::DwarfPointerEncoding; |
451 | | using lul_test::test_assembler::Endianness; |
452 | | using lul_test::test_assembler::Label; |
453 | | using lul_test::test_assembler::Section; |
454 | | |
455 | | class CFISection: public Section { |
456 | | public: |
457 | | |
458 | | // CFI augmentation strings beginning with 'z', defined by the |
459 | | // Linux/IA-64 C++ ABI, can specify interesting encodings for |
460 | | // addresses appearing in FDE headers and call frame instructions (and |
461 | | // for additional fields whose presence the augmentation string |
462 | | // specifies). In particular, pointers can be specified to be relative |
463 | | // to various base address: the start of the .text section, the |
464 | | // location holding the address itself, and so on. These allow the |
465 | | // frame data to be position-independent even when they live in |
466 | | // write-protected pages. These variants are specified at the |
467 | | // following two URLs: |
468 | | // |
469 | | // http://refspecs.linux-foundation.org/LSB_4.0.0/LSB-Core-generic/LSB-Core-generic/dwarfext.html |
470 | | // http://refspecs.linux-foundation.org/LSB_4.0.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html |
471 | | // |
472 | | // CFISection leaves the production of well-formed 'z'-augmented CIEs and |
473 | | // FDEs to the user, but does provide EncodedPointer, to emit |
474 | | // properly-encoded addresses for a given pointer encoding. |
475 | | // EncodedPointer uses an instance of this structure to find the base |
476 | | // addresses it should use; you can establish a default for all encoded |
477 | | // pointers appended to this section with SetEncodedPointerBases. |
478 | | struct EncodedPointerBases { |
479 | 0 | EncodedPointerBases() : cfi(), text(), data() { } |
480 | | |
481 | | // The starting address of this CFI section in memory, for |
482 | | // DW_EH_PE_pcrel. DW_EH_PE_pcrel pointers may only be used in data |
483 | | // that has is loaded into the program's address space. |
484 | | uint64_t cfi; |
485 | | |
486 | | // The starting address of this file's .text section, for DW_EH_PE_textrel. |
487 | | uint64_t text; |
488 | | |
489 | | // The starting address of this file's .got or .eh_frame_hdr section, |
490 | | // for DW_EH_PE_datarel. |
491 | | uint64_t data; |
492 | | }; |
493 | | |
494 | | // Create a CFISection whose endianness is ENDIANNESS, and where |
495 | | // machine addresses are ADDRESS_SIZE bytes long. If EH_FRAME is |
496 | | // true, use the .eh_frame format, as described by the Linux |
497 | | // Standards Base Core Specification, instead of the DWARF CFI |
498 | | // format. |
499 | | CFISection(Endianness endianness, size_t address_size, |
500 | | bool eh_frame = false) |
501 | | : Section(endianness), address_size_(address_size), eh_frame_(eh_frame), |
502 | | pointer_encoding_(lul::DW_EH_PE_absptr), |
503 | 0 | encoded_pointer_bases_(), entry_length_(NULL), in_fde_(false) { |
504 | 0 | // The 'start', 'Here', and 'Mark' members of a CFISection all refer |
505 | 0 | // to section offsets. |
506 | 0 | start() = 0; |
507 | 0 | } |
508 | | |
509 | | // Return this CFISection's address size. |
510 | 0 | size_t AddressSize() const { return address_size_; } |
511 | | |
512 | | // Return true if this CFISection uses the .eh_frame format, or |
513 | | // false if it contains ordinary DWARF CFI data. |
514 | 0 | bool ContainsEHFrame() const { return eh_frame_; } |
515 | | |
516 | | // Use ENCODING for pointers in calls to FDEHeader and EncodedPointer. |
517 | 0 | void SetPointerEncoding(DwarfPointerEncoding encoding) { |
518 | 0 | pointer_encoding_ = encoding; |
519 | 0 | } |
520 | | |
521 | | // Use the addresses in BASES as the base addresses for encoded |
522 | | // pointers in subsequent calls to FDEHeader or EncodedPointer. |
523 | | // This function makes a copy of BASES. |
524 | 0 | void SetEncodedPointerBases(const EncodedPointerBases &bases) { |
525 | 0 | encoded_pointer_bases_ = bases; |
526 | 0 | } |
527 | | |
528 | | // Append a Common Information Entry header to this section with the |
529 | | // given values. If dwarf64 is true, use the 64-bit DWARF initial |
530 | | // length format for the CIE's initial length. Return a reference to |
531 | | // this section. You should call FinishEntry after writing the last |
532 | | // instruction for the CIE. |
533 | | // |
534 | | // Before calling this function, you will typically want to use Mark |
535 | | // or Here to make a label to pass to FDEHeader that refers to this |
536 | | // CIE's position in the section. |
537 | | CFISection &CIEHeader(uint64_t code_alignment_factor, |
538 | | int data_alignment_factor, |
539 | | unsigned return_address_register, |
540 | | uint8_t version = 3, |
541 | | const string &augmentation = "", |
542 | | bool dwarf64 = false); |
543 | | |
544 | | // Append a Frame Description Entry header to this section with the |
545 | | // given values. If dwarf64 is true, use the 64-bit DWARF initial |
546 | | // length format for the CIE's initial length. Return a reference to |
547 | | // this section. You should call FinishEntry after writing the last |
548 | | // instruction for the CIE. |
549 | | // |
550 | | // This function doesn't support entries that are longer than |
551 | | // 0xffffff00 bytes. (The "initial length" is always a 32-bit |
552 | | // value.) Nor does it support .debug_frame sections longer than |
553 | | // 0xffffff00 bytes. |
554 | | CFISection &FDEHeader(Label cie_pointer, |
555 | | uint64_t initial_location, |
556 | | uint64_t address_range, |
557 | | bool dwarf64 = false); |
558 | | |
559 | | // Note the current position as the end of the last CIE or FDE we |
560 | | // started, after padding with DW_CFA_nops for alignment. This |
561 | | // defines the label representing the entry's length, cited in the |
562 | | // entry's header. Return a reference to this section. |
563 | | CFISection &FinishEntry(); |
564 | | |
565 | | // Append the contents of BLOCK as a DW_FORM_block value: an |
566 | | // unsigned LEB128 length, followed by that many bytes of data. |
567 | 0 | CFISection &Block(const string &block) { |
568 | 0 | ULEB128(block.size()); |
569 | 0 | Append(block); |
570 | 0 | return *this; |
571 | 0 | } |
572 | | |
573 | | // Append ADDRESS to this section, in the appropriate size and |
574 | | // endianness. Return a reference to this section. |
575 | 0 | CFISection &Address(uint64_t address) { |
576 | 0 | Section::Append(endianness(), address_size_, address); |
577 | 0 | return *this; |
578 | 0 | } |
579 | | |
580 | | // Append ADDRESS to this section, using ENCODING and BASES. ENCODING |
581 | | // defaults to this section's default encoding, established by |
582 | | // SetPointerEncoding. BASES defaults to this section's bases, set by |
583 | | // SetEncodedPointerBases. If the DW_EH_PE_indirect bit is set in the |
584 | | // encoding, assume that ADDRESS is where the true address is stored. |
585 | | // Return a reference to this section. |
586 | | // |
587 | | // (C++ doesn't let me use default arguments here, because I want to |
588 | | // refer to members of *this in the default argument expression.) |
589 | 0 | CFISection &EncodedPointer(uint64_t address) { |
590 | 0 | return EncodedPointer(address, pointer_encoding_, encoded_pointer_bases_); |
591 | 0 | } |
592 | 0 | CFISection &EncodedPointer(uint64_t address, DwarfPointerEncoding encoding) { |
593 | 0 | return EncodedPointer(address, encoding, encoded_pointer_bases_); |
594 | 0 | } |
595 | | CFISection &EncodedPointer(uint64_t address, DwarfPointerEncoding encoding, |
596 | | const EncodedPointerBases &bases); |
597 | | |
598 | | // Restate some member functions, to keep chaining working nicely. |
599 | 0 | CFISection &Mark(Label *label) { Section::Mark(label); return *this; } |
600 | 0 | CFISection &D8(uint8_t v) { Section::D8(v); return *this; } |
601 | 0 | CFISection &D16(uint16_t v) { Section::D16(v); return *this; } |
602 | 0 | CFISection &D16(Label v) { Section::D16(v); return *this; } |
603 | 0 | CFISection &D32(uint32_t v) { Section::D32(v); return *this; } |
604 | 0 | CFISection &D32(const Label &v) { Section::D32(v); return *this; } |
605 | 0 | CFISection &D64(uint64_t v) { Section::D64(v); return *this; } |
606 | 0 | CFISection &D64(const Label &v) { Section::D64(v); return *this; } |
607 | 0 | CFISection &LEB128(long long v) { Section::LEB128(v); return *this; } |
608 | 0 | CFISection &ULEB128(uint64_t v) { Section::ULEB128(v); return *this; } |
609 | | |
610 | | private: |
611 | | // A length value that we've appended to the section, but is not yet |
612 | | // known. LENGTH is the appended value; START is a label referring |
613 | | // to the start of the data whose length was cited. |
614 | | struct PendingLength { |
615 | | Label length; |
616 | | Label start; |
617 | | }; |
618 | | |
619 | | // Constants used in CFI/.eh_frame data: |
620 | | |
621 | | // If the first four bytes of an "initial length" are this constant, then |
622 | | // the data uses the 64-bit DWARF format, and the length itself is the |
623 | | // subsequent eight bytes. |
624 | | static const uint32_t kDwarf64InitialLengthMarker = 0xffffffffU; |
625 | | |
626 | | // The CIE identifier for 32- and 64-bit DWARF CFI and .eh_frame data. |
627 | | static const uint32_t kDwarf32CIEIdentifier = ~(uint32_t)0; |
628 | | static const uint64_t kDwarf64CIEIdentifier = ~(uint64_t)0; |
629 | | static const uint32_t kEHFrame32CIEIdentifier = 0; |
630 | | static const uint64_t kEHFrame64CIEIdentifier = 0; |
631 | | |
632 | | // The size of a machine address for the data in this section. |
633 | | size_t address_size_; |
634 | | |
635 | | // If true, we are generating a Linux .eh_frame section, instead of |
636 | | // a standard DWARF .debug_frame section. |
637 | | bool eh_frame_; |
638 | | |
639 | | // The encoding to use for FDE pointers. |
640 | | DwarfPointerEncoding pointer_encoding_; |
641 | | |
642 | | // The base addresses to use when emitting encoded pointers. |
643 | | EncodedPointerBases encoded_pointer_bases_; |
644 | | |
645 | | // The length value for the current entry. |
646 | | // |
647 | | // Oddly, this must be dynamically allocated. Labels never get new |
648 | | // values; they only acquire constraints on the value they already |
649 | | // have, or assert if you assign them something incompatible. So |
650 | | // each header needs truly fresh Label objects to cite in their |
651 | | // headers and track their positions. The alternative is explicit |
652 | | // destructor invocation and a placement new. Ick. |
653 | | PendingLength *entry_length_; |
654 | | |
655 | | // True if we are currently emitting an FDE --- that is, we have |
656 | | // called FDEHeader but have not yet called FinishEntry. |
657 | | bool in_fde_; |
658 | | |
659 | | // If in_fde_ is true, this is its starting address. We use this for |
660 | | // emitting DW_EH_PE_funcrel pointers. |
661 | | uint64_t fde_start_address_; |
662 | | }; |
663 | | |
664 | | } // namespace lul_test |
665 | | |
666 | | #endif // LUL_TEST_INFRASTRUCTURE_H |