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