1*5ac3bc71Schristos /* $NetBSD: gelf_phdr.c,v 1.5 2024/03/03 17:37:34 christos Exp $ */
2e81373b4Schristos
39dd9d0cfSchristos /*-
49dd9d0cfSchristos * Copyright (c) 2006,2008 Joseph Koshy
59dd9d0cfSchristos * All rights reserved.
69dd9d0cfSchristos *
79dd9d0cfSchristos * Redistribution and use in source and binary forms, with or without
89dd9d0cfSchristos * modification, are permitted provided that the following conditions
99dd9d0cfSchristos * are met:
109dd9d0cfSchristos * 1. Redistributions of source code must retain the above copyright
119dd9d0cfSchristos * notice, this list of conditions and the following disclaimer.
129dd9d0cfSchristos * 2. Redistributions in binary form must reproduce the above copyright
139dd9d0cfSchristos * notice, this list of conditions and the following disclaimer in the
149dd9d0cfSchristos * documentation and/or other materials provided with the distribution.
159dd9d0cfSchristos *
169dd9d0cfSchristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
179dd9d0cfSchristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
189dd9d0cfSchristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199dd9d0cfSchristos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
209dd9d0cfSchristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
219dd9d0cfSchristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
229dd9d0cfSchristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
239dd9d0cfSchristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
249dd9d0cfSchristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
259dd9d0cfSchristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
269dd9d0cfSchristos * SUCH DAMAGE.
279dd9d0cfSchristos */
289dd9d0cfSchristos
29*5ac3bc71Schristos #include <sys/cdefs.h>
30*5ac3bc71Schristos
319dd9d0cfSchristos #include <gelf.h>
329dd9d0cfSchristos #include <libelf.h>
339dd9d0cfSchristos #include <limits.h>
3442bd3019Schristos #include <stdint.h>
359dd9d0cfSchristos
369dd9d0cfSchristos #include "_libelf.h"
379dd9d0cfSchristos
38*5ac3bc71Schristos __RCSID("$NetBSD: gelf_phdr.c,v 1.5 2024/03/03 17:37:34 christos Exp $");
39*5ac3bc71Schristos ELFTC_VCSID("Id: gelf_phdr.c 3977 2022-05-01 06:45:34Z jkoshy");
409dd9d0cfSchristos
419dd9d0cfSchristos Elf32_Phdr *
elf32_getphdr(Elf * e)429dd9d0cfSchristos elf32_getphdr(Elf *e)
439dd9d0cfSchristos {
449dd9d0cfSchristos return (_libelf_getphdr(e, ELFCLASS32));
459dd9d0cfSchristos }
469dd9d0cfSchristos
479dd9d0cfSchristos Elf64_Phdr *
elf64_getphdr(Elf * e)489dd9d0cfSchristos elf64_getphdr(Elf *e)
499dd9d0cfSchristos {
509dd9d0cfSchristos return (_libelf_getphdr(e, ELFCLASS64));
519dd9d0cfSchristos }
529dd9d0cfSchristos
539dd9d0cfSchristos GElf_Phdr *
gelf_getphdr(Elf * e,int index,GElf_Phdr * d)549dd9d0cfSchristos gelf_getphdr(Elf *e, int index, GElf_Phdr *d)
559dd9d0cfSchristos {
569dd9d0cfSchristos int ec;
579dd9d0cfSchristos Elf32_Ehdr *eh32;
589dd9d0cfSchristos Elf64_Ehdr *eh64;
599dd9d0cfSchristos Elf32_Phdr *ep32;
609dd9d0cfSchristos Elf64_Phdr *ep64;
61*5ac3bc71Schristos size_t phnum;
629dd9d0cfSchristos
639dd9d0cfSchristos if (d == NULL || e == NULL ||
649dd9d0cfSchristos ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64) ||
65*5ac3bc71Schristos (e->e_kind != ELF_K_ELF) || index < 0 ||
66*5ac3bc71Schristos elf_getphdrnum(e, &phnum) < 0) {
67*5ac3bc71Schristos LIBELF_SET_ERROR(ARGUMENT, 0);
68*5ac3bc71Schristos return (NULL);
69*5ac3bc71Schristos }
70*5ac3bc71Schristos
71*5ac3bc71Schristos if ((size_t)index >= phnum) {
729dd9d0cfSchristos LIBELF_SET_ERROR(ARGUMENT, 0);
739dd9d0cfSchristos return (NULL);
749dd9d0cfSchristos }
759dd9d0cfSchristos
769dd9d0cfSchristos if (ec == ELFCLASS32) {
779dd9d0cfSchristos if ((eh32 = _libelf_ehdr(e, ELFCLASS32, 0)) == NULL ||
789dd9d0cfSchristos ((ep32 = _libelf_getphdr(e, ELFCLASS32)) == NULL))
799dd9d0cfSchristos return (NULL);
809dd9d0cfSchristos
819dd9d0cfSchristos ep32 += index;
829dd9d0cfSchristos
839dd9d0cfSchristos d->p_type = ep32->p_type;
849dd9d0cfSchristos d->p_offset = ep32->p_offset;
859dd9d0cfSchristos d->p_vaddr = (Elf64_Addr) ep32->p_vaddr;
869dd9d0cfSchristos d->p_paddr = (Elf64_Addr) ep32->p_paddr;
879dd9d0cfSchristos d->p_filesz = (Elf64_Xword) ep32->p_filesz;
889dd9d0cfSchristos d->p_memsz = (Elf64_Xword) ep32->p_memsz;
899dd9d0cfSchristos d->p_flags = ep32->p_flags;
909dd9d0cfSchristos d->p_align = (Elf64_Xword) ep32->p_align;
919dd9d0cfSchristos
929dd9d0cfSchristos } else {
939dd9d0cfSchristos if ((eh64 = _libelf_ehdr(e, ELFCLASS64, 0)) == NULL ||
949dd9d0cfSchristos (ep64 = _libelf_getphdr(e, ELFCLASS64)) == NULL)
959dd9d0cfSchristos return (NULL);
969dd9d0cfSchristos
979dd9d0cfSchristos ep64 += index;
989dd9d0cfSchristos
999dd9d0cfSchristos *d = *ep64;
1009dd9d0cfSchristos }
1019dd9d0cfSchristos
1029dd9d0cfSchristos return (d);
1039dd9d0cfSchristos }
1049dd9d0cfSchristos
1059dd9d0cfSchristos Elf32_Phdr *
elf32_newphdr(Elf * e,size_t count)1069dd9d0cfSchristos elf32_newphdr(Elf *e, size_t count)
1079dd9d0cfSchristos {
1089dd9d0cfSchristos return (_libelf_newphdr(e, ELFCLASS32, count));
1099dd9d0cfSchristos }
1109dd9d0cfSchristos
1119dd9d0cfSchristos Elf64_Phdr *
elf64_newphdr(Elf * e,size_t count)1129dd9d0cfSchristos elf64_newphdr(Elf *e, size_t count)
1139dd9d0cfSchristos {
1149dd9d0cfSchristos return (_libelf_newphdr(e, ELFCLASS64, count));
1159dd9d0cfSchristos }
1169dd9d0cfSchristos
1179dd9d0cfSchristos void *
gelf_newphdr(Elf * e,size_t count)1189dd9d0cfSchristos gelf_newphdr(Elf *e, size_t count)
1199dd9d0cfSchristos {
1209dd9d0cfSchristos if (e == NULL) {
1219dd9d0cfSchristos LIBELF_SET_ERROR(ARGUMENT, 0);
1229dd9d0cfSchristos return (NULL);
1239dd9d0cfSchristos }
1249dd9d0cfSchristos return (_libelf_newphdr(e, e->e_class, count));
1259dd9d0cfSchristos }
1269dd9d0cfSchristos
1279dd9d0cfSchristos int
gelf_update_phdr(Elf * e,int ndx,GElf_Phdr * s)1289dd9d0cfSchristos gelf_update_phdr(Elf *e, int ndx, GElf_Phdr *s)
1299dd9d0cfSchristos {
130*5ac3bc71Schristos int ec;
131*5ac3bc71Schristos size_t phnum;
1329dd9d0cfSchristos void *ehdr;
1339dd9d0cfSchristos Elf32_Phdr *ph32;
1349dd9d0cfSchristos Elf64_Phdr *ph64;
1359dd9d0cfSchristos
1369dd9d0cfSchristos if (s == NULL || e == NULL || e->e_kind != ELF_K_ELF ||
137*5ac3bc71Schristos ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64) ||
138*5ac3bc71Schristos elf_getphdrnum(e, &phnum) < 0) {
1399dd9d0cfSchristos LIBELF_SET_ERROR(ARGUMENT, 0);
1409dd9d0cfSchristos return (0);
1419dd9d0cfSchristos }
1429dd9d0cfSchristos
1439dd9d0cfSchristos if (e->e_cmd == ELF_C_READ) {
1449dd9d0cfSchristos LIBELF_SET_ERROR(MODE, 0);
1459dd9d0cfSchristos return (0);
1469dd9d0cfSchristos }
1479dd9d0cfSchristos
1489dd9d0cfSchristos if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
1499dd9d0cfSchristos return (0);
1509dd9d0cfSchristos
151*5ac3bc71Schristos if (ndx < 0 || (size_t)ndx > phnum) {
1529dd9d0cfSchristos LIBELF_SET_ERROR(ARGUMENT, 0);
1539dd9d0cfSchristos return (0);
1549dd9d0cfSchristos }
1559dd9d0cfSchristos
1569dd9d0cfSchristos (void) elf_flagphdr(e, ELF_C_SET, ELF_F_DIRTY);
1579dd9d0cfSchristos
1589dd9d0cfSchristos if (ec == ELFCLASS64) {
1599dd9d0cfSchristos ph64 = e->e_u.e_elf.e_phdr.e_phdr64 + ndx;
1609dd9d0cfSchristos *ph64 = *s;
1619dd9d0cfSchristos return (1);
1629dd9d0cfSchristos }
1639dd9d0cfSchristos
1649dd9d0cfSchristos ph32 = e->e_u.e_elf.e_phdr.e_phdr32 + ndx;
1659dd9d0cfSchristos
1669dd9d0cfSchristos ph32->p_type = s->p_type;
1679dd9d0cfSchristos ph32->p_flags = s->p_flags;
1689dd9d0cfSchristos LIBELF_COPY_U32(ph32, s, p_offset);
1699dd9d0cfSchristos LIBELF_COPY_U32(ph32, s, p_vaddr);
1709dd9d0cfSchristos LIBELF_COPY_U32(ph32, s, p_paddr);
1719dd9d0cfSchristos LIBELF_COPY_U32(ph32, s, p_filesz);
1729dd9d0cfSchristos LIBELF_COPY_U32(ph32, s, p_memsz);
1739dd9d0cfSchristos LIBELF_COPY_U32(ph32, s, p_align);
1749dd9d0cfSchristos
1759dd9d0cfSchristos return (1);
1769dd9d0cfSchristos }
177