xref: /aosp_15_r20/external/google-breakpad/src/common/linux/elf_core_dump.h (revision 9712c20fc9bbfbac4935993a2ca0b3958c5adad2)
1 // Copyright 2011 Google LLC
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 //     * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //     * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 //     * Neither the name of Google LLC nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 // elf_core_dump.h: Define the google_breakpad::ElfCoreDump class, which
30 // encapsulates an ELF core dump file mapped into memory.
31 
32 #ifndef COMMON_LINUX_ELF_CORE_DUMP_H_
33 #define COMMON_LINUX_ELF_CORE_DUMP_H_
34 
35 #include <elf.h>
36 #include <limits.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 native word size.
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 ULONG_MAX == 0xffffffff
55   static const int kClass = ELFCLASS32;
56 #elif ULONG_MAX == 0xffffffffffffffff
57   static const int kClass = ELFCLASS64;
58 #else
59 #error "Unsupported word size 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   ~ElfCoreDump();
109 
110   // Sets the core dump content to |content|.
111   void SetContent(const MemoryRange& content);
112 
113   // Returns true if a valid ELF header in the core dump, or false otherwise.
114   bool IsValid() const;
115 
116   // Returns the ELF header in the core dump, or NULL if no ELF header
117   // is found in |content_|.
118   const Ehdr* GetHeader() const;
119 
120   // Returns the |index|-th program header in the core dump, or NULL if no
121   // ELF header is found in |content_| or |index| is out of bounds.
122   const Phdr* GetProgramHeader(unsigned index) const;
123 
124   // Returns the first program header of |type| in the core dump, or NULL if
125   // no ELF header is found in |content_| or no program header of |type| is
126   // found.
127   const Phdr* GetFirstProgramHeaderOfType(Word type) const;
128 
129   // Returns the number of program headers in the core dump, or 0 if no
130   // ELF header is found in |content_|.
131   unsigned GetProgramHeaderCount() const;
132 
133   // Copies |length| bytes of data starting at |virtual_address| in the core
134   // dump to |buffer|. |buffer| should be a valid pointer to a buffer of at
135   // least |length| bytes. Returns true if the data to be copied is found in
136   // the core dump, or false otherwise.
137   bool CopyData(void* buffer, Addr virtual_address, size_t length);
138 
139   // Returns the first note found in the note section of the core dump, or
140   // an empty note if no note is found.
141   Note GetFirstNote() const;
142 
143   // Sets the mem fd.
144   void SetProcMem(const int fd);
145 
146  private:
147   // Core dump content.
148   MemoryRange content_;
149 
150   // Descriptor for /proc/<pid>/mem.
151   int proc_mem_fd_;
152 };
153 
154 }  // namespace google_breakpad
155 
156 #endif  // COMMON_LINUX_ELF_CORE_DUMP_H_
157