1 | /*- |
---|
2 | * Copyright (c) 2006,2008 Joseph Koshy |
---|
3 | * All rights reserved. |
---|
4 | * |
---|
5 | * Redistribution and use in source and binary forms, with or without |
---|
6 | * modification, are permitted provided that the following conditions |
---|
7 | * are met: |
---|
8 | * 1. Redistributions of source code must retain the above copyright |
---|
9 | * notice, this list of conditions and the following disclaimer. |
---|
10 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
11 | * notice, this list of conditions and the following disclaimer in the |
---|
12 | * documentation and/or other materials provided with the distribution. |
---|
13 | * |
---|
14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
24 | * SUCH DAMAGE. |
---|
25 | */ |
---|
26 | |
---|
27 | #include <sys/cdefs.h> |
---|
28 | |
---|
29 | #include <assert.h> |
---|
30 | #include <gelf.h> |
---|
31 | #include <libelf.h> |
---|
32 | #include <stdlib.h> |
---|
33 | |
---|
34 | #include "_libelf.h" |
---|
35 | |
---|
36 | LIBELF_VCSID("$Id: libelf_phdr.c 1677 2011-07-28 04:35:53Z jkoshy $"); |
---|
37 | |
---|
38 | void * |
---|
39 | _libelf_getphdr(Elf *e, int ec) |
---|
40 | { |
---|
41 | size_t phnum, phentsize; |
---|
42 | size_t fsz, msz; |
---|
43 | uint64_t phoff; |
---|
44 | Elf32_Ehdr *eh32; |
---|
45 | Elf64_Ehdr *eh64; |
---|
46 | void *ehdr, *phdr; |
---|
47 | int (*xlator)(char *_d, size_t _dsz, char *_s, size_t _c, int _swap); |
---|
48 | |
---|
49 | assert(ec == ELFCLASS32 || ec == ELFCLASS64); |
---|
50 | |
---|
51 | if (e == NULL) { |
---|
52 | LIBELF_SET_ERROR(ARGUMENT, 0); |
---|
53 | return (NULL); |
---|
54 | } |
---|
55 | |
---|
56 | if ((phdr = (ec == ELFCLASS32 ? |
---|
57 | (void *) e->e_u.e_elf.e_phdr.e_phdr32 : |
---|
58 | (void *) e->e_u.e_elf.e_phdr.e_phdr64)) != NULL) |
---|
59 | return (phdr); |
---|
60 | |
---|
61 | /* |
---|
62 | * Check the PHDR related fields in the EHDR for sanity. |
---|
63 | */ |
---|
64 | |
---|
65 | if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL) |
---|
66 | return (NULL); |
---|
67 | |
---|
68 | phnum = e->e_u.e_elf.e_nphdr; |
---|
69 | |
---|
70 | if (ec == ELFCLASS32) { |
---|
71 | eh32 = (Elf32_Ehdr *) ehdr; |
---|
72 | phentsize = eh32->e_phentsize; |
---|
73 | phoff = (uint64_t) eh32->e_phoff; |
---|
74 | } else { |
---|
75 | eh64 = (Elf64_Ehdr *) ehdr; |
---|
76 | phentsize = eh64->e_phentsize; |
---|
77 | phoff = (uint64_t) eh64->e_phoff; |
---|
78 | } |
---|
79 | |
---|
80 | fsz = gelf_fsize(e, ELF_T_PHDR, phnum, e->e_version); |
---|
81 | |
---|
82 | assert(fsz > 0); |
---|
83 | |
---|
84 | if ((uint64_t) e->e_rawsize < (phoff + fsz)) { |
---|
85 | LIBELF_SET_ERROR(HEADER, 0); |
---|
86 | return (NULL); |
---|
87 | } |
---|
88 | |
---|
89 | msz = _libelf_msize(ELF_T_PHDR, ec, EV_CURRENT); |
---|
90 | |
---|
91 | assert(msz > 0); |
---|
92 | |
---|
93 | if ((phdr = calloc(phnum, msz)) == NULL) { |
---|
94 | LIBELF_SET_ERROR(RESOURCE, 0); |
---|
95 | return (NULL); |
---|
96 | } |
---|
97 | |
---|
98 | if (ec == ELFCLASS32) |
---|
99 | e->e_u.e_elf.e_phdr.e_phdr32 = phdr; |
---|
100 | else |
---|
101 | e->e_u.e_elf.e_phdr.e_phdr64 = phdr; |
---|
102 | |
---|
103 | |
---|
104 | xlator = _libelf_get_translator(ELF_T_PHDR, ELF_TOMEMORY, ec); |
---|
105 | (*xlator)(phdr, phnum * msz, e->e_rawfile + phoff, phnum, |
---|
106 | e->e_byteorder != LIBELF_PRIVATE(byteorder)); |
---|
107 | |
---|
108 | return (phdr); |
---|
109 | } |
---|
110 | |
---|
111 | void * |
---|
112 | _libelf_newphdr(Elf *e, int ec, size_t count) |
---|
113 | { |
---|
114 | void *ehdr, *newphdr, *oldphdr; |
---|
115 | size_t msz; |
---|
116 | |
---|
117 | if (e == NULL) { |
---|
118 | LIBELF_SET_ERROR(ARGUMENT, 0); |
---|
119 | return (NULL); |
---|
120 | } |
---|
121 | |
---|
122 | if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL) { |
---|
123 | LIBELF_SET_ERROR(SEQUENCE, 0); |
---|
124 | return (NULL); |
---|
125 | } |
---|
126 | |
---|
127 | assert(e->e_class == ec); |
---|
128 | assert(ec == ELFCLASS32 || ec == ELFCLASS64); |
---|
129 | assert(e->e_version == EV_CURRENT); |
---|
130 | |
---|
131 | msz = _libelf_msize(ELF_T_PHDR, ec, e->e_version); |
---|
132 | |
---|
133 | assert(msz > 0); |
---|
134 | |
---|
135 | newphdr = NULL; |
---|
136 | if (count > 0 && (newphdr = calloc(count, msz)) == NULL) { |
---|
137 | LIBELF_SET_ERROR(RESOURCE, 0); |
---|
138 | return (NULL); |
---|
139 | } |
---|
140 | |
---|
141 | if (ec == ELFCLASS32) { |
---|
142 | if ((oldphdr = (void *) e->e_u.e_elf.e_phdr.e_phdr32) != NULL) |
---|
143 | free(oldphdr); |
---|
144 | e->e_u.e_elf.e_phdr.e_phdr32 = (Elf32_Phdr *) newphdr; |
---|
145 | } else { |
---|
146 | if ((oldphdr = (void *) e->e_u.e_elf.e_phdr.e_phdr64) != NULL) |
---|
147 | free(oldphdr); |
---|
148 | e->e_u.e_elf.e_phdr.e_phdr64 = (Elf64_Phdr *) newphdr; |
---|
149 | } |
---|
150 | |
---|
151 | e->e_u.e_elf.e_nphdr = count; |
---|
152 | |
---|
153 | elf_flagphdr(e, ELF_C_SET, ELF_F_DIRTY); |
---|
154 | |
---|
155 | return (newphdr); |
---|
156 | } |
---|