elf_shstrndx.c revision 164190
1/*- 2 * Copyright (c) 2006 Joseph Koshy 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: head/lib/libelf/elf_shstrndx.c 164190 2006-11-11 17:16:35Z jkoshy $"); 29 30#include <ar.h> 31#include <libelf.h> 32 33#include "_libelf.h" 34 35/* 36 * Helpers to get/set the e_shstrndx field of the ELF header. 37 */ 38 39int 40_libelf_getshstrndx(Elf *e, void *eh, int ec, size_t *strndx) 41{ 42 Elf_Scn *scn; 43 void *sh; 44 size_t n; 45 46 n = (ec == ELFCLASS32) ? ((Elf32_Ehdr *) eh)->e_shstrndx : 47 ((Elf64_Ehdr *) eh)->e_shstrndx; 48 49 if (n < SHN_LORESERVE) { 50 *strndx = n; 51 return (1); 52 } 53 54 if ((scn = elf_getscn(e, (size_t) SHN_UNDEF)) == NULL) 55 return (0); 56 if ((sh = _libelf_getshdr(scn, ec)) == NULL) 57 return (0); 58 59 if (ec == ELFCLASS32) 60 *strndx = ((Elf32_Shdr *) sh)->sh_link; 61 else 62 *strndx = ((Elf64_Shdr *) sh)->sh_link; 63 64 return (1); 65} 66 67int 68_libelf_setshstrndx(Elf *e, void *eh, int ec, size_t strndx) 69{ 70 Elf_Scn *scn; 71 void *sh; 72 73 if (strndx < SHN_LORESERVE) { 74 if (ec == ELFCLASS32) 75 ((Elf32_Ehdr *) eh)->e_shstrndx = strndx; 76 else 77 ((Elf64_Ehdr *) eh)->e_shstrndx = strndx; 78 return (1); 79 } 80 81 if ((scn = elf_getscn(e, (size_t) SHN_UNDEF)) == NULL) 82 return (0); 83 if ((sh = _libelf_getshdr(scn, ec)) == NULL) 84 return (0); 85 86 if (ec == ELFCLASS32) { 87 ((Elf32_Ehdr *) eh)->e_shstrndx = SHN_XINDEX; 88 ((Elf32_Shdr *) sh)->sh_link = strndx; 89 } else { 90 ((Elf64_Ehdr *) eh)->e_shstrndx = SHN_XINDEX; 91 ((Elf64_Shdr *) sh)->sh_link = strndx; 92 } 93 94 return (1); 95} 96 97int 98elf_getshstrndx(Elf *e, size_t *strndx) 99{ 100 void *eh; 101 int ec; 102 103 if (e == NULL || e->e_kind != ELF_K_ELF || 104 ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64) || 105 ((eh = _libelf_ehdr(e, ec, 0)) == NULL)) { 106 LIBELF_SET_ERROR(ARGUMENT, 0); 107 return (0); 108 } 109 110 return (_libelf_getshstrndx(e, eh, ec, strndx)); 111} 112 113int 114elf_setshstrndx(Elf *e, size_t strndx) 115{ 116 void *eh; 117 int ec; 118 119 if (e == NULL || e->e_kind != ELF_K_ELF || 120 ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64) || 121 ((eh = _libelf_ehdr(e, ec, 0)) == NULL)) { 122 LIBELF_SET_ERROR(ARGUMENT, 0); 123 return (0); 124 } 125 126 return (_libelf_setshstrndx(e, eh, ec, strndx)); 127} 128