1/* @LICENSE(UNSW_OZPLB) */
2
3/*
4 * Australian Public Licence B (OZPLB)
5 *
6 * Version 1-0
7 *
8 * Copyright (c) 2004 University of New South Wales
9 *
10 * All rights reserved.
11 *
12 * Developed by: Operating Systems and Distributed Systems Group (DiSy)
13 *               University of New South Wales
14 *               http://www.disy.cse.unsw.edu.au
15 *
16 * Permission is granted by University of New South Wales, free of charge, to
17 * any person obtaining a copy of this software and any associated
18 * documentation files (the "Software") to deal with the Software without
19 * restriction, including (without limitation) the rights to use, copy,
20 * modify, adapt, merge, publish, distribute, communicate to the public,
21 * sublicense, and/or sell, lend or rent out copies of the Software, and
22 * to permit persons to whom the Software is furnished to do so, subject
23 * to the following conditions:
24 *
25 *     * Redistributions of source code must retain the above copyright
26 *       notice, this list of conditions and the following disclaimers.
27 *
28 *     * Redistributions in binary form must reproduce the above
29 *       copyright notice, this list of conditions and the following
30 *       disclaimers in the documentation and/or other materials provided
31 *       with the distribution.
32 *
33 *     * Neither the name of University of New South Wales, nor the names of its
34 *       contributors, may be used to endorse or promote products derived
35 *       from this Software without specific prior written permission.
36 *
37 * EXCEPT AS EXPRESSLY STATED IN THIS LICENCE AND TO THE FULL EXTENT
38 * PERMITTED BY APPLICABLE LAW, THE SOFTWARE IS PROVIDED "AS-IS", AND
39 * NATIONAL ICT AUSTRALIA AND ITS CONTRIBUTORS MAKE NO REPRESENTATIONS,
40 * WARRANTIES OR CONDITIONS OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
41 * BUT NOT LIMITED TO ANY REPRESENTATIONS, WARRANTIES OR CONDITIONS
42 * REGARDING THE CONTENTS OR ACCURACY OF THE SOFTWARE, OR OF TITLE,
43 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT,
44 * THE ABSENCE OF LATENT OR OTHER DEFECTS, OR THE PRESENCE OR ABSENCE OF
45 * ERRORS, WHETHER OR NOT DISCOVERABLE.
46 *
47 * TO THE FULL EXTENT PERMITTED BY APPLICABLE LAW, IN NO EVENT SHALL
48 * NATIONAL ICT AUSTRALIA OR ITS CONTRIBUTORS BE LIABLE ON ANY LEGAL
49 * THEORY (INCLUDING, WITHOUT LIMITATION, IN AN ACTION OF CONTRACT,
50 * NEGLIGENCE OR OTHERWISE) FOR ANY CLAIM, LOSS, DAMAGES OR OTHER
51 * LIABILITY, INCLUDING (WITHOUT LIMITATION) LOSS OF PRODUCTION OR
52 * OPERATION TIME, LOSS, DAMAGE OR CORRUPTION OF DATA OR RECORDS; OR LOSS
53 * OF ANTICIPATED SAVINGS, OPPORTUNITY, REVENUE, PROFIT OR GOODWILL, OR
54 * OTHER ECONOMIC LOSS; OR ANY SPECIAL, INCIDENTAL, INDIRECT,
55 * CONSEQUENTIAL, PUNITIVE OR EXEMPLARY DAMAGES, ARISING OUT OF OR IN
56 * CONNECTION WITH THIS LICENCE, THE SOFTWARE OR THE USE OF OR OTHER
57 * DEALINGS WITH THE SOFTWARE, EVEN IF NATIONAL ICT AUSTRALIA OR ITS
58 * CONTRIBUTORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH CLAIM, LOSS,
59 * DAMAGES OR OTHER LIABILITY.
60 *
61 * If applicable legislation implies representations, warranties, or
62 * conditions, or imposes obligations or liability on University of New South
63 * Wales or one of its contributors in respect of the Software that
64 * cannot be wholly or partly excluded, restricted or modified, the
65 * liability of University of New South Wales or the contributor is limited, to
66 * the full extent permitted by the applicable legislation, at its
67 * option, to:
68 * a.  in the case of goods, any one or more of the following:
69 * i.  the replacement of the goods or the supply of equivalent goods;
70 * ii.  the repair of the goods;
71 * iii. the payment of the cost of replacing the goods or of acquiring
72 *  equivalent goods;
73 * iv.  the payment of the cost of having the goods repaired; or
74 * b.  in the case of services:
75 * i.  the supplying of the services again; or
76 * ii.  the payment of the cost of having the services supplied again.
77 *
78 * The construction, validity and performance of this licence is governed
79 * by the laws in force in New South Wales, Australia.
80 */
81#pragma once
82
83#include <stdint.h>
84#include <elf/elf.h>
85
86/* ELF header functions */
87int elf64_checkFile(elf_t *elf);
88
89int elf64_checkProgramHeaderTable(elf_t *elf);
90
91int elf64_checkSectionTable(elf_t *elf);
92
93static inline bool elf_isElf64(elf_t *elf)
94{
95    return elf->elfClass == ELFCLASS64;
96}
97
98static inline Elf64_Ehdr elf64_getHeader(elf_t *elf)
99{
100    return *(Elf64_Ehdr *) elf->elfFile;
101}
102
103static inline uintptr_t elf64_getEntryPoint(elf_t *file)
104{
105    return elf64_getHeader(file).e_entry;
106}
107
108static inline Elf64_Phdr *elf64_getProgramHeaderTable(elf_t *file)
109{
110    return file->elfFile + elf64_getHeader(file).e_phoff;
111}
112
113static inline Elf64_Shdr *elf64_getSectionTable(elf_t *file)
114{
115    return file->elfFile + elf64_getHeader(file).e_shoff;
116}
117
118static inline size_t elf64_getNumProgramHeaders(elf_t *file)
119{
120    return elf64_getHeader(file).e_phnum;
121}
122
123static inline size_t elf64_getNumSections(elf_t *elf)
124{
125    return elf64_getHeader(elf).e_shnum;
126}
127
128static inline size_t elf64_getSectionStringTableIndex(elf_t *elf)
129{
130    return elf64_getHeader(elf).e_shstrndx;
131}
132
133
134/* Section header functions */
135static inline size_t elf64_getSectionNameOffset(elf_t *elf, size_t s)
136{
137    return elf64_getSectionTable(elf)[s].sh_name;
138}
139
140static inline uint32_t elf64_getSectionType(elf_t *file, size_t s)
141{
142    return elf64_getSectionTable(file)[s].sh_type;
143}
144
145static inline size_t elf64_getSectionFlags(elf_t *file, size_t s)
146{
147    return elf64_getSectionTable(file)[s].sh_flags;
148}
149
150static inline uintptr_t elf64_getSectionAddr(elf_t *elf, size_t i)
151{
152    return elf64_getSectionTable(elf)[i].sh_addr;
153}
154
155static inline size_t elf64_getSectionOffset(elf_t *elf, size_t i)
156{
157    return elf64_getSectionTable(elf)[i].sh_offset;
158}
159
160static inline size_t elf64_getSectionSize(elf_t *elf, size_t i)
161{
162    return elf64_getSectionTable(elf)[i].sh_size;
163}
164
165static inline uint32_t elf64_getSectionLink(elf_t *elf, size_t i)
166{
167    return elf64_getSectionTable(elf)[i].sh_link;
168}
169
170static inline uint32_t elf64_getSectionInfo(elf_t *elf, size_t i)
171{
172    return elf64_getSectionTable(elf)[i].sh_info;
173}
174
175static inline size_t elf64_getSectionAddrAlign(elf_t *elf, size_t i)
176{
177    return elf64_getSectionTable(elf)[i].sh_addralign;
178}
179
180static inline size_t elf64_getSectionEntrySize(elf_t *elf, size_t i)
181{
182    return elf64_getSectionTable(elf)[i].sh_entsize;
183}
184
185
186/* Program header functions */
187static inline uint32_t elf64_getProgramHeaderType(elf_t *file, size_t ph)
188{
189    return elf64_getProgramHeaderTable(file)[ph].p_type;
190}
191
192static inline size_t elf64_getProgramHeaderOffset(elf_t *file, size_t ph)
193{
194    return elf64_getProgramHeaderTable(file)[ph].p_offset;
195}
196
197static inline uintptr_t elf64_getProgramHeaderVaddr(elf_t *file, size_t ph)
198{
199    return elf64_getProgramHeaderTable(file)[ph].p_vaddr;
200}
201
202static inline uintptr_t elf64_getProgramHeaderPaddr(elf_t *file, size_t ph)
203{
204    return elf64_getProgramHeaderTable(file)[ph].p_paddr;
205}
206
207static inline size_t elf64_getProgramHeaderFileSize(elf_t *file, size_t ph)
208{
209    return elf64_getProgramHeaderTable(file)[ph].p_filesz;
210}
211
212static inline size_t elf64_getProgramHeaderMemorySize(elf_t *file, size_t ph)
213{
214    return elf64_getProgramHeaderTable(file)[ph].p_memsz;
215}
216
217static inline uint32_t elf64_getProgramHeaderFlags(elf_t *file, size_t ph)
218{
219    return elf64_getProgramHeaderTable(file)[ph].p_flags;
220}
221
222static inline size_t elf64_getProgramHeaderAlign(elf_t *file, size_t ph)
223{
224    return elf64_getProgramHeaderTable(file)[ph].p_align;
225}
226
227