summaryrefslogtreecommitdiff
path: root/src/amd/common/ac_rtld.h
blob: b13270b181d43c61afa67bb2137b9db29bd8ca22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 * Copyright 2014-2019 Advanced Micro Devices, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#ifndef AC_RTLD_H
#define AC_RTLD_H

#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>

#include "util/u_dynarray.h"

struct ac_rtld_part;
struct ac_shader_config;
struct radeon_info;

struct ac_rtld_symbol {
	const char *name;
	uint32_t size;
	uint32_t align;
	uint64_t offset; /* filled in by ac_rtld_open */
	unsigned part_idx; /* shader part in which this symbol appears */
};

struct ac_rtld_options {
	/* Loader will insert an s_sethalt 1 instruction as the
	 * first instruction. */
	bool halt_at_entry:1;
};

/* Lightweight wrapper around underlying ELF objects. */
struct ac_rtld_binary {
	struct ac_rtld_options options;

	/* Required buffer sizes, currently read/executable only. */
	uint64_t rx_size;

	uint64_t rx_end_markers;

	unsigned num_parts;
	struct ac_rtld_part *parts;

	struct util_dynarray lds_symbols;
	uint32_t lds_size;
};

/**
 * Callback function type used during upload to resolve external symbols that
 * are not defined in any of the ELF binaries available to the linker.
 *
 * \param cb_data caller-defined data
 * \param symbol NUL-terminated symbol name
 * \param value to be filled in by the callback
 * \return whether the symbol was found successfully
 */
typedef bool (*ac_rtld_get_external_symbol_cb)(
	void *cb_data, const char *symbol, uint64_t *value);

/**
 * Lifetimes of \ref info, in-memory ELF objects, and the names of
 * \ref shared_lds_symbols must extend until \ref ac_rtld_close is called on
 * the opened binary.
 */
struct ac_rtld_open_info {
	const struct radeon_info *info;
	struct ac_rtld_options options;

	unsigned num_parts;
	const char * const *elf_ptrs; /* in-memory ELF objects of each part */
	const size_t *elf_sizes; /* sizes of corresponding in-memory ELF objects in bytes */

	/* Shared LDS symbols are layouted such that they are accessible from
	 * all shader parts. Non-shared (private) LDS symbols of one part may
	 * overlap private LDS symbols of another shader part.
	 */
	unsigned num_shared_lds_symbols;
	const struct ac_rtld_symbol *shared_lds_symbols;
};

bool ac_rtld_open(struct ac_rtld_binary *binary,
		  struct ac_rtld_open_info i);

void ac_rtld_close(struct ac_rtld_binary *binary);

bool ac_rtld_get_section_by_name(struct ac_rtld_binary *binary, const char *name,
				 const char **data, size_t *nbytes);

bool ac_rtld_read_config(struct ac_rtld_binary *binary,
			 struct ac_shader_config *config);

struct ac_rtld_upload_info {
	struct ac_rtld_binary *binary;

	/** GPU mapping of the read/executable buffer. */
	uint64_t rx_va;

	/** CPU mapping of the read/executable buffer */
	char *rx_ptr;

	/** Optional callback function that will be queried for symbols not
	 * defined in any of the binary's parts. */
	ac_rtld_get_external_symbol_cb get_external_symbol;

	/** Caller-defined data that will be passed to callback functions. */
	void *cb_data;
};

bool ac_rtld_upload(struct ac_rtld_upload_info *u);

#endif /* AC_RTLD_H */