xref: /netbsd-src/external/bsd/elftoolchain/dist/libelf/libelf_align.c (revision 5ac3bc719ce6e70593039505b491894133237d12)
1*5ac3bc71Schristos /*	$NetBSD: libelf_align.c,v 1.6 2024/03/03 17:37:34 christos Exp $	*/
2e81373b4Schristos 
39dd9d0cfSchristos /*-
49dd9d0cfSchristos  * Copyright (c) 2006,2008 Joseph Koshy
59dd9d0cfSchristos  * All rights reserved.
69dd9d0cfSchristos  *
79dd9d0cfSchristos  * Redistribution and use in source and binary forms, with or without
89dd9d0cfSchristos  * modification, are permitted provided that the following conditions
99dd9d0cfSchristos  * are met:
109dd9d0cfSchristos  * 1. Redistributions of source code must retain the above copyright
119dd9d0cfSchristos  *    notice, this list of conditions and the following disclaimer.
129dd9d0cfSchristos  * 2. Redistributions in binary form must reproduce the above copyright
139dd9d0cfSchristos  *    notice, this list of conditions and the following disclaimer in the
149dd9d0cfSchristos  *    documentation and/or other materials provided with the distribution.
159dd9d0cfSchristos  *
169dd9d0cfSchristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
179dd9d0cfSchristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
189dd9d0cfSchristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199dd9d0cfSchristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
209dd9d0cfSchristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
219dd9d0cfSchristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
229dd9d0cfSchristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
239dd9d0cfSchristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
249dd9d0cfSchristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
259dd9d0cfSchristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
269dd9d0cfSchristos  * SUCH DAMAGE.
279dd9d0cfSchristos  */
289dd9d0cfSchristos 
29e81373b4Schristos #if HAVE_NBTOOL_CONFIG_H
30e81373b4Schristos # include "nbtool_config.h"
31e81373b4Schristos #endif
32e81373b4Schristos 
339dd9d0cfSchristos #include <sys/cdefs.h>
349dd9d0cfSchristos 
359dd9d0cfSchristos #include <sys/types.h>
369dd9d0cfSchristos 
379dd9d0cfSchristos #include <libelf.h>
389dd9d0cfSchristos 
399dd9d0cfSchristos #include "_libelf.h"
409dd9d0cfSchristos 
41*5ac3bc71Schristos __RCSID("$NetBSD: libelf_align.c,v 1.6 2024/03/03 17:37:34 christos Exp $");
42*5ac3bc71Schristos ELFTC_VCSID("Id: libelf_align.c 3977 2022-05-01 06:45:34Z jkoshy");
439dd9d0cfSchristos 
449dd9d0cfSchristos struct align {
4542bd3019Schristos 	unsigned int a32;
4642bd3019Schristos 	unsigned int a64;
479dd9d0cfSchristos };
489dd9d0cfSchristos 
4990382180Schristos #if defined(__GNUC__) || defined(__lint__)
509dd9d0cfSchristos #define	MALIGN(N)	{					\
519dd9d0cfSchristos 		.a32 = __alignof__(Elf32_##N),			\
529dd9d0cfSchristos 		.a64 = __alignof__(Elf64_##N)			\
539dd9d0cfSchristos 	}
549dd9d0cfSchristos #define	MALIGN64(V)	  {					\
559dd9d0cfSchristos 		.a32 = 0,					\
569dd9d0cfSchristos 		.a64 = __alignof__(Elf64_##V)			\
579dd9d0cfSchristos 	}
589dd9d0cfSchristos #define	MALIGN_WORD()	{					\
599dd9d0cfSchristos 		.a32 = __alignof__(int32_t),			\
609dd9d0cfSchristos 		.a64 = __alignof__(int64_t)			\
619dd9d0cfSchristos 	    }
62*5ac3bc71Schristos #elif defined(__lint__)
63*5ac3bc71Schristos #define MALIGN(N)	{ .a32 = 0, .a64 = 0 }
64*5ac3bc71Schristos #define MALIGN64(N)	{ .a32 = 0, .a64 = 0 }
65*5ac3bc71Schristos #define MALIGN_WORD(N)	{ .a32 = 0, .a64 = 0 }
669dd9d0cfSchristos #else
679dd9d0cfSchristos #error	Need the __alignof__ builtin.
689dd9d0cfSchristos #endif
699dd9d0cfSchristos #define	UNSUPPORTED()	{					\
709dd9d0cfSchristos 		.a32 = 0,					\
719dd9d0cfSchristos 		.a64 = 0					\
729dd9d0cfSchristos 	}
739dd9d0cfSchristos 
749dd9d0cfSchristos static struct align malign[ELF_T_NUM] = {
759dd9d0cfSchristos 	[ELF_T_ADDR]	= MALIGN(Addr),
769dd9d0cfSchristos 	[ELF_T_BYTE]	= { .a32 = 1, .a64 = 1 },
779dd9d0cfSchristos 	[ELF_T_CAP]	= MALIGN(Cap),
789dd9d0cfSchristos 	[ELF_T_DYN]	= MALIGN(Dyn),
799dd9d0cfSchristos 	[ELF_T_EHDR]	= MALIGN(Ehdr),
809dd9d0cfSchristos 	[ELF_T_HALF]	= MALIGN(Half),
819dd9d0cfSchristos 	[ELF_T_LWORD]	= MALIGN(Lword),
829dd9d0cfSchristos 	[ELF_T_MOVE]	= MALIGN(Move),
839dd9d0cfSchristos 	[ELF_T_MOVEP] 	= UNSUPPORTED(),
849dd9d0cfSchristos 	[ELF_T_NOTE]	= MALIGN(Nhdr),
859dd9d0cfSchristos 	[ELF_T_OFF]	= MALIGN(Off),
869dd9d0cfSchristos 	[ELF_T_PHDR]	= MALIGN(Phdr),
879dd9d0cfSchristos 	[ELF_T_REL]	= MALIGN(Rel),
889dd9d0cfSchristos 	[ELF_T_RELA]	= MALIGN(Rela),
899dd9d0cfSchristos 	[ELF_T_SHDR]	= MALIGN(Shdr),
909dd9d0cfSchristos 	[ELF_T_SWORD]	= MALIGN(Sword),
919dd9d0cfSchristos 	[ELF_T_SXWORD]	= MALIGN64(Sxword),
929dd9d0cfSchristos 	[ELF_T_SYM]	= MALIGN(Sym),
939dd9d0cfSchristos 	[ELF_T_SYMINFO]	= MALIGN(Syminfo),
949dd9d0cfSchristos 	[ELF_T_VDEF]	= MALIGN(Verdef),
959dd9d0cfSchristos 	[ELF_T_VNEED]	= MALIGN(Verneed),
969dd9d0cfSchristos 	[ELF_T_WORD]	= MALIGN(Word),
979dd9d0cfSchristos 	[ELF_T_XWORD]	= MALIGN64(Xword),
989dd9d0cfSchristos 	[ELF_T_GNUHASH] = MALIGN_WORD()
999dd9d0cfSchristos };
1009dd9d0cfSchristos 
10142bd3019Schristos unsigned int
_libelf_malign(Elf_Type t,int elfclass)1029dd9d0cfSchristos _libelf_malign(Elf_Type t, int elfclass)
1039dd9d0cfSchristos {
1049dd9d0cfSchristos 	if (t >= ELF_T_NUM || (int) t < 0)
1059dd9d0cfSchristos 		return (0);
1069dd9d0cfSchristos 
1079dd9d0cfSchristos 	return (elfclass == ELFCLASS32 ? malign[t].a32 :
1089dd9d0cfSchristos 	    malign[t].a64);
1099dd9d0cfSchristos }
1109dd9d0cfSchristos 
1119dd9d0cfSchristos #define	FALIGN(A32,A64)	{ .a32 = (A32), .a64 = (A64) }
1129dd9d0cfSchristos 
1139dd9d0cfSchristos static struct align falign[ELF_T_NUM] = {
1149dd9d0cfSchristos 	[ELF_T_ADDR]	= FALIGN(4,8),
1159dd9d0cfSchristos 	[ELF_T_BYTE]	= FALIGN(1,1),
1169dd9d0cfSchristos 	[ELF_T_CAP]	= FALIGN(4,8),
1179dd9d0cfSchristos 	[ELF_T_DYN]	= FALIGN(4,8),
1189dd9d0cfSchristos 	[ELF_T_EHDR]	= FALIGN(4,8),
1199dd9d0cfSchristos 	[ELF_T_HALF]	= FALIGN(2,2),
1209dd9d0cfSchristos 	[ELF_T_LWORD]	= FALIGN(8,8),
1219dd9d0cfSchristos 	[ELF_T_MOVE]	= FALIGN(8,8),
1229dd9d0cfSchristos 	[ELF_T_MOVEP] 	= UNSUPPORTED(),
123*5ac3bc71Schristos 	[ELF_T_NOTE]	= FALIGN(4,4),
1249dd9d0cfSchristos 	[ELF_T_OFF]	= FALIGN(4,8),
1259dd9d0cfSchristos 	[ELF_T_PHDR]	= FALIGN(4,8),
1269dd9d0cfSchristos 	[ELF_T_REL]	= FALIGN(4,8),
1279dd9d0cfSchristos 	[ELF_T_RELA]	= FALIGN(4,8),
1289dd9d0cfSchristos 	[ELF_T_SHDR]	= FALIGN(4,8),
1299dd9d0cfSchristos 	[ELF_T_SWORD]	= FALIGN(4,4),
1309dd9d0cfSchristos 	[ELF_T_SXWORD]	= FALIGN(0,8),
1319dd9d0cfSchristos 	[ELF_T_SYM]	= FALIGN(4,8),
1329dd9d0cfSchristos 	[ELF_T_SYMINFO]	= FALIGN(2,2),
1339dd9d0cfSchristos 	[ELF_T_VDEF]	= FALIGN(4,4),
1349dd9d0cfSchristos 	[ELF_T_VNEED]	= FALIGN(4,4),
1359dd9d0cfSchristos 	[ELF_T_WORD]	= FALIGN(4,4),
1369dd9d0cfSchristos 	[ELF_T_XWORD]	= FALIGN(0,8),
1379dd9d0cfSchristos 	[ELF_T_GNUHASH] = FALIGN(4,8)
1389dd9d0cfSchristos };
1399dd9d0cfSchristos 
14042bd3019Schristos unsigned int
_libelf_falign(Elf_Type t,int elfclass)1419dd9d0cfSchristos _libelf_falign(Elf_Type t, int elfclass)
1429dd9d0cfSchristos {
1439dd9d0cfSchristos 	if (t >= ELF_T_NUM || (int) t < 0)
1449dd9d0cfSchristos 		return (0);
1459dd9d0cfSchristos 
1469dd9d0cfSchristos 	return (elfclass == ELFCLASS32 ? falign[t].a32 :
1479dd9d0cfSchristos 	    falign[t].a64);
1489dd9d0cfSchristos }
149