xref: /freebsd/contrib/elftoolchain/libelf/libelf_data.c (revision d003e0d7fe0d3a9b4b2c5835bb3f0f6faf3ab538)
12de3b87aSKai Wang /*-
22de3b87aSKai Wang  * Copyright (c) 2006,2008 Joseph Koshy
32de3b87aSKai Wang  * All rights reserved.
42de3b87aSKai Wang  *
52de3b87aSKai Wang  * Redistribution and use in source and binary forms, with or without
62de3b87aSKai Wang  * modification, are permitted provided that the following conditions
72de3b87aSKai Wang  * are met:
82de3b87aSKai Wang  * 1. Redistributions of source code must retain the above copyright
92de3b87aSKai Wang  *    notice, this list of conditions and the following disclaimer.
102de3b87aSKai Wang  * 2. Redistributions in binary form must reproduce the above copyright
112de3b87aSKai Wang  *    notice, this list of conditions and the following disclaimer in the
122de3b87aSKai Wang  *    documentation and/or other materials provided with the distribution.
132de3b87aSKai Wang  *
142de3b87aSKai Wang  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
152de3b87aSKai Wang  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
162de3b87aSKai Wang  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
172de3b87aSKai Wang  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
182de3b87aSKai Wang  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
192de3b87aSKai Wang  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
202de3b87aSKai Wang  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
212de3b87aSKai Wang  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
222de3b87aSKai Wang  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
232de3b87aSKai Wang  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
242de3b87aSKai Wang  * SUCH DAMAGE.
252de3b87aSKai Wang  */
262de3b87aSKai Wang 
272de3b87aSKai Wang #include <libelf.h>
282de3b87aSKai Wang 
292de3b87aSKai Wang #include "_libelf.h"
302de3b87aSKai Wang 
31*d003e0d7SEd Maste ELFTC_VCSID("$Id: libelf_data.c 3737 2019-05-05 14:49:50Z jkoshy $");
322de3b87aSKai Wang 
332de3b87aSKai Wang int
_libelf_xlate_shtype(uint32_t sht)342de3b87aSKai Wang _libelf_xlate_shtype(uint32_t sht)
352de3b87aSKai Wang {
36cf781b2eSEd Maste 	/*
37cf781b2eSEd Maste 	 * Look for known section types.
38cf781b2eSEd Maste 	 */
392de3b87aSKai Wang 	switch (sht) {
402de3b87aSKai Wang 	case SHT_DYNAMIC:
412de3b87aSKai Wang 		return (ELF_T_DYN);
422de3b87aSKai Wang 	case SHT_DYNSYM:
432de3b87aSKai Wang 		return (ELF_T_SYM);
442de3b87aSKai Wang 	case SHT_FINI_ARRAY:
452de3b87aSKai Wang 		return (ELF_T_ADDR);
462de3b87aSKai Wang 	case SHT_GNU_HASH:
472de3b87aSKai Wang 		return (ELF_T_GNUHASH);
482de3b87aSKai Wang 	case SHT_GNU_LIBLIST:
492de3b87aSKai Wang 		return (ELF_T_WORD);
502de3b87aSKai Wang 	case SHT_GROUP:
512de3b87aSKai Wang 		return (ELF_T_WORD);
522de3b87aSKai Wang 	case SHT_HASH:
532de3b87aSKai Wang 		return (ELF_T_WORD);
542de3b87aSKai Wang 	case SHT_INIT_ARRAY:
552de3b87aSKai Wang 		return (ELF_T_ADDR);
562de3b87aSKai Wang 	case SHT_NOBITS:
572de3b87aSKai Wang 		return (ELF_T_BYTE);
582de3b87aSKai Wang 	case SHT_NOTE:
592de3b87aSKai Wang 		return (ELF_T_NOTE);
602de3b87aSKai Wang 	case SHT_PREINIT_ARRAY:
612de3b87aSKai Wang 		return (ELF_T_ADDR);
622de3b87aSKai Wang 	case SHT_PROGBITS:
632de3b87aSKai Wang 		return (ELF_T_BYTE);
642de3b87aSKai Wang 	case SHT_REL:
652de3b87aSKai Wang 		return (ELF_T_REL);
662de3b87aSKai Wang 	case SHT_RELA:
672de3b87aSKai Wang 		return (ELF_T_RELA);
682de3b87aSKai Wang 	case SHT_STRTAB:
692de3b87aSKai Wang 		return (ELF_T_BYTE);
702de3b87aSKai Wang 	case SHT_SYMTAB:
712de3b87aSKai Wang 		return (ELF_T_SYM);
722de3b87aSKai Wang 	case SHT_SYMTAB_SHNDX:
732de3b87aSKai Wang 		return (ELF_T_WORD);
742de3b87aSKai Wang 	case SHT_SUNW_dof:
752de3b87aSKai Wang 		return (ELF_T_BYTE);
762de3b87aSKai Wang 	case SHT_SUNW_move:
772de3b87aSKai Wang 		return (ELF_T_MOVE);
782de3b87aSKai Wang 	case SHT_SUNW_syminfo:
792de3b87aSKai Wang 		return (ELF_T_SYMINFO);
802de3b87aSKai Wang 	case SHT_SUNW_verdef:	/* == SHT_GNU_verdef */
812de3b87aSKai Wang 		return (ELF_T_VDEF);
822de3b87aSKai Wang 	case SHT_SUNW_verneed:	/* == SHT_GNU_verneed */
832de3b87aSKai Wang 		return (ELF_T_VNEED);
842de3b87aSKai Wang 	case SHT_SUNW_versym:	/* == SHT_GNU_versym */
852de3b87aSKai Wang 		return (ELF_T_HALF);
86cf781b2eSEd Maste 	default:
87cf781b2eSEd Maste 		/*
88cf781b2eSEd Maste 		 * Values in the range [SHT_LOOS..SHT_HIUSER] (i.e.,
89cf781b2eSEd Maste 		 * OS, processor and user-defined section types) are
90cf781b2eSEd Maste 		 * legal, but since we do not know anything more about
91cf781b2eSEd Maste 		 * their semantics, we return a type of ELF_T_BYTE.
92*d003e0d7SEd Maste 		 *
93*d003e0d7SEd Maste 		 * The ELF specification uses 32 bit unsigned values for
94*d003e0d7SEd Maste 		 * denoting section types, and defines SHT_HIUSER to be
95*d003e0d7SEd Maste 		 * 0xFFFFFFFFUL (i.e., UINT32_MAX). Consequently, we only
96*d003e0d7SEd Maste 		 * need to check that 'sht' is greater than or equal to
97*d003e0d7SEd Maste 		 * SHT_LOOS.
98cf781b2eSEd Maste 		 */
99*d003e0d7SEd Maste 		if (sht >= SHT_LOOS)
100ee3d625dSKai Wang 			return (ELF_T_BYTE);
101ee3d625dSKai Wang 
102cf781b2eSEd Maste 		/*
103cf781b2eSEd Maste 		 * Other values are unsupported.
104cf781b2eSEd Maste 		 */
1052de3b87aSKai Wang 		return (-1);
1062de3b87aSKai Wang 	}
1072de3b87aSKai Wang }
108