1 /* Copyright (C) 2007-2010 The Android Open Source Project 2 ** 3 ** This software is licensed under the terms of the GNU General Public 4 ** License version 2, as published by the Free Software Foundation, and 5 ** may be copied, distributed, and modified under those terms. 6 ** 7 ** This program is distributed in the hope that it will be useful, 8 ** but WITHOUT ANY WARRANTY; without even the implied warranty of 9 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 ** GNU General Public License for more details. 11 */ 12 13 /* 14 * Contains declaration of a class ElfMappedSection, that encapsulates 15 * a section of an ELF file, mapped to memory. 16 */ 17 18 #ifndef ELFF_ELF_MAPPED_SECTION_H_ 19 #define ELFF_ELF_MAPPED_SECTION_H_ 20 21 #include "elf_defs.h" 22 #include "mapfile.h" 23 24 /* Encapsulates a section of an ELF file, mapped to memory. */ 25 class ElfMappedSection { 26 public: 27 /* Constructs ElfMappedSection instance. */ 28 ElfMappedSection(); 29 30 /* Destructs ElfMappedSection instance. */ 31 ~ElfMappedSection(); 32 33 /* Maps ELF file section to memory. 34 * Param: 35 * handle - Handle to an opened ELF file. 36 * offset - Offset of the beginning of the section data in ELF file. 37 * NOTE: we explicitly use 64-bit type for this parameter, since we may 38 * still allow 32-bit library to process 64 bits ELF/DWARF formats. We 39 * really only care about section size being small enough to fit in 32 40 * bits value in this case (which seems to be always true for ELF files, 41 * as section size is encoded with 32-bit value even in 64-bit ELF file). 42 * size - Section byte size in ELF file. 43 * Return: 44 * true on success, or false on failure, with errno providing extended 45 * error information. 46 * NOTE: if section has already been mapped, this method immediately 47 * returns with success. 48 */ 49 bool map(MapFile* handle, Elf_Xword offset, Elf_Word size); 50 51 /* Checks if section has been mapped. */ is_mapped()52 bool is_mapped() const { 53 return mapped_at_ != NULL; 54 } 55 56 /* Gets address of the beginning of the mapped section. */ data()57 const void* data() const { 58 assert(is_mapped()); 59 return data_; 60 } 61 62 /* Gets section size. */ size()63 Elf_Word size() const { 64 assert(is_mapped()); 65 return size_; 66 } 67 68 /* Checks if an address range is fully contained in this section. */ is_contained(const void * ptr,size_t rsize)69 bool is_contained(const void* ptr, size_t rsize) const { 70 assert(is_mapped()); 71 return is_mapped() && is_in_section(ptr, rsize, data(), size()); 72 } 73 74 protected: 75 /* Beginning of the memory mapping, containing the section. 76 * NOTE: due to page alignment requirements of the mapping API, mapping 77 * address may differ from the address where the actual section data 78 * starts inside that mapping. 79 */ 80 void* mapped_at_; 81 82 /* Address of the beginning of the mapped section. */ 83 const void* data_; 84 85 /* Section size. */ 86 Elf_Word size_; 87 }; 88 89 #endif // ELFF_ELF_MAPPED_SECTION_H_ 90