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.
Acquire()170     void Acquire() { reference_count_++; };
171     // Decrement our reference count, and return true if it is zero.
Release()172     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)
endianness_(endianness)259       : endianness_(endianness){};
260 
261   // A base class destructor should be either public and virtual,
262   // or protected and nonvirtual.
~Section()263   virtual ~Section(){};
264 
265   // Return the default endianness of this section.
endianness()266   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.
Append(const string & data)270   Section& Append(const string& data) {
271     contents_.append(data);
272     return *this;
273   };
274 
275   // Append SIZE copies of BYTE to the end of this section. Return a
276   // reference to this section.
Append(size_t size,uint8_t byte)277   Section& Append(size_t size, uint8_t byte) {
278     contents_.append(size, (char)byte);
279     return *this;
280   }
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& section);
296 
297   // Append the contents of DATA as a series of bytes terminated by
298   // a NULL character.
AppendCString(const string & data)299   Section& AppendCString(const string& data) {
300     Append(data);
301     contents_ += '\0';
302     return *this;
303   }
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.
L8(uint8_t value)321   Section& L8(uint8_t value) {
322     contents_ += value;
323     return *this;
324   }
B8(uint8_t value)325   Section& B8(uint8_t value) {
326     contents_ += value;
327     return *this;
328   }
D8(uint8_t value)329   Section& D8(uint8_t value) {
330     contents_ += value;
331     return *this;
332   }
333   Section &L16(uint16_t), &L32(uint32_t), &L64(uint64_t), &B16(uint16_t),
334       &B32(uint32_t), &B64(uint64_t), &D16(uint16_t), &D32(uint32_t),
335       &D64(uint64_t);
336   Section &L8(const Label&label), &L16(const Label&label),
337       &L32(const Label&label), &L64(const Label&label), &B8(const Label&label),
338       &B16(const Label&label), &B32(const Label&label), &B64(const Label&label),
339       &D8(const Label&label), &D16(const Label&label), &D32(const Label&label),
340       &D64(const Label&label);
341 
342   // Append VALUE in a signed LEB128 (Little-Endian Base 128) form.
343   //
344   // The signed LEB128 representation of an integer N is a variable
345   // number of bytes:
346   //
347   // - If N is between -0x40 and 0x3f, then its signed LEB128
348   //   representation is a single byte whose value is N.
349   //
350   // - Otherwise, its signed LEB128 representation is (N & 0x7f) |
351   //   0x80, followed by the signed LEB128 representation of N / 128,
352   //   rounded towards negative infinity.
353   //
354   // In other words, we break VALUE into groups of seven bits, put
355   // them in little-endian order, and then write them as eight-bit
356   // bytes with the high bit on all but the last.
357   //
358   // Note that VALUE cannot be a Label (we would have to implement
359   // relaxation).
360   Section& LEB128(long long value);
361 
362   // Append VALUE in unsigned LEB128 (Little-Endian Base 128) form.
363   //
364   // The unsigned LEB128 representation of an integer N is a variable
365   // number of bytes:
366   //
367   // - If N is between 0 and 0x7f, then its unsigned LEB128
368   //   representation is a single byte whose value is N.
369   //
370   // - Otherwise, its unsigned LEB128 representation is (N & 0x7f) |
371   //   0x80, followed by the unsigned LEB128 representation of N /
372   //   128, rounded towards negative infinity.
373   //
374   // Note that VALUE cannot be a Label (we would have to implement
375   // relaxation).
376   Section& ULEB128(uint64_t value);
377 
378   // Jump to the next location aligned on an ALIGNMENT-byte boundary,
379   // relative to the start of the section. Fill the gap with PAD_BYTE.
380   // ALIGNMENT must be a power of two. Return a reference to this
381   // section.
382   Section& Align(size_t alignment, uint8_t pad_byte = 0);
383 
384   // Return the current size of the section.
Size()385   size_t Size() const { return contents_.size(); }
386 
387   // Return a label representing the start of the section.
388   //
389   // It is up to the user whether this label represents the section's
390   // position in an object file, the section's address in memory, or
391   // what have you; some applications may need both, in which case
392   // this simple-minded interface won't be enough. This class only
393   // provides a single start label, for use with the Here and Mark
394   // member functions.
395   //
396   // Ideally, we'd provide this in a subclass that actually knows more
397   // about the application at hand and can provide an appropriate
398   // collection of start labels. But then the appending member
399   // functions like Append and D32 would return a reference to the
400   // base class, not the derived class, and the chaining won't work.
401   // Since the only value here is in pretty notation, that's a fatal
402   // flaw.
start()403   Label start() const { return start_; }
404 
405   // Return a label representing the point at which the next Appended
406   // item will appear in the section, relative to start().
Here()407   Label Here() const { return start_ + Size(); }
408 
409   // Set *LABEL to Here, and return a reference to this section.
Mark(Label * label)410   Section& Mark(Label* label) {
411     *label = Here();
412     return *this;
413   }
414 
415   // If there are no undefined label references left in this
416   // section, set CONTENTS to the contents of this section, as a
417   // string, and clear this section. Return true on success, or false
418   // if there were still undefined labels.
419   bool GetContents(string* contents);
420 
421  private:
422   // Used internally. A reference to a label's value.
423   struct Reference {
ReferenceReference424     Reference(size_t set_offset, Endianness set_endianness, size_t set_size,
425               const Label& set_label)
426         : offset(set_offset),
427           endianness(set_endianness),
428           size(set_size),
429           label(set_label) {}
430 
431     // The offset of the reference within the section.
432     size_t offset;
433 
434     // The endianness of the reference.
435     Endianness endianness;
436 
437     // The size of the reference.
438     size_t size;
439 
440     // The label to which this is a reference.
441     Label label;
442   };
443 
444   // The default endianness of this section.
445   Endianness endianness_;
446 
447   // The contents of the section.
448   string contents_;
449 
450   // References to labels within those contents.
451   vector<Reference> references_;
452 
453   // A label referring to the beginning of the section.
454   Label start_;
455 };
456 
457 }  // namespace test_assembler
458 }  // namespace lul_test
459 
460 namespace lul_test {
461 
462 using lul::DwarfPointerEncoding;
463 using lul_test::test_assembler::Endianness;
464 using lul_test::test_assembler::Label;
465 using lul_test::test_assembler::Section;
466 
467 class CFISection : public Section {
468  public:
469   // CFI augmentation strings beginning with 'z', defined by the
470   // Linux/IA-64 C++ ABI, can specify interesting encodings for
471   // addresses appearing in FDE headers and call frame instructions (and
472   // for additional fields whose presence the augmentation string
473   // specifies). In particular, pointers can be specified to be relative
474   // to various base address: the start of the .text section, the
475   // location holding the address itself, and so on. These allow the
476   // frame data to be position-independent even when they live in
477   // write-protected pages. These variants are specified at the
478   // following two URLs:
479   //
480   // http://refspecs.linux-foundation.org/LSB_4.0.0/LSB-Core-generic/LSB-Core-generic/dwarfext.html
481   // http://refspecs.linux-foundation.org/LSB_4.0.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html
482   //
483   // CFISection leaves the production of well-formed 'z'-augmented CIEs and
484   // FDEs to the user, but does provide EncodedPointer, to emit
485   // properly-encoded addresses for a given pointer encoding.
486   // EncodedPointer uses an instance of this structure to find the base
487   // addresses it should use; you can establish a default for all encoded
488   // pointers appended to this section with SetEncodedPointerBases.
489   struct EncodedPointerBases {
EncodedPointerBasesEncodedPointerBases490     EncodedPointerBases() : cfi(), text(), data() {}
491 
492     // The starting address of this CFI section in memory, for
493     // DW_EH_PE_pcrel. DW_EH_PE_pcrel pointers may only be used in data
494     // that has is loaded into the program's address space.
495     uint64_t cfi;
496 
497     // The starting address of this file's .text section, for DW_EH_PE_textrel.
498     uint64_t text;
499 
500     // The starting address of this file's .got or .eh_frame_hdr section,
501     // for DW_EH_PE_datarel.
502     uint64_t data;
503   };
504 
505   // Create a CFISection whose endianness is ENDIANNESS, and where
506   // machine addresses are ADDRESS_SIZE bytes long. If EH_FRAME is
507   // true, use the .eh_frame format, as described by the Linux
508   // Standards Base Core Specification, instead of the DWARF CFI
509   // format.
510   CFISection(Endianness endianness, size_t address_size, bool eh_frame = false)
Section(endianness)511       : Section(endianness),
512         address_size_(address_size),
513         eh_frame_(eh_frame),
514         pointer_encoding_(lul::DW_EH_PE_absptr),
515         encoded_pointer_bases_(),
516         entry_length_(NULL),
517         in_fde_(false) {
518     // The 'start', 'Here', and 'Mark' members of a CFISection all refer
519     // to section offsets.
520     start() = 0;
521   }
522 
523   // Return this CFISection's address size.
AddressSize()524   size_t AddressSize() const { return address_size_; }
525 
526   // Return true if this CFISection uses the .eh_frame format, or
527   // false if it contains ordinary DWARF CFI data.
ContainsEHFrame()528   bool ContainsEHFrame() const { return eh_frame_; }
529 
530   // Use ENCODING for pointers in calls to FDEHeader and EncodedPointer.
SetPointerEncoding(DwarfPointerEncoding encoding)531   void SetPointerEncoding(DwarfPointerEncoding encoding) {
532     pointer_encoding_ = encoding;
533   }
534 
535   // Use the addresses in BASES as the base addresses for encoded
536   // pointers in subsequent calls to FDEHeader or EncodedPointer.
537   // This function makes a copy of BASES.
SetEncodedPointerBases(const EncodedPointerBases & bases)538   void SetEncodedPointerBases(const EncodedPointerBases& bases) {
539     encoded_pointer_bases_ = bases;
540   }
541 
542   // Append a Common Information Entry header to this section with the
543   // given values. If dwarf64 is true, use the 64-bit DWARF initial
544   // length format for the CIE's initial length. Return a reference to
545   // this section. You should call FinishEntry after writing the last
546   // instruction for the CIE.
547   //
548   // Before calling this function, you will typically want to use Mark
549   // or Here to make a label to pass to FDEHeader that refers to this
550   // CIE's position in the section.
551   CFISection& CIEHeader(uint64_t code_alignment_factor,
552                         int data_alignment_factor,
553                         unsigned return_address_register, uint8_t version = 3,
554                         const string& augmentation = "", bool dwarf64 = false);
555 
556   // Append a Frame Description Entry header to this section with the
557   // given values. If dwarf64 is true, use the 64-bit DWARF initial
558   // length format for the CIE's initial length. Return a reference to
559   // this section. You should call FinishEntry after writing the last
560   // instruction for the CIE.
561   //
562   // This function doesn't support entries that are longer than
563   // 0xffffff00 bytes. (The "initial length" is always a 32-bit
564   // value.) Nor does it support .debug_frame sections longer than
565   // 0xffffff00 bytes.
566   CFISection& FDEHeader(Label cie_pointer, uint64_t initial_location,
567                         uint64_t address_range, bool dwarf64 = false);
568 
569   // Note the current position as the end of the last CIE or FDE we
570   // started, after padding with DW_CFA_nops for alignment. This
571   // defines the label representing the entry's length, cited in the
572   // entry's header. Return a reference to this section.
573   CFISection& FinishEntry();
574 
575   // Append the contents of BLOCK as a DW_FORM_block value: an
576   // unsigned LEB128 length, followed by that many bytes of data.
Block(const string & block)577   CFISection& Block(const string& block) {
578     ULEB128(block.size());
579     Append(block);
580     return *this;
581   }
582 
583   // Append ADDRESS to this section, in the appropriate size and
584   // endianness. Return a reference to this section.
Address(uint64_t address)585   CFISection& Address(uint64_t address) {
586     Section::Append(endianness(), address_size_, address);
587     return *this;
588   }
589 
590   // Append ADDRESS to this section, using ENCODING and BASES. ENCODING
591   // defaults to this section's default encoding, established by
592   // SetPointerEncoding. BASES defaults to this section's bases, set by
593   // SetEncodedPointerBases. If the DW_EH_PE_indirect bit is set in the
594   // encoding, assume that ADDRESS is where the true address is stored.
595   // Return a reference to this section.
596   //
597   // (C++ doesn't let me use default arguments here, because I want to
598   // refer to members of *this in the default argument expression.)
EncodedPointer(uint64_t address)599   CFISection& EncodedPointer(uint64_t address) {
600     return EncodedPointer(address, pointer_encoding_, encoded_pointer_bases_);
601   }
EncodedPointer(uint64_t address,DwarfPointerEncoding encoding)602   CFISection& EncodedPointer(uint64_t address, DwarfPointerEncoding encoding) {
603     return EncodedPointer(address, encoding, encoded_pointer_bases_);
604   }
605   CFISection& EncodedPointer(uint64_t address, DwarfPointerEncoding encoding,
606                              const EncodedPointerBases& bases);
607 
608   // Restate some member functions, to keep chaining working nicely.
Mark(Label * label)609   CFISection& Mark(Label* label) {
610     Section::Mark(label);
611     return *this;
612   }
D8(uint8_t v)613   CFISection& D8(uint8_t v) {
614     Section::D8(v);
615     return *this;
616   }
D16(uint16_t v)617   CFISection& D16(uint16_t v) {
618     Section::D16(v);
619     return *this;
620   }
D16(Label v)621   CFISection& D16(Label v) {
622     Section::D16(v);
623     return *this;
624   }
D32(uint32_t v)625   CFISection& D32(uint32_t v) {
626     Section::D32(v);
627     return *this;
628   }
D32(const Label & v)629   CFISection& D32(const Label& v) {
630     Section::D32(v);
631     return *this;
632   }
D64(uint64_t v)633   CFISection& D64(uint64_t v) {
634     Section::D64(v);
635     return *this;
636   }
D64(const Label & v)637   CFISection& D64(const Label& v) {
638     Section::D64(v);
639     return *this;
640   }
LEB128(long long v)641   CFISection& LEB128(long long v) {
642     Section::LEB128(v);
643     return *this;
644   }
ULEB128(uint64_t v)645   CFISection& ULEB128(uint64_t v) {
646     Section::ULEB128(v);
647     return *this;
648   }
649 
650  private:
651   // A length value that we've appended to the section, but is not yet
652   // known. LENGTH is the appended value; START is a label referring
653   // to the start of the data whose length was cited.
654   struct PendingLength {
655     Label length;
656     Label start;
657   };
658 
659   // Constants used in CFI/.eh_frame data:
660 
661   // If the first four bytes of an "initial length" are this constant, then
662   // the data uses the 64-bit DWARF format, and the length itself is the
663   // subsequent eight bytes.
664   static const uint32_t kDwarf64InitialLengthMarker = 0xffffffffU;
665 
666   // The CIE identifier for 32- and 64-bit DWARF CFI and .eh_frame data.
667   static const uint32_t kDwarf32CIEIdentifier = ~(uint32_t)0;
668   static const uint64_t kDwarf64CIEIdentifier = ~(uint64_t)0;
669   static const uint32_t kEHFrame32CIEIdentifier = 0;
670   static const uint64_t kEHFrame64CIEIdentifier = 0;
671 
672   // The size of a machine address for the data in this section.
673   size_t address_size_;
674 
675   // If true, we are generating a Linux .eh_frame section, instead of
676   // a standard DWARF .debug_frame section.
677   bool eh_frame_;
678 
679   // The encoding to use for FDE pointers.
680   DwarfPointerEncoding pointer_encoding_;
681 
682   // The base addresses to use when emitting encoded pointers.
683   EncodedPointerBases encoded_pointer_bases_;
684 
685   // The length value for the current entry.
686   //
687   // Oddly, this must be dynamically allocated. Labels never get new
688   // values; they only acquire constraints on the value they already
689   // have, or assert if you assign them something incompatible. So
690   // each header needs truly fresh Label objects to cite in their
691   // headers and track their positions. The alternative is explicit
692   // destructor invocation and a placement new. Ick.
693   PendingLength* entry_length_;
694 
695   // True if we are currently emitting an FDE --- that is, we have
696   // called FDEHeader but have not yet called FinishEntry.
697   bool in_fde_;
698 
699   // If in_fde_ is true, this is its starting address. We use this for
700   // emitting DW_EH_PE_funcrel pointers.
701   uint64_t fde_start_address_;
702 };
703 
704 }  // namespace lul_test
705 
706 #endif  // LUL_TEST_INFRASTRUCTURE_H
707