2014-01-15 08:43:20 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2006,2008 Joseph Koshy
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <gelf.h>
|
|
|
|
#include <limits.h>
|
2015-03-31 20:01:37 +00:00
|
|
|
#include <stdint.h>
|
2014-01-15 08:43:20 +00:00
|
|
|
|
|
|
|
#include "_libelf.h"
|
|
|
|
|
2019-06-29 15:27:18 +00:00
|
|
|
ELFTC_VCSID("$Id: gelf_rel.c 3739 2019-05-06 05:18:15Z jkoshy $");
|
2014-01-15 08:43:20 +00:00
|
|
|
|
|
|
|
GElf_Rel *
|
|
|
|
gelf_getrel(Elf_Data *ed, int ndx, GElf_Rel *dst)
|
|
|
|
{
|
|
|
|
int ec;
|
|
|
|
Elf *e;
|
|
|
|
size_t msz;
|
|
|
|
Elf_Scn *scn;
|
|
|
|
uint32_t sh_type;
|
|
|
|
Elf32_Rel *rel32;
|
|
|
|
Elf64_Rel *rel64;
|
|
|
|
struct _Libelf_Data *d;
|
|
|
|
|
|
|
|
d = (struct _Libelf_Data *) ed;
|
|
|
|
|
|
|
|
if (d == NULL || ndx < 0 || dst == NULL ||
|
|
|
|
(scn = d->d_scn) == NULL ||
|
|
|
|
(e = scn->s_elf) == NULL) {
|
|
|
|
LIBELF_SET_ERROR(ARGUMENT, 0);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
ec = e->e_class;
|
|
|
|
assert(ec == ELFCLASS32 || ec == ELFCLASS64);
|
|
|
|
|
|
|
|
if (ec == ELFCLASS32)
|
|
|
|
sh_type = scn->s_shdr.s_shdr32.sh_type;
|
|
|
|
else
|
|
|
|
sh_type = scn->s_shdr.s_shdr64.sh_type;
|
|
|
|
|
|
|
|
if (_libelf_xlate_shtype(sh_type) != ELF_T_REL) {
|
|
|
|
LIBELF_SET_ERROR(ARGUMENT, 0);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2019-06-29 15:27:18 +00:00
|
|
|
if ((msz = _libelf_msize(ELF_T_REL, ec, e->e_version)) == 0)
|
|
|
|
return (NULL);
|
2014-01-15 08:43:20 +00:00
|
|
|
|
2014-12-22 20:32:23 +00:00
|
|
|
assert(ndx >= 0);
|
2014-01-15 08:43:20 +00:00
|
|
|
|
2014-12-22 20:32:23 +00:00
|
|
|
if (msz * (size_t) ndx >= d->d_data.d_size) {
|
2014-01-15 08:43:20 +00:00
|
|
|
LIBELF_SET_ERROR(ARGUMENT, 0);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ec == ELFCLASS32) {
|
|
|
|
rel32 = (Elf32_Rel *) d->d_data.d_buf + ndx;
|
|
|
|
|
|
|
|
dst->r_offset = (Elf64_Addr) rel32->r_offset;
|
|
|
|
dst->r_info = ELF64_R_INFO(
|
|
|
|
(Elf64_Xword) ELF32_R_SYM(rel32->r_info),
|
|
|
|
ELF32_R_TYPE(rel32->r_info));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
rel64 = (Elf64_Rel *) d->d_data.d_buf + ndx;
|
|
|
|
|
|
|
|
*dst = *rel64;
|
Fix objcopy for little-endian MIPS64 objects.
MIPS64 does not store the 'r_info' field of a relocation table entry as
a 64-bit value consisting of a 32-bit symbol index in the high 32 bits
and a 32-bit type in the low 32 bits as on other architectures. Instead,
the 64-bit 'r_info' field is really a 32-bit symbol index followed by four
individual byte type fields. For big-endian MIPS64, treating this as a
64-bit integer happens to be compatible with the layout expected by other
architectures (symbol index in upper 32-bits of resulting "native" 64-bit
integer). However, for little-endian MIPS64 the parsed 64-bit integer
contains the symbol index in the low 32 bits and the 4 individual byte
type fields in the upper 32-bits (but as if the upper 32-bits were
byte-swapped).
To cope, add two helper routines in gelf_getrel.c to translate between the
correct native 'r_info' value and the value obtained after the normal
byte-swap translation. Use these routines in gelf_getrel(), gelf_getrela(),
gelf_update_rel(), and gelf_update_rela(). This fixes 'readelf -r' on
little-endian MIPS64 objects which was previously decoding incorrect
relocations as well as 'objcopy: invalid symbox index' warnings from
objcopy when extracting debug symbols from kernel modules.
Even with this fixed, objcopy was still crashing when trying to extract
debug symbols from little-endian MIPS64 modules. The workaround in
gelf_*rel*() depends on the current ELF object having a valid ELF header
so that the 'e_machine' field can be compared against EM_MIPS. objcopy
was parsing the relocation entries to possibly rewrite the 'r_info' fields
in the update_relocs() function before writing the initial ELF header to
the destination object file. Move the initial write of the ELF header
earlier before copy_contents() so that update_relocs() uses the correct
symbol index values.
Note that this change should really go upstream. The binutils readelf
source has a similar hack for MIPS64EL though I implemented this version
from scratch using the MIPS64 ABI PDF as a reference.
Discussed with: jkoshy
Reviewed by: emaste, imp
Approved by: re (gjb, kib)
MFC after: 1 month
Differential Revision: https://reviews.freebsd.org/D15734
2018-09-05 20:51:53 +00:00
|
|
|
|
|
|
|
if (_libelf_is_mips64el(e))
|
|
|
|
dst->r_info = _libelf_mips64el_r_info_tom(rel64->r_info);
|
2014-01-15 08:43:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
gelf_update_rel(Elf_Data *ed, int ndx, GElf_Rel *dr)
|
|
|
|
{
|
|
|
|
int ec;
|
|
|
|
Elf *e;
|
|
|
|
size_t msz;
|
|
|
|
Elf_Scn *scn;
|
|
|
|
uint32_t sh_type;
|
|
|
|
Elf32_Rel *rel32;
|
|
|
|
Elf64_Rel *rel64;
|
|
|
|
struct _Libelf_Data *d;
|
|
|
|
|
|
|
|
d = (struct _Libelf_Data *) ed;
|
|
|
|
|
|
|
|
if (d == NULL || ndx < 0 || dr == NULL ||
|
|
|
|
(scn = d->d_scn) == NULL ||
|
|
|
|
(e = scn->s_elf) == NULL) {
|
|
|
|
LIBELF_SET_ERROR(ARGUMENT, 0);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
ec = e->e_class;
|
|
|
|
assert(ec == ELFCLASS32 || ec == ELFCLASS64);
|
|
|
|
|
|
|
|
if (ec == ELFCLASS32)
|
|
|
|
sh_type = scn->s_shdr.s_shdr32.sh_type;
|
|
|
|
else
|
|
|
|
sh_type = scn->s_shdr.s_shdr64.sh_type;
|
|
|
|
|
|
|
|
if (_libelf_xlate_shtype(sh_type) != ELF_T_REL) {
|
|
|
|
LIBELF_SET_ERROR(ARGUMENT, 0);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2019-06-29 15:27:18 +00:00
|
|
|
if ((msz = _libelf_msize(ELF_T_REL, ec, e->e_version)) == 0)
|
|
|
|
return (0);
|
2014-12-22 20:32:23 +00:00
|
|
|
|
|
|
|
assert(ndx >= 0);
|
2014-01-15 08:43:20 +00:00
|
|
|
|
2014-12-22 20:32:23 +00:00
|
|
|
if (msz * (size_t) ndx >= d->d_data.d_size) {
|
2014-01-15 08:43:20 +00:00
|
|
|
LIBELF_SET_ERROR(ARGUMENT, 0);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ec == ELFCLASS32) {
|
|
|
|
rel32 = (Elf32_Rel *) d->d_data.d_buf + ndx;
|
|
|
|
|
|
|
|
LIBELF_COPY_U32(rel32, dr, r_offset);
|
|
|
|
|
2019-06-29 15:27:18 +00:00
|
|
|
if (ELF64_R_SYM(dr->r_info) > ELF32_R_SYM(~0U) ||
|
2014-01-15 08:43:20 +00:00
|
|
|
ELF64_R_TYPE(dr->r_info) > ELF32_R_TYPE(~0U)) {
|
|
|
|
LIBELF_SET_ERROR(RANGE, 0);
|
|
|
|
return (0);
|
|
|
|
}
|
2014-12-22 20:32:23 +00:00
|
|
|
rel32->r_info = ELF32_R_INFO(
|
|
|
|
(Elf32_Word) ELF64_R_SYM(dr->r_info),
|
|
|
|
(Elf32_Word) ELF64_R_TYPE(dr->r_info));
|
2014-01-15 08:43:20 +00:00
|
|
|
} else {
|
|
|
|
rel64 = (Elf64_Rel *) d->d_data.d_buf + ndx;
|
|
|
|
|
|
|
|
*rel64 = *dr;
|
Fix objcopy for little-endian MIPS64 objects.
MIPS64 does not store the 'r_info' field of a relocation table entry as
a 64-bit value consisting of a 32-bit symbol index in the high 32 bits
and a 32-bit type in the low 32 bits as on other architectures. Instead,
the 64-bit 'r_info' field is really a 32-bit symbol index followed by four
individual byte type fields. For big-endian MIPS64, treating this as a
64-bit integer happens to be compatible with the layout expected by other
architectures (symbol index in upper 32-bits of resulting "native" 64-bit
integer). However, for little-endian MIPS64 the parsed 64-bit integer
contains the symbol index in the low 32 bits and the 4 individual byte
type fields in the upper 32-bits (but as if the upper 32-bits were
byte-swapped).
To cope, add two helper routines in gelf_getrel.c to translate between the
correct native 'r_info' value and the value obtained after the normal
byte-swap translation. Use these routines in gelf_getrel(), gelf_getrela(),
gelf_update_rel(), and gelf_update_rela(). This fixes 'readelf -r' on
little-endian MIPS64 objects which was previously decoding incorrect
relocations as well as 'objcopy: invalid symbox index' warnings from
objcopy when extracting debug symbols from kernel modules.
Even with this fixed, objcopy was still crashing when trying to extract
debug symbols from little-endian MIPS64 modules. The workaround in
gelf_*rel*() depends on the current ELF object having a valid ELF header
so that the 'e_machine' field can be compared against EM_MIPS. objcopy
was parsing the relocation entries to possibly rewrite the 'r_info' fields
in the update_relocs() function before writing the initial ELF header to
the destination object file. Move the initial write of the ELF header
earlier before copy_contents() so that update_relocs() uses the correct
symbol index values.
Note that this change should really go upstream. The binutils readelf
source has a similar hack for MIPS64EL though I implemented this version
from scratch using the MIPS64 ABI PDF as a reference.
Discussed with: jkoshy
Reviewed by: emaste, imp
Approved by: re (gjb, kib)
MFC after: 1 month
Differential Revision: https://reviews.freebsd.org/D15734
2018-09-05 20:51:53 +00:00
|
|
|
|
|
|
|
if (_libelf_is_mips64el(e))
|
|
|
|
rel64->r_info = _libelf_mips64el_r_info_tof(dr->r_info);
|
2014-01-15 08:43:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (1);
|
|
|
|
}
|