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 28#include <sys/param.h>
| 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 28#include <sys/param.h>
|
| 29#ifdef __sun 30#include <sys/sysmacros.h> 31#endif
|
29 30#include <assert.h> 31#include "gelf.h" 32 33#include "_libelf.h" 34 35/* 36 * Convert an ELF section#,offset pair to a string pointer. 37 */ 38 39char * 40elf_strptr(Elf *e, size_t scndx, size_t offset) 41{ 42 Elf_Scn *s; 43 Elf_Data *d; 44 size_t alignment, count; 45 GElf_Shdr shdr; 46 47 if (e == NULL || e->e_kind != ELF_K_ELF) { 48 LIBELF_SET_ERROR(ARGUMENT, 0); 49 return (NULL); 50 } 51 52 if ((s = elf_getscn(e, scndx)) == NULL || 53 gelf_getshdr(s, &shdr) == NULL) 54 return (NULL); 55 56 if (shdr.sh_type != SHT_STRTAB || 57 offset >= shdr.sh_size) { 58 LIBELF_SET_ERROR(ARGUMENT, 0); 59 return (NULL); 60 } 61 62 d = NULL; 63 if (e->e_flags & ELF_F_LAYOUT) { 64 65 /* 66 * The application is taking responsibility for the 67 * ELF object's layout, so we can directly translate 68 * an offset to a `char *' address using the `d_off' 69 * members of Elf_Data descriptors. 70 */ 71 while ((d = elf_getdata(s, d)) != NULL) { 72 73 if (d->d_buf == 0 || d->d_size == 0) 74 continue; 75 76 if (d->d_type != ELF_T_BYTE) { 77 LIBELF_SET_ERROR(DATA, 0); 78 return (NULL); 79 } 80 81 if (offset >= d->d_off && 82 offset < d->d_off + d->d_size) 83 return ((char *) d->d_buf + offset - d->d_off); 84 } 85 } else { 86 /* 87 * Otherwise, the `d_off' members are not useable and 88 * we need to compute offsets ourselves, taking into 89 * account 'holes' in coverage of the section introduced 90 * by alignment requirements. 91 */ 92 count = (size_t) 0; /* cumulative count of bytes seen */ 93 while ((d = elf_getdata(s, d)) != NULL && count <= offset) { 94 95 if (d->d_buf == NULL || d->d_size == 0) 96 continue; 97 98 if (d->d_type != ELF_T_BYTE) { 99 LIBELF_SET_ERROR(DATA, 0); 100 return (NULL); 101 } 102 103 if ((alignment = d->d_align) > 1) { 104 if ((alignment & (alignment - 1)) != 0) { 105 LIBELF_SET_ERROR(DATA, 0); 106 return (NULL); 107 } 108 count = roundup(count, alignment); 109 } 110 111 if (offset < count) { 112 /* offset starts in the 'hole' */ 113 LIBELF_SET_ERROR(ARGUMENT, 0); 114 return (NULL); 115 } 116 117 if (offset < count + d->d_size) { 118 if (d->d_buf != NULL) 119 return ((char *) d->d_buf + 120 offset - count); 121 LIBELF_SET_ERROR(DATA, 0); 122 return (NULL); 123 } 124 125 count += d->d_size; 126 } 127 } 128 129 LIBELF_SET_ERROR(ARGUMENT, 0); 130 return (NULL); 131}
| 32 33#include <assert.h> 34#include "gelf.h" 35 36#include "_libelf.h" 37 38/* 39 * Convert an ELF section#,offset pair to a string pointer. 40 */ 41 42char * 43elf_strptr(Elf *e, size_t scndx, size_t offset) 44{ 45 Elf_Scn *s; 46 Elf_Data *d; 47 size_t alignment, count; 48 GElf_Shdr shdr; 49 50 if (e == NULL || e->e_kind != ELF_K_ELF) { 51 LIBELF_SET_ERROR(ARGUMENT, 0); 52 return (NULL); 53 } 54 55 if ((s = elf_getscn(e, scndx)) == NULL || 56 gelf_getshdr(s, &shdr) == NULL) 57 return (NULL); 58 59 if (shdr.sh_type != SHT_STRTAB || 60 offset >= shdr.sh_size) { 61 LIBELF_SET_ERROR(ARGUMENT, 0); 62 return (NULL); 63 } 64 65 d = NULL; 66 if (e->e_flags & ELF_F_LAYOUT) { 67 68 /* 69 * The application is taking responsibility for the 70 * ELF object's layout, so we can directly translate 71 * an offset to a `char *' address using the `d_off' 72 * members of Elf_Data descriptors. 73 */ 74 while ((d = elf_getdata(s, d)) != NULL) { 75 76 if (d->d_buf == 0 || d->d_size == 0) 77 continue; 78 79 if (d->d_type != ELF_T_BYTE) { 80 LIBELF_SET_ERROR(DATA, 0); 81 return (NULL); 82 } 83 84 if (offset >= d->d_off && 85 offset < d->d_off + d->d_size) 86 return ((char *) d->d_buf + offset - d->d_off); 87 } 88 } else { 89 /* 90 * Otherwise, the `d_off' members are not useable and 91 * we need to compute offsets ourselves, taking into 92 * account 'holes' in coverage of the section introduced 93 * by alignment requirements. 94 */ 95 count = (size_t) 0; /* cumulative count of bytes seen */ 96 while ((d = elf_getdata(s, d)) != NULL && count <= offset) { 97 98 if (d->d_buf == NULL || d->d_size == 0) 99 continue; 100 101 if (d->d_type != ELF_T_BYTE) { 102 LIBELF_SET_ERROR(DATA, 0); 103 return (NULL); 104 } 105 106 if ((alignment = d->d_align) > 1) { 107 if ((alignment & (alignment - 1)) != 0) { 108 LIBELF_SET_ERROR(DATA, 0); 109 return (NULL); 110 } 111 count = roundup(count, alignment); 112 } 113 114 if (offset < count) { 115 /* offset starts in the 'hole' */ 116 LIBELF_SET_ERROR(ARGUMENT, 0); 117 return (NULL); 118 } 119 120 if (offset < count + d->d_size) { 121 if (d->d_buf != NULL) 122 return ((char *) d->d_buf + 123 offset - count); 124 LIBELF_SET_ERROR(DATA, 0); 125 return (NULL); 126 } 127 128 count += d->d_size; 129 } 130 } 131 132 LIBELF_SET_ERROR(ARGUMENT, 0); 133 return (NULL); 134}
|