blob: 1a7e8aa2af582f278a95e238800ebb3a2e7b54f9 [file] [log] [blame]
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001/*
2 * elf.c - ELF access library
3 *
4 * Adapted from kpatch (https://github.com/dynup/kpatch):
5 * Copyright (C) 2013-2015 Josh Poimboeuf <jpoimboe@redhat.com>
6 * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include <sys/types.h>
23#include <sys/stat.h>
24#include <fcntl.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <unistd.h>
29
30#include "elf.h"
31#include "warn.h"
32
Arnaldo Carvalho de Melo774bec32016-07-13 15:28:51 -030033/*
34 * Fallback for systems without this "read, mmaping if possible" cmd.
35 */
36#ifndef ELF_C_READ_MMAP
37#define ELF_C_READ_MMAP ELF_C_READ
38#endif
39
Josh Poimboeufbaa41462017-06-28 10:11:07 -050040#define WARN_ELF(format, ...) \
41 WARN(format ": %s", ##__VA_ARGS__, elf_errmsg(-1))
42
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060043struct section *find_section_by_name(struct elf *elf, const char *name)
44{
45 struct section *sec;
46
47 list_for_each_entry(sec, &elf->sections, list)
48 if (!strcmp(sec->name, name))
49 return sec;
50
51 return NULL;
52}
53
54static struct section *find_section_by_index(struct elf *elf,
55 unsigned int idx)
56{
57 struct section *sec;
58
59 list_for_each_entry(sec, &elf->sections, list)
60 if (sec->idx == idx)
61 return sec;
62
63 return NULL;
64}
65
66static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx)
67{
68 struct section *sec;
69 struct symbol *sym;
70
71 list_for_each_entry(sec, &elf->sections, list)
Josh Poimboeuf042ba732016-03-09 00:07:00 -060072 hash_for_each_possible(sec->symbol_hash, sym, hash, idx)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060073 if (sym->idx == idx)
74 return sym;
75
76 return NULL;
77}
78
79struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)
80{
81 struct symbol *sym;
82
Josh Poimboeufa196e172016-03-09 00:06:57 -060083 list_for_each_entry(sym, &sec->symbol_list, list)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060084 if (sym->type != STT_SECTION &&
85 sym->offset == offset)
86 return sym;
87
88 return NULL;
89}
90
Josh Poimboeuf5c51f4a2017-03-02 16:57:23 -060091struct symbol *find_symbol_containing(struct section *sec, unsigned long offset)
92{
93 struct symbol *sym;
94
95 list_for_each_entry(sym, &sec->symbol_list, list)
96 if (sym->type != STT_SECTION &&
97 offset >= sym->offset && offset < sym->offset + sym->len)
98 return sym;
99
100 return NULL;
101}
102
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600103struct rela *find_rela_by_dest_range(struct section *sec, unsigned long offset,
104 unsigned int len)
105{
106 struct rela *rela;
Josh Poimboeuf042ba732016-03-09 00:07:00 -0600107 unsigned long o;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600108
109 if (!sec->rela)
110 return NULL;
111
Josh Poimboeuf042ba732016-03-09 00:07:00 -0600112 for (o = offset; o < offset + len; o++)
113 hash_for_each_possible(sec->rela->rela_hash, rela, hash, o)
114 if (rela->offset == o)
115 return rela;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600116
117 return NULL;
118}
119
120struct rela *find_rela_by_dest(struct section *sec, unsigned long offset)
121{
122 return find_rela_by_dest_range(sec, offset, 1);
123}
124
125struct symbol *find_containing_func(struct section *sec, unsigned long offset)
126{
127 struct symbol *func;
128
Josh Poimboeufa196e172016-03-09 00:06:57 -0600129 list_for_each_entry(func, &sec->symbol_list, list)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600130 if (func->type == STT_FUNC && offset >= func->offset &&
131 offset < func->offset + func->len)
132 return func;
133
134 return NULL;
135}
136
137static int read_sections(struct elf *elf)
138{
139 Elf_Scn *s = NULL;
140 struct section *sec;
141 size_t shstrndx, sections_nr;
142 int i;
143
144 if (elf_getshdrnum(elf->elf, &sections_nr)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500145 WARN_ELF("elf_getshdrnum");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600146 return -1;
147 }
148
149 if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500150 WARN_ELF("elf_getshdrstrndx");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600151 return -1;
152 }
153
154 for (i = 0; i < sections_nr; i++) {
155 sec = malloc(sizeof(*sec));
156 if (!sec) {
157 perror("malloc");
158 return -1;
159 }
160 memset(sec, 0, sizeof(*sec));
161
Josh Poimboeufa196e172016-03-09 00:06:57 -0600162 INIT_LIST_HEAD(&sec->symbol_list);
163 INIT_LIST_HEAD(&sec->rela_list);
Josh Poimboeuf042ba732016-03-09 00:07:00 -0600164 hash_init(sec->rela_hash);
165 hash_init(sec->symbol_hash);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600166
167 list_add_tail(&sec->list, &elf->sections);
168
169 s = elf_getscn(elf->elf, i);
170 if (!s) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500171 WARN_ELF("elf_getscn");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600172 return -1;
173 }
174
175 sec->idx = elf_ndxscn(s);
176
177 if (!gelf_getshdr(s, &sec->sh)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500178 WARN_ELF("gelf_getshdr");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600179 return -1;
180 }
181
182 sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
183 if (!sec->name) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500184 WARN_ELF("elf_strptr");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600185 return -1;
186 }
187
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500188 sec->data = elf_getdata(s, NULL);
189 if (!sec->data) {
190 WARN_ELF("elf_getdata");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600191 return -1;
192 }
193
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500194 if (sec->data->d_off != 0 ||
195 sec->data->d_size != sec->sh.sh_size) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600196 WARN("unexpected data attributes for %s", sec->name);
197 return -1;
198 }
199
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500200 sec->len = sec->data->d_size;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600201 }
202
203 /* sanity check, one more call to elf_nextscn() should return NULL */
204 if (elf_nextscn(elf->elf, s)) {
205 WARN("section entry mismatch");
206 return -1;
207 }
208
209 return 0;
210}
211
212static int read_symbols(struct elf *elf)
213{
214 struct section *symtab;
215 struct symbol *sym;
216 struct list_head *entry, *tmp;
217 int symbols_nr, i;
218
219 symtab = find_section_by_name(elf, ".symtab");
220 if (!symtab) {
221 WARN("missing symbol table");
222 return -1;
223 }
224
225 symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize;
226
227 for (i = 0; i < symbols_nr; i++) {
228 sym = malloc(sizeof(*sym));
229 if (!sym) {
230 perror("malloc");
231 return -1;
232 }
233 memset(sym, 0, sizeof(*sym));
234
235 sym->idx = i;
236
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500237 if (!gelf_getsym(symtab->data, i, &sym->sym)) {
238 WARN_ELF("gelf_getsym");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600239 goto err;
240 }
241
242 sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
243 sym->sym.st_name);
244 if (!sym->name) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500245 WARN_ELF("elf_strptr");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600246 goto err;
247 }
248
249 sym->type = GELF_ST_TYPE(sym->sym.st_info);
250 sym->bind = GELF_ST_BIND(sym->sym.st_info);
251
252 if (sym->sym.st_shndx > SHN_UNDEF &&
253 sym->sym.st_shndx < SHN_LORESERVE) {
254 sym->sec = find_section_by_index(elf,
255 sym->sym.st_shndx);
256 if (!sym->sec) {
257 WARN("couldn't find section for symbol %s",
258 sym->name);
259 goto err;
260 }
261 if (sym->type == STT_SECTION) {
262 sym->name = sym->sec->name;
263 sym->sec->sym = sym;
264 }
265 } else
266 sym->sec = find_section_by_index(elf, 0);
267
268 sym->offset = sym->sym.st_value;
269 sym->len = sym->sym.st_size;
270
271 /* sorted insert into a per-section list */
Josh Poimboeufa196e172016-03-09 00:06:57 -0600272 entry = &sym->sec->symbol_list;
273 list_for_each_prev(tmp, &sym->sec->symbol_list) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600274 struct symbol *s;
275
276 s = list_entry(tmp, struct symbol, list);
277
278 if (sym->offset > s->offset) {
279 entry = tmp;
280 break;
281 }
282
283 if (sym->offset == s->offset && sym->len >= s->len) {
284 entry = tmp;
285 break;
286 }
287 }
288 list_add(&sym->list, entry);
Josh Poimboeuf042ba732016-03-09 00:07:00 -0600289 hash_add(sym->sec->symbol_hash, &sym->hash, sym->idx);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600290 }
291
292 return 0;
293
294err:
295 free(sym);
296 return -1;
297}
298
299static int read_relas(struct elf *elf)
300{
301 struct section *sec;
302 struct rela *rela;
303 int i;
304 unsigned int symndx;
305
306 list_for_each_entry(sec, &elf->sections, list) {
307 if (sec->sh.sh_type != SHT_RELA)
308 continue;
309
310 sec->base = find_section_by_name(elf, sec->name + 5);
311 if (!sec->base) {
312 WARN("can't find base section for rela section %s",
313 sec->name);
314 return -1;
315 }
316
317 sec->base->rela = sec;
318
319 for (i = 0; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) {
320 rela = malloc(sizeof(*rela));
321 if (!rela) {
322 perror("malloc");
323 return -1;
324 }
325 memset(rela, 0, sizeof(*rela));
326
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500327 if (!gelf_getrela(sec->data, i, &rela->rela)) {
328 WARN_ELF("gelf_getrela");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600329 return -1;
330 }
331
332 rela->type = GELF_R_TYPE(rela->rela.r_info);
333 rela->addend = rela->rela.r_addend;
334 rela->offset = rela->rela.r_offset;
335 symndx = GELF_R_SYM(rela->rela.r_info);
336 rela->sym = find_symbol_by_index(elf, symndx);
337 if (!rela->sym) {
338 WARN("can't find rela entry symbol %d for %s",
339 symndx, sec->name);
340 return -1;
341 }
Josh Poimboeuf042ba732016-03-09 00:07:00 -0600342
343 list_add_tail(&rela->list, &sec->rela_list);
344 hash_add(sec->rela_hash, &rela->hash, rela->offset);
345
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600346 }
347 }
348
349 return 0;
350}
351
352struct elf *elf_open(const char *name)
353{
354 struct elf *elf;
355
356 elf_version(EV_CURRENT);
357
358 elf = malloc(sizeof(*elf));
359 if (!elf) {
360 perror("malloc");
361 return NULL;
362 }
363 memset(elf, 0, sizeof(*elf));
364
365 INIT_LIST_HEAD(&elf->sections);
366
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600367 elf->fd = open(name, O_RDONLY);
368 if (elf->fd == -1) {
369 perror("open");
370 goto err;
371 }
372
373 elf->elf = elf_begin(elf->fd, ELF_C_READ_MMAP, NULL);
374 if (!elf->elf) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500375 WARN_ELF("elf_begin");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600376 goto err;
377 }
378
379 if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500380 WARN_ELF("gelf_getehdr");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600381 goto err;
382 }
383
384 if (read_sections(elf))
385 goto err;
386
387 if (read_symbols(elf))
388 goto err;
389
390 if (read_relas(elf))
391 goto err;
392
393 return elf;
394
395err:
396 elf_close(elf);
397 return NULL;
398}
399
400void elf_close(struct elf *elf)
401{
402 struct section *sec, *tmpsec;
403 struct symbol *sym, *tmpsym;
404 struct rela *rela, *tmprela;
405
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500406 if (elf->elf)
407 elf_end(elf->elf);
408
409 if (elf->fd > 0)
410 close(elf->fd);
411
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600412 list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) {
Josh Poimboeufa196e172016-03-09 00:06:57 -0600413 list_for_each_entry_safe(sym, tmpsym, &sec->symbol_list, list) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600414 list_del(&sym->list);
Josh Poimboeuf042ba732016-03-09 00:07:00 -0600415 hash_del(&sym->hash);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600416 free(sym);
417 }
Josh Poimboeufa196e172016-03-09 00:06:57 -0600418 list_for_each_entry_safe(rela, tmprela, &sec->rela_list, list) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600419 list_del(&rela->list);
Josh Poimboeuf042ba732016-03-09 00:07:00 -0600420 hash_del(&rela->hash);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600421 free(rela);
422 }
423 list_del(&sec->list);
424 free(sec);
425 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500426
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600427 free(elf);
428}