12de3b87aSKai Wang /*-
22de3b87aSKai Wang * Copyright (c) 2006,2008 Joseph Koshy
32de3b87aSKai Wang * All rights reserved.
42de3b87aSKai Wang *
52de3b87aSKai Wang * Redistribution and use in source and binary forms, with or without
62de3b87aSKai Wang * modification, are permitted provided that the following conditions
72de3b87aSKai Wang * are met:
82de3b87aSKai Wang * 1. Redistributions of source code must retain the above copyright
92de3b87aSKai Wang * notice, this list of conditions and the following disclaimer.
102de3b87aSKai Wang * 2. Redistributions in binary form must reproduce the above copyright
112de3b87aSKai Wang * notice, this list of conditions and the following disclaimer in the
122de3b87aSKai Wang * documentation and/or other materials provided with the distribution.
132de3b87aSKai Wang *
142de3b87aSKai Wang * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
152de3b87aSKai Wang * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
162de3b87aSKai Wang * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
172de3b87aSKai Wang * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
182de3b87aSKai Wang * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
192de3b87aSKai Wang * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
202de3b87aSKai Wang * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
212de3b87aSKai Wang * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
222de3b87aSKai Wang * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
232de3b87aSKai Wang * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
242de3b87aSKai Wang * SUCH DAMAGE.
252de3b87aSKai Wang */
262de3b87aSKai Wang
272de3b87aSKai Wang #include <assert.h>
282de3b87aSKai Wang #include <gelf.h>
292de3b87aSKai Wang #include <libelf.h>
302de3b87aSKai Wang #include <stdlib.h>
312de3b87aSKai Wang
322de3b87aSKai Wang #include "_libelf.h"
332de3b87aSKai Wang
34*d003e0d7SEd Maste ELFTC_VCSID("$Id: libelf_phdr.c 3732 2019-04-22 11:08:38Z jkoshy $");
352de3b87aSKai Wang
362de3b87aSKai Wang void *
_libelf_getphdr(Elf * e,int ec)372de3b87aSKai Wang _libelf_getphdr(Elf *e, int ec)
382de3b87aSKai Wang {
392de3b87aSKai Wang size_t phnum;
402de3b87aSKai Wang size_t fsz, msz;
412de3b87aSKai Wang uint64_t phoff;
422de3b87aSKai Wang Elf32_Ehdr *eh32;
432de3b87aSKai Wang Elf64_Ehdr *eh64;
442de3b87aSKai Wang void *ehdr, *phdr;
45ae500c1fSEd Maste _libelf_translator_function *xlator;
462de3b87aSKai Wang
472de3b87aSKai Wang assert(ec == ELFCLASS32 || ec == ELFCLASS64);
482de3b87aSKai Wang
492de3b87aSKai Wang if (e == NULL) {
502de3b87aSKai Wang LIBELF_SET_ERROR(ARGUMENT, 0);
512de3b87aSKai Wang return (NULL);
522de3b87aSKai Wang }
532de3b87aSKai Wang
542de3b87aSKai Wang if ((phdr = (ec == ELFCLASS32 ?
552de3b87aSKai Wang (void *) e->e_u.e_elf.e_phdr.e_phdr32 :
562de3b87aSKai Wang (void *) e->e_u.e_elf.e_phdr.e_phdr64)) != NULL)
572de3b87aSKai Wang return (phdr);
582de3b87aSKai Wang
592de3b87aSKai Wang /*
602de3b87aSKai Wang * Check the PHDR related fields in the EHDR for sanity.
612de3b87aSKai Wang */
622de3b87aSKai Wang
632de3b87aSKai Wang if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
642de3b87aSKai Wang return (NULL);
652de3b87aSKai Wang
662de3b87aSKai Wang phnum = e->e_u.e_elf.e_nphdr;
672de3b87aSKai Wang
682de3b87aSKai Wang if (ec == ELFCLASS32) {
692de3b87aSKai Wang eh32 = (Elf32_Ehdr *) ehdr;
702de3b87aSKai Wang phoff = (uint64_t) eh32->e_phoff;
712de3b87aSKai Wang } else {
722de3b87aSKai Wang eh64 = (Elf64_Ehdr *) ehdr;
732de3b87aSKai Wang phoff = (uint64_t) eh64->e_phoff;
742de3b87aSKai Wang }
752de3b87aSKai Wang
762de3b87aSKai Wang fsz = gelf_fsize(e, ELF_T_PHDR, phnum, e->e_version);
772de3b87aSKai Wang
782de3b87aSKai Wang assert(fsz > 0);
792de3b87aSKai Wang
80*d003e0d7SEd Maste if (phoff + fsz < phoff) { /* Numeric overflow. */
81*d003e0d7SEd Maste LIBELF_SET_ERROR(HEADER, 0);
82*d003e0d7SEd Maste return (NULL);
83*d003e0d7SEd Maste }
84*d003e0d7SEd Maste
852de3b87aSKai Wang if ((uint64_t) e->e_rawsize < (phoff + fsz)) {
862de3b87aSKai Wang LIBELF_SET_ERROR(HEADER, 0);
872de3b87aSKai Wang return (NULL);
882de3b87aSKai Wang }
892de3b87aSKai Wang
90*d003e0d7SEd Maste if ((msz = _libelf_msize(ELF_T_PHDR, ec, EV_CURRENT)) == 0)
91*d003e0d7SEd Maste return (NULL);
922de3b87aSKai Wang
932de3b87aSKai Wang if ((phdr = calloc(phnum, msz)) == NULL) {
942de3b87aSKai Wang LIBELF_SET_ERROR(RESOURCE, 0);
952de3b87aSKai Wang return (NULL);
962de3b87aSKai Wang }
972de3b87aSKai Wang
982de3b87aSKai Wang if (ec == ELFCLASS32)
992de3b87aSKai Wang e->e_u.e_elf.e_phdr.e_phdr32 = phdr;
1002de3b87aSKai Wang else
1012de3b87aSKai Wang e->e_u.e_elf.e_phdr.e_phdr64 = phdr;
1022de3b87aSKai Wang
1032de3b87aSKai Wang
104ae500c1fSEd Maste xlator = _libelf_get_translator(ELF_T_PHDR, ELF_TOMEMORY, ec,
105ae500c1fSEd Maste _libelf_elfmachine(e));
1062de3b87aSKai Wang (*xlator)(phdr, phnum * msz, e->e_rawfile + phoff, phnum,
1072de3b87aSKai Wang e->e_byteorder != LIBELF_PRIVATE(byteorder));
1082de3b87aSKai Wang
1092de3b87aSKai Wang return (phdr);
1102de3b87aSKai Wang }
1112de3b87aSKai Wang
1122de3b87aSKai Wang void *
_libelf_newphdr(Elf * e,int ec,size_t count)1132de3b87aSKai Wang _libelf_newphdr(Elf *e, int ec, size_t count)
1142de3b87aSKai Wang {
1152de3b87aSKai Wang void *ehdr, *newphdr, *oldphdr;
1162de3b87aSKai Wang size_t msz;
1172de3b87aSKai Wang
1182de3b87aSKai Wang if (e == NULL) {
1192de3b87aSKai Wang LIBELF_SET_ERROR(ARGUMENT, 0);
1202de3b87aSKai Wang return (NULL);
1212de3b87aSKai Wang }
1222de3b87aSKai Wang
1232de3b87aSKai Wang if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL) {
1242de3b87aSKai Wang LIBELF_SET_ERROR(SEQUENCE, 0);
1252de3b87aSKai Wang return (NULL);
1262de3b87aSKai Wang }
1272de3b87aSKai Wang
1282de3b87aSKai Wang assert(e->e_class == ec);
1292de3b87aSKai Wang assert(ec == ELFCLASS32 || ec == ELFCLASS64);
1302de3b87aSKai Wang assert(e->e_version == EV_CURRENT);
1312de3b87aSKai Wang
132*d003e0d7SEd Maste if ((msz = _libelf_msize(ELF_T_PHDR, ec, e->e_version)) == 0)
133*d003e0d7SEd Maste return (NULL);
1342de3b87aSKai Wang
1352de3b87aSKai Wang newphdr = NULL;
1362de3b87aSKai Wang if (count > 0 && (newphdr = calloc(count, msz)) == NULL) {
1372de3b87aSKai Wang LIBELF_SET_ERROR(RESOURCE, 0);
1382de3b87aSKai Wang return (NULL);
1392de3b87aSKai Wang }
1402de3b87aSKai Wang
1412de3b87aSKai Wang if (ec == ELFCLASS32) {
1422de3b87aSKai Wang if ((oldphdr = (void *) e->e_u.e_elf.e_phdr.e_phdr32) != NULL)
1432de3b87aSKai Wang free(oldphdr);
1442de3b87aSKai Wang e->e_u.e_elf.e_phdr.e_phdr32 = (Elf32_Phdr *) newphdr;
1452de3b87aSKai Wang } else {
1462de3b87aSKai Wang if ((oldphdr = (void *) e->e_u.e_elf.e_phdr.e_phdr64) != NULL)
1472de3b87aSKai Wang free(oldphdr);
1482de3b87aSKai Wang e->e_u.e_elf.e_phdr.e_phdr64 = (Elf64_Phdr *) newphdr;
1492de3b87aSKai Wang }
1502de3b87aSKai Wang
1512de3b87aSKai Wang e->e_u.e_elf.e_nphdr = count;
1522de3b87aSKai Wang
1532de3b87aSKai Wang elf_flagphdr(e, ELF_C_SET, ELF_F_DIRTY);
1542de3b87aSKai Wang
1552de3b87aSKai Wang return (newphdr);
1562de3b87aSKai Wang }
157