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>
27 #include "radeon_elf_util.h"
28 #include "r600_pipe_common.h"
30 #include "util/u_memory.h"
36 void radeon_elf_read(const char *elf_data
, unsigned elf_size
,
37 struct radeon_shader_binary
*binary
,
42 Elf_Scn
*section
= NULL
;
43 size_t section_str_index
;
45 /* One of the libelf implementations
46 * (http://www.mr511.de/software/english.htm) requires calling
47 * elf_version() before elf_memory().
49 elf_version(EV_CURRENT
);
50 elf_buffer
= MALLOC(elf_size
);
51 memcpy(elf_buffer
, elf_data
, elf_size
);
53 elf
= elf_memory(elf_buffer
, elf_size
);
55 elf_getshdrstrndx(elf
, §ion_str_index
);
56 binary
->disassembled
= 0;
58 while ((section
= elf_nextscn(elf
, section
))) {
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");
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 } else if (!strncmp(name
, ".rodata", 7)) {
84 section_data
= elf_getdata(section
, section_data
);
85 binary
->rodata_size
= section_data
->d_size
;
86 binary
->rodata
= MALLOC(binary
->rodata_size
* sizeof(unsigned char));
87 memcpy(binary
->rodata
, section_data
->d_buf
, binary
->rodata_size
);