Home | History | Annotate | Line # | Download | only in libelf
      1 /*	$NetBSD: libelf_phdr.c,v 1.7 2026/05/17 21:40:51 jkoshy Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2006,2008 Joseph Koshy
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #if HAVE_NBTOOL_CONFIG_H
     30 # include "nbtool_config.h"
     31 #endif
     32 
     33 #include <sys/cdefs.h>
     34 
     35 #include <assert.h>
     36 #include <gelf.h>
     37 #include <libelf.h>
     38 #include <stdlib.h>
     39 
     40 #include "_libelf.h"
     41 
     42 ELFTC_VCSID("Id: libelf_phdr.c 4074 2025-01-07 15:34:21Z jkoshy");
     43 
     44 __RCSID("$NetBSD: libelf_phdr.c,v 1.7 2026/05/17 21:40:51 jkoshy Exp $");
     45 
     46 void *
     47 _libelf_getphdr(Elf *e, unsigned int ec)
     48 {
     49 	size_t phnum;
     50 	size_t fsz, msz;
     51 	uint64_t phoff;
     52 	Elf32_Ehdr *eh32;
     53 	Elf64_Ehdr *eh64;
     54 	void *ehdr, *phdr;
     55 	_libelf_translator_function *xlator;
     56 
     57 	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
     58 
     59 	if (e == NULL) {
     60 		LIBELF_SET_ERROR(ARGUMENT, 0);
     61 		return (NULL);
     62 	}
     63 
     64 	if ((phdr = (ec == ELFCLASS32 ?
     65 		 (void *) e->e_u.e_elf.e_phdr.e_phdr32 :
     66 		 (void *) e->e_u.e_elf.e_phdr.e_phdr64)) != NULL)
     67 		return (phdr);
     68 
     69 	/*
     70 	 * Check the PHDR related fields in the EHDR for sanity.
     71 	 */
     72 
     73 	if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
     74 		return (NULL);
     75 
     76 	phnum = e->e_u.e_elf.e_nphdr;
     77 
     78 	if (ec == ELFCLASS32) {
     79 		eh32      = (Elf32_Ehdr *) ehdr;
     80 		phoff     = (uint64_t) eh32->e_phoff;
     81 	} else {
     82 		eh64      = (Elf64_Ehdr *) ehdr;
     83 		phoff     = (uint64_t) eh64->e_phoff;
     84 	}
     85 
     86 	fsz = gelf_fsize(e, ELF_T_PHDR, phnum, e->e_version);
     87 
     88 	assert(fsz > 0);
     89 
     90 	if (phoff + fsz < phoff) {	/* Numeric overflow. */
     91 		LIBELF_SET_ERROR(HEADER, 0);
     92 		return (NULL);
     93 	}
     94 
     95 	if ((uint64_t) e->e_rawsize < (phoff + fsz)) {
     96 		LIBELF_SET_ERROR(HEADER, 0);
     97 		return (NULL);
     98 	}
     99 
    100 	if ((msz = _libelf_msize(ELF_T_PHDR, ec, EV_CURRENT)) == 0)
    101 		return (NULL);
    102 
    103 	if ((phdr = calloc(phnum, msz)) == NULL) {
    104 		LIBELF_SET_ERROR(RESOURCE, 0);
    105 		return (NULL);
    106 	}
    107 
    108 	if (ec == ELFCLASS32)
    109 		e->e_u.e_elf.e_phdr.e_phdr32 = phdr;
    110 	else
    111 		e->e_u.e_elf.e_phdr.e_phdr64 = phdr;
    112 
    113 
    114 	xlator = _libelf_get_translator(ELF_T_PHDR, ELF_TOMEMORY, ec,
    115 	    _libelf_elfmachine(e));
    116 	(*xlator)(phdr, phnum * msz, e->e_rawfile + phoff, phnum,
    117 	    e->e_byteorder != LIBELF_PRIVATE(byteorder));
    118 
    119 	return (phdr);
    120 }
    121 
    122 void *
    123 _libelf_newphdr(Elf *e, unsigned int ec, size_t count)
    124 {
    125 	void *ehdr, *newphdr, *oldphdr;
    126 	size_t msz;
    127 
    128 	if (e == NULL) {
    129 		LIBELF_SET_ERROR(ARGUMENT, 0);
    130 		return (NULL);
    131 	}
    132 
    133 	if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL) {
    134 		LIBELF_SET_ERROR(SEQUENCE, 0);
    135 		return (NULL);
    136 	}
    137 
    138 	assert(e->e_class == ec);
    139 	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
    140 	assert(e->e_version == EV_CURRENT);
    141 
    142 	if ((msz = _libelf_msize(ELF_T_PHDR, ec, e->e_version)) == 0)
    143 		return (NULL);
    144 
    145 	newphdr = NULL;
    146 	if (count > 0 && (newphdr = calloc(count, msz)) == NULL) {
    147 		LIBELF_SET_ERROR(RESOURCE, 0);
    148 		return (NULL);
    149 	}
    150 
    151 	if (ec == ELFCLASS32) {
    152 		if ((oldphdr = (void *) e->e_u.e_elf.e_phdr.e_phdr32) != NULL)
    153 			free(oldphdr);
    154 		e->e_u.e_elf.e_phdr.e_phdr32 = (Elf32_Phdr *) newphdr;
    155 	} else {
    156 		if ((oldphdr = (void *) e->e_u.e_elf.e_phdr.e_phdr64) != NULL)
    157 			free(oldphdr);
    158 		e->e_u.e_elf.e_phdr.e_phdr64 = (Elf64_Phdr *) newphdr;
    159 	}
    160 
    161 	e->e_u.e_elf.e_nphdr = count;
    162 
    163 	elf_flagphdr(e, ELF_C_SET, ELF_F_DIRTY);
    164 
    165 	return (newphdr);
    166 }
    167