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#include <elf/elf.h>
82#include <elf/elf64.h>
83#include <inttypes.h>
84#include <string.h>
85
86/* ELF header functions */
87int elf64_checkFile(elf_t *elf)
88{
89    if (sizeof(uintptr_t) != sizeof(uint64_t)) {
90        return -1; /* not supported on 32-bit architecture */
91    }
92
93    if (elf->elfSize < sizeof(Elf64_Ehdr)) {
94        return -1; /* file smaller than ELF header */
95    }
96
97    if (elf_check_magic(elf->elfFile) < 0) {
98        return -1; /* not an ELF file */
99    }
100
101    Elf64_Ehdr *header = elf->elfFile;
102    if (header->e_ident[EI_CLASS] != ELFCLASS64) {
103        return -1; /* not a 64-bit ELF */
104    }
105
106    if (header->e_phentsize != sizeof(Elf64_Phdr)) {
107        return -1; /* unexpected program header size */
108    }
109
110    if (header->e_shentsize != sizeof(Elf64_Shdr)) {
111        return -1; /* unexpected section header size */
112    }
113
114    if (header->e_shstrndx >= header->e_shnum) {
115        return -1; /* invalid section header string table section */
116    }
117
118    elf->elfClass = header->e_ident[EI_CLASS];
119    return 0; /* elf header looks OK */
120}
121
122int elf64_checkProgramHeaderTable(elf_t *elf)
123{
124    Elf64_Ehdr *header = elf->elfFile;
125    size_t ph_end = header->e_phoff + header->e_phentsize * header->e_phnum;
126    if (elf->elfSize < ph_end || ph_end < header->e_phoff) {
127        return -1; /* invalid program header table */
128    }
129
130    return 0;
131}
132
133int elf64_checkSectionTable(elf_t *elf)
134{
135    Elf64_Ehdr *header = elf->elfFile;
136    size_t sh_end = header->e_shoff + header->e_shentsize * header->e_shnum;
137    if (elf->elfSize < sh_end || sh_end < header->e_shoff) {
138        return -1; /* invalid section header table */
139    }
140
141    return 0;
142}
143