88 #define DEBUG_LINE_SECTION_NAME ".debug_line" 90 #define DEBUG_INFO_SECTION_NAME ".debug_info" 92 #define CASESTR(n) case n: return #n 93 #define section_index(n) (n-1) 95 #define DEBUG_CUBIN_RELOCATION 0 129 if (sections->size() < sindex)
return 0;
132 if (!gelf_getshdr((*sections)[sindex], &shdr))
return 0;
134 return shdr.sh_offset;
138 #if DEBUG_CUBIN_RELOCATION 145 switch(GELF_ST_BIND(s->st_info)){
149 default:
return "UNKNOWN";
161 uint64_t *addr64 = (uint64_t *) addr;
163 }
else if (rel_type ==
R_NV_32) {
164 uint32_t *addr32 = (uint32_t *) addr;
181 unsigned sym_index = GELF_R_SYM(rel->r_info);
184 unsigned rel_type = GELF_R_TYPE(rel->r_info);
187 unsigned sym_value = (*symbol_values)[sym_index];
190 void *
addr = (
void *) (line_map + rel->r_offset);
207 unsigned sym_index = GELF_R_SYM(rela->r_info);
210 unsigned rel_type = GELF_R_TYPE(rela->r_info);
213 unsigned sym_value = (*symbol_values)[sym_index];
216 void *
addr = (
unsigned long *) (debug_info + rela->r_offset);
230 Elf_Data *relocations_data
236 for (
int i = 0; i < n_relocations; i++) {
238 GElf_Rel *rel = gelf_getrel(relocations_data, i, &rel_v);
256 GElf_Ehdr *ehdr = gelf_getehdr(elf, &ehdr_v);
258 unsigned line_map_scn_index;
259 char *line_map =
NULL;
265 for (
auto si = sections->begin(); si != sections->end(); si++, index++) {
268 if (!gelf_getshdr(scn, &shdr))
continue;
269 if (shdr.sh_type == SHT_PROGBITS) {
270 const char *section_name = elf_strptr(elf, ehdr->e_shstrndx, shdr.sh_name);
274 line_map_scn_index = index;
278 line_map = cubin_ptr + shdr.sh_offset;
291 for (
auto si = sections->begin(); si != sections->end(); si++, index++) {
294 if (!gelf_getshdr(scn, &shdr))
continue;
295 if (shdr.sh_type == SHT_REL) {
298 #if DEBUG_CUBIN_RELOCATION 299 std::cout <<
"line map relocations section name: " 300 << elf_strptr(elf, ehdr->e_shstrndx, shdr.sh_name)
309 if (shdr.sh_entsize > 0) {
310 int n_relocations = shdr.sh_size / shdr.sh_entsize;
311 if (n_relocations > 0) {
312 Elf_Data *relocations_data = elf_getdata(scn,
NULL);
314 n_relocations, relocations_data);
332 Elf_Data *relocations_data
338 for (
int i = 0; i < n_relocations; i++) {
340 GElf_Rela *rela = gelf_getrela(relocations_data, i, &rela_v);
358 GElf_Ehdr *ehdr = gelf_getehdr(elf, &ehdr_v);
360 unsigned debug_info_scn_index;
361 char *debug_info =
NULL;
367 for (
auto si = sections->begin(); si != sections->end(); si++, index++) {
370 if (!gelf_getshdr(scn, &shdr))
continue;
371 if (shdr.sh_type == SHT_PROGBITS) {
372 const char *section_name = elf_strptr(elf, ehdr->e_shstrndx, shdr.sh_name);
376 debug_info_scn_index = index;
380 debug_info = cubin_ptr + shdr.sh_offset;
393 for (
auto si = sections->begin(); si != sections->end(); si++, index++) {
396 if (!gelf_getshdr(scn, &shdr))
continue;
397 if (shdr.sh_type == SHT_RELA) {
400 #if DEBUG_CUBIN_RELOCATION 401 std::cout <<
"debug info relocations section name: " 402 << elf_strptr(elf, ehdr->e_shstrndx, shdr.sh_name)
411 if (shdr.sh_entsize > 0) {
412 int n_relocations = shdr.sh_size / shdr.sh_entsize;
413 if (n_relocations > 0) {
414 Elf_Data *relocations_data = elf_getdata(scn,
NULL);
416 n_relocations, relocations_data);
440 assert (shdr->sh_type == SHT_SYMTAB);
441 if (shdr->sh_entsize > 0) {
442 nsymbols = shdr->sh_size / shdr->sh_entsize;
444 if (nsymbols <= 0)
return NULL;
446 Elf_Data *datap = elf_getdata(scn,
NULL);
449 for (
int i = 0; i < nsymbols; i++) {
451 GElf_Sym *symp = gelf_getsym(datap, i, &sym);
453 int symtype = GELF_ST_TYPE(sym.st_info);
454 if (sym.st_shndx == SHN_UNDEF)
continue;
459 #if DEBUG_CUBIN_RELOCATION 460 Elf64_Addr addr_signed = sym.st_value;
461 std::cout <<
"elf symbol " << elf_strptr(elf, shdr->sh_link, sym.st_name)
462 <<
" value=0x" << std::hex << addr_signed
463 <<
" binding=" << binding_name(&sym)
465 <<
" section offset=0x" << std::hex << s_offset
470 sym.st_value = (Elf64_Addr) s_offset;
471 gelf_update_sym(datap, i, &sym);
472 (*symbol_values)[i] = s_offset;
479 return symbol_values;
492 GElf_Ehdr *ehdr = gelf_getehdr(elf, &ehdr_v);
494 for (
auto si = sections->begin(); si != sections->end(); si++) {
497 if (!gelf_getshdr(scn, &shdr))
continue;
498 if (shdr.sh_type == SHT_SYMTAB) {
499 #if DEBUG_CUBIN_RELOCATION 500 std::cout <<
"relocating symbols in section " << elf_strptr(elf, ehdr->e_shstrndx, shdr.sh_name)
508 return symbol_values;
524 GElf_Ehdr *ehdr = gelf_getehdr(elf, &ehdr_v);
526 for (
auto si = sections->begin(); si != sections->end(); si++) {
529 if (!gelf_getshdr(scn, &shdr))
continue;
530 if (shdr.sh_type == SHT_PROGBITS) {
531 #if DEBUG_CUBIN_RELOCATION 532 std::cout <<
"relocating section " << elf_strptr(elf, ehdr->e_shstrndx, shdr.sh_name)
537 shdr.sh_addr = shdr.sh_offset;
538 gelf_update_shdr(scn, &shdr);
557 bool success =
false;
566 delete symbol_values;
static void relocateDebugInfo(char *cubin_ptr, Elf *elf, Elf_SectionVector *sections, Elf_SymbolVector *symbol_values)
#define DEBUG_LINE_SECTION_NAME
bool relocateCubin(char *cubin_ptr, Elf *cubin_elf)
static void relocateProgramDataSegments(Elf *elf, Elf_SectionVector *sections)
static void applyRelocation(void *addr, unsigned rel_type, uint64_t rel_value)
static void applyDebugInfoRelocations(Elf_SymbolVector *symbol_values, char *debug_info, int n_relocations, Elf_Data *relocations_data)
#define DEBUG_INFO_SECTION_NAME
std::vector< Elf64_Addr > Elf_SymbolVector
static Elf_SymbolVector * relocateSymbolsHelper(Elf *elf, GElf_Ehdr *ehdr, GElf_Shdr *shdr, Elf_SectionVector *sections, Elf_Scn *scn)
static void applyLineMapRelocations(Elf_SymbolVector *symbol_values, char *line_map, int n_relocations, Elf_Data *relocations_data)
static void applyRELrelocation(char *line_map, Elf_SymbolVector *symbol_values, GElf_Rel *rel)
static Elf_SymbolVector * relocateSymbols(Elf *elf, Elf_SectionVector *sections)
static size_t sectionOffset(Elf_SectionVector *sections, unsigned sindex)
ElfSectionVector * elfGetSectionVector(Elf *elf)
static void applyRELArelocation(char *debug_info, Elf_SymbolVector *symbol_values, GElf_Rela *rela)
static void relocateLineMap(char *cubin_ptr, Elf *elf, Elf_SectionVector *sections, Elf_SymbolVector *symbol_values)
std::vector< Elf_Scn * > Elf_SectionVector