1 // Copyright (c) 2011, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 // elf_core_dump.h: Define the google_breakpad::ElfCoreDump class, which
31 // encapsulates an ELF core dump file mapped into memory.
32 
33 #ifndef COMMON_LINUX_ELF_CORE_DUMP_H_
34 #define COMMON_LINUX_ELF_CORE_DUMP_H_
35 
36 #include <elf.h>
37 #include <link.h>
38 #include <stddef.h>
39 
40 #include "common/memory_range.h"
41 
42 namespace google_breakpad {
43 
44 // A class encapsulating an ELF core dump file mapped into memory, which
45 // provides methods for accessing program headers and the note section.
46 class ElfCoreDump {
47  public:
48   // ELF types based on the value of __WORDSIZE.
49   typedef ElfW(Ehdr) Ehdr;
50   typedef ElfW(Nhdr) Nhdr;
51   typedef ElfW(Phdr) Phdr;
52   typedef ElfW(Word) Word;
53   typedef ElfW(Addr) Addr;
54 #if __WORDSIZE == 32
55   static const int kClass = ELFCLASS32;
56 #elif __WORDSIZE == 64
57   static const int kClass = ELFCLASS64;
58 #else
59 #error "Unsupported __WORDSIZE for ElfCoreDump."
60 #endif
61 
62   // A class encapsulating the note content in a core dump, which provides
63   // methods for accessing the name and description of a note.
64   class Note {
65    public:
66     Note();
67 
68     // Constructor that takes the note content from |content|.
69     explicit Note(const MemoryRange& content);
70 
71     // Returns true if this note is valid, i,e. a note header is found in
72     // |content_|, or false otherwise.
73     bool IsValid() const;
74 
75     // Returns the note header, or NULL if no note header is found in
76     // |content_|.
77     const Nhdr* GetHeader() const;
78 
79     // Returns the note type, or 0 if no note header is found in |content_|.
80     Word GetType() const;
81 
82     // Returns a memory range covering the note name, or an empty range
83     // if no valid note name is found in |content_|.
84     MemoryRange GetName() const;
85 
86     // Returns a memory range covering the note description, or an empty
87     // range if no valid note description is found in |content_|.
88     MemoryRange GetDescription() const;
89 
90     // Returns the note following this note, or an empty note if no valid
91     // note is found after this note.
92     Note GetNextNote() const;
93 
94    private:
95     // Returns the size in bytes round up to the word alignment, specified
96     // for the note section, of a given size in bytes.
97     static size_t AlignedSize(size_t size);
98 
99     // Note content.
100     MemoryRange content_;
101   };
102 
103   ElfCoreDump();
104 
105   // Constructor that takes the core dump content from |content|.
106   explicit ElfCoreDump(const MemoryRange& content);
107 
108   // Sets the core dump content to |content|.
109   void SetContent(const MemoryRange& content);
110 
111   // Returns true if a valid ELF header in the core dump, or false otherwise.
112   bool IsValid() const;
113 
114   // Returns the ELF header in the core dump, or NULL if no ELF header
115   // is found in |content_|.
116   const Ehdr* GetHeader() const;
117 
118   // Returns the |index|-th program header in the core dump, or NULL if no
119   // ELF header is found in |content_| or |index| is out of bounds.
120   const Phdr* GetProgramHeader(unsigned index) const;
121 
122   // Returns the first program header of |type| in the core dump, or NULL if
123   // no ELF header is found in |content_| or no program header of |type| is
124   // found.
125   const Phdr* GetFirstProgramHeaderOfType(Word type) const;
126 
127   // Returns the number of program headers in the core dump, or 0 if no
128   // ELF header is found in |content_|.
129   unsigned GetProgramHeaderCount() const;
130 
131   // Copies |length| bytes of data starting at |virtual_address| in the core
132   // dump to |buffer|. |buffer| should be a valid pointer to a buffer of at
133   // least |length| bytes. Returns true if the data to be copied is found in
134   // the core dump, or false otherwise.
135   bool CopyData(void* buffer, Addr virtual_address, size_t length);
136 
137   // Returns the first note found in the note section of the core dump, or
138   // an empty note if no note is found.
139   Note GetFirstNote() const;
140 
141  private:
142   // Core dump content.
143   MemoryRange content_;
144 };
145 
146 }  // namespace google_breakpad
147 
148 #endif  // COMMON_LINUX_ELF_CORE_DUMP_H_
149