util.c revision 1.12
1/* $OpenBSD: util.c,v 1.12 2002/12/02 23:17:36 deraadt Exp $ */ 2 3/* 4 * Copyright (c) 1998 Per Fogelstrom, Opsycon AB 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed under OpenBSD by 17 * Per Fogelstrom, Opsycon AB, Sweden. 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 22 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 */ 34 35#include <sys/types.h> 36#include <sys/mman.h> 37#include <string.h> 38#include "archdep.h" 39 40/* 41 * Stack protector dummies. 42 * Ideally, a scheme to compile these stubs from libc should be used, but 43 * this would end up dragging too much code from libc here. 44 */ 45long __guard[8] = {0, 0, 0, 0, 0, 0, 0, 0}; 46void 47__stack_smash_handler(char func[], int damaged) 48{ 49 _dl_exit(127); 50} 51 52/* 53 * Static vars usable after bootstrapping. 54 */ 55static void *_dl_malloc_base; 56static void *_dl_malloc_pool = 0; 57static long *_dl_malloc_free = 0; 58 59char * 60_dl_strdup(const char *orig) 61{ 62 char *newstr; 63 int len; 64 65 len = _dl_strlen(orig)+1; 66 newstr = _dl_malloc(len); 67 _dl_strlcpy(newstr, orig, len); 68 return (newstr); 69} 70 71/* 72 * The following malloc/free code is a very simplified implementation 73 * of a malloc function. However, we do not need to be very complex here 74 * because we only free memory when 'dlclose()' is called and we can 75 * reuse at least the memory allocated for the object descriptor. We have 76 * one dynamic string allocated, the library name and it is likely that 77 * we can reuse that one to without a lot of complex colapsing code. 78 */ 79 80void * 81_dl_malloc(size_t size) 82{ 83 long *p, *t, *n; 84 85 size = (size + 8 + DL_MALLOC_ALIGN - 1) & ~(DL_MALLOC_ALIGN - 1); 86 87 if ((t = _dl_malloc_free) != 0) { /* Try free list first */ 88 n = (long *)&_dl_malloc_free; 89 while (t && t[-1] < size) { 90 n = t; 91 t = (long *)*t; 92 } 93 if (t) { 94 *n = *t; 95 _dl_memset(t, 0, t[-1] - 4); 96 return((void *)t); 97 } 98 } 99 if (_dl_malloc_pool == 0 || 100 _dl_malloc_pool + size > _dl_malloc_base + 4096) { 101 _dl_malloc_pool = (void *)_dl_mmap((void *)0, 4096, 102 PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0); 103 if (_dl_malloc_pool == 0 || _dl_malloc_pool == MAP_FAILED ) { 104 _dl_printf("Dynamic loader failure: malloc.\n"); 105 _dl_exit(7); 106 } 107 _dl_malloc_base = _dl_malloc_pool; 108 } 109 p = _dl_malloc_pool; 110 _dl_malloc_pool += size; 111 _dl_memset(p, 0, size); 112 *p = size; 113 return((void *)(p + 1)); 114} 115 116void 117_dl_free(void *p) 118{ 119 long *t = (long *)p; 120 121 *t = (long)_dl_malloc_free; 122 _dl_malloc_free = p; 123} 124