KNOWNSTYPE (FINI_ARRAY),
KNOWNSTYPE (PREINIT_ARRAY),
KNOWNSTYPE (GROUP),
- KNOWNSTYPE (SYMTAB_SHNDX)
+ KNOWNSTYPE (SYMTAB_SHNDX),
+ KNOWNSTYPE (RELR)
};
/* Handle standard names. */
case SHT_SUNW_syminfo:
sh_entsize = elf_typesize (LIBELFBITS, ELF_T_SYMINFO, 1);
break;
+ case SHT_RELR:
+ sh_entsize = elf_typesize (LIBELFBITS, ELF_T_RELR, 1);
+ break;
default:
break;
}
/* Relocation table entry with addend (in section of type SHT_RELA). */
typedef Elf64_Rela GElf_Rela;
+/* Relative relocation entry (in section of type SHT_RELR). */
+typedef Elf64_Relr GElf_Relr;
+
/* Program segment header. */
typedef Elf64_Phdr GElf_Phdr;
[ELF_T_LIB] = sizeof (ElfW2(LIBELFBITS, Ext_Lib)), \
[ELF_T_AUXV] = sizeof (ElfW2(LIBELFBITS, Ext_auxv_t)), \
[ELF_T_CHDR] = sizeof (ElfW2(LIBELFBITS, Ext_Chdr)), \
- [ELF_T_GNUHASH] = ELFW2(LIBELFBITS, FSZ_WORD)
+ [ELF_T_GNUHASH] = ELFW2(LIBELFBITS, FSZ_WORD), \
+ [ELF_T_RELR] = ELFW2(LIBELFBITS, FSZ_RELR)
TYPE_SIZES (32)
},
[ELFCLASS64 - 1] = {
[ELF_T_MOVE] = ElfW2(Bits, cvt_Move), \
[ELF_T_LIB] = ElfW2(Bits, cvt_Lib), \
[ELF_T_AUXV] = ElfW2(Bits, cvt_auxv_t), \
- [ELF_T_CHDR] = ElfW2(Bits, cvt_chdr)
+ [ELF_T_CHDR] = ElfW2(Bits, cvt_chdr), \
+ [ELF_T_RELR] = ElfW2(Bits, cvt_Relr)
define_xfcts (32),
[ELF_T_GNUHASH] = Elf32_cvt_Word
},
FUNDAMENTAL (SWORD, Sword, LIBELFBITS);
FUNDAMENTAL (XWORD, Xword, LIBELFBITS);
FUNDAMENTAL (SXWORD, Sxword, LIBELFBITS);
+FUNDAMENTAL (RELR, Relr, LIBELFBITS);
/* The structured types. */
TYPE (Ehdr, LIBELFBITS)
#define ELFCOMPRESS_ZSTD 2 /* Zstandard algorithm. */
#endif
+#ifndef SHT_RELR
+ /* So RELR defines/typedefs can be used even with an old system elf.h. */
+ #define SHT_RELR 19 /* RELR relative relocations */
+
+ /* RELR relocation table entry */
+ typedef Elf32_Word Elf32_Relr;
+ typedef Elf64_Xword Elf64_Relr;
+
+ #define DT_RELRSZ 35 /* Total size of RELR relative relocations */
+ #define DT_RELR 36 /* Address of RELR relative relocations */
+ #define DT_RELRENT 37 /* Size of one RELR relative relocaction */
+#endif
+
#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)
# define __nonnull_attribute__(...) __attribute__ ((__nonnull__ (__VA_ARGS__)))
# define __deprecated_attribute__ __attribute__ ((__deprecated__))
ELF_T_CHDR, /* Compressed, Elf32_Chdr, Elf64_Chdr, ... */
ELF_T_NHDR8, /* Special GNU Properties note. Same as Nhdr,
except padding. */
+ ELF_T_RELR, /* Relative relocation entry. */
/* Keep this the last entry. */
ELF_T_NUM
} Elf_Type;
#define ELF32_FSZ_SWORD 4
#define ELF32_FSZ_XWORD 8
#define ELF32_FSZ_SXWORD 8
+#define ELF32_FSZ_RELR 4
/* Same for 64 bits objects. */
#define ELF64_FSZ_ADDR 8
#define ELF64_FSZ_SWORD 4
#define ELF64_FSZ_XWORD 8
#define ELF64_FSZ_SXWORD 8
+#define ELF64_FSZ_RELR 8
/* This is an extension of the ELF_F_* enumeration. The values here are
/* Pedantic checking of ELF files compliance with gABI/psABI spec.
Copyright (C) 2001-2015, 2017, 2018 Red Hat, Inc.
+ Copyright (C) 2023 Mark J. Wielaard <mark@klomp.org>
This file is part of elfutils.
Written by Ulrich Drepper <drepper@redhat.com>, 2001.
size_t sh_entsize = gelf_fsize (ebl->elf, reltype, 1, EV_CURRENT);
if (shdr->sh_entsize != sh_entsize)
- ERROR (_(reltype == ELF_T_RELA ? "\
-section [%2d] '%s': section entry size does not match ElfXX_Rela\n" : "\
-section [%2d] '%s': section entry size does not match ElfXX_Rel\n"),
- idx, section_name (ebl, idx));
+ {
+ if (reltype == ELF_T_RELA)
+ ERROR ("\
+section [%2d] '%s': section entry size does not match ElfXX_Rela\n",
+ idx, section_name (ebl, idx));
+ else if (reltype == ELF_T_REL)
+ ERROR ("\
+section [%2d] '%s': section entry size does not match ElfXX_Rel\n",
+ idx, section_name (ebl, idx));
+ else
+ ERROR ("\
+section [%2d] '%s': section entry size does not match ElfXX_Relr\n",
+ idx, section_name (ebl, idx));
+ }
/* In preparation of checking whether relocations are text
relocations or not we need to determine whether the file is
}
}
+static void
+check_relr (Ebl *ebl, GElf_Ehdr *ehdr, GElf_Shdr *shdr, int idx)
+{
+ Elf_Data *data = elf_getdata (elf_getscn (ebl->elf, idx), NULL);
+ if (data == NULL)
+ {
+ ERROR (_("section [%2d] '%s': cannot get section data\n"),
+ idx, section_name (ebl, idx));
+ return;
+ }
+
+ /* Check the fields of the section header. */
+ GElf_Shdr destshdr_mem;
+ GElf_Shdr *destshdr = NULL;
+ struct loaded_segment *loaded = NULL;
+ check_reloc_shdr (ebl, ehdr, shdr, idx, ELF_T_RELR, &destshdr,
+ &destshdr_mem, &loaded);
+
+ /* Just throw them away. */
+ while (loaded != NULL)
+ {
+ struct loaded_segment *old = loaded;
+ loaded = loaded->next;
+ free (old);
+ }
+}
/* Number of dynamic sections. */
static int ndynamic;
case DT_PLTGOT:
case DT_REL:
case DT_RELA:
+ case DT_RELR:
case DT_SYMBOLIC:
case DT_SYMTAB:
case DT_VERDEF:
{ ".plt", 5, SHT_PROGBITS, unused, 0, 0 }, // XXX more tests
{ ".preinit_array", 15, SHT_PREINIT_ARRAY, exact, SHF_ALLOC | SHF_WRITE, 0 },
{ ".rela", 5, SHT_RELA, atleast, 0, SHF_ALLOC | SHF_INFO_LINK }, // XXX more tests
+ { ".relr", 5, SHT_RELR, atleast, 0, SHF_ALLOC }, // XXX more tests
{ ".rel", 4, SHT_REL, atleast, 0, SHF_ALLOC | SHF_INFO_LINK }, // XXX more tests
{ ".rodata", 8, SHT_PROGBITS, atleast, SHF_ALLOC, SHF_MERGE | SHF_STRINGS },
{ ".rodata1", 9, SHT_PROGBITS, atleast, SHF_ALLOC, SHF_MERGE | SHF_STRINGS },
check_rel (ebl, ehdr, shdr, cnt);
break;
+ case SHT_RELR:
+ check_relr (ebl, ehdr, shdr, cnt);
+ break;
+
case SHT_DYNAMIC:
check_dynamic (ebl, ehdr, shdr, cnt);
break;
/* Print information from ELF file in human-readable form.
Copyright (C) 1999-2018 Red Hat, Inc.
+ Copyright (C) 2023 Mark J. Wielaard <mark@klomp.org>
This file is part of elfutils.
This file is free software; you can redistribute it and/or modify
static void print_phdr (Ebl *ebl, GElf_Ehdr *ehdr);
static void print_scngrp (Ebl *ebl);
static void print_dynamic (Ebl *ebl);
-static void print_relocs (Ebl *ebl, GElf_Ehdr *ehdr);
+static void print_relocs (Ebl *ebl, Dwfl_Module *mod, GElf_Ehdr *ehdr);
static void handle_relocs_rel (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
GElf_Shdr *shdr);
static void handle_relocs_rela (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
GElf_Shdr *shdr);
+static void handle_relocs_relr (Ebl *ebl, Dwfl_Module *mod, Elf_Scn *scn,
+ GElf_Shdr *shdr);
static bool print_symtab (Ebl *ebl, int type);
static bool handle_symtab (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr);
static bool handle_dynamic_symtab (Ebl *ebl);
static void dump_strings (Ebl *ebl);
static void print_strings (Ebl *ebl);
static void dump_archive_index (Elf *, const char *);
+static void print_dwarf_addr (Dwfl_Module *dwflmod, int address_size,
+ Dwarf_Addr address, Dwarf_Addr raw);
enum dyn_idx
{
if (print_dynamic_table)
print_dynamic (ebl);
if (print_relocations)
- print_relocs (pure_ebl, ehdr);
+ print_relocs (pure_ebl, dwflmod, ehdr);
if (print_histogram)
handle_hash (ebl);
if (print_symbol_table || print_dynsym_table)
case DT_RELASZ:
case DT_STRSZ:
case DT_RELSZ:
+ case DT_RELRSZ:
case DT_RELAENT:
case DT_SYMENT:
case DT_RELENT:
+ case DT_RELRENT:
case DT_PLTPADSZ:
case DT_MOVEENT:
case DT_MOVESZ:
/* Print relocations. */
static void
-print_relocs (Ebl *ebl, GElf_Ehdr *ehdr)
+print_relocs (Ebl *ebl, Dwfl_Module *mod, GElf_Ehdr *ehdr)
{
/* Find all relocation sections and handle them. */
Elf_Scn *scn = NULL;
handle_relocs_rel (ebl, ehdr, scn, shdr);
else if (shdr->sh_type == SHT_RELA)
handle_relocs_rela (ebl, ehdr, scn, shdr);
+ else if (shdr->sh_type == SHT_RELR)
+ handle_relocs_relr (ebl, mod, scn, shdr);
}
}
}
}
}
+/* Handle a relocation section. */
+static void
+handle_relocs_relr (Ebl *ebl, Dwfl_Module *mod, Elf_Scn *scn, GElf_Shdr *shdr)
+{
+ int class = gelf_getclass (ebl->elf);
+ size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_RELR, 1, EV_CURRENT);
+ int nentries = shdr->sh_size / sh_entsize;
+
+ /* Get the data of the section. */
+ Elf_Data *data = elf_getdata (scn, NULL);
+ if (data == NULL)
+ return;
+
+ /* Get the section header string table index. */
+ size_t shstrndx;
+ if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
+ error_exit (0, _("cannot get section header string table index"));
+
+ /* A .relr.dyn section does not refer to a specific section. */
+ printf (ngettext ("\
+\nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
+ "\
+\nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
+ nentries),
+ (unsigned int) elf_ndxscn (scn),
+ elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
+ shdr->sh_offset,
+ nentries);
+
+ if (class == ELFCLASS32)
+ {
+ uint32_t base = 0;
+ for (int cnt = 0; cnt < nentries; ++cnt)
+ {
+ Elf32_Word *words = data->d_buf;
+ Elf32_Word entry = words[cnt];
+
+ /* Just the raw entries? */
+ if (print_unresolved_addresses)
+ printf (" %#010" PRIx32 "%s\n", entry,
+ (entry & 1) == 0 ? " *" : "");
+ else
+ {
+ /* A real address, also sets base. */
+ if ((entry & 1) == 0)
+ {
+ printf (" ");
+ print_dwarf_addr (mod, 4, entry, entry);
+ printf (" *\n");
+
+ base = entry + 4;
+ }
+ else
+ {
+ /* Untangle address from base and bits. */
+ uint32_t addr;
+ for (addr = base; (entry >>= 1) != 0; addr += 4)
+ if ((entry & 1) != 0)
+ {
+ printf (" ");
+ print_dwarf_addr (mod, 4, addr, addr);
+ printf ("\n");
+ }
+ base += 4 * (4 * 8 - 1);
+ }
+ }
+ }
+ }
+ else
+ {
+ uint64_t base = 0;
+ for (int cnt = 0; cnt < nentries; ++cnt)
+ {
+ Elf64_Xword *xwords = data->d_buf;
+ Elf64_Xword entry = xwords[cnt];
+
+ /* Just the raw entries? */
+ if (print_unresolved_addresses)
+ printf (" %#018" PRIx64 "%s\n", entry,
+ (entry & 1) == 0 ? " *" : "");
+ else
+ {
+ /* A real address, also sets base. */
+ if ((entry & 1) == 0)
+ {
+ printf (" ");
+ print_dwarf_addr (mod, 8, entry, entry);
+ printf (" *\n");
+
+ base = entry + 8;
+ }
+ else
+ {
+ /* Untangle address from base and bits. */
+ uint64_t addr;
+ for (addr = base; (entry >>= 1) != 0; addr += 8)
+ if ((entry & 1) != 0)
+ {
+ printf (" ");
+ print_dwarf_addr (mod, 8, addr, addr);
+ printf ("\n");
+ }
+ base += 8 * (8 * 8 - 1);
+ }
+ }
+ }
+ }
+}
/* Print the program header. Return true if a symtab is printed,
false otherwise. */
return elf_getdata (scn, NULL);
}
-void
+static void
print_dwarf_addr (Dwfl_Module *dwflmod,
int address_size, Dwarf_Addr address, Dwarf_Addr raw)
{
elfcopy addsections xlate_notes elfrdwrnop \
dwelf_elf_e_machine_string \
getphdrnum leb128 read_unaligned \
- msg_tst system-elf-libelf-test \
+ msg_tst system-elf-libelf-test system-elf-gelf-test \
nvidia_extended_linemap_libdw \
$(asm_TESTS)
run-pt_gnu_prop-tests.sh \
run-getphdrnum.sh run-test-includes.sh \
leb128 read_unaligned \
- msg_tst system-elf-libelf-test \
+ msg_tst system-elf-libelf-test system-elf-gelf-test \
$(asm_TESTS) run-disasm-bpf.sh run-low_high_pc-dw-form-indirect.sh \
run-nvidia-extended-linemap-libdw.sh run-nvidia-extended-linemap-readelf.sh \
run-readelf-dw-form-indirect.sh run-strip-largealign.sh \
read_unaligned_LDADD = $(libelf) $(libdw)
nvidia_extended_linemap_libdw_LDADD = $(libelf) $(libdw)
-# We want to test the libelf header against the system elf.h header.
+# We want to test the libelf headers against the system elf.h header.
# Don't include any -I CPPFLAGS. Except when we install our own elf.h.
if !INSTALL_ELFH
system_elf_libelf_test_CPPFLAGS =
+system_elf_gelf_test_CPPFLAGS =
else
system_elf_libelf_test_CPPFLAGS = -I$(top_srcdir)/libelf
+system_elf_gelf_test_CPPFLAGS = -I$(top_srcdir)/libelf
endif
system_elf_libelf_test_LDADD = $(libelf)
+system_elf_gelf_test_LDADD = $(libelf)
# A lock file used to make sure only one test dumps core at a time
CLEANFILES += core-dump-backtrace.lock
--- /dev/null
+/* Explicit test compiling with system elf.h header plus libelf/gelf headers.
+
+ Copyright (C) Mark J. Wielaard <mark@klomp.org>
+ This file is part of elfutils.
+
+ This file is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ elfutils is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include <elf.h>
+#include <stddef.h>
+#include "../libelf/libelf.h"
+#include "../libelf/gelf.h"
+
+int
+main (void)
+{
+ /* Trivial test, this is really a compile test anyway. */
+ if (elf_version (EV_CURRENT) == EV_NONE)
+ return -1;
+
+ /* This will obviously fail. It is just to check that gelf_getclass
+ available (both at compile time and runtime). */
+ int cls = gelf_getclass (NULL);
+
+ return cls == ELFCLASSNONE ? 0 : -1;
+}