Move internal elf macros to a private header
authorMichael Jeanson <mjeanson@efficios.com>
Fri, 26 Feb 2021 20:41:26 +0000 (15:41 -0500)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Tue, 2 Mar 2021 19:24:07 +0000 (14:24 -0500)
The public 'lttng/ust-elf.h' header exposed a dependency on the libelf
headers even though elfutils is only an optional dependency. None of the
macros and static inline functions using the libelf API need to be
publicly available, move them to a private header.

Also move non-namespaced convenience macros to a private header.

Change-Id: I3b75a3a984ffc2a2827664a76c45c7a8fd7b1fdf
Signed-off-by: Michael Jeanson <mjeanson@efficios.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
include/lttng/ust-elf.h
liblttng-ust/Makefile.am
liblttng-ust/lttng-ust-elf.c
liblttng-ust/lttng-ust-elf.h [new file with mode: 0644]

index 32d4ad1f16d18d33d2f2edd05b62e4f14ce7a6d1..29a9426f8ae740869afefa5a1a73f009046eb218 100644 (file)
 #include <stdlib.h>
 #include <stdint.h>
 #include <stdio.h>
-#include <elf.h>
-#include <lttng/ust-endian.h>
-
-/*
- * Determine native endianness in order to convert when reading an ELF
- * file if there is a mismatch.
- */
-#if BYTE_ORDER == LITTLE_ENDIAN
-#define NATIVE_ELF_ENDIANNESS ELFDATA2LSB
-#else
-#define NATIVE_ELF_ENDIANNESS ELFDATA2MSB
-#endif
-
-/*
- * The size in bytes of the debug link CRC as contained in an ELF
- * section.
- */
-#define ELF_CRC_SIZE           4
-/*
- * ELF notes are aligned on 4 bytes. ref: ELF specification version
- * 1.1 p. 2-5.
- */
-#define ELF_NOTE_ENTRY_ALIGN   4
-/*
- * Within an ELF note, the `desc` field is also aligned on 4
- * bytes. ref: ELF specification version 1.1 p. 2-5.
- */
-#define ELF_NOTE_DESC_ALIGN    4
-
-#define bswap(x)                               \
-       do {                                    \
-               switch (sizeof(x)) {            \
-               case 8:                         \
-                       x = bswap_64(x);        \
-                       break;                  \
-               case 4:                         \
-                       x = bswap_32(x);        \
-                       break;                  \
-               case 2:                         \
-                       x = bswap_16(x);        \
-                       break;                  \
-               case 1:                         \
-                       break;                  \
-               default:                        \
-                       abort();                \
-               }                               \
-       } while (0)
-
-#define bswap_phdr(phdr)               \
-       do {                            \
-               bswap((phdr).p_type);   \
-               bswap((phdr).p_offset); \
-               bswap((phdr).p_filesz); \
-               bswap((phdr).p_memsz);  \
-               bswap((phdr).p_align);  \
-               bswap((phdr).p_vaddr);  \
-       } while (0)
-
-#define bswap_shdr(shdr)                   \
-       do {                                \
-               bswap((shdr).sh_name);      \
-               bswap((shdr).sh_type);      \
-               bswap((shdr).sh_flags);     \
-               bswap((shdr).sh_addr);      \
-               bswap((shdr).sh_offset);    \
-               bswap((shdr).sh_size);      \
-               bswap((shdr).sh_link);      \
-               bswap((shdr).sh_info);      \
-               bswap((shdr).sh_addralign); \
-               bswap((shdr).sh_entsize);   \
-       } while (0)
-
-#define bswap_ehdr(ehdr)                               \
-       do {                                            \
-               bswap((ehdr).e_type);                   \
-               bswap((ehdr).e_machine);                \
-               bswap((ehdr).e_version);                \
-               bswap((ehdr).e_entry);                  \
-               bswap((ehdr).e_phoff);                  \
-               bswap((ehdr).e_shoff);                  \
-               bswap((ehdr).e_flags);                  \
-               bswap((ehdr).e_ehsize);                 \
-               bswap((ehdr).e_phentsize);              \
-               bswap((ehdr).e_phnum);                  \
-               bswap((ehdr).e_shentsize);              \
-               bswap((ehdr).e_shnum);                  \
-               bswap((ehdr).e_shstrndx);               \
-       } while (0)
-
-#define copy_phdr(src_phdr, dst_phdr)                          \
-       do {                                                    \
-               (dst_phdr).p_type = (src_phdr).p_type;          \
-               (dst_phdr).p_offset = (src_phdr).p_offset;      \
-               (dst_phdr).p_filesz = (src_phdr).p_filesz;      \
-               (dst_phdr).p_memsz = (src_phdr).p_memsz;        \
-               (dst_phdr).p_align = (src_phdr).p_align;        \
-               (dst_phdr).p_vaddr = (src_phdr).p_vaddr;        \
-       } while (0)
-
-#define copy_shdr(src_shdr, dst_shdr)                                  \
-       do {                                                            \
-               (dst_shdr).sh_name = (src_shdr).sh_name;                \
-               (dst_shdr).sh_type = (src_shdr).sh_type;                \
-               (dst_shdr).sh_flags = (src_shdr).sh_flags;              \
-               (dst_shdr).sh_addr = (src_shdr).sh_addr;                \
-               (dst_shdr).sh_offset = (src_shdr).sh_offset;            \
-               (dst_shdr).sh_size = (src_shdr).sh_size;                \
-               (dst_shdr).sh_link = (src_shdr).sh_link;                \
-               (dst_shdr).sh_info = (src_shdr).sh_info;                \
-               (dst_shdr).sh_addralign = (src_shdr).sh_addralign;      \
-               (dst_shdr).sh_entsize = (src_shdr).sh_entsize;          \
-       } while (0)
-
-#define copy_ehdr(src_ehdr, dst_ehdr)                                  \
-       do {                                                            \
-               (dst_ehdr).e_type = (src_ehdr).e_type;                  \
-               (dst_ehdr).e_machine = (src_ehdr).e_machine;            \
-               (dst_ehdr).e_version = (src_ehdr).e_version;            \
-               (dst_ehdr).e_entry = (src_ehdr).e_entry;                \
-               (dst_ehdr).e_phoff = (src_ehdr).e_phoff;                \
-               (dst_ehdr).e_shoff = (src_ehdr).e_shoff;                \
-               (dst_ehdr).e_flags = (src_ehdr).e_flags;                \
-               (dst_ehdr).e_ehsize = (src_ehdr).e_ehsize;              \
-               (dst_ehdr).e_phentsize = (src_ehdr).e_phentsize;        \
-               (dst_ehdr).e_phnum = (src_ehdr).e_phnum;                \
-               (dst_ehdr).e_shentsize = (src_ehdr).e_shentsize;        \
-               (dst_ehdr).e_shnum = (src_ehdr).e_shnum;                \
-               (dst_ehdr).e_shstrndx = (src_ehdr).e_shstrndx;          \
-       } while (0)
 
 struct lttng_ust_elf_ehdr {
        uint16_t e_type;
@@ -196,18 +67,6 @@ struct lttng_ust_elf {
        uint8_t endianness;
 };
 
-static inline
-int is_elf_32_bit(struct lttng_ust_elf *elf)
-{
-       return elf->bitness == ELFCLASS32;
-}
-
-static inline
-int is_elf_native_endian(struct lttng_ust_elf *elf)
-{
-       return elf->endianness == NATIVE_ELF_ENDIANNESS;
-}
-
 struct lttng_ust_elf *lttng_ust_elf_create(const char *path);
 void lttng_ust_elf_destroy(struct lttng_ust_elf *elf);
 uint8_t lttng_ust_elf_is_pic(struct lttng_ust_elf *elf);
index bf06727d7784c234bd5f0951f4fbe39b141057fa..8665be876ab21e2c6a9263842ab3be2b921aefc7 100644 (file)
@@ -63,6 +63,7 @@ liblttng_ust_runtime_la_SOURCES = \
        lttng-events.c \
        lttng-hash-helper.h \
        lttng-ust-elf.c \
+       lttng-ust-elf.h \
        lttng-ust-statedump.c \
        lttng-ust-statedump.h \
        lttng-ust-statedump-provider.h \
index b22b4b63087aee739a00ccad7ad59c570403a307..ce3b1cccc28efd8f262587be12a9e259d911498d 100644 (file)
@@ -19,6 +19,7 @@
 #include <ust-fd.h>
 
 #include "lttng-tracer-core.h"
+#include "lttng-ust-elf.h"
 
 #define BUF_LEN        4096
 
diff --git a/liblttng-ust/lttng-ust-elf.h b/liblttng-ust/lttng-ust-elf.h
new file mode 100644 (file)
index 0000000..b052e2a
--- /dev/null
@@ -0,0 +1,152 @@
+/*
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ *
+ * Copyright (C) 2015 Antoine Busque <abusque@efficios.com>
+ */
+
+#ifndef _LIB_LTTNG_UST_ELF_H
+#define _LIB_LTTNG_UST_ELF_H
+
+#include <elf.h>
+#include <lttng/ust-endian.h>
+
+/*
+ * Determine native endianness in order to convert when reading an ELF
+ * file if there is a mismatch.
+ */
+#if BYTE_ORDER == LITTLE_ENDIAN
+#define NATIVE_ELF_ENDIANNESS ELFDATA2LSB
+#else
+#define NATIVE_ELF_ENDIANNESS ELFDATA2MSB
+#endif
+
+/*
+ * The size in bytes of the debug link CRC as contained in an ELF
+ * section.
+ */
+#define ELF_CRC_SIZE           4
+/*
+ * ELF notes are aligned on 4 bytes. ref: ELF specification version
+ * 1.1 p. 2-5.
+ */
+#define ELF_NOTE_ENTRY_ALIGN   4
+/*
+ * Within an ELF note, the `desc` field is also aligned on 4
+ * bytes. ref: ELF specification version 1.1 p. 2-5.
+ */
+#define ELF_NOTE_DESC_ALIGN    4
+
+#define bswap(x)                               \
+       do {                                    \
+               switch (sizeof(x)) {            \
+               case 8:                         \
+                       x = bswap_64(x);        \
+                       break;                  \
+               case 4:                         \
+                       x = bswap_32(x);        \
+                       break;                  \
+               case 2:                         \
+                       x = bswap_16(x);        \
+                       break;                  \
+               case 1:                         \
+                       break;                  \
+               default:                        \
+                       abort();                \
+               }                               \
+       } while (0)
+
+#define bswap_phdr(phdr)               \
+       do {                            \
+               bswap((phdr).p_type);   \
+               bswap((phdr).p_offset); \
+               bswap((phdr).p_filesz); \
+               bswap((phdr).p_memsz);  \
+               bswap((phdr).p_align);  \
+               bswap((phdr).p_vaddr);  \
+       } while (0)
+
+#define bswap_shdr(shdr)                   \
+       do {                                \
+               bswap((shdr).sh_name);      \
+               bswap((shdr).sh_type);      \
+               bswap((shdr).sh_flags);     \
+               bswap((shdr).sh_addr);      \
+               bswap((shdr).sh_offset);    \
+               bswap((shdr).sh_size);      \
+               bswap((shdr).sh_link);      \
+               bswap((shdr).sh_info);      \
+               bswap((shdr).sh_addralign); \
+               bswap((shdr).sh_entsize);   \
+       } while (0)
+
+#define bswap_ehdr(ehdr)                               \
+       do {                                            \
+               bswap((ehdr).e_type);                   \
+               bswap((ehdr).e_machine);                \
+               bswap((ehdr).e_version);                \
+               bswap((ehdr).e_entry);                  \
+               bswap((ehdr).e_phoff);                  \
+               bswap((ehdr).e_shoff);                  \
+               bswap((ehdr).e_flags);                  \
+               bswap((ehdr).e_ehsize);                 \
+               bswap((ehdr).e_phentsize);              \
+               bswap((ehdr).e_phnum);                  \
+               bswap((ehdr).e_shentsize);              \
+               bswap((ehdr).e_shnum);                  \
+               bswap((ehdr).e_shstrndx);               \
+       } while (0)
+
+#define copy_phdr(src_phdr, dst_phdr)                          \
+       do {                                                    \
+               (dst_phdr).p_type = (src_phdr).p_type;          \
+               (dst_phdr).p_offset = (src_phdr).p_offset;      \
+               (dst_phdr).p_filesz = (src_phdr).p_filesz;      \
+               (dst_phdr).p_memsz = (src_phdr).p_memsz;        \
+               (dst_phdr).p_align = (src_phdr).p_align;        \
+               (dst_phdr).p_vaddr = (src_phdr).p_vaddr;        \
+       } while (0)
+
+#define copy_shdr(src_shdr, dst_shdr)                                  \
+       do {                                                            \
+               (dst_shdr).sh_name = (src_shdr).sh_name;                \
+               (dst_shdr).sh_type = (src_shdr).sh_type;                \
+               (dst_shdr).sh_flags = (src_shdr).sh_flags;              \
+               (dst_shdr).sh_addr = (src_shdr).sh_addr;                \
+               (dst_shdr).sh_offset = (src_shdr).sh_offset;            \
+               (dst_shdr).sh_size = (src_shdr).sh_size;                \
+               (dst_shdr).sh_link = (src_shdr).sh_link;                \
+               (dst_shdr).sh_info = (src_shdr).sh_info;                \
+               (dst_shdr).sh_addralign = (src_shdr).sh_addralign;      \
+               (dst_shdr).sh_entsize = (src_shdr).sh_entsize;          \
+       } while (0)
+
+#define copy_ehdr(src_ehdr, dst_ehdr)                                  \
+       do {                                                            \
+               (dst_ehdr).e_type = (src_ehdr).e_type;                  \
+               (dst_ehdr).e_machine = (src_ehdr).e_machine;            \
+               (dst_ehdr).e_version = (src_ehdr).e_version;            \
+               (dst_ehdr).e_entry = (src_ehdr).e_entry;                \
+               (dst_ehdr).e_phoff = (src_ehdr).e_phoff;                \
+               (dst_ehdr).e_shoff = (src_ehdr).e_shoff;                \
+               (dst_ehdr).e_flags = (src_ehdr).e_flags;                \
+               (dst_ehdr).e_ehsize = (src_ehdr).e_ehsize;              \
+               (dst_ehdr).e_phentsize = (src_ehdr).e_phentsize;        \
+               (dst_ehdr).e_phnum = (src_ehdr).e_phnum;                \
+               (dst_ehdr).e_shentsize = (src_ehdr).e_shentsize;        \
+               (dst_ehdr).e_shnum = (src_ehdr).e_shnum;                \
+               (dst_ehdr).e_shstrndx = (src_ehdr).e_shstrndx;          \
+       } while (0)
+
+static inline
+int is_elf_32_bit(struct lttng_ust_elf *elf)
+{
+       return elf->bitness == ELFCLASS32;
+}
+
+static inline
+int is_elf_native_endian(struct lttng_ust_elf *elf)
+{
+       return elf->endianness == NATIVE_ELF_ENDIANNESS;
+}
+
+#endif
This page took 0.028516 seconds and 4 git commands to generate.