1 1.8 mrg /* $NetBSD: libelf_align.c,v 1.8 2025/12/26 07:07:35 mrg Exp $ */ 2 1.2 christos 3 1.1 christos /*- 4 1.1 christos * Copyright (c) 2006,2008 Joseph Koshy 5 1.1 christos * All rights reserved. 6 1.1 christos * 7 1.1 christos * Redistribution and use in source and binary forms, with or without 8 1.1 christos * modification, are permitted provided that the following conditions 9 1.1 christos * are met: 10 1.1 christos * 1. Redistributions of source code must retain the above copyright 11 1.1 christos * notice, this list of conditions and the following disclaimer. 12 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 christos * notice, this list of conditions and the following disclaimer in the 14 1.1 christos * documentation and/or other materials provided with the distribution. 15 1.1 christos * 16 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 1.1 christos * SUCH DAMAGE. 27 1.1 christos */ 28 1.1 christos 29 1.8 mrg #if HAVE_NBTOOL_CONFIG_H 30 1.8 mrg # include "nbtool_config.h" 31 1.8 mrg #endif 32 1.8 mrg 33 1.8 mrg #include <sys/cdefs.h> 34 1.8 mrg 35 1.1 christos #include <sys/types.h> 36 1.1 christos 37 1.1 christos #include <libelf.h> 38 1.1 christos 39 1.1 christos #include "_libelf.h" 40 1.1 christos 41 1.7 jkoshy ELFTC_VCSID("Id: libelf_align.c 4074 2025-01-07 15:34:21Z jkoshy"); 42 1.7 jkoshy 43 1.8 mrg __RCSID("$NetBSD: libelf_align.c,v 1.8 2025/12/26 07:07:35 mrg Exp $"); 44 1.1 christos 45 1.1 christos struct align { 46 1.4 christos unsigned int a32; 47 1.4 christos unsigned int a64; 48 1.1 christos }; 49 1.1 christos 50 1.3 christos #if defined(__GNUC__) || defined(__lint__) 51 1.1 christos #define MALIGN(N) { \ 52 1.1 christos .a32 = __alignof__(Elf32_##N), \ 53 1.1 christos .a64 = __alignof__(Elf64_##N) \ 54 1.1 christos } 55 1.1 christos #define MALIGN64(V) { \ 56 1.1 christos .a32 = 0, \ 57 1.1 christos .a64 = __alignof__(Elf64_##V) \ 58 1.1 christos } 59 1.1 christos #define MALIGN_WORD() { \ 60 1.1 christos .a32 = __alignof__(int32_t), \ 61 1.1 christos .a64 = __alignof__(int64_t) \ 62 1.1 christos } 63 1.6 christos #elif defined(__lint__) 64 1.6 christos #define MALIGN(N) { .a32 = 0, .a64 = 0 } 65 1.6 christos #define MALIGN64(N) { .a32 = 0, .a64 = 0 } 66 1.6 christos #define MALIGN_WORD(N) { .a32 = 0, .a64 = 0 } 67 1.1 christos #else 68 1.1 christos #error Need the __alignof__ builtin. 69 1.1 christos #endif 70 1.1 christos #define UNSUPPORTED() { \ 71 1.1 christos .a32 = 0, \ 72 1.1 christos .a64 = 0 \ 73 1.1 christos } 74 1.1 christos 75 1.1 christos static struct align malign[ELF_T_NUM] = { 76 1.1 christos [ELF_T_ADDR] = MALIGN(Addr), 77 1.1 christos [ELF_T_BYTE] = { .a32 = 1, .a64 = 1 }, 78 1.1 christos [ELF_T_CAP] = MALIGN(Cap), 79 1.1 christos [ELF_T_DYN] = MALIGN(Dyn), 80 1.1 christos [ELF_T_EHDR] = MALIGN(Ehdr), 81 1.1 christos [ELF_T_HALF] = MALIGN(Half), 82 1.1 christos [ELF_T_LWORD] = MALIGN(Lword), 83 1.1 christos [ELF_T_MOVE] = MALIGN(Move), 84 1.1 christos [ELF_T_MOVEP] = UNSUPPORTED(), 85 1.1 christos [ELF_T_NOTE] = MALIGN(Nhdr), 86 1.1 christos [ELF_T_OFF] = MALIGN(Off), 87 1.1 christos [ELF_T_PHDR] = MALIGN(Phdr), 88 1.1 christos [ELF_T_REL] = MALIGN(Rel), 89 1.1 christos [ELF_T_RELA] = MALIGN(Rela), 90 1.1 christos [ELF_T_SHDR] = MALIGN(Shdr), 91 1.1 christos [ELF_T_SWORD] = MALIGN(Sword), 92 1.1 christos [ELF_T_SXWORD] = MALIGN64(Sxword), 93 1.1 christos [ELF_T_SYM] = MALIGN(Sym), 94 1.1 christos [ELF_T_SYMINFO] = MALIGN(Syminfo), 95 1.1 christos [ELF_T_VDEF] = MALIGN(Verdef), 96 1.1 christos [ELF_T_VNEED] = MALIGN(Verneed), 97 1.1 christos [ELF_T_WORD] = MALIGN(Word), 98 1.1 christos [ELF_T_XWORD] = MALIGN64(Xword), 99 1.1 christos [ELF_T_GNUHASH] = MALIGN_WORD() 100 1.1 christos }; 101 1.1 christos 102 1.4 christos unsigned int 103 1.7 jkoshy _libelf_malign(Elf_Type t, unsigned int elfclass) 104 1.1 christos { 105 1.1 christos if (t >= ELF_T_NUM || (int) t < 0) 106 1.1 christos return (0); 107 1.1 christos 108 1.1 christos return (elfclass == ELFCLASS32 ? malign[t].a32 : 109 1.1 christos malign[t].a64); 110 1.1 christos } 111 1.1 christos 112 1.1 christos #define FALIGN(A32,A64) { .a32 = (A32), .a64 = (A64) } 113 1.1 christos 114 1.1 christos static struct align falign[ELF_T_NUM] = { 115 1.1 christos [ELF_T_ADDR] = FALIGN(4,8), 116 1.1 christos [ELF_T_BYTE] = FALIGN(1,1), 117 1.1 christos [ELF_T_CAP] = FALIGN(4,8), 118 1.1 christos [ELF_T_DYN] = FALIGN(4,8), 119 1.1 christos [ELF_T_EHDR] = FALIGN(4,8), 120 1.1 christos [ELF_T_HALF] = FALIGN(2,2), 121 1.1 christos [ELF_T_LWORD] = FALIGN(8,8), 122 1.1 christos [ELF_T_MOVE] = FALIGN(8,8), 123 1.1 christos [ELF_T_MOVEP] = UNSUPPORTED(), 124 1.6 christos [ELF_T_NOTE] = FALIGN(4,4), 125 1.1 christos [ELF_T_OFF] = FALIGN(4,8), 126 1.1 christos [ELF_T_PHDR] = FALIGN(4,8), 127 1.1 christos [ELF_T_REL] = FALIGN(4,8), 128 1.1 christos [ELF_T_RELA] = FALIGN(4,8), 129 1.1 christos [ELF_T_SHDR] = FALIGN(4,8), 130 1.1 christos [ELF_T_SWORD] = FALIGN(4,4), 131 1.1 christos [ELF_T_SXWORD] = FALIGN(0,8), 132 1.1 christos [ELF_T_SYM] = FALIGN(4,8), 133 1.1 christos [ELF_T_SYMINFO] = FALIGN(2,2), 134 1.1 christos [ELF_T_VDEF] = FALIGN(4,4), 135 1.1 christos [ELF_T_VNEED] = FALIGN(4,4), 136 1.1 christos [ELF_T_WORD] = FALIGN(4,4), 137 1.1 christos [ELF_T_XWORD] = FALIGN(0,8), 138 1.1 christos [ELF_T_GNUHASH] = FALIGN(4,8) 139 1.1 christos }; 140 1.1 christos 141 1.4 christos unsigned int 142 1.7 jkoshy _libelf_falign(Elf_Type t, unsigned int elfclass) 143 1.1 christos { 144 1.1 christos if (t >= ELF_T_NUM || (int) t < 0) 145 1.1 christos return (0); 146 1.1 christos 147 1.1 christos return (elfclass == ELFCLASS32 ? falign[t].a32 : 148 1.1 christos falign[t].a64); 149 1.1 christos } 150