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/types.h>
28
29//#include <machine/elf.h>
30//#include <machine/endian.h>
31
32#include "libelf.h"
33
34#include "_libelf.h"
35
36struct align {
37        int a32;
38        int a64;
39};
40
41#ifdef	__GNUC__
42#define	MALIGN(N)	{					\
43                .a32 = __alignof__(Elf32_##N),			\
44                .a64 = __alignof__(Elf64_##N)			\
45        }
46#define	MALIGN64(V)	  {					\
47                .a32 = 0,					\
48                .a64 = __alignof__(Elf64_##V)			\
49        }
50#else
51#error	Need the __alignof__ builtin.
52#endif
53#define	UNSUPPORTED()	{					\
54                .a32 = 0,					\
55                .a64 = 0					\
56        }
57
58static struct align malign[ELF_T_NUM] = {
59        [ELF_T_ADDR]	= MALIGN(Addr),
60        [ELF_T_BYTE]	= { .a32 = 1, .a64 = 1 },
61        [ELF_T_CAP]	= MALIGN(Cap),
62        [ELF_T_DYN]	= MALIGN(Dyn),
63        [ELF_T_EHDR]	= MALIGN(Ehdr),
64        [ELF_T_HALF]	= MALIGN(Half),
65        [ELF_T_LWORD]	= MALIGN(Lword),
66        [ELF_T_MOVE]	= MALIGN(Move),
67        [ELF_T_MOVEP] 	= UNSUPPORTED(),
68        [ELF_T_NOTE]	= MALIGN(Nhdr),
69        [ELF_T_OFF]	= MALIGN(Off),
70        [ELF_T_PHDR]	= MALIGN(Phdr),
71        [ELF_T_REL]	= MALIGN(Rel),
72        [ELF_T_RELA]	= MALIGN(Rela),
73        [ELF_T_SHDR]	= MALIGN(Shdr),
74        [ELF_T_SWORD]	= MALIGN(Sword),
75        [ELF_T_SXWORD]	= MALIGN64(Sxword),
76        [ELF_T_SYM]	= MALIGN(Sym),
77        [ELF_T_SYMINFO]	= MALIGN(Syminfo),
78        [ELF_T_VDEF]	= MALIGN(Verdef),
79        [ELF_T_VNEED]	= MALIGN(Verneed),
80        [ELF_T_WORD]	= MALIGN(Word),
81        [ELF_T_XWORD]	= MALIGN64(Xword)
82};
83
84int
85_libelf_malign(Elf_Type t, int elfclass)
86{
87        if (t >= ELF_T_NUM || (int) t < 0)
88                return (0);
89
90        return (elfclass == ELFCLASS32 ? malign[t].a32 :
91            malign[t].a64);
92}
93
94#define	FALIGN(A32,A64)	{ .a32 = (A32), .a64 = (A64) }
95
96static struct align falign[ELF_T_NUM] = {
97        [ELF_T_ADDR]	= FALIGN(4,8),
98        [ELF_T_BYTE]	= FALIGN(1,1),
99        [ELF_T_CAP]	= FALIGN(4,8),
100        [ELF_T_DYN]	= FALIGN(4,8),
101        [ELF_T_EHDR]	= FALIGN(4,8),
102        [ELF_T_HALF]	= FALIGN(2,2),
103        [ELF_T_LWORD]	= FALIGN(8,8),
104        [ELF_T_MOVE]	= FALIGN(8,8),
105        [ELF_T_MOVEP] 	= UNSUPPORTED(),
106        [ELF_T_NOTE]	= FALIGN(4,4),
107        [ELF_T_OFF]	= FALIGN(4,8),
108        [ELF_T_PHDR]	= FALIGN(4,8),
109        [ELF_T_REL]	= FALIGN(4,8),
110        [ELF_T_RELA]	= FALIGN(4,8),
111        [ELF_T_SHDR]	= FALIGN(4,8),
112        [ELF_T_SWORD]	= FALIGN(4,4),
113        [ELF_T_SXWORD]	= FALIGN(0,8),
114        [ELF_T_SYM]	= FALIGN(4,8),
115        [ELF_T_SYMINFO]	= FALIGN(2,2),
116        [ELF_T_VDEF]	= FALIGN(4,4),
117        [ELF_T_VNEED]	= FALIGN(4,4),
118        [ELF_T_WORD]	= FALIGN(4,4),
119        [ELF_T_XWORD]	= FALIGN(0,8)
120};
121
122int
123_libelf_falign(Elf_Type t, int elfclass)
124{
125        if (t >= ELF_T_NUM || (int) t < 0)
126                return (0);
127
128        return (elfclass == ELFCLASS32 ? falign[t].a32 :
129            falign[t].a64);
130}
131