Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class HsaHeader : public Header {
int numProgramHeaders() const override { return static_cast<int>(ehdr_.e_phnum); }
uint64_t sectionHeaderOff() const override { return ehdr_.e_shoff; }
int numSectionHeaders() const override { return static_cast<int>(ehdr_.e_shnum); }
uint64_t sectionHeaderEntrySize() const override { return ehdr_.e_shentsize; }
int sectionHeaderStrIdx() const override { return static_cast<int>(ehdr_.e_shstrndx); }
uint32_t flags() const override { return ehdr_.e_flags; }

Expand Down Expand Up @@ -85,6 +86,8 @@ AmdGpuCodeObject::AmdGpuCodeObject(AmdGpuCodeObject &&other) noexcept
executable_nobits_sections_ = std::move(other.executable_nobits_sections_);
allocated_executable_sections_ = std::move(other.allocated_executable_sections_);
rodata_sections_ = std::move(other.rodata_sections_);
program_headers_ = std::move(other.program_headers_);
kd_offsets_ = std::move(other.kd_offsets_);
}

AmdGpuCodeObject::AmdGpuCodeObject(const std::string &elf_path) {
Expand All @@ -110,14 +113,18 @@ AmdGpuCodeObject::AmdGpuCodeObject(const std::string &elf_path) {
}

header_ = std::make_unique<HsaHeader>(ehdr);
if (!load_program_headers(ehdr)) {
is_valid_ = false;
return;
}
load_sections();
if (!is_valid_)
return;
target_id_ = target_from_machine_flags(header_->flags());
}

AmdGpuCodeObject::AmdGpuCodeObject(const uint8_t *elf_bytes, size_t elf_size) {
if (elf_size < sizeof(Elf64_Ehdr)) {
if (elf_bytes == nullptr || elf_size < sizeof(Elf64_Ehdr)) {
is_valid_ = false;
return;
}
Expand All @@ -135,6 +142,10 @@ AmdGpuCodeObject::AmdGpuCodeObject(const uint8_t *elf_bytes, size_t elf_size) {
}

header_ = std::make_unique<HsaHeader>(ehdr);
if (!load_program_headers(ehdr)) {
is_valid_ = false;
return;
}
load_sections();
if (!is_valid_)
return;
Expand All @@ -144,7 +155,7 @@ AmdGpuCodeObject::AmdGpuCodeObject(const uint8_t *elf_bytes, size_t elf_size) {
AmdGpuCodeObject::AmdGpuCodeObject(const uint8_t *elf_bytes, size_t elf_size,
std::string offload_kind, std::string target_triple)
: offload_kind_(std::move(offload_kind)), target_triple_(std::move(target_triple)) {
if (elf_size < sizeof(Elf64_Ehdr)) {
if (elf_bytes == nullptr || elf_size < sizeof(Elf64_Ehdr)) {
is_valid_ = false;
return;
}
Expand All @@ -162,6 +173,10 @@ AmdGpuCodeObject::AmdGpuCodeObject(const uint8_t *elf_bytes, size_t elf_size,
}

header_ = std::make_unique<HsaHeader>(ehdr);
if (!load_program_headers(ehdr)) {
is_valid_ = false;
return;
}
load_sections();
if (!is_valid_)
return;
Expand All @@ -183,34 +198,62 @@ AmdGpuCodeObject::AmdGpuCodeObject(const uint8_t *elf_bytes, size_t elf_size,
AmdGpuCodeObject::~AmdGpuCodeObject() = default;

void AmdGpuCodeObject::load_sections() {
const auto shoff = header_->sectionHeaderOff();
const uint64_t shoff = header_->sectionHeaderOff();
const int num_shdrs = header_->numSectionHeaders();
if (num_shdrs < 0 || !fits_in_bounds(shoff, static_cast<uint64_t>(num_shdrs) * sizeof(Elf64_Shdr),
image_.size())) {
// Keep accepted code objects compatible with the patcher's fixed-size
// section-header rewrites. A larger stride can carry extension bytes that
// the patcher cannot preserve.
if (num_shdrs < 0 || header_->sectionHeaderEntrySize() != sizeof(Elf64_Shdr)) {
is_valid_ = false;
return;
}
const std::span image(reinterpret_cast<const uint8_t *>(image_.data()), image_.size());
std::optional<std::vector<Elf64_Shdr>> section_hdrs = read_elf_table<Elf64_Shdr>(
Comment thread
kuhar marked this conversation as resolved.
Resolved
image, shoff, static_cast<uint64_t>(num_shdrs), header_->sectionHeaderEntrySize());
if (!section_hdrs) {
is_valid_ = false;
return;
}

std::vector<Elf64_Shdr> section_hdrs(static_cast<size_t>(num_shdrs));
std::memcpy(section_hdrs.data(), image_.data() + shoff, section_hdrs.size() * sizeof(Elf64_Shdr));

int shstrndx = header_->sectionHeaderStrIdx();
if (shstrndx < 0 || static_cast<size_t>(shstrndx) >= section_hdrs.size()) {
if (shstrndx < 0 || static_cast<size_t>(shstrndx) >= section_hdrs->size()) {
is_valid_ = false;
return;
}

auto &shstrtab = section_hdrs[shstrndx];
const Elf64_Shdr &shstrtab = (*section_hdrs)[shstrndx];
if (!fits_in_bounds(shstrtab.sh_offset, shstrtab.sh_size, image_.size())) {
is_valid_ = false;
return;
}
const char *shstrtab_data = image_.data() + shstrtab.sh_offset;

for (size_t section_index = 0; section_index < section_hdrs.size(); ++section_index) {
const Elf64_Shdr &shdr = section_hdrs[section_index];
for (size_t section_index = 0; section_index < section_hdrs->size(); ++section_index) {
const Elf64_Shdr &shdr = (*section_hdrs)[section_index];
if (shdr.sh_type == SHT_NULL)
continue;
// Symbol relocation and descriptor updates in the patcher require fixed
// records too; accepting extensions here would silently skip those updates.
if ((shdr.sh_type == SHT_SYMTAB || shdr.sh_type == SHT_DYNSYM) &&
shdr.sh_entsize != sizeof(Elf64_Sym)) {
is_valid_ = false;
return;
}
const uint64_t relocation_record_size = shdr.sh_type == SHT_RELA ? sizeof(Elf64_Rela)
: shdr.sh_type == SHT_REL ? sizeof(Elf64_Rel)
: 0;
// Explicit nonallocated relocation targets are ignored by the loader and
// translator. Other relocation tables can affect moved code/data, so the
// patcher must understand their complete records.
const bool nonallocated_relocation_target =
shdr.sh_info != SHN_UNDEF && shdr.sh_info < section_hdrs->size() &&
(*section_hdrs)[shdr.sh_info].sh_type != SHT_NULL &&
((*section_hdrs)[shdr.sh_info].sh_flags & SHF_ALLOC) == 0;
if (relocation_record_size != 0 && shdr.sh_entsize != relocation_record_size &&
!nonallocated_relocation_target) {
is_valid_ = false;
return;
}
const bool allocated_executable =
(shdr.sh_flags & (SHF_ALLOC | SHF_EXECINSTR)) == (SHF_ALLOC | SHF_EXECINSTR);
if (shdr.sh_type == SHT_NOBITS && !allocated_executable)
Expand All @@ -235,7 +278,8 @@ void AmdGpuCodeObject::load_sections() {
std::unique_ptr<char[]> sec_data;
if (shdr.sh_type != SHT_NOBITS) {
sec_data = std::make_unique<char[]>(shdr.sh_size);
std::memcpy(sec_data.get(), image_.data() + shdr.sh_offset, shdr.sh_size);
if (shdr.sh_size != 0)
std::memcpy(sec_data.get(), image_.data() + shdr.sh_offset, shdr.sh_size);
}
// SHT_NOBITS has no executable bytes to decode. Retain allocated executable
// metadata above so the translator's sole-PROGBITS-.text gate rejects the
Expand All @@ -261,21 +305,18 @@ void AmdGpuCodeObject::load_sections() {
// Parse symbol table for kernel descriptor offsets.
// Scan both SHT_SYMTAB and SHT_DYNSYM — stripped code objects may
// only have the latter.
for (size_t i = 0; i < section_hdrs.size(); ++i) {
if (section_hdrs[i].sh_type != SHT_SYMTAB && section_hdrs[i].sh_type != SHT_DYNSYM)
continue;
auto &symtab_shdr = section_hdrs[i];
if (symtab_shdr.sh_entsize == 0)
continue;
if (symtab_shdr.sh_entsize < sizeof(Elf64_Sym))
for (size_t section_index = 0; section_index < section_hdrs->size(); ++section_index) {
if ((*section_hdrs)[section_index].sh_type != SHT_SYMTAB &&
(*section_hdrs)[section_index].sh_type != SHT_DYNSYM)
continue;
const Elf64_Shdr &symtab_shdr = (*section_hdrs)[section_index];
if (!fits_in_bounds(symtab_shdr.sh_offset, symtab_shdr.sh_size, image_.size()))
continue;

// Read the string table linked to this symtab.
if (symtab_shdr.sh_link >= section_hdrs.size())
if (symtab_shdr.sh_link >= section_hdrs->size())
continue;
auto &strtab_shdr = section_hdrs[symtab_shdr.sh_link];
const Elf64_Shdr &strtab_shdr = (*section_hdrs)[symtab_shdr.sh_link];
if (!fits_in_bounds(strtab_shdr.sh_offset, strtab_shdr.sh_size, image_.size()))
continue;
const char *sym_strtab = image_.data() + strtab_shdr.sh_offset;
Expand Down
33 changes: 32 additions & 1 deletion emulation/rocjitsu/lib/rocjitsu/src/rocjitsu/code/amdgpu_elf.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
// Copyright (c) 2025 Advanced Micro Devices, Inc.
// Copyright (c) 2025-2026 Advanced Micro Devices, Inc.
// SPDX-License-Identifier: MIT

#ifndef ROCJITSU_CODE_AMDGPU_ELF_H_
#define ROCJITSU_CODE_AMDGPU_ELF_H_

#include "rocjitsu/code/rj_code.h"

#include <cstddef>
#include <cstdint>
#include <cstring>
#include <limits>
#include <optional>
#include <span>
#include <type_traits>
#include <vector>

namespace rocjitsu {

Expand Down Expand Up @@ -355,6 +361,31 @@ struct Elf64_Rela {
int64_t r_addend;
};

/// @brief Copy a possibly strided ELF record table from an unaligned image.
/// @details The declared stride may include trailing bytes, but it must hold a
/// complete record. The entire table is bounds checked before any record is
/// copied, including overflow in the count-times-stride calculation.
template <typename Record>
[[nodiscard]] std::optional<std::vector<Record>>
read_elf_table(std::span<const uint8_t> image, uint64_t offset, uint64_t count, uint64_t stride) {
static_assert(std::is_trivially_copyable_v<Record>);
if (count == 0)
return std::vector<Record>{};
if (stride < sizeof(Record) || stride > std::numeric_limits<uint64_t>::max() / count) {
return std::nullopt;
}
const uint64_t table_size = count * stride;
if (offset > image.size() || table_size > image.size() - offset)
return std::nullopt;

std::vector<Record> records(static_cast<std::size_t>(count));
for (std::size_t record_index = 0; record_index < records.size(); ++record_index) {
const uint64_t record_offset = offset + record_index * stride;
std::memcpy(&records[record_index], image.data() + record_offset, sizeof(Record));
}
return records;
}

/// @brief Whether ROCr's dynamic loader forms this relocation from a symbol value.
inline constexpr bool rocr_dynamic_relocation_uses_symbol_value(uint32_t relocation_type) {
switch (relocation_type) {
Expand Down
101 changes: 63 additions & 38 deletions emulation/rocjitsu/lib/rocjitsu/src/rocjitsu/code/code_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
#ifndef ROCJITSU_CODE_CODE_OBJECT_H_
#define ROCJITSU_CODE_CODE_OBJECT_H_

#include "rocjitsu/code/amdgpu_elf.h"

#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <memory>
#include <optional>
#include <span>
#include <string>
#include <vector>

Expand All @@ -37,6 +41,10 @@ class Header {
/// @returns Section header count.
virtual int numSectionHeaders() const = 0;

/// @brief Byte stride between section-header table entries.
/// @returns Byte stride declared by e_shentsize.
virtual uint64_t sectionHeaderEntrySize() const = 0;

/// @brief Index of the section header string table section.
/// @returns Section index.
virtual int sectionHeaderStrIdx() const = 0;
Expand Down Expand Up @@ -135,23 +143,13 @@ class CodeObject {
/// @returns The highest VA + size across all LOAD segments.
std::size_t mem_size() const {
uint64_t max_va = 0;
auto *ehdr = reinterpret_cast<const uint8_t *>(image_.data());
if (image_.size() < 64)
return image_.size();
uint64_t phoff = *reinterpret_cast<const uint64_t *>(ehdr + 32);
uint16_t phentsize = *reinterpret_cast<const uint16_t *>(ehdr + 54);
uint16_t phnum = *reinterpret_cast<const uint16_t *>(ehdr + 56);
if (phentsize == 0 || phoff + static_cast<uint64_t>(phnum) * phentsize > image_.size())
return image_.size(); // Malformed ELF — fall back to raw image size.
for (uint16_t i = 0; i < phnum; ++i) {
auto *ph = ehdr + phoff + i * phentsize;
uint32_t p_type = *reinterpret_cast<const uint32_t *>(ph);
if (p_type != 1)
continue; // PT_LOAD
uint64_t p_vaddr = *reinterpret_cast<const uint64_t *>(ph + 16);
uint64_t p_memsz = *reinterpret_cast<const uint64_t *>(ph + 40);
max_va = std::max(max_va, p_vaddr + p_memsz);
for (const Elf64_Phdr &phdr : program_headers_) {
if (phdr.p_type != PT_LOAD)
continue;
max_va = std::max(max_va, phdr.p_vaddr + phdr.p_memsz);
}
if (max_va > std::numeric_limits<std::size_t>::max())
return image_.size();
return max_va > 0 ? static_cast<std::size_t>(max_va) : image_.size();
}

Expand All @@ -161,33 +159,33 @@ class CodeObject {
/// a different VA than file offset (common with AMDGPU code objects).
/// @param memory Target memory to load into.
/// @param base_addr Base address for loading.
template <typename Memory> void load_to_memory(Memory *memory, uint64_t base_addr) const {
auto *ehdr = reinterpret_cast<const uint8_t *>(image_.data());
if (image_.size() < 64) {
memory->load_image(reinterpret_cast<const uint8_t *>(image_.data()), image_.size(),
base_addr);
return;
}
uint64_t phoff = *reinterpret_cast<const uint64_t *>(ehdr + 32);
uint16_t phentsize = *reinterpret_cast<const uint16_t *>(ehdr + 54);
uint16_t phnum = *reinterpret_cast<const uint16_t *>(ehdr + 56);
/// @returns False if adding the base address would overflow any load range;
/// no memory writes occur in that case.
template <typename Memory> bool load_to_memory(Memory *memory, uint64_t base_addr) const {
bool loaded_any = false;
for (uint16_t i = 0; i < phnum; ++i) {
auto *ph = ehdr + phoff + i * phentsize;
uint32_t p_type = *reinterpret_cast<const uint32_t *>(ph);
if (p_type != 1)
continue; // PT_LOAD
uint64_t p_offset = *reinterpret_cast<const uint64_t *>(ph + 8);
uint64_t p_vaddr = *reinterpret_cast<const uint64_t *>(ph + 16);
uint64_t p_filesz = *reinterpret_cast<const uint64_t *>(ph + 32);
if (p_filesz > 0 && p_offset + p_filesz <= image_.size())
memory->load_image(ehdr + p_offset, static_cast<std::size_t>(p_filesz),
base_addr + p_vaddr);
for (const Elf64_Phdr &phdr : program_headers_) {
if (phdr.p_type != PT_LOAD)
continue;
if (phdr.p_vaddr > std::numeric_limits<uint64_t>::max() - base_addr ||
phdr.p_memsz > std::numeric_limits<uint64_t>::max() - base_addr - phdr.p_vaddr)
return false;
loaded_any = true;
}
if (!loaded_any)
if (!loaded_any) {
if (image_.size() > std::numeric_limits<uint64_t>::max() - base_addr)
return false;
memory->load_image(reinterpret_cast<const uint8_t *>(image_.data()), image_.size(),
base_addr);
return true;
}

const auto *image = reinterpret_cast<const uint8_t *>(image_.data());
for (const Elf64_Phdr &phdr : program_headers_) {
if (phdr.p_type == PT_LOAD && phdr.p_filesz != 0)
memory->load_image(image + phdr.p_offset, static_cast<std::size_t>(phdr.p_filesz),
base_addr + phdr.p_vaddr);
}
return true;
}

/// @brief Find the kernel descriptor symbol offset for a named kernel.
Expand All @@ -196,13 +194,40 @@ class CodeObject {
virtual uint64_t kernel_descriptor_offset(const std::string & /*kernel_name*/) const { return 0; }

protected:
/// @brief Validate and retain program headers for device-image loading.
/// @returns False for unsupported strides, invalid table ranges, or LOAD
/// segments whose file range or virtual-address arithmetic overflows.
bool load_program_headers(const Elf64_Ehdr &ehdr) {
// The patcher rewrites contiguous records and cannot preserve extensions.
if (ehdr.e_phnum != 0 && ehdr.e_phentsize != sizeof(Elf64_Phdr))
return false;
const std::span image(reinterpret_cast<const uint8_t *>(image_.data()), image_.size());
std::optional<std::vector<Elf64_Phdr>> phdrs =
read_elf_table<Elf64_Phdr>(image, ehdr.e_phoff, ehdr.e_phnum, ehdr.e_phentsize);
if (!phdrs)
return false;
for (const Elf64_Phdr &phdr : *phdrs) {
if (phdr.p_type != PT_LOAD)
continue;
if ((phdr.p_filesz != 0 &&
(phdr.p_offset > image_.size() || phdr.p_filesz > image_.size() - phdr.p_offset)) ||
phdr.p_filesz > phdr.p_memsz ||
phdr.p_memsz > std::numeric_limits<uint64_t>::max() - phdr.p_vaddr) {
return false;
}
}
program_headers_ = std::move(*phdrs);
return true;
}

bool is_valid_ = true;
std::vector<char> image_;
std::unique_ptr<Header> header_;
std::vector<std::unique_ptr<Section>> sections_;
std::vector<const Section *> text_sections_;
std::vector<const Section *> allocated_executable_sections_;
std::vector<const Section *> rodata_sections_;
std::vector<Elf64_Phdr> program_headers_;
};

} // namespace rocjitsu
Expand Down
Loading
Loading