LCOV - code coverage report
Current view: top level - tools/profiler/lul - LulElfInt.h (source / functions) Hit Total Coverage
Test: output.info Lines: 0 17 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 8 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
       2             : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
       3             : 
       4             : // Copyright (c) 2006, 2012, Google Inc.
       5             : // All rights reserved.
       6             : //
       7             : // Redistribution and use in source and binary forms, with or without
       8             : // modification, are permitted provided that the following conditions are
       9             : // met:
      10             : //
      11             : //     * Redistributions of source code must retain the above copyright
      12             : // notice, this list of conditions and the following disclaimer.
      13             : //     * Redistributions in binary form must reproduce the above
      14             : // copyright notice, this list of conditions and the following disclaimer
      15             : // in the documentation and/or other materials provided with the
      16             : // distribution.
      17             : //     * Neither the name of Google Inc. nor the names of its
      18             : // contributors may be used to endorse or promote products derived from
      19             : // this software without specific prior written permission.
      20             : //
      21             : // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
      22             : // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
      23             : // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
      24             : // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
      25             : // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
      26             : // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
      27             : // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      28             : // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      29             : // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      30             : // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
      31             : // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      32             : 
      33             : // This file is derived from the following files in
      34             : // toolkit/crashreporter/google-breakpad:
      35             : //   src/common/android/include/elf.h
      36             : //   src/common/linux/elfutils.h
      37             : //   src/common/linux/file_id.h
      38             : //   src/common/linux/elfutils-inl.h
      39             : 
      40             : #ifndef LulElfInt_h
      41             : #define LulElfInt_h
      42             : 
      43             : // This header defines functions etc internal to the ELF reader.  It
      44             : // should not be included outside of LulElf.cpp.
      45             : 
      46             : #include <elf.h>
      47             : #include <stdlib.h>
      48             : 
      49             : #include "mozilla/Assertions.h"
      50             : 
      51             : #include "PlatformMacros.h"
      52             : 
      53             : 
      54             : // (derived from)
      55             : // elfutils.h: Utilities for dealing with ELF files.
      56             : //
      57             : 
      58             : #if defined(GP_OS_android)
      59             : 
      60             : // From toolkit/crashreporter/google-breakpad/src/common/android/include/elf.h
      61             : // The Android headers don't always define this constant.
      62             : #ifndef EM_X86_64
      63             : #define EM_X86_64  62
      64             : #endif
      65             : 
      66             : #ifndef EM_PPC64
      67             : #define EM_PPC64   21
      68             : #endif
      69             : 
      70             : #ifndef EM_S390
      71             : #define EM_S390    22
      72             : #endif
      73             : 
      74             : #ifndef NT_GNU_BUILD_ID
      75             : #define NT_GNU_BUILD_ID 3
      76             : #endif
      77             : 
      78             : #define ElfW(type)      _ElfW (Elf, ELFSIZE, type)
      79             : #define _ElfW(e,w,t)    _ElfW_1 (e, w, _##t)
      80             : #define _ElfW_1(e,w,t)  e##w##t
      81             : 
      82             : //FIXME
      83             : extern "C" {
      84             :   extern char*  basename(const char*  path);
      85             : };
      86             : #else
      87             : 
      88             : # include <link.h>
      89             : #endif
      90             : 
      91             : 
      92             : namespace lul {
      93             : 
      94             : // Traits classes so consumers can write templatized code to deal
      95             : // with specific ELF bits.
      96             : struct ElfClass32 {
      97             :   typedef Elf32_Addr Addr;
      98             :   typedef Elf32_Ehdr Ehdr;
      99             :   typedef Elf32_Nhdr Nhdr;
     100             :   typedef Elf32_Phdr Phdr;
     101             :   typedef Elf32_Shdr Shdr;
     102             :   typedef Elf32_Half Half;
     103             :   typedef Elf32_Off Off;
     104             :   typedef Elf32_Word Word;
     105             :   static const int kClass = ELFCLASS32;
     106             :   static const size_t kAddrSize = sizeof(Elf32_Addr);
     107             : };
     108             : 
     109             : struct ElfClass64 {
     110             :   typedef Elf64_Addr Addr;
     111             :   typedef Elf64_Ehdr Ehdr;
     112             :   typedef Elf64_Nhdr Nhdr;
     113             :   typedef Elf64_Phdr Phdr;
     114             :   typedef Elf64_Shdr Shdr;
     115             :   typedef Elf64_Half Half;
     116             :   typedef Elf64_Off Off;
     117             :   typedef Elf64_Word Word;
     118             :   static const int kClass = ELFCLASS64;
     119             :   static const size_t kAddrSize = sizeof(Elf64_Addr);
     120             : };
     121             : 
     122             : bool IsValidElf(const void* elf_header);
     123             : int ElfClass(const void* elf_base);
     124             : 
     125             : // Attempt to find a section named |section_name| of type |section_type|
     126             : // in the ELF binary data at |elf_mapped_base|. On success, returns true
     127             : // and sets |*section_start| to point to the start of the section data,
     128             : // and |*section_size| to the size of the section's data. If |elfclass|
     129             : // is not NULL, set |*elfclass| to the ELF file class.
     130             : bool FindElfSection(const void *elf_mapped_base,
     131             :                     const char *section_name,
     132             :                     uint32_t section_type,
     133             :                     const void **section_start,
     134             :                     int *section_size,
     135             :                     int *elfclass);
     136             : 
     137             : // Internal helper method, exposed for convenience for callers
     138             : // that already have more info.
     139             : template<typename ElfClass>
     140             : const typename ElfClass::Shdr*
     141             : FindElfSectionByName(const char* name,
     142             :                      typename ElfClass::Word section_type,
     143             :                      const typename ElfClass::Shdr* sections,
     144             :                      const char* section_names,
     145             :                      const char* names_end,
     146             :                      int nsection);
     147             : 
     148             : // Attempt to find the first segment of type |segment_type| in the ELF
     149             : // binary data at |elf_mapped_base|. On success, returns true and sets
     150             : // |*segment_start| to point to the start of the segment data, and
     151             : // and |*segment_size| to the size of the segment's data. If |elfclass|
     152             : // is not NULL, set |*elfclass| to the ELF file class.
     153             : bool FindElfSegment(const void *elf_mapped_base,
     154             :                     uint32_t segment_type,
     155             :                     const void **segment_start,
     156             :                     int *segment_size,
     157             :                     int *elfclass);
     158             : 
     159             : // Convert an offset from an Elf header into a pointer to the mapped
     160             : // address in the current process. Takes an extra template parameter
     161             : // to specify the return type to avoid having to dynamic_cast the
     162             : // result.
     163             : template<typename ElfClass, typename T>
     164             : const T*
     165             : GetOffset(const typename ElfClass::Ehdr* elf_header,
     166             :           typename ElfClass::Off offset);
     167             : 
     168             : 
     169             : // (derived from)
     170             : // file_id.h: Return a unique identifier for a file
     171             : //
     172             : 
     173             : static const size_t kMDGUIDSize = sizeof(MDGUID);
     174             : 
     175             : class FileID {
     176             :  public:
     177             : 
     178             :   // Load the identifier for the elf file mapped into memory at |base| into
     179             :   // |identifier|.  Return false if the identifier could not be created for the
     180             :   // file.
     181             :   static bool ElfFileIdentifierFromMappedFile(const void* base,
     182             :                                               uint8_t identifier[kMDGUIDSize]);
     183             : 
     184             :   // Convert the |identifier| data to a NULL terminated string.  The string will
     185             :   // be formatted as a UUID (e.g., 22F065BB-FC9C-49F7-80FE-26A7CEBD7BCE).
     186             :   // The |buffer| should be at least 37 bytes long to receive all of the data
     187             :   // and termination.  Shorter buffers will contain truncated data.
     188             :   static void ConvertIdentifierToString(const uint8_t identifier[kMDGUIDSize],
     189             :                                         char* buffer, int buffer_length);
     190             : };
     191             : 
     192             : 
     193             : 
     194             : template<typename ElfClass, typename T>
     195           0 : const T* GetOffset(const typename ElfClass::Ehdr* elf_header,
     196             :                    typename ElfClass::Off offset) {
     197           0 :   return reinterpret_cast<const T*>(reinterpret_cast<uintptr_t>(elf_header) +
     198           0 :                                     offset);
     199             : }
     200             : 
     201             : template<typename ElfClass>
     202           0 : const typename ElfClass::Shdr* FindElfSectionByName(
     203             :     const char* name,
     204             :     typename ElfClass::Word section_type,
     205             :     const typename ElfClass::Shdr* sections,
     206             :     const char* section_names,
     207             :     const char* names_end,
     208             :     int nsection) {
     209           0 :   MOZ_ASSERT(name != NULL);
     210           0 :   MOZ_ASSERT(sections != NULL);
     211           0 :   MOZ_ASSERT(nsection > 0);
     212             : 
     213           0 :   int name_len = strlen(name);
     214           0 :   if (name_len == 0)
     215           0 :     return NULL;
     216             : 
     217           0 :   for (int i = 0; i < nsection; ++i) {
     218           0 :     const char* section_name = section_names + sections[i].sh_name;
     219           0 :     if (sections[i].sh_type == section_type &&
     220           0 :         names_end - section_name >= name_len + 1 &&
     221           0 :         strcmp(name, section_name) == 0) {
     222           0 :       return sections + i;
     223             :     }
     224             :   }
     225           0 :   return NULL;
     226             : }
     227             : 
     228             : } // namespace lul
     229             : 
     230             : 
     231             : // And finally, the external interface, offered to LulMain.cpp
     232             : #include "LulElfExt.h"
     233             : 
     234             : #endif // LulElfInt_h

Generated by: LCOV version 1.13