1 /*
2  * Copyright (C) 2023 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 #pragma once
18 
19 #include <elf.h>
20 #include <string>
21 #include <vector>
22 
23 using namespace std;
24 
25 namespace android {
26 namespace elf64 {
27 
28 // Section content representation
29 typedef struct {
30     std::vector<char> data;   // Raw content of the data section.
31     uint64_t size;   // Size of the data section.
32     std::string name;      // The name of the section.
33     uint16_t index;  // Index of the section.
34 } Elf64_Sc;
35 
36 // Class to represent an ELF64 binary.
37 //
38 // An ELF binary is formed by 4 parts:
39 //
40 // - Executable header.
41 // - Program headers (present in executables or shared libraries).
42 // - Sections (.interp, .init, .plt, .text, .rodata, .data, .bss, .shstrtab, etc).
43 // - Section headers.
44 //
45 //                ______________________
46 //                |                    |
47 //                | Executable header  |
48 //                |____________________|
49 //                |                    |
50 //                |                    |
51 //                |  Program headers   |
52 //                |                    |
53 //                |____________________|
54 //                |                    |
55 //                |                    |
56 //                |      Sections      |
57 //                |                    |
58 //                |____________________|
59 //                |                    |
60 //                |                    |
61 //                |  Section headers   |
62 //                |                    |
63 //                |____________________|
64 //
65 //
66 // The structs defined in linux for ELF parts can be found in:
67 //
68 //   - /usr/include/elf.h.
69 //   - https://elixir.bootlin.com/linux/v5.14.21/source/include/uapi/linux/elf.h#L222
70 class Elf64Binary {
71   public:
72     Elf64_Ehdr ehdr;
73     std::vector<Elf64_Phdr> phdrs;
74     std::vector<Elf64_Shdr> shdrs;
75     std::vector<Elf64_Sc> sections;
76     std::string path;
77 };
78 
79 }  // namespace elf64
80 }  // namespace android
81