ac: fail shader compilation if libelf is replaced by an incompatible version
[mesa.git] / src / amd / common / ac_binary.c
1 /*
2 * Copyright 2014 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 * Authors: Tom Stellard <thomas.stellard@amd.com>
24 *
25 * Based on radeon_elf_util.c.
26 */
27
28 #include "ac_binary.h"
29
30 #include "util/u_math.h"
31 #include "util/u_memory.h"
32
33 #include <gelf.h>
34 #include <libelf.h>
35 #include <stdio.h>
36
37 #include <sid.h>
38
39 #define SPILLED_SGPRS 0x4
40 #define SPILLED_VGPRS 0x8
41
42 static void parse_symbol_table(Elf_Data *symbol_table_data,
43 const GElf_Shdr *symbol_table_header,
44 struct ac_shader_binary *binary)
45 {
46 GElf_Sym symbol;
47 unsigned i = 0;
48 unsigned symbol_count =
49 symbol_table_header->sh_size / symbol_table_header->sh_entsize;
50
51 /* We are over allocating this list, because symbol_count gives the
52 * total number of symbols, and we will only be filling the list
53 * with offsets of global symbols. The memory savings from
54 * allocating the correct size of this list will be small, and
55 * I don't think it is worth the cost of pre-computing the number
56 * of global symbols.
57 */
58 binary->global_symbol_offsets = CALLOC(symbol_count, sizeof(uint64_t));
59
60 while (gelf_getsym(symbol_table_data, i++, &symbol)) {
61 unsigned i;
62 if (GELF_ST_BIND(symbol.st_info) != STB_GLOBAL ||
63 symbol.st_shndx == 0 /* Undefined symbol */) {
64 continue;
65 }
66
67 binary->global_symbol_offsets[binary->global_symbol_count] =
68 symbol.st_value;
69
70 /* Sort the list using bubble sort. This list will usually
71 * be small. */
72 for (i = binary->global_symbol_count; i > 0; --i) {
73 uint64_t lhs = binary->global_symbol_offsets[i - 1];
74 uint64_t rhs = binary->global_symbol_offsets[i];
75 if (lhs < rhs) {
76 break;
77 }
78 binary->global_symbol_offsets[i] = lhs;
79 binary->global_symbol_offsets[i - 1] = rhs;
80 }
81 ++binary->global_symbol_count;
82 }
83 }
84
85 static void parse_relocs(Elf *elf, Elf_Data *relocs, Elf_Data *symbols,
86 unsigned symbol_sh_link,
87 struct ac_shader_binary *binary)
88 {
89 unsigned i;
90
91 if (!relocs || !symbols || !binary->reloc_count) {
92 return;
93 }
94 binary->relocs = CALLOC(binary->reloc_count,
95 sizeof(struct ac_shader_reloc));
96 for (i = 0; i < binary->reloc_count; i++) {
97 GElf_Sym symbol;
98 GElf_Rel rel;
99 char *symbol_name;
100 struct ac_shader_reloc *reloc = &binary->relocs[i];
101
102 gelf_getrel(relocs, i, &rel);
103 gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &symbol);
104 symbol_name = elf_strptr(elf, symbol_sh_link, symbol.st_name);
105
106 reloc->offset = rel.r_offset;
107 strncpy(reloc->name, symbol_name, sizeof(reloc->name)-1);
108 reloc->name[sizeof(reloc->name)-1] = 0;
109 }
110 }
111
112 bool ac_elf_read(const char *elf_data, unsigned elf_size,
113 struct ac_shader_binary *binary)
114 {
115 char *elf_buffer;
116 Elf *elf;
117 Elf_Scn *section = NULL;
118 Elf_Data *symbols = NULL, *relocs = NULL;
119 size_t section_str_index;
120 unsigned symbol_sh_link = 0;
121 bool success = true;
122
123 /* One of the libelf implementations
124 * (http://www.mr511.de/software/english.htm) requires calling
125 * elf_version() before elf_memory().
126 */
127 elf_version(EV_CURRENT);
128 elf_buffer = MALLOC(elf_size);
129 memcpy(elf_buffer, elf_data, elf_size);
130
131 elf = elf_memory(elf_buffer, elf_size);
132
133 elf_getshdrstrndx(elf, &section_str_index);
134
135 while ((section = elf_nextscn(elf, section))) {
136 const char *name;
137 Elf_Data *section_data = NULL;
138 GElf_Shdr section_header;
139 if (gelf_getshdr(section, &section_header) != &section_header) {
140 fprintf(stderr, "Failed to read ELF section header\n");
141 success = false;
142 break;
143 }
144 name = elf_strptr(elf, section_str_index, section_header.sh_name);
145 if (!strcmp(name, ".text")) {
146 section_data = elf_getdata(section, section_data);
147 binary->code_size = section_data->d_size;
148 binary->code = MALLOC(binary->code_size * sizeof(unsigned char));
149 memcpy(binary->code, section_data->d_buf, binary->code_size);
150 } else if (!strcmp(name, ".AMDGPU.config")) {
151 section_data = elf_getdata(section, section_data);
152 binary->config_size = section_data->d_size;
153 if (!binary->config_size) {
154 fprintf(stderr, ".AMDGPU.config is empty!\n");
155 success = false;
156 break;
157 }
158 binary->config = MALLOC(binary->config_size * sizeof(unsigned char));
159 memcpy(binary->config, section_data->d_buf, binary->config_size);
160 } else if (!strcmp(name, ".AMDGPU.disasm")) {
161 /* Always read disassembly if it's available. */
162 section_data = elf_getdata(section, section_data);
163 binary->disasm_string = strndup(section_data->d_buf,
164 section_data->d_size);
165 } else if (!strncmp(name, ".rodata", 7)) {
166 section_data = elf_getdata(section, section_data);
167 binary->rodata_size = section_data->d_size;
168 binary->rodata = MALLOC(binary->rodata_size * sizeof(unsigned char));
169 memcpy(binary->rodata, section_data->d_buf, binary->rodata_size);
170 } else if (!strncmp(name, ".symtab", 7)) {
171 symbols = elf_getdata(section, section_data);
172 symbol_sh_link = section_header.sh_link;
173 parse_symbol_table(symbols, &section_header, binary);
174 } else if (!strcmp(name, ".rel.text")) {
175 relocs = elf_getdata(section, section_data);
176 binary->reloc_count = section_header.sh_size /
177 section_header.sh_entsize;
178 }
179 }
180
181 parse_relocs(elf, relocs, symbols, symbol_sh_link, binary);
182
183 if (elf){
184 elf_end(elf);
185 }
186 FREE(elf_buffer);
187
188 /* Cache the config size per symbol */
189 if (binary->global_symbol_count) {
190 binary->config_size_per_symbol =
191 binary->config_size / binary->global_symbol_count;
192 } else {
193 binary->global_symbol_count = 1;
194 binary->config_size_per_symbol = binary->config_size;
195 }
196 return success;
197 }
198
199 const unsigned char *ac_shader_binary_config_start(
200 const struct ac_shader_binary *binary,
201 uint64_t symbol_offset)
202 {
203 unsigned i;
204 for (i = 0; i < binary->global_symbol_count; ++i) {
205 if (binary->global_symbol_offsets[i] == symbol_offset) {
206 unsigned offset = i * binary->config_size_per_symbol;
207 return binary->config + offset;
208 }
209 }
210 return binary->config;
211 }
212
213
214 static const char *scratch_rsrc_dword0_symbol =
215 "SCRATCH_RSRC_DWORD0";
216
217 static const char *scratch_rsrc_dword1_symbol =
218 "SCRATCH_RSRC_DWORD1";
219
220 void ac_shader_binary_read_config(struct ac_shader_binary *binary,
221 struct ac_shader_config *conf,
222 unsigned symbol_offset,
223 bool supports_spill)
224 {
225 unsigned i;
226 const unsigned char *config =
227 ac_shader_binary_config_start(binary, symbol_offset);
228 bool really_needs_scratch = false;
229 uint32_t wavesize = 0;
230 /* LLVM adds SGPR spills to the scratch size.
231 * Find out if we really need the scratch buffer.
232 */
233 if (supports_spill) {
234 really_needs_scratch = true;
235 } else {
236 for (i = 0; i < binary->reloc_count; i++) {
237 const struct ac_shader_reloc *reloc = &binary->relocs[i];
238
239 if (!strcmp(scratch_rsrc_dword0_symbol, reloc->name) ||
240 !strcmp(scratch_rsrc_dword1_symbol, reloc->name)) {
241 really_needs_scratch = true;
242 break;
243 }
244 }
245 }
246
247 for (i = 0; i < binary->config_size_per_symbol; i+= 8) {
248 unsigned reg = util_le32_to_cpu(*(uint32_t*)(config + i));
249 unsigned value = util_le32_to_cpu(*(uint32_t*)(config + i + 4));
250 switch (reg) {
251 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
252 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
253 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
254 case R_00B848_COMPUTE_PGM_RSRC1:
255 conf->num_sgprs = MAX2(conf->num_sgprs, (G_00B028_SGPRS(value) + 1) * 8);
256 conf->num_vgprs = MAX2(conf->num_vgprs, (G_00B028_VGPRS(value) + 1) * 4);
257 conf->float_mode = G_00B028_FLOAT_MODE(value);
258 break;
259 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
260 conf->lds_size = MAX2(conf->lds_size, G_00B02C_EXTRA_LDS_SIZE(value));
261 break;
262 case R_00B84C_COMPUTE_PGM_RSRC2:
263 conf->lds_size = MAX2(conf->lds_size, G_00B84C_LDS_SIZE(value));
264 break;
265 case R_0286CC_SPI_PS_INPUT_ENA:
266 conf->spi_ps_input_ena = value;
267 break;
268 case R_0286D0_SPI_PS_INPUT_ADDR:
269 conf->spi_ps_input_addr = value;
270 break;
271 case R_0286E8_SPI_TMPRING_SIZE:
272 case R_00B860_COMPUTE_TMPRING_SIZE:
273 /* WAVESIZE is in units of 256 dwords. */
274 wavesize = value;
275 break;
276 case SPILLED_SGPRS:
277 conf->spilled_sgprs = value;
278 break;
279 case SPILLED_VGPRS:
280 conf->spilled_vgprs = value;
281 break;
282 default:
283 {
284 static bool printed;
285
286 if (!printed) {
287 fprintf(stderr, "Warning: LLVM emitted unknown "
288 "config register: 0x%x\n", reg);
289 printed = true;
290 }
291 }
292 break;
293 }
294
295 if (!conf->spi_ps_input_addr)
296 conf->spi_ps_input_addr = conf->spi_ps_input_ena;
297 }
298
299 if (really_needs_scratch) {
300 /* sgprs spills aren't spilling */
301 conf->scratch_bytes_per_wave = G_00B860_WAVESIZE(wavesize) * 256 * 4;
302 }
303 }