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