1 // Copyright (c) 2012, 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 #include "common/linux/elfutils.h"
31 
32 #include <assert.h>
33 #include <string.h>
34 
35 #include "common/linux/linux_libc_support.h"
36 #include "common/linux/elfutils-inl.h"
37 
38 #if defined(__FreeBSD__)
39 #  define ElfW(type) Elf_##type
40 #endif
41 
42 namespace google_breakpad {
43 
44 namespace {
45 
46 template<typename ElfClass>
FindElfClassSection(const char * elf_base,const char * section_name,typename ElfClass::Word section_type,const void ** section_start,size_t * section_size)47 void FindElfClassSection(const char *elf_base,
48                          const char *section_name,
49                          typename ElfClass::Word section_type,
50                          const void **section_start,
51                          size_t *section_size) {
52   typedef typename ElfClass::Ehdr Ehdr;
53   typedef typename ElfClass::Shdr Shdr;
54 
55   assert(elf_base);
56   assert(section_start);
57   assert(section_size);
58 
59   assert(my_strncmp(elf_base, ELFMAG, SELFMAG) == 0);
60 
61   const Ehdr* elf_header = reinterpret_cast<const Ehdr*>(elf_base);
62   assert(elf_header->e_ident[EI_CLASS] == ElfClass::kClass);
63 
64   const Shdr* sections =
65     GetOffset<ElfClass, Shdr>(elf_header, elf_header->e_shoff);
66   const Shdr* section_names = sections + elf_header->e_shstrndx;
67   const char* names =
68     GetOffset<ElfClass, char>(elf_header, section_names->sh_offset);
69   const char *names_end = names + section_names->sh_size;
70 
71   const Shdr* section =
72     FindElfSectionByName<ElfClass>(section_name, section_type,
73                                    sections, names, names_end,
74                                    elf_header->e_shnum);
75 
76   if (section != NULL && section->sh_size > 0) {
77     *section_start = elf_base + section->sh_offset;
78     *section_size = section->sh_size;
79   }
80 }
81 
82 template<typename ElfClass>
FindElfClassSegment(const char * elf_base,typename ElfClass::Word segment_type,wasteful_vector<ElfSegment> * segments)83 void FindElfClassSegment(const char *elf_base,
84                          typename ElfClass::Word segment_type,
85                          wasteful_vector<ElfSegment> *segments) {
86   typedef typename ElfClass::Ehdr Ehdr;
87   typedef typename ElfClass::Phdr Phdr;
88 
89   assert(elf_base);
90   assert(segments);
91 
92   assert(my_strncmp(elf_base, ELFMAG, SELFMAG) == 0);
93 
94   const Ehdr* elf_header = reinterpret_cast<const Ehdr*>(elf_base);
95   assert(elf_header->e_ident[EI_CLASS] == ElfClass::kClass);
96 
97   const Phdr* phdrs =
98     GetOffset<ElfClass, Phdr>(elf_header, elf_header->e_phoff);
99 
100   for (int i = 0; i < elf_header->e_phnum; ++i) {
101     if (phdrs[i].p_type == segment_type) {
102       ElfSegment seg = {};
103       seg.start = elf_base + phdrs[i].p_offset;
104       seg.size = phdrs[i].p_filesz;
105       segments->push_back(seg);
106     }
107   }
108 }
109 
110 }  // namespace
111 
IsValidElf(const void * elf_base)112 bool IsValidElf(const void* elf_base) {
113   return my_strncmp(reinterpret_cast<const char*>(elf_base),
114                     ELFMAG, SELFMAG) == 0;
115 }
116 
ElfClass(const void * elf_base)117 int ElfClass(const void* elf_base) {
118   const ElfW(Ehdr)* elf_header =
119     reinterpret_cast<const ElfW(Ehdr)*>(elf_base);
120 
121   return elf_header->e_ident[EI_CLASS];
122 }
123 
FindElfSection(const void * elf_mapped_base,const char * section_name,uint32_t section_type,const void ** section_start,size_t * section_size)124 bool FindElfSection(const void *elf_mapped_base,
125                     const char *section_name,
126                     uint32_t section_type,
127                     const void **section_start,
128                     size_t *section_size) {
129   assert(elf_mapped_base);
130   assert(section_start);
131   assert(section_size);
132 
133   *section_start = NULL;
134   *section_size = 0;
135 
136   if (!IsValidElf(elf_mapped_base))
137     return false;
138 
139   int cls = ElfClass(elf_mapped_base);
140   const char* elf_base =
141     static_cast<const char*>(elf_mapped_base);
142 
143   if (cls == ELFCLASS32) {
144     FindElfClassSection<ElfClass32>(elf_base, section_name, section_type,
145                                     section_start, section_size);
146     return *section_start != NULL;
147   } else if (cls == ELFCLASS64) {
148     FindElfClassSection<ElfClass64>(elf_base, section_name, section_type,
149                                     section_start, section_size);
150     return *section_start != NULL;
151   }
152 
153   return false;
154 }
155 
FindElfSegments(const void * elf_mapped_base,uint32_t segment_type,wasteful_vector<ElfSegment> * segments)156 bool FindElfSegments(const void* elf_mapped_base,
157                      uint32_t segment_type,
158                      wasteful_vector<ElfSegment>* segments) {
159   assert(elf_mapped_base);
160   assert(segments);
161 
162   if (!IsValidElf(elf_mapped_base))
163     return false;
164 
165   int cls = ElfClass(elf_mapped_base);
166   const char* elf_base =
167     static_cast<const char*>(elf_mapped_base);
168 
169   if (cls == ELFCLASS32) {
170     FindElfClassSegment<ElfClass32>(elf_base, segment_type, segments);
171     return true;
172   } else if (cls == ELFCLASS64) {
173     FindElfClassSegment<ElfClass64>(elf_base, segment_type, segments);
174     return true;
175   }
176 
177   return false;
178 }
179 
180 template <typename ElfClass>
FindElfSoNameFromDynamicSection(const void * section_start,size_t section_size,const void * dynstr_start,size_t dynstr_size,char * soname,size_t soname_size)181 bool FindElfSoNameFromDynamicSection(const void* section_start,
182                                      size_t section_size,
183                                      const void* dynstr_start,
184                                      size_t dynstr_size,
185                                      char* soname,
186                                      size_t soname_size) {
187   typedef typename ElfClass::Dyn Dyn;
188 
189   auto* dynamic = static_cast<const Dyn*>(section_start);
190   size_t dcount = section_size / sizeof(Dyn);
191   for (const Dyn* dyn = dynamic; dyn < dynamic + dcount; ++dyn) {
192     if (dyn->d_tag == DT_SONAME) {
193       const char* dynstr = static_cast<const char*>(dynstr_start);
194       if (dyn->d_un.d_val >= dynstr_size) {
195         // Beyond the end of the dynstr section
196         return false;
197       }
198       const char* str = dynstr + dyn->d_un.d_val;
199       const size_t maxsize = dynstr_size - dyn->d_un.d_val;
200       my_strlcpy(soname, str, maxsize < soname_size ? maxsize : soname_size);
201       return true;
202     }
203   }
204 
205   return false;
206 }
207 
ElfFileSoNameFromMappedFile(const void * elf_base,char * soname,size_t soname_size)208 bool ElfFileSoNameFromMappedFile(const void* elf_base,
209                                  char* soname,
210                                  size_t soname_size) {
211   if (!IsValidElf(elf_base)) {
212     // Not ELF
213     return false;
214   }
215 
216   const void* segment_start;
217   size_t segment_size;
218   if (!FindElfSection(elf_base, ".dynamic", SHT_DYNAMIC, &segment_start,
219                       &segment_size)) {
220     // No dynamic section
221     return false;
222   }
223 
224   const void* dynstr_start;
225   size_t dynstr_size;
226   if (!FindElfSection(elf_base, ".dynstr", SHT_STRTAB, &dynstr_start,
227                       &dynstr_size)) {
228     // No dynstr section
229     return false;
230   }
231 
232   int cls = ElfClass(elf_base);
233   return cls == ELFCLASS32 ? FindElfSoNameFromDynamicSection<ElfClass32>(
234                                  segment_start, segment_size, dynstr_start,
235                                  dynstr_size, soname, soname_size)
236                            : FindElfSoNameFromDynamicSection<ElfClass64>(
237                                  segment_start, segment_size, dynstr_start,
238                                  dynstr_size, soname, soname_size);
239 }
240 
241 }  // namespace google_breakpad
242