radeon: Teach radeon_elf_read() how to parse reloc information v3
[mesa.git] / src / gallium / drivers / radeon / radeon_elf_util.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 */
26
27 #include "radeon_elf_util.h"
28 #include "r600_pipe_common.h"
29
30 #include "util/u_memory.h"
31
32 #include <gelf.h>
33 #include <libelf.h>
34 #include <stdio.h>
35
36 static void parse_symbol_table(Elf_Data *symbol_table_data,
37 const GElf_Shdr *symbol_table_header,
38 struct radeon_shader_binary *binary)
39 {
40 GElf_Sym symbol;
41 unsigned i = 0;
42 unsigned symbol_count =
43 symbol_table_header->sh_size / symbol_table_header->sh_entsize;
44
45 /* We are over allocating this list, because symbol_count gives the
46 * total number of symbols, and we will only be filling the list
47 * with offsets of global symbols. The memory savings from
48 * allocating the correct size of this list will be small, and
49 * I don't think it is worth the cost of pre-computing the number
50 * of global symbols.
51 */
52 binary->global_symbol_offsets = CALLOC(symbol_count, sizeof(uint64_t));
53
54 while (gelf_getsym(symbol_table_data, i++, &symbol)) {
55 unsigned i;
56 if (GELF_ST_BIND(symbol.st_info) != STB_GLOBAL ||
57 symbol.st_shndx == 0 /* Undefined symbol */) {
58 continue;
59 }
60
61 binary->global_symbol_offsets[binary->global_symbol_count] =
62 symbol.st_value;
63
64 /* Sort the list using bubble sort. This list will usually
65 * be small. */
66 for (i = binary->global_symbol_count; i > 0; --i) {
67 uint64_t lhs = binary->global_symbol_offsets[i - 1];
68 uint64_t rhs = binary->global_symbol_offsets[i];
69 if (lhs < rhs) {
70 break;
71 }
72 binary->global_symbol_offsets[i] = lhs;
73 binary->global_symbol_offsets[i - 1] = rhs;
74 }
75 ++binary->global_symbol_count;
76 }
77 }
78
79 static void parse_relocs(Elf *elf, Elf_Data *relocs, Elf_Data *symbols,
80 unsigned symbol_sh_link,
81 struct radeon_shader_binary *binary)
82 {
83 unsigned i;
84
85 if (!relocs || !symbols || !binary->reloc_count) {
86 return;
87 }
88 binary->relocs = CALLOC(binary->reloc_count,
89 sizeof(struct radeon_shader_reloc));
90 for (i = 0; i < binary->reloc_count; i++) {
91 GElf_Sym symbol;
92 GElf_Rel rel;
93 char *symbol_name;
94 struct radeon_shader_reloc *reloc = &binary->relocs[i];
95
96 gelf_getrel(relocs, i, &rel);
97 gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &symbol);
98 symbol_name = elf_strptr(elf, symbol_sh_link, symbol.st_name);
99
100 reloc->offset = rel.r_offset;
101 reloc->name = strdup(symbol_name);
102 }
103 }
104
105 void radeon_elf_read(const char *elf_data, unsigned elf_size,
106 struct radeon_shader_binary *binary,
107 unsigned debug)
108 {
109 char *elf_buffer;
110 Elf *elf;
111 Elf_Scn *section = NULL;
112 Elf_Data *symbols = NULL, *relocs = NULL;
113 size_t section_str_index;
114 unsigned symbol_sh_link;
115
116 /* One of the libelf implementations
117 * (http://www.mr511.de/software/english.htm) requires calling
118 * elf_version() before elf_memory().
119 */
120 elf_version(EV_CURRENT);
121 elf_buffer = MALLOC(elf_size);
122 memcpy(elf_buffer, elf_data, elf_size);
123
124 elf = elf_memory(elf_buffer, elf_size);
125
126 elf_getshdrstrndx(elf, &section_str_index);
127 binary->disassembled = 0;
128
129 while ((section = elf_nextscn(elf, section))) {
130 const char *name;
131 Elf_Data *section_data = NULL;
132 GElf_Shdr section_header;
133 if (gelf_getshdr(section, &section_header) != &section_header) {
134 fprintf(stderr, "Failed to read ELF section header\n");
135 return;
136 }
137 name = elf_strptr(elf, section_str_index, section_header.sh_name);
138 if (!strcmp(name, ".text")) {
139 section_data = elf_getdata(section, section_data);
140 binary->code_size = section_data->d_size;
141 binary->code = MALLOC(binary->code_size * sizeof(unsigned char));
142 memcpy(binary->code, section_data->d_buf, binary->code_size);
143 } else if (!strcmp(name, ".AMDGPU.config")) {
144 section_data = elf_getdata(section, section_data);
145 binary->config_size = section_data->d_size;
146 binary->config = MALLOC(binary->config_size * sizeof(unsigned char));
147 memcpy(binary->config, section_data->d_buf, binary->config_size);
148 } else if (debug && !strcmp(name, ".AMDGPU.disasm")) {
149 binary->disassembled = 1;
150 section_data = elf_getdata(section, section_data);
151 fprintf(stderr, "\nShader Disassembly:\n\n");
152 fprintf(stderr, "%.*s\n", (int)section_data->d_size,
153 (char *)section_data->d_buf);
154 } else if (!strncmp(name, ".rodata", 7)) {
155 section_data = elf_getdata(section, section_data);
156 binary->rodata_size = section_data->d_size;
157 binary->rodata = MALLOC(binary->rodata_size * sizeof(unsigned char));
158 memcpy(binary->rodata, section_data->d_buf, binary->rodata_size);
159 } else if (!strncmp(name, ".symtab", 7)) {
160 symbols = elf_getdata(section, section_data);
161 symbol_sh_link = section_header.sh_link;
162 parse_symbol_table(symbols, &section_header, binary);
163 } else if (!strcmp(name, ".rel.text")) {
164 relocs = elf_getdata(section, section_data);
165 binary->reloc_count = section_header.sh_size /
166 section_header.sh_entsize;
167 }
168 }
169
170 parse_relocs(elf, relocs, symbols, symbol_sh_link, binary);
171
172 if (elf){
173 elf_end(elf);
174 }
175 FREE(elf_buffer);
176
177 /* Cache the config size per symbol */
178 if (binary->global_symbol_count) {
179 binary->config_size_per_symbol =
180 binary->config_size / binary->global_symbol_count;
181 } else {
182 binary->global_symbol_count = 1;
183 binary->config_size_per_symbol = binary->config_size;
184 }
185 }
186
187 const unsigned char *radeon_shader_binary_config_start(
188 const struct radeon_shader_binary *binary,
189 uint64_t symbol_offset)
190 {
191 unsigned i;
192 for (i = 0; i < binary->global_symbol_count; ++i) {
193 if (binary->global_symbol_offsets[i] == symbol_offset) {
194 unsigned offset = i * binary->config_size_per_symbol;
195 return binary->config + offset;
196 }
197 }
198 return binary->config;
199 }
200
201 void radeon_shader_binary_free_relocs(struct radeon_shader_reloc *relocs,
202 unsigned reloc_count)
203 {
204 unsigned i;
205 for (i = 0; i < reloc_count; i++) {
206 FREE(relocs[i].name);
207 }
208 FREE(relocs);
209 }
210
211 void radeon_shader_binary_free_members(struct radeon_shader_binary *binary,
212 unsigned free_relocs)
213 {
214 FREE(binary->code);
215 FREE(binary->config);
216 FREE(binary->rodata);
217
218 if (free_relocs) {
219 radeon_shader_binary_free_relocs(binary->relocs,
220 binary->reloc_count);
221 }
222 }