123d0f849SIan Dowse /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni *
423d0f849SIan Dowse * Copyright (c) 2003 Jake Burkholder.
523d0f849SIan Dowse * Copyright 1996-1998 John D. Polstra.
623d0f849SIan Dowse * All rights reserved.
723d0f849SIan Dowse *
823d0f849SIan Dowse * Redistribution and use in source and binary forms, with or without
923d0f849SIan Dowse * modification, are permitted provided that the following conditions
1023d0f849SIan Dowse * are met:
1123d0f849SIan Dowse * 1. Redistributions of source code must retain the above copyright
1223d0f849SIan Dowse * notice, this list of conditions and the following disclaimer.
1323d0f849SIan Dowse * 2. Redistributions in binary form must reproduce the above copyright
1423d0f849SIan Dowse * notice, this list of conditions and the following disclaimer in the
1523d0f849SIan Dowse * documentation and/or other materials provided with the distribution.
1623d0f849SIan Dowse *
1723d0f849SIan Dowse * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1823d0f849SIan Dowse * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1923d0f849SIan Dowse * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2023d0f849SIan Dowse * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2123d0f849SIan Dowse * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2223d0f849SIan Dowse * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2323d0f849SIan Dowse * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2423d0f849SIan Dowse * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2523d0f849SIan Dowse * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2623d0f849SIan Dowse * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2723d0f849SIan Dowse * SUCH DAMAGE.
2823d0f849SIan Dowse */
2923d0f849SIan Dowse
300299afdfSJohn Baldwin #include <sys/endian.h>
3123d0f849SIan Dowse
3223d0f849SIan Dowse #include <err.h>
3323d0f849SIan Dowse #include <errno.h>
340299afdfSJohn Baldwin #include <gelf.h>
3523d0f849SIan Dowse
3623d0f849SIan Dowse #include "ef.h"
3723d0f849SIan Dowse
3823d0f849SIan Dowse /*
392a622f14SJessica Clarke * Apply relocations to the values obtained from the file. `relbase' is the
402a622f14SJessica Clarke * target relocation address of the section, and `dataoff/len' is the region
412a622f14SJessica Clarke * that is to be relocated, and has been copied to *dest
4223d0f849SIan Dowse */
430299afdfSJohn Baldwin static int
ef_amd64_reloc(struct elf_file * ef,const void * reldata,Elf_Type reltype,GElf_Addr relbase,GElf_Addr dataoff,size_t len,void * dest)440299afdfSJohn Baldwin ef_amd64_reloc(struct elf_file *ef, const void *reldata, Elf_Type reltype,
450299afdfSJohn Baldwin GElf_Addr relbase, GElf_Addr dataoff, size_t len, void *dest)
4623d0f849SIan Dowse {
470299afdfSJohn Baldwin char *where;
482a622f14SJessica Clarke GElf_Addr addr, addend;
490299afdfSJohn Baldwin GElf_Size rtype, symidx;
500299afdfSJohn Baldwin const GElf_Rel *rel;
510299afdfSJohn Baldwin const GElf_Rela *rela;
5223d0f849SIan Dowse
539dba198bSIan Dowse switch (reltype) {
540299afdfSJohn Baldwin case ELF_T_REL:
550299afdfSJohn Baldwin rel = (const GElf_Rel *)reldata;
562a622f14SJessica Clarke where = (char *)dest + (relbase + rel->r_offset - dataoff);
579dba198bSIan Dowse addend = 0;
580299afdfSJohn Baldwin rtype = GELF_R_TYPE(rel->r_info);
590299afdfSJohn Baldwin symidx = GELF_R_SYM(rel->r_info);
6023d0f849SIan Dowse break;
610299afdfSJohn Baldwin case ELF_T_RELA:
620299afdfSJohn Baldwin rela = (const GElf_Rela *)reldata;
632a622f14SJessica Clarke where = (char *)dest + (relbase + rela->r_offset - dataoff);
6423d0f849SIan Dowse addend = rela->r_addend;
650299afdfSJohn Baldwin rtype = GELF_R_TYPE(rela->r_info);
660299afdfSJohn Baldwin symidx = GELF_R_SYM(rela->r_info);
6723d0f849SIan Dowse break;
6823d0f849SIan Dowse default:
6923d0f849SIan Dowse return (EINVAL);
7023d0f849SIan Dowse }
7123d0f849SIan Dowse
720299afdfSJohn Baldwin if (where < (char *)dest || where >= (char *)dest + len)
7323d0f849SIan Dowse return (0);
7423d0f849SIan Dowse
750299afdfSJohn Baldwin if (reltype == ELF_T_REL) {
7623d0f849SIan Dowse /* Addend is 32 bit on 32 bit relocs */
7723d0f849SIan Dowse switch (rtype) {
7823d0f849SIan Dowse case R_X86_64_PC32:
7923d0f849SIan Dowse case R_X86_64_32S:
800299afdfSJohn Baldwin addend = le32dec(where);
8123d0f849SIan Dowse break;
8223d0f849SIan Dowse default:
830299afdfSJohn Baldwin addend = le64dec(where);
8423d0f849SIan Dowse break;
8523d0f849SIan Dowse }
8623d0f849SIan Dowse }
8723d0f849SIan Dowse
8823d0f849SIan Dowse switch (rtype) {
8923d0f849SIan Dowse case R_X86_64_NONE: /* none */
9023d0f849SIan Dowse break;
9123d0f849SIan Dowse case R_X86_64_64: /* S + A */
922a622f14SJessica Clarke addr = EF_SYMADDR(ef, symidx) + addend;
932a622f14SJessica Clarke le64enc(where, addr);
9423d0f849SIan Dowse break;
9523d0f849SIan Dowse case R_X86_64_32S: /* S + A sign extend */
962a622f14SJessica Clarke addr = EF_SYMADDR(ef, symidx) + addend;
972a622f14SJessica Clarke le32enc(where, addr);
9823d0f849SIan Dowse break;
9923d0f849SIan Dowse case R_X86_64_GLOB_DAT: /* S */
10023d0f849SIan Dowse addr = EF_SYMADDR(ef, symidx);
1010299afdfSJohn Baldwin le64enc(where, addr);
10223d0f849SIan Dowse break;
10323d0f849SIan Dowse case R_X86_64_RELATIVE: /* B + A */
1042a622f14SJessica Clarke addr = relbase + addend;
1052a622f14SJessica Clarke le64enc(where, addr);
10623d0f849SIan Dowse break;
10723d0f849SIan Dowse default:
10823d0f849SIan Dowse warnx("unhandled relocation type %d", (int)rtype);
10923d0f849SIan Dowse }
11023d0f849SIan Dowse return (0);
11123d0f849SIan Dowse }
1120299afdfSJohn Baldwin
1130299afdfSJohn Baldwin ELF_RELOC(ELFCLASS64, ELFDATA2LSB, EM_X86_64, ef_amd64_reloc);
114