1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Copyright (c) 2021, The Linux Foundation. All rights reserved. 4 * Copyright (c) 2023 Rockchip Electronics Co., Ltd. 5 */ 6 7 #ifndef __ROCKCHIP_ELF_COMMON_H 8 #define __ROCKCHIP_ELF_COMMON_H 9 10 #include <linux/elf.h> 11 12 /* Generic helpers for ELF use */ 13 /* Return first section header */ elf_sheader(struct elfhdr * hdr)14static inline struct elf_shdr *elf_sheader(struct elfhdr *hdr) 15 { 16 return (struct elf_shdr *)((size_t)hdr + (size_t)hdr->e_shoff); 17 } 18 19 /* Return idx section header */ elf_section(struct elfhdr * hdr,int idx)20static inline struct elf_shdr *elf_section(struct elfhdr *hdr, int idx) 21 { 22 return &elf_sheader(hdr)[idx]; 23 } 24 25 /* Return first program header */ elf_pheader(struct elfhdr * hdr)26static inline struct elf_phdr *elf_pheader(struct elfhdr *hdr) 27 { 28 return (struct elf_phdr *)((size_t)hdr + (size_t)hdr->e_phoff); 29 } 30 31 /* Return idx program header */ elf_program(struct elfhdr * hdr,int idx)32static inline struct elf_phdr *elf_program(struct elfhdr *hdr, int idx) 33 { 34 return &elf_pheader(hdr)[idx]; 35 } 36 37 /* Return section's string table header */ elf_str_table(struct elfhdr * hdr)38static inline char *elf_str_table(struct elfhdr *hdr) 39 { 40 if (hdr->e_shstrndx == SHN_UNDEF) 41 return NULL; 42 return (char *)hdr + elf_section(hdr, hdr->e_shstrndx)->sh_offset; 43 } 44 45 #endif 46