1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef BERBERIS_TINY_LOADER_TINY_LOADER_H_
18 #define BERBERIS_TINY_LOADER_TINY_LOADER_H_
19 
20 #include "berberis/base/macros.h"
21 
22 #include <sys/mman.h>
23 
24 #include <string>
25 
26 #include "berberis/tiny_loader/loaded_elf_file.h"
27 
28 class TinyLoader {
29  public:
30   using mmap64_fn_t =
31       void* (*)(void* addr, size_t length, int prot, int flags, int fd, off64_t offset);
32   using munmap_fn_t = int (*)(void* addr, size_t length);
33 
34   static bool LoadFromFile(const char* path, size_t align, mmap64_fn_t mmap64_fn,
35                            munmap_fn_t munmap_fn, LoadedElfFile* loaded_elf_file,
36                            std::string* error_msg);
37 
LoadFromFile(const char * path,LoadedElfFile * loaded_elf_file,std::string * error_msg)38   static bool LoadFromFile(const char* path, LoadedElfFile* loaded_elf_file,
39                            std::string* error_msg) {
40     return LoadFromFile(path, 0, mmap64, munmap, loaded_elf_file, error_msg);
41   }
42 
43   static bool LoadFromMemory(const char* path, void* address, size_t size,
44                              LoadedElfFile* loaded_elf_file, std::string* error_msg);
45 
46  private:
47   DISALLOW_IMPLICIT_CONSTRUCTORS(TinyLoader);
48 };
49 
50 #endif  // BERBERIS_TINY_LOADER_TINY_LOADER_H_
51