blob: 43851009ad55e682f29f1dd28f6a3a330926028d [file] [log] [blame]
Aaron Durbin4fde5a62014-03-07 15:11:53 -06001/*
2 * Copyright (C) 2014 Google, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
Aaron Durbin4fde5a62014-03-07 15:11:53 -060012 */
13
14#ifndef TOOL_RMODULE_H
15#define TOOL_RMODULE_H
16
Aaron Durbinb39a9742015-09-08 17:24:04 -050017#include "elfparsing.h"
Aaron Durbin4fde5a62014-03-07 15:11:53 -060018#include "common.h"
19
Aaron Durbinb39a9742015-09-08 17:24:04 -050020struct arch_ops {
21 int arch;
22 /* Determine if relocation is a valid type for the architecture. */
23 int (*valid_type)(Elf64_Rela *rel);
24 /* Determine if relocation should be emitted. */
25 int (*should_emit)(Elf64_Rela *rel);
26};
27
28/*
29 * The fields in rmod_context are read-only to the user. These are
30 * exposed for easy shareability.
31 */
32struct rmod_context {
33 /* Ops to process relocations. */
34 const struct arch_ops *ops;
35
36 /* endian conversion ops */
37 struct xdr *xdr;
38
39 /* Parsed ELF sturcture. */
40 struct parsed_elf pelf;
41 /* Program segment. */
42 Elf64_Phdr *phdr;
43
44 /* Collection of relocation addresses fixup in the module. */
45 Elf64_Xword nrelocs;
46 Elf64_Addr *emitted_relocs;
47
48 /* The following fields are addresses within the linked program. */
49 Elf64_Addr parameters_begin;
50 Elf64_Addr parameters_end;
51 Elf64_Addr bss_begin;
52 Elf64_Addr bss_end;
53};
54
55struct reloc_filter {
56 /* Return < 0 on error. 0 to ignore relocation and 1 to include
57 * relocation. */
58 int (*filter)(struct reloc_filter *f, const Elf64_Rela *r);
59 /* Pointer for filter provides */
60 void *context;
61};
62
Aaron Durbin4fde5a62014-03-07 15:11:53 -060063/*
64 * Parse an ELF file within the elfin buffer and fill in the elfout buffer
65 * with a created rmodule in ELF format. Return 0 on success, < 0 on error.
66 */
67int rmodule_create(const struct buffer *elfin, struct buffer *elfout);
68
Aaron Durbinb39a9742015-09-08 17:24:04 -050069/*
70 * Initialize an rmodule context from an ELF buffer. Returns 0 on scucess, < 0
71 * on error.
72 */
73int rmodule_init(struct rmod_context *ctx, const struct buffer *elfin);
74
75/*
76 * Collect all the relocations that apply to the program in
77 * nrelocs/emitted_relocs. One can optionally provide a reloc_filter object
78 * to help in relocation filtering. The filter function will be called twice:
79 * once for counting and once for emitting. The same response should be
80 * provided for each call. Returns 0 on success, < 0 on error.
81 */
82int rmodule_collect_relocations(struct rmod_context *c, struct reloc_filter *f);
83
84/* Clean up the memory consumed by the rmdoule context. */
85void rmodule_cleanup(struct rmod_context *ctx);
86
Aaron Durbin694fd132015-10-28 11:39:34 -050087/*
88 * Create an ELF file from the passed in rmodule in the buffer. The buffer
89 * contents will be replaced with an ELF file. Returns 1 if buff doesn't
90 * contain an rmodule and < 0 on failure, 0 on success.
91 */
92int rmodule_stage_to_elf(Elf64_Ehdr *ehdr, struct buffer *buff);
93
Aaron Durbin4fde5a62014-03-07 15:11:53 -060094#endif /* TOOL_RMODULE_H */