radeon: Add a helper function for freeing members of radeon_shader_binary
[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 continue;
58 }
59
60 binary->global_symbol_offsets[binary->global_symbol_count] =
61 symbol.st_value;
62
63 /* Sort the list using bubble sort. This list will usually
64 * be small. */
65 for (i = binary->global_symbol_count; i > 0; --i) {
66 uint64_t lhs = binary->global_symbol_offsets[i - 1];
67 uint64_t rhs = binary->global_symbol_offsets[i];
68 if (lhs < rhs) {
69 break;
70 }
71 binary->global_symbol_offsets[i] = lhs;
72 binary->global_symbol_offsets[i - 1] = rhs;
73 }
74 ++binary->global_symbol_count;
75 }
76 }
77
78 void radeon_elf_read(const char *elf_data, unsigned elf_size,
79 struct radeon_shader_binary *binary,
80 unsigned debug)
81 {
82 char *elf_buffer;
83 Elf *elf;
84 Elf_Scn *section = NULL;
85 size_t section_str_index;
86
87 /* One of the libelf implementations
88 * (http://www.mr511.de/software/english.htm) requires calling
89 * elf_version() before elf_memory().
90 */
91 elf_version(EV_CURRENT);
92 elf_buffer = MALLOC(elf_size);
93 memcpy(elf_buffer, elf_data, elf_size);
94
95 elf = elf_memory(elf_buffer, elf_size);
96
97 elf_getshdrstrndx(elf, &section_str_index);
98 binary->disassembled = 0;
99
100 while ((section = elf_nextscn(elf, section))) {
101 const char *name;
102 Elf_Data *section_data = NULL;
103 GElf_Shdr section_header;
104 if (gelf_getshdr(section, &section_header) != &section_header) {
105 fprintf(stderr, "Failed to read ELF section header\n");
106 return;
107 }
108 name = elf_strptr(elf, section_str_index, section_header.sh_name);
109 if (!strcmp(name, ".text")) {
110 section_data = elf_getdata(section, section_data);
111 binary->code_size = section_data->d_size;
112 binary->code = MALLOC(binary->code_size * sizeof(unsigned char));
113 memcpy(binary->code, section_data->d_buf, binary->code_size);
114 } else if (!strcmp(name, ".AMDGPU.config")) {
115 section_data = elf_getdata(section, section_data);
116 binary->config_size = section_data->d_size;
117 binary->config = MALLOC(binary->config_size * sizeof(unsigned char));
118 memcpy(binary->config, section_data->d_buf, binary->config_size);
119 } else if (debug && !strcmp(name, ".AMDGPU.disasm")) {
120 binary->disassembled = 1;
121 section_data = elf_getdata(section, section_data);
122 fprintf(stderr, "\nShader Disassembly:\n\n");
123 fprintf(stderr, "%.*s\n", (int)section_data->d_size,
124 (char *)section_data->d_buf);
125 } else if (!strncmp(name, ".rodata", 7)) {
126 section_data = elf_getdata(section, section_data);
127 binary->rodata_size = section_data->d_size;
128 binary->rodata = MALLOC(binary->rodata_size * sizeof(unsigned char));
129 memcpy(binary->rodata, section_data->d_buf, binary->rodata_size);
130 } else if (!strncmp(name, ".symtab", 7)) {
131 section_data = elf_getdata(section, section_data);
132 parse_symbol_table(section_data, &section_header, binary);
133 }
134 }
135
136 if (elf){
137 elf_end(elf);
138 }
139 FREE(elf_buffer);
140
141 /* Cache the config size per symbol */
142 if (binary->global_symbol_count) {
143 binary->config_size_per_symbol =
144 binary->config_size / binary->global_symbol_count;
145 } else {
146 binary->global_symbol_count = 1;
147 binary->config_size_per_symbol = binary->config_size;
148 }
149 }
150
151 const unsigned char *radeon_shader_binary_config_start(
152 const struct radeon_shader_binary *binary,
153 uint64_t symbol_offset)
154 {
155 unsigned i;
156 for (i = 0; i < binary->global_symbol_count; ++i) {
157 if (binary->global_symbol_offsets[i] == symbol_offset) {
158 unsigned offset = i * binary->config_size_per_symbol;
159 return binary->config + offset;
160 }
161 }
162 return binary->config;
163 }
164
165 void radeon_shader_binary_free_members(struct radeon_shader_binary *binary) {
166 FREE(binary->code);
167 FREE(binary->config);
168 FREE(binary->rodata);
169 }