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 implementation of a class ElfMappedSection, that encapsulates
15 * a section of an ELF file, mapped to memory.
16 */
17
18#include "elf_defs.h"
19#include "elf_mapped_section.h"
20
21ElfMappedSection::ElfMappedSection()
22    : mapped_at_(NULL),
23      data_(NULL),
24      size_(0) {
25}
26
27ElfMappedSection::~ElfMappedSection() {
28  if (mapped_at_ != NULL) {
29    mapfile_unmap(mapped_at_, diff_ptr(mapped_at_, data_) + size_);
30  }
31}
32
33bool ElfMappedSection::map(MapFile* handle,
34                           Elf_Xword offset,
35                           Elf_Word size) {
36    void* section_ptr;
37    size_t mapped_bytes;
38    mapped_at_ = mapfile_map(handle, offset, size, PROT_READ,
39                             &section_ptr, &mapped_bytes);
40    if (mapped_at_ == NULL) {
41        return false;
42    }
43
44    data_ = section_ptr;
45    size_ = (Elf_Word)mapped_bytes;
46
47    return true;
48}
49