From 4667b1924a9b01081722535013b8386c04700fe8 Mon Sep 17 00:00:00 2001 From: Michael Jeanson Date: Fri, 16 Apr 2021 16:56:14 -0400 Subject: [PATCH] Hide lttng_ust_elf symbols Change-Id: I5747d0dd9c5fe3d2fdba4f5606e8aa1b7138016a Signed-off-by: Michael Jeanson Signed-off-by: Mathieu Desnoyers --- src/common/Makefile.am | 5 +- .../lttng-ust-elf.c => common/elf.c} | 7 +- src/common/elf.h | 165 +++++++++++++++++- src/common/utils.c | 31 ++++ src/common/utils.h | 13 ++ src/lib/lttng-ust/Makefile.am | 2 - src/lib/lttng-ust/lttng-tracer-core.h | 3 - src/lib/lttng-ust/lttng-ust-comm.c | 18 -- src/lib/lttng-ust/lttng-ust-elf.h | 152 ---------------- tests/unit/ust-elf/Makefile.am | 3 +- 10 files changed, 212 insertions(+), 187 deletions(-) rename src/{lib/lttng-ust/lttng-ust-elf.c => common/elf.c} (99%) create mode 100644 src/common/utils.c create mode 100644 src/common/utils.h delete mode 100644 src/lib/lttng-ust/lttng-ust-elf.h diff --git a/src/common/Makefile.am b/src/common/Makefile.am index a53e4ce7..777764fd 100644 --- a/src/common/Makefile.am +++ b/src/common/Makefile.am @@ -13,7 +13,6 @@ noinst_HEADERS = \ clock.h \ creds.h \ dynamic-type.h \ - elf.h \ err-ptr.h \ events.h \ hash.h \ @@ -138,6 +137,8 @@ libsnprintf_la_SOURCES = \ # Common library libcommon_la_SOURCES = \ + elf.c \ + elf.h \ getenv.c \ getenv.h \ logging.c \ @@ -146,6 +147,8 @@ libcommon_la_SOURCES = \ smp.h \ strutils.c \ strutils.h \ + utils.c \ + utils.h \ patient.c libcommon_la_LIBADD = \ diff --git a/src/lib/lttng-ust/lttng-ust-elf.c b/src/common/elf.c similarity index 99% rename from src/lib/lttng-ust/lttng-ust-elf.c rename to src/common/elf.c index 59805365..b418176b 100644 --- a/src/lib/lttng-ust/lttng-ust-elf.c +++ b/src/common/elf.c @@ -16,11 +16,10 @@ #include #include "common/elf.h" -#include "common/ust-fd.h" - -#include "lttng-tracer-core.h" -#include "lttng-ust-elf.h" +#include "common/logging.h" #include "common/macros.h" +#include "common/ust-fd.h" +#include "common/utils.h" #define BUF_LEN 4096 diff --git a/src/common/elf.h b/src/common/elf.h index ea0351de..17f94676 100644 --- a/src/common/elf.h +++ b/src/common/elf.h @@ -10,6 +10,9 @@ #include #include #include +#include + +#include struct lttng_ust_elf_ehdr { uint16_t e_type; @@ -67,13 +70,163 @@ struct lttng_ust_elf { uint8_t 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); -int lttng_ust_elf_get_memsz(struct lttng_ust_elf *elf, uint64_t *memsz); +/* + * 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; +} + +struct lttng_ust_elf *lttng_ust_elf_create(const char *path) + __attribute__((visibility("hidden"))); + +void lttng_ust_elf_destroy(struct lttng_ust_elf *elf) + __attribute__((visibility("hidden"))); + +uint8_t lttng_ust_elf_is_pic(struct lttng_ust_elf *elf) + __attribute__((visibility("hidden"))); + +int lttng_ust_elf_get_memsz(struct lttng_ust_elf *elf, uint64_t *memsz) + __attribute__((visibility("hidden"))); + int lttng_ust_elf_get_build_id(struct lttng_ust_elf *elf, uint8_t **build_id, - size_t *length, int *found); + size_t *length, int *found) + __attribute__((visibility("hidden"))); + int lttng_ust_elf_get_debug_link(struct lttng_ust_elf *elf, char **filename, - uint32_t *crc, int *found); + uint32_t *crc, int *found) + __attribute__((visibility("hidden"))); #endif /* _UST_COMMON_ELF_H */ diff --git a/src/common/utils.c b/src/common/utils.c new file mode 100644 index 00000000..108f76db --- /dev/null +++ b/src/common/utils.c @@ -0,0 +1,31 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-only + * + * Copyright (C) 2011 David Goulet + * Copyright (C) 2011 Mathieu Desnoyers + */ + +#include +#include +#include + +#include "common/utils.h" + +ssize_t lttng_ust_read(int fd, void *buf, size_t len) +{ + ssize_t ret; + size_t copied = 0, to_copy = len; + + do { + ret = read(fd, buf + copied, to_copy); + if (ret > 0) { + copied += ret; + to_copy -= ret; + } + } while ((ret > 0 && to_copy > 0) + || (ret < 0 && errno == EINTR)); + if (ret > 0) { + ret = copied; + } + return ret; +} diff --git a/src/common/utils.h b/src/common/utils.h new file mode 100644 index 00000000..31ece9ea --- /dev/null +++ b/src/common/utils.h @@ -0,0 +1,13 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * + * Copyright (C) 2021 Michael Jeanson + */ + +#ifndef _UST_COMMON_UTILS_H +#define _UST_COMMON_UTILS_H + +ssize_t lttng_ust_read(int fd, void *buf, size_t len) + __attribute__((visibility("hidden"))); + +#endif diff --git a/src/lib/lttng-ust/Makefile.am b/src/lib/lttng-ust/Makefile.am index 4aa576be..7f82ebde 100644 --- a/src/lib/lttng-ust/Makefile.am +++ b/src/lib/lttng-ust/Makefile.am @@ -41,8 +41,6 @@ liblttng_ust_runtime_la_SOURCES = \ lttng-context-vsgid.c \ lttng-context.c \ lttng-events.c \ - lttng-ust-elf.c \ - lttng-ust-elf.h \ lttng-ust-statedump.c \ lttng-ust-statedump.h \ lttng-ust-statedump-provider.h \ diff --git a/src/lib/lttng-ust/lttng-tracer-core.h b/src/lib/lttng-ust/lttng-tracer-core.h index ade4636a..bfead6e5 100644 --- a/src/lib/lttng-ust/lttng-tracer-core.h +++ b/src/lib/lttng-ust/lttng-tracer-core.h @@ -73,9 +73,6 @@ char* lttng_ust_sockinfo_get_procname(void *owner) void lttng_ust_sockinfo_session_enabled(void *owner) __attribute__((visibility("hidden"))); -ssize_t lttng_ust_read(int fd, void *buf, size_t len) - __attribute__((visibility("hidden"))); - size_t lttng_ust_dummy_get_size(void *priv, size_t offset) __attribute__((visibility("hidden"))); diff --git a/src/lib/lttng-ust/lttng-ust-comm.c b/src/lib/lttng-ust/lttng-ust-comm.c index 2f96167e..c84594ba 100644 --- a/src/lib/lttng-ust/lttng-ust-comm.c +++ b/src/lib/lttng-ust/lttng-ust-comm.c @@ -357,24 +357,6 @@ static int got_timeout_env; static char *get_map_shm(struct sock_info *sock_info); -ssize_t lttng_ust_read(int fd, void *buf, size_t len) -{ - ssize_t ret; - size_t copied = 0, to_copy = len; - - do { - ret = read(fd, buf + copied, to_copy); - if (ret > 0) { - copied += ret; - to_copy -= ret; - } - } while ((ret > 0 && to_copy > 0) - || (ret < 0 && errno == EINTR)); - if (ret > 0) { - ret = copied; - } - return ret; -} /* * Returns the HOME directory path. Caller MUST NOT free(3) the returned * pointer. diff --git a/src/lib/lttng-ust/lttng-ust-elf.h b/src/lib/lttng-ust/lttng-ust-elf.h deleted file mode 100644 index b052e2ae..00000000 --- a/src/lib/lttng-ust/lttng-ust-elf.h +++ /dev/null @@ -1,152 +0,0 @@ -/* - * SPDX-License-Identifier: LGPL-2.1-or-later - * - * Copyright (C) 2015 Antoine Busque - */ - -#ifndef _LIB_LTTNG_UST_ELF_H -#define _LIB_LTTNG_UST_ELF_H - -#include -#include - -/* - * 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 diff --git a/tests/unit/ust-elf/Makefile.am b/tests/unit/ust-elf/Makefile.am index b7480556..b469453c 100644 --- a/tests/unit/ust-elf/Makefile.am +++ b/tests/unit/ust-elf/Makefile.am @@ -5,7 +5,8 @@ AM_CPPFLAGS += -I$(top_srcdir)/tests/utils noinst_PROGRAMS = ust-elf ust_elf_SOURCES = ust-elf.c ust_elf_LDADD = \ - $(top_builddir)/src/lib/lttng-ust/liblttng-ust.la \ + $(top_builddir)/src/common/libcommon.la \ + $(top_builddir)/src/lib/lttng-ust-common/liblttng-ust-common.la \ $(top_builddir)/tests/utils/libtap.a dist_check_SCRIPTS = test_ust_elf -- 2.34.1