xref: /freebsd/contrib/elftoolchain/libelf/gelf_rela.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_rela.c 3739 2019-05-06 05:18:15Z jkoshy $");
352de3b87aSKai Wang 
362de3b87aSKai Wang GElf_Rela *
gelf_getrela(Elf_Data * ed,int ndx,GElf_Rela * dst)372de3b87aSKai Wang gelf_getrela(Elf_Data *ed, int ndx, GElf_Rela *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_Rela *rela32;
452de3b87aSKai Wang 	Elf64_Rela *rela64;
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_RELA) {
662de3b87aSKai Wang 		LIBELF_SET_ERROR(ARGUMENT, 0);
672de3b87aSKai Wang 		return (NULL);
682de3b87aSKai Wang 	}
692de3b87aSKai Wang 
70*d003e0d7SEd Maste 	if ((msz = _libelf_msize(ELF_T_RELA, 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 		rela32 = (Elf32_Rela *) d->d_data.d_buf + ndx;
822de3b87aSKai Wang 
832de3b87aSKai Wang 		dst->r_offset = (Elf64_Addr) rela32->r_offset;
842de3b87aSKai Wang 		dst->r_info   = ELF64_R_INFO(
852de3b87aSKai Wang 		    (Elf64_Xword) ELF32_R_SYM(rela32->r_info),
862de3b87aSKai Wang 		    ELF32_R_TYPE(rela32->r_info));
872de3b87aSKai Wang 		dst->r_addend = (Elf64_Sxword) rela32->r_addend;
882de3b87aSKai Wang 
892de3b87aSKai Wang 	} else {
902de3b87aSKai Wang 
912de3b87aSKai Wang 		rela64 = (Elf64_Rela *) d->d_data.d_buf + ndx;
922de3b87aSKai Wang 
932de3b87aSKai Wang 		*dst = *rela64;
94eb81f38aSJohn Baldwin 
95eb81f38aSJohn Baldwin 		if (_libelf_is_mips64el(e))
96eb81f38aSJohn Baldwin 			dst->r_info =
97eb81f38aSJohn Baldwin 			    _libelf_mips64el_r_info_tom(rela64->r_info);
982de3b87aSKai Wang 	}
992de3b87aSKai Wang 
1002de3b87aSKai Wang 	return (dst);
1012de3b87aSKai Wang }
1022de3b87aSKai Wang 
1032de3b87aSKai Wang int
gelf_update_rela(Elf_Data * ed,int ndx,GElf_Rela * dr)1042de3b87aSKai Wang gelf_update_rela(Elf_Data *ed, int ndx, GElf_Rela *dr)
1052de3b87aSKai Wang {
1062de3b87aSKai Wang 	int ec;
1072de3b87aSKai Wang 	Elf *e;
1082de3b87aSKai Wang 	size_t msz;
1092de3b87aSKai Wang 	Elf_Scn *scn;
1102de3b87aSKai Wang 	uint32_t sh_type;
1112de3b87aSKai Wang 	Elf32_Rela *rela32;
1122de3b87aSKai Wang 	Elf64_Rela *rela64;
1132de3b87aSKai Wang 	struct _Libelf_Data *d;
1142de3b87aSKai Wang 
1152de3b87aSKai Wang 	d = (struct _Libelf_Data *) ed;
1162de3b87aSKai Wang 
1172de3b87aSKai Wang 	if (d == NULL || ndx < 0 || dr == NULL ||
1182de3b87aSKai Wang 	    (scn = d->d_scn) == NULL ||
1192de3b87aSKai Wang 	    (e = scn->s_elf) == NULL) {
1202de3b87aSKai Wang 		LIBELF_SET_ERROR(ARGUMENT, 0);
1212de3b87aSKai Wang 		return (0);
1222de3b87aSKai Wang 	}
1232de3b87aSKai Wang 
1242de3b87aSKai Wang 	ec = e->e_class;
1252de3b87aSKai Wang 	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
1262de3b87aSKai Wang 
1272de3b87aSKai Wang 	if (ec == ELFCLASS32)
1282de3b87aSKai Wang 		sh_type = scn->s_shdr.s_shdr32.sh_type;
1292de3b87aSKai Wang 	else
1302de3b87aSKai Wang 		sh_type = scn->s_shdr.s_shdr64.sh_type;
1312de3b87aSKai Wang 
1322de3b87aSKai Wang 	if (_libelf_xlate_shtype(sh_type) != ELF_T_RELA) {
1332de3b87aSKai Wang 		LIBELF_SET_ERROR(ARGUMENT, 0);
1342de3b87aSKai Wang 		return (0);
1352de3b87aSKai Wang 	}
1362de3b87aSKai Wang 
137*d003e0d7SEd Maste 	if ((msz = _libelf_msize(ELF_T_RELA, ec, e->e_version)) == 0)
138*d003e0d7SEd Maste 		return (0);
1392de3b87aSKai Wang 
140cf781b2eSEd Maste 	assert(ndx >= 0);
141cf781b2eSEd Maste 
142cf781b2eSEd Maste 	if (msz * (size_t) ndx >= d->d_data.d_size) {
1432de3b87aSKai Wang 		LIBELF_SET_ERROR(ARGUMENT, 0);
1442de3b87aSKai Wang 		return (0);
1452de3b87aSKai Wang 	}
1462de3b87aSKai Wang 
1472de3b87aSKai Wang 	if (ec == ELFCLASS32) {
1482de3b87aSKai Wang 		rela32 = (Elf32_Rela *) d->d_data.d_buf + ndx;
1492de3b87aSKai Wang 
1502de3b87aSKai Wang 		LIBELF_COPY_U32(rela32, dr, r_offset);
1512de3b87aSKai Wang 
152*d003e0d7SEd Maste 		if (ELF64_R_SYM(dr->r_info) > ELF32_R_SYM(~0U) ||
1532de3b87aSKai Wang 		    ELF64_R_TYPE(dr->r_info) > ELF32_R_TYPE(~0U)) {
1542de3b87aSKai Wang 			LIBELF_SET_ERROR(RANGE, 0);
1552de3b87aSKai Wang 			return (0);
1562de3b87aSKai Wang 		}
157cf781b2eSEd Maste 		rela32->r_info = ELF32_R_INFO(
158cf781b2eSEd Maste 			(Elf32_Word) ELF64_R_SYM(dr->r_info),
159cf781b2eSEd Maste 			(Elf32_Word) ELF64_R_TYPE(dr->r_info));
1602de3b87aSKai Wang 
1612de3b87aSKai Wang 		LIBELF_COPY_S32(rela32, dr, r_addend);
1622de3b87aSKai Wang 	} else {
1632de3b87aSKai Wang 		rela64 = (Elf64_Rela *) d->d_data.d_buf + ndx;
1642de3b87aSKai Wang 
1652de3b87aSKai Wang 		*rela64 = *dr;
166eb81f38aSJohn Baldwin 
167eb81f38aSJohn Baldwin 		if (_libelf_is_mips64el(e))
168eb81f38aSJohn Baldwin 			rela64->r_info = _libelf_mips64el_r_info_tof(dr->r_info);
1692de3b87aSKai Wang 	}
1702de3b87aSKai Wang 
1712de3b87aSKai Wang 	return (1);
1722de3b87aSKai Wang }
173