gelf_ehdr.c revision 4484
14484Sbinkertn@umich.edu/*- 24484Sbinkertn@umich.edu * Copyright (c) 2006 Joseph Koshy 34484Sbinkertn@umich.edu * All rights reserved. 44484Sbinkertn@umich.edu * 54484Sbinkertn@umich.edu * Redistribution and use in source and binary forms, with or without 64484Sbinkertn@umich.edu * modification, are permitted provided that the following conditions 74484Sbinkertn@umich.edu * are met: 84484Sbinkertn@umich.edu * 1. Redistributions of source code must retain the above copyright 94484Sbinkertn@umich.edu * notice, this list of conditions and the following disclaimer. 104484Sbinkertn@umich.edu * 2. Redistributions in binary form must reproduce the above copyright 114484Sbinkertn@umich.edu * notice, this list of conditions and the following disclaimer in the 124484Sbinkertn@umich.edu * documentation and/or other materials provided with the distribution. 134484Sbinkertn@umich.edu * 144484Sbinkertn@umich.edu * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 154484Sbinkertn@umich.edu * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 164484Sbinkertn@umich.edu * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 174484Sbinkertn@umich.edu * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 184484Sbinkertn@umich.edu * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 194484Sbinkertn@umich.edu * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 204484Sbinkertn@umich.edu * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 214484Sbinkertn@umich.edu * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 224484Sbinkertn@umich.edu * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 234484Sbinkertn@umich.edu * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 244484Sbinkertn@umich.edu * SUCH DAMAGE. 254484Sbinkertn@umich.edu */ 264484Sbinkertn@umich.edu 274484Sbinkertn@umich.edu#include <limits.h> 284484Sbinkertn@umich.edu 294484Sbinkertn@umich.edu#include <assert.h> 304484Sbinkertn@umich.edu#include "gelf.h" 314484Sbinkertn@umich.edu#include "libelf.h" 324484Sbinkertn@umich.edu#include <string.h> 334484Sbinkertn@umich.edu 344484Sbinkertn@umich.edu#include "_libelf.h" 354484Sbinkertn@umich.edu 364484Sbinkertn@umich.eduElf32_Ehdr * 374484Sbinkertn@umich.eduelf32_getehdr(Elf *e) 384484Sbinkertn@umich.edu{ 394484Sbinkertn@umich.edu return (_libelf_ehdr(e, ELFCLASS32, 0)); 404484Sbinkertn@umich.edu} 414484Sbinkertn@umich.edu 424484Sbinkertn@umich.eduElf64_Ehdr * 434484Sbinkertn@umich.eduelf64_getehdr(Elf *e) 444484Sbinkertn@umich.edu{ 454484Sbinkertn@umich.edu return (_libelf_ehdr(e, ELFCLASS64, 0)); 464484Sbinkertn@umich.edu} 474484Sbinkertn@umich.edu 484484Sbinkertn@umich.eduGElf_Ehdr * 494484Sbinkertn@umich.edugelf_getehdr(Elf *e, GElf_Ehdr *d) 504484Sbinkertn@umich.edu{ 514484Sbinkertn@umich.edu int ec; 524484Sbinkertn@umich.edu Elf32_Ehdr *eh32; 534484Sbinkertn@umich.edu Elf64_Ehdr *eh64; 544484Sbinkertn@umich.edu 554484Sbinkertn@umich.edu if (d == NULL || e == NULL || 564484Sbinkertn@umich.edu ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) { 574484Sbinkertn@umich.edu LIBELF_SET_ERROR(ARGUMENT, 0); 584484Sbinkertn@umich.edu return (NULL); 594484Sbinkertn@umich.edu } 604484Sbinkertn@umich.edu 614484Sbinkertn@umich.edu if (ec == ELFCLASS32) { 624484Sbinkertn@umich.edu if ((eh32 = _libelf_ehdr(e, ELFCLASS32, 0)) == NULL) 634484Sbinkertn@umich.edu return (NULL); 644484Sbinkertn@umich.edu 654484Sbinkertn@umich.edu (void) memcpy(d->e_ident, eh32->e_ident, sizeof(eh32->e_ident)); 664484Sbinkertn@umich.edu d->e_type = eh32->e_type; 674484Sbinkertn@umich.edu d->e_machine = eh32->e_machine; 684484Sbinkertn@umich.edu d->e_version = eh32->e_version; 694484Sbinkertn@umich.edu d->e_entry = eh32->e_entry; 704484Sbinkertn@umich.edu d->e_phoff = eh32->e_phoff; 714484Sbinkertn@umich.edu d->e_shoff = eh32->e_shoff; 724484Sbinkertn@umich.edu d->e_flags = eh32->e_flags; 734484Sbinkertn@umich.edu d->e_ehsize = eh32->e_ehsize; 744484Sbinkertn@umich.edu d->e_phentsize = eh32->e_phentsize; 754484Sbinkertn@umich.edu d->e_phnum = eh32->e_phnum; 764484Sbinkertn@umich.edu d->e_shentsize = eh32->e_shentsize; 774484Sbinkertn@umich.edu d->e_shnum = eh32->e_shnum; 784484Sbinkertn@umich.edu d->e_shstrndx = eh32->e_shstrndx; 794484Sbinkertn@umich.edu 804484Sbinkertn@umich.edu return (d); 814484Sbinkertn@umich.edu } 824484Sbinkertn@umich.edu 834484Sbinkertn@umich.edu assert(ec == ELFCLASS64); 844484Sbinkertn@umich.edu 854484Sbinkertn@umich.edu if ((eh64 = _libelf_ehdr(e, ELFCLASS64, 0)) == NULL) 864484Sbinkertn@umich.edu return (NULL); 874484Sbinkertn@umich.edu *d = *eh64; 884484Sbinkertn@umich.edu 894484Sbinkertn@umich.edu return (d); 904484Sbinkertn@umich.edu} 914484Sbinkertn@umich.edu 924484Sbinkertn@umich.eduElf32_Ehdr * 934484Sbinkertn@umich.eduelf32_newehdr(Elf *e) 944484Sbinkertn@umich.edu{ 954484Sbinkertn@umich.edu return (_libelf_ehdr(e, ELFCLASS32, 1)); 964484Sbinkertn@umich.edu} 974484Sbinkertn@umich.edu 984484Sbinkertn@umich.eduElf64_Ehdr * 994484Sbinkertn@umich.eduelf64_newehdr(Elf *e) 1004484Sbinkertn@umich.edu{ 1014484Sbinkertn@umich.edu return (_libelf_ehdr(e, ELFCLASS64, 1)); 1024484Sbinkertn@umich.edu} 1034484Sbinkertn@umich.edu 1044484Sbinkertn@umich.eduvoid * 1054484Sbinkertn@umich.edugelf_newehdr(Elf *e, int ec) 1064484Sbinkertn@umich.edu{ 1074484Sbinkertn@umich.edu if (e != NULL && 1084484Sbinkertn@umich.edu (ec == ELFCLASS32 || ec == ELFCLASS64)) 1094484Sbinkertn@umich.edu return (_libelf_ehdr(e, ec, 1)); 1104484Sbinkertn@umich.edu 1114484Sbinkertn@umich.edu LIBELF_SET_ERROR(ARGUMENT, 0); 1124484Sbinkertn@umich.edu return (NULL); 1134484Sbinkertn@umich.edu} 1144484Sbinkertn@umich.edu 1154484Sbinkertn@umich.eduint 1164484Sbinkertn@umich.edugelf_update_ehdr(Elf *e, GElf_Ehdr *s) 1174484Sbinkertn@umich.edu{ 1184484Sbinkertn@umich.edu int ec; 1194484Sbinkertn@umich.edu void *ehdr; 1204484Sbinkertn@umich.edu Elf32_Ehdr *eh32; 1214484Sbinkertn@umich.edu Elf64_Ehdr *eh64; 1224484Sbinkertn@umich.edu 1234484Sbinkertn@umich.edu if (s== NULL || e == NULL || e->e_kind != ELF_K_ELF || 1244484Sbinkertn@umich.edu ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) { 1254484Sbinkertn@umich.edu LIBELF_SET_ERROR(ARGUMENT, 0); 1264484Sbinkertn@umich.edu return (0); 1274484Sbinkertn@umich.edu } 1284484Sbinkertn@umich.edu 1294484Sbinkertn@umich.edu if (e->e_cmd == ELF_C_READ) { 1304484Sbinkertn@umich.edu LIBELF_SET_ERROR(MODE, 0); 1314484Sbinkertn@umich.edu return (0); 1324484Sbinkertn@umich.edu } 1334484Sbinkertn@umich.edu 1344484Sbinkertn@umich.edu if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL) 1354484Sbinkertn@umich.edu return (0); 1364484Sbinkertn@umich.edu 1374484Sbinkertn@umich.edu if (ec == ELFCLASS64) { 1384484Sbinkertn@umich.edu eh64 = (Elf64_Ehdr *) ehdr; 1394484Sbinkertn@umich.edu *eh64 = *s; 1404484Sbinkertn@umich.edu return (1); 1414484Sbinkertn@umich.edu } 1424484Sbinkertn@umich.edu 1434484Sbinkertn@umich.edu eh32 = (Elf32_Ehdr *) ehdr; 1444484Sbinkertn@umich.edu 1454484Sbinkertn@umich.edu (void) memcpy(eh32->e_ident, s->e_ident, sizeof(eh32->e_ident)); 1464484Sbinkertn@umich.edu 1474484Sbinkertn@umich.edu eh32->e_type = s->e_type; 1484484Sbinkertn@umich.edu eh32->e_machine = s->e_machine; 1494484Sbinkertn@umich.edu eh32->e_version = s->e_version; 1504484Sbinkertn@umich.edu LIBELF_COPY_U32(eh32, s, e_entry); 1514484Sbinkertn@umich.edu LIBELF_COPY_U32(eh32, s, e_phoff); 1524484Sbinkertn@umich.edu LIBELF_COPY_U32(eh32, s, e_shoff); 1534484Sbinkertn@umich.edu eh32->e_flags = s->e_flags; 1544484Sbinkertn@umich.edu eh32->e_ehsize = s->e_ehsize; 1554484Sbinkertn@umich.edu eh32->e_phentsize = s->e_phentsize; 1564484Sbinkertn@umich.edu eh32->e_phnum = s->e_phnum; 1574484Sbinkertn@umich.edu eh32->e_shentsize = s->e_shentsize; 1584484Sbinkertn@umich.edu eh32->e_shnum = s->e_shnum; 1594484Sbinkertn@umich.edu eh32->e_shstrndx = s->e_shstrndx; 1604484Sbinkertn@umich.edu 1614484Sbinkertn@umich.edu (void) elf_flagehdr(e, ELF_C_SET, ELF_F_DIRTY); 1624484Sbinkertn@umich.edu 1634484Sbinkertn@umich.edu return (1); 1644484Sbinkertn@umich.edu} 165