elf.h 1.1 KB

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