]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
perf dso: Add support for reading the e_machine type for a dso
authorIan Rogers <irogers@google.com>
Wed, 19 Mar 2025 05:07:33 +0000 (22:07 -0700)
committerNamhyung Kim <namhyung@kernel.org>
Fri, 21 Mar 2025 05:58:02 +0000 (22:58 -0700)
For ELF file dsos read the e_machine from the ELF header. For kernel
types assume the e_machine matches the perf tool. In other cases
return EM_NONE.

When reading from the ELF header use DSO__SWAP that may need
dso->needs_swap initializing. Factor out dso__swap_init to allow this.

Signed-off-by: Ian Rogers <irogers@google.com>
Reviewed-by: Namhyung Kim <namhyung@kernel.org>
Acked-by: Arnaldo Carvalho de Melo <acme@kernel.org>
Link: https://lore.kernel.org/r/20250319050741.269828-7-irogers@google.com
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
tools/perf/util/dso.c
tools/perf/util/dso.h
tools/perf/util/symbol-elf.c

index e0111049f6b0377643ecc51e2c8091b933659564..8619b6eea62de1fbad00feca14dd4ddc590b312c 100644 (file)
@@ -1194,6 +1194,68 @@ ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
        return data_read_write_offset(dso, machine, offset, data, size, true);
 }
 
+uint16_t dso__e_machine(struct dso *dso, struct machine *machine)
+{
+       uint16_t e_machine = EM_NONE;
+       int fd;
+
+       switch (dso__binary_type(dso)) {
+       case DSO_BINARY_TYPE__KALLSYMS:
+       case DSO_BINARY_TYPE__GUEST_KALLSYMS:
+       case DSO_BINARY_TYPE__VMLINUX:
+       case DSO_BINARY_TYPE__GUEST_VMLINUX:
+       case DSO_BINARY_TYPE__GUEST_KMODULE:
+       case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
+       case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
+       case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
+       case DSO_BINARY_TYPE__KCORE:
+       case DSO_BINARY_TYPE__GUEST_KCORE:
+       case DSO_BINARY_TYPE__BPF_PROG_INFO:
+       case DSO_BINARY_TYPE__BPF_IMAGE:
+       case DSO_BINARY_TYPE__OOL:
+       case DSO_BINARY_TYPE__JAVA_JIT:
+               return EM_HOST;
+       case DSO_BINARY_TYPE__DEBUGLINK:
+       case DSO_BINARY_TYPE__BUILD_ID_CACHE:
+       case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
+       case DSO_BINARY_TYPE__GNU_DEBUGDATA:
+       case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
+       case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
+       case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
+       case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
+       case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO:
+       case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
+               break;
+       case DSO_BINARY_TYPE__NOT_FOUND:
+       default:
+               return EM_NONE;
+       }
+
+       mutex_lock(dso__data_open_lock());
+
+       /*
+        * dso__data(dso)->fd might be closed if other thread opened another
+        * file (dso) due to open file limit (RLIMIT_NOFILE).
+        */
+       try_to_open_dso(dso, machine);
+       fd = dso__data(dso)->fd;
+       if (fd >= 0) {
+               _Static_assert(offsetof(Elf32_Ehdr, e_machine) == 18, "Unexpected offset");
+               _Static_assert(offsetof(Elf64_Ehdr, e_machine) == 18, "Unexpected offset");
+               if (dso__needs_swap(dso) == DSO_SWAP__UNSET) {
+                       unsigned char eidata;
+
+                       if (pread(fd, &eidata, sizeof(eidata), EI_DATA) == sizeof(eidata))
+                               dso__swap_init(dso, eidata);
+               }
+               if (dso__needs_swap(dso) != DSO_SWAP__UNSET &&
+                   pread(fd, &e_machine, sizeof(e_machine), 18) == sizeof(e_machine))
+                       e_machine = DSO__SWAP(dso, uint16_t, e_machine);
+       }
+       mutex_unlock(dso__data_open_lock());
+       return e_machine;
+}
+
 /**
  * dso__data_read_addr - Read data from dso address
  * @dso: dso object
@@ -1549,6 +1611,33 @@ void dso__put(struct dso *dso)
                RC_CHK_PUT(dso);
 }
 
+int dso__swap_init(struct dso *dso, unsigned char eidata)
+{
+       static unsigned int const endian = 1;
+
+       dso__set_needs_swap(dso, DSO_SWAP__NO);
+
+       switch (eidata) {
+       case ELFDATA2LSB:
+               /* We are big endian, DSO is little endian. */
+               if (*(unsigned char const *)&endian != 1)
+                       dso__set_needs_swap(dso, DSO_SWAP__YES);
+               break;
+
+       case ELFDATA2MSB:
+               /* We are little endian, DSO is big endian. */
+               if (*(unsigned char const *)&endian != 0)
+                       dso__set_needs_swap(dso, DSO_SWAP__YES);
+               break;
+
+       default:
+               pr_err("unrecognized DSO data encoding %d\n", eidata);
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
 void dso__set_build_id(struct dso *dso, struct build_id *bid)
 {
        RC_CHK_ACCESS(dso)->bid = *bid;
index a4980101e22b67446100d3d5cf168979df9271e3..c87564471f9b01b1c92b7d58824f48100db0b250 100644 (file)
@@ -737,6 +737,8 @@ bool dso__sorted_by_name(const struct dso *dso);
 void dso__set_sorted_by_name(struct dso *dso);
 void dso__sort_by_name(struct dso *dso);
 
+int dso__swap_init(struct dso *dso, unsigned char eidata);
+
 void dso__set_build_id(struct dso *dso, struct build_id *bid);
 bool dso__build_id_equal(const struct dso *dso, struct build_id *bid);
 void dso__read_running_kernel_build_id(struct dso *dso,
@@ -826,6 +828,7 @@ int dso__data_file_size(struct dso *dso, struct machine *machine);
 off_t dso__data_size(struct dso *dso, struct machine *machine);
 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
                              u64 offset, u8 *data, ssize_t size);
+uint16_t dso__e_machine(struct dso *dso, struct machine *machine);
 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
                            struct machine *machine, u64 addr,
                            u8 *data, ssize_t size);
index 3fa92697c457b7421f0532a2e62365bd7ca6cf70..fbf6d0f73af915bc01a8568534f416a3beeba00f 100644 (file)
@@ -1174,33 +1174,6 @@ out:
 
 #endif
 
-static int dso__swap_init(struct dso *dso, unsigned char eidata)
-{
-       static unsigned int const endian = 1;
-
-       dso__set_needs_swap(dso, DSO_SWAP__NO);
-
-       switch (eidata) {
-       case ELFDATA2LSB:
-               /* We are big endian, DSO is little endian. */
-               if (*(unsigned char const *)&endian != 1)
-                       dso__set_needs_swap(dso, DSO_SWAP__YES);
-               break;
-
-       case ELFDATA2MSB:
-               /* We are little endian, DSO is big endian. */
-               if (*(unsigned char const *)&endian != 0)
-                       dso__set_needs_swap(dso, DSO_SWAP__YES);
-               break;
-
-       default:
-               pr_err("unrecognized DSO data encoding %d\n", eidata);
-               return -EINVAL;
-       }
-
-       return 0;
-}
-
 bool symsrc__possibly_runtime(struct symsrc *ss)
 {
        return ss->dynsym || ss->opdsec;