Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

gpu: nova-core: firmware: factor out an elf_str() function

Factor out a chunk of complexity into a new subroutine. This is an
incremental step in adding ELF32 support to the existing ELF64 section
support, for handling GPU firmware.

Signed-off-by: John Hubbard <jhubbard@nvidia.com>
Acked-by: Danilo Krummrich <dakr@kernel.org>
Link: https://patch.msgid.link/20260326013902.588242-9-jhubbard@nvidia.com
[acourbot: use fuller prefix in commit message.]
Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>

authored by

John Hubbard and committed by
Alexandre Courbot
7c50d748 b3d24269

+15 -25
+15 -25
drivers/gpu/nova-core/firmware.rs
··· 484 484 // SAFETY: all bit patterns are valid for this type, and it doesn't use interior mutability. 485 485 unsafe impl FromBytes for Elf64SHdr {} 486 486 487 + /// Returns a NULL-terminated string from the ELF image at `offset`. 488 + fn elf_str(elf: &[u8], offset: u64) -> Option<&str> { 489 + let idx = usize::try_from(offset).ok()?; 490 + let bytes = elf.get(idx..)?; 491 + CStr::from_bytes_until_nul(bytes).ok()?.to_str().ok() 492 + } 493 + 487 494 /// Tries to extract section with name `name` from the ELF64 image `elf`, and returns it. 488 495 pub(super) fn elf64_section<'a, 'b>(elf: &'a [u8], name: &'b str) -> Option<&'a [u8]> { 489 496 let hdr = &elf ··· 517 510 .and_then(Elf64SHdr::from_bytes)?; 518 511 519 512 // Find the section which name matches `name` and return it. 520 - shdr.find(|&sh| { 521 - let Some(hdr) = Elf64SHdr::from_bytes(sh) else { 522 - return false; 523 - }; 524 - 525 - let Some(name_idx) = strhdr 526 - .0 527 - .sh_offset 528 - .checked_add(u64::from(hdr.0.sh_name)) 529 - .and_then(|idx| usize::try_from(idx).ok()) 530 - else { 531 - return false; 532 - }; 533 - 534 - // Get the start of the name. 535 - elf.get(name_idx..) 536 - .and_then(|nstr| CStr::from_bytes_until_nul(nstr).ok()) 537 - // Convert into str. 538 - .and_then(|c_str| c_str.to_str().ok()) 539 - // Check that the name matches. 540 - .map(|str| str == name) 541 - .unwrap_or(false) 542 - }) 543 - // Return the slice containing the section. 544 - .and_then(|sh| { 513 + shdr.find_map(|sh| { 545 514 let hdr = Elf64SHdr::from_bytes(sh)?; 515 + let name_offset = strhdr.0.sh_offset.checked_add(u64::from(hdr.0.sh_name))?; 516 + let section_name = elf_str(elf, name_offset)?; 517 + 518 + if section_name != name { 519 + return None; 520 + } 521 + 546 522 let start = usize::try_from(hdr.0.sh_offset).ok()?; 547 523 let end = usize::try_from(hdr.0.sh_size) 548 524 .ok()