radeon_elf_util.c revision 6974eb907600b9d0176d3158ff0fd30ac3e56a55
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 36void radeon_elf_read(const char *elf_data, unsigned elf_size, 37 struct radeon_shader_binary *binary, 38 unsigned debug) 39{ 40 char *elf_buffer; 41 Elf *elf; 42 Elf_Scn *section = NULL; 43 size_t section_str_index; 44 45 /* One of the libelf implementations 46 * (http://www.mr511.de/software/english.htm) requires calling 47 * elf_version() before elf_memory(). 48 */ 49 elf_version(EV_CURRENT); 50 elf_buffer = MALLOC(elf_size); 51 memcpy(elf_buffer, elf_data, elf_size); 52 53 elf = elf_memory(elf_buffer, elf_size); 54 55 elf_getshdrstrndx(elf, §ion_str_index); 56 binary->disassembled = 0; 57 58 while ((section = elf_nextscn(elf, section))) { 59 const char *name; 60 Elf_Data *section_data = NULL; 61 GElf_Shdr section_header; 62 if (gelf_getshdr(section, §ion_header) != §ion_header) { 63 fprintf(stderr, "Failed to read ELF section header\n"); 64 return; 65 } 66 name = elf_strptr(elf, section_str_index, section_header.sh_name); 67 if (!strcmp(name, ".text")) { 68 section_data = elf_getdata(section, section_data); 69 binary->code_size = section_data->d_size; 70 binary->code = MALLOC(binary->code_size * sizeof(unsigned char)); 71 memcpy(binary->code, section_data->d_buf, binary->code_size); 72 } else if (!strcmp(name, ".AMDGPU.config")) { 73 section_data = elf_getdata(section, section_data); 74 binary->config_size = section_data->d_size; 75 binary->config = MALLOC(binary->config_size * sizeof(unsigned char)); 76 memcpy(binary->config, section_data->d_buf, binary->config_size); 77 } else if (debug && !strcmp(name, ".AMDGPU.disasm")) { 78 binary->disassembled = 1; 79 section_data = elf_getdata(section, section_data); 80 fprintf(stderr, "\nShader Disassembly:\n\n"); 81 fprintf(stderr, "%.*s\n", (int)section_data->d_size, 82 (char *)section_data->d_buf); 83 } 84 } 85 86 if (elf){ 87 elf_end(elf); 88 } 89 FREE(elf_buffer); 90} 91