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