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 "compiler/shader_enums.h"
28#include "util/u_dynarray.h"
29
30#include <stdbool.h>
31#include <stddef.h>
32#include <stdint.h>
33
34#ifdef __cplusplus
35extern "C" {
36#endif
37
38struct ac_rtld_part;
39struct ac_shader_config;
40struct radeon_info;
41
42struct ac_rtld_symbol {
43   const char *name;
44   uint32_t size;
45   uint32_t align;
46   uint64_t offset;   /* filled in by ac_rtld_open */
47   unsigned part_idx; /* shader part in which this symbol appears */
48};
49
50struct ac_rtld_options {
51   /* Loader will insert an s_sethalt 1 instruction as the
52    * first instruction. */
53   bool halt_at_entry : 1;
54};
55
56/* Lightweight wrapper around underlying ELF objects. */
57struct ac_rtld_binary {
58   struct ac_rtld_options options;
59   unsigned wave_size;
60
61   /* Required buffer sizes, currently read/executable only. */
62   uint64_t rx_size;
63
64   /* Size of executable code, for reporting purposes. */
65   uint64_t exec_size;
66
67   uint64_t rx_end_markers;
68
69   unsigned num_parts;
70   struct ac_rtld_part *parts;
71
72   struct util_dynarray lds_symbols;
73   uint32_t lds_size;
74};
75
76/**
77 * Callback function type used during upload to resolve external symbols that
78 * are not defined in any of the ELF binaries available to the linker.
79 *
80 * \param cb_data caller-defined data
81 * \param symbol NUL-terminated symbol name
82 * \param value to be filled in by the callback
83 * \return whether the symbol was found successfully
84 */
85typedef bool (*ac_rtld_get_external_symbol_cb)(void *cb_data, const char *symbol, uint64_t *value);
86
87/**
88 * Lifetimes of \ref info, in-memory ELF objects, and the names of
89 * \ref shared_lds_symbols must extend until \ref ac_rtld_close is called on
90 * the opened binary.
91 */
92struct ac_rtld_open_info {
93   const struct radeon_info *info;
94   struct ac_rtld_options options;
95   gl_shader_stage shader_type;
96   unsigned wave_size;
97
98   unsigned num_parts;
99   const char *const *elf_ptrs; /* in-memory ELF objects of each part */
100   const size_t *elf_sizes;     /* sizes of corresponding in-memory ELF objects in bytes */
101
102   /* Shared LDS symbols are layouted such that they are accessible from
103    * all shader parts. Non-shared (private) LDS symbols of one part may
104    * overlap private LDS symbols of another shader part.
105    */
106   unsigned num_shared_lds_symbols;
107   const struct ac_rtld_symbol *shared_lds_symbols;
108};
109
110bool ac_rtld_open(struct ac_rtld_binary *binary, struct ac_rtld_open_info i);
111
112void ac_rtld_close(struct ac_rtld_binary *binary);
113
114bool ac_rtld_get_section_by_name(struct ac_rtld_binary *binary, const char *name, const char **data,
115                                 size_t *nbytes);
116
117bool ac_rtld_read_config(const struct radeon_info *info, struct ac_rtld_binary *binary,
118                         struct ac_shader_config *config);
119
120struct ac_rtld_upload_info {
121   struct ac_rtld_binary *binary;
122
123   /** GPU mapping of the read/executable buffer. */
124   uint64_t rx_va;
125
126   /** CPU mapping of the read/executable buffer */
127   char *rx_ptr;
128
129   /** Optional callback function that will be queried for symbols not
130    * defined in any of the binary's parts. */
131   ac_rtld_get_external_symbol_cb get_external_symbol;
132
133   /** Caller-defined data that will be passed to callback functions. */
134   void *cb_data;
135};
136
137int ac_rtld_upload(struct ac_rtld_upload_info *u);
138
139#ifdef __cplusplus
140}
141#endif
142
143#endif /* AC_RTLD_H */
144