xref: /freebsd/contrib/elftoolchain/libelf/gelf_rel.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 <assert.h>
282de3b87aSKai Wang #include <gelf.h>
292de3b87aSKai Wang #include <limits.h>
30b4e9f239SEd Maste #include <stdint.h>
312de3b87aSKai Wang 
322de3b87aSKai Wang #include "_libelf.h"
332de3b87aSKai Wang 
34*d003e0d7SEd Maste ELFTC_VCSID("$Id: gelf_rel.c 3739 2019-05-06 05:18:15Z jkoshy $");
352de3b87aSKai Wang 
362de3b87aSKai Wang GElf_Rel *
gelf_getrel(Elf_Data * ed,int ndx,GElf_Rel * dst)372de3b87aSKai Wang gelf_getrel(Elf_Data *ed, int ndx, GElf_Rel *dst)
382de3b87aSKai Wang {
392de3b87aSKai Wang 	int ec;
402de3b87aSKai Wang 	Elf *e;
412de3b87aSKai Wang 	size_t msz;
422de3b87aSKai Wang 	Elf_Scn *scn;
432de3b87aSKai Wang 	uint32_t sh_type;
442de3b87aSKai Wang 	Elf32_Rel *rel32;
452de3b87aSKai Wang 	Elf64_Rel *rel64;
462de3b87aSKai Wang 	struct _Libelf_Data *d;
472de3b87aSKai Wang 
482de3b87aSKai Wang 	d = (struct _Libelf_Data *) ed;
492de3b87aSKai Wang 
502de3b87aSKai Wang 	if (d == NULL || ndx < 0 || dst == NULL ||
512de3b87aSKai Wang 	    (scn = d->d_scn) == NULL ||
522de3b87aSKai Wang 	    (e = scn->s_elf) == NULL) {
532de3b87aSKai Wang 		LIBELF_SET_ERROR(ARGUMENT, 0);
542de3b87aSKai Wang 		return (NULL);
552de3b87aSKai Wang 	}
562de3b87aSKai Wang 
572de3b87aSKai Wang 	ec = e->e_class;
582de3b87aSKai Wang 	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
592de3b87aSKai Wang 
602de3b87aSKai Wang 	if (ec == ELFCLASS32)
612de3b87aSKai Wang 		sh_type = scn->s_shdr.s_shdr32.sh_type;
622de3b87aSKai Wang 	else
632de3b87aSKai Wang 		sh_type = scn->s_shdr.s_shdr64.sh_type;
642de3b87aSKai Wang 
652de3b87aSKai Wang 	if (_libelf_xlate_shtype(sh_type) != ELF_T_REL) {
662de3b87aSKai Wang 		LIBELF_SET_ERROR(ARGUMENT, 0);
672de3b87aSKai Wang 		return (NULL);
682de3b87aSKai Wang 	}
692de3b87aSKai Wang 
70*d003e0d7SEd Maste 	if ((msz = _libelf_msize(ELF_T_REL, ec, e->e_version)) == 0)
71*d003e0d7SEd Maste 		return (NULL);
722de3b87aSKai Wang 
73cf781b2eSEd Maste 	assert(ndx >= 0);
742de3b87aSKai Wang 
75cf781b2eSEd Maste 	if (msz * (size_t) ndx >= d->d_data.d_size) {
762de3b87aSKai Wang 		LIBELF_SET_ERROR(ARGUMENT, 0);
772de3b87aSKai Wang 		return (NULL);
782de3b87aSKai Wang 	}
792de3b87aSKai Wang 
802de3b87aSKai Wang 	if (ec == ELFCLASS32) {
812de3b87aSKai Wang 		rel32 = (Elf32_Rel *) d->d_data.d_buf + ndx;
822de3b87aSKai Wang 
832de3b87aSKai Wang 		dst->r_offset = (Elf64_Addr) rel32->r_offset;
842de3b87aSKai Wang 		dst->r_info   = ELF64_R_INFO(
852de3b87aSKai Wang 		    (Elf64_Xword) ELF32_R_SYM(rel32->r_info),
862de3b87aSKai Wang 		    ELF32_R_TYPE(rel32->r_info));
872de3b87aSKai Wang 
882de3b87aSKai Wang 	} else {
892de3b87aSKai Wang 
902de3b87aSKai Wang 		rel64 = (Elf64_Rel *) d->d_data.d_buf + ndx;
912de3b87aSKai Wang 
922de3b87aSKai Wang 		*dst = *rel64;
93eb81f38aSJohn Baldwin 
94eb81f38aSJohn Baldwin 		if (_libelf_is_mips64el(e))
95eb81f38aSJohn Baldwin 			dst->r_info = _libelf_mips64el_r_info_tom(rel64->r_info);
962de3b87aSKai Wang 	}
972de3b87aSKai Wang 
982de3b87aSKai Wang 	return (dst);
992de3b87aSKai Wang }
1002de3b87aSKai Wang 
1012de3b87aSKai Wang int
gelf_update_rel(Elf_Data * ed,int ndx,GElf_Rel * dr)1022de3b87aSKai Wang gelf_update_rel(Elf_Data *ed, int ndx, GElf_Rel *dr)
1032de3b87aSKai Wang {
1042de3b87aSKai Wang 	int ec;
1052de3b87aSKai Wang 	Elf *e;
1062de3b87aSKai Wang 	size_t msz;
1072de3b87aSKai Wang 	Elf_Scn *scn;
1082de3b87aSKai Wang 	uint32_t sh_type;
1092de3b87aSKai Wang 	Elf32_Rel *rel32;
1102de3b87aSKai Wang 	Elf64_Rel *rel64;
1112de3b87aSKai Wang 	struct _Libelf_Data *d;
1122de3b87aSKai Wang 
1132de3b87aSKai Wang 	d = (struct _Libelf_Data *) ed;
1142de3b87aSKai Wang 
1152de3b87aSKai Wang 	if (d == NULL || ndx < 0 || dr == NULL ||
1162de3b87aSKai Wang 	    (scn = d->d_scn) == NULL ||
1172de3b87aSKai Wang 	    (e = scn->s_elf) == NULL) {
1182de3b87aSKai Wang 		LIBELF_SET_ERROR(ARGUMENT, 0);
1192de3b87aSKai Wang 		return (0);
1202de3b87aSKai Wang 	}
1212de3b87aSKai Wang 
1222de3b87aSKai Wang 	ec = e->e_class;
1232de3b87aSKai Wang 	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
1242de3b87aSKai Wang 
1252de3b87aSKai Wang 	if (ec == ELFCLASS32)
1262de3b87aSKai Wang 		sh_type = scn->s_shdr.s_shdr32.sh_type;
1272de3b87aSKai Wang 	else
1282de3b87aSKai Wang 		sh_type = scn->s_shdr.s_shdr64.sh_type;
1292de3b87aSKai Wang 
1302de3b87aSKai Wang 	if (_libelf_xlate_shtype(sh_type) != ELF_T_REL) {
1312de3b87aSKai Wang 		LIBELF_SET_ERROR(ARGUMENT, 0);
1322de3b87aSKai Wang 		return (0);
1332de3b87aSKai Wang 	}
1342de3b87aSKai Wang 
135*d003e0d7SEd Maste 	if ((msz = _libelf_msize(ELF_T_REL, ec, e->e_version)) == 0)
136*d003e0d7SEd Maste 		return (0);
1372de3b87aSKai Wang 
138cf781b2eSEd Maste 	assert(ndx >= 0);
139cf781b2eSEd Maste 
140cf781b2eSEd Maste 	if (msz * (size_t) ndx >= d->d_data.d_size) {
1412de3b87aSKai Wang 		LIBELF_SET_ERROR(ARGUMENT, 0);
1422de3b87aSKai Wang 		return (0);
1432de3b87aSKai Wang 	}
1442de3b87aSKai Wang 
1452de3b87aSKai Wang 	if (ec == ELFCLASS32) {
1462de3b87aSKai Wang 		rel32 = (Elf32_Rel *) d->d_data.d_buf + ndx;
1472de3b87aSKai Wang 
1482de3b87aSKai Wang 		LIBELF_COPY_U32(rel32, dr, r_offset);
1492de3b87aSKai Wang 
150*d003e0d7SEd Maste 		if (ELF64_R_SYM(dr->r_info) > ELF32_R_SYM(~0U) ||
1512de3b87aSKai Wang 		    ELF64_R_TYPE(dr->r_info) > ELF32_R_TYPE(~0U)) {
1522de3b87aSKai Wang 			LIBELF_SET_ERROR(RANGE, 0);
1532de3b87aSKai Wang 			return (0);
1542de3b87aSKai Wang 		}
155cf781b2eSEd Maste 		rel32->r_info = ELF32_R_INFO(
156cf781b2eSEd Maste 			(Elf32_Word) ELF64_R_SYM(dr->r_info),
157cf781b2eSEd Maste 			(Elf32_Word) ELF64_R_TYPE(dr->r_info));
1582de3b87aSKai Wang 	} else {
1592de3b87aSKai Wang 		rel64 = (Elf64_Rel *) d->d_data.d_buf + ndx;
1602de3b87aSKai Wang 
1612de3b87aSKai Wang 		*rel64 = *dr;
162eb81f38aSJohn Baldwin 
163eb81f38aSJohn Baldwin 		if (_libelf_is_mips64el(e))
164eb81f38aSJohn Baldwin 			rel64->r_info = _libelf_mips64el_r_info_tof(dr->r_info);
1652de3b87aSKai Wang 	}
1662de3b87aSKai Wang 
1672de3b87aSKai Wang 	return (1);
1682de3b87aSKai Wang }
169