From 1daec75e0ff5313eec93b60f54fcb5ddc9d2ad28 Mon Sep 17 00:00:00 2001 From: Ulrich Drepper Date: Wed, 1 Jun 2022 13:14:27 +0200 Subject: [PATCH] Arm Ehdr flag printing Arm needs to decode flags and I modeled it after the binutils code. The same messages are printed. Given the requirement of the interface and the ABIs the current version of the callback function isn't sufficient unless one wants to create a stateful interface. The problem is that most flags need to be interpreted in the context of the ABI version. So I changed the API to also pass the original flag value. This shouldn't be a problem because there are no users yet. There is also a bug in ebl_machine_flag_name. When copying the string provided by the callback cp is moved past the NUL byte. It should move to the NUL byte. Otherwise one cannot anything but the first added flag description. Finally some cosmetic changes (space after each comma in the output). Signed-off-by: Mark Wielaard --- backends/ChangeLog | 6 ++ backends/Makefile.am | 3 +- backends/arm_init.c | 1 + backends/arm_machineflagname.c | 156 +++++++++++++++++++++++++++++++++++++++++ libebl/ChangeLog | 10 +++ libebl/ebl-hooks.h | 2 +- libebl/eblmachineflagname.c | 11 +-- libebl/eblopenbackend.c | 5 +- tests/ChangeLog | 7 ++ tests/Makefile.am | 2 + tests/run-readelf-arm-flags.sh | 30 ++++++++ tests/testfile-arm-flags.bz2 | Bin 0 -> 2593 bytes 12 files changed, 225 insertions(+), 8 deletions(-) create mode 100644 backends/arm_machineflagname.c create mode 100755 tests/run-readelf-arm-flags.sh create mode 100755 tests/testfile-arm-flags.bz2 diff --git a/backends/ChangeLog b/backends/ChangeLog index 5195925..495cdde 100644 --- a/backends/ChangeLog +++ b/backends/ChangeLog @@ -1,3 +1,9 @@ +2022-06-01 Ulrich Drepper + + * Makefile.am (arm_SRCS): Add arm_machineflagname.c. + * arm_init.c (arm_init): Hook in arm_machine_flag_name. + * arm_machineflagname.c: New file. + 2022-02-16 Mark Wielaard * ppc_initreg.c (ppc_set_initial_registers_tid): Define struct diff --git a/backends/Makefile.am b/backends/Makefile.am index 62916c9..9566377 100644 --- a/backends/Makefile.am +++ b/backends/Makefile.am @@ -56,7 +56,8 @@ alpha_SRCS = alpha_init.c alpha_symbol.c alpha_retval.c alpha_regs.c \ alpha_corenote.c alpha_auxv.c arm_SRCS = arm_init.c arm_symbol.c arm_regs.c arm_corenote.c \ - arm_auxv.c arm_attrs.c arm_retval.c arm_cfi.c arm_initreg.c + arm_auxv.c arm_attrs.c arm_retval.c arm_cfi.c arm_initreg.c \ + arm_machineflagname.c aarch64_SRCS = aarch64_init.c aarch64_regs.c aarch64_symbol.c \ aarch64_corenote.c aarch64_retval.c aarch64_cfi.c \ diff --git a/backends/arm_init.c b/backends/arm_init.c index edd53b7..70b7594 100644 --- a/backends/arm_init.c +++ b/backends/arm_init.c @@ -59,6 +59,7 @@ arm_init (Elf *elf __attribute__ ((unused)), HOOK (eh, check_reloc_target_type); HOOK (eh, symbol_type_name); HOOK (eh, data_marker_symbol); + HOOK (eh, machine_flag_name); /* We only unwind the core integer registers. */ eh->frame_nregs = 16; diff --git a/backends/arm_machineflagname.c b/backends/arm_machineflagname.c new file mode 100644 index 0000000..e93092a --- /dev/null +++ b/backends/arm_machineflagname.c @@ -0,0 +1,156 @@ +/* Arm-specific ELF flag names. + Copyright (C) 2022 Red Hat, Inc. + This file is part of elfutils. + + This file is free software; you can redistribute it and/or modify + it under the terms of either + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at + your option) any later version + + or + + * the GNU General Public License as published by the Free + Software Foundation; either version 2 of the License, or (at + your option) any later version + + or both in parallel, as here. + + 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 copies of the GNU General Public License and + the GNU Lesser General Public License along with this program. If + not, see . */ + +#ifdef HAVE_CONFIG_H +# include +#endif + +#define BACKEND arm_ +#include "libebl_CPU.h" + +const char * +arm_machine_flag_name (Elf64_Word orig, Elf64_Word *flagref) +{ + unsigned version = EF_ARM_EABI_VERSION (*flagref) >> 24; + if (version != 0) + { + static const char vername[5][14] = + { + "Version1 EABI", + "Version2 EABI", + "Version3 EABI", + "Version4 EABI", + "Version5 EABI", + }; + *flagref &= ~((Elf64_Word) EF_ARM_EABIMASK); + return vername[version - 1]; + } + switch (EF_ARM_EABI_VERSION (orig)) + { + case EF_ARM_EABI_VER2: + if ((*flagref & EF_ARM_DYNSYMSUSESEGIDX) != 0) + { + *flagref &= ~((Elf64_Word) EF_ARM_DYNSYMSUSESEGIDX); + return "dynamic symbols use segment index"; + } + if ((*flagref & EF_ARM_MAPSYMSFIRST) != 0) + { + *flagref &= ~((Elf64_Word) EF_ARM_MAPSYMSFIRST); + return "mapping symbols precede others"; + } + FALLTHROUGH; + case EF_ARM_EABI_VER1: + if ((*flagref & EF_ARM_SYMSARESORTED) != 0) + { + *flagref &= ~((Elf64_Word) EF_ARM_SYMSARESORTED); + return "sorted symbol tables"; + } + break; + case EF_ARM_EABI_VER3: + break; + case EF_ARM_EABI_VER5: + if ((*flagref & EF_ARM_SOFT_FLOAT) != 0) + { + *flagref &= ~((Elf64_Word) EF_ARM_SOFT_FLOAT); + return "soft-float ABI"; + } + if ((*flagref & EF_ARM_VFP_FLOAT) != 0) + { + *flagref &= ~((Elf64_Word) EF_ARM_VFP_FLOAT); + return "hard-float ABI"; + } + FALLTHROUGH; + case EF_ARM_EABI_VER4: + if ((*flagref & EF_ARM_BE8) != 0) + { + *flagref &= ~((Elf64_Word) EF_ARM_BE8); + return "BE8"; + } + if ((*flagref & EF_ARM_LE8) != 0) + { + *flagref &= ~((Elf64_Word) EF_ARM_LE8); + return "LE8"; + } + break; + case EF_ARM_EABI_UNKNOWN: + if ((*flagref & EF_ARM_INTERWORK) != 0) + { + *flagref &= ~((Elf64_Word) EF_ARM_INTERWORK); + return "interworking enabled"; + } + if ((*flagref & EF_ARM_APCS_26) != 0) + { + *flagref &= ~((Elf64_Word) EF_ARM_APCS_26); + return "uses APCS/26"; + } + if ((*flagref & EF_ARM_APCS_FLOAT) != 0) + { + *flagref &= ~((Elf64_Word) EF_ARM_APCS_FLOAT); + return "uses APCS/float"; + } + if ((*flagref & EF_ARM_PIC) != 0) + { + *flagref &= ~((Elf64_Word) EF_ARM_PIC); + return "position independent"; + } + if ((*flagref & EF_ARM_ALIGN8) != 0) + { + *flagref &= ~((Elf64_Word) EF_ARM_ALIGN8); + return "8 bit structure alignment"; + } + if ((*flagref & EF_ARM_NEW_ABI) != 0) + { + *flagref &= ~((Elf64_Word) EF_ARM_NEW_ABI); + return "uses new ABI"; + } + if ((*flagref & EF_ARM_OLD_ABI) != 0) + { + *flagref &= ~((Elf64_Word) EF_ARM_OLD_ABI); + return "uses old ABI"; + } + if ((*flagref & EF_ARM_SOFT_FLOAT) != 0) + { + *flagref &= ~((Elf64_Word) EF_ARM_SOFT_FLOAT); + return "software FP"; + } + if ((*flagref & EF_ARM_VFP_FLOAT) != 0) + { + *flagref &= ~((Elf64_Word) EF_ARM_VFP_FLOAT); + return "VFP"; + } + if ((*flagref & EF_ARM_MAVERICK_FLOAT) != 0) + { + *flagref &= ~((Elf64_Word) EF_ARM_MAVERICK_FLOAT); + return "Maverick FP"; + } + break; + default: + break; + } + return NULL; +} diff --git a/libebl/ChangeLog b/libebl/ChangeLog index 2e31e75..52b9c60 100644 --- a/libebl/ChangeLog +++ b/libebl/ChangeLog @@ -1,3 +1,13 @@ +2022-06-01 Ulrich Drepper + + * eblopenbackend.c (default_machine_flag_name): Add original flag + as first parameter. + * ebl-hooks.h (machine_flag_name): Ditto. + * eblmachineflagname.c (ebl_machine_flag_name): Modernize, use bool + for first. Pass original flag value to machine_flag_name + callback as well. Add space after comma in printed list. + Fix appending strings provided by callback. + 2021-12-21 Luca Boccassi * eblobjnote.c (ebl_object_note): Handle NT_FDO_PACKAGING_METADATA. diff --git a/libebl/ebl-hooks.h b/libebl/ebl-hooks.h index 1214bb8..d6437e5 100644 --- a/libebl/ebl-hooks.h +++ b/libebl/ebl-hooks.h @@ -51,7 +51,7 @@ const char *EBLHOOK(section_type_name) (int, char *, size_t); const char *EBLHOOK(section_name) (int, int, char *, size_t); /* Return next machine flag name. */ -const char *EBLHOOK(machine_flag_name) (GElf_Word *); +const char *EBLHOOK(machine_flag_name) (GElf_Word, GElf_Word *); /* Check whether machine flags are valid. */ bool EBLHOOK(machine_flag_check) (GElf_Word); diff --git a/libebl/eblmachineflagname.c b/libebl/eblmachineflagname.c index 5f44077..02e11c6 100644 --- a/libebl/eblmachineflagname.c +++ b/libebl/eblmachineflagname.c @@ -46,8 +46,9 @@ ebl_machine_flag_name (Ebl *ebl, Elf64_Word flags, char *buf, size_t len) res = ""; else { + Elf64_Word orig_flags = flags; char *cp = buf; - int first = 1; + bool first = true; const char *machstr; size_t machstrlen; @@ -55,12 +56,13 @@ ebl_machine_flag_name (Ebl *ebl, Elf64_Word flags, char *buf, size_t len) { if (! first) { - if (cp + 1 >= buf + len) + if (cp + 2 >= buf + len) break; *cp++ = ','; + *cp++ = ' '; } - machstr = ebl != NULL ? ebl->machine_flag_name (&flags) : NULL; + machstr = ebl != NULL ? ebl->machine_flag_name (orig_flags, &flags) : NULL; if (machstr == NULL) { /* No more known flag. */ @@ -76,8 +78,9 @@ ebl_machine_flag_name (Ebl *ebl, Elf64_Word flags, char *buf, size_t len) } cp = mempcpy (cp, machstr, machstrlen); + --cp; - first = 0; + first = false; } while (flags != 0); diff --git a/libebl/eblopenbackend.c b/libebl/eblopenbackend.c index 0c07296..c665725 100644 --- a/libebl/eblopenbackend.c +++ b/libebl/eblopenbackend.c @@ -169,7 +169,7 @@ static const char *default_section_type_name (int ignore, char *buf, size_t len); static const char *default_section_name (int ignore, int ignore2, char *buf, size_t len); -static const char *default_machine_flag_name (Elf64_Word *ignore); +static const char *default_machine_flag_name (Elf64_Word orig, Elf64_Word *ignore); static bool default_machine_flag_check (Elf64_Word flags); static bool default_machine_section_flag_check (GElf_Xword flags); static const char *default_symbol_type_name (int ignore, char *buf, @@ -450,7 +450,8 @@ default_section_name (int ignore __attribute__ ((unused)), } static const char * -default_machine_flag_name (Elf64_Word *ignore __attribute__ ((unused))) +default_machine_flag_name (Elf64_Word orig __attribute__ ((unused)), + Elf64_Word *ignore __attribute__ ((unused))) { return NULL; } diff --git a/tests/ChangeLog b/tests/ChangeLog index 6ea8e79..e65ea09 100644 --- a/tests/ChangeLog +++ b/tests/ChangeLog @@ -1,3 +1,10 @@ +2022-06-01 Mark Wielaard + + * testfile-arm-flags.bz2: New test file. + * run-readelf-arm-flags.sh: New test. + * Makefile.am (TESTS): Add run-readelf-arm-flags.sh. + (EXTRA_DIST): Add readelf-arm-flags.sh and testfile-arm-flags.bz2 + 2022-05-13 Noah Sanci * run-debuginfod-fd-prefetch-caches.sh: Rewritten. diff --git a/tests/Makefile.am b/tests/Makefile.am index d30b07c..0785159 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -164,6 +164,7 @@ TESTS = run-arextract.sh run-arsymtest.sh run-ar.sh newfile test-nlist \ run-backtrace-core-aarch64.sh run-backtrace-core-sparc.sh \ run-backtrace-demangle.sh run-stack-d-test.sh run-stack-i-test.sh \ run-stack-demangled-test.sh run-readelf-zx.sh run-readelf-zp.sh \ + run-readelf-arm-flags.sh \ run-readelf-addr.sh run-readelf-str.sh \ run-readelf-multi-noline.sh \ run-readelf-types.sh \ @@ -347,6 +348,7 @@ EXTRA_DIST = run-arextract.sh run-arsymtest.sh run-ar.sh \ run-readelf-dwz-multi.sh libtestfile_multi_shared.so.bz2 \ testfile_multi.dwz.bz2 testfile_multi_main.bz2 \ testfile-dwzstr.bz2 testfile-dwzstr.multi.bz2 \ + run-readelf-arm-flags.sh testfile-arm-flags.bz2 \ run-readelf-addr.sh run-readelf-str.sh \ run-readelf-multi-noline.sh testfile_multi_noline.bz2 \ run-readelf-types.sh \ diff --git a/tests/run-readelf-arm-flags.sh b/tests/run-readelf-arm-flags.sh new file mode 100755 index 0000000..25c5477 --- /dev/null +++ b/tests/run-readelf-arm-flags.sh @@ -0,0 +1,30 @@ +#! /bin/sh + +. $srcdir/test-subr.sh + +# echo "int main () {}" | gcc -xc -o testfile-arm-flags - +testfiles testfile-arm-flags + +testrun_compare ${abs_top_builddir}/src/readelf -h testfile-arm-flags <<\EOF +ELF Header: + Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 + Class: ELF32 + Data: 2's complement, little endian + Ident Version: 1 (current) + OS/ABI: UNIX - System V + ABI Version: 0 + Type: DYN (Shared object file) + Machine: ARM + Version: 1 (current) + Entry point address: 0x3d1 + Start of program headers: 52 (bytes into file) + Start of section headers: 6920 (bytes into file) + Flags: Version5 EABI, hard-float ABI + Size of this header: 52 (bytes) + Size of program header entries: 32 (bytes) + Number of program headers entries: 9 + Size of section header entries: 40 (bytes) + Number of section headers entries: 29 + Section header string table index: 28 + +EOF diff --git a/tests/testfile-arm-flags.bz2 b/tests/testfile-arm-flags.bz2 new file mode 100755 index 0000000000000000000000000000000000000000..d1b018adc504256350248641c45b03981f16cee6 GIT binary patch literal 2593 zcmV++3f}cXT4*^jL0KkKS$e_71po;tfB*mg|Ns8y{n!8h|M&m@|NHsm_}9&duP@Kv zY{&ZiDK0Gyn#MfuLvr00000XaE2J009sQq%w@qlL@p48hT7d&=U!!jWlQg zGH5htG#MBG1Pq!6MnfU$02vJiMnD)Q0h&;diK8i#H8fzDBTS7M4FQnQ05s6hGy#wR z01W^D000000000205s4t8ZWAl4O}5avT8 zWdaO_p%KvKZC|33H&1^iM%B4*RyF=shK_H4*k;|errX(YL#-3gZ+g{=WCF@D@^&2j z_Py#HOm^~J|23=RoIn65AXKvmSkQEV3DCp>1VAPghmQ!29;voNFaQ8NSN!Tks#l5O zxP&+WMWX-_U0KeoUvhmVWM?VZz(OEQ-px=1T^Q__Z4I-jg8u0ER5hK2#11(!Nf{)3+J|Lqys;Z$`pvM`RsALg@ zDr1bXuY5>e-X}pfYu3CZWFRjxS{6t_f#&! z@~o;wMR=4!6e4ZPZD$4W_}oh~Uy&0VM|NHq;we2_X$Z=7gZc zJR>nktTzRlvqPmVCXW3ml2?Wk5|m1Ua!5fS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