amd/common/gfx10: pad shader buffers for instruction prefetch
[mesa.git] / src / amd / common / ac_rtld.c
1 /*
2 * Copyright 2014-2019 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
24 #include "ac_rtld.h"
25
26 #include <gelf.h>
27 #include <libelf.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "ac_binary.h"
34 #include "ac_gpu_info.h"
35 #include "util/u_dynarray.h"
36 #include "util/u_math.h"
37
38 // Old distributions may not have this enum constant
39 #define MY_EM_AMDGPU 224
40
41 #ifndef STT_AMDGPU_LDS
42 #define STT_AMDGPU_LDS 13 // this is deprecated -- remove
43 #endif
44
45 #ifndef SHN_AMDGPU_LDS
46 #define SHN_AMDGPU_LDS 0xff00
47 #endif
48
49 #ifndef R_AMDGPU_NONE
50 #define R_AMDGPU_NONE 0
51 #define R_AMDGPU_ABS32_LO 1
52 #define R_AMDGPU_ABS32_HI 2
53 #define R_AMDGPU_ABS64 3
54 #define R_AMDGPU_REL32 4
55 #define R_AMDGPU_REL64 5
56 #define R_AMDGPU_ABS32 6
57 #define R_AMDGPU_GOTPCREL 7
58 #define R_AMDGPU_GOTPCREL32_LO 8
59 #define R_AMDGPU_GOTPCREL32_HI 9
60 #define R_AMDGPU_REL32_LO 10
61 #define R_AMDGPU_REL32_HI 11
62 #define R_AMDGPU_RELATIVE64 13
63 #endif
64
65 /* For the UMR disassembler. */
66 #define DEBUGGER_END_OF_CODE_MARKER 0xbf9f0000 /* invalid instruction */
67 #define DEBUGGER_NUM_MARKERS 5
68
69 struct ac_rtld_section {
70 bool is_rx : 1;
71 bool is_pasted_text : 1;
72 uint64_t offset;
73 const char *name;
74 };
75
76 struct ac_rtld_part {
77 Elf *elf;
78 struct ac_rtld_section *sections;
79 unsigned num_sections;
80 };
81
82 static void report_erroraf(const char *fmt, va_list va)
83 {
84 char *msg;
85 int ret = asprintf(&msg, fmt, va);
86 if (ret < 0)
87 msg = "(asprintf failed)";
88
89 fprintf(stderr, "ac_rtld error: %s\n", msg);
90
91 if (ret >= 0)
92 free(msg);
93 }
94
95 static void report_errorf(const char *fmt, ...) PRINTFLIKE(1, 2);
96
97 static void report_errorf(const char *fmt, ...)
98 {
99 va_list va;
100 va_start(va, fmt);
101 report_erroraf(fmt, va);
102 va_end(va);
103 }
104
105 static void report_elf_errorf(const char *fmt, ...) PRINTFLIKE(1, 2);
106
107 static void report_elf_errorf(const char *fmt, ...)
108 {
109 va_list va;
110 va_start(va, fmt);
111 report_erroraf(fmt, va);
112 va_end(va);
113
114 fprintf(stderr, "ELF error: %s\n", elf_errmsg(elf_errno()));
115 }
116
117 /**
118 * Find a symbol in a dynarray of struct ac_rtld_symbol by \p name and shader
119 * \p part_idx.
120 */
121 static const struct ac_rtld_symbol *find_symbol(const struct util_dynarray *symbols,
122 const char *name, unsigned part_idx)
123 {
124 util_dynarray_foreach(symbols, struct ac_rtld_symbol, symbol) {
125 if ((symbol->part_idx == ~0u || symbol->part_idx == part_idx) &&
126 !strcmp(name, symbol->name))
127 return symbol;
128 }
129 return 0;
130 }
131
132 static int compare_symbol_by_align(const void *lhsp, const void *rhsp)
133 {
134 const struct ac_rtld_symbol *lhs = lhsp;
135 const struct ac_rtld_symbol *rhs = rhsp;
136 if (rhs->align > lhs->align)
137 return 1;
138 if (rhs->align < lhs->align)
139 return -1;
140 return 0;
141 }
142
143 /**
144 * Sort the given symbol list by decreasing alignment and assign offsets.
145 */
146 static bool layout_symbols(struct ac_rtld_symbol *symbols, unsigned num_symbols,
147 uint64_t *ptotal_size)
148 {
149 qsort(symbols, num_symbols, sizeof(*symbols), compare_symbol_by_align);
150
151 uint64_t total_size = *ptotal_size;
152
153 for (unsigned i = 0; i < num_symbols; ++i) {
154 struct ac_rtld_symbol *s = &symbols[i];
155 assert(util_is_power_of_two_nonzero(s->align));
156
157 total_size = align64(total_size, s->align);
158 s->offset = total_size;
159
160 if (total_size + s->size < total_size) {
161 report_errorf("%s: size overflow", __FUNCTION__);
162 return false;
163 }
164
165 total_size += s->size;
166 }
167
168 *ptotal_size = total_size;
169 return true;
170 }
171
172 /**
173 * Read LDS symbols from the given \p section of the ELF of \p part and append
174 * them to the LDS symbols list.
175 *
176 * Shared LDS symbols are filtered out.
177 */
178 static bool read_private_lds_symbols(struct ac_rtld_binary *binary,
179 unsigned part_idx,
180 Elf_Scn *section,
181 uint32_t *lds_end_align)
182 {
183 #define report_if(cond) \
184 do { \
185 if ((cond)) { \
186 report_errorf(#cond); \
187 return false; \
188 } \
189 } while (false)
190 #define report_elf_if(cond) \
191 do { \
192 if ((cond)) { \
193 report_elf_errorf(#cond); \
194 return false; \
195 } \
196 } while (false)
197
198 struct ac_rtld_part *part = &binary->parts[part_idx];
199 Elf64_Shdr *shdr = elf64_getshdr(section);
200 uint32_t strtabidx = shdr->sh_link;
201 Elf_Data *symbols_data = elf_getdata(section, NULL);
202 report_elf_if(!symbols_data);
203
204 const Elf64_Sym *symbol = symbols_data->d_buf;
205 size_t num_symbols = symbols_data->d_size / sizeof(Elf64_Sym);
206
207 for (size_t j = 0; j < num_symbols; ++j, ++symbol) {
208 struct ac_rtld_symbol s = {};
209
210 if (ELF64_ST_TYPE(symbol->st_info) == STT_AMDGPU_LDS) {
211 /* old-style LDS symbols from initial prototype -- remove eventually */
212 s.align = MIN2(1u << (symbol->st_other >> 3), 1u << 16);
213 } else if (symbol->st_shndx == SHN_AMDGPU_LDS) {
214 s.align = MIN2(symbol->st_value, 1u << 16);
215 report_if(!util_is_power_of_two_nonzero(s.align));
216 } else
217 continue;
218
219 report_if(symbol->st_size > 1u << 29);
220
221 s.name = elf_strptr(part->elf, strtabidx, symbol->st_name);
222 s.size = symbol->st_size;
223 s.part_idx = part_idx;
224
225 if (!strcmp(s.name, "__lds_end")) {
226 report_elf_if(s.size != 0);
227 *lds_end_align = MAX2(*lds_end_align, s.align);
228 continue;
229 }
230
231 const struct ac_rtld_symbol *shared =
232 find_symbol(&binary->lds_symbols, s.name, part_idx);
233 if (shared) {
234 report_elf_if(s.align > shared->align);
235 report_elf_if(s.size > shared->size);
236 continue;
237 }
238
239 util_dynarray_append(&binary->lds_symbols, struct ac_rtld_symbol, s);
240 }
241
242 return true;
243
244 #undef report_if
245 #undef report_elf_if
246 }
247
248 /**
249 * Open a binary consisting of one or more shader parts.
250 *
251 * \param binary the uninitialized struct
252 * \param i binary opening parameters
253 */
254 bool ac_rtld_open(struct ac_rtld_binary *binary,
255 struct ac_rtld_open_info i)
256 {
257 /* One of the libelf implementations
258 * (http://www.mr511.de/software/english.htm) requires calling
259 * elf_version() before elf_memory().
260 */
261 elf_version(EV_CURRENT);
262
263 memset(binary, 0, sizeof(*binary));
264 memcpy(&binary->options, &i.options, sizeof(binary->options));
265 binary->num_parts = i.num_parts;
266 binary->parts = calloc(sizeof(*binary->parts), i.num_parts);
267 if (!binary->parts)
268 return false;
269
270 uint64_t pasted_text_size = 0;
271 uint64_t rx_align = 1;
272 uint64_t rx_size = 0;
273
274 #define report_if(cond) \
275 do { \
276 if ((cond)) { \
277 report_errorf(#cond); \
278 goto fail; \
279 } \
280 } while (false)
281 #define report_elf_if(cond) \
282 do { \
283 if ((cond)) { \
284 report_elf_errorf(#cond); \
285 goto fail; \
286 } \
287 } while (false)
288
289 /* Copy and layout shared LDS symbols. */
290 if (i.num_shared_lds_symbols) {
291 if (!util_dynarray_resize(&binary->lds_symbols, struct ac_rtld_symbol,
292 i.num_shared_lds_symbols))
293 goto fail;
294
295 memcpy(binary->lds_symbols.data, i.shared_lds_symbols, binary->lds_symbols.size);
296 }
297
298 util_dynarray_foreach(&binary->lds_symbols, struct ac_rtld_symbol, symbol)
299 symbol->part_idx = ~0u;
300
301 unsigned max_lds_size = 64 * 1024;
302
303 if (i.info->chip_class == GFX6 ||
304 (i.shader_type != MESA_SHADER_COMPUTE &&
305 i.shader_type != MESA_SHADER_FRAGMENT))
306 max_lds_size = 32 * 1024;
307
308 uint64_t shared_lds_size = 0;
309 if (!layout_symbols(binary->lds_symbols.data, i.num_shared_lds_symbols, &shared_lds_size))
310 goto fail;
311
312 if (shared_lds_size > max_lds_size) {
313 fprintf(stderr, "ac_rtld error(1): too much LDS (used = %u, max = %u)\n",
314 (unsigned)shared_lds_size, max_lds_size);
315 goto fail;
316 }
317 binary->lds_size = shared_lds_size;
318
319 /* First pass over all parts: open ELFs, pre-determine the placement of
320 * sections in the memory image, and collect and layout private LDS symbols. */
321 uint32_t lds_end_align = 0;
322
323 if (binary->options.halt_at_entry)
324 pasted_text_size += 4;
325
326 for (unsigned part_idx = 0; part_idx < i.num_parts; ++part_idx) {
327 struct ac_rtld_part *part = &binary->parts[part_idx];
328 unsigned part_lds_symbols_begin =
329 util_dynarray_num_elements(&binary->lds_symbols, struct ac_rtld_symbol);
330
331 part->elf = elf_memory((char *)i.elf_ptrs[part_idx], i.elf_sizes[part_idx]);
332 report_elf_if(!part->elf);
333
334 const Elf64_Ehdr *ehdr = elf64_getehdr(part->elf);
335 report_elf_if(!ehdr);
336 report_if(ehdr->e_machine != MY_EM_AMDGPU);
337
338 size_t section_str_index;
339 size_t num_shdrs;
340 report_elf_if(elf_getshdrstrndx(part->elf, &section_str_index) < 0);
341 report_elf_if(elf_getshdrnum(part->elf, &num_shdrs) < 0);
342
343 part->num_sections = num_shdrs;
344 part->sections = calloc(sizeof(*part->sections), num_shdrs);
345 report_if(!part->sections);
346
347 Elf_Scn *section = NULL;
348 while ((section = elf_nextscn(part->elf, section))) {
349 Elf64_Shdr *shdr = elf64_getshdr(section);
350 struct ac_rtld_section *s = &part->sections[elf_ndxscn(section)];
351 s->name = elf_strptr(part->elf, section_str_index, shdr->sh_name);
352 report_elf_if(!s->name);
353
354 /* Cannot actually handle linked objects yet */
355 report_elf_if(shdr->sh_addr != 0);
356
357 /* Alignment must be 0 or a power of two */
358 report_elf_if(shdr->sh_addralign & (shdr->sh_addralign - 1));
359 uint64_t sh_align = MAX2(shdr->sh_addralign, 1);
360
361 if (shdr->sh_flags & SHF_ALLOC &&
362 shdr->sh_type != SHT_NOTE) {
363 report_if(shdr->sh_flags & SHF_WRITE);
364
365 s->is_rx = true;
366
367 if (shdr->sh_flags & SHF_EXECINSTR) {
368 report_elf_if(shdr->sh_size & 3);
369
370 if (!strcmp(s->name, ".text"))
371 s->is_pasted_text = true;
372 }
373
374 if (s->is_pasted_text) {
375 s->offset = pasted_text_size;
376 pasted_text_size += shdr->sh_size;
377 } else {
378 rx_align = align(rx_align, sh_align);
379 rx_size = align(rx_size, sh_align);
380 s->offset = rx_size;
381 rx_size += shdr->sh_size;
382 }
383 } else if (shdr->sh_type == SHT_SYMTAB) {
384 if (!read_private_lds_symbols(binary, part_idx, section, &lds_end_align))
385 goto fail;
386 }
387 }
388
389 uint64_t part_lds_size = shared_lds_size;
390 if (!layout_symbols(
391 util_dynarray_element(&binary->lds_symbols, struct ac_rtld_symbol, part_lds_symbols_begin),
392 util_dynarray_num_elements(&binary->lds_symbols, struct ac_rtld_symbol) - part_lds_symbols_begin,
393 &part_lds_size))
394 goto fail;
395 binary->lds_size = MAX2(binary->lds_size, part_lds_size);
396 }
397
398 binary->rx_end_markers = pasted_text_size;
399 pasted_text_size += 4 * DEBUGGER_NUM_MARKERS;
400
401 /* __lds_end is a special symbol that points at the end of the memory
402 * occupied by other LDS symbols. Its alignment is taken as the
403 * maximum of its alignment over all shader parts where it occurs.
404 */
405 if (lds_end_align) {
406 binary->lds_size = align(binary->lds_size, lds_end_align);
407
408 struct ac_rtld_symbol *lds_end =
409 util_dynarray_grow(&binary->lds_symbols, struct ac_rtld_symbol, 1);
410 lds_end->name = "__lds_end";
411 lds_end->size = 0;
412 lds_end->align = lds_end_align;
413 lds_end->offset = binary->lds_size;
414 lds_end->part_idx = ~0u;
415 }
416
417 if (binary->lds_size > max_lds_size) {
418 fprintf(stderr, "ac_rtld error(2): too much LDS (used = %u, max = %u)\n",
419 (unsigned)binary->lds_size, max_lds_size);
420 goto fail;
421 }
422
423 /* Second pass: Adjust offsets of non-pasted text sections. */
424 binary->rx_size = pasted_text_size;
425 binary->rx_size = align(binary->rx_size, rx_align);
426
427 for (unsigned part_idx = 0; part_idx < i.num_parts; ++part_idx) {
428 struct ac_rtld_part *part = &binary->parts[part_idx];
429 size_t num_shdrs;
430 elf_getshdrnum(part->elf, &num_shdrs);
431
432 for (unsigned j = 0; j < num_shdrs; ++j) {
433 struct ac_rtld_section *s = &part->sections[j];
434 if (s->is_rx && !s->is_pasted_text)
435 s->offset += binary->rx_size;
436 }
437 }
438
439 binary->rx_size += rx_size;
440
441 if (i.info->chip_class >= GFX10) {
442 /* In gfx10, the SQ fetches up to 3 cache lines of 16 dwords
443 * ahead of the PC, configurable by SH_MEM_CONFIG and
444 * S_INST_PREFETCH. This can cause two issues:
445 *
446 * (1) Crossing a page boundary to an unmapped page. The logic
447 * does not distinguish between a required fetch and a "mere"
448 * prefetch and will fault.
449 *
450 * (2) Prefetching instructions that will be changed for a
451 * different shader.
452 *
453 * (2) is not currently an issue because we flush the I$ at IB
454 * boundaries, but (1) needs to be addressed. Due to buffer
455 * suballocation, we just play it safe.
456 */
457 binary->rx_size = align(binary->rx_size + 3 * 64, 64);
458 }
459
460 return true;
461
462 #undef report_if
463 #undef report_elf_if
464
465 fail:
466 ac_rtld_close(binary);
467 return false;
468 }
469
470 void ac_rtld_close(struct ac_rtld_binary *binary)
471 {
472 for (unsigned i = 0; i < binary->num_parts; ++i) {
473 struct ac_rtld_part *part = &binary->parts[i];
474 free(part->sections);
475 elf_end(part->elf);
476 }
477
478 util_dynarray_fini(&binary->lds_symbols);
479 free(binary->parts);
480 binary->parts = NULL;
481 binary->num_parts = 0;
482 }
483
484 static bool get_section_by_name(struct ac_rtld_part *part, const char *name,
485 const char **data, size_t *nbytes)
486 {
487 for (unsigned i = 0; i < part->num_sections; ++i) {
488 struct ac_rtld_section *s = &part->sections[i];
489 if (s->name && !strcmp(name, s->name)) {
490 Elf_Scn *target_scn = elf_getscn(part->elf, i);
491 Elf_Data *target_data = elf_getdata(target_scn, NULL);
492 if (!target_data) {
493 report_elf_errorf("ac_rtld: get_section_by_name: elf_getdata");
494 return false;
495 }
496
497 *data = target_data->d_buf;
498 *nbytes = target_data->d_size;
499 return true;
500 }
501 }
502 return false;
503 }
504
505 bool ac_rtld_get_section_by_name(struct ac_rtld_binary *binary, const char *name,
506 const char **data, size_t *nbytes)
507 {
508 assert(binary->num_parts == 1);
509 return get_section_by_name(&binary->parts[0], name, data, nbytes);
510 }
511
512 bool ac_rtld_read_config(struct ac_rtld_binary *binary,
513 struct ac_shader_config *config)
514 {
515 for (unsigned i = 0; i < binary->num_parts; ++i) {
516 struct ac_rtld_part *part = &binary->parts[i];
517 const char *config_data;
518 size_t config_nbytes;
519
520 if (!get_section_by_name(part, ".AMDGPU.config",
521 &config_data, &config_nbytes))
522 return false;
523
524 /* TODO: be precise about scratch use? */
525 struct ac_shader_config c = {};
526 ac_parse_shader_binary_config(config_data, config_nbytes, true, &c);
527
528 config->num_sgprs = MAX2(config->num_sgprs, c.num_sgprs);
529 config->num_vgprs = MAX2(config->num_vgprs, c.num_vgprs);
530 config->spilled_sgprs = MAX2(config->spilled_sgprs, c.spilled_sgprs);
531 config->spilled_vgprs = MAX2(config->spilled_vgprs, c.spilled_vgprs);
532 config->scratch_bytes_per_wave = MAX2(config->scratch_bytes_per_wave,
533 c.scratch_bytes_per_wave);
534
535 assert(i == 0 || config->float_mode == c.float_mode);
536 config->float_mode = c.float_mode;
537
538 /* SPI_PS_INPUT_ENA/ADDR can't be combined. Only the value from
539 * the main shader part is used. */
540 assert(config->spi_ps_input_ena == 0 &&
541 config->spi_ps_input_addr == 0);
542 config->spi_ps_input_ena = c.spi_ps_input_ena;
543 config->spi_ps_input_addr = c.spi_ps_input_addr;
544
545 /* TODO: consistently use LDS symbols for this */
546 config->lds_size = MAX2(config->lds_size, c.lds_size);
547
548 /* TODO: Should we combine these somehow? It's currently only
549 * used for radeonsi's compute, where multiple parts aren't used. */
550 assert(config->rsrc1 == 0 && config->rsrc2 == 0);
551 config->rsrc1 = c.rsrc1;
552 config->rsrc2 = c.rsrc2;
553 }
554
555 return true;
556 }
557
558 static bool resolve_symbol(const struct ac_rtld_upload_info *u,
559 unsigned part_idx, const Elf64_Sym *sym,
560 const char *name, uint64_t *value)
561 {
562 /* TODO: properly disentangle the undef and the LDS cases once
563 * STT_AMDGPU_LDS is retired. */
564 if (sym->st_shndx == SHN_UNDEF || sym->st_shndx == SHN_AMDGPU_LDS) {
565 const struct ac_rtld_symbol *lds_sym =
566 find_symbol(&u->binary->lds_symbols, name, part_idx);
567
568 if (lds_sym) {
569 *value = lds_sym->offset;
570 return true;
571 }
572
573 /* TODO: resolve from other parts */
574
575 if (u->get_external_symbol(u->cb_data, name, value))
576 return true;
577
578 report_errorf("symbol %s: unknown", name);
579 return false;
580 }
581
582 struct ac_rtld_part *part = &u->binary->parts[part_idx];
583 if (sym->st_shndx >= part->num_sections) {
584 report_errorf("symbol %s: section out of bounds", name);
585 return false;
586 }
587
588 struct ac_rtld_section *s = &part->sections[sym->st_shndx];
589 if (!s->is_rx) {
590 report_errorf("symbol %s: bad section", name);
591 return false;
592 }
593
594 uint64_t section_base = u->rx_va + s->offset;
595
596 *value = section_base + sym->st_value;
597 return true;
598 }
599
600 static bool apply_relocs(const struct ac_rtld_upload_info *u,
601 unsigned part_idx, const Elf64_Shdr *reloc_shdr,
602 const Elf_Data *reloc_data)
603 {
604 #define report_if(cond) \
605 do { \
606 if ((cond)) { \
607 report_errorf(#cond); \
608 return false; \
609 } \
610 } while (false)
611 #define report_elf_if(cond) \
612 do { \
613 if ((cond)) { \
614 report_elf_errorf(#cond); \
615 return false; \
616 } \
617 } while (false)
618
619 struct ac_rtld_part *part = &u->binary->parts[part_idx];
620 Elf_Scn *target_scn = elf_getscn(part->elf, reloc_shdr->sh_info);
621 report_elf_if(!target_scn);
622
623 Elf_Data *target_data = elf_getdata(target_scn, NULL);
624 report_elf_if(!target_data);
625
626 Elf_Scn *symbols_scn = elf_getscn(part->elf, reloc_shdr->sh_link);
627 report_elf_if(!symbols_scn);
628
629 Elf64_Shdr *symbols_shdr = elf64_getshdr(symbols_scn);
630 report_elf_if(!symbols_shdr);
631 uint32_t strtabidx = symbols_shdr->sh_link;
632
633 Elf_Data *symbols_data = elf_getdata(symbols_scn, NULL);
634 report_elf_if(!symbols_data);
635
636 const Elf64_Sym *symbols = symbols_data->d_buf;
637 size_t num_symbols = symbols_data->d_size / sizeof(Elf64_Sym);
638
639 struct ac_rtld_section *s = &part->sections[reloc_shdr->sh_info];
640 report_if(!s->is_rx);
641
642 const char *orig_base = target_data->d_buf;
643 char *dst_base = u->rx_ptr + s->offset;
644 uint64_t va_base = u->rx_va + s->offset;
645
646 Elf64_Rel *rel = reloc_data->d_buf;
647 size_t num_relocs = reloc_data->d_size / sizeof(*rel);
648 for (size_t i = 0; i < num_relocs; ++i, ++rel) {
649 size_t r_sym = ELF64_R_SYM(rel->r_info);
650 unsigned r_type = ELF64_R_TYPE(rel->r_info);
651
652 const char *orig_ptr = orig_base + rel->r_offset;
653 char *dst_ptr = dst_base + rel->r_offset;
654 uint64_t va = va_base + rel->r_offset;
655
656 uint64_t symbol;
657 uint64_t addend;
658
659 if (r_sym == STN_UNDEF) {
660 symbol = 0;
661 } else {
662 report_elf_if(r_sym >= num_symbols);
663
664 const Elf64_Sym *sym = &symbols[r_sym];
665 const char *symbol_name =
666 elf_strptr(part->elf, strtabidx, sym->st_name);
667 report_elf_if(!symbol_name);
668
669 if (!resolve_symbol(u, part_idx, sym, symbol_name, &symbol))
670 return false;
671 }
672
673 /* TODO: Should we also support .rela sections, where the
674 * addend is part of the relocation record? */
675
676 /* Load the addend from the ELF instead of the destination,
677 * because the destination may be in VRAM. */
678 switch (r_type) {
679 case R_AMDGPU_ABS32:
680 case R_AMDGPU_ABS32_LO:
681 case R_AMDGPU_ABS32_HI:
682 case R_AMDGPU_REL32:
683 case R_AMDGPU_REL32_LO:
684 case R_AMDGPU_REL32_HI:
685 addend = *(const uint32_t *)orig_ptr;
686 break;
687 case R_AMDGPU_ABS64:
688 case R_AMDGPU_REL64:
689 addend = *(const uint64_t *)orig_ptr;
690 break;
691 default:
692 report_errorf("unsupported r_type == %u", r_type);
693 return false;
694 }
695
696 uint64_t abs = symbol + addend;
697
698 switch (r_type) {
699 case R_AMDGPU_ABS32:
700 assert((uint32_t)abs == abs);
701 case R_AMDGPU_ABS32_LO:
702 *(uint32_t *)dst_ptr = util_cpu_to_le32(abs);
703 break;
704 case R_AMDGPU_ABS32_HI:
705 *(uint32_t *)dst_ptr = util_cpu_to_le32(abs >> 32);
706 break;
707 case R_AMDGPU_ABS64:
708 *(uint64_t *)dst_ptr = util_cpu_to_le64(abs);
709 break;
710 case R_AMDGPU_REL32:
711 assert((int64_t)(int32_t)(abs - va) == (int64_t)(abs - va));
712 case R_AMDGPU_REL32_LO:
713 *(uint32_t *)dst_ptr = util_cpu_to_le32(abs - va);
714 break;
715 case R_AMDGPU_REL32_HI:
716 *(uint32_t *)dst_ptr = util_cpu_to_le32((abs - va) >> 32);
717 break;
718 case R_AMDGPU_REL64:
719 *(uint64_t *)dst_ptr = util_cpu_to_le64(abs - va);
720 break;
721 default:
722 unreachable("bad r_type");
723 }
724 }
725
726 return true;
727
728 #undef report_if
729 #undef report_elf_if
730 }
731
732 /**
733 * Upload the binary or binaries to the provided GPU buffers, including
734 * relocations.
735 */
736 bool ac_rtld_upload(struct ac_rtld_upload_info *u)
737 {
738 #define report_if(cond) \
739 do { \
740 if ((cond)) { \
741 report_errorf(#cond); \
742 return false; \
743 } \
744 } while (false)
745 #define report_elf_if(cond) \
746 do { \
747 if ((cond)) { \
748 report_errorf(#cond); \
749 return false; \
750 } \
751 } while (false)
752
753 if (u->binary->options.halt_at_entry) {
754 /* s_sethalt 1 */
755 *(uint32_t *)u->rx_ptr = util_cpu_to_le32(0xbf8d0001);
756 }
757
758 /* First pass: upload raw section data and lay out private LDS symbols. */
759 for (unsigned i = 0; i < u->binary->num_parts; ++i) {
760 struct ac_rtld_part *part = &u->binary->parts[i];
761
762 Elf_Scn *section = NULL;
763 while ((section = elf_nextscn(part->elf, section))) {
764 Elf64_Shdr *shdr = elf64_getshdr(section);
765 struct ac_rtld_section *s = &part->sections[elf_ndxscn(section)];
766
767 if (!s->is_rx)
768 continue;
769
770 report_if(shdr->sh_type != SHT_PROGBITS);
771
772 Elf_Data *data = elf_getdata(section, NULL);
773 report_elf_if(!data || data->d_size != shdr->sh_size);
774 memcpy(u->rx_ptr + s->offset, data->d_buf, shdr->sh_size);
775 }
776 }
777
778 if (u->binary->rx_end_markers) {
779 uint32_t *dst = (uint32_t *)(u->rx_ptr + u->binary->rx_end_markers);
780 for (unsigned i = 0; i < DEBUGGER_NUM_MARKERS; ++i)
781 *dst++ = util_cpu_to_le32(DEBUGGER_END_OF_CODE_MARKER);
782 }
783
784 /* Second pass: handle relocations, overwriting uploaded data where
785 * appropriate. */
786 for (unsigned i = 0; i < u->binary->num_parts; ++i) {
787 struct ac_rtld_part *part = &u->binary->parts[i];
788 Elf_Scn *section = NULL;
789 while ((section = elf_nextscn(part->elf, section))) {
790 Elf64_Shdr *shdr = elf64_getshdr(section);
791 if (shdr->sh_type == SHT_REL) {
792 Elf_Data *relocs = elf_getdata(section, NULL);
793 report_elf_if(!relocs || relocs->d_size != shdr->sh_size);
794 if (!apply_relocs(u, i, shdr, relocs))
795 return false;
796 } else if (shdr->sh_type == SHT_RELA) {
797 report_errorf("SHT_RELA not supported");
798 return false;
799 }
800 }
801 }
802
803 return true;
804
805 #undef report_if
806 #undef report_elf_if
807 }