1 /* $NetBSD: libelf_align.c,v 1.3 2009/12/19 07:54:32 thorpej Exp $ */
4 * Copyright (c) 2006 Joseph Koshy
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 /* __FBSDID("$FreeBSD: src/lib/libelf/libelf_align.c,v 1.3.2.1.2.1 2009/10/25 01:10:29 kensmith Exp $"); */
32 #include <sys/types.h>
34 #include <machine/endian.h>
47 .a32 = sizeof(Elf32_##N), \
48 .a64 = sizeof(Elf64_##N) \
50 #define MALIGN64(V) { \
52 .a64 = sizeof(Elf64_##V) \
54 #elif defined(__GNUC__)
56 .a32 = __alignof__(Elf32_##N), \
57 .a64 = __alignof__(Elf64_##N) \
59 #define MALIGN64(V) { \
61 .a64 = __alignof__(Elf64_##V) \
64 #error Need the __alignof__ builtin.
66 #define UNSUPPORTED() { \
71 static struct align malign
[ELF_T_NUM
] = {
72 [ELF_T_ADDR
] = MALIGN(Addr
),
73 [ELF_T_BYTE
] = { .a32
= 1, .a64
= 1 },
74 #if defined(__LIBELF_HAVE_ELF_CAP)
75 [ELF_T_CAP
] = MALIGN(Cap
),
77 [ELF_T_DYN
] = MALIGN(Dyn
),
78 [ELF_T_EHDR
] = MALIGN(Ehdr
),
79 [ELF_T_HALF
] = MALIGN(Half
),
80 #if defined(__LIBELF_HAVE_ELF_MOVE)
81 [ELF_T_LWORD
] = MALIGN(Lword
),
82 [ELF_T_MOVE
] = MALIGN(Move
),
84 [ELF_T_MOVEP
] = UNSUPPORTED(),
85 #if defined(__LIBELF_HAVE_ELF_NOTE)
86 [ELF_T_NOTE
] = MALIGN(Nhdr
),
88 [ELF_T_OFF
] = MALIGN(Off
),
89 [ELF_T_PHDR
] = MALIGN(Phdr
),
90 [ELF_T_REL
] = MALIGN(Rel
),
91 [ELF_T_RELA
] = MALIGN(Rela
),
92 [ELF_T_SHDR
] = MALIGN(Shdr
),
93 [ELF_T_SWORD
] = MALIGN(Sword
),
94 [ELF_T_SXWORD
] = MALIGN64(Sxword
),
95 [ELF_T_SYM
] = MALIGN(Sym
),
96 #if defined(__LIBELF_HAVE_ELF_SYMINFO)
97 [ELF_T_SYMINFO
] = MALIGN(Syminfo
),
99 #if defined(__LIBELF_HAVE_ELF_VERS)
100 [ELF_T_VDEF
] = MALIGN(Verdef
),
101 [ELF_T_VNEED
] = MALIGN(Verneed
),
103 [ELF_T_WORD
] = MALIGN(Word
),
104 [ELF_T_XWORD
] = MALIGN64(Xword
)
108 _libelf_malign(Elf_Type t
, int elfclass
)
110 if (t
>= ELF_T_NUM
|| (int) t
< 0)
113 return (elfclass
== ELFCLASS32
? malign
[t
].a32
:
117 #define FALIGN(A32,A64) { .a32 = (A32), .a64 = (A64) }
119 static struct align falign
[ELF_T_NUM
] = {
120 [ELF_T_ADDR
] = FALIGN(4,8),
121 [ELF_T_BYTE
] = FALIGN(1,1),
122 #if defined(__LIBELF_HAVE_ELF_CAP)
123 [ELF_T_CAP
] = FALIGN(4,8),
125 [ELF_T_DYN
] = FALIGN(4,8),
126 [ELF_T_EHDR
] = FALIGN(4,8),
127 [ELF_T_HALF
] = FALIGN(2,2),
128 #if defined(__LIBELF_HAVE_ELF_MOVE)
129 [ELF_T_LWORD
] = FALIGN(8,8),
130 [ELF_T_MOVE
] = FALIGN(8,8),
132 [ELF_T_MOVEP
] = UNSUPPORTED(),
133 #if defined(__LIBELF_HAVE_ELF_NOTE)
134 [ELF_T_NOTE
] = FALIGN(1,1),
136 [ELF_T_OFF
] = FALIGN(4,8),
137 [ELF_T_PHDR
] = FALIGN(4,8),
138 [ELF_T_REL
] = FALIGN(4,8),
139 [ELF_T_RELA
] = FALIGN(4,8),
140 [ELF_T_SHDR
] = FALIGN(4,8),
141 [ELF_T_SWORD
] = FALIGN(4,4),
142 [ELF_T_SXWORD
] = FALIGN(0,8),
143 [ELF_T_SYM
] = FALIGN(4,8),
144 #if defined(__LIBELF_HAVE_ELF_SYMINFO)
145 [ELF_T_SYMINFO
] = FALIGN(2,2),
147 #if defined(__LIBELF_HAVE_ELF_VERS)
148 [ELF_T_VDEF
] = FALIGN(4,4),
149 [ELF_T_VNEED
] = FALIGN(4,4),
151 [ELF_T_WORD
] = FALIGN(4,4),
152 [ELF_T_XWORD
] = FALIGN(0,8)
156 _libelf_falign(Elf_Type t
, int elfclass
)
158 if (t
>= ELF_T_NUM
|| (int) t
< 0)
161 return (elfclass
== ELFCLASS32
? falign
[t
].a32
: