2 * Copyright 2014 Advanced Micro Devices, Inc.
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:
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
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
23 * Authors: Tom Stellard <thomas.stellard@amd.com>
25 * Based on radeon_elf_util.c.
28 #include "ac_binary.h"
30 #include "util/u_math.h"
31 #include "util/u_memory.h"
39 #define SPILLED_SGPRS 0x4
40 #define SPILLED_VGPRS 0x8
42 static void parse_symbol_table(Elf_Data
*symbol_table_data
,
43 const GElf_Shdr
*symbol_table_header
,
44 struct ac_shader_binary
*binary
)
48 unsigned symbol_count
=
49 symbol_table_header
->sh_size
/ symbol_table_header
->sh_entsize
;
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
58 binary
->global_symbol_offsets
= CALLOC(symbol_count
, sizeof(uint64_t));
60 while (gelf_getsym(symbol_table_data
, i
++, &symbol
)) {
62 if (GELF_ST_BIND(symbol
.st_info
) != STB_GLOBAL
||
63 symbol
.st_shndx
== 0 /* Undefined symbol */) {
67 binary
->global_symbol_offsets
[binary
->global_symbol_count
] =
70 /* Sort the list using bubble sort. This list will usually
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
];
78 binary
->global_symbol_offsets
[i
] = lhs
;
79 binary
->global_symbol_offsets
[i
- 1] = rhs
;
81 ++binary
->global_symbol_count
;
85 static void parse_relocs(Elf
*elf
, Elf_Data
*relocs
, Elf_Data
*symbols
,
86 unsigned symbol_sh_link
,
87 struct ac_shader_binary
*binary
)
91 if (!relocs
|| !symbols
|| !binary
->reloc_count
) {
94 binary
->relocs
= CALLOC(binary
->reloc_count
,
95 sizeof(struct ac_shader_reloc
));
96 for (i
= 0; i
< binary
->reloc_count
; i
++) {
100 struct ac_shader_reloc
*reloc
= &binary
->relocs
[i
];
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
);
106 reloc
->offset
= rel
.r_offset
;
107 strncpy(reloc
->name
, symbol_name
, sizeof(reloc
->name
)-1);
108 reloc
->name
[sizeof(reloc
->name
)-1] = 0;
112 void ac_elf_read(const char *elf_data
, unsigned elf_size
,
113 struct ac_shader_binary
*binary
)
117 Elf_Scn
*section
= NULL
;
118 Elf_Data
*symbols
= NULL
, *relocs
= NULL
;
119 size_t section_str_index
;
120 unsigned symbol_sh_link
= 0;
122 /* One of the libelf implementations
123 * (http://www.mr511.de/software/english.htm) requires calling
124 * elf_version() before elf_memory().
126 elf_version(EV_CURRENT
);
127 elf_buffer
= MALLOC(elf_size
);
128 memcpy(elf_buffer
, elf_data
, elf_size
);
130 elf
= elf_memory(elf_buffer
, elf_size
);
132 elf_getshdrstrndx(elf
, §ion_str_index
);
134 while ((section
= elf_nextscn(elf
, section
))) {
136 Elf_Data
*section_data
= NULL
;
137 GElf_Shdr section_header
;
138 if (gelf_getshdr(section
, §ion_header
) != §ion_header
) {
139 fprintf(stderr
, "Failed to read ELF section header\n");
142 name
= elf_strptr(elf
, section_str_index
, section_header
.sh_name
);
143 if (!strcmp(name
, ".text")) {
144 section_data
= elf_getdata(section
, section_data
);
145 binary
->code_size
= section_data
->d_size
;
146 binary
->code
= MALLOC(binary
->code_size
* sizeof(unsigned char));
147 memcpy(binary
->code
, section_data
->d_buf
, binary
->code_size
);
148 } else if (!strcmp(name
, ".AMDGPU.config")) {
149 section_data
= elf_getdata(section
, section_data
);
150 binary
->config_size
= section_data
->d_size
;
151 binary
->config
= MALLOC(binary
->config_size
* sizeof(unsigned char));
152 memcpy(binary
->config
, section_data
->d_buf
, binary
->config_size
);
153 } else if (!strcmp(name
, ".AMDGPU.disasm")) {
154 /* Always read disassembly if it's available. */
155 section_data
= elf_getdata(section
, section_data
);
156 binary
->disasm_string
= strndup(section_data
->d_buf
,
157 section_data
->d_size
);
158 } else if (!strncmp(name
, ".rodata", 7)) {
159 section_data
= elf_getdata(section
, section_data
);
160 binary
->rodata_size
= section_data
->d_size
;
161 binary
->rodata
= MALLOC(binary
->rodata_size
* sizeof(unsigned char));
162 memcpy(binary
->rodata
, section_data
->d_buf
, binary
->rodata_size
);
163 } else if (!strncmp(name
, ".symtab", 7)) {
164 symbols
= elf_getdata(section
, section_data
);
165 symbol_sh_link
= section_header
.sh_link
;
166 parse_symbol_table(symbols
, §ion_header
, binary
);
167 } else if (!strcmp(name
, ".rel.text")) {
168 relocs
= elf_getdata(section
, section_data
);
169 binary
->reloc_count
= section_header
.sh_size
/
170 section_header
.sh_entsize
;
174 parse_relocs(elf
, relocs
, symbols
, symbol_sh_link
, binary
);
181 /* Cache the config size per symbol */
182 if (binary
->global_symbol_count
) {
183 binary
->config_size_per_symbol
=
184 binary
->config_size
/ binary
->global_symbol_count
;
186 binary
->global_symbol_count
= 1;
187 binary
->config_size_per_symbol
= binary
->config_size
;
191 const unsigned char *ac_shader_binary_config_start(
192 const struct ac_shader_binary
*binary
,
193 uint64_t symbol_offset
)
196 for (i
= 0; i
< binary
->global_symbol_count
; ++i
) {
197 if (binary
->global_symbol_offsets
[i
] == symbol_offset
) {
198 unsigned offset
= i
* binary
->config_size_per_symbol
;
199 return binary
->config
+ offset
;
202 return binary
->config
;
206 static const char *scratch_rsrc_dword0_symbol
=
207 "SCRATCH_RSRC_DWORD0";
209 static const char *scratch_rsrc_dword1_symbol
=
210 "SCRATCH_RSRC_DWORD1";
212 void ac_shader_binary_read_config(struct ac_shader_binary
*binary
,
213 struct ac_shader_config
*conf
,
214 unsigned symbol_offset
,
218 const unsigned char *config
=
219 ac_shader_binary_config_start(binary
, symbol_offset
);
220 bool really_needs_scratch
= false;
221 uint32_t wavesize
= 0;
222 /* LLVM adds SGPR spills to the scratch size.
223 * Find out if we really need the scratch buffer.
225 if (supports_spill
) {
226 really_needs_scratch
= true;
228 for (i
= 0; i
< binary
->reloc_count
; i
++) {
229 const struct ac_shader_reloc
*reloc
= &binary
->relocs
[i
];
231 if (!strcmp(scratch_rsrc_dword0_symbol
, reloc
->name
) ||
232 !strcmp(scratch_rsrc_dword1_symbol
, reloc
->name
)) {
233 really_needs_scratch
= true;
239 for (i
= 0; i
< binary
->config_size_per_symbol
; i
+= 8) {
240 unsigned reg
= util_le32_to_cpu(*(uint32_t*)(config
+ i
));
241 unsigned value
= util_le32_to_cpu(*(uint32_t*)(config
+ i
+ 4));
243 case R_00B028_SPI_SHADER_PGM_RSRC1_PS
:
244 case R_00B128_SPI_SHADER_PGM_RSRC1_VS
:
245 case R_00B228_SPI_SHADER_PGM_RSRC1_GS
:
246 case R_00B848_COMPUTE_PGM_RSRC1
:
247 conf
->num_sgprs
= MAX2(conf
->num_sgprs
, (G_00B028_SGPRS(value
) + 1) * 8);
248 conf
->num_vgprs
= MAX2(conf
->num_vgprs
, (G_00B028_VGPRS(value
) + 1) * 4);
249 conf
->float_mode
= G_00B028_FLOAT_MODE(value
);
251 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS
:
252 conf
->lds_size
= MAX2(conf
->lds_size
, G_00B02C_EXTRA_LDS_SIZE(value
));
254 case R_00B84C_COMPUTE_PGM_RSRC2
:
255 conf
->lds_size
= MAX2(conf
->lds_size
, G_00B84C_LDS_SIZE(value
));
257 case R_0286CC_SPI_PS_INPUT_ENA
:
258 conf
->spi_ps_input_ena
= value
;
260 case R_0286D0_SPI_PS_INPUT_ADDR
:
261 conf
->spi_ps_input_addr
= value
;
263 case R_0286E8_SPI_TMPRING_SIZE
:
264 case R_00B860_COMPUTE_TMPRING_SIZE
:
265 /* WAVESIZE is in units of 256 dwords. */
269 conf
->spilled_sgprs
= value
;
272 conf
->spilled_vgprs
= value
;
279 fprintf(stderr
, "Warning: LLVM emitted unknown "
280 "config register: 0x%x\n", reg
);
287 if (!conf
->spi_ps_input_addr
)
288 conf
->spi_ps_input_addr
= conf
->spi_ps_input_ena
;
291 if (really_needs_scratch
) {
292 /* sgprs spills aren't spilling */
293 conf
->scratch_bytes_per_wave
= G_00B860_WAVESIZE(wavesize
) * 256 * 4;