Home | History | Annotate | Line # | Download | only in libelf
      1 /*	$NetBSD: gelf_ehdr.c,v 1.7 2026/05/17 21:40:50 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 <limits.h>
     39 #include <stdint.h>
     40 #include <string.h>
     41 
     42 #include "_libelf.h"
     43 
     44 ELFTC_VCSID("Id: gelf_ehdr.c 4074 2025-01-07 15:34:21Z jkoshy");
     45 
     46 __RCSID("$NetBSD: gelf_ehdr.c,v 1.7 2026/05/17 21:40:50 jkoshy Exp $");
     47 
     48 Elf32_Ehdr *
     49 elf32_getehdr(Elf *e)
     50 {
     51 	return (_libelf_ehdr(e, ELFCLASS32, 0));
     52 }
     53 
     54 Elf64_Ehdr *
     55 elf64_getehdr(Elf *e)
     56 {
     57 	return (_libelf_ehdr(e, ELFCLASS64, 0));
     58 }
     59 
     60 GElf_Ehdr *
     61 gelf_getehdr(Elf *e, GElf_Ehdr *d)
     62 {
     63 	unsigned int ec;
     64 	Elf32_Ehdr *eh32;
     65 	Elf64_Ehdr *eh64;
     66 
     67 	if (d == NULL || e == NULL ||
     68 	    ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
     69 		LIBELF_SET_ERROR(ARGUMENT, 0);
     70 		return (NULL);
     71 	}
     72 
     73 	if (ec == ELFCLASS32) {
     74 		if ((eh32 = _libelf_ehdr(e, ELFCLASS32, 0)) == NULL)
     75 			return (NULL);
     76 
     77 		(void) memcpy(d->e_ident, eh32->e_ident,
     78 		    sizeof(eh32->e_ident));
     79 		d->e_type		= eh32->e_type;
     80 		d->e_machine		= eh32->e_machine;
     81 		d->e_version		= eh32->e_version;
     82 		d->e_entry		= eh32->e_entry;
     83 		d->e_phoff		= eh32->e_phoff;
     84 		d->e_shoff		= eh32->e_shoff;
     85 		d->e_flags		= eh32->e_flags;
     86 		d->e_ehsize		= eh32->e_ehsize;
     87 		d->e_phentsize		= eh32->e_phentsize;
     88 		d->e_phnum		= eh32->e_phnum;
     89 		d->e_shentsize		= eh32->e_shentsize;
     90 		d->e_shnum		= eh32->e_shnum;
     91 		d->e_shstrndx		= eh32->e_shstrndx;
     92 
     93 		return (d);
     94 	}
     95 
     96 	assert(ec == ELFCLASS64);
     97 
     98 	if ((eh64 = _libelf_ehdr(e, ELFCLASS64, 0)) == NULL)
     99 		return (NULL);
    100 	*d = *eh64;
    101 
    102 	return (d);
    103 }
    104 
    105 Elf32_Ehdr *
    106 elf32_newehdr(Elf *e)
    107 {
    108 	return (_libelf_ehdr(e, ELFCLASS32, 1));
    109 }
    110 
    111 Elf64_Ehdr *
    112 elf64_newehdr(Elf *e)
    113 {
    114 	return (_libelf_ehdr(e, ELFCLASS64, 1));
    115 }
    116 
    117 void *
    118 gelf_newehdr(Elf *e, int ec)
    119 {
    120 	if (e != NULL &&
    121 	    (ec == ELFCLASS32 || ec == ELFCLASS64))
    122 		return (_libelf_ehdr(e, (unsigned) ec, 1));
    123 
    124 	LIBELF_SET_ERROR(ARGUMENT, 0);
    125 	return (NULL);
    126 }
    127 
    128 int
    129 gelf_update_ehdr(Elf *e, GElf_Ehdr *s)
    130 {
    131 	void *ehdr;
    132 	unsigned int ec;
    133 	Elf32_Ehdr *eh32;
    134 	Elf64_Ehdr *eh64;
    135 
    136 	if (s== NULL || e == NULL || e->e_kind != ELF_K_ELF ||
    137 	    ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
    138 		LIBELF_SET_ERROR(ARGUMENT, 0);
    139 		return (0);
    140 	}
    141 
    142 	if (e->e_cmd == ELF_C_READ) {
    143 		LIBELF_SET_ERROR(MODE, 0);
    144 		return (0);
    145 	}
    146 
    147 	if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
    148 		return (0);
    149 
    150 	(void) elf_flagehdr(e, ELF_C_SET, ELF_F_DIRTY);
    151 
    152 	if (ec == ELFCLASS64) {
    153 		eh64 = (Elf64_Ehdr *) ehdr;
    154 		*eh64 = *s;
    155 		return (1);
    156 	}
    157 
    158 	eh32 = (Elf32_Ehdr *) ehdr;
    159 
    160 	(void) memcpy(eh32->e_ident, s->e_ident, sizeof(eh32->e_ident));
    161 
    162 	eh32->e_type      = s->e_type;
    163 	eh32->e_machine   = s->e_machine;
    164 	eh32->e_version   = s->e_version;
    165 	LIBELF_COPY_U32(eh32, s, e_entry);
    166 	LIBELF_COPY_U32(eh32, s, e_phoff);
    167 	LIBELF_COPY_U32(eh32, s, e_shoff);
    168 	eh32->e_flags     = s->e_flags;
    169 	eh32->e_ehsize    = s->e_ehsize;
    170 	eh32->e_phentsize = s->e_phentsize;
    171 	eh32->e_phnum     = s->e_phnum;
    172 	eh32->e_shentsize = s->e_shentsize;
    173 	eh32->e_shnum     = s->e_shnum;
    174 	eh32->e_shstrndx  = s->e_shstrndx;
    175 
    176 	return (1);
    177 }
    178