2 * SPDX-License-Identifier: LGPL-2.1-or-later
4 * Copyright (C) 2015 Antoine Busque <abusque@efficios.com>
7 #ifndef _LTTNG_UST_ELF_H
8 #define _LTTNG_UST_ELF_H
14 #include <lttng/ust-endian.h>
17 * Determine native endianness in order to convert when reading an ELF
18 * file if there is a mismatch.
20 #if BYTE_ORDER == LITTLE_ENDIAN
21 #define NATIVE_ELF_ENDIANNESS ELFDATA2LSB
23 #define NATIVE_ELF_ENDIANNESS ELFDATA2MSB
27 * The size in bytes of the debug link CRC as contained in an ELF
30 #define ELF_CRC_SIZE 4
32 * ELF notes are aligned on 4 bytes. ref: ELF specification version
35 #define ELF_NOTE_ENTRY_ALIGN 4
37 * Within an ELF note, the `desc` field is also aligned on 4
38 * bytes. ref: ELF specification version 1.1 p. 2-5.
40 #define ELF_NOTE_DESC_ALIGN 4
44 switch (sizeof(x)) { \
61 #define bswap_phdr(phdr) \
63 bswap((phdr).p_type); \
64 bswap((phdr).p_offset); \
65 bswap((phdr).p_filesz); \
66 bswap((phdr).p_memsz); \
67 bswap((phdr).p_align); \
68 bswap((phdr).p_vaddr); \
71 #define bswap_shdr(shdr) \
73 bswap((shdr).sh_name); \
74 bswap((shdr).sh_type); \
75 bswap((shdr).sh_flags); \
76 bswap((shdr).sh_addr); \
77 bswap((shdr).sh_offset); \
78 bswap((shdr).sh_size); \
79 bswap((shdr).sh_link); \
80 bswap((shdr).sh_info); \
81 bswap((shdr).sh_addralign); \
82 bswap((shdr).sh_entsize); \
85 #define bswap_ehdr(ehdr) \
87 bswap((ehdr).e_type); \
88 bswap((ehdr).e_machine); \
89 bswap((ehdr).e_version); \
90 bswap((ehdr).e_entry); \
91 bswap((ehdr).e_phoff); \
92 bswap((ehdr).e_shoff); \
93 bswap((ehdr).e_flags); \
94 bswap((ehdr).e_ehsize); \
95 bswap((ehdr).e_phentsize); \
96 bswap((ehdr).e_phnum); \
97 bswap((ehdr).e_shentsize); \
98 bswap((ehdr).e_shnum); \
99 bswap((ehdr).e_shstrndx); \
102 #define copy_phdr(src_phdr, dst_phdr) \
104 (dst_phdr).p_type = (src_phdr).p_type; \
105 (dst_phdr).p_offset = (src_phdr).p_offset; \
106 (dst_phdr).p_filesz = (src_phdr).p_filesz; \
107 (dst_phdr).p_memsz = (src_phdr).p_memsz; \
108 (dst_phdr).p_align = (src_phdr).p_align; \
109 (dst_phdr).p_vaddr = (src_phdr).p_vaddr; \
112 #define copy_shdr(src_shdr, dst_shdr) \
114 (dst_shdr).sh_name = (src_shdr).sh_name; \
115 (dst_shdr).sh_type = (src_shdr).sh_type; \
116 (dst_shdr).sh_flags = (src_shdr).sh_flags; \
117 (dst_shdr).sh_addr = (src_shdr).sh_addr; \
118 (dst_shdr).sh_offset = (src_shdr).sh_offset; \
119 (dst_shdr).sh_size = (src_shdr).sh_size; \
120 (dst_shdr).sh_link = (src_shdr).sh_link; \
121 (dst_shdr).sh_info = (src_shdr).sh_info; \
122 (dst_shdr).sh_addralign = (src_shdr).sh_addralign; \
123 (dst_shdr).sh_entsize = (src_shdr).sh_entsize; \
126 #define copy_ehdr(src_ehdr, dst_ehdr) \
128 (dst_ehdr).e_type = (src_ehdr).e_type; \
129 (dst_ehdr).e_machine = (src_ehdr).e_machine; \
130 (dst_ehdr).e_version = (src_ehdr).e_version; \
131 (dst_ehdr).e_entry = (src_ehdr).e_entry; \
132 (dst_ehdr).e_phoff = (src_ehdr).e_phoff; \
133 (dst_ehdr).e_shoff = (src_ehdr).e_shoff; \
134 (dst_ehdr).e_flags = (src_ehdr).e_flags; \
135 (dst_ehdr).e_ehsize = (src_ehdr).e_ehsize; \
136 (dst_ehdr).e_phentsize = (src_ehdr).e_phentsize; \
137 (dst_ehdr).e_phnum = (src_ehdr).e_phnum; \
138 (dst_ehdr).e_shentsize = (src_ehdr).e_shentsize; \
139 (dst_ehdr).e_shnum = (src_ehdr).e_shnum; \
140 (dst_ehdr).e_shstrndx = (src_ehdr).e_shstrndx; \
143 struct lttng_ust_elf_ehdr
{
152 uint16_t e_phentsize
;
154 uint16_t e_shentsize
;
159 struct lttng_ust_elf_phdr
{
168 struct lttng_ust_elf_shdr
{
177 uint64_t sh_addralign
;
181 struct lttng_ust_elf_nhdr
{
187 struct lttng_ust_elf
{
188 /* Offset in bytes to start of section names string table. */
189 off_t section_names_offset
;
190 /* Size in bytes of section names string table. */
191 size_t section_names_size
;
194 struct lttng_ust_elf_ehdr
*ehdr
;
200 int is_elf_32_bit(struct lttng_ust_elf
*elf
)
202 return elf
->bitness
== ELFCLASS32
;
206 int is_elf_native_endian(struct lttng_ust_elf
*elf
)
208 return elf
->endianness
== NATIVE_ELF_ENDIANNESS
;
211 struct lttng_ust_elf
*lttng_ust_elf_create(const char *path
);
212 void lttng_ust_elf_destroy(struct lttng_ust_elf
*elf
);
213 uint8_t lttng_ust_elf_is_pic(struct lttng_ust_elf
*elf
);
214 int lttng_ust_elf_get_memsz(struct lttng_ust_elf
*elf
, uint64_t *memsz
);
215 int lttng_ust_elf_get_build_id(struct lttng_ust_elf
*elf
, uint8_t **build_id
,
216 size_t *length
, int *found
);
217 int lttng_ust_elf_get_debug_link(struct lttng_ust_elf
*elf
, char **filename
,
218 uint32_t *crc
, int *found
);
220 #endif /* _LTTNG_UST_ELF_H */