Commit | Line | Data |
---|---|---|
2a8db464 MJ |
1 | /* |
2 | * SPDX-License-Identifier: LGPL-2.1-or-later | |
3 | * | |
4 | * Copyright (C) 2015 Antoine Busque <abusque@efficios.com> | |
5 | */ | |
6 | ||
7 | #ifndef _LIB_LTTNG_UST_ELF_H | |
8 | #define _LIB_LTTNG_UST_ELF_H | |
9 | ||
10 | #include <elf.h> | |
11 | #include <lttng/ust-endian.h> | |
12 | ||
13 | /* | |
14 | * Determine native endianness in order to convert when reading an ELF | |
15 | * file if there is a mismatch. | |
16 | */ | |
17 | #if BYTE_ORDER == LITTLE_ENDIAN | |
18 | #define NATIVE_ELF_ENDIANNESS ELFDATA2LSB | |
19 | #else | |
20 | #define NATIVE_ELF_ENDIANNESS ELFDATA2MSB | |
21 | #endif | |
22 | ||
23 | /* | |
24 | * The size in bytes of the debug link CRC as contained in an ELF | |
25 | * section. | |
26 | */ | |
27 | #define ELF_CRC_SIZE 4 | |
28 | /* | |
29 | * ELF notes are aligned on 4 bytes. ref: ELF specification version | |
30 | * 1.1 p. 2-5. | |
31 | */ | |
32 | #define ELF_NOTE_ENTRY_ALIGN 4 | |
33 | /* | |
34 | * Within an ELF note, the `desc` field is also aligned on 4 | |
35 | * bytes. ref: ELF specification version 1.1 p. 2-5. | |
36 | */ | |
37 | #define ELF_NOTE_DESC_ALIGN 4 | |
38 | ||
39 | #define bswap(x) \ | |
40 | do { \ | |
41 | switch (sizeof(x)) { \ | |
42 | case 8: \ | |
43 | x = bswap_64(x); \ | |
44 | break; \ | |
45 | case 4: \ | |
46 | x = bswap_32(x); \ | |
47 | break; \ | |
48 | case 2: \ | |
49 | x = bswap_16(x); \ | |
50 | break; \ | |
51 | case 1: \ | |
52 | break; \ | |
53 | default: \ | |
54 | abort(); \ | |
55 | } \ | |
56 | } while (0) | |
57 | ||
58 | #define bswap_phdr(phdr) \ | |
59 | do { \ | |
60 | bswap((phdr).p_type); \ | |
61 | bswap((phdr).p_offset); \ | |
62 | bswap((phdr).p_filesz); \ | |
63 | bswap((phdr).p_memsz); \ | |
64 | bswap((phdr).p_align); \ | |
65 | bswap((phdr).p_vaddr); \ | |
66 | } while (0) | |
67 | ||
68 | #define bswap_shdr(shdr) \ | |
69 | do { \ | |
70 | bswap((shdr).sh_name); \ | |
71 | bswap((shdr).sh_type); \ | |
72 | bswap((shdr).sh_flags); \ | |
73 | bswap((shdr).sh_addr); \ | |
74 | bswap((shdr).sh_offset); \ | |
75 | bswap((shdr).sh_size); \ | |
76 | bswap((shdr).sh_link); \ | |
77 | bswap((shdr).sh_info); \ | |
78 | bswap((shdr).sh_addralign); \ | |
79 | bswap((shdr).sh_entsize); \ | |
80 | } while (0) | |
81 | ||
82 | #define bswap_ehdr(ehdr) \ | |
83 | do { \ | |
84 | bswap((ehdr).e_type); \ | |
85 | bswap((ehdr).e_machine); \ | |
86 | bswap((ehdr).e_version); \ | |
87 | bswap((ehdr).e_entry); \ | |
88 | bswap((ehdr).e_phoff); \ | |
89 | bswap((ehdr).e_shoff); \ | |
90 | bswap((ehdr).e_flags); \ | |
91 | bswap((ehdr).e_ehsize); \ | |
92 | bswap((ehdr).e_phentsize); \ | |
93 | bswap((ehdr).e_phnum); \ | |
94 | bswap((ehdr).e_shentsize); \ | |
95 | bswap((ehdr).e_shnum); \ | |
96 | bswap((ehdr).e_shstrndx); \ | |
97 | } while (0) | |
98 | ||
99 | #define copy_phdr(src_phdr, dst_phdr) \ | |
100 | do { \ | |
101 | (dst_phdr).p_type = (src_phdr).p_type; \ | |
102 | (dst_phdr).p_offset = (src_phdr).p_offset; \ | |
103 | (dst_phdr).p_filesz = (src_phdr).p_filesz; \ | |
104 | (dst_phdr).p_memsz = (src_phdr).p_memsz; \ | |
105 | (dst_phdr).p_align = (src_phdr).p_align; \ | |
106 | (dst_phdr).p_vaddr = (src_phdr).p_vaddr; \ | |
107 | } while (0) | |
108 | ||
109 | #define copy_shdr(src_shdr, dst_shdr) \ | |
110 | do { \ | |
111 | (dst_shdr).sh_name = (src_shdr).sh_name; \ | |
112 | (dst_shdr).sh_type = (src_shdr).sh_type; \ | |
113 | (dst_shdr).sh_flags = (src_shdr).sh_flags; \ | |
114 | (dst_shdr).sh_addr = (src_shdr).sh_addr; \ | |
115 | (dst_shdr).sh_offset = (src_shdr).sh_offset; \ | |
116 | (dst_shdr).sh_size = (src_shdr).sh_size; \ | |
117 | (dst_shdr).sh_link = (src_shdr).sh_link; \ | |
118 | (dst_shdr).sh_info = (src_shdr).sh_info; \ | |
119 | (dst_shdr).sh_addralign = (src_shdr).sh_addralign; \ | |
120 | (dst_shdr).sh_entsize = (src_shdr).sh_entsize; \ | |
121 | } while (0) | |
122 | ||
123 | #define copy_ehdr(src_ehdr, dst_ehdr) \ | |
124 | do { \ | |
125 | (dst_ehdr).e_type = (src_ehdr).e_type; \ | |
126 | (dst_ehdr).e_machine = (src_ehdr).e_machine; \ | |
127 | (dst_ehdr).e_version = (src_ehdr).e_version; \ | |
128 | (dst_ehdr).e_entry = (src_ehdr).e_entry; \ | |
129 | (dst_ehdr).e_phoff = (src_ehdr).e_phoff; \ | |
130 | (dst_ehdr).e_shoff = (src_ehdr).e_shoff; \ | |
131 | (dst_ehdr).e_flags = (src_ehdr).e_flags; \ | |
132 | (dst_ehdr).e_ehsize = (src_ehdr).e_ehsize; \ | |
133 | (dst_ehdr).e_phentsize = (src_ehdr).e_phentsize; \ | |
134 | (dst_ehdr).e_phnum = (src_ehdr).e_phnum; \ | |
135 | (dst_ehdr).e_shentsize = (src_ehdr).e_shentsize; \ | |
136 | (dst_ehdr).e_shnum = (src_ehdr).e_shnum; \ | |
137 | (dst_ehdr).e_shstrndx = (src_ehdr).e_shstrndx; \ | |
138 | } while (0) | |
139 | ||
140 | static inline | |
141 | int is_elf_32_bit(struct lttng_ust_elf *elf) | |
142 | { | |
143 | return elf->bitness == ELFCLASS32; | |
144 | } | |
145 | ||
146 | static inline | |
147 | int is_elf_native_endian(struct lttng_ust_elf *elf) | |
148 | { | |
149 | return elf->endianness == NATIVE_ELF_ENDIANNESS; | |
150 | } | |
151 | ||
152 | #endif |