amd/common: add a more powerful runtime linker
[mesa.git] / src / amd / common / ac_rtld.h
1 /*
2 * Copyright 2014-2019 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #ifndef AC_RTLD_H
25 #define AC_RTLD_H
26
27 #include <stdbool.h>
28 #include <stdint.h>
29 #include <stddef.h>
30
31 struct ac_rtld_part;
32 struct ac_shader_config;
33
34 /* Lightweight wrapper around underlying ELF objects. */
35 struct ac_rtld_binary {
36 /* Required buffer sizes, currently read/executable only. */
37 uint64_t rx_size;
38
39 uint64_t rx_end_markers;
40
41 unsigned num_parts;
42 struct ac_rtld_part *parts;
43 };
44
45 /**
46 * Callback function type used during upload to resolve external symbols that
47 * are not defined in any of the ELF binaries available to the linker.
48 *
49 * \param cb_data caller-defined data
50 * \param symbol NUL-terminated symbol name
51 * \param value to be filled in by the callback
52 * \return whether the symbol was found successfully
53 */
54 typedef bool (*ac_rtld_get_external_symbol_cb)(
55 void *cb_data, const char *symbol, uint64_t *value);
56
57 bool ac_rtld_open(struct ac_rtld_binary *binary, unsigned num_parts,
58 const char * const *elf_ptrs,
59 const size_t *elf_sizes);
60
61 void ac_rtld_close(struct ac_rtld_binary *binary);
62
63 bool ac_rtld_get_section_by_name(struct ac_rtld_binary *binary, const char *name,
64 const char **data, size_t *nbytes);
65
66 bool ac_rtld_read_config(struct ac_rtld_binary *binary,
67 struct ac_shader_config *config);
68
69 struct ac_rtld_upload_info {
70 struct ac_rtld_binary *binary;
71
72 /** GPU mapping of the read/executable buffer. */
73 uint64_t rx_va;
74
75 /** CPU mapping of the read/executable buffer */
76 char *rx_ptr;
77
78 /** Optional callback function that will be queried for symbols not
79 * defined in any of the binary's parts. */
80 ac_rtld_get_external_symbol_cb get_external_symbol;
81
82 /** Caller-defined data that will be passed to callback functions. */
83 void *cb_data;
84 };
85
86 bool ac_rtld_upload(struct ac_rtld_upload_info *u);
87
88 #endif /* AC_RTLD_H */