|
23 | 23 | #pragma once |
24 | 24 |
|
25 | 25 | #include <amd_comgr/amd_comgr.h> |
26 | | -#include <fcntl.h> |
27 | 26 | #include <hsa/amd_hsa_elf.h> |
| 27 | + |
| 28 | +#include <fcntl.h> |
28 | 29 | #include <sys/mman.h> |
29 | 30 | #include <sys/stat.h> |
30 | 31 | #include <sys/types.h> |
31 | 32 | #include <unistd.h> |
32 | 33 |
|
| 34 | +#include <algorithm> |
33 | 35 | #include <cstring> |
34 | 36 | #include <fstream> |
35 | 37 | #include <iostream> |
|
61 | 63 | return AMD_COMGR_STATUS_ERROR; \ |
62 | 64 | } |
63 | 65 |
|
64 | | -#define CHECK_VA2FO(x, msg) \ |
65 | | - if(!(x)) \ |
66 | | - { \ |
67 | | - std::cerr << __FILE__ << ' ' << __LINE__ << ' ' << msg << "\n"; \ |
68 | | - return std::nullopt; \ |
69 | | - } |
70 | | - |
71 | 66 | namespace rocprofiler |
72 | 67 | { |
73 | 68 | namespace sdk |
@@ -285,54 +280,26 @@ class DisassemblyInstance |
285 | 280 | instance.last_instruction = instruction; |
286 | 281 | } |
287 | 282 |
|
288 | | - std::optional<uint64_t> va2fo(uint64_t va) |
| 283 | + std::optional<uint64_t> va2fo(uint64_t va) const |
289 | 284 | { |
290 | | - CHECK_VA2FO(buffer.size() > sizeof(Elf64_Ehdr), "buffer is not large enough"); |
291 | | - |
292 | | - uint8_t* e_ident = (uint8_t*) buffer.data(); |
293 | | - CHECK_VA2FO(e_ident, "e_ident is nullptr"); |
294 | | - |
295 | | - CHECK_VA2FO(e_ident[EI_MAG0] == ELFMAG0 || e_ident[EI_MAG1] == ELFMAG1 || |
296 | | - e_ident[EI_MAG2] == ELFMAG2 || e_ident[EI_MAG3] == ELFMAG3, |
297 | | - "unexpected ei_mag"); |
| 285 | + uint64_t offset = 0; |
| 286 | + uint64_t slicesize = 0; |
| 287 | + bool nobits = false; |
298 | 288 |
|
299 | | - CHECK_VA2FO(e_ident[EI_CLASS] == ELFCLASS64, "unexpected ei_class"); |
300 | | - CHECK_VA2FO(e_ident[EI_DATA] == ELFDATA2LSB, "unexpected ei_data"); |
301 | | - CHECK_VA2FO(e_ident[EI_VERSION] == EV_CURRENT, "unexpected ei_version"); |
302 | | - CHECK_VA2FO(e_ident[EI_OSABI] == 64, "unexpected ei_osabi"); // ELFOSABI_AMDGPU_HSA |
| 289 | + auto status = amd_comgr_map_elf_virtual_address_to_code_object_offset( |
| 290 | + data, va, &offset, &slicesize, &nobits); |
303 | 291 |
|
304 | | - CHECK_VA2FO(e_ident[EI_ABIVERSION] == 2 || // ELFABIVERSION_AMDGPU_HSA_V4 |
305 | | - e_ident[EI_ABIVERSION] == 3 || // ELFABIVERSION_AMDGPU_HSA_V5 |
306 | | - e_ident[EI_ABIVERSION] == 4, // ELFABIVERSION_AMDGPU_HSA_V6 |
307 | | - "unexpected ei_abiversion"); |
308 | | - |
309 | | - Elf64_Ehdr* ehdr = (Elf64_Ehdr*) buffer.data(); |
310 | | - CHECK_VA2FO(ehdr, "ehdr is nullptr"); |
311 | | - CHECK_VA2FO(ehdr->e_type == ET_DYN, "unexpected e_type"); |
312 | | - CHECK_VA2FO(ehdr->e_machine == ELF::EM_AMDGPU, "unexpected e_machine"); |
313 | | - CHECK_VA2FO(ehdr->e_phoff != 0, "unexpected e_phoff"); |
314 | | - |
315 | | - CHECK_VA2FO(buffer.size() > ehdr->e_phoff + sizeof(Elf64_Phdr), |
316 | | - "buffer is not large enough"); |
317 | | - |
318 | | - Elf64_Phdr* phdr = (Elf64_Phdr*) ((uint8_t*) buffer.data() + ehdr->e_phoff); |
319 | | - CHECK_VA2FO(phdr, "phdr is nullptr"); |
320 | | - |
321 | | - for(uint16_t i = 0; i < ehdr->e_phnum; ++i) |
322 | | - { |
323 | | - if(phdr[i].p_type != PT_LOAD) continue; |
324 | | - if(va < phdr[i].p_vaddr || va >= (phdr[i].p_vaddr + phdr[i].p_memsz)) continue; |
325 | | - |
326 | | - return va + phdr[i].p_offset - phdr[i].p_vaddr; |
327 | | - } |
328 | | - return std::nullopt; |
| 292 | + if(status != AMD_COMGR_STATUS_SUCCESS || nobits) |
| 293 | + return std::nullopt; |
| 294 | + else |
| 295 | + return offset; |
329 | 296 | } |
330 | 297 |
|
331 | | - std::vector<char> buffer; |
332 | | - std::string last_instruction; |
333 | | - amd_comgr_disassembly_info_t info; |
334 | | - amd_comgr_data_t data; |
335 | | - std::map<uint64_t, SymbolInfo> symbol_map; |
| 298 | + std::vector<char> buffer{}; |
| 299 | + std::string last_instruction{}; |
| 300 | + amd_comgr_disassembly_info_t info{}; |
| 301 | + amd_comgr_data_t data{}; |
| 302 | + std::map<uint64_t, SymbolInfo> symbol_map{}; |
336 | 303 | }; |
337 | 304 |
|
338 | 305 | } // namespace disassembly |
|
0 commit comments