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