• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <limits.h>
38 #include <link.h>
39 #include <stddef.h>
40 
41 #include "common/memory_range.h"
42 
43 namespace google_breakpad {
44 
45 // A class encapsulating an ELF core dump file mapped into memory, which
46 // provides methods for accessing program headers and the note section.
47 class ElfCoreDump {
48  public:
49   // ELF types based on the native word size.
50   typedef ElfW(Ehdr) Ehdr;
51   typedef ElfW(Nhdr) Nhdr;
52   typedef ElfW(Phdr) Phdr;
53   typedef ElfW(Word) Word;
54   typedef ElfW(Addr) Addr;
55 #if ULONG_MAX == 0xffffffff
56   static const int kClass = ELFCLASS32;
57 #elif ULONG_MAX == 0xffffffffffffffff
58   static const int kClass = ELFCLASS64;
59 #else
60 #error "Unsupported word size for ElfCoreDump."
61 #endif
62 
63   // A class encapsulating the note content in a core dump, which provides
64   // methods for accessing the name and description of a note.
65   class Note {
66    public:
67     Note();
68 
69     // Constructor that takes the note content from |content|.
70     explicit Note(const MemoryRange& content);
71 
72     // Returns true if this note is valid, i,e. a note header is found in
73     // |content_|, or false otherwise.
74     bool IsValid() const;
75 
76     // Returns the note header, or NULL if no note header is found in
77     // |content_|.
78     const Nhdr* GetHeader() const;
79 
80     // Returns the note type, or 0 if no note header is found in |content_|.
81     Word GetType() const;
82 
83     // Returns a memory range covering the note name, or an empty range
84     // if no valid note name is found in |content_|.
85     MemoryRange GetName() const;
86 
87     // Returns a memory range covering the note description, or an empty
88     // range if no valid note description is found in |content_|.
89     MemoryRange GetDescription() const;
90 
91     // Returns the note following this note, or an empty note if no valid
92     // note is found after this note.
93     Note GetNextNote() const;
94 
95    private:
96     // Returns the size in bytes round up to the word alignment, specified
97     // for the note section, of a given size in bytes.
98     static size_t AlignedSize(size_t size);
99 
100     // Note content.
101     MemoryRange content_;
102   };
103 
104   ElfCoreDump();
105 
106   // Constructor that takes the core dump content from |content|.
107   explicit ElfCoreDump(const MemoryRange& content);
108 
109   // Sets the core dump content to |content|.
110   void SetContent(const MemoryRange& content);
111 
112   // Returns true if a valid ELF header in the core dump, or false otherwise.
113   bool IsValid() const;
114 
115   // Returns the ELF header in the core dump, or NULL if no ELF header
116   // is found in |content_|.
117   const Ehdr* GetHeader() const;
118 
119   // Returns the |index|-th program header in the core dump, or NULL if no
120   // ELF header is found in |content_| or |index| is out of bounds.
121   const Phdr* GetProgramHeader(unsigned index) const;
122 
123   // Returns the first program header of |type| in the core dump, or NULL if
124   // no ELF header is found in |content_| or no program header of |type| is
125   // found.
126   const Phdr* GetFirstProgramHeaderOfType(Word type) const;
127 
128   // Returns the number of program headers in the core dump, or 0 if no
129   // ELF header is found in |content_|.
130   unsigned GetProgramHeaderCount() const;
131 
132   // Copies |length| bytes of data starting at |virtual_address| in the core
133   // dump to |buffer|. |buffer| should be a valid pointer to a buffer of at
134   // least |length| bytes. Returns true if the data to be copied is found in
135   // the core dump, or false otherwise.
136   bool CopyData(void* buffer, Addr virtual_address, size_t length);
137 
138   // Returns the first note found in the note section of the core dump, or
139   // an empty note if no note is found.
140   Note GetFirstNote() const;
141 
142  private:
143   // Core dump content.
144   MemoryRange content_;
145 };
146 
147 }  // namespace google_breakpad
148 
149 #endif  // COMMON_LINUX_ELF_CORE_DUMP_H_
150