2 * SPDX-License-Identifier: LGPL-2.1-or-later
4 * Copyright (C) 2015 Antoine Busque <abusque@efficios.com>
7 #ifndef _UST_COMMON_ELF_H
8 #define _UST_COMMON_ELF_H
15 #include <lttng/ust-endian.h>
17 struct lttng_ust_elf_ehdr
{
33 struct lttng_ust_elf_phdr
{
42 struct lttng_ust_elf_shdr
{
51 uint64_t sh_addralign
;
55 struct lttng_ust_elf_nhdr
{
61 struct lttng_ust_elf
{
62 /* Offset in bytes to start of section names string table. */
63 off_t section_names_offset
;
64 /* Size in bytes of section names string table. */
65 size_t section_names_size
;
68 struct lttng_ust_elf_ehdr
*ehdr
;
74 * Determine native endianness in order to convert when reading an ELF
75 * file if there is a mismatch.
77 #if LTTNG_UST_BYTE_ORDER == LTTNG_UST_LITTLE_ENDIAN
78 #define NATIVE_ELF_ENDIANNESS ELFDATA2LSB
80 #define NATIVE_ELF_ENDIANNESS ELFDATA2MSB
84 * The size in bytes of the debug link CRC as contained in an ELF
87 #define ELF_CRC_SIZE 4
89 * ELF notes are aligned on 4 bytes. ref: ELF specification version
92 #define ELF_NOTE_ENTRY_ALIGN 4
94 * Within an ELF note, the `desc` field is also aligned on 4
95 * bytes. ref: ELF specification version 1.1 p. 2-5.
97 #define ELF_NOTE_DESC_ALIGN 4
101 switch (sizeof(x)) { \
103 x = lttng_ust_bswap_64(x); \
106 x = lttng_ust_bswap_32(x); \
109 x = lttng_ust_bswap_16(x); \
118 #define bswap_phdr(phdr) \
120 bswap((phdr).p_type); \
121 bswap((phdr).p_offset); \
122 bswap((phdr).p_filesz); \
123 bswap((phdr).p_memsz); \
124 bswap((phdr).p_align); \
125 bswap((phdr).p_vaddr); \
128 #define bswap_shdr(shdr) \
130 bswap((shdr).sh_name); \
131 bswap((shdr).sh_type); \
132 bswap((shdr).sh_flags); \
133 bswap((shdr).sh_addr); \
134 bswap((shdr).sh_offset); \
135 bswap((shdr).sh_size); \
136 bswap((shdr).sh_link); \
137 bswap((shdr).sh_info); \
138 bswap((shdr).sh_addralign); \
139 bswap((shdr).sh_entsize); \
142 #define bswap_ehdr(ehdr) \
144 bswap((ehdr).e_type); \
145 bswap((ehdr).e_machine); \
146 bswap((ehdr).e_version); \
147 bswap((ehdr).e_entry); \
148 bswap((ehdr).e_phoff); \
149 bswap((ehdr).e_shoff); \
150 bswap((ehdr).e_flags); \
151 bswap((ehdr).e_ehsize); \
152 bswap((ehdr).e_phentsize); \
153 bswap((ehdr).e_phnum); \
154 bswap((ehdr).e_shentsize); \
155 bswap((ehdr).e_shnum); \
156 bswap((ehdr).e_shstrndx); \
159 #define copy_phdr(src_phdr, dst_phdr) \
161 (dst_phdr).p_type = (src_phdr).p_type; \
162 (dst_phdr).p_offset = (src_phdr).p_offset; \
163 (dst_phdr).p_filesz = (src_phdr).p_filesz; \
164 (dst_phdr).p_memsz = (src_phdr).p_memsz; \
165 (dst_phdr).p_align = (src_phdr).p_align; \
166 (dst_phdr).p_vaddr = (src_phdr).p_vaddr; \
169 #define copy_shdr(src_shdr, dst_shdr) \
171 (dst_shdr).sh_name = (src_shdr).sh_name; \
172 (dst_shdr).sh_type = (src_shdr).sh_type; \
173 (dst_shdr).sh_flags = (src_shdr).sh_flags; \
174 (dst_shdr).sh_addr = (src_shdr).sh_addr; \
175 (dst_shdr).sh_offset = (src_shdr).sh_offset; \
176 (dst_shdr).sh_size = (src_shdr).sh_size; \
177 (dst_shdr).sh_link = (src_shdr).sh_link; \
178 (dst_shdr).sh_info = (src_shdr).sh_info; \
179 (dst_shdr).sh_addralign = (src_shdr).sh_addralign; \
180 (dst_shdr).sh_entsize = (src_shdr).sh_entsize; \
183 #define copy_ehdr(src_ehdr, dst_ehdr) \
185 (dst_ehdr).e_type = (src_ehdr).e_type; \
186 (dst_ehdr).e_machine = (src_ehdr).e_machine; \
187 (dst_ehdr).e_version = (src_ehdr).e_version; \
188 (dst_ehdr).e_entry = (src_ehdr).e_entry; \
189 (dst_ehdr).e_phoff = (src_ehdr).e_phoff; \
190 (dst_ehdr).e_shoff = (src_ehdr).e_shoff; \
191 (dst_ehdr).e_flags = (src_ehdr).e_flags; \
192 (dst_ehdr).e_ehsize = (src_ehdr).e_ehsize; \
193 (dst_ehdr).e_phentsize = (src_ehdr).e_phentsize; \
194 (dst_ehdr).e_phnum = (src_ehdr).e_phnum; \
195 (dst_ehdr).e_shentsize = (src_ehdr).e_shentsize; \
196 (dst_ehdr).e_shnum = (src_ehdr).e_shnum; \
197 (dst_ehdr).e_shstrndx = (src_ehdr).e_shstrndx; \
201 int is_elf_32_bit(struct lttng_ust_elf
*elf
)
203 return elf
->bitness
== ELFCLASS32
;
207 int is_elf_native_endian(struct lttng_ust_elf
*elf
)
209 return elf
->endianness
== NATIVE_ELF_ENDIANNESS
;
212 struct lttng_ust_elf
*lttng_ust_elf_create(const char *path
)
213 __attribute__((visibility("hidden")));
215 void lttng_ust_elf_destroy(struct lttng_ust_elf
*elf
)
216 __attribute__((visibility("hidden")));
218 uint8_t lttng_ust_elf_is_pic(struct lttng_ust_elf
*elf
)
219 __attribute__((visibility("hidden")));
221 int lttng_ust_elf_get_memsz(struct lttng_ust_elf
*elf
, uint64_t *memsz
)
222 __attribute__((visibility("hidden")));
224 int lttng_ust_elf_get_build_id(struct lttng_ust_elf
*elf
, uint8_t **build_id
,
225 size_t *length
, int *found
)
226 __attribute__((visibility("hidden")));
228 int lttng_ust_elf_get_debug_link(struct lttng_ust_elf
*elf
, char **filename
,
229 uint32_t *crc
, int *found
)
230 __attribute__((visibility("hidden")));
232 #endif /* _UST_COMMON_ELF_H */